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/amba/bus.h>
18 #include <linux/gpio.h>
19 #include <linux/spinlock.h>
20 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/slab.h>
24 #include <mach/hardware.h>
25 #include <mach/gpio.h>
28 * The GPIO module in the Nomadik family of Systems-on-Chip is an
29 * AMBA device, managing 32 pins and alternate functions. The logic block
30 * is currently only used in the Nomadik.
32 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
35 #define NMK_GPIO_PER_CHIP 32
36 struct nmk_gpio_chip {
37 struct gpio_chip chip;
39 unsigned int parent_irq;
41 /* Keep track of configured edges */
47 int nmk_gpio_set_mode(int gpio, int gpio_mode)
49 struct nmk_gpio_chip *nmk_chip;
51 u32 afunc, bfunc, bit;
53 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
57 bit = 1 << (gpio - nmk_chip->chip.base);
59 spin_lock_irqsave(&nmk_chip->lock, flags);
60 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
61 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
62 if (gpio_mode & NMK_GPIO_ALT_A)
64 if (gpio_mode & NMK_GPIO_ALT_B)
66 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
67 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
68 spin_unlock_irqrestore(&nmk_chip->lock, flags);
72 EXPORT_SYMBOL(nmk_gpio_set_mode);
74 int nmk_gpio_get_mode(int gpio)
76 struct nmk_gpio_chip *nmk_chip;
77 u32 afunc, bfunc, bit;
79 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
83 bit = 1 << (gpio - nmk_chip->chip.base);
85 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
86 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
88 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
90 EXPORT_SYMBOL(nmk_gpio_get_mode);
94 static inline int nmk_gpio_get_bitmask(int gpio)
96 return 1 << (gpio % 32);
99 static void nmk_gpio_irq_ack(unsigned int irq)
102 struct nmk_gpio_chip *nmk_chip;
104 gpio = NOMADIK_IRQ_TO_GPIO(irq);
105 nmk_chip = get_irq_chip_data(irq);
108 writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
111 static void nmk_gpio_irq_mask(unsigned int irq)
114 struct nmk_gpio_chip *nmk_chip;
118 gpio = NOMADIK_IRQ_TO_GPIO(irq);
119 nmk_chip = get_irq_chip_data(irq);
120 bitmask = nmk_gpio_get_bitmask(gpio);
124 /* we must individually clear the two edges */
125 spin_lock_irqsave(&nmk_chip->lock, flags);
126 if (nmk_chip->edge_rising & bitmask) {
127 reg = readl(nmk_chip->addr + NMK_GPIO_RWIMSC);
129 writel(reg, nmk_chip->addr + NMK_GPIO_RWIMSC);
131 if (nmk_chip->edge_falling & bitmask) {
132 reg = readl(nmk_chip->addr + NMK_GPIO_FWIMSC);
134 writel(reg, nmk_chip->addr + NMK_GPIO_FWIMSC);
136 spin_unlock_irqrestore(&nmk_chip->lock, flags);
139 static void nmk_gpio_irq_unmask(unsigned int irq)
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 /* we must individually set the two edges */
153 spin_lock_irqsave(&nmk_chip->lock, flags);
154 if (nmk_chip->edge_rising & bitmask) {
155 reg = readl(nmk_chip->addr + NMK_GPIO_RWIMSC);
157 writel(reg, nmk_chip->addr + NMK_GPIO_RWIMSC);
159 if (nmk_chip->edge_falling & bitmask) {
160 reg = readl(nmk_chip->addr + NMK_GPIO_FWIMSC);
162 writel(reg, nmk_chip->addr + NMK_GPIO_FWIMSC);
164 spin_unlock_irqrestore(&nmk_chip->lock, flags);
167 static int nmk_gpio_irq_set_type(unsigned int irq, unsigned int type)
170 struct nmk_gpio_chip *nmk_chip;
174 gpio = NOMADIK_IRQ_TO_GPIO(irq);
175 nmk_chip = get_irq_chip_data(irq);
176 bitmask = nmk_gpio_get_bitmask(gpio);
180 if (type & IRQ_TYPE_LEVEL_HIGH)
182 if (type & IRQ_TYPE_LEVEL_LOW)
185 spin_lock_irqsave(&nmk_chip->lock, flags);
187 nmk_chip->edge_rising &= ~bitmask;
188 if (type & IRQ_TYPE_EDGE_RISING)
189 nmk_chip->edge_rising |= bitmask;
190 writel(nmk_chip->edge_rising, nmk_chip->addr + NMK_GPIO_RIMSC);
192 nmk_chip->edge_falling &= ~bitmask;
193 if (type & IRQ_TYPE_EDGE_FALLING)
194 nmk_chip->edge_falling |= bitmask;
195 writel(nmk_chip->edge_falling, nmk_chip->addr + NMK_GPIO_FIMSC);
197 spin_unlock_irqrestore(&nmk_chip->lock, flags);
199 nmk_gpio_irq_unmask(irq);
204 static struct irq_chip nmk_gpio_irq_chip = {
205 .name = "Nomadik-GPIO",
206 .ack = nmk_gpio_irq_ack,
207 .mask = nmk_gpio_irq_mask,
208 .unmask = nmk_gpio_irq_unmask,
209 .set_type = nmk_gpio_irq_set_type,
212 static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
214 struct nmk_gpio_chip *nmk_chip;
215 struct irq_chip *host_chip;
216 unsigned int gpio_irq;
218 unsigned int first_irq;
220 nmk_chip = get_irq_data(irq);
221 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
222 while ( (pending = readl(nmk_chip->addr + NMK_GPIO_IS)) ) {
223 gpio_irq = first_irq + __ffs(pending);
224 generic_handle_irq(gpio_irq);
226 if (0) {/* don't ack parent irq, as ack == disable */
227 host_chip = get_irq_chip(irq);
232 static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
234 unsigned int first_irq;
237 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
238 for (i = first_irq; i < first_irq + NMK_GPIO_PER_CHIP; i++) {
239 set_irq_chip(i, &nmk_gpio_irq_chip);
240 set_irq_handler(i, handle_edge_irq);
241 set_irq_flags(i, IRQF_VALID);
242 set_irq_chip_data(i, nmk_chip);
244 set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
245 set_irq_data(nmk_chip->parent_irq, nmk_chip);
250 static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
252 struct nmk_gpio_chip *nmk_chip =
253 container_of(chip, struct nmk_gpio_chip, chip);
255 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
259 static int nmk_gpio_make_output(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_DIRS);
269 static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
271 struct nmk_gpio_chip *nmk_chip =
272 container_of(chip, struct nmk_gpio_chip, chip);
273 u32 bit = 1 << offset;
275 return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
278 static void nmk_gpio_set_output(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;
286 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
288 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
291 /* This structure is replicated for each GPIO block allocated at probe time */
292 static struct gpio_chip nmk_gpio_template = {
293 .direction_input = nmk_gpio_make_input,
294 .get = nmk_gpio_get_input,
295 .direction_output = nmk_gpio_make_output,
296 .set = nmk_gpio_set_output,
297 .ngpio = NMK_GPIO_PER_CHIP,
301 static int __init nmk_gpio_probe(struct amba_device *dev, struct amba_id *id)
303 struct nmk_gpio_platform_data *pdata;
304 struct nmk_gpio_chip *nmk_chip;
305 struct gpio_chip *chip;
308 pdata = dev->dev.platform_data;
309 ret = amba_request_regions(dev, pdata->name);
313 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
319 * The virt address in nmk_chip->addr is in the nomadik register space,
320 * so we can simply convert the resource address, without remapping
322 nmk_chip->addr = io_p2v(dev->res.start);
323 nmk_chip->chip = nmk_gpio_template;
324 nmk_chip->parent_irq = pdata->parent_irq;
326 chip = &nmk_chip->chip;
327 chip->base = pdata->first_gpio;
328 chip->label = pdata->name;
329 chip->dev = &dev->dev;
330 chip->owner = THIS_MODULE;
332 ret = gpiochip_add(&nmk_chip->chip);
336 amba_set_drvdata(dev, nmk_chip);
338 nmk_gpio_init_irq(nmk_chip);
340 dev_info(&dev->dev, "Bits %i-%i at address %p\n",
341 nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
347 amba_release_regions(dev);
348 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
349 pdata->first_gpio, pdata->first_gpio+31);
353 static int nmk_gpio_remove(struct amba_device *dev)
355 struct nmk_gpio_chip *nmk_chip;
357 nmk_chip = amba_get_drvdata(dev);
358 gpiochip_remove(&nmk_chip->chip);
360 amba_release_regions(dev);
365 /* We have 0x1f080060 and 0x1f180060, accept both using the mask */
366 static struct amba_id nmk_gpio_ids[] = {
374 static struct amba_driver nmk_gpio_driver = {
376 .owner = THIS_MODULE,
379 .probe = nmk_gpio_probe,
380 .remove = nmk_gpio_remove,
381 .suspend = NULL, /* to be done */
383 .id_table = nmk_gpio_ids,
386 static int __init nmk_gpio_init(void)
388 return amba_driver_register(&nmk_gpio_driver);
391 arch_initcall(nmk_gpio_init);
393 MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
394 MODULE_DESCRIPTION("Nomadik GPIO Driver");
395 MODULE_LICENSE("GPL");