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 | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 16 | int rc = GPIO_OK; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 17 | char buf[1]; |
| 18 | buf[0] = value; |
| 19 | if (write(gpio->fd, buf, 1) != 1) |
| 20 | { |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 21 | rc = GPIO_WRITE_ERROR; |
| 22 | } |
| 23 | return rc; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 24 | } |
| 25 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 26 | int gpio_write(GPIO* gpio, uint8_t value) |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 27 | { |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 28 | int rc = GPIO_OK; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 29 | char buf[1]; |
| 30 | buf[0] = '0'; |
| 31 | if (value==1) |
| 32 | { |
| 33 | buf[0]='1'; |
| 34 | } |
| 35 | if (write(gpio->fd, buf, 1) != 1) |
| 36 | { |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 37 | rc = GPIO_WRITE_ERROR; |
| 38 | } |
| 39 | return rc; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 40 | } |
| 41 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 42 | int gpio_read(GPIO* gpio, uint8_t* value) |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 43 | { |
| 44 | char buf[1]; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 45 | int r = GPIO_OK; |
Norman James | 8abb50c | 2015-09-16 10:58:16 -0500 | [diff] [blame] | 46 | if (gpio->fd <= 0) |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 47 | { |
Norman James | 8abb50c | 2015-09-16 10:58:16 -0500 | [diff] [blame] | 48 | r = GPIO_ERROR; |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | if (read(gpio->fd,&buf,1) != 1) |
| 53 | { |
| 54 | g_print("here1\n"); |
| 55 | r = GPIO_READ_ERROR; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 56 | } else { |
Norman James | 8abb50c | 2015-09-16 10:58:16 -0500 | [diff] [blame] | 57 | g_print("here2\n"); |
| 58 | if (buf[0]=='1') { |
| 59 | *value = 1; |
| 60 | } else { |
| 61 | *value = 0; |
| 62 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 63 | } |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 64 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 65 | return r; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 66 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 67 | int gpio_clock_cycle(GPIO* gpio, int num_clks) { |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 68 | int i=0; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 69 | int r=GPIO_OK; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 70 | for (i=0;i<num_clks;i++) { |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 71 | if (gpio_writec(gpio,'0') == -1) { |
| 72 | r = GPIO_WRITE_ERROR; |
| 73 | break; |
| 74 | } |
| 75 | if (gpio_writec(gpio,'1') == -1) { |
| 76 | r = GPIO_WRITE_ERROR; |
| 77 | break; |
| 78 | } |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 79 | } |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 80 | return r; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // Gets the gpio device path from gpio manager object |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 84 | int gpio_init(GDBusConnection *connection, GPIO* gpio) |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 85 | { |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 86 | int rc = GPIO_OK; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 87 | GDBusProxy *proxy; |
| 88 | GError *error; |
| 89 | GVariant *result; |
| 90 | |
| 91 | error = NULL; |
| 92 | g_assert_no_error (error); |
| 93 | error = NULL; |
| 94 | proxy = g_dbus_proxy_new_sync (connection, |
| 95 | G_DBUS_PROXY_FLAGS_NONE, |
| 96 | NULL, /* GDBusInterfaceInfo */ |
Norman James | ddb9738 | 2015-08-27 21:31:31 -0500 | [diff] [blame] | 97 | "org.openbmc.managers.System", /* name */ |
| 98 | "/org/openbmc/managers/System", /* object path */ |
| 99 | "org.openbmc.managers.System", /* interface */ |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 100 | NULL, /* GCancellable */ |
| 101 | &error); |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 102 | if (error != NULL) { |
| 103 | return GPIO_LOOKUP_ERROR; |
| 104 | } |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 105 | |
| 106 | result = g_dbus_proxy_call_sync (proxy, |
Norman James | ddb9738 | 2015-08-27 21:31:31 -0500 | [diff] [blame] | 107 | "gpioInit", |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 108 | g_variant_new ("(s)", gpio->name), |
| 109 | G_DBUS_CALL_FLAGS_NONE, |
| 110 | -1, |
| 111 | NULL, |
| 112 | &error); |
| 113 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 114 | if (error != NULL) { |
| 115 | return GPIO_LOOKUP_ERROR; |
| 116 | } |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 117 | g_assert (result != NULL); |
| 118 | g_variant_get (result, "(&si&s)", &gpio->dev,&gpio->num,&gpio->direction); |
| 119 | g_print("GPIO Lookup: %s = %d,%s\n",gpio->name,gpio->num,gpio->direction); |
| 120 | |
| 121 | //export and set direction |
| 122 | char dev[254]; |
| 123 | char data[4]; |
| 124 | sprintf(dev,"%s/export",gpio->dev); |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 125 | do { |
| 126 | int fd = open(dev, O_WRONLY); |
| 127 | if (fd == GPIO_ERROR) { |
| 128 | rc = GPIO_OPEN_ERROR; |
| 129 | break; |
| 130 | } |
| 131 | sprintf(data,"%d",gpio->num); |
| 132 | rc = write(fd,data,strlen(data)); |
| 133 | close(fd); |
| 134 | if (rc == GPIO_ERROR) { |
| 135 | rc = GPIO_WRITE_ERROR; |
| 136 | break; |
| 137 | } |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 138 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 139 | sprintf(dev,"%s/gpio%d/direction",gpio->dev,gpio->num); |
| 140 | fd = open(dev,O_WRONLY); |
| 141 | if (fd == GPIO_ERROR) { |
| 142 | rc = GPIO_WRITE_ERROR; |
| 143 | break; |
| 144 | } |
| 145 | rc = write(fd,gpio->direction,strlen(gpio->direction)); |
| 146 | close(fd); |
| 147 | } while(0); |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 148 | |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 149 | return rc; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 150 | } |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 151 | char* get_gpio_dev(GPIO* gpio) |
| 152 | { |
| 153 | char* buf; |
| 154 | sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); |
| 155 | return buf; |
| 156 | } |
| 157 | |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 158 | int gpio_open(GPIO* gpio) |
| 159 | { |
| 160 | // open gpio for writing or reading |
| 161 | char buf[254]; |
Norman James | 32e74e2 | 2015-09-15 21:28:06 -0500 | [diff] [blame] | 162 | int rc = 0; |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 163 | if (strcmp(gpio->direction,"in")==0) |
| 164 | { |
| 165 | sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); |
| 166 | gpio->fd = open(buf, O_RDONLY); |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); |
| 171 | gpio->fd = open(buf, O_WRONLY); |
| 172 | |
| 173 | } |
Norman James | e759492 | 2015-08-27 14:25:24 -0500 | [diff] [blame] | 174 | return gpio->fd; |
| 175 | } |
| 176 | |
| 177 | void gpio_close(GPIO* gpio) |
| 178 | { |
| 179 | close(gpio->fd); |
| 180 | } |