2 * Generic GPIO driver for logic cells found in the Nomadik SoC
4 * Copyright (C) 2008,2009 STMicroelectronics
5 * Copyright (C) 2009 Alessandro Rubini <rubini@unipv.it>
6 * Rewritten based on work by Prafulla WADASKAR <prafulla.wadaskar@st.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/device.h>
16 #include <linux/platform_device.h>
18 #include <linux/clk.h>
19 #include <linux/err.h>
20 #include <linux/gpio.h>
21 #include <linux/spinlock.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/slab.h>
26 #include <mach/hardware.h>
27 #include <mach/gpio.h>
30 * The GPIO module in the Nomadik family of Systems-on-Chip is an
31 * AMBA device, managing 32 pins and alternate functions. The logic block
32 * is currently only used in the Nomadik.
34 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
37 #define NMK_GPIO_PER_CHIP 32
38 struct nmk_gpio_chip {
39 struct gpio_chip chip;
42 unsigned int parent_irq;
44 /* Keep track of configured edges */
49 static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
50 unsigned offset, enum nmk_gpio_slpm mode)
52 u32 bit = 1 << offset;
55 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
56 if (mode == NMK_GPIO_SLPM_NOCHANGE)
60 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
63 static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
64 unsigned offset, enum nmk_gpio_pull pull)
66 u32 bit = 1 << offset;
69 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
70 if (pull == NMK_GPIO_PULL_NONE)
74 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
76 if (pull == NMK_GPIO_PULL_UP)
77 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
78 else if (pull == NMK_GPIO_PULL_DOWN)
79 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
83 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
85 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
87 * Sets the sleep mode of a pin. If @mode is NMK_GPIO_SLPM_INPUT, the pin is
88 * changed to an input (with pullup/down enabled) in sleep and deep sleep. If
89 * @mode is NMK_GPIO_SLPM_NOCHANGE, the pin remains in the state it was
90 * configured even when in sleep and deep sleep.
92 int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
94 struct nmk_gpio_chip *nmk_chip;
97 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
101 spin_lock_irqsave(&nmk_chip->lock, flags);
102 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
103 spin_unlock_irqrestore(&nmk_chip->lock, flags);
109 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
111 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
113 * Enables/disables pull up/down on a specified pin. This only takes effect if
114 * the pin is configured as an input (either explicitly or by the alternate
117 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
118 * configured as an input. Otherwise, due to the way the controller registers
119 * work, this function will change the value output on the pin.
121 int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
123 struct nmk_gpio_chip *nmk_chip;
126 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
130 spin_lock_irqsave(&nmk_chip->lock, flags);
131 __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
132 spin_unlock_irqrestore(&nmk_chip->lock, flags);
138 int nmk_gpio_set_mode(int gpio, int gpio_mode)
140 struct nmk_gpio_chip *nmk_chip;
142 u32 afunc, bfunc, bit;
144 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
148 bit = 1 << (gpio - nmk_chip->chip.base);
150 spin_lock_irqsave(&nmk_chip->lock, flags);
151 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
152 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
153 if (gpio_mode & NMK_GPIO_ALT_A)
155 if (gpio_mode & NMK_GPIO_ALT_B)
157 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
158 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
159 spin_unlock_irqrestore(&nmk_chip->lock, flags);
163 EXPORT_SYMBOL(nmk_gpio_set_mode);
165 int nmk_gpio_get_mode(int gpio)
167 struct nmk_gpio_chip *nmk_chip;
168 u32 afunc, bfunc, bit;
170 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
174 bit = 1 << (gpio - nmk_chip->chip.base);
176 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
177 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
179 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
181 EXPORT_SYMBOL(nmk_gpio_get_mode);
185 static inline int nmk_gpio_get_bitmask(int gpio)
187 return 1 << (gpio % 32);
190 static void nmk_gpio_irq_ack(unsigned int irq)
193 struct nmk_gpio_chip *nmk_chip;
195 gpio = NOMADIK_IRQ_TO_GPIO(irq);
196 nmk_chip = get_irq_chip_data(irq);
199 writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
202 static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
203 int gpio, bool enable)
205 u32 bitmask = nmk_gpio_get_bitmask(gpio);
208 /* we must individually set/clear the two edges */
209 if (nmk_chip->edge_rising & bitmask) {
210 reg = readl(nmk_chip->addr + NMK_GPIO_RIMSC);
215 writel(reg, nmk_chip->addr + NMK_GPIO_RIMSC);
217 if (nmk_chip->edge_falling & bitmask) {
218 reg = readl(nmk_chip->addr + NMK_GPIO_FIMSC);
223 writel(reg, nmk_chip->addr + NMK_GPIO_FIMSC);
227 static void nmk_gpio_irq_modify(unsigned int irq, bool enable)
230 struct nmk_gpio_chip *nmk_chip;
234 gpio = NOMADIK_IRQ_TO_GPIO(irq);
235 nmk_chip = get_irq_chip_data(irq);
236 bitmask = nmk_gpio_get_bitmask(gpio);
240 spin_lock_irqsave(&nmk_chip->lock, flags);
241 __nmk_gpio_irq_modify(nmk_chip, gpio, enable);
242 spin_unlock_irqrestore(&nmk_chip->lock, flags);
245 static void nmk_gpio_irq_mask(unsigned int irq)
247 nmk_gpio_irq_modify(irq, false);
250 static void nmk_gpio_irq_unmask(unsigned int irq)
252 nmk_gpio_irq_modify(irq, true);
255 static int nmk_gpio_irq_set_type(unsigned int irq, unsigned int type)
257 bool enabled = !(irq_to_desc(irq)->status & IRQ_DISABLED);
259 struct nmk_gpio_chip *nmk_chip;
263 gpio = NOMADIK_IRQ_TO_GPIO(irq);
264 nmk_chip = get_irq_chip_data(irq);
265 bitmask = nmk_gpio_get_bitmask(gpio);
269 if (type & IRQ_TYPE_LEVEL_HIGH)
271 if (type & IRQ_TYPE_LEVEL_LOW)
274 spin_lock_irqsave(&nmk_chip->lock, flags);
277 __nmk_gpio_irq_modify(nmk_chip, gpio, false);
279 nmk_chip->edge_rising &= ~bitmask;
280 if (type & IRQ_TYPE_EDGE_RISING)
281 nmk_chip->edge_rising |= bitmask;
283 nmk_chip->edge_falling &= ~bitmask;
284 if (type & IRQ_TYPE_EDGE_FALLING)
285 nmk_chip->edge_falling |= bitmask;
288 __nmk_gpio_irq_modify(nmk_chip, gpio, true);
290 spin_unlock_irqrestore(&nmk_chip->lock, flags);
295 static struct irq_chip nmk_gpio_irq_chip = {
296 .name = "Nomadik-GPIO",
297 .ack = nmk_gpio_irq_ack,
298 .mask = nmk_gpio_irq_mask,
299 .unmask = nmk_gpio_irq_unmask,
300 .set_type = nmk_gpio_irq_set_type,
303 static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
305 struct nmk_gpio_chip *nmk_chip;
306 struct irq_chip *host_chip = get_irq_chip(irq);
307 unsigned int gpio_irq;
309 unsigned int first_irq;
311 if (host_chip->mask_ack)
312 host_chip->mask_ack(irq);
314 host_chip->mask(irq);
319 nmk_chip = get_irq_data(irq);
320 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
321 while ( (pending = readl(nmk_chip->addr + NMK_GPIO_IS)) ) {
322 gpio_irq = first_irq + __ffs(pending);
323 generic_handle_irq(gpio_irq);
326 host_chip->unmask(irq);
329 static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
331 unsigned int first_irq;
334 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
335 for (i = first_irq; i < first_irq + NMK_GPIO_PER_CHIP; i++) {
336 set_irq_chip(i, &nmk_gpio_irq_chip);
337 set_irq_handler(i, handle_edge_irq);
338 set_irq_flags(i, IRQF_VALID);
339 set_irq_chip_data(i, nmk_chip);
340 set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
342 set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
343 set_irq_data(nmk_chip->parent_irq, nmk_chip);
348 static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
350 struct nmk_gpio_chip *nmk_chip =
351 container_of(chip, struct nmk_gpio_chip, chip);
353 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
357 static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
359 struct nmk_gpio_chip *nmk_chip =
360 container_of(chip, struct nmk_gpio_chip, chip);
361 u32 bit = 1 << offset;
363 return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
366 static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
369 struct nmk_gpio_chip *nmk_chip =
370 container_of(chip, struct nmk_gpio_chip, chip);
371 u32 bit = 1 << offset;
374 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
376 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
379 static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
382 struct nmk_gpio_chip *nmk_chip =
383 container_of(chip, struct nmk_gpio_chip, chip);
385 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
386 nmk_gpio_set_output(chip, offset, val);
391 /* This structure is replicated for each GPIO block allocated at probe time */
392 static struct gpio_chip nmk_gpio_template = {
393 .direction_input = nmk_gpio_make_input,
394 .get = nmk_gpio_get_input,
395 .direction_output = nmk_gpio_make_output,
396 .set = nmk_gpio_set_output,
397 .ngpio = NMK_GPIO_PER_CHIP,
401 static int __init nmk_gpio_probe(struct platform_device *dev)
403 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
404 struct nmk_gpio_chip *nmk_chip;
405 struct gpio_chip *chip;
406 struct resource *res;
414 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
420 irq = platform_get_irq(dev, 0);
426 if (request_mem_region(res->start, resource_size(res),
427 dev_name(&dev->dev)) == NULL) {
432 clk = clk_get(&dev->dev, NULL);
440 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
446 * The virt address in nmk_chip->addr is in the nomadik register space,
447 * so we can simply convert the resource address, without remapping
450 nmk_chip->addr = io_p2v(res->start);
451 nmk_chip->chip = nmk_gpio_template;
452 nmk_chip->parent_irq = irq;
453 spin_lock_init(&nmk_chip->lock);
455 chip = &nmk_chip->chip;
456 chip->base = pdata->first_gpio;
457 chip->label = pdata->name;
458 chip->dev = &dev->dev;
459 chip->owner = THIS_MODULE;
461 ret = gpiochip_add(&nmk_chip->chip);
465 platform_set_drvdata(dev, nmk_chip);
467 nmk_gpio_init_irq(nmk_chip);
469 dev_info(&dev->dev, "Bits %i-%i at address %p\n",
470 nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
479 release_mem_region(res->start, resource_size(res));
481 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
482 pdata->first_gpio, pdata->first_gpio+31);
486 static int __exit nmk_gpio_remove(struct platform_device *dev)
488 struct nmk_gpio_chip *nmk_chip;
489 struct resource *res;
491 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
493 nmk_chip = platform_get_drvdata(dev);
494 gpiochip_remove(&nmk_chip->chip);
495 clk_disable(nmk_chip->clk);
496 clk_put(nmk_chip->clk);
498 release_mem_region(res->start, resource_size(res));
503 static struct platform_driver nmk_gpio_driver = {
505 .owner = THIS_MODULE,
508 .probe = nmk_gpio_probe,
509 .remove = __exit_p(nmk_gpio_remove),
510 .suspend = NULL, /* to be done */
514 static int __init nmk_gpio_init(void)
516 return platform_driver_register(&nmk_gpio_driver);
519 arch_initcall(nmk_gpio_init);
521 MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
522 MODULE_DESCRIPTION("Nomadik GPIO Driver");
523 MODULE_LICENSE("GPL");