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" |
| 14 | |
| 15 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 16 | int gpio_writec(GPIO* gpio, char value) |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 17 | { |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 18 | g_assert (gpio != NULL); |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 19 | int rc = GPIO_OK; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 20 | char buf[1]; |
| 21 | buf[0] = value; |
Yong Li | 86101ea | 2017-11-17 14:34:18 +0800 | [diff] [blame] | 22 | |
| 23 | if (lseek(gpio->fd, 0, SEEK_SET) == -1) |
| 24 | { |
| 25 | return GPIO_ERROR; |
| 26 | } |
| 27 | |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 28 | if (write(gpio->fd, buf, 1) != 1) |
| 29 | { |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 30 | rc = GPIO_WRITE_ERROR; |
| 31 | } |
| 32 | return rc; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 33 | } |
| 34 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 35 | int gpio_write(GPIO* gpio, uint8_t value) |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 36 | { |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 37 | g_assert (gpio != NULL); |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 38 | int rc = GPIO_OK; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 39 | char buf[1]; |
| 40 | buf[0] = '0'; |
| 41 | if (value==1) |
| 42 | { |
| 43 | buf[0]='1'; |
Patrick Williams | 3a8fa6e | 2016-08-24 14:04:22 -0500 | [diff] [blame] | 44 | } |
Yong Li | 86101ea | 2017-11-17 14:34:18 +0800 | [diff] [blame] | 45 | |
| 46 | if (lseek(gpio->fd, 0, SEEK_SET) == -1) |
| 47 | { |
| 48 | return GPIO_ERROR; |
| 49 | } |
| 50 | |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 51 | if (write(gpio->fd, buf, 1) != 1) |
| 52 | { |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 53 | rc = GPIO_WRITE_ERROR; |
| 54 | } |
| 55 | return rc; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 56 | } |
| 57 | |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 58 | int gpio_read(GPIO* gpio, uint8_t *value) |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 59 | { |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 60 | g_assert (gpio != NULL); |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 61 | char buf[1]; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 62 | int r = GPIO_OK; |
Norman James | 8abb50c | 2015-09-16 10:58:16 -0500 | [diff] [blame] | 63 | if (gpio->fd <= 0) |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 64 | { |
Patrick Williams | 3a8fa6e | 2016-08-24 14:04:22 -0500 | [diff] [blame] | 65 | r = GPIO_ERROR; |
Norman James | 8abb50c | 2015-09-16 10:58:16 -0500 | [diff] [blame] | 66 | } |
| 67 | else |
| 68 | { |
Yong Li | 86101ea | 2017-11-17 14:34:18 +0800 | [diff] [blame] | 69 | if (lseek(gpio->fd, 0, SEEK_SET) == -1) |
| 70 | { |
| 71 | return GPIO_ERROR; |
| 72 | } |
| 73 | |
Norman James | 8abb50c | 2015-09-16 10:58:16 -0500 | [diff] [blame] | 74 | if (read(gpio->fd,&buf,1) != 1) |
| 75 | { |
Norman James | 8abb50c | 2015-09-16 10:58:16 -0500 | [diff] [blame] | 76 | r = GPIO_READ_ERROR; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 77 | } else { |
Norman James | 8abb50c | 2015-09-16 10:58:16 -0500 | [diff] [blame] | 78 | if (buf[0]=='1') { |
| 79 | *value = 1; |
| 80 | } else { |
| 81 | *value = 0; |
| 82 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 83 | } |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 84 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 85 | return r; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 86 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 87 | int gpio_clock_cycle(GPIO* gpio, int num_clks) { |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 88 | g_assert (gpio != NULL); |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 89 | int i=0; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 90 | int r=GPIO_OK; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 91 | for (i=0;i<num_clks;i++) { |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 92 | if (gpio_writec(gpio,'0') == -1) { |
| 93 | r = GPIO_WRITE_ERROR; |
| 94 | break; |
| 95 | } |
| 96 | if (gpio_writec(gpio,'1') == -1) { |
| 97 | r = GPIO_WRITE_ERROR; |
| 98 | break; |
| 99 | } |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 100 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 101 | return r; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Gets the gpio device path from gpio manager object |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 105 | int gpio_init(GDBusConnection *connection, GPIO* gpio) |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 106 | { |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 107 | int rc = GPIO_OK; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 108 | GDBusProxy *proxy; |
| 109 | GError *error; |
| 110 | GVariant *result; |
| 111 | |
| 112 | error = NULL; |
| 113 | g_assert_no_error (error); |
| 114 | error = NULL; |
| 115 | proxy = g_dbus_proxy_new_sync (connection, |
| 116 | G_DBUS_PROXY_FLAGS_NONE, |
| 117 | NULL, /* GDBusInterfaceInfo */ |
Norman James | ddb9738 | 2015-08-27 21:31:31 -0500 | [diff] [blame] | 118 | "org.openbmc.managers.System", /* name */ |
| 119 | "/org/openbmc/managers/System", /* object path */ |
| 120 | "org.openbmc.managers.System", /* interface */ |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 121 | NULL, /* GCancellable */ |
| 122 | &error); |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 123 | if (error != NULL) { |
| 124 | return GPIO_LOOKUP_ERROR; |
| 125 | } |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 126 | |
| 127 | result = g_dbus_proxy_call_sync (proxy, |
Norman James | ddb9738 | 2015-08-27 21:31:31 -0500 | [diff] [blame] | 128 | "gpioInit", |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 129 | g_variant_new ("(s)", gpio->name), |
| 130 | G_DBUS_CALL_FLAGS_NONE, |
| 131 | -1, |
| 132 | NULL, |
| 133 | &error); |
Patrick Williams | 3a8fa6e | 2016-08-24 14:04:22 -0500 | [diff] [blame] | 134 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 135 | if (error != NULL) { |
| 136 | return GPIO_LOOKUP_ERROR; |
| 137 | } |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 138 | g_assert (result != NULL); |
| 139 | g_variant_get (result, "(&si&s)", &gpio->dev,&gpio->num,&gpio->direction); |
| 140 | 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] | 141 | |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 142 | //export and set direction |
| 143 | char dev[254]; |
| 144 | char data[4]; |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 145 | int fd; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 146 | do { |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 147 | struct stat st; |
Patrick Williams | 3a8fa6e | 2016-08-24 14:04:22 -0500 | [diff] [blame] | 148 | |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 149 | sprintf(dev,"%s/gpio%d/value",gpio->dev,gpio->num); |
| 150 | //check if gpio is exported, if not export |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 151 | int result = stat(dev, &st); |
| 152 | if (result) |
| 153 | { |
| 154 | sprintf(dev,"%s/export",gpio->dev); |
| 155 | fd = open(dev, O_WRONLY); |
| 156 | if (fd == GPIO_ERROR) { |
| 157 | rc = GPIO_OPEN_ERROR; |
| 158 | break; |
Patrick Williams | 3a8fa6e | 2016-08-24 14:04:22 -0500 | [diff] [blame] | 159 | } |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 160 | sprintf(data,"%d",gpio->num); |
| 161 | rc = write(fd,data,strlen(data)); |
| 162 | close(fd); |
| 163 | if (rc != strlen(data)) { |
| 164 | rc = GPIO_WRITE_ERROR; |
| 165 | break; |
| 166 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 167 | } |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 168 | const char* file = "edge"; |
Patrick Williams | ed1368d | 2016-08-24 14:23:45 -0500 | [diff] [blame] | 169 | const char* direction = gpio->direction; |
| 170 | if (strcmp(direction, "in") == 0) |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 171 | { |
| 172 | file = "direction"; |
| 173 | } |
Patrick Williams | ed1368d | 2016-08-24 14:23:45 -0500 | [diff] [blame] | 174 | else if (strcmp(direction, "out") == 0) |
| 175 | { |
| 176 | file = "direction"; |
| 177 | |
| 178 | // Read current value, so we can set 'high' or 'low'. |
| 179 | // Setting direction directly to 'out' is the same as |
| 180 | // setting to 'low' which can change the value in the |
| 181 | // GPIO. |
| 182 | uint8_t value = 0; |
| 183 | rc = gpio_open(gpio); |
| 184 | if (rc) break; |
| 185 | rc = gpio_read(gpio, &value); |
| 186 | if (rc) break; |
| 187 | gpio_close(gpio); |
| 188 | |
| 189 | direction = (value ? "high" : "low"); |
| 190 | } |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 191 | sprintf(dev,"%s/gpio%d/%s",gpio->dev,gpio->num,file); |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 192 | fd = open(dev,O_WRONLY); |
| 193 | if (fd == GPIO_ERROR) { |
| 194 | rc = GPIO_WRITE_ERROR; |
| 195 | break; |
| 196 | } |
Patrick Williams | ed1368d | 2016-08-24 14:23:45 -0500 | [diff] [blame] | 197 | rc = write(fd,direction,strlen(direction)); |
| 198 | if (rc != strlen(direction)) { |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 199 | rc = GPIO_WRITE_ERROR; |
| 200 | break; |
| 201 | } |
| 202 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 203 | close(fd); |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 204 | rc = GPIO_OK; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 205 | } while(0); |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 206 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 207 | return rc; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 208 | } |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 209 | |
| 210 | |
| 211 | |
| 212 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 213 | char* get_gpio_dev(GPIO* gpio) |
| 214 | { |
| 215 | char* buf; |
Patrick Williams | d6baab9 | 2016-08-24 14:05:55 -0500 | [diff] [blame] | 216 | asprintf(&buf, "%s/gpio%d/value", gpio->dev, gpio->num); |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 217 | return buf; |
| 218 | } |
| 219 | |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 220 | int gpio_open_interrupt(GPIO* gpio, GIOFunc func, gpointer user_data) |
| 221 | { |
| 222 | int rc = GPIO_OK; |
Norman James | 02b77f3 | 2015-10-28 18:59:29 -0500 | [diff] [blame] | 223 | char buf[255]; |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 224 | sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); |
| 225 | gpio->fd = open(buf, O_RDONLY | O_NONBLOCK ); |
Norman James | 02b77f3 | 2015-10-28 18:59:29 -0500 | [diff] [blame] | 226 | gpio->irq_inited = false; |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 227 | if (gpio->fd == -1) |
| 228 | { |
| 229 | rc = GPIO_OPEN_ERROR; |
| 230 | } |
| 231 | else |
| 232 | { |
Norman James | 02b77f3 | 2015-10-28 18:59:29 -0500 | [diff] [blame] | 233 | GIOChannel* channel = g_io_channel_unix_new( gpio->fd); |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 234 | guint id = g_io_add_watch( channel, G_IO_PRI, func, user_data ); |
| 235 | } |
| 236 | return rc; |
| 237 | } |
| 238 | |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 239 | int gpio_open(GPIO* gpio) |
| 240 | { |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 241 | g_assert (gpio != NULL); |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 242 | // open gpio for writing or reading |
| 243 | char buf[254]; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 244 | int rc = 0; |
Norman James | 5a7cc8d | 2015-10-06 12:31:06 -0500 | [diff] [blame] | 245 | gpio->fd = -1; |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 246 | if (gpio->direction == NULL) { |
| 247 | return GPIO_OPEN_ERROR; |
| 248 | } |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 249 | if (strcmp(gpio->direction,"in")==0) |
| 250 | { |
| 251 | sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); |
| 252 | gpio->fd = open(buf, O_RDONLY); |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); |
Norman James | 2557111 | 2015-12-15 09:36:28 -0600 | [diff] [blame] | 257 | gpio->fd = open(buf, O_RDWR); |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 258 | |
| 259 | } |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 260 | if (gpio->fd == -1) { |
| 261 | return GPIO_OPEN_ERROR; |
| 262 | } |
| 263 | return GPIO_OK; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void gpio_close(GPIO* gpio) |
| 267 | { |
| 268 | close(gpio->fd); |
| 269 | } |