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 * @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 irq_domain *domain;
60 struct gpio_chip chip;
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_dt_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_enable(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 if (WARN_ON(g->mux_bit < 0))
149 for (i = 0; i < ARRAY_SIZE(g->funcs); i++) {
150 if (g->funcs[i] == function)
154 if (WARN_ON(i == ARRAY_SIZE(g->funcs)))
157 spin_lock_irqsave(&pctrl->lock, flags);
159 val = readl(pctrl->regs + g->ctl_reg);
160 val &= ~(0x7 << g->mux_bit);
161 val |= i << g->mux_bit;
162 writel(val, pctrl->regs + g->ctl_reg);
164 spin_unlock_irqrestore(&pctrl->lock, flags);
169 static void msm_pinmux_disable(struct pinctrl_dev *pctldev,
173 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
174 const struct msm_pingroup *g;
178 g = &pctrl->soc->groups[group];
180 if (WARN_ON(g->mux_bit < 0))
183 spin_lock_irqsave(&pctrl->lock, flags);
185 /* Clear the mux bits to select gpio mode */
186 val = readl(pctrl->regs + g->ctl_reg);
187 val &= ~(0x7 << g->mux_bit);
188 writel(val, pctrl->regs + g->ctl_reg);
190 spin_unlock_irqrestore(&pctrl->lock, flags);
193 static const struct pinmux_ops msm_pinmux_ops = {
194 .get_functions_count = msm_get_functions_count,
195 .get_function_name = msm_get_function_name,
196 .get_function_groups = msm_get_function_groups,
197 .enable = msm_pinmux_enable,
198 .disable = msm_pinmux_disable,
201 static int msm_config_reg(struct msm_pinctrl *pctrl,
202 const struct msm_pingroup *g,
209 case PIN_CONFIG_BIAS_DISABLE:
214 case PIN_CONFIG_BIAS_PULL_DOWN:
219 case PIN_CONFIG_BIAS_PULL_UP:
224 case PIN_CONFIG_DRIVE_STRENGTH:
229 case PIN_CONFIG_OUTPUT:
235 dev_err(pctrl->dev, "Invalid config param %04x\n", param);
240 dev_err(pctrl->dev, "Config param %04x not supported on group %s\n",
248 static int msm_config_get(struct pinctrl_dev *pctldev,
250 unsigned long *config)
252 dev_err(pctldev->dev, "pin_config_set op not supported\n");
256 static int msm_config_set(struct pinctrl_dev *pctldev, unsigned int pin,
257 unsigned long *configs, unsigned num_configs)
259 dev_err(pctldev->dev, "pin_config_set op not supported\n");
263 #define MSM_NO_PULL 0
264 #define MSM_PULL_DOWN 1
265 #define MSM_PULL_UP 3
267 static const unsigned msm_regval_to_drive[] = { 2, 4, 6, 8, 10, 12, 14, 16 };
268 static const unsigned msm_drive_to_regval[] = { -1, -1, 0, -1, 1, -1, 2, -1, 3, -1, 4, -1, 5, -1, 6, -1, 7 };
270 static int msm_config_group_get(struct pinctrl_dev *pctldev,
272 unsigned long *config)
274 const struct msm_pingroup *g;
275 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
276 unsigned param = pinconf_to_config_param(*config);
284 g = &pctrl->soc->groups[group];
286 ret = msm_config_reg(pctrl, g, param, ®, &mask, &bit);
290 val = readl(pctrl->regs + reg);
291 arg = (val >> bit) & mask;
293 /* Convert register value to pinconf value */
295 case PIN_CONFIG_BIAS_DISABLE:
296 arg = arg == MSM_NO_PULL;
298 case PIN_CONFIG_BIAS_PULL_DOWN:
299 arg = arg == MSM_PULL_DOWN;
301 case PIN_CONFIG_BIAS_PULL_UP:
302 arg = arg == MSM_PULL_UP;
304 case PIN_CONFIG_DRIVE_STRENGTH:
305 arg = msm_regval_to_drive[arg];
307 case PIN_CONFIG_OUTPUT:
308 /* Pin is not output */
312 val = readl(pctrl->regs + g->io_reg);
313 arg = !!(val & BIT(g->in_bit));
316 dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
321 *config = pinconf_to_config_packed(param, arg);
326 static int msm_config_group_set(struct pinctrl_dev *pctldev,
328 unsigned long *configs,
329 unsigned num_configs)
331 const struct msm_pingroup *g;
332 struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
343 g = &pctrl->soc->groups[group];
345 for (i = 0; i < num_configs; i++) {
346 param = pinconf_to_config_param(configs[i]);
347 arg = pinconf_to_config_argument(configs[i]);
349 ret = msm_config_reg(pctrl, g, param, ®, &mask, &bit);
353 /* Convert pinconf values to register values */
355 case PIN_CONFIG_BIAS_DISABLE:
358 case PIN_CONFIG_BIAS_PULL_DOWN:
361 case PIN_CONFIG_BIAS_PULL_UP:
364 case PIN_CONFIG_DRIVE_STRENGTH:
365 /* Check for invalid values */
366 if (arg >= ARRAY_SIZE(msm_drive_to_regval))
369 arg = msm_drive_to_regval[arg];
371 case PIN_CONFIG_OUTPUT:
372 /* set output value */
373 spin_lock_irqsave(&pctrl->lock, flags);
374 val = readl(pctrl->regs + g->io_reg);
376 val |= BIT(g->out_bit);
378 val &= ~BIT(g->out_bit);
379 writel(val, pctrl->regs + g->io_reg);
380 spin_unlock_irqrestore(&pctrl->lock, flags);
386 dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
391 /* Range-check user-supplied value */
393 dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
397 spin_lock_irqsave(&pctrl->lock, flags);
398 val = readl(pctrl->regs + reg);
399 val &= ~(mask << bit);
401 writel(val, pctrl->regs + reg);
402 spin_unlock_irqrestore(&pctrl->lock, flags);
408 static const struct pinconf_ops msm_pinconf_ops = {
409 .pin_config_get = msm_config_get,
410 .pin_config_set = msm_config_set,
411 .pin_config_group_get = msm_config_group_get,
412 .pin_config_group_set = msm_config_group_set,
415 static struct pinctrl_desc msm_pinctrl_desc = {
416 .pctlops = &msm_pinctrl_ops,
417 .pmxops = &msm_pinmux_ops,
418 .confops = &msm_pinconf_ops,
419 .owner = THIS_MODULE,
422 static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
424 const struct msm_pingroup *g;
425 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
429 g = &pctrl->soc->groups[offset];
430 if (WARN_ON(g->io_reg < 0))
433 spin_lock_irqsave(&pctrl->lock, flags);
435 val = readl(pctrl->regs + g->ctl_reg);
436 val &= ~BIT(g->oe_bit);
437 writel(val, pctrl->regs + g->ctl_reg);
439 spin_unlock_irqrestore(&pctrl->lock, flags);
444 static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
446 const struct msm_pingroup *g;
447 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
451 g = &pctrl->soc->groups[offset];
452 if (WARN_ON(g->io_reg < 0))
455 spin_lock_irqsave(&pctrl->lock, flags);
457 val = readl(pctrl->regs + g->io_reg);
459 val |= BIT(g->out_bit);
461 val &= ~BIT(g->out_bit);
462 writel(val, pctrl->regs + g->io_reg);
464 val = readl(pctrl->regs + g->ctl_reg);
465 val |= BIT(g->oe_bit);
466 writel(val, pctrl->regs + g->ctl_reg);
468 spin_unlock_irqrestore(&pctrl->lock, flags);
473 static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
475 const struct msm_pingroup *g;
476 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
479 g = &pctrl->soc->groups[offset];
480 if (WARN_ON(g->io_reg < 0))
483 val = readl(pctrl->regs + g->io_reg);
484 return !!(val & BIT(g->in_bit));
487 static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
489 const struct msm_pingroup *g;
490 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
494 g = &pctrl->soc->groups[offset];
495 if (WARN_ON(g->io_reg < 0))
498 spin_lock_irqsave(&pctrl->lock, flags);
500 val = readl(pctrl->regs + g->io_reg);
502 val |= BIT(g->out_bit);
504 val &= ~BIT(g->out_bit);
505 writel(val, pctrl->regs + g->io_reg);
507 spin_unlock_irqrestore(&pctrl->lock, flags);
510 static int msm_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
512 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
514 return irq_find_mapping(pctrl->domain, offset);
517 static int msm_gpio_request(struct gpio_chip *chip, unsigned offset)
519 int gpio = chip->base + offset;
520 return pinctrl_request_gpio(gpio);
523 static void msm_gpio_free(struct gpio_chip *chip, unsigned offset)
525 int gpio = chip->base + offset;
526 return pinctrl_free_gpio(gpio);
529 #ifdef CONFIG_DEBUG_FS
530 #include <linux/seq_file.h>
532 static void msm_gpio_dbg_show_one(struct seq_file *s,
533 struct pinctrl_dev *pctldev,
534 struct gpio_chip *chip,
538 const struct msm_pingroup *g;
539 struct msm_pinctrl *pctrl = container_of(chip, struct msm_pinctrl, chip);
546 static const char * const pulls[] = {
553 g = &pctrl->soc->groups[offset];
554 ctl_reg = readl(pctrl->regs + g->ctl_reg);
556 is_out = !!(ctl_reg & BIT(g->oe_bit));
557 func = (ctl_reg >> g->mux_bit) & 7;
558 drive = (ctl_reg >> g->drv_bit) & 7;
559 pull = (ctl_reg >> g->pull_bit) & 3;
561 seq_printf(s, " %-8s: %-3s %d", g->name, is_out ? "out" : "in", func);
562 seq_printf(s, " %dmA", msm_regval_to_drive[drive]);
563 seq_printf(s, " %s", pulls[pull]);
566 static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
568 unsigned gpio = chip->base;
571 for (i = 0; i < chip->ngpio; i++, gpio++) {
572 msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
578 #define msm_gpio_dbg_show NULL
581 static struct gpio_chip msm_gpio_template = {
582 .direction_input = msm_gpio_direction_input,
583 .direction_output = msm_gpio_direction_output,
586 .to_irq = msm_gpio_to_irq,
587 .request = msm_gpio_request,
588 .free = msm_gpio_free,
589 .dbg_show = msm_gpio_dbg_show,
592 /* For dual-edge interrupts in software, since some hardware has no
595 * At appropriate moments, this function may be called to flip the polarity
596 * settings of both-edge irq lines to try and catch the next edge.
598 * The attempt is considered successful if:
599 * - the status bit goes high, indicating that an edge was caught, or
600 * - the input value of the gpio doesn't change during the attempt.
601 * If the value changes twice during the process, that would cause the first
602 * test to fail but would force the second, as two opposite
603 * transitions would cause a detection no matter the polarity setting.
605 * The do-loop tries to sledge-hammer closed the timing hole between
606 * the initial value-read and the polarity-write - if the line value changes
607 * during that window, an interrupt is lost, the new polarity setting is
608 * incorrect, and the first success test will fail, causing a retry.
610 * Algorithm comes from Google's msmgpio driver.
612 static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
613 const struct msm_pingroup *g,
616 int loop_limit = 100;
617 unsigned val, val2, intstat;
621 val = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit);
623 pol = readl(pctrl->regs + g->intr_cfg_reg);
624 pol ^= BIT(g->intr_polarity_bit);
625 writel(pol, pctrl->regs + g->intr_cfg_reg);
627 val2 = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit);
628 intstat = readl(pctrl->regs + g->intr_status_reg);
629 if (intstat || (val == val2))
631 } while (loop_limit-- > 0);
632 dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
636 static void msm_gpio_irq_mask(struct irq_data *d)
638 const struct msm_pingroup *g;
639 struct msm_pinctrl *pctrl;
643 pctrl = irq_data_get_irq_chip_data(d);
644 g = &pctrl->soc->groups[d->hwirq];
645 if (WARN_ON(g->intr_cfg_reg < 0))
648 spin_lock_irqsave(&pctrl->lock, flags);
650 val = readl(pctrl->regs + g->intr_cfg_reg);
651 val &= ~BIT(g->intr_enable_bit);
652 writel(val, pctrl->regs + g->intr_cfg_reg);
654 clear_bit(d->hwirq, pctrl->enabled_irqs);
656 spin_unlock_irqrestore(&pctrl->lock, flags);
659 static void msm_gpio_irq_unmask(struct irq_data *d)
661 const struct msm_pingroup *g;
662 struct msm_pinctrl *pctrl;
666 pctrl = irq_data_get_irq_chip_data(d);
667 g = &pctrl->soc->groups[d->hwirq];
668 if (WARN_ON(g->intr_status_reg < 0))
671 spin_lock_irqsave(&pctrl->lock, flags);
673 val = readl(pctrl->regs + g->intr_status_reg);
674 val &= ~BIT(g->intr_status_bit);
675 writel(val, pctrl->regs + g->intr_status_reg);
677 val = readl(pctrl->regs + g->intr_cfg_reg);
678 val |= BIT(g->intr_enable_bit);
679 writel(val, pctrl->regs + g->intr_cfg_reg);
681 set_bit(d->hwirq, pctrl->enabled_irqs);
683 spin_unlock_irqrestore(&pctrl->lock, flags);
686 static void msm_gpio_irq_ack(struct irq_data *d)
688 const struct msm_pingroup *g;
689 struct msm_pinctrl *pctrl;
693 pctrl = irq_data_get_irq_chip_data(d);
694 g = &pctrl->soc->groups[d->hwirq];
695 if (WARN_ON(g->intr_status_reg < 0))
698 spin_lock_irqsave(&pctrl->lock, flags);
700 val = readl(pctrl->regs + g->intr_status_reg);
701 val &= ~BIT(g->intr_status_bit);
702 writel(val, pctrl->regs + g->intr_status_reg);
704 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
705 msm_gpio_update_dual_edge_pos(pctrl, g, d);
707 spin_unlock_irqrestore(&pctrl->lock, flags);
710 #define INTR_TARGET_PROC_APPS 4
712 static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
714 const struct msm_pingroup *g;
715 struct msm_pinctrl *pctrl;
719 pctrl = irq_data_get_irq_chip_data(d);
720 g = &pctrl->soc->groups[d->hwirq];
721 if (WARN_ON(g->intr_cfg_reg < 0))
724 spin_lock_irqsave(&pctrl->lock, flags);
727 * For hw without possibility of detecting both edges
729 if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
730 set_bit(d->hwirq, pctrl->dual_edge_irqs);
732 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
734 /* Route interrupts to application cpu */
735 val = readl(pctrl->regs + g->intr_target_reg);
736 val &= ~(7 << g->intr_target_bit);
737 val |= INTR_TARGET_PROC_APPS << g->intr_target_bit;
738 writel(val, pctrl->regs + g->intr_target_reg);
740 /* Update configuration for gpio.
741 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
742 * internal circuitry of TLMM, toggling the RAW_STATUS
743 * could cause the INTR_STATUS to be set for EDGE interrupts.
745 val = readl(pctrl->regs + g->intr_cfg_reg);
746 val |= BIT(g->intr_raw_status_bit);
747 if (g->intr_detection_width == 2) {
748 val &= ~(3 << g->intr_detection_bit);
749 val &= ~(1 << g->intr_polarity_bit);
751 case IRQ_TYPE_EDGE_RISING:
752 val |= 1 << g->intr_detection_bit;
753 val |= BIT(g->intr_polarity_bit);
755 case IRQ_TYPE_EDGE_FALLING:
756 val |= 2 << g->intr_detection_bit;
757 val |= BIT(g->intr_polarity_bit);
759 case IRQ_TYPE_EDGE_BOTH:
760 val |= 3 << g->intr_detection_bit;
761 val |= BIT(g->intr_polarity_bit);
763 case IRQ_TYPE_LEVEL_LOW:
765 case IRQ_TYPE_LEVEL_HIGH:
766 val |= BIT(g->intr_polarity_bit);
769 } else if (g->intr_detection_width == 1) {
770 val &= ~(1 << g->intr_detection_bit);
771 val &= ~(1 << g->intr_polarity_bit);
773 case IRQ_TYPE_EDGE_RISING:
774 val |= BIT(g->intr_detection_bit);
775 val |= BIT(g->intr_polarity_bit);
777 case IRQ_TYPE_EDGE_FALLING:
778 val |= BIT(g->intr_detection_bit);
780 case IRQ_TYPE_EDGE_BOTH:
781 val |= BIT(g->intr_detection_bit);
783 case IRQ_TYPE_LEVEL_LOW:
785 case IRQ_TYPE_LEVEL_HIGH:
786 val |= BIT(g->intr_polarity_bit);
792 writel(val, pctrl->regs + g->intr_cfg_reg);
794 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
795 msm_gpio_update_dual_edge_pos(pctrl, g, d);
797 spin_unlock_irqrestore(&pctrl->lock, flags);
799 if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
800 __irq_set_handler_locked(d->irq, handle_level_irq);
801 else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
802 __irq_set_handler_locked(d->irq, handle_edge_irq);
807 static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
809 struct msm_pinctrl *pctrl;
812 pctrl = irq_data_get_irq_chip_data(d);
814 spin_lock_irqsave(&pctrl->lock, flags);
816 irq_set_irq_wake(pctrl->irq, on);
818 spin_unlock_irqrestore(&pctrl->lock, flags);
823 static unsigned int msm_gpio_irq_startup(struct irq_data *d)
825 struct msm_pinctrl *pctrl = irq_data_get_irq_chip_data(d);
827 if (gpio_lock_as_irq(&pctrl->chip, d->hwirq)) {
828 dev_err(pctrl->dev, "unable to lock HW IRQ %lu for IRQ\n",
831 msm_gpio_irq_unmask(d);
835 static void msm_gpio_irq_shutdown(struct irq_data *d)
837 struct msm_pinctrl *pctrl = irq_data_get_irq_chip_data(d);
839 msm_gpio_irq_mask(d);
840 gpio_unlock_as_irq(&pctrl->chip, d->hwirq);
843 static struct irq_chip msm_gpio_irq_chip = {
845 .irq_mask = msm_gpio_irq_mask,
846 .irq_unmask = msm_gpio_irq_unmask,
847 .irq_ack = msm_gpio_irq_ack,
848 .irq_set_type = msm_gpio_irq_set_type,
849 .irq_set_wake = msm_gpio_irq_set_wake,
850 .irq_startup = msm_gpio_irq_startup,
851 .irq_shutdown = msm_gpio_irq_shutdown,
854 static void msm_gpio_irq_handler(unsigned int irq, struct irq_desc *desc)
856 const struct msm_pingroup *g;
857 struct msm_pinctrl *pctrl = irq_desc_get_handler_data(desc);
858 struct irq_chip *chip = irq_get_chip(irq);
864 chained_irq_enter(chip, desc);
867 * Each pin has it's own IRQ status register, so use
868 * enabled_irq bitmap to limit the number of reads.
870 for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
871 g = &pctrl->soc->groups[i];
872 val = readl(pctrl->regs + g->intr_status_reg);
873 if (val & BIT(g->intr_status_bit)) {
874 irq_pin = irq_find_mapping(pctrl->domain, i);
875 generic_handle_irq(irq_pin);
880 /* No interrupts were flagged */
882 handle_bad_irq(irq, desc);
884 chained_irq_exit(chip, desc);
888 * This lock class tells lockdep that GPIO irqs are in a different
889 * category than their parents, so it won't report false recursion.
891 static struct lock_class_key gpio_lock_class;
893 static int msm_gpio_init(struct msm_pinctrl *pctrl)
895 struct gpio_chip *chip;
900 unsigned ngpio = pctrl->soc->ngpios;
902 if (WARN_ON(ngpio > MAX_NR_GPIO))
908 chip->label = dev_name(pctrl->dev);
909 chip->dev = pctrl->dev;
910 chip->owner = THIS_MODULE;
911 chip->of_node = pctrl->dev->of_node;
913 ret = gpiochip_add(&pctrl->chip);
915 dev_err(pctrl->dev, "Failed register gpiochip\n");
919 ret = gpiochip_add_pin_range(&pctrl->chip, dev_name(pctrl->dev), 0, 0, chip->ngpio);
921 dev_err(pctrl->dev, "Failed to add pin range\n");
925 pctrl->domain = irq_domain_add_linear(pctrl->dev->of_node, chip->ngpio,
926 &irq_domain_simple_ops, NULL);
927 if (!pctrl->domain) {
928 dev_err(pctrl->dev, "Failed to register irq domain\n");
929 r = gpiochip_remove(&pctrl->chip);
933 for (i = 0; i < chip->ngpio; i++) {
934 irq = irq_create_mapping(pctrl->domain, i);
935 irq_set_lockdep_class(irq, &gpio_lock_class);
936 irq_set_chip_and_handler(irq, &msm_gpio_irq_chip, handle_edge_irq);
937 irq_set_chip_data(irq, pctrl);
940 irq_set_handler_data(pctrl->irq, pctrl);
941 irq_set_chained_handler(pctrl->irq, msm_gpio_irq_handler);
946 int msm_pinctrl_probe(struct platform_device *pdev,
947 const struct msm_pinctrl_soc_data *soc_data)
949 struct msm_pinctrl *pctrl;
950 struct resource *res;
953 pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
955 dev_err(&pdev->dev, "Can't allocate msm_pinctrl\n");
958 pctrl->dev = &pdev->dev;
959 pctrl->soc = soc_data;
960 pctrl->chip = msm_gpio_template;
962 spin_lock_init(&pctrl->lock);
964 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
965 pctrl->regs = devm_ioremap_resource(&pdev->dev, res);
966 if (IS_ERR(pctrl->regs))
967 return PTR_ERR(pctrl->regs);
969 pctrl->irq = platform_get_irq(pdev, 0);
970 if (pctrl->irq < 0) {
971 dev_err(&pdev->dev, "No interrupt defined for msmgpio\n");
975 msm_pinctrl_desc.name = dev_name(&pdev->dev);
976 msm_pinctrl_desc.pins = pctrl->soc->pins;
977 msm_pinctrl_desc.npins = pctrl->soc->npins;
978 pctrl->pctrl = pinctrl_register(&msm_pinctrl_desc, &pdev->dev, pctrl);
980 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
984 ret = msm_gpio_init(pctrl);
986 pinctrl_unregister(pctrl->pctrl);
990 platform_set_drvdata(pdev, pctrl);
992 dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
996 EXPORT_SYMBOL(msm_pinctrl_probe);
998 int msm_pinctrl_remove(struct platform_device *pdev)
1000 struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
1003 ret = gpiochip_remove(&pctrl->chip);
1005 dev_err(&pdev->dev, "Failed to remove gpiochip\n");
1009 irq_set_chained_handler(pctrl->irq, NULL);
1010 irq_domain_remove(pctrl->domain);
1011 pinctrl_unregister(pctrl->pctrl);
1015 EXPORT_SYMBOL(msm_pinctrl_remove);