slot-gpio.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Generic GPIO card-detect helper
  3. *
  4. * Copyright (C) 2011, Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/err.h>
  11. #include <linux/gpio.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/mmc/host.h>
  15. #include <linux/mmc/slot-gpio.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. struct mmc_gpio {
  19. int ro_gpio;
  20. int cd_gpio;
  21. char *ro_label;
  22. char cd_label[0];
  23. };
  24. static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
  25. {
  26. /* Schedule a card detection after a debounce timeout */
  27. struct mmc_host *host = dev_id;
  28. if (host->ops->card_event)
  29. host->ops->card_event(host);
  30. mmc_detect_change(host, msecs_to_jiffies(200));
  31. return IRQ_HANDLED;
  32. }
  33. static int mmc_gpio_alloc(struct mmc_host *host)
  34. {
  35. size_t len = strlen(dev_name(host->parent)) + 4;
  36. struct mmc_gpio *ctx;
  37. mutex_lock(&host->slot.lock);
  38. ctx = host->slot.handler_priv;
  39. if (!ctx) {
  40. /*
  41. * devm_kzalloc() can be called after device_initialize(), even
  42. * before device_add(), i.e., between mmc_alloc_host() and
  43. * mmc_add_host()
  44. */
  45. ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + 2 * len,
  46. GFP_KERNEL);
  47. if (ctx) {
  48. ctx->ro_label = ctx->cd_label + len;
  49. snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
  50. snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
  51. ctx->cd_gpio = -EINVAL;
  52. ctx->ro_gpio = -EINVAL;
  53. host->slot.handler_priv = ctx;
  54. }
  55. }
  56. mutex_unlock(&host->slot.lock);
  57. return ctx ? 0 : -ENOMEM;
  58. }
  59. int mmc_gpio_get_ro(struct mmc_host *host)
  60. {
  61. struct mmc_gpio *ctx = host->slot.handler_priv;
  62. if (!ctx || !gpio_is_valid(ctx->ro_gpio))
  63. return -ENOSYS;
  64. return !gpio_get_value_cansleep(ctx->ro_gpio) ^
  65. !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
  66. }
  67. EXPORT_SYMBOL(mmc_gpio_get_ro);
  68. int mmc_gpio_get_cd(struct mmc_host *host)
  69. {
  70. struct mmc_gpio *ctx = host->slot.handler_priv;
  71. if (!ctx || !gpio_is_valid(ctx->cd_gpio))
  72. return -ENOSYS;
  73. return !gpio_get_value_cansleep(ctx->cd_gpio) ^
  74. !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
  75. }
  76. EXPORT_SYMBOL(mmc_gpio_get_cd);
  77. /**
  78. * mmc_gpio_request_ro - request a gpio for write-protection
  79. * @host: mmc host
  80. * @gpio: gpio number requested
  81. *
  82. * As devm_* managed functions are used in mmc_gpio_request_ro(), client
  83. * drivers do not need to explicitly call mmc_gpio_free_ro() for freeing up,
  84. * if the requesting and freeing are only needed at probing and unbinding time
  85. * for once. However, if client drivers do something special like runtime
  86. * switching for write-protection, they are responsible for calling
  87. * mmc_gpio_request_ro() and mmc_gpio_free_ro() as a pair on their own.
  88. *
  89. * Returns zero on success, else an error.
  90. */
  91. int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
  92. {
  93. struct mmc_gpio *ctx;
  94. int ret;
  95. if (!gpio_is_valid(gpio))
  96. return -EINVAL;
  97. ret = mmc_gpio_alloc(host);
  98. if (ret < 0)
  99. return ret;
  100. ctx = host->slot.handler_priv;
  101. ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
  102. ctx->ro_label);
  103. if (ret < 0)
  104. return ret;
  105. ctx->ro_gpio = gpio;
  106. return 0;
  107. }
  108. EXPORT_SYMBOL(mmc_gpio_request_ro);
  109. /**
  110. * mmc_gpio_request_cd - request a gpio for card-detection
  111. * @host: mmc host
  112. * @gpio: gpio number requested
  113. *
  114. * As devm_* managed functions are used in mmc_gpio_request_cd(), client
  115. * drivers do not need to explicitly call mmc_gpio_free_cd() for freeing up,
  116. * if the requesting and freeing are only needed at probing and unbinding time
  117. * for once. However, if client drivers do something special like runtime
  118. * switching for card-detection, they are responsible for calling
  119. * mmc_gpio_request_cd() and mmc_gpio_free_cd() as a pair on their own.
  120. *
  121. * Returns zero on success, else an error.
  122. */
  123. int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio)
  124. {
  125. struct mmc_gpio *ctx;
  126. int irq = gpio_to_irq(gpio);
  127. int ret;
  128. ret = mmc_gpio_alloc(host);
  129. if (ret < 0)
  130. return ret;
  131. ctx = host->slot.handler_priv;
  132. ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
  133. ctx->cd_label);
  134. if (ret < 0)
  135. /*
  136. * don't bother freeing memory. It might still get used by other
  137. * slot functions, in any case it will be freed, when the device
  138. * is destroyed.
  139. */
  140. return ret;
  141. /*
  142. * Even if gpio_to_irq() returns a valid IRQ number, the platform might
  143. * still prefer to poll, e.g., because that IRQ number is already used
  144. * by another unit and cannot be shared.
  145. */
  146. if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
  147. irq = -EINVAL;
  148. if (irq >= 0) {
  149. ret = devm_request_threaded_irq(&host->class_dev, irq,
  150. NULL, mmc_gpio_cd_irqt,
  151. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  152. ctx->cd_label, host);
  153. if (ret < 0)
  154. irq = ret;
  155. }
  156. host->slot.cd_irq = irq;
  157. if (irq < 0)
  158. host->caps |= MMC_CAP_NEEDS_POLL;
  159. ctx->cd_gpio = gpio;
  160. return 0;
  161. }
  162. EXPORT_SYMBOL(mmc_gpio_request_cd);
  163. /**
  164. * mmc_gpio_free_ro - free the write-protection gpio
  165. * @host: mmc host
  166. *
  167. * It's provided only for cases that client drivers need to manually free
  168. * up the write-protection gpio requested by mmc_gpio_request_ro().
  169. */
  170. void mmc_gpio_free_ro(struct mmc_host *host)
  171. {
  172. struct mmc_gpio *ctx = host->slot.handler_priv;
  173. int gpio;
  174. if (!ctx || !gpio_is_valid(ctx->ro_gpio))
  175. return;
  176. gpio = ctx->ro_gpio;
  177. ctx->ro_gpio = -EINVAL;
  178. devm_gpio_free(&host->class_dev, gpio);
  179. }
  180. EXPORT_SYMBOL(mmc_gpio_free_ro);
  181. /**
  182. * mmc_gpio_free_cd - free the card-detection gpio
  183. * @host: mmc host
  184. *
  185. * It's provided only for cases that client drivers need to manually free
  186. * up the card-detection gpio requested by mmc_gpio_request_cd().
  187. */
  188. void mmc_gpio_free_cd(struct mmc_host *host)
  189. {
  190. struct mmc_gpio *ctx = host->slot.handler_priv;
  191. int gpio;
  192. if (!ctx || !gpio_is_valid(ctx->cd_gpio))
  193. return;
  194. if (host->slot.cd_irq >= 0) {
  195. devm_free_irq(&host->class_dev, host->slot.cd_irq, host);
  196. host->slot.cd_irq = -EINVAL;
  197. }
  198. gpio = ctx->cd_gpio;
  199. ctx->cd_gpio = -EINVAL;
  200. devm_gpio_free(&host->class_dev, gpio);
  201. }
  202. EXPORT_SYMBOL(mmc_gpio_free_cd);