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