blob: db0fa9b728b7ddbea0f3380af8593bd39a9229a0 [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;
47};
48
49struct sh_pfc_pinctrl {
50 struct sh_pfc *pfc;
51
52 struct sh_pfc_pin_config *configs;
53
54 const char *func_prop_name;
55 const char *groups_prop_name;
56 const char *pins_prop_name;
57};
58
59struct sh_pfc_pin_range {
60 u16 start;
61 u16 end;
62};
63
64struct sh_pfc_pinctrl_priv {
65 struct sh_pfc pfc;
66 struct sh_pfc_pinctrl pmx;
67};
68
69int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
70{
71 unsigned int offset;
72 unsigned int i;
73
74 for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
75 const struct sh_pfc_pin_range *range = &pfc->ranges[i];
76
77 if (pin <= range->end)
78 return pin >= range->start
79 ? offset + pin - range->start : -1;
80
81 offset += range->end - range->start + 1;
82 }
83
84 return -EINVAL;
85}
86
87static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
88{
89 if (enum_id < r->begin)
90 return 0;
91
92 if (enum_id > r->end)
93 return 0;
94
95 return 1;
96}
97
98u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width)
99{
100 switch (reg_width) {
101 case 8:
102 return readb(mapped_reg);
103 case 16:
104 return readw(mapped_reg);
105 case 32:
106 return readl(mapped_reg);
107 }
108
109 BUG();
110 return 0;
111}
112
113void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width,
114 u32 data)
115{
116 switch (reg_width) {
117 case 8:
118 writeb(data, mapped_reg);
119 return;
120 case 16:
121 writew(data, mapped_reg);
122 return;
123 case 32:
124 writel(data, mapped_reg);
125 return;
126 }
127
128 BUG();
129}
130
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200131u32 sh_pfc_read(struct sh_pfc *pfc, u32 reg)
Marek Vasut3066a062017-09-15 21:13:55 +0200132{
Marek Vasut068a90b2018-06-19 06:13:42 +0200133 return sh_pfc_read_raw_reg((void __iomem *)(uintptr_t)reg, 32);
Marek Vasut3066a062017-09-15 21:13:55 +0200134}
135
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200136void sh_pfc_write(struct sh_pfc *pfc, u32 reg, u32 data)
Marek Vasut3066a062017-09-15 21:13:55 +0200137{
138 void __iomem *unlock_reg =
139 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
140
141 if (pfc->info->unlock_reg)
142 sh_pfc_write_raw_reg(unlock_reg, 32, ~data);
143
Marek Vasut068a90b2018-06-19 06:13:42 +0200144 sh_pfc_write_raw_reg((void __iomem *)(uintptr_t)reg, 32, data);
Marek Vasut3066a062017-09-15 21:13:55 +0200145}
146
147static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
148 const struct pinmux_cfg_reg *crp,
149 unsigned int in_pos,
150 void __iomem **mapped_regp, u32 *maskp,
151 unsigned int *posp)
152{
153 unsigned int k;
154
155 *mapped_regp = (void __iomem *)(uintptr_t)crp->reg;
156
157 if (crp->field_width) {
158 *maskp = (1 << crp->field_width) - 1;
159 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
160 } else {
161 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
162 *posp = crp->reg_width;
163 for (k = 0; k <= in_pos; k++)
164 *posp -= crp->var_field_width[k];
165 }
166}
167
168static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
169 const struct pinmux_cfg_reg *crp,
170 unsigned int field, u32 value)
171{
172 void __iomem *mapped_reg;
173 void __iomem *unlock_reg =
174 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
175 unsigned int pos;
176 u32 mask, data;
177
178 sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
179
180 dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, "
181 "r_width = %u, f_width = %u\n",
182 crp->reg, value, field, crp->reg_width, crp->field_width);
183
184 mask = ~(mask << pos);
185 value = value << pos;
186
187 data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
188 data &= mask;
189 data |= value;
190
191 if (pfc->info->unlock_reg)
192 sh_pfc_write_raw_reg(unlock_reg, 32, ~data);
193
194 sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
195}
196
197static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
198 const struct pinmux_cfg_reg **crp,
199 unsigned int *fieldp, u32 *valuep)
200{
201 unsigned int k = 0;
202
203 while (1) {
204 const struct pinmux_cfg_reg *config_reg =
205 pfc->info->cfg_regs + k;
206 unsigned int r_width = config_reg->reg_width;
207 unsigned int f_width = config_reg->field_width;
208 unsigned int curr_width;
209 unsigned int bit_pos;
210 unsigned int pos = 0;
211 unsigned int m = 0;
212
213 if (!r_width)
214 break;
215
216 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
217 u32 ncomb;
218 u32 n;
219
220 if (f_width)
221 curr_width = f_width;
222 else
223 curr_width = config_reg->var_field_width[m];
224
225 ncomb = 1 << curr_width;
226 for (n = 0; n < ncomb; n++) {
227 if (config_reg->enum_ids[pos + n] == enum_id) {
228 *crp = config_reg;
229 *fieldp = m;
230 *valuep = n;
231 return 0;
232 }
233 }
234 pos += ncomb;
235 m++;
236 }
237 k++;
238 }
239
240 return -EINVAL;
241}
242
243static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
244 u16 *enum_idp)
245{
246 const u16 *data = pfc->info->pinmux_data;
247 unsigned int k;
248
249 if (pos) {
250 *enum_idp = data[pos + 1];
251 return pos + 1;
252 }
253
254 for (k = 0; k < pfc->info->pinmux_data_size; k++) {
255 if (data[k] == mark) {
256 *enum_idp = data[k + 1];
257 return k + 1;
258 }
259 }
260
261 dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
262 mark);
263 return -EINVAL;
264}
265
266int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
267{
268 const struct pinmux_range *range;
269 int pos = 0;
270
271 switch (pinmux_type) {
272 case PINMUX_TYPE_GPIO:
273 case PINMUX_TYPE_FUNCTION:
274 range = NULL;
275 break;
276
277 case PINMUX_TYPE_OUTPUT:
278 range = &pfc->info->output;
279 break;
280
281 case PINMUX_TYPE_INPUT:
282 range = &pfc->info->input;
283 break;
284
285 default:
286 return -EINVAL;
287 }
288
289 /* Iterate over all the configuration fields we need to update. */
290 while (1) {
291 const struct pinmux_cfg_reg *cr;
292 unsigned int field;
293 u16 enum_id;
294 u32 value;
295 int in_range;
296 int ret;
297
298 pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
299 if (pos < 0)
300 return pos;
301
302 if (!enum_id)
303 break;
304
305 /* Check if the configuration field selects a function. If it
306 * doesn't, skip the field if it's not applicable to the
307 * requested pinmux type.
308 */
309 in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
310 if (!in_range) {
311 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
312 /* Functions are allowed to modify all
313 * fields.
314 */
315 in_range = 1;
316 } else if (pinmux_type != PINMUX_TYPE_GPIO) {
317 /* Input/output types can only modify fields
318 * that correspond to their respective ranges.
319 */
320 in_range = sh_pfc_enum_in_range(enum_id, range);
321
322 /*
323 * special case pass through for fixed
324 * input-only or output-only pins without
325 * function enum register association.
326 */
327 if (in_range && enum_id == range->force)
328 continue;
329 }
330 /* GPIOs are only allowed to modify function fields. */
331 }
332
333 if (!in_range)
334 continue;
335
336 ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
337 if (ret < 0)
338 return ret;
339
340 sh_pfc_write_config_reg(pfc, cr, field, value);
341 }
342
343 return 0;
344}
345
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200346const struct pinmux_bias_reg *
347sh_pfc_pin_to_bias_reg(const struct sh_pfc *pfc, unsigned int pin,
348 unsigned int *bit)
Marek Vasut3066a062017-09-15 21:13:55 +0200349{
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200350 unsigned int i, j;
Marek Vasut3066a062017-09-15 21:13:55 +0200351
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200352 for (i = 0; pfc->info->bias_regs[i].puen; i++) {
353 for (j = 0; j < ARRAY_SIZE(pfc->info->bias_regs[i].pins); j++) {
354 if (pfc->info->bias_regs[i].pins[j] == pin) {
355 *bit = j;
356 return &pfc->info->bias_regs[i];
357 }
358 }
359 }
Marek Vasut3066a062017-09-15 21:13:55 +0200360
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200361 WARN_ONCE(1, "Pin %u is not in bias info list\n", pin);
Marek Vasut3066a062017-09-15 21:13:55 +0200362
363 return NULL;
364}
365
366static int sh_pfc_init_ranges(struct sh_pfc *pfc)
367{
368 struct sh_pfc_pin_range *range;
369 unsigned int nr_ranges;
370 unsigned int i;
371
372 if (pfc->info->pins[0].pin == (u16)-1) {
373 /* Pin number -1 denotes that the SoC doesn't report pin numbers
374 * in its pin arrays yet. Consider the pin numbers range as
375 * continuous and allocate a single range.
376 */
377 pfc->nr_ranges = 1;
378 pfc->ranges = kzalloc(sizeof(*pfc->ranges), GFP_KERNEL);
379 if (pfc->ranges == NULL)
380 return -ENOMEM;
381
382 pfc->ranges->start = 0;
383 pfc->ranges->end = pfc->info->nr_pins - 1;
384 pfc->nr_gpio_pins = pfc->info->nr_pins;
385
386 return 0;
387 }
388
389 /* Count, allocate and fill the ranges. The PFC SoC data pins array must
390 * be sorted by pin numbers, and pins without a GPIO port must come
391 * last.
392 */
393 for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
394 if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
395 nr_ranges++;
396 }
397
398 pfc->nr_ranges = nr_ranges;
399 pfc->ranges = kzalloc(sizeof(*pfc->ranges) * nr_ranges, GFP_KERNEL);
400 if (pfc->ranges == NULL)
401 return -ENOMEM;
402
403 range = pfc->ranges;
404 range->start = pfc->info->pins[0].pin;
405
406 for (i = 1; i < pfc->info->nr_pins; ++i) {
407 if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
408 continue;
409
410 range->end = pfc->info->pins[i-1].pin;
411 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
412 pfc->nr_gpio_pins = range->end + 1;
413
414 range++;
415 range->start = pfc->info->pins[i].pin;
416 }
417
418 range->end = pfc->info->pins[i-1].pin;
419 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
420 pfc->nr_gpio_pins = range->end + 1;
421
422 return 0;
423}
424
425static int sh_pfc_pinctrl_get_pins_count(struct udevice *dev)
426{
427 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
428
429 return priv->pfc.info->nr_pins;
430}
431
432static const char *sh_pfc_pinctrl_get_pin_name(struct udevice *dev,
433 unsigned selector)
434{
435 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
436
437 return priv->pfc.info->pins[selector].name;
438}
439
440static int sh_pfc_pinctrl_get_groups_count(struct udevice *dev)
441{
442 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
443
444 return priv->pfc.info->nr_groups;
445}
446
447static const char *sh_pfc_pinctrl_get_group_name(struct udevice *dev,
448 unsigned selector)
449{
450 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
451
452 return priv->pfc.info->groups[selector].name;
453}
454
455static int sh_pfc_pinctrl_get_functions_count(struct udevice *dev)
456{
457 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
458
459 return priv->pfc.info->nr_functions;
460}
461
462static const char *sh_pfc_pinctrl_get_function_name(struct udevice *dev,
463 unsigned selector)
464{
465 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
466
467 return priv->pfc.info->functions[selector].name;
468}
469
Marek Vasut02d34f02019-04-21 22:46:25 +0200470static int sh_pfc_gpio_request_enable(struct udevice *dev,
471 unsigned pin_selector)
Marek Vasut489d79c2017-11-26 18:07:29 +0100472{
473 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
474 struct sh_pfc_pinctrl *pmx = &priv->pmx;
475 struct sh_pfc *pfc = &priv->pfc;
476 struct sh_pfc_pin_config *cfg;
477 const struct sh_pfc_pin *pin = NULL;
Marek Vasut0cc19362019-04-21 22:46:25 +0200478 int i, ret, idx;
Marek Vasut489d79c2017-11-26 18:07:29 +0100479
titron09bf4982019-07-22 17:45:37 +0800480 for (i = 0; i < pfc->info->nr_pins; i++) {
Marek Vasut489d79c2017-11-26 18:07:29 +0100481 if (priv->pfc.info->pins[i].pin != pin_selector)
482 continue;
483
484 pin = &priv->pfc.info->pins[i];
485 break;
486 }
487
488 if (!pin)
489 return -EINVAL;
490
491 idx = sh_pfc_get_pin_index(pfc, pin->pin);
492 cfg = &pmx->configs[idx];
493
494 if (cfg->type != PINMUX_TYPE_NONE)
495 return -EBUSY;
496
Marek Vasut0cc19362019-04-21 22:46:25 +0200497 ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
498 if (ret)
499 return ret;
500
501 cfg->type = PINMUX_TYPE_GPIO;
502
503 return 0;
Marek Vasut489d79c2017-11-26 18:07:29 +0100504}
505
Marek Vasut02d34f02019-04-21 22:46:25 +0200506static int sh_pfc_gpio_disable_free(struct udevice *dev,
507 unsigned pin_selector)
508{
509 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
510 struct sh_pfc_pinctrl *pmx = &priv->pmx;
511 struct sh_pfc *pfc = &priv->pfc;
512 struct sh_pfc_pin_config *cfg;
513 const struct sh_pfc_pin *pin = NULL;
514 int i, idx;
515
titron09bf4982019-07-22 17:45:37 +0800516 for (i = 0; i < pfc->info->nr_pins; i++) {
Marek Vasut02d34f02019-04-21 22:46:25 +0200517 if (priv->pfc.info->pins[i].pin != pin_selector)
518 continue;
519
520 pin = &priv->pfc.info->pins[i];
521 break;
522 }
523
524 if (!pin)
525 return -EINVAL;
526
527 idx = sh_pfc_get_pin_index(pfc, pin->pin);
528 cfg = &pmx->configs[idx];
529
530 cfg->type = PINMUX_TYPE_NONE;
531
532 return 0;
533}
534
Marek Vasut5e6db842017-11-26 17:42:16 +0100535static int sh_pfc_pinctrl_pin_set(struct udevice *dev, unsigned pin_selector,
536 unsigned func_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 const struct sh_pfc_pin *pin = &priv->pfc.info->pins[pin_selector];
542 int idx = sh_pfc_get_pin_index(pfc, pin->pin);
543 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
544
545 if (cfg->type != PINMUX_TYPE_NONE)
546 return -EBUSY;
547
548 return sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_FUNCTION);
549}
550
Marek Vasut3066a062017-09-15 21:13:55 +0200551static int sh_pfc_pinctrl_group_set(struct udevice *dev, unsigned group_selector,
552 unsigned func_selector)
553{
554 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
555 struct sh_pfc_pinctrl *pmx = &priv->pmx;
556 struct sh_pfc *pfc = &priv->pfc;
557 const struct sh_pfc_pin_group *grp = &priv->pfc.info->groups[group_selector];
558 unsigned int i;
559 int ret = 0;
560
561 for (i = 0; i < grp->nr_pins; ++i) {
562 int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
563 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
564
565 if (cfg->type != PINMUX_TYPE_NONE) {
566 ret = -EBUSY;
567 goto done;
568 }
569 }
570
571 for (i = 0; i < grp->nr_pins; ++i) {
572 ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
573 if (ret < 0)
574 break;
575 }
576
577done:
578 return ret;
579}
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200580#if CONFIG_IS_ENABLED(PINCONF)
581static const struct pinconf_param sh_pfc_pinconf_params[] = {
582 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
583 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
584 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
585 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
586 { "power-source", PIN_CONFIG_POWER_SOURCE, 3300 },
587};
588
589static void __iomem *
590sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc, unsigned int pin,
591 unsigned int *offset, unsigned int *size)
592{
593 const struct pinmux_drive_reg_field *field;
594 const struct pinmux_drive_reg *reg;
595 unsigned int i;
596
597 for (reg = pfc->info->drive_regs; reg->reg; ++reg) {
598 for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) {
599 field = &reg->fields[i];
600
601 if (field->size && field->pin == pin) {
602 *offset = field->offset;
603 *size = field->size;
604
605 return (void __iomem *)(uintptr_t)reg->reg;
606 }
607 }
608 }
609
610 return NULL;
611}
612
613static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc,
614 unsigned int pin, u16 strength)
615{
616 unsigned int offset;
617 unsigned int size;
618 unsigned int step;
619 void __iomem *reg;
620 void __iomem *unlock_reg =
621 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
622 u32 val;
623
624 reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
625 if (!reg)
626 return -EINVAL;
627
628 step = size == 2 ? 6 : 3;
629
630 if (strength < step || strength > 24)
631 return -EINVAL;
632
633 /* Convert the value from mA based on a full drive strength value of
634 * 24mA. We can make the full value configurable later if needed.
635 */
636 strength = strength / step - 1;
637
638 val = sh_pfc_read_raw_reg(reg, 32);
Marek Vasut0d9c8102018-06-13 08:02:55 +0200639 val &= ~GENMASK(offset + 4 - 1, offset);
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200640 val |= strength << offset;
641
642 if (unlock_reg)
643 sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
644
645 sh_pfc_write_raw_reg(reg, 32, val);
646
647 return 0;
648}
649
650/* Check whether the requested parameter is supported for a pin. */
651static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
652 unsigned int param)
653{
654 int idx = sh_pfc_get_pin_index(pfc, _pin);
655 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
656
657 switch (param) {
658 case PIN_CONFIG_BIAS_DISABLE:
659 return pin->configs &
660 (SH_PFC_PIN_CFG_PULL_UP | SH_PFC_PIN_CFG_PULL_DOWN);
661
662 case PIN_CONFIG_BIAS_PULL_UP:
663 return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
664
665 case PIN_CONFIG_BIAS_PULL_DOWN:
666 return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
667
668 case PIN_CONFIG_DRIVE_STRENGTH:
669 return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH;
670
671 case PIN_CONFIG_POWER_SOURCE:
672 return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;
673
674 default:
675 return false;
676 }
677}
678
679static int sh_pfc_pinconf_set(struct sh_pfc_pinctrl *pmx, unsigned _pin,
680 unsigned int param, unsigned int arg)
681{
682 struct sh_pfc *pfc = pmx->pfc;
683 void __iomem *pocctrl;
684 void __iomem *unlock_reg =
685 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
686 u32 addr, val;
687 int bit, ret;
688
689 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
690 return -ENOTSUPP;
691
692 switch (param) {
693 case PIN_CONFIG_BIAS_PULL_UP:
694 case PIN_CONFIG_BIAS_PULL_DOWN:
695 case PIN_CONFIG_BIAS_DISABLE:
696 if (!pfc->info->ops || !pfc->info->ops->set_bias)
697 return -ENOTSUPP;
698
699 pfc->info->ops->set_bias(pfc, _pin, param);
700
701 break;
702
703 case PIN_CONFIG_DRIVE_STRENGTH:
704 ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg);
705 if (ret < 0)
706 return ret;
707
708 break;
709
710 case PIN_CONFIG_POWER_SOURCE:
711 if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
712 return -ENOTSUPP;
713
714 bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &addr);
715 if (bit < 0) {
716 printf("invalid pin %#x", _pin);
717 return bit;
718 }
719
720 if (arg != 1800 && arg != 3300)
721 return -EINVAL;
722
723 pocctrl = (void __iomem *)(uintptr_t)addr;
724
725 val = sh_pfc_read_raw_reg(pocctrl, 32);
726 if (arg == 3300)
727 val |= BIT(bit);
728 else
729 val &= ~BIT(bit);
730
731 if (unlock_reg)
732 sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
733
734 sh_pfc_write_raw_reg(pocctrl, 32, val);
735
736 break;
737
738 default:
739 return -ENOTSUPP;
740 }
741
742 return 0;
743}
744
Marek Vasut5e6db842017-11-26 17:42:16 +0100745static int sh_pfc_pinconf_pin_set(struct udevice *dev,
746 unsigned int pin_selector,
747 unsigned int param, unsigned int arg)
748{
749 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
750 struct sh_pfc_pinctrl *pmx = &priv->pmx;
751 struct sh_pfc *pfc = &priv->pfc;
752 const struct sh_pfc_pin *pin = &pfc->info->pins[pin_selector];
753
754 sh_pfc_pinconf_set(pmx, pin->pin, param, arg);
755
756 return 0;
757}
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200758
759static int sh_pfc_pinconf_group_set(struct udevice *dev,
760 unsigned int group_selector,
761 unsigned int param, unsigned int arg)
762{
763 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
764 struct sh_pfc_pinctrl *pmx = &priv->pmx;
765 struct sh_pfc *pfc = &priv->pfc;
766 const struct sh_pfc_pin_group *grp = &pfc->info->groups[group_selector];
767 unsigned int i;
768
769 for (i = 0; i < grp->nr_pins; i++)
770 sh_pfc_pinconf_set(pmx, grp->pins[i], param, arg);
771
772 return 0;
773}
774#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200775
776static struct pinctrl_ops sh_pfc_pinctrl_ops = {
777 .get_pins_count = sh_pfc_pinctrl_get_pins_count,
778 .get_pin_name = sh_pfc_pinctrl_get_pin_name,
779 .get_groups_count = sh_pfc_pinctrl_get_groups_count,
780 .get_group_name = sh_pfc_pinctrl_get_group_name,
781 .get_functions_count = sh_pfc_pinctrl_get_functions_count,
782 .get_function_name = sh_pfc_pinctrl_get_function_name,
783
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200784#if CONFIG_IS_ENABLED(PINCONF)
785 .pinconf_num_params = ARRAY_SIZE(sh_pfc_pinconf_params),
786 .pinconf_params = sh_pfc_pinconf_params,
Marek Vasut5e6db842017-11-26 17:42:16 +0100787 .pinconf_set = sh_pfc_pinconf_pin_set,
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200788 .pinconf_group_set = sh_pfc_pinconf_group_set,
789#endif
Marek Vasut5e6db842017-11-26 17:42:16 +0100790 .pinmux_set = sh_pfc_pinctrl_pin_set,
Marek Vasut3066a062017-09-15 21:13:55 +0200791 .pinmux_group_set = sh_pfc_pinctrl_group_set,
792 .set_state = pinctrl_generic_set_state,
Marek Vasut02d34f02019-04-21 22:46:25 +0200793
794 .gpio_request_enable = sh_pfc_gpio_request_enable,
795 .gpio_disable_free = sh_pfc_gpio_disable_free,
Marek Vasut3066a062017-09-15 21:13:55 +0200796};
797
798static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
799{
800 unsigned int i;
801
802 /* Allocate and initialize the pins and configs arrays. */
803 pmx->configs = kzalloc(sizeof(*pmx->configs) * pfc->info->nr_pins,
804 GFP_KERNEL);
805 if (unlikely(!pmx->configs))
806 return -ENOMEM;
807
808 for (i = 0; i < pfc->info->nr_pins; ++i) {
809 struct sh_pfc_pin_config *cfg = &pmx->configs[i];
810 cfg->type = PINMUX_TYPE_NONE;
811 }
812
813 return 0;
814}
815
816
817static int sh_pfc_pinctrl_probe(struct udevice *dev)
818{
819 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
820 enum sh_pfc_model model = dev_get_driver_data(dev);
821 fdt_addr_t base;
822
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900823 base = dev_read_addr(dev);
Marek Vasut3066a062017-09-15 21:13:55 +0200824 if (base == FDT_ADDR_T_NONE)
825 return -EINVAL;
826
827 priv->pfc.regs = devm_ioremap(dev, base, SZ_2K);
828 if (!priv->pfc.regs)
829 return -ENOMEM;
830
Marek Vasutc40f2d62018-01-17 22:18:59 +0100831#ifdef CONFIG_PINCTRL_PFC_R8A7790
832 if (model == SH_PFC_R8A7790)
833 priv->pfc.info = &r8a7790_pinmux_info;
834#endif
Marek Vasut06ef9e82018-01-17 17:14:45 +0100835#ifdef CONFIG_PINCTRL_PFC_R8A7791
836 if (model == SH_PFC_R8A7791)
837 priv->pfc.info = &r8a7791_pinmux_info;
838#endif
Marek Vasut1ef39302018-01-17 22:29:50 +0100839#ifdef CONFIG_PINCTRL_PFC_R8A7792
840 if (model == SH_PFC_R8A7792)
841 priv->pfc.info = &r8a7792_pinmux_info;
842#endif
Marek Vasut06ef9e82018-01-17 17:14:45 +0100843#ifdef CONFIG_PINCTRL_PFC_R8A7793
844 if (model == SH_PFC_R8A7793)
845 priv->pfc.info = &r8a7793_pinmux_info;
846#endif
Marek Vasut4dd88d52018-01-17 22:33:59 +0100847#ifdef CONFIG_PINCTRL_PFC_R8A7794
848 if (model == SH_PFC_R8A7794)
849 priv->pfc.info = &r8a7794_pinmux_info;
850#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200851#ifdef CONFIG_PINCTRL_PFC_R8A7795
852 if (model == SH_PFC_R8A7795)
853 priv->pfc.info = &r8a7795_pinmux_info;
854#endif
855#ifdef CONFIG_PINCTRL_PFC_R8A7796
856 if (model == SH_PFC_R8A7796)
857 priv->pfc.info = &r8a7796_pinmux_info;
858#endif
Adam Ford96980fb2020-06-30 09:30:09 -0500859#ifdef CONFIG_PINCTRL_PFC_R8A774A1
860 if (model == SH_PFC_R8A774A1)
861 priv->pfc.info = &r8a774a1_pinmux_info;
862#endif
Biju Dasd1d78882020-10-28 10:34:21 +0000863#ifdef CONFIG_PINCTRL_PFC_R8A774B1
864 if (model == SH_PFC_R8A774B1)
865 priv->pfc.info = &r8a774b1_pinmux_info;
866#endif
Biju Das121bd002020-10-28 10:34:22 +0000867#ifdef CONFIG_PINCTRL_PFC_R8A774E1
868 if (model == SH_PFC_R8A774E1)
869 priv->pfc.info = &r8a774e1_pinmux_info;
870#endif
Marek Vasut72269e02019-03-04 01:32:44 +0100871#ifdef CONFIG_PINCTRL_PFC_R8A77965
872 if (model == SH_PFC_R8A77965)
873 priv->pfc.info = &r8a77965_pinmux_info;
874#endif
Marek Vasuta0e11e52017-10-09 20:57:29 +0200875#ifdef CONFIG_PINCTRL_PFC_R8A77970
876 if (model == SH_PFC_R8A77970)
877 priv->pfc.info = &r8a77970_pinmux_info;
878#endif
Marek Vasuta6a7f482019-07-29 19:59:44 +0200879#ifdef CONFIG_PINCTRL_PFC_R8A77980
880 if (model == SH_PFC_R8A77980)
881 priv->pfc.info = &r8a77980_pinmux_info;
882#endif
Marek Vasut68a77042018-04-26 13:09:20 +0200883#ifdef CONFIG_PINCTRL_PFC_R8A77990
884 if (model == SH_PFC_R8A77990)
885 priv->pfc.info = &r8a77990_pinmux_info;
886#endif
Marek Vasut7d35e642017-10-08 20:57:37 +0200887#ifdef CONFIG_PINCTRL_PFC_R8A77995
888 if (model == SH_PFC_R8A77995)
889 priv->pfc.info = &r8a77995_pinmux_info;
890#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200891
892 priv->pmx.pfc = &priv->pfc;
893 sh_pfc_init_ranges(&priv->pfc);
894 sh_pfc_map_pins(&priv->pfc, &priv->pmx);
895
896 return 0;
897}
898
899static const struct udevice_id sh_pfc_pinctrl_ids[] = {
Marek Vasutc40f2d62018-01-17 22:18:59 +0100900#ifdef CONFIG_PINCTRL_PFC_R8A7790
901 {
902 .compatible = "renesas,pfc-r8a7790",
903 .data = SH_PFC_R8A7790,
904 },
905#endif
Marek Vasut06ef9e82018-01-17 17:14:45 +0100906#ifdef CONFIG_PINCTRL_PFC_R8A7791
907 {
908 .compatible = "renesas,pfc-r8a7791",
909 .data = SH_PFC_R8A7791,
910 },
911#endif
Marek Vasut1ef39302018-01-17 22:29:50 +0100912#ifdef CONFIG_PINCTRL_PFC_R8A7792
913 {
914 .compatible = "renesas,pfc-r8a7792",
915 .data = SH_PFC_R8A7792,
916 },
917#endif
Marek Vasut06ef9e82018-01-17 17:14:45 +0100918#ifdef CONFIG_PINCTRL_PFC_R8A7793
919 {
920 .compatible = "renesas,pfc-r8a7793",
921 .data = SH_PFC_R8A7793,
922 },
923#endif
Marek Vasut4dd88d52018-01-17 22:33:59 +0100924#ifdef CONFIG_PINCTRL_PFC_R8A7794
925 {
926 .compatible = "renesas,pfc-r8a7794",
927 .data = SH_PFC_R8A7794,
928 },
929#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200930#ifdef CONFIG_PINCTRL_PFC_R8A7795
931 {
932 .compatible = "renesas,pfc-r8a7795",
933 .data = SH_PFC_R8A7795,
934 },
935#endif
936#ifdef CONFIG_PINCTRL_PFC_R8A7796
937 {
938 .compatible = "renesas,pfc-r8a7796",
939 .data = SH_PFC_R8A7796,
Marek Vasut72269e02019-03-04 01:32:44 +0100940 },
941#endif
Adam Ford96980fb2020-06-30 09:30:09 -0500942#ifdef CONFIG_PINCTRL_PFC_R8A774A1
943 {
944 .compatible = "renesas,pfc-r8a774a1",
945 .data = SH_PFC_R8A774A1,
946 },
947#endif
Biju Dasd1d78882020-10-28 10:34:21 +0000948#ifdef CONFIG_PINCTRL_PFC_R8A774B1
949 {
950 .compatible = "renesas,pfc-r8a774b1",
951 .data = SH_PFC_R8A774B1,
952 },
953#endif
Biju Das121bd002020-10-28 10:34:22 +0000954#ifdef CONFIG_PINCTRL_PFC_R8A774E1
955 {
956 .compatible = "renesas,pfc-r8a774e1",
957 .data = SH_PFC_R8A774E1,
958 },
959#endif
Marek Vasut72269e02019-03-04 01:32:44 +0100960#ifdef CONFIG_PINCTRL_PFC_R8A77965
961 {
Marek Vasut20d721e2018-02-26 10:35:15 +0100962 .compatible = "renesas,pfc-r8a77965",
Marek Vasut72269e02019-03-04 01:32:44 +0100963 .data = SH_PFC_R8A77965,
Marek Vasut3066a062017-09-15 21:13:55 +0200964 },
965#endif
Marek Vasuta0e11e52017-10-09 20:57:29 +0200966#ifdef CONFIG_PINCTRL_PFC_R8A77970
967 {
968 .compatible = "renesas,pfc-r8a77970",
969 .data = SH_PFC_R8A77970,
970 },
971#endif
Marek Vasuta6a7f482019-07-29 19:59:44 +0200972#ifdef CONFIG_PINCTRL_PFC_R8A77980
973 {
974 .compatible = "renesas,pfc-r8a77980",
975 .data = SH_PFC_R8A77980,
976 },
977#endif
Marek Vasut68a77042018-04-26 13:09:20 +0200978#ifdef CONFIG_PINCTRL_PFC_R8A77990
979 {
980 .compatible = "renesas,pfc-r8a77990",
981 .data = SH_PFC_R8A77990,
982 },
983#endif
Marek Vasut7d35e642017-10-08 20:57:37 +0200984#ifdef CONFIG_PINCTRL_PFC_R8A77995
985 {
986 .compatible = "renesas,pfc-r8a77995",
987 .data = SH_PFC_R8A77995,
988 },
989#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200990 { },
991};
992
993U_BOOT_DRIVER(pinctrl_sh_pfc) = {
994 .name = "sh_pfc_pinctrl",
995 .id = UCLASS_PINCTRL,
996 .of_match = sh_pfc_pinctrl_ids,
997 .priv_auto_alloc_size = sizeof(struct sh_pfc_pinctrl_priv),
998 .ops = &sh_pfc_pinctrl_ops,
999 .probe = sh_pfc_pinctrl_probe,
1000};