blob: 50c49fa9e01a4172596511d8997e85302dc5545d [file] [log] [blame]
Alessandro Rubini2ec1d352009-07-02 15:29:12 +01001/*
2 * Generic GPIO driver for logic cells found in the Nomadik SoC
3 *
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>
7 *
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.
11 */
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/device.h>
Rabin Vincent3e3c62c2010-03-03 04:52:34 +010016#include <linux/platform_device.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010017#include <linux/io.h>
Rabin Vincentaf7dc222010-05-06 11:14:17 +010018#include <linux/clk.h>
19#include <linux/err.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010020#include <linux/gpio.h>
21#include <linux/spinlock.h>
22#include <linux/interrupt.h>
23#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010025
26#include <mach/hardware.h>
27#include <mach/gpio.h>
28
29/*
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.
33 *
34 * Symbols in this file are called "nmk_gpio" for "nomadik gpio"
35 */
36
37#define NMK_GPIO_PER_CHIP 32
38struct nmk_gpio_chip {
39 struct gpio_chip chip;
40 void __iomem *addr;
Rabin Vincentaf7dc222010-05-06 11:14:17 +010041 struct clk *clk;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010042 unsigned int parent_irq;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +010043 spinlock_t lock;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010044 /* Keep track of configured edges */
45 u32 edge_rising;
46 u32 edge_falling;
47};
48
Rabin Vincent5b327ed2010-05-27 12:29:50 +010049static void __nmk_gpio_set_pull(struct nmk_gpio_chip *nmk_chip,
50 unsigned offset, enum nmk_gpio_pull pull)
51{
52 u32 bit = 1 << offset;
53 u32 pdis;
54
55 pdis = readl(nmk_chip->addr + NMK_GPIO_PDIS);
56 if (pull == NMK_GPIO_PULL_NONE)
57 pdis |= bit;
58 else
59 pdis &= ~bit;
60 writel(pdis, nmk_chip->addr + NMK_GPIO_PDIS);
61
62 if (pull == NMK_GPIO_PULL_UP)
63 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
64 else if (pull == NMK_GPIO_PULL_DOWN)
65 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
66}
67
68/**
69 * nmk_gpio_set_pull() - enable/disable pull up/down on a gpio
70 * @gpio: pin number
71 * @pull: one of NMK_GPIO_PULL_DOWN, NMK_GPIO_PULL_UP, and NMK_GPIO_PULL_NONE
72 *
73 * Enables/disables pull up/down on a specified pin. This only takes effect if
74 * the pin is configured as an input (either explicitly or by the alternate
75 * function).
76 *
77 * NOTE: If enabling the pull up/down, the caller must ensure that the GPIO is
78 * configured as an input. Otherwise, due to the way the controller registers
79 * work, this function will change the value output on the pin.
80 */
81int nmk_gpio_set_pull(int gpio, enum nmk_gpio_pull pull)
82{
83 struct nmk_gpio_chip *nmk_chip;
84 unsigned long flags;
85
86 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
87 if (!nmk_chip)
88 return -EINVAL;
89
90 spin_lock_irqsave(&nmk_chip->lock, flags);
91 __nmk_gpio_set_pull(nmk_chip, gpio - nmk_chip->chip.base, pull);
92 spin_unlock_irqrestore(&nmk_chip->lock, flags);
93
94 return 0;
95}
96
Alessandro Rubini2ec1d352009-07-02 15:29:12 +010097/* Mode functions */
98int nmk_gpio_set_mode(int gpio, int gpio_mode)
99{
100 struct nmk_gpio_chip *nmk_chip;
101 unsigned long flags;
102 u32 afunc, bfunc, bit;
103
104 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
105 if (!nmk_chip)
106 return -EINVAL;
107
108 bit = 1 << (gpio - nmk_chip->chip.base);
109
110 spin_lock_irqsave(&nmk_chip->lock, flags);
111 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & ~bit;
112 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & ~bit;
113 if (gpio_mode & NMK_GPIO_ALT_A)
114 afunc |= bit;
115 if (gpio_mode & NMK_GPIO_ALT_B)
116 bfunc |= bit;
117 writel(afunc, nmk_chip->addr + NMK_GPIO_AFSLA);
118 writel(bfunc, nmk_chip->addr + NMK_GPIO_AFSLB);
119 spin_unlock_irqrestore(&nmk_chip->lock, flags);
120
121 return 0;
122}
123EXPORT_SYMBOL(nmk_gpio_set_mode);
124
125int nmk_gpio_get_mode(int gpio)
126{
127 struct nmk_gpio_chip *nmk_chip;
128 u32 afunc, bfunc, bit;
129
130 nmk_chip = get_irq_chip_data(NOMADIK_GPIO_TO_IRQ(gpio));
131 if (!nmk_chip)
132 return -EINVAL;
133
134 bit = 1 << (gpio - nmk_chip->chip.base);
135
136 afunc = readl(nmk_chip->addr + NMK_GPIO_AFSLA) & bit;
137 bfunc = readl(nmk_chip->addr + NMK_GPIO_AFSLB) & bit;
138
139 return (afunc ? NMK_GPIO_ALT_A : 0) | (bfunc ? NMK_GPIO_ALT_B : 0);
140}
141EXPORT_SYMBOL(nmk_gpio_get_mode);
142
143
144/* IRQ functions */
145static inline int nmk_gpio_get_bitmask(int gpio)
146{
147 return 1 << (gpio % 32);
148}
149
150static void nmk_gpio_irq_ack(unsigned int irq)
151{
152 int gpio;
153 struct nmk_gpio_chip *nmk_chip;
154
155 gpio = NOMADIK_IRQ_TO_GPIO(irq);
156 nmk_chip = get_irq_chip_data(irq);
157 if (!nmk_chip)
158 return;
159 writel(nmk_gpio_get_bitmask(gpio), nmk_chip->addr + NMK_GPIO_IC);
160}
161
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100162static void __nmk_gpio_irq_modify(struct nmk_gpio_chip *nmk_chip,
163 int gpio, bool enable)
164{
165 u32 bitmask = nmk_gpio_get_bitmask(gpio);
166 u32 reg;
167
168 /* we must individually set/clear the two edges */
169 if (nmk_chip->edge_rising & bitmask) {
170 reg = readl(nmk_chip->addr + NMK_GPIO_RIMSC);
171 if (enable)
172 reg |= bitmask;
173 else
174 reg &= ~bitmask;
175 writel(reg, nmk_chip->addr + NMK_GPIO_RIMSC);
176 }
177 if (nmk_chip->edge_falling & bitmask) {
178 reg = readl(nmk_chip->addr + NMK_GPIO_FIMSC);
179 if (enable)
180 reg |= bitmask;
181 else
182 reg &= ~bitmask;
183 writel(reg, nmk_chip->addr + NMK_GPIO_FIMSC);
184 }
185}
186
187static void nmk_gpio_irq_modify(unsigned int irq, bool enable)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100188{
189 int gpio;
190 struct nmk_gpio_chip *nmk_chip;
191 unsigned long flags;
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100192 u32 bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100193
194 gpio = NOMADIK_IRQ_TO_GPIO(irq);
195 nmk_chip = get_irq_chip_data(irq);
196 bitmask = nmk_gpio_get_bitmask(gpio);
197 if (!nmk_chip)
198 return;
199
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100200 spin_lock_irqsave(&nmk_chip->lock, flags);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100201 __nmk_gpio_irq_modify(nmk_chip, gpio, enable);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100202 spin_unlock_irqrestore(&nmk_chip->lock, flags);
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100203}
204
205static void nmk_gpio_irq_mask(unsigned int irq)
206{
207 nmk_gpio_irq_modify(irq, false);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100208};
209
210static void nmk_gpio_irq_unmask(unsigned int irq)
211{
Rabin Vincent040e5ec2010-05-06 10:42:42 +0100212 nmk_gpio_irq_modify(irq, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100213}
214
215static int nmk_gpio_irq_set_type(unsigned int irq, unsigned int type)
216{
Rabin Vincent7a852d82010-05-06 10:43:55 +0100217 bool enabled = !(irq_to_desc(irq)->status & IRQ_DISABLED);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100218 int gpio;
219 struct nmk_gpio_chip *nmk_chip;
220 unsigned long flags;
221 u32 bitmask;
222
223 gpio = NOMADIK_IRQ_TO_GPIO(irq);
224 nmk_chip = get_irq_chip_data(irq);
225 bitmask = nmk_gpio_get_bitmask(gpio);
226 if (!nmk_chip)
227 return -EINVAL;
228
229 if (type & IRQ_TYPE_LEVEL_HIGH)
230 return -EINVAL;
231 if (type & IRQ_TYPE_LEVEL_LOW)
232 return -EINVAL;
233
234 spin_lock_irqsave(&nmk_chip->lock, flags);
235
Rabin Vincent7a852d82010-05-06 10:43:55 +0100236 if (enabled)
237 __nmk_gpio_irq_modify(nmk_chip, gpio, false);
238
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100239 nmk_chip->edge_rising &= ~bitmask;
240 if (type & IRQ_TYPE_EDGE_RISING)
241 nmk_chip->edge_rising |= bitmask;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100242
243 nmk_chip->edge_falling &= ~bitmask;
244 if (type & IRQ_TYPE_EDGE_FALLING)
245 nmk_chip->edge_falling |= bitmask;
Rabin Vincent7a852d82010-05-06 10:43:55 +0100246
247 if (enabled)
248 __nmk_gpio_irq_modify(nmk_chip, gpio, true);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100249
250 spin_unlock_irqrestore(&nmk_chip->lock, flags);
251
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100252 return 0;
253}
254
255static struct irq_chip nmk_gpio_irq_chip = {
256 .name = "Nomadik-GPIO",
257 .ack = nmk_gpio_irq_ack,
258 .mask = nmk_gpio_irq_mask,
259 .unmask = nmk_gpio_irq_unmask,
260 .set_type = nmk_gpio_irq_set_type,
261};
262
263static void nmk_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
264{
265 struct nmk_gpio_chip *nmk_chip;
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100266 struct irq_chip *host_chip = get_irq_chip(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100267 unsigned int gpio_irq;
268 u32 pending;
269 unsigned int first_irq;
270
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100271 if (host_chip->mask_ack)
272 host_chip->mask_ack(irq);
273 else {
274 host_chip->mask(irq);
275 if (host_chip->ack)
276 host_chip->ack(irq);
277 }
278
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100279 nmk_chip = get_irq_data(irq);
280 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
281 while ( (pending = readl(nmk_chip->addr + NMK_GPIO_IS)) ) {
282 gpio_irq = first_irq + __ffs(pending);
283 generic_handle_irq(gpio_irq);
284 }
Rabin Vincentaaedaa22010-03-03 04:50:27 +0100285
286 host_chip->unmask(irq);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100287}
288
289static int nmk_gpio_init_irq(struct nmk_gpio_chip *nmk_chip)
290{
291 unsigned int first_irq;
292 int i;
293
294 first_irq = NOMADIK_GPIO_TO_IRQ(nmk_chip->chip.base);
295 for (i = first_irq; i < first_irq + NMK_GPIO_PER_CHIP; i++) {
296 set_irq_chip(i, &nmk_gpio_irq_chip);
297 set_irq_handler(i, handle_edge_irq);
298 set_irq_flags(i, IRQF_VALID);
299 set_irq_chip_data(i, nmk_chip);
Rabin Vincent2210d642010-05-06 10:45:18 +0100300 set_irq_type(i, IRQ_TYPE_EDGE_FALLING);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100301 }
302 set_irq_chained_handler(nmk_chip->parent_irq, nmk_gpio_irq_handler);
303 set_irq_data(nmk_chip->parent_irq, nmk_chip);
304 return 0;
305}
306
307/* I/O Functions */
308static int nmk_gpio_make_input(struct gpio_chip *chip, unsigned offset)
309{
310 struct nmk_gpio_chip *nmk_chip =
311 container_of(chip, struct nmk_gpio_chip, chip);
312
313 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRC);
314 return 0;
315}
316
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100317static int nmk_gpio_get_input(struct gpio_chip *chip, unsigned offset)
318{
319 struct nmk_gpio_chip *nmk_chip =
320 container_of(chip, struct nmk_gpio_chip, chip);
321 u32 bit = 1 << offset;
322
323 return (readl(nmk_chip->addr + NMK_GPIO_DAT) & bit) != 0;
324}
325
326static void nmk_gpio_set_output(struct gpio_chip *chip, unsigned offset,
327 int val)
328{
329 struct nmk_gpio_chip *nmk_chip =
330 container_of(chip, struct nmk_gpio_chip, chip);
331 u32 bit = 1 << offset;
332
333 if (val)
334 writel(bit, nmk_chip->addr + NMK_GPIO_DATS);
335 else
336 writel(bit, nmk_chip->addr + NMK_GPIO_DATC);
337}
338
Rabin Vincent6647c6c2010-05-27 12:22:42 +0100339static int nmk_gpio_make_output(struct gpio_chip *chip, unsigned offset,
340 int val)
341{
342 struct nmk_gpio_chip *nmk_chip =
343 container_of(chip, struct nmk_gpio_chip, chip);
344
345 writel(1 << offset, nmk_chip->addr + NMK_GPIO_DIRS);
346 nmk_gpio_set_output(chip, offset, val);
347
348 return 0;
349}
350
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100351/* This structure is replicated for each GPIO block allocated at probe time */
352static struct gpio_chip nmk_gpio_template = {
353 .direction_input = nmk_gpio_make_input,
354 .get = nmk_gpio_get_input,
355 .direction_output = nmk_gpio_make_output,
356 .set = nmk_gpio_set_output,
357 .ngpio = NMK_GPIO_PER_CHIP,
358 .can_sleep = 0,
359};
360
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100361static int __init nmk_gpio_probe(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100362{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100363 struct nmk_gpio_platform_data *pdata = dev->dev.platform_data;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100364 struct nmk_gpio_chip *nmk_chip;
365 struct gpio_chip *chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100366 struct resource *res;
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100367 struct clk *clk;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100368 int irq;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100369 int ret;
370
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100371 if (!pdata)
372 return -ENODEV;
373
374 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
375 if (!res) {
376 ret = -ENOENT;
377 goto out;
378 }
379
380 irq = platform_get_irq(dev, 0);
381 if (irq < 0) {
382 ret = irq;
383 goto out;
384 }
385
386 if (request_mem_region(res->start, resource_size(res),
387 dev_name(&dev->dev)) == NULL) {
388 ret = -EBUSY;
389 goto out;
390 }
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100391
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100392 clk = clk_get(&dev->dev, NULL);
393 if (IS_ERR(clk)) {
394 ret = PTR_ERR(clk);
395 goto out_release;
396 }
397
398 clk_enable(clk);
399
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100400 nmk_chip = kzalloc(sizeof(*nmk_chip), GFP_KERNEL);
401 if (!nmk_chip) {
402 ret = -ENOMEM;
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100403 goto out_clk;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100404 }
405 /*
406 * The virt address in nmk_chip->addr is in the nomadik register space,
407 * so we can simply convert the resource address, without remapping
408 */
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100409 nmk_chip->clk = clk;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100410 nmk_chip->addr = io_p2v(res->start);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100411 nmk_chip->chip = nmk_gpio_template;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100412 nmk_chip->parent_irq = irq;
Rabin Vincentc0fcb8d2010-03-03 04:48:54 +0100413 spin_lock_init(&nmk_chip->lock);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100414
415 chip = &nmk_chip->chip;
416 chip->base = pdata->first_gpio;
417 chip->label = pdata->name;
418 chip->dev = &dev->dev;
419 chip->owner = THIS_MODULE;
420
421 ret = gpiochip_add(&nmk_chip->chip);
422 if (ret)
423 goto out_free;
424
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100425 platform_set_drvdata(dev, nmk_chip);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100426
427 nmk_gpio_init_irq(nmk_chip);
428
429 dev_info(&dev->dev, "Bits %i-%i at address %p\n",
430 nmk_chip->chip.base, nmk_chip->chip.base+31, nmk_chip->addr);
431 return 0;
432
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100433out_free:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100434 kfree(nmk_chip);
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100435out_clk:
436 clk_disable(clk);
437 clk_put(clk);
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100438out_release:
439 release_mem_region(res->start, resource_size(res));
440out:
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100441 dev_err(&dev->dev, "Failure %i for GPIO %i-%i\n", ret,
442 pdata->first_gpio, pdata->first_gpio+31);
443 return ret;
444}
445
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100446static int __exit nmk_gpio_remove(struct platform_device *dev)
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100447{
448 struct nmk_gpio_chip *nmk_chip;
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100449 struct resource *res;
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100450
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100451 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
452
453 nmk_chip = platform_get_drvdata(dev);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100454 gpiochip_remove(&nmk_chip->chip);
Rabin Vincentaf7dc222010-05-06 11:14:17 +0100455 clk_disable(nmk_chip->clk);
456 clk_put(nmk_chip->clk);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100457 kfree(nmk_chip);
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100458 release_mem_region(res->start, resource_size(res));
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100459 return 0;
460}
461
462
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100463static struct platform_driver nmk_gpio_driver = {
464 .driver = {
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100465 .owner = THIS_MODULE,
466 .name = "gpio",
467 },
468 .probe = nmk_gpio_probe,
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100469 .remove = __exit_p(nmk_gpio_remove),
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100470 .suspend = NULL, /* to be done */
471 .resume = NULL,
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100472};
473
474static int __init nmk_gpio_init(void)
475{
Rabin Vincent3e3c62c2010-03-03 04:52:34 +0100476 return platform_driver_register(&nmk_gpio_driver);
Alessandro Rubini2ec1d352009-07-02 15:29:12 +0100477}
478
479arch_initcall(nmk_gpio_init);
480
481MODULE_AUTHOR("Prafulla WADASKAR and Alessandro Rubini");
482MODULE_DESCRIPTION("Nomadik GPIO Driver");
483MODULE_LICENSE("GPL");
484
485