slot-gpio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. /**
  133. * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
  134. * @host: mmc host
  135. * @con_id: function within the GPIO consumer
  136. * @idx: index of the GPIO to obtain in the consumer
  137. * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
  138. * @debounce: debounce time in microseconds
  139. *
  140. * Note that this must be called prior to mmc_add_host()
  141. * otherwise the caller must also call mmc_gpiod_request_cd_irq().
  142. *
  143. * Returns zero on success, else an error.
  144. */
  145. int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
  146. unsigned int idx, bool override_active_level,
  147. unsigned int debounce)
  148. {
  149. struct mmc_gpio *ctx = host->slot.handler_priv;
  150. struct gpio_desc *desc;
  151. int ret;
  152. desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
  153. if (IS_ERR(desc))
  154. return PTR_ERR(desc);
  155. /* Update default label if no con_id provided */
  156. if (!con_id)
  157. gpiod_set_consumer_name(desc, ctx->cd_label);
  158. if (debounce) {
  159. ret = gpiod_set_debounce(desc, debounce);
  160. if (ret < 0)
  161. ctx->cd_debounce_delay_ms = debounce / 1000;
  162. }
  163. /* override forces default (active-low) polarity ... */
  164. if (override_active_level && !gpiod_is_active_low(desc))
  165. gpiod_toggle_active_low(desc);
  166. /* ... or active-high */
  167. if (host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH)
  168. gpiod_toggle_active_low(desc);
  169. ctx->cd_gpio = desc;
  170. return 0;
  171. }
  172. EXPORT_SYMBOL(mmc_gpiod_request_cd);
  173. /**
  174. * mmc_gpiod_set_cd_config - set config for card-detection GPIO
  175. * @host: mmc host
  176. * @config: Generic pinconf config (from pinconf_to_config_packed())
  177. *
  178. * This can be used by mmc host drivers to fixup a card-detection GPIO's config
  179. * (e.g. set PIN_CONFIG_BIAS_PULL_UP) after acquiring the GPIO descriptor
  180. * through mmc_gpiod_request_cd().
  181. *
  182. * Returns:
  183. * 0 on success, or a negative errno value on error.
  184. */
  185. int mmc_gpiod_set_cd_config(struct mmc_host *host, unsigned long config)
  186. {
  187. struct mmc_gpio *ctx = host->slot.handler_priv;
  188. return gpiod_set_config(ctx->cd_gpio, config);
  189. }
  190. EXPORT_SYMBOL(mmc_gpiod_set_cd_config);
  191. bool mmc_host_can_gpio_cd(struct mmc_host *host)
  192. {
  193. struct mmc_gpio *ctx = host->slot.handler_priv;
  194. return ctx->cd_gpio ? true : false;
  195. }
  196. EXPORT_SYMBOL(mmc_host_can_gpio_cd);
  197. /**
  198. * mmc_gpiod_request_ro - request a gpio descriptor for write protection
  199. * @host: mmc host
  200. * @con_id: function within the GPIO consumer
  201. * @idx: index of the GPIO to obtain in the consumer
  202. * @debounce: debounce time in microseconds
  203. *
  204. * Returns zero on success, else an error.
  205. */
  206. int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
  207. unsigned int idx, unsigned int debounce)
  208. {
  209. struct mmc_gpio *ctx = host->slot.handler_priv;
  210. struct gpio_desc *desc;
  211. int ret;
  212. desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
  213. if (IS_ERR(desc))
  214. return PTR_ERR(desc);
  215. /* Update default label if no con_id provided */
  216. if (!con_id)
  217. gpiod_set_consumer_name(desc, ctx->ro_label);
  218. if (debounce) {
  219. ret = gpiod_set_debounce(desc, debounce);
  220. if (ret < 0)
  221. return ret;
  222. }
  223. if (host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH)
  224. gpiod_toggle_active_low(desc);
  225. ctx->ro_gpio = desc;
  226. return 0;
  227. }
  228. EXPORT_SYMBOL(mmc_gpiod_request_ro);
  229. bool mmc_host_can_gpio_ro(struct mmc_host *host)
  230. {
  231. struct mmc_gpio *ctx = host->slot.handler_priv;
  232. return ctx->ro_gpio ? true : false;
  233. }
  234. EXPORT_SYMBOL(mmc_host_can_gpio_ro);