slot-gpio.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. }
  126. EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
  127. /* Register an alternate interrupt service routine for
  128. * the card-detect GPIO.
  129. */
  130. void mmc_gpio_set_cd_isr(struct mmc_host *host,
  131. irqreturn_t (*isr)(int irq, void *dev_id))
  132. {
  133. struct mmc_gpio *ctx = host->slot.handler_priv;
  134. WARN_ON(ctx->cd_gpio_isr);
  135. ctx->cd_gpio_isr = isr;
  136. }
  137. EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
  138. /**
  139. * mmc_gpio_request_cd - request a gpio for card-detection
  140. * @host: mmc host
  141. * @gpio: gpio number requested
  142. * @debounce: debounce time in microseconds
  143. *
  144. * As devm_* managed functions are used in mmc_gpio_request_cd(), client
  145. * drivers do not need to worry about freeing up memory.
  146. *
  147. * If GPIO debouncing is desired, set the debounce parameter to a non-zero
  148. * value. The caller is responsible for ensuring that the GPIO driver associated
  149. * with the GPIO supports debouncing, otherwise an error will be returned.
  150. *
  151. * Returns zero on success, else an error.
  152. */
  153. int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
  154. unsigned int debounce)
  155. {
  156. struct mmc_gpio *ctx = host->slot.handler_priv;
  157. int ret;
  158. ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
  159. ctx->cd_label);
  160. if (ret < 0)
  161. /*
  162. * don't bother freeing memory. It might still get used by other
  163. * slot functions, in any case it will be freed, when the device
  164. * is destroyed.
  165. */
  166. return ret;
  167. if (debounce) {
  168. ret = gpio_set_debounce(gpio, debounce);
  169. if (ret < 0)
  170. return ret;
  171. }
  172. ctx->override_cd_active_level = true;
  173. ctx->cd_gpio = gpio_to_desc(gpio);
  174. return 0;
  175. }
  176. EXPORT_SYMBOL(mmc_gpio_request_cd);
  177. /**
  178. * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
  179. * @host: mmc host
  180. * @con_id: function within the GPIO consumer
  181. * @idx: index of the GPIO to obtain in the consumer
  182. * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
  183. * @debounce: debounce time in microseconds
  184. * @gpio_invert: will return whether the GPIO line is inverted or not, set
  185. * to NULL to ignore
  186. *
  187. * Use this function in place of mmc_gpio_request_cd() to use the GPIO
  188. * descriptor API. Note that it must be called prior to mmc_add_host()
  189. * otherwise the caller must also call mmc_gpiod_request_cd_irq().
  190. *
  191. * Returns zero on success, else an error.
  192. */
  193. int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
  194. unsigned int idx, bool override_active_level,
  195. unsigned int debounce, bool *gpio_invert)
  196. {
  197. struct mmc_gpio *ctx = host->slot.handler_priv;
  198. struct gpio_desc *desc;
  199. int ret;
  200. if (!con_id)
  201. con_id = ctx->cd_label;
  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. /**
  218. * mmc_gpiod_request_ro - request a gpio descriptor for write protection
  219. * @host: mmc host
  220. * @con_id: function within the GPIO consumer
  221. * @idx: index of the GPIO to obtain in the consumer
  222. * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
  223. * @debounce: debounce time in microseconds
  224. * @gpio_invert: will return whether the GPIO line is inverted or not,
  225. * set to NULL to ignore
  226. *
  227. * Use this function in place of mmc_gpio_request_ro() to use the GPIO
  228. * descriptor API.
  229. *
  230. * Returns zero on success, else an error.
  231. */
  232. int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
  233. unsigned int idx, bool override_active_level,
  234. unsigned int debounce, bool *gpio_invert)
  235. {
  236. struct mmc_gpio *ctx = host->slot.handler_priv;
  237. struct gpio_desc *desc;
  238. int ret;
  239. if (!con_id)
  240. con_id = ctx->ro_label;
  241. desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
  242. if (IS_ERR(desc))
  243. return PTR_ERR(desc);
  244. if (debounce) {
  245. ret = gpiod_set_debounce(desc, debounce);
  246. if (ret < 0)
  247. return ret;
  248. }
  249. if (gpio_invert)
  250. *gpio_invert = !gpiod_is_active_low(desc);
  251. ctx->override_ro_active_level = override_active_level;
  252. ctx->ro_gpio = desc;
  253. return 0;
  254. }
  255. EXPORT_SYMBOL(mmc_gpiod_request_ro);