blob: 992745f36ee83ec8c1be9c1611e7b286b4801f52 [file] [log] [blame]
Norman Jamese7594922015-08-27 14:25:24 -05001#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 James362a80f2015-09-14 14:04:39 -050010#include "interfaces/openbmc_intf.h"
Norman Jamese7594922015-08-27 14:25:24 -050011#include "gpio.h"
12
13
Norman James32e74e22015-09-15 21:28:06 -050014int gpio_writec(GPIO* gpio, char value)
Norman Jamese7594922015-08-27 14:25:24 -050015{
Norman James32e74e22015-09-15 21:28:06 -050016 int rc = GPIO_OK;
Norman Jamese7594922015-08-27 14:25:24 -050017 char buf[1];
18 buf[0] = value;
19 if (write(gpio->fd, buf, 1) != 1)
20 {
Norman James32e74e22015-09-15 21:28:06 -050021 rc = GPIO_WRITE_ERROR;
22 }
23 return rc;
Norman Jamese7594922015-08-27 14:25:24 -050024}
25
Norman James32e74e22015-09-15 21:28:06 -050026int gpio_write(GPIO* gpio, uint8_t value)
Norman Jamese7594922015-08-27 14:25:24 -050027{
Norman James32e74e22015-09-15 21:28:06 -050028 int rc = GPIO_OK;
Norman Jamese7594922015-08-27 14:25:24 -050029 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 James32e74e22015-09-15 21:28:06 -050037 rc = GPIO_WRITE_ERROR;
38 }
39 return rc;
Norman Jamese7594922015-08-27 14:25:24 -050040}
41
Norman James32e74e22015-09-15 21:28:06 -050042int gpio_read(GPIO* gpio, uint8_t* value)
Norman Jamese7594922015-08-27 14:25:24 -050043{
44 char buf[1];
Norman James32e74e22015-09-15 21:28:06 -050045 int r = GPIO_OK;
Norman James8abb50c2015-09-16 10:58:16 -050046 if (gpio->fd <= 0)
Norman Jamese7594922015-08-27 14:25:24 -050047 {
Norman James8abb50c2015-09-16 10:58:16 -050048 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 James32e74e22015-09-15 21:28:06 -050056 } else {
Norman James8abb50c2015-09-16 10:58:16 -050057 g_print("here2\n");
58 if (buf[0]=='1') {
59 *value = 1;
60 } else {
61 *value = 0;
62 }
Norman James32e74e22015-09-15 21:28:06 -050063 }
Norman Jamese7594922015-08-27 14:25:24 -050064 }
Norman James32e74e22015-09-15 21:28:06 -050065 return r;
Norman Jamese7594922015-08-27 14:25:24 -050066}
Norman James32e74e22015-09-15 21:28:06 -050067int gpio_clock_cycle(GPIO* gpio, int num_clks) {
Norman Jamese7594922015-08-27 14:25:24 -050068 int i=0;
Norman James32e74e22015-09-15 21:28:06 -050069 int r=GPIO_OK;
Norman Jamese7594922015-08-27 14:25:24 -050070 for (i=0;i<num_clks;i++) {
Norman James32e74e22015-09-15 21:28:06 -050071 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 Jamese7594922015-08-27 14:25:24 -050079 }
Norman James32e74e22015-09-15 21:28:06 -050080 return r;
Norman Jamese7594922015-08-27 14:25:24 -050081}
82
83// Gets the gpio device path from gpio manager object
Norman James32e74e22015-09-15 21:28:06 -050084int gpio_init(GDBusConnection *connection, GPIO* gpio)
Norman Jamese7594922015-08-27 14:25:24 -050085{
Norman James32e74e22015-09-15 21:28:06 -050086 int rc = GPIO_OK;
Norman Jamese7594922015-08-27 14:25:24 -050087 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 Jamesddb97382015-08-27 21:31:31 -050097 "org.openbmc.managers.System", /* name */
98 "/org/openbmc/managers/System", /* object path */
99 "org.openbmc.managers.System", /* interface */
Norman Jamese7594922015-08-27 14:25:24 -0500100 NULL, /* GCancellable */
101 &error);
Norman James32e74e22015-09-15 21:28:06 -0500102 if (error != NULL) {
103 return GPIO_LOOKUP_ERROR;
104 }
Norman Jamese7594922015-08-27 14:25:24 -0500105
106 result = g_dbus_proxy_call_sync (proxy,
Norman Jamesddb97382015-08-27 21:31:31 -0500107 "gpioInit",
Norman Jamese7594922015-08-27 14:25:24 -0500108 g_variant_new ("(s)", gpio->name),
109 G_DBUS_CALL_FLAGS_NONE,
110 -1,
111 NULL,
112 &error);
113
Norman James32e74e22015-09-15 21:28:06 -0500114 if (error != NULL) {
115 return GPIO_LOOKUP_ERROR;
116 }
Norman Jamese7594922015-08-27 14:25:24 -0500117 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 James32e74e22015-09-15 21:28:06 -0500125 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 Jamese7594922015-08-27 14:25:24 -0500138
Norman James32e74e22015-09-15 21:28:06 -0500139 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 Jamese7594922015-08-27 14:25:24 -0500148
Norman James32e74e22015-09-15 21:28:06 -0500149 return rc;
Norman Jamese7594922015-08-27 14:25:24 -0500150}
Norman James471ab592015-08-30 22:29:40 -0500151char* 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 Jamese7594922015-08-27 14:25:24 -0500158int gpio_open(GPIO* gpio)
159{
160 // open gpio for writing or reading
161 char buf[254];
Norman James32e74e22015-09-15 21:28:06 -0500162 int rc = 0;
Norman Jamese7594922015-08-27 14:25:24 -0500163 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 Jamese7594922015-08-27 14:25:24 -0500174 return gpio->fd;
175}
176
177void gpio_close(GPIO* gpio)
178{
179 close(gpio->fd);
180}