gpio.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 <asm/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 inline struct qe_gpio_chip *
  33. to_qe_gpio_chip(struct of_mm_gpio_chip *mm_gc)
  34. {
  35. return container_of(mm_gc, struct qe_gpio_chip, mm_gc);
  36. }
  37. static void qe_gpio_save_regs(struct of_mm_gpio_chip *mm_gc)
  38. {
  39. struct qe_gpio_chip *qe_gc = to_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 = to_qe_gpio_chip(mm_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 = to_qe_gpio_chip(mm_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 = to_qe_gpio_chip(mm_gc);
  85. unsigned long flags;
  86. spin_lock_irqsave(&qe_gc->lock, flags);
  87. __par_io_config_pin(mm_gc->regs, gpio, QE_PIO_DIR_OUT, 0, 0, 0);
  88. spin_unlock_irqrestore(&qe_gc->lock, flags);
  89. qe_gpio_set(gc, gpio, val);
  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 device_node *gc;
  113. struct of_gpio_chip *of_gc = NULL;
  114. struct of_mm_gpio_chip *mm_gc;
  115. struct qe_gpio_chip *qe_gc;
  116. int err;
  117. int size;
  118. const void *gpio_spec;
  119. const u32 *gpio_cells;
  120. unsigned long flags;
  121. qe_pin = kzalloc(sizeof(*qe_pin), GFP_KERNEL);
  122. if (!qe_pin) {
  123. pr_debug("%s: can't allocate memory\n", __func__);
  124. return ERR_PTR(-ENOMEM);
  125. }
  126. err = of_parse_phandles_with_args(np, "gpios", "#gpio-cells", index,
  127. &gc, &gpio_spec);
  128. if (err) {
  129. pr_debug("%s: can't parse gpios property\n", __func__);
  130. goto err0;
  131. }
  132. if (!of_device_is_compatible(gc, "fsl,mpc8323-qe-pario-bank")) {
  133. pr_debug("%s: tried to get a non-qe pin\n", __func__);
  134. err = -EINVAL;
  135. goto err1;
  136. }
  137. of_gc = gc->data;
  138. if (!of_gc) {
  139. pr_debug("%s: gpio controller %s isn't registered\n",
  140. np->full_name, gc->full_name);
  141. err = -ENODEV;
  142. goto err1;
  143. }
  144. gpio_cells = of_get_property(gc, "#gpio-cells", &size);
  145. if (!gpio_cells || size != sizeof(*gpio_cells) ||
  146. *gpio_cells != of_gc->gpio_cells) {
  147. pr_debug("%s: wrong #gpio-cells for %s\n",
  148. np->full_name, gc->full_name);
  149. err = -EINVAL;
  150. goto err1;
  151. }
  152. err = of_gc->xlate(of_gc, np, gpio_spec, NULL);
  153. if (err < 0)
  154. goto err1;
  155. mm_gc = to_of_mm_gpio_chip(&of_gc->gc);
  156. qe_gc = to_qe_gpio_chip(mm_gc);
  157. spin_lock_irqsave(&qe_gc->lock, flags);
  158. if (test_and_set_bit(QE_PIN_REQUESTED, &qe_gc->pin_flags[err]) == 0) {
  159. qe_pin->controller = qe_gc;
  160. qe_pin->num = err;
  161. err = 0;
  162. } else {
  163. err = -EBUSY;
  164. }
  165. spin_unlock_irqrestore(&qe_gc->lock, flags);
  166. if (!err)
  167. return qe_pin;
  168. err1:
  169. of_node_put(gc);
  170. err0:
  171. kfree(qe_pin);
  172. pr_debug("%s failed with status %d\n", __func__, err);
  173. return ERR_PTR(err);
  174. }
  175. EXPORT_SYMBOL(qe_pin_request);
  176. /**
  177. * qe_pin_free - Free a pin
  178. * @qe_pin: pointer to the qe_pin structure
  179. * Context: any
  180. *
  181. * This function frees the qe_pin structure and makes a pin available
  182. * for further qe_pin_request() calls.
  183. */
  184. void qe_pin_free(struct qe_pin *qe_pin)
  185. {
  186. struct qe_gpio_chip *qe_gc = qe_pin->controller;
  187. unsigned long flags;
  188. const int pin = qe_pin->num;
  189. spin_lock_irqsave(&qe_gc->lock, flags);
  190. test_and_clear_bit(QE_PIN_REQUESTED, &qe_gc->pin_flags[pin]);
  191. spin_unlock_irqrestore(&qe_gc->lock, flags);
  192. kfree(qe_pin);
  193. }
  194. EXPORT_SYMBOL(qe_pin_free);
  195. /**
  196. * qe_pin_set_dedicated - Revert a pin to a dedicated peripheral function mode
  197. * @qe_pin: pointer to the qe_pin structure
  198. * Context: any
  199. *
  200. * This function resets a pin to a dedicated peripheral function that
  201. * has been set up by the firmware.
  202. */
  203. void qe_pin_set_dedicated(struct qe_pin *qe_pin)
  204. {
  205. struct qe_gpio_chip *qe_gc = qe_pin->controller;
  206. struct qe_pio_regs __iomem *regs = qe_gc->mm_gc.regs;
  207. struct qe_pio_regs *sregs = &qe_gc->saved_regs;
  208. int pin = qe_pin->num;
  209. u32 mask1 = 1 << (QE_PIO_PINS - (pin + 1));
  210. u32 mask2 = 0x3 << (QE_PIO_PINS - (pin % (QE_PIO_PINS / 2) + 1) * 2);
  211. bool second_reg = pin > (QE_PIO_PINS / 2) - 1;
  212. unsigned long flags;
  213. spin_lock_irqsave(&qe_gc->lock, flags);
  214. if (second_reg) {
  215. clrsetbits_be32(&regs->cpdir2, mask2, sregs->cpdir2 & mask2);
  216. clrsetbits_be32(&regs->cppar2, mask2, sregs->cppar2 & mask2);
  217. } else {
  218. clrsetbits_be32(&regs->cpdir1, mask2, sregs->cpdir1 & mask2);
  219. clrsetbits_be32(&regs->cppar1, mask2, sregs->cppar1 & mask2);
  220. }
  221. if (sregs->cpdata & mask1)
  222. qe_gc->cpdata |= mask1;
  223. else
  224. qe_gc->cpdata &= ~mask1;
  225. out_be32(&regs->cpdata, qe_gc->cpdata);
  226. clrsetbits_be32(&regs->cpodr, mask1, sregs->cpodr & mask1);
  227. spin_unlock_irqrestore(&qe_gc->lock, flags);
  228. }
  229. EXPORT_SYMBOL(qe_pin_set_dedicated);
  230. /**
  231. * qe_pin_set_gpio - Set a pin to the GPIO mode
  232. * @qe_pin: pointer to the qe_pin structure
  233. * Context: any
  234. *
  235. * This function sets a pin to the GPIO mode.
  236. */
  237. void qe_pin_set_gpio(struct qe_pin *qe_pin)
  238. {
  239. struct qe_gpio_chip *qe_gc = qe_pin->controller;
  240. struct qe_pio_regs __iomem *regs = qe_gc->mm_gc.regs;
  241. unsigned long flags;
  242. spin_lock_irqsave(&qe_gc->lock, flags);
  243. /* Let's make it input by default, GPIO API is able to change that. */
  244. __par_io_config_pin(regs, qe_pin->num, QE_PIO_DIR_IN, 0, 0, 0);
  245. spin_unlock_irqrestore(&qe_gc->lock, flags);
  246. }
  247. EXPORT_SYMBOL(qe_pin_set_gpio);
  248. static int __init qe_add_gpiochips(void)
  249. {
  250. struct device_node *np;
  251. for_each_compatible_node(np, NULL, "fsl,mpc8323-qe-pario-bank") {
  252. int ret;
  253. struct qe_gpio_chip *qe_gc;
  254. struct of_mm_gpio_chip *mm_gc;
  255. struct of_gpio_chip *of_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. of_gc = &mm_gc->of_gc;
  265. gc = &of_gc->gc;
  266. mm_gc->save_regs = qe_gpio_save_regs;
  267. of_gc->gpio_cells = 2;
  268. gc->ngpio = QE_PIO_PINS;
  269. gc->direction_input = qe_gpio_dir_in;
  270. gc->direction_output = qe_gpio_dir_out;
  271. gc->get = qe_gpio_get;
  272. gc->set = qe_gpio_set;
  273. ret = of_mm_gpiochip_add(np, mm_gc);
  274. if (ret)
  275. goto err;
  276. continue;
  277. err:
  278. pr_err("%s: registration failed with status %d\n",
  279. np->full_name, ret);
  280. kfree(qe_gc);
  281. /* try others anyway */
  282. }
  283. return 0;
  284. }
  285. arch_initcall(qe_add_gpiochips);