2 * Copyright (c) 2013, Sony Mobile Communications AB.
3 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/err.h>
16 #include <linux/irqdomain.h>
18 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/pinctrl/machine.h>
22 #include <linux/pinctrl/pinctrl.h>
23 #include <linux/pinctrl/pinmux.h>
24 #include <linux/pinctrl/pinconf.h>
25 #include <linux/pinctrl/pinconf-generic.h>
26 #include <linux/slab.h>
27 #include <linux/gpio.h>
28 #include <linux/interrupt.h>
29 #include <linux/irq.h>
30 #include <linux/irqchip/chained_irq.h>
31 #include <linux/of_irq.h>
32 #include <linux/spinlock.h>
36 #include "pinctrl-msm.h"
37 #include "pinctrl-utils.h"
40 * struct msm_pinctrl - state for a pinctrl-msm device
41 * @dev: device handle.
42 * @pctrl: pinctrl handle.
43 * @domain: irqdomain handle.
44 * @chip: gpiochip handle.
45 * @irq: parent irq for the TLMM irq_chip.
46 * @lock: Spinlock to protect register resources as well
47 * as msm_pinctrl data structures.
48 * @enabled_irqs: Bitmap of currently enabled irqs.
49 * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
51 * @wake_irqs: Bitmap of irqs with requested as wakeup source.
52 * @soc; Reference to soc_data of platform specific data.
53 * @regs: Base address for the TLMM register map.
57 struct pinctrl_dev *pctrl;
58 struct irq_domain *domain;
59 struct gpio_chip chip;
64 unsigned long *enabled_irqs;
65 unsigned long *dual_edge_irqs;
66 unsigned long *wake_irqs;
68 const struct msm_pinctrl_soc_data *soc;
72 static int msm_get_groups_count(struct pinctrl_dev *pctldev)
74 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
76 return pctrl->soc->ngroups;
79 static const char *msm_get_group_name(struct pinctrl_dev *pctldev,
82 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
84 return pctrl->soc->groups[group].name;
87 static int msm_get_group_pins(struct pinctrl_dev *pctldev,
89 const unsigned **pins,
92 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
94 *pins = pctrl->soc->groups[group].pins;
95 *num_pins = pctrl->soc->groups[group].npins;
99 static struct pinctrl_ops msm_pinctrl_ops = {
100 .get_groups_count = msm_get_groups_count,
101 .get_group_name = msm_get_group_name,
102 .get_group_pins = msm_get_group_pins,
103 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
104 .dt_free_map = pinctrl_utils_dt_free_map,
107 static int msm_get_functions_count(struct pinctrl_dev *pctldev)
109 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
111 return pctrl->soc->nfunctions;
114 static const char *msm_get_function_name(struct pinctrl_dev *pctldev,
117 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
119 return pctrl->soc->functions[function].name;
122 static int msm_get_function_groups(struct pinctrl_dev *pctldev,
124 const char * const **groups,
125 unsigned * const num_groups)
127 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
129 *groups = pctrl->soc->functions[function].groups;
130 *num_groups = pctrl->soc->functions[function].ngroups;
134 static int msm_pinmux_enable(struct pinctrl_dev *pctldev,
138 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
139 const struct msm_pingroup *g;
144 g = &pctrl->soc->groups[group];
146 if (WARN_ON(g->mux_bit < 0))
149 for (i = 0; i < ARRAY_SIZE(g->funcs); i++) {
150 if (g->funcs[i] == function)
154 if (WARN_ON(i == ARRAY_SIZE(g->funcs)))
157 spin_lock_irqsave(&pctrl->lock, flags);
159 val = readl(pctrl->regs + g->ctl_reg);
160 val &= ~(0x7 << g->mux_bit);
161 val |= i << g->mux_bit;
162 writel(val, pctrl->regs + g->ctl_reg);
164 spin_unlock_irqrestore(&pctrl->lock, flags);
169 static void msm_pinmux_disable(struct pinctrl_dev *pctldev,
173 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
174 const struct msm_pingroup *g;
178 g = &pctrl->soc->groups[group];
180 if (WARN_ON(g->mux_bit < 0))
183 spin_lock_irqsave(&pctrl->lock, flags);
185 /* Clear the mux bits to select gpio mode */
186 val = readl(pctrl->regs + g->ctl_reg);
187 val &= ~(0x7 << g->mux_bit);
188 writel(val, pctrl->regs + g->ctl_reg);
190 spin_unlock_irqrestore(&pctrl->lock, flags);
193 static struct pinmux_ops msm_pinmux_ops = {
194 .get_functions_count = msm_get_functions_count,
195 .get_function_name = msm_get_function_name,
196 .get_function_groups = msm_get_function_groups,
197 .enable = msm_pinmux_enable,
198 .disable = msm_pinmux_disable,
201 static int msm_config_reg(struct msm_pinctrl *pctrl,
202 const struct msm_pingroup *g,
209 case PIN_CONFIG_BIAS_DISABLE:
214 case PIN_CONFIG_BIAS_PULL_DOWN:
219 case PIN_CONFIG_BIAS_PULL_UP:
224 case PIN_CONFIG_DRIVE_STRENGTH:
230 dev_err(pctrl->dev, "Invalid config param %04x\n", param);
235 dev_err(pctrl->dev, "Config param %04x not supported on group %s\n",
243 static int msm_config_get(struct pinctrl_dev *pctldev,
245 unsigned long *config)
247 dev_err(pctldev->dev, "pin_config_set op not supported\n");
251 static int msm_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
252 unsigned long *configs, unsigned num_configs)
254 dev_err(pctldev->dev, "pin_config_set op not supported\n");
258 #define MSM_NO_PULL 0
259 #define MSM_PULL_DOWN 1
260 #define MSM_PULL_UP 3
262 static const unsigned msm_regval_to_drive[] = { 2, 4, 6, 8, 10, 12, 14, 16 };
263 static const unsigned msm_drive_to_regval[] = { -1, -1, 0, -1, 1, -1, 2, -1, 3, -1, 4, -1, 5, -1, 6, -1, 7 };
265 static int msm_config_group_get(struct pinctrl_dev *pctldev,
267 unsigned long *config)
269 const struct msm_pingroup *g;
270 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
271 unsigned param = pinconf_to_config_param(*config);
279 g = &pctrl->soc->groups[group];
281 ret = msm_config_reg(pctrl, g, param, ®, &mask, &bit);
285 val = readl(pctrl->regs + reg);
286 arg = (val >> bit) & mask;
288 /* Convert register value to pinconf value */
290 case PIN_CONFIG_BIAS_DISABLE:
291 arg = arg == MSM_NO_PULL;
293 case PIN_CONFIG_BIAS_PULL_DOWN:
294 arg = arg == MSM_PULL_DOWN;
296 case PIN_CONFIG_BIAS_PULL_UP:
297 arg = arg == MSM_PULL_UP;
299 case PIN_CONFIG_DRIVE_STRENGTH:
300 arg = msm_regval_to_drive[arg];
303 dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
308 *config = pinconf_to_config_packed(param, arg);
313 static int msm_config_group_set(struct pinctrl_dev *pctldev,
315 unsigned long *configs,
316 unsigned num_configs)
318 const struct msm_pingroup *g;
319 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
330 g = &pctrl->soc->groups[group];
332 for (i = 0; i < num_configs; i++) {
333 param = pinconf_to_config_param(configs[i]);
334 arg = pinconf_to_config_argument(configs[i]);
336 ret = msm_config_reg(pctrl, g, param, ®, &mask, &bit);
340 /* Convert pinconf values to register values */
342 case PIN_CONFIG_BIAS_DISABLE:
345 case PIN_CONFIG_BIAS_PULL_DOWN:
348 case PIN_CONFIG_BIAS_PULL_UP:
351 case PIN_CONFIG_DRIVE_STRENGTH:
352 /* Check for invalid values */
353 if (arg > ARRAY_SIZE(msm_drive_to_regval))
356 arg = msm_drive_to_regval[arg];
359 dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
364 /* Range-check user-supplied value */
366 dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
370 spin_lock_irqsave(&pctrl->lock, flags);
371 val = readl(pctrl->regs + reg);
372 val &= ~(mask << bit);
374 writel(val, pctrl->regs + reg);
375 spin_unlock_irqrestore(&pctrl->lock, flags);
381 static struct pinconf_ops msm_pinconf_ops = {
382 .pin_config_get = msm_config_get,
383 .pin_config_set = msm_config_set,
384 .pin_config_group_get = msm_config_group_get,
385 .pin_config_group_set = msm_config_group_set,
388 static struct pinctrl_desc msm_pinctrl_desc = {
389 .pctlops = &msm_pinctrl_ops,
390 .pmxops = &msm_pinmux_ops,
391 .confops = &msm_pinconf_ops,
392 .owner = THIS_MODULE,
395 static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
397 const struct msm_pingroup *g;
398 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
402 if (WARN_ON(offset >= pctrl->soc->ngroups))
405 g = &pctrl->soc->groups[offset];
407 if (WARN_ON(g->oe_bit < 0))
410 spin_lock_irqsave(&pctrl->lock, flags);
412 val = readl(pctrl->regs + g->ctl_reg);
413 val &= ~BIT(g->oe_bit);
414 writel(val, pctrl->regs + g->ctl_reg);
416 spin_unlock_irqrestore(&pctrl->lock, flags);
421 static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
423 const struct msm_pingroup *g;
424 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
428 if (WARN_ON(offset >= pctrl->soc->ngroups))
431 g = &pctrl->soc->groups[offset];
433 if (WARN_ON(g->oe_bit < 0))
436 spin_lock_irqsave(&pctrl->lock, flags);
438 writel(value ? BIT(g->out_bit) : 0, pctrl->regs + g->io_reg);
440 val = readl(pctrl->regs + g->ctl_reg);
441 val |= BIT(g->oe_bit);
442 writel(val, pctrl->regs + g->ctl_reg);
444 spin_unlock_irqrestore(&pctrl->lock, flags);
449 static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
451 const struct msm_pingroup *g;
452 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
455 if (WARN_ON(offset >= pctrl->soc->ngroups))
458 g = &pctrl->soc->groups[offset];
460 val = readl(pctrl->regs + g->io_reg);
461 return !!(val & BIT(g->in_bit));
464 static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
466 const struct msm_pingroup *g;
467 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
471 if (WARN_ON(offset >= pctrl->soc->ngroups))
474 g = &pctrl->soc->groups[offset];
476 spin_lock_irqsave(&pctrl->lock, flags);
478 val = readl(pctrl->regs + g->io_reg);
479 val |= BIT(g->out_bit);
480 writel(val, pctrl->regs + g->io_reg);
482 spin_unlock_irqrestore(&pctrl->lock, flags);
485 static int msm_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
487 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
489 return irq_find_mapping(pctrl->domain, offset);
492 static int msm_gpio_request(struct gpio_chip *chip, unsigned offset)
494 int gpio = chip->base + offset;
495 return pinctrl_request_gpio(gpio);
498 static void msm_gpio_free(struct gpio_chip *chip, unsigned offset)
500 int gpio = chip->base + offset;
501 return pinctrl_free_gpio(gpio);
504 #ifdef CONFIG_DEBUG_FS
505 #include <linux/seq_file.h>
507 static void msm_gpio_dbg_show_one(struct seq_file *s,
508 struct pinctrl_dev *pctldev,
509 struct gpio_chip *chip,
513 const struct msm_pingroup *g;
514 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
521 const char *pulls[] = {
528 g = &pctrl->soc->groups[offset];
529 ctl_reg = readl(pctrl->regs + g->ctl_reg);
531 is_out = !!(ctl_reg & BIT(g->oe_bit));
532 func = (ctl_reg >> g->mux_bit) & 7;
533 drive = (ctl_reg >> g->drv_bit) & 7;
534 pull = (ctl_reg >> g->pull_bit) & 3;
536 seq_printf(s, " %-8s: %-3s %d", g->name, is_out ? "out" : "in", func);
537 seq_printf(s, " %dmA", msm_regval_to_drive[drive]);
538 seq_printf(s, " %s", pulls[pull]);
541 static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
543 unsigned gpio = chip->base;
546 for (i = 0; i < chip->ngpio; i++, gpio++) {
547 msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
553 #define msm_gpio_dbg_show NULL
556 static struct gpio_chip msm_gpio_template = {
557 .direction_input = msm_gpio_direction_input,
558 .direction_output = msm_gpio_direction_output,
561 .to_irq = msm_gpio_to_irq,
562 .request = msm_gpio_request,
563 .free = msm_gpio_free,
564 .dbg_show = msm_gpio_dbg_show,
567 /* For dual-edge interrupts in software, since some hardware has no
570 * At appropriate moments, this function may be called to flip the polarity
571 * settings of both-edge irq lines to try and catch the next edge.
573 * The attempt is considered successful if:
574 * - the status bit goes high, indicating that an edge was caught, or
575 * - the input value of the gpio doesn't change during the attempt.
576 * If the value changes twice during the process, that would cause the first
577 * test to fail but would force the second, as two opposite
578 * transitions would cause a detection no matter the polarity setting.
580 * The do-loop tries to sledge-hammer closed the timing hole between
581 * the initial value-read and the polarity-write - if the line value changes
582 * during that window, an interrupt is lost, the new polarity setting is
583 * incorrect, and the first success test will fail, causing a retry.
585 * Algorithm comes from Google's msmgpio driver.
587 static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
588 const struct msm_pingroup *g,
591 int loop_limit = 100;
592 unsigned val, val2, intstat;
596 val = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit);
598 pol = readl(pctrl->regs + g->intr_cfg_reg);
599 pol ^= BIT(g->intr_polarity_bit);
600 writel(pol, pctrl->regs + g->intr_cfg_reg);
602 val2 = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit);
603 intstat = readl(pctrl->regs + g->intr_status_reg);
604 if (intstat || (val == val2))
606 } while (loop_limit-- > 0);
607 dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
611 static void msm_gpio_irq_mask(struct irq_data *d)
613 const struct msm_pingroup *g;
614 struct msm_pinctrl *pctrl;
618 pctrl = irq_data_get_irq_chip_data(d);
622 if (WARN_ON(d->hwirq >= pctrl->soc->ngroups))
625 g = &pctrl->soc->groups[d->hwirq];
627 spin_lock_irqsave(&pctrl->lock, flags);
629 val = readl(pctrl->regs + g->intr_cfg_reg);
630 val &= ~BIT(g->intr_enable_bit);
631 writel(val, pctrl->regs + g->intr_cfg_reg);
633 clear_bit(d->hwirq, pctrl->enabled_irqs);
635 spin_unlock_irqrestore(&pctrl->lock, flags);
638 static void msm_gpio_irq_unmask(struct irq_data *d)
640 const struct msm_pingroup *g;
641 struct msm_pinctrl *pctrl;
645 pctrl = irq_data_get_irq_chip_data(d);
649 if (WARN_ON(d->hwirq >= pctrl->soc->ngroups))
652 g = &pctrl->soc->groups[d->hwirq];
654 spin_lock_irqsave(&pctrl->lock, flags);
656 val = readl(pctrl->regs + g->intr_status_reg);
657 val &= ~BIT(g->intr_status_bit);
658 writel(val, pctrl->regs + g->intr_status_reg);
660 val = readl(pctrl->regs + g->intr_cfg_reg);
661 val |= BIT(g->intr_enable_bit);
662 writel(val, pctrl->regs + g->intr_cfg_reg);
664 set_bit(d->hwirq, pctrl->enabled_irqs);
666 spin_unlock_irqrestore(&pctrl->lock, flags);
669 static void msm_gpio_irq_ack(struct irq_data *d)
671 const struct msm_pingroup *g;
672 struct msm_pinctrl *pctrl;
676 pctrl = irq_data_get_irq_chip_data(d);
680 if (WARN_ON(d->hwirq >= pctrl->soc->ngroups))
683 g = &pctrl->soc->groups[d->hwirq];
685 spin_lock_irqsave(&pctrl->lock, flags);
687 val = readl(pctrl->regs + g->intr_status_reg);
688 val &= ~BIT(g->intr_status_bit);
689 writel(val, pctrl->regs + g->intr_status_reg);
691 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
692 msm_gpio_update_dual_edge_pos(pctrl, g, d);
694 spin_unlock_irqrestore(&pctrl->lock, flags);
697 #define INTR_TARGET_PROC_APPS 4
699 static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
701 const struct msm_pingroup *g;
702 struct msm_pinctrl *pctrl;
706 pctrl = irq_data_get_irq_chip_data(d);
710 if (WARN_ON(d->hwirq >= pctrl->soc->ngroups))
713 g = &pctrl->soc->groups[d->hwirq];
715 spin_lock_irqsave(&pctrl->lock, flags);
718 * For hw without possibility of detecting both edges
720 if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
721 set_bit(d->hwirq, pctrl->dual_edge_irqs);
723 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
725 /* Route interrupts to application cpu */
726 val = readl(pctrl->regs + g->intr_target_reg);
727 val &= ~(7 << g->intr_target_bit);
728 val |= INTR_TARGET_PROC_APPS << g->intr_target_bit;
729 writel(val, pctrl->regs + g->intr_target_reg);
731 /* Update configuration for gpio.
732 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
733 * internal circuitry of TLMM, toggling the RAW_STATUS
734 * could cause the INTR_STATUS to be set for EDGE interrupts.
736 val = readl(pctrl->regs + g->intr_cfg_reg);
737 val |= BIT(g->intr_raw_status_bit);
738 if (g->intr_detection_width == 2) {
739 val &= ~(3 << g->intr_detection_bit);
740 val &= ~(1 << g->intr_polarity_bit);
742 case IRQ_TYPE_EDGE_RISING:
743 val |= 1 << g->intr_detection_bit;
744 val |= BIT(g->intr_polarity_bit);
746 case IRQ_TYPE_EDGE_FALLING:
747 val |= 2 << g->intr_detection_bit;
748 val |= BIT(g->intr_polarity_bit);
750 case IRQ_TYPE_EDGE_BOTH:
751 val |= 3 << g->intr_detection_bit;
752 val |= BIT(g->intr_polarity_bit);
754 case IRQ_TYPE_LEVEL_LOW:
756 case IRQ_TYPE_LEVEL_HIGH:
757 val |= BIT(g->intr_polarity_bit);
760 } else if (g->intr_detection_width == 1) {
761 val &= ~(1 << g->intr_detection_bit);
762 val &= ~(1 << g->intr_polarity_bit);
764 case IRQ_TYPE_EDGE_RISING:
765 val |= BIT(g->intr_detection_bit);
766 val |= BIT(g->intr_polarity_bit);
768 case IRQ_TYPE_EDGE_FALLING:
769 val |= BIT(g->intr_detection_bit);
771 case IRQ_TYPE_EDGE_BOTH:
772 val |= BIT(g->intr_detection_bit);
774 case IRQ_TYPE_LEVEL_LOW:
776 case IRQ_TYPE_LEVEL_HIGH:
777 val |= BIT(g->intr_polarity_bit);
783 writel(val, pctrl->regs + g->intr_cfg_reg);
785 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
786 msm_gpio_update_dual_edge_pos(pctrl, g, d);
788 spin_unlock_irqrestore(&pctrl->lock, flags);
790 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
791 __irq_set_handler_locked(d->irq, handle_level_irq);
792 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
793 __irq_set_handler_locked(d->irq, handle_edge_irq);
798 static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
800 struct msm_pinctrl *pctrl;
804 pctrl = irq_data_get_irq_chip_data(d);
808 ngpio = pctrl->chip.ngpio;
810 spin_lock_irqsave(&pctrl->lock, flags);
813 if (bitmap_empty(pctrl->wake_irqs, ngpio))
814 enable_irq_wake(pctrl->irq);
815 set_bit(d->hwirq, pctrl->wake_irqs);
817 clear_bit(d->hwirq, pctrl->wake_irqs);
818 if (bitmap_empty(pctrl->wake_irqs, ngpio))
819 disable_irq_wake(pctrl->irq);
822 spin_unlock_irqrestore(&pctrl->lock, flags);
827 static unsigned int msm_gpio_irq_startup(struct irq_data *d)
829 struct msm_pinctrl *pctrl = irq_data_get_irq_chip_data(d);
831 if (gpio_lock_as_irq(&pctrl->chip, d->hwirq)) {
832 dev_err(pctrl->dev, "unable to lock HW IRQ %lu for IRQ\n",
835 msm_gpio_irq_unmask(d);
839 static void msm_gpio_irq_shutdown(struct irq_data *d)
841 struct msm_pinctrl *pctrl = irq_data_get_irq_chip_data(d);
843 msm_gpio_irq_mask(d);
844 gpio_unlock_as_irq(&pctrl->chip, d->hwirq);
847 static struct irq_chip msm_gpio_irq_chip = {
849 .irq_mask = msm_gpio_irq_mask,
850 .irq_unmask = msm_gpio_irq_unmask,
851 .irq_ack = msm_gpio_irq_ack,
852 .irq_set_type = msm_gpio_irq_set_type,
853 .irq_set_wake = msm_gpio_irq_set_wake,
854 .irq_startup = msm_gpio_irq_startup,
855 .irq_shutdown = msm_gpio_irq_shutdown,
858 static void msm_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
860 const struct msm_pingroup *g;
861 struct msm_pinctrl *pctrl = irq_desc_get_handler_data(desc);
862 struct irq_chip *chip = irq_get_chip(irq);
868 chained_irq_enter(chip, desc);
871 * Each pin have it's own IRQ status register, so use
872 * enabled_irq bitmap to limit the number of reads.
874 for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
875 g = &pctrl->soc->groups[i];
876 val = readl(pctrl->regs + g->intr_status_reg);
877 if (val & BIT(g->intr_status_bit)) {
878 irq_pin = irq_find_mapping(pctrl->domain, i);
879 generic_handle_irq(irq_pin);
884 /* No interrutps where flagged */
886 handle_bad_irq(irq, desc);
888 chained_irq_exit(chip, desc);
891 static int msm_gpio_init(struct msm_pinctrl *pctrl)
893 struct gpio_chip *chip;
901 chip->ngpio = pctrl->soc->ngpios;
902 chip->label = dev_name(pctrl->dev);
903 chip->dev = pctrl->dev;
904 chip->owner = THIS_MODULE;
905 chip->of_node = pctrl->dev->of_node;
907 pctrl->enabled_irqs = devm_kzalloc(pctrl->dev,
908 sizeof(unsigned long) * BITS_TO_LONGS(chip->ngpio),
910 if (!pctrl->enabled_irqs) {
911 dev_err(pctrl->dev, "Failed to allocate enabled_irqs bitmap\n");
915 pctrl->dual_edge_irqs = devm_kzalloc(pctrl->dev,
916 sizeof(unsigned long) * BITS_TO_LONGS(chip->ngpio),
918 if (!pctrl->dual_edge_irqs) {
919 dev_err(pctrl->dev, "Failed to allocate dual_edge_irqs bitmap\n");
923 pctrl->wake_irqs = devm_kzalloc(pctrl->dev,
924 sizeof(unsigned long) * BITS_TO_LONGS(chip->ngpio),
926 if (!pctrl->wake_irqs) {
927 dev_err(pctrl->dev, "Failed to allocate wake_irqs bitmap\n");
931 ret = gpiochip_add(&pctrl->chip);
933 dev_err(pctrl->dev, "Failed register gpiochip\n");
937 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev), 0, 0, chip->ngpio);
939 dev_err(pctrl->dev, "Failed to add pin range\n");
943 pctrl->domain = irq_domain_add_linear(pctrl->dev->of_node, chip->ngpio,
944 &irq_domain_simple_ops, NULL);
945 if (!pctrl->domain) {
946 dev_err(pctrl->dev, "Failed to register irq domain\n");
947 r = gpiochip_remove(&pctrl->chip);
951 for (i = 0; i < chip->ngpio; i++) {
952 irq = irq_create_mapping(pctrl->domain, i);
953 irq_set_chip_and_handler(irq, &msm_gpio_irq_chip, handle_edge_irq);
954 irq_set_chip_data(irq, pctrl);
957 irq_set_handler_data(pctrl->irq, pctrl);
958 irq_set_chained_handler(pctrl->irq, msm_gpio_irq_handler);
963 int msm_pinctrl_probe(struct platform_device *pdev,
964 const struct msm_pinctrl_soc_data *soc_data)
966 struct msm_pinctrl *pctrl;
967 struct resource *res;
970 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
972 dev_err(&pdev->dev, "Can't allocate msm_pinctrl\n");
975 pctrl->dev = &pdev->dev;
976 pctrl->soc = soc_data;
977 pctrl->chip = msm_gpio_template;
979 spin_lock_init(&pctrl->lock);
981 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
982 pctrl->regs = devm_ioremap_resource(&pdev->dev, res);
983 if (IS_ERR(pctrl->regs))
984 return PTR_ERR(pctrl->regs);
986 pctrl->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
987 if (pctrl->irq < 0) {
988 dev_err(&pdev->dev, "No interrupt defined for msmgpio\n");
992 msm_pinctrl_desc.name = dev_name(&pdev->dev);
993 msm_pinctrl_desc.pins = pctrl->soc->pins;
994 msm_pinctrl_desc.npins = pctrl->soc->npins;
995 pctrl->pctrl = pinctrl_register(&msm_pinctrl_desc, &pdev->dev, pctrl);
997 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
1001 ret = msm_gpio_init(pctrl);
1003 pinctrl_unregister(pctrl->pctrl);
1007 platform_set_drvdata(pdev, pctrl);
1009 dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
1013 EXPORT_SYMBOL(msm_pinctrl_probe);
1015 int msm_pinctrl_remove(struct platform_device *pdev)
1017 struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
1020 irq_set_chained_handler(pctrl->irq, NULL);
1021 irq_domain_remove(pctrl->domain);
1022 ret = gpiochip_remove(&pctrl->chip);
1023 pinctrl_unregister(pctrl->pctrl);
1027 EXPORT_SYMBOL(msm_pinctrl_remove);