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