of_mmc_spi.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * OpenFirmware bindings for the MMC-over-SPI driver
  4. *
  5. * Copyright (c) MontaVista Software, Inc. 2008.
  6. *
  7. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/device.h>
  12. #include <linux/slab.h>
  13. #include <linux/irq.h>
  14. #include <linux/of.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/spi/spi.h>
  17. #include <linux/spi/mmc_spi.h>
  18. #include <linux/mmc/core.h>
  19. #include <linux/mmc/host.h>
  20. /* For archs that don't support NO_IRQ (such as mips), provide a dummy value */
  21. #ifndef NO_IRQ
  22. #define NO_IRQ 0
  23. #endif
  24. MODULE_LICENSE("GPL");
  25. struct of_mmc_spi {
  26. int detect_irq;
  27. struct mmc_spi_platform_data pdata;
  28. };
  29. static struct of_mmc_spi *to_of_mmc_spi(struct device *dev)
  30. {
  31. return container_of(dev->platform_data, struct of_mmc_spi, pdata);
  32. }
  33. static int of_mmc_spi_init(struct device *dev,
  34. irqreturn_t (*irqhandler)(int, void *), void *mmc)
  35. {
  36. struct of_mmc_spi *oms = to_of_mmc_spi(dev);
  37. return request_threaded_irq(oms->detect_irq, NULL, irqhandler,
  38. IRQF_ONESHOT, dev_name(dev), mmc);
  39. }
  40. static void of_mmc_spi_exit(struct device *dev, void *mmc)
  41. {
  42. struct of_mmc_spi *oms = to_of_mmc_spi(dev);
  43. free_irq(oms->detect_irq, mmc);
  44. }
  45. struct mmc_spi_platform_data *mmc_spi_get_pdata(struct spi_device *spi)
  46. {
  47. struct device *dev = &spi->dev;
  48. struct device_node *np = dev->of_node;
  49. struct of_mmc_spi *oms;
  50. if (dev->platform_data || !np)
  51. return dev->platform_data;
  52. oms = kzalloc(sizeof(*oms), GFP_KERNEL);
  53. if (!oms)
  54. return NULL;
  55. if (mmc_of_parse_voltage(np, &oms->pdata.ocr_mask) <= 0)
  56. goto err_ocr;
  57. oms->detect_irq = irq_of_parse_and_map(np, 0);
  58. if (oms->detect_irq != 0) {
  59. oms->pdata.init = of_mmc_spi_init;
  60. oms->pdata.exit = of_mmc_spi_exit;
  61. } else {
  62. oms->pdata.caps |= MMC_CAP_NEEDS_POLL;
  63. }
  64. dev->platform_data = &oms->pdata;
  65. return dev->platform_data;
  66. err_ocr:
  67. kfree(oms);
  68. return NULL;
  69. }
  70. EXPORT_SYMBOL(mmc_spi_get_pdata);
  71. void mmc_spi_put_pdata(struct spi_device *spi)
  72. {
  73. struct device *dev = &spi->dev;
  74. struct device_node *np = dev->of_node;
  75. struct of_mmc_spi *oms = to_of_mmc_spi(dev);
  76. if (!dev->platform_data || !np)
  77. return;
  78. kfree(oms);
  79. dev->platform_data = NULL;
  80. }
  81. EXPORT_SYMBOL(mmc_spi_put_pdata);