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_mode(struct nmk_gpio_chip *nmk_chip,
50 unsigned offset, int gpio_mode)
52 u32 bit = 1 << offset;
55 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
56 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
57 if (gpio_mode & NMK_GPIO_ALT_A)
59 if (gpio_mode & NMK_GPIO_ALT_B)
61 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
62 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
65 static void __nmk_gpio_set_slpm(struct nmk_gpio_chip *nmk_chip,
66 unsigned offset, enum nmk_gpio_slpm mode)
68 u32 bit = 1 << offset;
71 slpm = readl(nmk_chip->addr + NMK_GPIO_SLPC);
72 if (mode == NMK_GPIO_SLPM_NOCHANGE)
76 writel(slpm, nmk_chip->addr + NMK_GPIO_SLPC);
79 static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
80 unsigned offset, enum nmk_gpio_pull pull)
82 u32 bit = 1 << offset;
85 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
86 if (pull == NMK_GPIO_PULL_NONE)
90 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
92 if (pull == NMK_GPIO_PULL_UP)
93 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
94 else if (pull == NMK_GPIO_PULL_DOWN)
95 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
99 * nmk_gpio_set_slpm() - configure the sleep mode of a pin
101 * @mode: NMK_GPIO_SLPM_INPUT or NMK_GPIO_SLPM_NOCHANGE,
103 * Sets the sleep mode of a pin. If @mode is NMK_GPIO_SLPM_INPUT, the pin is
104 * changed to an input (with pullup/down enabled) in sleep and deep sleep. If
105 * @mode is NMK_GPIO_SLPM_NOCHANGE, the pin remains in the state it was
106 * configured even when in sleep and deep sleep.
108 int nmk_gpio_set_slpm(int gpio, enum nmk_gpio_slpm mode)
110 struct nmk_gpio_chip *nmk_chip;
113 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
117 spin_lock_irqsave(&nmk_chip->lock, flags);
118 __nmk_gpio_set_slpm(nmk_chip, gpio - nmk_chip->chip.base, mode);
119 spin_unlock_irqrestore(&nmk_chip->lock, flags);
125 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
127 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
129 * Enables/disables pull up/down on a specified pin. This only takes effect if
130 * the pin is configured as an input (either explicitly or by the alternate
133 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
134 * configured as an input. Otherwise, due to the way the controller registers
135 * work, this function will change the value output on the pin.
137 int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
139 struct nmk_gpio_chip *nmk_chip;
142 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
146 spin_lock_irqsave(&nmk_chip->lock, flags);
147 __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
148 spin_unlock_irqrestore(&nmk_chip->lock, flags);
154 int nmk_gpio_set_mode(int gpio, int gpio_mode)
156 struct nmk_gpio_chip *nmk_chip;
159 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
163 spin_lock_irqsave(&nmk_chip->lock, flags);
164 __nmk_gpio_set_mode(nmk_chip, gpio - nmk_chip->chip.base, gpio_mode);
165 spin_unlock_irqrestore(&nmk_chip->lock, flags);
169 EXPORT_SYMBOL(nmk_gpio_set_mode);
171 int nmk_gpio_get_mode(int gpio)
173 struct nmk_gpio_chip *nmk_chip;
174 u32 afunc, bfunc, bit;
176 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
180 bit = 1 << (gpio - nmk_chip->chip.base);
182 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
183 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
185 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
187 EXPORT_SYMBOL(nmk_gpio_get_mode);
191 static inline int nmk_gpio_get_bitmask(int gpio)
193 return 1 << (gpio % 32);
196 static void nmk_gpio_irq_ack(unsigned int irq)
199 struct nmk_gpio_chip *nmk_chip;
201 gpio = NOMADIK_IRQ_TO_GPIO(irq);
202 nmk_chip = get_irq_chip_data(irq);
205 writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
208 static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
209 int gpio, bool enable)
211 u32 bitmask = nmk_gpio_get_bitmask(gpio);
214 /* we must individually set/clear the two edges */
215 if (nmk_chip->edge_rising & bitmask) {
216 reg = readl(nmk_chip->addr + NMK_GPIO_RIMSC);
221 writel(reg, nmk_chip->addr + NMK_GPIO_RIMSC);
223 if (nmk_chip->edge_falling & bitmask) {
224 reg = readl(nmk_chip->addr + NMK_GPIO_FIMSC);
229 writel(reg, nmk_chip->addr + NMK_GPIO_FIMSC);
233 static void nmk_gpio_irq_modify(unsigned int irq, bool enable)
236 struct nmk_gpio_chip *nmk_chip;
240 gpio = NOMADIK_IRQ_TO_GPIO(irq);
241 nmk_chip = get_irq_chip_data(irq);
242 bitmask = nmk_gpio_get_bitmask(gpio);
246 spin_lock_irqsave(&nmk_chip->lock, flags);
247 __nmk_gpio_irq_modify(nmk_chip, gpio, enable);
248 spin_unlock_irqrestore(&nmk_chip->lock, flags);
251 static void nmk_gpio_irq_mask(unsigned int irq)
253 nmk_gpio_irq_modify(irq, false);
256 static void nmk_gpio_irq_unmask(unsigned int irq)
258 nmk_gpio_irq_modify(irq, true);
261 static int nmk_gpio_irq_set_type(unsigned int irq, unsigned int type)
263 bool enabled = !(irq_to_desc(irq)->status & IRQ_DISABLED);
265 struct nmk_gpio_chip *nmk_chip;
269 gpio = NOMADIK_IRQ_TO_GPIO(irq);
270 nmk_chip = get_irq_chip_data(irq);
271 bitmask = nmk_gpio_get_bitmask(gpio);
275 if (type & IRQ_TYPE_LEVEL_HIGH)
277 if (type & IRQ_TYPE_LEVEL_LOW)
280 spin_lock_irqsave(&nmk_chip->lock, flags);
283 __nmk_gpio_irq_modify(nmk_chip, gpio, false);
285 nmk_chip->edge_rising &= ~bitmask;
286 if (type & IRQ_TYPE_EDGE_RISING)
287 nmk_chip->edge_rising |= bitmask;
289 nmk_chip->edge_falling &= ~bitmask;
290 if (type & IRQ_TYPE_EDGE_FALLING)
291 nmk_chip->edge_falling |= bitmask;
294 __nmk_gpio_irq_modify(nmk_chip, gpio, true);
296 spin_unlock_irqrestore(&nmk_chip->lock, flags);
301 static struct irq_chip nmk_gpio_irq_chip = {
302 .name = "Nomadik-GPIO",
303 .ack = nmk_gpio_irq_ack,
304 .mask = nmk_gpio_irq_mask,
305 .unmask = nmk_gpio_irq_unmask,
306 .set_type = nmk_gpio_irq_set_type,
309 static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
311 struct nmk_gpio_chip *nmk_chip;
312 struct irq_chip *host_chip = get_irq_chip(irq);
313 unsigned int gpio_irq;
315 unsigned int first_irq;
317 if (host_chip->mask_ack)
318 host_chip->mask_ack(irq);
320 host_chip->mask(irq);
325 nmk_chip = get_irq_data(irq);
326 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
327 while ( (pending = readl(nmk_chip->addr + NMK_GPIO_IS)) ) {
328 gpio_irq = first_irq + __ffs(pending);
329 generic_handle_irq(gpio_irq);
332 host_chip->unmask(irq);
335 static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
337 unsigned int first_irq;
340 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
341 for (i = first_irq; i < first_irq + NMK_GPIO_PER_CHIP; i++) {
342 set_irq_chip(i, &nmk_gpio_irq_chip);
343 set_irq_handler(i, handle_edge_irq);
344 set_irq_flags(i, IRQF_VALID);
345 set_irq_chip_data(i, nmk_chip);
346 set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
348 set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
349 set_irq_data(nmk_chip->parent_irq, nmk_chip);
354 static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
356 struct nmk_gpio_chip *nmk_chip =
357 container_of(chip, struct nmk_gpio_chip, chip);
359 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
363 static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
365 struct nmk_gpio_chip *nmk_chip =
366 container_of(chip, struct nmk_gpio_chip, chip);
367 u32 bit = 1 << offset;
369 return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
372 static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
375 struct nmk_gpio_chip *nmk_chip =
376 container_of(chip, struct nmk_gpio_chip, chip);
377 u32 bit = 1 << offset;
380 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
382 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
385 static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
388 struct nmk_gpio_chip *nmk_chip =
389 container_of(chip, struct nmk_gpio_chip, chip);
391 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
392 nmk_gpio_set_output(chip, offset, val);
397 /* This structure is replicated for each GPIO block allocated at probe time */
398 static struct gpio_chip nmk_gpio_template = {
399 .direction_input = nmk_gpio_make_input,
400 .get = nmk_gpio_get_input,
401 .direction_output = nmk_gpio_make_output,
402 .set = nmk_gpio_set_output,
403 .ngpio = NMK_GPIO_PER_CHIP,
407 static int __init nmk_gpio_probe(struct platform_device *dev)
409 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
410 struct nmk_gpio_chip *nmk_chip;
411 struct gpio_chip *chip;
412 struct resource *res;
420 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
426 irq = platform_get_irq(dev, 0);
432 if (request_mem_region(res->start, resource_size(res),
433 dev_name(&dev->dev)) == NULL) {
438 clk = clk_get(&dev->dev, NULL);
446 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
452 * The virt address in nmk_chip->addr is in the nomadik register space,
453 * so we can simply convert the resource address, without remapping
456 nmk_chip->addr = io_p2v(res->start);
457 nmk_chip->chip = nmk_gpio_template;
458 nmk_chip->parent_irq = irq;
459 spin_lock_init(&nmk_chip->lock);
461 chip = &nmk_chip->chip;
462 chip->base = pdata->first_gpio;
463 chip->label = pdata->name;
464 chip->dev = &dev->dev;
465 chip->owner = THIS_MODULE;
467 ret = gpiochip_add(&nmk_chip->chip);
471 platform_set_drvdata(dev, nmk_chip);
473 nmk_gpio_init_irq(nmk_chip);
475 dev_info(&dev->dev, "Bits %i-%i at address %p\n",
476 nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
485 release_mem_region(res->start, resource_size(res));
487 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
488 pdata->first_gpio, pdata->first_gpio+31);
492 static int __exit nmk_gpio_remove(struct platform_device *dev)
494 struct nmk_gpio_chip *nmk_chip;
495 struct resource *res;
497 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
499 nmk_chip = platform_get_drvdata(dev);
500 gpiochip_remove(&nmk_chip->chip);
501 clk_disable(nmk_chip->clk);
502 clk_put(nmk_chip->clk);
504 release_mem_region(res->start, resource_size(res));
509 static struct platform_driver nmk_gpio_driver = {
511 .owner = THIS_MODULE,
514 .probe = nmk_gpio_probe,
515 .remove = __exit_p(nmk_gpio_remove),
516 .suspend = NULL, /* to be done */
520 static int __init nmk_gpio_init(void)
522 return platform_driver_register(&nmk_gpio_driver);
525 arch_initcall(nmk_gpio_init);
527 MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
528 MODULE_DESCRIPTION("Nomadik GPIO Driver");
529 MODULE_LICENSE("GPL");