hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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,39 @@
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
+ /*
248
+ * Only change MOSI to an input if using 3WIRE mode.
249
+ * Otherwise, MOSI could be left floating if there is
250
+ * no pull resistor connected to the I/O pin, or could
251
+ * be left logic high if there is a pull-up. Transmitting
252
+ * logic high when only clocking MISO data in can put some
253
+ * SPI devices in to a bad state.
254
+ */
255
+ if (spi->mode & SPI_3WIRE) {
256
+ ret = gpiod_direction_input(spi_gpio->mosi);
257
+ if (ret)
258
+ return ret;
259
+ }
260
+ /*
261
+ * Send a turnaround high impedance cycle when switching
262
+ * from output to input. Theoretically there should be
263
+ * a clock delay here, but as has been noted above, the
264
+ * nsec delay function for bit-banged GPIO is simply
265
+ * {} because bit-banging just doesn't get fast enough
266
+ * anyway.
267
+ */
268
+ if (spi->mode & SPI_3WIRE_HIZ) {
269
+ gpiod_set_value_cansleep(spi_gpio->sck,
270
+ !(spi->mode & SPI_CPOL));
271
+ gpiod_set_value_cansleep(spi_gpio->sck,
272
+ !!(spi->mode & SPI_CPOL));
273
+ }
274
+ return 0;
264275 }
265276
266277 static void spi_gpio_cleanup(struct spi_device *spi)
....@@ -278,41 +289,18 @@
278289 * floating signals. (A weak pulldown would save power too, but many
279290 * drivers expect to see all-ones data as the no slave "response".)
280291 */
281
-static int spi_gpio_request(struct device *dev,
282
- struct spi_gpio *spi_gpio,
283
- unsigned int num_chipselects,
284
- u16 *mflags)
292
+static int spi_gpio_request(struct device *dev, struct spi_gpio *spi_gpio)
285293 {
286
- int i;
287
-
288294 spi_gpio->mosi = devm_gpiod_get_optional(dev, "mosi", GPIOD_OUT_LOW);
289295 if (IS_ERR(spi_gpio->mosi))
290296 return PTR_ERR(spi_gpio->mosi);
291
- if (!spi_gpio->mosi)
292
- /* HW configuration without MOSI pin */
293
- *mflags |= SPI_MASTER_NO_TX;
294297
295298 spi_gpio->miso = devm_gpiod_get_optional(dev, "miso", GPIOD_IN);
296299 if (IS_ERR(spi_gpio->miso))
297300 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
- */
303301
304302 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;
303
+ return PTR_ERR_OR_ZERO(spi_gpio->sck);
316304 }
317305
318306 #ifdef CONFIG_OF
....@@ -322,139 +310,128 @@
322310 };
323311 MODULE_DEVICE_TABLE(of, spi_gpio_dt_ids);
324312
325
-static int spi_gpio_probe_dt(struct platform_device *pdev)
313
+static int spi_gpio_probe_dt(struct platform_device *pdev,
314
+ struct spi_master *master)
326315 {
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);
316
+ master->dev.of_node = pdev->dev.of_node;
317
+ master->use_gpio_descriptors = true;
333318
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;
319
+ return 0;
356320 }
357321 #else
358
-static inline int spi_gpio_probe_dt(struct platform_device *pdev)
322
+static inline int spi_gpio_probe_dt(struct platform_device *pdev,
323
+ struct spi_master *master)
359324 {
360325 return 0;
361326 }
362327 #endif
328
+
329
+static int spi_gpio_probe_pdata(struct platform_device *pdev,
330
+ struct spi_master *master)
331
+{
332
+ struct device *dev = &pdev->dev;
333
+ struct spi_gpio_platform_data *pdata = dev_get_platdata(dev);
334
+ struct spi_gpio *spi_gpio = spi_master_get_devdata(master);
335
+ int i;
336
+
337
+#ifdef GENERIC_BITBANG
338
+ if (!pdata || !pdata->num_chipselect)
339
+ return -ENODEV;
340
+#endif
341
+ /*
342
+ * The master needs to think there is a chipselect even if not
343
+ * connected
344
+ */
345
+ master->num_chipselect = pdata->num_chipselect ?: 1;
346
+
347
+ spi_gpio->cs_gpios = devm_kcalloc(dev, master->num_chipselect,
348
+ sizeof(*spi_gpio->cs_gpios),
349
+ GFP_KERNEL);
350
+ if (!spi_gpio->cs_gpios)
351
+ return -ENOMEM;
352
+
353
+ for (i = 0; i < master->num_chipselect; i++) {
354
+ spi_gpio->cs_gpios[i] = devm_gpiod_get_index(dev, "cs", i,
355
+ GPIOD_OUT_HIGH);
356
+ if (IS_ERR(spi_gpio->cs_gpios[i]))
357
+ return PTR_ERR(spi_gpio->cs_gpios[i]);
358
+ }
359
+
360
+ return 0;
361
+}
363362
364363 static int spi_gpio_probe(struct platform_device *pdev)
365364 {
366365 int status;
367366 struct spi_master *master;
368367 struct spi_gpio *spi_gpio;
369
- struct spi_gpio_platform_data *pdata;
370
- u16 master_flags = 0;
371
- bool use_of = 0;
368
+ struct device *dev = &pdev->dev;
369
+ struct spi_bitbang *bb;
372370
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));
371
+ master = devm_spi_alloc_master(dev, sizeof(*spi_gpio));
386372 if (!master)
387373 return -ENOMEM;
388374
375
+ if (pdev->dev.of_node)
376
+ status = spi_gpio_probe_dt(pdev, master);
377
+ else
378
+ status = spi_gpio_probe_pdata(pdev, master);
379
+
380
+ if (status)
381
+ return status;
382
+
389383 spi_gpio = spi_master_get_devdata(master);
390384
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);
385
+ status = spi_gpio_request(dev, spi_gpio);
409386 if (status)
410387 return status;
411388
412389 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;
390
+ master->mode_bits = SPI_3WIRE | SPI_3WIRE_HIZ | SPI_CPHA | SPI_CPOL |
391
+ SPI_CS_HIGH;
392
+ if (!spi_gpio->mosi) {
393
+ /* HW configuration without MOSI pin
394
+ *
395
+ * No setting SPI_MASTER_NO_RX here - if there is only
396
+ * a MOSI pin connected the host can still do RX by
397
+ * changing the direction of the line.
398
+ */
399
+ master->flags = SPI_MASTER_NO_TX;
400
+ }
401
+
415402 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;
418403 master->setup = spi_gpio_setup;
419404 master->cleanup = spi_gpio_cleanup;
420
-#ifdef CONFIG_OF
421
- master->dev.of_node = pdev->dev.of_node;
422
-#endif
423405
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;
406
+ bb = &spi_gpio->bitbang;
407
+ bb->master = master;
408
+ /*
409
+ * There is some additional business, apart from driving the CS GPIO
410
+ * line, that we need to do on selection. This makes the local
411
+ * callback for chipselect always get called.
412
+ */
413
+ master->flags |= SPI_MASTER_GPIO_SS;
414
+ bb->chipselect = spi_gpio_chipselect;
415
+ bb->set_line_direction = spi_gpio_set_direction;
427416
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;
417
+ if (master->flags & SPI_MASTER_NO_TX) {
418
+ bb->txrx_word[SPI_MODE_0] = spi_gpio_spec_txrx_word_mode0;
419
+ bb->txrx_word[SPI_MODE_1] = spi_gpio_spec_txrx_word_mode1;
420
+ bb->txrx_word[SPI_MODE_2] = spi_gpio_spec_txrx_word_mode2;
421
+ bb->txrx_word[SPI_MODE_3] = spi_gpio_spec_txrx_word_mode3;
433422 } 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;
423
+ bb->txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0;
424
+ bb->txrx_word[SPI_MODE_1] = spi_gpio_txrx_word_mode1;
425
+ bb->txrx_word[SPI_MODE_2] = spi_gpio_txrx_word_mode2;
426
+ bb->txrx_word[SPI_MODE_3] = spi_gpio_txrx_word_mode3;
438427 }
439
- spi_gpio->bitbang.setup_transfer = spi_bitbang_setup_transfer;
428
+ bb->setup_transfer = spi_bitbang_setup_transfer;
440429
441
- return spi_bitbang_start(&spi_gpio->bitbang);
442
-}
430
+ status = spi_bitbang_init(&spi_gpio->bitbang);
431
+ if (status)
432
+ return status;
443433
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;
434
+ return devm_spi_register_master(&pdev->dev, master);
458435 }
459436
460437 MODULE_ALIAS("platform:" DRIVER_NAME);
....@@ -465,7 +442,6 @@
465442 .of_match_table = of_match_ptr(spi_gpio_dt_ids),
466443 },
467444 .probe = spi_gpio_probe,
468
- .remove = spi_gpio_remove,
469445 };
470446 module_platform_driver(spi_gpio_driver);
471447