cd-gpio.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. unsigned int irq, unsigned long flags)
  29. {
  30. size_t len = strlen(dev_name(host->parent)) + 4;
  31. struct mmc_cd_gpio *cd = kmalloc(sizeof(*cd) + len, GFP_KERNEL);
  32. int ret;
  33. if (!cd)
  34. return -ENOMEM;
  35. snprintf(cd->label, len, "%s cd", dev_name(host->parent));
  36. ret = gpio_request_one(gpio, GPIOF_DIR_IN, cd->label);
  37. if (ret < 0)
  38. goto egpioreq;
  39. ret = request_threaded_irq(irq, NULL, mmc_cd_gpio_irqt,
  40. flags, cd->label, host);
  41. if (ret < 0)
  42. goto eirqreq;
  43. cd->gpio = gpio;
  44. host->hotplug.irq = irq;
  45. host->hotplug.handler_priv = cd;
  46. return 0;
  47. eirqreq:
  48. gpio_free(gpio);
  49. egpioreq:
  50. kfree(cd);
  51. return ret;
  52. }
  53. EXPORT_SYMBOL(mmc_cd_gpio_request);
  54. void mmc_cd_gpio_free(struct mmc_host *host)
  55. {
  56. struct mmc_cd_gpio *cd = host->hotplug.handler_priv;
  57. free_irq(host->hotplug.irq, host);
  58. gpio_free(cd->gpio);
  59. kfree(cd);
  60. }
  61. EXPORT_SYMBOL(mmc_cd_gpio_free);