blob: 0177c1701a77794303e92e0b85bb301fe1bf8a37 [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 James6dd74452015-08-30 22:34:31 -050046 //g_print("read error\n");
47 //TODO: error handling
Norman Jamese7594922015-08-27 14:25:24 -050048 }
49 if (buf[0]=='1') {
50 return 1;
51 }
52 return 0;
53
54}
55void gpio_clock_cycle(GPIO* gpio, int num_clks) {
56 int i=0;
57 for (i=0;i<num_clks;i++) {
58 gpio_writec(gpio,'0');
59 gpio_writec(gpio,'1');
60 }
61}
62
63// Gets the gpio device path from gpio manager object
64void gpio_init(GDBusConnection *connection, GPIO* gpio)
65{
66 GDBusProxy *proxy;
67 GError *error;
68 GVariant *result;
69
70 error = NULL;
71 g_assert_no_error (error);
72 error = NULL;
73 proxy = g_dbus_proxy_new_sync (connection,
74 G_DBUS_PROXY_FLAGS_NONE,
75 NULL, /* GDBusInterfaceInfo */
Norman Jamesddb97382015-08-27 21:31:31 -050076 "org.openbmc.managers.System", /* name */
77 "/org/openbmc/managers/System", /* object path */
78 "org.openbmc.managers.System", /* interface */
Norman Jamese7594922015-08-27 14:25:24 -050079 NULL, /* GCancellable */
80 &error);
81 g_assert_no_error (error);
82
83 result = g_dbus_proxy_call_sync (proxy,
Norman Jamesddb97382015-08-27 21:31:31 -050084 "gpioInit",
Norman Jamese7594922015-08-27 14:25:24 -050085 g_variant_new ("(s)", gpio->name),
86 G_DBUS_CALL_FLAGS_NONE,
87 -1,
88 NULL,
89 &error);
90
91 g_assert_no_error (error);
92 g_assert (result != NULL);
93 g_variant_get (result, "(&si&s)", &gpio->dev,&gpio->num,&gpio->direction);
94 g_print("GPIO Lookup: %s = %d,%s\n",gpio->name,gpio->num,gpio->direction);
95
96 //export and set direction
97 char dev[254];
98 char data[4];
99 sprintf(dev,"%s/export",gpio->dev);
100 int fd = open(dev, O_WRONLY);
101 sprintf(data,"%d",gpio->num);
102 write(fd,data,strlen(data));
103 close(fd);
104
105 sprintf(dev,"%s/gpio%d/direction",gpio->dev,gpio->num);
106 fd = open(dev,O_WRONLY);
107 write(fd,gpio->direction,strlen(gpio->direction));
108 close(fd);
109
110
111}
Norman James471ab592015-08-30 22:29:40 -0500112char* get_gpio_dev(GPIO* gpio)
113{
114 char* buf;
115 sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num);
116 return buf;
117}
118
Norman Jamese7594922015-08-27 14:25:24 -0500119int gpio_open(GPIO* gpio)
120{
121 // open gpio for writing or reading
122 char buf[254];
123 if (strcmp(gpio->direction,"in")==0)
124 {
125 sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num);
126 gpio->fd = open(buf, O_RDONLY);
127 }
128 else
129 {
130 sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num);
131 gpio->fd = open(buf, O_WRONLY);
132
133 }
134 if (gpio->fd == -1)
135 {
136 printf("error opening: %s\n",buf);
137 }
138 return gpio->fd;
139}
140
141void gpio_close(GPIO* gpio)
142{
143 close(gpio->fd);
144}