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 */
50 int nmk_gpio_set_mode(int gpio, int gpio_mode)
52 struct nmk_gpio_chip *nmk_chip;
54 u32 afunc, bfunc, bit;
56 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
60 bit = 1 << (gpio - nmk_chip->chip.base);
62 spin_lock_irqsave(&nmk_chip->lock, flags);
63 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
64 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
65 if (gpio_mode & NMK_GPIO_ALT_A)
67 if (gpio_mode & NMK_GPIO_ALT_B)
69 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
70 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
71 spin_unlock_irqrestore(&nmk_chip->lock, flags);
75 EXPORT_SYMBOL(nmk_gpio_set_mode);
77 int nmk_gpio_get_mode(int gpio)
79 struct nmk_gpio_chip *nmk_chip;
80 u32 afunc, bfunc, bit;
82 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
86 bit = 1 << (gpio - nmk_chip->chip.base);
88 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
89 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
91 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
93 EXPORT_SYMBOL(nmk_gpio_get_mode);
97 static inline int nmk_gpio_get_bitmask(int gpio)
99 return 1 << (gpio % 32);
102 static void nmk_gpio_irq_ack(unsigned int irq)
105 struct nmk_gpio_chip *nmk_chip;
107 gpio = NOMADIK_IRQ_TO_GPIO(irq);
108 nmk_chip = get_irq_chip_data(irq);
111 writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
114 static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
115 int gpio, bool enable)
117 u32 bitmask = nmk_gpio_get_bitmask(gpio);
120 /* we must individually set/clear the two edges */
121 if (nmk_chip->edge_rising & bitmask) {
122 reg = readl(nmk_chip->addr + NMK_GPIO_RIMSC);
127 writel(reg, nmk_chip->addr + NMK_GPIO_RIMSC);
129 if (nmk_chip->edge_falling & bitmask) {
130 reg = readl(nmk_chip->addr + NMK_GPIO_FIMSC);
135 writel(reg, nmk_chip->addr + NMK_GPIO_FIMSC);
139 static void nmk_gpio_irq_modify(unsigned int irq, bool enable)
142 struct nmk_gpio_chip *nmk_chip;
146 gpio = NOMADIK_IRQ_TO_GPIO(irq);
147 nmk_chip = get_irq_chip_data(irq);
148 bitmask = nmk_gpio_get_bitmask(gpio);
152 spin_lock_irqsave(&nmk_chip->lock, flags);
153 __nmk_gpio_irq_modify(nmk_chip, gpio, enable);
154 spin_unlock_irqrestore(&nmk_chip->lock, flags);
157 static void nmk_gpio_irq_mask(unsigned int irq)
159 nmk_gpio_irq_modify(irq, false);
162 static void nmk_gpio_irq_unmask(unsigned int irq)
164 nmk_gpio_irq_modify(irq, true);
167 static int nmk_gpio_irq_set_type(unsigned int irq, unsigned int type)
169 bool enabled = !(irq_to_desc(irq)->status & IRQ_DISABLED);
171 struct nmk_gpio_chip *nmk_chip;
175 gpio = NOMADIK_IRQ_TO_GPIO(irq);
176 nmk_chip = get_irq_chip_data(irq);
177 bitmask = nmk_gpio_get_bitmask(gpio);
181 if (type & IRQ_TYPE_LEVEL_HIGH)
183 if (type & IRQ_TYPE_LEVEL_LOW)
186 spin_lock_irqsave(&nmk_chip->lock, flags);
189 __nmk_gpio_irq_modify(nmk_chip, gpio, false);
191 nmk_chip->edge_rising &= ~bitmask;
192 if (type & IRQ_TYPE_EDGE_RISING)
193 nmk_chip->edge_rising |= bitmask;
195 nmk_chip->edge_falling &= ~bitmask;
196 if (type & IRQ_TYPE_EDGE_FALLING)
197 nmk_chip->edge_falling |= bitmask;
200 __nmk_gpio_irq_modify(nmk_chip, gpio, true);
202 spin_unlock_irqrestore(&nmk_chip->lock, flags);
207 static struct irq_chip nmk_gpio_irq_chip = {
208 .name = "Nomadik-GPIO",
209 .ack = nmk_gpio_irq_ack,
210 .mask = nmk_gpio_irq_mask,
211 .unmask = nmk_gpio_irq_unmask,
212 .set_type = nmk_gpio_irq_set_type,
215 static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
217 struct nmk_gpio_chip *nmk_chip;
218 struct irq_chip *host_chip = get_irq_chip(irq);
219 unsigned int gpio_irq;
221 unsigned int first_irq;
223 if (host_chip->mask_ack)
224 host_chip->mask_ack(irq);
226 host_chip->mask(irq);
231 nmk_chip = get_irq_data(irq);
232 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
233 while ( (pending = readl(nmk_chip->addr + NMK_GPIO_IS)) ) {
234 gpio_irq = first_irq + __ffs(pending);
235 generic_handle_irq(gpio_irq);
238 host_chip->unmask(irq);
241 static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
243 unsigned int first_irq;
246 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
247 for (i = first_irq; i < first_irq + NMK_GPIO_PER_CHIP; i++) {
248 set_irq_chip(i, &nmk_gpio_irq_chip);
249 set_irq_handler(i, handle_edge_irq);
250 set_irq_flags(i, IRQF_VALID);
251 set_irq_chip_data(i, nmk_chip);
252 set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
254 set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
255 set_irq_data(nmk_chip->parent_irq, nmk_chip);
260 static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
262 struct nmk_gpio_chip *nmk_chip =
263 container_of(chip, struct nmk_gpio_chip, chip);
265 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
269 static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
272 struct nmk_gpio_chip *nmk_chip =
273 container_of(chip, struct nmk_gpio_chip, chip);
275 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
279 static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
281 struct nmk_gpio_chip *nmk_chip =
282 container_of(chip, struct nmk_gpio_chip, chip);
283 u32 bit = 1 << offset;
285 return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
288 static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
291 struct nmk_gpio_chip *nmk_chip =
292 container_of(chip, struct nmk_gpio_chip, chip);
293 u32 bit = 1 << offset;
296 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
298 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
301 /* This structure is replicated for each GPIO block allocated at probe time */
302 static struct gpio_chip nmk_gpio_template = {
303 .direction_input = nmk_gpio_make_input,
304 .get = nmk_gpio_get_input,
305 .direction_output = nmk_gpio_make_output,
306 .set = nmk_gpio_set_output,
307 .ngpio = NMK_GPIO_PER_CHIP,
311 static int __init nmk_gpio_probe(struct platform_device *dev)
313 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
314 struct nmk_gpio_chip *nmk_chip;
315 struct gpio_chip *chip;
316 struct resource *res;
324 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
330 irq = platform_get_irq(dev, 0);
336 if (request_mem_region(res->start, resource_size(res),
337 dev_name(&dev->dev)) == NULL) {
342 clk = clk_get(&dev->dev, NULL);
350 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
356 * The virt address in nmk_chip->addr is in the nomadik register space,
357 * so we can simply convert the resource address, without remapping
360 nmk_chip->addr = io_p2v(res->start);
361 nmk_chip->chip = nmk_gpio_template;
362 nmk_chip->parent_irq = irq;
363 spin_lock_init(&nmk_chip->lock);
365 chip = &nmk_chip->chip;
366 chip->base = pdata->first_gpio;
367 chip->label = pdata->name;
368 chip->dev = &dev->dev;
369 chip->owner = THIS_MODULE;
371 ret = gpiochip_add(&nmk_chip->chip);
375 platform_set_drvdata(dev, nmk_chip);
377 nmk_gpio_init_irq(nmk_chip);
379 dev_info(&dev->dev, "Bits %i-%i at address %p\n",
380 nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
389 release_mem_region(res->start, resource_size(res));
391 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
392 pdata->first_gpio, pdata->first_gpio+31);
396 static int __exit nmk_gpio_remove(struct platform_device *dev)
398 struct nmk_gpio_chip *nmk_chip;
399 struct resource *res;
401 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
403 nmk_chip = platform_get_drvdata(dev);
404 gpiochip_remove(&nmk_chip->chip);
405 clk_disable(nmk_chip->clk);
406 clk_put(nmk_chip->clk);
408 release_mem_region(res->start, resource_size(res));
413 static struct platform_driver nmk_gpio_driver = {
415 .owner = THIS_MODULE,
418 .probe = nmk_gpio_probe,
419 .remove = __exit_p(nmk_gpio_remove),
420 .suspend = NULL, /* to be done */
424 static int __init nmk_gpio_init(void)
426 return platform_driver_register(&nmk_gpio_driver);
429 arch_initcall(nmk_gpio_init);
431 MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
432 MODULE_DESCRIPTION("Nomadik GPIO Driver");
433 MODULE_LICENSE("GPL");