1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
11 #include <dm/pinctrl.h>
12 #include <linux/bitops.h>
14 static const char * const sandbox_pins[] = {
26 static const char * const sandbox_pins_muxing[] = {
38 static const char * const sandbox_groups[] = {
46 static const char * const sandbox_functions[] = {
57 static const struct pinconf_param sandbox_conf_params[] = {
58 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
59 { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 },
60 { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
61 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
62 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
63 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
64 { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
65 { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 },
66 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
67 { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
68 { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
71 /* bitfield used to save param and value of each pin/selector */
72 static unsigned int sandbox_pins_param[ARRAY_SIZE(sandbox_pins)];
73 static unsigned int sandbox_pins_value[ARRAY_SIZE(sandbox_pins)];
75 static int sandbox_get_pins_count(struct udevice *dev)
77 return ARRAY_SIZE(sandbox_pins);
80 static const char *sandbox_get_pin_name(struct udevice *dev, unsigned selector)
82 return sandbox_pins[selector];
85 static int sandbox_get_pin_muxing(struct udevice *dev,
86 unsigned int selector,
89 const struct pinconf_param *p;
92 snprintf(buf, size, "%s", sandbox_pins_muxing[selector]);
94 if (sandbox_pins_param[selector]) {
95 for (i = 0, p = sandbox_conf_params;
96 i < ARRAY_SIZE(sandbox_conf_params);
98 if ((sandbox_pins_param[selector] & BIT(p->param)) &&
99 (!!(sandbox_pins_value[selector] & BIT(p->param)) ==
101 strncat(buf, " ", size);
102 strncat(buf, p->property, size);
106 strncat(buf, ".", size);
111 static int sandbox_get_groups_count(struct udevice *dev)
113 return ARRAY_SIZE(sandbox_groups);
116 static const char *sandbox_get_group_name(struct udevice *dev,
119 return sandbox_groups[selector];
122 static int sandbox_get_functions_count(struct udevice *dev)
124 return ARRAY_SIZE(sandbox_functions);
127 static const char *sandbox_get_function_name(struct udevice *dev,
130 return sandbox_functions[selector];
133 static int sandbox_pinmux_set(struct udevice *dev, unsigned pin_selector,
134 unsigned func_selector)
136 debug("sandbox pinmux: pin = %d (%s), function = %d (%s)\n",
137 pin_selector, sandbox_get_pin_name(dev, pin_selector),
138 func_selector, sandbox_get_function_name(dev, func_selector));
140 sandbox_pins_param[pin_selector] = 0;
141 sandbox_pins_value[pin_selector] = 0;
146 static int sandbox_pinmux_group_set(struct udevice *dev,
147 unsigned group_selector,
148 unsigned func_selector)
150 debug("sandbox pinmux: group = %d (%s), function = %d (%s)\n",
151 group_selector, sandbox_get_group_name(dev, group_selector),
152 func_selector, sandbox_get_function_name(dev, func_selector));
157 static int sandbox_pinconf_set(struct udevice *dev, unsigned pin_selector,
158 unsigned param, unsigned argument)
160 debug("sandbox pinconf: pin = %d (%s), param = %d, arg = %d\n",
161 pin_selector, sandbox_get_pin_name(dev, pin_selector),
164 sandbox_pins_param[pin_selector] |= BIT(param);
166 sandbox_pins_value[pin_selector] |= BIT(param);
168 sandbox_pins_value[pin_selector] &= ~BIT(param);
173 static int sandbox_pinconf_group_set(struct udevice *dev,
174 unsigned group_selector,
175 unsigned param, unsigned argument)
177 debug("sandbox pinconf: group = %d (%s), param = %d, arg = %d\n",
178 group_selector, sandbox_get_group_name(dev, group_selector),
184 const struct pinctrl_ops sandbox_pinctrl_ops = {
185 .get_pins_count = sandbox_get_pins_count,
186 .get_pin_name = sandbox_get_pin_name,
187 .get_pin_muxing = sandbox_get_pin_muxing,
188 .get_groups_count = sandbox_get_groups_count,
189 .get_group_name = sandbox_get_group_name,
190 .get_functions_count = sandbox_get_functions_count,
191 .get_function_name = sandbox_get_function_name,
192 .pinmux_set = sandbox_pinmux_set,
193 .pinmux_group_set = sandbox_pinmux_group_set,
194 .pinconf_num_params = ARRAY_SIZE(sandbox_conf_params),
195 .pinconf_params = sandbox_conf_params,
196 .pinconf_set = sandbox_pinconf_set,
197 .pinconf_group_set = sandbox_pinconf_group_set,
198 .set_state = pinctrl_generic_set_state,
201 static const struct udevice_id sandbox_pinctrl_match[] = {
202 { .compatible = "sandbox,pinctrl" },
206 U_BOOT_DRIVER(sandbox_pinctrl) = {
207 .name = "sandbox_pinctrl",
208 .id = UCLASS_PINCTRL,
209 .of_match = sandbox_pinctrl_match,
210 .ops = &sandbox_pinctrl_ops,