2 * Copyright (c) 2013, Sony Mobile Communications AB.
3 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 and
7 * only version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/delay.h>
16 #include <linux/err.h>
18 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/pinctrl/machine.h>
22 #include <linux/pinctrl/pinctrl.h>
23 #include <linux/pinctrl/pinmux.h>
24 #include <linux/pinctrl/pinconf.h>
25 #include <linux/pinctrl/pinconf-generic.h>
26 #include <linux/slab.h>
27 #include <linux/gpio.h>
28 #include <linux/interrupt.h>
29 #include <linux/spinlock.h>
30 #include <linux/reboot.h>
34 #include "../pinconf.h"
35 #include "pinctrl-msm.h"
36 #include "../pinctrl-utils.h"
38 #define MAX_NR_GPIO 300
39 #define PS_HOLD_OFFSET 0x820
42 * struct msm_pinctrl - state for a pinctrl-msm device
43 * @dev: device handle.
44 * @pctrl: pinctrl handle.
45 * @chip: gpiochip handle.
46 * @restart_nb: restart notifier block.
47 * @irq: parent irq for the TLMM irq_chip.
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 * @soc; Reference to soc_data of platform specific data.
54 * @regs: Base address for the TLMM register map.
58 struct pinctrl_dev
*pctrl
;
59 struct gpio_chip chip
;
60 struct notifier_block restart_nb
;
65 DECLARE_BITMAP(dual_edge_irqs
, MAX_NR_GPIO
);
66 DECLARE_BITMAP(enabled_irqs
, MAX_NR_GPIO
);
68 const struct msm_pinctrl_soc_data
*soc
;
72 static int msm_get_groups_count(struct pinctrl_dev
*pctldev
)
74 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
76 return pctrl
->soc
->ngroups
;
79 static const char *msm_get_group_name(struct pinctrl_dev
*pctldev
,
82 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
84 return pctrl
->soc
->groups
[group
].name
;
87 static int msm_get_group_pins(struct pinctrl_dev
*pctldev
,
89 const unsigned **pins
,
92 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
94 *pins
= pctrl
->soc
->groups
[group
].pins
;
95 *num_pins
= pctrl
->soc
->groups
[group
].npins
;
99 static const struct pinctrl_ops msm_pinctrl_ops
= {
100 .get_groups_count
= msm_get_groups_count
,
101 .get_group_name
= msm_get_group_name
,
102 .get_group_pins
= msm_get_group_pins
,
103 .dt_node_to_map
= pinconf_generic_dt_node_to_map_group
,
104 .dt_free_map
= pinctrl_utils_free_map
,
107 static int msm_get_functions_count(struct pinctrl_dev
*pctldev
)
109 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
111 return pctrl
->soc
->nfunctions
;
114 static const char *msm_get_function_name(struct pinctrl_dev
*pctldev
,
117 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
119 return pctrl
->soc
->functions
[function
].name
;
122 static int msm_get_function_groups(struct pinctrl_dev
*pctldev
,
124 const char * const **groups
,
125 unsigned * const num_groups
)
127 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
129 *groups
= pctrl
->soc
->functions
[function
].groups
;
130 *num_groups
= pctrl
->soc
->functions
[function
].ngroups
;
134 static int msm_pinmux_set_mux(struct pinctrl_dev
*pctldev
,
138 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
139 const struct msm_pingroup
*g
;
144 g
= &pctrl
->soc
->groups
[group
];
146 for (i
= 0; i
< g
->nfuncs
; i
++) {
147 if (g
->funcs
[i
] == function
)
151 if (WARN_ON(i
== g
->nfuncs
))
154 spin_lock_irqsave(&pctrl
->lock
, flags
);
156 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
157 val
&= ~(0x7 << g
->mux_bit
);
158 val
|= i
<< g
->mux_bit
;
159 writel(val
, pctrl
->regs
+ g
->ctl_reg
);
161 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
166 static const struct pinmux_ops msm_pinmux_ops
= {
167 .get_functions_count
= msm_get_functions_count
,
168 .get_function_name
= msm_get_function_name
,
169 .get_function_groups
= msm_get_function_groups
,
170 .set_mux
= msm_pinmux_set_mux
,
173 static int msm_config_reg(struct msm_pinctrl
*pctrl
,
174 const struct msm_pingroup
*g
,
180 case PIN_CONFIG_BIAS_DISABLE
:
181 case PIN_CONFIG_BIAS_PULL_DOWN
:
182 case PIN_CONFIG_BIAS_BUS_HOLD
:
183 case PIN_CONFIG_BIAS_PULL_UP
:
187 case PIN_CONFIG_DRIVE_STRENGTH
:
191 case PIN_CONFIG_OUTPUT
:
192 case PIN_CONFIG_INPUT_ENABLE
:
203 #define MSM_NO_PULL 0
204 #define MSM_PULL_DOWN 1
206 #define MSM_PULL_UP 3
208 static unsigned msm_regval_to_drive(u32 val
)
210 return (val
+ 1) * 2;
213 static int msm_config_group_get(struct pinctrl_dev
*pctldev
,
215 unsigned long *config
)
217 const struct msm_pingroup
*g
;
218 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
219 unsigned param
= pinconf_to_config_param(*config
);
226 g
= &pctrl
->soc
->groups
[group
];
228 ret
= msm_config_reg(pctrl
, g
, param
, &mask
, &bit
);
232 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
233 arg
= (val
>> bit
) & mask
;
235 /* Convert register value to pinconf value */
237 case PIN_CONFIG_BIAS_DISABLE
:
238 arg
= arg
== MSM_NO_PULL
;
240 case PIN_CONFIG_BIAS_PULL_DOWN
:
241 arg
= arg
== MSM_PULL_DOWN
;
243 case PIN_CONFIG_BIAS_BUS_HOLD
:
244 arg
= arg
== MSM_KEEPER
;
246 case PIN_CONFIG_BIAS_PULL_UP
:
247 arg
= arg
== MSM_PULL_UP
;
249 case PIN_CONFIG_DRIVE_STRENGTH
:
250 arg
= msm_regval_to_drive(arg
);
252 case PIN_CONFIG_OUTPUT
:
253 /* Pin is not output */
257 val
= readl(pctrl
->regs
+ g
->io_reg
);
258 arg
= !!(val
& BIT(g
->in_bit
));
260 case PIN_CONFIG_INPUT_ENABLE
:
270 *config
= pinconf_to_config_packed(param
, arg
);
275 static int msm_config_group_set(struct pinctrl_dev
*pctldev
,
277 unsigned long *configs
,
278 unsigned num_configs
)
280 const struct msm_pingroup
*g
;
281 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
291 g
= &pctrl
->soc
->groups
[group
];
293 for (i
= 0; i
< num_configs
; i
++) {
294 param
= pinconf_to_config_param(configs
[i
]);
295 arg
= pinconf_to_config_argument(configs
[i
]);
297 ret
= msm_config_reg(pctrl
, g
, param
, &mask
, &bit
);
301 /* Convert pinconf values to register values */
303 case PIN_CONFIG_BIAS_DISABLE
:
306 case PIN_CONFIG_BIAS_PULL_DOWN
:
309 case PIN_CONFIG_BIAS_BUS_HOLD
:
312 case PIN_CONFIG_BIAS_PULL_UP
:
315 case PIN_CONFIG_DRIVE_STRENGTH
:
316 /* Check for invalid values */
317 if (arg
> 16 || arg
< 2 || (arg
% 2) != 0)
322 case PIN_CONFIG_OUTPUT
:
323 /* set output value */
324 spin_lock_irqsave(&pctrl
->lock
, flags
);
325 val
= readl(pctrl
->regs
+ g
->io_reg
);
327 val
|= BIT(g
->out_bit
);
329 val
&= ~BIT(g
->out_bit
);
330 writel(val
, pctrl
->regs
+ g
->io_reg
);
331 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
336 case PIN_CONFIG_INPUT_ENABLE
:
341 dev_err(pctrl
->dev
, "Unsupported config parameter: %x\n",
346 /* Range-check user-supplied value */
348 dev_err(pctrl
->dev
, "config %x: %x is invalid\n", param
, arg
);
352 spin_lock_irqsave(&pctrl
->lock
, flags
);
353 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
354 val
&= ~(mask
<< bit
);
356 writel(val
, pctrl
->regs
+ g
->ctl_reg
);
357 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
363 static const struct pinconf_ops msm_pinconf_ops
= {
365 .pin_config_group_get
= msm_config_group_get
,
366 .pin_config_group_set
= msm_config_group_set
,
369 static struct pinctrl_desc msm_pinctrl_desc
= {
370 .pctlops
= &msm_pinctrl_ops
,
371 .pmxops
= &msm_pinmux_ops
,
372 .confops
= &msm_pinconf_ops
,
373 .owner
= THIS_MODULE
,
376 static int msm_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
378 const struct msm_pingroup
*g
;
379 struct msm_pinctrl
*pctrl
= gpiochip_get_data(chip
);
383 g
= &pctrl
->soc
->groups
[offset
];
385 spin_lock_irqsave(&pctrl
->lock
, flags
);
387 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
388 val
&= ~BIT(g
->oe_bit
);
389 writel(val
, pctrl
->regs
+ g
->ctl_reg
);
391 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
396 static int msm_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
, int value
)
398 const struct msm_pingroup
*g
;
399 struct msm_pinctrl
*pctrl
= gpiochip_get_data(chip
);
403 g
= &pctrl
->soc
->groups
[offset
];
405 spin_lock_irqsave(&pctrl
->lock
, flags
);
407 val
= readl(pctrl
->regs
+ g
->io_reg
);
409 val
|= BIT(g
->out_bit
);
411 val
&= ~BIT(g
->out_bit
);
412 writel(val
, pctrl
->regs
+ g
->io_reg
);
414 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
415 val
|= BIT(g
->oe_bit
);
416 writel(val
, pctrl
->regs
+ g
->ctl_reg
);
418 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
423 static int msm_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
425 const struct msm_pingroup
*g
;
426 struct msm_pinctrl
*pctrl
= gpiochip_get_data(chip
);
429 g
= &pctrl
->soc
->groups
[offset
];
431 val
= readl(pctrl
->regs
+ g
->io_reg
);
432 return !!(val
& BIT(g
->in_bit
));
435 static void msm_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
437 const struct msm_pingroup
*g
;
438 struct msm_pinctrl
*pctrl
= gpiochip_get_data(chip
);
442 g
= &pctrl
->soc
->groups
[offset
];
444 spin_lock_irqsave(&pctrl
->lock
, flags
);
446 val
= readl(pctrl
->regs
+ g
->io_reg
);
448 val
|= BIT(g
->out_bit
);
450 val
&= ~BIT(g
->out_bit
);
451 writel(val
, pctrl
->regs
+ g
->io_reg
);
453 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
456 #ifdef CONFIG_DEBUG_FS
457 #include <linux/seq_file.h>
459 static void msm_gpio_dbg_show_one(struct seq_file
*s
,
460 struct pinctrl_dev
*pctldev
,
461 struct gpio_chip
*chip
,
465 const struct msm_pingroup
*g
;
466 struct msm_pinctrl
*pctrl
= gpiochip_get_data(chip
);
473 static const char * const pulls
[] = {
480 g
= &pctrl
->soc
->groups
[offset
];
481 ctl_reg
= readl(pctrl
->regs
+ g
->ctl_reg
);
483 is_out
= !!(ctl_reg
& BIT(g
->oe_bit
));
484 func
= (ctl_reg
>> g
->mux_bit
) & 7;
485 drive
= (ctl_reg
>> g
->drv_bit
) & 7;
486 pull
= (ctl_reg
>> g
->pull_bit
) & 3;
488 seq_printf(s
, " %-8s: %-3s %d", g
->name
, is_out
? "out" : "in", func
);
489 seq_printf(s
, " %dmA", msm_regval_to_drive(drive
));
490 seq_printf(s
, " %s", pulls
[pull
]);
493 static void msm_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
495 unsigned gpio
= chip
->base
;
498 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++) {
499 msm_gpio_dbg_show_one(s
, NULL
, chip
, i
, gpio
);
505 #define msm_gpio_dbg_show NULL
508 static struct gpio_chip msm_gpio_template
= {
509 .direction_input
= msm_gpio_direction_input
,
510 .direction_output
= msm_gpio_direction_output
,
513 .request
= gpiochip_generic_request
,
514 .free
= gpiochip_generic_free
,
515 .dbg_show
= msm_gpio_dbg_show
,
518 /* For dual-edge interrupts in software, since some hardware has no
521 * At appropriate moments, this function may be called to flip the polarity
522 * settings of both-edge irq lines to try and catch the next edge.
524 * The attempt is considered successful if:
525 * - the status bit goes high, indicating that an edge was caught, or
526 * - the input value of the gpio doesn't change during the attempt.
527 * If the value changes twice during the process, that would cause the first
528 * test to fail but would force the second, as two opposite
529 * transitions would cause a detection no matter the polarity setting.
531 * The do-loop tries to sledge-hammer closed the timing hole between
532 * the initial value-read and the polarity-write - if the line value changes
533 * during that window, an interrupt is lost, the new polarity setting is
534 * incorrect, and the first success test will fail, causing a retry.
536 * Algorithm comes from Google's msmgpio driver.
538 static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl
*pctrl
,
539 const struct msm_pingroup
*g
,
542 int loop_limit
= 100;
543 unsigned val
, val2
, intstat
;
547 val
= readl(pctrl
->regs
+ g
->io_reg
) & BIT(g
->in_bit
);
549 pol
= readl(pctrl
->regs
+ g
->intr_cfg_reg
);
550 pol
^= BIT(g
->intr_polarity_bit
);
551 writel(pol
, pctrl
->regs
+ g
->intr_cfg_reg
);
553 val2
= readl(pctrl
->regs
+ g
->io_reg
) & BIT(g
->in_bit
);
554 intstat
= readl(pctrl
->regs
+ g
->intr_status_reg
);
555 if (intstat
|| (val
== val2
))
557 } while (loop_limit
-- > 0);
558 dev_err(pctrl
->dev
, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
562 static void msm_gpio_irq_mask(struct irq_data
*d
)
564 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
565 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
566 const struct msm_pingroup
*g
;
570 g
= &pctrl
->soc
->groups
[d
->hwirq
];
572 spin_lock_irqsave(&pctrl
->lock
, flags
);
574 val
= readl(pctrl
->regs
+ g
->intr_cfg_reg
);
575 val
&= ~BIT(g
->intr_enable_bit
);
576 writel(val
, pctrl
->regs
+ g
->intr_cfg_reg
);
578 clear_bit(d
->hwirq
, pctrl
->enabled_irqs
);
580 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
583 static void msm_gpio_irq_unmask(struct irq_data
*d
)
585 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
586 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
587 const struct msm_pingroup
*g
;
591 g
= &pctrl
->soc
->groups
[d
->hwirq
];
593 spin_lock_irqsave(&pctrl
->lock
, flags
);
595 val
= readl(pctrl
->regs
+ g
->intr_status_reg
);
596 val
&= ~BIT(g
->intr_status_bit
);
597 writel(val
, pctrl
->regs
+ g
->intr_status_reg
);
599 val
= readl(pctrl
->regs
+ g
->intr_cfg_reg
);
600 val
|= BIT(g
->intr_enable_bit
);
601 writel(val
, pctrl
->regs
+ g
->intr_cfg_reg
);
603 set_bit(d
->hwirq
, pctrl
->enabled_irqs
);
605 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
608 static void msm_gpio_irq_ack(struct irq_data
*d
)
610 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
611 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
612 const struct msm_pingroup
*g
;
616 g
= &pctrl
->soc
->groups
[d
->hwirq
];
618 spin_lock_irqsave(&pctrl
->lock
, flags
);
620 val
= readl(pctrl
->regs
+ g
->intr_status_reg
);
621 if (g
->intr_ack_high
)
622 val
|= BIT(g
->intr_status_bit
);
624 val
&= ~BIT(g
->intr_status_bit
);
625 writel(val
, pctrl
->regs
+ g
->intr_status_reg
);
627 if (test_bit(d
->hwirq
, pctrl
->dual_edge_irqs
))
628 msm_gpio_update_dual_edge_pos(pctrl
, g
, d
);
630 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
633 static int msm_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
635 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
636 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
637 const struct msm_pingroup
*g
;
641 g
= &pctrl
->soc
->groups
[d
->hwirq
];
643 spin_lock_irqsave(&pctrl
->lock
, flags
);
646 * For hw without possibility of detecting both edges
648 if (g
->intr_detection_width
== 1 && type
== IRQ_TYPE_EDGE_BOTH
)
649 set_bit(d
->hwirq
, pctrl
->dual_edge_irqs
);
651 clear_bit(d
->hwirq
, pctrl
->dual_edge_irqs
);
653 /* Route interrupts to application cpu */
654 val
= readl(pctrl
->regs
+ g
->intr_target_reg
);
655 val
&= ~(7 << g
->intr_target_bit
);
656 val
|= g
->intr_target_kpss_val
<< g
->intr_target_bit
;
657 writel(val
, pctrl
->regs
+ g
->intr_target_reg
);
659 /* Update configuration for gpio.
660 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
661 * internal circuitry of TLMM, toggling the RAW_STATUS
662 * could cause the INTR_STATUS to be set for EDGE interrupts.
664 val
= readl(pctrl
->regs
+ g
->intr_cfg_reg
);
665 val
|= BIT(g
->intr_raw_status_bit
);
666 if (g
->intr_detection_width
== 2) {
667 val
&= ~(3 << g
->intr_detection_bit
);
668 val
&= ~(1 << g
->intr_polarity_bit
);
670 case IRQ_TYPE_EDGE_RISING
:
671 val
|= 1 << g
->intr_detection_bit
;
672 val
|= BIT(g
->intr_polarity_bit
);
674 case IRQ_TYPE_EDGE_FALLING
:
675 val
|= 2 << g
->intr_detection_bit
;
676 val
|= BIT(g
->intr_polarity_bit
);
678 case IRQ_TYPE_EDGE_BOTH
:
679 val
|= 3 << g
->intr_detection_bit
;
680 val
|= BIT(g
->intr_polarity_bit
);
682 case IRQ_TYPE_LEVEL_LOW
:
684 case IRQ_TYPE_LEVEL_HIGH
:
685 val
|= BIT(g
->intr_polarity_bit
);
688 } else if (g
->intr_detection_width
== 1) {
689 val
&= ~(1 << g
->intr_detection_bit
);
690 val
&= ~(1 << g
->intr_polarity_bit
);
692 case IRQ_TYPE_EDGE_RISING
:
693 val
|= BIT(g
->intr_detection_bit
);
694 val
|= BIT(g
->intr_polarity_bit
);
696 case IRQ_TYPE_EDGE_FALLING
:
697 val
|= BIT(g
->intr_detection_bit
);
699 case IRQ_TYPE_EDGE_BOTH
:
700 val
|= BIT(g
->intr_detection_bit
);
701 val
|= BIT(g
->intr_polarity_bit
);
703 case IRQ_TYPE_LEVEL_LOW
:
705 case IRQ_TYPE_LEVEL_HIGH
:
706 val
|= BIT(g
->intr_polarity_bit
);
712 writel(val
, pctrl
->regs
+ g
->intr_cfg_reg
);
714 if (test_bit(d
->hwirq
, pctrl
->dual_edge_irqs
))
715 msm_gpio_update_dual_edge_pos(pctrl
, g
, d
);
717 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
719 if (type
& (IRQ_TYPE_LEVEL_LOW
| IRQ_TYPE_LEVEL_HIGH
))
720 irq_set_handler_locked(d
, handle_level_irq
);
721 else if (type
& (IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
))
722 irq_set_handler_locked(d
, handle_edge_irq
);
727 static int msm_gpio_irq_set_wake(struct irq_data
*d
, unsigned int on
)
729 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
730 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
733 spin_lock_irqsave(&pctrl
->lock
, flags
);
735 irq_set_irq_wake(pctrl
->irq
, on
);
737 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
742 static struct irq_chip msm_gpio_irq_chip
= {
744 .irq_mask
= msm_gpio_irq_mask
,
745 .irq_unmask
= msm_gpio_irq_unmask
,
746 .irq_ack
= msm_gpio_irq_ack
,
747 .irq_set_type
= msm_gpio_irq_set_type
,
748 .irq_set_wake
= msm_gpio_irq_set_wake
,
751 static void msm_gpio_irq_handler(struct irq_desc
*desc
)
753 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
754 const struct msm_pingroup
*g
;
755 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
756 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
762 chained_irq_enter(chip
, desc
);
765 * Each pin has it's own IRQ status register, so use
766 * enabled_irq bitmap to limit the number of reads.
768 for_each_set_bit(i
, pctrl
->enabled_irqs
, pctrl
->chip
.ngpio
) {
769 g
= &pctrl
->soc
->groups
[i
];
770 val
= readl(pctrl
->regs
+ g
->intr_status_reg
);
771 if (val
& BIT(g
->intr_status_bit
)) {
772 irq_pin
= irq_find_mapping(gc
->irqdomain
, i
);
773 generic_handle_irq(irq_pin
);
778 /* No interrupts were flagged */
780 handle_bad_irq(desc
);
782 chained_irq_exit(chip
, desc
);
785 static int msm_gpio_init(struct msm_pinctrl
*pctrl
)
787 struct gpio_chip
*chip
;
789 unsigned ngpio
= pctrl
->soc
->ngpios
;
791 if (WARN_ON(ngpio
> MAX_NR_GPIO
))
797 chip
->label
= dev_name(pctrl
->dev
);
798 chip
->parent
= pctrl
->dev
;
799 chip
->owner
= THIS_MODULE
;
800 chip
->of_node
= pctrl
->dev
->of_node
;
802 ret
= gpiochip_add_data(&pctrl
->chip
, pctrl
);
804 dev_err(pctrl
->dev
, "Failed register gpiochip\n");
808 ret
= gpiochip_add_pin_range(&pctrl
->chip
, dev_name(pctrl
->dev
), 0, 0, chip
->ngpio
);
810 dev_err(pctrl
->dev
, "Failed to add pin range\n");
811 gpiochip_remove(&pctrl
->chip
);
815 ret
= gpiochip_irqchip_add(chip
,
821 dev_err(pctrl
->dev
, "Failed to add irqchip to gpiochip\n");
822 gpiochip_remove(&pctrl
->chip
);
826 gpiochip_set_chained_irqchip(chip
, &msm_gpio_irq_chip
, pctrl
->irq
,
827 msm_gpio_irq_handler
);
832 static int msm_ps_hold_restart(struct notifier_block
*nb
, unsigned long action
,
835 struct msm_pinctrl
*pctrl
= container_of(nb
, struct msm_pinctrl
, restart_nb
);
837 writel(0, pctrl
->regs
+ PS_HOLD_OFFSET
);
842 static struct msm_pinctrl
*poweroff_pctrl
;
844 static void msm_ps_hold_poweroff(void)
846 msm_ps_hold_restart(&poweroff_pctrl
->restart_nb
, 0, NULL
);
849 static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl
*pctrl
)
852 const struct msm_function
*func
= pctrl
->soc
->functions
;
854 for (i
= 0; i
< pctrl
->soc
->nfunctions
; i
++)
855 if (!strcmp(func
[i
].name
, "ps_hold")) {
856 pctrl
->restart_nb
.notifier_call
= msm_ps_hold_restart
;
857 pctrl
->restart_nb
.priority
= 128;
858 if (register_restart_handler(&pctrl
->restart_nb
))
860 "failed to setup restart handler.\n");
861 poweroff_pctrl
= pctrl
;
862 pm_power_off
= msm_ps_hold_poweroff
;
867 int msm_pinctrl_probe(struct platform_device
*pdev
,
868 const struct msm_pinctrl_soc_data
*soc_data
)
870 struct msm_pinctrl
*pctrl
;
871 struct resource
*res
;
874 pctrl
= devm_kzalloc(&pdev
->dev
, sizeof(*pctrl
), GFP_KERNEL
);
876 dev_err(&pdev
->dev
, "Can't allocate msm_pinctrl\n");
879 pctrl
->dev
= &pdev
->dev
;
880 pctrl
->soc
= soc_data
;
881 pctrl
->chip
= msm_gpio_template
;
883 spin_lock_init(&pctrl
->lock
);
885 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
886 pctrl
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
887 if (IS_ERR(pctrl
->regs
))
888 return PTR_ERR(pctrl
->regs
);
890 msm_pinctrl_setup_pm_reset(pctrl
);
892 pctrl
->irq
= platform_get_irq(pdev
, 0);
893 if (pctrl
->irq
< 0) {
894 dev_err(&pdev
->dev
, "No interrupt defined for msmgpio\n");
898 msm_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
899 msm_pinctrl_desc
.pins
= pctrl
->soc
->pins
;
900 msm_pinctrl_desc
.npins
= pctrl
->soc
->npins
;
901 pctrl
->pctrl
= devm_pinctrl_register(&pdev
->dev
, &msm_pinctrl_desc
,
903 if (IS_ERR(pctrl
->pctrl
)) {
904 dev_err(&pdev
->dev
, "Couldn't register pinctrl driver\n");
905 return PTR_ERR(pctrl
->pctrl
);
908 ret
= msm_gpio_init(pctrl
);
912 platform_set_drvdata(pdev
, pctrl
);
914 dev_dbg(&pdev
->dev
, "Probed Qualcomm pinctrl driver\n");
918 EXPORT_SYMBOL(msm_pinctrl_probe
);
920 int msm_pinctrl_remove(struct platform_device
*pdev
)
922 struct msm_pinctrl
*pctrl
= platform_get_drvdata(pdev
);
924 gpiochip_remove(&pctrl
->chip
);
926 unregister_restart_handler(&pctrl
->restart_nb
);
930 EXPORT_SYMBOL(msm_pinctrl_remove
);