slot-gpio.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. * @debounce: debounce time in microseconds
  114. *
  115. * As devm_* managed functions are used in mmc_gpio_request_cd(), client
  116. * drivers do not need to explicitly call mmc_gpio_free_cd() for freeing up,
  117. * if the requesting and freeing are only needed at probing and unbinding time
  118. * for once. However, if client drivers do something special like runtime
  119. * switching for card-detection, they are responsible for calling
  120. * mmc_gpio_request_cd() and mmc_gpio_free_cd() as a pair on their own.
  121. *
  122. * If GPIO debouncing is desired, set the debounce parameter to a non-zero
  123. * value. The caller is responsible for ensuring that the GPIO driver associated
  124. * with the GPIO supports debouncing, otherwise an error will be returned.
  125. *
  126. * Returns zero on success, else an error.
  127. */
  128. int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
  129. unsigned int debounce)
  130. {
  131. struct mmc_gpio *ctx;
  132. int irq = gpio_to_irq(gpio);
  133. int ret;
  134. ret = mmc_gpio_alloc(host);
  135. if (ret < 0)
  136. return ret;
  137. ctx = host->slot.handler_priv;
  138. ret = devm_gpio_request_one(&host->class_dev, gpio, GPIOF_DIR_IN,
  139. ctx->cd_label);
  140. if (ret < 0)
  141. /*
  142. * don't bother freeing memory. It might still get used by other
  143. * slot functions, in any case it will be freed, when the device
  144. * is destroyed.
  145. */
  146. return ret;
  147. if (debounce) {
  148. ret = gpio_set_debounce(gpio, debounce);
  149. if (ret < 0)
  150. return ret;
  151. }
  152. /*
  153. * Even if gpio_to_irq() returns a valid IRQ number, the platform might
  154. * still prefer to poll, e.g., because that IRQ number is already used
  155. * by another unit and cannot be shared.
  156. */
  157. if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
  158. irq = -EINVAL;
  159. if (irq >= 0) {
  160. ret = devm_request_threaded_irq(&host->class_dev, irq,
  161. NULL, mmc_gpio_cd_irqt,
  162. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  163. ctx->cd_label, host);
  164. if (ret < 0)
  165. irq = ret;
  166. }
  167. host->slot.cd_irq = irq;
  168. if (irq < 0)
  169. host->caps |= MMC_CAP_NEEDS_POLL;
  170. ctx->cd_gpio = gpio;
  171. return 0;
  172. }
  173. EXPORT_SYMBOL(mmc_gpio_request_cd);
  174. /**
  175. * mmc_gpio_free_ro - free the write-protection gpio
  176. * @host: mmc host
  177. *
  178. * It's provided only for cases that client drivers need to manually free
  179. * up the write-protection gpio requested by mmc_gpio_request_ro().
  180. */
  181. void mmc_gpio_free_ro(struct mmc_host *host)
  182. {
  183. struct mmc_gpio *ctx = host->slot.handler_priv;
  184. int gpio;
  185. if (!ctx || !gpio_is_valid(ctx->ro_gpio))
  186. return;
  187. gpio = ctx->ro_gpio;
  188. ctx->ro_gpio = -EINVAL;
  189. devm_gpio_free(&host->class_dev, gpio);
  190. }
  191. EXPORT_SYMBOL(mmc_gpio_free_ro);
  192. /**
  193. * mmc_gpio_free_cd - free the card-detection gpio
  194. * @host: mmc host
  195. *
  196. * It's provided only for cases that client drivers need to manually free
  197. * up the card-detection gpio requested by mmc_gpio_request_cd().
  198. */
  199. void mmc_gpio_free_cd(struct mmc_host *host)
  200. {
  201. struct mmc_gpio *ctx = host->slot.handler_priv;
  202. int gpio;
  203. if (!ctx || !gpio_is_valid(ctx->cd_gpio))
  204. return;
  205. if (host->slot.cd_irq >= 0) {
  206. devm_free_irq(&host->class_dev, host->slot.cd_irq, host);
  207. host->slot.cd_irq = -EINVAL;
  208. }
  209. gpio = ctx->cd_gpio;
  210. ctx->cd_gpio = -EINVAL;
  211. devm_gpio_free(&host->class_dev, gpio);
  212. }
  213. EXPORT_SYMBOL(mmc_gpio_free_cd);