blob: 65033258f47887d26f5afe1cd14c02e8ce277cb5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse821d182014-02-26 15:59:24 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glasse821d182014-02-26 15:59:24 -07004 */
5
Simon Glasse43eae22021-02-04 21:22:08 -07006#define LOG_CATEGORY UCLASS_GPIO
7
Simon Glasse821d182014-02-26 15:59:24 -07008#include <common.h>
9#include <dm.h>
Simon Glass2149e112021-08-07 07:24:12 -060010#include <dt-structs.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Jean-Jacques Hiblot775d6a22020-09-11 13:43:34 +053012#include <dm/devres.h>
13#include <dm/device_compat.h>
Heiko Schocher39cb3402019-06-12 06:11:46 +020014#include <dm/device-internal.h>
15#include <dm/lists.h>
16#include <dm/uclass-internal.h>
Eric Nelson786e98d2016-04-24 16:32:40 -070017#include <dt-bindings/gpio/gpio.h>
Simon Glasse821d182014-02-26 15:59:24 -070018#include <errno.h>
Simon Glassd3322bb2015-01-05 20:05:28 -070019#include <fdtdec.h>
Simon Glass0f4517d2014-10-04 11:29:42 -060020#include <malloc.h>
Simon Glass3176b6c2020-07-07 13:11:44 -060021#include <acpi/acpi_device.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060022#include <asm/global_data.h>
Simon Glasse821d182014-02-26 15:59:24 -070023#include <asm/gpio.h>
Simon Glass0f2af882020-05-10 11:40:05 -060024#include <dm/device_compat.h>
Masahiro Yamada78eeb912016-01-24 23:27:48 +090025#include <linux/bug.h>
Simon Glass43b2e1a2014-10-22 21:37:01 -060026#include <linux/ctype.h>
Simon Glass247ccf22021-02-04 21:22:09 -070027#include <linux/delay.h>
Simon Glasse821d182014-02-26 15:59:24 -070028
Simon Glass16e10402015-01-05 20:05:29 -070029DECLARE_GLOBAL_DATA_PTR;
30
Simon Glasse821d182014-02-26 15:59:24 -070031/**
Patrick Delaunay758bba92020-01-13 11:35:01 +010032 * gpio_desc_init() - Initialize the GPIO descriptor
33 *
34 * @desc: GPIO descriptor to initialize
35 * @dev: GPIO device
36 * @offset: Offset of device GPIO
37 */
38static void gpio_desc_init(struct gpio_desc *desc,
39 struct udevice *dev,
40 uint offset)
41{
42 desc->dev = dev;
43 desc->offset = offset;
44 desc->flags = 0;
45}
46
47/**
Simon Glasse821d182014-02-26 15:59:24 -070048 * gpio_to_device() - Convert global GPIO number to device, number
Simon Glasse821d182014-02-26 15:59:24 -070049 *
50 * Convert the GPIO number to an entry in the list of GPIOs
51 * or GPIO blocks registered with the GPIO controller. Returns
52 * entry on success, NULL on error.
Simon Glassce555292015-01-05 20:05:27 -070053 *
54 * @gpio: The numeric representation of the GPIO
55 * @desc: Returns description (desc->flags will always be 0)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010056 * Return: 0 if found, -ENOENT if not found
Simon Glasse821d182014-02-26 15:59:24 -070057 */
Simon Glassce555292015-01-05 20:05:27 -070058static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -070059{
60 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +020061 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -070062 int ret;
63
64 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
65 dev;
66 ret = uclass_next_device(&dev)) {
Simon Glassde0977b2015-03-05 12:25:20 -070067 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -070068 if (gpio >= uc_priv->gpio_base &&
69 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
Patrick Delaunay758bba92020-01-13 11:35:01 +010070 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
Simon Glasse821d182014-02-26 15:59:24 -070071 return 0;
72 }
73 }
74
75 /* No such GPIO */
Simon Glassce555292015-01-05 20:05:27 -070076 return ret ? ret : -ENOENT;
Simon Glasse821d182014-02-26 15:59:24 -070077}
78
Heiko Schochera3e793c2020-05-22 11:08:59 +020079#if CONFIG_IS_ENABLED(DM_GPIO_LOOKUP_LABEL)
80/**
81 * dm_gpio_lookup_label() - look for name in gpio device
82 *
83 * search in uc_priv, if there is a gpio with labelname same
84 * as name.
85 *
86 * @name: name which is searched
87 * @uc_priv: gpio_dev_priv pointer.
88 * @offset: gpio offset within the device
89 * @return: 0 if found, -ENOENT if not.
90 */
91static int dm_gpio_lookup_label(const char *name,
92 struct gpio_dev_priv *uc_priv, ulong *offset)
93{
Heiko Schochera3e793c2020-05-22 11:08:59 +020094 int i;
95
96 *offset = -1;
Heiko Schochera3e793c2020-05-22 11:08:59 +020097 for (i = 0; i < uc_priv->gpio_count; i++) {
98 if (!uc_priv->name[i])
99 continue;
Rasmus Villemoes48828082022-10-03 11:02:45 +0200100 if (!strcmp(name, uc_priv->name[i])) {
Heiko Schochera3e793c2020-05-22 11:08:59 +0200101 *offset = i;
102 return 0;
103 }
104 }
105 return -ENOENT;
106}
107#else
108static int
109dm_gpio_lookup_label(const char *name, struct gpio_dev_priv *uc_priv,
110 ulong *offset)
111{
112 return -ENOENT;
113}
114#endif
115
Simon Glass215bcc72015-06-23 15:38:40 -0600116int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -0700117{
Simon Glass43b2e1a2014-10-22 21:37:01 -0600118 struct gpio_dev_priv *uc_priv = NULL;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200119 struct udevice *dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -0600120 ulong offset;
121 int numeric;
Simon Glasse821d182014-02-26 15:59:24 -0700122 int ret;
123
Simon Glassff9b9032021-07-24 09:03:30 -0600124 numeric = isdigit(*name) ? dectoul(name, NULL) : -1;
Simon Glasse821d182014-02-26 15:59:24 -0700125 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
126 dev;
127 ret = uclass_next_device(&dev)) {
Simon Glasse821d182014-02-26 15:59:24 -0700128 int len;
129
Simon Glassde0977b2015-03-05 12:25:20 -0700130 uc_priv = dev_get_uclass_priv(dev);
Simon Glass43b2e1a2014-10-22 21:37:01 -0600131 if (numeric != -1) {
132 offset = numeric - uc_priv->gpio_base;
133 /* Allow GPIOs to be numbered from 0 */
Tom Rini26fd9682017-05-10 15:20:15 -0400134 if (offset < uc_priv->gpio_count)
Simon Glass43b2e1a2014-10-22 21:37:01 -0600135 break;
136 }
137
Simon Glasse821d182014-02-26 15:59:24 -0700138 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
139
Simon Glassd4acf632014-06-11 23:29:47 -0600140 if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glass43b2e1a2014-10-22 21:37:01 -0600141 if (!strict_strtoul(name + len, 10, &offset))
Samuel Holland8f53a332021-09-11 17:05:51 -0500142 if (offset < uc_priv->gpio_count)
143 break;
Simon Glasse821d182014-02-26 15:59:24 -0700144 }
Heiko Schochera3e793c2020-05-22 11:08:59 +0200145
146 /*
147 * if we did not found a gpio through its bank
148 * name, we search for a valid gpio label.
149 */
150 if (!dm_gpio_lookup_label(name, uc_priv, &offset))
151 break;
Simon Glasse821d182014-02-26 15:59:24 -0700152 }
153
Simon Glass43b2e1a2014-10-22 21:37:01 -0600154 if (!dev)
155 return ret ? ret : -EINVAL;
156
Patrick Delaunay758bba92020-01-13 11:35:01 +0100157 gpio_desc_init(desc, dev, offset);
Simon Glass215bcc72015-06-23 15:38:40 -0600158
159 return 0;
160}
161
162int gpio_lookup_name(const char *name, struct udevice **devp,
163 unsigned int *offsetp, unsigned int *gpiop)
164{
165 struct gpio_desc desc;
166 int ret;
167
168 if (devp)
169 *devp = NULL;
170 ret = dm_gpio_lookup_name(name, &desc);
171 if (ret)
172 return ret;
173
Simon Glass43b2e1a2014-10-22 21:37:01 -0600174 if (devp)
Simon Glass215bcc72015-06-23 15:38:40 -0600175 *devp = desc.dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -0600176 if (offsetp)
Simon Glass215bcc72015-06-23 15:38:40 -0600177 *offsetp = desc.offset;
178 if (gpiop) {
179 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
180
181 *gpiop = uc_priv->gpio_base + desc.offset;
182 }
Simon Glass43b2e1a2014-10-22 21:37:01 -0600183
184 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700185}
186
Samuel Holland8e10c712021-09-11 17:05:53 -0500187unsigned long gpio_flags_xlate(uint32_t arg)
188{
189 unsigned long flags = 0;
190
191 if (arg & GPIO_ACTIVE_LOW)
192 flags |= GPIOD_ACTIVE_LOW;
193
194 /*
195 * need to test 2 bits for gpio output binding:
196 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
197 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
198 */
199 if (arg & GPIO_SINGLE_ENDED) {
200 if (arg & GPIO_LINE_OPEN_DRAIN)
201 flags |= GPIOD_OPEN_DRAIN;
202 else
203 flags |= GPIOD_OPEN_SOURCE;
204 }
205
206 if (arg & GPIO_PULL_UP)
207 flags |= GPIOD_PULL_UP;
208
209 if (arg & GPIO_PULL_DOWN)
210 flags |= GPIOD_PULL_DOWN;
211
212 return flags;
213}
214
Simon Glass12faa022017-05-18 20:09:18 -0600215int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
216 struct ofnode_phandle_args *args)
Eric Nelson786e98d2016-04-24 16:32:40 -0700217{
Samuel Holland78acb922021-09-11 17:05:52 -0500218 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
219
Eric Nelson786e98d2016-04-24 16:32:40 -0700220 if (args->args_count < 1)
221 return -EINVAL;
222
223 desc->offset = args->args[0];
Samuel Holland78acb922021-09-11 17:05:52 -0500224 if (desc->offset >= uc_priv->gpio_count)
225 return -EINVAL;
Eric Nelson786e98d2016-04-24 16:32:40 -0700226
227 if (args->args_count < 2)
228 return 0;
229
Samuel Holland8e10c712021-09-11 17:05:53 -0500230 desc->flags = gpio_flags_xlate(args->args[1]);
Patrick Delaunay5c1c06e2020-01-13 11:35:07 +0100231
Eric Nelson786e98d2016-04-24 16:32:40 -0700232 return 0;
233}
234
Simon Glass16e10402015-01-05 20:05:29 -0700235static int gpio_find_and_xlate(struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600236 struct ofnode_phandle_args *args)
Simon Glassd3322bb2015-01-05 20:05:28 -0700237{
Simon Glass49f315a2021-02-04 21:22:05 -0700238 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassd3322bb2015-01-05 20:05:28 -0700239
Eric Nelson786e98d2016-04-24 16:32:40 -0700240 if (ops->xlate)
241 return ops->xlate(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700242 else
Eric Nelson786e98d2016-04-24 16:32:40 -0700243 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700244}
245
Simon Glass2149e112021-08-07 07:24:12 -0600246#if CONFIG_IS_ENABLED(GPIO_HOG)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200247
248struct gpio_hog_priv {
249 struct gpio_desc gpiod;
250};
251
252struct gpio_hog_data {
253 int gpiod_flags;
254 int value;
255 u32 val[2];
256};
257
Simon Glassaad29ae2020-12-03 16:55:21 -0700258static int gpio_hog_of_to_plat(struct udevice *dev)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200259{
Simon Glassfa20e932020-12-03 16:55:20 -0700260 struct gpio_hog_data *plat = dev_get_plat(dev);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200261 const char *nodename;
262 int ret;
263
264 plat->value = 0;
265 if (dev_read_bool(dev, "input")) {
266 plat->gpiod_flags = GPIOD_IS_IN;
267 } else if (dev_read_bool(dev, "output-high")) {
268 plat->value = 1;
269 plat->gpiod_flags = GPIOD_IS_OUT;
270 } else if (dev_read_bool(dev, "output-low")) {
271 plat->gpiod_flags = GPIOD_IS_OUT;
272 } else {
273 printf("%s: missing gpio-hog state.\n", __func__);
274 return -EINVAL;
275 }
276 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
277 if (ret) {
278 printf("%s: wrong gpios property, 2 values needed %d\n",
279 __func__, ret);
280 return ret;
281 }
282 nodename = dev_read_string(dev, "line-name");
Heiko Schocher58e4c382019-07-17 06:59:51 +0200283 if (nodename)
284 device_set_name(dev, nodename);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200285
286 return 0;
287}
288
289static int gpio_hog_probe(struct udevice *dev)
290{
Simon Glassfa20e932020-12-03 16:55:20 -0700291 struct gpio_hog_data *plat = dev_get_plat(dev);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200292 struct gpio_hog_priv *priv = dev_get_priv(dev);
293 int ret;
294
295 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
296 plat->val[0], plat->gpiod_flags,
297 plat->val[1], &priv->gpiod);
298 if (ret < 0) {
299 debug("%s: node %s could not get gpio.\n", __func__,
300 dev->name);
301 return ret;
302 }
Heiko Schocher58e4c382019-07-17 06:59:51 +0200303
304 if (plat->gpiod_flags == GPIOD_IS_OUT) {
305 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
306 if (ret < 0) {
307 debug("%s: node %s could not set gpio.\n", __func__,
308 dev->name);
309 return ret;
310 }
311 }
Heiko Schocher39cb3402019-06-12 06:11:46 +0200312
313 return 0;
314}
315
316int gpio_hog_probe_all(void)
317{
318 struct udevice *dev;
319 int ret;
Heiko Schocher58e4c382019-07-17 06:59:51 +0200320 int retval = 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200321
322 for (uclass_first_device(UCLASS_NOP, &dev);
323 dev;
324 uclass_find_next_device(&dev)) {
Simon Glass65130cd2020-12-28 20:34:56 -0700325 if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
Heiko Schocher39cb3402019-06-12 06:11:46 +0200326 ret = device_probe(dev);
Heiko Schocher58e4c382019-07-17 06:59:51 +0200327 if (ret) {
328 printf("Failed to probe device %s err: %d\n",
329 dev->name, ret);
330 retval = ret;
331 }
Heiko Schocher39cb3402019-06-12 06:11:46 +0200332 }
333 }
334
Heiko Schocher58e4c382019-07-17 06:59:51 +0200335 return retval;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200336}
337
Heiko Schocher58e4c382019-07-17 06:59:51 +0200338int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200339{
340 struct udevice *dev;
341
Heiko Schocher58e4c382019-07-17 06:59:51 +0200342 *desc = NULL;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200343 gpio_hog_probe_all();
344 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
345 struct gpio_hog_priv *priv = dev_get_priv(dev);
346
Heiko Schocher58e4c382019-07-17 06:59:51 +0200347 *desc = &priv->gpiod;
348 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200349 }
350
Heiko Schocher58e4c382019-07-17 06:59:51 +0200351 return -ENODEV;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200352}
353
354U_BOOT_DRIVER(gpio_hog) = {
355 .name = "gpio_hog",
356 .id = UCLASS_NOP,
Simon Glassaad29ae2020-12-03 16:55:21 -0700357 .of_to_plat = gpio_hog_of_to_plat,
Heiko Schocher39cb3402019-06-12 06:11:46 +0200358 .probe = gpio_hog_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700359 .priv_auto = sizeof(struct gpio_hog_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -0700360 .plat_auto = sizeof(struct gpio_hog_data),
Heiko Schocher39cb3402019-06-12 06:11:46 +0200361};
362#else
Heiko Schocher58e4c382019-07-17 06:59:51 +0200363int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200364{
Heiko Schocher58e4c382019-07-17 06:59:51 +0200365 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200366}
367#endif
368
Simon Glass047cdb32015-06-23 15:38:41 -0600369int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassce555292015-01-05 20:05:27 -0700370{
Simon Glass49f315a2021-02-04 21:22:05 -0700371 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700372 struct udevice *dev = desc->dev;
373 struct gpio_dev_priv *uc_priv;
374 char *str;
375 int ret;
376
Simon Glassde0977b2015-03-05 12:25:20 -0700377 uc_priv = dev_get_uclass_priv(dev);
Simon Glassce555292015-01-05 20:05:27 -0700378 if (uc_priv->name[desc->offset])
379 return -EBUSY;
380 str = strdup(label);
381 if (!str)
382 return -ENOMEM;
Simon Glass49f315a2021-02-04 21:22:05 -0700383 if (ops->request) {
384 ret = ops->request(dev, desc->offset, label);
Simon Glassce555292015-01-05 20:05:27 -0700385 if (ret) {
386 free(str);
387 return ret;
388 }
389 }
390 uc_priv->name[desc->offset] = str;
391
392 return 0;
393}
394
Simon Glass16e10402015-01-05 20:05:29 -0700395static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
396{
Simon Glass7611ac62019-09-25 08:56:27 -0600397#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass16e10402015-01-05 20:05:29 -0700398 va_list args;
399 char buf[40];
400
401 va_start(args, fmt);
402 vscnprintf(buf, sizeof(buf), fmt, args);
403 va_end(args);
404 return dm_gpio_request(desc, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700405#else
406 return dm_gpio_request(desc, fmt);
407#endif
Simon Glass16e10402015-01-05 20:05:29 -0700408}
409
Simon Glasse821d182014-02-26 15:59:24 -0700410/**
411 * gpio_request() - [COMPAT] Request GPIO
412 * gpio: GPIO number
413 * label: Name for the requested GPIO
414 *
Simon Glass0f4517d2014-10-04 11:29:42 -0600415 * The label is copied and allocated so the caller does not need to keep
416 * the pointer around.
417 *
Simon Glasse821d182014-02-26 15:59:24 -0700418 * This function implements the API that's compatible with current
419 * GPIO API used in U-Boot. The request is forwarded to particular
420 * GPIO driver. Returns 0 on success, negative value on error.
421 */
422int gpio_request(unsigned gpio, const char *label)
423{
Simon Glassce555292015-01-05 20:05:27 -0700424 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700425 int ret;
426
Simon Glassce555292015-01-05 20:05:27 -0700427 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700428 if (ret)
429 return ret;
430
Simon Glassce555292015-01-05 20:05:27 -0700431 return dm_gpio_request(&desc, label);
Simon Glasse821d182014-02-26 15:59:24 -0700432}
433
434/**
Simon Glass1b27d602014-10-04 11:29:49 -0600435 * gpio_requestf() - [COMPAT] Request GPIO
436 * @gpio: GPIO number
437 * @fmt: Format string for the requested GPIO
438 * @...: Arguments for the printf() format string
439 *
440 * This function implements the API that's compatible with current
441 * GPIO API used in U-Boot. The request is forwarded to particular
442 * GPIO driver. Returns 0 on success, negative value on error.
443 */
444int gpio_requestf(unsigned gpio, const char *fmt, ...)
445{
Simon Glass7611ac62019-09-25 08:56:27 -0600446#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass1b27d602014-10-04 11:29:49 -0600447 va_list args;
448 char buf[40];
449
450 va_start(args, fmt);
451 vscnprintf(buf, sizeof(buf), fmt, args);
452 va_end(args);
453 return gpio_request(gpio, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700454#else
455 return gpio_request(gpio, fmt);
456#endif
Simon Glass1b27d602014-10-04 11:29:49 -0600457}
458
Simon Glassce555292015-01-05 20:05:27 -0700459int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glasse821d182014-02-26 15:59:24 -0700460{
Simon Glass49f315a2021-02-04 21:22:05 -0700461 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600462 struct gpio_dev_priv *uc_priv;
Simon Glasse821d182014-02-26 15:59:24 -0700463 int ret;
464
Simon Glassde0977b2015-03-05 12:25:20 -0700465 uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600466 if (!uc_priv->name[offset])
467 return -ENXIO;
Simon Glass49f315a2021-02-04 21:22:05 -0700468 if (ops->rfree) {
469 ret = ops->rfree(dev, offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600470 if (ret)
471 return ret;
472 }
473
474 free(uc_priv->name[offset]);
475 uc_priv->name[offset] = NULL;
476
477 return 0;
478}
479
Simon Glassce555292015-01-05 20:05:27 -0700480/**
481 * gpio_free() - [COMPAT] Relinquish GPIO
482 * gpio: GPIO number
483 *
484 * This function implements the API that's compatible with current
485 * GPIO API used in U-Boot. The request is forwarded to particular
486 * GPIO driver. Returns 0 on success, negative value on error.
487 */
488int gpio_free(unsigned gpio)
489{
490 struct gpio_desc desc;
491 int ret;
492
493 ret = gpio_to_device(gpio, &desc);
494 if (ret)
495 return ret;
496
497 return _dm_gpio_free(desc.dev, desc.offset);
498}
499
Simon Glassfd838972016-03-06 19:27:51 -0700500static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glass0f4517d2014-10-04 11:29:42 -0600501{
Simon Glass230c1432015-07-02 18:16:16 -0600502 struct gpio_dev_priv *uc_priv;
503
504 if (!dm_gpio_is_valid(desc))
505 return -ENOENT;
Simon Glass0f4517d2014-10-04 11:29:42 -0600506
Simon Glass230c1432015-07-02 18:16:16 -0600507 uc_priv = dev_get_uclass_priv(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700508 if (!uc_priv->name[desc->offset]) {
Simon Glass0f4517d2014-10-04 11:29:42 -0600509 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassce555292015-01-05 20:05:27 -0700510 desc->dev->name, func,
511 uc_priv->bank_name ? uc_priv->bank_name : "",
512 desc->offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600513 return -EBUSY;
514 }
515
516 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700517}
518
519/**
520 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
521 * gpio: GPIO number
522 *
523 * This function implements the API that's compatible with current
524 * GPIO API used in U-Boot. The request is forwarded to particular
525 * GPIO driver. Returns 0 on success, negative value on error.
526 */
527int gpio_direction_input(unsigned gpio)
528{
Simon Glassce555292015-01-05 20:05:27 -0700529 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700530 int ret;
531
Simon Glassce555292015-01-05 20:05:27 -0700532 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700533 if (ret)
534 return ret;
535
Simon Glass722f9682021-02-04 21:22:04 -0700536 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, GPIOD_IS_IN);
Simon Glasse821d182014-02-26 15:59:24 -0700537}
538
539/**
540 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
541 * gpio: GPIO number
542 * value: Logical value to be set on the GPIO pin
543 *
544 * This function implements the API that's compatible with current
545 * GPIO API used in U-Boot. The request is forwarded to particular
546 * GPIO driver. Returns 0 on success, negative value on error.
547 */
548int gpio_direction_output(unsigned gpio, int value)
549{
Simon Glassce555292015-01-05 20:05:27 -0700550 struct gpio_desc desc;
Simon Glass722f9682021-02-04 21:22:04 -0700551 ulong flags;
Simon Glassce555292015-01-05 20:05:27 -0700552 int ret;
553
554 ret = gpio_to_device(gpio, &desc);
555 if (ret)
556 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700557
Simon Glass722f9682021-02-04 21:22:04 -0700558 flags = GPIOD_IS_OUT;
559 if (value)
560 flags |= GPIOD_IS_OUT_ACTIVE;
561 return dm_gpio_clrset_flags(&desc, GPIOD_MASK_DIR, flags);
Simon Glassce555292015-01-05 20:05:27 -0700562}
563
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100564static int _gpio_get_value(const struct gpio_desc *desc)
Simon Glassce555292015-01-05 20:05:27 -0700565{
Simon Glass49f315a2021-02-04 21:22:05 -0700566 const struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700567 int value;
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100568
Simon Glass49f315a2021-02-04 21:22:05 -0700569 value = ops->get_value(desc->dev, desc->offset);
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100570
571 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
572}
573
574int dm_gpio_get_value(const struct gpio_desc *desc)
575{
Simon Glassce555292015-01-05 20:05:27 -0700576 int ret;
577
578 ret = check_reserved(desc, "get_value");
579 if (ret)
580 return ret;
581
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100582 return _gpio_get_value(desc);
Simon Glassce555292015-01-05 20:05:27 -0700583}
584
Simon Glassfd838972016-03-06 19:27:51 -0700585int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassce555292015-01-05 20:05:27 -0700586{
Simon Glass7b893f92021-02-04 21:22:03 -0700587 const struct dm_gpio_ops *ops;
Simon Glassce555292015-01-05 20:05:27 -0700588 int ret;
589
590 ret = check_reserved(desc, "set_value");
591 if (ret)
592 return ret;
593
594 if (desc->flags & GPIOD_ACTIVE_LOW)
595 value = !value;
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200596
Simon Glass7b893f92021-02-04 21:22:03 -0700597 /* GPIOD_ are directly managed by driver in set_flags */
598 ops = gpio_get_ops(desc->dev);
599 if (ops->set_flags) {
600 ulong flags = desc->flags;
601
602 if (value)
603 flags |= GPIOD_IS_OUT_ACTIVE;
604 else
605 flags &= ~GPIOD_IS_OUT_ACTIVE;
606 return ops->set_flags(desc->dev, desc->offset, flags);
607 }
608
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200609 /*
610 * Emulate open drain by not actively driving the line high or
611 * Emulate open source by not actively driving the line low
612 */
613 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
614 (desc->flags & GPIOD_OPEN_SOURCE && !value))
Simon Glass7b893f92021-02-04 21:22:03 -0700615 return ops->direction_input(desc->dev, desc->offset);
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200616 else if (desc->flags & GPIOD_OPEN_DRAIN ||
617 desc->flags & GPIOD_OPEN_SOURCE)
Simon Glass7b893f92021-02-04 21:22:03 -0700618 return ops->direction_output(desc->dev, desc->offset, value);
619
620 ret = ops->set_value(desc->dev, desc->offset, value);
621 if (ret)
622 return ret;
Neil Armstrongf51b8da2020-05-05 10:43:17 +0200623
Simon Glassce555292015-01-05 20:05:27 -0700624 return 0;
625}
626
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100627/* check dir flags invalid configuration */
628static int check_dir_flags(ulong flags)
629{
630 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
631 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
632 __func__, flags);
633 return -EINVAL;
634 }
635
Patrick Delaunay5c1c06e2020-01-13 11:35:07 +0100636 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
637 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
638 __func__, flags);
639 return -EINVAL;
640 }
641
642 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
643 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
644 __func__, flags);
645 return -EINVAL;
646 }
647
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100648 return 0;
649}
650
Simon Glass7b893f92021-02-04 21:22:03 -0700651/**
652 * _dm_gpio_set_flags() - Send flags to the driver
653 *
654 * This uses the best available method to send the given flags to the driver.
655 * Note that if flags & GPIOD_ACTIVE_LOW, the driver sees the opposite value
656 * of GPIOD_IS_OUT_ACTIVE.
657 *
658 * @desc: GPIO description
659 * @flags: flags value to set
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100660 * Return: 0 if OK, -ve on error
Simon Glass7b893f92021-02-04 21:22:03 -0700661 */
Simon Glass54befdd2021-02-04 21:21:55 -0700662static int _dm_gpio_set_flags(struct gpio_desc *desc, ulong flags)
Simon Glassce555292015-01-05 20:05:27 -0700663{
664 struct udevice *dev = desc->dev;
Simon Glass49f315a2021-02-04 21:22:05 -0700665 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100666 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100667 int ret = 0;
Simon Glasse821d182014-02-26 15:59:24 -0700668
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100669 ret = check_dir_flags(flags);
670 if (ret) {
671 dev_dbg(dev,
672 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
673 desc->dev->name,
674 uc_priv->bank_name ? uc_priv->bank_name : "",
675 desc->offset, flags);
676
677 return ret;
678 }
679
Simon Glass7b893f92021-02-04 21:22:03 -0700680 /* If active low, invert the output state */
681 if ((flags & (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW)) ==
682 (GPIOD_IS_OUT | GPIOD_ACTIVE_LOW))
683 flags ^= GPIOD_IS_OUT_ACTIVE;
684
Simon Glass54befdd2021-02-04 21:21:55 -0700685 /* GPIOD_ are directly managed by driver in set_flags */
686 if (ops->set_flags) {
687 ret = ops->set_flags(dev, desc->offset, flags);
Patrick Delaunay684326f2020-01-13 11:35:09 +0100688 } else {
689 if (flags & GPIOD_IS_OUT) {
Simon Glass7b893f92021-02-04 21:22:03 -0700690 bool value = flags & GPIOD_IS_OUT_ACTIVE;
691
692 ret = ops->direction_output(dev, desc->offset, value);
Patrick Delaunay684326f2020-01-13 11:35:09 +0100693 } else if (flags & GPIOD_IS_IN) {
694 ret = ops->direction_input(dev, desc->offset);
695 }
Simon Glassce555292015-01-05 20:05:27 -0700696 }
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100697
698 return ret;
699}
700
Simon Glass7b893f92021-02-04 21:22:03 -0700701int dm_gpio_clrset_flags(struct gpio_desc *desc, ulong clr, ulong set)
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100702{
Simon Glass7b893f92021-02-04 21:22:03 -0700703 ulong flags;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100704 int ret;
705
706 ret = check_reserved(desc, "set_dir_flags");
Simon Glassce555292015-01-05 20:05:27 -0700707 if (ret)
708 return ret;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100709
Simon Glass7b893f92021-02-04 21:22:03 -0700710 flags = (desc->flags & ~clr) | set;
711
Simon Glass54befdd2021-02-04 21:21:55 -0700712 ret = _dm_gpio_set_flags(desc, flags);
Simon Glass7b893f92021-02-04 21:22:03 -0700713 if (ret)
714 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700715
Simon Glass7b893f92021-02-04 21:22:03 -0700716 /* save the flags also in descriptor */
717 desc->flags = flags;
718
719 return 0;
720}
721
722int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
723{
724 /* combine the requested flags (for IN/OUT) and the descriptor flags */
725 return dm_gpio_clrset_flags(desc, GPIOD_MASK_DIR, flags);
Simon Glassce555292015-01-05 20:05:27 -0700726}
727
Simon Glass247ccf22021-02-04 21:22:09 -0700728int dm_gpios_clrset_flags(struct gpio_desc *desc, int count, ulong clr,
729 ulong set)
730{
731 int ret;
732 int i;
733
734 for (i = 0; i < count; i++) {
735 ret = dm_gpio_clrset_flags(&desc[i], clr, set);
736 if (ret)
737 return log_ret(ret);
738 }
739
740 return 0;
741}
742
Simon Glass909bee32021-02-04 21:21:57 -0700743int dm_gpio_get_flags(struct gpio_desc *desc, ulong *flagsp)
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100744{
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100745 struct udevice *dev = desc->dev;
746 int ret, value;
Simon Glass49f315a2021-02-04 21:22:05 -0700747 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glassd063ce92021-02-04 21:21:56 -0700748 ulong flags;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100749
Simon Glassd063ce92021-02-04 21:21:56 -0700750 ret = check_reserved(desc, "get_flags");
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100751 if (ret)
752 return ret;
753
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100754 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
Simon Glassd063ce92021-02-04 21:21:56 -0700755 if (ops->get_flags) {
756 ret = ops->get_flags(dev, desc->offset, &flags);
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100757 if (ret)
758 return ret;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100759
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100760 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
Simon Glassd063ce92021-02-04 21:21:56 -0700761 value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100762 if (desc->flags & GPIOD_ACTIVE_LOW)
763 value = !value;
Simon Glassd063ce92021-02-04 21:21:56 -0700764 flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
765 flags |= (desc->flags & GPIOD_ACTIVE_LOW);
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100766 if (value)
Simon Glassd063ce92021-02-04 21:21:56 -0700767 flags |= GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100768 } else {
Simon Glassd063ce92021-02-04 21:21:56 -0700769 flags = desc->flags;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100770 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
Simon Glassd063ce92021-02-04 21:21:56 -0700771 flags &= ~GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100772 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
Simon Glassd063ce92021-02-04 21:21:56 -0700773 flags |= GPIOD_IS_OUT_ACTIVE;
Patrick Delaunayfbacd622020-01-13 11:35:08 +0100774 }
Simon Glassd063ce92021-02-04 21:21:56 -0700775 *flagsp = flags;
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100776
777 return 0;
778}
779
Simon Glasse821d182014-02-26 15:59:24 -0700780/**
781 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
782 * gpio: GPIO number
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 the value of the GPIO pin, or negative value
787 * on error.
788 */
789int gpio_get_value(unsigned gpio)
790{
Simon Glasse821d182014-02-26 15:59:24 -0700791 int ret;
792
Simon Glassce555292015-01-05 20:05:27 -0700793 struct gpio_desc desc;
794
795 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700796 if (ret)
797 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700798 return dm_gpio_get_value(&desc);
Simon Glasse821d182014-02-26 15:59:24 -0700799}
800
801/**
802 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
803 * gpio: GPIO number
804 * value: Logical value to be set on the GPIO pin.
805 *
806 * This function implements the API that's compatible with current
807 * GPIO API used in U-Boot. The request is forwarded to particular
808 * GPIO driver. Returns 0 on success, negative value on error.
809 */
810int gpio_set_value(unsigned gpio, int value)
811{
Simon Glassce555292015-01-05 20:05:27 -0700812 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700813 int ret;
814
Simon Glassce555292015-01-05 20:05:27 -0700815 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700816 if (ret)
817 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700818 return dm_gpio_set_value(&desc, value);
Simon Glasse821d182014-02-26 15:59:24 -0700819}
820
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200821const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glasse821d182014-02-26 15:59:24 -0700822{
823 struct gpio_dev_priv *priv;
824
825 /* Must be called on an active device */
Simon Glassde0977b2015-03-05 12:25:20 -0700826 priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -0700827 assert(priv);
828
829 *bit_count = priv->gpio_count;
830 return priv->bank_name;
831}
832
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600833static const char * const gpio_function[GPIOF_COUNT] = {
834 "input",
835 "output",
836 "unused",
837 "unknown",
838 "func",
839};
840
Masahiro Yamada286c2522017-06-22 16:50:25 +0900841static int get_function(struct udevice *dev, int offset, bool skip_unused,
842 const char **namep)
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600843{
Simon Glassde0977b2015-03-05 12:25:20 -0700844 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass49f315a2021-02-04 21:22:05 -0700845 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600846
847 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
848 if (!device_active(dev))
849 return -ENODEV;
850 if (offset < 0 || offset >= uc_priv->gpio_count)
851 return -EINVAL;
852 if (namep)
853 *namep = uc_priv->name[offset];
854 if (skip_unused && !uc_priv->name[offset])
855 return GPIOF_UNUSED;
856 if (ops->get_function) {
857 int ret;
858
859 ret = ops->get_function(dev, offset);
860 if (ret < 0)
861 return ret;
862 if (ret >= ARRAY_SIZE(gpio_function))
863 return -ENODATA;
864 return ret;
865 }
866
867 return GPIOF_UNKNOWN;
868}
869
870int gpio_get_function(struct udevice *dev, int offset, const char **namep)
871{
872 return get_function(dev, offset, true, namep);
873}
874
875int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
876{
877 return get_function(dev, offset, false, namep);
878}
879
Simon Glass6b1ef592014-10-04 11:29:44 -0600880int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
881{
Simon Glass49f315a2021-02-04 21:22:05 -0700882 const struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glass6b1ef592014-10-04 11:29:44 -0600883 struct gpio_dev_priv *priv;
884 char *str = buf;
Patrice Chotard8b1310e2022-08-30 14:09:11 +0200885 const char *label;
Simon Glass6b1ef592014-10-04 11:29:44 -0600886 int func;
887 int ret;
888 int len;
Patrice Chotard8b1310e2022-08-30 14:09:11 +0200889 bool used;
Simon Glass6b1ef592014-10-04 11:29:44 -0600890
891 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
892
893 *buf = 0;
Simon Glassde0977b2015-03-05 12:25:20 -0700894 priv = dev_get_uclass_priv(dev);
Patrice Chotard8b1310e2022-08-30 14:09:11 +0200895 ret = gpio_get_raw_function(dev, offset, &label);
Simon Glass6b1ef592014-10-04 11:29:44 -0600896 if (ret < 0)
897 return ret;
898 func = ret;
899 len = snprintf(str, buffsize, "%s%d: %s",
900 priv->bank_name ? priv->bank_name : "",
901 offset, gpio_function[func]);
Simon Glass6b1ef592014-10-04 11:29:44 -0600902
Patrice Chotard8b1310e2022-08-30 14:09:11 +0200903 switch (func) {
904 case GPIOF_FUNC:
905 snprintf(str + len, buffsize - len, " %s", label ? label : "");
906 break;
907 case GPIOF_INPUT:
908 case GPIOF_OUTPUT:
909 case GPIOF_UNUSED:
Simon Glass6b1ef592014-10-04 11:29:44 -0600910 ret = ops->get_value(dev, offset);
911 if (ret < 0)
912 return ret;
913 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
914 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
915 ret,
916 used ? 'x' : ' ',
Patrice Chotardeb8f3a72022-08-30 14:09:12 +0200917 label ? " " : "",
Simon Glass6b1ef592014-10-04 11:29:44 -0600918 label ? label : "");
Patrice Chotard8b1310e2022-08-30 14:09:11 +0200919 break;
Simon Glass6b1ef592014-10-04 11:29:44 -0600920 }
921
922 return 0;
923}
924
Simon Glass3176b6c2020-07-07 13:11:44 -0600925#if CONFIG_IS_ENABLED(ACPIGEN)
926int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
927{
Simon Glass49f315a2021-02-04 21:22:05 -0700928 const struct dm_gpio_ops *ops;
Simon Glass3176b6c2020-07-07 13:11:44 -0600929
930 memset(gpio, '\0', sizeof(*gpio));
931 if (!dm_gpio_is_valid(desc)) {
932 /* Indicate that the GPIO is not valid */
933 gpio->pin_count = 0;
934 gpio->pins[0] = 0;
935 return -EINVAL;
936 }
937
938 ops = gpio_get_ops(desc->dev);
939 if (!ops->get_acpi)
940 return -ENOSYS;
941
942 return ops->get_acpi(desc, gpio);
943}
944#endif
945
Simon Glassbef54db2015-04-14 21:03:20 -0600946int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
947{
948 int i, ret;
949 int gpio;
950
951 for (i = 0; i < 32; i++) {
952 gpio = gpio_num_array[i];
953 if (gpio == -1)
954 break;
955 ret = gpio_requestf(gpio, fmt, i);
956 if (ret)
957 goto err;
958 ret = gpio_direction_input(gpio);
959 if (ret) {
960 gpio_free(gpio);
961 goto err;
962 }
963 }
964
965 return 0;
966err:
967 for (i--; i >= 0; i--)
968 gpio_free(gpio_num_array[i]);
969
970 return ret;
971}
972
Simon Glass2c97a8f2014-11-10 18:00:21 -0700973/*
974 * get a number comprised of multiple GPIO values. gpio_num_array points to
975 * the array of gpio pin numbers to scan, terminated by -1.
976 */
Simon Glassbef54db2015-04-14 21:03:20 -0600977int gpio_get_values_as_int(const int *gpio_list)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700978{
979 int gpio;
980 unsigned bitmask = 1;
981 unsigned vector = 0;
Simon Glassbef54db2015-04-14 21:03:20 -0600982 int ret;
Simon Glass2c97a8f2014-11-10 18:00:21 -0700983
984 while (bitmask &&
Simon Glassbef54db2015-04-14 21:03:20 -0600985 ((gpio = *gpio_list++) != -1)) {
986 ret = gpio_get_value(gpio);
987 if (ret < 0)
988 return ret;
989 else if (ret)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700990 vector |= bitmask;
991 bitmask <<= 1;
992 }
Simon Glassbef54db2015-04-14 21:03:20 -0600993
Simon Glass2c97a8f2014-11-10 18:00:21 -0700994 return vector;
995}
996
Simon Glassfd838972016-03-06 19:27:51 -0700997int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassdf1687d2016-03-06 19:27:50 -0700998{
999 unsigned bitmask = 1;
1000 unsigned vector = 0;
1001 int ret, i;
1002
1003 for (i = 0; i < count; i++) {
1004 ret = dm_gpio_get_value(&desc_list[i]);
1005 if (ret < 0)
1006 return ret;
1007 else if (ret)
1008 vector |= bitmask;
1009 bitmask <<= 1;
1010 }
Simon Glass247ccf22021-02-04 21:22:09 -07001011
1012 return vector;
1013}
1014
1015int dm_gpio_get_values_as_int_base3(struct gpio_desc *desc_list,
1016 int count)
1017{
1018 static const char tristate[] = "01z";
1019 enum {
1020 PULLUP,
1021 PULLDOWN,
1022
1023 NUM_OPTIONS,
1024 };
1025 int vals[NUM_OPTIONS];
1026 uint mask;
1027 uint vector = 0;
1028 int ret, i;
1029
1030 /*
1031 * Limit to 19 digits which should be plenty. This avoids overflow of a
1032 * 32-bit int
1033 */
1034 assert(count < 20);
1035
1036 for (i = 0; i < NUM_OPTIONS; i++) {
1037 uint flags = GPIOD_IS_IN;
1038
1039 flags |= (i == PULLDOWN) ? GPIOD_PULL_DOWN : GPIOD_PULL_UP;
1040 ret = dm_gpios_clrset_flags(desc_list, count, GPIOD_MASK_PULL,
1041 flags);
1042 if (ret)
1043 return log_msg_ret("pu", ret);
1044
1045 /* Give the lines time to settle */
1046 udelay(10);
1047
1048 ret = dm_gpio_get_values_as_int(desc_list, count);
1049 if (ret < 0)
1050 return log_msg_ret("get1", ret);
1051 vals[i] = ret;
1052 }
1053
1054 log_debug("values: %x %x, count = %d\n", vals[0], vals[1], count);
1055 for (i = count - 1, mask = 1 << i; i >= 0; i--, mask >>= 1) {
1056 uint pd = vals[PULLDOWN] & mask ? 1 : 0;
1057 uint pu = vals[PULLUP] & mask ? 1 : 0;
1058 uint digit;
1059
1060 /*
1061 * Get value with internal pulldown active. If this is 1 then
1062 * there is a stronger external pullup, which we call 1. If not
1063 * then call it 0.
1064 *
1065 * If the values differ then the pin is floating so we call
1066 * this a 2.
1067 */
1068 if (pu == pd)
1069 digit = pd;
1070 else
1071 digit = 2;
1072 log_debug("%c ", tristate[digit]);
1073 vector = 3 * vector + digit;
1074 }
1075 log_debug("vector=%d\n", vector);
Simon Glassdf1687d2016-03-06 19:27:50 -07001076
1077 return vector;
1078}
1079
Heiko Schocher58e4c382019-07-17 06:59:51 +02001080/**
1081 * gpio_request_tail: common work for requesting a gpio.
1082 *
1083 * ret: return value from previous work in function which calls
1084 * this function.
1085 * This seems bogus (why calling this function instead not
1086 * calling it and end caller function instead?).
1087 * Because on error in caller function we want to set some
1088 * default values in gpio desc and have a common error
1089 * debug message, which provides this function.
1090 * nodename: Name of node for which gpio gets requested
1091 * used for gpio label name.
1092 * args: pointer to output arguments structure
1093 * list_name: Name of GPIO list
1094 * used for gpio label name.
1095 * index: gpio index in gpio list
1096 * used for gpio label name.
1097 * desc: pointer to gpio descriptor, filled from this
1098 * function.
1099 * flags: gpio flags to use.
1100 * add_index: should index added to gpio label name
1101 * gpio_dev: pointer to gpio device from which the gpio
1102 * will be requested. If NULL try to get the
1103 * gpio device with uclass_get_device_by_ofnode()
1104 *
1105 * return: In error case this function sets default values in
1106 * gpio descriptor, also emmits a debug message.
1107 * On success it returns 0 else the error code from
1108 * function calls, or the error code passed through
1109 * ret to this function.
1110 *
1111 */
Heiko Schocher39cb3402019-06-12 06:11:46 +02001112static int gpio_request_tail(int ret, const char *nodename,
Simon Glass12faa022017-05-18 20:09:18 -06001113 struct ofnode_phandle_args *args,
1114 const char *list_name, int index,
Heiko Schocher39cb3402019-06-12 06:11:46 +02001115 struct gpio_desc *desc, int flags,
Heiko Schocher58e4c382019-07-17 06:59:51 +02001116 bool add_index, struct udevice *gpio_dev)
Simon Glass16e10402015-01-05 20:05:29 -07001117{
Patrick Delaunay758bba92020-01-13 11:35:01 +01001118 gpio_desc_init(desc, gpio_dev, 0);
Simon Glass12faa022017-05-18 20:09:18 -06001119 if (ret)
Simon Glass16e10402015-01-05 20:05:29 -07001120 goto err;
Simon Glass16e10402015-01-05 20:05:29 -07001121
Heiko Schocher39cb3402019-06-12 06:11:46 +02001122 if (!desc->dev) {
1123 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
1124 &desc->dev);
1125 if (ret) {
Heiko Schocher58e4c382019-07-17 06:59:51 +02001126 debug("%s: uclass_get_device_by_ofnode failed\n",
1127 __func__);
Heiko Schocher39cb3402019-06-12 06:11:46 +02001128 goto err;
1129 }
Simon Glass16e10402015-01-05 20:05:29 -07001130 }
Simon Glass12faa022017-05-18 20:09:18 -06001131 ret = gpio_find_and_xlate(desc, args);
Simon Glass16e10402015-01-05 20:05:29 -07001132 if (ret) {
1133 debug("%s: gpio_find_and_xlate failed\n", __func__);
1134 goto err;
1135 }
1136 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Heiko Schocher39cb3402019-06-12 06:11:46 +02001137 nodename, list_name, index);
Simon Glass16e10402015-01-05 20:05:29 -07001138 if (ret) {
1139 debug("%s: dm_gpio_requestf failed\n", __func__);
1140 goto err;
1141 }
Simon Glass7b893f92021-02-04 21:22:03 -07001142
1143 /* Keep any direction flags provided by the devicetree */
1144 ret = dm_gpio_set_dir_flags(desc,
1145 flags | (desc->flags & GPIOD_MASK_DIR));
Simon Glass16e10402015-01-05 20:05:29 -07001146 if (ret) {
1147 debug("%s: dm_gpio_set_dir failed\n", __func__);
1148 goto err;
1149 }
1150
1151 return 0;
1152err:
1153 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Heiko Schocher39cb3402019-06-12 06:11:46 +02001154 __func__, nodename, list_name, index, ret);
Simon Glass16e10402015-01-05 20:05:29 -07001155 return ret;
1156}
1157
Simon Glass92882652021-08-07 07:24:04 -06001158#if CONFIG_IS_ENABLED(OF_REAL)
Simon Glass1d9af1f2017-05-30 21:47:09 -06001159static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1160 int index, struct gpio_desc *desc,
1161 int flags, bool add_index)
Simon Glass12faa022017-05-18 20:09:18 -06001162{
1163 struct ofnode_phandle_args args;
1164 int ret;
1165
Simon Glass1d9af1f2017-05-30 21:47:09 -06001166 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1167 index, &args);
Simon Glass12faa022017-05-18 20:09:18 -06001168
Heiko Schocher39cb3402019-06-12 06:11:46 +02001169 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1170 index, desc, flags, add_index, NULL);
Simon Glass12faa022017-05-18 20:09:18 -06001171}
1172
Simon Glass1d9af1f2017-05-30 21:47:09 -06001173int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -07001174 struct gpio_desc *desc, int flags)
1175{
Simon Glass1d9af1f2017-05-30 21:47:09 -06001176 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1177 index > 0);
Simon Glass16e10402015-01-05 20:05:29 -07001178}
1179
Simon Glass1d9af1f2017-05-30 21:47:09 -06001180int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -07001181 struct gpio_desc *desc, int flags)
1182{
Simon Glass1d9af1f2017-05-30 21:47:09 -06001183 struct ofnode_phandle_args args;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001184 ofnode node;
Simon Glass1d9af1f2017-05-30 21:47:09 -06001185 int ret;
1186
1187 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1188 index, &args);
Heiko Schocher39cb3402019-06-12 06:11:46 +02001189 node = dev_ofnode(dev);
1190 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1191 index, desc, flags, index > 0, NULL);
Simon Glass16e10402015-01-05 20:05:29 -07001192}
1193
Andrew Jeffery2e62c6b2022-01-31 13:54:05 +10301194int gpio_request_by_line_name(struct udevice *dev, const char *line_name,
1195 struct gpio_desc *desc, int flags)
1196{
1197 int ret;
1198
1199 ret = dev_read_stringlist_search(dev, "gpio-line-names", line_name);
1200 if (ret < 0)
1201 return ret;
1202
1203 desc->dev = dev;
1204 desc->offset = ret;
1205 desc->flags = 0;
1206
1207 ret = dm_gpio_request(desc, line_name);
1208 if (ret) {
1209 debug("%s: dm_gpio_requestf failed\n", __func__);
1210 return ret;
1211 }
1212
1213 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
1214 if (ret)
1215 debug("%s: dm_gpio_set_dir failed\n", __func__);
1216
1217 return ret;
1218}
1219
Simon Glass1d9af1f2017-05-30 21:47:09 -06001220int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass16e10402015-01-05 20:05:29 -07001221 struct gpio_desc *desc, int max_count,
1222 int flags)
1223{
1224 int count;
1225 int ret;
1226
Przemyslaw Marczak44fc5362015-03-31 18:57:16 +02001227 for (count = 0; count < max_count; count++) {
Simon Glass1d9af1f2017-05-30 21:47:09 -06001228 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass16e10402015-01-05 20:05:29 -07001229 &desc[count], flags, true);
1230 if (ret == -ENOENT)
1231 break;
1232 else if (ret)
1233 goto err;
1234 }
1235
1236 /* We ran out of GPIOs in the list */
1237 return count;
1238
1239err:
1240 gpio_free_list_nodev(desc, count - 1);
1241
1242 return ret;
1243}
1244
1245int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1246 struct gpio_desc *desc, int max_count,
1247 int flags)
1248{
1249 /*
1250 * This isn't ideal since we don't use dev->name in the debug()
1251 * calls in gpio_request_by_name(), but we can do this until
1252 * gpio_request_list_by_name_nodev() can be dropped.
1253 */
Simon Glass1d9af1f2017-05-30 21:47:09 -06001254 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1255 max_count, flags);
Simon Glass16e10402015-01-05 20:05:29 -07001256}
1257
1258int gpio_get_list_count(struct udevice *dev, const char *list_name)
1259{
1260 int ret;
1261
Sean Anderson8ad66c52021-04-20 10:50:54 -04001262 ret = dev_count_phandle_with_args(dev, list_name, "#gpio-cells",
1263 -ENOENT);
1264 if (ret < 0) {
Simon Glass16e10402015-01-05 20:05:29 -07001265 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1266 __func__, dev->name, list_name, ret);
1267 }
1268
1269 return ret;
1270}
Simon Glassea383722021-02-04 21:21:54 -07001271#endif /* OF_PLATDATA */
Simon Glass16e10402015-01-05 20:05:29 -07001272
Simon Glass2149e112021-08-07 07:24:12 -06001273#if CONFIG_IS_ENABLED(OF_PLATDATA)
1274int gpio_request_by_phandle(struct udevice *dev,
1275 const struct phandle_2_arg *cells,
1276 struct gpio_desc *desc, int flags)
1277{
1278 struct ofnode_phandle_args args;
1279 struct udevice *gpio_dev;
1280 const int index = 0;
1281 int ret;
1282
1283 ret = device_get_by_ofplat_idx(cells->idx, &gpio_dev);
1284 if (ret)
1285 return ret;
1286 args.args[0] = cells->arg[0];
1287 args.args[1] = cells->arg[1];
1288
1289 return gpio_request_tail(ret, NULL, &args, NULL, index, desc, flags,
1290 index > 0, gpio_dev);
1291}
1292#endif
1293
Simon Glass16e10402015-01-05 20:05:29 -07001294int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1295{
1296 /* For now, we don't do any checking of dev */
1297 return _dm_gpio_free(desc->dev, desc->offset);
1298}
1299
1300int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1301{
1302 int i;
1303
1304 /* For now, we don't do any checking of dev */
1305 for (i = 0; i < count; i++)
1306 dm_gpio_free(dev, &desc[i]);
1307
1308 return 0;
1309}
1310
1311int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1312{
1313 return gpio_free_list(NULL, desc, count);
1314}
1315
Simon Glasse821d182014-02-26 15:59:24 -07001316/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glass0f4517d2014-10-04 11:29:42 -06001317static int gpio_renumber(struct udevice *removed_dev)
Simon Glasse821d182014-02-26 15:59:24 -07001318{
1319 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001320 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -07001321 struct uclass *uc;
1322 unsigned base;
1323 int ret;
1324
1325 ret = uclass_get(UCLASS_GPIO, &uc);
1326 if (ret)
1327 return ret;
1328
1329 /* Ensure that we have a base for each bank */
1330 base = 0;
1331 uclass_foreach_dev(dev, uc) {
Simon Glass0f4517d2014-10-04 11:29:42 -06001332 if (device_active(dev) && dev != removed_dev) {
Simon Glassde0977b2015-03-05 12:25:20 -07001333 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001334 uc_priv->gpio_base = base;
1335 base += uc_priv->gpio_count;
1336 }
1337 }
1338
1339 return 0;
1340}
1341
Simon Glassfd838972016-03-06 19:27:51 -07001342int gpio_get_number(const struct gpio_desc *desc)
Simon Glass94f54d12015-03-25 12:21:58 -06001343{
1344 struct udevice *dev = desc->dev;
1345 struct gpio_dev_priv *uc_priv;
1346
1347 if (!dev)
1348 return -1;
Simon Glass95588622020-12-22 19:30:28 -07001349 uc_priv = dev_get_uclass_priv(dev);
Simon Glass94f54d12015-03-25 12:21:58 -06001350
1351 return uc_priv->gpio_base + desc->offset;
1352}
1353
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001354static int gpio_post_probe(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001355{
Simon Glassde0977b2015-03-05 12:25:20 -07001356 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001357
1358 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1359 if (!uc_priv->name)
1360 return -ENOMEM;
1361
1362 return gpio_renumber(NULL);
Simon Glasse821d182014-02-26 15:59:24 -07001363}
1364
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001365static int gpio_pre_remove(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001366{
Simon Glassde0977b2015-03-05 12:25:20 -07001367 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001368 int i;
1369
1370 for (i = 0; i < uc_priv->gpio_count; i++) {
1371 if (uc_priv->name[i])
1372 free(uc_priv->name[i]);
1373 }
1374 free(uc_priv->name);
1375
1376 return gpio_renumber(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001377}
1378
Heiko Schocher39cb3402019-06-12 06:11:46 +02001379int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1380 char *list_name, int index, int flags,
1381 int dtflags, struct gpio_desc *desc)
1382{
1383 struct ofnode_phandle_args args;
1384
1385 args.node = ofnode_null();
1386 args.args_count = 2;
1387 args.args[0] = index;
1388 args.args[1] = dtflags;
1389
1390 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1391 flags, 0, dev);
1392}
1393
Jean-Jacques Hiblot775d6a22020-09-11 13:43:34 +05301394static void devm_gpiod_release(struct udevice *dev, void *res)
1395{
1396 dm_gpio_free(dev, res);
1397}
1398
1399static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1400{
1401 return res == data;
1402}
1403
1404struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1405 unsigned int index, int flags)
1406{
1407 int rc;
1408 struct gpio_desc *desc;
1409 char *propname;
1410 static const char suffix[] = "-gpios";
1411
1412 propname = malloc(strlen(id) + sizeof(suffix));
1413 if (!propname) {
1414 rc = -ENOMEM;
1415 goto end;
1416 }
1417
1418 strcpy(propname, id);
1419 strcat(propname, suffix);
1420
1421 desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1422 __GFP_ZERO);
1423 if (unlikely(!desc)) {
1424 rc = -ENOMEM;
1425 goto end;
1426 }
1427
1428 rc = gpio_request_by_name(dev, propname, index, desc, flags);
1429
1430end:
1431 if (propname)
1432 free(propname);
1433
1434 if (rc)
1435 return ERR_PTR(rc);
1436
1437 devres_add(dev, desc);
1438
1439 return desc;
1440}
1441
1442struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1443 const char *id,
1444 unsigned int index,
1445 int flags)
1446{
1447 struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1448
1449 if (IS_ERR(desc))
1450 return NULL;
1451
1452 return desc;
1453}
1454
1455void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1456{
1457 int rc;
1458
1459 rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1460 WARN_ON(rc);
1461}
1462
Michal Simek5f7202c2018-07-12 12:42:27 +02001463static int gpio_post_bind(struct udevice *dev)
1464{
1465#if defined(CONFIG_NEEDS_MANUAL_RELOC)
1466 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1467 static int reloc_done;
1468
1469 if (!reloc_done) {
1470 if (ops->request)
1471 ops->request += gd->reloc_off;
Simon Glassb3a47542020-02-04 20:15:17 -07001472 if (ops->rfree)
1473 ops->rfree += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001474 if (ops->direction_input)
1475 ops->direction_input += gd->reloc_off;
1476 if (ops->direction_output)
1477 ops->direction_output += gd->reloc_off;
1478 if (ops->get_value)
1479 ops->get_value += gd->reloc_off;
1480 if (ops->set_value)
1481 ops->set_value += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001482 if (ops->get_function)
1483 ops->get_function += gd->reloc_off;
1484 if (ops->xlate)
1485 ops->xlate += gd->reloc_off;
Simon Glass54befdd2021-02-04 21:21:55 -07001486 if (ops->set_flags)
1487 ops->set_flags += gd->reloc_off;
Simon Glassd063ce92021-02-04 21:21:56 -07001488 if (ops->get_flags)
1489 ops->get_flags += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001490
1491 reloc_done++;
1492 }
1493#endif
Heiko Schocher39cb3402019-06-12 06:11:46 +02001494
Eddie James652b8692022-02-07 17:09:01 -06001495 if (CONFIG_IS_ENABLED(GPIO_HOG)) {
1496 struct udevice *child;
1497 ofnode node;
1498
Heiko Schocher58e4c382019-07-17 06:59:51 +02001499 dev_for_each_subnode(node, dev) {
1500 if (ofnode_read_bool(node, "gpio-hog")) {
1501 const char *name = ofnode_get_name(node);
1502 int ret;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001503
Heiko Schocher58e4c382019-07-17 06:59:51 +02001504 ret = device_bind_driver_to_node(dev,
1505 "gpio_hog",
1506 name, node,
1507 &child);
1508 if (ret)
1509 return ret;
1510 }
Heiko Schocher39cb3402019-06-12 06:11:46 +02001511 }
1512 }
Michal Simek5f7202c2018-07-12 12:42:27 +02001513 return 0;
1514}
1515
Simon Glasse821d182014-02-26 15:59:24 -07001516UCLASS_DRIVER(gpio) = {
1517 .id = UCLASS_GPIO,
1518 .name = "gpio",
Bhuvanchandra DVb1a1fc92015-06-01 18:37:15 +05301519 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glasse821d182014-02-26 15:59:24 -07001520 .post_probe = gpio_post_probe,
Michal Simek5f7202c2018-07-12 12:42:27 +02001521 .post_bind = gpio_post_bind,
Simon Glasse821d182014-02-26 15:59:24 -07001522 .pre_remove = gpio_pre_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001523 .per_device_auto = sizeof(struct gpio_dev_priv),
Simon Glasse821d182014-02-26 15:59:24 -07001524};