slot-gpio.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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/interrupt.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/mmc/host.h>
  15. #include <linux/mmc/slot-gpio.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. struct mmc_gpio {
  19. int cd_gpio;
  20. char cd_label[0];
  21. };
  22. static irqreturn_t mmc_gpio_cd_irqt(int irq, void *dev_id)
  23. {
  24. /* Schedule a card detection after a debounce timeout */
  25. mmc_detect_change(dev_id, msecs_to_jiffies(100));
  26. return IRQ_HANDLED;
  27. }
  28. int mmc_gpio_get_cd(struct mmc_host *host)
  29. {
  30. struct mmc_gpio *ctx = host->slot.handler_priv;
  31. if (!ctx || !gpio_is_valid(ctx->cd_gpio))
  32. return -ENOSYS;
  33. return !gpio_get_value_cansleep(ctx->cd_gpio) ^
  34. !!(host->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
  35. }
  36. EXPORT_SYMBOL(mmc_gpio_get_cd);
  37. int mmc_gpio_request_cd(struct mmc_host *host, unsigned int gpio)
  38. {
  39. size_t len = strlen(dev_name(host->parent)) + 4;
  40. struct mmc_gpio *ctx;
  41. int irq = gpio_to_irq(gpio);
  42. int ret;
  43. ctx = kmalloc(sizeof(*ctx) + len, GFP_KERNEL);
  44. if (!ctx)
  45. return -ENOMEM;
  46. snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
  47. ret = gpio_request_one(gpio, GPIOF_DIR_IN, ctx->cd_label);
  48. if (ret < 0)
  49. goto egpioreq;
  50. /*
  51. * Even if gpio_to_irq() returns a valid IRQ number, the platform might
  52. * still prefer to poll, e.g., because that IRQ number is already used
  53. * by another unit and cannot be shared.
  54. */
  55. if (irq >= 0 && host->caps & MMC_CAP_NEEDS_POLL)
  56. irq = -EINVAL;
  57. if (irq >= 0) {
  58. ret = request_threaded_irq(irq, NULL, mmc_gpio_cd_irqt,
  59. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  60. ctx->cd_label, host);
  61. if (ret < 0)
  62. irq = ret;
  63. }
  64. host->slot.cd_irq = irq;
  65. if (irq < 0)
  66. host->caps |= MMC_CAP_NEEDS_POLL;
  67. ctx->cd_gpio = gpio;
  68. host->slot.handler_priv = ctx;
  69. return 0;
  70. egpioreq:
  71. kfree(ctx);
  72. return ret;
  73. }
  74. EXPORT_SYMBOL(mmc_gpio_request_cd);
  75. void mmc_gpio_free_cd(struct mmc_host *host)
  76. {
  77. struct mmc_gpio *ctx = host->slot.handler_priv;
  78. int gpio;
  79. if (!ctx || !gpio_is_valid(ctx->cd_gpio))
  80. return;
  81. if (host->slot.cd_irq >= 0) {
  82. free_irq(host->slot.cd_irq, host);
  83. host->slot.cd_irq = -EINVAL;
  84. }
  85. gpio = ctx->cd_gpio;
  86. ctx->cd_gpio = -EINVAL;
  87. gpio_free(gpio);
  88. host->slot.handler_priv = NULL;
  89. kfree(ctx);
  90. }
  91. EXPORT_SYMBOL(mmc_gpio_free_cd);