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