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>
28 #include <linux/soc/qcom/irq.h>
31 #include "../pinconf.h"
32 #include "pinctrl-msm.h"
33 #include "../pinctrl-utils.h"
35 #define MAX_NR_GPIO 300
36 #define MAX_NR_TILES 4
37 #define PS_HOLD_OFFSET 0x820
40 * struct msm_pinctrl - state for a pinctrl-msm device
41 * @dev: device handle.
42 * @pctrl: pinctrl handle.
43 * @chip: gpiochip handle.
44 * @restart_nb: restart notifier block.
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 * @skip_wake_irqs: Skip IRQs that are handled by wakeup interrupt controller
52 * @soc; Reference to soc_data of platform specific data.
53 * @regs: Base addresses for the TLMM tiles.
57 struct pinctrl_dev
*pctrl
;
58 struct gpio_chip chip
;
59 struct pinctrl_desc desc
;
60 struct notifier_block restart_nb
;
62 struct irq_chip irq_chip
;
65 bool intr_target_use_scm
;
69 DECLARE_BITMAP(dual_edge_irqs
, MAX_NR_GPIO
);
70 DECLARE_BITMAP(enabled_irqs
, MAX_NR_GPIO
);
71 DECLARE_BITMAP(skip_wake_irqs
, MAX_NR_GPIO
);
73 const struct msm_pinctrl_soc_data
*soc
;
74 void __iomem
*regs
[MAX_NR_TILES
];
75 u32 phys_base
[MAX_NR_TILES
];
78 #define MSM_ACCESSOR(name) \
79 static u32 msm_readl_##name(struct msm_pinctrl *pctrl, \
80 const struct msm_pingroup *g) \
82 return readl(pctrl->regs[g->tile] + g->name##_reg); \
84 static void msm_writel_##name(u32 val, struct msm_pinctrl *pctrl, \
85 const struct msm_pingroup *g) \
87 writel(val, pctrl->regs[g->tile] + g->name##_reg); \
92 MSM_ACCESSOR(intr_cfg
)
93 MSM_ACCESSOR(intr_status
)
94 MSM_ACCESSOR(intr_target
)
96 static int msm_get_groups_count(struct pinctrl_dev
*pctldev
)
98 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
100 return pctrl
->soc
->ngroups
;
103 static const char *msm_get_group_name(struct pinctrl_dev
*pctldev
,
106 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
108 return pctrl
->soc
->groups
[group
].name
;
111 static int msm_get_group_pins(struct pinctrl_dev
*pctldev
,
113 const unsigned **pins
,
116 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
118 *pins
= pctrl
->soc
->groups
[group
].pins
;
119 *num_pins
= pctrl
->soc
->groups
[group
].npins
;
123 static const struct pinctrl_ops msm_pinctrl_ops
= {
124 .get_groups_count
= msm_get_groups_count
,
125 .get_group_name
= msm_get_group_name
,
126 .get_group_pins
= msm_get_group_pins
,
127 .dt_node_to_map
= pinconf_generic_dt_node_to_map_group
,
128 .dt_free_map
= pinctrl_utils_free_map
,
131 static int msm_pinmux_request(struct pinctrl_dev
*pctldev
, unsigned offset
)
133 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
134 struct gpio_chip
*chip
= &pctrl
->chip
;
136 return gpiochip_line_is_valid(chip
, offset
) ? 0 : -EINVAL
;
139 static int msm_get_functions_count(struct pinctrl_dev
*pctldev
)
141 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
143 return pctrl
->soc
->nfunctions
;
146 static const char *msm_get_function_name(struct pinctrl_dev
*pctldev
,
149 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
151 return pctrl
->soc
->functions
[function
].name
;
154 static int msm_get_function_groups(struct pinctrl_dev
*pctldev
,
156 const char * const **groups
,
157 unsigned * const num_groups
)
159 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
161 *groups
= pctrl
->soc
->functions
[function
].groups
;
162 *num_groups
= pctrl
->soc
->functions
[function
].ngroups
;
166 static int msm_pinmux_set_mux(struct pinctrl_dev
*pctldev
,
170 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
171 const struct msm_pingroup
*g
;
176 g
= &pctrl
->soc
->groups
[group
];
177 mask
= GENMASK(g
->mux_bit
+ order_base_2(g
->nfuncs
) - 1, g
->mux_bit
);
179 for (i
= 0; i
< g
->nfuncs
; i
++) {
180 if (g
->funcs
[i
] == function
)
184 if (WARN_ON(i
== g
->nfuncs
))
187 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
189 val
= msm_readl_ctl(pctrl
, g
);
191 val
|= i
<< g
->mux_bit
;
192 msm_writel_ctl(val
, pctrl
, g
);
194 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
199 static int msm_pinmux_request_gpio(struct pinctrl_dev
*pctldev
,
200 struct pinctrl_gpio_range
*range
,
203 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
204 const struct msm_pingroup
*g
= &pctrl
->soc
->groups
[offset
];
206 /* No funcs? Probably ACPI so can't do anything here */
210 /* For now assume function 0 is GPIO because it always is */
211 return msm_pinmux_set_mux(pctldev
, g
->funcs
[0], offset
);
214 static const struct pinmux_ops msm_pinmux_ops
= {
215 .request
= msm_pinmux_request
,
216 .get_functions_count
= msm_get_functions_count
,
217 .get_function_name
= msm_get_function_name
,
218 .get_function_groups
= msm_get_function_groups
,
219 .gpio_request_enable
= msm_pinmux_request_gpio
,
220 .set_mux
= msm_pinmux_set_mux
,
223 static int msm_config_reg(struct msm_pinctrl
*pctrl
,
224 const struct msm_pingroup
*g
,
230 case PIN_CONFIG_BIAS_DISABLE
:
231 case PIN_CONFIG_BIAS_PULL_DOWN
:
232 case PIN_CONFIG_BIAS_BUS_HOLD
:
233 case PIN_CONFIG_BIAS_PULL_UP
:
237 case PIN_CONFIG_DRIVE_STRENGTH
:
241 case PIN_CONFIG_OUTPUT
:
242 case PIN_CONFIG_INPUT_ENABLE
:
253 #define MSM_NO_PULL 0
254 #define MSM_PULL_DOWN 1
256 #define MSM_PULL_UP_NO_KEEPER 2
257 #define MSM_PULL_UP 3
259 static unsigned msm_regval_to_drive(u32 val
)
261 return (val
+ 1) * 2;
264 static int msm_config_group_get(struct pinctrl_dev
*pctldev
,
266 unsigned long *config
)
268 const struct msm_pingroup
*g
;
269 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
270 unsigned param
= pinconf_to_config_param(*config
);
277 g
= &pctrl
->soc
->groups
[group
];
279 ret
= msm_config_reg(pctrl
, g
, param
, &mask
, &bit
);
283 val
= msm_readl_ctl(pctrl
, g
);
284 arg
= (val
>> bit
) & mask
;
286 /* Convert register value to pinconf value */
288 case PIN_CONFIG_BIAS_DISABLE
:
289 if (arg
!= MSM_NO_PULL
)
293 case PIN_CONFIG_BIAS_PULL_DOWN
:
294 if (arg
!= MSM_PULL_DOWN
)
298 case PIN_CONFIG_BIAS_BUS_HOLD
:
299 if (pctrl
->soc
->pull_no_keeper
)
302 if (arg
!= MSM_KEEPER
)
306 case PIN_CONFIG_BIAS_PULL_UP
:
307 if (pctrl
->soc
->pull_no_keeper
)
308 arg
= arg
== MSM_PULL_UP_NO_KEEPER
;
310 arg
= arg
== MSM_PULL_UP
;
314 case PIN_CONFIG_DRIVE_STRENGTH
:
315 arg
= msm_regval_to_drive(arg
);
317 case PIN_CONFIG_OUTPUT
:
318 /* Pin is not output */
322 val
= msm_readl_io(pctrl
, g
);
323 arg
= !!(val
& BIT(g
->in_bit
));
325 case PIN_CONFIG_INPUT_ENABLE
:
335 *config
= pinconf_to_config_packed(param
, arg
);
340 static int msm_config_group_set(struct pinctrl_dev
*pctldev
,
342 unsigned long *configs
,
343 unsigned num_configs
)
345 const struct msm_pingroup
*g
;
346 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
356 g
= &pctrl
->soc
->groups
[group
];
358 for (i
= 0; i
< num_configs
; i
++) {
359 param
= pinconf_to_config_param(configs
[i
]);
360 arg
= pinconf_to_config_argument(configs
[i
]);
362 ret
= msm_config_reg(pctrl
, g
, param
, &mask
, &bit
);
366 /* Convert pinconf values to register values */
368 case PIN_CONFIG_BIAS_DISABLE
:
371 case PIN_CONFIG_BIAS_PULL_DOWN
:
374 case PIN_CONFIG_BIAS_BUS_HOLD
:
375 if (pctrl
->soc
->pull_no_keeper
)
380 case PIN_CONFIG_BIAS_PULL_UP
:
381 if (pctrl
->soc
->pull_no_keeper
)
382 arg
= MSM_PULL_UP_NO_KEEPER
;
386 case PIN_CONFIG_DRIVE_STRENGTH
:
387 /* Check for invalid values */
388 if (arg
> 16 || arg
< 2 || (arg
% 2) != 0)
393 case PIN_CONFIG_OUTPUT
:
394 /* set output value */
395 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
396 val
= msm_readl_io(pctrl
, g
);
398 val
|= BIT(g
->out_bit
);
400 val
&= ~BIT(g
->out_bit
);
401 msm_writel_io(val
, pctrl
, g
);
402 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
407 case PIN_CONFIG_INPUT_ENABLE
:
412 dev_err(pctrl
->dev
, "Unsupported config parameter: %x\n",
417 /* Range-check user-supplied value */
419 dev_err(pctrl
->dev
, "config %x: %x is invalid\n", param
, arg
);
423 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
424 val
= msm_readl_ctl(pctrl
, g
);
425 val
&= ~(mask
<< bit
);
427 msm_writel_ctl(val
, pctrl
, g
);
428 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
434 static const struct pinconf_ops msm_pinconf_ops
= {
436 .pin_config_group_get
= msm_config_group_get
,
437 .pin_config_group_set
= msm_config_group_set
,
440 static int msm_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
442 const struct msm_pingroup
*g
;
443 struct msm_pinctrl
*pctrl
= gpiochip_get_data(chip
);
447 g
= &pctrl
->soc
->groups
[offset
];
449 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
451 val
= msm_readl_ctl(pctrl
, g
);
452 val
&= ~BIT(g
->oe_bit
);
453 msm_writel_ctl(val
, pctrl
, g
);
455 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
460 static int msm_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
, int value
)
462 const struct msm_pingroup
*g
;
463 struct msm_pinctrl
*pctrl
= gpiochip_get_data(chip
);
467 g
= &pctrl
->soc
->groups
[offset
];
469 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
471 val
= msm_readl_io(pctrl
, g
);
473 val
|= BIT(g
->out_bit
);
475 val
&= ~BIT(g
->out_bit
);
476 msm_writel_io(val
, pctrl
, g
);
478 val
= msm_readl_ctl(pctrl
, g
);
479 val
|= BIT(g
->oe_bit
);
480 msm_writel_ctl(val
, pctrl
, g
);
482 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
487 static int msm_gpio_get_direction(struct gpio_chip
*chip
, unsigned int offset
)
489 struct msm_pinctrl
*pctrl
= gpiochip_get_data(chip
);
490 const struct msm_pingroup
*g
;
493 g
= &pctrl
->soc
->groups
[offset
];
495 val
= msm_readl_ctl(pctrl
, g
);
497 return val
& BIT(g
->oe_bit
) ? GPIO_LINE_DIRECTION_OUT
:
498 GPIO_LINE_DIRECTION_IN
;
501 static int msm_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
503 const struct msm_pingroup
*g
;
504 struct msm_pinctrl
*pctrl
= gpiochip_get_data(chip
);
507 g
= &pctrl
->soc
->groups
[offset
];
509 val
= msm_readl_io(pctrl
, g
);
510 return !!(val
& BIT(g
->in_bit
));
513 static void msm_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
515 const struct msm_pingroup
*g
;
516 struct msm_pinctrl
*pctrl
= gpiochip_get_data(chip
);
520 g
= &pctrl
->soc
->groups
[offset
];
522 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
524 val
= msm_readl_io(pctrl
, g
);
526 val
|= BIT(g
->out_bit
);
528 val
&= ~BIT(g
->out_bit
);
529 msm_writel_io(val
, pctrl
, g
);
531 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
534 #ifdef CONFIG_DEBUG_FS
535 #include <linux/seq_file.h>
537 static void msm_gpio_dbg_show_one(struct seq_file
*s
,
538 struct pinctrl_dev
*pctldev
,
539 struct gpio_chip
*chip
,
543 const struct msm_pingroup
*g
;
544 struct msm_pinctrl
*pctrl
= gpiochip_get_data(chip
);
552 static const char * const pulls_keeper
[] = {
559 static const char * const pulls_no_keeper
[] = {
565 if (!gpiochip_line_is_valid(chip
, offset
))
568 g
= &pctrl
->soc
->groups
[offset
];
569 ctl_reg
= msm_readl_ctl(pctrl
, g
);
570 io_reg
= msm_readl_io(pctrl
, g
);
572 is_out
= !!(ctl_reg
& BIT(g
->oe_bit
));
573 func
= (ctl_reg
>> g
->mux_bit
) & 7;
574 drive
= (ctl_reg
>> g
->drv_bit
) & 7;
575 pull
= (ctl_reg
>> g
->pull_bit
) & 3;
578 val
= !!(io_reg
& BIT(g
->out_bit
));
580 val
= !!(io_reg
& BIT(g
->in_bit
));
582 seq_printf(s
, " %-8s: %-3s", g
->name
, is_out
? "out" : "in");
583 seq_printf(s
, " %-4s func%d", val
? "high" : "low", func
);
584 seq_printf(s
, " %dmA", msm_regval_to_drive(drive
));
585 if (pctrl
->soc
->pull_no_keeper
)
586 seq_printf(s
, " %s", pulls_no_keeper
[pull
]);
588 seq_printf(s
, " %s", pulls_keeper
[pull
]);
592 static void msm_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
594 unsigned gpio
= chip
->base
;
597 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++)
598 msm_gpio_dbg_show_one(s
, NULL
, chip
, i
, gpio
);
602 #define msm_gpio_dbg_show NULL
605 static int msm_gpio_init_valid_mask(struct gpio_chip
*gc
,
606 unsigned long *valid_mask
,
609 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
612 const int *reserved
= pctrl
->soc
->reserved_gpios
;
615 /* Driver provided reserved list overrides DT and ACPI */
617 bitmap_fill(valid_mask
, ngpios
);
618 for (i
= 0; reserved
[i
] >= 0; i
++) {
619 if (i
>= ngpios
|| reserved
[i
] >= ngpios
) {
620 dev_err(pctrl
->dev
, "invalid list of reserved GPIOs\n");
623 clear_bit(reserved
[i
], valid_mask
);
629 /* The number of GPIOs in the ACPI tables */
630 len
= ret
= device_property_count_u16(pctrl
->dev
, "gpios");
637 tmp
= kmalloc_array(len
, sizeof(*tmp
), GFP_KERNEL
);
641 ret
= device_property_read_u16_array(pctrl
->dev
, "gpios", tmp
, len
);
643 dev_err(pctrl
->dev
, "could not read list of GPIOs\n");
647 bitmap_zero(valid_mask
, ngpios
);
648 for (i
= 0; i
< len
; i
++)
649 set_bit(tmp
[i
], valid_mask
);
656 static const struct gpio_chip msm_gpio_template
= {
657 .direction_input
= msm_gpio_direction_input
,
658 .direction_output
= msm_gpio_direction_output
,
659 .get_direction
= msm_gpio_get_direction
,
662 .request
= gpiochip_generic_request
,
663 .free
= gpiochip_generic_free
,
664 .dbg_show
= msm_gpio_dbg_show
,
667 /* For dual-edge interrupts in software, since some hardware has no
670 * At appropriate moments, this function may be called to flip the polarity
671 * settings of both-edge irq lines to try and catch the next edge.
673 * The attempt is considered successful if:
674 * - the status bit goes high, indicating that an edge was caught, or
675 * - the input value of the gpio doesn't change during the attempt.
676 * If the value changes twice during the process, that would cause the first
677 * test to fail but would force the second, as two opposite
678 * transitions would cause a detection no matter the polarity setting.
680 * The do-loop tries to sledge-hammer closed the timing hole between
681 * the initial value-read and the polarity-write - if the line value changes
682 * during that window, an interrupt is lost, the new polarity setting is
683 * incorrect, and the first success test will fail, causing a retry.
685 * Algorithm comes from Google's msmgpio driver.
687 static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl
*pctrl
,
688 const struct msm_pingroup
*g
,
691 int loop_limit
= 100;
692 unsigned val
, val2
, intstat
;
696 val
= msm_readl_io(pctrl
, g
) & BIT(g
->in_bit
);
698 pol
= msm_readl_intr_cfg(pctrl
, g
);
699 pol
^= BIT(g
->intr_polarity_bit
);
700 msm_writel_intr_cfg(val
, pctrl
, g
);
702 val2
= msm_readl_io(pctrl
, g
) & BIT(g
->in_bit
);
703 intstat
= msm_readl_intr_status(pctrl
, g
);
704 if (intstat
|| (val
== val2
))
706 } while (loop_limit
-- > 0);
707 dev_err(pctrl
->dev
, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
711 static void msm_gpio_irq_mask(struct irq_data
*d
)
713 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
714 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
715 const struct msm_pingroup
*g
;
720 irq_chip_mask_parent(d
);
722 if (test_bit(d
->hwirq
, pctrl
->skip_wake_irqs
))
725 g
= &pctrl
->soc
->groups
[d
->hwirq
];
727 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
729 val
= msm_readl_intr_cfg(pctrl
, g
);
731 * There are two bits that control interrupt forwarding to the CPU. The
732 * RAW_STATUS_EN bit causes the level or edge sensed on the line to be
733 * latched into the interrupt status register when the hardware detects
734 * an irq that it's configured for (either edge for edge type or level
735 * for level type irq). The 'non-raw' status enable bit causes the
736 * hardware to assert the summary interrupt to the CPU if the latched
737 * status bit is set. There's a bug though, the edge detection logic
738 * seems to have a problem where toggling the RAW_STATUS_EN bit may
739 * cause the status bit to latch spuriously when there isn't any edge
740 * so we can't touch that bit for edge type irqs and we have to keep
741 * the bit set anyway so that edges are latched while the line is masked.
743 * To make matters more complicated, leaving the RAW_STATUS_EN bit
744 * enabled all the time causes level interrupts to re-latch into the
745 * status register because the level is still present on the line after
746 * we ack it. We clear the raw status enable bit during mask here and
747 * set the bit on unmask so the interrupt can't latch into the hardware
750 if (irqd_get_trigger_type(d
) & IRQ_TYPE_LEVEL_MASK
)
751 val
&= ~BIT(g
->intr_raw_status_bit
);
753 val
&= ~BIT(g
->intr_enable_bit
);
754 msm_writel_intr_cfg(val
, pctrl
, g
);
756 clear_bit(d
->hwirq
, pctrl
->enabled_irqs
);
758 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
761 static void msm_gpio_irq_clear_unmask(struct irq_data
*d
, bool status_clear
)
763 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
764 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
765 const struct msm_pingroup
*g
;
770 irq_chip_unmask_parent(d
);
772 if (test_bit(d
->hwirq
, pctrl
->skip_wake_irqs
))
775 g
= &pctrl
->soc
->groups
[d
->hwirq
];
777 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
781 * clear the interrupt status bit before unmask to avoid
782 * any erroneous interrupts that would have got latched
783 * when the interrupt is not in use.
785 val
= msm_readl_intr_status(pctrl
, g
);
786 val
&= ~BIT(g
->intr_status_bit
);
787 msm_writel_intr_status(val
, pctrl
, g
);
790 val
= msm_readl_intr_cfg(pctrl
, g
);
791 val
|= BIT(g
->intr_raw_status_bit
);
792 val
|= BIT(g
->intr_enable_bit
);
793 msm_writel_intr_cfg(val
, pctrl
, g
);
795 set_bit(d
->hwirq
, pctrl
->enabled_irqs
);
797 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
800 static void msm_gpio_irq_enable(struct irq_data
*d
)
803 * Clear the interrupt that may be pending before we enable
805 * This is especially a problem with the GPIOs routed to the
806 * PDC. These GPIOs are direct-connect interrupts to the GIC.
807 * Disabling the interrupt line at the PDC does not prevent
808 * the interrupt from being latched at the GIC. The state at
809 * GIC needs to be cleared before enabling.
811 if (d
->parent_data
) {
812 irq_chip_set_parent_state(d
, IRQCHIP_STATE_PENDING
, 0);
813 irq_chip_enable_parent(d
);
816 msm_gpio_irq_clear_unmask(d
, true);
819 static void msm_gpio_irq_disable(struct irq_data
*d
)
821 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
822 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
825 irq_chip_disable_parent(d
);
827 if (!test_bit(d
->hwirq
, pctrl
->skip_wake_irqs
))
828 msm_gpio_irq_mask(d
);
831 static void msm_gpio_irq_unmask(struct irq_data
*d
)
833 msm_gpio_irq_clear_unmask(d
, false);
836 static void msm_gpio_irq_ack(struct irq_data
*d
)
838 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
839 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
840 const struct msm_pingroup
*g
;
844 if (test_bit(d
->hwirq
, pctrl
->skip_wake_irqs
))
847 g
= &pctrl
->soc
->groups
[d
->hwirq
];
849 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
851 val
= msm_readl_intr_status(pctrl
, g
);
852 if (g
->intr_ack_high
)
853 val
|= BIT(g
->intr_status_bit
);
855 val
&= ~BIT(g
->intr_status_bit
);
856 msm_writel_intr_status(val
, pctrl
, g
);
858 if (test_bit(d
->hwirq
, pctrl
->dual_edge_irqs
))
859 msm_gpio_update_dual_edge_pos(pctrl
, g
, d
);
861 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
864 static int msm_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
866 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
867 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
868 const struct msm_pingroup
*g
;
873 irq_chip_set_type_parent(d
, type
);
875 if (test_bit(d
->hwirq
, pctrl
->skip_wake_irqs
))
878 g
= &pctrl
->soc
->groups
[d
->hwirq
];
880 raw_spin_lock_irqsave(&pctrl
->lock
, flags
);
883 * For hw without possibility of detecting both edges
885 if (g
->intr_detection_width
== 1 && type
== IRQ_TYPE_EDGE_BOTH
)
886 set_bit(d
->hwirq
, pctrl
->dual_edge_irqs
);
888 clear_bit(d
->hwirq
, pctrl
->dual_edge_irqs
);
890 /* Route interrupts to application cpu.
891 * With intr_target_use_scm interrupts are routed to
892 * application cpu using scm calls.
894 if (pctrl
->intr_target_use_scm
) {
895 u32 addr
= pctrl
->phys_base
[0] + g
->intr_target_reg
;
898 qcom_scm_io_readl(addr
, &val
);
900 val
&= ~(7 << g
->intr_target_bit
);
901 val
|= g
->intr_target_kpss_val
<< g
->intr_target_bit
;
903 ret
= qcom_scm_io_writel(addr
, val
);
906 "Failed routing %lu interrupt to Apps proc",
909 val
= msm_readl_intr_target(pctrl
, g
);
910 val
&= ~(7 << g
->intr_target_bit
);
911 val
|= g
->intr_target_kpss_val
<< g
->intr_target_bit
;
912 msm_writel_intr_target(val
, pctrl
, g
);
915 /* Update configuration for gpio.
916 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
917 * internal circuitry of TLMM, toggling the RAW_STATUS
918 * could cause the INTR_STATUS to be set for EDGE interrupts.
920 val
= msm_readl_intr_cfg(pctrl
, g
);
921 val
|= BIT(g
->intr_raw_status_bit
);
922 if (g
->intr_detection_width
== 2) {
923 val
&= ~(3 << g
->intr_detection_bit
);
924 val
&= ~(1 << g
->intr_polarity_bit
);
926 case IRQ_TYPE_EDGE_RISING
:
927 val
|= 1 << g
->intr_detection_bit
;
928 val
|= BIT(g
->intr_polarity_bit
);
930 case IRQ_TYPE_EDGE_FALLING
:
931 val
|= 2 << g
->intr_detection_bit
;
932 val
|= BIT(g
->intr_polarity_bit
);
934 case IRQ_TYPE_EDGE_BOTH
:
935 val
|= 3 << g
->intr_detection_bit
;
936 val
|= BIT(g
->intr_polarity_bit
);
938 case IRQ_TYPE_LEVEL_LOW
:
940 case IRQ_TYPE_LEVEL_HIGH
:
941 val
|= BIT(g
->intr_polarity_bit
);
944 } else if (g
->intr_detection_width
== 1) {
945 val
&= ~(1 << g
->intr_detection_bit
);
946 val
&= ~(1 << g
->intr_polarity_bit
);
948 case IRQ_TYPE_EDGE_RISING
:
949 val
|= BIT(g
->intr_detection_bit
);
950 val
|= BIT(g
->intr_polarity_bit
);
952 case IRQ_TYPE_EDGE_FALLING
:
953 val
|= BIT(g
->intr_detection_bit
);
955 case IRQ_TYPE_EDGE_BOTH
:
956 val
|= BIT(g
->intr_detection_bit
);
957 val
|= BIT(g
->intr_polarity_bit
);
959 case IRQ_TYPE_LEVEL_LOW
:
961 case IRQ_TYPE_LEVEL_HIGH
:
962 val
|= BIT(g
->intr_polarity_bit
);
968 msm_writel_intr_cfg(val
, pctrl
, g
);
970 if (test_bit(d
->hwirq
, pctrl
->dual_edge_irqs
))
971 msm_gpio_update_dual_edge_pos(pctrl
, g
, d
);
973 raw_spin_unlock_irqrestore(&pctrl
->lock
, flags
);
975 if (type
& (IRQ_TYPE_LEVEL_LOW
| IRQ_TYPE_LEVEL_HIGH
))
976 irq_set_handler_locked(d
, handle_level_irq
);
977 else if (type
& (IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
))
978 irq_set_handler_locked(d
, handle_edge_irq
);
983 static int msm_gpio_irq_set_wake(struct irq_data
*d
, unsigned int on
)
985 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
986 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
989 * While they may not wake up when the TLMM is powered off,
990 * some GPIOs would like to wakeup the system from suspend
991 * when TLMM is powered on. To allow that, enable the GPIO
992 * summary line to be wakeup capable at GIC.
995 irq_chip_set_wake_parent(d
, on
);
997 irq_set_irq_wake(pctrl
->irq
, on
);
1002 static int msm_gpio_irq_reqres(struct irq_data
*d
)
1004 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1005 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
1008 if (!try_module_get(gc
->owner
))
1011 ret
= msm_pinmux_request_gpio(pctrl
->pctrl
, NULL
, d
->hwirq
);
1014 msm_gpio_direction_input(gc
, d
->hwirq
);
1016 if (gpiochip_lock_as_irq(gc
, d
->hwirq
)) {
1018 "unable to lock HW IRQ %lu for IRQ\n",
1025 module_put(gc
->owner
);
1029 static void msm_gpio_irq_relres(struct irq_data
*d
)
1031 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
1033 gpiochip_unlock_as_irq(gc
, d
->hwirq
);
1034 module_put(gc
->owner
);
1037 static void msm_gpio_irq_handler(struct irq_desc
*desc
)
1039 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
1040 const struct msm_pingroup
*g
;
1041 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
1042 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
1048 chained_irq_enter(chip
, desc
);
1051 * Each pin has it's own IRQ status register, so use
1052 * enabled_irq bitmap to limit the number of reads.
1054 for_each_set_bit(i
, pctrl
->enabled_irqs
, pctrl
->chip
.ngpio
) {
1055 g
= &pctrl
->soc
->groups
[i
];
1056 val
= msm_readl_intr_status(pctrl
, g
);
1057 if (val
& BIT(g
->intr_status_bit
)) {
1058 irq_pin
= irq_find_mapping(gc
->irq
.domain
, i
);
1059 generic_handle_irq(irq_pin
);
1064 /* No interrupts were flagged */
1066 handle_bad_irq(desc
);
1068 chained_irq_exit(chip
, desc
);
1071 static int msm_gpio_wakeirq(struct gpio_chip
*gc
,
1073 unsigned int child_type
,
1074 unsigned int *parent
,
1075 unsigned int *parent_type
)
1077 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
1078 const struct msm_gpio_wakeirq_map
*map
;
1081 *parent
= GPIO_NO_WAKE_IRQ
;
1082 *parent_type
= IRQ_TYPE_EDGE_RISING
;
1084 for (i
= 0; i
< pctrl
->soc
->nwakeirq_map
; i
++) {
1085 map
= &pctrl
->soc
->wakeirq_map
[i
];
1086 if (map
->gpio
== child
) {
1087 *parent
= map
->wakeirq
;
1095 static bool msm_gpio_needs_valid_mask(struct msm_pinctrl
*pctrl
)
1097 if (pctrl
->soc
->reserved_gpios
)
1100 return device_property_count_u16(pctrl
->dev
, "gpios") > 0;
1103 static int msm_gpio_init(struct msm_pinctrl
*pctrl
)
1105 struct gpio_chip
*chip
;
1106 struct gpio_irq_chip
*girq
;
1108 unsigned gpio
, ngpio
= pctrl
->soc
->ngpios
;
1109 struct device_node
*np
;
1112 if (WARN_ON(ngpio
> MAX_NR_GPIO
))
1115 chip
= &pctrl
->chip
;
1117 chip
->ngpio
= ngpio
;
1118 chip
->label
= dev_name(pctrl
->dev
);
1119 chip
->parent
= pctrl
->dev
;
1120 chip
->owner
= THIS_MODULE
;
1121 chip
->of_node
= pctrl
->dev
->of_node
;
1122 if (msm_gpio_needs_valid_mask(pctrl
))
1123 chip
->init_valid_mask
= msm_gpio_init_valid_mask
;
1125 pctrl
->irq_chip
.name
= "msmgpio";
1126 pctrl
->irq_chip
.irq_enable
= msm_gpio_irq_enable
;
1127 pctrl
->irq_chip
.irq_disable
= msm_gpio_irq_disable
;
1128 pctrl
->irq_chip
.irq_mask
= msm_gpio_irq_mask
;
1129 pctrl
->irq_chip
.irq_unmask
= msm_gpio_irq_unmask
;
1130 pctrl
->irq_chip
.irq_ack
= msm_gpio_irq_ack
;
1131 pctrl
->irq_chip
.irq_set_type
= msm_gpio_irq_set_type
;
1132 pctrl
->irq_chip
.irq_set_wake
= msm_gpio_irq_set_wake
;
1133 pctrl
->irq_chip
.irq_request_resources
= msm_gpio_irq_reqres
;
1134 pctrl
->irq_chip
.irq_release_resources
= msm_gpio_irq_relres
;
1136 np
= of_parse_phandle(pctrl
->dev
->of_node
, "wakeup-parent", 0);
1138 chip
->irq
.parent_domain
= irq_find_matching_host(np
,
1141 if (!chip
->irq
.parent_domain
)
1142 return -EPROBE_DEFER
;
1143 chip
->irq
.child_to_parent_hwirq
= msm_gpio_wakeirq
;
1144 pctrl
->irq_chip
.irq_eoi
= irq_chip_eoi_parent
;
1146 * Let's skip handling the GPIOs, if the parent irqchip
1147 * is handling the direct connect IRQ of the GPIO.
1149 skip
= irq_domain_qcom_handle_wakeup(chip
->irq
.parent_domain
);
1150 for (i
= 0; skip
&& i
< pctrl
->soc
->nwakeirq_map
; i
++) {
1151 gpio
= pctrl
->soc
->wakeirq_map
[i
].gpio
;
1152 set_bit(gpio
, pctrl
->skip_wake_irqs
);
1157 girq
->chip
= &pctrl
->irq_chip
;
1158 girq
->parent_handler
= msm_gpio_irq_handler
;
1159 girq
->fwnode
= pctrl
->dev
->fwnode
;
1160 girq
->num_parents
= 1;
1161 girq
->parents
= devm_kcalloc(pctrl
->dev
, 1, sizeof(*girq
->parents
),
1165 girq
->default_type
= IRQ_TYPE_NONE
;
1166 girq
->handler
= handle_bad_irq
;
1167 girq
->parents
[0] = pctrl
->irq
;
1169 ret
= gpiochip_add_data(&pctrl
->chip
, pctrl
);
1171 dev_err(pctrl
->dev
, "Failed register gpiochip\n");
1176 * For DeviceTree-supported systems, the gpio core checks the
1177 * pinctrl's device node for the "gpio-ranges" property.
1178 * If it is present, it takes care of adding the pin ranges
1179 * for the driver. In this case the driver can skip ahead.
1181 * In order to remain compatible with older, existing DeviceTree
1182 * files which don't set the "gpio-ranges" property or systems that
1183 * utilize ACPI the driver has to call gpiochip_add_pin_range().
1185 if (!of_property_read_bool(pctrl
->dev
->of_node
, "gpio-ranges")) {
1186 ret
= gpiochip_add_pin_range(&pctrl
->chip
,
1187 dev_name(pctrl
->dev
), 0, 0, chip
->ngpio
);
1189 dev_err(pctrl
->dev
, "Failed to add pin range\n");
1190 gpiochip_remove(&pctrl
->chip
);
1198 static int msm_ps_hold_restart(struct notifier_block
*nb
, unsigned long action
,
1201 struct msm_pinctrl
*pctrl
= container_of(nb
, struct msm_pinctrl
, restart_nb
);
1203 writel(0, pctrl
->regs
[0] + PS_HOLD_OFFSET
);
1208 static struct msm_pinctrl
*poweroff_pctrl
;
1210 static void msm_ps_hold_poweroff(void)
1212 msm_ps_hold_restart(&poweroff_pctrl
->restart_nb
, 0, NULL
);
1215 static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl
*pctrl
)
1218 const struct msm_function
*func
= pctrl
->soc
->functions
;
1220 for (i
= 0; i
< pctrl
->soc
->nfunctions
; i
++)
1221 if (!strcmp(func
[i
].name
, "ps_hold")) {
1222 pctrl
->restart_nb
.notifier_call
= msm_ps_hold_restart
;
1223 pctrl
->restart_nb
.priority
= 128;
1224 if (register_restart_handler(&pctrl
->restart_nb
))
1226 "failed to setup restart handler.\n");
1227 poweroff_pctrl
= pctrl
;
1228 pm_power_off
= msm_ps_hold_poweroff
;
1233 static __maybe_unused
int msm_pinctrl_suspend(struct device
*dev
)
1235 struct msm_pinctrl
*pctrl
= dev_get_drvdata(dev
);
1237 return pinctrl_force_sleep(pctrl
->pctrl
);
1240 static __maybe_unused
int msm_pinctrl_resume(struct device
*dev
)
1242 struct msm_pinctrl
*pctrl
= dev_get_drvdata(dev
);
1244 return pinctrl_force_default(pctrl
->pctrl
);
1247 SIMPLE_DEV_PM_OPS(msm_pinctrl_dev_pm_ops
, msm_pinctrl_suspend
,
1248 msm_pinctrl_resume
);
1250 EXPORT_SYMBOL(msm_pinctrl_dev_pm_ops
);
1252 int msm_pinctrl_probe(struct platform_device
*pdev
,
1253 const struct msm_pinctrl_soc_data
*soc_data
)
1255 struct msm_pinctrl
*pctrl
;
1256 struct resource
*res
;
1260 pctrl
= devm_kzalloc(&pdev
->dev
, sizeof(*pctrl
), GFP_KERNEL
);
1264 pctrl
->dev
= &pdev
->dev
;
1265 pctrl
->soc
= soc_data
;
1266 pctrl
->chip
= msm_gpio_template
;
1267 pctrl
->intr_target_use_scm
= of_device_is_compatible(
1268 pctrl
->dev
->of_node
,
1269 "qcom,ipq8064-pinctrl");
1271 raw_spin_lock_init(&pctrl
->lock
);
1273 if (soc_data
->tiles
) {
1274 for (i
= 0; i
< soc_data
->ntiles
; i
++) {
1275 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
1276 soc_data
->tiles
[i
]);
1277 pctrl
->regs
[i
] = devm_ioremap_resource(&pdev
->dev
, res
);
1278 if (IS_ERR(pctrl
->regs
[i
]))
1279 return PTR_ERR(pctrl
->regs
[i
]);
1282 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1283 pctrl
->regs
[0] = devm_ioremap_resource(&pdev
->dev
, res
);
1284 if (IS_ERR(pctrl
->regs
[0]))
1285 return PTR_ERR(pctrl
->regs
[0]);
1287 pctrl
->phys_base
[0] = res
->start
;
1290 msm_pinctrl_setup_pm_reset(pctrl
);
1292 pctrl
->irq
= platform_get_irq(pdev
, 0);
1296 pctrl
->desc
.owner
= THIS_MODULE
;
1297 pctrl
->desc
.pctlops
= &msm_pinctrl_ops
;
1298 pctrl
->desc
.pmxops
= &msm_pinmux_ops
;
1299 pctrl
->desc
.confops
= &msm_pinconf_ops
;
1300 pctrl
->desc
.name
= dev_name(&pdev
->dev
);
1301 pctrl
->desc
.pins
= pctrl
->soc
->pins
;
1302 pctrl
->desc
.npins
= pctrl
->soc
->npins
;
1304 pctrl
->pctrl
= devm_pinctrl_register(&pdev
->dev
, &pctrl
->desc
, pctrl
);
1305 if (IS_ERR(pctrl
->pctrl
)) {
1306 dev_err(&pdev
->dev
, "Couldn't register pinctrl driver\n");
1307 return PTR_ERR(pctrl
->pctrl
);
1310 ret
= msm_gpio_init(pctrl
);
1314 platform_set_drvdata(pdev
, pctrl
);
1316 dev_dbg(&pdev
->dev
, "Probed Qualcomm pinctrl driver\n");
1320 EXPORT_SYMBOL(msm_pinctrl_probe
);
1322 int msm_pinctrl_remove(struct platform_device
*pdev
)
1324 struct msm_pinctrl
*pctrl
= platform_get_drvdata(pdev
);
1326 gpiochip_remove(&pctrl
->chip
);
1328 unregister_restart_handler(&pctrl
->restart_nb
);
1332 EXPORT_SYMBOL(msm_pinctrl_remove
);