summaryrefslogtreecommitdiff
path: root/firmware/target/hosted/samsungypr/gpio-ypr.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/hosted/samsungypr/gpio-ypr.c')
-rw-r--r--firmware/target/hosted/samsungypr/gpio-ypr.c94
1 files changed, 90 insertions, 4 deletions
diff --git a/firmware/target/hosted/samsungypr/gpio-ypr.c b/firmware/target/hosted/samsungypr/gpio-ypr.c
index e5abc4cdc9..d6a19da34f 100644
--- a/firmware/target/hosted/samsungypr/gpio-ypr.c
+++ b/firmware/target/hosted/samsungypr/gpio-ypr.c
@@ -8,7 +8,7 @@
8 * 8 *
9 * Module wrapper for GPIO, using kernel module of Samsung YP-R0/YP-R1 9 * Module wrapper for GPIO, using kernel module of Samsung YP-R0/YP-R1
10 * 10 *
11 * Copyright (c) 2011 Lorenzo Miori 11 * Copyright (c) 2011-2015 Lorenzo Miori
12 * 12 *
13 * This program is free software; you can redistribute it and/or 13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License 14 * modify it under the terms of the GNU General Public License
@@ -23,16 +23,42 @@
23#include <stdio.h> 23#include <stdio.h>
24#include <unistd.h> 24#include <unistd.h>
25#include <fcntl.h> 25#include <fcntl.h>
26#include <gpio-ypr.h> /* includes common ioctl device definitions */
27#include <sys/ioctl.h> 26#include <sys/ioctl.h>
28 27
28#include "panic.h"
29#include "gpio-ypr.h" /* includes common ioctl device definitions */
30
29static int gpio_dev = 0; 31static int gpio_dev = 0;
30 32
33#ifdef GPIO_DEBUG
34// 3 banks of 32 pins
35static const char *pin_use[3][32];
36
37void gpio_acquire(unsigned bank, unsigned pin, const char *name)
38{
39 if(pin_use[bank][pin] != NULL && pin_use[bank][pin] != name)
40 panicf("acquire B%dP%02d for %s, was %s!", bank, pin, name, pin_use[bank][pin]);
41 pin_use[bank][pin] = name;
42}
43
44void gpio_release(unsigned bank, unsigned pin, const char *name)
45{
46 if(pin_use[bank][pin] != NULL && pin_use[bank][pin] != name)
47 panicf("release B%dP%02d for %s: was %s!", bank, pin, name, pin_use[bank][pin]);
48 pin_use[bank][pin] = NULL;
49}
50
51const char *gpio_blame(unsigned bank, unsigned pin)
52{
53 return pin_use[bank][pin];
54}
55#endif
56
31void gpio_init(void) 57void gpio_init(void)
32{ 58{
33 gpio_dev = open(GPIO_DEVICE, O_RDONLY); 59 gpio_dev = open(GPIO_DEVICE, O_RDONLY);
34 if (gpio_dev < 0) 60 if (gpio_dev < 0)
35 printf("GPIO device open error!"); 61 panicf("GPIO device open error!");
36} 62}
37 63
38void gpio_close(void) 64void gpio_close(void)
@@ -41,8 +67,68 @@ void gpio_close(void)
41 close(gpio_dev); 67 close(gpio_dev);
42} 68}
43 69
44int gpio_control(int request, int num, int mode, int val) 70static int gpio_control(int request, int num, int mode, int val)
45{ 71{
46 struct gpio_info r = { .num = num, .mode = mode, .val = val, }; 72 struct gpio_info r = { .num = num, .mode = mode, .val = val, };
47 return ioctl(gpio_dev, request, &r); 73 return ioctl(gpio_dev, request, &r);
48} 74}
75
76void gpio_set_iomux(int pin, int iomux)
77{
78 if (gpio_control(DEV_CTRL_GPIO_SET_MUX, pin, iomux, 0) != 0)
79 {
80 panicf("Unable to set iomux to pin %d", pin);
81 }
82}
83
84void gpio_free_iomux(int pin, int iomux)
85{
86 if (gpio_control(DEV_CTRL_GPIO_UNSET_MUX, pin, iomux, 0) != 0)
87 {
88 panicf("Unable to free iomux to pin %d", pin);
89 }
90}
91
92void gpio_set_pad(int pin, int type)
93{
94 if (gpio_control(DEV_CTRL_GPIO_SET_TYPE, pin, type, 0) != 0)
95 {
96 panicf("Unable to set pad to pin %d", pin);
97 }
98}
99
100void gpio_direction_output(int pin)
101{
102 if (gpio_control(DEV_CTRL_GPIO_SET_OUTPUT, pin, 0, 0) != 0)
103 {
104 panicf("Unable to set output direction to pin %d", pin);
105 }
106}
107
108void gpio_direction_input(int pin)
109{
110 if (gpio_control(DEV_CTRL_GPIO_SET_INPUT, pin, 0, 0) != 0)
111 {
112 panicf("Unable to set input direction to pin %d", pin);
113 }
114}
115
116bool gpio_get(int pin)
117{
118 return gpio_control(DEV_CTRL_GPIO_GET_VAL, pin, 0, 0);
119}
120
121void gpio_set(int pin, bool value)
122{
123 int ret = -1;
124
125 if (value)
126 ret = gpio_control(DEV_CTRL_GPIO_SET_HIGH, pin, 0, 0);
127 else
128 ret = gpio_control(DEV_CTRL_GPIO_SET_LOW, pin, 0, 0);
129
130 if (ret != 0)
131 {
132 panicf("Unable to set input direction to pin %d", pin);
133 }
134}