blob: 732b6c2afa12ac8b99b66ee97465971defeb1f73 [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 */
71 if (offset >= 0 && offset < uc_priv->gpio_count)
72 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
Eric Nelson6c880b72016-04-24 16:32:40 -0700117int gpio_xlate_offs_flags(struct udevice *dev,
118 struct gpio_desc *desc,
119 struct fdtdec_phandle_args *args)
120{
121 if (args->args_count < 1)
122 return -EINVAL;
123
124 desc->offset = args->args[0];
125
126 if (args->args_count < 2)
127 return 0;
128
129 if (args->args[1] & GPIO_ACTIVE_LOW)
130 desc->flags = GPIOD_ACTIVE_LOW;
131
132 return 0;
133}
134
Simon Glass3669e0e2015-01-05 20:05:29 -0700135static int gpio_find_and_xlate(struct gpio_desc *desc,
136 struct fdtdec_phandle_args *args)
Simon Glass0dac4d52015-01-05 20:05:28 -0700137{
138 struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
139
Eric Nelson6c880b72016-04-24 16:32:40 -0700140 if (ops->xlate)
141 return ops->xlate(desc->dev, desc, args);
Simon Glass0dac4d52015-01-05 20:05:28 -0700142 else
Eric Nelson6c880b72016-04-24 16:32:40 -0700143 return gpio_xlate_offs_flags(desc->dev, desc, args);
Simon Glass0dac4d52015-01-05 20:05:28 -0700144}
145
Simon Glassefa677f2015-06-23 15:38:41 -0600146int dm_gpio_request(struct gpio_desc *desc, const char *label)
Simon Glassae7123f2015-01-05 20:05:27 -0700147{
148 struct udevice *dev = desc->dev;
149 struct gpio_dev_priv *uc_priv;
150 char *str;
151 int ret;
152
Simon Glasse564f052015-03-05 12:25:20 -0700153 uc_priv = dev_get_uclass_priv(dev);
Simon Glassae7123f2015-01-05 20:05:27 -0700154 if (uc_priv->name[desc->offset])
155 return -EBUSY;
156 str = strdup(label);
157 if (!str)
158 return -ENOMEM;
159 if (gpio_get_ops(dev)->request) {
160 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
161 if (ret) {
162 free(str);
163 return ret;
164 }
165 }
166 uc_priv->name[desc->offset] = str;
167
168 return 0;
169}
170
Simon Glass3669e0e2015-01-05 20:05:29 -0700171static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
172{
Simon Glass4dc52592015-12-29 05:22:48 -0700173#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
Simon Glass3669e0e2015-01-05 20:05:29 -0700174 va_list args;
175 char buf[40];
176
177 va_start(args, fmt);
178 vscnprintf(buf, sizeof(buf), fmt, args);
179 va_end(args);
180 return dm_gpio_request(desc, buf);
Simon Glass4dc52592015-12-29 05:22:48 -0700181#else
182 return dm_gpio_request(desc, fmt);
183#endif
Simon Glass3669e0e2015-01-05 20:05:29 -0700184}
185
Simon Glass96495d92014-02-26 15:59:24 -0700186/**
187 * gpio_request() - [COMPAT] Request GPIO
188 * gpio: GPIO number
189 * label: Name for the requested GPIO
190 *
Simon Glassb892d122014-10-04 11:29:42 -0600191 * The label is copied and allocated so the caller does not need to keep
192 * the pointer around.
193 *
Simon Glass96495d92014-02-26 15:59:24 -0700194 * This function implements the API that's compatible with current
195 * GPIO API used in U-Boot. The request is forwarded to particular
196 * GPIO driver. Returns 0 on success, negative value on error.
197 */
198int gpio_request(unsigned gpio, const char *label)
199{
Simon Glassae7123f2015-01-05 20:05:27 -0700200 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700201 int ret;
202
Simon Glassae7123f2015-01-05 20:05:27 -0700203 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700204 if (ret)
205 return ret;
206
Simon Glassae7123f2015-01-05 20:05:27 -0700207 return dm_gpio_request(&desc, label);
Simon Glass96495d92014-02-26 15:59:24 -0700208}
209
210/**
Simon Glassd44f5972014-10-04 11:29:49 -0600211 * gpio_requestf() - [COMPAT] Request GPIO
212 * @gpio: GPIO number
213 * @fmt: Format string for the requested GPIO
214 * @...: Arguments for the printf() format string
215 *
216 * This function implements the API that's compatible with current
217 * GPIO API used in U-Boot. The request is forwarded to particular
218 * GPIO driver. Returns 0 on success, negative value on error.
219 */
220int gpio_requestf(unsigned gpio, const char *fmt, ...)
221{
Simon Glass4dc52592015-12-29 05:22:48 -0700222#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
Simon Glassd44f5972014-10-04 11:29:49 -0600223 va_list args;
224 char buf[40];
225
226 va_start(args, fmt);
227 vscnprintf(buf, sizeof(buf), fmt, args);
228 va_end(args);
229 return gpio_request(gpio, buf);
Simon Glass4dc52592015-12-29 05:22:48 -0700230#else
231 return gpio_request(gpio, fmt);
232#endif
Simon Glassd44f5972014-10-04 11:29:49 -0600233}
234
Simon Glassae7123f2015-01-05 20:05:27 -0700235int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glass96495d92014-02-26 15:59:24 -0700236{
Simon Glassb892d122014-10-04 11:29:42 -0600237 struct gpio_dev_priv *uc_priv;
Simon Glass96495d92014-02-26 15:59:24 -0700238 int ret;
239
Simon Glasse564f052015-03-05 12:25:20 -0700240 uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -0600241 if (!uc_priv->name[offset])
242 return -ENXIO;
243 if (gpio_get_ops(dev)->free) {
244 ret = gpio_get_ops(dev)->free(dev, offset);
245 if (ret)
246 return ret;
247 }
248
249 free(uc_priv->name[offset]);
250 uc_priv->name[offset] = NULL;
251
252 return 0;
253}
254
Simon Glassae7123f2015-01-05 20:05:27 -0700255/**
256 * gpio_free() - [COMPAT] Relinquish GPIO
257 * gpio: GPIO number
258 *
259 * This function implements the API that's compatible with current
260 * GPIO API used in U-Boot. The request is forwarded to particular
261 * GPIO driver. Returns 0 on success, negative value on error.
262 */
263int gpio_free(unsigned gpio)
Simon Glassb892d122014-10-04 11:29:42 -0600264{
Simon Glassae7123f2015-01-05 20:05:27 -0700265 struct gpio_desc desc;
266 int ret;
Simon Glassb892d122014-10-04 11:29:42 -0600267
Simon Glassae7123f2015-01-05 20:05:27 -0700268 ret = gpio_to_device(gpio, &desc);
269 if (ret)
270 return ret;
271
272 return _dm_gpio_free(desc.dev, desc.offset);
273}
274
Simon Glass17c43f12016-03-06 19:27:51 -0700275static int check_reserved(const struct gpio_desc *desc, const char *func)
Simon Glassae7123f2015-01-05 20:05:27 -0700276{
Simon Glasseca48662015-07-02 18:16:16 -0600277 struct gpio_dev_priv *uc_priv;
Simon Glassae7123f2015-01-05 20:05:27 -0700278
Simon Glasseca48662015-07-02 18:16:16 -0600279 if (!dm_gpio_is_valid(desc))
280 return -ENOENT;
281
282 uc_priv = dev_get_uclass_priv(desc->dev);
Simon Glassae7123f2015-01-05 20:05:27 -0700283 if (!uc_priv->name[desc->offset]) {
Simon Glassb892d122014-10-04 11:29:42 -0600284 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassae7123f2015-01-05 20:05:27 -0700285 desc->dev->name, func,
286 uc_priv->bank_name ? uc_priv->bank_name : "",
287 desc->offset);
Simon Glassb892d122014-10-04 11:29:42 -0600288 return -EBUSY;
289 }
290
291 return 0;
Simon Glass96495d92014-02-26 15:59:24 -0700292}
293
294/**
295 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
296 * gpio: GPIO number
297 *
298 * This function implements the API that's compatible with current
299 * GPIO API used in U-Boot. The request is forwarded to particular
300 * GPIO driver. Returns 0 on success, negative value on error.
301 */
302int gpio_direction_input(unsigned gpio)
303{
Simon Glassae7123f2015-01-05 20:05:27 -0700304 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700305 int ret;
306
Simon Glassae7123f2015-01-05 20:05:27 -0700307 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700308 if (ret)
309 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700310 ret = check_reserved(&desc, "dir_input");
311 if (ret)
312 return ret;
Simon Glass96495d92014-02-26 15:59:24 -0700313
Simon Glassae7123f2015-01-05 20:05:27 -0700314 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
Simon Glass96495d92014-02-26 15:59:24 -0700315}
316
317/**
318 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
319 * gpio: GPIO number
320 * value: Logical value to be set on the GPIO pin
321 *
322 * This function implements the API that's compatible with current
323 * GPIO API used in U-Boot. The request is forwarded to particular
324 * GPIO driver. Returns 0 on success, negative value on error.
325 */
326int gpio_direction_output(unsigned gpio, int value)
327{
Simon Glassae7123f2015-01-05 20:05:27 -0700328 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700329 int ret;
330
Simon Glassae7123f2015-01-05 20:05:27 -0700331 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700332 if (ret)
333 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700334 ret = check_reserved(&desc, "dir_output");
335 if (ret)
336 return ret;
Simon Glass96495d92014-02-26 15:59:24 -0700337
Simon Glassae7123f2015-01-05 20:05:27 -0700338 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
339 desc.offset, value);
340}
341
Simon Glass17c43f12016-03-06 19:27:51 -0700342int dm_gpio_get_value(const struct gpio_desc *desc)
Simon Glassae7123f2015-01-05 20:05:27 -0700343{
344 int value;
345 int ret;
346
347 ret = check_reserved(desc, "get_value");
348 if (ret)
349 return ret;
350
351 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
352
353 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
354}
355
Simon Glass17c43f12016-03-06 19:27:51 -0700356int dm_gpio_set_value(const struct gpio_desc *desc, int value)
Simon Glassae7123f2015-01-05 20:05:27 -0700357{
358 int ret;
359
360 ret = check_reserved(desc, "set_value");
361 if (ret)
362 return ret;
363
364 if (desc->flags & GPIOD_ACTIVE_LOW)
365 value = !value;
366 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
367 return 0;
368}
369
370int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
371{
372 struct udevice *dev = desc->dev;
373 struct dm_gpio_ops *ops = gpio_get_ops(dev);
374 int ret;
375
376 ret = check_reserved(desc, "set_dir");
377 if (ret)
378 return ret;
379
380 if (flags & GPIOD_IS_OUT) {
381 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
382
383 if (flags & GPIOD_ACTIVE_LOW)
384 value = !value;
385 ret = ops->direction_output(dev, desc->offset, value);
386 } else if (flags & GPIOD_IS_IN) {
387 ret = ops->direction_input(dev, desc->offset);
388 }
389 if (ret)
390 return ret;
391 /*
392 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
393 * futures
394 */
395 desc->flags = flags;
396
397 return 0;
398}
399
400int dm_gpio_set_dir(struct gpio_desc *desc)
401{
402 return dm_gpio_set_dir_flags(desc, desc->flags);
Simon Glass96495d92014-02-26 15:59:24 -0700403}
404
405/**
406 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
407 * gpio: GPIO number
408 *
409 * This function implements the API that's compatible with current
410 * GPIO API used in U-Boot. The request is forwarded to particular
411 * GPIO driver. Returns the value of the GPIO pin, or negative value
412 * on error.
413 */
414int gpio_get_value(unsigned gpio)
415{
Simon Glass96495d92014-02-26 15:59:24 -0700416 int ret;
417
Simon Glassae7123f2015-01-05 20:05:27 -0700418 struct gpio_desc desc;
419
420 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700421 if (ret)
422 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700423 return dm_gpio_get_value(&desc);
Simon Glass96495d92014-02-26 15:59:24 -0700424}
425
426/**
427 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
428 * gpio: GPIO number
429 * value: Logical value to be set on the GPIO pin.
430 *
431 * This function implements the API that's compatible with current
432 * GPIO API used in U-Boot. The request is forwarded to particular
433 * GPIO driver. Returns 0 on success, negative value on error.
434 */
435int gpio_set_value(unsigned gpio, int value)
436{
Simon Glassae7123f2015-01-05 20:05:27 -0700437 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700438 int ret;
439
Simon Glassae7123f2015-01-05 20:05:27 -0700440 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700441 if (ret)
442 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700443 return dm_gpio_set_value(&desc, value);
Simon Glass96495d92014-02-26 15:59:24 -0700444}
445
Heiko Schocher54c5d082014-05-22 12:43:05 +0200446const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glass96495d92014-02-26 15:59:24 -0700447{
448 struct gpio_dev_priv *priv;
449
450 /* Must be called on an active device */
Simon Glasse564f052015-03-05 12:25:20 -0700451 priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -0700452 assert(priv);
453
454 *bit_count = priv->gpio_count;
455 return priv->bank_name;
456}
457
Simon Glass6449a502014-10-04 11:29:43 -0600458static const char * const gpio_function[GPIOF_COUNT] = {
459 "input",
460 "output",
461 "unused",
462 "unknown",
463 "func",
464};
465
466int get_function(struct udevice *dev, int offset, bool skip_unused,
467 const char **namep)
468{
Simon Glasse564f052015-03-05 12:25:20 -0700469 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass6449a502014-10-04 11:29:43 -0600470 struct dm_gpio_ops *ops = gpio_get_ops(dev);
471
472 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
473 if (!device_active(dev))
474 return -ENODEV;
475 if (offset < 0 || offset >= uc_priv->gpio_count)
476 return -EINVAL;
477 if (namep)
478 *namep = uc_priv->name[offset];
479 if (skip_unused && !uc_priv->name[offset])
480 return GPIOF_UNUSED;
481 if (ops->get_function) {
482 int ret;
483
484 ret = ops->get_function(dev, offset);
485 if (ret < 0)
486 return ret;
487 if (ret >= ARRAY_SIZE(gpio_function))
488 return -ENODATA;
489 return ret;
490 }
491
492 return GPIOF_UNKNOWN;
493}
494
495int gpio_get_function(struct udevice *dev, int offset, const char **namep)
496{
497 return get_function(dev, offset, true, namep);
498}
499
500int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
501{
502 return get_function(dev, offset, false, namep);
503}
504
Simon Glass07575352014-10-04 11:29:44 -0600505int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
506{
507 struct dm_gpio_ops *ops = gpio_get_ops(dev);
508 struct gpio_dev_priv *priv;
509 char *str = buf;
510 int func;
511 int ret;
512 int len;
513
514 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
515
516 *buf = 0;
Simon Glasse564f052015-03-05 12:25:20 -0700517 priv = dev_get_uclass_priv(dev);
Simon Glass07575352014-10-04 11:29:44 -0600518 ret = gpio_get_raw_function(dev, offset, NULL);
519 if (ret < 0)
520 return ret;
521 func = ret;
522 len = snprintf(str, buffsize, "%s%d: %s",
523 priv->bank_name ? priv->bank_name : "",
524 offset, gpio_function[func]);
525 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
526 func == GPIOF_UNUSED) {
527 const char *label;
528 bool used;
529
530 ret = ops->get_value(dev, offset);
531 if (ret < 0)
532 return ret;
533 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
534 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
535 ret,
536 used ? 'x' : ' ',
537 used ? " " : "",
538 label ? label : "");
539 }
540
541 return 0;
542}
543
Simon Glass962f5ca2015-04-14 21:03:20 -0600544int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
545{
546 int i, ret;
547 int gpio;
548
549 for (i = 0; i < 32; i++) {
550 gpio = gpio_num_array[i];
551 if (gpio == -1)
552 break;
553 ret = gpio_requestf(gpio, fmt, i);
554 if (ret)
555 goto err;
556 ret = gpio_direction_input(gpio);
557 if (ret) {
558 gpio_free(gpio);
559 goto err;
560 }
561 }
562
563 return 0;
564err:
565 for (i--; i >= 0; i--)
566 gpio_free(gpio_num_array[i]);
567
568 return ret;
569}
570
Simon Glasse5901c92014-11-10 18:00:21 -0700571/*
572 * get a number comprised of multiple GPIO values. gpio_num_array points to
573 * the array of gpio pin numbers to scan, terminated by -1.
574 */
Simon Glass962f5ca2015-04-14 21:03:20 -0600575int gpio_get_values_as_int(const int *gpio_list)
Simon Glasse5901c92014-11-10 18:00:21 -0700576{
577 int gpio;
578 unsigned bitmask = 1;
579 unsigned vector = 0;
Simon Glass962f5ca2015-04-14 21:03:20 -0600580 int ret;
Simon Glasse5901c92014-11-10 18:00:21 -0700581
582 while (bitmask &&
Simon Glass962f5ca2015-04-14 21:03:20 -0600583 ((gpio = *gpio_list++) != -1)) {
584 ret = gpio_get_value(gpio);
585 if (ret < 0)
586 return ret;
587 else if (ret)
Simon Glasse5901c92014-11-10 18:00:21 -0700588 vector |= bitmask;
589 bitmask <<= 1;
590 }
Simon Glass962f5ca2015-04-14 21:03:20 -0600591
Simon Glasse5901c92014-11-10 18:00:21 -0700592 return vector;
593}
594
Simon Glass17c43f12016-03-06 19:27:51 -0700595int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
Simon Glassbbf24782016-03-06 19:27:50 -0700596{
597 unsigned bitmask = 1;
598 unsigned vector = 0;
599 int ret, i;
600
601 for (i = 0; i < count; i++) {
602 ret = dm_gpio_get_value(&desc_list[i]);
603 if (ret < 0)
604 return ret;
605 else if (ret)
606 vector |= bitmask;
607 bitmask <<= 1;
608 }
609
610 return vector;
611}
612
Simon Glass3669e0e2015-01-05 20:05:29 -0700613static int _gpio_request_by_name_nodev(const void *blob, int node,
614 const char *list_name, int index,
615 struct gpio_desc *desc, int flags,
616 bool add_index)
617{
618 struct fdtdec_phandle_args args;
619 int ret;
620
621 desc->dev = NULL;
622 desc->offset = 0;
Eric Nelson6c880b72016-04-24 16:32:40 -0700623 desc->flags = 0;
Simon Glass3669e0e2015-01-05 20:05:29 -0700624 ret = fdtdec_parse_phandle_with_args(blob, node, list_name,
625 "#gpio-cells", 0, index, &args);
626 if (ret) {
627 debug("%s: fdtdec_parse_phandle_with_args failed\n", __func__);
628 goto err;
629 }
630
631 ret = uclass_get_device_by_of_offset(UCLASS_GPIO, args.node,
632 &desc->dev);
633 if (ret) {
634 debug("%s: uclass_get_device_by_of_offset failed\n", __func__);
635 goto err;
636 }
637 ret = gpio_find_and_xlate(desc, &args);
638 if (ret) {
639 debug("%s: gpio_find_and_xlate failed\n", __func__);
640 goto err;
641 }
642 ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
643 fdt_get_name(blob, node, NULL),
644 list_name, index);
645 if (ret) {
646 debug("%s: dm_gpio_requestf failed\n", __func__);
647 goto err;
648 }
649 ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
650 if (ret) {
651 debug("%s: dm_gpio_set_dir failed\n", __func__);
652 goto err;
653 }
654
655 return 0;
656err:
657 debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
658 __func__, fdt_get_name(blob, node, NULL), list_name, index, ret);
659 return ret;
660}
661
662int gpio_request_by_name_nodev(const void *blob, int node,
663 const char *list_name, int index,
664 struct gpio_desc *desc, int flags)
665{
666 return _gpio_request_by_name_nodev(blob, node, list_name, index, desc,
667 flags, index > 0);
668}
669
670int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
671 struct gpio_desc *desc, int flags)
672{
673 /*
674 * This isn't ideal since we don't use dev->name in the debug()
675 * calls in gpio_request_by_name(), but we can do this until
676 * gpio_request_by_name_nodev() can be dropped.
677 */
678 return gpio_request_by_name_nodev(gd->fdt_blob, dev->of_offset,
679 list_name, index, desc, flags);
680}
681
682int gpio_request_list_by_name_nodev(const void *blob, int node,
683 const char *list_name,
684 struct gpio_desc *desc, int max_count,
685 int flags)
686{
687 int count;
688 int ret;
689
Przemyslaw Marczak2984e7a2015-03-31 18:57:16 +0200690 for (count = 0; count < max_count; count++) {
Simon Glass3669e0e2015-01-05 20:05:29 -0700691 ret = _gpio_request_by_name_nodev(blob, node, list_name, count,
692 &desc[count], flags, true);
693 if (ret == -ENOENT)
694 break;
695 else if (ret)
696 goto err;
697 }
698
699 /* We ran out of GPIOs in the list */
700 return count;
701
702err:
703 gpio_free_list_nodev(desc, count - 1);
704
705 return ret;
706}
707
708int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
709 struct gpio_desc *desc, int max_count,
710 int flags)
711{
712 /*
713 * This isn't ideal since we don't use dev->name in the debug()
714 * calls in gpio_request_by_name(), but we can do this until
715 * gpio_request_list_by_name_nodev() can be dropped.
716 */
717 return gpio_request_list_by_name_nodev(gd->fdt_blob, dev->of_offset,
718 list_name, desc, max_count,
719 flags);
720}
721
722int gpio_get_list_count(struct udevice *dev, const char *list_name)
723{
724 int ret;
725
726 ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev->of_offset,
727 list_name, "#gpio-cells", 0, -1,
728 NULL);
729 if (ret) {
730 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
731 __func__, dev->name, list_name, ret);
732 }
733
734 return ret;
735}
736
737int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
738{
739 /* For now, we don't do any checking of dev */
740 return _dm_gpio_free(desc->dev, desc->offset);
741}
742
743int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
744{
745 int i;
746
747 /* For now, we don't do any checking of dev */
748 for (i = 0; i < count; i++)
749 dm_gpio_free(dev, &desc[i]);
750
751 return 0;
752}
753
754int gpio_free_list_nodev(struct gpio_desc *desc, int count)
755{
756 return gpio_free_list(NULL, desc, count);
757}
758
Simon Glass96495d92014-02-26 15:59:24 -0700759/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glassb892d122014-10-04 11:29:42 -0600760static int gpio_renumber(struct udevice *removed_dev)
Simon Glass96495d92014-02-26 15:59:24 -0700761{
762 struct gpio_dev_priv *uc_priv;
Heiko Schocher54c5d082014-05-22 12:43:05 +0200763 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -0700764 struct uclass *uc;
765 unsigned base;
766 int ret;
767
768 ret = uclass_get(UCLASS_GPIO, &uc);
769 if (ret)
770 return ret;
771
772 /* Ensure that we have a base for each bank */
773 base = 0;
774 uclass_foreach_dev(dev, uc) {
Simon Glassb892d122014-10-04 11:29:42 -0600775 if (device_active(dev) && dev != removed_dev) {
Simon Glasse564f052015-03-05 12:25:20 -0700776 uc_priv = dev_get_uclass_priv(dev);
Simon Glass96495d92014-02-26 15:59:24 -0700777 uc_priv->gpio_base = base;
778 base += uc_priv->gpio_count;
779 }
780 }
781
782 return 0;
783}
784
Simon Glass17c43f12016-03-06 19:27:51 -0700785int gpio_get_number(const struct gpio_desc *desc)
Simon Glass56a71f82015-03-25 12:21:58 -0600786{
787 struct udevice *dev = desc->dev;
788 struct gpio_dev_priv *uc_priv;
789
790 if (!dev)
791 return -1;
792 uc_priv = dev->uclass_priv;
793
794 return uc_priv->gpio_base + desc->offset;
795}
796
Heiko Schocher54c5d082014-05-22 12:43:05 +0200797static int gpio_post_probe(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -0700798{
Simon Glasse564f052015-03-05 12:25:20 -0700799 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -0600800
801 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
802 if (!uc_priv->name)
803 return -ENOMEM;
804
805 return gpio_renumber(NULL);
Simon Glass96495d92014-02-26 15:59:24 -0700806}
807
Heiko Schocher54c5d082014-05-22 12:43:05 +0200808static int gpio_pre_remove(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -0700809{
Simon Glasse564f052015-03-05 12:25:20 -0700810 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glassb892d122014-10-04 11:29:42 -0600811 int i;
812
813 for (i = 0; i < uc_priv->gpio_count; i++) {
814 if (uc_priv->name[i])
815 free(uc_priv->name[i]);
816 }
817 free(uc_priv->name);
818
819 return gpio_renumber(dev);
Simon Glass96495d92014-02-26 15:59:24 -0700820}
821
822UCLASS_DRIVER(gpio) = {
823 .id = UCLASS_GPIO,
824 .name = "gpio",
Bhuvanchandra DVae89bb02015-06-01 18:37:15 +0530825 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass96495d92014-02-26 15:59:24 -0700826 .post_probe = gpio_post_probe,
827 .pre_remove = gpio_pre_remove,
828 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
829};