io_uring: ensure finish_wait() is always called in __io_uring_task_cancel()
[linux/fpc-iii.git] / drivers / pinctrl / renesas / pinctrl.c
blobac542d278a387de1a90bfc1bf1f4e291c343ce4e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SuperH Pin Function Controller pinmux support.
5 * Copyright (C) 2012 Paul Mundt
6 */
8 #define DRV_NAME "sh-pfc"
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/pinctrl/consumer.h>
16 #include <linux/pinctrl/machine.h>
17 #include <linux/pinctrl/pinconf.h>
18 #include <linux/pinctrl/pinconf-generic.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/pinctrl/pinmux.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
24 #include "core.h"
25 #include "../core.h"
26 #include "../pinconf.h"
28 struct sh_pfc_pin_config {
29 u16 gpio_enabled:1;
30 u16 mux_mark:15;
33 struct sh_pfc_pinctrl {
34 struct pinctrl_dev *pctl;
35 struct pinctrl_desc pctl_desc;
37 struct sh_pfc *pfc;
39 struct pinctrl_pin_desc *pins;
40 struct sh_pfc_pin_config *configs;
42 const char *func_prop_name;
43 const char *groups_prop_name;
44 const char *pins_prop_name;
47 static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
49 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
51 return pmx->pfc->info->nr_groups;
54 static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
55 unsigned selector)
57 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
59 return pmx->pfc->info->groups[selector].name;
62 static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
63 const unsigned **pins, unsigned *num_pins)
65 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
67 *pins = pmx->pfc->info->groups[selector].pins;
68 *num_pins = pmx->pfc->info->groups[selector].nr_pins;
70 return 0;
73 static void sh_pfc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
74 unsigned offset)
76 seq_puts(s, DRV_NAME);
79 #ifdef CONFIG_OF
80 static int sh_pfc_map_add_config(struct pinctrl_map *map,
81 const char *group_or_pin,
82 enum pinctrl_map_type type,
83 unsigned long *configs,
84 unsigned int num_configs)
86 unsigned long *cfgs;
88 cfgs = kmemdup(configs, num_configs * sizeof(*cfgs),
89 GFP_KERNEL);
90 if (cfgs == NULL)
91 return -ENOMEM;
93 map->type = type;
94 map->data.configs.group_or_pin = group_or_pin;
95 map->data.configs.configs = cfgs;
96 map->data.configs.num_configs = num_configs;
98 return 0;
101 static int sh_pfc_dt_subnode_to_map(struct pinctrl_dev *pctldev,
102 struct device_node *np,
103 struct pinctrl_map **map,
104 unsigned int *num_maps, unsigned int *index)
106 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
107 struct device *dev = pmx->pfc->dev;
108 struct pinctrl_map *maps = *map;
109 unsigned int nmaps = *num_maps;
110 unsigned int idx = *index;
111 unsigned int num_configs;
112 const char *function = NULL;
113 unsigned long *configs;
114 struct property *prop;
115 unsigned int num_groups;
116 unsigned int num_pins;
117 const char *group;
118 const char *pin;
119 int ret;
121 /* Support both the old Renesas-specific properties and the new standard
122 * properties. Mixing old and new properties isn't allowed, neither
123 * inside a subnode nor across subnodes.
125 if (!pmx->func_prop_name) {
126 if (of_find_property(np, "groups", NULL) ||
127 of_find_property(np, "pins", NULL)) {
128 pmx->func_prop_name = "function";
129 pmx->groups_prop_name = "groups";
130 pmx->pins_prop_name = "pins";
131 } else {
132 pmx->func_prop_name = "renesas,function";
133 pmx->groups_prop_name = "renesas,groups";
134 pmx->pins_prop_name = "renesas,pins";
138 /* Parse the function and configuration properties. At least a function
139 * or one configuration must be specified.
141 ret = of_property_read_string(np, pmx->func_prop_name, &function);
142 if (ret < 0 && ret != -EINVAL) {
143 dev_err(dev, "Invalid function in DT\n");
144 return ret;
147 ret = pinconf_generic_parse_dt_config(np, NULL, &configs, &num_configs);
148 if (ret < 0)
149 return ret;
151 if (!function && num_configs == 0) {
152 dev_err(dev,
153 "DT node must contain at least a function or config\n");
154 ret = -ENODEV;
155 goto done;
158 /* Count the number of pins and groups and reallocate mappings. */
159 ret = of_property_count_strings(np, pmx->pins_prop_name);
160 if (ret == -EINVAL) {
161 num_pins = 0;
162 } else if (ret < 0) {
163 dev_err(dev, "Invalid pins list in DT\n");
164 goto done;
165 } else {
166 num_pins = ret;
169 ret = of_property_count_strings(np, pmx->groups_prop_name);
170 if (ret == -EINVAL) {
171 num_groups = 0;
172 } else if (ret < 0) {
173 dev_err(dev, "Invalid pin groups list in DT\n");
174 goto done;
175 } else {
176 num_groups = ret;
179 if (!num_pins && !num_groups) {
180 dev_err(dev, "No pin or group provided in DT node\n");
181 ret = -ENODEV;
182 goto done;
185 if (function)
186 nmaps += num_groups;
187 if (configs)
188 nmaps += num_pins + num_groups;
190 maps = krealloc(maps, sizeof(*maps) * nmaps, GFP_KERNEL);
191 if (maps == NULL) {
192 ret = -ENOMEM;
193 goto done;
196 *map = maps;
197 *num_maps = nmaps;
199 /* Iterate over pins and groups and create the mappings. */
200 of_property_for_each_string(np, pmx->groups_prop_name, prop, group) {
201 if (function) {
202 maps[idx].type = PIN_MAP_TYPE_MUX_GROUP;
203 maps[idx].data.mux.group = group;
204 maps[idx].data.mux.function = function;
205 idx++;
208 if (configs) {
209 ret = sh_pfc_map_add_config(&maps[idx], group,
210 PIN_MAP_TYPE_CONFIGS_GROUP,
211 configs, num_configs);
212 if (ret < 0)
213 goto done;
215 idx++;
219 if (!configs) {
220 ret = 0;
221 goto done;
224 of_property_for_each_string(np, pmx->pins_prop_name, prop, pin) {
225 ret = sh_pfc_map_add_config(&maps[idx], pin,
226 PIN_MAP_TYPE_CONFIGS_PIN,
227 configs, num_configs);
228 if (ret < 0)
229 goto done;
231 idx++;
234 done:
235 *index = idx;
236 kfree(configs);
237 return ret;
240 static void sh_pfc_dt_free_map(struct pinctrl_dev *pctldev,
241 struct pinctrl_map *map, unsigned num_maps)
243 unsigned int i;
245 if (map == NULL)
246 return;
248 for (i = 0; i < num_maps; ++i) {
249 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP ||
250 map[i].type == PIN_MAP_TYPE_CONFIGS_PIN)
251 kfree(map[i].data.configs.configs);
254 kfree(map);
257 static int sh_pfc_dt_node_to_map(struct pinctrl_dev *pctldev,
258 struct device_node *np,
259 struct pinctrl_map **map, unsigned *num_maps)
261 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
262 struct device *dev = pmx->pfc->dev;
263 struct device_node *child;
264 unsigned int index;
265 int ret;
267 *map = NULL;
268 *num_maps = 0;
269 index = 0;
271 for_each_child_of_node(np, child) {
272 ret = sh_pfc_dt_subnode_to_map(pctldev, child, map, num_maps,
273 &index);
274 if (ret < 0) {
275 of_node_put(child);
276 goto done;
280 /* If no mapping has been found in child nodes try the config node. */
281 if (*num_maps == 0) {
282 ret = sh_pfc_dt_subnode_to_map(pctldev, np, map, num_maps,
283 &index);
284 if (ret < 0)
285 goto done;
288 if (*num_maps)
289 return 0;
291 dev_err(dev, "no mapping found in node %pOF\n", np);
292 ret = -EINVAL;
294 done:
295 if (ret < 0)
296 sh_pfc_dt_free_map(pctldev, *map, *num_maps);
298 return ret;
300 #endif /* CONFIG_OF */
302 static const struct pinctrl_ops sh_pfc_pinctrl_ops = {
303 .get_groups_count = sh_pfc_get_groups_count,
304 .get_group_name = sh_pfc_get_group_name,
305 .get_group_pins = sh_pfc_get_group_pins,
306 .pin_dbg_show = sh_pfc_pin_dbg_show,
307 #ifdef CONFIG_OF
308 .dt_node_to_map = sh_pfc_dt_node_to_map,
309 .dt_free_map = sh_pfc_dt_free_map,
310 #endif
313 static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
315 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
317 return pmx->pfc->info->nr_functions;
320 static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
321 unsigned selector)
323 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
325 return pmx->pfc->info->functions[selector].name;
328 static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev,
329 unsigned selector,
330 const char * const **groups,
331 unsigned * const num_groups)
333 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
335 *groups = pmx->pfc->info->functions[selector].groups;
336 *num_groups = pmx->pfc->info->functions[selector].nr_groups;
338 return 0;
341 static int sh_pfc_func_set_mux(struct pinctrl_dev *pctldev, unsigned selector,
342 unsigned group)
344 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
345 struct sh_pfc *pfc = pmx->pfc;
346 const struct sh_pfc_pin_group *grp = &pfc->info->groups[group];
347 unsigned long flags;
348 unsigned int i;
349 int ret = 0;
351 dev_dbg(pctldev->dev, "Configuring pin group %s\n", grp->name);
353 spin_lock_irqsave(&pfc->lock, flags);
355 for (i = 0; i < grp->nr_pins; ++i) {
356 int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
357 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
360 * This driver cannot manage both gpio and mux when the gpio
361 * pin is already enabled. So, this function fails.
363 if (cfg->gpio_enabled) {
364 ret = -EBUSY;
365 goto done;
368 ret = sh_pfc_config_mux(pfc, grp->mux[i], PINMUX_TYPE_FUNCTION);
369 if (ret < 0)
370 goto done;
373 /* All group pins are configured, mark the pins as muxed */
374 for (i = 0; i < grp->nr_pins; ++i) {
375 int idx = sh_pfc_get_pin_index(pfc, grp->pins[i]);
376 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
378 cfg->mux_mark = grp->mux[i];
381 done:
382 spin_unlock_irqrestore(&pfc->lock, flags);
383 return ret;
386 static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
387 struct pinctrl_gpio_range *range,
388 unsigned offset)
390 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
391 struct sh_pfc *pfc = pmx->pfc;
392 int idx = sh_pfc_get_pin_index(pfc, offset);
393 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
394 unsigned long flags;
395 int ret;
397 spin_lock_irqsave(&pfc->lock, flags);
399 if (!pfc->gpio) {
400 /* If GPIOs are handled externally the pin mux type needs to be
401 * set to GPIO here.
403 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
405 ret = sh_pfc_config_mux(pfc, pin->enum_id, PINMUX_TYPE_GPIO);
406 if (ret < 0)
407 goto done;
410 cfg->gpio_enabled = true;
412 ret = 0;
414 done:
415 spin_unlock_irqrestore(&pfc->lock, flags);
417 return ret;
420 static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
421 struct pinctrl_gpio_range *range,
422 unsigned offset)
424 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
425 struct sh_pfc *pfc = pmx->pfc;
426 int idx = sh_pfc_get_pin_index(pfc, offset);
427 struct sh_pfc_pin_config *cfg = &pmx->configs[idx];
428 unsigned long flags;
430 spin_lock_irqsave(&pfc->lock, flags);
431 cfg->gpio_enabled = false;
432 /* If mux is already set, this configures it here */
433 if (cfg->mux_mark)
434 sh_pfc_config_mux(pfc, cfg->mux_mark, PINMUX_TYPE_FUNCTION);
435 spin_unlock_irqrestore(&pfc->lock, flags);
438 #ifdef CONFIG_PINCTRL_SH_PFC_GPIO
439 static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
440 struct pinctrl_gpio_range *range,
441 unsigned offset, bool input)
443 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
444 struct sh_pfc *pfc = pmx->pfc;
445 int new_type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
446 int idx = sh_pfc_get_pin_index(pfc, offset);
447 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
448 unsigned long flags;
449 unsigned int dir;
450 int ret;
452 /* Check if the requested direction is supported by the pin. Not all
453 * SoCs provide pin config data, so perform the check conditionally.
455 if (pin->configs) {
456 dir = input ? SH_PFC_PIN_CFG_INPUT : SH_PFC_PIN_CFG_OUTPUT;
457 if (!(pin->configs & dir))
458 return -EINVAL;
461 spin_lock_irqsave(&pfc->lock, flags);
462 ret = sh_pfc_config_mux(pfc, pin->enum_id, new_type);
463 spin_unlock_irqrestore(&pfc->lock, flags);
464 return ret;
466 #else
467 #define sh_pfc_gpio_set_direction NULL
468 #endif
470 static const struct pinmux_ops sh_pfc_pinmux_ops = {
471 .get_functions_count = sh_pfc_get_functions_count,
472 .get_function_name = sh_pfc_get_function_name,
473 .get_function_groups = sh_pfc_get_function_groups,
474 .set_mux = sh_pfc_func_set_mux,
475 .gpio_request_enable = sh_pfc_gpio_request_enable,
476 .gpio_disable_free = sh_pfc_gpio_disable_free,
477 .gpio_set_direction = sh_pfc_gpio_set_direction,
480 static u32 sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc *pfc,
481 unsigned int pin, unsigned int *offset, unsigned int *size)
483 const struct pinmux_drive_reg_field *field;
484 const struct pinmux_drive_reg *reg;
485 unsigned int i;
487 for (reg = pfc->info->drive_regs; reg->reg; ++reg) {
488 for (i = 0; i < ARRAY_SIZE(reg->fields); ++i) {
489 field = &reg->fields[i];
491 if (field->size && field->pin == pin) {
492 *offset = field->offset;
493 *size = field->size;
495 return reg->reg;
500 return 0;
503 static int sh_pfc_pinconf_get_drive_strength(struct sh_pfc *pfc,
504 unsigned int pin)
506 unsigned long flags;
507 unsigned int offset;
508 unsigned int size;
509 u32 reg;
510 u32 val;
512 reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
513 if (!reg)
514 return -EINVAL;
516 spin_lock_irqsave(&pfc->lock, flags);
517 val = sh_pfc_read(pfc, reg);
518 spin_unlock_irqrestore(&pfc->lock, flags);
520 val = (val >> offset) & GENMASK(size - 1, 0);
522 /* Convert the value to mA based on a full drive strength value of 24mA.
523 * We can make the full value configurable later if needed.
525 return (val + 1) * (size == 2 ? 6 : 3);
528 static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc *pfc,
529 unsigned int pin, u16 strength)
531 unsigned long flags;
532 unsigned int offset;
533 unsigned int size;
534 unsigned int step;
535 u32 reg;
536 u32 val;
538 reg = sh_pfc_pinconf_find_drive_strength_reg(pfc, pin, &offset, &size);
539 if (!reg)
540 return -EINVAL;
542 step = size == 2 ? 6 : 3;
544 if (strength < step || strength > 24)
545 return -EINVAL;
547 /* Convert the value from mA based on a full drive strength value of
548 * 24mA. We can make the full value configurable later if needed.
550 strength = strength / step - 1;
552 spin_lock_irqsave(&pfc->lock, flags);
554 val = sh_pfc_read(pfc, reg);
555 val &= ~GENMASK(offset + size - 1, offset);
556 val |= strength << offset;
558 sh_pfc_write(pfc, reg, val);
560 spin_unlock_irqrestore(&pfc->lock, flags);
562 return 0;
565 /* Check whether the requested parameter is supported for a pin. */
566 static bool sh_pfc_pinconf_validate(struct sh_pfc *pfc, unsigned int _pin,
567 enum pin_config_param param)
569 int idx = sh_pfc_get_pin_index(pfc, _pin);
570 const struct sh_pfc_pin *pin = &pfc->info->pins[idx];
572 switch (param) {
573 case PIN_CONFIG_BIAS_DISABLE:
574 return pin->configs & SH_PFC_PIN_CFG_PULL_UP_DOWN;
576 case PIN_CONFIG_BIAS_PULL_UP:
577 return pin->configs & SH_PFC_PIN_CFG_PULL_UP;
579 case PIN_CONFIG_BIAS_PULL_DOWN:
580 return pin->configs & SH_PFC_PIN_CFG_PULL_DOWN;
582 case PIN_CONFIG_DRIVE_STRENGTH:
583 return pin->configs & SH_PFC_PIN_CFG_DRIVE_STRENGTH;
585 case PIN_CONFIG_POWER_SOURCE:
586 return pin->configs & SH_PFC_PIN_CFG_IO_VOLTAGE;
588 default:
589 return false;
593 static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned _pin,
594 unsigned long *config)
596 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
597 struct sh_pfc *pfc = pmx->pfc;
598 enum pin_config_param param = pinconf_to_config_param(*config);
599 unsigned long flags;
600 unsigned int arg;
602 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
603 return -ENOTSUPP;
605 switch (param) {
606 case PIN_CONFIG_BIAS_DISABLE:
607 case PIN_CONFIG_BIAS_PULL_UP:
608 case PIN_CONFIG_BIAS_PULL_DOWN: {
609 unsigned int bias;
611 if (!pfc->info->ops || !pfc->info->ops->get_bias)
612 return -ENOTSUPP;
614 spin_lock_irqsave(&pfc->lock, flags);
615 bias = pfc->info->ops->get_bias(pfc, _pin);
616 spin_unlock_irqrestore(&pfc->lock, flags);
618 if (bias != param)
619 return -EINVAL;
621 arg = 0;
622 break;
625 case PIN_CONFIG_DRIVE_STRENGTH: {
626 int ret;
628 ret = sh_pfc_pinconf_get_drive_strength(pfc, _pin);
629 if (ret < 0)
630 return ret;
632 arg = ret;
633 break;
636 case PIN_CONFIG_POWER_SOURCE: {
637 u32 pocctrl, val;
638 int bit;
640 if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
641 return -ENOTSUPP;
643 bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &pocctrl);
644 if (WARN(bit < 0, "invalid pin %#x", _pin))
645 return bit;
647 spin_lock_irqsave(&pfc->lock, flags);
648 val = sh_pfc_read(pfc, pocctrl);
649 spin_unlock_irqrestore(&pfc->lock, flags);
651 arg = (val & BIT(bit)) ? 3300 : 1800;
652 break;
655 default:
656 return -ENOTSUPP;
659 *config = pinconf_to_config_packed(param, arg);
660 return 0;
663 static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned _pin,
664 unsigned long *configs, unsigned num_configs)
666 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
667 struct sh_pfc *pfc = pmx->pfc;
668 enum pin_config_param param;
669 unsigned long flags;
670 unsigned int i;
672 for (i = 0; i < num_configs; i++) {
673 param = pinconf_to_config_param(configs[i]);
675 if (!sh_pfc_pinconf_validate(pfc, _pin, param))
676 return -ENOTSUPP;
678 switch (param) {
679 case PIN_CONFIG_BIAS_PULL_UP:
680 case PIN_CONFIG_BIAS_PULL_DOWN:
681 case PIN_CONFIG_BIAS_DISABLE:
682 if (!pfc->info->ops || !pfc->info->ops->set_bias)
683 return -ENOTSUPP;
685 spin_lock_irqsave(&pfc->lock, flags);
686 pfc->info->ops->set_bias(pfc, _pin, param);
687 spin_unlock_irqrestore(&pfc->lock, flags);
689 break;
691 case PIN_CONFIG_DRIVE_STRENGTH: {
692 unsigned int arg =
693 pinconf_to_config_argument(configs[i]);
694 int ret;
696 ret = sh_pfc_pinconf_set_drive_strength(pfc, _pin, arg);
697 if (ret < 0)
698 return ret;
700 break;
703 case PIN_CONFIG_POWER_SOURCE: {
704 unsigned int mV = pinconf_to_config_argument(configs[i]);
705 u32 pocctrl, val;
706 int bit;
708 if (!pfc->info->ops || !pfc->info->ops->pin_to_pocctrl)
709 return -ENOTSUPP;
711 bit = pfc->info->ops->pin_to_pocctrl(pfc, _pin, &pocctrl);
712 if (WARN(bit < 0, "invalid pin %#x", _pin))
713 return bit;
715 if (mV != 1800 && mV != 3300)
716 return -EINVAL;
718 spin_lock_irqsave(&pfc->lock, flags);
719 val = sh_pfc_read(pfc, pocctrl);
720 if (mV == 3300)
721 val |= BIT(bit);
722 else
723 val &= ~BIT(bit);
724 sh_pfc_write(pfc, pocctrl, val);
725 spin_unlock_irqrestore(&pfc->lock, flags);
727 break;
730 default:
731 return -ENOTSUPP;
733 } /* for each config */
735 return 0;
738 static int sh_pfc_pinconf_group_set(struct pinctrl_dev *pctldev, unsigned group,
739 unsigned long *configs,
740 unsigned num_configs)
742 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
743 const unsigned int *pins;
744 unsigned int num_pins;
745 unsigned int i, ret;
747 pins = pmx->pfc->info->groups[group].pins;
748 num_pins = pmx->pfc->info->groups[group].nr_pins;
750 for (i = 0; i < num_pins; ++i) {
751 ret = sh_pfc_pinconf_set(pctldev, pins[i], configs, num_configs);
752 if (ret)
753 return ret;
756 return 0;
759 static const struct pinconf_ops sh_pfc_pinconf_ops = {
760 .is_generic = true,
761 .pin_config_get = sh_pfc_pinconf_get,
762 .pin_config_set = sh_pfc_pinconf_set,
763 .pin_config_group_set = sh_pfc_pinconf_group_set,
764 .pin_config_config_dbg_show = pinconf_generic_dump_config,
767 /* PFC ranges -> pinctrl pin descs */
768 static int sh_pfc_map_pins(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
770 unsigned int i;
772 /* Allocate and initialize the pins and configs arrays. */
773 pmx->pins = devm_kcalloc(pfc->dev,
774 pfc->info->nr_pins, sizeof(*pmx->pins),
775 GFP_KERNEL);
776 if (unlikely(!pmx->pins))
777 return -ENOMEM;
779 pmx->configs = devm_kcalloc(pfc->dev,
780 pfc->info->nr_pins, sizeof(*pmx->configs),
781 GFP_KERNEL);
782 if (unlikely(!pmx->configs))
783 return -ENOMEM;
785 for (i = 0; i < pfc->info->nr_pins; ++i) {
786 const struct sh_pfc_pin *info = &pfc->info->pins[i];
787 struct pinctrl_pin_desc *pin = &pmx->pins[i];
789 /* If the pin number is equal to -1 all pins are considered */
790 pin->number = info->pin != (u16)-1 ? info->pin : i;
791 pin->name = info->name;
794 return 0;
797 int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
799 struct sh_pfc_pinctrl *pmx;
800 int ret;
802 pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
803 if (unlikely(!pmx))
804 return -ENOMEM;
806 pmx->pfc = pfc;
808 ret = sh_pfc_map_pins(pfc, pmx);
809 if (ret < 0)
810 return ret;
812 pmx->pctl_desc.name = DRV_NAME;
813 pmx->pctl_desc.owner = THIS_MODULE;
814 pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
815 pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
816 pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
817 pmx->pctl_desc.pins = pmx->pins;
818 pmx->pctl_desc.npins = pfc->info->nr_pins;
820 ret = devm_pinctrl_register_and_init(pfc->dev, &pmx->pctl_desc, pmx,
821 &pmx->pctl);
822 if (ret) {
823 dev_err(pfc->dev, "could not register: %i\n", ret);
825 return ret;
828 return pinctrl_enable(pmx->pctl);
831 unsigned int rcar_pinmux_get_bias(struct sh_pfc *pfc, unsigned int pin)
833 const struct pinmux_bias_reg *reg;
834 unsigned int bit;
836 reg = sh_pfc_pin_to_bias_reg(pfc, pin, &bit);
837 if (!reg)
838 return PIN_CONFIG_BIAS_DISABLE;
840 if (!(sh_pfc_read(pfc, reg->puen) & BIT(bit)))
841 return PIN_CONFIG_BIAS_DISABLE;
842 else if (!reg->pud || (sh_pfc_read(pfc, reg->pud) & BIT(bit)))
843 return PIN_CONFIG_BIAS_PULL_UP;
844 else
845 return PIN_CONFIG_BIAS_PULL_DOWN;
848 void rcar_pinmux_set_bias(struct sh_pfc *pfc, unsigned int pin,
849 unsigned int bias)
851 const struct pinmux_bias_reg *reg;
852 u32 enable, updown;
853 unsigned int bit;
855 reg = sh_pfc_pin_to_bias_reg(pfc, pin, &bit);
856 if (!reg)
857 return;
859 enable = sh_pfc_read(pfc, reg->puen) & ~BIT(bit);
860 if (bias != PIN_CONFIG_BIAS_DISABLE)
861 enable |= BIT(bit);
863 if (reg->pud) {
864 updown = sh_pfc_read(pfc, reg->pud) & ~BIT(bit);
865 if (bias == PIN_CONFIG_BIAS_PULL_UP)
866 updown |= BIT(bit);
868 sh_pfc_write(pfc, reg->pud, updown);
871 sh_pfc_write(pfc, reg->puen, enable);