GNU Linux-libre 6.1.24-gnu
[releases.git] / drivers / pinctrl / qcom / pinctrl-msm.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2013, Sony Mobile Communications AB.
4  * Copyright (c) 2013, The Linux Foundation. All rights reserved.
5  */
6
7 #include <linux/delay.h>
8 #include <linux/err.h>
9 #include <linux/io.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/pinctrl/machine.h>
14 #include <linux/pinctrl/pinctrl.h>
15 #include <linux/pinctrl/pinmux.h>
16 #include <linux/pinctrl/pinconf.h>
17 #include <linux/pinctrl/pinconf-generic.h>
18 #include <linux/slab.h>
19 #include <linux/gpio/driver.h>
20 #include <linux/interrupt.h>
21 #include <linux/spinlock.h>
22 #include <linux/reboot.h>
23 #include <linux/pm.h>
24 #include <linux/log2.h>
25 #include <linux/qcom_scm.h>
26
27 #include <linux/soc/qcom/irq.h>
28
29 #include "../core.h"
30 #include "../pinconf.h"
31 #include "pinctrl-msm.h"
32 #include "../pinctrl-utils.h"
33
34 #define MAX_NR_GPIO 300
35 #define MAX_NR_TILES 4
36 #define PS_HOLD_OFFSET 0x820
37
38 /**
39  * struct msm_pinctrl - state for a pinctrl-msm device
40  * @dev:            device handle.
41  * @pctrl:          pinctrl handle.
42  * @chip:           gpiochip handle.
43  * @desc:           pin controller descriptor
44  * @restart_nb:     restart notifier block.
45  * @irq:            parent irq for the TLMM irq_chip.
46  * @intr_target_use_scm: route irq to application cpu using scm calls
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
51  *                  detection.
52  * @skip_wake_irqs: Skip IRQs that are handled by wakeup interrupt controller
53  * @disabled_for_mux: These IRQs were disabled because we muxed away.
54  * @ever_gpio:      This bit is set the first time we mux a pin to gpio_func.
55  * @soc:            Reference to soc_data of platform specific data.
56  * @regs:           Base addresses for the TLMM tiles.
57  * @phys_base:      Physical base address
58  */
59 struct msm_pinctrl {
60         struct device *dev;
61         struct pinctrl_dev *pctrl;
62         struct gpio_chip chip;
63         struct pinctrl_desc desc;
64         struct notifier_block restart_nb;
65
66         int irq;
67
68         bool intr_target_use_scm;
69
70         raw_spinlock_t lock;
71
72         DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
73         DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
74         DECLARE_BITMAP(skip_wake_irqs, MAX_NR_GPIO);
75         DECLARE_BITMAP(disabled_for_mux, MAX_NR_GPIO);
76         DECLARE_BITMAP(ever_gpio, MAX_NR_GPIO);
77
78         const struct msm_pinctrl_soc_data *soc;
79         void __iomem *regs[MAX_NR_TILES];
80         u32 phys_base[MAX_NR_TILES];
81 };
82
83 #define MSM_ACCESSOR(name) \
84 static u32 msm_readl_##name(struct msm_pinctrl *pctrl, \
85                             const struct msm_pingroup *g) \
86 { \
87         return readl(pctrl->regs[g->tile] + g->name##_reg); \
88 } \
89 static void msm_writel_##name(u32 val, struct msm_pinctrl *pctrl, \
90                               const struct msm_pingroup *g) \
91 { \
92         writel(val, pctrl->regs[g->tile] + g->name##_reg); \
93 }
94
95 MSM_ACCESSOR(ctl)
96 MSM_ACCESSOR(io)
97 MSM_ACCESSOR(intr_cfg)
98 MSM_ACCESSOR(intr_status)
99 MSM_ACCESSOR(intr_target)
100
101 static void msm_ack_intr_status(struct msm_pinctrl *pctrl,
102                                 const struct msm_pingroup *g)
103 {
104         u32 val = g->intr_ack_high ? BIT(g->intr_status_bit) : 0;
105
106         msm_writel_intr_status(val, pctrl, g);
107 }
108
109 static int msm_get_groups_count(struct pinctrl_dev *pctldev)
110 {
111         struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
112
113         return pctrl->soc->ngroups;
114 }
115
116 static const char *msm_get_group_name(struct pinctrl_dev *pctldev,
117                                       unsigned group)
118 {
119         struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
120
121         return pctrl->soc->groups[group].name;
122 }
123
124 static int msm_get_group_pins(struct pinctrl_dev *pctldev,
125                               unsigned group,
126                               const unsigned **pins,
127                               unsigned *num_pins)
128 {
129         struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
130
131         *pins = pctrl->soc->groups[group].pins;
132         *num_pins = pctrl->soc->groups[group].npins;
133         return 0;
134 }
135
136 static const struct pinctrl_ops msm_pinctrl_ops = {
137         .get_groups_count       = msm_get_groups_count,
138         .get_group_name         = msm_get_group_name,
139         .get_group_pins         = msm_get_group_pins,
140         .dt_node_to_map         = pinconf_generic_dt_node_to_map_group,
141         .dt_free_map            = pinctrl_utils_free_map,
142 };
143
144 static int msm_pinmux_request(struct pinctrl_dev *pctldev, unsigned offset)
145 {
146         struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
147         struct gpio_chip *chip = &pctrl->chip;
148
149         return gpiochip_line_is_valid(chip, offset) ? 0 : -EINVAL;
150 }
151
152 static int msm_get_functions_count(struct pinctrl_dev *pctldev)
153 {
154         struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
155
156         return pctrl->soc->nfunctions;
157 }
158
159 static const char *msm_get_function_name(struct pinctrl_dev *pctldev,
160                                          unsigned function)
161 {
162         struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
163
164         return pctrl->soc->functions[function].name;
165 }
166
167 static int msm_get_function_groups(struct pinctrl_dev *pctldev,
168                                    unsigned function,
169                                    const char * const **groups,
170                                    unsigned * const num_groups)
171 {
172         struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
173
174         *groups = pctrl->soc->functions[function].groups;
175         *num_groups = pctrl->soc->functions[function].ngroups;
176         return 0;
177 }
178
179 static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
180                               unsigned function,
181                               unsigned group)
182 {
183         struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
184         struct gpio_chip *gc = &pctrl->chip;
185         unsigned int irq = irq_find_mapping(gc->irq.domain, group);
186         struct irq_data *d = irq_get_irq_data(irq);
187         unsigned int gpio_func = pctrl->soc->gpio_func;
188         unsigned int egpio_func = pctrl->soc->egpio_func;
189         const struct msm_pingroup *g;
190         unsigned long flags;
191         u32 val, mask;
192         int i;
193
194         g = &pctrl->soc->groups[group];
195         mask = GENMASK(g->mux_bit + order_base_2(g->nfuncs) - 1, g->mux_bit);
196
197         for (i = 0; i < g->nfuncs; i++) {
198                 if (g->funcs[i] == function)
199                         break;
200         }
201
202         if (WARN_ON(i == g->nfuncs))
203                 return -EINVAL;
204
205         /*
206          * If an GPIO interrupt is setup on this pin then we need special
207          * handling.  Specifically interrupt detection logic will still see
208          * the pin twiddle even when we're muxed away.
209          *
210          * When we see a pin with an interrupt setup on it then we'll disable
211          * (mask) interrupts on it when we mux away until we mux back.  Note
212          * that disable_irq() refcounts and interrupts are disabled as long as
213          * at least one disable_irq() has been called.
214          */
215         if (d && i != gpio_func &&
216             !test_and_set_bit(d->hwirq, pctrl->disabled_for_mux))
217                 disable_irq(irq);
218
219         raw_spin_lock_irqsave(&pctrl->lock, flags);
220
221         val = msm_readl_ctl(pctrl, g);
222
223         /*
224          * If this is the first time muxing to GPIO and the direction is
225          * output, make sure that we're not going to be glitching the pin
226          * by reading the current state of the pin and setting it as the
227          * output.
228          */
229         if (i == gpio_func && (val & BIT(g->oe_bit)) &&
230             !test_and_set_bit(group, pctrl->ever_gpio)) {
231                 u32 io_val = msm_readl_io(pctrl, g);
232
233                 if (io_val & BIT(g->in_bit)) {
234                         if (!(io_val & BIT(g->out_bit)))
235                                 msm_writel_io(io_val | BIT(g->out_bit), pctrl, g);
236                 } else {
237                         if (io_val & BIT(g->out_bit))
238                                 msm_writel_io(io_val & ~BIT(g->out_bit), pctrl, g);
239                 }
240         }
241
242         if (egpio_func && i == egpio_func) {
243                 if (val & BIT(g->egpio_present))
244                         val &= ~BIT(g->egpio_enable);
245         } else {
246                 val &= ~mask;
247                 val |= i << g->mux_bit;
248                 /* Claim ownership of pin if egpio capable */
249                 if (egpio_func && val & BIT(g->egpio_present))
250                         val |= BIT(g->egpio_enable);
251         }
252
253         msm_writel_ctl(val, pctrl, g);
254
255         raw_spin_unlock_irqrestore(&pctrl->lock, flags);
256
257         if (d && i == gpio_func &&
258             test_and_clear_bit(d->hwirq, pctrl->disabled_for_mux)) {
259                 /*
260                  * Clear interrupts detected while not GPIO since we only
261                  * masked things.
262                  */
263                 if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
264                         irq_chip_set_parent_state(d, IRQCHIP_STATE_PENDING, false);
265                 else
266                         msm_ack_intr_status(pctrl, g);
267
268                 enable_irq(irq);
269         }
270
271         return 0;
272 }
273
274 static int msm_pinmux_request_gpio(struct pinctrl_dev *pctldev,
275                                    struct pinctrl_gpio_range *range,
276                                    unsigned offset)
277 {
278         struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
279         const struct msm_pingroup *g = &pctrl->soc->groups[offset];
280
281         /* No funcs? Probably ACPI so can't do anything here */
282         if (!g->nfuncs)
283                 return 0;
284
285         return msm_pinmux_set_mux(pctldev, g->funcs[pctrl->soc->gpio_func], offset);
286 }
287
288 static const struct pinmux_ops msm_pinmux_ops = {
289         .request                = msm_pinmux_request,
290         .get_functions_count    = msm_get_functions_count,
291         .get_function_name      = msm_get_function_name,
292         .get_function_groups    = msm_get_function_groups,
293         .gpio_request_enable    = msm_pinmux_request_gpio,
294         .set_mux                = msm_pinmux_set_mux,
295 };
296
297 static int msm_config_reg(struct msm_pinctrl *pctrl,
298                           const struct msm_pingroup *g,
299                           unsigned param,
300                           unsigned *mask,
301                           unsigned *bit)
302 {
303         switch (param) {
304         case PIN_CONFIG_BIAS_DISABLE:
305         case PIN_CONFIG_BIAS_PULL_DOWN:
306         case PIN_CONFIG_BIAS_BUS_HOLD:
307         case PIN_CONFIG_BIAS_PULL_UP:
308                 *bit = g->pull_bit;
309                 *mask = 3;
310                 break;
311         case PIN_CONFIG_DRIVE_OPEN_DRAIN:
312                 *bit = g->od_bit;
313                 *mask = 1;
314                 break;
315         case PIN_CONFIG_DRIVE_STRENGTH:
316                 *bit = g->drv_bit;
317                 *mask = 7;
318                 break;
319         case PIN_CONFIG_OUTPUT:
320         case PIN_CONFIG_INPUT_ENABLE:
321                 *bit = g->oe_bit;
322                 *mask = 1;
323                 break;
324         default:
325                 return -ENOTSUPP;
326         }
327
328         return 0;
329 }
330
331 #define MSM_NO_PULL             0
332 #define MSM_PULL_DOWN           1
333 #define MSM_KEEPER              2
334 #define MSM_PULL_UP_NO_KEEPER   2
335 #define MSM_PULL_UP             3
336
337 static unsigned msm_regval_to_drive(u32 val)
338 {
339         return (val + 1) * 2;
340 }
341
342 static int msm_config_group_get(struct pinctrl_dev *pctldev,
343                                 unsigned int group,
344                                 unsigned long *config)
345 {
346         const struct msm_pingroup *g;
347         struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
348         unsigned param = pinconf_to_config_param(*config);
349         unsigned mask;
350         unsigned arg;
351         unsigned bit;
352         int ret;
353         u32 val;
354
355         g = &pctrl->soc->groups[group];
356
357         ret = msm_config_reg(pctrl, g, param, &mask, &bit);
358         if (ret < 0)
359                 return ret;
360
361         val = msm_readl_ctl(pctrl, g);
362         arg = (val >> bit) & mask;
363
364         /* Convert register value to pinconf value */
365         switch (param) {
366         case PIN_CONFIG_BIAS_DISABLE:
367                 if (arg != MSM_NO_PULL)
368                         return -EINVAL;
369                 arg = 1;
370                 break;
371         case PIN_CONFIG_BIAS_PULL_DOWN:
372                 if (arg != MSM_PULL_DOWN)
373                         return -EINVAL;
374                 arg = 1;
375                 break;
376         case PIN_CONFIG_BIAS_BUS_HOLD:
377                 if (pctrl->soc->pull_no_keeper)
378                         return -ENOTSUPP;
379
380                 if (arg != MSM_KEEPER)
381                         return -EINVAL;
382                 arg = 1;
383                 break;
384         case PIN_CONFIG_BIAS_PULL_UP:
385                 if (pctrl->soc->pull_no_keeper)
386                         arg = arg == MSM_PULL_UP_NO_KEEPER;
387                 else
388                         arg = arg == MSM_PULL_UP;
389                 if (!arg)
390                         return -EINVAL;
391                 break;
392         case PIN_CONFIG_DRIVE_OPEN_DRAIN:
393                 /* Pin is not open-drain */
394                 if (!arg)
395                         return -EINVAL;
396                 arg = 1;
397                 break;
398         case PIN_CONFIG_DRIVE_STRENGTH:
399                 arg = msm_regval_to_drive(arg);
400                 break;
401         case PIN_CONFIG_OUTPUT:
402                 /* Pin is not output */
403                 if (!arg)
404                         return -EINVAL;
405
406                 val = msm_readl_io(pctrl, g);
407                 arg = !!(val & BIT(g->in_bit));
408                 break;
409         case PIN_CONFIG_INPUT_ENABLE:
410                 /* Pin is output */
411                 if (arg)
412                         return -EINVAL;
413                 arg = 1;
414                 break;
415         default:
416                 return -ENOTSUPP;
417         }
418
419         *config = pinconf_to_config_packed(param, arg);
420
421         return 0;
422 }
423
424 static int msm_config_group_set(struct pinctrl_dev *pctldev,
425                                 unsigned group,
426                                 unsigned long *configs,
427                                 unsigned num_configs)
428 {
429         const struct msm_pingroup *g;
430         struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
431         unsigned long flags;
432         unsigned param;
433         unsigned mask;
434         unsigned arg;
435         unsigned bit;
436         int ret;
437         u32 val;
438         int i;
439
440         g = &pctrl->soc->groups[group];
441
442         for (i = 0; i < num_configs; i++) {
443                 param = pinconf_to_config_param(configs[i]);
444                 arg = pinconf_to_config_argument(configs[i]);
445
446                 ret = msm_config_reg(pctrl, g, param, &mask, &bit);
447                 if (ret < 0)
448                         return ret;
449
450                 /* Convert pinconf values to register values */
451                 switch (param) {
452                 case PIN_CONFIG_BIAS_DISABLE:
453                         arg = MSM_NO_PULL;
454                         break;
455                 case PIN_CONFIG_BIAS_PULL_DOWN:
456                         arg = MSM_PULL_DOWN;
457                         break;
458                 case PIN_CONFIG_BIAS_BUS_HOLD:
459                         if (pctrl->soc->pull_no_keeper)
460                                 return -ENOTSUPP;
461
462                         arg = MSM_KEEPER;
463                         break;
464                 case PIN_CONFIG_BIAS_PULL_UP:
465                         if (pctrl->soc->pull_no_keeper)
466                                 arg = MSM_PULL_UP_NO_KEEPER;
467                         else
468                                 arg = MSM_PULL_UP;
469                         break;
470                 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
471                         arg = 1;
472                         break;
473                 case PIN_CONFIG_DRIVE_STRENGTH:
474                         /* Check for invalid values */
475                         if (arg > 16 || arg < 2 || (arg % 2) != 0)
476                                 arg = -1;
477                         else
478                                 arg = (arg / 2) - 1;
479                         break;
480                 case PIN_CONFIG_OUTPUT:
481                         /* set output value */
482                         raw_spin_lock_irqsave(&pctrl->lock, flags);
483                         val = msm_readl_io(pctrl, g);
484                         if (arg)
485                                 val |= BIT(g->out_bit);
486                         else
487                                 val &= ~BIT(g->out_bit);
488                         msm_writel_io(val, pctrl, g);
489                         raw_spin_unlock_irqrestore(&pctrl->lock, flags);
490
491                         /* enable output */
492                         arg = 1;
493                         break;
494                 case PIN_CONFIG_INPUT_ENABLE:
495                         /* disable output */
496                         arg = 0;
497                         break;
498                 default:
499                         dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
500                                 param);
501                         return -EINVAL;
502                 }
503
504                 /* Range-check user-supplied value */
505                 if (arg & ~mask) {
506                         dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
507                         return -EINVAL;
508                 }
509
510                 raw_spin_lock_irqsave(&pctrl->lock, flags);
511                 val = msm_readl_ctl(pctrl, g);
512                 val &= ~(mask << bit);
513                 val |= arg << bit;
514                 msm_writel_ctl(val, pctrl, g);
515                 raw_spin_unlock_irqrestore(&pctrl->lock, flags);
516         }
517
518         return 0;
519 }
520
521 static const struct pinconf_ops msm_pinconf_ops = {
522         .is_generic             = true,
523         .pin_config_group_get   = msm_config_group_get,
524         .pin_config_group_set   = msm_config_group_set,
525 };
526
527 static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
528 {
529         const struct msm_pingroup *g;
530         struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
531         unsigned long flags;
532         u32 val;
533
534         g = &pctrl->soc->groups[offset];
535
536         raw_spin_lock_irqsave(&pctrl->lock, flags);
537
538         val = msm_readl_ctl(pctrl, g);
539         val &= ~BIT(g->oe_bit);
540         msm_writel_ctl(val, pctrl, g);
541
542         raw_spin_unlock_irqrestore(&pctrl->lock, flags);
543
544         return 0;
545 }
546
547 static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
548 {
549         const struct msm_pingroup *g;
550         struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
551         unsigned long flags;
552         u32 val;
553
554         g = &pctrl->soc->groups[offset];
555
556         raw_spin_lock_irqsave(&pctrl->lock, flags);
557
558         val = msm_readl_io(pctrl, g);
559         if (value)
560                 val |= BIT(g->out_bit);
561         else
562                 val &= ~BIT(g->out_bit);
563         msm_writel_io(val, pctrl, g);
564
565         val = msm_readl_ctl(pctrl, g);
566         val |= BIT(g->oe_bit);
567         msm_writel_ctl(val, pctrl, g);
568
569         raw_spin_unlock_irqrestore(&pctrl->lock, flags);
570
571         return 0;
572 }
573
574 static int msm_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
575 {
576         struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
577         const struct msm_pingroup *g;
578         u32 val;
579
580         g = &pctrl->soc->groups[offset];
581
582         val = msm_readl_ctl(pctrl, g);
583
584         return val & BIT(g->oe_bit) ? GPIO_LINE_DIRECTION_OUT :
585                                       GPIO_LINE_DIRECTION_IN;
586 }
587
588 static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
589 {
590         const struct msm_pingroup *g;
591         struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
592         u32 val;
593
594         g = &pctrl->soc->groups[offset];
595
596         val = msm_readl_io(pctrl, g);
597         return !!(val & BIT(g->in_bit));
598 }
599
600 static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
601 {
602         const struct msm_pingroup *g;
603         struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
604         unsigned long flags;
605         u32 val;
606
607         g = &pctrl->soc->groups[offset];
608
609         raw_spin_lock_irqsave(&pctrl->lock, flags);
610
611         val = msm_readl_io(pctrl, g);
612         if (value)
613                 val |= BIT(g->out_bit);
614         else
615                 val &= ~BIT(g->out_bit);
616         msm_writel_io(val, pctrl, g);
617
618         raw_spin_unlock_irqrestore(&pctrl->lock, flags);
619 }
620
621 #ifdef CONFIG_DEBUG_FS
622 #include <linux/seq_file.h>
623
624 static void msm_gpio_dbg_show_one(struct seq_file *s,
625                                   struct pinctrl_dev *pctldev,
626                                   struct gpio_chip *chip,
627                                   unsigned offset,
628                                   unsigned gpio)
629 {
630         const struct msm_pingroup *g;
631         struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
632         unsigned func;
633         int is_out;
634         int drive;
635         int pull;
636         int val;
637         int egpio_enable;
638         u32 ctl_reg, io_reg;
639
640         static const char * const pulls_keeper[] = {
641                 "no pull",
642                 "pull down",
643                 "keeper",
644                 "pull up"
645         };
646
647         static const char * const pulls_no_keeper[] = {
648                 "no pull",
649                 "pull down",
650                 "pull up",
651         };
652
653         if (!gpiochip_line_is_valid(chip, offset))
654                 return;
655
656         g = &pctrl->soc->groups[offset];
657         ctl_reg = msm_readl_ctl(pctrl, g);
658         io_reg = msm_readl_io(pctrl, g);
659
660         is_out = !!(ctl_reg & BIT(g->oe_bit));
661         func = (ctl_reg >> g->mux_bit) & 7;
662         drive = (ctl_reg >> g->drv_bit) & 7;
663         pull = (ctl_reg >> g->pull_bit) & 3;
664         egpio_enable = 0;
665         if (pctrl->soc->egpio_func && ctl_reg & BIT(g->egpio_present))
666                 egpio_enable = !(ctl_reg & BIT(g->egpio_enable));
667
668         if (is_out)
669                 val = !!(io_reg & BIT(g->out_bit));
670         else
671                 val = !!(io_reg & BIT(g->in_bit));
672
673         if (egpio_enable) {
674                 seq_printf(s, " %-8s: egpio\n", g->name);
675                 return;
676         }
677
678         seq_printf(s, " %-8s: %-3s", g->name, is_out ? "out" : "in");
679         seq_printf(s, " %-4s func%d", val ? "high" : "low", func);
680         seq_printf(s, " %dmA", msm_regval_to_drive(drive));
681         if (pctrl->soc->pull_no_keeper)
682                 seq_printf(s, " %s", pulls_no_keeper[pull]);
683         else
684                 seq_printf(s, " %s", pulls_keeper[pull]);
685         seq_puts(s, "\n");
686 }
687
688 static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
689 {
690         unsigned gpio = chip->base;
691         unsigned i;
692
693         for (i = 0; i < chip->ngpio; i++, gpio++)
694                 msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
695 }
696
697 #else
698 #define msm_gpio_dbg_show NULL
699 #endif
700
701 static int msm_gpio_init_valid_mask(struct gpio_chip *gc,
702                                     unsigned long *valid_mask,
703                                     unsigned int ngpios)
704 {
705         struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
706         int ret;
707         unsigned int len, i;
708         const int *reserved = pctrl->soc->reserved_gpios;
709         u16 *tmp;
710
711         /* Driver provided reserved list overrides DT and ACPI */
712         if (reserved) {
713                 bitmap_fill(valid_mask, ngpios);
714                 for (i = 0; reserved[i] >= 0; i++) {
715                         if (i >= ngpios || reserved[i] >= ngpios) {
716                                 dev_err(pctrl->dev, "invalid list of reserved GPIOs\n");
717                                 return -EINVAL;
718                         }
719                         clear_bit(reserved[i], valid_mask);
720                 }
721
722                 return 0;
723         }
724
725         /* The number of GPIOs in the ACPI tables */
726         len = ret = device_property_count_u16(pctrl->dev, "gpios");
727         if (ret < 0)
728                 return 0;
729
730         if (ret > ngpios)
731                 return -EINVAL;
732
733         tmp = kmalloc_array(len, sizeof(*tmp), GFP_KERNEL);
734         if (!tmp)
735                 return -ENOMEM;
736
737         ret = device_property_read_u16_array(pctrl->dev, "gpios", tmp, len);
738         if (ret < 0) {
739                 dev_err(pctrl->dev, "could not read list of GPIOs\n");
740                 goto out;
741         }
742
743         bitmap_zero(valid_mask, ngpios);
744         for (i = 0; i < len; i++)
745                 set_bit(tmp[i], valid_mask);
746
747 out:
748         kfree(tmp);
749         return ret;
750 }
751
752 static const struct gpio_chip msm_gpio_template = {
753         .direction_input  = msm_gpio_direction_input,
754         .direction_output = msm_gpio_direction_output,
755         .get_direction    = msm_gpio_get_direction,
756         .get              = msm_gpio_get,
757         .set              = msm_gpio_set,
758         .request          = gpiochip_generic_request,
759         .free             = gpiochip_generic_free,
760         .dbg_show         = msm_gpio_dbg_show,
761 };
762
763 /* For dual-edge interrupts in software, since some hardware has no
764  * such support:
765  *
766  * At appropriate moments, this function may be called to flip the polarity
767  * settings of both-edge irq lines to try and catch the next edge.
768  *
769  * The attempt is considered successful if:
770  * - the status bit goes high, indicating that an edge was caught, or
771  * - the input value of the gpio doesn't change during the attempt.
772  * If the value changes twice during the process, that would cause the first
773  * test to fail but would force the second, as two opposite
774  * transitions would cause a detection no matter the polarity setting.
775  *
776  * The do-loop tries to sledge-hammer closed the timing hole between
777  * the initial value-read and the polarity-write - if the line value changes
778  * during that window, an interrupt is lost, the new polarity setting is
779  * incorrect, and the first success test will fail, causing a retry.
780  *
781  * Algorithm comes from Google's msmgpio driver.
782  */
783 static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
784                                           const struct msm_pingroup *g,
785                                           struct irq_data *d)
786 {
787         int loop_limit = 100;
788         unsigned val, val2, intstat;
789         unsigned pol;
790
791         do {
792                 val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
793
794                 pol = msm_readl_intr_cfg(pctrl, g);
795                 pol ^= BIT(g->intr_polarity_bit);
796                 msm_writel_intr_cfg(pol, pctrl, g);
797
798                 val2 = msm_readl_io(pctrl, g) & BIT(g->in_bit);
799                 intstat = msm_readl_intr_status(pctrl, g);
800                 if (intstat || (val == val2))
801                         return;
802         } while (loop_limit-- > 0);
803         dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
804                 val, val2);
805 }
806
807 static void msm_gpio_irq_mask(struct irq_data *d)
808 {
809         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
810         struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
811         const struct msm_pingroup *g;
812         unsigned long flags;
813         u32 val;
814
815         if (d->parent_data)
816                 irq_chip_mask_parent(d);
817
818         if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
819                 return;
820
821         g = &pctrl->soc->groups[d->hwirq];
822
823         raw_spin_lock_irqsave(&pctrl->lock, flags);
824
825         val = msm_readl_intr_cfg(pctrl, g);
826         /*
827          * There are two bits that control interrupt forwarding to the CPU. The
828          * RAW_STATUS_EN bit causes the level or edge sensed on the line to be
829          * latched into the interrupt status register when the hardware detects
830          * an irq that it's configured for (either edge for edge type or level
831          * for level type irq). The 'non-raw' status enable bit causes the
832          * hardware to assert the summary interrupt to the CPU if the latched
833          * status bit is set. There's a bug though, the edge detection logic
834          * seems to have a problem where toggling the RAW_STATUS_EN bit may
835          * cause the status bit to latch spuriously when there isn't any edge
836          * so we can't touch that bit for edge type irqs and we have to keep
837          * the bit set anyway so that edges are latched while the line is masked.
838          *
839          * To make matters more complicated, leaving the RAW_STATUS_EN bit
840          * enabled all the time causes level interrupts to re-latch into the
841          * status register because the level is still present on the line after
842          * we ack it. We clear the raw status enable bit during mask here and
843          * set the bit on unmask so the interrupt can't latch into the hardware
844          * while it's masked.
845          */
846         if (irqd_get_trigger_type(d) & IRQ_TYPE_LEVEL_MASK)
847                 val &= ~BIT(g->intr_raw_status_bit);
848
849         val &= ~BIT(g->intr_enable_bit);
850         msm_writel_intr_cfg(val, pctrl, g);
851
852         clear_bit(d->hwirq, pctrl->enabled_irqs);
853
854         raw_spin_unlock_irqrestore(&pctrl->lock, flags);
855 }
856
857 static void msm_gpio_irq_unmask(struct irq_data *d)
858 {
859         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
860         struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
861         const struct msm_pingroup *g;
862         unsigned long flags;
863         u32 val;
864
865         if (d->parent_data)
866                 irq_chip_unmask_parent(d);
867
868         if (test_bit(d->hwirq, pctrl->skip_wake_irqs))
869                 return;
870
871         g = &pctrl->soc->groups[d->hwirq];
872
873         raw_spin_lock_irqsave(&pctrl->lock, flags);
874
875         val = msm_readl_intr_cfg(pctrl, g);
876         val |= BIT(g->intr_raw_status_bit);
877         val |= BIT(g->intr_enable_bit);
878         msm_writel_intr_cfg(val, pctrl, g);
879
880         set_bit(d->hwirq, pctrl->enabled_irqs);
881
882         raw_spin_unlock_irqrestore(&pctrl->lock, flags);
883 }
884
885 static void msm_gpio_irq_enable(struct irq_data *d)
886 {
887         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
888         struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
889
890         gpiochip_enable_irq(gc, d->hwirq);
891
892         if (d->parent_data)
893                 irq_chip_enable_parent(d);
894
895         if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
896                 msm_gpio_irq_unmask(d);
897 }
898
899 static void msm_gpio_irq_disable(struct irq_data *d)
900 {
901         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
902         struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
903
904         if (d->parent_data)
905                 irq_chip_disable_parent(d);
906
907         if (!test_bit(d->hwirq, pctrl->skip_wake_irqs))
908                 msm_gpio_irq_mask(d);
909
910         gpiochip_disable_irq(gc, d->hwirq);
911 }
912
913 /**
914  * msm_gpio_update_dual_edge_parent() - Prime next edge for IRQs handled by parent.
915  * @d: The irq dta.
916  *
917  * This is much like msm_gpio_update_dual_edge_pos() but for IRQs that are
918  * normally handled by the parent irqchip.  The logic here is slightly
919  * different due to what's easy to do with our parent, but in principle it's
920  * the same.
921  */
922 static void msm_gpio_update_dual_edge_parent(struct irq_data *d)
923 {
924         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
925         struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
926         const struct msm_pingroup *g = &pctrl->soc->groups[d->hwirq];
927         int loop_limit = 100;
928         unsigned int val;
929         unsigned int type;
930
931         /* Read the value and make a guess about what edge we need to catch */
932         val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
933         type = val ? IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
934
935         do {
936                 /* Set the parent to catch the next edge */
937                 irq_chip_set_type_parent(d, type);
938
939                 /*
940                  * Possibly the line changed between when we last read "val"
941                  * (and decided what edge we needed) and when set the edge.
942                  * If the value didn't change (or changed and then changed
943                  * back) then we're done.
944                  */
945                 val = msm_readl_io(pctrl, g) & BIT(g->in_bit);
946                 if (type == IRQ_TYPE_EDGE_RISING) {
947                         if (!val)
948                                 return;
949                         type = IRQ_TYPE_EDGE_FALLING;
950                 } else if (type == IRQ_TYPE_EDGE_FALLING) {
951                         if (val)
952                                 return;
953                         type = IRQ_TYPE_EDGE_RISING;
954                 }
955         } while (loop_limit-- > 0);
956         dev_warn_once(pctrl->dev, "dual-edge irq failed to stabilize\n");
957 }
958
959 static void msm_gpio_irq_ack(struct irq_data *d)
960 {
961         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
962         struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
963         const struct msm_pingroup *g;
964         unsigned long flags;
965
966         if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
967                 if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
968                         msm_gpio_update_dual_edge_parent(d);
969                 return;
970         }
971
972         g = &pctrl->soc->groups[d->hwirq];
973
974         raw_spin_lock_irqsave(&pctrl->lock, flags);
975
976         msm_ack_intr_status(pctrl, g);
977
978         if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
979                 msm_gpio_update_dual_edge_pos(pctrl, g, d);
980
981         raw_spin_unlock_irqrestore(&pctrl->lock, flags);
982 }
983
984 static void msm_gpio_irq_eoi(struct irq_data *d)
985 {
986         d = d->parent_data;
987
988         if (d)
989                 d->chip->irq_eoi(d);
990 }
991
992 static bool msm_gpio_needs_dual_edge_parent_workaround(struct irq_data *d,
993                                                        unsigned int type)
994 {
995         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
996         struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
997
998         return type == IRQ_TYPE_EDGE_BOTH &&
999                pctrl->soc->wakeirq_dual_edge_errata && d->parent_data &&
1000                test_bit(d->hwirq, pctrl->skip_wake_irqs);
1001 }
1002
1003 static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
1004 {
1005         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1006         struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1007         const struct msm_pingroup *g;
1008         unsigned long flags;
1009         bool was_enabled;
1010         u32 val;
1011
1012         if (msm_gpio_needs_dual_edge_parent_workaround(d, type)) {
1013                 set_bit(d->hwirq, pctrl->dual_edge_irqs);
1014                 irq_set_handler_locked(d, handle_fasteoi_ack_irq);
1015                 msm_gpio_update_dual_edge_parent(d);
1016                 return 0;
1017         }
1018
1019         if (d->parent_data)
1020                 irq_chip_set_type_parent(d, type);
1021
1022         if (test_bit(d->hwirq, pctrl->skip_wake_irqs)) {
1023                 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
1024                 irq_set_handler_locked(d, handle_fasteoi_irq);
1025                 return 0;
1026         }
1027
1028         g = &pctrl->soc->groups[d->hwirq];
1029
1030         raw_spin_lock_irqsave(&pctrl->lock, flags);
1031
1032         /*
1033          * For hw without possibility of detecting both edges
1034          */
1035         if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
1036                 set_bit(d->hwirq, pctrl->dual_edge_irqs);
1037         else
1038                 clear_bit(d->hwirq, pctrl->dual_edge_irqs);
1039
1040         /* Route interrupts to application cpu.
1041          * With intr_target_use_scm interrupts are routed to
1042          * application cpu using scm calls.
1043          */
1044         if (pctrl->intr_target_use_scm) {
1045                 u32 addr = pctrl->phys_base[0] + g->intr_target_reg;
1046                 int ret;
1047
1048                 qcom_scm_io_readl(addr, &val);
1049
1050                 val &= ~(7 << g->intr_target_bit);
1051                 val |= g->intr_target_kpss_val << g->intr_target_bit;
1052
1053                 ret = qcom_scm_io_writel(addr, val);
1054                 if (ret)
1055                         dev_err(pctrl->dev,
1056                                 "Failed routing %lu interrupt to Apps proc",
1057                                 d->hwirq);
1058         } else {
1059                 val = msm_readl_intr_target(pctrl, g);
1060                 val &= ~(7 << g->intr_target_bit);
1061                 val |= g->intr_target_kpss_val << g->intr_target_bit;
1062                 msm_writel_intr_target(val, pctrl, g);
1063         }
1064
1065         /* Update configuration for gpio.
1066          * RAW_STATUS_EN is left on for all gpio irqs. Due to the
1067          * internal circuitry of TLMM, toggling the RAW_STATUS
1068          * could cause the INTR_STATUS to be set for EDGE interrupts.
1069          */
1070         val = msm_readl_intr_cfg(pctrl, g);
1071         was_enabled = val & BIT(g->intr_raw_status_bit);
1072         val |= BIT(g->intr_raw_status_bit);
1073         if (g->intr_detection_width == 2) {
1074                 val &= ~(3 << g->intr_detection_bit);
1075                 val &= ~(1 << g->intr_polarity_bit);
1076                 switch (type) {
1077                 case IRQ_TYPE_EDGE_RISING:
1078                         val |= 1 << g->intr_detection_bit;
1079                         val |= BIT(g->intr_polarity_bit);
1080                         break;
1081                 case IRQ_TYPE_EDGE_FALLING:
1082                         val |= 2 << g->intr_detection_bit;
1083                         val |= BIT(g->intr_polarity_bit);
1084                         break;
1085                 case IRQ_TYPE_EDGE_BOTH:
1086                         val |= 3 << 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 if (g->intr_detection_width == 1) {
1096                 val &= ~(1 << g->intr_detection_bit);
1097                 val &= ~(1 << g->intr_polarity_bit);
1098                 switch (type) {
1099                 case IRQ_TYPE_EDGE_RISING:
1100                         val |= BIT(g->intr_detection_bit);
1101                         val |= BIT(g->intr_polarity_bit);
1102                         break;
1103                 case IRQ_TYPE_EDGE_FALLING:
1104                         val |= BIT(g->intr_detection_bit);
1105                         break;
1106                 case IRQ_TYPE_EDGE_BOTH:
1107                         val |= BIT(g->intr_detection_bit);
1108                         val |= BIT(g->intr_polarity_bit);
1109                         break;
1110                 case IRQ_TYPE_LEVEL_LOW:
1111                         break;
1112                 case IRQ_TYPE_LEVEL_HIGH:
1113                         val |= BIT(g->intr_polarity_bit);
1114                         break;
1115                 }
1116         } else {
1117                 BUG();
1118         }
1119         msm_writel_intr_cfg(val, pctrl, g);
1120
1121         /*
1122          * The first time we set RAW_STATUS_EN it could trigger an interrupt.
1123          * Clear the interrupt.  This is safe because we have
1124          * IRQCHIP_SET_TYPE_MASKED.
1125          */
1126         if (!was_enabled)
1127                 msm_ack_intr_status(pctrl, g);
1128
1129         if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
1130                 msm_gpio_update_dual_edge_pos(pctrl, g, d);
1131
1132         raw_spin_unlock_irqrestore(&pctrl->lock, flags);
1133
1134         if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
1135                 irq_set_handler_locked(d, handle_level_irq);
1136         else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
1137                 irq_set_handler_locked(d, handle_edge_irq);
1138
1139         return 0;
1140 }
1141
1142 static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
1143 {
1144         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1145         struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1146
1147         /*
1148          * While they may not wake up when the TLMM is powered off,
1149          * some GPIOs would like to wakeup the system from suspend
1150          * when TLMM is powered on. To allow that, enable the GPIO
1151          * summary line to be wakeup capable at GIC.
1152          */
1153         if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1154                 return irq_chip_set_wake_parent(d, on);
1155
1156         return irq_set_irq_wake(pctrl->irq, on);
1157 }
1158
1159 static int msm_gpio_irq_reqres(struct irq_data *d)
1160 {
1161         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1162         struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1163         int ret;
1164
1165         if (!try_module_get(gc->owner))
1166                 return -ENODEV;
1167
1168         ret = msm_pinmux_request_gpio(pctrl->pctrl, NULL, d->hwirq);
1169         if (ret)
1170                 goto out;
1171         msm_gpio_direction_input(gc, d->hwirq);
1172
1173         if (gpiochip_lock_as_irq(gc, d->hwirq)) {
1174                 dev_err(gc->parent,
1175                         "unable to lock HW IRQ %lu for IRQ\n",
1176                         d->hwirq);
1177                 ret = -EINVAL;
1178                 goto out;
1179         }
1180
1181         /*
1182          * The disable / clear-enable workaround we do in msm_pinmux_set_mux()
1183          * only works if disable is not lazy since we only clear any bogus
1184          * interrupt in hardware. Explicitly mark the interrupt as UNLAZY.
1185          */
1186         irq_set_status_flags(d->irq, IRQ_DISABLE_UNLAZY);
1187
1188         return 0;
1189 out:
1190         module_put(gc->owner);
1191         return ret;
1192 }
1193
1194 static void msm_gpio_irq_relres(struct irq_data *d)
1195 {
1196         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1197
1198         gpiochip_unlock_as_irq(gc, d->hwirq);
1199         module_put(gc->owner);
1200 }
1201
1202 static int msm_gpio_irq_set_affinity(struct irq_data *d,
1203                                 const struct cpumask *dest, bool force)
1204 {
1205         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1206         struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1207
1208         if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1209                 return irq_chip_set_affinity_parent(d, dest, force);
1210
1211         return -EINVAL;
1212 }
1213
1214 static int msm_gpio_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1215 {
1216         struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
1217         struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1218
1219         if (d->parent_data && test_bit(d->hwirq, pctrl->skip_wake_irqs))
1220                 return irq_chip_set_vcpu_affinity_parent(d, vcpu_info);
1221
1222         return -EINVAL;
1223 }
1224
1225 static void msm_gpio_irq_handler(struct irq_desc *desc)
1226 {
1227         struct gpio_chip *gc = irq_desc_get_handler_data(desc);
1228         const struct msm_pingroup *g;
1229         struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1230         struct irq_chip *chip = irq_desc_get_chip(desc);
1231         int handled = 0;
1232         u32 val;
1233         int i;
1234
1235         chained_irq_enter(chip, desc);
1236
1237         /*
1238          * Each pin has it's own IRQ status register, so use
1239          * enabled_irq bitmap to limit the number of reads.
1240          */
1241         for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
1242                 g = &pctrl->soc->groups[i];
1243                 val = msm_readl_intr_status(pctrl, g);
1244                 if (val & BIT(g->intr_status_bit)) {
1245                         generic_handle_domain_irq(gc->irq.domain, i);
1246                         handled++;
1247                 }
1248         }
1249
1250         /* No interrupts were flagged */
1251         if (handled == 0)
1252                 handle_bad_irq(desc);
1253
1254         chained_irq_exit(chip, desc);
1255 }
1256
1257 static int msm_gpio_wakeirq(struct gpio_chip *gc,
1258                             unsigned int child,
1259                             unsigned int child_type,
1260                             unsigned int *parent,
1261                             unsigned int *parent_type)
1262 {
1263         struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
1264         const struct msm_gpio_wakeirq_map *map;
1265         int i;
1266
1267         *parent = GPIO_NO_WAKE_IRQ;
1268         *parent_type = IRQ_TYPE_EDGE_RISING;
1269
1270         for (i = 0; i < pctrl->soc->nwakeirq_map; i++) {
1271                 map = &pctrl->soc->wakeirq_map[i];
1272                 if (map->gpio == child) {
1273                         *parent = map->wakeirq;
1274                         break;
1275                 }
1276         }
1277
1278         return 0;
1279 }
1280
1281 static bool msm_gpio_needs_valid_mask(struct msm_pinctrl *pctrl)
1282 {
1283         if (pctrl->soc->reserved_gpios)
1284                 return true;
1285
1286         return device_property_count_u16(pctrl->dev, "gpios") > 0;
1287 }
1288
1289 static const struct irq_chip msm_gpio_irq_chip = {
1290         .name                   = "msmgpio",
1291         .irq_enable             = msm_gpio_irq_enable,
1292         .irq_disable            = msm_gpio_irq_disable,
1293         .irq_mask               = msm_gpio_irq_mask,
1294         .irq_unmask             = msm_gpio_irq_unmask,
1295         .irq_ack                = msm_gpio_irq_ack,
1296         .irq_eoi                = msm_gpio_irq_eoi,
1297         .irq_set_type           = msm_gpio_irq_set_type,
1298         .irq_set_wake           = msm_gpio_irq_set_wake,
1299         .irq_request_resources  = msm_gpio_irq_reqres,
1300         .irq_release_resources  = msm_gpio_irq_relres,
1301         .irq_set_affinity       = msm_gpio_irq_set_affinity,
1302         .irq_set_vcpu_affinity  = msm_gpio_irq_set_vcpu_affinity,
1303         .flags                  = (IRQCHIP_MASK_ON_SUSPEND |
1304                                    IRQCHIP_SET_TYPE_MASKED |
1305                                    IRQCHIP_ENABLE_WAKEUP_ON_SUSPEND |
1306                                    IRQCHIP_IMMUTABLE),
1307 };
1308
1309 static int msm_gpio_init(struct msm_pinctrl *pctrl)
1310 {
1311         struct gpio_chip *chip;
1312         struct gpio_irq_chip *girq;
1313         int i, ret;
1314         unsigned gpio, ngpio = pctrl->soc->ngpios;
1315         struct device_node *np;
1316         bool skip;
1317
1318         if (WARN_ON(ngpio > MAX_NR_GPIO))
1319                 return -EINVAL;
1320
1321         chip = &pctrl->chip;
1322         chip->base = -1;
1323         chip->ngpio = ngpio;
1324         chip->label = dev_name(pctrl->dev);
1325         chip->parent = pctrl->dev;
1326         chip->owner = THIS_MODULE;
1327         if (msm_gpio_needs_valid_mask(pctrl))
1328                 chip->init_valid_mask = msm_gpio_init_valid_mask;
1329
1330         np = of_parse_phandle(pctrl->dev->of_node, "wakeup-parent", 0);
1331         if (np) {
1332                 chip->irq.parent_domain = irq_find_matching_host(np,
1333                                                  DOMAIN_BUS_WAKEUP);
1334                 of_node_put(np);
1335                 if (!chip->irq.parent_domain)
1336                         return -EPROBE_DEFER;
1337                 chip->irq.child_to_parent_hwirq = msm_gpio_wakeirq;
1338                 /*
1339                  * Let's skip handling the GPIOs, if the parent irqchip
1340                  * is handling the direct connect IRQ of the GPIO.
1341                  */
1342                 skip = irq_domain_qcom_handle_wakeup(chip->irq.parent_domain);
1343                 for (i = 0; skip && i < pctrl->soc->nwakeirq_map; i++) {
1344                         gpio = pctrl->soc->wakeirq_map[i].gpio;
1345                         set_bit(gpio, pctrl->skip_wake_irqs);
1346                 }
1347         }
1348
1349         girq = &chip->irq;
1350         gpio_irq_chip_set_chip(girq, &msm_gpio_irq_chip);
1351         girq->parent_handler = msm_gpio_irq_handler;
1352         girq->fwnode = pctrl->dev->fwnode;
1353         girq->num_parents = 1;
1354         girq->parents = devm_kcalloc(pctrl->dev, 1, sizeof(*girq->parents),
1355                                      GFP_KERNEL);
1356         if (!girq->parents)
1357                 return -ENOMEM;
1358         girq->default_type = IRQ_TYPE_NONE;
1359         girq->handler = handle_bad_irq;
1360         girq->parents[0] = pctrl->irq;
1361
1362         ret = gpiochip_add_data(&pctrl->chip, pctrl);
1363         if (ret) {
1364                 dev_err(pctrl->dev, "Failed register gpiochip\n");
1365                 return ret;
1366         }
1367
1368         /*
1369          * For DeviceTree-supported systems, the gpio core checks the
1370          * pinctrl's device node for the "gpio-ranges" property.
1371          * If it is present, it takes care of adding the pin ranges
1372          * for the driver. In this case the driver can skip ahead.
1373          *
1374          * In order to remain compatible with older, existing DeviceTree
1375          * files which don't set the "gpio-ranges" property or systems that
1376          * utilize ACPI the driver has to call gpiochip_add_pin_range().
1377          */
1378         if (!of_property_read_bool(pctrl->dev->of_node, "gpio-ranges")) {
1379                 ret = gpiochip_add_pin_range(&pctrl->chip,
1380                         dev_name(pctrl->dev), 0, 0, chip->ngpio);
1381                 if (ret) {
1382                         dev_err(pctrl->dev, "Failed to add pin range\n");
1383                         gpiochip_remove(&pctrl->chip);
1384                         return ret;
1385                 }
1386         }
1387
1388         return 0;
1389 }
1390
1391 static int msm_ps_hold_restart(struct notifier_block *nb, unsigned long action,
1392                                void *data)
1393 {
1394         struct msm_pinctrl *pctrl = container_of(nb, struct msm_pinctrl, restart_nb);
1395
1396         writel(0, pctrl->regs[0] + PS_HOLD_OFFSET);
1397         mdelay(1000);
1398         return NOTIFY_DONE;
1399 }
1400
1401 static struct msm_pinctrl *poweroff_pctrl;
1402
1403 static void msm_ps_hold_poweroff(void)
1404 {
1405         msm_ps_hold_restart(&poweroff_pctrl->restart_nb, 0, NULL);
1406 }
1407
1408 static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
1409 {
1410         int i;
1411         const struct msm_function *func = pctrl->soc->functions;
1412
1413         for (i = 0; i < pctrl->soc->nfunctions; i++)
1414                 if (!strcmp(func[i].name, "ps_hold")) {
1415                         pctrl->restart_nb.notifier_call = msm_ps_hold_restart;
1416                         pctrl->restart_nb.priority = 128;
1417                         if (register_restart_handler(&pctrl->restart_nb))
1418                                 dev_err(pctrl->dev,
1419                                         "failed to setup restart handler.\n");
1420                         poweroff_pctrl = pctrl;
1421                         pm_power_off = msm_ps_hold_poweroff;
1422                         break;
1423                 }
1424 }
1425
1426 static __maybe_unused int msm_pinctrl_suspend(struct device *dev)
1427 {
1428         struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
1429
1430         return pinctrl_force_sleep(pctrl->pctrl);
1431 }
1432
1433 static __maybe_unused int msm_pinctrl_resume(struct device *dev)
1434 {
1435         struct msm_pinctrl *pctrl = dev_get_drvdata(dev);
1436
1437         return pinctrl_force_default(pctrl->pctrl);
1438 }
1439
1440 SIMPLE_DEV_PM_OPS(msm_pinctrl_dev_pm_ops, msm_pinctrl_suspend,
1441                   msm_pinctrl_resume);
1442
1443 EXPORT_SYMBOL(msm_pinctrl_dev_pm_ops);
1444
1445 int msm_pinctrl_probe(struct platform_device *pdev,
1446                       const struct msm_pinctrl_soc_data *soc_data)
1447 {
1448         struct msm_pinctrl *pctrl;
1449         struct resource *res;
1450         int ret;
1451         int i;
1452
1453         pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
1454         if (!pctrl)
1455                 return -ENOMEM;
1456
1457         pctrl->dev = &pdev->dev;
1458         pctrl->soc = soc_data;
1459         pctrl->chip = msm_gpio_template;
1460         pctrl->intr_target_use_scm = of_device_is_compatible(
1461                                         pctrl->dev->of_node,
1462                                         "qcom,ipq8064-pinctrl");
1463
1464         raw_spin_lock_init(&pctrl->lock);
1465
1466         if (soc_data->tiles) {
1467                 for (i = 0; i < soc_data->ntiles; i++) {
1468                         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1469                                                            soc_data->tiles[i]);
1470                         pctrl->regs[i] = devm_ioremap_resource(&pdev->dev, res);
1471                         if (IS_ERR(pctrl->regs[i]))
1472                                 return PTR_ERR(pctrl->regs[i]);
1473                 }
1474         } else {
1475                 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1476                 pctrl->regs[0] = devm_ioremap_resource(&pdev->dev, res);
1477                 if (IS_ERR(pctrl->regs[0]))
1478                         return PTR_ERR(pctrl->regs[0]);
1479
1480                 pctrl->phys_base[0] = res->start;
1481         }
1482
1483         msm_pinctrl_setup_pm_reset(pctrl);
1484
1485         pctrl->irq = platform_get_irq(pdev, 0);
1486         if (pctrl->irq < 0)
1487                 return pctrl->irq;
1488
1489         pctrl->desc.owner = THIS_MODULE;
1490         pctrl->desc.pctlops = &msm_pinctrl_ops;
1491         pctrl->desc.pmxops = &msm_pinmux_ops;
1492         pctrl->desc.confops = &msm_pinconf_ops;
1493         pctrl->desc.name = dev_name(&pdev->dev);
1494         pctrl->desc.pins = pctrl->soc->pins;
1495         pctrl->desc.npins = pctrl->soc->npins;
1496
1497         pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &pctrl->desc, pctrl);
1498         if (IS_ERR(pctrl->pctrl)) {
1499                 dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
1500                 return PTR_ERR(pctrl->pctrl);
1501         }
1502
1503         ret = msm_gpio_init(pctrl);
1504         if (ret)
1505                 return ret;
1506
1507         platform_set_drvdata(pdev, pctrl);
1508
1509         dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
1510
1511         return 0;
1512 }
1513 EXPORT_SYMBOL(msm_pinctrl_probe);
1514
1515 int msm_pinctrl_remove(struct platform_device *pdev)
1516 {
1517         struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
1518
1519         gpiochip_remove(&pctrl->chip);
1520
1521         unregister_restart_handler(&pctrl->restart_nb);
1522
1523         return 0;
1524 }
1525 EXPORT_SYMBOL(msm_pinctrl_remove);
1526
1527 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. TLMM driver");
1528 MODULE_LICENSE("GPL v2");