1 From 02d9df56be1ba23c7bec51c94e5d2ac0d13d2d78 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Thu, 11 Aug 2011 14:35:02 +0200
4 Subject: [PATCH 07/24] MIPS: lantiq: add support for FALC-ON GPIOs
6 FALC-ON uses a different GPIO core than the other Lantiq SoCs. This patch adds
9 Signed-off-by: Thomas Langer <thomas.langer@lantiq.com>
10 Signed-off-by: John Crispin <blogic@openwrt.org>
11 Cc: linux-mips@linux-mips.org
13 arch/mips/lantiq/falcon/Makefile | 2 +-
14 arch/mips/lantiq/falcon/devices.c | 41 ++++
15 arch/mips/lantiq/falcon/devices.h | 2 +
16 arch/mips/lantiq/falcon/gpio.c | 398 +++++++++++++++++++++++++++++++++++++
17 4 files changed, 442 insertions(+), 1 deletions(-)
18 create mode 100644 arch/mips/lantiq/falcon/gpio.c
20 --- a/arch/mips/lantiq/falcon/Makefile
21 +++ b/arch/mips/lantiq/falcon/Makefile
23 -obj-y := clk.o prom.o reset.o sysctrl.o devices.o
24 +obj-y := clk.o prom.o reset.o sysctrl.o devices.o gpio.o
25 --- a/arch/mips/lantiq/falcon/devices.c
26 +++ b/arch/mips/lantiq/falcon/devices.c
29 #include <linux/platform_device.h>
30 #include <linux/mtd/nand.h>
31 +#include <linux/gpio.h>
33 #include <lantiq_soc.h>
35 @@ -85,3 +86,43 @@ falcon_register_nand(void)
37 platform_device_register(<q_flash_nand);
41 +#define DECLARE_GPIO_RES(port) \
42 +static struct resource falcon_gpio ## port ## _res[] = { \
43 + MEM_RES("gpio"#port, LTQ_GPIO ## port ## _BASE_ADDR, \
44 + LTQ_GPIO ## port ## _SIZE), \
45 + MEM_RES("padctrl"#port, LTQ_PADCTRL ## port ## _BASE_ADDR, \
46 + LTQ_PADCTRL ## port ## _SIZE), \
47 + IRQ_RES("gpio_mux"#port, FALCON_IRQ_GPIO_P ## port) \
56 +falcon_register_gpio(void)
58 + platform_device_register_simple("falcon_gpio", 0,
59 + falcon_gpio0_res, ARRAY_SIZE(falcon_gpio0_res));
60 + platform_device_register_simple("falcon_gpio", 1,
61 + falcon_gpio1_res, ARRAY_SIZE(falcon_gpio1_res));
62 + platform_device_register_simple("falcon_gpio", 2,
63 + falcon_gpio2_res, ARRAY_SIZE(falcon_gpio2_res));
64 + ltq_sysctl_activate(SYSCTL_SYS1, ACTS_PADCTRL1 | ACTS_P1);
65 + ltq_sysctl_activate(SYSCTL_SYSETH, ACTS_PADCTRL0 |
66 + ACTS_PADCTRL2 | ACTS_P0 | ACTS_P2);
70 +falcon_register_gpio_extra(void)
72 + platform_device_register_simple("falcon_gpio", 3,
73 + falcon_gpio3_res, ARRAY_SIZE(falcon_gpio3_res));
74 + platform_device_register_simple("falcon_gpio", 4,
75 + falcon_gpio4_res, ARRAY_SIZE(falcon_gpio4_res));
76 + ltq_sysctl_activate(SYSCTL_SYS1,
77 + ACTS_PADCTRL3 | ACTS_PADCTRL4 | ACTS_P3 | ACTS_P4);
79 --- a/arch/mips/lantiq/falcon/devices.h
80 +++ b/arch/mips/lantiq/falcon/devices.h
82 #include "../devices.h"
84 extern void falcon_register_nand(void);
85 +extern void falcon_register_gpio(void);
86 +extern void falcon_register_gpio_extra(void);
90 +++ b/arch/mips/lantiq/falcon/gpio.c
93 + * This program is free software; you can redistribute it and/or modify it
94 + * under the terms of the GNU General Public License version 2 as published
95 + * by the Free Software Foundation.
97 + * Copyright (C) 2011 Thomas Langer <thomas.langer@lantiq.com>
98 + * Copyright (C) 2011 John Crispin <blogic@openwrt.org>
101 +#include <linux/gpio.h>
102 +#include <linux/interrupt.h>
103 +#include <linux/slab.h>
104 +#include <linux/platform_device.h>
106 +#include <lantiq_soc.h>
108 +/* Multiplexer Control Register */
109 +#define LTQ_PADC_MUX(x) (x * 0x4)
110 +/* Pad Control Availability Register */
111 +#define LTQ_PADC_AVAIL 0x000000F0
113 +/* Data Output Register */
114 +#define LTQ_GPIO_OUT 0x00000000
115 +/* Data Input Register */
116 +#define LTQ_GPIO_IN 0x00000004
117 +/* Direction Register */
118 +#define LTQ_GPIO_DIR 0x00000008
119 +/* External Interrupt Control Register 0 */
120 +#define LTQ_GPIO_EXINTCR0 0x00000018
121 +/* External Interrupt Control Register 1 */
122 +#define LTQ_GPIO_EXINTCR1 0x0000001C
123 +/* IRN Capture Register */
124 +#define LTQ_GPIO_IRNCR 0x00000020
125 +/* IRN Interrupt Configuration Register */
126 +#define LTQ_GPIO_IRNCFG 0x0000002C
127 +/* IRN Interrupt Enable Set Register */
128 +#define LTQ_GPIO_IRNRNSET 0x00000030
129 +/* IRN Interrupt Enable Clear Register */
130 +#define LTQ_GPIO_IRNENCLR 0x00000034
131 +/* Output Set Register */
132 +#define LTQ_GPIO_OUTSET 0x00000040
133 +/* Output Cler Register */
134 +#define LTQ_GPIO_OUTCLR 0x00000044
135 +/* Direction Clear Register */
136 +#define LTQ_GPIO_DIRSET 0x00000048
137 +/* Direction Set Register */
138 +#define LTQ_GPIO_DIRCLR 0x0000004C
140 +/* turn a gpio_chip into a falcon_gpio_port */
141 +#define ctop(c) container_of(c, struct falcon_gpio_port, gpio_chip)
142 +/* turn a irq_data into a falcon_gpio_port */
143 +#define itop(i) ((struct falcon_gpio_port *) irq_get_chip_data(i->irq))
145 +#define ltq_pad_r32(p, reg) ltq_r32(p->pad + reg)
146 +#define ltq_pad_w32(p, val, reg) ltq_w32(val, p->pad + reg)
147 +#define ltq_pad_w32_mask(c, clear, set, reg) \
148 + ltq_pad_w32(c, (ltq_pad_r32(c, reg) & ~(clear)) | (set), reg)
150 +#define ltq_port_r32(p, reg) ltq_r32(p->port + reg)
151 +#define ltq_port_w32(p, val, reg) ltq_w32(val, p->port + reg)
152 +#define ltq_port_w32_mask(p, clear, set, reg) \
153 + ltq_port_w32(p, (ltq_port_r32(p, reg) & ~(clear)) | (set), reg)
156 +#define PINS_PER_PORT 32
158 +struct falcon_gpio_port {
159 + struct gpio_chip gpio_chip;
161 + void __iomem *port;
162 + unsigned int irq_base;
163 + unsigned int chained_irq;
166 +static struct falcon_gpio_port ltq_gpio_port[MAX_PORTS];
168 +int gpio_to_irq(unsigned int gpio)
170 + return __gpio_to_irq(gpio);
172 +EXPORT_SYMBOL(gpio_to_irq);
174 +int ltq_gpio_mux_set(unsigned int pin, unsigned int mux)
176 + int port = pin / 100;
177 + int offset = pin % 100;
178 + struct falcon_gpio_port *gpio_port;
180 + if ((offset >= PINS_PER_PORT) || (port >= MAX_PORTS))
183 + gpio_port = <q_gpio_port[port];
184 + ltq_pad_w32(gpio_port, mux & 0x3, LTQ_PADC_MUX(offset));
188 +EXPORT_SYMBOL(ltq_gpio_mux_set);
190 +int ltq_gpio_request(unsigned int pin, unsigned int val,
191 + unsigned int dir, const char *name)
193 + int port = pin / 100;
194 + int offset = pin % 100;
196 + if (offset >= PINS_PER_PORT || port >= MAX_PORTS)
199 + if (gpio_request(pin, name)) {
200 + pr_err("failed to setup lantiq gpio: %s\n", name);
205 + gpio_direction_output(pin, 1);
207 + gpio_direction_input(pin);
209 + return ltq_gpio_mux_set(pin, val);
211 +EXPORT_SYMBOL(ltq_gpio_request);
214 +falcon_gpio_direction_input(struct gpio_chip *chip, unsigned int offset)
216 + ltq_port_w32(ctop(chip), 1 << offset, LTQ_GPIO_DIRCLR);
222 +falcon_gpio_set(struct gpio_chip *chip, unsigned int offset, int value)
225 + ltq_port_w32(ctop(chip), 1 << offset, LTQ_GPIO_OUTSET);
227 + ltq_port_w32(ctop(chip), 1 << offset, LTQ_GPIO_OUTCLR);
231 +falcon_gpio_direction_output(struct gpio_chip *chip,
232 + unsigned int offset, int value)
234 + falcon_gpio_set(chip, offset, value);
235 + ltq_port_w32(ctop(chip), 1 << offset, LTQ_GPIO_DIRSET);
241 +falcon_gpio_get(struct gpio_chip *chip, unsigned int offset)
243 + if ((ltq_port_r32(ctop(chip), LTQ_GPIO_DIR) >> offset) & 1)
244 + return (ltq_port_r32(ctop(chip), LTQ_GPIO_OUT) >> offset) & 1;
246 + return (ltq_port_r32(ctop(chip), LTQ_GPIO_IN) >> offset) & 1;
250 +falcon_gpio_request(struct gpio_chip *chip, unsigned offset)
252 + if ((ltq_pad_r32(ctop(chip), LTQ_PADC_AVAIL) >> offset) & 1) {
253 + if (ltq_pad_r32(ctop(chip), LTQ_PADC_MUX(offset)) > 1)
255 + /* switch on gpio function */
256 + ltq_pad_w32(ctop(chip), 1, LTQ_PADC_MUX(offset));
264 +falcon_gpio_free(struct gpio_chip *chip, unsigned offset)
266 + if ((ltq_pad_r32(ctop(chip), LTQ_PADC_AVAIL) >> offset) & 1) {
267 + if (ltq_pad_r32(ctop(chip), LTQ_PADC_MUX(offset)) > 1)
269 + /* switch off gpio function */
270 + ltq_pad_w32(ctop(chip), 0, LTQ_PADC_MUX(offset));
275 +falcon_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
277 + return ctop(chip)->irq_base + offset;
281 +falcon_gpio_disable_irq(struct irq_data *d)
283 + unsigned int offset = d->irq - itop(d)->irq_base;
285 + ltq_port_w32(itop(d), 1 << offset, LTQ_GPIO_IRNENCLR);
289 +falcon_gpio_enable_irq(struct irq_data *d)
291 + unsigned int offset = d->irq - itop(d)->irq_base;
293 + if (!ltq_pad_r32(itop(d), LTQ_PADC_MUX(offset)) < 1)
294 + /* switch on gpio function */
295 + ltq_pad_w32(itop(d), 1, LTQ_PADC_MUX(offset));
297 + ltq_port_w32(itop(d), 1 << offset, LTQ_GPIO_IRNRNSET);
301 +falcon_gpio_ack_irq(struct irq_data *d)
303 + unsigned int offset = d->irq - itop(d)->irq_base;
305 + ltq_port_w32(itop(d), 1 << offset, LTQ_GPIO_IRNCR);
309 +falcon_gpio_mask_and_ack_irq(struct irq_data *d)
311 + unsigned int offset = d->irq - itop(d)->irq_base;
313 + ltq_port_w32(itop(d), 1 << offset, LTQ_GPIO_IRNENCLR);
314 + ltq_port_w32(itop(d), 1 << offset, LTQ_GPIO_IRNCR);
317 +static struct irq_chip falcon_gpio_irq_chip;
319 +falcon_gpio_irq_type(struct irq_data *d, unsigned int type)
321 + unsigned int offset = d->irq - itop(d)->irq_base;
322 + unsigned int mask = 1 << offset;
324 + if ((type & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_NONE)
327 + if ((type & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) != 0) {
328 + /* level triggered */
329 + ltq_port_w32_mask(itop(d), 0, mask, LTQ_GPIO_IRNCFG);
330 + irq_set_chip_and_handler_name(d->irq,
331 + &falcon_gpio_irq_chip, handle_level_irq, "mux");
333 + /* edge triggered */
334 + ltq_port_w32_mask(itop(d), mask, 0, LTQ_GPIO_IRNCFG);
335 + irq_set_chip_and_handler_name(d->irq,
336 + &falcon_gpio_irq_chip, handle_simple_irq, "mux");
339 + if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
340 + ltq_port_w32_mask(itop(d), mask, 0, LTQ_GPIO_EXINTCR0);
341 + ltq_port_w32_mask(itop(d), 0, mask, LTQ_GPIO_EXINTCR1);
343 + if ((type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_LEVEL_HIGH)) != 0)
344 + /* positive logic: rising edge, high level */
345 + ltq_port_w32_mask(itop(d), mask, 0, LTQ_GPIO_EXINTCR0);
347 + /* negative logic: falling edge, low level */
348 + ltq_port_w32_mask(itop(d), 0, mask, LTQ_GPIO_EXINTCR0);
349 + ltq_port_w32_mask(itop(d), mask, 0, LTQ_GPIO_EXINTCR1);
352 + return gpio_direction_input(itop(d)->gpio_chip.base + offset);
356 +falcon_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
358 + struct falcon_gpio_port *gpio_port = irq_desc_get_handler_data(desc);
359 + unsigned long irncr;
362 + /* acknowledge interrupt */
363 + irncr = ltq_port_r32(gpio_port, LTQ_GPIO_IRNCR);
364 + ltq_port_w32(gpio_port, irncr, LTQ_GPIO_IRNCR);
366 + desc->irq_data.chip->irq_ack(&desc->irq_data);
368 + for_each_set_bit(offset, &irncr, gpio_port->gpio_chip.ngpio)
369 + generic_handle_irq(gpio_port->irq_base + offset);
372 +static struct irq_chip falcon_gpio_irq_chip = {
373 + .name = "gpio_irq_mux",
374 + .irq_mask = falcon_gpio_disable_irq,
375 + .irq_unmask = falcon_gpio_enable_irq,
376 + .irq_ack = falcon_gpio_ack_irq,
377 + .irq_mask_ack = falcon_gpio_mask_and_ack_irq,
378 + .irq_set_type = falcon_gpio_irq_type,
381 +static struct irqaction gpio_cascade = {
382 + .handler = no_action,
383 + .flags = IRQF_DISABLED,
384 + .name = "gpio_cascade",
388 +falcon_gpio_probe(struct platform_device *pdev)
390 + struct falcon_gpio_port *gpio_port;
392 + struct resource *gpiores, *padres;
395 + if (pdev->id >= MAX_PORTS)
398 + gpiores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
399 + padres = platform_get_resource(pdev, IORESOURCE_MEM, 1);
400 + irq = platform_get_irq(pdev, 0);
401 + if (!gpiores || !padres)
404 + gpio_port = <q_gpio_port[pdev->id];
405 + gpio_port->gpio_chip.label = "falcon-gpio";
406 + gpio_port->gpio_chip.direction_input = falcon_gpio_direction_input;
407 + gpio_port->gpio_chip.direction_output = falcon_gpio_direction_output;
408 + gpio_port->gpio_chip.get = falcon_gpio_get;
409 + gpio_port->gpio_chip.set = falcon_gpio_set;
410 + gpio_port->gpio_chip.request = falcon_gpio_request;
411 + gpio_port->gpio_chip.free = falcon_gpio_free;
412 + gpio_port->gpio_chip.base = 100 * pdev->id;
413 + gpio_port->gpio_chip.ngpio = 32;
414 + gpio_port->gpio_chip.dev = &pdev->dev;
416 + gpio_port->port = ltq_remap_resource(gpiores);
417 + gpio_port->pad = ltq_remap_resource(padres);
419 + if (!gpio_port->port || !gpio_port->pad) {
420 + dev_err(&pdev->dev, "Could not map io ranges\n");
426 + /* irq_chip support */
427 + gpio_port->gpio_chip.to_irq = falcon_gpio_to_irq;
428 + gpio_port->irq_base = INT_NUM_EXTRA_START + (32 * pdev->id);
430 + for (i = 0; i < 32; i++) {
431 + irq_set_chip_and_handler_name(gpio_port->irq_base + i,
432 + &falcon_gpio_irq_chip, handle_simple_irq,
434 + irq_set_chip_data(gpio_port->irq_base + i, gpio_port);
435 + /* set to negative logic (falling edge, low level) */
436 + ltq_port_w32_mask(gpio_port, 0, 1 << i,
437 + LTQ_GPIO_EXINTCR0);
440 + gpio_port->chained_irq = irq;
441 + setup_irq(irq, &gpio_cascade);
442 + irq_set_handler_data(irq, gpio_port);
443 + irq_set_chained_handler(irq, falcon_gpio_irq_handler);
446 + ret = gpiochip_add(&gpio_port->gpio_chip);
448 + dev_err(&pdev->dev, "Could not register gpiochip %d, %d\n",
452 + platform_set_drvdata(pdev, gpio_port);
456 + dev_err(&pdev->dev, "Error in gpio_probe %d, %d\n", pdev->id, ret);
458 + release_resource(gpiores);
460 + release_resource(padres);
462 + if (gpio_port->port)
463 + iounmap(gpio_port->port);
464 + if (gpio_port->pad)
465 + iounmap(gpio_port->pad);
469 +static struct platform_driver falcon_gpio_driver = {
470 + .probe = falcon_gpio_probe,
472 + .name = "falcon_gpio",
473 + .owner = THIS_MODULE,
478 +falcon_gpio_init(void)
482 + pr_info("FALC(tm) ON GPIO Driver, (C) 2011 Lantiq Deutschland Gmbh\n");
483 + ret = platform_driver_register(&falcon_gpio_driver);
485 + pr_err("falcon_gpio: Error registering platform driver!");
489 +postcore_initcall(falcon_gpio_init);