blob: 9faf3357af8bad2dad5a6a02eee2017df7d542b8 [file] [log] [blame]
Simon Glass96495d92014-02-26 15:59:24 -07001/*
2 * Copyright (c) 2013 Google, Inc
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <common.h>
8#include <dm.h>
Eric Nelson6c880b72016-04-24 16:32:40 -07009#include <dt-bindings/gpio/gpio.h>
Simon Glass96495d92014-02-26 15:59:24 -070010#include <errno.h>
Simon Glass0dac4d52015-01-05 20:05:28 -070011#include <fdtdec.h>
Simon Glassb892d122014-10-04 11:29:42 -060012#include <malloc.h>
Simon Glass96495d92014-02-26 15:59:24 -070013#include <asm/gpio.h>
Masahiro Yamada84b8bf62016-01-24 23:27:48 +090014#include <linux/bug.h>
Simon Glassfe1ef502014-10-22 21:37:01 -060015#include <linux/ctype.h>
Simon Glass96495d92014-02-26 15:59:24 -070016
Simon Glass3669e0e2015-01-05 20:05:29 -070017DECLARE_GLOBAL_DATA_PTR;
18
Simon Glass96495d92014-02-26 15:59:24 -070019/**
20 * gpio_to_device() - Convert global GPIO number to device, number
Simon Glass96495d92014-02-26 15:59:24 -070021 *
22 * Convert the GPIO number to an entry in the list of GPIOs
23 * or GPIO blocks registered with the GPIO controller. Returns
24 * entry on success, NULL on error.
Simon Glassae7123f2015-01-05 20:05:27 -070025 *
26 * @gpio: The numeric representation of the GPIO
27 * @desc: Returns description (desc->flags will always be 0)
28 * @return 0 if found, -ENOENT if not found
Simon Glass96495d92014-02-26 15:59:24 -070029 */
Simon Glassae7123f2015-01-05 20:05:27 -070030static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glass96495d92014-02-26 15:59:24 -070031{
32 struct gpio_dev_priv *uc_priv;
Heiko Schocher54c5d082014-05-22 12:43:05 +020033 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -070034 int ret;
35
36 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
37 dev;
38 ret = uclass_next_device(&dev)) {
Simon Glasse564f052015-03-05 12:25:20 -070039 uc_priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -070040 if (gpio >= uc_priv->gpio_base &&
41 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
Simon Glassae7123f2015-01-05 20:05:27 -070042 desc->dev = dev;
43 desc->offset = gpio - uc_priv->gpio_base;
44 desc->flags = 0;
Simon Glass96495d92014-02-26 15:59:24 -070045 return 0;
46 }
47 }
48
49 /* No such GPIO */
Simon Glassae7123f2015-01-05 20:05:27 -070050 return ret ? ret : -ENOENT;
Simon Glass96495d92014-02-26 15:59:24 -070051}
52
Simon Glass32ec1592015-06-23 15:38:40 -060053int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
Simon Glass96495d92014-02-26 15:59:24 -070054{
Simon Glassfe1ef502014-10-22 21:37:01 -060055 struct gpio_dev_priv *uc_priv = NULL;
Heiko Schocher54c5d082014-05-22 12:43:05 +020056 struct udevice *dev;
Simon Glassfe1ef502014-10-22 21:37:01 -060057 ulong offset;
58 int numeric;
Simon Glass96495d92014-02-26 15:59:24 -070059 int ret;
60
Simon Glassfe1ef502014-10-22 21:37:01 -060061 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
Simon Glass96495d92014-02-26 15:59:24 -070062 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
63 dev;
64 ret = uclass_next_device(&dev)) {
Simon Glass96495d92014-02-26 15:59:24 -070065 int len;
66
Simon Glasse564f052015-03-05 12:25:20 -070067 uc_priv = dev_get_uclass_priv(dev);
Simon Glassfe1ef502014-10-22 21:37:01 -060068 if (numeric != -1) {
69 offset = numeric - uc_priv->gpio_base;
70 /* Allow GPIOs to be numbered from 0 */
Tom Rini75897912017-05-10 15:20:15 -040071 if (offset < uc_priv->gpio_count)
Simon Glassfe1ef502014-10-22 21:37:01 -060072 break;
73 }
74
Simon Glass96495d92014-02-26 15:59:24 -070075 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
76
Simon Glass939cda52014-06-11 23:29:47 -060077 if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glassfe1ef502014-10-22 21:37:01 -060078 if (!strict_strtoul(name + len, 10, &offset))
79 break;
Simon Glass96495d92014-02-26 15:59:24 -070080 }
81 }
82
Simon Glassfe1ef502014-10-22 21:37:01 -060083 if (!dev)
84 return ret ? ret : -EINVAL;
85
Simon Glass32ec1592015-06-23 15:38:40 -060086 desc->dev = dev;
87 desc->offset = offset;
88
89 return 0;
90}
91
92int gpio_lookup_name(const char *name, struct udevice **devp,
93 unsigned int *offsetp, unsigned int *gpiop)
94{
95 struct gpio_desc desc;
96 int ret;
97
Simon Glassfe1ef502014-10-22 21:37:01 -060098 if (devp)
Simon Glass32ec1592015-06-23 15:38:40 -060099 *devp = NULL;
100 ret = dm_gpio_lookup_name(name, &desc);
101 if (ret)
102 return ret;
103
104 if (devp)
105 *devp = desc.dev;
Simon Glassfe1ef502014-10-22 21:37:01 -0600106 if (offsetp)
Simon Glass32ec1592015-06-23 15:38:40 -0600107 *offsetp = desc.offset;
108 if (gpiop) {
109 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
110
111 *gpiop = uc_priv->gpio_base + desc.offset;
112 }
Simon Glassfe1ef502014-10-22 21:37:01 -0600113
114 return 0;
Simon Glass96495d92014-02-26 15:59:24 -0700115}
116
Simon Glass3a571232017-05-18 20:09:18 -0600117int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
118 struct ofnode_phandle_args *args)
Eric Nelson6c880b72016-04-24 16:32:40 -0700119{
120 if (args->args_count < 1)
121 return -EINVAL;
122
123 desc->offset = args->args[0];
124
125 if (args->args_count < 2)
126 return 0;
127
128 if (args->args[1] & GPIO_ACTIVE_LOW)
129 desc->flags = GPIOD_ACTIVE_LOW;
130
131 return 0;
132}
133
Simon Glass3669e0e2015-01-05 20:05:29 -0700134static int gpio_find_and_xlate(struct gpio_desc *desc,
Simon Glass3a571232017-05-18 20:09:18 -0600135 struct ofnode_phandle_args *args)
Simon Glass0dac4d52015-01-05 20:05:28 -0700136{
137 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
138
Eric Nelson6c880b72016-04-24 16:32:40 -0700139 if (ops->xlate)
140 return ops->xlate(desc->dev, desc, args);
Simon Glass0dac4d52015-01-05 20:05:28 -0700141 else
Eric Nelson6c880b72016-04-24 16:32:40 -0700142 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glass0dac4d52015-01-05 20:05:28 -0700143}
144
Simon Glassefa677f2015-06-23 15:38:41 -0600145int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassae7123f2015-01-05 20:05:27 -0700146{
147 struct udevice *dev = desc->dev;
148 struct gpio_dev_priv *uc_priv;
149 char *str;
150 int ret;
151
Simon Glasse564f052015-03-05 12:25:20 -0700152 uc_priv = dev_get_uclass_priv(dev);
Simon Glassae7123f2015-01-05 20:05:27 -0700153 if (uc_priv->name[desc->offset])
154 return -EBUSY;
155 str = strdup(label);
156 if (!str)
157 return -ENOMEM;
158 if (gpio_get_ops(dev)->request) {
159 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
160 if (ret) {
161 free(str);
162 return ret;
163 }
164 }
165 uc_priv->name[desc->offset] = str;
166
167 return 0;
168}
169
Simon Glass3669e0e2015-01-05 20:05:29 -0700170static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
171{
Simon Glass4dc52592015-12-29 05:22:48 -0700172#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
Simon Glass3669e0e2015-01-05 20:05:29 -0700173 va_list args;
174 char buf[40];
175
176 va_start(args, fmt);
177 vscnprintf(buf, sizeof(buf), fmt, args);
178 va_end(args);
179 return dm_gpio_request(desc, buf);
Simon Glass4dc52592015-12-29 05:22:48 -0700180#else
181 return dm_gpio_request(desc, fmt);
182#endif
Simon Glass3669e0e2015-01-05 20:05:29 -0700183}
184
Simon Glass96495d92014-02-26 15:59:24 -0700185/**
186 * gpio_request() - [COMPAT] Request GPIO
187 * gpio: GPIO number
188 * label: Name for the requested GPIO
189 *
Simon Glassb892d122014-10-04 11:29:42 -0600190 * The label is copied and allocated so the caller does not need to keep
191 * the pointer around.
192 *
Simon Glass96495d92014-02-26 15:59:24 -0700193 * This function implements the API that's compatible with current
194 * GPIO API used in U-Boot. The request is forwarded to particular
195 * GPIO driver. Returns 0 on success, negative value on error.
196 */
197int gpio_request(unsigned gpio, const char *label)
198{
Simon Glassae7123f2015-01-05 20:05:27 -0700199 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700200 int ret;
201
Simon Glassae7123f2015-01-05 20:05:27 -0700202 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700203 if (ret)
204 return ret;
205
Simon Glassae7123f2015-01-05 20:05:27 -0700206 return dm_gpio_request(&desc, label);
Simon Glass96495d92014-02-26 15:59:24 -0700207}
208
209/**
Simon Glassd44f5972014-10-04 11:29:49 -0600210 * gpio_requestf() - [COMPAT] Request GPIO
211 * @gpio: GPIO number
212 * @fmt: Format string for the requested GPIO
213 * @...: Arguments for the printf() format string
214 *
215 * This function implements the API that's compatible with current
216 * GPIO API used in U-Boot. The request is forwarded to particular
217 * GPIO driver. Returns 0 on success, negative value on error.
218 */
219int gpio_requestf(unsigned gpio, const char *fmt, ...)
220{
Simon Glass4dc52592015-12-29 05:22:48 -0700221#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
Simon Glassd44f5972014-10-04 11:29:49 -0600222 va_list args;
223 char buf[40];
224
225 va_start(args, fmt);
226 vscnprintf(buf, sizeof(buf), fmt, args);
227 va_end(args);
228 return gpio_request(gpio, buf);
Simon Glass4dc52592015-12-29 05:22:48 -0700229#else
230 return gpio_request(gpio, fmt);
231#endif
Simon Glassd44f5972014-10-04 11:29:49 -0600232}
233
Simon Glassae7123f2015-01-05 20:05:27 -0700234int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glass96495d92014-02-26 15:59:24 -0700235{
Simon Glassb892d122014-10-04 11:29:42 -0600236 struct gpio_dev_priv *uc_priv;
Simon Glass96495d92014-02-26 15:59:24 -0700237 int ret;
238
Simon Glasse564f052015-03-05 12:25:20 -0700239 uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -0600240 if (!uc_priv->name[offset])
241 return -ENXIO;
242 if (gpio_get_ops(dev)->free) {
243 ret = gpio_get_ops(dev)->free(dev, offset);
244 if (ret)
245 return ret;
246 }
247
248 free(uc_priv->name[offset]);
249 uc_priv->name[offset] = NULL;
250
251 return 0;
252}
253
Simon Glassae7123f2015-01-05 20:05:27 -0700254/**
255 * gpio_free() - [COMPAT] Relinquish GPIO
256 * gpio: GPIO number
257 *
258 * This function implements the API that's compatible with current
259 * GPIO API used in U-Boot. The request is forwarded to particular
260 * GPIO driver. Returns 0 on success, negative value on error.
261 */
262int gpio_free(unsigned gpio)
Simon Glassb892d122014-10-04 11:29:42 -0600263{
Simon Glassae7123f2015-01-05 20:05:27 -0700264 struct gpio_desc desc;
265 int ret;
Simon Glassb892d122014-10-04 11:29:42 -0600266
Simon Glassae7123f2015-01-05 20:05:27 -0700267 ret = gpio_to_device(gpio, &desc);
268 if (ret)
269 return ret;
270
271 return _dm_gpio_free(desc.dev, desc.offset);
272}
273
Simon Glass17c43f12016-03-06 19:27:51 -0700274static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glassae7123f2015-01-05 20:05:27 -0700275{
Simon Glasseca48662015-07-02 18:16:16 -0600276 struct gpio_dev_priv *uc_priv;
Simon Glassae7123f2015-01-05 20:05:27 -0700277
Simon Glasseca48662015-07-02 18:16:16 -0600278 if (!dm_gpio_is_valid(desc))
279 return -ENOENT;
280
281 uc_priv = dev_get_uclass_priv(desc->dev);
Simon Glassae7123f2015-01-05 20:05:27 -0700282 if (!uc_priv->name[desc->offset]) {
Simon Glassb892d122014-10-04 11:29:42 -0600283 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassae7123f2015-01-05 20:05:27 -0700284 desc->dev->name, func,
285 uc_priv->bank_name ? uc_priv->bank_name : "",
286 desc->offset);
Simon Glassb892d122014-10-04 11:29:42 -0600287 return -EBUSY;
288 }
289
290 return 0;
Simon Glass96495d92014-02-26 15:59:24 -0700291}
292
293/**
294 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
295 * gpio: GPIO number
296 *
297 * This function implements the API that's compatible with current
298 * GPIO API used in U-Boot. The request is forwarded to particular
299 * GPIO driver. Returns 0 on success, negative value on error.
300 */
301int gpio_direction_input(unsigned gpio)
302{
Simon Glassae7123f2015-01-05 20:05:27 -0700303 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700304 int ret;
305
Simon Glassae7123f2015-01-05 20:05:27 -0700306 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700307 if (ret)
308 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700309 ret = check_reserved(&desc, "dir_input");
310 if (ret)
311 return ret;
Simon Glass96495d92014-02-26 15:59:24 -0700312
Simon Glassae7123f2015-01-05 20:05:27 -0700313 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
Simon Glass96495d92014-02-26 15:59:24 -0700314}
315
316/**
317 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
318 * gpio: GPIO number
319 * value: Logical value to be set on the GPIO pin
320 *
321 * This function implements the API that's compatible with current
322 * GPIO API used in U-Boot. The request is forwarded to particular
323 * GPIO driver. Returns 0 on success, negative value on error.
324 */
325int gpio_direction_output(unsigned gpio, int value)
326{
Simon Glassae7123f2015-01-05 20:05:27 -0700327 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700328 int ret;
329
Simon Glassae7123f2015-01-05 20:05:27 -0700330 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700331 if (ret)
332 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700333 ret = check_reserved(&desc, "dir_output");
334 if (ret)
335 return ret;
Simon Glass96495d92014-02-26 15:59:24 -0700336
Simon Glassae7123f2015-01-05 20:05:27 -0700337 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
338 desc.offset, value);
339}
340
Simon Glass17c43f12016-03-06 19:27:51 -0700341int dm_gpio_get_value(const struct gpio_desc *desc)
Simon Glassae7123f2015-01-05 20:05:27 -0700342{
343 int value;
344 int ret;
345
346 ret = check_reserved(desc, "get_value");
347 if (ret)
348 return ret;
349
350 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
351
352 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
353}
354
Simon Glass17c43f12016-03-06 19:27:51 -0700355int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassae7123f2015-01-05 20:05:27 -0700356{
357 int ret;
358
359 ret = check_reserved(desc, "set_value");
360 if (ret)
361 return ret;
362
363 if (desc->flags & GPIOD_ACTIVE_LOW)
364 value = !value;
365 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
366 return 0;
367}
368
mario.six@gdsys.cc53ecdfb2016-05-25 15:15:21 +0200369int dm_gpio_get_open_drain(struct gpio_desc *desc)
370{
371 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
372 int ret;
373
374 ret = check_reserved(desc, "get_open_drain");
375 if (ret)
376 return ret;
377
378 if (ops->set_open_drain)
379 return ops->get_open_drain(desc->dev, desc->offset);
380 else
381 return -ENOSYS;
382}
383
384int dm_gpio_set_open_drain(struct gpio_desc *desc, int value)
385{
386 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
387 int ret;
388
389 ret = check_reserved(desc, "set_open_drain");
390 if (ret)
391 return ret;
392
393 if (ops->set_open_drain)
394 ret = ops->set_open_drain(desc->dev, desc->offset, value);
395 else
396 return 0; /* feature not supported -> ignore setting */
397
398 return ret;
399}
400
Simon Glassae7123f2015-01-05 20:05:27 -0700401int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
402{
403 struct udevice *dev = desc->dev;
404 struct dm_gpio_ops *ops = gpio_get_ops(dev);
405 int ret;
406
407 ret = check_reserved(desc, "set_dir");
408 if (ret)
409 return ret;
410
411 if (flags & GPIOD_IS_OUT) {
412 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
413
414 if (flags & GPIOD_ACTIVE_LOW)
415 value = !value;
416 ret = ops->direction_output(dev, desc->offset, value);
417 } else if (flags & GPIOD_IS_IN) {
418 ret = ops->direction_input(dev, desc->offset);
419 }
420 if (ret)
421 return ret;
422 /*
423 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
424 * futures
425 */
426 desc->flags = flags;
427
428 return 0;
429}
430
431int dm_gpio_set_dir(struct gpio_desc *desc)
432{
433 return dm_gpio_set_dir_flags(desc, desc->flags);
Simon Glass96495d92014-02-26 15:59:24 -0700434}
435
436/**
437 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
438 * gpio: GPIO number
439 *
440 * This function implements the API that's compatible with current
441 * GPIO API used in U-Boot. The request is forwarded to particular
442 * GPIO driver. Returns the value of the GPIO pin, or negative value
443 * on error.
444 */
445int gpio_get_value(unsigned gpio)
446{
Simon Glass96495d92014-02-26 15:59:24 -0700447 int ret;
448
Simon Glassae7123f2015-01-05 20:05:27 -0700449 struct gpio_desc desc;
450
451 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700452 if (ret)
453 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700454 return dm_gpio_get_value(&desc);
Simon Glass96495d92014-02-26 15:59:24 -0700455}
456
457/**
458 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
459 * gpio: GPIO number
460 * value: Logical value to be set on the GPIO pin.
461 *
462 * This function implements the API that's compatible with current
463 * GPIO API used in U-Boot. The request is forwarded to particular
464 * GPIO driver. Returns 0 on success, negative value on error.
465 */
466int gpio_set_value(unsigned gpio, int value)
467{
Simon Glassae7123f2015-01-05 20:05:27 -0700468 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700469 int ret;
470
Simon Glassae7123f2015-01-05 20:05:27 -0700471 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700472 if (ret)
473 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700474 return dm_gpio_set_value(&desc, value);
Simon Glass96495d92014-02-26 15:59:24 -0700475}
476
Heiko Schocher54c5d082014-05-22 12:43:05 +0200477const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glass96495d92014-02-26 15:59:24 -0700478{
479 struct gpio_dev_priv *priv;
480
481 /* Must be called on an active device */
Simon Glasse564f052015-03-05 12:25:20 -0700482 priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -0700483 assert(priv);
484
485 *bit_count = priv->gpio_count;
486 return priv->bank_name;
487}
488
Simon Glass6449a502014-10-04 11:29:43 -0600489static const char * const gpio_function[GPIOF_COUNT] = {
490 "input",
491 "output",
492 "unused",
493 "unknown",
494 "func",
495};
496
Masahiro Yamadafb07f972017-06-22 16:50:25 +0900497static int get_function(struct udevice *dev, int offset, bool skip_unused,
498 const char **namep)
Simon Glass6449a502014-10-04 11:29:43 -0600499{
Simon Glasse564f052015-03-05 12:25:20 -0700500 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass6449a502014-10-04 11:29:43 -0600501 struct dm_gpio_ops *ops = gpio_get_ops(dev);
502
503 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
504 if (!device_active(dev))
505 return -ENODEV;
506 if (offset < 0 || offset >= uc_priv->gpio_count)
507 return -EINVAL;
508 if (namep)
509 *namep = uc_priv->name[offset];
510 if (skip_unused && !uc_priv->name[offset])
511 return GPIOF_UNUSED;
512 if (ops->get_function) {
513 int ret;
514
515 ret = ops->get_function(dev, offset);
516 if (ret < 0)
517 return ret;
518 if (ret >= ARRAY_SIZE(gpio_function))
519 return -ENODATA;
520 return ret;
521 }
522
523 return GPIOF_UNKNOWN;
524}
525
526int gpio_get_function(struct udevice *dev, int offset, const char **namep)
527{
528 return get_function(dev, offset, true, namep);
529}
530
531int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
532{
533 return get_function(dev, offset, false, namep);
534}
535
Simon Glass07575352014-10-04 11:29:44 -0600536int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
537{
538 struct dm_gpio_ops *ops = gpio_get_ops(dev);
539 struct gpio_dev_priv *priv;
540 char *str = buf;
541 int func;
542 int ret;
543 int len;
544
545 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
546
547 *buf = 0;
Simon Glasse564f052015-03-05 12:25:20 -0700548 priv = dev_get_uclass_priv(dev);
Simon Glass07575352014-10-04 11:29:44 -0600549 ret = gpio_get_raw_function(dev, offset, NULL);
550 if (ret < 0)
551 return ret;
552 func = ret;
553 len = snprintf(str, buffsize, "%s%d: %s",
554 priv->bank_name ? priv->bank_name : "",
555 offset, gpio_function[func]);
556 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
557 func == GPIOF_UNUSED) {
558 const char *label;
559 bool used;
560
561 ret = ops->get_value(dev, offset);
562 if (ret < 0)
563 return ret;
564 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
565 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
566 ret,
567 used ? 'x' : ' ',
568 used ? " " : "",
569 label ? label : "");
570 }
571
572 return 0;
573}
574
Simon Glass962f5ca2015-04-14 21:03:20 -0600575int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
576{
577 int i, ret;
578 int gpio;
579
580 for (i = 0; i < 32; i++) {
581 gpio = gpio_num_array[i];
582 if (gpio == -1)
583 break;
584 ret = gpio_requestf(gpio, fmt, i);
585 if (ret)
586 goto err;
587 ret = gpio_direction_input(gpio);
588 if (ret) {
589 gpio_free(gpio);
590 goto err;
591 }
592 }
593
594 return 0;
595err:
596 for (i--; i >= 0; i--)
597 gpio_free(gpio_num_array[i]);
598
599 return ret;
600}
601
Simon Glasse5901c92014-11-10 18:00:21 -0700602/*
603 * get a number comprised of multiple GPIO values. gpio_num_array points to
604 * the array of gpio pin numbers to scan, terminated by -1.
605 */
Simon Glass962f5ca2015-04-14 21:03:20 -0600606int gpio_get_values_as_int(const int *gpio_list)
Simon Glasse5901c92014-11-10 18:00:21 -0700607{
608 int gpio;
609 unsigned bitmask = 1;
610 unsigned vector = 0;
Simon Glass962f5ca2015-04-14 21:03:20 -0600611 int ret;
Simon Glasse5901c92014-11-10 18:00:21 -0700612
613 while (bitmask &&
Simon Glass962f5ca2015-04-14 21:03:20 -0600614 ((gpio = *gpio_list++) != -1)) {
615 ret = gpio_get_value(gpio);
616 if (ret < 0)
617 return ret;
618 else if (ret)
Simon Glasse5901c92014-11-10 18:00:21 -0700619 vector |= bitmask;
620 bitmask <<= 1;
621 }
Simon Glass962f5ca2015-04-14 21:03:20 -0600622
Simon Glasse5901c92014-11-10 18:00:21 -0700623 return vector;
624}
625
Simon Glass17c43f12016-03-06 19:27:51 -0700626int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassbbf24782016-03-06 19:27:50 -0700627{
628 unsigned bitmask = 1;
629 unsigned vector = 0;
630 int ret, i;
631
632 for (i = 0; i < count; i++) {
633 ret = dm_gpio_get_value(&desc_list[i]);
634 if (ret < 0)
635 return ret;
636 else if (ret)
637 vector |= bitmask;
638 bitmask <<= 1;
639 }
640
641 return vector;
642}
643
Simon Glass3a571232017-05-18 20:09:18 -0600644static int gpio_request_tail(int ret, ofnode node,
645 struct ofnode_phandle_args *args,
646 const char *list_name, int index,
647 struct gpio_desc *desc, int flags, bool add_index)
Simon Glass3669e0e2015-01-05 20:05:29 -0700648{
Simon Glass3669e0e2015-01-05 20:05:29 -0700649 desc->dev = NULL;
650 desc->offset = 0;
Eric Nelson6c880b72016-04-24 16:32:40 -0700651 desc->flags = 0;
Simon Glass3a571232017-05-18 20:09:18 -0600652 if (ret)
Simon Glass3669e0e2015-01-05 20:05:29 -0700653 goto err;
Simon Glass3669e0e2015-01-05 20:05:29 -0700654
Simon Glass3a571232017-05-18 20:09:18 -0600655 ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
656 &desc->dev);
Simon Glass3669e0e2015-01-05 20:05:29 -0700657 if (ret) {
658 debug("%s: uclass_get_device_by_of_offset failed\n", __func__);
659 goto err;
660 }
Simon Glass3a571232017-05-18 20:09:18 -0600661 ret = gpio_find_and_xlate(desc, args);
Simon Glass3669e0e2015-01-05 20:05:29 -0700662 if (ret) {
663 debug("%s: gpio_find_and_xlate failed\n", __func__);
664 goto err;
665 }
666 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
Simon Glass3a571232017-05-18 20:09:18 -0600667 ofnode_get_name(node),
Simon Glass3669e0e2015-01-05 20:05:29 -0700668 list_name, index);
669 if (ret) {
670 debug("%s: dm_gpio_requestf failed\n", __func__);
671 goto err;
672 }
673 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
674 if (ret) {
675 debug("%s: dm_gpio_set_dir failed\n", __func__);
676 goto err;
677 }
678
679 return 0;
680err:
681 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
Simon Glass3a571232017-05-18 20:09:18 -0600682 __func__, ofnode_get_name(node), list_name, index, ret);
Simon Glass3669e0e2015-01-05 20:05:29 -0700683 return ret;
684}
685
Simon Glass150c5af2017-05-30 21:47:09 -0600686static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
687 int index, struct gpio_desc *desc,
688 int flags, bool add_index)
Simon Glass3a571232017-05-18 20:09:18 -0600689{
690 struct ofnode_phandle_args args;
691 int ret;
692
Simon Glass150c5af2017-05-30 21:47:09 -0600693 ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
694 index, &args);
Simon Glass3a571232017-05-18 20:09:18 -0600695
Simon Glass150c5af2017-05-30 21:47:09 -0600696 return gpio_request_tail(ret, node, &args, list_name, index, desc,
697 flags, add_index);
Simon Glass3a571232017-05-18 20:09:18 -0600698}
699
Simon Glass150c5af2017-05-30 21:47:09 -0600700int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
Simon Glass3669e0e2015-01-05 20:05:29 -0700701 struct gpio_desc *desc, int flags)
702{
Simon Glass150c5af2017-05-30 21:47:09 -0600703 return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
704 index > 0);
Simon Glass3669e0e2015-01-05 20:05:29 -0700705}
706
Simon Glass150c5af2017-05-30 21:47:09 -0600707int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
Simon Glass3669e0e2015-01-05 20:05:29 -0700708 struct gpio_desc *desc, int flags)
709{
Simon Glass150c5af2017-05-30 21:47:09 -0600710 struct ofnode_phandle_args args;
711 int ret;
712
713 ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
714 index, &args);
715
716 return gpio_request_tail(ret, dev_ofnode(dev), &args, list_name,
717 index, desc, flags, index > 0);
Simon Glass3669e0e2015-01-05 20:05:29 -0700718}
719
Simon Glass150c5af2017-05-30 21:47:09 -0600720int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
Simon Glass3669e0e2015-01-05 20:05:29 -0700721 struct gpio_desc *desc, int max_count,
722 int flags)
723{
724 int count;
725 int ret;
726
Przemyslaw Marczak2984e7a2015-03-31 18:57:16 +0200727 for (count = 0; count < max_count; count++) {
Simon Glass150c5af2017-05-30 21:47:09 -0600728 ret = _gpio_request_by_name_nodev(node, list_name, count,
Simon Glass3669e0e2015-01-05 20:05:29 -0700729 &desc[count], flags, true);
730 if (ret == -ENOENT)
731 break;
732 else if (ret)
733 goto err;
734 }
735
736 /* We ran out of GPIOs in the list */
737 return count;
738
739err:
740 gpio_free_list_nodev(desc, count - 1);
741
742 return ret;
743}
744
745int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
746 struct gpio_desc *desc, int max_count,
747 int flags)
748{
749 /*
750 * This isn't ideal since we don't use dev->name in the debug()
751 * calls in gpio_request_by_name(), but we can do this until
752 * gpio_request_list_by_name_nodev() can be dropped.
753 */
Simon Glass150c5af2017-05-30 21:47:09 -0600754 return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
755 max_count, flags);
Simon Glass3669e0e2015-01-05 20:05:29 -0700756}
757
758int gpio_get_list_count(struct udevice *dev, const char *list_name)
759{
760 int ret;
761
Simon Glasse160f7d2017-01-17 16:52:55 -0700762 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev_of_offset(dev),
Simon Glass3669e0e2015-01-05 20:05:29 -0700763 list_name, "#gpio-cells", 0, -1,
764 NULL);
765 if (ret) {
766 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
767 __func__, dev->name, list_name, ret);
768 }
769
770 return ret;
771}
772
773int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
774{
775 /* For now, we don't do any checking of dev */
776 return _dm_gpio_free(desc->dev, desc->offset);
777}
778
779int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
780{
781 int i;
782
783 /* For now, we don't do any checking of dev */
784 for (i = 0; i < count; i++)
785 dm_gpio_free(dev, &desc[i]);
786
787 return 0;
788}
789
790int gpio_free_list_nodev(struct gpio_desc *desc, int count)
791{
792 return gpio_free_list(NULL, desc, count);
793}
794
Simon Glass96495d92014-02-26 15:59:24 -0700795/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glassb892d122014-10-04 11:29:42 -0600796static int gpio_renumber(struct udevice *removed_dev)
Simon Glass96495d92014-02-26 15:59:24 -0700797{
798 struct gpio_dev_priv *uc_priv;
Heiko Schocher54c5d082014-05-22 12:43:05 +0200799 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -0700800 struct uclass *uc;
801 unsigned base;
802 int ret;
803
804 ret = uclass_get(UCLASS_GPIO, &uc);
805 if (ret)
806 return ret;
807
808 /* Ensure that we have a base for each bank */
809 base = 0;
810 uclass_foreach_dev(dev, uc) {
Simon Glassb892d122014-10-04 11:29:42 -0600811 if (device_active(dev) && dev != removed_dev) {
Simon Glasse564f052015-03-05 12:25:20 -0700812 uc_priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -0700813 uc_priv->gpio_base = base;
814 base += uc_priv->gpio_count;
815 }
816 }
817
818 return 0;
819}
820
Simon Glass17c43f12016-03-06 19:27:51 -0700821int gpio_get_number(const struct gpio_desc *desc)
Simon Glass56a71f82015-03-25 12:21:58 -0600822{
823 struct udevice *dev = desc->dev;
824 struct gpio_dev_priv *uc_priv;
825
826 if (!dev)
827 return -1;
828 uc_priv = dev->uclass_priv;
829
830 return uc_priv->gpio_base + desc->offset;
831}
832
Heiko Schocher54c5d082014-05-22 12:43:05 +0200833static int gpio_post_probe(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -0700834{
Simon Glasse564f052015-03-05 12:25:20 -0700835 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -0600836
837 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
838 if (!uc_priv->name)
839 return -ENOMEM;
840
841 return gpio_renumber(NULL);
Simon Glass96495d92014-02-26 15:59:24 -0700842}
843
Heiko Schocher54c5d082014-05-22 12:43:05 +0200844static int gpio_pre_remove(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -0700845{
Simon Glasse564f052015-03-05 12:25:20 -0700846 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -0600847 int i;
848
849 for (i = 0; i < uc_priv->gpio_count; i++) {
850 if (uc_priv->name[i])
851 free(uc_priv->name[i]);
852 }
853 free(uc_priv->name);
854
855 return gpio_renumber(dev);
Simon Glass96495d92014-02-26 15:59:24 -0700856}
857
858UCLASS_DRIVER(gpio) = {
859 .id = UCLASS_GPIO,
860 .name = "gpio",
Bhuvanchandra DVae89bb02015-06-01 18:37:15 +0530861 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass96495d92014-02-26 15:59:24 -0700862 .post_probe = gpio_post_probe,
863 .pre_remove = gpio_pre_remove,
864 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
865};