blob: 6f2eed50bf1d7646f070a1014d5b4d987a1cb73a [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 -080022
23struct gpio_state {
24 const char *label; /* label given by requester */
Simon Glass3f2d5752021-02-04 21:21:59 -070025 ulong flags; /* flags (GPIOD_...) */
Simon Glassb43d0442012-02-15 15:51:13 -080026};
27
Simon Glass3f2d5752021-02-04 21:21:59 -070028/* Access routines for GPIO info */
29static struct gpio_state *get_gpio_state(struct udevice *dev, uint offset)
Simon Glassb43d0442012-02-15 15:51:13 -080030{
Simon Glassde0977b2015-03-05 12:25:20 -070031 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb4d70702014-02-26 15:59:25 -070032 struct gpio_state *state = dev_get_priv(dev);
33
34 if (offset >= uc_priv->gpio_count) {
Simon Glassb4d70702014-02-26 15:59:25 -070035 printf("sandbox_gpio: error: invalid gpio %u\n", offset);
Simon Glass3f2d5752021-02-04 21:21:59 -070036 return NULL;
Simon Glassb43d0442012-02-15 15:51:13 -080037 }
38
Simon Glass3f2d5752021-02-04 21:21:59 -070039 return &state[offset];
40}
41
42/* Access routines for GPIO flags */
43static ulong *get_gpio_flags(struct udevice *dev, unsigned int offset)
44{
45 struct gpio_state *state = get_gpio_state(dev, offset);
46
47 if (!state)
48 return NULL;
49
50 return &state->flags;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010051
Simon Glassb43d0442012-02-15 15:51:13 -080052}
53
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010054static int get_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag)
Simon Glassb43d0442012-02-15 15:51:13 -080055{
Simon Glass3f2d5752021-02-04 21:21:59 -070056 return (*get_gpio_flags(dev, offset) & flag) != 0;
Simon Glassb43d0442012-02-15 15:51:13 -080057}
58
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010059static int set_gpio_flag(struct udevice *dev, unsigned int offset, ulong flag,
Simon Glassb4d70702014-02-26 15:59:25 -070060 int value)
Simon Glassb43d0442012-02-15 15:51:13 -080061{
Simon Glass3f2d5752021-02-04 21:21:59 -070062 ulong *gpio = get_gpio_flags(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -080063
64 if (value)
65 *gpio |= flag;
66 else
67 *gpio &= ~flag;
68
69 return 0;
70}
71
Simon Glassb43d0442012-02-15 15:51:13 -080072/*
73 * Back-channel sandbox-internal-only access to GPIO state
74 */
75
Heiko Schocherb74fcb42014-05-22 12:43:05 +020076int sandbox_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080077{
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010078 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
Simon Glassb4d70702014-02-26 15:59:25 -070079 debug("sandbox_gpio: get_value on output gpio %u\n", offset);
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010080 return get_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE);
Simon Glassb43d0442012-02-15 15:51:13 -080081}
82
Heiko Schocherb74fcb42014-05-22 12:43:05 +020083int sandbox_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -080084{
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010085 return set_gpio_flag(dev, offset, GPIOD_IS_OUT_ACTIVE, value);
Simon Glassb43d0442012-02-15 15:51:13 -080086}
87
Heiko Schocherb74fcb42014-05-22 12:43:05 +020088int sandbox_gpio_get_direction(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -080089{
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010090 return get_gpio_flag(dev, offset, GPIOD_IS_OUT);
Simon Glassb43d0442012-02-15 15:51:13 -080091}
92
Heiko Schocherb74fcb42014-05-22 12:43:05 +020093int sandbox_gpio_set_direction(struct udevice *dev, unsigned offset, int output)
Simon Glassb43d0442012-02-15 15:51:13 -080094{
Patrick Delaunay28bdaa52020-01-13 11:35:14 +010095 set_gpio_flag(dev, offset, GPIOD_IS_OUT, output);
96 set_gpio_flag(dev, offset, GPIOD_IS_IN, !(output));
97
98 return 0;
99}
100
Simon Glass3f2d5752021-02-04 21:21:59 -0700101ulong sandbox_gpio_get_flags(struct udevice *dev, uint offset)
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100102{
Simon Glass3f2d5752021-02-04 21:21:59 -0700103 return *get_gpio_flags(dev, offset);
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100104}
105
Simon Glass3f2d5752021-02-04 21:21:59 -0700106int sandbox_gpio_set_flags(struct udevice *dev, uint offset, ulong flags)
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100107{
Simon Glass3f2d5752021-02-04 21:21:59 -0700108 *get_gpio_flags(dev, offset) = flags;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100109
110 return 0;
Simon Glassb43d0442012-02-15 15:51:13 -0800111}
112
113/*
114 * These functions implement the public interface within U-Boot
115 */
116
Simon Glassb4d70702014-02-26 15:59:25 -0700117/* set GPIO port 'offset' as an input */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200118static int sb_gpio_direction_input(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800119{
Simon Glassb4d70702014-02-26 15:59:25 -0700120 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800121
Simon Glassb4d70702014-02-26 15:59:25 -0700122 return sandbox_gpio_set_direction(dev, offset, 0);
Simon Glassb43d0442012-02-15 15:51:13 -0800123}
124
Simon Glassb4d70702014-02-26 15:59:25 -0700125/* set GPIO port 'offset' as an output, with polarity 'value' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200126static int sb_gpio_direction_output(struct udevice *dev, unsigned offset,
Simon Glassb4d70702014-02-26 15:59:25 -0700127 int value)
Simon Glassb43d0442012-02-15 15:51:13 -0800128{
Simon Glassb4d70702014-02-26 15:59:25 -0700129 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800130
Simon Glassb4d70702014-02-26 15:59:25 -0700131 return sandbox_gpio_set_direction(dev, offset, 1) |
132 sandbox_gpio_set_value(dev, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800133}
134
Simon Glassb4d70702014-02-26 15:59:25 -0700135/* read GPIO IN value of port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200136static int sb_gpio_get_value(struct udevice *dev, unsigned offset)
Simon Glassb43d0442012-02-15 15:51:13 -0800137{
Simon Glassb4d70702014-02-26 15:59:25 -0700138 debug("%s: offset:%u\n", __func__, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800139
Simon Glassb4d70702014-02-26 15:59:25 -0700140 return sandbox_gpio_get_value(dev, offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800141}
142
Simon Glassb4d70702014-02-26 15:59:25 -0700143/* write GPIO OUT value to port 'offset' */
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200144static int sb_gpio_set_value(struct udevice *dev, unsigned offset, int value)
Simon Glassb43d0442012-02-15 15:51:13 -0800145{
Simon Glassb4d70702014-02-26 15:59:25 -0700146 debug("%s: offset:%u, value = %d\n", __func__, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800147
Simon Glassb4d70702014-02-26 15:59:25 -0700148 if (!sandbox_gpio_get_direction(dev, offset)) {
149 printf("sandbox_gpio: error: set_value on input gpio %u\n",
150 offset);
Simon Glassb43d0442012-02-15 15:51:13 -0800151 return -1;
152 }
153
Simon Glassb4d70702014-02-26 15:59:25 -0700154 return sandbox_gpio_set_value(dev, offset, value);
Simon Glassb43d0442012-02-15 15:51:13 -0800155}
156
Simon Glass32048632014-10-04 11:29:45 -0600157static int sb_gpio_get_function(struct udevice *dev, unsigned offset)
158{
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100159 if (get_gpio_flag(dev, offset, GPIOD_IS_OUT))
Simon Glass32048632014-10-04 11:29:45 -0600160 return GPIOF_OUTPUT;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100161 if (get_gpio_flag(dev, offset, GPIOD_IS_IN))
162 return GPIOF_INPUT;
163
164 return GPIOF_INPUT; /*GPIO is not configurated */
Simon Glass32048632014-10-04 11:29:45 -0600165}
166
Simon Glass16e10402015-01-05 20:05:29 -0700167static int sb_gpio_xlate(struct udevice *dev, struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600168 struct ofnode_phandle_args *args)
Simon Glass16e10402015-01-05 20:05:29 -0700169{
170 desc->offset = args->args[0];
171 if (args->args_count < 2)
172 return 0;
Patrick Delaunay23aee612020-01-13 11:35:13 +0100173 /* treat generic binding with gpio uclass */
174 gpio_xlate_offs_flags(dev, desc, args);
175
176 /* sandbox test specific, not defined in gpio.h */
177 if (args->args[1] & GPIO_IN)
Simon Glass16e10402015-01-05 20:05:29 -0700178 desc->flags |= GPIOD_IS_IN;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100179
Patrick Delaunay23aee612020-01-13 11:35:13 +0100180 if (args->args[1] & GPIO_OUT)
Simon Glass16e10402015-01-05 20:05:29 -0700181 desc->flags |= GPIOD_IS_OUT;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100182
Patrick Delaunay23aee612020-01-13 11:35:13 +0100183 if (args->args[1] & GPIO_OUT_ACTIVE)
Simon Glass16e10402015-01-05 20:05:29 -0700184 desc->flags |= GPIOD_IS_OUT_ACTIVE;
185
186 return 0;
187}
188
Simon Glass54befdd2021-02-04 21:21:55 -0700189static int sb_gpio_set_flags(struct udevice *dev, unsigned int offset,
190 ulong flags)
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100191{
Simon Glass3f2d5752021-02-04 21:21:59 -0700192 ulong *newf;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100193
Simon Glass3f2d5752021-02-04 21:21:59 -0700194 debug("%s: offset:%u, flags = %lx\n", __func__, offset, flags);
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100195
Simon Glass3f2d5752021-02-04 21:21:59 -0700196 newf = get_gpio_flags(dev, offset);
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100197
Heinrich Schuchardt5355be62020-09-14 12:50:55 +0200198 /*
199 * For testing purposes keep the output value when switching to input.
200 * This allows us to manipulate the input value via the gpio command.
201 */
202 if (flags & GPIOD_IS_IN)
Simon Glass3f2d5752021-02-04 21:21:59 -0700203 *newf = (flags & ~GPIOD_IS_OUT_ACTIVE) |
204 (*newf & GPIOD_IS_OUT_ACTIVE);
Heinrich Schuchardt5355be62020-09-14 12:50:55 +0200205 else
Simon Glass3f2d5752021-02-04 21:21:59 -0700206 *newf = flags;
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100207
208 return 0;
209}
210
Simon Glass3f2d5752021-02-04 21:21:59 -0700211static int sb_gpio_get_flags(struct udevice *dev, uint offset, ulong *flagsp)
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100212{
213 debug("%s: offset:%u\n", __func__, offset);
Simon Glass3f2d5752021-02-04 21:21:59 -0700214 *flagsp = *get_gpio_flags(dev, offset);
Patrick Delaunay28bdaa52020-01-13 11:35:14 +0100215
216 return 0;
217}
218
Simon Glass3176b6c2020-07-07 13:11:44 -0600219#if CONFIG_IS_ENABLED(ACPIGEN)
220static int sb_gpio_get_acpi(const struct gpio_desc *desc,
221 struct acpi_gpio *gpio)
222{
223 int ret;
224
225 /* Note that gpio_get_acpi() zeroes *gpio before calling here */
226 gpio->pin_count = 1;
227 gpio->pins[0] = desc->offset;
228 ret = acpi_device_scope(desc->dev, gpio->resource,
229 sizeof(gpio->resource));
230 if (ret)
231 return log_ret(ret);
232
233 /* All of these values are just used for testing */
234 if (desc->flags & GPIOD_ACTIVE_LOW) {
235 gpio->pin0_addr = 0x80012 + desc->offset;
236 gpio->type = ACPI_GPIO_TYPE_INTERRUPT;
237 gpio->pull = ACPI_GPIO_PULL_DOWN;
238 gpio->interrupt_debounce_timeout = 4321;
239
240 /* We use the GpioInt part */
241 gpio->irq.pin = desc->offset;
242 gpio->irq.polarity = ACPI_IRQ_ACTIVE_BOTH;
243 gpio->irq.shared = ACPI_IRQ_SHARED;
244 gpio->irq.wake = ACPI_IRQ_WAKE;
245
246 /* The GpioIo part is only used for testing */
247 gpio->polarity = ACPI_GPIO_ACTIVE_LOW;
248 } else {
249 gpio->pin0_addr = 0xc00dc + desc->offset;
250 gpio->type = ACPI_GPIO_TYPE_IO;
251 gpio->pull = ACPI_GPIO_PULL_UP;
252 gpio->interrupt_debounce_timeout = 0;
253
254 /* The GpioInt part is not used */
255
256 /* We use the GpioIo part */
257 gpio->output_drive_strength = 1234;
258 gpio->io_shared = true;
259 gpio->io_restrict = ACPI_GPIO_IO_RESTRICT_INPUT;
260 gpio->polarity = 0;
261 }
262
263 return 0;
264}
265
266static int sb_gpio_get_name(const struct udevice *dev, char *out_name)
267{
268 return acpi_copy_name(out_name, "GPIO");
269}
270
271struct acpi_ops gpio_sandbox_acpi_ops = {
272 .get_name = sb_gpio_get_name,
273};
274#endif /* ACPIGEN */
275
Simon Glassb4d70702014-02-26 15:59:25 -0700276static const struct dm_gpio_ops gpio_sandbox_ops = {
Simon Glassb4d70702014-02-26 15:59:25 -0700277 .direction_input = sb_gpio_direction_input,
278 .direction_output = sb_gpio_direction_output,
279 .get_value = sb_gpio_get_value,
280 .set_value = sb_gpio_set_value,
Simon Glass32048632014-10-04 11:29:45 -0600281 .get_function = sb_gpio_get_function,
Simon Glass16e10402015-01-05 20:05:29 -0700282 .xlate = sb_gpio_xlate,
Simon Glass54befdd2021-02-04 21:21:55 -0700283 .set_flags = sb_gpio_set_flags,
Simon Glassd063ce92021-02-04 21:21:56 -0700284 .get_flags = sb_gpio_get_flags,
Simon Glass3176b6c2020-07-07 13:11:44 -0600285#if CONFIG_IS_ENABLED(ACPIGEN)
286 .get_acpi = sb_gpio_get_acpi,
287#endif
Simon Glassb4d70702014-02-26 15:59:25 -0700288};
289
Simon Glassaad29ae2020-12-03 16:55:21 -0700290static int sandbox_gpio_of_to_plat(struct udevice *dev)
Simon Glassb43d0442012-02-15 15:51:13 -0800291{
Simon Glassde0977b2015-03-05 12:25:20 -0700292 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb43d0442012-02-15 15:51:13 -0800293
Simon Glass9e7ab232018-02-03 10:36:59 -0700294 uc_priv->gpio_count = dev_read_u32_default(dev, "sandbox,gpio-count",
295 0);
Simon Glass1185fcb2017-05-18 20:09:20 -0600296 uc_priv->bank_name = dev_read_string(dev, "gpio-bank-name");
Simon Glassb43d0442012-02-15 15:51:13 -0800297
Simon Glassb4d70702014-02-26 15:59:25 -0700298 return 0;
299}
Simon Glassb43d0442012-02-15 15:51:13 -0800300
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200301static int gpio_sandbox_probe(struct udevice *dev)
Simon Glassb4d70702014-02-26 15:59:25 -0700302{
Simon Glassde0977b2015-03-05 12:25:20 -0700303 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb4d70702014-02-26 15:59:25 -0700304
Simon Glassf1d50f72020-12-19 10:40:13 -0700305 if (!dev_has_ofnode(dev))
Simon Glassb4d70702014-02-26 15:59:25 -0700306 /* Tell the uclass how many GPIOs we have */
307 uc_priv->gpio_count = CONFIG_SANDBOX_GPIO_COUNT;
Simon Glassb4d70702014-02-26 15:59:25 -0700308
Simon Glass95588622020-12-22 19:30:28 -0700309 dev_set_priv(dev,
310 calloc(sizeof(struct gpio_state), uc_priv->gpio_count));
Simon Glassb4d70702014-02-26 15:59:25 -0700311
312 return 0;
Simon Glassb43d0442012-02-15 15:51:13 -0800313}
Simon Glassb4d70702014-02-26 15:59:25 -0700314
Simon Glassa367f1d2014-10-04 11:29:46 -0600315static int gpio_sandbox_remove(struct udevice *dev)
316{
Simon Glass95588622020-12-22 19:30:28 -0700317 free(dev_get_priv(dev));
Simon Glassa367f1d2014-10-04 11:29:46 -0600318
319 return 0;
320}
321
Simon Glass767827a2014-06-11 23:29:45 -0600322static const struct udevice_id sandbox_gpio_ids[] = {
Simon Glassb4d70702014-02-26 15:59:25 -0700323 { .compatible = "sandbox,gpio" },
324 { }
325};
326
Walter Lozano2901ac62020-06-25 01:10:04 -0300327U_BOOT_DRIVER(sandbox_gpio) = {
328 .name = "sandbox_gpio",
Simon Glassb4d70702014-02-26 15:59:25 -0700329 .id = UCLASS_GPIO,
330 .of_match = sandbox_gpio_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700331 .of_to_plat = sandbox_gpio_of_to_plat,
Simon Glassb4d70702014-02-26 15:59:25 -0700332 .probe = gpio_sandbox_probe,
Simon Glassa367f1d2014-10-04 11:29:46 -0600333 .remove = gpio_sandbox_remove,
Simon Glassb4d70702014-02-26 15:59:25 -0700334 .ops = &gpio_sandbox_ops,
Simon Glass3176b6c2020-07-07 13:11:44 -0600335 ACPI_OPS_PTR(&gpio_sandbox_acpi_ops)
Simon Glassb4d70702014-02-26 15:59:25 -0700336};
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100337
Simon Glassdf65db82020-12-28 20:34:57 -0700338DM_DRIVER_ALIAS(sandbox_gpio, sandbox_gpio_alias)
Walter Lozano48e5b042020-06-25 01:10:06 -0300339
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100340/* pincontrol: used only to check GPIO pin configuration (pinmux command) */
341
342struct sb_pinctrl_priv {
343 int pinctrl_ngpios;
344 struct list_head gpio_dev;
345};
346
347struct sb_gpio_bank {
348 struct udevice *gpio_dev;
349 struct list_head list;
350};
351
352static int sb_populate_gpio_dev_list(struct udevice *dev)
353{
354 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
355 struct udevice *gpio_dev;
356 struct udevice *child;
357 struct sb_gpio_bank *gpio_bank;
358 int ret;
359
360 /*
361 * parse pin-controller sub-nodes (ie gpio bank nodes) and fill
362 * a list with all gpio device reference which belongs to the
363 * current pin-controller. This list is used to find pin_name and
364 * pin muxing
365 */
366 list_for_each_entry(child, &dev->child_head, sibling_node) {
367 ret = uclass_get_device_by_name(UCLASS_GPIO, child->name,
368 &gpio_dev);
369 if (ret < 0)
370 continue;
371
372 gpio_bank = malloc(sizeof(*gpio_bank));
373 if (!gpio_bank) {
374 dev_err(dev, "Not enough memory\n");
375 return -ENOMEM;
376 }
377
378 gpio_bank->gpio_dev = gpio_dev;
379 list_add_tail(&gpio_bank->list, &priv->gpio_dev);
380 }
381
382 return 0;
383}
384
385static int sb_pinctrl_get_pins_count(struct udevice *dev)
386{
387 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
388 struct gpio_dev_priv *uc_priv;
389 struct sb_gpio_bank *gpio_bank;
390
391 /*
392 * if get_pins_count has already been executed once on this
393 * pin-controller, no need to run it again
394 */
395 if (priv->pinctrl_ngpios)
396 return priv->pinctrl_ngpios;
397
398 if (list_empty(&priv->gpio_dev))
399 sb_populate_gpio_dev_list(dev);
400 /*
401 * walk through all banks to retrieve the pin-controller
402 * pins number
403 */
404 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
405 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
406
407 priv->pinctrl_ngpios += uc_priv->gpio_count;
408 }
409
410 return priv->pinctrl_ngpios;
411}
412
413static struct udevice *sb_pinctrl_get_gpio_dev(struct udevice *dev,
414 unsigned int selector,
415 unsigned int *idx)
416{
417 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
418 struct sb_gpio_bank *gpio_bank;
419 struct gpio_dev_priv *uc_priv;
420 int pin_count = 0;
421
422 if (list_empty(&priv->gpio_dev))
423 sb_populate_gpio_dev_list(dev);
424
425 /* look up for the bank which owns the requested pin */
426 list_for_each_entry(gpio_bank, &priv->gpio_dev, list) {
427 uc_priv = dev_get_uclass_priv(gpio_bank->gpio_dev);
428
429 if (selector < (pin_count + uc_priv->gpio_count)) {
430 /*
431 * we found the bank, convert pin selector to
432 * gpio bank index
433 */
434 *idx = selector - pin_count;
435
436 return gpio_bank->gpio_dev;
437 }
438 pin_count += uc_priv->gpio_count;
439 }
440
441 return NULL;
442}
443
444static const char *sb_pinctrl_get_pin_name(struct udevice *dev,
445 unsigned int selector)
446{
447 struct gpio_dev_priv *uc_priv;
448 struct udevice *gpio_dev;
449 unsigned int gpio_idx;
450 static char pin_name[PINNAME_SIZE];
451
452 /* look up for the bank which owns the requested pin */
453 gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
454 if (!gpio_dev) {
455 snprintf(pin_name, PINNAME_SIZE, "Error");
456 } else {
457 uc_priv = dev_get_uclass_priv(gpio_dev);
458
459 snprintf(pin_name, PINNAME_SIZE, "%s%d",
460 uc_priv->bank_name,
461 gpio_idx);
462 }
463
464 return pin_name;
465}
466
Simon Glass3f2d5752021-02-04 21:21:59 -0700467static char *get_flags_string(ulong flags)
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100468{
469 if (flags & GPIOD_OPEN_DRAIN)
470 return "drive-open-drain";
471 if (flags & GPIOD_OPEN_SOURCE)
472 return "drive-open-source";
473 if (flags & GPIOD_PULL_UP)
474 return "bias-pull-up";
475 if (flags & GPIOD_PULL_DOWN)
476 return "bias-pull-down";
477 return ".";
478}
479
480static int sb_pinctrl_get_pin_muxing(struct udevice *dev,
481 unsigned int selector,
482 char *buf, int size)
483{
484 struct udevice *gpio_dev;
485 unsigned int gpio_idx;
Simon Glass3f2d5752021-02-04 21:21:59 -0700486 ulong flags;
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100487 int function;
488
489 /* look up for the bank which owns the requested pin */
490 gpio_dev = sb_pinctrl_get_gpio_dev(dev, selector, &gpio_idx);
491 if (!gpio_dev) {
492 snprintf(buf, size, "Error");
493 } else {
494 function = sb_gpio_get_function(gpio_dev, gpio_idx);
Simon Glass3f2d5752021-02-04 21:21:59 -0700495 flags = *get_gpio_flags(gpio_dev, gpio_idx);
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100496
497 snprintf(buf, size, "gpio %s %s",
498 function == GPIOF_OUTPUT ? "output" : "input",
Simon Glass3f2d5752021-02-04 21:21:59 -0700499 get_flags_string(flags));
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100500 }
501
502 return 0;
503}
Simon Glass3176b6c2020-07-07 13:11:44 -0600504
505#if CONFIG_IS_ENABLED(ACPIGEN)
506static int sb_pinctrl_get_name(const struct udevice *dev, char *out_name)
507{
508 return acpi_copy_name(out_name, "PINC");
509}
510#endif
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100511
512static int sandbox_pinctrl_probe(struct udevice *dev)
513{
514 struct sb_pinctrl_priv *priv = dev_get_priv(dev);
515
516 INIT_LIST_HEAD(&priv->gpio_dev);
517
518 return 0;
519}
520
521static struct pinctrl_ops sandbox_pinctrl_gpio_ops = {
522 .get_pin_name = sb_pinctrl_get_pin_name,
523 .get_pins_count = sb_pinctrl_get_pins_count,
524 .get_pin_muxing = sb_pinctrl_get_pin_muxing,
525};
Simon Glass3176b6c2020-07-07 13:11:44 -0600526
527#if CONFIG_IS_ENABLED(ACPIGEN)
528struct acpi_ops pinctrl_sandbox_acpi_ops = {
529 .get_name = sb_pinctrl_get_name,
530};
531#endif
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100532
533static const struct udevice_id sandbox_pinctrl_gpio_match[] = {
534 { .compatible = "sandbox,pinctrl-gpio" },
535 { /* sentinel */ }
536};
537
538U_BOOT_DRIVER(sandbox_pinctrl_gpio) = {
539 .name = "sandbox_pinctrl_gpio",
540 .id = UCLASS_PINCTRL,
541 .of_match = sandbox_pinctrl_gpio_match,
542 .ops = &sandbox_pinctrl_gpio_ops,
543 .bind = dm_scan_fdt_dev,
544 .probe = sandbox_pinctrl_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700545 .priv_auto = sizeof(struct sb_pinctrl_priv),
Simon Glass3176b6c2020-07-07 13:11:44 -0600546 ACPI_OPS_PTR(&pinctrl_sandbox_acpi_ops)
Patrick Delaunay1b4a22f2020-01-13 11:35:15 +0100547};