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>
32 #include <linux/log2.h>
35 #include "../pinconf.h"
36 #include "pinctrl-msm.h"
37 #include "../pinctrl-utils.h"
39 #define MAX_NR_GPIO 300
40 #define PS_HOLD_OFFSET 0x820
43 * struct msm_pinctrl - state for a pinctrl-msm device
44 * @dev: device handle.
45 * @pctrl: pinctrl handle.
46 * @chip: gpiochip handle.
47 * @restart_nb: restart notifier block.
48 * @irq: parent irq for the TLMM irq_chip.
49 * @lock: Spinlock to protect register resources as well
50 * as msm_pinctrl data structures.
51 * @enabled_irqs: Bitmap of currently enabled irqs.
52 * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
54 * @soc; Reference to soc_data of platform specific data.
55 * @regs: Base address for the TLMM register map.
59 struct pinctrl_dev
*pctrl
;
60 struct gpio_chip chip
;
61 struct notifier_block restart_nb
;
66 DECLARE_BITMAP(dual_edge_irqs
, MAX_NR_GPIO
);
67 DECLARE_BITMAP(enabled_irqs
, MAX_NR_GPIO
);
69 const struct msm_pinctrl_soc_data
*soc
;
73 static int msm_get_groups_count(struct pinctrl_dev
*pctldev
)
75 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
77 return pctrl
->soc
->ngroups
;
80 static const char *msm_get_group_name(struct pinctrl_dev
*pctldev
,
83 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
85 return pctrl
->soc
->groups
[group
].name
;
88 static int msm_get_group_pins(struct pinctrl_dev
*pctldev
,
90 const unsigned **pins
,
93 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
95 *pins
= pctrl
->soc
->groups
[group
].pins
;
96 *num_pins
= pctrl
->soc
->groups
[group
].npins
;
100 static const struct pinctrl_ops msm_pinctrl_ops
= {
101 .get_groups_count
= msm_get_groups_count
,
102 .get_group_name
= msm_get_group_name
,
103 .get_group_pins
= msm_get_group_pins
,
104 .dt_node_to_map
= pinconf_generic_dt_node_to_map_group
,
105 .dt_free_map
= pinctrl_utils_free_map
,
108 static int msm_get_functions_count(struct pinctrl_dev
*pctldev
)
110 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
112 return pctrl
->soc
->nfunctions
;
115 static const char *msm_get_function_name(struct pinctrl_dev
*pctldev
,
118 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
120 return pctrl
->soc
->functions
[function
].name
;
123 static int msm_get_function_groups(struct pinctrl_dev
*pctldev
,
125 const char * const **groups
,
126 unsigned * const num_groups
)
128 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
130 *groups
= pctrl
->soc
->functions
[function
].groups
;
131 *num_groups
= pctrl
->soc
->functions
[function
].ngroups
;
135 static int msm_pinmux_set_mux(struct pinctrl_dev
*pctldev
,
139 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
140 const struct msm_pingroup
*g
;
145 g
= &pctrl
->soc
->groups
[group
];
146 mask
= GENMASK(g
->mux_bit
+ order_base_2(g
->nfuncs
) - 1, g
->mux_bit
);
148 for (i
= 0; i
< g
->nfuncs
; i
++) {
149 if (g
->funcs
[i
] == function
)
153 if (WARN_ON(i
== g
->nfuncs
))
156 spin_lock_irqsave(&pctrl
->lock
, flags
);
158 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
160 val
|= i
<< g
->mux_bit
;
161 writel(val
, pctrl
->regs
+ g
->ctl_reg
);
163 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
168 static const struct pinmux_ops msm_pinmux_ops
= {
169 .get_functions_count
= msm_get_functions_count
,
170 .get_function_name
= msm_get_function_name
,
171 .get_function_groups
= msm_get_function_groups
,
172 .set_mux
= msm_pinmux_set_mux
,
175 static int msm_config_reg(struct msm_pinctrl
*pctrl
,
176 const struct msm_pingroup
*g
,
182 case PIN_CONFIG_BIAS_DISABLE
:
183 case PIN_CONFIG_BIAS_PULL_DOWN
:
184 case PIN_CONFIG_BIAS_BUS_HOLD
:
185 case PIN_CONFIG_BIAS_PULL_UP
:
189 case PIN_CONFIG_DRIVE_STRENGTH
:
193 case PIN_CONFIG_OUTPUT
:
194 case PIN_CONFIG_INPUT_ENABLE
:
205 #define MSM_NO_PULL 0
206 #define MSM_PULL_DOWN 1
208 #define MSM_PULL_UP 3
210 static unsigned msm_regval_to_drive(u32 val
)
212 return (val
+ 1) * 2;
215 static int msm_config_group_get(struct pinctrl_dev
*pctldev
,
217 unsigned long *config
)
219 const struct msm_pingroup
*g
;
220 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
221 unsigned param
= pinconf_to_config_param(*config
);
228 g
= &pctrl
->soc
->groups
[group
];
230 ret
= msm_config_reg(pctrl
, g
, param
, &mask
, &bit
);
234 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
235 arg
= (val
>> bit
) & mask
;
237 /* Convert register value to pinconf value */
239 case PIN_CONFIG_BIAS_DISABLE
:
240 arg
= arg
== MSM_NO_PULL
;
242 case PIN_CONFIG_BIAS_PULL_DOWN
:
243 arg
= arg
== MSM_PULL_DOWN
;
245 case PIN_CONFIG_BIAS_BUS_HOLD
:
246 arg
= arg
== MSM_KEEPER
;
248 case PIN_CONFIG_BIAS_PULL_UP
:
249 arg
= arg
== MSM_PULL_UP
;
251 case PIN_CONFIG_DRIVE_STRENGTH
:
252 arg
= msm_regval_to_drive(arg
);
254 case PIN_CONFIG_OUTPUT
:
255 /* Pin is not output */
259 val
= readl(pctrl
->regs
+ g
->io_reg
);
260 arg
= !!(val
& BIT(g
->in_bit
));
262 case PIN_CONFIG_INPUT_ENABLE
:
272 *config
= pinconf_to_config_packed(param
, arg
);
277 static int msm_config_group_set(struct pinctrl_dev
*pctldev
,
279 unsigned long *configs
,
280 unsigned num_configs
)
282 const struct msm_pingroup
*g
;
283 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
293 g
= &pctrl
->soc
->groups
[group
];
295 for (i
= 0; i
< num_configs
; i
++) {
296 param
= pinconf_to_config_param(configs
[i
]);
297 arg
= pinconf_to_config_argument(configs
[i
]);
299 ret
= msm_config_reg(pctrl
, g
, param
, &mask
, &bit
);
303 /* Convert pinconf values to register values */
305 case PIN_CONFIG_BIAS_DISABLE
:
308 case PIN_CONFIG_BIAS_PULL_DOWN
:
311 case PIN_CONFIG_BIAS_BUS_HOLD
:
314 case PIN_CONFIG_BIAS_PULL_UP
:
317 case PIN_CONFIG_DRIVE_STRENGTH
:
318 /* Check for invalid values */
319 if (arg
> 16 || arg
< 2 || (arg
% 2) != 0)
324 case PIN_CONFIG_OUTPUT
:
325 /* set output value */
326 spin_lock_irqsave(&pctrl
->lock
, flags
);
327 val
= readl(pctrl
->regs
+ g
->io_reg
);
329 val
|= BIT(g
->out_bit
);
331 val
&= ~BIT(g
->out_bit
);
332 writel(val
, pctrl
->regs
+ g
->io_reg
);
333 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
338 case PIN_CONFIG_INPUT_ENABLE
:
343 dev_err(pctrl
->dev
, "Unsupported config parameter: %x\n",
348 /* Range-check user-supplied value */
350 dev_err(pctrl
->dev
, "config %x: %x is invalid\n", param
, arg
);
354 spin_lock_irqsave(&pctrl
->lock
, flags
);
355 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
356 val
&= ~(mask
<< bit
);
358 writel(val
, pctrl
->regs
+ g
->ctl_reg
);
359 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
365 static const struct pinconf_ops msm_pinconf_ops
= {
367 .pin_config_group_get
= msm_config_group_get
,
368 .pin_config_group_set
= msm_config_group_set
,
371 static struct pinctrl_desc msm_pinctrl_desc
= {
372 .pctlops
= &msm_pinctrl_ops
,
373 .pmxops
= &msm_pinmux_ops
,
374 .confops
= &msm_pinconf_ops
,
375 .owner
= THIS_MODULE
,
378 static int msm_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
380 const struct msm_pingroup
*g
;
381 struct msm_pinctrl
*pctrl
= gpiochip_get_data(chip
);
385 g
= &pctrl
->soc
->groups
[offset
];
387 spin_lock_irqsave(&pctrl
->lock
, flags
);
389 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
390 val
&= ~BIT(g
->oe_bit
);
391 writel(val
, pctrl
->regs
+ g
->ctl_reg
);
393 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
398 static int msm_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
, int value
)
400 const struct msm_pingroup
*g
;
401 struct msm_pinctrl
*pctrl
= gpiochip_get_data(chip
);
405 g
= &pctrl
->soc
->groups
[offset
];
407 spin_lock_irqsave(&pctrl
->lock
, flags
);
409 val
= readl(pctrl
->regs
+ g
->io_reg
);
411 val
|= BIT(g
->out_bit
);
413 val
&= ~BIT(g
->out_bit
);
414 writel(val
, pctrl
->regs
+ g
->io_reg
);
416 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
417 val
|= BIT(g
->oe_bit
);
418 writel(val
, pctrl
->regs
+ g
->ctl_reg
);
420 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
425 static int msm_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
427 const struct msm_pingroup
*g
;
428 struct msm_pinctrl
*pctrl
= gpiochip_get_data(chip
);
431 g
= &pctrl
->soc
->groups
[offset
];
433 val
= readl(pctrl
->regs
+ g
->io_reg
);
434 return !!(val
& BIT(g
->in_bit
));
437 static void msm_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
439 const struct msm_pingroup
*g
;
440 struct msm_pinctrl
*pctrl
= gpiochip_get_data(chip
);
444 g
= &pctrl
->soc
->groups
[offset
];
446 spin_lock_irqsave(&pctrl
->lock
, flags
);
448 val
= readl(pctrl
->regs
+ g
->io_reg
);
450 val
|= BIT(g
->out_bit
);
452 val
&= ~BIT(g
->out_bit
);
453 writel(val
, pctrl
->regs
+ g
->io_reg
);
455 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
458 #ifdef CONFIG_DEBUG_FS
459 #include <linux/seq_file.h>
461 static void msm_gpio_dbg_show_one(struct seq_file
*s
,
462 struct pinctrl_dev
*pctldev
,
463 struct gpio_chip
*chip
,
467 const struct msm_pingroup
*g
;
468 struct msm_pinctrl
*pctrl
= gpiochip_get_data(chip
);
475 static const char * const pulls
[] = {
482 g
= &pctrl
->soc
->groups
[offset
];
483 ctl_reg
= readl(pctrl
->regs
+ g
->ctl_reg
);
485 is_out
= !!(ctl_reg
& BIT(g
->oe_bit
));
486 func
= (ctl_reg
>> g
->mux_bit
) & 7;
487 drive
= (ctl_reg
>> g
->drv_bit
) & 7;
488 pull
= (ctl_reg
>> g
->pull_bit
) & 3;
490 seq_printf(s
, " %-8s: %-3s %d", g
->name
, is_out
? "out" : "in", func
);
491 seq_printf(s
, " %dmA", msm_regval_to_drive(drive
));
492 seq_printf(s
, " %s", pulls
[pull
]);
495 static void msm_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
497 unsigned gpio
= chip
->base
;
500 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++) {
501 msm_gpio_dbg_show_one(s
, NULL
, chip
, i
, gpio
);
507 #define msm_gpio_dbg_show NULL
510 static struct gpio_chip msm_gpio_template
= {
511 .direction_input
= msm_gpio_direction_input
,
512 .direction_output
= msm_gpio_direction_output
,
515 .request
= gpiochip_generic_request
,
516 .free
= gpiochip_generic_free
,
517 .dbg_show
= msm_gpio_dbg_show
,
520 /* For dual-edge interrupts in software, since some hardware has no
523 * At appropriate moments, this function may be called to flip the polarity
524 * settings of both-edge irq lines to try and catch the next edge.
526 * The attempt is considered successful if:
527 * - the status bit goes high, indicating that an edge was caught, or
528 * - the input value of the gpio doesn't change during the attempt.
529 * If the value changes twice during the process, that would cause the first
530 * test to fail but would force the second, as two opposite
531 * transitions would cause a detection no matter the polarity setting.
533 * The do-loop tries to sledge-hammer closed the timing hole between
534 * the initial value-read and the polarity-write - if the line value changes
535 * during that window, an interrupt is lost, the new polarity setting is
536 * incorrect, and the first success test will fail, causing a retry.
538 * Algorithm comes from Google's msmgpio driver.
540 static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl
*pctrl
,
541 const struct msm_pingroup
*g
,
544 int loop_limit
= 100;
545 unsigned val
, val2
, intstat
;
549 val
= readl(pctrl
->regs
+ g
->io_reg
) & BIT(g
->in_bit
);
551 pol
= readl(pctrl
->regs
+ g
->intr_cfg_reg
);
552 pol
^= BIT(g
->intr_polarity_bit
);
553 writel(pol
, pctrl
->regs
+ g
->intr_cfg_reg
);
555 val2
= readl(pctrl
->regs
+ g
->io_reg
) & BIT(g
->in_bit
);
556 intstat
= readl(pctrl
->regs
+ g
->intr_status_reg
);
557 if (intstat
|| (val
== val2
))
559 } while (loop_limit
-- > 0);
560 dev_err(pctrl
->dev
, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
564 static void msm_gpio_irq_mask(struct irq_data
*d
)
566 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
567 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
568 const struct msm_pingroup
*g
;
572 g
= &pctrl
->soc
->groups
[d
->hwirq
];
574 spin_lock_irqsave(&pctrl
->lock
, flags
);
576 val
= readl(pctrl
->regs
+ g
->intr_cfg_reg
);
577 val
&= ~BIT(g
->intr_enable_bit
);
578 writel(val
, pctrl
->regs
+ g
->intr_cfg_reg
);
580 clear_bit(d
->hwirq
, pctrl
->enabled_irqs
);
582 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
585 static void msm_gpio_irq_unmask(struct irq_data
*d
)
587 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
588 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
589 const struct msm_pingroup
*g
;
593 g
= &pctrl
->soc
->groups
[d
->hwirq
];
595 spin_lock_irqsave(&pctrl
->lock
, flags
);
597 val
= readl(pctrl
->regs
+ g
->intr_status_reg
);
598 val
&= ~BIT(g
->intr_status_bit
);
599 writel(val
, pctrl
->regs
+ g
->intr_status_reg
);
601 val
= readl(pctrl
->regs
+ g
->intr_cfg_reg
);
602 val
|= BIT(g
->intr_enable_bit
);
603 writel(val
, pctrl
->regs
+ g
->intr_cfg_reg
);
605 set_bit(d
->hwirq
, pctrl
->enabled_irqs
);
607 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
610 static void msm_gpio_irq_ack(struct irq_data
*d
)
612 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
613 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
614 const struct msm_pingroup
*g
;
618 g
= &pctrl
->soc
->groups
[d
->hwirq
];
620 spin_lock_irqsave(&pctrl
->lock
, flags
);
622 val
= readl(pctrl
->regs
+ g
->intr_status_reg
);
623 if (g
->intr_ack_high
)
624 val
|= BIT(g
->intr_status_bit
);
626 val
&= ~BIT(g
->intr_status_bit
);
627 writel(val
, pctrl
->regs
+ g
->intr_status_reg
);
629 if (test_bit(d
->hwirq
, pctrl
->dual_edge_irqs
))
630 msm_gpio_update_dual_edge_pos(pctrl
, g
, d
);
632 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
635 static int msm_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
637 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
638 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
639 const struct msm_pingroup
*g
;
643 g
= &pctrl
->soc
->groups
[d
->hwirq
];
645 spin_lock_irqsave(&pctrl
->lock
, flags
);
648 * For hw without possibility of detecting both edges
650 if (g
->intr_detection_width
== 1 && type
== IRQ_TYPE_EDGE_BOTH
)
651 set_bit(d
->hwirq
, pctrl
->dual_edge_irqs
);
653 clear_bit(d
->hwirq
, pctrl
->dual_edge_irqs
);
655 /* Route interrupts to application cpu */
656 val
= readl(pctrl
->regs
+ g
->intr_target_reg
);
657 val
&= ~(7 << g
->intr_target_bit
);
658 val
|= g
->intr_target_kpss_val
<< g
->intr_target_bit
;
659 writel(val
, pctrl
->regs
+ g
->intr_target_reg
);
661 /* Update configuration for gpio.
662 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
663 * internal circuitry of TLMM, toggling the RAW_STATUS
664 * could cause the INTR_STATUS to be set for EDGE interrupts.
666 val
= readl(pctrl
->regs
+ g
->intr_cfg_reg
);
667 val
|= BIT(g
->intr_raw_status_bit
);
668 if (g
->intr_detection_width
== 2) {
669 val
&= ~(3 << g
->intr_detection_bit
);
670 val
&= ~(1 << g
->intr_polarity_bit
);
672 case IRQ_TYPE_EDGE_RISING
:
673 val
|= 1 << g
->intr_detection_bit
;
674 val
|= BIT(g
->intr_polarity_bit
);
676 case IRQ_TYPE_EDGE_FALLING
:
677 val
|= 2 << g
->intr_detection_bit
;
678 val
|= BIT(g
->intr_polarity_bit
);
680 case IRQ_TYPE_EDGE_BOTH
:
681 val
|= 3 << g
->intr_detection_bit
;
682 val
|= BIT(g
->intr_polarity_bit
);
684 case IRQ_TYPE_LEVEL_LOW
:
686 case IRQ_TYPE_LEVEL_HIGH
:
687 val
|= BIT(g
->intr_polarity_bit
);
690 } else if (g
->intr_detection_width
== 1) {
691 val
&= ~(1 << g
->intr_detection_bit
);
692 val
&= ~(1 << g
->intr_polarity_bit
);
694 case IRQ_TYPE_EDGE_RISING
:
695 val
|= BIT(g
->intr_detection_bit
);
696 val
|= BIT(g
->intr_polarity_bit
);
698 case IRQ_TYPE_EDGE_FALLING
:
699 val
|= BIT(g
->intr_detection_bit
);
701 case IRQ_TYPE_EDGE_BOTH
:
702 val
|= BIT(g
->intr_detection_bit
);
703 val
|= BIT(g
->intr_polarity_bit
);
705 case IRQ_TYPE_LEVEL_LOW
:
707 case IRQ_TYPE_LEVEL_HIGH
:
708 val
|= BIT(g
->intr_polarity_bit
);
714 writel(val
, pctrl
->regs
+ g
->intr_cfg_reg
);
716 if (test_bit(d
->hwirq
, pctrl
->dual_edge_irqs
))
717 msm_gpio_update_dual_edge_pos(pctrl
, g
, d
);
719 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
721 if (type
& (IRQ_TYPE_LEVEL_LOW
| IRQ_TYPE_LEVEL_HIGH
))
722 irq_set_handler_locked(d
, handle_level_irq
);
723 else if (type
& (IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
))
724 irq_set_handler_locked(d
, handle_edge_irq
);
729 static int msm_gpio_irq_set_wake(struct irq_data
*d
, unsigned int on
)
731 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
732 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
735 spin_lock_irqsave(&pctrl
->lock
, flags
);
737 irq_set_irq_wake(pctrl
->irq
, on
);
739 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
744 static struct irq_chip msm_gpio_irq_chip
= {
746 .irq_mask
= msm_gpio_irq_mask
,
747 .irq_unmask
= msm_gpio_irq_unmask
,
748 .irq_ack
= msm_gpio_irq_ack
,
749 .irq_set_type
= msm_gpio_irq_set_type
,
750 .irq_set_wake
= msm_gpio_irq_set_wake
,
753 static void msm_gpio_irq_handler(struct irq_desc
*desc
)
755 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
756 const struct msm_pingroup
*g
;
757 struct msm_pinctrl
*pctrl
= gpiochip_get_data(gc
);
758 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
764 chained_irq_enter(chip
, desc
);
767 * Each pin has it's own IRQ status register, so use
768 * enabled_irq bitmap to limit the number of reads.
770 for_each_set_bit(i
, pctrl
->enabled_irqs
, pctrl
->chip
.ngpio
) {
771 g
= &pctrl
->soc
->groups
[i
];
772 val
= readl(pctrl
->regs
+ g
->intr_status_reg
);
773 if (val
& BIT(g
->intr_status_bit
)) {
774 irq_pin
= irq_find_mapping(gc
->irqdomain
, i
);
775 generic_handle_irq(irq_pin
);
780 /* No interrupts were flagged */
782 handle_bad_irq(desc
);
784 chained_irq_exit(chip
, desc
);
787 static int msm_gpio_init(struct msm_pinctrl
*pctrl
)
789 struct gpio_chip
*chip
;
791 unsigned ngpio
= pctrl
->soc
->ngpios
;
793 if (WARN_ON(ngpio
> MAX_NR_GPIO
))
799 chip
->label
= dev_name(pctrl
->dev
);
800 chip
->parent
= pctrl
->dev
;
801 chip
->owner
= THIS_MODULE
;
802 chip
->of_node
= pctrl
->dev
->of_node
;
804 ret
= gpiochip_add_data(&pctrl
->chip
, pctrl
);
806 dev_err(pctrl
->dev
, "Failed register gpiochip\n");
810 ret
= gpiochip_add_pin_range(&pctrl
->chip
, dev_name(pctrl
->dev
), 0, 0, chip
->ngpio
);
812 dev_err(pctrl
->dev
, "Failed to add pin range\n");
813 gpiochip_remove(&pctrl
->chip
);
817 ret
= gpiochip_irqchip_add(chip
,
823 dev_err(pctrl
->dev
, "Failed to add irqchip to gpiochip\n");
824 gpiochip_remove(&pctrl
->chip
);
828 gpiochip_set_chained_irqchip(chip
, &msm_gpio_irq_chip
, pctrl
->irq
,
829 msm_gpio_irq_handler
);
834 static int msm_ps_hold_restart(struct notifier_block
*nb
, unsigned long action
,
837 struct msm_pinctrl
*pctrl
= container_of(nb
, struct msm_pinctrl
, restart_nb
);
839 writel(0, pctrl
->regs
+ PS_HOLD_OFFSET
);
844 static struct msm_pinctrl
*poweroff_pctrl
;
846 static void msm_ps_hold_poweroff(void)
848 msm_ps_hold_restart(&poweroff_pctrl
->restart_nb
, 0, NULL
);
851 static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl
*pctrl
)
854 const struct msm_function
*func
= pctrl
->soc
->functions
;
856 for (i
= 0; i
< pctrl
->soc
->nfunctions
; i
++)
857 if (!strcmp(func
[i
].name
, "ps_hold")) {
858 pctrl
->restart_nb
.notifier_call
= msm_ps_hold_restart
;
859 pctrl
->restart_nb
.priority
= 128;
860 if (register_restart_handler(&pctrl
->restart_nb
))
862 "failed to setup restart handler.\n");
863 poweroff_pctrl
= pctrl
;
864 pm_power_off
= msm_ps_hold_poweroff
;
869 int msm_pinctrl_probe(struct platform_device
*pdev
,
870 const struct msm_pinctrl_soc_data
*soc_data
)
872 struct msm_pinctrl
*pctrl
;
873 struct resource
*res
;
876 pctrl
= devm_kzalloc(&pdev
->dev
, sizeof(*pctrl
), GFP_KERNEL
);
878 dev_err(&pdev
->dev
, "Can't allocate msm_pinctrl\n");
881 pctrl
->dev
= &pdev
->dev
;
882 pctrl
->soc
= soc_data
;
883 pctrl
->chip
= msm_gpio_template
;
885 spin_lock_init(&pctrl
->lock
);
887 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
888 pctrl
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
889 if (IS_ERR(pctrl
->regs
))
890 return PTR_ERR(pctrl
->regs
);
892 msm_pinctrl_setup_pm_reset(pctrl
);
894 pctrl
->irq
= platform_get_irq(pdev
, 0);
895 if (pctrl
->irq
< 0) {
896 dev_err(&pdev
->dev
, "No interrupt defined for msmgpio\n");
900 msm_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
901 msm_pinctrl_desc
.pins
= pctrl
->soc
->pins
;
902 msm_pinctrl_desc
.npins
= pctrl
->soc
->npins
;
903 pctrl
->pctrl
= devm_pinctrl_register(&pdev
->dev
, &msm_pinctrl_desc
,
905 if (IS_ERR(pctrl
->pctrl
)) {
906 dev_err(&pdev
->dev
, "Couldn't register pinctrl driver\n");
907 return PTR_ERR(pctrl
->pctrl
);
910 ret
= msm_gpio_init(pctrl
);
914 platform_set_drvdata(pdev
, pctrl
);
916 dev_dbg(&pdev
->dev
, "Probed Qualcomm pinctrl driver\n");
920 EXPORT_SYMBOL(msm_pinctrl_probe
);
922 int msm_pinctrl_remove(struct platform_device
*pdev
)
924 struct msm_pinctrl
*pctrl
= platform_get_drvdata(pdev
);
926 gpiochip_remove(&pctrl
->chip
);
928 unregister_restart_handler(&pctrl
->restart_nb
);
932 EXPORT_SYMBOL(msm_pinctrl_remove
);