gpio-xilinx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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/driver.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 = gpiochip_get_data(gc);
  84. u32 val;
  85. val = xgpio_readreg(mm_gc->regs + XGPIO_DATA_OFFSET +
  86. xgpio_regoffset(chip, gpio));
  87. return !!(val & BIT(xgpio_offset(chip, gpio)));
  88. }
  89. /**
  90. * xgpio_set - Write the specified signal of the GPIO device.
  91. * @gc: Pointer to gpio_chip device structure.
  92. * @gpio: GPIO signal number.
  93. * @val: Value to be written to specified signal.
  94. *
  95. * This function writes the specified value in to the specified signal of the
  96. * GPIO device.
  97. */
  98. static void xgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  99. {
  100. unsigned long flags;
  101. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  102. struct xgpio_instance *chip = gpiochip_get_data(gc);
  103. int index = xgpio_index(chip, gpio);
  104. int offset = xgpio_offset(chip, gpio);
  105. spin_lock_irqsave(&chip->gpio_lock[index], flags);
  106. /* Write to GPIO signal and set its direction to output */
  107. if (val)
  108. chip->gpio_state[index] |= BIT(offset);
  109. else
  110. chip->gpio_state[index] &= ~BIT(offset);
  111. xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET +
  112. xgpio_regoffset(chip, gpio), chip->gpio_state[index]);
  113. spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
  114. }
  115. /**
  116. * xgpio_set_multiple - Write the specified signals of the GPIO device.
  117. * @gc: Pointer to gpio_chip device structure.
  118. * @mask: Mask of the GPIOS to modify.
  119. * @bits: Value to be wrote on each GPIO
  120. *
  121. * This function writes the specified values into the specified signals of the
  122. * GPIO devices.
  123. */
  124. static void xgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask,
  125. unsigned long *bits)
  126. {
  127. unsigned long flags;
  128. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  129. struct xgpio_instance *chip = gpiochip_get_data(gc);
  130. int index = xgpio_index(chip, 0);
  131. int offset, i;
  132. spin_lock_irqsave(&chip->gpio_lock[index], flags);
  133. /* Write to GPIO signals */
  134. for (i = 0; i < gc->ngpio; i++) {
  135. if (*mask == 0)
  136. break;
  137. if (index != xgpio_index(chip, i)) {
  138. xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET +
  139. xgpio_regoffset(chip, i),
  140. chip->gpio_state[index]);
  141. spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
  142. index = xgpio_index(chip, i);
  143. spin_lock_irqsave(&chip->gpio_lock[index], flags);
  144. }
  145. if (__test_and_clear_bit(i, mask)) {
  146. offset = xgpio_offset(chip, i);
  147. if (test_bit(i, bits))
  148. chip->gpio_state[index] |= BIT(offset);
  149. else
  150. chip->gpio_state[index] &= ~BIT(offset);
  151. }
  152. }
  153. xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET +
  154. xgpio_regoffset(chip, i), chip->gpio_state[index]);
  155. spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
  156. }
  157. /**
  158. * xgpio_dir_in - Set the direction of the specified GPIO signal as input.
  159. * @gc: Pointer to gpio_chip device structure.
  160. * @gpio: GPIO signal number.
  161. *
  162. * Return:
  163. * 0 - if direction of GPIO signals is set as input
  164. * otherwise it returns negative error value.
  165. */
  166. static int xgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  167. {
  168. unsigned long flags;
  169. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  170. struct xgpio_instance *chip = gpiochip_get_data(gc);
  171. int index = xgpio_index(chip, gpio);
  172. int offset = xgpio_offset(chip, gpio);
  173. spin_lock_irqsave(&chip->gpio_lock[index], flags);
  174. /* Set the GPIO bit in shadow register and set direction as input */
  175. chip->gpio_dir[index] |= BIT(offset);
  176. xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET +
  177. xgpio_regoffset(chip, gpio), chip->gpio_dir[index]);
  178. spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
  179. return 0;
  180. }
  181. /**
  182. * xgpio_dir_out - Set the direction of the specified GPIO signal as output.
  183. * @gc: Pointer to gpio_chip device structure.
  184. * @gpio: GPIO signal number.
  185. * @val: Value to be written to specified signal.
  186. *
  187. * This function sets the direction of specified GPIO signal as output.
  188. *
  189. * Return:
  190. * If all GPIO signals of GPIO chip is configured as input then it returns
  191. * error otherwise it returns 0.
  192. */
  193. static int xgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  194. {
  195. unsigned long flags;
  196. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  197. struct xgpio_instance *chip = gpiochip_get_data(gc);
  198. int index = xgpio_index(chip, gpio);
  199. int offset = xgpio_offset(chip, gpio);
  200. spin_lock_irqsave(&chip->gpio_lock[index], flags);
  201. /* Write state of GPIO signal */
  202. if (val)
  203. chip->gpio_state[index] |= BIT(offset);
  204. else
  205. chip->gpio_state[index] &= ~BIT(offset);
  206. xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET +
  207. xgpio_regoffset(chip, gpio), chip->gpio_state[index]);
  208. /* Clear the GPIO bit in shadow register and set direction as output */
  209. chip->gpio_dir[index] &= ~BIT(offset);
  210. xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET +
  211. xgpio_regoffset(chip, gpio), chip->gpio_dir[index]);
  212. spin_unlock_irqrestore(&chip->gpio_lock[index], flags);
  213. return 0;
  214. }
  215. /**
  216. * xgpio_save_regs - Set initial values of GPIO pins
  217. * @mm_gc: Pointer to memory mapped GPIO chip structure
  218. */
  219. static void xgpio_save_regs(struct of_mm_gpio_chip *mm_gc)
  220. {
  221. struct xgpio_instance *chip =
  222. container_of(mm_gc, struct xgpio_instance, mmchip);
  223. xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET, chip->gpio_state[0]);
  224. xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET, chip->gpio_dir[0]);
  225. if (!chip->gpio_width[1])
  226. return;
  227. xgpio_writereg(mm_gc->regs + XGPIO_DATA_OFFSET + XGPIO_CHANNEL_OFFSET,
  228. chip->gpio_state[1]);
  229. xgpio_writereg(mm_gc->regs + XGPIO_TRI_OFFSET + XGPIO_CHANNEL_OFFSET,
  230. chip->gpio_dir[1]);
  231. }
  232. /**
  233. * xgpio_remove - Remove method for the GPIO device.
  234. * @pdev: pointer to the platform device
  235. *
  236. * This function remove gpiochips and frees all the allocated resources.
  237. *
  238. * Return: 0 always
  239. */
  240. static int xgpio_remove(struct platform_device *pdev)
  241. {
  242. struct xgpio_instance *chip = platform_get_drvdata(pdev);
  243. of_mm_gpiochip_remove(&chip->mmchip);
  244. return 0;
  245. }
  246. /**
  247. * xgpio_of_probe - Probe method for the GPIO device.
  248. * @pdev: pointer to the platform device
  249. *
  250. * Return:
  251. * It returns 0, if the driver is bound to the GPIO device, or
  252. * a negative value if there is an error.
  253. */
  254. static int xgpio_probe(struct platform_device *pdev)
  255. {
  256. struct xgpio_instance *chip;
  257. int status = 0;
  258. struct device_node *np = pdev->dev.of_node;
  259. u32 is_dual;
  260. chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
  261. if (!chip)
  262. return -ENOMEM;
  263. platform_set_drvdata(pdev, chip);
  264. /* Update GPIO state shadow register with default value */
  265. of_property_read_u32(np, "xlnx,dout-default", &chip->gpio_state[0]);
  266. /* Update GPIO direction shadow register with default value */
  267. if (of_property_read_u32(np, "xlnx,tri-default", &chip->gpio_dir[0]))
  268. chip->gpio_dir[0] = 0xFFFFFFFF;
  269. /*
  270. * Check device node and parent device node for device width
  271. * and assume default width of 32
  272. */
  273. if (of_property_read_u32(np, "xlnx,gpio-width", &chip->gpio_width[0]))
  274. chip->gpio_width[0] = 32;
  275. spin_lock_init(&chip->gpio_lock[0]);
  276. if (of_property_read_u32(np, "xlnx,is-dual", &is_dual))
  277. is_dual = 0;
  278. if (is_dual) {
  279. /* Update GPIO state shadow register with default value */
  280. of_property_read_u32(np, "xlnx,dout-default-2",
  281. &chip->gpio_state[1]);
  282. /* Update GPIO direction shadow register with default value */
  283. if (of_property_read_u32(np, "xlnx,tri-default-2",
  284. &chip->gpio_dir[1]))
  285. chip->gpio_dir[1] = 0xFFFFFFFF;
  286. /*
  287. * Check device node and parent device node for device width
  288. * and assume default width of 32
  289. */
  290. if (of_property_read_u32(np, "xlnx,gpio2-width",
  291. &chip->gpio_width[1]))
  292. chip->gpio_width[1] = 32;
  293. spin_lock_init(&chip->gpio_lock[1]);
  294. }
  295. chip->mmchip.gc.ngpio = chip->gpio_width[0] + chip->gpio_width[1];
  296. chip->mmchip.gc.parent = &pdev->dev;
  297. chip->mmchip.gc.direction_input = xgpio_dir_in;
  298. chip->mmchip.gc.direction_output = xgpio_dir_out;
  299. chip->mmchip.gc.get = xgpio_get;
  300. chip->mmchip.gc.set = xgpio_set;
  301. chip->mmchip.gc.set_multiple = xgpio_set_multiple;
  302. chip->mmchip.save_regs = xgpio_save_regs;
  303. /* Call the OF gpio helper to setup and register the GPIO device */
  304. status = of_mm_gpiochip_add_data(np, &chip->mmchip, chip);
  305. if (status) {
  306. pr_err("%pOF: error in probe function with status %d\n",
  307. np, status);
  308. return status;
  309. }
  310. return 0;
  311. }
  312. static const struct of_device_id xgpio_of_match[] = {
  313. { .compatible = "xlnx,xps-gpio-1.00.a", },
  314. { /* end of list */ },
  315. };
  316. MODULE_DEVICE_TABLE(of, xgpio_of_match);
  317. static struct platform_driver xgpio_plat_driver = {
  318. .probe = xgpio_probe,
  319. .remove = xgpio_remove,
  320. .driver = {
  321. .name = "gpio-xilinx",
  322. .of_match_table = xgpio_of_match,
  323. },
  324. };
  325. static int __init xgpio_init(void)
  326. {
  327. return platform_driver_register(&xgpio_plat_driver);
  328. }
  329. subsys_initcall(xgpio_init);
  330. static void __exit xgpio_exit(void)
  331. {
  332. platform_driver_unregister(&xgpio_plat_driver);
  333. }
  334. module_exit(xgpio_exit);
  335. MODULE_AUTHOR("Xilinx, Inc.");
  336. MODULE_DESCRIPTION("Xilinx GPIO driver");
  337. MODULE_LICENSE("GPL");