Merge branch 'master' into for-linus
[sfrench/cifs-2.6.git] / arch / arm / mach-ep93xx / gpio.c
1 /*
2  * linux/arch/arm/mach-ep93xx/gpio.c
3  *
4  * Generic EP93xx GPIO handling
5  *
6  * Copyright (c) 2008 Ryan Mallon <ryan@bluewatersys.com>
7  *
8  * Based on code originally from:
9  *  linux/arch/arm/mach-ep93xx/core.c
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License version 2 as
13  *  published by the Free Software Foundation.
14  */
15
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/seq_file.h>
19 #include <linux/io.h>
20 #include <linux/gpio.h>
21 #include <linux/irq.h>
22
23 #include <mach/hardware.h>
24
25 struct ep93xx_gpio_chip {
26         struct gpio_chip        chip;
27
28         void __iomem            *data_reg;
29         void __iomem            *data_dir_reg;
30 };
31
32 #define to_ep93xx_gpio_chip(c) container_of(c, struct ep93xx_gpio_chip, chip)
33
34 /* From core.c */
35 extern void ep93xx_gpio_int_mask(unsigned line);
36 extern void ep93xx_gpio_update_int_params(unsigned port);
37
38 static int ep93xx_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
39 {
40         struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
41         unsigned long flags;
42         u8 v;
43
44         local_irq_save(flags);
45         v = __raw_readb(ep93xx_chip->data_dir_reg);
46         v &= ~(1 << offset);
47         __raw_writeb(v, ep93xx_chip->data_dir_reg);
48         local_irq_restore(flags);
49
50         return 0;
51 }
52
53 static int ep93xx_gpio_direction_output(struct gpio_chip *chip,
54                                         unsigned offset, int val)
55 {
56         struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
57         unsigned long flags;
58         int line;
59         u8 v;
60
61         local_irq_save(flags);
62
63         /* Set the value */
64         v = __raw_readb(ep93xx_chip->data_reg);
65         if (val)
66                 v |= (1 << offset);
67         else
68                 v &= ~(1 << offset);
69         __raw_writeb(v, ep93xx_chip->data_reg);
70
71         /* Drive as an output */
72         line = chip->base + offset;
73         if (line <= EP93XX_GPIO_LINE_MAX_IRQ) {
74                 /* Ports A/B/F */
75                 ep93xx_gpio_int_mask(line);
76                 ep93xx_gpio_update_int_params(line >> 3);
77         }
78
79         v = __raw_readb(ep93xx_chip->data_dir_reg);
80         v |= (1 << offset);
81         __raw_writeb(v, ep93xx_chip->data_dir_reg);
82
83         local_irq_restore(flags);
84
85         return 0;
86 }
87
88 static int ep93xx_gpio_get(struct gpio_chip *chip, unsigned offset)
89 {
90         struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
91
92         return !!(__raw_readb(ep93xx_chip->data_reg) & (1 << offset));
93 }
94
95 static void ep93xx_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
96 {
97         struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
98         unsigned long flags;
99         u8 v;
100
101         local_irq_save(flags);
102         v = __raw_readb(ep93xx_chip->data_reg);
103         if (val)
104                 v |= (1 << offset);
105         else
106                 v &= ~(1 << offset);
107         __raw_writeb(v, ep93xx_chip->data_reg);
108         local_irq_restore(flags);
109 }
110
111 static void ep93xx_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
112 {
113         struct ep93xx_gpio_chip *ep93xx_chip = to_ep93xx_gpio_chip(chip);
114         u8 data_reg, data_dir_reg;
115         int gpio, i;
116
117         data_reg = __raw_readb(ep93xx_chip->data_reg);
118         data_dir_reg = __raw_readb(ep93xx_chip->data_dir_reg);
119
120         gpio = ep93xx_chip->chip.base;
121         for (i = 0; i < chip->ngpio; i++, gpio++) {
122                 int is_out = data_dir_reg & (1 << i);
123
124                 seq_printf(s, " %s%d gpio-%-3d (%-12s) %s %s",
125                                 chip->label, i, gpio,
126                                 gpiochip_is_requested(chip, i) ? : "",
127                                 is_out ? "out" : "in ",
128                                 (data_reg & (1 << i)) ? "hi" : "lo");
129
130                 if (!is_out) {
131                         int irq = gpio_to_irq(gpio);
132                         struct irq_desc *desc = irq_desc + irq;
133
134                         if (irq >= 0 && desc->action) {
135                                 char *trigger;
136
137                                 switch (desc->status & IRQ_TYPE_SENSE_MASK) {
138                                 case IRQ_TYPE_NONE:
139                                         trigger = "(default)";
140                                         break;
141                                 case IRQ_TYPE_EDGE_FALLING:
142                                         trigger = "edge-falling";
143                                         break;
144                                 case IRQ_TYPE_EDGE_RISING:
145                                         trigger = "edge-rising";
146                                         break;
147                                 case IRQ_TYPE_EDGE_BOTH:
148                                         trigger = "edge-both";
149                                         break;
150                                 case IRQ_TYPE_LEVEL_HIGH:
151                                         trigger = "level-high";
152                                         break;
153                                 case IRQ_TYPE_LEVEL_LOW:
154                                         trigger = "level-low";
155                                         break;
156                                 default:
157                                         trigger = "?trigger?";
158                                         break;
159                                 }
160
161                                 seq_printf(s, " irq-%d %s%s",
162                                                 irq, trigger,
163                                                 (desc->status & IRQ_WAKEUP)
164                                                         ? " wakeup" : "");
165                         }
166                 }
167
168                 seq_printf(s, "\n");
169         }
170 }
171
172 #define EP93XX_GPIO_BANK(name, dr, ddr, base_gpio)                      \
173         {                                                               \
174                 .chip = {                                               \
175                         .label            = name,                       \
176                         .direction_input  = ep93xx_gpio_direction_input, \
177                         .direction_output = ep93xx_gpio_direction_output, \
178                         .get              = ep93xx_gpio_get,            \
179                         .set              = ep93xx_gpio_set,            \
180                         .dbg_show         = ep93xx_gpio_dbg_show,       \
181                         .base             = base_gpio,                  \
182                         .ngpio            = 8,                          \
183                 },                                                      \
184                 .data_reg       = EP93XX_GPIO_REG(dr),                  \
185                 .data_dir_reg   = EP93XX_GPIO_REG(ddr),                 \
186         }
187
188 static struct ep93xx_gpio_chip ep93xx_gpio_banks[] = {
189         EP93XX_GPIO_BANK("A", 0x00, 0x10, 0),
190         EP93XX_GPIO_BANK("B", 0x04, 0x14, 8),
191         EP93XX_GPIO_BANK("C", 0x08, 0x18, 40),
192         EP93XX_GPIO_BANK("D", 0x0c, 0x1c, 24),
193         EP93XX_GPIO_BANK("E", 0x20, 0x24, 32),
194         EP93XX_GPIO_BANK("F", 0x30, 0x34, 16),
195         EP93XX_GPIO_BANK("G", 0x38, 0x3c, 48),
196         EP93XX_GPIO_BANK("H", 0x40, 0x44, 56),
197 };
198
199 void __init ep93xx_gpio_init(void)
200 {
201         int i;
202
203         for (i = 0; i < ARRAY_SIZE(ep93xx_gpio_banks); i++)
204                 gpiochip_add(&ep93xx_gpio_banks[i].chip);
205 }