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>
17 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/pinctrl/machine.h>
21 #include <linux/pinctrl/pinctrl.h>
22 #include <linux/pinctrl/pinmux.h>
23 #include <linux/pinctrl/pinconf.h>
24 #include <linux/pinctrl/pinconf-generic.h>
25 #include <linux/slab.h>
26 #include <linux/gpio.h>
27 #include <linux/interrupt.h>
28 #include <linux/spinlock.h>
31 #include "../pinconf.h"
32 #include "pinctrl-msm.h"
33 #include "../pinctrl-utils.h"
35 #define MAX_NR_GPIO 300
38 * struct msm_pinctrl - state for a pinctrl-msm device
39 * @dev: device handle.
40 * @pctrl: pinctrl handle.
41 * @chip: gpiochip handle.
42 * @irq: parent irq for the TLMM irq_chip.
43 * @lock: Spinlock to protect register resources as well
44 * as msm_pinctrl data structures.
45 * @enabled_irqs: Bitmap of currently enabled irqs.
46 * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
48 * @soc; Reference to soc_data of platform specific data.
49 * @regs: Base address for the TLMM register map.
53 struct pinctrl_dev
*pctrl
;
54 struct gpio_chip chip
;
59 DECLARE_BITMAP(dual_edge_irqs
, MAX_NR_GPIO
);
60 DECLARE_BITMAP(enabled_irqs
, MAX_NR_GPIO
);
62 const struct msm_pinctrl_soc_data
*soc
;
66 static inline struct msm_pinctrl
*to_msm_pinctrl(struct gpio_chip
*gc
)
68 return container_of(gc
, struct msm_pinctrl
, chip
);
71 static int msm_get_groups_count(struct pinctrl_dev
*pctldev
)
73 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
75 return pctrl
->soc
->ngroups
;
78 static const char *msm_get_group_name(struct pinctrl_dev
*pctldev
,
81 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
83 return pctrl
->soc
->groups
[group
].name
;
86 static int msm_get_group_pins(struct pinctrl_dev
*pctldev
,
88 const unsigned **pins
,
91 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
93 *pins
= pctrl
->soc
->groups
[group
].pins
;
94 *num_pins
= pctrl
->soc
->groups
[group
].npins
;
98 static const struct pinctrl_ops msm_pinctrl_ops
= {
99 .get_groups_count
= msm_get_groups_count
,
100 .get_group_name
= msm_get_group_name
,
101 .get_group_pins
= msm_get_group_pins
,
102 .dt_node_to_map
= pinconf_generic_dt_node_to_map_group
,
103 .dt_free_map
= pinctrl_utils_dt_free_map
,
106 static int msm_get_functions_count(struct pinctrl_dev
*pctldev
)
108 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
110 return pctrl
->soc
->nfunctions
;
113 static const char *msm_get_function_name(struct pinctrl_dev
*pctldev
,
116 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
118 return pctrl
->soc
->functions
[function
].name
;
121 static int msm_get_function_groups(struct pinctrl_dev
*pctldev
,
123 const char * const **groups
,
124 unsigned * const num_groups
)
126 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
128 *groups
= pctrl
->soc
->functions
[function
].groups
;
129 *num_groups
= pctrl
->soc
->functions
[function
].ngroups
;
133 static int msm_pinmux_enable(struct pinctrl_dev
*pctldev
,
137 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
138 const struct msm_pingroup
*g
;
143 g
= &pctrl
->soc
->groups
[group
];
145 if (WARN_ON(g
->mux_bit
< 0))
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
);
159 val
&= ~(0x7 << g
->mux_bit
);
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 .enable
= msm_pinmux_enable
,
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
:
198 dev_err(pctrl
->dev
, "Invalid config param %04x\n", param
);
205 static int msm_config_get(struct pinctrl_dev
*pctldev
,
207 unsigned long *config
)
209 dev_err(pctldev
->dev
, "pin_config_set op not supported\n");
213 static int msm_config_set(struct pinctrl_dev
*pctldev
, unsigned int pin
,
214 unsigned long *configs
, unsigned num_configs
)
216 dev_err(pctldev
->dev
, "pin_config_set op not supported\n");
220 #define MSM_NO_PULL 0
221 #define MSM_PULL_DOWN 1
223 #define MSM_PULL_UP 3
225 static unsigned msm_regval_to_drive(u32 val
)
227 return (val
+ 1) * 2;
230 static int msm_config_group_get(struct pinctrl_dev
*pctldev
,
232 unsigned long *config
)
234 const struct msm_pingroup
*g
;
235 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
236 unsigned param
= pinconf_to_config_param(*config
);
243 g
= &pctrl
->soc
->groups
[group
];
245 ret
= msm_config_reg(pctrl
, g
, param
, &mask
, &bit
);
249 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
250 arg
= (val
>> bit
) & mask
;
252 /* Convert register value to pinconf value */
254 case PIN_CONFIG_BIAS_DISABLE
:
255 arg
= arg
== MSM_NO_PULL
;
257 case PIN_CONFIG_BIAS_PULL_DOWN
:
258 arg
= arg
== MSM_PULL_DOWN
;
260 case PIN_CONFIG_BIAS_BUS_HOLD
:
261 arg
= arg
== MSM_KEEPER
;
263 case PIN_CONFIG_BIAS_PULL_UP
:
264 arg
= arg
== MSM_PULL_UP
;
266 case PIN_CONFIG_DRIVE_STRENGTH
:
267 arg
= msm_regval_to_drive(arg
);
269 case PIN_CONFIG_OUTPUT
:
270 /* Pin is not output */
274 val
= readl(pctrl
->regs
+ g
->io_reg
);
275 arg
= !!(val
& BIT(g
->in_bit
));
278 dev_err(pctrl
->dev
, "Unsupported config parameter: %x\n",
283 *config
= pinconf_to_config_packed(param
, arg
);
288 static int msm_config_group_set(struct pinctrl_dev
*pctldev
,
290 unsigned long *configs
,
291 unsigned num_configs
)
293 const struct msm_pingroup
*g
;
294 struct msm_pinctrl
*pctrl
= pinctrl_dev_get_drvdata(pctldev
);
304 g
= &pctrl
->soc
->groups
[group
];
306 for (i
= 0; i
< num_configs
; i
++) {
307 param
= pinconf_to_config_param(configs
[i
]);
308 arg
= pinconf_to_config_argument(configs
[i
]);
310 ret
= msm_config_reg(pctrl
, g
, param
, &mask
, &bit
);
314 /* Convert pinconf values to register values */
316 case PIN_CONFIG_BIAS_DISABLE
:
319 case PIN_CONFIG_BIAS_PULL_DOWN
:
322 case PIN_CONFIG_BIAS_BUS_HOLD
:
325 case PIN_CONFIG_BIAS_PULL_UP
:
328 case PIN_CONFIG_DRIVE_STRENGTH
:
329 /* Check for invalid values */
330 if (arg
> 16 || arg
< 2 || (arg
% 2) != 0)
335 case PIN_CONFIG_OUTPUT
:
336 /* set output value */
337 spin_lock_irqsave(&pctrl
->lock
, flags
);
338 val
= readl(pctrl
->regs
+ g
->io_reg
);
340 val
|= BIT(g
->out_bit
);
342 val
&= ~BIT(g
->out_bit
);
343 writel(val
, pctrl
->regs
+ g
->io_reg
);
344 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
350 dev_err(pctrl
->dev
, "Unsupported config parameter: %x\n",
355 /* Range-check user-supplied value */
357 dev_err(pctrl
->dev
, "config %x: %x is invalid\n", param
, arg
);
361 spin_lock_irqsave(&pctrl
->lock
, flags
);
362 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
363 val
&= ~(mask
<< bit
);
365 writel(val
, pctrl
->regs
+ g
->ctl_reg
);
366 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
372 static const struct pinconf_ops msm_pinconf_ops
= {
373 .pin_config_get
= msm_config_get
,
374 .pin_config_set
= msm_config_set
,
375 .pin_config_group_get
= msm_config_group_get
,
376 .pin_config_group_set
= msm_config_group_set
,
379 static struct pinctrl_desc msm_pinctrl_desc
= {
380 .pctlops
= &msm_pinctrl_ops
,
381 .pmxops
= &msm_pinmux_ops
,
382 .confops
= &msm_pinconf_ops
,
383 .owner
= THIS_MODULE
,
386 static int msm_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
388 const struct msm_pingroup
*g
;
389 struct msm_pinctrl
*pctrl
= container_of(chip
, struct msm_pinctrl
, chip
);
393 g
= &pctrl
->soc
->groups
[offset
];
395 spin_lock_irqsave(&pctrl
->lock
, flags
);
397 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
398 val
&= ~BIT(g
->oe_bit
);
399 writel(val
, pctrl
->regs
+ g
->ctl_reg
);
401 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
406 static int msm_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
, int value
)
408 const struct msm_pingroup
*g
;
409 struct msm_pinctrl
*pctrl
= container_of(chip
, struct msm_pinctrl
, chip
);
413 g
= &pctrl
->soc
->groups
[offset
];
415 spin_lock_irqsave(&pctrl
->lock
, flags
);
417 val
= readl(pctrl
->regs
+ g
->io_reg
);
419 val
|= BIT(g
->out_bit
);
421 val
&= ~BIT(g
->out_bit
);
422 writel(val
, pctrl
->regs
+ g
->io_reg
);
424 val
= readl(pctrl
->regs
+ g
->ctl_reg
);
425 val
|= BIT(g
->oe_bit
);
426 writel(val
, pctrl
->regs
+ g
->ctl_reg
);
428 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
433 static int msm_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
435 const struct msm_pingroup
*g
;
436 struct msm_pinctrl
*pctrl
= container_of(chip
, struct msm_pinctrl
, chip
);
439 g
= &pctrl
->soc
->groups
[offset
];
441 val
= readl(pctrl
->regs
+ g
->io_reg
);
442 return !!(val
& BIT(g
->in_bit
));
445 static void msm_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int value
)
447 const struct msm_pingroup
*g
;
448 struct msm_pinctrl
*pctrl
= container_of(chip
, struct msm_pinctrl
, chip
);
452 g
= &pctrl
->soc
->groups
[offset
];
454 spin_lock_irqsave(&pctrl
->lock
, flags
);
456 val
= readl(pctrl
->regs
+ g
->io_reg
);
458 val
|= BIT(g
->out_bit
);
460 val
&= ~BIT(g
->out_bit
);
461 writel(val
, pctrl
->regs
+ g
->io_reg
);
463 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
466 static int msm_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
468 int gpio
= chip
->base
+ offset
;
469 return pinctrl_request_gpio(gpio
);
472 static void msm_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
474 int gpio
= chip
->base
+ offset
;
475 return pinctrl_free_gpio(gpio
);
478 #ifdef CONFIG_DEBUG_FS
479 #include <linux/seq_file.h>
481 static void msm_gpio_dbg_show_one(struct seq_file
*s
,
482 struct pinctrl_dev
*pctldev
,
483 struct gpio_chip
*chip
,
487 const struct msm_pingroup
*g
;
488 struct msm_pinctrl
*pctrl
= container_of(chip
, struct msm_pinctrl
, chip
);
495 static const char * const pulls
[] = {
502 g
= &pctrl
->soc
->groups
[offset
];
503 ctl_reg
= readl(pctrl
->regs
+ g
->ctl_reg
);
505 is_out
= !!(ctl_reg
& BIT(g
->oe_bit
));
506 func
= (ctl_reg
>> g
->mux_bit
) & 7;
507 drive
= (ctl_reg
>> g
->drv_bit
) & 7;
508 pull
= (ctl_reg
>> g
->pull_bit
) & 3;
510 seq_printf(s
, " %-8s: %-3s %d", g
->name
, is_out
? "out" : "in", func
);
511 seq_printf(s
, " %dmA", msm_regval_to_drive(drive
));
512 seq_printf(s
, " %s", pulls
[pull
]);
515 static void msm_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
517 unsigned gpio
= chip
->base
;
520 for (i
= 0; i
< chip
->ngpio
; i
++, gpio
++) {
521 msm_gpio_dbg_show_one(s
, NULL
, chip
, i
, gpio
);
527 #define msm_gpio_dbg_show NULL
530 static struct gpio_chip msm_gpio_template
= {
531 .direction_input
= msm_gpio_direction_input
,
532 .direction_output
= msm_gpio_direction_output
,
535 .request
= msm_gpio_request
,
536 .free
= msm_gpio_free
,
537 .dbg_show
= msm_gpio_dbg_show
,
540 /* For dual-edge interrupts in software, since some hardware has no
543 * At appropriate moments, this function may be called to flip the polarity
544 * settings of both-edge irq lines to try and catch the next edge.
546 * The attempt is considered successful if:
547 * - the status bit goes high, indicating that an edge was caught, or
548 * - the input value of the gpio doesn't change during the attempt.
549 * If the value changes twice during the process, that would cause the first
550 * test to fail but would force the second, as two opposite
551 * transitions would cause a detection no matter the polarity setting.
553 * The do-loop tries to sledge-hammer closed the timing hole between
554 * the initial value-read and the polarity-write - if the line value changes
555 * during that window, an interrupt is lost, the new polarity setting is
556 * incorrect, and the first success test will fail, causing a retry.
558 * Algorithm comes from Google's msmgpio driver.
560 static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl
*pctrl
,
561 const struct msm_pingroup
*g
,
564 int loop_limit
= 100;
565 unsigned val
, val2
, intstat
;
569 val
= readl(pctrl
->regs
+ g
->io_reg
) & BIT(g
->in_bit
);
571 pol
= readl(pctrl
->regs
+ g
->intr_cfg_reg
);
572 pol
^= BIT(g
->intr_polarity_bit
);
573 writel(pol
, pctrl
->regs
+ g
->intr_cfg_reg
);
575 val2
= readl(pctrl
->regs
+ g
->io_reg
) & BIT(g
->in_bit
);
576 intstat
= readl(pctrl
->regs
+ g
->intr_status_reg
);
577 if (intstat
|| (val
== val2
))
579 } while (loop_limit
-- > 0);
580 dev_err(pctrl
->dev
, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
584 static void msm_gpio_irq_mask(struct irq_data
*d
)
586 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
587 struct msm_pinctrl
*pctrl
= to_msm_pinctrl(gc
);
588 const struct msm_pingroup
*g
;
592 g
= &pctrl
->soc
->groups
[d
->hwirq
];
594 spin_lock_irqsave(&pctrl
->lock
, flags
);
596 val
= readl(pctrl
->regs
+ g
->intr_cfg_reg
);
597 val
&= ~BIT(g
->intr_enable_bit
);
598 writel(val
, pctrl
->regs
+ g
->intr_cfg_reg
);
600 clear_bit(d
->hwirq
, pctrl
->enabled_irqs
);
602 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
605 static void msm_gpio_irq_unmask(struct irq_data
*d
)
607 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
608 struct msm_pinctrl
*pctrl
= to_msm_pinctrl(gc
);
609 const struct msm_pingroup
*g
;
613 g
= &pctrl
->soc
->groups
[d
->hwirq
];
615 spin_lock_irqsave(&pctrl
->lock
, flags
);
617 val
= readl(pctrl
->regs
+ g
->intr_status_reg
);
618 val
&= ~BIT(g
->intr_status_bit
);
619 writel(val
, pctrl
->regs
+ g
->intr_status_reg
);
621 val
= readl(pctrl
->regs
+ g
->intr_cfg_reg
);
622 val
|= BIT(g
->intr_enable_bit
);
623 writel(val
, pctrl
->regs
+ g
->intr_cfg_reg
);
625 set_bit(d
->hwirq
, pctrl
->enabled_irqs
);
627 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
630 static void msm_gpio_irq_ack(struct irq_data
*d
)
632 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
633 struct msm_pinctrl
*pctrl
= to_msm_pinctrl(gc
);
634 const struct msm_pingroup
*g
;
638 g
= &pctrl
->soc
->groups
[d
->hwirq
];
640 spin_lock_irqsave(&pctrl
->lock
, flags
);
642 val
= readl(pctrl
->regs
+ g
->intr_status_reg
);
643 if (g
->intr_ack_high
)
644 val
|= BIT(g
->intr_status_bit
);
646 val
&= ~BIT(g
->intr_status_bit
);
647 writel(val
, pctrl
->regs
+ g
->intr_status_reg
);
649 if (test_bit(d
->hwirq
, pctrl
->dual_edge_irqs
))
650 msm_gpio_update_dual_edge_pos(pctrl
, g
, d
);
652 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
655 #define INTR_TARGET_PROC_APPS 4
657 static int msm_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
659 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
660 struct msm_pinctrl
*pctrl
= to_msm_pinctrl(gc
);
661 const struct msm_pingroup
*g
;
665 g
= &pctrl
->soc
->groups
[d
->hwirq
];
667 spin_lock_irqsave(&pctrl
->lock
, flags
);
670 * For hw without possibility of detecting both edges
672 if (g
->intr_detection_width
== 1 && type
== IRQ_TYPE_EDGE_BOTH
)
673 set_bit(d
->hwirq
, pctrl
->dual_edge_irqs
);
675 clear_bit(d
->hwirq
, pctrl
->dual_edge_irqs
);
677 /* Route interrupts to application cpu */
678 val
= readl(pctrl
->regs
+ g
->intr_target_reg
);
679 val
&= ~(7 << g
->intr_target_bit
);
680 val
|= INTR_TARGET_PROC_APPS
<< g
->intr_target_bit
;
681 writel(val
, pctrl
->regs
+ g
->intr_target_reg
);
683 /* Update configuration for gpio.
684 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
685 * internal circuitry of TLMM, toggling the RAW_STATUS
686 * could cause the INTR_STATUS to be set for EDGE interrupts.
688 val
= readl(pctrl
->regs
+ g
->intr_cfg_reg
);
689 val
|= BIT(g
->intr_raw_status_bit
);
690 if (g
->intr_detection_width
== 2) {
691 val
&= ~(3 << g
->intr_detection_bit
);
692 val
&= ~(1 << g
->intr_polarity_bit
);
694 case IRQ_TYPE_EDGE_RISING
:
695 val
|= 1 << g
->intr_detection_bit
;
696 val
|= BIT(g
->intr_polarity_bit
);
698 case IRQ_TYPE_EDGE_FALLING
:
699 val
|= 2 << g
->intr_detection_bit
;
700 val
|= BIT(g
->intr_polarity_bit
);
702 case IRQ_TYPE_EDGE_BOTH
:
703 val
|= 3 << g
->intr_detection_bit
;
704 val
|= BIT(g
->intr_polarity_bit
);
706 case IRQ_TYPE_LEVEL_LOW
:
708 case IRQ_TYPE_LEVEL_HIGH
:
709 val
|= BIT(g
->intr_polarity_bit
);
712 } else if (g
->intr_detection_width
== 1) {
713 val
&= ~(1 << g
->intr_detection_bit
);
714 val
&= ~(1 << g
->intr_polarity_bit
);
716 case IRQ_TYPE_EDGE_RISING
:
717 val
|= BIT(g
->intr_detection_bit
);
718 val
|= BIT(g
->intr_polarity_bit
);
720 case IRQ_TYPE_EDGE_FALLING
:
721 val
|= BIT(g
->intr_detection_bit
);
723 case IRQ_TYPE_EDGE_BOTH
:
724 val
|= BIT(g
->intr_detection_bit
);
725 val
|= BIT(g
->intr_polarity_bit
);
727 case IRQ_TYPE_LEVEL_LOW
:
729 case IRQ_TYPE_LEVEL_HIGH
:
730 val
|= BIT(g
->intr_polarity_bit
);
736 writel(val
, pctrl
->regs
+ g
->intr_cfg_reg
);
738 if (test_bit(d
->hwirq
, pctrl
->dual_edge_irqs
))
739 msm_gpio_update_dual_edge_pos(pctrl
, g
, d
);
741 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
743 if (type
& (IRQ_TYPE_LEVEL_LOW
| IRQ_TYPE_LEVEL_HIGH
))
744 __irq_set_handler_locked(d
->irq
, handle_level_irq
);
745 else if (type
& (IRQ_TYPE_EDGE_FALLING
| IRQ_TYPE_EDGE_RISING
))
746 __irq_set_handler_locked(d
->irq
, handle_edge_irq
);
751 static int msm_gpio_irq_set_wake(struct irq_data
*d
, unsigned int on
)
753 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
754 struct msm_pinctrl
*pctrl
= to_msm_pinctrl(gc
);
757 spin_lock_irqsave(&pctrl
->lock
, flags
);
759 irq_set_irq_wake(pctrl
->irq
, on
);
761 spin_unlock_irqrestore(&pctrl
->lock
, flags
);
766 static struct irq_chip msm_gpio_irq_chip
= {
768 .irq_mask
= msm_gpio_irq_mask
,
769 .irq_unmask
= msm_gpio_irq_unmask
,
770 .irq_ack
= msm_gpio_irq_ack
,
771 .irq_set_type
= msm_gpio_irq_set_type
,
772 .irq_set_wake
= msm_gpio_irq_set_wake
,
775 static void msm_gpio_irq_handler(unsigned int irq
, struct irq_desc
*desc
)
777 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
778 const struct msm_pingroup
*g
;
779 struct msm_pinctrl
*pctrl
= to_msm_pinctrl(gc
);
780 struct irq_chip
*chip
= irq_get_chip(irq
);
786 chained_irq_enter(chip
, desc
);
789 * Each pin has it's own IRQ status register, so use
790 * enabled_irq bitmap to limit the number of reads.
792 for_each_set_bit(i
, pctrl
->enabled_irqs
, pctrl
->chip
.ngpio
) {
793 g
= &pctrl
->soc
->groups
[i
];
794 val
= readl(pctrl
->regs
+ g
->intr_status_reg
);
795 if (val
& BIT(g
->intr_status_bit
)) {
796 irq_pin
= irq_find_mapping(gc
->irqdomain
, i
);
797 generic_handle_irq(irq_pin
);
802 /* No interrupts were flagged */
804 handle_bad_irq(irq
, desc
);
806 chained_irq_exit(chip
, desc
);
809 static int msm_gpio_init(struct msm_pinctrl
*pctrl
)
811 struct gpio_chip
*chip
;
813 unsigned ngpio
= pctrl
->soc
->ngpios
;
815 if (WARN_ON(ngpio
> MAX_NR_GPIO
))
821 chip
->label
= dev_name(pctrl
->dev
);
822 chip
->dev
= pctrl
->dev
;
823 chip
->owner
= THIS_MODULE
;
824 chip
->of_node
= pctrl
->dev
->of_node
;
826 ret
= gpiochip_add(&pctrl
->chip
);
828 dev_err(pctrl
->dev
, "Failed register gpiochip\n");
832 ret
= gpiochip_add_pin_range(&pctrl
->chip
, dev_name(pctrl
->dev
), 0, 0, chip
->ngpio
);
834 dev_err(pctrl
->dev
, "Failed to add pin range\n");
838 ret
= gpiochip_irqchip_add(chip
,
844 dev_err(pctrl
->dev
, "Failed to add irqchip to gpiochip\n");
848 gpiochip_set_chained_irqchip(chip
, &msm_gpio_irq_chip
, pctrl
->irq
,
849 msm_gpio_irq_handler
);
854 int msm_pinctrl_probe(struct platform_device
*pdev
,
855 const struct msm_pinctrl_soc_data
*soc_data
)
857 struct msm_pinctrl
*pctrl
;
858 struct resource
*res
;
861 pctrl
= devm_kzalloc(&pdev
->dev
, sizeof(*pctrl
), GFP_KERNEL
);
863 dev_err(&pdev
->dev
, "Can't allocate msm_pinctrl\n");
866 pctrl
->dev
= &pdev
->dev
;
867 pctrl
->soc
= soc_data
;
868 pctrl
->chip
= msm_gpio_template
;
870 spin_lock_init(&pctrl
->lock
);
872 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
873 pctrl
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
874 if (IS_ERR(pctrl
->regs
))
875 return PTR_ERR(pctrl
->regs
);
877 pctrl
->irq
= platform_get_irq(pdev
, 0);
878 if (pctrl
->irq
< 0) {
879 dev_err(&pdev
->dev
, "No interrupt defined for msmgpio\n");
883 msm_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
884 msm_pinctrl_desc
.pins
= pctrl
->soc
->pins
;
885 msm_pinctrl_desc
.npins
= pctrl
->soc
->npins
;
886 pctrl
->pctrl
= pinctrl_register(&msm_pinctrl_desc
, &pdev
->dev
, pctrl
);
888 dev_err(&pdev
->dev
, "Couldn't register pinctrl driver\n");
892 ret
= msm_gpio_init(pctrl
);
894 pinctrl_unregister(pctrl
->pctrl
);
898 platform_set_drvdata(pdev
, pctrl
);
900 dev_dbg(&pdev
->dev
, "Probed Qualcomm pinctrl driver\n");
904 EXPORT_SYMBOL(msm_pinctrl_probe
);
906 int msm_pinctrl_remove(struct platform_device
*pdev
)
908 struct msm_pinctrl
*pctrl
= platform_get_drvdata(pdev
);
911 ret
= gpiochip_remove(&pctrl
->chip
);
913 dev_err(&pdev
->dev
, "Failed to remove gpiochip\n");
917 pinctrl_unregister(pctrl
->pctrl
);
921 EXPORT_SYMBOL(msm_pinctrl_remove
);