950-0900-drivers-spi-Fix-spi-gpio-to-correctly-implement-sck-.patch 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. From 380c336af070edf85826abbb0057bf92a03ec466 Mon Sep 17 00:00:00 2001
  2. From: Nick Hollinghurst <[email protected]>
  3. Date: Wed, 1 Mar 2023 17:57:11 +0000
  4. Subject: [PATCH] drivers: spi: Fix spi-gpio to correctly implement
  5. sck-idle-input
  6. Formerly, if configured using DT, CS GPIOs were driven from spi.c
  7. and it was possible for CS to be asserted (low) *before* starting
  8. to drive SCK. CS GPIOs have been brought under control of this
  9. driver in both ACPI and DT cases, with a fixup for GPIO polarity.
  10. Signed-off-by: Nick Hollinghurst <[email protected]>
  11. ---
  12. drivers/spi/spi-gpio.c | 74 +++++++++++++++++++++++++++++-------------
  13. 1 file changed, 51 insertions(+), 23 deletions(-)
  14. --- a/drivers/spi/spi-gpio.c
  15. +++ b/drivers/spi/spi-gpio.c
  16. @@ -37,6 +37,7 @@ struct spi_gpio {
  17. struct gpio_desc *mosi;
  18. bool sck_idle_input;
  19. struct gpio_desc **cs_gpios;
  20. + bool cs_dont_invert;
  21. };
  22. /*----------------------------------------------------------------------*/
  23. @@ -233,12 +234,18 @@ static void spi_gpio_chipselect(struct s
  24. gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL);
  25. }
  26. - /* Drive chip select line, if we have one */
  27. + /*
  28. + * Drive chip select line, if we have one.
  29. + * SPI chip selects are normally active-low, but when
  30. + * cs_dont_invert is set, we assume their polarity is
  31. + * controlled by the GPIO, and write '1' to assert.
  32. + */
  33. if (spi_gpio->cs_gpios) {
  34. struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select];
  35. + int val = ((spi->mode & SPI_CS_HIGH) || spi_gpio->cs_dont_invert) ?
  36. + is_active : !is_active;
  37. - /* SPI chip selects are normally active-low */
  38. - gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
  39. + gpiod_set_value_cansleep(cs, val);
  40. }
  41. if (spi_gpio->sck_idle_input && !is_active)
  42. @@ -254,12 +261,14 @@ static int spi_gpio_setup(struct spi_dev
  43. /*
  44. * The CS GPIOs have already been
  45. * initialized from the descriptor lookup.
  46. + * Here we set them to the non-asserted state.
  47. */
  48. if (spi_gpio->cs_gpios) {
  49. cs = spi_gpio->cs_gpios[spi->chip_select];
  50. if (!spi->controller_state && cs)
  51. status = gpiod_direction_output(cs,
  52. - !(spi->mode & SPI_CS_HIGH));
  53. + !((spi->mode & SPI_CS_HIGH) ||
  54. + spi_gpio->cs_dont_invert));
  55. }
  56. if (!status)
  57. @@ -336,6 +345,38 @@ static int spi_gpio_request(struct devic
  58. return PTR_ERR_OR_ZERO(spi_gpio->sck);
  59. }
  60. +/*
  61. + * In order to implement "sck-idle-input" (which requires SCK
  62. + * direction and CS level to be switched in a particular order),
  63. + * we need to control GPIO chip selects from within this driver.
  64. + */
  65. +
  66. +static int spi_gpio_probe_get_cs_gpios(struct device *dev,
  67. + struct spi_master *master,
  68. + bool gpio_defines_polarity)
  69. +{
  70. + int i;
  71. + struct spi_gpio *spi_gpio = spi_master_get_devdata(master);
  72. +
  73. + spi_gpio->cs_dont_invert = gpio_defines_polarity;
  74. + spi_gpio->cs_gpios = devm_kcalloc(dev, master->num_chipselect,
  75. + sizeof(*spi_gpio->cs_gpios),
  76. + GFP_KERNEL);
  77. + if (!spi_gpio->cs_gpios)
  78. + return -ENOMEM;
  79. +
  80. + for (i = 0; i < master->num_chipselect; i++) {
  81. + spi_gpio->cs_gpios[i] =
  82. + devm_gpiod_get_index(dev, "cs", i,
  83. + gpio_defines_polarity ?
  84. + GPIOD_OUT_LOW : GPIOD_OUT_HIGH);
  85. + if (IS_ERR(spi_gpio->cs_gpios[i]))
  86. + return PTR_ERR(spi_gpio->cs_gpios[i]);
  87. + }
  88. +
  89. + return 0;
  90. +}
  91. +
  92. #ifdef CONFIG_OF
  93. static const struct of_device_id spi_gpio_dt_ids[] = {
  94. { .compatible = "spi-gpio" },
  95. @@ -346,10 +387,12 @@ MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids)
  96. static int spi_gpio_probe_dt(struct platform_device *pdev,
  97. struct spi_master *master)
  98. {
  99. - master->dev.of_node = pdev->dev.of_node;
  100. - master->use_gpio_descriptors = true;
  101. + struct device *dev = &pdev->dev;
  102. - return 0;
  103. + master->dev.of_node = dev->of_node;
  104. + master->num_chipselect = gpiod_count(dev, "cs");
  105. +
  106. + return spi_gpio_probe_get_cs_gpios(dev, master, true);
  107. }
  108. #else
  109. static inline int spi_gpio_probe_dt(struct platform_device *pdev,
  110. @@ -364,8 +407,6 @@ static int spi_gpio_probe_pdata(struct p
  111. {
  112. struct device *dev = &pdev->dev;
  113. struct spi_gpio_platform_data *pdata = dev_get_platdata(dev);
  114. - struct spi_gpio *spi_gpio = spi_master_get_devdata(master);
  115. - int i;
  116. #ifdef GENERIC_BITBANG
  117. if (!pdata || !pdata->num_chipselect)
  118. @@ -377,20 +418,7 @@ static int spi_gpio_probe_pdata(struct p
  119. */
  120. master->num_chipselect = pdata->num_chipselect ?: 1;
  121. - spi_gpio->cs_gpios = devm_kcalloc(dev, master->num_chipselect,
  122. - sizeof(*spi_gpio->cs_gpios),
  123. - GFP_KERNEL);
  124. - if (!spi_gpio->cs_gpios)
  125. - return -ENOMEM;
  126. -
  127. - for (i = 0; i < master->num_chipselect; i++) {
  128. - spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i,
  129. - GPIOD_OUT_HIGH);
  130. - if (IS_ERR(spi_gpio->cs_gpios[i]))
  131. - return PTR_ERR(spi_gpio->cs_gpios[i]);
  132. - }
  133. -
  134. - return 0;
  135. + return spi_gpio_probe_get_cs_gpios(dev, master, false);
  136. }
  137. static int spi_gpio_probe(struct platform_device *pdev)