1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // Freescale ALSA SoC Digital Audio Interface (SAI) driver. 4 // 5 // Copyright 2012-2015 Freescale Semiconductor, Inc. 6 7 #include <linux/clk.h> 8 #include <linux/delay.h> 9 #include <linux/dmaengine.h> 10 #include <linux/module.h> 11 #include <linux/of_address.h> 12 #include <linux/of_device.h> 13 #include <linux/pm_qos.h> 14 #include <linux/pm_runtime.h> 15 #include <linux/regmap.h> 16 #include <linux/slab.h> 17 #include <linux/time.h> 18 #include <sound/core.h> 19 #include <sound/dmaengine_pcm.h> 20 #include <sound/pcm_params.h> 21 #include <linux/mfd/syscon.h> 22 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h> 23 24 #include "fsl_sai.h" 25 #include "imx-pcm.h" 26 27 #define FSL_SAI_FLAGS (FSL_SAI_CSR_SEIE |\ 28 FSL_SAI_CSR_FEIE) 29 30 static const unsigned int fsl_sai_rates[] = { 31 8000, 11025, 12000, 16000, 22050, 32 24000, 32000, 44100, 48000, 64000, 33 88200, 96000, 176400, 192000 34 }; 35 36 static const struct snd_pcm_hw_constraint_list fsl_sai_rate_constraints = { 37 .count = ARRAY_SIZE(fsl_sai_rates), 38 .list = fsl_sai_rates, 39 }; 40 41 /** 42 * fsl_sai_dir_is_synced - Check if stream is synced by the opposite stream 43 * 44 * SAI supports synchronous mode using bit/frame clocks of either Transmitter's 45 * or Receiver's for both streams. This function is used to check if clocks of 46 * the stream's are synced by the opposite stream. 47 * 48 * @sai: SAI context 49 * @dir: stream direction 50 */ 51 static inline bool fsl_sai_dir_is_synced(struct fsl_sai *sai, int dir) 52 { 53 int adir = (dir == TX) ? RX : TX; 54 55 /* current dir in async mode while opposite dir in sync mode */ 56 return !sai->synchronous[dir] && sai->synchronous[adir]; 57 } 58 59 static irqreturn_t fsl_sai_isr(int irq, void *devid) 60 { 61 struct fsl_sai *sai = (struct fsl_sai *)devid; 62 unsigned int ofs = sai->soc_data->reg_offset; 63 struct device *dev = &sai->pdev->dev; 64 u32 flags, xcsr, mask; 65 bool irq_none = true; 66 67 /* 68 * Both IRQ status bits and IRQ mask bits are in the xCSR but 69 * different shifts. And we here create a mask only for those 70 * IRQs that we activated. 71 */ 72 mask = (FSL_SAI_FLAGS >> FSL_SAI_CSR_xIE_SHIFT) << FSL_SAI_CSR_xF_SHIFT; 73 74 /* Tx IRQ */ 75 regmap_read(sai->regmap, FSL_SAI_TCSR(ofs), &xcsr); 76 flags = xcsr & mask; 77 78 if (flags) 79 irq_none = false; 80 else 81 goto irq_rx; 82 83 if (flags & FSL_SAI_CSR_WSF) 84 dev_dbg(dev, "isr: Start of Tx word detected\n"); 85 86 if (flags & FSL_SAI_CSR_SEF) 87 dev_dbg(dev, "isr: Tx Frame sync error detected\n"); 88 89 if (flags & FSL_SAI_CSR_FEF) { 90 dev_dbg(dev, "isr: Transmit underrun detected\n"); 91 /* FIFO reset for safety */ 92 xcsr |= FSL_SAI_CSR_FR; 93 } 94 95 if (flags & FSL_SAI_CSR_FWF) 96 dev_dbg(dev, "isr: Enabled transmit FIFO is empty\n"); 97 98 if (flags & FSL_SAI_CSR_FRF) 99 dev_dbg(dev, "isr: Transmit FIFO watermark has been reached\n"); 100 101 flags &= FSL_SAI_CSR_xF_W_MASK; 102 xcsr &= ~FSL_SAI_CSR_xF_MASK; 103 104 if (flags) 105 regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), flags | xcsr); 106 107 irq_rx: 108 /* Rx IRQ */ 109 regmap_read(sai->regmap, FSL_SAI_RCSR(ofs), &xcsr); 110 flags = xcsr & mask; 111 112 if (flags) 113 irq_none = false; 114 else 115 goto out; 116 117 if (flags & FSL_SAI_CSR_WSF) 118 dev_dbg(dev, "isr: Start of Rx word detected\n"); 119 120 if (flags & FSL_SAI_CSR_SEF) 121 dev_dbg(dev, "isr: Rx Frame sync error detected\n"); 122 123 if (flags & FSL_SAI_CSR_FEF) { 124 dev_dbg(dev, "isr: Receive overflow detected\n"); 125 /* FIFO reset for safety */ 126 xcsr |= FSL_SAI_CSR_FR; 127 } 128 129 if (flags & FSL_SAI_CSR_FWF) 130 dev_dbg(dev, "isr: Enabled receive FIFO is full\n"); 131 132 if (flags & FSL_SAI_CSR_FRF) 133 dev_dbg(dev, "isr: Receive FIFO watermark has been reached\n"); 134 135 flags &= FSL_SAI_CSR_xF_W_MASK; 136 xcsr &= ~FSL_SAI_CSR_xF_MASK; 137 138 if (flags) 139 regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), flags | xcsr); 140 141 out: 142 if (irq_none) 143 return IRQ_NONE; 144 else 145 return IRQ_HANDLED; 146 } 147 148 static int fsl_sai_set_dai_tdm_slot(struct snd_soc_dai *cpu_dai, u32 tx_mask, 149 u32 rx_mask, int slots, int slot_width) 150 { 151 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai); 152 153 sai->slots = slots; 154 sai->slot_width = slot_width; 155 156 return 0; 157 } 158 159 static int fsl_sai_set_dai_bclk_ratio(struct snd_soc_dai *dai, 160 unsigned int ratio) 161 { 162 struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai); 163 164 sai->bclk_ratio = ratio; 165 166 return 0; 167 } 168 169 static int fsl_sai_set_dai_sysclk_tr(struct snd_soc_dai *cpu_dai, 170 int clk_id, unsigned int freq, int fsl_dir) 171 { 172 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai); 173 unsigned int ofs = sai->soc_data->reg_offset; 174 bool tx = fsl_dir == FSL_FMT_TRANSMITTER; 175 u32 val_cr2 = 0; 176 177 switch (clk_id) { 178 case FSL_SAI_CLK_BUS: 179 val_cr2 |= FSL_SAI_CR2_MSEL_BUS; 180 break; 181 case FSL_SAI_CLK_MAST1: 182 val_cr2 |= FSL_SAI_CR2_MSEL_MCLK1; 183 break; 184 case FSL_SAI_CLK_MAST2: 185 val_cr2 |= FSL_SAI_CR2_MSEL_MCLK2; 186 break; 187 case FSL_SAI_CLK_MAST3: 188 val_cr2 |= FSL_SAI_CR2_MSEL_MCLK3; 189 break; 190 default: 191 return -EINVAL; 192 } 193 194 regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs), 195 FSL_SAI_CR2_MSEL_MASK, val_cr2); 196 197 return 0; 198 } 199 200 static int fsl_sai_set_dai_sysclk(struct snd_soc_dai *cpu_dai, 201 int clk_id, unsigned int freq, int dir) 202 { 203 int ret; 204 205 if (dir == SND_SOC_CLOCK_IN) 206 return 0; 207 208 ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq, 209 FSL_FMT_TRANSMITTER); 210 if (ret) { 211 dev_err(cpu_dai->dev, "Cannot set tx sysclk: %d\n", ret); 212 return ret; 213 } 214 215 ret = fsl_sai_set_dai_sysclk_tr(cpu_dai, clk_id, freq, 216 FSL_FMT_RECEIVER); 217 if (ret) 218 dev_err(cpu_dai->dev, "Cannot set rx sysclk: %d\n", ret); 219 220 return ret; 221 } 222 223 static int fsl_sai_set_dai_fmt_tr(struct snd_soc_dai *cpu_dai, 224 unsigned int fmt, int fsl_dir) 225 { 226 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai); 227 unsigned int ofs = sai->soc_data->reg_offset; 228 bool tx = fsl_dir == FSL_FMT_TRANSMITTER; 229 u32 val_cr2 = 0, val_cr4 = 0; 230 231 if (!sai->is_lsb_first) 232 val_cr4 |= FSL_SAI_CR4_MF; 233 234 /* DAI mode */ 235 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 236 case SND_SOC_DAIFMT_I2S: 237 /* 238 * Frame low, 1clk before data, one word length for frame sync, 239 * frame sync starts one serial clock cycle earlier, 240 * that is, together with the last bit of the previous 241 * data word. 242 */ 243 val_cr2 |= FSL_SAI_CR2_BCP; 244 val_cr4 |= FSL_SAI_CR4_FSE | FSL_SAI_CR4_FSP; 245 break; 246 case SND_SOC_DAIFMT_LEFT_J: 247 /* 248 * Frame high, one word length for frame sync, 249 * frame sync asserts with the first bit of the frame. 250 */ 251 val_cr2 |= FSL_SAI_CR2_BCP; 252 break; 253 case SND_SOC_DAIFMT_DSP_A: 254 /* 255 * Frame high, 1clk before data, one bit for frame sync, 256 * frame sync starts one serial clock cycle earlier, 257 * that is, together with the last bit of the previous 258 * data word. 259 */ 260 val_cr2 |= FSL_SAI_CR2_BCP; 261 val_cr4 |= FSL_SAI_CR4_FSE; 262 sai->is_dsp_mode = true; 263 break; 264 case SND_SOC_DAIFMT_DSP_B: 265 /* 266 * Frame high, one bit for frame sync, 267 * frame sync asserts with the first bit of the frame. 268 */ 269 val_cr2 |= FSL_SAI_CR2_BCP; 270 sai->is_dsp_mode = true; 271 break; 272 case SND_SOC_DAIFMT_RIGHT_J: 273 /* To be done */ 274 default: 275 return -EINVAL; 276 } 277 278 /* DAI clock inversion */ 279 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 280 case SND_SOC_DAIFMT_IB_IF: 281 /* Invert both clocks */ 282 val_cr2 ^= FSL_SAI_CR2_BCP; 283 val_cr4 ^= FSL_SAI_CR4_FSP; 284 break; 285 case SND_SOC_DAIFMT_IB_NF: 286 /* Invert bit clock */ 287 val_cr2 ^= FSL_SAI_CR2_BCP; 288 break; 289 case SND_SOC_DAIFMT_NB_IF: 290 /* Invert frame clock */ 291 val_cr4 ^= FSL_SAI_CR4_FSP; 292 break; 293 case SND_SOC_DAIFMT_NB_NF: 294 /* Nothing to do for both normal cases */ 295 break; 296 default: 297 return -EINVAL; 298 } 299 300 /* DAI clock master masks */ 301 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 302 case SND_SOC_DAIFMT_CBS_CFS: 303 val_cr2 |= FSL_SAI_CR2_BCD_MSTR; 304 val_cr4 |= FSL_SAI_CR4_FSD_MSTR; 305 sai->is_slave_mode = false; 306 break; 307 case SND_SOC_DAIFMT_CBM_CFM: 308 sai->is_slave_mode = true; 309 break; 310 case SND_SOC_DAIFMT_CBS_CFM: 311 val_cr2 |= FSL_SAI_CR2_BCD_MSTR; 312 sai->is_slave_mode = false; 313 break; 314 case SND_SOC_DAIFMT_CBM_CFS: 315 val_cr4 |= FSL_SAI_CR4_FSD_MSTR; 316 sai->is_slave_mode = true; 317 break; 318 default: 319 return -EINVAL; 320 } 321 322 regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs), 323 FSL_SAI_CR2_BCP | FSL_SAI_CR2_BCD_MSTR, val_cr2); 324 regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs), 325 FSL_SAI_CR4_MF | FSL_SAI_CR4_FSE | 326 FSL_SAI_CR4_FSP | FSL_SAI_CR4_FSD_MSTR, val_cr4); 327 328 return 0; 329 } 330 331 static int fsl_sai_set_dai_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt) 332 { 333 int ret; 334 335 ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_TRANSMITTER); 336 if (ret) { 337 dev_err(cpu_dai->dev, "Cannot set tx format: %d\n", ret); 338 return ret; 339 } 340 341 ret = fsl_sai_set_dai_fmt_tr(cpu_dai, fmt, FSL_FMT_RECEIVER); 342 if (ret) 343 dev_err(cpu_dai->dev, "Cannot set rx format: %d\n", ret); 344 345 return ret; 346 } 347 348 static int fsl_sai_set_bclk(struct snd_soc_dai *dai, bool tx, u32 freq) 349 { 350 struct fsl_sai *sai = snd_soc_dai_get_drvdata(dai); 351 unsigned int ofs = sai->soc_data->reg_offset; 352 unsigned long clk_rate; 353 u32 savediv = 0, ratio, savesub = freq; 354 int adir = tx ? RX : TX; 355 int dir = tx ? TX : RX; 356 u32 id; 357 int ret = 0; 358 359 /* Don't apply to slave mode */ 360 if (sai->is_slave_mode) 361 return 0; 362 363 /* 364 * There is no point in polling MCLK0 if it is identical to MCLK1. 365 * And given that MQS use case has to use MCLK1 though two clocks 366 * are the same, we simply skip MCLK0 and start to find from MCLK1. 367 */ 368 id = sai->soc_data->mclk0_is_mclk1 ? 1 : 0; 369 370 for (; id < FSL_SAI_MCLK_MAX; id++) { 371 clk_rate = clk_get_rate(sai->mclk_clk[id]); 372 if (!clk_rate) 373 continue; 374 375 ratio = clk_rate / freq; 376 377 ret = clk_rate - ratio * freq; 378 379 /* 380 * Drop the source that can not be 381 * divided into the required rate. 382 */ 383 if (ret != 0 && clk_rate / ret < 1000) 384 continue; 385 386 dev_dbg(dai->dev, 387 "ratio %d for freq %dHz based on clock %ldHz\n", 388 ratio, freq, clk_rate); 389 390 if (ratio % 2 == 0 && ratio >= 2 && ratio <= 512) 391 ratio /= 2; 392 else 393 continue; 394 395 if (ret < savesub) { 396 savediv = ratio; 397 sai->mclk_id[tx] = id; 398 savesub = ret; 399 } 400 401 if (ret == 0) 402 break; 403 } 404 405 if (savediv == 0) { 406 dev_err(dai->dev, "failed to derive required %cx rate: %d\n", 407 tx ? 'T' : 'R', freq); 408 return -EINVAL; 409 } 410 411 /* 412 * 1) For Asynchronous mode, we must set RCR2 register for capture, and 413 * set TCR2 register for playback. 414 * 2) For Tx sync with Rx clock, we must set RCR2 register for playback 415 * and capture. 416 * 3) For Rx sync with Tx clock, we must set TCR2 register for playback 417 * and capture. 418 * 4) For Tx and Rx are both Synchronous with another SAI, we just 419 * ignore it. 420 */ 421 if (fsl_sai_dir_is_synced(sai, adir)) { 422 regmap_update_bits(sai->regmap, FSL_SAI_xCR2(!tx, ofs), 423 FSL_SAI_CR2_MSEL_MASK, 424 FSL_SAI_CR2_MSEL(sai->mclk_id[tx])); 425 regmap_update_bits(sai->regmap, FSL_SAI_xCR2(!tx, ofs), 426 FSL_SAI_CR2_DIV_MASK, savediv - 1); 427 } else if (!sai->synchronous[dir]) { 428 regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs), 429 FSL_SAI_CR2_MSEL_MASK, 430 FSL_SAI_CR2_MSEL(sai->mclk_id[tx])); 431 regmap_update_bits(sai->regmap, FSL_SAI_xCR2(tx, ofs), 432 FSL_SAI_CR2_DIV_MASK, savediv - 1); 433 } 434 435 dev_dbg(dai->dev, "best fit: clock id=%d, div=%d, deviation =%d\n", 436 sai->mclk_id[tx], savediv, savesub); 437 438 return 0; 439 } 440 441 static int fsl_sai_hw_params(struct snd_pcm_substream *substream, 442 struct snd_pcm_hw_params *params, 443 struct snd_soc_dai *cpu_dai) 444 { 445 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai); 446 unsigned int ofs = sai->soc_data->reg_offset; 447 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 448 unsigned int channels = params_channels(params); 449 u32 word_width = params_width(params); 450 u32 val_cr4 = 0, val_cr5 = 0; 451 u32 slots = (channels == 1) ? 2 : channels; 452 u32 slot_width = word_width; 453 int adir = tx ? RX : TX; 454 u32 pins; 455 int ret; 456 457 if (sai->slots) 458 slots = sai->slots; 459 460 if (sai->slot_width) 461 slot_width = sai->slot_width; 462 463 pins = DIV_ROUND_UP(channels, slots); 464 465 if (!sai->is_slave_mode) { 466 if (sai->bclk_ratio) 467 ret = fsl_sai_set_bclk(cpu_dai, tx, 468 sai->bclk_ratio * 469 params_rate(params)); 470 else 471 ret = fsl_sai_set_bclk(cpu_dai, tx, 472 slots * slot_width * 473 params_rate(params)); 474 if (ret) 475 return ret; 476 477 /* Do not enable the clock if it is already enabled */ 478 if (!(sai->mclk_streams & BIT(substream->stream))) { 479 ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[tx]]); 480 if (ret) 481 return ret; 482 483 sai->mclk_streams |= BIT(substream->stream); 484 } 485 } 486 487 if (!sai->is_dsp_mode) 488 val_cr4 |= FSL_SAI_CR4_SYWD(slot_width); 489 490 val_cr5 |= FSL_SAI_CR5_WNW(slot_width); 491 val_cr5 |= FSL_SAI_CR5_W0W(slot_width); 492 493 if (sai->is_lsb_first) 494 val_cr5 |= FSL_SAI_CR5_FBT(0); 495 else 496 val_cr5 |= FSL_SAI_CR5_FBT(word_width - 1); 497 498 val_cr4 |= FSL_SAI_CR4_FRSZ(slots); 499 500 /* Set to output mode to avoid tri-stated data pins */ 501 if (tx) 502 val_cr4 |= FSL_SAI_CR4_CHMOD; 503 504 /* 505 * For SAI master mode, when Tx(Rx) sync with Rx(Tx) clock, Rx(Tx) will 506 * generate bclk and frame clock for Tx(Rx), we should set RCR4(TCR4), 507 * RCR5(TCR5) for playback(capture), or there will be sync error. 508 */ 509 510 if (!sai->is_slave_mode && fsl_sai_dir_is_synced(sai, adir)) { 511 regmap_update_bits(sai->regmap, FSL_SAI_xCR4(!tx, ofs), 512 FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK | 513 FSL_SAI_CR4_CHMOD_MASK, 514 val_cr4); 515 regmap_update_bits(sai->regmap, FSL_SAI_xCR5(!tx, ofs), 516 FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK | 517 FSL_SAI_CR5_FBT_MASK, val_cr5); 518 } 519 520 regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx, ofs), 521 FSL_SAI_CR3_TRCE_MASK, 522 FSL_SAI_CR3_TRCE((1 << pins) - 1)); 523 regmap_update_bits(sai->regmap, FSL_SAI_xCR4(tx, ofs), 524 FSL_SAI_CR4_SYWD_MASK | FSL_SAI_CR4_FRSZ_MASK | 525 FSL_SAI_CR4_CHMOD_MASK, 526 val_cr4); 527 regmap_update_bits(sai->regmap, FSL_SAI_xCR5(tx, ofs), 528 FSL_SAI_CR5_WNW_MASK | FSL_SAI_CR5_W0W_MASK | 529 FSL_SAI_CR5_FBT_MASK, val_cr5); 530 regmap_write(sai->regmap, FSL_SAI_xMR(tx), 531 ~0UL - ((1 << min(channels, slots)) - 1)); 532 533 return 0; 534 } 535 536 static int fsl_sai_hw_free(struct snd_pcm_substream *substream, 537 struct snd_soc_dai *cpu_dai) 538 { 539 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai); 540 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 541 unsigned int ofs = sai->soc_data->reg_offset; 542 543 regmap_update_bits(sai->regmap, FSL_SAI_xCR3(tx, ofs), 544 FSL_SAI_CR3_TRCE_MASK, 0); 545 546 if (!sai->is_slave_mode && 547 sai->mclk_streams & BIT(substream->stream)) { 548 clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[tx]]); 549 sai->mclk_streams &= ~BIT(substream->stream); 550 } 551 552 return 0; 553 } 554 555 static void fsl_sai_config_disable(struct fsl_sai *sai, int dir) 556 { 557 unsigned int ofs = sai->soc_data->reg_offset; 558 bool tx = dir == TX; 559 u32 xcsr, count = 100; 560 561 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs), 562 FSL_SAI_CSR_TERE, 0); 563 564 /* TERE will remain set till the end of current frame */ 565 do { 566 udelay(10); 567 regmap_read(sai->regmap, FSL_SAI_xCSR(tx, ofs), &xcsr); 568 } while (--count && xcsr & FSL_SAI_CSR_TERE); 569 570 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs), 571 FSL_SAI_CSR_FR, FSL_SAI_CSR_FR); 572 573 /* 574 * For sai master mode, after several open/close sai, 575 * there will be no frame clock, and can't recover 576 * anymore. Add software reset to fix this issue. 577 * This is a hardware bug, and will be fix in the 578 * next sai version. 579 */ 580 if (!sai->is_slave_mode) { 581 /* Software Reset */ 582 regmap_write(sai->regmap, FSL_SAI_xCSR(tx, ofs), FSL_SAI_CSR_SR); 583 /* Clear SR bit to finish the reset */ 584 regmap_write(sai->regmap, FSL_SAI_xCSR(tx, ofs), 0); 585 } 586 } 587 588 static int fsl_sai_trigger(struct snd_pcm_substream *substream, int cmd, 589 struct snd_soc_dai *cpu_dai) 590 { 591 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai); 592 unsigned int ofs = sai->soc_data->reg_offset; 593 594 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 595 int adir = tx ? RX : TX; 596 int dir = tx ? TX : RX; 597 u32 xcsr; 598 599 /* 600 * Asynchronous mode: Clear SYNC for both Tx and Rx. 601 * Rx sync with Tx clocks: Clear SYNC for Tx, set it for Rx. 602 * Tx sync with Rx clocks: Clear SYNC for Rx, set it for Tx. 603 */ 604 regmap_update_bits(sai->regmap, FSL_SAI_TCR2(ofs), FSL_SAI_CR2_SYNC, 605 sai->synchronous[TX] ? FSL_SAI_CR2_SYNC : 0); 606 regmap_update_bits(sai->regmap, FSL_SAI_RCR2(ofs), FSL_SAI_CR2_SYNC, 607 sai->synchronous[RX] ? FSL_SAI_CR2_SYNC : 0); 608 609 /* 610 * It is recommended that the transmitter is the last enabled 611 * and the first disabled. 612 */ 613 switch (cmd) { 614 case SNDRV_PCM_TRIGGER_START: 615 case SNDRV_PCM_TRIGGER_RESUME: 616 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 617 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs), 618 FSL_SAI_CSR_FRDE, FSL_SAI_CSR_FRDE); 619 620 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs), 621 FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE); 622 /* 623 * Enable the opposite direction for synchronous mode 624 * 1. Tx sync with Rx: only set RE for Rx; set TE & RE for Tx 625 * 2. Rx sync with Tx: only set TE for Tx; set RE & TE for Rx 626 * 627 * RM recommends to enable RE after TE for case 1 and to enable 628 * TE after RE for case 2, but we here may not always guarantee 629 * that happens: "arecord 1.wav; aplay 2.wav" in case 1 enables 630 * TE after RE, which is against what RM recommends but should 631 * be safe to do, judging by years of testing results. 632 */ 633 if (fsl_sai_dir_is_synced(sai, adir)) 634 regmap_update_bits(sai->regmap, FSL_SAI_xCSR((!tx), ofs), 635 FSL_SAI_CSR_TERE, FSL_SAI_CSR_TERE); 636 637 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs), 638 FSL_SAI_CSR_xIE_MASK, FSL_SAI_FLAGS); 639 break; 640 case SNDRV_PCM_TRIGGER_STOP: 641 case SNDRV_PCM_TRIGGER_SUSPEND: 642 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 643 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs), 644 FSL_SAI_CSR_FRDE, 0); 645 regmap_update_bits(sai->regmap, FSL_SAI_xCSR(tx, ofs), 646 FSL_SAI_CSR_xIE_MASK, 0); 647 648 /* Check if the opposite FRDE is also disabled */ 649 regmap_read(sai->regmap, FSL_SAI_xCSR(!tx, ofs), &xcsr); 650 651 /* 652 * If opposite stream provides clocks for synchronous mode and 653 * it is inactive, disable it before disabling the current one 654 */ 655 if (fsl_sai_dir_is_synced(sai, adir) && !(xcsr & FSL_SAI_CSR_FRDE)) 656 fsl_sai_config_disable(sai, adir); 657 658 /* 659 * Disable current stream if either of: 660 * 1. current stream doesn't provide clocks for synchronous mode 661 * 2. current stream provides clocks for synchronous mode but no 662 * more stream is active. 663 */ 664 if (!fsl_sai_dir_is_synced(sai, dir) || !(xcsr & FSL_SAI_CSR_FRDE)) 665 fsl_sai_config_disable(sai, dir); 666 667 break; 668 default: 669 return -EINVAL; 670 } 671 672 return 0; 673 } 674 675 static int fsl_sai_startup(struct snd_pcm_substream *substream, 676 struct snd_soc_dai *cpu_dai) 677 { 678 struct fsl_sai *sai = snd_soc_dai_get_drvdata(cpu_dai); 679 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 680 int ret; 681 682 /* 683 * EDMA controller needs period size to be a multiple of 684 * tx/rx maxburst 685 */ 686 if (sai->soc_data->use_edma) 687 snd_pcm_hw_constraint_step(substream->runtime, 0, 688 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 689 tx ? sai->dma_params_tx.maxburst : 690 sai->dma_params_rx.maxburst); 691 692 ret = snd_pcm_hw_constraint_list(substream->runtime, 0, 693 SNDRV_PCM_HW_PARAM_RATE, &fsl_sai_rate_constraints); 694 695 return ret; 696 } 697 698 static const struct snd_soc_dai_ops fsl_sai_pcm_dai_ops = { 699 .set_bclk_ratio = fsl_sai_set_dai_bclk_ratio, 700 .set_sysclk = fsl_sai_set_dai_sysclk, 701 .set_fmt = fsl_sai_set_dai_fmt, 702 .set_tdm_slot = fsl_sai_set_dai_tdm_slot, 703 .hw_params = fsl_sai_hw_params, 704 .hw_free = fsl_sai_hw_free, 705 .trigger = fsl_sai_trigger, 706 .startup = fsl_sai_startup, 707 }; 708 709 static int fsl_sai_dai_probe(struct snd_soc_dai *cpu_dai) 710 { 711 struct fsl_sai *sai = dev_get_drvdata(cpu_dai->dev); 712 unsigned int ofs = sai->soc_data->reg_offset; 713 714 /* Software Reset for both Tx and Rx */ 715 regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), FSL_SAI_CSR_SR); 716 regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), FSL_SAI_CSR_SR); 717 /* Clear SR bit to finish the reset */ 718 regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), 0); 719 regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), 0); 720 721 regmap_update_bits(sai->regmap, FSL_SAI_TCR1(ofs), 722 FSL_SAI_CR1_RFW_MASK(sai->soc_data->fifo_depth), 723 sai->soc_data->fifo_depth - FSL_SAI_MAXBURST_TX); 724 regmap_update_bits(sai->regmap, FSL_SAI_RCR1(ofs), 725 FSL_SAI_CR1_RFW_MASK(sai->soc_data->fifo_depth), 726 FSL_SAI_MAXBURST_RX - 1); 727 728 snd_soc_dai_init_dma_data(cpu_dai, &sai->dma_params_tx, 729 &sai->dma_params_rx); 730 731 snd_soc_dai_set_drvdata(cpu_dai, sai); 732 733 return 0; 734 } 735 736 static struct snd_soc_dai_driver fsl_sai_dai_template = { 737 .probe = fsl_sai_dai_probe, 738 .playback = { 739 .stream_name = "CPU-Playback", 740 .channels_min = 1, 741 .channels_max = 32, 742 .rate_min = 8000, 743 .rate_max = 192000, 744 .rates = SNDRV_PCM_RATE_KNOT, 745 .formats = FSL_SAI_FORMATS, 746 }, 747 .capture = { 748 .stream_name = "CPU-Capture", 749 .channels_min = 1, 750 .channels_max = 32, 751 .rate_min = 8000, 752 .rate_max = 192000, 753 .rates = SNDRV_PCM_RATE_KNOT, 754 .formats = FSL_SAI_FORMATS, 755 }, 756 .ops = &fsl_sai_pcm_dai_ops, 757 }; 758 759 static const struct snd_soc_component_driver fsl_component = { 760 .name = "fsl-sai", 761 }; 762 763 static struct reg_default fsl_sai_reg_defaults_ofs0[] = { 764 {FSL_SAI_TCR1(0), 0}, 765 {FSL_SAI_TCR2(0), 0}, 766 {FSL_SAI_TCR3(0), 0}, 767 {FSL_SAI_TCR4(0), 0}, 768 {FSL_SAI_TCR5(0), 0}, 769 {FSL_SAI_TDR0, 0}, 770 {FSL_SAI_TDR1, 0}, 771 {FSL_SAI_TDR2, 0}, 772 {FSL_SAI_TDR3, 0}, 773 {FSL_SAI_TDR4, 0}, 774 {FSL_SAI_TDR5, 0}, 775 {FSL_SAI_TDR6, 0}, 776 {FSL_SAI_TDR7, 0}, 777 {FSL_SAI_TMR, 0}, 778 {FSL_SAI_RCR1(0), 0}, 779 {FSL_SAI_RCR2(0), 0}, 780 {FSL_SAI_RCR3(0), 0}, 781 {FSL_SAI_RCR4(0), 0}, 782 {FSL_SAI_RCR5(0), 0}, 783 {FSL_SAI_RMR, 0}, 784 }; 785 786 static struct reg_default fsl_sai_reg_defaults_ofs8[] = { 787 {FSL_SAI_TCR1(8), 0}, 788 {FSL_SAI_TCR2(8), 0}, 789 {FSL_SAI_TCR3(8), 0}, 790 {FSL_SAI_TCR4(8), 0}, 791 {FSL_SAI_TCR5(8), 0}, 792 {FSL_SAI_TDR0, 0}, 793 {FSL_SAI_TDR1, 0}, 794 {FSL_SAI_TDR2, 0}, 795 {FSL_SAI_TDR3, 0}, 796 {FSL_SAI_TDR4, 0}, 797 {FSL_SAI_TDR5, 0}, 798 {FSL_SAI_TDR6, 0}, 799 {FSL_SAI_TDR7, 0}, 800 {FSL_SAI_TMR, 0}, 801 {FSL_SAI_RCR1(8), 0}, 802 {FSL_SAI_RCR2(8), 0}, 803 {FSL_SAI_RCR3(8), 0}, 804 {FSL_SAI_RCR4(8), 0}, 805 {FSL_SAI_RCR5(8), 0}, 806 {FSL_SAI_RMR, 0}, 807 {FSL_SAI_MCTL, 0}, 808 {FSL_SAI_MDIV, 0}, 809 }; 810 811 static bool fsl_sai_readable_reg(struct device *dev, unsigned int reg) 812 { 813 struct fsl_sai *sai = dev_get_drvdata(dev); 814 unsigned int ofs = sai->soc_data->reg_offset; 815 816 if (reg >= FSL_SAI_TCSR(ofs) && reg <= FSL_SAI_TCR5(ofs)) 817 return true; 818 819 if (reg >= FSL_SAI_RCSR(ofs) && reg <= FSL_SAI_RCR5(ofs)) 820 return true; 821 822 switch (reg) { 823 case FSL_SAI_TFR0: 824 case FSL_SAI_TFR1: 825 case FSL_SAI_TFR2: 826 case FSL_SAI_TFR3: 827 case FSL_SAI_TFR4: 828 case FSL_SAI_TFR5: 829 case FSL_SAI_TFR6: 830 case FSL_SAI_TFR7: 831 case FSL_SAI_TMR: 832 case FSL_SAI_RDR0: 833 case FSL_SAI_RDR1: 834 case FSL_SAI_RDR2: 835 case FSL_SAI_RDR3: 836 case FSL_SAI_RDR4: 837 case FSL_SAI_RDR5: 838 case FSL_SAI_RDR6: 839 case FSL_SAI_RDR7: 840 case FSL_SAI_RFR0: 841 case FSL_SAI_RFR1: 842 case FSL_SAI_RFR2: 843 case FSL_SAI_RFR3: 844 case FSL_SAI_RFR4: 845 case FSL_SAI_RFR5: 846 case FSL_SAI_RFR6: 847 case FSL_SAI_RFR7: 848 case FSL_SAI_RMR: 849 case FSL_SAI_MCTL: 850 case FSL_SAI_MDIV: 851 case FSL_SAI_VERID: 852 case FSL_SAI_PARAM: 853 case FSL_SAI_TTCTN: 854 case FSL_SAI_RTCTN: 855 case FSL_SAI_TTCTL: 856 case FSL_SAI_TBCTN: 857 case FSL_SAI_TTCAP: 858 case FSL_SAI_RTCTL: 859 case FSL_SAI_RBCTN: 860 case FSL_SAI_RTCAP: 861 return true; 862 default: 863 return false; 864 } 865 } 866 867 static bool fsl_sai_volatile_reg(struct device *dev, unsigned int reg) 868 { 869 struct fsl_sai *sai = dev_get_drvdata(dev); 870 unsigned int ofs = sai->soc_data->reg_offset; 871 872 if (reg == FSL_SAI_TCSR(ofs) || reg == FSL_SAI_RCSR(ofs)) 873 return true; 874 875 /* Set VERID and PARAM be volatile for reading value in probe */ 876 if (ofs == 8 && (reg == FSL_SAI_VERID || reg == FSL_SAI_PARAM)) 877 return true; 878 879 switch (reg) { 880 case FSL_SAI_TFR0: 881 case FSL_SAI_TFR1: 882 case FSL_SAI_TFR2: 883 case FSL_SAI_TFR3: 884 case FSL_SAI_TFR4: 885 case FSL_SAI_TFR5: 886 case FSL_SAI_TFR6: 887 case FSL_SAI_TFR7: 888 case FSL_SAI_RFR0: 889 case FSL_SAI_RFR1: 890 case FSL_SAI_RFR2: 891 case FSL_SAI_RFR3: 892 case FSL_SAI_RFR4: 893 case FSL_SAI_RFR5: 894 case FSL_SAI_RFR6: 895 case FSL_SAI_RFR7: 896 case FSL_SAI_RDR0: 897 case FSL_SAI_RDR1: 898 case FSL_SAI_RDR2: 899 case FSL_SAI_RDR3: 900 case FSL_SAI_RDR4: 901 case FSL_SAI_RDR5: 902 case FSL_SAI_RDR6: 903 case FSL_SAI_RDR7: 904 return true; 905 default: 906 return false; 907 } 908 } 909 910 static bool fsl_sai_writeable_reg(struct device *dev, unsigned int reg) 911 { 912 struct fsl_sai *sai = dev_get_drvdata(dev); 913 unsigned int ofs = sai->soc_data->reg_offset; 914 915 if (reg >= FSL_SAI_TCSR(ofs) && reg <= FSL_SAI_TCR5(ofs)) 916 return true; 917 918 if (reg >= FSL_SAI_RCSR(ofs) && reg <= FSL_SAI_RCR5(ofs)) 919 return true; 920 921 switch (reg) { 922 case FSL_SAI_TDR0: 923 case FSL_SAI_TDR1: 924 case FSL_SAI_TDR2: 925 case FSL_SAI_TDR3: 926 case FSL_SAI_TDR4: 927 case FSL_SAI_TDR5: 928 case FSL_SAI_TDR6: 929 case FSL_SAI_TDR7: 930 case FSL_SAI_TMR: 931 case FSL_SAI_RMR: 932 case FSL_SAI_MCTL: 933 case FSL_SAI_MDIV: 934 case FSL_SAI_TTCTL: 935 case FSL_SAI_RTCTL: 936 return true; 937 default: 938 return false; 939 } 940 } 941 942 static struct regmap_config fsl_sai_regmap_config = { 943 .reg_bits = 32, 944 .reg_stride = 4, 945 .val_bits = 32, 946 .fast_io = true, 947 948 .max_register = FSL_SAI_RMR, 949 .reg_defaults = fsl_sai_reg_defaults_ofs0, 950 .num_reg_defaults = ARRAY_SIZE(fsl_sai_reg_defaults_ofs0), 951 .readable_reg = fsl_sai_readable_reg, 952 .volatile_reg = fsl_sai_volatile_reg, 953 .writeable_reg = fsl_sai_writeable_reg, 954 .cache_type = REGCACHE_FLAT, 955 }; 956 957 static int fsl_sai_check_version(struct device *dev) 958 { 959 struct fsl_sai *sai = dev_get_drvdata(dev); 960 unsigned char ofs = sai->soc_data->reg_offset; 961 unsigned int val; 962 int ret; 963 964 if (FSL_SAI_TCSR(ofs) == FSL_SAI_VERID) 965 return 0; 966 967 ret = regmap_read(sai->regmap, FSL_SAI_VERID, &val); 968 if (ret < 0) 969 return ret; 970 971 dev_dbg(dev, "VERID: 0x%016X\n", val); 972 973 sai->verid.major = (val & FSL_SAI_VERID_MAJOR_MASK) >> 974 FSL_SAI_VERID_MAJOR_SHIFT; 975 sai->verid.minor = (val & FSL_SAI_VERID_MINOR_MASK) >> 976 FSL_SAI_VERID_MINOR_SHIFT; 977 sai->verid.feature = val & FSL_SAI_VERID_FEATURE_MASK; 978 979 ret = regmap_read(sai->regmap, FSL_SAI_PARAM, &val); 980 if (ret < 0) 981 return ret; 982 983 dev_dbg(dev, "PARAM: 0x%016X\n", val); 984 985 /* Max slots per frame, power of 2 */ 986 sai->param.slot_num = 1 << 987 ((val & FSL_SAI_PARAM_SPF_MASK) >> FSL_SAI_PARAM_SPF_SHIFT); 988 989 /* Words per fifo, power of 2 */ 990 sai->param.fifo_depth = 1 << 991 ((val & FSL_SAI_PARAM_WPF_MASK) >> FSL_SAI_PARAM_WPF_SHIFT); 992 993 /* Number of datalines implemented */ 994 sai->param.dataline = val & FSL_SAI_PARAM_DLN_MASK; 995 996 return 0; 997 } 998 999 static int fsl_sai_probe(struct platform_device *pdev) 1000 { 1001 struct device_node *np = pdev->dev.of_node; 1002 struct fsl_sai *sai; 1003 struct regmap *gpr; 1004 struct resource *res; 1005 void __iomem *base; 1006 char tmp[8]; 1007 int irq, ret, i; 1008 int index; 1009 1010 sai = devm_kzalloc(&pdev->dev, sizeof(*sai), GFP_KERNEL); 1011 if (!sai) 1012 return -ENOMEM; 1013 1014 sai->pdev = pdev; 1015 sai->soc_data = of_device_get_match_data(&pdev->dev); 1016 1017 sai->is_lsb_first = of_property_read_bool(np, "lsb-first"); 1018 1019 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1020 base = devm_ioremap_resource(&pdev->dev, res); 1021 if (IS_ERR(base)) 1022 return PTR_ERR(base); 1023 1024 if (sai->soc_data->reg_offset == 8) { 1025 fsl_sai_regmap_config.reg_defaults = fsl_sai_reg_defaults_ofs8; 1026 fsl_sai_regmap_config.max_register = FSL_SAI_MDIV; 1027 fsl_sai_regmap_config.num_reg_defaults = 1028 ARRAY_SIZE(fsl_sai_reg_defaults_ofs8); 1029 } 1030 1031 sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev, 1032 "bus", base, &fsl_sai_regmap_config); 1033 1034 /* Compatible with old DTB cases */ 1035 if (IS_ERR(sai->regmap) && PTR_ERR(sai->regmap) != -EPROBE_DEFER) 1036 sai->regmap = devm_regmap_init_mmio_clk(&pdev->dev, 1037 "sai", base, &fsl_sai_regmap_config); 1038 if (IS_ERR(sai->regmap)) { 1039 dev_err(&pdev->dev, "regmap init failed\n"); 1040 return PTR_ERR(sai->regmap); 1041 } 1042 1043 /* No error out for old DTB cases but only mark the clock NULL */ 1044 sai->bus_clk = devm_clk_get(&pdev->dev, "bus"); 1045 if (IS_ERR(sai->bus_clk)) { 1046 dev_err(&pdev->dev, "failed to get bus clock: %ld\n", 1047 PTR_ERR(sai->bus_clk)); 1048 sai->bus_clk = NULL; 1049 } 1050 1051 for (i = 1; i < FSL_SAI_MCLK_MAX; i++) { 1052 sprintf(tmp, "mclk%d", i); 1053 sai->mclk_clk[i] = devm_clk_get(&pdev->dev, tmp); 1054 if (IS_ERR(sai->mclk_clk[i])) { 1055 dev_err(&pdev->dev, "failed to get mclk%d clock: %ld\n", 1056 i + 1, PTR_ERR(sai->mclk_clk[i])); 1057 sai->mclk_clk[i] = NULL; 1058 } 1059 } 1060 1061 if (sai->soc_data->mclk0_is_mclk1) 1062 sai->mclk_clk[0] = sai->mclk_clk[1]; 1063 else 1064 sai->mclk_clk[0] = sai->bus_clk; 1065 1066 irq = platform_get_irq(pdev, 0); 1067 if (irq < 0) 1068 return irq; 1069 1070 ret = devm_request_irq(&pdev->dev, irq, fsl_sai_isr, IRQF_SHARED, 1071 np->name, sai); 1072 if (ret) { 1073 dev_err(&pdev->dev, "failed to claim irq %u\n", irq); 1074 return ret; 1075 } 1076 1077 memcpy(&sai->cpu_dai_drv, &fsl_sai_dai_template, 1078 sizeof(fsl_sai_dai_template)); 1079 1080 /* Sync Tx with Rx as default by following old DT binding */ 1081 sai->synchronous[RX] = true; 1082 sai->synchronous[TX] = false; 1083 sai->cpu_dai_drv.symmetric_rate = 1; 1084 sai->cpu_dai_drv.symmetric_channels = 1; 1085 sai->cpu_dai_drv.symmetric_sample_bits = 1; 1086 1087 if (of_find_property(np, "fsl,sai-synchronous-rx", NULL) && 1088 of_find_property(np, "fsl,sai-asynchronous", NULL)) { 1089 /* error out if both synchronous and asynchronous are present */ 1090 dev_err(&pdev->dev, "invalid binding for synchronous mode\n"); 1091 return -EINVAL; 1092 } 1093 1094 if (of_find_property(np, "fsl,sai-synchronous-rx", NULL)) { 1095 /* Sync Rx with Tx */ 1096 sai->synchronous[RX] = false; 1097 sai->synchronous[TX] = true; 1098 } else if (of_find_property(np, "fsl,sai-asynchronous", NULL)) { 1099 /* Discard all settings for asynchronous mode */ 1100 sai->synchronous[RX] = false; 1101 sai->synchronous[TX] = false; 1102 sai->cpu_dai_drv.symmetric_rate = 0; 1103 sai->cpu_dai_drv.symmetric_channels = 0; 1104 sai->cpu_dai_drv.symmetric_sample_bits = 0; 1105 } 1106 1107 if (of_find_property(np, "fsl,sai-mclk-direction-output", NULL) && 1108 of_device_is_compatible(np, "fsl,imx6ul-sai")) { 1109 gpr = syscon_regmap_lookup_by_compatible("fsl,imx6ul-iomuxc-gpr"); 1110 if (IS_ERR(gpr)) { 1111 dev_err(&pdev->dev, "cannot find iomuxc registers\n"); 1112 return PTR_ERR(gpr); 1113 } 1114 1115 index = of_alias_get_id(np, "sai"); 1116 if (index < 0) 1117 return index; 1118 1119 regmap_update_bits(gpr, IOMUXC_GPR1, MCLK_DIR(index), 1120 MCLK_DIR(index)); 1121 } 1122 1123 sai->dma_params_rx.addr = res->start + FSL_SAI_RDR0; 1124 sai->dma_params_tx.addr = res->start + FSL_SAI_TDR0; 1125 sai->dma_params_rx.maxburst = FSL_SAI_MAXBURST_RX; 1126 sai->dma_params_tx.maxburst = FSL_SAI_MAXBURST_TX; 1127 1128 platform_set_drvdata(pdev, sai); 1129 1130 /* Get sai version */ 1131 ret = fsl_sai_check_version(&pdev->dev); 1132 if (ret < 0) 1133 dev_warn(&pdev->dev, "Error reading SAI version: %d\n", ret); 1134 1135 /* Select MCLK direction */ 1136 if (of_find_property(np, "fsl,sai-mclk-direction-output", NULL) && 1137 sai->verid.major >= 3 && sai->verid.minor >= 1) { 1138 regmap_update_bits(sai->regmap, FSL_SAI_MCTL, 1139 FSL_SAI_MCTL_MCLK_EN, FSL_SAI_MCTL_MCLK_EN); 1140 } 1141 1142 pm_runtime_enable(&pdev->dev); 1143 regcache_cache_only(sai->regmap, true); 1144 1145 ret = devm_snd_soc_register_component(&pdev->dev, &fsl_component, 1146 &sai->cpu_dai_drv, 1); 1147 if (ret) 1148 goto err_pm_disable; 1149 1150 if (sai->soc_data->use_imx_pcm) { 1151 ret = imx_pcm_dma_init(pdev, IMX_SAI_DMABUF_SIZE); 1152 if (ret) 1153 goto err_pm_disable; 1154 } else { 1155 ret = devm_snd_dmaengine_pcm_register(&pdev->dev, NULL, 0); 1156 if (ret) 1157 goto err_pm_disable; 1158 } 1159 1160 return ret; 1161 1162 err_pm_disable: 1163 pm_runtime_disable(&pdev->dev); 1164 1165 return ret; 1166 } 1167 1168 static int fsl_sai_remove(struct platform_device *pdev) 1169 { 1170 pm_runtime_disable(&pdev->dev); 1171 1172 return 0; 1173 } 1174 1175 static const struct fsl_sai_soc_data fsl_sai_vf610_data = { 1176 .use_imx_pcm = false, 1177 .use_edma = false, 1178 .fifo_depth = 32, 1179 .reg_offset = 0, 1180 .mclk0_is_mclk1 = false, 1181 .flags = 0, 1182 }; 1183 1184 static const struct fsl_sai_soc_data fsl_sai_imx6sx_data = { 1185 .use_imx_pcm = true, 1186 .use_edma = false, 1187 .fifo_depth = 32, 1188 .reg_offset = 0, 1189 .mclk0_is_mclk1 = true, 1190 .flags = 0, 1191 }; 1192 1193 static const struct fsl_sai_soc_data fsl_sai_imx7ulp_data = { 1194 .use_imx_pcm = true, 1195 .use_edma = false, 1196 .fifo_depth = 16, 1197 .reg_offset = 8, 1198 .mclk0_is_mclk1 = false, 1199 .flags = PMQOS_CPU_LATENCY, 1200 }; 1201 1202 static const struct fsl_sai_soc_data fsl_sai_imx8mq_data = { 1203 .use_imx_pcm = true, 1204 .use_edma = false, 1205 .fifo_depth = 128, 1206 .reg_offset = 8, 1207 .mclk0_is_mclk1 = false, 1208 .flags = 0, 1209 }; 1210 1211 static const struct fsl_sai_soc_data fsl_sai_imx8qm_data = { 1212 .use_imx_pcm = true, 1213 .use_edma = true, 1214 .fifo_depth = 64, 1215 .reg_offset = 0, 1216 .mclk0_is_mclk1 = false, 1217 .flags = 0, 1218 }; 1219 1220 static const struct of_device_id fsl_sai_ids[] = { 1221 { .compatible = "fsl,vf610-sai", .data = &fsl_sai_vf610_data }, 1222 { .compatible = "fsl,imx6sx-sai", .data = &fsl_sai_imx6sx_data }, 1223 { .compatible = "fsl,imx6ul-sai", .data = &fsl_sai_imx6sx_data }, 1224 { .compatible = "fsl,imx7ulp-sai", .data = &fsl_sai_imx7ulp_data }, 1225 { .compatible = "fsl,imx8mq-sai", .data = &fsl_sai_imx8mq_data }, 1226 { .compatible = "fsl,imx8qm-sai", .data = &fsl_sai_imx8qm_data }, 1227 { /* sentinel */ } 1228 }; 1229 MODULE_DEVICE_TABLE(of, fsl_sai_ids); 1230 1231 #ifdef CONFIG_PM 1232 static int fsl_sai_runtime_suspend(struct device *dev) 1233 { 1234 struct fsl_sai *sai = dev_get_drvdata(dev); 1235 1236 if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE)) 1237 clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[0]]); 1238 1239 if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK)) 1240 clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[1]]); 1241 1242 clk_disable_unprepare(sai->bus_clk); 1243 1244 if (sai->soc_data->flags & PMQOS_CPU_LATENCY) 1245 cpu_latency_qos_remove_request(&sai->pm_qos_req); 1246 1247 regcache_cache_only(sai->regmap, true); 1248 1249 return 0; 1250 } 1251 1252 static int fsl_sai_runtime_resume(struct device *dev) 1253 { 1254 struct fsl_sai *sai = dev_get_drvdata(dev); 1255 unsigned int ofs = sai->soc_data->reg_offset; 1256 int ret; 1257 1258 ret = clk_prepare_enable(sai->bus_clk); 1259 if (ret) { 1260 dev_err(dev, "failed to enable bus clock: %d\n", ret); 1261 return ret; 1262 } 1263 1264 if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK)) { 1265 ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[1]]); 1266 if (ret) 1267 goto disable_bus_clk; 1268 } 1269 1270 if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE)) { 1271 ret = clk_prepare_enable(sai->mclk_clk[sai->mclk_id[0]]); 1272 if (ret) 1273 goto disable_tx_clk; 1274 } 1275 1276 if (sai->soc_data->flags & PMQOS_CPU_LATENCY) 1277 cpu_latency_qos_add_request(&sai->pm_qos_req, 0); 1278 1279 regcache_cache_only(sai->regmap, false); 1280 regcache_mark_dirty(sai->regmap); 1281 regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), FSL_SAI_CSR_SR); 1282 regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), FSL_SAI_CSR_SR); 1283 usleep_range(1000, 2000); 1284 regmap_write(sai->regmap, FSL_SAI_TCSR(ofs), 0); 1285 regmap_write(sai->regmap, FSL_SAI_RCSR(ofs), 0); 1286 1287 ret = regcache_sync(sai->regmap); 1288 if (ret) 1289 goto disable_rx_clk; 1290 1291 return 0; 1292 1293 disable_rx_clk: 1294 if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_CAPTURE)) 1295 clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[0]]); 1296 disable_tx_clk: 1297 if (sai->mclk_streams & BIT(SNDRV_PCM_STREAM_PLAYBACK)) 1298 clk_disable_unprepare(sai->mclk_clk[sai->mclk_id[1]]); 1299 disable_bus_clk: 1300 clk_disable_unprepare(sai->bus_clk); 1301 1302 return ret; 1303 } 1304 #endif /* CONFIG_PM */ 1305 1306 static const struct dev_pm_ops fsl_sai_pm_ops = { 1307 SET_RUNTIME_PM_OPS(fsl_sai_runtime_suspend, 1308 fsl_sai_runtime_resume, NULL) 1309 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, 1310 pm_runtime_force_resume) 1311 }; 1312 1313 static struct platform_driver fsl_sai_driver = { 1314 .probe = fsl_sai_probe, 1315 .remove = fsl_sai_remove, 1316 .driver = { 1317 .name = "fsl-sai", 1318 .pm = &fsl_sai_pm_ops, 1319 .of_match_table = fsl_sai_ids, 1320 }, 1321 }; 1322 module_platform_driver(fsl_sai_driver); 1323 1324 MODULE_DESCRIPTION("Freescale Soc SAI Interface"); 1325 MODULE_AUTHOR("Xiubo Li, <Li.Xiubo@freescale.com>"); 1326 MODULE_ALIAS("platform:fsl-sai"); 1327 MODULE_LICENSE("GPL"); 1328