1 /* 2 * Freescale SSI ALSA SoC Digital Audio Interface (DAI) driver 3 * 4 * Author: Timur Tabi <timur@freescale.com> 5 * 6 * Copyright 2007-2010 Freescale Semiconductor, Inc. 7 * 8 * This file is licensed under the terms of the GNU General Public License 9 * version 2. This program is licensed "as is" without any warranty of any 10 * kind, whether express or implied. 11 * 12 * 13 * Some notes why imx-pcm-fiq is used instead of DMA on some boards: 14 * 15 * The i.MX SSI core has some nasty limitations in AC97 mode. While most 16 * sane processor vendors have a FIFO per AC97 slot, the i.MX has only 17 * one FIFO which combines all valid receive slots. We cannot even select 18 * which slots we want to receive. The WM9712 with which this driver 19 * was developed with always sends GPIO status data in slot 12 which 20 * we receive in our (PCM-) data stream. The only chance we have is to 21 * manually skip this data in the FIQ handler. With sampling rates different 22 * from 48000Hz not every frame has valid receive data, so the ratio 23 * between pcm data and GPIO status data changes. Our FIQ handler is not 24 * able to handle this, hence this driver only works with 48000Hz sampling 25 * rate. 26 * Reading and writing AC97 registers is another challenge. The core 27 * provides us status bits when the read register is updated with *another* 28 * value. When we read the same register two times (and the register still 29 * contains the same value) these status bits are not set. We work 30 * around this by not polling these bits but only wait a fixed delay. 31 */ 32 33 #include <linux/init.h> 34 #include <linux/io.h> 35 #include <linux/module.h> 36 #include <linux/interrupt.h> 37 #include <linux/clk.h> 38 #include <linux/ctype.h> 39 #include <linux/device.h> 40 #include <linux/delay.h> 41 #include <linux/mutex.h> 42 #include <linux/slab.h> 43 #include <linux/spinlock.h> 44 #include <linux/of.h> 45 #include <linux/of_address.h> 46 #include <linux/of_irq.h> 47 #include <linux/of_platform.h> 48 49 #include <sound/core.h> 50 #include <sound/pcm.h> 51 #include <sound/pcm_params.h> 52 #include <sound/initval.h> 53 #include <sound/soc.h> 54 #include <sound/dmaengine_pcm.h> 55 56 #include "fsl_ssi.h" 57 #include "imx-pcm.h" 58 59 /* Define RX and TX to index ssi->regvals array; Can be 0 or 1 only */ 60 #define RX 0 61 #define TX 1 62 63 /** 64 * FSLSSI_I2S_FORMATS: audio formats supported by the SSI 65 * 66 * The SSI has a limitation in that the samples must be in the same byte 67 * order as the host CPU. This is because when multiple bytes are written 68 * to the STX register, the bytes and bits must be written in the same 69 * order. The STX is a shift register, so all the bits need to be aligned 70 * (bit-endianness must match byte-endianness). Processors typically write 71 * the bits within a byte in the same order that the bytes of a word are 72 * written in. So if the host CPU is big-endian, then only big-endian 73 * samples will be written to STX properly. 74 */ 75 #ifdef __BIG_ENDIAN 76 #define FSLSSI_I2S_FORMATS \ 77 (SNDRV_PCM_FMTBIT_S8 | \ 78 SNDRV_PCM_FMTBIT_S16_BE | \ 79 SNDRV_PCM_FMTBIT_S18_3BE | \ 80 SNDRV_PCM_FMTBIT_S20_3BE | \ 81 SNDRV_PCM_FMTBIT_S24_3BE | \ 82 SNDRV_PCM_FMTBIT_S24_BE) 83 #else 84 #define FSLSSI_I2S_FORMATS \ 85 (SNDRV_PCM_FMTBIT_S8 | \ 86 SNDRV_PCM_FMTBIT_S16_LE | \ 87 SNDRV_PCM_FMTBIT_S18_3LE | \ 88 SNDRV_PCM_FMTBIT_S20_3LE | \ 89 SNDRV_PCM_FMTBIT_S24_3LE | \ 90 SNDRV_PCM_FMTBIT_S24_LE) 91 #endif 92 93 /* 94 * In AC97 mode, TXDIR bit is forced to 0 and TFDIR bit is forced to 1: 95 * - SSI inputs external bit clock and outputs frame sync clock -- CBM_CFS 96 * - Also have NB_NF to mark these two clocks will not be inverted 97 */ 98 #define FSLSSI_AC97_DAIFMT \ 99 (SND_SOC_DAIFMT_AC97 | \ 100 SND_SOC_DAIFMT_CBM_CFS | \ 101 SND_SOC_DAIFMT_NB_NF) 102 103 #define FSLSSI_SIER_DBG_RX_FLAGS \ 104 (SSI_SIER_RFF0_EN | \ 105 SSI_SIER_RLS_EN | \ 106 SSI_SIER_RFS_EN | \ 107 SSI_SIER_ROE0_EN | \ 108 SSI_SIER_RFRC_EN) 109 #define FSLSSI_SIER_DBG_TX_FLAGS \ 110 (SSI_SIER_TFE0_EN | \ 111 SSI_SIER_TLS_EN | \ 112 SSI_SIER_TFS_EN | \ 113 SSI_SIER_TUE0_EN | \ 114 SSI_SIER_TFRC_EN) 115 116 enum fsl_ssi_type { 117 FSL_SSI_MCP8610, 118 FSL_SSI_MX21, 119 FSL_SSI_MX35, 120 FSL_SSI_MX51, 121 }; 122 123 struct fsl_ssi_regvals { 124 u32 sier; 125 u32 srcr; 126 u32 stcr; 127 u32 scr; 128 }; 129 130 static bool fsl_ssi_readable_reg(struct device *dev, unsigned int reg) 131 { 132 switch (reg) { 133 case REG_SSI_SACCEN: 134 case REG_SSI_SACCDIS: 135 return false; 136 default: 137 return true; 138 } 139 } 140 141 static bool fsl_ssi_volatile_reg(struct device *dev, unsigned int reg) 142 { 143 switch (reg) { 144 case REG_SSI_STX0: 145 case REG_SSI_STX1: 146 case REG_SSI_SRX0: 147 case REG_SSI_SRX1: 148 case REG_SSI_SISR: 149 case REG_SSI_SFCSR: 150 case REG_SSI_SACNT: 151 case REG_SSI_SACADD: 152 case REG_SSI_SACDAT: 153 case REG_SSI_SATAG: 154 case REG_SSI_SACCST: 155 case REG_SSI_SOR: 156 return true; 157 default: 158 return false; 159 } 160 } 161 162 static bool fsl_ssi_precious_reg(struct device *dev, unsigned int reg) 163 { 164 switch (reg) { 165 case REG_SSI_SRX0: 166 case REG_SSI_SRX1: 167 case REG_SSI_SISR: 168 case REG_SSI_SACADD: 169 case REG_SSI_SACDAT: 170 case REG_SSI_SATAG: 171 return true; 172 default: 173 return false; 174 } 175 } 176 177 static bool fsl_ssi_writeable_reg(struct device *dev, unsigned int reg) 178 { 179 switch (reg) { 180 case REG_SSI_SRX0: 181 case REG_SSI_SRX1: 182 case REG_SSI_SACCST: 183 return false; 184 default: 185 return true; 186 } 187 } 188 189 static const struct regmap_config fsl_ssi_regconfig = { 190 .max_register = REG_SSI_SACCDIS, 191 .reg_bits = 32, 192 .val_bits = 32, 193 .reg_stride = 4, 194 .val_format_endian = REGMAP_ENDIAN_NATIVE, 195 .num_reg_defaults_raw = REG_SSI_SACCDIS / sizeof(uint32_t) + 1, 196 .readable_reg = fsl_ssi_readable_reg, 197 .volatile_reg = fsl_ssi_volatile_reg, 198 .precious_reg = fsl_ssi_precious_reg, 199 .writeable_reg = fsl_ssi_writeable_reg, 200 .cache_type = REGCACHE_FLAT, 201 }; 202 203 struct fsl_ssi_soc_data { 204 bool imx; 205 bool imx21regs; /* imx21-class SSI - no SACC{ST,EN,DIS} regs */ 206 bool offline_config; 207 u32 sisr_write_mask; 208 }; 209 210 /** 211 * fsl_ssi: per-SSI private data 212 * 213 * @regs: Pointer to the regmap registers 214 * @irq: IRQ of this SSI 215 * @cpu_dai_drv: CPU DAI driver for this device 216 * 217 * @dai_fmt: DAI configuration this device is currently used with 218 * @streams: Mask of current active streams: BIT(TX) and BIT(RX) 219 * @i2s_net: I2S and Network mode configurations of SCR register 220 * @synchronous: Use synchronous mode - both of TX and RX use STCK and SFCK 221 * @use_dma: DMA is used or FIQ with stream filter 222 * @use_dual_fifo: DMA with support for dual FIFO mode 223 * @has_ipg_clk_name: If "ipg" is in the clock name list of device tree 224 * @fifo_depth: Depth of the SSI FIFOs 225 * @slot_width: Width of each DAI slot 226 * @slots: Number of slots 227 * @regvals: Specific RX/TX register settings 228 * 229 * @clk: Clock source to access register 230 * @baudclk: Clock source to generate bit and frame-sync clocks 231 * @baudclk_streams: Active streams that are using baudclk 232 * 233 * @regcache_sfcsr: Cache sfcsr register value during suspend and resume 234 * @regcache_sacnt: Cache sacnt register value during suspend and resume 235 * 236 * @dma_params_tx: DMA transmit parameters 237 * @dma_params_rx: DMA receive parameters 238 * @ssi_phys: physical address of the SSI registers 239 * 240 * @fiq_params: FIQ stream filtering parameters 241 * 242 * @card_pdev: Platform_device pointer to register a sound card for PowerPC or 243 * to register a CODEC platform device for AC97 244 * @card_name: Platform_device name to register a sound card for PowerPC or 245 * to register a CODEC platform device for AC97 246 * @card_idx: The index of SSI to register a sound card for PowerPC or 247 * to register a CODEC platform device for AC97 248 * 249 * @dbg_stats: Debugging statistics 250 * 251 * @soc: SoC specific data 252 * @dev: Pointer to &pdev->dev 253 * 254 * @fifo_watermark: The FIFO watermark setting. Notifies DMA when there are 255 * @fifo_watermark or fewer words in TX fifo or 256 * @fifo_watermark or more empty words in RX fifo. 257 * @dma_maxburst: Max number of words to transfer in one go. So far, 258 * this is always the same as fifo_watermark. 259 * 260 * @ac97_reg_lock: Mutex lock to serialize AC97 register access operations 261 */ 262 struct fsl_ssi { 263 struct regmap *regs; 264 int irq; 265 struct snd_soc_dai_driver cpu_dai_drv; 266 267 unsigned int dai_fmt; 268 u8 streams; 269 u8 i2s_net; 270 bool synchronous; 271 bool use_dma; 272 bool use_dual_fifo; 273 bool has_ipg_clk_name; 274 unsigned int fifo_depth; 275 unsigned int slot_width; 276 unsigned int slots; 277 struct fsl_ssi_regvals regvals[2]; 278 279 struct clk *clk; 280 struct clk *baudclk; 281 unsigned int baudclk_streams; 282 283 u32 regcache_sfcsr; 284 u32 regcache_sacnt; 285 286 struct snd_dmaengine_dai_dma_data dma_params_tx; 287 struct snd_dmaengine_dai_dma_data dma_params_rx; 288 dma_addr_t ssi_phys; 289 290 struct imx_pcm_fiq_params fiq_params; 291 292 struct platform_device *card_pdev; 293 char card_name[32]; 294 u32 card_idx; 295 296 struct fsl_ssi_dbg dbg_stats; 297 298 const struct fsl_ssi_soc_data *soc; 299 struct device *dev; 300 301 u32 fifo_watermark; 302 u32 dma_maxburst; 303 304 struct mutex ac97_reg_lock; 305 }; 306 307 /* 308 * SoC specific data 309 * 310 * Notes: 311 * 1) SSI in earlier SoCS has critical bits in control registers that 312 * cannot be changed after SSI starts running -- a software reset 313 * (set SSIEN to 0) is required to change their values. So adding 314 * an offline_config flag for these SoCs. 315 * 2) SDMA is available since imx35. However, imx35 does not support 316 * DMA bits changing when SSI is running, so set offline_config. 317 * 3) imx51 and later versions support register configurations when 318 * SSI is running (SSIEN); For these versions, DMA needs to be 319 * configured before SSI sends DMA request to avoid an undefined 320 * DMA request on the SDMA side. 321 */ 322 323 static struct fsl_ssi_soc_data fsl_ssi_mpc8610 = { 324 .imx = false, 325 .offline_config = true, 326 .sisr_write_mask = SSI_SISR_RFRC | SSI_SISR_TFRC | 327 SSI_SISR_ROE0 | SSI_SISR_ROE1 | 328 SSI_SISR_TUE0 | SSI_SISR_TUE1, 329 }; 330 331 static struct fsl_ssi_soc_data fsl_ssi_imx21 = { 332 .imx = true, 333 .imx21regs = true, 334 .offline_config = true, 335 .sisr_write_mask = 0, 336 }; 337 338 static struct fsl_ssi_soc_data fsl_ssi_imx35 = { 339 .imx = true, 340 .offline_config = true, 341 .sisr_write_mask = SSI_SISR_RFRC | SSI_SISR_TFRC | 342 SSI_SISR_ROE0 | SSI_SISR_ROE1 | 343 SSI_SISR_TUE0 | SSI_SISR_TUE1, 344 }; 345 346 static struct fsl_ssi_soc_data fsl_ssi_imx51 = { 347 .imx = true, 348 .offline_config = false, 349 .sisr_write_mask = SSI_SISR_ROE0 | SSI_SISR_ROE1 | 350 SSI_SISR_TUE0 | SSI_SISR_TUE1, 351 }; 352 353 static const struct of_device_id fsl_ssi_ids[] = { 354 { .compatible = "fsl,mpc8610-ssi", .data = &fsl_ssi_mpc8610 }, 355 { .compatible = "fsl,imx51-ssi", .data = &fsl_ssi_imx51 }, 356 { .compatible = "fsl,imx35-ssi", .data = &fsl_ssi_imx35 }, 357 { .compatible = "fsl,imx21-ssi", .data = &fsl_ssi_imx21 }, 358 {} 359 }; 360 MODULE_DEVICE_TABLE(of, fsl_ssi_ids); 361 362 static bool fsl_ssi_is_ac97(struct fsl_ssi *ssi) 363 { 364 return (ssi->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) == 365 SND_SOC_DAIFMT_AC97; 366 } 367 368 static bool fsl_ssi_is_i2s_master(struct fsl_ssi *ssi) 369 { 370 return (ssi->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) == 371 SND_SOC_DAIFMT_CBS_CFS; 372 } 373 374 static bool fsl_ssi_is_i2s_cbm_cfs(struct fsl_ssi *ssi) 375 { 376 return (ssi->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) == 377 SND_SOC_DAIFMT_CBM_CFS; 378 } 379 380 /** 381 * Interrupt handler to gather states 382 */ 383 static irqreturn_t fsl_ssi_isr(int irq, void *dev_id) 384 { 385 struct fsl_ssi *ssi = dev_id; 386 struct regmap *regs = ssi->regs; 387 __be32 sisr; 388 __be32 sisr2; 389 390 regmap_read(regs, REG_SSI_SISR, &sisr); 391 392 sisr2 = sisr & ssi->soc->sisr_write_mask; 393 /* Clear the bits that we set */ 394 if (sisr2) 395 regmap_write(regs, REG_SSI_SISR, sisr2); 396 397 fsl_ssi_dbg_isr(&ssi->dbg_stats, sisr); 398 399 return IRQ_HANDLED; 400 } 401 402 /** 403 * Set SCR, SIER, STCR and SRCR registers with cached values in regvals 404 * 405 * Notes: 406 * 1) For offline_config SoCs, enable all necessary bits of both streams 407 * when 1st stream starts, even if the opposite stream will not start 408 * 2) It also clears FIFO before setting regvals; SOR is safe to set online 409 */ 410 static void fsl_ssi_config_enable(struct fsl_ssi *ssi, bool tx) 411 { 412 struct fsl_ssi_regvals *vals = ssi->regvals; 413 int dir = tx ? TX : RX; 414 u32 sier, srcr, stcr; 415 416 /* Clear dirty data in the FIFO; It also prevents channel slipping */ 417 regmap_update_bits(ssi->regs, REG_SSI_SOR, 418 SSI_SOR_xX_CLR(tx), SSI_SOR_xX_CLR(tx)); 419 420 /* 421 * On offline_config SoCs, SxCR and SIER are already configured when 422 * the previous stream started. So skip all SxCR and SIER settings 423 * to prevent online reconfigurations, then jump to set SCR directly 424 */ 425 if (ssi->soc->offline_config && ssi->streams) 426 goto enable_scr; 427 428 if (ssi->soc->offline_config) { 429 /* 430 * Online reconfiguration not supported, so enable all bits for 431 * both streams at once to avoid necessity of reconfigurations 432 */ 433 srcr = vals[RX].srcr | vals[TX].srcr; 434 stcr = vals[RX].stcr | vals[TX].stcr; 435 sier = vals[RX].sier | vals[TX].sier; 436 } else { 437 /* Otherwise, only set bits for the current stream */ 438 srcr = vals[dir].srcr; 439 stcr = vals[dir].stcr; 440 sier = vals[dir].sier; 441 } 442 443 /* Configure SRCR, STCR and SIER at once */ 444 regmap_update_bits(ssi->regs, REG_SSI_SRCR, srcr, srcr); 445 regmap_update_bits(ssi->regs, REG_SSI_STCR, stcr, stcr); 446 regmap_update_bits(ssi->regs, REG_SSI_SIER, sier, sier); 447 448 enable_scr: 449 /* 450 * Start DMA before setting TE to avoid FIFO underrun 451 * which may cause a channel slip or a channel swap 452 * 453 * TODO: FIQ cases might also need this upon testing 454 */ 455 if (ssi->use_dma && tx) { 456 int try = 100; 457 u32 sfcsr; 458 459 /* Enable SSI first to send TX DMA request */ 460 regmap_update_bits(ssi->regs, REG_SSI_SCR, 461 SSI_SCR_SSIEN, SSI_SCR_SSIEN); 462 463 /* Busy wait until TX FIFO not empty -- DMA working */ 464 do { 465 regmap_read(ssi->regs, REG_SSI_SFCSR, &sfcsr); 466 if (SSI_SFCSR_TFCNT0(sfcsr)) 467 break; 468 } while (--try); 469 470 /* FIFO still empty -- something might be wrong */ 471 if (!SSI_SFCSR_TFCNT0(sfcsr)) 472 dev_warn(ssi->dev, "Timeout waiting TX FIFO filling\n"); 473 } 474 /* Enable all remaining bits in SCR */ 475 regmap_update_bits(ssi->regs, REG_SSI_SCR, 476 vals[dir].scr, vals[dir].scr); 477 478 /* Log the enabled stream to the mask */ 479 ssi->streams |= BIT(dir); 480 } 481 482 /** 483 * Exclude bits that are used by the opposite stream 484 * 485 * When both streams are active, disabling some bits for the current stream 486 * might break the other stream if these bits are used by it. 487 * 488 * @vals : regvals of the current stream 489 * @avals: regvals of the opposite stream 490 * @aactive: active state of the opposite stream 491 * 492 * 1) XOR vals and avals to get the differences if the other stream is active; 493 * Otherwise, return current vals if the other stream is not active 494 * 2) AND the result of 1) with the current vals 495 */ 496 #define _ssi_xor_shared_bits(vals, avals, aactive) \ 497 ((vals) ^ ((avals) * (aactive))) 498 499 #define ssi_excl_shared_bits(vals, avals, aactive) \ 500 ((vals) & _ssi_xor_shared_bits(vals, avals, aactive)) 501 502 /** 503 * Unset SCR, SIER, STCR and SRCR registers with cached values in regvals 504 * 505 * Notes: 506 * 1) For offline_config SoCs, to avoid online reconfigurations, disable all 507 * bits of both streams at once when the last stream is abort to end 508 * 2) It also clears FIFO after unsetting regvals; SOR is safe to set online 509 */ 510 static void fsl_ssi_config_disable(struct fsl_ssi *ssi, bool tx) 511 { 512 struct fsl_ssi_regvals *vals, *avals; 513 u32 sier, srcr, stcr, scr; 514 int adir = tx ? RX : TX; 515 int dir = tx ? TX : RX; 516 bool aactive; 517 518 /* Check if the opposite stream is active */ 519 aactive = ssi->streams & BIT(adir); 520 521 vals = &ssi->regvals[dir]; 522 523 /* Get regvals of the opposite stream to keep opposite stream safe */ 524 avals = &ssi->regvals[adir]; 525 526 /* 527 * To keep the other stream safe, exclude shared bits between 528 * both streams, and get safe bits to disable current stream 529 */ 530 scr = ssi_excl_shared_bits(vals->scr, avals->scr, aactive); 531 532 /* Disable safe bits of SCR register for the current stream */ 533 regmap_update_bits(ssi->regs, REG_SSI_SCR, scr, 0); 534 535 /* Log the disabled stream to the mask */ 536 ssi->streams &= ~BIT(dir); 537 538 /* 539 * On offline_config SoCs, if the other stream is active, skip 540 * SxCR and SIER settings to prevent online reconfigurations 541 */ 542 if (ssi->soc->offline_config && aactive) 543 goto fifo_clear; 544 545 if (ssi->soc->offline_config) { 546 /* Now there is only current stream active, disable all bits */ 547 srcr = vals->srcr | avals->srcr; 548 stcr = vals->stcr | avals->stcr; 549 sier = vals->sier | avals->sier; 550 } else { 551 /* 552 * To keep the other stream safe, exclude shared bits between 553 * both streams, and get safe bits to disable current stream 554 */ 555 sier = ssi_excl_shared_bits(vals->sier, avals->sier, aactive); 556 srcr = ssi_excl_shared_bits(vals->srcr, avals->srcr, aactive); 557 stcr = ssi_excl_shared_bits(vals->stcr, avals->stcr, aactive); 558 } 559 560 /* Clear configurations of SRCR, STCR and SIER at once */ 561 regmap_update_bits(ssi->regs, REG_SSI_SRCR, srcr, 0); 562 regmap_update_bits(ssi->regs, REG_SSI_STCR, stcr, 0); 563 regmap_update_bits(ssi->regs, REG_SSI_SIER, sier, 0); 564 565 fifo_clear: 566 /* Clear remaining data in the FIFO */ 567 regmap_update_bits(ssi->regs, REG_SSI_SOR, 568 SSI_SOR_xX_CLR(tx), SSI_SOR_xX_CLR(tx)); 569 } 570 571 static void fsl_ssi_tx_ac97_saccst_setup(struct fsl_ssi *ssi) 572 { 573 struct regmap *regs = ssi->regs; 574 575 /* no SACC{ST,EN,DIS} regs on imx21-class SSI */ 576 if (!ssi->soc->imx21regs) { 577 /* Disable all channel slots */ 578 regmap_write(regs, REG_SSI_SACCDIS, 0xff); 579 /* Enable slots 3 & 4 -- PCM Playback Left & Right channels */ 580 regmap_write(regs, REG_SSI_SACCEN, 0x300); 581 } 582 } 583 584 /** 585 * Cache critical bits of SIER, SRCR, STCR and SCR to later set them safely 586 */ 587 static void fsl_ssi_setup_regvals(struct fsl_ssi *ssi) 588 { 589 struct fsl_ssi_regvals *vals = ssi->regvals; 590 591 vals[RX].sier = SSI_SIER_RFF0_EN | FSLSSI_SIER_DBG_RX_FLAGS; 592 vals[RX].srcr = SSI_SRCR_RFEN0; 593 vals[RX].scr = SSI_SCR_SSIEN | SSI_SCR_RE; 594 vals[TX].sier = SSI_SIER_TFE0_EN | FSLSSI_SIER_DBG_TX_FLAGS; 595 vals[TX].stcr = SSI_STCR_TFEN0; 596 vals[TX].scr = SSI_SCR_SSIEN | SSI_SCR_TE; 597 598 /* AC97 has already enabled SSIEN, RE and TE, so ignore them */ 599 if (fsl_ssi_is_ac97(ssi)) 600 vals[RX].scr = vals[TX].scr = 0; 601 602 if (ssi->use_dual_fifo) { 603 vals[RX].srcr |= SSI_SRCR_RFEN1; 604 vals[TX].stcr |= SSI_STCR_TFEN1; 605 } 606 607 if (ssi->use_dma) { 608 vals[RX].sier |= SSI_SIER_RDMAE; 609 vals[TX].sier |= SSI_SIER_TDMAE; 610 } else { 611 vals[RX].sier |= SSI_SIER_RIE; 612 vals[TX].sier |= SSI_SIER_TIE; 613 } 614 } 615 616 static void fsl_ssi_setup_ac97(struct fsl_ssi *ssi) 617 { 618 struct regmap *regs = ssi->regs; 619 620 /* Setup the clock control register */ 621 regmap_write(regs, REG_SSI_STCCR, SSI_SxCCR_WL(17) | SSI_SxCCR_DC(13)); 622 regmap_write(regs, REG_SSI_SRCCR, SSI_SxCCR_WL(17) | SSI_SxCCR_DC(13)); 623 624 /* Enable AC97 mode and startup the SSI */ 625 regmap_write(regs, REG_SSI_SACNT, SSI_SACNT_AC97EN | SSI_SACNT_FV); 626 627 /* AC97 has to communicate with codec before starting a stream */ 628 regmap_update_bits(regs, REG_SSI_SCR, 629 SSI_SCR_SSIEN | SSI_SCR_TE | SSI_SCR_RE, 630 SSI_SCR_SSIEN | SSI_SCR_TE | SSI_SCR_RE); 631 632 regmap_write(regs, REG_SSI_SOR, SSI_SOR_WAIT(3)); 633 } 634 635 static int fsl_ssi_startup(struct snd_pcm_substream *substream, 636 struct snd_soc_dai *dai) 637 { 638 struct snd_soc_pcm_runtime *rtd = substream->private_data; 639 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(rtd->cpu_dai); 640 int ret; 641 642 ret = clk_prepare_enable(ssi->clk); 643 if (ret) 644 return ret; 645 646 /* 647 * When using dual fifo mode, it is safer to ensure an even period 648 * size. If appearing to an odd number while DMA always starts its 649 * task from fifo0, fifo1 would be neglected at the end of each 650 * period. But SSI would still access fifo1 with an invalid data. 651 */ 652 if (ssi->use_dual_fifo) 653 snd_pcm_hw_constraint_step(substream->runtime, 0, 654 SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2); 655 656 return 0; 657 } 658 659 static void fsl_ssi_shutdown(struct snd_pcm_substream *substream, 660 struct snd_soc_dai *dai) 661 { 662 struct snd_soc_pcm_runtime *rtd = substream->private_data; 663 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(rtd->cpu_dai); 664 665 clk_disable_unprepare(ssi->clk); 666 } 667 668 /** 669 * Configure Digital Audio Interface bit clock 670 * 671 * Note: This function can be only called when using SSI as DAI master 672 * 673 * Quick instruction for parameters: 674 * freq: Output BCLK frequency = samplerate * slots * slot_width 675 * (In 2-channel I2S Master mode, slot_width is fixed 32) 676 */ 677 static int fsl_ssi_set_bclk(struct snd_pcm_substream *substream, 678 struct snd_soc_dai *dai, 679 struct snd_pcm_hw_params *hw_params) 680 { 681 bool tx2, tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 682 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai); 683 struct regmap *regs = ssi->regs; 684 u32 pm = 999, div2, psr, stccr, mask, afreq, factor, i; 685 unsigned long clkrate, baudrate, tmprate; 686 unsigned int slots = params_channels(hw_params); 687 unsigned int slot_width = 32; 688 u64 sub, savesub = 100000; 689 unsigned int freq; 690 bool baudclk_is_used; 691 int ret; 692 693 /* Override slots and slot_width if being specifically set... */ 694 if (ssi->slots) 695 slots = ssi->slots; 696 /* ...but keep 32 bits if slots is 2 -- I2S Master mode */ 697 if (ssi->slot_width && slots != 2) 698 slot_width = ssi->slot_width; 699 700 /* Generate bit clock based on the slot number and slot width */ 701 freq = slots * slot_width * params_rate(hw_params); 702 703 /* Don't apply it to any non-baudclk circumstance */ 704 if (IS_ERR(ssi->baudclk)) 705 return -EINVAL; 706 707 /* 708 * Hardware limitation: The bclk rate must be 709 * never greater than 1/5 IPG clock rate 710 */ 711 if (freq * 5 > clk_get_rate(ssi->clk)) { 712 dev_err(dai->dev, "bitclk > ipgclk / 5\n"); 713 return -EINVAL; 714 } 715 716 baudclk_is_used = ssi->baudclk_streams & ~(BIT(substream->stream)); 717 718 /* It should be already enough to divide clock by setting pm alone */ 719 psr = 0; 720 div2 = 0; 721 722 factor = (div2 + 1) * (7 * psr + 1) * 2; 723 724 for (i = 0; i < 255; i++) { 725 tmprate = freq * factor * (i + 1); 726 727 if (baudclk_is_used) 728 clkrate = clk_get_rate(ssi->baudclk); 729 else 730 clkrate = clk_round_rate(ssi->baudclk, tmprate); 731 732 clkrate /= factor; 733 afreq = clkrate / (i + 1); 734 735 if (freq == afreq) 736 sub = 0; 737 else if (freq / afreq == 1) 738 sub = freq - afreq; 739 else if (afreq / freq == 1) 740 sub = afreq - freq; 741 else 742 continue; 743 744 /* Calculate the fraction */ 745 sub *= 100000; 746 do_div(sub, freq); 747 748 if (sub < savesub && !(i == 0 && psr == 0 && div2 == 0)) { 749 baudrate = tmprate; 750 savesub = sub; 751 pm = i; 752 } 753 754 /* We are lucky */ 755 if (savesub == 0) 756 break; 757 } 758 759 /* No proper pm found if it is still remaining the initial value */ 760 if (pm == 999) { 761 dev_err(dai->dev, "failed to handle the required sysclk\n"); 762 return -EINVAL; 763 } 764 765 stccr = SSI_SxCCR_PM(pm + 1) | (div2 ? SSI_SxCCR_DIV2 : 0) | 766 (psr ? SSI_SxCCR_PSR : 0); 767 mask = SSI_SxCCR_PM_MASK | SSI_SxCCR_DIV2 | SSI_SxCCR_PSR; 768 769 /* STCCR is used for RX in synchronous mode */ 770 tx2 = tx || ssi->synchronous; 771 regmap_update_bits(regs, REG_SSI_SxCCR(tx2), mask, stccr); 772 773 if (!baudclk_is_used) { 774 ret = clk_set_rate(ssi->baudclk, baudrate); 775 if (ret) { 776 dev_err(dai->dev, "failed to set baudclk rate\n"); 777 return -EINVAL; 778 } 779 } 780 781 return 0; 782 } 783 784 /** 785 * Configure SSI based on PCM hardware parameters 786 * 787 * Notes: 788 * 1) SxCCR.WL bits are critical bits that require SSI to be temporarily 789 * disabled on offline_config SoCs. Even for online configurable SoCs 790 * running in synchronous mode (both TX and RX use STCCR), it is not 791 * safe to re-configure them when both two streams start running. 792 * 2) SxCCR.PM, SxCCR.DIV2 and SxCCR.PSR bits will be configured in the 793 * fsl_ssi_set_bclk() if SSI is the DAI clock master. 794 */ 795 static int fsl_ssi_hw_params(struct snd_pcm_substream *substream, 796 struct snd_pcm_hw_params *hw_params, 797 struct snd_soc_dai *dai) 798 { 799 bool tx2, tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 800 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai); 801 struct regmap *regs = ssi->regs; 802 unsigned int channels = params_channels(hw_params); 803 unsigned int sample_size = params_width(hw_params); 804 u32 wl = SSI_SxCCR_WL(sample_size); 805 int ret; 806 807 /* 808 * SSI is properly configured if it is enabled and running in 809 * the synchronous mode; Note that AC97 mode is an exception 810 * that should set separate configurations for STCCR and SRCCR 811 * despite running in the synchronous mode. 812 */ 813 if (ssi->streams && ssi->synchronous) 814 return 0; 815 816 if (fsl_ssi_is_i2s_master(ssi)) { 817 ret = fsl_ssi_set_bclk(substream, dai, hw_params); 818 if (ret) 819 return ret; 820 821 /* Do not enable the clock if it is already enabled */ 822 if (!(ssi->baudclk_streams & BIT(substream->stream))) { 823 ret = clk_prepare_enable(ssi->baudclk); 824 if (ret) 825 return ret; 826 827 ssi->baudclk_streams |= BIT(substream->stream); 828 } 829 } 830 831 if (!fsl_ssi_is_ac97(ssi)) { 832 /* Normal + Network mode to send 16-bit data in 32-bit frames */ 833 if (fsl_ssi_is_i2s_cbm_cfs(ssi) && sample_size == 16) 834 ssi->i2s_net = SSI_SCR_I2S_MODE_NORMAL | SSI_SCR_NET; 835 836 /* Use Normal mode to send mono data at 1st slot of 2 slots */ 837 if (channels == 1) 838 ssi->i2s_net = SSI_SCR_I2S_MODE_NORMAL; 839 840 regmap_update_bits(regs, REG_SSI_SCR, 841 SSI_SCR_I2S_NET_MASK, ssi->i2s_net); 842 } 843 844 /* In synchronous mode, the SSI uses STCCR for capture */ 845 tx2 = tx || ssi->synchronous; 846 regmap_update_bits(regs, REG_SSI_SxCCR(tx2), SSI_SxCCR_WL_MASK, wl); 847 848 return 0; 849 } 850 851 static int fsl_ssi_hw_free(struct snd_pcm_substream *substream, 852 struct snd_soc_dai *dai) 853 { 854 struct snd_soc_pcm_runtime *rtd = substream->private_data; 855 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(rtd->cpu_dai); 856 857 if (fsl_ssi_is_i2s_master(ssi) && 858 ssi->baudclk_streams & BIT(substream->stream)) { 859 clk_disable_unprepare(ssi->baudclk); 860 ssi->baudclk_streams &= ~BIT(substream->stream); 861 } 862 863 return 0; 864 } 865 866 static int _fsl_ssi_set_dai_fmt(struct fsl_ssi *ssi, unsigned int fmt) 867 { 868 u32 strcr = 0, scr = 0, stcr, srcr, mask; 869 870 ssi->dai_fmt = fmt; 871 872 /* Synchronize frame sync clock for TE to avoid data slipping */ 873 scr |= SSI_SCR_SYNC_TX_FS; 874 875 /* Set to default shifting settings: LSB_ALIGNED */ 876 strcr |= SSI_STCR_TXBIT0; 877 878 /* Use Network mode as default */ 879 ssi->i2s_net = SSI_SCR_NET; 880 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 881 case SND_SOC_DAIFMT_I2S: 882 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 883 case SND_SOC_DAIFMT_CBS_CFS: 884 if (IS_ERR(ssi->baudclk)) { 885 dev_err(ssi->dev, 886 "missing baudclk for master mode\n"); 887 return -EINVAL; 888 } 889 /* fall through */ 890 case SND_SOC_DAIFMT_CBM_CFS: 891 ssi->i2s_net |= SSI_SCR_I2S_MODE_MASTER; 892 break; 893 case SND_SOC_DAIFMT_CBM_CFM: 894 ssi->i2s_net |= SSI_SCR_I2S_MODE_SLAVE; 895 break; 896 default: 897 return -EINVAL; 898 } 899 900 regmap_update_bits(ssi->regs, REG_SSI_STCCR, 901 SSI_SxCCR_DC_MASK, SSI_SxCCR_DC(2)); 902 regmap_update_bits(ssi->regs, REG_SSI_SRCCR, 903 SSI_SxCCR_DC_MASK, SSI_SxCCR_DC(2)); 904 905 /* Data on rising edge of bclk, frame low, 1clk before data */ 906 strcr |= SSI_STCR_TFSI | SSI_STCR_TSCKP | SSI_STCR_TEFS; 907 break; 908 case SND_SOC_DAIFMT_LEFT_J: 909 /* Data on rising edge of bclk, frame high */ 910 strcr |= SSI_STCR_TSCKP; 911 break; 912 case SND_SOC_DAIFMT_DSP_A: 913 /* Data on rising edge of bclk, frame high, 1clk before data */ 914 strcr |= SSI_STCR_TFSL | SSI_STCR_TSCKP | SSI_STCR_TEFS; 915 break; 916 case SND_SOC_DAIFMT_DSP_B: 917 /* Data on rising edge of bclk, frame high */ 918 strcr |= SSI_STCR_TFSL | SSI_STCR_TSCKP; 919 break; 920 case SND_SOC_DAIFMT_AC97: 921 /* Data on falling edge of bclk, frame high, 1clk before data */ 922 strcr |= SSI_STCR_TEFS; 923 break; 924 default: 925 return -EINVAL; 926 } 927 928 scr |= ssi->i2s_net; 929 930 /* DAI clock inversion */ 931 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 932 case SND_SOC_DAIFMT_NB_NF: 933 /* Nothing to do for both normal cases */ 934 break; 935 case SND_SOC_DAIFMT_IB_NF: 936 /* Invert bit clock */ 937 strcr ^= SSI_STCR_TSCKP; 938 break; 939 case SND_SOC_DAIFMT_NB_IF: 940 /* Invert frame clock */ 941 strcr ^= SSI_STCR_TFSI; 942 break; 943 case SND_SOC_DAIFMT_IB_IF: 944 /* Invert both clocks */ 945 strcr ^= SSI_STCR_TSCKP; 946 strcr ^= SSI_STCR_TFSI; 947 break; 948 default: 949 return -EINVAL; 950 } 951 952 /* DAI clock master masks */ 953 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 954 case SND_SOC_DAIFMT_CBS_CFS: 955 /* Output bit and frame sync clocks */ 956 strcr |= SSI_STCR_TFDIR | SSI_STCR_TXDIR; 957 scr |= SSI_SCR_SYS_CLK_EN; 958 break; 959 case SND_SOC_DAIFMT_CBM_CFM: 960 /* Input bit or frame sync clocks */ 961 break; 962 case SND_SOC_DAIFMT_CBM_CFS: 963 /* Input bit clock but output frame sync clock */ 964 strcr |= SSI_STCR_TFDIR; 965 break; 966 default: 967 return -EINVAL; 968 } 969 970 stcr = strcr; 971 srcr = strcr; 972 973 /* Set SYN mode and clear RXDIR bit when using SYN or AC97 mode */ 974 if (ssi->synchronous || fsl_ssi_is_ac97(ssi)) { 975 srcr &= ~SSI_SRCR_RXDIR; 976 scr |= SSI_SCR_SYN; 977 } 978 979 mask = SSI_STCR_TFDIR | SSI_STCR_TXDIR | SSI_STCR_TSCKP | 980 SSI_STCR_TFSL | SSI_STCR_TFSI | SSI_STCR_TEFS | SSI_STCR_TXBIT0; 981 982 regmap_update_bits(ssi->regs, REG_SSI_STCR, mask, stcr); 983 regmap_update_bits(ssi->regs, REG_SSI_SRCR, mask, srcr); 984 985 mask = SSI_SCR_SYNC_TX_FS | SSI_SCR_I2S_MODE_MASK | 986 SSI_SCR_SYS_CLK_EN | SSI_SCR_SYN; 987 regmap_update_bits(ssi->regs, REG_SSI_SCR, mask, scr); 988 989 return 0; 990 } 991 992 /** 993 * Configure Digital Audio Interface (DAI) Format 994 */ 995 static int fsl_ssi_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt) 996 { 997 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai); 998 999 /* AC97 configured DAIFMT earlier in the probe() */ 1000 if (fsl_ssi_is_ac97(ssi)) 1001 return 0; 1002 1003 return _fsl_ssi_set_dai_fmt(ssi, fmt); 1004 } 1005 1006 /** 1007 * Set TDM slot number and slot width 1008 */ 1009 static int fsl_ssi_set_dai_tdm_slot(struct snd_soc_dai *dai, u32 tx_mask, 1010 u32 rx_mask, int slots, int slot_width) 1011 { 1012 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai); 1013 struct regmap *regs = ssi->regs; 1014 u32 val; 1015 1016 /* The word length should be 8, 10, 12, 16, 18, 20, 22 or 24 */ 1017 if (slot_width & 1 || slot_width < 8 || slot_width > 24) { 1018 dev_err(dai->dev, "invalid slot width: %d\n", slot_width); 1019 return -EINVAL; 1020 } 1021 1022 /* The slot number should be >= 2 if using Network mode or I2S mode */ 1023 if (ssi->i2s_net && slots < 2) { 1024 dev_err(dai->dev, "slot number should be >= 2 in I2S or NET\n"); 1025 return -EINVAL; 1026 } 1027 1028 regmap_update_bits(regs, REG_SSI_STCCR, 1029 SSI_SxCCR_DC_MASK, SSI_SxCCR_DC(slots)); 1030 regmap_update_bits(regs, REG_SSI_SRCCR, 1031 SSI_SxCCR_DC_MASK, SSI_SxCCR_DC(slots)); 1032 1033 /* Save the SCR register value */ 1034 regmap_read(regs, REG_SSI_SCR, &val); 1035 /* Temporarily enable SSI to allow SxMSKs to be configurable */ 1036 regmap_update_bits(regs, REG_SSI_SCR, SSI_SCR_SSIEN, SSI_SCR_SSIEN); 1037 1038 regmap_write(regs, REG_SSI_STMSK, ~tx_mask); 1039 regmap_write(regs, REG_SSI_SRMSK, ~rx_mask); 1040 1041 /* Restore the value of SSIEN bit */ 1042 regmap_update_bits(regs, REG_SSI_SCR, SSI_SCR_SSIEN, val); 1043 1044 ssi->slot_width = slot_width; 1045 ssi->slots = slots; 1046 1047 return 0; 1048 } 1049 1050 /** 1051 * Start or stop SSI and corresponding DMA transaction. 1052 * 1053 * The DMA channel is in external master start and pause mode, which 1054 * means the SSI completely controls the flow of data. 1055 */ 1056 static int fsl_ssi_trigger(struct snd_pcm_substream *substream, int cmd, 1057 struct snd_soc_dai *dai) 1058 { 1059 struct snd_soc_pcm_runtime *rtd = substream->private_data; 1060 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(rtd->cpu_dai); 1061 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 1062 1063 switch (cmd) { 1064 case SNDRV_PCM_TRIGGER_START: 1065 case SNDRV_PCM_TRIGGER_RESUME: 1066 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 1067 /* 1068 * SACCST might be modified via AC Link by a CODEC if it sends 1069 * extra bits in their SLOTREQ requests, which'll accidentally 1070 * send valid data to slots other than normal playback slots. 1071 * 1072 * To be safe, configure SACCST right before TX starts. 1073 */ 1074 if (tx && fsl_ssi_is_ac97(ssi)) 1075 fsl_ssi_tx_ac97_saccst_setup(ssi); 1076 fsl_ssi_config_enable(ssi, tx); 1077 break; 1078 1079 case SNDRV_PCM_TRIGGER_STOP: 1080 case SNDRV_PCM_TRIGGER_SUSPEND: 1081 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 1082 fsl_ssi_config_disable(ssi, tx); 1083 break; 1084 1085 default: 1086 return -EINVAL; 1087 } 1088 1089 return 0; 1090 } 1091 1092 static int fsl_ssi_dai_probe(struct snd_soc_dai *dai) 1093 { 1094 struct fsl_ssi *ssi = snd_soc_dai_get_drvdata(dai); 1095 1096 if (ssi->soc->imx && ssi->use_dma) 1097 snd_soc_dai_init_dma_data(dai, &ssi->dma_params_tx, 1098 &ssi->dma_params_rx); 1099 1100 return 0; 1101 } 1102 1103 static const struct snd_soc_dai_ops fsl_ssi_dai_ops = { 1104 .startup = fsl_ssi_startup, 1105 .shutdown = fsl_ssi_shutdown, 1106 .hw_params = fsl_ssi_hw_params, 1107 .hw_free = fsl_ssi_hw_free, 1108 .set_fmt = fsl_ssi_set_dai_fmt, 1109 .set_tdm_slot = fsl_ssi_set_dai_tdm_slot, 1110 .trigger = fsl_ssi_trigger, 1111 }; 1112 1113 static struct snd_soc_dai_driver fsl_ssi_dai_template = { 1114 .probe = fsl_ssi_dai_probe, 1115 .playback = { 1116 .stream_name = "CPU-Playback", 1117 .channels_min = 1, 1118 .channels_max = 32, 1119 .rates = SNDRV_PCM_RATE_CONTINUOUS, 1120 .formats = FSLSSI_I2S_FORMATS, 1121 }, 1122 .capture = { 1123 .stream_name = "CPU-Capture", 1124 .channels_min = 1, 1125 .channels_max = 32, 1126 .rates = SNDRV_PCM_RATE_CONTINUOUS, 1127 .formats = FSLSSI_I2S_FORMATS, 1128 }, 1129 .ops = &fsl_ssi_dai_ops, 1130 }; 1131 1132 static const struct snd_soc_component_driver fsl_ssi_component = { 1133 .name = "fsl-ssi", 1134 }; 1135 1136 static struct snd_soc_dai_driver fsl_ssi_ac97_dai = { 1137 .bus_control = true, 1138 .symmetric_channels = 1, 1139 .probe = fsl_ssi_dai_probe, 1140 .playback = { 1141 .stream_name = "AC97 Playback", 1142 .channels_min = 2, 1143 .channels_max = 2, 1144 .rates = SNDRV_PCM_RATE_8000_48000, 1145 .formats = SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_S20, 1146 }, 1147 .capture = { 1148 .stream_name = "AC97 Capture", 1149 .channels_min = 2, 1150 .channels_max = 2, 1151 .rates = SNDRV_PCM_RATE_48000, 1152 /* 16-bit capture is broken (errata ERR003778) */ 1153 .formats = SNDRV_PCM_FMTBIT_S20, 1154 }, 1155 .ops = &fsl_ssi_dai_ops, 1156 }; 1157 1158 static struct fsl_ssi *fsl_ac97_data; 1159 1160 static void fsl_ssi_ac97_write(struct snd_ac97 *ac97, unsigned short reg, 1161 unsigned short val) 1162 { 1163 struct regmap *regs = fsl_ac97_data->regs; 1164 unsigned int lreg; 1165 unsigned int lval; 1166 int ret; 1167 1168 if (reg > 0x7f) 1169 return; 1170 1171 mutex_lock(&fsl_ac97_data->ac97_reg_lock); 1172 1173 ret = clk_prepare_enable(fsl_ac97_data->clk); 1174 if (ret) { 1175 pr_err("ac97 write clk_prepare_enable failed: %d\n", 1176 ret); 1177 goto ret_unlock; 1178 } 1179 1180 lreg = reg << 12; 1181 regmap_write(regs, REG_SSI_SACADD, lreg); 1182 1183 lval = val << 4; 1184 regmap_write(regs, REG_SSI_SACDAT, lval); 1185 1186 regmap_update_bits(regs, REG_SSI_SACNT, 1187 SSI_SACNT_RDWR_MASK, SSI_SACNT_WR); 1188 udelay(100); 1189 1190 clk_disable_unprepare(fsl_ac97_data->clk); 1191 1192 ret_unlock: 1193 mutex_unlock(&fsl_ac97_data->ac97_reg_lock); 1194 } 1195 1196 static unsigned short fsl_ssi_ac97_read(struct snd_ac97 *ac97, 1197 unsigned short reg) 1198 { 1199 struct regmap *regs = fsl_ac97_data->regs; 1200 unsigned short val = 0; 1201 u32 reg_val; 1202 unsigned int lreg; 1203 int ret; 1204 1205 mutex_lock(&fsl_ac97_data->ac97_reg_lock); 1206 1207 ret = clk_prepare_enable(fsl_ac97_data->clk); 1208 if (ret) { 1209 pr_err("ac97 read clk_prepare_enable failed: %d\n", ret); 1210 goto ret_unlock; 1211 } 1212 1213 lreg = (reg & 0x7f) << 12; 1214 regmap_write(regs, REG_SSI_SACADD, lreg); 1215 regmap_update_bits(regs, REG_SSI_SACNT, 1216 SSI_SACNT_RDWR_MASK, SSI_SACNT_RD); 1217 1218 udelay(100); 1219 1220 regmap_read(regs, REG_SSI_SACDAT, ®_val); 1221 val = (reg_val >> 4) & 0xffff; 1222 1223 clk_disable_unprepare(fsl_ac97_data->clk); 1224 1225 ret_unlock: 1226 mutex_unlock(&fsl_ac97_data->ac97_reg_lock); 1227 return val; 1228 } 1229 1230 static struct snd_ac97_bus_ops fsl_ssi_ac97_ops = { 1231 .read = fsl_ssi_ac97_read, 1232 .write = fsl_ssi_ac97_write, 1233 }; 1234 1235 /** 1236 * Initialize SSI registers 1237 */ 1238 static int fsl_ssi_hw_init(struct fsl_ssi *ssi) 1239 { 1240 u32 wm = ssi->fifo_watermark; 1241 1242 /* Initialize regvals */ 1243 fsl_ssi_setup_regvals(ssi); 1244 1245 /* Set watermarks */ 1246 regmap_write(ssi->regs, REG_SSI_SFCSR, 1247 SSI_SFCSR_TFWM0(wm) | SSI_SFCSR_RFWM0(wm) | 1248 SSI_SFCSR_TFWM1(wm) | SSI_SFCSR_RFWM1(wm)); 1249 1250 /* Enable Dual FIFO mode */ 1251 if (ssi->use_dual_fifo) 1252 regmap_update_bits(ssi->regs, REG_SSI_SCR, 1253 SSI_SCR_TCH_EN, SSI_SCR_TCH_EN); 1254 1255 /* AC97 should start earlier to communicate with CODECs */ 1256 if (fsl_ssi_is_ac97(ssi)) { 1257 _fsl_ssi_set_dai_fmt(ssi, ssi->dai_fmt); 1258 fsl_ssi_setup_ac97(ssi); 1259 } 1260 1261 return 0; 1262 } 1263 1264 /** 1265 * Clear SSI registers 1266 */ 1267 static void fsl_ssi_hw_clean(struct fsl_ssi *ssi) 1268 { 1269 /* Disable registers for AC97 */ 1270 if (fsl_ssi_is_ac97(ssi)) { 1271 /* Disable TE and RE bits first */ 1272 regmap_update_bits(ssi->regs, REG_SSI_SCR, 1273 SSI_SCR_TE | SSI_SCR_RE, 0); 1274 /* Disable AC97 mode */ 1275 regmap_write(ssi->regs, REG_SSI_SACNT, 0); 1276 /* Unset WAIT bits */ 1277 regmap_write(ssi->regs, REG_SSI_SOR, 0); 1278 /* Disable SSI -- software reset */ 1279 regmap_update_bits(ssi->regs, REG_SSI_SCR, SSI_SCR_SSIEN, 0); 1280 } 1281 } 1282 /** 1283 * Make every character in a string lower-case 1284 */ 1285 static void make_lowercase(char *s) 1286 { 1287 if (!s) 1288 return; 1289 for (; *s; s++) 1290 *s = tolower(*s); 1291 } 1292 1293 static int fsl_ssi_imx_probe(struct platform_device *pdev, 1294 struct fsl_ssi *ssi, void __iomem *iomem) 1295 { 1296 struct device *dev = &pdev->dev; 1297 int ret; 1298 1299 /* Backward compatible for a DT without ipg clock name assigned */ 1300 if (ssi->has_ipg_clk_name) 1301 ssi->clk = devm_clk_get(dev, "ipg"); 1302 else 1303 ssi->clk = devm_clk_get(dev, NULL); 1304 if (IS_ERR(ssi->clk)) { 1305 ret = PTR_ERR(ssi->clk); 1306 dev_err(dev, "failed to get clock: %d\n", ret); 1307 return ret; 1308 } 1309 1310 /* Enable the clock since regmap will not handle it in this case */ 1311 if (!ssi->has_ipg_clk_name) { 1312 ret = clk_prepare_enable(ssi->clk); 1313 if (ret) { 1314 dev_err(dev, "clk_prepare_enable failed: %d\n", ret); 1315 return ret; 1316 } 1317 } 1318 1319 /* Do not error out for slave cases that live without a baud clock */ 1320 ssi->baudclk = devm_clk_get(dev, "baud"); 1321 if (IS_ERR(ssi->baudclk)) 1322 dev_dbg(dev, "failed to get baud clock: %ld\n", 1323 PTR_ERR(ssi->baudclk)); 1324 1325 ssi->dma_params_tx.maxburst = ssi->dma_maxburst; 1326 ssi->dma_params_rx.maxburst = ssi->dma_maxburst; 1327 ssi->dma_params_tx.addr = ssi->ssi_phys + REG_SSI_STX0; 1328 ssi->dma_params_rx.addr = ssi->ssi_phys + REG_SSI_SRX0; 1329 1330 /* Use even numbers to avoid channel swap due to SDMA script design */ 1331 if (ssi->use_dual_fifo) { 1332 ssi->dma_params_tx.maxburst &= ~0x1; 1333 ssi->dma_params_rx.maxburst &= ~0x1; 1334 } 1335 1336 if (!ssi->use_dma) { 1337 /* 1338 * Some boards use an incompatible codec. Use imx-fiq-pcm-audio 1339 * to get it working, as DMA is not possible in this situation. 1340 */ 1341 ssi->fiq_params.irq = ssi->irq; 1342 ssi->fiq_params.base = iomem; 1343 ssi->fiq_params.dma_params_rx = &ssi->dma_params_rx; 1344 ssi->fiq_params.dma_params_tx = &ssi->dma_params_tx; 1345 1346 ret = imx_pcm_fiq_init(pdev, &ssi->fiq_params); 1347 if (ret) 1348 goto error_pcm; 1349 } else { 1350 ret = imx_pcm_dma_init(pdev, IMX_SSI_DMABUF_SIZE); 1351 if (ret) 1352 goto error_pcm; 1353 } 1354 1355 return 0; 1356 1357 error_pcm: 1358 if (!ssi->has_ipg_clk_name) 1359 clk_disable_unprepare(ssi->clk); 1360 1361 return ret; 1362 } 1363 1364 static void fsl_ssi_imx_clean(struct platform_device *pdev, struct fsl_ssi *ssi) 1365 { 1366 if (!ssi->use_dma) 1367 imx_pcm_fiq_exit(pdev); 1368 if (!ssi->has_ipg_clk_name) 1369 clk_disable_unprepare(ssi->clk); 1370 } 1371 1372 static int fsl_ssi_probe_from_dt(struct fsl_ssi *ssi) 1373 { 1374 struct device *dev = ssi->dev; 1375 struct device_node *np = dev->of_node; 1376 const struct of_device_id *of_id; 1377 const char *p, *sprop; 1378 const __be32 *iprop; 1379 u32 dmas[4]; 1380 int ret; 1381 1382 of_id = of_match_device(fsl_ssi_ids, dev); 1383 if (!of_id || !of_id->data) 1384 return -EINVAL; 1385 1386 ssi->soc = of_id->data; 1387 1388 ret = of_property_match_string(np, "clock-names", "ipg"); 1389 /* Get error code if not found */ 1390 ssi->has_ipg_clk_name = ret >= 0; 1391 1392 /* Check if being used in AC97 mode */ 1393 sprop = of_get_property(np, "fsl,mode", NULL); 1394 if (sprop && !strcmp(sprop, "ac97-slave")) { 1395 ssi->dai_fmt = FSLSSI_AC97_DAIFMT; 1396 1397 ret = of_property_read_u32(np, "cell-index", &ssi->card_idx); 1398 if (ret) { 1399 dev_err(dev, "failed to get SSI index property\n"); 1400 return -EINVAL; 1401 } 1402 strcpy(ssi->card_name, "ac97-codec"); 1403 } else if (!of_find_property(np, "fsl,ssi-asynchronous", NULL)) { 1404 /* 1405 * In synchronous mode, STCK and STFS ports are used by RX 1406 * as well. So the software should limit the sample rates, 1407 * sample bits and channels to be symmetric. 1408 * 1409 * This is exclusive with FSLSSI_AC97_FORMATS as AC97 runs 1410 * in the SSI synchronous mode however it does not have to 1411 * limit symmetric sample rates and sample bits. 1412 */ 1413 ssi->synchronous = true; 1414 } 1415 1416 /* Select DMA or FIQ */ 1417 ssi->use_dma = !of_property_read_bool(np, "fsl,fiq-stream-filter"); 1418 1419 /* Fetch FIFO depth; Set to 8 for older DT without this property */ 1420 iprop = of_get_property(np, "fsl,fifo-depth", NULL); 1421 if (iprop) 1422 ssi->fifo_depth = be32_to_cpup(iprop); 1423 else 1424 ssi->fifo_depth = 8; 1425 1426 /* Use dual FIFO mode depending on the support from SDMA script */ 1427 ret = of_property_read_u32_array(np, "dmas", dmas, 4); 1428 if (ssi->use_dma && !ret && dmas[2] == IMX_DMATYPE_SSI_DUAL) 1429 ssi->use_dual_fifo = true; 1430 1431 /* 1432 * Backward compatible for older bindings by manually triggering the 1433 * machine driver's probe(). Use /compatible property, including the 1434 * address of CPU DAI driver structure, as the name of machine driver 1435 * 1436 * If card_name is set by AC97 earlier, bypass here since it uses a 1437 * different name to register the device. 1438 */ 1439 if (!ssi->card_name[0] && of_get_property(np, "codec-handle", NULL)) { 1440 sprop = of_get_property(of_find_node_by_path("/"), 1441 "compatible", NULL); 1442 /* Strip "fsl," in the compatible name if applicable */ 1443 p = strrchr(sprop, ','); 1444 if (p) 1445 sprop = p + 1; 1446 snprintf(ssi->card_name, sizeof(ssi->card_name), 1447 "snd-soc-%s", sprop); 1448 make_lowercase(ssi->card_name); 1449 ssi->card_idx = 0; 1450 } 1451 1452 return 0; 1453 } 1454 1455 static int fsl_ssi_probe(struct platform_device *pdev) 1456 { 1457 struct regmap_config regconfig = fsl_ssi_regconfig; 1458 struct device *dev = &pdev->dev; 1459 struct fsl_ssi *ssi; 1460 struct resource *res; 1461 void __iomem *iomem; 1462 int ret = 0; 1463 1464 ssi = devm_kzalloc(dev, sizeof(*ssi), GFP_KERNEL); 1465 if (!ssi) 1466 return -ENOMEM; 1467 1468 ssi->dev = dev; 1469 1470 /* Probe from DT */ 1471 ret = fsl_ssi_probe_from_dt(ssi); 1472 if (ret) 1473 return ret; 1474 1475 if (fsl_ssi_is_ac97(ssi)) { 1476 memcpy(&ssi->cpu_dai_drv, &fsl_ssi_ac97_dai, 1477 sizeof(fsl_ssi_ac97_dai)); 1478 fsl_ac97_data = ssi; 1479 } else { 1480 memcpy(&ssi->cpu_dai_drv, &fsl_ssi_dai_template, 1481 sizeof(fsl_ssi_dai_template)); 1482 } 1483 ssi->cpu_dai_drv.name = dev_name(dev); 1484 1485 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1486 iomem = devm_ioremap_resource(dev, res); 1487 if (IS_ERR(iomem)) 1488 return PTR_ERR(iomem); 1489 ssi->ssi_phys = res->start; 1490 1491 if (ssi->soc->imx21regs) { 1492 /* No SACC{ST,EN,DIS} regs in imx21-class SSI */ 1493 regconfig.max_register = REG_SSI_SRMSK; 1494 regconfig.num_reg_defaults_raw = 1495 REG_SSI_SRMSK / sizeof(uint32_t) + 1; 1496 } 1497 1498 if (ssi->has_ipg_clk_name) 1499 ssi->regs = devm_regmap_init_mmio_clk(dev, "ipg", iomem, 1500 ®config); 1501 else 1502 ssi->regs = devm_regmap_init_mmio(dev, iomem, ®config); 1503 if (IS_ERR(ssi->regs)) { 1504 dev_err(dev, "failed to init register map\n"); 1505 return PTR_ERR(ssi->regs); 1506 } 1507 1508 ssi->irq = platform_get_irq(pdev, 0); 1509 if (ssi->irq < 0) { 1510 dev_err(dev, "no irq for node %s\n", pdev->name); 1511 return ssi->irq; 1512 } 1513 1514 /* Set software limitations for synchronous mode except AC97 */ 1515 if (ssi->synchronous && !fsl_ssi_is_ac97(ssi)) { 1516 ssi->cpu_dai_drv.symmetric_rates = 1; 1517 ssi->cpu_dai_drv.symmetric_channels = 1; 1518 ssi->cpu_dai_drv.symmetric_samplebits = 1; 1519 } 1520 1521 /* 1522 * Configure TX and RX DMA watermarks -- when to send a DMA request 1523 * 1524 * Values should be tested to avoid FIFO under/over run. Set maxburst 1525 * to fifo_watermark to maxiumize DMA transaction to reduce overhead. 1526 */ 1527 switch (ssi->fifo_depth) { 1528 case 15: 1529 /* 1530 * Set to 8 as a balanced configuration -- When TX FIFO has 8 1531 * empty slots, send a DMA request to fill these 8 slots. The 1532 * remaining 7 slots should be able to allow DMA to finish the 1533 * transaction before TX FIFO underruns; Same applies to RX. 1534 * 1535 * Tested with cases running at 48kHz @ 16 bits x 16 channels 1536 */ 1537 ssi->fifo_watermark = 8; 1538 ssi->dma_maxburst = 8; 1539 break; 1540 case 8: 1541 default: 1542 /* Safely use old watermark configurations for older chips */ 1543 ssi->fifo_watermark = ssi->fifo_depth - 2; 1544 ssi->dma_maxburst = ssi->fifo_depth - 2; 1545 break; 1546 } 1547 1548 dev_set_drvdata(dev, ssi); 1549 1550 if (ssi->soc->imx) { 1551 ret = fsl_ssi_imx_probe(pdev, ssi, iomem); 1552 if (ret) 1553 return ret; 1554 } 1555 1556 if (fsl_ssi_is_ac97(ssi)) { 1557 mutex_init(&ssi->ac97_reg_lock); 1558 ret = snd_soc_set_ac97_ops_of_reset(&fsl_ssi_ac97_ops, pdev); 1559 if (ret) { 1560 dev_err(dev, "failed to set AC'97 ops\n"); 1561 goto error_ac97_ops; 1562 } 1563 } 1564 1565 ret = devm_snd_soc_register_component(dev, &fsl_ssi_component, 1566 &ssi->cpu_dai_drv, 1); 1567 if (ret) { 1568 dev_err(dev, "failed to register DAI: %d\n", ret); 1569 goto error_asoc_register; 1570 } 1571 1572 if (ssi->use_dma) { 1573 ret = devm_request_irq(dev, ssi->irq, fsl_ssi_isr, 0, 1574 dev_name(dev), ssi); 1575 if (ret < 0) { 1576 dev_err(dev, "failed to claim irq %u\n", ssi->irq); 1577 goto error_asoc_register; 1578 } 1579 } 1580 1581 ret = fsl_ssi_debugfs_create(&ssi->dbg_stats, dev); 1582 if (ret) 1583 goto error_asoc_register; 1584 1585 /* Initially configures SSI registers */ 1586 fsl_ssi_hw_init(ssi); 1587 1588 /* Register a platform device for older bindings or AC97 */ 1589 if (ssi->card_name[0]) { 1590 struct device *parent = dev; 1591 /* 1592 * Do not set SSI dev as the parent of AC97 CODEC device since 1593 * it does not have a DT node. Otherwise ASoC core will assume 1594 * CODEC has the same DT node as the SSI, so it may bypass the 1595 * dai_probe() of SSI and then cause NULL DMA data pointers. 1596 */ 1597 if (fsl_ssi_is_ac97(ssi)) 1598 parent = NULL; 1599 1600 ssi->card_pdev = platform_device_register_data(parent, 1601 ssi->card_name, ssi->card_idx, NULL, 0); 1602 if (IS_ERR(ssi->card_pdev)) { 1603 ret = PTR_ERR(ssi->card_pdev); 1604 dev_err(dev, "failed to register %s: %d\n", 1605 ssi->card_name, ret); 1606 goto error_sound_card; 1607 } 1608 } 1609 1610 return 0; 1611 1612 error_sound_card: 1613 fsl_ssi_debugfs_remove(&ssi->dbg_stats); 1614 error_asoc_register: 1615 if (fsl_ssi_is_ac97(ssi)) 1616 snd_soc_set_ac97_ops(NULL); 1617 error_ac97_ops: 1618 if (fsl_ssi_is_ac97(ssi)) 1619 mutex_destroy(&ssi->ac97_reg_lock); 1620 1621 if (ssi->soc->imx) 1622 fsl_ssi_imx_clean(pdev, ssi); 1623 1624 return ret; 1625 } 1626 1627 static int fsl_ssi_remove(struct platform_device *pdev) 1628 { 1629 struct fsl_ssi *ssi = dev_get_drvdata(&pdev->dev); 1630 1631 fsl_ssi_debugfs_remove(&ssi->dbg_stats); 1632 1633 if (ssi->card_pdev) 1634 platform_device_unregister(ssi->card_pdev); 1635 1636 /* Clean up SSI registers */ 1637 fsl_ssi_hw_clean(ssi); 1638 1639 if (ssi->soc->imx) 1640 fsl_ssi_imx_clean(pdev, ssi); 1641 1642 if (fsl_ssi_is_ac97(ssi)) { 1643 snd_soc_set_ac97_ops(NULL); 1644 mutex_destroy(&ssi->ac97_reg_lock); 1645 } 1646 1647 return 0; 1648 } 1649 1650 #ifdef CONFIG_PM_SLEEP 1651 static int fsl_ssi_suspend(struct device *dev) 1652 { 1653 struct fsl_ssi *ssi = dev_get_drvdata(dev); 1654 struct regmap *regs = ssi->regs; 1655 1656 regmap_read(regs, REG_SSI_SFCSR, &ssi->regcache_sfcsr); 1657 regmap_read(regs, REG_SSI_SACNT, &ssi->regcache_sacnt); 1658 1659 regcache_cache_only(regs, true); 1660 regcache_mark_dirty(regs); 1661 1662 return 0; 1663 } 1664 1665 static int fsl_ssi_resume(struct device *dev) 1666 { 1667 struct fsl_ssi *ssi = dev_get_drvdata(dev); 1668 struct regmap *regs = ssi->regs; 1669 1670 regcache_cache_only(regs, false); 1671 1672 regmap_update_bits(regs, REG_SSI_SFCSR, 1673 SSI_SFCSR_RFWM1_MASK | SSI_SFCSR_TFWM1_MASK | 1674 SSI_SFCSR_RFWM0_MASK | SSI_SFCSR_TFWM0_MASK, 1675 ssi->regcache_sfcsr); 1676 regmap_write(regs, REG_SSI_SACNT, ssi->regcache_sacnt); 1677 1678 return regcache_sync(regs); 1679 } 1680 #endif /* CONFIG_PM_SLEEP */ 1681 1682 static const struct dev_pm_ops fsl_ssi_pm = { 1683 SET_SYSTEM_SLEEP_PM_OPS(fsl_ssi_suspend, fsl_ssi_resume) 1684 }; 1685 1686 static struct platform_driver fsl_ssi_driver = { 1687 .driver = { 1688 .name = "fsl-ssi-dai", 1689 .of_match_table = fsl_ssi_ids, 1690 .pm = &fsl_ssi_pm, 1691 }, 1692 .probe = fsl_ssi_probe, 1693 .remove = fsl_ssi_remove, 1694 }; 1695 1696 module_platform_driver(fsl_ssi_driver); 1697 1698 MODULE_ALIAS("platform:fsl-ssi-dai"); 1699 MODULE_AUTHOR("Timur Tabi <timur@freescale.com>"); 1700 MODULE_DESCRIPTION("Freescale Synchronous Serial Interface (SSI) ASoC Driver"); 1701 MODULE_LICENSE("GPL v2"); 1702