1 /*
2 * Copyright (c) 2017 Piotr Mienkowski
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /**
8 * @file
9 * @brief Public APIs for the I2S (Inter-IC Sound) bus drivers.
10 */
11
12 #ifndef ZEPHYR_INCLUDE_DRIVERS_I2S_H_
13 #define ZEPHYR_INCLUDE_DRIVERS_I2S_H_
14
15 /**
16 * @defgroup i2s_interface I2S Interface
17 * @ingroup io_interfaces
18 * @brief I2S (Inter-IC Sound) Interface
19 *
20 * The I2S API provides support for the standard I2S interface standard as well
21 * as common non-standard extensions such as PCM Short/Long Frame Sync,
22 * Left/Right Justified Data Format.
23 * @{
24 */
25
26 #include <zephyr/types.h>
27 #include <zephyr/device.h>
28
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32
33 /*
34 * The following #defines are used to configure the I2S controller.
35 */
36
37 /** I2S data stream format options */
38 typedef uint8_t i2s_fmt_t;
39
40 /** Data Format bit field position. */
41 #define I2S_FMT_DATA_FORMAT_SHIFT 0
42 /** Data Format bit field mask. */
43 #define I2S_FMT_DATA_FORMAT_MASK (0x7 << I2S_FMT_DATA_FORMAT_SHIFT)
44
45 /** @brief Standard I2S Data Format.
46 *
47 * Serial data is transmitted in two's complement with the MSB first. Both
48 * Word Select (WS) and Serial Data (SD) signals are sampled on the rising edge
49 * of the clock signal (SCK). The MSB is always sent one clock period after the
50 * WS changes. Left channel data are sent first indicated by WS = 0, followed
51 * by right channel data indicated by WS = 1.
52 *
53 * -. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-.
54 * SCK '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '
55 * -. .-------------------------------.
56 * WS '-------------------------------' '----
57 * -.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.
58 * SD | |MSB| |...| |LSB| x |...| x |MSB| |...| |LSB| x |...| x |
59 * -'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'
60 * | Left channel | Right channel |
61 */
62 #define I2S_FMT_DATA_FORMAT_I2S (0 << I2S_FMT_DATA_FORMAT_SHIFT)
63
64 /** @brief PCM Short Frame Sync Data Format.
65 *
66 * Serial data is transmitted in two's complement with the MSB first. Both
67 * Word Select (WS) and Serial Data (SD) signals are sampled on the falling edge
68 * of the clock signal (SCK). The falling edge of the frame sync signal (WS)
69 * indicates the start of the PCM word. The frame sync is one clock cycle long.
70 * An arbitrary number of data words can be sent in one frame.
71 *
72 * .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-.
73 * SCK -' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-
74 * .---. .---.
75 * WS -' '- -' '-
76 * -.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---
77 * SD | |MSB| |...| |LSB|MSB| |...| |LSB|MSB| |...| |LSB|
78 * -'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---
79 * | Word 1 | Word 2 | Word 3 | Word n |
80 */
81 #define I2S_FMT_DATA_FORMAT_PCM_SHORT (1 << I2S_FMT_DATA_FORMAT_SHIFT)
82
83 /** @brief PCM Long Frame Sync Data Format.
84 *
85 * Serial data is transmitted in two's complement with the MSB first. Both
86 * Word Select (WS) and Serial Data (SD) signals are sampled on the falling edge
87 * of the clock signal (SCK). The rising edge of the frame sync signal (WS)
88 * indicates the start of the PCM word. The frame sync has an arbitrary length,
89 * however it has to fall before the start of the next frame. An arbitrary
90 * number of data words can be sent in one frame.
91 *
92 * .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-.
93 * SCK -' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-
94 * .--- ---. ---. ---. .---
95 * WS -' '- '- '- -'
96 * -.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---
97 * SD | |MSB| |...| |LSB|MSB| |...| |LSB|MSB| |...| |LSB|
98 * -'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---
99 * | Word 1 | Word 2 | Word 3 | Word n |
100 */
101 #define I2S_FMT_DATA_FORMAT_PCM_LONG (2 << I2S_FMT_DATA_FORMAT_SHIFT)
102
103 /**
104 * @brief Left Justified Data Format.
105 *
106 * Serial data is transmitted in two's complement with the MSB first. Both
107 * Word Select (WS) and Serial Data (SD) signals are sampled on the rising edge
108 * of the clock signal (SCK). The bits within the data word are left justified
109 * such that the MSB is always sent in the clock period following the WS
110 * transition. Left channel data are sent first indicated by WS = 1, followed
111 * by right channel data indicated by WS = 0.
112 *
113 * .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-.
114 * SCK -' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-
115 * .-------------------------------. .-
116 * WS ---' '-------------------------------'
117 * ---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.-
118 * SD |MSB| |...| |LSB| x |...| x |MSB| |...| |LSB| x |...| x |
119 * ---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'-
120 * | Left channel | Right channel |
121 */
122 #define I2S_FMT_DATA_FORMAT_LEFT_JUSTIFIED (3 << I2S_FMT_DATA_FORMAT_SHIFT)
123
124 /**
125 * @brief Right Justified Data Format.
126 *
127 * Serial data is transmitted in two's complement with the MSB first. Both
128 * Word Select (WS) and Serial Data (SD) signals are sampled on the rising edge
129 * of the clock signal (SCK). The bits within the data word are right justified
130 * such that the LSB is always sent in the clock period preceding the WS
131 * transition. Left channel data are sent first indicated by WS = 1, followed
132 * by right channel data indicated by WS = 0.
133 *
134 * .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-. .-.
135 * SCK -' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-' '-
136 * .-------------------------------. .-
137 * WS ---' '-------------------------------'
138 * ---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.---.-
139 * SD | x |...| x |MSB| |...| |LSB| x |...| x |MSB| |...| |LSB|
140 * ---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'---'-
141 * | Left channel | Right channel |
142 */
143 #define I2S_FMT_DATA_FORMAT_RIGHT_JUSTIFIED (4 << I2S_FMT_DATA_FORMAT_SHIFT)
144
145 /** Send MSB first */
146 #define I2S_FMT_DATA_ORDER_MSB (0 << 3)
147 /** Send LSB first */
148 #define I2S_FMT_DATA_ORDER_LSB BIT(3)
149 /** Invert bit ordering, send LSB first */
150 #define I2S_FMT_DATA_ORDER_INV I2S_FMT_DATA_ORDER_LSB
151
152 /** Data Format bit field position. */
153 #define I2S_FMT_CLK_FORMAT_SHIFT 4
154 /** Data Format bit field mask. */
155 #define I2S_FMT_CLK_FORMAT_MASK (0x3 << I2S_FMT_CLK_FORMAT_SHIFT)
156
157 /** Invert bit clock */
158 #define I2S_FMT_BIT_CLK_INV BIT(4)
159 /** Invert frame clock */
160 #define I2S_FMT_FRAME_CLK_INV BIT(5)
161
162 /** Normal Frame, Normal Bit Clk */
163 #define I2S_FMT_CLK_NF_NB (0 << I2S_FMT_CLK_FORMAT_SHIFT)
164 /** Normal Frame, Inverted Bit Clk */
165 #define I2S_FMT_CLK_NF_IB (1 << I2S_FMT_CLK_FORMAT_SHIFT)
166 /** Inverted Frame, Normal Bit Clk */
167 #define I2S_FMT_CLK_IF_NB (2 << I2S_FMT_CLK_FORMAT_SHIFT)
168 /** Inverted Frame, Inverted Bit Clk */
169 #define I2S_FMT_CLK_IF_IB (3 << I2S_FMT_CLK_FORMAT_SHIFT)
170
171 /** I2S configuration options */
172 typedef uint8_t i2s_opt_t;
173
174 /** Run bit clock continuously */
175 #define I2S_OPT_BIT_CLK_CONT (0 << 0)
176 /** Run bit clock when sending data only */
177 #define I2S_OPT_BIT_CLK_GATED BIT(0)
178 /** I2S driver is bit clock master */
179 #define I2S_OPT_BIT_CLK_MASTER (0 << 1)
180 /** I2S driver is bit clock slave */
181 #define I2S_OPT_BIT_CLK_SLAVE BIT(1)
182 /** I2S driver is frame clock master */
183 #define I2S_OPT_FRAME_CLK_MASTER (0 << 2)
184 /** I2S driver is frame clock slave */
185 #define I2S_OPT_FRAME_CLK_SLAVE BIT(2)
186
187 /** @brief Loop back mode.
188 *
189 * In loop back mode RX input will be connected internally to TX output.
190 * This is used primarily for testing.
191 */
192 #define I2S_OPT_LOOPBACK BIT(7)
193
194 /** @brief Ping pong mode
195 *
196 * In ping pong mode TX output will keep alternating between a ping buffer
197 * and a pong buffer. This is normally used in audio streams when one buffer
198 * is being populated while the other is being played (DMAed) and vice versa.
199 * So, in this mode, 2 sets of buffers fixed in size are used. Static Arrays
200 * are used to achieve this and hence they are never freed.
201 */
202 #define I2S_OPT_PINGPONG BIT(6)
203
204 /**
205 * @brief I2C Direction
206 */
207 enum i2s_dir {
208 /** Receive data */
209 I2S_DIR_RX,
210 /** Transmit data */
211 I2S_DIR_TX,
212 /** Both receive and transmit data */
213 I2S_DIR_BOTH,
214 };
215
216 /** Interface state */
217 enum i2s_state {
218 /** @brief The interface is not ready.
219 *
220 * The interface was initialized but is not yet ready to receive /
221 * transmit data. Call i2s_configure() to configure interface and change
222 * its state to READY.
223 */
224 I2S_STATE_NOT_READY,
225 /** The interface is ready to receive / transmit data. */
226 I2S_STATE_READY,
227 /** The interface is receiving / transmitting data. */
228 I2S_STATE_RUNNING,
229 /** The interface is draining its transmit queue. */
230 I2S_STATE_STOPPING,
231 /** TX buffer underrun or RX buffer overrun has occurred. */
232 I2S_STATE_ERROR,
233 };
234
235 /** Trigger command */
236 enum i2s_trigger_cmd {
237 /** @brief Start the transmission / reception of data.
238 *
239 * If I2S_DIR_TX is set some data has to be queued for transmission by
240 * the i2s_write() function. This trigger can be used in READY state
241 * only and changes the interface state to RUNNING.
242 */
243 I2S_TRIGGER_START,
244 /** @brief Stop the transmission / reception of data.
245 *
246 * Stop the transmission / reception of data at the end of the current
247 * memory block. This trigger can be used in RUNNING state only and at
248 * first changes the interface state to STOPPING. When the current TX /
249 * RX block is transmitted / received the state is changed to READY.
250 * Subsequent START trigger will resume transmission / reception where
251 * it stopped.
252 */
253 I2S_TRIGGER_STOP,
254 /** @brief Empty the transmit queue.
255 *
256 * Send all data in the transmit queue and stop the transmission.
257 * If the trigger is applied to the RX queue it has the same effect as
258 * I2S_TRIGGER_STOP. This trigger can be used in RUNNING state only and
259 * at first changes the interface state to STOPPING. When all TX blocks
260 * are transmitted the state is changed to READY.
261 */
262 I2S_TRIGGER_DRAIN,
263 /** @brief Discard the transmit / receive queue.
264 *
265 * Stop the transmission / reception immediately and discard the
266 * contents of the respective queue. This trigger can be used in any
267 * state other than NOT_READY and changes the interface state to READY.
268 */
269 I2S_TRIGGER_DROP,
270 /** @brief Prepare the queues after underrun/overrun error has occurred.
271 *
272 * This trigger can be used in ERROR state only and changes the
273 * interface state to READY.
274 */
275 I2S_TRIGGER_PREPARE,
276 };
277
278 /** @struct i2s_config
279 * @brief Interface configuration options.
280 *
281 * Memory slab pointed to by the mem_slab field has to be defined and
282 * initialized by the user. For I2S driver to function correctly number of
283 * memory blocks in a slab has to be at least 2 per queue. Size of the memory
284 * block should be multiple of frame_size where frame_size = (channels *
285 * word_size_bytes). As an example 16 bit word will occupy 2 bytes, 24 or 32
286 * bit word will occupy 4 bytes.
287 *
288 * Please check Zephyr Kernel Primer for more information on memory slabs.
289 *
290 * @remark When I2S data format is selected parameter channels is ignored,
291 * number of words in a frame is always 2.
292 */
293 struct i2s_config {
294 /** Number of bits representing one data word. */
295 uint8_t word_size;
296 /** Number of words per frame. */
297 uint8_t channels;
298 /** Data stream format as defined by I2S_FMT_* constants. */
299 i2s_fmt_t format;
300 /** Configuration options as defined by I2S_OPT_* constants. */
301 i2s_opt_t options;
302 /** Frame clock (WS) frequency, this is sampling rate. */
303 uint32_t frame_clk_freq;
304 /** Memory slab to store RX/TX data. */
305 struct k_mem_slab *mem_slab;
306 /** Size of one RX/TX memory block (buffer) in bytes. */
307 size_t block_size;
308 /** Read/Write timeout. Number of milliseconds to wait in case TX queue
309 * is full or RX queue is empty, or 0, or SYS_FOREVER_MS.
310 */
311 int32_t timeout;
312 };
313
314 /**
315 * @cond INTERNAL_HIDDEN
316 *
317 * For internal use only, skip these in public documentation.
318 */
319 __subsystem struct i2s_driver_api {
320 int (*configure)(const struct device *dev, enum i2s_dir dir,
321 const struct i2s_config *cfg);
322 const struct i2s_config *(*config_get)(const struct device *dev,
323 enum i2s_dir dir);
324 int (*read)(const struct device *dev, void **mem_block, size_t *size);
325 int (*write)(const struct device *dev, void *mem_block, size_t size);
326 int (*trigger)(const struct device *dev, enum i2s_dir dir,
327 enum i2s_trigger_cmd cmd);
328 };
329 /**
330 * @endcond
331 */
332
333 /**
334 * @brief Configure operation of a host I2S controller.
335 *
336 * The dir parameter specifies if Transmit (TX) or Receive (RX) direction
337 * will be configured by data provided via cfg parameter.
338 *
339 * The function can be called in NOT_READY or READY state only. If executed
340 * successfully the function will change the interface state to READY.
341 *
342 * If the function is called with the parameter cfg->frame_clk_freq set to 0
343 * the interface state will be changed to NOT_READY.
344 *
345 * @param dev Pointer to the device structure for the driver instance.
346 * @param dir Stream direction: RX, TX, or both, as defined by I2S_DIR_*.
347 * The I2S_DIR_BOTH value may not be supported by some drivers.
348 * For those, the RX and TX streams need to be configured separately.
349 * @param cfg Pointer to the structure containing configuration parameters.
350 *
351 * @retval 0 If successful.
352 * @retval -EINVAL Invalid argument.
353 * @retval -ENOSYS I2S_DIR_BOTH value is not supported.
354 */
355 __syscall int i2s_configure(const struct device *dev, enum i2s_dir dir,
356 const struct i2s_config *cfg);
357
z_impl_i2s_configure(const struct device * dev,enum i2s_dir dir,const struct i2s_config * cfg)358 static inline int z_impl_i2s_configure(const struct device *dev,
359 enum i2s_dir dir,
360 const struct i2s_config *cfg)
361 {
362 const struct i2s_driver_api *api =
363 (const struct i2s_driver_api *)dev->api;
364
365 return api->configure(dev, dir, cfg);
366 }
367
368 /**
369 * @brief Fetch configuration information of a host I2S controller
370 *
371 * @param dev Pointer to the device structure for the driver instance
372 * @param dir Stream direction: RX or TX as defined by I2S_DIR_*
373 * @retval Pointer to the structure containing configuration parameters,
374 * or NULL if un-configured
375 */
i2s_config_get(const struct device * dev,enum i2s_dir dir)376 static inline const struct i2s_config *i2s_config_get(const struct device *dev,
377 enum i2s_dir dir)
378 {
379 const struct i2s_driver_api *api =
380 (const struct i2s_driver_api *)dev->api;
381
382 return api->config_get(dev, dir);
383 }
384
385 /**
386 * @brief Read data from the RX queue.
387 *
388 * Data received by the I2S interface is stored in the RX queue consisting of
389 * memory blocks preallocated by this function from rx_mem_slab (as defined by
390 * i2s_configure). Ownership of the RX memory block is passed on to the user
391 * application which has to release it.
392 *
393 * The data is read in chunks equal to the size of the memory block. If the
394 * interface is in READY state the number of bytes read can be smaller.
395 *
396 * If there is no data in the RX queue the function will block waiting for
397 * the next RX memory block to fill in. This operation can timeout as defined
398 * by i2s_configure. If the timeout value is set to K_NO_WAIT the function
399 * is non-blocking.
400 *
401 * Reading from the RX queue is possible in any state other than NOT_READY.
402 * If the interface is in the ERROR state it is still possible to read all the
403 * valid data stored in RX queue. Afterwards the function will return -EIO
404 * error.
405 *
406 * @param dev Pointer to the device structure for the driver instance.
407 * @param mem_block Pointer to the RX memory block containing received data.
408 * @param size Pointer to the variable storing the number of bytes read.
409 *
410 * @retval 0 If successful.
411 * @retval -EIO The interface is in NOT_READY or ERROR state and there are no
412 * more data blocks in the RX queue.
413 * @retval -EBUSY Returned without waiting.
414 * @retval -EAGAIN Waiting period timed out.
415 */
i2s_read(const struct device * dev,void ** mem_block,size_t * size)416 static inline int i2s_read(const struct device *dev, void **mem_block,
417 size_t *size)
418 {
419 const struct i2s_driver_api *api =
420 (const struct i2s_driver_api *)dev->api;
421
422 return api->read(dev, mem_block, size);
423 }
424
425 /**
426 * @brief Read data from the RX queue into a provided buffer
427 *
428 * Data received by the I2S interface is stored in the RX queue consisting of
429 * memory blocks preallocated by this function from rx_mem_slab (as defined by
430 * i2s_configure). Calling this function removes one block from the queue
431 * which is copied into the provided buffer and then freed.
432 *
433 * The provided buffer must be large enough to contain a full memory block
434 * of data, which is parameterized for the channel via i2s_configure().
435 *
436 * This function is otherwise equivalent to i2s_read().
437 *
438 * @param dev Pointer to the device structure for the driver instance.
439 * @param buf Destination buffer for read data, which must be at least the
440 * as large as the configured memory block size for the RX channel.
441 * @param size Pointer to the variable storing the number of bytes read.
442 *
443 * @retval 0 If successful.
444 * @retval -EIO The interface is in NOT_READY or ERROR state and there are no
445 * more data blocks in the RX queue.
446 * @retval -EBUSY Returned without waiting.
447 * @retval -EAGAIN Waiting period timed out.
448 */
449 __syscall int i2s_buf_read(const struct device *dev, void *buf, size_t *size);
450
451 /**
452 * @brief Write data to the TX queue.
453 *
454 * Data to be sent by the I2S interface is stored first in the TX queue. TX
455 * queue consists of memory blocks preallocated by the user from tx_mem_slab
456 * (as defined by i2s_configure). This function takes ownership of the memory
457 * block and will release it when all data are transmitted.
458 *
459 * If there are no free slots in the TX queue the function will block waiting
460 * for the next TX memory block to be send and removed from the queue. This
461 * operation can timeout as defined by i2s_configure. If the timeout value is
462 * set to K_NO_WAIT the function is non-blocking.
463 *
464 * Writing to the TX queue is only possible if the interface is in READY or
465 * RUNNING state.
466 *
467 * @param dev Pointer to the device structure for the driver instance.
468 * @param mem_block Pointer to the TX memory block containing data to be sent.
469 * @param size Number of bytes to write. This value has to be equal or smaller
470 * than the size of the memory block.
471 *
472 * @retval 0 If successful.
473 * @retval -EIO The interface is not in READY or RUNNING state.
474 * @retval -EBUSY Returned without waiting.
475 * @retval -EAGAIN Waiting period timed out.
476 */
i2s_write(const struct device * dev,void * mem_block,size_t size)477 static inline int i2s_write(const struct device *dev, void *mem_block,
478 size_t size)
479 {
480 const struct i2s_driver_api *api =
481 (const struct i2s_driver_api *)dev->api;
482
483 return api->write(dev, mem_block, size);
484 }
485
486 /**
487 * @brief Write data to the TX queue from a provided buffer
488 *
489 * This function acquires a memory block from the I2S channel TX queue
490 * and copies the provided data buffer into it. It is otherwise equivalent
491 * to i2s_write().
492 *
493 * @param dev Pointer to the device structure for the driver instance.
494 * @param buf Pointer to a buffer containing the data to transmit.
495 * @param size Number of bytes to write. This value has to be equal or smaller
496 * than the size of the channel's TX memory block configuration.
497 *
498 * @retval 0 If successful.
499 * @retval -EIO The interface is not in READY or RUNNING state.
500 * @retval -EBUSY Returned without waiting.
501 * @retval -EAGAIN Waiting period timed out.
502 * @retval -ENOMEM No memory in TX slab queue.
503 * @retval -EINVAL Size parameter larger than TX queue memory block.
504 */
505 __syscall int i2s_buf_write(const struct device *dev, void *buf, size_t size);
506
507 /**
508 * @brief Send a trigger command.
509 *
510 * @param dev Pointer to the device structure for the driver instance.
511 * @param dir Stream direction: RX, TX, or both, as defined by I2S_DIR_*.
512 * The I2S_DIR_BOTH value may not be supported by some drivers.
513 * For those, triggering need to be done separately for the RX
514 * and TX streams.
515 * @param cmd Trigger command.
516 *
517 * @retval 0 If successful.
518 * @retval -EINVAL Invalid argument.
519 * @retval -EIO The trigger cannot be executed in the current state or a DMA
520 * channel cannot be allocated.
521 * @retval -ENOMEM RX/TX memory block not available.
522 * @retval -ENOSYS I2S_DIR_BOTH value is not supported.
523 */
524 __syscall int i2s_trigger(const struct device *dev, enum i2s_dir dir,
525 enum i2s_trigger_cmd cmd);
526
z_impl_i2s_trigger(const struct device * dev,enum i2s_dir dir,enum i2s_trigger_cmd cmd)527 static inline int z_impl_i2s_trigger(const struct device *dev,
528 enum i2s_dir dir,
529 enum i2s_trigger_cmd cmd)
530 {
531 const struct i2s_driver_api *api =
532 (const struct i2s_driver_api *)dev->api;
533
534 return api->trigger(dev, dir, cmd);
535 }
536
537 /**
538 * @}
539 */
540
541 #ifdef __cplusplus
542 }
543 #endif
544
545 #include <syscalls/i2s.h>
546
547 #endif /* ZEPHYR_INCLUDE_DRIVERS_I2S_H_ */
548