gef_gpio.c 4.4 KB

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