blob: 5ee11615de26e31e2ce1264ba6ea02e7d5eb54fe [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 Glassd66c5f72020-02-03 07:36:15 -070017#include <dm/devres.h>
Marek Vasut3066a062017-09-15 21:13:55 +020018#include <dm/pinctrl.h>
19#include <linux/io.h>
20#include <linux/sizes.h>
21
22#include "sh_pfc.h"
23
Marek Vasut3066a062017-09-15 21:13:55 +020024enum sh_pfc_model {
Marek Vasutc40f2d62018-01-17 22:18:59 +010025 SH_PFC_R8A7790 = 0,
Marek Vasut06ef9e82018-01-17 17:14:45 +010026 SH_PFC_R8A7791,
Marek Vasut1ef39302018-01-17 22:29:50 +010027 SH_PFC_R8A7792,
Marek Vasut06ef9e82018-01-17 17:14:45 +010028 SH_PFC_R8A7793,
Marek Vasut4dd88d52018-01-17 22:33:59 +010029 SH_PFC_R8A7794,
Marek Vasutc40f2d62018-01-17 22:18:59 +010030 SH_PFC_R8A7795,
Marek Vasut3066a062017-09-15 21:13:55 +020031 SH_PFC_R8A7796,
Marek Vasut72269e02019-03-04 01:32:44 +010032 SH_PFC_R8A77965,
Marek Vasuta0e11e52017-10-09 20:57:29 +020033 SH_PFC_R8A77970,
Marek Vasuta6a7f482019-07-29 19:59:44 +020034 SH_PFC_R8A77980,
Marek Vasut68a77042018-04-26 13:09:20 +020035 SH_PFC_R8A77990,
Marek Vasut7d35e642017-10-08 20:57:37 +020036 SH_PFC_R8A77995,
Marek Vasut3066a062017-09-15 21:13:55 +020037};
38
39struct sh_pfc_pin_config {
40 u32 type;
41};
42
43struct sh_pfc_pinctrl {
44 struct sh_pfc *pfc;
45
46 struct sh_pfc_pin_config *configs;
47
48 const char *func_prop_name;
49 const char *groups_prop_name;
50 const char *pins_prop_name;
51};
52
53struct sh_pfc_pin_range {
54 u16 start;
55 u16 end;
56};
57
58struct sh_pfc_pinctrl_priv {
59 struct sh_pfc pfc;
60 struct sh_pfc_pinctrl pmx;
61};
62
63int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
64{
65 unsigned int offset;
66 unsigned int i;
67
68 for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
69 const struct sh_pfc_pin_range *range = &pfc->ranges[i];
70
71 if (pin <= range->end)
72 return pin >= range->start
73 ? offset + pin - range->start : -1;
74
75 offset += range->end - range->start + 1;
76 }
77
78 return -EINVAL;
79}
80
81static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
82{
83 if (enum_id < r->begin)
84 return 0;
85
86 if (enum_id > r->end)
87 return 0;
88
89 return 1;
90}
91
92u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width)
93{
94 switch (reg_width) {
95 case 8:
96 return readb(mapped_reg);
97 case 16:
98 return readw(mapped_reg);
99 case 32:
100 return readl(mapped_reg);
101 }
102
103 BUG();
104 return 0;
105}
106
107void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width,
108 u32 data)
109{
110 switch (reg_width) {
111 case 8:
112 writeb(data, mapped_reg);
113 return;
114 case 16:
115 writew(data, mapped_reg);
116 return;
117 case 32:
118 writel(data, mapped_reg);
119 return;
120 }
121
122 BUG();
123}
124
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200125u32 sh_pfc_read(struct sh_pfc *pfc, u32 reg)
Marek Vasut3066a062017-09-15 21:13:55 +0200126{
Marek Vasut068a90b2018-06-19 06:13:42 +0200127 return sh_pfc_read_raw_reg((void __iomem *)(uintptr_t)reg, 32);
Marek Vasut3066a062017-09-15 21:13:55 +0200128}
129
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200130void sh_pfc_write(struct sh_pfc *pfc, u32 reg, u32 data)
Marek Vasut3066a062017-09-15 21:13:55 +0200131{
132 void __iomem *unlock_reg =
133 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
134
135 if (pfc->info->unlock_reg)
136 sh_pfc_write_raw_reg(unlock_reg, 32, ~data);
137
Marek Vasut068a90b2018-06-19 06:13:42 +0200138 sh_pfc_write_raw_reg((void __iomem *)(uintptr_t)reg, 32, data);
Marek Vasut3066a062017-09-15 21:13:55 +0200139}
140
141static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
142 const struct pinmux_cfg_reg *crp,
143 unsigned int in_pos,
144 void __iomem **mapped_regp, u32 *maskp,
145 unsigned int *posp)
146{
147 unsigned int k;
148
149 *mapped_regp = (void __iomem *)(uintptr_t)crp->reg;
150
151 if (crp->field_width) {
152 *maskp = (1 << crp->field_width) - 1;
153 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
154 } else {
155 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
156 *posp = crp->reg_width;
157 for (k = 0; k <= in_pos; k++)
158 *posp -= crp->var_field_width[k];
159 }
160}
161
162static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
163 const struct pinmux_cfg_reg *crp,
164 unsigned int field, u32 value)
165{
166 void __iomem *mapped_reg;
167 void __iomem *unlock_reg =
168 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
169 unsigned int pos;
170 u32 mask, data;
171
172 sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
173
174 dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, "
175 "r_width = %u, f_width = %u\n",
176 crp->reg, value, field, crp->reg_width, crp->field_width);
177
178 mask = ~(mask << pos);
179 value = value << pos;
180
181 data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
182 data &= mask;
183 data |= value;
184
185 if (pfc->info->unlock_reg)
186 sh_pfc_write_raw_reg(unlock_reg, 32, ~data);
187
188 sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
189}
190
191static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
192 const struct pinmux_cfg_reg **crp,
193 unsigned int *fieldp, u32 *valuep)
194{
195 unsigned int k = 0;
196
197 while (1) {
198 const struct pinmux_cfg_reg *config_reg =
199 pfc->info->cfg_regs + k;
200 unsigned int r_width = config_reg->reg_width;
201 unsigned int f_width = config_reg->field_width;
202 unsigned int curr_width;
203 unsigned int bit_pos;
204 unsigned int pos = 0;
205 unsigned int m = 0;
206
207 if (!r_width)
208 break;
209
210 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
211 u32 ncomb;
212 u32 n;
213
214 if (f_width)
215 curr_width = f_width;
216 else
217 curr_width = config_reg->var_field_width[m];
218
219 ncomb = 1 << curr_width;
220 for (n = 0; n < ncomb; n++) {
221 if (config_reg->enum_ids[pos + n] == enum_id) {
222 *crp = config_reg;
223 *fieldp = m;
224 *valuep = n;
225 return 0;
226 }
227 }
228 pos += ncomb;
229 m++;
230 }
231 k++;
232 }
233
234 return -EINVAL;
235}
236
237static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
238 u16 *enum_idp)
239{
240 const u16 *data = pfc->info->pinmux_data;
241 unsigned int k;
242
243 if (pos) {
244 *enum_idp = data[pos + 1];
245 return pos + 1;
246 }
247
248 for (k = 0; k < pfc->info->pinmux_data_size; k++) {
249 if (data[k] == mark) {
250 *enum_idp = data[k + 1];
251 return k + 1;
252 }
253 }
254
255 dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
256 mark);
257 return -EINVAL;
258}
259
260int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
261{
262 const struct pinmux_range *range;
263 int pos = 0;
264
265 switch (pinmux_type) {
266 case PINMUX_TYPE_GPIO:
267 case PINMUX_TYPE_FUNCTION:
268 range = NULL;
269 break;
270
271 case PINMUX_TYPE_OUTPUT:
272 range = &pfc->info->output;
273 break;
274
275 case PINMUX_TYPE_INPUT:
276 range = &pfc->info->input;
277 break;
278
279 default:
280 return -EINVAL;
281 }
282
283 /* Iterate over all the configuration fields we need to update. */
284 while (1) {
285 const struct pinmux_cfg_reg *cr;
286 unsigned int field;
287 u16 enum_id;
288 u32 value;
289 int in_range;
290 int ret;
291
292 pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
293 if (pos < 0)
294 return pos;
295
296 if (!enum_id)
297 break;
298
299 /* Check if the configuration field selects a function. If it
300 * doesn't, skip the field if it's not applicable to the
301 * requested pinmux type.
302 */
303 in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
304 if (!in_range) {
305 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
306 /* Functions are allowed to modify all
307 * fields.
308 */
309 in_range = 1;
310 } else if (pinmux_type != PINMUX_TYPE_GPIO) {
311 /* Input/output types can only modify fields
312 * that correspond to their respective ranges.
313 */
314 in_range = sh_pfc_enum_in_range(enum_id, range);
315
316 /*
317 * special case pass through for fixed
318 * input-only or output-only pins without
319 * function enum register association.
320 */
321 if (in_range && enum_id == range->force)
322 continue;
323 }
324 /* GPIOs are only allowed to modify function fields. */
325 }
326
327 if (!in_range)
328 continue;
329
330 ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
331 if (ret < 0)
332 return ret;
333
334 sh_pfc_write_config_reg(pfc, cr, field, value);
335 }
336
337 return 0;
338}
339
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200340const struct pinmux_bias_reg *
341sh_pfc_pin_to_bias_reg(const struct sh_pfc *pfc, unsigned int pin,
342 unsigned int *bit)
Marek Vasut3066a062017-09-15 21:13:55 +0200343{
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200344 unsigned int i, j;
Marek Vasut3066a062017-09-15 21:13:55 +0200345
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200346 for (i = 0; pfc->info->bias_regs[i].puen; i++) {
347 for (j = 0; j < ARRAY_SIZE(pfc->info->bias_regs[i].pins); j++) {
348 if (pfc->info->bias_regs[i].pins[j] == pin) {
349 *bit = j;
350 return &pfc->info->bias_regs[i];
351 }
352 }
353 }
Marek Vasut3066a062017-09-15 21:13:55 +0200354
Marek Vasuteb13e0f2018-06-10 16:05:48 +0200355 WARN_ONCE(1, "Pin %u is not in bias info list\n", pin);
Marek Vasut3066a062017-09-15 21:13:55 +0200356
357 return NULL;
358}
359
360static int sh_pfc_init_ranges(struct sh_pfc *pfc)
361{
362 struct sh_pfc_pin_range *range;
363 unsigned int nr_ranges;
364 unsigned int i;
365
366 if (pfc->info->pins[0].pin == (u16)-1) {
367 /* Pin number -1 denotes that the SoC doesn't report pin numbers
368 * in its pin arrays yet. Consider the pin numbers range as
369 * continuous and allocate a single range.
370 */
371 pfc->nr_ranges = 1;
372 pfc->ranges = kzalloc(sizeof(*pfc->ranges), GFP_KERNEL);
373 if (pfc->ranges == NULL)
374 return -ENOMEM;
375
376 pfc->ranges->start = 0;
377 pfc->ranges->end = pfc->info->nr_pins - 1;
378 pfc->nr_gpio_pins = pfc->info->nr_pins;
379
380 return 0;
381 }
382
383 /* Count, allocate and fill the ranges. The PFC SoC data pins array must
384 * be sorted by pin numbers, and pins without a GPIO port must come
385 * last.
386 */
387 for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
388 if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
389 nr_ranges++;
390 }
391
392 pfc->nr_ranges = nr_ranges;
393 pfc->ranges = kzalloc(sizeof(*pfc->ranges) * nr_ranges, GFP_KERNEL);
394 if (pfc->ranges == NULL)
395 return -ENOMEM;
396
397 range = pfc->ranges;
398 range->start = pfc->info->pins[0].pin;
399
400 for (i = 1; i < pfc->info->nr_pins; ++i) {
401 if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
402 continue;
403
404 range->end = pfc->info->pins[i-1].pin;
405 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
406 pfc->nr_gpio_pins = range->end + 1;
407
408 range++;
409 range->start = pfc->info->pins[i].pin;
410 }
411
412 range->end = pfc->info->pins[i-1].pin;
413 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
414 pfc->nr_gpio_pins = range->end + 1;
415
416 return 0;
417}
418
419static int sh_pfc_pinctrl_get_pins_count(struct udevice *dev)
420{
421 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
422
423 return priv->pfc.info->nr_pins;
424}
425
426static const char *sh_pfc_pinctrl_get_pin_name(struct udevice *dev,
427 unsigned selector)
428{
429 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
430
431 return priv->pfc.info->pins[selector].name;
432}
433
434static int sh_pfc_pinctrl_get_groups_count(struct udevice *dev)
435{
436 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
437
438 return priv->pfc.info->nr_groups;
439}
440
441static const char *sh_pfc_pinctrl_get_group_name(struct udevice *dev,
442 unsigned selector)
443{
444 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
445
446 return priv->pfc.info->groups[selector].name;
447}
448
449static int sh_pfc_pinctrl_get_functions_count(struct udevice *dev)
450{
451 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
452
453 return priv->pfc.info->nr_functions;
454}
455
456static const char *sh_pfc_pinctrl_get_function_name(struct udevice *dev,
457 unsigned selector)
458{
459 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
460
461 return priv->pfc.info->functions[selector].name;
462}
463
Marek Vasut02d34f02019-04-21 22:46:25 +0200464static int sh_pfc_gpio_request_enable(struct udevice *dev,
465 unsigned pin_selector)
Marek Vasut489d79c2017-11-26 18:07:29 +0100466{
467 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
468 struct sh_pfc_pinctrl *pmx = &priv->pmx;
469 struct sh_pfc *pfc = &priv->pfc;
470 struct sh_pfc_pin_config *cfg;
471 const struct sh_pfc_pin *pin = NULL;
Marek Vasut0cc19362019-04-21 22:46:25 +0200472 int i, ret, idx;
Marek Vasut489d79c2017-11-26 18:07:29 +0100473
titron09bf4982019-07-22 17:45:37 +0800474 for (i = 0; i < pfc->info->nr_pins; i++) {
Marek Vasut489d79c2017-11-26 18:07:29 +0100475 if (priv->pfc.info->pins[i].pin != pin_selector)
476 continue;
477
478 pin = &priv->pfc.info->pins[i];
479 break;
480 }
481
482 if (!pin)
483 return -EINVAL;
484
485 idx = sh_pfc_get_pin_index(pfc, pin->pin);
486 cfg = &pmx->configs[idx];
487
488 if (cfg->type != PINMUX_TYPE_NONE)
489 return -EBUSY;
490
Marek Vasut0cc19362019-04-21 22:46:25 +0200491 ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
492 if (ret)
493 return ret;
494
495 cfg->type = PINMUX_TYPE_GPIO;
496
497 return 0;
Marek Vasut489d79c2017-11-26 18:07:29 +0100498}
499
Marek Vasut02d34f02019-04-21 22:46:25 +0200500static int sh_pfc_gpio_disable_free(struct udevice *dev,
501 unsigned pin_selector)
502{
503 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
504 struct sh_pfc_pinctrl *pmx = &priv->pmx;
505 struct sh_pfc *pfc = &priv->pfc;
506 struct sh_pfc_pin_config *cfg;
507 const struct sh_pfc_pin *pin = NULL;
508 int i, idx;
509
titron09bf4982019-07-22 17:45:37 +0800510 for (i = 0; i < pfc->info->nr_pins; i++) {
Marek Vasut02d34f02019-04-21 22:46:25 +0200511 if (priv->pfc.info->pins[i].pin != pin_selector)
512 continue;
513
514 pin = &priv->pfc.info->pins[i];
515 break;
516 }
517
518 if (!pin)
519 return -EINVAL;
520
521 idx = sh_pfc_get_pin_index(pfc, pin->pin);
522 cfg = &pmx->configs[idx];
523
524 cfg->type = PINMUX_TYPE_NONE;
525
526 return 0;
527}
528
Marek Vasut5e6db842017-11-26 17:42:16 +0100529static int sh_pfc_pinctrl_pin_set(struct udevice *dev, unsigned pin_selector,
530 unsigned func_selector)
531{
532 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
533 struct sh_pfc_pinctrl *pmx = &priv->pmx;
534 struct sh_pfc *pfc = &priv->pfc;
535 const struct sh_pfc_pin *pin = &priv->pfc.info->pins[pin_selector];
536 int idx = sh_pfc_get_pin_index(pfc, pin->pin);
537 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
538
539 if (cfg->type != PINMUX_TYPE_NONE)
540 return -EBUSY;
541
542 return sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_FUNCTION);
543}
544
Marek Vasut3066a062017-09-15 21:13:55 +0200545static int sh_pfc_pinctrl_group_set(struct udevice *dev, unsigned group_selector,
546 unsigned func_selector)
547{
548 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
549 struct sh_pfc_pinctrl *pmx = &priv->pmx;
550 struct sh_pfc *pfc = &priv->pfc;
551 const struct sh_pfc_pin_group *grp = &priv->pfc.info->groups[group_selector];
552 unsigned int i;
553 int ret = 0;
554
555 for (i = 0; i < grp->nr_pins; ++i) {
556 int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
557 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
558
559 if (cfg->type != PINMUX_TYPE_NONE) {
560 ret = -EBUSY;
561 goto done;
562 }
563 }
564
565 for (i = 0; i < grp->nr_pins; ++i) {
566 ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
567 if (ret < 0)
568 break;
569 }
570
571done:
572 return ret;
573}
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200574#if CONFIG_IS_ENABLED(PINCONF)
575static const struct pinconf_param sh_pfc_pinconf_params[] = {
576 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
577 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
578 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
579 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
580 { "power-source", PIN_CONFIG_POWER_SOURCE, 3300 },
581};
582
583static void __iomem *
584sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc, unsigned int pin,
585 unsigned int *offset, unsigned int *size)
586{
587 const struct pinmux_drive_reg_field *field;
588 const struct pinmux_drive_reg *reg;
589 unsigned int i;
590
591 for (reg = pfc->info->drive_regs; reg->reg; ++reg) {
592 for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) {
593 field = &reg->fields[i];
594
595 if (field->size && field->pin == pin) {
596 *offset = field->offset;
597 *size = field->size;
598
599 return (void __iomem *)(uintptr_t)reg->reg;
600 }
601 }
602 }
603
604 return NULL;
605}
606
607static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc,
608 unsigned int pin, u16 strength)
609{
610 unsigned int offset;
611 unsigned int size;
612 unsigned int step;
613 void __iomem *reg;
614 void __iomem *unlock_reg =
615 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
616 u32 val;
617
618 reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
619 if (!reg)
620 return -EINVAL;
621
622 step = size == 2 ? 6 : 3;
623
624 if (strength < step || strength > 24)
625 return -EINVAL;
626
627 /* Convert the value from mA based on a full drive strength value of
628 * 24mA. We can make the full value configurable later if needed.
629 */
630 strength = strength / step - 1;
631
632 val = sh_pfc_read_raw_reg(reg, 32);
Marek Vasut0d9c8102018-06-13 08:02:55 +0200633 val &= ~GENMASK(offset + 4 - 1, offset);
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200634 val |= strength << offset;
635
636 if (unlock_reg)
637 sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
638
639 sh_pfc_write_raw_reg(reg, 32, val);
640
641 return 0;
642}
643
644/* Check whether the requested parameter is supported for a pin. */
645static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
646 unsigned int param)
647{
648 int idx = sh_pfc_get_pin_index(pfc, _pin);
649 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
650
651 switch (param) {
652 case PIN_CONFIG_BIAS_DISABLE:
653 return pin->configs &
654 (SH_PFC_PIN_CFG_PULL_UP | SH_PFC_PIN_CFG_PULL_DOWN);
655
656 case PIN_CONFIG_BIAS_PULL_UP:
657 return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
658
659 case PIN_CONFIG_BIAS_PULL_DOWN:
660 return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
661
662 case PIN_CONFIG_DRIVE_STRENGTH:
663 return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH;
664
665 case PIN_CONFIG_POWER_SOURCE:
666 return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;
667
668 default:
669 return false;
670 }
671}
672
673static int sh_pfc_pinconf_set(struct sh_pfc_pinctrl *pmx, unsigned _pin,
674 unsigned int param, unsigned int arg)
675{
676 struct sh_pfc *pfc = pmx->pfc;
677 void __iomem *pocctrl;
678 void __iomem *unlock_reg =
679 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
680 u32 addr, val;
681 int bit, ret;
682
683 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
684 return -ENOTSUPP;
685
686 switch (param) {
687 case PIN_CONFIG_BIAS_PULL_UP:
688 case PIN_CONFIG_BIAS_PULL_DOWN:
689 case PIN_CONFIG_BIAS_DISABLE:
690 if (!pfc->info->ops || !pfc->info->ops->set_bias)
691 return -ENOTSUPP;
692
693 pfc->info->ops->set_bias(pfc, _pin, param);
694
695 break;
696
697 case PIN_CONFIG_DRIVE_STRENGTH:
698 ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg);
699 if (ret < 0)
700 return ret;
701
702 break;
703
704 case PIN_CONFIG_POWER_SOURCE:
705 if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
706 return -ENOTSUPP;
707
708 bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &addr);
709 if (bit < 0) {
710 printf("invalid pin %#x", _pin);
711 return bit;
712 }
713
714 if (arg != 1800 && arg != 3300)
715 return -EINVAL;
716
717 pocctrl = (void __iomem *)(uintptr_t)addr;
718
719 val = sh_pfc_read_raw_reg(pocctrl, 32);
720 if (arg == 3300)
721 val |= BIT(bit);
722 else
723 val &= ~BIT(bit);
724
725 if (unlock_reg)
726 sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
727
728 sh_pfc_write_raw_reg(pocctrl, 32, val);
729
730 break;
731
732 default:
733 return -ENOTSUPP;
734 }
735
736 return 0;
737}
738
Marek Vasut5e6db842017-11-26 17:42:16 +0100739static int sh_pfc_pinconf_pin_set(struct udevice *dev,
740 unsigned int pin_selector,
741 unsigned int param, unsigned int arg)
742{
743 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
744 struct sh_pfc_pinctrl *pmx = &priv->pmx;
745 struct sh_pfc *pfc = &priv->pfc;
746 const struct sh_pfc_pin *pin = &pfc->info->pins[pin_selector];
747
748 sh_pfc_pinconf_set(pmx, pin->pin, param, arg);
749
750 return 0;
751}
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200752
753static int sh_pfc_pinconf_group_set(struct udevice *dev,
754 unsigned int group_selector,
755 unsigned int param, unsigned int arg)
756{
757 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
758 struct sh_pfc_pinctrl *pmx = &priv->pmx;
759 struct sh_pfc *pfc = &priv->pfc;
760 const struct sh_pfc_pin_group *grp = &pfc->info->groups[group_selector];
761 unsigned int i;
762
763 for (i = 0; i < grp->nr_pins; i++)
764 sh_pfc_pinconf_set(pmx, grp->pins[i], param, arg);
765
766 return 0;
767}
768#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200769
770static struct pinctrl_ops sh_pfc_pinctrl_ops = {
771 .get_pins_count = sh_pfc_pinctrl_get_pins_count,
772 .get_pin_name = sh_pfc_pinctrl_get_pin_name,
773 .get_groups_count = sh_pfc_pinctrl_get_groups_count,
774 .get_group_name = sh_pfc_pinctrl_get_group_name,
775 .get_functions_count = sh_pfc_pinctrl_get_functions_count,
776 .get_function_name = sh_pfc_pinctrl_get_function_name,
777
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200778#if CONFIG_IS_ENABLED(PINCONF)
779 .pinconf_num_params = ARRAY_SIZE(sh_pfc_pinconf_params),
780 .pinconf_params = sh_pfc_pinconf_params,
Marek Vasut5e6db842017-11-26 17:42:16 +0100781 .pinconf_set = sh_pfc_pinconf_pin_set,
Marek Vasutc9dd9ae2017-09-28 00:56:24 +0200782 .pinconf_group_set = sh_pfc_pinconf_group_set,
783#endif
Marek Vasut5e6db842017-11-26 17:42:16 +0100784 .pinmux_set = sh_pfc_pinctrl_pin_set,
Marek Vasut3066a062017-09-15 21:13:55 +0200785 .pinmux_group_set = sh_pfc_pinctrl_group_set,
786 .set_state = pinctrl_generic_set_state,
Marek Vasut02d34f02019-04-21 22:46:25 +0200787
788 .gpio_request_enable = sh_pfc_gpio_request_enable,
789 .gpio_disable_free = sh_pfc_gpio_disable_free,
Marek Vasut3066a062017-09-15 21:13:55 +0200790};
791
792static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
793{
794 unsigned int i;
795
796 /* Allocate and initialize the pins and configs arrays. */
797 pmx->configs = kzalloc(sizeof(*pmx->configs) * pfc->info->nr_pins,
798 GFP_KERNEL);
799 if (unlikely(!pmx->configs))
800 return -ENOMEM;
801
802 for (i = 0; i < pfc->info->nr_pins; ++i) {
803 struct sh_pfc_pin_config *cfg = &pmx->configs[i];
804 cfg->type = PINMUX_TYPE_NONE;
805 }
806
807 return 0;
808}
809
810
811static int sh_pfc_pinctrl_probe(struct udevice *dev)
812{
813 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
814 enum sh_pfc_model model = dev_get_driver_data(dev);
815 fdt_addr_t base;
816
817 base = devfdt_get_addr(dev);
818 if (base == FDT_ADDR_T_NONE)
819 return -EINVAL;
820
821 priv->pfc.regs = devm_ioremap(dev, base, SZ_2K);
822 if (!priv->pfc.regs)
823 return -ENOMEM;
824
Marek Vasutc40f2d62018-01-17 22:18:59 +0100825#ifdef CONFIG_PINCTRL_PFC_R8A7790
826 if (model == SH_PFC_R8A7790)
827 priv->pfc.info = &r8a7790_pinmux_info;
828#endif
Marek Vasut06ef9e82018-01-17 17:14:45 +0100829#ifdef CONFIG_PINCTRL_PFC_R8A7791
830 if (model == SH_PFC_R8A7791)
831 priv->pfc.info = &r8a7791_pinmux_info;
832#endif
Marek Vasut1ef39302018-01-17 22:29:50 +0100833#ifdef CONFIG_PINCTRL_PFC_R8A7792
834 if (model == SH_PFC_R8A7792)
835 priv->pfc.info = &r8a7792_pinmux_info;
836#endif
Marek Vasut06ef9e82018-01-17 17:14:45 +0100837#ifdef CONFIG_PINCTRL_PFC_R8A7793
838 if (model == SH_PFC_R8A7793)
839 priv->pfc.info = &r8a7793_pinmux_info;
840#endif
Marek Vasut4dd88d52018-01-17 22:33:59 +0100841#ifdef CONFIG_PINCTRL_PFC_R8A7794
842 if (model == SH_PFC_R8A7794)
843 priv->pfc.info = &r8a7794_pinmux_info;
844#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200845#ifdef CONFIG_PINCTRL_PFC_R8A7795
846 if (model == SH_PFC_R8A7795)
847 priv->pfc.info = &r8a7795_pinmux_info;
848#endif
849#ifdef CONFIG_PINCTRL_PFC_R8A7796
850 if (model == SH_PFC_R8A7796)
851 priv->pfc.info = &r8a7796_pinmux_info;
852#endif
Marek Vasut72269e02019-03-04 01:32:44 +0100853#ifdef CONFIG_PINCTRL_PFC_R8A77965
854 if (model == SH_PFC_R8A77965)
855 priv->pfc.info = &r8a77965_pinmux_info;
856#endif
Marek Vasuta0e11e52017-10-09 20:57:29 +0200857#ifdef CONFIG_PINCTRL_PFC_R8A77970
858 if (model == SH_PFC_R8A77970)
859 priv->pfc.info = &r8a77970_pinmux_info;
860#endif
Marek Vasuta6a7f482019-07-29 19:59:44 +0200861#ifdef CONFIG_PINCTRL_PFC_R8A77980
862 if (model == SH_PFC_R8A77980)
863 priv->pfc.info = &r8a77980_pinmux_info;
864#endif
Marek Vasut68a77042018-04-26 13:09:20 +0200865#ifdef CONFIG_PINCTRL_PFC_R8A77990
866 if (model == SH_PFC_R8A77990)
867 priv->pfc.info = &r8a77990_pinmux_info;
868#endif
Marek Vasut7d35e642017-10-08 20:57:37 +0200869#ifdef CONFIG_PINCTRL_PFC_R8A77995
870 if (model == SH_PFC_R8A77995)
871 priv->pfc.info = &r8a77995_pinmux_info;
872#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200873
874 priv->pmx.pfc = &priv->pfc;
875 sh_pfc_init_ranges(&priv->pfc);
876 sh_pfc_map_pins(&priv->pfc, &priv->pmx);
877
878 return 0;
879}
880
881static const struct udevice_id sh_pfc_pinctrl_ids[] = {
Marek Vasutc40f2d62018-01-17 22:18:59 +0100882#ifdef CONFIG_PINCTRL_PFC_R8A7790
883 {
884 .compatible = "renesas,pfc-r8a7790",
885 .data = SH_PFC_R8A7790,
886 },
887#endif
Marek Vasut06ef9e82018-01-17 17:14:45 +0100888#ifdef CONFIG_PINCTRL_PFC_R8A7791
889 {
890 .compatible = "renesas,pfc-r8a7791",
891 .data = SH_PFC_R8A7791,
892 },
893#endif
Marek Vasut1ef39302018-01-17 22:29:50 +0100894#ifdef CONFIG_PINCTRL_PFC_R8A7792
895 {
896 .compatible = "renesas,pfc-r8a7792",
897 .data = SH_PFC_R8A7792,
898 },
899#endif
Marek Vasut06ef9e82018-01-17 17:14:45 +0100900#ifdef CONFIG_PINCTRL_PFC_R8A7793
901 {
902 .compatible = "renesas,pfc-r8a7793",
903 .data = SH_PFC_R8A7793,
904 },
905#endif
Marek Vasut4dd88d52018-01-17 22:33:59 +0100906#ifdef CONFIG_PINCTRL_PFC_R8A7794
907 {
908 .compatible = "renesas,pfc-r8a7794",
909 .data = SH_PFC_R8A7794,
910 },
911#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200912#ifdef CONFIG_PINCTRL_PFC_R8A7795
913 {
914 .compatible = "renesas,pfc-r8a7795",
915 .data = SH_PFC_R8A7795,
916 },
917#endif
918#ifdef CONFIG_PINCTRL_PFC_R8A7796
919 {
920 .compatible = "renesas,pfc-r8a7796",
921 .data = SH_PFC_R8A7796,
Marek Vasut72269e02019-03-04 01:32:44 +0100922 },
923#endif
924#ifdef CONFIG_PINCTRL_PFC_R8A77965
925 {
Marek Vasut20d721e2018-02-26 10:35:15 +0100926 .compatible = "renesas,pfc-r8a77965",
Marek Vasut72269e02019-03-04 01:32:44 +0100927 .data = SH_PFC_R8A77965,
Marek Vasut3066a062017-09-15 21:13:55 +0200928 },
929#endif
Marek Vasuta0e11e52017-10-09 20:57:29 +0200930#ifdef CONFIG_PINCTRL_PFC_R8A77970
931 {
932 .compatible = "renesas,pfc-r8a77970",
933 .data = SH_PFC_R8A77970,
934 },
935#endif
Marek Vasuta6a7f482019-07-29 19:59:44 +0200936#ifdef CONFIG_PINCTRL_PFC_R8A77980
937 {
938 .compatible = "renesas,pfc-r8a77980",
939 .data = SH_PFC_R8A77980,
940 },
941#endif
Marek Vasut68a77042018-04-26 13:09:20 +0200942#ifdef CONFIG_PINCTRL_PFC_R8A77990
943 {
944 .compatible = "renesas,pfc-r8a77990",
945 .data = SH_PFC_R8A77990,
946 },
947#endif
Marek Vasut7d35e642017-10-08 20:57:37 +0200948#ifdef CONFIG_PINCTRL_PFC_R8A77995
949 {
950 .compatible = "renesas,pfc-r8a77995",
951 .data = SH_PFC_R8A77995,
952 },
953#endif
Marek Vasut3066a062017-09-15 21:13:55 +0200954 { },
955};
956
957U_BOOT_DRIVER(pinctrl_sh_pfc) = {
958 .name = "sh_pfc_pinctrl",
959 .id = UCLASS_PINCTRL,
960 .of_match = sh_pfc_pinctrl_ids,
961 .priv_auto_alloc_size = sizeof(struct sh_pfc_pinctrl_priv),
962 .ops = &sh_pfc_pinctrl_ops,
963 .probe = sh_pfc_pinctrl_probe,
964};