slot-gpio.c 8.7 KB

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