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/spinlock.h>
35 #include "pinctrl-msm.h"
36 #include "pinctrl-utils.h"
38 #define MAX_NR_GPIO 300
41 * struct msm_pinctrl - state for a pinctrl-msm device
42 * @dev: device handle.
43 * @pctrl: pinctrl handle.
44 * @domain: irqdomain handle.
45 * @chip: gpiochip handle.
46 * @irq: parent irq for the TLMM irq_chip.
47 * @lock: Spinlock to protect register resources as well
48 * as msm_pinctrl data structures.
49 * @enabled_irqs: Bitmap of currently enabled irqs.
50 * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
52 * @soc; Reference to soc_data of platform specific data.
53 * @regs: Base address for the TLMM register map.
57 struct pinctrl_dev *pctrl;
58 struct irq_domain *domain;
59 struct gpio_chip chip;
64 DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
65 DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
67 const struct msm_pinctrl_soc_data *soc;
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 < ARRAY_SIZE(g->funcs); i++) {
149 if (g->funcs[i] == function)
153 if (WARN_ON(i == ARRAY_SIZE(g->funcs)))
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 void msm_pinmux_disable(struct pinctrl_dev *pctldev,
172 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
173 const struct msm_pingroup *g;
177 g = &pctrl->soc->groups[group];
179 if (WARN_ON(g->mux_bit < 0))
182 spin_lock_irqsave(&pctrl->lock, flags);
184 /* Clear the mux bits to select gpio mode */
185 val = readl(pctrl->regs + g->ctl_reg);
186 val &= ~(0x7 << g->mux_bit);
187 writel(val, pctrl->regs + g->ctl_reg);
189 spin_unlock_irqrestore(&pctrl->lock, flags);
192 static const struct pinmux_ops msm_pinmux_ops = {
193 .get_functions_count = msm_get_functions_count,
194 .get_function_name = msm_get_function_name,
195 .get_function_groups = msm_get_function_groups,
196 .enable = msm_pinmux_enable,
197 .disable = msm_pinmux_disable,
200 static int msm_config_reg(struct msm_pinctrl *pctrl,
201 const struct msm_pingroup *g,
208 case PIN_CONFIG_BIAS_DISABLE:
213 case PIN_CONFIG_BIAS_PULL_DOWN:
218 case PIN_CONFIG_BIAS_PULL_UP:
223 case PIN_CONFIG_DRIVE_STRENGTH:
228 case PIN_CONFIG_OUTPUT:
234 dev_err(pctrl->dev, "Invalid config param %04x\n", param);
239 dev_err(pctrl->dev, "Config param %04x not supported on group %s\n",
247 static int msm_config_get(struct pinctrl_dev *pctldev,
249 unsigned long *config)
251 dev_err(pctldev->dev, "pin_config_set op not supported\n");
255 static int msm_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
256 unsigned long *configs, unsigned num_configs)
258 dev_err(pctldev->dev, "pin_config_set op not supported\n");
262 #define MSM_NO_PULL 0
263 #define MSM_PULL_DOWN 1
264 #define MSM_PULL_UP 3
266 static unsigned msm_regval_to_drive(u32 val)
268 return (val + 1) * 2;
271 static int msm_config_group_get(struct pinctrl_dev *pctldev,
273 unsigned long *config)
275 const struct msm_pingroup *g;
276 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
277 unsigned param = pinconf_to_config_param(*config);
285 g = &pctrl->soc->groups[group];
287 ret = msm_config_reg(pctrl, g, param, ®, &mask, &bit);
291 val = readl(pctrl->regs + reg);
292 arg = (val >> bit) & mask;
294 /* Convert register value to pinconf value */
296 case PIN_CONFIG_BIAS_DISABLE:
297 arg = arg == MSM_NO_PULL;
299 case PIN_CONFIG_BIAS_PULL_DOWN:
300 arg = arg == MSM_PULL_DOWN;
302 case PIN_CONFIG_BIAS_PULL_UP:
303 arg = arg == MSM_PULL_UP;
305 case PIN_CONFIG_DRIVE_STRENGTH:
306 arg = msm_regval_to_drive(arg);
308 case PIN_CONFIG_OUTPUT:
309 /* Pin is not output */
313 val = readl(pctrl->regs + g->io_reg);
314 arg = !!(val & BIT(g->in_bit));
317 dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
322 *config = pinconf_to_config_packed(param, arg);
327 static int msm_config_group_set(struct pinctrl_dev *pctldev,
329 unsigned long *configs,
330 unsigned num_configs)
332 const struct msm_pingroup *g;
333 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
344 g = &pctrl->soc->groups[group];
346 for (i = 0; i < num_configs; i++) {
347 param = pinconf_to_config_param(configs[i]);
348 arg = pinconf_to_config_argument(configs[i]);
350 ret = msm_config_reg(pctrl, g, param, ®, &mask, &bit);
354 /* Convert pinconf values to register values */
356 case PIN_CONFIG_BIAS_DISABLE:
359 case PIN_CONFIG_BIAS_PULL_DOWN:
362 case PIN_CONFIG_BIAS_PULL_UP:
365 case PIN_CONFIG_DRIVE_STRENGTH:
366 /* Check for invalid values */
367 if (arg > 16 || arg < 2 || (arg % 2) != 0)
372 case PIN_CONFIG_OUTPUT:
373 /* set output value */
374 spin_lock_irqsave(&pctrl->lock, flags);
375 val = readl(pctrl->regs + g->io_reg);
377 val |= BIT(g->out_bit);
379 val &= ~BIT(g->out_bit);
380 writel(val, pctrl->regs + g->io_reg);
381 spin_unlock_irqrestore(&pctrl->lock, flags);
387 dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
392 /* Range-check user-supplied value */
394 dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
398 spin_lock_irqsave(&pctrl->lock, flags);
399 val = readl(pctrl->regs + reg);
400 val &= ~(mask << bit);
402 writel(val, pctrl->regs + reg);
403 spin_unlock_irqrestore(&pctrl->lock, flags);
409 static const struct pinconf_ops msm_pinconf_ops = {
410 .pin_config_get = msm_config_get,
411 .pin_config_set = msm_config_set,
412 .pin_config_group_get = msm_config_group_get,
413 .pin_config_group_set = msm_config_group_set,
416 static struct pinctrl_desc msm_pinctrl_desc = {
417 .pctlops = &msm_pinctrl_ops,
418 .pmxops = &msm_pinmux_ops,
419 .confops = &msm_pinconf_ops,
420 .owner = THIS_MODULE,
423 static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
425 const struct msm_pingroup *g;
426 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
430 g = &pctrl->soc->groups[offset];
431 if (WARN_ON(g->io_reg < 0))
434 spin_lock_irqsave(&pctrl->lock, flags);
436 val = readl(pctrl->regs + g->ctl_reg);
437 val &= ~BIT(g->oe_bit);
438 writel(val, pctrl->regs + g->ctl_reg);
440 spin_unlock_irqrestore(&pctrl->lock, flags);
445 static int msm_gpio_direction_output(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];
453 if (WARN_ON(g->io_reg < 0))
456 spin_lock_irqsave(&pctrl->lock, flags);
458 val = readl(pctrl->regs + g->io_reg);
460 val |= BIT(g->out_bit);
462 val &= ~BIT(g->out_bit);
463 writel(val, pctrl->regs + g->io_reg);
465 val = readl(pctrl->regs + g->ctl_reg);
466 val |= BIT(g->oe_bit);
467 writel(val, pctrl->regs + g->ctl_reg);
469 spin_unlock_irqrestore(&pctrl->lock, flags);
474 static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
476 const struct msm_pingroup *g;
477 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
480 g = &pctrl->soc->groups[offset];
481 if (WARN_ON(g->io_reg < 0))
484 val = readl(pctrl->regs + g->io_reg);
485 return !!(val & BIT(g->in_bit));
488 static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
490 const struct msm_pingroup *g;
491 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
495 g = &pctrl->soc->groups[offset];
496 if (WARN_ON(g->io_reg < 0))
499 spin_lock_irqsave(&pctrl->lock, flags);
501 val = readl(pctrl->regs + g->io_reg);
503 val |= BIT(g->out_bit);
505 val &= ~BIT(g->out_bit);
506 writel(val, pctrl->regs + g->io_reg);
508 spin_unlock_irqrestore(&pctrl->lock, flags);
511 static int msm_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
513 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
515 return irq_find_mapping(pctrl->domain, offset);
518 static int msm_gpio_request(struct gpio_chip *chip, unsigned offset)
520 int gpio = chip->base + offset;
521 return pinctrl_request_gpio(gpio);
524 static void msm_gpio_free(struct gpio_chip *chip, unsigned offset)
526 int gpio = chip->base + offset;
527 return pinctrl_free_gpio(gpio);
530 #ifdef CONFIG_DEBUG_FS
531 #include <linux/seq_file.h>
533 static void msm_gpio_dbg_show_one(struct seq_file *s,
534 struct pinctrl_dev *pctldev,
535 struct gpio_chip *chip,
539 const struct msm_pingroup *g;
540 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
547 static const char * const pulls[] = {
554 g = &pctrl->soc->groups[offset];
555 ctl_reg = readl(pctrl->regs + g->ctl_reg);
557 is_out = !!(ctl_reg & BIT(g->oe_bit));
558 func = (ctl_reg >> g->mux_bit) & 7;
559 drive = (ctl_reg >> g->drv_bit) & 7;
560 pull = (ctl_reg >> g->pull_bit) & 3;
562 seq_printf(s, " %-8s: %-3s %d", g->name, is_out ? "out" : "in", func);
563 seq_printf(s, " %dmA", msm_regval_to_drive(drive));
564 seq_printf(s, " %s", pulls[pull]);
567 static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
569 unsigned gpio = chip->base;
572 for (i = 0; i < chip->ngpio; i++, gpio++) {
573 msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
579 #define msm_gpio_dbg_show NULL
582 static struct gpio_chip msm_gpio_template = {
583 .direction_input = msm_gpio_direction_input,
584 .direction_output = msm_gpio_direction_output,
587 .to_irq = msm_gpio_to_irq,
588 .request = msm_gpio_request,
589 .free = msm_gpio_free,
590 .dbg_show = msm_gpio_dbg_show,
593 /* For dual-edge interrupts in software, since some hardware has no
596 * At appropriate moments, this function may be called to flip the polarity
597 * settings of both-edge irq lines to try and catch the next edge.
599 * The attempt is considered successful if:
600 * - the status bit goes high, indicating that an edge was caught, or
601 * - the input value of the gpio doesn't change during the attempt.
602 * If the value changes twice during the process, that would cause the first
603 * test to fail but would force the second, as two opposite
604 * transitions would cause a detection no matter the polarity setting.
606 * The do-loop tries to sledge-hammer closed the timing hole between
607 * the initial value-read and the polarity-write - if the line value changes
608 * during that window, an interrupt is lost, the new polarity setting is
609 * incorrect, and the first success test will fail, causing a retry.
611 * Algorithm comes from Google's msmgpio driver.
613 static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
614 const struct msm_pingroup *g,
617 int loop_limit = 100;
618 unsigned val, val2, intstat;
622 val = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit);
624 pol = readl(pctrl->regs + g->intr_cfg_reg);
625 pol ^= BIT(g->intr_polarity_bit);
626 writel(pol, pctrl->regs + g->intr_cfg_reg);
628 val2 = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit);
629 intstat = readl(pctrl->regs + g->intr_status_reg);
630 if (intstat || (val == val2))
632 } while (loop_limit-- > 0);
633 dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
637 static void msm_gpio_irq_mask(struct irq_data *d)
639 const struct msm_pingroup *g;
640 struct msm_pinctrl *pctrl;
644 pctrl = irq_data_get_irq_chip_data(d);
645 g = &pctrl->soc->groups[d->hwirq];
646 if (WARN_ON(g->intr_cfg_reg < 0))
649 spin_lock_irqsave(&pctrl->lock, flags);
651 val = readl(pctrl->regs + g->intr_cfg_reg);
652 val &= ~BIT(g->intr_enable_bit);
653 writel(val, pctrl->regs + g->intr_cfg_reg);
655 clear_bit(d->hwirq, pctrl->enabled_irqs);
657 spin_unlock_irqrestore(&pctrl->lock, flags);
660 static void msm_gpio_irq_unmask(struct irq_data *d)
662 const struct msm_pingroup *g;
663 struct msm_pinctrl *pctrl;
667 pctrl = irq_data_get_irq_chip_data(d);
668 g = &pctrl->soc->groups[d->hwirq];
669 if (WARN_ON(g->intr_status_reg < 0))
672 spin_lock_irqsave(&pctrl->lock, flags);
674 val = readl(pctrl->regs + g->intr_status_reg);
675 val &= ~BIT(g->intr_status_bit);
676 writel(val, pctrl->regs + g->intr_status_reg);
678 val = readl(pctrl->regs + g->intr_cfg_reg);
679 val |= BIT(g->intr_enable_bit);
680 writel(val, pctrl->regs + g->intr_cfg_reg);
682 set_bit(d->hwirq, pctrl->enabled_irqs);
684 spin_unlock_irqrestore(&pctrl->lock, flags);
687 static void msm_gpio_irq_ack(struct irq_data *d)
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_status_reg < 0))
699 spin_lock_irqsave(&pctrl->lock, flags);
701 val = readl(pctrl->regs + g->intr_status_reg);
702 val &= ~BIT(g->intr_status_bit);
703 writel(val, pctrl->regs + g->intr_status_reg);
705 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
706 msm_gpio_update_dual_edge_pos(pctrl, g, d);
708 spin_unlock_irqrestore(&pctrl->lock, flags);
711 #define INTR_TARGET_PROC_APPS 4
713 static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
715 const struct msm_pingroup *g;
716 struct msm_pinctrl *pctrl;
720 pctrl = irq_data_get_irq_chip_data(d);
721 g = &pctrl->soc->groups[d->hwirq];
722 if (WARN_ON(g->intr_cfg_reg < 0))
725 spin_lock_irqsave(&pctrl->lock, flags);
728 * For hw without possibility of detecting both edges
730 if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
731 set_bit(d->hwirq, pctrl->dual_edge_irqs);
733 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
735 /* Route interrupts to application cpu */
736 val = readl(pctrl->regs + g->intr_target_reg);
737 val &= ~(7 << g->intr_target_bit);
738 val |= INTR_TARGET_PROC_APPS << g->intr_target_bit;
739 writel(val, pctrl->regs + g->intr_target_reg);
741 /* Update configuration for gpio.
742 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
743 * internal circuitry of TLMM, toggling the RAW_STATUS
744 * could cause the INTR_STATUS to be set for EDGE interrupts.
746 val = readl(pctrl->regs + g->intr_cfg_reg);
747 val |= BIT(g->intr_raw_status_bit);
748 if (g->intr_detection_width == 2) {
749 val &= ~(3 << g->intr_detection_bit);
750 val &= ~(1 << g->intr_polarity_bit);
752 case IRQ_TYPE_EDGE_RISING:
753 val |= 1 << g->intr_detection_bit;
754 val |= BIT(g->intr_polarity_bit);
756 case IRQ_TYPE_EDGE_FALLING:
757 val |= 2 << g->intr_detection_bit;
758 val |= BIT(g->intr_polarity_bit);
760 case IRQ_TYPE_EDGE_BOTH:
761 val |= 3 << g->intr_detection_bit;
762 val |= BIT(g->intr_polarity_bit);
764 case IRQ_TYPE_LEVEL_LOW:
766 case IRQ_TYPE_LEVEL_HIGH:
767 val |= BIT(g->intr_polarity_bit);
770 } else if (g->intr_detection_width == 1) {
771 val &= ~(1 << g->intr_detection_bit);
772 val &= ~(1 << g->intr_polarity_bit);
774 case IRQ_TYPE_EDGE_RISING:
775 val |= BIT(g->intr_detection_bit);
776 val |= BIT(g->intr_polarity_bit);
778 case IRQ_TYPE_EDGE_FALLING:
779 val |= BIT(g->intr_detection_bit);
781 case IRQ_TYPE_EDGE_BOTH:
782 val |= BIT(g->intr_detection_bit);
784 case IRQ_TYPE_LEVEL_LOW:
786 case IRQ_TYPE_LEVEL_HIGH:
787 val |= BIT(g->intr_polarity_bit);
793 writel(val, pctrl->regs + g->intr_cfg_reg);
795 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
796 msm_gpio_update_dual_edge_pos(pctrl, g, d);
798 spin_unlock_irqrestore(&pctrl->lock, flags);
800 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
801 __irq_set_handler_locked(d->irq, handle_level_irq);
802 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
803 __irq_set_handler_locked(d->irq, handle_edge_irq);
808 static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
810 struct msm_pinctrl *pctrl;
813 pctrl = irq_data_get_irq_chip_data(d);
815 spin_lock_irqsave(&pctrl->lock, flags);
817 irq_set_irq_wake(pctrl->irq, on);
819 spin_unlock_irqrestore(&pctrl->lock, flags);
824 static unsigned int msm_gpio_irq_startup(struct irq_data *d)
826 struct msm_pinctrl *pctrl = irq_data_get_irq_chip_data(d);
828 if (gpio_lock_as_irq(&pctrl->chip, d->hwirq)) {
829 dev_err(pctrl->dev, "unable to lock HW IRQ %lu for IRQ\n",
832 msm_gpio_irq_unmask(d);
836 static void msm_gpio_irq_shutdown(struct irq_data *d)
838 struct msm_pinctrl *pctrl = irq_data_get_irq_chip_data(d);
840 msm_gpio_irq_mask(d);
841 gpio_unlock_as_irq(&pctrl->chip, d->hwirq);
844 static struct irq_chip msm_gpio_irq_chip = {
846 .irq_mask = msm_gpio_irq_mask,
847 .irq_unmask = msm_gpio_irq_unmask,
848 .irq_ack = msm_gpio_irq_ack,
849 .irq_set_type = msm_gpio_irq_set_type,
850 .irq_set_wake = msm_gpio_irq_set_wake,
851 .irq_startup = msm_gpio_irq_startup,
852 .irq_shutdown = msm_gpio_irq_shutdown,
855 static void msm_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
857 const struct msm_pingroup *g;
858 struct msm_pinctrl *pctrl = irq_desc_get_handler_data(desc);
859 struct irq_chip *chip = irq_get_chip(irq);
865 chained_irq_enter(chip, desc);
868 * Each pin has it's own IRQ status register, so use
869 * enabled_irq bitmap to limit the number of reads.
871 for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
872 g = &pctrl->soc->groups[i];
873 val = readl(pctrl->regs + g->intr_status_reg);
874 if (val & BIT(g->intr_status_bit)) {
875 irq_pin = irq_find_mapping(pctrl->domain, i);
876 generic_handle_irq(irq_pin);
881 /* No interrupts were flagged */
883 handle_bad_irq(irq, desc);
885 chained_irq_exit(chip, desc);
889 * This lock class tells lockdep that GPIO irqs are in a different
890 * category than their parents, so it won't report false recursion.
892 static struct lock_class_key gpio_lock_class;
894 static int msm_gpio_init(struct msm_pinctrl *pctrl)
896 struct gpio_chip *chip;
901 unsigned ngpio = pctrl->soc->ngpios;
903 if (WARN_ON(ngpio > MAX_NR_GPIO))
909 chip->label = dev_name(pctrl->dev);
910 chip->dev = pctrl->dev;
911 chip->owner = THIS_MODULE;
912 chip->of_node = pctrl->dev->of_node;
914 ret = gpiochip_add(&pctrl->chip);
916 dev_err(pctrl->dev, "Failed register gpiochip\n");
920 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev), 0, 0, chip->ngpio);
922 dev_err(pctrl->dev, "Failed to add pin range\n");
926 pctrl->domain = irq_domain_add_linear(pctrl->dev->of_node, chip->ngpio,
927 &irq_domain_simple_ops, NULL);
928 if (!pctrl->domain) {
929 dev_err(pctrl->dev, "Failed to register irq domain\n");
930 r = gpiochip_remove(&pctrl->chip);
934 for (i = 0; i < chip->ngpio; i++) {
935 irq = irq_create_mapping(pctrl->domain, i);
936 irq_set_lockdep_class(irq, &gpio_lock_class);
937 irq_set_chip_and_handler(irq, &msm_gpio_irq_chip, handle_edge_irq);
938 irq_set_chip_data(irq, pctrl);
941 irq_set_handler_data(pctrl->irq, pctrl);
942 irq_set_chained_handler(pctrl->irq, msm_gpio_irq_handler);
947 int msm_pinctrl_probe(struct platform_device *pdev,
948 const struct msm_pinctrl_soc_data *soc_data)
950 struct msm_pinctrl *pctrl;
951 struct resource *res;
954 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
956 dev_err(&pdev->dev, "Can't allocate msm_pinctrl\n");
959 pctrl->dev = &pdev->dev;
960 pctrl->soc = soc_data;
961 pctrl->chip = msm_gpio_template;
963 spin_lock_init(&pctrl->lock);
965 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
966 pctrl->regs = devm_ioremap_resource(&pdev->dev, res);
967 if (IS_ERR(pctrl->regs))
968 return PTR_ERR(pctrl->regs);
970 pctrl->irq = platform_get_irq(pdev, 0);
971 if (pctrl->irq < 0) {
972 dev_err(&pdev->dev, "No interrupt defined for msmgpio\n");
976 msm_pinctrl_desc.name = dev_name(&pdev->dev);
977 msm_pinctrl_desc.pins = pctrl->soc->pins;
978 msm_pinctrl_desc.npins = pctrl->soc->npins;
979 pctrl->pctrl = pinctrl_register(&msm_pinctrl_desc, &pdev->dev, pctrl);
981 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
985 ret = msm_gpio_init(pctrl);
987 pinctrl_unregister(pctrl->pctrl);
991 platform_set_drvdata(pdev, pctrl);
993 dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
997 EXPORT_SYMBOL(msm_pinctrl_probe);
999 int msm_pinctrl_remove(struct platform_device *pdev)
1001 struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
1004 ret = gpiochip_remove(&pctrl->chip);
1006 dev_err(&pdev->dev, "Failed to remove gpiochip\n");
1010 irq_set_chained_handler(pctrl->irq, NULL);
1011 irq_domain_remove(pctrl->domain);
1012 pinctrl_unregister(pctrl->pctrl);
1016 EXPORT_SYMBOL(msm_pinctrl_remove);