blob: 7d61a3b71ee5b28d7b3fd97fd151c73d008ad9de [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * polling/bitbanging SPI master controller driver utilities
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 */
5
6#include <linux/spinlock.h>
7#include <linux/workqueue.h>
8#include <linux/interrupt.h>
9#include <linux/module.h>
10#include <linux/delay.h>
11#include <linux/errno.h>
12#include <linux/platform_device.h>
13#include <linux/slab.h>
14
15#include <linux/spi/spi.h>
16#include <linux/spi/spi_bitbang.h>
17
18#define SPI_BITBANG_CS_DELAY 100
19
20
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 *
31 * chipselect() and friends may use spi_device->controller_data and
32 * 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,
43 u32 word, u8 bits, unsigned flags);
44 unsigned (*txrx_bufs)(struct spi_device *,
45 u32 (*txrx_word)(
46 struct spi_device *spi,
47 unsigned nsecs,
48 u32 word, u8 bits,
49 unsigned flags),
50 unsigned, struct spi_transfer *,
51 unsigned);
52};
53
54static unsigned bitbang_txrx_8(
55 struct spi_device *spi,
56 u32 (*txrx_word)(struct spi_device *spi,
57 unsigned nsecs,
58 u32 word, u8 bits,
59 unsigned flags),
60 unsigned ns,
61 struct spi_transfer *t,
62 unsigned flags
63) {
64 unsigned bits = t->bits_per_word;
65 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++;
74 word = txrx_word(spi, ns, word, bits, flags);
75 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,
86 u32 word, u8 bits,
87 unsigned flags),
88 unsigned ns,
89 struct spi_transfer *t,
90 unsigned flags
91) {
92 unsigned bits = t->bits_per_word;
93 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++;
102 word = txrx_word(spi, ns, word, bits, flags);
103 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,
114 u32 word, u8 bits,
115 unsigned flags),
116 unsigned ns,
117 struct spi_transfer *t,
118 unsigned flags
119) {
120 unsigned bits = t->bits_per_word;
121 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++;
130 word = txrx_word(spi, ns, word, bits, flags);
131 if (rx)
132 *rx++ = word;
133 count -= 4;
134 }
135 return t->len - count;
136}
137
138int spi_bitbang_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
139{
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;
167 if (hz) {
168 cs->nsecs = (1000000000/2) / hz;
169 if (cs->nsecs > (MAX_UDELAY_MS * 1000 * 1000))
170 return -EINVAL;
171 }
172
173 return 0;
174}
175EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
176
177/**
178 * 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;
Olivier Deprez0e641232021-09-23 10:07:05 +0200184 bool initial_setup = false;
185 int retval;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000186
187 bitbang = spi_master_get_devdata(spi->master);
188
189 if (!cs) {
190 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
191 if (!cs)
192 return -ENOMEM;
193 spi->controller_state = cs;
Olivier Deprez0e641232021-09-23 10:07:05 +0200194 initial_setup = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000195 }
196
197 /* per-word shift register access, in hardware or bitbanging */
198 cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
Olivier Deprez0e641232021-09-23 10:07:05 +0200199 if (!cs->txrx_word) {
200 retval = -EINVAL;
201 goto err_free;
202 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000203
204 if (bitbang->setup_transfer) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200205 retval = bitbang->setup_transfer(spi, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000206 if (retval < 0)
Olivier Deprez0e641232021-09-23 10:07:05 +0200207 goto err_free;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000208 }
209
210 dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
211
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000212 return 0;
Olivier Deprez0e641232021-09-23 10:07:05 +0200213
214err_free:
215 if (initial_setup)
216 kfree(cs);
217 return retval;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000218}
219EXPORT_SYMBOL_GPL(spi_bitbang_setup);
220
221/**
222 * spi_bitbang_cleanup - default cleanup for per-word I/O loops
223 */
224void spi_bitbang_cleanup(struct spi_device *spi)
225{
226 kfree(spi->controller_state);
227}
228EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
229
230static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
231{
232 struct spi_bitbang_cs *cs = spi->controller_state;
233 unsigned nsecs = cs->nsecs;
234 struct spi_bitbang *bitbang;
235
236 bitbang = spi_master_get_devdata(spi->master);
237 if (bitbang->set_line_direction) {
238 int err;
239
240 err = bitbang->set_line_direction(spi, !!(t->tx_buf));
241 if (err < 0)
242 return err;
243 }
244
245 if (spi->mode & SPI_3WIRE) {
246 unsigned flags;
247
248 flags = t->tx_buf ? SPI_MASTER_NO_RX : SPI_MASTER_NO_TX;
249 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, flags);
250 }
251 return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, 0);
252}
253
254/*----------------------------------------------------------------------*/
255
256/*
257 * SECOND PART ... simple transfer queue runner.
258 *
259 * This costs a task context per controller, running the queue by
260 * performing each transfer in sequence. Smarter hardware can queue
261 * several DMA transfers at once, and process several controller queues
262 * in parallel; this driver doesn't match such hardware very well.
263 *
264 * Drivers can provide word-at-a-time i/o primitives, or provide
265 * transfer-at-a-time ones to leverage dma or fifo hardware.
266 */
267
268static int spi_bitbang_prepare_hardware(struct spi_master *spi)
269{
270 struct spi_bitbang *bitbang;
271
272 bitbang = spi_master_get_devdata(spi);
273
274 mutex_lock(&bitbang->lock);
275 bitbang->busy = 1;
276 mutex_unlock(&bitbang->lock);
277
278 return 0;
279}
280
281static int spi_bitbang_transfer_one(struct spi_master *master,
282 struct spi_device *spi,
283 struct spi_transfer *transfer)
284{
285 struct spi_bitbang *bitbang = spi_master_get_devdata(master);
286 int status = 0;
287
288 if (bitbang->setup_transfer) {
289 status = bitbang->setup_transfer(spi, transfer);
290 if (status < 0)
291 goto out;
292 }
293
294 if (transfer->len)
295 status = bitbang->txrx_bufs(spi, transfer);
296
297 if (status == transfer->len)
298 status = 0;
299 else if (status >= 0)
300 status = -EREMOTEIO;
301
302out:
303 spi_finalize_current_transfer(master);
304
305 return status;
306}
307
308static int spi_bitbang_unprepare_hardware(struct spi_master *spi)
309{
310 struct spi_bitbang *bitbang;
311
312 bitbang = spi_master_get_devdata(spi);
313
314 mutex_lock(&bitbang->lock);
315 bitbang->busy = 0;
316 mutex_unlock(&bitbang->lock);
317
318 return 0;
319}
320
321static void spi_bitbang_set_cs(struct spi_device *spi, bool enable)
322{
323 struct spi_bitbang *bitbang = spi_master_get_devdata(spi->master);
324
325 /* SPI core provides CS high / low, but bitbang driver
326 * expects CS active
327 * spi device driver takes care of handling SPI_CS_HIGH
328 */
329 enable = (!!(spi->mode & SPI_CS_HIGH) == enable);
330
331 ndelay(SPI_BITBANG_CS_DELAY);
332 bitbang->chipselect(spi, enable ? BITBANG_CS_ACTIVE :
333 BITBANG_CS_INACTIVE);
334 ndelay(SPI_BITBANG_CS_DELAY);
335}
336
337/*----------------------------------------------------------------------*/
338
David Brazdil0f672f62019-12-10 10:32:29 +0000339int spi_bitbang_init(struct spi_bitbang *bitbang)
340{
341 struct spi_master *master = bitbang->master;
342
343 if (!master || !bitbang->chipselect)
344 return -EINVAL;
345
346 mutex_init(&bitbang->lock);
347
348 if (!master->mode_bits)
349 master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
350
351 if (master->transfer || master->transfer_one_message)
352 return -EINVAL;
353
354 master->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
355 master->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
356 master->transfer_one = spi_bitbang_transfer_one;
357 master->set_cs = spi_bitbang_set_cs;
358
359 if (!bitbang->txrx_bufs) {
360 bitbang->use_dma = 0;
361 bitbang->txrx_bufs = spi_bitbang_bufs;
362 if (!master->setup) {
363 if (!bitbang->setup_transfer)
364 bitbang->setup_transfer =
365 spi_bitbang_setup_transfer;
366 master->setup = spi_bitbang_setup;
367 master->cleanup = spi_bitbang_cleanup;
368 }
369 }
370
371 return 0;
372}
373EXPORT_SYMBOL_GPL(spi_bitbang_init);
374
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000375/**
376 * spi_bitbang_start - start up a polled/bitbanging SPI master driver
377 * @bitbang: driver handle
378 *
379 * Caller should have zero-initialized all parts of the structure, and then
380 * provided callbacks for chip selection and I/O loops. If the master has
381 * a transfer method, its final step should call spi_bitbang_transfer; or,
382 * that's the default if the transfer routine is not initialized. It should
383 * also set up the bus number and number of chipselects.
384 *
385 * For i/o loops, provide callbacks either per-word (for bitbanging, or for
386 * hardware that basically exposes a shift register) or per-spi_transfer
387 * (which takes better advantage of hardware like fifos or DMA engines).
388 *
389 * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup,
390 * spi_bitbang_cleanup and spi_bitbang_setup_transfer to handle those spi
391 * master methods. Those methods are the defaults if the bitbang->txrx_bufs
392 * routine isn't initialized.
393 *
394 * This routine registers the spi_master, which will process requests in a
395 * dedicated task, keeping IRQs unblocked most of the time. To stop
396 * processing those requests, call spi_bitbang_stop().
397 *
398 * On success, this routine will take a reference to master. The caller is
399 * responsible for calling spi_bitbang_stop() to decrement the reference and
400 * spi_master_put() as counterpart of spi_alloc_master() to prevent a memory
401 * leak.
402 */
403int spi_bitbang_start(struct spi_bitbang *bitbang)
404{
405 struct spi_master *master = bitbang->master;
406 int ret;
407
David Brazdil0f672f62019-12-10 10:32:29 +0000408 ret = spi_bitbang_init(bitbang);
409 if (ret)
410 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000411
412 /* driver may get busy before register() returns, especially
413 * if someone registered boardinfo for devices
414 */
415 ret = spi_register_master(spi_master_get(master));
416 if (ret)
417 spi_master_put(master);
418
David Brazdil0f672f62019-12-10 10:32:29 +0000419 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000420}
421EXPORT_SYMBOL_GPL(spi_bitbang_start);
422
423/**
424 * spi_bitbang_stop - stops the task providing spi communication
425 */
426void spi_bitbang_stop(struct spi_bitbang *bitbang)
427{
428 spi_unregister_master(bitbang->master);
429}
430EXPORT_SYMBOL_GPL(spi_bitbang_stop);
431
432MODULE_LICENSE("GPL");
433