blob: 3ee75fbbeea1740c8d02242f2a25fe58fee8eed4 [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/*
3 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada0b53a752015-08-27 12:44:30 +09004 */
5
6/* #define DEBUG */
7
8#include <common.h>
Simon Glass11c89f32017-05-17 17:18:03 -06009#include <dm.h>
Masahiro Yamada0b53a752015-08-27 12:44:30 +090010#include <dm/pinctrl.h>
11
12static const char * const sandbox_pins[] = {
13 "SCL",
14 "SDA",
15 "TX",
16 "RX",
Patrick Delaunay939cbe92020-01-13 11:35:12 +010017 "W1",
18 "GPIO0",
19 "GPIO1",
20 "GPIO2",
21 "GPIO3",
Masahiro Yamada0b53a752015-08-27 12:44:30 +090022};
23
Patrice Chotard3cd1f332018-10-24 14:10:22 +020024static const char * const sandbox_pins_muxing[] = {
25 "I2C SCL",
26 "I2C SDA",
27 "Uart TX",
28 "Uart RX",
29 "1-wire gpio",
Patrick Delaunay939cbe92020-01-13 11:35:12 +010030 "gpio",
31 "gpio",
32 "gpio",
33 "gpio",
Patrice Chotard3cd1f332018-10-24 14:10:22 +020034};
35
Masahiro Yamada0b53a752015-08-27 12:44:30 +090036static const char * const sandbox_groups[] = {
37 "i2c",
38 "serial_a",
39 "serial_b",
40 "spi",
Eugen Hristev200e35e2018-09-18 10:35:34 +030041 "w1",
Masahiro Yamada0b53a752015-08-27 12:44:30 +090042};
43
44static const char * const sandbox_functions[] = {
45 "i2c",
46 "serial",
47 "spi",
Eugen Hristev200e35e2018-09-18 10:35:34 +030048 "w1",
Patrick Delaunay939cbe92020-01-13 11:35:12 +010049 "gpio",
50 "gpio",
51 "gpio",
52 "gpio",
Masahiro Yamada0b53a752015-08-27 12:44:30 +090053};
54
55static const struct pinconf_param sandbox_conf_params[] = {
56 { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
57 { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 },
58 { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
59 { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
60 { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
61 { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
62 { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
63 { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 },
64 { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
65 { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
66 { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
67};
68
Patrick Delaunay43826922020-01-13 11:35:11 +010069/* bitfield used to save param and value of each pin/selector */
70static unsigned int sandbox_pins_param[ARRAY_SIZE(sandbox_pins)];
71static unsigned int sandbox_pins_value[ARRAY_SIZE(sandbox_pins)];
72
Masahiro Yamada0b53a752015-08-27 12:44:30 +090073static int sandbox_get_pins_count(struct udevice *dev)
74{
75 return ARRAY_SIZE(sandbox_pins);
76}
77
78static const char *sandbox_get_pin_name(struct udevice *dev, unsigned selector)
79{
80 return sandbox_pins[selector];
81}
82
Patrice Chotard3cd1f332018-10-24 14:10:22 +020083static int sandbox_get_pin_muxing(struct udevice *dev,
84 unsigned int selector,
85 char *buf, int size)
86{
Patrick Delaunay43826922020-01-13 11:35:11 +010087 const struct pinconf_param *p;
88 int i;
89
Patrice Chotard3cd1f332018-10-24 14:10:22 +020090 snprintf(buf, size, "%s", sandbox_pins_muxing[selector]);
91
Patrick Delaunay43826922020-01-13 11:35:11 +010092 if (sandbox_pins_param[selector]) {
93 for (i = 0, p = sandbox_conf_params;
94 i < ARRAY_SIZE(sandbox_conf_params);
95 i++, p++) {
96 if ((sandbox_pins_param[selector] & BIT(p->param)) &&
97 (!!(sandbox_pins_value[selector] & BIT(p->param)) ==
98 p->default_value)) {
99 strncat(buf, " ", size);
100 strncat(buf, p->property, size);
101 }
102 }
103 }
104 strncat(buf, ".", size);
105
Patrice Chotard3cd1f332018-10-24 14:10:22 +0200106 return 0;
107}
108
Masahiro Yamada0b53a752015-08-27 12:44:30 +0900109static int sandbox_get_groups_count(struct udevice *dev)
110{
111 return ARRAY_SIZE(sandbox_groups);
112}
113
114static const char *sandbox_get_group_name(struct udevice *dev,
115 unsigned selector)
116{
117 return sandbox_groups[selector];
118}
119
120static int sandbox_get_functions_count(struct udevice *dev)
121{
122 return ARRAY_SIZE(sandbox_functions);
123}
124
125static const char *sandbox_get_function_name(struct udevice *dev,
126 unsigned selector)
127{
128 return sandbox_functions[selector];
129}
130
131static int sandbox_pinmux_set(struct udevice *dev, unsigned pin_selector,
132 unsigned func_selector)
133{
134 debug("sandbox pinmux: pin = %d (%s), function = %d (%s)\n",
135 pin_selector, sandbox_get_pin_name(dev, pin_selector),
136 func_selector, sandbox_get_function_name(dev, func_selector));
137
Patrick Delaunay43826922020-01-13 11:35:11 +0100138 sandbox_pins_param[pin_selector] = 0;
139 sandbox_pins_value[pin_selector] = 0;
140
Masahiro Yamada0b53a752015-08-27 12:44:30 +0900141 return 0;
142}
143
144static int sandbox_pinmux_group_set(struct udevice *dev,
145 unsigned group_selector,
146 unsigned func_selector)
147{
148 debug("sandbox pinmux: group = %d (%s), function = %d (%s)\n",
149 group_selector, sandbox_get_group_name(dev, group_selector),
150 func_selector, sandbox_get_function_name(dev, func_selector));
151
152 return 0;
153}
154
155static int sandbox_pinconf_set(struct udevice *dev, unsigned pin_selector,
156 unsigned param, unsigned argument)
157{
158 debug("sandbox pinconf: pin = %d (%s), param = %d, arg = %d\n",
159 pin_selector, sandbox_get_pin_name(dev, pin_selector),
160 param, argument);
161
Patrick Delaunay43826922020-01-13 11:35:11 +0100162 sandbox_pins_param[pin_selector] |= BIT(param);
163 if (argument)
164 sandbox_pins_value[pin_selector] |= BIT(param);
165 else
166 sandbox_pins_value[pin_selector] &= ~BIT(param);
167
Masahiro Yamada0b53a752015-08-27 12:44:30 +0900168 return 0;
169}
170
171static int sandbox_pinconf_group_set(struct udevice *dev,
172 unsigned group_selector,
173 unsigned param, unsigned argument)
174{
175 debug("sandbox pinconf: group = %d (%s), param = %d, arg = %d\n",
176 group_selector, sandbox_get_group_name(dev, group_selector),
177 param, argument);
178
179 return 0;
180}
181
182const struct pinctrl_ops sandbox_pinctrl_ops = {
183 .get_pins_count = sandbox_get_pins_count,
184 .get_pin_name = sandbox_get_pin_name,
Patrice Chotard3cd1f332018-10-24 14:10:22 +0200185 .get_pin_muxing = sandbox_get_pin_muxing,
Masahiro Yamada0b53a752015-08-27 12:44:30 +0900186 .get_groups_count = sandbox_get_groups_count,
187 .get_group_name = sandbox_get_group_name,
188 .get_functions_count = sandbox_get_functions_count,
189 .get_function_name = sandbox_get_function_name,
190 .pinmux_set = sandbox_pinmux_set,
191 .pinmux_group_set = sandbox_pinmux_group_set,
192 .pinconf_num_params = ARRAY_SIZE(sandbox_conf_params),
193 .pinconf_params = sandbox_conf_params,
194 .pinconf_set = sandbox_pinconf_set,
195 .pinconf_group_set = sandbox_pinconf_group_set,
196 .set_state = pinctrl_generic_set_state,
197};
198
199static const struct udevice_id sandbox_pinctrl_match[] = {
200 { .compatible = "sandbox,pinctrl" },
201 { /* sentinel */ }
202};
203
204U_BOOT_DRIVER(sandbox_pinctrl) = {
205 .name = "sandbox_pinctrl",
206 .id = UCLASS_PINCTRL,
207 .of_match = sandbox_pinctrl_match,
208 .ops = &sandbox_pinctrl_ops,
209};