2 * Copyright (C) ST-Ericsson SA 2011
4 * Author: BIBEK BASU <bibek.basu@stericsson.com>
5 * License terms: GNU General Public License (GPL) version 2
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/slab.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/err.h>
17 #include <linux/platform_device.h>
18 #include <linux/gpio.h>
19 #include <linux/irq.h>
20 #include <linux/interrupt.h>
21 #include <linux/mfd/ab8500.h>
22 #include <linux/mfd/abx500.h>
23 #include <linux/mfd/ab8500/gpio.h>
26 * GPIO registers offset
29 #define AB8500_GPIO_SEL1_REG 0x00
30 #define AB8500_GPIO_SEL2_REG 0x01
31 #define AB8500_GPIO_SEL3_REG 0x02
32 #define AB8500_GPIO_SEL4_REG 0x03
33 #define AB8500_GPIO_SEL5_REG 0x04
34 #define AB8500_GPIO_SEL6_REG 0x05
36 #define AB8500_GPIO_DIR1_REG 0x10
37 #define AB8500_GPIO_DIR2_REG 0x11
38 #define AB8500_GPIO_DIR3_REG 0x12
39 #define AB8500_GPIO_DIR4_REG 0x13
40 #define AB8500_GPIO_DIR5_REG 0x14
41 #define AB8500_GPIO_DIR6_REG 0x15
43 #define AB8500_GPIO_OUT1_REG 0x20
44 #define AB8500_GPIO_OUT2_REG 0x21
45 #define AB8500_GPIO_OUT3_REG 0x22
46 #define AB8500_GPIO_OUT4_REG 0x23
47 #define AB8500_GPIO_OUT5_REG 0x24
48 #define AB8500_GPIO_OUT6_REG 0x25
50 #define AB8500_GPIO_PUD1_REG 0x30
51 #define AB8500_GPIO_PUD2_REG 0x31
52 #define AB8500_GPIO_PUD3_REG 0x32
53 #define AB8500_GPIO_PUD4_REG 0x33
54 #define AB8500_GPIO_PUD5_REG 0x34
55 #define AB8500_GPIO_PUD6_REG 0x35
57 #define AB8500_GPIO_IN1_REG 0x40
58 #define AB8500_GPIO_IN2_REG 0x41
59 #define AB8500_GPIO_IN3_REG 0x42
60 #define AB8500_GPIO_IN4_REG 0x43
61 #define AB8500_GPIO_IN5_REG 0x44
62 #define AB8500_GPIO_IN6_REG 0x45
63 #define AB8500_GPIO_ALTFUN_REG 0x45
64 #define ALTFUN_REG_INDEX 6
65 #define AB8500_NUM_GPIO 42
66 #define AB8500_NUM_VIR_GPIO_IRQ 16
68 enum ab8500_gpio_action {
77 struct gpio_chip chip;
78 struct ab8500 *parent;
82 enum ab8500_gpio_action irq_action;
87 * to_ab8500_gpio() - get the pointer to ab8500_gpio
88 * @chip: Member of the structure ab8500_gpio
90 static inline struct ab8500_gpio *to_ab8500_gpio(struct gpio_chip *chip)
92 return container_of(chip, struct ab8500_gpio, chip);
95 static int ab8500_gpio_set_bits(struct gpio_chip *chip, u8 reg,
96 unsigned offset, int val)
98 struct ab8500_gpio *ab8500_gpio = to_ab8500_gpio(chip);
102 reg = reg + (offset / 8);
103 ret = abx500_mask_and_set_register_interruptible(ab8500_gpio->dev,
104 AB8500_MISC, reg, 1 << pos, val << pos);
106 dev_err(ab8500_gpio->dev, "%s write failed\n", __func__);
110 * ab8500_gpio_get() - Get the particular GPIO value
112 * @offset: GPIO number to read
114 static int ab8500_gpio_get(struct gpio_chip *chip, unsigned offset)
116 struct ab8500_gpio *ab8500_gpio = to_ab8500_gpio(chip);
117 u8 mask = 1 << (offset % 8);
118 u8 reg = AB8500_GPIO_OUT1_REG + (offset / 8);
121 ret = abx500_get_register_interruptible(ab8500_gpio->dev, AB8500_MISC,
124 dev_err(ab8500_gpio->dev, "%s read failed\n", __func__);
127 return (data & mask) >> (offset % 8);
130 static void ab8500_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
132 struct ab8500_gpio *ab8500_gpio = to_ab8500_gpio(chip);
135 ret = ab8500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, 1);
137 dev_err(ab8500_gpio->dev, "%s write failed\n", __func__);
140 static int ab8500_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
144 /* set direction as output */
145 ret = ab8500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 1);
148 /* disable pull down */
149 ret = ab8500_gpio_set_bits(chip, AB8500_GPIO_PUD1_REG, offset, 1);
152 /* set the output as 1 or 0 */
153 return ab8500_gpio_set_bits(chip, AB8500_GPIO_OUT1_REG, offset, val);
157 static int ab8500_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
159 /* set the register as input */
160 return ab8500_gpio_set_bits(chip, AB8500_GPIO_DIR1_REG, offset, 0);
163 static int ab8500_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
166 * Only some GPIOs are interrupt capable, and they are
167 * organized in discontiguous clusters:
173 static struct ab8500_gpio_irq_cluster {
177 {.start = 6, .end = 13},
178 {.start = 24, .end = 25},
179 {.start = 36, .end = 41},
181 struct ab8500_gpio *ab8500_gpio = to_ab8500_gpio(chip);
182 int base = ab8500_gpio->irq_base;
185 for (i = 0; i < ARRAY_SIZE(clusters); i++) {
186 struct ab8500_gpio_irq_cluster *cluster = &clusters[i];
188 if (offset >= cluster->start && offset <= cluster->end)
189 return base + offset - cluster->start;
191 /* Advance by the number of gpios in this cluster */
192 base += cluster->end - cluster->start + 1;
198 static struct gpio_chip ab8500gpio_chip = {
199 .label = "ab8500_gpio",
200 .owner = THIS_MODULE,
201 .direction_input = ab8500_gpio_direction_input,
202 .get = ab8500_gpio_get,
203 .direction_output = ab8500_gpio_direction_output,
204 .set = ab8500_gpio_set,
205 .to_irq = ab8500_gpio_to_irq,
208 static unsigned int irq_to_rising(unsigned int irq)
210 struct ab8500_gpio *ab8500_gpio = get_irq_chip_data(irq);
211 int offset = irq - ab8500_gpio->irq_base;
212 int new_irq = offset + AB8500_INT_GPIO6R
213 + ab8500_gpio->parent->irq_base;
217 static unsigned int irq_to_falling(unsigned int irq)
219 struct ab8500_gpio *ab8500_gpio = get_irq_chip_data(irq);
220 int offset = irq - ab8500_gpio->irq_base;
221 int new_irq = offset + AB8500_INT_GPIO6F
222 + ab8500_gpio->parent->irq_base;
227 static unsigned int rising_to_irq(unsigned int irq, void *dev)
229 struct ab8500_gpio *ab8500_gpio = dev;
230 int offset = irq - AB8500_INT_GPIO6R
231 - ab8500_gpio->parent->irq_base ;
232 int new_irq = offset + ab8500_gpio->irq_base;
236 static unsigned int falling_to_irq(unsigned int irq, void *dev)
238 struct ab8500_gpio *ab8500_gpio = dev;
239 int offset = irq - AB8500_INT_GPIO6F
240 - ab8500_gpio->parent->irq_base ;
241 int new_irq = offset + ab8500_gpio->irq_base;
250 static irqreturn_t handle_rising(int irq, void *dev)
253 handle_nested_irq(rising_to_irq(irq , dev));
257 static irqreturn_t handle_falling(int irq, void *dev)
260 handle_nested_irq(falling_to_irq(irq, dev));
264 static void ab8500_gpio_irq_lock(unsigned int irq)
266 struct ab8500_gpio *ab8500_gpio = get_irq_chip_data(irq);
267 mutex_lock(&ab8500_gpio->lock);
270 static void ab8500_gpio_irq_sync_unlock(unsigned int irq)
272 struct ab8500_gpio *ab8500_gpio = get_irq_chip_data(irq);
273 int offset = irq - ab8500_gpio->irq_base;
274 bool rising = ab8500_gpio->rising & BIT(offset);
275 bool falling = ab8500_gpio->falling & BIT(offset);
278 switch (ab8500_gpio->irq_action) {
281 ret = request_threaded_irq(irq_to_rising(irq),
284 "ab8500-gpio-r", ab8500_gpio);
286 ret = request_threaded_irq(irq_to_falling(irq),
287 NULL, handle_falling,
288 IRQF_TRIGGER_FALLING,
289 "ab8500-gpio-f", ab8500_gpio);
293 free_irq(irq_to_rising(irq), ab8500_gpio);
295 free_irq(irq_to_falling(irq), ab8500_gpio);
299 disable_irq(irq_to_rising(irq));
301 disable_irq(irq_to_falling(irq));
305 enable_irq(irq_to_rising(irq));
307 enable_irq(irq_to_falling(irq));
312 ab8500_gpio->irq_action = NONE;
313 ab8500_gpio->rising &= ~(BIT(offset));
314 ab8500_gpio->falling &= ~(BIT(offset));
315 mutex_unlock(&ab8500_gpio->lock);
319 static void ab8500_gpio_irq_mask(unsigned int irq)
321 struct ab8500_gpio *ab8500_gpio = get_irq_chip_data(irq);
322 ab8500_gpio->irq_action = MASK;
325 static void ab8500_gpio_irq_unmask(unsigned int irq)
327 struct ab8500_gpio *ab8500_gpio = get_irq_chip_data(irq);
328 ab8500_gpio->irq_action = UNMASK;
331 static int ab8500_gpio_irq_set_type(unsigned int irq, unsigned int type)
333 struct ab8500_gpio *ab8500_gpio = get_irq_chip_data(irq);
334 int offset = irq - ab8500_gpio->irq_base;
336 if (type == IRQ_TYPE_EDGE_BOTH) {
337 ab8500_gpio->rising = BIT(offset);
338 ab8500_gpio->falling = BIT(offset);
339 } else if (type == IRQ_TYPE_EDGE_RISING) {
340 ab8500_gpio->rising = BIT(offset);
342 ab8500_gpio->falling = BIT(offset);
347 unsigned int ab8500_gpio_irq_startup(unsigned int irq)
349 struct ab8500_gpio *ab8500_gpio = get_irq_chip_data(irq);
350 ab8500_gpio->irq_action = STARTUP;
354 void ab8500_gpio_irq_shutdown(unsigned int irq)
356 struct ab8500_gpio *ab8500_gpio = get_irq_chip_data(irq);
357 ab8500_gpio->irq_action = SHUTDOWN;
360 static struct irq_chip ab8500_gpio_irq_chip = {
361 .name = "ab8500-gpio",
362 .startup = ab8500_gpio_irq_startup,
363 .shutdown = ab8500_gpio_irq_shutdown,
364 .bus_lock = ab8500_gpio_irq_lock,
365 .bus_sync_unlock = ab8500_gpio_irq_sync_unlock,
366 .mask = ab8500_gpio_irq_mask,
367 .unmask = ab8500_gpio_irq_unmask,
368 .set_type = ab8500_gpio_irq_set_type,
371 static int ab8500_gpio_irq_init(struct ab8500_gpio *ab8500_gpio)
373 u32 base = ab8500_gpio->irq_base;
376 for (irq = base; irq < base + AB8500_NUM_VIR_GPIO_IRQ ; irq++) {
377 set_irq_chip_data(irq, ab8500_gpio);
378 set_irq_chip_and_handler(irq, &ab8500_gpio_irq_chip,
380 set_irq_nested_thread(irq, 1);
382 set_irq_flags(irq, IRQF_VALID);
384 set_irq_noprobe(irq);
391 static void ab8500_gpio_irq_remove(struct ab8500_gpio *ab8500_gpio)
393 int base = ab8500_gpio->irq_base;
396 for (irq = base; irq < base + AB8500_NUM_VIR_GPIO_IRQ; irq++) {
398 set_irq_flags(irq, 0);
400 set_irq_chip_and_handler(irq, NULL, NULL);
401 set_irq_chip_data(irq, NULL);
405 static int __devinit ab8500_gpio_probe(struct platform_device *pdev)
407 struct ab8500_platform_data *ab8500_pdata =
408 dev_get_platdata(pdev->dev.parent);
409 struct ab8500_gpio_platform_data *pdata;
410 struct ab8500_gpio *ab8500_gpio;
414 pdata = ab8500_pdata->gpio;
416 dev_err(&pdev->dev, "gpio platform data missing\n");
420 ab8500_gpio = kzalloc(sizeof(struct ab8500_gpio), GFP_KERNEL);
421 if (ab8500_gpio == NULL) {
422 dev_err(&pdev->dev, "failed to allocate memory\n");
425 ab8500_gpio->dev = &pdev->dev;
426 ab8500_gpio->parent = dev_get_drvdata(pdev->dev.parent);
427 ab8500_gpio->chip = ab8500gpio_chip;
428 ab8500_gpio->chip.ngpio = AB8500_NUM_GPIO;
429 ab8500_gpio->chip.dev = &pdev->dev;
430 ab8500_gpio->chip.base = pdata->gpio_base;
431 ab8500_gpio->irq_base = pdata->irq_base;
432 /* initialize the lock */
433 mutex_init(&ab8500_gpio->lock);
435 * AB8500 core will handle and clear the IRQ
436 * configre GPIO based on config-reg value.
437 * These values are for selecting the PINs as
438 * GPIO or alternate function
440 for (i = AB8500_GPIO_SEL1_REG; i <= AB8500_GPIO_SEL6_REG; i++) {
441 ret = abx500_set_register_interruptible(ab8500_gpio->dev,
443 pdata->config_reg[i]);
447 ret = abx500_set_register_interruptible(ab8500_gpio->dev, AB8500_MISC,
448 AB8500_GPIO_ALTFUN_REG,
449 pdata->config_reg[ALTFUN_REG_INDEX]);
453 ret = ab8500_gpio_irq_init(ab8500_gpio);
456 ret = gpiochip_add(&ab8500_gpio->chip);
458 dev_err(&pdev->dev, "unable to add gpiochip: %d\n",
462 platform_set_drvdata(pdev, ab8500_gpio);
466 ab8500_gpio_irq_remove(ab8500_gpio);
468 mutex_destroy(&ab8500_gpio->lock);
474 * ab8500_gpio_remove() - remove Ab8500-gpio driver
475 * @pdev : Platform device registered
477 static int __devexit ab8500_gpio_remove(struct platform_device *pdev)
479 struct ab8500_gpio *ab8500_gpio = platform_get_drvdata(pdev);
482 ret = gpiochip_remove(&ab8500_gpio->chip);
484 dev_err(ab8500_gpio->dev, "unable to remove gpiochip: %d\n",
489 platform_set_drvdata(pdev, NULL);
490 mutex_destroy(&ab8500_gpio->lock);
496 static struct platform_driver ab8500_gpio_driver = {
498 .name = "ab8500-gpio",
499 .owner = THIS_MODULE,
501 .probe = ab8500_gpio_probe,
502 .remove = __devexit_p(ab8500_gpio_remove),
505 static int __init ab8500_gpio_init(void)
507 return platform_driver_register(&ab8500_gpio_driver);
509 arch_initcall(ab8500_gpio_init);
511 static void __exit ab8500_gpio_exit(void)
513 platform_driver_unregister(&ab8500_gpio_driver);
515 module_exit(ab8500_gpio_exit);
517 MODULE_AUTHOR("BIBEK BASU <bibek.basu@stericsson.com>");
518 MODULE_DESCRIPTION("Driver allows to use AB8500 unused pins to be used as GPIO");
519 MODULE_ALIAS("platform:ab8500-gpio");
520 MODULE_LICENSE("GPL v2");