xilinx_gpio.c 7.0 KB

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