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