gpio.c 7.9 KB

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