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 | |
| 130 | sprintf(dev,"%s/gpio%d/direction",gpio->dev,gpio->num); |
| 131 | int result = stat(dev, &st); |
| 132 | if (result) |
| 133 | { |
| 134 | sprintf(dev,"%s/export",gpio->dev); |
| 135 | fd = open(dev, O_WRONLY); |
| 136 | if (fd == GPIO_ERROR) { |
| 137 | rc = GPIO_OPEN_ERROR; |
| 138 | break; |
| 139 | } |
| 140 | sprintf(data,"%d",gpio->num); |
| 141 | rc = write(fd,data,strlen(data)); |
| 142 | close(fd); |
| 143 | if (rc != strlen(data)) { |
| 144 | rc = GPIO_WRITE_ERROR; |
| 145 | break; |
| 146 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 147 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 148 | sprintf(dev,"%s/gpio%d/direction",gpio->dev,gpio->num); |
| 149 | fd = open(dev,O_WRONLY); |
| 150 | if (fd == GPIO_ERROR) { |
| 151 | rc = GPIO_WRITE_ERROR; |
| 152 | break; |
| 153 | } |
| 154 | rc = write(fd,gpio->direction,strlen(gpio->direction)); |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 155 | if (rc != strlen(gpio->direction)) { |
| 156 | rc = GPIO_WRITE_ERROR; |
| 157 | break; |
| 158 | } |
| 159 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 160 | close(fd); |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 161 | rc = 0; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 162 | } while(0); |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 163 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 164 | return rc; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 165 | } |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 166 | char* get_gpio_dev(GPIO* gpio) |
| 167 | { |
| 168 | char* buf; |
| 169 | sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); |
| 170 | return buf; |
| 171 | } |
| 172 | |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 173 | int gpio_open(GPIO* gpio) |
| 174 | { |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 175 | g_assert (gpio != NULL); |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 176 | // open gpio for writing or reading |
| 177 | char buf[254]; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 178 | int rc = 0; |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 179 | if (gpio->direction == NULL) { |
| 180 | return GPIO_OPEN_ERROR; |
| 181 | } |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 182 | if (strcmp(gpio->direction,"in")==0) |
| 183 | { |
| 184 | sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); |
| 185 | gpio->fd = open(buf, O_RDONLY); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); |
| 190 | gpio->fd = open(buf, O_WRONLY); |
| 191 | |
| 192 | } |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 193 | if (gpio->fd == -1) { |
| 194 | return GPIO_OPEN_ERROR; |
| 195 | } |
| 196 | return GPIO_OK; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void gpio_close(GPIO* gpio) |
| 200 | { |
| 201 | close(gpio->fd); |
| 202 | } |