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