1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2013, Sony Mobile Communications AB.
4 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
7 #include <linux/delay.h>
10 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/pinctrl/machine.h>
14 #include <linux/pinctrl/pinctrl.h>
15 #include <linux/pinctrl/pinmux.h>
16 #include <linux/pinctrl/pinconf.h>
17 #include <linux/pinctrl/pinconf-generic.h>
18 #include <linux/slab.h>
19 #include <linux/gpio/driver.h>
20 #include <linux/interrupt.h>
21 #include <linux/spinlock.h>
22 #include <linux/reboot.h>
24 #include <linux/log2.h>
25 #include <linux/qcom_scm.h>
27 #include <linux/soc/qcom/irq.h>
30 #include "../pinconf.h"
31 #include "pinctrl-msm.h"
32 #include "../pinctrl-utils.h"
34 #define MAX_NR_GPIO 300
35 #define MAX_NR_TILES 4
36 #define PS_HOLD_OFFSET 0x820
39 * struct msm_pinctrl - state for a pinctrl-msm device
40 * @dev: device handle.
41 * @pctrl: pinctrl handle.
42 * @chip: gpiochip handle.
43 * @desc: pin controller descriptor
44 * @restart_nb: restart notifier block.
45 * @irq_chip: irq chip information
46 * @irq: parent irq for the TLMM irq_chip.
47 * @intr_target_use_scm: route irq to application cpu using scm calls
48 * @lock: Spinlock to protect register resources as well
49 * as msm_pinctrl data structures.
50 * @enabled_irqs: Bitmap of currently enabled irqs.
51 * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
53 * @skip_wake_irqs: Skip IRQs that are handled by wakeup interrupt controller
54 * @soc: Reference to soc_data of platform specific data.
55 * @regs: Base addresses for the TLMM tiles.
56 * @phys_base: Physical base address
60 struct pinctrl_dev *pctrl;
61 struct gpio_chip chip;
62 struct pinctrl_desc desc;
63 struct notifier_block restart_nb;
65 struct irq_chip irq_chip;
68 bool intr_target_use_scm;
72 DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
73 DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
74 DECLARE_BITMAP(skip_wake_irqs, MAX_NR_GPIO);
76 const struct msm_pinctrl_soc_data *soc;
77 void __iomem *regs[MAX_NR_TILES];
78 u32 phys_base[MAX_NR_TILES];
81 #define MSM_ACCESSOR(name) \
82 static u32 msm_readl_##name(struct msm_pinctrl *pctrl, \
83 const struct msm_pingroup *g) \
85 return readl(pctrl->regs[g->tile] + g->name##_reg); \
87 static void msm_writel_##name(u32 val, struct msm_pinctrl *pctrl, \
88 const struct msm_pingroup *g) \
90 writel(val, pctrl->regs[g->tile] + g->name##_reg); \
95 MSM_ACCESSOR(intr_cfg)
96 MSM_ACCESSOR(intr_status)
97 MSM_ACCESSOR(intr_target)
99 static int msm_get_groups_count(struct pinctrl_dev *pctldev)
101 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
103 return pctrl->soc->ngroups;
106 static const char *msm_get_group_name(struct pinctrl_dev *pctldev,
109 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
111 return pctrl->soc->groups[group].name;
114 static int msm_get_group_pins(struct pinctrl_dev *pctldev,
116 const unsigned **pins,
119 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
121 *pins = pctrl->soc->groups[group].pins;
122 *num_pins = pctrl->soc->groups[group].npins;
126 static const struct pinctrl_ops msm_pinctrl_ops = {
127 .get_groups_count = msm_get_groups_count,
128 .get_group_name = msm_get_group_name,
129 .get_group_pins = msm_get_group_pins,
130 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
131 .dt_free_map = pinctrl_utils_free_map,
134 static int msm_pinmux_request(struct pinctrl_dev *pctldev, unsigned offset)
136 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
137 struct gpio_chip *chip = &pctrl->chip;
139 return gpiochip_line_is_valid(chip, offset) ? 0 : -EINVAL;
142 static int msm_get_functions_count(struct pinctrl_dev *pctldev)
144 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
146 return pctrl->soc->nfunctions;
149 static const char *msm_get_function_name(struct pinctrl_dev *pctldev,
152 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
154 return pctrl->soc->functions[function].name;
157 static int msm_get_function_groups(struct pinctrl_dev *pctldev,
159 const char * const **groups,
160 unsigned * const num_groups)
162 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
164 *groups = pctrl->soc->functions[function].groups;
165 *num_groups = pctrl->soc->functions[function].ngroups;
169 static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
173 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
174 const struct msm_pingroup *g;
179 g = &pctrl->soc->groups[group];
180 mask = GENMASK(g->mux_bit + order_base_2(g->nfuncs) - 1, g->mux_bit);
182 for (i = 0; i < g->nfuncs; i++) {
183 if (g->funcs[i] == function)
187 if (WARN_ON(i == g->nfuncs))
190 raw_spin_lock_irqsave(&pctrl->lock, flags);
192 val = msm_readl_ctl(pctrl, g);
194 val |= i << g->mux_bit;
195 msm_writel_ctl(val, pctrl, g);
197 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
202 static int msm_pinmux_request_gpio(struct pinctrl_dev *pctldev,
203 struct pinctrl_gpio_range *range,
206 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
207 const struct msm_pingroup *g = &pctrl->soc->groups[offset];
209 /* No funcs? Probably ACPI so can't do anything here */
213 /* For now assume function 0 is GPIO because it always is */
214 return msm_pinmux_set_mux(pctldev, g->funcs[0], offset);
217 static const struct pinmux_ops msm_pinmux_ops = {
218 .request = msm_pinmux_request,
219 .get_functions_count = msm_get_functions_count,
220 .get_function_name = msm_get_function_name,
221 .get_function_groups = msm_get_function_groups,
222 .gpio_request_enable = msm_pinmux_request_gpio,
223 .set_mux = msm_pinmux_set_mux,
226 static int msm_config_reg(struct msm_pinctrl *pctrl,
227 const struct msm_pingroup *g,
233 case PIN_CONFIG_BIAS_DISABLE:
234 case PIN_CONFIG_BIAS_PULL_DOWN:
235 case PIN_CONFIG_BIAS_BUS_HOLD:
236 case PIN_CONFIG_BIAS_PULL_UP:
240 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
244 case PIN_CONFIG_DRIVE_STRENGTH:
248 case PIN_CONFIG_OUTPUT:
249 case PIN_CONFIG_INPUT_ENABLE:
260 #define MSM_NO_PULL 0
261 #define MSM_PULL_DOWN 1
263 #define MSM_PULL_UP_NO_KEEPER 2
264 #define MSM_PULL_UP 3
266 static unsigned msm_regval_to_drive(u32 val)
268 return (val + 1) * 2;
271 static int msm_config_group_get(struct pinctrl_dev *pctldev,
273 unsigned long *config)
275 const struct msm_pingroup *g;
276 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
277 unsigned param = pinconf_to_config_param(*config);
284 g = &pctrl->soc->groups[group];
286 ret = msm_config_reg(pctrl, g, param, &mask, &bit);
290 val = msm_readl_ctl(pctrl, g);
291 arg = (val >> bit) & mask;
293 /* Convert register value to pinconf value */
295 case PIN_CONFIG_BIAS_DISABLE:
296 if (arg != MSM_NO_PULL)
300 case PIN_CONFIG_BIAS_PULL_DOWN:
301 if (arg != MSM_PULL_DOWN)
305 case PIN_CONFIG_BIAS_BUS_HOLD:
306 if (pctrl->soc->pull_no_keeper)
309 if (arg != MSM_KEEPER)
313 case PIN_CONFIG_BIAS_PULL_UP:
314 if (pctrl->soc->pull_no_keeper)
315 arg = arg == MSM_PULL_UP_NO_KEEPER;
317 arg = arg == MSM_PULL_UP;
321 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
322 /* Pin is not open-drain */
327 case PIN_CONFIG_DRIVE_STRENGTH:
328 arg = msm_regval_to_drive(arg);
330 case PIN_CONFIG_OUTPUT:
331 /* Pin is not output */
335 val = msm_readl_io(pctrl, g);
336 arg = !!(val & BIT(g->in_bit));
338 case PIN_CONFIG_INPUT_ENABLE:
348 *config = pinconf_to_config_packed(param, arg);
353 static int msm_config_group_set(struct pinctrl_dev *pctldev,
355 unsigned long *configs,
356 unsigned num_configs)
358 const struct msm_pingroup *g;
359 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
369 g = &pctrl->soc->groups[group];
371 for (i = 0; i < num_configs; i++) {
372 param = pinconf_to_config_param(configs[i]);
373 arg = pinconf_to_config_argument(configs[i]);
375 ret = msm_config_reg(pctrl, g, param, &mask, &bit);
379 /* Convert pinconf values to register values */
381 case PIN_CONFIG_BIAS_DISABLE:
384 case PIN_CONFIG_BIAS_PULL_DOWN:
387 case PIN_CONFIG_BIAS_BUS_HOLD:
388 if (pctrl->soc->pull_no_keeper)
393 case PIN_CONFIG_BIAS_PULL_UP:
394 if (pctrl->soc->pull_no_keeper)
395 arg = MSM_PULL_UP_NO_KEEPER;
399 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
402 case PIN_CONFIG_DRIVE_STRENGTH:
403 /* Check for invalid values */
404 if (arg > 16 || arg < 2 || (arg % 2) != 0)
409 case PIN_CONFIG_OUTPUT:
410 /* set output value */
411 raw_spin_lock_irqsave(&pctrl->lock, flags);
412 val = msm_readl_io(pctrl, g);
414 val |= BIT(g->out_bit);
416 val &= ~BIT(g->out_bit);
417 msm_writel_io(val, pctrl, g);
418 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
423 case PIN_CONFIG_INPUT_ENABLE:
428 dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
433 /* Range-check user-supplied value */
435 dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
439 raw_spin_lock_irqsave(&pctrl->lock, flags);
440 val = msm_readl_ctl(pctrl, g);
441 val &= ~(mask << bit);
443 msm_writel_ctl(val, pctrl, g);
444 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
450 static const struct pinconf_ops msm_pinconf_ops = {
452 .pin_config_group_get = msm_config_group_get,
453 .pin_config_group_set = msm_config_group_set,
456 static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
458 const struct msm_pingroup *g;
459 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
463 g = &pctrl->soc->groups[offset];
465 raw_spin_lock_irqsave(&pctrl->lock, flags);
467 val = msm_readl_ctl(pctrl, g);
468 val &= ~BIT(g->oe_bit);
469 msm_writel_ctl(val, pctrl, g);
471 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
476 static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
478 const struct msm_pingroup *g;
479 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
483 g = &pctrl->soc->groups[offset];
485 raw_spin_lock_irqsave(&pctrl->lock, flags);
487 val = msm_readl_io(pctrl, g);
489 val |= BIT(g->out_bit);
491 val &= ~BIT(g->out_bit);
492 msm_writel_io(val, pctrl, g);
494 val = msm_readl_ctl(pctrl, g);
495 val |= BIT(g->oe_bit);
496 msm_writel_ctl(val, pctrl, g);
498 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
503 static int msm_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
505 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
506 const struct msm_pingroup *g;
509 g = &pctrl->soc->groups[offset];
511 val = msm_readl_ctl(pctrl, g);
513 return val & BIT(g->oe_bit) ? GPIO_LINE_DIRECTION_OUT :
514 GPIO_LINE_DIRECTION_IN;
517 static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
519 const struct msm_pingroup *g;
520 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
523 g = &pctrl->soc->groups[offset];
525 val = msm_readl_io(pctrl, g);
526 return !!(val & BIT(g->in_bit));
529 static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
531 const struct msm_pingroup *g;
532 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
536 g = &pctrl->soc->groups[offset];
538 raw_spin_lock_irqsave(&pctrl->lock, flags);
540 val = msm_readl_io(pctrl, g);
542 val |= BIT(g->out_bit);
544 val &= ~BIT(g->out_bit);
545 msm_writel_io(val, pctrl, g);
547 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
550 #ifdef CONFIG_DEBUG_FS
551 #include <linux/seq_file.h>
553 static void msm_gpio_dbg_show_one(struct seq_file *s,
554 struct pinctrl_dev *pctldev,
555 struct gpio_chip *chip,
559 const struct msm_pingroup *g;
560 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
568 static const char * const pulls_keeper[] = {
575 static const char * const pulls_no_keeper[] = {
581 if (!gpiochip_line_is_valid(chip, offset))
584 g = &pctrl->soc->groups[offset];
585 ctl_reg = msm_readl_ctl(pctrl, g);
586 io_reg = msm_readl_io(pctrl, g);
588 is_out = !!(ctl_reg & BIT(g->oe_bit));
589 func = (ctl_reg >> g->mux_bit) & 7;
590 drive = (ctl_reg >> g->drv_bit) & 7;
591 pull = (ctl_reg >> g->pull_bit) & 3;
594 val = !!(io_reg & BIT(g->out_bit));
596 val = !!(io_reg & BIT(g->in_bit));
598 seq_printf(s, " %-8s: %-3s", g->name, is_out ? "out" : "in");
599 seq_printf(s, " %-4s func%d", val ? "high" : "low", func);
600 seq_printf(s, " %dmA", msm_regval_to_drive(drive));
601 if (pctrl->soc->pull_no_keeper)
602 seq_printf(s, " %s", pulls_no_keeper[pull]);
604 seq_printf(s, " %s", pulls_keeper[pull]);
608 static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
610 unsigned gpio = chip->base;
613 for (i = 0; i < chip->ngpio; i++, gpio++)
614 msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
618 #define msm_gpio_dbg_show NULL
621 static int msm_gpio_init_valid_mask(struct gpio_chip *gc,
622 unsigned long *valid_mask,
625 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
628 const int *reserved = pctrl->soc->reserved_gpios;
631 /* Driver provided reserved list overrides DT and ACPI */
633 bitmap_fill(valid_mask, ngpios);
634 for (i = 0; reserved[i] >= 0; i++) {
635 if (i >= ngpios || reserved[i] >= ngpios) {
636 dev_err(pctrl->dev, "invalid list of reserved GPIOs\n");
639 clear_bit(reserved[i], valid_mask);
645 /* The number of GPIOs in the ACPI tables */
646 len = ret = device_property_count_u16(pctrl->dev, "gpios");
653 tmp = kmalloc_array(len, sizeof(*tmp), GFP_KERNEL);
657 ret = device_property_read_u16_array(pctrl->dev, "gpios", tmp, len);
659 dev_err(pctrl->dev, "could not read list of GPIOs\n");
663 bitmap_zero(valid_mask, ngpios);
664 for (i = 0; i < len; i++)
665 set_bit(tmp[i], valid_mask);
672 static const struct gpio_chip msm_gpio_template = {
673 .direction_input = msm_gpio_direction_input,
674 .direction_output = msm_gpio_direction_output,
675 .get_direction = msm_gpio_get_direction,
678 .request = gpiochip_generic_request,
679 .free = gpiochip_generic_free,
680 .dbg_show = msm_gpio_dbg_show,
683 /* For dual-edge interrupts in software, since some hardware has no
686 * At appropriate moments, this function may be called to flip the polarity
687 * settings of both-edge irq lines to try and catch the next edge.
689 * The attempt is considered successful if:
690 * - the status bit goes high, indicating that an edge was caught, or
691 * - the input value of the gpio doesn't change during the attempt.
692 * If the value changes twice during the process, that would cause the first
693 * test to fail but would force the second, as two opposite
694 * transitions would cause a detection no matter the polarity setting.
696 * The do-loop tries to sledge-hammer closed the timing hole between
697 * the initial value-read and the polarity-write - if the line value changes
698 * during that window, an interrupt is lost, the new polarity setting is
699 * incorrect, and the first success test will fail, causing a retry.
701 * Algorithm comes from Google's msmgpio driver.
703 static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
704 const struct msm_pingroup *g,
707 int loop_limit = 100;
708 unsigned val, val2, intstat;
712 val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
714 pol = msm_readl_intr_cfg(pctrl, g);
715 pol ^= BIT(g->intr_polarity_bit);
716 msm_writel_intr_cfg(pol, pctrl, g);
718 val2 = msm_readl_io(pctrl, g) & BIT(g->in_bit);
719 intstat = msm_readl_intr_status(pctrl, g);
720 if (intstat || (val == val2))
722 } while (loop_limit-- > 0);
723 dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
727 static void msm_gpio_irq_mask(struct irq_data *d)
729 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
730 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
731 const struct msm_pingroup *g;
736 irq_chip_mask_parent(d);
738 if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
741 g = &pctrl->soc->groups[d->hwirq];
743 raw_spin_lock_irqsave(&pctrl->lock, flags);
745 val = msm_readl_intr_cfg(pctrl, g);
747 * There are two bits that control interrupt forwarding to the CPU. The
748 * RAW_STATUS_EN bit causes the level or edge sensed on the line to be
749 * latched into the interrupt status register when the hardware detects
750 * an irq that it's configured for (either edge for edge type or level
751 * for level type irq). The 'non-raw' status enable bit causes the
752 * hardware to assert the summary interrupt to the CPU if the latched
753 * status bit is set. There's a bug though, the edge detection logic
754 * seems to have a problem where toggling the RAW_STATUS_EN bit may
755 * cause the status bit to latch spuriously when there isn't any edge
756 * so we can't touch that bit for edge type irqs and we have to keep
757 * the bit set anyway so that edges are latched while the line is masked.
759 * To make matters more complicated, leaving the RAW_STATUS_EN bit
760 * enabled all the time causes level interrupts to re-latch into the
761 * status register because the level is still present on the line after
762 * we ack it. We clear the raw status enable bit during mask here and
763 * set the bit on unmask so the interrupt can't latch into the hardware
766 if (irqd_get_trigger_type(d) & IRQ_TYPE_LEVEL_MASK)
767 val &= ~BIT(g->intr_raw_status_bit);
769 val &= ~BIT(g->intr_enable_bit);
770 msm_writel_intr_cfg(val, pctrl, g);
772 clear_bit(d->hwirq, pctrl->enabled_irqs);
774 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
777 static void msm_gpio_irq_clear_unmask(struct irq_data *d, bool status_clear)
779 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
780 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
781 const struct msm_pingroup *g;
786 irq_chip_unmask_parent(d);
788 if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
791 g = &pctrl->soc->groups[d->hwirq];
793 raw_spin_lock_irqsave(&pctrl->lock, flags);
797 * clear the interrupt status bit before unmask to avoid
798 * any erroneous interrupts that would have got latched
799 * when the interrupt is not in use.
801 val = msm_readl_intr_status(pctrl, g);
802 val &= ~BIT(g->intr_status_bit);
803 msm_writel_intr_status(val, pctrl, g);
806 val = msm_readl_intr_cfg(pctrl, g);
807 val |= BIT(g->intr_raw_status_bit);
808 val |= BIT(g->intr_enable_bit);
809 msm_writel_intr_cfg(val, pctrl, g);
811 set_bit(d->hwirq, pctrl->enabled_irqs);
813 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
816 static void msm_gpio_irq_enable(struct irq_data *d)
818 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
819 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
822 irq_chip_enable_parent(d);
824 if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
825 msm_gpio_irq_clear_unmask(d, true);
828 static void msm_gpio_irq_disable(struct irq_data *d)
830 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
831 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
834 irq_chip_disable_parent(d);
836 if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
837 msm_gpio_irq_mask(d);
840 static void msm_gpio_irq_unmask(struct irq_data *d)
842 msm_gpio_irq_clear_unmask(d, false);
846 * msm_gpio_update_dual_edge_parent() - Prime next edge for IRQs handled by parent.
849 * This is much like msm_gpio_update_dual_edge_pos() but for IRQs that are
850 * normally handled by the parent irqchip. The logic here is slightly
851 * different due to what's easy to do with our parent, but in principle it's
854 static void msm_gpio_update_dual_edge_parent(struct irq_data *d)
856 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
857 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
858 const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
859 int loop_limit = 100;
863 /* Read the value and make a guess about what edge we need to catch */
864 val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
865 type = val ? IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
868 /* Set the parent to catch the next edge */
869 irq_chip_set_type_parent(d, type);
872 * Possibly the line changed between when we last read "val"
873 * (and decided what edge we needed) and when set the edge.
874 * If the value didn't change (or changed and then changed
875 * back) then we're done.
877 val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
878 if (type == IRQ_TYPE_EDGE_RISING) {
881 type = IRQ_TYPE_EDGE_FALLING;
882 } else if (type == IRQ_TYPE_EDGE_FALLING) {
885 type = IRQ_TYPE_EDGE_RISING;
887 } while (loop_limit-- > 0);
888 dev_warn_once(pctrl->dev, "dual-edge irq failed to stabilize\n");
891 static void msm_gpio_irq_ack(struct irq_data *d)
893 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
894 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
895 const struct msm_pingroup *g;
899 if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
900 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
901 msm_gpio_update_dual_edge_parent(d);
905 g = &pctrl->soc->groups[d->hwirq];
907 raw_spin_lock_irqsave(&pctrl->lock, flags);
909 val = msm_readl_intr_status(pctrl, g);
910 if (g->intr_ack_high)
911 val |= BIT(g->intr_status_bit);
913 val &= ~BIT(g->intr_status_bit);
914 msm_writel_intr_status(val, pctrl, g);
916 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
917 msm_gpio_update_dual_edge_pos(pctrl, g, d);
919 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
922 static bool msm_gpio_needs_dual_edge_parent_workaround(struct irq_data *d,
925 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
926 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
928 return type == IRQ_TYPE_EDGE_BOTH &&
929 pctrl->soc->wakeirq_dual_edge_errata && d->parent_data &&
930 test_bit(d->hwirq, pctrl->skip_wake_irqs);
933 static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
935 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
936 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
937 const struct msm_pingroup *g;
941 if (msm_gpio_needs_dual_edge_parent_workaround(d, type)) {
942 set_bit(d->hwirq, pctrl->dual_edge_irqs);
943 irq_set_handler_locked(d, handle_fasteoi_ack_irq);
944 msm_gpio_update_dual_edge_parent(d);
949 irq_chip_set_type_parent(d, type);
951 if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
952 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
953 irq_set_handler_locked(d, handle_fasteoi_irq);
957 g = &pctrl->soc->groups[d->hwirq];
959 raw_spin_lock_irqsave(&pctrl->lock, flags);
962 * For hw without possibility of detecting both edges
964 if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
965 set_bit(d->hwirq, pctrl->dual_edge_irqs);
967 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
969 /* Route interrupts to application cpu.
970 * With intr_target_use_scm interrupts are routed to
971 * application cpu using scm calls.
973 if (pctrl->intr_target_use_scm) {
974 u32 addr = pctrl->phys_base[0] + g->intr_target_reg;
977 qcom_scm_io_readl(addr, &val);
979 val &= ~(7 << g->intr_target_bit);
980 val |= g->intr_target_kpss_val << g->intr_target_bit;
982 ret = qcom_scm_io_writel(addr, val);
985 "Failed routing %lu interrupt to Apps proc",
988 val = msm_readl_intr_target(pctrl, g);
989 val &= ~(7 << g->intr_target_bit);
990 val |= g->intr_target_kpss_val << g->intr_target_bit;
991 msm_writel_intr_target(val, pctrl, g);
994 /* Update configuration for gpio.
995 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
996 * internal circuitry of TLMM, toggling the RAW_STATUS
997 * could cause the INTR_STATUS to be set for EDGE interrupts.
999 val = msm_readl_intr_cfg(pctrl, g);
1000 val |= BIT(g->intr_raw_status_bit);
1001 if (g->intr_detection_width == 2) {
1002 val &= ~(3 << g->intr_detection_bit);
1003 val &= ~(1 << g->intr_polarity_bit);
1005 case IRQ_TYPE_EDGE_RISING:
1006 val |= 1 << g->intr_detection_bit;
1007 val |= BIT(g->intr_polarity_bit);
1009 case IRQ_TYPE_EDGE_FALLING:
1010 val |= 2 << g->intr_detection_bit;
1011 val |= BIT(g->intr_polarity_bit);
1013 case IRQ_TYPE_EDGE_BOTH:
1014 val |= 3 << g->intr_detection_bit;
1015 val |= BIT(g->intr_polarity_bit);
1017 case IRQ_TYPE_LEVEL_LOW:
1019 case IRQ_TYPE_LEVEL_HIGH:
1020 val |= BIT(g->intr_polarity_bit);
1023 } else if (g->intr_detection_width == 1) {
1024 val &= ~(1 << g->intr_detection_bit);
1025 val &= ~(1 << g->intr_polarity_bit);
1027 case IRQ_TYPE_EDGE_RISING:
1028 val |= BIT(g->intr_detection_bit);
1029 val |= BIT(g->intr_polarity_bit);
1031 case IRQ_TYPE_EDGE_FALLING:
1032 val |= BIT(g->intr_detection_bit);
1034 case IRQ_TYPE_EDGE_BOTH:
1035 val |= BIT(g->intr_detection_bit);
1036 val |= BIT(g->intr_polarity_bit);
1038 case IRQ_TYPE_LEVEL_LOW:
1040 case IRQ_TYPE_LEVEL_HIGH:
1041 val |= BIT(g->intr_polarity_bit);
1047 msm_writel_intr_cfg(val, pctrl, g);
1049 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
1050 msm_gpio_update_dual_edge_pos(pctrl, g, d);
1052 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1054 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
1055 irq_set_handler_locked(d, handle_level_irq);
1056 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
1057 irq_set_handler_locked(d, handle_edge_irq);
1062 static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
1064 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1065 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1068 * While they may not wake up when the TLMM is powered off,
1069 * some GPIOs would like to wakeup the system from suspend
1070 * when TLMM is powered on. To allow that, enable the GPIO
1071 * summary line to be wakeup capable at GIC.
1073 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1074 return irq_chip_set_wake_parent(d, on);
1076 return irq_set_irq_wake(pctrl->irq, on);
1079 static int msm_gpio_irq_reqres(struct irq_data *d)
1081 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1082 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1085 if (!try_module_get(gc->owner))
1088 ret = msm_pinmux_request_gpio(pctrl->pctrl, NULL, d->hwirq);
1091 msm_gpio_direction_input(gc, d->hwirq);
1093 if (gpiochip_lock_as_irq(gc, d->hwirq)) {
1095 "unable to lock HW IRQ %lu for IRQ\n",
1102 * Clear the interrupt that may be pending before we enable
1104 * This is especially a problem with the GPIOs routed to the
1105 * PDC. These GPIOs are direct-connect interrupts to the GIC.
1106 * Disabling the interrupt line at the PDC does not prevent
1107 * the interrupt from being latched at the GIC. The state at
1108 * GIC needs to be cleared before enabling.
1110 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1111 irq_chip_set_parent_state(d, IRQCHIP_STATE_PENDING, 0);
1115 module_put(gc->owner);
1119 static void msm_gpio_irq_relres(struct irq_data *d)
1121 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1123 gpiochip_unlock_as_irq(gc, d->hwirq);
1124 module_put(gc->owner);
1127 static int msm_gpio_irq_set_affinity(struct irq_data *d,
1128 const struct cpumask *dest, bool force)
1130 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1131 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1133 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1134 return irq_chip_set_affinity_parent(d, dest, force);
1139 static int msm_gpio_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1141 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1142 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1144 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1145 return irq_chip_set_vcpu_affinity_parent(d, vcpu_info);
1150 static void msm_gpio_irq_handler(struct irq_desc *desc)
1152 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
1153 const struct msm_pingroup *g;
1154 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1155 struct irq_chip *chip = irq_desc_get_chip(desc);
1161 chained_irq_enter(chip, desc);
1164 * Each pin has it's own IRQ status register, so use
1165 * enabled_irq bitmap to limit the number of reads.
1167 for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
1168 g = &pctrl->soc->groups[i];
1169 val = msm_readl_intr_status(pctrl, g);
1170 if (val & BIT(g->intr_status_bit)) {
1171 irq_pin = irq_find_mapping(gc->irq.domain, i);
1172 generic_handle_irq(irq_pin);
1177 /* No interrupts were flagged */
1179 handle_bad_irq(desc);
1181 chained_irq_exit(chip, desc);
1184 static int msm_gpio_wakeirq(struct gpio_chip *gc,
1186 unsigned int child_type,
1187 unsigned int *parent,
1188 unsigned int *parent_type)
1190 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1191 const struct msm_gpio_wakeirq_map *map;
1194 *parent = GPIO_NO_WAKE_IRQ;
1195 *parent_type = IRQ_TYPE_EDGE_RISING;
1197 for (i = 0; i < pctrl->soc->nwakeirq_map; i++) {
1198 map = &pctrl->soc->wakeirq_map[i];
1199 if (map->gpio == child) {
1200 *parent = map->wakeirq;
1208 static bool msm_gpio_needs_valid_mask(struct msm_pinctrl *pctrl)
1210 if (pctrl->soc->reserved_gpios)
1213 return device_property_count_u16(pctrl->dev, "gpios") > 0;
1216 static int msm_gpio_init(struct msm_pinctrl *pctrl)
1218 struct gpio_chip *chip;
1219 struct gpio_irq_chip *girq;
1221 unsigned gpio, ngpio = pctrl->soc->ngpios;
1222 struct device_node *np;
1225 if (WARN_ON(ngpio > MAX_NR_GPIO))
1228 chip = &pctrl->chip;
1230 chip->ngpio = ngpio;
1231 chip->label = dev_name(pctrl->dev);
1232 chip->parent = pctrl->dev;
1233 chip->owner = THIS_MODULE;
1234 chip->of_node = pctrl->dev->of_node;
1235 if (msm_gpio_needs_valid_mask(pctrl))
1236 chip->init_valid_mask = msm_gpio_init_valid_mask;
1238 pctrl->irq_chip.name = "msmgpio";
1239 pctrl->irq_chip.irq_enable = msm_gpio_irq_enable;
1240 pctrl->irq_chip.irq_disable = msm_gpio_irq_disable;
1241 pctrl->irq_chip.irq_mask = msm_gpio_irq_mask;
1242 pctrl->irq_chip.irq_unmask = msm_gpio_irq_unmask;
1243 pctrl->irq_chip.irq_ack = msm_gpio_irq_ack;
1244 pctrl->irq_chip.irq_set_type = msm_gpio_irq_set_type;
1245 pctrl->irq_chip.irq_set_wake = msm_gpio_irq_set_wake;
1246 pctrl->irq_chip.irq_request_resources = msm_gpio_irq_reqres;
1247 pctrl->irq_chip.irq_release_resources = msm_gpio_irq_relres;
1248 pctrl->irq_chip.irq_set_affinity = msm_gpio_irq_set_affinity;
1249 pctrl->irq_chip.irq_set_vcpu_affinity = msm_gpio_irq_set_vcpu_affinity;
1250 pctrl->irq_chip.flags = IRQCHIP_MASK_ON_SUSPEND |
1251 IRQCHIP_SET_TYPE_MASKED |
1252 IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND;
1254 np = of_parse_phandle(pctrl->dev->of_node, "wakeup-parent", 0);
1256 chip->irq.parent_domain = irq_find_matching_host(np,
1259 if (!chip->irq.parent_domain)
1260 return -EPROBE_DEFER;
1261 chip->irq.child_to_parent_hwirq = msm_gpio_wakeirq;
1262 pctrl->irq_chip.irq_eoi = irq_chip_eoi_parent;
1264 * Let's skip handling the GPIOs, if the parent irqchip
1265 * is handling the direct connect IRQ of the GPIO.
1267 skip = irq_domain_qcom_handle_wakeup(chip->irq.parent_domain);
1268 for (i = 0; skip && i < pctrl->soc->nwakeirq_map; i++) {
1269 gpio = pctrl->soc->wakeirq_map[i].gpio;
1270 set_bit(gpio, pctrl->skip_wake_irqs);
1275 girq->chip = &pctrl->irq_chip;
1276 girq->parent_handler = msm_gpio_irq_handler;
1277 girq->fwnode = pctrl->dev->fwnode;
1278 girq->num_parents = 1;
1279 girq->parents = devm_kcalloc(pctrl->dev, 1, sizeof(*girq->parents),
1283 girq->default_type = IRQ_TYPE_NONE;
1284 girq->handler = handle_bad_irq;
1285 girq->parents[0] = pctrl->irq;
1287 ret = gpiochip_add_data(&pctrl->chip, pctrl);
1289 dev_err(pctrl->dev, "Failed register gpiochip\n");
1294 * For DeviceTree-supported systems, the gpio core checks the
1295 * pinctrl's device node for the "gpio-ranges" property.
1296 * If it is present, it takes care of adding the pin ranges
1297 * for the driver. In this case the driver can skip ahead.
1299 * In order to remain compatible with older, existing DeviceTree
1300 * files which don't set the "gpio-ranges" property or systems that
1301 * utilize ACPI the driver has to call gpiochip_add_pin_range().
1303 if (!of_property_read_bool(pctrl->dev->of_node, "gpio-ranges")) {
1304 ret = gpiochip_add_pin_range(&pctrl->chip,
1305 dev_name(pctrl->dev), 0, 0, chip->ngpio);
1307 dev_err(pctrl->dev, "Failed to add pin range\n");
1308 gpiochip_remove(&pctrl->chip);
1316 static int msm_ps_hold_restart(struct notifier_block *nb, unsigned long action,
1319 struct msm_pinctrl *pctrl = container_of(nb, struct msm_pinctrl, restart_nb);
1321 writel(0, pctrl->regs[0] + PS_HOLD_OFFSET);
1326 static struct msm_pinctrl *poweroff_pctrl;
1328 static void msm_ps_hold_poweroff(void)
1330 msm_ps_hold_restart(&poweroff_pctrl->restart_nb, 0, NULL);
1333 static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
1336 const struct msm_function *func = pctrl->soc->functions;
1338 for (i = 0; i < pctrl->soc->nfunctions; i++)
1339 if (!strcmp(func[i].name, "ps_hold")) {
1340 pctrl->restart_nb.notifier_call = msm_ps_hold_restart;
1341 pctrl->restart_nb.priority = 128;
1342 if (register_restart_handler(&pctrl->restart_nb))
1344 "failed to setup restart handler.\n");
1345 poweroff_pctrl = pctrl;
1346 pm_power_off = msm_ps_hold_poweroff;
1351 static __maybe_unused int msm_pinctrl_suspend(struct device *dev)
1353 struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
1355 return pinctrl_force_sleep(pctrl->pctrl);
1358 static __maybe_unused int msm_pinctrl_resume(struct device *dev)
1360 struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
1362 return pinctrl_force_default(pctrl->pctrl);
1365 SIMPLE_DEV_PM_OPS(msm_pinctrl_dev_pm_ops, msm_pinctrl_suspend,
1366 msm_pinctrl_resume);
1368 EXPORT_SYMBOL(msm_pinctrl_dev_pm_ops);
1370 int msm_pinctrl_probe(struct platform_device *pdev,
1371 const struct msm_pinctrl_soc_data *soc_data)
1373 struct msm_pinctrl *pctrl;
1374 struct resource *res;
1378 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1382 pctrl->dev = &pdev->dev;
1383 pctrl->soc = soc_data;
1384 pctrl->chip = msm_gpio_template;
1385 pctrl->intr_target_use_scm = of_device_is_compatible(
1386 pctrl->dev->of_node,
1387 "qcom,ipq8064-pinctrl");
1389 raw_spin_lock_init(&pctrl->lock);
1391 if (soc_data->tiles) {
1392 for (i = 0; i < soc_data->ntiles; i++) {
1393 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1394 soc_data->tiles[i]);
1395 pctrl->regs[i] = devm_ioremap_resource(&pdev->dev, res);
1396 if (IS_ERR(pctrl->regs[i]))
1397 return PTR_ERR(pctrl->regs[i]);
1400 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1401 pctrl->regs[0] = devm_ioremap_resource(&pdev->dev, res);
1402 if (IS_ERR(pctrl->regs[0]))
1403 return PTR_ERR(pctrl->regs[0]);
1405 pctrl->phys_base[0] = res->start;
1408 msm_pinctrl_setup_pm_reset(pctrl);
1410 pctrl->irq = platform_get_irq(pdev, 0);
1414 pctrl->desc.owner = THIS_MODULE;
1415 pctrl->desc.pctlops = &msm_pinctrl_ops;
1416 pctrl->desc.pmxops = &msm_pinmux_ops;
1417 pctrl->desc.confops = &msm_pinconf_ops;
1418 pctrl->desc.name = dev_name(&pdev->dev);
1419 pctrl->desc.pins = pctrl->soc->pins;
1420 pctrl->desc.npins = pctrl->soc->npins;
1422 pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &pctrl->desc, pctrl);
1423 if (IS_ERR(pctrl->pctrl)) {
1424 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
1425 return PTR_ERR(pctrl->pctrl);
1428 ret = msm_gpio_init(pctrl);
1432 platform_set_drvdata(pdev, pctrl);
1434 dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
1438 EXPORT_SYMBOL(msm_pinctrl_probe);
1440 int msm_pinctrl_remove(struct platform_device *pdev)
1442 struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
1444 gpiochip_remove(&pctrl->chip);
1446 unregister_restart_handler(&pctrl->restart_nb);
1450 EXPORT_SYMBOL(msm_pinctrl_remove);
1452 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. TLMM driver");
1453 MODULE_LICENSE("GPL v2");