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