slot-gpio.c 10 KB

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