11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/slab.h>
15 #include <linux/module.h>
21 #include <linux/mfd/ab8500.h>
23 #include <linux/mfd/ab8500/gpio.h>
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
90 static inline struct ab8500_gpio *to_ab8500_gpio(
struct gpio_chip *
chip)
95 static int ab8500_gpio_set_bits(
struct gpio_chip *
chip,
u8 reg,
102 reg = reg + (offset / 8);
106 dev_err(ab8500_gpio->
dev,
"%s write failed\n", __func__);
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);
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);
137 dev_err(ab8500_gpio->
dev,
"%s write failed\n", __func__);
140 static int ab8500_gpio_direction_output(
struct gpio_chip *chip,
unsigned offset,
157 static int ab8500_gpio_direction_input(
struct gpio_chip *chip,
unsigned offset)
163 static int ab8500_gpio_to_irq(
struct gpio_chip *chip,
unsigned offset)
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);
186 struct ab8500_gpio_irq_cluster *
cluster = &clusters[
i];
189 return base + offset - cluster->start;
192 base += cluster->end - cluster->start + 1;
198 static struct gpio_chip ab8500gpio_chip = {
199 .label =
"ab8500_gpio",
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;
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;
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;
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;
240 - ab8500_gpio->
parent->irq_base ;
241 int new_irq = offset + ab8500_gpio->
irq_base;
257 static irqreturn_t handle_falling(
int irq,
void *dev)
264 static void ab8500_gpio_irq_lock(
unsigned int irq)
266 struct ab8500_gpio *ab8500_gpio = get_irq_chip_data(irq);
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;
284 "ab8500-gpio-r", ab8500_gpio);
287 NULL, handle_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);
319 static void ab8500_gpio_irq_mask(
unsigned int irq)
321 struct ab8500_gpio *ab8500_gpio = get_irq_chip_data(irq);
325 static void ab8500_gpio_irq_unmask(
unsigned int irq)
327 struct ab8500_gpio *ab8500_gpio = get_irq_chip_data(irq);
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;
349 struct ab8500_gpio *ab8500_gpio = get_irq_chip_data(irq);
356 struct ab8500_gpio *ab8500_gpio = get_irq_chip_data(irq);
360 static struct irq_chip ab8500_gpio_irq_chip = {
361 .name =
"ab8500-gpio",
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)
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);
384 set_irq_noprobe(irq);
391 static void ab8500_gpio_irq_remove(
struct ab8500_gpio *ab8500_gpio)
400 set_irq_chip_and_handler(irq,
NULL,
NULL);
401 set_irq_chip_data(irq,
NULL);
408 dev_get_platdata(pdev->
dev.parent);
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;
427 ab8500_gpio->
chip = ab8500gpio_chip;
429 ab8500_gpio->
chip.dev = &pdev->
dev;
453 ret = ab8500_gpio_irq_init(ab8500_gpio);
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);
479 struct ab8500_gpio *ab8500_gpio = platform_get_drvdata(pdev);
484 dev_err(ab8500_gpio->
dev,
"unable to remove gpiochip: %d\n",
489 platform_set_drvdata(pdev,
NULL);
498 .name =
"ab8500-gpio",
501 .probe = ab8500_gpio_probe,
505 static int __init ab8500_gpio_init(
void)
511 static void __exit ab8500_gpio_exit(
void)