Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2013, Sony Mobile Communications AB.
0004  * Copyright (c) 2013, The Linux Foundation. All rights reserved.
0005  */
0006 
0007 #include <linux/delay.h>
0008 #include <linux/err.h>
0009 #include <linux/io.h>
0010 #include <linux/module.h>
0011 #include <linux/of.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/pinctrl/machine.h>
0014 #include <linux/pinctrl/pinctrl.h>
0015 #include <linux/pinctrl/pinmux.h>
0016 #include <linux/pinctrl/pinconf.h>
0017 #include <linux/pinctrl/pinconf-generic.h>
0018 #include <linux/slab.h>
0019 #include <linux/gpio/driver.h>
0020 #include <linux/interrupt.h>
0021 #include <linux/spinlock.h>
0022 #include <linux/reboot.h>
0023 #include <linux/pm.h>
0024 #include <linux/log2.h>
0025 #include <linux/qcom_scm.h>
0026 
0027 #include <linux/soc/qcom/irq.h>
0028 
0029 #include "../core.h"
0030 #include "../pinconf.h"
0031 #include "pinctrl-msm.h"
0032 #include "../pinctrl-utils.h"
0033 
0034 #define MAX_NR_GPIO 300
0035 #define MAX_NR_TILES 4
0036 #define PS_HOLD_OFFSET 0x820
0037 
0038 /**
0039  * struct msm_pinctrl - state for a pinctrl-msm device
0040  * @dev:            device handle.
0041  * @pctrl:          pinctrl handle.
0042  * @chip:           gpiochip handle.
0043  * @desc:           pin controller descriptor
0044  * @restart_nb:     restart notifier block.
0045  * @irq:            parent irq for the TLMM irq_chip.
0046  * @intr_target_use_scm: route irq to application cpu using scm calls
0047  * @lock:           Spinlock to protect register resources as well
0048  *                  as msm_pinctrl data structures.
0049  * @enabled_irqs:   Bitmap of currently enabled irqs.
0050  * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
0051  *                  detection.
0052  * @skip_wake_irqs: Skip IRQs that are handled by wakeup interrupt controller
0053  * @disabled_for_mux: These IRQs were disabled because we muxed away.
0054  * @soc:            Reference to soc_data of platform specific data.
0055  * @regs:           Base addresses for the TLMM tiles.
0056  * @phys_base:      Physical base address
0057  */
0058 struct msm_pinctrl {
0059     struct device *dev;
0060     struct pinctrl_dev *pctrl;
0061     struct gpio_chip chip;
0062     struct pinctrl_desc desc;
0063     struct notifier_block restart_nb;
0064 
0065     int irq;
0066 
0067     bool intr_target_use_scm;
0068 
0069     raw_spinlock_t lock;
0070 
0071     DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
0072     DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
0073     DECLARE_BITMAP(skip_wake_irqs, MAX_NR_GPIO);
0074     DECLARE_BITMAP(disabled_for_mux, MAX_NR_GPIO);
0075 
0076     const struct msm_pinctrl_soc_data *soc;
0077     void __iomem *regs[MAX_NR_TILES];
0078     u32 phys_base[MAX_NR_TILES];
0079 };
0080 
0081 #define MSM_ACCESSOR(name) \
0082 static u32 msm_readl_##name(struct msm_pinctrl *pctrl, \
0083                 const struct msm_pingroup *g) \
0084 { \
0085     return readl(pctrl->regs[g->tile] + g->name##_reg); \
0086 } \
0087 static void msm_writel_##name(u32 val, struct msm_pinctrl *pctrl, \
0088                   const struct msm_pingroup *g) \
0089 { \
0090     writel(val, pctrl->regs[g->tile] + g->name##_reg); \
0091 }
0092 
0093 MSM_ACCESSOR(ctl)
0094 MSM_ACCESSOR(io)
0095 MSM_ACCESSOR(intr_cfg)
0096 MSM_ACCESSOR(intr_status)
0097 MSM_ACCESSOR(intr_target)
0098 
0099 static void msm_ack_intr_status(struct msm_pinctrl *pctrl,
0100                 const struct msm_pingroup *g)
0101 {
0102     u32 val = g->intr_ack_high ? BIT(g->intr_status_bit) : 0;
0103 
0104     msm_writel_intr_status(val, pctrl, g);
0105 }
0106 
0107 static int msm_get_groups_count(struct pinctrl_dev *pctldev)
0108 {
0109     struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
0110 
0111     return pctrl->soc->ngroups;
0112 }
0113 
0114 static const char *msm_get_group_name(struct pinctrl_dev *pctldev,
0115                       unsigned group)
0116 {
0117     struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
0118 
0119     return pctrl->soc->groups[group].name;
0120 }
0121 
0122 static int msm_get_group_pins(struct pinctrl_dev *pctldev,
0123                   unsigned group,
0124                   const unsigned **pins,
0125                   unsigned *num_pins)
0126 {
0127     struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
0128 
0129     *pins = pctrl->soc->groups[group].pins;
0130     *num_pins = pctrl->soc->groups[group].npins;
0131     return 0;
0132 }
0133 
0134 static const struct pinctrl_ops msm_pinctrl_ops = {
0135     .get_groups_count   = msm_get_groups_count,
0136     .get_group_name     = msm_get_group_name,
0137     .get_group_pins     = msm_get_group_pins,
0138     .dt_node_to_map     = pinconf_generic_dt_node_to_map_group,
0139     .dt_free_map        = pinctrl_utils_free_map,
0140 };
0141 
0142 static int msm_pinmux_request(struct pinctrl_dev *pctldev, unsigned offset)
0143 {
0144     struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
0145     struct gpio_chip *chip = &pctrl->chip;
0146 
0147     return gpiochip_line_is_valid(chip, offset) ? 0 : -EINVAL;
0148 }
0149 
0150 static int msm_get_functions_count(struct pinctrl_dev *pctldev)
0151 {
0152     struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
0153 
0154     return pctrl->soc->nfunctions;
0155 }
0156 
0157 static const char *msm_get_function_name(struct pinctrl_dev *pctldev,
0158                      unsigned function)
0159 {
0160     struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
0161 
0162     return pctrl->soc->functions[function].name;
0163 }
0164 
0165 static int msm_get_function_groups(struct pinctrl_dev *pctldev,
0166                    unsigned function,
0167                    const char * const **groups,
0168                    unsigned * const num_groups)
0169 {
0170     struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
0171 
0172     *groups = pctrl->soc->functions[function].groups;
0173     *num_groups = pctrl->soc->functions[function].ngroups;
0174     return 0;
0175 }
0176 
0177 static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
0178                   unsigned function,
0179                   unsigned group)
0180 {
0181     struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
0182     struct gpio_chip *gc = &pctrl->chip;
0183     unsigned int irq = irq_find_mapping(gc->irq.domain, group);
0184     struct irq_data *d = irq_get_irq_data(irq);
0185     unsigned int gpio_func = pctrl->soc->gpio_func;
0186     unsigned int egpio_func = pctrl->soc->egpio_func;
0187     const struct msm_pingroup *g;
0188     unsigned long flags;
0189     u32 val, mask;
0190     int i;
0191 
0192     g = &pctrl->soc->groups[group];
0193     mask = GENMASK(g->mux_bit + order_base_2(g->nfuncs) - 1, g->mux_bit);
0194 
0195     for (i = 0; i < g->nfuncs; i++) {
0196         if (g->funcs[i] == function)
0197             break;
0198     }
0199 
0200     if (WARN_ON(i == g->nfuncs))
0201         return -EINVAL;
0202 
0203     /*
0204      * If an GPIO interrupt is setup on this pin then we need special
0205      * handling.  Specifically interrupt detection logic will still see
0206      * the pin twiddle even when we're muxed away.
0207      *
0208      * When we see a pin with an interrupt setup on it then we'll disable
0209      * (mask) interrupts on it when we mux away until we mux back.  Note
0210      * that disable_irq() refcounts and interrupts are disabled as long as
0211      * at least one disable_irq() has been called.
0212      */
0213     if (d && i != gpio_func &&
0214         !test_and_set_bit(d->hwirq, pctrl->disabled_for_mux))
0215         disable_irq(irq);
0216 
0217     raw_spin_lock_irqsave(&pctrl->lock, flags);
0218 
0219     val = msm_readl_ctl(pctrl, g);
0220 
0221     if (egpio_func && i == egpio_func) {
0222         if (val & BIT(g->egpio_present))
0223             val &= ~BIT(g->egpio_enable);
0224     } else {
0225         val &= ~mask;
0226         val |= i << g->mux_bit;
0227         /* Claim ownership of pin if egpio capable */
0228         if (egpio_func && val & BIT(g->egpio_present))
0229             val |= BIT(g->egpio_enable);
0230     }
0231 
0232     msm_writel_ctl(val, pctrl, g);
0233 
0234     raw_spin_unlock_irqrestore(&pctrl->lock, flags);
0235 
0236     if (d && i == gpio_func &&
0237         test_and_clear_bit(d->hwirq, pctrl->disabled_for_mux)) {
0238         /*
0239          * Clear interrupts detected while not GPIO since we only
0240          * masked things.
0241          */
0242         if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
0243             irq_chip_set_parent_state(d, IRQCHIP_STATE_PENDING, false);
0244         else
0245             msm_ack_intr_status(pctrl, g);
0246 
0247         enable_irq(irq);
0248     }
0249 
0250     return 0;
0251 }
0252 
0253 static int msm_pinmux_request_gpio(struct pinctrl_dev *pctldev,
0254                    struct pinctrl_gpio_range *range,
0255                    unsigned offset)
0256 {
0257     struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
0258     const struct msm_pingroup *g = &pctrl->soc->groups[offset];
0259 
0260     /* No funcs? Probably ACPI so can't do anything here */
0261     if (!g->nfuncs)
0262         return 0;
0263 
0264     return msm_pinmux_set_mux(pctldev, g->funcs[pctrl->soc->gpio_func], offset);
0265 }
0266 
0267 static const struct pinmux_ops msm_pinmux_ops = {
0268     .request        = msm_pinmux_request,
0269     .get_functions_count    = msm_get_functions_count,
0270     .get_function_name  = msm_get_function_name,
0271     .get_function_groups    = msm_get_function_groups,
0272     .gpio_request_enable    = msm_pinmux_request_gpio,
0273     .set_mux        = msm_pinmux_set_mux,
0274 };
0275 
0276 static int msm_config_reg(struct msm_pinctrl *pctrl,
0277               const struct msm_pingroup *g,
0278               unsigned param,
0279               unsigned *mask,
0280               unsigned *bit)
0281 {
0282     switch (param) {
0283     case PIN_CONFIG_BIAS_DISABLE:
0284     case PIN_CONFIG_BIAS_PULL_DOWN:
0285     case PIN_CONFIG_BIAS_BUS_HOLD:
0286     case PIN_CONFIG_BIAS_PULL_UP:
0287         *bit = g->pull_bit;
0288         *mask = 3;
0289         break;
0290     case PIN_CONFIG_DRIVE_OPEN_DRAIN:
0291         *bit = g->od_bit;
0292         *mask = 1;
0293         break;
0294     case PIN_CONFIG_DRIVE_STRENGTH:
0295         *bit = g->drv_bit;
0296         *mask = 7;
0297         break;
0298     case PIN_CONFIG_OUTPUT:
0299     case PIN_CONFIG_INPUT_ENABLE:
0300         *bit = g->oe_bit;
0301         *mask = 1;
0302         break;
0303     default:
0304         return -ENOTSUPP;
0305     }
0306 
0307     return 0;
0308 }
0309 
0310 #define MSM_NO_PULL     0
0311 #define MSM_PULL_DOWN       1
0312 #define MSM_KEEPER      2
0313 #define MSM_PULL_UP_NO_KEEPER   2
0314 #define MSM_PULL_UP     3
0315 
0316 static unsigned msm_regval_to_drive(u32 val)
0317 {
0318     return (val + 1) * 2;
0319 }
0320 
0321 static int msm_config_group_get(struct pinctrl_dev *pctldev,
0322                 unsigned int group,
0323                 unsigned long *config)
0324 {
0325     const struct msm_pingroup *g;
0326     struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
0327     unsigned param = pinconf_to_config_param(*config);
0328     unsigned mask;
0329     unsigned arg;
0330     unsigned bit;
0331     int ret;
0332     u32 val;
0333 
0334     g = &pctrl->soc->groups[group];
0335 
0336     ret = msm_config_reg(pctrl, g, param, &mask, &bit);
0337     if (ret < 0)
0338         return ret;
0339 
0340     val = msm_readl_ctl(pctrl, g);
0341     arg = (val >> bit) & mask;
0342 
0343     /* Convert register value to pinconf value */
0344     switch (param) {
0345     case PIN_CONFIG_BIAS_DISABLE:
0346         if (arg != MSM_NO_PULL)
0347             return -EINVAL;
0348         arg = 1;
0349         break;
0350     case PIN_CONFIG_BIAS_PULL_DOWN:
0351         if (arg != MSM_PULL_DOWN)
0352             return -EINVAL;
0353         arg = 1;
0354         break;
0355     case PIN_CONFIG_BIAS_BUS_HOLD:
0356         if (pctrl->soc->pull_no_keeper)
0357             return -ENOTSUPP;
0358 
0359         if (arg != MSM_KEEPER)
0360             return -EINVAL;
0361         arg = 1;
0362         break;
0363     case PIN_CONFIG_BIAS_PULL_UP:
0364         if (pctrl->soc->pull_no_keeper)
0365             arg = arg == MSM_PULL_UP_NO_KEEPER;
0366         else
0367             arg = arg == MSM_PULL_UP;
0368         if (!arg)
0369             return -EINVAL;
0370         break;
0371     case PIN_CONFIG_DRIVE_OPEN_DRAIN:
0372         /* Pin is not open-drain */
0373         if (!arg)
0374             return -EINVAL;
0375         arg = 1;
0376         break;
0377     case PIN_CONFIG_DRIVE_STRENGTH:
0378         arg = msm_regval_to_drive(arg);
0379         break;
0380     case PIN_CONFIG_OUTPUT:
0381         /* Pin is not output */
0382         if (!arg)
0383             return -EINVAL;
0384 
0385         val = msm_readl_io(pctrl, g);
0386         arg = !!(val & BIT(g->in_bit));
0387         break;
0388     case PIN_CONFIG_INPUT_ENABLE:
0389         /* Pin is output */
0390         if (arg)
0391             return -EINVAL;
0392         arg = 1;
0393         break;
0394     default:
0395         return -ENOTSUPP;
0396     }
0397 
0398     *config = pinconf_to_config_packed(param, arg);
0399 
0400     return 0;
0401 }
0402 
0403 static int msm_config_group_set(struct pinctrl_dev *pctldev,
0404                 unsigned group,
0405                 unsigned long *configs,
0406                 unsigned num_configs)
0407 {
0408     const struct msm_pingroup *g;
0409     struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
0410     unsigned long flags;
0411     unsigned param;
0412     unsigned mask;
0413     unsigned arg;
0414     unsigned bit;
0415     int ret;
0416     u32 val;
0417     int i;
0418 
0419     g = &pctrl->soc->groups[group];
0420 
0421     for (i = 0; i < num_configs; i++) {
0422         param = pinconf_to_config_param(configs[i]);
0423         arg = pinconf_to_config_argument(configs[i]);
0424 
0425         ret = msm_config_reg(pctrl, g, param, &mask, &bit);
0426         if (ret < 0)
0427             return ret;
0428 
0429         /* Convert pinconf values to register values */
0430         switch (param) {
0431         case PIN_CONFIG_BIAS_DISABLE:
0432             arg = MSM_NO_PULL;
0433             break;
0434         case PIN_CONFIG_BIAS_PULL_DOWN:
0435             arg = MSM_PULL_DOWN;
0436             break;
0437         case PIN_CONFIG_BIAS_BUS_HOLD:
0438             if (pctrl->soc->pull_no_keeper)
0439                 return -ENOTSUPP;
0440 
0441             arg = MSM_KEEPER;
0442             break;
0443         case PIN_CONFIG_BIAS_PULL_UP:
0444             if (pctrl->soc->pull_no_keeper)
0445                 arg = MSM_PULL_UP_NO_KEEPER;
0446             else
0447                 arg = MSM_PULL_UP;
0448             break;
0449         case PIN_CONFIG_DRIVE_OPEN_DRAIN:
0450             arg = 1;
0451             break;
0452         case PIN_CONFIG_DRIVE_STRENGTH:
0453             /* Check for invalid values */
0454             if (arg > 16 || arg < 2 || (arg % 2) != 0)
0455                 arg = -1;
0456             else
0457                 arg = (arg / 2) - 1;
0458             break;
0459         case PIN_CONFIG_OUTPUT:
0460             /* set output value */
0461             raw_spin_lock_irqsave(&pctrl->lock, flags);
0462             val = msm_readl_io(pctrl, g);
0463             if (arg)
0464                 val |= BIT(g->out_bit);
0465             else
0466                 val &= ~BIT(g->out_bit);
0467             msm_writel_io(val, pctrl, g);
0468             raw_spin_unlock_irqrestore(&pctrl->lock, flags);
0469 
0470             /* enable output */
0471             arg = 1;
0472             break;
0473         case PIN_CONFIG_INPUT_ENABLE:
0474             /* disable output */
0475             arg = 0;
0476             break;
0477         default:
0478             dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
0479                 param);
0480             return -EINVAL;
0481         }
0482 
0483         /* Range-check user-supplied value */
0484         if (arg & ~mask) {
0485             dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
0486             return -EINVAL;
0487         }
0488 
0489         raw_spin_lock_irqsave(&pctrl->lock, flags);
0490         val = msm_readl_ctl(pctrl, g);
0491         val &= ~(mask << bit);
0492         val |= arg << bit;
0493         msm_writel_ctl(val, pctrl, g);
0494         raw_spin_unlock_irqrestore(&pctrl->lock, flags);
0495     }
0496 
0497     return 0;
0498 }
0499 
0500 static const struct pinconf_ops msm_pinconf_ops = {
0501     .is_generic     = true,
0502     .pin_config_group_get   = msm_config_group_get,
0503     .pin_config_group_set   = msm_config_group_set,
0504 };
0505 
0506 static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
0507 {
0508     const struct msm_pingroup *g;
0509     struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
0510     unsigned long flags;
0511     u32 val;
0512 
0513     g = &pctrl->soc->groups[offset];
0514 
0515     raw_spin_lock_irqsave(&pctrl->lock, flags);
0516 
0517     val = msm_readl_ctl(pctrl, g);
0518     val &= ~BIT(g->oe_bit);
0519     msm_writel_ctl(val, pctrl, g);
0520 
0521     raw_spin_unlock_irqrestore(&pctrl->lock, flags);
0522 
0523     return 0;
0524 }
0525 
0526 static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
0527 {
0528     const struct msm_pingroup *g;
0529     struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
0530     unsigned long flags;
0531     u32 val;
0532 
0533     g = &pctrl->soc->groups[offset];
0534 
0535     raw_spin_lock_irqsave(&pctrl->lock, flags);
0536 
0537     val = msm_readl_io(pctrl, g);
0538     if (value)
0539         val |= BIT(g->out_bit);
0540     else
0541         val &= ~BIT(g->out_bit);
0542     msm_writel_io(val, pctrl, g);
0543 
0544     val = msm_readl_ctl(pctrl, g);
0545     val |= BIT(g->oe_bit);
0546     msm_writel_ctl(val, pctrl, g);
0547 
0548     raw_spin_unlock_irqrestore(&pctrl->lock, flags);
0549 
0550     return 0;
0551 }
0552 
0553 static int msm_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
0554 {
0555     struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
0556     const struct msm_pingroup *g;
0557     u32 val;
0558 
0559     g = &pctrl->soc->groups[offset];
0560 
0561     val = msm_readl_ctl(pctrl, g);
0562 
0563     return val & BIT(g->oe_bit) ? GPIO_LINE_DIRECTION_OUT :
0564                       GPIO_LINE_DIRECTION_IN;
0565 }
0566 
0567 static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
0568 {
0569     const struct msm_pingroup *g;
0570     struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
0571     u32 val;
0572 
0573     g = &pctrl->soc->groups[offset];
0574 
0575     val = msm_readl_io(pctrl, g);
0576     return !!(val & BIT(g->in_bit));
0577 }
0578 
0579 static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
0580 {
0581     const struct msm_pingroup *g;
0582     struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
0583     unsigned long flags;
0584     u32 val;
0585 
0586     g = &pctrl->soc->groups[offset];
0587 
0588     raw_spin_lock_irqsave(&pctrl->lock, flags);
0589 
0590     val = msm_readl_io(pctrl, g);
0591     if (value)
0592         val |= BIT(g->out_bit);
0593     else
0594         val &= ~BIT(g->out_bit);
0595     msm_writel_io(val, pctrl, g);
0596 
0597     raw_spin_unlock_irqrestore(&pctrl->lock, flags);
0598 }
0599 
0600 #ifdef CONFIG_DEBUG_FS
0601 #include <linux/seq_file.h>
0602 
0603 static void msm_gpio_dbg_show_one(struct seq_file *s,
0604                   struct pinctrl_dev *pctldev,
0605                   struct gpio_chip *chip,
0606                   unsigned offset,
0607                   unsigned gpio)
0608 {
0609     const struct msm_pingroup *g;
0610     struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
0611     unsigned func;
0612     int is_out;
0613     int drive;
0614     int pull;
0615     int val;
0616     int egpio_enable;
0617     u32 ctl_reg, io_reg;
0618 
0619     static const char * const pulls_keeper[] = {
0620         "no pull",
0621         "pull down",
0622         "keeper",
0623         "pull up"
0624     };
0625 
0626     static const char * const pulls_no_keeper[] = {
0627         "no pull",
0628         "pull down",
0629         "pull up",
0630     };
0631 
0632     if (!gpiochip_line_is_valid(chip, offset))
0633         return;
0634 
0635     g = &pctrl->soc->groups[offset];
0636     ctl_reg = msm_readl_ctl(pctrl, g);
0637     io_reg = msm_readl_io(pctrl, g);
0638 
0639     is_out = !!(ctl_reg & BIT(g->oe_bit));
0640     func = (ctl_reg >> g->mux_bit) & 7;
0641     drive = (ctl_reg >> g->drv_bit) & 7;
0642     pull = (ctl_reg >> g->pull_bit) & 3;
0643     egpio_enable = 0;
0644     if (pctrl->soc->egpio_func && ctl_reg & BIT(g->egpio_present))
0645         egpio_enable = !(ctl_reg & BIT(g->egpio_enable));
0646 
0647     if (is_out)
0648         val = !!(io_reg & BIT(g->out_bit));
0649     else
0650         val = !!(io_reg & BIT(g->in_bit));
0651 
0652     if (egpio_enable) {
0653         seq_printf(s, " %-8s: egpio\n", g->name);
0654         return;
0655     }
0656 
0657     seq_printf(s, " %-8s: %-3s", g->name, is_out ? "out" : "in");
0658     seq_printf(s, " %-4s func%d", val ? "high" : "low", func);
0659     seq_printf(s, " %dmA", msm_regval_to_drive(drive));
0660     if (pctrl->soc->pull_no_keeper)
0661         seq_printf(s, " %s", pulls_no_keeper[pull]);
0662     else
0663         seq_printf(s, " %s", pulls_keeper[pull]);
0664     seq_puts(s, "\n");
0665 }
0666 
0667 static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
0668 {
0669     unsigned gpio = chip->base;
0670     unsigned i;
0671 
0672     for (i = 0; i < chip->ngpio; i++, gpio++)
0673         msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
0674 }
0675 
0676 #else
0677 #define msm_gpio_dbg_show NULL
0678 #endif
0679 
0680 static int msm_gpio_init_valid_mask(struct gpio_chip *gc,
0681                     unsigned long *valid_mask,
0682                     unsigned int ngpios)
0683 {
0684     struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
0685     int ret;
0686     unsigned int len, i;
0687     const int *reserved = pctrl->soc->reserved_gpios;
0688     u16 *tmp;
0689 
0690     /* Driver provided reserved list overrides DT and ACPI */
0691     if (reserved) {
0692         bitmap_fill(valid_mask, ngpios);
0693         for (i = 0; reserved[i] >= 0; i++) {
0694             if (i >= ngpios || reserved[i] >= ngpios) {
0695                 dev_err(pctrl->dev, "invalid list of reserved GPIOs\n");
0696                 return -EINVAL;
0697             }
0698             clear_bit(reserved[i], valid_mask);
0699         }
0700 
0701         return 0;
0702     }
0703 
0704     /* The number of GPIOs in the ACPI tables */
0705     len = ret = device_property_count_u16(pctrl->dev, "gpios");
0706     if (ret < 0)
0707         return 0;
0708 
0709     if (ret > ngpios)
0710         return -EINVAL;
0711 
0712     tmp = kmalloc_array(len, sizeof(*tmp), GFP_KERNEL);
0713     if (!tmp)
0714         return -ENOMEM;
0715 
0716     ret = device_property_read_u16_array(pctrl->dev, "gpios", tmp, len);
0717     if (ret < 0) {
0718         dev_err(pctrl->dev, "could not read list of GPIOs\n");
0719         goto out;
0720     }
0721 
0722     bitmap_zero(valid_mask, ngpios);
0723     for (i = 0; i < len; i++)
0724         set_bit(tmp[i], valid_mask);
0725 
0726 out:
0727     kfree(tmp);
0728     return ret;
0729 }
0730 
0731 static const struct gpio_chip msm_gpio_template = {
0732     .direction_input  = msm_gpio_direction_input,
0733     .direction_output = msm_gpio_direction_output,
0734     .get_direction    = msm_gpio_get_direction,
0735     .get              = msm_gpio_get,
0736     .set              = msm_gpio_set,
0737     .request          = gpiochip_generic_request,
0738     .free             = gpiochip_generic_free,
0739     .dbg_show         = msm_gpio_dbg_show,
0740 };
0741 
0742 /* For dual-edge interrupts in software, since some hardware has no
0743  * such support:
0744  *
0745  * At appropriate moments, this function may be called to flip the polarity
0746  * settings of both-edge irq lines to try and catch the next edge.
0747  *
0748  * The attempt is considered successful if:
0749  * - the status bit goes high, indicating that an edge was caught, or
0750  * - the input value of the gpio doesn't change during the attempt.
0751  * If the value changes twice during the process, that would cause the first
0752  * test to fail but would force the second, as two opposite
0753  * transitions would cause a detection no matter the polarity setting.
0754  *
0755  * The do-loop tries to sledge-hammer closed the timing hole between
0756  * the initial value-read and the polarity-write - if the line value changes
0757  * during that window, an interrupt is lost, the new polarity setting is
0758  * incorrect, and the first success test will fail, causing a retry.
0759  *
0760  * Algorithm comes from Google's msmgpio driver.
0761  */
0762 static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
0763                       const struct msm_pingroup *g,
0764                       struct irq_data *d)
0765 {
0766     int loop_limit = 100;
0767     unsigned val, val2, intstat;
0768     unsigned pol;
0769 
0770     do {
0771         val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
0772 
0773         pol = msm_readl_intr_cfg(pctrl, g);
0774         pol ^= BIT(g->intr_polarity_bit);
0775         msm_writel_intr_cfg(pol, pctrl, g);
0776 
0777         val2 = msm_readl_io(pctrl, g) & BIT(g->in_bit);
0778         intstat = msm_readl_intr_status(pctrl, g);
0779         if (intstat || (val == val2))
0780             return;
0781     } while (loop_limit-- > 0);
0782     dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
0783         val, val2);
0784 }
0785 
0786 static void msm_gpio_irq_mask(struct irq_data *d)
0787 {
0788     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0789     struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
0790     const struct msm_pingroup *g;
0791     unsigned long flags;
0792     u32 val;
0793 
0794     if (d->parent_data)
0795         irq_chip_mask_parent(d);
0796 
0797     if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
0798         return;
0799 
0800     g = &pctrl->soc->groups[d->hwirq];
0801 
0802     raw_spin_lock_irqsave(&pctrl->lock, flags);
0803 
0804     val = msm_readl_intr_cfg(pctrl, g);
0805     /*
0806      * There are two bits that control interrupt forwarding to the CPU. The
0807      * RAW_STATUS_EN bit causes the level or edge sensed on the line to be
0808      * latched into the interrupt status register when the hardware detects
0809      * an irq that it's configured for (either edge for edge type or level
0810      * for level type irq). The 'non-raw' status enable bit causes the
0811      * hardware to assert the summary interrupt to the CPU if the latched
0812      * status bit is set. There's a bug though, the edge detection logic
0813      * seems to have a problem where toggling the RAW_STATUS_EN bit may
0814      * cause the status bit to latch spuriously when there isn't any edge
0815      * so we can't touch that bit for edge type irqs and we have to keep
0816      * the bit set anyway so that edges are latched while the line is masked.
0817      *
0818      * To make matters more complicated, leaving the RAW_STATUS_EN bit
0819      * enabled all the time causes level interrupts to re-latch into the
0820      * status register because the level is still present on the line after
0821      * we ack it. We clear the raw status enable bit during mask here and
0822      * set the bit on unmask so the interrupt can't latch into the hardware
0823      * while it's masked.
0824      */
0825     if (irqd_get_trigger_type(d) & IRQ_TYPE_LEVEL_MASK)
0826         val &= ~BIT(g->intr_raw_status_bit);
0827 
0828     val &= ~BIT(g->intr_enable_bit);
0829     msm_writel_intr_cfg(val, pctrl, g);
0830 
0831     clear_bit(d->hwirq, pctrl->enabled_irqs);
0832 
0833     raw_spin_unlock_irqrestore(&pctrl->lock, flags);
0834 }
0835 
0836 static void msm_gpio_irq_unmask(struct irq_data *d)
0837 {
0838     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0839     struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
0840     const struct msm_pingroup *g;
0841     unsigned long flags;
0842     u32 val;
0843 
0844     if (d->parent_data)
0845         irq_chip_unmask_parent(d);
0846 
0847     if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
0848         return;
0849 
0850     g = &pctrl->soc->groups[d->hwirq];
0851 
0852     raw_spin_lock_irqsave(&pctrl->lock, flags);
0853 
0854     val = msm_readl_intr_cfg(pctrl, g);
0855     val |= BIT(g->intr_raw_status_bit);
0856     val |= BIT(g->intr_enable_bit);
0857     msm_writel_intr_cfg(val, pctrl, g);
0858 
0859     set_bit(d->hwirq, pctrl->enabled_irqs);
0860 
0861     raw_spin_unlock_irqrestore(&pctrl->lock, flags);
0862 }
0863 
0864 static void msm_gpio_irq_enable(struct irq_data *d)
0865 {
0866     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0867     struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
0868 
0869     gpiochip_enable_irq(gc, d->hwirq);
0870 
0871     if (d->parent_data)
0872         irq_chip_enable_parent(d);
0873 
0874     if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
0875         msm_gpio_irq_unmask(d);
0876 }
0877 
0878 static void msm_gpio_irq_disable(struct irq_data *d)
0879 {
0880     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0881     struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
0882 
0883     if (d->parent_data)
0884         irq_chip_disable_parent(d);
0885 
0886     if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
0887         msm_gpio_irq_mask(d);
0888 
0889     gpiochip_disable_irq(gc, d->hwirq);
0890 }
0891 
0892 /**
0893  * msm_gpio_update_dual_edge_parent() - Prime next edge for IRQs handled by parent.
0894  * @d: The irq dta.
0895  *
0896  * This is much like msm_gpio_update_dual_edge_pos() but for IRQs that are
0897  * normally handled by the parent irqchip.  The logic here is slightly
0898  * different due to what's easy to do with our parent, but in principle it's
0899  * the same.
0900  */
0901 static void msm_gpio_update_dual_edge_parent(struct irq_data *d)
0902 {
0903     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0904     struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
0905     const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
0906     int loop_limit = 100;
0907     unsigned int val;
0908     unsigned int type;
0909 
0910     /* Read the value and make a guess about what edge we need to catch */
0911     val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
0912     type = val ? IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
0913 
0914     do {
0915         /* Set the parent to catch the next edge */
0916         irq_chip_set_type_parent(d, type);
0917 
0918         /*
0919          * Possibly the line changed between when we last read "val"
0920          * (and decided what edge we needed) and when set the edge.
0921          * If the value didn't change (or changed and then changed
0922          * back) then we're done.
0923          */
0924         val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
0925         if (type == IRQ_TYPE_EDGE_RISING) {
0926             if (!val)
0927                 return;
0928             type = IRQ_TYPE_EDGE_FALLING;
0929         } else if (type == IRQ_TYPE_EDGE_FALLING) {
0930             if (val)
0931                 return;
0932             type = IRQ_TYPE_EDGE_RISING;
0933         }
0934     } while (loop_limit-- > 0);
0935     dev_warn_once(pctrl->dev, "dual-edge irq failed to stabilize\n");
0936 }
0937 
0938 static void msm_gpio_irq_ack(struct irq_data *d)
0939 {
0940     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0941     struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
0942     const struct msm_pingroup *g;
0943     unsigned long flags;
0944 
0945     if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
0946         if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
0947             msm_gpio_update_dual_edge_parent(d);
0948         return;
0949     }
0950 
0951     g = &pctrl->soc->groups[d->hwirq];
0952 
0953     raw_spin_lock_irqsave(&pctrl->lock, flags);
0954 
0955     msm_ack_intr_status(pctrl, g);
0956 
0957     if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
0958         msm_gpio_update_dual_edge_pos(pctrl, g, d);
0959 
0960     raw_spin_unlock_irqrestore(&pctrl->lock, flags);
0961 }
0962 
0963 static void msm_gpio_irq_eoi(struct irq_data *d)
0964 {
0965     d = d->parent_data;
0966 
0967     if (d)
0968         d->chip->irq_eoi(d);
0969 }
0970 
0971 static bool msm_gpio_needs_dual_edge_parent_workaround(struct irq_data *d,
0972                                unsigned int type)
0973 {
0974     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0975     struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
0976 
0977     return type == IRQ_TYPE_EDGE_BOTH &&
0978            pctrl->soc->wakeirq_dual_edge_errata && d->parent_data &&
0979            test_bit(d->hwirq, pctrl->skip_wake_irqs);
0980 }
0981 
0982 static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
0983 {
0984     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
0985     struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
0986     const struct msm_pingroup *g;
0987     unsigned long flags;
0988     bool was_enabled;
0989     u32 val;
0990 
0991     if (msm_gpio_needs_dual_edge_parent_workaround(d, type)) {
0992         set_bit(d->hwirq, pctrl->dual_edge_irqs);
0993         irq_set_handler_locked(d, handle_fasteoi_ack_irq);
0994         msm_gpio_update_dual_edge_parent(d);
0995         return 0;
0996     }
0997 
0998     if (d->parent_data)
0999         irq_chip_set_type_parent(d, type);
1000 
1001     if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
1002         clear_bit(d->hwirq, pctrl->dual_edge_irqs);
1003         irq_set_handler_locked(d, handle_fasteoi_irq);
1004         return 0;
1005     }
1006 
1007     g = &pctrl->soc->groups[d->hwirq];
1008 
1009     raw_spin_lock_irqsave(&pctrl->lock, flags);
1010 
1011     /*
1012      * For hw without possibility of detecting both edges
1013      */
1014     if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
1015         set_bit(d->hwirq, pctrl->dual_edge_irqs);
1016     else
1017         clear_bit(d->hwirq, pctrl->dual_edge_irqs);
1018 
1019     /* Route interrupts to application cpu.
1020      * With intr_target_use_scm interrupts are routed to
1021      * application cpu using scm calls.
1022      */
1023     if (pctrl->intr_target_use_scm) {
1024         u32 addr = pctrl->phys_base[0] + g->intr_target_reg;
1025         int ret;
1026 
1027         qcom_scm_io_readl(addr, &val);
1028 
1029         val &= ~(7 << g->intr_target_bit);
1030         val |= g->intr_target_kpss_val << g->intr_target_bit;
1031 
1032         ret = qcom_scm_io_writel(addr, val);
1033         if (ret)
1034             dev_err(pctrl->dev,
1035                 "Failed routing %lu interrupt to Apps proc",
1036                 d->hwirq);
1037     } else {
1038         val = msm_readl_intr_target(pctrl, g);
1039         val &= ~(7 << g->intr_target_bit);
1040         val |= g->intr_target_kpss_val << g->intr_target_bit;
1041         msm_writel_intr_target(val, pctrl, g);
1042     }
1043 
1044     /* Update configuration for gpio.
1045      * RAW_STATUS_EN is left on for all gpio irqs. Due to the
1046      * internal circuitry of TLMM, toggling the RAW_STATUS
1047      * could cause the INTR_STATUS to be set for EDGE interrupts.
1048      */
1049     val = msm_readl_intr_cfg(pctrl, g);
1050     was_enabled = val & BIT(g->intr_raw_status_bit);
1051     val |= BIT(g->intr_raw_status_bit);
1052     if (g->intr_detection_width == 2) {
1053         val &= ~(3 << g->intr_detection_bit);
1054         val &= ~(1 << g->intr_polarity_bit);
1055         switch (type) {
1056         case IRQ_TYPE_EDGE_RISING:
1057             val |= 1 << g->intr_detection_bit;
1058             val |= BIT(g->intr_polarity_bit);
1059             break;
1060         case IRQ_TYPE_EDGE_FALLING:
1061             val |= 2 << g->intr_detection_bit;
1062             val |= BIT(g->intr_polarity_bit);
1063             break;
1064         case IRQ_TYPE_EDGE_BOTH:
1065             val |= 3 << g->intr_detection_bit;
1066             val |= BIT(g->intr_polarity_bit);
1067             break;
1068         case IRQ_TYPE_LEVEL_LOW:
1069             break;
1070         case IRQ_TYPE_LEVEL_HIGH:
1071             val |= BIT(g->intr_polarity_bit);
1072             break;
1073         }
1074     } else if (g->intr_detection_width == 1) {
1075         val &= ~(1 << g->intr_detection_bit);
1076         val &= ~(1 << g->intr_polarity_bit);
1077         switch (type) {
1078         case IRQ_TYPE_EDGE_RISING:
1079             val |= BIT(g->intr_detection_bit);
1080             val |= BIT(g->intr_polarity_bit);
1081             break;
1082         case IRQ_TYPE_EDGE_FALLING:
1083             val |= BIT(g->intr_detection_bit);
1084             break;
1085         case IRQ_TYPE_EDGE_BOTH:
1086             val |= BIT(g->intr_detection_bit);
1087             val |= BIT(g->intr_polarity_bit);
1088             break;
1089         case IRQ_TYPE_LEVEL_LOW:
1090             break;
1091         case IRQ_TYPE_LEVEL_HIGH:
1092             val |= BIT(g->intr_polarity_bit);
1093             break;
1094         }
1095     } else {
1096         BUG();
1097     }
1098     msm_writel_intr_cfg(val, pctrl, g);
1099 
1100     /*
1101      * The first time we set RAW_STATUS_EN it could trigger an interrupt.
1102      * Clear the interrupt.  This is safe because we have
1103      * IRQCHIP_SET_TYPE_MASKED.
1104      */
1105     if (!was_enabled)
1106         msm_ack_intr_status(pctrl, g);
1107 
1108     if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
1109         msm_gpio_update_dual_edge_pos(pctrl, g, d);
1110 
1111     raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1112 
1113     if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
1114         irq_set_handler_locked(d, handle_level_irq);
1115     else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
1116         irq_set_handler_locked(d, handle_edge_irq);
1117 
1118     return 0;
1119 }
1120 
1121 static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
1122 {
1123     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1124     struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1125 
1126     /*
1127      * While they may not wake up when the TLMM is powered off,
1128      * some GPIOs would like to wakeup the system from suspend
1129      * when TLMM is powered on. To allow that, enable the GPIO
1130      * summary line to be wakeup capable at GIC.
1131      */
1132     if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1133         return irq_chip_set_wake_parent(d, on);
1134 
1135     return irq_set_irq_wake(pctrl->irq, on);
1136 }
1137 
1138 static int msm_gpio_irq_reqres(struct irq_data *d)
1139 {
1140     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1141     struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1142     int ret;
1143 
1144     if (!try_module_get(gc->owner))
1145         return -ENODEV;
1146 
1147     ret = msm_pinmux_request_gpio(pctrl->pctrl, NULL, d->hwirq);
1148     if (ret)
1149         goto out;
1150     msm_gpio_direction_input(gc, d->hwirq);
1151 
1152     if (gpiochip_lock_as_irq(gc, d->hwirq)) {
1153         dev_err(gc->parent,
1154             "unable to lock HW IRQ %lu for IRQ\n",
1155             d->hwirq);
1156         ret = -EINVAL;
1157         goto out;
1158     }
1159 
1160     /*
1161      * The disable / clear-enable workaround we do in msm_pinmux_set_mux()
1162      * only works if disable is not lazy since we only clear any bogus
1163      * interrupt in hardware. Explicitly mark the interrupt as UNLAZY.
1164      */
1165     irq_set_status_flags(d->irq, IRQ_DISABLE_UNLAZY);
1166 
1167     return 0;
1168 out:
1169     module_put(gc->owner);
1170     return ret;
1171 }
1172 
1173 static void msm_gpio_irq_relres(struct irq_data *d)
1174 {
1175     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1176 
1177     gpiochip_unlock_as_irq(gc, d->hwirq);
1178     module_put(gc->owner);
1179 }
1180 
1181 static int msm_gpio_irq_set_affinity(struct irq_data *d,
1182                 const struct cpumask *dest, bool force)
1183 {
1184     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1185     struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1186 
1187     if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1188         return irq_chip_set_affinity_parent(d, dest, force);
1189 
1190     return -EINVAL;
1191 }
1192 
1193 static int msm_gpio_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1194 {
1195     struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1196     struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1197 
1198     if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1199         return irq_chip_set_vcpu_affinity_parent(d, vcpu_info);
1200 
1201     return -EINVAL;
1202 }
1203 
1204 static void msm_gpio_irq_handler(struct irq_desc *desc)
1205 {
1206     struct gpio_chip *gc = irq_desc_get_handler_data(desc);
1207     const struct msm_pingroup *g;
1208     struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1209     struct irq_chip *chip = irq_desc_get_chip(desc);
1210     int handled = 0;
1211     u32 val;
1212     int i;
1213 
1214     chained_irq_enter(chip, desc);
1215 
1216     /*
1217      * Each pin has it's own IRQ status register, so use
1218      * enabled_irq bitmap to limit the number of reads.
1219      */
1220     for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
1221         g = &pctrl->soc->groups[i];
1222         val = msm_readl_intr_status(pctrl, g);
1223         if (val & BIT(g->intr_status_bit)) {
1224             generic_handle_domain_irq(gc->irq.domain, i);
1225             handled++;
1226         }
1227     }
1228 
1229     /* No interrupts were flagged */
1230     if (handled == 0)
1231         handle_bad_irq(desc);
1232 
1233     chained_irq_exit(chip, desc);
1234 }
1235 
1236 static int msm_gpio_wakeirq(struct gpio_chip *gc,
1237                 unsigned int child,
1238                 unsigned int child_type,
1239                 unsigned int *parent,
1240                 unsigned int *parent_type)
1241 {
1242     struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1243     const struct msm_gpio_wakeirq_map *map;
1244     int i;
1245 
1246     *parent = GPIO_NO_WAKE_IRQ;
1247     *parent_type = IRQ_TYPE_EDGE_RISING;
1248 
1249     for (i = 0; i < pctrl->soc->nwakeirq_map; i++) {
1250         map = &pctrl->soc->wakeirq_map[i];
1251         if (map->gpio == child) {
1252             *parent = map->wakeirq;
1253             break;
1254         }
1255     }
1256 
1257     return 0;
1258 }
1259 
1260 static bool msm_gpio_needs_valid_mask(struct msm_pinctrl *pctrl)
1261 {
1262     if (pctrl->soc->reserved_gpios)
1263         return true;
1264 
1265     return device_property_count_u16(pctrl->dev, "gpios") > 0;
1266 }
1267 
1268 static const struct irq_chip msm_gpio_irq_chip = {
1269     .name           = "msmgpio",
1270     .irq_enable     = msm_gpio_irq_enable,
1271     .irq_disable        = msm_gpio_irq_disable,
1272     .irq_mask       = msm_gpio_irq_mask,
1273     .irq_unmask     = msm_gpio_irq_unmask,
1274     .irq_ack        = msm_gpio_irq_ack,
1275     .irq_eoi        = msm_gpio_irq_eoi,
1276     .irq_set_type       = msm_gpio_irq_set_type,
1277     .irq_set_wake       = msm_gpio_irq_set_wake,
1278     .irq_request_resources  = msm_gpio_irq_reqres,
1279     .irq_release_resources  = msm_gpio_irq_relres,
1280     .irq_set_affinity   = msm_gpio_irq_set_affinity,
1281     .irq_set_vcpu_affinity  = msm_gpio_irq_set_vcpu_affinity,
1282     .flags          = (IRQCHIP_MASK_ON_SUSPEND |
1283                    IRQCHIP_SET_TYPE_MASKED |
1284                    IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND |
1285                    IRQCHIP_IMMUTABLE),
1286 };
1287 
1288 static int msm_gpio_init(struct msm_pinctrl *pctrl)
1289 {
1290     struct gpio_chip *chip;
1291     struct gpio_irq_chip *girq;
1292     int i, ret;
1293     unsigned gpio, ngpio = pctrl->soc->ngpios;
1294     struct device_node *np;
1295     bool skip;
1296 
1297     if (WARN_ON(ngpio > MAX_NR_GPIO))
1298         return -EINVAL;
1299 
1300     chip = &pctrl->chip;
1301     chip->base = -1;
1302     chip->ngpio = ngpio;
1303     chip->label = dev_name(pctrl->dev);
1304     chip->parent = pctrl->dev;
1305     chip->owner = THIS_MODULE;
1306     if (msm_gpio_needs_valid_mask(pctrl))
1307         chip->init_valid_mask = msm_gpio_init_valid_mask;
1308 
1309     np = of_parse_phandle(pctrl->dev->of_node, "wakeup-parent", 0);
1310     if (np) {
1311         chip->irq.parent_domain = irq_find_matching_host(np,
1312                          DOMAIN_BUS_WAKEUP);
1313         of_node_put(np);
1314         if (!chip->irq.parent_domain)
1315             return -EPROBE_DEFER;
1316         chip->irq.child_to_parent_hwirq = msm_gpio_wakeirq;
1317         /*
1318          * Let's skip handling the GPIOs, if the parent irqchip
1319          * is handling the direct connect IRQ of the GPIO.
1320          */
1321         skip = irq_domain_qcom_handle_wakeup(chip->irq.parent_domain);
1322         for (i = 0; skip && i < pctrl->soc->nwakeirq_map; i++) {
1323             gpio = pctrl->soc->wakeirq_map[i].gpio;
1324             set_bit(gpio, pctrl->skip_wake_irqs);
1325         }
1326     }
1327 
1328     girq = &chip->irq;
1329     gpio_irq_chip_set_chip(girq, &msm_gpio_irq_chip);
1330     girq->parent_handler = msm_gpio_irq_handler;
1331     girq->fwnode = pctrl->dev->fwnode;
1332     girq->num_parents = 1;
1333     girq->parents = devm_kcalloc(pctrl->dev, 1, sizeof(*girq->parents),
1334                      GFP_KERNEL);
1335     if (!girq->parents)
1336         return -ENOMEM;
1337     girq->default_type = IRQ_TYPE_NONE;
1338     girq->handler = handle_bad_irq;
1339     girq->parents[0] = pctrl->irq;
1340 
1341     ret = gpiochip_add_data(&pctrl->chip, pctrl);
1342     if (ret) {
1343         dev_err(pctrl->dev, "Failed register gpiochip\n");
1344         return ret;
1345     }
1346 
1347     /*
1348      * For DeviceTree-supported systems, the gpio core checks the
1349      * pinctrl's device node for the "gpio-ranges" property.
1350      * If it is present, it takes care of adding the pin ranges
1351      * for the driver. In this case the driver can skip ahead.
1352      *
1353      * In order to remain compatible with older, existing DeviceTree
1354      * files which don't set the "gpio-ranges" property or systems that
1355      * utilize ACPI the driver has to call gpiochip_add_pin_range().
1356      */
1357     if (!of_property_read_bool(pctrl->dev->of_node, "gpio-ranges")) {
1358         ret = gpiochip_add_pin_range(&pctrl->chip,
1359             dev_name(pctrl->dev), 0, 0, chip->ngpio);
1360         if (ret) {
1361             dev_err(pctrl->dev, "Failed to add pin range\n");
1362             gpiochip_remove(&pctrl->chip);
1363             return ret;
1364         }
1365     }
1366 
1367     return 0;
1368 }
1369 
1370 static int msm_ps_hold_restart(struct notifier_block *nb, unsigned long action,
1371                    void *data)
1372 {
1373     struct msm_pinctrl *pctrl = container_of(nb, struct msm_pinctrl, restart_nb);
1374 
1375     writel(0, pctrl->regs[0] + PS_HOLD_OFFSET);
1376     mdelay(1000);
1377     return NOTIFY_DONE;
1378 }
1379 
1380 static struct msm_pinctrl *poweroff_pctrl;
1381 
1382 static void msm_ps_hold_poweroff(void)
1383 {
1384     msm_ps_hold_restart(&poweroff_pctrl->restart_nb, 0, NULL);
1385 }
1386 
1387 static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
1388 {
1389     int i;
1390     const struct msm_function *func = pctrl->soc->functions;
1391 
1392     for (i = 0; i < pctrl->soc->nfunctions; i++)
1393         if (!strcmp(func[i].name, "ps_hold")) {
1394             pctrl->restart_nb.notifier_call = msm_ps_hold_restart;
1395             pctrl->restart_nb.priority = 128;
1396             if (register_restart_handler(&pctrl->restart_nb))
1397                 dev_err(pctrl->dev,
1398                     "failed to setup restart handler.\n");
1399             poweroff_pctrl = pctrl;
1400             pm_power_off = msm_ps_hold_poweroff;
1401             break;
1402         }
1403 }
1404 
1405 static __maybe_unused int msm_pinctrl_suspend(struct device *dev)
1406 {
1407     struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
1408 
1409     return pinctrl_force_sleep(pctrl->pctrl);
1410 }
1411 
1412 static __maybe_unused int msm_pinctrl_resume(struct device *dev)
1413 {
1414     struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
1415 
1416     return pinctrl_force_default(pctrl->pctrl);
1417 }
1418 
1419 SIMPLE_DEV_PM_OPS(msm_pinctrl_dev_pm_ops, msm_pinctrl_suspend,
1420           msm_pinctrl_resume);
1421 
1422 EXPORT_SYMBOL(msm_pinctrl_dev_pm_ops);
1423 
1424 int msm_pinctrl_probe(struct platform_device *pdev,
1425               const struct msm_pinctrl_soc_data *soc_data)
1426 {
1427     struct msm_pinctrl *pctrl;
1428     struct resource *res;
1429     int ret;
1430     int i;
1431 
1432     pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1433     if (!pctrl)
1434         return -ENOMEM;
1435 
1436     pctrl->dev = &pdev->dev;
1437     pctrl->soc = soc_data;
1438     pctrl->chip = msm_gpio_template;
1439     pctrl->intr_target_use_scm = of_device_is_compatible(
1440                     pctrl->dev->of_node,
1441                     "qcom,ipq8064-pinctrl");
1442 
1443     raw_spin_lock_init(&pctrl->lock);
1444 
1445     if (soc_data->tiles) {
1446         for (i = 0; i < soc_data->ntiles; i++) {
1447             res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1448                                soc_data->tiles[i]);
1449             pctrl->regs[i] = devm_ioremap_resource(&pdev->dev, res);
1450             if (IS_ERR(pctrl->regs[i]))
1451                 return PTR_ERR(pctrl->regs[i]);
1452         }
1453     } else {
1454         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1455         pctrl->regs[0] = devm_ioremap_resource(&pdev->dev, res);
1456         if (IS_ERR(pctrl->regs[0]))
1457             return PTR_ERR(pctrl->regs[0]);
1458 
1459         pctrl->phys_base[0] = res->start;
1460     }
1461 
1462     msm_pinctrl_setup_pm_reset(pctrl);
1463 
1464     pctrl->irq = platform_get_irq(pdev, 0);
1465     if (pctrl->irq < 0)
1466         return pctrl->irq;
1467 
1468     pctrl->desc.owner = THIS_MODULE;
1469     pctrl->desc.pctlops = &msm_pinctrl_ops;
1470     pctrl->desc.pmxops = &msm_pinmux_ops;
1471     pctrl->desc.confops = &msm_pinconf_ops;
1472     pctrl->desc.name = dev_name(&pdev->dev);
1473     pctrl->desc.pins = pctrl->soc->pins;
1474     pctrl->desc.npins = pctrl->soc->npins;
1475 
1476     pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &pctrl->desc, pctrl);
1477     if (IS_ERR(pctrl->pctrl)) {
1478         dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
1479         return PTR_ERR(pctrl->pctrl);
1480     }
1481 
1482     ret = msm_gpio_init(pctrl);
1483     if (ret)
1484         return ret;
1485 
1486     platform_set_drvdata(pdev, pctrl);
1487 
1488     dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
1489 
1490     return 0;
1491 }
1492 EXPORT_SYMBOL(msm_pinctrl_probe);
1493 
1494 int msm_pinctrl_remove(struct platform_device *pdev)
1495 {
1496     struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
1497 
1498     gpiochip_remove(&pctrl->chip);
1499 
1500     unregister_restart_handler(&pctrl->restart_nb);
1501 
1502     return 0;
1503 }
1504 EXPORT_SYMBOL(msm_pinctrl_remove);
1505 
1506 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. TLMM driver");
1507 MODULE_LICENSE("GPL v2");