blob: d03fdb484096e72246172ebe470755a9ec6670ca [file] [log] [blame]
Norman Jamesfa2e76e2015-08-26 17:41:20 -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>
10
11
12#include "interfaces/power_control.h"
13#include "objects/openbmc_utilities.h"
14
15void gpio_writec(GPIO* gpio, char value)
16{
17 char buf[1];
18 buf[0] = value;
19 if (write(gpio->fd, buf, 1) != 1)
20 {
21 //TODO: error handling
22 printf("Write error\n");
23 }
24}
25
26void gpio_write(GPIO* gpio, uint8_t value)
27{
28 char buf[1];
29 buf[0] = '0';
30 if (value==1)
31 {
32 buf[0]='1';
33 }
34 if (write(gpio->fd, buf, 1) != 1)
35 {
36 //TODO: error handling
37 printf("write error\n");
38 }
39}
40
41uint8_t gpio_read(GPIO* gpio)
42{
43 char buf[1];
44 if (read(gpio->fd,&buf,1) != 1)
45 {
46 //TODO: error hjandling
47 printf("read error\n");
48 }
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 */
76 "org.openbmc.managers.Gpios", /* name */
77 "/org/openbmc/managers/Gpios", /* object path */
78 "org.openbmc.managers.Gpios", /* interface */
79 NULL, /* GCancellable */
80 &error);
81 g_assert_no_error (error);
82
83 result = g_dbus_proxy_call_sync (proxy,
84 "init",
85 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}
112int gpio_open(GPIO* gpio)
113{
114 // open gpio for writing or reading
115 char buf[254];
116 if (strcmp(gpio->direction,"in")==0)
117 {
118 sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num);
119 gpio->fd = open(buf, O_RDONLY);
120 }
121 else
122 {
123 sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num);
124 gpio->fd = open(buf, O_WRONLY);
125
126 }
127 if (gpio->fd == -1)
128 {
129 printf("error opening: %s\n",buf);
130 }
131 return gpio->fd;
132}
133
134void gpio_close(GPIO* gpio)
135{
136 close(gpio->fd);
137}