of_mmc_spi.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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_read_gpio(struct device *dev, int gpio_num)
  47. {
  48. struct of_mmc_spi *oms = to_of_mmc_spi(dev);
  49. bool active_low = oms->alow_gpios[gpio_num];
  50. bool value = gpio_get_value(oms->gpios[gpio_num]);
  51. return active_low ^ value;
  52. }
  53. static int of_mmc_spi_get_cd(struct device *dev)
  54. {
  55. return of_mmc_spi_read_gpio(dev, CD_GPIO);
  56. }
  57. static int of_mmc_spi_get_ro(struct device *dev)
  58. {
  59. return of_mmc_spi_read_gpio(dev, WP_GPIO);
  60. }
  61. static int of_mmc_spi_init(struct device *dev,
  62. irqreturn_t (*irqhandler)(int, void *), void *mmc)
  63. {
  64. struct of_mmc_spi *oms = to_of_mmc_spi(dev);
  65. return request_threaded_irq(oms->detect_irq, NULL, irqhandler, 0,
  66. dev_name(dev), mmc);
  67. }
  68. static void of_mmc_spi_exit(struct device *dev, void *mmc)
  69. {
  70. struct of_mmc_spi *oms = to_of_mmc_spi(dev);
  71. free_irq(oms->detect_irq, mmc);
  72. }
  73. struct mmc_spi_platform_data *mmc_spi_get_pdata(struct spi_device *spi)
  74. {
  75. struct device *dev = &spi->dev;
  76. struct device_node *np = dev->of_node;
  77. struct of_mmc_spi *oms;
  78. const u32 *voltage_ranges;
  79. int num_ranges;
  80. int i;
  81. int ret = -EINVAL;
  82. if (dev->platform_data || !np)
  83. return dev->platform_data;
  84. oms = kzalloc(sizeof(*oms), GFP_KERNEL);
  85. if (!oms)
  86. return NULL;
  87. voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
  88. num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
  89. if (!voltage_ranges || !num_ranges) {
  90. dev_err(dev, "OF: voltage-ranges unspecified\n");
  91. goto err_ocr;
  92. }
  93. for (i = 0; i < num_ranges; i++) {
  94. const int j = i * 2;
  95. u32 mask;
  96. mask = mmc_vddrange_to_ocrmask(be32_to_cpu(voltage_ranges[j]),
  97. be32_to_cpu(voltage_ranges[j + 1]));
  98. if (!mask) {
  99. ret = -EINVAL;
  100. dev_err(dev, "OF: voltage-range #%d is invalid\n", i);
  101. goto err_ocr;
  102. }
  103. oms->pdata.ocr_mask |= mask;
  104. }
  105. for (i = 0; i < ARRAY_SIZE(oms->gpios); i++) {
  106. enum of_gpio_flags gpio_flags;
  107. oms->gpios[i] = of_get_gpio_flags(np, i, &gpio_flags);
  108. if (!gpio_is_valid(oms->gpios[i]))
  109. continue;
  110. ret = gpio_request(oms->gpios[i], dev_name(dev));
  111. if (ret < 0) {
  112. oms->gpios[i] = -EINVAL;
  113. continue;
  114. }
  115. if (gpio_flags & OF_GPIO_ACTIVE_LOW)
  116. oms->alow_gpios[i] = true;
  117. }
  118. if (gpio_is_valid(oms->gpios[CD_GPIO]))
  119. oms->pdata.get_cd = of_mmc_spi_get_cd;
  120. if (gpio_is_valid(oms->gpios[WP_GPIO]))
  121. oms->pdata.get_ro = of_mmc_spi_get_ro;
  122. oms->detect_irq = irq_of_parse_and_map(np, 0);
  123. if (oms->detect_irq != NO_IRQ) {
  124. oms->pdata.init = of_mmc_spi_init;
  125. oms->pdata.exit = of_mmc_spi_exit;
  126. } else {
  127. oms->pdata.caps |= MMC_CAP_NEEDS_POLL;
  128. }
  129. dev->platform_data = &oms->pdata;
  130. return dev->platform_data;
  131. err_ocr:
  132. kfree(oms);
  133. return NULL;
  134. }
  135. EXPORT_SYMBOL(mmc_spi_get_pdata);
  136. void mmc_spi_put_pdata(struct spi_device *spi)
  137. {
  138. struct device *dev = &spi->dev;
  139. struct device_node *np = dev->of_node;
  140. struct of_mmc_spi *oms = to_of_mmc_spi(dev);
  141. int i;
  142. if (!dev->platform_data || !np)
  143. return;
  144. for (i = 0; i < ARRAY_SIZE(oms->gpios); i++) {
  145. if (gpio_is_valid(oms->gpios[i]))
  146. gpio_free(oms->gpios[i]);
  147. }
  148. kfree(oms);
  149. dev->platform_data = NULL;
  150. }
  151. EXPORT_SYMBOL(mmc_spi_put_pdata);