blob: da5e9ba6e524f584ff472e34167e5a7861f1c8c6 [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>
Eric Nelson6c880b72016-04-24 16:32:40 -07008#include <dt-bindings/gpio/gpio.h>
Simon Glass96495d92014-02-26 15:59:24 -07009#include <errno.h>
Simon Glass0dac4d52015-01-05 20:05:28 -070010#include <fdtdec.h>
Simon Glassb892d122014-10-04 11:29:42 -060011#include <malloc.h>
Simon Glass96495d92014-02-26 15:59:24 -070012#include <asm/gpio.h>
Masahiro Yamada84b8bf62016-01-24 23:27:48 +090013#include <linux/bug.h>
Simon Glassfe1ef502014-10-22 21:37:01 -060014#include <linux/ctype.h>
Simon Glass96495d92014-02-26 15:59:24 -070015
Simon Glass3669e0e2015-01-05 20:05:29 -070016DECLARE_GLOBAL_DATA_PTR;
17
Simon Glass96495d92014-02-26 15:59:24 -070018/**
19 * gpio_to_device() - Convert global GPIO number to device, number
Simon Glass96495d92014-02-26 15:59:24 -070020 *
21 * Convert the GPIO number to an entry in the list of GPIOs
22 * or GPIO blocks registered with the GPIO controller. Returns
23 * entry on success, NULL on error.
Simon Glassae7123f2015-01-05 20:05:27 -070024 *
25 * @gpio: The numeric representation of the GPIO
26 * @desc: Returns description (desc->flags will always be 0)
27 * @return 0 if found, -ENOENT if not found
Simon Glass96495d92014-02-26 15:59:24 -070028 */
Simon Glassae7123f2015-01-05 20:05:27 -070029static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glass96495d92014-02-26 15:59:24 -070030{
31 struct gpio_dev_priv *uc_priv;
Heiko Schocher54c5d082014-05-22 12:43:05 +020032 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -070033 int ret;
34
35 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
36 dev;
37 ret = uclass_next_device(&dev)) {
Simon Glasse564f052015-03-05 12:25:20 -070038 uc_priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -070039 if (gpio >= uc_priv->gpio_base &&
40 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
Simon Glassae7123f2015-01-05 20:05:27 -070041 desc->dev = dev;
42 desc->offset = gpio - uc_priv->gpio_base;
43 desc->flags = 0;
Simon Glass96495d92014-02-26 15:59:24 -070044 return 0;
45 }
46 }
47
48 /* No such GPIO */
Simon Glassae7123f2015-01-05 20:05:27 -070049 return ret ? ret : -ENOENT;
Simon Glass96495d92014-02-26 15:59:24 -070050}
51
Simon Glass32ec1592015-06-23 15:38:40 -060052int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
Simon Glass96495d92014-02-26 15:59:24 -070053{
Simon Glassfe1ef502014-10-22 21:37:01 -060054 struct gpio_dev_priv *uc_priv = NULL;
Heiko Schocher54c5d082014-05-22 12:43:05 +020055 struct udevice *dev;
Simon Glassfe1ef502014-10-22 21:37:01 -060056 ulong offset;
57 int numeric;
Simon Glass96495d92014-02-26 15:59:24 -070058 int ret;
59
Simon Glassfe1ef502014-10-22 21:37:01 -060060 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
Simon Glass96495d92014-02-26 15:59:24 -070061 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
62 dev;
63 ret = uclass_next_device(&dev)) {
Simon Glass96495d92014-02-26 15:59:24 -070064 int len;
65
Simon Glasse564f052015-03-05 12:25:20 -070066 uc_priv = dev_get_uclass_priv(dev);
Simon Glassfe1ef502014-10-22 21:37:01 -060067 if (numeric != -1) {
68 offset = numeric - uc_priv->gpio_base;
69 /* Allow GPIOs to be numbered from 0 */
Tom Rini75897912017-05-10 15:20:15 -040070 if (offset < uc_priv->gpio_count)
Simon Glassfe1ef502014-10-22 21:37:01 -060071 break;
72 }
73
Simon Glass96495d92014-02-26 15:59:24 -070074 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
75
Simon Glass939cda52014-06-11 23:29:47 -060076 if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glassfe1ef502014-10-22 21:37:01 -060077 if (!strict_strtoul(name + len, 10, &offset))
78 break;
Simon Glass96495d92014-02-26 15:59:24 -070079 }
80 }
81
Simon Glassfe1ef502014-10-22 21:37:01 -060082 if (!dev)
83 return ret ? ret : -EINVAL;
84
Simon Glass32ec1592015-06-23 15:38:40 -060085 desc->dev = dev;
86 desc->offset = offset;
87
88 return 0;
89}
90
91int gpio_lookup_name(const char *name, struct udevice **devp,
92 unsigned int *offsetp, unsigned int *gpiop)
93{
94 struct gpio_desc desc;
95 int ret;
96
Simon Glassfe1ef502014-10-22 21:37:01 -060097 if (devp)
Simon Glass32ec1592015-06-23 15:38:40 -060098 *devp = NULL;
99 ret = dm_gpio_lookup_name(name, &desc);
100 if (ret)
101 return ret;
102
103 if (devp)
104 *devp = desc.dev;
Simon Glassfe1ef502014-10-22 21:37:01 -0600105 if (offsetp)
Simon Glass32ec1592015-06-23 15:38:40 -0600106 *offsetp = desc.offset;
107 if (gpiop) {
108 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
109
110 *gpiop = uc_priv->gpio_base + desc.offset;
111 }
Simon Glassfe1ef502014-10-22 21:37:01 -0600112
113 return 0;
Simon Glass96495d92014-02-26 15:59:24 -0700114}
115
Simon Glass3a571232017-05-18 20:09:18 -0600116int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
117 struct ofnode_phandle_args *args)
Eric Nelson6c880b72016-04-24 16:32:40 -0700118{
119 if (args->args_count < 1)
120 return -EINVAL;
121
122 desc->offset = args->args[0];
123
124 if (args->args_count < 2)
125 return 0;
126
127 if (args->args[1] & GPIO_ACTIVE_LOW)
128 desc->flags = GPIOD_ACTIVE_LOW;
129
130 return 0;
131}
132
Simon Glass3669e0e2015-01-05 20:05:29 -0700133static int gpio_find_and_xlate(struct gpio_desc *desc,
Simon Glass3a571232017-05-18 20:09:18 -0600134 struct ofnode_phandle_args *args)
Simon Glass0dac4d52015-01-05 20:05:28 -0700135{
136 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
137
Eric Nelson6c880b72016-04-24 16:32:40 -0700138 if (ops->xlate)
139 return ops->xlate(desc->dev, desc, args);
Simon Glass0dac4d52015-01-05 20:05:28 -0700140 else
Eric Nelson6c880b72016-04-24 16:32:40 -0700141 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glass0dac4d52015-01-05 20:05:28 -0700142}
143
Simon Glassefa677f2015-06-23 15:38:41 -0600144int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassae7123f2015-01-05 20:05:27 -0700145{
146 struct udevice *dev = desc->dev;
147 struct gpio_dev_priv *uc_priv;
148 char *str;
149 int ret;
150
Simon Glasse564f052015-03-05 12:25:20 -0700151 uc_priv = dev_get_uclass_priv(dev);
Simon Glassae7123f2015-01-05 20:05:27 -0700152 if (uc_priv->name[desc->offset])
153 return -EBUSY;
154 str = strdup(label);
155 if (!str)
156 return -ENOMEM;
157 if (gpio_get_ops(dev)->request) {
158 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
159 if (ret) {
160 free(str);
161 return ret;
162 }
163 }
164 uc_priv->name[desc->offset] = str;
165
166 return 0;
167}
168
Simon Glass3669e0e2015-01-05 20:05:29 -0700169static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
170{
Simon Glass4dc52592015-12-29 05:22:48 -0700171#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
Simon Glass3669e0e2015-01-05 20:05:29 -0700172 va_list args;
173 char buf[40];
174
175 va_start(args, fmt);
176 vscnprintf(buf, sizeof(buf), fmt, args);
177 va_end(args);
178 return dm_gpio_request(desc, buf);
Simon Glass4dc52592015-12-29 05:22:48 -0700179#else
180 return dm_gpio_request(desc, fmt);
181#endif
Simon Glass3669e0e2015-01-05 20:05:29 -0700182}
183
Simon Glass96495d92014-02-26 15:59:24 -0700184/**
185 * gpio_request() - [COMPAT] Request GPIO
186 * gpio: GPIO number
187 * label: Name for the requested GPIO
188 *
Simon Glassb892d122014-10-04 11:29:42 -0600189 * The label is copied and allocated so the caller does not need to keep
190 * the pointer around.
191 *
Simon Glass96495d92014-02-26 15:59:24 -0700192 * This function implements the API that's compatible with current
193 * GPIO API used in U-Boot. The request is forwarded to particular
194 * GPIO driver. Returns 0 on success, negative value on error.
195 */
196int gpio_request(unsigned gpio, const char *label)
197{
Simon Glassae7123f2015-01-05 20:05:27 -0700198 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700199 int ret;
200
Simon Glassae7123f2015-01-05 20:05:27 -0700201 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700202 if (ret)
203 return ret;
204
Simon Glassae7123f2015-01-05 20:05:27 -0700205 return dm_gpio_request(&desc, label);
Simon Glass96495d92014-02-26 15:59:24 -0700206}
207
208/**
Simon Glassd44f5972014-10-04 11:29:49 -0600209 * gpio_requestf() - [COMPAT] Request GPIO
210 * @gpio: GPIO number
211 * @fmt: Format string for the requested GPIO
212 * @...: Arguments for the printf() format string
213 *
214 * This function implements the API that's compatible with current
215 * GPIO API used in U-Boot. The request is forwarded to particular
216 * GPIO driver. Returns 0 on success, negative value on error.
217 */
218int gpio_requestf(unsigned gpio, const char *fmt, ...)
219{
Simon Glass4dc52592015-12-29 05:22:48 -0700220#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
Simon Glassd44f5972014-10-04 11:29:49 -0600221 va_list args;
222 char buf[40];
223
224 va_start(args, fmt);
225 vscnprintf(buf, sizeof(buf), fmt, args);
226 va_end(args);
227 return gpio_request(gpio, buf);
Simon Glass4dc52592015-12-29 05:22:48 -0700228#else
229 return gpio_request(gpio, fmt);
230#endif
Simon Glassd44f5972014-10-04 11:29:49 -0600231}
232
Simon Glassae7123f2015-01-05 20:05:27 -0700233int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glass96495d92014-02-26 15:59:24 -0700234{
Simon Glassb892d122014-10-04 11:29:42 -0600235 struct gpio_dev_priv *uc_priv;
Simon Glass96495d92014-02-26 15:59:24 -0700236 int ret;
237
Simon Glasse564f052015-03-05 12:25:20 -0700238 uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -0600239 if (!uc_priv->name[offset])
240 return -ENXIO;
241 if (gpio_get_ops(dev)->free) {
242 ret = gpio_get_ops(dev)->free(dev, offset);
243 if (ret)
244 return ret;
245 }
246
247 free(uc_priv->name[offset]);
248 uc_priv->name[offset] = NULL;
249
250 return 0;
251}
252
Simon Glassae7123f2015-01-05 20:05:27 -0700253/**
254 * gpio_free() - [COMPAT] Relinquish GPIO
255 * gpio: GPIO number
256 *
257 * This function implements the API that's compatible with current
258 * GPIO API used in U-Boot. The request is forwarded to particular
259 * GPIO driver. Returns 0 on success, negative value on error.
260 */
261int gpio_free(unsigned gpio)
Simon Glassb892d122014-10-04 11:29:42 -0600262{
Simon Glassae7123f2015-01-05 20:05:27 -0700263 struct gpio_desc desc;
264 int ret;
Simon Glassb892d122014-10-04 11:29:42 -0600265
Simon Glassae7123f2015-01-05 20:05:27 -0700266 ret = gpio_to_device(gpio, &desc);
267 if (ret)
268 return ret;
269
270 return _dm_gpio_free(desc.dev, desc.offset);
271}
272
Simon Glass17c43f12016-03-06 19:27:51 -0700273static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glassae7123f2015-01-05 20:05:27 -0700274{
Simon Glasseca48662015-07-02 18:16:16 -0600275 struct gpio_dev_priv *uc_priv;
Simon Glassae7123f2015-01-05 20:05:27 -0700276
Simon Glasseca48662015-07-02 18:16:16 -0600277 if (!dm_gpio_is_valid(desc))
278 return -ENOENT;
279
280 uc_priv = dev_get_uclass_priv(desc->dev);
Simon Glassae7123f2015-01-05 20:05:27 -0700281 if (!uc_priv->name[desc->offset]) {
Simon Glassb892d122014-10-04 11:29:42 -0600282 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassae7123f2015-01-05 20:05:27 -0700283 desc->dev->name, func,
284 uc_priv->bank_name ? uc_priv->bank_name : "",
285 desc->offset);
Simon Glassb892d122014-10-04 11:29:42 -0600286 return -EBUSY;
287 }
288
289 return 0;
Simon Glass96495d92014-02-26 15:59:24 -0700290}
291
292/**
293 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
294 * gpio: GPIO number
295 *
296 * This function implements the API that's compatible with current
297 * GPIO API used in U-Boot. The request is forwarded to particular
298 * GPIO driver. Returns 0 on success, negative value on error.
299 */
300int gpio_direction_input(unsigned gpio)
301{
Simon Glassae7123f2015-01-05 20:05:27 -0700302 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700303 int ret;
304
Simon Glassae7123f2015-01-05 20:05:27 -0700305 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700306 if (ret)
307 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700308 ret = check_reserved(&desc, "dir_input");
309 if (ret)
310 return ret;
Simon Glass96495d92014-02-26 15:59:24 -0700311
Simon Glassae7123f2015-01-05 20:05:27 -0700312 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
Simon Glass96495d92014-02-26 15:59:24 -0700313}
314
315/**
316 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
317 * gpio: GPIO number
318 * value: Logical value to be set on the GPIO pin
319 *
320 * This function implements the API that's compatible with current
321 * GPIO API used in U-Boot. The request is forwarded to particular
322 * GPIO driver. Returns 0 on success, negative value on error.
323 */
324int gpio_direction_output(unsigned gpio, int value)
325{
Simon Glassae7123f2015-01-05 20:05:27 -0700326 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700327 int ret;
328
Simon Glassae7123f2015-01-05 20:05:27 -0700329 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700330 if (ret)
331 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700332 ret = check_reserved(&desc, "dir_output");
333 if (ret)
334 return ret;
Simon Glass96495d92014-02-26 15:59:24 -0700335
Simon Glassae7123f2015-01-05 20:05:27 -0700336 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
337 desc.offset, value);
338}
339
Simon Glass17c43f12016-03-06 19:27:51 -0700340int dm_gpio_get_value(const struct gpio_desc *desc)
Simon Glassae7123f2015-01-05 20:05:27 -0700341{
342 int value;
343 int ret;
344
345 ret = check_reserved(desc, "get_value");
346 if (ret)
347 return ret;
348
349 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
350
351 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
352}
353
Simon Glass17c43f12016-03-06 19:27:51 -0700354int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassae7123f2015-01-05 20:05:27 -0700355{
356 int ret;
357
358 ret = check_reserved(desc, "set_value");
359 if (ret)
360 return ret;
361
362 if (desc->flags & GPIOD_ACTIVE_LOW)
363 value = !value;
364 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
365 return 0;
366}
367
mario.six@gdsys.cc53ecdfb2016-05-25 15:15:21 +0200368int dm_gpio_get_open_drain(struct gpio_desc *desc)
369{
370 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
371 int ret;
372
373 ret = check_reserved(desc, "get_open_drain");
374 if (ret)
375 return ret;
376
377 if (ops->set_open_drain)
378 return ops->get_open_drain(desc->dev, desc->offset);
379 else
380 return -ENOSYS;
381}
382
383int dm_gpio_set_open_drain(struct gpio_desc *desc, int value)
384{
385 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
386 int ret;
387
388 ret = check_reserved(desc, "set_open_drain");
389 if (ret)
390 return ret;
391
392 if (ops->set_open_drain)
393 ret = ops->set_open_drain(desc->dev, desc->offset, value);
394 else
395 return 0; /* feature not supported -> ignore setting */
396
397 return ret;
398}
399
Simon Glassae7123f2015-01-05 20:05:27 -0700400int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
401{
402 struct udevice *dev = desc->dev;
403 struct dm_gpio_ops *ops = gpio_get_ops(dev);
404 int ret;
405
406 ret = check_reserved(desc, "set_dir");
407 if (ret)
408 return ret;
409
410 if (flags & GPIOD_IS_OUT) {
411 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
412
413 if (flags & GPIOD_ACTIVE_LOW)
414 value = !value;
415 ret = ops->direction_output(dev, desc->offset, value);
416 } else if (flags & GPIOD_IS_IN) {
417 ret = ops->direction_input(dev, desc->offset);
418 }
419 if (ret)
420 return ret;
421 /*
422 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
423 * futures
424 */
425 desc->flags = flags;
426
427 return 0;
428}
429
430int dm_gpio_set_dir(struct gpio_desc *desc)
431{
432 return dm_gpio_set_dir_flags(desc, desc->flags);
Simon Glass96495d92014-02-26 15:59:24 -0700433}
434
435/**
436 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
437 * gpio: GPIO number
438 *
439 * This function implements the API that's compatible with current
440 * GPIO API used in U-Boot. The request is forwarded to particular
441 * GPIO driver. Returns the value of the GPIO pin, or negative value
442 * on error.
443 */
444int gpio_get_value(unsigned gpio)
445{
Simon Glass96495d92014-02-26 15:59:24 -0700446 int ret;
447
Simon Glassae7123f2015-01-05 20:05:27 -0700448 struct gpio_desc desc;
449
450 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700451 if (ret)
452 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700453 return dm_gpio_get_value(&desc);
Simon Glass96495d92014-02-26 15:59:24 -0700454}
455
456/**
457 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
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_set_value(unsigned gpio, int value)
466{
Simon Glassae7123f2015-01-05 20:05:27 -0700467 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700468 int ret;
469
Simon Glassae7123f2015-01-05 20:05:27 -0700470 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700471 if (ret)
472 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700473 return dm_gpio_set_value(&desc, value);
Simon Glass96495d92014-02-26 15:59:24 -0700474}
475
Heiko Schocher54c5d082014-05-22 12:43:05 +0200476const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glass96495d92014-02-26 15:59:24 -0700477{
478 struct gpio_dev_priv *priv;
479
480 /* Must be called on an active device */
Simon Glasse564f052015-03-05 12:25:20 -0700481 priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -0700482 assert(priv);
483
484 *bit_count = priv->gpio_count;
485 return priv->bank_name;
486}
487
Simon Glass6449a502014-10-04 11:29:43 -0600488static const char * const gpio_function[GPIOF_COUNT] = {
489 "input",
490 "output",
491 "unused",
492 "unknown",
493 "func",
494};
495
Masahiro Yamadafb07f972017-06-22 16:50:25 +0900496static int get_function(struct udevice *dev, int offset, bool skip_unused,
497 const char **namep)
Simon Glass6449a502014-10-04 11:29:43 -0600498{
Simon Glasse564f052015-03-05 12:25:20 -0700499 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass6449a502014-10-04 11:29:43 -0600500 struct dm_gpio_ops *ops = gpio_get_ops(dev);
501
502 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
503 if (!device_active(dev))
504 return -ENODEV;
505 if (offset < 0 || offset >= uc_priv->gpio_count)
506 return -EINVAL;
507 if (namep)
508 *namep = uc_priv->name[offset];
509 if (skip_unused && !uc_priv->name[offset])
510 return GPIOF_UNUSED;
511 if (ops->get_function) {
512 int ret;
513
514 ret = ops->get_function(dev, offset);
515 if (ret < 0)
516 return ret;
517 if (ret >= ARRAY_SIZE(gpio_function))
518 return -ENODATA;
519 return ret;
520 }
521
522 return GPIOF_UNKNOWN;
523}
524
525int gpio_get_function(struct udevice *dev, int offset, const char **namep)
526{
527 return get_function(dev, offset, true, namep);
528}
529
530int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
531{
532 return get_function(dev, offset, false, namep);
533}
534
Simon Glass07575352014-10-04 11:29:44 -0600535int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
536{
537 struct dm_gpio_ops *ops = gpio_get_ops(dev);
538 struct gpio_dev_priv *priv;
539 char *str = buf;
540 int func;
541 int ret;
542 int len;
543
544 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
545
546 *buf = 0;
Simon Glasse564f052015-03-05 12:25:20 -0700547 priv = dev_get_uclass_priv(dev);
Simon Glass07575352014-10-04 11:29:44 -0600548 ret = gpio_get_raw_function(dev, offset, NULL);
549 if (ret < 0)
550 return ret;
551 func = ret;
552 len = snprintf(str, buffsize, "%s%d: %s",
553 priv->bank_name ? priv->bank_name : "",
554 offset, gpio_function[func]);
555 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
556 func == GPIOF_UNUSED) {
557 const char *label;
558 bool used;
559
560 ret = ops->get_value(dev, offset);
561 if (ret < 0)
562 return ret;
563 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
564 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
565 ret,
566 used ? 'x' : ' ',
567 used ? " " : "",
568 label ? label : "");
569 }
570
571 return 0;
572}
573
Simon Glass962f5ca2015-04-14 21:03:20 -0600574int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
575{
576 int i, ret;
577 int gpio;
578
579 for (i = 0; i < 32; i++) {
580 gpio = gpio_num_array[i];
581 if (gpio == -1)
582 break;
583 ret = gpio_requestf(gpio, fmt, i);
584 if (ret)
585 goto err;
586 ret = gpio_direction_input(gpio);
587 if (ret) {
588 gpio_free(gpio);
589 goto err;
590 }
591 }
592
593 return 0;
594err:
595 for (i--; i >= 0; i--)
596 gpio_free(gpio_num_array[i]);
597
598 return ret;
599}
600
Simon Glasse5901c92014-11-10 18:00:21 -0700601/*
602 * get a number comprised of multiple GPIO values. gpio_num_array points to
603 * the array of gpio pin numbers to scan, terminated by -1.
604 */
Simon Glass962f5ca2015-04-14 21:03:20 -0600605int gpio_get_values_as_int(const int *gpio_list)
Simon Glasse5901c92014-11-10 18:00:21 -0700606{
607 int gpio;
608 unsigned bitmask = 1;
609 unsigned vector = 0;
Simon Glass962f5ca2015-04-14 21:03:20 -0600610 int ret;
Simon Glasse5901c92014-11-10 18:00:21 -0700611
612 while (bitmask &&
Simon Glass962f5ca2015-04-14 21:03:20 -0600613 ((gpio = *gpio_list++) != -1)) {
614 ret = gpio_get_value(gpio);
615 if (ret < 0)
616 return ret;
617 else if (ret)
Simon Glasse5901c92014-11-10 18:00:21 -0700618 vector |= bitmask;
619 bitmask <<= 1;
620 }
Simon Glass962f5ca2015-04-14 21:03:20 -0600621
Simon Glasse5901c92014-11-10 18:00:21 -0700622 return vector;
623}
624
Simon Glass17c43f12016-03-06 19:27:51 -0700625int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassbbf24782016-03-06 19:27:50 -0700626{
627 unsigned bitmask = 1;
628 unsigned vector = 0;
629 int ret, i;
630
631 for (i = 0; i < count; i++) {
632 ret = dm_gpio_get_value(&desc_list[i]);
633 if (ret < 0)
634 return ret;
635 else if (ret)
636 vector |= bitmask;
637 bitmask <<= 1;
638 }
639
640 return vector;
641}
642
Simon Glass3a571232017-05-18 20:09:18 -0600643static int gpio_request_tail(int ret, ofnode node,
644 struct ofnode_phandle_args *args,
645 const char *list_name, int index,
646 struct gpio_desc *desc, int flags, bool add_index)
Simon Glass3669e0e2015-01-05 20:05:29 -0700647{
Simon Glass3669e0e2015-01-05 20:05:29 -0700648 desc->dev = NULL;
649 desc->offset = 0;
Eric Nelson6c880b72016-04-24 16:32:40 -0700650 desc->flags = 0;
Simon Glass3a571232017-05-18 20:09:18 -0600651 if (ret)
Simon Glass3669e0e2015-01-05 20:05:29 -0700652 goto err;
Simon Glass3669e0e2015-01-05 20:05:29 -0700653
Simon Glass3a571232017-05-18 20:09:18 -0600654 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
655 &desc->dev);
Simon Glass3669e0e2015-01-05 20:05:29 -0700656 if (ret) {
Mario Sixb053dd72018-03-28 14:39:01 +0200657 debug("%s: uclass_get_device_by_ofnode failed\n", __func__);
Simon Glass3669e0e2015-01-05 20:05:29 -0700658 goto err;
659 }
Simon Glass3a571232017-05-18 20:09:18 -0600660 ret = gpio_find_and_xlate(desc, args);
Simon Glass3669e0e2015-01-05 20:05:29 -0700661 if (ret) {
662 debug("%s: gpio_find_and_xlate failed\n", __func__);
663 goto err;
664 }
665 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Simon Glass3a571232017-05-18 20:09:18 -0600666 ofnode_get_name(node),
Simon Glass3669e0e2015-01-05 20:05:29 -0700667 list_name, index);
668 if (ret) {
669 debug("%s: dm_gpio_requestf failed\n", __func__);
670 goto err;
671 }
672 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
673 if (ret) {
674 debug("%s: dm_gpio_set_dir failed\n", __func__);
675 goto err;
676 }
677
678 return 0;
679err:
680 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Simon Glass3a571232017-05-18 20:09:18 -0600681 __func__, ofnode_get_name(node), list_name, index, ret);
Simon Glass3669e0e2015-01-05 20:05:29 -0700682 return ret;
683}
684
Simon Glass150c5af2017-05-30 21:47:09 -0600685static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
686 int index, struct gpio_desc *desc,
687 int flags, bool add_index)
Simon Glass3a571232017-05-18 20:09:18 -0600688{
689 struct ofnode_phandle_args args;
690 int ret;
691
Simon Glass150c5af2017-05-30 21:47:09 -0600692 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
693 index, &args);
Simon Glass3a571232017-05-18 20:09:18 -0600694
Simon Glass150c5af2017-05-30 21:47:09 -0600695 return gpio_request_tail(ret, node, &args, list_name, index, desc,
696 flags, add_index);
Simon Glass3a571232017-05-18 20:09:18 -0600697}
698
Simon Glass150c5af2017-05-30 21:47:09 -0600699int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass3669e0e2015-01-05 20:05:29 -0700700 struct gpio_desc *desc, int flags)
701{
Simon Glass150c5af2017-05-30 21:47:09 -0600702 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
703 index > 0);
Simon Glass3669e0e2015-01-05 20:05:29 -0700704}
705
Simon Glass150c5af2017-05-30 21:47:09 -0600706int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass3669e0e2015-01-05 20:05:29 -0700707 struct gpio_desc *desc, int flags)
708{
Simon Glass150c5af2017-05-30 21:47:09 -0600709 struct ofnode_phandle_args args;
710 int ret;
711
712 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
713 index, &args);
714
715 return gpio_request_tail(ret, dev_ofnode(dev), &args, list_name,
716 index, desc, flags, index > 0);
Simon Glass3669e0e2015-01-05 20:05:29 -0700717}
718
Simon Glass150c5af2017-05-30 21:47:09 -0600719int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass3669e0e2015-01-05 20:05:29 -0700720 struct gpio_desc *desc, int max_count,
721 int flags)
722{
723 int count;
724 int ret;
725
Przemyslaw Marczak2984e7a2015-03-31 18:57:16 +0200726 for (count = 0; count < max_count; count++) {
Simon Glass150c5af2017-05-30 21:47:09 -0600727 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass3669e0e2015-01-05 20:05:29 -0700728 &desc[count], flags, true);
729 if (ret == -ENOENT)
730 break;
731 else if (ret)
732 goto err;
733 }
734
735 /* We ran out of GPIOs in the list */
736 return count;
737
738err:
739 gpio_free_list_nodev(desc, count - 1);
740
741 return ret;
742}
743
744int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
745 struct gpio_desc *desc, int max_count,
746 int flags)
747{
748 /*
749 * This isn't ideal since we don't use dev->name in the debug()
750 * calls in gpio_request_by_name(), but we can do this until
751 * gpio_request_list_by_name_nodev() can be dropped.
752 */
Simon Glass150c5af2017-05-30 21:47:09 -0600753 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
754 max_count, flags);
Simon Glass3669e0e2015-01-05 20:05:29 -0700755}
756
757int gpio_get_list_count(struct udevice *dev, const char *list_name)
758{
759 int ret;
760
Simon Glasse160f7d2017-01-17 16:52:55 -0700761 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
Simon Glass3669e0e2015-01-05 20:05:29 -0700762 list_name, "#gpio-cells", 0, -1,
763 NULL);
764 if (ret) {
765 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
766 __func__, dev->name, list_name, ret);
767 }
768
769 return ret;
770}
771
772int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
773{
774 /* For now, we don't do any checking of dev */
775 return _dm_gpio_free(desc->dev, desc->offset);
776}
777
778int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
779{
780 int i;
781
782 /* For now, we don't do any checking of dev */
783 for (i = 0; i < count; i++)
784 dm_gpio_free(dev, &desc[i]);
785
786 return 0;
787}
788
789int gpio_free_list_nodev(struct gpio_desc *desc, int count)
790{
791 return gpio_free_list(NULL, desc, count);
792}
793
Simon Glass96495d92014-02-26 15:59:24 -0700794/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glassb892d122014-10-04 11:29:42 -0600795static int gpio_renumber(struct udevice *removed_dev)
Simon Glass96495d92014-02-26 15:59:24 -0700796{
797 struct gpio_dev_priv *uc_priv;
Heiko Schocher54c5d082014-05-22 12:43:05 +0200798 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -0700799 struct uclass *uc;
800 unsigned base;
801 int ret;
802
803 ret = uclass_get(UCLASS_GPIO, &uc);
804 if (ret)
805 return ret;
806
807 /* Ensure that we have a base for each bank */
808 base = 0;
809 uclass_foreach_dev(dev, uc) {
Simon Glassb892d122014-10-04 11:29:42 -0600810 if (device_active(dev) && dev != removed_dev) {
Simon Glasse564f052015-03-05 12:25:20 -0700811 uc_priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -0700812 uc_priv->gpio_base = base;
813 base += uc_priv->gpio_count;
814 }
815 }
816
817 return 0;
818}
819
Simon Glass17c43f12016-03-06 19:27:51 -0700820int gpio_get_number(const struct gpio_desc *desc)
Simon Glass56a71f82015-03-25 12:21:58 -0600821{
822 struct udevice *dev = desc->dev;
823 struct gpio_dev_priv *uc_priv;
824
825 if (!dev)
826 return -1;
827 uc_priv = dev->uclass_priv;
828
829 return uc_priv->gpio_base + desc->offset;
830}
831
Heiko Schocher54c5d082014-05-22 12:43:05 +0200832static int gpio_post_probe(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -0700833{
Simon Glasse564f052015-03-05 12:25:20 -0700834 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -0600835
836 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
837 if (!uc_priv->name)
838 return -ENOMEM;
839
840 return gpio_renumber(NULL);
Simon Glass96495d92014-02-26 15:59:24 -0700841}
842
Heiko Schocher54c5d082014-05-22 12:43:05 +0200843static int gpio_pre_remove(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -0700844{
Simon Glasse564f052015-03-05 12:25:20 -0700845 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -0600846 int i;
847
848 for (i = 0; i < uc_priv->gpio_count; i++) {
849 if (uc_priv->name[i])
850 free(uc_priv->name[i]);
851 }
852 free(uc_priv->name);
853
854 return gpio_renumber(dev);
Simon Glass96495d92014-02-26 15:59:24 -0700855}
856
Michal Simek1b4c2aa2018-07-12 12:42:27 +0200857static int gpio_post_bind(struct udevice *dev)
858{
859#if defined(CONFIG_NEEDS_MANUAL_RELOC)
860 struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
861 static int reloc_done;
862
863 if (!reloc_done) {
864 if (ops->request)
865 ops->request += gd->reloc_off;
866 if (ops->free)
867 ops->free += gd->reloc_off;
868 if (ops->direction_input)
869 ops->direction_input += gd->reloc_off;
870 if (ops->direction_output)
871 ops->direction_output += gd->reloc_off;
872 if (ops->get_value)
873 ops->get_value += gd->reloc_off;
874 if (ops->set_value)
875 ops->set_value += gd->reloc_off;
876 if (ops->get_open_drain)
877 ops->get_open_drain += gd->reloc_off;
878 if (ops->set_open_drain)
879 ops->set_open_drain += gd->reloc_off;
880 if (ops->get_function)
881 ops->get_function += gd->reloc_off;
882 if (ops->xlate)
883 ops->xlate += gd->reloc_off;
884
885 reloc_done++;
886 }
887#endif
888 return 0;
889}
890
Simon Glass96495d92014-02-26 15:59:24 -0700891UCLASS_DRIVER(gpio) = {
892 .id = UCLASS_GPIO,
893 .name = "gpio",
Bhuvanchandra DVae89bb02015-06-01 18:37:15 +0530894 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass96495d92014-02-26 15:59:24 -0700895 .post_probe = gpio_post_probe,
Michal Simek1b4c2aa2018-07-12 12:42:27 +0200896 .post_bind = gpio_post_bind,
Simon Glass96495d92014-02-26 15:59:24 -0700897 .pre_remove = gpio_pre_remove,
898 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
899};