gpio-xilinx.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 |= BIT(gpio);
  87. else
  88. chip->gpio_state &= ~BIT(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 |= BIT(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 |= BIT(gpio);
  137. else
  138. chip->gpio_state &= ~BIT(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 &= ~BIT(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. u32 ngpio;
  174. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  175. if (!chip)
  176. return -ENOMEM;
  177. /* Update GPIO state shadow register with default value */
  178. of_property_read_u32(np, "xlnx,dout-default", &chip->gpio_state);
  179. /* By default, all pins are inputs */
  180. chip->gpio_dir = 0xFFFFFFFF;
  181. /* Update GPIO direction shadow register with default value */
  182. of_property_read_u32(np, "xlnx,tri-default", &chip->gpio_dir);
  183. /*
  184. * Check device node and parent device node for device width
  185. * and assume default width of 32
  186. */
  187. if (of_property_read_u32(np, "xlnx,gpio-width", &ngpio))
  188. ngpio = 32;
  189. chip->mmchip.gc.ngpio = (u16)ngpio;
  190. spin_lock_init(&chip->gpio_lock);
  191. chip->mmchip.gc.direction_input = xgpio_dir_in;
  192. chip->mmchip.gc.direction_output = xgpio_dir_out;
  193. chip->mmchip.gc.get = xgpio_get;
  194. chip->mmchip.gc.set = xgpio_set;
  195. chip->mmchip.save_regs = xgpio_save_regs;
  196. /* Call the OF gpio helper to setup and register the GPIO device */
  197. status = of_mm_gpiochip_add(np, &chip->mmchip);
  198. if (status) {
  199. kfree(chip);
  200. pr_err("%s: error in probe function with status %d\n",
  201. np->full_name, status);
  202. return status;
  203. }
  204. pr_info("XGpio: %s: registered, base is %d\n", np->full_name,
  205. chip->mmchip.gc.base);
  206. tree_info = of_get_property(np, "xlnx,is-dual", NULL);
  207. if (tree_info && be32_to_cpup(tree_info)) {
  208. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  209. if (!chip)
  210. return -ENOMEM;
  211. /* Add dual channel offset */
  212. chip->offset = XGPIO_CHANNEL_OFFSET;
  213. /* Update GPIO state shadow register with default value */
  214. of_property_read_u32(np, "xlnx,dout-default-2",
  215. &chip->gpio_state);
  216. /* By default, all pins are inputs */
  217. chip->gpio_dir = 0xFFFFFFFF;
  218. /* Update GPIO direction shadow register with default value */
  219. of_property_read_u32(np, "xlnx,tri-default-2", &chip->gpio_dir);
  220. /*
  221. * Check device node and parent device node for device width
  222. * and assume default width of 32
  223. */
  224. if (of_property_read_u32(np, "xlnx,gpio2-width", &ngpio))
  225. ngpio = 32;
  226. chip->mmchip.gc.ngpio = (u16)ngpio;
  227. spin_lock_init(&chip->gpio_lock);
  228. chip->mmchip.gc.direction_input = xgpio_dir_in;
  229. chip->mmchip.gc.direction_output = xgpio_dir_out;
  230. chip->mmchip.gc.get = xgpio_get;
  231. chip->mmchip.gc.set = xgpio_set;
  232. chip->mmchip.save_regs = xgpio_save_regs;
  233. /* Call the OF gpio helper to setup and register the GPIO dev */
  234. status = of_mm_gpiochip_add(np, &chip->mmchip);
  235. if (status) {
  236. kfree(chip);
  237. pr_err("%s: error in probe function with status %d\n",
  238. np->full_name, status);
  239. return status;
  240. }
  241. pr_info("XGpio: %s: dual channel registered, base is %d\n",
  242. np->full_name, chip->mmchip.gc.base);
  243. }
  244. return 0;
  245. }
  246. static const struct of_device_id xgpio_of_match[] = {
  247. { .compatible = "xlnx,xps-gpio-1.00.a", },
  248. { /* end of list */ },
  249. };
  250. static int __init xgpio_init(void)
  251. {
  252. struct device_node *np;
  253. for_each_matching_node(np, xgpio_of_match)
  254. xgpio_of_probe(np);
  255. return 0;
  256. }
  257. /* Make sure we get initialized before anyone else tries to use us */
  258. subsys_initcall(xgpio_init);
  259. /* No exit call at the moment as we cannot unregister of GPIO chips */
  260. MODULE_AUTHOR("Xilinx, Inc.");
  261. MODULE_DESCRIPTION("Xilinx GPIO driver");
  262. MODULE_LICENSE("GPL");