blob: 106b2a7b27c25edec466a0649aa372b553731166 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassb43d0442012-02-15 15:51:13 -08002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glassb43d0442012-02-15 15:51:13 -08004 */
5
6#include <common.h>
Simon Glassb4d70702014-02-26 15:59:25 -07007#include <dm.h>
8#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glassb4d70702014-02-26 15:59:25 -070010#include <malloc.h>
Simon Glass3176b6c2020-07-07 13:11:44 -060011#include <acpi/acpi_device.h>
Simon Glassb43d0442012-02-15 15:51:13 -080012#include <asm/gpio.h>
Simon Glass3176b6c2020-07-07 13:11:44 -060013#include <dm/acpi.h>
Simon Glass95588622020-12-22 19:30:28 -070014#include <dm/device-internal.h>
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +010015#include <dm/device_compat.h>
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010016#include <dm/lists.h>
Simon Glass12faa022017-05-18 20:09:18 -060017#include <dm/of.h>
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010018#include <dm/pinctrl.h>
Simon Glass16e10402015-01-05 20:05:29 -070019#include <dt-bindings/gpio/gpio.h>
Patrick Delaunay23aee612020-01-13 11:35:13 +010020#include <dt-bindings/gpio/sandbox-gpio.h>
Simon Glassb43d0442012-02-15 15:51:13 -080021
Simon Glassb43d0442012-02-15 15:51:13 -080022struct gpio_state {
23 const char *label; /* label given by requester */
Simon Glass3f2d5752021-02-04 21:21:59 -070024 ulong flags; /* flags (GPIOD_...) */
Simon Glassb43d0442012-02-15 15:51:13 -080025};
26
Simon Glass3f2d5752021-02-04 21:21:59 -070027/* Access routines for GPIO info */
28static struct gpio_state *get_gpio_state(struct udevice *dev, uint offset)
Simon Glassb43d0442012-02-15 15:51:13 -080029{
Simon Glassde0977b2015-03-05 12:25:20 -070030 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb4d70702014-02-26 15:59:25 -070031 struct gpio_state *state = dev_get_priv(dev);
32
33 if (offset >= uc_priv->gpio_count) {
Simon Glassb4d70702014-02-26 15:59:25 -070034 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
Simon Glass3f2d5752021-02-04 21:21:59 -070035 return NULL;
Simon Glassb43d0442012-02-15 15:51:13 -080036 }
37
Simon Glass3f2d5752021-02-04 21:21:59 -070038 return &state[offset];
39}
40
41/* Access routines for GPIO flags */
42static ulong *get_gpio_flags(struct udevice *dev, unsigned int offset)
43{
44 struct gpio_state *state = get_gpio_state(dev, offset);
45
46 if (!state)
47 return NULL;
48
49 return &state->flags;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010050
Simon Glassb43d0442012-02-15 15:51:13 -080051}
52
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010053static int get_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag)
Simon Glassb43d0442012-02-15 15:51:13 -080054{
Simon Glass3f2d5752021-02-04 21:21:59 -070055 return (*get_gpio_flags(dev, offset) & flag) != 0;
Simon Glassb43d0442012-02-15 15:51:13 -080056}
57
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010058static int set_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag,
Simon Glassb4d70702014-02-26 15:59:25 -070059 int value)
Simon Glassb43d0442012-02-15 15:51:13 -080060{
Simon Glass44a1a5b2021-02-04 21:22:00 -070061 struct gpio_state *state = get_gpio_state(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -080062
63 if (value)
Simon Glass44a1a5b2021-02-04 21:22:00 -070064 state->flags |= flag;
Simon Glassb43d0442012-02-15 15:51:13 -080065 else
Simon Glass44a1a5b2021-02-04 21:22:00 -070066 state->flags &= ~flag;
Simon Glassb43d0442012-02-15 15:51:13 -080067
68 return 0;
69}
70
Simon Glassb43d0442012-02-15 15:51:13 -080071/*
72 * Back-channel sandbox-internal-only access to GPIO state
73 */
74
Heiko Schocherb74fcb42014-05-22 12:43:05 +020075int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080076{
Simon Glass44a1a5b2021-02-04 21:22:00 -070077 struct gpio_state *state = get_gpio_state(dev, offset);
Simon Glass59356522021-02-04 21:22:07 -070078 bool val;
Simon Glass44a1a5b2021-02-04 21:22:00 -070079
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010080 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
Simon Glassb4d70702014-02-26 15:59:25 -070081 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
Simon Glass44a1a5b2021-02-04 21:22:00 -070082
Simon Glass247ccf22021-02-04 21:22:09 -070083 if (state->flags & GPIOD_EXT_DRIVEN) {
Simon Glass59356522021-02-04 21:22:07 -070084 val = state->flags & GPIOD_EXT_HIGH;
Simon Glass247ccf22021-02-04 21:22:09 -070085 } else {
86 if (state->flags & GPIOD_EXT_PULL_UP)
87 val = true;
88 else if (state->flags & GPIOD_EXT_PULL_DOWN)
89 val = false;
90 else
91 val = state->flags & GPIOD_PULL_UP;
92 }
Simon Glass59356522021-02-04 21:22:07 -070093
94 return val;
Simon Glassb43d0442012-02-15 15:51:13 -080095}
96
Heiko Schocherb74fcb42014-05-22 12:43:05 +020097int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -080098{
Simon Glass59356522021-02-04 21:22:07 -070099 set_gpio_flag(dev, offset, GPIOD_EXT_DRIVEN | GPIOD_EXT_HIGH, value);
Simon Glass44a1a5b2021-02-04 21:22:00 -0700100
101 return 0;
Simon Glassb43d0442012-02-15 15:51:13 -0800102}
103
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200104int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800105{
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100106 return get_gpio_flag(dev, offset, GPIOD_IS_OUT);
Simon Glassb43d0442012-02-15 15:51:13 -0800107}
108
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200109int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
Simon Glassb43d0442012-02-15 15:51:13 -0800110{
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100111 set_gpio_flag(dev, offset, GPIOD_IS_OUT, output);
Simon Glass44a1a5b2021-02-04 21:22:00 -0700112 set_gpio_flag(dev, offset, GPIOD_IS_IN, !output);
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100113
114 return 0;
115}
116
Simon Glass3f2d5752021-02-04 21:21:59 -0700117ulong sandbox_gpio_get_flags(struct udevice *dev, uint offset)
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100118{
Simon Glass44a1a5b2021-02-04 21:22:00 -0700119 ulong flags = *get_gpio_flags(dev, offset);
120
121 return flags & ~GPIOD_SANDBOX_MASK;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100122}
123
Simon Glass3f2d5752021-02-04 21:21:59 -0700124int sandbox_gpio_set_flags(struct udevice *dev, uint offset, ulong flags)
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100125{
Simon Glass44a1a5b2021-02-04 21:22:00 -0700126 struct gpio_state *state = get_gpio_state(dev, offset);
127
Simon Glass4d3b73e2021-02-04 21:22:02 -0700128 state->flags = flags;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100129
130 return 0;
Simon Glassb43d0442012-02-15 15:51:13 -0800131}
132
133/*
134 * These functions implement the public interface within U-Boot
135 */
136
Simon Glassb4d70702014-02-26 15:59:25 -0700137/* set GPIO port 'offset' as an input */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200138static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800139{
Simon Glassb4d70702014-02-26 15:59:25 -0700140 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800141
Simon Glassb4d70702014-02-26 15:59:25 -0700142 return sandbox_gpio_set_direction(dev, offset, 0);
Simon Glassb43d0442012-02-15 15:51:13 -0800143}
144
Simon Glassb4d70702014-02-26 15:59:25 -0700145/* set GPIO port 'offset' as an output, with polarity 'value' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200146static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
Simon Glassb4d70702014-02-26 15:59:25 -0700147 int value)
Simon Glassb43d0442012-02-15 15:51:13 -0800148{
Simon Glassae1e85b2021-02-04 21:22:01 -0700149 int ret;
150
Simon Glassb4d70702014-02-26 15:59:25 -0700151 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800152
Simon Glassae1e85b2021-02-04 21:22:01 -0700153 ret = sandbox_gpio_set_direction(dev, offset, 1);
154 if (ret)
155 return ret;
Simon Glass59356522021-02-04 21:22:07 -0700156 ret = set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE |
157 GPIOD_EXT_DRIVEN | GPIOD_EXT_HIGH, value);
Simon Glassae1e85b2021-02-04 21:22:01 -0700158 if (ret)
159 return ret;
160
161 return 0;
Simon Glassb43d0442012-02-15 15:51:13 -0800162}
163
Simon Glassb4d70702014-02-26 15:59:25 -0700164/* read GPIO IN value of port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200165static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800166{
Simon Glassb4d70702014-02-26 15:59:25 -0700167 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800168
Simon Glassb4d70702014-02-26 15:59:25 -0700169 return sandbox_gpio_get_value(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800170}
171
Simon Glassb4d70702014-02-26 15:59:25 -0700172/* write GPIO OUT value to port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200173static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -0800174{
Simon Glassae1e85b2021-02-04 21:22:01 -0700175 int ret;
176
Simon Glassb4d70702014-02-26 15:59:25 -0700177 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800178
Simon Glassb4d70702014-02-26 15:59:25 -0700179 if (!sandbox_gpio_get_direction(dev, offset)) {
180 printf("sandbox_gpio: error: set_value on input gpio %u\n",
181 offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800182 return -1;
183 }
184
Simon Glass59356522021-02-04 21:22:07 -0700185 ret = set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE |
186 GPIOD_EXT_DRIVEN | GPIOD_EXT_HIGH, value);
Simon Glassae1e85b2021-02-04 21:22:01 -0700187 if (ret)
188 return ret;
189
190 return 0;
Simon Glassb43d0442012-02-15 15:51:13 -0800191}
192
Simon Glass32048632014-10-04 11:29:45 -0600193static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
194{
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100195 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
Simon Glass32048632014-10-04 11:29:45 -0600196 return GPIOF_OUTPUT;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100197 if (get_gpio_flag(dev, offset, GPIOD_IS_IN))
198 return GPIOF_INPUT;
199
200 return GPIOF_INPUT; /*GPIO is not configurated */
Simon Glass32048632014-10-04 11:29:45 -0600201}
202
Simon Glass16e10402015-01-05 20:05:29 -0700203static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600204 struct ofnode_phandle_args *args)
Simon Glass16e10402015-01-05 20:05:29 -0700205{
206 desc->offset = args->args[0];
207 if (args->args_count < 2)
208 return 0;
Patrick Delaunay23aee612020-01-13 11:35:13 +0100209 /* treat generic binding with gpio uclass */
210 gpio_xlate_offs_flags(dev, desc, args);
211
212 /* sandbox test specific, not defined in gpio.h */
213 if (args->args[1] & GPIO_IN)
Simon Glass16e10402015-01-05 20:05:29 -0700214 desc->flags |= GPIOD_IS_IN;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100215
Patrick Delaunay23aee612020-01-13 11:35:13 +0100216 if (args->args[1] & GPIO_OUT)
Simon Glass16e10402015-01-05 20:05:29 -0700217 desc->flags |= GPIOD_IS_OUT;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100218
Patrick Delaunay23aee612020-01-13 11:35:13 +0100219 if (args->args[1] & GPIO_OUT_ACTIVE)
Simon Glass16e10402015-01-05 20:05:29 -0700220 desc->flags |= GPIOD_IS_OUT_ACTIVE;
221
222 return 0;
223}
224
Simon Glass54befdd2021-02-04 21:21:55 -0700225static int sb_gpio_set_flags(struct udevice *dev, unsigned int offset,
226 ulong flags)
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100227{
Simon Glass3f2d5752021-02-04 21:21:59 -0700228 debug("%s: offset:%u, flags = %lx\n", __func__, offset, flags);
Simon Glass4d3b73e2021-02-04 21:22:02 -0700229 struct gpio_state *state = get_gpio_state(dev, offset);
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100230
Simon Glass4d3b73e2021-02-04 21:22:02 -0700231 if (flags & GPIOD_IS_OUT) {
Simon Glass59356522021-02-04 21:22:07 -0700232 flags |= GPIOD_EXT_DRIVEN;
Simon Glass4d3b73e2021-02-04 21:22:02 -0700233 if (flags & GPIOD_IS_OUT_ACTIVE)
234 flags |= GPIOD_EXT_HIGH;
235 else
236 flags &= ~GPIOD_EXT_HIGH;
Simon Glass59356522021-02-04 21:22:07 -0700237 } else {
238 flags |= state->flags & GPIOD_SANDBOX_MASK;
Simon Glass4d3b73e2021-02-04 21:22:02 -0700239 }
240 state->flags = flags;
241
242 return 0;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100243}
244
Simon Glass3f2d5752021-02-04 21:21:59 -0700245static int sb_gpio_get_flags(struct udevice *dev, uint offset, ulong *flagsp)
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100246{
247 debug("%s: offset:%u\n", __func__, offset);
Simon Glass4d3b73e2021-02-04 21:22:02 -0700248 *flagsp = *get_gpio_flags(dev, offset) & ~GPIOD_SANDBOX_MASK;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100249
250 return 0;
251}
252
Simon Glass3176b6c2020-07-07 13:11:44 -0600253#if CONFIG_IS_ENABLED(ACPIGEN)
254static int sb_gpio_get_acpi(const struct gpio_desc *desc,
255 struct acpi_gpio *gpio)
256{
257 int ret;
258
259 /* Note that gpio_get_acpi() zeroes *gpio before calling here */
260 gpio->pin_count = 1;
261 gpio->pins[0] = desc->offset;
262 ret = acpi_device_scope(desc->dev, gpio->resource,
263 sizeof(gpio->resource));
264 if (ret)
265 return log_ret(ret);
266
267 /* All of these values are just used for testing */
268 if (desc->flags & GPIOD_ACTIVE_LOW) {
269 gpio->pin0_addr = 0x80012 + desc->offset;
270 gpio->type = ACPI_GPIO_TYPE_INTERRUPT;
271 gpio->pull = ACPI_GPIO_PULL_DOWN;
272 gpio->interrupt_debounce_timeout = 4321;
273
274 /* We use the GpioInt part */
275 gpio->irq.pin = desc->offset;
276 gpio->irq.polarity = ACPI_IRQ_ACTIVE_BOTH;
277 gpio->irq.shared = ACPI_IRQ_SHARED;
278 gpio->irq.wake = ACPI_IRQ_WAKE;
279
280 /* The GpioIo part is only used for testing */
281 gpio->polarity = ACPI_GPIO_ACTIVE_LOW;
282 } else {
283 gpio->pin0_addr = 0xc00dc + desc->offset;
284 gpio->type = ACPI_GPIO_TYPE_IO;
285 gpio->pull = ACPI_GPIO_PULL_UP;
286 gpio->interrupt_debounce_timeout = 0;
287
288 /* The GpioInt part is not used */
289
290 /* We use the GpioIo part */
291 gpio->output_drive_strength = 1234;
292 gpio->io_shared = true;
293 gpio->io_restrict = ACPI_GPIO_IO_RESTRICT_INPUT;
294 gpio->polarity = 0;
295 }
296
297 return 0;
298}
299
300static int sb_gpio_get_name(const struct udevice *dev, char *out_name)
301{
302 return acpi_copy_name(out_name, "GPIO");
303}
304
305struct acpi_ops gpio_sandbox_acpi_ops = {
306 .get_name = sb_gpio_get_name,
307};
308#endif /* ACPIGEN */
309
Simon Glassb4d70702014-02-26 15:59:25 -0700310static const struct dm_gpio_ops gpio_sandbox_ops = {
Simon Glassb4d70702014-02-26 15:59:25 -0700311 .direction_input = sb_gpio_direction_input,
312 .direction_output = sb_gpio_direction_output,
313 .get_value = sb_gpio_get_value,
314 .set_value = sb_gpio_set_value,
Simon Glass32048632014-10-04 11:29:45 -0600315 .get_function = sb_gpio_get_function,
Simon Glass16e10402015-01-05 20:05:29 -0700316 .xlate = sb_gpio_xlate,
Simon Glass54befdd2021-02-04 21:21:55 -0700317 .set_flags = sb_gpio_set_flags,
Simon Glassd063ce92021-02-04 21:21:56 -0700318 .get_flags = sb_gpio_get_flags,
Simon Glass3176b6c2020-07-07 13:11:44 -0600319#if CONFIG_IS_ENABLED(ACPIGEN)
320 .get_acpi = sb_gpio_get_acpi,
321#endif
Simon Glassb4d70702014-02-26 15:59:25 -0700322};
323
Simon Glassaad29ae2020-12-03 16:55:21 -0700324static int sandbox_gpio_of_to_plat(struct udevice *dev)
Simon Glassb43d0442012-02-15 15:51:13 -0800325{
Simon Glass2149e112021-08-07 07:24:12 -0600326 if (CONFIG_IS_ENABLED(OF_REAL)) {
327 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb43d0442012-02-15 15:51:13 -0800328
Simon Glass2149e112021-08-07 07:24:12 -0600329 uc_priv->gpio_count =
330 dev_read_u32_default(dev, "sandbox,gpio-count", 0);
331 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
332 }
Simon Glassb43d0442012-02-15 15:51:13 -0800333
Simon Glassb4d70702014-02-26 15:59:25 -0700334 return 0;
335}
Simon Glassb43d0442012-02-15 15:51:13 -0800336
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200337static int gpio_sandbox_probe(struct udevice *dev)
Simon Glassb4d70702014-02-26 15:59:25 -0700338{
Simon Glassde0977b2015-03-05 12:25:20 -0700339 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb4d70702014-02-26 15:59:25 -0700340
Simon Glassf1d50f72020-12-19 10:40:13 -0700341 if (!dev_has_ofnode(dev))
Simon Glassb4d70702014-02-26 15:59:25 -0700342 /* Tell the uclass how many GPIOs we have */
343 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
Simon Glassb4d70702014-02-26 15:59:25 -0700344
Simon Glass95588622020-12-22 19:30:28 -0700345 dev_set_priv(dev,
346 calloc(sizeof(struct gpio_state), uc_priv->gpio_count));
Simon Glassb4d70702014-02-26 15:59:25 -0700347
348 return 0;
Simon Glassb43d0442012-02-15 15:51:13 -0800349}
Simon Glassb4d70702014-02-26 15:59:25 -0700350
Simon Glassa367f1d2014-10-04 11:29:46 -0600351static int gpio_sandbox_remove(struct udevice *dev)
352{
Simon Glass95588622020-12-22 19:30:28 -0700353 free(dev_get_priv(dev));
Simon Glassa367f1d2014-10-04 11:29:46 -0600354
355 return 0;
356}
357
Simon Glass767827a2014-06-11 23:29:45 -0600358static const struct udevice_id sandbox_gpio_ids[] = {
Simon Glassb4d70702014-02-26 15:59:25 -0700359 { .compatible = "sandbox,gpio" },
360 { }
361};
362
Walter Lozano2901ac62020-06-25 01:10:04 -0300363U_BOOT_DRIVER(sandbox_gpio) = {
364 .name = "sandbox_gpio",
Simon Glassb4d70702014-02-26 15:59:25 -0700365 .id = UCLASS_GPIO,
366 .of_match = sandbox_gpio_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700367 .of_to_plat = sandbox_gpio_of_to_plat,
Simon Glassb4d70702014-02-26 15:59:25 -0700368 .probe = gpio_sandbox_probe,
Simon Glassa367f1d2014-10-04 11:29:46 -0600369 .remove = gpio_sandbox_remove,
Simon Glassb4d70702014-02-26 15:59:25 -0700370 .ops = &gpio_sandbox_ops,
Simon Glass3176b6c2020-07-07 13:11:44 -0600371 ACPI_OPS_PTR(&gpio_sandbox_acpi_ops)
Simon Glassb4d70702014-02-26 15:59:25 -0700372};
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100373
Simon Glassdf65db82020-12-28 20:34:57 -0700374DM_DRIVER_ALIAS(sandbox_gpio, sandbox_gpio_alias)
Walter Lozano48e5b042020-06-25 01:10:06 -0300375
Simon Glass2149e112021-08-07 07:24:12 -0600376#if CONFIG_IS_ENABLED(PINCTRL)
377
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100378/* pincontrol: used only to check GPIO pin configuration (pinmux command) */
379
380struct sb_pinctrl_priv {
381 int pinctrl_ngpios;
382 struct list_head gpio_dev;
383};
384
385struct sb_gpio_bank {
386 struct udevice *gpio_dev;
387 struct list_head list;
388};
389
390static int sb_populate_gpio_dev_list(struct udevice *dev)
391{
392 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
393 struct udevice *gpio_dev;
394 struct udevice *child;
395 struct sb_gpio_bank *gpio_bank;
396 int ret;
397
398 /*
399 * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
400 * a list with all gpio device reference which belongs to the
401 * current pin-controller. This list is used to find pin_name and
402 * pin muxing
403 */
404 list_for_each_entry(child, &dev->child_head, sibling_node) {
405 ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
406 &gpio_dev);
407 if (ret < 0)
408 continue;
409
410 gpio_bank = malloc(sizeof(*gpio_bank));
411 if (!gpio_bank) {
412 dev_err(dev, "Not enough memory\n");
413 return -ENOMEM;
414 }
415
416 gpio_bank->gpio_dev = gpio_dev;
417 list_add_tail(&gpio_bank->list, &priv->gpio_dev);
418 }
419
420 return 0;
421}
422
423static int sb_pinctrl_get_pins_count(struct udevice *dev)
424{
425 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
426 struct gpio_dev_priv *uc_priv;
427 struct sb_gpio_bank *gpio_bank;
428
429 /*
430 * if get_pins_count has already been executed once on this
431 * pin-controller, no need to run it again
432 */
433 if (priv->pinctrl_ngpios)
434 return priv->pinctrl_ngpios;
435
436 if (list_empty(&priv->gpio_dev))
437 sb_populate_gpio_dev_list(dev);
438 /*
439 * walk through all banks to retrieve the pin-controller
440 * pins number
441 */
442 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
443 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
444
445 priv->pinctrl_ngpios += uc_priv->gpio_count;
446 }
447
448 return priv->pinctrl_ngpios;
449}
450
451static struct udevice *sb_pinctrl_get_gpio_dev(struct udevice *dev,
452 unsigned int selector,
453 unsigned int *idx)
454{
455 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
456 struct sb_gpio_bank *gpio_bank;
457 struct gpio_dev_priv *uc_priv;
458 int pin_count = 0;
459
460 if (list_empty(&priv->gpio_dev))
461 sb_populate_gpio_dev_list(dev);
462
463 /* look up for the bank which owns the requested pin */
464 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
465 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
466
467 if (selector < (pin_count + uc_priv->gpio_count)) {
468 /*
469 * we found the bank, convert pin selector to
470 * gpio bank index
471 */
472 *idx = selector - pin_count;
473
474 return gpio_bank->gpio_dev;
475 }
476 pin_count += uc_priv->gpio_count;
477 }
478
479 return NULL;
480}
481
482static const char *sb_pinctrl_get_pin_name(struct udevice *dev,
483 unsigned int selector)
484{
485 struct gpio_dev_priv *uc_priv;
486 struct udevice *gpio_dev;
487 unsigned int gpio_idx;
488 static char pin_name[PINNAME_SIZE];
489
490 /* look up for the bank which owns the requested pin */
491 gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
492 if (!gpio_dev) {
493 snprintf(pin_name, PINNAME_SIZE, "Error");
494 } else {
495 uc_priv = dev_get_uclass_priv(gpio_dev);
496
497 snprintf(pin_name, PINNAME_SIZE, "%s%d",
498 uc_priv->bank_name,
499 gpio_idx);
500 }
501
502 return pin_name;
503}
504
Simon Glass3f2d5752021-02-04 21:21:59 -0700505static char *get_flags_string(ulong flags)
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100506{
507 if (flags & GPIOD_OPEN_DRAIN)
508 return "drive-open-drain";
509 if (flags & GPIOD_OPEN_SOURCE)
510 return "drive-open-source";
511 if (flags & GPIOD_PULL_UP)
512 return "bias-pull-up";
513 if (flags & GPIOD_PULL_DOWN)
514 return "bias-pull-down";
515 return ".";
516}
517
518static int sb_pinctrl_get_pin_muxing(struct udevice *dev,
519 unsigned int selector,
520 char *buf, int size)
521{
522 struct udevice *gpio_dev;
523 unsigned int gpio_idx;
Simon Glass3f2d5752021-02-04 21:21:59 -0700524 ulong flags;
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100525 int function;
526
527 /* look up for the bank which owns the requested pin */
528 gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
529 if (!gpio_dev) {
530 snprintf(buf, size, "Error");
531 } else {
532 function = sb_gpio_get_function(gpio_dev, gpio_idx);
Simon Glass3f2d5752021-02-04 21:21:59 -0700533 flags = *get_gpio_flags(gpio_dev, gpio_idx);
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100534
535 snprintf(buf, size, "gpio %s %s",
536 function == GPIOF_OUTPUT ? "output" : "input",
Simon Glass3f2d5752021-02-04 21:21:59 -0700537 get_flags_string(flags));
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100538 }
539
540 return 0;
541}
Simon Glass3176b6c2020-07-07 13:11:44 -0600542
543#if CONFIG_IS_ENABLED(ACPIGEN)
544static int sb_pinctrl_get_name(const struct udevice *dev, char *out_name)
545{
546 return acpi_copy_name(out_name, "PINC");
547}
548#endif
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100549
550static int sandbox_pinctrl_probe(struct udevice *dev)
551{
552 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
553
554 INIT_LIST_HEAD(&priv->gpio_dev);
555
556 return 0;
557}
558
559static struct pinctrl_ops sandbox_pinctrl_gpio_ops = {
560 .get_pin_name = sb_pinctrl_get_pin_name,
561 .get_pins_count = sb_pinctrl_get_pins_count,
562 .get_pin_muxing = sb_pinctrl_get_pin_muxing,
563};
Simon Glass3176b6c2020-07-07 13:11:44 -0600564
565#if CONFIG_IS_ENABLED(ACPIGEN)
566struct acpi_ops pinctrl_sandbox_acpi_ops = {
567 .get_name = sb_pinctrl_get_name,
568};
569#endif
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100570
571static const struct udevice_id sandbox_pinctrl_gpio_match[] = {
572 { .compatible = "sandbox,pinctrl-gpio" },
573 { /* sentinel */ }
574};
575
576U_BOOT_DRIVER(sandbox_pinctrl_gpio) = {
577 .name = "sandbox_pinctrl_gpio",
578 .id = UCLASS_PINCTRL,
579 .of_match = sandbox_pinctrl_gpio_match,
580 .ops = &sandbox_pinctrl_gpio_ops,
581 .bind = dm_scan_fdt_dev,
582 .probe = sandbox_pinctrl_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700583 .priv_auto = sizeof(struct sb_pinctrl_priv),
Simon Glass3176b6c2020-07-07 13:11:44 -0600584 ACPI_OPS_PTR(&pinctrl_sandbox_acpi_ops)
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100585};
Simon Glass2149e112021-08-07 07:24:12 -0600586
587#endif /* PINCTRL */