slot-gpio.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. unsigned 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_request_cd(struct mmc_host *host, unsigned int gpio)
  29. {
  30. size_t len = strlen(dev_name(host->parent)) + 4;
  31. struct mmc_gpio *ctx;
  32. int irq = gpio_to_irq(gpio);
  33. int ret;
  34. if (irq < 0)
  35. return irq;
  36. ctx = kmalloc(sizeof(*ctx) + len, GFP_KERNEL);
  37. if (!ctx)
  38. return -ENOMEM;
  39. snprintf(ctx->cd_label, len, "%s cd", dev_name(host->parent));
  40. ret = gpio_request_one(gpio, GPIOF_DIR_IN, ctx->cd_label);
  41. if (ret < 0)
  42. goto egpioreq;
  43. ret = request_threaded_irq(irq, NULL, mmc_gpio_cd_irqt,
  44. IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  45. ctx->cd_label, host);
  46. if (ret < 0)
  47. goto eirqreq;
  48. ctx->cd_gpio = gpio;
  49. host->hotplug.irq = irq;
  50. host->hotplug.handler_priv = ctx;
  51. return 0;
  52. eirqreq:
  53. gpio_free(gpio);
  54. egpioreq:
  55. kfree(ctx);
  56. return ret;
  57. }
  58. EXPORT_SYMBOL(mmc_gpio_request_cd);
  59. void mmc_gpio_free_cd(struct mmc_host *host)
  60. {
  61. struct mmc_gpio *ctx = host->hotplug.handler_priv;
  62. if (!ctx)
  63. return;
  64. free_irq(host->hotplug.irq, host);
  65. gpio_free(ctx->cd_gpio);
  66. kfree(ctx);
  67. }
  68. EXPORT_SYMBOL(mmc_gpio_free_cd);