gpio.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Coldfire generic GPIO support.
  4. *
  5. * (C) Copyright 2009, Steven King <[email protected]>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/device.h>
  11. #include <linux/gpio/driver.h>
  12. #include <linux/io.h>
  13. #include <asm/coldfire.h>
  14. #include <asm/mcfsim.h>
  15. #include <asm/mcfgpio.h>
  16. int __mcfgpio_get_value(unsigned gpio)
  17. {
  18. return mcfgpio_read(__mcfgpio_ppdr(gpio)) & mcfgpio_bit(gpio);
  19. }
  20. EXPORT_SYMBOL(__mcfgpio_get_value);
  21. void __mcfgpio_set_value(unsigned gpio, int value)
  22. {
  23. if (gpio < MCFGPIO_SCR_START) {
  24. unsigned long flags;
  25. MCFGPIO_PORTTYPE data;
  26. local_irq_save(flags);
  27. data = mcfgpio_read(__mcfgpio_podr(gpio));
  28. if (value)
  29. data |= mcfgpio_bit(gpio);
  30. else
  31. data &= ~mcfgpio_bit(gpio);
  32. mcfgpio_write(data, __mcfgpio_podr(gpio));
  33. local_irq_restore(flags);
  34. } else {
  35. if (value)
  36. mcfgpio_write(mcfgpio_bit(gpio),
  37. MCFGPIO_SETR_PORT(gpio));
  38. else
  39. mcfgpio_write(~mcfgpio_bit(gpio),
  40. MCFGPIO_CLRR_PORT(gpio));
  41. }
  42. }
  43. EXPORT_SYMBOL(__mcfgpio_set_value);
  44. int __mcfgpio_direction_input(unsigned gpio)
  45. {
  46. unsigned long flags;
  47. MCFGPIO_PORTTYPE dir;
  48. local_irq_save(flags);
  49. dir = mcfgpio_read(__mcfgpio_pddr(gpio));
  50. dir &= ~mcfgpio_bit(gpio);
  51. mcfgpio_write(dir, __mcfgpio_pddr(gpio));
  52. local_irq_restore(flags);
  53. return 0;
  54. }
  55. EXPORT_SYMBOL(__mcfgpio_direction_input);
  56. int __mcfgpio_direction_output(unsigned gpio, int value)
  57. {
  58. unsigned long flags;
  59. MCFGPIO_PORTTYPE data;
  60. local_irq_save(flags);
  61. data = mcfgpio_read(__mcfgpio_pddr(gpio));
  62. data |= mcfgpio_bit(gpio);
  63. mcfgpio_write(data, __mcfgpio_pddr(gpio));
  64. /* now set the data to output */
  65. if (gpio < MCFGPIO_SCR_START) {
  66. data = mcfgpio_read(__mcfgpio_podr(gpio));
  67. if (value)
  68. data |= mcfgpio_bit(gpio);
  69. else
  70. data &= ~mcfgpio_bit(gpio);
  71. mcfgpio_write(data, __mcfgpio_podr(gpio));
  72. } else {
  73. if (value)
  74. mcfgpio_write(mcfgpio_bit(gpio),
  75. MCFGPIO_SETR_PORT(gpio));
  76. else
  77. mcfgpio_write(~mcfgpio_bit(gpio),
  78. MCFGPIO_CLRR_PORT(gpio));
  79. }
  80. local_irq_restore(flags);
  81. return 0;
  82. }
  83. EXPORT_SYMBOL(__mcfgpio_direction_output);
  84. int __mcfgpio_request(unsigned gpio)
  85. {
  86. return 0;
  87. }
  88. EXPORT_SYMBOL(__mcfgpio_request);
  89. void __mcfgpio_free(unsigned gpio)
  90. {
  91. __mcfgpio_direction_input(gpio);
  92. }
  93. EXPORT_SYMBOL(__mcfgpio_free);
  94. #ifdef CONFIG_GPIOLIB
  95. static int mcfgpio_direction_input(struct gpio_chip *chip, unsigned offset)
  96. {
  97. return __mcfgpio_direction_input(offset);
  98. }
  99. static int mcfgpio_get_value(struct gpio_chip *chip, unsigned offset)
  100. {
  101. return !!__mcfgpio_get_value(offset);
  102. }
  103. static int mcfgpio_direction_output(struct gpio_chip *chip, unsigned offset,
  104. int value)
  105. {
  106. return __mcfgpio_direction_output(offset, value);
  107. }
  108. static void mcfgpio_set_value(struct gpio_chip *chip, unsigned offset,
  109. int value)
  110. {
  111. __mcfgpio_set_value(offset, value);
  112. }
  113. static int mcfgpio_request(struct gpio_chip *chip, unsigned offset)
  114. {
  115. return __mcfgpio_request(offset);
  116. }
  117. static void mcfgpio_free(struct gpio_chip *chip, unsigned offset)
  118. {
  119. __mcfgpio_free(offset);
  120. }
  121. static int mcfgpio_to_irq(struct gpio_chip *chip, unsigned offset)
  122. {
  123. #if defined(MCFGPIO_IRQ_MIN)
  124. if ((offset >= MCFGPIO_IRQ_MIN) && (offset < MCFGPIO_IRQ_MAX))
  125. #else
  126. if (offset < MCFGPIO_IRQ_MAX)
  127. #endif
  128. return MCFGPIO_IRQ_VECBASE + offset;
  129. else
  130. return -EINVAL;
  131. }
  132. static struct gpio_chip mcfgpio_chip = {
  133. .label = "mcfgpio",
  134. .request = mcfgpio_request,
  135. .free = mcfgpio_free,
  136. .direction_input = mcfgpio_direction_input,
  137. .direction_output = mcfgpio_direction_output,
  138. .get = mcfgpio_get_value,
  139. .set = mcfgpio_set_value,
  140. .to_irq = mcfgpio_to_irq,
  141. .base = 0,
  142. .ngpio = MCFGPIO_PIN_MAX,
  143. };
  144. static int __init mcfgpio_sysinit(void)
  145. {
  146. return gpiochip_add_data(&mcfgpio_chip, NULL);
  147. }
  148. core_initcall(mcfgpio_sysinit);
  149. #endif