gpio-xilinx.c 9.2 KB

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