xilinx_gpio.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Xilinx gpio driver
  3. *
  4. * Copyright 2008 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/init.h>
  15. #include <linux/errno.h>
  16. #include <linux/of_device.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/io.h>
  20. #include <linux/gpio.h>
  21. #include <linux/slab.h>
  22. /* Register Offset Definitions */
  23. #define XGPIO_DATA_OFFSET (0x0) /* Data register */
  24. #define XGPIO_TRI_OFFSET (0x4) /* I/O direction register */
  25. struct xgpio_instance {
  26. struct of_mm_gpio_chip mmchip;
  27. u32 gpio_state; /* GPIO state shadow register */
  28. u32 gpio_dir; /* GPIO direction shadow register */
  29. spinlock_t gpio_lock; /* Lock used for synchronization */
  30. };
  31. /**
  32. * xgpio_get - Read the specified signal of the GPIO device.
  33. * @gc: Pointer to gpio_chip device structure.
  34. * @gpio: GPIO signal number.
  35. *
  36. * This function reads the specified signal of the GPIO device. It returns 0 if
  37. * the signal clear, 1 if signal is set or negative value on error.
  38. */
  39. static int xgpio_get(struct gpio_chip *gc, unsigned int gpio)
  40. {
  41. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  42. return (in_be32(mm_gc->regs + XGPIO_DATA_OFFSET) >> gpio) & 1;
  43. }
  44. /**
  45. * xgpio_set - Write the specified signal of the GPIO device.
  46. * @gc: Pointer to gpio_chip device structure.
  47. * @gpio: GPIO signal number.
  48. * @val: Value to be written to specified signal.
  49. *
  50. * This function writes the specified value in to the specified signal of the
  51. * GPIO device.
  52. */
  53. static void xgpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
  54. {
  55. unsigned long flags;
  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. spin_lock_irqsave(&chip->gpio_lock, flags);
  60. /* Write to GPIO signal and set its direction to output */
  61. if (val)
  62. chip->gpio_state |= 1 << gpio;
  63. else
  64. chip->gpio_state &= ~(1 << gpio);
  65. out_be32(mm_gc->regs + XGPIO_DATA_OFFSET, chip->gpio_state);
  66. spin_unlock_irqrestore(&chip->gpio_lock, flags);
  67. }
  68. /**
  69. * xgpio_dir_in - Set the direction of the specified GPIO signal as input.
  70. * @gc: Pointer to gpio_chip device structure.
  71. * @gpio: GPIO signal number.
  72. *
  73. * This function sets the direction of specified GPIO signal as input.
  74. * It returns 0 if direction of GPIO signals is set as input otherwise it
  75. * returns negative error value.
  76. */
  77. static int xgpio_dir_in(struct gpio_chip *gc, unsigned int gpio)
  78. {
  79. unsigned long flags;
  80. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  81. struct xgpio_instance *chip =
  82. container_of(mm_gc, struct xgpio_instance, mmchip);
  83. spin_lock_irqsave(&chip->gpio_lock, flags);
  84. /* Set the GPIO bit in shadow register and set direction as input */
  85. chip->gpio_dir |= (1 << gpio);
  86. out_be32(mm_gc->regs + XGPIO_TRI_OFFSET, chip->gpio_dir);
  87. spin_unlock_irqrestore(&chip->gpio_lock, flags);
  88. return 0;
  89. }
  90. /**
  91. * xgpio_dir_out - Set the direction of the specified GPIO signal as output.
  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 sets the direction of specified GPIO signal as output. If all
  97. * GPIO signals of GPIO chip is configured as input then it returns
  98. * error otherwise it returns 0.
  99. */
  100. static int xgpio_dir_out(struct gpio_chip *gc, unsigned int gpio, int val)
  101. {
  102. unsigned long flags;
  103. struct of_mm_gpio_chip *mm_gc = to_of_mm_gpio_chip(gc);
  104. struct xgpio_instance *chip =
  105. container_of(mm_gc, struct xgpio_instance, mmchip);
  106. spin_lock_irqsave(&chip->gpio_lock, flags);
  107. /* Write state of GPIO signal */
  108. if (val)
  109. chip->gpio_state |= 1 << gpio;
  110. else
  111. chip->gpio_state &= ~(1 << gpio);
  112. out_be32(mm_gc->regs + XGPIO_DATA_OFFSET, chip->gpio_state);
  113. /* Clear the GPIO bit in shadow register and set direction as output */
  114. chip->gpio_dir &= (~(1 << gpio));
  115. out_be32(mm_gc->regs + XGPIO_TRI_OFFSET, chip->gpio_dir);
  116. spin_unlock_irqrestore(&chip->gpio_lock, flags);
  117. return 0;
  118. }
  119. /**
  120. * xgpio_save_regs - Set initial values of GPIO pins
  121. * @mm_gc: pointer to memory mapped GPIO chip structure
  122. */
  123. static void xgpio_save_regs(struct of_mm_gpio_chip *mm_gc)
  124. {
  125. struct xgpio_instance *chip =
  126. container_of(mm_gc, struct xgpio_instance, mmchip);
  127. out_be32(mm_gc->regs + XGPIO_DATA_OFFSET, chip->gpio_state);
  128. out_be32(mm_gc->regs + XGPIO_TRI_OFFSET, chip->gpio_dir);
  129. }
  130. /**
  131. * xgpio_of_probe - Probe method for the GPIO device.
  132. * @np: pointer to device tree node
  133. *
  134. * This function probes the GPIO device in the device tree. It initializes the
  135. * driver data structure. It returns 0, if the driver is bound to the GPIO
  136. * device, or a negative value if there is an error.
  137. */
  138. static int __devinit xgpio_of_probe(struct device_node *np)
  139. {
  140. struct xgpio_instance *chip;
  141. int status = 0;
  142. const u32 *tree_info;
  143. chip = kzalloc(sizeof(*chip), GFP_KERNEL);
  144. if (!chip)
  145. return -ENOMEM;
  146. /* Update GPIO state shadow register with default value */
  147. tree_info = of_get_property(np, "xlnx,dout-default", NULL);
  148. if (tree_info)
  149. chip->gpio_state = be32_to_cpup(tree_info);
  150. /* Update GPIO direction shadow register with default value */
  151. chip->gpio_dir = 0xFFFFFFFF; /* By default, all pins are inputs */
  152. tree_info = of_get_property(np, "xlnx,tri-default", NULL);
  153. if (tree_info)
  154. chip->gpio_dir = be32_to_cpup(tree_info);
  155. /* Check device node and parent device node for device width */
  156. chip->mmchip.gc.ngpio = 32; /* By default assume full GPIO controller */
  157. tree_info = of_get_property(np, "xlnx,gpio-width", NULL);
  158. if (!tree_info)
  159. tree_info = of_get_property(np->parent,
  160. "xlnx,gpio-width", NULL);
  161. if (tree_info)
  162. chip->mmchip.gc.ngpio = be32_to_cpup(tree_info);
  163. spin_lock_init(&chip->gpio_lock);
  164. chip->mmchip.gc.direction_input = xgpio_dir_in;
  165. chip->mmchip.gc.direction_output = xgpio_dir_out;
  166. chip->mmchip.gc.get = xgpio_get;
  167. chip->mmchip.gc.set = xgpio_set;
  168. chip->mmchip.save_regs = xgpio_save_regs;
  169. /* Call the OF gpio helper to setup and register the GPIO device */
  170. status = of_mm_gpiochip_add(np, &chip->mmchip);
  171. if (status) {
  172. kfree(chip);
  173. pr_err("%s: error in probe function with status %d\n",
  174. np->full_name, status);
  175. return status;
  176. }
  177. pr_info("XGpio: %s: registered\n", np->full_name);
  178. return 0;
  179. }
  180. static struct of_device_id xgpio_of_match[] __devinitdata = {
  181. { .compatible = "xlnx,xps-gpio-1.00.a", },
  182. { /* end of list */ },
  183. };
  184. static int __init xgpio_init(void)
  185. {
  186. struct device_node *np;
  187. for_each_matching_node(np, xgpio_of_match)
  188. xgpio_of_probe(np);
  189. return 0;
  190. }
  191. /* Make sure we get initialized before anyone else tries to use us */
  192. subsys_initcall(xgpio_init);
  193. /* No exit call at the moment as we cannot unregister of GPIO chips */
  194. MODULE_AUTHOR("Xilinx, Inc.");
  195. MODULE_DESCRIPTION("Xilinx GPIO driver");
  196. MODULE_LICENSE("GPL");