0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <linux/bitops.h>
0016 #include <linux/err.h>
0017 #include <linux/gpio/driver.h>
0018 #include <linux/init.h>
0019 #include <linux/ioport.h>
0020 #include <linux/module.h>
0021 #include <linux/of.h>
0022 #include <linux/of_address.h>
0023 #include <linux/of_device.h>
0024 #include <linux/pinctrl/pinconf-generic.h>
0025 #include <linux/pinctrl/pinctrl.h>
0026 #include <linux/pinctrl/pinmux.h>
0027 #include <linux/property.h>
0028 #include <linux/slab.h>
0029
0030 #include "../core.h"
0031 #include "../devicetree.h"
0032 #include "../pinconf.h"
0033 #include "../pinmux.h"
0034
0035 #define DRIVER_NAME "pinctrl-rza1"
0036
0037 #define RZA1_P_REG 0x0000
0038 #define RZA1_PPR_REG 0x0200
0039 #define RZA1_PM_REG 0x0300
0040 #define RZA1_PMC_REG 0x0400
0041 #define RZA1_PFC_REG 0x0500
0042 #define RZA1_PFCE_REG 0x0600
0043 #define RZA1_PFCEA_REG 0x0a00
0044 #define RZA1_PIBC_REG 0x4000
0045 #define RZA1_PBDC_REG 0x4100
0046 #define RZA1_PIPC_REG 0x4200
0047
0048 #define RZA1_ADDR(mem, reg, port) ((mem) + (reg) + ((port) * 4))
0049
0050 #define RZA1_NPORTS 12
0051 #define RZA1_PINS_PER_PORT 16
0052 #define RZA1_NPINS (RZA1_PINS_PER_PORT * RZA1_NPORTS)
0053 #define RZA1_PIN_ID_TO_PORT(id) ((id) / RZA1_PINS_PER_PORT)
0054 #define RZA1_PIN_ID_TO_PIN(id) ((id) % RZA1_PINS_PER_PORT)
0055
0056
0057
0058
0059
0060 #define MUX_PIN_ID_MASK GENMASK(15, 0)
0061 #define MUX_FUNC_MASK GENMASK(31, 16)
0062
0063 #define MUX_FUNC_OFFS 16
0064 #define MUX_FUNC(pinconf) \
0065 ((pinconf & MUX_FUNC_MASK) >> MUX_FUNC_OFFS)
0066 #define MUX_FUNC_PFC_MASK BIT(0)
0067 #define MUX_FUNC_PFCE_MASK BIT(1)
0068 #define MUX_FUNC_PFCEA_MASK BIT(2)
0069
0070
0071 #define MUX_FLAGS_BIDIR BIT(0)
0072 #define MUX_FLAGS_SWIO_INPUT BIT(1)
0073 #define MUX_FLAGS_SWIO_OUTPUT BIT(2)
0074
0075
0076
0077
0078
0079
0080
0081
0082 struct rza1_bidir_pin {
0083 u8 pin: 4;
0084 u8 func: 4;
0085 };
0086
0087
0088
0089
0090
0091 struct rza1_bidir_entry {
0092 const unsigned int npins;
0093 const struct rza1_bidir_pin *pins;
0094 };
0095
0096
0097
0098
0099 struct rza1_swio_pin {
0100 u16 pin: 4;
0101 u16 port: 4;
0102 u16 func: 4;
0103 u16 input: 1;
0104 };
0105
0106
0107
0108
0109 struct rza1_swio_entry {
0110 const unsigned int npins;
0111 const struct rza1_swio_pin *pins;
0112 };
0113
0114
0115
0116
0117 struct rza1_pinmux_conf {
0118 const struct rza1_bidir_entry *bidir_entries;
0119 const struct rza1_swio_entry *swio_entries;
0120 };
0121
0122
0123
0124
0125
0126 static const struct rza1_bidir_pin rza1h_bidir_pins_p1[] = {
0127 { .pin = 0, .func = 1 },
0128 { .pin = 1, .func = 1 },
0129 { .pin = 2, .func = 1 },
0130 { .pin = 3, .func = 1 },
0131 { .pin = 4, .func = 1 },
0132 { .pin = 5, .func = 1 },
0133 { .pin = 6, .func = 1 },
0134 { .pin = 7, .func = 1 },
0135 };
0136
0137 static const struct rza1_bidir_pin rza1h_bidir_pins_p2[] = {
0138 { .pin = 0, .func = 1 },
0139 { .pin = 1, .func = 1 },
0140 { .pin = 2, .func = 1 },
0141 { .pin = 3, .func = 1 },
0142 { .pin = 4, .func = 1 },
0143 { .pin = 0, .func = 4 },
0144 { .pin = 1, .func = 4 },
0145 { .pin = 2, .func = 4 },
0146 { .pin = 3, .func = 4 },
0147 { .pin = 5, .func = 1 },
0148 { .pin = 6, .func = 1 },
0149 { .pin = 7, .func = 1 },
0150 { .pin = 8, .func = 1 },
0151 { .pin = 9, .func = 1 },
0152 { .pin = 10, .func = 1 },
0153 { .pin = 11, .func = 1 },
0154 { .pin = 12, .func = 1 },
0155 { .pin = 13, .func = 1 },
0156 { .pin = 14, .func = 1 },
0157 { .pin = 15, .func = 1 },
0158 { .pin = 12, .func = 4 },
0159 { .pin = 13, .func = 4 },
0160 { .pin = 14, .func = 4 },
0161 { .pin = 15, .func = 4 },
0162 };
0163
0164 static const struct rza1_bidir_pin rza1h_bidir_pins_p3[] = {
0165 { .pin = 3, .func = 2 },
0166 { .pin = 10, .func = 7 },
0167 { .pin = 11, .func = 7 },
0168 { .pin = 13, .func = 7 },
0169 { .pin = 14, .func = 7 },
0170 { .pin = 15, .func = 7 },
0171 { .pin = 10, .func = 8 },
0172 { .pin = 11, .func = 8 },
0173 { .pin = 13, .func = 8 },
0174 { .pin = 14, .func = 8 },
0175 { .pin = 15, .func = 8 },
0176 };
0177
0178 static const struct rza1_bidir_pin rza1h_bidir_pins_p4[] = {
0179 { .pin = 0, .func = 8 },
0180 { .pin = 1, .func = 8 },
0181 { .pin = 2, .func = 8 },
0182 { .pin = 3, .func = 8 },
0183 { .pin = 10, .func = 3 },
0184 { .pin = 11, .func = 3 },
0185 { .pin = 13, .func = 3 },
0186 { .pin = 14, .func = 3 },
0187 { .pin = 15, .func = 3 },
0188 { .pin = 10, .func = 4 },
0189 { .pin = 11, .func = 4 },
0190 { .pin = 13, .func = 4 },
0191 { .pin = 14, .func = 4 },
0192 { .pin = 15, .func = 4 },
0193 { .pin = 12, .func = 5 },
0194 { .pin = 13, .func = 5 },
0195 { .pin = 14, .func = 5 },
0196 { .pin = 15, .func = 5 },
0197 };
0198
0199 static const struct rza1_bidir_pin rza1h_bidir_pins_p6[] = {
0200 { .pin = 0, .func = 1 },
0201 { .pin = 1, .func = 1 },
0202 { .pin = 2, .func = 1 },
0203 { .pin = 3, .func = 1 },
0204 { .pin = 4, .func = 1 },
0205 { .pin = 5, .func = 1 },
0206 { .pin = 6, .func = 1 },
0207 { .pin = 7, .func = 1 },
0208 { .pin = 8, .func = 1 },
0209 { .pin = 9, .func = 1 },
0210 { .pin = 10, .func = 1 },
0211 { .pin = 11, .func = 1 },
0212 { .pin = 12, .func = 1 },
0213 { .pin = 13, .func = 1 },
0214 { .pin = 14, .func = 1 },
0215 { .pin = 15, .func = 1 },
0216 };
0217
0218 static const struct rza1_bidir_pin rza1h_bidir_pins_p7[] = {
0219 { .pin = 13, .func = 3 },
0220 };
0221
0222 static const struct rza1_bidir_pin rza1h_bidir_pins_p8[] = {
0223 { .pin = 8, .func = 3 },
0224 { .pin = 9, .func = 3 },
0225 { .pin = 10, .func = 3 },
0226 { .pin = 11, .func = 3 },
0227 { .pin = 14, .func = 2 },
0228 { .pin = 15, .func = 2 },
0229 { .pin = 14, .func = 3 },
0230 { .pin = 15, .func = 3 },
0231 };
0232
0233 static const struct rza1_bidir_pin rza1h_bidir_pins_p9[] = {
0234 { .pin = 0, .func = 2 },
0235 { .pin = 1, .func = 2 },
0236 { .pin = 4, .func = 2 },
0237 { .pin = 5, .func = 2 },
0238 { .pin = 6, .func = 2 },
0239 { .pin = 7, .func = 2 },
0240 };
0241
0242 static const struct rza1_bidir_pin rza1h_bidir_pins_p11[] = {
0243 { .pin = 6, .func = 2 },
0244 { .pin = 7, .func = 2 },
0245 { .pin = 9, .func = 2 },
0246 { .pin = 6, .func = 4 },
0247 { .pin = 7, .func = 4 },
0248 { .pin = 9, .func = 4 },
0249 { .pin = 10, .func = 2 },
0250 { .pin = 11, .func = 2 },
0251 { .pin = 10, .func = 4 },
0252 { .pin = 11, .func = 4 },
0253 { .pin = 12, .func = 4 },
0254 { .pin = 13, .func = 4 },
0255 { .pin = 14, .func = 4 },
0256 { .pin = 15, .func = 4 },
0257 };
0258
0259 static const struct rza1_swio_pin rza1h_swio_pins[] = {
0260 { .port = 2, .pin = 7, .func = 4, .input = 0 },
0261 { .port = 2, .pin = 11, .func = 4, .input = 0 },
0262 { .port = 3, .pin = 7, .func = 3, .input = 0 },
0263 { .port = 3, .pin = 7, .func = 8, .input = 0 },
0264 { .port = 4, .pin = 7, .func = 5, .input = 0 },
0265 { .port = 4, .pin = 7, .func = 11, .input = 0 },
0266 { .port = 4, .pin = 15, .func = 6, .input = 0 },
0267 { .port = 5, .pin = 0, .func = 1, .input = 1 },
0268 { .port = 5, .pin = 1, .func = 1, .input = 1 },
0269 { .port = 5, .pin = 2, .func = 1, .input = 1 },
0270 { .port = 5, .pin = 3, .func = 1, .input = 1 },
0271 { .port = 5, .pin = 4, .func = 1, .input = 1 },
0272 { .port = 5, .pin = 5, .func = 1, .input = 1 },
0273 { .port = 5, .pin = 6, .func = 1, .input = 1 },
0274 { .port = 5, .pin = 7, .func = 1, .input = 1 },
0275 { .port = 7, .pin = 4, .func = 6, .input = 0 },
0276 { .port = 7, .pin = 11, .func = 2, .input = 0 },
0277 { .port = 8, .pin = 10, .func = 8, .input = 0 },
0278 { .port = 10, .pin = 15, .func = 2, .input = 0 },
0279 };
0280
0281 static const struct rza1_bidir_entry rza1h_bidir_entries[RZA1_NPORTS] = {
0282 [1] = { ARRAY_SIZE(rza1h_bidir_pins_p1), rza1h_bidir_pins_p1 },
0283 [2] = { ARRAY_SIZE(rza1h_bidir_pins_p2), rza1h_bidir_pins_p2 },
0284 [3] = { ARRAY_SIZE(rza1h_bidir_pins_p3), rza1h_bidir_pins_p3 },
0285 [4] = { ARRAY_SIZE(rza1h_bidir_pins_p4), rza1h_bidir_pins_p4 },
0286 [6] = { ARRAY_SIZE(rza1h_bidir_pins_p6), rza1h_bidir_pins_p6 },
0287 [7] = { ARRAY_SIZE(rza1h_bidir_pins_p7), rza1h_bidir_pins_p7 },
0288 [8] = { ARRAY_SIZE(rza1h_bidir_pins_p8), rza1h_bidir_pins_p8 },
0289 [9] = { ARRAY_SIZE(rza1h_bidir_pins_p9), rza1h_bidir_pins_p9 },
0290 [11] = { ARRAY_SIZE(rza1h_bidir_pins_p11), rza1h_bidir_pins_p11 },
0291 };
0292
0293 static const struct rza1_swio_entry rza1h_swio_entries[] = {
0294 [0] = { ARRAY_SIZE(rza1h_swio_pins), rza1h_swio_pins },
0295 };
0296
0297
0298 static const struct rza1_pinmux_conf rza1h_pmx_conf = {
0299 .bidir_entries = rza1h_bidir_entries,
0300 .swio_entries = rza1h_swio_entries,
0301 };
0302
0303
0304
0305
0306
0307 static const struct rza1_bidir_pin rza1l_bidir_pins_p1[] = {
0308 { .pin = 0, .func = 1 },
0309 { .pin = 1, .func = 1 },
0310 { .pin = 2, .func = 1 },
0311 { .pin = 3, .func = 1 },
0312 { .pin = 4, .func = 1 },
0313 { .pin = 5, .func = 1 },
0314 { .pin = 6, .func = 1 },
0315 { .pin = 7, .func = 1 },
0316 };
0317
0318 static const struct rza1_bidir_pin rza1l_bidir_pins_p3[] = {
0319 { .pin = 0, .func = 2 },
0320 { .pin = 1, .func = 2 },
0321 { .pin = 2, .func = 2 },
0322 { .pin = 4, .func = 2 },
0323 { .pin = 5, .func = 2 },
0324 { .pin = 10, .func = 2 },
0325 { .pin = 11, .func = 2 },
0326 { .pin = 12, .func = 2 },
0327 { .pin = 13, .func = 2 },
0328 };
0329
0330 static const struct rza1_bidir_pin rza1l_bidir_pins_p4[] = {
0331 { .pin = 1, .func = 4 },
0332 { .pin = 2, .func = 2 },
0333 { .pin = 3, .func = 2 },
0334 { .pin = 6, .func = 2 },
0335 { .pin = 7, .func = 2 },
0336 };
0337
0338 static const struct rza1_bidir_pin rza1l_bidir_pins_p5[] = {
0339 { .pin = 0, .func = 1 },
0340 { .pin = 1, .func = 1 },
0341 { .pin = 2, .func = 1 },
0342 { .pin = 3, .func = 1 },
0343 { .pin = 4, .func = 1 },
0344 { .pin = 5, .func = 1 },
0345 { .pin = 6, .func = 1 },
0346 { .pin = 7, .func = 1 },
0347 { .pin = 8, .func = 1 },
0348 { .pin = 9, .func = 1 },
0349 { .pin = 10, .func = 1 },
0350 { .pin = 11, .func = 1 },
0351 { .pin = 12, .func = 1 },
0352 { .pin = 13, .func = 1 },
0353 { .pin = 14, .func = 1 },
0354 { .pin = 15, .func = 1 },
0355 { .pin = 0, .func = 2 },
0356 { .pin = 1, .func = 2 },
0357 { .pin = 2, .func = 2 },
0358 { .pin = 3, .func = 2 },
0359 };
0360
0361 static const struct rza1_bidir_pin rza1l_bidir_pins_p6[] = {
0362 { .pin = 0, .func = 1 },
0363 { .pin = 1, .func = 1 },
0364 { .pin = 2, .func = 1 },
0365 { .pin = 3, .func = 1 },
0366 { .pin = 4, .func = 1 },
0367 { .pin = 5, .func = 1 },
0368 { .pin = 6, .func = 1 },
0369 { .pin = 7, .func = 1 },
0370 { .pin = 8, .func = 1 },
0371 { .pin = 9, .func = 1 },
0372 { .pin = 10, .func = 1 },
0373 { .pin = 11, .func = 1 },
0374 { .pin = 12, .func = 1 },
0375 { .pin = 13, .func = 1 },
0376 { .pin = 14, .func = 1 },
0377 { .pin = 15, .func = 1 },
0378 };
0379
0380 static const struct rza1_bidir_pin rza1l_bidir_pins_p7[] = {
0381 { .pin = 2, .func = 2 },
0382 { .pin = 3, .func = 2 },
0383 { .pin = 5, .func = 2 },
0384 { .pin = 6, .func = 2 },
0385 { .pin = 7, .func = 2 },
0386 { .pin = 2, .func = 3 },
0387 { .pin = 3, .func = 3 },
0388 { .pin = 5, .func = 3 },
0389 { .pin = 6, .func = 3 },
0390 { .pin = 7, .func = 3 },
0391 };
0392
0393 static const struct rza1_bidir_pin rza1l_bidir_pins_p9[] = {
0394 { .pin = 1, .func = 2 },
0395 { .pin = 0, .func = 3 },
0396 { .pin = 1, .func = 3 },
0397 { .pin = 3, .func = 3 },
0398 { .pin = 4, .func = 3 },
0399 { .pin = 5, .func = 3 },
0400 };
0401
0402 static const struct rza1_swio_pin rza1l_swio_pins[] = {
0403 { .port = 2, .pin = 8, .func = 2, .input = 0 },
0404 { .port = 5, .pin = 6, .func = 3, .input = 0 },
0405 { .port = 6, .pin = 6, .func = 3, .input = 0 },
0406 { .port = 6, .pin = 10, .func = 3, .input = 0 },
0407 { .port = 7, .pin = 10, .func = 2, .input = 0 },
0408 { .port = 8, .pin = 2, .func = 3, .input = 0 },
0409 };
0410
0411 static const struct rza1_bidir_entry rza1l_bidir_entries[RZA1_NPORTS] = {
0412 [1] = { ARRAY_SIZE(rza1l_bidir_pins_p1), rza1l_bidir_pins_p1 },
0413 [3] = { ARRAY_SIZE(rza1l_bidir_pins_p3), rza1l_bidir_pins_p3 },
0414 [4] = { ARRAY_SIZE(rza1l_bidir_pins_p4), rza1l_bidir_pins_p4 },
0415 [5] = { ARRAY_SIZE(rza1l_bidir_pins_p4), rza1l_bidir_pins_p5 },
0416 [6] = { ARRAY_SIZE(rza1l_bidir_pins_p6), rza1l_bidir_pins_p6 },
0417 [7] = { ARRAY_SIZE(rza1l_bidir_pins_p7), rza1l_bidir_pins_p7 },
0418 [9] = { ARRAY_SIZE(rza1l_bidir_pins_p9), rza1l_bidir_pins_p9 },
0419 };
0420
0421 static const struct rza1_swio_entry rza1l_swio_entries[] = {
0422 [0] = { ARRAY_SIZE(rza1l_swio_pins), rza1l_swio_pins },
0423 };
0424
0425
0426 static const struct rza1_pinmux_conf rza1l_pmx_conf = {
0427 .bidir_entries = rza1l_bidir_entries,
0428 .swio_entries = rza1l_swio_entries,
0429 };
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444 struct rza1_mux_conf {
0445 u16 id;
0446 u8 port;
0447 u8 pin;
0448 u8 mux_func;
0449 u8 mux_flags;
0450 u8 value;
0451 };
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463 struct rza1_port {
0464 spinlock_t lock;
0465 unsigned int id;
0466 void __iomem *base;
0467 struct pinctrl_pin_desc *pins;
0468 };
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483 struct rza1_pinctrl {
0484 struct device *dev;
0485
0486 struct mutex mutex;
0487
0488 void __iomem *base;
0489
0490 unsigned int nport;
0491 struct rza1_port *ports;
0492
0493 struct pinctrl_pin_desc *pins;
0494 struct pinctrl_desc desc;
0495 struct pinctrl_dev *pctl;
0496
0497 const void *data;
0498 };
0499
0500
0501
0502
0503 static inline bool rza1_pinmux_get_bidir(unsigned int port,
0504 unsigned int pin,
0505 unsigned int func,
0506 const struct rza1_bidir_entry *table)
0507 {
0508 const struct rza1_bidir_entry *entry = &table[port];
0509 const struct rza1_bidir_pin *bidir_pin;
0510 unsigned int i;
0511
0512 for (i = 0; i < entry->npins; ++i) {
0513 bidir_pin = &entry->pins[i];
0514 if (bidir_pin->pin == pin && bidir_pin->func == func)
0515 return true;
0516 }
0517
0518 return false;
0519 }
0520
0521 static inline int rza1_pinmux_get_swio(unsigned int port,
0522 unsigned int pin,
0523 unsigned int func,
0524 const struct rza1_swio_entry *table)
0525 {
0526 const struct rza1_swio_pin *swio_pin;
0527 unsigned int i;
0528
0529
0530 for (i = 0; i < table->npins; ++i) {
0531 swio_pin = &table->pins[i];
0532 if (swio_pin->port == port && swio_pin->pin == pin &&
0533 swio_pin->func == func)
0534 return swio_pin->input;
0535 }
0536
0537 return -ENOENT;
0538 }
0539
0540
0541
0542
0543 static unsigned int rza1_pinmux_get_flags(unsigned int port, unsigned int pin,
0544 unsigned int func,
0545 struct rza1_pinctrl *rza1_pctl)
0546
0547 {
0548 const struct rza1_pinmux_conf *pmx_conf = rza1_pctl->data;
0549 const struct rza1_bidir_entry *bidir_entries = pmx_conf->bidir_entries;
0550 const struct rza1_swio_entry *swio_entries = pmx_conf->swio_entries;
0551 unsigned int pmx_flags = 0;
0552 int ret;
0553
0554 if (rza1_pinmux_get_bidir(port, pin, func, bidir_entries))
0555 pmx_flags |= MUX_FLAGS_BIDIR;
0556
0557 ret = rza1_pinmux_get_swio(port, pin, func, swio_entries);
0558 if (ret == 0)
0559 pmx_flags |= MUX_FLAGS_SWIO_OUTPUT;
0560 else if (ret > 0)
0561 pmx_flags |= MUX_FLAGS_SWIO_INPUT;
0562
0563 return pmx_flags;
0564 }
0565
0566
0567
0568
0569
0570
0571
0572
0573
0574 static inline void rza1_set_bit(struct rza1_port *port, unsigned int reg,
0575 unsigned int bit, bool set)
0576 {
0577 void __iomem *mem = RZA1_ADDR(port->base, reg, port->id);
0578 u16 val = ioread16(mem);
0579
0580 if (set)
0581 val |= BIT(bit);
0582 else
0583 val &= ~BIT(bit);
0584
0585 iowrite16(val, mem);
0586 }
0587
0588 static inline unsigned int rza1_get_bit(struct rza1_port *port,
0589 unsigned int reg, unsigned int bit)
0590 {
0591 void __iomem *mem = RZA1_ADDR(port->base, reg, port->id);
0592
0593 return ioread16(mem) & BIT(bit);
0594 }
0595
0596
0597
0598
0599
0600
0601
0602
0603
0604
0605
0606
0607 static void rza1_pin_reset(struct rza1_port *port, unsigned int pin)
0608 {
0609 unsigned long irqflags;
0610
0611 spin_lock_irqsave(&port->lock, irqflags);
0612 rza1_set_bit(port, RZA1_PIBC_REG, pin, 0);
0613 rza1_set_bit(port, RZA1_PBDC_REG, pin, 0);
0614
0615 rza1_set_bit(port, RZA1_PM_REG, pin, 1);
0616 rza1_set_bit(port, RZA1_PMC_REG, pin, 0);
0617 rza1_set_bit(port, RZA1_PIPC_REG, pin, 0);
0618 spin_unlock_irqrestore(&port->lock, irqflags);
0619 }
0620
0621
0622
0623
0624
0625
0626
0627
0628
0629
0630
0631 static inline void rza1_pin_set_direction(struct rza1_port *port,
0632 unsigned int pin, bool input)
0633 {
0634 unsigned long irqflags;
0635
0636 spin_lock_irqsave(&port->lock, irqflags);
0637
0638 rza1_set_bit(port, RZA1_PIBC_REG, pin, 1);
0639 if (input) {
0640 rza1_set_bit(port, RZA1_PM_REG, pin, 1);
0641 rza1_set_bit(port, RZA1_PBDC_REG, pin, 0);
0642 } else {
0643 rza1_set_bit(port, RZA1_PM_REG, pin, 0);
0644 rza1_set_bit(port, RZA1_PBDC_REG, pin, 1);
0645 }
0646
0647 spin_unlock_irqrestore(&port->lock, irqflags);
0648 }
0649
0650 static inline void rza1_pin_set(struct rza1_port *port, unsigned int pin,
0651 unsigned int value)
0652 {
0653 unsigned long irqflags;
0654
0655 spin_lock_irqsave(&port->lock, irqflags);
0656 rza1_set_bit(port, RZA1_P_REG, pin, !!value);
0657 spin_unlock_irqrestore(&port->lock, irqflags);
0658 }
0659
0660 static inline int rza1_pin_get(struct rza1_port *port, unsigned int pin)
0661 {
0662 return rza1_get_bit(port, RZA1_PPR_REG, pin);
0663 }
0664
0665
0666
0667
0668
0669
0670
0671 static int rza1_pin_mux_single(struct rza1_pinctrl *rza1_pctl,
0672 struct rza1_mux_conf *mux_conf)
0673 {
0674 struct rza1_port *port = &rza1_pctl->ports[mux_conf->port];
0675 unsigned int pin = mux_conf->pin;
0676 u8 mux_func = mux_conf->mux_func;
0677 u8 mux_flags = mux_conf->mux_flags;
0678 u8 mux_flags_from_table;
0679
0680 rza1_pin_reset(port, pin);
0681
0682
0683 mux_flags_from_table = rza1_pinmux_get_flags(port->id, pin, mux_func,
0684 rza1_pctl);
0685 if (mux_flags)
0686 mux_flags |= (mux_flags_from_table & MUX_FLAGS_BIDIR);
0687 else
0688 mux_flags = mux_flags_from_table;
0689
0690 if (mux_flags & MUX_FLAGS_BIDIR)
0691 rza1_set_bit(port, RZA1_PBDC_REG, pin, 1);
0692
0693
0694
0695
0696
0697
0698
0699
0700
0701
0702
0703
0704
0705
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715 mux_func -= 1;
0716 rza1_set_bit(port, RZA1_PFC_REG, pin, mux_func & MUX_FUNC_PFC_MASK);
0717 rza1_set_bit(port, RZA1_PFCE_REG, pin, mux_func & MUX_FUNC_PFCE_MASK);
0718 rza1_set_bit(port, RZA1_PFCEA_REG, pin, mux_func & MUX_FUNC_PFCEA_MASK);
0719
0720
0721
0722
0723
0724
0725
0726 if (mux_flags & (MUX_FLAGS_SWIO_INPUT | MUX_FLAGS_SWIO_OUTPUT))
0727 rza1_set_bit(port, RZA1_PM_REG, pin,
0728 mux_flags & MUX_FLAGS_SWIO_INPUT);
0729 else
0730 rza1_set_bit(port, RZA1_PIPC_REG, pin, 1);
0731
0732 rza1_set_bit(port, RZA1_PMC_REG, pin, 1);
0733
0734 return 0;
0735 }
0736
0737
0738
0739
0740
0741
0742
0743
0744
0745
0746
0747
0748
0749
0750
0751 static int rza1_gpio_request(struct gpio_chip *chip, unsigned int gpio)
0752 {
0753 struct rza1_port *port = gpiochip_get_data(chip);
0754
0755 rza1_pin_reset(port, gpio);
0756
0757 return 0;
0758 }
0759
0760
0761
0762
0763
0764
0765
0766
0767
0768
0769
0770 static void rza1_gpio_free(struct gpio_chip *chip, unsigned int gpio)
0771 {
0772 struct rza1_port *port = gpiochip_get_data(chip);
0773
0774 rza1_pin_reset(port, gpio);
0775 }
0776
0777 static int rza1_gpio_get_direction(struct gpio_chip *chip, unsigned int gpio)
0778 {
0779 struct rza1_port *port = gpiochip_get_data(chip);
0780
0781 if (rza1_get_bit(port, RZA1_PM_REG, gpio))
0782 return GPIO_LINE_DIRECTION_IN;
0783
0784 return GPIO_LINE_DIRECTION_OUT;
0785 }
0786
0787 static int rza1_gpio_direction_input(struct gpio_chip *chip,
0788 unsigned int gpio)
0789 {
0790 struct rza1_port *port = gpiochip_get_data(chip);
0791
0792 rza1_pin_set_direction(port, gpio, true);
0793
0794 return 0;
0795 }
0796
0797 static int rza1_gpio_direction_output(struct gpio_chip *chip,
0798 unsigned int gpio,
0799 int value)
0800 {
0801 struct rza1_port *port = gpiochip_get_data(chip);
0802
0803
0804 rza1_pin_set(port, gpio, value);
0805 rza1_pin_set_direction(port, gpio, false);
0806
0807 return 0;
0808 }
0809
0810
0811
0812
0813
0814
0815
0816
0817
0818
0819
0820 static int rza1_gpio_get(struct gpio_chip *chip, unsigned int gpio)
0821 {
0822 struct rza1_port *port = gpiochip_get_data(chip);
0823
0824 return rza1_pin_get(port, gpio);
0825 }
0826
0827 static void rza1_gpio_set(struct gpio_chip *chip, unsigned int gpio,
0828 int value)
0829 {
0830 struct rza1_port *port = gpiochip_get_data(chip);
0831
0832 rza1_pin_set(port, gpio, value);
0833 }
0834
0835 static const struct gpio_chip rza1_gpiochip_template = {
0836 .request = rza1_gpio_request,
0837 .free = rza1_gpio_free,
0838 .get_direction = rza1_gpio_get_direction,
0839 .direction_input = rza1_gpio_direction_input,
0840 .direction_output = rza1_gpio_direction_output,
0841 .get = rza1_gpio_get,
0842 .set = rza1_gpio_set,
0843 };
0844
0845
0846
0847
0848
0849
0850
0851
0852
0853
0854 static int rza1_dt_node_pin_count(struct device_node *np)
0855 {
0856 struct device_node *child;
0857 struct property *of_pins;
0858 unsigned int npins;
0859
0860 of_pins = of_find_property(np, "pinmux", NULL);
0861 if (of_pins)
0862 return of_pins->length / sizeof(u32);
0863
0864 npins = 0;
0865 for_each_child_of_node(np, child) {
0866 of_pins = of_find_property(child, "pinmux", NULL);
0867 if (!of_pins) {
0868 of_node_put(child);
0869 return -EINVAL;
0870 }
0871
0872 npins += of_pins->length / sizeof(u32);
0873 }
0874
0875 return npins;
0876 }
0877
0878
0879
0880
0881
0882
0883
0884
0885
0886 static int rza1_parse_pinmux_node(struct rza1_pinctrl *rza1_pctl,
0887 struct device_node *np,
0888 struct rza1_mux_conf *mux_confs,
0889 unsigned int *grpins)
0890 {
0891 struct pinctrl_dev *pctldev = rza1_pctl->pctl;
0892 char const *prop_name = "pinmux";
0893 unsigned long *pin_configs;
0894 unsigned int npin_configs;
0895 struct property *of_pins;
0896 unsigned int npins;
0897 u8 pinmux_flags;
0898 unsigned int i;
0899 int ret;
0900
0901 of_pins = of_find_property(np, prop_name, NULL);
0902 if (!of_pins) {
0903 dev_dbg(rza1_pctl->dev, "Missing %s property\n", prop_name);
0904 return -ENOENT;
0905 }
0906 npins = of_pins->length / sizeof(u32);
0907
0908
0909
0910
0911
0912 ret = pinconf_generic_parse_dt_config(np, pctldev, &pin_configs,
0913 &npin_configs);
0914 if (ret) {
0915 dev_err(rza1_pctl->dev,
0916 "Unable to parse pin configuration options for %pOFn\n",
0917 np);
0918 return ret;
0919 }
0920
0921
0922
0923
0924
0925
0926 pinmux_flags = 0;
0927 for (i = 0; i < npin_configs && pinmux_flags == 0; i++)
0928 switch (pinconf_to_config_param(pin_configs[i])) {
0929 case PIN_CONFIG_INPUT_ENABLE:
0930 pinmux_flags |= MUX_FLAGS_SWIO_INPUT;
0931 break;
0932 case PIN_CONFIG_OUTPUT:
0933 case PIN_CONFIG_OUTPUT_ENABLE:
0934 pinmux_flags |= MUX_FLAGS_SWIO_OUTPUT;
0935 break;
0936 default:
0937 break;
0938
0939 }
0940
0941 kfree(pin_configs);
0942
0943
0944 for (i = 0; i < npins; ++i) {
0945 u32 of_pinconf;
0946 struct rza1_mux_conf *mux_conf = &mux_confs[i];
0947
0948 ret = of_property_read_u32_index(np, prop_name, i, &of_pinconf);
0949 if (ret)
0950 return ret;
0951
0952 mux_conf->id = of_pinconf & MUX_PIN_ID_MASK;
0953 mux_conf->port = RZA1_PIN_ID_TO_PORT(mux_conf->id);
0954 mux_conf->pin = RZA1_PIN_ID_TO_PIN(mux_conf->id);
0955 mux_conf->mux_func = MUX_FUNC(of_pinconf);
0956 mux_conf->mux_flags = pinmux_flags;
0957
0958 if (mux_conf->port >= RZA1_NPORTS ||
0959 mux_conf->pin >= RZA1_PINS_PER_PORT) {
0960 dev_err(rza1_pctl->dev,
0961 "Wrong port %u pin %u for %s property\n",
0962 mux_conf->port, mux_conf->pin, prop_name);
0963 return -EINVAL;
0964 }
0965
0966 grpins[i] = mux_conf->id;
0967 }
0968
0969 return npins;
0970 }
0971
0972
0973
0974
0975
0976
0977
0978
0979
0980
0981
0982 static int rza1_dt_node_to_map(struct pinctrl_dev *pctldev,
0983 struct device_node *np,
0984 struct pinctrl_map **map,
0985 unsigned int *num_maps)
0986 {
0987 struct rza1_pinctrl *rza1_pctl = pinctrl_dev_get_drvdata(pctldev);
0988 struct rza1_mux_conf *mux_confs, *mux_conf;
0989 unsigned int *grpins, *grpin;
0990 struct device_node *child;
0991 const char *grpname;
0992 const char **fngrps;
0993 int ret, npins;
0994 int gsel, fsel;
0995
0996 npins = rza1_dt_node_pin_count(np);
0997 if (npins < 0) {
0998 dev_err(rza1_pctl->dev, "invalid pinmux node structure\n");
0999 return -EINVAL;
1000 }
1001
1002
1003
1004
1005
1006
1007
1008 mux_confs = devm_kcalloc(rza1_pctl->dev, npins, sizeof(*mux_confs),
1009 GFP_KERNEL);
1010 grpins = devm_kcalloc(rza1_pctl->dev, npins, sizeof(*grpins),
1011 GFP_KERNEL);
1012 fngrps = devm_kzalloc(rza1_pctl->dev, sizeof(*fngrps), GFP_KERNEL);
1013
1014 if (!mux_confs || !grpins || !fngrps)
1015 return -ENOMEM;
1016
1017
1018
1019
1020
1021
1022 mux_conf = &mux_confs[0];
1023 grpin = &grpins[0];
1024
1025 ret = rza1_parse_pinmux_node(rza1_pctl, np, mux_conf, grpin);
1026 if (ret == -ENOENT)
1027 for_each_child_of_node(np, child) {
1028 ret = rza1_parse_pinmux_node(rza1_pctl, child, mux_conf,
1029 grpin);
1030 if (ret < 0) {
1031 of_node_put(child);
1032 return ret;
1033 }
1034
1035 grpin += ret;
1036 mux_conf += ret;
1037 }
1038 else if (ret < 0)
1039 return ret;
1040
1041
1042 grpname = np->name;
1043 fngrps[0] = grpname;
1044
1045 mutex_lock(&rza1_pctl->mutex);
1046 gsel = pinctrl_generic_add_group(pctldev, grpname, grpins, npins,
1047 NULL);
1048 if (gsel < 0) {
1049 mutex_unlock(&rza1_pctl->mutex);
1050 return gsel;
1051 }
1052
1053 fsel = pinmux_generic_add_function(pctldev, grpname, fngrps, 1,
1054 mux_confs);
1055 if (fsel < 0) {
1056 ret = fsel;
1057 goto remove_group;
1058 }
1059
1060 dev_info(rza1_pctl->dev, "Parsed function and group %s with %d pins\n",
1061 grpname, npins);
1062
1063
1064 *num_maps = 0;
1065 *map = kzalloc(sizeof(**map), GFP_KERNEL);
1066 if (!*map) {
1067 ret = -ENOMEM;
1068 goto remove_function;
1069 }
1070
1071 (*map)->type = PIN_MAP_TYPE_MUX_GROUP;
1072 (*map)->data.mux.group = np->name;
1073 (*map)->data.mux.function = np->name;
1074 *num_maps = 1;
1075 mutex_unlock(&rza1_pctl->mutex);
1076
1077 return 0;
1078
1079 remove_function:
1080 pinmux_generic_remove_function(pctldev, fsel);
1081
1082 remove_group:
1083 pinctrl_generic_remove_group(pctldev, gsel);
1084 mutex_unlock(&rza1_pctl->mutex);
1085
1086 dev_info(rza1_pctl->dev, "Unable to parse function and group %s\n",
1087 grpname);
1088
1089 return ret;
1090 }
1091
1092 static void rza1_dt_free_map(struct pinctrl_dev *pctldev,
1093 struct pinctrl_map *map, unsigned int num_maps)
1094 {
1095 kfree(map);
1096 }
1097
1098 static const struct pinctrl_ops rza1_pinctrl_ops = {
1099 .get_groups_count = pinctrl_generic_get_group_count,
1100 .get_group_name = pinctrl_generic_get_group_name,
1101 .get_group_pins = pinctrl_generic_get_group_pins,
1102 .dt_node_to_map = rza1_dt_node_to_map,
1103 .dt_free_map = rza1_dt_free_map,
1104 };
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117 static int rza1_set_mux(struct pinctrl_dev *pctldev, unsigned int selector,
1118 unsigned int group)
1119 {
1120 struct rza1_pinctrl *rza1_pctl = pinctrl_dev_get_drvdata(pctldev);
1121 struct rza1_mux_conf *mux_confs;
1122 struct function_desc *func;
1123 struct group_desc *grp;
1124 int i;
1125
1126 grp = pinctrl_generic_get_group(pctldev, group);
1127 if (!grp)
1128 return -EINVAL;
1129
1130 func = pinmux_generic_get_function(pctldev, selector);
1131 if (!func)
1132 return -EINVAL;
1133
1134 mux_confs = (struct rza1_mux_conf *)func->data;
1135 for (i = 0; i < grp->num_pins; ++i) {
1136 int ret;
1137
1138 ret = rza1_pin_mux_single(rza1_pctl, &mux_confs[i]);
1139 if (ret)
1140 return ret;
1141 }
1142
1143 return 0;
1144 }
1145
1146 static const struct pinmux_ops rza1_pinmux_ops = {
1147 .get_functions_count = pinmux_generic_get_function_count,
1148 .get_function_name = pinmux_generic_get_function_name,
1149 .get_function_groups = pinmux_generic_get_function_groups,
1150 .set_mux = rza1_set_mux,
1151 .strict = true,
1152 };
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169 static int rza1_parse_gpiochip(struct rza1_pinctrl *rza1_pctl,
1170 struct fwnode_handle *fwnode,
1171 struct gpio_chip *chip,
1172 struct pinctrl_gpio_range *range)
1173 {
1174 const char *list_name = "gpio-ranges";
1175 struct fwnode_reference_args args;
1176 unsigned int gpioport;
1177 u32 pinctrl_base;
1178 int ret;
1179
1180 ret = fwnode_property_get_reference_args(fwnode, list_name, NULL, 3, 0, &args);
1181 if (ret) {
1182 dev_err(rza1_pctl->dev, "Unable to parse %s list property\n",
1183 list_name);
1184 return ret;
1185 }
1186
1187
1188
1189
1190
1191 pinctrl_base = args.args[1];
1192 gpioport = RZA1_PIN_ID_TO_PORT(pinctrl_base);
1193 if (gpioport >= RZA1_NPORTS) {
1194 dev_err(rza1_pctl->dev,
1195 "Invalid values in property %s\n", list_name);
1196 return -EINVAL;
1197 }
1198
1199 *chip = rza1_gpiochip_template;
1200 chip->base = -1;
1201 chip->ngpio = args.args[2];
1202 chip->label = devm_kasprintf(rza1_pctl->dev, GFP_KERNEL, "%pfwP", fwnode);
1203 if (!chip->label)
1204 return -ENOMEM;
1205
1206 chip->fwnode = fwnode;
1207 chip->parent = rza1_pctl->dev;
1208
1209 range->id = gpioport;
1210 range->name = chip->label;
1211 range->pin_base = range->base = pinctrl_base;
1212 range->npins = args.args[2];
1213 range->gc = chip;
1214
1215 ret = devm_gpiochip_add_data(rza1_pctl->dev, chip,
1216 &rza1_pctl->ports[gpioport]);
1217 if (ret)
1218 return ret;
1219
1220 pinctrl_add_gpio_range(rza1_pctl->pctl, range);
1221
1222 dev_dbg(rza1_pctl->dev, "Parsed gpiochip %s with %d pins\n",
1223 chip->label, chip->ngpio);
1224
1225 return 0;
1226 }
1227
1228
1229
1230
1231
1232
1233 static int rza1_gpio_register(struct rza1_pinctrl *rza1_pctl)
1234 {
1235 struct pinctrl_gpio_range *gpio_ranges;
1236 struct gpio_chip *gpio_chips;
1237 struct fwnode_handle *child;
1238 unsigned int ngpiochips;
1239 unsigned int i;
1240 int ret;
1241
1242 ngpiochips = gpiochip_node_count(rza1_pctl->dev);
1243 if (ngpiochips == 0) {
1244 dev_dbg(rza1_pctl->dev, "No gpiochip registered\n");
1245 return 0;
1246 }
1247
1248 gpio_chips = devm_kcalloc(rza1_pctl->dev, ngpiochips,
1249 sizeof(*gpio_chips), GFP_KERNEL);
1250 gpio_ranges = devm_kcalloc(rza1_pctl->dev, ngpiochips,
1251 sizeof(*gpio_ranges), GFP_KERNEL);
1252 if (!gpio_chips || !gpio_ranges)
1253 return -ENOMEM;
1254
1255 i = 0;
1256 for_each_gpiochip_node(rza1_pctl->dev, child) {
1257 ret = rza1_parse_gpiochip(rza1_pctl, child, &gpio_chips[i],
1258 &gpio_ranges[i]);
1259 if (ret) {
1260 fwnode_handle_put(child);
1261 return ret;
1262 }
1263
1264 ++i;
1265 }
1266
1267 dev_info(rza1_pctl->dev, "Registered %u gpio controllers\n", i);
1268
1269 return 0;
1270 }
1271
1272
1273
1274
1275
1276
1277
1278 static int rza1_pinctrl_register(struct rza1_pinctrl *rza1_pctl)
1279 {
1280 struct pinctrl_pin_desc *pins;
1281 struct rza1_port *ports;
1282 unsigned int i;
1283 int ret;
1284
1285 pins = devm_kcalloc(rza1_pctl->dev, RZA1_NPINS, sizeof(*pins),
1286 GFP_KERNEL);
1287 ports = devm_kcalloc(rza1_pctl->dev, RZA1_NPORTS, sizeof(*ports),
1288 GFP_KERNEL);
1289 if (!pins || !ports)
1290 return -ENOMEM;
1291
1292 rza1_pctl->pins = pins;
1293 rza1_pctl->desc.pins = pins;
1294 rza1_pctl->desc.npins = RZA1_NPINS;
1295 rza1_pctl->ports = ports;
1296
1297 for (i = 0; i < RZA1_NPINS; ++i) {
1298 unsigned int pin = RZA1_PIN_ID_TO_PIN(i);
1299 unsigned int port = RZA1_PIN_ID_TO_PORT(i);
1300
1301 pins[i].number = i;
1302 pins[i].name = devm_kasprintf(rza1_pctl->dev, GFP_KERNEL,
1303 "P%u-%u", port, pin);
1304 if (!pins[i].name)
1305 return -ENOMEM;
1306
1307 if (i % RZA1_PINS_PER_PORT == 0) {
1308
1309
1310
1311
1312 unsigned int port_id = RZA1_PIN_ID_TO_PORT(i);
1313
1314 ports[port_id].id = port_id;
1315 ports[port_id].base = rza1_pctl->base;
1316 ports[port_id].pins = &pins[i];
1317 spin_lock_init(&ports[port_id].lock);
1318 }
1319 }
1320
1321 ret = devm_pinctrl_register_and_init(rza1_pctl->dev, &rza1_pctl->desc,
1322 rza1_pctl, &rza1_pctl->pctl);
1323 if (ret) {
1324 dev_err(rza1_pctl->dev,
1325 "RZ/A1 pin controller registration failed\n");
1326 return ret;
1327 }
1328
1329 ret = pinctrl_enable(rza1_pctl->pctl);
1330 if (ret) {
1331 dev_err(rza1_pctl->dev,
1332 "RZ/A1 pin controller failed to start\n");
1333 return ret;
1334 }
1335
1336 ret = rza1_gpio_register(rza1_pctl);
1337 if (ret) {
1338 dev_err(rza1_pctl->dev, "RZ/A1 GPIO registration failed\n");
1339 return ret;
1340 }
1341
1342 return 0;
1343 }
1344
1345 static int rza1_pinctrl_probe(struct platform_device *pdev)
1346 {
1347 struct rza1_pinctrl *rza1_pctl;
1348 int ret;
1349
1350 rza1_pctl = devm_kzalloc(&pdev->dev, sizeof(*rza1_pctl), GFP_KERNEL);
1351 if (!rza1_pctl)
1352 return -ENOMEM;
1353
1354 rza1_pctl->dev = &pdev->dev;
1355
1356 rza1_pctl->base = devm_platform_ioremap_resource(pdev, 0);
1357 if (IS_ERR(rza1_pctl->base))
1358 return PTR_ERR(rza1_pctl->base);
1359
1360 mutex_init(&rza1_pctl->mutex);
1361
1362 platform_set_drvdata(pdev, rza1_pctl);
1363
1364 rza1_pctl->desc.name = DRIVER_NAME;
1365 rza1_pctl->desc.pctlops = &rza1_pinctrl_ops;
1366 rza1_pctl->desc.pmxops = &rza1_pinmux_ops;
1367 rza1_pctl->desc.owner = THIS_MODULE;
1368 rza1_pctl->data = of_device_get_match_data(&pdev->dev);
1369
1370 ret = rza1_pinctrl_register(rza1_pctl);
1371 if (ret)
1372 return ret;
1373
1374 dev_info(&pdev->dev,
1375 "RZ/A1 pin controller and gpio successfully registered\n");
1376
1377 return 0;
1378 }
1379
1380 static const struct of_device_id rza1_pinctrl_of_match[] = {
1381 {
1382
1383 .compatible = "renesas,r7s72100-ports",
1384 .data = &rza1h_pmx_conf,
1385 },
1386 {
1387
1388 .compatible = "renesas,r7s72102-ports",
1389 .data = &rza1l_pmx_conf,
1390 },
1391 { }
1392 };
1393
1394 static struct platform_driver rza1_pinctrl_driver = {
1395 .driver = {
1396 .name = DRIVER_NAME,
1397 .of_match_table = rza1_pinctrl_of_match,
1398 },
1399 .probe = rza1_pinctrl_probe,
1400 };
1401
1402 static int __init rza1_pinctrl_init(void)
1403 {
1404 return platform_driver_register(&rza1_pinctrl_driver);
1405 }
1406 core_initcall(rza1_pinctrl_init);
1407
1408 MODULE_AUTHOR("Jacopo Mondi <jacopo+renesas@jmondi.org");
1409 MODULE_DESCRIPTION("Pin and gpio controller driver for Reneas RZ/A1 SoC");
1410 MODULE_LICENSE("GPL v2");