blob: 07fcc3d39310f7f7162dd8fba3bbe95acaddbecc [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Marek Vasut3066a062017-09-15 21:13:55 +02002/*
3 * Pin Control driver for SuperH Pin Function Controller.
4 *
5 * Authors: Magnus Damm, Paul Mundt, Laurent Pinchart
6 *
7 * Copyright (C) 2008 Magnus Damm
8 * Copyright (C) 2009 - 2012 Paul Mundt
9 * Copyright (C) 2017 Marek Vasut
Marek Vasut3066a062017-09-15 21:13:55 +020010 */
11
12#define DRV_NAME "sh-pfc"
13
14#include <common.h>
15#include <dm.h>
16#include <errno.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070018#include <dm/devres.h>
Marek Vasut3066a062017-09-15 21:13:55 +020019#include <dm/pinctrl.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060020#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060021#include <linux/bug.h>
Marek Vasut3066a062017-09-15 21:13:55 +020022#include <linux/io.h>
23#include <linux/sizes.h>
24
25#include "sh_pfc.h"
26
Marek Vasut3066a062017-09-15 21:13:55 +020027enum sh_pfc_model {
Marek Vasutc40f2d62018-01-17 22:18:59 +010028 SH_PFC_R8A7790 = 0,
Marek Vasut06ef9e82018-01-17 17:14:45 +010029 SH_PFC_R8A7791,
Marek Vasut1ef39302018-01-17 22:29:50 +010030 SH_PFC_R8A7792,
Marek Vasut06ef9e82018-01-17 17:14:45 +010031 SH_PFC_R8A7793,
Marek Vasut4dd88d52018-01-17 22:33:59 +010032 SH_PFC_R8A7794,
Marek Vasutc40f2d62018-01-17 22:18:59 +010033 SH_PFC_R8A7795,
Marek Vasut3066a062017-09-15 21:13:55 +020034 SH_PFC_R8A7796,
Adam Ford96980fb2020-06-30 09:30:09 -050035 SH_PFC_R8A774A1,
Biju Dasd1d78882020-10-28 10:34:21 +000036 SH_PFC_R8A774B1,
Lad Prabhakar53b88b92021-03-15 22:24:04 +000037 SH_PFC_R8A774C0,
Biju Das121bd002020-10-28 10:34:22 +000038 SH_PFC_R8A774E1,
Marek Vasut72269e02019-03-04 01:32:44 +010039 SH_PFC_R8A77965,
Marek Vasuta0e11e52017-10-09 20:57:29 +020040 SH_PFC_R8A77970,
Marek Vasuta6a7f482019-07-29 19:59:44 +020041 SH_PFC_R8A77980,
Marek Vasut68a77042018-04-26 13:09:20 +020042 SH_PFC_R8A77990,
Marek Vasut7d35e642017-10-08 20:57:37 +020043 SH_PFC_R8A77995,
Marek Vasut3066a062017-09-15 21:13:55 +020044};
45
46struct sh_pfc_pin_config {
47 u32 type;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +000048 const char *name;
Marek Vasut3066a062017-09-15 21:13:55 +020049};
50
51struct sh_pfc_pinctrl {
52 struct sh_pfc *pfc;
53
54 struct sh_pfc_pin_config *configs;
Marek Vasut3066a062017-09-15 21:13:55 +020055};
56
57struct sh_pfc_pin_range {
58 u16 start;
59 u16 end;
60};
61
62struct sh_pfc_pinctrl_priv {
63 struct sh_pfc pfc;
64 struct sh_pfc_pinctrl pmx;
65};
66
67int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
68{
69 unsigned int offset;
70 unsigned int i;
71
72 for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
73 const struct sh_pfc_pin_range *range = &pfc->ranges[i];
74
75 if (pin <= range->end)
76 return pin >= range->start
77 ? offset + pin - range->start : -1;
78
79 offset += range->end - range->start + 1;
80 }
81
82 return -EINVAL;
83}
84
85static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
86{
87 if (enum_id < r->begin)
88 return 0;
89
90 if (enum_id > r->end)
91 return 0;
92
93 return 1;
94}
95
96u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width)
97{
98 switch (reg_width) {
99 case 8:
100 return readb(mapped_reg);
101 case 16:
102 return readw(mapped_reg);
103 case 32:
104 return readl(mapped_reg);
105 }
106
107 BUG();
108 return 0;
109}
110
111void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width,
112 u32 data)
113{
114 switch (reg_width) {
115 case 8:
116 writeb(data, mapped_reg);
117 return;
118 case 16:
119 writew(data, mapped_reg);
120 return;
121 case 32:
122 writel(data, mapped_reg);
123 return;
124 }
125
126 BUG();
127}
128
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200129u32 sh_pfc_read(struct sh_pfc *pfc, u32 reg)
Marek Vasut3066a062017-09-15 21:13:55 +0200130{
Marek Vasut068a90b2018-06-19 06:13:42 +0200131 return sh_pfc_read_raw_reg((void __iomem *)(uintptr_t)reg, 32);
Marek Vasut3066a062017-09-15 21:13:55 +0200132}
133
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200134void sh_pfc_write(struct sh_pfc *pfc, u32 reg, u32 data)
Marek Vasut3066a062017-09-15 21:13:55 +0200135{
136 void __iomem *unlock_reg =
137 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
138
139 if (pfc->info->unlock_reg)
140 sh_pfc_write_raw_reg(unlock_reg, 32, ~data);
141
Marek Vasut068a90b2018-06-19 06:13:42 +0200142 sh_pfc_write_raw_reg((void __iomem *)(uintptr_t)reg, 32, data);
Marek Vasut3066a062017-09-15 21:13:55 +0200143}
144
145static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
146 const struct pinmux_cfg_reg *crp,
147 unsigned int in_pos,
148 void __iomem **mapped_regp, u32 *maskp,
149 unsigned int *posp)
150{
151 unsigned int k;
152
153 *mapped_regp = (void __iomem *)(uintptr_t)crp->reg;
154
155 if (crp->field_width) {
156 *maskp = (1 << crp->field_width) - 1;
157 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
158 } else {
159 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
160 *posp = crp->reg_width;
161 for (k = 0; k <= in_pos; k++)
162 *posp -= crp->var_field_width[k];
163 }
164}
165
166static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
167 const struct pinmux_cfg_reg *crp,
168 unsigned int field, u32 value)
169{
170 void __iomem *mapped_reg;
171 void __iomem *unlock_reg =
172 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
173 unsigned int pos;
174 u32 mask, data;
175
176 sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
177
178 dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, "
179 "r_width = %u, f_width = %u\n",
180 crp->reg, value, field, crp->reg_width, crp->field_width);
181
182 mask = ~(mask << pos);
183 value = value << pos;
184
185 data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
186 data &= mask;
187 data |= value;
188
189 if (pfc->info->unlock_reg)
190 sh_pfc_write_raw_reg(unlock_reg, 32, ~data);
191
192 sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
193}
194
195static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
196 const struct pinmux_cfg_reg **crp,
197 unsigned int *fieldp, u32 *valuep)
198{
199 unsigned int k = 0;
200
201 while (1) {
202 const struct pinmux_cfg_reg *config_reg =
203 pfc->info->cfg_regs + k;
204 unsigned int r_width = config_reg->reg_width;
205 unsigned int f_width = config_reg->field_width;
206 unsigned int curr_width;
207 unsigned int bit_pos;
208 unsigned int pos = 0;
209 unsigned int m = 0;
210
211 if (!r_width)
212 break;
213
214 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
215 u32 ncomb;
216 u32 n;
217
218 if (f_width)
219 curr_width = f_width;
220 else
221 curr_width = config_reg->var_field_width[m];
222
223 ncomb = 1 << curr_width;
224 for (n = 0; n < ncomb; n++) {
225 if (config_reg->enum_ids[pos + n] == enum_id) {
226 *crp = config_reg;
227 *fieldp = m;
228 *valuep = n;
229 return 0;
230 }
231 }
232 pos += ncomb;
233 m++;
234 }
235 k++;
236 }
237
238 return -EINVAL;
239}
240
241static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
242 u16 *enum_idp)
243{
244 const u16 *data = pfc->info->pinmux_data;
245 unsigned int k;
246
247 if (pos) {
248 *enum_idp = data[pos + 1];
249 return pos + 1;
250 }
251
252 for (k = 0; k < pfc->info->pinmux_data_size; k++) {
253 if (data[k] == mark) {
254 *enum_idp = data[k + 1];
255 return k + 1;
256 }
257 }
258
259 dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
260 mark);
261 return -EINVAL;
262}
263
264int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
265{
266 const struct pinmux_range *range;
267 int pos = 0;
268
269 switch (pinmux_type) {
270 case PINMUX_TYPE_GPIO:
271 case PINMUX_TYPE_FUNCTION:
272 range = NULL;
273 break;
274
275 case PINMUX_TYPE_OUTPUT:
276 range = &pfc->info->output;
277 break;
278
279 case PINMUX_TYPE_INPUT:
280 range = &pfc->info->input;
281 break;
282
283 default:
284 return -EINVAL;
285 }
286
287 /* Iterate over all the configuration fields we need to update. */
288 while (1) {
289 const struct pinmux_cfg_reg *cr;
290 unsigned int field;
291 u16 enum_id;
292 u32 value;
293 int in_range;
294 int ret;
295
296 pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
297 if (pos < 0)
298 return pos;
299
300 if (!enum_id)
301 break;
302
303 /* Check if the configuration field selects a function. If it
304 * doesn't, skip the field if it's not applicable to the
305 * requested pinmux type.
306 */
307 in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
308 if (!in_range) {
309 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
310 /* Functions are allowed to modify all
311 * fields.
312 */
313 in_range = 1;
314 } else if (pinmux_type != PINMUX_TYPE_GPIO) {
315 /* Input/output types can only modify fields
316 * that correspond to their respective ranges.
317 */
318 in_range = sh_pfc_enum_in_range(enum_id, range);
319
320 /*
321 * special case pass through for fixed
322 * input-only or output-only pins without
323 * function enum register association.
324 */
325 if (in_range && enum_id == range->force)
326 continue;
327 }
328 /* GPIOs are only allowed to modify function fields. */
329 }
330
331 if (!in_range)
332 continue;
333
334 ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
335 if (ret < 0)
336 return ret;
337
338 sh_pfc_write_config_reg(pfc, cr, field, value);
339 }
340
341 return 0;
342}
343
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200344const struct pinmux_bias_reg *
345sh_pfc_pin_to_bias_reg(const struct sh_pfc *pfc, unsigned int pin,
346 unsigned int *bit)
Marek Vasut3066a062017-09-15 21:13:55 +0200347{
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200348 unsigned int i, j;
Marek Vasut3066a062017-09-15 21:13:55 +0200349
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200350 for (i = 0; pfc->info->bias_regs[i].puen; i++) {
351 for (j = 0; j < ARRAY_SIZE(pfc->info->bias_regs[i].pins); j++) {
352 if (pfc->info->bias_regs[i].pins[j] == pin) {
353 *bit = j;
354 return &pfc->info->bias_regs[i];
355 }
356 }
357 }
Marek Vasut3066a062017-09-15 21:13:55 +0200358
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200359 WARN_ONCE(1, "Pin %u is not in bias info list\n", pin);
Marek Vasut3066a062017-09-15 21:13:55 +0200360
361 return NULL;
362}
363
364static int sh_pfc_init_ranges(struct sh_pfc *pfc)
365{
366 struct sh_pfc_pin_range *range;
367 unsigned int nr_ranges;
368 unsigned int i;
369
370 if (pfc->info->pins[0].pin == (u16)-1) {
371 /* Pin number -1 denotes that the SoC doesn't report pin numbers
372 * in its pin arrays yet. Consider the pin numbers range as
373 * continuous and allocate a single range.
374 */
375 pfc->nr_ranges = 1;
376 pfc->ranges = kzalloc(sizeof(*pfc->ranges), GFP_KERNEL);
377 if (pfc->ranges == NULL)
378 return -ENOMEM;
379
380 pfc->ranges->start = 0;
381 pfc->ranges->end = pfc->info->nr_pins - 1;
382 pfc->nr_gpio_pins = pfc->info->nr_pins;
383
384 return 0;
385 }
386
387 /* Count, allocate and fill the ranges. The PFC SoC data pins array must
388 * be sorted by pin numbers, and pins without a GPIO port must come
389 * last.
390 */
391 for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
392 if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
393 nr_ranges++;
394 }
395
396 pfc->nr_ranges = nr_ranges;
397 pfc->ranges = kzalloc(sizeof(*pfc->ranges) * nr_ranges, GFP_KERNEL);
398 if (pfc->ranges == NULL)
399 return -ENOMEM;
400
401 range = pfc->ranges;
402 range->start = pfc->info->pins[0].pin;
403
404 for (i = 1; i < pfc->info->nr_pins; ++i) {
405 if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
406 continue;
407
408 range->end = pfc->info->pins[i-1].pin;
409 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
410 pfc->nr_gpio_pins = range->end + 1;
411
412 range++;
413 range->start = pfc->info->pins[i].pin;
414 }
415
416 range->end = pfc->info->pins[i-1].pin;
417 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
418 pfc->nr_gpio_pins = range->end + 1;
419
420 return 0;
421}
422
423static int sh_pfc_pinctrl_get_pins_count(struct udevice *dev)
424{
425 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
426
427 return priv->pfc.info->nr_pins;
428}
429
430static const char *sh_pfc_pinctrl_get_pin_name(struct udevice *dev,
431 unsigned selector)
432{
433 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
434
435 return priv->pfc.info->pins[selector].name;
436}
437
438static int sh_pfc_pinctrl_get_groups_count(struct udevice *dev)
439{
440 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
441
442 return priv->pfc.info->nr_groups;
443}
444
445static const char *sh_pfc_pinctrl_get_group_name(struct udevice *dev,
446 unsigned selector)
447{
448 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
449
450 return priv->pfc.info->groups[selector].name;
451}
452
Lad Prabhakar23ea9b42020-11-28 13:13:09 +0000453static int sh_pfc_pinctrl_get_pin_muxing(struct udevice *dev,
454 unsigned int selector,
455 char *buf, int size)
456{
457 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
458 struct sh_pfc_pinctrl *pmx = &priv->pmx;
459 struct sh_pfc *pfc = &priv->pfc;
460 struct sh_pfc_pin_config *cfg;
461 const struct sh_pfc_pin *pin;
462 int idx;
463
464 pin = &priv->pfc.info->pins[selector];
465 if (!pin) {
466 snprintf(buf, size, "Unknown");
467 return -EINVAL;
468 }
469
470 idx = sh_pfc_get_pin_index(pfc, pin->pin);
471 cfg = &pmx->configs[idx];
472 snprintf(buf, size, "%s", cfg->name);
473
474 return 0;
475}
476
Marek Vasut3066a062017-09-15 21:13:55 +0200477static int sh_pfc_pinctrl_get_functions_count(struct udevice *dev)
478{
479 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
480
481 return priv->pfc.info->nr_functions;
482}
483
484static const char *sh_pfc_pinctrl_get_function_name(struct udevice *dev,
485 unsigned selector)
486{
487 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
488
489 return priv->pfc.info->functions[selector].name;
490}
491
Marek Vasut02d34f02019-04-21 22:46:25 +0200492static int sh_pfc_gpio_request_enable(struct udevice *dev,
493 unsigned pin_selector)
Marek Vasut489d79c2017-11-26 18:07:29 +0100494{
495 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
496 struct sh_pfc_pinctrl *pmx = &priv->pmx;
497 struct sh_pfc *pfc = &priv->pfc;
498 struct sh_pfc_pin_config *cfg;
499 const struct sh_pfc_pin *pin = NULL;
Marek Vasut0cc19362019-04-21 22:46:25 +0200500 int i, ret, idx;
Marek Vasut489d79c2017-11-26 18:07:29 +0100501
titron09bf4982019-07-22 17:45:37 +0800502 for (i = 0; i < pfc->info->nr_pins; i++) {
Marek Vasut489d79c2017-11-26 18:07:29 +0100503 if (priv->pfc.info->pins[i].pin != pin_selector)
504 continue;
505
506 pin = &priv->pfc.info->pins[i];
507 break;
508 }
509
510 if (!pin)
511 return -EINVAL;
512
513 idx = sh_pfc_get_pin_index(pfc, pin->pin);
514 cfg = &pmx->configs[idx];
515
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000516 if (cfg->type != PINMUX_TYPE_NONE) {
517 if (!strcmp(cfg->name, pin->name))
518 return 0;
519
520 dev_err(pfc->dev, "Pin already used as %s\n",
521 cfg->name);
Marek Vasut489d79c2017-11-26 18:07:29 +0100522 return -EBUSY;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000523 }
Marek Vasut489d79c2017-11-26 18:07:29 +0100524
Marek Vasut0cc19362019-04-21 22:46:25 +0200525 ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
526 if (ret)
527 return ret;
528
529 cfg->type = PINMUX_TYPE_GPIO;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000530 cfg->name = "gpio";
Marek Vasut0cc19362019-04-21 22:46:25 +0200531
532 return 0;
Marek Vasut489d79c2017-11-26 18:07:29 +0100533}
534
Marek Vasut02d34f02019-04-21 22:46:25 +0200535static int sh_pfc_gpio_disable_free(struct udevice *dev,
536 unsigned pin_selector)
537{
538 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
539 struct sh_pfc_pinctrl *pmx = &priv->pmx;
540 struct sh_pfc *pfc = &priv->pfc;
541 struct sh_pfc_pin_config *cfg;
542 const struct sh_pfc_pin *pin = NULL;
543 int i, idx;
544
titron09bf4982019-07-22 17:45:37 +0800545 for (i = 0; i < pfc->info->nr_pins; i++) {
Marek Vasut02d34f02019-04-21 22:46:25 +0200546 if (priv->pfc.info->pins[i].pin != pin_selector)
547 continue;
548
549 pin = &priv->pfc.info->pins[i];
550 break;
551 }
552
553 if (!pin)
554 return -EINVAL;
555
556 idx = sh_pfc_get_pin_index(pfc, pin->pin);
557 cfg = &pmx->configs[idx];
558
559 cfg->type = PINMUX_TYPE_NONE;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000560 cfg->name = "none";
Marek Vasut02d34f02019-04-21 22:46:25 +0200561
562 return 0;
563}
564
Marek Vasut5e6db842017-11-26 17:42:16 +0100565static int sh_pfc_pinctrl_pin_set(struct udevice *dev, unsigned pin_selector,
566 unsigned func_selector)
567{
568 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
569 struct sh_pfc_pinctrl *pmx = &priv->pmx;
570 struct sh_pfc *pfc = &priv->pfc;
571 const struct sh_pfc_pin *pin = &priv->pfc.info->pins[pin_selector];
572 int idx = sh_pfc_get_pin_index(pfc, pin->pin);
573 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000574 int ret;
Marek Vasut5e6db842017-11-26 17:42:16 +0100575
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000576 if (cfg->type != PINMUX_TYPE_NONE) {
577 if (!strcmp(cfg->name, pin->name))
578 return 0;
579
580 dev_err(pfc->dev, "Pin already used as %s\n",
581 cfg->name);
Marek Vasut5e6db842017-11-26 17:42:16 +0100582 return -EBUSY;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000583 }
584
585 ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_FUNCTION);
586 if (ret)
587 return ret;
Marek Vasut5e6db842017-11-26 17:42:16 +0100588
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000589 cfg->type = PINMUX_TYPE_FUNCTION;
590 cfg->name = "function";
591
592 return 0;
Marek Vasut5e6db842017-11-26 17:42:16 +0100593}
594
Marek Vasut3066a062017-09-15 21:13:55 +0200595static int sh_pfc_pinctrl_group_set(struct udevice *dev, unsigned group_selector,
596 unsigned func_selector)
597{
598 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
599 struct sh_pfc_pinctrl *pmx = &priv->pmx;
600 struct sh_pfc *pfc = &priv->pfc;
601 const struct sh_pfc_pin_group *grp = &priv->pfc.info->groups[group_selector];
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000602 bool grp_pins_configured = true;
603 struct sh_pfc_pin_config *cfg;
Marek Vasut3066a062017-09-15 21:13:55 +0200604 unsigned int i;
605 int ret = 0;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000606 int idx;
Marek Vasut3066a062017-09-15 21:13:55 +0200607
608 for (i = 0; i < grp->nr_pins; ++i) {
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000609 idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
610 cfg = &pmx->configs[idx];
Marek Vasut3066a062017-09-15 21:13:55 +0200611
612 if (cfg->type != PINMUX_TYPE_NONE) {
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000613 if (!strcmp(cfg->name, grp->name))
614 continue;
615
616 dev_err(pfc->dev, "Pin already used as %s\n",
617 cfg->name);
Marek Vasut3066a062017-09-15 21:13:55 +0200618 ret = -EBUSY;
619 goto done;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000620 } else {
621 grp_pins_configured = false;
Marek Vasut3066a062017-09-15 21:13:55 +0200622 }
623 }
624
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000625 if (grp_pins_configured)
626 return 0;
627
Marek Vasut3066a062017-09-15 21:13:55 +0200628 for (i = 0; i < grp->nr_pins; ++i) {
629 ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
630 if (ret < 0)
631 break;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000632
633 idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
634 cfg = &pmx->configs[idx];
635 cfg->type = PINMUX_TYPE_FUNCTION;
636 cfg->name = priv->pfc.info->groups[group_selector].name;
Marek Vasut3066a062017-09-15 21:13:55 +0200637 }
638
639done:
640 return ret;
641}
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200642#if CONFIG_IS_ENABLED(PINCONF)
643static const struct pinconf_param sh_pfc_pinconf_params[] = {
644 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
645 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
646 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
647 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
648 { "power-source", PIN_CONFIG_POWER_SOURCE, 3300 },
649};
650
651static void __iomem *
652sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc, unsigned int pin,
653 unsigned int *offset, unsigned int *size)
654{
655 const struct pinmux_drive_reg_field *field;
656 const struct pinmux_drive_reg *reg;
657 unsigned int i;
658
659 for (reg = pfc->info->drive_regs; reg->reg; ++reg) {
660 for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) {
661 field = &reg->fields[i];
662
663 if (field->size && field->pin == pin) {
664 *offset = field->offset;
665 *size = field->size;
666
667 return (void __iomem *)(uintptr_t)reg->reg;
668 }
669 }
670 }
671
672 return NULL;
673}
674
675static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc,
676 unsigned int pin, u16 strength)
677{
678 unsigned int offset;
679 unsigned int size;
680 unsigned int step;
681 void __iomem *reg;
682 void __iomem *unlock_reg =
683 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
684 u32 val;
685
686 reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
687 if (!reg)
688 return -EINVAL;
689
690 step = size == 2 ? 6 : 3;
691
692 if (strength < step || strength > 24)
693 return -EINVAL;
694
695 /* Convert the value from mA based on a full drive strength value of
696 * 24mA. We can make the full value configurable later if needed.
697 */
698 strength = strength / step - 1;
699
700 val = sh_pfc_read_raw_reg(reg, 32);
Marek Vasut0d9c8102018-06-13 08:02:55 +0200701 val &= ~GENMASK(offset + 4 - 1, offset);
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200702 val |= strength << offset;
703
704 if (unlock_reg)
705 sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
706
707 sh_pfc_write_raw_reg(reg, 32, val);
708
709 return 0;
710}
711
712/* Check whether the requested parameter is supported for a pin. */
713static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
714 unsigned int param)
715{
716 int idx = sh_pfc_get_pin_index(pfc, _pin);
717 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
718
719 switch (param) {
720 case PIN_CONFIG_BIAS_DISABLE:
721 return pin->configs &
722 (SH_PFC_PIN_CFG_PULL_UP | SH_PFC_PIN_CFG_PULL_DOWN);
723
724 case PIN_CONFIG_BIAS_PULL_UP:
725 return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
726
727 case PIN_CONFIG_BIAS_PULL_DOWN:
728 return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
729
730 case PIN_CONFIG_DRIVE_STRENGTH:
731 return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH;
732
733 case PIN_CONFIG_POWER_SOURCE:
734 return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;
735
736 default:
737 return false;
738 }
739}
740
741static int sh_pfc_pinconf_set(struct sh_pfc_pinctrl *pmx, unsigned _pin,
742 unsigned int param, unsigned int arg)
743{
744 struct sh_pfc *pfc = pmx->pfc;
745 void __iomem *pocctrl;
746 void __iomem *unlock_reg =
747 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
748 u32 addr, val;
749 int bit, ret;
750
751 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
752 return -ENOTSUPP;
753
754 switch (param) {
755 case PIN_CONFIG_BIAS_PULL_UP:
756 case PIN_CONFIG_BIAS_PULL_DOWN:
757 case PIN_CONFIG_BIAS_DISABLE:
758 if (!pfc->info->ops || !pfc->info->ops->set_bias)
759 return -ENOTSUPP;
760
761 pfc->info->ops->set_bias(pfc, _pin, param);
762
763 break;
764
765 case PIN_CONFIG_DRIVE_STRENGTH:
766 ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg);
767 if (ret < 0)
768 return ret;
769
770 break;
771
772 case PIN_CONFIG_POWER_SOURCE:
773 if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
774 return -ENOTSUPP;
775
776 bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &addr);
777 if (bit < 0) {
778 printf("invalid pin %#x", _pin);
779 return bit;
780 }
781
782 if (arg != 1800 && arg != 3300)
783 return -EINVAL;
784
785 pocctrl = (void __iomem *)(uintptr_t)addr;
786
787 val = sh_pfc_read_raw_reg(pocctrl, 32);
788 if (arg == 3300)
789 val |= BIT(bit);
790 else
791 val &= ~BIT(bit);
792
793 if (unlock_reg)
794 sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
795
796 sh_pfc_write_raw_reg(pocctrl, 32, val);
797
798 break;
799
800 default:
801 return -ENOTSUPP;
802 }
803
804 return 0;
805}
806
Marek Vasut5e6db842017-11-26 17:42:16 +0100807static int sh_pfc_pinconf_pin_set(struct udevice *dev,
808 unsigned int pin_selector,
809 unsigned int param, unsigned int arg)
810{
811 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
812 struct sh_pfc_pinctrl *pmx = &priv->pmx;
813 struct sh_pfc *pfc = &priv->pfc;
814 const struct sh_pfc_pin *pin = &pfc->info->pins[pin_selector];
815
816 sh_pfc_pinconf_set(pmx, pin->pin, param, arg);
817
818 return 0;
819}
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200820
821static int sh_pfc_pinconf_group_set(struct udevice *dev,
822 unsigned int group_selector,
823 unsigned int param, unsigned int arg)
824{
825 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
826 struct sh_pfc_pinctrl *pmx = &priv->pmx;
827 struct sh_pfc *pfc = &priv->pfc;
828 const struct sh_pfc_pin_group *grp = &pfc->info->groups[group_selector];
829 unsigned int i;
830
831 for (i = 0; i < grp->nr_pins; i++)
832 sh_pfc_pinconf_set(pmx, grp->pins[i], param, arg);
833
834 return 0;
835}
836#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200837
838static struct pinctrl_ops sh_pfc_pinctrl_ops = {
839 .get_pins_count = sh_pfc_pinctrl_get_pins_count,
840 .get_pin_name = sh_pfc_pinctrl_get_pin_name,
841 .get_groups_count = sh_pfc_pinctrl_get_groups_count,
842 .get_group_name = sh_pfc_pinctrl_get_group_name,
Lad Prabhakar23ea9b42020-11-28 13:13:09 +0000843 .get_pin_muxing = sh_pfc_pinctrl_get_pin_muxing,
Marek Vasut3066a062017-09-15 21:13:55 +0200844 .get_functions_count = sh_pfc_pinctrl_get_functions_count,
845 .get_function_name = sh_pfc_pinctrl_get_function_name,
846
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200847#if CONFIG_IS_ENABLED(PINCONF)
848 .pinconf_num_params = ARRAY_SIZE(sh_pfc_pinconf_params),
849 .pinconf_params = sh_pfc_pinconf_params,
Marek Vasut5e6db842017-11-26 17:42:16 +0100850 .pinconf_set = sh_pfc_pinconf_pin_set,
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200851 .pinconf_group_set = sh_pfc_pinconf_group_set,
852#endif
Marek Vasut5e6db842017-11-26 17:42:16 +0100853 .pinmux_set = sh_pfc_pinctrl_pin_set,
Marek Vasut3066a062017-09-15 21:13:55 +0200854 .pinmux_group_set = sh_pfc_pinctrl_group_set,
855 .set_state = pinctrl_generic_set_state,
Marek Vasut02d34f02019-04-21 22:46:25 +0200856
857 .gpio_request_enable = sh_pfc_gpio_request_enable,
858 .gpio_disable_free = sh_pfc_gpio_disable_free,
Marek Vasut3066a062017-09-15 21:13:55 +0200859};
860
861static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
862{
863 unsigned int i;
864
865 /* Allocate and initialize the pins and configs arrays. */
866 pmx->configs = kzalloc(sizeof(*pmx->configs) * pfc->info->nr_pins,
867 GFP_KERNEL);
868 if (unlikely(!pmx->configs))
869 return -ENOMEM;
870
871 for (i = 0; i < pfc->info->nr_pins; ++i) {
872 struct sh_pfc_pin_config *cfg = &pmx->configs[i];
873 cfg->type = PINMUX_TYPE_NONE;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000874 cfg->name = "none";
Marek Vasut3066a062017-09-15 21:13:55 +0200875 }
876
877 return 0;
878}
879
880
881static int sh_pfc_pinctrl_probe(struct udevice *dev)
882{
883 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
884 enum sh_pfc_model model = dev_get_driver_data(dev);
885 fdt_addr_t base;
886
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900887 base = dev_read_addr(dev);
Marek Vasut3066a062017-09-15 21:13:55 +0200888 if (base == FDT_ADDR_T_NONE)
889 return -EINVAL;
890
891 priv->pfc.regs = devm_ioremap(dev, base, SZ_2K);
892 if (!priv->pfc.regs)
893 return -ENOMEM;
894
Marek Vasutc40f2d62018-01-17 22:18:59 +0100895#ifdef CONFIG_PINCTRL_PFC_R8A7790
896 if (model == SH_PFC_R8A7790)
897 priv->pfc.info = &r8a7790_pinmux_info;
898#endif
Marek Vasut06ef9e82018-01-17 17:14:45 +0100899#ifdef CONFIG_PINCTRL_PFC_R8A7791
900 if (model == SH_PFC_R8A7791)
901 priv->pfc.info = &r8a7791_pinmux_info;
902#endif
Marek Vasut1ef39302018-01-17 22:29:50 +0100903#ifdef CONFIG_PINCTRL_PFC_R8A7792
904 if (model == SH_PFC_R8A7792)
905 priv->pfc.info = &r8a7792_pinmux_info;
906#endif
Marek Vasut06ef9e82018-01-17 17:14:45 +0100907#ifdef CONFIG_PINCTRL_PFC_R8A7793
908 if (model == SH_PFC_R8A7793)
909 priv->pfc.info = &r8a7793_pinmux_info;
910#endif
Marek Vasut4dd88d52018-01-17 22:33:59 +0100911#ifdef CONFIG_PINCTRL_PFC_R8A7794
912 if (model == SH_PFC_R8A7794)
913 priv->pfc.info = &r8a7794_pinmux_info;
914#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200915#ifdef CONFIG_PINCTRL_PFC_R8A7795
916 if (model == SH_PFC_R8A7795)
917 priv->pfc.info = &r8a7795_pinmux_info;
918#endif
919#ifdef CONFIG_PINCTRL_PFC_R8A7796
920 if (model == SH_PFC_R8A7796)
921 priv->pfc.info = &r8a7796_pinmux_info;
922#endif
Adam Ford96980fb2020-06-30 09:30:09 -0500923#ifdef CONFIG_PINCTRL_PFC_R8A774A1
924 if (model == SH_PFC_R8A774A1)
925 priv->pfc.info = &r8a774a1_pinmux_info;
926#endif
Biju Dasd1d78882020-10-28 10:34:21 +0000927#ifdef CONFIG_PINCTRL_PFC_R8A774B1
928 if (model == SH_PFC_R8A774B1)
929 priv->pfc.info = &r8a774b1_pinmux_info;
930#endif
Lad Prabhakar53b88b92021-03-15 22:24:04 +0000931#ifdef CONFIG_PINCTRL_PFC_R8A774C0
932 if (model == SH_PFC_R8A774C0)
933 priv->pfc.info = &r8a774c0_pinmux_info;
934#endif
Biju Das121bd002020-10-28 10:34:22 +0000935#ifdef CONFIG_PINCTRL_PFC_R8A774E1
936 if (model == SH_PFC_R8A774E1)
937 priv->pfc.info = &r8a774e1_pinmux_info;
938#endif
Marek Vasut72269e02019-03-04 01:32:44 +0100939#ifdef CONFIG_PINCTRL_PFC_R8A77965
940 if (model == SH_PFC_R8A77965)
941 priv->pfc.info = &r8a77965_pinmux_info;
942#endif
Marek Vasuta0e11e52017-10-09 20:57:29 +0200943#ifdef CONFIG_PINCTRL_PFC_R8A77970
944 if (model == SH_PFC_R8A77970)
945 priv->pfc.info = &r8a77970_pinmux_info;
946#endif
Marek Vasuta6a7f482019-07-29 19:59:44 +0200947#ifdef CONFIG_PINCTRL_PFC_R8A77980
948 if (model == SH_PFC_R8A77980)
949 priv->pfc.info = &r8a77980_pinmux_info;
950#endif
Marek Vasut68a77042018-04-26 13:09:20 +0200951#ifdef CONFIG_PINCTRL_PFC_R8A77990
952 if (model == SH_PFC_R8A77990)
953 priv->pfc.info = &r8a77990_pinmux_info;
954#endif
Marek Vasut7d35e642017-10-08 20:57:37 +0200955#ifdef CONFIG_PINCTRL_PFC_R8A77995
956 if (model == SH_PFC_R8A77995)
957 priv->pfc.info = &r8a77995_pinmux_info;
958#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200959
960 priv->pmx.pfc = &priv->pfc;
961 sh_pfc_init_ranges(&priv->pfc);
962 sh_pfc_map_pins(&priv->pfc, &priv->pmx);
963
964 return 0;
965}
966
967static const struct udevice_id sh_pfc_pinctrl_ids[] = {
Marek Vasutc40f2d62018-01-17 22:18:59 +0100968#ifdef CONFIG_PINCTRL_PFC_R8A7790
969 {
970 .compatible = "renesas,pfc-r8a7790",
971 .data = SH_PFC_R8A7790,
972 },
973#endif
Marek Vasut06ef9e82018-01-17 17:14:45 +0100974#ifdef CONFIG_PINCTRL_PFC_R8A7791
975 {
976 .compatible = "renesas,pfc-r8a7791",
977 .data = SH_PFC_R8A7791,
978 },
979#endif
Marek Vasut1ef39302018-01-17 22:29:50 +0100980#ifdef CONFIG_PINCTRL_PFC_R8A7792
981 {
982 .compatible = "renesas,pfc-r8a7792",
983 .data = SH_PFC_R8A7792,
984 },
985#endif
Marek Vasut06ef9e82018-01-17 17:14:45 +0100986#ifdef CONFIG_PINCTRL_PFC_R8A7793
987 {
988 .compatible = "renesas,pfc-r8a7793",
989 .data = SH_PFC_R8A7793,
990 },
991#endif
Marek Vasut4dd88d52018-01-17 22:33:59 +0100992#ifdef CONFIG_PINCTRL_PFC_R8A7794
993 {
994 .compatible = "renesas,pfc-r8a7794",
995 .data = SH_PFC_R8A7794,
996 },
997#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200998#ifdef CONFIG_PINCTRL_PFC_R8A7795
999 {
1000 .compatible = "renesas,pfc-r8a7795",
1001 .data = SH_PFC_R8A7795,
1002 },
1003#endif
1004#ifdef CONFIG_PINCTRL_PFC_R8A7796
1005 {
1006 .compatible = "renesas,pfc-r8a7796",
1007 .data = SH_PFC_R8A7796,
Marek Vasut72269e02019-03-04 01:32:44 +01001008 },
1009#endif
Adam Ford96980fb2020-06-30 09:30:09 -05001010#ifdef CONFIG_PINCTRL_PFC_R8A774A1
1011 {
1012 .compatible = "renesas,pfc-r8a774a1",
1013 .data = SH_PFC_R8A774A1,
1014 },
1015#endif
Biju Dasd1d78882020-10-28 10:34:21 +00001016#ifdef CONFIG_PINCTRL_PFC_R8A774B1
1017 {
1018 .compatible = "renesas,pfc-r8a774b1",
1019 .data = SH_PFC_R8A774B1,
1020 },
1021#endif
Lad Prabhakar53b88b92021-03-15 22:24:04 +00001022#ifdef CONFIG_PINCTRL_PFC_R8A774C0
1023 {
1024 .compatible = "renesas,pfc-r8a774c0",
1025 .data = SH_PFC_R8A774C0,
1026 },
1027#endif
Biju Das121bd002020-10-28 10:34:22 +00001028#ifdef CONFIG_PINCTRL_PFC_R8A774E1
1029 {
1030 .compatible = "renesas,pfc-r8a774e1",
1031 .data = SH_PFC_R8A774E1,
1032 },
1033#endif
Marek Vasut72269e02019-03-04 01:32:44 +01001034#ifdef CONFIG_PINCTRL_PFC_R8A77965
1035 {
Marek Vasut20d721e2018-02-26 10:35:15 +01001036 .compatible = "renesas,pfc-r8a77965",
Marek Vasut72269e02019-03-04 01:32:44 +01001037 .data = SH_PFC_R8A77965,
Marek Vasut3066a062017-09-15 21:13:55 +02001038 },
1039#endif
Marek Vasuta0e11e52017-10-09 20:57:29 +02001040#ifdef CONFIG_PINCTRL_PFC_R8A77970
1041 {
1042 .compatible = "renesas,pfc-r8a77970",
1043 .data = SH_PFC_R8A77970,
1044 },
1045#endif
Marek Vasuta6a7f482019-07-29 19:59:44 +02001046#ifdef CONFIG_PINCTRL_PFC_R8A77980
1047 {
1048 .compatible = "renesas,pfc-r8a77980",
1049 .data = SH_PFC_R8A77980,
1050 },
1051#endif
Marek Vasut68a77042018-04-26 13:09:20 +02001052#ifdef CONFIG_PINCTRL_PFC_R8A77990
1053 {
1054 .compatible = "renesas,pfc-r8a77990",
1055 .data = SH_PFC_R8A77990,
1056 },
1057#endif
Marek Vasut7d35e642017-10-08 20:57:37 +02001058#ifdef CONFIG_PINCTRL_PFC_R8A77995
1059 {
1060 .compatible = "renesas,pfc-r8a77995",
1061 .data = SH_PFC_R8A77995,
1062 },
1063#endif
Marek Vasut3066a062017-09-15 21:13:55 +02001064 { },
1065};
1066
1067U_BOOT_DRIVER(pinctrl_sh_pfc) = {
1068 .name = "sh_pfc_pinctrl",
1069 .id = UCLASS_PINCTRL,
1070 .of_match = sh_pfc_pinctrl_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001071 .priv_auto = sizeof(struct sh_pfc_pinctrl_priv),
Marek Vasut3066a062017-09-15 21:13:55 +02001072 .ops = &sh_pfc_pinctrl_ops,
1073 .probe = sh_pfc_pinctrl_probe,
1074};