1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
4 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
9 #include <dm/pinctrl.h>
10 #include <dt-bindings/pinctrl/sandbox-pinmux.h>
12 #include <linux/bitops.h>
15 * This driver emulates a pin controller with the following rules:
16 * - The pinctrl config for each pin must be set individually
17 * - The first three pins (P0-P2) must be muxed as a group
18 * - The next two pins (P3-P4) must be muxed as a group
19 * - The last four pins (P5-P8) must be muxed individually
22 static const char * const sandbox_pins[] = {
37 static const char * const sandbox_pins_muxing[][2] = {
38 { "UART TX", "I2C SCL" },
39 { "UART RX", "I2C SDA" },
40 { "SPI SCLK", "I2S SCK" },
41 { "SPI MOSI", "I2S SD" },
42 { "SPI MISO", "I2S WS" },
43 { "GPIO0", "SPI CS0" },
44 { "GPIO1", "SPI CS1" },
49 #define SANDBOX_GROUP_I2C_UART 0
50 #define SANDBOX_GROUP_SPI_I2S 1
52 static const char * const sandbox_groups[] = {
53 [SANDBOX_GROUP_I2C_UART] = "I2C_UART",
54 [SANDBOX_GROUP_SPI_I2S] = "SPI_I2S",
57 static const char * const sandbox_functions[] = {
59 [SANDBOX_PINMUX_##id] = #id
70 static const struct pinconf_param sandbox_conf_params[] = {
71 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
72 { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 },
73 { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
74 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
75 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
76 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
77 { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
78 { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 },
79 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
80 { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
81 { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
84 /* Bitfield used to save param and value of each pin/selector */
85 struct sandbox_pinctrl_priv {
87 unsigned int pins_param[ARRAY_SIZE(sandbox_pins)];
88 unsigned int pins_value[ARRAY_SIZE(sandbox_pins)];
91 static int sandbox_get_pins_count(struct udevice *dev)
93 return ARRAY_SIZE(sandbox_pins);
96 static const char *sandbox_get_pin_name(struct udevice *dev, unsigned selector)
98 return sandbox_pins[selector];
101 static int sandbox_get_pin_muxing(struct udevice *dev,
102 unsigned int selector,
105 const struct pinconf_param *p;
106 struct sandbox_pinctrl_priv *priv = dev_get_priv(dev);
109 snprintf(buf, size, "%s",
110 sandbox_pins_muxing[selector][!!(priv->mux & BIT(selector))]);
112 if (priv->pins_param[selector]) {
113 for (i = 0, p = sandbox_conf_params;
114 i < ARRAY_SIZE(sandbox_conf_params);
116 if ((priv->pins_param[selector] & BIT(p->param)) &&
117 (!!(priv->pins_value[selector] & BIT(p->param)) ==
119 strncat(buf, " ", size);
120 strncat(buf, p->property, size);
124 strncat(buf, ".", size);
129 static int sandbox_get_groups_count(struct udevice *dev)
131 return ARRAY_SIZE(sandbox_groups);
134 static const char *sandbox_get_group_name(struct udevice *dev,
137 return sandbox_groups[selector];
140 static int sandbox_get_functions_count(struct udevice *dev)
142 return ARRAY_SIZE(sandbox_functions);
145 static const char *sandbox_get_function_name(struct udevice *dev,
148 return sandbox_functions[selector];
151 static int sandbox_pinmux_set(struct udevice *dev, unsigned pin_selector,
152 unsigned func_selector)
155 struct sandbox_pinctrl_priv *priv = dev_get_priv(dev);
157 debug("sandbox pinmux: pin = %d (%s), function = %d (%s)\n",
158 pin_selector, sandbox_get_pin_name(dev, pin_selector),
159 func_selector, sandbox_get_function_name(dev, func_selector));
161 if (pin_selector < 5)
164 switch (func_selector) {
165 case SANDBOX_PINMUX_GPIO:
168 case SANDBOX_PINMUX_CS:
169 case SANDBOX_PINMUX_PWM:
170 mux = BIT(pin_selector);
176 priv->mux &= ~BIT(pin_selector);
178 priv->pins_param[pin_selector] = 0;
179 priv->pins_value[pin_selector] = 0;
184 static int sandbox_pinmux_group_set(struct udevice *dev,
185 unsigned group_selector,
186 unsigned func_selector)
189 int i, group_start, group_end;
190 struct sandbox_pinctrl_priv *priv = dev_get_priv(dev);
193 debug("sandbox pinmux: group = %d (%s), function = %d (%s)\n",
194 group_selector, sandbox_get_group_name(dev, group_selector),
195 func_selector, sandbox_get_function_name(dev, func_selector));
197 if (group_selector == SANDBOX_GROUP_I2C_UART) {
201 if (func_selector == SANDBOX_PINMUX_UART)
203 else if (func_selector == SANDBOX_PINMUX_I2C)
207 } else if (group_selector == SANDBOX_GROUP_SPI_I2S) {
211 if (func_selector == SANDBOX_PINMUX_SPI)
213 else if (func_selector == SANDBOX_PINMUX_I2S)
221 mask = GENMASK(group_end, group_start);
223 priv->mux |= mux ? mask : 0;
225 for (i = group_start; i < group_end; i++) {
226 priv->pins_param[i] = 0;
227 priv->pins_value[i] = 0;
233 static int sandbox_pinmux_property_set(struct udevice *dev, u32 pinmux_group)
236 unsigned pin_selector = pinmux_group & 0xFFFF;
237 unsigned func_selector = pinmux_group >> 16;
239 ret = sandbox_pinmux_set(dev, pin_selector, func_selector);
240 return ret ? ret : pin_selector;
243 static int sandbox_pinconf_set(struct udevice *dev, unsigned pin_selector,
244 unsigned param, unsigned argument)
246 struct sandbox_pinctrl_priv *priv = dev_get_priv(dev);
248 debug("sandbox pinconf: pin = %d (%s), param = %d, arg = %d\n",
249 pin_selector, sandbox_get_pin_name(dev, pin_selector),
252 priv->pins_param[pin_selector] |= BIT(param);
254 priv->pins_value[pin_selector] |= BIT(param);
256 priv->pins_value[pin_selector] &= ~BIT(param);
261 static int sandbox_pinconf_group_set(struct udevice *dev,
262 unsigned group_selector,
263 unsigned param, unsigned argument)
265 debug("sandbox pinconf: group = %d (%s), param = %d, arg = %d\n",
266 group_selector, sandbox_get_group_name(dev, group_selector),
272 const struct pinctrl_ops sandbox_pinctrl_ops = {
273 .get_pins_count = sandbox_get_pins_count,
274 .get_pin_name = sandbox_get_pin_name,
275 .get_pin_muxing = sandbox_get_pin_muxing,
276 .get_groups_count = sandbox_get_groups_count,
277 .get_group_name = sandbox_get_group_name,
278 .get_functions_count = sandbox_get_functions_count,
279 .get_function_name = sandbox_get_function_name,
280 .pinmux_set = sandbox_pinmux_set,
281 .pinmux_group_set = sandbox_pinmux_group_set,
282 .pinmux_property_set = sandbox_pinmux_property_set,
283 .pinconf_num_params = ARRAY_SIZE(sandbox_conf_params),
284 .pinconf_params = sandbox_conf_params,
285 .pinconf_set = sandbox_pinconf_set,
286 .pinconf_group_set = sandbox_pinconf_group_set,
287 .set_state = pinctrl_generic_set_state,
290 static const struct udevice_id sandbox_pinctrl_match[] = {
291 { .compatible = "sandbox,pinctrl" },
295 U_BOOT_DRIVER(sandbox_pinctrl) = {
296 .name = "sandbox_pinctrl",
297 .id = UCLASS_PINCTRL,
298 .of_match = sandbox_pinctrl_match,
299 .priv_auto = sizeof(struct sandbox_pinctrl_priv),
300 .ops = &sandbox_pinctrl_ops,