of_mmc_spi.c 3.5 KB

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