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/err.h>
16 #include <linux/irqdomain.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/irq.h>
30 #include <linux/irqchip/chained_irq.h>
31 #include <linux/of_irq.h>
32 #include <linux/spinlock.h>
36 #include "pinctrl-msm.h"
37 #include "pinctrl-utils.h"
39 #define MAX_NR_GPIO 300
42 * struct msm_pinctrl - state for a pinctrl-msm device
43 * @dev: device handle.
44 * @pctrl: pinctrl handle.
45 * @domain: irqdomain handle.
46 * @chip: gpiochip handle.
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 * @wake_irqs: Bitmap of irqs with requested as wakeup source.
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 irq_domain
*domain
;
61 struct gpio_chip chip
;
66 DECLARE_BITMAP(dual_edge_irqs
, MAX_NR_GPIO
);
67 DECLARE_BITMAP(enabled_irqs
, MAX_NR_GPIO
);
68 DECLARE_BITMAP(wake_irqs
, MAX_NR_GPIO
);
70 const struct msm_pinctrl_soc_data
*soc
;
74 static int msm_get_groups_count(struct pinctrl_dev
*pctldev
)
76 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
78 return pctrl
->soc
->ngroups
;
81 static const char *msm_get_group_name(struct pinctrl_dev
*pctldev
,
84 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
86 return pctrl
->soc
->groups
[group
].name
;
89 static int msm_get_group_pins(struct pinctrl_dev
*pctldev
,
91 const unsigned **pins
,
94 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
96 *pins
= pctrl
->soc
->groups
[group
].pins
;
97 *num_pins
= pctrl
->soc
->groups
[group
].npins
;
101 static const struct pinctrl_ops msm_pinctrl_ops
= {
102 .get_groups_count
= msm_get_groups_count
,
103 .get_group_name
= msm_get_group_name
,
104 .get_group_pins
= msm_get_group_pins
,
105 .dt_node_to_map
= pinconf_generic_dt_node_to_map_group
,
106 .dt_free_map
= pinctrl_utils_dt_free_map
,
109 static int msm_get_functions_count(struct pinctrl_dev
*pctldev
)
111 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
113 return pctrl
->soc
->nfunctions
;
116 static const char *msm_get_function_name(struct pinctrl_dev
*pctldev
,
119 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
121 return pctrl
->soc
->functions
[function
].name
;
124 static int msm_get_function_groups(struct pinctrl_dev
*pctldev
,
126 const char * const **groups
,
127 unsigned * const num_groups
)
129 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
131 *groups
= pctrl
->soc
->functions
[function
].groups
;
132 *num_groups
= pctrl
->soc
->functions
[function
].ngroups
;
136 static int msm_pinmux_enable(struct pinctrl_dev
*pctldev
,
140 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
141 const struct msm_pingroup
*g
;
146 g
= &pctrl
->soc
->groups
[group
];
148 if (WARN_ON(g
->mux_bit
< 0))
151 for (i
= 0; i
< ARRAY_SIZE(g
->funcs
); i
++) {
152 if (g
->funcs
[i
] == function
)
156 if (WARN_ON(i
== ARRAY_SIZE(g
->funcs
)))
159 spin_lock_irqsave(&pctrl
->lock
, flags
);
161 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
162 val
&= ~(0x7 << g
->mux_bit
);
163 val
|= i
<< g
->mux_bit
;
164 writel(val
, pctrl
->regs
+ g
->ctl_reg
);
166 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
171 static void msm_pinmux_disable(struct pinctrl_dev
*pctldev
,
175 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
176 const struct msm_pingroup
*g
;
180 g
= &pctrl
->soc
->groups
[group
];
182 if (WARN_ON(g
->mux_bit
< 0))
185 spin_lock_irqsave(&pctrl
->lock
, flags
);
187 /* Clear the mux bits to select gpio mode */
188 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
189 val
&= ~(0x7 << g
->mux_bit
);
190 writel(val
, pctrl
->regs
+ g
->ctl_reg
);
192 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
195 static const struct pinmux_ops msm_pinmux_ops
= {
196 .get_functions_count
= msm_get_functions_count
,
197 .get_function_name
= msm_get_function_name
,
198 .get_function_groups
= msm_get_function_groups
,
199 .enable
= msm_pinmux_enable
,
200 .disable
= msm_pinmux_disable
,
203 static int msm_config_reg(struct msm_pinctrl
*pctrl
,
204 const struct msm_pingroup
*g
,
211 case PIN_CONFIG_BIAS_DISABLE
:
216 case PIN_CONFIG_BIAS_PULL_DOWN
:
221 case PIN_CONFIG_BIAS_PULL_UP
:
226 case PIN_CONFIG_DRIVE_STRENGTH
:
232 dev_err(pctrl
->dev
, "Invalid config param %04x\n", param
);
237 dev_err(pctrl
->dev
, "Config param %04x not supported on group %s\n",
245 static int msm_config_get(struct pinctrl_dev
*pctldev
,
247 unsigned long *config
)
249 dev_err(pctldev
->dev
, "pin_config_set op not supported\n");
253 static int msm_config_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
254 unsigned long *configs
, unsigned num_configs
)
256 dev_err(pctldev
->dev
, "pin_config_set op not supported\n");
260 #define MSM_NO_PULL 0
261 #define MSM_PULL_DOWN 1
262 #define MSM_PULL_UP 3
264 static const unsigned msm_regval_to_drive
[] = { 2, 4, 6, 8, 10, 12, 14, 16 };
265 static const unsigned msm_drive_to_regval
[] = { -1, -1, 0, -1, 1, -1, 2, -1, 3, -1, 4, -1, 5, -1, 6, -1, 7 };
267 static int msm_config_group_get(struct pinctrl_dev
*pctldev
,
269 unsigned long *config
)
271 const struct msm_pingroup
*g
;
272 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
273 unsigned param
= pinconf_to_config_param(*config
);
281 g
= &pctrl
->soc
->groups
[group
];
283 ret
= msm_config_reg(pctrl
, g
, param
, ®
, &mask
, &bit
);
287 val
= readl(pctrl
->regs
+ reg
);
288 arg
= (val
>> bit
) & mask
;
290 /* Convert register value to pinconf value */
292 case PIN_CONFIG_BIAS_DISABLE
:
293 arg
= arg
== MSM_NO_PULL
;
295 case PIN_CONFIG_BIAS_PULL_DOWN
:
296 arg
= arg
== MSM_PULL_DOWN
;
298 case PIN_CONFIG_BIAS_PULL_UP
:
299 arg
= arg
== MSM_PULL_UP
;
301 case PIN_CONFIG_DRIVE_STRENGTH
:
302 arg
= msm_regval_to_drive
[arg
];
305 dev_err(pctrl
->dev
, "Unsupported config parameter: %x\n",
310 *config
= pinconf_to_config_packed(param
, arg
);
315 static int msm_config_group_set(struct pinctrl_dev
*pctldev
,
317 unsigned long *configs
,
318 unsigned num_configs
)
320 const struct msm_pingroup
*g
;
321 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
332 g
= &pctrl
->soc
->groups
[group
];
334 for (i
= 0; i
< num_configs
; i
++) {
335 param
= pinconf_to_config_param(configs
[i
]);
336 arg
= pinconf_to_config_argument(configs
[i
]);
338 ret
= msm_config_reg(pctrl
, g
, param
, ®
, &mask
, &bit
);
342 /* Convert pinconf values to register values */
344 case PIN_CONFIG_BIAS_DISABLE
:
347 case PIN_CONFIG_BIAS_PULL_DOWN
:
350 case PIN_CONFIG_BIAS_PULL_UP
:
353 case PIN_CONFIG_DRIVE_STRENGTH
:
354 /* Check for invalid values */
355 if (arg
>= ARRAY_SIZE(msm_drive_to_regval
))
358 arg
= msm_drive_to_regval
[arg
];
361 dev_err(pctrl
->dev
, "Unsupported config parameter: %x\n",
366 /* Range-check user-supplied value */
368 dev_err(pctrl
->dev
, "config %x: %x is invalid\n", param
, arg
);
372 spin_lock_irqsave(&pctrl
->lock
, flags
);
373 val
= readl(pctrl
->regs
+ reg
);
374 val
&= ~(mask
<< bit
);
376 writel(val
, pctrl
->regs
+ reg
);
377 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
383 static const struct pinconf_ops msm_pinconf_ops
= {
384 .pin_config_get
= msm_config_get
,
385 .pin_config_set
= msm_config_set
,
386 .pin_config_group_get
= msm_config_group_get
,
387 .pin_config_group_set
= msm_config_group_set
,
390 static struct pinctrl_desc msm_pinctrl_desc
= {
391 .pctlops
= &msm_pinctrl_ops
,
392 .pmxops
= &msm_pinmux_ops
,
393 .confops
= &msm_pinconf_ops
,
394 .owner
= THIS_MODULE
,
397 static int msm_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
399 const struct msm_pingroup
*g
;
400 struct msm_pinctrl
*pctrl
= container_of(chip
, struct msm_pinctrl
, chip
);
404 g
= &pctrl
->soc
->groups
[offset
];
405 if (WARN_ON(g
->io_reg
< 0))
408 spin_lock_irqsave(&pctrl
->lock
, flags
);
410 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
411 val
&= ~BIT(g
->oe_bit
);
412 writel(val
, pctrl
->regs
+ g
->ctl_reg
);
414 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
419 static int msm_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
, int value
)
421 const struct msm_pingroup
*g
;
422 struct msm_pinctrl
*pctrl
= container_of(chip
, struct msm_pinctrl
, chip
);
426 g
= &pctrl
->soc
->groups
[offset
];
427 if (WARN_ON(g
->io_reg
< 0))
430 spin_lock_irqsave(&pctrl
->lock
, flags
);
432 val
= readl(pctrl
->regs
+ g
->io_reg
);
434 val
|= BIT(g
->out_bit
);
436 val
&= ~BIT(g
->out_bit
);
437 writel(val
, pctrl
->regs
+ g
->io_reg
);
439 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
440 val
|= BIT(g
->oe_bit
);
441 writel(val
, pctrl
->regs
+ g
->ctl_reg
);
443 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
448 static int msm_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
450 const struct msm_pingroup
*g
;
451 struct msm_pinctrl
*pctrl
= container_of(chip
, struct msm_pinctrl
, chip
);
454 g
= &pctrl
->soc
->groups
[offset
];
455 if (WARN_ON(g
->io_reg
< 0))
458 val
= readl(pctrl
->regs
+ g
->io_reg
);
459 return !!(val
& BIT(g
->in_bit
));
462 static void msm_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
464 const struct msm_pingroup
*g
;
465 struct msm_pinctrl
*pctrl
= container_of(chip
, struct msm_pinctrl
, chip
);
469 g
= &pctrl
->soc
->groups
[offset
];
470 if (WARN_ON(g
->io_reg
< 0))
473 spin_lock_irqsave(&pctrl
->lock
, flags
);
475 val
= readl(pctrl
->regs
+ g
->io_reg
);
477 val
|= BIT(g
->out_bit
);
479 val
&= ~BIT(g
->out_bit
);
480 writel(val
, pctrl
->regs
+ g
->io_reg
);
482 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
485 static int msm_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
487 struct msm_pinctrl
*pctrl
= container_of(chip
, struct msm_pinctrl
, chip
);
489 return irq_find_mapping(pctrl
->domain
, offset
);
492 static int msm_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
494 int gpio
= chip
->base
+ offset
;
495 return pinctrl_request_gpio(gpio
);
498 static void msm_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
500 int gpio
= chip
->base
+ offset
;
501 return pinctrl_free_gpio(gpio
);
504 #ifdef CONFIG_DEBUG_FS
505 #include <linux/seq_file.h>
507 static void msm_gpio_dbg_show_one(struct seq_file
*s
,
508 struct pinctrl_dev
*pctldev
,
509 struct gpio_chip
*chip
,
513 const struct msm_pingroup
*g
;
514 struct msm_pinctrl
*pctrl
= container_of(chip
, struct msm_pinctrl
, chip
);
521 static const char * const pulls
[] = {
528 g
= &pctrl
->soc
->groups
[offset
];
529 ctl_reg
= readl(pctrl
->regs
+ g
->ctl_reg
);
531 is_out
= !!(ctl_reg
& BIT(g
->oe_bit
));
532 func
= (ctl_reg
>> g
->mux_bit
) & 7;
533 drive
= (ctl_reg
>> g
->drv_bit
) & 7;
534 pull
= (ctl_reg
>> g
->pull_bit
) & 3;
536 seq_printf(s
, " %-8s: %-3s %d", g
->name
, is_out
? "out" : "in", func
);
537 seq_printf(s
, " %dmA", msm_regval_to_drive
[drive
]);
538 seq_printf(s
, " %s", pulls
[pull
]);
541 static void msm_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
543 unsigned gpio
= chip
->base
;
546 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++) {
547 msm_gpio_dbg_show_one(s
, NULL
, chip
, i
, gpio
);
553 #define msm_gpio_dbg_show NULL
556 static struct gpio_chip msm_gpio_template
= {
557 .direction_input
= msm_gpio_direction_input
,
558 .direction_output
= msm_gpio_direction_output
,
561 .to_irq
= msm_gpio_to_irq
,
562 .request
= msm_gpio_request
,
563 .free
= msm_gpio_free
,
564 .dbg_show
= msm_gpio_dbg_show
,
567 /* For dual-edge interrupts in software, since some hardware has no
570 * At appropriate moments, this function may be called to flip the polarity
571 * settings of both-edge irq lines to try and catch the next edge.
573 * The attempt is considered successful if:
574 * - the status bit goes high, indicating that an edge was caught, or
575 * - the input value of the gpio doesn't change during the attempt.
576 * If the value changes twice during the process, that would cause the first
577 * test to fail but would force the second, as two opposite
578 * transitions would cause a detection no matter the polarity setting.
580 * The do-loop tries to sledge-hammer closed the timing hole between
581 * the initial value-read and the polarity-write - if the line value changes
582 * during that window, an interrupt is lost, the new polarity setting is
583 * incorrect, and the first success test will fail, causing a retry.
585 * Algorithm comes from Google's msmgpio driver.
587 static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl
*pctrl
,
588 const struct msm_pingroup
*g
,
591 int loop_limit
= 100;
592 unsigned val
, val2
, intstat
;
596 val
= readl(pctrl
->regs
+ g
->io_reg
) & BIT(g
->in_bit
);
598 pol
= readl(pctrl
->regs
+ g
->intr_cfg_reg
);
599 pol
^= BIT(g
->intr_polarity_bit
);
600 writel(pol
, pctrl
->regs
+ g
->intr_cfg_reg
);
602 val2
= readl(pctrl
->regs
+ g
->io_reg
) & BIT(g
->in_bit
);
603 intstat
= readl(pctrl
->regs
+ g
->intr_status_reg
);
604 if (intstat
|| (val
== val2
))
606 } while (loop_limit
-- > 0);
607 dev_err(pctrl
->dev
, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
611 static void msm_gpio_irq_mask(struct irq_data
*d
)
613 const struct msm_pingroup
*g
;
614 struct msm_pinctrl
*pctrl
;
618 pctrl
= irq_data_get_irq_chip_data(d
);
619 g
= &pctrl
->soc
->groups
[d
->hwirq
];
620 if (WARN_ON(g
->intr_cfg_reg
< 0))
623 spin_lock_irqsave(&pctrl
->lock
, flags
);
625 val
= readl(pctrl
->regs
+ g
->intr_cfg_reg
);
626 val
&= ~BIT(g
->intr_enable_bit
);
627 writel(val
, pctrl
->regs
+ g
->intr_cfg_reg
);
629 clear_bit(d
->hwirq
, pctrl
->enabled_irqs
);
631 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
634 static void msm_gpio_irq_unmask(struct irq_data
*d
)
636 const struct msm_pingroup
*g
;
637 struct msm_pinctrl
*pctrl
;
641 pctrl
= irq_data_get_irq_chip_data(d
);
642 g
= &pctrl
->soc
->groups
[d
->hwirq
];
643 if (WARN_ON(g
->intr_status_reg
< 0))
646 spin_lock_irqsave(&pctrl
->lock
, flags
);
648 val
= readl(pctrl
->regs
+ g
->intr_status_reg
);
649 val
&= ~BIT(g
->intr_status_bit
);
650 writel(val
, pctrl
->regs
+ g
->intr_status_reg
);
652 val
= readl(pctrl
->regs
+ g
->intr_cfg_reg
);
653 val
|= BIT(g
->intr_enable_bit
);
654 writel(val
, pctrl
->regs
+ g
->intr_cfg_reg
);
656 set_bit(d
->hwirq
, pctrl
->enabled_irqs
);
658 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
661 static void msm_gpio_irq_ack(struct irq_data
*d
)
663 const struct msm_pingroup
*g
;
664 struct msm_pinctrl
*pctrl
;
668 pctrl
= irq_data_get_irq_chip_data(d
);
669 g
= &pctrl
->soc
->groups
[d
->hwirq
];
670 if (WARN_ON(g
->intr_status_reg
< 0))
673 spin_lock_irqsave(&pctrl
->lock
, flags
);
675 val
= readl(pctrl
->regs
+ g
->intr_status_reg
);
676 val
&= ~BIT(g
->intr_status_bit
);
677 writel(val
, pctrl
->regs
+ g
->intr_status_reg
);
679 if (test_bit(d
->hwirq
, pctrl
->dual_edge_irqs
))
680 msm_gpio_update_dual_edge_pos(pctrl
, g
, d
);
682 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
685 #define INTR_TARGET_PROC_APPS 4
687 static int msm_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
689 const struct msm_pingroup
*g
;
690 struct msm_pinctrl
*pctrl
;
694 pctrl
= irq_data_get_irq_chip_data(d
);
695 g
= &pctrl
->soc
->groups
[d
->hwirq
];
696 if (WARN_ON(g
->intr_cfg_reg
< 0))
699 spin_lock_irqsave(&pctrl
->lock
, flags
);
702 * For hw without possibility of detecting both edges
704 if (g
->intr_detection_width
== 1 && type
== IRQ_TYPE_EDGE_BOTH
)
705 set_bit(d
->hwirq
, pctrl
->dual_edge_irqs
);
707 clear_bit(d
->hwirq
, pctrl
->dual_edge_irqs
);
709 /* Route interrupts to application cpu */
710 val
= readl(pctrl
->regs
+ g
->intr_target_reg
);
711 val
&= ~(7 << g
->intr_target_bit
);
712 val
|= INTR_TARGET_PROC_APPS
<< g
->intr_target_bit
;
713 writel(val
, pctrl
->regs
+ g
->intr_target_reg
);
715 /* Update configuration for gpio.
716 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
717 * internal circuitry of TLMM, toggling the RAW_STATUS
718 * could cause the INTR_STATUS to be set for EDGE interrupts.
720 val
= readl(pctrl
->regs
+ g
->intr_cfg_reg
);
721 val
|= BIT(g
->intr_raw_status_bit
);
722 if (g
->intr_detection_width
== 2) {
723 val
&= ~(3 << g
->intr_detection_bit
);
724 val
&= ~(1 << g
->intr_polarity_bit
);
726 case IRQ_TYPE_EDGE_RISING
:
727 val
|= 1 << g
->intr_detection_bit
;
728 val
|= BIT(g
->intr_polarity_bit
);
730 case IRQ_TYPE_EDGE_FALLING
:
731 val
|= 2 << g
->intr_detection_bit
;
732 val
|= BIT(g
->intr_polarity_bit
);
734 case IRQ_TYPE_EDGE_BOTH
:
735 val
|= 3 << g
->intr_detection_bit
;
736 val
|= BIT(g
->intr_polarity_bit
);
738 case IRQ_TYPE_LEVEL_LOW
:
740 case IRQ_TYPE_LEVEL_HIGH
:
741 val
|= BIT(g
->intr_polarity_bit
);
744 } else if (g
->intr_detection_width
== 1) {
745 val
&= ~(1 << g
->intr_detection_bit
);
746 val
&= ~(1 << g
->intr_polarity_bit
);
748 case IRQ_TYPE_EDGE_RISING
:
749 val
|= BIT(g
->intr_detection_bit
);
750 val
|= BIT(g
->intr_polarity_bit
);
752 case IRQ_TYPE_EDGE_FALLING
:
753 val
|= BIT(g
->intr_detection_bit
);
755 case IRQ_TYPE_EDGE_BOTH
:
756 val
|= BIT(g
->intr_detection_bit
);
758 case IRQ_TYPE_LEVEL_LOW
:
760 case IRQ_TYPE_LEVEL_HIGH
:
761 val
|= BIT(g
->intr_polarity_bit
);
767 writel(val
, pctrl
->regs
+ g
->intr_cfg_reg
);
769 if (test_bit(d
->hwirq
, pctrl
->dual_edge_irqs
))
770 msm_gpio_update_dual_edge_pos(pctrl
, g
, d
);
772 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
774 if (type
& (IRQ_TYPE_LEVEL_LOW
| IRQ_TYPE_LEVEL_HIGH
))
775 __irq_set_handler_locked(d
->irq
, handle_level_irq
);
776 else if (type
& (IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
))
777 __irq_set_handler_locked(d
->irq
, handle_edge_irq
);
782 static int msm_gpio_irq_set_wake(struct irq_data
*d
, unsigned int on
)
784 struct msm_pinctrl
*pctrl
;
788 pctrl
= irq_data_get_irq_chip_data(d
);
789 ngpio
= pctrl
->chip
.ngpio
;
791 spin_lock_irqsave(&pctrl
->lock
, flags
);
794 if (bitmap_empty(pctrl
->wake_irqs
, ngpio
))
795 enable_irq_wake(pctrl
->irq
);
796 set_bit(d
->hwirq
, pctrl
->wake_irqs
);
798 clear_bit(d
->hwirq
, pctrl
->wake_irqs
);
799 if (bitmap_empty(pctrl
->wake_irqs
, ngpio
))
800 disable_irq_wake(pctrl
->irq
);
803 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
808 static unsigned int msm_gpio_irq_startup(struct irq_data
*d
)
810 struct msm_pinctrl
*pctrl
= irq_data_get_irq_chip_data(d
);
812 if (gpio_lock_as_irq(&pctrl
->chip
, d
->hwirq
)) {
813 dev_err(pctrl
->dev
, "unable to lock HW IRQ %lu for IRQ\n",
816 msm_gpio_irq_unmask(d
);
820 static void msm_gpio_irq_shutdown(struct irq_data
*d
)
822 struct msm_pinctrl
*pctrl
= irq_data_get_irq_chip_data(d
);
824 msm_gpio_irq_mask(d
);
825 gpio_unlock_as_irq(&pctrl
->chip
, d
->hwirq
);
828 static struct irq_chip msm_gpio_irq_chip
= {
830 .irq_mask
= msm_gpio_irq_mask
,
831 .irq_unmask
= msm_gpio_irq_unmask
,
832 .irq_ack
= msm_gpio_irq_ack
,
833 .irq_set_type
= msm_gpio_irq_set_type
,
834 .irq_set_wake
= msm_gpio_irq_set_wake
,
835 .irq_startup
= msm_gpio_irq_startup
,
836 .irq_shutdown
= msm_gpio_irq_shutdown
,
839 static void msm_gpio_irq_handler(unsigned int irq
, struct irq_desc
*desc
)
841 const struct msm_pingroup
*g
;
842 struct msm_pinctrl
*pctrl
= irq_desc_get_handler_data(desc
);
843 struct irq_chip
*chip
= irq_get_chip(irq
);
849 chained_irq_enter(chip
, desc
);
852 * Each pin has it's own IRQ status register, so use
853 * enabled_irq bitmap to limit the number of reads.
855 for_each_set_bit(i
, pctrl
->enabled_irqs
, pctrl
->chip
.ngpio
) {
856 g
= &pctrl
->soc
->groups
[i
];
857 val
= readl(pctrl
->regs
+ g
->intr_status_reg
);
858 if (val
& BIT(g
->intr_status_bit
)) {
859 irq_pin
= irq_find_mapping(pctrl
->domain
, i
);
860 generic_handle_irq(irq_pin
);
865 /* No interrupts were flagged */
867 handle_bad_irq(irq
, desc
);
869 chained_irq_exit(chip
, desc
);
872 static int msm_gpio_init(struct msm_pinctrl
*pctrl
)
874 struct gpio_chip
*chip
;
882 chip
->ngpio
= pctrl
->soc
->ngpios
;
883 chip
->label
= dev_name(pctrl
->dev
);
884 chip
->dev
= pctrl
->dev
;
885 chip
->owner
= THIS_MODULE
;
886 chip
->of_node
= pctrl
->dev
->of_node
;
888 ret
= gpiochip_add(&pctrl
->chip
);
890 dev_err(pctrl
->dev
, "Failed register gpiochip\n");
894 ret
= gpiochip_add_pin_range(&pctrl
->chip
, dev_name(pctrl
->dev
), 0, 0, chip
->ngpio
);
896 dev_err(pctrl
->dev
, "Failed to add pin range\n");
900 pctrl
->domain
= irq_domain_add_linear(pctrl
->dev
->of_node
, chip
->ngpio
,
901 &irq_domain_simple_ops
, NULL
);
902 if (!pctrl
->domain
) {
903 dev_err(pctrl
->dev
, "Failed to register irq domain\n");
904 r
= gpiochip_remove(&pctrl
->chip
);
908 for (i
= 0; i
< chip
->ngpio
; i
++) {
909 irq
= irq_create_mapping(pctrl
->domain
, i
);
910 irq_set_chip_and_handler(irq
, &msm_gpio_irq_chip
, handle_edge_irq
);
911 irq_set_chip_data(irq
, pctrl
);
914 irq_set_handler_data(pctrl
->irq
, pctrl
);
915 irq_set_chained_handler(pctrl
->irq
, msm_gpio_irq_handler
);
920 int msm_pinctrl_probe(struct platform_device
*pdev
,
921 const struct msm_pinctrl_soc_data
*soc_data
)
923 struct msm_pinctrl
*pctrl
;
924 struct resource
*res
;
927 pctrl
= devm_kzalloc(&pdev
->dev
, sizeof(*pctrl
), GFP_KERNEL
);
929 dev_err(&pdev
->dev
, "Can't allocate msm_pinctrl\n");
932 pctrl
->dev
= &pdev
->dev
;
933 pctrl
->soc
= soc_data
;
934 pctrl
->chip
= msm_gpio_template
;
936 spin_lock_init(&pctrl
->lock
);
938 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
939 pctrl
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
940 if (IS_ERR(pctrl
->regs
))
941 return PTR_ERR(pctrl
->regs
);
943 pctrl
->irq
= platform_get_irq(pdev
, 0);
944 if (pctrl
->irq
< 0) {
945 dev_err(&pdev
->dev
, "No interrupt defined for msmgpio\n");
949 msm_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
950 msm_pinctrl_desc
.pins
= pctrl
->soc
->pins
;
951 msm_pinctrl_desc
.npins
= pctrl
->soc
->npins
;
952 pctrl
->pctrl
= pinctrl_register(&msm_pinctrl_desc
, &pdev
->dev
, pctrl
);
954 dev_err(&pdev
->dev
, "Couldn't register pinctrl driver\n");
958 ret
= msm_gpio_init(pctrl
);
960 pinctrl_unregister(pctrl
->pctrl
);
964 platform_set_drvdata(pdev
, pctrl
);
966 dev_dbg(&pdev
->dev
, "Probed Qualcomm pinctrl driver\n");
970 EXPORT_SYMBOL(msm_pinctrl_probe
);
972 int msm_pinctrl_remove(struct platform_device
*pdev
)
974 struct msm_pinctrl
*pctrl
= platform_get_drvdata(pdev
);
977 ret
= gpiochip_remove(&pctrl
->chip
);
979 dev_err(&pdev
->dev
, "Failed to remove gpiochip\n");
983 irq_set_chained_handler(pctrl
->irq
, NULL
);
984 irq_domain_remove(pctrl
->domain
);
985 pinctrl_unregister(pctrl
->pctrl
);
989 EXPORT_SYMBOL(msm_pinctrl_remove
);