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