gpio-xilinx.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Xilinx gpio driver for xps/axi_gpio IP.
  3. *
  4. * Copyright 2008 - 2013 Xilinx, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation.
  9. *
  10. * You should have received a copy of the GNU General Public License
  11. * along with this program; if not, write to the Free Software
  12. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  13. */
  14. #include <linux/bitops.h>
  15. #include <linux/init.h>
  16. #include <linux/errno.h>
  17. #include <linux/module.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of_platform.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/io.h>
  22. #include <linux/gpio.h>
  23. #include <linux/slab.h>
  24. /* Register Offset Definitions */
  25. #define XGPIO_DATA_OFFSET (0x0) /* Data register */
  26. #define XGPIO_TRI_OFFSET (0x4) /* I/O direction register */
  27. #define XGPIO_CHANNEL_OFFSET 0x8
  28. /* Read/Write access to the GPIO registers */
  29. #define xgpio_readreg(offset) in_be32(offset)
  30. #define xgpio_writereg(offset, val) out_be32(offset, val)
  31. /**
  32. * struct xgpio_instance - Stores information about GPIO device
  33. * struct of_mm_gpio_chip mmchip: OF GPIO chip for memory mapped banks
  34. * gpio_state: GPIO state shadow register
  35. * gpio_dir: GPIO direction shadow register
  36. * offset: GPIO channel offset
  37. * gpio_lock: Lock used for synchronization
  38. */
  39. struct xgpio_instance {
  40. struct of_mm_gpio_chip mmchip;
  41. u32 gpio_state;
  42. u32 gpio_dir;
  43. u32 offset;
  44. spinlock_t gpio_lock;
  45. };
  46. /**
  47. * xgpio_get - Read the specified signal of the GPIO device.
  48. * @gc: Pointer to gpio_chip device structure.
  49. * @gpio: GPIO signal number.
  50. *
  51. * This function reads the specified signal of the GPIO device. It returns 0 if
  52. * the signal clear, 1 if signal is set or negative value on error.
  53. */
  54. static int xgpio_get(struct gpio_chip *gc, unsigned int gpio)
  55. {
  56. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  57. struct xgpio_instance *chip =
  58. container_of(mm_gc, struct xgpio_instance, mmchip);
  59. void __iomem *regs = mm_gc->regs + chip->offset;
  60. return !!(xgpio_readreg(regs + XGPIO_DATA_OFFSET) & BIT(gpio));
  61. }
  62. /**
  63. * xgpio_set - Write the specified signal of the GPIO device.
  64. * @gc: Pointer to gpio_chip device structure.
  65. * @gpio: GPIO signal number.
  66. * @val: Value to be written to specified signal.
  67. *
  68. * This function writes the specified value in to the specified signal of the
  69. * GPIO device.
  70. */
  71. static void xgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  72. {
  73. unsigned long flags;
  74. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  75. struct xgpio_instance *chip =
  76. container_of(mm_gc, struct xgpio_instance, mmchip);
  77. void __iomem *regs = mm_gc->regs;
  78. spin_lock_irqsave(&chip->gpio_lock, flags);
  79. /* Write to GPIO signal and set its direction to output */
  80. if (val)
  81. chip->gpio_state |= 1 << gpio;
  82. else
  83. chip->gpio_state &= ~(1 << gpio);
  84. xgpio_writereg(regs + chip->offset + XGPIO_DATA_OFFSET,
  85. chip->gpio_state);
  86. spin_unlock_irqrestore(&chip->gpio_lock, flags);
  87. }
  88. /**
  89. * xgpio_dir_in - Set the direction of the specified GPIO signal as input.
  90. * @gc: Pointer to gpio_chip device structure.
  91. * @gpio: GPIO signal number.
  92. *
  93. * This function sets the direction of specified GPIO signal as input.
  94. * It returns 0 if direction of GPIO signals is set as input otherwise it
  95. * returns negative error value.
  96. */
  97. static int xgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  98. {
  99. unsigned long flags;
  100. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  101. struct xgpio_instance *chip =
  102. container_of(mm_gc, struct xgpio_instance, mmchip);
  103. void __iomem *regs = mm_gc->regs;
  104. spin_lock_irqsave(&chip->gpio_lock, flags);
  105. /* Set the GPIO bit in shadow register and set direction as input */
  106. chip->gpio_dir |= (1 << gpio);
  107. xgpio_writereg(regs + chip->offset + XGPIO_TRI_OFFSET, chip->gpio_dir);
  108. spin_unlock_irqrestore(&chip->gpio_lock, flags);
  109. return 0;
  110. }
  111. /**
  112. * xgpio_dir_out - Set the direction of the specified GPIO signal as output.
  113. * @gc: Pointer to gpio_chip device structure.
  114. * @gpio: GPIO signal number.
  115. * @val: Value to be written to specified signal.
  116. *
  117. * This function sets the direction of specified GPIO signal as output. If all
  118. * GPIO signals of GPIO chip is configured as input then it returns
  119. * error otherwise it returns 0.
  120. */
  121. static int xgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  122. {
  123. unsigned long flags;
  124. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  125. struct xgpio_instance *chip =
  126. container_of(mm_gc, struct xgpio_instance, mmchip);
  127. void __iomem *regs = mm_gc->regs;
  128. spin_lock_irqsave(&chip->gpio_lock, flags);
  129. /* Write state of GPIO signal */
  130. if (val)
  131. chip->gpio_state |= 1 << gpio;
  132. else
  133. chip->gpio_state &= ~(1 << gpio);
  134. xgpio_writereg(regs + chip->offset + XGPIO_DATA_OFFSET,
  135. chip->gpio_state);
  136. /* Clear the GPIO bit in shadow register and set direction as output */
  137. chip->gpio_dir &= (~(1 << gpio));
  138. xgpio_writereg(regs + chip->offset + XGPIO_TRI_OFFSET, chip->gpio_dir);
  139. spin_unlock_irqrestore(&chip->gpio_lock, flags);
  140. return 0;
  141. }
  142. /**
  143. * xgpio_save_regs - Set initial values of GPIO pins
  144. * @mm_gc: pointer to memory mapped GPIO chip structure
  145. */
  146. static void xgpio_save_regs(struct of_mm_gpio_chip *mm_gc)
  147. {
  148. struct xgpio_instance *chip =
  149. container_of(mm_gc, struct xgpio_instance, mmchip);
  150. xgpio_writereg(mm_gc->regs + chip->offset + XGPIO_DATA_OFFSET,
  151. chip->gpio_state);
  152. xgpio_writereg(mm_gc->regs + chip->offset + XGPIO_TRI_OFFSET,
  153. chip->gpio_dir);
  154. }
  155. /**
  156. * xgpio_of_probe - Probe method for the GPIO device.
  157. * @np: pointer to device tree node
  158. *
  159. * This function probes the GPIO device in the device tree. It initializes the
  160. * driver data structure. It returns 0, if the driver is bound to the GPIO
  161. * device, or a negative value if there is an error.
  162. */
  163. static int xgpio_of_probe(struct device_node *np)
  164. {
  165. struct xgpio_instance *chip;
  166. int status = 0;
  167. const u32 *tree_info;
  168. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  169. if (!chip)
  170. return -ENOMEM;
  171. /* Update GPIO state shadow register with default value */
  172. of_property_read_u32(np, "xlnx,dout-default", &chip->gpio_state);
  173. /* By default, all pins are inputs */
  174. chip->gpio_dir = 0xFFFFFFFF;
  175. /* Update GPIO direction shadow register with default value */
  176. of_property_read_u32(np, "xlnx,tri-default", &chip->gpio_dir);
  177. /* By default assume full GPIO controller */
  178. chip->mmchip.gc.ngpio = 32;
  179. /* Check device node and parent device node for device width */
  180. of_property_read_u32(np, "xlnx,gpio-width",
  181. (u32 *)&chip->mmchip.gc.ngpio);
  182. spin_lock_init(&chip->gpio_lock);
  183. chip->mmchip.gc.direction_input = xgpio_dir_in;
  184. chip->mmchip.gc.direction_output = xgpio_dir_out;
  185. chip->mmchip.gc.get = xgpio_get;
  186. chip->mmchip.gc.set = xgpio_set;
  187. chip->mmchip.save_regs = xgpio_save_regs;
  188. /* Call the OF gpio helper to setup and register the GPIO device */
  189. status = of_mm_gpiochip_add(np, &chip->mmchip);
  190. if (status) {
  191. kfree(chip);
  192. pr_err("%s: error in probe function with status %d\n",
  193. np->full_name, status);
  194. return status;
  195. }
  196. pr_info("XGpio: %s: registered, base is %d\n", np->full_name,
  197. chip->mmchip.gc.base);
  198. tree_info = of_get_property(np, "xlnx,is-dual", NULL);
  199. if (tree_info && be32_to_cpup(tree_info)) {
  200. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  201. if (!chip)
  202. return -ENOMEM;
  203. /* Add dual channel offset */
  204. chip->offset = XGPIO_CHANNEL_OFFSET;
  205. /* Update GPIO state shadow register with default value */
  206. of_property_read_u32(np, "xlnx,dout-default-2",
  207. &chip->gpio_state);
  208. /* By default, all pins are inputs */
  209. chip->gpio_dir = 0xFFFFFFFF;
  210. /* Update GPIO direction shadow register with default value */
  211. of_property_read_u32(np, "xlnx,tri-default-2", &chip->gpio_dir);
  212. /* By default assume full GPIO controller */
  213. chip->mmchip.gc.ngpio = 32;
  214. /* Check device node and parent device node for device width */
  215. of_property_read_u32(np, "xlnx,gpio2-width",
  216. (u32 *)&chip->mmchip.gc.ngpio);
  217. spin_lock_init(&chip->gpio_lock);
  218. chip->mmchip.gc.direction_input = xgpio_dir_in;
  219. chip->mmchip.gc.direction_output = xgpio_dir_out;
  220. chip->mmchip.gc.get = xgpio_get;
  221. chip->mmchip.gc.set = xgpio_set;
  222. chip->mmchip.save_regs = xgpio_save_regs;
  223. /* Call the OF gpio helper to setup and register the GPIO dev */
  224. status = of_mm_gpiochip_add(np, &chip->mmchip);
  225. if (status) {
  226. kfree(chip);
  227. pr_err("%s: error in probe function with status %d\n",
  228. np->full_name, status);
  229. return status;
  230. }
  231. pr_info("XGpio: %s: dual channel registered, base is %d\n",
  232. np->full_name, chip->mmchip.gc.base);
  233. }
  234. return 0;
  235. }
  236. static struct of_device_id xgpio_of_match[] = {
  237. { .compatible = "xlnx,xps-gpio-1.00.a", },
  238. { /* end of list */ },
  239. };
  240. static int __init xgpio_init(void)
  241. {
  242. struct device_node *np;
  243. for_each_matching_node(np, xgpio_of_match)
  244. xgpio_of_probe(np);
  245. return 0;
  246. }
  247. /* Make sure we get initialized before anyone else tries to use us */
  248. subsys_initcall(xgpio_init);
  249. /* No exit call at the moment as we cannot unregister of GPIO chips */
  250. MODULE_AUTHOR("Xilinx, Inc.");
  251. MODULE_DESCRIPTION("Xilinx GPIO driver");
  252. MODULE_LICENSE("GPL");