hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/spi/spi-bitbang.c
....@@ -1,15 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * polling/bitbanging SPI master controller driver utilities
3
- *
4
- * This program is free software; you can redistribute it and/or modify
5
- * it under the terms of the GNU General Public License as published by
6
- * the Free Software Foundation; either version 2 of the License, or
7
- * (at your option) any later version.
8
- *
9
- * This program is distributed in the hope that it will be useful,
10
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
- * GNU General Public License for more details.
134 */
145
156 #include <linux/spinlock.h>
....@@ -183,13 +174,15 @@
183174 }
184175 EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
185176
186
-/**
177
+/*
187178 * spi_bitbang_setup - default setup for per-word I/O loops
188179 */
189180 int spi_bitbang_setup(struct spi_device *spi)
190181 {
191182 struct spi_bitbang_cs *cs = spi->controller_state;
192183 struct spi_bitbang *bitbang;
184
+ bool initial_setup = false;
185
+ int retval;
193186
194187 bitbang = spi_master_get_devdata(spi->master);
195188
....@@ -198,39 +191,34 @@
198191 if (!cs)
199192 return -ENOMEM;
200193 spi->controller_state = cs;
194
+ initial_setup = true;
201195 }
202196
203197 /* per-word shift register access, in hardware or bitbanging */
204198 cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
205
- if (!cs->txrx_word)
206
- return -EINVAL;
199
+ if (!cs->txrx_word) {
200
+ retval = -EINVAL;
201
+ goto err_free;
202
+ }
207203
208204 if (bitbang->setup_transfer) {
209
- int retval = bitbang->setup_transfer(spi, NULL);
205
+ retval = bitbang->setup_transfer(spi, NULL);
210206 if (retval < 0)
211
- return retval;
207
+ goto err_free;
212208 }
213209
214210 dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
215211
216
- /* NOTE we _need_ to call chipselect() early, ideally with adapter
217
- * setup, unless the hardware defaults cooperate to avoid confusion
218
- * between normal (active low) and inverted chipselects.
219
- */
220
-
221
- /* deselect chip (low or high) */
222
- mutex_lock(&bitbang->lock);
223
- if (!bitbang->busy) {
224
- bitbang->chipselect(spi, BITBANG_CS_INACTIVE);
225
- ndelay(cs->nsecs);
226
- }
227
- mutex_unlock(&bitbang->lock);
228
-
229212 return 0;
213
+
214
+err_free:
215
+ if (initial_setup)
216
+ kfree(cs);
217
+ return retval;
230218 }
231219 EXPORT_SYMBOL_GPL(spi_bitbang_setup);
232220
233
-/**
221
+/*
234222 * spi_bitbang_cleanup - default cleanup for per-word I/O loops
235223 */
236224 void spi_bitbang_cleanup(struct spi_device *spi)
....@@ -348,6 +336,59 @@
348336
349337 /*----------------------------------------------------------------------*/
350338
339
+int spi_bitbang_init(struct spi_bitbang *bitbang)
340
+{
341
+ struct spi_master *master = bitbang->master;
342
+ bool custom_cs;
343
+
344
+ if (!master)
345
+ return -EINVAL;
346
+ /*
347
+ * We only need the chipselect callback if we are actually using it.
348
+ * If we just use GPIO descriptors, it is surplus. If the
349
+ * SPI_MASTER_GPIO_SS flag is set, we always need to call the
350
+ * driver-specific chipselect routine.
351
+ */
352
+ custom_cs = (!master->use_gpio_descriptors ||
353
+ (master->flags & SPI_MASTER_GPIO_SS));
354
+
355
+ if (custom_cs && !bitbang->chipselect)
356
+ return -EINVAL;
357
+
358
+ mutex_init(&bitbang->lock);
359
+
360
+ if (!master->mode_bits)
361
+ master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
362
+
363
+ if (master->transfer || master->transfer_one_message)
364
+ return -EINVAL;
365
+
366
+ master->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
367
+ master->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
368
+ master->transfer_one = spi_bitbang_transfer_one;
369
+ /*
370
+ * When using GPIO descriptors, the ->set_cs() callback doesn't even
371
+ * get called unless SPI_MASTER_GPIO_SS is set.
372
+ */
373
+ if (custom_cs)
374
+ master->set_cs = spi_bitbang_set_cs;
375
+
376
+ if (!bitbang->txrx_bufs) {
377
+ bitbang->use_dma = 0;
378
+ bitbang->txrx_bufs = spi_bitbang_bufs;
379
+ if (!master->setup) {
380
+ if (!bitbang->setup_transfer)
381
+ bitbang->setup_transfer =
382
+ spi_bitbang_setup_transfer;
383
+ master->setup = spi_bitbang_setup;
384
+ master->cleanup = spi_bitbang_cleanup;
385
+ }
386
+ }
387
+
388
+ return 0;
389
+}
390
+EXPORT_SYMBOL_GPL(spi_bitbang_init);
391
+
351392 /**
352393 * spi_bitbang_start - start up a polled/bitbanging SPI master driver
353394 * @bitbang: driver handle
....@@ -381,33 +422,9 @@
381422 struct spi_master *master = bitbang->master;
382423 int ret;
383424
384
- if (!master || !bitbang->chipselect)
385
- return -EINVAL;
386
-
387
- mutex_init(&bitbang->lock);
388
-
389
- if (!master->mode_bits)
390
- master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
391
-
392
- if (master->transfer || master->transfer_one_message)
393
- return -EINVAL;
394
-
395
- master->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
396
- master->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
397
- master->transfer_one = spi_bitbang_transfer_one;
398
- master->set_cs = spi_bitbang_set_cs;
399
-
400
- if (!bitbang->txrx_bufs) {
401
- bitbang->use_dma = 0;
402
- bitbang->txrx_bufs = spi_bitbang_bufs;
403
- if (!master->setup) {
404
- if (!bitbang->setup_transfer)
405
- bitbang->setup_transfer =
406
- spi_bitbang_setup_transfer;
407
- master->setup = spi_bitbang_setup;
408
- master->cleanup = spi_bitbang_cleanup;
409
- }
410
- }
425
+ ret = spi_bitbang_init(bitbang);
426
+ if (ret)
427
+ return ret;
411428
412429 /* driver may get busy before register() returns, especially
413430 * if someone registered boardinfo for devices
....@@ -420,7 +437,7 @@
420437 }
421438 EXPORT_SYMBOL_GPL(spi_bitbang_start);
422439
423
-/**
440
+/*
424441 * spi_bitbang_stop - stops the task providing spi communication
425442 */
426443 void spi_bitbang_stop(struct spi_bitbang *bitbang)