slot-gpio.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Generic GPIO card-detect helper
  4. *
  5. * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  6. */
  7. #include <linux/err.h>
  8. #include <linux/gpio/consumer.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/jiffies.h>
  11. #include <linux/mmc/host.h>
  12. #include <linux/mmc/slot-gpio.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <trace/hooks/mmc_core.h>
  16. #include "slot-gpio.h"
  17. struct mmc_gpio {
  18. struct gpio_desc *ro_gpio;
  19. struct gpio_desc *cd_gpio;
  20. irqreturn_t (*cd_gpio_isr)(int irq, void *dev_id);
  21. char *ro_label;
  22. char *cd_label;
  23. u32 cd_debounce_delay_ms;
  24. };
  25. static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
  26. {
  27. /* Schedule a card detection after a debounce timeout */
  28. struct mmc_host *host = dev_id;
  29. struct mmc_gpio *ctx = host->slot.handler_priv;
  30. bool allow = true;
  31. trace_android_vh_mmc_gpio_cd_irqt(host, &allow);
  32. if (!allow)
  33. return IRQ_HANDLED;
  34. host->trigger_card_event = true;
  35. mmc_detect_change(host, msecs_to_jiffies(ctx->cd_debounce_delay_ms));
  36. return IRQ_HANDLED;
  37. }
  38. int mmc_gpio_alloc(struct mmc_host *host)
  39. {
  40. struct mmc_gpio *ctx = devm_kzalloc(host->parent,
  41. sizeof(*ctx), GFP_KERNEL);
  42. if (ctx) {
  43. ctx->cd_debounce_delay_ms = 200;
  44. ctx->cd_label = devm_kasprintf(host->parent, GFP_KERNEL,
  45. "%s cd", dev_name(host->parent));
  46. if (!ctx->cd_label)
  47. return -ENOMEM;
  48. ctx->ro_label = devm_kasprintf(host->parent, GFP_KERNEL,
  49. "%s ro", dev_name(host->parent));
  50. if (!ctx->ro_label)
  51. return -ENOMEM;
  52. host->slot.handler_priv = ctx;
  53. host->slot.cd_irq = -EINVAL;
  54. }
  55. return ctx ? 0 : -ENOMEM;
  56. }
  57. int mmc_gpio_get_ro(struct mmc_host *host)
  58. {
  59. struct mmc_gpio *ctx = host->slot.handler_priv;
  60. if (!ctx || !ctx->ro_gpio)
  61. return -ENOSYS;
  62. return gpiod_get_value_cansleep(ctx->ro_gpio);
  63. }
  64. EXPORT_SYMBOL(mmc_gpio_get_ro);
  65. int mmc_gpio_get_cd(struct mmc_host *host)
  66. {
  67. struct mmc_gpio *ctx = host->slot.handler_priv;
  68. int cansleep;
  69. if (!ctx || !ctx->cd_gpio)
  70. return -ENOSYS;
  71. cansleep = gpiod_cansleep(ctx->cd_gpio);
  72. return cansleep ?
  73. gpiod_get_value_cansleep(ctx->cd_gpio) :
  74. gpiod_get_value(ctx->cd_gpio);
  75. }
  76. EXPORT_SYMBOL(mmc_gpio_get_cd);
  77. void mmc_gpiod_request_cd_irq(struct mmc_host *host)
  78. {
  79. struct mmc_gpio *ctx = host->slot.handler_priv;
  80. int irq = -EINVAL;
  81. int ret;
  82. if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
  83. return;
  84. /*
  85. * Do not use IRQ if the platform prefers to poll, e.g., because that
  86. * IRQ number is already used by another unit and cannot be shared.
  87. */
  88. if (!(host->caps & MMC_CAP_NEEDS_POLL))
  89. irq = gpiod_to_irq(ctx->cd_gpio);
  90. if (irq >= 0) {
  91. if (!ctx->cd_gpio_isr)
  92. ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
  93. ret = devm_request_threaded_irq(host->parent, irq,
  94. NULL, ctx->cd_gpio_isr,
  95. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  96. ctx->cd_label, host);
  97. if (ret < 0)
  98. irq = ret;
  99. }
  100. host->slot.cd_irq = irq;
  101. if (irq < 0)
  102. host->caps |= MMC_CAP_NEEDS_POLL;
  103. }
  104. EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
  105. int mmc_gpio_set_cd_wake(struct mmc_host *host, bool on)
  106. {
  107. int ret = 0;
  108. if (!(host->caps & MMC_CAP_CD_WAKE) ||
  109. host->slot.cd_irq < 0 ||
  110. on == host->slot.cd_wake_enabled)
  111. return 0;
  112. if (on) {
  113. ret = enable_irq_wake(host->slot.cd_irq);
  114. host->slot.cd_wake_enabled = !ret;
  115. } else {
  116. disable_irq_wake(host->slot.cd_irq);
  117. host->slot.cd_wake_enabled = false;
  118. }
  119. return ret;
  120. }
  121. EXPORT_SYMBOL(mmc_gpio_set_cd_wake);
  122. /* Register an alternate interrupt service routine for
  123. * the card-detect GPIO.
  124. */
  125. void mmc_gpio_set_cd_isr(struct mmc_host *host,
  126. irqreturn_t (*isr)(int irq, void *dev_id))
  127. {
  128. struct mmc_gpio *ctx = host->slot.handler_priv;
  129. WARN_ON(ctx->cd_gpio_isr);
  130. ctx->cd_gpio_isr = isr;
  131. }
  132. EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
  133. /**
  134. * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
  135. * @host: mmc host
  136. * @con_id: function within the GPIO consumer
  137. * @idx: index of the GPIO to obtain in the consumer
  138. * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
  139. * @debounce: debounce time in microseconds
  140. *
  141. * Note that this must be called prior to mmc_add_host()
  142. * otherwise the caller must also call mmc_gpiod_request_cd_irq().
  143. *
  144. * Returns zero on success, else an error.
  145. */
  146. int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
  147. unsigned int idx, bool override_active_level,
  148. unsigned int debounce)
  149. {
  150. struct mmc_gpio *ctx = host->slot.handler_priv;
  151. struct gpio_desc *desc;
  152. int ret;
  153. desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
  154. if (IS_ERR(desc))
  155. return PTR_ERR(desc);
  156. if (debounce) {
  157. ret = gpiod_set_debounce(desc, debounce);
  158. if (ret < 0)
  159. ctx->cd_debounce_delay_ms = debounce / 1000;
  160. }
  161. /* override forces default (active-low) polarity ... */
  162. if (override_active_level && !gpiod_is_active_low(desc))
  163. gpiod_toggle_active_low(desc);
  164. /* ... or active-high */
  165. if (host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH)
  166. gpiod_toggle_active_low(desc);
  167. ctx->cd_gpio = desc;
  168. return 0;
  169. }
  170. EXPORT_SYMBOL(mmc_gpiod_request_cd);
  171. bool mmc_can_gpio_cd(struct mmc_host *host)
  172. {
  173. struct mmc_gpio *ctx = host->slot.handler_priv;
  174. return ctx->cd_gpio ? true : false;
  175. }
  176. EXPORT_SYMBOL(mmc_can_gpio_cd);
  177. /**
  178. * mmc_gpiod_request_ro - request a gpio descriptor for write protection
  179. * @host: mmc host
  180. * @con_id: function within the GPIO consumer
  181. * @idx: index of the GPIO to obtain in the consumer
  182. * @debounce: debounce time in microseconds
  183. *
  184. * Returns zero on success, else an error.
  185. */
  186. int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
  187. unsigned int idx, unsigned int debounce)
  188. {
  189. struct mmc_gpio *ctx = host->slot.handler_priv;
  190. struct gpio_desc *desc;
  191. int ret;
  192. desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
  193. if (IS_ERR(desc))
  194. return PTR_ERR(desc);
  195. if (debounce) {
  196. ret = gpiod_set_debounce(desc, debounce);
  197. if (ret < 0)
  198. return ret;
  199. }
  200. if (host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH)
  201. gpiod_toggle_active_low(desc);
  202. ctx->ro_gpio = desc;
  203. return 0;
  204. }
  205. EXPORT_SYMBOL(mmc_gpiod_request_ro);
  206. bool mmc_can_gpio_ro(struct mmc_host *host)
  207. {
  208. struct mmc_gpio *ctx = host->slot.handler_priv;
  209. return ctx->ro_gpio ? true : false;
  210. }
  211. EXPORT_SYMBOL(mmc_can_gpio_ro);