206-spi-ath79-make-chipselect-logic-more-flexible.patch 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. From 7008284716403237f6bc7d7590b3ed073555bd56 Mon Sep 17 00:00:00 2001
  2. From: Gabor Juhos <juhosg@openwrt.org>
  3. Date: Wed, 11 Jan 2012 22:25:11 +0100
  4. Subject: [PATCH 34/34] spi/ath79: make chipselect logic more flexible
  5. Signed-off-by: Gabor Juhos <juhosg@openwrt.org>
  6. ---
  7. arch/mips/ath79/mach-pb44.c | 6 ++
  8. .../include/asm/mach-ath79/ath79_spi_platform.h | 8 ++-
  9. drivers/spi/spi-ath79.c | 67 +++++++++++++-------
  10. 8 files changed, 88 insertions(+), 23 deletions(-)
  11. --- a/arch/mips/ath79/mach-pb44.c
  12. +++ b/arch/mips/ath79/mach-pb44.c
  13. @@ -87,12 +87,18 @@ static struct gpio_keys_button pb44_gpio
  14. }
  15. };
  16. +static struct ath79_spi_controller_data pb44_spi0_data = {
  17. + .cs_type = ATH79_SPI_CS_TYPE_INTERNAL,
  18. + .cs_line = 0,
  19. +};
  20. +
  21. static struct spi_board_info pb44_spi_info[] = {
  22. {
  23. .bus_num = 0,
  24. .chip_select = 0,
  25. .max_speed_hz = 25000000,
  26. .modalias = "m25p64",
  27. + .controller_data = &pb44_spi0_data,
  28. },
  29. };
  30. --- a/arch/mips/include/asm/mach-ath79/ath79_spi_platform.h
  31. +++ b/arch/mips/include/asm/mach-ath79/ath79_spi_platform.h
  32. @@ -16,8 +16,14 @@ struct ath79_spi_platform_data {
  33. unsigned num_chipselect;
  34. };
  35. +enum ath79_spi_cs_type {
  36. + ATH79_SPI_CS_TYPE_INTERNAL,
  37. + ATH79_SPI_CS_TYPE_GPIO,
  38. +};
  39. +
  40. struct ath79_spi_controller_data {
  41. - unsigned gpio;
  42. + enum ath79_spi_cs_type cs_type;
  43. + unsigned cs_line;
  44. };
  45. #endif /* _ATH79_SPI_PLATFORM_H */
  46. --- a/drivers/spi/spi-ath79.c
  47. +++ b/drivers/spi/spi-ath79.c
  48. @@ -33,6 +33,8 @@
  49. #define ATH79_SPI_RRW_DELAY_FACTOR 12000
  50. #define MHZ (1000 * 1000)
  51. +#define ATH79_SPI_CS_LINE_MAX 2
  52. +
  53. struct ath79_spi {
  54. struct spi_bitbang bitbang;
  55. u32 ioc_base;
  56. @@ -67,6 +69,7 @@ static void ath79_spi_chipselect(struct
  57. {
  58. struct ath79_spi *sp = ath79_spidev_to_sp(spi);
  59. int cs_high = (spi->mode & SPI_CS_HIGH) ? is_active : !is_active;
  60. + struct ath79_spi_controller_data *cdata = spi->controller_data;
  61. if (is_active) {
  62. /* set initial clock polarity */
  63. @@ -78,20 +81,24 @@ static void ath79_spi_chipselect(struct
  64. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
  65. }
  66. - if (spi->chip_select) {
  67. - struct ath79_spi_controller_data *cdata = spi->controller_data;
  68. -
  69. - /* SPI is normally active-low */
  70. - gpio_set_value(cdata->gpio, cs_high);
  71. - } else {
  72. + switch (cdata->cs_type) {
  73. + case ATH79_SPI_CS_TYPE_INTERNAL:
  74. if (cs_high)
  75. - sp->ioc_base |= AR71XX_SPI_IOC_CS0;
  76. + sp->ioc_base |= AR71XX_SPI_IOC_CS(cdata->cs_line);
  77. else
  78. - sp->ioc_base &= ~AR71XX_SPI_IOC_CS0;
  79. + sp->ioc_base &= ~AR71XX_SPI_IOC_CS(cdata->cs_line);
  80. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
  81. - }
  82. + break;
  83. + case ATH79_SPI_CS_TYPE_GPIO:
  84. + /* SPI is normally active-low */
  85. + if (gpio_cansleep(cdata->cs_line))
  86. + gpio_set_value_cansleep(cdata->cs_line, cs_high);
  87. + else
  88. + gpio_set_value(cdata->cs_line, cs_high);
  89. + break;
  90. + }
  91. }
  92. static void ath79_spi_enable(struct ath79_spi *sp)
  93. @@ -119,14 +126,15 @@ static int ath79_spi_setup_cs(struct spi
  94. {
  95. struct ath79_spi *sp = ath79_spidev_to_sp(spi);
  96. struct ath79_spi_controller_data *cdata = spi->controller_data;
  97. + unsigned long flags;
  98. int status;
  99. - if (spi->chip_select && (!cdata || !gpio_is_valid(cdata->gpio)))
  100. + if (!cdata)
  101. return -EINVAL;
  102. status = 0;
  103. - if (spi->chip_select) {
  104. - unsigned long flags;
  105. + switch (cdata->cs_type) {
  106. + case ATH79_SPI_CS_TYPE_GPIO:
  107. flags = GPIOF_DIR_OUT;
  108. if (spi->mode & SPI_CS_HIGH)
  109. @@ -134,15 +142,21 @@ static int ath79_spi_setup_cs(struct spi
  110. else
  111. flags |= GPIOF_INIT_HIGH;
  112. - status = gpio_request_one(cdata->gpio, flags,
  113. + status = gpio_request_one(cdata->cs_line, flags,
  114. dev_name(&spi->dev));
  115. - } else {
  116. + break;
  117. + case ATH79_SPI_CS_TYPE_INTERNAL:
  118. + if (cdata->cs_line > ATH79_SPI_CS_LINE_MAX)
  119. + status = -EINVAL;
  120. + break;
  121. +
  122. if (spi->mode & SPI_CS_HIGH)
  123. sp->ioc_base &= ~AR71XX_SPI_IOC_CS0;
  124. else
  125. sp->ioc_base |= AR71XX_SPI_IOC_CS0;
  126. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
  127. + break;
  128. }
  129. return status;
  130. @@ -150,9 +164,19 @@ static int ath79_spi_setup_cs(struct spi
  131. static void ath79_spi_cleanup_cs(struct spi_device *spi)
  132. {
  133. - if (spi->chip_select) {
  134. - struct ath79_spi_controller_data *cdata = spi->controller_data;
  135. - gpio_free(cdata->gpio);
  136. + struct ath79_spi_controller_data *cdata;
  137. +
  138. + cdata = spi->controller_data;
  139. + if (!cdata)
  140. + return;
  141. +
  142. + switch (cdata->cs_type) {
  143. + case ATH79_SPI_CS_TYPE_INTERNAL:
  144. + /* nothing to do */
  145. + break;
  146. + case ATH79_SPI_CS_TYPE_GPIO:
  147. + gpio_free(cdata->cs_line);
  148. + break;
  149. }
  150. }
  151. @@ -217,6 +241,10 @@ static int ath79_spi_probe(struct platfo
  152. unsigned long rate;
  153. int ret;
  154. + pdata = pdev->dev.platform_data;
  155. + if (!pdata)
  156. + return -EINVAL;
  157. +
  158. master = spi_alloc_master(&pdev->dev, sizeof(*sp));
  159. if (master == NULL) {
  160. dev_err(&pdev->dev, "failed to allocate spi master\n");
  161. @@ -226,15 +254,11 @@ static int ath79_spi_probe(struct platfo
  162. sp = spi_master_get_devdata(master);
  163. platform_set_drvdata(pdev, sp);
  164. - pdata = dev_get_platdata(&pdev->dev);
  165. -
  166. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
  167. master->setup = ath79_spi_setup;
  168. master->cleanup = ath79_spi_cleanup;
  169. - if (pdata) {
  170. - master->bus_num = pdata->bus_num;
  171. - master->num_chipselect = pdata->num_chipselect;
  172. - }
  173. + master->bus_num = pdata->bus_num;
  174. + master->num_chipselect = pdata->num_chipselect;
  175. sp->bitbang.master = master;
  176. sp->bitbang.chipselect = ath79_spi_chipselect;