Patrick Williams | d6baab9 | 2016-08-24 14:05:55 -0500 | [diff] [blame] | 1 | #define _GNU_SOURCE |
| 2 | |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 3 | #include <stdint.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <unistd.h> |
| 9 | #include <argp.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <sys/mman.h> |
Brad Bishop | f6c8568 | 2016-06-27 11:56:39 -0400 | [diff] [blame] | 12 | #include "openbmc_intf.h" |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 13 | #include "gpio.h" |
Matt Spinler | 3a70e93 | 2018-08-07 14:16:47 -0500 | [diff] [blame^] | 14 | #include "gpio_json.h" |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 15 | |
Matt Spinler | 3a70e93 | 2018-08-07 14:16:47 -0500 | [diff] [blame^] | 16 | #define GPIO_BASE_PATH "/sys/class/gpio" |
| 17 | |
| 18 | cJSON* gpio_json = NULL; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 19 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 20 | int gpio_writec(GPIO* gpio, char value) |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 21 | { |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 22 | g_assert (gpio != NULL); |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 23 | int rc = GPIO_OK; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 24 | char buf[1]; |
| 25 | buf[0] = value; |
Yong Li | 86101ea | 2017-11-17 14:34:18 +0800 | [diff] [blame] | 26 | |
| 27 | if (lseek(gpio->fd, 0, SEEK_SET) == -1) |
| 28 | { |
| 29 | return GPIO_ERROR; |
| 30 | } |
| 31 | |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 32 | if (write(gpio->fd, buf, 1) != 1) |
| 33 | { |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 34 | rc = GPIO_WRITE_ERROR; |
| 35 | } |
| 36 | return rc; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 37 | } |
| 38 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 39 | int gpio_write(GPIO* gpio, uint8_t value) |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 40 | { |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 41 | g_assert (gpio != NULL); |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 42 | int rc = GPIO_OK; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 43 | char buf[1]; |
| 44 | buf[0] = '0'; |
| 45 | if (value==1) |
| 46 | { |
| 47 | buf[0]='1'; |
Patrick Williams | 3a8fa6e | 2016-08-24 14:04:22 -0500 | [diff] [blame] | 48 | } |
Yong Li | 86101ea | 2017-11-17 14:34:18 +0800 | [diff] [blame] | 49 | |
| 50 | if (lseek(gpio->fd, 0, SEEK_SET) == -1) |
| 51 | { |
| 52 | return GPIO_ERROR; |
| 53 | } |
| 54 | |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 55 | if (write(gpio->fd, buf, 1) != 1) |
| 56 | { |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 57 | rc = GPIO_WRITE_ERROR; |
| 58 | } |
| 59 | return rc; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 60 | } |
| 61 | |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 62 | int gpio_read(GPIO* gpio, uint8_t *value) |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 63 | { |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 64 | g_assert (gpio != NULL); |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 65 | char buf[1]; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 66 | int r = GPIO_OK; |
Norman James | 8abb50c | 2015-09-16 10:58:16 -0500 | [diff] [blame] | 67 | if (gpio->fd <= 0) |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 68 | { |
Patrick Williams | 3a8fa6e | 2016-08-24 14:04:22 -0500 | [diff] [blame] | 69 | r = GPIO_ERROR; |
Norman James | 8abb50c | 2015-09-16 10:58:16 -0500 | [diff] [blame] | 70 | } |
| 71 | else |
| 72 | { |
Yong Li | 86101ea | 2017-11-17 14:34:18 +0800 | [diff] [blame] | 73 | if (lseek(gpio->fd, 0, SEEK_SET) == -1) |
| 74 | { |
| 75 | return GPIO_ERROR; |
| 76 | } |
| 77 | |
Norman James | 8abb50c | 2015-09-16 10:58:16 -0500 | [diff] [blame] | 78 | if (read(gpio->fd,&buf,1) != 1) |
| 79 | { |
Norman James | 8abb50c | 2015-09-16 10:58:16 -0500 | [diff] [blame] | 80 | r = GPIO_READ_ERROR; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 81 | } else { |
Norman James | 8abb50c | 2015-09-16 10:58:16 -0500 | [diff] [blame] | 82 | if (buf[0]=='1') { |
| 83 | *value = 1; |
| 84 | } else { |
| 85 | *value = 0; |
| 86 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 87 | } |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 88 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 89 | return r; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 90 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 91 | int gpio_clock_cycle(GPIO* gpio, int num_clks) { |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 92 | g_assert (gpio != NULL); |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 93 | int i=0; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 94 | int r=GPIO_OK; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 95 | for (i=0;i<num_clks;i++) { |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 96 | if (gpio_writec(gpio,'0') == -1) { |
| 97 | r = GPIO_WRITE_ERROR; |
| 98 | break; |
| 99 | } |
| 100 | if (gpio_writec(gpio,'1') == -1) { |
| 101 | r = GPIO_WRITE_ERROR; |
| 102 | break; |
| 103 | } |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 104 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 105 | return r; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Matt Spinler | 3a70e93 | 2018-08-07 14:16:47 -0500 | [diff] [blame^] | 108 | int convert_gpio_to_num(const char* gpio) |
| 109 | { |
| 110 | /* TODO */ |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns the cJSON pointer to the GPIO definition |
| 116 | * for the GPIO passed in. |
| 117 | * |
| 118 | * @param[in] gpio_name - the GPIO name, like BMC_POWER_UP |
| 119 | * |
| 120 | * @return cJSON* - pointer to the cJSON object or NULL |
| 121 | * if not found. |
| 122 | */ |
| 123 | cJSON* get_gpio_def(const char* gpio_name) |
| 124 | { |
| 125 | if (gpio_json == NULL) |
| 126 | { |
| 127 | gpio_json = load_json(); |
| 128 | if (gpio_json == NULL) |
| 129 | { |
| 130 | return NULL; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | cJSON* gpio_defs = cJSON_GetObjectItem(gpio_json, "gpio_definitions"); |
| 135 | g_assert(gpio_defs != NULL); |
| 136 | |
| 137 | cJSON* def; |
| 138 | cJSON_ArrayForEach(def, gpio_defs) |
| 139 | { |
| 140 | cJSON* name = cJSON_GetObjectItem(def, "name"); |
| 141 | g_assert(name != NULL); |
| 142 | |
| 143 | if (strcmp(name->valuestring, gpio_name) == 0) |
| 144 | { |
| 145 | return def; |
| 146 | } |
| 147 | } |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Frees the gpio_json memory |
| 153 | * |
| 154 | * Can be called once when callers are done calling making calls |
| 155 | * to gpio_init() so that the JSON only needs to be loaded once. |
| 156 | */ |
| 157 | void gpio_inits_done() |
| 158 | { |
| 159 | if (gpio_json != NULL) |
| 160 | { |
| 161 | cJSON_Delete(gpio_json); |
| 162 | gpio_json = NULL; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Fills in the dev, direction, and num elements in |
| 168 | * the GPIO structure. |
| 169 | * |
| 170 | * @param gpio - the GPIO structure to fill in |
| 171 | * |
| 172 | * @return GPIO_OK if successful |
| 173 | */ |
| 174 | int gpio_get_params(GPIO* gpio) |
| 175 | { |
| 176 | gpio->dev = g_strdup(GPIO_BASE_PATH); |
| 177 | |
| 178 | const cJSON* def = get_gpio_def(gpio->name); |
| 179 | if (def == NULL) |
| 180 | { |
| 181 | fprintf(stderr, "Unable to find GPIO %s in the JSON\n", |
| 182 | gpio->name); |
| 183 | return GPIO_LOOKUP_ERROR; |
| 184 | } |
| 185 | |
| 186 | const cJSON* dir = cJSON_GetObjectItem(def, "direction"); |
| 187 | g_assert(dir != NULL); |
| 188 | gpio->direction = g_strdup(dir->valuestring); |
| 189 | |
| 190 | /* Must use either 'num', like 87, or 'pin', like "A5" */ |
| 191 | const cJSON* num = cJSON_GetObjectItem(def, "num"); |
| 192 | if ((num != NULL) && cJSON_IsNumber(num)) |
| 193 | { |
| 194 | gpio->num = num->valueint; |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | const cJSON* pin = cJSON_GetObjectItem(def, "pin"); |
| 199 | g_assert(pin != NULL); |
| 200 | |
| 201 | gpio->num = convert_gpio_to_num(pin->valuestring); |
| 202 | if (gpio->num < 0) |
| 203 | { |
| 204 | return GPIO_LOOKUP_ERROR; |
| 205 | } |
| 206 | } |
| 207 | return GPIO_OK; |
| 208 | } |
| 209 | |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 210 | // Gets the gpio device path from gpio manager object |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 211 | int gpio_init(GDBusConnection *connection, GPIO* gpio) |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 212 | { |
Matt Spinler | 3a70e93 | 2018-08-07 14:16:47 -0500 | [diff] [blame^] | 213 | int rc = gpio_get_params(gpio); |
| 214 | if (rc != GPIO_OK) |
| 215 | { |
| 216 | return rc; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 217 | } |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 218 | |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 219 | g_print("GPIO Lookup: %s = %d,%s\n",gpio->name,gpio->num,gpio->direction); |
Patrick Williams | 3a8fa6e | 2016-08-24 14:04:22 -0500 | [diff] [blame] | 220 | |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 221 | //export and set direction |
| 222 | char dev[254]; |
| 223 | char data[4]; |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 224 | int fd; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 225 | do { |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 226 | struct stat st; |
Patrick Williams | 3a8fa6e | 2016-08-24 14:04:22 -0500 | [diff] [blame] | 227 | |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 228 | sprintf(dev,"%s/gpio%d/value",gpio->dev,gpio->num); |
| 229 | //check if gpio is exported, if not export |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 230 | int result = stat(dev, &st); |
| 231 | if (result) |
| 232 | { |
| 233 | sprintf(dev,"%s/export",gpio->dev); |
| 234 | fd = open(dev, O_WRONLY); |
| 235 | if (fd == GPIO_ERROR) { |
| 236 | rc = GPIO_OPEN_ERROR; |
| 237 | break; |
Patrick Williams | 3a8fa6e | 2016-08-24 14:04:22 -0500 | [diff] [blame] | 238 | } |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 239 | sprintf(data,"%d",gpio->num); |
| 240 | rc = write(fd,data,strlen(data)); |
| 241 | close(fd); |
| 242 | if (rc != strlen(data)) { |
| 243 | rc = GPIO_WRITE_ERROR; |
| 244 | break; |
| 245 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 246 | } |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 247 | const char* file = "edge"; |
Patrick Williams | ed1368d | 2016-08-24 14:23:45 -0500 | [diff] [blame] | 248 | const char* direction = gpio->direction; |
| 249 | if (strcmp(direction, "in") == 0) |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 250 | { |
| 251 | file = "direction"; |
| 252 | } |
Patrick Williams | ed1368d | 2016-08-24 14:23:45 -0500 | [diff] [blame] | 253 | else if (strcmp(direction, "out") == 0) |
| 254 | { |
| 255 | file = "direction"; |
| 256 | |
| 257 | // Read current value, so we can set 'high' or 'low'. |
| 258 | // Setting direction directly to 'out' is the same as |
| 259 | // setting to 'low' which can change the value in the |
| 260 | // GPIO. |
| 261 | uint8_t value = 0; |
| 262 | rc = gpio_open(gpio); |
| 263 | if (rc) break; |
| 264 | rc = gpio_read(gpio, &value); |
| 265 | if (rc) break; |
| 266 | gpio_close(gpio); |
| 267 | |
| 268 | direction = (value ? "high" : "low"); |
| 269 | } |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 270 | sprintf(dev,"%s/gpio%d/%s",gpio->dev,gpio->num,file); |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 271 | fd = open(dev,O_WRONLY); |
| 272 | if (fd == GPIO_ERROR) { |
| 273 | rc = GPIO_WRITE_ERROR; |
| 274 | break; |
| 275 | } |
Patrick Williams | ed1368d | 2016-08-24 14:23:45 -0500 | [diff] [blame] | 276 | rc = write(fd,direction,strlen(direction)); |
| 277 | if (rc != strlen(direction)) { |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 278 | rc = GPIO_WRITE_ERROR; |
| 279 | break; |
| 280 | } |
| 281 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 282 | close(fd); |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 283 | rc = GPIO_OK; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 284 | } while(0); |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 285 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 286 | return rc; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 287 | } |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 288 | |
| 289 | |
| 290 | |
| 291 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 292 | char* get_gpio_dev(GPIO* gpio) |
| 293 | { |
| 294 | char* buf; |
Patrick Williams | d6baab9 | 2016-08-24 14:05:55 -0500 | [diff] [blame] | 295 | asprintf(&buf, "%s/gpio%d/value", gpio->dev, gpio->num); |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 296 | return buf; |
| 297 | } |
| 298 | |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 299 | int gpio_open_interrupt(GPIO* gpio, GIOFunc func, gpointer user_data) |
| 300 | { |
| 301 | int rc = GPIO_OK; |
Norman James | 02b77f3 | 2015-10-28 18:59:29 -0500 | [diff] [blame] | 302 | char buf[255]; |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 303 | sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); |
| 304 | gpio->fd = open(buf, O_RDONLY | O_NONBLOCK ); |
Norman James | 02b77f3 | 2015-10-28 18:59:29 -0500 | [diff] [blame] | 305 | gpio->irq_inited = false; |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 306 | if (gpio->fd == -1) |
| 307 | { |
| 308 | rc = GPIO_OPEN_ERROR; |
| 309 | } |
| 310 | else |
| 311 | { |
Norman James | 02b77f3 | 2015-10-28 18:59:29 -0500 | [diff] [blame] | 312 | GIOChannel* channel = g_io_channel_unix_new( gpio->fd); |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 313 | guint id = g_io_add_watch( channel, G_IO_PRI, func, user_data ); |
| 314 | } |
| 315 | return rc; |
| 316 | } |
| 317 | |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 318 | int gpio_open(GPIO* gpio) |
| 319 | { |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 320 | g_assert (gpio != NULL); |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 321 | // open gpio for writing or reading |
| 322 | char buf[254]; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 323 | int rc = 0; |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 324 | gpio->fd = -1; |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 325 | if (gpio->direction == NULL) { |
| 326 | return GPIO_OPEN_ERROR; |
| 327 | } |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 328 | if (strcmp(gpio->direction,"in")==0) |
| 329 | { |
| 330 | sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); |
| 331 | gpio->fd = open(buf, O_RDONLY); |
| 332 | } |
| 333 | else |
| 334 | { |
| 335 | sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); |
Norman James | 2557111 | 2015-12-15 09:36:28 -0600 | [diff] [blame] | 336 | gpio->fd = open(buf, O_RDWR); |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 337 | |
| 338 | } |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 339 | if (gpio->fd == -1) { |
| 340 | return GPIO_OPEN_ERROR; |
| 341 | } |
| 342 | return GPIO_OK; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void gpio_close(GPIO* gpio) |
| 346 | { |
| 347 | close(gpio->fd); |
| 348 | } |