[ubicom32]: move new files out from platform support patch
[openwrt.git] / target / linux / ubicom32 / files / arch / ubicom32 / mach-common / ubi32-gpio.c
1 /*
2  * arch/ubicom32/mach-common/ubi32-gpio.c
3  *   Ubicom gpio driver
4  *
5  * (C) Copyright 2009, Ubicom, Inc.
6  *
7  * This file is part of the Ubicom32 Linux Kernel Port.
8  *
9  * The Ubicom32 Linux Kernel Port is free software: you can redistribute
10  * it and/or modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation, either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * The Ubicom32 Linux Kernel Port is distributed in the hope that it
15  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
16  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
17  * the GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with the Ubicom32 Linux Kernel Port.  If not,
21  * see <http://www.gnu.org/licenses/>.
22  *
23  * Ubicom32 implementation derived from (with many thanks):
24  *   arch/m68knommu
25  *   arch/blackfin
26  *   arch/parisc
27  */
28
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/io.h>
34 #include <linux/gpio.h>
35 #include <linux/irq.h>
36 #include <linux/version.h>
37
38 #if defined(CONFIG_PROC_FS)
39 #include <linux/proc_fs.h>
40 #endif
41
42 #include <linux/io.h>
43 #include <asm/ip5000.h>
44 #include <linux/gpio.h>
45
46 #define UBI_GPIO_CHECK_RANGE     0  /* !0 enables range checking */
47
48
49 /*
50  * Each I/O port can be configured to operate in one of several
51  * functional modes. One of these modes is GPIO, which causes the
52  * entire port to function as a GPIO port.  Since the various port
53  * registers serve the system with other important functions, such as
54  * ethernet, serial, USB, etc., it isn't advantageous to set any of
55  * the ports to be entirely dedicated for GPIO use.  The processor
56  * alternatively allows individual bits of a port to be assigned to be
57  * used as GPIO independently from the overall port function.  This
58  * bit-by-bit assignment is selected by setting the corresponding bit
59  * in the port's gpio_mask register.  When set, the selected bit is
60  * then enabled as a GPIO.  If the corresponding bit is set in the
61  * gpio_ctl register of the port, the bit is configured as a GPIO
62  * output.  Otherwise, it is an input.
63  *
64  * NOTE: This driver uses the bit-by-bit GPIO function assignment
65  * exclusively and *never* sets the port function registers to the
66  * GPIO function.
67  *
68  * GPIO is not the main function of any of the I/O ports.  The port
69  * bit widths are variable from one port to the next, determined by
70  * the more common I/O functions of the ports.  For simplicity, this
71  * driver assumes all the ports are 32 bits wide regardless of the
72  * real bit width of the port.  GPIO bits are numbered from zero to
73  * MAX_UBICOM_GPIOS.  Within a port, the least significant bit is
74  * numbered bit zero, the most significant is bit 31.  Since the ports
75  * are considered logically contiguous, GPIO #32 is the zeroth bit in
76  * port #1, and so on.  Due to the hardware definition, there are
77  * large gaps in the GPIO numbers representing real pins.
78  *
79  * NOTE: It is up to the programmer to refer to the processor data
80  * sheet to determine which bits in which ports can be accessed and
81  * used for GPIO.
82  *
83  */
84
85
86 /* There are 9 ports, A through I. Not all 32 bits in each
87  * port can be a GPIO, but we pretend they are.  Its up to the
88  * programmer to refer to the processor data sheet.
89  */
90 #define MAX_UBICOM_GPIOS   (9 * 32) /* ARCH_NR_GPIOS */
91 #define NUM_GPIO_PORTS     (gpio_bank(MAX_UBICOM_GPIOS))
92
93
94 /* GPIO reservation bit map array */
95 static int reserved_gpio_map[NUM_GPIO_PORTS];
96
97
98 /* Array of hardware io_port addresses */
99 static struct ubicom32_io_port *gpio_bank_addr[NUM_GPIO_PORTS] =
100 {
101         UBICOM32_IO_PORT(RA),
102         UBICOM32_IO_PORT(RB),
103         UBICOM32_IO_PORT(RC),
104         UBICOM32_IO_PORT(RD),
105         UBICOM32_IO_PORT(RE),
106         UBICOM32_IO_PORT(RF),
107         UBICOM32_IO_PORT(RG),
108         UBICOM32_IO_PORT(RH),
109         UBICOM32_IO_PORT(RI)
110 };
111
112
113 struct ubi_gpio_chip {
114         /*
115          * Right now, nothing else lives here.
116          */
117         struct gpio_chip gpio_chip;
118 };
119
120
121 #if UBI_GPIO_CHECK_RANGE
122 inline int check_gpio(unsigned gpio)
123 {
124         if (gpio >= MAX_UBICOM_GPIOS)
125                 return -EINVAL;
126         return 0;
127 }
128 #else
129 #define check_gpio(n)   (0)
130 #endif
131
132 /*
133  * ubi_gpio_get_port
134  *      Get the IO port associated with a certain gpio
135  */
136 struct ubicom32_io_port *ubi_gpio_get_port(unsigned gpio)
137 {
138         if (gpio_bank(gpio) > NUM_GPIO_PORTS) {
139                 return NULL;
140         }
141         return gpio_bank_addr[gpio_bank(gpio)];
142 }
143
144 /*
145  * ubi_gpio_error()
146  */
147 static void ubi_gpio_error(unsigned gpio)
148 {
149         printk(KERN_ERR "ubicom-gpio: GPIO %d wasn't requested!\n", gpio);
150 }
151
152 /*
153  * ubi_port_setup()
154  */
155 static void ubi_port_setup(unsigned gpio, unsigned short usage)
156 {
157         if (!check_gpio(gpio)) {
158                 if (usage) {
159                         UBICOM32_GPIO_ENABLE(gpio);
160                 } else {
161                         UBICOM32_GPIO_DISABLE(gpio);
162                 }
163         }
164 }
165
166 /*
167  * ubi_gpio_request()
168  */
169 static int ubi_gpio_request(struct gpio_chip *chip, unsigned gpio)
170 {
171         unsigned long flags;
172
173         if (check_gpio(gpio) < 0)
174                 return -EINVAL;
175
176         local_irq_save(flags);
177
178         if (unlikely(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
179                 printk(KERN_ERR "ubi-gpio: GPIO %d is already reserved!\n",
180                        gpio);
181                 local_irq_restore(flags);
182                 return -EBUSY;
183         }
184
185         reserved_gpio_map[gpio_bank(gpio)] |= gpio_bit(gpio);
186
187         ubi_port_setup(gpio, 1);
188
189         local_irq_restore(flags);
190
191         return 0;
192 }
193
194 /*
195  * ubi_gpio_free()
196  */
197 static void ubi_gpio_free(struct gpio_chip *chip, unsigned gpio)
198 {
199         unsigned long flags;
200
201         if (check_gpio(gpio) < 0)
202                 return;
203
204         local_irq_save(flags);
205
206         if (unlikely(!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio)))) {
207                 ubi_gpio_error(gpio);
208                 local_irq_restore(flags);
209                 return;
210         }
211
212         /* Assert the pin is no longer claimed */
213         reserved_gpio_map[gpio_bank(gpio)] &= ~gpio_bit(gpio);
214
215         /* Revert port bit to use specified by port->function */
216         ubi_port_setup(gpio, 0);
217
218         local_irq_restore(flags);
219 }
220
221 /*
222  * ubi_gpio_direction_input()
223  */
224 static int ubi_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
225 {
226         unsigned long flags;
227
228         if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
229                 ubi_gpio_error(gpio);
230                 return -EINVAL;
231         }
232
233         local_irq_save(flags);
234
235         /* Configure pin as gpio */
236         ubi_port_setup(gpio, 1);
237
238         /* Assert pin is an input */
239         UBICOM32_GPIO_SET_PIN_INPUT(gpio);
240
241         local_irq_restore(flags);
242
243         return 0;
244 }
245
246
247 /*
248  * ubi_gpio_direction_output()
249  */
250 static int ubi_gpio_direction_output(struct gpio_chip *chip,
251                                      unsigned gpio, int value)
252 {
253         unsigned long flags;
254
255         if (!(reserved_gpio_map[gpio_bank(gpio)] & gpio_bit(gpio))) {
256                 ubi_gpio_error(gpio);
257                 return -EINVAL;
258         }
259
260         local_irq_save(flags);
261
262         /* Configure pin as gpio and set initial value in gpio_out register
263          * so that when we enable it as an output, it will have the correct
264          * initial value.
265          */
266         ubi_port_setup(gpio, 1);
267         if (value) {
268                 UBICOM32_GPIO_SET_PIN_HIGH(gpio);
269         } else {
270                 UBICOM32_GPIO_SET_PIN_LOW(gpio);
271         }
272
273         /* Enable the pin as an output */
274         UBICOM32_GPIO_SET_PIN_OUTPUT(gpio);
275
276         local_irq_restore(flags);
277
278         return 0;
279 }
280
281
282 /*
283  * ubi_gpio_get_value()
284  */
285 static int ubi_gpio_get_value(struct gpio_chip *chip, unsigned gpio)
286 {
287         return 0 != (gpio_bank_addr[gpio_bank(gpio)]->gpio_in & gpio_bit(gpio));
288 }
289
290
291 /*
292  * ubi_gpio_set_value()
293  */
294 static void ubi_gpio_set_value(struct gpio_chip *chip, unsigned gpio,
295                                int arg)
296 {
297         unsigned long flags;
298         local_irq_save(flags);
299
300         if (arg) {
301                 UBICOM32_GPIO_SET_PIN_HIGH(gpio);
302         } else {
303                 UBICOM32_GPIO_SET_PIN_LOW(gpio);
304         }
305
306         local_irq_restore(flags);
307 }
308
309
310 /*
311  * ubi_gpio_to_irq()
312  */
313 static int ubi_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
314 {
315         return gpio_to_irq(gpio);
316 }
317
318
319 /*
320  * ubi_gpio_init()
321  */
322 int __init ubi_gpio_init(void)
323 {
324         int k;
325         int status;
326         struct ubi_gpio_chip *chip;
327         struct gpio_chip *gc;
328
329         printk(KERN_INFO "Ubicom GPIO Controller\n");
330
331         chip = kzalloc(sizeof(struct ubi_gpio_chip), GFP_KERNEL);
332         if (chip == NULL)
333                 return -ENOMEM;
334
335         gc = &chip->gpio_chip;
336         gc->request          = ubi_gpio_request;
337         gc->free             = ubi_gpio_free;
338         gc->to_irq           = ubi_gpio_to_irq;
339         gc->direction_input  = ubi_gpio_direction_input;
340         gc->direction_output = ubi_gpio_direction_output;
341         gc->get              = ubi_gpio_get_value;
342         gc->set              = ubi_gpio_set_value;
343         gc->can_sleep        = 0;
344         gc->base             = 0;
345         gc->ngpio            = MAX_UBICOM_GPIOS; /* ARCH_NR_GPIOS - 1 */
346         gc->label            = "ubi_gpio";
347
348         status = gpiochip_add(gc);
349         if (status != 0) {
350                 kfree(chip);
351                 return status;
352         }
353
354         /* Assert all pins are free */
355         for (k = 0; k < NUM_GPIO_PORTS; k++) {
356                 reserved_gpio_map[k] = 0;
357         }
358
359         return 0;
360 }
361
362 #if defined(CONFIG_PROC_FS)
363 /*
364  * ubi_get_gpio_dir()
365  */
366 static int ubi_get_gpio_dir(unsigned gpio)
367 {
368         if (gpio_bank_addr[gpio_bank(gpio)]->gpio_ctl & gpio_bit(gpio))
369                 return 1;
370         else
371                 return 0;
372 }
373
374 /*
375  * gpio_proc_read()
376  */
377 static int ubi_gpio_proc_read(char *buf, char **start, off_t offset,
378                           int len, int *unused_i, void *unused_v)
379 {
380         int c, outlen = 0;
381
382         for (c = 0; c < MAX_UBICOM_GPIOS; c++) {
383                 if (!check_gpio(c) &&
384                     (reserved_gpio_map[gpio_bank(c)] & gpio_bit(c))) {
385                         len = sprintf(buf, "GPIO_%d:\t\tGPIO %s\n", c,
386                                       ubi_get_gpio_dir(c) ? "OUTPUT" : "INPUT");
387                 } else {
388                         continue;
389                 }
390
391                 buf += len;
392                 outlen += len;
393         }
394         return outlen;
395 }
396
397 /*
398  * ubi_gpio_register_proc()
399  */
400 static __init int ubi_gpio_register_proc(void)
401 {
402         struct proc_dir_entry *proc_gpio;
403
404         proc_gpio = create_proc_entry("gpio", S_IRUGO, NULL);
405         if (proc_gpio)
406                 proc_gpio->read_proc = ubi_gpio_proc_read;
407
408         return proc_gpio != NULL;
409 }
410 device_initcall(ubi_gpio_register_proc);
411 #endif