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