Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 | /*
* Copyright (c) 2021 NXP Semiconductor INC.
* All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
/** @file
* @brief I2S bus (SAI) driver for NXP i.MX RT series.
*/
#include <errno.h>
#include <string.h>
#include <zephyr/sys/__assert.h>
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/init.h>
#include <zephyr/drivers/dma.h>
#include <zephyr/drivers/i2s.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/drivers/clock_control.h>
#include <zephyr/dt-bindings/clock/imx_ccm.h>
#include <zephyr/sys/barrier.h>
#include <soc.h>
#include "i2s_mcux_sai.h"
#define LOG_DOMAIN dev_i2s_mcux
#define LOG_LEVEL CONFIG_I2S_LOG_LEVEL
#include <zephyr/logging/log.h>
#include <zephyr/irq.h>
LOG_MODULE_REGISTER(LOG_DOMAIN);
#define DT_DRV_COMPAT nxp_mcux_i2s
#define NUM_DMA_BLOCKS_RX_PREP 3
#define MAX_TX_DMA_BLOCKS CONFIG_DMA_TCD_QUEUE_SIZE
#if (NUM_DMA_BLOCKS_RX_PREP >= CONFIG_DMA_TCD_QUEUE_SIZE)
#error NUM_DMA_BLOCKS_RX_PREP must be < CONFIG_DMA_TCD_QUEUE_SIZE
#endif
#if defined(CONFIG_DMA_MCUX_EDMA) && (NUM_DMA_BLOCKS_RX_PREP < 3)
#error eDMA avoids TCD coherency issue if NUM_DMA_BLOCKS_RX_PREP >= 3
#endif
/*
* SAI driver uses source_gather_en/dest_scatter_en feature of DMA, and relies
* on DMA driver managing circular list of DMA blocks. Like eDMA driver links
* Transfer Control Descriptors (TCDs) in list, and manages the tcdpool.
* Calling dma_reload() adds new DMA block to DMA channel already configured,
* into the DMA driver's circular list of blocks.
* This indicates the Tx/Rx stream.
*
* in_queue and out_queue are used as follows
* transmit stream:
* application provided buffer is queued to in_queue until loaded to DMA.
* when DMA channel is idle, buffer is retrieved from in_queue and loaded
* to DMA and queued to out_queue. when DMA completes, buffer is retrieved
* from out_queue and freed.
*
* receive stream:
* driver allocates buffer from slab and loads DMA buffer is queued to
* in_queue when DMA completes, buffer is retrieved from in_queue
* and queued to out_queue when application reads, buffer is read
* (may optionally block) from out_queue and presented to application.
*/
struct stream {
int32_t state;
uint32_t dma_channel;
uint32_t start_channel;
void (*irq_call_back)(void);
struct i2s_config cfg;
struct dma_config dma_cfg;
struct dma_block_config dma_block;
uint8_t free_tx_dma_blocks;
bool last_block;
struct k_msgq in_queue;
struct k_msgq out_queue;
};
struct i2s_mcux_config {
I2S_Type *base;
uint32_t clk_src;
uint32_t clk_pre_div;
uint32_t clk_src_div;
uint32_t pll_src;
uint32_t pll_lp;
uint32_t pll_pd;
uint32_t pll_num;
uint32_t pll_den;
uint32_t mclk_pin_mask;
uint32_t mclk_pin_offset;
uint32_t tx_channel;
clock_control_subsys_t clk_sub_sys;
const struct device *ccm_dev;
const struct pinctrl_dev_config *pinctrl;
void (*irq_connect)(const struct device *dev);
bool rx_sync_mode;
bool tx_sync_mode;
};
/* Device run time data */
struct i2s_dev_data {
const struct device *dev_dma;
struct stream tx;
void *tx_in_msgs[CONFIG_I2S_TX_BLOCK_COUNT];
void *tx_out_msgs[CONFIG_I2S_TX_BLOCK_COUNT];
struct stream rx;
void *rx_in_msgs[CONFIG_I2S_RX_BLOCK_COUNT];
void *rx_out_msgs[CONFIG_I2S_RX_BLOCK_COUNT];
};
static void i2s_dma_tx_callback(const struct device *, void *,
uint32_t, int);
static void i2s_tx_stream_disable(const struct device *, bool drop);
static void i2s_rx_stream_disable(const struct device *,
bool in_drop, bool out_drop);
static inline void i2s_purge_stream_buffers(struct stream *strm,
struct k_mem_slab *mem_slab,
bool in_drop, bool out_drop)
{
void *buffer;
if (in_drop) {
while (k_msgq_get(&strm->in_queue, &buffer, K_NO_WAIT) == 0) {
k_mem_slab_free(mem_slab, buffer);
}
}
if (out_drop) {
while (k_msgq_get(&strm->out_queue, &buffer, K_NO_WAIT) == 0) {
k_mem_slab_free(mem_slab, buffer);
}
}
}
static void i2s_tx_stream_disable(const struct device *dev, bool drop)
{
struct i2s_dev_data *dev_data = dev->data;
struct stream *strm = &dev_data->tx;
const struct device *dev_dma = dev_data->dev_dma;
const struct i2s_mcux_config *dev_cfg = dev->config;
LOG_DBG("Stopping DMA channel %u for TX stream", strm->dma_channel);
/* Disable FIFO DMA request */
SAI_TxEnableDMA(dev_cfg->base, kSAI_FIFORequestDMAEnable,
false);
dma_stop(dev_dma, strm->dma_channel);
/* wait for TX FIFO to drain before disabling */
while ((dev_cfg->base->TCSR & I2S_TCSR_FWF_MASK) == 0)
;
/* Disable the channel FIFO */
dev_cfg->base->TCR3 &= ~I2S_TCR3_TCE_MASK;
/* Disable Tx */
SAI_TxEnable(dev_cfg->base, false);
/* If Tx is disabled, reset the FIFO pointer, clear error flags */
if ((dev_cfg->base->TCSR & I2S_TCSR_TE_MASK) == 0UL) {
dev_cfg->base->TCSR |=
(I2S_TCSR_FR_MASK | I2S_TCSR_SR_MASK);
dev_cfg->base->TCSR &= ~I2S_TCSR_SR_MASK;
}
/* purge buffers queued in the stream */
if (drop) {
i2s_purge_stream_buffers(strm, dev_data->tx.cfg.mem_slab,
true, true);
}
}
static void i2s_rx_stream_disable(const struct device *dev,
bool in_drop, bool out_drop)
{
struct i2s_dev_data *dev_data = dev->data;
struct stream *strm = &dev_data->rx;
const struct device *dev_dma = dev_data->dev_dma;
const struct i2s_mcux_config *dev_cfg = dev->config;
LOG_DBG("Stopping RX stream & DMA channel %u", strm->dma_channel);
dma_stop(dev_dma, strm->dma_channel);
/* Disable the channel FIFO */
dev_cfg->base->RCR3 &= ~I2S_RCR3_RCE_MASK;
/* Disable DMA enable bit */
SAI_RxEnableDMA(dev_cfg->base, kSAI_FIFORequestDMAEnable,
false);
/* Disable Rx */
SAI_RxEnable(dev_cfg->base, false);
/* wait for Receiver to disable */
while (dev_cfg->base->RCSR & I2S_RCSR_RE_MASK)
;
/* reset the FIFO pointer and clear error flags */
dev_cfg->base->RCSR |= (I2S_RCSR_FR_MASK | I2S_RCSR_SR_MASK);
dev_cfg->base->RCSR &= ~I2S_RCSR_SR_MASK;
/* purge buffers queued in the stream */
if (in_drop || out_drop) {
i2s_purge_stream_buffers(strm, dev_data->rx.cfg.mem_slab,
in_drop, out_drop);
}
}
static int i2s_tx_reload_multiple_dma_blocks(const struct device *dev,
uint8_t *blocks_queued)
{
struct i2s_dev_data *dev_data = dev->data;
const struct i2s_mcux_config *dev_cfg = dev->config;
I2S_Type *base = (I2S_Type *)dev_cfg->base;
struct stream *strm = &dev_data->tx;
void *buffer = NULL;
int ret = 0;
unsigned int key;
*blocks_queued = 0;
key = irq_lock();
/* queue additional blocks to DMA if in_queue and DMA has free blocks */
while (strm->free_tx_dma_blocks) {
/* get the next buffer from queue */
ret = k_msgq_get(&strm->in_queue, &buffer, K_NO_WAIT);
if (ret) {
/* in_queue is empty, no more blocks to send to DMA */
ret = 0;
break;
}
/* reload the DMA */
ret = dma_reload(dev_data->dev_dma, strm->dma_channel,
(uint32_t)buffer,
(uint32_t)&base->TDR[strm->start_channel],
strm->cfg.block_size);
if (ret != 0) {
LOG_ERR("dma_reload() failed with error 0x%x", ret);
break;
}
(strm->free_tx_dma_blocks)--;
ret = k_msgq_put(&strm->out_queue,
&buffer, K_NO_WAIT);
if (ret != 0) {
LOG_ERR("buffer %p -> out %p err %d",
buffer, &strm->out_queue, ret);
break;
}
(*blocks_queued)++;
}
irq_unlock(key);
return ret;
}
/* This function is executed in the interrupt context */
static void i2s_dma_tx_callback(const struct device *dma_dev,
void *arg, uint32_t channel, int status)
{
const struct device *dev = (struct device *)arg;
struct i2s_dev_data *dev_data = dev->data;
struct stream *strm = &dev_data->tx;
void *buffer = NULL;
int ret;
uint8_t blocks_queued;
LOG_DBG("tx cb");
ret = k_msgq_get(&strm->out_queue, &buffer, K_NO_WAIT);
if (ret == 0) {
/* transmission complete. free the buffer */
k_mem_slab_free(strm->cfg.mem_slab, buffer);
(strm->free_tx_dma_blocks)++;
} else {
LOG_ERR("no buf in out_queue for channel %u", channel);
}
if (strm->free_tx_dma_blocks > MAX_TX_DMA_BLOCKS) {
strm->state = I2S_STATE_ERROR;
LOG_ERR("free_tx_dma_blocks exceeded maximum, now %d",
strm->free_tx_dma_blocks);
goto disabled_exit_no_drop;
}
/* Received a STOP trigger, terminate TX immediately */
if (strm->last_block) {
strm->state = I2S_STATE_READY;
LOG_DBG("TX STOPPED last_block set");
goto disabled_exit_no_drop;
}
if (ret) {
/* k_msgq_get() returned error, and was not last_block */
strm->state = I2S_STATE_ERROR;
goto disabled_exit_no_drop;
}
switch (strm->state) {
case I2S_STATE_RUNNING:
case I2S_STATE_STOPPING:
ret = i2s_tx_reload_multiple_dma_blocks(dev, &blocks_queued);
if (ret) {
strm->state = I2S_STATE_ERROR;
goto disabled_exit_no_drop;
}
dma_start(dev_data->dev_dma, strm->dma_channel);
if (blocks_queued ||
(strm->free_tx_dma_blocks < MAX_TX_DMA_BLOCKS)) {
goto enabled_exit;
} else {
/* all DMA blocks are free but no blocks were queued */
if (strm->state == I2S_STATE_STOPPING) {
/* TX queue has drained */
strm->state = I2S_STATE_READY;
LOG_DBG("TX stream has stopped");
} else {
strm->state = I2S_STATE_ERROR;
LOG_ERR("TX Failed to reload DMA");
}
goto disabled_exit_no_drop;
}
case I2S_STATE_ERROR:
default:
goto disabled_exit_drop;
}
disabled_exit_no_drop:
i2s_tx_stream_disable(dev, false);
return;
disabled_exit_drop:
i2s_tx_stream_disable(dev, true);
return;
enabled_exit:
return;
}
static void i2s_dma_rx_callback(const struct device *dma_dev,
void *arg, uint32_t channel, int status)
{
struct device *dev = (struct device *)arg;
const struct i2s_mcux_config *dev_cfg = dev->config;
I2S_Type *base = (I2S_Type *)dev_cfg->base;
struct i2s_dev_data *dev_data = dev->data;
struct stream *strm = &dev_data->rx;
void *buffer;
int ret;
LOG_DBG("RX cb");
switch (strm->state) {
case I2S_STATE_STOPPING:
case I2S_STATE_RUNNING:
/* retrieve buffer from input queue */
ret = k_msgq_get(&strm->in_queue, &buffer, K_NO_WAIT);
__ASSERT_NO_MSG(ret == 0);
/* put buffer to output queue */
ret = k_msgq_put(&strm->out_queue, &buffer, K_NO_WAIT);
if (ret != 0) {
LOG_ERR("buffer %p -> out_queue %p err %d",
buffer,
&strm->out_queue, ret);
i2s_rx_stream_disable(dev, false, false);
strm->state = I2S_STATE_ERROR;
return;
}
if (strm->state == I2S_STATE_RUNNING) {
/* allocate new buffer for next audio frame */
ret = k_mem_slab_alloc(strm->cfg.mem_slab,
&buffer, K_NO_WAIT);
if (ret != 0) {
LOG_ERR("buffer alloc from slab %p err %d",
strm->cfg.mem_slab, ret);
i2s_rx_stream_disable(dev, false, false);
strm->state = I2S_STATE_ERROR;
} else {
uint32_t data_path = strm->start_channel;
ret = dma_reload(dev_data->dev_dma,
strm->dma_channel,
(uint32_t)&base->RDR[data_path],
(uint32_t)buffer,
strm->cfg.block_size);
if (ret != 0) {
LOG_ERR("dma_reload() failed with error 0x%x",
ret);
i2s_rx_stream_disable(dev,
false, false);
strm->state = I2S_STATE_ERROR;
return;
}
/* put buffer in input queue */
ret = k_msgq_put(&strm->in_queue,
&buffer, K_NO_WAIT);
if (ret != 0) {
LOG_ERR("%p -> in_queue %p err %d",
buffer, &strm->in_queue,
ret);
}
dma_start(dev_data->dev_dma,
strm->dma_channel);
}
} else {
i2s_rx_stream_disable(dev, true, false);
/* Received a STOP/DRAIN trigger */
strm->state = I2S_STATE_READY;
}
break;
case I2S_STATE_ERROR:
i2s_rx_stream_disable(dev, true, true);
break;
}
}
static void enable_mclk_direction(const struct device *dev, bool dir)
{
const struct i2s_mcux_config *dev_cfg = dev->config;
uint32_t offset = dev_cfg->mclk_pin_offset;
uint32_t mask = dev_cfg->mclk_pin_mask;
uint32_t *gpr = (uint32_t *)
(DT_REG_ADDR(DT_NODELABEL(iomuxcgpr)) + offset);
if (dir) {
*gpr |= mask;
} else {
*gpr &= ~mask;
}
}
static void get_mclk_rate(const struct device *dev, uint32_t *mclk)
{
const struct i2s_mcux_config *dev_cfg = dev->config;
const struct device *ccm_dev = dev_cfg->ccm_dev;
clock_control_subsys_t clk_sub_sys = dev_cfg->clk_sub_sys;
uint32_t rate = 0;
if (device_is_ready(ccm_dev)) {
clock_control_get_rate(ccm_dev, clk_sub_sys, &rate);
} else {
LOG_ERR("CCM driver is not installed");
*mclk = rate;
return;
}
*mclk = rate;
}
static int i2s_mcux_config(const struct device *dev, enum i2s_dir dir,
const struct i2s_config *i2s_cfg)
{
const struct i2s_mcux_config *dev_cfg = dev->config;
I2S_Type *base = (I2S_Type *)dev_cfg->base;
struct i2s_dev_data *dev_data = dev->data;
sai_transceiver_t config;
uint32_t mclk;
/*num_words is frame size*/
uint8_t num_words = i2s_cfg->channels;
uint8_t word_size_bits = i2s_cfg->word_size;
if ((dev_data->tx.state != I2S_STATE_NOT_READY) &&
(dev_data->tx.state != I2S_STATE_READY) &&
(dev_data->rx.state != I2S_STATE_NOT_READY) &&
(dev_data->rx.state != I2S_STATE_READY)) {
LOG_ERR("invalid state tx(%u) rx(%u)",
dev_data->tx.state,
dev_data->rx.state);
if (dir == I2S_DIR_TX) {
dev_data->tx.state = I2S_STATE_NOT_READY;
} else {
dev_data->rx.state = I2S_STATE_NOT_READY;
}
return -EINVAL;
}
if (i2s_cfg->frame_clk_freq == 0U) {
LOG_ERR("Invalid frame_clk_freq %u",
i2s_cfg->frame_clk_freq);
if (dir == I2S_DIR_TX) {
dev_data->tx.state = I2S_STATE_NOT_READY;
} else {
dev_data->rx.state = I2S_STATE_NOT_READY;
}
return 0;
}
if (word_size_bits < SAI_WORD_SIZE_BITS_MIN ||
word_size_bits > SAI_WORD_SIZE_BITS_MAX) {
LOG_ERR("Unsupported I2S word size %u", word_size_bits);
if (dir == I2S_DIR_TX) {
dev_data->tx.state = I2S_STATE_NOT_READY;
} else {
dev_data->rx.state = I2S_STATE_NOT_READY;
}
return -EINVAL;
}
if (num_words < SAI_WORD_PER_FRAME_MIN ||
num_words > SAI_WORD_PER_FRAME_MAX) {
LOG_ERR("Unsupported words length %u", num_words);
if (dir == I2S_DIR_TX) {
dev_data->tx.state = I2S_STATE_NOT_READY;
} else {
dev_data->rx.state = I2S_STATE_NOT_READY;
}
return -EINVAL;
}
if ((i2s_cfg->options & I2S_OPT_PINGPONG) == I2S_OPT_PINGPONG) {
LOG_ERR("Ping-pong mode not supported");
if (dir == I2S_DIR_TX) {
dev_data->tx.state = I2S_STATE_NOT_READY;
} else {
dev_data->rx.state = I2S_STATE_NOT_READY;
}
return -ENOTSUP;
}
memset(&config, 0, sizeof(config));
const bool is_mclk_slave = i2s_cfg->options & I2S_OPT_BIT_CLK_SLAVE;
enable_mclk_direction(dev, !is_mclk_slave);
get_mclk_rate(dev, &mclk);
LOG_DBG("mclk is %d", mclk);
/* bit clock source is MCLK */
config.bitClock.bclkSource = kSAI_BclkSourceMclkDiv;
/*
* additional settings for bclk
* read the SDK header file for more details
*/
config.bitClock.bclkInputDelay = false;
/* frame sync default configurations */
#if defined(FSL_FEATURE_SAI_HAS_ON_DEMAND_MODE) && \
FSL_FEATURE_SAI_HAS_ON_DEMAND_MODE
config.frameSync.frameSyncGenerateOnDemand = false;
#endif
/* serial data default configurations */
#if defined(FSL_FEATURE_SAI_HAS_CHANNEL_MODE) && \
FSL_FEATURE_SAI_HAS_CHANNEL_MODE
config.serialData.dataMode = kSAI_DataPinStateOutputZero;
#endif
config.frameSync.frameSyncPolarity = kSAI_PolarityActiveLow;
config.bitClock.bclkSrcSwap = false;
/* format */
switch (i2s_cfg->format & I2S_FMT_DATA_FORMAT_MASK) {
case I2S_FMT_DATA_FORMAT_I2S:
SAI_GetClassicI2SConfig(&config, word_size_bits,
kSAI_Stereo,
dev_cfg->tx_channel);
break;
case I2S_FMT_DATA_FORMAT_LEFT_JUSTIFIED:
SAI_GetLeftJustifiedConfig(&config, word_size_bits,
kSAI_Stereo,
dev_cfg->tx_channel);
break;
case I2S_FMT_DATA_FORMAT_PCM_SHORT:
SAI_GetDSPConfig(&config, kSAI_FrameSyncLenOneBitClk,
word_size_bits, kSAI_Stereo,
dev_cfg->tx_channel);
break;
case I2S_FMT_DATA_FORMAT_PCM_LONG:
SAI_GetTDMConfig(&config, kSAI_FrameSyncLenPerWordWidth,
word_size_bits, num_words,
dev_cfg->tx_channel);
break;
default:
LOG_ERR("Unsupported I2S data format");
if (dir == I2S_DIR_TX) {
dev_data->tx.state = I2S_STATE_NOT_READY;
} else {
dev_data->rx.state = I2S_STATE_NOT_READY;
}
return -EINVAL;
}
/* sync mode configurations */
if (dir == I2S_DIR_TX) {
/* TX */
if (dev_cfg->tx_sync_mode) {
config.syncMode = kSAI_ModeSync;
} else {
config.syncMode = kSAI_ModeAsync;
}
} else {
/* RX */
if (dev_cfg->rx_sync_mode) {
config.syncMode = kSAI_ModeSync;
} else {
config.syncMode = kSAI_ModeAsync;
}
}
if (i2s_cfg->options & I2S_OPT_FRAME_CLK_SLAVE) {
if (i2s_cfg->options & I2S_OPT_BIT_CLK_SLAVE) {
config.masterSlave = kSAI_Slave;
} else {
config.masterSlave =
kSAI_Bclk_Master_FrameSync_Slave;
}
} else {
if (i2s_cfg->options & I2S_OPT_BIT_CLK_SLAVE) {
config.masterSlave =
kSAI_Bclk_Slave_FrameSync_Master;
} else {
config.masterSlave = kSAI_Master;
}
}
/* clock signal polarity */
switch (i2s_cfg->format & I2S_FMT_CLK_FORMAT_MASK) {
case I2S_FMT_CLK_NF_NB:
config.frameSync.frameSyncPolarity =
kSAI_PolarityActiveLow;
config.bitClock.bclkSrcSwap = false;
break;
case I2S_FMT_CLK_NF_IB:
config.frameSync.frameSyncPolarity =
kSAI_PolarityActiveLow;
config.bitClock.bclkSrcSwap = true;
break;
case I2S_FMT_CLK_IF_NB:
config.frameSync.frameSyncPolarity =
kSAI_PolarityActiveHigh;
config.bitClock.bclkSrcSwap = false;
break;
case I2S_FMT_CLK_IF_IB:
config.frameSync.frameSyncPolarity =
kSAI_PolarityActiveHigh;
config.bitClock.bclkSrcSwap = true;
break;
}
config.frameSync.frameSyncWidth = (uint8_t)word_size_bits;
if (dir == I2S_DIR_TX) {
memcpy(&dev_data->tx.cfg, i2s_cfg, sizeof(struct i2s_config));
LOG_DBG("tx slab free_list = 0x%x",
(uint32_t)i2s_cfg->mem_slab->free_list);
LOG_DBG("tx slab num_blocks = %d",
(uint32_t)i2s_cfg->mem_slab->info.num_blocks);
LOG_DBG("tx slab block_size = %d",
(uint32_t)i2s_cfg->mem_slab->info.block_size);
LOG_DBG("tx slab buffer = 0x%x",
(uint32_t)i2s_cfg->mem_slab->buffer);
/* set bit clock divider */
SAI_TxSetConfig(base, &config);
dev_data->tx.start_channel = config.startChannel;
/* Disable the channel FIFO */
base->TCR3 &= ~I2S_TCR3_TCE_MASK;
SAI_TxSetBitClockRate(base, mclk,
i2s_cfg->frame_clk_freq,
word_size_bits,
i2s_cfg->channels);
LOG_DBG("tx start_channel = %d", dev_data->tx.start_channel);
/*set up dma settings*/
dev_data->tx.dma_cfg.source_data_size = word_size_bits / 8;
dev_data->tx.dma_cfg.dest_data_size = word_size_bits / 8;
dev_data->tx.dma_cfg.source_burst_length =
i2s_cfg->word_size / 8;
dev_data->tx.dma_cfg.dest_burst_length =
i2s_cfg->word_size / 8;
dev_data->tx.dma_cfg.user_data = (void *)dev;
dev_data->tx.state = I2S_STATE_READY;
} else {
/* For RX, DMA reads from FIFO whenever data present */
config.fifo.fifoWatermark = 0;
memcpy(&dev_data->rx.cfg, i2s_cfg, sizeof(struct i2s_config));
LOG_DBG("rx slab free_list = 0x%x",
(uint32_t)i2s_cfg->mem_slab->free_list);
LOG_DBG("rx slab num_blocks = %d",
(uint32_t)i2s_cfg->mem_slab->info.num_blocks);
LOG_DBG("rx slab block_size = %d",
(uint32_t)i2s_cfg->mem_slab->info.block_size);
LOG_DBG("rx slab buffer = 0x%x",
(uint32_t)i2s_cfg->mem_slab->buffer);
/* set bit clock divider */
SAI_RxSetConfig(base, &config);
dev_data->rx.start_channel = config.startChannel;
SAI_RxSetBitClockRate(base, mclk,
i2s_cfg->frame_clk_freq,
word_size_bits,
i2s_cfg->channels);
LOG_DBG("rx start_channel = %d", dev_data->rx.start_channel);
/*set up dma settings*/
dev_data->rx.dma_cfg.source_data_size = word_size_bits / 8;
dev_data->rx.dma_cfg.dest_data_size = word_size_bits / 8;
dev_data->rx.dma_cfg.source_burst_length =
i2s_cfg->word_size / 8;
dev_data->rx.dma_cfg.dest_burst_length =
i2s_cfg->word_size / 8;
dev_data->rx.dma_cfg.user_data = (void *)dev;
dev_data->rx.state = I2S_STATE_READY;
}
return 0;
}
const struct i2s_config *i2s_mcux_config_get(const struct device *dev,
enum i2s_dir dir)
{
struct i2s_dev_data *dev_data = dev->data;
if (dir == I2S_DIR_RX) {
return &dev_data->rx.cfg;
}
return &dev_data->tx.cfg;
}
static int i2s_tx_stream_start(const struct device *dev)
{
int ret = 0;
void *buffer;
struct i2s_dev_data *dev_data = dev->data;
struct stream *strm = &dev_data->tx;
const struct device *dev_dma = dev_data->dev_dma;
const struct i2s_mcux_config *dev_cfg = dev->config;
I2S_Type *base = (I2S_Type *)dev_cfg->base;
/* retrieve buffer from input queue */
ret = k_msgq_get(&strm->in_queue, &buffer, K_NO_WAIT);
if (ret != 0) {
LOG_ERR("No buffer in input queue to start");
return -EIO;
}
LOG_DBG("tx stream start");
/* Driver keeps track of how many DMA blocks can be loaded to the DMA */
strm->free_tx_dma_blocks = MAX_TX_DMA_BLOCKS;
/* Configure the DMA with the first TX block */
struct dma_block_config *blk_cfg = &strm->dma_block;
memset(blk_cfg, 0, sizeof(struct dma_block_config));
uint32_t data_path = strm->start_channel;
blk_cfg->dest_address = (uint32_t)&base->TDR[data_path];
blk_cfg->source_address = (uint32_t)buffer;
blk_cfg->block_size = strm->cfg.block_size;
blk_cfg->dest_scatter_en = 1;
strm->dma_cfg.block_count = 1;
strm->dma_cfg.head_block = &strm->dma_block;
strm->dma_cfg.user_data = (void *)dev;
(strm->free_tx_dma_blocks)--;
dma_config(dev_dma, strm->dma_channel, &strm->dma_cfg);
/* put buffer in output queue */
ret = k_msgq_put(&strm->out_queue, &buffer, K_NO_WAIT);
if (ret != 0) {
LOG_ERR("failed to put buffer in output queue");
return ret;
}
uint8_t blocks_queued;
ret = i2s_tx_reload_multiple_dma_blocks(dev, &blocks_queued);
if (ret) {
LOG_ERR("i2s_tx_reload_multiple_dma_blocks() failed (%d)", ret);
return ret;
}
ret = dma_start(dev_dma, strm->dma_channel);
if (ret < 0) {
LOG_ERR("dma_start failed (%d)", ret);
return ret;
}
/* Enable DMA enable bit */
SAI_TxEnableDMA(base, kSAI_FIFORequestDMAEnable, true);
/* Enable the channel FIFO */
base->TCR3 |= I2S_TCR3_TCE(1UL << strm->start_channel);
/* Enable SAI Tx clock */
SAI_TxEnable(base, true);
return 0;
}
static int i2s_rx_stream_start(const struct device *dev)
{
int ret = 0;
void *buffer;
struct i2s_dev_data *dev_data = dev->data;
struct stream *strm = &dev_data->rx;
const struct device *dev_dma = dev_data->dev_dma;
const struct i2s_mcux_config *dev_cfg = dev->config;
I2S_Type *base = (I2S_Type *)dev_cfg->base;
uint8_t num_of_bufs;
num_of_bufs = k_mem_slab_num_free_get(strm->cfg.mem_slab);
/*
* Need at least NUM_DMA_BLOCKS_RX_PREP buffers on the RX memory slab
* for reliable DMA reception.
*/
if (num_of_bufs < NUM_DMA_BLOCKS_RX_PREP) {
return -EINVAL;
}
/* allocate 1st receive buffer from SLAB */
ret = k_mem_slab_alloc(strm->cfg.mem_slab, &buffer,
K_NO_WAIT);
if (ret != 0) {
LOG_DBG("buffer alloc from mem_slab failed (%d)", ret);
return ret;
}
/* Configure DMA block */
struct dma_block_config *blk_cfg = &strm->dma_block;
memset(blk_cfg, 0, sizeof(struct dma_block_config));
uint32_t data_path = strm->start_channel;
blk_cfg->dest_address = (uint32_t)buffer;
blk_cfg->source_address = (uint32_t)&base->RDR[data_path];
blk_cfg->block_size = strm->cfg.block_size;
blk_cfg->source_gather_en = 1;
strm->dma_cfg.block_count = 1;
strm->dma_cfg.head_block = &strm->dma_block;
strm->dma_cfg.user_data = (void *)dev;
dma_config(dev_dma, strm->dma_channel, &strm->dma_cfg);
/* put buffer in input queue */
ret = k_msgq_put(&strm->in_queue, &buffer, K_NO_WAIT);
if (ret != 0) {
LOG_ERR("failed to put buffer in input queue, ret1 %d", ret);
return ret;
}
/* prep DMA for each of remaining (NUM_DMA_BLOCKS_RX_PREP-1) buffers */
for (int i = 0; i < NUM_DMA_BLOCKS_RX_PREP - 1; i++) {
/* allocate receive buffer from SLAB */
ret = k_mem_slab_alloc(strm->cfg.mem_slab, &buffer,
K_NO_WAIT);
if (ret != 0) {
LOG_ERR("buffer alloc from mem_slab failed (%d)", ret);
return ret;
}
ret = dma_reload(dev_dma, strm->dma_channel,
(uint32_t)&base->RDR[data_path],
(uint32_t)buffer, blk_cfg->block_size);
if (ret != 0) {
LOG_ERR("dma_reload() failed with error 0x%x", ret);
return ret;
}
/* put buffer in input queue */
ret = k_msgq_put(&strm->in_queue, &buffer, K_NO_WAIT);
if (ret != 0) {
LOG_ERR("failed to put buffer in input queue, ret2 %d",
ret);
return ret;
}
}
LOG_DBG("Starting DMA Ch%u", strm->dma_channel);
ret = dma_start(dev_dma, strm->dma_channel);
if (ret < 0) {
LOG_ERR("Failed to start DMA Ch%d (%d)", strm->dma_channel,
ret);
return ret;
}
/* Enable DMA enable bit */
SAI_RxEnableDMA(base, kSAI_FIFORequestDMAEnable, true);
/* Enable the channel FIFO */
base->RCR3 |= I2S_RCR3_RCE(1UL << strm->start_channel);
/* Enable SAI Rx clock */
SAI_RxEnable(base, true);
return 0;
}
static int i2s_mcux_trigger(const struct device *dev, enum i2s_dir dir,
enum i2s_trigger_cmd cmd)
{
struct i2s_dev_data *dev_data = dev->data;
struct stream *strm;
unsigned int key;
int ret = 0;
if (dir == I2S_DIR_BOTH) {
return -ENOSYS;
}
strm = (dir == I2S_DIR_TX) ? &dev_data->tx : &dev_data->rx;
key = irq_lock();
switch (cmd) {
case I2S_TRIGGER_START:
if (strm->state != I2S_STATE_READY) {
LOG_ERR("START trigger: invalid state %u",
strm->state);
ret = -EIO;
break;
}
if (dir == I2S_DIR_TX) {
ret = i2s_tx_stream_start(dev);
} else {
ret = i2s_rx_stream_start(dev);
}
if (ret < 0) {
LOG_DBG("START trigger failed %d", ret);
ret = -EIO;
break;
}
strm->state = I2S_STATE_RUNNING;
strm->last_block = false;
break;
case I2S_TRIGGER_DROP:
if (strm->state == I2S_STATE_NOT_READY) {
LOG_ERR("DROP trigger: invalid state %d",
strm->state);
ret = -EIO;
break;
}
strm->state = I2S_STATE_READY;
if (dir == I2S_DIR_TX) {
i2s_tx_stream_disable(dev, true);
} else {
i2s_rx_stream_disable(dev, true, true);
}
break;
case I2S_TRIGGER_STOP:
if (strm->state != I2S_STATE_RUNNING) {
LOG_ERR("STOP trigger: invalid state %d", strm->state);
ret = -EIO;
break;
}
strm->state = I2S_STATE_STOPPING;
strm->last_block = true;
break;
case I2S_TRIGGER_DRAIN:
if (strm->state != I2S_STATE_RUNNING) {
LOG_ERR("DRAIN/STOP trigger: invalid state %d",
strm->state);
ret = -EIO;
break;
}
strm->state = I2S_STATE_STOPPING;
break;
case I2S_TRIGGER_PREPARE:
if (strm->state != I2S_STATE_ERROR) {
LOG_ERR("PREPARE trigger: invalid state %d",
strm->state);
ret = -EIO;
break;
}
strm->state = I2S_STATE_READY;
if (dir == I2S_DIR_TX) {
i2s_tx_stream_disable(dev, true);
} else {
i2s_rx_stream_disable(dev, true, true);
}
break;
default:
LOG_ERR("Unsupported trigger command");
ret = -EINVAL;
}
irq_unlock(key);
return ret;
}
static int i2s_mcux_read(const struct device *dev, void **mem_block,
size_t *size)
{
struct i2s_dev_data *dev_data = dev->data;
struct stream *strm = &dev_data->rx;
void *buffer;
int status, ret = 0;
LOG_DBG("i2s_mcux_read");
if (strm->state == I2S_STATE_NOT_READY) {
LOG_ERR("invalid state %d", strm->state);
return -EIO;
}
status = k_msgq_get(&strm->out_queue, &buffer,
SYS_TIMEOUT_MS(strm->cfg.timeout));
if (status != 0) {
if (strm->state == I2S_STATE_ERROR) {
ret = -EIO;
} else {
LOG_DBG("need retry");
ret = -EAGAIN;
}
return ret;
}
*mem_block = buffer;
*size = strm->cfg.block_size;
return 0;
}
static int i2s_mcux_write(const struct device *dev, void *mem_block,
size_t size)
{
struct i2s_dev_data *dev_data = dev->data;
struct stream *strm = &dev_data->tx;
int ret;
LOG_DBG("i2s_mcux_write");
if (strm->state != I2S_STATE_RUNNING &&
strm->state != I2S_STATE_READY) {
LOG_ERR("invalid state (%d)", strm->state);
return -EIO;
}
ret = k_msgq_put(&strm->in_queue, &mem_block,
SYS_TIMEOUT_MS(strm->cfg.timeout));
if (ret) {
LOG_DBG("k_msgq_put returned code %d", ret);
return ret;
}
return ret;
}
static void sai_driver_irq(const struct device *dev)
{
const struct i2s_mcux_config *dev_cfg = dev->config;
I2S_Type *base = (I2S_Type *)dev_cfg->base;
if ((base->TCSR & I2S_TCSR_FEF_MASK) == I2S_TCSR_FEF_MASK) {
/* Clear FIFO error flag to continue transfer */
SAI_TxClearStatusFlags(base, I2S_TCSR_FEF_MASK);
/* Reset FIFO for safety */
SAI_TxSoftwareReset(base, kSAI_ResetTypeFIFO);
LOG_DBG("sai tx error occurred");
}
if ((base->RCSR & I2S_RCSR_FEF_MASK) == I2S_RCSR_FEF_MASK) {
/* Clear FIFO error flag to continue transfer */
SAI_RxClearStatusFlags(base, I2S_RCSR_FEF_MASK);
/* Reset FIFO for safety */
SAI_RxSoftwareReset(base, kSAI_ResetTypeFIFO);
LOG_DBG("sai rx error occurred");
}
}
/* clear IRQ sources atm */
static void i2s_mcux_isr(void *arg)
{
struct device *dev = (struct device *)arg;
const struct i2s_mcux_config *dev_cfg = dev->config;
I2S_Type *base = (I2S_Type *)dev_cfg->base;
if ((base->RCSR & I2S_TCSR_FEF_MASK) == I2S_TCSR_FEF_MASK) {
sai_driver_irq(dev);
}
if ((base->TCSR & I2S_RCSR_FEF_MASK) == I2S_RCSR_FEF_MASK) {
sai_driver_irq(dev);
}
/*
* Add for ARM errata 838869, affects Cortex-M4,
* Cortex-M4F Store immediate overlapping exception return operation
* might vector to incorrect interrupt
*/
#if defined __CORTEX_M && (__CORTEX_M == 4U)
barrier_dsync_fence_full();
#endif
}
static void audio_clock_settings(const struct device *dev)
{
clock_audio_pll_config_t audioPllConfig;
const struct i2s_mcux_config *dev_cfg = dev->config;
uint32_t clock_name = (uint32_t) dev_cfg->clk_sub_sys;
/*Clock setting for SAI*/
imxrt_audio_codec_pll_init(clock_name, dev_cfg->clk_src,
dev_cfg->clk_pre_div, dev_cfg->clk_src_div);
#ifdef CONFIG_SOC_SERIES_IMX_RT11XX
audioPllConfig.loopDivider = dev_cfg->pll_lp;
audioPllConfig.postDivider = dev_cfg->pll_pd;
audioPllConfig.numerator = dev_cfg->pll_num;
audioPllConfig.denominator = dev_cfg->pll_den;
audioPllConfig.ssEnable = false;
#elif defined CONFIG_SOC_SERIES_IMX_RT10XX
audioPllConfig.src = dev_cfg->pll_src;
audioPllConfig.loopDivider = dev_cfg->pll_lp;
audioPllConfig.postDivider = dev_cfg->pll_pd;
audioPllConfig.numerator = dev_cfg->pll_num;
audioPllConfig.denominator = dev_cfg->pll_den;
#else
#error Initialize SOC Series-specific clock_audio_pll_config_t
#endif /* CONFIG_SOC_SERIES */
CLOCK_InitAudioPll(&audioPllConfig);
}
static int i2s_mcux_initialize(const struct device *dev)
{
const struct i2s_mcux_config *dev_cfg = dev->config;
I2S_Type *base = (I2S_Type *)dev_cfg->base;
struct i2s_dev_data *dev_data = dev->data;
uint32_t mclk;
int err;
if (!dev_data->dev_dma) {
LOG_ERR("DMA device not found");
return -ENODEV;
}
/* Initialize the buffer queues */
k_msgq_init(&dev_data->tx.in_queue, (char *)dev_data->tx_in_msgs,
sizeof(void *), CONFIG_I2S_TX_BLOCK_COUNT);
k_msgq_init(&dev_data->rx.in_queue, (char *)dev_data->rx_in_msgs,
sizeof(void *), CONFIG_I2S_RX_BLOCK_COUNT);
k_msgq_init(&dev_data->tx.out_queue, (char *)dev_data->tx_out_msgs,
sizeof(void *), CONFIG_I2S_TX_BLOCK_COUNT);
k_msgq_init(&dev_data->rx.out_queue, (char *)dev_data->rx_out_msgs,
sizeof(void *), CONFIG_I2S_RX_BLOCK_COUNT);
/* register ISR */
dev_cfg->irq_connect(dev);
/* pinctrl */
err = pinctrl_apply_state(dev_cfg->pinctrl, PINCTRL_STATE_DEFAULT);
if (err) {
LOG_ERR("mclk pinctrl setup failed (%d)", err);
return err;
}
/*clock configuration*/
audio_clock_settings(dev);
SAI_Init(base);
dev_data->tx.state = I2S_STATE_NOT_READY;
dev_data->rx.state = I2S_STATE_NOT_READY;
#if (defined(FSL_FEATURE_SAI_HAS_MCR) && (FSL_FEATURE_SAI_HAS_MCR)) || \
(defined(FSL_FEATURE_SAI_HAS_MCLKDIV_REGISTER) && \
(FSL_FEATURE_SAI_HAS_MCLKDIV_REGISTER))
sai_master_clock_t mclkConfig = {
#if defined(FSL_FEATURE_SAI_HAS_MCR) && (FSL_FEATURE_SAI_HAS_MCR)
.mclkOutputEnable = true,
#if !(defined(FSL_FEATURE_SAI_HAS_NO_MCR_MICS) && \
(FSL_FEATURE_SAI_HAS_NO_MCR_MICS))
.mclkSource = kSAI_MclkSourceSysclk,
#endif
#endif
};
#endif
get_mclk_rate(dev, &mclk);
/* master clock configurations */
#if (defined(FSL_FEATURE_SAI_HAS_MCR) && (FSL_FEATURE_SAI_HAS_MCR)) || \
(defined(FSL_FEATURE_SAI_HAS_MCLKDIV_REGISTER) && \
(FSL_FEATURE_SAI_HAS_MCLKDIV_REGISTER))
#if defined(FSL_FEATURE_SAI_HAS_MCLKDIV_REGISTER) && \
(FSL_FEATURE_SAI_HAS_MCLKDIV_REGISTER)
mclkConfig.mclkHz = mclk;
mclkConfig.mclkSourceClkHz = mclk;
#endif
SAI_SetMasterClockConfig(base, &mclkConfig);
#endif
LOG_INF("Device %s initialized", dev->name);
return 0;
}
static const struct i2s_driver_api i2s_mcux_driver_api = {
.configure = i2s_mcux_config,
.read = i2s_mcux_read,
.write = i2s_mcux_write,
.config_get = i2s_mcux_config_get,
.trigger = i2s_mcux_trigger,
};
#define I2S_MCUX_INIT(i2s_id) \
static void i2s_irq_connect_##i2s_id(const struct device *dev); \
\
PINCTRL_DT_INST_DEFINE(i2s_id); \
\
static const struct i2s_mcux_config i2s_##i2s_id##_config = { \
.base = (I2S_Type *)DT_INST_REG_ADDR(i2s_id), \
.clk_src = \
DT_CLOCKS_CELL_BY_IDX(DT_DRV_INST(i2s_id), \
0, bits), \
.clk_pre_div = DT_INST_PROP(i2s_id, pre_div), \
.clk_src_div = DT_INST_PROP(i2s_id, podf), \
.pll_src = \
DT_PHA_BY_NAME(DT_DRV_INST(i2s_id), \
pll_clocks, src, value), \
.pll_lp = \
DT_PHA_BY_NAME(DT_DRV_INST(i2s_id), \
pll_clocks, lp, value), \
.pll_pd = \
DT_PHA_BY_NAME(DT_DRV_INST(i2s_id), \
pll_clocks, pd, value), \
.pll_num = \
DT_PHA_BY_NAME(DT_DRV_INST(i2s_id), \
pll_clocks, num, value), \
.pll_den = \
DT_PHA_BY_NAME(DT_DRV_INST(i2s_id), \
pll_clocks, den, value), \
.mclk_pin_mask = \
DT_PHA_BY_IDX(DT_DRV_INST(i2s_id), \
pinmuxes, 0, function), \
.mclk_pin_offset = \
DT_PHA_BY_IDX(DT_DRV_INST(i2s_id), \
pinmuxes, 0, pin), \
.clk_sub_sys = (clock_control_subsys_t) \
DT_INST_CLOCKS_CELL_BY_IDX(i2s_id, 0, name), \
.ccm_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(i2s_id)), \
.irq_connect = i2s_irq_connect_##i2s_id, \
.pinctrl = PINCTRL_DT_INST_DEV_CONFIG_GET(i2s_id), \
.tx_sync_mode = \
DT_INST_PROP(i2s_id, nxp_tx_sync_mode), \
.rx_sync_mode = \
DT_INST_PROP(i2s_id, nxp_rx_sync_mode), \
.tx_channel = DT_INST_PROP(i2s_id, nxp_tx_channel), \
}; \
\
static struct i2s_dev_data i2s_##i2s_id##_data = { \
.dev_dma = DEVICE_DT_GET( \
DT_INST_DMAS_CTLR_BY_NAME(i2s_id, rx)), \
.tx = { \
.dma_channel = \
DT_INST_PROP(i2s_id, nxp_tx_dma_channel), \
.dma_cfg = { \
.source_burst_length = \
CONFIG_I2S_EDMA_BURST_SIZE, \
.dest_burst_length = \
CONFIG_I2S_EDMA_BURST_SIZE, \
.dma_callback = i2s_dma_tx_callback, \
.complete_callback_en = 1, \
.error_callback_en = 1, \
.block_count = 1, \
.head_block = \
&i2s_##i2s_id##_data.tx.dma_block, \
.channel_direction = MEMORY_TO_PERIPHERAL, \
.dma_slot = \
DT_INST_DMAS_CELL_BY_NAME(i2s_id, \
tx, source), \
}, \
}, \
.rx = { \
.dma_channel = \
DT_INST_PROP(i2s_id, nxp_rx_dma_channel), \
.dma_cfg = { \
.source_burst_length = \
CONFIG_I2S_EDMA_BURST_SIZE, \
.dest_burst_length = \
CONFIG_I2S_EDMA_BURST_SIZE, \
.dma_callback = i2s_dma_rx_callback, \
.complete_callback_en = 1, \
.error_callback_en = 1, \
.block_count = 1, \
.head_block = \
&i2s_##i2s_id##_data.rx.dma_block, \
.channel_direction = PERIPHERAL_TO_MEMORY, \
.dma_slot = \
DT_INST_DMAS_CELL_BY_NAME(i2s_id, \
rx, source), \
}, \
}, \
}; \
\
DEVICE_DT_INST_DEFINE(i2s_id, &i2s_mcux_initialize, NULL, \
&i2s_##i2s_id##_data, &i2s_##i2s_id##_config, \
POST_KERNEL, \
CONFIG_I2S_INIT_PRIORITY, &i2s_mcux_driver_api); \
\
static void i2s_irq_connect_##i2s_id(const struct device *dev) \
{ \
IRQ_CONNECT(DT_INST_IRQ_BY_IDX(i2s_id, 0, irq), \
DT_INST_IRQ_BY_IDX(i2s_id, 0, priority), \
i2s_mcux_isr, \
DEVICE_DT_INST_GET(i2s_id), 0); \
irq_enable(DT_INST_IRQN(i2s_id)); \
}
DT_INST_FOREACH_STATUS_OKAY(I2S_MCUX_INIT)
|