slot-gpio.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
  78. {
  79. struct mmc_gpio *ctx;
  80. int ret;
  81. if (!gpio_is_valid(gpio))
  82. return -EINVAL;
  83. ret = mmc_gpio_alloc(host);
  84. if (ret < 0)
  85. return ret;
  86. ctx = host->slot.handler_priv;
  87. ret = gpio_request_one(gpio, GPIOF_DIR_IN, ctx->ro_label);
  88. if (ret < 0)
  89. return ret;
  90. ctx->ro_gpio = gpio;
  91. return 0;
  92. }
  93. EXPORT_SYMBOL(mmc_gpio_request_ro);
  94. int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio)
  95. {
  96. struct mmc_gpio *ctx;
  97. int irq = gpio_to_irq(gpio);
  98. int ret;
  99. ret = mmc_gpio_alloc(host);
  100. if (ret < 0)
  101. return ret;
  102. ctx = host->slot.handler_priv;
  103. ret = gpio_request_one(gpio, GPIOF_DIR_IN, ctx->cd_label);
  104. if (ret < 0)
  105. /*
  106. * don't bother freeing memory. It might still get used by other
  107. * slot functions, in any case it will be freed, when the device
  108. * is destroyed.
  109. */
  110. return ret;
  111. /*
  112. * Even if gpio_to_irq() returns a valid IRQ number, the platform might
  113. * still prefer to poll, e.g., because that IRQ number is already used
  114. * by another unit and cannot be shared.
  115. */
  116. if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
  117. irq = -EINVAL;
  118. if (irq >= 0) {
  119. ret = request_threaded_irq(irq, NULL, mmc_gpio_cd_irqt,
  120. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  121. ctx->cd_label, host);
  122. if (ret < 0)
  123. irq = ret;
  124. }
  125. host->slot.cd_irq = irq;
  126. if (irq < 0)
  127. host->caps |= MMC_CAP_NEEDS_POLL;
  128. ctx->cd_gpio = gpio;
  129. return 0;
  130. }
  131. EXPORT_SYMBOL(mmc_gpio_request_cd);
  132. void mmc_gpio_free_ro(struct mmc_host *host)
  133. {
  134. struct mmc_gpio *ctx = host->slot.handler_priv;
  135. int gpio;
  136. if (!ctx || !gpio_is_valid(ctx->ro_gpio))
  137. return;
  138. gpio = ctx->ro_gpio;
  139. ctx->ro_gpio = -EINVAL;
  140. gpio_free(gpio);
  141. }
  142. EXPORT_SYMBOL(mmc_gpio_free_ro);
  143. void mmc_gpio_free_cd(struct mmc_host *host)
  144. {
  145. struct mmc_gpio *ctx = host->slot.handler_priv;
  146. int gpio;
  147. if (!ctx || !gpio_is_valid(ctx->cd_gpio))
  148. return;
  149. if (host->slot.cd_irq >= 0) {
  150. free_irq(host->slot.cd_irq, host);
  151. host->slot.cd_irq = -EINVAL;
  152. }
  153. gpio = ctx->cd_gpio;
  154. ctx->cd_gpio = -EINVAL;
  155. gpio_free(gpio);
  156. }
  157. EXPORT_SYMBOL(mmc_gpio_free_cd);