blob: 1db5ef001e284cbdd311c33fe483396d348bbd86 [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;
Simon Glassae7123f2015-01-05 20:05:27 -0700518 ret = check_reserved(&desc, "dir_input");
519 if (ret)
520 return ret;
Simon Glass96495d92014-02-26 15:59:24 -0700521
Simon Glassae7123f2015-01-05 20:05:27 -0700522 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
Simon Glass96495d92014-02-26 15:59:24 -0700523}
524
525/**
526 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
527 * gpio: GPIO number
528 * value: Logical value to be set on the GPIO pin
529 *
530 * This function implements the API that's compatible with current
531 * GPIO API used in U-Boot. The request is forwarded to particular
532 * GPIO driver. Returns 0 on success, negative value on error.
533 */
534int gpio_direction_output(unsigned gpio, int value)
535{
Simon Glassae7123f2015-01-05 20:05:27 -0700536 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700537 int ret;
538
Simon Glassae7123f2015-01-05 20:05:27 -0700539 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700540 if (ret)
541 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700542 ret = check_reserved(&desc, "dir_output");
543 if (ret)
544 return ret;
Simon Glass96495d92014-02-26 15:59:24 -0700545
Simon Glassae7123f2015-01-05 20:05:27 -0700546 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
547 desc.offset, value);
548}
549
Patrick Delaunay8a9140c2020-01-13 11:35:02 +0100550static int _gpio_get_value(const struct gpio_desc *desc)
Simon Glassae7123f2015-01-05 20:05:27 -0700551{
552 int value;
Patrick Delaunay8a9140c2020-01-13 11:35:02 +0100553
554 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
555
556 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
557}
558
559int dm_gpio_get_value(const struct gpio_desc *desc)
560{
Simon Glassae7123f2015-01-05 20:05:27 -0700561 int ret;
562
563 ret = check_reserved(desc, "get_value");
564 if (ret)
565 return ret;
566
Patrick Delaunay8a9140c2020-01-13 11:35:02 +0100567 return _gpio_get_value(desc);
Simon Glassae7123f2015-01-05 20:05:27 -0700568}
569
Simon Glass17c43f12016-03-06 19:27:51 -0700570int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassae7123f2015-01-05 20:05:27 -0700571{
572 int ret;
573
574 ret = check_reserved(desc, "set_value");
575 if (ret)
576 return ret;
577
578 if (desc->flags & GPIOD_ACTIVE_LOW)
579 value = !value;
Neil Armstrong47bd5332020-05-05 10:43:17 +0200580
581 /*
582 * Emulate open drain by not actively driving the line high or
583 * Emulate open source by not actively driving the line low
584 */
585 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
586 (desc->flags & GPIOD_OPEN_SOURCE && !value))
587 return gpio_get_ops(desc->dev)->direction_input(desc->dev,
588 desc->offset);
589 else if (desc->flags & GPIOD_OPEN_DRAIN ||
590 desc->flags & GPIOD_OPEN_SOURCE)
591 return gpio_get_ops(desc->dev)->direction_output(desc->dev,
592 desc->offset,
593 value);
594
Simon Glassae7123f2015-01-05 20:05:27 -0700595 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
596 return 0;
597}
598
Patrick Delaunay4292fb12020-01-13 11:35:04 +0100599/* check dir flags invalid configuration */
600static int check_dir_flags(ulong flags)
601{
602 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
603 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
604 __func__, flags);
605 return -EINVAL;
606 }
607
Patrick Delaunay477ca572020-01-13 11:35:07 +0100608 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
609 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
610 __func__, flags);
611 return -EINVAL;
612 }
613
614 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
615 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
616 __func__, flags);
617 return -EINVAL;
618 }
619
Patrick Delaunay4292fb12020-01-13 11:35:04 +0100620 return 0;
621}
622
Patrick Delaunay788ea832020-01-13 11:35:03 +0100623static int _dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
Simon Glassae7123f2015-01-05 20:05:27 -0700624{
625 struct udevice *dev = desc->dev;
626 struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunay4292fb12020-01-13 11:35:04 +0100627 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Patrick Delaunay788ea832020-01-13 11:35:03 +0100628 int ret = 0;
Simon Glassae7123f2015-01-05 20:05:27 -0700629
Patrick Delaunay4292fb12020-01-13 11:35:04 +0100630 ret = check_dir_flags(flags);
631 if (ret) {
632 dev_dbg(dev,
633 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
634 desc->dev->name,
635 uc_priv->bank_name ? uc_priv->bank_name : "",
636 desc->offset, flags);
637
638 return ret;
639 }
640
Patrick Delaunay8fd9daf2020-01-13 11:35:09 +0100641 /* GPIOD_ are directly managed by driver in set_dir_flags*/
642 if (ops->set_dir_flags) {
643 ret = ops->set_dir_flags(dev, desc->offset, flags);
644 } else {
645 if (flags & GPIOD_IS_OUT) {
646 ret = ops->direction_output(dev, desc->offset,
647 GPIOD_FLAGS_OUTPUT(flags));
648 } else if (flags & GPIOD_IS_IN) {
649 ret = ops->direction_input(dev, desc->offset);
650 }
Simon Glassae7123f2015-01-05 20:05:27 -0700651 }
Patrick Delaunay788ea832020-01-13 11:35:03 +0100652
Heiko Schochercd2faeb2020-05-22 11:08:56 +0200653 /* save the flags also in descriptor */
654 if (!ret)
655 desc->flags = flags;
656
Patrick Delaunay788ea832020-01-13 11:35:03 +0100657 return ret;
658}
659
660int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
661{
662 int ret;
663
664 ret = check_reserved(desc, "set_dir_flags");
Simon Glassae7123f2015-01-05 20:05:27 -0700665 if (ret)
666 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700667
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100668 /* combine the requested flags (for IN/OUT) and the descriptor flags */
669 flags |= desc->flags;
Patrick Delaunay788ea832020-01-13 11:35:03 +0100670 ret = _dm_gpio_set_dir_flags(desc, flags);
671
Patrick Delaunay788ea832020-01-13 11:35:03 +0100672 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700673}
674
675int dm_gpio_set_dir(struct gpio_desc *desc)
676{
Patrick Delaunay788ea832020-01-13 11:35:03 +0100677 int ret;
678
679 ret = check_reserved(desc, "set_dir");
680 if (ret)
681 return ret;
682
683 return _dm_gpio_set_dir_flags(desc, desc->flags);
Simon Glass96495d92014-02-26 15:59:24 -0700684}
685
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100686int dm_gpio_get_dir_flags(struct gpio_desc *desc, ulong *flags)
687{
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100688 struct udevice *dev = desc->dev;
689 int ret, value;
690 struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100691 ulong dir_flags;
692
693 ret = check_reserved(desc, "get_dir_flags");
694 if (ret)
695 return ret;
696
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100697 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
698 if (ops->get_dir_flags) {
699 ret = ops->get_dir_flags(dev, desc->offset, &dir_flags);
700 if (ret)
701 return ret;
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100702
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100703 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
704 value = dir_flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
705 if (desc->flags & GPIOD_ACTIVE_LOW)
706 value = !value;
707 dir_flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
708 dir_flags |= (desc->flags & GPIOD_ACTIVE_LOW);
709 if (value)
710 dir_flags |= GPIOD_IS_OUT_ACTIVE;
711 } else {
712 dir_flags = desc->flags;
713 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
714 dir_flags &= ~GPIOD_IS_OUT_ACTIVE;
715 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
716 dir_flags |= GPIOD_IS_OUT_ACTIVE;
717 }
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100718 *flags = dir_flags;
719
720 return 0;
721}
722
Simon Glass96495d92014-02-26 15:59:24 -0700723/**
724 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
725 * gpio: GPIO number
726 *
727 * This function implements the API that's compatible with current
728 * GPIO API used in U-Boot. The request is forwarded to particular
729 * GPIO driver. Returns the value of the GPIO pin, or negative value
730 * on error.
731 */
732int gpio_get_value(unsigned gpio)
733{
Simon Glass96495d92014-02-26 15:59:24 -0700734 int ret;
735
Simon Glassae7123f2015-01-05 20:05:27 -0700736 struct gpio_desc desc;
737
738 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700739 if (ret)
740 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700741 return dm_gpio_get_value(&desc);
Simon Glass96495d92014-02-26 15:59:24 -0700742}
743
744/**
745 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
746 * gpio: GPIO number
747 * value: Logical value to be set on the GPIO pin.
748 *
749 * This function implements the API that's compatible with current
750 * GPIO API used in U-Boot. The request is forwarded to particular
751 * GPIO driver. Returns 0 on success, negative value on error.
752 */
753int gpio_set_value(unsigned gpio, int value)
754{
Simon Glassae7123f2015-01-05 20:05:27 -0700755 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700756 int ret;
757
Simon Glassae7123f2015-01-05 20:05:27 -0700758 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700759 if (ret)
760 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700761 return dm_gpio_set_value(&desc, value);
Simon Glass96495d92014-02-26 15:59:24 -0700762}
763
Heiko Schocher54c5d082014-05-22 12:43:05 +0200764const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glass96495d92014-02-26 15:59:24 -0700765{
766 struct gpio_dev_priv *priv;
767
768 /* Must be called on an active device */
Simon Glasse564f052015-03-05 12:25:20 -0700769 priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -0700770 assert(priv);
771
772 *bit_count = priv->gpio_count;
773 return priv->bank_name;
774}
775
Simon Glass6449a502014-10-04 11:29:43 -0600776static const char * const gpio_function[GPIOF_COUNT] = {
777 "input",
778 "output",
779 "unused",
780 "unknown",
781 "func",
782};
783
Masahiro Yamadafb07f972017-06-22 16:50:25 +0900784static int get_function(struct udevice *dev, int offset, bool skip_unused,
785 const char **namep)
Simon Glass6449a502014-10-04 11:29:43 -0600786{
Simon Glasse564f052015-03-05 12:25:20 -0700787 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass6449a502014-10-04 11:29:43 -0600788 struct dm_gpio_ops *ops = gpio_get_ops(dev);
789
790 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
791 if (!device_active(dev))
792 return -ENODEV;
793 if (offset < 0 || offset >= uc_priv->gpio_count)
794 return -EINVAL;
795 if (namep)
796 *namep = uc_priv->name[offset];
797 if (skip_unused && !uc_priv->name[offset])
798 return GPIOF_UNUSED;
799 if (ops->get_function) {
800 int ret;
801
802 ret = ops->get_function(dev, offset);
803 if (ret < 0)
804 return ret;
805 if (ret >= ARRAY_SIZE(gpio_function))
806 return -ENODATA;
807 return ret;
808 }
809
810 return GPIOF_UNKNOWN;
811}
812
813int gpio_get_function(struct udevice *dev, int offset, const char **namep)
814{
815 return get_function(dev, offset, true, namep);
816}
817
818int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
819{
820 return get_function(dev, offset, false, namep);
821}
822
Simon Glass07575352014-10-04 11:29:44 -0600823int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
824{
825 struct dm_gpio_ops *ops = gpio_get_ops(dev);
826 struct gpio_dev_priv *priv;
827 char *str = buf;
828 int func;
829 int ret;
830 int len;
831
832 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
833
834 *buf = 0;
Simon Glasse564f052015-03-05 12:25:20 -0700835 priv = dev_get_uclass_priv(dev);
Simon Glass07575352014-10-04 11:29:44 -0600836 ret = gpio_get_raw_function(dev, offset, NULL);
837 if (ret < 0)
838 return ret;
839 func = ret;
840 len = snprintf(str, buffsize, "%s%d: %s",
841 priv->bank_name ? priv->bank_name : "",
842 offset, gpio_function[func]);
843 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
844 func == GPIOF_UNUSED) {
845 const char *label;
846 bool used;
847
848 ret = ops->get_value(dev, offset);
849 if (ret < 0)
850 return ret;
851 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
852 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
853 ret,
854 used ? 'x' : ' ',
855 used ? " " : "",
856 label ? label : "");
857 }
858
859 return 0;
860}
861
Simon Glass29126862020-07-07 13:11:44 -0600862#if CONFIG_IS_ENABLED(ACPIGEN)
863int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
864{
865 struct dm_gpio_ops *ops;
866
867 memset(gpio, '\0', sizeof(*gpio));
868 if (!dm_gpio_is_valid(desc)) {
869 /* Indicate that the GPIO is not valid */
870 gpio->pin_count = 0;
871 gpio->pins[0] = 0;
872 return -EINVAL;
873 }
874
875 ops = gpio_get_ops(desc->dev);
876 if (!ops->get_acpi)
877 return -ENOSYS;
878
879 return ops->get_acpi(desc, gpio);
880}
881#endif
882
Simon Glass962f5ca2015-04-14 21:03:20 -0600883int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
884{
885 int i, ret;
886 int gpio;
887
888 for (i = 0; i < 32; i++) {
889 gpio = gpio_num_array[i];
890 if (gpio == -1)
891 break;
892 ret = gpio_requestf(gpio, fmt, i);
893 if (ret)
894 goto err;
895 ret = gpio_direction_input(gpio);
896 if (ret) {
897 gpio_free(gpio);
898 goto err;
899 }
900 }
901
902 return 0;
903err:
904 for (i--; i >= 0; i--)
905 gpio_free(gpio_num_array[i]);
906
907 return ret;
908}
909
Simon Glasse5901c92014-11-10 18:00:21 -0700910/*
911 * get a number comprised of multiple GPIO values. gpio_num_array points to
912 * the array of gpio pin numbers to scan, terminated by -1.
913 */
Simon Glass962f5ca2015-04-14 21:03:20 -0600914int gpio_get_values_as_int(const int *gpio_list)
Simon Glasse5901c92014-11-10 18:00:21 -0700915{
916 int gpio;
917 unsigned bitmask = 1;
918 unsigned vector = 0;
Simon Glass962f5ca2015-04-14 21:03:20 -0600919 int ret;
Simon Glasse5901c92014-11-10 18:00:21 -0700920
921 while (bitmask &&
Simon Glass962f5ca2015-04-14 21:03:20 -0600922 ((gpio = *gpio_list++) != -1)) {
923 ret = gpio_get_value(gpio);
924 if (ret < 0)
925 return ret;
926 else if (ret)
Simon Glasse5901c92014-11-10 18:00:21 -0700927 vector |= bitmask;
928 bitmask <<= 1;
929 }
Simon Glass962f5ca2015-04-14 21:03:20 -0600930
Simon Glasse5901c92014-11-10 18:00:21 -0700931 return vector;
932}
933
Simon Glass17c43f12016-03-06 19:27:51 -0700934int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassbbf24782016-03-06 19:27:50 -0700935{
936 unsigned bitmask = 1;
937 unsigned vector = 0;
938 int ret, i;
939
940 for (i = 0; i < count; i++) {
941 ret = dm_gpio_get_value(&desc_list[i]);
942 if (ret < 0)
943 return ret;
944 else if (ret)
945 vector |= bitmask;
946 bitmask <<= 1;
947 }
948
949 return vector;
950}
951
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200952/**
953 * gpio_request_tail: common work for requesting a gpio.
954 *
955 * ret: return value from previous work in function which calls
956 * this function.
957 * This seems bogus (why calling this function instead not
958 * calling it and end caller function instead?).
959 * Because on error in caller function we want to set some
960 * default values in gpio desc and have a common error
961 * debug message, which provides this function.
962 * nodename: Name of node for which gpio gets requested
963 * used for gpio label name.
964 * args: pointer to output arguments structure
965 * list_name: Name of GPIO list
966 * used for gpio label name.
967 * index: gpio index in gpio list
968 * used for gpio label name.
969 * desc: pointer to gpio descriptor, filled from this
970 * function.
971 * flags: gpio flags to use.
972 * add_index: should index added to gpio label name
973 * gpio_dev: pointer to gpio device from which the gpio
974 * will be requested. If NULL try to get the
975 * gpio device with uclass_get_device_by_ofnode()
976 *
977 * return: In error case this function sets default values in
978 * gpio descriptor, also emmits a debug message.
979 * On success it returns 0 else the error code from
980 * function calls, or the error code passed through
981 * ret to this function.
982 *
983 */
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200984static int gpio_request_tail(int ret, const char *nodename,
Simon Glass3a571232017-05-18 20:09:18 -0600985 struct ofnode_phandle_args *args,
986 const char *list_name, int index,
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200987 struct gpio_desc *desc, int flags,
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200988 bool add_index, struct udevice *gpio_dev)
Simon Glass3669e0e2015-01-05 20:05:29 -0700989{
Patrick Delaunay9f2b0662020-01-13 11:35:01 +0100990 gpio_desc_init(desc, gpio_dev, 0);
Simon Glass3a571232017-05-18 20:09:18 -0600991 if (ret)
Simon Glass3669e0e2015-01-05 20:05:29 -0700992 goto err;
Simon Glass3669e0e2015-01-05 20:05:29 -0700993
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200994 if (!desc->dev) {
995 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
996 &desc->dev);
997 if (ret) {
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200998 debug("%s: uclass_get_device_by_ofnode failed\n",
999 __func__);
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001000 goto err;
1001 }
Simon Glass3669e0e2015-01-05 20:05:29 -07001002 }
Simon Glass3a571232017-05-18 20:09:18 -06001003 ret = gpio_find_and_xlate(desc, args);
Simon Glass3669e0e2015-01-05 20:05:29 -07001004 if (ret) {
1005 debug("%s: gpio_find_and_xlate failed\n", __func__);
1006 goto err;
1007 }
1008 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001009 nodename, list_name, index);
Simon Glass3669e0e2015-01-05 20:05:29 -07001010 if (ret) {
1011 debug("%s: dm_gpio_requestf failed\n", __func__);
1012 goto err;
1013 }
Patrick Delaunay695e5fd2020-01-13 11:35:06 +01001014 ret = dm_gpio_set_dir_flags(desc, flags);
Simon Glass3669e0e2015-01-05 20:05:29 -07001015 if (ret) {
1016 debug("%s: dm_gpio_set_dir failed\n", __func__);
1017 goto err;
1018 }
1019
1020 return 0;
1021err:
1022 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001023 __func__, nodename, list_name, index, ret);
Simon Glass3669e0e2015-01-05 20:05:29 -07001024 return ret;
1025}
1026
Simon Glass4fe40672021-02-04 21:21:54 -07001027#if !CONFIG_IS_ENABLED(OF_PLATDATA)
Simon Glass150c5af2017-05-30 21:47:09 -06001028static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1029 int index, struct gpio_desc *desc,
1030 int flags, bool add_index)
Simon Glass3a571232017-05-18 20:09:18 -06001031{
1032 struct ofnode_phandle_args args;
1033 int ret;
1034
Simon Glass150c5af2017-05-30 21:47:09 -06001035 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1036 index, &args);
Simon Glass3a571232017-05-18 20:09:18 -06001037
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001038 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1039 index, desc, flags, add_index, NULL);
Simon Glass3a571232017-05-18 20:09:18 -06001040}
1041
Simon Glass150c5af2017-05-30 21:47:09 -06001042int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass3669e0e2015-01-05 20:05:29 -07001043 struct gpio_desc *desc, int flags)
1044{
Simon Glass150c5af2017-05-30 21:47:09 -06001045 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1046 index > 0);
Simon Glass3669e0e2015-01-05 20:05:29 -07001047}
1048
Simon Glass150c5af2017-05-30 21:47:09 -06001049int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass3669e0e2015-01-05 20:05:29 -07001050 struct gpio_desc *desc, int flags)
1051{
Simon Glass150c5af2017-05-30 21:47:09 -06001052 struct ofnode_phandle_args args;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001053 ofnode node;
Simon Glass150c5af2017-05-30 21:47:09 -06001054 int ret;
1055
1056 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1057 index, &args);
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001058 node = dev_ofnode(dev);
1059 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1060 index, desc, flags, index > 0, NULL);
Simon Glass3669e0e2015-01-05 20:05:29 -07001061}
1062
Simon Glass150c5af2017-05-30 21:47:09 -06001063int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass3669e0e2015-01-05 20:05:29 -07001064 struct gpio_desc *desc, int max_count,
1065 int flags)
1066{
1067 int count;
1068 int ret;
1069
Przemyslaw Marczak2984e7a2015-03-31 18:57:16 +02001070 for (count = 0; count < max_count; count++) {
Simon Glass150c5af2017-05-30 21:47:09 -06001071 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass3669e0e2015-01-05 20:05:29 -07001072 &desc[count], flags, true);
1073 if (ret == -ENOENT)
1074 break;
1075 else if (ret)
1076 goto err;
1077 }
1078
1079 /* We ran out of GPIOs in the list */
1080 return count;
1081
1082err:
1083 gpio_free_list_nodev(desc, count - 1);
1084
1085 return ret;
1086}
1087
1088int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1089 struct gpio_desc *desc, int max_count,
1090 int flags)
1091{
1092 /*
1093 * This isn't ideal since we don't use dev->name in the debug()
1094 * calls in gpio_request_by_name(), but we can do this until
1095 * gpio_request_list_by_name_nodev() can be dropped.
1096 */
Simon Glass150c5af2017-05-30 21:47:09 -06001097 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1098 max_count, flags);
Simon Glass3669e0e2015-01-05 20:05:29 -07001099}
1100
1101int gpio_get_list_count(struct udevice *dev, const char *list_name)
1102{
1103 int ret;
1104
Patrick Delaunay85582172020-09-09 18:26:16 +02001105 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0, -1,
1106 NULL);
Simon Glass3669e0e2015-01-05 20:05:29 -07001107 if (ret) {
1108 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1109 __func__, dev->name, list_name, ret);
1110 }
1111
1112 return ret;
1113}
Simon Glass4fe40672021-02-04 21:21:54 -07001114#endif /* OF_PLATDATA */
Simon Glass3669e0e2015-01-05 20:05:29 -07001115
1116int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1117{
1118 /* For now, we don't do any checking of dev */
1119 return _dm_gpio_free(desc->dev, desc->offset);
1120}
1121
1122int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1123{
1124 int i;
1125
1126 /* For now, we don't do any checking of dev */
1127 for (i = 0; i < count; i++)
1128 dm_gpio_free(dev, &desc[i]);
1129
1130 return 0;
1131}
1132
1133int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1134{
1135 return gpio_free_list(NULL, desc, count);
1136}
1137
Simon Glass96495d92014-02-26 15:59:24 -07001138/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glassb892d122014-10-04 11:29:42 -06001139static int gpio_renumber(struct udevice *removed_dev)
Simon Glass96495d92014-02-26 15:59:24 -07001140{
1141 struct gpio_dev_priv *uc_priv;
Heiko Schocher54c5d082014-05-22 12:43:05 +02001142 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -07001143 struct uclass *uc;
1144 unsigned base;
1145 int ret;
1146
1147 ret = uclass_get(UCLASS_GPIO, &uc);
1148 if (ret)
1149 return ret;
1150
1151 /* Ensure that we have a base for each bank */
1152 base = 0;
1153 uclass_foreach_dev(dev, uc) {
Simon Glassb892d122014-10-04 11:29:42 -06001154 if (device_active(dev) && dev != removed_dev) {
Simon Glasse564f052015-03-05 12:25:20 -07001155 uc_priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -07001156 uc_priv->gpio_base = base;
1157 base += uc_priv->gpio_count;
1158 }
1159 }
1160
1161 return 0;
1162}
1163
Simon Glass17c43f12016-03-06 19:27:51 -07001164int gpio_get_number(const struct gpio_desc *desc)
Simon Glass56a71f82015-03-25 12:21:58 -06001165{
1166 struct udevice *dev = desc->dev;
1167 struct gpio_dev_priv *uc_priv;
1168
1169 if (!dev)
1170 return -1;
Simon Glass0fd3d912020-12-22 19:30:28 -07001171 uc_priv = dev_get_uclass_priv(dev);
Simon Glass56a71f82015-03-25 12:21:58 -06001172
1173 return uc_priv->gpio_base + desc->offset;
1174}
1175
Heiko Schocher54c5d082014-05-22 12:43:05 +02001176static int gpio_post_probe(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -07001177{
Simon Glasse564f052015-03-05 12:25:20 -07001178 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -06001179
1180 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1181 if (!uc_priv->name)
1182 return -ENOMEM;
1183
1184 return gpio_renumber(NULL);
Simon Glass96495d92014-02-26 15:59:24 -07001185}
1186
Heiko Schocher54c5d082014-05-22 12:43:05 +02001187static int gpio_pre_remove(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -07001188{
Simon Glasse564f052015-03-05 12:25:20 -07001189 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -06001190 int i;
1191
1192 for (i = 0; i < uc_priv->gpio_count; i++) {
1193 if (uc_priv->name[i])
1194 free(uc_priv->name[i]);
1195 }
1196 free(uc_priv->name);
1197
1198 return gpio_renumber(dev);
Simon Glass96495d92014-02-26 15:59:24 -07001199}
1200
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001201int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1202 char *list_name, int index, int flags,
1203 int dtflags, struct gpio_desc *desc)
1204{
1205 struct ofnode_phandle_args args;
1206
1207 args.node = ofnode_null();
1208 args.args_count = 2;
1209 args.args[0] = index;
1210 args.args[1] = dtflags;
1211
1212 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1213 flags, 0, dev);
1214}
1215
Jean-Jacques Hiblotd4b722e2020-09-11 13:43:34 +05301216static void devm_gpiod_release(struct udevice *dev, void *res)
1217{
1218 dm_gpio_free(dev, res);
1219}
1220
1221static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1222{
1223 return res == data;
1224}
1225
1226struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1227 unsigned int index, int flags)
1228{
1229 int rc;
1230 struct gpio_desc *desc;
1231 char *propname;
1232 static const char suffix[] = "-gpios";
1233
1234 propname = malloc(strlen(id) + sizeof(suffix));
1235 if (!propname) {
1236 rc = -ENOMEM;
1237 goto end;
1238 }
1239
1240 strcpy(propname, id);
1241 strcat(propname, suffix);
1242
1243 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1244 __GFP_ZERO);
1245 if (unlikely(!desc)) {
1246 rc = -ENOMEM;
1247 goto end;
1248 }
1249
1250 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1251
1252end:
1253 if (propname)
1254 free(propname);
1255
1256 if (rc)
1257 return ERR_PTR(rc);
1258
1259 devres_add(dev, desc);
1260
1261 return desc;
1262}
1263
1264struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1265 const char *id,
1266 unsigned int index,
1267 int flags)
1268{
1269 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1270
1271 if (IS_ERR(desc))
1272 return NULL;
1273
1274 return desc;
1275}
1276
1277void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1278{
1279 int rc;
1280
1281 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1282 WARN_ON(rc);
1283}
1284
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001285static int gpio_post_bind(struct udevice *dev)
1286{
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001287 struct udevice *child;
1288 ofnode node;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001289
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001290#if defined(CONFIG_NEEDS_MANUAL_RELOC)
1291 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1292 static int reloc_done;
1293
1294 if (!reloc_done) {
1295 if (ops->request)
1296 ops->request += gd->reloc_off;
Simon Glass093152f2020-02-04 20:15:17 -07001297 if (ops->rfree)
1298 ops->rfree += gd->reloc_off;
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001299 if (ops->direction_input)
1300 ops->direction_input += gd->reloc_off;
1301 if (ops->direction_output)
1302 ops->direction_output += gd->reloc_off;
1303 if (ops->get_value)
1304 ops->get_value += gd->reloc_off;
1305 if (ops->set_value)
1306 ops->set_value += gd->reloc_off;
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001307 if (ops->get_function)
1308 ops->get_function += gd->reloc_off;
1309 if (ops->xlate)
1310 ops->xlate += gd->reloc_off;
Patrick Delaunay8fd9daf2020-01-13 11:35:09 +01001311 if (ops->set_dir_flags)
1312 ops->set_dir_flags += gd->reloc_off;
Patrick Delaunayd2c07e52020-01-13 11:35:08 +01001313 if (ops->get_dir_flags)
1314 ops->get_dir_flags += gd->reloc_off;
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001315
1316 reloc_done++;
1317 }
1318#endif
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001319
Heiko Schocher49b10cb2019-07-17 06:59:51 +02001320 if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1321 dev_for_each_subnode(node, dev) {
1322 if (ofnode_read_bool(node, "gpio-hog")) {
1323 const char *name = ofnode_get_name(node);
1324 int ret;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001325
Heiko Schocher49b10cb2019-07-17 06:59:51 +02001326 ret = device_bind_driver_to_node(dev,
1327 "gpio_hog",
1328 name, node,
1329 &child);
1330 if (ret)
1331 return ret;
1332 }
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001333 }
1334 }
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001335 return 0;
1336}
1337
Simon Glass96495d92014-02-26 15:59:24 -07001338UCLASS_DRIVER(gpio) = {
1339 .id = UCLASS_GPIO,
1340 .name = "gpio",
Bhuvanchandra DVae89bb02015-06-01 18:37:15 +05301341 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass96495d92014-02-26 15:59:24 -07001342 .post_probe = gpio_post_probe,
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001343 .post_bind = gpio_post_bind,
Simon Glass96495d92014-02-26 15:59:24 -07001344 .pre_remove = gpio_pre_remove,
Simon Glass41575d82020-12-03 16:55:17 -07001345 .per_device_auto = sizeof(struct gpio_dev_priv),
Simon Glass96495d92014-02-26 15:59:24 -07001346};