gpio.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * QUICC Engine GPIOs
  3. *
  4. * Copyright (c) MontaVista Software, Inc. 2008.
  5. *
  6. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/err.h>
  17. #include <linux/io.h>
  18. #include <linux/of.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/gpio/driver.h>
  21. /* FIXME: needed for gpio_to_chip() get rid of this */
  22. #include <linux/gpio.h>
  23. #include <linux/slab.h>
  24. #include <linux/export.h>
  25. #include <soc/fsl/qe/qe.h>
  26. struct qe_gpio_chip {
  27. struct of_mm_gpio_chip mm_gc;
  28. spinlock_t lock;
  29. unsigned long pin_flags[QE_PIO_PINS];
  30. #define QE_PIN_REQUESTED 0
  31. /* shadowed data register to clear/set bits safely */
  32. u32 cpdata;
  33. /* saved_regs used to restore dedicated functions */
  34. struct qe_pio_regs saved_regs;
  35. };
  36. static void qe_gpio_save_regs(struct of_mm_gpio_chip *mm_gc)
  37. {
  38. struct qe_gpio_chip *qe_gc =
  39. container_of(mm_gc, struct qe_gpio_chip, mm_gc);
  40. struct qe_pio_regs __iomem *regs = mm_gc->regs;
  41. qe_gc->cpdata = in_be32(&regs->cpdata);
  42. qe_gc->saved_regs.cpdata = qe_gc->cpdata;
  43. qe_gc->saved_regs.cpdir1 = in_be32(&regs->cpdir1);
  44. qe_gc->saved_regs.cpdir2 = in_be32(&regs->cpdir2);
  45. qe_gc->saved_regs.cppar1 = in_be32(&regs->cppar1);
  46. qe_gc->saved_regs.cppar2 = in_be32(&regs->cppar2);
  47. qe_gc->saved_regs.cpodr = in_be32(&regs->cpodr);
  48. }
  49. static int qe_gpio_get(struct gpio_chip *gc, unsigned int gpio)
  50. {
  51. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  52. struct qe_pio_regs __iomem *regs = mm_gc->regs;
  53. u32 pin_mask = 1 << (QE_PIO_PINS - 1 - gpio);
  54. return !!(in_be32(&regs->cpdata) & pin_mask);
  55. }
  56. static void qe_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  57. {
  58. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  59. struct qe_gpio_chip *qe_gc = gpiochip_get_data(gc);
  60. struct qe_pio_regs __iomem *regs = mm_gc->regs;
  61. unsigned long flags;
  62. u32 pin_mask = 1 << (QE_PIO_PINS - 1 - gpio);
  63. spin_lock_irqsave(&qe_gc->lock, flags);
  64. if (val)
  65. qe_gc->cpdata |= pin_mask;
  66. else
  67. qe_gc->cpdata &= ~pin_mask;
  68. out_be32(&regs->cpdata, qe_gc->cpdata);
  69. spin_unlock_irqrestore(&qe_gc->lock, flags);
  70. }
  71. static int qe_gpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  72. {
  73. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  74. struct qe_gpio_chip *qe_gc = gpiochip_get_data(gc);
  75. unsigned long flags;
  76. spin_lock_irqsave(&qe_gc->lock, flags);
  77. __par_io_config_pin(mm_gc->regs, gpio, QE_PIO_DIR_IN, 0, 0, 0);
  78. spin_unlock_irqrestore(&qe_gc->lock, flags);
  79. return 0;
  80. }
  81. static int qe_gpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  82. {
  83. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  84. struct qe_gpio_chip *qe_gc = gpiochip_get_data(gc);
  85. unsigned long flags;
  86. qe_gpio_set(gc, gpio, val);
  87. spin_lock_irqsave(&qe_gc->lock, flags);
  88. __par_io_config_pin(mm_gc->regs, gpio, QE_PIO_DIR_OUT, 0, 0, 0);
  89. spin_unlock_irqrestore(&qe_gc->lock, flags);
  90. return 0;
  91. }
  92. struct qe_pin {
  93. /*
  94. * The qe_gpio_chip name is unfortunate, we should change that to
  95. * something like qe_pio_controller. Someday.
  96. */
  97. struct qe_gpio_chip *controller;
  98. int num;
  99. };
  100. /**
  101. * qe_pin_request - Request a QE pin
  102. * @np: device node to get a pin from
  103. * @index: index of a pin in the device tree
  104. * Context: non-atomic
  105. *
  106. * This function return qe_pin so that you could use it with the rest of
  107. * the QE Pin Multiplexing API.
  108. */
  109. struct qe_pin *qe_pin_request(struct device_node *np, int index)
  110. {
  111. struct qe_pin *qe_pin;
  112. struct gpio_chip *gc;
  113. struct of_mm_gpio_chip *mm_gc;
  114. struct qe_gpio_chip *qe_gc;
  115. int err;
  116. unsigned long flags;
  117. qe_pin = kzalloc(sizeof(*qe_pin), GFP_KERNEL);
  118. if (!qe_pin) {
  119. pr_debug("%s: can't allocate memory\n", __func__);
  120. return ERR_PTR(-ENOMEM);
  121. }
  122. err = of_get_gpio(np, index);
  123. if (err < 0)
  124. goto err0;
  125. gc = gpio_to_chip(err);
  126. if (WARN_ON(!gc))
  127. goto err0;
  128. if (!of_device_is_compatible(gc->of_node, "fsl,mpc8323-qe-pario-bank")) {
  129. pr_debug("%s: tried to get a non-qe pin\n", __func__);
  130. err = -EINVAL;
  131. goto err0;
  132. }
  133. mm_gc = to_of_mm_gpio_chip(gc);
  134. qe_gc = gpiochip_get_data(gc);
  135. spin_lock_irqsave(&qe_gc->lock, flags);
  136. err -= gc->base;
  137. if (test_and_set_bit(QE_PIN_REQUESTED, &qe_gc->pin_flags[err]) == 0) {
  138. qe_pin->controller = qe_gc;
  139. qe_pin->num = err;
  140. err = 0;
  141. } else {
  142. err = -EBUSY;
  143. }
  144. spin_unlock_irqrestore(&qe_gc->lock, flags);
  145. if (!err)
  146. return qe_pin;
  147. err0:
  148. kfree(qe_pin);
  149. pr_debug("%s failed with status %d\n", __func__, err);
  150. return ERR_PTR(err);
  151. }
  152. EXPORT_SYMBOL(qe_pin_request);
  153. /**
  154. * qe_pin_free - Free a pin
  155. * @qe_pin: pointer to the qe_pin structure
  156. * Context: any
  157. *
  158. * This function frees the qe_pin structure and makes a pin available
  159. * for further qe_pin_request() calls.
  160. */
  161. void qe_pin_free(struct qe_pin *qe_pin)
  162. {
  163. struct qe_gpio_chip *qe_gc = qe_pin->controller;
  164. unsigned long flags;
  165. const int pin = qe_pin->num;
  166. spin_lock_irqsave(&qe_gc->lock, flags);
  167. test_and_clear_bit(QE_PIN_REQUESTED, &qe_gc->pin_flags[pin]);
  168. spin_unlock_irqrestore(&qe_gc->lock, flags);
  169. kfree(qe_pin);
  170. }
  171. EXPORT_SYMBOL(qe_pin_free);
  172. /**
  173. * qe_pin_set_dedicated - Revert a pin to a dedicated peripheral function mode
  174. * @qe_pin: pointer to the qe_pin structure
  175. * Context: any
  176. *
  177. * This function resets a pin to a dedicated peripheral function that
  178. * has been set up by the firmware.
  179. */
  180. void qe_pin_set_dedicated(struct qe_pin *qe_pin)
  181. {
  182. struct qe_gpio_chip *qe_gc = qe_pin->controller;
  183. struct qe_pio_regs __iomem *regs = qe_gc->mm_gc.regs;
  184. struct qe_pio_regs *sregs = &qe_gc->saved_regs;
  185. int pin = qe_pin->num;
  186. u32 mask1 = 1 << (QE_PIO_PINS - (pin + 1));
  187. u32 mask2 = 0x3 << (QE_PIO_PINS - (pin % (QE_PIO_PINS / 2) + 1) * 2);
  188. bool second_reg = pin > (QE_PIO_PINS / 2) - 1;
  189. unsigned long flags;
  190. spin_lock_irqsave(&qe_gc->lock, flags);
  191. if (second_reg) {
  192. clrsetbits_be32(&regs->cpdir2, mask2, sregs->cpdir2 & mask2);
  193. clrsetbits_be32(&regs->cppar2, mask2, sregs->cppar2 & mask2);
  194. } else {
  195. clrsetbits_be32(&regs->cpdir1, mask2, sregs->cpdir1 & mask2);
  196. clrsetbits_be32(&regs->cppar1, mask2, sregs->cppar1 & mask2);
  197. }
  198. if (sregs->cpdata & mask1)
  199. qe_gc->cpdata |= mask1;
  200. else
  201. qe_gc->cpdata &= ~mask1;
  202. out_be32(&regs->cpdata, qe_gc->cpdata);
  203. clrsetbits_be32(&regs->cpodr, mask1, sregs->cpodr & mask1);
  204. spin_unlock_irqrestore(&qe_gc->lock, flags);
  205. }
  206. EXPORT_SYMBOL(qe_pin_set_dedicated);
  207. /**
  208. * qe_pin_set_gpio - Set a pin to the GPIO mode
  209. * @qe_pin: pointer to the qe_pin structure
  210. * Context: any
  211. *
  212. * This function sets a pin to the GPIO mode.
  213. */
  214. void qe_pin_set_gpio(struct qe_pin *qe_pin)
  215. {
  216. struct qe_gpio_chip *qe_gc = qe_pin->controller;
  217. struct qe_pio_regs __iomem *regs = qe_gc->mm_gc.regs;
  218. unsigned long flags;
  219. spin_lock_irqsave(&qe_gc->lock, flags);
  220. /* Let's make it input by default, GPIO API is able to change that. */
  221. __par_io_config_pin(regs, qe_pin->num, QE_PIO_DIR_IN, 0, 0, 0);
  222. spin_unlock_irqrestore(&qe_gc->lock, flags);
  223. }
  224. EXPORT_SYMBOL(qe_pin_set_gpio);
  225. static int __init qe_add_gpiochips(void)
  226. {
  227. struct device_node *np;
  228. for_each_compatible_node(np, NULL, "fsl,mpc8323-qe-pario-bank") {
  229. int ret;
  230. struct qe_gpio_chip *qe_gc;
  231. struct of_mm_gpio_chip *mm_gc;
  232. struct gpio_chip *gc;
  233. qe_gc = kzalloc(sizeof(*qe_gc), GFP_KERNEL);
  234. if (!qe_gc) {
  235. ret = -ENOMEM;
  236. goto err;
  237. }
  238. spin_lock_init(&qe_gc->lock);
  239. mm_gc = &qe_gc->mm_gc;
  240. gc = &mm_gc->gc;
  241. mm_gc->save_regs = qe_gpio_save_regs;
  242. gc->ngpio = QE_PIO_PINS;
  243. gc->direction_input = qe_gpio_dir_in;
  244. gc->direction_output = qe_gpio_dir_out;
  245. gc->get = qe_gpio_get;
  246. gc->set = qe_gpio_set;
  247. ret = of_mm_gpiochip_add_data(np, mm_gc, qe_gc);
  248. if (ret)
  249. goto err;
  250. continue;
  251. err:
  252. pr_err("%s: registration failed with status %d\n",
  253. np->full_name, ret);
  254. kfree(qe_gc);
  255. /* try others anyway */
  256. }
  257. return 0;
  258. }
  259. arch_initcall(qe_add_gpiochips);