Fix memory detection and hcd compilation, thanks Gabor ! (closes #1813)
[openwrt.git] / target / linux / adm5120-2.6 / files / arch / mips / adm5120 / gpio.c
1 /*
2 * $Id$
3 *
4 * ADM5120 GPIO support
5 *
6 * Copyright (C) 2007 OpenWrt.org
7 * Copyright (C) 2007 Gabor Juhos <juhosg@freemail.hu>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23 *
24 */
25
26 #include <linux/autoconf.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/module.h>
30 #include <linux/delay.h>
31 #include <linux/platform_device.h>
32
33 #include <asm/addrspace.h>
34 #include <asm/io.h>
35 #include <asm/gpio.h>
36
37 #include <asm/mach-adm5120/adm5120_defs.h>
38 #include <asm/mach-adm5120/adm5120_info.h>
39 #include <asm/mach-adm5120/adm5120_switch.h>
40
41 typedef void __iomem * gpio_reg_t;
42 #define GPIO_READ(r) readl((r))
43 #define GPIO_WRITE(v,r) writel((v),(r))
44 #define GPIO_REG(r) (gpio_reg_t)(KSEG1ADDR(ADM5120_SWITCH_BASE)+r)
45
46 struct adm5120_gpio_line {
47 u32 flags;
48 const char *label;
49 };
50
51 #define GPIO_FLAG_VALID 0x01
52 #define GPIO_FLAG_USED 0x02
53
54 struct led_desc {
55 gpio_reg_t reg; /* LED register address */
56 u8 iv_shift; /* shift amount for input bit */
57 u8 mode_shift; /* shift amount for mode bits */
58 };
59
60 #define LED_DESC(_port,_led) { \
61 .reg = GPIO_REG(SWITCH_REG_PORT0_LED+_port*4), \
62 .iv_shift = LED0_IV_SHIFT+_led, \
63 .mode_shift = _led*4 \
64 }
65
66 static struct led_desc led_table[15] = {
67 LED_DESC(0, 0), LED_DESC(0, 1), LED_DESC(0, 2),
68 LED_DESC(1, 0), LED_DESC(1, 1), LED_DESC(1, 2),
69 LED_DESC(2, 0), LED_DESC(2, 1), LED_DESC(2, 2),
70 LED_DESC(3, 0), LED_DESC(3, 1), LED_DESC(3, 2),
71 LED_DESC(4, 0), LED_DESC(4, 1), LED_DESC(4, 2)
72 };
73
74 static struct adm5120_gpio_line adm5120_gpio_map[ADM5120_GPIO_COUNT] = {
75 [ADM5120_GPIO_PIN0] = {.flags = GPIO_FLAG_VALID},
76 [ADM5120_GPIO_PIN1] = {.flags = GPIO_FLAG_VALID},
77 [ADM5120_GPIO_PIN2] = {.flags = GPIO_FLAG_VALID},
78 [ADM5120_GPIO_PIN3] = {.flags = GPIO_FLAG_VALID},
79 [ADM5120_GPIO_PIN4] = {.flags = GPIO_FLAG_VALID},
80 [ADM5120_GPIO_PIN5] = {.flags = GPIO_FLAG_VALID},
81 [ADM5120_GPIO_PIN6] = {.flags = GPIO_FLAG_VALID},
82 [ADM5120_GPIO_PIN7] = {.flags = GPIO_FLAG_VALID},
83 [ADM5120_GPIO_P0L0] = {.flags = GPIO_FLAG_VALID},
84 [ADM5120_GPIO_P0L1] = {.flags = GPIO_FLAG_VALID},
85 [ADM5120_GPIO_P0L2] = {.flags = GPIO_FLAG_VALID},
86 [ADM5120_GPIO_P1L0] = {.flags = GPIO_FLAG_VALID},
87 [ADM5120_GPIO_P1L1] = {.flags = GPIO_FLAG_VALID},
88 [ADM5120_GPIO_P1L2] = {.flags = GPIO_FLAG_VALID},
89 [ADM5120_GPIO_P2L0] = {.flags = GPIO_FLAG_VALID},
90 [ADM5120_GPIO_P2L1] = {.flags = GPIO_FLAG_VALID},
91 [ADM5120_GPIO_P2L2] = {.flags = GPIO_FLAG_VALID},
92 [ADM5120_GPIO_P3L0] = {.flags = GPIO_FLAG_VALID},
93 [ADM5120_GPIO_P3L1] = {.flags = GPIO_FLAG_VALID},
94 [ADM5120_GPIO_P3L2] = {.flags = GPIO_FLAG_VALID},
95 [ADM5120_GPIO_P4L0] = {.flags = GPIO_FLAG_VALID},
96 [ADM5120_GPIO_P4L1] = {.flags = GPIO_FLAG_VALID},
97 [ADM5120_GPIO_P4L2] = {.flags = GPIO_FLAG_VALID}
98 };
99
100 #define gpio_is_invalid(g) ( \
101 (g) > ADM5120_GPIO_MAX || \
102 ((adm5120_gpio_map[(g)].flags & GPIO_FLAG_VALID) == 0) \
103 )
104
105 #define gpio_is_used(g) ((adm5120_gpio_map[(g)].flags & GPIO_FLAG_USED) != 0)
106
107 /*
108 * Helpers for GPIO lines in GPIO_CONF0 register
109 */
110 #define PIN_IM(p) ((1 << GPIO_CONF0_IM_SHIFT) << p)
111 #define PIN_IV(p) ((1 << GPIO_CONF0_IV_SHIFT) << p)
112 #define PIN_OE(p) ((1 << GPIO_CONF0_OE_SHIFT) << p)
113 #define PIN_OV(p) ((1 << GPIO_CONF0_OV_SHIFT) << p)
114
115 static inline int pins_direction_input(unsigned pin)
116 {
117 gpio_reg_t *reg;
118 u32 t;
119
120 reg = GPIO_REG(SWITCH_REG_GPIO_CONF0);
121
122 t = GPIO_READ(reg);
123 t &= ~(PIN_OE(pin));
124 t |= PIN_IM(pin);
125 GPIO_WRITE(t,reg);
126
127 return 0;
128 }
129
130 static inline int pins_direction_output(unsigned pin, int value)
131 {
132 gpio_reg_t *reg;
133 u32 t;
134
135 reg = GPIO_REG(SWITCH_REG_GPIO_CONF0);
136
137 t = GPIO_READ(reg);
138 t &= ~(PIN_IM(pin) | PIN_OV(pin));
139 t |= PIN_OE(pin);
140
141 if (value)
142 t |= PIN_OV(pin);
143
144 GPIO_WRITE(t,reg);
145
146 return 0;
147 }
148
149 static inline int pins_get_value(unsigned pin)
150 {
151 gpio_reg_t *reg;
152 u32 t;
153
154 reg = GPIO_REG(SWITCH_REG_GPIO_CONF0);
155
156 t = GPIO_READ(reg);
157 if ((t & PIN_IM(pin)) != 0)
158 t &= PIN_IV(pin);
159 else
160 t &= PIN_OV(pin);
161
162 return (t) ? 1 : 0;
163 }
164
165 static inline void pins_set_value(unsigned pin, int value)
166 {
167 gpio_reg_t *reg;
168 u32 t;
169
170 reg = GPIO_REG(SWITCH_REG_GPIO_CONF0);
171
172 t = GPIO_READ(reg);
173 if (value == 0)
174 t &= ~(PIN_OV(pin));
175 else
176 t |= PIN_OV(pin);
177
178 GPIO_WRITE(t,reg);
179 }
180
181 /*
182 * Helpers for GPIO lines in PORTx_LED registers
183 */
184 static inline int leds_direction_input(unsigned led)
185 {
186 gpio_reg_t *reg;
187 u32 t;
188
189 reg = led_table[led].reg;
190 t = GPIO_READ(reg);
191 t &= ~(LED_MODE_MASK << led_table[led].mode_shift);
192 GPIO_WRITE(t,reg);
193
194 return 0;
195 }
196
197 static inline int leds_direction_output(unsigned led, int value)
198 {
199 gpio_reg_t *reg;
200 u32 t, s;
201
202 reg = led_table[led].reg;
203 s = led_table[led].mode_shift;
204
205 t = GPIO_READ(reg);
206 t &= ~(LED_MODE_MASK << s);
207 if (value)
208 t |= (LED_MODE_OUT_HIGH << s);
209 else
210 t |= (LED_MODE_OUT_LOW << s);
211
212 GPIO_WRITE(t,reg);
213
214 return 0;
215 }
216
217 static inline int leds_get_value(unsigned led)
218 {
219 gpio_reg_t *reg;
220 u32 t, m;
221
222 reg = led_table[led].reg;
223
224 t = GPIO_READ(reg);
225 m = (t >> led_table[led].mode_shift) & LED_MODE_MASK;
226 if (m == LED_MODE_INPUT)
227 return (t >> led_table[led].iv_shift) & 1;
228
229 if (m == LED_MODE_OUT_LOW)
230 return 0;
231
232 return 1;
233 }
234
235 static inline void leds_set_value(unsigned led, int value)
236 {
237 gpio_reg_t *reg;
238 u32 s,t;
239
240 reg = led_table[led].reg;
241 s = led_table[led].mode_shift;
242
243 t = GPIO_READ(reg);
244 t &= ~(LED_MODE_MASK << s);
245 if (value)
246 t |= (LED_MODE_OUT_HIGH << s);
247 else
248 t |= (LED_MODE_OUT_LOW << s);
249
250 GPIO_WRITE(t,reg);
251 }
252
253 /*
254 * Main GPIO support routines
255 */
256 int adm5120_gpio_direction_input(unsigned gpio)
257 {
258 if (gpio_is_invalid(gpio))
259 return -EINVAL;
260
261 if (gpio < ADM5120_GPIO_P0L0)
262 return pins_direction_input(gpio);
263
264 gpio -= ADM5120_GPIO_P0L0;
265 return leds_direction_input(gpio);
266 }
267
268 int adm5120_gpio_direction_output(unsigned gpio, int value)
269 {
270 if (gpio_is_invalid(gpio))
271 return -EINVAL;
272
273 if (gpio < ADM5120_GPIO_P0L0)
274 return pins_direction_output(gpio, value);
275
276 gpio -= ADM5120_GPIO_P0L0;
277 return leds_direction_output(gpio, value);
278 }
279
280 int adm5120_gpio_get_value(unsigned gpio)
281 {
282 if (gpio < ADM5120_GPIO_P0L0)
283 return pins_get_value(gpio);
284
285 gpio -= ADM5120_GPIO_P0L0;
286 return leds_get_value(gpio);
287 }
288
289 void adm5120_gpio_set_value(unsigned gpio, int value)
290 {
291 if (gpio < ADM5120_GPIO_P0L0) {
292 pins_set_value(gpio, value);
293 return;
294 }
295
296 gpio -= ADM5120_GPIO_P0L0;
297 leds_set_value(gpio, value);
298 }
299
300 int adm5120_gpio_request(unsigned gpio, const char *label)
301 {
302 if (gpio_is_invalid(gpio))
303 return -EINVAL;
304
305 if (gpio_is_used(gpio))
306 return -EBUSY;
307
308 adm5120_gpio_map[gpio].flags |= GPIO_FLAG_USED;
309 adm5120_gpio_map[gpio].label = label;
310
311 return 0;
312 }
313
314 void adm5120_gpio_free(unsigned gpio)
315 {
316 if (gpio_is_invalid(gpio))
317 return;
318
319 adm5120_gpio_map[gpio].flags &= ~GPIO_FLAG_USED;
320 adm5120_gpio_map[gpio].label = NULL;
321 }
322
323 int adm5120_gpio_to_irq(unsigned gpio)
324 {
325 /* FIXME: not yet implemented */
326 return -EINVAL;
327 }
328
329 int adm5120_irq_to_gpio(unsigned irq)
330 {
331 /* FIXME: not yet implemented */
332 return -EINVAL;
333 }
334
335 static int __init adm5120_gpio_init(void)
336 {
337 int i;
338
339 if (adm5120_package_pqfp()) {
340 /* GPIO pins 4-7 are unavailable in ADM5120P */
341 for (i=ADM5120_GPIO_PIN4; i<=ADM5120_GPIO_PIN7; i++)
342 adm5120_gpio_map[i].flags &= ~GPIO_FLAG_VALID;
343 }
344
345 return 0;
346 }
347
348 subsys_initcall(adm5120_gpio_init);
349
350 EXPORT_SYMBOL(adm5120_gpio_direction_output);
351 EXPORT_SYMBOL(adm5120_gpio_direction_input);
352 EXPORT_SYMBOL(adm5120_gpio_get_value);
353 EXPORT_SYMBOL(adm5120_gpio_set_value);
354 EXPORT_SYMBOL(adm5120_gpio_request);
355 EXPORT_SYMBOL(adm5120_gpio_free);
356 EXPORT_SYMBOL(adm5120_gpio_to_irq);
357 EXPORT_SYMBOL(adm5120_irq_to_gpio);
This page took 0.061735 seconds and 5 git commands to generate.