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