of_mmc_spi.c 3.5 KB

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