blob: 6ff948420c5a3ee1144952f8515517a7c35353aa [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,
Biju Das121bd002020-10-28 10:34:22 +000037 SH_PFC_R8A774E1,
Marek Vasut72269e02019-03-04 01:32:44 +010038 SH_PFC_R8A77965,
Marek Vasuta0e11e52017-10-09 20:57:29 +020039 SH_PFC_R8A77970,
Marek Vasuta6a7f482019-07-29 19:59:44 +020040 SH_PFC_R8A77980,
Marek Vasut68a77042018-04-26 13:09:20 +020041 SH_PFC_R8A77990,
Marek Vasut7d35e642017-10-08 20:57:37 +020042 SH_PFC_R8A77995,
Marek Vasut3066a062017-09-15 21:13:55 +020043};
44
45struct sh_pfc_pin_config {
46 u32 type;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +000047 const char *name;
Marek Vasut3066a062017-09-15 21:13:55 +020048};
49
50struct sh_pfc_pinctrl {
51 struct sh_pfc *pfc;
52
53 struct sh_pfc_pin_config *configs;
Marek Vasut3066a062017-09-15 21:13:55 +020054};
55
56struct sh_pfc_pin_range {
57 u16 start;
58 u16 end;
59};
60
61struct sh_pfc_pinctrl_priv {
62 struct sh_pfc pfc;
63 struct sh_pfc_pinctrl pmx;
64};
65
66int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
67{
68 unsigned int offset;
69 unsigned int i;
70
71 for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
72 const struct sh_pfc_pin_range *range = &pfc->ranges[i];
73
74 if (pin <= range->end)
75 return pin >= range->start
76 ? offset + pin - range->start : -1;
77
78 offset += range->end - range->start + 1;
79 }
80
81 return -EINVAL;
82}
83
84static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
85{
86 if (enum_id < r->begin)
87 return 0;
88
89 if (enum_id > r->end)
90 return 0;
91
92 return 1;
93}
94
95u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width)
96{
97 switch (reg_width) {
98 case 8:
99 return readb(mapped_reg);
100 case 16:
101 return readw(mapped_reg);
102 case 32:
103 return readl(mapped_reg);
104 }
105
106 BUG();
107 return 0;
108}
109
110void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width,
111 u32 data)
112{
113 switch (reg_width) {
114 case 8:
115 writeb(data, mapped_reg);
116 return;
117 case 16:
118 writew(data, mapped_reg);
119 return;
120 case 32:
121 writel(data, mapped_reg);
122 return;
123 }
124
125 BUG();
126}
127
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200128u32 sh_pfc_read(struct sh_pfc *pfc, u32 reg)
Marek Vasut3066a062017-09-15 21:13:55 +0200129{
Marek Vasut068a90b2018-06-19 06:13:42 +0200130 return sh_pfc_read_raw_reg((void __iomem *)(uintptr_t)reg, 32);
Marek Vasut3066a062017-09-15 21:13:55 +0200131}
132
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200133void sh_pfc_write(struct sh_pfc *pfc, u32 reg, u32 data)
Marek Vasut3066a062017-09-15 21:13:55 +0200134{
135 void __iomem *unlock_reg =
136 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
137
138 if (pfc->info->unlock_reg)
139 sh_pfc_write_raw_reg(unlock_reg, 32, ~data);
140
Marek Vasut068a90b2018-06-19 06:13:42 +0200141 sh_pfc_write_raw_reg((void __iomem *)(uintptr_t)reg, 32, data);
Marek Vasut3066a062017-09-15 21:13:55 +0200142}
143
144static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
145 const struct pinmux_cfg_reg *crp,
146 unsigned int in_pos,
147 void __iomem **mapped_regp, u32 *maskp,
148 unsigned int *posp)
149{
150 unsigned int k;
151
152 *mapped_regp = (void __iomem *)(uintptr_t)crp->reg;
153
154 if (crp->field_width) {
155 *maskp = (1 << crp->field_width) - 1;
156 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
157 } else {
158 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
159 *posp = crp->reg_width;
160 for (k = 0; k <= in_pos; k++)
161 *posp -= crp->var_field_width[k];
162 }
163}
164
165static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
166 const struct pinmux_cfg_reg *crp,
167 unsigned int field, u32 value)
168{
169 void __iomem *mapped_reg;
170 void __iomem *unlock_reg =
171 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
172 unsigned int pos;
173 u32 mask, data;
174
175 sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
176
177 dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, "
178 "r_width = %u, f_width = %u\n",
179 crp->reg, value, field, crp->reg_width, crp->field_width);
180
181 mask = ~(mask << pos);
182 value = value << pos;
183
184 data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
185 data &= mask;
186 data |= value;
187
188 if (pfc->info->unlock_reg)
189 sh_pfc_write_raw_reg(unlock_reg, 32, ~data);
190
191 sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
192}
193
194static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
195 const struct pinmux_cfg_reg **crp,
196 unsigned int *fieldp, u32 *valuep)
197{
198 unsigned int k = 0;
199
200 while (1) {
201 const struct pinmux_cfg_reg *config_reg =
202 pfc->info->cfg_regs + k;
203 unsigned int r_width = config_reg->reg_width;
204 unsigned int f_width = config_reg->field_width;
205 unsigned int curr_width;
206 unsigned int bit_pos;
207 unsigned int pos = 0;
208 unsigned int m = 0;
209
210 if (!r_width)
211 break;
212
213 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
214 u32 ncomb;
215 u32 n;
216
217 if (f_width)
218 curr_width = f_width;
219 else
220 curr_width = config_reg->var_field_width[m];
221
222 ncomb = 1 << curr_width;
223 for (n = 0; n < ncomb; n++) {
224 if (config_reg->enum_ids[pos + n] == enum_id) {
225 *crp = config_reg;
226 *fieldp = m;
227 *valuep = n;
228 return 0;
229 }
230 }
231 pos += ncomb;
232 m++;
233 }
234 k++;
235 }
236
237 return -EINVAL;
238}
239
240static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
241 u16 *enum_idp)
242{
243 const u16 *data = pfc->info->pinmux_data;
244 unsigned int k;
245
246 if (pos) {
247 *enum_idp = data[pos + 1];
248 return pos + 1;
249 }
250
251 for (k = 0; k < pfc->info->pinmux_data_size; k++) {
252 if (data[k] == mark) {
253 *enum_idp = data[k + 1];
254 return k + 1;
255 }
256 }
257
258 dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
259 mark);
260 return -EINVAL;
261}
262
263int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
264{
265 const struct pinmux_range *range;
266 int pos = 0;
267
268 switch (pinmux_type) {
269 case PINMUX_TYPE_GPIO:
270 case PINMUX_TYPE_FUNCTION:
271 range = NULL;
272 break;
273
274 case PINMUX_TYPE_OUTPUT:
275 range = &pfc->info->output;
276 break;
277
278 case PINMUX_TYPE_INPUT:
279 range = &pfc->info->input;
280 break;
281
282 default:
283 return -EINVAL;
284 }
285
286 /* Iterate over all the configuration fields we need to update. */
287 while (1) {
288 const struct pinmux_cfg_reg *cr;
289 unsigned int field;
290 u16 enum_id;
291 u32 value;
292 int in_range;
293 int ret;
294
295 pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
296 if (pos < 0)
297 return pos;
298
299 if (!enum_id)
300 break;
301
302 /* Check if the configuration field selects a function. If it
303 * doesn't, skip the field if it's not applicable to the
304 * requested pinmux type.
305 */
306 in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
307 if (!in_range) {
308 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
309 /* Functions are allowed to modify all
310 * fields.
311 */
312 in_range = 1;
313 } else if (pinmux_type != PINMUX_TYPE_GPIO) {
314 /* Input/output types can only modify fields
315 * that correspond to their respective ranges.
316 */
317 in_range = sh_pfc_enum_in_range(enum_id, range);
318
319 /*
320 * special case pass through for fixed
321 * input-only or output-only pins without
322 * function enum register association.
323 */
324 if (in_range && enum_id == range->force)
325 continue;
326 }
327 /* GPIOs are only allowed to modify function fields. */
328 }
329
330 if (!in_range)
331 continue;
332
333 ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
334 if (ret < 0)
335 return ret;
336
337 sh_pfc_write_config_reg(pfc, cr, field, value);
338 }
339
340 return 0;
341}
342
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200343const struct pinmux_bias_reg *
344sh_pfc_pin_to_bias_reg(const struct sh_pfc *pfc, unsigned int pin,
345 unsigned int *bit)
Marek Vasut3066a062017-09-15 21:13:55 +0200346{
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200347 unsigned int i, j;
Marek Vasut3066a062017-09-15 21:13:55 +0200348
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200349 for (i = 0; pfc->info->bias_regs[i].puen; i++) {
350 for (j = 0; j < ARRAY_SIZE(pfc->info->bias_regs[i].pins); j++) {
351 if (pfc->info->bias_regs[i].pins[j] == pin) {
352 *bit = j;
353 return &pfc->info->bias_regs[i];
354 }
355 }
356 }
Marek Vasut3066a062017-09-15 21:13:55 +0200357
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200358 WARN_ONCE(1, "Pin %u is not in bias info list\n", pin);
Marek Vasut3066a062017-09-15 21:13:55 +0200359
360 return NULL;
361}
362
363static int sh_pfc_init_ranges(struct sh_pfc *pfc)
364{
365 struct sh_pfc_pin_range *range;
366 unsigned int nr_ranges;
367 unsigned int i;
368
369 if (pfc->info->pins[0].pin == (u16)-1) {
370 /* Pin number -1 denotes that the SoC doesn't report pin numbers
371 * in its pin arrays yet. Consider the pin numbers range as
372 * continuous and allocate a single range.
373 */
374 pfc->nr_ranges = 1;
375 pfc->ranges = kzalloc(sizeof(*pfc->ranges), GFP_KERNEL);
376 if (pfc->ranges == NULL)
377 return -ENOMEM;
378
379 pfc->ranges->start = 0;
380 pfc->ranges->end = pfc->info->nr_pins - 1;
381 pfc->nr_gpio_pins = pfc->info->nr_pins;
382
383 return 0;
384 }
385
386 /* Count, allocate and fill the ranges. The PFC SoC data pins array must
387 * be sorted by pin numbers, and pins without a GPIO port must come
388 * last.
389 */
390 for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
391 if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
392 nr_ranges++;
393 }
394
395 pfc->nr_ranges = nr_ranges;
396 pfc->ranges = kzalloc(sizeof(*pfc->ranges) * nr_ranges, GFP_KERNEL);
397 if (pfc->ranges == NULL)
398 return -ENOMEM;
399
400 range = pfc->ranges;
401 range->start = pfc->info->pins[0].pin;
402
403 for (i = 1; i < pfc->info->nr_pins; ++i) {
404 if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
405 continue;
406
407 range->end = pfc->info->pins[i-1].pin;
408 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
409 pfc->nr_gpio_pins = range->end + 1;
410
411 range++;
412 range->start = pfc->info->pins[i].pin;
413 }
414
415 range->end = pfc->info->pins[i-1].pin;
416 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
417 pfc->nr_gpio_pins = range->end + 1;
418
419 return 0;
420}
421
422static int sh_pfc_pinctrl_get_pins_count(struct udevice *dev)
423{
424 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
425
426 return priv->pfc.info->nr_pins;
427}
428
429static const char *sh_pfc_pinctrl_get_pin_name(struct udevice *dev,
430 unsigned selector)
431{
432 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
433
434 return priv->pfc.info->pins[selector].name;
435}
436
437static int sh_pfc_pinctrl_get_groups_count(struct udevice *dev)
438{
439 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
440
441 return priv->pfc.info->nr_groups;
442}
443
444static const char *sh_pfc_pinctrl_get_group_name(struct udevice *dev,
445 unsigned selector)
446{
447 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
448
449 return priv->pfc.info->groups[selector].name;
450}
451
Lad Prabhakar23ea9b42020-11-28 13:13:09 +0000452static int sh_pfc_pinctrl_get_pin_muxing(struct udevice *dev,
453 unsigned int selector,
454 char *buf, int size)
455{
456 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
457 struct sh_pfc_pinctrl *pmx = &priv->pmx;
458 struct sh_pfc *pfc = &priv->pfc;
459 struct sh_pfc_pin_config *cfg;
460 const struct sh_pfc_pin *pin;
461 int idx;
462
463 pin = &priv->pfc.info->pins[selector];
464 if (!pin) {
465 snprintf(buf, size, "Unknown");
466 return -EINVAL;
467 }
468
469 idx = sh_pfc_get_pin_index(pfc, pin->pin);
470 cfg = &pmx->configs[idx];
471 snprintf(buf, size, "%s", cfg->name);
472
473 return 0;
474}
475
Marek Vasut3066a062017-09-15 21:13:55 +0200476static int sh_pfc_pinctrl_get_functions_count(struct udevice *dev)
477{
478 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
479
480 return priv->pfc.info->nr_functions;
481}
482
483static const char *sh_pfc_pinctrl_get_function_name(struct udevice *dev,
484 unsigned selector)
485{
486 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
487
488 return priv->pfc.info->functions[selector].name;
489}
490
Marek Vasut02d34f02019-04-21 22:46:25 +0200491static int sh_pfc_gpio_request_enable(struct udevice *dev,
492 unsigned pin_selector)
Marek Vasut489d79c2017-11-26 18:07:29 +0100493{
494 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
495 struct sh_pfc_pinctrl *pmx = &priv->pmx;
496 struct sh_pfc *pfc = &priv->pfc;
497 struct sh_pfc_pin_config *cfg;
498 const struct sh_pfc_pin *pin = NULL;
Marek Vasut0cc19362019-04-21 22:46:25 +0200499 int i, ret, idx;
Marek Vasut489d79c2017-11-26 18:07:29 +0100500
titron09bf4982019-07-22 17:45:37 +0800501 for (i = 0; i < pfc->info->nr_pins; i++) {
Marek Vasut489d79c2017-11-26 18:07:29 +0100502 if (priv->pfc.info->pins[i].pin != pin_selector)
503 continue;
504
505 pin = &priv->pfc.info->pins[i];
506 break;
507 }
508
509 if (!pin)
510 return -EINVAL;
511
512 idx = sh_pfc_get_pin_index(pfc, pin->pin);
513 cfg = &pmx->configs[idx];
514
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000515 if (cfg->type != PINMUX_TYPE_NONE) {
516 if (!strcmp(cfg->name, pin->name))
517 return 0;
518
519 dev_err(pfc->dev, "Pin already used as %s\n",
520 cfg->name);
Marek Vasut489d79c2017-11-26 18:07:29 +0100521 return -EBUSY;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000522 }
Marek Vasut489d79c2017-11-26 18:07:29 +0100523
Marek Vasut0cc19362019-04-21 22:46:25 +0200524 ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
525 if (ret)
526 return ret;
527
528 cfg->type = PINMUX_TYPE_GPIO;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000529 cfg->name = "gpio";
Marek Vasut0cc19362019-04-21 22:46:25 +0200530
531 return 0;
Marek Vasut489d79c2017-11-26 18:07:29 +0100532}
533
Marek Vasut02d34f02019-04-21 22:46:25 +0200534static int sh_pfc_gpio_disable_free(struct udevice *dev,
535 unsigned pin_selector)
536{
537 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
538 struct sh_pfc_pinctrl *pmx = &priv->pmx;
539 struct sh_pfc *pfc = &priv->pfc;
540 struct sh_pfc_pin_config *cfg;
541 const struct sh_pfc_pin *pin = NULL;
542 int i, idx;
543
titron09bf4982019-07-22 17:45:37 +0800544 for (i = 0; i < pfc->info->nr_pins; i++) {
Marek Vasut02d34f02019-04-21 22:46:25 +0200545 if (priv->pfc.info->pins[i].pin != pin_selector)
546 continue;
547
548 pin = &priv->pfc.info->pins[i];
549 break;
550 }
551
552 if (!pin)
553 return -EINVAL;
554
555 idx = sh_pfc_get_pin_index(pfc, pin->pin);
556 cfg = &pmx->configs[idx];
557
558 cfg->type = PINMUX_TYPE_NONE;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000559 cfg->name = "none";
Marek Vasut02d34f02019-04-21 22:46:25 +0200560
561 return 0;
562}
563
Marek Vasut5e6db842017-11-26 17:42:16 +0100564static int sh_pfc_pinctrl_pin_set(struct udevice *dev, unsigned pin_selector,
565 unsigned func_selector)
566{
567 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
568 struct sh_pfc_pinctrl *pmx = &priv->pmx;
569 struct sh_pfc *pfc = &priv->pfc;
570 const struct sh_pfc_pin *pin = &priv->pfc.info->pins[pin_selector];
571 int idx = sh_pfc_get_pin_index(pfc, pin->pin);
572 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000573 int ret;
Marek Vasut5e6db842017-11-26 17:42:16 +0100574
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000575 if (cfg->type != PINMUX_TYPE_NONE) {
576 if (!strcmp(cfg->name, pin->name))
577 return 0;
578
579 dev_err(pfc->dev, "Pin already used as %s\n",
580 cfg->name);
Marek Vasut5e6db842017-11-26 17:42:16 +0100581 return -EBUSY;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000582 }
583
584 ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_FUNCTION);
585 if (ret)
586 return ret;
Marek Vasut5e6db842017-11-26 17:42:16 +0100587
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000588 cfg->type = PINMUX_TYPE_FUNCTION;
589 cfg->name = "function";
590
591 return 0;
Marek Vasut5e6db842017-11-26 17:42:16 +0100592}
593
Marek Vasut3066a062017-09-15 21:13:55 +0200594static int sh_pfc_pinctrl_group_set(struct udevice *dev, unsigned group_selector,
595 unsigned func_selector)
596{
597 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
598 struct sh_pfc_pinctrl *pmx = &priv->pmx;
599 struct sh_pfc *pfc = &priv->pfc;
600 const struct sh_pfc_pin_group *grp = &priv->pfc.info->groups[group_selector];
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000601 bool grp_pins_configured = true;
602 struct sh_pfc_pin_config *cfg;
Marek Vasut3066a062017-09-15 21:13:55 +0200603 unsigned int i;
604 int ret = 0;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000605 int idx;
Marek Vasut3066a062017-09-15 21:13:55 +0200606
607 for (i = 0; i < grp->nr_pins; ++i) {
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000608 idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
609 cfg = &pmx->configs[idx];
Marek Vasut3066a062017-09-15 21:13:55 +0200610
611 if (cfg->type != PINMUX_TYPE_NONE) {
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000612 if (!strcmp(cfg->name, grp->name))
613 continue;
614
615 dev_err(pfc->dev, "Pin already used as %s\n",
616 cfg->name);
Marek Vasut3066a062017-09-15 21:13:55 +0200617 ret = -EBUSY;
618 goto done;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000619 } else {
620 grp_pins_configured = false;
Marek Vasut3066a062017-09-15 21:13:55 +0200621 }
622 }
623
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000624 if (grp_pins_configured)
625 return 0;
626
Marek Vasut3066a062017-09-15 21:13:55 +0200627 for (i = 0; i < grp->nr_pins; ++i) {
628 ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
629 if (ret < 0)
630 break;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000631
632 idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
633 cfg = &pmx->configs[idx];
634 cfg->type = PINMUX_TYPE_FUNCTION;
635 cfg->name = priv->pfc.info->groups[group_selector].name;
Marek Vasut3066a062017-09-15 21:13:55 +0200636 }
637
638done:
639 return ret;
640}
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200641#if CONFIG_IS_ENABLED(PINCONF)
642static const struct pinconf_param sh_pfc_pinconf_params[] = {
643 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
644 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
645 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
646 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
647 { "power-source", PIN_CONFIG_POWER_SOURCE, 3300 },
648};
649
650static void __iomem *
651sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc, unsigned int pin,
652 unsigned int *offset, unsigned int *size)
653{
654 const struct pinmux_drive_reg_field *field;
655 const struct pinmux_drive_reg *reg;
656 unsigned int i;
657
658 for (reg = pfc->info->drive_regs; reg->reg; ++reg) {
659 for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) {
660 field = &reg->fields[i];
661
662 if (field->size && field->pin == pin) {
663 *offset = field->offset;
664 *size = field->size;
665
666 return (void __iomem *)(uintptr_t)reg->reg;
667 }
668 }
669 }
670
671 return NULL;
672}
673
674static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc,
675 unsigned int pin, u16 strength)
676{
677 unsigned int offset;
678 unsigned int size;
679 unsigned int step;
680 void __iomem *reg;
681 void __iomem *unlock_reg =
682 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
683 u32 val;
684
685 reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
686 if (!reg)
687 return -EINVAL;
688
689 step = size == 2 ? 6 : 3;
690
691 if (strength < step || strength > 24)
692 return -EINVAL;
693
694 /* Convert the value from mA based on a full drive strength value of
695 * 24mA. We can make the full value configurable later if needed.
696 */
697 strength = strength / step - 1;
698
699 val = sh_pfc_read_raw_reg(reg, 32);
Marek Vasut0d9c8102018-06-13 08:02:55 +0200700 val &= ~GENMASK(offset + 4 - 1, offset);
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200701 val |= strength << offset;
702
703 if (unlock_reg)
704 sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
705
706 sh_pfc_write_raw_reg(reg, 32, val);
707
708 return 0;
709}
710
711/* Check whether the requested parameter is supported for a pin. */
712static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
713 unsigned int param)
714{
715 int idx = sh_pfc_get_pin_index(pfc, _pin);
716 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
717
718 switch (param) {
719 case PIN_CONFIG_BIAS_DISABLE:
720 return pin->configs &
721 (SH_PFC_PIN_CFG_PULL_UP | SH_PFC_PIN_CFG_PULL_DOWN);
722
723 case PIN_CONFIG_BIAS_PULL_UP:
724 return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
725
726 case PIN_CONFIG_BIAS_PULL_DOWN:
727 return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
728
729 case PIN_CONFIG_DRIVE_STRENGTH:
730 return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH;
731
732 case PIN_CONFIG_POWER_SOURCE:
733 return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;
734
735 default:
736 return false;
737 }
738}
739
740static int sh_pfc_pinconf_set(struct sh_pfc_pinctrl *pmx, unsigned _pin,
741 unsigned int param, unsigned int arg)
742{
743 struct sh_pfc *pfc = pmx->pfc;
744 void __iomem *pocctrl;
745 void __iomem *unlock_reg =
746 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
747 u32 addr, val;
748 int bit, ret;
749
750 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
751 return -ENOTSUPP;
752
753 switch (param) {
754 case PIN_CONFIG_BIAS_PULL_UP:
755 case PIN_CONFIG_BIAS_PULL_DOWN:
756 case PIN_CONFIG_BIAS_DISABLE:
757 if (!pfc->info->ops || !pfc->info->ops->set_bias)
758 return -ENOTSUPP;
759
760 pfc->info->ops->set_bias(pfc, _pin, param);
761
762 break;
763
764 case PIN_CONFIG_DRIVE_STRENGTH:
765 ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg);
766 if (ret < 0)
767 return ret;
768
769 break;
770
771 case PIN_CONFIG_POWER_SOURCE:
772 if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
773 return -ENOTSUPP;
774
775 bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &addr);
776 if (bit < 0) {
777 printf("invalid pin %#x", _pin);
778 return bit;
779 }
780
781 if (arg != 1800 && arg != 3300)
782 return -EINVAL;
783
784 pocctrl = (void __iomem *)(uintptr_t)addr;
785
786 val = sh_pfc_read_raw_reg(pocctrl, 32);
787 if (arg == 3300)
788 val |= BIT(bit);
789 else
790 val &= ~BIT(bit);
791
792 if (unlock_reg)
793 sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
794
795 sh_pfc_write_raw_reg(pocctrl, 32, val);
796
797 break;
798
799 default:
800 return -ENOTSUPP;
801 }
802
803 return 0;
804}
805
Marek Vasut5e6db842017-11-26 17:42:16 +0100806static int sh_pfc_pinconf_pin_set(struct udevice *dev,
807 unsigned int pin_selector,
808 unsigned int param, unsigned int arg)
809{
810 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
811 struct sh_pfc_pinctrl *pmx = &priv->pmx;
812 struct sh_pfc *pfc = &priv->pfc;
813 const struct sh_pfc_pin *pin = &pfc->info->pins[pin_selector];
814
815 sh_pfc_pinconf_set(pmx, pin->pin, param, arg);
816
817 return 0;
818}
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200819
820static int sh_pfc_pinconf_group_set(struct udevice *dev,
821 unsigned int group_selector,
822 unsigned int param, unsigned int arg)
823{
824 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
825 struct sh_pfc_pinctrl *pmx = &priv->pmx;
826 struct sh_pfc *pfc = &priv->pfc;
827 const struct sh_pfc_pin_group *grp = &pfc->info->groups[group_selector];
828 unsigned int i;
829
830 for (i = 0; i < grp->nr_pins; i++)
831 sh_pfc_pinconf_set(pmx, grp->pins[i], param, arg);
832
833 return 0;
834}
835#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200836
837static struct pinctrl_ops sh_pfc_pinctrl_ops = {
838 .get_pins_count = sh_pfc_pinctrl_get_pins_count,
839 .get_pin_name = sh_pfc_pinctrl_get_pin_name,
840 .get_groups_count = sh_pfc_pinctrl_get_groups_count,
841 .get_group_name = sh_pfc_pinctrl_get_group_name,
Lad Prabhakar23ea9b42020-11-28 13:13:09 +0000842 .get_pin_muxing = sh_pfc_pinctrl_get_pin_muxing,
Marek Vasut3066a062017-09-15 21:13:55 +0200843 .get_functions_count = sh_pfc_pinctrl_get_functions_count,
844 .get_function_name = sh_pfc_pinctrl_get_function_name,
845
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200846#if CONFIG_IS_ENABLED(PINCONF)
847 .pinconf_num_params = ARRAY_SIZE(sh_pfc_pinconf_params),
848 .pinconf_params = sh_pfc_pinconf_params,
Marek Vasut5e6db842017-11-26 17:42:16 +0100849 .pinconf_set = sh_pfc_pinconf_pin_set,
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200850 .pinconf_group_set = sh_pfc_pinconf_group_set,
851#endif
Marek Vasut5e6db842017-11-26 17:42:16 +0100852 .pinmux_set = sh_pfc_pinctrl_pin_set,
Marek Vasut3066a062017-09-15 21:13:55 +0200853 .pinmux_group_set = sh_pfc_pinctrl_group_set,
854 .set_state = pinctrl_generic_set_state,
Marek Vasut02d34f02019-04-21 22:46:25 +0200855
856 .gpio_request_enable = sh_pfc_gpio_request_enable,
857 .gpio_disable_free = sh_pfc_gpio_disable_free,
Marek Vasut3066a062017-09-15 21:13:55 +0200858};
859
860static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
861{
862 unsigned int i;
863
864 /* Allocate and initialize the pins and configs arrays. */
865 pmx->configs = kzalloc(sizeof(*pmx->configs) * pfc->info->nr_pins,
866 GFP_KERNEL);
867 if (unlikely(!pmx->configs))
868 return -ENOMEM;
869
870 for (i = 0; i < pfc->info->nr_pins; ++i) {
871 struct sh_pfc_pin_config *cfg = &pmx->configs[i];
872 cfg->type = PINMUX_TYPE_NONE;
Lad Prabhakar13ed8d52020-11-28 13:13:08 +0000873 cfg->name = "none";
Marek Vasut3066a062017-09-15 21:13:55 +0200874 }
875
876 return 0;
877}
878
879
880static int sh_pfc_pinctrl_probe(struct udevice *dev)
881{
882 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
883 enum sh_pfc_model model = dev_get_driver_data(dev);
884 fdt_addr_t base;
885
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900886 base = dev_read_addr(dev);
Marek Vasut3066a062017-09-15 21:13:55 +0200887 if (base == FDT_ADDR_T_NONE)
888 return -EINVAL;
889
890 priv->pfc.regs = devm_ioremap(dev, base, SZ_2K);
891 if (!priv->pfc.regs)
892 return -ENOMEM;
893
Marek Vasutc40f2d62018-01-17 22:18:59 +0100894#ifdef CONFIG_PINCTRL_PFC_R8A7790
895 if (model == SH_PFC_R8A7790)
896 priv->pfc.info = &r8a7790_pinmux_info;
897#endif
Marek Vasut06ef9e82018-01-17 17:14:45 +0100898#ifdef CONFIG_PINCTRL_PFC_R8A7791
899 if (model == SH_PFC_R8A7791)
900 priv->pfc.info = &r8a7791_pinmux_info;
901#endif
Marek Vasut1ef39302018-01-17 22:29:50 +0100902#ifdef CONFIG_PINCTRL_PFC_R8A7792
903 if (model == SH_PFC_R8A7792)
904 priv->pfc.info = &r8a7792_pinmux_info;
905#endif
Marek Vasut06ef9e82018-01-17 17:14:45 +0100906#ifdef CONFIG_PINCTRL_PFC_R8A7793
907 if (model == SH_PFC_R8A7793)
908 priv->pfc.info = &r8a7793_pinmux_info;
909#endif
Marek Vasut4dd88d52018-01-17 22:33:59 +0100910#ifdef CONFIG_PINCTRL_PFC_R8A7794
911 if (model == SH_PFC_R8A7794)
912 priv->pfc.info = &r8a7794_pinmux_info;
913#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200914#ifdef CONFIG_PINCTRL_PFC_R8A7795
915 if (model == SH_PFC_R8A7795)
916 priv->pfc.info = &r8a7795_pinmux_info;
917#endif
918#ifdef CONFIG_PINCTRL_PFC_R8A7796
919 if (model == SH_PFC_R8A7796)
920 priv->pfc.info = &r8a7796_pinmux_info;
921#endif
Adam Ford96980fb2020-06-30 09:30:09 -0500922#ifdef CONFIG_PINCTRL_PFC_R8A774A1
923 if (model == SH_PFC_R8A774A1)
924 priv->pfc.info = &r8a774a1_pinmux_info;
925#endif
Biju Dasd1d78882020-10-28 10:34:21 +0000926#ifdef CONFIG_PINCTRL_PFC_R8A774B1
927 if (model == SH_PFC_R8A774B1)
928 priv->pfc.info = &r8a774b1_pinmux_info;
929#endif
Biju Das121bd002020-10-28 10:34:22 +0000930#ifdef CONFIG_PINCTRL_PFC_R8A774E1
931 if (model == SH_PFC_R8A774E1)
932 priv->pfc.info = &r8a774e1_pinmux_info;
933#endif
Marek Vasut72269e02019-03-04 01:32:44 +0100934#ifdef CONFIG_PINCTRL_PFC_R8A77965
935 if (model == SH_PFC_R8A77965)
936 priv->pfc.info = &r8a77965_pinmux_info;
937#endif
Marek Vasuta0e11e52017-10-09 20:57:29 +0200938#ifdef CONFIG_PINCTRL_PFC_R8A77970
939 if (model == SH_PFC_R8A77970)
940 priv->pfc.info = &r8a77970_pinmux_info;
941#endif
Marek Vasuta6a7f482019-07-29 19:59:44 +0200942#ifdef CONFIG_PINCTRL_PFC_R8A77980
943 if (model == SH_PFC_R8A77980)
944 priv->pfc.info = &r8a77980_pinmux_info;
945#endif
Marek Vasut68a77042018-04-26 13:09:20 +0200946#ifdef CONFIG_PINCTRL_PFC_R8A77990
947 if (model == SH_PFC_R8A77990)
948 priv->pfc.info = &r8a77990_pinmux_info;
949#endif
Marek Vasut7d35e642017-10-08 20:57:37 +0200950#ifdef CONFIG_PINCTRL_PFC_R8A77995
951 if (model == SH_PFC_R8A77995)
952 priv->pfc.info = &r8a77995_pinmux_info;
953#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200954
955 priv->pmx.pfc = &priv->pfc;
956 sh_pfc_init_ranges(&priv->pfc);
957 sh_pfc_map_pins(&priv->pfc, &priv->pmx);
958
959 return 0;
960}
961
962static const struct udevice_id sh_pfc_pinctrl_ids[] = {
Marek Vasutc40f2d62018-01-17 22:18:59 +0100963#ifdef CONFIG_PINCTRL_PFC_R8A7790
964 {
965 .compatible = "renesas,pfc-r8a7790",
966 .data = SH_PFC_R8A7790,
967 },
968#endif
Marek Vasut06ef9e82018-01-17 17:14:45 +0100969#ifdef CONFIG_PINCTRL_PFC_R8A7791
970 {
971 .compatible = "renesas,pfc-r8a7791",
972 .data = SH_PFC_R8A7791,
973 },
974#endif
Marek Vasut1ef39302018-01-17 22:29:50 +0100975#ifdef CONFIG_PINCTRL_PFC_R8A7792
976 {
977 .compatible = "renesas,pfc-r8a7792",
978 .data = SH_PFC_R8A7792,
979 },
980#endif
Marek Vasut06ef9e82018-01-17 17:14:45 +0100981#ifdef CONFIG_PINCTRL_PFC_R8A7793
982 {
983 .compatible = "renesas,pfc-r8a7793",
984 .data = SH_PFC_R8A7793,
985 },
986#endif
Marek Vasut4dd88d52018-01-17 22:33:59 +0100987#ifdef CONFIG_PINCTRL_PFC_R8A7794
988 {
989 .compatible = "renesas,pfc-r8a7794",
990 .data = SH_PFC_R8A7794,
991 },
992#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200993#ifdef CONFIG_PINCTRL_PFC_R8A7795
994 {
995 .compatible = "renesas,pfc-r8a7795",
996 .data = SH_PFC_R8A7795,
997 },
998#endif
999#ifdef CONFIG_PINCTRL_PFC_R8A7796
1000 {
1001 .compatible = "renesas,pfc-r8a7796",
1002 .data = SH_PFC_R8A7796,
Marek Vasut72269e02019-03-04 01:32:44 +01001003 },
1004#endif
Adam Ford96980fb2020-06-30 09:30:09 -05001005#ifdef CONFIG_PINCTRL_PFC_R8A774A1
1006 {
1007 .compatible = "renesas,pfc-r8a774a1",
1008 .data = SH_PFC_R8A774A1,
1009 },
1010#endif
Biju Dasd1d78882020-10-28 10:34:21 +00001011#ifdef CONFIG_PINCTRL_PFC_R8A774B1
1012 {
1013 .compatible = "renesas,pfc-r8a774b1",
1014 .data = SH_PFC_R8A774B1,
1015 },
1016#endif
Biju Das121bd002020-10-28 10:34:22 +00001017#ifdef CONFIG_PINCTRL_PFC_R8A774E1
1018 {
1019 .compatible = "renesas,pfc-r8a774e1",
1020 .data = SH_PFC_R8A774E1,
1021 },
1022#endif
Marek Vasut72269e02019-03-04 01:32:44 +01001023#ifdef CONFIG_PINCTRL_PFC_R8A77965
1024 {
Marek Vasut20d721e2018-02-26 10:35:15 +01001025 .compatible = "renesas,pfc-r8a77965",
Marek Vasut72269e02019-03-04 01:32:44 +01001026 .data = SH_PFC_R8A77965,
Marek Vasut3066a062017-09-15 21:13:55 +02001027 },
1028#endif
Marek Vasuta0e11e52017-10-09 20:57:29 +02001029#ifdef CONFIG_PINCTRL_PFC_R8A77970
1030 {
1031 .compatible = "renesas,pfc-r8a77970",
1032 .data = SH_PFC_R8A77970,
1033 },
1034#endif
Marek Vasuta6a7f482019-07-29 19:59:44 +02001035#ifdef CONFIG_PINCTRL_PFC_R8A77980
1036 {
1037 .compatible = "renesas,pfc-r8a77980",
1038 .data = SH_PFC_R8A77980,
1039 },
1040#endif
Marek Vasut68a77042018-04-26 13:09:20 +02001041#ifdef CONFIG_PINCTRL_PFC_R8A77990
1042 {
1043 .compatible = "renesas,pfc-r8a77990",
1044 .data = SH_PFC_R8A77990,
1045 },
1046#endif
Marek Vasut7d35e642017-10-08 20:57:37 +02001047#ifdef CONFIG_PINCTRL_PFC_R8A77995
1048 {
1049 .compatible = "renesas,pfc-r8a77995",
1050 .data = SH_PFC_R8A77995,
1051 },
1052#endif
Marek Vasut3066a062017-09-15 21:13:55 +02001053 { },
1054};
1055
1056U_BOOT_DRIVER(pinctrl_sh_pfc) = {
1057 .name = "sh_pfc_pinctrl",
1058 .id = UCLASS_PINCTRL,
1059 .of_match = sh_pfc_pinctrl_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001060 .priv_auto = sizeof(struct sh_pfc_pinctrl_priv),
Marek Vasut3066a062017-09-15 21:13:55 +02001061 .ops = &sh_pfc_pinctrl_ops,
1062 .probe = sh_pfc_pinctrl_probe,
1063};