blob: a5d056643a0fcfba178c217ceb434c38ad2a6478 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada0b53a752015-08-27 12:44:30 +09002/*
Sean Anderson3438e3b2020-09-14 11:01:57 -04003 * Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
4 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada0b53a752015-08-27 12:44:30 +09005 */
6
Simon Glass11c89f32017-05-17 17:18:03 -06007#include <dm.h>
Masahiro Yamada0b53a752015-08-27 12:44:30 +09008#include <dm/pinctrl.h>
Sean Anderson3438e3b2020-09-14 11:01:57 -04009#include <dt-bindings/pinctrl/sandbox-pinmux.h>
10#include <log.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060011#include <linux/bitops.h>
Masahiro Yamada0b53a752015-08-27 12:44:30 +090012
Sean Anderson3438e3b2020-09-14 11:01:57 -040013/*
14 * This driver emulates a pin controller with the following rules:
15 * - The pinctrl config for each pin must be set individually
16 * - The first three pins (P0-P2) must be muxed as a group
17 * - The next two pins (P3-P4) must be muxed as a group
18 * - The last four pins (P5-P8) must be muxed individually
19 */
20
Masahiro Yamada0b53a752015-08-27 12:44:30 +090021static const char * const sandbox_pins[] = {
Sean Anderson3438e3b2020-09-14 11:01:57 -040022#define PIN(x) \
23 [x] = "P" #x
24 PIN(0),
25 PIN(1),
26 PIN(2),
27 PIN(3),
28 PIN(4),
29 PIN(5),
30 PIN(6),
31 PIN(7),
32 PIN(8),
33#undef PIN
Masahiro Yamada0b53a752015-08-27 12:44:30 +090034};
35
Sean Anderson3438e3b2020-09-14 11:01:57 -040036static const char * const sandbox_pins_muxing[][2] = {
37 { "UART TX", "I2C SCL" },
38 { "UART RX", "I2C SDA" },
39 { "SPI SCLK", "I2S SCK" },
40 { "SPI MOSI", "I2S SD" },
41 { "SPI MISO", "I2S WS" },
42 { "GPIO0", "SPI CS0" },
43 { "GPIO1", "SPI CS1" },
44 { "GPIO2", "PWM0" },
45 { "GPIO3", "PWM1" },
Patrice Chotard3cd1f332018-10-24 14:10:22 +020046};
47
Sean Anderson3438e3b2020-09-14 11:01:57 -040048#define SANDBOX_GROUP_I2C_UART 0
49#define SANDBOX_GROUP_SPI_I2S 1
50
Masahiro Yamada0b53a752015-08-27 12:44:30 +090051static const char * const sandbox_groups[] = {
Sean Anderson3438e3b2020-09-14 11:01:57 -040052 [SANDBOX_GROUP_I2C_UART] = "I2C_UART",
53 [SANDBOX_GROUP_SPI_I2S] = "SPI_I2S",
Masahiro Yamada0b53a752015-08-27 12:44:30 +090054};
55
56static const char * const sandbox_functions[] = {
Sean Anderson3438e3b2020-09-14 11:01:57 -040057#define FUNC(id) \
58 [SANDBOX_PINMUX_##id] = #id
59 FUNC(UART),
60 FUNC(I2C),
61 FUNC(SPI),
62 FUNC(I2S),
63 FUNC(GPIO),
64 FUNC(CS),
65 FUNC(PWM),
66#undef FUNC
Masahiro Yamada0b53a752015-08-27 12:44:30 +090067};
68
69static const struct pinconf_param sandbox_conf_params[] = {
70 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
71 { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 },
72 { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
73 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
74 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
75 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
76 { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
77 { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 },
78 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
79 { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
80 { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
81};
82
Sean Anderson3438e3b2020-09-14 11:01:57 -040083/* Bitfield used to save param and value of each pin/selector */
84struct sandbox_pinctrl_priv {
85 unsigned int mux;
86 unsigned int pins_param[ARRAY_SIZE(sandbox_pins)];
87 unsigned int pins_value[ARRAY_SIZE(sandbox_pins)];
88};
Patrick Delaunay43826922020-01-13 11:35:11 +010089
Masahiro Yamada0b53a752015-08-27 12:44:30 +090090static int sandbox_get_pins_count(struct udevice *dev)
91{
92 return ARRAY_SIZE(sandbox_pins);
93}
94
95static const char *sandbox_get_pin_name(struct udevice *dev, unsigned selector)
96{
97 return sandbox_pins[selector];
98}
99
Patrice Chotard3cd1f332018-10-24 14:10:22 +0200100static int sandbox_get_pin_muxing(struct udevice *dev,
101 unsigned int selector,
102 char *buf, int size)
103{
Patrick Delaunay43826922020-01-13 11:35:11 +0100104 const struct pinconf_param *p;
Sean Anderson3438e3b2020-09-14 11:01:57 -0400105 struct sandbox_pinctrl_priv *priv = dev_get_priv(dev);
Patrick Delaunay43826922020-01-13 11:35:11 +0100106 int i;
107
Sean Anderson3438e3b2020-09-14 11:01:57 -0400108 snprintf(buf, size, "%s",
109 sandbox_pins_muxing[selector][!!(priv->mux & BIT(selector))]);
Patrice Chotard3cd1f332018-10-24 14:10:22 +0200110
Sean Anderson3438e3b2020-09-14 11:01:57 -0400111 if (priv->pins_param[selector]) {
Patrick Delaunay43826922020-01-13 11:35:11 +0100112 for (i = 0, p = sandbox_conf_params;
113 i < ARRAY_SIZE(sandbox_conf_params);
114 i++, p++) {
Sean Anderson3438e3b2020-09-14 11:01:57 -0400115 if ((priv->pins_param[selector] & BIT(p->param)) &&
116 (!!(priv->pins_value[selector] & BIT(p->param)) ==
Patrick Delaunay43826922020-01-13 11:35:11 +0100117 p->default_value)) {
118 strncat(buf, " ", size);
119 strncat(buf, p->property, size);
120 }
121 }
122 }
123 strncat(buf, ".", size);
124
Patrice Chotard3cd1f332018-10-24 14:10:22 +0200125 return 0;
126}
127
Masahiro Yamada0b53a752015-08-27 12:44:30 +0900128static int sandbox_get_groups_count(struct udevice *dev)
129{
130 return ARRAY_SIZE(sandbox_groups);
131}
132
133static const char *sandbox_get_group_name(struct udevice *dev,
134 unsigned selector)
135{
136 return sandbox_groups[selector];
137}
138
139static int sandbox_get_functions_count(struct udevice *dev)
140{
141 return ARRAY_SIZE(sandbox_functions);
142}
143
144static const char *sandbox_get_function_name(struct udevice *dev,
145 unsigned selector)
146{
147 return sandbox_functions[selector];
148}
149
150static int sandbox_pinmux_set(struct udevice *dev, unsigned pin_selector,
151 unsigned func_selector)
152{
Sean Anderson3438e3b2020-09-14 11:01:57 -0400153 int mux;
154 struct sandbox_pinctrl_priv *priv = dev_get_priv(dev);
155
Masahiro Yamada0b53a752015-08-27 12:44:30 +0900156 debug("sandbox pinmux: pin = %d (%s), function = %d (%s)\n",
157 pin_selector, sandbox_get_pin_name(dev, pin_selector),
158 func_selector, sandbox_get_function_name(dev, func_selector));
159
Sean Anderson3438e3b2020-09-14 11:01:57 -0400160 if (pin_selector < 5)
161 return -EINVAL;
162
163 switch (func_selector) {
164 case SANDBOX_PINMUX_GPIO:
165 mux = 0;
166 break;
167 case SANDBOX_PINMUX_CS:
168 case SANDBOX_PINMUX_PWM:
169 mux = BIT(pin_selector);
170 break;
171 default:
172 return -EINVAL;
173 }
174
175 priv->mux &= ~BIT(pin_selector);
176 priv->mux |= mux;
177 priv->pins_param[pin_selector] = 0;
178 priv->pins_value[pin_selector] = 0;
Patrick Delaunay43826922020-01-13 11:35:11 +0100179
Masahiro Yamada0b53a752015-08-27 12:44:30 +0900180 return 0;
181}
182
183static int sandbox_pinmux_group_set(struct udevice *dev,
184 unsigned group_selector,
185 unsigned func_selector)
186{
Sean Anderson3438e3b2020-09-14 11:01:57 -0400187 bool mux;
188 int i, group_start, group_end;
189 struct sandbox_pinctrl_priv *priv = dev_get_priv(dev);
190 unsigned int mask;
191
Masahiro Yamada0b53a752015-08-27 12:44:30 +0900192 debug("sandbox pinmux: group = %d (%s), function = %d (%s)\n",
193 group_selector, sandbox_get_group_name(dev, group_selector),
194 func_selector, sandbox_get_function_name(dev, func_selector));
195
Sean Anderson3438e3b2020-09-14 11:01:57 -0400196 if (group_selector == SANDBOX_GROUP_I2C_UART) {
197 group_start = 0;
198 group_end = 1;
199
200 if (func_selector == SANDBOX_PINMUX_UART)
201 mux = false;
202 else if (func_selector == SANDBOX_PINMUX_I2C)
203 mux = true;
204 else
205 return -EINVAL;
206 } else if (group_selector == SANDBOX_GROUP_SPI_I2S) {
207 group_start = 2;
208 group_end = 4;
209
210 if (func_selector == SANDBOX_PINMUX_SPI)
211 mux = false;
212 else if (func_selector == SANDBOX_PINMUX_I2S)
213 mux = true;
214 else
215 return -EINVAL;
216 } else {
217 return -EINVAL;
218 }
219
220 mask = GENMASK(group_end, group_start);
221 priv->mux &= ~mask;
222 priv->mux |= mux ? mask : 0;
223
224 for (i = group_start; i < group_end; i++) {
225 priv->pins_param[i] = 0;
226 priv->pins_value[i] = 0;
227 }
228
Masahiro Yamada0b53a752015-08-27 12:44:30 +0900229 return 0;
230}
231
Sean Anderson3438e3b2020-09-14 11:01:57 -0400232static int sandbox_pinmux_property_set(struct udevice *dev, u32 pinmux_group)
233{
234 int ret;
235 unsigned pin_selector = pinmux_group & 0xFFFF;
236 unsigned func_selector = pinmux_group >> 16;
237
238 ret = sandbox_pinmux_set(dev, pin_selector, func_selector);
239 return ret ? ret : pin_selector;
240}
241
Masahiro Yamada0b53a752015-08-27 12:44:30 +0900242static int sandbox_pinconf_set(struct udevice *dev, unsigned pin_selector,
243 unsigned param, unsigned argument)
244{
Sean Anderson3438e3b2020-09-14 11:01:57 -0400245 struct sandbox_pinctrl_priv *priv = dev_get_priv(dev);
246
Masahiro Yamada0b53a752015-08-27 12:44:30 +0900247 debug("sandbox pinconf: pin = %d (%s), param = %d, arg = %d\n",
248 pin_selector, sandbox_get_pin_name(dev, pin_selector),
249 param, argument);
250
Sean Anderson3438e3b2020-09-14 11:01:57 -0400251 priv->pins_param[pin_selector] |= BIT(param);
Patrick Delaunay43826922020-01-13 11:35:11 +0100252 if (argument)
Sean Anderson3438e3b2020-09-14 11:01:57 -0400253 priv->pins_value[pin_selector] |= BIT(param);
Patrick Delaunay43826922020-01-13 11:35:11 +0100254 else
Sean Anderson3438e3b2020-09-14 11:01:57 -0400255 priv->pins_value[pin_selector] &= ~BIT(param);
Patrick Delaunay43826922020-01-13 11:35:11 +0100256
Masahiro Yamada0b53a752015-08-27 12:44:30 +0900257 return 0;
258}
259
260static int sandbox_pinconf_group_set(struct udevice *dev,
261 unsigned group_selector,
262 unsigned param, unsigned argument)
263{
264 debug("sandbox pinconf: group = %d (%s), param = %d, arg = %d\n",
265 group_selector, sandbox_get_group_name(dev, group_selector),
266 param, argument);
267
268 return 0;
269}
270
271const struct pinctrl_ops sandbox_pinctrl_ops = {
272 .get_pins_count = sandbox_get_pins_count,
273 .get_pin_name = sandbox_get_pin_name,
Patrice Chotard3cd1f332018-10-24 14:10:22 +0200274 .get_pin_muxing = sandbox_get_pin_muxing,
Masahiro Yamada0b53a752015-08-27 12:44:30 +0900275 .get_groups_count = sandbox_get_groups_count,
276 .get_group_name = sandbox_get_group_name,
277 .get_functions_count = sandbox_get_functions_count,
278 .get_function_name = sandbox_get_function_name,
279 .pinmux_set = sandbox_pinmux_set,
280 .pinmux_group_set = sandbox_pinmux_group_set,
Sean Anderson3438e3b2020-09-14 11:01:57 -0400281 .pinmux_property_set = sandbox_pinmux_property_set,
Masahiro Yamada0b53a752015-08-27 12:44:30 +0900282 .pinconf_num_params = ARRAY_SIZE(sandbox_conf_params),
283 .pinconf_params = sandbox_conf_params,
284 .pinconf_set = sandbox_pinconf_set,
285 .pinconf_group_set = sandbox_pinconf_group_set,
286 .set_state = pinctrl_generic_set_state,
287};
288
289static const struct udevice_id sandbox_pinctrl_match[] = {
290 { .compatible = "sandbox,pinctrl" },
291 { /* sentinel */ }
292};
293
294U_BOOT_DRIVER(sandbox_pinctrl) = {
295 .name = "sandbox_pinctrl",
296 .id = UCLASS_PINCTRL,
297 .of_match = sandbox_pinctrl_match,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700298 .priv_auto = sizeof(struct sandbox_pinctrl_priv),
Masahiro Yamada0b53a752015-08-27 12:44:30 +0900299 .ops = &sandbox_pinctrl_ops,
300};