of_mmc_spi.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/gpio.h>
  17. #include <linux/of.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/spi/mmc_spi.h>
  21. #include <linux/mmc/core.h>
  22. #include <linux/mmc/host.h>
  23. MODULE_LICENSE("GPL");
  24. enum {
  25. CD_GPIO = 0,
  26. WP_GPIO,
  27. NUM_GPIOS,
  28. };
  29. struct of_mmc_spi {
  30. int gpios[NUM_GPIOS];
  31. bool alow_gpios[NUM_GPIOS];
  32. struct mmc_spi_platform_data pdata;
  33. };
  34. static struct of_mmc_spi *to_of_mmc_spi(struct device *dev)
  35. {
  36. return container_of(dev->platform_data, struct of_mmc_spi, pdata);
  37. }
  38. static int of_mmc_spi_read_gpio(struct device *dev, int gpio_num)
  39. {
  40. struct of_mmc_spi *oms = to_of_mmc_spi(dev);
  41. bool active_low = oms->alow_gpios[gpio_num];
  42. bool value = gpio_get_value(oms->gpios[gpio_num]);
  43. return active_low ^ value;
  44. }
  45. static int of_mmc_spi_get_cd(struct device *dev)
  46. {
  47. return of_mmc_spi_read_gpio(dev, CD_GPIO);
  48. }
  49. static int of_mmc_spi_get_ro(struct device *dev)
  50. {
  51. return of_mmc_spi_read_gpio(dev, WP_GPIO);
  52. }
  53. struct mmc_spi_platform_data *mmc_spi_get_pdata(struct spi_device *spi)
  54. {
  55. struct device *dev = &spi->dev;
  56. struct device_node *np = dev_archdata_get_node(&dev->archdata);
  57. struct of_mmc_spi *oms;
  58. const u32 *voltage_ranges;
  59. int num_ranges;
  60. int i;
  61. int ret = -EINVAL;
  62. if (dev->platform_data || !np)
  63. return dev->platform_data;
  64. oms = kzalloc(sizeof(*oms), GFP_KERNEL);
  65. if (!oms)
  66. return NULL;
  67. voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
  68. num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
  69. if (!voltage_ranges || !num_ranges) {
  70. dev_err(dev, "OF: voltage-ranges unspecified\n");
  71. goto err_ocr;
  72. }
  73. for (i = 0; i < num_ranges; i++) {
  74. const int j = i * 2;
  75. u32 mask;
  76. mask = mmc_vddrange_to_ocrmask(voltage_ranges[j],
  77. voltage_ranges[j + 1]);
  78. if (!mask) {
  79. ret = -EINVAL;
  80. dev_err(dev, "OF: voltage-range #%d is invalid\n", i);
  81. goto err_ocr;
  82. }
  83. oms->pdata.ocr_mask |= mask;
  84. }
  85. for (i = 0; i < ARRAY_SIZE(oms->gpios); i++) {
  86. enum of_gpio_flags gpio_flags;
  87. oms->gpios[i] = of_get_gpio_flags(np, i, &gpio_flags);
  88. if (!gpio_is_valid(oms->gpios[i]))
  89. continue;
  90. ret = gpio_request(oms->gpios[i], dev_name(dev));
  91. if (ret < 0) {
  92. oms->gpios[i] = -EINVAL;
  93. continue;
  94. }
  95. if (gpio_flags & OF_GPIO_ACTIVE_LOW)
  96. oms->alow_gpios[i] = true;
  97. }
  98. if (gpio_is_valid(oms->gpios[CD_GPIO]))
  99. oms->pdata.get_cd = of_mmc_spi_get_cd;
  100. if (gpio_is_valid(oms->gpios[WP_GPIO]))
  101. oms->pdata.get_ro = of_mmc_spi_get_ro;
  102. /* We don't support interrupts yet, let's poll. */
  103. oms->pdata.caps |= MMC_CAP_NEEDS_POLL;
  104. dev->platform_data = &oms->pdata;
  105. return dev->platform_data;
  106. err_ocr:
  107. kfree(oms);
  108. return NULL;
  109. }
  110. EXPORT_SYMBOL(mmc_spi_get_pdata);
  111. void mmc_spi_put_pdata(struct spi_device *spi)
  112. {
  113. struct device *dev = &spi->dev;
  114. struct device_node *np = dev_archdata_get_node(&dev->archdata);
  115. struct of_mmc_spi *oms = to_of_mmc_spi(dev);
  116. int i;
  117. if (!dev->platform_data || !np)
  118. return;
  119. for (i = 0; i < ARRAY_SIZE(oms->gpios); i++) {
  120. if (gpio_is_valid(oms->gpios[i]))
  121. gpio_free(oms->gpios[i]);
  122. }
  123. kfree(oms);
  124. dev->platform_data = NULL;
  125. }
  126. EXPORT_SYMBOL(mmc_spi_put_pdata);