hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/spi/spi-gpio.c
....@@ -1,18 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * SPI master driver using generic bitbanged GPIO
34 *
45 * Copyright (C) 2006,2008 David Brownell
56 * Copyright (C) 2017 Linus Walleij
6
- *
7
- * This program is free software; you can redistribute it and/or modify
8
- * it under the terms of the GNU General Public License as published by
9
- * the Free Software Foundation; either version 2 of the License, or
10
- * (at your option) any later version.
11
- *
12
- * This program is distributed in the hope that it will be useful,
13
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- * GNU General Public License for more details.
167 */
178 #include <linux/kernel.h>
189 #include <linux/module.h>
....@@ -35,20 +26,16 @@
3526 * platform_device->driver_data ... points to spi_gpio
3627 *
3728 * spi->controller_state ... reserved for bitbang framework code
38
- * spi->controller_data ... holds chipselect GPIO
3929 *
4030 * spi->master->dev.driver_data ... points to spi_gpio->bitbang
4131 */
4232
4333 struct spi_gpio {
4434 struct spi_bitbang bitbang;
45
- struct spi_gpio_platform_data pdata;
46
- struct platform_device *pdev;
4735 struct gpio_desc *sck;
4836 struct gpio_desc *miso;
4937 struct gpio_desc *mosi;
5038 struct gpio_desc **cs_gpios;
51
- bool has_cs;
5239 };
5340
5441 /*----------------------------------------------------------------------*/
....@@ -94,12 +81,6 @@
9481 bang = spi_master_get_devdata(spi->master);
9582 spi_gpio = container_of(bang, struct spi_gpio, bitbang);
9683 return spi_gpio;
97
-}
98
-
99
-static inline struct spi_gpio_platform_data *__pure
100
-spi_to_pdata(const struct spi_device *spi)
101
-{
102
- return &spi_to_spi_gpio(spi)->pdata;
10384 }
10485
10586 /* These helpers are in turn called by the bitbang inlines */
....@@ -224,7 +205,7 @@
224205 gpiod_set_value_cansleep(spi_gpio->sck, spi->mode & SPI_CPOL);
225206
226207 /* Drive chip select line, if we have one */
227
- if (spi_gpio->has_cs) {
208
+ if (spi_gpio->cs_gpios) {
228209 struct gpio_desc *cs = spi_gpio->cs_gpios[spi->chip_select];
229210
230211 /* SPI chip selects are normally active-low */
....@@ -242,10 +223,12 @@
242223 * The CS GPIOs have already been
243224 * initialized from the descriptor lookup.
244225 */
245
- cs = spi_gpio->cs_gpios[spi->chip_select];
246
- if (!spi->controller_state && cs)
247
- status = gpiod_direction_output(cs,
248
- !(spi->mode & SPI_CS_HIGH));
226
+ if (spi_gpio->cs_gpios) {
227
+ cs = spi_gpio->cs_gpios[spi->chip_select];
228
+ if (!spi->controller_state && cs)
229
+ status = gpiod_direction_output(cs,
230
+ !(spi->mode & SPI_CS_HIGH));
231
+ }
249232
250233 if (!status)
251234 status = spi_bitbang_setup(spi);
....@@ -256,11 +239,29 @@
256239 static int spi_gpio_set_direction(struct spi_device *spi, bool output)
257240 {
258241 struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi);
242
+ int ret;
259243
260244 if (output)
261245 return gpiod_direction_output(spi_gpio->mosi, 1);
262
- else
263
- return gpiod_direction_input(spi_gpio->mosi);
246
+
247
+ ret = gpiod_direction_input(spi_gpio->mosi);
248
+ if (ret)
249
+ return ret;
250
+ /*
251
+ * Send a turnaround high impedance cycle when switching
252
+ * from output to input. Theoretically there should be
253
+ * a clock delay here, but as has been noted above, the
254
+ * nsec delay function for bit-banged GPIO is simply
255
+ * {} because bit-banging just doesn't get fast enough
256
+ * anyway.
257
+ */
258
+ if (spi->mode & SPI_3WIRE_HIZ) {
259
+ gpiod_set_value_cansleep(spi_gpio->sck,
260
+ !(spi->mode & SPI_CPOL));
261
+ gpiod_set_value_cansleep(spi_gpio->sck,
262
+ !!(spi->mode & SPI_CPOL));
263
+ }
264
+ return 0;
264265 }
265266
266267 static void spi_gpio_cleanup(struct spi_device *spi)
....@@ -278,41 +279,18 @@
278279 * floating signals. (A weak pulldown would save power too, but many
279280 * drivers expect to see all-ones data as the no slave "response".)
280281 */
281
-static int spi_gpio_request(struct device *dev,
282
- struct spi_gpio *spi_gpio,
283
- unsigned int num_chipselects,
284
- u16 *mflags)
282
+static int spi_gpio_request(struct device *dev, struct spi_gpio *spi_gpio)
285283 {
286
- int i;
287
-
288284 spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW);
289285 if (IS_ERR(spi_gpio->mosi))
290286 return PTR_ERR(spi_gpio->mosi);
291
- if (!spi_gpio->mosi)
292
- /* HW configuration without MOSI pin */
293
- *mflags |= SPI_MASTER_NO_TX;
294287
295288 spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN);
296289 if (IS_ERR(spi_gpio->miso))
297290 return PTR_ERR(spi_gpio->miso);
298
- /*
299
- * No setting SPI_MASTER_NO_RX here - if there is only a MOSI
300
- * pin connected the host can still do RX by changing the
301
- * direction of the line.
302
- */
303291
304292 spi_gpio->sck = devm_gpiod_get(dev, "sck", GPIOD_OUT_LOW);
305
- if (IS_ERR(spi_gpio->sck))
306
- return PTR_ERR(spi_gpio->sck);
307
-
308
- for (i = 0; i < num_chipselects; i++) {
309
- spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs",
310
- i, GPIOD_OUT_HIGH);
311
- if (IS_ERR(spi_gpio->cs_gpios[i]))
312
- return PTR_ERR(spi_gpio->cs_gpios[i]);
313
- }
314
-
315
- return 0;
293
+ return PTR_ERR_OR_ZERO(spi_gpio->sck);
316294 }
317295
318296 #ifdef CONFIG_OF
....@@ -322,139 +300,128 @@
322300 };
323301 MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);
324302
325
-static int spi_gpio_probe_dt(struct platform_device *pdev)
303
+static int spi_gpio_probe_dt(struct platform_device *pdev,
304
+ struct spi_master *master)
326305 {
327
- int ret;
328
- u32 tmp;
329
- struct spi_gpio_platform_data *pdata;
330
- struct device_node *np = pdev->dev.of_node;
331
- const struct of_device_id *of_id =
332
- of_match_device(spi_gpio_dt_ids, &pdev->dev);
306
+ master->dev.of_node = pdev->dev.of_node;
307
+ master->use_gpio_descriptors = true;
333308
334
- if (!of_id)
335
- return 0;
336
-
337
- pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
338
- if (!pdata)
339
- return -ENOMEM;
340
-
341
-
342
- ret = of_property_read_u32(np, "num-chipselects", &tmp);
343
- if (ret < 0) {
344
- dev_err(&pdev->dev, "num-chipselects property not found\n");
345
- goto error_free;
346
- }
347
-
348
- pdata->num_chipselect = tmp;
349
- pdev->dev.platform_data = pdata;
350
-
351
- return 1;
352
-
353
-error_free:
354
- devm_kfree(&pdev->dev, pdata);
355
- return ret;
309
+ return 0;
356310 }
357311 #else
358
-static inline int spi_gpio_probe_dt(struct platform_device *pdev)
312
+static inline int spi_gpio_probe_dt(struct platform_device *pdev,
313
+ struct spi_master *master)
359314 {
360315 return 0;
361316 }
362317 #endif
318
+
319
+static int spi_gpio_probe_pdata(struct platform_device *pdev,
320
+ struct spi_master *master)
321
+{
322
+ struct device *dev = &pdev->dev;
323
+ struct spi_gpio_platform_data *pdata = dev_get_platdata(dev);
324
+ struct spi_gpio *spi_gpio = spi_master_get_devdata(master);
325
+ int i;
326
+
327
+#ifdef GENERIC_BITBANG
328
+ if (!pdata || !pdata->num_chipselect)
329
+ return -ENODEV;
330
+#endif
331
+ /*
332
+ * The master needs to think there is a chipselect even if not
333
+ * connected
334
+ */
335
+ master->num_chipselect = pdata->num_chipselect ?: 1;
336
+
337
+ spi_gpio->cs_gpios = devm_kcalloc(dev, master->num_chipselect,
338
+ sizeof(*spi_gpio->cs_gpios),
339
+ GFP_KERNEL);
340
+ if (!spi_gpio->cs_gpios)
341
+ return -ENOMEM;
342
+
343
+ for (i = 0; i < master->num_chipselect; i++) {
344
+ spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i,
345
+ GPIOD_OUT_HIGH);
346
+ if (IS_ERR(spi_gpio->cs_gpios[i]))
347
+ return PTR_ERR(spi_gpio->cs_gpios[i]);
348
+ }
349
+
350
+ return 0;
351
+}
363352
364353 static int spi_gpio_probe(struct platform_device *pdev)
365354 {
366355 int status;
367356 struct spi_master *master;
368357 struct spi_gpio *spi_gpio;
369
- struct spi_gpio_platform_data *pdata;
370
- u16 master_flags = 0;
371
- bool use_of = 0;
358
+ struct device *dev = &pdev->dev;
359
+ struct spi_bitbang *bb;
372360
373
- status = spi_gpio_probe_dt(pdev);
374
- if (status < 0)
375
- return status;
376
- if (status > 0)
377
- use_of = 1;
378
-
379
- pdata = dev_get_platdata(&pdev->dev);
380
-#ifdef GENERIC_BITBANG
381
- if (!pdata || (!use_of && !pdata->num_chipselect))
382
- return -ENODEV;
383
-#endif
384
-
385
- master = devm_spi_alloc_master(&pdev->dev, sizeof(*spi_gpio));
361
+ master = devm_spi_alloc_master(dev, sizeof(*spi_gpio));
386362 if (!master)
387363 return -ENOMEM;
388364
365
+ if (pdev->dev.of_node)
366
+ status = spi_gpio_probe_dt(pdev, master);
367
+ else
368
+ status = spi_gpio_probe_pdata(pdev, master);
369
+
370
+ if (status)
371
+ return status;
372
+
389373 spi_gpio = spi_master_get_devdata(master);
390374
391
- spi_gpio->cs_gpios = devm_kcalloc(&pdev->dev,
392
- pdata->num_chipselect,
393
- sizeof(*spi_gpio->cs_gpios),
394
- GFP_KERNEL);
395
- if (!spi_gpio->cs_gpios)
396
- return -ENOMEM;
397
-
398
- platform_set_drvdata(pdev, spi_gpio);
399
-
400
- /* Determine if we have chip selects connected */
401
- spi_gpio->has_cs = !!pdata->num_chipselect;
402
-
403
- spi_gpio->pdev = pdev;
404
- if (pdata)
405
- spi_gpio->pdata = *pdata;
406
-
407
- status = spi_gpio_request(&pdev->dev, spi_gpio,
408
- pdata->num_chipselect, &master_flags);
375
+ status = spi_gpio_request(dev, spi_gpio);
409376 if (status)
410377 return status;
411378
412379 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
413
- master->mode_bits = SPI_3WIRE | SPI_CPHA | SPI_CPOL | SPI_CS_HIGH;
414
- master->flags = master_flags;
380
+ master->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL |
381
+ SPI_CS_HIGH;
382
+ if (!spi_gpio->mosi) {
383
+ /* HW configuration without MOSI pin
384
+ *
385
+ * No setting SPI_MASTER_NO_RX here - if there is only
386
+ * a MOSI pin connected the host can still do RX by
387
+ * changing the direction of the line.
388
+ */
389
+ master->flags = SPI_MASTER_NO_TX;
390
+ }
391
+
415392 master->bus_num = pdev->id;
416
- /* The master needs to think there is a chipselect even if not connected */
417
- master->num_chipselect = spi_gpio->has_cs ? pdata->num_chipselect : 1;
418393 master->setup = spi_gpio_setup;
419394 master->cleanup = spi_gpio_cleanup;
420
-#ifdef CONFIG_OF
421
- master->dev.of_node = pdev->dev.of_node;
422
-#endif
423395
424
- spi_gpio->bitbang.master = master;
425
- spi_gpio->bitbang.chipselect = spi_gpio_chipselect;
426
- spi_gpio->bitbang.set_line_direction = spi_gpio_set_direction;
396
+ bb = &spi_gpio->bitbang;
397
+ bb->master = master;
398
+ /*
399
+ * There is some additional business, apart from driving the CS GPIO
400
+ * line, that we need to do on selection. This makes the local
401
+ * callback for chipselect always get called.
402
+ */
403
+ master->flags |= SPI_MASTER_GPIO_SS;
404
+ bb->chipselect = spi_gpio_chipselect;
405
+ bb->set_line_direction = spi_gpio_set_direction;
427406
428
- if ((master_flags & SPI_MASTER_NO_TX) == 0) {
429
- spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
430
- spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
431
- spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
432
- spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
407
+ if (master->flags & SPI_MASTER_NO_TX) {
408
+ bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
409
+ bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
410
+ bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
411
+ bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
433412 } else {
434
- spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
435
- spi_gpio->bitbang.txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
436
- spi_gpio->bitbang.txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
437
- spi_gpio->bitbang.txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
413
+ bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
414
+ bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
415
+ bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
416
+ bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
438417 }
439
- spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
418
+ bb->setup_transfer = spi_bitbang_setup_transfer;
440419
441
- return spi_bitbang_start(&spi_gpio->bitbang);
442
-}
420
+ status = spi_bitbang_init(&spi_gpio->bitbang);
421
+ if (status)
422
+ return status;
443423
444
-static int spi_gpio_remove(struct platform_device *pdev)
445
-{
446
- struct spi_gpio *spi_gpio;
447
- struct spi_gpio_platform_data *pdata;
448
-
449
- spi_gpio = platform_get_drvdata(pdev);
450
- pdata = dev_get_platdata(&pdev->dev);
451
-
452
- /* stop() unregisters child devices too */
453
- spi_bitbang_stop(&spi_gpio->bitbang);
454
-
455
- spi_master_put(spi_gpio->bitbang.master);
456
-
457
- return 0;
424
+ return devm_spi_register_master(&pdev->dev, master);
458425 }
459426
460427 MODULE_ALIAS("platform:" DRIVER_NAME);
....@@ -465,7 +432,6 @@
465432 .of_match_table = of_match_ptr(spi_gpio_dt_ids),
466433 },
467434 .probe = spi_gpio_probe,
468
- .remove = spi_gpio_remove,
469435 };
470436 module_platform_driver(spi_gpio_driver);
471437