2 * Pin Control driver for SuperH Pin Function Controller.
4 * Authors: Magnus Damm, Paul Mundt, Laurent Pinchart
6 * Copyright (C) 2008 Magnus Damm
7 * Copyright (C) 2009 - 2012 Paul Mundt
8 * Copyright (C) 2017 Marek Vasut
10 * SPDX-License-Identifier: GPL-2.0
13 #define DRV_NAME "sh-pfc"
18 #include <dm/pinctrl.h>
20 #include <linux/sizes.h>
24 DECLARE_GLOBAL_DATA_PTR;
34 struct sh_pfc_pin_config {
38 struct sh_pfc_pinctrl {
41 struct sh_pfc_pin_config *configs;
43 const char *func_prop_name;
44 const char *groups_prop_name;
45 const char *pins_prop_name;
48 struct sh_pfc_pin_range {
53 struct sh_pfc_pinctrl_priv {
55 struct sh_pfc_pinctrl pmx;
58 int sh_pfc_get_pin_index(struct sh_pfc *pfc, unsigned int pin)
63 for (i = 0, offset = 0; i < pfc->nr_ranges; ++i) {
64 const struct sh_pfc_pin_range *range = &pfc->ranges[i];
66 if (pin <= range->end)
67 return pin >= range->start
68 ? offset + pin - range->start : -1;
70 offset += range->end - range->start + 1;
76 static int sh_pfc_enum_in_range(u16 enum_id, const struct pinmux_range *r)
78 if (enum_id < r->begin)
87 u32 sh_pfc_read_raw_reg(void __iomem *mapped_reg, unsigned int reg_width)
91 return readb(mapped_reg);
93 return readw(mapped_reg);
95 return readl(mapped_reg);
102 void sh_pfc_write_raw_reg(void __iomem *mapped_reg, unsigned int reg_width,
107 writeb(data, mapped_reg);
110 writew(data, mapped_reg);
113 writel(data, mapped_reg);
120 u32 sh_pfc_read_reg(struct sh_pfc *pfc, u32 reg, unsigned int width)
122 return sh_pfc_read_raw_reg(pfc->regs + reg, width);
125 void sh_pfc_write_reg(struct sh_pfc *pfc, u32 reg, unsigned int width, u32 data)
127 void __iomem *unlock_reg =
128 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
130 if (pfc->info->unlock_reg)
131 sh_pfc_write_raw_reg(unlock_reg, 32, ~data);
133 sh_pfc_write_raw_reg(pfc->regs + reg, width, data);
136 static void sh_pfc_config_reg_helper(struct sh_pfc *pfc,
137 const struct pinmux_cfg_reg *crp,
139 void __iomem **mapped_regp, u32 *maskp,
144 *mapped_regp = (void __iomem *)(uintptr_t)crp->reg;
146 if (crp->field_width) {
147 *maskp = (1 << crp->field_width) - 1;
148 *posp = crp->reg_width - ((in_pos + 1) * crp->field_width);
150 *maskp = (1 << crp->var_field_width[in_pos]) - 1;
151 *posp = crp->reg_width;
152 for (k = 0; k <= in_pos; k++)
153 *posp -= crp->var_field_width[k];
157 static void sh_pfc_write_config_reg(struct sh_pfc *pfc,
158 const struct pinmux_cfg_reg *crp,
159 unsigned int field, u32 value)
161 void __iomem *mapped_reg;
162 void __iomem *unlock_reg =
163 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
167 sh_pfc_config_reg_helper(pfc, crp, field, &mapped_reg, &mask, &pos);
169 dev_dbg(pfc->dev, "write_reg addr = %x, value = 0x%x, field = %u, "
170 "r_width = %u, f_width = %u\n",
171 crp->reg, value, field, crp->reg_width, crp->field_width);
173 mask = ~(mask << pos);
174 value = value << pos;
176 data = sh_pfc_read_raw_reg(mapped_reg, crp->reg_width);
180 if (pfc->info->unlock_reg)
181 sh_pfc_write_raw_reg(unlock_reg, 32, ~data);
183 sh_pfc_write_raw_reg(mapped_reg, crp->reg_width, data);
186 static int sh_pfc_get_config_reg(struct sh_pfc *pfc, u16 enum_id,
187 const struct pinmux_cfg_reg **crp,
188 unsigned int *fieldp, u32 *valuep)
193 const struct pinmux_cfg_reg *config_reg =
194 pfc->info->cfg_regs + k;
195 unsigned int r_width = config_reg->reg_width;
196 unsigned int f_width = config_reg->field_width;
197 unsigned int curr_width;
198 unsigned int bit_pos;
199 unsigned int pos = 0;
205 for (bit_pos = 0; bit_pos < r_width; bit_pos += curr_width) {
210 curr_width = f_width;
212 curr_width = config_reg->var_field_width[m];
214 ncomb = 1 << curr_width;
215 for (n = 0; n < ncomb; n++) {
216 if (config_reg->enum_ids[pos + n] == enum_id) {
232 static int sh_pfc_mark_to_enum(struct sh_pfc *pfc, u16 mark, int pos,
235 const u16 *data = pfc->info->pinmux_data;
239 *enum_idp = data[pos + 1];
243 for (k = 0; k < pfc->info->pinmux_data_size; k++) {
244 if (data[k] == mark) {
245 *enum_idp = data[k + 1];
250 dev_err(pfc->dev, "cannot locate data/mark enum_id for mark %d\n",
255 int sh_pfc_config_mux(struct sh_pfc *pfc, unsigned mark, int pinmux_type)
257 const struct pinmux_range *range;
260 switch (pinmux_type) {
261 case PINMUX_TYPE_GPIO:
262 case PINMUX_TYPE_FUNCTION:
266 case PINMUX_TYPE_OUTPUT:
267 range = &pfc->info->output;
270 case PINMUX_TYPE_INPUT:
271 range = &pfc->info->input;
278 /* Iterate over all the configuration fields we need to update. */
280 const struct pinmux_cfg_reg *cr;
287 pos = sh_pfc_mark_to_enum(pfc, mark, pos, &enum_id);
294 /* Check if the configuration field selects a function. If it
295 * doesn't, skip the field if it's not applicable to the
296 * requested pinmux type.
298 in_range = sh_pfc_enum_in_range(enum_id, &pfc->info->function);
300 if (pinmux_type == PINMUX_TYPE_FUNCTION) {
301 /* Functions are allowed to modify all
305 } else if (pinmux_type != PINMUX_TYPE_GPIO) {
306 /* Input/output types can only modify fields
307 * that correspond to their respective ranges.
309 in_range = sh_pfc_enum_in_range(enum_id, range);
312 * special case pass through for fixed
313 * input-only or output-only pins without
314 * function enum register association.
316 if (in_range && enum_id == range->force)
319 /* GPIOs are only allowed to modify function fields. */
325 ret = sh_pfc_get_config_reg(pfc, enum_id, &cr, &field, &value);
329 sh_pfc_write_config_reg(pfc, cr, field, value);
335 const struct sh_pfc_bias_info *
336 sh_pfc_pin_to_bias_info(const struct sh_pfc_bias_info *info,
337 unsigned int num, unsigned int pin)
341 for (i = 0; i < num; i++)
342 if (info[i].pin == pin)
345 printf("Pin %u is not in bias info list\n", pin);
350 static int sh_pfc_init_ranges(struct sh_pfc *pfc)
352 struct sh_pfc_pin_range *range;
353 unsigned int nr_ranges;
356 if (pfc->info->pins[0].pin == (u16)-1) {
357 /* Pin number -1 denotes that the SoC doesn't report pin numbers
358 * in its pin arrays yet. Consider the pin numbers range as
359 * continuous and allocate a single range.
362 pfc->ranges = kzalloc(sizeof(*pfc->ranges), GFP_KERNEL);
363 if (pfc->ranges == NULL)
366 pfc->ranges->start = 0;
367 pfc->ranges->end = pfc->info->nr_pins - 1;
368 pfc->nr_gpio_pins = pfc->info->nr_pins;
373 /* Count, allocate and fill the ranges. The PFC SoC data pins array must
374 * be sorted by pin numbers, and pins without a GPIO port must come
377 for (i = 1, nr_ranges = 1; i < pfc->info->nr_pins; ++i) {
378 if (pfc->info->pins[i-1].pin != pfc->info->pins[i].pin - 1)
382 pfc->nr_ranges = nr_ranges;
383 pfc->ranges = kzalloc(sizeof(*pfc->ranges) * nr_ranges, GFP_KERNEL);
384 if (pfc->ranges == NULL)
388 range->start = pfc->info->pins[0].pin;
390 for (i = 1; i < pfc->info->nr_pins; ++i) {
391 if (pfc->info->pins[i-1].pin == pfc->info->pins[i].pin - 1)
394 range->end = pfc->info->pins[i-1].pin;
395 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
396 pfc->nr_gpio_pins = range->end + 1;
399 range->start = pfc->info->pins[i].pin;
402 range->end = pfc->info->pins[i-1].pin;
403 if (!(pfc->info->pins[i-1].configs & SH_PFC_PIN_CFG_NO_GPIO))
404 pfc->nr_gpio_pins = range->end + 1;
409 static int sh_pfc_pinctrl_get_pins_count(struct udevice *dev)
411 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
413 return priv->pfc.info->nr_pins;
416 static const char *sh_pfc_pinctrl_get_pin_name(struct udevice *dev,
419 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
421 return priv->pfc.info->pins[selector].name;
424 static int sh_pfc_pinctrl_get_groups_count(struct udevice *dev)
426 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
428 return priv->pfc.info->nr_groups;
431 static const char *sh_pfc_pinctrl_get_group_name(struct udevice *dev,
434 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
436 return priv->pfc.info->groups[selector].name;
439 static int sh_pfc_pinctrl_get_functions_count(struct udevice *dev)
441 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
443 return priv->pfc.info->nr_functions;
446 static const char *sh_pfc_pinctrl_get_function_name(struct udevice *dev,
449 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
451 return priv->pfc.info->functions[selector].name;
454 int sh_pfc_config_mux_for_gpio(struct udevice *dev, unsigned pin_selector)
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 = NULL;
463 for (i = 1; i < pfc->info->nr_pins; i++) {
464 if (priv->pfc.info->pins[i].pin != pin_selector)
467 pin = &priv->pfc.info->pins[i];
474 idx = sh_pfc_get_pin_index(pfc, pin->pin);
475 cfg = &pmx->configs[idx];
477 if (cfg->type != PINMUX_TYPE_NONE)
480 return sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
483 static int sh_pfc_pinctrl_pin_set(struct udevice *dev, unsigned pin_selector,
484 unsigned func_selector)
486 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
487 struct sh_pfc_pinctrl *pmx = &priv->pmx;
488 struct sh_pfc *pfc = &priv->pfc;
489 const struct sh_pfc_pin *pin = &priv->pfc.info->pins[pin_selector];
490 int idx = sh_pfc_get_pin_index(pfc, pin->pin);
491 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
493 if (cfg->type != PINMUX_TYPE_NONE)
496 return sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_FUNCTION);
499 static int sh_pfc_pinctrl_group_set(struct udevice *dev, unsigned group_selector,
500 unsigned func_selector)
502 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
503 struct sh_pfc_pinctrl *pmx = &priv->pmx;
504 struct sh_pfc *pfc = &priv->pfc;
505 const struct sh_pfc_pin_group *grp = &priv->pfc.info->groups[group_selector];
509 for (i = 0; i < grp->nr_pins; ++i) {
510 int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
511 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
513 if (cfg->type != PINMUX_TYPE_NONE) {
519 for (i = 0; i < grp->nr_pins; ++i) {
520 ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
528 #if CONFIG_IS_ENABLED(PINCONF)
529 static const struct pinconf_param sh_pfc_pinconf_params[] = {
530 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
531 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
532 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
533 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
534 { "power-source", PIN_CONFIG_POWER_SOURCE, 3300 },
537 static void __iomem *
538 sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc, unsigned int pin,
539 unsigned int *offset, unsigned int *size)
541 const struct pinmux_drive_reg_field *field;
542 const struct pinmux_drive_reg *reg;
545 for (reg = pfc->info->drive_regs; reg->reg; ++reg) {
546 for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) {
547 field = ®->fields[i];
549 if (field->size && field->pin == pin) {
550 *offset = field->offset;
553 return (void __iomem *)(uintptr_t)reg->reg;
561 static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc,
562 unsigned int pin, u16 strength)
568 void __iomem *unlock_reg =
569 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
572 reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
576 step = size == 2 ? 6 : 3;
578 if (strength < step || strength > 24)
581 /* Convert the value from mA based on a full drive strength value of
582 * 24mA. We can make the full value configurable later if needed.
584 strength = strength / step - 1;
586 val = sh_pfc_read_raw_reg(reg, 32);
587 val &= ~GENMASK(offset + size - 1, offset);
588 val |= strength << offset;
591 sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
593 sh_pfc_write_raw_reg(reg, 32, val);
598 /* Check whether the requested parameter is supported for a pin. */
599 static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
602 int idx = sh_pfc_get_pin_index(pfc, _pin);
603 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
606 case PIN_CONFIG_BIAS_DISABLE:
607 return pin->configs &
608 (SH_PFC_PIN_CFG_PULL_UP | SH_PFC_PIN_CFG_PULL_DOWN);
610 case PIN_CONFIG_BIAS_PULL_UP:
611 return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
613 case PIN_CONFIG_BIAS_PULL_DOWN:
614 return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
616 case PIN_CONFIG_DRIVE_STRENGTH:
617 return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH;
619 case PIN_CONFIG_POWER_SOURCE:
620 return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;
627 static int sh_pfc_pinconf_set(struct sh_pfc_pinctrl *pmx, unsigned _pin,
628 unsigned int param, unsigned int arg)
630 struct sh_pfc *pfc = pmx->pfc;
631 void __iomem *pocctrl;
632 void __iomem *unlock_reg =
633 (void __iomem *)(uintptr_t)pfc->info->unlock_reg;
637 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
641 case PIN_CONFIG_BIAS_PULL_UP:
642 case PIN_CONFIG_BIAS_PULL_DOWN:
643 case PIN_CONFIG_BIAS_DISABLE:
644 if (!pfc->info->ops || !pfc->info->ops->set_bias)
647 pfc->info->ops->set_bias(pfc, _pin, param);
651 case PIN_CONFIG_DRIVE_STRENGTH:
652 ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg);
658 case PIN_CONFIG_POWER_SOURCE:
659 if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
662 bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &addr);
664 printf("invalid pin %#x", _pin);
668 if (arg != 1800 && arg != 3300)
671 pocctrl = (void __iomem *)(uintptr_t)addr;
673 val = sh_pfc_read_raw_reg(pocctrl, 32);
680 sh_pfc_write_raw_reg(unlock_reg, 32, ~val);
682 sh_pfc_write_raw_reg(pocctrl, 32, val);
693 static int sh_pfc_pinconf_pin_set(struct udevice *dev,
694 unsigned int pin_selector,
695 unsigned int param, unsigned int arg)
697 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
698 struct sh_pfc_pinctrl *pmx = &priv->pmx;
699 struct sh_pfc *pfc = &priv->pfc;
700 const struct sh_pfc_pin *pin = &pfc->info->pins[pin_selector];
702 sh_pfc_pinconf_set(pmx, pin->pin, param, arg);
707 static int sh_pfc_pinconf_group_set(struct udevice *dev,
708 unsigned int group_selector,
709 unsigned int param, unsigned int arg)
711 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
712 struct sh_pfc_pinctrl *pmx = &priv->pmx;
713 struct sh_pfc *pfc = &priv->pfc;
714 const struct sh_pfc_pin_group *grp = &pfc->info->groups[group_selector];
717 for (i = 0; i < grp->nr_pins; i++)
718 sh_pfc_pinconf_set(pmx, grp->pins[i], param, arg);
724 static struct pinctrl_ops sh_pfc_pinctrl_ops = {
725 .get_pins_count = sh_pfc_pinctrl_get_pins_count,
726 .get_pin_name = sh_pfc_pinctrl_get_pin_name,
727 .get_groups_count = sh_pfc_pinctrl_get_groups_count,
728 .get_group_name = sh_pfc_pinctrl_get_group_name,
729 .get_functions_count = sh_pfc_pinctrl_get_functions_count,
730 .get_function_name = sh_pfc_pinctrl_get_function_name,
732 #if CONFIG_IS_ENABLED(PINCONF)
733 .pinconf_num_params = ARRAY_SIZE(sh_pfc_pinconf_params),
734 .pinconf_params = sh_pfc_pinconf_params,
735 .pinconf_set = sh_pfc_pinconf_pin_set,
736 .pinconf_group_set = sh_pfc_pinconf_group_set,
738 .pinmux_set = sh_pfc_pinctrl_pin_set,
739 .pinmux_group_set = sh_pfc_pinctrl_group_set,
740 .set_state = pinctrl_generic_set_state,
743 static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
747 /* Allocate and initialize the pins and configs arrays. */
748 pmx->configs = kzalloc(sizeof(*pmx->configs) * pfc->info->nr_pins,
750 if (unlikely(!pmx->configs))
753 for (i = 0; i < pfc->info->nr_pins; ++i) {
754 struct sh_pfc_pin_config *cfg = &pmx->configs[i];
755 cfg->type = PINMUX_TYPE_NONE;
762 static int sh_pfc_pinctrl_probe(struct udevice *dev)
764 struct sh_pfc_pinctrl_priv *priv = dev_get_priv(dev);
765 enum sh_pfc_model model = dev_get_driver_data(dev);
768 base = devfdt_get_addr(dev);
769 if (base == FDT_ADDR_T_NONE)
772 priv->pfc.regs = devm_ioremap(dev, base, SZ_2K);
776 #ifdef CONFIG_PINCTRL_PFC_R8A7790
777 if (model == SH_PFC_R8A7790)
778 priv->pfc.info = &r8a7790_pinmux_info;
780 #ifdef CONFIG_PINCTRL_PFC_R8A7795
781 if (model == SH_PFC_R8A7795)
782 priv->pfc.info = &r8a7795_pinmux_info;
784 #ifdef CONFIG_PINCTRL_PFC_R8A7796
785 if (model == SH_PFC_R8A7796)
786 priv->pfc.info = &r8a7796_pinmux_info;
788 #ifdef CONFIG_PINCTRL_PFC_R8A77970
789 if (model == SH_PFC_R8A77970)
790 priv->pfc.info = &r8a77970_pinmux_info;
792 #ifdef CONFIG_PINCTRL_PFC_R8A77995
793 if (model == SH_PFC_R8A77995)
794 priv->pfc.info = &r8a77995_pinmux_info;
797 priv->pmx.pfc = &priv->pfc;
798 sh_pfc_init_ranges(&priv->pfc);
799 sh_pfc_map_pins(&priv->pfc, &priv->pmx);
804 static const struct udevice_id sh_pfc_pinctrl_ids[] = {
805 #ifdef CONFIG_PINCTRL_PFC_R8A7790
807 .compatible = "renesas,pfc-r8a7790",
808 .data = SH_PFC_R8A7790,
811 #ifdef CONFIG_PINCTRL_PFC_R8A7795
813 .compatible = "renesas,pfc-r8a7795",
814 .data = SH_PFC_R8A7795,
817 #ifdef CONFIG_PINCTRL_PFC_R8A7796
819 .compatible = "renesas,pfc-r8a7796",
820 .data = SH_PFC_R8A7796,
823 #ifdef CONFIG_PINCTRL_PFC_R8A77970
825 .compatible = "renesas,pfc-r8a77970",
826 .data = SH_PFC_R8A77970,
829 #ifdef CONFIG_PINCTRL_PFC_R8A77995
831 .compatible = "renesas,pfc-r8a77995",
832 .data = SH_PFC_R8A77995,
838 U_BOOT_DRIVER(pinctrl_sh_pfc) = {
839 .name = "sh_pfc_pinctrl",
840 .id = UCLASS_PINCTRL,
841 .of_match = sh_pfc_pinctrl_ids,
842 .priv_auto_alloc_size = sizeof(struct sh_pfc_pinctrl_priv),
843 .ops = &sh_pfc_pinctrl_ops,
844 .probe = sh_pfc_pinctrl_probe,