blob: 9550e45e6cdd244d92386ab9ab4ec7fac0928bea [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>
Patrick Delaunay187c45f2020-01-13 11:35:04 +01008#include <dm/device_compat.h>
Heiko Schocher39cb3402019-06-12 06:11:46 +02009#include <dm/device-internal.h>
10#include <dm/lists.h>
11#include <dm/uclass-internal.h>
Eric Nelson786e98d2016-04-24 16:32:40 -070012#include <dt-bindings/gpio/gpio.h>
Simon Glasse821d182014-02-26 15:59:24 -070013#include <errno.h>
Simon Glassd3322bb2015-01-05 20:05:28 -070014#include <fdtdec.h>
Simon Glass0f4517d2014-10-04 11:29:42 -060015#include <malloc.h>
Simon Glasse821d182014-02-26 15:59:24 -070016#include <asm/gpio.h>
Masahiro Yamada78eeb912016-01-24 23:27:48 +090017#include <linux/bug.h>
Simon Glass43b2e1a2014-10-22 21:37:01 -060018#include <linux/ctype.h>
Simon Glasse821d182014-02-26 15:59:24 -070019
Simon Glass16e10402015-01-05 20:05:29 -070020DECLARE_GLOBAL_DATA_PTR;
21
Simon Glasse821d182014-02-26 15:59:24 -070022/**
Patrick Delaunay758bba92020-01-13 11:35:01 +010023 * gpio_desc_init() - Initialize the GPIO descriptor
24 *
25 * @desc: GPIO descriptor to initialize
26 * @dev: GPIO device
27 * @offset: Offset of device GPIO
28 */
29static void gpio_desc_init(struct gpio_desc *desc,
30 struct udevice *dev,
31 uint offset)
32{
33 desc->dev = dev;
34 desc->offset = offset;
35 desc->flags = 0;
36}
37
38/**
Simon Glasse821d182014-02-26 15:59:24 -070039 * gpio_to_device() - Convert global GPIO number to device, number
Simon Glasse821d182014-02-26 15:59:24 -070040 *
41 * Convert the GPIO number to an entry in the list of GPIOs
42 * or GPIO blocks registered with the GPIO controller. Returns
43 * entry on success, NULL on error.
Simon Glassce555292015-01-05 20:05:27 -070044 *
45 * @gpio: The numeric representation of the GPIO
46 * @desc: Returns description (desc->flags will always be 0)
47 * @return 0 if found, -ENOENT if not found
Simon Glasse821d182014-02-26 15:59:24 -070048 */
Simon Glassce555292015-01-05 20:05:27 -070049static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -070050{
51 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +020052 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -070053 int ret;
54
55 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
56 dev;
57 ret = uclass_next_device(&dev)) {
Simon Glassde0977b2015-03-05 12:25:20 -070058 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -070059 if (gpio >= uc_priv->gpio_base &&
60 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
Patrick Delaunay758bba92020-01-13 11:35:01 +010061 gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
Simon Glasse821d182014-02-26 15:59:24 -070062 return 0;
63 }
64 }
65
66 /* No such GPIO */
Simon Glassce555292015-01-05 20:05:27 -070067 return ret ? ret : -ENOENT;
Simon Glasse821d182014-02-26 15:59:24 -070068}
69
Simon Glass215bcc72015-06-23 15:38:40 -060070int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -070071{
Simon Glass43b2e1a2014-10-22 21:37:01 -060072 struct gpio_dev_priv *uc_priv = NULL;
Heiko Schocherb74fcb42014-05-22 12:43:05 +020073 struct udevice *dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -060074 ulong offset;
75 int numeric;
Simon Glasse821d182014-02-26 15:59:24 -070076 int ret;
77
Simon Glass43b2e1a2014-10-22 21:37:01 -060078 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
Simon Glasse821d182014-02-26 15:59:24 -070079 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
80 dev;
81 ret = uclass_next_device(&dev)) {
Simon Glasse821d182014-02-26 15:59:24 -070082 int len;
83
Simon Glassde0977b2015-03-05 12:25:20 -070084 uc_priv = dev_get_uclass_priv(dev);
Simon Glass43b2e1a2014-10-22 21:37:01 -060085 if (numeric != -1) {
86 offset = numeric - uc_priv->gpio_base;
87 /* Allow GPIOs to be numbered from 0 */
Tom Rini26fd9682017-05-10 15:20:15 -040088 if (offset < uc_priv->gpio_count)
Simon Glass43b2e1a2014-10-22 21:37:01 -060089 break;
90 }
91
Simon Glasse821d182014-02-26 15:59:24 -070092 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
93
Simon Glassd4acf632014-06-11 23:29:47 -060094 if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glass43b2e1a2014-10-22 21:37:01 -060095 if (!strict_strtoul(name + len, 10, &offset))
96 break;
Simon Glasse821d182014-02-26 15:59:24 -070097 }
98 }
99
Simon Glass43b2e1a2014-10-22 21:37:01 -0600100 if (!dev)
101 return ret ? ret : -EINVAL;
102
Patrick Delaunay758bba92020-01-13 11:35:01 +0100103 gpio_desc_init(desc, dev, offset);
Simon Glass215bcc72015-06-23 15:38:40 -0600104
105 return 0;
106}
107
108int gpio_lookup_name(const char *name, struct udevice **devp,
109 unsigned int *offsetp, unsigned int *gpiop)
110{
111 struct gpio_desc desc;
112 int ret;
113
114 if (devp)
115 *devp = NULL;
116 ret = dm_gpio_lookup_name(name, &desc);
117 if (ret)
118 return ret;
119
Simon Glass43b2e1a2014-10-22 21:37:01 -0600120 if (devp)
Simon Glass215bcc72015-06-23 15:38:40 -0600121 *devp = desc.dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -0600122 if (offsetp)
Simon Glass215bcc72015-06-23 15:38:40 -0600123 *offsetp = desc.offset;
124 if (gpiop) {
125 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
126
127 *gpiop = uc_priv->gpio_base + desc.offset;
128 }
Simon Glass43b2e1a2014-10-22 21:37:01 -0600129
130 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700131}
132
Simon Glass12faa022017-05-18 20:09:18 -0600133int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
134 struct ofnode_phandle_args *args)
Eric Nelson786e98d2016-04-24 16:32:40 -0700135{
136 if (args->args_count < 1)
137 return -EINVAL;
138
139 desc->offset = args->args[0];
140
141 if (args->args_count < 2)
142 return 0;
143
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100144 desc->flags = 0;
Eric Nelson786e98d2016-04-24 16:32:40 -0700145 if (args->args[1] & GPIO_ACTIVE_LOW)
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100146 desc->flags |= GPIOD_ACTIVE_LOW;
Eric Nelson786e98d2016-04-24 16:32:40 -0700147
148 return 0;
149}
150
Simon Glass16e10402015-01-05 20:05:29 -0700151static int gpio_find_and_xlate(struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600152 struct ofnode_phandle_args *args)
Simon Glassd3322bb2015-01-05 20:05:28 -0700153{
154 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
155
Eric Nelson786e98d2016-04-24 16:32:40 -0700156 if (ops->xlate)
157 return ops->xlate(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700158 else
Eric Nelson786e98d2016-04-24 16:32:40 -0700159 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700160}
161
Heiko Schocher58e4c382019-07-17 06:59:51 +0200162#if defined(CONFIG_GPIO_HOG)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200163
164struct gpio_hog_priv {
165 struct gpio_desc gpiod;
166};
167
168struct gpio_hog_data {
169 int gpiod_flags;
170 int value;
171 u32 val[2];
172};
173
174static int gpio_hog_ofdata_to_platdata(struct udevice *dev)
175{
176 struct gpio_hog_data *plat = dev_get_platdata(dev);
177 const char *nodename;
178 int ret;
179
180 plat->value = 0;
181 if (dev_read_bool(dev, "input")) {
182 plat->gpiod_flags = GPIOD_IS_IN;
183 } else if (dev_read_bool(dev, "output-high")) {
184 plat->value = 1;
185 plat->gpiod_flags = GPIOD_IS_OUT;
186 } else if (dev_read_bool(dev, "output-low")) {
187 plat->gpiod_flags = GPIOD_IS_OUT;
188 } else {
189 printf("%s: missing gpio-hog state.\n", __func__);
190 return -EINVAL;
191 }
192 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
193 if (ret) {
194 printf("%s: wrong gpios property, 2 values needed %d\n",
195 __func__, ret);
196 return ret;
197 }
198 nodename = dev_read_string(dev, "line-name");
Heiko Schocher58e4c382019-07-17 06:59:51 +0200199 if (nodename)
200 device_set_name(dev, nodename);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200201
202 return 0;
203}
204
205static int gpio_hog_probe(struct udevice *dev)
206{
207 struct gpio_hog_data *plat = dev_get_platdata(dev);
208 struct gpio_hog_priv *priv = dev_get_priv(dev);
209 int ret;
210
211 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
212 plat->val[0], plat->gpiod_flags,
213 plat->val[1], &priv->gpiod);
214 if (ret < 0) {
215 debug("%s: node %s could not get gpio.\n", __func__,
216 dev->name);
217 return ret;
218 }
Heiko Schocher58e4c382019-07-17 06:59:51 +0200219
220 if (plat->gpiod_flags == GPIOD_IS_OUT) {
221 ret = dm_gpio_set_value(&priv->gpiod, plat->value);
222 if (ret < 0) {
223 debug("%s: node %s could not set gpio.\n", __func__,
224 dev->name);
225 return ret;
226 }
227 }
Heiko Schocher39cb3402019-06-12 06:11:46 +0200228
229 return 0;
230}
231
232int gpio_hog_probe_all(void)
233{
234 struct udevice *dev;
235 int ret;
Heiko Schocher58e4c382019-07-17 06:59:51 +0200236 int retval = 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200237
238 for (uclass_first_device(UCLASS_NOP, &dev);
239 dev;
240 uclass_find_next_device(&dev)) {
241 if (dev->driver == DM_GET_DRIVER(gpio_hog)) {
242 ret = device_probe(dev);
Heiko Schocher58e4c382019-07-17 06:59:51 +0200243 if (ret) {
244 printf("Failed to probe device %s err: %d\n",
245 dev->name, ret);
246 retval = ret;
247 }
Heiko Schocher39cb3402019-06-12 06:11:46 +0200248 }
249 }
250
Heiko Schocher58e4c382019-07-17 06:59:51 +0200251 return retval;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200252}
253
Heiko Schocher58e4c382019-07-17 06:59:51 +0200254int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200255{
256 struct udevice *dev;
257
Heiko Schocher58e4c382019-07-17 06:59:51 +0200258 *desc = NULL;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200259 gpio_hog_probe_all();
260 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
261 struct gpio_hog_priv *priv = dev_get_priv(dev);
262
Heiko Schocher58e4c382019-07-17 06:59:51 +0200263 *desc = &priv->gpiod;
264 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200265 }
266
Heiko Schocher58e4c382019-07-17 06:59:51 +0200267 return -ENODEV;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200268}
269
270U_BOOT_DRIVER(gpio_hog) = {
271 .name = "gpio_hog",
272 .id = UCLASS_NOP,
273 .ofdata_to_platdata = gpio_hog_ofdata_to_platdata,
274 .probe = gpio_hog_probe,
275 .priv_auto_alloc_size = sizeof(struct gpio_hog_priv),
276 .platdata_auto_alloc_size = sizeof(struct gpio_hog_data),
277};
278#else
Heiko Schocher58e4c382019-07-17 06:59:51 +0200279int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
Heiko Schocher39cb3402019-06-12 06:11:46 +0200280{
Heiko Schocher58e4c382019-07-17 06:59:51 +0200281 return 0;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200282}
283#endif
284
Simon Glass047cdb32015-06-23 15:38:41 -0600285int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassce555292015-01-05 20:05:27 -0700286{
287 struct udevice *dev = desc->dev;
288 struct gpio_dev_priv *uc_priv;
289 char *str;
290 int ret;
291
Simon Glassde0977b2015-03-05 12:25:20 -0700292 uc_priv = dev_get_uclass_priv(dev);
Simon Glassce555292015-01-05 20:05:27 -0700293 if (uc_priv->name[desc->offset])
294 return -EBUSY;
295 str = strdup(label);
296 if (!str)
297 return -ENOMEM;
298 if (gpio_get_ops(dev)->request) {
299 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
300 if (ret) {
301 free(str);
302 return ret;
303 }
304 }
305 uc_priv->name[desc->offset] = str;
306
307 return 0;
308}
309
Simon Glass16e10402015-01-05 20:05:29 -0700310static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
311{
Simon Glass7611ac62019-09-25 08:56:27 -0600312#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass16e10402015-01-05 20:05:29 -0700313 va_list args;
314 char buf[40];
315
316 va_start(args, fmt);
317 vscnprintf(buf, sizeof(buf), fmt, args);
318 va_end(args);
319 return dm_gpio_request(desc, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700320#else
321 return dm_gpio_request(desc, fmt);
322#endif
Simon Glass16e10402015-01-05 20:05:29 -0700323}
324
Simon Glasse821d182014-02-26 15:59:24 -0700325/**
326 * gpio_request() - [COMPAT] Request GPIO
327 * gpio: GPIO number
328 * label: Name for the requested GPIO
329 *
Simon Glass0f4517d2014-10-04 11:29:42 -0600330 * The label is copied and allocated so the caller does not need to keep
331 * the pointer around.
332 *
Simon Glasse821d182014-02-26 15:59:24 -0700333 * This function implements the API that's compatible with current
334 * GPIO API used in U-Boot. The request is forwarded to particular
335 * GPIO driver. Returns 0 on success, negative value on error.
336 */
337int gpio_request(unsigned gpio, const char *label)
338{
Simon Glassce555292015-01-05 20:05:27 -0700339 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700340 int ret;
341
Simon Glassce555292015-01-05 20:05:27 -0700342 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700343 if (ret)
344 return ret;
345
Simon Glassce555292015-01-05 20:05:27 -0700346 return dm_gpio_request(&desc, label);
Simon Glasse821d182014-02-26 15:59:24 -0700347}
348
349/**
Simon Glass1b27d602014-10-04 11:29:49 -0600350 * gpio_requestf() - [COMPAT] Request GPIO
351 * @gpio: GPIO number
352 * @fmt: Format string for the requested GPIO
353 * @...: Arguments for the printf() format string
354 *
355 * This function implements the API that's compatible with current
356 * GPIO API used in U-Boot. The request is forwarded to particular
357 * GPIO driver. Returns 0 on success, negative value on error.
358 */
359int gpio_requestf(unsigned gpio, const char *fmt, ...)
360{
Simon Glass7611ac62019-09-25 08:56:27 -0600361#if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
Simon Glass1b27d602014-10-04 11:29:49 -0600362 va_list args;
363 char buf[40];
364
365 va_start(args, fmt);
366 vscnprintf(buf, sizeof(buf), fmt, args);
367 va_end(args);
368 return gpio_request(gpio, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700369#else
370 return gpio_request(gpio, fmt);
371#endif
Simon Glass1b27d602014-10-04 11:29:49 -0600372}
373
Simon Glassce555292015-01-05 20:05:27 -0700374int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glasse821d182014-02-26 15:59:24 -0700375{
Simon Glass0f4517d2014-10-04 11:29:42 -0600376 struct gpio_dev_priv *uc_priv;
Simon Glasse821d182014-02-26 15:59:24 -0700377 int ret;
378
Simon Glassde0977b2015-03-05 12:25:20 -0700379 uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600380 if (!uc_priv->name[offset])
381 return -ENXIO;
Simon Glassb3a47542020-02-04 20:15:17 -0700382 if (gpio_get_ops(dev)->rfree) {
383 ret = gpio_get_ops(dev)->rfree(dev, offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600384 if (ret)
385 return ret;
386 }
387
388 free(uc_priv->name[offset]);
389 uc_priv->name[offset] = NULL;
390
391 return 0;
392}
393
Simon Glassce555292015-01-05 20:05:27 -0700394/**
395 * gpio_free() - [COMPAT] Relinquish GPIO
396 * gpio: GPIO number
397 *
398 * 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_free(unsigned gpio)
403{
404 struct gpio_desc desc;
405 int ret;
406
407 ret = gpio_to_device(gpio, &desc);
408 if (ret)
409 return ret;
410
411 return _dm_gpio_free(desc.dev, desc.offset);
412}
413
Simon Glassfd838972016-03-06 19:27:51 -0700414static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glass0f4517d2014-10-04 11:29:42 -0600415{
Simon Glass230c1432015-07-02 18:16:16 -0600416 struct gpio_dev_priv *uc_priv;
417
418 if (!dm_gpio_is_valid(desc))
419 return -ENOENT;
Simon Glass0f4517d2014-10-04 11:29:42 -0600420
Simon Glass230c1432015-07-02 18:16:16 -0600421 uc_priv = dev_get_uclass_priv(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700422 if (!uc_priv->name[desc->offset]) {
Simon Glass0f4517d2014-10-04 11:29:42 -0600423 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassce555292015-01-05 20:05:27 -0700424 desc->dev->name, func,
425 uc_priv->bank_name ? uc_priv->bank_name : "",
426 desc->offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600427 return -EBUSY;
428 }
429
430 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700431}
432
433/**
434 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
435 * gpio: GPIO number
436 *
437 * This function implements the API that's compatible with current
438 * GPIO API used in U-Boot. The request is forwarded to particular
439 * GPIO driver. Returns 0 on success, negative value on error.
440 */
441int gpio_direction_input(unsigned gpio)
442{
Simon Glassce555292015-01-05 20:05:27 -0700443 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700444 int ret;
445
Simon Glassce555292015-01-05 20:05:27 -0700446 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700447 if (ret)
448 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700449 ret = check_reserved(&desc, "dir_input");
450 if (ret)
451 return ret;
Simon Glasse821d182014-02-26 15:59:24 -0700452
Simon Glassce555292015-01-05 20:05:27 -0700453 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
Simon Glasse821d182014-02-26 15:59:24 -0700454}
455
456/**
457 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
458 * gpio: GPIO number
459 * value: Logical value to be set on the GPIO pin
460 *
461 * This function implements the API that's compatible with current
462 * GPIO API used in U-Boot. The request is forwarded to particular
463 * GPIO driver. Returns 0 on success, negative value on error.
464 */
465int gpio_direction_output(unsigned gpio, int value)
466{
Simon Glassce555292015-01-05 20:05:27 -0700467 struct gpio_desc desc;
468 int ret;
469
470 ret = gpio_to_device(gpio, &desc);
471 if (ret)
472 return ret;
473 ret = check_reserved(&desc, "dir_output");
474 if (ret)
475 return ret;
476
477 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
478 desc.offset, value);
479}
480
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100481static int _gpio_get_value(const struct gpio_desc *desc)
Simon Glassce555292015-01-05 20:05:27 -0700482{
483 int value;
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100484
485 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
486
487 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
488}
489
490int dm_gpio_get_value(const struct gpio_desc *desc)
491{
Simon Glassce555292015-01-05 20:05:27 -0700492 int ret;
493
494 ret = check_reserved(desc, "get_value");
495 if (ret)
496 return ret;
497
Patrick Delaunay4c2c96f2020-01-13 11:35:02 +0100498 return _gpio_get_value(desc);
Simon Glassce555292015-01-05 20:05:27 -0700499}
500
Simon Glassfd838972016-03-06 19:27:51 -0700501int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassce555292015-01-05 20:05:27 -0700502{
503 int ret;
504
505 ret = check_reserved(desc, "set_value");
506 if (ret)
507 return ret;
508
509 if (desc->flags & GPIOD_ACTIVE_LOW)
510 value = !value;
511 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
512 return 0;
513}
514
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100515/* check dir flags invalid configuration */
516static int check_dir_flags(ulong flags)
517{
518 if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
519 log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
520 __func__, flags);
521 return -EINVAL;
522 }
523
524 return 0;
525}
526
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100527static int _dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
Simon Glassce555292015-01-05 20:05:27 -0700528{
529 struct udevice *dev = desc->dev;
530 struct dm_gpio_ops *ops = gpio_get_ops(dev);
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100531 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100532 int ret = 0;
Simon Glasse821d182014-02-26 15:59:24 -0700533
Patrick Delaunay187c45f2020-01-13 11:35:04 +0100534 ret = check_dir_flags(flags);
535 if (ret) {
536 dev_dbg(dev,
537 "%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
538 desc->dev->name,
539 uc_priv->bank_name ? uc_priv->bank_name : "",
540 desc->offset, flags);
541
542 return ret;
543 }
544
Simon Glassce555292015-01-05 20:05:27 -0700545 if (flags & GPIOD_IS_OUT) {
Patrick Delaunayd346d4c2020-01-13 11:35:05 +0100546 ret = ops->direction_output(dev, desc->offset,
547 GPIOD_FLAGS_OUTPUT(flags));
548 } else if (flags & GPIOD_IS_IN) {
Simon Glassce555292015-01-05 20:05:27 -0700549 ret = ops->direction_input(dev, desc->offset);
550 }
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100551
552 return ret;
553}
554
555int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
556{
557 int ret;
558
559 ret = check_reserved(desc, "set_dir_flags");
Simon Glassce555292015-01-05 20:05:27 -0700560 if (ret)
561 return ret;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100562
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100563 /* combine the requested flags (for IN/OUT) and the descriptor flags */
564 flags |= desc->flags;
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100565 ret = _dm_gpio_set_dir_flags(desc, flags);
Simon Glassce555292015-01-05 20:05:27 -0700566
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100567 /* update the descriptor flags */
568 if (ret)
569 desc->flags = flags;
570
571 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700572}
573
574int dm_gpio_set_dir(struct gpio_desc *desc)
575{
Patrick Delaunay3b2a1172020-01-13 11:35:03 +0100576 int ret;
577
578 ret = check_reserved(desc, "set_dir");
579 if (ret)
580 return ret;
581
582 return _dm_gpio_set_dir_flags(desc, desc->flags);
Simon Glasse821d182014-02-26 15:59:24 -0700583}
584
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100585int dm_gpio_get_dir_flags(struct gpio_desc *desc, ulong *flags)
586{
587 int ret;
588 ulong dir_flags;
589
590 ret = check_reserved(desc, "get_dir_flags");
591 if (ret)
592 return ret;
593
594 dir_flags = desc->flags;
595 /* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
596 dir_flags &= ~GPIOD_IS_OUT_ACTIVE;
597 if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
598 dir_flags |= GPIOD_IS_OUT_ACTIVE;
599
600 *flags = dir_flags;
601
602 return 0;
603}
604
Simon Glasse821d182014-02-26 15:59:24 -0700605/**
606 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
607 * gpio: GPIO number
608 *
609 * This function implements the API that's compatible with current
610 * GPIO API used in U-Boot. The request is forwarded to particular
611 * GPIO driver. Returns the value of the GPIO pin, or negative value
612 * on error.
613 */
614int gpio_get_value(unsigned gpio)
615{
Simon Glasse821d182014-02-26 15:59:24 -0700616 int ret;
617
Simon Glassce555292015-01-05 20:05:27 -0700618 struct gpio_desc desc;
619
620 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700621 if (ret)
622 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700623 return dm_gpio_get_value(&desc);
Simon Glasse821d182014-02-26 15:59:24 -0700624}
625
626/**
627 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
628 * gpio: GPIO number
629 * value: Logical value to be set on the GPIO pin.
630 *
631 * This function implements the API that's compatible with current
632 * GPIO API used in U-Boot. The request is forwarded to particular
633 * GPIO driver. Returns 0 on success, negative value on error.
634 */
635int gpio_set_value(unsigned gpio, int value)
636{
Simon Glassce555292015-01-05 20:05:27 -0700637 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700638 int ret;
639
Simon Glassce555292015-01-05 20:05:27 -0700640 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700641 if (ret)
642 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700643 return dm_gpio_set_value(&desc, value);
Simon Glasse821d182014-02-26 15:59:24 -0700644}
645
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200646const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glasse821d182014-02-26 15:59:24 -0700647{
648 struct gpio_dev_priv *priv;
649
650 /* Must be called on an active device */
Simon Glassde0977b2015-03-05 12:25:20 -0700651 priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -0700652 assert(priv);
653
654 *bit_count = priv->gpio_count;
655 return priv->bank_name;
656}
657
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600658static const char * const gpio_function[GPIOF_COUNT] = {
659 "input",
660 "output",
661 "unused",
662 "unknown",
663 "func",
664};
665
Masahiro Yamada286c2522017-06-22 16:50:25 +0900666static int get_function(struct udevice *dev, int offset, bool skip_unused,
667 const char **namep)
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600668{
Simon Glassde0977b2015-03-05 12:25:20 -0700669 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600670 struct dm_gpio_ops *ops = gpio_get_ops(dev);
671
672 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
673 if (!device_active(dev))
674 return -ENODEV;
675 if (offset < 0 || offset >= uc_priv->gpio_count)
676 return -EINVAL;
677 if (namep)
678 *namep = uc_priv->name[offset];
679 if (skip_unused && !uc_priv->name[offset])
680 return GPIOF_UNUSED;
681 if (ops->get_function) {
682 int ret;
683
684 ret = ops->get_function(dev, offset);
685 if (ret < 0)
686 return ret;
687 if (ret >= ARRAY_SIZE(gpio_function))
688 return -ENODATA;
689 return ret;
690 }
691
692 return GPIOF_UNKNOWN;
693}
694
695int gpio_get_function(struct udevice *dev, int offset, const char **namep)
696{
697 return get_function(dev, offset, true, namep);
698}
699
700int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
701{
702 return get_function(dev, offset, false, namep);
703}
704
Simon Glass6b1ef592014-10-04 11:29:44 -0600705int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
706{
707 struct dm_gpio_ops *ops = gpio_get_ops(dev);
708 struct gpio_dev_priv *priv;
709 char *str = buf;
710 int func;
711 int ret;
712 int len;
713
714 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
715
716 *buf = 0;
Simon Glassde0977b2015-03-05 12:25:20 -0700717 priv = dev_get_uclass_priv(dev);
Simon Glass6b1ef592014-10-04 11:29:44 -0600718 ret = gpio_get_raw_function(dev, offset, NULL);
719 if (ret < 0)
720 return ret;
721 func = ret;
722 len = snprintf(str, buffsize, "%s%d: %s",
723 priv->bank_name ? priv->bank_name : "",
724 offset, gpio_function[func]);
725 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
726 func == GPIOF_UNUSED) {
727 const char *label;
728 bool used;
729
730 ret = ops->get_value(dev, offset);
731 if (ret < 0)
732 return ret;
733 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
734 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
735 ret,
736 used ? 'x' : ' ',
737 used ? " " : "",
738 label ? label : "");
739 }
740
741 return 0;
742}
743
Simon Glassbef54db2015-04-14 21:03:20 -0600744int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
745{
746 int i, ret;
747 int gpio;
748
749 for (i = 0; i < 32; i++) {
750 gpio = gpio_num_array[i];
751 if (gpio == -1)
752 break;
753 ret = gpio_requestf(gpio, fmt, i);
754 if (ret)
755 goto err;
756 ret = gpio_direction_input(gpio);
757 if (ret) {
758 gpio_free(gpio);
759 goto err;
760 }
761 }
762
763 return 0;
764err:
765 for (i--; i >= 0; i--)
766 gpio_free(gpio_num_array[i]);
767
768 return ret;
769}
770
Simon Glass2c97a8f2014-11-10 18:00:21 -0700771/*
772 * get a number comprised of multiple GPIO values. gpio_num_array points to
773 * the array of gpio pin numbers to scan, terminated by -1.
774 */
Simon Glassbef54db2015-04-14 21:03:20 -0600775int gpio_get_values_as_int(const int *gpio_list)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700776{
777 int gpio;
778 unsigned bitmask = 1;
779 unsigned vector = 0;
Simon Glassbef54db2015-04-14 21:03:20 -0600780 int ret;
Simon Glass2c97a8f2014-11-10 18:00:21 -0700781
782 while (bitmask &&
Simon Glassbef54db2015-04-14 21:03:20 -0600783 ((gpio = *gpio_list++) != -1)) {
784 ret = gpio_get_value(gpio);
785 if (ret < 0)
786 return ret;
787 else if (ret)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700788 vector |= bitmask;
789 bitmask <<= 1;
790 }
Simon Glassbef54db2015-04-14 21:03:20 -0600791
Simon Glass2c97a8f2014-11-10 18:00:21 -0700792 return vector;
793}
794
Simon Glassfd838972016-03-06 19:27:51 -0700795int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassdf1687d2016-03-06 19:27:50 -0700796{
797 unsigned bitmask = 1;
798 unsigned vector = 0;
799 int ret, i;
800
801 for (i = 0; i < count; i++) {
802 ret = dm_gpio_get_value(&desc_list[i]);
803 if (ret < 0)
804 return ret;
805 else if (ret)
806 vector |= bitmask;
807 bitmask <<= 1;
808 }
809
810 return vector;
811}
812
Heiko Schocher58e4c382019-07-17 06:59:51 +0200813/**
814 * gpio_request_tail: common work for requesting a gpio.
815 *
816 * ret: return value from previous work in function which calls
817 * this function.
818 * This seems bogus (why calling this function instead not
819 * calling it and end caller function instead?).
820 * Because on error in caller function we want to set some
821 * default values in gpio desc and have a common error
822 * debug message, which provides this function.
823 * nodename: Name of node for which gpio gets requested
824 * used for gpio label name.
825 * args: pointer to output arguments structure
826 * list_name: Name of GPIO list
827 * used for gpio label name.
828 * index: gpio index in gpio list
829 * used for gpio label name.
830 * desc: pointer to gpio descriptor, filled from this
831 * function.
832 * flags: gpio flags to use.
833 * add_index: should index added to gpio label name
834 * gpio_dev: pointer to gpio device from which the gpio
835 * will be requested. If NULL try to get the
836 * gpio device with uclass_get_device_by_ofnode()
837 *
838 * return: In error case this function sets default values in
839 * gpio descriptor, also emmits a debug message.
840 * On success it returns 0 else the error code from
841 * function calls, or the error code passed through
842 * ret to this function.
843 *
844 */
Heiko Schocher39cb3402019-06-12 06:11:46 +0200845static int gpio_request_tail(int ret, const char *nodename,
Simon Glass12faa022017-05-18 20:09:18 -0600846 struct ofnode_phandle_args *args,
847 const char *list_name, int index,
Heiko Schocher39cb3402019-06-12 06:11:46 +0200848 struct gpio_desc *desc, int flags,
Heiko Schocher58e4c382019-07-17 06:59:51 +0200849 bool add_index, struct udevice *gpio_dev)
Simon Glass16e10402015-01-05 20:05:29 -0700850{
Patrick Delaunay758bba92020-01-13 11:35:01 +0100851 gpio_desc_init(desc, gpio_dev, 0);
Simon Glass12faa022017-05-18 20:09:18 -0600852 if (ret)
Simon Glass16e10402015-01-05 20:05:29 -0700853 goto err;
Simon Glass16e10402015-01-05 20:05:29 -0700854
Heiko Schocher39cb3402019-06-12 06:11:46 +0200855 if (!desc->dev) {
856 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
857 &desc->dev);
858 if (ret) {
Heiko Schocher58e4c382019-07-17 06:59:51 +0200859 debug("%s: uclass_get_device_by_ofnode failed\n",
860 __func__);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200861 goto err;
862 }
Simon Glass16e10402015-01-05 20:05:29 -0700863 }
Simon Glass12faa022017-05-18 20:09:18 -0600864 ret = gpio_find_and_xlate(desc, args);
Simon Glass16e10402015-01-05 20:05:29 -0700865 if (ret) {
866 debug("%s: gpio_find_and_xlate failed\n", __func__);
867 goto err;
868 }
869 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Heiko Schocher39cb3402019-06-12 06:11:46 +0200870 nodename, list_name, index);
Simon Glass16e10402015-01-05 20:05:29 -0700871 if (ret) {
872 debug("%s: dm_gpio_requestf failed\n", __func__);
873 goto err;
874 }
Patrick Delaunayaecf9d72020-01-13 11:35:06 +0100875 ret = dm_gpio_set_dir_flags(desc, flags);
Simon Glass16e10402015-01-05 20:05:29 -0700876 if (ret) {
877 debug("%s: dm_gpio_set_dir failed\n", __func__);
878 goto err;
879 }
880
881 return 0;
882err:
883 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Heiko Schocher39cb3402019-06-12 06:11:46 +0200884 __func__, nodename, list_name, index, ret);
Simon Glass16e10402015-01-05 20:05:29 -0700885 return ret;
886}
887
Simon Glass1d9af1f2017-05-30 21:47:09 -0600888static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
889 int index, struct gpio_desc *desc,
890 int flags, bool add_index)
Simon Glass12faa022017-05-18 20:09:18 -0600891{
892 struct ofnode_phandle_args args;
893 int ret;
894
Simon Glass1d9af1f2017-05-30 21:47:09 -0600895 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
896 index, &args);
Simon Glass12faa022017-05-18 20:09:18 -0600897
Heiko Schocher39cb3402019-06-12 06:11:46 +0200898 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
899 index, desc, flags, add_index, NULL);
Simon Glass12faa022017-05-18 20:09:18 -0600900}
901
Simon Glass1d9af1f2017-05-30 21:47:09 -0600902int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -0700903 struct gpio_desc *desc, int flags)
904{
Simon Glass1d9af1f2017-05-30 21:47:09 -0600905 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
906 index > 0);
Simon Glass16e10402015-01-05 20:05:29 -0700907}
908
Simon Glass1d9af1f2017-05-30 21:47:09 -0600909int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -0700910 struct gpio_desc *desc, int flags)
911{
Simon Glass1d9af1f2017-05-30 21:47:09 -0600912 struct ofnode_phandle_args args;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200913 ofnode node;
Simon Glass1d9af1f2017-05-30 21:47:09 -0600914 int ret;
915
916 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
917 index, &args);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200918 node = dev_ofnode(dev);
919 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
920 index, desc, flags, index > 0, NULL);
Simon Glass16e10402015-01-05 20:05:29 -0700921}
922
Simon Glass1d9af1f2017-05-30 21:47:09 -0600923int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass16e10402015-01-05 20:05:29 -0700924 struct gpio_desc *desc, int max_count,
925 int flags)
926{
927 int count;
928 int ret;
929
Przemyslaw Marczak44fc5362015-03-31 18:57:16 +0200930 for (count = 0; count < max_count; count++) {
Simon Glass1d9af1f2017-05-30 21:47:09 -0600931 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass16e10402015-01-05 20:05:29 -0700932 &desc[count], flags, true);
933 if (ret == -ENOENT)
934 break;
935 else if (ret)
936 goto err;
937 }
938
939 /* We ran out of GPIOs in the list */
940 return count;
941
942err:
943 gpio_free_list_nodev(desc, count - 1);
944
945 return ret;
946}
947
948int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
949 struct gpio_desc *desc, int max_count,
950 int flags)
951{
952 /*
953 * This isn't ideal since we don't use dev->name in the debug()
954 * calls in gpio_request_by_name(), but we can do this until
955 * gpio_request_list_by_name_nodev() can be dropped.
956 */
Simon Glass1d9af1f2017-05-30 21:47:09 -0600957 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
958 max_count, flags);
Simon Glass16e10402015-01-05 20:05:29 -0700959}
960
961int gpio_get_list_count(struct udevice *dev, const char *list_name)
962{
963 int ret;
964
Simon Glassdd79d6e2017-01-17 16:52:55 -0700965 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
Simon Glass16e10402015-01-05 20:05:29 -0700966 list_name, "#gpio-cells", 0, -1,
967 NULL);
968 if (ret) {
969 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
970 __func__, dev->name, list_name, ret);
971 }
972
973 return ret;
974}
975
976int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
977{
978 /* For now, we don't do any checking of dev */
979 return _dm_gpio_free(desc->dev, desc->offset);
980}
981
982int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
983{
984 int i;
985
986 /* For now, we don't do any checking of dev */
987 for (i = 0; i < count; i++)
988 dm_gpio_free(dev, &desc[i]);
989
990 return 0;
991}
992
993int gpio_free_list_nodev(struct gpio_desc *desc, int count)
994{
995 return gpio_free_list(NULL, desc, count);
996}
997
Simon Glasse821d182014-02-26 15:59:24 -0700998/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glass0f4517d2014-10-04 11:29:42 -0600999static int gpio_renumber(struct udevice *removed_dev)
Simon Glasse821d182014-02-26 15:59:24 -07001000{
1001 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001002 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -07001003 struct uclass *uc;
1004 unsigned base;
1005 int ret;
1006
1007 ret = uclass_get(UCLASS_GPIO, &uc);
1008 if (ret)
1009 return ret;
1010
1011 /* Ensure that we have a base for each bank */
1012 base = 0;
1013 uclass_foreach_dev(dev, uc) {
Simon Glass0f4517d2014-10-04 11:29:42 -06001014 if (device_active(dev) && dev != removed_dev) {
Simon Glassde0977b2015-03-05 12:25:20 -07001015 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001016 uc_priv->gpio_base = base;
1017 base += uc_priv->gpio_count;
1018 }
1019 }
1020
1021 return 0;
1022}
1023
Simon Glassfd838972016-03-06 19:27:51 -07001024int gpio_get_number(const struct gpio_desc *desc)
Simon Glass94f54d12015-03-25 12:21:58 -06001025{
1026 struct udevice *dev = desc->dev;
1027 struct gpio_dev_priv *uc_priv;
1028
1029 if (!dev)
1030 return -1;
1031 uc_priv = dev->uclass_priv;
1032
1033 return uc_priv->gpio_base + desc->offset;
1034}
1035
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001036static int gpio_post_probe(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001037{
Simon Glassde0977b2015-03-05 12:25:20 -07001038 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001039
1040 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1041 if (!uc_priv->name)
1042 return -ENOMEM;
1043
1044 return gpio_renumber(NULL);
Simon Glasse821d182014-02-26 15:59:24 -07001045}
1046
Heiko Schocherb74fcb42014-05-22 12:43:05 +02001047static int gpio_pre_remove(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -07001048{
Simon Glassde0977b2015-03-05 12:25:20 -07001049 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -06001050 int i;
1051
1052 for (i = 0; i < uc_priv->gpio_count; i++) {
1053 if (uc_priv->name[i])
1054 free(uc_priv->name[i]);
1055 }
1056 free(uc_priv->name);
1057
1058 return gpio_renumber(dev);
Simon Glasse821d182014-02-26 15:59:24 -07001059}
1060
Heiko Schocher39cb3402019-06-12 06:11:46 +02001061int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1062 char *list_name, int index, int flags,
1063 int dtflags, struct gpio_desc *desc)
1064{
1065 struct ofnode_phandle_args args;
1066
1067 args.node = ofnode_null();
1068 args.args_count = 2;
1069 args.args[0] = index;
1070 args.args[1] = dtflags;
1071
1072 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1073 flags, 0, dev);
1074}
1075
Michal Simek5f7202c2018-07-12 12:42:27 +02001076static int gpio_post_bind(struct udevice *dev)
1077{
Heiko Schocher39cb3402019-06-12 06:11:46 +02001078 struct udevice *child;
1079 ofnode node;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001080
Michal Simek5f7202c2018-07-12 12:42:27 +02001081#if defined(CONFIG_NEEDS_MANUAL_RELOC)
1082 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1083 static int reloc_done;
1084
1085 if (!reloc_done) {
1086 if (ops->request)
1087 ops->request += gd->reloc_off;
Simon Glassb3a47542020-02-04 20:15:17 -07001088 if (ops->rfree)
1089 ops->rfree += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001090 if (ops->direction_input)
1091 ops->direction_input += gd->reloc_off;
1092 if (ops->direction_output)
1093 ops->direction_output += gd->reloc_off;
1094 if (ops->get_value)
1095 ops->get_value += gd->reloc_off;
1096 if (ops->set_value)
1097 ops->set_value += gd->reloc_off;
Michal Simek5f7202c2018-07-12 12:42:27 +02001098 if (ops->get_function)
1099 ops->get_function += gd->reloc_off;
1100 if (ops->xlate)
1101 ops->xlate += gd->reloc_off;
1102
1103 reloc_done++;
1104 }
1105#endif
Heiko Schocher39cb3402019-06-12 06:11:46 +02001106
Heiko Schocher58e4c382019-07-17 06:59:51 +02001107 if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1108 dev_for_each_subnode(node, dev) {
1109 if (ofnode_read_bool(node, "gpio-hog")) {
1110 const char *name = ofnode_get_name(node);
1111 int ret;
Heiko Schocher39cb3402019-06-12 06:11:46 +02001112
Heiko Schocher58e4c382019-07-17 06:59:51 +02001113 ret = device_bind_driver_to_node(dev,
1114 "gpio_hog",
1115 name, node,
1116 &child);
1117 if (ret)
1118 return ret;
1119 }
Heiko Schocher39cb3402019-06-12 06:11:46 +02001120 }
1121 }
Michal Simek5f7202c2018-07-12 12:42:27 +02001122 return 0;
1123}
1124
Simon Glasse821d182014-02-26 15:59:24 -07001125UCLASS_DRIVER(gpio) = {
1126 .id = UCLASS_GPIO,
1127 .name = "gpio",
Bhuvanchandra DVb1a1fc92015-06-01 18:37:15 +05301128 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glasse821d182014-02-26 15:59:24 -07001129 .post_probe = gpio_post_probe,
Michal Simek5f7202c2018-07-12 12:42:27 +02001130 .post_bind = gpio_post_bind,
Simon Glasse821d182014-02-26 15:59:24 -07001131 .pre_remove = gpio_pre_remove,
1132 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
1133};