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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. @@ -118,24 +125,30 @@ static void ath79_spi_disable(struct ath
  94. static int ath79_spi_setup_cs(struct spi_device *spi)
  95. {
  96. struct ath79_spi_controller_data *cdata;
  97. + unsigned long flags;
  98. int status;
  99. cdata = spi->controller_data;
  100. - if (spi->chip_select && !cdata)
  101. + if (!cdata)
  102. return -EINVAL;
  103. status = 0;
  104. - if (spi->chip_select) {
  105. - unsigned long flags;
  106. + switch (cdata->cs_type) {
  107. + case ATH79_SPI_CS_TYPE_INTERNAL:
  108. + if (cdata->cs_line > ATH79_SPI_CS_LINE_MAX)
  109. + status = -EINVAL;
  110. + break;
  111. + case ATH79_SPI_CS_TYPE_GPIO:
  112. flags = GPIOF_DIR_OUT;
  113. if (spi->mode & SPI_CS_HIGH)
  114. flags |= GPIOF_INIT_LOW;
  115. else
  116. flags |= GPIOF_INIT_HIGH;
  117. - status = gpio_request_one(cdata->gpio, flags,
  118. + status = gpio_request_one(cdata->cs_line, flags,
  119. dev_name(&spi->dev));
  120. + break;
  121. }
  122. return status;
  123. @@ -143,9 +156,19 @@ static int ath79_spi_setup_cs(struct spi
  124. static void ath79_spi_cleanup_cs(struct spi_device *spi)
  125. {
  126. - if (spi->chip_select) {
  127. - struct ath79_spi_controller_data *cdata = spi->controller_data;
  128. - gpio_free(cdata->gpio);
  129. + struct ath79_spi_controller_data *cdata;
  130. +
  131. + cdata = spi->controller_data;
  132. + if (!cdata)
  133. + return;
  134. +
  135. + switch (cdata->cs_type) {
  136. + case ATH79_SPI_CS_TYPE_INTERNAL:
  137. + /* nothing to do */
  138. + break;
  139. + case ATH79_SPI_CS_TYPE_GPIO:
  140. + gpio_free(cdata->cs_line);
  141. + break;
  142. }
  143. }
  144. @@ -210,6 +233,10 @@ static int ath79_spi_probe(struct platfo
  145. unsigned long rate;
  146. int ret;
  147. + pdata = pdev->dev.platform_data;
  148. + if (!pdata)
  149. + return -EINVAL;
  150. +
  151. master = spi_alloc_master(&pdev->dev, sizeof(*sp));
  152. if (master == NULL) {
  153. dev_err(&pdev->dev, "failed to allocate spi master\n");
  154. @@ -219,15 +246,11 @@ static int ath79_spi_probe(struct platfo
  155. sp = spi_master_get_devdata(master);
  156. platform_set_drvdata(pdev, sp);
  157. - pdata = dev_get_platdata(&pdev->dev);
  158. -
  159. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
  160. master->setup = ath79_spi_setup;
  161. master->cleanup = ath79_spi_cleanup;
  162. - if (pdata) {
  163. - master->bus_num = pdata->bus_num;
  164. - master->num_chipselect = pdata->num_chipselect;
  165. - }
  166. + master->bus_num = pdata->bus_num;
  167. + master->num_chipselect = pdata->num_chipselect;
  168. sp->bitbang.master = master;
  169. sp->bitbang.chipselect = ath79_spi_chipselect;