]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/gpio/gpio-uclass.c
update to 2015.04-rc1
[karo-tx-uboot.git] / drivers / gpio / gpio-uclass.c
1 /*
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>
10 #include <fdtdec.h>
11 #include <malloc.h>
12 #include <asm/gpio.h>
13 #include <linux/ctype.h>
14
15 DECLARE_GLOBAL_DATA_PTR;
16
17 /**
18  * gpio_to_device() - Convert global GPIO number to device, number
19  *
20  * Convert the GPIO number to an entry in the list of GPIOs
21  * or GPIO blocks registered with the GPIO controller. Returns
22  * entry on success, NULL on error.
23  *
24  * @gpio:       The numeric representation of the GPIO
25  * @desc:       Returns description (desc->flags will always be 0)
26  * @return 0 if found, -ENOENT if not found
27  */
28 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
29 {
30         struct gpio_dev_priv *uc_priv;
31         struct udevice *dev;
32         int ret;
33
34         for (ret = uclass_first_device(UCLASS_GPIO, &dev);
35              dev;
36              ret = uclass_next_device(&dev)) {
37                 uc_priv = dev->uclass_priv;
38                 if (gpio >= uc_priv->gpio_base &&
39                     gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
40                         desc->dev = dev;
41                         desc->offset = gpio - uc_priv->gpio_base;
42                         desc->flags = 0;
43                         return 0;
44                 }
45         }
46
47         /* No such GPIO */
48         return ret ? ret : -ENOENT;
49 }
50
51 int gpio_lookup_name(const char *name, struct udevice **devp,
52                      unsigned int *offsetp, unsigned int *gpiop)
53 {
54         struct gpio_dev_priv *uc_priv = NULL;
55         struct udevice *dev;
56         ulong offset;
57         int numeric;
58         int ret;
59
60         if (devp)
61                 *devp = NULL;
62         numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
63         for (ret = uclass_first_device(UCLASS_GPIO, &dev);
64              dev;
65              ret = uclass_next_device(&dev)) {
66                 int len;
67
68                 uc_priv = dev->uclass_priv;
69                 if (numeric != -1) {
70                         offset = numeric - uc_priv->gpio_base;
71                         /* Allow GPIOs to be numbered from 0 */
72                         if (offset >= 0 && offset < uc_priv->gpio_count)
73                                 break;
74                 }
75
76                 len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
77
78                 if (!strncasecmp(name, uc_priv->bank_name, len)) {
79                         if (!strict_strtoul(name + len, 10, &offset))
80                                 break;
81                 }
82         }
83
84         if (!dev)
85                 return ret ? ret : -EINVAL;
86
87         if (devp)
88                 *devp = dev;
89         if (offsetp)
90                 *offsetp = offset;
91         if (gpiop)
92                 *gpiop = uc_priv->gpio_base + offset;
93
94         return 0;
95 }
96
97 static int gpio_find_and_xlate(struct gpio_desc *desc,
98                                struct fdtdec_phandle_args *args)
99 {
100         struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
101
102         /* Use the first argument as the offset by default */
103         if (args->args_count > 0)
104                 desc->offset = args->args[0];
105         else
106                 desc->offset = -1;
107         desc->flags = 0;
108
109         return ops->xlate ? ops->xlate(desc->dev, desc, args) : 0;
110 }
111
112 static int dm_gpio_request(struct gpio_desc *desc, const char *label)
113 {
114         struct udevice *dev = desc->dev;
115         struct gpio_dev_priv *uc_priv;
116         char *str;
117         int ret;
118
119         uc_priv = dev->uclass_priv;
120         if (uc_priv->name[desc->offset])
121                 return -EBUSY;
122         str = strdup(label);
123         if (!str)
124                 return -ENOMEM;
125         if (gpio_get_ops(dev)->request) {
126                 ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
127                 if (ret) {
128                         free(str);
129                         return ret;
130                 }
131         }
132         uc_priv->name[desc->offset] = str;
133
134         return 0;
135 }
136
137 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
138 {
139         va_list args;
140         char buf[40];
141
142         va_start(args, fmt);
143         vscnprintf(buf, sizeof(buf), fmt, args);
144         va_end(args);
145         return dm_gpio_request(desc, buf);
146 }
147
148 /**
149  * gpio_request() - [COMPAT] Request GPIO
150  * gpio:        GPIO number
151  * label:       Name for the requested GPIO
152  *
153  * The label is copied and allocated so the caller does not need to keep
154  * the pointer around.
155  *
156  * This function implements the API that's compatible with current
157  * GPIO API used in U-Boot. The request is forwarded to particular
158  * GPIO driver. Returns 0 on success, negative value on error.
159  */
160 int gpio_request(unsigned gpio, const char *label)
161 {
162         struct gpio_desc desc;
163         int ret;
164
165         ret = gpio_to_device(gpio, &desc);
166         if (ret)
167                 return ret;
168
169         return dm_gpio_request(&desc, label);
170 }
171
172 /**
173  * gpio_requestf() - [COMPAT] Request GPIO
174  * @gpio:       GPIO number
175  * @fmt:        Format string for the requested GPIO
176  * @...:        Arguments for the printf() format string
177  *
178  * This function implements the API that's compatible with current
179  * GPIO API used in U-Boot. The request is forwarded to particular
180  * GPIO driver. Returns 0 on success, negative value on error.
181  */
182 int gpio_requestf(unsigned gpio, const char *fmt, ...)
183 {
184         va_list args;
185         char buf[40];
186
187         va_start(args, fmt);
188         vscnprintf(buf, sizeof(buf), fmt, args);
189         va_end(args);
190         return gpio_request(gpio, buf);
191 }
192
193 int gpio_request_one(unsigned int gpio, enum gpio_flags flags,
194                 const char *label)
195 {
196         int ret;
197
198         ret = gpio_request(gpio, label);
199         if (ret)
200                 return ret;
201
202         if (flags == GPIOFLAG_INPUT)
203                 gpio_direction_input(gpio);
204         else if (flags == GPIOFLAG_OUTPUT_INIT_LOW)
205                 gpio_direction_output(gpio, 0);
206         else if (flags == GPIOFLAG_OUTPUT_INIT_HIGH)
207                 gpio_direction_output(gpio, 1);
208
209         return ret;
210 }
211
212 int gpio_request_array(const struct gpio *gpios, int count)
213 {
214         int ret;
215         int i;
216
217         for (i = 0; i < count; i++) {
218                 ret = gpio_request_one(gpios[i].gpio, gpios[i].flags,
219                                 gpios[i].label);
220                 if (ret) {
221                         printf("Failed to request GPIO%d (%u of %u): %d\n",
222                                 gpios[i].gpio, i, count, ret);
223                         goto error;
224                 }
225         }
226         return 0;
227
228 error:
229         while (--i >= 0)
230                 gpio_free(gpios[i].gpio);
231
232         return ret;
233 }
234
235 int _dm_gpio_free(struct udevice *dev, uint offset)
236 {
237         struct gpio_dev_priv *uc_priv;
238         int ret;
239
240         uc_priv = dev->uclass_priv;
241         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
255 int gpio_free_array(const struct gpio *gpios, int count)
256 {
257         int ret = 0;
258         int i;
259
260         for (i = 0; i < count; i++)
261                 ret |= gpio_free(gpios[i].gpio);
262
263         return ret;
264 }
265
266 /**
267  * gpio_free() - [COMPAT] Relinquish GPIO
268  * gpio:        GPIO number
269  *
270  * This function implements the API that's compatible with current
271  * GPIO API used in U-Boot. The request is forwarded to particular
272  * GPIO driver. Returns 0 on success, negative value on error.
273  */
274 int gpio_free(unsigned gpio)
275 {
276         struct gpio_desc desc;
277         int ret;
278
279         ret = gpio_to_device(gpio, &desc);
280         if (ret)
281                 return ret;
282
283         return _dm_gpio_free(desc.dev, desc.offset);
284 }
285
286 static int check_reserved(struct gpio_desc *desc, const char *func)
287 {
288         struct gpio_dev_priv *uc_priv = desc->dev->uclass_priv;
289
290         if (!uc_priv->name[desc->offset]) {
291                 printf("%s: %s: error: gpio %s%d not reserved\n",
292                        desc->dev->name, func,
293                        uc_priv->bank_name ? uc_priv->bank_name : "",
294                        desc->offset);
295                 return -EBUSY;
296         }
297
298         return 0;
299 }
300
301 /**
302  * gpio_direction_input() - [COMPAT] Set GPIO direction to input
303  * gpio:        GPIO number
304  *
305  * This function implements the API that's compatible with current
306  * GPIO API used in U-Boot. The request is forwarded to particular
307  * GPIO driver. Returns 0 on success, negative value on error.
308  */
309 int gpio_direction_input(unsigned gpio)
310 {
311         struct gpio_desc desc;
312         int ret;
313
314         ret = gpio_to_device(gpio, &desc);
315         if (ret)
316                 return ret;
317         ret = check_reserved(&desc, "dir_input");
318         if (ret)
319                 return ret;
320
321         return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
322 }
323
324 /**
325  * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
326  * gpio:        GPIO number
327  * value:       Logical value to be set on the GPIO pin
328  *
329  * This function implements the API that's compatible with current
330  * GPIO API used in U-Boot. The request is forwarded to particular
331  * GPIO driver. Returns 0 on success, negative value on error.
332  */
333 int gpio_direction_output(unsigned gpio, int value)
334 {
335         struct gpio_desc desc;
336         int ret;
337
338         ret = gpio_to_device(gpio, &desc);
339         if (ret)
340                 return ret;
341         ret = check_reserved(&desc, "dir_output");
342         if (ret)
343                 return ret;
344
345         return gpio_get_ops(desc.dev)->direction_output(desc.dev,
346                                                         desc.offset, value);
347 }
348
349 int dm_gpio_get_value(struct gpio_desc *desc)
350 {
351         int value;
352         int ret;
353
354         ret = check_reserved(desc, "get_value");
355         if (ret)
356                 return ret;
357
358         value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
359
360         return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
361 }
362
363 int dm_gpio_set_value(struct gpio_desc *desc, int value)
364 {
365         int ret;
366
367         ret = check_reserved(desc, "set_value");
368         if (ret)
369                 return ret;
370
371         if (desc->flags & GPIOD_ACTIVE_LOW)
372                 value = !value;
373         gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
374         return 0;
375 }
376
377 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
378 {
379         struct udevice *dev = desc->dev;
380         struct dm_gpio_ops *ops = gpio_get_ops(dev);
381         int ret;
382
383         ret = check_reserved(desc, "set_dir");
384         if (ret)
385                 return ret;
386
387         if (flags & GPIOD_IS_OUT) {
388                 int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
389
390                 if (flags & GPIOD_ACTIVE_LOW)
391                         value = !value;
392                 ret = ops->direction_output(dev, desc->offset, value);
393         } else  if (flags & GPIOD_IS_IN) {
394                 ret = ops->direction_input(dev, desc->offset);
395         }
396         if (ret)
397                 return ret;
398         /*
399          * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
400          * futures
401          */
402         desc->flags = flags;
403
404         return 0;
405 }
406
407 int dm_gpio_set_dir(struct gpio_desc *desc)
408 {
409         return dm_gpio_set_dir_flags(desc, desc->flags);
410 }
411
412 /**
413  * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
414  * gpio:        GPIO number
415  *
416  * This function implements the API that's compatible with current
417  * GPIO API used in U-Boot. The request is forwarded to particular
418  * GPIO driver. Returns the value of the GPIO pin, or negative value
419  * on error.
420  */
421 int gpio_get_value(unsigned gpio)
422 {
423         int ret;
424
425         struct gpio_desc desc;
426
427         ret = gpio_to_device(gpio, &desc);
428         if (ret)
429                 return ret;
430         return dm_gpio_get_value(&desc);
431 }
432
433 /**
434  * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
435  * gpio:        GPIO number
436  * value:       Logical value to be set on the GPIO pin.
437  *
438  * This function implements the API that's compatible with current
439  * GPIO API used in U-Boot. The request is forwarded to particular
440  * GPIO driver. Returns 0 on success, negative value on error.
441  */
442 int gpio_set_value(unsigned gpio, int value)
443 {
444         struct gpio_desc desc;
445         int ret;
446
447         ret = gpio_to_device(gpio, &desc);
448         if (ret)
449                 return ret;
450         return dm_gpio_set_value(&desc, value);
451 }
452
453 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
454 {
455         struct gpio_dev_priv *priv;
456
457         /* Must be called on an active device */
458         priv = dev->uclass_priv;
459         assert(priv);
460
461         *bit_count = priv->gpio_count;
462         return priv->bank_name;
463 }
464
465 static const char * const gpio_function[GPIOF_COUNT] = {
466         "input",
467         "output",
468         "unused",
469         "unknown",
470         "func",
471 };
472
473 int get_function(struct udevice *dev, int offset, bool skip_unused,
474                  const char **namep)
475 {
476         struct gpio_dev_priv *uc_priv = dev->uclass_priv;
477         struct dm_gpio_ops *ops = gpio_get_ops(dev);
478
479         BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
480         if (!device_active(dev))
481                 return -ENODEV;
482         if (offset < 0 || offset >= uc_priv->gpio_count)
483                 return -EINVAL;
484         if (namep)
485                 *namep = uc_priv->name[offset];
486         if (skip_unused && !uc_priv->name[offset])
487                 return GPIOF_UNUSED;
488         if (ops->get_function) {
489                 int ret;
490
491                 ret = ops->get_function(dev, offset);
492                 if (ret < 0)
493                         return ret;
494                 if (ret >= ARRAY_SIZE(gpio_function))
495                         return -ENODATA;
496                 return ret;
497         }
498
499         return GPIOF_UNKNOWN;
500 }
501
502 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
503 {
504         return get_function(dev, offset, true, namep);
505 }
506
507 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
508 {
509         return get_function(dev, offset, false, namep);
510 }
511
512 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
513 {
514         struct dm_gpio_ops *ops = gpio_get_ops(dev);
515         struct gpio_dev_priv *priv;
516         char *str = buf;
517         int func;
518         int ret;
519         int len;
520
521         BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
522
523         *buf = 0;
524         priv = dev->uclass_priv;
525         ret = gpio_get_raw_function(dev, offset, NULL);
526         if (ret < 0)
527                 return ret;
528         func = ret;
529         len = snprintf(str, buffsize, "%s%d: %s",
530                        priv->bank_name ? priv->bank_name : "",
531                        offset, gpio_function[func]);
532         if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
533             func == GPIOF_UNUSED) {
534                 const char *label;
535                 bool used;
536
537                 ret = ops->get_value(dev, offset);
538                 if (ret < 0)
539                         return ret;
540                 used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
541                 snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
542                          ret,
543                          used ? 'x' : ' ',
544                          used ? " " : "",
545                          label ? label : "");
546         }
547
548         return 0;
549 }
550
551 /*
552  * get a number comprised of multiple GPIO values. gpio_num_array points to
553  * the array of gpio pin numbers to scan, terminated by -1.
554  */
555 unsigned gpio_get_values_as_int(const int *gpio_num_array)
556 {
557         int gpio;
558         unsigned bitmask = 1;
559         unsigned vector = 0;
560
561         while (bitmask &&
562                ((gpio = *gpio_num_array++) != -1)) {
563                 if (gpio_get_value(gpio))
564                         vector |= bitmask;
565                 bitmask <<= 1;
566         }
567         return vector;
568 }
569
570 static int _gpio_request_by_name_nodev(const void *blob, int node,
571                                        const char *list_name, int index,
572                                        struct gpio_desc *desc, int flags,
573                                        bool add_index)
574 {
575         struct fdtdec_phandle_args args;
576         int ret;
577
578         desc->dev = NULL;
579         desc->offset = 0;
580         ret = fdtdec_parse_phandle_with_args(blob, node, list_name,
581                                              "#gpio-cells", 0, index, &args);
582         if (ret) {
583                 debug("%s: fdtdec_parse_phandle_with_args failed\n", __func__);
584                 goto err;
585         }
586
587         ret = uclass_get_device_by_of_offset(UCLASS_GPIO, args.node,
588                                              &desc->dev);
589         if (ret) {
590                 debug("%s: uclass_get_device_by_of_offset failed\n", __func__);
591                 goto err;
592         }
593         ret = gpio_find_and_xlate(desc, &args);
594         if (ret) {
595                 debug("%s: gpio_find_and_xlate failed\n", __func__);
596                 goto err;
597         }
598         ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
599                                fdt_get_name(blob, node, NULL),
600                                list_name, index);
601         if (ret) {
602                 debug("%s: dm_gpio_requestf failed\n", __func__);
603                 goto err;
604         }
605         ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
606         if (ret) {
607                 debug("%s: dm_gpio_set_dir failed\n", __func__);
608                 goto err;
609         }
610
611         return 0;
612 err:
613         debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
614               __func__, fdt_get_name(blob, node, NULL), list_name, index, ret);
615         return ret;
616 }
617
618 int gpio_request_by_name_nodev(const void *blob, int node,
619                                const char *list_name, int index,
620                                struct gpio_desc *desc, int flags)
621 {
622         return _gpio_request_by_name_nodev(blob, node, list_name, index, desc,
623                                            flags, index > 0);
624 }
625
626 int gpio_request_by_name(struct udevice *dev,  const char *list_name, int index,
627                          struct gpio_desc *desc, int flags)
628 {
629         /*
630          * This isn't ideal since we don't use dev->name in the debug()
631          * calls in gpio_request_by_name(), but we can do this until
632          * gpio_request_by_name_nodev() can be dropped.
633          */
634         return gpio_request_by_name_nodev(gd->fdt_blob, dev->of_offset,
635                                           list_name, index, desc, flags);
636 }
637
638 int gpio_request_list_by_name_nodev(const void *blob, int node,
639                                     const char *list_name,
640                                     struct gpio_desc *desc, int max_count,
641                                     int flags)
642 {
643         int count;
644         int ret;
645
646         for (count = 0; ; count++) {
647                 if (count >= max_count) {
648                         ret = -ENOSPC;
649                         goto err;
650                 }
651                 ret = _gpio_request_by_name_nodev(blob, node, list_name, count,
652                                                   &desc[count], flags, true);
653                 if (ret == -ENOENT)
654                         break;
655                 else if (ret)
656                         goto err;
657         }
658
659         /* We ran out of GPIOs in the list */
660         return count;
661
662 err:
663         gpio_free_list_nodev(desc, count - 1);
664
665         return ret;
666 }
667
668 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
669                               struct gpio_desc *desc, int max_count,
670                               int flags)
671 {
672         /*
673          * This isn't ideal since we don't use dev->name in the debug()
674          * calls in gpio_request_by_name(), but we can do this until
675          * gpio_request_list_by_name_nodev() can be dropped.
676          */
677         return gpio_request_list_by_name_nodev(gd->fdt_blob, dev->of_offset,
678                                                list_name, desc, max_count,
679                                                flags);
680 }
681
682 int gpio_get_list_count(struct udevice *dev, const char *list_name)
683 {
684         int ret;
685
686         ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev->of_offset,
687                                              list_name, "#gpio-cells", 0, -1,
688                                              NULL);
689         if (ret) {
690                 debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
691                       __func__, dev->name, list_name, ret);
692         }
693
694         return ret;
695 }
696
697 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
698 {
699         /* For now, we don't do any checking of dev */
700         return _dm_gpio_free(desc->dev, desc->offset);
701 }
702
703 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
704 {
705         int i;
706
707         /* For now, we don't do any checking of dev */
708         for (i = 0; i < count; i++)
709                 dm_gpio_free(dev, &desc[i]);
710
711         return 0;
712 }
713
714 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
715 {
716         return gpio_free_list(NULL, desc, count);
717 }
718
719 /* We need to renumber the GPIOs when any driver is probed/removed */
720 static int gpio_renumber(struct udevice *removed_dev)
721 {
722         struct gpio_dev_priv *uc_priv;
723         struct udevice *dev;
724         struct uclass *uc;
725         unsigned base;
726         int ret;
727
728         ret = uclass_get(UCLASS_GPIO, &uc);
729         if (ret)
730                 return ret;
731
732         /* Ensure that we have a base for each bank */
733         base = 0;
734         uclass_foreach_dev(dev, uc) {
735                 if (device_active(dev) && dev != removed_dev) {
736                         uc_priv = dev->uclass_priv;
737                         uc_priv->gpio_base = base;
738                         base += uc_priv->gpio_count;
739                 }
740         }
741
742         return 0;
743 }
744
745 static int gpio_post_probe(struct udevice *dev)
746 {
747         struct gpio_dev_priv *uc_priv = dev->uclass_priv;
748
749         uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
750         if (!uc_priv->name)
751                 return -ENOMEM;
752
753         return gpio_renumber(NULL);
754 }
755
756 static int gpio_pre_remove(struct udevice *dev)
757 {
758         struct gpio_dev_priv *uc_priv = dev->uclass_priv;
759         int i;
760
761         for (i = 0; i < uc_priv->gpio_count; i++) {
762                 if (uc_priv->name[i])
763                         free(uc_priv->name[i]);
764         }
765         free(uc_priv->name);
766
767         return gpio_renumber(dev);
768 }
769
770 UCLASS_DRIVER(gpio) = {
771         .id             = UCLASS_GPIO,
772         .name           = "gpio",
773         .post_probe     = gpio_post_probe,
774         .pre_remove     = gpio_pre_remove,
775         .per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
776 };