slot-gpio.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/gpio/consumer.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/mmc/host.h>
  16. #include <linux/mmc/slot-gpio.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include "slot-gpio.h"
  20. struct mmc_gpio {
  21. struct gpio_desc *ro_gpio;
  22. struct gpio_desc *cd_gpio;
  23. bool override_ro_active_level;
  24. bool override_cd_active_level;
  25. irqreturn_t (*cd_gpio_isr)(int irq, void *dev_id);
  26. char *ro_label;
  27. char cd_label[0];
  28. };
  29. static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
  30. {
  31. /* Schedule a card detection after a debounce timeout */
  32. struct mmc_host *host = dev_id;
  33. host->trigger_card_event = true;
  34. mmc_detect_change(host, msecs_to_jiffies(200));
  35. return IRQ_HANDLED;
  36. }
  37. int mmc_gpio_alloc(struct mmc_host *host)
  38. {
  39. size_t len = strlen(dev_name(host->parent)) + 4;
  40. struct mmc_gpio *ctx = devm_kzalloc(host->parent,
  41. sizeof(*ctx) + 2 * len, GFP_KERNEL);
  42. if (ctx) {
  43. ctx->ro_label = ctx->cd_label + len;
  44. snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
  45. snprintf(ctx->ro_label, len, "%s ro", dev_name(host->parent));
  46. host->slot.handler_priv = ctx;
  47. host->slot.cd_irq = -EINVAL;
  48. }
  49. return ctx ? 0 : -ENOMEM;
  50. }
  51. int mmc_gpio_get_ro(struct mmc_host *host)
  52. {
  53. struct mmc_gpio *ctx = host->slot.handler_priv;
  54. if (!ctx || !ctx->ro_gpio)
  55. return -ENOSYS;
  56. if (ctx->override_ro_active_level)
  57. return !gpiod_get_raw_value_cansleep(ctx->ro_gpio) ^
  58. !!(host->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
  59. return gpiod_get_value_cansleep(ctx->ro_gpio);
  60. }
  61. EXPORT_SYMBOL(mmc_gpio_get_ro);
  62. int mmc_gpio_get_cd(struct mmc_host *host)
  63. {
  64. struct mmc_gpio *ctx = host->slot.handler_priv;
  65. if (!ctx || !ctx->cd_gpio)
  66. return -ENOSYS;
  67. if (ctx->override_cd_active_level)
  68. return !gpiod_get_raw_value_cansleep(ctx->cd_gpio) ^
  69. !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
  70. return gpiod_get_value_cansleep(ctx->cd_gpio);
  71. }
  72. EXPORT_SYMBOL(mmc_gpio_get_cd);
  73. /**
  74. * mmc_gpio_request_ro - request a gpio for write-protection
  75. * @host: mmc host
  76. * @gpio: gpio number requested
  77. *
  78. * As devm_* managed functions are used in mmc_gpio_request_ro(), client
  79. * drivers do not need to worry about freeing up memory.
  80. *
  81. * Returns zero on success, else an error.
  82. */
  83. int mmc_gpio_request_ro(struct mmc_host *host, unsigned int gpio)
  84. {
  85. struct mmc_gpio *ctx = host->slot.handler_priv;
  86. int ret;
  87. if (!gpio_is_valid(gpio))
  88. return -EINVAL;
  89. ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
  90. ctx->ro_label);
  91. if (ret < 0)
  92. return ret;
  93. ctx->override_ro_active_level = true;
  94. ctx->ro_gpio = gpio_to_desc(gpio);
  95. return 0;
  96. }
  97. EXPORT_SYMBOL(mmc_gpio_request_ro);
  98. void mmc_gpiod_request_cd_irq(struct mmc_host *host)
  99. {
  100. struct mmc_gpio *ctx = host->slot.handler_priv;
  101. int ret, irq;
  102. if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
  103. return;
  104. irq = gpiod_to_irq(ctx->cd_gpio);
  105. /*
  106. * Even if gpiod_to_irq() returns a valid IRQ number, the platform might
  107. * still prefer to poll, e.g., because that IRQ number is already used
  108. * by another unit and cannot be shared.
  109. */
  110. if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
  111. irq = -EINVAL;
  112. if (irq >= 0) {
  113. if (!ctx->cd_gpio_isr)
  114. ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
  115. ret = devm_request_threaded_irq(host->parent, irq,
  116. NULL, ctx->cd_gpio_isr,
  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. else if ((host->caps & MMC_CAP_CD_WAKE) && !enable_irq_wake(irq))
  126. host->slot.cd_wake_enabled = true;
  127. }
  128. EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
  129. /* Register an alternate interrupt service routine for
  130. * the card-detect GPIO.
  131. */
  132. void mmc_gpio_set_cd_isr(struct mmc_host *host,
  133. irqreturn_t (*isr)(int irq, void *dev_id))
  134. {
  135. struct mmc_gpio *ctx = host->slot.handler_priv;
  136. WARN_ON(ctx->cd_gpio_isr);
  137. ctx->cd_gpio_isr = isr;
  138. }
  139. EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
  140. /**
  141. * mmc_gpio_request_cd - request a gpio for card-detection
  142. * @host: mmc host
  143. * @gpio: gpio number requested
  144. * @debounce: debounce time in microseconds
  145. *
  146. * As devm_* managed functions are used in mmc_gpio_request_cd(), client
  147. * drivers do not need to worry about freeing up memory.
  148. *
  149. * If GPIO debouncing is desired, set the debounce parameter to a non-zero
  150. * value. The caller is responsible for ensuring that the GPIO driver associated
  151. * with the GPIO supports debouncing, otherwise an error will be returned.
  152. *
  153. * Returns zero on success, else an error.
  154. */
  155. int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
  156. unsigned int debounce)
  157. {
  158. struct mmc_gpio *ctx = host->slot.handler_priv;
  159. int ret;
  160. ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
  161. ctx->cd_label);
  162. if (ret < 0)
  163. /*
  164. * don't bother freeing memory. It might still get used by other
  165. * slot functions, in any case it will be freed, when the device
  166. * is destroyed.
  167. */
  168. return ret;
  169. if (debounce) {
  170. ret = gpio_set_debounce(gpio, debounce);
  171. if (ret < 0)
  172. return ret;
  173. }
  174. ctx->override_cd_active_level = true;
  175. ctx->cd_gpio = gpio_to_desc(gpio);
  176. return 0;
  177. }
  178. EXPORT_SYMBOL(mmc_gpio_request_cd);
  179. /**
  180. * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
  181. * @host: mmc host
  182. * @con_id: function within the GPIO consumer
  183. * @idx: index of the GPIO to obtain in the consumer
  184. * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
  185. * @debounce: debounce time in microseconds
  186. * @gpio_invert: will return whether the GPIO line is inverted or not, set
  187. * to NULL to ignore
  188. *
  189. * Use this function in place of mmc_gpio_request_cd() to use the GPIO
  190. * descriptor API. Note that it must be called prior to mmc_add_host()
  191. * otherwise the caller must also call mmc_gpiod_request_cd_irq().
  192. *
  193. * Returns zero on success, else an error.
  194. */
  195. int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
  196. unsigned int idx, bool override_active_level,
  197. unsigned int debounce, bool *gpio_invert)
  198. {
  199. struct mmc_gpio *ctx = host->slot.handler_priv;
  200. struct gpio_desc *desc;
  201. int ret;
  202. desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
  203. if (IS_ERR(desc))
  204. return PTR_ERR(desc);
  205. if (debounce) {
  206. ret = gpiod_set_debounce(desc, debounce);
  207. if (ret < 0)
  208. return ret;
  209. }
  210. if (gpio_invert)
  211. *gpio_invert = !gpiod_is_active_low(desc);
  212. ctx->override_cd_active_level = override_active_level;
  213. ctx->cd_gpio = desc;
  214. return 0;
  215. }
  216. EXPORT_SYMBOL(mmc_gpiod_request_cd);
  217. bool mmc_can_gpio_cd(struct mmc_host *host)
  218. {
  219. struct mmc_gpio *ctx = host->slot.handler_priv;
  220. return ctx->cd_gpio ? true : false;
  221. }
  222. EXPORT_SYMBOL(mmc_can_gpio_cd);
  223. /**
  224. * mmc_gpiod_request_ro - request a gpio descriptor for write protection
  225. * @host: mmc host
  226. * @con_id: function within the GPIO consumer
  227. * @idx: index of the GPIO to obtain in the consumer
  228. * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
  229. * @debounce: debounce time in microseconds
  230. * @gpio_invert: will return whether the GPIO line is inverted or not,
  231. * set to NULL to ignore
  232. *
  233. * Use this function in place of mmc_gpio_request_ro() to use the GPIO
  234. * descriptor API.
  235. *
  236. * Returns zero on success, else an error.
  237. */
  238. int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
  239. unsigned int idx, bool override_active_level,
  240. unsigned int debounce, bool *gpio_invert)
  241. {
  242. struct mmc_gpio *ctx = host->slot.handler_priv;
  243. struct gpio_desc *desc;
  244. int ret;
  245. desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
  246. if (IS_ERR(desc))
  247. return PTR_ERR(desc);
  248. if (debounce) {
  249. ret = gpiod_set_debounce(desc, debounce);
  250. if (ret < 0)
  251. return ret;
  252. }
  253. if (gpio_invert)
  254. *gpio_invert = !gpiod_is_active_low(desc);
  255. ctx->override_ro_active_level = override_active_level;
  256. ctx->ro_gpio = desc;
  257. return 0;
  258. }
  259. EXPORT_SYMBOL(mmc_gpiod_request_ro);