eadcce4f90621a29f1c2d662e71c9fb883714b56
[openwrt/staging/linusw.git] /
1 From 78ced8c841ba08b41eacd064e7c7a9b3fe7556ad 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 0553/1085] spi: gpio: Fix spi-gpio to correctly implement
5 sck-idle-input
6
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.
11
12 Signed-off-by: Nick Hollinghurst <nick.hollinghurst@raspberrypi.com>
13 ---
14 drivers/spi/spi-gpio.c | 76 +++++++++++++++++++++++++++++-------------
15 1 file changed, 52 insertions(+), 24 deletions(-)
16
17 --- a/drivers/spi/spi-gpio.c
18 +++ b/drivers/spi/spi-gpio.c
19 @@ -34,8 +34,9 @@ struct spi_gpio {
20 struct gpio_desc *sck;
21 struct gpio_desc *miso;
22 struct gpio_desc *mosi;
23 - bool sck_idle_input;
24 struct gpio_desc **cs_gpios;
25 + bool sck_idle_input;
26 + bool cs_dont_invert;
27 };
28
29 /*----------------------------------------------------------------------*/
30 @@ -232,12 +233,18 @@ static void spi_gpio_chipselect(struct s
31 gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL);
32 }
33
34 - /* Drive chip select line, if we have one */
35 + /*
36 + * Drive chip select line, if we have one.
37 + * SPI chip selects are normally active-low, but when
38 + * cs_dont_invert is set, we assume their polarity is
39 + * controlled by the GPIO, and write '1' to assert.
40 + */
41 if (spi_gpio->cs_gpios) {
42 struct gpio_desc *cs = spi_gpio->cs_gpios[spi_get_chipselect(spi, 0)];
43 + int val = ((spi->mode & SPI_CS_HIGH) || spi_gpio->cs_dont_invert) ?
44 + is_active : !is_active;
45
46 - /* SPI chip selects are normally active-low */
47 - gpiod_set_value_cansleep(cs, (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
48 + gpiod_set_value_cansleep(cs, val);
49 }
50
51 if (spi_gpio->sck_idle_input && !is_active)
52 @@ -253,12 +260,14 @@ static int spi_gpio_setup(struct spi_dev
53 /*
54 * The CS GPIOs have already been
55 * initialized from the descriptor lookup.
56 + * Here we set them to the non-asserted state.
57 */
58 if (spi_gpio->cs_gpios) {
59 cs = spi_gpio->cs_gpios[spi_get_chipselect(spi, 0)];
60 if (!spi->controller_state && cs)
61 status = gpiod_direction_output(cs,
62 - !(spi->mode & SPI_CS_HIGH));
63 + !((spi->mode & SPI_CS_HIGH) ||
64 + spi_gpio->cs_dont_invert));
65 }
66
67 if (!status)
68 @@ -335,6 +344,38 @@ static int spi_gpio_request(struct devic
69 return PTR_ERR_OR_ZERO(spi_gpio->sck);
70 }
71
72 +/*
73 + * In order to implement "sck-idle-input" (which requires SCK
74 + * direction and CS level to be switched in a particular order),
75 + * we need to control GPIO chip selects from within this driver.
76 + */
77 +
78 +static int spi_gpio_probe_get_cs_gpios(struct device *dev,
79 + struct spi_master *master,
80 + bool gpio_defines_polarity)
81 +{
82 + int i;
83 + struct spi_gpio *spi_gpio = spi_master_get_devdata(master);
84 +
85 + spi_gpio->cs_dont_invert = gpio_defines_polarity;
86 + spi_gpio->cs_gpios = devm_kcalloc(dev, master->num_chipselect,
87 + sizeof(*spi_gpio->cs_gpios),
88 + GFP_KERNEL);
89 + if (!spi_gpio->cs_gpios)
90 + return -ENOMEM;
91 +
92 + for (i = 0; i < master->num_chipselect; i++) {
93 + spi_gpio->cs_gpios[i] =
94 + devm_gpiod_get_index(dev, "cs", i,
95 + gpio_defines_polarity ?
96 + GPIOD_OUT_LOW : GPIOD_OUT_HIGH);
97 + if (IS_ERR(spi_gpio->cs_gpios[i]))
98 + return PTR_ERR(spi_gpio->cs_gpios[i]);
99 + }
100 +
101 + return 0;
102 +}
103 +
104 #ifdef CONFIG_OF
105 static const struct of_device_id spi_gpio_dt_ids[] = {
106 { .compatible = "spi-gpio" },
107 @@ -345,10 +386,12 @@ MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids)
108 static int spi_gpio_probe_dt(struct platform_device *pdev,
109 struct spi_controller *host)
110 {
111 - host->dev.of_node = pdev->dev.of_node;
112 - host->use_gpio_descriptors = true;
113 + struct device *dev = &pdev->dev;
114
115 - return 0;
116 + host->dev.of_node = dev->of_node;
117 + host->num_chipselect = gpiod_count(dev, "cs");
118 +
119 + return spi_gpio_probe_get_cs_gpios(dev, host, true);
120 }
121 #else
122 static inline int spi_gpio_probe_dt(struct platform_device *pdev,
123 @@ -363,8 +406,6 @@ static int spi_gpio_probe_pdata(struct p
124 {
125 struct device *dev = &pdev->dev;
126 struct spi_gpio_platform_data *pdata = dev_get_platdata(dev);
127 - struct spi_gpio *spi_gpio = spi_controller_get_devdata(host);
128 - int i;
129
130 #ifdef GENERIC_BITBANG
131 if (!pdata || !pdata->num_chipselect)
132 @@ -376,20 +417,7 @@ static int spi_gpio_probe_pdata(struct p
133 */
134 host->num_chipselect = pdata->num_chipselect ?: 1;
135
136 - spi_gpio->cs_gpios = devm_kcalloc(dev, host->num_chipselect,
137 - sizeof(*spi_gpio->cs_gpios),
138 - GFP_KERNEL);
139 - if (!spi_gpio->cs_gpios)
140 - return -ENOMEM;
141 -
142 - for (i = 0; i < host->num_chipselect; i++) {
143 - spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i,
144 - GPIOD_OUT_HIGH);
145 - if (IS_ERR(spi_gpio->cs_gpios[i]))
146 - return PTR_ERR(spi_gpio->cs_gpios[i]);
147 - }
148 -
149 - return 0;
150 + return spi_gpio_probe_get_cs_gpios(dev, host, false);
151 }
152
153 static int spi_gpio_probe(struct platform_device *pdev)