blob: 53f882d1f4919789e7cba11e0df91bcf55cddcbc [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 James471ab592015-08-30 22:29:40 -050010#include "interfaces/eventlog.h"
Norman Jamese7594922015-08-27 14:25:24 -050011#include "gpio.h"
12
13
14void gpio_writec(GPIO* gpio, char value)
15{
16 char buf[1];
17 buf[0] = value;
18 if (write(gpio->fd, buf, 1) != 1)
19 {
20 //TODO: error handling
21 printf("Write error\n");
22 }
23}
24
25void gpio_write(GPIO* gpio, uint8_t value)
26{
27 char buf[1];
28 buf[0] = '0';
29 if (value==1)
30 {
31 buf[0]='1';
32 }
33 if (write(gpio->fd, buf, 1) != 1)
34 {
35 //TODO: error handling
36 printf("write error\n");
37 }
38}
39
40uint8_t gpio_read(GPIO* gpio)
41{
42 char buf[1];
Norman James471ab592015-08-30 22:29:40 -050043
Norman Jamese7594922015-08-27 14:25:24 -050044 if (read(gpio->fd,&buf,1) != 1)
45 {
Norman James471ab592015-08-30 22:29:40 -050046 g_print("read error\n");
Norman Jamese7594922015-08-27 14:25:24 -050047 }
48 if (buf[0]=='1') {
49 return 1;
50 }
51 return 0;
52
53}
54void gpio_clock_cycle(GPIO* gpio, int num_clks) {
55 int i=0;
56 for (i=0;i<num_clks;i++) {
57 gpio_writec(gpio,'0');
58 gpio_writec(gpio,'1');
59 }
60}
61
62// Gets the gpio device path from gpio manager object
63void gpio_init(GDBusConnection *connection, GPIO* gpio)
64{
65 GDBusProxy *proxy;
66 GError *error;
67 GVariant *result;
68
69 error = NULL;
70 g_assert_no_error (error);
71 error = NULL;
72 proxy = g_dbus_proxy_new_sync (connection,
73 G_DBUS_PROXY_FLAGS_NONE,
74 NULL, /* GDBusInterfaceInfo */
Norman Jamesddb97382015-08-27 21:31:31 -050075 "org.openbmc.managers.System", /* name */
76 "/org/openbmc/managers/System", /* object path */
77 "org.openbmc.managers.System", /* interface */
Norman Jamese7594922015-08-27 14:25:24 -050078 NULL, /* GCancellable */
79 &error);
80 g_assert_no_error (error);
81
82 result = g_dbus_proxy_call_sync (proxy,
Norman Jamesddb97382015-08-27 21:31:31 -050083 "gpioInit",
Norman Jamese7594922015-08-27 14:25:24 -050084 g_variant_new ("(s)", gpio->name),
85 G_DBUS_CALL_FLAGS_NONE,
86 -1,
87 NULL,
88 &error);
89
90 g_assert_no_error (error);
91 g_assert (result != NULL);
92 g_variant_get (result, "(&si&s)", &gpio->dev,&gpio->num,&gpio->direction);
93 g_print("GPIO Lookup: %s = %d,%s\n",gpio->name,gpio->num,gpio->direction);
94
95 //export and set direction
96 char dev[254];
97 char data[4];
98 sprintf(dev,"%s/export",gpio->dev);
99 int fd = open(dev, O_WRONLY);
100 sprintf(data,"%d",gpio->num);
101 write(fd,data,strlen(data));
102 close(fd);
103
104 sprintf(dev,"%s/gpio%d/direction",gpio->dev,gpio->num);
105 fd = open(dev,O_WRONLY);
106 write(fd,gpio->direction,strlen(gpio->direction));
107 close(fd);
108
109
110}
Norman James471ab592015-08-30 22:29:40 -0500111char* get_gpio_dev(GPIO* gpio)
112{
113 char* buf;
114 sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num);
115 return buf;
116}
117
Norman Jamese7594922015-08-27 14:25:24 -0500118int gpio_open(GPIO* gpio)
119{
120 // open gpio for writing or reading
121 char buf[254];
122 if (strcmp(gpio->direction,"in")==0)
123 {
124 sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num);
125 gpio->fd = open(buf, O_RDONLY);
126 }
127 else
128 {
129 sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num);
130 gpio->fd = open(buf, O_WRONLY);
131
132 }
133 if (gpio->fd == -1)
134 {
135 printf("error opening: %s\n",buf);
136 }
137 return gpio->fd;
138}
139
140void gpio_close(GPIO* gpio)
141{
142 close(gpio->fd);
143}