blob: f90962a0075a039b342c10d2dd01b8f2244836b2 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass96495d92014-02-26 15:59:24 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glass96495d92014-02-26 15:59:24 -07004 */
5
6#include <common.h>
7#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Jean-Jacques Hiblotd4b722e2020-09-11 13:43:34 +05309#include <dm/devres.h>
10#include <dm/device_compat.h>
Heiko Schocher5fc7cf82019-06-12 06:11:46 +020011#include <dm/device-internal.h>
12#include <dm/lists.h>
13#include <dm/uclass-internal.h>
Eric Nelson6c880b72016-04-24 16:32:40 -070014#include <dt-bindings/gpio/gpio.h>
Simon Glass96495d92014-02-26 15:59:24 -070015#include <errno.h>
Simon Glass0dac4d52015-01-05 20:05:28 -070016#include <fdtdec.h>
Simon Glassb892d122014-10-04 11:29:42 -060017#include <malloc.h>
Simon Glass29126862020-07-07 13:11:44 -060018#include <acpi/acpi_device.h>
Simon Glass401d1c42020-10-30 21:38:53 -060019#include <asm/global_data.h>
Simon Glass96495d92014-02-26 15:59:24 -070020#include <asm/gpio.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060021#include <dm/device_compat.h>
Masahiro Yamada84b8bf62016-01-24 23:27:48 +090022#include <linux/bug.h>
Simon Glassfe1ef502014-10-22 21:37:01 -060023#include <linux/ctype.h>
Simon Glass96495d92014-02-26 15:59:24 -070024
Simon Glass3669e0e2015-01-05 20:05:29 -070025DECLARE_GLOBAL_DATA_PTR;
26
Simon Glass96495d92014-02-26 15:59:24 -070027/**
Patrick Delaunay9f2b0662020-01-13 11:35:01 +010028 * gpio_desc_init() - Initialize the GPIO descriptor
29 *
30 * @desc: GPIO descriptor to initialize
31 * @dev: GPIO device
32 * @offset: Offset of device GPIO
33 */
34static void gpio_desc_init(struct gpio_desc *desc,
35 struct udevice *dev,
36 uint offset)
37{
38 desc->dev = dev;
39 desc->offset = offset;
40 desc->flags = 0;
41}
42
43/**
Simon Glass96495d92014-02-26 15:59:24 -070044 * gpio_to_device() - Convert global GPIO number to device, number
Simon Glass96495d92014-02-26 15:59:24 -070045 *
46 * Convert the GPIO number to an entry in the list of GPIOs
47 * or GPIO blocks registered with the GPIO controller. Returns
48 * entry on success, NULL on error.
Simon Glassae7123f2015-01-05 20:05:27 -070049 *
50 * @gpio: The numeric representation of the GPIO
51 * @desc: Returns description (desc->flags will always be 0)
52 * @return 0 if found, -ENOENT if not found
Simon Glass96495d92014-02-26 15:59:24 -070053 */
Simon Glassae7123f2015-01-05 20:05:27 -070054static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glass96495d92014-02-26 15:59:24 -070055{
56 struct gpio_dev_priv *uc_priv;
Heiko Schocher54c5d082014-05-22 12:43:05 +020057 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -070058 int ret;
59
60 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
61 dev;
62 ret = uclass_next_device(&dev)) {
Simon Glasse564f052015-03-05 12:25:20 -070063 uc_priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -070064 if (gpio >= uc_priv->gpio_base &&
65 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
Patrick Delaunay9f2b0662020-01-13 11:35:01 +010066 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
Simon Glass96495d92014-02-26 15:59:24 -070067 return 0;
68 }
69 }
70
71 /* No such GPIO */
Simon Glassae7123f2015-01-05 20:05:27 -070072 return ret ? ret : -ENOENT;
Simon Glass96495d92014-02-26 15:59:24 -070073}
74
Heiko Schocher2bd261d2020-05-22 11:08:59 +020075#if CONFIG_IS_ENABLED(DM_GPIO_LOOKUP_LABEL)
76/**
77 * dm_gpio_lookup_label() - look for name in gpio device
78 *
79 * search in uc_priv, if there is a gpio with labelname same
80 * as name.
81 *
82 * @name: name which is searched
83 * @uc_priv: gpio_dev_priv pointer.
84 * @offset: gpio offset within the device
85 * @return: 0 if found, -ENOENT if not.
86 */
87static int dm_gpio_lookup_label(const char *name,
88 struct gpio_dev_priv *uc_priv, ulong *offset)
89{
90 int len;
91 int i;
92
93 *offset = -1;
94 len = strlen(name);
95 for (i = 0; i < uc_priv->gpio_count; i++) {
96 if (!uc_priv->name[i])
97 continue;
98 if (!strncmp(name, uc_priv->name[i], len)) {
99 *offset = i;
100 return 0;
101 }
102 }
103 return -ENOENT;
104}
105#else
106static int
107dm_gpio_lookup_label(const char *name, struct gpio_dev_priv *uc_priv,
108 ulong *offset)
109{
110 return -ENOENT;
111}
112#endif
113
Simon Glass32ec1592015-06-23 15:38:40 -0600114int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
Simon Glass96495d92014-02-26 15:59:24 -0700115{
Simon Glassfe1ef502014-10-22 21:37:01 -0600116 struct gpio_dev_priv *uc_priv = NULL;
Heiko Schocher54c5d082014-05-22 12:43:05 +0200117 struct udevice *dev;
Simon Glassfe1ef502014-10-22 21:37:01 -0600118 ulong offset;
119 int numeric;
Simon Glass96495d92014-02-26 15:59:24 -0700120 int ret;
121
Simon Glassfe1ef502014-10-22 21:37:01 -0600122 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
Simon Glass96495d92014-02-26 15:59:24 -0700123 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
124 dev;
125 ret = uclass_next_device(&dev)) {
Simon Glass96495d92014-02-26 15:59:24 -0700126 int len;
127
Simon Glasse564f052015-03-05 12:25:20 -0700128 uc_priv = dev_get_uclass_priv(dev);
Simon Glassfe1ef502014-10-22 21:37:01 -0600129 if (numeric != -1) {
130 offset = numeric - uc_priv->gpio_base;
131 /* Allow GPIOs to be numbered from 0 */
Tom Rini75897912017-05-10 15:20:15 -0400132 if (offset < uc_priv->gpio_count)
Simon Glassfe1ef502014-10-22 21:37:01 -0600133 break;
134 }
135
Simon Glass96495d92014-02-26 15:59:24 -0700136 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
137
Simon Glass939cda52014-06-11 23:29:47 -0600138 if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glassfe1ef502014-10-22 21:37:01 -0600139 if (!strict_strtoul(name + len, 10, &offset))
140 break;
Simon Glass96495d92014-02-26 15:59:24 -0700141 }
Heiko Schocher2bd261d2020-05-22 11:08:59 +0200142
143 /*
144 * if we did not found a gpio through its bank
145 * name, we search for a valid gpio label.
146 */
147 if (!dm_gpio_lookup_label(name, uc_priv, &offset))
148 break;
Simon Glass96495d92014-02-26 15:59:24 -0700149 }
150
Simon Glassfe1ef502014-10-22 21:37:01 -0600151 if (!dev)
152 return ret ? ret : -EINVAL;
153
Patrick Delaunay9f2b0662020-01-13 11:35:01 +0100154 gpio_desc_init(desc, dev, offset);
Simon Glass32ec1592015-06-23 15:38:40 -0600155
156 return 0;
157}
158
159int gpio_lookup_name(const char *name, struct udevice **devp,
160 unsigned int *offsetp, unsigned int *gpiop)
161{
162 struct gpio_desc desc;
163 int ret;
164
Simon Glassfe1ef502014-10-22 21:37:01 -0600165 if (devp)
Simon Glass32ec1592015-06-23 15:38:40 -0600166 *devp = NULL;
167 ret = dm_gpio_lookup_name(name, &desc);
168 if (ret)
169 return ret;
170
171 if (devp)
172 *devp = desc.dev;
Simon Glassfe1ef502014-10-22 21:37:01 -0600173 if (offsetp)
Simon Glass32ec1592015-06-23 15:38:40 -0600174 *offsetp = desc.offset;
175 if (gpiop) {
176 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
177
178 *gpiop = uc_priv->gpio_base + desc.offset;
179 }
Simon Glassfe1ef502014-10-22 21:37:01 -0600180
181 return 0;
Simon Glass96495d92014-02-26 15:59:24 -0700182}
183
Simon Glass3a571232017-05-18 20:09:18 -0600184int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
185 struct ofnode_phandle_args *args)
Eric Nelson6c880b72016-04-24 16:32:40 -0700186{
187 if (args->args_count < 1)
188 return -EINVAL;
189
190 desc->offset = args->args[0];
191
192 if (args->args_count < 2)
193 return 0;
194
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100195 desc->flags = 0;
Eric Nelson6c880b72016-04-24 16:32:40 -0700196 if (args->args[1] & GPIO_ACTIVE_LOW)
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100197 desc->flags |= GPIOD_ACTIVE_LOW;
Eric Nelson6c880b72016-04-24 16:32:40 -0700198
Patrick Delaunay477ca572020-01-13 11:35:07 +0100199 /*
200 * need to test 2 bits for gpio output binding:
201 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
202 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
203 */
204 if (args->args[1] & GPIO_SINGLE_ENDED) {
205 if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
206 desc->flags |= GPIOD_OPEN_DRAIN;
207 else
208 desc->flags |= GPIOD_OPEN_SOURCE;
209 }
210
211 if (args->args[1] & GPIO_PULL_UP)
212 desc->flags |= GPIOD_PULL_UP;
213
214 if (args->args[1] & GPIO_PULL_DOWN)
215 desc->flags |= GPIOD_PULL_DOWN;
216
Eric Nelson6c880b72016-04-24 16:32:40 -0700217 return 0;
218}
219
Simon Glass3669e0e2015-01-05 20:05:29 -0700220static int gpio_find_and_xlate(struct gpio_desc *desc,
Simon Glass3a571232017-05-18 20:09:18 -0600221 struct ofnode_phandle_args *args)
Simon Glass0dac4d52015-01-05 20:05:28 -0700222{
223 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
224
Eric Nelson6c880b72016-04-24 16:32:40 -0700225 if (ops->xlate)
226 return ops->xlate(desc->dev, desc, args);
Simon Glass0dac4d52015-01-05 20:05:28 -0700227 else
Eric Nelson6c880b72016-04-24 16:32:40 -0700228 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glass0dac4d52015-01-05 20:05:28 -0700229}
230
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200231#if defined(CONFIG_GPIO_HOG)
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200232
233struct gpio_hog_priv {
234 struct gpio_desc gpiod;
235};
236
237struct gpio_hog_data {
238 int gpiod_flags;
239 int value;
240 u32 val[2];
241};
242
Simon Glassd1998a92020-12-03 16:55:21 -0700243static int gpio_hog_of_to_plat(struct udevice *dev)
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200244{
Simon Glassc69cda22020-12-03 16:55:20 -0700245 struct gpio_hog_data *plat = dev_get_plat(dev);
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200246 const char *nodename;
247 int ret;
248
249 plat->value = 0;
250 if (dev_read_bool(dev, "input")) {
251 plat->gpiod_flags = GPIOD_IS_IN;
252 } else if (dev_read_bool(dev, "output-high")) {
253 plat->value = 1;
254 plat->gpiod_flags = GPIOD_IS_OUT;
255 } else if (dev_read_bool(dev, "output-low")) {
256 plat->gpiod_flags = GPIOD_IS_OUT;
257 } else {
258 printf("%s: missing gpio-hog state.\n", __func__);
259 return -EINVAL;
260 }
261 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
262 if (ret) {
263 printf("%s: wrong gpios property, 2 values needed %d\n",
264 __func__, ret);
265 return ret;
266 }
267 nodename = dev_read_string(dev, "line-name");
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200268 if (nodename)
269 device_set_name(dev, nodename);
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200270
271 return 0;
272}
273
274static int gpio_hog_probe(struct udevice *dev)
275{
Simon Glassc69cda22020-12-03 16:55:20 -0700276 struct gpio_hog_data *plat = dev_get_plat(dev);
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200277 struct gpio_hog_priv *priv = dev_get_priv(dev);
278 int ret;
279
280 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
281 plat->val[0], plat->gpiod_flags,
282 plat->val[1], &priv->gpiod);
283 if (ret < 0) {
284 debug("%s: node %s could not get gpio.\n", __func__,
285 dev->name);
286 return ret;
287 }
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200288
289 if (plat->gpiod_flags == GPIOD_IS_OUT) {
290 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
291 if (ret < 0) {
292 debug("%s: node %s could not set gpio.\n", __func__,
293 dev->name);
294 return ret;
295 }
296 }
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200297
298 return 0;
299}
300
301int gpio_hog_probe_all(void)
302{
303 struct udevice *dev;
304 int ret;
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200305 int retval = 0;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200306
307 for (uclass_first_device(UCLASS_NOP, &dev);
308 dev;
309 uclass_find_next_device(&dev)) {
Simon Glass65e25be2020-12-28 20:34:56 -0700310 if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200311 ret = device_probe(dev);
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200312 if (ret) {
313 printf("Failed to probe device %s err: %d\n",
314 dev->name, ret);
315 retval = ret;
316 }
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200317 }
318 }
319
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200320 return retval;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200321}
322
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200323int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200324{
325 struct udevice *dev;
326
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200327 *desc = NULL;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200328 gpio_hog_probe_all();
329 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
330 struct gpio_hog_priv *priv = dev_get_priv(dev);
331
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200332 *desc = &priv->gpiod;
333 return 0;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200334 }
335
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200336 return -ENODEV;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200337}
338
339U_BOOT_DRIVER(gpio_hog) = {
340 .name = "gpio_hog",
341 .id = UCLASS_NOP,
Simon Glassd1998a92020-12-03 16:55:21 -0700342 .of_to_plat = gpio_hog_of_to_plat,
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200343 .probe = gpio_hog_probe,
Simon Glass41575d82020-12-03 16:55:17 -0700344 .priv_auto = sizeof(struct gpio_hog_priv),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700345 .plat_auto = sizeof(struct gpio_hog_data),
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200346};
347#else
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200348int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200349{
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200350 return 0;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200351}
352#endif
353
Simon Glassefa677f2015-06-23 15:38:41 -0600354int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassae7123f2015-01-05 20:05:27 -0700355{
356 struct udevice *dev = desc->dev;
357 struct gpio_dev_priv *uc_priv;
358 char *str;
359 int ret;
360
Simon Glasse564f052015-03-05 12:25:20 -0700361 uc_priv = dev_get_uclass_priv(dev);
Simon Glassae7123f2015-01-05 20:05:27 -0700362 if (uc_priv->name[desc->offset])
363 return -EBUSY;
364 str = strdup(label);
365 if (!str)
366 return -ENOMEM;
367 if (gpio_get_ops(dev)->request) {
368 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
369 if (ret) {
370 free(str);
371 return ret;
372 }
373 }
374 uc_priv->name[desc->offset] = str;
375
376 return 0;
377}
378
Simon Glass3669e0e2015-01-05 20:05:29 -0700379static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
380{
Simon Glass27084c02019-09-25 08:56:27 -0600381#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass3669e0e2015-01-05 20:05:29 -0700382 va_list args;
383 char buf[40];
384
385 va_start(args, fmt);
386 vscnprintf(buf, sizeof(buf), fmt, args);
387 va_end(args);
388 return dm_gpio_request(desc, buf);
Simon Glass4dc52592015-12-29 05:22:48 -0700389#else
390 return dm_gpio_request(desc, fmt);
391#endif
Simon Glass3669e0e2015-01-05 20:05:29 -0700392}
393
Simon Glass96495d92014-02-26 15:59:24 -0700394/**
395 * gpio_request() - [COMPAT] Request GPIO
396 * gpio: GPIO number
397 * label: Name for the requested GPIO
398 *
Simon Glassb892d122014-10-04 11:29:42 -0600399 * The label is copied and allocated so the caller does not need to keep
400 * the pointer around.
401 *
Simon Glass96495d92014-02-26 15:59:24 -0700402 * This function implements the API that's compatible with current
403 * GPIO API used in U-Boot. The request is forwarded to particular
404 * GPIO driver. Returns 0 on success, negative value on error.
405 */
406int gpio_request(unsigned gpio, const char *label)
407{
Simon Glassae7123f2015-01-05 20:05:27 -0700408 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700409 int ret;
410
Simon Glassae7123f2015-01-05 20:05:27 -0700411 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700412 if (ret)
413 return ret;
414
Simon Glassae7123f2015-01-05 20:05:27 -0700415 return dm_gpio_request(&desc, label);
Simon Glass96495d92014-02-26 15:59:24 -0700416}
417
418/**
Simon Glassd44f5972014-10-04 11:29:49 -0600419 * gpio_requestf() - [COMPAT] Request GPIO
420 * @gpio: GPIO number
421 * @fmt: Format string for the requested GPIO
422 * @...: Arguments for the printf() format string
423 *
424 * This function implements the API that's compatible with current
425 * GPIO API used in U-Boot. The request is forwarded to particular
426 * GPIO driver. Returns 0 on success, negative value on error.
427 */
428int gpio_requestf(unsigned gpio, const char *fmt, ...)
429{
Simon Glass27084c02019-09-25 08:56:27 -0600430#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glassd44f5972014-10-04 11:29:49 -0600431 va_list args;
432 char buf[40];
433
434 va_start(args, fmt);
435 vscnprintf(buf, sizeof(buf), fmt, args);
436 va_end(args);
437 return gpio_request(gpio, buf);
Simon Glass4dc52592015-12-29 05:22:48 -0700438#else
439 return gpio_request(gpio, fmt);
440#endif
Simon Glassd44f5972014-10-04 11:29:49 -0600441}
442
Simon Glassae7123f2015-01-05 20:05:27 -0700443int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glass96495d92014-02-26 15:59:24 -0700444{
Simon Glassb892d122014-10-04 11:29:42 -0600445 struct gpio_dev_priv *uc_priv;
Simon Glass96495d92014-02-26 15:59:24 -0700446 int ret;
447
Simon Glasse564f052015-03-05 12:25:20 -0700448 uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -0600449 if (!uc_priv->name[offset])
450 return -ENXIO;
Simon Glass093152f2020-02-04 20:15:17 -0700451 if (gpio_get_ops(dev)->rfree) {
452 ret = gpio_get_ops(dev)->rfree(dev, offset);
Simon Glassb892d122014-10-04 11:29:42 -0600453 if (ret)
454 return ret;
455 }
456
457 free(uc_priv->name[offset]);
458 uc_priv->name[offset] = NULL;
459
460 return 0;
461}
462
Simon Glassae7123f2015-01-05 20:05:27 -0700463/**
464 * gpio_free() - [COMPAT] Relinquish GPIO
465 * gpio: GPIO number
466 *
467 * This function implements the API that's compatible with current
468 * GPIO API used in U-Boot. The request is forwarded to particular
469 * GPIO driver. Returns 0 on success, negative value on error.
470 */
471int gpio_free(unsigned gpio)
Simon Glassb892d122014-10-04 11:29:42 -0600472{
Simon Glassae7123f2015-01-05 20:05:27 -0700473 struct gpio_desc desc;
474 int ret;
Simon Glassb892d122014-10-04 11:29:42 -0600475
Simon Glassae7123f2015-01-05 20:05:27 -0700476 ret = gpio_to_device(gpio, &desc);
477 if (ret)
478 return ret;
479
480 return _dm_gpio_free(desc.dev, desc.offset);
481}
482
Simon Glass17c43f12016-03-06 19:27:51 -0700483static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glassae7123f2015-01-05 20:05:27 -0700484{
Simon Glasseca48662015-07-02 18:16:16 -0600485 struct gpio_dev_priv *uc_priv;
Simon Glassae7123f2015-01-05 20:05:27 -0700486
Simon Glasseca48662015-07-02 18:16:16 -0600487 if (!dm_gpio_is_valid(desc))
488 return -ENOENT;
489
490 uc_priv = dev_get_uclass_priv(desc->dev);
Simon Glassae7123f2015-01-05 20:05:27 -0700491 if (!uc_priv->name[desc->offset]) {
Simon Glassb892d122014-10-04 11:29:42 -0600492 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassae7123f2015-01-05 20:05:27 -0700493 desc->dev->name, func,
494 uc_priv->bank_name ? uc_priv->bank_name : "",
495 desc->offset);
Simon Glassb892d122014-10-04 11:29:42 -0600496 return -EBUSY;
497 }
498
499 return 0;
Simon Glass96495d92014-02-26 15:59:24 -0700500}
501
502/**
503 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
504 * gpio: GPIO number
505 *
506 * This function implements the API that's compatible with current
507 * GPIO API used in U-Boot. The request is forwarded to particular
508 * GPIO driver. Returns 0 on success, negative value on error.
509 */
510int gpio_direction_input(unsigned gpio)
511{
Simon Glassae7123f2015-01-05 20:05:27 -0700512 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700513 int ret;
514
Simon Glassae7123f2015-01-05 20:05:27 -0700515 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700516 if (ret)
517 return ret;
518
Simon Glassca1e1f52021-02-04 21:22:04 -0700519 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN);
Simon Glass96495d92014-02-26 15:59:24 -0700520}
521
522/**
523 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
524 * gpio: GPIO number
525 * value: Logical value to be set on the GPIO pin
526 *
527 * This function implements the API that's compatible with current
528 * GPIO API used in U-Boot. The request is forwarded to particular
529 * GPIO driver. Returns 0 on success, negative value on error.
530 */
531int gpio_direction_output(unsigned gpio, int value)
532{
Simon Glassae7123f2015-01-05 20:05:27 -0700533 struct gpio_desc desc;
Simon Glassca1e1f52021-02-04 21:22:04 -0700534 ulong flags;
Simon Glass96495d92014-02-26 15:59:24 -0700535 int ret;
536
Simon Glassae7123f2015-01-05 20:05:27 -0700537 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700538 if (ret)
539 return ret;
540
Simon Glassca1e1f52021-02-04 21:22:04 -0700541 flags = GPIOD_IS_OUT;
542 if (value)
543 flags |= GPIOD_IS_OUT_ACTIVE;
544 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, flags);
Simon Glassae7123f2015-01-05 20:05:27 -0700545}
546
Patrick Delaunay8a9140c2020-01-13 11:35:02 +0100547static int _gpio_get_value(const struct gpio_desc *desc)
Simon Glassae7123f2015-01-05 20:05:27 -0700548{
549 int value;
Patrick Delaunay8a9140c2020-01-13 11:35:02 +0100550
551 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
552
553 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
554}
555
556int dm_gpio_get_value(const struct gpio_desc *desc)
557{
Simon Glassae7123f2015-01-05 20:05:27 -0700558 int ret;
559
560 ret = check_reserved(desc, "get_value");
561 if (ret)
562 return ret;
563
Patrick Delaunay8a9140c2020-01-13 11:35:02 +0100564 return _gpio_get_value(desc);
Simon Glassae7123f2015-01-05 20:05:27 -0700565}
566
Simon Glass17c43f12016-03-06 19:27:51 -0700567int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassae7123f2015-01-05 20:05:27 -0700568{
Simon Glass7e0a96d2021-02-04 21:22:03 -0700569 const struct dm_gpio_ops *ops;
Simon Glassae7123f2015-01-05 20:05:27 -0700570 int ret;
571
572 ret = check_reserved(desc, "set_value");
573 if (ret)
574 return ret;
575
576 if (desc->flags & GPIOD_ACTIVE_LOW)
577 value = !value;
Neil Armstrong47bd5332020-05-05 10:43:17 +0200578
Simon Glass7e0a96d2021-02-04 21:22:03 -0700579 /* GPIOD_ are directly managed by driver in set_flags */
580 ops = gpio_get_ops(desc->dev);
581 if (ops->set_flags) {
582 ulong flags = desc->flags;
583
584 if (value)
585 flags |= GPIOD_IS_OUT_ACTIVE;
586 else
587 flags &= ~GPIOD_IS_OUT_ACTIVE;
588 return ops->set_flags(desc->dev, desc->offset, flags);
589 }
590
Neil Armstrong47bd5332020-05-05 10:43:17 +0200591 /*
592 * Emulate open drain by not actively driving the line high or
593 * Emulate open source by not actively driving the line low
594 */
595 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
596 (desc->flags & GPIOD_OPEN_SOURCE && !value))
Simon Glass7e0a96d2021-02-04 21:22:03 -0700597 return ops->direction_input(desc->dev, desc->offset);
Neil Armstrong47bd5332020-05-05 10:43:17 +0200598 else if (desc->flags & GPIOD_OPEN_DRAIN ||
599 desc->flags & GPIOD_OPEN_SOURCE)
Simon Glass7e0a96d2021-02-04 21:22:03 -0700600 return ops->direction_output(desc->dev, desc->offset, value);
Neil Armstrong47bd5332020-05-05 10:43:17 +0200601
Simon Glass7e0a96d2021-02-04 21:22:03 -0700602 ret = ops->set_value(desc->dev, desc->offset, value);
603 if (ret)
604 return ret;
605
Simon Glassae7123f2015-01-05 20:05:27 -0700606 return 0;
607}
608
Patrick Delaunay4292fb12020-01-13 11:35:04 +0100609/* check dir flags invalid configuration */
610static int check_dir_flags(ulong flags)
611{
612 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
613 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
614 __func__, flags);
615 return -EINVAL;
616 }
617
Patrick Delaunay477ca572020-01-13 11:35:07 +0100618 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
619 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
620 __func__, flags);
621 return -EINVAL;
622 }
623
624 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
625 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
626 __func__, flags);
627 return -EINVAL;
628 }
629
Patrick Delaunay4292fb12020-01-13 11:35:04 +0100630 return 0;
631}
632
Simon Glass7e0a96d2021-02-04 21:22:03 -0700633/**
634 * _dm_gpio_set_flags() - Send flags to the driver
635 *
636 * This uses the best available method to send the given flags to the driver.
637 * Note that if flags & GPIOD_ACTIVE_LOW, the driver sees the opposite value
638 * of GPIOD_IS_OUT_ACTIVE.
639 *
640 * @desc: GPIO description
641 * @flags: flags value to set
642 * @return 0 if OK, -ve on error
643 */
Simon Glass13979fc2021-02-04 21:21:55 -0700644static int _dm_gpio_set_flags(struct gpio_desc *desc, ulong flags)
Simon Glassae7123f2015-01-05 20:05:27 -0700645{
646 struct udevice *dev = desc->dev;
647 struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunay4292fb12020-01-13 11:35:04 +0100648 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Patrick Delaunay788ea832020-01-13 11:35:03 +0100649 int ret = 0;
Simon Glassae7123f2015-01-05 20:05:27 -0700650
Patrick Delaunay4292fb12020-01-13 11:35:04 +0100651 ret = check_dir_flags(flags);
652 if (ret) {
653 dev_dbg(dev,
654 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
655 desc->dev->name,
656 uc_priv->bank_name ? uc_priv->bank_name : "",
657 desc->offset, flags);
658
659 return ret;
660 }
661
Simon Glass7e0a96d2021-02-04 21:22:03 -0700662 /* If active low, invert the output state */
663 if ((flags & (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)) ==
664 (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW))
665 flags ^= GPIOD_IS_OUT_ACTIVE;
666
Simon Glass13979fc2021-02-04 21:21:55 -0700667 /* GPIOD_ are directly managed by driver in set_flags */
668 if (ops->set_flags) {
669 ret = ops->set_flags(dev, desc->offset, flags);
Patrick Delaunay8fd9daf2020-01-13 11:35:09 +0100670 } else {
671 if (flags & GPIOD_IS_OUT) {
Simon Glass7e0a96d2021-02-04 21:22:03 -0700672 bool value = flags & GPIOD_IS_OUT_ACTIVE;
673
674 ret = ops->direction_output(dev, desc->offset, value);
Patrick Delaunay8fd9daf2020-01-13 11:35:09 +0100675 } else if (flags & GPIOD_IS_IN) {
676 ret = ops->direction_input(dev, desc->offset);
677 }
Simon Glassae7123f2015-01-05 20:05:27 -0700678 }
Patrick Delaunay788ea832020-01-13 11:35:03 +0100679
680 return ret;
681}
682
Simon Glass7e0a96d2021-02-04 21:22:03 -0700683int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set)
Patrick Delaunay788ea832020-01-13 11:35:03 +0100684{
Simon Glass7e0a96d2021-02-04 21:22:03 -0700685 ulong flags;
Patrick Delaunay788ea832020-01-13 11:35:03 +0100686 int ret;
687
688 ret = check_reserved(desc, "set_dir_flags");
Simon Glassae7123f2015-01-05 20:05:27 -0700689 if (ret)
690 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700691
Simon Glass7e0a96d2021-02-04 21:22:03 -0700692 flags = (desc->flags & ~clr) | set;
Patrick Delaunay788ea832020-01-13 11:35:03 +0100693
Simon Glass7e0a96d2021-02-04 21:22:03 -0700694 ret = _dm_gpio_set_flags(desc, flags);
695 if (ret)
696 return ret;
697
698 /* save the flags also in descriptor */
699 desc->flags = flags;
700
701 return 0;
702}
703
704int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
705{
706 /* combine the requested flags (for IN/OUT) and the descriptor flags */
707 return dm_gpio_clrset_flags(desc, GPIOD_MASK_DIR, flags);
Simon Glassae7123f2015-01-05 20:05:27 -0700708}
709
710int dm_gpio_set_dir(struct gpio_desc *desc)
711{
Patrick Delaunay788ea832020-01-13 11:35:03 +0100712 int ret;
713
714 ret = check_reserved(desc, "set_dir");
715 if (ret)
716 return ret;
717
Simon Glass13979fc2021-02-04 21:21:55 -0700718 return _dm_gpio_set_flags(desc, desc->flags);
Simon Glass96495d92014-02-26 15:59:24 -0700719}
720
Simon Glassc0c1e622021-02-04 21:21:57 -0700721int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100722{
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100723 struct udevice *dev = desc->dev;
724 int ret, value;
725 struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass96487892021-02-04 21:21:56 -0700726 ulong flags;
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100727
Simon Glass96487892021-02-04 21:21:56 -0700728 ret = check_reserved(desc, "get_flags");
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100729 if (ret)
730 return ret;
731
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100732 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
Simon Glass96487892021-02-04 21:21:56 -0700733 if (ops->get_flags) {
734 ret = ops->get_flags(dev, desc->offset, &flags);
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100735 if (ret)
736 return ret;
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100737
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100738 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
Simon Glass96487892021-02-04 21:21:56 -0700739 value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100740 if (desc->flags & GPIOD_ACTIVE_LOW)
741 value = !value;
Simon Glass96487892021-02-04 21:21:56 -0700742 flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
743 flags |= (desc->flags & GPIOD_ACTIVE_LOW);
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100744 if (value)
Simon Glass96487892021-02-04 21:21:56 -0700745 flags |= GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100746 } else {
Simon Glass96487892021-02-04 21:21:56 -0700747 flags = desc->flags;
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100748 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
Simon Glass96487892021-02-04 21:21:56 -0700749 flags &= ~GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100750 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
Simon Glass96487892021-02-04 21:21:56 -0700751 flags |= GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100752 }
Simon Glass96487892021-02-04 21:21:56 -0700753 *flagsp = flags;
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100754
755 return 0;
756}
757
Simon Glass96495d92014-02-26 15:59:24 -0700758/**
759 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
760 * gpio: GPIO number
761 *
762 * This function implements the API that's compatible with current
763 * GPIO API used in U-Boot. The request is forwarded to particular
764 * GPIO driver. Returns the value of the GPIO pin, or negative value
765 * on error.
766 */
767int gpio_get_value(unsigned gpio)
768{
Simon Glass96495d92014-02-26 15:59:24 -0700769 int ret;
770
Simon Glassae7123f2015-01-05 20:05:27 -0700771 struct gpio_desc desc;
772
773 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700774 if (ret)
775 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700776 return dm_gpio_get_value(&desc);
Simon Glass96495d92014-02-26 15:59:24 -0700777}
778
779/**
780 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
781 * gpio: GPIO number
782 * value: Logical value to be set on the GPIO pin.
783 *
784 * This function implements the API that's compatible with current
785 * GPIO API used in U-Boot. The request is forwarded to particular
786 * GPIO driver. Returns 0 on success, negative value on error.
787 */
788int gpio_set_value(unsigned gpio, int value)
789{
Simon Glassae7123f2015-01-05 20:05:27 -0700790 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700791 int ret;
792
Simon Glassae7123f2015-01-05 20:05:27 -0700793 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700794 if (ret)
795 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700796 return dm_gpio_set_value(&desc, value);
Simon Glass96495d92014-02-26 15:59:24 -0700797}
798
Heiko Schocher54c5d082014-05-22 12:43:05 +0200799const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glass96495d92014-02-26 15:59:24 -0700800{
801 struct gpio_dev_priv *priv;
802
803 /* Must be called on an active device */
Simon Glasse564f052015-03-05 12:25:20 -0700804 priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -0700805 assert(priv);
806
807 *bit_count = priv->gpio_count;
808 return priv->bank_name;
809}
810
Simon Glass6449a502014-10-04 11:29:43 -0600811static const char * const gpio_function[GPIOF_COUNT] = {
812 "input",
813 "output",
814 "unused",
815 "unknown",
816 "func",
817};
818
Masahiro Yamadafb07f972017-06-22 16:50:25 +0900819static int get_function(struct udevice *dev, int offset, bool skip_unused,
820 const char **namep)
Simon Glass6449a502014-10-04 11:29:43 -0600821{
Simon Glasse564f052015-03-05 12:25:20 -0700822 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass6449a502014-10-04 11:29:43 -0600823 struct dm_gpio_ops *ops = gpio_get_ops(dev);
824
825 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
826 if (!device_active(dev))
827 return -ENODEV;
828 if (offset < 0 || offset >= uc_priv->gpio_count)
829 return -EINVAL;
830 if (namep)
831 *namep = uc_priv->name[offset];
832 if (skip_unused && !uc_priv->name[offset])
833 return GPIOF_UNUSED;
834 if (ops->get_function) {
835 int ret;
836
837 ret = ops->get_function(dev, offset);
838 if (ret < 0)
839 return ret;
840 if (ret >= ARRAY_SIZE(gpio_function))
841 return -ENODATA;
842 return ret;
843 }
844
845 return GPIOF_UNKNOWN;
846}
847
848int gpio_get_function(struct udevice *dev, int offset, const char **namep)
849{
850 return get_function(dev, offset, true, namep);
851}
852
853int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
854{
855 return get_function(dev, offset, false, namep);
856}
857
Simon Glass07575352014-10-04 11:29:44 -0600858int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
859{
860 struct dm_gpio_ops *ops = gpio_get_ops(dev);
861 struct gpio_dev_priv *priv;
862 char *str = buf;
863 int func;
864 int ret;
865 int len;
866
867 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
868
869 *buf = 0;
Simon Glasse564f052015-03-05 12:25:20 -0700870 priv = dev_get_uclass_priv(dev);
Simon Glass07575352014-10-04 11:29:44 -0600871 ret = gpio_get_raw_function(dev, offset, NULL);
872 if (ret < 0)
873 return ret;
874 func = ret;
875 len = snprintf(str, buffsize, "%s%d: %s",
876 priv->bank_name ? priv->bank_name : "",
877 offset, gpio_function[func]);
878 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
879 func == GPIOF_UNUSED) {
880 const char *label;
881 bool used;
882
883 ret = ops->get_value(dev, offset);
884 if (ret < 0)
885 return ret;
886 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
887 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
888 ret,
889 used ? 'x' : ' ',
890 used ? " " : "",
891 label ? label : "");
892 }
893
894 return 0;
895}
896
Simon Glass29126862020-07-07 13:11:44 -0600897#if CONFIG_IS_ENABLED(ACPIGEN)
898int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
899{
900 struct dm_gpio_ops *ops;
901
902 memset(gpio, '\0', sizeof(*gpio));
903 if (!dm_gpio_is_valid(desc)) {
904 /* Indicate that the GPIO is not valid */
905 gpio->pin_count = 0;
906 gpio->pins[0] = 0;
907 return -EINVAL;
908 }
909
910 ops = gpio_get_ops(desc->dev);
911 if (!ops->get_acpi)
912 return -ENOSYS;
913
914 return ops->get_acpi(desc, gpio);
915}
916#endif
917
Simon Glass962f5ca2015-04-14 21:03:20 -0600918int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
919{
920 int i, ret;
921 int gpio;
922
923 for (i = 0; i < 32; i++) {
924 gpio = gpio_num_array[i];
925 if (gpio == -1)
926 break;
927 ret = gpio_requestf(gpio, fmt, i);
928 if (ret)
929 goto err;
930 ret = gpio_direction_input(gpio);
931 if (ret) {
932 gpio_free(gpio);
933 goto err;
934 }
935 }
936
937 return 0;
938err:
939 for (i--; i >= 0; i--)
940 gpio_free(gpio_num_array[i]);
941
942 return ret;
943}
944
Simon Glasse5901c92014-11-10 18:00:21 -0700945/*
946 * get a number comprised of multiple GPIO values. gpio_num_array points to
947 * the array of gpio pin numbers to scan, terminated by -1.
948 */
Simon Glass962f5ca2015-04-14 21:03:20 -0600949int gpio_get_values_as_int(const int *gpio_list)
Simon Glasse5901c92014-11-10 18:00:21 -0700950{
951 int gpio;
952 unsigned bitmask = 1;
953 unsigned vector = 0;
Simon Glass962f5ca2015-04-14 21:03:20 -0600954 int ret;
Simon Glasse5901c92014-11-10 18:00:21 -0700955
956 while (bitmask &&
Simon Glass962f5ca2015-04-14 21:03:20 -0600957 ((gpio = *gpio_list++) != -1)) {
958 ret = gpio_get_value(gpio);
959 if (ret < 0)
960 return ret;
961 else if (ret)
Simon Glasse5901c92014-11-10 18:00:21 -0700962 vector |= bitmask;
963 bitmask <<= 1;
964 }
Simon Glass962f5ca2015-04-14 21:03:20 -0600965
Simon Glasse5901c92014-11-10 18:00:21 -0700966 return vector;
967}
968
Simon Glass17c43f12016-03-06 19:27:51 -0700969int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassbbf24782016-03-06 19:27:50 -0700970{
971 unsigned bitmask = 1;
972 unsigned vector = 0;
973 int ret, i;
974
975 for (i = 0; i < count; i++) {
976 ret = dm_gpio_get_value(&desc_list[i]);
977 if (ret < 0)
978 return ret;
979 else if (ret)
980 vector |= bitmask;
981 bitmask <<= 1;
982 }
983
984 return vector;
985}
986
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200987/**
988 * gpio_request_tail: common work for requesting a gpio.
989 *
990 * ret: return value from previous work in function which calls
991 * this function.
992 * This seems bogus (why calling this function instead not
993 * calling it and end caller function instead?).
994 * Because on error in caller function we want to set some
995 * default values in gpio desc and have a common error
996 * debug message, which provides this function.
997 * nodename: Name of node for which gpio gets requested
998 * used for gpio label name.
999 * args: pointer to output arguments structure
1000 * list_name: Name of GPIO list
1001 * used for gpio label name.
1002 * index: gpio index in gpio list
1003 * used for gpio label name.
1004 * desc: pointer to gpio descriptor, filled from this
1005 * function.
1006 * flags: gpio flags to use.
1007 * add_index: should index added to gpio label name
1008 * gpio_dev: pointer to gpio device from which the gpio
1009 * will be requested. If NULL try to get the
1010 * gpio device with uclass_get_device_by_ofnode()
1011 *
1012 * return: In error case this function sets default values in
1013 * gpio descriptor, also emmits a debug message.
1014 * On success it returns 0 else the error code from
1015 * function calls, or the error code passed through
1016 * ret to this function.
1017 *
1018 */
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001019static int gpio_request_tail(int ret, const char *nodename,
Simon Glass3a571232017-05-18 20:09:18 -06001020 struct ofnode_phandle_args *args,
1021 const char *list_name, int index,
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001022 struct gpio_desc *desc, int flags,
Heiko Schocher49b10cb2019-07-17 06:59:51 +02001023 bool add_index, struct udevice *gpio_dev)
Simon Glass3669e0e2015-01-05 20:05:29 -07001024{
Patrick Delaunay9f2b0662020-01-13 11:35:01 +01001025 gpio_desc_init(desc, gpio_dev, 0);
Simon Glass3a571232017-05-18 20:09:18 -06001026 if (ret)
Simon Glass3669e0e2015-01-05 20:05:29 -07001027 goto err;
Simon Glass3669e0e2015-01-05 20:05:29 -07001028
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001029 if (!desc->dev) {
1030 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
1031 &desc->dev);
1032 if (ret) {
Heiko Schocher49b10cb2019-07-17 06:59:51 +02001033 debug("%s: uclass_get_device_by_ofnode failed\n",
1034 __func__);
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001035 goto err;
1036 }
Simon Glass3669e0e2015-01-05 20:05:29 -07001037 }
Simon Glass3a571232017-05-18 20:09:18 -06001038 ret = gpio_find_and_xlate(desc, args);
Simon Glass3669e0e2015-01-05 20:05:29 -07001039 if (ret) {
1040 debug("%s: gpio_find_and_xlate failed\n", __func__);
1041 goto err;
1042 }
1043 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001044 nodename, list_name, index);
Simon Glass3669e0e2015-01-05 20:05:29 -07001045 if (ret) {
1046 debug("%s: dm_gpio_requestf failed\n", __func__);
1047 goto err;
1048 }
Simon Glass7e0a96d2021-02-04 21:22:03 -07001049
1050 /* Keep any direction flags provided by the devicetree */
1051 ret = dm_gpio_set_dir_flags(desc,
1052 flags | (desc->flags & GPIOD_MASK_DIR));
Simon Glass3669e0e2015-01-05 20:05:29 -07001053 if (ret) {
1054 debug("%s: dm_gpio_set_dir failed\n", __func__);
1055 goto err;
1056 }
1057
1058 return 0;
1059err:
1060 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001061 __func__, nodename, list_name, index, ret);
Simon Glass3669e0e2015-01-05 20:05:29 -07001062 return ret;
1063}
1064
Simon Glass4fe40672021-02-04 21:21:54 -07001065#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass150c5af2017-05-30 21:47:09 -06001066static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1067 int index, struct gpio_desc *desc,
1068 int flags, bool add_index)
Simon Glass3a571232017-05-18 20:09:18 -06001069{
1070 struct ofnode_phandle_args args;
1071 int ret;
1072
Simon Glass150c5af2017-05-30 21:47:09 -06001073 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1074 index, &args);
Simon Glass3a571232017-05-18 20:09:18 -06001075
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001076 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1077 index, desc, flags, add_index, NULL);
Simon Glass3a571232017-05-18 20:09:18 -06001078}
1079
Simon Glass150c5af2017-05-30 21:47:09 -06001080int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass3669e0e2015-01-05 20:05:29 -07001081 struct gpio_desc *desc, int flags)
1082{
Simon Glass150c5af2017-05-30 21:47:09 -06001083 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1084 index > 0);
Simon Glass3669e0e2015-01-05 20:05:29 -07001085}
1086
Simon Glass150c5af2017-05-30 21:47:09 -06001087int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass3669e0e2015-01-05 20:05:29 -07001088 struct gpio_desc *desc, int flags)
1089{
Simon Glass150c5af2017-05-30 21:47:09 -06001090 struct ofnode_phandle_args args;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001091 ofnode node;
Simon Glass150c5af2017-05-30 21:47:09 -06001092 int ret;
1093
1094 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1095 index, &args);
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001096 node = dev_ofnode(dev);
1097 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1098 index, desc, flags, index > 0, NULL);
Simon Glass3669e0e2015-01-05 20:05:29 -07001099}
1100
Simon Glass150c5af2017-05-30 21:47:09 -06001101int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass3669e0e2015-01-05 20:05:29 -07001102 struct gpio_desc *desc, int max_count,
1103 int flags)
1104{
1105 int count;
1106 int ret;
1107
Przemyslaw Marczak2984e7a2015-03-31 18:57:16 +02001108 for (count = 0; count < max_count; count++) {
Simon Glass150c5af2017-05-30 21:47:09 -06001109 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass3669e0e2015-01-05 20:05:29 -07001110 &desc[count], flags, true);
1111 if (ret == -ENOENT)
1112 break;
1113 else if (ret)
1114 goto err;
1115 }
1116
1117 /* We ran out of GPIOs in the list */
1118 return count;
1119
1120err:
1121 gpio_free_list_nodev(desc, count - 1);
1122
1123 return ret;
1124}
1125
1126int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1127 struct gpio_desc *desc, int max_count,
1128 int flags)
1129{
1130 /*
1131 * This isn't ideal since we don't use dev->name in the debug()
1132 * calls in gpio_request_by_name(), but we can do this until
1133 * gpio_request_list_by_name_nodev() can be dropped.
1134 */
Simon Glass150c5af2017-05-30 21:47:09 -06001135 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1136 max_count, flags);
Simon Glass3669e0e2015-01-05 20:05:29 -07001137}
1138
1139int gpio_get_list_count(struct udevice *dev, const char *list_name)
1140{
1141 int ret;
1142
Patrick Delaunay85582172020-09-09 18:26:16 +02001143 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0, -1,
1144 NULL);
Simon Glass3669e0e2015-01-05 20:05:29 -07001145 if (ret) {
1146 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1147 __func__, dev->name, list_name, ret);
1148 }
1149
1150 return ret;
1151}
Simon Glass4fe40672021-02-04 21:21:54 -07001152#endif /* OF_PLATDATA */
Simon Glass3669e0e2015-01-05 20:05:29 -07001153
1154int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1155{
1156 /* For now, we don't do any checking of dev */
1157 return _dm_gpio_free(desc->dev, desc->offset);
1158}
1159
1160int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1161{
1162 int i;
1163
1164 /* For now, we don't do any checking of dev */
1165 for (i = 0; i < count; i++)
1166 dm_gpio_free(dev, &desc[i]);
1167
1168 return 0;
1169}
1170
1171int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1172{
1173 return gpio_free_list(NULL, desc, count);
1174}
1175
Simon Glass96495d92014-02-26 15:59:24 -07001176/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glassb892d122014-10-04 11:29:42 -06001177static int gpio_renumber(struct udevice *removed_dev)
Simon Glass96495d92014-02-26 15:59:24 -07001178{
1179 struct gpio_dev_priv *uc_priv;
Heiko Schocher54c5d082014-05-22 12:43:05 +02001180 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -07001181 struct uclass *uc;
1182 unsigned base;
1183 int ret;
1184
1185 ret = uclass_get(UCLASS_GPIO, &uc);
1186 if (ret)
1187 return ret;
1188
1189 /* Ensure that we have a base for each bank */
1190 base = 0;
1191 uclass_foreach_dev(dev, uc) {
Simon Glassb892d122014-10-04 11:29:42 -06001192 if (device_active(dev) && dev != removed_dev) {
Simon Glasse564f052015-03-05 12:25:20 -07001193 uc_priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -07001194 uc_priv->gpio_base = base;
1195 base += uc_priv->gpio_count;
1196 }
1197 }
1198
1199 return 0;
1200}
1201
Simon Glass17c43f12016-03-06 19:27:51 -07001202int gpio_get_number(const struct gpio_desc *desc)
Simon Glass56a71f82015-03-25 12:21:58 -06001203{
1204 struct udevice *dev = desc->dev;
1205 struct gpio_dev_priv *uc_priv;
1206
1207 if (!dev)
1208 return -1;
Simon Glass0fd3d912020-12-22 19:30:28 -07001209 uc_priv = dev_get_uclass_priv(dev);
Simon Glass56a71f82015-03-25 12:21:58 -06001210
1211 return uc_priv->gpio_base + desc->offset;
1212}
1213
Heiko Schocher54c5d082014-05-22 12:43:05 +02001214static int gpio_post_probe(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -07001215{
Simon Glasse564f052015-03-05 12:25:20 -07001216 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -06001217
1218 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1219 if (!uc_priv->name)
1220 return -ENOMEM;
1221
1222 return gpio_renumber(NULL);
Simon Glass96495d92014-02-26 15:59:24 -07001223}
1224
Heiko Schocher54c5d082014-05-22 12:43:05 +02001225static int gpio_pre_remove(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -07001226{
Simon Glasse564f052015-03-05 12:25:20 -07001227 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -06001228 int i;
1229
1230 for (i = 0; i < uc_priv->gpio_count; i++) {
1231 if (uc_priv->name[i])
1232 free(uc_priv->name[i]);
1233 }
1234 free(uc_priv->name);
1235
1236 return gpio_renumber(dev);
Simon Glass96495d92014-02-26 15:59:24 -07001237}
1238
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001239int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1240 char *list_name, int index, int flags,
1241 int dtflags, struct gpio_desc *desc)
1242{
1243 struct ofnode_phandle_args args;
1244
1245 args.node = ofnode_null();
1246 args.args_count = 2;
1247 args.args[0] = index;
1248 args.args[1] = dtflags;
1249
1250 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1251 flags, 0, dev);
1252}
1253
Jean-Jacques Hiblotd4b722e2020-09-11 13:43:34 +05301254static void devm_gpiod_release(struct udevice *dev, void *res)
1255{
1256 dm_gpio_free(dev, res);
1257}
1258
1259static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1260{
1261 return res == data;
1262}
1263
1264struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1265 unsigned int index, int flags)
1266{
1267 int rc;
1268 struct gpio_desc *desc;
1269 char *propname;
1270 static const char suffix[] = "-gpios";
1271
1272 propname = malloc(strlen(id) + sizeof(suffix));
1273 if (!propname) {
1274 rc = -ENOMEM;
1275 goto end;
1276 }
1277
1278 strcpy(propname, id);
1279 strcat(propname, suffix);
1280
1281 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1282 __GFP_ZERO);
1283 if (unlikely(!desc)) {
1284 rc = -ENOMEM;
1285 goto end;
1286 }
1287
1288 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1289
1290end:
1291 if (propname)
1292 free(propname);
1293
1294 if (rc)
1295 return ERR_PTR(rc);
1296
1297 devres_add(dev, desc);
1298
1299 return desc;
1300}
1301
1302struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1303 const char *id,
1304 unsigned int index,
1305 int flags)
1306{
1307 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1308
1309 if (IS_ERR(desc))
1310 return NULL;
1311
1312 return desc;
1313}
1314
1315void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1316{
1317 int rc;
1318
1319 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1320 WARN_ON(rc);
1321}
1322
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001323static int gpio_post_bind(struct udevice *dev)
1324{
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001325 struct udevice *child;
1326 ofnode node;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001327
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001328#if defined(CONFIG_NEEDS_MANUAL_RELOC)
1329 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1330 static int reloc_done;
1331
1332 if (!reloc_done) {
1333 if (ops->request)
1334 ops->request += gd->reloc_off;
Simon Glass093152f2020-02-04 20:15:17 -07001335 if (ops->rfree)
1336 ops->rfree += gd->reloc_off;
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001337 if (ops->direction_input)
1338 ops->direction_input += gd->reloc_off;
1339 if (ops->direction_output)
1340 ops->direction_output += gd->reloc_off;
1341 if (ops->get_value)
1342 ops->get_value += gd->reloc_off;
1343 if (ops->set_value)
1344 ops->set_value += gd->reloc_off;
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001345 if (ops->get_function)
1346 ops->get_function += gd->reloc_off;
1347 if (ops->xlate)
1348 ops->xlate += gd->reloc_off;
Simon Glass13979fc2021-02-04 21:21:55 -07001349 if (ops->set_flags)
1350 ops->set_flags += gd->reloc_off;
Simon Glass96487892021-02-04 21:21:56 -07001351 if (ops->get_flags)
1352 ops->get_flags += gd->reloc_off;
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001353
1354 reloc_done++;
1355 }
1356#endif
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001357
Heiko Schocher49b10cb2019-07-17 06:59:51 +02001358 if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1359 dev_for_each_subnode(node, dev) {
1360 if (ofnode_read_bool(node, "gpio-hog")) {
1361 const char *name = ofnode_get_name(node);
1362 int ret;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001363
Heiko Schocher49b10cb2019-07-17 06:59:51 +02001364 ret = device_bind_driver_to_node(dev,
1365 "gpio_hog",
1366 name, node,
1367 &child);
1368 if (ret)
1369 return ret;
1370 }
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001371 }
1372 }
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001373 return 0;
1374}
1375
Simon Glass96495d92014-02-26 15:59:24 -07001376UCLASS_DRIVER(gpio) = {
1377 .id = UCLASS_GPIO,
1378 .name = "gpio",
Bhuvanchandra DVae89bb02015-06-01 18:37:15 +05301379 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass96495d92014-02-26 15:59:24 -07001380 .post_probe = gpio_post_probe,
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001381 .post_bind = gpio_post_bind,
Simon Glass96495d92014-02-26 15:59:24 -07001382 .pre_remove = gpio_pre_remove,
Simon Glass41575d82020-12-03 16:55:17 -07001383 .per_device_auto = sizeof(struct gpio_dev_priv),
Simon Glass96495d92014-02-26 15:59:24 -07001384};