slot-gpio.c 4.2 KB

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