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: parent irq for the TLMM irq_chip.
46 * @intr_target_use_scm: route irq to application cpu using scm calls
47 * @lock: Spinlock to protect register resources as well
48 * as msm_pinctrl data structures.
49 * @enabled_irqs: Bitmap of currently enabled irqs.
50 * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
52 * @skip_wake_irqs: Skip IRQs that are handled by wakeup interrupt controller
53 * @disabled_for_mux: These IRQs were disabled because we muxed away.
54 * @ever_gpio: This bit is set the first time we mux a pin to gpio_func.
55 * @soc: Reference to soc_data of platform specific data.
56 * @regs: Base addresses for the TLMM tiles.
57 * @phys_base: Physical base address
61 struct pinctrl_dev *pctrl;
62 struct gpio_chip chip;
63 struct pinctrl_desc desc;
64 struct notifier_block restart_nb;
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);
75 DECLARE_BITMAP(disabled_for_mux, MAX_NR_GPIO);
76 DECLARE_BITMAP(ever_gpio, MAX_NR_GPIO);
78 const struct msm_pinctrl_soc_data *soc;
79 void __iomem *regs[MAX_NR_TILES];
80 u32 phys_base[MAX_NR_TILES];
83 #define MSM_ACCESSOR(name) \
84 static u32 msm_readl_##name(struct msm_pinctrl *pctrl, \
85 const struct msm_pingroup *g) \
87 return readl(pctrl->regs[g->tile] + g->name##_reg); \
89 static void msm_writel_##name(u32 val, struct msm_pinctrl *pctrl, \
90 const struct msm_pingroup *g) \
92 writel(val, pctrl->regs[g->tile] + g->name##_reg); \
97 MSM_ACCESSOR(intr_cfg)
98 MSM_ACCESSOR(intr_status)
99 MSM_ACCESSOR(intr_target)
101 static void msm_ack_intr_status(struct msm_pinctrl *pctrl,
102 const struct msm_pingroup *g)
104 u32 val = g->intr_ack_high ? BIT(g->intr_status_bit) : 0;
106 msm_writel_intr_status(val, pctrl, g);
109 static int msm_get_groups_count(struct pinctrl_dev *pctldev)
111 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
113 return pctrl->soc->ngroups;
116 static const char *msm_get_group_name(struct pinctrl_dev *pctldev,
119 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
121 return pctrl->soc->groups[group].name;
124 static int msm_get_group_pins(struct pinctrl_dev *pctldev,
126 const unsigned **pins,
129 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
131 *pins = pctrl->soc->groups[group].pins;
132 *num_pins = pctrl->soc->groups[group].npins;
136 static const struct pinctrl_ops msm_pinctrl_ops = {
137 .get_groups_count = msm_get_groups_count,
138 .get_group_name = msm_get_group_name,
139 .get_group_pins = msm_get_group_pins,
140 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
141 .dt_free_map = pinctrl_utils_free_map,
144 static int msm_pinmux_request(struct pinctrl_dev *pctldev, unsigned offset)
146 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
147 struct gpio_chip *chip = &pctrl->chip;
149 return gpiochip_line_is_valid(chip, offset) ? 0 : -EINVAL;
152 static int msm_get_functions_count(struct pinctrl_dev *pctldev)
154 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
156 return pctrl->soc->nfunctions;
159 static const char *msm_get_function_name(struct pinctrl_dev *pctldev,
162 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
164 return pctrl->soc->functions[function].name;
167 static int msm_get_function_groups(struct pinctrl_dev *pctldev,
169 const char * const **groups,
170 unsigned * const num_groups)
172 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
174 *groups = pctrl->soc->functions[function].groups;
175 *num_groups = pctrl->soc->functions[function].ngroups;
179 static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
183 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
184 struct gpio_chip *gc = &pctrl->chip;
185 unsigned int irq = irq_find_mapping(gc->irq.domain, group);
186 struct irq_data *d = irq_get_irq_data(irq);
187 unsigned int gpio_func = pctrl->soc->gpio_func;
188 unsigned int egpio_func = pctrl->soc->egpio_func;
189 const struct msm_pingroup *g;
194 g = &pctrl->soc->groups[group];
195 mask = GENMASK(g->mux_bit + order_base_2(g->nfuncs) - 1, g->mux_bit);
197 for (i = 0; i < g->nfuncs; i++) {
198 if (g->funcs[i] == function)
202 if (WARN_ON(i == g->nfuncs))
206 * If an GPIO interrupt is setup on this pin then we need special
207 * handling. Specifically interrupt detection logic will still see
208 * the pin twiddle even when we're muxed away.
210 * When we see a pin with an interrupt setup on it then we'll disable
211 * (mask) interrupts on it when we mux away until we mux back. Note
212 * that disable_irq() refcounts and interrupts are disabled as long as
213 * at least one disable_irq() has been called.
215 if (d && i != gpio_func &&
216 !test_and_set_bit(d->hwirq, pctrl->disabled_for_mux))
219 raw_spin_lock_irqsave(&pctrl->lock, flags);
221 val = msm_readl_ctl(pctrl, g);
224 * If this is the first time muxing to GPIO and the direction is
225 * output, make sure that we're not going to be glitching the pin
226 * by reading the current state of the pin and setting it as the
229 if (i == gpio_func && (val & BIT(g->oe_bit)) &&
230 !test_and_set_bit(group, pctrl->ever_gpio)) {
231 u32 io_val = msm_readl_io(pctrl, g);
233 if (io_val & BIT(g->in_bit)) {
234 if (!(io_val & BIT(g->out_bit)))
235 msm_writel_io(io_val | BIT(g->out_bit), pctrl, g);
237 if (io_val & BIT(g->out_bit))
238 msm_writel_io(io_val & ~BIT(g->out_bit), pctrl, g);
242 if (egpio_func && i == egpio_func) {
243 if (val & BIT(g->egpio_present))
244 val &= ~BIT(g->egpio_enable);
247 val |= i << g->mux_bit;
248 /* Claim ownership of pin if egpio capable */
249 if (egpio_func && val & BIT(g->egpio_present))
250 val |= BIT(g->egpio_enable);
253 msm_writel_ctl(val, pctrl, g);
255 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
257 if (d && i == gpio_func &&
258 test_and_clear_bit(d->hwirq, pctrl->disabled_for_mux)) {
260 * Clear interrupts detected while not GPIO since we only
263 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
264 irq_chip_set_parent_state(d, IRQCHIP_STATE_PENDING, false);
266 msm_ack_intr_status(pctrl, g);
274 static int msm_pinmux_request_gpio(struct pinctrl_dev *pctldev,
275 struct pinctrl_gpio_range *range,
278 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
279 const struct msm_pingroup *g = &pctrl->soc->groups[offset];
281 /* No funcs? Probably ACPI so can't do anything here */
285 return msm_pinmux_set_mux(pctldev, g->funcs[pctrl->soc->gpio_func], offset);
288 static const struct pinmux_ops msm_pinmux_ops = {
289 .request = msm_pinmux_request,
290 .get_functions_count = msm_get_functions_count,
291 .get_function_name = msm_get_function_name,
292 .get_function_groups = msm_get_function_groups,
293 .gpio_request_enable = msm_pinmux_request_gpio,
294 .set_mux = msm_pinmux_set_mux,
297 static int msm_config_reg(struct msm_pinctrl *pctrl,
298 const struct msm_pingroup *g,
304 case PIN_CONFIG_BIAS_DISABLE:
305 case PIN_CONFIG_BIAS_PULL_DOWN:
306 case PIN_CONFIG_BIAS_BUS_HOLD:
307 case PIN_CONFIG_BIAS_PULL_UP:
311 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
315 case PIN_CONFIG_DRIVE_STRENGTH:
319 case PIN_CONFIG_OUTPUT:
320 case PIN_CONFIG_INPUT_ENABLE:
331 #define MSM_NO_PULL 0
332 #define MSM_PULL_DOWN 1
334 #define MSM_PULL_UP_NO_KEEPER 2
335 #define MSM_PULL_UP 3
337 static unsigned msm_regval_to_drive(u32 val)
339 return (val + 1) * 2;
342 static int msm_config_group_get(struct pinctrl_dev *pctldev,
344 unsigned long *config)
346 const struct msm_pingroup *g;
347 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
348 unsigned param = pinconf_to_config_param(*config);
355 g = &pctrl->soc->groups[group];
357 ret = msm_config_reg(pctrl, g, param, &mask, &bit);
361 val = msm_readl_ctl(pctrl, g);
362 arg = (val >> bit) & mask;
364 /* Convert register value to pinconf value */
366 case PIN_CONFIG_BIAS_DISABLE:
367 if (arg != MSM_NO_PULL)
371 case PIN_CONFIG_BIAS_PULL_DOWN:
372 if (arg != MSM_PULL_DOWN)
376 case PIN_CONFIG_BIAS_BUS_HOLD:
377 if (pctrl->soc->pull_no_keeper)
380 if (arg != MSM_KEEPER)
384 case PIN_CONFIG_BIAS_PULL_UP:
385 if (pctrl->soc->pull_no_keeper)
386 arg = arg == MSM_PULL_UP_NO_KEEPER;
388 arg = arg == MSM_PULL_UP;
392 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
393 /* Pin is not open-drain */
398 case PIN_CONFIG_DRIVE_STRENGTH:
399 arg = msm_regval_to_drive(arg);
401 case PIN_CONFIG_OUTPUT:
402 /* Pin is not output */
406 val = msm_readl_io(pctrl, g);
407 arg = !!(val & BIT(g->in_bit));
409 case PIN_CONFIG_INPUT_ENABLE:
419 *config = pinconf_to_config_packed(param, arg);
424 static int msm_config_group_set(struct pinctrl_dev *pctldev,
426 unsigned long *configs,
427 unsigned num_configs)
429 const struct msm_pingroup *g;
430 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
440 g = &pctrl->soc->groups[group];
442 for (i = 0; i < num_configs; i++) {
443 param = pinconf_to_config_param(configs[i]);
444 arg = pinconf_to_config_argument(configs[i]);
446 ret = msm_config_reg(pctrl, g, param, &mask, &bit);
450 /* Convert pinconf values to register values */
452 case PIN_CONFIG_BIAS_DISABLE:
455 case PIN_CONFIG_BIAS_PULL_DOWN:
458 case PIN_CONFIG_BIAS_BUS_HOLD:
459 if (pctrl->soc->pull_no_keeper)
464 case PIN_CONFIG_BIAS_PULL_UP:
465 if (pctrl->soc->pull_no_keeper)
466 arg = MSM_PULL_UP_NO_KEEPER;
470 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
473 case PIN_CONFIG_DRIVE_STRENGTH:
474 /* Check for invalid values */
475 if (arg > 16 || arg < 2 || (arg % 2) != 0)
480 case PIN_CONFIG_OUTPUT:
481 /* set output value */
482 raw_spin_lock_irqsave(&pctrl->lock, flags);
483 val = msm_readl_io(pctrl, g);
485 val |= BIT(g->out_bit);
487 val &= ~BIT(g->out_bit);
488 msm_writel_io(val, pctrl, g);
489 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
494 case PIN_CONFIG_INPUT_ENABLE:
499 dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
504 /* Range-check user-supplied value */
506 dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
510 raw_spin_lock_irqsave(&pctrl->lock, flags);
511 val = msm_readl_ctl(pctrl, g);
512 val &= ~(mask << bit);
514 msm_writel_ctl(val, pctrl, g);
515 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
521 static const struct pinconf_ops msm_pinconf_ops = {
523 .pin_config_group_get = msm_config_group_get,
524 .pin_config_group_set = msm_config_group_set,
527 static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
529 const struct msm_pingroup *g;
530 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
534 g = &pctrl->soc->groups[offset];
536 raw_spin_lock_irqsave(&pctrl->lock, flags);
538 val = msm_readl_ctl(pctrl, g);
539 val &= ~BIT(g->oe_bit);
540 msm_writel_ctl(val, pctrl, g);
542 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
547 static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
549 const struct msm_pingroup *g;
550 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
554 g = &pctrl->soc->groups[offset];
556 raw_spin_lock_irqsave(&pctrl->lock, flags);
558 val = msm_readl_io(pctrl, g);
560 val |= BIT(g->out_bit);
562 val &= ~BIT(g->out_bit);
563 msm_writel_io(val, pctrl, g);
565 val = msm_readl_ctl(pctrl, g);
566 val |= BIT(g->oe_bit);
567 msm_writel_ctl(val, pctrl, g);
569 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
574 static int msm_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
576 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
577 const struct msm_pingroup *g;
580 g = &pctrl->soc->groups[offset];
582 val = msm_readl_ctl(pctrl, g);
584 return val & BIT(g->oe_bit) ? GPIO_LINE_DIRECTION_OUT :
585 GPIO_LINE_DIRECTION_IN;
588 static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
590 const struct msm_pingroup *g;
591 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
594 g = &pctrl->soc->groups[offset];
596 val = msm_readl_io(pctrl, g);
597 return !!(val & BIT(g->in_bit));
600 static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
602 const struct msm_pingroup *g;
603 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
607 g = &pctrl->soc->groups[offset];
609 raw_spin_lock_irqsave(&pctrl->lock, flags);
611 val = msm_readl_io(pctrl, g);
613 val |= BIT(g->out_bit);
615 val &= ~BIT(g->out_bit);
616 msm_writel_io(val, pctrl, g);
618 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
621 #ifdef CONFIG_DEBUG_FS
622 #include <linux/seq_file.h>
624 static void msm_gpio_dbg_show_one(struct seq_file *s,
625 struct pinctrl_dev *pctldev,
626 struct gpio_chip *chip,
630 const struct msm_pingroup *g;
631 struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
640 static const char * const pulls_keeper[] = {
647 static const char * const pulls_no_keeper[] = {
653 if (!gpiochip_line_is_valid(chip, offset))
656 g = &pctrl->soc->groups[offset];
657 ctl_reg = msm_readl_ctl(pctrl, g);
658 io_reg = msm_readl_io(pctrl, g);
660 is_out = !!(ctl_reg & BIT(g->oe_bit));
661 func = (ctl_reg >> g->mux_bit) & 7;
662 drive = (ctl_reg >> g->drv_bit) & 7;
663 pull = (ctl_reg >> g->pull_bit) & 3;
665 if (pctrl->soc->egpio_func && ctl_reg & BIT(g->egpio_present))
666 egpio_enable = !(ctl_reg & BIT(g->egpio_enable));
669 val = !!(io_reg & BIT(g->out_bit));
671 val = !!(io_reg & BIT(g->in_bit));
674 seq_printf(s, " %-8s: egpio\n", g->name);
678 seq_printf(s, " %-8s: %-3s", g->name, is_out ? "out" : "in");
679 seq_printf(s, " %-4s func%d", val ? "high" : "low", func);
680 seq_printf(s, " %dmA", msm_regval_to_drive(drive));
681 if (pctrl->soc->pull_no_keeper)
682 seq_printf(s, " %s", pulls_no_keeper[pull]);
684 seq_printf(s, " %s", pulls_keeper[pull]);
688 static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
690 unsigned gpio = chip->base;
693 for (i = 0; i < chip->ngpio; i++, gpio++)
694 msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
698 #define msm_gpio_dbg_show NULL
701 static int msm_gpio_init_valid_mask(struct gpio_chip *gc,
702 unsigned long *valid_mask,
705 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
708 const int *reserved = pctrl->soc->reserved_gpios;
711 /* Driver provided reserved list overrides DT and ACPI */
713 bitmap_fill(valid_mask, ngpios);
714 for (i = 0; reserved[i] >= 0; i++) {
715 if (i >= ngpios || reserved[i] >= ngpios) {
716 dev_err(pctrl->dev, "invalid list of reserved GPIOs\n");
719 clear_bit(reserved[i], valid_mask);
725 /* The number of GPIOs in the ACPI tables */
726 len = ret = device_property_count_u16(pctrl->dev, "gpios");
733 tmp = kmalloc_array(len, sizeof(*tmp), GFP_KERNEL);
737 ret = device_property_read_u16_array(pctrl->dev, "gpios", tmp, len);
739 dev_err(pctrl->dev, "could not read list of GPIOs\n");
743 bitmap_zero(valid_mask, ngpios);
744 for (i = 0; i < len; i++)
745 set_bit(tmp[i], valid_mask);
752 static const struct gpio_chip msm_gpio_template = {
753 .direction_input = msm_gpio_direction_input,
754 .direction_output = msm_gpio_direction_output,
755 .get_direction = msm_gpio_get_direction,
758 .request = gpiochip_generic_request,
759 .free = gpiochip_generic_free,
760 .dbg_show = msm_gpio_dbg_show,
763 /* For dual-edge interrupts in software, since some hardware has no
766 * At appropriate moments, this function may be called to flip the polarity
767 * settings of both-edge irq lines to try and catch the next edge.
769 * The attempt is considered successful if:
770 * - the status bit goes high, indicating that an edge was caught, or
771 * - the input value of the gpio doesn't change during the attempt.
772 * If the value changes twice during the process, that would cause the first
773 * test to fail but would force the second, as two opposite
774 * transitions would cause a detection no matter the polarity setting.
776 * The do-loop tries to sledge-hammer closed the timing hole between
777 * the initial value-read and the polarity-write - if the line value changes
778 * during that window, an interrupt is lost, the new polarity setting is
779 * incorrect, and the first success test will fail, causing a retry.
781 * Algorithm comes from Google's msmgpio driver.
783 static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
784 const struct msm_pingroup *g,
787 int loop_limit = 100;
788 unsigned val, val2, intstat;
792 val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
794 pol = msm_readl_intr_cfg(pctrl, g);
795 pol ^= BIT(g->intr_polarity_bit);
796 msm_writel_intr_cfg(pol, pctrl, g);
798 val2 = msm_readl_io(pctrl, g) & BIT(g->in_bit);
799 intstat = msm_readl_intr_status(pctrl, g);
800 if (intstat || (val == val2))
802 } while (loop_limit-- > 0);
803 dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
807 static void msm_gpio_irq_mask(struct irq_data *d)
809 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
810 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
811 const struct msm_pingroup *g;
816 irq_chip_mask_parent(d);
818 if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
821 g = &pctrl->soc->groups[d->hwirq];
823 raw_spin_lock_irqsave(&pctrl->lock, flags);
825 val = msm_readl_intr_cfg(pctrl, g);
827 * There are two bits that control interrupt forwarding to the CPU. The
828 * RAW_STATUS_EN bit causes the level or edge sensed on the line to be
829 * latched into the interrupt status register when the hardware detects
830 * an irq that it's configured for (either edge for edge type or level
831 * for level type irq). The 'non-raw' status enable bit causes the
832 * hardware to assert the summary interrupt to the CPU if the latched
833 * status bit is set. There's a bug though, the edge detection logic
834 * seems to have a problem where toggling the RAW_STATUS_EN bit may
835 * cause the status bit to latch spuriously when there isn't any edge
836 * so we can't touch that bit for edge type irqs and we have to keep
837 * the bit set anyway so that edges are latched while the line is masked.
839 * To make matters more complicated, leaving the RAW_STATUS_EN bit
840 * enabled all the time causes level interrupts to re-latch into the
841 * status register because the level is still present on the line after
842 * we ack it. We clear the raw status enable bit during mask here and
843 * set the bit on unmask so the interrupt can't latch into the hardware
846 if (irqd_get_trigger_type(d) & IRQ_TYPE_LEVEL_MASK)
847 val &= ~BIT(g->intr_raw_status_bit);
849 val &= ~BIT(g->intr_enable_bit);
850 msm_writel_intr_cfg(val, pctrl, g);
852 clear_bit(d->hwirq, pctrl->enabled_irqs);
854 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
857 static void msm_gpio_irq_unmask(struct irq_data *d)
859 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
860 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
861 const struct msm_pingroup *g;
866 irq_chip_unmask_parent(d);
868 if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
871 g = &pctrl->soc->groups[d->hwirq];
873 raw_spin_lock_irqsave(&pctrl->lock, flags);
875 val = msm_readl_intr_cfg(pctrl, g);
876 val |= BIT(g->intr_raw_status_bit);
877 val |= BIT(g->intr_enable_bit);
878 msm_writel_intr_cfg(val, pctrl, g);
880 set_bit(d->hwirq, pctrl->enabled_irqs);
882 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
885 static void msm_gpio_irq_enable(struct irq_data *d)
887 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
888 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
890 gpiochip_enable_irq(gc, d->hwirq);
893 irq_chip_enable_parent(d);
895 if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
896 msm_gpio_irq_unmask(d);
899 static void msm_gpio_irq_disable(struct irq_data *d)
901 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
902 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
905 irq_chip_disable_parent(d);
907 if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
908 msm_gpio_irq_mask(d);
910 gpiochip_disable_irq(gc, d->hwirq);
914 * msm_gpio_update_dual_edge_parent() - Prime next edge for IRQs handled by parent.
917 * This is much like msm_gpio_update_dual_edge_pos() but for IRQs that are
918 * normally handled by the parent irqchip. The logic here is slightly
919 * different due to what's easy to do with our parent, but in principle it's
922 static void msm_gpio_update_dual_edge_parent(struct irq_data *d)
924 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
925 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
926 const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
927 int loop_limit = 100;
931 /* Read the value and make a guess about what edge we need to catch */
932 val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
933 type = val ? IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
936 /* Set the parent to catch the next edge */
937 irq_chip_set_type_parent(d, type);
940 * Possibly the line changed between when we last read "val"
941 * (and decided what edge we needed) and when set the edge.
942 * If the value didn't change (or changed and then changed
943 * back) then we're done.
945 val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
946 if (type == IRQ_TYPE_EDGE_RISING) {
949 type = IRQ_TYPE_EDGE_FALLING;
950 } else if (type == IRQ_TYPE_EDGE_FALLING) {
953 type = IRQ_TYPE_EDGE_RISING;
955 } while (loop_limit-- > 0);
956 dev_warn_once(pctrl->dev, "dual-edge irq failed to stabilize\n");
959 static void msm_gpio_irq_ack(struct irq_data *d)
961 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
962 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
963 const struct msm_pingroup *g;
966 if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
967 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
968 msm_gpio_update_dual_edge_parent(d);
972 g = &pctrl->soc->groups[d->hwirq];
974 raw_spin_lock_irqsave(&pctrl->lock, flags);
976 msm_ack_intr_status(pctrl, g);
978 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
979 msm_gpio_update_dual_edge_pos(pctrl, g, d);
981 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
984 static void msm_gpio_irq_eoi(struct irq_data *d)
992 static bool msm_gpio_needs_dual_edge_parent_workaround(struct irq_data *d,
995 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
996 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
998 return type == IRQ_TYPE_EDGE_BOTH &&
999 pctrl->soc->wakeirq_dual_edge_errata && d->parent_data &&
1000 test_bit(d->hwirq, pctrl->skip_wake_irqs);
1003 static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
1005 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1006 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1007 const struct msm_pingroup *g;
1008 unsigned long flags;
1012 if (msm_gpio_needs_dual_edge_parent_workaround(d, type)) {
1013 set_bit(d->hwirq, pctrl->dual_edge_irqs);
1014 irq_set_handler_locked(d, handle_fasteoi_ack_irq);
1015 msm_gpio_update_dual_edge_parent(d);
1020 irq_chip_set_type_parent(d, type);
1022 if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
1023 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
1024 irq_set_handler_locked(d, handle_fasteoi_irq);
1028 g = &pctrl->soc->groups[d->hwirq];
1030 raw_spin_lock_irqsave(&pctrl->lock, flags);
1033 * For hw without possibility of detecting both edges
1035 if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
1036 set_bit(d->hwirq, pctrl->dual_edge_irqs);
1038 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
1040 /* Route interrupts to application cpu.
1041 * With intr_target_use_scm interrupts are routed to
1042 * application cpu using scm calls.
1044 if (pctrl->intr_target_use_scm) {
1045 u32 addr = pctrl->phys_base[0] + g->intr_target_reg;
1048 qcom_scm_io_readl(addr, &val);
1050 val &= ~(7 << g->intr_target_bit);
1051 val |= g->intr_target_kpss_val << g->intr_target_bit;
1053 ret = qcom_scm_io_writel(addr, val);
1056 "Failed routing %lu interrupt to Apps proc",
1059 val = msm_readl_intr_target(pctrl, g);
1060 val &= ~(7 << g->intr_target_bit);
1061 val |= g->intr_target_kpss_val << g->intr_target_bit;
1062 msm_writel_intr_target(val, pctrl, g);
1065 /* Update configuration for gpio.
1066 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
1067 * internal circuitry of TLMM, toggling the RAW_STATUS
1068 * could cause the INTR_STATUS to be set for EDGE interrupts.
1070 val = msm_readl_intr_cfg(pctrl, g);
1071 was_enabled = val & BIT(g->intr_raw_status_bit);
1072 val |= BIT(g->intr_raw_status_bit);
1073 if (g->intr_detection_width == 2) {
1074 val &= ~(3 << g->intr_detection_bit);
1075 val &= ~(1 << g->intr_polarity_bit);
1077 case IRQ_TYPE_EDGE_RISING:
1078 val |= 1 << g->intr_detection_bit;
1079 val |= BIT(g->intr_polarity_bit);
1081 case IRQ_TYPE_EDGE_FALLING:
1082 val |= 2 << g->intr_detection_bit;
1083 val |= BIT(g->intr_polarity_bit);
1085 case IRQ_TYPE_EDGE_BOTH:
1086 val |= 3 << g->intr_detection_bit;
1087 val |= BIT(g->intr_polarity_bit);
1089 case IRQ_TYPE_LEVEL_LOW:
1091 case IRQ_TYPE_LEVEL_HIGH:
1092 val |= BIT(g->intr_polarity_bit);
1095 } else if (g->intr_detection_width == 1) {
1096 val &= ~(1 << g->intr_detection_bit);
1097 val &= ~(1 << g->intr_polarity_bit);
1099 case IRQ_TYPE_EDGE_RISING:
1100 val |= BIT(g->intr_detection_bit);
1101 val |= BIT(g->intr_polarity_bit);
1103 case IRQ_TYPE_EDGE_FALLING:
1104 val |= BIT(g->intr_detection_bit);
1106 case IRQ_TYPE_EDGE_BOTH:
1107 val |= BIT(g->intr_detection_bit);
1108 val |= BIT(g->intr_polarity_bit);
1110 case IRQ_TYPE_LEVEL_LOW:
1112 case IRQ_TYPE_LEVEL_HIGH:
1113 val |= BIT(g->intr_polarity_bit);
1119 msm_writel_intr_cfg(val, pctrl, g);
1122 * The first time we set RAW_STATUS_EN it could trigger an interrupt.
1123 * Clear the interrupt. This is safe because we have
1124 * IRQCHIP_SET_TYPE_MASKED.
1127 msm_ack_intr_status(pctrl, g);
1129 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
1130 msm_gpio_update_dual_edge_pos(pctrl, g, d);
1132 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1134 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
1135 irq_set_handler_locked(d, handle_level_irq);
1136 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
1137 irq_set_handler_locked(d, handle_edge_irq);
1142 static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
1144 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1145 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1148 * While they may not wake up when the TLMM is powered off,
1149 * some GPIOs would like to wakeup the system from suspend
1150 * when TLMM is powered on. To allow that, enable the GPIO
1151 * summary line to be wakeup capable at GIC.
1153 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1154 return irq_chip_set_wake_parent(d, on);
1156 return irq_set_irq_wake(pctrl->irq, on);
1159 static int msm_gpio_irq_reqres(struct irq_data *d)
1161 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1162 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1165 if (!try_module_get(gc->owner))
1168 ret = msm_pinmux_request_gpio(pctrl->pctrl, NULL, d->hwirq);
1171 msm_gpio_direction_input(gc, d->hwirq);
1173 if (gpiochip_lock_as_irq(gc, d->hwirq)) {
1175 "unable to lock HW IRQ %lu for IRQ\n",
1182 * The disable / clear-enable workaround we do in msm_pinmux_set_mux()
1183 * only works if disable is not lazy since we only clear any bogus
1184 * interrupt in hardware. Explicitly mark the interrupt as UNLAZY.
1186 irq_set_status_flags(d->irq, IRQ_DISABLE_UNLAZY);
1190 module_put(gc->owner);
1194 static void msm_gpio_irq_relres(struct irq_data *d)
1196 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1198 gpiochip_unlock_as_irq(gc, d->hwirq);
1199 module_put(gc->owner);
1202 static int msm_gpio_irq_set_affinity(struct irq_data *d,
1203 const struct cpumask *dest, bool force)
1205 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1206 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1208 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1209 return irq_chip_set_affinity_parent(d, dest, force);
1214 static int msm_gpio_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1216 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1217 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1219 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1220 return irq_chip_set_vcpu_affinity_parent(d, vcpu_info);
1225 static void msm_gpio_irq_handler(struct irq_desc *desc)
1227 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
1228 const struct msm_pingroup *g;
1229 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1230 struct irq_chip *chip = irq_desc_get_chip(desc);
1235 chained_irq_enter(chip, desc);
1238 * Each pin has it's own IRQ status register, so use
1239 * enabled_irq bitmap to limit the number of reads.
1241 for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
1242 g = &pctrl->soc->groups[i];
1243 val = msm_readl_intr_status(pctrl, g);
1244 if (val & BIT(g->intr_status_bit)) {
1245 generic_handle_domain_irq(gc->irq.domain, i);
1250 /* No interrupts were flagged */
1252 handle_bad_irq(desc);
1254 chained_irq_exit(chip, desc);
1257 static int msm_gpio_wakeirq(struct gpio_chip *gc,
1259 unsigned int child_type,
1260 unsigned int *parent,
1261 unsigned int *parent_type)
1263 struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1264 const struct msm_gpio_wakeirq_map *map;
1267 *parent = GPIO_NO_WAKE_IRQ;
1268 *parent_type = IRQ_TYPE_EDGE_RISING;
1270 for (i = 0; i < pctrl->soc->nwakeirq_map; i++) {
1271 map = &pctrl->soc->wakeirq_map[i];
1272 if (map->gpio == child) {
1273 *parent = map->wakeirq;
1281 static bool msm_gpio_needs_valid_mask(struct msm_pinctrl *pctrl)
1283 if (pctrl->soc->reserved_gpios)
1286 return device_property_count_u16(pctrl->dev, "gpios") > 0;
1289 static const struct irq_chip msm_gpio_irq_chip = {
1291 .irq_enable = msm_gpio_irq_enable,
1292 .irq_disable = msm_gpio_irq_disable,
1293 .irq_mask = msm_gpio_irq_mask,
1294 .irq_unmask = msm_gpio_irq_unmask,
1295 .irq_ack = msm_gpio_irq_ack,
1296 .irq_eoi = msm_gpio_irq_eoi,
1297 .irq_set_type = msm_gpio_irq_set_type,
1298 .irq_set_wake = msm_gpio_irq_set_wake,
1299 .irq_request_resources = msm_gpio_irq_reqres,
1300 .irq_release_resources = msm_gpio_irq_relres,
1301 .irq_set_affinity = msm_gpio_irq_set_affinity,
1302 .irq_set_vcpu_affinity = msm_gpio_irq_set_vcpu_affinity,
1303 .flags = (IRQCHIP_MASK_ON_SUSPEND |
1304 IRQCHIP_SET_TYPE_MASKED |
1305 IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND |
1309 static int msm_gpio_init(struct msm_pinctrl *pctrl)
1311 struct gpio_chip *chip;
1312 struct gpio_irq_chip *girq;
1314 unsigned gpio, ngpio = pctrl->soc->ngpios;
1315 struct device_node *np;
1318 if (WARN_ON(ngpio > MAX_NR_GPIO))
1321 chip = &pctrl->chip;
1323 chip->ngpio = ngpio;
1324 chip->label = dev_name(pctrl->dev);
1325 chip->parent = pctrl->dev;
1326 chip->owner = THIS_MODULE;
1327 if (msm_gpio_needs_valid_mask(pctrl))
1328 chip->init_valid_mask = msm_gpio_init_valid_mask;
1330 np = of_parse_phandle(pctrl->dev->of_node, "wakeup-parent", 0);
1332 chip->irq.parent_domain = irq_find_matching_host(np,
1335 if (!chip->irq.parent_domain)
1336 return -EPROBE_DEFER;
1337 chip->irq.child_to_parent_hwirq = msm_gpio_wakeirq;
1339 * Let's skip handling the GPIOs, if the parent irqchip
1340 * is handling the direct connect IRQ of the GPIO.
1342 skip = irq_domain_qcom_handle_wakeup(chip->irq.parent_domain);
1343 for (i = 0; skip && i < pctrl->soc->nwakeirq_map; i++) {
1344 gpio = pctrl->soc->wakeirq_map[i].gpio;
1345 set_bit(gpio, pctrl->skip_wake_irqs);
1350 gpio_irq_chip_set_chip(girq, &msm_gpio_irq_chip);
1351 girq->parent_handler = msm_gpio_irq_handler;
1352 girq->fwnode = pctrl->dev->fwnode;
1353 girq->num_parents = 1;
1354 girq->parents = devm_kcalloc(pctrl->dev, 1, sizeof(*girq->parents),
1358 girq->default_type = IRQ_TYPE_NONE;
1359 girq->handler = handle_bad_irq;
1360 girq->parents[0] = pctrl->irq;
1362 ret = gpiochip_add_data(&pctrl->chip, pctrl);
1364 dev_err(pctrl->dev, "Failed register gpiochip\n");
1369 * For DeviceTree-supported systems, the gpio core checks the
1370 * pinctrl's device node for the "gpio-ranges" property.
1371 * If it is present, it takes care of adding the pin ranges
1372 * for the driver. In this case the driver can skip ahead.
1374 * In order to remain compatible with older, existing DeviceTree
1375 * files which don't set the "gpio-ranges" property or systems that
1376 * utilize ACPI the driver has to call gpiochip_add_pin_range().
1378 if (!of_property_read_bool(pctrl->dev->of_node, "gpio-ranges")) {
1379 ret = gpiochip_add_pin_range(&pctrl->chip,
1380 dev_name(pctrl->dev), 0, 0, chip->ngpio);
1382 dev_err(pctrl->dev, "Failed to add pin range\n");
1383 gpiochip_remove(&pctrl->chip);
1391 static int msm_ps_hold_restart(struct notifier_block *nb, unsigned long action,
1394 struct msm_pinctrl *pctrl = container_of(nb, struct msm_pinctrl, restart_nb);
1396 writel(0, pctrl->regs[0] + PS_HOLD_OFFSET);
1401 static struct msm_pinctrl *poweroff_pctrl;
1403 static void msm_ps_hold_poweroff(void)
1405 msm_ps_hold_restart(&poweroff_pctrl->restart_nb, 0, NULL);
1408 static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
1411 const struct msm_function *func = pctrl->soc->functions;
1413 for (i = 0; i < pctrl->soc->nfunctions; i++)
1414 if (!strcmp(func[i].name, "ps_hold")) {
1415 pctrl->restart_nb.notifier_call = msm_ps_hold_restart;
1416 pctrl->restart_nb.priority = 128;
1417 if (register_restart_handler(&pctrl->restart_nb))
1419 "failed to setup restart handler.\n");
1420 poweroff_pctrl = pctrl;
1421 pm_power_off = msm_ps_hold_poweroff;
1426 static __maybe_unused int msm_pinctrl_suspend(struct device *dev)
1428 struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
1430 return pinctrl_force_sleep(pctrl->pctrl);
1433 static __maybe_unused int msm_pinctrl_resume(struct device *dev)
1435 struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
1437 return pinctrl_force_default(pctrl->pctrl);
1440 SIMPLE_DEV_PM_OPS(msm_pinctrl_dev_pm_ops, msm_pinctrl_suspend,
1441 msm_pinctrl_resume);
1443 EXPORT_SYMBOL(msm_pinctrl_dev_pm_ops);
1445 int msm_pinctrl_probe(struct platform_device *pdev,
1446 const struct msm_pinctrl_soc_data *soc_data)
1448 struct msm_pinctrl *pctrl;
1449 struct resource *res;
1453 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1457 pctrl->dev = &pdev->dev;
1458 pctrl->soc = soc_data;
1459 pctrl->chip = msm_gpio_template;
1460 pctrl->intr_target_use_scm = of_device_is_compatible(
1461 pctrl->dev->of_node,
1462 "qcom,ipq8064-pinctrl");
1464 raw_spin_lock_init(&pctrl->lock);
1466 if (soc_data->tiles) {
1467 for (i = 0; i < soc_data->ntiles; i++) {
1468 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1469 soc_data->tiles[i]);
1470 pctrl->regs[i] = devm_ioremap_resource(&pdev->dev, res);
1471 if (IS_ERR(pctrl->regs[i]))
1472 return PTR_ERR(pctrl->regs[i]);
1475 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1476 pctrl->regs[0] = devm_ioremap_resource(&pdev->dev, res);
1477 if (IS_ERR(pctrl->regs[0]))
1478 return PTR_ERR(pctrl->regs[0]);
1480 pctrl->phys_base[0] = res->start;
1483 msm_pinctrl_setup_pm_reset(pctrl);
1485 pctrl->irq = platform_get_irq(pdev, 0);
1489 pctrl->desc.owner = THIS_MODULE;
1490 pctrl->desc.pctlops = &msm_pinctrl_ops;
1491 pctrl->desc.pmxops = &msm_pinmux_ops;
1492 pctrl->desc.confops = &msm_pinconf_ops;
1493 pctrl->desc.name = dev_name(&pdev->dev);
1494 pctrl->desc.pins = pctrl->soc->pins;
1495 pctrl->desc.npins = pctrl->soc->npins;
1497 pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &pctrl->desc, pctrl);
1498 if (IS_ERR(pctrl->pctrl)) {
1499 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
1500 return PTR_ERR(pctrl->pctrl);
1503 ret = msm_gpio_init(pctrl);
1507 platform_set_drvdata(pdev, pctrl);
1509 dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
1513 EXPORT_SYMBOL(msm_pinctrl_probe);
1515 int msm_pinctrl_remove(struct platform_device *pdev)
1517 struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
1519 gpiochip_remove(&pctrl->chip);
1521 unregister_restart_handler(&pctrl->restart_nb);
1525 EXPORT_SYMBOL(msm_pinctrl_remove);
1527 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. TLMM driver");
1528 MODULE_LICENSE("GPL v2");