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