cd-gpio.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/module.h>
  16. #include <linux/slab.h>
  17. struct mmc_cd_gpio {
  18. unsigned int gpio;
  19. char label[0];
  20. };
  21. static irqreturn_t mmc_cd_gpio_irqt(int irq, void *dev_id)
  22. {
  23. /* Schedule a card detection after a debounce timeout */
  24. mmc_detect_change(dev_id, msecs_to_jiffies(100));
  25. return IRQ_HANDLED;
  26. }
  27. int mmc_cd_gpio_request(struct mmc_host *host, unsigned int gpio)
  28. {
  29. size_t len = strlen(dev_name(host->parent)) + 4;
  30. struct mmc_cd_gpio *cd;
  31. int irq = gpio_to_irq(gpio);
  32. int ret;
  33. if (irq < 0)
  34. return irq;
  35. cd = kmalloc(sizeof(*cd) + len, GFP_KERNEL);
  36. if (!cd)
  37. return -ENOMEM;
  38. snprintf(cd->label, len, "%s cd", dev_name(host->parent));
  39. ret = gpio_request_one(gpio, GPIOF_DIR_IN, cd->label);
  40. if (ret < 0)
  41. goto egpioreq;
  42. ret = request_threaded_irq(irq, NULL, mmc_cd_gpio_irqt,
  43. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
  44. cd->label, host);
  45. if (ret < 0)
  46. goto eirqreq;
  47. cd->gpio = gpio;
  48. host->hotplug.irq = irq;
  49. host->hotplug.handler_priv = cd;
  50. return 0;
  51. eirqreq:
  52. gpio_free(gpio);
  53. egpioreq:
  54. kfree(cd);
  55. return ret;
  56. }
  57. EXPORT_SYMBOL(mmc_cd_gpio_request);
  58. void mmc_cd_gpio_free(struct mmc_host *host)
  59. {
  60. struct mmc_cd_gpio *cd = host->hotplug.handler_priv;
  61. free_irq(host->hotplug.irq, host);
  62. gpio_free(cd->gpio);
  63. kfree(cd);
  64. }
  65. EXPORT_SYMBOL(mmc_cd_gpio_free);