2 * SPEAr platform PLGPIO driver
4 * Copyright (C) 2012 ST Microelectronics
5 * Viresh Kumar <viresh.kumar@linaro.org>
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
12 #include <linux/clk.h>
13 #include <linux/err.h>
14 #include <linux/gpio.h>
16 #include <linux/irq.h>
17 #include <linux/irqdomain.h>
18 #include <linux/module.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/platform_device.h>
22 #include <linux/spinlock.h>
23 #include <asm/mach/irq.h>
25 #define MAX_GPIO_PER_REG 32
26 #define PIN_OFFSET(pin) (pin % MAX_GPIO_PER_REG)
27 #define REG_OFFSET(base, reg, pin) (base + reg + (pin / MAX_GPIO_PER_REG) \
31 * plgpio pins in all machines are not one to one mapped, bitwise with registers
32 * bits. These set of macros define register masks for which below functions
33 * (pin_to_offset and offset_to_pin) are required to be called.
35 #define PTO_ENB_REG 0x001
36 #define PTO_WDATA_REG 0x002
37 #define PTO_DIR_REG 0x004
38 #define PTO_IE_REG 0x008
39 #define PTO_RDATA_REG 0x010
40 #define PTO_MIS_REG 0x020
43 u32 enb; /* enable register */
44 u32 wdata; /* write data register */
45 u32 dir; /* direction set register */
46 u32 rdata; /* read data register */
47 u32 ie; /* interrupt enable register */
48 u32 mis; /* mask interrupt status register */
49 u32 eit; /* edge interrupt type */
53 * struct plgpio: plgpio driver specific structure
55 * lock: lock for guarding gpio registers
56 * base: base address of plgpio block
57 * irq_base: irq number of plgpio0
58 * chip: gpio framework specific chip information structure
59 * p2o: function ptr for pin to offset conversion. This is required only for
60 * machines where mapping b/w pin and offset is not 1-to-1.
61 * o2p: function ptr for offset to pin conversion. This is required only for
62 * machines where mapping b/w pin and offset is not 1-to-1.
63 * p2o_regs: mask of registers for which p2o and o2p are applicable
64 * regs: register offsets
65 * csave_regs: context save registers for standby/sleep/hibernate cases
72 struct irq_domain *irq_domain;
73 struct gpio_chip chip;
74 int (*p2o)(int pin); /* pin_to_offset */
75 int (*o2p)(int offset); /* offset_to_pin */
77 struct plgpio_regs regs;
79 struct plgpio_regs *csave_regs;
83 /* register manipulation inline functions */
84 static inline u32 is_plgpio_set(void __iomem *base, u32 pin, u32 reg)
86 u32 offset = PIN_OFFSET(pin);
87 void __iomem *reg_off = REG_OFFSET(base, reg, pin);
88 u32 val = readl_relaxed(reg_off);
90 return !!(val & (1 << offset));
93 static inline void plgpio_reg_set(void __iomem *base, u32 pin, u32 reg)
95 u32 offset = PIN_OFFSET(pin);
96 void __iomem *reg_off = REG_OFFSET(base, reg, pin);
97 u32 val = readl_relaxed(reg_off);
99 writel_relaxed(val | (1 << offset), reg_off);
102 static inline void plgpio_reg_reset(void __iomem *base, u32 pin, u32 reg)
104 u32 offset = PIN_OFFSET(pin);
105 void __iomem *reg_off = REG_OFFSET(base, reg, pin);
106 u32 val = readl_relaxed(reg_off);
108 writel_relaxed(val & ~(1 << offset), reg_off);
111 /* gpio framework specific routines */
112 static int plgpio_direction_input(struct gpio_chip *chip, unsigned offset)
114 struct plgpio *plgpio = container_of(chip, struct plgpio, chip);
117 /* get correct offset for "offset" pin */
118 if (plgpio->p2o && (plgpio->p2o_regs & PTO_DIR_REG)) {
119 offset = plgpio->p2o(offset);
124 spin_lock_irqsave(&plgpio->lock, flags);
125 plgpio_reg_set(plgpio->base, offset, plgpio->regs.dir);
126 spin_unlock_irqrestore(&plgpio->lock, flags);
131 static int plgpio_direction_output(struct gpio_chip *chip, unsigned offset,
134 struct plgpio *plgpio = container_of(chip, struct plgpio, chip);
136 unsigned dir_offset = offset, wdata_offset = offset, tmp;
138 /* get correct offset for "offset" pin */
139 if (plgpio->p2o && (plgpio->p2o_regs & (PTO_DIR_REG | PTO_WDATA_REG))) {
140 tmp = plgpio->p2o(offset);
144 if (plgpio->p2o_regs & PTO_DIR_REG)
146 if (plgpio->p2o_regs & PTO_WDATA_REG)
150 spin_lock_irqsave(&plgpio->lock, flags);
152 plgpio_reg_set(plgpio->base, wdata_offset,
155 plgpio_reg_reset(plgpio->base, wdata_offset,
158 plgpio_reg_reset(plgpio->base, dir_offset, plgpio->regs.dir);
159 spin_unlock_irqrestore(&plgpio->lock, flags);
164 static int plgpio_get_value(struct gpio_chip *chip, unsigned offset)
166 struct plgpio *plgpio = container_of(chip, struct plgpio, chip);
168 if (offset >= chip->ngpio)
171 /* get correct offset for "offset" pin */
172 if (plgpio->p2o && (plgpio->p2o_regs & PTO_RDATA_REG)) {
173 offset = plgpio->p2o(offset);
178 return is_plgpio_set(plgpio->base, offset, plgpio->regs.rdata);
181 static void plgpio_set_value(struct gpio_chip *chip, unsigned offset, int value)
183 struct plgpio *plgpio = container_of(chip, struct plgpio, chip);
185 if (offset >= chip->ngpio)
188 /* get correct offset for "offset" pin */
189 if (plgpio->p2o && (plgpio->p2o_regs & PTO_WDATA_REG)) {
190 offset = plgpio->p2o(offset);
196 plgpio_reg_set(plgpio->base, offset, plgpio->regs.wdata);
198 plgpio_reg_reset(plgpio->base, offset, plgpio->regs.wdata);
201 static int plgpio_request(struct gpio_chip *chip, unsigned offset)
203 struct plgpio *plgpio = container_of(chip, struct plgpio, chip);
204 int gpio = chip->base + offset;
208 if (offset >= chip->ngpio)
211 ret = pinctrl_request_gpio(gpio);
215 if (!IS_ERR(plgpio->clk)) {
216 ret = clk_enable(plgpio->clk);
221 if (plgpio->regs.enb == -1)
225 * put gpio in IN mode before enabling it. This make enabling gpio safe
227 ret = plgpio_direction_input(chip, offset);
231 /* get correct offset for "offset" pin */
232 if (plgpio->p2o && (plgpio->p2o_regs & PTO_ENB_REG)) {
233 offset = plgpio->p2o(offset);
240 spin_lock_irqsave(&plgpio->lock, flags);
241 plgpio_reg_set(plgpio->base, offset, plgpio->regs.enb);
242 spin_unlock_irqrestore(&plgpio->lock, flags);
246 if (!IS_ERR(plgpio->clk))
247 clk_disable(plgpio->clk);
249 pinctrl_free_gpio(gpio);
253 static void plgpio_free(struct gpio_chip *chip, unsigned offset)
255 struct plgpio *plgpio = container_of(chip, struct plgpio, chip);
256 int gpio = chip->base + offset;
259 if (offset >= chip->ngpio)
262 if (plgpio->regs.enb == -1)
265 /* get correct offset for "offset" pin */
266 if (plgpio->p2o && (plgpio->p2o_regs & PTO_ENB_REG)) {
267 offset = plgpio->p2o(offset);
272 spin_lock_irqsave(&plgpio->lock, flags);
273 plgpio_reg_reset(plgpio->base, offset, plgpio->regs.enb);
274 spin_unlock_irqrestore(&plgpio->lock, flags);
277 if (!IS_ERR(plgpio->clk))
278 clk_disable(plgpio->clk);
280 pinctrl_free_gpio(gpio);
283 static int plgpio_to_irq(struct gpio_chip *chip, unsigned offset)
285 struct plgpio *plgpio = container_of(chip, struct plgpio, chip);
287 if (IS_ERR_VALUE(plgpio->irq_base))
290 return irq_find_mapping(plgpio->irq_domain, offset);
294 static void plgpio_irq_disable(struct irq_data *d)
296 struct plgpio *plgpio = irq_data_get_irq_chip_data(d);
297 int offset = d->irq - plgpio->irq_base;
300 /* get correct offset for "offset" pin */
301 if (plgpio->p2o && (plgpio->p2o_regs & PTO_IE_REG)) {
302 offset = plgpio->p2o(offset);
307 spin_lock_irqsave(&plgpio->lock, flags);
308 plgpio_reg_set(plgpio->base, offset, plgpio->regs.ie);
309 spin_unlock_irqrestore(&plgpio->lock, flags);
312 static void plgpio_irq_enable(struct irq_data *d)
314 struct plgpio *plgpio = irq_data_get_irq_chip_data(d);
315 int offset = d->irq - plgpio->irq_base;
318 /* get correct offset for "offset" pin */
319 if (plgpio->p2o && (plgpio->p2o_regs & PTO_IE_REG)) {
320 offset = plgpio->p2o(offset);
325 spin_lock_irqsave(&plgpio->lock, flags);
326 plgpio_reg_reset(plgpio->base, offset, plgpio->regs.ie);
327 spin_unlock_irqrestore(&plgpio->lock, flags);
330 static int plgpio_irq_set_type(struct irq_data *d, unsigned trigger)
332 struct plgpio *plgpio = irq_data_get_irq_chip_data(d);
333 int offset = d->irq - plgpio->irq_base;
334 void __iomem *reg_off;
335 unsigned int supported_type = 0, val;
337 if (offset >= plgpio->chip.ngpio)
340 if (plgpio->regs.eit == -1)
341 supported_type = IRQ_TYPE_LEVEL_HIGH;
343 supported_type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
345 if (!(trigger & supported_type))
348 if (plgpio->regs.eit == -1)
351 reg_off = REG_OFFSET(plgpio->base, plgpio->regs.eit, offset);
352 val = readl_relaxed(reg_off);
354 offset = PIN_OFFSET(offset);
355 if (trigger & IRQ_TYPE_EDGE_RISING)
356 writel_relaxed(val | (1 << offset), reg_off);
358 writel_relaxed(val & ~(1 << offset), reg_off);
363 static struct irq_chip plgpio_irqchip = {
365 .irq_enable = plgpio_irq_enable,
366 .irq_disable = plgpio_irq_disable,
367 .irq_set_type = plgpio_irq_set_type,
370 static void plgpio_irq_handler(unsigned irq, struct irq_desc *desc)
372 struct plgpio *plgpio = irq_get_handler_data(irq);
373 struct irq_chip *irqchip = irq_desc_get_chip(desc);
374 int regs_count, count, pin, offset, i = 0;
375 unsigned long pending;
377 count = plgpio->chip.ngpio;
378 regs_count = DIV_ROUND_UP(count, MAX_GPIO_PER_REG);
380 chained_irq_enter(irqchip, desc);
381 /* check all plgpio MIS registers for a possible interrupt */
382 for (; i < regs_count; i++) {
383 pending = readl_relaxed(plgpio->base + plgpio->regs.mis +
388 /* clear interrupts */
389 writel_relaxed(~pending, plgpio->base + plgpio->regs.mis +
392 * clear extra bits in last register having gpios < MAX/REG
393 * ex: Suppose there are max 102 plgpios. then last register
394 * must have only (102 - MAX_GPIO_PER_REG * 3) = 6 relevant bits
395 * so, we must not take other 28 bits into consideration for
396 * checking interrupt. so clear those bits.
398 count = count - i * MAX_GPIO_PER_REG;
399 if (count < MAX_GPIO_PER_REG)
400 pending &= (1 << count) - 1;
402 for_each_set_bit(offset, &pending, MAX_GPIO_PER_REG) {
403 /* get correct pin for "offset" */
404 if (plgpio->o2p && (plgpio->p2o_regs & PTO_MIS_REG)) {
405 pin = plgpio->o2p(offset);
411 /* get correct irq line number */
412 pin = i * MAX_GPIO_PER_REG + pin;
413 generic_handle_irq(plgpio_to_irq(&plgpio->chip, pin));
416 chained_irq_exit(irqchip, desc);
420 * pin to offset and offset to pin converter functions
422 * In spear310 there is inconsistency among bit positions in plgpio regiseters,
423 * for different plgpio pins. For example: for pin 27, bit offset is 23, pin
424 * 28-33 are not supported, pin 95 has offset bit 95, bit 100 has offset bit 1
426 static int spear310_p2o(int pin)
444 int spear310_o2p(int offset)
448 else if (offset <= 31)
454 static int plgpio_probe_dt(struct platform_device *pdev, struct plgpio *plgpio)
456 struct device_node *np = pdev->dev.of_node;
460 if (of_machine_is_compatible("st,spear310")) {
461 plgpio->p2o = spear310_p2o;
462 plgpio->o2p = spear310_o2p;
463 plgpio->p2o_regs = PTO_WDATA_REG | PTO_DIR_REG | PTO_IE_REG |
464 PTO_RDATA_REG | PTO_MIS_REG;
467 if (!of_property_read_u32(np, "st-plgpio,ngpio", &val)) {
468 plgpio->chip.ngpio = val;
470 dev_err(&pdev->dev, "DT: Invalid ngpio field\n");
474 if (!of_property_read_u32(np, "st-plgpio,enb-reg", &val))
475 plgpio->regs.enb = val;
477 plgpio->regs.enb = -1;
479 if (!of_property_read_u32(np, "st-plgpio,wdata-reg", &val)) {
480 plgpio->regs.wdata = val;
482 dev_err(&pdev->dev, "DT: Invalid wdata reg\n");
486 if (!of_property_read_u32(np, "st-plgpio,dir-reg", &val)) {
487 plgpio->regs.dir = val;
489 dev_err(&pdev->dev, "DT: Invalid dir reg\n");
493 if (!of_property_read_u32(np, "st-plgpio,ie-reg", &val)) {
494 plgpio->regs.ie = val;
496 dev_err(&pdev->dev, "DT: Invalid ie reg\n");
500 if (!of_property_read_u32(np, "st-plgpio,rdata-reg", &val)) {
501 plgpio->regs.rdata = val;
503 dev_err(&pdev->dev, "DT: Invalid rdata reg\n");
507 if (!of_property_read_u32(np, "st-plgpio,mis-reg", &val)) {
508 plgpio->regs.mis = val;
510 dev_err(&pdev->dev, "DT: Invalid mis reg\n");
514 if (!of_property_read_u32(np, "st-plgpio,eit-reg", &val))
515 plgpio->regs.eit = val;
517 plgpio->regs.eit = -1;
524 static int plgpio_probe(struct platform_device *pdev)
526 struct device_node *np = pdev->dev.of_node;
527 struct plgpio *plgpio;
528 struct resource *res;
531 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
533 dev_err(&pdev->dev, "invalid IORESOURCE_MEM\n");
537 plgpio = devm_kzalloc(&pdev->dev, sizeof(*plgpio), GFP_KERNEL);
539 dev_err(&pdev->dev, "memory allocation fail\n");
543 plgpio->base = devm_request_and_ioremap(&pdev->dev, res);
545 dev_err(&pdev->dev, "request and ioremap fail\n");
549 ret = plgpio_probe_dt(pdev, plgpio);
551 dev_err(&pdev->dev, "DT probe failed\n");
555 plgpio->clk = devm_clk_get(&pdev->dev, NULL);
556 if (IS_ERR(plgpio->clk))
557 dev_warn(&pdev->dev, "clk_get() failed, work without it\n");
560 plgpio->csave_regs = devm_kzalloc(&pdev->dev,
561 sizeof(*plgpio->csave_regs) *
562 DIV_ROUND_UP(plgpio->chip.ngpio, MAX_GPIO_PER_REG),
564 if (!plgpio->csave_regs) {
565 dev_err(&pdev->dev, "csave registers memory allocation fail\n");
570 platform_set_drvdata(pdev, plgpio);
571 spin_lock_init(&plgpio->lock);
573 plgpio->irq_base = -1;
574 plgpio->chip.base = -1;
575 plgpio->chip.request = plgpio_request;
576 plgpio->chip.free = plgpio_free;
577 plgpio->chip.direction_input = plgpio_direction_input;
578 plgpio->chip.direction_output = plgpio_direction_output;
579 plgpio->chip.get = plgpio_get_value;
580 plgpio->chip.set = plgpio_set_value;
581 plgpio->chip.to_irq = plgpio_to_irq;
582 plgpio->chip.label = dev_name(&pdev->dev);
583 plgpio->chip.dev = &pdev->dev;
584 plgpio->chip.owner = THIS_MODULE;
586 if (!IS_ERR(plgpio->clk)) {
587 ret = clk_prepare(plgpio->clk);
589 dev_err(&pdev->dev, "clk prepare failed\n");
594 ret = gpiochip_add(&plgpio->chip);
596 dev_err(&pdev->dev, "unable to add gpio chip\n");
600 irq = platform_get_irq(pdev, 0);
602 dev_info(&pdev->dev, "irqs not supported\n");
606 plgpio->irq_base = irq_alloc_descs(-1, 0, plgpio->chip.ngpio, 0);
607 if (IS_ERR_VALUE(plgpio->irq_base)) {
608 /* we would not support irq for gpio */
609 dev_warn(&pdev->dev, "couldn't allocate irq base\n");
613 plgpio->irq_domain = irq_domain_add_legacy(np, plgpio->chip.ngpio,
614 plgpio->irq_base, 0, &irq_domain_simple_ops, NULL);
615 if (WARN_ON(!plgpio->irq_domain)) {
616 dev_err(&pdev->dev, "irq domain init failed\n");
617 irq_free_descs(plgpio->irq_base, plgpio->chip.ngpio);
619 goto remove_gpiochip;
622 irq_set_chained_handler(irq, plgpio_irq_handler);
623 for (i = 0; i < plgpio->chip.ngpio; i++) {
624 irq_set_chip_and_handler(i + plgpio->irq_base, &plgpio_irqchip,
626 set_irq_flags(i + plgpio->irq_base, IRQF_VALID);
627 irq_set_chip_data(i + plgpio->irq_base, plgpio);
630 irq_set_handler_data(irq, plgpio);
631 dev_info(&pdev->dev, "PLGPIO registered with IRQs\n");
636 dev_info(&pdev->dev, "Remove gpiochip\n");
637 if (gpiochip_remove(&plgpio->chip))
638 dev_err(&pdev->dev, "unable to remove gpiochip\n");
640 if (!IS_ERR(plgpio->clk))
641 clk_unprepare(plgpio->clk);
647 static int plgpio_suspend(struct device *dev)
649 struct plgpio *plgpio = dev_get_drvdata(dev);
650 int i, reg_count = DIV_ROUND_UP(plgpio->chip.ngpio, MAX_GPIO_PER_REG);
653 for (i = 0; i < reg_count; i++) {
654 off = plgpio->base + i * sizeof(int *);
656 if (plgpio->regs.enb != -1)
657 plgpio->csave_regs[i].enb =
658 readl_relaxed(plgpio->regs.enb + off);
659 if (plgpio->regs.eit != -1)
660 plgpio->csave_regs[i].eit =
661 readl_relaxed(plgpio->regs.eit + off);
662 plgpio->csave_regs[i].wdata = readl_relaxed(plgpio->regs.wdata +
664 plgpio->csave_regs[i].dir = readl_relaxed(plgpio->regs.dir +
666 plgpio->csave_regs[i].ie = readl_relaxed(plgpio->regs.ie + off);
673 * This is used to correct the values in end registers. End registers contain
674 * extra bits that might be used for other purpose in platform. So, we shouldn't
675 * overwrite these bits. This macro, reads given register again, preserves other
676 * bit values (non-plgpio bits), and retain captured value (plgpio bits).
678 #define plgpio_prepare_reg(__reg, _off, _mask, _tmp) \
680 _tmp = readl_relaxed(plgpio->regs.__reg + _off); \
682 plgpio->csave_regs[i].__reg = \
683 _tmp | (plgpio->csave_regs[i].__reg & _mask); \
686 static int plgpio_resume(struct device *dev)
688 struct plgpio *plgpio = dev_get_drvdata(dev);
689 int i, reg_count = DIV_ROUND_UP(plgpio->chip.ngpio, MAX_GPIO_PER_REG);
693 for (i = 0; i < reg_count; i++) {
694 off = plgpio->base + i * sizeof(int *);
696 if (i == reg_count - 1) {
697 mask = (1 << (plgpio->chip.ngpio - i *
698 MAX_GPIO_PER_REG)) - 1;
700 if (plgpio->regs.enb != -1)
701 plgpio_prepare_reg(enb, off, mask, tmp);
703 if (plgpio->regs.eit != -1)
704 plgpio_prepare_reg(eit, off, mask, tmp);
706 plgpio_prepare_reg(wdata, off, mask, tmp);
707 plgpio_prepare_reg(dir, off, mask, tmp);
708 plgpio_prepare_reg(ie, off, mask, tmp);
711 writel_relaxed(plgpio->csave_regs[i].wdata, plgpio->regs.wdata +
713 writel_relaxed(plgpio->csave_regs[i].dir, plgpio->regs.dir +
716 if (plgpio->regs.eit != -1)
717 writel_relaxed(plgpio->csave_regs[i].eit,
718 plgpio->regs.eit + off);
720 writel_relaxed(plgpio->csave_regs[i].ie, plgpio->regs.ie + off);
722 if (plgpio->regs.enb != -1)
723 writel_relaxed(plgpio->csave_regs[i].enb,
724 plgpio->regs.enb + off);
731 static SIMPLE_DEV_PM_OPS(plgpio_dev_pm_ops, plgpio_suspend, plgpio_resume);
733 static const struct of_device_id plgpio_of_match[] = {
734 { .compatible = "st,spear-plgpio" },
737 MODULE_DEVICE_TABLE(of, plgpio_of_match);
739 static struct platform_driver plgpio_driver = {
740 .probe = plgpio_probe,
742 .owner = THIS_MODULE,
743 .name = "spear-plgpio",
744 .pm = &plgpio_dev_pm_ops,
745 .of_match_table = of_match_ptr(plgpio_of_match),
749 static int __init plgpio_init(void)
751 return platform_driver_register(&plgpio_driver);
753 subsys_initcall(plgpio_init);
755 MODULE_AUTHOR("Viresh Kumar <viresh.kumar@linaro.org>");
756 MODULE_DESCRIPTION("ST Microlectronics SPEAr PLGPIO driver");
757 MODULE_LICENSE("GPL");