blob: 3b505d070a9818083872f222b36df11fe4c21302 [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
6#include <common.h>
7#include <dm.h>
Heiko Schocher39cb3402019-06-12 06:11:46 +02008#include <dm/device-internal.h>
9#include <dm/lists.h>
10#include <dm/uclass-internal.h>
Eric Nelson786e98d2016-04-24 16:32:40 -070011#include <dt-bindings/gpio/gpio.h>
Simon Glasse821d182014-02-26 15:59:24 -070012#include <errno.h>
Simon Glassd3322bb2015-01-05 20:05:28 -070013#include <fdtdec.h>
Simon Glass0f4517d2014-10-04 11:29:42 -060014#include <malloc.h>
Simon Glasse821d182014-02-26 15:59:24 -070015#include <asm/gpio.h>
Masahiro Yamada78eeb912016-01-24 23:27:48 +090016#include <linux/bug.h>
Simon Glass43b2e1a2014-10-22 21:37:01 -060017#include <linux/ctype.h>
Simon Glasse821d182014-02-26 15:59:24 -070018
Simon Glass16e10402015-01-05 20:05:29 -070019DECLARE_GLOBAL_DATA_PTR;
20
Simon Glasse821d182014-02-26 15:59:24 -070021/**
Patrick Delaunay758bba92020-01-13 11:35:01 +010022 * gpio_desc_init() - Initialize the GPIO descriptor
23 *
24 * @desc: GPIO descriptor to initialize
25 * @dev: GPIO device
26 * @offset: Offset of device GPIO
27 */
28static void gpio_desc_init(struct gpio_desc *desc,
29 struct udevice *dev,
30 uint offset)
31{
32 desc->dev = dev;
33 desc->offset = offset;
34 desc->flags = 0;
35}
36
37/**
Simon Glasse821d182014-02-26 15:59:24 -070038 * gpio_to_device() - Convert global GPIO number to device, number
Simon Glasse821d182014-02-26 15:59:24 -070039 *
40 * Convert the GPIO number to an entry in the list of GPIOs
41 * or GPIO blocks registered with the GPIO controller. Returns
42 * entry on success, NULL on error.
Simon Glassce555292015-01-05 20:05:27 -070043 *
44 * @gpio: The numeric representation of the GPIO
45 * @desc: Returns description (desc->flags will always be 0)
46 * @return 0 if found, -ENOENT if not found
Simon Glasse821d182014-02-26 15:59:24 -070047 */
Simon Glassce555292015-01-05 20:05:27 -070048static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -070049{
50 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +020051 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -070052 int ret;
53
54 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
55 dev;
56 ret = uclass_next_device(&dev)) {
Simon Glassde0977b2015-03-05 12:25:20 -070057 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -070058 if (gpio >= uc_priv->gpio_base &&
59 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
Patrick Delaunay758bba92020-01-13 11:35:01 +010060 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
Simon Glasse821d182014-02-26 15:59:24 -070061 return 0;
62 }
63 }
64
65 /* No such GPIO */
Simon Glassce555292015-01-05 20:05:27 -070066 return ret ? ret : -ENOENT;
Simon Glasse821d182014-02-26 15:59:24 -070067}
68
Simon Glass215bcc72015-06-23 15:38:40 -060069int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -070070{
Simon Glass43b2e1a2014-10-22 21:37:01 -060071 struct gpio_dev_priv *uc_priv = NULL;
Heiko Schocherb74fcb42014-05-22 12:43:05 +020072 struct udevice *dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -060073 ulong offset;
74 int numeric;
Simon Glasse821d182014-02-26 15:59:24 -070075 int ret;
76
Simon Glass43b2e1a2014-10-22 21:37:01 -060077 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
Simon Glasse821d182014-02-26 15:59:24 -070078 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
79 dev;
80 ret = uclass_next_device(&dev)) {
Simon Glasse821d182014-02-26 15:59:24 -070081 int len;
82
Simon Glassde0977b2015-03-05 12:25:20 -070083 uc_priv = dev_get_uclass_priv(dev);
Simon Glass43b2e1a2014-10-22 21:37:01 -060084 if (numeric != -1) {
85 offset = numeric - uc_priv->gpio_base;
86 /* Allow GPIOs to be numbered from 0 */
Tom Rini26fd9682017-05-10 15:20:15 -040087 if (offset < uc_priv->gpio_count)
Simon Glass43b2e1a2014-10-22 21:37:01 -060088 break;
89 }
90
Simon Glasse821d182014-02-26 15:59:24 -070091 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
92
Simon Glassd4acf632014-06-11 23:29:47 -060093 if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glass43b2e1a2014-10-22 21:37:01 -060094 if (!strict_strtoul(name + len, 10, &offset))
95 break;
Simon Glasse821d182014-02-26 15:59:24 -070096 }
97 }
98
Simon Glass43b2e1a2014-10-22 21:37:01 -060099 if (!dev)
100 return ret ? ret : -EINVAL;
101
Patrick Delaunay758bba92020-01-13 11:35:01 +0100102 gpio_desc_init(desc, dev, offset);
Simon Glass215bcc72015-06-23 15:38:40 -0600103
104 return 0;
105}
106
107int gpio_lookup_name(const char *name, struct udevice **devp,
108 unsigned int *offsetp, unsigned int *gpiop)
109{
110 struct gpio_desc desc;
111 int ret;
112
113 if (devp)
114 *devp = NULL;
115 ret = dm_gpio_lookup_name(name, &desc);
116 if (ret)
117 return ret;
118
Simon Glass43b2e1a2014-10-22 21:37:01 -0600119 if (devp)
Simon Glass215bcc72015-06-23 15:38:40 -0600120 *devp = desc.dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -0600121 if (offsetp)
Simon Glass215bcc72015-06-23 15:38:40 -0600122 *offsetp = desc.offset;
123 if (gpiop) {
124 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
125
126 *gpiop = uc_priv->gpio_base + desc.offset;
127 }
Simon Glass43b2e1a2014-10-22 21:37:01 -0600128
129 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700130}
131
Simon Glass12faa022017-05-18 20:09:18 -0600132int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
133 struct ofnode_phandle_args *args)
Eric Nelson786e98d2016-04-24 16:32:40 -0700134{
135 if (args->args_count < 1)
136 return -EINVAL;
137
138 desc->offset = args->args[0];
139
140 if (args->args_count < 2)
141 return 0;
142
143 if (args->args[1] & GPIO_ACTIVE_LOW)
144 desc->flags = GPIOD_ACTIVE_LOW;
145
146 return 0;
147}
148
Simon Glass16e10402015-01-05 20:05:29 -0700149static int gpio_find_and_xlate(struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600150 struct ofnode_phandle_args *args)
Simon Glassd3322bb2015-01-05 20:05:28 -0700151{
152 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
153
Eric Nelson786e98d2016-04-24 16:32:40 -0700154 if (ops->xlate)
155 return ops->xlate(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700156 else
Eric Nelson786e98d2016-04-24 16:32:40 -0700157 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700158}
159
Heiko Schocher58e4c382019-07-17 06:59:51 +0200160#if defined(CONFIG_GPIO_HOG)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200161
162struct gpio_hog_priv {
163 struct gpio_desc gpiod;
164};
165
166struct gpio_hog_data {
167 int gpiod_flags;
168 int value;
169 u32 val[2];
170};
171
172static int gpio_hog_ofdata_to_platdata(struct udevice *dev)
173{
174 struct gpio_hog_data *plat = dev_get_platdata(dev);
175 const char *nodename;
176 int ret;
177
178 plat->value = 0;
179 if (dev_read_bool(dev, "input")) {
180 plat->gpiod_flags = GPIOD_IS_IN;
181 } else if (dev_read_bool(dev, "output-high")) {
182 plat->value = 1;
183 plat->gpiod_flags = GPIOD_IS_OUT;
184 } else if (dev_read_bool(dev, "output-low")) {
185 plat->gpiod_flags = GPIOD_IS_OUT;
186 } else {
187 printf("%s: missing gpio-hog state.\n", __func__);
188 return -EINVAL;
189 }
190 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
191 if (ret) {
192 printf("%s: wrong gpios property, 2 values needed %d\n",
193 __func__, ret);
194 return ret;
195 }
196 nodename = dev_read_string(dev, "line-name");
Heiko Schocher58e4c382019-07-17 06:59:51 +0200197 if (nodename)
198 device_set_name(dev, nodename);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200199
200 return 0;
201}
202
203static int gpio_hog_probe(struct udevice *dev)
204{
205 struct gpio_hog_data *plat = dev_get_platdata(dev);
206 struct gpio_hog_priv *priv = dev_get_priv(dev);
207 int ret;
208
209 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
210 plat->val[0], plat->gpiod_flags,
211 plat->val[1], &priv->gpiod);
212 if (ret < 0) {
213 debug("%s: node %s could not get gpio.\n", __func__,
214 dev->name);
215 return ret;
216 }
Heiko Schocher58e4c382019-07-17 06:59:51 +0200217
218 if (plat->gpiod_flags == GPIOD_IS_OUT) {
219 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
220 if (ret < 0) {
221 debug("%s: node %s could not set gpio.\n", __func__,
222 dev->name);
223 return ret;
224 }
225 }
Heiko Schocher39cb3402019-06-12 06:11:46 +0200226
227 return 0;
228}
229
230int gpio_hog_probe_all(void)
231{
232 struct udevice *dev;
233 int ret;
Heiko Schocher58e4c382019-07-17 06:59:51 +0200234 int retval = 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200235
236 for (uclass_first_device(UCLASS_NOP, &dev);
237 dev;
238 uclass_find_next_device(&dev)) {
239 if (dev->driver == DM_GET_DRIVER(gpio_hog)) {
240 ret = device_probe(dev);
Heiko Schocher58e4c382019-07-17 06:59:51 +0200241 if (ret) {
242 printf("Failed to probe device %s err: %d\n",
243 dev->name, ret);
244 retval = ret;
245 }
Heiko Schocher39cb3402019-06-12 06:11:46 +0200246 }
247 }
248
Heiko Schocher58e4c382019-07-17 06:59:51 +0200249 return retval;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200250}
251
Heiko Schocher58e4c382019-07-17 06:59:51 +0200252int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200253{
254 struct udevice *dev;
255
Heiko Schocher58e4c382019-07-17 06:59:51 +0200256 *desc = NULL;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200257 gpio_hog_probe_all();
258 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
259 struct gpio_hog_priv *priv = dev_get_priv(dev);
260
Heiko Schocher58e4c382019-07-17 06:59:51 +0200261 *desc = &priv->gpiod;
262 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200263 }
264
Heiko Schocher58e4c382019-07-17 06:59:51 +0200265 return -ENODEV;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200266}
267
268U_BOOT_DRIVER(gpio_hog) = {
269 .name = "gpio_hog",
270 .id = UCLASS_NOP,
271 .ofdata_to_platdata = gpio_hog_ofdata_to_platdata,
272 .probe = gpio_hog_probe,
273 .priv_auto_alloc_size = sizeof(struct gpio_hog_priv),
274 .platdata_auto_alloc_size = sizeof(struct gpio_hog_data),
275};
276#else
Heiko Schocher58e4c382019-07-17 06:59:51 +0200277int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200278{
Heiko Schocher58e4c382019-07-17 06:59:51 +0200279 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200280}
281#endif
282
Simon Glass047cdb32015-06-23 15:38:41 -0600283int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassce555292015-01-05 20:05:27 -0700284{
285 struct udevice *dev = desc->dev;
286 struct gpio_dev_priv *uc_priv;
287 char *str;
288 int ret;
289
Simon Glassde0977b2015-03-05 12:25:20 -0700290 uc_priv = dev_get_uclass_priv(dev);
Simon Glassce555292015-01-05 20:05:27 -0700291 if (uc_priv->name[desc->offset])
292 return -EBUSY;
293 str = strdup(label);
294 if (!str)
295 return -ENOMEM;
296 if (gpio_get_ops(dev)->request) {
297 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
298 if (ret) {
299 free(str);
300 return ret;
301 }
302 }
303 uc_priv->name[desc->offset] = str;
304
305 return 0;
306}
307
Simon Glass16e10402015-01-05 20:05:29 -0700308static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
309{
Simon Glass7611ac62019-09-25 08:56:27 -0600310#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass16e10402015-01-05 20:05:29 -0700311 va_list args;
312 char buf[40];
313
314 va_start(args, fmt);
315 vscnprintf(buf, sizeof(buf), fmt, args);
316 va_end(args);
317 return dm_gpio_request(desc, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700318#else
319 return dm_gpio_request(desc, fmt);
320#endif
Simon Glass16e10402015-01-05 20:05:29 -0700321}
322
Simon Glasse821d182014-02-26 15:59:24 -0700323/**
324 * gpio_request() - [COMPAT] Request GPIO
325 * gpio: GPIO number
326 * label: Name for the requested GPIO
327 *
Simon Glass0f4517d2014-10-04 11:29:42 -0600328 * The label is copied and allocated so the caller does not need to keep
329 * the pointer around.
330 *
Simon Glasse821d182014-02-26 15:59:24 -0700331 * This function implements the API that's compatible with current
332 * GPIO API used in U-Boot. The request is forwarded to particular
333 * GPIO driver. Returns 0 on success, negative value on error.
334 */
335int gpio_request(unsigned gpio, const char *label)
336{
Simon Glassce555292015-01-05 20:05:27 -0700337 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700338 int ret;
339
Simon Glassce555292015-01-05 20:05:27 -0700340 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700341 if (ret)
342 return ret;
343
Simon Glassce555292015-01-05 20:05:27 -0700344 return dm_gpio_request(&desc, label);
Simon Glasse821d182014-02-26 15:59:24 -0700345}
346
347/**
Simon Glass1b27d602014-10-04 11:29:49 -0600348 * gpio_requestf() - [COMPAT] Request GPIO
349 * @gpio: GPIO number
350 * @fmt: Format string for the requested GPIO
351 * @...: Arguments for the printf() format string
352 *
353 * This function implements the API that's compatible with current
354 * GPIO API used in U-Boot. The request is forwarded to particular
355 * GPIO driver. Returns 0 on success, negative value on error.
356 */
357int gpio_requestf(unsigned gpio, const char *fmt, ...)
358{
Simon Glass7611ac62019-09-25 08:56:27 -0600359#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass1b27d602014-10-04 11:29:49 -0600360 va_list args;
361 char buf[40];
362
363 va_start(args, fmt);
364 vscnprintf(buf, sizeof(buf), fmt, args);
365 va_end(args);
366 return gpio_request(gpio, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700367#else
368 return gpio_request(gpio, fmt);
369#endif
Simon Glass1b27d602014-10-04 11:29:49 -0600370}
371
Simon Glassce555292015-01-05 20:05:27 -0700372int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glasse821d182014-02-26 15:59:24 -0700373{
Simon Glass0f4517d2014-10-04 11:29:42 -0600374 struct gpio_dev_priv *uc_priv;
Simon Glasse821d182014-02-26 15:59:24 -0700375 int ret;
376
Simon Glassde0977b2015-03-05 12:25:20 -0700377 uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600378 if (!uc_priv->name[offset])
379 return -ENXIO;
Simon Glassb3a47542020-02-04 20:15:17 -0700380 if (gpio_get_ops(dev)->rfree) {
381 ret = gpio_get_ops(dev)->rfree(dev, offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600382 if (ret)
383 return ret;
384 }
385
386 free(uc_priv->name[offset]);
387 uc_priv->name[offset] = NULL;
388
389 return 0;
390}
391
Simon Glassce555292015-01-05 20:05:27 -0700392/**
393 * gpio_free() - [COMPAT] Relinquish GPIO
394 * gpio: GPIO number
395 *
396 * This function implements the API that's compatible with current
397 * GPIO API used in U-Boot. The request is forwarded to particular
398 * GPIO driver. Returns 0 on success, negative value on error.
399 */
400int gpio_free(unsigned gpio)
401{
402 struct gpio_desc desc;
403 int ret;
404
405 ret = gpio_to_device(gpio, &desc);
406 if (ret)
407 return ret;
408
409 return _dm_gpio_free(desc.dev, desc.offset);
410}
411
Simon Glassfd838972016-03-06 19:27:51 -0700412static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glass0f4517d2014-10-04 11:29:42 -0600413{
Simon Glass230c1432015-07-02 18:16:16 -0600414 struct gpio_dev_priv *uc_priv;
415
416 if (!dm_gpio_is_valid(desc))
417 return -ENOENT;
Simon Glass0f4517d2014-10-04 11:29:42 -0600418
Simon Glass230c1432015-07-02 18:16:16 -0600419 uc_priv = dev_get_uclass_priv(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700420 if (!uc_priv->name[desc->offset]) {
Simon Glass0f4517d2014-10-04 11:29:42 -0600421 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassce555292015-01-05 20:05:27 -0700422 desc->dev->name, func,
423 uc_priv->bank_name ? uc_priv->bank_name : "",
424 desc->offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600425 return -EBUSY;
426 }
427
428 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700429}
430
431/**
432 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
433 * gpio: GPIO number
434 *
435 * This function implements the API that's compatible with current
436 * GPIO API used in U-Boot. The request is forwarded to particular
437 * GPIO driver. Returns 0 on success, negative value on error.
438 */
439int gpio_direction_input(unsigned gpio)
440{
Simon Glassce555292015-01-05 20:05:27 -0700441 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700442 int ret;
443
Simon Glassce555292015-01-05 20:05:27 -0700444 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700445 if (ret)
446 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700447 ret = check_reserved(&desc, "dir_input");
448 if (ret)
449 return ret;
Simon Glasse821d182014-02-26 15:59:24 -0700450
Simon Glassce555292015-01-05 20:05:27 -0700451 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
Simon Glasse821d182014-02-26 15:59:24 -0700452}
453
454/**
455 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
456 * gpio: GPIO number
457 * value: Logical value to be set on the GPIO pin
458 *
459 * This function implements the API that's compatible with current
460 * GPIO API used in U-Boot. The request is forwarded to particular
461 * GPIO driver. Returns 0 on success, negative value on error.
462 */
463int gpio_direction_output(unsigned gpio, int value)
464{
Simon Glassce555292015-01-05 20:05:27 -0700465 struct gpio_desc desc;
466 int ret;
467
468 ret = gpio_to_device(gpio, &desc);
469 if (ret)
470 return ret;
471 ret = check_reserved(&desc, "dir_output");
472 if (ret)
473 return ret;
474
475 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
476 desc.offset, value);
477}
478
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100479static int _gpio_get_value(const struct gpio_desc *desc)
Simon Glassce555292015-01-05 20:05:27 -0700480{
481 int value;
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100482
483 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
484
485 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
486}
487
488int dm_gpio_get_value(const struct gpio_desc *desc)
489{
Simon Glassce555292015-01-05 20:05:27 -0700490 int ret;
491
492 ret = check_reserved(desc, "get_value");
493 if (ret)
494 return ret;
495
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100496 return _gpio_get_value(desc);
Simon Glassce555292015-01-05 20:05:27 -0700497}
498
Simon Glassfd838972016-03-06 19:27:51 -0700499int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassce555292015-01-05 20:05:27 -0700500{
501 int ret;
502
503 ret = check_reserved(desc, "set_value");
504 if (ret)
505 return ret;
506
507 if (desc->flags & GPIOD_ACTIVE_LOW)
508 value = !value;
509 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
510 return 0;
511}
512
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100513static int _dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
Simon Glassce555292015-01-05 20:05:27 -0700514{
515 struct udevice *dev = desc->dev;
516 struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100517 int ret = 0;
Simon Glasse821d182014-02-26 15:59:24 -0700518
Simon Glassce555292015-01-05 20:05:27 -0700519 if (flags & GPIOD_IS_OUT) {
520 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
521
522 if (flags & GPIOD_ACTIVE_LOW)
523 value = !value;
524 ret = ops->direction_output(dev, desc->offset, value);
525 } else if (flags & GPIOD_IS_IN) {
526 ret = ops->direction_input(dev, desc->offset);
527 }
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100528
529 return ret;
530}
531
532int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
533{
534 int ret;
535
536 ret = check_reserved(desc, "set_dir_flags");
Simon Glassce555292015-01-05 20:05:27 -0700537 if (ret)
538 return ret;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100539
540 ret = _dm_gpio_set_dir_flags(desc, flags);
Simon Glassce555292015-01-05 20:05:27 -0700541
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100542 /* update the descriptor flags */
543 if (ret)
544 desc->flags = flags;
545
546 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700547}
548
549int dm_gpio_set_dir(struct gpio_desc *desc)
550{
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100551 int ret;
552
553 ret = check_reserved(desc, "set_dir");
554 if (ret)
555 return ret;
556
557 return _dm_gpio_set_dir_flags(desc, desc->flags);
Simon Glasse821d182014-02-26 15:59:24 -0700558}
559
560/**
561 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
562 * gpio: GPIO number
563 *
564 * This function implements the API that's compatible with current
565 * GPIO API used in U-Boot. The request is forwarded to particular
566 * GPIO driver. Returns the value of the GPIO pin, or negative value
567 * on error.
568 */
569int gpio_get_value(unsigned gpio)
570{
Simon Glasse821d182014-02-26 15:59:24 -0700571 int ret;
572
Simon Glassce555292015-01-05 20:05:27 -0700573 struct gpio_desc desc;
574
575 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700576 if (ret)
577 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700578 return dm_gpio_get_value(&desc);
Simon Glasse821d182014-02-26 15:59:24 -0700579}
580
581/**
582 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
583 * gpio: GPIO number
584 * value: Logical value to be set on the GPIO pin.
585 *
586 * This function implements the API that's compatible with current
587 * GPIO API used in U-Boot. The request is forwarded to particular
588 * GPIO driver. Returns 0 on success, negative value on error.
589 */
590int gpio_set_value(unsigned gpio, int value)
591{
Simon Glassce555292015-01-05 20:05:27 -0700592 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700593 int ret;
594
Simon Glassce555292015-01-05 20:05:27 -0700595 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700596 if (ret)
597 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700598 return dm_gpio_set_value(&desc, value);
Simon Glasse821d182014-02-26 15:59:24 -0700599}
600
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200601const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glasse821d182014-02-26 15:59:24 -0700602{
603 struct gpio_dev_priv *priv;
604
605 /* Must be called on an active device */
Simon Glassde0977b2015-03-05 12:25:20 -0700606 priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -0700607 assert(priv);
608
609 *bit_count = priv->gpio_count;
610 return priv->bank_name;
611}
612
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600613static const char * const gpio_function[GPIOF_COUNT] = {
614 "input",
615 "output",
616 "unused",
617 "unknown",
618 "func",
619};
620
Masahiro Yamada286c2522017-06-22 16:50:25 +0900621static int get_function(struct udevice *dev, int offset, bool skip_unused,
622 const char **namep)
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600623{
Simon Glassde0977b2015-03-05 12:25:20 -0700624 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600625 struct dm_gpio_ops *ops = gpio_get_ops(dev);
626
627 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
628 if (!device_active(dev))
629 return -ENODEV;
630 if (offset < 0 || offset >= uc_priv->gpio_count)
631 return -EINVAL;
632 if (namep)
633 *namep = uc_priv->name[offset];
634 if (skip_unused && !uc_priv->name[offset])
635 return GPIOF_UNUSED;
636 if (ops->get_function) {
637 int ret;
638
639 ret = ops->get_function(dev, offset);
640 if (ret < 0)
641 return ret;
642 if (ret >= ARRAY_SIZE(gpio_function))
643 return -ENODATA;
644 return ret;
645 }
646
647 return GPIOF_UNKNOWN;
648}
649
650int gpio_get_function(struct udevice *dev, int offset, const char **namep)
651{
652 return get_function(dev, offset, true, namep);
653}
654
655int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
656{
657 return get_function(dev, offset, false, namep);
658}
659
Simon Glass6b1ef592014-10-04 11:29:44 -0600660int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
661{
662 struct dm_gpio_ops *ops = gpio_get_ops(dev);
663 struct gpio_dev_priv *priv;
664 char *str = buf;
665 int func;
666 int ret;
667 int len;
668
669 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
670
671 *buf = 0;
Simon Glassde0977b2015-03-05 12:25:20 -0700672 priv = dev_get_uclass_priv(dev);
Simon Glass6b1ef592014-10-04 11:29:44 -0600673 ret = gpio_get_raw_function(dev, offset, NULL);
674 if (ret < 0)
675 return ret;
676 func = ret;
677 len = snprintf(str, buffsize, "%s%d: %s",
678 priv->bank_name ? priv->bank_name : "",
679 offset, gpio_function[func]);
680 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
681 func == GPIOF_UNUSED) {
682 const char *label;
683 bool used;
684
685 ret = ops->get_value(dev, offset);
686 if (ret < 0)
687 return ret;
688 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
689 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
690 ret,
691 used ? 'x' : ' ',
692 used ? " " : "",
693 label ? label : "");
694 }
695
696 return 0;
697}
698
Simon Glassbef54db2015-04-14 21:03:20 -0600699int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
700{
701 int i, ret;
702 int gpio;
703
704 for (i = 0; i < 32; i++) {
705 gpio = gpio_num_array[i];
706 if (gpio == -1)
707 break;
708 ret = gpio_requestf(gpio, fmt, i);
709 if (ret)
710 goto err;
711 ret = gpio_direction_input(gpio);
712 if (ret) {
713 gpio_free(gpio);
714 goto err;
715 }
716 }
717
718 return 0;
719err:
720 for (i--; i >= 0; i--)
721 gpio_free(gpio_num_array[i]);
722
723 return ret;
724}
725
Simon Glass2c97a8f2014-11-10 18:00:21 -0700726/*
727 * get a number comprised of multiple GPIO values. gpio_num_array points to
728 * the array of gpio pin numbers to scan, terminated by -1.
729 */
Simon Glassbef54db2015-04-14 21:03:20 -0600730int gpio_get_values_as_int(const int *gpio_list)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700731{
732 int gpio;
733 unsigned bitmask = 1;
734 unsigned vector = 0;
Simon Glassbef54db2015-04-14 21:03:20 -0600735 int ret;
Simon Glass2c97a8f2014-11-10 18:00:21 -0700736
737 while (bitmask &&
Simon Glassbef54db2015-04-14 21:03:20 -0600738 ((gpio = *gpio_list++) != -1)) {
739 ret = gpio_get_value(gpio);
740 if (ret < 0)
741 return ret;
742 else if (ret)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700743 vector |= bitmask;
744 bitmask <<= 1;
745 }
Simon Glassbef54db2015-04-14 21:03:20 -0600746
Simon Glass2c97a8f2014-11-10 18:00:21 -0700747 return vector;
748}
749
Simon Glassfd838972016-03-06 19:27:51 -0700750int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassdf1687d2016-03-06 19:27:50 -0700751{
752 unsigned bitmask = 1;
753 unsigned vector = 0;
754 int ret, i;
755
756 for (i = 0; i < count; i++) {
757 ret = dm_gpio_get_value(&desc_list[i]);
758 if (ret < 0)
759 return ret;
760 else if (ret)
761 vector |= bitmask;
762 bitmask <<= 1;
763 }
764
765 return vector;
766}
767
Heiko Schocher58e4c382019-07-17 06:59:51 +0200768/**
769 * gpio_request_tail: common work for requesting a gpio.
770 *
771 * ret: return value from previous work in function which calls
772 * this function.
773 * This seems bogus (why calling this function instead not
774 * calling it and end caller function instead?).
775 * Because on error in caller function we want to set some
776 * default values in gpio desc and have a common error
777 * debug message, which provides this function.
778 * nodename: Name of node for which gpio gets requested
779 * used for gpio label name.
780 * args: pointer to output arguments structure
781 * list_name: Name of GPIO list
782 * used for gpio label name.
783 * index: gpio index in gpio list
784 * used for gpio label name.
785 * desc: pointer to gpio descriptor, filled from this
786 * function.
787 * flags: gpio flags to use.
788 * add_index: should index added to gpio label name
789 * gpio_dev: pointer to gpio device from which the gpio
790 * will be requested. If NULL try to get the
791 * gpio device with uclass_get_device_by_ofnode()
792 *
793 * return: In error case this function sets default values in
794 * gpio descriptor, also emmits a debug message.
795 * On success it returns 0 else the error code from
796 * function calls, or the error code passed through
797 * ret to this function.
798 *
799 */
Heiko Schocher39cb3402019-06-12 06:11:46 +0200800static int gpio_request_tail(int ret, const char *nodename,
Simon Glass12faa022017-05-18 20:09:18 -0600801 struct ofnode_phandle_args *args,
802 const char *list_name, int index,
Heiko Schocher39cb3402019-06-12 06:11:46 +0200803 struct gpio_desc *desc, int flags,
Heiko Schocher58e4c382019-07-17 06:59:51 +0200804 bool add_index, struct udevice *gpio_dev)
Simon Glass16e10402015-01-05 20:05:29 -0700805{
Patrick Delaunay758bba92020-01-13 11:35:01 +0100806 gpio_desc_init(desc, gpio_dev, 0);
Simon Glass12faa022017-05-18 20:09:18 -0600807 if (ret)
Simon Glass16e10402015-01-05 20:05:29 -0700808 goto err;
Simon Glass16e10402015-01-05 20:05:29 -0700809
Heiko Schocher39cb3402019-06-12 06:11:46 +0200810 if (!desc->dev) {
811 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
812 &desc->dev);
813 if (ret) {
Heiko Schocher58e4c382019-07-17 06:59:51 +0200814 debug("%s: uclass_get_device_by_ofnode failed\n",
815 __func__);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200816 goto err;
817 }
Simon Glass16e10402015-01-05 20:05:29 -0700818 }
Simon Glass12faa022017-05-18 20:09:18 -0600819 ret = gpio_find_and_xlate(desc, args);
Simon Glass16e10402015-01-05 20:05:29 -0700820 if (ret) {
821 debug("%s: gpio_find_and_xlate failed\n", __func__);
822 goto err;
823 }
824 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Heiko Schocher39cb3402019-06-12 06:11:46 +0200825 nodename, list_name, index);
Simon Glass16e10402015-01-05 20:05:29 -0700826 if (ret) {
827 debug("%s: dm_gpio_requestf failed\n", __func__);
828 goto err;
829 }
830 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
831 if (ret) {
832 debug("%s: dm_gpio_set_dir failed\n", __func__);
833 goto err;
834 }
835
836 return 0;
837err:
838 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Heiko Schocher39cb3402019-06-12 06:11:46 +0200839 __func__, nodename, list_name, index, ret);
Simon Glass16e10402015-01-05 20:05:29 -0700840 return ret;
841}
842
Simon Glass1d9af1f2017-05-30 21:47:09 -0600843static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
844 int index, struct gpio_desc *desc,
845 int flags, bool add_index)
Simon Glass12faa022017-05-18 20:09:18 -0600846{
847 struct ofnode_phandle_args args;
848 int ret;
849
Simon Glass1d9af1f2017-05-30 21:47:09 -0600850 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
851 index, &args);
Simon Glass12faa022017-05-18 20:09:18 -0600852
Heiko Schocher39cb3402019-06-12 06:11:46 +0200853 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
854 index, desc, flags, add_index, NULL);
Simon Glass12faa022017-05-18 20:09:18 -0600855}
856
Simon Glass1d9af1f2017-05-30 21:47:09 -0600857int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -0700858 struct gpio_desc *desc, int flags)
859{
Simon Glass1d9af1f2017-05-30 21:47:09 -0600860 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
861 index > 0);
Simon Glass16e10402015-01-05 20:05:29 -0700862}
863
Simon Glass1d9af1f2017-05-30 21:47:09 -0600864int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -0700865 struct gpio_desc *desc, int flags)
866{
Simon Glass1d9af1f2017-05-30 21:47:09 -0600867 struct ofnode_phandle_args args;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200868 ofnode node;
Simon Glass1d9af1f2017-05-30 21:47:09 -0600869 int ret;
870
871 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
872 index, &args);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200873 node = dev_ofnode(dev);
874 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
875 index, desc, flags, index > 0, NULL);
Simon Glass16e10402015-01-05 20:05:29 -0700876}
877
Simon Glass1d9af1f2017-05-30 21:47:09 -0600878int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass16e10402015-01-05 20:05:29 -0700879 struct gpio_desc *desc, int max_count,
880 int flags)
881{
882 int count;
883 int ret;
884
Przemyslaw Marczak44fc5362015-03-31 18:57:16 +0200885 for (count = 0; count < max_count; count++) {
Simon Glass1d9af1f2017-05-30 21:47:09 -0600886 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass16e10402015-01-05 20:05:29 -0700887 &desc[count], flags, true);
888 if (ret == -ENOENT)
889 break;
890 else if (ret)
891 goto err;
892 }
893
894 /* We ran out of GPIOs in the list */
895 return count;
896
897err:
898 gpio_free_list_nodev(desc, count - 1);
899
900 return ret;
901}
902
903int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
904 struct gpio_desc *desc, int max_count,
905 int flags)
906{
907 /*
908 * This isn't ideal since we don't use dev->name in the debug()
909 * calls in gpio_request_by_name(), but we can do this until
910 * gpio_request_list_by_name_nodev() can be dropped.
911 */
Simon Glass1d9af1f2017-05-30 21:47:09 -0600912 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
913 max_count, flags);
Simon Glass16e10402015-01-05 20:05:29 -0700914}
915
916int gpio_get_list_count(struct udevice *dev, const char *list_name)
917{
918 int ret;
919
Simon Glassdd79d6e2017-01-17 16:52:55 -0700920 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
Simon Glass16e10402015-01-05 20:05:29 -0700921 list_name, "#gpio-cells", 0, -1,
922 NULL);
923 if (ret) {
924 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
925 __func__, dev->name, list_name, ret);
926 }
927
928 return ret;
929}
930
931int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
932{
933 /* For now, we don't do any checking of dev */
934 return _dm_gpio_free(desc->dev, desc->offset);
935}
936
937int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
938{
939 int i;
940
941 /* For now, we don't do any checking of dev */
942 for (i = 0; i < count; i++)
943 dm_gpio_free(dev, &desc[i]);
944
945 return 0;
946}
947
948int gpio_free_list_nodev(struct gpio_desc *desc, int count)
949{
950 return gpio_free_list(NULL, desc, count);
951}
952
Simon Glasse821d182014-02-26 15:59:24 -0700953/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glass0f4517d2014-10-04 11:29:42 -0600954static int gpio_renumber(struct udevice *removed_dev)
Simon Glasse821d182014-02-26 15:59:24 -0700955{
956 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200957 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -0700958 struct uclass *uc;
959 unsigned base;
960 int ret;
961
962 ret = uclass_get(UCLASS_GPIO, &uc);
963 if (ret)
964 return ret;
965
966 /* Ensure that we have a base for each bank */
967 base = 0;
968 uclass_foreach_dev(dev, uc) {
Simon Glass0f4517d2014-10-04 11:29:42 -0600969 if (device_active(dev) && dev != removed_dev) {
Simon Glassde0977b2015-03-05 12:25:20 -0700970 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -0700971 uc_priv->gpio_base = base;
972 base += uc_priv->gpio_count;
973 }
974 }
975
976 return 0;
977}
978
Simon Glassfd838972016-03-06 19:27:51 -0700979int gpio_get_number(const struct gpio_desc *desc)
Simon Glass94f54d12015-03-25 12:21:58 -0600980{
981 struct udevice *dev = desc->dev;
982 struct gpio_dev_priv *uc_priv;
983
984 if (!dev)
985 return -1;
986 uc_priv = dev->uclass_priv;
987
988 return uc_priv->gpio_base + desc->offset;
989}
990
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200991static int gpio_post_probe(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -0700992{
Simon Glassde0977b2015-03-05 12:25:20 -0700993 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600994
995 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
996 if (!uc_priv->name)
997 return -ENOMEM;
998
999 return gpio_renumber(NULL);
Simon Glasse821d182014-02-26 15:59:24 -07001000}
1001
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001002static int gpio_pre_remove(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001003{
Simon Glassde0977b2015-03-05 12:25:20 -07001004 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001005 int i;
1006
1007 for (i = 0; i < uc_priv->gpio_count; i++) {
1008 if (uc_priv->name[i])
1009 free(uc_priv->name[i]);
1010 }
1011 free(uc_priv->name);
1012
1013 return gpio_renumber(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001014}
1015
Heiko Schocher39cb3402019-06-12 06:11:46 +02001016int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1017 char *list_name, int index, int flags,
1018 int dtflags, struct gpio_desc *desc)
1019{
1020 struct ofnode_phandle_args args;
1021
1022 args.node = ofnode_null();
1023 args.args_count = 2;
1024 args.args[0] = index;
1025 args.args[1] = dtflags;
1026
1027 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1028 flags, 0, dev);
1029}
1030
Michal Simek5f7202c2018-07-12 12:42:27 +02001031static int gpio_post_bind(struct udevice *dev)
1032{
Heiko Schocher39cb3402019-06-12 06:11:46 +02001033 struct udevice *child;
1034 ofnode node;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001035
Michal Simek5f7202c2018-07-12 12:42:27 +02001036#if defined(CONFIG_NEEDS_MANUAL_RELOC)
1037 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1038 static int reloc_done;
1039
1040 if (!reloc_done) {
1041 if (ops->request)
1042 ops->request += gd->reloc_off;
Simon Glassb3a47542020-02-04 20:15:17 -07001043 if (ops->rfree)
1044 ops->rfree += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001045 if (ops->direction_input)
1046 ops->direction_input += gd->reloc_off;
1047 if (ops->direction_output)
1048 ops->direction_output += gd->reloc_off;
1049 if (ops->get_value)
1050 ops->get_value += gd->reloc_off;
1051 if (ops->set_value)
1052 ops->set_value += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001053 if (ops->get_function)
1054 ops->get_function += gd->reloc_off;
1055 if (ops->xlate)
1056 ops->xlate += gd->reloc_off;
1057
1058 reloc_done++;
1059 }
1060#endif
Heiko Schocher39cb3402019-06-12 06:11:46 +02001061
Heiko Schocher58e4c382019-07-17 06:59:51 +02001062 if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1063 dev_for_each_subnode(node, dev) {
1064 if (ofnode_read_bool(node, "gpio-hog")) {
1065 const char *name = ofnode_get_name(node);
1066 int ret;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001067
Heiko Schocher58e4c382019-07-17 06:59:51 +02001068 ret = device_bind_driver_to_node(dev,
1069 "gpio_hog",
1070 name, node,
1071 &child);
1072 if (ret)
1073 return ret;
1074 }
Heiko Schocher39cb3402019-06-12 06:11:46 +02001075 }
1076 }
Michal Simek5f7202c2018-07-12 12:42:27 +02001077 return 0;
1078}
1079
Simon Glasse821d182014-02-26 15:59:24 -07001080UCLASS_DRIVER(gpio) = {
1081 .id = UCLASS_GPIO,
1082 .name = "gpio",
Bhuvanchandra DVb1a1fc92015-06-01 18:37:15 +05301083 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glasse821d182014-02-26 15:59:24 -07001084 .post_probe = gpio_post_probe,
Michal Simek5f7202c2018-07-12 12:42:27 +02001085 .post_bind = gpio_post_bind,
Simon Glasse821d182014-02-26 15:59:24 -07001086 .pre_remove = gpio_pre_remove,
1087 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
1088};