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/driver.h>
16 #include <linux/init.h>
17 #include <linux/mfd/syscon.h>
19 #include <linux/of_platform.h>
20 #include <linux/pinctrl/consumer.h>
21 #include <linux/platform_device.h>
23 #include <linux/regmap.h>
24 #include <linux/spinlock.h>
26 #define MAX_GPIO_PER_REG 32
27 #define PIN_OFFSET(pin) (pin % MAX_GPIO_PER_REG)
28 #define REG_OFFSET(base, reg, pin) (base + reg + (pin / MAX_GPIO_PER_REG) \
32 * plgpio pins in all machines are not one to one mapped, bitwise with registers
33 * bits. These set of macros define register masks for which below functions
34 * (pin_to_offset and offset_to_pin) are required to be called.
36 #define PTO_ENB_REG 0x001
37 #define PTO_WDATA_REG 0x002
38 #define PTO_DIR_REG 0x004
39 #define PTO_IE_REG 0x008
40 #define PTO_RDATA_REG 0x010
41 #define PTO_MIS_REG 0x020
44 u32 enb; /* enable register */
45 u32 wdata; /* write data register */
46 u32 dir; /* direction set register */
47 u32 rdata; /* read data register */
48 u32 ie; /* interrupt enable register */
49 u32 mis; /* mask interrupt status register */
50 u32 eit; /* edge interrupt type */
54 * struct plgpio: plgpio driver specific structure
56 * lock: lock for guarding gpio registers
57 * base: base address of plgpio block
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
69 struct regmap *regmap;
71 struct gpio_chip chip;
72 int (*p2o)(int pin); /* pin_to_offset */
73 int (*o2p)(int offset); /* offset_to_pin */
75 struct plgpio_regs regs;
76 #ifdef CONFIG_PM_SLEEP
77 struct plgpio_regs *csave_regs;
81 /* register manipulation inline functions */
82 static inline u32 is_plgpio_set(struct regmap *regmap, u32 pin, u32 reg)
84 u32 offset = PIN_OFFSET(pin);
85 u32 reg_off = REG_OFFSET(0, reg, pin);
88 regmap_read(regmap, reg_off, &val);
90 return !!(val & (1 << offset));
93 static inline void plgpio_reg_set(struct regmap *regmap, u32 pin, u32 reg)
95 u32 offset = PIN_OFFSET(pin);
96 u32 reg_off = REG_OFFSET(0, reg, pin);
100 regmap_update_bits(regmap, reg_off, mask, mask);
103 static inline void plgpio_reg_reset(struct regmap *regmap, u32 pin, u32 reg)
105 u32 offset = PIN_OFFSET(pin);
106 u32 reg_off = REG_OFFSET(0, reg, pin);
110 regmap_update_bits(regmap, reg_off, mask, 0);
114 /* gpio framework specific routines */
115 static int plgpio_direction_input(struct gpio_chip *chip, unsigned offset)
117 struct plgpio *plgpio = gpiochip_get_data(chip);
120 /* get correct offset for "offset" pin */
121 if (plgpio->p2o && (plgpio->p2o_regs & PTO_DIR_REG)) {
122 offset = plgpio->p2o(offset);
127 spin_lock_irqsave(&plgpio->lock, flags);
128 plgpio_reg_set(plgpio->regmap, offset, plgpio->regs.dir);
129 spin_unlock_irqrestore(&plgpio->lock, flags);
134 static int plgpio_direction_output(struct gpio_chip *chip, unsigned offset,
137 struct plgpio *plgpio = gpiochip_get_data(chip);
139 unsigned dir_offset = offset, wdata_offset = offset, tmp;
141 /* get correct offset for "offset" pin */
142 if (plgpio->p2o && (plgpio->p2o_regs & (PTO_DIR_REG | PTO_WDATA_REG))) {
143 tmp = plgpio->p2o(offset);
147 if (plgpio->p2o_regs & PTO_DIR_REG)
149 if (plgpio->p2o_regs & PTO_WDATA_REG)
153 spin_lock_irqsave(&plgpio->lock, flags);
155 plgpio_reg_set(plgpio->regmap, wdata_offset,
158 plgpio_reg_reset(plgpio->regmap, wdata_offset,
161 plgpio_reg_reset(plgpio->regmap, dir_offset, plgpio->regs.dir);
162 spin_unlock_irqrestore(&plgpio->lock, flags);
167 static int plgpio_get_value(struct gpio_chip *chip, unsigned offset)
169 struct plgpio *plgpio = gpiochip_get_data(chip);
171 if (offset >= chip->ngpio)
174 /* get correct offset for "offset" pin */
175 if (plgpio->p2o && (plgpio->p2o_regs & PTO_RDATA_REG)) {
176 offset = plgpio->p2o(offset);
181 return is_plgpio_set(plgpio->regmap, offset, plgpio->regs.rdata);
184 static void plgpio_set_value(struct gpio_chip *chip, unsigned offset, int value)
186 struct plgpio *plgpio = gpiochip_get_data(chip);
188 if (offset >= chip->ngpio)
191 /* get correct offset for "offset" pin */
192 if (plgpio->p2o && (plgpio->p2o_regs & PTO_WDATA_REG)) {
193 offset = plgpio->p2o(offset);
199 plgpio_reg_set(plgpio->regmap, offset, plgpio->regs.wdata);
201 plgpio_reg_reset(plgpio->regmap, offset, plgpio->regs.wdata);
204 static int plgpio_request(struct gpio_chip *chip, unsigned offset)
206 struct plgpio *plgpio = gpiochip_get_data(chip);
210 if (offset >= chip->ngpio)
213 ret = pinctrl_gpio_request(chip, offset);
217 if (!IS_ERR(plgpio->clk)) {
218 ret = clk_enable(plgpio->clk);
223 if (plgpio->regs.enb == -1)
227 * put gpio in IN mode before enabling it. This make enabling gpio safe
229 ret = plgpio_direction_input(chip, offset);
233 /* get correct offset for "offset" pin */
234 if (plgpio->p2o && (plgpio->p2o_regs & PTO_ENB_REG)) {
235 offset = plgpio->p2o(offset);
242 spin_lock_irqsave(&plgpio->lock, flags);
243 plgpio_reg_set(plgpio->regmap, offset, plgpio->regs.enb);
244 spin_unlock_irqrestore(&plgpio->lock, flags);
248 if (!IS_ERR(plgpio->clk))
249 clk_disable(plgpio->clk);
251 pinctrl_gpio_free(chip, offset);
255 static void plgpio_free(struct gpio_chip *chip, unsigned offset)
257 struct plgpio *plgpio = gpiochip_get_data(chip);
260 if (offset >= chip->ngpio)
263 if (plgpio->regs.enb == -1)
266 /* get correct offset for "offset" pin */
267 if (plgpio->p2o && (plgpio->p2o_regs & PTO_ENB_REG)) {
268 offset = plgpio->p2o(offset);
273 spin_lock_irqsave(&plgpio->lock, flags);
274 plgpio_reg_reset(plgpio->regmap, offset, plgpio->regs.enb);
275 spin_unlock_irqrestore(&plgpio->lock, flags);
278 if (!IS_ERR(plgpio->clk))
279 clk_disable(plgpio->clk);
281 pinctrl_gpio_free(chip, offset);
285 static void plgpio_irq_disable(struct irq_data *d)
287 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
288 struct plgpio *plgpio = gpiochip_get_data(gc);
289 int offset = d->hwirq;
292 /* get correct offset for "offset" pin */
293 if (plgpio->p2o && (plgpio->p2o_regs & PTO_IE_REG)) {
294 offset = plgpio->p2o(offset);
299 spin_lock_irqsave(&plgpio->lock, flags);
300 plgpio_reg_set(plgpio->regmap, offset, plgpio->regs.ie);
301 spin_unlock_irqrestore(&plgpio->lock, flags);
302 gpiochip_disable_irq(gc, irqd_to_hwirq(d));
305 static void plgpio_irq_enable(struct irq_data *d)
307 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
308 struct plgpio *plgpio = gpiochip_get_data(gc);
309 int offset = d->hwirq;
312 /* get correct offset for "offset" pin */
313 if (plgpio->p2o && (plgpio->p2o_regs & PTO_IE_REG)) {
314 offset = plgpio->p2o(offset);
319 gpiochip_enable_irq(gc, irqd_to_hwirq(d));
320 spin_lock_irqsave(&plgpio->lock, flags);
321 plgpio_reg_reset(plgpio->regmap, offset, plgpio->regs.ie);
322 spin_unlock_irqrestore(&plgpio->lock, flags);
325 static int plgpio_irq_set_type(struct irq_data *d, unsigned trigger)
327 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
328 struct plgpio *plgpio = gpiochip_get_data(gc);
329 int offset = d->hwirq;
331 unsigned int supported_type = 0, val;
333 if (offset >= plgpio->chip.ngpio)
336 if (plgpio->regs.eit == -1)
337 supported_type = IRQ_TYPE_LEVEL_HIGH;
339 supported_type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
341 if (!(trigger & supported_type))
344 if (plgpio->regs.eit == -1)
347 reg_off = REG_OFFSET(0, plgpio->regs.eit, offset);
348 regmap_read(plgpio->regmap, reg_off, &val);
350 offset = PIN_OFFSET(offset);
351 if (trigger & IRQ_TYPE_EDGE_RISING)
352 regmap_write(plgpio->regmap, reg_off, val | (1 << offset));
354 regmap_write(plgpio->regmap, reg_off, val & ~(1 << offset));
359 static const struct irq_chip plgpio_irqchip = {
361 .irq_enable = plgpio_irq_enable,
362 .irq_disable = plgpio_irq_disable,
363 .irq_set_type = plgpio_irq_set_type,
364 .flags = IRQCHIP_IMMUTABLE,
365 GPIOCHIP_IRQ_RESOURCE_HELPERS,
368 static void plgpio_irq_handler(struct irq_desc *desc)
370 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
371 struct plgpio *plgpio = gpiochip_get_data(gc);
372 struct irq_chip *irqchip = irq_desc_get_chip(desc);
373 int regs_count, count, pin, offset, i = 0;
375 unsigned long pendingl;
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 regmap_read(plgpio->regmap, plgpio->regs.mis +
384 i * sizeof(int *), &pending);
388 /* clear interrupts */
389 regmap_write(plgpio->regmap, plgpio->regs.mis +
390 i * sizeof(int *), ~pending);
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;
403 for_each_set_bit(offset, &pendingl, MAX_GPIO_PER_REG) {
404 /* get correct pin for "offset" */
405 if (plgpio->o2p && (plgpio->p2o_regs & PTO_MIS_REG)) {
406 pin = plgpio->o2p(offset);
412 /* get correct irq line number */
413 pin = i * MAX_GPIO_PER_REG + pin;
414 generic_handle_domain_irq(gc->irq.domain, pin);
417 chained_irq_exit(irqchip, desc);
421 * pin to offset and offset to pin converter functions
423 * In spear310 there is inconsistency among bit positions in plgpio regiseters,
424 * for different plgpio pins. For example: for pin 27, bit offset is 23, pin
425 * 28-33 are not supported, pin 95 has offset bit 95, bit 100 has offset bit 1
427 static int spear310_p2o(int pin)
445 static int spear310_o2p(int offset)
449 else if (offset <= 31)
455 static int plgpio_probe_dt(struct platform_device *pdev, struct plgpio *plgpio)
457 struct device_node *np = pdev->dev.of_node;
461 if (of_machine_is_compatible("st,spear310")) {
462 plgpio->p2o = spear310_p2o;
463 plgpio->o2p = spear310_o2p;
464 plgpio->p2o_regs = PTO_WDATA_REG | PTO_DIR_REG | PTO_IE_REG |
465 PTO_RDATA_REG | PTO_MIS_REG;
468 if (!of_property_read_u32(np, "st-plgpio,ngpio", &val)) {
469 plgpio->chip.ngpio = val;
471 dev_err(&pdev->dev, "DT: Invalid ngpio field\n");
475 if (!of_property_read_u32(np, "st-plgpio,enb-reg", &val))
476 plgpio->regs.enb = val;
478 plgpio->regs.enb = -1;
480 if (!of_property_read_u32(np, "st-plgpio,wdata-reg", &val)) {
481 plgpio->regs.wdata = val;
483 dev_err(&pdev->dev, "DT: Invalid wdata reg\n");
487 if (!of_property_read_u32(np, "st-plgpio,dir-reg", &val)) {
488 plgpio->regs.dir = val;
490 dev_err(&pdev->dev, "DT: Invalid dir reg\n");
494 if (!of_property_read_u32(np, "st-plgpio,ie-reg", &val)) {
495 plgpio->regs.ie = val;
497 dev_err(&pdev->dev, "DT: Invalid ie reg\n");
501 if (!of_property_read_u32(np, "st-plgpio,rdata-reg", &val)) {
502 plgpio->regs.rdata = val;
504 dev_err(&pdev->dev, "DT: Invalid rdata reg\n");
508 if (!of_property_read_u32(np, "st-plgpio,mis-reg", &val)) {
509 plgpio->regs.mis = val;
511 dev_err(&pdev->dev, "DT: Invalid mis reg\n");
515 if (!of_property_read_u32(np, "st-plgpio,eit-reg", &val))
516 plgpio->regs.eit = val;
518 plgpio->regs.eit = -1;
526 static int plgpio_probe(struct platform_device *pdev)
528 struct device_node *regmap_np;
529 struct plgpio *plgpio;
532 plgpio = devm_kzalloc(&pdev->dev, sizeof(*plgpio), GFP_KERNEL);
536 regmap_np = of_parse_phandle(pdev->dev.of_node, "regmap", 0);
538 plgpio->regmap = device_node_to_regmap(regmap_np);
539 of_node_put(regmap_np);
540 if (IS_ERR(plgpio->regmap)) {
541 dev_err(&pdev->dev, "Retrieve regmap failed (%pe)\n",
543 return PTR_ERR(plgpio->regmap);
546 plgpio->regmap = device_node_to_regmap(pdev->dev.of_node);
547 if (IS_ERR(plgpio->regmap)) {
548 dev_err(&pdev->dev, "Init regmap failed (%pe)\n",
550 return PTR_ERR(plgpio->regmap);
554 ret = plgpio_probe_dt(pdev, plgpio);
556 dev_err(&pdev->dev, "DT probe failed\n");
560 plgpio->clk = devm_clk_get(&pdev->dev, NULL);
561 if (IS_ERR(plgpio->clk))
562 dev_warn(&pdev->dev, "clk_get() failed, work without it\n");
564 #ifdef CONFIG_PM_SLEEP
565 plgpio->csave_regs = devm_kcalloc(&pdev->dev,
566 DIV_ROUND_UP(plgpio->chip.ngpio, MAX_GPIO_PER_REG),
567 sizeof(*plgpio->csave_regs),
569 if (!plgpio->csave_regs)
573 platform_set_drvdata(pdev, plgpio);
574 spin_lock_init(&plgpio->lock);
576 plgpio->chip.base = -1;
577 plgpio->chip.request = plgpio_request;
578 plgpio->chip.free = plgpio_free;
579 plgpio->chip.direction_input = plgpio_direction_input;
580 plgpio->chip.direction_output = plgpio_direction_output;
581 plgpio->chip.get = plgpio_get_value;
582 plgpio->chip.set = plgpio_set_value;
583 plgpio->chip.label = dev_name(&pdev->dev);
584 plgpio->chip.parent = &pdev->dev;
585 plgpio->chip.owner = THIS_MODULE;
587 if (!IS_ERR(plgpio->clk)) {
588 ret = clk_prepare(plgpio->clk);
590 dev_err(&pdev->dev, "clk prepare failed\n");
595 irq = platform_get_irq(pdev, 0);
597 struct gpio_irq_chip *girq;
599 girq = &plgpio->chip.irq;
600 gpio_irq_chip_set_chip(girq, &plgpio_irqchip);
601 girq->parent_handler = plgpio_irq_handler;
602 girq->num_parents = 1;
603 girq->parents = devm_kcalloc(&pdev->dev, 1,
604 sizeof(*girq->parents),
608 girq->parents[0] = irq;
609 girq->default_type = IRQ_TYPE_NONE;
610 girq->handler = handle_simple_irq;
611 dev_info(&pdev->dev, "PLGPIO registering with IRQs\n");
613 dev_info(&pdev->dev, "PLGPIO registering without IRQs\n");
616 ret = gpiochip_add_data(&plgpio->chip, plgpio);
618 dev_err(&pdev->dev, "unable to add gpio chip\n");
625 if (!IS_ERR(plgpio->clk))
626 clk_unprepare(plgpio->clk);
631 #ifdef CONFIG_PM_SLEEP
632 static int plgpio_suspend(struct device *dev)
634 struct plgpio *plgpio = dev_get_drvdata(dev);
635 int i, reg_count = DIV_ROUND_UP(plgpio->chip.ngpio, MAX_GPIO_PER_REG);
638 for (i = 0; i < reg_count; i++) {
639 off = i * sizeof(int *);
641 if (plgpio->regs.enb != -1)
642 regmap_read(plgpio->regmap, plgpio->regs.enb + off,
643 &plgpio->csave_regs[i].enb);
644 if (plgpio->regs.eit != -1)
645 regmap_read(plgpio->regmap, plgpio->regs.eit + off,
646 &plgpio->csave_regs[i].eit);
647 regmap_read(plgpio->regmap, plgpio->regs.wdata + off,
648 &plgpio->csave_regs[i].wdata);
649 regmap_read(plgpio->regmap, plgpio->regs.dir + off,
650 &plgpio->csave_regs[i].dir);
651 regmap_read(plgpio->regmap, plgpio->regs.ie + off,
652 &plgpio->csave_regs[i].ie);
659 * This is used to correct the values in end registers. End registers contain
660 * extra bits that might be used for other purpose in platform. So, we shouldn't
661 * overwrite these bits. This macro, reads given register again, preserves other
662 * bit values (non-plgpio bits), and retain captured value (plgpio bits).
664 #define plgpio_prepare_reg(__reg, _off, _mask, _tmp) \
666 regmap_read(plgpio->regmap, plgpio->regs.__reg + _off, &_tmp); \
668 plgpio->csave_regs[i].__reg = \
669 _tmp | (plgpio->csave_regs[i].__reg & _mask); \
672 static int plgpio_resume(struct device *dev)
674 struct plgpio *plgpio = dev_get_drvdata(dev);
675 int i, reg_count = DIV_ROUND_UP(plgpio->chip.ngpio, MAX_GPIO_PER_REG);
679 for (i = 0; i < reg_count; i++) {
680 off = i * sizeof(int *);
682 if (i == reg_count - 1) {
683 mask = (1 << (plgpio->chip.ngpio - i *
684 MAX_GPIO_PER_REG)) - 1;
686 if (plgpio->regs.enb != -1)
687 plgpio_prepare_reg(enb, off, mask, tmp);
689 if (plgpio->regs.eit != -1)
690 plgpio_prepare_reg(eit, off, mask, tmp);
692 plgpio_prepare_reg(wdata, off, mask, tmp);
693 plgpio_prepare_reg(dir, off, mask, tmp);
694 plgpio_prepare_reg(ie, off, mask, tmp);
697 regmap_write(plgpio->regmap, plgpio->regs.wdata + off,
698 plgpio->csave_regs[i].wdata);
700 regmap_write(plgpio->regmap, plgpio->regs.dir + off,
701 plgpio->csave_regs[i].dir);
703 if (plgpio->regs.eit != -1)
704 regmap_write(plgpio->regmap, plgpio->regs.eit + off,
705 plgpio->csave_regs[i].eit);
707 regmap_write(plgpio->regmap, plgpio->regs.ie + off,
708 plgpio->csave_regs[i].ie);
710 if (plgpio->regs.enb != -1)
711 regmap_write(plgpio->regmap, plgpio->regs.enb + off,
712 plgpio->csave_regs[i].enb);
719 static SIMPLE_DEV_PM_OPS(plgpio_dev_pm_ops, plgpio_suspend, plgpio_resume);
721 static const struct of_device_id plgpio_of_match[] = {
722 { .compatible = "st,spear-plgpio" },
726 static struct platform_driver plgpio_driver = {
727 .probe = plgpio_probe,
729 .name = "spear-plgpio",
730 .pm = &plgpio_dev_pm_ops,
731 .of_match_table = plgpio_of_match,
735 static int __init plgpio_init(void)
737 return platform_driver_register(&plgpio_driver);
739 subsys_initcall(plgpio_init);