blob: ab17fa8a5d8ef9e259e2eb4be4d686c9c2ab28a6 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass96495d92014-02-26 15:59:24 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glass96495d92014-02-26 15:59:24 -07004 */
5
6#include <common.h>
7#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02009#include <dm/device-internal.h>
10#include <dm/lists.h>
11#include <dm/uclass-internal.h>
Eric Nelson6c880b72016-04-24 16:32:40 -070012#include <dt-bindings/gpio/gpio.h>
Simon Glass96495d92014-02-26 15:59:24 -070013#include <errno.h>
Simon Glass0dac4d52015-01-05 20:05:28 -070014#include <fdtdec.h>
Simon Glassb892d122014-10-04 11:29:42 -060015#include <malloc.h>
Simon Glass96495d92014-02-26 15:59:24 -070016#include <asm/gpio.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060017#include <dm/device_compat.h>
Masahiro Yamada84b8bf62016-01-24 23:27:48 +090018#include <linux/bug.h>
Simon Glassfe1ef502014-10-22 21:37:01 -060019#include <linux/ctype.h>
Simon Glass96495d92014-02-26 15:59:24 -070020
Simon Glass3669e0e2015-01-05 20:05:29 -070021DECLARE_GLOBAL_DATA_PTR;
22
Simon Glass96495d92014-02-26 15:59:24 -070023/**
Patrick Delaunay9f2b0662020-01-13 11:35:01 +010024 * gpio_desc_init() - Initialize the GPIO descriptor
25 *
26 * @desc: GPIO descriptor to initialize
27 * @dev: GPIO device
28 * @offset: Offset of device GPIO
29 */
30static void gpio_desc_init(struct gpio_desc *desc,
31 struct udevice *dev,
32 uint offset)
33{
34 desc->dev = dev;
35 desc->offset = offset;
36 desc->flags = 0;
37}
38
39/**
Simon Glass96495d92014-02-26 15:59:24 -070040 * gpio_to_device() - Convert global GPIO number to device, number
Simon Glass96495d92014-02-26 15:59:24 -070041 *
42 * Convert the GPIO number to an entry in the list of GPIOs
43 * or GPIO blocks registered with the GPIO controller. Returns
44 * entry on success, NULL on error.
Simon Glassae7123f2015-01-05 20:05:27 -070045 *
46 * @gpio: The numeric representation of the GPIO
47 * @desc: Returns description (desc->flags will always be 0)
48 * @return 0 if found, -ENOENT if not found
Simon Glass96495d92014-02-26 15:59:24 -070049 */
Simon Glassae7123f2015-01-05 20:05:27 -070050static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glass96495d92014-02-26 15:59:24 -070051{
52 struct gpio_dev_priv *uc_priv;
Heiko Schocher54c5d082014-05-22 12:43:05 +020053 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -070054 int ret;
55
56 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
57 dev;
58 ret = uclass_next_device(&dev)) {
Simon Glasse564f052015-03-05 12:25:20 -070059 uc_priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -070060 if (gpio >= uc_priv->gpio_base &&
61 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
Patrick Delaunay9f2b0662020-01-13 11:35:01 +010062 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
Simon Glass96495d92014-02-26 15:59:24 -070063 return 0;
64 }
65 }
66
67 /* No such GPIO */
Simon Glassae7123f2015-01-05 20:05:27 -070068 return ret ? ret : -ENOENT;
Simon Glass96495d92014-02-26 15:59:24 -070069}
70
Heiko Schocher2bd261d2020-05-22 11:08:59 +020071#if CONFIG_IS_ENABLED(DM_GPIO_LOOKUP_LABEL)
72/**
73 * dm_gpio_lookup_label() - look for name in gpio device
74 *
75 * search in uc_priv, if there is a gpio with labelname same
76 * as name.
77 *
78 * @name: name which is searched
79 * @uc_priv: gpio_dev_priv pointer.
80 * @offset: gpio offset within the device
81 * @return: 0 if found, -ENOENT if not.
82 */
83static int dm_gpio_lookup_label(const char *name,
84 struct gpio_dev_priv *uc_priv, ulong *offset)
85{
86 int len;
87 int i;
88
89 *offset = -1;
90 len = strlen(name);
91 for (i = 0; i < uc_priv->gpio_count; i++) {
92 if (!uc_priv->name[i])
93 continue;
94 if (!strncmp(name, uc_priv->name[i], len)) {
95 *offset = i;
96 return 0;
97 }
98 }
99 return -ENOENT;
100}
101#else
102static int
103dm_gpio_lookup_label(const char *name, struct gpio_dev_priv *uc_priv,
104 ulong *offset)
105{
106 return -ENOENT;
107}
108#endif
109
Simon Glass32ec1592015-06-23 15:38:40 -0600110int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
Simon Glass96495d92014-02-26 15:59:24 -0700111{
Simon Glassfe1ef502014-10-22 21:37:01 -0600112 struct gpio_dev_priv *uc_priv = NULL;
Heiko Schocher54c5d082014-05-22 12:43:05 +0200113 struct udevice *dev;
Simon Glassfe1ef502014-10-22 21:37:01 -0600114 ulong offset;
115 int numeric;
Simon Glass96495d92014-02-26 15:59:24 -0700116 int ret;
117
Simon Glassfe1ef502014-10-22 21:37:01 -0600118 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
Simon Glass96495d92014-02-26 15:59:24 -0700119 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
120 dev;
121 ret = uclass_next_device(&dev)) {
Simon Glass96495d92014-02-26 15:59:24 -0700122 int len;
123
Simon Glasse564f052015-03-05 12:25:20 -0700124 uc_priv = dev_get_uclass_priv(dev);
Simon Glassfe1ef502014-10-22 21:37:01 -0600125 if (numeric != -1) {
126 offset = numeric - uc_priv->gpio_base;
127 /* Allow GPIOs to be numbered from 0 */
Tom Rini75897912017-05-10 15:20:15 -0400128 if (offset < uc_priv->gpio_count)
Simon Glassfe1ef502014-10-22 21:37:01 -0600129 break;
130 }
131
Simon Glass96495d92014-02-26 15:59:24 -0700132 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
133
Simon Glass939cda52014-06-11 23:29:47 -0600134 if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glassfe1ef502014-10-22 21:37:01 -0600135 if (!strict_strtoul(name + len, 10, &offset))
136 break;
Simon Glass96495d92014-02-26 15:59:24 -0700137 }
Heiko Schocher2bd261d2020-05-22 11:08:59 +0200138
139 /*
140 * if we did not found a gpio through its bank
141 * name, we search for a valid gpio label.
142 */
143 if (!dm_gpio_lookup_label(name, uc_priv, &offset))
144 break;
Simon Glass96495d92014-02-26 15:59:24 -0700145 }
146
Simon Glassfe1ef502014-10-22 21:37:01 -0600147 if (!dev)
148 return ret ? ret : -EINVAL;
149
Patrick Delaunay9f2b0662020-01-13 11:35:01 +0100150 gpio_desc_init(desc, dev, offset);
Simon Glass32ec1592015-06-23 15:38:40 -0600151
152 return 0;
153}
154
155int gpio_lookup_name(const char *name, struct udevice **devp,
156 unsigned int *offsetp, unsigned int *gpiop)
157{
158 struct gpio_desc desc;
159 int ret;
160
Simon Glassfe1ef502014-10-22 21:37:01 -0600161 if (devp)
Simon Glass32ec1592015-06-23 15:38:40 -0600162 *devp = NULL;
163 ret = dm_gpio_lookup_name(name, &desc);
164 if (ret)
165 return ret;
166
167 if (devp)
168 *devp = desc.dev;
Simon Glassfe1ef502014-10-22 21:37:01 -0600169 if (offsetp)
Simon Glass32ec1592015-06-23 15:38:40 -0600170 *offsetp = desc.offset;
171 if (gpiop) {
172 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
173
174 *gpiop = uc_priv->gpio_base + desc.offset;
175 }
Simon Glassfe1ef502014-10-22 21:37:01 -0600176
177 return 0;
Simon Glass96495d92014-02-26 15:59:24 -0700178}
179
Simon Glass3a571232017-05-18 20:09:18 -0600180int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
181 struct ofnode_phandle_args *args)
Eric Nelson6c880b72016-04-24 16:32:40 -0700182{
183 if (args->args_count < 1)
184 return -EINVAL;
185
186 desc->offset = args->args[0];
187
188 if (args->args_count < 2)
189 return 0;
190
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100191 desc->flags = 0;
Eric Nelson6c880b72016-04-24 16:32:40 -0700192 if (args->args[1] & GPIO_ACTIVE_LOW)
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100193 desc->flags |= GPIOD_ACTIVE_LOW;
Eric Nelson6c880b72016-04-24 16:32:40 -0700194
Patrick Delaunay477ca572020-01-13 11:35:07 +0100195 /*
196 * need to test 2 bits for gpio output binding:
197 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
198 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
199 */
200 if (args->args[1] & GPIO_SINGLE_ENDED) {
201 if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
202 desc->flags |= GPIOD_OPEN_DRAIN;
203 else
204 desc->flags |= GPIOD_OPEN_SOURCE;
205 }
206
207 if (args->args[1] & GPIO_PULL_UP)
208 desc->flags |= GPIOD_PULL_UP;
209
210 if (args->args[1] & GPIO_PULL_DOWN)
211 desc->flags |= GPIOD_PULL_DOWN;
212
Eric Nelson6c880b72016-04-24 16:32:40 -0700213 return 0;
214}
215
Simon Glass3669e0e2015-01-05 20:05:29 -0700216static int gpio_find_and_xlate(struct gpio_desc *desc,
Simon Glass3a571232017-05-18 20:09:18 -0600217 struct ofnode_phandle_args *args)
Simon Glass0dac4d52015-01-05 20:05:28 -0700218{
219 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
220
Eric Nelson6c880b72016-04-24 16:32:40 -0700221 if (ops->xlate)
222 return ops->xlate(desc->dev, desc, args);
Simon Glass0dac4d52015-01-05 20:05:28 -0700223 else
Eric Nelson6c880b72016-04-24 16:32:40 -0700224 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glass0dac4d52015-01-05 20:05:28 -0700225}
226
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200227#if defined(CONFIG_GPIO_HOG)
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200228
229struct gpio_hog_priv {
230 struct gpio_desc gpiod;
231};
232
233struct gpio_hog_data {
234 int gpiod_flags;
235 int value;
236 u32 val[2];
237};
238
239static int gpio_hog_ofdata_to_platdata(struct udevice *dev)
240{
241 struct gpio_hog_data *plat = dev_get_platdata(dev);
242 const char *nodename;
243 int ret;
244
245 plat->value = 0;
246 if (dev_read_bool(dev, "input")) {
247 plat->gpiod_flags = GPIOD_IS_IN;
248 } else if (dev_read_bool(dev, "output-high")) {
249 plat->value = 1;
250 plat->gpiod_flags = GPIOD_IS_OUT;
251 } else if (dev_read_bool(dev, "output-low")) {
252 plat->gpiod_flags = GPIOD_IS_OUT;
253 } else {
254 printf("%s: missing gpio-hog state.\n", __func__);
255 return -EINVAL;
256 }
257 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
258 if (ret) {
259 printf("%s: wrong gpios property, 2 values needed %d\n",
260 __func__, ret);
261 return ret;
262 }
263 nodename = dev_read_string(dev, "line-name");
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200264 if (nodename)
265 device_set_name(dev, nodename);
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200266
267 return 0;
268}
269
270static int gpio_hog_probe(struct udevice *dev)
271{
272 struct gpio_hog_data *plat = dev_get_platdata(dev);
273 struct gpio_hog_priv *priv = dev_get_priv(dev);
274 int ret;
275
276 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
277 plat->val[0], plat->gpiod_flags,
278 plat->val[1], &priv->gpiod);
279 if (ret < 0) {
280 debug("%s: node %s could not get gpio.\n", __func__,
281 dev->name);
282 return ret;
283 }
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200284
285 if (plat->gpiod_flags == GPIOD_IS_OUT) {
286 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
287 if (ret < 0) {
288 debug("%s: node %s could not set gpio.\n", __func__,
289 dev->name);
290 return ret;
291 }
292 }
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200293
294 return 0;
295}
296
297int gpio_hog_probe_all(void)
298{
299 struct udevice *dev;
300 int ret;
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200301 int retval = 0;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200302
303 for (uclass_first_device(UCLASS_NOP, &dev);
304 dev;
305 uclass_find_next_device(&dev)) {
306 if (dev->driver == DM_GET_DRIVER(gpio_hog)) {
307 ret = device_probe(dev);
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200308 if (ret) {
309 printf("Failed to probe device %s err: %d\n",
310 dev->name, ret);
311 retval = ret;
312 }
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200313 }
314 }
315
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200316 return retval;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200317}
318
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200319int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200320{
321 struct udevice *dev;
322
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200323 *desc = NULL;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200324 gpio_hog_probe_all();
325 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
326 struct gpio_hog_priv *priv = dev_get_priv(dev);
327
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200328 *desc = &priv->gpiod;
329 return 0;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200330 }
331
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200332 return -ENODEV;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200333}
334
335U_BOOT_DRIVER(gpio_hog) = {
336 .name = "gpio_hog",
337 .id = UCLASS_NOP,
338 .ofdata_to_platdata = gpio_hog_ofdata_to_platdata,
339 .probe = gpio_hog_probe,
340 .priv_auto_alloc_size = sizeof(struct gpio_hog_priv),
341 .platdata_auto_alloc_size = sizeof(struct gpio_hog_data),
342};
343#else
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200344int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200345{
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200346 return 0;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200347}
348#endif
349
Simon Glassefa677f2015-06-23 15:38:41 -0600350int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassae7123f2015-01-05 20:05:27 -0700351{
352 struct udevice *dev = desc->dev;
353 struct gpio_dev_priv *uc_priv;
354 char *str;
355 int ret;
356
Simon Glasse564f052015-03-05 12:25:20 -0700357 uc_priv = dev_get_uclass_priv(dev);
Simon Glassae7123f2015-01-05 20:05:27 -0700358 if (uc_priv->name[desc->offset])
359 return -EBUSY;
360 str = strdup(label);
361 if (!str)
362 return -ENOMEM;
363 if (gpio_get_ops(dev)->request) {
364 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
365 if (ret) {
366 free(str);
367 return ret;
368 }
369 }
370 uc_priv->name[desc->offset] = str;
371
372 return 0;
373}
374
Simon Glass3669e0e2015-01-05 20:05:29 -0700375static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
376{
Simon Glass27084c02019-09-25 08:56:27 -0600377#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass3669e0e2015-01-05 20:05:29 -0700378 va_list args;
379 char buf[40];
380
381 va_start(args, fmt);
382 vscnprintf(buf, sizeof(buf), fmt, args);
383 va_end(args);
384 return dm_gpio_request(desc, buf);
Simon Glass4dc52592015-12-29 05:22:48 -0700385#else
386 return dm_gpio_request(desc, fmt);
387#endif
Simon Glass3669e0e2015-01-05 20:05:29 -0700388}
389
Simon Glass96495d92014-02-26 15:59:24 -0700390/**
391 * gpio_request() - [COMPAT] Request GPIO
392 * gpio: GPIO number
393 * label: Name for the requested GPIO
394 *
Simon Glassb892d122014-10-04 11:29:42 -0600395 * The label is copied and allocated so the caller does not need to keep
396 * the pointer around.
397 *
Simon Glass96495d92014-02-26 15:59:24 -0700398 * This function implements the API that's compatible with current
399 * GPIO API used in U-Boot. The request is forwarded to particular
400 * GPIO driver. Returns 0 on success, negative value on error.
401 */
402int gpio_request(unsigned gpio, const char *label)
403{
Simon Glassae7123f2015-01-05 20:05:27 -0700404 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700405 int ret;
406
Simon Glassae7123f2015-01-05 20:05:27 -0700407 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700408 if (ret)
409 return ret;
410
Simon Glassae7123f2015-01-05 20:05:27 -0700411 return dm_gpio_request(&desc, label);
Simon Glass96495d92014-02-26 15:59:24 -0700412}
413
414/**
Simon Glassd44f5972014-10-04 11:29:49 -0600415 * gpio_requestf() - [COMPAT] Request GPIO
416 * @gpio: GPIO number
417 * @fmt: Format string for the requested GPIO
418 * @...: Arguments for the printf() format string
419 *
420 * This function implements the API that's compatible with current
421 * GPIO API used in U-Boot. The request is forwarded to particular
422 * GPIO driver. Returns 0 on success, negative value on error.
423 */
424int gpio_requestf(unsigned gpio, const char *fmt, ...)
425{
Simon Glass27084c02019-09-25 08:56:27 -0600426#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glassd44f5972014-10-04 11:29:49 -0600427 va_list args;
428 char buf[40];
429
430 va_start(args, fmt);
431 vscnprintf(buf, sizeof(buf), fmt, args);
432 va_end(args);
433 return gpio_request(gpio, buf);
Simon Glass4dc52592015-12-29 05:22:48 -0700434#else
435 return gpio_request(gpio, fmt);
436#endif
Simon Glassd44f5972014-10-04 11:29:49 -0600437}
438
Simon Glassae7123f2015-01-05 20:05:27 -0700439int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glass96495d92014-02-26 15:59:24 -0700440{
Simon Glassb892d122014-10-04 11:29:42 -0600441 struct gpio_dev_priv *uc_priv;
Simon Glass96495d92014-02-26 15:59:24 -0700442 int ret;
443
Simon Glasse564f052015-03-05 12:25:20 -0700444 uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -0600445 if (!uc_priv->name[offset])
446 return -ENXIO;
Simon Glass093152f2020-02-04 20:15:17 -0700447 if (gpio_get_ops(dev)->rfree) {
448 ret = gpio_get_ops(dev)->rfree(dev, offset);
Simon Glassb892d122014-10-04 11:29:42 -0600449 if (ret)
450 return ret;
451 }
452
453 free(uc_priv->name[offset]);
454 uc_priv->name[offset] = NULL;
455
456 return 0;
457}
458
Simon Glassae7123f2015-01-05 20:05:27 -0700459/**
460 * gpio_free() - [COMPAT] Relinquish GPIO
461 * gpio: GPIO number
462 *
463 * This function implements the API that's compatible with current
464 * GPIO API used in U-Boot. The request is forwarded to particular
465 * GPIO driver. Returns 0 on success, negative value on error.
466 */
467int gpio_free(unsigned gpio)
Simon Glassb892d122014-10-04 11:29:42 -0600468{
Simon Glassae7123f2015-01-05 20:05:27 -0700469 struct gpio_desc desc;
470 int ret;
Simon Glassb892d122014-10-04 11:29:42 -0600471
Simon Glassae7123f2015-01-05 20:05:27 -0700472 ret = gpio_to_device(gpio, &desc);
473 if (ret)
474 return ret;
475
476 return _dm_gpio_free(desc.dev, desc.offset);
477}
478
Simon Glass17c43f12016-03-06 19:27:51 -0700479static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glassae7123f2015-01-05 20:05:27 -0700480{
Simon Glasseca48662015-07-02 18:16:16 -0600481 struct gpio_dev_priv *uc_priv;
Simon Glassae7123f2015-01-05 20:05:27 -0700482
Simon Glasseca48662015-07-02 18:16:16 -0600483 if (!dm_gpio_is_valid(desc))
484 return -ENOENT;
485
486 uc_priv = dev_get_uclass_priv(desc->dev);
Simon Glassae7123f2015-01-05 20:05:27 -0700487 if (!uc_priv->name[desc->offset]) {
Simon Glassb892d122014-10-04 11:29:42 -0600488 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassae7123f2015-01-05 20:05:27 -0700489 desc->dev->name, func,
490 uc_priv->bank_name ? uc_priv->bank_name : "",
491 desc->offset);
Simon Glassb892d122014-10-04 11:29:42 -0600492 return -EBUSY;
493 }
494
495 return 0;
Simon Glass96495d92014-02-26 15:59:24 -0700496}
497
498/**
499 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
500 * gpio: GPIO number
501 *
502 * This function implements the API that's compatible with current
503 * GPIO API used in U-Boot. The request is forwarded to particular
504 * GPIO driver. Returns 0 on success, negative value on error.
505 */
506int gpio_direction_input(unsigned gpio)
507{
Simon Glassae7123f2015-01-05 20:05:27 -0700508 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700509 int ret;
510
Simon Glassae7123f2015-01-05 20:05:27 -0700511 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700512 if (ret)
513 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700514 ret = check_reserved(&desc, "dir_input");
515 if (ret)
516 return ret;
Simon Glass96495d92014-02-26 15:59:24 -0700517
Simon Glassae7123f2015-01-05 20:05:27 -0700518 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
Simon Glass96495d92014-02-26 15:59:24 -0700519}
520
521/**
522 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
523 * gpio: GPIO number
524 * value: Logical value to be set on the GPIO pin
525 *
526 * This function implements the API that's compatible with current
527 * GPIO API used in U-Boot. The request is forwarded to particular
528 * GPIO driver. Returns 0 on success, negative value on error.
529 */
530int gpio_direction_output(unsigned gpio, int value)
531{
Simon Glassae7123f2015-01-05 20:05:27 -0700532 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700533 int ret;
534
Simon Glassae7123f2015-01-05 20:05:27 -0700535 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700536 if (ret)
537 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700538 ret = check_reserved(&desc, "dir_output");
539 if (ret)
540 return ret;
Simon Glass96495d92014-02-26 15:59:24 -0700541
Simon Glassae7123f2015-01-05 20:05:27 -0700542 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
543 desc.offset, value);
544}
545
Patrick Delaunay8a9140c2020-01-13 11:35:02 +0100546static int _gpio_get_value(const struct gpio_desc *desc)
Simon Glassae7123f2015-01-05 20:05:27 -0700547{
548 int value;
Patrick Delaunay8a9140c2020-01-13 11:35:02 +0100549
550 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
551
552 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
553}
554
555int dm_gpio_get_value(const struct gpio_desc *desc)
556{
Simon Glassae7123f2015-01-05 20:05:27 -0700557 int ret;
558
559 ret = check_reserved(desc, "get_value");
560 if (ret)
561 return ret;
562
Patrick Delaunay8a9140c2020-01-13 11:35:02 +0100563 return _gpio_get_value(desc);
Simon Glassae7123f2015-01-05 20:05:27 -0700564}
565
Simon Glass17c43f12016-03-06 19:27:51 -0700566int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassae7123f2015-01-05 20:05:27 -0700567{
568 int ret;
569
570 ret = check_reserved(desc, "set_value");
571 if (ret)
572 return ret;
573
574 if (desc->flags & GPIOD_ACTIVE_LOW)
575 value = !value;
Neil Armstrong47bd5332020-05-05 10:43:17 +0200576
577 /*
578 * Emulate open drain by not actively driving the line high or
579 * Emulate open source by not actively driving the line low
580 */
581 if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
582 (desc->flags & GPIOD_OPEN_SOURCE && !value))
583 return gpio_get_ops(desc->dev)->direction_input(desc->dev,
584 desc->offset);
585 else if (desc->flags & GPIOD_OPEN_DRAIN ||
586 desc->flags & GPIOD_OPEN_SOURCE)
587 return gpio_get_ops(desc->dev)->direction_output(desc->dev,
588 desc->offset,
589 value);
590
Simon Glassae7123f2015-01-05 20:05:27 -0700591 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
592 return 0;
593}
594
Patrick Delaunay4292fb12020-01-13 11:35:04 +0100595/* check dir flags invalid configuration */
596static int check_dir_flags(ulong flags)
597{
598 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
599 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
600 __func__, flags);
601 return -EINVAL;
602 }
603
Patrick Delaunay477ca572020-01-13 11:35:07 +0100604 if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
605 log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
606 __func__, flags);
607 return -EINVAL;
608 }
609
610 if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
611 log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
612 __func__, flags);
613 return -EINVAL;
614 }
615
Patrick Delaunay4292fb12020-01-13 11:35:04 +0100616 return 0;
617}
618
Patrick Delaunay788ea832020-01-13 11:35:03 +0100619static int _dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
Simon Glassae7123f2015-01-05 20:05:27 -0700620{
621 struct udevice *dev = desc->dev;
622 struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunay4292fb12020-01-13 11:35:04 +0100623 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Patrick Delaunay788ea832020-01-13 11:35:03 +0100624 int ret = 0;
Simon Glassae7123f2015-01-05 20:05:27 -0700625
Patrick Delaunay4292fb12020-01-13 11:35:04 +0100626 ret = check_dir_flags(flags);
627 if (ret) {
628 dev_dbg(dev,
629 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
630 desc->dev->name,
631 uc_priv->bank_name ? uc_priv->bank_name : "",
632 desc->offset, flags);
633
634 return ret;
635 }
636
Patrick Delaunay8fd9daf2020-01-13 11:35:09 +0100637 /* GPIOD_ are directly managed by driver in set_dir_flags*/
638 if (ops->set_dir_flags) {
639 ret = ops->set_dir_flags(dev, desc->offset, flags);
640 } else {
641 if (flags & GPIOD_IS_OUT) {
642 ret = ops->direction_output(dev, desc->offset,
643 GPIOD_FLAGS_OUTPUT(flags));
644 } else if (flags & GPIOD_IS_IN) {
645 ret = ops->direction_input(dev, desc->offset);
646 }
Simon Glassae7123f2015-01-05 20:05:27 -0700647 }
Patrick Delaunay788ea832020-01-13 11:35:03 +0100648
Heiko Schochercd2faeb2020-05-22 11:08:56 +0200649 /* save the flags also in descriptor */
650 if (!ret)
651 desc->flags = flags;
652
Patrick Delaunay788ea832020-01-13 11:35:03 +0100653 return ret;
654}
655
656int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
657{
658 int ret;
659
660 ret = check_reserved(desc, "set_dir_flags");
Simon Glassae7123f2015-01-05 20:05:27 -0700661 if (ret)
662 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700663
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100664 /* combine the requested flags (for IN/OUT) and the descriptor flags */
665 flags |= desc->flags;
Patrick Delaunay788ea832020-01-13 11:35:03 +0100666 ret = _dm_gpio_set_dir_flags(desc, flags);
667
Patrick Delaunay788ea832020-01-13 11:35:03 +0100668 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700669}
670
671int dm_gpio_set_dir(struct gpio_desc *desc)
672{
Patrick Delaunay788ea832020-01-13 11:35:03 +0100673 int ret;
674
675 ret = check_reserved(desc, "set_dir");
676 if (ret)
677 return ret;
678
679 return _dm_gpio_set_dir_flags(desc, desc->flags);
Simon Glass96495d92014-02-26 15:59:24 -0700680}
681
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100682int dm_gpio_get_dir_flags(struct gpio_desc *desc, ulong *flags)
683{
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100684 struct udevice *dev = desc->dev;
685 int ret, value;
686 struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100687 ulong dir_flags;
688
689 ret = check_reserved(desc, "get_dir_flags");
690 if (ret)
691 return ret;
692
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100693 /* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
694 if (ops->get_dir_flags) {
695 ret = ops->get_dir_flags(dev, desc->offset, &dir_flags);
696 if (ret)
697 return ret;
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100698
Patrick Delaunayd2c07e52020-01-13 11:35:08 +0100699 /* GPIOD_ACTIVE_LOW is saved in desc->flags */
700 value = dir_flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
701 if (desc->flags & GPIOD_ACTIVE_LOW)
702 value = !value;
703 dir_flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
704 dir_flags |= (desc->flags & GPIOD_ACTIVE_LOW);
705 if (value)
706 dir_flags |= GPIOD_IS_OUT_ACTIVE;
707 } else {
708 dir_flags = desc->flags;
709 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
710 dir_flags &= ~GPIOD_IS_OUT_ACTIVE;
711 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
712 dir_flags |= GPIOD_IS_OUT_ACTIVE;
713 }
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100714 *flags = dir_flags;
715
716 return 0;
717}
718
Simon Glass96495d92014-02-26 15:59:24 -0700719/**
720 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
721 * gpio: GPIO number
722 *
723 * This function implements the API that's compatible with current
724 * GPIO API used in U-Boot. The request is forwarded to particular
725 * GPIO driver. Returns the value of the GPIO pin, or negative value
726 * on error.
727 */
728int gpio_get_value(unsigned gpio)
729{
Simon Glass96495d92014-02-26 15:59:24 -0700730 int ret;
731
Simon Glassae7123f2015-01-05 20:05:27 -0700732 struct gpio_desc desc;
733
734 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700735 if (ret)
736 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700737 return dm_gpio_get_value(&desc);
Simon Glass96495d92014-02-26 15:59:24 -0700738}
739
740/**
741 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
742 * gpio: GPIO number
743 * value: Logical value to be set on the GPIO pin.
744 *
745 * This function implements the API that's compatible with current
746 * GPIO API used in U-Boot. The request is forwarded to particular
747 * GPIO driver. Returns 0 on success, negative value on error.
748 */
749int gpio_set_value(unsigned gpio, int value)
750{
Simon Glassae7123f2015-01-05 20:05:27 -0700751 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700752 int ret;
753
Simon Glassae7123f2015-01-05 20:05:27 -0700754 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700755 if (ret)
756 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700757 return dm_gpio_set_value(&desc, value);
Simon Glass96495d92014-02-26 15:59:24 -0700758}
759
Heiko Schocher54c5d082014-05-22 12:43:05 +0200760const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glass96495d92014-02-26 15:59:24 -0700761{
762 struct gpio_dev_priv *priv;
763
764 /* Must be called on an active device */
Simon Glasse564f052015-03-05 12:25:20 -0700765 priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -0700766 assert(priv);
767
768 *bit_count = priv->gpio_count;
769 return priv->bank_name;
770}
771
Simon Glass6449a502014-10-04 11:29:43 -0600772static const char * const gpio_function[GPIOF_COUNT] = {
773 "input",
774 "output",
775 "unused",
776 "unknown",
777 "func",
778};
779
Masahiro Yamadafb07f972017-06-22 16:50:25 +0900780static int get_function(struct udevice *dev, int offset, bool skip_unused,
781 const char **namep)
Simon Glass6449a502014-10-04 11:29:43 -0600782{
Simon Glasse564f052015-03-05 12:25:20 -0700783 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass6449a502014-10-04 11:29:43 -0600784 struct dm_gpio_ops *ops = gpio_get_ops(dev);
785
786 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
787 if (!device_active(dev))
788 return -ENODEV;
789 if (offset < 0 || offset >= uc_priv->gpio_count)
790 return -EINVAL;
791 if (namep)
792 *namep = uc_priv->name[offset];
793 if (skip_unused && !uc_priv->name[offset])
794 return GPIOF_UNUSED;
795 if (ops->get_function) {
796 int ret;
797
798 ret = ops->get_function(dev, offset);
799 if (ret < 0)
800 return ret;
801 if (ret >= ARRAY_SIZE(gpio_function))
802 return -ENODATA;
803 return ret;
804 }
805
806 return GPIOF_UNKNOWN;
807}
808
809int gpio_get_function(struct udevice *dev, int offset, const char **namep)
810{
811 return get_function(dev, offset, true, namep);
812}
813
814int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
815{
816 return get_function(dev, offset, false, namep);
817}
818
Simon Glass07575352014-10-04 11:29:44 -0600819int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
820{
821 struct dm_gpio_ops *ops = gpio_get_ops(dev);
822 struct gpio_dev_priv *priv;
823 char *str = buf;
824 int func;
825 int ret;
826 int len;
827
828 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
829
830 *buf = 0;
Simon Glasse564f052015-03-05 12:25:20 -0700831 priv = dev_get_uclass_priv(dev);
Simon Glass07575352014-10-04 11:29:44 -0600832 ret = gpio_get_raw_function(dev, offset, NULL);
833 if (ret < 0)
834 return ret;
835 func = ret;
836 len = snprintf(str, buffsize, "%s%d: %s",
837 priv->bank_name ? priv->bank_name : "",
838 offset, gpio_function[func]);
839 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
840 func == GPIOF_UNUSED) {
841 const char *label;
842 bool used;
843
844 ret = ops->get_value(dev, offset);
845 if (ret < 0)
846 return ret;
847 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
848 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
849 ret,
850 used ? 'x' : ' ',
851 used ? " " : "",
852 label ? label : "");
853 }
854
855 return 0;
856}
857
Simon Glass962f5ca2015-04-14 21:03:20 -0600858int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
859{
860 int i, ret;
861 int gpio;
862
863 for (i = 0; i < 32; i++) {
864 gpio = gpio_num_array[i];
865 if (gpio == -1)
866 break;
867 ret = gpio_requestf(gpio, fmt, i);
868 if (ret)
869 goto err;
870 ret = gpio_direction_input(gpio);
871 if (ret) {
872 gpio_free(gpio);
873 goto err;
874 }
875 }
876
877 return 0;
878err:
879 for (i--; i >= 0; i--)
880 gpio_free(gpio_num_array[i]);
881
882 return ret;
883}
884
Simon Glasse5901c92014-11-10 18:00:21 -0700885/*
886 * get a number comprised of multiple GPIO values. gpio_num_array points to
887 * the array of gpio pin numbers to scan, terminated by -1.
888 */
Simon Glass962f5ca2015-04-14 21:03:20 -0600889int gpio_get_values_as_int(const int *gpio_list)
Simon Glasse5901c92014-11-10 18:00:21 -0700890{
891 int gpio;
892 unsigned bitmask = 1;
893 unsigned vector = 0;
Simon Glass962f5ca2015-04-14 21:03:20 -0600894 int ret;
Simon Glasse5901c92014-11-10 18:00:21 -0700895
896 while (bitmask &&
Simon Glass962f5ca2015-04-14 21:03:20 -0600897 ((gpio = *gpio_list++) != -1)) {
898 ret = gpio_get_value(gpio);
899 if (ret < 0)
900 return ret;
901 else if (ret)
Simon Glasse5901c92014-11-10 18:00:21 -0700902 vector |= bitmask;
903 bitmask <<= 1;
904 }
Simon Glass962f5ca2015-04-14 21:03:20 -0600905
Simon Glasse5901c92014-11-10 18:00:21 -0700906 return vector;
907}
908
Simon Glass17c43f12016-03-06 19:27:51 -0700909int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassbbf24782016-03-06 19:27:50 -0700910{
911 unsigned bitmask = 1;
912 unsigned vector = 0;
913 int ret, i;
914
915 for (i = 0; i < count; i++) {
916 ret = dm_gpio_get_value(&desc_list[i]);
917 if (ret < 0)
918 return ret;
919 else if (ret)
920 vector |= bitmask;
921 bitmask <<= 1;
922 }
923
924 return vector;
925}
926
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200927/**
928 * gpio_request_tail: common work for requesting a gpio.
929 *
930 * ret: return value from previous work in function which calls
931 * this function.
932 * This seems bogus (why calling this function instead not
933 * calling it and end caller function instead?).
934 * Because on error in caller function we want to set some
935 * default values in gpio desc and have a common error
936 * debug message, which provides this function.
937 * nodename: Name of node for which gpio gets requested
938 * used for gpio label name.
939 * args: pointer to output arguments structure
940 * list_name: Name of GPIO list
941 * used for gpio label name.
942 * index: gpio index in gpio list
943 * used for gpio label name.
944 * desc: pointer to gpio descriptor, filled from this
945 * function.
946 * flags: gpio flags to use.
947 * add_index: should index added to gpio label name
948 * gpio_dev: pointer to gpio device from which the gpio
949 * will be requested. If NULL try to get the
950 * gpio device with uclass_get_device_by_ofnode()
951 *
952 * return: In error case this function sets default values in
953 * gpio descriptor, also emmits a debug message.
954 * On success it returns 0 else the error code from
955 * function calls, or the error code passed through
956 * ret to this function.
957 *
958 */
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200959static int gpio_request_tail(int ret, const char *nodename,
Simon Glass3a571232017-05-18 20:09:18 -0600960 struct ofnode_phandle_args *args,
961 const char *list_name, int index,
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200962 struct gpio_desc *desc, int flags,
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200963 bool add_index, struct udevice *gpio_dev)
Simon Glass3669e0e2015-01-05 20:05:29 -0700964{
Patrick Delaunay9f2b0662020-01-13 11:35:01 +0100965 gpio_desc_init(desc, gpio_dev, 0);
Simon Glass3a571232017-05-18 20:09:18 -0600966 if (ret)
Simon Glass3669e0e2015-01-05 20:05:29 -0700967 goto err;
Simon Glass3669e0e2015-01-05 20:05:29 -0700968
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200969 if (!desc->dev) {
970 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
971 &desc->dev);
972 if (ret) {
Heiko Schocher49b10cb2019-07-17 06:59:51 +0200973 debug("%s: uclass_get_device_by_ofnode failed\n",
974 __func__);
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200975 goto err;
976 }
Simon Glass3669e0e2015-01-05 20:05:29 -0700977 }
Simon Glass3a571232017-05-18 20:09:18 -0600978 ret = gpio_find_and_xlate(desc, args);
Simon Glass3669e0e2015-01-05 20:05:29 -0700979 if (ret) {
980 debug("%s: gpio_find_and_xlate failed\n", __func__);
981 goto err;
982 }
983 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200984 nodename, list_name, index);
Simon Glass3669e0e2015-01-05 20:05:29 -0700985 if (ret) {
986 debug("%s: dm_gpio_requestf failed\n", __func__);
987 goto err;
988 }
Patrick Delaunay695e5fd2020-01-13 11:35:06 +0100989 ret = dm_gpio_set_dir_flags(desc, flags);
Simon Glass3669e0e2015-01-05 20:05:29 -0700990 if (ret) {
991 debug("%s: dm_gpio_set_dir failed\n", __func__);
992 goto err;
993 }
994
995 return 0;
996err:
997 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Heiko Schocher5fc7cf82019-06-12 06:11:46 +0200998 __func__, nodename, list_name, index, ret);
Simon Glass3669e0e2015-01-05 20:05:29 -0700999 return ret;
1000}
1001
Simon Glass150c5af2017-05-30 21:47:09 -06001002static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1003 int index, struct gpio_desc *desc,
1004 int flags, bool add_index)
Simon Glass3a571232017-05-18 20:09:18 -06001005{
1006 struct ofnode_phandle_args args;
1007 int ret;
1008
Simon Glass150c5af2017-05-30 21:47:09 -06001009 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1010 index, &args);
Simon Glass3a571232017-05-18 20:09:18 -06001011
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001012 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1013 index, desc, flags, add_index, NULL);
Simon Glass3a571232017-05-18 20:09:18 -06001014}
1015
Simon Glass150c5af2017-05-30 21:47:09 -06001016int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass3669e0e2015-01-05 20:05:29 -07001017 struct gpio_desc *desc, int flags)
1018{
Simon Glass150c5af2017-05-30 21:47:09 -06001019 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1020 index > 0);
Simon Glass3669e0e2015-01-05 20:05:29 -07001021}
1022
Simon Glass150c5af2017-05-30 21:47:09 -06001023int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass3669e0e2015-01-05 20:05:29 -07001024 struct gpio_desc *desc, int flags)
1025{
Simon Glass150c5af2017-05-30 21:47:09 -06001026 struct ofnode_phandle_args args;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001027 ofnode node;
Simon Glass150c5af2017-05-30 21:47:09 -06001028 int ret;
1029
1030 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1031 index, &args);
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001032 node = dev_ofnode(dev);
1033 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1034 index, desc, flags, index > 0, NULL);
Simon Glass3669e0e2015-01-05 20:05:29 -07001035}
1036
Simon Glass150c5af2017-05-30 21:47:09 -06001037int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass3669e0e2015-01-05 20:05:29 -07001038 struct gpio_desc *desc, int max_count,
1039 int flags)
1040{
1041 int count;
1042 int ret;
1043
Przemyslaw Marczak2984e7a2015-03-31 18:57:16 +02001044 for (count = 0; count < max_count; count++) {
Simon Glass150c5af2017-05-30 21:47:09 -06001045 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass3669e0e2015-01-05 20:05:29 -07001046 &desc[count], flags, true);
1047 if (ret == -ENOENT)
1048 break;
1049 else if (ret)
1050 goto err;
1051 }
1052
1053 /* We ran out of GPIOs in the list */
1054 return count;
1055
1056err:
1057 gpio_free_list_nodev(desc, count - 1);
1058
1059 return ret;
1060}
1061
1062int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1063 struct gpio_desc *desc, int max_count,
1064 int flags)
1065{
1066 /*
1067 * This isn't ideal since we don't use dev->name in the debug()
1068 * calls in gpio_request_by_name(), but we can do this until
1069 * gpio_request_list_by_name_nodev() can be dropped.
1070 */
Simon Glass150c5af2017-05-30 21:47:09 -06001071 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1072 max_count, flags);
Simon Glass3669e0e2015-01-05 20:05:29 -07001073}
1074
1075int gpio_get_list_count(struct udevice *dev, const char *list_name)
1076{
1077 int ret;
1078
Simon Glasse160f7d2017-01-17 16:52:55 -07001079 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
Simon Glass3669e0e2015-01-05 20:05:29 -07001080 list_name, "#gpio-cells", 0, -1,
1081 NULL);
1082 if (ret) {
1083 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1084 __func__, dev->name, list_name, ret);
1085 }
1086
1087 return ret;
1088}
1089
1090int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1091{
1092 /* For now, we don't do any checking of dev */
1093 return _dm_gpio_free(desc->dev, desc->offset);
1094}
1095
1096int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1097{
1098 int i;
1099
1100 /* For now, we don't do any checking of dev */
1101 for (i = 0; i < count; i++)
1102 dm_gpio_free(dev, &desc[i]);
1103
1104 return 0;
1105}
1106
1107int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1108{
1109 return gpio_free_list(NULL, desc, count);
1110}
1111
Simon Glass96495d92014-02-26 15:59:24 -07001112/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glassb892d122014-10-04 11:29:42 -06001113static int gpio_renumber(struct udevice *removed_dev)
Simon Glass96495d92014-02-26 15:59:24 -07001114{
1115 struct gpio_dev_priv *uc_priv;
Heiko Schocher54c5d082014-05-22 12:43:05 +02001116 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -07001117 struct uclass *uc;
1118 unsigned base;
1119 int ret;
1120
1121 ret = uclass_get(UCLASS_GPIO, &uc);
1122 if (ret)
1123 return ret;
1124
1125 /* Ensure that we have a base for each bank */
1126 base = 0;
1127 uclass_foreach_dev(dev, uc) {
Simon Glassb892d122014-10-04 11:29:42 -06001128 if (device_active(dev) && dev != removed_dev) {
Simon Glasse564f052015-03-05 12:25:20 -07001129 uc_priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -07001130 uc_priv->gpio_base = base;
1131 base += uc_priv->gpio_count;
1132 }
1133 }
1134
1135 return 0;
1136}
1137
Simon Glass17c43f12016-03-06 19:27:51 -07001138int gpio_get_number(const struct gpio_desc *desc)
Simon Glass56a71f82015-03-25 12:21:58 -06001139{
1140 struct udevice *dev = desc->dev;
1141 struct gpio_dev_priv *uc_priv;
1142
1143 if (!dev)
1144 return -1;
1145 uc_priv = dev->uclass_priv;
1146
1147 return uc_priv->gpio_base + desc->offset;
1148}
1149
Heiko Schocher54c5d082014-05-22 12:43:05 +02001150static int gpio_post_probe(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -07001151{
Simon Glasse564f052015-03-05 12:25:20 -07001152 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -06001153
1154 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1155 if (!uc_priv->name)
1156 return -ENOMEM;
1157
1158 return gpio_renumber(NULL);
Simon Glass96495d92014-02-26 15:59:24 -07001159}
1160
Heiko Schocher54c5d082014-05-22 12:43:05 +02001161static int gpio_pre_remove(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -07001162{
Simon Glasse564f052015-03-05 12:25:20 -07001163 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -06001164 int i;
1165
1166 for (i = 0; i < uc_priv->gpio_count; i++) {
1167 if (uc_priv->name[i])
1168 free(uc_priv->name[i]);
1169 }
1170 free(uc_priv->name);
1171
1172 return gpio_renumber(dev);
Simon Glass96495d92014-02-26 15:59:24 -07001173}
1174
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001175int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1176 char *list_name, int index, int flags,
1177 int dtflags, struct gpio_desc *desc)
1178{
1179 struct ofnode_phandle_args args;
1180
1181 args.node = ofnode_null();
1182 args.args_count = 2;
1183 args.args[0] = index;
1184 args.args[1] = dtflags;
1185
1186 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1187 flags, 0, dev);
1188}
1189
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001190static int gpio_post_bind(struct udevice *dev)
1191{
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001192 struct udevice *child;
1193 ofnode node;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001194
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001195#if defined(CONFIG_NEEDS_MANUAL_RELOC)
1196 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1197 static int reloc_done;
1198
1199 if (!reloc_done) {
1200 if (ops->request)
1201 ops->request += gd->reloc_off;
Simon Glass093152f2020-02-04 20:15:17 -07001202 if (ops->rfree)
1203 ops->rfree += gd->reloc_off;
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001204 if (ops->direction_input)
1205 ops->direction_input += gd->reloc_off;
1206 if (ops->direction_output)
1207 ops->direction_output += gd->reloc_off;
1208 if (ops->get_value)
1209 ops->get_value += gd->reloc_off;
1210 if (ops->set_value)
1211 ops->set_value += gd->reloc_off;
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001212 if (ops->get_function)
1213 ops->get_function += gd->reloc_off;
1214 if (ops->xlate)
1215 ops->xlate += gd->reloc_off;
Patrick Delaunay8fd9daf2020-01-13 11:35:09 +01001216 if (ops->set_dir_flags)
1217 ops->set_dir_flags += gd->reloc_off;
Patrick Delaunayd2c07e52020-01-13 11:35:08 +01001218 if (ops->get_dir_flags)
1219 ops->get_dir_flags += gd->reloc_off;
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001220
1221 reloc_done++;
1222 }
1223#endif
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001224
Heiko Schocher49b10cb2019-07-17 06:59:51 +02001225 if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1226 dev_for_each_subnode(node, dev) {
1227 if (ofnode_read_bool(node, "gpio-hog")) {
1228 const char *name = ofnode_get_name(node);
1229 int ret;
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001230
Heiko Schocher49b10cb2019-07-17 06:59:51 +02001231 ret = device_bind_driver_to_node(dev,
1232 "gpio_hog",
1233 name, node,
1234 &child);
1235 if (ret)
1236 return ret;
1237 }
Heiko Schocher5fc7cf82019-06-12 06:11:46 +02001238 }
1239 }
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001240 return 0;
1241}
1242
Simon Glass96495d92014-02-26 15:59:24 -07001243UCLASS_DRIVER(gpio) = {
1244 .id = UCLASS_GPIO,
1245 .name = "gpio",
Bhuvanchandra DVae89bb02015-06-01 18:37:15 +05301246 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass96495d92014-02-26 15:59:24 -07001247 .post_probe = gpio_post_probe,
Michal Simek1b4c2aa2018-07-12 12:42:27 +02001248 .post_bind = gpio_post_bind,
Simon Glass96495d92014-02-26 15:59:24 -07001249 .pre_remove = gpio_pre_remove,
1250 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
1251};