Merge branch '2020-04-16-master-imports'
[oweals/u-boot.git] / drivers / pinctrl / pinctrl-sandbox.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2015  Masahiro Yamada <yamada.masahiro@socionext.com>
4  */
5
6 /* #define DEBUG */
7
8 #include <common.h>
9 #include <dm.h>
10 #include <dm/pinctrl.h>
11
12 static const char * const sandbox_pins[] = {
13         "SCL",
14         "SDA",
15         "TX",
16         "RX",
17         "W1",
18         "GPIO0",
19         "GPIO1",
20         "GPIO2",
21         "GPIO3",
22 };
23
24 static const char * const sandbox_pins_muxing[] = {
25         "I2C SCL",
26         "I2C SDA",
27         "Uart TX",
28         "Uart RX",
29         "1-wire gpio",
30         "gpio",
31         "gpio",
32         "gpio",
33         "gpio",
34 };
35
36 static const char * const sandbox_groups[] = {
37         "i2c",
38         "serial_a",
39         "serial_b",
40         "spi",
41         "w1",
42 };
43
44 static const char * const sandbox_functions[] = {
45         "i2c",
46         "serial",
47         "spi",
48         "w1",
49         "gpio",
50         "gpio",
51         "gpio",
52         "gpio",
53 };
54
55 static 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
69 /* bitfield used to save param and value of each pin/selector */
70 static unsigned int sandbox_pins_param[ARRAY_SIZE(sandbox_pins)];
71 static unsigned int sandbox_pins_value[ARRAY_SIZE(sandbox_pins)];
72
73 static int sandbox_get_pins_count(struct udevice *dev)
74 {
75         return ARRAY_SIZE(sandbox_pins);
76 }
77
78 static const char *sandbox_get_pin_name(struct udevice *dev, unsigned selector)
79 {
80         return sandbox_pins[selector];
81 }
82
83 static int sandbox_get_pin_muxing(struct udevice *dev,
84                                   unsigned int selector,
85                                   char *buf, int size)
86 {
87         const struct pinconf_param *p;
88         int i;
89
90         snprintf(buf, size, "%s", sandbox_pins_muxing[selector]);
91
92         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
106         return 0;
107 }
108
109 static int sandbox_get_groups_count(struct udevice *dev)
110 {
111         return ARRAY_SIZE(sandbox_groups);
112 }
113
114 static const char *sandbox_get_group_name(struct udevice *dev,
115                                           unsigned selector)
116 {
117         return sandbox_groups[selector];
118 }
119
120 static int sandbox_get_functions_count(struct udevice *dev)
121 {
122         return ARRAY_SIZE(sandbox_functions);
123 }
124
125 static const char *sandbox_get_function_name(struct udevice *dev,
126                                              unsigned selector)
127 {
128         return sandbox_functions[selector];
129 }
130
131 static 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
138         sandbox_pins_param[pin_selector] = 0;
139         sandbox_pins_value[pin_selector] = 0;
140
141         return 0;
142 }
143
144 static 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
155 static 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
162         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
168         return 0;
169 }
170
171 static 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
182 const struct pinctrl_ops sandbox_pinctrl_ops = {
183         .get_pins_count = sandbox_get_pins_count,
184         .get_pin_name = sandbox_get_pin_name,
185         .get_pin_muxing = sandbox_get_pin_muxing,
186         .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
199 static const struct udevice_id sandbox_pinctrl_match[] = {
200         { .compatible = "sandbox,pinctrl" },
201         { /* sentinel */ }
202 };
203
204 U_BOOT_DRIVER(sandbox_pinctrl) = {
205         .name = "sandbox_pinctrl",
206         .id = UCLASS_PINCTRL,
207         .of_match = sandbox_pinctrl_match,
208         .ops = &sandbox_pinctrl_ops,
209 };