1 // SPDX-License-Identifier: GPL-2.0
3 * Combined GPIO and pin controller support for Renesas RZ/A1 (r7s72100) SoC
5 * Copyright (C) 2017 Jacopo Mondi
9 * This pin controller/gpio combined driver supports Renesas devices of RZ/A1
11 * This includes SoCs which are sub- or super- sets of this particular line,
12 * as RZ/A1H (r7s721000), RZ/A1M (r7s721010) and RZ/A1L (r7s721020).
15 #include <linux/bitops.h>
16 #include <linux/err.h>
17 #include <linux/gpio/driver.h>
18 #include <linux/init.h>
19 #include <linux/ioport.h>
20 #include <linux/module.h>
22 #include <linux/of_address.h>
23 #include <linux/of_device.h>
24 #include <linux/pinctrl/pinconf-generic.h>
25 #include <linux/pinctrl/pinctrl.h>
26 #include <linux/pinctrl/pinmux.h>
27 #include <linux/slab.h>
30 #include "devicetree.h"
34 #define DRIVER_NAME "pinctrl-rza1"
36 #define RZA1_P_REG 0x0000
37 #define RZA1_PPR_REG 0x0200
38 #define RZA1_PM_REG 0x0300
39 #define RZA1_PMC_REG 0x0400
40 #define RZA1_PFC_REG 0x0500
41 #define RZA1_PFCE_REG 0x0600
42 #define RZA1_PFCEA_REG 0x0a00
43 #define RZA1_PIBC_REG 0x4000
44 #define RZA1_PBDC_REG 0x4100
45 #define RZA1_PIPC_REG 0x4200
47 #define RZA1_ADDR(mem, reg, port) ((mem) + (reg) + ((port) * 4))
49 #define RZA1_NPORTS 12
50 #define RZA1_PINS_PER_PORT 16
51 #define RZA1_NPINS (RZA1_PINS_PER_PORT * RZA1_NPORTS)
52 #define RZA1_PIN_ID_TO_PORT(id) ((id) / RZA1_PINS_PER_PORT)
53 #define RZA1_PIN_ID_TO_PIN(id) ((id) % RZA1_PINS_PER_PORT)
56 * Use 16 lower bits [15:0] for pin identifier
57 * Use 16 higher bits [31:16] for pin mux function
59 #define MUX_PIN_ID_MASK GENMASK(15, 0)
60 #define MUX_FUNC_MASK GENMASK(31, 16)
62 #define MUX_FUNC_OFFS 16
63 #define MUX_FUNC(pinconf) \
64 ((pinconf & MUX_FUNC_MASK) >> MUX_FUNC_OFFS)
65 #define MUX_FUNC_PFC_MASK BIT(0)
66 #define MUX_FUNC_PFCE_MASK BIT(1)
67 #define MUX_FUNC_PFCEA_MASK BIT(2)
70 #define MUX_FLAGS_BIDIR BIT(0)
71 #define MUX_FLAGS_SWIO_INPUT BIT(1)
72 #define MUX_FLAGS_SWIO_OUTPUT BIT(2)
74 /* ----------------------------------------------------------------------------
79 * rza1_bidir_pin - describe a single pin that needs bidir flag applied.
81 struct rza1_bidir_pin
{
87 * rza1_bidir_entry - describe a list of pins that needs bidir flag applied.
88 * Each struct rza1_bidir_entry describes a port.
90 struct rza1_bidir_entry
{
91 const unsigned int npins
;
92 const struct rza1_bidir_pin
*pins
;
96 * rza1_swio_pin - describe a single pin that needs bidir flag applied.
98 struct rza1_swio_pin
{
106 * rza1_swio_entry - describe a list of pins that needs swio flag applied
108 struct rza1_swio_entry
{
109 const unsigned int npins
;
110 const struct rza1_swio_pin
*pins
;
114 * rza1_pinmux_conf - group together bidir and swio pinmux flag tables
116 struct rza1_pinmux_conf
{
117 const struct rza1_bidir_entry
*bidir_entries
;
118 const struct rza1_swio_entry
*swio_entries
;
121 /* ----------------------------------------------------------------------------
122 * RZ/A1H (r7s72100) pinmux flags
125 static const struct rza1_bidir_pin rza1h_bidir_pins_p1
[] = {
126 { .pin
= 0, .func
= 1 },
127 { .pin
= 1, .func
= 1 },
128 { .pin
= 2, .func
= 1 },
129 { .pin
= 3, .func
= 1 },
130 { .pin
= 4, .func
= 1 },
131 { .pin
= 5, .func
= 1 },
132 { .pin
= 6, .func
= 1 },
133 { .pin
= 7, .func
= 1 },
136 static const struct rza1_bidir_pin rza1h_bidir_pins_p2
[] = {
137 { .pin
= 0, .func
= 1 },
138 { .pin
= 1, .func
= 1 },
139 { .pin
= 2, .func
= 1 },
140 { .pin
= 3, .func
= 1 },
141 { .pin
= 4, .func
= 1 },
142 { .pin
= 0, .func
= 4 },
143 { .pin
= 1, .func
= 4 },
144 { .pin
= 2, .func
= 4 },
145 { .pin
= 3, .func
= 4 },
146 { .pin
= 5, .func
= 1 },
147 { .pin
= 6, .func
= 1 },
148 { .pin
= 7, .func
= 1 },
149 { .pin
= 8, .func
= 1 },
150 { .pin
= 9, .func
= 1 },
151 { .pin
= 10, .func
= 1 },
152 { .pin
= 11, .func
= 1 },
153 { .pin
= 12, .func
= 1 },
154 { .pin
= 13, .func
= 1 },
155 { .pin
= 14, .func
= 1 },
156 { .pin
= 15, .func
= 1 },
157 { .pin
= 12, .func
= 4 },
158 { .pin
= 13, .func
= 4 },
159 { .pin
= 14, .func
= 4 },
160 { .pin
= 15, .func
= 4 },
163 static const struct rza1_bidir_pin rza1h_bidir_pins_p3
[] = {
164 { .pin
= 3, .func
= 2 },
165 { .pin
= 10, .func
= 7 },
166 { .pin
= 11, .func
= 7 },
167 { .pin
= 13, .func
= 7 },
168 { .pin
= 14, .func
= 7 },
169 { .pin
= 15, .func
= 7 },
170 { .pin
= 10, .func
= 8 },
171 { .pin
= 11, .func
= 8 },
172 { .pin
= 13, .func
= 8 },
173 { .pin
= 14, .func
= 8 },
174 { .pin
= 15, .func
= 8 },
177 static const struct rza1_bidir_pin rza1h_bidir_pins_p4
[] = {
178 { .pin
= 0, .func
= 8 },
179 { .pin
= 1, .func
= 8 },
180 { .pin
= 2, .func
= 8 },
181 { .pin
= 3, .func
= 8 },
182 { .pin
= 10, .func
= 3 },
183 { .pin
= 11, .func
= 3 },
184 { .pin
= 13, .func
= 3 },
185 { .pin
= 14, .func
= 3 },
186 { .pin
= 15, .func
= 3 },
187 { .pin
= 10, .func
= 4 },
188 { .pin
= 11, .func
= 4 },
189 { .pin
= 13, .func
= 4 },
190 { .pin
= 14, .func
= 4 },
191 { .pin
= 15, .func
= 4 },
192 { .pin
= 12, .func
= 5 },
193 { .pin
= 13, .func
= 5 },
194 { .pin
= 14, .func
= 5 },
195 { .pin
= 15, .func
= 5 },
198 static const struct rza1_bidir_pin rza1h_bidir_pins_p6
[] = {
199 { .pin
= 0, .func
= 1 },
200 { .pin
= 1, .func
= 1 },
201 { .pin
= 2, .func
= 1 },
202 { .pin
= 3, .func
= 1 },
203 { .pin
= 4, .func
= 1 },
204 { .pin
= 5, .func
= 1 },
205 { .pin
= 6, .func
= 1 },
206 { .pin
= 7, .func
= 1 },
207 { .pin
= 8, .func
= 1 },
208 { .pin
= 9, .func
= 1 },
209 { .pin
= 10, .func
= 1 },
210 { .pin
= 11, .func
= 1 },
211 { .pin
= 12, .func
= 1 },
212 { .pin
= 13, .func
= 1 },
213 { .pin
= 14, .func
= 1 },
214 { .pin
= 15, .func
= 1 },
217 static const struct rza1_bidir_pin rza1h_bidir_pins_p7
[] = {
218 { .pin
= 13, .func
= 3 },
221 static const struct rza1_bidir_pin rza1h_bidir_pins_p8
[] = {
222 { .pin
= 8, .func
= 3 },
223 { .pin
= 9, .func
= 3 },
224 { .pin
= 10, .func
= 3 },
225 { .pin
= 11, .func
= 3 },
226 { .pin
= 14, .func
= 2 },
227 { .pin
= 15, .func
= 2 },
228 { .pin
= 14, .func
= 3 },
229 { .pin
= 15, .func
= 3 },
232 static const struct rza1_bidir_pin rza1h_bidir_pins_p9
[] = {
233 { .pin
= 0, .func
= 2 },
234 { .pin
= 1, .func
= 2 },
235 { .pin
= 4, .func
= 2 },
236 { .pin
= 5, .func
= 2 },
237 { .pin
= 6, .func
= 2 },
238 { .pin
= 7, .func
= 2 },
241 static const struct rza1_bidir_pin rza1h_bidir_pins_p11
[] = {
242 { .pin
= 6, .func
= 2 },
243 { .pin
= 7, .func
= 2 },
244 { .pin
= 9, .func
= 2 },
245 { .pin
= 6, .func
= 4 },
246 { .pin
= 7, .func
= 4 },
247 { .pin
= 9, .func
= 4 },
248 { .pin
= 10, .func
= 2 },
249 { .pin
= 11, .func
= 2 },
250 { .pin
= 10, .func
= 4 },
251 { .pin
= 11, .func
= 4 },
252 { .pin
= 12, .func
= 4 },
253 { .pin
= 13, .func
= 4 },
254 { .pin
= 14, .func
= 4 },
255 { .pin
= 15, .func
= 4 },
258 static const struct rza1_swio_pin rza1h_swio_pins
[] = {
259 { .port
= 2, .pin
= 7, .func
= 4, .input
= 0 },
260 { .port
= 2, .pin
= 11, .func
= 4, .input
= 0 },
261 { .port
= 3, .pin
= 7, .func
= 3, .input
= 0 },
262 { .port
= 3, .pin
= 7, .func
= 8, .input
= 0 },
263 { .port
= 4, .pin
= 7, .func
= 5, .input
= 0 },
264 { .port
= 4, .pin
= 7, .func
= 11, .input
= 0 },
265 { .port
= 4, .pin
= 15, .func
= 6, .input
= 0 },
266 { .port
= 5, .pin
= 0, .func
= 1, .input
= 1 },
267 { .port
= 5, .pin
= 1, .func
= 1, .input
= 1 },
268 { .port
= 5, .pin
= 2, .func
= 1, .input
= 1 },
269 { .port
= 5, .pin
= 3, .func
= 1, .input
= 1 },
270 { .port
= 5, .pin
= 4, .func
= 1, .input
= 1 },
271 { .port
= 5, .pin
= 5, .func
= 1, .input
= 1 },
272 { .port
= 5, .pin
= 6, .func
= 1, .input
= 1 },
273 { .port
= 5, .pin
= 7, .func
= 1, .input
= 1 },
274 { .port
= 7, .pin
= 4, .func
= 6, .input
= 0 },
275 { .port
= 7, .pin
= 11, .func
= 2, .input
= 0 },
276 { .port
= 8, .pin
= 10, .func
= 8, .input
= 0 },
277 { .port
= 10, .pin
= 15, .func
= 2, .input
= 0 },
280 static const struct rza1_bidir_entry rza1h_bidir_entries
[RZA1_NPORTS
] = {
281 [1] = { ARRAY_SIZE(rza1h_bidir_pins_p1
), rza1h_bidir_pins_p1
},
282 [2] = { ARRAY_SIZE(rza1h_bidir_pins_p2
), rza1h_bidir_pins_p2
},
283 [3] = { ARRAY_SIZE(rza1h_bidir_pins_p3
), rza1h_bidir_pins_p3
},
284 [4] = { ARRAY_SIZE(rza1h_bidir_pins_p4
), rza1h_bidir_pins_p4
},
285 [6] = { ARRAY_SIZE(rza1h_bidir_pins_p6
), rza1h_bidir_pins_p6
},
286 [7] = { ARRAY_SIZE(rza1h_bidir_pins_p7
), rza1h_bidir_pins_p7
},
287 [8] = { ARRAY_SIZE(rza1h_bidir_pins_p8
), rza1h_bidir_pins_p8
},
288 [9] = { ARRAY_SIZE(rza1h_bidir_pins_p9
), rza1h_bidir_pins_p9
},
289 [11] = { ARRAY_SIZE(rza1h_bidir_pins_p11
), rza1h_bidir_pins_p11
},
292 static const struct rza1_swio_entry rza1h_swio_entries
[] = {
293 [0] = { ARRAY_SIZE(rza1h_swio_pins
), rza1h_swio_pins
},
296 /* RZ/A1H (r7s72100x) pinmux flags table */
297 static const struct rza1_pinmux_conf rza1h_pmx_conf
= {
298 .bidir_entries
= rza1h_bidir_entries
,
299 .swio_entries
= rza1h_swio_entries
,
302 /* ----------------------------------------------------------------------------
303 * RZ/A1L (r7s72102) pinmux flags
306 static const struct rza1_bidir_pin rza1l_bidir_pins_p1
[] = {
307 { .pin
= 0, .func
= 1 },
308 { .pin
= 1, .func
= 1 },
309 { .pin
= 2, .func
= 1 },
310 { .pin
= 3, .func
= 1 },
311 { .pin
= 4, .func
= 1 },
312 { .pin
= 5, .func
= 1 },
313 { .pin
= 6, .func
= 1 },
314 { .pin
= 7, .func
= 1 },
317 static const struct rza1_bidir_pin rza1l_bidir_pins_p3
[] = {
318 { .pin
= 0, .func
= 2 },
319 { .pin
= 1, .func
= 2 },
320 { .pin
= 2, .func
= 2 },
321 { .pin
= 4, .func
= 2 },
322 { .pin
= 5, .func
= 2 },
323 { .pin
= 10, .func
= 2 },
324 { .pin
= 11, .func
= 2 },
325 { .pin
= 12, .func
= 2 },
326 { .pin
= 13, .func
= 2 },
329 static const struct rza1_bidir_pin rza1l_bidir_pins_p4
[] = {
330 { .pin
= 1, .func
= 4 },
331 { .pin
= 2, .func
= 2 },
332 { .pin
= 3, .func
= 2 },
333 { .pin
= 6, .func
= 2 },
334 { .pin
= 7, .func
= 2 },
337 static const struct rza1_bidir_pin rza1l_bidir_pins_p5
[] = {
338 { .pin
= 0, .func
= 1 },
339 { .pin
= 1, .func
= 1 },
340 { .pin
= 2, .func
= 1 },
341 { .pin
= 3, .func
= 1 },
342 { .pin
= 4, .func
= 1 },
343 { .pin
= 5, .func
= 1 },
344 { .pin
= 6, .func
= 1 },
345 { .pin
= 7, .func
= 1 },
346 { .pin
= 8, .func
= 1 },
347 { .pin
= 9, .func
= 1 },
348 { .pin
= 10, .func
= 1 },
349 { .pin
= 11, .func
= 1 },
350 { .pin
= 12, .func
= 1 },
351 { .pin
= 13, .func
= 1 },
352 { .pin
= 14, .func
= 1 },
353 { .pin
= 15, .func
= 1 },
354 { .pin
= 0, .func
= 2 },
355 { .pin
= 1, .func
= 2 },
356 { .pin
= 2, .func
= 2 },
357 { .pin
= 3, .func
= 2 },
360 static const struct rza1_bidir_pin rza1l_bidir_pins_p6
[] = {
361 { .pin
= 0, .func
= 1 },
362 { .pin
= 1, .func
= 1 },
363 { .pin
= 2, .func
= 1 },
364 { .pin
= 3, .func
= 1 },
365 { .pin
= 4, .func
= 1 },
366 { .pin
= 5, .func
= 1 },
367 { .pin
= 6, .func
= 1 },
368 { .pin
= 7, .func
= 1 },
369 { .pin
= 8, .func
= 1 },
370 { .pin
= 9, .func
= 1 },
371 { .pin
= 10, .func
= 1 },
372 { .pin
= 11, .func
= 1 },
373 { .pin
= 12, .func
= 1 },
374 { .pin
= 13, .func
= 1 },
375 { .pin
= 14, .func
= 1 },
376 { .pin
= 15, .func
= 1 },
379 static const struct rza1_bidir_pin rza1l_bidir_pins_p7
[] = {
380 { .pin
= 2, .func
= 2 },
381 { .pin
= 3, .func
= 2 },
382 { .pin
= 5, .func
= 2 },
383 { .pin
= 6, .func
= 2 },
384 { .pin
= 7, .func
= 2 },
385 { .pin
= 2, .func
= 3 },
386 { .pin
= 3, .func
= 3 },
387 { .pin
= 5, .func
= 3 },
388 { .pin
= 6, .func
= 3 },
389 { .pin
= 7, .func
= 3 },
392 static const struct rza1_bidir_pin rza1l_bidir_pins_p9
[] = {
393 { .pin
= 1, .func
= 2 },
394 { .pin
= 0, .func
= 3 },
395 { .pin
= 1, .func
= 3 },
396 { .pin
= 3, .func
= 3 },
397 { .pin
= 4, .func
= 3 },
398 { .pin
= 5, .func
= 3 },
401 static const struct rza1_swio_pin rza1l_swio_pins
[] = {
402 { .port
= 2, .pin
= 8, .func
= 2, .input
= 0 },
403 { .port
= 5, .pin
= 6, .func
= 3, .input
= 0 },
404 { .port
= 6, .pin
= 6, .func
= 3, .input
= 0 },
405 { .port
= 6, .pin
= 10, .func
= 3, .input
= 0 },
406 { .port
= 7, .pin
= 10, .func
= 2, .input
= 0 },
407 { .port
= 8, .pin
= 2, .func
= 3, .input
= 0 },
410 static const struct rza1_bidir_entry rza1l_bidir_entries
[RZA1_NPORTS
] = {
411 [1] = { ARRAY_SIZE(rza1l_bidir_pins_p1
), rza1l_bidir_pins_p1
},
412 [3] = { ARRAY_SIZE(rza1l_bidir_pins_p3
), rza1l_bidir_pins_p3
},
413 [4] = { ARRAY_SIZE(rza1l_bidir_pins_p4
), rza1l_bidir_pins_p4
},
414 [5] = { ARRAY_SIZE(rza1l_bidir_pins_p4
), rza1l_bidir_pins_p5
},
415 [6] = { ARRAY_SIZE(rza1l_bidir_pins_p6
), rza1l_bidir_pins_p6
},
416 [7] = { ARRAY_SIZE(rza1l_bidir_pins_p7
), rza1l_bidir_pins_p7
},
417 [9] = { ARRAY_SIZE(rza1l_bidir_pins_p9
), rza1l_bidir_pins_p9
},
420 static const struct rza1_swio_entry rza1l_swio_entries
[] = {
421 [0] = { ARRAY_SIZE(rza1h_swio_pins
), rza1h_swio_pins
},
424 /* RZ/A1L (r7s72102x) pinmux flags table */
425 static const struct rza1_pinmux_conf rza1l_pmx_conf
= {
426 .bidir_entries
= rza1l_bidir_entries
,
427 .swio_entries
= rza1l_swio_entries
,
430 /* ----------------------------------------------------------------------------
434 * rza1_mux_conf - describes a pin multiplexing operation
436 * @id: the pin identifier from 0 to RZA1_NPINS
437 * @port: the port where pin sits on
439 * @mux_func: alternate function id number
440 * @mux_flags: alternate function flags
441 * @value: output value to set the pin to
443 struct rza1_mux_conf
{
453 * rza1_port - describes a pin port
455 * This is mostly useful to lock register writes per-bank and not globally.
457 * @lock: protect access to HW registers
459 * @base: logical address base
460 * @pins: pins sitting on this port
466 struct pinctrl_pin_desc
*pins
;
470 * rza1_pinctrl - RZ pincontroller device
472 * @dev: parent device structure
473 * @mutex: protect [pinctrl|pinmux]_generic functions
474 * @base: logical address base
475 * @nports: number of pin controller ports
476 * @ports: pin controller banks
477 * @pins: pin array for pinctrl core
478 * @desc: pincontroller desc for pinctrl core
479 * @pctl: pinctrl device
480 * @data: device specific data
482 struct rza1_pinctrl
{
490 struct rza1_port
*ports
;
492 struct pinctrl_pin_desc
*pins
;
493 struct pinctrl_desc desc
;
494 struct pinctrl_dev
*pctl
;
499 /* ----------------------------------------------------------------------------
502 static inline bool rza1_pinmux_get_bidir(unsigned int port
,
505 const struct rza1_bidir_entry
*table
)
507 const struct rza1_bidir_entry
*entry
= &table
[port
];
508 const struct rza1_bidir_pin
*bidir_pin
;
511 for (i
= 0; i
< entry
->npins
; ++i
) {
512 bidir_pin
= &entry
->pins
[i
];
513 if (bidir_pin
->pin
== pin
&& bidir_pin
->func
== func
)
520 static inline int rza1_pinmux_get_swio(unsigned int port
,
523 const struct rza1_swio_entry
*table
)
525 const struct rza1_swio_pin
*swio_pin
;
529 for (i
= 0; i
< table
->npins
; ++i
) {
530 swio_pin
= &table
->pins
[i
];
531 if (swio_pin
->port
== port
&& swio_pin
->pin
== pin
&&
532 swio_pin
->func
== func
)
533 return swio_pin
->input
;
540 * rza1_pinmux_get_flags() - return pinmux flags associated to a pin
542 static unsigned int rza1_pinmux_get_flags(unsigned int port
, unsigned int pin
,
544 struct rza1_pinctrl
*rza1_pctl
)
547 const struct rza1_pinmux_conf
*pmx_conf
= rza1_pctl
->data
;
548 const struct rza1_bidir_entry
*bidir_entries
= pmx_conf
->bidir_entries
;
549 const struct rza1_swio_entry
*swio_entries
= pmx_conf
->swio_entries
;
550 unsigned int pmx_flags
= 0;
553 if (rza1_pinmux_get_bidir(port
, pin
, func
, bidir_entries
))
554 pmx_flags
|= MUX_FLAGS_BIDIR
;
556 ret
= rza1_pinmux_get_swio(port
, pin
, func
, swio_entries
);
558 pmx_flags
|= MUX_FLAGS_SWIO_OUTPUT
;
560 pmx_flags
|= MUX_FLAGS_SWIO_INPUT
;
565 /* ----------------------------------------------------------------------------
566 * RZ/A1 SoC operations
570 * rza1_set_bit() - un-locked set/clear a single bit in pin configuration
573 static inline void rza1_set_bit(struct rza1_port
*port
, unsigned int reg
,
574 unsigned int bit
, bool set
)
576 void __iomem
*mem
= RZA1_ADDR(port
->base
, reg
, port
->id
);
577 u16 val
= ioread16(mem
);
587 static inline unsigned int rza1_get_bit(struct rza1_port
*port
,
588 unsigned int reg
, unsigned int bit
)
590 void __iomem
*mem
= RZA1_ADDR(port
->base
, reg
, port
->id
);
592 return ioread16(mem
) & BIT(bit
);
596 * rza1_pin_reset() - reset a pin to default initial state
598 * Reset pin state disabling input buffer and bi-directional control,
599 * and configure it as input port.
600 * Note that pin is now configured with direction as input but with input
601 * buffer disabled. This implies the pin value cannot be read in this state.
603 * @port: port where pin sits on
606 static void rza1_pin_reset(struct rza1_port
*port
, unsigned int pin
)
608 unsigned long irqflags
;
610 spin_lock_irqsave(&port
->lock
, irqflags
);
611 rza1_set_bit(port
, RZA1_PIBC_REG
, pin
, 0);
612 rza1_set_bit(port
, RZA1_PBDC_REG
, pin
, 0);
614 rza1_set_bit(port
, RZA1_PM_REG
, pin
, 1);
615 rza1_set_bit(port
, RZA1_PMC_REG
, pin
, 0);
616 rza1_set_bit(port
, RZA1_PIPC_REG
, pin
, 0);
617 spin_unlock_irqrestore(&port
->lock
, irqflags
);
620 static inline int rza1_pin_get_direction(struct rza1_port
*port
,
623 unsigned long irqflags
;
626 spin_lock_irqsave(&port
->lock
, irqflags
);
627 input
= rza1_get_bit(port
, RZA1_PM_REG
, pin
);
628 spin_unlock_irqrestore(&port
->lock
, irqflags
);
634 * rza1_pin_set_direction() - set I/O direction on a pin in port mode
636 * When running in output port mode keep PBDC enabled to allow reading the
637 * pin value from PPR.
639 * @port: port where pin sits on
641 * @input: input enable/disable flag
643 static inline void rza1_pin_set_direction(struct rza1_port
*port
,
644 unsigned int pin
, bool input
)
646 unsigned long irqflags
;
648 spin_lock_irqsave(&port
->lock
, irqflags
);
650 rza1_set_bit(port
, RZA1_PIBC_REG
, pin
, 1);
652 rza1_set_bit(port
, RZA1_PM_REG
, pin
, 1);
653 rza1_set_bit(port
, RZA1_PBDC_REG
, pin
, 0);
655 rza1_set_bit(port
, RZA1_PM_REG
, pin
, 0);
656 rza1_set_bit(port
, RZA1_PBDC_REG
, pin
, 1);
659 spin_unlock_irqrestore(&port
->lock
, irqflags
);
662 static inline void rza1_pin_set(struct rza1_port
*port
, unsigned int pin
,
665 unsigned long irqflags
;
667 spin_lock_irqsave(&port
->lock
, irqflags
);
668 rza1_set_bit(port
, RZA1_P_REG
, pin
, !!value
);
669 spin_unlock_irqrestore(&port
->lock
, irqflags
);
672 static inline int rza1_pin_get(struct rza1_port
*port
, unsigned int pin
)
674 unsigned long irqflags
;
677 spin_lock_irqsave(&port
->lock
, irqflags
);
678 val
= rza1_get_bit(port
, RZA1_PPR_REG
, pin
);
679 spin_unlock_irqrestore(&port
->lock
, irqflags
);
685 * rza1_pin_mux_single() - configure pin multiplexing on a single pin
687 * @pinctrl: RZ/A1 pin controller device
688 * @mux_conf: pin multiplexing descriptor
690 static int rza1_pin_mux_single(struct rza1_pinctrl
*rza1_pctl
,
691 struct rza1_mux_conf
*mux_conf
)
693 struct rza1_port
*port
= &rza1_pctl
->ports
[mux_conf
->port
];
694 unsigned int pin
= mux_conf
->pin
;
695 u8 mux_func
= mux_conf
->mux_func
;
696 u8 mux_flags
= mux_conf
->mux_flags
;
697 u8 mux_flags_from_table
;
699 rza1_pin_reset(port
, pin
);
701 /* SWIO pinmux flags coming from DT are high precedence */
702 mux_flags_from_table
= rza1_pinmux_get_flags(port
->id
, pin
, mux_func
,
705 mux_flags
|= (mux_flags_from_table
& MUX_FLAGS_BIDIR
);
707 mux_flags
= mux_flags_from_table
;
709 if (mux_flags
& MUX_FLAGS_BIDIR
)
710 rza1_set_bit(port
, RZA1_PBDC_REG
, pin
, 1);
713 * Enable alternate function mode and select it.
715 * Be careful here: the pin mux sub-nodes in device tree
716 * enumerate alternate functions from 1 to 8;
717 * subtract 1 before using macros to match registers configuration
718 * which expects numbers from 0 to 7 instead.
720 * ----------------------------------------------------
721 * Alternate mode selection table:
723 * PMC PFC PFCE PFCAE (mux_func - 1)
732 * ----------------------------------------------------
735 rza1_set_bit(port
, RZA1_PFC_REG
, pin
, mux_func
& MUX_FUNC_PFC_MASK
);
736 rza1_set_bit(port
, RZA1_PFCE_REG
, pin
, mux_func
& MUX_FUNC_PFCE_MASK
);
737 rza1_set_bit(port
, RZA1_PFCEA_REG
, pin
, mux_func
& MUX_FUNC_PFCEA_MASK
);
740 * All alternate functions except a few need PIPCn = 1.
741 * If PIPCn has to stay disabled (SW IO mode), configure PMn according
742 * to I/O direction specified by pin configuration -after- PMC has been
745 if (mux_flags
& (MUX_FLAGS_SWIO_INPUT
| MUX_FLAGS_SWIO_OUTPUT
))
746 rza1_set_bit(port
, RZA1_PM_REG
, pin
,
747 mux_flags
& MUX_FLAGS_SWIO_INPUT
);
749 rza1_set_bit(port
, RZA1_PIPC_REG
, pin
, 1);
751 rza1_set_bit(port
, RZA1_PMC_REG
, pin
, 1);
756 /* ----------------------------------------------------------------------------
761 * rza1_gpio_request() - configure pin in port mode
763 * Configure a pin as gpio (port mode).
764 * After reset, the pin is in input mode with input buffer disabled.
765 * To use the pin as input or output, set_direction shall be called first
767 * @chip: gpio chip where the gpio sits on
770 static int rza1_gpio_request(struct gpio_chip
*chip
, unsigned int gpio
)
772 struct rza1_port
*port
= gpiochip_get_data(chip
);
774 rza1_pin_reset(port
, gpio
);
780 * rza1_gpio_disable_free() - reset a pin
782 * Surprisingly, disable_free a gpio, is equivalent to request it.
783 * Reset pin to port mode, with input buffer disabled. This overwrites all
784 * port direction settings applied with set_direction
786 * @chip: gpio chip where the gpio sits on
789 static void rza1_gpio_free(struct gpio_chip
*chip
, unsigned int gpio
)
791 struct rza1_port
*port
= gpiochip_get_data(chip
);
793 rza1_pin_reset(port
, gpio
);
796 static int rza1_gpio_get_direction(struct gpio_chip
*chip
, unsigned int gpio
)
798 struct rza1_port
*port
= gpiochip_get_data(chip
);
800 return rza1_pin_get_direction(port
, gpio
);
803 static int rza1_gpio_direction_input(struct gpio_chip
*chip
,
806 struct rza1_port
*port
= gpiochip_get_data(chip
);
808 rza1_pin_set_direction(port
, gpio
, true);
813 static int rza1_gpio_direction_output(struct gpio_chip
*chip
,
817 struct rza1_port
*port
= gpiochip_get_data(chip
);
819 /* Set value before driving pin direction */
820 rza1_pin_set(port
, gpio
, value
);
821 rza1_pin_set_direction(port
, gpio
, false);
827 * rza1_gpio_get() - read a gpio pin value
829 * Read gpio pin value through PPR register.
830 * Requires bi-directional mode to work when reading the value of a pin
833 * @chip: gpio chip where the gpio sits on
836 static int rza1_gpio_get(struct gpio_chip
*chip
, unsigned int gpio
)
838 struct rza1_port
*port
= gpiochip_get_data(chip
);
840 return rza1_pin_get(port
, gpio
);
843 static void rza1_gpio_set(struct gpio_chip
*chip
, unsigned int gpio
,
846 struct rza1_port
*port
= gpiochip_get_data(chip
);
848 rza1_pin_set(port
, gpio
, value
);
851 static const struct gpio_chip rza1_gpiochip_template
= {
852 .request
= rza1_gpio_request
,
853 .free
= rza1_gpio_free
,
854 .get_direction
= rza1_gpio_get_direction
,
855 .direction_input
= rza1_gpio_direction_input
,
856 .direction_output
= rza1_gpio_direction_output
,
857 .get
= rza1_gpio_get
,
858 .set
= rza1_gpio_set
,
860 /* ----------------------------------------------------------------------------
865 * rza1_dt_node_pin_count() - Count number of pins in a dt node or in all its
868 * @np: device tree node to parse
870 static int rza1_dt_node_pin_count(struct device_node
*np
)
872 struct device_node
*child
;
873 struct property
*of_pins
;
876 of_pins
= of_find_property(np
, "pinmux", NULL
);
878 return of_pins
->length
/ sizeof(u32
);
881 for_each_child_of_node(np
, child
) {
882 of_pins
= of_find_property(child
, "pinmux", NULL
);
886 npins
+= of_pins
->length
/ sizeof(u32
);
893 * rza1_parse_pmx_function() - parse a pin mux sub-node
895 * @rza1_pctl: RZ/A1 pin controller device
896 * @np: of pmx sub-node
897 * @mux_confs: array of pin mux configurations to fill with parsed info
898 * @grpins: array of pin ids to mux
900 static int rza1_parse_pinmux_node(struct rza1_pinctrl
*rza1_pctl
,
901 struct device_node
*np
,
902 struct rza1_mux_conf
*mux_confs
,
903 unsigned int *grpins
)
905 struct pinctrl_dev
*pctldev
= rza1_pctl
->pctl
;
906 char const *prop_name
= "pinmux";
907 unsigned long *pin_configs
;
908 unsigned int npin_configs
;
909 struct property
*of_pins
;
915 of_pins
= of_find_property(np
, prop_name
, NULL
);
917 dev_dbg(rza1_pctl
->dev
, "Missing %s property\n", prop_name
);
920 npins
= of_pins
->length
/ sizeof(u32
);
923 * Collect pin configuration properties: they apply to all pins in
926 ret
= pinconf_generic_parse_dt_config(np
, pctldev
, &pin_configs
,
929 dev_err(rza1_pctl
->dev
,
930 "Unable to parse pin configuration options for %pOFn\n",
936 * Create a mask with pinmux flags from pin configuration;
937 * very few pins (TIOC[0-4][A|B|C|D] require SWIO direction
938 * specified in device tree.
941 for (i
= 0; i
< npin_configs
&& pinmux_flags
== 0; i
++)
942 switch (pinconf_to_config_param(pin_configs
[i
])) {
943 case PIN_CONFIG_INPUT_ENABLE
:
944 pinmux_flags
|= MUX_FLAGS_SWIO_INPUT
;
946 case PIN_CONFIG_OUTPUT
:
947 pinmux_flags
|= MUX_FLAGS_SWIO_OUTPUT
;
955 /* Collect pin positions and their mux settings. */
956 for (i
= 0; i
< npins
; ++i
) {
958 struct rza1_mux_conf
*mux_conf
= &mux_confs
[i
];
960 ret
= of_property_read_u32_index(np
, prop_name
, i
, &of_pinconf
);
964 mux_conf
->id
= of_pinconf
& MUX_PIN_ID_MASK
;
965 mux_conf
->port
= RZA1_PIN_ID_TO_PORT(mux_conf
->id
);
966 mux_conf
->pin
= RZA1_PIN_ID_TO_PIN(mux_conf
->id
);
967 mux_conf
->mux_func
= MUX_FUNC(of_pinconf
);
968 mux_conf
->mux_flags
= pinmux_flags
;
970 if (mux_conf
->port
>= RZA1_NPORTS
||
971 mux_conf
->pin
>= RZA1_PINS_PER_PORT
) {
972 dev_err(rza1_pctl
->dev
,
973 "Wrong port %u pin %u for %s property\n",
974 mux_conf
->port
, mux_conf
->pin
, prop_name
);
978 grpins
[i
] = mux_conf
->id
;
985 * rza1_dt_node_to_map() - map a pin mux node to a function/group
987 * Parse and register a pin mux function.
989 * @pctldev: pin controller device
990 * @np: device tree node to parse
991 * @map: pointer to pin map (output)
992 * @num_maps: number of collected maps (output)
994 static int rza1_dt_node_to_map(struct pinctrl_dev
*pctldev
,
995 struct device_node
*np
,
996 struct pinctrl_map
**map
,
997 unsigned int *num_maps
)
999 struct rza1_pinctrl
*rza1_pctl
= pinctrl_dev_get_drvdata(pctldev
);
1000 struct rza1_mux_conf
*mux_confs
, *mux_conf
;
1001 unsigned int *grpins
, *grpin
;
1002 struct device_node
*child
;
1003 const char *grpname
;
1004 const char **fngrps
;
1008 npins
= rza1_dt_node_pin_count(np
);
1010 dev_err(rza1_pctl
->dev
, "invalid pinmux node structure\n");
1015 * Functions are made of 1 group only;
1016 * in fact, functions and groups are identical for this pin controller
1017 * except that functions carry an array of per-pin mux configuration
1020 mux_confs
= devm_kcalloc(rza1_pctl
->dev
, npins
, sizeof(*mux_confs
),
1022 grpins
= devm_kcalloc(rza1_pctl
->dev
, npins
, sizeof(*grpins
),
1024 fngrps
= devm_kzalloc(rza1_pctl
->dev
, sizeof(*fngrps
), GFP_KERNEL
);
1026 if (!mux_confs
|| !grpins
|| !fngrps
)
1030 * Parse the pinmux node.
1031 * If the node does not contain "pinmux" property (-ENOENT)
1032 * that property shall be specified in all its children sub-nodes.
1034 mux_conf
= &mux_confs
[0];
1037 ret
= rza1_parse_pinmux_node(rza1_pctl
, np
, mux_conf
, grpin
);
1039 for_each_child_of_node(np
, child
) {
1040 ret
= rza1_parse_pinmux_node(rza1_pctl
, child
, mux_conf
,
1051 /* Register pin group and function name to pinctrl_generic */
1053 fngrps
[0] = grpname
;
1055 mutex_lock(&rza1_pctl
->mutex
);
1056 gsel
= pinctrl_generic_add_group(pctldev
, grpname
, grpins
, npins
,
1059 mutex_unlock(&rza1_pctl
->mutex
);
1063 fsel
= pinmux_generic_add_function(pctldev
, grpname
, fngrps
, 1,
1070 dev_info(rza1_pctl
->dev
, "Parsed function and group %s with %d pins\n",
1073 /* Create map where to retrieve function and mux settings from */
1075 *map
= kzalloc(sizeof(**map
), GFP_KERNEL
);
1078 goto remove_function
;
1081 (*map
)->type
= PIN_MAP_TYPE_MUX_GROUP
;
1082 (*map
)->data
.mux
.group
= np
->name
;
1083 (*map
)->data
.mux
.function
= np
->name
;
1085 mutex_unlock(&rza1_pctl
->mutex
);
1090 pinmux_generic_remove_function(pctldev
, fsel
);
1093 pinctrl_generic_remove_group(pctldev
, gsel
);
1094 mutex_unlock(&rza1_pctl
->mutex
);
1096 dev_info(rza1_pctl
->dev
, "Unable to parse function and group %s\n",
1102 static void rza1_dt_free_map(struct pinctrl_dev
*pctldev
,
1103 struct pinctrl_map
*map
, unsigned int num_maps
)
1108 static const struct pinctrl_ops rza1_pinctrl_ops
= {
1109 .get_groups_count
= pinctrl_generic_get_group_count
,
1110 .get_group_name
= pinctrl_generic_get_group_name
,
1111 .get_group_pins
= pinctrl_generic_get_group_pins
,
1112 .dt_node_to_map
= rza1_dt_node_to_map
,
1113 .dt_free_map
= rza1_dt_free_map
,
1116 /* ----------------------------------------------------------------------------
1121 * rza1_set_mux() - retrieve pins from a group and apply their mux settings
1123 * @pctldev: pin controller device
1124 * @selector: function selector
1125 * @group: group selector
1127 static int rza1_set_mux(struct pinctrl_dev
*pctldev
, unsigned int selector
,
1130 struct rza1_pinctrl
*rza1_pctl
= pinctrl_dev_get_drvdata(pctldev
);
1131 struct rza1_mux_conf
*mux_confs
;
1132 struct function_desc
*func
;
1133 struct group_desc
*grp
;
1136 grp
= pinctrl_generic_get_group(pctldev
, group
);
1140 func
= pinmux_generic_get_function(pctldev
, selector
);
1144 mux_confs
= (struct rza1_mux_conf
*)func
->data
;
1145 for (i
= 0; i
< grp
->num_pins
; ++i
) {
1148 ret
= rza1_pin_mux_single(rza1_pctl
, &mux_confs
[i
]);
1156 static const struct pinmux_ops rza1_pinmux_ops
= {
1157 .get_functions_count
= pinmux_generic_get_function_count
,
1158 .get_function_name
= pinmux_generic_get_function_name
,
1159 .get_function_groups
= pinmux_generic_get_function_groups
,
1160 .set_mux
= rza1_set_mux
,
1164 /* ----------------------------------------------------------------------------
1165 * RZ/A1 pin controller driver operations
1168 static unsigned int rza1_count_gpio_chips(struct device_node
*np
)
1170 struct device_node
*child
;
1171 unsigned int count
= 0;
1173 for_each_child_of_node(np
, child
) {
1174 if (!of_property_read_bool(child
, "gpio-controller"))
1184 * rza1_parse_gpiochip() - parse and register a gpio chip and pin range
1186 * The gpio controller subnode shall provide a "gpio-ranges" list property as
1187 * defined by gpio device tree binding documentation.
1189 * @rza1_pctl: RZ/A1 pin controller device
1190 * @np: of gpio-controller node
1191 * @chip: gpio chip to register to gpiolib
1192 * @range: pin range to register to pinctrl core
1194 static int rza1_parse_gpiochip(struct rza1_pinctrl
*rza1_pctl
,
1195 struct device_node
*np
,
1196 struct gpio_chip
*chip
,
1197 struct pinctrl_gpio_range
*range
)
1199 const char *list_name
= "gpio-ranges";
1200 struct of_phandle_args of_args
;
1201 unsigned int gpioport
;
1205 ret
= of_parse_phandle_with_fixed_args(np
, list_name
, 3, 0, &of_args
);
1207 dev_err(rza1_pctl
->dev
, "Unable to parse %s list property\n",
1213 * Find out on which port this gpio-chip maps to by inspecting the
1214 * second argument of the "gpio-ranges" property.
1216 pinctrl_base
= of_args
.args
[1];
1217 gpioport
= RZA1_PIN_ID_TO_PORT(pinctrl_base
);
1218 if (gpioport
>= RZA1_NPORTS
) {
1219 dev_err(rza1_pctl
->dev
,
1220 "Invalid values in property %s\n", list_name
);
1224 *chip
= rza1_gpiochip_template
;
1226 chip
->label
= devm_kasprintf(rza1_pctl
->dev
, GFP_KERNEL
, "%pOFn",
1231 chip
->ngpio
= of_args
.args
[2];
1233 chip
->parent
= rza1_pctl
->dev
;
1235 range
->id
= gpioport
;
1236 range
->name
= chip
->label
;
1237 range
->pin_base
= range
->base
= pinctrl_base
;
1238 range
->npins
= of_args
.args
[2];
1241 ret
= devm_gpiochip_add_data(rza1_pctl
->dev
, chip
,
1242 &rza1_pctl
->ports
[gpioport
]);
1246 pinctrl_add_gpio_range(rza1_pctl
->pctl
, range
);
1248 dev_info(rza1_pctl
->dev
, "Parsed gpiochip %s with %d pins\n",
1249 chip
->label
, chip
->ngpio
);
1255 * rza1_gpio_register() - parse DT to collect gpio-chips and gpio-ranges
1257 * @rza1_pctl: RZ/A1 pin controller device
1259 static int rza1_gpio_register(struct rza1_pinctrl
*rza1_pctl
)
1261 struct device_node
*np
= rza1_pctl
->dev
->of_node
;
1262 struct pinctrl_gpio_range
*gpio_ranges
;
1263 struct gpio_chip
*gpio_chips
;
1264 struct device_node
*child
;
1265 unsigned int ngpiochips
;
1269 ngpiochips
= rza1_count_gpio_chips(np
);
1270 if (ngpiochips
== 0) {
1271 dev_dbg(rza1_pctl
->dev
, "No gpiochip registered\n");
1275 gpio_chips
= devm_kcalloc(rza1_pctl
->dev
, ngpiochips
,
1276 sizeof(*gpio_chips
), GFP_KERNEL
);
1277 gpio_ranges
= devm_kcalloc(rza1_pctl
->dev
, ngpiochips
,
1278 sizeof(*gpio_ranges
), GFP_KERNEL
);
1279 if (!gpio_chips
|| !gpio_ranges
)
1283 for_each_child_of_node(np
, child
) {
1284 if (!of_property_read_bool(child
, "gpio-controller"))
1287 ret
= rza1_parse_gpiochip(rza1_pctl
, child
, &gpio_chips
[i
],
1295 dev_info(rza1_pctl
->dev
, "Registered %u gpio controllers\n", i
);
1301 * rza1_pinctrl_register() - Enumerate pins, ports and gpiochips; register
1302 * them to pinctrl and gpio cores.
1304 * @rza1_pctl: RZ/A1 pin controller device
1306 static int rza1_pinctrl_register(struct rza1_pinctrl
*rza1_pctl
)
1308 struct pinctrl_pin_desc
*pins
;
1309 struct rza1_port
*ports
;
1313 pins
= devm_kcalloc(rza1_pctl
->dev
, RZA1_NPINS
, sizeof(*pins
),
1315 ports
= devm_kcalloc(rza1_pctl
->dev
, RZA1_NPORTS
, sizeof(*ports
),
1317 if (!pins
|| !ports
)
1320 rza1_pctl
->pins
= pins
;
1321 rza1_pctl
->desc
.pins
= pins
;
1322 rza1_pctl
->desc
.npins
= RZA1_NPINS
;
1323 rza1_pctl
->ports
= ports
;
1325 for (i
= 0; i
< RZA1_NPINS
; ++i
) {
1326 unsigned int pin
= RZA1_PIN_ID_TO_PIN(i
);
1327 unsigned int port
= RZA1_PIN_ID_TO_PORT(i
);
1330 pins
[i
].name
= devm_kasprintf(rza1_pctl
->dev
, GFP_KERNEL
,
1331 "P%u-%u", port
, pin
);
1335 if (i
% RZA1_PINS_PER_PORT
== 0) {
1338 * they provide per-port lock and logical base address.
1340 unsigned int port_id
= RZA1_PIN_ID_TO_PORT(i
);
1342 ports
[port_id
].id
= port_id
;
1343 ports
[port_id
].base
= rza1_pctl
->base
;
1344 ports
[port_id
].pins
= &pins
[i
];
1345 spin_lock_init(&ports
[port_id
].lock
);
1349 ret
= devm_pinctrl_register_and_init(rza1_pctl
->dev
, &rza1_pctl
->desc
,
1350 rza1_pctl
, &rza1_pctl
->pctl
);
1352 dev_err(rza1_pctl
->dev
,
1353 "RZ/A1 pin controller registration failed\n");
1357 ret
= pinctrl_enable(rza1_pctl
->pctl
);
1359 dev_err(rza1_pctl
->dev
,
1360 "RZ/A1 pin controller failed to start\n");
1364 ret
= rza1_gpio_register(rza1_pctl
);
1366 dev_err(rza1_pctl
->dev
, "RZ/A1 GPIO registration failed\n");
1373 static int rza1_pinctrl_probe(struct platform_device
*pdev
)
1375 struct rza1_pinctrl
*rza1_pctl
;
1376 struct resource
*res
;
1379 rza1_pctl
= devm_kzalloc(&pdev
->dev
, sizeof(*rza1_pctl
), GFP_KERNEL
);
1383 rza1_pctl
->dev
= &pdev
->dev
;
1385 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1386 rza1_pctl
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
1387 if (IS_ERR(rza1_pctl
->base
))
1388 return PTR_ERR(rza1_pctl
->base
);
1390 mutex_init(&rza1_pctl
->mutex
);
1392 platform_set_drvdata(pdev
, rza1_pctl
);
1394 rza1_pctl
->desc
.name
= DRIVER_NAME
;
1395 rza1_pctl
->desc
.pctlops
= &rza1_pinctrl_ops
;
1396 rza1_pctl
->desc
.pmxops
= &rza1_pinmux_ops
;
1397 rza1_pctl
->desc
.owner
= THIS_MODULE
;
1398 rza1_pctl
->data
= of_device_get_match_data(&pdev
->dev
);
1400 ret
= rza1_pinctrl_register(rza1_pctl
);
1404 dev_info(&pdev
->dev
,
1405 "RZ/A1 pin controller and gpio successfully registered\n");
1410 static const struct of_device_id rza1_pinctrl_of_match
[] = {
1412 /* RZ/A1H, RZ/A1M */
1413 .compatible
= "renesas,r7s72100-ports",
1414 .data
= &rza1h_pmx_conf
,
1418 .compatible
= "renesas,r7s72102-ports",
1419 .data
= &rza1l_pmx_conf
,
1424 static struct platform_driver rza1_pinctrl_driver
= {
1426 .name
= DRIVER_NAME
,
1427 .of_match_table
= rza1_pinctrl_of_match
,
1429 .probe
= rza1_pinctrl_probe
,
1432 static int __init
rza1_pinctrl_init(void)
1434 return platform_driver_register(&rza1_pinctrl_driver
);
1436 core_initcall(rza1_pinctrl_init
);
1438 MODULE_AUTHOR("Jacopo Mondi <jacopo+renesas@jmondi.org");
1439 MODULE_DESCRIPTION("Pin and gpio controller driver for Reneas RZ/A1 SoC");
1440 MODULE_LICENSE("GPL v2");