slot-gpio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. return 0;
  186. }
  187. EXPORT_SYMBOL(mmc_gpio_request_cd);
  188. /**
  189. * mmc_gpio_free_ro - free the write-protection gpio
  190. * @host: mmc host
  191. *
  192. * It's provided only for cases that client drivers need to manually free
  193. * up the write-protection gpio requested by mmc_gpio_request_ro().
  194. */
  195. void mmc_gpio_free_ro(struct mmc_host *host)
  196. {
  197. struct mmc_gpio *ctx = host->slot.handler_priv;
  198. int gpio;
  199. if (!ctx || !ctx->ro_gpio)
  200. return;
  201. gpio = desc_to_gpio(ctx->ro_gpio);
  202. ctx->ro_gpio = NULL;
  203. devm_gpio_free(&host->class_dev, gpio);
  204. }
  205. EXPORT_SYMBOL(mmc_gpio_free_ro);
  206. /**
  207. * mmc_gpio_free_cd - free the card-detection gpio
  208. * @host: mmc host
  209. *
  210. * It's provided only for cases that client drivers need to manually free
  211. * up the card-detection gpio requested by mmc_gpio_request_cd().
  212. */
  213. void mmc_gpio_free_cd(struct mmc_host *host)
  214. {
  215. struct mmc_gpio *ctx = host->slot.handler_priv;
  216. int gpio;
  217. if (!ctx || !ctx->cd_gpio)
  218. return;
  219. if (host->slot.cd_irq >= 0) {
  220. devm_free_irq(&host->class_dev, host->slot.cd_irq, host);
  221. host->slot.cd_irq = -EINVAL;
  222. }
  223. gpio = desc_to_gpio(ctx->cd_gpio);
  224. ctx->cd_gpio = NULL;
  225. devm_gpio_free(&host->class_dev, gpio);
  226. }
  227. EXPORT_SYMBOL(mmc_gpio_free_cd);
  228. /**
  229. * mmc_gpiod_request_cd - request a gpio descriptor for card-detection
  230. * @host: mmc host
  231. * @con_id: function within the GPIO consumer
  232. * @idx: index of the GPIO to obtain in the consumer
  233. * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
  234. * @debounce: debounce time in microseconds
  235. * @gpio_invert: will return whether the GPIO line is inverted or not, set
  236. * to NULL to ignore
  237. *
  238. * Use this function in place of mmc_gpio_request_cd() to use the GPIO
  239. * descriptor API. Note that it is paired with mmc_gpiod_free_cd() not
  240. * mmc_gpio_free_cd(). Note also that it must be called prior to mmc_add_host()
  241. * otherwise the caller must also call mmc_gpiod_request_cd_irq().
  242. *
  243. * Returns zero on success, else an error.
  244. */
  245. int mmc_gpiod_request_cd(struct mmc_host *host, const char *con_id,
  246. unsigned int idx, bool override_active_level,
  247. unsigned int debounce, bool *gpio_invert)
  248. {
  249. struct mmc_gpio *ctx;
  250. struct gpio_desc *desc;
  251. int ret;
  252. ret = mmc_gpio_alloc(host);
  253. if (ret < 0)
  254. return ret;
  255. ctx = host->slot.handler_priv;
  256. if (!con_id)
  257. con_id = ctx->cd_label;
  258. desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
  259. if (IS_ERR(desc))
  260. return PTR_ERR(desc);
  261. if (debounce) {
  262. ret = gpiod_set_debounce(desc, debounce);
  263. if (ret < 0)
  264. return ret;
  265. }
  266. if (gpio_invert)
  267. *gpio_invert = !gpiod_is_active_low(desc);
  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_request_ro - request a gpio descriptor for write protection
  275. * @host: mmc host
  276. * @con_id: function within the GPIO consumer
  277. * @idx: index of the GPIO to obtain in the consumer
  278. * @override_active_level: ignore %GPIO_ACTIVE_LOW flag
  279. * @debounce: debounce time in microseconds
  280. * @gpio_invert: will return whether the GPIO line is inverted or not,
  281. * set to NULL to ignore
  282. *
  283. * Use this function in place of mmc_gpio_request_ro() to use the GPIO
  284. * descriptor API. Note that it is paired with mmc_gpiod_free_ro() not
  285. * mmc_gpio_free_ro().
  286. *
  287. * Returns zero on success, else an error.
  288. */
  289. int mmc_gpiod_request_ro(struct mmc_host *host, const char *con_id,
  290. unsigned int idx, bool override_active_level,
  291. unsigned int debounce, bool *gpio_invert)
  292. {
  293. struct mmc_gpio *ctx;
  294. struct gpio_desc *desc;
  295. int ret;
  296. ret = mmc_gpio_alloc(host);
  297. if (ret < 0)
  298. return ret;
  299. ctx = host->slot.handler_priv;
  300. if (!con_id)
  301. con_id = ctx->ro_label;
  302. desc = devm_gpiod_get_index(host->parent, con_id, idx, GPIOD_IN);
  303. if (IS_ERR(desc))
  304. return PTR_ERR(desc);
  305. if (debounce) {
  306. ret = gpiod_set_debounce(desc, debounce);
  307. if (ret < 0)
  308. return ret;
  309. }
  310. if (gpio_invert)
  311. *gpio_invert = !gpiod_is_active_low(desc);
  312. ctx->override_ro_active_level = override_active_level;
  313. ctx->ro_gpio = desc;
  314. return 0;
  315. }
  316. EXPORT_SYMBOL(mmc_gpiod_request_ro);
  317. /**
  318. * mmc_gpiod_free_cd - free the card-detection gpio descriptor
  319. * @host: mmc host
  320. *
  321. * It's provided only for cases that client drivers need to manually free
  322. * up the card-detection gpio requested by mmc_gpiod_request_cd().
  323. */
  324. void mmc_gpiod_free_cd(struct mmc_host *host)
  325. {
  326. struct mmc_gpio *ctx = host->slot.handler_priv;
  327. if (!ctx || !ctx->cd_gpio)
  328. return;
  329. if (host->slot.cd_irq >= 0) {
  330. devm_free_irq(&host->class_dev, host->slot.cd_irq, host);
  331. host->slot.cd_irq = -EINVAL;
  332. }
  333. devm_gpiod_put(host->parent, ctx->cd_gpio);
  334. ctx->cd_gpio = NULL;
  335. }
  336. EXPORT_SYMBOL(mmc_gpiod_free_cd);