1 /*
2  * Copyright (c) 2015 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Public API for SPI drivers and applications
10  */
11 
12 #ifndef ZEPHYR_INCLUDE_DRIVERS_SPI_H_
13 #define ZEPHYR_INCLUDE_DRIVERS_SPI_H_
14 
15 /**
16  * @brief SPI Interface
17  * @defgroup spi_interface SPI Interface
18  * @since 1.0
19  * @version 1.0.0
20  * @ingroup io_interfaces
21  * @{
22  */
23 
24 #include <zephyr/types.h>
25 #include <stddef.h>
26 #include <zephyr/device.h>
27 #include <zephyr/dt-bindings/spi/spi.h>
28 #include <zephyr/drivers/gpio.h>
29 #include <zephyr/kernel.h>
30 #include <zephyr/sys/__assert.h>
31 #include <zephyr/rtio/rtio.h>
32 #include <zephyr/stats/stats.h>
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /**
39  * @name SPI operational mode
40  * @{
41  */
42 #define SPI_OP_MODE_MASTER	0U      /**< Master mode. */
43 #define SPI_OP_MODE_SLAVE	BIT(0)  /**< Slave mode. */
44 /** @cond INTERNAL_HIDDEN */
45 #define SPI_OP_MODE_MASK	0x1U
46 /** @endcond */
47 /** Get SPI operational mode. */
48 #define SPI_OP_MODE_GET(_operation_) ((_operation_) & SPI_OP_MODE_MASK)
49 /** @} */
50 
51 /**
52  * @name SPI Polarity & Phase Modes
53  * @{
54  */
55 
56 /**
57  * Clock Polarity: if set, clock idle state will be 1
58  * and active state will be 0. If untouched, the inverse will be true
59  * which is the default.
60  */
61 #define SPI_MODE_CPOL		BIT(1)
62 
63 /**
64  * Clock Phase: this dictates when is the data captured, and depends
65  * clock's polarity. When SPI_MODE_CPOL is set and this bit as well,
66  * capture will occur on low to high transition and high to low if
67  * this bit is not set (default). This is fully reversed if CPOL is
68  * not set.
69  */
70 #define SPI_MODE_CPHA		BIT(2)
71 
72 /**
73  * Whatever data is transmitted is looped-back to the receiving buffer of
74  * the controller. This is fully controller dependent as some may not
75  * support this, and can be used for testing purposes only.
76  */
77 #define SPI_MODE_LOOP		BIT(3)
78 /** @cond INTERNAL_HIDDEN */
79 #define SPI_MODE_MASK		(0xEU)
80 /** @endcond */
81 /** Get SPI polarity and phase mode bits. */
82 #define SPI_MODE_GET(_mode_)			\
83 	((_mode_) & SPI_MODE_MASK)
84 
85 /** @} */
86 
87 /**
88  * @name SPI Transfer modes (host controller dependent)
89  * @{
90  */
91 #define SPI_TRANSFER_MSB	(0U)    /**< Most significant bit first. */
92 #define SPI_TRANSFER_LSB	BIT(4)  /**< Least significant bit first. */
93 /** @} */
94 
95 /**
96  * @name SPI word size
97  * @{
98  */
99 /** @cond INTERNAL_HIDDEN */
100 #define SPI_WORD_SIZE_SHIFT	(5U)
101 #define SPI_WORD_SIZE_MASK	(0x3FU << SPI_WORD_SIZE_SHIFT)
102 /** @endcond */
103 /** Get SPI word size (data frame size) in bits. */
104 #define SPI_WORD_SIZE_GET(_operation_)					\
105 	(((_operation_) & SPI_WORD_SIZE_MASK) >> SPI_WORD_SIZE_SHIFT)
106 /** Set SPI word size (data frame size) in bits. */
107 #define SPI_WORD_SET(_word_size_)		\
108 	((_word_size_) << SPI_WORD_SIZE_SHIFT)
109 /** @} */
110 
111 /**
112  * @name Specific SPI devices control bits
113  * @{
114  */
115 /** Requests - if possible - to keep CS asserted after the transaction */
116 #define SPI_HOLD_ON_CS		BIT(12)
117 /** Keep the device locked after the transaction for the current config.
118  * Use this with extreme caution (see spi_release() below) as it will
119  * prevent other callers to access the SPI device until spi_release() is
120  * properly called.
121  */
122 #define SPI_LOCK_ON		BIT(13)
123 
124 /** Active high logic on CS. Usually, and by default, CS logic is active
125  * low. However, some devices may require the reverse logic: active high.
126  * This bit will request the controller to use that logic. Note that not
127  * all controllers are able to handle that natively. In this case deferring
128  * the CS control to a gpio line through struct spi_cs_control would be
129  * the solution.
130  */
131 #define SPI_CS_ACTIVE_HIGH	BIT(14)
132 /** @} */
133 
134 /**
135  * @name SPI MISO lines
136  * @{
137  *
138  * Some controllers support dual, quad or octal MISO lines connected to slaves.
139  * Default is single, which is the case most of the time.
140  * Without @kconfig{CONFIG_SPI_EXTENDED_MODES} being enabled, single is the
141  * only supported one.
142  */
143 #define SPI_LINES_SINGLE	(0U << 16)     /**< Single line */
144 #define SPI_LINES_DUAL		(1U << 16)     /**< Dual lines */
145 #define SPI_LINES_QUAD		(2U << 16)     /**< Quad lines */
146 #define SPI_LINES_OCTAL		(3U << 16)     /**< Octal lines */
147 
148 #define SPI_LINES_MASK		(0x3U << 16)   /**< Mask for MISO lines in spi_operation_t */
149 
150 /** @} */
151 
152 /**
153  * @brief SPI Chip Select control structure
154  *
155  * This can be used to control a CS line via a GPIO line, instead of
156  * using the controller inner CS logic.
157  *
158  */
159 struct spi_cs_control {
160 	/**
161 	 * GPIO devicetree specification of CS GPIO.
162 	 * The device pointer can be set to NULL to fully inhibit CS control if
163 	 * necessary. The GPIO flags GPIO_ACTIVE_LOW/GPIO_ACTIVE_HIGH should be
164 	 * equivalent to SPI_CS_ACTIVE_HIGH/SPI_CS_ACTIVE_LOW options in struct
165 	 * spi_config.
166 	 */
167 	struct gpio_dt_spec gpio;
168 	/**
169 	 * Delay in microseconds to wait before starting the
170 	 * transmission and before releasing the CS line.
171 	 */
172 	uint32_t delay;
173 };
174 
175 /**
176  * @brief Get a <tt>struct gpio_dt_spec</tt> for a SPI device's chip select pin
177  *
178  * Example devicetree fragment:
179  *
180  * @code{.devicetree}
181  *     gpio1: gpio@abcd0001 { ... };
182  *
183  *     gpio2: gpio@abcd0002 { ... };
184  *
185  *     spi@abcd0003 {
186  *             compatible = "vnd,spi";
187  *             cs-gpios = <&gpio1 10 GPIO_ACTIVE_LOW>,
188  *                        <&gpio2 20 GPIO_ACTIVE_LOW>;
189  *
190  *             a: spi-dev-a@0 {
191  *                     reg = <0>;
192  *             };
193  *
194  *             b: spi-dev-b@1 {
195  *                     reg = <1>;
196  *             };
197  *     };
198  * @endcode
199  *
200  * Example usage:
201  *
202  * @code{.c}
203  *     SPI_CS_GPIOS_DT_SPEC_GET(DT_NODELABEL(a)) \
204  *           // { DEVICE_DT_GET(DT_NODELABEL(gpio1)), 10, GPIO_ACTIVE_LOW }
205  *     SPI_CS_GPIOS_DT_SPEC_GET(DT_NODELABEL(b)) \
206  *           // { DEVICE_DT_GET(DT_NODELABEL(gpio2)), 20, GPIO_ACTIVE_LOW }
207  * @endcode
208  *
209  * @param spi_dev a SPI device node identifier
210  * @return #gpio_dt_spec struct corresponding with spi_dev's chip select
211  */
212 #define SPI_CS_GPIOS_DT_SPEC_GET(spi_dev)			\
213 	GPIO_DT_SPEC_GET_BY_IDX_OR(DT_BUS(spi_dev), cs_gpios,	\
214 				   DT_REG_ADDR_RAW(spi_dev), {})
215 
216 /**
217  * @brief Get a <tt>struct gpio_dt_spec</tt> for a SPI device's chip select pin
218  *
219  * This is equivalent to
220  * <tt>SPI_CS_GPIOS_DT_SPEC_GET(DT_DRV_INST(inst))</tt>.
221  *
222  * @param inst Devicetree instance number
223  * @return #gpio_dt_spec struct corresponding with spi_dev's chip select
224  */
225 #define SPI_CS_GPIOS_DT_SPEC_INST_GET(inst) \
226 	SPI_CS_GPIOS_DT_SPEC_GET(DT_DRV_INST(inst))
227 
228 /**
229  * @brief Initialize and get a pointer to a @p spi_cs_control from a
230  *        devicetree node identifier
231  *
232  * This helper is useful for initializing a device on a SPI bus. It
233  * initializes a struct spi_cs_control and returns a pointer to it.
234  * Here, @p node_id is a node identifier for a SPI device, not a SPI
235  * controller.
236  *
237  * Example devicetree fragment:
238  *
239  * @code{.devicetree}
240  *     spi@abcd0001 {
241  *             cs-gpios = <&gpio0 1 GPIO_ACTIVE_LOW>;
242  *             spidev: spi-device@0 { ... };
243  *     };
244  * @endcode
245  *
246  * Example usage:
247  *
248  * @code{.c}
249  *     struct spi_cs_control ctrl =
250  *             SPI_CS_CONTROL_INIT(DT_NODELABEL(spidev), 2);
251  * @endcode
252  *
253  * This example is equivalent to:
254  *
255  * @code{.c}
256  *     struct spi_cs_control ctrl = {
257  *             .gpio = SPI_CS_GPIOS_DT_SPEC_GET(DT_NODELABEL(spidev)),
258  *             .delay = 2,
259  *     };
260  * @endcode
261  *
262  * @param node_id Devicetree node identifier for a device on a SPI bus
263  * @param delay_ The @p delay field to set in the @p spi_cs_control
264  * @return a pointer to the @p spi_cs_control structure
265  */
266 #define SPI_CS_CONTROL_INIT(node_id, delay_)			  \
267 	{							  \
268 		.gpio = SPI_CS_GPIOS_DT_SPEC_GET(node_id),	  \
269 		.delay = (delay_),				  \
270 	}
271 
272 /**
273  * @brief Get a pointer to a @p spi_cs_control from a devicetree node
274  *
275  * This is equivalent to
276  * <tt>SPI_CS_CONTROL_INIT(DT_DRV_INST(inst), delay)</tt>.
277  *
278  * Therefore, @p DT_DRV_COMPAT must already be defined before using
279  * this macro.
280  *
281  * @param inst Devicetree node instance number
282  * @param delay_ The @p delay field to set in the @p spi_cs_control
283  * @return a pointer to the @p spi_cs_control structure
284  */
285 #define SPI_CS_CONTROL_INIT_INST(inst, delay_)		\
286 	SPI_CS_CONTROL_INIT(DT_DRV_INST(inst), delay_)
287 
288 /**
289  * @typedef spi_operation_t
290  * Opaque type to hold the SPI operation flags.
291  */
292 #if defined(CONFIG_SPI_EXTENDED_MODES)
293 typedef uint32_t spi_operation_t;
294 #else
295 typedef uint16_t spi_operation_t;
296 #endif
297 
298 /**
299  * @brief SPI controller configuration structure
300  */
301 struct spi_config {
302 	/** @brief Bus frequency in Hertz. */
303 	uint32_t frequency;
304 	/**
305 	 * @brief Operation flags.
306 	 *
307 	 * It is a bit field with the following parts:
308 	 *
309 	 * - 0:      Master or slave.
310 	 * - 1..3:   Polarity, phase and loop mode.
311 	 * - 4:      LSB or MSB first.
312 	 * - 5..10:  Size of a data frame (word) in bits.
313 	 * - 11:     Full/half duplex.
314 	 * - 12:     Hold on the CS line if possible.
315 	 * - 13:     Keep resource locked for the caller.
316 	 * - 14:     Active high CS logic.
317 	 * - 15:     Motorola or TI frame format (optional).
318 	 *
319 	 * If @kconfig{CONFIG_SPI_EXTENDED_MODES} is enabled:
320 	 *
321 	 * - 16..17: MISO lines (Single/Dual/Quad/Octal).
322 	 * - 18..31: Reserved for future use.
323 	 */
324 	spi_operation_t operation;
325 	/** @brief Slave number from 0 to host controller slave limit. */
326 	uint16_t slave;
327 	/**
328 	 * @brief GPIO chip-select line (optional, must be initialized to zero
329 	 * if not used).
330 	 */
331 	struct spi_cs_control cs;
332 };
333 
334 /**
335  * @brief Structure initializer for spi_config from devicetree
336  *
337  * This helper macro expands to a static initializer for a <tt>struct
338  * spi_config</tt> by reading the relevant @p frequency, @p slave, and
339  * @p cs data from the devicetree.
340  *
341  * @param node_id Devicetree node identifier for the SPI device whose
342  *                struct spi_config to create an initializer for
343  * @param operation_ the desired @p operation field in the struct spi_config
344  * @param delay_ the desired @p delay field in the struct spi_config's
345  *               spi_cs_control, if there is one
346  */
347 #define SPI_CONFIG_DT(node_id, operation_, delay_)			\
348 	{								\
349 		.frequency = DT_PROP(node_id, spi_max_frequency),	\
350 		.operation = (operation_) |				\
351 			DT_PROP(node_id, duplex) |			\
352 			DT_PROP(node_id, frame_format) |			\
353 			COND_CODE_1(DT_PROP(node_id, spi_cpol), SPI_MODE_CPOL, (0)) |	\
354 			COND_CODE_1(DT_PROP(node_id, spi_cpha), SPI_MODE_CPHA, (0)) |	\
355 			COND_CODE_1(DT_PROP(node_id, spi_hold_cs), SPI_HOLD_ON_CS, (0)),	\
356 		.slave = DT_REG_ADDR(node_id),				\
357 		.cs = SPI_CS_CONTROL_INIT(node_id, delay_),		\
358 	}
359 
360 /**
361  * @brief Structure initializer for spi_config from devicetree instance
362  *
363  * This is equivalent to
364  * <tt>SPI_CONFIG_DT(DT_DRV_INST(inst), operation_, delay_)</tt>.
365  *
366  * @param inst Devicetree instance number
367  * @param operation_ the desired @p operation field in the struct spi_config
368  * @param delay_ the desired @p delay field in the struct spi_config's
369  *               spi_cs_control, if there is one
370  */
371 #define SPI_CONFIG_DT_INST(inst, operation_, delay_)	\
372 	SPI_CONFIG_DT(DT_DRV_INST(inst), operation_, delay_)
373 
374 /**
375  * @brief Complete SPI DT information
376  */
377 struct spi_dt_spec {
378 	/** SPI bus */
379 	const struct device *bus;
380 	/** Slave specific configuration */
381 	struct spi_config config;
382 };
383 
384 /**
385  * @brief Structure initializer for spi_dt_spec from devicetree
386  *
387  * This helper macro expands to a static initializer for a <tt>struct
388  * spi_dt_spec</tt> by reading the relevant bus, frequency, slave, and cs
389  * data from the devicetree.
390  *
391  * Important: multiple fields are automatically constructed by this macro
392  * which must be checked before use. @ref spi_is_ready_dt performs the required
393  * @ref device_is_ready checks.
394  *
395  * @param node_id Devicetree node identifier for the SPI device whose
396  *                struct spi_dt_spec to create an initializer for
397  * @param operation_ the desired @p operation field in the struct spi_config
398  * @param delay_ the desired @p delay field in the struct spi_config's
399  *               spi_cs_control, if there is one
400  */
401 #define SPI_DT_SPEC_GET(node_id, operation_, delay_)		     \
402 	{							     \
403 		.bus = DEVICE_DT_GET(DT_BUS(node_id)),		     \
404 		.config = SPI_CONFIG_DT(node_id, operation_, delay_) \
405 	}
406 
407 /**
408  * @brief Structure initializer for spi_dt_spec from devicetree instance
409  *
410  * This is equivalent to
411  * <tt>SPI_DT_SPEC_GET(DT_DRV_INST(inst), operation_, delay_)</tt>.
412  *
413  * @param inst Devicetree instance number
414  * @param operation_ the desired @p operation field in the struct spi_config
415  * @param delay_ the desired @p delay field in the struct spi_config's
416  *               spi_cs_control, if there is one
417  */
418 #define SPI_DT_SPEC_INST_GET(inst, operation_, delay_) \
419 	SPI_DT_SPEC_GET(DT_DRV_INST(inst), operation_, delay_)
420 
421 /**
422  * @brief Value that will never compare true with any valid overrun character
423  */
424 #define SPI_MOSI_OVERRUN_UNKNOWN 0x100
425 
426 /**
427  * @brief The value sent on MOSI when all TX bytes are sent, but RX continues
428  *
429  * For drivers where the MOSI line state when receiving is important, this value
430  * can be queried at compile-time to determine whether allocating a constant
431  * array is necessary.
432  *
433  * @param node_id Devicetree node identifier for the SPI device to query
434  *
435  * @retval SPI_MOSI_OVERRUN_UNKNOWN if controller does not export the value
436  * @retval byte default MOSI value otherwise
437  */
438 #define SPI_MOSI_OVERRUN_DT(node_id) \
439 	DT_PROP_OR(node_id, overrun_character, SPI_MOSI_OVERRUN_UNKNOWN)
440 
441 /**
442  * @brief The value sent on MOSI when all TX bytes are sent, but RX continues
443  *
444  * This is equivalent to
445  * <tt>SPI_MOSI_OVERRUN_DT(DT_DRV_INST(inst))</tt>.
446  *
447  * @param inst Devicetree instance number
448  *
449  * @retval SPI_MOSI_OVERRUN_UNKNOWN if controller does not export the value
450  * @retval byte default MOSI value otherwise
451  */
452 #define SPI_MOSI_OVERRUN_DT_INST(inst) \
453 	DT_INST_PROP_OR(inst, overrun_character, SPI_MOSI_OVERRUN_UNKNOWN)
454 
455 /**
456  * @brief SPI buffer structure
457  */
458 struct spi_buf {
459 	/** Valid pointer to a data buffer, or NULL otherwise */
460 	void *buf;
461 	/** Length of the buffer @a buf in bytes.
462 	 * If @a buf is NULL, length which as to be sent as dummy bytes (as TX
463 	 * buffer) or the length of bytes that should be skipped (as RX buffer).
464 	 */
465 	size_t len;
466 };
467 
468 /**
469  * @brief SPI buffer array structure
470  */
471 struct spi_buf_set {
472 	/** Pointer to an array of spi_buf, or NULL */
473 	const struct spi_buf *buffers;
474 	/** Length of the array (number of buffers) pointed by @a buffers */
475 	size_t count;
476 };
477 
478 #if defined(CONFIG_SPI_STATS)
479 STATS_SECT_START(spi)
480 STATS_SECT_ENTRY32(rx_bytes)
481 STATS_SECT_ENTRY32(tx_bytes)
482 STATS_SECT_ENTRY32(transfer_error)
483 STATS_SECT_END;
484 
485 STATS_NAME_START(spi)
486 STATS_NAME(spi, rx_bytes)
487 STATS_NAME(spi, tx_bytes)
488 STATS_NAME(spi, transfer_error)
489 STATS_NAME_END(spi);
490 
491 /**
492  * @brief SPI specific device state which allows for SPI device class specific additions
493  */
494 struct spi_device_state {
495 	struct device_state devstate;
496 	struct stats_spi stats;
497 };
498 
499 /**
500  * @brief Get pointer to SPI statistics structure
501  */
502 #define Z_SPI_GET_STATS(dev_)				\
503 	CONTAINER_OF(dev_->state, struct spi_device_state, devstate)->stats
504 
505 /**
506  * @brief Increment the rx bytes for a SPI device
507  *
508  * @param dev_ Pointer to the device structure for the driver instance.
509  */
510 #define SPI_STATS_RX_BYTES_INCN(dev_, n)			\
511 	STATS_INCN(Z_SPI_GET_STATS(dev_), rx_bytes, n)
512 
513 /**
514  * @brief Increment the tx bytes for a SPI device
515  *
516  * @param dev_ Pointer to the device structure for the driver instance.
517  */
518 #define SPI_STATS_TX_BYTES_INCN(dev_, n)			\
519 	STATS_INCN(Z_SPI_GET_STATS(dev_), tx_bytes, n)
520 
521 /**
522  * @brief Increment the transfer error counter for a SPI device
523  *
524  * The transfer error count is incremented when there occurred a transfer error
525  *
526  * @param dev_ Pointer to the device structure for the driver instance.
527  */
528 #define SPI_STATS_TRANSFER_ERROR_INC(dev_)			\
529 	STATS_INC(Z_SPI_GET_STATS(dev_), transfer_error)
530 
531 /**
532  * @brief Define a statically allocated and section assigned SPI device state
533  */
534 #define Z_SPI_DEVICE_STATE_DEFINE(dev_id)	\
535 	static struct spi_device_state Z_DEVICE_STATE_NAME(dev_id)	\
536 	__attribute__((__section__(".z_devstate")));
537 
538 /**
539  * @brief Define an SPI device init wrapper function
540  *
541  * This does device instance specific initialization of common data (such as stats)
542  * and calls the given init_fn
543  */
544 #define Z_SPI_INIT_FN(dev_id, init_fn)					\
545 	static inline int UTIL_CAT(dev_id, _init)(const struct device *dev) \
546 	{								\
547 		struct spi_device_state *state =			\
548 			CONTAINER_OF(dev->state, struct spi_device_state, devstate); \
549 		stats_init(&state->stats.s_hdr, STATS_SIZE_32, 3,	\
550 			   STATS_NAME_INIT_PARMS(spi));			\
551 		stats_register(dev->name, &(state->stats.s_hdr));	\
552 		return init_fn(dev);					\
553 	}
554 
555 /**
556  * @brief Like DEVICE_DT_DEFINE() with SPI specifics.
557  *
558  * @details Defines a device which implements the SPI API. May
559  * generate a custom device_state container struct and init_fn
560  * wrapper when needed depending on SPI @kconfig{CONFIG_SPI_STATS}.
561  *
562  * @param node_id The devicetree node identifier.
563  * @param init_fn Name of the init function of the driver.
564  * @param pm_device PM device resources reference (NULL if device does not use PM).
565  * @param data_ptr Pointer to the device's private data.
566  * @param cfg_ptr The address to the structure containing the configuration
567  *                information for this instance of the driver.
568  * @param level The initialization level. See SYS_INIT() for details.
569  * @param prio Priority within the selected initialization level. See SYS_INIT()
570  *             for details.
571  * @param api_ptr Provides an initial pointer to the API function struct used by
572  *                the driver. Can be NULL.
573  */
574 #define SPI_DEVICE_DT_DEFINE(node_id, init_fn, pm_device,		\
575 			     data_ptr, cfg_ptr, level, prio,		\
576 			     api_ptr, ...)				\
577 	Z_SPI_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id));		\
578 	Z_SPI_INIT_FN(Z_DEVICE_DT_DEV_ID(node_id), init_fn)		\
579 	Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id),		\
580 			DEVICE_DT_NAME(node_id),			\
581 			&UTIL_CAT(Z_DEVICE_DT_DEV_ID(node_id), _init),	\
582 			pm_device,					\
583 			data_ptr, cfg_ptr, level, prio,			\
584 			api_ptr,					\
585 			&(Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)).devstate), \
586 			__VA_ARGS__)
587 
spi_transceive_stats(const struct device * dev,int error,const struct spi_buf_set * tx_bufs,const struct spi_buf_set * rx_bufs)588 static inline void spi_transceive_stats(const struct device *dev, int error,
589 					const struct spi_buf_set *tx_bufs,
590 					const struct spi_buf_set *rx_bufs)
591 {
592 	uint32_t tx_bytes;
593 	uint32_t rx_bytes;
594 
595 	if (error) {
596 		SPI_STATS_TRANSFER_ERROR_INC(dev);
597 	}
598 
599 	if (tx_bufs) {
600 		tx_bytes = tx_bufs->count ? tx_bufs->buffers->len : 0;
601 		SPI_STATS_TX_BYTES_INCN(dev, tx_bytes);
602 	}
603 
604 	if (rx_bufs) {
605 		rx_bytes = rx_bufs->count ? rx_bufs->buffers->len : 0;
606 		SPI_STATS_RX_BYTES_INCN(dev, rx_bytes);
607 	}
608 }
609 
610 #else /*CONFIG_SPI_STATS*/
611 
612 #define SPI_DEVICE_DT_DEFINE(node_id, init_fn, pm,		\
613 				data, config, level, prio,	\
614 				api, ...)			\
615 	Z_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id));			\
616 	Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id),			\
617 			DEVICE_DT_NAME(node_id), init_fn, pm, data, config,	\
618 			level, prio, api,					\
619 			&Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)),	\
620 			__VA_ARGS__)
621 
622 #define SPI_STATS_RX_BYTES_INC(dev_)
623 #define SPI_STATS_TX_BYTES_INC(dev_)
624 #define SPI_STATS_TRANSFER_ERROR_INC(dev_)
625 
626 #define spi_transceive_stats(dev, error, tx_bufs, rx_bufs)
627 
628 #endif /*CONFIG_SPI_STATS*/
629 
630 /**
631  * @brief Like SPI_DEVICE_DT_DEFINE(), but uses an instance of a `DT_DRV_COMPAT`
632  * compatible instead of a node identifier.
633  *
634  * @param inst Instance number. The `node_id` argument to SPI_DEVICE_DT_DEFINE() is
635  * set to `DT_DRV_INST(inst)`.
636  * @param ... Other parameters as expected by SPI_DEVICE_DT_DEFINE().
637  */
638 #define SPI_DEVICE_DT_INST_DEFINE(inst, ...)                                       \
639 	SPI_DEVICE_DT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
640 
641 /**
642  * @typedef spi_api_io
643  * @brief Callback API for I/O
644  * See spi_transceive() for argument descriptions
645  */
646 typedef int (*spi_api_io)(const struct device *dev,
647 			  const struct spi_config *config,
648 			  const struct spi_buf_set *tx_bufs,
649 			  const struct spi_buf_set *rx_bufs);
650 
651 /**
652  * @brief SPI callback for asynchronous transfer requests
653  *
654  * @param dev SPI device which is notifying of transfer completion or error
655  * @param result Result code of the transfer request. 0 is success, -errno for failure.
656  * @param data Transfer requester supplied data which is passed along to the callback.
657  */
658 typedef void (*spi_callback_t)(const struct device *dev, int result, void *data);
659 
660 /**
661  * @typedef spi_api_io
662  * @brief Callback API for asynchronous I/O
663  * See spi_transceive_signal() for argument descriptions
664  */
665 typedef int (*spi_api_io_async)(const struct device *dev,
666 				const struct spi_config *config,
667 				const struct spi_buf_set *tx_bufs,
668 				const struct spi_buf_set *rx_bufs,
669 				spi_callback_t cb,
670 				void *userdata);
671 
672 #if defined(CONFIG_SPI_RTIO) || defined(DOXYGEN)
673 
674 /**
675  * @typedef spi_api_iodev_submit
676  * @brief Callback API for submitting work to a SPI device with RTIO
677  */
678 typedef void (*spi_api_iodev_submit)(const struct device *dev,
679 				     struct rtio_iodev_sqe *iodev_sqe);
680 #endif /* CONFIG_SPI_RTIO */
681 
682 /**
683  * @typedef spi_api_release
684  * @brief Callback API for unlocking SPI device.
685  * See spi_release() for argument descriptions
686  */
687 typedef int (*spi_api_release)(const struct device *dev,
688 			       const struct spi_config *config);
689 
690 
691 /**
692  * @brief SPI driver API
693  * This is the mandatory API any SPI driver needs to expose.
694  */
695 __subsystem struct spi_driver_api {
696 	spi_api_io transceive;
697 #ifdef CONFIG_SPI_ASYNC
698 	spi_api_io_async transceive_async;
699 #endif /* CONFIG_SPI_ASYNC */
700 #ifdef CONFIG_SPI_RTIO
701 	spi_api_iodev_submit iodev_submit;
702 #endif /* CONFIG_SPI_RTIO */
703 	spi_api_release release;
704 };
705 
706 /**
707  * @brief Check if SPI CS is controlled using a GPIO.
708  *
709  * @param config SPI configuration.
710  * @return true If CS is controlled using a GPIO.
711  * @return false If CS is controlled by hardware or any other means.
712  */
spi_cs_is_gpio(const struct spi_config * config)713 static inline bool spi_cs_is_gpio(const struct spi_config *config)
714 {
715 	return config->cs.gpio.port != NULL;
716 }
717 
718 /**
719  * @brief Check if SPI CS in @ref spi_dt_spec is controlled using a GPIO.
720  *
721  * @param spec SPI specification from devicetree.
722  * @return true If CS is controlled using a GPIO.
723  * @return false If CS is controlled by hardware or any other means.
724  */
spi_cs_is_gpio_dt(const struct spi_dt_spec * spec)725 static inline bool spi_cs_is_gpio_dt(const struct spi_dt_spec *spec)
726 {
727 	return spi_cs_is_gpio(&spec->config);
728 }
729 
730 /**
731  * @brief Validate that SPI bus (and CS gpio if defined) is ready.
732  *
733  * @param spec SPI specification from devicetree
734  *
735  * @retval true if the SPI bus is ready for use.
736  * @retval false if the SPI bus (or the CS gpio defined) is not ready for use.
737  */
spi_is_ready_dt(const struct spi_dt_spec * spec)738 static inline bool spi_is_ready_dt(const struct spi_dt_spec *spec)
739 {
740 	/* Validate bus is ready */
741 	if (!device_is_ready(spec->bus)) {
742 		return false;
743 	}
744 	/* Validate CS gpio port is ready, if it is used */
745 	if (spi_cs_is_gpio_dt(spec) &&
746 	    !gpio_is_ready_dt(&spec->config.cs.gpio)) {
747 		return false;
748 	}
749 	return true;
750 }
751 
752 /**
753  * @brief Read/write the specified amount of data from the SPI driver.
754  *
755  * @note This function is synchronous.
756  *
757  * @param dev Pointer to the device structure for the driver instance
758  * @param config Pointer to a valid spi_config structure instance.
759  *        Pointer-comparison may be used to detect changes from
760  *        previous operations.
761  * @param tx_bufs Buffer array where data to be sent originates from,
762  *        or NULL if none.
763  * @param rx_bufs Buffer array where data to be read will be written to,
764  *        or NULL if none.
765  *
766  * @retval frames Positive number of frames received in slave mode.
767  * @retval 0 If successful in master mode.
768  * @retval -errno Negative errno code on failure.
769  */
770 __syscall int spi_transceive(const struct device *dev,
771 			     const struct spi_config *config,
772 			     const struct spi_buf_set *tx_bufs,
773 			     const struct spi_buf_set *rx_bufs);
774 
z_impl_spi_transceive(const struct device * dev,const struct spi_config * config,const struct spi_buf_set * tx_bufs,const struct spi_buf_set * rx_bufs)775 static inline int z_impl_spi_transceive(const struct device *dev,
776 					const struct spi_config *config,
777 					const struct spi_buf_set *tx_bufs,
778 					const struct spi_buf_set *rx_bufs)
779 {
780 	const struct spi_driver_api *api =
781 		(const struct spi_driver_api *)dev->api;
782 	int ret;
783 
784 	ret = api->transceive(dev, config, tx_bufs, rx_bufs);
785 	spi_transceive_stats(dev, ret, tx_bufs, rx_bufs);
786 
787 	return ret;
788 }
789 
790 /**
791  * @brief Read/write data from an SPI bus specified in @p spi_dt_spec.
792  *
793  * This is equivalent to:
794  *
795  *     spi_transceive(spec->bus, &spec->config, tx_bufs, rx_bufs);
796  *
797  * @param spec SPI specification from devicetree
798  * @param tx_bufs Buffer array where data to be sent originates from,
799  *        or NULL if none.
800  * @param rx_bufs Buffer array where data to be read will be written to,
801  *        or NULL if none.
802  *
803  * @return a value from spi_transceive().
804  */
spi_transceive_dt(const struct spi_dt_spec * spec,const struct spi_buf_set * tx_bufs,const struct spi_buf_set * rx_bufs)805 static inline int spi_transceive_dt(const struct spi_dt_spec *spec,
806 				    const struct spi_buf_set *tx_bufs,
807 				    const struct spi_buf_set *rx_bufs)
808 {
809 	return spi_transceive(spec->bus, &spec->config, tx_bufs, rx_bufs);
810 }
811 
812 /**
813  * @brief Read the specified amount of data from the SPI driver.
814  *
815  * @note This function is synchronous.
816  *
817  * @note This function is a helper function calling spi_transceive.
818  *
819  * @param dev Pointer to the device structure for the driver instance
820  * @param config Pointer to a valid spi_config structure instance.
821  *        Pointer-comparison may be used to detect changes from
822  *        previous operations.
823  * @param rx_bufs Buffer array where data to be read will be written to.
824  *
825  * @retval frames Positive number of frames received in slave mode.
826  * @retval 0 If successful.
827  * @retval -errno Negative errno code on failure.
828  */
spi_read(const struct device * dev,const struct spi_config * config,const struct spi_buf_set * rx_bufs)829 static inline int spi_read(const struct device *dev,
830 			   const struct spi_config *config,
831 			   const struct spi_buf_set *rx_bufs)
832 {
833 	return spi_transceive(dev, config, NULL, rx_bufs);
834 }
835 
836 /**
837  * @brief Read data from a SPI bus specified in @p spi_dt_spec.
838  *
839  * This is equivalent to:
840  *
841  *     spi_read(spec->bus, &spec->config, rx_bufs);
842  *
843  * @param spec SPI specification from devicetree
844  * @param rx_bufs Buffer array where data to be read will be written to.
845  *
846  * @return a value from spi_read().
847  */
spi_read_dt(const struct spi_dt_spec * spec,const struct spi_buf_set * rx_bufs)848 static inline int spi_read_dt(const struct spi_dt_spec *spec,
849 			      const struct spi_buf_set *rx_bufs)
850 {
851 	return spi_read(spec->bus, &spec->config, rx_bufs);
852 }
853 
854 /**
855  * @brief Write the specified amount of data from the SPI driver.
856  *
857  * @note This function is synchronous.
858  *
859  * @note This function is a helper function calling spi_transceive.
860  *
861  * @param dev Pointer to the device structure for the driver instance
862  * @param config Pointer to a valid spi_config structure instance.
863  *        Pointer-comparison may be used to detect changes from
864  *        previous operations.
865  * @param tx_bufs Buffer array where data to be sent originates from.
866  *
867  * @retval 0 If successful.
868  * @retval -errno Negative errno code on failure.
869  */
spi_write(const struct device * dev,const struct spi_config * config,const struct spi_buf_set * tx_bufs)870 static inline int spi_write(const struct device *dev,
871 			    const struct spi_config *config,
872 			    const struct spi_buf_set *tx_bufs)
873 {
874 	return spi_transceive(dev, config, tx_bufs, NULL);
875 }
876 
877 /**
878  * @brief Write data to a SPI bus specified in @p spi_dt_spec.
879  *
880  * This is equivalent to:
881  *
882  *     spi_write(spec->bus, &spec->config, tx_bufs);
883  *
884  * @param spec SPI specification from devicetree
885  * @param tx_bufs Buffer array where data to be sent originates from.
886  *
887  * @return a value from spi_write().
888  */
spi_write_dt(const struct spi_dt_spec * spec,const struct spi_buf_set * tx_bufs)889 static inline int spi_write_dt(const struct spi_dt_spec *spec,
890 			       const struct spi_buf_set *tx_bufs)
891 {
892 	return spi_write(spec->bus, &spec->config, tx_bufs);
893 }
894 
895 #if defined(CONFIG_SPI_ASYNC) || defined(__DOXYGEN__)
896 
897 /**
898  * @brief Read/write the specified amount of data from the SPI driver.
899  *
900  * @note This function is asynchronous.
901  *
902  * @note This function is available only if @kconfig{CONFIG_SPI_ASYNC}
903  * is selected.
904  *
905  * @param dev Pointer to the device structure for the driver instance
906  * @param config Pointer to a valid spi_config structure instance.
907  *        Pointer-comparison may be used to detect changes from
908  *        previous operations.
909  * @param tx_bufs Buffer array where data to be sent originates from,
910  *        or NULL if none.
911  * @param rx_bufs Buffer array where data to be read will be written to,
912  *        or NULL if none.
913  * @param callback Function pointer to completion callback.
914  *	  (Note: if NULL this function will not
915  *        notify the end of the transaction, and whether it went
916  *        successfully or not).
917  * @param userdata Userdata passed to callback
918  *
919  * @retval frames Positive number of frames received in slave mode.
920  * @retval 0 If successful in master mode.
921  * @retval -errno Negative errno code on failure.
922  */
spi_transceive_cb(const struct device * dev,const struct spi_config * config,const struct spi_buf_set * tx_bufs,const struct spi_buf_set * rx_bufs,spi_callback_t callback,void * userdata)923 static inline int spi_transceive_cb(const struct device *dev,
924 				    const struct spi_config *config,
925 				    const struct spi_buf_set *tx_bufs,
926 				    const struct spi_buf_set *rx_bufs,
927 				    spi_callback_t callback,
928 				    void *userdata)
929 {
930 	const struct spi_driver_api *api =
931 		(const struct spi_driver_api *)dev->api;
932 
933 	return api->transceive_async(dev, config, tx_bufs, rx_bufs, callback, userdata);
934 }
935 
936 #if defined(CONFIG_POLL) || defined(__DOXYGEN__)
937 
938 /** @cond INTERNAL_HIDDEN */
939 void z_spi_transfer_signal_cb(const struct device *dev, int result, void *userdata);
940 /** @endcond */
941 
942 /**
943  * @brief Read/write the specified amount of data from the SPI driver.
944  *
945  * @note This function is asynchronous.
946  *
947  * @note This function is available only if @kconfig{CONFIG_SPI_ASYNC}
948  * and @kconfig{CONFIG_POLL} are selected.
949  *
950  * @param dev Pointer to the device structure for the driver instance
951  * @param config Pointer to a valid spi_config structure instance.
952  *        Pointer-comparison may be used to detect changes from
953  *        previous operations.
954  * @param tx_bufs Buffer array where data to be sent originates from,
955  *        or NULL if none.
956  * @param rx_bufs Buffer array where data to be read will be written to,
957  *        or NULL if none.
958  * @param sig A pointer to a valid and ready to be signaled
959  *        struct k_poll_signal. (Note: if NULL this function will not
960  *        notify the end of the transaction, and whether it went
961  *        successfully or not).
962  *
963  * @retval frames Positive number of frames received in slave mode.
964  * @retval 0 If successful in master mode.
965  * @retval -errno Negative errno code on failure.
966  */
spi_transceive_signal(const struct device * dev,const struct spi_config * config,const struct spi_buf_set * tx_bufs,const struct spi_buf_set * rx_bufs,struct k_poll_signal * sig)967 static inline int spi_transceive_signal(const struct device *dev,
968 				       const struct spi_config *config,
969 				       const struct spi_buf_set *tx_bufs,
970 				       const struct spi_buf_set *rx_bufs,
971 				       struct k_poll_signal *sig)
972 {
973 	const struct spi_driver_api *api =
974 		(const struct spi_driver_api *)dev->api;
975 	spi_callback_t cb = (sig == NULL) ? NULL : z_spi_transfer_signal_cb;
976 
977 	return api->transceive_async(dev, config, tx_bufs, rx_bufs, cb, sig);
978 }
979 
980 /**
981  * @brief Read the specified amount of data from the SPI driver.
982  *
983  * @note This function is asynchronous.
984  *
985  * @note This function is a helper function calling spi_transceive_signal.
986  *
987  * @note This function is available only if @kconfig{CONFIG_SPI_ASYNC}
988  * and @kconfig{CONFIG_POLL} are selected.
989  *
990  * @param dev Pointer to the device structure for the driver instance
991  * @param config Pointer to a valid spi_config structure instance.
992  *        Pointer-comparison may be used to detect changes from
993  *        previous operations.
994  * @param rx_bufs Buffer array where data to be read will be written to.
995  * @param sig A pointer to a valid and ready to be signaled
996  *        struct k_poll_signal. (Note: if NULL this function will not
997  *        notify the end of the transaction, and whether it went
998  *        successfully or not).
999  *
1000  * @retval frames Positive number of frames received in slave mode.
1001  * @retval 0 If successful
1002  * @retval -errno Negative errno code on failure.
1003  */
spi_read_signal(const struct device * dev,const struct spi_config * config,const struct spi_buf_set * rx_bufs,struct k_poll_signal * sig)1004 static inline int spi_read_signal(const struct device *dev,
1005 				 const struct spi_config *config,
1006 				 const struct spi_buf_set *rx_bufs,
1007 				 struct k_poll_signal *sig)
1008 {
1009 	return spi_transceive_signal(dev, config, NULL, rx_bufs, sig);
1010 }
1011 
1012 /**
1013  * @brief Write the specified amount of data from the SPI driver.
1014  *
1015  * @note This function is asynchronous.
1016  *
1017  * @note This function is a helper function calling spi_transceive_signal.
1018  *
1019  * @note This function is available only if @kconfig{CONFIG_SPI_ASYNC}
1020  * and @kconfig{CONFIG_POLL} are selected.
1021  *
1022  * @param dev Pointer to the device structure for the driver instance
1023  * @param config Pointer to a valid spi_config structure instance.
1024  *        Pointer-comparison may be used to detect changes from
1025  *        previous operations.
1026  * @param tx_bufs Buffer array where data to be sent originates from.
1027  * @param sig A pointer to a valid and ready to be signaled
1028  *        struct k_poll_signal. (Note: if NULL this function will not
1029  *        notify the end of the transaction, and whether it went
1030  *        successfully or not).
1031  *
1032  * @retval 0 If successful.
1033  * @retval -errno Negative errno code on failure.
1034  */
spi_write_signal(const struct device * dev,const struct spi_config * config,const struct spi_buf_set * tx_bufs,struct k_poll_signal * sig)1035 static inline int spi_write_signal(const struct device *dev,
1036 				  const struct spi_config *config,
1037 				  const struct spi_buf_set *tx_bufs,
1038 				  struct k_poll_signal *sig)
1039 {
1040 	return spi_transceive_signal(dev, config, tx_bufs, NULL, sig);
1041 }
1042 
1043 #endif /* CONFIG_POLL */
1044 
1045 #endif /* CONFIG_SPI_ASYNC */
1046 
1047 
1048 #if defined(CONFIG_SPI_RTIO) || defined(__DOXYGEN__)
1049 
1050 /**
1051  * @brief Submit a SPI device with a request
1052  *
1053  * @param iodev_sqe Prepared submissions queue entry connected to an iodev
1054  *                  defined by SPI_IODEV_DEFINE.
1055  *                  Must live as long as the request is in flight.
1056  */
spi_iodev_submit(struct rtio_iodev_sqe * iodev_sqe)1057 static inline void spi_iodev_submit(struct rtio_iodev_sqe *iodev_sqe)
1058 {
1059 	const struct spi_dt_spec *dt_spec = (const struct spi_dt_spec *)iodev_sqe->sqe.iodev->data;
1060 	const struct device *dev = dt_spec->bus;
1061 	const struct spi_driver_api *api = (const struct spi_driver_api *)dev->api;
1062 
1063 	api->iodev_submit(dt_spec->bus, iodev_sqe);
1064 }
1065 
1066 extern const struct rtio_iodev_api spi_iodev_api;
1067 
1068 /**
1069  * @brief Define an iodev for a given dt node on the bus
1070  *
1071  * These do not need to be shared globally but doing so
1072  * will save a small amount of memory.
1073  *
1074  * @param name Symbolic name to use for defining the iodev
1075  * @param node_id Devicetree node identifier
1076  * @param operation_ SPI operational mode
1077  * @param delay_ Chip select delay in microseconds
1078  */
1079 #define SPI_DT_IODEV_DEFINE(name, node_id, operation_, delay_)			\
1080 	const struct spi_dt_spec _spi_dt_spec_##name =				\
1081 		SPI_DT_SPEC_GET(node_id, operation_, delay_);			\
1082 	RTIO_IODEV_DEFINE(name, &spi_iodev_api, (void *)&_spi_dt_spec_##name)
1083 
1084 /**
1085  * @brief Validate that SPI bus (and CS gpio if defined) is ready.
1086  *
1087  * @param spi_iodev SPI iodev defined with SPI_DT_IODEV_DEFINE
1088  *
1089  * @retval true if the SPI bus is ready for use.
1090  * @retval false if the SPI bus (or the CS gpio defined) is not ready for use.
1091  */
spi_is_ready_iodev(const struct rtio_iodev * spi_iodev)1092 static inline bool spi_is_ready_iodev(const struct rtio_iodev *spi_iodev)
1093 {
1094 	struct spi_dt_spec *spec = (struct spi_dt_spec *)spi_iodev->data;
1095 
1096 	return spi_is_ready_dt(spec);
1097 }
1098 
1099 #endif /* CONFIG_SPI_RTIO */
1100 
1101 /**
1102  * @brief Release the SPI device locked on and/or the CS by the current config
1103  *
1104  * Note: This synchronous function is used to release either the lock on the
1105  *       SPI device and/or the CS line that was kept if, and if only,
1106  *       given config parameter was the last one to be used (in any of the
1107  *       above functions) and if it has the SPI_LOCK_ON bit set and/or the
1108  *       SPI_HOLD_ON_CS bit set into its operation bits field.
1109  *       This can be used if the caller needs to keep its hand on the SPI
1110  *       device for consecutive transactions and/or if it needs the device to
1111  *       stay selected. Usually both bits will be used along each other, so the
1112  *       the device is locked and stays on until another operation is necessary
1113  *       or until it gets released with the present function.
1114  *
1115  * @param dev Pointer to the device structure for the driver instance
1116  * @param config Pointer to a valid spi_config structure instance.
1117  *
1118  * @retval 0 If successful.
1119  * @retval -errno Negative errno code on failure.
1120  */
1121 __syscall int spi_release(const struct device *dev,
1122 			  const struct spi_config *config);
1123 
z_impl_spi_release(const struct device * dev,const struct spi_config * config)1124 static inline int z_impl_spi_release(const struct device *dev,
1125 				     const struct spi_config *config)
1126 {
1127 	const struct spi_driver_api *api =
1128 		(const struct spi_driver_api *)dev->api;
1129 
1130 	return api->release(dev, config);
1131 }
1132 
1133 /**
1134  * @brief Release the SPI device specified in @p spi_dt_spec.
1135  *
1136  * This is equivalent to:
1137  *
1138  *     spi_release(spec->bus, &spec->config);
1139  *
1140  * @param spec SPI specification from devicetree
1141  *
1142  * @return a value from spi_release().
1143  */
spi_release_dt(const struct spi_dt_spec * spec)1144 static inline int spi_release_dt(const struct spi_dt_spec *spec)
1145 {
1146 	return spi_release(spec->bus, &spec->config);
1147 }
1148 
1149 #ifdef __cplusplus
1150 }
1151 #endif
1152 
1153 /**
1154  * @}
1155  */
1156 
1157 #include <zephyr/syscalls/spi.h>
1158 
1159 #endif /* ZEPHYR_INCLUDE_DRIVERS_SPI_H_ */
1160