of_mmc_spi.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * OpenFirmware bindings for the MMC-over-SPI driver
  3. *
  4. * Copyright (c) MontaVista Software, Inc. 2008.
  5. *
  6. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/device.h>
  16. #include <linux/slab.h>
  17. #include <linux/irq.h>
  18. #include <linux/gpio.h>
  19. #include <linux/of.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/spi/spi.h>
  23. #include <linux/spi/mmc_spi.h>
  24. #include <linux/mmc/core.h>
  25. #include <linux/mmc/host.h>
  26. /* For archs that don't support NO_IRQ (such as mips), provide a dummy value */
  27. #ifndef NO_IRQ
  28. #define NO_IRQ 0
  29. #endif
  30. MODULE_LICENSE("GPL");
  31. enum {
  32. CD_GPIO = 0,
  33. WP_GPIO,
  34. NUM_GPIOS,
  35. };
  36. struct of_mmc_spi {
  37. int gpios[NUM_GPIOS];
  38. bool alow_gpios[NUM_GPIOS];
  39. int detect_irq;
  40. struct mmc_spi_platform_data pdata;
  41. };
  42. static struct of_mmc_spi *to_of_mmc_spi(struct device *dev)
  43. {
  44. return container_of(dev->platform_data, struct of_mmc_spi, pdata);
  45. }
  46. static int of_mmc_spi_init(struct device *dev,
  47. irqreturn_t (*irqhandler)(int, void *), void *mmc)
  48. {
  49. struct of_mmc_spi *oms = to_of_mmc_spi(dev);
  50. return request_threaded_irq(oms->detect_irq, NULL, irqhandler,
  51. IRQF_ONESHOT, dev_name(dev), mmc);
  52. }
  53. static void of_mmc_spi_exit(struct device *dev, void *mmc)
  54. {
  55. struct of_mmc_spi *oms = to_of_mmc_spi(dev);
  56. free_irq(oms->detect_irq, mmc);
  57. }
  58. struct mmc_spi_platform_data *mmc_spi_get_pdata(struct spi_device *spi)
  59. {
  60. struct device *dev = &spi->dev;
  61. struct device_node *np = dev->of_node;
  62. struct of_mmc_spi *oms;
  63. const u32 *voltage_ranges;
  64. int num_ranges;
  65. int i;
  66. if (dev->platform_data || !np)
  67. return dev->platform_data;
  68. oms = kzalloc(sizeof(*oms), GFP_KERNEL);
  69. if (!oms)
  70. return NULL;
  71. voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
  72. num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
  73. if (!voltage_ranges || !num_ranges) {
  74. dev_err(dev, "OF: voltage-ranges unspecified\n");
  75. goto err_ocr;
  76. }
  77. for (i = 0; i < num_ranges; i++) {
  78. const int j = i * 2;
  79. u32 mask;
  80. mask = mmc_vddrange_to_ocrmask(be32_to_cpu(voltage_ranges[j]),
  81. be32_to_cpu(voltage_ranges[j + 1]));
  82. if (!mask) {
  83. dev_err(dev, "OF: voltage-range #%d is invalid\n", i);
  84. goto err_ocr;
  85. }
  86. oms->pdata.ocr_mask |= mask;
  87. }
  88. for (i = 0; i < ARRAY_SIZE(oms->gpios); i++) {
  89. enum of_gpio_flags gpio_flags;
  90. oms->gpios[i] = of_get_gpio_flags(np, i, &gpio_flags);
  91. if (!gpio_is_valid(oms->gpios[i]))
  92. continue;
  93. if (gpio_flags & OF_GPIO_ACTIVE_LOW)
  94. oms->alow_gpios[i] = true;
  95. }
  96. if (gpio_is_valid(oms->gpios[CD_GPIO])) {
  97. oms->pdata.cd_gpio = oms->gpios[CD_GPIO];
  98. oms->pdata.flags |= MMC_SPI_USE_CD_GPIO;
  99. if (!oms->alow_gpios[CD_GPIO])
  100. oms->pdata.caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
  101. }
  102. if (gpio_is_valid(oms->gpios[WP_GPIO])) {
  103. oms->pdata.ro_gpio = oms->gpios[WP_GPIO];
  104. oms->pdata.flags |= MMC_SPI_USE_RO_GPIO;
  105. if (!oms->alow_gpios[WP_GPIO])
  106. oms->pdata.caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
  107. }
  108. oms->detect_irq = irq_of_parse_and_map(np, 0);
  109. if (oms->detect_irq != 0) {
  110. oms->pdata.init = of_mmc_spi_init;
  111. oms->pdata.exit = of_mmc_spi_exit;
  112. } else {
  113. oms->pdata.caps |= MMC_CAP_NEEDS_POLL;
  114. }
  115. dev->platform_data = &oms->pdata;
  116. return dev->platform_data;
  117. err_ocr:
  118. kfree(oms);
  119. return NULL;
  120. }
  121. EXPORT_SYMBOL(mmc_spi_get_pdata);
  122. void mmc_spi_put_pdata(struct spi_device *spi)
  123. {
  124. struct device *dev = &spi->dev;
  125. struct device_node *np = dev->of_node;
  126. struct of_mmc_spi *oms = to_of_mmc_spi(dev);
  127. if (!dev->platform_data || !np)
  128. return;
  129. kfree(oms);
  130. dev->platform_data = NULL;
  131. }
  132. EXPORT_SYMBOL(mmc_spi_put_pdata);