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