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>
13 #include <linux/module.h>
15 #include <linux/seq_file.h>
16 #include <linux/slab.h>
17 #include <linux/spinlock.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/pinctrl/machine.h>
21 #include <linux/pinctrl/pinconf-generic.h>
22 #include <linux/pinctrl/pinconf.h>
23 #include <linux/pinctrl/pinctrl.h>
24 #include <linux/pinctrl/pinmux.h>
28 #include "../pinconf.h"
30 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
;
45 static int sh_pfc_get_groups_count(struct pinctrl_dev
*pctldev
)
47 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
49 return pmx
->pfc
->info
->nr_groups
;
52 static const char *sh_pfc_get_group_name(struct pinctrl_dev
*pctldev
,
55 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
57 return pmx
->pfc
->info
->groups
[selector
].name
;
60 static int sh_pfc_get_group_pins(struct pinctrl_dev
*pctldev
, unsigned selector
,
61 const unsigned **pins
, unsigned *num_pins
)
63 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
65 *pins
= pmx
->pfc
->info
->groups
[selector
].pins
;
66 *num_pins
= pmx
->pfc
->info
->groups
[selector
].nr_pins
;
71 static void sh_pfc_pin_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
74 seq_puts(s
, DRV_NAME
);
78 static int sh_pfc_map_add_config(struct pinctrl_map
*map
,
79 const char *group_or_pin
,
80 enum pinctrl_map_type type
,
81 unsigned long *configs
,
82 unsigned int num_configs
)
86 cfgs
= kmemdup_array(configs
, num_configs
, sizeof(*cfgs
), GFP_KERNEL
);
91 map
->data
.configs
.group_or_pin
= group_or_pin
;
92 map
->data
.configs
.configs
= cfgs
;
93 map
->data
.configs
.num_configs
= num_configs
;
98 static int sh_pfc_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
99 struct device_node
*np
,
100 struct pinctrl_map
**map
,
101 unsigned int *num_maps
, unsigned int *index
)
103 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
104 struct device
*dev
= pmx
->pfc
->dev
;
105 struct pinctrl_map
*maps
= *map
;
106 unsigned int nmaps
= *num_maps
;
107 unsigned int idx
= *index
;
108 unsigned int num_configs
;
109 const char *function
= NULL
;
110 unsigned long *configs
;
111 struct property
*prop
;
112 unsigned int num_groups
;
113 unsigned int num_pins
;
118 /* Parse the function and configuration properties. At least a function
119 * or one configuration must be specified.
121 ret
= of_property_read_string(np
, "function", &function
);
122 if (ret
< 0 && ret
!= -EINVAL
) {
123 dev_err(dev
, "Invalid function in DT\n");
127 ret
= pinconf_generic_parse_dt_config(np
, NULL
, &configs
, &num_configs
);
131 if (!function
&& num_configs
== 0) {
133 "DT node must contain at least a function or config\n");
138 /* Count the number of pins and groups and reallocate mappings. */
139 ret
= of_property_count_strings(np
, "pins");
140 if (ret
== -EINVAL
) {
142 } else if (ret
< 0) {
143 dev_err(dev
, "Invalid pins list in DT\n");
149 ret
= of_property_count_strings(np
, "groups");
150 if (ret
== -EINVAL
) {
152 } else if (ret
< 0) {
153 dev_err(dev
, "Invalid pin groups list in DT\n");
159 if (!num_pins
&& !num_groups
) {
160 dev_err(dev
, "No pin or group provided in DT node\n");
168 nmaps
+= num_pins
+ num_groups
;
170 maps
= krealloc(maps
, sizeof(*maps
) * nmaps
, GFP_KERNEL
);
179 /* Iterate over pins and groups and create the mappings. */
180 of_property_for_each_string(np
, "groups", prop
, group
) {
182 maps
[idx
].type
= PIN_MAP_TYPE_MUX_GROUP
;
183 maps
[idx
].data
.mux
.group
= group
;
184 maps
[idx
].data
.mux
.function
= function
;
189 ret
= sh_pfc_map_add_config(&maps
[idx
], group
,
190 PIN_MAP_TYPE_CONFIGS_GROUP
,
191 configs
, num_configs
);
204 of_property_for_each_string(np
, "pins", prop
, pin
) {
205 ret
= sh_pfc_map_add_config(&maps
[idx
], pin
,
206 PIN_MAP_TYPE_CONFIGS_PIN
,
207 configs
, num_configs
);
220 static void sh_pfc_dt_free_map(struct pinctrl_dev
*pctldev
,
221 struct pinctrl_map
*map
, unsigned num_maps
)
228 for (i
= 0; i
< num_maps
; ++i
) {
229 if (map
[i
].type
== PIN_MAP_TYPE_CONFIGS_GROUP
||
230 map
[i
].type
== PIN_MAP_TYPE_CONFIGS_PIN
)
231 kfree(map
[i
].data
.configs
.configs
);
237 static int sh_pfc_dt_node_to_map(struct pinctrl_dev
*pctldev
,
238 struct device_node
*np
,
239 struct pinctrl_map
**map
, unsigned *num_maps
)
241 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
242 struct device
*dev
= pmx
->pfc
->dev
;
250 for_each_child_of_node_scoped(np
, child
) {
251 ret
= sh_pfc_dt_subnode_to_map(pctldev
, child
, map
, num_maps
,
257 /* If no mapping has been found in child nodes try the config node. */
258 if (*num_maps
== 0) {
259 ret
= sh_pfc_dt_subnode_to_map(pctldev
, np
, map
, num_maps
,
268 dev_err(dev
, "no mapping found in node %pOF\n", np
);
273 sh_pfc_dt_free_map(pctldev
, *map
, *num_maps
);
277 #endif /* CONFIG_OF */
279 static const struct pinctrl_ops sh_pfc_pinctrl_ops
= {
280 .get_groups_count
= sh_pfc_get_groups_count
,
281 .get_group_name
= sh_pfc_get_group_name
,
282 .get_group_pins
= sh_pfc_get_group_pins
,
283 .pin_dbg_show
= sh_pfc_pin_dbg_show
,
285 .dt_node_to_map
= sh_pfc_dt_node_to_map
,
286 .dt_free_map
= sh_pfc_dt_free_map
,
290 static int sh_pfc_get_functions_count(struct pinctrl_dev
*pctldev
)
292 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
294 return pmx
->pfc
->info
->nr_functions
;
297 static const char *sh_pfc_get_function_name(struct pinctrl_dev
*pctldev
,
300 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
302 return pmx
->pfc
->info
->functions
[selector
].name
;
305 static int sh_pfc_get_function_groups(struct pinctrl_dev
*pctldev
,
307 const char * const **groups
,
308 unsigned * const num_groups
)
310 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
312 *groups
= pmx
->pfc
->info
->functions
[selector
].groups
;
313 *num_groups
= pmx
->pfc
->info
->functions
[selector
].nr_groups
;
318 static int sh_pfc_func_set_mux(struct pinctrl_dev
*pctldev
, unsigned selector
,
321 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
322 struct sh_pfc
*pfc
= pmx
->pfc
;
323 const struct sh_pfc_pin_group
*grp
= &pfc
->info
->groups
[group
];
328 dev_dbg(pctldev
->dev
, "Configuring pin group %s\n", grp
->name
);
330 spin_lock_irqsave(&pfc
->lock
, flags
);
332 for (i
= 0; i
< grp
->nr_pins
; ++i
) {
333 int idx
= sh_pfc_get_pin_index(pfc
, grp
->pins
[i
]);
334 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[idx
];
337 * This driver cannot manage both gpio and mux when the gpio
338 * pin is already enabled. So, this function fails.
340 if (cfg
->gpio_enabled
) {
345 ret
= sh_pfc_config_mux(pfc
, grp
->mux
[i
], PINMUX_TYPE_FUNCTION
);
350 /* All group pins are configured, mark the pins as muxed */
351 for (i
= 0; i
< grp
->nr_pins
; ++i
) {
352 int idx
= sh_pfc_get_pin_index(pfc
, grp
->pins
[i
]);
353 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[idx
];
355 cfg
->mux_mark
= grp
->mux
[i
];
359 spin_unlock_irqrestore(&pfc
->lock
, flags
);
363 static int sh_pfc_gpio_request_enable(struct pinctrl_dev
*pctldev
,
364 struct pinctrl_gpio_range
*range
,
367 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
368 struct sh_pfc
*pfc
= pmx
->pfc
;
369 int idx
= sh_pfc_get_pin_index(pfc
, offset
);
370 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[idx
];
374 spin_lock_irqsave(&pfc
->lock
, flags
);
376 if (!pfc
->gpio
&& !cfg
->mux_mark
) {
377 /* If GPIOs are handled externally the pin mux type needs to be
380 const struct sh_pfc_pin
*pin
= &pfc
->info
->pins
[idx
];
382 ret
= sh_pfc_config_mux(pfc
, pin
->enum_id
, PINMUX_TYPE_GPIO
);
387 cfg
->gpio_enabled
= true;
392 spin_unlock_irqrestore(&pfc
->lock
, flags
);
397 static void sh_pfc_gpio_disable_free(struct pinctrl_dev
*pctldev
,
398 struct pinctrl_gpio_range
*range
,
401 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
402 struct sh_pfc
*pfc
= pmx
->pfc
;
403 int idx
= sh_pfc_get_pin_index(pfc
, offset
);
404 struct sh_pfc_pin_config
*cfg
= &pmx
->configs
[idx
];
407 spin_lock_irqsave(&pfc
->lock
, flags
);
408 cfg
->gpio_enabled
= false;
409 /* If mux is already set, this configures it here */
411 sh_pfc_config_mux(pfc
, cfg
->mux_mark
, PINMUX_TYPE_FUNCTION
);
412 spin_unlock_irqrestore(&pfc
->lock
, flags
);
415 #ifdef CONFIG_PINCTRL_SH_PFC_GPIO
416 static int sh_pfc_gpio_set_direction(struct pinctrl_dev
*pctldev
,
417 struct pinctrl_gpio_range
*range
,
418 unsigned offset
, bool input
)
420 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
421 struct sh_pfc
*pfc
= pmx
->pfc
;
422 int new_type
= input
? PINMUX_TYPE_INPUT
: PINMUX_TYPE_OUTPUT
;
423 int idx
= sh_pfc_get_pin_index(pfc
, offset
);
424 const struct sh_pfc_pin
*pin
= &pfc
->info
->pins
[idx
];
429 /* Check if the requested direction is supported by the pin. Not all
430 * SoCs provide pin config data, so perform the check conditionally.
433 dir
= input
? SH_PFC_PIN_CFG_INPUT
: SH_PFC_PIN_CFG_OUTPUT
;
434 if (!(pin
->configs
& dir
))
438 spin_lock_irqsave(&pfc
->lock
, flags
);
439 ret
= sh_pfc_config_mux(pfc
, pin
->enum_id
, new_type
);
440 spin_unlock_irqrestore(&pfc
->lock
, flags
);
444 #define sh_pfc_gpio_set_direction NULL
447 static const struct pinmux_ops sh_pfc_pinmux_ops
= {
448 .get_functions_count
= sh_pfc_get_functions_count
,
449 .get_function_name
= sh_pfc_get_function_name
,
450 .get_function_groups
= sh_pfc_get_function_groups
,
451 .set_mux
= sh_pfc_func_set_mux
,
452 .gpio_request_enable
= sh_pfc_gpio_request_enable
,
453 .gpio_disable_free
= sh_pfc_gpio_disable_free
,
454 .gpio_set_direction
= sh_pfc_gpio_set_direction
,
457 static u32
sh_pfc_pinconf_find_drive_strength_reg(struct sh_pfc
*pfc
,
458 unsigned int pin
, unsigned int *offset
, unsigned int *size
)
460 const struct pinmux_drive_reg_field
*field
;
461 const struct pinmux_drive_reg
*reg
;
464 for (reg
= pfc
->info
->drive_regs
; reg
->reg
; ++reg
) {
465 for (i
= 0; i
< ARRAY_SIZE(reg
->fields
); ++i
) {
466 field
= ®
->fields
[i
];
468 if (field
->size
&& field
->pin
== pin
) {
469 *offset
= field
->offset
;
480 static int sh_pfc_pinconf_get_drive_strength(struct sh_pfc
*pfc
,
488 reg
= sh_pfc_pinconf_find_drive_strength_reg(pfc
, pin
, &offset
, &size
);
492 val
= (sh_pfc_read(pfc
, reg
) >> offset
) & GENMASK(size
- 1, 0);
494 /* Convert the value to mA based on a full drive strength value of 24mA.
495 * We can make the full value configurable later if needed.
497 return (val
+ 1) * (size
== 2 ? 6 : 3);
500 static int sh_pfc_pinconf_set_drive_strength(struct sh_pfc
*pfc
,
501 unsigned int pin
, u16 strength
)
510 reg
= sh_pfc_pinconf_find_drive_strength_reg(pfc
, pin
, &offset
, &size
);
514 step
= size
== 2 ? 6 : 3;
516 if (strength
< step
|| strength
> 24)
519 /* Convert the value from mA based on a full drive strength value of
520 * 24mA. We can make the full value configurable later if needed.
522 strength
= strength
/ step
- 1;
524 spin_lock_irqsave(&pfc
->lock
, flags
);
526 val
= sh_pfc_read(pfc
, reg
);
527 val
&= ~GENMASK(offset
+ size
- 1, offset
);
528 val
|= strength
<< offset
;
530 sh_pfc_write(pfc
, reg
, val
);
532 spin_unlock_irqrestore(&pfc
->lock
, flags
);
537 /* Check whether the requested parameter is supported for a pin. */
538 static bool sh_pfc_pinconf_validate(struct sh_pfc
*pfc
, unsigned int _pin
,
539 enum pin_config_param param
)
541 int idx
= sh_pfc_get_pin_index(pfc
, _pin
);
542 const struct sh_pfc_pin
*pin
= &pfc
->info
->pins
[idx
];
545 case PIN_CONFIG_BIAS_DISABLE
:
546 return pin
->configs
& SH_PFC_PIN_CFG_PULL_UP_DOWN
;
548 case PIN_CONFIG_BIAS_PULL_UP
:
549 return pin
->configs
& SH_PFC_PIN_CFG_PULL_UP
;
551 case PIN_CONFIG_BIAS_PULL_DOWN
:
552 return pin
->configs
& SH_PFC_PIN_CFG_PULL_DOWN
;
554 case PIN_CONFIG_DRIVE_STRENGTH
:
555 return pin
->configs
& SH_PFC_PIN_CFG_DRIVE_STRENGTH
;
557 case PIN_CONFIG_POWER_SOURCE
:
558 return pin
->configs
& SH_PFC_PIN_CFG_IO_VOLTAGE_MASK
;
565 static int sh_pfc_pinconf_get(struct pinctrl_dev
*pctldev
, unsigned _pin
,
566 unsigned long *config
)
568 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
569 struct sh_pfc
*pfc
= pmx
->pfc
;
570 enum pin_config_param param
= pinconf_to_config_param(*config
);
574 if (!sh_pfc_pinconf_validate(pfc
, _pin
, param
))
578 case PIN_CONFIG_BIAS_DISABLE
:
579 case PIN_CONFIG_BIAS_PULL_UP
:
580 case PIN_CONFIG_BIAS_PULL_DOWN
: {
583 if (!pfc
->info
->ops
|| !pfc
->info
->ops
->get_bias
)
586 spin_lock_irqsave(&pfc
->lock
, flags
);
587 bias
= pfc
->info
->ops
->get_bias(pfc
, _pin
);
588 spin_unlock_irqrestore(&pfc
->lock
, flags
);
597 case PIN_CONFIG_DRIVE_STRENGTH
: {
600 ret
= sh_pfc_pinconf_get_drive_strength(pfc
, _pin
);
608 case PIN_CONFIG_POWER_SOURCE
: {
609 int idx
= sh_pfc_get_pin_index(pfc
, _pin
);
610 const struct sh_pfc_pin
*pin
= &pfc
->info
->pins
[idx
];
611 unsigned int mode
, lo
, hi
;
615 if (!pfc
->info
->ops
|| !pfc
->info
->ops
->pin_to_pocctrl
)
618 bit
= pfc
->info
->ops
->pin_to_pocctrl(_pin
, &pocctrl
);
619 if (WARN(bit
< 0, "invalid pin %#x", _pin
))
622 val
= sh_pfc_read(pfc
, pocctrl
);
624 mode
= pin
->configs
& SH_PFC_PIN_CFG_IO_VOLTAGE_MASK
;
625 lo
= mode
<= SH_PFC_PIN_CFG_IO_VOLTAGE_18_33
? 1800 : 2500;
626 hi
= mode
>= SH_PFC_PIN_CFG_IO_VOLTAGE_18_33
? 3300 : 2500;
628 arg
= (val
& BIT(bit
)) ? hi
: lo
;
636 *config
= pinconf_to_config_packed(param
, arg
);
640 static int sh_pfc_pinconf_set(struct pinctrl_dev
*pctldev
, unsigned _pin
,
641 unsigned long *configs
, unsigned num_configs
)
643 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
644 struct sh_pfc
*pfc
= pmx
->pfc
;
645 enum pin_config_param param
;
649 for (i
= 0; i
< num_configs
; i
++) {
650 param
= pinconf_to_config_param(configs
[i
]);
652 if (!sh_pfc_pinconf_validate(pfc
, _pin
, param
))
656 case PIN_CONFIG_BIAS_PULL_UP
:
657 case PIN_CONFIG_BIAS_PULL_DOWN
:
658 case PIN_CONFIG_BIAS_DISABLE
:
659 if (!pfc
->info
->ops
|| !pfc
->info
->ops
->set_bias
)
662 spin_lock_irqsave(&pfc
->lock
, flags
);
663 pfc
->info
->ops
->set_bias(pfc
, _pin
, param
);
664 spin_unlock_irqrestore(&pfc
->lock
, flags
);
668 case PIN_CONFIG_DRIVE_STRENGTH
: {
670 pinconf_to_config_argument(configs
[i
]);
673 ret
= sh_pfc_pinconf_set_drive_strength(pfc
, _pin
, arg
);
680 case PIN_CONFIG_POWER_SOURCE
: {
681 unsigned int mV
= pinconf_to_config_argument(configs
[i
]);
682 int idx
= sh_pfc_get_pin_index(pfc
, _pin
);
683 const struct sh_pfc_pin
*pin
= &pfc
->info
->pins
[idx
];
684 unsigned int mode
, lo
, hi
;
688 if (!pfc
->info
->ops
|| !pfc
->info
->ops
->pin_to_pocctrl
)
691 bit
= pfc
->info
->ops
->pin_to_pocctrl(_pin
, &pocctrl
);
692 if (WARN(bit
< 0, "invalid pin %#x", _pin
))
695 mode
= pin
->configs
& SH_PFC_PIN_CFG_IO_VOLTAGE_MASK
;
696 lo
= mode
<= SH_PFC_PIN_CFG_IO_VOLTAGE_18_33
? 1800 : 2500;
697 hi
= mode
>= SH_PFC_PIN_CFG_IO_VOLTAGE_18_33
? 3300 : 2500;
699 if (mV
!= lo
&& mV
!= hi
)
702 spin_lock_irqsave(&pfc
->lock
, flags
);
703 val
= sh_pfc_read(pfc
, pocctrl
);
708 sh_pfc_write(pfc
, pocctrl
, val
);
709 spin_unlock_irqrestore(&pfc
->lock
, flags
);
717 } /* for each config */
722 static int sh_pfc_pinconf_group_set(struct pinctrl_dev
*pctldev
, unsigned group
,
723 unsigned long *configs
,
724 unsigned num_configs
)
726 struct sh_pfc_pinctrl
*pmx
= pinctrl_dev_get_drvdata(pctldev
);
727 const unsigned int *pins
;
728 unsigned int num_pins
;
731 pins
= pmx
->pfc
->info
->groups
[group
].pins
;
732 num_pins
= pmx
->pfc
->info
->groups
[group
].nr_pins
;
734 for (i
= 0; i
< num_pins
; ++i
) {
735 ret
= sh_pfc_pinconf_set(pctldev
, pins
[i
], configs
, num_configs
);
743 static const struct pinconf_ops sh_pfc_pinconf_ops
= {
745 .pin_config_get
= sh_pfc_pinconf_get
,
746 .pin_config_set
= sh_pfc_pinconf_set
,
747 .pin_config_group_set
= sh_pfc_pinconf_group_set
,
748 .pin_config_config_dbg_show
= pinconf_generic_dump_config
,
751 /* PFC ranges -> pinctrl pin descs */
752 static int sh_pfc_map_pins(struct sh_pfc
*pfc
, struct sh_pfc_pinctrl
*pmx
)
756 /* Allocate and initialize the pins and configs arrays. */
757 pmx
->pins
= devm_kcalloc(pfc
->dev
,
758 pfc
->info
->nr_pins
, sizeof(*pmx
->pins
),
760 if (unlikely(!pmx
->pins
))
763 pmx
->configs
= devm_kcalloc(pfc
->dev
,
764 pfc
->info
->nr_pins
, sizeof(*pmx
->configs
),
766 if (unlikely(!pmx
->configs
))
769 for (i
= 0; i
< pfc
->info
->nr_pins
; ++i
) {
770 const struct sh_pfc_pin
*info
= &pfc
->info
->pins
[i
];
771 struct pinctrl_pin_desc
*pin
= &pmx
->pins
[i
];
773 /* If the pin number is equal to -1 all pins are considered */
774 pin
->number
= info
->pin
!= (u16
)-1 ? info
->pin
: i
;
775 pin
->name
= info
->name
;
781 int sh_pfc_register_pinctrl(struct sh_pfc
*pfc
)
783 struct sh_pfc_pinctrl
*pmx
;
786 pmx
= devm_kzalloc(pfc
->dev
, sizeof(*pmx
), GFP_KERNEL
);
792 ret
= sh_pfc_map_pins(pfc
, pmx
);
796 pmx
->pctl_desc
.name
= DRV_NAME
;
797 pmx
->pctl_desc
.owner
= THIS_MODULE
;
798 pmx
->pctl_desc
.pctlops
= &sh_pfc_pinctrl_ops
;
799 pmx
->pctl_desc
.pmxops
= &sh_pfc_pinmux_ops
;
800 pmx
->pctl_desc
.confops
= &sh_pfc_pinconf_ops
;
801 pmx
->pctl_desc
.pins
= pmx
->pins
;
802 pmx
->pctl_desc
.npins
= pfc
->info
->nr_pins
;
804 ret
= devm_pinctrl_register_and_init(pfc
->dev
, &pmx
->pctl_desc
, pmx
,
807 dev_err(pfc
->dev
, "could not register: %i\n", ret
);
812 return pinctrl_enable(pmx
->pctl
);
815 const struct pinmux_bias_reg
*
816 rcar_pin_to_bias_reg(const struct sh_pfc_soc_info
*info
, unsigned int pin
,
821 for (i
= 0; info
->bias_regs
[i
].puen
|| info
->bias_regs
[i
].pud
; i
++) {
822 for (j
= 0; j
< ARRAY_SIZE(info
->bias_regs
[i
].pins
); j
++) {
823 if (info
->bias_regs
[i
].pins
[j
] == pin
) {
825 return &info
->bias_regs
[i
];
830 WARN_ONCE(1, "Pin %u is not in bias info list\n", pin
);
835 unsigned int rcar_pinmux_get_bias(struct sh_pfc
*pfc
, unsigned int pin
)
837 const struct pinmux_bias_reg
*reg
;
840 reg
= rcar_pin_to_bias_reg(pfc
->info
, pin
, &bit
);
842 return PIN_CONFIG_BIAS_DISABLE
;
845 if (!(sh_pfc_read(pfc
, reg
->puen
) & BIT(bit
)))
846 return PIN_CONFIG_BIAS_DISABLE
;
847 else if (!reg
->pud
|| (sh_pfc_read(pfc
, reg
->pud
) & BIT(bit
)))
848 return PIN_CONFIG_BIAS_PULL_UP
;
850 return PIN_CONFIG_BIAS_PULL_DOWN
;
852 if (sh_pfc_read(pfc
, reg
->pud
) & BIT(bit
))
853 return PIN_CONFIG_BIAS_PULL_DOWN
;
855 return PIN_CONFIG_BIAS_DISABLE
;
859 void rcar_pinmux_set_bias(struct sh_pfc
*pfc
, unsigned int pin
,
862 const struct pinmux_bias_reg
*reg
;
866 reg
= rcar_pin_to_bias_reg(pfc
->info
, pin
, &bit
);
871 enable
= sh_pfc_read(pfc
, reg
->puen
) & ~BIT(bit
);
872 if (bias
!= PIN_CONFIG_BIAS_DISABLE
) {
876 updown
= sh_pfc_read(pfc
, reg
->pud
) & ~BIT(bit
);
877 if (bias
== PIN_CONFIG_BIAS_PULL_UP
)
880 sh_pfc_write(pfc
, reg
->pud
, updown
);
883 sh_pfc_write(pfc
, reg
->puen
, enable
);
885 enable
= sh_pfc_read(pfc
, reg
->pud
) & ~BIT(bit
);
886 if (bias
== PIN_CONFIG_BIAS_PULL_DOWN
)
889 sh_pfc_write(pfc
, reg
->pud
, enable
);
893 #define PORTnCR_PULMD_OFF (0 << 6)
894 #define PORTnCR_PULMD_DOWN (2 << 6)
895 #define PORTnCR_PULMD_UP (3 << 6)
896 #define PORTnCR_PULMD_MASK (3 << 6)
898 unsigned int rmobile_pinmux_get_bias(struct sh_pfc
*pfc
, unsigned int pin
)
900 void __iomem
*reg
= pfc
->windows
->virt
+
901 pfc
->info
->ops
->pin_to_portcr(pin
);
902 u32 value
= ioread8(reg
) & PORTnCR_PULMD_MASK
;
905 case PORTnCR_PULMD_UP
:
906 return PIN_CONFIG_BIAS_PULL_UP
;
907 case PORTnCR_PULMD_DOWN
:
908 return PIN_CONFIG_BIAS_PULL_DOWN
;
909 case PORTnCR_PULMD_OFF
:
911 return PIN_CONFIG_BIAS_DISABLE
;
915 void rmobile_pinmux_set_bias(struct sh_pfc
*pfc
, unsigned int pin
,
918 void __iomem
*reg
= pfc
->windows
->virt
+
919 pfc
->info
->ops
->pin_to_portcr(pin
);
920 u32 value
= ioread8(reg
) & ~PORTnCR_PULMD_MASK
;
923 case PIN_CONFIG_BIAS_PULL_UP
:
924 value
|= PORTnCR_PULMD_UP
;
926 case PIN_CONFIG_BIAS_PULL_DOWN
:
927 value
|= PORTnCR_PULMD_DOWN
;
931 iowrite8(value
, reg
);