gpio.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Coldfire generic GPIO support.
  3. *
  4. * (C) Copyright 2009, Steven King <sfking@fdwdc.com>
  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 as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/device.h>
  19. #include <linux/io.h>
  20. #include <asm/coldfire.h>
  21. #include <asm/mcfsim.h>
  22. #include <asm/mcfgpio.h>
  23. int __mcfgpio_get_value(unsigned gpio)
  24. {
  25. return mcfgpio_read(__mcfgpio_ppdr(gpio)) & mcfgpio_bit(gpio);
  26. }
  27. EXPORT_SYMBOL(__mcfgpio_get_value);
  28. void __mcfgpio_set_value(unsigned gpio, int value)
  29. {
  30. if (gpio < MCFGPIO_SCR_START) {
  31. unsigned long flags;
  32. MCFGPIO_PORTTYPE data;
  33. local_irq_save(flags);
  34. data = mcfgpio_read(__mcfgpio_podr(gpio));
  35. if (value)
  36. data |= mcfgpio_bit(gpio);
  37. else
  38. data &= ~mcfgpio_bit(gpio);
  39. mcfgpio_write(data, __mcfgpio_podr(gpio));
  40. local_irq_restore(flags);
  41. } else {
  42. if (value)
  43. mcfgpio_write(mcfgpio_bit(gpio),
  44. MCFGPIO_SETR_PORT(gpio));
  45. else
  46. mcfgpio_write(~mcfgpio_bit(gpio),
  47. MCFGPIO_CLRR_PORT(gpio));
  48. }
  49. }
  50. EXPORT_SYMBOL(__mcfgpio_set_value);
  51. int __mcfgpio_direction_input(unsigned gpio)
  52. {
  53. unsigned long flags;
  54. MCFGPIO_PORTTYPE dir;
  55. local_irq_save(flags);
  56. dir = mcfgpio_read(__mcfgpio_pddr(gpio));
  57. dir &= ~mcfgpio_bit(gpio);
  58. mcfgpio_write(dir, __mcfgpio_pddr(gpio));
  59. local_irq_restore(flags);
  60. return 0;
  61. }
  62. EXPORT_SYMBOL(__mcfgpio_direction_input);
  63. int __mcfgpio_direction_output(unsigned gpio, int value)
  64. {
  65. unsigned long flags;
  66. MCFGPIO_PORTTYPE data;
  67. local_irq_save(flags);
  68. data = mcfgpio_read(__mcfgpio_pddr(gpio));
  69. if (value)
  70. data |= mcfgpio_bit(gpio);
  71. else
  72. data &= mcfgpio_bit(gpio);
  73. mcfgpio_write(data, __mcfgpio_pddr(gpio));
  74. /* now set the data to output */
  75. if (gpio < MCFGPIO_SCR_START) {
  76. data = mcfgpio_read(__mcfgpio_podr(gpio));
  77. if (value)
  78. data |= mcfgpio_bit(gpio);
  79. else
  80. data &= ~mcfgpio_bit(gpio);
  81. mcfgpio_write(data, __mcfgpio_podr(gpio));
  82. } else {
  83. if (value)
  84. mcfgpio_write(mcfgpio_bit(gpio),
  85. MCFGPIO_SETR_PORT(gpio));
  86. else
  87. mcfgpio_write(~mcfgpio_bit(gpio),
  88. MCFGPIO_CLRR_PORT(gpio));
  89. }
  90. local_irq_restore(flags);
  91. return 0;
  92. }
  93. EXPORT_SYMBOL(__mcfgpio_direction_output);
  94. int __mcfgpio_request(unsigned gpio)
  95. {
  96. return 0;
  97. }
  98. EXPORT_SYMBOL(__mcfgpio_request);
  99. void __mcfgpio_free(unsigned gpio)
  100. {
  101. __mcfgpio_direction_input(gpio);
  102. }
  103. EXPORT_SYMBOL(__mcfgpio_free);
  104. #ifdef CONFIG_GPIOLIB
  105. int mcfgpio_direction_input(struct gpio_chip *chip, unsigned offset)
  106. {
  107. return __mcfgpio_direction_input(offset);
  108. }
  109. int mcfgpio_get_value(struct gpio_chip *chip, unsigned offset)
  110. {
  111. return __mcfgpio_get_value(offset);
  112. }
  113. int mcfgpio_direction_output(struct gpio_chip *chip, unsigned offset, int value)
  114. {
  115. return __mcfgpio_direction_output(offset, value);
  116. }
  117. void mcfgpio_set_value(struct gpio_chip *chip, unsigned offset, int value)
  118. {
  119. __mcfgpio_set_value(offset, value);
  120. }
  121. int mcfgpio_request(struct gpio_chip *chip, unsigned offset)
  122. {
  123. return __mcfgpio_request(offset);
  124. }
  125. void mcfgpio_free(struct gpio_chip *chip, unsigned offset)
  126. {
  127. __mcfgpio_free(offset);
  128. }
  129. struct bus_type mcfgpio_subsys = {
  130. .name = "gpio",
  131. .dev_name = "gpio",
  132. };
  133. static struct gpio_chip mcfgpio_chip = {
  134. .label = "mcfgpio",
  135. .request = mcfgpio_request,
  136. .free = mcfgpio_free,
  137. .direction_input = mcfgpio_direction_input,
  138. .direction_output = mcfgpio_direction_output,
  139. .get = mcfgpio_get_value,
  140. .set = mcfgpio_set_value,
  141. .base = 0,
  142. .ngpio = MCFGPIO_PIN_MAX,
  143. };
  144. static int __init mcfgpio_sysinit(void)
  145. {
  146. gpiochip_add(&mcfgpio_chip);
  147. return subsys_system_register(&mcfgpio_subsys, NULL);
  148. }
  149. core_initcall(mcfgpio_sysinit);
  150. #endif