Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[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 <log.h>
11 #include <dm/pinctrl.h>
12 #include <linux/bitops.h>
13
14 static const char * const sandbox_pins[] = {
15         "SCL",
16         "SDA",
17         "TX",
18         "RX",
19         "W1",
20         "GPIO0",
21         "GPIO1",
22         "GPIO2",
23         "GPIO3",
24 };
25
26 static const char * const sandbox_pins_muxing[] = {
27         "I2C SCL",
28         "I2C SDA",
29         "Uart TX",
30         "Uart RX",
31         "1-wire gpio",
32         "gpio",
33         "gpio",
34         "gpio",
35         "gpio",
36 };
37
38 static const char * const sandbox_groups[] = {
39         "i2c",
40         "serial_a",
41         "serial_b",
42         "spi",
43         "w1",
44 };
45
46 static const char * const sandbox_functions[] = {
47         "i2c",
48         "serial",
49         "spi",
50         "w1",
51         "gpio",
52         "gpio",
53         "gpio",
54         "gpio",
55 };
56
57 static 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
71 /* bitfield used to save param and value of each pin/selector */
72 static unsigned int sandbox_pins_param[ARRAY_SIZE(sandbox_pins)];
73 static unsigned int sandbox_pins_value[ARRAY_SIZE(sandbox_pins)];
74
75 static int sandbox_get_pins_count(struct udevice *dev)
76 {
77         return ARRAY_SIZE(sandbox_pins);
78 }
79
80 static const char *sandbox_get_pin_name(struct udevice *dev, unsigned selector)
81 {
82         return sandbox_pins[selector];
83 }
84
85 static int sandbox_get_pin_muxing(struct udevice *dev,
86                                   unsigned int selector,
87                                   char *buf, int size)
88 {
89         const struct pinconf_param *p;
90         int i;
91
92         snprintf(buf, size, "%s", sandbox_pins_muxing[selector]);
93
94         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
108         return 0;
109 }
110
111 static int sandbox_get_groups_count(struct udevice *dev)
112 {
113         return ARRAY_SIZE(sandbox_groups);
114 }
115
116 static const char *sandbox_get_group_name(struct udevice *dev,
117                                           unsigned selector)
118 {
119         return sandbox_groups[selector];
120 }
121
122 static int sandbox_get_functions_count(struct udevice *dev)
123 {
124         return ARRAY_SIZE(sandbox_functions);
125 }
126
127 static const char *sandbox_get_function_name(struct udevice *dev,
128                                              unsigned selector)
129 {
130         return sandbox_functions[selector];
131 }
132
133 static 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
140         sandbox_pins_param[pin_selector] = 0;
141         sandbox_pins_value[pin_selector] = 0;
142
143         return 0;
144 }
145
146 static 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
157 static 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
164         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
170         return 0;
171 }
172
173 static 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
184 const struct pinctrl_ops sandbox_pinctrl_ops = {
185         .get_pins_count = sandbox_get_pins_count,
186         .get_pin_name = sandbox_get_pin_name,
187         .get_pin_muxing = sandbox_get_pin_muxing,
188         .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
201 static const struct udevice_id sandbox_pinctrl_match[] = {
202         { .compatible = "sandbox,pinctrl" },
203         { /* sentinel */ }
204 };
205
206 U_BOOT_DRIVER(sandbox_pinctrl) = {
207         .name = "sandbox_pinctrl",
208         .id = UCLASS_PINCTRL,
209         .of_match = sandbox_pinctrl_match,
210         .ops = &sandbox_pinctrl_ops,
211 };