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