blob: ac0119d1988b3d17d773424b14cc3a9429c5b203 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada9c6a3c62015-08-27 12:44:30 +09002/*
3 * Copyright (C) 2015 Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada9c6a3c62015-08-27 12:44:30 +09004 */
5
6/* #define DEBUG */
7
8#include <common.h>
Simon Glass9d922452017-05-17 17:18:03 -06009#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Masahiro Yamada9c6a3c62015-08-27 12:44:30 +090011#include <dm/pinctrl.h>
Simon Glasscd93d622020-05-10 11:40:13 -060012#include <linux/bitops.h>
Masahiro Yamada9c6a3c62015-08-27 12:44:30 +090013
14static const char * const sandbox_pins[] = {
15 "SCL",
16 "SDA",
17 "TX",
18 "RX",
Patrick Delaunayd15c05b2020-01-13 11:35:12 +010019 "W1",
20 "GPIO0",
21 "GPIO1",
22 "GPIO2",
23 "GPIO3",
Masahiro Yamada9c6a3c62015-08-27 12:44:30 +090024};
25
Patrice Chotard21e23aa2018-10-24 14:10:22 +020026static const char * const sandbox_pins_muxing[] = {
27 "I2C SCL",
28 "I2C SDA",
29 "Uart TX",
30 "Uart RX",
31 "1-wire gpio",
Patrick Delaunayd15c05b2020-01-13 11:35:12 +010032 "gpio",
33 "gpio",
34 "gpio",
35 "gpio",
Patrice Chotard21e23aa2018-10-24 14:10:22 +020036};
37
Masahiro Yamada9c6a3c62015-08-27 12:44:30 +090038static const char * const sandbox_groups[] = {
39 "i2c",
40 "serial_a",
41 "serial_b",
42 "spi",
Eugen Hristev0523a6c2018-09-18 10:35:34 +030043 "w1",
Masahiro Yamada9c6a3c62015-08-27 12:44:30 +090044};
45
46static const char * const sandbox_functions[] = {
47 "i2c",
48 "serial",
49 "spi",
Eugen Hristev0523a6c2018-09-18 10:35:34 +030050 "w1",
Patrick Delaunayd15c05b2020-01-13 11:35:12 +010051 "gpio",
52 "gpio",
53 "gpio",
54 "gpio",
Masahiro Yamada9c6a3c62015-08-27 12:44:30 +090055};
56
57static 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 },
69};
70
Patrick Delaunay77ed5692020-01-13 11:35:11 +010071/* bitfield used to save param and value of each pin/selector */
72static unsigned int sandbox_pins_param[ARRAY_SIZE(sandbox_pins)];
73static unsigned int sandbox_pins_value[ARRAY_SIZE(sandbox_pins)];
74
Masahiro Yamada9c6a3c62015-08-27 12:44:30 +090075static int sandbox_get_pins_count(struct udevice *dev)
76{
77 return ARRAY_SIZE(sandbox_pins);
78}
79
80static const char *sandbox_get_pin_name(struct udevice *dev, unsigned selector)
81{
82 return sandbox_pins[selector];
83}
84
Patrice Chotard21e23aa2018-10-24 14:10:22 +020085static int sandbox_get_pin_muxing(struct udevice *dev,
86 unsigned int selector,
87 char *buf, int size)
88{
Patrick Delaunay77ed5692020-01-13 11:35:11 +010089 const struct pinconf_param *p;
90 int i;
91
Patrice Chotard21e23aa2018-10-24 14:10:22 +020092 snprintf(buf, size, "%s", sandbox_pins_muxing[selector]);
93
Patrick Delaunay77ed5692020-01-13 11:35:11 +010094 if (sandbox_pins_param[selector]) {
95 for (i = 0, p = sandbox_conf_params;
96 i < ARRAY_SIZE(sandbox_conf_params);
97 i++, p++) {
98 if ((sandbox_pins_param[selector] & BIT(p->param)) &&
99 (!!(sandbox_pins_value[selector] & BIT(p->param)) ==
100 p->default_value)) {
101 strncat(buf, " ", size);
102 strncat(buf, p->property, size);
103 }
104 }
105 }
106 strncat(buf, ".", size);
107
Patrice Chotard21e23aa2018-10-24 14:10:22 +0200108 return 0;
109}
110
Masahiro Yamada9c6a3c62015-08-27 12:44:30 +0900111static int sandbox_get_groups_count(struct udevice *dev)
112{
113 return ARRAY_SIZE(sandbox_groups);
114}
115
116static const char *sandbox_get_group_name(struct udevice *dev,
117 unsigned selector)
118{
119 return sandbox_groups[selector];
120}
121
122static int sandbox_get_functions_count(struct udevice *dev)
123{
124 return ARRAY_SIZE(sandbox_functions);
125}
126
127static const char *sandbox_get_function_name(struct udevice *dev,
128 unsigned selector)
129{
130 return sandbox_functions[selector];
131}
132
133static int sandbox_pinmux_set(struct udevice *dev, unsigned pin_selector,
134 unsigned func_selector)
135{
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));
139
Patrick Delaunay77ed5692020-01-13 11:35:11 +0100140 sandbox_pins_param[pin_selector] = 0;
141 sandbox_pins_value[pin_selector] = 0;
142
Masahiro Yamada9c6a3c62015-08-27 12:44:30 +0900143 return 0;
144}
145
146static int sandbox_pinmux_group_set(struct udevice *dev,
147 unsigned group_selector,
148 unsigned func_selector)
149{
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));
153
154 return 0;
155}
156
157static int sandbox_pinconf_set(struct udevice *dev, unsigned pin_selector,
158 unsigned param, unsigned argument)
159{
160 debug("sandbox pinconf: pin = %d (%s), param = %d, arg = %d\n",
161 pin_selector, sandbox_get_pin_name(dev, pin_selector),
162 param, argument);
163
Patrick Delaunay77ed5692020-01-13 11:35:11 +0100164 sandbox_pins_param[pin_selector] |= BIT(param);
165 if (argument)
166 sandbox_pins_value[pin_selector] |= BIT(param);
167 else
168 sandbox_pins_value[pin_selector] &= ~BIT(param);
169
Masahiro Yamada9c6a3c62015-08-27 12:44:30 +0900170 return 0;
171}
172
173static int sandbox_pinconf_group_set(struct udevice *dev,
174 unsigned group_selector,
175 unsigned param, unsigned argument)
176{
177 debug("sandbox pinconf: group = %d (%s), param = %d, arg = %d\n",
178 group_selector, sandbox_get_group_name(dev, group_selector),
179 param, argument);
180
181 return 0;
182}
183
184const struct pinctrl_ops sandbox_pinctrl_ops = {
185 .get_pins_count = sandbox_get_pins_count,
186 .get_pin_name = sandbox_get_pin_name,
Patrice Chotard21e23aa2018-10-24 14:10:22 +0200187 .get_pin_muxing = sandbox_get_pin_muxing,
Masahiro Yamada9c6a3c62015-08-27 12:44:30 +0900188 .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,
199};
200
201static const struct udevice_id sandbox_pinctrl_match[] = {
202 { .compatible = "sandbox,pinctrl" },
203 { /* sentinel */ }
204};
205
206U_BOOT_DRIVER(sandbox_pinctrl) = {
207 .name = "sandbox_pinctrl",
208 .id = UCLASS_PINCTRL,
209 .of_match = sandbox_pinctrl_match,
210 .ops = &sandbox_pinctrl_ops,
211};