blob: fa871305baf1bd885eba075b9b07323c0bcbc2e3 [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>
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +010014#include <dm/device_compat.h>
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010015#include <dm/lists.h>
Simon Glass12faa022017-05-18 20:09:18 -060016#include <dm/of.h>
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010017#include <dm/pinctrl.h>
Simon Glass16e10402015-01-05 20:05:29 -070018#include <dt-bindings/gpio/gpio.h>
Patrick Delaunay23aee612020-01-13 11:35:13 +010019#include <dt-bindings/gpio/sandbox-gpio.h>
Simon Glassb43d0442012-02-15 15:51:13 -080020
Simon Glassb43d0442012-02-15 15:51:13 -080021
22struct gpio_state {
23 const char *label; /* label given by requester */
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010024 ulong dir_flags; /* dir_flags (GPIOD_...) */
Simon Glassb43d0442012-02-15 15:51:13 -080025};
26
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010027/* Access routines for GPIO dir flags */
28static ulong *get_gpio_dir_flags(struct udevice *dev, unsigned int 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) {
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010034 static ulong invalid_dir_flags;
Simon Glassb4d70702014-02-26 15:59:25 -070035 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010036 return &invalid_dir_flags;
Simon Glassb43d0442012-02-15 15:51:13 -080037 }
38
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010039 return &state[offset].dir_flags;
40
Simon Glassb43d0442012-02-15 15:51:13 -080041}
42
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010043static int get_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag)
Simon Glassb43d0442012-02-15 15:51:13 -080044{
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010045 return (*get_gpio_dir_flags(dev, offset) & flag) != 0;
Simon Glassb43d0442012-02-15 15:51:13 -080046}
47
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010048static int set_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag,
Simon Glassb4d70702014-02-26 15:59:25 -070049 int value)
Simon Glassb43d0442012-02-15 15:51:13 -080050{
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010051 ulong *gpio = get_gpio_dir_flags(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -080052
53 if (value)
54 *gpio |= flag;
55 else
56 *gpio &= ~flag;
57
58 return 0;
59}
60
Simon Glassb43d0442012-02-15 15:51:13 -080061/*
62 * Back-channel sandbox-internal-only access to GPIO state
63 */
64
Heiko Schocherb74fcb42014-05-22 12:43:05 +020065int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080066{
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010067 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
Simon Glassb4d70702014-02-26 15:59:25 -070068 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010069 return get_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE);
Simon Glassb43d0442012-02-15 15:51:13 -080070}
71
Heiko Schocherb74fcb42014-05-22 12:43:05 +020072int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -080073{
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010074 return set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE, value);
Simon Glassb43d0442012-02-15 15:51:13 -080075}
76
Heiko Schocherb74fcb42014-05-22 12:43:05 +020077int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080078{
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010079 return get_gpio_flag(dev, offset, GPIOD_IS_OUT);
Simon Glassb43d0442012-02-15 15:51:13 -080080}
81
Heiko Schocherb74fcb42014-05-22 12:43:05 +020082int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
Simon Glassb43d0442012-02-15 15:51:13 -080083{
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010084 set_gpio_flag(dev, offset, GPIOD_IS_OUT, output);
85 set_gpio_flag(dev, offset, GPIOD_IS_IN, !(output));
86
87 return 0;
88}
89
90ulong sandbox_gpio_get_dir_flags(struct udevice *dev, unsigned int offset)
91{
92 return *get_gpio_dir_flags(dev, offset);
93}
94
95int sandbox_gpio_set_dir_flags(struct udevice *dev, unsigned int offset,
96 ulong flags)
97{
98 *get_gpio_dir_flags(dev, offset) = flags;
99
100 return 0;
Simon Glassb43d0442012-02-15 15:51:13 -0800101}
102
103/*
104 * These functions implement the public interface within U-Boot
105 */
106
Simon Glassb4d70702014-02-26 15:59:25 -0700107/* set GPIO port 'offset' as an input */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200108static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800109{
Simon Glassb4d70702014-02-26 15:59:25 -0700110 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800111
Simon Glassb4d70702014-02-26 15:59:25 -0700112 return sandbox_gpio_set_direction(dev, offset, 0);
Simon Glassb43d0442012-02-15 15:51:13 -0800113}
114
Simon Glassb4d70702014-02-26 15:59:25 -0700115/* set GPIO port 'offset' as an output, with polarity 'value' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200116static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
Simon Glassb4d70702014-02-26 15:59:25 -0700117 int value)
Simon Glassb43d0442012-02-15 15:51:13 -0800118{
Simon Glassb4d70702014-02-26 15:59:25 -0700119 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800120
Simon Glassb4d70702014-02-26 15:59:25 -0700121 return sandbox_gpio_set_direction(dev, offset, 1) |
122 sandbox_gpio_set_value(dev, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800123}
124
Simon Glassb4d70702014-02-26 15:59:25 -0700125/* read GPIO IN value of port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200126static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800127{
Simon Glassb4d70702014-02-26 15:59:25 -0700128 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800129
Simon Glassb4d70702014-02-26 15:59:25 -0700130 return sandbox_gpio_get_value(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800131}
132
Simon Glassb4d70702014-02-26 15:59:25 -0700133/* write GPIO OUT value to port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200134static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -0800135{
Simon Glassb4d70702014-02-26 15:59:25 -0700136 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800137
Simon Glassb4d70702014-02-26 15:59:25 -0700138 if (!sandbox_gpio_get_direction(dev, offset)) {
139 printf("sandbox_gpio: error: set_value on input gpio %u\n",
140 offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800141 return -1;
142 }
143
Simon Glassb4d70702014-02-26 15:59:25 -0700144 return sandbox_gpio_set_value(dev, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800145}
146
Simon Glass32048632014-10-04 11:29:45 -0600147static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
148{
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100149 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
Simon Glass32048632014-10-04 11:29:45 -0600150 return GPIOF_OUTPUT;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100151 if (get_gpio_flag(dev, offset, GPIOD_IS_IN))
152 return GPIOF_INPUT;
153
154 return GPIOF_INPUT; /*GPIO is not configurated */
Simon Glass32048632014-10-04 11:29:45 -0600155}
156
Simon Glass16e10402015-01-05 20:05:29 -0700157static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600158 struct ofnode_phandle_args *args)
Simon Glass16e10402015-01-05 20:05:29 -0700159{
160 desc->offset = args->args[0];
161 if (args->args_count < 2)
162 return 0;
Patrick Delaunay23aee612020-01-13 11:35:13 +0100163 /* treat generic binding with gpio uclass */
164 gpio_xlate_offs_flags(dev, desc, args);
165
166 /* sandbox test specific, not defined in gpio.h */
167 if (args->args[1] & GPIO_IN)
Simon Glass16e10402015-01-05 20:05:29 -0700168 desc->flags |= GPIOD_IS_IN;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100169
Patrick Delaunay23aee612020-01-13 11:35:13 +0100170 if (args->args[1] & GPIO_OUT)
Simon Glass16e10402015-01-05 20:05:29 -0700171 desc->flags |= GPIOD_IS_OUT;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100172
Patrick Delaunay23aee612020-01-13 11:35:13 +0100173 if (args->args[1] & GPIO_OUT_ACTIVE)
Simon Glass16e10402015-01-05 20:05:29 -0700174 desc->flags |= GPIOD_IS_OUT_ACTIVE;
175
176 return 0;
177}
178
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100179static int sb_gpio_set_dir_flags(struct udevice *dev, unsigned int offset,
180 ulong flags)
181{
182 ulong *dir_flags;
183
184 debug("%s: offset:%u, dir_flags = %lx\n", __func__, offset, flags);
185
186 dir_flags = get_gpio_dir_flags(dev, offset);
187
Heinrich Schuchardt5355be62020-09-14 12:50:55 +0200188 /*
189 * For testing purposes keep the output value when switching to input.
190 * This allows us to manipulate the input value via the gpio command.
191 */
192 if (flags & GPIOD_IS_IN)
193 *dir_flags = (flags & ~GPIOD_IS_OUT_ACTIVE) |
194 (*dir_flags & GPIOD_IS_OUT_ACTIVE);
195 else
196 *dir_flags = flags;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100197
198 return 0;
199}
200
201static int sb_gpio_get_dir_flags(struct udevice *dev, unsigned int offset,
202 ulong *flags)
203{
204 debug("%s: offset:%u\n", __func__, offset);
205 *flags = *get_gpio_dir_flags(dev, offset);
206
207 return 0;
208}
209
Simon Glass3176b6c2020-07-07 13:11:44 -0600210#if CONFIG_IS_ENABLED(ACPIGEN)
211static int sb_gpio_get_acpi(const struct gpio_desc *desc,
212 struct acpi_gpio *gpio)
213{
214 int ret;
215
216 /* Note that gpio_get_acpi() zeroes *gpio before calling here */
217 gpio->pin_count = 1;
218 gpio->pins[0] = desc->offset;
219 ret = acpi_device_scope(desc->dev, gpio->resource,
220 sizeof(gpio->resource));
221 if (ret)
222 return log_ret(ret);
223
224 /* All of these values are just used for testing */
225 if (desc->flags & GPIOD_ACTIVE_LOW) {
226 gpio->pin0_addr = 0x80012 + desc->offset;
227 gpio->type = ACPI_GPIO_TYPE_INTERRUPT;
228 gpio->pull = ACPI_GPIO_PULL_DOWN;
229 gpio->interrupt_debounce_timeout = 4321;
230
231 /* We use the GpioInt part */
232 gpio->irq.pin = desc->offset;
233 gpio->irq.polarity = ACPI_IRQ_ACTIVE_BOTH;
234 gpio->irq.shared = ACPI_IRQ_SHARED;
235 gpio->irq.wake = ACPI_IRQ_WAKE;
236
237 /* The GpioIo part is only used for testing */
238 gpio->polarity = ACPI_GPIO_ACTIVE_LOW;
239 } else {
240 gpio->pin0_addr = 0xc00dc + desc->offset;
241 gpio->type = ACPI_GPIO_TYPE_IO;
242 gpio->pull = ACPI_GPIO_PULL_UP;
243 gpio->interrupt_debounce_timeout = 0;
244
245 /* The GpioInt part is not used */
246
247 /* We use the GpioIo part */
248 gpio->output_drive_strength = 1234;
249 gpio->io_shared = true;
250 gpio->io_restrict = ACPI_GPIO_IO_RESTRICT_INPUT;
251 gpio->polarity = 0;
252 }
253
254 return 0;
255}
256
257static int sb_gpio_get_name(const struct udevice *dev, char *out_name)
258{
259 return acpi_copy_name(out_name, "GPIO");
260}
261
262struct acpi_ops gpio_sandbox_acpi_ops = {
263 .get_name = sb_gpio_get_name,
264};
265#endif /* ACPIGEN */
266
Simon Glassb4d70702014-02-26 15:59:25 -0700267static const struct dm_gpio_ops gpio_sandbox_ops = {
Simon Glassb4d70702014-02-26 15:59:25 -0700268 .direction_input = sb_gpio_direction_input,
269 .direction_output = sb_gpio_direction_output,
270 .get_value = sb_gpio_get_value,
271 .set_value = sb_gpio_set_value,
Simon Glass32048632014-10-04 11:29:45 -0600272 .get_function = sb_gpio_get_function,
Simon Glass16e10402015-01-05 20:05:29 -0700273 .xlate = sb_gpio_xlate,
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100274 .set_dir_flags = sb_gpio_set_dir_flags,
275 .get_dir_flags = sb_gpio_get_dir_flags,
Simon Glass3176b6c2020-07-07 13:11:44 -0600276#if CONFIG_IS_ENABLED(ACPIGEN)
277 .get_acpi = sb_gpio_get_acpi,
278#endif
Simon Glassb4d70702014-02-26 15:59:25 -0700279};
280
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200281static int sandbox_gpio_ofdata_to_platdata(struct udevice *dev)
Simon Glassb43d0442012-02-15 15:51:13 -0800282{
Simon Glassde0977b2015-03-05 12:25:20 -0700283 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb43d0442012-02-15 15:51:13 -0800284
Simon Glass9e7ab232018-02-03 10:36:59 -0700285 uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count",
286 0);
Simon Glass1185fcb2017-05-18 20:09:20 -0600287 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
Simon Glassb43d0442012-02-15 15:51:13 -0800288
Simon Glassb4d70702014-02-26 15:59:25 -0700289 return 0;
290}
Simon Glassb43d0442012-02-15 15:51:13 -0800291
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200292static int gpio_sandbox_probe(struct udevice *dev)
Simon Glassb4d70702014-02-26 15:59:25 -0700293{
Simon Glassde0977b2015-03-05 12:25:20 -0700294 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb4d70702014-02-26 15:59:25 -0700295
Simon Glass1185fcb2017-05-18 20:09:20 -0600296 if (!dev_of_valid(dev))
Simon Glassb4d70702014-02-26 15:59:25 -0700297 /* Tell the uclass how many GPIOs we have */
298 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
Simon Glassb4d70702014-02-26 15:59:25 -0700299
300 dev->priv = calloc(sizeof(struct gpio_state), uc_priv->gpio_count);
301
302 return 0;
Simon Glassb43d0442012-02-15 15:51:13 -0800303}
Simon Glassb4d70702014-02-26 15:59:25 -0700304
Simon Glassa367f1d2014-10-04 11:29:46 -0600305static int gpio_sandbox_remove(struct udevice *dev)
306{
307 free(dev->priv);
308
309 return 0;
310}
311
Simon Glass767827a2014-06-11 23:29:45 -0600312static const struct udevice_id sandbox_gpio_ids[] = {
Simon Glassb4d70702014-02-26 15:59:25 -0700313 { .compatible = "sandbox,gpio" },
314 { }
315};
316
Walter Lozano2901ac62020-06-25 01:10:04 -0300317U_BOOT_DRIVER(sandbox_gpio) = {
318 .name = "sandbox_gpio",
Simon Glassb4d70702014-02-26 15:59:25 -0700319 .id = UCLASS_GPIO,
320 .of_match = sandbox_gpio_ids,
321 .ofdata_to_platdata = sandbox_gpio_ofdata_to_platdata,
322 .probe = gpio_sandbox_probe,
Simon Glassa367f1d2014-10-04 11:29:46 -0600323 .remove = gpio_sandbox_remove,
Simon Glassb4d70702014-02-26 15:59:25 -0700324 .ops = &gpio_sandbox_ops,
Simon Glass3176b6c2020-07-07 13:11:44 -0600325 ACPI_OPS_PTR(&gpio_sandbox_acpi_ops)
Simon Glassb4d70702014-02-26 15:59:25 -0700326};
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100327
Walter Lozano48e5b042020-06-25 01:10:06 -0300328U_BOOT_DRIVER_ALIAS(sandbox_gpio, sandbox_gpio_alias)
329
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100330/* pincontrol: used only to check GPIO pin configuration (pinmux command) */
331
332struct sb_pinctrl_priv {
333 int pinctrl_ngpios;
334 struct list_head gpio_dev;
335};
336
337struct sb_gpio_bank {
338 struct udevice *gpio_dev;
339 struct list_head list;
340};
341
342static int sb_populate_gpio_dev_list(struct udevice *dev)
343{
344 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
345 struct udevice *gpio_dev;
346 struct udevice *child;
347 struct sb_gpio_bank *gpio_bank;
348 int ret;
349
350 /*
351 * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
352 * a list with all gpio device reference which belongs to the
353 * current pin-controller. This list is used to find pin_name and
354 * pin muxing
355 */
356 list_for_each_entry(child, &dev->child_head, sibling_node) {
357 ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
358 &gpio_dev);
359 if (ret < 0)
360 continue;
361
362 gpio_bank = malloc(sizeof(*gpio_bank));
363 if (!gpio_bank) {
364 dev_err(dev, "Not enough memory\n");
365 return -ENOMEM;
366 }
367
368 gpio_bank->gpio_dev = gpio_dev;
369 list_add_tail(&gpio_bank->list, &priv->gpio_dev);
370 }
371
372 return 0;
373}
374
375static int sb_pinctrl_get_pins_count(struct udevice *dev)
376{
377 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
378 struct gpio_dev_priv *uc_priv;
379 struct sb_gpio_bank *gpio_bank;
380
381 /*
382 * if get_pins_count has already been executed once on this
383 * pin-controller, no need to run it again
384 */
385 if (priv->pinctrl_ngpios)
386 return priv->pinctrl_ngpios;
387
388 if (list_empty(&priv->gpio_dev))
389 sb_populate_gpio_dev_list(dev);
390 /*
391 * walk through all banks to retrieve the pin-controller
392 * pins number
393 */
394 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
395 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
396
397 priv->pinctrl_ngpios += uc_priv->gpio_count;
398 }
399
400 return priv->pinctrl_ngpios;
401}
402
403static struct udevice *sb_pinctrl_get_gpio_dev(struct udevice *dev,
404 unsigned int selector,
405 unsigned int *idx)
406{
407 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
408 struct sb_gpio_bank *gpio_bank;
409 struct gpio_dev_priv *uc_priv;
410 int pin_count = 0;
411
412 if (list_empty(&priv->gpio_dev))
413 sb_populate_gpio_dev_list(dev);
414
415 /* look up for the bank which owns the requested pin */
416 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
417 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
418
419 if (selector < (pin_count + uc_priv->gpio_count)) {
420 /*
421 * we found the bank, convert pin selector to
422 * gpio bank index
423 */
424 *idx = selector - pin_count;
425
426 return gpio_bank->gpio_dev;
427 }
428 pin_count += uc_priv->gpio_count;
429 }
430
431 return NULL;
432}
433
434static const char *sb_pinctrl_get_pin_name(struct udevice *dev,
435 unsigned int selector)
436{
437 struct gpio_dev_priv *uc_priv;
438 struct udevice *gpio_dev;
439 unsigned int gpio_idx;
440 static char pin_name[PINNAME_SIZE];
441
442 /* look up for the bank which owns the requested pin */
443 gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
444 if (!gpio_dev) {
445 snprintf(pin_name, PINNAME_SIZE, "Error");
446 } else {
447 uc_priv = dev_get_uclass_priv(gpio_dev);
448
449 snprintf(pin_name, PINNAME_SIZE, "%s%d",
450 uc_priv->bank_name,
451 gpio_idx);
452 }
453
454 return pin_name;
455}
456
457static char *get_dir_flags_string(ulong flags)
458{
459 if (flags & GPIOD_OPEN_DRAIN)
460 return "drive-open-drain";
461 if (flags & GPIOD_OPEN_SOURCE)
462 return "drive-open-source";
463 if (flags & GPIOD_PULL_UP)
464 return "bias-pull-up";
465 if (flags & GPIOD_PULL_DOWN)
466 return "bias-pull-down";
467 return ".";
468}
469
470static int sb_pinctrl_get_pin_muxing(struct udevice *dev,
471 unsigned int selector,
472 char *buf, int size)
473{
474 struct udevice *gpio_dev;
475 unsigned int gpio_idx;
476 ulong dir_flags;
477 int function;
478
479 /* look up for the bank which owns the requested pin */
480 gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
481 if (!gpio_dev) {
482 snprintf(buf, size, "Error");
483 } else {
484 function = sb_gpio_get_function(gpio_dev, gpio_idx);
485 dir_flags = *get_gpio_dir_flags(gpio_dev, gpio_idx);
486
487 snprintf(buf, size, "gpio %s %s",
488 function == GPIOF_OUTPUT ? "output" : "input",
489 get_dir_flags_string(dir_flags));
490 }
491
492 return 0;
493}
Simon Glass3176b6c2020-07-07 13:11:44 -0600494
495#if CONFIG_IS_ENABLED(ACPIGEN)
496static int sb_pinctrl_get_name(const struct udevice *dev, char *out_name)
497{
498 return acpi_copy_name(out_name, "PINC");
499}
500#endif
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100501
502static int sandbox_pinctrl_probe(struct udevice *dev)
503{
504 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
505
506 INIT_LIST_HEAD(&priv->gpio_dev);
507
508 return 0;
509}
510
511static struct pinctrl_ops sandbox_pinctrl_gpio_ops = {
512 .get_pin_name = sb_pinctrl_get_pin_name,
513 .get_pins_count = sb_pinctrl_get_pins_count,
514 .get_pin_muxing = sb_pinctrl_get_pin_muxing,
515};
Simon Glass3176b6c2020-07-07 13:11:44 -0600516
517#if CONFIG_IS_ENABLED(ACPIGEN)
518struct acpi_ops pinctrl_sandbox_acpi_ops = {
519 .get_name = sb_pinctrl_get_name,
520};
521#endif
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100522
523static const struct udevice_id sandbox_pinctrl_gpio_match[] = {
524 { .compatible = "sandbox,pinctrl-gpio" },
525 { /* sentinel */ }
526};
527
528U_BOOT_DRIVER(sandbox_pinctrl_gpio) = {
529 .name = "sandbox_pinctrl_gpio",
530 .id = UCLASS_PINCTRL,
531 .of_match = sandbox_pinctrl_gpio_match,
532 .ops = &sandbox_pinctrl_gpio_ops,
533 .bind = dm_scan_fdt_dev,
534 .probe = sandbox_pinctrl_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700535 .priv_auto = sizeof(struct sb_pinctrl_priv),
Simon Glass3176b6c2020-07-07 13:11:44 -0600536 ACPI_OPS_PTR(&pinctrl_sandbox_acpi_ops)
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100537};