2 * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3 * Author: Brendan Le Foll <brendan.le.foll@intel.com>
4 * Copyright (c) 2014 Intel Corporation.
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 #define SYSFS_CLASS_GPIO "/sys/class/gpio"
38 maa_gpio_get_valfp(maa_gpio_context *dev)
41 sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
43 dev->value_fp = open(bu, O_RDWR);
44 if (dev->value_fp == -1) {
45 return MAA_ERROR_INVALID_RESOURCE;
52 maa_gpio_init(int pin)
54 int pinm = maa_check_gpio(pin);
58 return maa_gpio_init_raw(pinm);
62 maa_gpio_init_raw(int pin)
71 maa_gpio_context* dev = (maa_gpio_context*) malloc(sizeof(maa_gpio_context));
72 memset(dev, 0, sizeof(maa_gpio_context));
76 if ((export_f = fopen(SYSFS_CLASS_GPIO "/export", "w")) == NULL) {
77 fprintf(stderr, "Failed to open export for writing!\n");
80 length = snprintf(bu, sizeof(bu), "%d", dev->pin);
81 fwrite(bu, sizeof(char), length, export_f);
88 maa_gpio_wait_interrupt(int fd)
93 // setup poll on POLLPRI
97 // do an initial read to clear interupt
101 return MAA_ERROR_INVALID_RESOURCE;
104 // Wait for it forever
105 int x = poll (&pfd, 1, -1);
107 // do a final read to clear interupt
114 maa_gpio_interrupt_handler(void* arg)
116 maa_gpio_context* dev = (maa_gpio_context*) arg;
119 // open gpio value with open(3)
121 sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
122 dev->isr_value_fp = open(bu, O_RDONLY);
125 ret = maa_gpio_wait_interrupt(dev->isr_value_fp);
126 if (ret == MAA_SUCCESS) {
129 // we must have got an error code so die nicely
130 close(dev->isr_value_fp);
137 maa_gpio_edge_mode(maa_gpio_context *dev, gpio_edge_t mode)
139 if (dev->value_fp != -1) {
140 close(dev->value_fp);
144 char filepath[MAX_SIZE];
145 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/edge", dev->pin);
148 if ((edge= fopen(filepath, "w")) == NULL) {
149 fprintf(stderr, "Failed to open edge for writing!\n");
150 return MAA_ERROR_INVALID_RESOURCE;
156 case MAA_GPIO_EDGE_NONE:
157 length = snprintf(bu, sizeof(bu), "none");
159 case MAA_GPIO_EDGE_BOTH:
160 length = snprintf(bu, sizeof(bu), "both");
162 case MAA_GPIO_EDGE_RISING:
163 length = snprintf(bu, sizeof(bu), "rising");
165 case MAA_GPIO_EDGE_FALLING:
166 length = snprintf(bu, sizeof(bu), "falling");
170 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
172 fwrite(bu, sizeof(char), length, edge);
179 maa_gpio_isr(maa_gpio_context *dev, gpio_edge_t mode, void (*fptr)(void))
181 maa_gpio_edge_mode(dev, mode);
183 pthread_create (&dev->thread_id, NULL, maa_gpio_interrupt_handler, (void *) dev);
189 maa_gpio_isr_exit(maa_gpio_context *dev)
191 maa_result_t ret = MAA_SUCCESS;
193 if (dev->thread_id == 0) {
197 if (pthread_kill(dev->thread_id) != 0) {
198 ret = MAA_ERROR_INVALID_HANDLE;
200 if (close(dev->isr_value_fp) != 0) {
201 ret = MAA_ERROR_INVALID_PARAMETER;
204 // this is only required if we had an isr setup
205 if (ret == MAA_SUCCESS) {
206 ret = maa_gpio_edge_mode(dev, MAA_GPIO_EDGE_NONE);
213 maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode)
215 if (dev->value_fp != -1) {
216 close(dev->value_fp);
220 char filepath[MAX_SIZE];
221 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/drive", dev->pin);
224 if ((drive = fopen(filepath, "w")) == NULL) {
225 fprintf(stderr, "Failed to open drive for writing!\n");
226 return MAA_ERROR_INVALID_RESOURCE;
232 case MAA_GPIO_STRONG:
233 length = snprintf(bu, sizeof(bu), "strong");
235 case MAA_GPIO_PULLUP:
236 length = snprintf(bu, sizeof(bu), "pullup");
238 case MAA_GPIO_PULLDOWN:
239 length = snprintf(bu, sizeof(bu), "pulldown");
242 length = snprintf(bu, sizeof(bu), "hiz");
246 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
248 fwrite(bu, sizeof(char), length, drive);
255 maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir)
258 return MAA_ERROR_INVALID_HANDLE;
260 if (dev->value_fp != -1) {
261 close(dev->value_fp);
264 char filepath[MAX_SIZE];
265 snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/direction", dev->pin);
268 if ((direction = fopen(filepath, "w")) == NULL) {
269 fprintf(stderr, "Failed to open direction for writing!\n");
270 return MAA_ERROR_INVALID_RESOURCE;
277 length = snprintf(bu, sizeof(bu), "out");
280 length = snprintf(bu, sizeof(bu), "in");
284 return MAA_ERROR_FEATURE_NOT_IMPLEMENTED;
286 fwrite(bu, sizeof(char), length, direction);
293 maa_gpio_read(maa_gpio_context *dev)
295 if (dev->value_fp == -1) {
296 if (maa_gpio_get_valfp(dev) != MAA_SUCCESS) {
297 fprintf(stderr, "Failed to get value file pointer\n");
301 // if value_fp is new this is pointless
302 lseek(dev->value_fp, 0, SEEK_SET);
305 if (read(dev->value_fp, bu, 2*sizeof(char)) != 2) {
306 fprintf(stderr, "Failed to read a sensible value from sysfs");
308 lseek(dev->value_fp, 0, SEEK_SET);
309 int ret = strtol(bu, NULL, 10);
315 maa_gpio_write(maa_gpio_context *dev, int value)
317 if (dev->value_fp == -1) {
318 maa_gpio_get_valfp(dev);
320 if (lseek(dev->value_fp, 0, SEEK_SET) == -1) {
321 return MAA_ERROR_INVALID_RESOURCE;
325 int length = snprintf(bu, sizeof(bu), "%d", value);
326 if (write(dev->value_fp, bu, length*sizeof(char)) == -1) {
327 return MAA_ERROR_INVALID_HANDLE;
334 maa_gpio_unexport(maa_gpio_context *dev)
338 if ((unexport_f = fopen(SYSFS_CLASS_GPIO "/unexport", "w")) == NULL) {
339 fprintf(stderr, "Failed to open unexport for writing!\n");
340 return MAA_ERROR_INVALID_RESOURCE;
344 int length = snprintf(bu, sizeof(bu), "%d", dev->pin);
345 fwrite(bu, sizeof(char), length, unexport_f);
348 maa_gpio_isr_exit(dev);
354 maa_gpio_close(maa_gpio_context *dev)
356 if (dev->value_fp != -1) {
357 close(dev->value_fp);
359 maa_gpio_unexport(dev);