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