gef_gpio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Driver for GE Fanuc's FPGA based GPIO pins
  3. *
  4. * Author: Martyn Welch <martyn.welch@gefanuc.com>
  5. *
  6. * 2008 (c) GE Fanuc 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. #define GEF_GPIO_DIRECT 0x00
  28. #define GEF_GPIO_IN 0x04
  29. #define GEF_GPIO_OUT 0x08
  30. #define GEF_GPIO_TRIG 0x0C
  31. #define GEF_GPIO_POLAR_A 0x10
  32. #define GEF_GPIO_POLAR_B 0x14
  33. #define GEF_GPIO_INT_STAT 0x18
  34. #define GEF_GPIO_OVERRUN 0x1C
  35. #define GEF_GPIO_MODE 0x20
  36. static void _gef_gpio_set(void __iomem *reg, unsigned int offset, int value)
  37. {
  38. unsigned int data;
  39. data = ioread32be(reg);
  40. /* value: 0=low; 1=high */
  41. if (value & 0x1)
  42. data = data | (0x1 << offset);
  43. else
  44. data = data & ~(0x1 << offset);
  45. iowrite32be(data, reg);
  46. }
  47. static int gef_gpio_dir_in(struct gpio_chip *chip, unsigned offset)
  48. {
  49. unsigned int data;
  50. struct of_mm_gpio_chip *mmchip = to_of_mm_gpio_chip(chip);
  51. data = ioread32be(mmchip->regs + GEF_GPIO_DIRECT);
  52. data = data | (0x1 << offset);
  53. iowrite32be(data, mmchip->regs + GEF_GPIO_DIRECT);
  54. return 0;
  55. }
  56. static int gef_gpio_dir_out(struct gpio_chip *chip, unsigned offset, int value)
  57. {
  58. unsigned int data;
  59. struct of_mm_gpio_chip *mmchip = to_of_mm_gpio_chip(chip);
  60. /* Set direction before switching to input */
  61. _gef_gpio_set(mmchip->regs + GEF_GPIO_OUT, offset, value);
  62. data = ioread32be(mmchip->regs + GEF_GPIO_DIRECT);
  63. data = data & ~(0x1 << offset);
  64. iowrite32be(data, mmchip->regs + GEF_GPIO_DIRECT);
  65. return 0;
  66. }
  67. static int gef_gpio_get(struct gpio_chip *chip, unsigned offset)
  68. {
  69. unsigned int data;
  70. int state = 0;
  71. struct of_mm_gpio_chip *mmchip = to_of_mm_gpio_chip(chip);
  72. data = ioread32be(mmchip->regs + GEF_GPIO_IN);
  73. state = (int)((data >> offset) & 0x1);
  74. return state;
  75. }
  76. static void gef_gpio_set(struct gpio_chip *chip, unsigned offset, int value)
  77. {
  78. struct of_mm_gpio_chip *mmchip = to_of_mm_gpio_chip(chip);
  79. _gef_gpio_set(mmchip->regs + GEF_GPIO_OUT, offset, value);
  80. }
  81. static int __init gef_gpio_init(void)
  82. {
  83. struct device_node *np;
  84. int retval;
  85. struct of_mm_gpio_chip *gef_gpio_chip;
  86. for_each_compatible_node(np, NULL, "gef,sbc610-gpio") {
  87. pr_debug("%s: Initialising GEF GPIO\n", np->full_name);
  88. /* Allocate chip structure */
  89. gef_gpio_chip = kzalloc(sizeof(*gef_gpio_chip), GFP_KERNEL);
  90. if (!gef_gpio_chip) {
  91. pr_err("%s: Unable to allocate structure\n",
  92. np->full_name);
  93. continue;
  94. }
  95. /* Setup pointers to chip functions */
  96. gef_gpio_chip->of_gc.gpio_cells = 2;
  97. gef_gpio_chip->of_gc.gc.ngpio = 19;
  98. gef_gpio_chip->of_gc.gc.direction_input = gef_gpio_dir_in;
  99. gef_gpio_chip->of_gc.gc.direction_output = gef_gpio_dir_out;
  100. gef_gpio_chip->of_gc.gc.get = gef_gpio_get;
  101. gef_gpio_chip->of_gc.gc.set = gef_gpio_set;
  102. /* This function adds a memory mapped GPIO chip */
  103. retval = of_mm_gpiochip_add(np, gef_gpio_chip);
  104. if (retval) {
  105. kfree(gef_gpio_chip);
  106. pr_err("%s: Unable to add GPIO\n", np->full_name);
  107. }
  108. }
  109. for_each_compatible_node(np, NULL, "gef,sbc310-gpio") {
  110. pr_debug("%s: Initialising GEF GPIO\n", np->full_name);
  111. /* Allocate chip structure */
  112. gef_gpio_chip = kzalloc(sizeof(*gef_gpio_chip), GFP_KERNEL);
  113. if (!gef_gpio_chip) {
  114. pr_err("%s: Unable to allocate structure\n",
  115. np->full_name);
  116. continue;
  117. }
  118. /* Setup pointers to chip functions */
  119. gef_gpio_chip->of_gc.gpio_cells = 2;
  120. gef_gpio_chip->of_gc.gc.ngpio = 6;
  121. gef_gpio_chip->of_gc.gc.direction_input = gef_gpio_dir_in;
  122. gef_gpio_chip->of_gc.gc.direction_output = gef_gpio_dir_out;
  123. gef_gpio_chip->of_gc.gc.get = gef_gpio_get;
  124. gef_gpio_chip->of_gc.gc.set = gef_gpio_set;
  125. /* This function adds a memory mapped GPIO chip */
  126. retval = of_mm_gpiochip_add(np, gef_gpio_chip);
  127. if (retval) {
  128. kfree(gef_gpio_chip);
  129. pr_err("%s: Unable to add GPIO\n", np->full_name);
  130. }
  131. }
  132. return 0;
  133. };
  134. arch_initcall(gef_gpio_init);
  135. MODULE_DESCRIPTION("GE Fanuc I/O FPGA GPIO driver");
  136. MODULE_AUTHOR("Martyn Welch <martyn.welch@gefanuc.com");
  137. MODULE_LICENSE("GPL");