gpio.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * OF helpers for the GPIO API
  3. *
  4. * Copyright (c) 2007-2008 MontaVista Software, Inc.
  5. *
  6. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/io.h>
  16. #include <linux/of.h>
  17. #include <linux/of_gpio.h>
  18. #include <asm/prom.h>
  19. /**
  20. * of_get_gpio - Get a GPIO number from the device tree to use with GPIO API
  21. * @np: device node to get GPIO from
  22. * @index: index of the GPIO
  23. *
  24. * Returns GPIO number to use with Linux generic GPIO API, or one of the errno
  25. * value on the error condition.
  26. */
  27. int of_get_gpio(struct device_node *np, int index)
  28. {
  29. int ret;
  30. struct device_node *gc;
  31. struct of_gpio_chip *of_gc = NULL;
  32. int size;
  33. const void *gpio_spec;
  34. const u32 *gpio_cells;
  35. ret = of_parse_phandles_with_args(np, "gpios", "#gpio-cells", index,
  36. &gc, &gpio_spec);
  37. if (ret) {
  38. pr_debug("%s: can't parse gpios property\n", __func__);
  39. goto err0;
  40. }
  41. of_gc = gc->data;
  42. if (!of_gc) {
  43. pr_debug("%s: gpio controller %s isn't registered\n",
  44. np->full_name, gc->full_name);
  45. ret = -ENODEV;
  46. goto err1;
  47. }
  48. gpio_cells = of_get_property(gc, "#gpio-cells", &size);
  49. if (!gpio_cells || size != sizeof(*gpio_cells) ||
  50. *gpio_cells != of_gc->gpio_cells) {
  51. pr_debug("%s: wrong #gpio-cells for %s\n",
  52. np->full_name, gc->full_name);
  53. ret = -EINVAL;
  54. goto err1;
  55. }
  56. ret = of_gc->xlate(of_gc, np, gpio_spec);
  57. if (ret < 0)
  58. goto err1;
  59. ret += of_gc->gc.base;
  60. err1:
  61. of_node_put(gc);
  62. err0:
  63. pr_debug("%s exited with status %d\n", __func__, ret);
  64. return ret;
  65. }
  66. EXPORT_SYMBOL(of_get_gpio);
  67. /**
  68. * of_gpio_simple_xlate - translate gpio_spec to the GPIO number
  69. * @of_gc: pointer to the of_gpio_chip structure
  70. * @np: device node of the GPIO chip
  71. * @gpio_spec: gpio specifier as found in the device tree
  72. *
  73. * This is simple translation function, suitable for the most 1:1 mapped
  74. * gpio chips. This function performs only one sanity check: whether gpio
  75. * is less than ngpios (that is specified in the gpio_chip).
  76. */
  77. int of_gpio_simple_xlate(struct of_gpio_chip *of_gc, struct device_node *np,
  78. const void *gpio_spec)
  79. {
  80. const u32 *gpio = gpio_spec;
  81. if (*gpio > of_gc->gc.ngpio)
  82. return -EINVAL;
  83. return *gpio;
  84. }
  85. EXPORT_SYMBOL(of_gpio_simple_xlate);
  86. /**
  87. * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
  88. * @np: device node of the GPIO chip
  89. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  90. *
  91. * To use this function you should allocate and fill mm_gc with:
  92. *
  93. * 1) In the gpio_chip structure:
  94. * - all the callbacks
  95. *
  96. * 2) In the of_gpio_chip structure:
  97. * - gpio_cells
  98. * - xlate callback (optional)
  99. *
  100. * 3) In the of_mm_gpio_chip structure:
  101. * - save_regs callback (optional)
  102. *
  103. * If succeeded, this function will map bank's memory and will
  104. * do all necessary work for you. Then you'll able to use .regs
  105. * to manage GPIOs from the callbacks.
  106. */
  107. int of_mm_gpiochip_add(struct device_node *np,
  108. struct of_mm_gpio_chip *mm_gc)
  109. {
  110. int ret = -ENOMEM;
  111. struct of_gpio_chip *of_gc = &mm_gc->of_gc;
  112. struct gpio_chip *gc = &of_gc->gc;
  113. gc->label = kstrdup(np->full_name, GFP_KERNEL);
  114. if (!gc->label)
  115. goto err0;
  116. mm_gc->regs = of_iomap(np, 0);
  117. if (!mm_gc->regs)
  118. goto err1;
  119. gc->base = -1;
  120. if (!of_gc->xlate)
  121. of_gc->xlate = of_gpio_simple_xlate;
  122. if (mm_gc->save_regs)
  123. mm_gc->save_regs(mm_gc);
  124. np->data = of_gc;
  125. ret = gpiochip_add(gc);
  126. if (ret)
  127. goto err2;
  128. /* We don't want to lose the node and its ->data */
  129. of_node_get(np);
  130. pr_debug("%s: registered as generic GPIO chip, base is %d\n",
  131. np->full_name, gc->base);
  132. return 0;
  133. err2:
  134. np->data = NULL;
  135. iounmap(mm_gc->regs);
  136. err1:
  137. kfree(gc->label);
  138. err0:
  139. pr_err("%s: GPIO chip registration failed with status %d\n",
  140. np->full_name, ret);
  141. return ret;
  142. }
  143. EXPORT_SYMBOL(of_mm_gpiochip_add);