blob: 1a7352abd87860956d6f989eb638ad2c42a0003b [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Brownell9904f222006-01-08 13:34:26 -08002/*
Grant Likelyca632f52011-06-06 01:16:30 -06003 * polling/bitbanging SPI master controller driver utilities
David Brownell9904f222006-01-08 13:34:26 -08004 */
5
David Brownell9904f222006-01-08 13:34:26 -08006#include <linux/spinlock.h>
7#include <linux/workqueue.h>
8#include <linux/interrupt.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -04009#include <linux/module.h>
David Brownell9904f222006-01-08 13:34:26 -080010#include <linux/delay.h>
11#include <linux/errno.h>
12#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
David Brownell9904f222006-01-08 13:34:26 -080014
15#include <linux/spi/spi.h>
16#include <linux/spi/spi_bitbang.h>
17
Heiner Kallweit00376862015-09-29 23:15:53 +020018#define SPI_BITBANG_CS_DELAY 100
19
David Brownell9904f222006-01-08 13:34:26 -080020
21/*----------------------------------------------------------------------*/
22
23/*
24 * FIRST PART (OPTIONAL): word-at-a-time spi_transfer support.
25 * Use this for GPIO or shift-register level hardware APIs.
26 *
27 * spi_bitbang_cs is in spi_device->controller_state, which is unavailable
28 * to glue code. These bitbang setup() and cleanup() routines are always
29 * used, though maybe they're called from controller-aware code.
30 *
Uwe Kleine-König03ddcbc2013-08-07 21:22:20 +020031 * chipselect() and friends may use spi_device->controller_data and
David Brownell9904f222006-01-08 13:34:26 -080032 * controller registers as appropriate.
33 *
34 *
35 * NOTE: SPI controller pins can often be used as GPIO pins instead,
36 * which means you could use a bitbang driver either to get hardware
37 * working quickly, or testing for differences that aren't speed related.
38 */
39
40struct spi_bitbang_cs {
41 unsigned nsecs; /* (clock cycle time)/2 */
42 u32 (*txrx_word)(struct spi_device *spi, unsigned nsecs,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +020043 u32 word, u8 bits, unsigned flags);
David Brownell9904f222006-01-08 13:34:26 -080044 unsigned (*txrx_bufs)(struct spi_device *,
45 u32 (*txrx_word)(
46 struct spi_device *spi,
47 unsigned nsecs,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +020048 u32 word, u8 bits,
49 unsigned flags),
50 unsigned, struct spi_transfer *,
51 unsigned);
David Brownell9904f222006-01-08 13:34:26 -080052};
53
54static unsigned bitbang_txrx_8(
55 struct spi_device *spi,
56 u32 (*txrx_word)(struct spi_device *spi,
57 unsigned nsecs,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +020058 u32 word, u8 bits,
59 unsigned flags),
David Brownell9904f222006-01-08 13:34:26 -080060 unsigned ns,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +020061 struct spi_transfer *t,
62 unsigned flags
David Brownell9904f222006-01-08 13:34:26 -080063) {
Laxman Dewangan766ed702012-12-18 14:25:43 +053064 unsigned bits = t->bits_per_word;
David Brownell9904f222006-01-08 13:34:26 -080065 unsigned count = t->len;
66 const u8 *tx = t->tx_buf;
67 u8 *rx = t->rx_buf;
68
69 while (likely(count > 0)) {
70 u8 word = 0;
71
72 if (tx)
73 word = *tx++;
Lorenzo Bianconi304d3432018-07-28 10:19:13 +020074 word = txrx_word(spi, ns, word, bits, flags);
David Brownell9904f222006-01-08 13:34:26 -080075 if (rx)
76 *rx++ = word;
77 count -= 1;
78 }
79 return t->len - count;
80}
81
82static unsigned bitbang_txrx_16(
83 struct spi_device *spi,
84 u32 (*txrx_word)(struct spi_device *spi,
85 unsigned nsecs,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +020086 u32 word, u8 bits,
87 unsigned flags),
David Brownell9904f222006-01-08 13:34:26 -080088 unsigned ns,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +020089 struct spi_transfer *t,
90 unsigned flags
David Brownell9904f222006-01-08 13:34:26 -080091) {
Laxman Dewangan766ed702012-12-18 14:25:43 +053092 unsigned bits = t->bits_per_word;
David Brownell9904f222006-01-08 13:34:26 -080093 unsigned count = t->len;
94 const u16 *tx = t->tx_buf;
95 u16 *rx = t->rx_buf;
96
97 while (likely(count > 1)) {
98 u16 word = 0;
99
100 if (tx)
101 word = *tx++;
Lorenzo Bianconi304d3432018-07-28 10:19:13 +0200102 word = txrx_word(spi, ns, word, bits, flags);
David Brownell9904f222006-01-08 13:34:26 -0800103 if (rx)
104 *rx++ = word;
105 count -= 2;
106 }
107 return t->len - count;
108}
109
110static unsigned bitbang_txrx_32(
111 struct spi_device *spi,
112 u32 (*txrx_word)(struct spi_device *spi,
113 unsigned nsecs,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +0200114 u32 word, u8 bits,
115 unsigned flags),
David Brownell9904f222006-01-08 13:34:26 -0800116 unsigned ns,
Lorenzo Bianconi304d3432018-07-28 10:19:13 +0200117 struct spi_transfer *t,
118 unsigned flags
David Brownell9904f222006-01-08 13:34:26 -0800119) {
Laxman Dewangan766ed702012-12-18 14:25:43 +0530120 unsigned bits = t->bits_per_word;
David Brownell9904f222006-01-08 13:34:26 -0800121 unsigned count = t->len;
122 const u32 *tx = t->tx_buf;
123 u32 *rx = t->rx_buf;
124
125 while (likely(count > 3)) {
126 u32 word = 0;
127
128 if (tx)
129 word = *tx++;
Lorenzo Bianconi304d3432018-07-28 10:19:13 +0200130 word = txrx_word(spi, ns, word, bits, flags);
David Brownell9904f222006-01-08 13:34:26 -0800131 if (rx)
132 *rx++ = word;
133 count -= 4;
134 }
135 return t->len - count;
136}
137
Kumar Galaff9f4772006-04-02 16:06:35 -0500138int spi_bitbang_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
Imre Deak4cff33f2006-02-17 10:02:18 -0800139{
140 struct spi_bitbang_cs *cs = spi->controller_state;
141 u8 bits_per_word;
142 u32 hz;
143
144 if (t) {
145 bits_per_word = t->bits_per_word;
146 hz = t->speed_hz;
147 } else {
148 bits_per_word = 0;
149 hz = 0;
150 }
151
152 /* spi_transfer level calls that work per-word */
153 if (!bits_per_word)
154 bits_per_word = spi->bits_per_word;
155 if (bits_per_word <= 8)
156 cs->txrx_bufs = bitbang_txrx_8;
157 else if (bits_per_word <= 16)
158 cs->txrx_bufs = bitbang_txrx_16;
159 else if (bits_per_word <= 32)
160 cs->txrx_bufs = bitbang_txrx_32;
161 else
162 return -EINVAL;
163
164 /* nsecs = (clock period)/2 */
165 if (!hz)
166 hz = spi->max_speed_hz;
David Brownell1e316d72006-04-06 22:25:56 -0700167 if (hz) {
168 cs->nsecs = (1000000000/2) / hz;
169 if (cs->nsecs > (MAX_UDELAY_MS * 1000 * 1000))
170 return -EINVAL;
171 }
Imre Deak4cff33f2006-02-17 10:02:18 -0800172
173 return 0;
174}
Kumar Galaff9f4772006-04-02 16:06:35 -0500175EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
Imre Deak4cff33f2006-02-17 10:02:18 -0800176
Lee Jonesc13b5042020-07-17 14:54:12 +0100177/*
David Brownell9904f222006-01-08 13:34:26 -0800178 * spi_bitbang_setup - default setup for per-word I/O loops
179 */
180int spi_bitbang_setup(struct spi_device *spi)
181{
182 struct spi_bitbang_cs *cs = spi->controller_state;
183 struct spi_bitbang *bitbang;
184
David Brownellccf77cc2006-04-03 15:46:22 -0700185 bitbang = spi_master_get_devdata(spi->master);
186
David Brownell9904f222006-01-08 13:34:26 -0800187 if (!cs) {
Jingoo Hancff93c52013-10-14 10:33:38 +0900188 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
David Brownell9904f222006-01-08 13:34:26 -0800189 if (!cs)
190 return -ENOMEM;
191 spi->controller_state = cs;
192 }
David Brownell9904f222006-01-08 13:34:26 -0800193
David Brownell9904f222006-01-08 13:34:26 -0800194 /* per-word shift register access, in hardware or bitbanging */
195 cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
196 if (!cs->txrx_word)
197 return -EINVAL;
198
Pelle Nilsson7d0ec8b2015-04-14 15:40:17 +0200199 if (bitbang->setup_transfer) {
200 int retval = bitbang->setup_transfer(spi, NULL);
201 if (retval < 0)
202 return retval;
203 }
David Brownell9904f222006-01-08 13:34:26 -0800204
David Brownell7d077192009-06-17 16:26:03 -0700205 dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
David Brownell9904f222006-01-08 13:34:26 -0800206
David Brownell9904f222006-01-08 13:34:26 -0800207 return 0;
208}
209EXPORT_SYMBOL_GPL(spi_bitbang_setup);
210
Lee Jonesc13b5042020-07-17 14:54:12 +0100211/*
David Brownell9904f222006-01-08 13:34:26 -0800212 * spi_bitbang_cleanup - default cleanup for per-word I/O loops
213 */
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -0800214void spi_bitbang_cleanup(struct spi_device *spi)
David Brownell9904f222006-01-08 13:34:26 -0800215{
216 kfree(spi->controller_state);
217}
218EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
219
220static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
221{
222 struct spi_bitbang_cs *cs = spi->controller_state;
223 unsigned nsecs = cs->nsecs;
Lorenzo Bianconi4b859db2018-07-28 10:19:14 +0200224 struct spi_bitbang *bitbang;
David Brownell9904f222006-01-08 13:34:26 -0800225
Lorenzo Bianconi4b859db2018-07-28 10:19:14 +0200226 bitbang = spi_master_get_devdata(spi->master);
227 if (bitbang->set_line_direction) {
228 int err;
229
230 err = bitbang->set_line_direction(spi, !!(t->tx_buf));
231 if (err < 0)
232 return err;
233 }
234
235 if (spi->mode & SPI_3WIRE) {
236 unsigned flags;
237
238 flags = t->tx_buf ? SPI_MASTER_NO_RX : SPI_MASTER_NO_TX;
239 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, flags);
240 }
Lorenzo Bianconi304d3432018-07-28 10:19:13 +0200241 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, 0);
David Brownell9904f222006-01-08 13:34:26 -0800242}
243
244/*----------------------------------------------------------------------*/
245
246/*
247 * SECOND PART ... simple transfer queue runner.
248 *
249 * This costs a task context per controller, running the queue by
250 * performing each transfer in sequence. Smarter hardware can queue
251 * several DMA transfers at once, and process several controller queues
252 * in parallel; this driver doesn't match such hardware very well.
253 *
254 * Drivers can provide word-at-a-time i/o primitives, or provide
255 * transfer-at-a-time ones to leverage dma or fifo hardware.
256 */
Mark Brown20251722013-07-05 20:07:27 +0100257
258static int spi_bitbang_prepare_hardware(struct spi_master *spi)
259{
Jingoo Hancff93c52013-10-14 10:33:38 +0900260 struct spi_bitbang *bitbang;
Mark Brown20251722013-07-05 20:07:27 +0100261
262 bitbang = spi_master_get_devdata(spi);
263
Nicolas Boichatc15f6ed2015-08-17 11:52:54 +0800264 mutex_lock(&bitbang->lock);
Mark Brown20251722013-07-05 20:07:27 +0100265 bitbang->busy = 1;
Nicolas Boichatc15f6ed2015-08-17 11:52:54 +0800266 mutex_unlock(&bitbang->lock);
Mark Brown20251722013-07-05 20:07:27 +0100267
268 return 0;
269}
270
Fabio Estevamd60990d52013-07-17 15:49:54 -0300271static int spi_bitbang_transfer_one(struct spi_master *master,
Heiner Kallweit00376862015-09-29 23:15:53 +0200272 struct spi_device *spi,
273 struct spi_transfer *transfer)
Mark Brown91b30852013-07-05 12:06:44 +0100274{
Heiner Kallweit00376862015-09-29 23:15:53 +0200275 struct spi_bitbang *bitbang = spi_master_get_devdata(master);
276 int status = 0;
Mark Brown91b30852013-07-05 12:06:44 +0100277
Heiner Kallweit00376862015-09-29 23:15:53 +0200278 if (bitbang->setup_transfer) {
279 status = bitbang->setup_transfer(spi, transfer);
280 if (status < 0)
281 goto out;
282 }
Mark Brown91b30852013-07-05 12:06:44 +0100283
Heiner Kallweit00376862015-09-29 23:15:53 +0200284 if (transfer->len)
285 status = bitbang->txrx_bufs(spi, transfer);
Mark Brown91b30852013-07-05 12:06:44 +0100286
Heiner Kallweit00376862015-09-29 23:15:53 +0200287 if (status == transfer->len)
Mark Brown91b30852013-07-05 12:06:44 +0100288 status = 0;
Heiner Kallweit00376862015-09-29 23:15:53 +0200289 else if (status >= 0)
290 status = -EREMOTEIO;
Mark Brown91b30852013-07-05 12:06:44 +0100291
Heiner Kallweit00376862015-09-29 23:15:53 +0200292out:
293 spi_finalize_current_transfer(master);
Mark Brown20251722013-07-05 20:07:27 +0100294
Mark Brown91b30852013-07-05 12:06:44 +0100295 return status;
296}
297
Mark Brown20251722013-07-05 20:07:27 +0100298static int spi_bitbang_unprepare_hardware(struct spi_master *spi)
David Brownell9904f222006-01-08 13:34:26 -0800299{
Jingoo Hancff93c52013-10-14 10:33:38 +0900300 struct spi_bitbang *bitbang;
Mark Brown20251722013-07-05 20:07:27 +0100301
302 bitbang = spi_master_get_devdata(spi);
David Brownell9904f222006-01-08 13:34:26 -0800303
Nicolas Boichatc15f6ed2015-08-17 11:52:54 +0800304 mutex_lock(&bitbang->lock);
David Brownell9904f222006-01-08 13:34:26 -0800305 bitbang->busy = 0;
Nicolas Boichatc15f6ed2015-08-17 11:52:54 +0800306 mutex_unlock(&bitbang->lock);
David Brownell9904f222006-01-08 13:34:26 -0800307
Mark Brown20251722013-07-05 20:07:27 +0100308 return 0;
David Brownell9904f222006-01-08 13:34:26 -0800309}
David Brownell9904f222006-01-08 13:34:26 -0800310
Heiner Kallweit00376862015-09-29 23:15:53 +0200311static void spi_bitbang_set_cs(struct spi_device *spi, bool enable)
312{
313 struct spi_bitbang *bitbang = spi_master_get_devdata(spi->master);
314
315 /* SPI core provides CS high / low, but bitbang driver
316 * expects CS active
317 * spi device driver takes care of handling SPI_CS_HIGH
318 */
319 enable = (!!(spi->mode & SPI_CS_HIGH) == enable);
320
321 ndelay(SPI_BITBANG_CS_DELAY);
322 bitbang->chipselect(spi, enable ? BITBANG_CS_ACTIVE :
323 BITBANG_CS_INACTIVE);
324 ndelay(SPI_BITBANG_CS_DELAY);
325}
326
David Brownell9904f222006-01-08 13:34:26 -0800327/*----------------------------------------------------------------------*/
328
Andrey Smirnov45beec32019-04-02 21:01:32 -0700329int spi_bitbang_init(struct spi_bitbang *bitbang)
330{
331 struct spi_master *master = bitbang->master;
Linus Walleij4a07b8b2019-12-05 10:13:40 +0100332 bool custom_cs;
Andrey Smirnov45beec32019-04-02 21:01:32 -0700333
Linus Walleij4a07b8b2019-12-05 10:13:40 +0100334 if (!master)
335 return -EINVAL;
336 /*
337 * We only need the chipselect callback if we are actually using it.
338 * If we just use GPIO descriptors, it is surplus. If the
339 * SPI_MASTER_GPIO_SS flag is set, we always need to call the
340 * driver-specific chipselect routine.
341 */
342 custom_cs = (!master->use_gpio_descriptors ||
343 (master->flags & SPI_MASTER_GPIO_SS));
344
345 if (custom_cs && !bitbang->chipselect)
Andrey Smirnov45beec32019-04-02 21:01:32 -0700346 return -EINVAL;
347
348 mutex_init(&bitbang->lock);
349
350 if (!master->mode_bits)
351 master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
352
353 if (master->transfer || master->transfer_one_message)
354 return -EINVAL;
355
356 master->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
357 master->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
358 master->transfer_one = spi_bitbang_transfer_one;
Linus Walleij4a07b8b2019-12-05 10:13:40 +0100359 /*
360 * When using GPIO descriptors, the ->set_cs() callback doesn't even
361 * get called unless SPI_MASTER_GPIO_SS is set.
362 */
363 if (custom_cs)
364 master->set_cs = spi_bitbang_set_cs;
Andrey Smirnov45beec32019-04-02 21:01:32 -0700365
366 if (!bitbang->txrx_bufs) {
367 bitbang->use_dma = 0;
368 bitbang->txrx_bufs = spi_bitbang_bufs;
369 if (!master->setup) {
370 if (!bitbang->setup_transfer)
371 bitbang->setup_transfer =
372 spi_bitbang_setup_transfer;
373 master->setup = spi_bitbang_setup;
374 master->cleanup = spi_bitbang_cleanup;
375 }
376 }
377
378 return 0;
379}
380EXPORT_SYMBOL_GPL(spi_bitbang_init);
381
David Brownell9904f222006-01-08 13:34:26 -0800382/**
383 * spi_bitbang_start - start up a polled/bitbanging SPI master driver
384 * @bitbang: driver handle
385 *
386 * Caller should have zero-initialized all parts of the structure, and then
387 * provided callbacks for chip selection and I/O loops. If the master has
388 * a transfer method, its final step should call spi_bitbang_transfer; or,
389 * that's the default if the transfer routine is not initialized. It should
390 * also set up the bus number and number of chipselects.
391 *
392 * For i/o loops, provide callbacks either per-word (for bitbanging, or for
393 * hardware that basically exposes a shift register) or per-spi_transfer
394 * (which takes better advantage of hardware like fifos or DMA engines).
395 *
Hans-Peter Nilsson7f8c7612007-02-12 00:52:44 -0800396 * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup,
397 * spi_bitbang_cleanup and spi_bitbang_setup_transfer to handle those spi
398 * master methods. Those methods are the defaults if the bitbang->txrx_bufs
399 * routine isn't initialized.
David Brownell9904f222006-01-08 13:34:26 -0800400 *
401 * This routine registers the spi_master, which will process requests in a
402 * dedicated task, keeping IRQs unblocked most of the time. To stop
403 * processing those requests, call spi_bitbang_stop().
Axel Lin702a4872013-09-10 15:43:41 +0800404 *
405 * On success, this routine will take a reference to master. The caller is
406 * responsible for calling spi_bitbang_stop() to decrement the reference and
407 * spi_master_put() as counterpart of spi_alloc_master() to prevent a memory
408 * leak.
David Brownell9904f222006-01-08 13:34:26 -0800409 */
410int spi_bitbang_start(struct spi_bitbang *bitbang)
411{
Guennadi Liakhovetski7a5d8ca2013-01-10 13:13:56 +0100412 struct spi_master *master = bitbang->master;
Axel Lin702a4872013-09-10 15:43:41 +0800413 int ret;
David Brownell9904f222006-01-08 13:34:26 -0800414
Andrey Smirnov45beec32019-04-02 21:01:32 -0700415 ret = spi_bitbang_init(bitbang);
416 if (ret)
417 return ret;
David Brownell9904f222006-01-08 13:34:26 -0800418
419 /* driver may get busy before register() returns, especially
420 * if someone registered boardinfo for devices
421 */
Axel Lin702a4872013-09-10 15:43:41 +0800422 ret = spi_register_master(spi_master_get(master));
423 if (ret)
424 spi_master_put(master);
425
YueHaibing5caaf292019-05-16 15:56:56 +0800426 return ret;
David Brownell9904f222006-01-08 13:34:26 -0800427}
428EXPORT_SYMBOL_GPL(spi_bitbang_start);
429
Lee Jonesc13b5042020-07-17 14:54:12 +0100430/*
David Brownell9904f222006-01-08 13:34:26 -0800431 * spi_bitbang_stop - stops the task providing spi communication
432 */
Axel Lind9721ae2014-03-29 18:50:12 +0800433void spi_bitbang_stop(struct spi_bitbang *bitbang)
David Brownell9904f222006-01-08 13:34:26 -0800434{
Chris Lesiaka836f582007-03-16 13:38:13 -0800435 spi_unregister_master(bitbang->master);
David Brownell9904f222006-01-08 13:34:26 -0800436}
437EXPORT_SYMBOL_GPL(spi_bitbang_stop);
438
439MODULE_LICENSE("GPL");
440