1 /*
2  * Copyright (c) 2013, Sony Mobile Communications AB.
3  * Copyright (c) 2013, The Linux Foundation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 and
7  * only version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 
15 #include <linux/delay.h>
16 #include <linux/err.h>
17 #include <linux/io.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/platform_device.h>
21 #include <linux/pinctrl/machine.h>
22 #include <linux/pinctrl/pinctrl.h>
23 #include <linux/pinctrl/pinmux.h>
24 #include <linux/pinctrl/pinconf.h>
25 #include <linux/pinctrl/pinconf-generic.h>
26 #include <linux/slab.h>
27 #include <linux/gpio.h>
28 #include <linux/interrupt.h>
29 #include <linux/spinlock.h>
30 #include <linux/reboot.h>
31 #include <linux/pm.h>
32 #include <linux/log2.h>
33 
34 #include "../core.h"
35 #include "../pinconf.h"
36 #include "pinctrl-msm.h"
37 #include "../pinctrl-utils.h"
38 
39 #define MAX_NR_GPIO 300
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  * @restart_nb:     restart notifier block.
48  * @irq:            parent irq for the TLMM irq_chip.
49  * @lock:           Spinlock to protect register resources as well
50  *                  as msm_pinctrl data structures.
51  * @enabled_irqs:   Bitmap of currently enabled irqs.
52  * @dual_edge_irqs: Bitmap of irqs that need sw emulated dual edge
53  *                  detection.
54  * @soc;            Reference to soc_data of platform specific data.
55  * @regs:           Base address for the TLMM register map.
56  */
57 struct msm_pinctrl {
58 	struct device *dev;
59 	struct pinctrl_dev *pctrl;
60 	struct gpio_chip chip;
61 	struct pinctrl_desc desc;
62 	struct notifier_block restart_nb;
63 
64 	struct irq_chip irq_chip;
65 	int irq;
66 
67 	raw_spinlock_t lock;
68 
69 	DECLARE_BITMAP(dual_edge_irqs, MAX_NR_GPIO);
70 	DECLARE_BITMAP(enabled_irqs, MAX_NR_GPIO);
71 
72 	const struct msm_pinctrl_soc_data *soc;
73 	void __iomem *regs;
74 };
75 
76 static int msm_get_groups_count(struct pinctrl_dev *pctldev)
77 {
78 	struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
79 
80 	return pctrl->soc->ngroups;
81 }
82 
83 static const char *msm_get_group_name(struct pinctrl_dev *pctldev,
84 				      unsigned group)
85 {
86 	struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
87 
88 	return pctrl->soc->groups[group].name;
89 }
90 
91 static int msm_get_group_pins(struct pinctrl_dev *pctldev,
92 			      unsigned group,
93 			      const unsigned **pins,
94 			      unsigned *num_pins)
95 {
96 	struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
97 
98 	*pins = pctrl->soc->groups[group].pins;
99 	*num_pins = pctrl->soc->groups[group].npins;
100 	return 0;
101 }
102 
103 static const struct pinctrl_ops msm_pinctrl_ops = {
104 	.get_groups_count	= msm_get_groups_count,
105 	.get_group_name		= msm_get_group_name,
106 	.get_group_pins		= msm_get_group_pins,
107 	.dt_node_to_map		= pinconf_generic_dt_node_to_map_group,
108 	.dt_free_map		= pinctrl_utils_free_map,
109 };
110 
111 static int msm_pinmux_request(struct pinctrl_dev *pctldev, unsigned offset)
112 {
113 	struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
114 	struct gpio_chip *chip = &pctrl->chip;
115 
116 	return gpiochip_line_is_valid(chip, offset) ? 0 : -EINVAL;
117 }
118 
119 static int msm_get_functions_count(struct pinctrl_dev *pctldev)
120 {
121 	struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
122 
123 	return pctrl->soc->nfunctions;
124 }
125 
126 static const char *msm_get_function_name(struct pinctrl_dev *pctldev,
127 					 unsigned function)
128 {
129 	struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
130 
131 	return pctrl->soc->functions[function].name;
132 }
133 
134 static int msm_get_function_groups(struct pinctrl_dev *pctldev,
135 				   unsigned function,
136 				   const char * const **groups,
137 				   unsigned * const num_groups)
138 {
139 	struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
140 
141 	*groups = pctrl->soc->functions[function].groups;
142 	*num_groups = pctrl->soc->functions[function].ngroups;
143 	return 0;
144 }
145 
146 static int msm_pinmux_set_mux(struct pinctrl_dev *pctldev,
147 			      unsigned function,
148 			      unsigned group)
149 {
150 	struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
151 	const struct msm_pingroup *g;
152 	unsigned long flags;
153 	u32 val, mask;
154 	int i;
155 
156 	g = &pctrl->soc->groups[group];
157 	mask = GENMASK(g->mux_bit + order_base_2(g->nfuncs) - 1, g->mux_bit);
158 
159 	for (i = 0; i < g->nfuncs; i++) {
160 		if (g->funcs[i] == function)
161 			break;
162 	}
163 
164 	if (WARN_ON(i == g->nfuncs))
165 		return -EINVAL;
166 
167 	raw_spin_lock_irqsave(&pctrl->lock, flags);
168 
169 	val = readl(pctrl->regs + g->ctl_reg);
170 	val &= ~mask;
171 	val |= i << g->mux_bit;
172 	writel(val, pctrl->regs + g->ctl_reg);
173 
174 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
175 
176 	return 0;
177 }
178 
179 static const struct pinmux_ops msm_pinmux_ops = {
180 	.request		= msm_pinmux_request,
181 	.get_functions_count	= msm_get_functions_count,
182 	.get_function_name	= msm_get_function_name,
183 	.get_function_groups	= msm_get_function_groups,
184 	.set_mux		= msm_pinmux_set_mux,
185 };
186 
187 static int msm_config_reg(struct msm_pinctrl *pctrl,
188 			  const struct msm_pingroup *g,
189 			  unsigned param,
190 			  unsigned *mask,
191 			  unsigned *bit)
192 {
193 	switch (param) {
194 	case PIN_CONFIG_BIAS_DISABLE:
195 	case PIN_CONFIG_BIAS_PULL_DOWN:
196 	case PIN_CONFIG_BIAS_BUS_HOLD:
197 	case PIN_CONFIG_BIAS_PULL_UP:
198 		*bit = g->pull_bit;
199 		*mask = 3;
200 		break;
201 	case PIN_CONFIG_DRIVE_STRENGTH:
202 		*bit = g->drv_bit;
203 		*mask = 7;
204 		break;
205 	case PIN_CONFIG_OUTPUT:
206 	case PIN_CONFIG_INPUT_ENABLE:
207 		*bit = g->oe_bit;
208 		*mask = 1;
209 		break;
210 	default:
211 		return -ENOTSUPP;
212 	}
213 
214 	return 0;
215 }
216 
217 #define MSM_NO_PULL		0
218 #define MSM_PULL_DOWN		1
219 #define MSM_KEEPER		2
220 #define MSM_PULL_UP_NO_KEEPER	2
221 #define MSM_PULL_UP		3
222 
223 static unsigned msm_regval_to_drive(u32 val)
224 {
225 	return (val + 1) * 2;
226 }
227 
228 static int msm_config_group_get(struct pinctrl_dev *pctldev,
229 				unsigned int group,
230 				unsigned long *config)
231 {
232 	const struct msm_pingroup *g;
233 	struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
234 	unsigned param = pinconf_to_config_param(*config);
235 	unsigned mask;
236 	unsigned arg;
237 	unsigned bit;
238 	int ret;
239 	u32 val;
240 
241 	g = &pctrl->soc->groups[group];
242 
243 	ret = msm_config_reg(pctrl, g, param, &mask, &bit);
244 	if (ret < 0)
245 		return ret;
246 
247 	val = readl(pctrl->regs + g->ctl_reg);
248 	arg = (val >> bit) & mask;
249 
250 	/* Convert register value to pinconf value */
251 	switch (param) {
252 	case PIN_CONFIG_BIAS_DISABLE:
253 		if (arg != MSM_NO_PULL)
254 			return -EINVAL;
255 		arg = 1;
256 		break;
257 	case PIN_CONFIG_BIAS_PULL_DOWN:
258 		if (arg != MSM_PULL_DOWN)
259 			return -EINVAL;
260 		arg = 1;
261 		break;
262 	case PIN_CONFIG_BIAS_BUS_HOLD:
263 		if (pctrl->soc->pull_no_keeper)
264 			return -ENOTSUPP;
265 
266 		if (arg != MSM_KEEPER)
267 			return -EINVAL;
268 		arg = 1;
269 		break;
270 	case PIN_CONFIG_BIAS_PULL_UP:
271 		if (pctrl->soc->pull_no_keeper)
272 			arg = arg == MSM_PULL_UP_NO_KEEPER;
273 		else
274 			arg = arg == MSM_PULL_UP;
275 		if (!arg)
276 			return -EINVAL;
277 		break;
278 	case PIN_CONFIG_DRIVE_STRENGTH:
279 		arg = msm_regval_to_drive(arg);
280 		break;
281 	case PIN_CONFIG_OUTPUT:
282 		/* Pin is not output */
283 		if (!arg)
284 			return -EINVAL;
285 
286 		val = readl(pctrl->regs + g->io_reg);
287 		arg = !!(val & BIT(g->in_bit));
288 		break;
289 	case PIN_CONFIG_INPUT_ENABLE:
290 		/* Pin is output */
291 		if (arg)
292 			return -EINVAL;
293 		arg = 1;
294 		break;
295 	default:
296 		return -ENOTSUPP;
297 	}
298 
299 	*config = pinconf_to_config_packed(param, arg);
300 
301 	return 0;
302 }
303 
304 static int msm_config_group_set(struct pinctrl_dev *pctldev,
305 				unsigned group,
306 				unsigned long *configs,
307 				unsigned num_configs)
308 {
309 	const struct msm_pingroup *g;
310 	struct msm_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
311 	unsigned long flags;
312 	unsigned param;
313 	unsigned mask;
314 	unsigned arg;
315 	unsigned bit;
316 	int ret;
317 	u32 val;
318 	int i;
319 
320 	g = &pctrl->soc->groups[group];
321 
322 	for (i = 0; i < num_configs; i++) {
323 		param = pinconf_to_config_param(configs[i]);
324 		arg = pinconf_to_config_argument(configs[i]);
325 
326 		ret = msm_config_reg(pctrl, g, param, &mask, &bit);
327 		if (ret < 0)
328 			return ret;
329 
330 		/* Convert pinconf values to register values */
331 		switch (param) {
332 		case PIN_CONFIG_BIAS_DISABLE:
333 			arg = MSM_NO_PULL;
334 			break;
335 		case PIN_CONFIG_BIAS_PULL_DOWN:
336 			arg = MSM_PULL_DOWN;
337 			break;
338 		case PIN_CONFIG_BIAS_BUS_HOLD:
339 			if (pctrl->soc->pull_no_keeper)
340 				return -ENOTSUPP;
341 
342 			arg = MSM_KEEPER;
343 			break;
344 		case PIN_CONFIG_BIAS_PULL_UP:
345 			if (pctrl->soc->pull_no_keeper)
346 				arg = MSM_PULL_UP_NO_KEEPER;
347 			else
348 				arg = MSM_PULL_UP;
349 			break;
350 		case PIN_CONFIG_DRIVE_STRENGTH:
351 			/* Check for invalid values */
352 			if (arg > 16 || arg < 2 || (arg % 2) != 0)
353 				arg = -1;
354 			else
355 				arg = (arg / 2) - 1;
356 			break;
357 		case PIN_CONFIG_OUTPUT:
358 			/* set output value */
359 			raw_spin_lock_irqsave(&pctrl->lock, flags);
360 			val = readl(pctrl->regs + g->io_reg);
361 			if (arg)
362 				val |= BIT(g->out_bit);
363 			else
364 				val &= ~BIT(g->out_bit);
365 			writel(val, pctrl->regs + g->io_reg);
366 			raw_spin_unlock_irqrestore(&pctrl->lock, flags);
367 
368 			/* enable output */
369 			arg = 1;
370 			break;
371 		case PIN_CONFIG_INPUT_ENABLE:
372 			/* disable output */
373 			arg = 0;
374 			break;
375 		default:
376 			dev_err(pctrl->dev, "Unsupported config parameter: %x\n",
377 				param);
378 			return -EINVAL;
379 		}
380 
381 		/* Range-check user-supplied value */
382 		if (arg & ~mask) {
383 			dev_err(pctrl->dev, "config %x: %x is invalid\n", param, arg);
384 			return -EINVAL;
385 		}
386 
387 		raw_spin_lock_irqsave(&pctrl->lock, flags);
388 		val = readl(pctrl->regs + g->ctl_reg);
389 		val &= ~(mask << bit);
390 		val |= arg << bit;
391 		writel(val, pctrl->regs + g->ctl_reg);
392 		raw_spin_unlock_irqrestore(&pctrl->lock, flags);
393 	}
394 
395 	return 0;
396 }
397 
398 static const struct pinconf_ops msm_pinconf_ops = {
399 	.is_generic		= true,
400 	.pin_config_group_get	= msm_config_group_get,
401 	.pin_config_group_set	= msm_config_group_set,
402 };
403 
404 static int msm_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
405 {
406 	const struct msm_pingroup *g;
407 	struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
408 	unsigned long flags;
409 	u32 val;
410 
411 	g = &pctrl->soc->groups[offset];
412 
413 	raw_spin_lock_irqsave(&pctrl->lock, flags);
414 
415 	val = readl(pctrl->regs + g->ctl_reg);
416 	val &= ~BIT(g->oe_bit);
417 	writel(val, pctrl->regs + g->ctl_reg);
418 
419 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
420 
421 	return 0;
422 }
423 
424 static int msm_gpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
425 {
426 	const struct msm_pingroup *g;
427 	struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
428 	unsigned long flags;
429 	u32 val;
430 
431 	g = &pctrl->soc->groups[offset];
432 
433 	raw_spin_lock_irqsave(&pctrl->lock, flags);
434 
435 	val = readl(pctrl->regs + g->io_reg);
436 	if (value)
437 		val |= BIT(g->out_bit);
438 	else
439 		val &= ~BIT(g->out_bit);
440 	writel(val, pctrl->regs + g->io_reg);
441 
442 	val = readl(pctrl->regs + g->ctl_reg);
443 	val |= BIT(g->oe_bit);
444 	writel(val, pctrl->regs + g->ctl_reg);
445 
446 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
447 
448 	return 0;
449 }
450 
451 static int msm_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
452 {
453 	struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
454 	const struct msm_pingroup *g;
455 	u32 val;
456 
457 	g = &pctrl->soc->groups[offset];
458 
459 	val = readl(pctrl->regs + g->ctl_reg);
460 
461 	/* 0 = output, 1 = input */
462 	return val & BIT(g->oe_bit) ? 0 : 1;
463 }
464 
465 static int msm_gpio_get(struct gpio_chip *chip, unsigned offset)
466 {
467 	const struct msm_pingroup *g;
468 	struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
469 	u32 val;
470 
471 	g = &pctrl->soc->groups[offset];
472 
473 	val = readl(pctrl->regs + g->io_reg);
474 	return !!(val & BIT(g->in_bit));
475 }
476 
477 static void msm_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
478 {
479 	const struct msm_pingroup *g;
480 	struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
481 	unsigned long flags;
482 	u32 val;
483 
484 	g = &pctrl->soc->groups[offset];
485 
486 	raw_spin_lock_irqsave(&pctrl->lock, flags);
487 
488 	val = readl(pctrl->regs + g->io_reg);
489 	if (value)
490 		val |= BIT(g->out_bit);
491 	else
492 		val &= ~BIT(g->out_bit);
493 	writel(val, pctrl->regs + g->io_reg);
494 
495 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
496 }
497 
498 #ifdef CONFIG_DEBUG_FS
499 #include <linux/seq_file.h>
500 
501 static void msm_gpio_dbg_show_one(struct seq_file *s,
502 				  struct pinctrl_dev *pctldev,
503 				  struct gpio_chip *chip,
504 				  unsigned offset,
505 				  unsigned gpio)
506 {
507 	const struct msm_pingroup *g;
508 	struct msm_pinctrl *pctrl = gpiochip_get_data(chip);
509 	unsigned func;
510 	int is_out;
511 	int drive;
512 	int pull;
513 	int val;
514 	u32 ctl_reg, io_reg;
515 
516 	static const char * const pulls_keeper[] = {
517 		"no pull",
518 		"pull down",
519 		"keeper",
520 		"pull up"
521 	};
522 
523 	static const char * const pulls_no_keeper[] = {
524 		"no pull",
525 		"pull down",
526 		"pull up",
527 	};
528 
529 	if (!gpiochip_line_is_valid(chip, offset))
530 		return;
531 
532 	g = &pctrl->soc->groups[offset];
533 	ctl_reg = readl(pctrl->regs + g->ctl_reg);
534 	io_reg = readl(pctrl->regs + g->io_reg);
535 
536 	is_out = !!(ctl_reg & BIT(g->oe_bit));
537 	func = (ctl_reg >> g->mux_bit) & 7;
538 	drive = (ctl_reg >> g->drv_bit) & 7;
539 	pull = (ctl_reg >> g->pull_bit) & 3;
540 
541 	if (is_out)
542 		val = !!(io_reg & BIT(g->out_bit));
543 	else
544 		val = !!(io_reg & BIT(g->in_bit));
545 
546 	seq_printf(s, " %-8s: %-3s", g->name, is_out ? "out" : "in");
547 	seq_printf(s, " %-4s func%d", val ? "high" : "low", func);
548 	seq_printf(s, " %dmA", msm_regval_to_drive(drive));
549 	if (pctrl->soc->pull_no_keeper)
550 		seq_printf(s, " %s", pulls_no_keeper[pull]);
551 	else
552 		seq_printf(s, " %s", pulls_keeper[pull]);
553 	seq_puts(s, "\n");
554 }
555 
556 static void msm_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
557 {
558 	unsigned gpio = chip->base;
559 	unsigned i;
560 
561 	for (i = 0; i < chip->ngpio; i++, gpio++)
562 		msm_gpio_dbg_show_one(s, NULL, chip, i, gpio);
563 }
564 
565 #else
566 #define msm_gpio_dbg_show NULL
567 #endif
568 
569 static const struct gpio_chip msm_gpio_template = {
570 	.direction_input  = msm_gpio_direction_input,
571 	.direction_output = msm_gpio_direction_output,
572 	.get_direction    = msm_gpio_get_direction,
573 	.get              = msm_gpio_get,
574 	.set              = msm_gpio_set,
575 	.request          = gpiochip_generic_request,
576 	.free             = gpiochip_generic_free,
577 	.dbg_show         = msm_gpio_dbg_show,
578 };
579 
580 /* For dual-edge interrupts in software, since some hardware has no
581  * such support:
582  *
583  * At appropriate moments, this function may be called to flip the polarity
584  * settings of both-edge irq lines to try and catch the next edge.
585  *
586  * The attempt is considered successful if:
587  * - the status bit goes high, indicating that an edge was caught, or
588  * - the input value of the gpio doesn't change during the attempt.
589  * If the value changes twice during the process, that would cause the first
590  * test to fail but would force the second, as two opposite
591  * transitions would cause a detection no matter the polarity setting.
592  *
593  * The do-loop tries to sledge-hammer closed the timing hole between
594  * the initial value-read and the polarity-write - if the line value changes
595  * during that window, an interrupt is lost, the new polarity setting is
596  * incorrect, and the first success test will fail, causing a retry.
597  *
598  * Algorithm comes from Google's msmgpio driver.
599  */
600 static void msm_gpio_update_dual_edge_pos(struct msm_pinctrl *pctrl,
601 					  const struct msm_pingroup *g,
602 					  struct irq_data *d)
603 {
604 	int loop_limit = 100;
605 	unsigned val, val2, intstat;
606 	unsigned pol;
607 
608 	do {
609 		val = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit);
610 
611 		pol = readl(pctrl->regs + g->intr_cfg_reg);
612 		pol ^= BIT(g->intr_polarity_bit);
613 		writel(pol, pctrl->regs + g->intr_cfg_reg);
614 
615 		val2 = readl(pctrl->regs + g->io_reg) & BIT(g->in_bit);
616 		intstat = readl(pctrl->regs + g->intr_status_reg);
617 		if (intstat || (val == val2))
618 			return;
619 	} while (loop_limit-- > 0);
620 	dev_err(pctrl->dev, "dual-edge irq failed to stabilize, %#08x != %#08x\n",
621 		val, val2);
622 }
623 
624 static void msm_gpio_irq_mask(struct irq_data *d)
625 {
626 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
627 	struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
628 	const struct msm_pingroup *g;
629 	unsigned long flags;
630 	u32 val;
631 
632 	g = &pctrl->soc->groups[d->hwirq];
633 
634 	raw_spin_lock_irqsave(&pctrl->lock, flags);
635 
636 	val = readl(pctrl->regs + g->intr_cfg_reg);
637 	val &= ~BIT(g->intr_enable_bit);
638 	writel(val, pctrl->regs + g->intr_cfg_reg);
639 
640 	clear_bit(d->hwirq, pctrl->enabled_irqs);
641 
642 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
643 }
644 
645 static void msm_gpio_irq_unmask(struct irq_data *d)
646 {
647 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
648 	struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
649 	const struct msm_pingroup *g;
650 	unsigned long flags;
651 	u32 val;
652 
653 	g = &pctrl->soc->groups[d->hwirq];
654 
655 	raw_spin_lock_irqsave(&pctrl->lock, flags);
656 
657 	val = readl(pctrl->regs + g->intr_cfg_reg);
658 	val |= BIT(g->intr_enable_bit);
659 	writel(val, pctrl->regs + g->intr_cfg_reg);
660 
661 	set_bit(d->hwirq, pctrl->enabled_irqs);
662 
663 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
664 }
665 
666 static void msm_gpio_irq_ack(struct irq_data *d)
667 {
668 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
669 	struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
670 	const struct msm_pingroup *g;
671 	unsigned long flags;
672 	u32 val;
673 
674 	g = &pctrl->soc->groups[d->hwirq];
675 
676 	raw_spin_lock_irqsave(&pctrl->lock, flags);
677 
678 	val = readl(pctrl->regs + g->intr_status_reg);
679 	if (g->intr_ack_high)
680 		val |= BIT(g->intr_status_bit);
681 	else
682 		val &= ~BIT(g->intr_status_bit);
683 	writel(val, pctrl->regs + g->intr_status_reg);
684 
685 	if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
686 		msm_gpio_update_dual_edge_pos(pctrl, g, d);
687 
688 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
689 }
690 
691 static int msm_gpio_irq_set_type(struct irq_data *d, unsigned int type)
692 {
693 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
694 	struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
695 	const struct msm_pingroup *g;
696 	unsigned long flags;
697 	u32 val;
698 
699 	g = &pctrl->soc->groups[d->hwirq];
700 
701 	raw_spin_lock_irqsave(&pctrl->lock, flags);
702 
703 	/*
704 	 * For hw without possibility of detecting both edges
705 	 */
706 	if (g->intr_detection_width == 1 && type == IRQ_TYPE_EDGE_BOTH)
707 		set_bit(d->hwirq, pctrl->dual_edge_irqs);
708 	else
709 		clear_bit(d->hwirq, pctrl->dual_edge_irqs);
710 
711 	/* Route interrupts to application cpu */
712 	val = readl(pctrl->regs + g->intr_target_reg);
713 	val &= ~(7 << g->intr_target_bit);
714 	val |= g->intr_target_kpss_val << g->intr_target_bit;
715 	writel(val, pctrl->regs + g->intr_target_reg);
716 
717 	/* Update configuration for gpio.
718 	 * RAW_STATUS_EN is left on for all gpio irqs. Due to the
719 	 * internal circuitry of TLMM, toggling the RAW_STATUS
720 	 * could cause the INTR_STATUS to be set for EDGE interrupts.
721 	 */
722 	val = readl(pctrl->regs + g->intr_cfg_reg);
723 	val |= BIT(g->intr_raw_status_bit);
724 	if (g->intr_detection_width == 2) {
725 		val &= ~(3 << g->intr_detection_bit);
726 		val &= ~(1 << g->intr_polarity_bit);
727 		switch (type) {
728 		case IRQ_TYPE_EDGE_RISING:
729 			val |= 1 << g->intr_detection_bit;
730 			val |= BIT(g->intr_polarity_bit);
731 			break;
732 		case IRQ_TYPE_EDGE_FALLING:
733 			val |= 2 << g->intr_detection_bit;
734 			val |= BIT(g->intr_polarity_bit);
735 			break;
736 		case IRQ_TYPE_EDGE_BOTH:
737 			val |= 3 << g->intr_detection_bit;
738 			val |= BIT(g->intr_polarity_bit);
739 			break;
740 		case IRQ_TYPE_LEVEL_LOW:
741 			break;
742 		case IRQ_TYPE_LEVEL_HIGH:
743 			val |= BIT(g->intr_polarity_bit);
744 			break;
745 		}
746 	} else if (g->intr_detection_width == 1) {
747 		val &= ~(1 << g->intr_detection_bit);
748 		val &= ~(1 << g->intr_polarity_bit);
749 		switch (type) {
750 		case IRQ_TYPE_EDGE_RISING:
751 			val |= BIT(g->intr_detection_bit);
752 			val |= BIT(g->intr_polarity_bit);
753 			break;
754 		case IRQ_TYPE_EDGE_FALLING:
755 			val |= BIT(g->intr_detection_bit);
756 			break;
757 		case IRQ_TYPE_EDGE_BOTH:
758 			val |= BIT(g->intr_detection_bit);
759 			val |= BIT(g->intr_polarity_bit);
760 			break;
761 		case IRQ_TYPE_LEVEL_LOW:
762 			break;
763 		case IRQ_TYPE_LEVEL_HIGH:
764 			val |= BIT(g->intr_polarity_bit);
765 			break;
766 		}
767 	} else {
768 		BUG();
769 	}
770 	writel(val, pctrl->regs + g->intr_cfg_reg);
771 
772 	if (test_bit(d->hwirq, pctrl->dual_edge_irqs))
773 		msm_gpio_update_dual_edge_pos(pctrl, g, d);
774 
775 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
776 
777 	if (type & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
778 		irq_set_handler_locked(d, handle_level_irq);
779 	else if (type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_EDGE_RISING))
780 		irq_set_handler_locked(d, handle_edge_irq);
781 
782 	return 0;
783 }
784 
785 static int msm_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
786 {
787 	struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
788 	struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
789 	unsigned long flags;
790 
791 	raw_spin_lock_irqsave(&pctrl->lock, flags);
792 
793 	irq_set_irq_wake(pctrl->irq, on);
794 
795 	raw_spin_unlock_irqrestore(&pctrl->lock, flags);
796 
797 	return 0;
798 }
799 
800 static void msm_gpio_irq_handler(struct irq_desc *desc)
801 {
802 	struct gpio_chip *gc = irq_desc_get_handler_data(desc);
803 	const struct msm_pingroup *g;
804 	struct msm_pinctrl *pctrl = gpiochip_get_data(gc);
805 	struct irq_chip *chip = irq_desc_get_chip(desc);
806 	int irq_pin;
807 	int handled = 0;
808 	u32 val;
809 	int i;
810 
811 	chained_irq_enter(chip, desc);
812 
813 	/*
814 	 * Each pin has it's own IRQ status register, so use
815 	 * enabled_irq bitmap to limit the number of reads.
816 	 */
817 	for_each_set_bit(i, pctrl->enabled_irqs, pctrl->chip.ngpio) {
818 		g = &pctrl->soc->groups[i];
819 		val = readl(pctrl->regs + g->intr_status_reg);
820 		if (val & BIT(g->intr_status_bit)) {
821 			irq_pin = irq_find_mapping(gc->irq.domain, i);
822 			generic_handle_irq(irq_pin);
823 			handled++;
824 		}
825 	}
826 
827 	/* No interrupts were flagged */
828 	if (handled == 0)
829 		handle_bad_irq(desc);
830 
831 	chained_irq_exit(chip, desc);
832 }
833 
834 static int msm_gpio_init_valid_mask(struct gpio_chip *chip,
835 				    struct msm_pinctrl *pctrl)
836 {
837 	int ret;
838 	unsigned int len, i;
839 	unsigned int max_gpios = pctrl->soc->ngpios;
840 	u16 *tmp;
841 
842 	/* The number of GPIOs in the ACPI tables */
843 	len = ret = device_property_read_u16_array(pctrl->dev, "gpios", NULL, 0);
844 	if (ret < 0)
845 		return 0;
846 
847 	if (ret > max_gpios)
848 		return -EINVAL;
849 
850 	tmp = kmalloc_array(len, sizeof(*tmp), GFP_KERNEL);
851 	if (!tmp)
852 		return -ENOMEM;
853 
854 	ret = device_property_read_u16_array(pctrl->dev, "gpios", tmp, len);
855 	if (ret < 0) {
856 		dev_err(pctrl->dev, "could not read list of GPIOs\n");
857 		goto out;
858 	}
859 
860 	bitmap_zero(chip->valid_mask, max_gpios);
861 	for (i = 0; i < len; i++)
862 		set_bit(tmp[i], chip->valid_mask);
863 
864 out:
865 	kfree(tmp);
866 	return ret;
867 }
868 
869 static bool msm_gpio_needs_valid_mask(struct msm_pinctrl *pctrl)
870 {
871 	return device_property_read_u16_array(pctrl->dev, "gpios", NULL, 0) > 0;
872 }
873 
874 static int msm_gpio_init(struct msm_pinctrl *pctrl)
875 {
876 	struct gpio_chip *chip;
877 	int ret;
878 	unsigned ngpio = pctrl->soc->ngpios;
879 
880 	if (WARN_ON(ngpio > MAX_NR_GPIO))
881 		return -EINVAL;
882 
883 	chip = &pctrl->chip;
884 	chip->base = -1;
885 	chip->ngpio = ngpio;
886 	chip->label = dev_name(pctrl->dev);
887 	chip->parent = pctrl->dev;
888 	chip->owner = THIS_MODULE;
889 	chip->of_node = pctrl->dev->of_node;
890 	chip->need_valid_mask = msm_gpio_needs_valid_mask(pctrl);
891 
892 	pctrl->irq_chip.name = "msmgpio";
893 	pctrl->irq_chip.irq_mask = msm_gpio_irq_mask;
894 	pctrl->irq_chip.irq_unmask = msm_gpio_irq_unmask;
895 	pctrl->irq_chip.irq_ack = msm_gpio_irq_ack;
896 	pctrl->irq_chip.irq_set_type = msm_gpio_irq_set_type;
897 	pctrl->irq_chip.irq_set_wake = msm_gpio_irq_set_wake;
898 
899 	ret = gpiochip_add_data(&pctrl->chip, pctrl);
900 	if (ret) {
901 		dev_err(pctrl->dev, "Failed register gpiochip\n");
902 		return ret;
903 	}
904 
905 	ret = msm_gpio_init_valid_mask(chip, pctrl);
906 	if (ret) {
907 		dev_err(pctrl->dev, "Failed to setup irq valid bits\n");
908 		gpiochip_remove(&pctrl->chip);
909 		return ret;
910 	}
911 
912 	/*
913 	 * For DeviceTree-supported systems, the gpio core checks the
914 	 * pinctrl's device node for the "gpio-ranges" property.
915 	 * If it is present, it takes care of adding the pin ranges
916 	 * for the driver. In this case the driver can skip ahead.
917 	 *
918 	 * In order to remain compatible with older, existing DeviceTree
919 	 * files which don't set the "gpio-ranges" property or systems that
920 	 * utilize ACPI the driver has to call gpiochip_add_pin_range().
921 	 */
922 	if (!of_property_read_bool(pctrl->dev->of_node, "gpio-ranges")) {
923 		ret = gpiochip_add_pin_range(&pctrl->chip,
924 			dev_name(pctrl->dev), 0, 0, chip->ngpio);
925 		if (ret) {
926 			dev_err(pctrl->dev, "Failed to add pin range\n");
927 			gpiochip_remove(&pctrl->chip);
928 			return ret;
929 		}
930 	}
931 
932 	ret = gpiochip_irqchip_add(chip,
933 				   &pctrl->irq_chip,
934 				   0,
935 				   handle_edge_irq,
936 				   IRQ_TYPE_NONE);
937 	if (ret) {
938 		dev_err(pctrl->dev, "Failed to add irqchip to gpiochip\n");
939 		gpiochip_remove(&pctrl->chip);
940 		return -ENOSYS;
941 	}
942 
943 	gpiochip_set_chained_irqchip(chip, &pctrl->irq_chip, pctrl->irq,
944 				     msm_gpio_irq_handler);
945 
946 	return 0;
947 }
948 
949 static int msm_ps_hold_restart(struct notifier_block *nb, unsigned long action,
950 			       void *data)
951 {
952 	struct msm_pinctrl *pctrl = container_of(nb, struct msm_pinctrl, restart_nb);
953 
954 	writel(0, pctrl->regs + PS_HOLD_OFFSET);
955 	mdelay(1000);
956 	return NOTIFY_DONE;
957 }
958 
959 static struct msm_pinctrl *poweroff_pctrl;
960 
961 static void msm_ps_hold_poweroff(void)
962 {
963 	msm_ps_hold_restart(&poweroff_pctrl->restart_nb, 0, NULL);
964 }
965 
966 static void msm_pinctrl_setup_pm_reset(struct msm_pinctrl *pctrl)
967 {
968 	int i;
969 	const struct msm_function *func = pctrl->soc->functions;
970 
971 	for (i = 0; i < pctrl->soc->nfunctions; i++)
972 		if (!strcmp(func[i].name, "ps_hold")) {
973 			pctrl->restart_nb.notifier_call = msm_ps_hold_restart;
974 			pctrl->restart_nb.priority = 128;
975 			if (register_restart_handler(&pctrl->restart_nb))
976 				dev_err(pctrl->dev,
977 					"failed to setup restart handler.\n");
978 			poweroff_pctrl = pctrl;
979 			pm_power_off = msm_ps_hold_poweroff;
980 			break;
981 		}
982 }
983 
984 int msm_pinctrl_probe(struct platform_device *pdev,
985 		      const struct msm_pinctrl_soc_data *soc_data)
986 {
987 	struct msm_pinctrl *pctrl;
988 	struct resource *res;
989 	int ret;
990 
991 	pctrl = devm_kzalloc(&pdev->dev, sizeof(*pctrl), GFP_KERNEL);
992 	if (!pctrl)
993 		return -ENOMEM;
994 
995 	pctrl->dev = &pdev->dev;
996 	pctrl->soc = soc_data;
997 	pctrl->chip = msm_gpio_template;
998 
999 	raw_spin_lock_init(&pctrl->lock);
1000 
1001 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1002 	pctrl->regs = devm_ioremap_resource(&pdev->dev, res);
1003 	if (IS_ERR(pctrl->regs))
1004 		return PTR_ERR(pctrl->regs);
1005 
1006 	msm_pinctrl_setup_pm_reset(pctrl);
1007 
1008 	pctrl->irq = platform_get_irq(pdev, 0);
1009 	if (pctrl->irq < 0) {
1010 		dev_err(&pdev->dev, "No interrupt defined for msmgpio\n");
1011 		return pctrl->irq;
1012 	}
1013 
1014 	pctrl->desc.owner = THIS_MODULE;
1015 	pctrl->desc.pctlops = &msm_pinctrl_ops;
1016 	pctrl->desc.pmxops = &msm_pinmux_ops;
1017 	pctrl->desc.confops = &msm_pinconf_ops;
1018 	pctrl->desc.name = dev_name(&pdev->dev);
1019 	pctrl->desc.pins = pctrl->soc->pins;
1020 	pctrl->desc.npins = pctrl->soc->npins;
1021 
1022 	pctrl->pctrl = devm_pinctrl_register(&pdev->dev, &pctrl->desc, pctrl);
1023 	if (IS_ERR(pctrl->pctrl)) {
1024 		dev_err(&pdev->dev, "Couldn't register pinctrl driver\n");
1025 		return PTR_ERR(pctrl->pctrl);
1026 	}
1027 
1028 	ret = msm_gpio_init(pctrl);
1029 	if (ret)
1030 		return ret;
1031 
1032 	platform_set_drvdata(pdev, pctrl);
1033 
1034 	dev_dbg(&pdev->dev, "Probed Qualcomm pinctrl driver\n");
1035 
1036 	return 0;
1037 }
1038 EXPORT_SYMBOL(msm_pinctrl_probe);
1039 
1040 int msm_pinctrl_remove(struct platform_device *pdev)
1041 {
1042 	struct msm_pinctrl *pctrl = platform_get_drvdata(pdev);
1043 
1044 	gpiochip_remove(&pctrl->chip);
1045 
1046 	unregister_restart_handler(&pctrl->restart_nb);
1047 
1048 	return 0;
1049 }
1050 EXPORT_SYMBOL(msm_pinctrl_remove);
1051 
1052