2 * Combined GPIO and pin controller support for Renesas RZ/A1 (r7s72100) SoC
4 * Copyright (C) 2017 Jacopo Mondi
6 * This file is licensed under the terms of the GNU General Public
7 * License version 2. This program is licensed "as is" without any
8 * warranty of any kind, whether express or implied.
12 * This pin controller/gpio combined driver supports Renesas devices of RZ/A1
14 * This includes SoCs which are sub- or super- sets of this particular line,
15 * as RZ/A1H (r7s721000), RZ/A1M (r7s721010) and RZ/A1L (r7s721020).
18 #include <linux/bitops.h>
19 #include <linux/err.h>
20 #include <linux/gpio/driver.h>
21 #include <linux/init.h>
22 #include <linux/ioport.h>
23 #include <linux/module.h>
25 #include <linux/of_address.h>
26 #include <linux/of_device.h>
27 #include <linux/pinctrl/pinconf-generic.h>
28 #include <linux/pinctrl/pinctrl.h>
29 #include <linux/pinctrl/pinmux.h>
30 #include <linux/slab.h>
33 #include "devicetree.h"
37 #define DRIVER_NAME "pinctrl-rza1"
39 #define RZA1_P_REG 0x0000
40 #define RZA1_PPR_REG 0x0200
41 #define RZA1_PM_REG 0x0300
42 #define RZA1_PMC_REG 0x0400
43 #define RZA1_PFC_REG 0x0500
44 #define RZA1_PFCE_REG 0x0600
45 #define RZA1_PFCEA_REG 0x0a00
46 #define RZA1_PIBC_REG 0x4000
47 #define RZA1_PBDC_REG 0x4100
48 #define RZA1_PIPC_REG 0x4200
50 #define RZA1_ADDR(mem, reg, port) ((mem) + (reg) + ((port) * 4))
52 #define RZA1_NPORTS 12
53 #define RZA1_PINS_PER_PORT 16
54 #define RZA1_NPINS (RZA1_PINS_PER_PORT * RZA1_NPORTS)
55 #define RZA1_PIN_ID_TO_PORT(id) ((id) / RZA1_PINS_PER_PORT)
56 #define RZA1_PIN_ID_TO_PIN(id) ((id) % RZA1_PINS_PER_PORT)
59 * Use 16 lower bits [15:0] for pin identifier
60 * Use 16 higher bits [31:16] for pin mux function
62 #define MUX_PIN_ID_MASK GENMASK(15, 0)
63 #define MUX_FUNC_MASK GENMASK(31, 16)
65 #define MUX_FUNC_OFFS 16
66 #define MUX_FUNC(pinconf) \
67 ((pinconf & MUX_FUNC_MASK) >> MUX_FUNC_OFFS)
68 #define MUX_FUNC_PFC_MASK BIT(0)
69 #define MUX_FUNC_PFCE_MASK BIT(1)
70 #define MUX_FUNC_PFCEA_MASK BIT(2)
73 #define MUX_FLAGS_BIDIR BIT(0)
74 #define MUX_FLAGS_SWIO_INPUT BIT(1)
75 #define MUX_FLAGS_SWIO_OUTPUT BIT(2)
77 /* ----------------------------------------------------------------------------
82 * rza1_bidir_pin - describe a single pin that needs bidir flag applied.
84 struct rza1_bidir_pin
{
90 * rza1_bidir_entry - describe a list of pins that needs bidir flag applied.
91 * Each struct rza1_bidir_entry describes a port.
93 struct rza1_bidir_entry
{
94 const unsigned int npins
;
95 const struct rza1_bidir_pin
*pins
;
99 * rza1_swio_pin - describe a single pin that needs bidir flag applied.
101 struct rza1_swio_pin
{
109 * rza1_swio_entry - describe a list of pins that needs swio flag applied
111 struct rza1_swio_entry
{
112 const unsigned int npins
;
113 const struct rza1_swio_pin
*pins
;
117 * rza1_pinmux_conf - group together bidir and swio pinmux flag tables
119 struct rza1_pinmux_conf
{
120 const struct rza1_bidir_entry
*bidir_entries
;
121 const struct rza1_swio_entry
*swio_entries
;
124 /* ----------------------------------------------------------------------------
125 * RZ/A1H (r7s72100) pinmux flags
128 static const struct rza1_bidir_pin rza1h_bidir_pins_p1
[] = {
129 { .pin
= 0, .func
= 1 },
130 { .pin
= 1, .func
= 1 },
131 { .pin
= 2, .func
= 1 },
132 { .pin
= 3, .func
= 1 },
133 { .pin
= 4, .func
= 1 },
134 { .pin
= 5, .func
= 1 },
135 { .pin
= 6, .func
= 1 },
136 { .pin
= 7, .func
= 1 },
139 static const struct rza1_bidir_pin rza1h_bidir_pins_p2
[] = {
140 { .pin
= 0, .func
= 1 },
141 { .pin
= 1, .func
= 1 },
142 { .pin
= 2, .func
= 1 },
143 { .pin
= 3, .func
= 1 },
144 { .pin
= 4, .func
= 1 },
145 { .pin
= 0, .func
= 4 },
146 { .pin
= 1, .func
= 4 },
147 { .pin
= 2, .func
= 4 },
148 { .pin
= 3, .func
= 4 },
149 { .pin
= 5, .func
= 1 },
150 { .pin
= 6, .func
= 1 },
151 { .pin
= 7, .func
= 1 },
152 { .pin
= 8, .func
= 1 },
153 { .pin
= 9, .func
= 1 },
154 { .pin
= 10, .func
= 1 },
155 { .pin
= 11, .func
= 1 },
156 { .pin
= 12, .func
= 1 },
157 { .pin
= 13, .func
= 1 },
158 { .pin
= 14, .func
= 1 },
159 { .pin
= 15, .func
= 1 },
160 { .pin
= 12, .func
= 4 },
161 { .pin
= 13, .func
= 4 },
162 { .pin
= 14, .func
= 4 },
163 { .pin
= 15, .func
= 4 },
166 static const struct rza1_bidir_pin rza1h_bidir_pins_p3
[] = {
167 { .pin
= 3, .func
= 2 },
168 { .pin
= 10, .func
= 7 },
169 { .pin
= 11, .func
= 7 },
170 { .pin
= 13, .func
= 7 },
171 { .pin
= 14, .func
= 7 },
172 { .pin
= 15, .func
= 7 },
173 { .pin
= 10, .func
= 8 },
174 { .pin
= 11, .func
= 8 },
175 { .pin
= 13, .func
= 8 },
176 { .pin
= 14, .func
= 8 },
177 { .pin
= 15, .func
= 8 },
180 static const struct rza1_bidir_pin rza1h_bidir_pins_p4
[] = {
181 { .pin
= 0, .func
= 8 },
182 { .pin
= 1, .func
= 8 },
183 { .pin
= 2, .func
= 8 },
184 { .pin
= 3, .func
= 8 },
185 { .pin
= 10, .func
= 3 },
186 { .pin
= 11, .func
= 3 },
187 { .pin
= 13, .func
= 3 },
188 { .pin
= 14, .func
= 3 },
189 { .pin
= 15, .func
= 3 },
190 { .pin
= 10, .func
= 4 },
191 { .pin
= 11, .func
= 4 },
192 { .pin
= 13, .func
= 4 },
193 { .pin
= 14, .func
= 4 },
194 { .pin
= 15, .func
= 4 },
195 { .pin
= 12, .func
= 5 },
196 { .pin
= 13, .func
= 5 },
197 { .pin
= 14, .func
= 5 },
198 { .pin
= 15, .func
= 5 },
201 static const struct rza1_bidir_pin rza1h_bidir_pins_p6
[] = {
202 { .pin
= 0, .func
= 1 },
203 { .pin
= 1, .func
= 1 },
204 { .pin
= 2, .func
= 1 },
205 { .pin
= 3, .func
= 1 },
206 { .pin
= 4, .func
= 1 },
207 { .pin
= 5, .func
= 1 },
208 { .pin
= 6, .func
= 1 },
209 { .pin
= 7, .func
= 1 },
210 { .pin
= 8, .func
= 1 },
211 { .pin
= 9, .func
= 1 },
212 { .pin
= 10, .func
= 1 },
213 { .pin
= 11, .func
= 1 },
214 { .pin
= 12, .func
= 1 },
215 { .pin
= 13, .func
= 1 },
216 { .pin
= 14, .func
= 1 },
217 { .pin
= 15, .func
= 1 },
220 static const struct rza1_bidir_pin rza1h_bidir_pins_p7
[] = {
221 { .pin
= 13, .func
= 3 },
224 static const struct rza1_bidir_pin rza1h_bidir_pins_p8
[] = {
225 { .pin
= 8, .func
= 3 },
226 { .pin
= 9, .func
= 3 },
227 { .pin
= 10, .func
= 3 },
228 { .pin
= 11, .func
= 3 },
229 { .pin
= 14, .func
= 2 },
230 { .pin
= 15, .func
= 2 },
231 { .pin
= 14, .func
= 3 },
232 { .pin
= 15, .func
= 3 },
235 static const struct rza1_bidir_pin rza1h_bidir_pins_p9
[] = {
236 { .pin
= 0, .func
= 2 },
237 { .pin
= 1, .func
= 2 },
238 { .pin
= 4, .func
= 2 },
239 { .pin
= 5, .func
= 2 },
240 { .pin
= 6, .func
= 2 },
241 { .pin
= 7, .func
= 2 },
244 static const struct rza1_bidir_pin rza1h_bidir_pins_p11
[] = {
245 { .pin
= 6, .func
= 2 },
246 { .pin
= 7, .func
= 2 },
247 { .pin
= 9, .func
= 2 },
248 { .pin
= 6, .func
= 4 },
249 { .pin
= 7, .func
= 4 },
250 { .pin
= 9, .func
= 4 },
251 { .pin
= 10, .func
= 2 },
252 { .pin
= 11, .func
= 2 },
253 { .pin
= 10, .func
= 4 },
254 { .pin
= 11, .func
= 4 },
255 { .pin
= 12, .func
= 4 },
256 { .pin
= 13, .func
= 4 },
257 { .pin
= 14, .func
= 4 },
258 { .pin
= 15, .func
= 4 },
261 static const struct rza1_swio_pin rza1h_swio_pins
[] = {
262 { .port
= 2, .pin
= 7, .func
= 4, .input
= 0 },
263 { .port
= 2, .pin
= 11, .func
= 4, .input
= 0 },
264 { .port
= 3, .pin
= 7, .func
= 3, .input
= 0 },
265 { .port
= 3, .pin
= 7, .func
= 8, .input
= 0 },
266 { .port
= 4, .pin
= 7, .func
= 5, .input
= 0 },
267 { .port
= 4, .pin
= 7, .func
= 11, .input
= 0 },
268 { .port
= 4, .pin
= 15, .func
= 6, .input
= 0 },
269 { .port
= 5, .pin
= 0, .func
= 1, .input
= 1 },
270 { .port
= 5, .pin
= 1, .func
= 1, .input
= 1 },
271 { .port
= 5, .pin
= 2, .func
= 1, .input
= 1 },
272 { .port
= 5, .pin
= 3, .func
= 1, .input
= 1 },
273 { .port
= 5, .pin
= 4, .func
= 1, .input
= 1 },
274 { .port
= 5, .pin
= 5, .func
= 1, .input
= 1 },
275 { .port
= 5, .pin
= 6, .func
= 1, .input
= 1 },
276 { .port
= 5, .pin
= 7, .func
= 1, .input
= 1 },
277 { .port
= 7, .pin
= 4, .func
= 6, .input
= 0 },
278 { .port
= 7, .pin
= 11, .func
= 2, .input
= 0 },
279 { .port
= 8, .pin
= 10, .func
= 8, .input
= 0 },
280 { .port
= 10, .pin
= 15, .func
= 2, .input
= 0 },
283 static const struct rza1_bidir_entry rza1h_bidir_entries
[RZA1_NPORTS
] = {
284 [1] = { ARRAY_SIZE(rza1h_bidir_pins_p1
), rza1h_bidir_pins_p1
},
285 [2] = { ARRAY_SIZE(rza1h_bidir_pins_p2
), rza1h_bidir_pins_p2
},
286 [3] = { ARRAY_SIZE(rza1h_bidir_pins_p3
), rza1h_bidir_pins_p3
},
287 [4] = { ARRAY_SIZE(rza1h_bidir_pins_p4
), rza1h_bidir_pins_p4
},
288 [6] = { ARRAY_SIZE(rza1h_bidir_pins_p6
), rza1h_bidir_pins_p6
},
289 [7] = { ARRAY_SIZE(rza1h_bidir_pins_p7
), rza1h_bidir_pins_p7
},
290 [8] = { ARRAY_SIZE(rza1h_bidir_pins_p8
), rza1h_bidir_pins_p8
},
291 [9] = { ARRAY_SIZE(rza1h_bidir_pins_p9
), rza1h_bidir_pins_p9
},
292 [11] = { ARRAY_SIZE(rza1h_bidir_pins_p11
), rza1h_bidir_pins_p11
},
295 static const struct rza1_swio_entry rza1h_swio_entries
[] = {
296 [0] = { ARRAY_SIZE(rza1h_swio_pins
), rza1h_swio_pins
},
299 /* RZ/A1H (r7s72100x) pinmux flags table */
300 static const struct rza1_pinmux_conf rza1h_pmx_conf
= {
301 .bidir_entries
= rza1h_bidir_entries
,
302 .swio_entries
= rza1h_swio_entries
,
305 /* ----------------------------------------------------------------------------
309 * rza1_mux_conf - describes a pin multiplexing operation
311 * @id: the pin identifier from 0 to RZA1_NPINS
312 * @port: the port where pin sits on
314 * @mux_func: alternate function id number
315 * @mux_flags: alternate function flags
316 * @value: output value to set the pin to
318 struct rza1_mux_conf
{
328 * rza1_port - describes a pin port
330 * This is mostly useful to lock register writes per-bank and not globally.
332 * @lock: protect access to HW registers
334 * @base: logical address base
335 * @pins: pins sitting on this port
341 struct pinctrl_pin_desc
*pins
;
345 * rza1_pinctrl - RZ pincontroller device
347 * @dev: parent device structure
348 * @mutex: protect [pinctrl|pinmux]_generic functions
349 * @base: logical address base
350 * @nports: number of pin controller ports
351 * @ports: pin controller banks
352 * @pins: pin array for pinctrl core
353 * @desc: pincontroller desc for pinctrl core
354 * @pctl: pinctrl device
355 * @data: device specific data
357 struct rza1_pinctrl
{
365 struct rza1_port
*ports
;
367 struct pinctrl_pin_desc
*pins
;
368 struct pinctrl_desc desc
;
369 struct pinctrl_dev
*pctl
;
374 /* ----------------------------------------------------------------------------
377 static inline bool rza1_pinmux_get_bidir(unsigned int port
,
380 const struct rza1_bidir_entry
*table
)
382 const struct rza1_bidir_entry
*entry
= &table
[port
];
383 const struct rza1_bidir_pin
*bidir_pin
;
386 for (i
= 0; i
< entry
->npins
; ++i
) {
387 bidir_pin
= &entry
->pins
[i
];
388 if (bidir_pin
->pin
== pin
&& bidir_pin
->func
== func
)
395 static inline int rza1_pinmux_get_swio(unsigned int port
,
398 const struct rza1_swio_entry
*table
)
400 const struct rza1_swio_pin
*swio_pin
;
404 for (i
= 0; i
< table
->npins
; ++i
) {
405 swio_pin
= &table
->pins
[i
];
406 if (swio_pin
->port
== port
&& swio_pin
->pin
== pin
&&
407 swio_pin
->func
== func
)
408 return swio_pin
->input
;
415 * rza1_pinmux_get_flags() - return pinmux flags associated to a pin
417 static unsigned int rza1_pinmux_get_flags(unsigned int port
, unsigned int pin
,
419 struct rza1_pinctrl
*rza1_pctl
)
422 const struct rza1_pinmux_conf
*pmx_conf
= rza1_pctl
->data
;
423 const struct rza1_bidir_entry
*bidir_entries
= pmx_conf
->bidir_entries
;
424 const struct rza1_swio_entry
*swio_entries
= pmx_conf
->swio_entries
;
425 unsigned int pmx_flags
= 0;
428 if (rza1_pinmux_get_bidir(port
, pin
, func
, bidir_entries
))
429 pmx_flags
|= MUX_FLAGS_BIDIR
;
431 ret
= rza1_pinmux_get_swio(port
, pin
, func
, swio_entries
);
433 pmx_flags
|= MUX_FLAGS_SWIO_OUTPUT
;
435 pmx_flags
|= MUX_FLAGS_SWIO_INPUT
;
440 /* ----------------------------------------------------------------------------
441 * RZ/A1 SoC operations
445 * rza1_set_bit() - un-locked set/clear a single bit in pin configuration
448 static inline void rza1_set_bit(struct rza1_port
*port
, unsigned int reg
,
449 unsigned int bit
, bool set
)
451 void __iomem
*mem
= RZA1_ADDR(port
->base
, reg
, port
->id
);
452 u16 val
= ioread16(mem
);
462 static inline unsigned int rza1_get_bit(struct rza1_port
*port
,
463 unsigned int reg
, unsigned int bit
)
465 void __iomem
*mem
= RZA1_ADDR(port
->base
, reg
, port
->id
);
467 return ioread16(mem
) & BIT(bit
);
471 * rza1_pin_reset() - reset a pin to default initial state
473 * Reset pin state disabling input buffer and bi-directional control,
474 * and configure it as input port.
475 * Note that pin is now configured with direction as input but with input
476 * buffer disabled. This implies the pin value cannot be read in this state.
478 * @port: port where pin sits on
481 static void rza1_pin_reset(struct rza1_port
*port
, unsigned int pin
)
483 unsigned long irqflags
;
485 spin_lock_irqsave(&port
->lock
, irqflags
);
486 rza1_set_bit(port
, RZA1_PIBC_REG
, pin
, 0);
487 rza1_set_bit(port
, RZA1_PBDC_REG
, pin
, 0);
489 rza1_set_bit(port
, RZA1_PM_REG
, pin
, 1);
490 rza1_set_bit(port
, RZA1_PMC_REG
, pin
, 0);
491 rza1_set_bit(port
, RZA1_PIPC_REG
, pin
, 0);
492 spin_unlock_irqrestore(&port
->lock
, irqflags
);
495 static inline int rza1_pin_get_direction(struct rza1_port
*port
,
498 unsigned long irqflags
;
501 spin_lock_irqsave(&port
->lock
, irqflags
);
502 input
= rza1_get_bit(port
, RZA1_PM_REG
, pin
);
503 spin_unlock_irqrestore(&port
->lock
, irqflags
);
509 * rza1_pin_set_direction() - set I/O direction on a pin in port mode
511 * When running in output port mode keep PBDC enabled to allow reading the
512 * pin value from PPR.
514 * @port: port where pin sits on
516 * @input: input enable/disable flag
518 static inline void rza1_pin_set_direction(struct rza1_port
*port
,
519 unsigned int pin
, bool input
)
521 unsigned long irqflags
;
523 spin_lock_irqsave(&port
->lock
, irqflags
);
525 rza1_set_bit(port
, RZA1_PIBC_REG
, pin
, 1);
527 rza1_set_bit(port
, RZA1_PM_REG
, pin
, 1);
528 rza1_set_bit(port
, RZA1_PBDC_REG
, pin
, 0);
530 rza1_set_bit(port
, RZA1_PM_REG
, pin
, 0);
531 rza1_set_bit(port
, RZA1_PBDC_REG
, pin
, 1);
534 spin_unlock_irqrestore(&port
->lock
, irqflags
);
537 static inline void rza1_pin_set(struct rza1_port
*port
, unsigned int pin
,
540 unsigned long irqflags
;
542 spin_lock_irqsave(&port
->lock
, irqflags
);
543 rza1_set_bit(port
, RZA1_P_REG
, pin
, !!value
);
544 spin_unlock_irqrestore(&port
->lock
, irqflags
);
547 static inline int rza1_pin_get(struct rza1_port
*port
, unsigned int pin
)
549 unsigned long irqflags
;
552 spin_lock_irqsave(&port
->lock
, irqflags
);
553 val
= rza1_get_bit(port
, RZA1_PPR_REG
, pin
);
554 spin_unlock_irqrestore(&port
->lock
, irqflags
);
560 * rza1_pin_mux_single() - configure pin multiplexing on a single pin
562 * @pinctrl: RZ/A1 pin controller device
563 * @mux_conf: pin multiplexing descriptor
565 static int rza1_pin_mux_single(struct rza1_pinctrl
*rza1_pctl
,
566 struct rza1_mux_conf
*mux_conf
)
568 struct rza1_port
*port
= &rza1_pctl
->ports
[mux_conf
->port
];
569 unsigned int pin
= mux_conf
->pin
;
570 u8 mux_func
= mux_conf
->mux_func
;
571 u8 mux_flags
= mux_conf
->mux_flags
;
572 u8 mux_flags_from_table
;
574 rza1_pin_reset(port
, pin
);
576 /* SWIO pinmux flags coming from DT are high precedence */
577 mux_flags_from_table
= rza1_pinmux_get_flags(port
->id
, pin
, mux_func
,
580 mux_flags
|= (mux_flags_from_table
& MUX_FLAGS_BIDIR
);
582 mux_flags
= mux_flags_from_table
;
584 if (mux_flags
& MUX_FLAGS_BIDIR
)
585 rza1_set_bit(port
, RZA1_PBDC_REG
, pin
, 1);
588 * Enable alternate function mode and select it.
590 * Be careful here: the pin mux sub-nodes in device tree
591 * enumerate alternate functions from 1 to 8;
592 * subtract 1 before using macros to match registers configuration
593 * which expects numbers from 0 to 7 instead.
595 * ----------------------------------------------------
596 * Alternate mode selection table:
598 * PMC PFC PFCE PFCAE (mux_func - 1)
607 * ----------------------------------------------------
610 rza1_set_bit(port
, RZA1_PFC_REG
, pin
, mux_func
& MUX_FUNC_PFC_MASK
);
611 rza1_set_bit(port
, RZA1_PFCE_REG
, pin
, mux_func
& MUX_FUNC_PFCE_MASK
);
612 rza1_set_bit(port
, RZA1_PFCEA_REG
, pin
, mux_func
& MUX_FUNC_PFCEA_MASK
);
615 * All alternate functions except a few need PIPCn = 1.
616 * If PIPCn has to stay disabled (SW IO mode), configure PMn according
617 * to I/O direction specified by pin configuration -after- PMC has been
620 if (mux_flags
& (MUX_FLAGS_SWIO_INPUT
| MUX_FLAGS_SWIO_OUTPUT
))
621 rza1_set_bit(port
, RZA1_PM_REG
, pin
,
622 mux_flags
& MUX_FLAGS_SWIO_INPUT
);
624 rza1_set_bit(port
, RZA1_PIPC_REG
, pin
, 1);
626 rza1_set_bit(port
, RZA1_PMC_REG
, pin
, 1);
631 /* ----------------------------------------------------------------------------
636 * rza1_gpio_request() - configure pin in port mode
638 * Configure a pin as gpio (port mode).
639 * After reset, the pin is in input mode with input buffer disabled.
640 * To use the pin as input or output, set_direction shall be called first
642 * @chip: gpio chip where the gpio sits on
645 static int rza1_gpio_request(struct gpio_chip
*chip
, unsigned int gpio
)
647 struct rza1_port
*port
= gpiochip_get_data(chip
);
649 rza1_pin_reset(port
, gpio
);
655 * rza1_gpio_disable_free() - reset a pin
657 * Surprisingly, disable_free a gpio, is equivalent to request it.
658 * Reset pin to port mode, with input buffer disabled. This overwrites all
659 * port direction settings applied with set_direction
661 * @chip: gpio chip where the gpio sits on
664 static void rza1_gpio_free(struct gpio_chip
*chip
, unsigned int gpio
)
666 struct rza1_port
*port
= gpiochip_get_data(chip
);
668 rza1_pin_reset(port
, gpio
);
671 static int rza1_gpio_get_direction(struct gpio_chip
*chip
, unsigned int gpio
)
673 struct rza1_port
*port
= gpiochip_get_data(chip
);
675 return rza1_pin_get_direction(port
, gpio
);
678 static int rza1_gpio_direction_input(struct gpio_chip
*chip
,
681 struct rza1_port
*port
= gpiochip_get_data(chip
);
683 rza1_pin_set_direction(port
, gpio
, true);
688 static int rza1_gpio_direction_output(struct gpio_chip
*chip
,
692 struct rza1_port
*port
= gpiochip_get_data(chip
);
694 /* Set value before driving pin direction */
695 rza1_pin_set(port
, gpio
, value
);
696 rza1_pin_set_direction(port
, gpio
, false);
702 * rza1_gpio_get() - read a gpio pin value
704 * Read gpio pin value through PPR register.
705 * Requires bi-directional mode to work when reading the value of a pin
708 * @chip: gpio chip where the gpio sits on
711 static int rza1_gpio_get(struct gpio_chip
*chip
, unsigned int gpio
)
713 struct rza1_port
*port
= gpiochip_get_data(chip
);
715 return rza1_pin_get(port
, gpio
);
718 static void rza1_gpio_set(struct gpio_chip
*chip
, unsigned int gpio
,
721 struct rza1_port
*port
= gpiochip_get_data(chip
);
723 rza1_pin_set(port
, gpio
, value
);
726 static const struct gpio_chip rza1_gpiochip_template
= {
727 .request
= rza1_gpio_request
,
728 .free
= rza1_gpio_free
,
729 .get_direction
= rza1_gpio_get_direction
,
730 .direction_input
= rza1_gpio_direction_input
,
731 .direction_output
= rza1_gpio_direction_output
,
732 .get
= rza1_gpio_get
,
733 .set
= rza1_gpio_set
,
735 /* ----------------------------------------------------------------------------
740 * rza1_dt_node_pin_count() - Count number of pins in a dt node or in all its
743 * @np: device tree node to parse
745 static int rza1_dt_node_pin_count(struct device_node
*np
)
747 struct device_node
*child
;
748 struct property
*of_pins
;
751 of_pins
= of_find_property(np
, "pinmux", NULL
);
753 return of_pins
->length
/ sizeof(u32
);
756 for_each_child_of_node(np
, child
) {
757 of_pins
= of_find_property(child
, "pinmux", NULL
);
761 npins
+= of_pins
->length
/ sizeof(u32
);
768 * rza1_parse_pmx_function() - parse a pin mux sub-node
770 * @rza1_pctl: RZ/A1 pin controller device
771 * @np: of pmx sub-node
772 * @mux_confs: array of pin mux configurations to fill with parsed info
773 * @grpins: array of pin ids to mux
775 static int rza1_parse_pinmux_node(struct rza1_pinctrl
*rza1_pctl
,
776 struct device_node
*np
,
777 struct rza1_mux_conf
*mux_confs
,
778 unsigned int *grpins
)
780 struct pinctrl_dev
*pctldev
= rza1_pctl
->pctl
;
781 char const *prop_name
= "pinmux";
782 unsigned long *pin_configs
;
783 unsigned int npin_configs
;
784 struct property
*of_pins
;
790 of_pins
= of_find_property(np
, prop_name
, NULL
);
792 dev_dbg(rza1_pctl
->dev
, "Missing %s property\n", prop_name
);
795 npins
= of_pins
->length
/ sizeof(u32
);
798 * Collect pin configuration properties: they apply to all pins in
801 ret
= pinconf_generic_parse_dt_config(np
, pctldev
, &pin_configs
,
804 dev_err(rza1_pctl
->dev
,
805 "Unable to parse pin configuration options for %s\n",
811 * Create a mask with pinmux flags from pin configuration;
812 * very few pins (TIOC[0-4][A|B|C|D] require SWIO direction
813 * specified in device tree.
816 for (i
= 0; i
< npin_configs
&& pinmux_flags
== 0; i
++)
817 switch (pinconf_to_config_param(pin_configs
[i
])) {
818 case PIN_CONFIG_INPUT_ENABLE
:
819 pinmux_flags
|= MUX_FLAGS_SWIO_INPUT
;
821 case PIN_CONFIG_OUTPUT
:
822 pinmux_flags
|= MUX_FLAGS_SWIO_OUTPUT
;
830 /* Collect pin positions and their mux settings. */
831 for (i
= 0; i
< npins
; ++i
) {
833 struct rza1_mux_conf
*mux_conf
= &mux_confs
[i
];
835 ret
= of_property_read_u32_index(np
, prop_name
, i
, &of_pinconf
);
839 mux_conf
->id
= of_pinconf
& MUX_PIN_ID_MASK
;
840 mux_conf
->port
= RZA1_PIN_ID_TO_PORT(mux_conf
->id
);
841 mux_conf
->pin
= RZA1_PIN_ID_TO_PIN(mux_conf
->id
);
842 mux_conf
->mux_func
= MUX_FUNC(of_pinconf
);
843 mux_conf
->mux_flags
= pinmux_flags
;
845 if (mux_conf
->port
>= RZA1_NPORTS
||
846 mux_conf
->pin
>= RZA1_PINS_PER_PORT
) {
847 dev_err(rza1_pctl
->dev
,
848 "Wrong port %u pin %u for %s property\n",
849 mux_conf
->port
, mux_conf
->pin
, prop_name
);
853 grpins
[i
] = mux_conf
->id
;
860 * rza1_dt_node_to_map() - map a pin mux node to a function/group
862 * Parse and register a pin mux function.
864 * @pctldev: pin controller device
865 * @np: device tree node to parse
866 * @map: pointer to pin map (output)
867 * @num_maps: number of collected maps (output)
869 static int rza1_dt_node_to_map(struct pinctrl_dev
*pctldev
,
870 struct device_node
*np
,
871 struct pinctrl_map
**map
,
872 unsigned int *num_maps
)
874 struct rza1_pinctrl
*rza1_pctl
= pinctrl_dev_get_drvdata(pctldev
);
875 struct rza1_mux_conf
*mux_confs
, *mux_conf
;
876 unsigned int *grpins
, *grpin
;
877 struct device_node
*child
;
882 npins
= rza1_dt_node_pin_count(np
);
884 dev_err(rza1_pctl
->dev
, "invalid pinmux node structure\n");
889 * Functions are made of 1 group only;
890 * in fact, functions and groups are identical for this pin controller
891 * except that functions carry an array of per-pin mux configuration
894 mux_confs
= devm_kcalloc(rza1_pctl
->dev
, npins
, sizeof(*mux_confs
),
896 grpins
= devm_kcalloc(rza1_pctl
->dev
, npins
, sizeof(*grpins
),
898 fngrps
= devm_kzalloc(rza1_pctl
->dev
, sizeof(*fngrps
), GFP_KERNEL
);
900 if (!mux_confs
|| !grpins
|| !fngrps
)
904 * Parse the pinmux node.
905 * If the node does not contain "pinmux" property (-ENOENT)
906 * that property shall be specified in all its children sub-nodes.
908 mux_conf
= &mux_confs
[0];
911 ret
= rza1_parse_pinmux_node(rza1_pctl
, np
, mux_conf
, grpin
);
913 for_each_child_of_node(np
, child
) {
914 ret
= rza1_parse_pinmux_node(rza1_pctl
, child
, mux_conf
,
925 /* Register pin group and function name to pinctrl_generic */
929 mutex_lock(&rza1_pctl
->mutex
);
930 ret
= pinctrl_generic_add_group(pctldev
, grpname
, grpins
, npins
,
933 mutex_unlock(&rza1_pctl
->mutex
);
937 ret
= pinmux_generic_add_function(pctldev
, grpname
, fngrps
, 1,
941 mutex_unlock(&rza1_pctl
->mutex
);
943 dev_info(rza1_pctl
->dev
, "Parsed function and group %s with %d pins\n",
946 /* Create map where to retrieve function and mux settings from */
948 *map
= kzalloc(sizeof(**map
), GFP_KERNEL
);
951 goto remove_function
;
954 (*map
)->type
= PIN_MAP_TYPE_MUX_GROUP
;
955 (*map
)->data
.mux
.group
= np
->name
;
956 (*map
)->data
.mux
.function
= np
->name
;
962 mutex_lock(&rza1_pctl
->mutex
);
963 pinmux_generic_remove_last_function(pctldev
);
966 pinctrl_generic_remove_last_group(pctldev
);
967 mutex_unlock(&rza1_pctl
->mutex
);
969 dev_info(rza1_pctl
->dev
, "Unable to parse function and group %s\n",
975 static void rza1_dt_free_map(struct pinctrl_dev
*pctldev
,
976 struct pinctrl_map
*map
, unsigned int num_maps
)
981 static const struct pinctrl_ops rza1_pinctrl_ops
= {
982 .get_groups_count
= pinctrl_generic_get_group_count
,
983 .get_group_name
= pinctrl_generic_get_group_name
,
984 .get_group_pins
= pinctrl_generic_get_group_pins
,
985 .dt_node_to_map
= rza1_dt_node_to_map
,
986 .dt_free_map
= rza1_dt_free_map
,
989 /* ----------------------------------------------------------------------------
994 * rza1_set_mux() - retrieve pins from a group and apply their mux settings
996 * @pctldev: pin controller device
997 * @selector: function selector
998 * @group: group selector
1000 static int rza1_set_mux(struct pinctrl_dev
*pctldev
, unsigned int selector
,
1003 struct rza1_pinctrl
*rza1_pctl
= pinctrl_dev_get_drvdata(pctldev
);
1004 struct rza1_mux_conf
*mux_confs
;
1005 struct function_desc
*func
;
1006 struct group_desc
*grp
;
1009 grp
= pinctrl_generic_get_group(pctldev
, group
);
1013 func
= pinmux_generic_get_function(pctldev
, selector
);
1017 mux_confs
= (struct rza1_mux_conf
*)func
->data
;
1018 for (i
= 0; i
< grp
->num_pins
; ++i
) {
1021 ret
= rza1_pin_mux_single(rza1_pctl
, &mux_confs
[i
]);
1029 static const struct pinmux_ops rza1_pinmux_ops
= {
1030 .get_functions_count
= pinmux_generic_get_function_count
,
1031 .get_function_name
= pinmux_generic_get_function_name
,
1032 .get_function_groups
= pinmux_generic_get_function_groups
,
1033 .set_mux
= rza1_set_mux
,
1037 /* ----------------------------------------------------------------------------
1038 * RZ/A1 pin controller driver operations
1041 static unsigned int rza1_count_gpio_chips(struct device_node
*np
)
1043 struct device_node
*child
;
1044 unsigned int count
= 0;
1046 for_each_child_of_node(np
, child
) {
1047 if (!of_property_read_bool(child
, "gpio-controller"))
1057 * rza1_parse_gpiochip() - parse and register a gpio chip and pin range
1059 * The gpio controller subnode shall provide a "gpio-ranges" list property as
1060 * defined by gpio device tree binding documentation.
1062 * @rza1_pctl: RZ/A1 pin controller device
1063 * @np: of gpio-controller node
1064 * @chip: gpio chip to register to gpiolib
1065 * @range: pin range to register to pinctrl core
1067 static int rza1_parse_gpiochip(struct rza1_pinctrl
*rza1_pctl
,
1068 struct device_node
*np
,
1069 struct gpio_chip
*chip
,
1070 struct pinctrl_gpio_range
*range
)
1072 const char *list_name
= "gpio-ranges";
1073 struct of_phandle_args of_args
;
1074 unsigned int gpioport
;
1078 ret
= of_parse_phandle_with_fixed_args(np
, list_name
, 3, 0, &of_args
);
1080 dev_err(rza1_pctl
->dev
, "Unable to parse %s list property\n",
1086 * Find out on which port this gpio-chip maps to by inspecting the
1087 * second argument of the "gpio-ranges" property.
1089 pinctrl_base
= of_args
.args
[1];
1090 gpioport
= RZA1_PIN_ID_TO_PORT(pinctrl_base
);
1091 if (gpioport
>= RZA1_NPORTS
) {
1092 dev_err(rza1_pctl
->dev
,
1093 "Invalid values in property %s\n", list_name
);
1097 *chip
= rza1_gpiochip_template
;
1099 chip
->label
= devm_kasprintf(rza1_pctl
->dev
, GFP_KERNEL
, "%s",
1101 chip
->ngpio
= of_args
.args
[2];
1103 chip
->parent
= rza1_pctl
->dev
;
1105 range
->id
= gpioport
;
1106 range
->name
= chip
->label
;
1107 range
->pin_base
= range
->base
= pinctrl_base
;
1108 range
->npins
= of_args
.args
[2];
1111 ret
= devm_gpiochip_add_data(rza1_pctl
->dev
, chip
,
1112 &rza1_pctl
->ports
[gpioport
]);
1116 pinctrl_add_gpio_range(rza1_pctl
->pctl
, range
);
1118 dev_info(rza1_pctl
->dev
, "Parsed gpiochip %s with %d pins\n",
1119 chip
->label
, chip
->ngpio
);
1125 * rza1_gpio_register() - parse DT to collect gpio-chips and gpio-ranges
1127 * @rza1_pctl: RZ/A1 pin controller device
1129 static int rza1_gpio_register(struct rza1_pinctrl
*rza1_pctl
)
1131 struct device_node
*np
= rza1_pctl
->dev
->of_node
;
1132 struct pinctrl_gpio_range
*gpio_ranges
;
1133 struct gpio_chip
*gpio_chips
;
1134 struct device_node
*child
;
1135 unsigned int ngpiochips
;
1139 ngpiochips
= rza1_count_gpio_chips(np
);
1140 if (ngpiochips
== 0) {
1141 dev_dbg(rza1_pctl
->dev
, "No gpiochip registered\n");
1145 gpio_chips
= devm_kcalloc(rza1_pctl
->dev
, ngpiochips
,
1146 sizeof(*gpio_chips
), GFP_KERNEL
);
1147 gpio_ranges
= devm_kcalloc(rza1_pctl
->dev
, ngpiochips
,
1148 sizeof(*gpio_ranges
), GFP_KERNEL
);
1149 if (!gpio_chips
|| !gpio_ranges
)
1153 for_each_child_of_node(np
, child
) {
1154 if (!of_property_read_bool(child
, "gpio-controller"))
1157 ret
= rza1_parse_gpiochip(rza1_pctl
, child
, &gpio_chips
[i
],
1160 goto gpiochip_remove
;
1165 dev_info(rza1_pctl
->dev
, "Registered %u gpio controllers\n", i
);
1171 devm_gpiochip_remove(rza1_pctl
->dev
, &gpio_chips
[i
- 1]);
1177 * rza1_pinctrl_register() - Enumerate pins, ports and gpiochips; register
1178 * them to pinctrl and gpio cores.
1180 * @rza1_pctl: RZ/A1 pin controller device
1182 static int rza1_pinctrl_register(struct rza1_pinctrl
*rza1_pctl
)
1184 struct pinctrl_pin_desc
*pins
;
1185 struct rza1_port
*ports
;
1189 pins
= devm_kcalloc(rza1_pctl
->dev
, RZA1_NPINS
, sizeof(*pins
),
1191 ports
= devm_kcalloc(rza1_pctl
->dev
, RZA1_NPORTS
, sizeof(*ports
),
1193 if (!pins
|| !ports
)
1196 rza1_pctl
->pins
= pins
;
1197 rza1_pctl
->desc
.pins
= pins
;
1198 rza1_pctl
->desc
.npins
= RZA1_NPINS
;
1199 rza1_pctl
->ports
= ports
;
1201 for (i
= 0; i
< RZA1_NPINS
; ++i
) {
1202 unsigned int pin
= RZA1_PIN_ID_TO_PIN(i
);
1203 unsigned int port
= RZA1_PIN_ID_TO_PORT(i
);
1206 pins
[i
].name
= devm_kasprintf(rza1_pctl
->dev
, GFP_KERNEL
,
1207 "P%u-%u", port
, pin
);
1209 if (i
% RZA1_PINS_PER_PORT
== 0) {
1212 * they provide per-port lock and logical base address.
1214 unsigned int port_id
= RZA1_PIN_ID_TO_PORT(i
);
1216 ports
[port_id
].id
= port_id
;
1217 ports
[port_id
].base
= rza1_pctl
->base
;
1218 ports
[port_id
].pins
= &pins
[i
];
1219 spin_lock_init(&ports
[port_id
].lock
);
1223 ret
= devm_pinctrl_register_and_init(rza1_pctl
->dev
, &rza1_pctl
->desc
,
1224 rza1_pctl
, &rza1_pctl
->pctl
);
1226 dev_err(rza1_pctl
->dev
,
1227 "RZ/A1 pin controller registration failed\n");
1231 ret
= pinctrl_enable(rza1_pctl
->pctl
);
1233 dev_err(rza1_pctl
->dev
,
1234 "RZ/A1 pin controller failed to start\n");
1238 ret
= rza1_gpio_register(rza1_pctl
);
1240 dev_err(rza1_pctl
->dev
, "RZ/A1 GPIO registration failed\n");
1247 static int rza1_pinctrl_probe(struct platform_device
*pdev
)
1249 struct rza1_pinctrl
*rza1_pctl
;
1250 struct resource
*res
;
1253 rza1_pctl
= devm_kzalloc(&pdev
->dev
, sizeof(*rza1_pctl
), GFP_KERNEL
);
1257 rza1_pctl
->dev
= &pdev
->dev
;
1259 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1260 rza1_pctl
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
1261 if (IS_ERR(rza1_pctl
->base
))
1262 return PTR_ERR(rza1_pctl
->base
);
1264 mutex_init(&rza1_pctl
->mutex
);
1266 platform_set_drvdata(pdev
, rza1_pctl
);
1268 rza1_pctl
->desc
.name
= DRIVER_NAME
;
1269 rza1_pctl
->desc
.pctlops
= &rza1_pinctrl_ops
;
1270 rza1_pctl
->desc
.pmxops
= &rza1_pinmux_ops
;
1271 rza1_pctl
->desc
.owner
= THIS_MODULE
;
1272 rza1_pctl
->data
= of_device_get_match_data(&pdev
->dev
);
1274 ret
= rza1_pinctrl_register(rza1_pctl
);
1278 dev_info(&pdev
->dev
,
1279 "RZ/A1 pin controller and gpio successfully registered\n");
1284 static const struct of_device_id rza1_pinctrl_of_match
[] = {
1286 .compatible
= "renesas,r7s72100-ports",
1287 .data
= &rza1h_pmx_conf
,
1292 static struct platform_driver rza1_pinctrl_driver
= {
1294 .name
= DRIVER_NAME
,
1295 .of_match_table
= rza1_pinctrl_of_match
,
1297 .probe
= rza1_pinctrl_probe
,
1300 static int __init
rza1_pinctrl_init(void)
1302 return platform_driver_register(&rza1_pinctrl_driver
);
1304 core_initcall(rza1_pinctrl_init
);
1306 MODULE_AUTHOR("Jacopo Mondi <jacopo+renesas@jmondi.org");
1307 MODULE_DESCRIPTION("Pin and gpio controller driver for Reneas RZ/A1 SoC");
1308 MODULE_LICENSE("GPL v2");