slot-gpio.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. mmc_detect_change(dev_id, msecs_to_jiffies(100));
  28. return IRQ_HANDLED;
  29. }
  30. static int mmc_gpio_alloc(struct mmc_host *host)
  31. {
  32. size_t len = strlen(dev_name(host->parent)) + 4;
  33. struct mmc_gpio *ctx;
  34. mutex_lock(&host->slot.lock);
  35. ctx = host->slot.handler_priv;
  36. if (!ctx) {
  37. /*
  38. * devm_kzalloc() can be called after device_initialize(), even
  39. * before device_add(), i.e., between mmc_alloc_host() and
  40. * mmc_add_host()
  41. */
  42. ctx = devm_kzalloc(&host->class_dev, sizeof(*ctx) + 2 * len,
  43. GFP_KERNEL);
  44. if (ctx) {
  45. ctx->ro_label = ctx->cd_label + len;
  46. snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
  47. snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
  48. ctx->cd_gpio = -EINVAL;
  49. ctx->ro_gpio = -EINVAL;
  50. host->slot.handler_priv = ctx;
  51. }
  52. }
  53. mutex_unlock(&host->slot.lock);
  54. return ctx ? 0 : -ENOMEM;
  55. }
  56. int mmc_gpio_get_ro(struct mmc_host *host)
  57. {
  58. struct mmc_gpio *ctx = host->slot.handler_priv;
  59. if (!ctx || !gpio_is_valid(ctx->ro_gpio))
  60. return -ENOSYS;
  61. return !gpio_get_value_cansleep(ctx->ro_gpio) ^
  62. !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
  63. }
  64. EXPORT_SYMBOL(mmc_gpio_get_ro);
  65. int mmc_gpio_get_cd(struct mmc_host *host)
  66. {
  67. struct mmc_gpio *ctx = host->slot.handler_priv;
  68. if (!ctx || !gpio_is_valid(ctx->cd_gpio))
  69. return -ENOSYS;
  70. return !gpio_get_value_cansleep(ctx->cd_gpio) ^
  71. !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
  72. }
  73. EXPORT_SYMBOL(mmc_gpio_get_cd);
  74. int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
  75. {
  76. struct mmc_gpio *ctx;
  77. int ret;
  78. if (!gpio_is_valid(gpio))
  79. return -EINVAL;
  80. ret = mmc_gpio_alloc(host);
  81. if (ret < 0)
  82. return ret;
  83. ctx = host->slot.handler_priv;
  84. return gpio_request_one(gpio, GPIOF_DIR_IN, ctx->ro_label);
  85. }
  86. EXPORT_SYMBOL(mmc_gpio_request_ro);
  87. int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio)
  88. {
  89. struct mmc_gpio *ctx;
  90. int irq = gpio_to_irq(gpio);
  91. int ret;
  92. ret = mmc_gpio_alloc(host);
  93. if (ret < 0)
  94. return ret;
  95. ctx = host->slot.handler_priv;
  96. ret = gpio_request_one(gpio, GPIOF_DIR_IN, ctx->cd_label);
  97. if (ret < 0)
  98. /*
  99. * don't bother freeing memory. It might still get used by other
  100. * slot functions, in any case it will be freed, when the device
  101. * is destroyed.
  102. */
  103. return ret;
  104. /*
  105. * Even if gpio_to_irq() returns a valid IRQ number, the platform might
  106. * still prefer to poll, e.g., because that IRQ number is already used
  107. * by another unit and cannot be shared.
  108. */
  109. if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
  110. irq = -EINVAL;
  111. if (irq >= 0) {
  112. ret = request_threaded_irq(irq, NULL, mmc_gpio_cd_irqt,
  113. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  114. ctx->cd_label, host);
  115. if (ret < 0)
  116. irq = ret;
  117. }
  118. host->slot.cd_irq = irq;
  119. if (irq < 0)
  120. host->caps |= MMC_CAP_NEEDS_POLL;
  121. ctx->cd_gpio = gpio;
  122. return 0;
  123. }
  124. EXPORT_SYMBOL(mmc_gpio_request_cd);
  125. void mmc_gpio_free_ro(struct mmc_host *host)
  126. {
  127. struct mmc_gpio *ctx = host->slot.handler_priv;
  128. int gpio;
  129. if (!ctx || !gpio_is_valid(ctx->ro_gpio))
  130. return;
  131. gpio = ctx->ro_gpio;
  132. ctx->ro_gpio = -EINVAL;
  133. gpio_free(gpio);
  134. }
  135. EXPORT_SYMBOL(mmc_gpio_free_ro);
  136. void mmc_gpio_free_cd(struct mmc_host *host)
  137. {
  138. struct mmc_gpio *ctx = host->slot.handler_priv;
  139. int gpio;
  140. if (!ctx || !gpio_is_valid(ctx->cd_gpio))
  141. return;
  142. if (host->slot.cd_irq >= 0) {
  143. free_irq(host->slot.cd_irq, host);
  144. host->slot.cd_irq = -EINVAL;
  145. }
  146. gpio = ctx->cd_gpio;
  147. ctx->cd_gpio = -EINVAL;
  148. gpio_free(gpio);
  149. }
  150. EXPORT_SYMBOL(mmc_gpio_free_cd);