blob: 308d0863ada30a1ca1dc83004e0bcd003b6eac75 [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/**
22 * gpio_to_device() - Convert global GPIO number to device, number
Simon Glasse821d182014-02-26 15:59:24 -070023 *
24 * Convert the GPIO number to an entry in the list of GPIOs
25 * or GPIO blocks registered with the GPIO controller. Returns
26 * entry on success, NULL on error.
Simon Glassce555292015-01-05 20:05:27 -070027 *
28 * @gpio: The numeric representation of the GPIO
29 * @desc: Returns description (desc->flags will always be 0)
30 * @return 0 if found, -ENOENT if not found
Simon Glasse821d182014-02-26 15:59:24 -070031 */
Simon Glassce555292015-01-05 20:05:27 -070032static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -070033{
34 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +020035 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -070036 int ret;
37
38 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
39 dev;
40 ret = uclass_next_device(&dev)) {
Simon Glassde0977b2015-03-05 12:25:20 -070041 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -070042 if (gpio >= uc_priv->gpio_base &&
43 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
Simon Glassce555292015-01-05 20:05:27 -070044 desc->dev = dev;
45 desc->offset = gpio - uc_priv->gpio_base;
46 desc->flags = 0;
Simon Glasse821d182014-02-26 15:59:24 -070047 return 0;
48 }
49 }
50
51 /* No such GPIO */
Simon Glassce555292015-01-05 20:05:27 -070052 return ret ? ret : -ENOENT;
Simon Glasse821d182014-02-26 15:59:24 -070053}
54
Simon Glass215bcc72015-06-23 15:38:40 -060055int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
Simon Glasse821d182014-02-26 15:59:24 -070056{
Simon Glass43b2e1a2014-10-22 21:37:01 -060057 struct gpio_dev_priv *uc_priv = NULL;
Heiko Schocherb74fcb42014-05-22 12:43:05 +020058 struct udevice *dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -060059 ulong offset;
60 int numeric;
Simon Glasse821d182014-02-26 15:59:24 -070061 int ret;
62
Simon Glass43b2e1a2014-10-22 21:37:01 -060063 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
Simon Glasse821d182014-02-26 15:59:24 -070064 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
65 dev;
66 ret = uclass_next_device(&dev)) {
Simon Glasse821d182014-02-26 15:59:24 -070067 int len;
68
Simon Glassde0977b2015-03-05 12:25:20 -070069 uc_priv = dev_get_uclass_priv(dev);
Simon Glass43b2e1a2014-10-22 21:37:01 -060070 if (numeric != -1) {
71 offset = numeric - uc_priv->gpio_base;
72 /* Allow GPIOs to be numbered from 0 */
Tom Rini26fd9682017-05-10 15:20:15 -040073 if (offset < uc_priv->gpio_count)
Simon Glass43b2e1a2014-10-22 21:37:01 -060074 break;
75 }
76
Simon Glasse821d182014-02-26 15:59:24 -070077 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
78
Simon Glassd4acf632014-06-11 23:29:47 -060079 if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glass43b2e1a2014-10-22 21:37:01 -060080 if (!strict_strtoul(name + len, 10, &offset))
81 break;
Simon Glasse821d182014-02-26 15:59:24 -070082 }
83 }
84
Simon Glass43b2e1a2014-10-22 21:37:01 -060085 if (!dev)
86 return ret ? ret : -EINVAL;
87
Simon Glass215bcc72015-06-23 15:38:40 -060088 desc->dev = dev;
89 desc->offset = offset;
90
91 return 0;
92}
93
94int gpio_lookup_name(const char *name, struct udevice **devp,
95 unsigned int *offsetp, unsigned int *gpiop)
96{
97 struct gpio_desc desc;
98 int ret;
99
100 if (devp)
101 *devp = NULL;
102 ret = dm_gpio_lookup_name(name, &desc);
103 if (ret)
104 return ret;
105
Simon Glass43b2e1a2014-10-22 21:37:01 -0600106 if (devp)
Simon Glass215bcc72015-06-23 15:38:40 -0600107 *devp = desc.dev;
Simon Glass43b2e1a2014-10-22 21:37:01 -0600108 if (offsetp)
Simon Glass215bcc72015-06-23 15:38:40 -0600109 *offsetp = desc.offset;
110 if (gpiop) {
111 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
112
113 *gpiop = uc_priv->gpio_base + desc.offset;
114 }
Simon Glass43b2e1a2014-10-22 21:37:01 -0600115
116 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700117}
118
Simon Glass12faa022017-05-18 20:09:18 -0600119int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
120 struct ofnode_phandle_args *args)
Eric Nelson786e98d2016-04-24 16:32:40 -0700121{
122 if (args->args_count < 1)
123 return -EINVAL;
124
125 desc->offset = args->args[0];
126
127 if (args->args_count < 2)
128 return 0;
129
130 if (args->args[1] & GPIO_ACTIVE_LOW)
131 desc->flags = GPIOD_ACTIVE_LOW;
132
133 return 0;
134}
135
Simon Glass16e10402015-01-05 20:05:29 -0700136static int gpio_find_and_xlate(struct gpio_desc *desc,
Simon Glass12faa022017-05-18 20:09:18 -0600137 struct ofnode_phandle_args *args)
Simon Glassd3322bb2015-01-05 20:05:28 -0700138{
139 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
140
Eric Nelson786e98d2016-04-24 16:32:40 -0700141 if (ops->xlate)
142 return ops->xlate(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700143 else
Eric Nelson786e98d2016-04-24 16:32:40 -0700144 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glassd3322bb2015-01-05 20:05:28 -0700145}
146
Heiko Schocher39cb3402019-06-12 06:11:46 +0200147#if defined(CONFIG_DM_GPIO_HOG)
148
149struct gpio_hog_priv {
150 struct gpio_desc gpiod;
151};
152
153struct gpio_hog_data {
154 int gpiod_flags;
155 int value;
156 u32 val[2];
157};
158
159static int gpio_hog_ofdata_to_platdata(struct udevice *dev)
160{
161 struct gpio_hog_data *plat = dev_get_platdata(dev);
162 const char *nodename;
163 int ret;
164
165 plat->value = 0;
166 if (dev_read_bool(dev, "input")) {
167 plat->gpiod_flags = GPIOD_IS_IN;
168 } else if (dev_read_bool(dev, "output-high")) {
169 plat->value = 1;
170 plat->gpiod_flags = GPIOD_IS_OUT;
171 } else if (dev_read_bool(dev, "output-low")) {
172 plat->gpiod_flags = GPIOD_IS_OUT;
173 } else {
174 printf("%s: missing gpio-hog state.\n", __func__);
175 return -EINVAL;
176 }
177 ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
178 if (ret) {
179 printf("%s: wrong gpios property, 2 values needed %d\n",
180 __func__, ret);
181 return ret;
182 }
183 nodename = dev_read_string(dev, "line-name");
184 if (!nodename)
185 nodename = dev_read_name(dev);
186 device_set_name(dev, nodename);
187
188 return 0;
189}
190
191static int gpio_hog_probe(struct udevice *dev)
192{
193 struct gpio_hog_data *plat = dev_get_platdata(dev);
194 struct gpio_hog_priv *priv = dev_get_priv(dev);
195 int ret;
196
197 ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
198 plat->val[0], plat->gpiod_flags,
199 plat->val[1], &priv->gpiod);
200 if (ret < 0) {
201 debug("%s: node %s could not get gpio.\n", __func__,
202 dev->name);
203 return ret;
204 }
205 dm_gpio_set_dir(&priv->gpiod);
206 if (plat->gpiod_flags == GPIOD_IS_OUT)
207 dm_gpio_set_value(&priv->gpiod, plat->value);
208
209 return 0;
210}
211
212int gpio_hog_probe_all(void)
213{
214 struct udevice *dev;
215 int ret;
216
217 for (uclass_first_device(UCLASS_NOP, &dev);
218 dev;
219 uclass_find_next_device(&dev)) {
220 if (dev->driver == DM_GET_DRIVER(gpio_hog)) {
221 ret = device_probe(dev);
222 if (ret)
223 return ret;
224 }
225 }
226
227 return 0;
228}
229
230struct gpio_desc *gpio_hog_lookup_name(const char *name)
231{
232 struct udevice *dev;
233
234 gpio_hog_probe_all();
235 if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
236 struct gpio_hog_priv *priv = dev_get_priv(dev);
237
238 return &priv->gpiod;
239 }
240
241 return NULL;
242}
243
244U_BOOT_DRIVER(gpio_hog) = {
245 .name = "gpio_hog",
246 .id = UCLASS_NOP,
247 .ofdata_to_platdata = gpio_hog_ofdata_to_platdata,
248 .probe = gpio_hog_probe,
249 .priv_auto_alloc_size = sizeof(struct gpio_hog_priv),
250 .platdata_auto_alloc_size = sizeof(struct gpio_hog_data),
251};
252#else
253struct gpio_desc *gpio_hog_lookup_name(const char *name)
254{
255 return NULL;
256}
257#endif
258
Simon Glass047cdb32015-06-23 15:38:41 -0600259int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassce555292015-01-05 20:05:27 -0700260{
261 struct udevice *dev = desc->dev;
262 struct gpio_dev_priv *uc_priv;
263 char *str;
264 int ret;
265
Simon Glassde0977b2015-03-05 12:25:20 -0700266 uc_priv = dev_get_uclass_priv(dev);
Simon Glassce555292015-01-05 20:05:27 -0700267 if (uc_priv->name[desc->offset])
268 return -EBUSY;
269 str = strdup(label);
270 if (!str)
271 return -ENOMEM;
272 if (gpio_get_ops(dev)->request) {
273 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
274 if (ret) {
275 free(str);
276 return ret;
277 }
278 }
279 uc_priv->name[desc->offset] = str;
280
281 return 0;
282}
283
Simon Glass16e10402015-01-05 20:05:29 -0700284static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
285{
Simon Glassee8a3d92015-12-29 05:22:48 -0700286#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
Simon Glass16e10402015-01-05 20:05:29 -0700287 va_list args;
288 char buf[40];
289
290 va_start(args, fmt);
291 vscnprintf(buf, sizeof(buf), fmt, args);
292 va_end(args);
293 return dm_gpio_request(desc, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700294#else
295 return dm_gpio_request(desc, fmt);
296#endif
Simon Glass16e10402015-01-05 20:05:29 -0700297}
298
Simon Glasse821d182014-02-26 15:59:24 -0700299/**
300 * gpio_request() - [COMPAT] Request GPIO
301 * gpio: GPIO number
302 * label: Name for the requested GPIO
303 *
Simon Glass0f4517d2014-10-04 11:29:42 -0600304 * The label is copied and allocated so the caller does not need to keep
305 * the pointer around.
306 *
Simon Glasse821d182014-02-26 15:59:24 -0700307 * This function implements the API that's compatible with current
308 * GPIO API used in U-Boot. The request is forwarded to particular
309 * GPIO driver. Returns 0 on success, negative value on error.
310 */
311int gpio_request(unsigned gpio, const char *label)
312{
Simon Glassce555292015-01-05 20:05:27 -0700313 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700314 int ret;
315
Simon Glassce555292015-01-05 20:05:27 -0700316 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700317 if (ret)
318 return ret;
319
Simon Glassce555292015-01-05 20:05:27 -0700320 return dm_gpio_request(&desc, label);
Simon Glasse821d182014-02-26 15:59:24 -0700321}
322
323/**
Simon Glass1b27d602014-10-04 11:29:49 -0600324 * gpio_requestf() - [COMPAT] Request GPIO
325 * @gpio: GPIO number
326 * @fmt: Format string for the requested GPIO
327 * @...: Arguments for the printf() format string
328 *
329 * This function implements the API that's compatible with current
330 * GPIO API used in U-Boot. The request is forwarded to particular
331 * GPIO driver. Returns 0 on success, negative value on error.
332 */
333int gpio_requestf(unsigned gpio, const char *fmt, ...)
334{
Simon Glassee8a3d92015-12-29 05:22:48 -0700335#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
Simon Glass1b27d602014-10-04 11:29:49 -0600336 va_list args;
337 char buf[40];
338
339 va_start(args, fmt);
340 vscnprintf(buf, sizeof(buf), fmt, args);
341 va_end(args);
342 return gpio_request(gpio, buf);
Simon Glassee8a3d92015-12-29 05:22:48 -0700343#else
344 return gpio_request(gpio, fmt);
345#endif
Simon Glass1b27d602014-10-04 11:29:49 -0600346}
347
Simon Glassce555292015-01-05 20:05:27 -0700348int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glasse821d182014-02-26 15:59:24 -0700349{
Simon Glass0f4517d2014-10-04 11:29:42 -0600350 struct gpio_dev_priv *uc_priv;
Simon Glasse821d182014-02-26 15:59:24 -0700351 int ret;
352
Simon Glassde0977b2015-03-05 12:25:20 -0700353 uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600354 if (!uc_priv->name[offset])
355 return -ENXIO;
356 if (gpio_get_ops(dev)->free) {
357 ret = gpio_get_ops(dev)->free(dev, offset);
358 if (ret)
359 return ret;
360 }
361
362 free(uc_priv->name[offset]);
363 uc_priv->name[offset] = NULL;
364
365 return 0;
366}
367
Simon Glassce555292015-01-05 20:05:27 -0700368/**
369 * gpio_free() - [COMPAT] Relinquish GPIO
370 * gpio: GPIO number
371 *
372 * This function implements the API that's compatible with current
373 * GPIO API used in U-Boot. The request is forwarded to particular
374 * GPIO driver. Returns 0 on success, negative value on error.
375 */
376int gpio_free(unsigned gpio)
377{
378 struct gpio_desc desc;
379 int ret;
380
381 ret = gpio_to_device(gpio, &desc);
382 if (ret)
383 return ret;
384
385 return _dm_gpio_free(desc.dev, desc.offset);
386}
387
Simon Glassfd838972016-03-06 19:27:51 -0700388static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glass0f4517d2014-10-04 11:29:42 -0600389{
Simon Glass230c1432015-07-02 18:16:16 -0600390 struct gpio_dev_priv *uc_priv;
391
392 if (!dm_gpio_is_valid(desc))
393 return -ENOENT;
Simon Glass0f4517d2014-10-04 11:29:42 -0600394
Simon Glass230c1432015-07-02 18:16:16 -0600395 uc_priv = dev_get_uclass_priv(desc->dev);
Simon Glassce555292015-01-05 20:05:27 -0700396 if (!uc_priv->name[desc->offset]) {
Simon Glass0f4517d2014-10-04 11:29:42 -0600397 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassce555292015-01-05 20:05:27 -0700398 desc->dev->name, func,
399 uc_priv->bank_name ? uc_priv->bank_name : "",
400 desc->offset);
Simon Glass0f4517d2014-10-04 11:29:42 -0600401 return -EBUSY;
402 }
403
404 return 0;
Simon Glasse821d182014-02-26 15:59:24 -0700405}
406
407/**
408 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
409 * gpio: GPIO number
410 *
411 * This function implements the API that's compatible with current
412 * GPIO API used in U-Boot. The request is forwarded to particular
413 * GPIO driver. Returns 0 on success, negative value on error.
414 */
415int gpio_direction_input(unsigned gpio)
416{
Simon Glassce555292015-01-05 20:05:27 -0700417 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700418 int ret;
419
Simon Glassce555292015-01-05 20:05:27 -0700420 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700421 if (ret)
422 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700423 ret = check_reserved(&desc, "dir_input");
424 if (ret)
425 return ret;
Simon Glasse821d182014-02-26 15:59:24 -0700426
Simon Glassce555292015-01-05 20:05:27 -0700427 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
Simon Glasse821d182014-02-26 15:59:24 -0700428}
429
430/**
431 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
432 * gpio: GPIO number
433 * value: Logical value to be set on the GPIO pin
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_output(unsigned gpio, int value)
440{
Simon Glassce555292015-01-05 20:05:27 -0700441 struct gpio_desc desc;
442 int ret;
443
444 ret = gpio_to_device(gpio, &desc);
445 if (ret)
446 return ret;
447 ret = check_reserved(&desc, "dir_output");
448 if (ret)
449 return ret;
450
451 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
452 desc.offset, value);
453}
454
Simon Glassfd838972016-03-06 19:27:51 -0700455int dm_gpio_get_value(const struct gpio_desc *desc)
Simon Glassce555292015-01-05 20:05:27 -0700456{
457 int value;
458 int ret;
459
460 ret = check_reserved(desc, "get_value");
461 if (ret)
462 return ret;
463
464 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
465
466 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
467}
468
Simon Glassfd838972016-03-06 19:27:51 -0700469int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassce555292015-01-05 20:05:27 -0700470{
471 int ret;
472
473 ret = check_reserved(desc, "set_value");
474 if (ret)
475 return ret;
476
477 if (desc->flags & GPIOD_ACTIVE_LOW)
478 value = !value;
479 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
480 return 0;
481}
482
mario.six@gdsys.cc0a44d492016-05-25 15:15:21 +0200483int dm_gpio_get_open_drain(struct gpio_desc *desc)
484{
485 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
486 int ret;
487
488 ret = check_reserved(desc, "get_open_drain");
489 if (ret)
490 return ret;
491
492 if (ops->set_open_drain)
493 return ops->get_open_drain(desc->dev, desc->offset);
494 else
495 return -ENOSYS;
496}
497
498int dm_gpio_set_open_drain(struct gpio_desc *desc, int value)
499{
500 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
501 int ret;
502
503 ret = check_reserved(desc, "set_open_drain");
504 if (ret)
505 return ret;
506
507 if (ops->set_open_drain)
508 ret = ops->set_open_drain(desc->dev, desc->offset, value);
509 else
510 return 0; /* feature not supported -> ignore setting */
511
512 return ret;
513}
514
Simon Glassce555292015-01-05 20:05:27 -0700515int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
516{
517 struct udevice *dev = desc->dev;
518 struct dm_gpio_ops *ops = gpio_get_ops(dev);
Simon Glasse821d182014-02-26 15:59:24 -0700519 int ret;
520
Simon Glassce555292015-01-05 20:05:27 -0700521 ret = check_reserved(desc, "set_dir");
Simon Glasse821d182014-02-26 15:59:24 -0700522 if (ret)
523 return ret;
524
Simon Glassce555292015-01-05 20:05:27 -0700525 if (flags & GPIOD_IS_OUT) {
526 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
527
528 if (flags & GPIOD_ACTIVE_LOW)
529 value = !value;
530 ret = ops->direction_output(dev, desc->offset, value);
531 } else if (flags & GPIOD_IS_IN) {
532 ret = ops->direction_input(dev, desc->offset);
533 }
534 if (ret)
535 return ret;
536 /*
537 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
538 * futures
539 */
540 desc->flags = flags;
541
542 return 0;
543}
544
545int dm_gpio_set_dir(struct gpio_desc *desc)
546{
547 return dm_gpio_set_dir_flags(desc, desc->flags);
Simon Glasse821d182014-02-26 15:59:24 -0700548}
549
550/**
551 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
552 * gpio: GPIO number
553 *
554 * This function implements the API that's compatible with current
555 * GPIO API used in U-Boot. The request is forwarded to particular
556 * GPIO driver. Returns the value of the GPIO pin, or negative value
557 * on error.
558 */
559int gpio_get_value(unsigned gpio)
560{
Simon Glasse821d182014-02-26 15:59:24 -0700561 int ret;
562
Simon Glassce555292015-01-05 20:05:27 -0700563 struct gpio_desc desc;
564
565 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700566 if (ret)
567 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700568 return dm_gpio_get_value(&desc);
Simon Glasse821d182014-02-26 15:59:24 -0700569}
570
571/**
572 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
573 * gpio: GPIO number
574 * value: Logical value to be set on the GPIO pin.
575 *
576 * This function implements the API that's compatible with current
577 * GPIO API used in U-Boot. The request is forwarded to particular
578 * GPIO driver. Returns 0 on success, negative value on error.
579 */
580int gpio_set_value(unsigned gpio, int value)
581{
Simon Glassce555292015-01-05 20:05:27 -0700582 struct gpio_desc desc;
Simon Glasse821d182014-02-26 15:59:24 -0700583 int ret;
584
Simon Glassce555292015-01-05 20:05:27 -0700585 ret = gpio_to_device(gpio, &desc);
Simon Glasse821d182014-02-26 15:59:24 -0700586 if (ret)
587 return ret;
Simon Glassce555292015-01-05 20:05:27 -0700588 return dm_gpio_set_value(&desc, value);
Simon Glasse821d182014-02-26 15:59:24 -0700589}
590
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200591const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glasse821d182014-02-26 15:59:24 -0700592{
593 struct gpio_dev_priv *priv;
594
595 /* Must be called on an active device */
Simon Glassde0977b2015-03-05 12:25:20 -0700596 priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -0700597 assert(priv);
598
599 *bit_count = priv->gpio_count;
600 return priv->bank_name;
601}
602
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600603static const char * const gpio_function[GPIOF_COUNT] = {
604 "input",
605 "output",
606 "unused",
607 "unknown",
608 "func",
609};
610
Masahiro Yamada286c2522017-06-22 16:50:25 +0900611static int get_function(struct udevice *dev, int offset, bool skip_unused,
612 const char **namep)
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600613{
Simon Glassde0977b2015-03-05 12:25:20 -0700614 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass6e5e6dd2014-10-04 11:29:43 -0600615 struct dm_gpio_ops *ops = gpio_get_ops(dev);
616
617 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
618 if (!device_active(dev))
619 return -ENODEV;
620 if (offset < 0 || offset >= uc_priv->gpio_count)
621 return -EINVAL;
622 if (namep)
623 *namep = uc_priv->name[offset];
624 if (skip_unused && !uc_priv->name[offset])
625 return GPIOF_UNUSED;
626 if (ops->get_function) {
627 int ret;
628
629 ret = ops->get_function(dev, offset);
630 if (ret < 0)
631 return ret;
632 if (ret >= ARRAY_SIZE(gpio_function))
633 return -ENODATA;
634 return ret;
635 }
636
637 return GPIOF_UNKNOWN;
638}
639
640int gpio_get_function(struct udevice *dev, int offset, const char **namep)
641{
642 return get_function(dev, offset, true, namep);
643}
644
645int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
646{
647 return get_function(dev, offset, false, namep);
648}
649
Simon Glass6b1ef592014-10-04 11:29:44 -0600650int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
651{
652 struct dm_gpio_ops *ops = gpio_get_ops(dev);
653 struct gpio_dev_priv *priv;
654 char *str = buf;
655 int func;
656 int ret;
657 int len;
658
659 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
660
661 *buf = 0;
Simon Glassde0977b2015-03-05 12:25:20 -0700662 priv = dev_get_uclass_priv(dev);
Simon Glass6b1ef592014-10-04 11:29:44 -0600663 ret = gpio_get_raw_function(dev, offset, NULL);
664 if (ret < 0)
665 return ret;
666 func = ret;
667 len = snprintf(str, buffsize, "%s%d: %s",
668 priv->bank_name ? priv->bank_name : "",
669 offset, gpio_function[func]);
670 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
671 func == GPIOF_UNUSED) {
672 const char *label;
673 bool used;
674
675 ret = ops->get_value(dev, offset);
676 if (ret < 0)
677 return ret;
678 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
679 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
680 ret,
681 used ? 'x' : ' ',
682 used ? " " : "",
683 label ? label : "");
684 }
685
686 return 0;
687}
688
Simon Glassbef54db2015-04-14 21:03:20 -0600689int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
690{
691 int i, ret;
692 int gpio;
693
694 for (i = 0; i < 32; i++) {
695 gpio = gpio_num_array[i];
696 if (gpio == -1)
697 break;
698 ret = gpio_requestf(gpio, fmt, i);
699 if (ret)
700 goto err;
701 ret = gpio_direction_input(gpio);
702 if (ret) {
703 gpio_free(gpio);
704 goto err;
705 }
706 }
707
708 return 0;
709err:
710 for (i--; i >= 0; i--)
711 gpio_free(gpio_num_array[i]);
712
713 return ret;
714}
715
Simon Glass2c97a8f2014-11-10 18:00:21 -0700716/*
717 * get a number comprised of multiple GPIO values. gpio_num_array points to
718 * the array of gpio pin numbers to scan, terminated by -1.
719 */
Simon Glassbef54db2015-04-14 21:03:20 -0600720int gpio_get_values_as_int(const int *gpio_list)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700721{
722 int gpio;
723 unsigned bitmask = 1;
724 unsigned vector = 0;
Simon Glassbef54db2015-04-14 21:03:20 -0600725 int ret;
Simon Glass2c97a8f2014-11-10 18:00:21 -0700726
727 while (bitmask &&
Simon Glassbef54db2015-04-14 21:03:20 -0600728 ((gpio = *gpio_list++) != -1)) {
729 ret = gpio_get_value(gpio);
730 if (ret < 0)
731 return ret;
732 else if (ret)
Simon Glass2c97a8f2014-11-10 18:00:21 -0700733 vector |= bitmask;
734 bitmask <<= 1;
735 }
Simon Glassbef54db2015-04-14 21:03:20 -0600736
Simon Glass2c97a8f2014-11-10 18:00:21 -0700737 return vector;
738}
739
Simon Glassfd838972016-03-06 19:27:51 -0700740int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassdf1687d2016-03-06 19:27:50 -0700741{
742 unsigned bitmask = 1;
743 unsigned vector = 0;
744 int ret, i;
745
746 for (i = 0; i < count; i++) {
747 ret = dm_gpio_get_value(&desc_list[i]);
748 if (ret < 0)
749 return ret;
750 else if (ret)
751 vector |= bitmask;
752 bitmask <<= 1;
753 }
754
755 return vector;
756}
757
Heiko Schocher39cb3402019-06-12 06:11:46 +0200758static int gpio_request_tail(int ret, const char *nodename,
Simon Glass12faa022017-05-18 20:09:18 -0600759 struct ofnode_phandle_args *args,
760 const char *list_name, int index,
Heiko Schocher39cb3402019-06-12 06:11:46 +0200761 struct gpio_desc *desc, int flags,
762 bool add_index, struct udevice *dev)
Simon Glass16e10402015-01-05 20:05:29 -0700763{
Heiko Schocher39cb3402019-06-12 06:11:46 +0200764 desc->dev = dev;
Simon Glass16e10402015-01-05 20:05:29 -0700765 desc->offset = 0;
Eric Nelson786e98d2016-04-24 16:32:40 -0700766 desc->flags = 0;
Simon Glass12faa022017-05-18 20:09:18 -0600767 if (ret)
Simon Glass16e10402015-01-05 20:05:29 -0700768 goto err;
Simon Glass16e10402015-01-05 20:05:29 -0700769
Heiko Schocher39cb3402019-06-12 06:11:46 +0200770 if (!desc->dev) {
771 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
772 &desc->dev);
773 if (ret) {
774 debug("%s: uclass_get_device_by_ofnode failed\n", __func__);
775 goto err;
776 }
Simon Glass16e10402015-01-05 20:05:29 -0700777 }
Simon Glass12faa022017-05-18 20:09:18 -0600778 ret = gpio_find_and_xlate(desc, args);
Simon Glass16e10402015-01-05 20:05:29 -0700779 if (ret) {
780 debug("%s: gpio_find_and_xlate failed\n", __func__);
781 goto err;
782 }
783 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Heiko Schocher39cb3402019-06-12 06:11:46 +0200784 nodename, list_name, index);
Simon Glass16e10402015-01-05 20:05:29 -0700785 if (ret) {
786 debug("%s: dm_gpio_requestf failed\n", __func__);
787 goto err;
788 }
789 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
790 if (ret) {
791 debug("%s: dm_gpio_set_dir failed\n", __func__);
792 goto err;
793 }
794
795 return 0;
796err:
797 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Heiko Schocher39cb3402019-06-12 06:11:46 +0200798 __func__, nodename, list_name, index, ret);
Simon Glass16e10402015-01-05 20:05:29 -0700799 return ret;
800}
801
Simon Glass1d9af1f2017-05-30 21:47:09 -0600802static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
803 int index, struct gpio_desc *desc,
804 int flags, bool add_index)
Simon Glass12faa022017-05-18 20:09:18 -0600805{
806 struct ofnode_phandle_args args;
807 int ret;
808
Simon Glass1d9af1f2017-05-30 21:47:09 -0600809 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
810 index, &args);
Simon Glass12faa022017-05-18 20:09:18 -0600811
Heiko Schocher39cb3402019-06-12 06:11:46 +0200812 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
813 index, desc, flags, add_index, NULL);
Simon Glass12faa022017-05-18 20:09:18 -0600814}
815
Simon Glass1d9af1f2017-05-30 21:47:09 -0600816int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -0700817 struct gpio_desc *desc, int flags)
818{
Simon Glass1d9af1f2017-05-30 21:47:09 -0600819 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
820 index > 0);
Simon Glass16e10402015-01-05 20:05:29 -0700821}
822
Simon Glass1d9af1f2017-05-30 21:47:09 -0600823int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass16e10402015-01-05 20:05:29 -0700824 struct gpio_desc *desc, int flags)
825{
Simon Glass1d9af1f2017-05-30 21:47:09 -0600826 struct ofnode_phandle_args args;
Heiko Schocher39cb3402019-06-12 06:11:46 +0200827 ofnode node;
Simon Glass1d9af1f2017-05-30 21:47:09 -0600828 int ret;
829
830 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
831 index, &args);
Heiko Schocher39cb3402019-06-12 06:11:46 +0200832 node = dev_ofnode(dev);
833 return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
834 index, desc, flags, index > 0, NULL);
Simon Glass16e10402015-01-05 20:05:29 -0700835}
836
Simon Glass1d9af1f2017-05-30 21:47:09 -0600837int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass16e10402015-01-05 20:05:29 -0700838 struct gpio_desc *desc, int max_count,
839 int flags)
840{
841 int count;
842 int ret;
843
Przemyslaw Marczak44fc5362015-03-31 18:57:16 +0200844 for (count = 0; count < max_count; count++) {
Simon Glass1d9af1f2017-05-30 21:47:09 -0600845 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass16e10402015-01-05 20:05:29 -0700846 &desc[count], flags, true);
847 if (ret == -ENOENT)
848 break;
849 else if (ret)
850 goto err;
851 }
852
853 /* We ran out of GPIOs in the list */
854 return count;
855
856err:
857 gpio_free_list_nodev(desc, count - 1);
858
859 return ret;
860}
861
862int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
863 struct gpio_desc *desc, int max_count,
864 int flags)
865{
866 /*
867 * This isn't ideal since we don't use dev->name in the debug()
868 * calls in gpio_request_by_name(), but we can do this until
869 * gpio_request_list_by_name_nodev() can be dropped.
870 */
Simon Glass1d9af1f2017-05-30 21:47:09 -0600871 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
872 max_count, flags);
Simon Glass16e10402015-01-05 20:05:29 -0700873}
874
875int gpio_get_list_count(struct udevice *dev, const char *list_name)
876{
877 int ret;
878
Simon Glassdd79d6e2017-01-17 16:52:55 -0700879 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
Simon Glass16e10402015-01-05 20:05:29 -0700880 list_name, "#gpio-cells", 0, -1,
881 NULL);
882 if (ret) {
883 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
884 __func__, dev->name, list_name, ret);
885 }
886
887 return ret;
888}
889
890int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
891{
892 /* For now, we don't do any checking of dev */
893 return _dm_gpio_free(desc->dev, desc->offset);
894}
895
896int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
897{
898 int i;
899
900 /* For now, we don't do any checking of dev */
901 for (i = 0; i < count; i++)
902 dm_gpio_free(dev, &desc[i]);
903
904 return 0;
905}
906
907int gpio_free_list_nodev(struct gpio_desc *desc, int count)
908{
909 return gpio_free_list(NULL, desc, count);
910}
911
Simon Glasse821d182014-02-26 15:59:24 -0700912/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glass0f4517d2014-10-04 11:29:42 -0600913static int gpio_renumber(struct udevice *removed_dev)
Simon Glasse821d182014-02-26 15:59:24 -0700914{
915 struct gpio_dev_priv *uc_priv;
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200916 struct udevice *dev;
Simon Glasse821d182014-02-26 15:59:24 -0700917 struct uclass *uc;
918 unsigned base;
919 int ret;
920
921 ret = uclass_get(UCLASS_GPIO, &uc);
922 if (ret)
923 return ret;
924
925 /* Ensure that we have a base for each bank */
926 base = 0;
927 uclass_foreach_dev(dev, uc) {
Simon Glass0f4517d2014-10-04 11:29:42 -0600928 if (device_active(dev) && dev != removed_dev) {
Simon Glassde0977b2015-03-05 12:25:20 -0700929 uc_priv = dev_get_uclass_priv(dev);
Simon Glasse821d182014-02-26 15:59:24 -0700930 uc_priv->gpio_base = base;
931 base += uc_priv->gpio_count;
932 }
933 }
934
935 return 0;
936}
937
Simon Glassfd838972016-03-06 19:27:51 -0700938int gpio_get_number(const struct gpio_desc *desc)
Simon Glass94f54d12015-03-25 12:21:58 -0600939{
940 struct udevice *dev = desc->dev;
941 struct gpio_dev_priv *uc_priv;
942
943 if (!dev)
944 return -1;
945 uc_priv = dev->uclass_priv;
946
947 return uc_priv->gpio_base + desc->offset;
948}
949
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200950static int gpio_post_probe(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -0700951{
Simon Glassde0977b2015-03-05 12:25:20 -0700952 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600953
954 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
955 if (!uc_priv->name)
956 return -ENOMEM;
957
958 return gpio_renumber(NULL);
Simon Glasse821d182014-02-26 15:59:24 -0700959}
960
Heiko Schocherb74fcb42014-05-22 12:43:05 +0200961static int gpio_pre_remove(struct udevice *dev)
Simon Glasse821d182014-02-26 15:59:24 -0700962{
Simon Glassde0977b2015-03-05 12:25:20 -0700963 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass0f4517d2014-10-04 11:29:42 -0600964 int i;
965
966 for (i = 0; i < uc_priv->gpio_count; i++) {
967 if (uc_priv->name[i])
968 free(uc_priv->name[i]);
969 }
970 free(uc_priv->name);
971
972 return gpio_renumber(dev);
Simon Glasse821d182014-02-26 15:59:24 -0700973}
974
Heiko Schocher39cb3402019-06-12 06:11:46 +0200975int gpio_dev_request_index(struct udevice *dev, const char *nodename,
976 char *list_name, int index, int flags,
977 int dtflags, struct gpio_desc *desc)
978{
979 struct ofnode_phandle_args args;
980
981 args.node = ofnode_null();
982 args.args_count = 2;
983 args.args[0] = index;
984 args.args[1] = dtflags;
985
986 return gpio_request_tail(0, nodename, &args, list_name, index, desc,
987 flags, 0, dev);
988}
989
Michal Simek5f7202c2018-07-12 12:42:27 +0200990static int gpio_post_bind(struct udevice *dev)
991{
Heiko Schocher39cb3402019-06-12 06:11:46 +0200992#if defined(CONFIG_DM_GPIO_HOG)
993 struct udevice *child;
994 ofnode node;
995#endif
996
Michal Simek5f7202c2018-07-12 12:42:27 +0200997#if defined(CONFIG_NEEDS_MANUAL_RELOC)
998 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
999 static int reloc_done;
1000
1001 if (!reloc_done) {
1002 if (ops->request)
1003 ops->request += gd->reloc_off;
1004 if (ops->free)
1005 ops->free += gd->reloc_off;
1006 if (ops->direction_input)
1007 ops->direction_input += gd->reloc_off;
1008 if (ops->direction_output)
1009 ops->direction_output += gd->reloc_off;
1010 if (ops->get_value)
1011 ops->get_value += gd->reloc_off;
1012 if (ops->set_value)
1013 ops->set_value += gd->reloc_off;
1014 if (ops->get_open_drain)
1015 ops->get_open_drain += gd->reloc_off;
1016 if (ops->set_open_drain)
1017 ops->set_open_drain += gd->reloc_off;
1018 if (ops->get_function)
1019 ops->get_function += gd->reloc_off;
1020 if (ops->xlate)
1021 ops->xlate += gd->reloc_off;
1022
1023 reloc_done++;
1024 }
1025#endif
Heiko Schocher39cb3402019-06-12 06:11:46 +02001026
1027#if defined(CONFIG_DM_GPIO_HOG)
1028 dev_for_each_subnode(node, dev) {
1029 if (ofnode_read_bool(node, "gpio-hog")) {
1030 const char *name = ofnode_get_name(node);
1031
1032 device_bind_driver_to_node(dev, "gpio_hog", name,
1033 node, &child);
1034 }
1035 }
1036#endif
Michal Simek5f7202c2018-07-12 12:42:27 +02001037 return 0;
1038}
1039
Simon Glasse821d182014-02-26 15:59:24 -07001040UCLASS_DRIVER(gpio) = {
1041 .id = UCLASS_GPIO,
1042 .name = "gpio",
Bhuvanchandra DVb1a1fc92015-06-01 18:37:15 +05301043 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glasse821d182014-02-26 15:59:24 -07001044 .post_probe = gpio_post_probe,
Michal Simek5f7202c2018-07-12 12:42:27 +02001045 .post_bind = gpio_post_bind,
Simon Glasse821d182014-02-26 15:59:24 -07001046 .pre_remove = gpio_pre_remove,
1047 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
1048};