blob: 0a22441d38a4eeb99818b45ba80dbc2430809386 [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>
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02008#include <dm/device-internal.h>
9#include <dm/lists.h>
10#include <dm/uclass-internal.h>
Eric Nelson6c880b72016-04-24 16:32:40 -070011#include <dt-bindings/gpio/gpio.h>
Simon Glass96495d92014-02-26 15:59:24 -070012#include <errno.h>
Simon Glass0dac4d52015-01-05 20:05:28 -070013#include <fdtdec.h>
Simon Glassb892d122014-10-04 11:29:42 -060014#include <malloc.h>
Simon Glass96495d92014-02-26 15:59:24 -070015#include <asm/gpio.h>
Masahiro Yamada84b8bf62016-01-24 23:27:48 +090016#include <linux/bug.h>
Simon Glassfe1ef502014-10-22 21:37:01 -060017#include <linux/ctype.h>
Simon Glass96495d92014-02-26 15:59:24 -070018
Simon Glass3669e0e2015-01-05 20:05:29 -070019DECLARE_GLOBAL_DATA_PTR;
20
Simon Glass96495d92014-02-26 15:59:24 -070021/**
22 * gpio_to_device() - Convert global GPIO number to device, number
Simon Glass96495d92014-02-26 15:59:24 -070023 *
24 * Convert the GPIO number to an entry in the list of GPIOs
25 * or GPIO blocks registered with the GPIO controller. Returns
26 * entry on success, NULL on error.
Simon Glassae7123f2015-01-05 20:05:27 -070027 *
28 * @gpio: The numeric representation of the GPIO
29 * @desc: Returns description (desc->flags will always be 0)
30 * @return 0 if found, -ENOENT if not found
Simon Glass96495d92014-02-26 15:59:24 -070031 */
Simon Glassae7123f2015-01-05 20:05:27 -070032static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glass96495d92014-02-26 15:59:24 -070033{
34 struct gpio_dev_priv *uc_priv;
Heiko Schocher54c5d082014-05-22 12:43:05 +020035 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -070036 int ret;
37
38 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
39 dev;
40 ret = uclass_next_device(&dev)) {
Simon Glasse564f052015-03-05 12:25:20 -070041 uc_priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -070042 if (gpio >= uc_priv->gpio_base &&
43 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
Simon Glassae7123f2015-01-05 20:05:27 -070044 desc->dev = dev;
45 desc->offset = gpio - uc_priv->gpio_base;
46 desc->flags = 0;
Simon Glass96495d92014-02-26 15:59:24 -070047 return 0;
48 }
49 }
50
51 /* No such GPIO */
Simon Glassae7123f2015-01-05 20:05:27 -070052 return ret ? ret : -ENOENT;
Simon Glass96495d92014-02-26 15:59:24 -070053}
54
Simon Glass32ec1592015-06-23 15:38:40 -060055int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
Simon Glass96495d92014-02-26 15:59:24 -070056{
Simon Glassfe1ef502014-10-22 21:37:01 -060057 struct gpio_dev_priv *uc_priv = NULL;
Heiko Schocher54c5d082014-05-22 12:43:05 +020058 struct udevice *dev;
Simon Glassfe1ef502014-10-22 21:37:01 -060059 ulong offset;
60 int numeric;
Simon Glass96495d92014-02-26 15:59:24 -070061 int ret;
62
Simon Glassfe1ef502014-10-22 21:37:01 -060063 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
Simon Glass96495d92014-02-26 15:59:24 -070064 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
65 dev;
66 ret = uclass_next_device(&dev)) {
Simon Glass96495d92014-02-26 15:59:24 -070067 int len;
68
Simon Glasse564f052015-03-05 12:25:20 -070069 uc_priv = dev_get_uclass_priv(dev);
Simon Glassfe1ef502014-10-22 21:37:01 -060070 if (numeric != -1) {
71 offset = numeric - uc_priv->gpio_base;
72 /* Allow GPIOs to be numbered from 0 */
Tom Rini75897912017-05-10 15:20:15 -040073 if (offset < uc_priv->gpio_count)
Simon Glassfe1ef502014-10-22 21:37:01 -060074 break;
75 }
76
Simon Glass96495d92014-02-26 15:59:24 -070077 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
78
Simon Glass939cda52014-06-11 23:29:47 -060079 if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glassfe1ef502014-10-22 21:37:01 -060080 if (!strict_strtoul(name + len, 10, &offset))
81 break;
Simon Glass96495d92014-02-26 15:59:24 -070082 }
83 }
84
Simon Glassfe1ef502014-10-22 21:37:01 -060085 if (!dev)
86 return ret ? ret : -EINVAL;
87
Simon Glass32ec1592015-06-23 15:38:40 -060088 desc->dev = dev;
89 desc->offset = offset;
90
91 return 0;
92}
93
94int gpio_lookup_name(const char *name, struct udevice **devp,
95 unsigned int *offsetp, unsigned int *gpiop)
96{
97 struct gpio_desc desc;
98 int ret;
99
Simon Glassfe1ef502014-10-22 21:37:01 -0600100 if (devp)
Simon Glass32ec1592015-06-23 15:38:40 -0600101 *devp = NULL;
102 ret = dm_gpio_lookup_name(name, &desc);
103 if (ret)
104 return ret;
105
106 if (devp)
107 *devp = desc.dev;
Simon Glassfe1ef502014-10-22 21:37:01 -0600108 if (offsetp)
Simon Glass32ec1592015-06-23 15:38:40 -0600109 *offsetp = desc.offset;
110 if (gpiop) {
111 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
112
113 *gpiop = uc_priv->gpio_base + desc.offset;
114 }
Simon Glassfe1ef502014-10-22 21:37:01 -0600115
116 return 0;
Simon Glass96495d92014-02-26 15:59:24 -0700117}
118
Simon Glass3a571232017-05-18 20:09:18 -0600119int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
120 struct ofnode_phandle_args *args)
Eric Nelson6c880b72016-04-24 16:32:40 -0700121{
122 if (args->args_count < 1)
123 return -EINVAL;
124
125 desc->offset = args->args[0];
126
127 if (args->args_count < 2)
128 return 0;
129
130 if (args->args[1] & GPIO_ACTIVE_LOW)
131 desc->flags = GPIOD_ACTIVE_LOW;
132
133 return 0;
134}
135
Simon Glass3669e0e2015-01-05 20:05:29 -0700136static int gpio_find_and_xlate(struct gpio_desc *desc,
Simon Glass3a571232017-05-18 20:09:18 -0600137 struct ofnode_phandle_args *args)
Simon Glass0dac4d52015-01-05 20:05:28 -0700138{
139 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
140
Eric Nelson6c880b72016-04-24 16:32:40 -0700141 if (ops->xlate)
142 return ops->xlate(desc->dev, desc, args);
Simon Glass0dac4d52015-01-05 20:05:28 -0700143 else
Eric Nelson6c880b72016-04-24 16:32:40 -0700144 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glass0dac4d52015-01-05 20:05:28 -0700145}
146
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200147#if defined(CONFIG_GPIO_HOG)
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200148
149struct gpio_hog_priv {
150 struct gpio_desc gpiod;
151};
152
153struct gpio_hog_data {
154 int gpiod_flags;
155 int value;
156 u32 val[2];
157};
158
159static int gpio_hog_ofdata_to_platdata(struct udevice *dev)
160{
161 struct gpio_hog_data *plat = dev_get_platdata(dev);
162 const char *nodename;
163 int ret;
164
165 plat->value = 0;
166 if (dev_read_bool(dev, "input")) {
167 plat->gpiod_flags = GPIOD_IS_IN;
168 } else if (dev_read_bool(dev, "output-high")) {
169 plat->value = 1;
170 plat->gpiod_flags = GPIOD_IS_OUT;
171 } else if (dev_read_bool(dev, "output-low")) {
172 plat->gpiod_flags = GPIOD_IS_OUT;
173 } else {
174 printf("%s: missing gpio-hog state.\n", __func__);
175 return -EINVAL;
176 }
177 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
178 if (ret) {
179 printf("%s: wrong gpios property, 2 values needed %d\n",
180 __func__, ret);
181 return ret;
182 }
183 nodename = dev_read_string(dev, "line-name");
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200184 if (nodename)
185 device_set_name(dev, nodename);
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200186
187 return 0;
188}
189
190static int gpio_hog_probe(struct udevice *dev)
191{
192 struct gpio_hog_data *plat = dev_get_platdata(dev);
193 struct gpio_hog_priv *priv = dev_get_priv(dev);
194 int ret;
195
196 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
197 plat->val[0], plat->gpiod_flags,
198 plat->val[1], &priv->gpiod);
199 if (ret < 0) {
200 debug("%s: node %s could not get gpio.\n", __func__,
201 dev->name);
202 return ret;
203 }
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200204
205 if (plat->gpiod_flags == GPIOD_IS_OUT) {
206 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
207 if (ret < 0) {
208 debug("%s: node %s could not set gpio.\n", __func__,
209 dev->name);
210 return ret;
211 }
212 }
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200213
214 return 0;
215}
216
217int gpio_hog_probe_all(void)
218{
219 struct udevice *dev;
220 int ret;
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200221 int retval = 0;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200222
223 for (uclass_first_device(UCLASS_NOP, &dev);
224 dev;
225 uclass_find_next_device(&dev)) {
226 if (dev->driver == DM_GET_DRIVER(gpio_hog)) {
227 ret = device_probe(dev);
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200228 if (ret) {
229 printf("Failed to probe device %s err: %d\n",
230 dev->name, ret);
231 retval = ret;
232 }
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200233 }
234 }
235
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200236 return retval;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200237}
238
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200239int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200240{
241 struct udevice *dev;
242
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200243 *desc = NULL;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200244 gpio_hog_probe_all();
245 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
246 struct gpio_hog_priv *priv = dev_get_priv(dev);
247
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200248 *desc = &priv->gpiod;
249 return 0;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200250 }
251
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200252 return -ENODEV;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200253}
254
255U_BOOT_DRIVER(gpio_hog) = {
256 .name = "gpio_hog",
257 .id = UCLASS_NOP,
258 .ofdata_to_platdata = gpio_hog_ofdata_to_platdata,
259 .probe = gpio_hog_probe,
260 .priv_auto_alloc_size = sizeof(struct gpio_hog_priv),
261 .platdata_auto_alloc_size = sizeof(struct gpio_hog_data),
262};
263#else
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200264int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200265{
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200266 return 0;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200267}
268#endif
269
Simon Glassefa677f2015-06-23 15:38:41 -0600270int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassae7123f2015-01-05 20:05:27 -0700271{
272 struct udevice *dev = desc->dev;
273 struct gpio_dev_priv *uc_priv;
274 char *str;
275 int ret;
276
Simon Glasse564f052015-03-05 12:25:20 -0700277 uc_priv = dev_get_uclass_priv(dev);
Simon Glassae7123f2015-01-05 20:05:27 -0700278 if (uc_priv->name[desc->offset])
279 return -EBUSY;
280 str = strdup(label);
281 if (!str)
282 return -ENOMEM;
283 if (gpio_get_ops(dev)->request) {
284 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
285 if (ret) {
286 free(str);
287 return ret;
288 }
289 }
290 uc_priv->name[desc->offset] = str;
291
292 return 0;
293}
294
Simon Glass3669e0e2015-01-05 20:05:29 -0700295static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
296{
Simon Glass27084c02019-09-25 08:56:27 -0600297#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass3669e0e2015-01-05 20:05:29 -0700298 va_list args;
299 char buf[40];
300
301 va_start(args, fmt);
302 vscnprintf(buf, sizeof(buf), fmt, args);
303 va_end(args);
304 return dm_gpio_request(desc, buf);
Simon Glass4dc52592015-12-29 05:22:48 -0700305#else
306 return dm_gpio_request(desc, fmt);
307#endif
Simon Glass3669e0e2015-01-05 20:05:29 -0700308}
309
Simon Glass96495d92014-02-26 15:59:24 -0700310/**
311 * gpio_request() - [COMPAT] Request GPIO
312 * gpio: GPIO number
313 * label: Name for the requested GPIO
314 *
Simon Glassb892d122014-10-04 11:29:42 -0600315 * The label is copied and allocated so the caller does not need to keep
316 * the pointer around.
317 *
Simon Glass96495d92014-02-26 15:59:24 -0700318 * This function implements the API that's compatible with current
319 * GPIO API used in U-Boot. The request is forwarded to particular
320 * GPIO driver. Returns 0 on success, negative value on error.
321 */
322int gpio_request(unsigned gpio, const char *label)
323{
Simon Glassae7123f2015-01-05 20:05:27 -0700324 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700325 int ret;
326
Simon Glassae7123f2015-01-05 20:05:27 -0700327 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700328 if (ret)
329 return ret;
330
Simon Glassae7123f2015-01-05 20:05:27 -0700331 return dm_gpio_request(&desc, label);
Simon Glass96495d92014-02-26 15:59:24 -0700332}
333
334/**
Simon Glassd44f5972014-10-04 11:29:49 -0600335 * gpio_requestf() - [COMPAT] Request GPIO
336 * @gpio: GPIO number
337 * @fmt: Format string for the requested GPIO
338 * @...: Arguments for the printf() format string
339 *
340 * This function implements the API that's compatible with current
341 * GPIO API used in U-Boot. The request is forwarded to particular
342 * GPIO driver. Returns 0 on success, negative value on error.
343 */
344int gpio_requestf(unsigned gpio, const char *fmt, ...)
345{
Simon Glass27084c02019-09-25 08:56:27 -0600346#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glassd44f5972014-10-04 11:29:49 -0600347 va_list args;
348 char buf[40];
349
350 va_start(args, fmt);
351 vscnprintf(buf, sizeof(buf), fmt, args);
352 va_end(args);
353 return gpio_request(gpio, buf);
Simon Glass4dc52592015-12-29 05:22:48 -0700354#else
355 return gpio_request(gpio, fmt);
356#endif
Simon Glassd44f5972014-10-04 11:29:49 -0600357}
358
Simon Glassae7123f2015-01-05 20:05:27 -0700359int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glass96495d92014-02-26 15:59:24 -0700360{
Simon Glassb892d122014-10-04 11:29:42 -0600361 struct gpio_dev_priv *uc_priv;
Simon Glass96495d92014-02-26 15:59:24 -0700362 int ret;
363
Simon Glasse564f052015-03-05 12:25:20 -0700364 uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -0600365 if (!uc_priv->name[offset])
366 return -ENXIO;
Simon Glass093152f2020-02-04 20:15:17 -0700367 if (gpio_get_ops(dev)->rfree) {
368 ret = gpio_get_ops(dev)->rfree(dev, offset);
Simon Glassb892d122014-10-04 11:29:42 -0600369 if (ret)
370 return ret;
371 }
372
373 free(uc_priv->name[offset]);
374 uc_priv->name[offset] = NULL;
375
376 return 0;
377}
378
Simon Glassae7123f2015-01-05 20:05:27 -0700379/**
380 * gpio_free() - [COMPAT] Relinquish GPIO
381 * gpio: GPIO number
382 *
383 * This function implements the API that's compatible with current
384 * GPIO API used in U-Boot. The request is forwarded to particular
385 * GPIO driver. Returns 0 on success, negative value on error.
386 */
387int gpio_free(unsigned gpio)
Simon Glassb892d122014-10-04 11:29:42 -0600388{
Simon Glassae7123f2015-01-05 20:05:27 -0700389 struct gpio_desc desc;
390 int ret;
Simon Glassb892d122014-10-04 11:29:42 -0600391
Simon Glassae7123f2015-01-05 20:05:27 -0700392 ret = gpio_to_device(gpio, &desc);
393 if (ret)
394 return ret;
395
396 return _dm_gpio_free(desc.dev, desc.offset);
397}
398
Simon Glass17c43f12016-03-06 19:27:51 -0700399static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glassae7123f2015-01-05 20:05:27 -0700400{
Simon Glasseca48662015-07-02 18:16:16 -0600401 struct gpio_dev_priv *uc_priv;
Simon Glassae7123f2015-01-05 20:05:27 -0700402
Simon Glasseca48662015-07-02 18:16:16 -0600403 if (!dm_gpio_is_valid(desc))
404 return -ENOENT;
405
406 uc_priv = dev_get_uclass_priv(desc->dev);
Simon Glassae7123f2015-01-05 20:05:27 -0700407 if (!uc_priv->name[desc->offset]) {
Simon Glassb892d122014-10-04 11:29:42 -0600408 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassae7123f2015-01-05 20:05:27 -0700409 desc->dev->name, func,
410 uc_priv->bank_name ? uc_priv->bank_name : "",
411 desc->offset);
Simon Glassb892d122014-10-04 11:29:42 -0600412 return -EBUSY;
413 }
414
415 return 0;
Simon Glass96495d92014-02-26 15:59:24 -0700416}
417
418/**
419 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
420 * gpio: GPIO number
421 *
422 * This function implements the API that's compatible with current
423 * GPIO API used in U-Boot. The request is forwarded to particular
424 * GPIO driver. Returns 0 on success, negative value on error.
425 */
426int gpio_direction_input(unsigned gpio)
427{
Simon Glassae7123f2015-01-05 20:05:27 -0700428 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700429 int ret;
430
Simon Glassae7123f2015-01-05 20:05:27 -0700431 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700432 if (ret)
433 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700434 ret = check_reserved(&desc, "dir_input");
435 if (ret)
436 return ret;
Simon Glass96495d92014-02-26 15:59:24 -0700437
Simon Glassae7123f2015-01-05 20:05:27 -0700438 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
Simon Glass96495d92014-02-26 15:59:24 -0700439}
440
441/**
442 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
443 * gpio: GPIO number
444 * value: Logical value to be set on the GPIO pin
445 *
446 * This function implements the API that's compatible with current
447 * GPIO API used in U-Boot. The request is forwarded to particular
448 * GPIO driver. Returns 0 on success, negative value on error.
449 */
450int gpio_direction_output(unsigned gpio, int value)
451{
Simon Glassae7123f2015-01-05 20:05:27 -0700452 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700453 int ret;
454
Simon Glassae7123f2015-01-05 20:05:27 -0700455 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700456 if (ret)
457 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700458 ret = check_reserved(&desc, "dir_output");
459 if (ret)
460 return ret;
Simon Glass96495d92014-02-26 15:59:24 -0700461
Simon Glassae7123f2015-01-05 20:05:27 -0700462 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
463 desc.offset, value);
464}
465
Simon Glass17c43f12016-03-06 19:27:51 -0700466int dm_gpio_get_value(const struct gpio_desc *desc)
Simon Glassae7123f2015-01-05 20:05:27 -0700467{
468 int value;
469 int ret;
470
471 ret = check_reserved(desc, "get_value");
472 if (ret)
473 return ret;
474
475 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
476
477 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
478}
479
Simon Glass17c43f12016-03-06 19:27:51 -0700480int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassae7123f2015-01-05 20:05:27 -0700481{
482 int ret;
483
484 ret = check_reserved(desc, "set_value");
485 if (ret)
486 return ret;
487
488 if (desc->flags & GPIOD_ACTIVE_LOW)
489 value = !value;
490 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
491 return 0;
492}
493
mario.six@gdsys.cc53ecdfb2016-05-25 15:15:21 +0200494int dm_gpio_get_open_drain(struct gpio_desc *desc)
495{
496 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
497 int ret;
498
499 ret = check_reserved(desc, "get_open_drain");
500 if (ret)
501 return ret;
502
503 if (ops->set_open_drain)
504 return ops->get_open_drain(desc->dev, desc->offset);
505 else
506 return -ENOSYS;
507}
508
509int dm_gpio_set_open_drain(struct gpio_desc *desc, int value)
510{
511 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
512 int ret;
513
514 ret = check_reserved(desc, "set_open_drain");
515 if (ret)
516 return ret;
517
518 if (ops->set_open_drain)
519 ret = ops->set_open_drain(desc->dev, desc->offset, value);
520 else
521 return 0; /* feature not supported -> ignore setting */
522
523 return ret;
524}
525
Simon Glassae7123f2015-01-05 20:05:27 -0700526int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
527{
528 struct udevice *dev = desc->dev;
529 struct dm_gpio_ops *ops = gpio_get_ops(dev);
530 int ret;
531
532 ret = check_reserved(desc, "set_dir");
533 if (ret)
534 return ret;
535
536 if (flags & GPIOD_IS_OUT) {
537 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
538
539 if (flags & GPIOD_ACTIVE_LOW)
540 value = !value;
541 ret = ops->direction_output(dev, desc->offset, value);
542 } else if (flags & GPIOD_IS_IN) {
543 ret = ops->direction_input(dev, desc->offset);
544 }
545 if (ret)
546 return ret;
547 /*
548 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
549 * futures
550 */
551 desc->flags = flags;
552
553 return 0;
554}
555
556int dm_gpio_set_dir(struct gpio_desc *desc)
557{
558 return dm_gpio_set_dir_flags(desc, desc->flags);
Simon Glass96495d92014-02-26 15:59:24 -0700559}
560
561/**
562 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
563 * gpio: GPIO number
564 *
565 * This function implements the API that's compatible with current
566 * GPIO API used in U-Boot. The request is forwarded to particular
567 * GPIO driver. Returns the value of the GPIO pin, or negative value
568 * on error.
569 */
570int gpio_get_value(unsigned gpio)
571{
Simon Glass96495d92014-02-26 15:59:24 -0700572 int ret;
573
Simon Glassae7123f2015-01-05 20:05:27 -0700574 struct gpio_desc desc;
575
576 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700577 if (ret)
578 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700579 return dm_gpio_get_value(&desc);
Simon Glass96495d92014-02-26 15:59:24 -0700580}
581
582/**
583 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
584 * gpio: GPIO number
585 * value: Logical value to be set on the GPIO pin.
586 *
587 * This function implements the API that's compatible with current
588 * GPIO API used in U-Boot. The request is forwarded to particular
589 * GPIO driver. Returns 0 on success, negative value on error.
590 */
591int gpio_set_value(unsigned gpio, int value)
592{
Simon Glassae7123f2015-01-05 20:05:27 -0700593 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700594 int ret;
595
Simon Glassae7123f2015-01-05 20:05:27 -0700596 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700597 if (ret)
598 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700599 return dm_gpio_set_value(&desc, value);
Simon Glass96495d92014-02-26 15:59:24 -0700600}
601
Heiko Schocher54c5d082014-05-22 12:43:05 +0200602const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glass96495d92014-02-26 15:59:24 -0700603{
604 struct gpio_dev_priv *priv;
605
606 /* Must be called on an active device */
Simon Glasse564f052015-03-05 12:25:20 -0700607 priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -0700608 assert(priv);
609
610 *bit_count = priv->gpio_count;
611 return priv->bank_name;
612}
613
Simon Glass6449a502014-10-04 11:29:43 -0600614static const char * const gpio_function[GPIOF_COUNT] = {
615 "input",
616 "output",
617 "unused",
618 "unknown",
619 "func",
620};
621
Masahiro Yamadafb07f972017-06-22 16:50:25 +0900622static int get_function(struct udevice *dev, int offset, bool skip_unused,
623 const char **namep)
Simon Glass6449a502014-10-04 11:29:43 -0600624{
Simon Glasse564f052015-03-05 12:25:20 -0700625 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass6449a502014-10-04 11:29:43 -0600626 struct dm_gpio_ops *ops = gpio_get_ops(dev);
627
628 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
629 if (!device_active(dev))
630 return -ENODEV;
631 if (offset < 0 || offset >= uc_priv->gpio_count)
632 return -EINVAL;
633 if (namep)
634 *namep = uc_priv->name[offset];
635 if (skip_unused && !uc_priv->name[offset])
636 return GPIOF_UNUSED;
637 if (ops->get_function) {
638 int ret;
639
640 ret = ops->get_function(dev, offset);
641 if (ret < 0)
642 return ret;
643 if (ret >= ARRAY_SIZE(gpio_function))
644 return -ENODATA;
645 return ret;
646 }
647
648 return GPIOF_UNKNOWN;
649}
650
651int gpio_get_function(struct udevice *dev, int offset, const char **namep)
652{
653 return get_function(dev, offset, true, namep);
654}
655
656int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
657{
658 return get_function(dev, offset, false, namep);
659}
660
Simon Glass07575352014-10-04 11:29:44 -0600661int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
662{
663 struct dm_gpio_ops *ops = gpio_get_ops(dev);
664 struct gpio_dev_priv *priv;
665 char *str = buf;
666 int func;
667 int ret;
668 int len;
669
670 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
671
672 *buf = 0;
Simon Glasse564f052015-03-05 12:25:20 -0700673 priv = dev_get_uclass_priv(dev);
Simon Glass07575352014-10-04 11:29:44 -0600674 ret = gpio_get_raw_function(dev, offset, NULL);
675 if (ret < 0)
676 return ret;
677 func = ret;
678 len = snprintf(str, buffsize, "%s%d: %s",
679 priv->bank_name ? priv->bank_name : "",
680 offset, gpio_function[func]);
681 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
682 func == GPIOF_UNUSED) {
683 const char *label;
684 bool used;
685
686 ret = ops->get_value(dev, offset);
687 if (ret < 0)
688 return ret;
689 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
690 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
691 ret,
692 used ? 'x' : ' ',
693 used ? " " : "",
694 label ? label : "");
695 }
696
697 return 0;
698}
699
Simon Glass962f5ca2015-04-14 21:03:20 -0600700int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
701{
702 int i, ret;
703 int gpio;
704
705 for (i = 0; i < 32; i++) {
706 gpio = gpio_num_array[i];
707 if (gpio == -1)
708 break;
709 ret = gpio_requestf(gpio, fmt, i);
710 if (ret)
711 goto err;
712 ret = gpio_direction_input(gpio);
713 if (ret) {
714 gpio_free(gpio);
715 goto err;
716 }
717 }
718
719 return 0;
720err:
721 for (i--; i >= 0; i--)
722 gpio_free(gpio_num_array[i]);
723
724 return ret;
725}
726
Simon Glasse5901c92014-11-10 18:00:21 -0700727/*
728 * get a number comprised of multiple GPIO values. gpio_num_array points to
729 * the array of gpio pin numbers to scan, terminated by -1.
730 */
Simon Glass962f5ca2015-04-14 21:03:20 -0600731int gpio_get_values_as_int(const int *gpio_list)
Simon Glasse5901c92014-11-10 18:00:21 -0700732{
733 int gpio;
734 unsigned bitmask = 1;
735 unsigned vector = 0;
Simon Glass962f5ca2015-04-14 21:03:20 -0600736 int ret;
Simon Glasse5901c92014-11-10 18:00:21 -0700737
738 while (bitmask &&
Simon Glass962f5ca2015-04-14 21:03:20 -0600739 ((gpio = *gpio_list++) != -1)) {
740 ret = gpio_get_value(gpio);
741 if (ret < 0)
742 return ret;
743 else if (ret)
Simon Glasse5901c92014-11-10 18:00:21 -0700744 vector |= bitmask;
745 bitmask <<= 1;
746 }
Simon Glass962f5ca2015-04-14 21:03:20 -0600747
Simon Glasse5901c92014-11-10 18:00:21 -0700748 return vector;
749}
750
Simon Glass17c43f12016-03-06 19:27:51 -0700751int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassbbf24782016-03-06 19:27:50 -0700752{
753 unsigned bitmask = 1;
754 unsigned vector = 0;
755 int ret, i;
756
757 for (i = 0; i < count; i++) {
758 ret = dm_gpio_get_value(&desc_list[i]);
759 if (ret < 0)
760 return ret;
761 else if (ret)
762 vector |= bitmask;
763 bitmask <<= 1;
764 }
765
766 return vector;
767}
768
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200769/**
770 * gpio_request_tail: common work for requesting a gpio.
771 *
772 * ret: return value from previous work in function which calls
773 * this function.
774 * This seems bogus (why calling this function instead not
775 * calling it and end caller function instead?).
776 * Because on error in caller function we want to set some
777 * default values in gpio desc and have a common error
778 * debug message, which provides this function.
779 * nodename: Name of node for which gpio gets requested
780 * used for gpio label name.
781 * args: pointer to output arguments structure
782 * list_name: Name of GPIO list
783 * used for gpio label name.
784 * index: gpio index in gpio list
785 * used for gpio label name.
786 * desc: pointer to gpio descriptor, filled from this
787 * function.
788 * flags: gpio flags to use.
789 * add_index: should index added to gpio label name
790 * gpio_dev: pointer to gpio device from which the gpio
791 * will be requested. If NULL try to get the
792 * gpio device with uclass_get_device_by_ofnode()
793 *
794 * return: In error case this function sets default values in
795 * gpio descriptor, also emmits a debug message.
796 * On success it returns 0 else the error code from
797 * function calls, or the error code passed through
798 * ret to this function.
799 *
800 */
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200801static int gpio_request_tail(int ret, const char *nodename,
Simon Glass3a571232017-05-18 20:09:18 -0600802 struct ofnode_phandle_args *args,
803 const char *list_name, int index,
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200804 struct gpio_desc *desc, int flags,
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200805 bool add_index, struct udevice *gpio_dev)
Simon Glass3669e0e2015-01-05 20:05:29 -0700806{
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200807 desc->dev = gpio_dev;
Simon Glass3669e0e2015-01-05 20:05:29 -0700808 desc->offset = 0;
Eric Nelson6c880b72016-04-24 16:32:40 -0700809 desc->flags = 0;
Simon Glass3a571232017-05-18 20:09:18 -0600810 if (ret)
Simon Glass3669e0e2015-01-05 20:05:29 -0700811 goto err;
Simon Glass3669e0e2015-01-05 20:05:29 -0700812
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200813 if (!desc->dev) {
814 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
815 &desc->dev);
816 if (ret) {
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200817 debug("%s: uclass_get_device_by_ofnode failed\n",
818 __func__);
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200819 goto err;
820 }
Simon Glass3669e0e2015-01-05 20:05:29 -0700821 }
Simon Glass3a571232017-05-18 20:09:18 -0600822 ret = gpio_find_and_xlate(desc, args);
Simon Glass3669e0e2015-01-05 20:05:29 -0700823 if (ret) {
824 debug("%s: gpio_find_and_xlate failed\n", __func__);
825 goto err;
826 }
827 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200828 nodename, list_name, index);
Simon Glass3669e0e2015-01-05 20:05:29 -0700829 if (ret) {
830 debug("%s: dm_gpio_requestf failed\n", __func__);
831 goto err;
832 }
833 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
834 if (ret) {
835 debug("%s: dm_gpio_set_dir failed\n", __func__);
836 goto err;
837 }
838
839 return 0;
840err:
841 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200842 __func__, nodename, list_name, index, ret);
Simon Glass3669e0e2015-01-05 20:05:29 -0700843 return ret;
844}
845
Simon Glass150c5af2017-05-30 21:47:09 -0600846static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
847 int index, struct gpio_desc *desc,
848 int flags, bool add_index)
Simon Glass3a571232017-05-18 20:09:18 -0600849{
850 struct ofnode_phandle_args args;
851 int ret;
852
Simon Glass150c5af2017-05-30 21:47:09 -0600853 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
854 index, &args);
Simon Glass3a571232017-05-18 20:09:18 -0600855
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200856 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
857 index, desc, flags, add_index, NULL);
Simon Glass3a571232017-05-18 20:09:18 -0600858}
859
Simon Glass150c5af2017-05-30 21:47:09 -0600860int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass3669e0e2015-01-05 20:05:29 -0700861 struct gpio_desc *desc, int flags)
862{
Simon Glass150c5af2017-05-30 21:47:09 -0600863 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
864 index > 0);
Simon Glass3669e0e2015-01-05 20:05:29 -0700865}
866
Simon Glass150c5af2017-05-30 21:47:09 -0600867int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass3669e0e2015-01-05 20:05:29 -0700868 struct gpio_desc *desc, int flags)
869{
Simon Glass150c5af2017-05-30 21:47:09 -0600870 struct ofnode_phandle_args args;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200871 ofnode node;
Simon Glass150c5af2017-05-30 21:47:09 -0600872 int ret;
873
874 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
875 index, &args);
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200876 node = dev_ofnode(dev);
877 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
878 index, desc, flags, index > 0, NULL);
Simon Glass3669e0e2015-01-05 20:05:29 -0700879}
880
Simon Glass150c5af2017-05-30 21:47:09 -0600881int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass3669e0e2015-01-05 20:05:29 -0700882 struct gpio_desc *desc, int max_count,
883 int flags)
884{
885 int count;
886 int ret;
887
Przemyslaw Marczak2984e7a2015-03-31 18:57:16 +0200888 for (count = 0; count < max_count; count++) {
Simon Glass150c5af2017-05-30 21:47:09 -0600889 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass3669e0e2015-01-05 20:05:29 -0700890 &desc[count], flags, true);
891 if (ret == -ENOENT)
892 break;
893 else if (ret)
894 goto err;
895 }
896
897 /* We ran out of GPIOs in the list */
898 return count;
899
900err:
901 gpio_free_list_nodev(desc, count - 1);
902
903 return ret;
904}
905
906int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
907 struct gpio_desc *desc, int max_count,
908 int flags)
909{
910 /*
911 * This isn't ideal since we don't use dev->name in the debug()
912 * calls in gpio_request_by_name(), but we can do this until
913 * gpio_request_list_by_name_nodev() can be dropped.
914 */
Simon Glass150c5af2017-05-30 21:47:09 -0600915 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
916 max_count, flags);
Simon Glass3669e0e2015-01-05 20:05:29 -0700917}
918
919int gpio_get_list_count(struct udevice *dev, const char *list_name)
920{
921 int ret;
922
Simon Glasse160f7d2017-01-17 16:52:55 -0700923 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
Simon Glass3669e0e2015-01-05 20:05:29 -0700924 list_name, "#gpio-cells", 0, -1,
925 NULL);
926 if (ret) {
927 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
928 __func__, dev->name, list_name, ret);
929 }
930
931 return ret;
932}
933
934int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
935{
936 /* For now, we don't do any checking of dev */
937 return _dm_gpio_free(desc->dev, desc->offset);
938}
939
940int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
941{
942 int i;
943
944 /* For now, we don't do any checking of dev */
945 for (i = 0; i < count; i++)
946 dm_gpio_free(dev, &desc[i]);
947
948 return 0;
949}
950
951int gpio_free_list_nodev(struct gpio_desc *desc, int count)
952{
953 return gpio_free_list(NULL, desc, count);
954}
955
Simon Glass96495d92014-02-26 15:59:24 -0700956/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glassb892d122014-10-04 11:29:42 -0600957static int gpio_renumber(struct udevice *removed_dev)
Simon Glass96495d92014-02-26 15:59:24 -0700958{
959 struct gpio_dev_priv *uc_priv;
Heiko Schocher54c5d082014-05-22 12:43:05 +0200960 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -0700961 struct uclass *uc;
962 unsigned base;
963 int ret;
964
965 ret = uclass_get(UCLASS_GPIO, &uc);
966 if (ret)
967 return ret;
968
969 /* Ensure that we have a base for each bank */
970 base = 0;
971 uclass_foreach_dev(dev, uc) {
Simon Glassb892d122014-10-04 11:29:42 -0600972 if (device_active(dev) && dev != removed_dev) {
Simon Glasse564f052015-03-05 12:25:20 -0700973 uc_priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -0700974 uc_priv->gpio_base = base;
975 base += uc_priv->gpio_count;
976 }
977 }
978
979 return 0;
980}
981
Simon Glass17c43f12016-03-06 19:27:51 -0700982int gpio_get_number(const struct gpio_desc *desc)
Simon Glass56a71f82015-03-25 12:21:58 -0600983{
984 struct udevice *dev = desc->dev;
985 struct gpio_dev_priv *uc_priv;
986
987 if (!dev)
988 return -1;
989 uc_priv = dev->uclass_priv;
990
991 return uc_priv->gpio_base + desc->offset;
992}
993
Heiko Schocher54c5d082014-05-22 12:43:05 +0200994static int gpio_post_probe(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -0700995{
Simon Glasse564f052015-03-05 12:25:20 -0700996 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -0600997
998 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
999 if (!uc_priv->name)
1000 return -ENOMEM;
1001
1002 return gpio_renumber(NULL);
Simon Glass96495d92014-02-26 15:59:24 -07001003}
1004
Heiko Schocher54c5d082014-05-22 12:43:05 +02001005static int gpio_pre_remove(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -07001006{
Simon Glasse564f052015-03-05 12:25:20 -07001007 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -06001008 int i;
1009
1010 for (i = 0; i < uc_priv->gpio_count; i++) {
1011 if (uc_priv->name[i])
1012 free(uc_priv->name[i]);
1013 }
1014 free(uc_priv->name);
1015
1016 return gpio_renumber(dev);
Simon Glass96495d92014-02-26 15:59:24 -07001017}
1018
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001019int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1020 char *list_name, int index, int flags,
1021 int dtflags, struct gpio_desc *desc)
1022{
1023 struct ofnode_phandle_args args;
1024
1025 args.node = ofnode_null();
1026 args.args_count = 2;
1027 args.args[0] = index;
1028 args.args[1] = dtflags;
1029
1030 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1031 flags, 0, dev);
1032}
1033
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001034static int gpio_post_bind(struct udevice *dev)
1035{
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001036 struct udevice *child;
1037 ofnode node;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001038
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001039#if defined(CONFIG_NEEDS_MANUAL_RELOC)
1040 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1041 static int reloc_done;
1042
1043 if (!reloc_done) {
1044 if (ops->request)
1045 ops->request += gd->reloc_off;
Simon Glass093152f2020-02-04 20:15:17 -07001046 if (ops->rfree)
1047 ops->rfree += gd->reloc_off;
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001048 if (ops->direction_input)
1049 ops->direction_input += gd->reloc_off;
1050 if (ops->direction_output)
1051 ops->direction_output += gd->reloc_off;
1052 if (ops->get_value)
1053 ops->get_value += gd->reloc_off;
1054 if (ops->set_value)
1055 ops->set_value += gd->reloc_off;
1056 if (ops->get_open_drain)
1057 ops->get_open_drain += gd->reloc_off;
1058 if (ops->set_open_drain)
1059 ops->set_open_drain += gd->reloc_off;
1060 if (ops->get_function)
1061 ops->get_function += gd->reloc_off;
1062 if (ops->xlate)
1063 ops->xlate += gd->reloc_off;
1064
1065 reloc_done++;
1066 }
1067#endif
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001068
Heiko Schocher49b10cb2019-07-17 06:59:51 +02001069 if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1070 dev_for_each_subnode(node, dev) {
1071 if (ofnode_read_bool(node, "gpio-hog")) {
1072 const char *name = ofnode_get_name(node);
1073 int ret;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001074
Heiko Schocher49b10cb2019-07-17 06:59:51 +02001075 ret = device_bind_driver_to_node(dev,
1076 "gpio_hog",
1077 name, node,
1078 &child);
1079 if (ret)
1080 return ret;
1081 }
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001082 }
1083 }
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001084 return 0;
1085}
1086
Simon Glass96495d92014-02-26 15:59:24 -07001087UCLASS_DRIVER(gpio) = {
1088 .id = UCLASS_GPIO,
1089 .name = "gpio",
Bhuvanchandra DVae89bb02015-06-01 18:37:15 +05301090 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass96495d92014-02-26 15:59:24 -07001091 .post_probe = gpio_post_probe,
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001092 .post_bind = gpio_post_bind,
Simon Glass96495d92014-02-26 15:59:24 -07001093 .pre_remove = gpio_pre_remove,
1094 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
1095};