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