slot-gpio.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "slot-gpio.h"
  16. struct mmc_gpio {
  17. struct gpio_desc *ro_gpio;
  18. struct gpio_desc *cd_gpio;
  19. irq_handler_t cd_gpio_isr;
  20. char *ro_label;
  21. char *cd_label;
  22. u32 cd_debounce_delay_ms;
  23. int cd_irq;
  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. host->trigger_card_event = true;
  31. mmc_detect_change(host, msecs_to_jiffies(ctx->cd_debounce_delay_ms));
  32. return IRQ_HANDLED;
  33. }
  34. int mmc_gpio_alloc(struct mmc_host *host)
  35. {
  36. const char *devname = dev_name(host->parent);
  37. struct mmc_gpio *ctx;
  38. ctx = devm_kzalloc(host->parent, sizeof(*ctx), GFP_KERNEL);
  39. if (!ctx)
  40. return -ENOMEM;
  41. ctx->cd_debounce_delay_ms = 200;
  42. ctx->cd_label = devm_kasprintf(host->parent, GFP_KERNEL, "%s cd", devname);
  43. if (!ctx->cd_label)
  44. return -ENOMEM;
  45. ctx->ro_label = devm_kasprintf(host->parent, GFP_KERNEL, "%s ro", devname);
  46. if (!ctx->ro_label)
  47. return -ENOMEM;
  48. ctx->cd_irq = -EINVAL;
  49. host->slot.handler_priv = ctx;
  50. host->slot.cd_irq = -EINVAL;
  51. return 0;
  52. }
  53. void mmc_gpio_set_cd_irq(struct mmc_host *host, int irq)
  54. {
  55. struct mmc_gpio *ctx = host->slot.handler_priv;
  56. if (!ctx || irq < 0)
  57. return;
  58. ctx->cd_irq = irq;
  59. }
  60. EXPORT_SYMBOL(mmc_gpio_set_cd_irq);
  61. int mmc_gpio_get_ro(struct mmc_host *host)
  62. {
  63. struct mmc_gpio *ctx = host->slot.handler_priv;
  64. int cansleep;
  65. if (!ctx || !ctx->ro_gpio)
  66. return -ENOSYS;
  67. cansleep = gpiod_cansleep(ctx->ro_gpio);
  68. return cansleep ?
  69. gpiod_get_value_cansleep(ctx->ro_gpio) :
  70. gpiod_get_value(ctx->ro_gpio);
  71. }
  72. EXPORT_SYMBOL(mmc_gpio_get_ro);
  73. int mmc_gpio_get_cd(struct mmc_host *host)
  74. {
  75. struct mmc_gpio *ctx = host->slot.handler_priv;
  76. int cansleep;
  77. if (!ctx || !ctx->cd_gpio)
  78. return -ENOSYS;
  79. cansleep = gpiod_cansleep(ctx->cd_gpio);
  80. return cansleep ?
  81. gpiod_get_value_cansleep(ctx->cd_gpio) :
  82. gpiod_get_value(ctx->cd_gpio);
  83. }
  84. EXPORT_SYMBOL(mmc_gpio_get_cd);
  85. void mmc_gpiod_request_cd_irq(struct mmc_host *host)
  86. {
  87. struct mmc_gpio *ctx = host->slot.handler_priv;
  88. int irq = -EINVAL;
  89. int ret;
  90. if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
  91. return;
  92. /*
  93. * Do not use IRQ if the platform prefers to poll, e.g., because that
  94. * IRQ number is already used by another unit and cannot be shared.
  95. */
  96. if (ctx->cd_irq >= 0)
  97. irq = ctx->cd_irq;
  98. else if (!(host->caps & MMC_CAP_NEEDS_POLL))
  99. irq = gpiod_to_irq(ctx->cd_gpio);
  100. if (irq >= 0) {
  101. if (!ctx->cd_gpio_isr)
  102. ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
  103. ret = devm_request_threaded_irq(host->parent, irq,
  104. NULL, ctx->cd_gpio_isr,
  105. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  106. ctx->cd_label, host);
  107. if (ret < 0)
  108. irq = ret;
  109. }
  110. host->slot.cd_irq = irq;
  111. if (irq < 0)
  112. host->caps |= MMC_CAP_NEEDS_POLL;
  113. }
  114. EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
  115. int mmc_gpio_set_cd_wake(struct mmc_host *host, bool on)
  116. {
  117. int ret = 0;
  118. if (!(host->caps & MMC_CAP_CD_WAKE) ||
  119. host->slot.cd_irq < 0 ||
  120. on == host->slot.cd_wake_enabled)
  121. return 0;
  122. if (on) {
  123. ret = enable_irq_wake(host->slot.cd_irq);
  124. host->slot.cd_wake_enabled = !ret;
  125. } else {
  126. disable_irq_wake(host->slot.cd_irq);
  127. host->slot.cd_wake_enabled = false;
  128. }
  129. return ret;
  130. }
  131. EXPORT_SYMBOL(mmc_gpio_set_cd_wake);
  132. /* Register an alternate interrupt service routine for
  133. * the card-detect GPIO.
  134. */
  135. void mmc_gpio_set_cd_isr(struct mmc_host *host, irq_handler_t isr)
  136. {
  137. struct mmc_gpio *ctx = host->slot.handler_priv;
  138. WARN_ON(ctx->cd_gpio_isr);
  139. ctx->cd_gpio_isr = isr;
  140. }
  141. EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
  142. /**
  143. * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
  144. * @host: mmc host
  145. * @con_id: function within the GPIO consumer
  146. * @idx: index of the GPIO to obtain in the consumer
  147. * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
  148. * @debounce: debounce time in microseconds
  149. *
  150. * Note that this must be called prior to mmc_add_host()
  151. * otherwise the caller must also call mmc_gpiod_request_cd_irq().
  152. *
  153. * Returns zero on success, else an error.
  154. */
  155. int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
  156. unsigned int idx, bool override_active_level,
  157. unsigned int debounce)
  158. {
  159. struct mmc_gpio *ctx = host->slot.handler_priv;
  160. struct gpio_desc *desc;
  161. int ret;
  162. desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
  163. if (IS_ERR(desc))
  164. return PTR_ERR(desc);
  165. /* Update default label if no con_id provided */
  166. if (!con_id)
  167. gpiod_set_consumer_name(desc, ctx->cd_label);
  168. if (debounce) {
  169. ret = gpiod_set_debounce(desc, debounce);
  170. if (ret < 0)
  171. ctx->cd_debounce_delay_ms = debounce / 1000;
  172. }
  173. /* override forces default (active-low) polarity ... */
  174. if (override_active_level && !gpiod_is_active_low(desc))
  175. gpiod_toggle_active_low(desc);
  176. /* ... or active-high */
  177. if (host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH)
  178. gpiod_toggle_active_low(desc);
  179. ctx->cd_gpio = desc;
  180. return 0;
  181. }
  182. EXPORT_SYMBOL(mmc_gpiod_request_cd);
  183. /**
  184. * mmc_gpiod_set_cd_config - set config for card-detection GPIO
  185. * @host: mmc host
  186. * @config: Generic pinconf config (from pinconf_to_config_packed())
  187. *
  188. * This can be used by mmc host drivers to fixup a card-detection GPIO's config
  189. * (e.g. set PIN_CONFIG_BIAS_PULL_UP) after acquiring the GPIO descriptor
  190. * through mmc_gpiod_request_cd().
  191. *
  192. * Returns:
  193. * 0 on success, or a negative errno value on error.
  194. */
  195. int mmc_gpiod_set_cd_config(struct mmc_host *host, unsigned long config)
  196. {
  197. struct mmc_gpio *ctx = host->slot.handler_priv;
  198. return gpiod_set_config(ctx->cd_gpio, config);
  199. }
  200. EXPORT_SYMBOL(mmc_gpiod_set_cd_config);
  201. bool mmc_can_gpio_cd(struct mmc_host *host)
  202. {
  203. struct mmc_gpio *ctx = host->slot.handler_priv;
  204. return ctx->cd_gpio ? true : false;
  205. }
  206. EXPORT_SYMBOL(mmc_can_gpio_cd);
  207. /**
  208. * mmc_gpiod_request_ro - request a gpio descriptor for write protection
  209. * @host: mmc host
  210. * @con_id: function within the GPIO consumer
  211. * @idx: index of the GPIO to obtain in the consumer
  212. * @debounce: debounce time in microseconds
  213. *
  214. * Returns zero on success, else an error.
  215. */
  216. int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
  217. unsigned int idx, unsigned int debounce)
  218. {
  219. struct mmc_gpio *ctx = host->slot.handler_priv;
  220. struct gpio_desc *desc;
  221. int ret;
  222. desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
  223. if (IS_ERR(desc))
  224. return PTR_ERR(desc);
  225. /* Update default label if no con_id provided */
  226. if (!con_id)
  227. gpiod_set_consumer_name(desc, ctx->ro_label);
  228. if (debounce) {
  229. ret = gpiod_set_debounce(desc, debounce);
  230. if (ret < 0)
  231. return ret;
  232. }
  233. if (host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH)
  234. gpiod_toggle_active_low(desc);
  235. ctx->ro_gpio = desc;
  236. return 0;
  237. }
  238. EXPORT_SYMBOL(mmc_gpiod_request_ro);
  239. bool mmc_can_gpio_ro(struct mmc_host *host)
  240. {
  241. struct mmc_gpio *ctx = host->slot.handler_priv;
  242. return ctx->ro_gpio ? true : false;
  243. }
  244. EXPORT_SYMBOL(mmc_can_gpio_ro);