gpio.c 8.4 KB

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