of_mmc_spi.c 4.1 KB

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