xref: /linux/drivers/pinctrl/pinctrl-microchip-sgpio.c (revision 0a94608f0f7de9b1135ffea3546afe68eafef57f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Microsemi/Microchip SoCs serial gpio driver
4  *
5  * Author: Lars Povlsen <lars.povlsen@microchip.com>
6  *
7  * Copyright (c) 2020 Microchip Technology Inc. and its subsidiaries.
8  */
9 
10 #include <linux/bitfield.h>
11 #include <linux/bits.h>
12 #include <linux/clk.h>
13 #include <linux/gpio/driver.h>
14 #include <linux/io.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/pinctrl/pinmux.h>
18 #include <linux/platform_device.h>
19 #include <linux/property.h>
20 #include <linux/regmap.h>
21 #include <linux/reset.h>
22 #include <linux/spinlock.h>
23 
24 #include "core.h"
25 #include "pinconf.h"
26 
27 #define SGPIO_BITS_PER_WORD	32
28 #define SGPIO_MAX_BITS		4
29 #define SGPIO_SRC_BITS		3 /* 3 bit wide field per pin */
30 
31 enum {
32 	REG_INPUT_DATA,
33 	REG_PORT_CONFIG,
34 	REG_PORT_ENABLE,
35 	REG_SIO_CONFIG,
36 	REG_SIO_CLOCK,
37 	REG_INT_POLARITY,
38 	REG_INT_TRIGGER,
39 	REG_INT_ACK,
40 	REG_INT_ENABLE,
41 	REG_INT_IDENT,
42 	MAXREG
43 };
44 
45 enum {
46 	SGPIO_ARCH_LUTON,
47 	SGPIO_ARCH_OCELOT,
48 	SGPIO_ARCH_SPARX5,
49 };
50 
51 enum {
52 	SGPIO_FLAGS_HAS_IRQ	= BIT(0),
53 };
54 
55 struct sgpio_properties {
56 	int arch;
57 	int flags;
58 	u8 regoff[MAXREG];
59 };
60 
61 #define SGPIO_LUTON_AUTO_REPEAT  BIT(5)
62 #define SGPIO_LUTON_PORT_WIDTH   GENMASK(3, 2)
63 #define SGPIO_LUTON_CLK_FREQ     GENMASK(11, 0)
64 #define SGPIO_LUTON_BIT_SOURCE   GENMASK(11, 0)
65 
66 #define SGPIO_OCELOT_AUTO_REPEAT BIT(10)
67 #define SGPIO_OCELOT_SINGLE_SHOT BIT(11)
68 #define SGPIO_OCELOT_PORT_WIDTH  GENMASK(8, 7)
69 #define SGPIO_OCELOT_CLK_FREQ    GENMASK(19, 8)
70 #define SGPIO_OCELOT_BIT_SOURCE  GENMASK(23, 12)
71 
72 #define SGPIO_SPARX5_AUTO_REPEAT BIT(6)
73 #define SGPIO_SPARX5_SINGLE_SHOT BIT(7)
74 #define SGPIO_SPARX5_PORT_WIDTH  GENMASK(4, 3)
75 #define SGPIO_SPARX5_CLK_FREQ    GENMASK(19, 8)
76 #define SGPIO_SPARX5_BIT_SOURCE  GENMASK(23, 12)
77 
78 #define SGPIO_MASTER_INTR_ENA    BIT(0)
79 
80 #define SGPIO_INT_TRG_LEVEL	0
81 #define SGPIO_INT_TRG_EDGE	1
82 #define SGPIO_INT_TRG_EDGE_FALL	2
83 #define SGPIO_INT_TRG_EDGE_RISE	3
84 
85 #define SGPIO_TRG_LEVEL_HIGH	0
86 #define SGPIO_TRG_LEVEL_LOW	1
87 
88 static const struct sgpio_properties properties_luton = {
89 	.arch   = SGPIO_ARCH_LUTON,
90 	.regoff = { 0x00, 0x09, 0x29, 0x2a, 0x2b },
91 };
92 
93 static const struct sgpio_properties properties_ocelot = {
94 	.arch   = SGPIO_ARCH_OCELOT,
95 	.regoff = { 0x00, 0x06, 0x26, 0x04, 0x05 },
96 };
97 
98 static const struct sgpio_properties properties_sparx5 = {
99 	.arch   = SGPIO_ARCH_SPARX5,
100 	.flags  = SGPIO_FLAGS_HAS_IRQ,
101 	.regoff = { 0x00, 0x06, 0x26, 0x04, 0x05, 0x2a, 0x32, 0x3a, 0x3e, 0x42 },
102 };
103 
104 static const char * const functions[] = { "gpio" };
105 
106 struct sgpio_bank {
107 	struct sgpio_priv *priv;
108 	bool is_input;
109 	struct gpio_chip gpio;
110 	struct pinctrl_desc pctl_desc;
111 };
112 
113 struct sgpio_priv {
114 	struct device *dev;
115 	struct sgpio_bank in;
116 	struct sgpio_bank out;
117 	u32 bitcount;
118 	u32 ports;
119 	u32 clock;
120 	struct regmap *regs;
121 	const struct sgpio_properties *properties;
122 	spinlock_t lock;
123 	/* protects the config register and single shot mode */
124 	struct mutex poll_lock;
125 };
126 
127 struct sgpio_port_addr {
128 	u8 port;
129 	u8 bit;
130 };
131 
132 static inline void sgpio_pin_to_addr(struct sgpio_priv *priv, int pin,
133 				     struct sgpio_port_addr *addr)
134 {
135 	addr->port = pin / priv->bitcount;
136 	addr->bit = pin % priv->bitcount;
137 }
138 
139 static inline int sgpio_addr_to_pin(struct sgpio_priv *priv, int port, int bit)
140 {
141 	return bit + port * priv->bitcount;
142 }
143 
144 static inline u32 sgpio_get_addr(struct sgpio_priv *priv, u32 rno, u32 off)
145 {
146 	return (priv->properties->regoff[rno] + off) *
147 		regmap_get_reg_stride(priv->regs);
148 }
149 
150 static u32 sgpio_readl(struct sgpio_priv *priv, u32 rno, u32 off)
151 {
152 	u32 addr = sgpio_get_addr(priv, rno, off);
153 	u32 val = 0;
154 	int ret;
155 
156 	ret = regmap_read(priv->regs, addr, &val);
157 	WARN_ONCE(ret, "error reading sgpio reg %d\n", ret);
158 
159 	return val;
160 }
161 
162 static void sgpio_writel(struct sgpio_priv *priv,
163 				u32 val, u32 rno, u32 off)
164 {
165 	u32 addr = sgpio_get_addr(priv, rno, off);
166 	int ret;
167 
168 	ret = regmap_write(priv->regs, addr, val);
169 	WARN_ONCE(ret, "error writing sgpio reg %d\n", ret);
170 }
171 
172 static inline void sgpio_clrsetbits(struct sgpio_priv *priv,
173 				    u32 rno, u32 off, u32 clear, u32 set)
174 {
175 	u32 addr = sgpio_get_addr(priv, rno, off);
176 	int ret;
177 
178 	ret = regmap_update_bits(priv->regs, addr, clear | set, set);
179 	WARN_ONCE(ret, "error updating sgpio reg %d\n", ret);
180 }
181 
182 static inline void sgpio_configure_bitstream(struct sgpio_priv *priv)
183 {
184 	int width = priv->bitcount - 1;
185 	u32 clr, set;
186 
187 	switch (priv->properties->arch) {
188 	case SGPIO_ARCH_LUTON:
189 		clr = SGPIO_LUTON_PORT_WIDTH;
190 		set = SGPIO_LUTON_AUTO_REPEAT |
191 			FIELD_PREP(SGPIO_LUTON_PORT_WIDTH, width);
192 		break;
193 	case SGPIO_ARCH_OCELOT:
194 		clr = SGPIO_OCELOT_PORT_WIDTH;
195 		set = SGPIO_OCELOT_AUTO_REPEAT |
196 			FIELD_PREP(SGPIO_OCELOT_PORT_WIDTH, width);
197 		break;
198 	case SGPIO_ARCH_SPARX5:
199 		clr = SGPIO_SPARX5_PORT_WIDTH;
200 		set = SGPIO_SPARX5_AUTO_REPEAT |
201 			FIELD_PREP(SGPIO_SPARX5_PORT_WIDTH, width);
202 		break;
203 	default:
204 		return;
205 	}
206 	sgpio_clrsetbits(priv, REG_SIO_CONFIG, 0, clr, set);
207 }
208 
209 static inline void sgpio_configure_clock(struct sgpio_priv *priv, u32 clkfrq)
210 {
211 	u32 clr, set;
212 
213 	switch (priv->properties->arch) {
214 	case SGPIO_ARCH_LUTON:
215 		clr = SGPIO_LUTON_CLK_FREQ;
216 		set = FIELD_PREP(SGPIO_LUTON_CLK_FREQ, clkfrq);
217 		break;
218 	case SGPIO_ARCH_OCELOT:
219 		clr = SGPIO_OCELOT_CLK_FREQ;
220 		set = FIELD_PREP(SGPIO_OCELOT_CLK_FREQ, clkfrq);
221 		break;
222 	case SGPIO_ARCH_SPARX5:
223 		clr = SGPIO_SPARX5_CLK_FREQ;
224 		set = FIELD_PREP(SGPIO_SPARX5_CLK_FREQ, clkfrq);
225 		break;
226 	default:
227 		return;
228 	}
229 	sgpio_clrsetbits(priv, REG_SIO_CLOCK, 0, clr, set);
230 }
231 
232 static int sgpio_single_shot(struct sgpio_priv *priv)
233 {
234 	u32 addr = sgpio_get_addr(priv, REG_SIO_CONFIG, 0);
235 	int ret, ret2;
236 	u32 ctrl;
237 	unsigned int single_shot;
238 	unsigned int auto_repeat;
239 
240 	switch (priv->properties->arch) {
241 	case SGPIO_ARCH_LUTON:
242 		/* not supported for now */
243 		return 0;
244 	case SGPIO_ARCH_OCELOT:
245 		single_shot = SGPIO_OCELOT_SINGLE_SHOT;
246 		auto_repeat = SGPIO_OCELOT_AUTO_REPEAT;
247 		break;
248 	case SGPIO_ARCH_SPARX5:
249 		single_shot = SGPIO_SPARX5_SINGLE_SHOT;
250 		auto_repeat = SGPIO_SPARX5_AUTO_REPEAT;
251 		break;
252 	default:
253 		return -EINVAL;
254 	}
255 
256 	/*
257 	 * Trigger immediate burst. This only works when auto repeat is turned
258 	 * off. Otherwise, the single shot bit will never be cleared by the
259 	 * hardware. Measurements showed that an update might take as long as
260 	 * the burst gap. On a LAN9668 this is about 50ms for the largest
261 	 * setting.
262 	 * After the manual burst, reenable the auto repeat mode again.
263 	 */
264 	mutex_lock(&priv->poll_lock);
265 	ret = regmap_update_bits(priv->regs, addr, single_shot | auto_repeat,
266 				 single_shot);
267 	if (ret)
268 		goto out;
269 
270 	ret = regmap_read_poll_timeout(priv->regs, addr, ctrl,
271 				       !(ctrl & single_shot), 100, 60000);
272 
273 	/* reenable auto repeat mode even if there was an error */
274 	ret2 = regmap_update_bits(priv->regs, addr, auto_repeat, auto_repeat);
275 out:
276 	mutex_unlock(&priv->poll_lock);
277 
278 	return ret ?: ret2;
279 }
280 
281 static int sgpio_output_set(struct sgpio_priv *priv,
282 			    struct sgpio_port_addr *addr,
283 			    int value)
284 {
285 	unsigned int bit = SGPIO_SRC_BITS * addr->bit;
286 	u32 reg = sgpio_get_addr(priv, REG_PORT_CONFIG, addr->port);
287 	bool changed;
288 	u32 clr, set;
289 	int ret;
290 
291 	switch (priv->properties->arch) {
292 	case SGPIO_ARCH_LUTON:
293 		clr = FIELD_PREP(SGPIO_LUTON_BIT_SOURCE, BIT(bit));
294 		set = FIELD_PREP(SGPIO_LUTON_BIT_SOURCE, value << bit);
295 		break;
296 	case SGPIO_ARCH_OCELOT:
297 		clr = FIELD_PREP(SGPIO_OCELOT_BIT_SOURCE, BIT(bit));
298 		set = FIELD_PREP(SGPIO_OCELOT_BIT_SOURCE, value << bit);
299 		break;
300 	case SGPIO_ARCH_SPARX5:
301 		clr = FIELD_PREP(SGPIO_SPARX5_BIT_SOURCE, BIT(bit));
302 		set = FIELD_PREP(SGPIO_SPARX5_BIT_SOURCE, value << bit);
303 		break;
304 	default:
305 		return -EINVAL;
306 	}
307 
308 	ret = regmap_update_bits_check(priv->regs, reg, clr | set, set,
309 				       &changed);
310 	if (ret)
311 		return ret;
312 
313 	if (changed) {
314 		ret = sgpio_single_shot(priv);
315 		if (ret)
316 			return ret;
317 	}
318 
319 	return 0;
320 }
321 
322 static int sgpio_output_get(struct sgpio_priv *priv,
323 			    struct sgpio_port_addr *addr)
324 {
325 	u32 val, portval = sgpio_readl(priv, REG_PORT_CONFIG, addr->port);
326 	unsigned int bit = SGPIO_SRC_BITS * addr->bit;
327 
328 	switch (priv->properties->arch) {
329 	case SGPIO_ARCH_LUTON:
330 		val = FIELD_GET(SGPIO_LUTON_BIT_SOURCE, portval);
331 		break;
332 	case SGPIO_ARCH_OCELOT:
333 		val = FIELD_GET(SGPIO_OCELOT_BIT_SOURCE, portval);
334 		break;
335 	case SGPIO_ARCH_SPARX5:
336 		val = FIELD_GET(SGPIO_SPARX5_BIT_SOURCE, portval);
337 		break;
338 	default:
339 		val = 0;
340 		break;
341 	}
342 	return !!(val & BIT(bit));
343 }
344 
345 static int sgpio_input_get(struct sgpio_priv *priv,
346 			   struct sgpio_port_addr *addr)
347 {
348 	return !!(sgpio_readl(priv, REG_INPUT_DATA, addr->bit) & BIT(addr->port));
349 }
350 
351 static int sgpio_pinconf_get(struct pinctrl_dev *pctldev,
352 			     unsigned int pin, unsigned long *config)
353 {
354 	struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
355 	u32 param = pinconf_to_config_param(*config);
356 	struct sgpio_priv *priv = bank->priv;
357 	struct sgpio_port_addr addr;
358 	int val;
359 
360 	sgpio_pin_to_addr(priv, pin, &addr);
361 
362 	switch (param) {
363 	case PIN_CONFIG_INPUT_ENABLE:
364 		val = bank->is_input;
365 		break;
366 
367 	case PIN_CONFIG_OUTPUT_ENABLE:
368 		val = !bank->is_input;
369 		break;
370 
371 	case PIN_CONFIG_OUTPUT:
372 		if (bank->is_input)
373 			return -EINVAL;
374 		val = sgpio_output_get(priv, &addr);
375 		break;
376 
377 	default:
378 		return -ENOTSUPP;
379 	}
380 
381 	*config = pinconf_to_config_packed(param, val);
382 
383 	return 0;
384 }
385 
386 static int sgpio_pinconf_set(struct pinctrl_dev *pctldev, unsigned int pin,
387 			     unsigned long *configs, unsigned int num_configs)
388 {
389 	struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
390 	struct sgpio_priv *priv = bank->priv;
391 	struct sgpio_port_addr addr;
392 	int cfg, err = 0;
393 	u32 param, arg;
394 
395 	sgpio_pin_to_addr(priv, pin, &addr);
396 
397 	for (cfg = 0; cfg < num_configs; cfg++) {
398 		param = pinconf_to_config_param(configs[cfg]);
399 		arg = pinconf_to_config_argument(configs[cfg]);
400 
401 		switch (param) {
402 		case PIN_CONFIG_OUTPUT:
403 			if (bank->is_input)
404 				return -EINVAL;
405 			err = sgpio_output_set(priv, &addr, arg);
406 			break;
407 
408 		default:
409 			err = -ENOTSUPP;
410 		}
411 	}
412 
413 	return err;
414 }
415 
416 static const struct pinconf_ops sgpio_confops = {
417 	.is_generic = true,
418 	.pin_config_get = sgpio_pinconf_get,
419 	.pin_config_set = sgpio_pinconf_set,
420 	.pin_config_config_dbg_show = pinconf_generic_dump_config,
421 };
422 
423 static int sgpio_get_functions_count(struct pinctrl_dev *pctldev)
424 {
425 	return 1;
426 }
427 
428 static const char *sgpio_get_function_name(struct pinctrl_dev *pctldev,
429 					   unsigned int function)
430 {
431 	return functions[0];
432 }
433 
434 static int sgpio_get_function_groups(struct pinctrl_dev *pctldev,
435 				     unsigned int function,
436 				     const char *const **groups,
437 				     unsigned *const num_groups)
438 {
439 	*groups  = functions;
440 	*num_groups = ARRAY_SIZE(functions);
441 
442 	return 0;
443 }
444 
445 static int sgpio_pinmux_set_mux(struct pinctrl_dev *pctldev,
446 				unsigned int selector, unsigned int group)
447 {
448 	return 0;
449 }
450 
451 static int sgpio_gpio_set_direction(struct pinctrl_dev *pctldev,
452 				    struct pinctrl_gpio_range *range,
453 				    unsigned int pin, bool input)
454 {
455 	struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
456 
457 	return (input == bank->is_input) ? 0 : -EINVAL;
458 }
459 
460 static int sgpio_gpio_request_enable(struct pinctrl_dev *pctldev,
461 				     struct pinctrl_gpio_range *range,
462 				     unsigned int offset)
463 {
464 	struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
465 	struct sgpio_priv *priv = bank->priv;
466 	struct sgpio_port_addr addr;
467 
468 	sgpio_pin_to_addr(priv, offset, &addr);
469 
470 	if ((priv->ports & BIT(addr.port)) == 0) {
471 		dev_warn(priv->dev, "Request port %d.%d: Port is not enabled\n",
472 			 addr.port, addr.bit);
473 		return -EINVAL;
474 	}
475 
476 	return 0;
477 }
478 
479 static const struct pinmux_ops sgpio_pmx_ops = {
480 	.get_functions_count = sgpio_get_functions_count,
481 	.get_function_name = sgpio_get_function_name,
482 	.get_function_groups = sgpio_get_function_groups,
483 	.set_mux = sgpio_pinmux_set_mux,
484 	.gpio_set_direction = sgpio_gpio_set_direction,
485 	.gpio_request_enable = sgpio_gpio_request_enable,
486 };
487 
488 static int sgpio_pctl_get_groups_count(struct pinctrl_dev *pctldev)
489 {
490 	struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
491 
492 	return bank->pctl_desc.npins;
493 }
494 
495 static const char *sgpio_pctl_get_group_name(struct pinctrl_dev *pctldev,
496 					     unsigned int group)
497 {
498 	struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
499 
500 	return bank->pctl_desc.pins[group].name;
501 }
502 
503 static int sgpio_pctl_get_group_pins(struct pinctrl_dev *pctldev,
504 				     unsigned int group,
505 				     const unsigned int **pins,
506 				     unsigned int *num_pins)
507 {
508 	struct sgpio_bank *bank = pinctrl_dev_get_drvdata(pctldev);
509 
510 	*pins = &bank->pctl_desc.pins[group].number;
511 	*num_pins = 1;
512 
513 	return 0;
514 }
515 
516 static const struct pinctrl_ops sgpio_pctl_ops = {
517 	.get_groups_count = sgpio_pctl_get_groups_count,
518 	.get_group_name = sgpio_pctl_get_group_name,
519 	.get_group_pins = sgpio_pctl_get_group_pins,
520 	.dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
521 	.dt_free_map = pinconf_generic_dt_free_map,
522 };
523 
524 static int microchip_sgpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
525 {
526 	struct sgpio_bank *bank = gpiochip_get_data(gc);
527 
528 	/* Fixed-position function */
529 	return bank->is_input ? 0 : -EINVAL;
530 }
531 
532 static int microchip_sgpio_direction_output(struct gpio_chip *gc,
533 				       unsigned int gpio, int value)
534 {
535 	struct sgpio_bank *bank = gpiochip_get_data(gc);
536 	struct sgpio_priv *priv = bank->priv;
537 	struct sgpio_port_addr addr;
538 
539 	/* Fixed-position function */
540 	if (bank->is_input)
541 		return -EINVAL;
542 
543 	sgpio_pin_to_addr(priv, gpio, &addr);
544 
545 	return sgpio_output_set(priv, &addr, value);
546 }
547 
548 static int microchip_sgpio_get_direction(struct gpio_chip *gc, unsigned int gpio)
549 {
550 	struct sgpio_bank *bank = gpiochip_get_data(gc);
551 
552 	return bank->is_input ? GPIO_LINE_DIRECTION_IN : GPIO_LINE_DIRECTION_OUT;
553 }
554 
555 static void microchip_sgpio_set_value(struct gpio_chip *gc,
556 				unsigned int gpio, int value)
557 {
558 	microchip_sgpio_direction_output(gc, gpio, value);
559 }
560 
561 static int microchip_sgpio_get_value(struct gpio_chip *gc, unsigned int gpio)
562 {
563 	struct sgpio_bank *bank = gpiochip_get_data(gc);
564 	struct sgpio_priv *priv = bank->priv;
565 	struct sgpio_port_addr addr;
566 
567 	sgpio_pin_to_addr(priv, gpio, &addr);
568 
569 	return bank->is_input ? sgpio_input_get(priv, &addr) : sgpio_output_get(priv, &addr);
570 }
571 
572 static int microchip_sgpio_of_xlate(struct gpio_chip *gc,
573 			       const struct of_phandle_args *gpiospec,
574 			       u32 *flags)
575 {
576 	struct sgpio_bank *bank = gpiochip_get_data(gc);
577 	struct sgpio_priv *priv = bank->priv;
578 	int pin;
579 
580 	/*
581 	 * Note that the SGIO pin is defined by *2* numbers, a port
582 	 * number between 0 and 31, and a bit index, 0 to 3.
583 	 */
584 	if (gpiospec->args[0] > SGPIO_BITS_PER_WORD ||
585 	    gpiospec->args[1] > priv->bitcount)
586 		return -EINVAL;
587 
588 	pin = sgpio_addr_to_pin(priv, gpiospec->args[0], gpiospec->args[1]);
589 
590 	if (pin > gc->ngpio)
591 		return -EINVAL;
592 
593 	if (flags)
594 		*flags = gpiospec->args[2];
595 
596 	return pin;
597 }
598 
599 static int microchip_sgpio_get_ports(struct sgpio_priv *priv)
600 {
601 	const char *range_property_name = "microchip,sgpio-port-ranges";
602 	struct device *dev = priv->dev;
603 	u32 range_params[64];
604 	int i, nranges, ret;
605 
606 	/* Calculate port mask */
607 	nranges = device_property_count_u32(dev, range_property_name);
608 	if (nranges < 2 || nranges % 2 || nranges > ARRAY_SIZE(range_params)) {
609 		dev_err(dev, "%s port range: '%s' property\n",
610 			nranges == -EINVAL ? "Missing" : "Invalid",
611 			range_property_name);
612 		return -EINVAL;
613 	}
614 
615 	ret = device_property_read_u32_array(dev, range_property_name,
616 					     range_params, nranges);
617 	if (ret) {
618 		dev_err(dev, "failed to parse '%s' property: %d\n",
619 			range_property_name, ret);
620 		return ret;
621 	}
622 	for (i = 0; i < nranges; i += 2) {
623 		int start, end;
624 
625 		start = range_params[i];
626 		end = range_params[i + 1];
627 		if (start > end || end >= SGPIO_BITS_PER_WORD) {
628 			dev_err(dev, "Ill-formed port-range [%d:%d]\n",
629 				start, end);
630 		}
631 		priv->ports |= GENMASK(end, start);
632 	}
633 
634 	return 0;
635 }
636 
637 static void microchip_sgpio_irq_settype(struct irq_data *data,
638 					int type,
639 					int polarity)
640 {
641 	struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
642 	struct sgpio_bank *bank = gpiochip_get_data(chip);
643 	unsigned int gpio = irqd_to_hwirq(data);
644 	struct sgpio_port_addr addr;
645 	unsigned long flags;
646 	u32 ena;
647 
648 	sgpio_pin_to_addr(bank->priv, gpio, &addr);
649 
650 	spin_lock_irqsave(&bank->priv->lock, flags);
651 
652 	/* Disable interrupt while changing type */
653 	ena = sgpio_readl(bank->priv, REG_INT_ENABLE, addr.bit);
654 	sgpio_writel(bank->priv, ena & ~BIT(addr.port), REG_INT_ENABLE, addr.bit);
655 
656 	/* Type value spread over 2 registers sets: low, high bit */
657 	sgpio_clrsetbits(bank->priv, REG_INT_TRIGGER, addr.bit,
658 			 BIT(addr.port), (!!(type & 0x1)) << addr.port);
659 	sgpio_clrsetbits(bank->priv, REG_INT_TRIGGER, SGPIO_MAX_BITS + addr.bit,
660 			 BIT(addr.port), (!!(type & 0x2)) << addr.port);
661 
662 	if (type == SGPIO_INT_TRG_LEVEL)
663 		sgpio_clrsetbits(bank->priv, REG_INT_POLARITY, addr.bit,
664 				 BIT(addr.port), polarity << addr.port);
665 
666 	/* Possibly re-enable interrupts */
667 	sgpio_writel(bank->priv, ena, REG_INT_ENABLE, addr.bit);
668 
669 	spin_unlock_irqrestore(&bank->priv->lock, flags);
670 }
671 
672 static void microchip_sgpio_irq_setreg(struct irq_data *data,
673 				       int reg,
674 				       bool clear)
675 {
676 	struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
677 	struct sgpio_bank *bank = gpiochip_get_data(chip);
678 	unsigned int gpio = irqd_to_hwirq(data);
679 	struct sgpio_port_addr addr;
680 
681 	sgpio_pin_to_addr(bank->priv, gpio, &addr);
682 
683 	if (clear)
684 		sgpio_clrsetbits(bank->priv, reg, addr.bit, BIT(addr.port), 0);
685 	else
686 		sgpio_clrsetbits(bank->priv, reg, addr.bit, 0, BIT(addr.port));
687 }
688 
689 static void microchip_sgpio_irq_mask(struct irq_data *data)
690 {
691 	microchip_sgpio_irq_setreg(data, REG_INT_ENABLE, true);
692 }
693 
694 static void microchip_sgpio_irq_unmask(struct irq_data *data)
695 {
696 	microchip_sgpio_irq_setreg(data, REG_INT_ENABLE, false);
697 }
698 
699 static void microchip_sgpio_irq_ack(struct irq_data *data)
700 {
701 	struct gpio_chip *chip = irq_data_get_irq_chip_data(data);
702 	struct sgpio_bank *bank = gpiochip_get_data(chip);
703 	unsigned int gpio = irqd_to_hwirq(data);
704 	struct sgpio_port_addr addr;
705 
706 	sgpio_pin_to_addr(bank->priv, gpio, &addr);
707 
708 	sgpio_writel(bank->priv, BIT(addr.port), REG_INT_ACK, addr.bit);
709 }
710 
711 static int microchip_sgpio_irq_set_type(struct irq_data *data, unsigned int type)
712 {
713 	type &= IRQ_TYPE_SENSE_MASK;
714 
715 	switch (type) {
716 	case IRQ_TYPE_EDGE_BOTH:
717 		irq_set_handler_locked(data, handle_edge_irq);
718 		microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE, 0);
719 		break;
720 	case IRQ_TYPE_EDGE_RISING:
721 		irq_set_handler_locked(data, handle_edge_irq);
722 		microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE_RISE, 0);
723 		break;
724 	case IRQ_TYPE_EDGE_FALLING:
725 		irq_set_handler_locked(data, handle_edge_irq);
726 		microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_EDGE_FALL, 0);
727 		break;
728 	case IRQ_TYPE_LEVEL_HIGH:
729 		irq_set_handler_locked(data, handle_level_irq);
730 		microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_LEVEL, SGPIO_TRG_LEVEL_HIGH);
731 		break;
732 	case IRQ_TYPE_LEVEL_LOW:
733 		irq_set_handler_locked(data, handle_level_irq);
734 		microchip_sgpio_irq_settype(data, SGPIO_INT_TRG_LEVEL, SGPIO_TRG_LEVEL_LOW);
735 		break;
736 	default:
737 		return -EINVAL;
738 	}
739 
740 	return 0;
741 }
742 
743 static const struct irq_chip microchip_sgpio_irqchip = {
744 	.name		= "gpio",
745 	.irq_mask	= microchip_sgpio_irq_mask,
746 	.irq_ack	= microchip_sgpio_irq_ack,
747 	.irq_unmask	= microchip_sgpio_irq_unmask,
748 	.irq_set_type	= microchip_sgpio_irq_set_type,
749 };
750 
751 static void sgpio_irq_handler(struct irq_desc *desc)
752 {
753 	struct irq_chip *parent_chip = irq_desc_get_chip(desc);
754 	struct gpio_chip *chip = irq_desc_get_handler_data(desc);
755 	struct sgpio_bank *bank = gpiochip_get_data(chip);
756 	struct sgpio_priv *priv = bank->priv;
757 	int bit, port, gpio;
758 	long val;
759 
760 	for (bit = 0; bit < priv->bitcount; bit++) {
761 		val = sgpio_readl(priv, REG_INT_IDENT, bit);
762 		if (!val)
763 			continue;
764 
765 		chained_irq_enter(parent_chip, desc);
766 
767 		for_each_set_bit(port, &val, SGPIO_BITS_PER_WORD) {
768 			gpio = sgpio_addr_to_pin(priv, port, bit);
769 			generic_handle_domain_irq(chip->irq.domain, gpio);
770 		}
771 
772 		chained_irq_exit(parent_chip, desc);
773 	}
774 }
775 
776 static int microchip_sgpio_register_bank(struct device *dev,
777 					 struct sgpio_priv *priv,
778 					 struct fwnode_handle *fwnode,
779 					 int bankno)
780 {
781 	struct pinctrl_pin_desc *pins;
782 	struct pinctrl_desc *pctl_desc;
783 	struct pinctrl_dev *pctldev;
784 	struct sgpio_bank *bank;
785 	struct gpio_chip *gc;
786 	u32 ngpios;
787 	int i, ret;
788 
789 	/* Get overall bank struct */
790 	bank = (bankno == 0) ? &priv->in : &priv->out;
791 	bank->priv = priv;
792 
793 	if (fwnode_property_read_u32(fwnode, "ngpios", &ngpios)) {
794 		dev_info(dev, "failed to get number of gpios for bank%d\n",
795 			 bankno);
796 		ngpios = 64;
797 	}
798 
799 	priv->bitcount = ngpios / SGPIO_BITS_PER_WORD;
800 	if (priv->bitcount > SGPIO_MAX_BITS) {
801 		dev_err(dev, "Bit width exceeds maximum (%d)\n",
802 			SGPIO_MAX_BITS);
803 		return -EINVAL;
804 	}
805 
806 	pctl_desc = &bank->pctl_desc;
807 	pctl_desc->name = devm_kasprintf(dev, GFP_KERNEL, "%s-%sput",
808 					 dev_name(dev),
809 					 bank->is_input ? "in" : "out");
810 	pctl_desc->pctlops = &sgpio_pctl_ops;
811 	pctl_desc->pmxops = &sgpio_pmx_ops;
812 	pctl_desc->confops = &sgpio_confops;
813 	pctl_desc->owner = THIS_MODULE;
814 
815 	pins = devm_kzalloc(dev, sizeof(*pins)*ngpios, GFP_KERNEL);
816 	if (!pins)
817 		return -ENOMEM;
818 
819 	pctl_desc->npins = ngpios;
820 	pctl_desc->pins = pins;
821 
822 	for (i = 0; i < ngpios; i++) {
823 		struct sgpio_port_addr addr;
824 
825 		sgpio_pin_to_addr(priv, i, &addr);
826 
827 		pins[i].number = i;
828 		pins[i].name = devm_kasprintf(dev, GFP_KERNEL,
829 					      "SGPIO_%c_p%db%d",
830 					      bank->is_input ? 'I' : 'O',
831 					      addr.port, addr.bit);
832 		if (!pins[i].name)
833 			return -ENOMEM;
834 	}
835 
836 	pctldev = devm_pinctrl_register(dev, pctl_desc, bank);
837 	if (IS_ERR(pctldev))
838 		return dev_err_probe(dev, PTR_ERR(pctldev), "Failed to register pinctrl\n");
839 
840 	gc			= &bank->gpio;
841 	gc->label		= pctl_desc->name;
842 	gc->parent		= dev;
843 	gc->of_node		= to_of_node(fwnode);
844 	gc->owner		= THIS_MODULE;
845 	gc->get_direction	= microchip_sgpio_get_direction;
846 	gc->direction_input	= microchip_sgpio_direction_input;
847 	gc->direction_output	= microchip_sgpio_direction_output;
848 	gc->get			= microchip_sgpio_get_value;
849 	gc->set			= microchip_sgpio_set_value;
850 	gc->request		= gpiochip_generic_request;
851 	gc->free		= gpiochip_generic_free;
852 	gc->of_xlate		= microchip_sgpio_of_xlate;
853 	gc->of_gpio_n_cells     = 3;
854 	gc->base		= -1;
855 	gc->ngpio		= ngpios;
856 	gc->can_sleep		= !bank->is_input;
857 
858 	if (bank->is_input && priv->properties->flags & SGPIO_FLAGS_HAS_IRQ) {
859 		int irq = fwnode_irq_get(fwnode, 0);
860 
861 		if (irq) {
862 			struct gpio_irq_chip *girq = &gc->irq;
863 
864 			girq->chip = devm_kmemdup(dev, &microchip_sgpio_irqchip,
865 						  sizeof(microchip_sgpio_irqchip),
866 						  GFP_KERNEL);
867 			if (!girq->chip)
868 				return -ENOMEM;
869 			girq->parent_handler = sgpio_irq_handler;
870 			girq->num_parents = 1;
871 			girq->parents = devm_kcalloc(dev, 1,
872 						     sizeof(*girq->parents),
873 						     GFP_KERNEL);
874 			if (!girq->parents)
875 				return -ENOMEM;
876 			girq->parents[0] = irq;
877 			girq->default_type = IRQ_TYPE_NONE;
878 			girq->handler = handle_bad_irq;
879 
880 			/* Disable all individual pins */
881 			for (i = 0; i < SGPIO_MAX_BITS; i++)
882 				sgpio_writel(priv, 0, REG_INT_ENABLE, i);
883 			/* Master enable */
884 			sgpio_clrsetbits(priv, REG_SIO_CONFIG, 0, 0, SGPIO_MASTER_INTR_ENA);
885 		}
886 	}
887 
888 	ret = devm_gpiochip_add_data(dev, gc, bank);
889 	if (ret)
890 		dev_err(dev, "Failed to register: ret %d\n", ret);
891 
892 	return ret;
893 }
894 
895 static int microchip_sgpio_probe(struct platform_device *pdev)
896 {
897 	int div_clock = 0, ret, port, i, nbanks;
898 	struct device *dev = &pdev->dev;
899 	struct fwnode_handle *fwnode;
900 	struct reset_control *reset;
901 	struct sgpio_priv *priv;
902 	struct clk *clk;
903 	u32 __iomem *regs;
904 	u32 val;
905 	struct regmap_config regmap_config = {
906 		.reg_bits = 32,
907 		.val_bits = 32,
908 		.reg_stride = 4,
909 	};
910 
911 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
912 	if (!priv)
913 		return -ENOMEM;
914 
915 	priv->dev = dev;
916 	spin_lock_init(&priv->lock);
917 	mutex_init(&priv->poll_lock);
918 
919 	reset = devm_reset_control_get_optional_shared(&pdev->dev, "switch");
920 	if (IS_ERR(reset))
921 		return dev_err_probe(dev, PTR_ERR(reset), "Failed to get reset\n");
922 	reset_control_reset(reset);
923 
924 	clk = devm_clk_get(dev, NULL);
925 	if (IS_ERR(clk))
926 		return dev_err_probe(dev, PTR_ERR(clk), "Failed to get clock\n");
927 
928 	div_clock = clk_get_rate(clk);
929 	if (device_property_read_u32(dev, "bus-frequency", &priv->clock))
930 		priv->clock = 12500000;
931 	if (priv->clock == 0 || priv->clock > (div_clock / 2)) {
932 		dev_err(dev, "Invalid frequency %d\n", priv->clock);
933 		return -EINVAL;
934 	}
935 
936 	regs = devm_platform_ioremap_resource(pdev, 0);
937 	if (IS_ERR(regs))
938 		return PTR_ERR(regs);
939 
940 	priv->regs = devm_regmap_init_mmio(dev, regs, &regmap_config);
941 	if (IS_ERR(priv->regs))
942 		return PTR_ERR(priv->regs);
943 
944 	priv->properties = device_get_match_data(dev);
945 	priv->in.is_input = true;
946 
947 	/* Get rest of device properties */
948 	ret = microchip_sgpio_get_ports(priv);
949 	if (ret)
950 		return ret;
951 
952 	nbanks = device_get_child_node_count(dev);
953 	if (nbanks != 2) {
954 		dev_err(dev, "Must have 2 banks (have %d)\n", nbanks);
955 		return -EINVAL;
956 	}
957 
958 	i = 0;
959 	device_for_each_child_node(dev, fwnode) {
960 		ret = microchip_sgpio_register_bank(dev, priv, fwnode, i++);
961 		if (ret) {
962 			fwnode_handle_put(fwnode);
963 			return ret;
964 		}
965 	}
966 
967 	if (priv->in.gpio.ngpio != priv->out.gpio.ngpio) {
968 		dev_err(dev, "Banks must have same GPIO count\n");
969 		return -ERANGE;
970 	}
971 
972 	sgpio_configure_bitstream(priv);
973 
974 	val = max(2U, div_clock / priv->clock);
975 	sgpio_configure_clock(priv, val);
976 
977 	for (port = 0; port < SGPIO_BITS_PER_WORD; port++)
978 		sgpio_writel(priv, 0, REG_PORT_CONFIG, port);
979 	sgpio_writel(priv, priv->ports, REG_PORT_ENABLE, 0);
980 
981 	return 0;
982 }
983 
984 static const struct of_device_id microchip_sgpio_gpio_of_match[] = {
985 	{
986 		.compatible = "microchip,sparx5-sgpio",
987 		.data = &properties_sparx5,
988 	}, {
989 		.compatible = "mscc,luton-sgpio",
990 		.data = &properties_luton,
991 	}, {
992 		.compatible = "mscc,ocelot-sgpio",
993 		.data = &properties_ocelot,
994 	}, {
995 		/* sentinel */
996 	}
997 };
998 
999 static struct platform_driver microchip_sgpio_pinctrl_driver = {
1000 	.driver = {
1001 		.name = "pinctrl-microchip-sgpio",
1002 		.of_match_table = microchip_sgpio_gpio_of_match,
1003 		.suppress_bind_attrs = true,
1004 	},
1005 	.probe = microchip_sgpio_probe,
1006 };
1007 builtin_platform_driver(microchip_sgpio_pinctrl_driver);
1008