common: Drop linux/bug.h from common header
[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
13 static const char * const sandbox_pins[] = {
14         "SCL",
15         "SDA",
16         "TX",
17         "RX",
18         "W1",
19         "GPIO0",
20         "GPIO1",
21         "GPIO2",
22         "GPIO3",
23 };
24
25 static const char * const sandbox_pins_muxing[] = {
26         "I2C SCL",
27         "I2C SDA",
28         "Uart TX",
29         "Uart RX",
30         "1-wire gpio",
31         "gpio",
32         "gpio",
33         "gpio",
34         "gpio",
35 };
36
37 static const char * const sandbox_groups[] = {
38         "i2c",
39         "serial_a",
40         "serial_b",
41         "spi",
42         "w1",
43 };
44
45 static const char * const sandbox_functions[] = {
46         "i2c",
47         "serial",
48         "spi",
49         "w1",
50         "gpio",
51         "gpio",
52         "gpio",
53         "gpio",
54 };
55
56 static const struct pinconf_param sandbox_conf_params[] = {
57         { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
58         { "bias-high-impedance", PIN_CONFIG_BIAS_HIGH_IMPEDANCE, 0 },
59         { "bias-bus-hold", PIN_CONFIG_BIAS_BUS_HOLD, 0 },
60         { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 1 },
61         { "bias-pull-down", PIN_CONFIG_BIAS_PULL_DOWN, 1 },
62         { "bias-pull-pin-default", PIN_CONFIG_BIAS_PULL_PIN_DEFAULT, 1 },
63         { "drive-open-drain", PIN_CONFIG_DRIVE_OPEN_DRAIN, 0 },
64         { "drive-open-source", PIN_CONFIG_DRIVE_OPEN_SOURCE, 0 },
65         { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 0 },
66         { "input-enable", PIN_CONFIG_INPUT_ENABLE, 1 },
67         { "input-disable", PIN_CONFIG_INPUT_ENABLE, 0 },
68 };
69
70 /* bitfield used to save param and value of each pin/selector */
71 static unsigned int sandbox_pins_param[ARRAY_SIZE(sandbox_pins)];
72 static unsigned int sandbox_pins_value[ARRAY_SIZE(sandbox_pins)];
73
74 static int sandbox_get_pins_count(struct udevice *dev)
75 {
76         return ARRAY_SIZE(sandbox_pins);
77 }
78
79 static const char *sandbox_get_pin_name(struct udevice *dev, unsigned selector)
80 {
81         return sandbox_pins[selector];
82 }
83
84 static int sandbox_get_pin_muxing(struct udevice *dev,
85                                   unsigned int selector,
86                                   char *buf, int size)
87 {
88         const struct pinconf_param *p;
89         int i;
90
91         snprintf(buf, size, "%s", sandbox_pins_muxing[selector]);
92
93         if (sandbox_pins_param[selector]) {
94                 for (i = 0, p = sandbox_conf_params;
95                      i < ARRAY_SIZE(sandbox_conf_params);
96                      i++, p++) {
97                         if ((sandbox_pins_param[selector] & BIT(p->param)) &&
98                             (!!(sandbox_pins_value[selector] & BIT(p->param)) ==
99                              p->default_value)) {
100                                 strncat(buf, " ", size);
101                                 strncat(buf, p->property, size);
102                         }
103                 }
104         }
105         strncat(buf, ".", size);
106
107         return 0;
108 }
109
110 static int sandbox_get_groups_count(struct udevice *dev)
111 {
112         return ARRAY_SIZE(sandbox_groups);
113 }
114
115 static const char *sandbox_get_group_name(struct udevice *dev,
116                                           unsigned selector)
117 {
118         return sandbox_groups[selector];
119 }
120
121 static int sandbox_get_functions_count(struct udevice *dev)
122 {
123         return ARRAY_SIZE(sandbox_functions);
124 }
125
126 static const char *sandbox_get_function_name(struct udevice *dev,
127                                              unsigned selector)
128 {
129         return sandbox_functions[selector];
130 }
131
132 static int sandbox_pinmux_set(struct udevice *dev, unsigned pin_selector,
133                               unsigned func_selector)
134 {
135         debug("sandbox pinmux: pin = %d (%s), function = %d (%s)\n",
136               pin_selector, sandbox_get_pin_name(dev, pin_selector),
137               func_selector, sandbox_get_function_name(dev, func_selector));
138
139         sandbox_pins_param[pin_selector] = 0;
140         sandbox_pins_value[pin_selector] = 0;
141
142         return 0;
143 }
144
145 static int sandbox_pinmux_group_set(struct udevice *dev,
146                                     unsigned group_selector,
147                                     unsigned func_selector)
148 {
149         debug("sandbox pinmux: group = %d (%s), function = %d (%s)\n",
150               group_selector, sandbox_get_group_name(dev, group_selector),
151               func_selector, sandbox_get_function_name(dev, func_selector));
152
153         return 0;
154 }
155
156 static int sandbox_pinconf_set(struct udevice *dev, unsigned pin_selector,
157                                unsigned param, unsigned argument)
158 {
159         debug("sandbox pinconf: pin = %d (%s), param = %d, arg = %d\n",
160               pin_selector, sandbox_get_pin_name(dev, pin_selector),
161               param, argument);
162
163         sandbox_pins_param[pin_selector] |= BIT(param);
164         if (argument)
165                 sandbox_pins_value[pin_selector] |= BIT(param);
166         else
167                 sandbox_pins_value[pin_selector] &= ~BIT(param);
168
169         return 0;
170 }
171
172 static int sandbox_pinconf_group_set(struct udevice *dev,
173                                      unsigned group_selector,
174                                      unsigned param, unsigned argument)
175 {
176         debug("sandbox pinconf: group = %d (%s), param = %d, arg = %d\n",
177               group_selector, sandbox_get_group_name(dev, group_selector),
178               param, argument);
179
180         return 0;
181 }
182
183 const struct pinctrl_ops sandbox_pinctrl_ops = {
184         .get_pins_count = sandbox_get_pins_count,
185         .get_pin_name = sandbox_get_pin_name,
186         .get_pin_muxing = sandbox_get_pin_muxing,
187         .get_groups_count = sandbox_get_groups_count,
188         .get_group_name = sandbox_get_group_name,
189         .get_functions_count = sandbox_get_functions_count,
190         .get_function_name = sandbox_get_function_name,
191         .pinmux_set = sandbox_pinmux_set,
192         .pinmux_group_set = sandbox_pinmux_group_set,
193         .pinconf_num_params = ARRAY_SIZE(sandbox_conf_params),
194         .pinconf_params = sandbox_conf_params,
195         .pinconf_set = sandbox_pinconf_set,
196         .pinconf_group_set = sandbox_pinconf_group_set,
197         .set_state = pinctrl_generic_set_state,
198 };
199
200 static const struct udevice_id sandbox_pinctrl_match[] = {
201         { .compatible = "sandbox,pinctrl" },
202         { /* sentinel */ }
203 };
204
205 U_BOOT_DRIVER(sandbox_pinctrl) = {
206         .name = "sandbox_pinctrl",
207         .id = UCLASS_PINCTRL,
208         .of_match = sandbox_pinctrl_match,
209         .ops = &sandbox_pinctrl_ops,
210 };