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