gpio-xilinx.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. #if defined(CONFIG_ARCH_ZYNQ) || defined(CONFIG_X86)
  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. * @mmchip: OF GPIO chip for memory mapped banks
  39. * @gpio_width: GPIO width for every channel
  40. * @gpio_state: GPIO state shadow register
  41. * @gpio_dir: GPIO direction shadow register
  42. * @gpio_lock: Lock used for synchronization
  43. */
  44. struct xgpio_instance {
  45. struct of_mm_gpio_chip mmchip;
  46. unsigned int gpio_width[2];
  47. u32 gpio_state[2];
  48. u32 gpio_dir[2];
  49. spinlock_t gpio_lock[2];
  50. };
  51. static inline int xgpio_index(struct xgpio_instance *chip, int gpio)
  52. {
  53. if (gpio >= chip->gpio_width[0])
  54. return 1;
  55. return 0;
  56. }
  57. static inline int xgpio_regoffset(struct xgpio_instance *chip, int gpio)
  58. {
  59. if (xgpio_index(chip, gpio))
  60. return XGPIO_CHANNEL_OFFSET;
  61. return 0;
  62. }
  63. static inline int xgpio_offset(struct xgpio_instance *chip, int gpio)
  64. {
  65. if (xgpio_index(chip, gpio))
  66. return gpio - chip->gpio_width[0];
  67. return gpio;
  68. }
  69. /**
  70. * xgpio_get - Read the specified signal of the GPIO device.
  71. * @gc: Pointer to gpio_chip device structure.
  72. * @gpio: GPIO signal number.
  73. *
  74. * This function reads the specified signal of the GPIO device.
  75. *
  76. * Return:
  77. * 0 if direction of GPIO signals is set as input otherwise it
  78. * returns negative error value.
  79. */
  80. static int xgpio_get(struct gpio_chip *gc, unsigned int gpio)
  81. {
  82. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  83. struct xgpio_instance *chip =
  84. container_of(mm_gc, struct xgpio_instance, mmchip);
  85. u32 val;
  86. val = xgpio_readreg(mm_gc->regs + XGPIO_DATA_OFFSET +
  87. xgpio_regoffset(chip, gpio));
  88. return !!(val & BIT(xgpio_offset(chip, gpio)));
  89. }
  90. /**
  91. * xgpio_set - Write the specified signal of the GPIO device.
  92. * @gc: Pointer to gpio_chip device structure.
  93. * @gpio: GPIO signal number.
  94. * @val: Value to be written to specified signal.
  95. *
  96. * This function writes the specified value in to the specified signal of the
  97. * GPIO device.
  98. */
  99. static void xgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  100. {
  101. unsigned long flags;
  102. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  103. struct xgpio_instance *chip =
  104. container_of(mm_gc, struct xgpio_instance, mmchip);
  105. int index = xgpio_index(chip, gpio);
  106. int offset = xgpio_offset(chip, gpio);
  107. spin_lock_irqsave(&chip->gpio_lock[index], flags);
  108. /* Write to GPIO signal and set its direction to output */
  109. if (val)
  110. chip->gpio_state[index] |= BIT(offset);
  111. else
  112. chip->gpio_state[index] &= ~BIT(offset);
  113. xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET +
  114. xgpio_regoffset(chip, gpio), chip->gpio_state[index]);
  115. spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
  116. }
  117. /**
  118. * xgpio_dir_in - Set the direction of the specified GPIO signal as input.
  119. * @gc: Pointer to gpio_chip device structure.
  120. * @gpio: GPIO signal number.
  121. *
  122. * Return:
  123. * 0 - if direction of GPIO signals is set as input
  124. * otherwise it returns negative error value.
  125. */
  126. static int xgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  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. int index = xgpio_index(chip, gpio);
  133. int offset = xgpio_offset(chip, gpio);
  134. spin_lock_irqsave(&chip->gpio_lock[index], flags);
  135. /* Set the GPIO bit in shadow register and set direction as input */
  136. chip->gpio_dir[index] |= BIT(offset);
  137. xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET +
  138. xgpio_regoffset(chip, gpio), chip->gpio_dir[index]);
  139. spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
  140. return 0;
  141. }
  142. /**
  143. * xgpio_dir_out - Set the direction of the specified GPIO signal as output.
  144. * @gc: Pointer to gpio_chip device structure.
  145. * @gpio: GPIO signal number.
  146. * @val: Value to be written to specified signal.
  147. *
  148. * This function sets the direction of specified GPIO signal as output.
  149. *
  150. * Return:
  151. * If all GPIO signals of GPIO chip is configured as input then it returns
  152. * error otherwise it returns 0.
  153. */
  154. static int xgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  155. {
  156. unsigned long flags;
  157. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  158. struct xgpio_instance *chip =
  159. container_of(mm_gc, struct xgpio_instance, mmchip);
  160. int index = xgpio_index(chip, gpio);
  161. int offset = xgpio_offset(chip, gpio);
  162. spin_lock_irqsave(&chip->gpio_lock[index], flags);
  163. /* Write state of GPIO signal */
  164. if (val)
  165. chip->gpio_state[index] |= BIT(offset);
  166. else
  167. chip->gpio_state[index] &= ~BIT(offset);
  168. xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET +
  169. xgpio_regoffset(chip, gpio), chip->gpio_state[index]);
  170. /* Clear the GPIO bit in shadow register and set direction as output */
  171. chip->gpio_dir[index] &= ~BIT(offset);
  172. xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET +
  173. xgpio_regoffset(chip, gpio), chip->gpio_dir[index]);
  174. spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
  175. return 0;
  176. }
  177. /**
  178. * xgpio_save_regs - Set initial values of GPIO pins
  179. * @mm_gc: Pointer to memory mapped GPIO chip structure
  180. */
  181. static void xgpio_save_regs(struct of_mm_gpio_chip *mm_gc)
  182. {
  183. struct xgpio_instance *chip =
  184. container_of(mm_gc, struct xgpio_instance, mmchip);
  185. xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET, chip->gpio_state[0]);
  186. xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET, chip->gpio_dir[0]);
  187. if (!chip->gpio_width[1])
  188. return;
  189. xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET + XGPIO_CHANNEL_OFFSET,
  190. chip->gpio_state[1]);
  191. xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET + XGPIO_CHANNEL_OFFSET,
  192. chip->gpio_dir[1]);
  193. }
  194. /**
  195. * xgpio_remove - Remove method for the GPIO device.
  196. * @pdev: pointer to the platform device
  197. *
  198. * This function remove gpiochips and frees all the allocated resources.
  199. *
  200. * Return: 0 always
  201. */
  202. static int xgpio_remove(struct platform_device *pdev)
  203. {
  204. struct xgpio_instance *chip = platform_get_drvdata(pdev);
  205. of_mm_gpiochip_remove(&chip->mmchip);
  206. return 0;
  207. }
  208. /**
  209. * xgpio_of_probe - Probe method for the GPIO device.
  210. * @pdev: pointer to the platform device
  211. *
  212. * Return:
  213. * It returns 0, if the driver is bound to the GPIO device, or
  214. * a negative value if there is an error.
  215. */
  216. static int xgpio_probe(struct platform_device *pdev)
  217. {
  218. struct xgpio_instance *chip;
  219. int status = 0;
  220. struct device_node *np = pdev->dev.of_node;
  221. u32 is_dual;
  222. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  223. if (!chip)
  224. return -ENOMEM;
  225. platform_set_drvdata(pdev, chip);
  226. /* Update GPIO state shadow register with default value */
  227. of_property_read_u32(np, "xlnx,dout-default", &chip->gpio_state[0]);
  228. /* Update GPIO direction shadow register with default value */
  229. if (of_property_read_u32(np, "xlnx,tri-default", &chip->gpio_dir[0]))
  230. chip->gpio_dir[0] = 0xFFFFFFFF;
  231. /*
  232. * Check device node and parent device node for device width
  233. * and assume default width of 32
  234. */
  235. if (of_property_read_u32(np, "xlnx,gpio-width", &chip->gpio_width[0]))
  236. chip->gpio_width[0] = 32;
  237. spin_lock_init(&chip->gpio_lock[0]);
  238. if (of_property_read_u32(np, "xlnx,is-dual", &is_dual))
  239. is_dual = 0;
  240. if (is_dual) {
  241. /* Update GPIO state shadow register with default value */
  242. of_property_read_u32(np, "xlnx,dout-default-2",
  243. &chip->gpio_state[1]);
  244. /* Update GPIO direction shadow register with default value */
  245. if (of_property_read_u32(np, "xlnx,tri-default-2",
  246. &chip->gpio_dir[1]))
  247. chip->gpio_dir[1] = 0xFFFFFFFF;
  248. /*
  249. * Check device node and parent device node for device width
  250. * and assume default width of 32
  251. */
  252. if (of_property_read_u32(np, "xlnx,gpio2-width",
  253. &chip->gpio_width[1]))
  254. chip->gpio_width[1] = 32;
  255. spin_lock_init(&chip->gpio_lock[1]);
  256. }
  257. chip->mmchip.gc.ngpio = chip->gpio_width[0] + chip->gpio_width[1];
  258. chip->mmchip.gc.dev = &pdev->dev;
  259. chip->mmchip.gc.direction_input = xgpio_dir_in;
  260. chip->mmchip.gc.direction_output = xgpio_dir_out;
  261. chip->mmchip.gc.get = xgpio_get;
  262. chip->mmchip.gc.set = xgpio_set;
  263. chip->mmchip.save_regs = xgpio_save_regs;
  264. /* Call the OF gpio helper to setup and register the GPIO device */
  265. status = of_mm_gpiochip_add(np, &chip->mmchip);
  266. if (status) {
  267. pr_err("%s: error in probe function with status %d\n",
  268. np->full_name, status);
  269. return status;
  270. }
  271. return 0;
  272. }
  273. static const struct of_device_id xgpio_of_match[] = {
  274. { .compatible = "xlnx,xps-gpio-1.00.a", },
  275. { /* end of list */ },
  276. };
  277. MODULE_DEVICE_TABLE(of, xgpio_of_match);
  278. static struct platform_driver xgpio_plat_driver = {
  279. .probe = xgpio_probe,
  280. .remove = xgpio_remove,
  281. .driver = {
  282. .name = "gpio-xilinx",
  283. .of_match_table = xgpio_of_match,
  284. },
  285. };
  286. static int __init xgpio_init(void)
  287. {
  288. return platform_driver_register(&xgpio_plat_driver);
  289. }
  290. subsys_initcall(xgpio_init);
  291. static void __exit xgpio_exit(void)
  292. {
  293. platform_driver_unregister(&xgpio_plat_driver);
  294. }
  295. module_exit(xgpio_exit);
  296. MODULE_AUTHOR("Xilinx, Inc.");
  297. MODULE_DESCRIPTION("Xilinx GPIO driver");
  298. MODULE_LICENSE("GPL");