Debian Codegraph @

drivers/pinctrl/renesas/pinctrl-rzt2h.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Renesas RZ/T2H Pin Control and GPIO driver core
4 *
5 * Based on drivers/pinctrl/renesas/pinctrl-rzg2l.c
6 *
7 * Copyright (C) 2025 Renesas Electronics Corporation.
8 */
9
10 #include <linux/bitfield.h>
11 #include <linux/bitops.h>
12 #include <linux/bits.h>
13 #include <linux/cleanup.h>
14 #include <linux/clk.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/io.h>
17 #include <linux/ioport.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/of_device.h>
21 #include <linux/of_irq.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/spinlock.h>
25 #include <linux/types.h>
26
27 #include <linux/pinctrl/consumer.h>
28 #include <linux/pinctrl/pinconf-generic.h>
29 #include <linux/pinctrl/pinconf.h>
30 #include <linux/pinctrl/pinctrl.h>
31 #include <linux/pinctrl/pinmux.h>
32
33 #include <dt-bindings/pinctrl/renesas,r9a09g077-pinctrl.h>
34
35 #include "../core.h"
36 #include "../pinconf.h"
37 #include "../pinmux.h"
38
39 #define DRV_NAME "pinctrl-rzt2h"
40
41 #define P(m) (0x001 * (m))
42 #define PM(m) (0x200 + 2 * (m))
43 #define PMC(m) (0x400 + (m))
44 #define PFC(m) (0x600 + 8 * (m))
45 #define PIN(m) (0x800 + (m))
46 #define RSELP(m) (0xc00 + (m))
47
48 #define PM_MASK GENMASK(1, 0)
49 #define PM_PIN_MASK(pin) (PM_MASK << ((pin) * 2))
50 #define PM_INPUT BIT(0)
51 #define PM_OUTPUT BIT(1)
52
53 #define PFC_MASK GENMASK_ULL(5, 0)
54 #define PFC_PIN_MASK(pin) (PFC_MASK << ((pin) * 8))
55 #define PFC_FUNC_INTERRUPT 0
56
57 /*
58 * Use 16 lower bits [15:0] for pin identifier
59 * Use 8 higher bits [23:16] for pin mux function
60 */
61 #define MUX_PIN_ID_MASK GENMASK(15, 0)
62 #define MUX_FUNC_MASK GENMASK(23, 16)
63
64 #define RZT2H_PIN_ID_TO_PORT(id) ((id) / RZT2H_PINS_PER_PORT)
65 #define RZT2H_PIN_ID_TO_PIN(id) ((id) % RZT2H_PINS_PER_PORT)
66
67 #define RZT2H_MAX_SAFETY_PORTS 12
68
69 #define RZT2H_INTERRUPTS_START 16
70 #define RZT2H_INTERRUPTS_NUM 17
71
72 struct rzt2h_pinctrl_data {
73 unsigned int n_port_pins;
74 const u8 *port_pin_configs;
75 unsigned int n_ports;
76 };
77
78 struct rzt2h_pinctrl {
79 struct pinctrl_dev *pctl;
80 struct pinctrl_desc desc;
81 struct pinctrl_pin_desc *pins;
82 const struct rzt2h_pinctrl_data *data;
83 void __iomem *base0, *base1;
84 struct device *dev;
85 struct gpio_chip gpio_chip;
86 struct pinctrl_gpio_range gpio_range;
87 DECLARE_BITMAP(used_irqs, RZT2H_INTERRUPTS_NUM);
88 raw_spinlock_t lock; /* lock read/write registers */
89 struct mutex mutex; /* serialize adding groups and functions */
90 bool safety_port_enabled;
91 atomic_t wakeup_path;
92 };
93
94 #define RZT2H_GET_BASE(pctrl, port) \
95 ((port) > RZT2H_MAX_SAFETY_PORTS ? (pctrl)->base0 : (pctrl)->base1)
96
97 #define RZT2H_PINCTRL_REG_ACCESS(size, type) \
98 static inline void rzt2h_pinctrl_write##size(struct rzt2h_pinctrl *pctrl, u8 port, \
99 type val, unsigned int offset) \
100 { \
101 write##size(val, RZT2H_GET_BASE(pctrl, port) + offset); \
102 } \
103 static inline type rzt2h_pinctrl_read##size(struct rzt2h_pinctrl *pctrl, u8 port, \
104 unsigned int offset) \
105 { \
106 return read##size(RZT2H_GET_BASE(pctrl, port) + offset); \
107 }
108
109 RZT2H_PINCTRL_REG_ACCESS(b, u8)
110 RZT2H_PINCTRL_REG_ACCESS(w, u16)
111 RZT2H_PINCTRL_REG_ACCESS(q, u64)
112
113 static int rzt2h_validate_pin(struct rzt2h_pinctrl *pctrl, unsigned int offset)
114 {
115 u8 port = RZT2H_PIN_ID_TO_PORT(offset);
116 u8 pin = RZT2H_PIN_ID_TO_PIN(offset);
117 u8 pincfg;
118
119 if (offset >= pctrl->data->n_port_pins || port >= pctrl->data->n_ports)
120 return -EINVAL;
121
122 if (!pctrl->safety_port_enabled && port <= RZT2H_MAX_SAFETY_PORTS)
123 return -EINVAL;
124
125 pincfg = pctrl->data->port_pin_configs[port];
126 return (pincfg & BIT(pin)) ? 0 : -EINVAL;
127 }
128
129 static void rzt2h_pinctrl_set_gpio_en(struct rzt2h_pinctrl *pctrl,
130 u8 port, u8 pin, bool en)
131 {
132 u8 reg = rzt2h_pinctrl_readb(pctrl, port, PMC(port));
133
134 if (en)
135 reg &= ~BIT(pin);
136 else
137 reg |= BIT(pin);
138
139 rzt2h_pinctrl_writeb(pctrl, port, reg, PMC(port));
140 }
141
142 static void rzt2h_pinctrl_set_pfc_mode(struct rzt2h_pinctrl *pctrl,
143 u8 port, u8 pin, u8 func)
144 {
145 u64 reg64;
146 u16 reg16;
147
148 guard(raw_spinlock_irqsave)(&pctrl->lock);
149
150 /* Set pin to 'Non-use (Hi-Z input protection)' */
151 reg16 = rzt2h_pinctrl_readw(pctrl, port, PM(port));
152 reg16 &= ~PM_PIN_MASK(pin);
153 rzt2h_pinctrl_writew(pctrl, port, reg16, PM(port));
154
155 /* Temporarily switch to GPIO mode with PMC register */
156 rzt2h_pinctrl_set_gpio_en(pctrl, port, pin, true);
157
158 /* Select Pin function mode with PFC register */
159 reg64 = rzt2h_pinctrl_readq(pctrl, port, PFC(port));
160 reg64 &= ~PFC_PIN_MASK(pin);
161 rzt2h_pinctrl_writeq(pctrl, port, reg64 | ((u64)func << (pin * 8)), PFC(port));
162
163 /* Switch to Peripheral pin function with PMC register */
164 rzt2h_pinctrl_set_gpio_en(pctrl, port, pin, false);
165 }
166
167 static int rzt2h_pinctrl_set_mux(struct pinctrl_dev *pctldev,
168 unsigned int func_selector,
169 unsigned int group_selector)
170 {
171 struct rzt2h_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
172 const struct function_desc *func;
173 struct group_desc *group;
174 const unsigned int *pins;
175 unsigned int i;
176 u8 *psel_val;
177 int ret;
178
179 func = pinmux_generic_get_function(pctldev, func_selector);
180 if (!func)
181 return -EINVAL;
182
183 group = pinctrl_generic_get_group(pctldev, group_selector);
184 if (!group)
185 return -EINVAL;
186
187 psel_val = func->data;
188 pins = group->grp.pins;
189
190 for (i = 0; i < group->grp.npins; i++) {
191 dev_dbg(pctrl->dev, "port:%u pin:%u PSEL:%u\n",
192 RZT2H_PIN_ID_TO_PORT(pins[i]), RZT2H_PIN_ID_TO_PIN(pins[i]),
193 psel_val[i]);
194 ret = rzt2h_validate_pin(pctrl, pins[i]);
195 if (ret)
196 return ret;
197
198 rzt2h_pinctrl_set_pfc_mode(pctrl, RZT2H_PIN_ID_TO_PORT(pins[i]),
199 RZT2H_PIN_ID_TO_PIN(pins[i]), psel_val[i]);
200 }
201
202 return 0;
203 }
204
205 static int rzt2h_map_add_config(struct pinctrl_map *map,
206 const char *group_or_pin,
207 enum pinctrl_map_type type,
208 unsigned long *configs,
209 unsigned int num_configs)
210 {
211 unsigned long *cfgs;
212
213 cfgs = kmemdup_array(configs, num_configs, sizeof(*cfgs), GFP_KERNEL);
214 if (!cfgs)
215 return -ENOMEM;
216
217 map->type = type;
218 map->data.configs.group_or_pin = group_or_pin;
219 map->data.configs.configs = cfgs;
220 map->data.configs.num_configs = num_configs;
221
222 return 0;
223 }
224
225 static int rzt2h_dt_subnode_to_map(struct pinctrl_dev *pctldev,
226 struct device_node *np,
227 struct device_node *parent,
228 struct pinctrl_map **map,
229 unsigned int *num_maps,
230 unsigned int *index)
231 {
232 struct rzt2h_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
233 struct pinctrl_map *maps = *map;
234 unsigned int nmaps = *num_maps;
235 unsigned long *configs = NULL;
236 unsigned int num_pinmux = 0;
237 unsigned int idx = *index;
238 unsigned int num_pins, i;
239 unsigned int num_configs;
240 struct property *pinmux;
241 struct property *prop;
242 int ret, gsel, fsel;
243 const char **pin_fn;
244 unsigned int *pins;
245 const char *name;
246 const char *pin;
247 u8 *psel_val;
248
249 pinmux = of_find_property(np, "pinmux", NULL);
250 if (pinmux)
251 num_pinmux = pinmux->length / sizeof(u32);
252
253 ret = of_property_count_strings(np, "pins");
254 if (ret == -EINVAL) {
255 num_pins = 0;
256 } else if (ret < 0) {
257 dev_err(pctrl->dev, "Invalid pins list in DT\n");
258 return ret;
259 } else {
260 num_pins = ret;
261 }
262
263 if (!num_pinmux && !num_pins)
264 return 0;
265
266 if (num_pinmux && num_pins) {
267 dev_err(pctrl->dev,
268 "DT node must contain either a pinmux or pins and not both\n");
269 return -EINVAL;
270 }
271
272 ret = pinconf_generic_parse_dt_config(np, pctldev, &configs, &num_configs);
273 if (ret < 0)
274 return ret;
275
276 if (num_pins && !num_configs) {
277 dev_err(pctrl->dev, "DT node must contain a config\n");
278 ret = -ENODEV;
279 goto done;
280 }
281
282 if (num_pinmux) {
283 nmaps += 1;
284 if (num_configs)
285 nmaps += 1;
286 }
287
288 if (num_pins)
289 nmaps += num_pins;
290
291 maps = krealloc_array(maps, nmaps, sizeof(*maps), GFP_KERNEL);
292 if (!maps) {
293 ret = -ENOMEM;
294 goto done;
295 }
296
297 *map = maps;
298 *num_maps = nmaps;
299 if (num_pins) {
300 of_property_for_each_string(np, "pins", prop, pin) {
301 ret = rzt2h_map_add_config(&maps[idx], pin,
302 PIN_MAP_TYPE_CONFIGS_PIN,
303 configs, num_configs);
304 if (ret < 0)
305 goto done;
306
307 idx++;
308 }
309 ret = 0;
310 goto done;
311 }
312
313 pins = devm_kcalloc(pctrl->dev, num_pinmux, sizeof(*pins), GFP_KERNEL);
314 psel_val = devm_kcalloc(pctrl->dev, num_pinmux, sizeof(*psel_val),
315 GFP_KERNEL);
316 pin_fn = devm_kzalloc(pctrl->dev, sizeof(*pin_fn), GFP_KERNEL);
317 if (!pins || !psel_val || !pin_fn) {
318 ret = -ENOMEM;
319 goto done;
320 }
321
322 /* Collect pin locations and mux settings from DT properties */
323 for (i = 0; i < num_pinmux; ++i) {
324 u32 value;
325
326 ret = of_property_read_u32_index(np, "pinmux", i, &value);
327 if (ret)
328 goto done;
329 pins[i] = FIELD_GET(MUX_PIN_ID_MASK, value);
330 psel_val[i] = FIELD_GET(MUX_FUNC_MASK, value);
331 }
332
333 if (parent) {
334 name = devm_kasprintf(pctrl->dev, GFP_KERNEL, "%pOFn.%pOFn",
335 parent, np);
336 if (!name) {
337 ret = -ENOMEM;
338 goto done;
339 }
340 } else {
341 name = np->name;
342 }
343
344 if (num_configs) {
345 ret = rzt2h_map_add_config(&maps[idx], name,
346 PIN_MAP_TYPE_CONFIGS_GROUP,
347 configs, num_configs);
348 if (ret < 0)
349 goto done;
350
351 idx++;
352 }
353
354 scoped_guard(mutex, &pctrl->mutex) {
355 /* Register a single pin group listing all the pins we read from DT */
356 gsel = pinctrl_generic_add_group(pctldev, name, pins, num_pinmux, NULL);
357 if (gsel < 0) {
358 ret = gsel;
359 goto done;
360 }
361
362 /*
363 * Register a single group function where the 'data' is an array PSEL
364 * register values read from DT.
365 */
366 pin_fn[0] = name;
367 fsel = pinmux_generic_add_function(pctldev, name, pin_fn, 1, psel_val);
368 if (fsel < 0) {
369 ret = fsel;
370 goto remove_group;
371 }
372 }
373
374 maps[idx].type = PIN_MAP_TYPE_MUX_GROUP;
375 maps[idx].data.mux.group = name;
376 maps[idx].data.mux.function = name;
377 idx++;
378
379 dev_dbg(pctrl->dev, "Parsed %pOF with %d pins\n", np, num_pinmux);
380 ret = 0;
381 goto done;
382
383 remove_group:
384 pinctrl_generic_remove_group(pctldev, gsel);
385 done:
386 *index = idx;
387 kfree(configs);
388 return ret;
389 }
390
391 static void rzt2h_dt_free_map(struct pinctrl_dev *pctldev,
392 struct pinctrl_map *map,
393 unsigned int num_maps)
394 {
395 unsigned int i;
396
397 if (!map)
398 return;
399
400 for (i = 0; i < num_maps; ++i) {
401 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP ||
402 map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
403 kfree(map[i].data.configs.configs);
404 }
405 kfree(map);
406 }
407
408 static int rzt2h_dt_node_to_map(struct pinctrl_dev *pctldev,
409 struct device_node *np,
410 struct pinctrl_map **map,
411 unsigned int *num_maps)
412 {
413 struct rzt2h_pinctrl *pctrl = pinctrl_dev_get_drvdata(pctldev);
414 unsigned int index;
415 int ret;
416
417 *map = NULL;
418 *num_maps = 0;
419 index = 0;
420
421 for_each_child_of_node_scoped(np, child) {
422 ret = rzt2h_dt_subnode_to_map(pctldev, child, np, map,
423 num_maps, &index);
424 if (ret < 0)
425 goto done;
426 }
427
428 if (*num_maps == 0) {
429 ret = rzt2h_dt_subnode_to_map(pctldev, np, NULL, map,
430 num_maps, &index);
431 if (ret < 0)
432 goto done;
433 }
434
435 if (*num_maps)
436 return 0;
437
438 dev_err(pctrl->dev, "no mapping found in node %pOF\n", np);
439 ret = -EINVAL;
440
441 done:
442 rzt2h_dt_free_map(pctldev, *map, *num_maps);
443 return ret;
444 }
445
446 static const struct pinctrl_ops rzt2h_pinctrl_pctlops = {
447 .get_groups_count = pinctrl_generic_get_group_count,
448 .get_group_name = pinctrl_generic_get_group_name,
449 .get_group_pins = pinctrl_generic_get_group_pins,
450 .dt_node_to_map = rzt2h_dt_node_to_map,
451 .dt_free_map = rzt2h_dt_free_map,
452 };
453
454 static const struct pinmux_ops rzt2h_pinctrl_pmxops = {
455 .get_functions_count = pinmux_generic_get_function_count,
456 .get_function_name = pinmux_generic_get_function_name,
457 .get_function_groups = pinmux_generic_get_function_groups,
458 .set_mux = rzt2h_pinctrl_set_mux,
459 .strict = true,
460 };
461
462 static int rzt2h_gpio_request(struct gpio_chip *chip, unsigned int offset)
463 {
464 struct rzt2h_pinctrl *pctrl = gpiochip_get_data(chip);
465 u8 port = RZT2H_PIN_ID_TO_PORT(offset);
466 u8 bit = RZT2H_PIN_ID_TO_PIN(offset);
467 int ret;
468
469 ret = rzt2h_validate_pin(pctrl, offset);
470 if (ret)
471 return ret;
472
473 ret = pinctrl_gpio_request(chip, offset);
474 if (ret)
475 return ret;
476
477 guard(raw_spinlock_irqsave)(&pctrl->lock);
478
479 /* Select GPIO mode in PMC Register */
480 rzt2h_pinctrl_set_gpio_en(pctrl, port, bit, true);
481
482 return 0;
483 }
484
485 static void rzt2h_gpio_set_direction(struct rzt2h_pinctrl *pctrl, u32 port,
486 u8 bit, bool output)
487 {
488 u16 reg;
489
490 guard(raw_spinlock_irqsave)(&pctrl->lock);
491
492 reg = rzt2h_pinctrl_readw(pctrl, port, PM(port));
493 reg &= ~PM_PIN_MASK(bit);
494
495 reg |= (output ? PM_OUTPUT : PM_INPUT) << (bit * 2);
496 rzt2h_pinctrl_writew(pctrl, port, reg, PM(port));
497 }
498
499 static int rzt2h_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
500 {
501 struct rzt2h_pinctrl *pctrl = gpiochip_get_data(chip);
502 u8 port = RZT2H_PIN_ID_TO_PORT(offset);
503 u8 bit = RZT2H_PIN_ID_TO_PIN(offset);
504 u64 reg64;
505 u16 reg;
506 int ret;
507
508 ret = rzt2h_validate_pin(pctrl, offset);
509 if (ret)
510 return ret;
511
512 guard(raw_spinlock_irqsave)(&pctrl->lock);
513
514 if (rzt2h_pinctrl_readb(pctrl, port, PMC(port)) & BIT(bit)) {
515 /*
516 * When a GPIO is being requested as an IRQ, the pinctrl
517 * framework expects to be able to read the GPIO's direction.
518 * IRQ function is separate from GPIO, and enabling it takes the
519 * pin out of GPIO mode.
520 * At this point, .child_to_parent_hwirq() has already been
521 * called to enable the IRQ function.
522 * Default to input direction for IRQ function.
523 */
524 reg64 = rzt2h_pinctrl_readq(pctrl, port, PFC(port));
525 reg64 = (reg64 >> (bit * 8)) & PFC_MASK;
526 if (reg64 == PFC_FUNC_INTERRUPT)
527 return GPIO_LINE_DIRECTION_IN;
528
529 return -EINVAL;
530 }
531
532 reg = rzt2h_pinctrl_readw(pctrl, port, PM(port));
533 reg = (reg >> (bit * 2)) & PM_MASK;
534 if (reg & PM_OUTPUT)
535 return GPIO_LINE_DIRECTION_OUT;
536 if (reg & PM_INPUT)
537 return GPIO_LINE_DIRECTION_IN;
538
539 return -EINVAL;
540 }
541
542 static int rzt2h_gpio_set(struct gpio_chip *chip, unsigned int offset,
543 int value)
544 {
545 struct rzt2h_pinctrl *pctrl = gpiochip_get_data(chip);
546 u8 port = RZT2H_PIN_ID_TO_PORT(offset);
547 u8 bit = RZT2H_PIN_ID_TO_PIN(offset);
548 u8 reg;
549
550 guard(raw_spinlock_irqsave)(&pctrl->lock);
551
552 reg = rzt2h_pinctrl_readb(pctrl, port, P(port));
553 if (value)
554 rzt2h_pinctrl_writeb(pctrl, port, reg | BIT(bit), P(port));
555 else
556 rzt2h_pinctrl_writeb(pctrl, port, reg & ~BIT(bit), P(port));
557
558 return 0;
559 }
560
561 static int rzt2h_gpio_get(struct gpio_chip *chip, unsigned int offset)
562 {
563 struct rzt2h_pinctrl *pctrl = gpiochip_get_data(chip);
564 u8 port = RZT2H_PIN_ID_TO_PORT(offset);
565 u8 bit = RZT2H_PIN_ID_TO_PIN(offset);
566 u16 reg;
567
568 reg = rzt2h_pinctrl_readw(pctrl, port, PM(port));
569 reg = (reg >> (bit * 2)) & PM_MASK;
570 if (reg & PM_INPUT)
571 return !!(rzt2h_pinctrl_readb(pctrl, port, PIN(port)) & BIT(bit));
572 if (reg & PM_OUTPUT)
573 return !!(rzt2h_pinctrl_readb(pctrl, port, P(port)) & BIT(bit));
574
575 return -EINVAL;
576 }
577
578 static int rzt2h_gpio_direction_input(struct gpio_chip *chip,
579 unsigned int offset)
580 {
581 struct rzt2h_pinctrl *pctrl = gpiochip_get_data(chip);
582 u8 port = RZT2H_PIN_ID_TO_PORT(offset);
583 u8 bit = RZT2H_PIN_ID_TO_PIN(offset);
584
585 rzt2h_gpio_set_direction(pctrl, port, bit, false);
586
587 return 0;
588 }
589
590 static int rzt2h_gpio_direction_output(struct gpio_chip *chip,
591 unsigned int offset, int value)
592 {
593 struct rzt2h_pinctrl *pctrl = gpiochip_get_data(chip);
594 u8 port = RZT2H_PIN_ID_TO_PORT(offset);
595 u8 bit = RZT2H_PIN_ID_TO_PIN(offset);
596
597 rzt2h_gpio_set(chip, offset, value);
598 rzt2h_gpio_set_direction(pctrl, port, bit, true);
599
600 return 0;
601 }
602
603 static void rzt2h_gpio_free(struct gpio_chip *chip, unsigned int offset)
604 {
605 pinctrl_gpio_free(chip, offset);
606
607 /*
608 * Set the GPIO as an input to ensure that the next GPIO request won't
609 * drive the GPIO pin as an output.
610 */
611 rzt2h_gpio_direction_input(chip, offset);
612 }
613
614 static const char * const rzt2h_gpio_names[] = {
615 "P00_0", "P00_1", "P00_2", "P00_3", "P00_4", "P00_5", "P00_6", "P00_7",
616 "P01_0", "P01_1", "P01_2", "P01_3", "P01_4", "P01_5", "P01_6", "P01_7",
617 "P02_0", "P02_1", "P02_2", "P02_3", "P02_4", "P02_5", "P02_6", "P02_7",
618 "P03_0", "P03_1", "P03_2", "P03_3", "P03_4", "P03_5", "P03_6", "P03_7",
619 "P04_0", "P04_1", "P04_2", "P04_3", "P04_4", "P04_5", "P04_6", "P04_7",
620 "P05_0", "P05_1", "P05_2", "P05_3", "P05_4", "P05_5", "P05_6", "P05_7",
621 "P06_0", "P06_1", "P06_2", "P06_3", "P06_4", "P06_5", "P06_6", "P06_7",
622 "P07_0", "P07_1", "P07_2", "P07_3", "P07_4", "P07_5", "P07_6", "P07_7",
623 "P08_0", "P08_1", "P08_2", "P08_3", "P08_4", "P08_5", "P08_6", "P08_7",
624 "P09_0", "P09_1", "P09_2", "P09_3", "P09_4", "P09_5", "P09_6", "P09_7",
625 "P10_0", "P10_1", "P10_2", "P10_3", "P10_4", "P10_5", "P10_6", "P10_7",
626 "P11_0", "P11_1", "P11_2", "P11_3", "P11_4", "P11_5", "P11_6", "P11_7",
627 "P12_0", "P12_1", "P12_2", "P12_3", "P12_4", "P12_5", "P12_6", "P12_7",
628 "P13_0", "P13_1", "P13_2", "P13_3", "P13_4", "P13_5", "P13_6", "P13_7",
629 "P14_0", "P14_1", "P14_2", "P14_3", "P14_4", "P14_5", "P14_6", "P14_7",
630 "P15_0", "P15_1", "P15_2", "P15_3", "P15_4", "P15_5", "P15_6", "P15_7",
631 "P16_0", "P16_1", "P16_2", "P16_3", "P16_4", "P16_5", "P16_6", "P16_7",
632 "P17_0", "P17_1", "P17_2", "P17_3", "P17_4", "P17_5", "P17_6", "P17_7",
633 "P18_0", "P18_1", "P18_2", "P18_3", "P18_4", "P18_5", "P18_6", "P18_7",
634 "P19_0", "P19_1", "P19_2", "P19_3", "P19_4", "P19_5", "P19_6", "P19_7",
635 "P20_0", "P20_1", "P20_2", "P20_3", "P20_4", "P20_5", "P20_6", "P20_7",
636 "P21_0", "P21_1", "P21_2", "P21_3", "P21_4", "P21_5", "P21_6", "P21_7",
637 "P22_0", "P22_1", "P22_2", "P22_3", "P22_4", "P22_5", "P22_6", "P22_7",
638 "P23_0", "P23_1", "P23_2", "P23_3", "P23_4", "P23_5", "P23_6", "P23_7",
639 "P24_0", "P24_1", "P24_2", "P24_3", "P24_4", "P24_5", "P24_6", "P24_7",
640 "P25_0", "P25_1", "P25_2", "P25_3", "P25_4", "P25_5", "P25_6", "P25_7",
641 "P26_0", "P26_1", "P26_2", "P26_3", "P26_4", "P26_5", "P26_6", "P26_7",
642 "P27_0", "P27_1", "P27_2", "P27_3", "P27_4", "P27_5", "P27_6", "P27_7",
643 "P28_0", "P28_1", "P28_2", "P28_3", "P28_4", "P28_5", "P28_6", "P28_7",
644 "P29_0", "P29_1", "P29_2", "P29_3", "P29_4", "P29_5", "P29_6", "P29_7",
645 "P30_0", "P30_1", "P30_2", "P30_3", "P30_4", "P30_5", "P30_6", "P30_7",
646 "P31_0", "P31_1", "P31_2", "P31_3", "P31_4", "P31_5", "P31_6", "P31_7",
647 "P32_0", "P32_1", "P32_2", "P32_3", "P32_4", "P32_5", "P32_6", "P32_7",
648 "P33_0", "P33_1", "P33_2", "P33_3", "P33_4", "P33_5", "P33_6", "P33_7",
649 "P34_0", "P34_1", "P34_2", "P34_3", "P34_4", "P34_5", "P34_6", "P34_7",
650 "P35_0", "P35_1", "P35_2", "P35_3", "P35_4", "P35_5", "P35_6", "P35_7",
651 };
652
653 /*
654 * Interrupts 0-15 are for INTCPUn, which are not exposed externally.
655 * Interrupts 16-31 are for IRQn. SEI is 32.
656 * This table matches the information found in User Manual's Section
657 * 17.5, Multiplexed Pin Configurations, Tables 17.5 to 17.40, on the
658 * Interrupt rows.
659 * RZ/N2H has the same GPIO to IRQ mapping, except for the pins which
660 * are not present.
661 */
662 static const u8 rzt2h_gpio_irq_map[] = {
663 32, 16, 17, 18, 19, 0, 20, 21,
664 22, 0, 0, 0, 0, 0, 0, 0,
665 23, 24, 25, 26, 27, 0, 0, 0,
666 0, 0, 28, 29, 30, 31, 0, 0,
667 0, 0, 0, 0, 0, 32, 16, 17,
668 18, 19, 20, 21, 22, 0, 0, 0,
669 0, 0, 24, 25, 26, 27, 0, 28,
670 29, 30, 31, 0, 0, 0, 0, 0,
671 0, 0, 0, 0, 0, 24, 32, 16,
672 0, 0, 0, 0, 0, 0, 0, 0,
673 20, 23, 17, 18, 19, 0, 16, 25,
674 29, 20, 21, 22, 23, 0, 0, 0,
675 0, 0, 0, 0, 17, 0, 0, 18,
676 0, 0, 19, 0, 0, 20, 0, 30,
677 21, 0, 0, 22, 0, 0, 24, 25,
678 0, 0, 0, 0, 0, 16, 17, 0,
679 18, 0, 0, 26, 27, 0, 0, 0,
680 28, 29, 30, 31, 0, 0, 0, 0,
681 23, 31, 32, 16, 17, 18, 19, 20,
682 0, 0, 0, 0, 0, 0, 0, 0,
683 0, 0, 0, 0, 0, 0, 0, 0,
684 0, 0, 0, 0, 0, 0, 0, 0,
685 27, 0, 0, 21, 22, 23, 24, 25,
686 26, 0, 0, 0, 0, 0, 0, 0,
687 27, 28, 29, 30, 31, 0, 0, 0,
688 0, 0, 0, 0, 0, 0, 0, 0,
689 0, 0, 0, 0, 0, 28, 32, 16,
690 17, 18, 19, 0, 0, 0, 0, 20,
691 21, 22, 23, 0, 0, 0, 0, 0,
692 0, 0, 0, 0, 24, 25, 0, 0,
693 0, 0, 26, 27, 0, 0, 0, 30,
694 0, 29, 0, 0, 0, 0, 0, 0,
695 0, 0, 0, 0, 0, 0, 0, 0,
696 0, 0, 0, 28, 29, 30, 31, 0,
697 0, 0, 0, 0, 0, 0, 0, 30,
698 0, 0, 0, 0, 0, 0, 0, 0,
699 };
700
701 static void rzt2h_gpio_irq_disable(struct irq_data *d)
702 {
703 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
704 unsigned int hwirq = irqd_to_hwirq(d);
705
706 irq_chip_disable_parent(d);
707 gpiochip_disable_irq(gc, hwirq);
708 }
709
710 static void rzt2h_gpio_irq_enable(struct irq_data *d)
711 {
712 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
713 unsigned int hwirq = irqd_to_hwirq(d);
714
715 gpiochip_enable_irq(gc, hwirq);
716 irq_chip_enable_parent(d);
717 }
718
719 static int rzt2h_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
720 {
721 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
722 struct rzt2h_pinctrl *pctrl = container_of(gc, struct rzt2h_pinctrl, gpio_chip);
723 int ret;
724
725 ret = irq_chip_set_wake_parent(d, on);
726 if (ret)
727 return ret;
728
729 /*
730 * If any of the IRQs are in use, put the entire pin controller on the
731 * device wakeup path.
732 */
733 if (on)
734 atomic_inc(&pctrl->wakeup_path);
735 else
736 atomic_dec(&pctrl->wakeup_path);
737
738 return 0;
739 }
740
741 static const struct irq_chip rzt2h_gpio_irqchip = {
742 .name = "rzt2h-gpio",
743 .irq_disable = rzt2h_gpio_irq_disable,
744 .irq_enable = rzt2h_gpio_irq_enable,
745 .irq_mask = irq_chip_mask_parent,
746 .irq_unmask = irq_chip_unmask_parent,
747 .irq_set_type = irq_chip_set_type_parent,
748 .irq_set_wake = rzt2h_gpio_irq_set_wake,
749 .irq_eoi = irq_chip_eoi_parent,
750 .irq_set_affinity = irq_chip_set_affinity_parent,
751 .flags = IRQCHIP_IMMUTABLE,
752 GPIOCHIP_IRQ_RESOURCE_HELPERS,
753 };
754
755 static int rzt2h_gpio_child_to_parent_hwirq(struct gpio_chip *gc,
756 unsigned int child,
757 unsigned int child_type,
758 unsigned int *parent,
759 unsigned int *parent_type)
760 {
761 struct rzt2h_pinctrl *pctrl = gpiochip_get_data(gc);
762 u8 port = RZT2H_PIN_ID_TO_PORT(child);
763 u8 pin = RZT2H_PIN_ID_TO_PIN(child);
764 u8 parent_irq;
765
766 parent_irq = rzt2h_gpio_irq_map[child];
767 if (parent_irq < RZT2H_INTERRUPTS_START)
768 return -EINVAL;
769
770 if (test_and_set_bit(parent_irq - RZT2H_INTERRUPTS_START,
771 pctrl->used_irqs))
772 return -EBUSY;
773
774 rzt2h_pinctrl_set_pfc_mode(pctrl, port, pin, PFC_FUNC_INTERRUPT);
775
776 *parent = parent_irq;
777 *parent_type = child_type;
778
779 return 0;
780 }
781
782 static void rzt2h_gpio_irq_domain_free(struct irq_domain *domain, unsigned int virq,
783 unsigned int nr_irqs)
784 {
785 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
786 struct gpio_chip *gc = irq_data_get_irq_chip_data(d);
787 struct rzt2h_pinctrl *pctrl = container_of(gc, struct rzt2h_pinctrl, gpio_chip);
788 irq_hw_number_t hwirq = irqd_to_hwirq(d);
789 u8 port = RZT2H_PIN_ID_TO_PORT(hwirq);
790 u8 pin = RZT2H_PIN_ID_TO_PIN(hwirq);
791
792 if (test_and_clear_bit(hwirq - RZT2H_INTERRUPTS_START, pctrl->used_irqs))
793 rzt2h_pinctrl_set_gpio_en(pctrl, port, pin, false);
794
795 irq_domain_free_irqs_common(domain, virq, nr_irqs);
796 }
797
798 static void rzt2h_gpio_init_irq_valid_mask(struct gpio_chip *gc,
799 unsigned long *valid_mask,
800 unsigned int ngpios)
801 {
802 struct rzt2h_pinctrl *pctrl = gpiochip_get_data(gc);
803 unsigned int offset;
804
805 for (offset = 0; offset < ngpios; offset++) {
806 if (!rzt2h_gpio_irq_map[offset] || rzt2h_validate_pin(pctrl, offset))
807 clear_bit(offset, valid_mask);
808 }
809 }
810
811 static int rzt2h_gpio_register(struct rzt2h_pinctrl *pctrl)
812 {
813 struct pinctrl_gpio_range *range = &pctrl->gpio_range;
814 struct gpio_chip *chip = &pctrl->gpio_chip;
815 struct device_node *np = pctrl->dev->of_node;
816 struct irq_domain *parent_domain;
817 struct device *dev = pctrl->dev;
818 struct of_phandle_args of_args;
819 struct device_node *parent_np;
820 struct gpio_irq_chip *girq;
821 int ret;
822
823 parent_np = of_irq_find_parent(np);
824 if (!parent_np)
825 return -ENXIO;
826
827 parent_domain = irq_find_host(parent_np);
828 of_node_put(parent_np);
829 if (!parent_domain)
830 return -EPROBE_DEFER;
831
832 ret = of_parse_phandle_with_fixed_args(dev->of_node, "gpio-ranges", 3, 0, &of_args);
833 if (ret)
834 return dev_err_probe(dev, ret, "Unable to parse gpio-ranges\n");
835
836 of_node_put(of_args.np);
837 if (of_args.args[0] != 0 || of_args.args[1] != 0 ||
838 of_args.args[2] != pctrl->data->n_port_pins)
839 return dev_err_probe(dev, -EINVAL,
840 "gpio-ranges does not match selected SOC\n");
841
842 chip->base = -1;
843 chip->parent = dev;
844 chip->owner = THIS_MODULE;
845 chip->ngpio = of_args.args[2];
846 chip->names = rzt2h_gpio_names;
847 chip->request = rzt2h_gpio_request;
848 chip->free = rzt2h_gpio_free;
849 chip->get_direction = rzt2h_gpio_get_direction;
850 chip->direction_input = rzt2h_gpio_direction_input;
851 chip->direction_output = rzt2h_gpio_direction_output;
852 chip->get = rzt2h_gpio_get;
853 chip->set = rzt2h_gpio_set;
854 chip->label = dev_name(dev);
855
856 if (of_property_present(np, "interrupt-controller")) {
857 girq = &chip->irq;
858 gpio_irq_chip_set_chip(girq, &rzt2h_gpio_irqchip);
859 girq->fwnode = dev_fwnode(pctrl->dev);
860 girq->parent_domain = parent_domain;
861 girq->child_to_parent_hwirq = rzt2h_gpio_child_to_parent_hwirq;
862 girq->populate_parent_alloc_arg = gpiochip_populate_parent_fwspec_twocell;
863 girq->child_irq_domain_ops.free = rzt2h_gpio_irq_domain_free;
864 girq->init_valid_mask = rzt2h_gpio_init_irq_valid_mask;
865 }
866
867 range->id = 0;
868 range->pin_base = 0;
869 range->base = 0;
870 range->npins = chip->ngpio;
871 range->name = chip->label;
872 range->gc = chip;
873
874 ret = devm_gpiochip_add_data(dev, chip, pctrl);
875 if (ret)
876 return dev_err_probe(dev, ret, "gpiochip registration failed\n");
877
878 return ret;
879 }
880
881 static int rzt2h_pinctrl_register(struct rzt2h_pinctrl *pctrl)
882 {
883 struct pinctrl_desc *desc = &pctrl->desc;
884 struct device *dev = pctrl->dev;
885 struct pinctrl_pin_desc *pins;
886 unsigned int i, j;
887 int ret;
888
889 desc->name = DRV_NAME;
890 desc->npins = pctrl->data->n_port_pins;
891 desc->pctlops = &rzt2h_pinctrl_pctlops;
892 desc->pmxops = &rzt2h_pinctrl_pmxops;
893 desc->owner = THIS_MODULE;
894
895 pins = devm_kcalloc(dev, desc->npins, sizeof(*pins), GFP_KERNEL);
896 if (!pins)
897 return -ENOMEM;
898
899 pctrl->pins = pins;
900 desc->pins = pins;
901
902 for (i = 0, j = 0; i < pctrl->data->n_port_pins; i++) {
903 pins[i].number = i;
904 pins[i].name = rzt2h_gpio_names[i];
905 if (i && !(i % RZT2H_PINS_PER_PORT))
906 j++;
907 }
908
909 ret = devm_pinctrl_register_and_init(dev, desc, pctrl, &pctrl->pctl);
910 if (ret)
911 return dev_err_probe(dev, ret, "pinctrl registration failed\n");
912
913 ret = pinctrl_enable(pctrl->pctl);
914 if (ret)
915 return dev_err_probe(dev, ret, "pinctrl enable failed\n");
916
917 return rzt2h_gpio_register(pctrl);
918 }
919
920 static int rzt2h_pinctrl_cfg_regions(struct platform_device *pdev,
921 struct rzt2h_pinctrl *pctrl)
922 {
923 struct resource *res;
924
925 pctrl->base0 = devm_platform_ioremap_resource_byname(pdev, "nsr");
926 if (IS_ERR(pctrl->base0))
927 return PTR_ERR(pctrl->base0);
928
929 /*
930 * Open-coded instead of using devm_platform_ioremap_resource_byname()
931 * because the "srs" region is optional.
932 */
933 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "srs");
934 if (res) {
935 u8 port;
936
937 pctrl->base1 = devm_ioremap_resource(&pdev->dev, res);
938 if (IS_ERR(pctrl->base1))
939 return PTR_ERR(pctrl->base1);
940
941 pctrl->safety_port_enabled = true;
942
943 /* Configure to select safety region 0x812c0xxx */
944 for (port = 0; port <= RZT2H_MAX_SAFETY_PORTS; port++)
945 writeb(0x0, pctrl->base1 + RSELP(port));
946 }
947
948 return 0;
949 }
950
951 static int rzt2h_pinctrl_probe(struct platform_device *pdev)
952 {
953 struct device *dev = &pdev->dev;
954 struct rzt2h_pinctrl *pctrl;
955 int ret;
956
957 pctrl = devm_kzalloc(dev, sizeof(*pctrl), GFP_KERNEL);
958 if (!pctrl)
959 return -ENOMEM;
960
961 pctrl->dev = dev;
962 pctrl->data = of_device_get_match_data(dev);
963
964 ret = rzt2h_pinctrl_cfg_regions(pdev, pctrl);
965 if (ret)
966 return ret;
967
968 raw_spin_lock_init(&pctrl->lock);
969 mutex_init(&pctrl->mutex);
970 platform_set_drvdata(pdev, pctrl);
971
972 return rzt2h_pinctrl_register(pctrl);
973 }
974
975 static const u8 r9a09g077_gpio_configs[] = {
976 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
977 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
978 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f,
979 };
980
981 static const u8 r9a09g087_gpio_configs[] = {
982 0x1f, 0xff, 0xff, 0x1f, 0x00, 0xfe, 0xff, 0x00, 0x7e, 0xf0, 0xff, 0x01,
983 0xff, 0xff, 0xff, 0x00, 0xe0, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff, 0x01,
984 0xe0, 0xff, 0xff, 0x7f, 0x00, 0xfe, 0xff, 0x7f, 0x00, 0xfc, 0x7f,
985 };
986
987 static struct rzt2h_pinctrl_data r9a09g077_data = {
988 .n_port_pins = ARRAY_SIZE(r9a09g077_gpio_configs) * RZT2H_PINS_PER_PORT,
989 .port_pin_configs = r9a09g077_gpio_configs,
990 .n_ports = ARRAY_SIZE(r9a09g077_gpio_configs),
991 };
992
993 static struct rzt2h_pinctrl_data r9a09g087_data = {
994 .n_port_pins = ARRAY_SIZE(r9a09g087_gpio_configs) * RZT2H_PINS_PER_PORT,
995 .port_pin_configs = r9a09g087_gpio_configs,
996 .n_ports = ARRAY_SIZE(r9a09g087_gpio_configs),
997 };
998
999 static const struct of_device_id rzt2h_pinctrl_of_table[] = {
1000 {
1001 .compatible = "renesas,r9a09g077-pinctrl",
1002 .data = &r9a09g077_data,
1003 },
1004 {
1005 .compatible = "renesas,r9a09g087-pinctrl",
1006 .data = &r9a09g087_data,
1007 },
1008 { /* sentinel */ }
1009 };
1010
1011 static int rzt2h_pinctrl_suspend_noirq(struct device *dev)
1012 {
1013 struct rzt2h_pinctrl *pctrl = dev_get_drvdata(dev);
1014
1015 if (atomic_read(&pctrl->wakeup_path))
1016 device_set_wakeup_path(dev);
1017
1018 return 0;
1019 }
1020
1021 static const struct dev_pm_ops rzt2h_pinctrl_pm_ops = {
1022 NOIRQ_SYSTEM_SLEEP_PM_OPS(rzt2h_pinctrl_suspend_noirq, NULL)
1023 };
1024
1025 static struct platform_driver rzt2h_pinctrl_driver = {
1026 .driver = {
1027 .name = DRV_NAME,
1028 .of_match_table = of_match_ptr(rzt2h_pinctrl_of_table),
1029 .pm = pm_sleep_ptr(&rzt2h_pinctrl_pm_ops),
1030 .suppress_bind_attrs = true,
1031 },
1032 .probe = rzt2h_pinctrl_probe,
1033 };
1034
1035 static int __init rzt2h_pinctrl_init(void)
1036 {
1037 return platform_driver_register(&rzt2h_pinctrl_driver);
1038 }
1039 core_initcall(rzt2h_pinctrl_init);
1040
1041 MODULE_LICENSE("GPL");
1042 MODULE_AUTHOR("Thierry Bultel <[email protected]>");
1043 MODULE_AUTHOR("Lad Prabhakar <[email protected]>");
1044 MODULE_DESCRIPTION("Pin and gpio controller driver for the RZ/T2H family");