2 * SuperH Pin Function Controller pinmux support.
4 * Copyright (C) 2012 Paul Mundt
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
11 #define DRV_NAME "sh-pfc"
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/pinctrl/machine.h>
20 #include <linux/pinctrl/pinconf.h>
21 #include <linux/pinctrl/pinconf-generic.h>
22 #include <linux/pinctrl/pinctrl.h>
23 #include <linux/pinctrl/pinmux.h>
24 #include <linux/slab.h>
25 #include <linux/spinlock.h>
29 #include "../pinconf.h"
31 struct sh_pfc_pin_config
{
35 struct sh_pfc_pinctrl
{
36 struct pinctrl_dev
*pctl
;
37 struct pinctrl_desc pctl_desc
;
41 struct pinctrl_pin_desc
*pins
;
42 struct sh_pfc_pin_config
*configs
;
44 const char *func_prop_name
;
45 const char *groups_prop_name
;
46 const char *pins_prop_name
;
49 static int sh_pfc_get_groups_count(struct pinctrl_dev
*pctldev
)
51 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
53 return pmx
->pfc
->info
->nr_groups
;
56 static const char *sh_pfc_get_group_name(struct pinctrl_dev
*pctldev
,
59 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
61 return pmx
->pfc
->info
->groups
[selector
].name
;
64 static int sh_pfc_get_group_pins(struct pinctrl_dev
*pctldev
, unsigned selector
,
65 const unsigned **pins
, unsigned *num_pins
)
67 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
69 *pins
= pmx
->pfc
->info
->groups
[selector
].pins
;
70 *num_pins
= pmx
->pfc
->info
->groups
[selector
].nr_pins
;
75 static void sh_pfc_pin_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
78 seq_printf(s
, "%s", DRV_NAME
);
82 static int sh_pfc_map_add_config(struct pinctrl_map
*map
,
83 const char *group_or_pin
,
84 enum pinctrl_map_type type
,
85 unsigned long *configs
,
86 unsigned int num_configs
)
90 cfgs
= kmemdup(configs
, num_configs
* sizeof(*cfgs
),
96 map
->data
.configs
.group_or_pin
= group_or_pin
;
97 map
->data
.configs
.configs
= cfgs
;
98 map
->data
.configs
.num_configs
= num_configs
;
103 static int sh_pfc_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
104 struct device_node
*np
,
105 struct pinctrl_map
**map
,
106 unsigned int *num_maps
, unsigned int *index
)
108 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
109 struct device
*dev
= pmx
->pfc
->dev
;
110 struct pinctrl_map
*maps
= *map
;
111 unsigned int nmaps
= *num_maps
;
112 unsigned int idx
= *index
;
113 unsigned int num_configs
;
114 const char *function
= NULL
;
115 unsigned long *configs
;
116 struct property
*prop
;
117 unsigned int num_groups
;
118 unsigned int num_pins
;
123 /* Support both the old Renesas-specific properties and the new standard
124 * properties. Mixing old and new properties isn't allowed, neither
125 * inside a subnode nor across subnodes.
127 if (!pmx
->func_prop_name
) {
128 if (of_find_property(np
, "groups", NULL
) ||
129 of_find_property(np
, "pins", NULL
)) {
130 pmx
->func_prop_name
= "function";
131 pmx
->groups_prop_name
= "groups";
132 pmx
->pins_prop_name
= "pins";
134 pmx
->func_prop_name
= "renesas,function";
135 pmx
->groups_prop_name
= "renesas,groups";
136 pmx
->pins_prop_name
= "renesas,pins";
140 /* Parse the function and configuration properties. At least a function
141 * or one configuration must be specified.
143 ret
= of_property_read_string(np
, pmx
->func_prop_name
, &function
);
144 if (ret
< 0 && ret
!= -EINVAL
) {
145 dev_err(dev
, "Invalid function in DT\n");
149 ret
= pinconf_generic_parse_dt_config(np
, NULL
, &configs
, &num_configs
);
153 if (!function
&& num_configs
== 0) {
155 "DT node must contain at least a function or config\n");
160 /* Count the number of pins and groups and reallocate mappings. */
161 ret
= of_property_count_strings(np
, pmx
->pins_prop_name
);
162 if (ret
== -EINVAL
) {
164 } else if (ret
< 0) {
165 dev_err(dev
, "Invalid pins list in DT\n");
171 ret
= of_property_count_strings(np
, pmx
->groups_prop_name
);
172 if (ret
== -EINVAL
) {
174 } else if (ret
< 0) {
175 dev_err(dev
, "Invalid pin groups list in DT\n");
181 if (!num_pins
&& !num_groups
) {
182 dev_err(dev
, "No pin or group provided in DT node\n");
190 nmaps
+= num_pins
+ num_groups
;
192 maps
= krealloc(maps
, sizeof(*maps
) * nmaps
, GFP_KERNEL
);
201 /* Iterate over pins and groups and create the mappings. */
202 of_property_for_each_string(np
, pmx
->groups_prop_name
, prop
, group
) {
204 maps
[idx
].type
= PIN_MAP_TYPE_MUX_GROUP
;
205 maps
[idx
].data
.mux
.group
= group
;
206 maps
[idx
].data
.mux
.function
= function
;
211 ret
= sh_pfc_map_add_config(&maps
[idx
], group
,
212 PIN_MAP_TYPE_CONFIGS_GROUP
,
213 configs
, num_configs
);
226 of_property_for_each_string(np
, pmx
->pins_prop_name
, prop
, pin
) {
227 ret
= sh_pfc_map_add_config(&maps
[idx
], pin
,
228 PIN_MAP_TYPE_CONFIGS_PIN
,
229 configs
, num_configs
);
242 static void sh_pfc_dt_free_map(struct pinctrl_dev
*pctldev
,
243 struct pinctrl_map
*map
, unsigned num_maps
)
250 for (i
= 0; i
< num_maps
; ++i
) {
251 if (map
[i
].type
== PIN_MAP_TYPE_CONFIGS_GROUP
||
252 map
[i
].type
== PIN_MAP_TYPE_CONFIGS_PIN
)
253 kfree(map
[i
].data
.configs
.configs
);
259 static int sh_pfc_dt_node_to_map(struct pinctrl_dev
*pctldev
,
260 struct device_node
*np
,
261 struct pinctrl_map
**map
, unsigned *num_maps
)
263 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
264 struct device
*dev
= pmx
->pfc
->dev
;
265 struct device_node
*child
;
273 for_each_child_of_node(np
, child
) {
274 ret
= sh_pfc_dt_subnode_to_map(pctldev
, child
, map
, num_maps
,
282 /* If no mapping has been found in child nodes try the config node. */
283 if (*num_maps
== 0) {
284 ret
= sh_pfc_dt_subnode_to_map(pctldev
, np
, map
, num_maps
,
293 dev_err(dev
, "no mapping found in node %pOF\n", np
);
298 sh_pfc_dt_free_map(pctldev
, *map
, *num_maps
);
302 #endif /* CONFIG_OF */
304 static const struct pinctrl_ops sh_pfc_pinctrl_ops
= {
305 .get_groups_count
= sh_pfc_get_groups_count
,
306 .get_group_name
= sh_pfc_get_group_name
,
307 .get_group_pins
= sh_pfc_get_group_pins
,
308 .pin_dbg_show
= sh_pfc_pin_dbg_show
,
310 .dt_node_to_map
= sh_pfc_dt_node_to_map
,
311 .dt_free_map
= sh_pfc_dt_free_map
,
315 static int sh_pfc_get_functions_count(struct pinctrl_dev
*pctldev
)
317 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
319 return pmx
->pfc
->info
->nr_functions
;
322 static const char *sh_pfc_get_function_name(struct pinctrl_dev
*pctldev
,
325 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
327 return pmx
->pfc
->info
->functions
[selector
].name
;
330 static int sh_pfc_get_function_groups(struct pinctrl_dev
*pctldev
,
332 const char * const **groups
,
333 unsigned * const num_groups
)
335 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
337 *groups
= pmx
->pfc
->info
->functions
[selector
].groups
;
338 *num_groups
= pmx
->pfc
->info
->functions
[selector
].nr_groups
;
343 static int sh_pfc_func_set_mux(struct pinctrl_dev
*pctldev
, unsigned selector
,
346 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
347 struct sh_pfc
*pfc
= pmx
->pfc
;
348 const struct sh_pfc_pin_group
*grp
= &pfc
->info
->groups
[group
];
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
];
359 if (cfg
->type
!= PINMUX_TYPE_NONE
) {
365 for (i
= 0; i
< grp
->nr_pins
; ++i
) {
366 ret
= sh_pfc_config_mux(pfc
, grp
->mux
[i
], PINMUX_TYPE_FUNCTION
);
372 spin_unlock_irqrestore(&pfc
->lock
, flags
);
376 static int sh_pfc_gpio_request_enable(struct pinctrl_dev
*pctldev
,
377 struct pinctrl_gpio_range
*range
,
380 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
381 struct sh_pfc
*pfc
= pmx
->pfc
;
382 int idx
= sh_pfc_get_pin_index(pfc
, offset
);
383 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[idx
];
387 spin_lock_irqsave(&pfc
->lock
, flags
);
389 if (cfg
->type
!= PINMUX_TYPE_NONE
) {
391 "Pin %u is busy, can't configure it as GPIO.\n",
398 /* If GPIOs are handled externally the pin mux type need to be
401 const struct sh_pfc_pin
*pin
= &pfc
->info
->pins
[idx
];
403 ret
= sh_pfc_config_mux(pfc
, pin
->enum_id
, PINMUX_TYPE_GPIO
);
408 cfg
->type
= PINMUX_TYPE_GPIO
;
413 spin_unlock_irqrestore(&pfc
->lock
, flags
);
418 static void sh_pfc_gpio_disable_free(struct pinctrl_dev
*pctldev
,
419 struct pinctrl_gpio_range
*range
,
422 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
423 struct sh_pfc
*pfc
= pmx
->pfc
;
424 int idx
= sh_pfc_get_pin_index(pfc
, offset
);
425 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[idx
];
428 spin_lock_irqsave(&pfc
->lock
, flags
);
429 cfg
->type
= PINMUX_TYPE_NONE
;
430 spin_unlock_irqrestore(&pfc
->lock
, flags
);
433 static int sh_pfc_gpio_set_direction(struct pinctrl_dev
*pctldev
,
434 struct pinctrl_gpio_range
*range
,
435 unsigned offset
, bool input
)
437 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
438 struct sh_pfc
*pfc
= pmx
->pfc
;
439 int new_type
= input
? PINMUX_TYPE_INPUT
: PINMUX_TYPE_OUTPUT
;
440 int idx
= sh_pfc_get_pin_index(pfc
, offset
);
441 const struct sh_pfc_pin
*pin
= &pfc
->info
->pins
[idx
];
442 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[idx
];
447 /* Check if the requested direction is supported by the pin. Not all SoC
448 * provide pin config data, so perform the check conditionally.
451 dir
= input
? SH_PFC_PIN_CFG_INPUT
: SH_PFC_PIN_CFG_OUTPUT
;
452 if (!(pin
->configs
& dir
))
456 spin_lock_irqsave(&pfc
->lock
, flags
);
458 ret
= sh_pfc_config_mux(pfc
, pin
->enum_id
, new_type
);
462 cfg
->type
= new_type
;
465 spin_unlock_irqrestore(&pfc
->lock
, flags
);
469 static const struct pinmux_ops sh_pfc_pinmux_ops
= {
470 .get_functions_count
= sh_pfc_get_functions_count
,
471 .get_function_name
= sh_pfc_get_function_name
,
472 .get_function_groups
= sh_pfc_get_function_groups
,
473 .set_mux
= sh_pfc_func_set_mux
,
474 .gpio_request_enable
= sh_pfc_gpio_request_enable
,
475 .gpio_disable_free
= sh_pfc_gpio_disable_free
,
476 .gpio_set_direction
= sh_pfc_gpio_set_direction
,
479 static u32
sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc
*pfc
,
480 unsigned int pin
, unsigned int *offset
, unsigned int *size
)
482 const struct pinmux_drive_reg_field
*field
;
483 const struct pinmux_drive_reg
*reg
;
486 for (reg
= pfc
->info
->drive_regs
; reg
->reg
; ++reg
) {
487 for (i
= 0; i
< ARRAY_SIZE(reg
->fields
); ++i
) {
488 field
= ®
->fields
[i
];
490 if (field
->size
&& field
->pin
== pin
) {
491 *offset
= field
->offset
;
502 static int sh_pfc_pinconf_get_drive_strength(struct sh_pfc
*pfc
,
511 reg
= sh_pfc_pinconf_find_drive_strength_reg(pfc
, pin
, &offset
, &size
);
515 spin_lock_irqsave(&pfc
->lock
, flags
);
516 val
= sh_pfc_read(pfc
, reg
);
517 spin_unlock_irqrestore(&pfc
->lock
, flags
);
519 val
= (val
>> offset
) & GENMASK(size
- 1, 0);
521 /* Convert the value to mA based on a full drive strength value of 24mA.
522 * We can make the full value configurable later if needed.
524 return (val
+ 1) * (size
== 2 ? 6 : 3);
527 static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc
*pfc
,
528 unsigned int pin
, u16 strength
)
537 reg
= sh_pfc_pinconf_find_drive_strength_reg(pfc
, pin
, &offset
, &size
);
541 step
= size
== 2 ? 6 : 3;
543 if (strength
< step
|| strength
> 24)
546 /* Convert the value from mA based on a full drive strength value of
547 * 24mA. We can make the full value configurable later if needed.
549 strength
= strength
/ step
- 1;
551 spin_lock_irqsave(&pfc
->lock
, flags
);
553 val
= sh_pfc_read(pfc
, reg
);
554 val
&= ~GENMASK(offset
+ size
- 1, offset
);
555 val
|= strength
<< offset
;
557 sh_pfc_write(pfc
, reg
, val
);
559 spin_unlock_irqrestore(&pfc
->lock
, flags
);
564 /* Check whether the requested parameter is supported for a pin. */
565 static bool sh_pfc_pinconf_validate(struct sh_pfc
*pfc
, unsigned int _pin
,
566 enum pin_config_param param
)
568 int idx
= sh_pfc_get_pin_index(pfc
, _pin
);
569 const struct sh_pfc_pin
*pin
= &pfc
->info
->pins
[idx
];
572 case PIN_CONFIG_BIAS_DISABLE
:
573 return pin
->configs
&
574 (SH_PFC_PIN_CFG_PULL_UP
| SH_PFC_PIN_CFG_PULL_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
;
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
);
602 if (!sh_pfc_pinconf_validate(pfc
, _pin
, param
))
606 case PIN_CONFIG_BIAS_DISABLE
:
607 case PIN_CONFIG_BIAS_PULL_UP
:
608 case PIN_CONFIG_BIAS_PULL_DOWN
: {
611 if (!pfc
->info
->ops
|| !pfc
->info
->ops
->get_bias
)
614 spin_lock_irqsave(&pfc
->lock
, flags
);
615 bias
= pfc
->info
->ops
->get_bias(pfc
, _pin
);
616 spin_unlock_irqrestore(&pfc
->lock
, flags
);
625 case PIN_CONFIG_DRIVE_STRENGTH
: {
628 ret
= sh_pfc_pinconf_get_drive_strength(pfc
, _pin
);
636 case PIN_CONFIG_POWER_SOURCE
: {
640 if (!pfc
->info
->ops
|| !pfc
->info
->ops
->pin_to_pocctrl
)
643 bit
= pfc
->info
->ops
->pin_to_pocctrl(pfc
, _pin
, &pocctrl
);
644 if (WARN(bit
< 0, "invalid pin %#x", _pin
))
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;
659 *config
= pinconf_to_config_packed(param
, arg
);
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
;
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
))
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
)
685 spin_lock_irqsave(&pfc
->lock
, flags
);
686 pfc
->info
->ops
->set_bias(pfc
, _pin
, param
);
687 spin_unlock_irqrestore(&pfc
->lock
, flags
);
691 case PIN_CONFIG_DRIVE_STRENGTH
: {
693 pinconf_to_config_argument(configs
[i
]);
696 ret
= sh_pfc_pinconf_set_drive_strength(pfc
, _pin
, arg
);
703 case PIN_CONFIG_POWER_SOURCE
: {
704 unsigned int mV
= pinconf_to_config_argument(configs
[i
]);
708 if (!pfc
->info
->ops
|| !pfc
->info
->ops
->pin_to_pocctrl
)
711 bit
= pfc
->info
->ops
->pin_to_pocctrl(pfc
, _pin
, &pocctrl
);
712 if (WARN(bit
< 0, "invalid pin %#x", _pin
))
715 if (mV
!= 1800 && mV
!= 3300)
718 spin_lock_irqsave(&pfc
->lock
, flags
);
719 val
= sh_pfc_read(pfc
, pocctrl
);
724 sh_pfc_write(pfc
, pocctrl
, val
);
725 spin_unlock_irqrestore(&pfc
->lock
, flags
);
733 } /* for each config */
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
;
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
);
759 static const struct pinconf_ops sh_pfc_pinconf_ops
= {
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
)
772 /* Allocate and initialize the pins and configs arrays. */
773 pmx
->pins
= devm_kzalloc(pfc
->dev
,
774 sizeof(*pmx
->pins
) * pfc
->info
->nr_pins
,
776 if (unlikely(!pmx
->pins
))
779 pmx
->configs
= devm_kzalloc(pfc
->dev
,
780 sizeof(*pmx
->configs
) * pfc
->info
->nr_pins
,
782 if (unlikely(!pmx
->configs
))
785 for (i
= 0; i
< pfc
->info
->nr_pins
; ++i
) {
786 const struct sh_pfc_pin
*info
= &pfc
->info
->pins
[i
];
787 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[i
];
788 struct pinctrl_pin_desc
*pin
= &pmx
->pins
[i
];
790 /* If the pin number is equal to -1 all pins are considered */
791 pin
->number
= info
->pin
!= (u16
)-1 ? info
->pin
: i
;
792 pin
->name
= info
->name
;
793 cfg
->type
= PINMUX_TYPE_NONE
;
799 int sh_pfc_register_pinctrl(struct sh_pfc
*pfc
)
801 struct sh_pfc_pinctrl
*pmx
;
804 pmx
= devm_kzalloc(pfc
->dev
, sizeof(*pmx
), GFP_KERNEL
);
810 ret
= sh_pfc_map_pins(pfc
, pmx
);
814 pmx
->pctl_desc
.name
= DRV_NAME
;
815 pmx
->pctl_desc
.owner
= THIS_MODULE
;
816 pmx
->pctl_desc
.pctlops
= &sh_pfc_pinctrl_ops
;
817 pmx
->pctl_desc
.pmxops
= &sh_pfc_pinmux_ops
;
818 pmx
->pctl_desc
.confops
= &sh_pfc_pinconf_ops
;
819 pmx
->pctl_desc
.pins
= pmx
->pins
;
820 pmx
->pctl_desc
.npins
= pfc
->info
->nr_pins
;
822 ret
= devm_pinctrl_register_and_init(pfc
->dev
, &pmx
->pctl_desc
, pmx
,
825 dev_err(pfc
->dev
, "could not register: %i\n", ret
);
830 return pinctrl_enable(pmx
->pctl
);