1 // SPDX-License-Identifier: GPL-2.0
3 * SuperH Pin Function Controller pinmux support.
5 * Copyright (C) 2012 Paul Mundt
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>
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>
26 #include "../pinconf.h"
28 struct sh_pfc_pin_config
{
32 struct sh_pfc_pinctrl
{
33 struct pinctrl_dev
*pctl
;
34 struct pinctrl_desc pctl_desc
;
38 struct pinctrl_pin_desc
*pins
;
39 struct sh_pfc_pin_config
*configs
;
41 const char *func_prop_name
;
42 const char *groups_prop_name
;
43 const char *pins_prop_name
;
46 static int sh_pfc_get_groups_count(struct pinctrl_dev
*pctldev
)
48 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
50 return pmx
->pfc
->info
->nr_groups
;
53 static const char *sh_pfc_get_group_name(struct pinctrl_dev
*pctldev
,
56 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
58 return pmx
->pfc
->info
->groups
[selector
].name
;
61 static int sh_pfc_get_group_pins(struct pinctrl_dev
*pctldev
, unsigned selector
,
62 const unsigned **pins
, unsigned *num_pins
)
64 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
66 *pins
= pmx
->pfc
->info
->groups
[selector
].pins
;
67 *num_pins
= pmx
->pfc
->info
->groups
[selector
].nr_pins
;
72 static void sh_pfc_pin_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
75 seq_puts(s
, DRV_NAME
);
79 static int sh_pfc_map_add_config(struct pinctrl_map
*map
,
80 const char *group_or_pin
,
81 enum pinctrl_map_type type
,
82 unsigned long *configs
,
83 unsigned int num_configs
)
87 cfgs
= kmemdup(configs
, num_configs
* sizeof(*cfgs
),
93 map
->data
.configs
.group_or_pin
= group_or_pin
;
94 map
->data
.configs
.configs
= cfgs
;
95 map
->data
.configs
.num_configs
= num_configs
;
100 static int sh_pfc_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
101 struct device_node
*np
,
102 struct pinctrl_map
**map
,
103 unsigned int *num_maps
, unsigned int *index
)
105 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
106 struct device
*dev
= pmx
->pfc
->dev
;
107 struct pinctrl_map
*maps
= *map
;
108 unsigned int nmaps
= *num_maps
;
109 unsigned int idx
= *index
;
110 unsigned int num_configs
;
111 const char *function
= NULL
;
112 unsigned long *configs
;
113 struct property
*prop
;
114 unsigned int num_groups
;
115 unsigned int num_pins
;
120 /* Support both the old Renesas-specific properties and the new standard
121 * properties. Mixing old and new properties isn't allowed, neither
122 * inside a subnode nor across subnodes.
124 if (!pmx
->func_prop_name
) {
125 if (of_find_property(np
, "groups", NULL
) ||
126 of_find_property(np
, "pins", NULL
)) {
127 pmx
->func_prop_name
= "function";
128 pmx
->groups_prop_name
= "groups";
129 pmx
->pins_prop_name
= "pins";
131 pmx
->func_prop_name
= "renesas,function";
132 pmx
->groups_prop_name
= "renesas,groups";
133 pmx
->pins_prop_name
= "renesas,pins";
137 /* Parse the function and configuration properties. At least a function
138 * or one configuration must be specified.
140 ret
= of_property_read_string(np
, pmx
->func_prop_name
, &function
);
141 if (ret
< 0 && ret
!= -EINVAL
) {
142 dev_err(dev
, "Invalid function in DT\n");
146 ret
= pinconf_generic_parse_dt_config(np
, NULL
, &configs
, &num_configs
);
150 if (!function
&& num_configs
== 0) {
152 "DT node must contain at least a function or config\n");
157 /* Count the number of pins and groups and reallocate mappings. */
158 ret
= of_property_count_strings(np
, pmx
->pins_prop_name
);
159 if (ret
== -EINVAL
) {
161 } else if (ret
< 0) {
162 dev_err(dev
, "Invalid pins list in DT\n");
168 ret
= of_property_count_strings(np
, pmx
->groups_prop_name
);
169 if (ret
== -EINVAL
) {
171 } else if (ret
< 0) {
172 dev_err(dev
, "Invalid pin groups list in DT\n");
178 if (!num_pins
&& !num_groups
) {
179 dev_err(dev
, "No pin or group provided in DT node\n");
187 nmaps
+= num_pins
+ num_groups
;
189 maps
= krealloc(maps
, sizeof(*maps
) * nmaps
, GFP_KERNEL
);
198 /* Iterate over pins and groups and create the mappings. */
199 of_property_for_each_string(np
, pmx
->groups_prop_name
, prop
, group
) {
201 maps
[idx
].type
= PIN_MAP_TYPE_MUX_GROUP
;
202 maps
[idx
].data
.mux
.group
= group
;
203 maps
[idx
].data
.mux
.function
= function
;
208 ret
= sh_pfc_map_add_config(&maps
[idx
], group
,
209 PIN_MAP_TYPE_CONFIGS_GROUP
,
210 configs
, num_configs
);
223 of_property_for_each_string(np
, pmx
->pins_prop_name
, prop
, pin
) {
224 ret
= sh_pfc_map_add_config(&maps
[idx
], pin
,
225 PIN_MAP_TYPE_CONFIGS_PIN
,
226 configs
, num_configs
);
239 static void sh_pfc_dt_free_map(struct pinctrl_dev
*pctldev
,
240 struct pinctrl_map
*map
, unsigned num_maps
)
247 for (i
= 0; i
< num_maps
; ++i
) {
248 if (map
[i
].type
== PIN_MAP_TYPE_CONFIGS_GROUP
||
249 map
[i
].type
== PIN_MAP_TYPE_CONFIGS_PIN
)
250 kfree(map
[i
].data
.configs
.configs
);
256 static int sh_pfc_dt_node_to_map(struct pinctrl_dev
*pctldev
,
257 struct device_node
*np
,
258 struct pinctrl_map
**map
, unsigned *num_maps
)
260 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
261 struct device
*dev
= pmx
->pfc
->dev
;
262 struct device_node
*child
;
270 for_each_child_of_node(np
, child
) {
271 ret
= sh_pfc_dt_subnode_to_map(pctldev
, child
, map
, num_maps
,
279 /* If no mapping has been found in child nodes try the config node. */
280 if (*num_maps
== 0) {
281 ret
= sh_pfc_dt_subnode_to_map(pctldev
, np
, map
, num_maps
,
290 dev_err(dev
, "no mapping found in node %pOF\n", np
);
295 sh_pfc_dt_free_map(pctldev
, *map
, *num_maps
);
299 #endif /* CONFIG_OF */
301 static const struct pinctrl_ops sh_pfc_pinctrl_ops
= {
302 .get_groups_count
= sh_pfc_get_groups_count
,
303 .get_group_name
= sh_pfc_get_group_name
,
304 .get_group_pins
= sh_pfc_get_group_pins
,
305 .pin_dbg_show
= sh_pfc_pin_dbg_show
,
307 .dt_node_to_map
= sh_pfc_dt_node_to_map
,
308 .dt_free_map
= sh_pfc_dt_free_map
,
312 static int sh_pfc_get_functions_count(struct pinctrl_dev
*pctldev
)
314 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
316 return pmx
->pfc
->info
->nr_functions
;
319 static const char *sh_pfc_get_function_name(struct pinctrl_dev
*pctldev
,
322 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
324 return pmx
->pfc
->info
->functions
[selector
].name
;
327 static int sh_pfc_get_function_groups(struct pinctrl_dev
*pctldev
,
329 const char * const **groups
,
330 unsigned * const num_groups
)
332 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
334 *groups
= pmx
->pfc
->info
->functions
[selector
].groups
;
335 *num_groups
= pmx
->pfc
->info
->functions
[selector
].nr_groups
;
340 static int sh_pfc_func_set_mux(struct pinctrl_dev
*pctldev
, unsigned selector
,
343 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
344 struct sh_pfc
*pfc
= pmx
->pfc
;
345 const struct sh_pfc_pin_group
*grp
= &pfc
->info
->groups
[group
];
350 dev_dbg(pctldev
->dev
, "Configuring pin group %s\n", grp
->name
);
352 spin_lock_irqsave(&pfc
->lock
, flags
);
354 for (i
= 0; i
< grp
->nr_pins
; ++i
) {
355 int idx
= sh_pfc_get_pin_index(pfc
, grp
->pins
[i
]);
356 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[idx
];
358 if (cfg
->type
!= PINMUX_TYPE_NONE
) {
364 for (i
= 0; i
< grp
->nr_pins
; ++i
) {
365 ret
= sh_pfc_config_mux(pfc
, grp
->mux
[i
], PINMUX_TYPE_FUNCTION
);
371 spin_unlock_irqrestore(&pfc
->lock
, flags
);
375 static int sh_pfc_gpio_request_enable(struct pinctrl_dev
*pctldev
,
376 struct pinctrl_gpio_range
*range
,
379 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
380 struct sh_pfc
*pfc
= pmx
->pfc
;
381 int idx
= sh_pfc_get_pin_index(pfc
, offset
);
382 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[idx
];
386 spin_lock_irqsave(&pfc
->lock
, flags
);
388 if (cfg
->type
!= PINMUX_TYPE_NONE
) {
390 "Pin %u is busy, can't configure it as GPIO.\n",
397 /* If GPIOs are handled externally the pin mux type need to be
400 const struct sh_pfc_pin
*pin
= &pfc
->info
->pins
[idx
];
402 ret
= sh_pfc_config_mux(pfc
, pin
->enum_id
, PINMUX_TYPE_GPIO
);
407 cfg
->type
= PINMUX_TYPE_GPIO
;
412 spin_unlock_irqrestore(&pfc
->lock
, flags
);
417 static void sh_pfc_gpio_disable_free(struct pinctrl_dev
*pctldev
,
418 struct pinctrl_gpio_range
*range
,
421 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
422 struct sh_pfc
*pfc
= pmx
->pfc
;
423 int idx
= sh_pfc_get_pin_index(pfc
, offset
);
424 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[idx
];
427 spin_lock_irqsave(&pfc
->lock
, flags
);
428 cfg
->type
= PINMUX_TYPE_NONE
;
429 spin_unlock_irqrestore(&pfc
->lock
, flags
);
432 static int sh_pfc_gpio_set_direction(struct pinctrl_dev
*pctldev
,
433 struct pinctrl_gpio_range
*range
,
434 unsigned offset
, bool input
)
436 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
437 struct sh_pfc
*pfc
= pmx
->pfc
;
438 int new_type
= input
? PINMUX_TYPE_INPUT
: PINMUX_TYPE_OUTPUT
;
439 int idx
= sh_pfc_get_pin_index(pfc
, offset
);
440 const struct sh_pfc_pin
*pin
= &pfc
->info
->pins
[idx
];
441 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[idx
];
446 /* Check if the requested direction is supported by the pin. Not all SoC
447 * provide pin config data, so perform the check conditionally.
450 dir
= input
? SH_PFC_PIN_CFG_INPUT
: SH_PFC_PIN_CFG_OUTPUT
;
451 if (!(pin
->configs
& dir
))
455 spin_lock_irqsave(&pfc
->lock
, flags
);
457 ret
= sh_pfc_config_mux(pfc
, pin
->enum_id
, new_type
);
461 cfg
->type
= new_type
;
464 spin_unlock_irqrestore(&pfc
->lock
, flags
);
468 static const struct pinmux_ops sh_pfc_pinmux_ops
= {
469 .get_functions_count
= sh_pfc_get_functions_count
,
470 .get_function_name
= sh_pfc_get_function_name
,
471 .get_function_groups
= sh_pfc_get_function_groups
,
472 .set_mux
= sh_pfc_func_set_mux
,
473 .gpio_request_enable
= sh_pfc_gpio_request_enable
,
474 .gpio_disable_free
= sh_pfc_gpio_disable_free
,
475 .gpio_set_direction
= sh_pfc_gpio_set_direction
,
478 static u32
sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc
*pfc
,
479 unsigned int pin
, unsigned int *offset
, unsigned int *size
)
481 const struct pinmux_drive_reg_field
*field
;
482 const struct pinmux_drive_reg
*reg
;
485 for (reg
= pfc
->info
->drive_regs
; reg
->reg
; ++reg
) {
486 for (i
= 0; i
< ARRAY_SIZE(reg
->fields
); ++i
) {
487 field
= ®
->fields
[i
];
489 if (field
->size
&& field
->pin
== pin
) {
490 *offset
= field
->offset
;
501 static int sh_pfc_pinconf_get_drive_strength(struct sh_pfc
*pfc
,
510 reg
= sh_pfc_pinconf_find_drive_strength_reg(pfc
, pin
, &offset
, &size
);
514 spin_lock_irqsave(&pfc
->lock
, flags
);
515 val
= sh_pfc_read(pfc
, reg
);
516 spin_unlock_irqrestore(&pfc
->lock
, flags
);
518 val
= (val
>> offset
) & GENMASK(size
- 1, 0);
520 /* Convert the value to mA based on a full drive strength value of 24mA.
521 * We can make the full value configurable later if needed.
523 return (val
+ 1) * (size
== 2 ? 6 : 3);
526 static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc
*pfc
,
527 unsigned int pin
, u16 strength
)
536 reg
= sh_pfc_pinconf_find_drive_strength_reg(pfc
, pin
, &offset
, &size
);
540 step
= size
== 2 ? 6 : 3;
542 if (strength
< step
|| strength
> 24)
545 /* Convert the value from mA based on a full drive strength value of
546 * 24mA. We can make the full value configurable later if needed.
548 strength
= strength
/ step
- 1;
550 spin_lock_irqsave(&pfc
->lock
, flags
);
552 val
= sh_pfc_read(pfc
, reg
);
553 val
&= ~GENMASK(offset
+ size
- 1, offset
);
554 val
|= strength
<< offset
;
556 sh_pfc_write(pfc
, reg
, val
);
558 spin_unlock_irqrestore(&pfc
->lock
, flags
);
563 /* Check whether the requested parameter is supported for a pin. */
564 static bool sh_pfc_pinconf_validate(struct sh_pfc
*pfc
, unsigned int _pin
,
565 enum pin_config_param param
)
567 int idx
= sh_pfc_get_pin_index(pfc
, _pin
);
568 const struct sh_pfc_pin
*pin
= &pfc
->info
->pins
[idx
];
571 case PIN_CONFIG_BIAS_DISABLE
:
572 return pin
->configs
& SH_PFC_PIN_CFG_PULL_UP_DOWN
;
574 case PIN_CONFIG_BIAS_PULL_UP
:
575 return pin
->configs
& SH_PFC_PIN_CFG_PULL_UP
;
577 case PIN_CONFIG_BIAS_PULL_DOWN
:
578 return pin
->configs
& SH_PFC_PIN_CFG_PULL_DOWN
;
580 case PIN_CONFIG_DRIVE_STRENGTH
:
581 return pin
->configs
& SH_PFC_PIN_CFG_DRIVE_STRENGTH
;
583 case PIN_CONFIG_POWER_SOURCE
:
584 return pin
->configs
& SH_PFC_PIN_CFG_IO_VOLTAGE
;
591 static int sh_pfc_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned _pin
,
592 unsigned long *config
)
594 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
595 struct sh_pfc
*pfc
= pmx
->pfc
;
596 enum pin_config_param param
= pinconf_to_config_param(*config
);
600 if (!sh_pfc_pinconf_validate(pfc
, _pin
, param
))
604 case PIN_CONFIG_BIAS_DISABLE
:
605 case PIN_CONFIG_BIAS_PULL_UP
:
606 case PIN_CONFIG_BIAS_PULL_DOWN
: {
609 if (!pfc
->info
->ops
|| !pfc
->info
->ops
->get_bias
)
612 spin_lock_irqsave(&pfc
->lock
, flags
);
613 bias
= pfc
->info
->ops
->get_bias(pfc
, _pin
);
614 spin_unlock_irqrestore(&pfc
->lock
, flags
);
623 case PIN_CONFIG_DRIVE_STRENGTH
: {
626 ret
= sh_pfc_pinconf_get_drive_strength(pfc
, _pin
);
634 case PIN_CONFIG_POWER_SOURCE
: {
638 if (!pfc
->info
->ops
|| !pfc
->info
->ops
->pin_to_pocctrl
)
641 bit
= pfc
->info
->ops
->pin_to_pocctrl(pfc
, _pin
, &pocctrl
);
642 if (WARN(bit
< 0, "invalid pin %#x", _pin
))
645 spin_lock_irqsave(&pfc
->lock
, flags
);
646 val
= sh_pfc_read(pfc
, pocctrl
);
647 spin_unlock_irqrestore(&pfc
->lock
, flags
);
649 arg
= (val
& BIT(bit
)) ? 3300 : 1800;
657 *config
= pinconf_to_config_packed(param
, arg
);
661 static int sh_pfc_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned _pin
,
662 unsigned long *configs
, unsigned num_configs
)
664 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
665 struct sh_pfc
*pfc
= pmx
->pfc
;
666 enum pin_config_param param
;
670 for (i
= 0; i
< num_configs
; i
++) {
671 param
= pinconf_to_config_param(configs
[i
]);
673 if (!sh_pfc_pinconf_validate(pfc
, _pin
, param
))
677 case PIN_CONFIG_BIAS_PULL_UP
:
678 case PIN_CONFIG_BIAS_PULL_DOWN
:
679 case PIN_CONFIG_BIAS_DISABLE
:
680 if (!pfc
->info
->ops
|| !pfc
->info
->ops
->set_bias
)
683 spin_lock_irqsave(&pfc
->lock
, flags
);
684 pfc
->info
->ops
->set_bias(pfc
, _pin
, param
);
685 spin_unlock_irqrestore(&pfc
->lock
, flags
);
689 case PIN_CONFIG_DRIVE_STRENGTH
: {
691 pinconf_to_config_argument(configs
[i
]);
694 ret
= sh_pfc_pinconf_set_drive_strength(pfc
, _pin
, arg
);
701 case PIN_CONFIG_POWER_SOURCE
: {
702 unsigned int mV
= pinconf_to_config_argument(configs
[i
]);
706 if (!pfc
->info
->ops
|| !pfc
->info
->ops
->pin_to_pocctrl
)
709 bit
= pfc
->info
->ops
->pin_to_pocctrl(pfc
, _pin
, &pocctrl
);
710 if (WARN(bit
< 0, "invalid pin %#x", _pin
))
713 if (mV
!= 1800 && mV
!= 3300)
716 spin_lock_irqsave(&pfc
->lock
, flags
);
717 val
= sh_pfc_read(pfc
, pocctrl
);
722 sh_pfc_write(pfc
, pocctrl
, val
);
723 spin_unlock_irqrestore(&pfc
->lock
, flags
);
731 } /* for each config */
736 static int sh_pfc_pinconf_group_set(struct pinctrl_dev
*pctldev
, unsigned group
,
737 unsigned long *configs
,
738 unsigned num_configs
)
740 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
741 const unsigned int *pins
;
742 unsigned int num_pins
;
745 pins
= pmx
->pfc
->info
->groups
[group
].pins
;
746 num_pins
= pmx
->pfc
->info
->groups
[group
].nr_pins
;
748 for (i
= 0; i
< num_pins
; ++i
) {
749 ret
= sh_pfc_pinconf_set(pctldev
, pins
[i
], configs
, num_configs
);
757 static const struct pinconf_ops sh_pfc_pinconf_ops
= {
759 .pin_config_get
= sh_pfc_pinconf_get
,
760 .pin_config_set
= sh_pfc_pinconf_set
,
761 .pin_config_group_set
= sh_pfc_pinconf_group_set
,
762 .pin_config_config_dbg_show
= pinconf_generic_dump_config
,
765 /* PFC ranges -> pinctrl pin descs */
766 static int sh_pfc_map_pins(struct sh_pfc
*pfc
, struct sh_pfc_pinctrl
*pmx
)
770 /* Allocate and initialize the pins and configs arrays. */
771 pmx
->pins
= devm_kcalloc(pfc
->dev
,
772 pfc
->info
->nr_pins
, sizeof(*pmx
->pins
),
774 if (unlikely(!pmx
->pins
))
777 pmx
->configs
= devm_kcalloc(pfc
->dev
,
778 pfc
->info
->nr_pins
, sizeof(*pmx
->configs
),
780 if (unlikely(!pmx
->configs
))
783 for (i
= 0; i
< pfc
->info
->nr_pins
; ++i
) {
784 const struct sh_pfc_pin
*info
= &pfc
->info
->pins
[i
];
785 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[i
];
786 struct pinctrl_pin_desc
*pin
= &pmx
->pins
[i
];
788 /* If the pin number is equal to -1 all pins are considered */
789 pin
->number
= info
->pin
!= (u16
)-1 ? info
->pin
: i
;
790 pin
->name
= info
->name
;
791 cfg
->type
= PINMUX_TYPE_NONE
;
797 int sh_pfc_register_pinctrl(struct sh_pfc
*pfc
)
799 struct sh_pfc_pinctrl
*pmx
;
802 pmx
= devm_kzalloc(pfc
->dev
, sizeof(*pmx
), GFP_KERNEL
);
808 ret
= sh_pfc_map_pins(pfc
, pmx
);
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
,
823 dev_err(pfc
->dev
, "could not register: %i\n", ret
);
828 return pinctrl_enable(pmx
->pctl
);