xref: /linux/drivers/spi/spi-bitbang.c (revision 712676ea2bb3882a852bcf49862c4247317fc9b2)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Polling/bitbanging SPI host controller controller driver utilities
4  */
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 #include <linux/time64.h>
15 
16 #include <linux/spi/spi.h>
17 #include <linux/spi/spi_bitbang.h>
18 
19 #define SPI_BITBANG_CS_DELAY	100
20 
21 
22 /*----------------------------------------------------------------------*/
23 
24 /*
25  * FIRST PART (OPTIONAL):  word-at-a-time spi_transfer support.
26  * Use this for GPIO or shift-register level hardware APIs.
27  *
28  * spi_bitbang_cs is in spi_device->controller_state, which is unavailable
29  * to glue code.  These bitbang setup() and cleanup() routines are always
30  * used, though maybe they're called from controller-aware code.
31  *
32  * chipselect() and friends may use spi_device->controller_data and
33  * controller registers as appropriate.
34  *
35  *
36  * NOTE:  SPI controller pins can often be used as GPIO pins instead,
37  * which means you could use a bitbang driver either to get hardware
38  * working quickly, or testing for differences that aren't speed related.
39  */
40 
41 typedef unsigned int (*spi_bb_txrx_bufs_fn)(struct spi_device *, spi_bb_txrx_word_fn,
42 					    unsigned int, struct spi_transfer *,
43 					    unsigned int);
44 
45 struct spi_bitbang_cs {
46 	unsigned int nsecs;	/* (clock cycle time) / 2 */
47 	spi_bb_txrx_word_fn txrx_word;
48 	spi_bb_txrx_bufs_fn txrx_bufs;
49 };
50 
51 static unsigned int bitbang_txrx_8(struct spi_device *spi,
52 	spi_bb_txrx_word_fn txrx_word,
53 	unsigned int ns,
54 	struct spi_transfer	*t,
55 	unsigned int flags)
56 {
57 	unsigned int		bits = t->bits_per_word;
58 	unsigned int		count = t->len;
59 	const u8		*tx = t->tx_buf;
60 	u8			*rx = t->rx_buf;
61 
62 	while (likely(count > 0)) {
63 		u8		word = 0;
64 
65 		if (tx)
66 			word = *tx++;
67 		word = txrx_word(spi, ns, word, bits, flags);
68 		if (rx)
69 			*rx++ = word;
70 		count -= 1;
71 	}
72 	return t->len - count;
73 }
74 
75 static unsigned int bitbang_txrx_16(struct spi_device *spi,
76 	spi_bb_txrx_word_fn txrx_word,
77 	unsigned int ns,
78 	struct spi_transfer	*t,
79 	unsigned int flags)
80 {
81 	unsigned int		bits = t->bits_per_word;
82 	unsigned int		count = t->len;
83 	const u16		*tx = t->tx_buf;
84 	u16			*rx = t->rx_buf;
85 
86 	while (likely(count > 1)) {
87 		u16		word = 0;
88 
89 		if (tx)
90 			word = *tx++;
91 		word = txrx_word(spi, ns, word, bits, flags);
92 		if (rx)
93 			*rx++ = word;
94 		count -= 2;
95 	}
96 	return t->len - count;
97 }
98 
99 static unsigned int bitbang_txrx_32(struct spi_device *spi,
100 	spi_bb_txrx_word_fn txrx_word,
101 	unsigned int ns,
102 	struct spi_transfer	*t,
103 	unsigned int flags)
104 {
105 	unsigned int		bits = t->bits_per_word;
106 	unsigned int		count = t->len;
107 	const u32		*tx = t->tx_buf;
108 	u32			*rx = t->rx_buf;
109 
110 	while (likely(count > 3)) {
111 		u32		word = 0;
112 
113 		if (tx)
114 			word = *tx++;
115 		word = txrx_word(spi, ns, word, bits, flags);
116 		if (rx)
117 			*rx++ = word;
118 		count -= 4;
119 	}
120 	return t->len - count;
121 }
122 
123 int spi_bitbang_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
124 {
125 	struct spi_bitbang_cs	*cs = spi->controller_state;
126 	u8			bits_per_word;
127 	u32			hz;
128 
129 	if (t) {
130 		bits_per_word = t->bits_per_word;
131 		hz = t->speed_hz;
132 	} else {
133 		bits_per_word = 0;
134 		hz = 0;
135 	}
136 
137 	/* spi_transfer level calls that work per-word */
138 	if (!bits_per_word)
139 		bits_per_word = spi->bits_per_word;
140 	if (bits_per_word <= 8)
141 		cs->txrx_bufs = bitbang_txrx_8;
142 	else if (bits_per_word <= 16)
143 		cs->txrx_bufs = bitbang_txrx_16;
144 	else if (bits_per_word <= 32)
145 		cs->txrx_bufs = bitbang_txrx_32;
146 	else
147 		return -EINVAL;
148 
149 	/* nsecs = (clock period)/2 */
150 	if (!hz)
151 		hz = spi->max_speed_hz;
152 	if (hz) {
153 		cs->nsecs = (NSEC_PER_SEC / 2) / hz;
154 		if (cs->nsecs > (MAX_UDELAY_MS * NSEC_PER_MSEC))
155 			return -EINVAL;
156 	}
157 
158 	return 0;
159 }
160 EXPORT_SYMBOL_GPL(spi_bitbang_setup_transfer);
161 
162 /*
163  * spi_bitbang_setup - default setup for per-word I/O loops
164  */
165 int spi_bitbang_setup(struct spi_device *spi)
166 {
167 	struct spi_bitbang_cs	*cs = spi->controller_state;
168 	struct spi_bitbang	*bitbang;
169 	bool			initial_setup = false;
170 	int			retval;
171 
172 	bitbang = spi_controller_get_devdata(spi->controller);
173 
174 	if (!cs) {
175 		cs = kzalloc(sizeof(*cs), GFP_KERNEL);
176 		if (!cs)
177 			return -ENOMEM;
178 		spi->controller_state = cs;
179 		initial_setup = true;
180 	}
181 
182 	/* per-word shift register access, in hardware or bitbanging */
183 	cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
184 	if (!cs->txrx_word) {
185 		retval = -EINVAL;
186 		goto err_free;
187 	}
188 
189 	if (bitbang->setup_transfer) {
190 		retval = bitbang->setup_transfer(spi, NULL);
191 		if (retval < 0)
192 			goto err_free;
193 	}
194 
195 	dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
196 
197 	return 0;
198 
199 err_free:
200 	if (initial_setup)
201 		kfree(cs);
202 	return retval;
203 }
204 EXPORT_SYMBOL_GPL(spi_bitbang_setup);
205 
206 /*
207  * spi_bitbang_cleanup - default cleanup for per-word I/O loops
208  */
209 void spi_bitbang_cleanup(struct spi_device *spi)
210 {
211 	kfree(spi->controller_state);
212 }
213 EXPORT_SYMBOL_GPL(spi_bitbang_cleanup);
214 
215 static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t)
216 {
217 	struct spi_bitbang_cs	*cs = spi->controller_state;
218 	unsigned int		nsecs = cs->nsecs;
219 	struct spi_bitbang	*bitbang;
220 
221 	bitbang = spi_controller_get_devdata(spi->controller);
222 	if (bitbang->set_line_direction) {
223 		int err;
224 
225 		err = bitbang->set_line_direction(spi, !!(t->tx_buf));
226 		if (err < 0)
227 			return err;
228 	}
229 
230 	if (spi->mode & SPI_3WIRE) {
231 		unsigned int flags;
232 
233 		flags = t->tx_buf ? SPI_CONTROLLER_NO_RX : SPI_CONTROLLER_NO_TX;
234 		return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, flags);
235 	}
236 	return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, 0);
237 }
238 
239 /*----------------------------------------------------------------------*/
240 
241 /*
242  * SECOND PART ... simple transfer queue runner.
243  *
244  * This costs a task context per controller, running the queue by
245  * performing each transfer in sequence.  Smarter hardware can queue
246  * several DMA transfers at once, and process several controller queues
247  * in parallel; this driver doesn't match such hardware very well.
248  *
249  * Drivers can provide word-at-a-time i/o primitives, or provide
250  * transfer-at-a-time ones to leverage dma or fifo hardware.
251  */
252 
253 static int spi_bitbang_prepare_hardware(struct spi_controller *spi)
254 {
255 	struct spi_bitbang	*bitbang;
256 
257 	bitbang = spi_controller_get_devdata(spi);
258 
259 	mutex_lock(&bitbang->lock);
260 	bitbang->busy = 1;
261 	mutex_unlock(&bitbang->lock);
262 
263 	return 0;
264 }
265 
266 static int spi_bitbang_transfer_one(struct spi_controller *ctlr,
267 				    struct spi_device *spi,
268 				    struct spi_transfer *transfer)
269 {
270 	struct spi_bitbang *bitbang = spi_controller_get_devdata(ctlr);
271 	int status = 0;
272 
273 	if (bitbang->setup_transfer) {
274 		status = bitbang->setup_transfer(spi, transfer);
275 		if (status < 0)
276 			goto out;
277 	}
278 
279 	if (transfer->len)
280 		status = bitbang->txrx_bufs(spi, transfer);
281 
282 	if (status == transfer->len)
283 		status = 0;
284 	else if (status >= 0)
285 		status = -EREMOTEIO;
286 
287 out:
288 	spi_finalize_current_transfer(ctlr);
289 
290 	return status;
291 }
292 
293 static int spi_bitbang_unprepare_hardware(struct spi_controller *spi)
294 {
295 	struct spi_bitbang	*bitbang;
296 
297 	bitbang = spi_controller_get_devdata(spi);
298 
299 	mutex_lock(&bitbang->lock);
300 	bitbang->busy = 0;
301 	mutex_unlock(&bitbang->lock);
302 
303 	return 0;
304 }
305 
306 static void spi_bitbang_set_cs(struct spi_device *spi, bool enable)
307 {
308 	struct spi_bitbang *bitbang = spi_controller_get_devdata(spi->controller);
309 
310 	/* SPI core provides CS high / low, but bitbang driver
311 	 * expects CS active
312 	 * spi device driver takes care of handling SPI_CS_HIGH
313 	 */
314 	enable = (!!(spi->mode & SPI_CS_HIGH) == enable);
315 
316 	ndelay(SPI_BITBANG_CS_DELAY);
317 	bitbang->chipselect(spi, enable ? BITBANG_CS_ACTIVE :
318 			    BITBANG_CS_INACTIVE);
319 	ndelay(SPI_BITBANG_CS_DELAY);
320 }
321 
322 /*----------------------------------------------------------------------*/
323 
324 int spi_bitbang_init(struct spi_bitbang *bitbang)
325 {
326 	struct spi_controller *ctlr = bitbang->ctlr;
327 	bool custom_cs;
328 
329 	if (!ctlr)
330 		return -EINVAL;
331 	/*
332 	 * We only need the chipselect callback if we are actually using it.
333 	 * If we just use GPIO descriptors, it is surplus. If the
334 	 * SPI_CONTROLLER_GPIO_SS flag is set, we always need to call the
335 	 * driver-specific chipselect routine.
336 	 */
337 	custom_cs = (!ctlr->use_gpio_descriptors ||
338 		     (ctlr->flags & SPI_CONTROLLER_GPIO_SS));
339 
340 	if (custom_cs && !bitbang->chipselect)
341 		return -EINVAL;
342 
343 	mutex_init(&bitbang->lock);
344 
345 	if (!ctlr->mode_bits)
346 		ctlr->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags;
347 
348 	if (ctlr->transfer || ctlr->transfer_one_message)
349 		return -EINVAL;
350 
351 	ctlr->prepare_transfer_hardware = spi_bitbang_prepare_hardware;
352 	ctlr->unprepare_transfer_hardware = spi_bitbang_unprepare_hardware;
353 	ctlr->transfer_one = spi_bitbang_transfer_one;
354 	/*
355 	 * When using GPIO descriptors, the ->set_cs() callback doesn't even
356 	 * get called unless SPI_CONTROLLER_GPIO_SS is set.
357 	 */
358 	if (custom_cs)
359 		ctlr->set_cs = spi_bitbang_set_cs;
360 
361 	if (!bitbang->txrx_bufs) {
362 		bitbang->use_dma = 0;
363 		bitbang->txrx_bufs = spi_bitbang_bufs;
364 		if (!ctlr->setup) {
365 			if (!bitbang->setup_transfer)
366 				bitbang->setup_transfer =
367 					 spi_bitbang_setup_transfer;
368 			ctlr->setup = spi_bitbang_setup;
369 			ctlr->cleanup = spi_bitbang_cleanup;
370 		}
371 	}
372 
373 	return 0;
374 }
375 EXPORT_SYMBOL_GPL(spi_bitbang_init);
376 
377 /**
378  * spi_bitbang_start - start up a polled/bitbanging SPI host controller driver
379  * @bitbang: driver handle
380  *
381  * Caller should have zero-initialized all parts of the structure, and then
382  * provided callbacks for chip selection and I/O loops.  If the host controller has
383  * a transfer method, its final step should call spi_bitbang_transfer(); or,
384  * that's the default if the transfer routine is not initialized.  It should
385  * also set up the bus number and number of chipselects.
386  *
387  * For i/o loops, provide callbacks either per-word (for bitbanging, or for
388  * hardware that basically exposes a shift register) or per-spi_transfer
389  * (which takes better advantage of hardware like fifos or DMA engines).
390  *
391  * Drivers using per-word I/O loops should use (or call) spi_bitbang_setup(),
392  * spi_bitbang_cleanup() and spi_bitbang_setup_transfer() to handle those SPI
393  * host controller methods.  Those methods are the defaults if the bitbang->txrx_bufs
394  * routine isn't initialized.
395  *
396  * This routine registers the spi_controller, which will process requests in a
397  * dedicated task, keeping IRQs unblocked most of the time.  To stop
398  * processing those requests, call spi_bitbang_stop().
399  *
400  * On success, this routine will take a reference to the controller. The caller
401  * is responsible for calling spi_bitbang_stop() to decrement the reference and
402  * spi_controller_put() as counterpart of spi_alloc_host() to prevent a memory
403  * leak.
404  */
405 int spi_bitbang_start(struct spi_bitbang *bitbang)
406 {
407 	struct spi_controller *ctlr = bitbang->ctlr;
408 	int ret;
409 
410 	ret = spi_bitbang_init(bitbang);
411 	if (ret)
412 		return ret;
413 
414 	/* driver may get busy before register() returns, especially
415 	 * if someone registered boardinfo for devices
416 	 */
417 	ret = spi_register_controller(spi_controller_get(ctlr));
418 	if (ret)
419 		spi_controller_put(ctlr);
420 
421 	return ret;
422 }
423 EXPORT_SYMBOL_GPL(spi_bitbang_start);
424 
425 /*
426  * spi_bitbang_stop - stops the task providing spi communication
427  */
428 void spi_bitbang_stop(struct spi_bitbang *bitbang)
429 {
430 	spi_unregister_controller(bitbang->ctlr);
431 }
432 EXPORT_SYMBOL_GPL(spi_bitbang_stop);
433 
434 MODULE_LICENSE("GPL");
435 MODULE_DESCRIPTION("Utilities for Bitbanging SPI host controllers");
436