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