blob: 2f3c36b48ff652d4f81a169a67f97c6a45acfab5 [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>
9#include <errno.h>
Simon Glassb892d122014-10-04 11:29:42 -060010#include <malloc.h>
Simon Glass96495d92014-02-26 15:59:24 -070011#include <asm/gpio.h>
Simon Glassfe1ef502014-10-22 21:37:01 -060012#include <linux/ctype.h>
Simon Glass96495d92014-02-26 15:59:24 -070013
14/**
15 * gpio_to_device() - Convert global GPIO number to device, number
Simon Glass96495d92014-02-26 15:59:24 -070016 *
17 * Convert the GPIO number to an entry in the list of GPIOs
18 * or GPIO blocks registered with the GPIO controller. Returns
19 * entry on success, NULL on error.
Simon Glassae7123f2015-01-05 20:05:27 -070020 *
21 * @gpio: The numeric representation of the GPIO
22 * @desc: Returns description (desc->flags will always be 0)
23 * @return 0 if found, -ENOENT if not found
Simon Glass96495d92014-02-26 15:59:24 -070024 */
Simon Glassae7123f2015-01-05 20:05:27 -070025static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
Simon Glass96495d92014-02-26 15:59:24 -070026{
27 struct gpio_dev_priv *uc_priv;
Heiko Schocher54c5d082014-05-22 12:43:05 +020028 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -070029 int ret;
30
31 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
32 dev;
33 ret = uclass_next_device(&dev)) {
34 uc_priv = dev->uclass_priv;
35 if (gpio >= uc_priv->gpio_base &&
36 gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
Simon Glassae7123f2015-01-05 20:05:27 -070037 desc->dev = dev;
38 desc->offset = gpio - uc_priv->gpio_base;
39 desc->flags = 0;
Simon Glass96495d92014-02-26 15:59:24 -070040 return 0;
41 }
42 }
43
44 /* No such GPIO */
Simon Glassae7123f2015-01-05 20:05:27 -070045 return ret ? ret : -ENOENT;
Simon Glass96495d92014-02-26 15:59:24 -070046}
47
Heiko Schocher54c5d082014-05-22 12:43:05 +020048int gpio_lookup_name(const char *name, struct udevice **devp,
Simon Glass96495d92014-02-26 15:59:24 -070049 unsigned int *offsetp, unsigned int *gpiop)
50{
Simon Glassfe1ef502014-10-22 21:37:01 -060051 struct gpio_dev_priv *uc_priv = NULL;
Heiko Schocher54c5d082014-05-22 12:43:05 +020052 struct udevice *dev;
Simon Glassfe1ef502014-10-22 21:37:01 -060053 ulong offset;
54 int numeric;
Simon Glass96495d92014-02-26 15:59:24 -070055 int ret;
56
57 if (devp)
58 *devp = NULL;
Simon Glassfe1ef502014-10-22 21:37:01 -060059 numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
Simon Glass96495d92014-02-26 15:59:24 -070060 for (ret = uclass_first_device(UCLASS_GPIO, &dev);
61 dev;
62 ret = uclass_next_device(&dev)) {
Simon Glass96495d92014-02-26 15:59:24 -070063 int len;
64
65 uc_priv = dev->uclass_priv;
Simon Glassfe1ef502014-10-22 21:37:01 -060066 if (numeric != -1) {
67 offset = numeric - uc_priv->gpio_base;
68 /* Allow GPIOs to be numbered from 0 */
69 if (offset >= 0 && offset < uc_priv->gpio_count)
70 break;
71 }
72
Simon Glass96495d92014-02-26 15:59:24 -070073 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
74
Simon Glass939cda52014-06-11 23:29:47 -060075 if (!strncasecmp(name, uc_priv->bank_name, len)) {
Simon Glassfe1ef502014-10-22 21:37:01 -060076 if (!strict_strtoul(name + len, 10, &offset))
77 break;
Simon Glass96495d92014-02-26 15:59:24 -070078 }
79 }
80
Simon Glassfe1ef502014-10-22 21:37:01 -060081 if (!dev)
82 return ret ? ret : -EINVAL;
83
84 if (devp)
85 *devp = dev;
86 if (offsetp)
87 *offsetp = offset;
88 if (gpiop)
89 *gpiop = uc_priv->gpio_base + offset;
90
91 return 0;
Simon Glass96495d92014-02-26 15:59:24 -070092}
93
Simon Glassae7123f2015-01-05 20:05:27 -070094static int dm_gpio_request(struct gpio_desc *desc, const char *label)
95{
96 struct udevice *dev = desc->dev;
97 struct gpio_dev_priv *uc_priv;
98 char *str;
99 int ret;
100
101 uc_priv = dev->uclass_priv;
102 if (uc_priv->name[desc->offset])
103 return -EBUSY;
104 str = strdup(label);
105 if (!str)
106 return -ENOMEM;
107 if (gpio_get_ops(dev)->request) {
108 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
109 if (ret) {
110 free(str);
111 return ret;
112 }
113 }
114 uc_priv->name[desc->offset] = str;
115
116 return 0;
117}
118
Simon Glass96495d92014-02-26 15:59:24 -0700119/**
120 * gpio_request() - [COMPAT] Request GPIO
121 * gpio: GPIO number
122 * label: Name for the requested GPIO
123 *
Simon Glassb892d122014-10-04 11:29:42 -0600124 * The label is copied and allocated so the caller does not need to keep
125 * the pointer around.
126 *
Simon Glass96495d92014-02-26 15:59:24 -0700127 * This function implements the API that's compatible with current
128 * GPIO API used in U-Boot. The request is forwarded to particular
129 * GPIO driver. Returns 0 on success, negative value on error.
130 */
131int gpio_request(unsigned gpio, const char *label)
132{
Simon Glassae7123f2015-01-05 20:05:27 -0700133 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700134 int ret;
135
Simon Glassae7123f2015-01-05 20:05:27 -0700136 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700137 if (ret)
138 return ret;
139
Simon Glassae7123f2015-01-05 20:05:27 -0700140 return dm_gpio_request(&desc, label);
Simon Glass96495d92014-02-26 15:59:24 -0700141}
142
143/**
Simon Glassd44f5972014-10-04 11:29:49 -0600144 * gpio_requestf() - [COMPAT] Request GPIO
145 * @gpio: GPIO number
146 * @fmt: Format string for the requested GPIO
147 * @...: Arguments for the printf() format string
148 *
149 * This function implements the API that's compatible with current
150 * GPIO API used in U-Boot. The request is forwarded to particular
151 * GPIO driver. Returns 0 on success, negative value on error.
152 */
153int gpio_requestf(unsigned gpio, const char *fmt, ...)
154{
155 va_list args;
156 char buf[40];
157
158 va_start(args, fmt);
159 vscnprintf(buf, sizeof(buf), fmt, args);
160 va_end(args);
161 return gpio_request(gpio, buf);
162}
163
Simon Glassae7123f2015-01-05 20:05:27 -0700164int _dm_gpio_free(struct udevice *dev, uint offset)
Simon Glass96495d92014-02-26 15:59:24 -0700165{
Simon Glassb892d122014-10-04 11:29:42 -0600166 struct gpio_dev_priv *uc_priv;
Simon Glass96495d92014-02-26 15:59:24 -0700167 int ret;
168
Simon Glassb892d122014-10-04 11:29:42 -0600169 uc_priv = dev->uclass_priv;
170 if (!uc_priv->name[offset])
171 return -ENXIO;
172 if (gpio_get_ops(dev)->free) {
173 ret = gpio_get_ops(dev)->free(dev, offset);
174 if (ret)
175 return ret;
176 }
177
178 free(uc_priv->name[offset]);
179 uc_priv->name[offset] = NULL;
180
181 return 0;
182}
183
Simon Glassae7123f2015-01-05 20:05:27 -0700184/**
185 * gpio_free() - [COMPAT] Relinquish GPIO
186 * gpio: GPIO number
187 *
188 * This function implements the API that's compatible with current
189 * GPIO API used in U-Boot. The request is forwarded to particular
190 * GPIO driver. Returns 0 on success, negative value on error.
191 */
192int gpio_free(unsigned gpio)
Simon Glassb892d122014-10-04 11:29:42 -0600193{
Simon Glassae7123f2015-01-05 20:05:27 -0700194 struct gpio_desc desc;
195 int ret;
Simon Glassb892d122014-10-04 11:29:42 -0600196
Simon Glassae7123f2015-01-05 20:05:27 -0700197 ret = gpio_to_device(gpio, &desc);
198 if (ret)
199 return ret;
200
201 return _dm_gpio_free(desc.dev, desc.offset);
202}
203
204static int check_reserved(struct gpio_desc *desc, const char *func)
205{
206 struct gpio_dev_priv *uc_priv = desc->dev->uclass_priv;
207
208 if (!uc_priv->name[desc->offset]) {
Simon Glassb892d122014-10-04 11:29:42 -0600209 printf("%s: %s: error: gpio %s%d not reserved\n",
Simon Glassae7123f2015-01-05 20:05:27 -0700210 desc->dev->name, func,
211 uc_priv->bank_name ? uc_priv->bank_name : "",
212 desc->offset);
Simon Glassb892d122014-10-04 11:29:42 -0600213 return -EBUSY;
214 }
215
216 return 0;
Simon Glass96495d92014-02-26 15:59:24 -0700217}
218
219/**
220 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
221 * gpio: GPIO number
222 *
223 * This function implements the API that's compatible with current
224 * GPIO API used in U-Boot. The request is forwarded to particular
225 * GPIO driver. Returns 0 on success, negative value on error.
226 */
227int gpio_direction_input(unsigned gpio)
228{
Simon Glassae7123f2015-01-05 20:05:27 -0700229 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700230 int ret;
231
Simon Glassae7123f2015-01-05 20:05:27 -0700232 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700233 if (ret)
234 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700235 ret = check_reserved(&desc, "dir_input");
236 if (ret)
237 return ret;
Simon Glass96495d92014-02-26 15:59:24 -0700238
Simon Glassae7123f2015-01-05 20:05:27 -0700239 return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
Simon Glass96495d92014-02-26 15:59:24 -0700240}
241
242/**
243 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
244 * gpio: GPIO number
245 * value: Logical value to be set on the GPIO pin
246 *
247 * This function implements the API that's compatible with current
248 * GPIO API used in U-Boot. The request is forwarded to particular
249 * GPIO driver. Returns 0 on success, negative value on error.
250 */
251int gpio_direction_output(unsigned gpio, int value)
252{
Simon Glassae7123f2015-01-05 20:05:27 -0700253 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700254 int ret;
255
Simon Glassae7123f2015-01-05 20:05:27 -0700256 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700257 if (ret)
258 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700259 ret = check_reserved(&desc, "dir_output");
260 if (ret)
261 return ret;
Simon Glass96495d92014-02-26 15:59:24 -0700262
Simon Glassae7123f2015-01-05 20:05:27 -0700263 return gpio_get_ops(desc.dev)->direction_output(desc.dev,
264 desc.offset, value);
265}
266
267int dm_gpio_get_value(struct gpio_desc *desc)
268{
269 int value;
270 int ret;
271
272 ret = check_reserved(desc, "get_value");
273 if (ret)
274 return ret;
275
276 value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
277
278 return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
279}
280
281int dm_gpio_set_value(struct gpio_desc *desc, int value)
282{
283 int ret;
284
285 ret = check_reserved(desc, "set_value");
286 if (ret)
287 return ret;
288
289 if (desc->flags & GPIOD_ACTIVE_LOW)
290 value = !value;
291 gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
292 return 0;
293}
294
295int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
296{
297 struct udevice *dev = desc->dev;
298 struct dm_gpio_ops *ops = gpio_get_ops(dev);
299 int ret;
300
301 ret = check_reserved(desc, "set_dir");
302 if (ret)
303 return ret;
304
305 if (flags & GPIOD_IS_OUT) {
306 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
307
308 if (flags & GPIOD_ACTIVE_LOW)
309 value = !value;
310 ret = ops->direction_output(dev, desc->offset, value);
311 } else if (flags & GPIOD_IS_IN) {
312 ret = ops->direction_input(dev, desc->offset);
313 }
314 if (ret)
315 return ret;
316 /*
317 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
318 * futures
319 */
320 desc->flags = flags;
321
322 return 0;
323}
324
325int dm_gpio_set_dir(struct gpio_desc *desc)
326{
327 return dm_gpio_set_dir_flags(desc, desc->flags);
Simon Glass96495d92014-02-26 15:59:24 -0700328}
329
330/**
331 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
332 * gpio: GPIO number
333 *
334 * This function implements the API that's compatible with current
335 * GPIO API used in U-Boot. The request is forwarded to particular
336 * GPIO driver. Returns the value of the GPIO pin, or negative value
337 * on error.
338 */
339int gpio_get_value(unsigned gpio)
340{
Simon Glass96495d92014-02-26 15:59:24 -0700341 int ret;
342
Simon Glassae7123f2015-01-05 20:05:27 -0700343 struct gpio_desc desc;
344
345 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700346 if (ret)
347 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700348 return dm_gpio_get_value(&desc);
Simon Glass96495d92014-02-26 15:59:24 -0700349}
350
351/**
352 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
353 * gpio: GPIO number
354 * value: Logical value to be set on the GPIO pin.
355 *
356 * This function implements the API that's compatible with current
357 * GPIO API used in U-Boot. The request is forwarded to particular
358 * GPIO driver. Returns 0 on success, negative value on error.
359 */
360int gpio_set_value(unsigned gpio, int value)
361{
Simon Glassae7123f2015-01-05 20:05:27 -0700362 struct gpio_desc desc;
Simon Glass96495d92014-02-26 15:59:24 -0700363 int ret;
364
Simon Glassae7123f2015-01-05 20:05:27 -0700365 ret = gpio_to_device(gpio, &desc);
Simon Glass96495d92014-02-26 15:59:24 -0700366 if (ret)
367 return ret;
Simon Glassae7123f2015-01-05 20:05:27 -0700368 return dm_gpio_set_value(&desc, value);
Simon Glass96495d92014-02-26 15:59:24 -0700369}
370
Heiko Schocher54c5d082014-05-22 12:43:05 +0200371const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
Simon Glass96495d92014-02-26 15:59:24 -0700372{
373 struct gpio_dev_priv *priv;
374
375 /* Must be called on an active device */
376 priv = dev->uclass_priv;
377 assert(priv);
378
379 *bit_count = priv->gpio_count;
380 return priv->bank_name;
381}
382
Simon Glass6449a502014-10-04 11:29:43 -0600383static const char * const gpio_function[GPIOF_COUNT] = {
384 "input",
385 "output",
386 "unused",
387 "unknown",
388 "func",
389};
390
391int get_function(struct udevice *dev, int offset, bool skip_unused,
392 const char **namep)
393{
394 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
395 struct dm_gpio_ops *ops = gpio_get_ops(dev);
396
397 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
398 if (!device_active(dev))
399 return -ENODEV;
400 if (offset < 0 || offset >= uc_priv->gpio_count)
401 return -EINVAL;
402 if (namep)
403 *namep = uc_priv->name[offset];
404 if (skip_unused && !uc_priv->name[offset])
405 return GPIOF_UNUSED;
406 if (ops->get_function) {
407 int ret;
408
409 ret = ops->get_function(dev, offset);
410 if (ret < 0)
411 return ret;
412 if (ret >= ARRAY_SIZE(gpio_function))
413 return -ENODATA;
414 return ret;
415 }
416
417 return GPIOF_UNKNOWN;
418}
419
420int gpio_get_function(struct udevice *dev, int offset, const char **namep)
421{
422 return get_function(dev, offset, true, namep);
423}
424
425int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
426{
427 return get_function(dev, offset, false, namep);
428}
429
Simon Glass07575352014-10-04 11:29:44 -0600430int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
431{
432 struct dm_gpio_ops *ops = gpio_get_ops(dev);
433 struct gpio_dev_priv *priv;
434 char *str = buf;
435 int func;
436 int ret;
437 int len;
438
439 BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
440
441 *buf = 0;
442 priv = dev->uclass_priv;
443 ret = gpio_get_raw_function(dev, offset, NULL);
444 if (ret < 0)
445 return ret;
446 func = ret;
447 len = snprintf(str, buffsize, "%s%d: %s",
448 priv->bank_name ? priv->bank_name : "",
449 offset, gpio_function[func]);
450 if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
451 func == GPIOF_UNUSED) {
452 const char *label;
453 bool used;
454
455 ret = ops->get_value(dev, offset);
456 if (ret < 0)
457 return ret;
458 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
459 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
460 ret,
461 used ? 'x' : ' ',
462 used ? " " : "",
463 label ? label : "");
464 }
465
466 return 0;
467}
468
Simon Glasse5901c92014-11-10 18:00:21 -0700469/*
470 * get a number comprised of multiple GPIO values. gpio_num_array points to
471 * the array of gpio pin numbers to scan, terminated by -1.
472 */
473unsigned gpio_get_values_as_int(const int *gpio_num_array)
474{
475 int gpio;
476 unsigned bitmask = 1;
477 unsigned vector = 0;
478
479 while (bitmask &&
480 ((gpio = *gpio_num_array++) != -1)) {
481 if (gpio_get_value(gpio))
482 vector |= bitmask;
483 bitmask <<= 1;
484 }
485 return vector;
486}
487
Simon Glass96495d92014-02-26 15:59:24 -0700488/* We need to renumber the GPIOs when any driver is probed/removed */
Simon Glassb892d122014-10-04 11:29:42 -0600489static int gpio_renumber(struct udevice *removed_dev)
Simon Glass96495d92014-02-26 15:59:24 -0700490{
491 struct gpio_dev_priv *uc_priv;
Heiko Schocher54c5d082014-05-22 12:43:05 +0200492 struct udevice *dev;
Simon Glass96495d92014-02-26 15:59:24 -0700493 struct uclass *uc;
494 unsigned base;
495 int ret;
496
497 ret = uclass_get(UCLASS_GPIO, &uc);
498 if (ret)
499 return ret;
500
501 /* Ensure that we have a base for each bank */
502 base = 0;
503 uclass_foreach_dev(dev, uc) {
Simon Glassb892d122014-10-04 11:29:42 -0600504 if (device_active(dev) && dev != removed_dev) {
Simon Glass96495d92014-02-26 15:59:24 -0700505 uc_priv = dev->uclass_priv;
506 uc_priv->gpio_base = base;
507 base += uc_priv->gpio_count;
508 }
509 }
510
511 return 0;
512}
513
Heiko Schocher54c5d082014-05-22 12:43:05 +0200514static int gpio_post_probe(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -0700515{
Simon Glassb892d122014-10-04 11:29:42 -0600516 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
517
518 uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
519 if (!uc_priv->name)
520 return -ENOMEM;
521
522 return gpio_renumber(NULL);
Simon Glass96495d92014-02-26 15:59:24 -0700523}
524
Heiko Schocher54c5d082014-05-22 12:43:05 +0200525static int gpio_pre_remove(struct udevice *dev)
Simon Glass96495d92014-02-26 15:59:24 -0700526{
Simon Glassb892d122014-10-04 11:29:42 -0600527 struct gpio_dev_priv *uc_priv = dev->uclass_priv;
528 int i;
529
530 for (i = 0; i < uc_priv->gpio_count; i++) {
531 if (uc_priv->name[i])
532 free(uc_priv->name[i]);
533 }
534 free(uc_priv->name);
535
536 return gpio_renumber(dev);
Simon Glass96495d92014-02-26 15:59:24 -0700537}
538
539UCLASS_DRIVER(gpio) = {
540 .id = UCLASS_GPIO,
541 .name = "gpio",
542 .post_probe = gpio_post_probe,
543 .pre_remove = gpio_pre_remove,
544 .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
545};