xref: /linux/drivers/pinctrl/mvebu/pinctrl-armada-37xx.c (revision 0c7c237b1c35011ef0b8d30c1d5c20bc6ae7b69b)
1 /*
2  * Marvell 37xx SoC pinctrl driver
3  *
4  * Copyright (C) 2017 Marvell
5  *
6  * Gregory CLEMENT <gregory.clement@free-electrons.com>
7  *
8  * This file is licensed under the terms of the GNU General Public
9  * License version 2 or later. This program is licensed "as is"
10  * without any warranty of any kind, whether express or implied.
11  */
12 
13 #include <linux/gpio/driver.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/of.h>
16 #include <linux/of_address.h>
17 #include <linux/of_device.h>
18 #include <linux/of_irq.h>
19 #include <linux/pinctrl/pinconf-generic.h>
20 #include <linux/pinctrl/pinconf.h>
21 #include <linux/pinctrl/pinctrl.h>
22 #include <linux/pinctrl/pinmux.h>
23 #include <linux/platform_device.h>
24 #include <linux/property.h>
25 #include <linux/regmap.h>
26 #include <linux/seq_file.h>
27 #include <linux/slab.h>
28 #include <linux/string_helpers.h>
29 
30 #include "../pinctrl-utils.h"
31 
32 #define OUTPUT_EN	0x0
33 #define INPUT_VAL	0x10
34 #define OUTPUT_VAL	0x18
35 #define OUTPUT_CTL	0x20
36 #define SELECTION	0x30
37 
38 #define IRQ_EN		0x0
39 #define IRQ_POL		0x08
40 #define IRQ_STATUS	0x10
41 #define IRQ_WKUP	0x18
42 
43 #define NB_FUNCS 3
44 #define GPIO_PER_REG	32
45 
46 /**
47  * struct armada_37xx_pin_group: represents group of pins of a pinmux function.
48  * The pins of a pinmux groups are composed of one or two groups of contiguous
49  * pins.
50  * @name:	Name of the pin group, used to lookup the group.
51  * @start_pin:	Index of the first pin of the main range of pins belonging to
52  *		the group
53  * @npins:	Number of pins included in the first range
54  * @reg_mask:	Bit mask matching the group in the selection register
55  * @val:	Value to write to the registers for a given function
56  * @extra_pin:	Index of the first pin of the optional second range of pins
57  *		belonging to the group
58  * @extra_npins:Number of pins included in the second optional range
59  * @funcs:	A list of pinmux functions that can be selected for this group.
60  * @pins:	List of the pins included in the group
61  */
62 struct armada_37xx_pin_group {
63 	const char	*name;
64 	unsigned int	start_pin;
65 	unsigned int	npins;
66 	u32		reg_mask;
67 	u32		val[NB_FUNCS];
68 	unsigned int	extra_pin;
69 	unsigned int	extra_npins;
70 	const char	*funcs[NB_FUNCS];
71 	unsigned int	*pins;
72 };
73 
74 struct armada_37xx_pin_data {
75 	u8				nr_pins;
76 	char				*name;
77 	struct armada_37xx_pin_group	*groups;
78 	int				ngroups;
79 };
80 
81 struct armada_37xx_pmx_func {
82 	const char		*name;
83 	const char		**groups;
84 	unsigned int		ngroups;
85 };
86 
87 struct armada_37xx_pm_state {
88 	u32 out_en_l;
89 	u32 out_en_h;
90 	u32 out_val_l;
91 	u32 out_val_h;
92 	u32 irq_en_l;
93 	u32 irq_en_h;
94 	u32 irq_pol_l;
95 	u32 irq_pol_h;
96 	u32 selection;
97 };
98 
99 struct armada_37xx_pinctrl {
100 	struct regmap			*regmap;
101 	void __iomem			*base;
102 	const struct armada_37xx_pin_data	*data;
103 	struct device			*dev;
104 	struct gpio_chip		gpio_chip;
105 	raw_spinlock_t			irq_lock;
106 	struct pinctrl_desc		pctl;
107 	struct pinctrl_dev		*pctl_dev;
108 	struct armada_37xx_pin_group	*groups;
109 	unsigned int			ngroups;
110 	struct armada_37xx_pmx_func	*funcs;
111 	unsigned int			nfuncs;
112 	struct armada_37xx_pm_state	pm;
113 };
114 
115 #define PIN_GRP_GPIO_0(_name, _start, _nr)	\
116 	{					\
117 		.name = _name,			\
118 		.start_pin = _start,		\
119 		.npins = _nr,			\
120 		.reg_mask = 0,			\
121 		.val = {0},			\
122 		.funcs = {"gpio"}		\
123 	}
124 
125 #define PIN_GRP_GPIO(_name, _start, _nr, _mask, _func1)	\
126 	{					\
127 		.name = _name,			\
128 		.start_pin = _start,		\
129 		.npins = _nr,			\
130 		.reg_mask = _mask,		\
131 		.val = {0, _mask},		\
132 		.funcs = {_func1, "gpio"}	\
133 	}
134 
135 #define PIN_GRP_GPIO_2(_name, _start, _nr, _mask, _val1, _val2, _func1)   \
136 	{					\
137 		.name = _name,			\
138 		.start_pin = _start,		\
139 		.npins = _nr,			\
140 		.reg_mask = _mask,		\
141 		.val = {_val1, _val2},		\
142 		.funcs = {_func1, "gpio"}	\
143 	}
144 
145 #define PIN_GRP_GPIO_3(_name, _start, _nr, _mask, _v1, _v2, _v3, _f1, _f2) \
146 	{					\
147 		.name = _name,			\
148 		.start_pin = _start,		\
149 		.npins = _nr,			\
150 		.reg_mask = _mask,		\
151 		.val = {_v1, _v2, _v3},	\
152 		.funcs = {_f1, _f2, "gpio"}	\
153 	}
154 
155 #define PIN_GRP_EXTRA(_name, _start, _nr, _mask, _v1, _v2, _start2, _nr2, \
156 		      _f1, _f2)				\
157 	{						\
158 		.name = _name,				\
159 		.start_pin = _start,			\
160 		.npins = _nr,				\
161 		.reg_mask = _mask,			\
162 		.val = {_v1, _v2},			\
163 		.extra_pin = _start2,			\
164 		.extra_npins = _nr2,			\
165 		.funcs = {_f1, _f2}			\
166 	}
167 
168 static struct armada_37xx_pin_group armada_37xx_nb_groups[] = {
169 	PIN_GRP_GPIO("jtag", 20, 5, BIT(0), "jtag"),
170 	PIN_GRP_GPIO("sdio0", 8, 3, BIT(1), "sdio"),
171 	PIN_GRP_GPIO("emmc_nb", 27, 9, BIT(2), "emmc"),
172 	PIN_GRP_GPIO_3("pwm0", 11, 1, BIT(3) | BIT(20), 0, BIT(20), BIT(3),
173 		       "pwm", "led"),
174 	PIN_GRP_GPIO_3("pwm1", 12, 1, BIT(4) | BIT(21), 0, BIT(21), BIT(4),
175 		       "pwm", "led"),
176 	PIN_GRP_GPIO_3("pwm2", 13, 1, BIT(5) | BIT(22), 0, BIT(22), BIT(5),
177 		       "pwm", "led"),
178 	PIN_GRP_GPIO_3("pwm3", 14, 1, BIT(6) | BIT(23), 0, BIT(23), BIT(6),
179 		       "pwm", "led"),
180 	PIN_GRP_GPIO("pmic1", 7, 1, BIT(7), "pmic"),
181 	PIN_GRP_GPIO("pmic0", 6, 1, BIT(8), "pmic"),
182 	PIN_GRP_GPIO_0("gpio1_5", 5, 1),
183 	PIN_GRP_GPIO("i2c2", 2, 2, BIT(9), "i2c"),
184 	PIN_GRP_GPIO("i2c1", 0, 2, BIT(10), "i2c"),
185 	PIN_GRP_GPIO("spi_cs1", 17, 1, BIT(12), "spi"),
186 	PIN_GRP_GPIO_2("spi_cs2", 18, 1, BIT(13) | BIT(19), 0, BIT(13), "spi"),
187 	PIN_GRP_GPIO_2("spi_cs3", 19, 1, BIT(14) | BIT(19), 0, BIT(14), "spi"),
188 	PIN_GRP_GPIO("onewire", 4, 1, BIT(16), "onewire"),
189 	PIN_GRP_GPIO("uart1", 25, 2, BIT(17), "uart"),
190 	PIN_GRP_GPIO("spi_quad", 15, 2, BIT(18), "spi"),
191 	PIN_GRP_EXTRA("uart2", 9, 2, BIT(1) | BIT(13) | BIT(14) | BIT(19),
192 		      BIT(1) | BIT(13) | BIT(14), BIT(1) | BIT(19),
193 		      18, 2, "gpio", "uart"),
194 };
195 
196 static struct armada_37xx_pin_group armada_37xx_sb_groups[] = {
197 	PIN_GRP_GPIO("usb32_drvvbus0", 0, 1, BIT(0), "drvbus"),
198 	PIN_GRP_GPIO("usb2_drvvbus1", 1, 1, BIT(1), "drvbus"),
199 	PIN_GRP_GPIO_0("gpio2_2", 2, 1),
200 	PIN_GRP_GPIO("sdio_sb", 24, 6, BIT(2), "sdio"),
201 	PIN_GRP_GPIO("rgmii", 6, 12, BIT(3), "mii"),
202 	PIN_GRP_GPIO("smi", 18, 2, BIT(4), "smi"),
203 	PIN_GRP_GPIO("pcie1", 3, 1, BIT(5), "pcie"), /* this actually controls "pcie1_reset" */
204 	PIN_GRP_GPIO("pcie1_clkreq", 4, 1, BIT(9), "pcie"),
205 	PIN_GRP_GPIO("pcie1_wakeup", 5, 1, BIT(10), "pcie"),
206 	PIN_GRP_GPIO("ptp", 20, 1, BIT(11), "ptp"),
207 	PIN_GRP_GPIO_3("ptp_clk", 21, 1, BIT(6) | BIT(12), 0, BIT(6), BIT(12),
208 		       "ptp", "mii"),
209 	PIN_GRP_GPIO_3("ptp_trig", 22, 1, BIT(7) | BIT(13), 0, BIT(7), BIT(13),
210 		       "ptp", "mii"),
211 	PIN_GRP_GPIO_3("mii_col", 23, 1, BIT(8) | BIT(14), 0, BIT(8), BIT(14),
212 		       "mii", "mii_err"),
213 };
214 
215 static const struct armada_37xx_pin_data armada_37xx_pin_nb = {
216 	.nr_pins = 36,
217 	.name = "GPIO1",
218 	.groups = armada_37xx_nb_groups,
219 	.ngroups = ARRAY_SIZE(armada_37xx_nb_groups),
220 };
221 
222 static const struct armada_37xx_pin_data armada_37xx_pin_sb = {
223 	.nr_pins = 30,
224 	.name = "GPIO2",
225 	.groups = armada_37xx_sb_groups,
226 	.ngroups = ARRAY_SIZE(armada_37xx_sb_groups),
227 };
228 
229 static inline void armada_37xx_update_reg(unsigned int *reg,
230 					  unsigned int *offset)
231 {
232 	/* We never have more than 2 registers */
233 	if (*offset >= GPIO_PER_REG) {
234 		*offset -= GPIO_PER_REG;
235 		*reg += sizeof(u32);
236 	}
237 }
238 
239 static struct armada_37xx_pin_group *armada_37xx_find_next_grp_by_pin(
240 	struct armada_37xx_pinctrl *info, int pin, int *grp)
241 {
242 	while (*grp < info->ngroups) {
243 		struct armada_37xx_pin_group *group = &info->groups[*grp];
244 		int j;
245 
246 		*grp = *grp + 1;
247 		for (j = 0; j < (group->npins + group->extra_npins); j++)
248 			if (group->pins[j] == pin)
249 				return group;
250 	}
251 	return NULL;
252 }
253 
254 static int armada_37xx_pin_config_group_get(struct pinctrl_dev *pctldev,
255 			    unsigned int selector, unsigned long *config)
256 {
257 	return -ENOTSUPP;
258 }
259 
260 static int armada_37xx_pin_config_group_set(struct pinctrl_dev *pctldev,
261 			    unsigned int selector, unsigned long *configs,
262 			    unsigned int num_configs)
263 {
264 	return -ENOTSUPP;
265 }
266 
267 static const struct pinconf_ops armada_37xx_pinconf_ops = {
268 	.is_generic = true,
269 	.pin_config_group_get = armada_37xx_pin_config_group_get,
270 	.pin_config_group_set = armada_37xx_pin_config_group_set,
271 };
272 
273 static int armada_37xx_get_groups_count(struct pinctrl_dev *pctldev)
274 {
275 	struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
276 
277 	return info->ngroups;
278 }
279 
280 static const char *armada_37xx_get_group_name(struct pinctrl_dev *pctldev,
281 					      unsigned int group)
282 {
283 	struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
284 
285 	return info->groups[group].name;
286 }
287 
288 static int armada_37xx_get_group_pins(struct pinctrl_dev *pctldev,
289 				      unsigned int selector,
290 				      const unsigned int **pins,
291 				      unsigned int *npins)
292 {
293 	struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
294 
295 	if (selector >= info->ngroups)
296 		return -EINVAL;
297 
298 	*pins = info->groups[selector].pins;
299 	*npins = info->groups[selector].npins +
300 		info->groups[selector].extra_npins;
301 
302 	return 0;
303 }
304 
305 static const struct pinctrl_ops armada_37xx_pctrl_ops = {
306 	.get_groups_count	= armada_37xx_get_groups_count,
307 	.get_group_name		= armada_37xx_get_group_name,
308 	.get_group_pins		= armada_37xx_get_group_pins,
309 	.dt_node_to_map		= pinconf_generic_dt_node_to_map_group,
310 	.dt_free_map		= pinctrl_utils_free_map,
311 };
312 
313 /*
314  * Pinmux_ops handling
315  */
316 
317 static int armada_37xx_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
318 {
319 	struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
320 
321 	return info->nfuncs;
322 }
323 
324 static const char *armada_37xx_pmx_get_func_name(struct pinctrl_dev *pctldev,
325 						 unsigned int selector)
326 {
327 	struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
328 
329 	return info->funcs[selector].name;
330 }
331 
332 static int armada_37xx_pmx_get_groups(struct pinctrl_dev *pctldev,
333 				      unsigned int selector,
334 				      const char * const **groups,
335 				      unsigned int * const num_groups)
336 {
337 	struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
338 
339 	*groups = info->funcs[selector].groups;
340 	*num_groups = info->funcs[selector].ngroups;
341 
342 	return 0;
343 }
344 
345 static int armada_37xx_pmx_set_by_name(struct pinctrl_dev *pctldev,
346 				       const char *name,
347 				       struct armada_37xx_pin_group *grp)
348 {
349 	struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
350 	struct device *dev = info->dev;
351 	unsigned int reg = SELECTION;
352 	unsigned int mask = grp->reg_mask;
353 	int func, val;
354 
355 	dev_dbg(dev, "enable function %s group %s\n", name, grp->name);
356 
357 	func = match_string(grp->funcs, NB_FUNCS, name);
358 	if (func < 0)
359 		return -ENOTSUPP;
360 
361 	val = grp->val[func];
362 
363 	regmap_update_bits(info->regmap, reg, mask, val);
364 
365 	return 0;
366 }
367 
368 static int armada_37xx_pmx_set(struct pinctrl_dev *pctldev,
369 			       unsigned int selector,
370 			       unsigned int group)
371 {
372 
373 	struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
374 	struct armada_37xx_pin_group *grp = &info->groups[group];
375 	const char *name = info->funcs[selector].name;
376 
377 	return armada_37xx_pmx_set_by_name(pctldev, name, grp);
378 }
379 
380 static inline void armada_37xx_irq_update_reg(unsigned int *reg,
381 					  struct irq_data *d)
382 {
383 	int offset = irqd_to_hwirq(d);
384 
385 	armada_37xx_update_reg(reg, &offset);
386 }
387 
388 static int armada_37xx_gpio_direction_input(struct gpio_chip *chip,
389 					    unsigned int offset)
390 {
391 	struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
392 	unsigned int reg = OUTPUT_EN;
393 	unsigned int mask;
394 
395 	armada_37xx_update_reg(&reg, &offset);
396 	mask = BIT(offset);
397 
398 	return regmap_update_bits(info->regmap, reg, mask, 0);
399 }
400 
401 static int armada_37xx_gpio_get_direction(struct gpio_chip *chip,
402 					  unsigned int offset)
403 {
404 	struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
405 	unsigned int reg = OUTPUT_EN;
406 	unsigned int val, mask;
407 
408 	armada_37xx_update_reg(&reg, &offset);
409 	mask = BIT(offset);
410 	regmap_read(info->regmap, reg, &val);
411 
412 	if (val & mask)
413 		return GPIO_LINE_DIRECTION_OUT;
414 
415 	return GPIO_LINE_DIRECTION_IN;
416 }
417 
418 static int armada_37xx_gpio_direction_output(struct gpio_chip *chip,
419 					     unsigned int offset, int value)
420 {
421 	struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
422 	unsigned int reg = OUTPUT_EN;
423 	unsigned int mask, val, ret;
424 
425 	armada_37xx_update_reg(&reg, &offset);
426 	mask = BIT(offset);
427 
428 	ret = regmap_update_bits(info->regmap, reg, mask, mask);
429 
430 	if (ret)
431 		return ret;
432 
433 	reg = OUTPUT_VAL;
434 	val = value ? mask : 0;
435 	regmap_update_bits(info->regmap, reg, mask, val);
436 
437 	return 0;
438 }
439 
440 static int armada_37xx_gpio_get(struct gpio_chip *chip, unsigned int offset)
441 {
442 	struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
443 	unsigned int reg = INPUT_VAL;
444 	unsigned int val, mask;
445 
446 	armada_37xx_update_reg(&reg, &offset);
447 	mask = BIT(offset);
448 
449 	regmap_read(info->regmap, reg, &val);
450 
451 	return (val & mask) != 0;
452 }
453 
454 static void armada_37xx_gpio_set(struct gpio_chip *chip, unsigned int offset,
455 				 int value)
456 {
457 	struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
458 	unsigned int reg = OUTPUT_VAL;
459 	unsigned int mask, val;
460 
461 	armada_37xx_update_reg(&reg, &offset);
462 	mask = BIT(offset);
463 	val = value ? mask : 0;
464 
465 	regmap_update_bits(info->regmap, reg, mask, val);
466 }
467 
468 static int armada_37xx_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
469 					      struct pinctrl_gpio_range *range,
470 					      unsigned int offset, bool input)
471 {
472 	struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
473 	struct gpio_chip *chip = range->gc;
474 
475 	dev_dbg(info->dev, "gpio_direction for pin %u as %s-%d to %s\n",
476 		offset, range->name, offset, input ? "input" : "output");
477 
478 	if (input)
479 		armada_37xx_gpio_direction_input(chip, offset);
480 	else
481 		armada_37xx_gpio_direction_output(chip, offset, 0);
482 
483 	return 0;
484 }
485 
486 static int armada_37xx_gpio_request_enable(struct pinctrl_dev *pctldev,
487 					   struct pinctrl_gpio_range *range,
488 					   unsigned int offset)
489 {
490 	struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
491 	struct armada_37xx_pin_group *group;
492 	int grp = 0;
493 	int ret;
494 
495 	dev_dbg(info->dev, "requesting gpio %d\n", offset);
496 
497 	while ((group = armada_37xx_find_next_grp_by_pin(info, offset, &grp))) {
498 		ret = armada_37xx_pmx_set_by_name(pctldev, "gpio", group);
499 		if (ret)
500 			return ret;
501 	}
502 
503 	return 0;
504 }
505 
506 static const struct pinmux_ops armada_37xx_pmx_ops = {
507 	.get_functions_count	= armada_37xx_pmx_get_funcs_count,
508 	.get_function_name	= armada_37xx_pmx_get_func_name,
509 	.get_function_groups	= armada_37xx_pmx_get_groups,
510 	.set_mux		= armada_37xx_pmx_set,
511 	.gpio_request_enable	= armada_37xx_gpio_request_enable,
512 	.gpio_set_direction	= armada_37xx_pmx_gpio_set_direction,
513 };
514 
515 static const struct gpio_chip armada_37xx_gpiolib_chip = {
516 	.request = gpiochip_generic_request,
517 	.free = gpiochip_generic_free,
518 	.set = armada_37xx_gpio_set,
519 	.get = armada_37xx_gpio_get,
520 	.get_direction	= armada_37xx_gpio_get_direction,
521 	.direction_input = armada_37xx_gpio_direction_input,
522 	.direction_output = armada_37xx_gpio_direction_output,
523 	.owner = THIS_MODULE,
524 };
525 
526 static void armada_37xx_irq_ack(struct irq_data *d)
527 {
528 	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
529 	struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
530 	u32 reg = IRQ_STATUS;
531 	unsigned long flags;
532 
533 	armada_37xx_irq_update_reg(&reg, d);
534 	raw_spin_lock_irqsave(&info->irq_lock, flags);
535 	writel(d->mask, info->base + reg);
536 	raw_spin_unlock_irqrestore(&info->irq_lock, flags);
537 }
538 
539 static void armada_37xx_irq_mask(struct irq_data *d)
540 {
541 	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
542 	struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
543 	u32 val, reg = IRQ_EN;
544 	unsigned long flags;
545 
546 	armada_37xx_irq_update_reg(&reg, d);
547 	raw_spin_lock_irqsave(&info->irq_lock, flags);
548 	val = readl(info->base + reg);
549 	writel(val & ~d->mask, info->base + reg);
550 	raw_spin_unlock_irqrestore(&info->irq_lock, flags);
551 	gpiochip_disable_irq(chip, irqd_to_hwirq(d));
552 }
553 
554 static void armada_37xx_irq_unmask(struct irq_data *d)
555 {
556 	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
557 	struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
558 	u32 val, reg = IRQ_EN;
559 	unsigned long flags;
560 
561 	gpiochip_enable_irq(chip, irqd_to_hwirq(d));
562 	armada_37xx_irq_update_reg(&reg, d);
563 	raw_spin_lock_irqsave(&info->irq_lock, flags);
564 	val = readl(info->base + reg);
565 	writel(val | d->mask, info->base + reg);
566 	raw_spin_unlock_irqrestore(&info->irq_lock, flags);
567 }
568 
569 static int armada_37xx_irq_set_wake(struct irq_data *d, unsigned int on)
570 {
571 	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
572 	struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
573 	u32 val, reg = IRQ_WKUP;
574 	unsigned long flags;
575 
576 	armada_37xx_irq_update_reg(&reg, d);
577 	raw_spin_lock_irqsave(&info->irq_lock, flags);
578 	val = readl(info->base + reg);
579 	if (on)
580 		val |= (BIT(d->hwirq % GPIO_PER_REG));
581 	else
582 		val &= ~(BIT(d->hwirq % GPIO_PER_REG));
583 	writel(val, info->base + reg);
584 	raw_spin_unlock_irqrestore(&info->irq_lock, flags);
585 
586 	return 0;
587 }
588 
589 static int armada_37xx_irq_set_type(struct irq_data *d, unsigned int type)
590 {
591 	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
592 	struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
593 	u32 val, reg = IRQ_POL;
594 	unsigned long flags;
595 
596 	raw_spin_lock_irqsave(&info->irq_lock, flags);
597 	armada_37xx_irq_update_reg(&reg, d);
598 	val = readl(info->base + reg);
599 	switch (type) {
600 	case IRQ_TYPE_EDGE_RISING:
601 		val &= ~(BIT(d->hwirq % GPIO_PER_REG));
602 		break;
603 	case IRQ_TYPE_EDGE_FALLING:
604 		val |= (BIT(d->hwirq % GPIO_PER_REG));
605 		break;
606 	case IRQ_TYPE_EDGE_BOTH: {
607 		u32 in_val, in_reg = INPUT_VAL;
608 
609 		armada_37xx_irq_update_reg(&in_reg, d);
610 		regmap_read(info->regmap, in_reg, &in_val);
611 
612 		/* Set initial polarity based on current input level. */
613 		if (in_val & BIT(d->hwirq % GPIO_PER_REG))
614 			val |= BIT(d->hwirq % GPIO_PER_REG);	/* falling */
615 		else
616 			val &= ~(BIT(d->hwirq % GPIO_PER_REG));	/* rising */
617 		break;
618 	}
619 	default:
620 		raw_spin_unlock_irqrestore(&info->irq_lock, flags);
621 		return -EINVAL;
622 	}
623 	writel(val, info->base + reg);
624 	raw_spin_unlock_irqrestore(&info->irq_lock, flags);
625 
626 	return 0;
627 }
628 
629 static int armada_37xx_edge_both_irq_swap_pol(struct armada_37xx_pinctrl *info,
630 					     u32 pin_idx)
631 {
632 	u32 reg_idx = pin_idx / GPIO_PER_REG;
633 	u32 bit_num = pin_idx % GPIO_PER_REG;
634 	u32 p, l, ret;
635 	unsigned long flags;
636 
637 	regmap_read(info->regmap, INPUT_VAL + 4*reg_idx, &l);
638 
639 	raw_spin_lock_irqsave(&info->irq_lock, flags);
640 	p = readl(info->base + IRQ_POL + 4 * reg_idx);
641 	if ((p ^ l) & (1 << bit_num)) {
642 		/*
643 		 * For the gpios which are used for both-edge irqs, when their
644 		 * interrupts happen, their input levels are changed,
645 		 * yet their interrupt polarities are kept in old values, we
646 		 * should synchronize their interrupt polarities; for example,
647 		 * at first a gpio's input level is low and its interrupt
648 		 * polarity control is "Detect rising edge", then the gpio has
649 		 * a interrupt , its level turns to high, we should change its
650 		 * polarity control to "Detect falling edge" correspondingly.
651 		 */
652 		p ^= 1 << bit_num;
653 		writel(p, info->base + IRQ_POL + 4 * reg_idx);
654 		ret = 0;
655 	} else {
656 		/* Spurious irq */
657 		ret = -1;
658 	}
659 
660 	raw_spin_unlock_irqrestore(&info->irq_lock, flags);
661 	return ret;
662 }
663 
664 static void armada_37xx_irq_handler(struct irq_desc *desc)
665 {
666 	struct gpio_chip *gc = irq_desc_get_handler_data(desc);
667 	struct irq_chip *chip = irq_desc_get_chip(desc);
668 	struct armada_37xx_pinctrl *info = gpiochip_get_data(gc);
669 	struct irq_domain *d = gc->irq.domain;
670 	int i;
671 
672 	chained_irq_enter(chip, desc);
673 	for (i = 0; i <= d->revmap_size / GPIO_PER_REG; i++) {
674 		u32 status;
675 		unsigned long flags;
676 
677 		raw_spin_lock_irqsave(&info->irq_lock, flags);
678 		status = readl_relaxed(info->base + IRQ_STATUS + 4 * i);
679 		/* Manage only the interrupt that was enabled */
680 		status &= readl_relaxed(info->base + IRQ_EN + 4 * i);
681 		raw_spin_unlock_irqrestore(&info->irq_lock, flags);
682 		while (status) {
683 			u32 hwirq = ffs(status) - 1;
684 			u32 virq = irq_find_mapping(d, hwirq +
685 						     i * GPIO_PER_REG);
686 			u32 t = irq_get_trigger_type(virq);
687 
688 			if ((t & IRQ_TYPE_SENSE_MASK) == IRQ_TYPE_EDGE_BOTH) {
689 				/* Swap polarity (race with GPIO line) */
690 				if (armada_37xx_edge_both_irq_swap_pol(info,
691 					hwirq + i * GPIO_PER_REG)) {
692 					/*
693 					 * For spurious irq, which gpio level
694 					 * is not as expected after incoming
695 					 * edge, just ack the gpio irq.
696 					 */
697 					writel(1 << hwirq,
698 					       info->base +
699 					       IRQ_STATUS + 4 * i);
700 					goto update_status;
701 				}
702 			}
703 
704 			generic_handle_irq(virq);
705 
706 update_status:
707 			/* Update status in case a new IRQ appears */
708 			raw_spin_lock_irqsave(&info->irq_lock, flags);
709 			status = readl_relaxed(info->base +
710 					       IRQ_STATUS + 4 * i);
711 			/* Manage only the interrupt that was enabled */
712 			status &= readl_relaxed(info->base + IRQ_EN + 4 * i);
713 			raw_spin_unlock_irqrestore(&info->irq_lock, flags);
714 		}
715 	}
716 	chained_irq_exit(chip, desc);
717 }
718 
719 static unsigned int armada_37xx_irq_startup(struct irq_data *d)
720 {
721 	/*
722 	 * The mask field is a "precomputed bitmask for accessing the
723 	 * chip registers" which was introduced for the generic
724 	 * irqchip framework. As we don't use this framework, we can
725 	 * reuse this field for our own usage.
726 	 */
727 	d->mask = BIT(d->hwirq % GPIO_PER_REG);
728 
729 	armada_37xx_irq_unmask(d);
730 
731 	return 0;
732 }
733 
734 static void armada_37xx_irq_print_chip(struct irq_data *d, struct seq_file *p)
735 {
736 	struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
737 	struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
738 
739 	seq_printf(p, info->data->name);
740 }
741 
742 static const struct irq_chip armada_37xx_irqchip = {
743 	.irq_ack = armada_37xx_irq_ack,
744 	.irq_mask = armada_37xx_irq_mask,
745 	.irq_unmask = armada_37xx_irq_unmask,
746 	.irq_set_wake = armada_37xx_irq_set_wake,
747 	.irq_set_type = armada_37xx_irq_set_type,
748 	.irq_startup = armada_37xx_irq_startup,
749 	.irq_print_chip = armada_37xx_irq_print_chip,
750 	.flags = IRQCHIP_IMMUTABLE,
751 	GPIOCHIP_IRQ_RESOURCE_HELPERS,
752 };
753 
754 static int armada_37xx_irqchip_register(struct platform_device *pdev,
755 					struct armada_37xx_pinctrl *info)
756 {
757 	struct gpio_chip *gc = &info->gpio_chip;
758 	struct gpio_irq_chip *girq = &gc->irq;
759 	struct device_node *np = to_of_node(gc->fwnode);
760 	struct device *dev = &pdev->dev;
761 	unsigned int i, nr_irq_parent;
762 
763 	raw_spin_lock_init(&info->irq_lock);
764 
765 	nr_irq_parent = of_irq_count(np);
766 	if (!nr_irq_parent) {
767 		dev_err(dev, "invalid or no IRQ\n");
768 		return 0;
769 	}
770 
771 	info->base = devm_platform_ioremap_resource(pdev, 1);
772 	if (IS_ERR(info->base))
773 		return PTR_ERR(info->base);
774 
775 	gpio_irq_chip_set_chip(girq, &armada_37xx_irqchip);
776 	girq->parent_handler = armada_37xx_irq_handler;
777 	/*
778 	 * Many interrupts are connected to the parent interrupt
779 	 * controller. But we do not take advantage of this and use
780 	 * the chained irq with all of them.
781 	 */
782 	girq->num_parents = nr_irq_parent;
783 	girq->parents = devm_kcalloc(dev, nr_irq_parent, sizeof(*girq->parents), GFP_KERNEL);
784 	if (!girq->parents)
785 		return -ENOMEM;
786 	for (i = 0; i < nr_irq_parent; i++) {
787 		int irq = irq_of_parse_and_map(np, i);
788 
789 		if (!irq)
790 			continue;
791 		girq->parents[i] = irq;
792 	}
793 	girq->default_type = IRQ_TYPE_NONE;
794 	girq->handler = handle_edge_irq;
795 
796 	return 0;
797 }
798 
799 static int armada_37xx_gpiochip_register(struct platform_device *pdev,
800 					struct armada_37xx_pinctrl *info)
801 {
802 	struct device *dev = &pdev->dev;
803 	struct fwnode_handle *fwnode;
804 	struct gpio_chip *gc;
805 	int ret;
806 
807 	fwnode = gpiochip_node_get_first(dev);
808 	if (!fwnode)
809 		return -ENODEV;
810 
811 	info->gpio_chip = armada_37xx_gpiolib_chip;
812 
813 	gc = &info->gpio_chip;
814 	gc->ngpio = info->data->nr_pins;
815 	gc->parent = dev;
816 	gc->base = -1;
817 	gc->fwnode = fwnode;
818 	gc->label = info->data->name;
819 
820 	ret = armada_37xx_irqchip_register(pdev, info);
821 	if (ret)
822 		return ret;
823 
824 	return devm_gpiochip_add_data(dev, gc, info);
825 }
826 
827 /**
828  * armada_37xx_add_function() - Add a new function to the list
829  * @funcs: array of function to add the new one
830  * @funcsize: size of the remaining space for the function
831  * @name: name of the function to add
832  *
833  * If it is a new function then create it by adding its name else
834  * increment the number of group associated to this function.
835  */
836 static int armada_37xx_add_function(struct armada_37xx_pmx_func *funcs,
837 				    int *funcsize, const char *name)
838 {
839 	int i = 0;
840 
841 	if (*funcsize <= 0)
842 		return -EOVERFLOW;
843 
844 	while (funcs->ngroups) {
845 		/* function already there */
846 		if (strcmp(funcs->name, name) == 0) {
847 			funcs->ngroups++;
848 
849 			return -EEXIST;
850 		}
851 		funcs++;
852 		i++;
853 	}
854 
855 	/* append new unique function */
856 	funcs->name = name;
857 	funcs->ngroups = 1;
858 	(*funcsize)--;
859 
860 	return 0;
861 }
862 
863 /**
864  * armada_37xx_fill_group() - complete the group array
865  * @info: info driver instance
866  *
867  * Based on the data available from the armada_37xx_pin_group array
868  * completes the last member of the struct for each function: the list
869  * of the groups associated to this function.
870  *
871  */
872 static int armada_37xx_fill_group(struct armada_37xx_pinctrl *info)
873 {
874 	int n, num = 0, funcsize = info->data->nr_pins;
875 	struct device *dev = info->dev;
876 
877 	for (n = 0; n < info->ngroups; n++) {
878 		struct armada_37xx_pin_group *grp = &info->groups[n];
879 		int i, j, f;
880 
881 		grp->pins = devm_kcalloc(dev, grp->npins + grp->extra_npins,
882 					 sizeof(*grp->pins),
883 					 GFP_KERNEL);
884 		if (!grp->pins)
885 			return -ENOMEM;
886 
887 		for (i = 0; i < grp->npins; i++)
888 			grp->pins[i] = grp->start_pin + i;
889 
890 		for (j = 0; j < grp->extra_npins; j++)
891 			grp->pins[i+j] = grp->extra_pin + j;
892 
893 		for (f = 0; (f < NB_FUNCS) && grp->funcs[f]; f++) {
894 			int ret;
895 			/* check for unique functions and count groups */
896 			ret = armada_37xx_add_function(info->funcs, &funcsize,
897 					    grp->funcs[f]);
898 			if (ret == -EOVERFLOW)
899 				dev_err(dev, "More functions than pins(%d)\n",
900 					info->data->nr_pins);
901 			if (ret < 0)
902 				continue;
903 			num++;
904 		}
905 	}
906 
907 	info->nfuncs = num;
908 
909 	return 0;
910 }
911 
912 /**
913  * armada_37xx_fill_func() - complete the funcs array
914  * @info: info driver instance
915  *
916  * Based on the data available from the armada_37xx_pin_group array
917  * completes the last two member of the struct for each group:
918  * - the list of the pins included in the group
919  * - the list of pinmux functions that can be selected for this group
920  *
921  */
922 static int armada_37xx_fill_func(struct armada_37xx_pinctrl *info)
923 {
924 	struct armada_37xx_pmx_func *funcs = info->funcs;
925 	struct device *dev = info->dev;
926 	int n;
927 
928 	for (n = 0; n < info->nfuncs; n++) {
929 		const char *name = funcs[n].name;
930 		const char **groups;
931 		int g;
932 
933 		funcs[n].groups = devm_kcalloc(dev, funcs[n].ngroups,
934 					       sizeof(*(funcs[n].groups)),
935 					       GFP_KERNEL);
936 		if (!funcs[n].groups)
937 			return -ENOMEM;
938 
939 		groups = funcs[n].groups;
940 
941 		for (g = 0; g < info->ngroups; g++) {
942 			struct armada_37xx_pin_group *gp = &info->groups[g];
943 			int f;
944 
945 			f = match_string(gp->funcs, NB_FUNCS, name);
946 			if (f < 0)
947 				continue;
948 
949 			*groups = gp->name;
950 			groups++;
951 		}
952 	}
953 	return 0;
954 }
955 
956 static int armada_37xx_pinctrl_register(struct platform_device *pdev,
957 					struct armada_37xx_pinctrl *info)
958 {
959 	const struct armada_37xx_pin_data *pin_data = info->data;
960 	struct pinctrl_desc *ctrldesc = &info->pctl;
961 	struct pinctrl_pin_desc *pindesc, *pdesc;
962 	struct device *dev = &pdev->dev;
963 	char **pin_names;
964 	int pin, ret;
965 
966 	info->groups = pin_data->groups;
967 	info->ngroups = pin_data->ngroups;
968 
969 	ctrldesc->name = "armada_37xx-pinctrl";
970 	ctrldesc->owner = THIS_MODULE;
971 	ctrldesc->pctlops = &armada_37xx_pctrl_ops;
972 	ctrldesc->pmxops = &armada_37xx_pmx_ops;
973 	ctrldesc->confops = &armada_37xx_pinconf_ops;
974 
975 	pindesc = devm_kcalloc(dev, pin_data->nr_pins, sizeof(*pindesc), GFP_KERNEL);
976 	if (!pindesc)
977 		return -ENOMEM;
978 
979 	ctrldesc->pins = pindesc;
980 	ctrldesc->npins = pin_data->nr_pins;
981 
982 	pin_names = devm_kasprintf_strarray(dev, pin_data->name, pin_data->nr_pins);
983 	if (IS_ERR(pin_names))
984 		return PTR_ERR(pin_names);
985 
986 	pdesc = pindesc;
987 	for (pin = 0; pin < pin_data->nr_pins; pin++) {
988 		pdesc->number = pin;
989 		pdesc->name = pin_names[pin];
990 		pdesc++;
991 	}
992 
993 	/*
994 	 * we allocate functions for number of pins and hope there are
995 	 * fewer unique functions than pins available
996 	 */
997 	info->funcs = devm_kcalloc(dev, pin_data->nr_pins, sizeof(*info->funcs), GFP_KERNEL);
998 	if (!info->funcs)
999 		return -ENOMEM;
1000 
1001 	ret = armada_37xx_fill_group(info);
1002 	if (ret)
1003 		return ret;
1004 
1005 	ret = armada_37xx_fill_func(info);
1006 	if (ret)
1007 		return ret;
1008 
1009 	info->pctl_dev = devm_pinctrl_register(dev, ctrldesc, info);
1010 	if (IS_ERR(info->pctl_dev))
1011 		return dev_err_probe(dev, PTR_ERR(info->pctl_dev), "could not register pinctrl driver\n");
1012 
1013 	return 0;
1014 }
1015 
1016 #if defined(CONFIG_PM)
1017 static int armada_3700_pinctrl_suspend(struct device *dev)
1018 {
1019 	struct armada_37xx_pinctrl *info = dev_get_drvdata(dev);
1020 
1021 	/* Save GPIO state */
1022 	regmap_read(info->regmap, OUTPUT_EN, &info->pm.out_en_l);
1023 	regmap_read(info->regmap, OUTPUT_EN + sizeof(u32), &info->pm.out_en_h);
1024 	regmap_read(info->regmap, OUTPUT_VAL, &info->pm.out_val_l);
1025 	regmap_read(info->regmap, OUTPUT_VAL + sizeof(u32),
1026 		    &info->pm.out_val_h);
1027 
1028 	info->pm.irq_en_l = readl(info->base + IRQ_EN);
1029 	info->pm.irq_en_h = readl(info->base + IRQ_EN + sizeof(u32));
1030 	info->pm.irq_pol_l = readl(info->base + IRQ_POL);
1031 	info->pm.irq_pol_h = readl(info->base + IRQ_POL + sizeof(u32));
1032 
1033 	/* Save pinctrl state */
1034 	regmap_read(info->regmap, SELECTION, &info->pm.selection);
1035 
1036 	return 0;
1037 }
1038 
1039 static int armada_3700_pinctrl_resume(struct device *dev)
1040 {
1041 	struct armada_37xx_pinctrl *info = dev_get_drvdata(dev);
1042 	struct gpio_chip *gc;
1043 	struct irq_domain *d;
1044 	int i;
1045 
1046 	/* Restore GPIO state */
1047 	regmap_write(info->regmap, OUTPUT_EN, info->pm.out_en_l);
1048 	regmap_write(info->regmap, OUTPUT_EN + sizeof(u32),
1049 		     info->pm.out_en_h);
1050 	regmap_write(info->regmap, OUTPUT_VAL, info->pm.out_val_l);
1051 	regmap_write(info->regmap, OUTPUT_VAL + sizeof(u32),
1052 		     info->pm.out_val_h);
1053 
1054 	/*
1055 	 * Input levels may change during suspend, which is not monitored at
1056 	 * that time. GPIOs used for both-edge IRQs may not be synchronized
1057 	 * anymore with their polarities (rising/falling edge) and must be
1058 	 * re-configured manually.
1059 	 */
1060 	gc = &info->gpio_chip;
1061 	d = gc->irq.domain;
1062 	for (i = 0; i < gc->ngpio; i++) {
1063 		u32 irq_bit = BIT(i % GPIO_PER_REG);
1064 		u32 mask, *irq_pol, input_reg, virq, type, level;
1065 
1066 		if (i < GPIO_PER_REG) {
1067 			mask = info->pm.irq_en_l;
1068 			irq_pol = &info->pm.irq_pol_l;
1069 			input_reg = INPUT_VAL;
1070 		} else {
1071 			mask = info->pm.irq_en_h;
1072 			irq_pol = &info->pm.irq_pol_h;
1073 			input_reg = INPUT_VAL + sizeof(u32);
1074 		}
1075 
1076 		if (!(mask & irq_bit))
1077 			continue;
1078 
1079 		virq = irq_find_mapping(d, i);
1080 		type = irq_get_trigger_type(virq);
1081 
1082 		/*
1083 		 * Synchronize level and polarity for both-edge irqs:
1084 		 *     - a high input level expects a falling edge,
1085 		 *     - a low input level exepects a rising edge.
1086 		 */
1087 		if ((type & IRQ_TYPE_SENSE_MASK) ==
1088 		    IRQ_TYPE_EDGE_BOTH) {
1089 			regmap_read(info->regmap, input_reg, &level);
1090 			if ((*irq_pol ^ level) & irq_bit)
1091 				*irq_pol ^= irq_bit;
1092 		}
1093 	}
1094 
1095 	writel(info->pm.irq_en_l, info->base + IRQ_EN);
1096 	writel(info->pm.irq_en_h, info->base + IRQ_EN + sizeof(u32));
1097 	writel(info->pm.irq_pol_l, info->base + IRQ_POL);
1098 	writel(info->pm.irq_pol_h, info->base + IRQ_POL + sizeof(u32));
1099 
1100 	/* Restore pinctrl state */
1101 	regmap_write(info->regmap, SELECTION, info->pm.selection);
1102 
1103 	return 0;
1104 }
1105 
1106 /*
1107  * Since pinctrl is an infrastructure module, its resume should be issued prior
1108  * to other IO drivers.
1109  */
1110 static const struct dev_pm_ops armada_3700_pinctrl_pm_ops = {
1111 	.suspend_noirq = armada_3700_pinctrl_suspend,
1112 	.resume_noirq = armada_3700_pinctrl_resume,
1113 };
1114 
1115 #define PINCTRL_ARMADA_37XX_DEV_PM_OPS (&armada_3700_pinctrl_pm_ops)
1116 #else
1117 #define PINCTRL_ARMADA_37XX_DEV_PM_OPS NULL
1118 #endif /* CONFIG_PM */
1119 
1120 static const struct of_device_id armada_37xx_pinctrl_of_match[] = {
1121 	{
1122 		.compatible = "marvell,armada3710-sb-pinctrl",
1123 		.data = &armada_37xx_pin_sb,
1124 	},
1125 	{
1126 		.compatible = "marvell,armada3710-nb-pinctrl",
1127 		.data = &armada_37xx_pin_nb,
1128 	},
1129 	{ },
1130 };
1131 
1132 static const struct regmap_config armada_37xx_pinctrl_regmap_config = {
1133 	.reg_bits = 32,
1134 	.val_bits = 32,
1135 	.reg_stride = 4,
1136 	.use_raw_spinlock = true,
1137 };
1138 
1139 static int __init armada_37xx_pinctrl_probe(struct platform_device *pdev)
1140 {
1141 	struct armada_37xx_pinctrl *info;
1142 	struct device *dev = &pdev->dev;
1143 	struct regmap *regmap;
1144 	void __iomem *base;
1145 	int ret;
1146 
1147 	base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
1148 	if (IS_ERR(base)) {
1149 		dev_err(dev, "failed to ioremap base address: %pe\n", base);
1150 		return PTR_ERR(base);
1151 	}
1152 
1153 	regmap = devm_regmap_init_mmio(dev, base,
1154 				       &armada_37xx_pinctrl_regmap_config);
1155 	if (IS_ERR(regmap)) {
1156 		dev_err(dev, "failed to create regmap: %pe\n", regmap);
1157 		return PTR_ERR(regmap);
1158 	}
1159 
1160 	info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1161 	if (!info)
1162 		return -ENOMEM;
1163 
1164 	info->dev = dev;
1165 	info->regmap = regmap;
1166 	info->data = of_device_get_match_data(dev);
1167 
1168 	ret = armada_37xx_pinctrl_register(pdev, info);
1169 	if (ret)
1170 		return ret;
1171 
1172 	ret = armada_37xx_gpiochip_register(pdev, info);
1173 	if (ret)
1174 		return ret;
1175 
1176 	platform_set_drvdata(pdev, info);
1177 
1178 	return 0;
1179 }
1180 
1181 static struct platform_driver armada_37xx_pinctrl_driver = {
1182 	.driver = {
1183 		.name = "armada-37xx-pinctrl",
1184 		.of_match_table = armada_37xx_pinctrl_of_match,
1185 		.pm = PINCTRL_ARMADA_37XX_DEV_PM_OPS,
1186 	},
1187 };
1188 
1189 builtin_platform_driver_probe(armada_37xx_pinctrl_driver,
1190 			      armada_37xx_pinctrl_probe);
1191