gpio.c 8.5 KB

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