slot-gpio.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 irq = -EINVAL;
  102. int ret;
  103. if (host->slot.cd_irq >= 0 || !ctx || !ctx->cd_gpio)
  104. return;
  105. /*
  106. * Do not use IRQ if the platform prefers to poll, e.g., because that
  107. * IRQ number is already used by another unit and cannot be shared.
  108. */
  109. if (!(host->caps & MMC_CAP_NEEDS_POLL))
  110. irq = gpiod_to_irq(ctx->cd_gpio);
  111. if (irq >= 0) {
  112. if (!ctx->cd_gpio_isr)
  113. ctx->cd_gpio_isr = mmc_gpio_cd_irqt;
  114. ret = devm_request_threaded_irq(host->parent, irq,
  115. NULL, ctx->cd_gpio_isr,
  116. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  117. ctx->cd_label, host);
  118. if (ret < 0)
  119. irq = ret;
  120. }
  121. host->slot.cd_irq = irq;
  122. if (irq < 0)
  123. host->caps |= MMC_CAP_NEEDS_POLL;
  124. }
  125. EXPORT_SYMBOL(mmc_gpiod_request_cd_irq);
  126. int mmc_gpio_set_cd_wake(struct mmc_host *host, bool on)
  127. {
  128. int ret = 0;
  129. if (!(host->caps & MMC_CAP_CD_WAKE) ||
  130. host->slot.cd_irq < 0 ||
  131. on == host->slot.cd_wake_enabled)
  132. return 0;
  133. if (on) {
  134. ret = enable_irq_wake(host->slot.cd_irq);
  135. host->slot.cd_wake_enabled = !ret;
  136. } else {
  137. disable_irq_wake(host->slot.cd_irq);
  138. host->slot.cd_wake_enabled = false;
  139. }
  140. return ret;
  141. }
  142. EXPORT_SYMBOL(mmc_gpio_set_cd_wake);
  143. /* Register an alternate interrupt service routine for
  144. * the card-detect GPIO.
  145. */
  146. void mmc_gpio_set_cd_isr(struct mmc_host *host,
  147. irqreturn_t (*isr)(int irq, void *dev_id))
  148. {
  149. struct mmc_gpio *ctx = host->slot.handler_priv;
  150. WARN_ON(ctx->cd_gpio_isr);
  151. ctx->cd_gpio_isr = isr;
  152. }
  153. EXPORT_SYMBOL(mmc_gpio_set_cd_isr);
  154. /**
  155. * mmc_gpio_request_cd - request a gpio for card-detection
  156. * @host: mmc host
  157. * @gpio: gpio number requested
  158. * @debounce: debounce time in microseconds
  159. *
  160. * As devm_* managed functions are used in mmc_gpio_request_cd(), client
  161. * drivers do not need to worry about freeing up memory.
  162. *
  163. * If GPIO debouncing is desired, set the debounce parameter to a non-zero
  164. * value. The caller is responsible for ensuring that the GPIO driver associated
  165. * with the GPIO supports debouncing, otherwise an error will be returned.
  166. *
  167. * Returns zero on success, else an error.
  168. */
  169. int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio,
  170. unsigned int debounce)
  171. {
  172. struct mmc_gpio *ctx = host->slot.handler_priv;
  173. int ret;
  174. ret = devm_gpio_request_one(host->parent, gpio, GPIOF_DIR_IN,
  175. ctx->cd_label);
  176. if (ret < 0)
  177. /*
  178. * don't bother freeing memory. It might still get used by other
  179. * slot functions, in any case it will be freed, when the device
  180. * is destroyed.
  181. */
  182. return ret;
  183. if (debounce) {
  184. ret = gpio_set_debounce(gpio, debounce);
  185. if (ret < 0)
  186. return ret;
  187. }
  188. ctx->override_cd_active_level = true;
  189. ctx->cd_gpio = gpio_to_desc(gpio);
  190. return 0;
  191. }
  192. EXPORT_SYMBOL(mmc_gpio_request_cd);
  193. /**
  194. * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
  195. * @host: mmc host
  196. * @con_id: function within the GPIO consumer
  197. * @idx: index of the GPIO to obtain in the consumer
  198. * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
  199. * @debounce: debounce time in microseconds
  200. * @gpio_invert: will return whether the GPIO line is inverted or not, set
  201. * to NULL to ignore
  202. *
  203. * Use this function in place of mmc_gpio_request_cd() to use the GPIO
  204. * descriptor API. Note that it must be called prior to mmc_add_host()
  205. * otherwise the caller must also call mmc_gpiod_request_cd_irq().
  206. *
  207. * Returns zero on success, else an error.
  208. */
  209. int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
  210. unsigned int idx, bool override_active_level,
  211. unsigned int debounce, bool *gpio_invert)
  212. {
  213. struct mmc_gpio *ctx = host->slot.handler_priv;
  214. struct gpio_desc *desc;
  215. int ret;
  216. desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
  217. if (IS_ERR(desc))
  218. return PTR_ERR(desc);
  219. if (debounce) {
  220. ret = gpiod_set_debounce(desc, debounce);
  221. if (ret < 0)
  222. return ret;
  223. }
  224. if (gpio_invert)
  225. *gpio_invert = !gpiod_is_active_low(desc);
  226. ctx->override_cd_active_level = override_active_level;
  227. ctx->cd_gpio = desc;
  228. return 0;
  229. }
  230. EXPORT_SYMBOL(mmc_gpiod_request_cd);
  231. bool mmc_can_gpio_cd(struct mmc_host *host)
  232. {
  233. struct mmc_gpio *ctx = host->slot.handler_priv;
  234. return ctx->cd_gpio ? true : false;
  235. }
  236. EXPORT_SYMBOL(mmc_can_gpio_cd);
  237. /**
  238. * mmc_gpiod_request_ro - request a gpio descriptor for write protection
  239. * @host: mmc host
  240. * @con_id: function within the GPIO consumer
  241. * @idx: index of the GPIO to obtain in the consumer
  242. * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
  243. * @debounce: debounce time in microseconds
  244. * @gpio_invert: will return whether the GPIO line is inverted or not,
  245. * set to NULL to ignore
  246. *
  247. * Use this function in place of mmc_gpio_request_ro() to use the GPIO
  248. * descriptor API.
  249. *
  250. * Returns zero on success, else an error.
  251. */
  252. int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
  253. unsigned int idx, bool override_active_level,
  254. unsigned int debounce, bool *gpio_invert)
  255. {
  256. struct mmc_gpio *ctx = host->slot.handler_priv;
  257. struct gpio_desc *desc;
  258. int ret;
  259. desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
  260. if (IS_ERR(desc))
  261. return PTR_ERR(desc);
  262. if (debounce) {
  263. ret = gpiod_set_debounce(desc, debounce);
  264. if (ret < 0)
  265. return ret;
  266. }
  267. if (gpio_invert)
  268. *gpio_invert = !gpiod_is_active_low(desc);
  269. ctx->override_ro_active_level = override_active_level;
  270. ctx->ro_gpio = desc;
  271. return 0;
  272. }
  273. EXPORT_SYMBOL(mmc_gpiod_request_ro);
  274. bool mmc_can_gpio_ro(struct mmc_host *host)
  275. {
  276. struct mmc_gpio *ctx = host->slot.handler_priv;
  277. return ctx->ro_gpio ? true : false;
  278. }
  279. EXPORT_SYMBOL(mmc_can_gpio_ro);