slot-gpio.c 8.9 KB

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