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