gpio-ge.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Driver for GE FPGA based GPIO
  3. *
  4. * Author: Martyn Welch <martyn.welch@ge.com>
  5. *
  6. * 2008 (c) GE Intelligent Platforms Embedded Systems, Inc.
  7. *
  8. * This file is licensed under the terms of the GNU General Public License
  9. * version 2. This program is licensed "as is" without any warranty of any
  10. * kind, whether express or implied.
  11. */
  12. /* TODO
  13. *
  14. * Configuration of output modes (totem-pole/open-drain)
  15. * Interrupt configuration - interrupts are always generated the FPGA relies on
  16. * the I/O interrupt controllers mask to stop them propergating
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/compiler.h>
  20. #include <linux/init.h>
  21. #include <linux/io.h>
  22. #include <linux/of.h>
  23. #include <linux/of_device.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/of_gpio.h>
  26. #include <linux/gpio.h>
  27. #include <linux/slab.h>
  28. #include <linux/module.h>
  29. #define GEF_GPIO_DIRECT 0x00
  30. #define GEF_GPIO_IN 0x04
  31. #define GEF_GPIO_OUT 0x08
  32. #define GEF_GPIO_TRIG 0x0C
  33. #define GEF_GPIO_POLAR_A 0x10
  34. #define GEF_GPIO_POLAR_B 0x14
  35. #define GEF_GPIO_INT_STAT 0x18
  36. #define GEF_GPIO_OVERRUN 0x1C
  37. #define GEF_GPIO_MODE 0x20
  38. static void _gef_gpio_set(void __iomem *reg, unsigned int offset, int value)
  39. {
  40. unsigned int data;
  41. data = ioread32be(reg);
  42. /* value: 0=low; 1=high */
  43. if (value & 0x1)
  44. data = data | (0x1 << offset);
  45. else
  46. data = data & ~(0x1 << offset);
  47. iowrite32be(data, reg);
  48. }
  49. static int gef_gpio_dir_in(struct gpio_chip *chip, unsigned offset)
  50. {
  51. unsigned int data;
  52. struct of_mm_gpio_chip *mmchip = to_of_mm_gpio_chip(chip);
  53. data = ioread32be(mmchip->regs + GEF_GPIO_DIRECT);
  54. data = data | (0x1 << offset);
  55. iowrite32be(data, mmchip->regs + GEF_GPIO_DIRECT);
  56. return 0;
  57. }
  58. static int gef_gpio_dir_out(struct gpio_chip *chip, unsigned offset, int value)
  59. {
  60. unsigned int data;
  61. struct of_mm_gpio_chip *mmchip = to_of_mm_gpio_chip(chip);
  62. /* Set direction before switching to input */
  63. _gef_gpio_set(mmchip->regs + GEF_GPIO_OUT, offset, value);
  64. data = ioread32be(mmchip->regs + GEF_GPIO_DIRECT);
  65. data = data & ~(0x1 << offset);
  66. iowrite32be(data, mmchip->regs + GEF_GPIO_DIRECT);
  67. return 0;
  68. }
  69. static int gef_gpio_get(struct gpio_chip *chip, unsigned offset)
  70. {
  71. unsigned int data;
  72. int state = 0;
  73. struct of_mm_gpio_chip *mmchip = to_of_mm_gpio_chip(chip);
  74. data = ioread32be(mmchip->regs + GEF_GPIO_IN);
  75. state = (int)((data >> offset) & 0x1);
  76. return state;
  77. }
  78. static void gef_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  79. {
  80. struct of_mm_gpio_chip *mmchip = to_of_mm_gpio_chip(chip);
  81. _gef_gpio_set(mmchip->regs + GEF_GPIO_OUT, offset, value);
  82. }
  83. static int __init gef_gpio_init(void)
  84. {
  85. struct device_node *np;
  86. int retval;
  87. struct of_mm_gpio_chip *gef_gpio_chip;
  88. for_each_compatible_node(np, NULL, "gef,sbc610-gpio") {
  89. pr_debug("%s: Initialising GEF GPIO\n", np->full_name);
  90. /* Allocate chip structure */
  91. gef_gpio_chip = kzalloc(sizeof(*gef_gpio_chip), GFP_KERNEL);
  92. if (!gef_gpio_chip) {
  93. pr_err("%s: Unable to allocate structure\n",
  94. np->full_name);
  95. continue;
  96. }
  97. /* Setup pointers to chip functions */
  98. gef_gpio_chip->gc.of_gpio_n_cells = 2;
  99. gef_gpio_chip->gc.ngpio = 19;
  100. gef_gpio_chip->gc.direction_input = gef_gpio_dir_in;
  101. gef_gpio_chip->gc.direction_output = gef_gpio_dir_out;
  102. gef_gpio_chip->gc.get = gef_gpio_get;
  103. gef_gpio_chip->gc.set = gef_gpio_set;
  104. /* This function adds a memory mapped GPIO chip */
  105. retval = of_mm_gpiochip_add(np, gef_gpio_chip);
  106. if (retval) {
  107. kfree(gef_gpio_chip);
  108. pr_err("%s: Unable to add GPIO\n", np->full_name);
  109. }
  110. }
  111. for_each_compatible_node(np, NULL, "gef,sbc310-gpio") {
  112. pr_debug("%s: Initialising GEF GPIO\n", np->full_name);
  113. /* Allocate chip structure */
  114. gef_gpio_chip = kzalloc(sizeof(*gef_gpio_chip), GFP_KERNEL);
  115. if (!gef_gpio_chip) {
  116. pr_err("%s: Unable to allocate structure\n",
  117. np->full_name);
  118. continue;
  119. }
  120. /* Setup pointers to chip functions */
  121. gef_gpio_chip->gc.of_gpio_n_cells = 2;
  122. gef_gpio_chip->gc.ngpio = 6;
  123. gef_gpio_chip->gc.direction_input = gef_gpio_dir_in;
  124. gef_gpio_chip->gc.direction_output = gef_gpio_dir_out;
  125. gef_gpio_chip->gc.get = gef_gpio_get;
  126. gef_gpio_chip->gc.set = gef_gpio_set;
  127. /* This function adds a memory mapped GPIO chip */
  128. retval = of_mm_gpiochip_add(np, gef_gpio_chip);
  129. if (retval) {
  130. kfree(gef_gpio_chip);
  131. pr_err("%s: Unable to add GPIO\n", np->full_name);
  132. }
  133. }
  134. for_each_compatible_node(np, NULL, "ge,imp3a-gpio") {
  135. pr_debug("%s: Initialising GE GPIO\n", np->full_name);
  136. /* Allocate chip structure */
  137. gef_gpio_chip = kzalloc(sizeof(*gef_gpio_chip), GFP_KERNEL);
  138. if (!gef_gpio_chip) {
  139. pr_err("%s: Unable to allocate structure\n",
  140. np->full_name);
  141. continue;
  142. }
  143. /* Setup pointers to chip functions */
  144. gef_gpio_chip->gc.of_gpio_n_cells = 2;
  145. gef_gpio_chip->gc.ngpio = 16;
  146. gef_gpio_chip->gc.direction_input = gef_gpio_dir_in;
  147. gef_gpio_chip->gc.direction_output = gef_gpio_dir_out;
  148. gef_gpio_chip->gc.get = gef_gpio_get;
  149. gef_gpio_chip->gc.set = gef_gpio_set;
  150. /* This function adds a memory mapped GPIO chip */
  151. retval = of_mm_gpiochip_add(np, gef_gpio_chip);
  152. if (retval) {
  153. kfree(gef_gpio_chip);
  154. pr_err("%s: Unable to add GPIO\n", np->full_name);
  155. }
  156. }
  157. return 0;
  158. };
  159. arch_initcall(gef_gpio_init);
  160. MODULE_DESCRIPTION("GE I/O FPGA GPIO driver");
  161. MODULE_AUTHOR("Martyn Welch <martyn.welch@ge.com");
  162. MODULE_LICENSE("GPL");