1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * atmel_ssc_dai.c -- ALSA SoC ATMEL SSC Audio Layer Platform driver 4 * 5 * Copyright (C) 2005 SAN People 6 * Copyright (C) 2008 Atmel 7 * 8 * Author: Sedji Gaouaou <sedji.gaouaou@atmel.com> 9 * ATMEL CORP. 10 * 11 * Based on at91-ssc.c by 12 * Frank Mandarino <fmandarino@endrelia.com> 13 * Based on pxa2xx Platform drivers by 14 * Liam Girdwood <lrg@slimlogic.co.uk> 15 */ 16 17 #include <linux/init.h> 18 #include <linux/module.h> 19 #include <linux/interrupt.h> 20 #include <linux/device.h> 21 #include <linux/delay.h> 22 #include <linux/clk.h> 23 #include <linux/atmel_pdc.h> 24 25 #include <linux/atmel-ssc.h> 26 #include <sound/core.h> 27 #include <sound/pcm.h> 28 #include <sound/pcm_params.h> 29 #include <sound/initval.h> 30 #include <sound/soc.h> 31 32 #include "atmel-pcm.h" 33 #include "atmel_ssc_dai.h" 34 35 36 #define NUM_SSC_DEVICES 3 37 38 /* 39 * SSC PDC registers required by the PCM DMA engine. 40 */ 41 static struct atmel_pdc_regs pdc_tx_reg = { 42 .xpr = ATMEL_PDC_TPR, 43 .xcr = ATMEL_PDC_TCR, 44 .xnpr = ATMEL_PDC_TNPR, 45 .xncr = ATMEL_PDC_TNCR, 46 }; 47 48 static struct atmel_pdc_regs pdc_rx_reg = { 49 .xpr = ATMEL_PDC_RPR, 50 .xcr = ATMEL_PDC_RCR, 51 .xnpr = ATMEL_PDC_RNPR, 52 .xncr = ATMEL_PDC_RNCR, 53 }; 54 55 /* 56 * SSC & PDC status bits for transmit and receive. 57 */ 58 static struct atmel_ssc_mask ssc_tx_mask = { 59 .ssc_enable = SSC_BIT(CR_TXEN), 60 .ssc_disable = SSC_BIT(CR_TXDIS), 61 .ssc_endx = SSC_BIT(SR_ENDTX), 62 .ssc_endbuf = SSC_BIT(SR_TXBUFE), 63 .ssc_error = SSC_BIT(SR_OVRUN), 64 .pdc_enable = ATMEL_PDC_TXTEN, 65 .pdc_disable = ATMEL_PDC_TXTDIS, 66 }; 67 68 static struct atmel_ssc_mask ssc_rx_mask = { 69 .ssc_enable = SSC_BIT(CR_RXEN), 70 .ssc_disable = SSC_BIT(CR_RXDIS), 71 .ssc_endx = SSC_BIT(SR_ENDRX), 72 .ssc_endbuf = SSC_BIT(SR_RXBUFF), 73 .ssc_error = SSC_BIT(SR_OVRUN), 74 .pdc_enable = ATMEL_PDC_RXTEN, 75 .pdc_disable = ATMEL_PDC_RXTDIS, 76 }; 77 78 79 /* 80 * DMA parameters. 81 */ 82 static struct atmel_pcm_dma_params ssc_dma_params[NUM_SSC_DEVICES][2] = { 83 {{ 84 .name = "SSC0 PCM out", 85 .pdc = &pdc_tx_reg, 86 .mask = &ssc_tx_mask, 87 }, 88 { 89 .name = "SSC0 PCM in", 90 .pdc = &pdc_rx_reg, 91 .mask = &ssc_rx_mask, 92 } }, 93 {{ 94 .name = "SSC1 PCM out", 95 .pdc = &pdc_tx_reg, 96 .mask = &ssc_tx_mask, 97 }, 98 { 99 .name = "SSC1 PCM in", 100 .pdc = &pdc_rx_reg, 101 .mask = &ssc_rx_mask, 102 } }, 103 {{ 104 .name = "SSC2 PCM out", 105 .pdc = &pdc_tx_reg, 106 .mask = &ssc_tx_mask, 107 }, 108 { 109 .name = "SSC2 PCM in", 110 .pdc = &pdc_rx_reg, 111 .mask = &ssc_rx_mask, 112 } }, 113 }; 114 115 116 static struct atmel_ssc_info ssc_info[NUM_SSC_DEVICES] = { 117 { 118 .name = "ssc0", 119 .lock = __SPIN_LOCK_UNLOCKED(ssc_info[0].lock), 120 .dir_mask = SSC_DIR_MASK_UNUSED, 121 .initialized = 0, 122 }, 123 { 124 .name = "ssc1", 125 .lock = __SPIN_LOCK_UNLOCKED(ssc_info[1].lock), 126 .dir_mask = SSC_DIR_MASK_UNUSED, 127 .initialized = 0, 128 }, 129 { 130 .name = "ssc2", 131 .lock = __SPIN_LOCK_UNLOCKED(ssc_info[2].lock), 132 .dir_mask = SSC_DIR_MASK_UNUSED, 133 .initialized = 0, 134 }, 135 }; 136 137 138 /* 139 * SSC interrupt handler. Passes PDC interrupts to the DMA 140 * interrupt handler in the PCM driver. 141 */ 142 static irqreturn_t atmel_ssc_interrupt(int irq, void *dev_id) 143 { 144 struct atmel_ssc_info *ssc_p = dev_id; 145 struct atmel_pcm_dma_params *dma_params; 146 u32 ssc_sr; 147 u32 ssc_substream_mask; 148 int i; 149 150 ssc_sr = (unsigned long)ssc_readl(ssc_p->ssc->regs, SR) 151 & (unsigned long)ssc_readl(ssc_p->ssc->regs, IMR); 152 153 /* 154 * Loop through the substreams attached to this SSC. If 155 * a DMA-related interrupt occurred on that substream, call 156 * the DMA interrupt handler function, if one has been 157 * registered in the dma_params structure by the PCM driver. 158 */ 159 for (i = 0; i < ARRAY_SIZE(ssc_p->dma_params); i++) { 160 dma_params = ssc_p->dma_params[i]; 161 162 if ((dma_params != NULL) && 163 (dma_params->dma_intr_handler != NULL)) { 164 ssc_substream_mask = (dma_params->mask->ssc_endx | 165 dma_params->mask->ssc_endbuf); 166 if (ssc_sr & ssc_substream_mask) { 167 dma_params->dma_intr_handler(ssc_sr, 168 dma_params-> 169 substream); 170 } 171 } 172 } 173 174 return IRQ_HANDLED; 175 } 176 177 /* 178 * When the bit clock is input, limit the maximum rate according to the 179 * Serial Clock Ratio Considerations section from the SSC documentation: 180 * 181 * The Transmitter and the Receiver can be programmed to operate 182 * with the clock signals provided on either the TK or RK pins. 183 * This allows the SSC to support many slave-mode data transfers. 184 * In this case, the maximum clock speed allowed on the RK pin is: 185 * - Peripheral clock divided by 2 if Receiver Frame Synchro is input 186 * - Peripheral clock divided by 3 if Receiver Frame Synchro is output 187 * In addition, the maximum clock speed allowed on the TK pin is: 188 * - Peripheral clock divided by 6 if Transmit Frame Synchro is input 189 * - Peripheral clock divided by 2 if Transmit Frame Synchro is output 190 * 191 * When the bit clock is output, limit the rate according to the 192 * SSC divider restrictions. 193 */ 194 static int atmel_ssc_hw_rule_rate(struct snd_pcm_hw_params *params, 195 struct snd_pcm_hw_rule *rule) 196 { 197 struct atmel_ssc_info *ssc_p = rule->private; 198 struct ssc_device *ssc = ssc_p->ssc; 199 struct snd_interval *i = hw_param_interval(params, rule->var); 200 struct snd_interval t; 201 struct snd_ratnum r = { 202 .den_min = 1, 203 .den_max = 4095, 204 .den_step = 1, 205 }; 206 unsigned int num = 0, den = 0; 207 int frame_size; 208 int mck_div = 2; 209 int ret; 210 211 frame_size = snd_soc_params_to_frame_size(params); 212 if (frame_size < 0) 213 return frame_size; 214 215 switch (ssc_p->daifmt & SND_SOC_DAIFMT_MASTER_MASK) { 216 case SND_SOC_DAIFMT_CBM_CFS: 217 if ((ssc_p->dir_mask & SSC_DIR_MASK_CAPTURE) 218 && ssc->clk_from_rk_pin) 219 /* Receiver Frame Synchro (i.e. capture) 220 * is output (format is _CFS) and the RK pin 221 * is used for input (format is _CBM_). 222 */ 223 mck_div = 3; 224 break; 225 226 case SND_SOC_DAIFMT_CBM_CFM: 227 if ((ssc_p->dir_mask & SSC_DIR_MASK_PLAYBACK) 228 && !ssc->clk_from_rk_pin) 229 /* Transmit Frame Synchro (i.e. playback) 230 * is input (format is _CFM) and the TK pin 231 * is used for input (format _CBM_ but not 232 * using the RK pin). 233 */ 234 mck_div = 6; 235 break; 236 } 237 238 switch (ssc_p->daifmt & SND_SOC_DAIFMT_MASTER_MASK) { 239 case SND_SOC_DAIFMT_CBS_CFS: 240 r.num = ssc_p->mck_rate / mck_div / frame_size; 241 242 ret = snd_interval_ratnum(i, 1, &r, &num, &den); 243 if (ret >= 0 && den && rule->var == SNDRV_PCM_HW_PARAM_RATE) { 244 params->rate_num = num; 245 params->rate_den = den; 246 } 247 break; 248 249 case SND_SOC_DAIFMT_CBM_CFS: 250 case SND_SOC_DAIFMT_CBM_CFM: 251 t.min = 8000; 252 t.max = ssc_p->mck_rate / mck_div / frame_size; 253 t.openmin = t.openmax = 0; 254 t.integer = 0; 255 ret = snd_interval_refine(i, &t); 256 break; 257 258 default: 259 ret = -EINVAL; 260 break; 261 } 262 263 return ret; 264 } 265 266 /*-------------------------------------------------------------------------*\ 267 * DAI functions 268 \*-------------------------------------------------------------------------*/ 269 /* 270 * Startup. Only that one substream allowed in each direction. 271 */ 272 static int atmel_ssc_startup(struct snd_pcm_substream *substream, 273 struct snd_soc_dai *dai) 274 { 275 struct platform_device *pdev = to_platform_device(dai->dev); 276 struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id]; 277 struct atmel_pcm_dma_params *dma_params; 278 int dir, dir_mask; 279 int ret; 280 281 pr_debug("atmel_ssc_startup: SSC_SR=0x%x\n", 282 ssc_readl(ssc_p->ssc->regs, SR)); 283 284 /* Enable PMC peripheral clock for this SSC */ 285 pr_debug("atmel_ssc_dai: Starting clock\n"); 286 clk_enable(ssc_p->ssc->clk); 287 ssc_p->mck_rate = clk_get_rate(ssc_p->ssc->clk); 288 289 /* Reset the SSC unless initialized to keep it in a clean state */ 290 if (!ssc_p->initialized) 291 ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_SWRST)); 292 293 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) { 294 dir = 0; 295 dir_mask = SSC_DIR_MASK_PLAYBACK; 296 } else { 297 dir = 1; 298 dir_mask = SSC_DIR_MASK_CAPTURE; 299 } 300 301 ret = snd_pcm_hw_rule_add(substream->runtime, 0, 302 SNDRV_PCM_HW_PARAM_RATE, 303 atmel_ssc_hw_rule_rate, 304 ssc_p, 305 SNDRV_PCM_HW_PARAM_FRAME_BITS, 306 SNDRV_PCM_HW_PARAM_CHANNELS, -1); 307 if (ret < 0) { 308 dev_err(dai->dev, "Failed to specify rate rule: %d\n", ret); 309 return ret; 310 } 311 312 dma_params = &ssc_dma_params[pdev->id][dir]; 313 dma_params->ssc = ssc_p->ssc; 314 dma_params->substream = substream; 315 316 ssc_p->dma_params[dir] = dma_params; 317 318 snd_soc_dai_set_dma_data(dai, substream, dma_params); 319 320 spin_lock_irq(&ssc_p->lock); 321 if (ssc_p->dir_mask & dir_mask) { 322 spin_unlock_irq(&ssc_p->lock); 323 return -EBUSY; 324 } 325 ssc_p->dir_mask |= dir_mask; 326 spin_unlock_irq(&ssc_p->lock); 327 328 return 0; 329 } 330 331 /* 332 * Shutdown. Clear DMA parameters and shutdown the SSC if there 333 * are no other substreams open. 334 */ 335 static void atmel_ssc_shutdown(struct snd_pcm_substream *substream, 336 struct snd_soc_dai *dai) 337 { 338 struct platform_device *pdev = to_platform_device(dai->dev); 339 struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id]; 340 struct atmel_pcm_dma_params *dma_params; 341 int dir, dir_mask; 342 343 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 344 dir = 0; 345 else 346 dir = 1; 347 348 dma_params = ssc_p->dma_params[dir]; 349 350 if (dma_params != NULL) { 351 dma_params->ssc = NULL; 352 dma_params->substream = NULL; 353 ssc_p->dma_params[dir] = NULL; 354 } 355 356 dir_mask = 1 << dir; 357 358 spin_lock_irq(&ssc_p->lock); 359 ssc_p->dir_mask &= ~dir_mask; 360 if (!ssc_p->dir_mask) { 361 if (ssc_p->initialized) { 362 free_irq(ssc_p->ssc->irq, ssc_p); 363 ssc_p->initialized = 0; 364 } 365 366 /* Reset the SSC */ 367 ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_SWRST)); 368 /* Clear the SSC dividers */ 369 ssc_p->cmr_div = ssc_p->tcmr_period = ssc_p->rcmr_period = 0; 370 ssc_p->forced_divider = 0; 371 } 372 spin_unlock_irq(&ssc_p->lock); 373 374 /* Shutdown the SSC clock. */ 375 pr_debug("atmel_ssc_dai: Stopping clock\n"); 376 clk_disable(ssc_p->ssc->clk); 377 } 378 379 380 /* 381 * Record the DAI format for use in hw_params(). 382 */ 383 static int atmel_ssc_set_dai_fmt(struct snd_soc_dai *cpu_dai, 384 unsigned int fmt) 385 { 386 struct platform_device *pdev = to_platform_device(cpu_dai->dev); 387 struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id]; 388 389 ssc_p->daifmt = fmt; 390 return 0; 391 } 392 393 /* 394 * Record SSC clock dividers for use in hw_params(). 395 */ 396 static int atmel_ssc_set_dai_clkdiv(struct snd_soc_dai *cpu_dai, 397 int div_id, int div) 398 { 399 struct platform_device *pdev = to_platform_device(cpu_dai->dev); 400 struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id]; 401 402 switch (div_id) { 403 case ATMEL_SSC_CMR_DIV: 404 /* 405 * The same master clock divider is used for both 406 * transmit and receive, so if a value has already 407 * been set, it must match this value. 408 */ 409 if (ssc_p->dir_mask != 410 (SSC_DIR_MASK_PLAYBACK | SSC_DIR_MASK_CAPTURE)) 411 ssc_p->cmr_div = div; 412 else if (ssc_p->cmr_div == 0) 413 ssc_p->cmr_div = div; 414 else 415 if (div != ssc_p->cmr_div) 416 return -EBUSY; 417 ssc_p->forced_divider |= BIT(ATMEL_SSC_CMR_DIV); 418 break; 419 420 case ATMEL_SSC_TCMR_PERIOD: 421 ssc_p->tcmr_period = div; 422 ssc_p->forced_divider |= BIT(ATMEL_SSC_TCMR_PERIOD); 423 break; 424 425 case ATMEL_SSC_RCMR_PERIOD: 426 ssc_p->rcmr_period = div; 427 ssc_p->forced_divider |= BIT(ATMEL_SSC_RCMR_PERIOD); 428 break; 429 430 default: 431 return -EINVAL; 432 } 433 434 return 0; 435 } 436 437 /* Is the cpu-dai master of the frame clock? */ 438 static int atmel_ssc_cfs(struct atmel_ssc_info *ssc_p) 439 { 440 switch (ssc_p->daifmt & SND_SOC_DAIFMT_MASTER_MASK) { 441 case SND_SOC_DAIFMT_CBM_CFS: 442 case SND_SOC_DAIFMT_CBS_CFS: 443 return 1; 444 } 445 return 0; 446 } 447 448 /* Is the cpu-dai master of the bit clock? */ 449 static int atmel_ssc_cbs(struct atmel_ssc_info *ssc_p) 450 { 451 switch (ssc_p->daifmt & SND_SOC_DAIFMT_MASTER_MASK) { 452 case SND_SOC_DAIFMT_CBS_CFM: 453 case SND_SOC_DAIFMT_CBS_CFS: 454 return 1; 455 } 456 return 0; 457 } 458 459 /* 460 * Configure the SSC. 461 */ 462 static int atmel_ssc_hw_params(struct snd_pcm_substream *substream, 463 struct snd_pcm_hw_params *params, 464 struct snd_soc_dai *dai) 465 { 466 struct platform_device *pdev = to_platform_device(dai->dev); 467 int id = pdev->id; 468 struct atmel_ssc_info *ssc_p = &ssc_info[id]; 469 struct ssc_device *ssc = ssc_p->ssc; 470 struct atmel_pcm_dma_params *dma_params; 471 int dir, channels, bits; 472 u32 tfmr, rfmr, tcmr, rcmr; 473 int ret; 474 int fslen, fslen_ext, fs_osync, fs_edge; 475 u32 cmr_div; 476 u32 tcmr_period; 477 u32 rcmr_period; 478 479 /* 480 * Currently, there is only one set of dma params for 481 * each direction. If more are added, this code will 482 * have to be changed to select the proper set. 483 */ 484 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 485 dir = 0; 486 else 487 dir = 1; 488 489 /* 490 * If the cpu dai should provide BCLK, but noone has provided the 491 * divider needed for that to work, fall back to something sensible. 492 */ 493 cmr_div = ssc_p->cmr_div; 494 if (!(ssc_p->forced_divider & BIT(ATMEL_SSC_CMR_DIV)) && 495 atmel_ssc_cbs(ssc_p)) { 496 int bclk_rate = snd_soc_params_to_bclk(params); 497 498 if (bclk_rate < 0) { 499 dev_err(dai->dev, "unable to calculate cmr_div: %d\n", 500 bclk_rate); 501 return bclk_rate; 502 } 503 504 cmr_div = DIV_ROUND_CLOSEST(ssc_p->mck_rate, 2 * bclk_rate); 505 } 506 507 /* 508 * If the cpu dai should provide LRCLK, but noone has provided the 509 * dividers needed for that to work, fall back to something sensible. 510 */ 511 tcmr_period = ssc_p->tcmr_period; 512 rcmr_period = ssc_p->rcmr_period; 513 if (atmel_ssc_cfs(ssc_p)) { 514 int frame_size = snd_soc_params_to_frame_size(params); 515 516 if (frame_size < 0) { 517 dev_err(dai->dev, 518 "unable to calculate tx/rx cmr_period: %d\n", 519 frame_size); 520 return frame_size; 521 } 522 523 if (!(ssc_p->forced_divider & BIT(ATMEL_SSC_TCMR_PERIOD))) 524 tcmr_period = frame_size / 2 - 1; 525 if (!(ssc_p->forced_divider & BIT(ATMEL_SSC_RCMR_PERIOD))) 526 rcmr_period = frame_size / 2 - 1; 527 } 528 529 dma_params = ssc_p->dma_params[dir]; 530 531 channels = params_channels(params); 532 533 /* 534 * Determine sample size in bits and the PDC increment. 535 */ 536 switch (params_format(params)) { 537 case SNDRV_PCM_FORMAT_S8: 538 bits = 8; 539 dma_params->pdc_xfer_size = 1; 540 break; 541 case SNDRV_PCM_FORMAT_S16_LE: 542 bits = 16; 543 dma_params->pdc_xfer_size = 2; 544 break; 545 case SNDRV_PCM_FORMAT_S24_LE: 546 bits = 24; 547 dma_params->pdc_xfer_size = 4; 548 break; 549 case SNDRV_PCM_FORMAT_S32_LE: 550 bits = 32; 551 dma_params->pdc_xfer_size = 4; 552 break; 553 default: 554 printk(KERN_WARNING "atmel_ssc_dai: unsupported PCM format"); 555 return -EINVAL; 556 } 557 558 /* 559 * Compute SSC register settings. 560 */ 561 562 fslen_ext = (bits - 1) / 16; 563 fslen = (bits - 1) % 16; 564 565 switch (ssc_p->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) { 566 567 case SND_SOC_DAIFMT_LEFT_J: 568 fs_osync = SSC_FSOS_POSITIVE; 569 fs_edge = SSC_START_RISING_RF; 570 571 rcmr = SSC_BF(RCMR_STTDLY, 0); 572 tcmr = SSC_BF(TCMR_STTDLY, 0); 573 574 break; 575 576 case SND_SOC_DAIFMT_I2S: 577 fs_osync = SSC_FSOS_NEGATIVE; 578 fs_edge = SSC_START_FALLING_RF; 579 580 rcmr = SSC_BF(RCMR_STTDLY, 1); 581 tcmr = SSC_BF(TCMR_STTDLY, 1); 582 583 break; 584 585 case SND_SOC_DAIFMT_DSP_A: 586 /* 587 * DSP/PCM Mode A format 588 * 589 * Data is transferred on first BCLK after LRC pulse rising 590 * edge.If stereo, the right channel data is contiguous with 591 * the left channel data. 592 */ 593 fs_osync = SSC_FSOS_POSITIVE; 594 fs_edge = SSC_START_RISING_RF; 595 fslen = fslen_ext = 0; 596 597 rcmr = SSC_BF(RCMR_STTDLY, 1); 598 tcmr = SSC_BF(TCMR_STTDLY, 1); 599 600 break; 601 602 default: 603 printk(KERN_WARNING "atmel_ssc_dai: unsupported DAI format 0x%x\n", 604 ssc_p->daifmt); 605 return -EINVAL; 606 } 607 608 if (!atmel_ssc_cfs(ssc_p)) { 609 fslen = fslen_ext = 0; 610 rcmr_period = tcmr_period = 0; 611 fs_osync = SSC_FSOS_NONE; 612 } 613 614 rcmr |= SSC_BF(RCMR_START, fs_edge); 615 tcmr |= SSC_BF(TCMR_START, fs_edge); 616 617 if (atmel_ssc_cbs(ssc_p)) { 618 /* 619 * SSC provides BCLK 620 * 621 * The SSC transmit and receive clocks are generated from the 622 * MCK divider, and the BCLK signal is output 623 * on the SSC TK line. 624 */ 625 rcmr |= SSC_BF(RCMR_CKS, SSC_CKS_DIV) 626 | SSC_BF(RCMR_CKO, SSC_CKO_NONE); 627 628 tcmr |= SSC_BF(TCMR_CKS, SSC_CKS_DIV) 629 | SSC_BF(TCMR_CKO, SSC_CKO_CONTINUOUS); 630 } else { 631 rcmr |= SSC_BF(RCMR_CKS, ssc->clk_from_rk_pin ? 632 SSC_CKS_PIN : SSC_CKS_CLOCK) 633 | SSC_BF(RCMR_CKO, SSC_CKO_NONE); 634 635 tcmr |= SSC_BF(TCMR_CKS, ssc->clk_from_rk_pin ? 636 SSC_CKS_CLOCK : SSC_CKS_PIN) 637 | SSC_BF(TCMR_CKO, SSC_CKO_NONE); 638 } 639 640 rcmr |= SSC_BF(RCMR_PERIOD, rcmr_period) 641 | SSC_BF(RCMR_CKI, SSC_CKI_RISING); 642 643 tcmr |= SSC_BF(TCMR_PERIOD, tcmr_period) 644 | SSC_BF(TCMR_CKI, SSC_CKI_FALLING); 645 646 rfmr = SSC_BF(RFMR_FSLEN_EXT, fslen_ext) 647 | SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE) 648 | SSC_BF(RFMR_FSOS, fs_osync) 649 | SSC_BF(RFMR_FSLEN, fslen) 650 | SSC_BF(RFMR_DATNB, (channels - 1)) 651 | SSC_BIT(RFMR_MSBF) 652 | SSC_BF(RFMR_LOOP, 0) 653 | SSC_BF(RFMR_DATLEN, (bits - 1)); 654 655 tfmr = SSC_BF(TFMR_FSLEN_EXT, fslen_ext) 656 | SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE) 657 | SSC_BF(TFMR_FSDEN, 0) 658 | SSC_BF(TFMR_FSOS, fs_osync) 659 | SSC_BF(TFMR_FSLEN, fslen) 660 | SSC_BF(TFMR_DATNB, (channels - 1)) 661 | SSC_BIT(TFMR_MSBF) 662 | SSC_BF(TFMR_DATDEF, 0) 663 | SSC_BF(TFMR_DATLEN, (bits - 1)); 664 665 if (fslen_ext && !ssc->pdata->has_fslen_ext) { 666 dev_err(dai->dev, "sample size %d is too large for SSC device\n", 667 bits); 668 return -EINVAL; 669 } 670 671 pr_debug("atmel_ssc_hw_params: " 672 "RCMR=%08x RFMR=%08x TCMR=%08x TFMR=%08x\n", 673 rcmr, rfmr, tcmr, tfmr); 674 675 if (!ssc_p->initialized) { 676 if (!ssc_p->ssc->pdata->use_dma) { 677 ssc_writel(ssc_p->ssc->regs, PDC_RPR, 0); 678 ssc_writel(ssc_p->ssc->regs, PDC_RCR, 0); 679 ssc_writel(ssc_p->ssc->regs, PDC_RNPR, 0); 680 ssc_writel(ssc_p->ssc->regs, PDC_RNCR, 0); 681 682 ssc_writel(ssc_p->ssc->regs, PDC_TPR, 0); 683 ssc_writel(ssc_p->ssc->regs, PDC_TCR, 0); 684 ssc_writel(ssc_p->ssc->regs, PDC_TNPR, 0); 685 ssc_writel(ssc_p->ssc->regs, PDC_TNCR, 0); 686 } 687 688 ret = request_irq(ssc_p->ssc->irq, atmel_ssc_interrupt, 0, 689 ssc_p->name, ssc_p); 690 if (ret < 0) { 691 printk(KERN_WARNING 692 "atmel_ssc_dai: request_irq failure\n"); 693 pr_debug("Atmel_ssc_dai: Stopping clock\n"); 694 clk_disable(ssc_p->ssc->clk); 695 return ret; 696 } 697 698 ssc_p->initialized = 1; 699 } 700 701 /* set SSC clock mode register */ 702 ssc_writel(ssc_p->ssc->regs, CMR, cmr_div); 703 704 /* set receive clock mode and format */ 705 ssc_writel(ssc_p->ssc->regs, RCMR, rcmr); 706 ssc_writel(ssc_p->ssc->regs, RFMR, rfmr); 707 708 /* set transmit clock mode and format */ 709 ssc_writel(ssc_p->ssc->regs, TCMR, tcmr); 710 ssc_writel(ssc_p->ssc->regs, TFMR, tfmr); 711 712 pr_debug("atmel_ssc_dai,hw_params: SSC initialized\n"); 713 return 0; 714 } 715 716 717 static int atmel_ssc_prepare(struct snd_pcm_substream *substream, 718 struct snd_soc_dai *dai) 719 { 720 struct platform_device *pdev = to_platform_device(dai->dev); 721 struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id]; 722 struct atmel_pcm_dma_params *dma_params; 723 int dir; 724 725 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 726 dir = 0; 727 else 728 dir = 1; 729 730 dma_params = ssc_p->dma_params[dir]; 731 732 ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_disable); 733 ssc_writel(ssc_p->ssc->regs, IDR, dma_params->mask->ssc_error); 734 735 pr_debug("%s enabled SSC_SR=0x%08x\n", 736 dir ? "receive" : "transmit", 737 ssc_readl(ssc_p->ssc->regs, SR)); 738 return 0; 739 } 740 741 static int atmel_ssc_trigger(struct snd_pcm_substream *substream, 742 int cmd, struct snd_soc_dai *dai) 743 { 744 struct platform_device *pdev = to_platform_device(dai->dev); 745 struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id]; 746 struct atmel_pcm_dma_params *dma_params; 747 int dir; 748 749 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 750 dir = 0; 751 else 752 dir = 1; 753 754 dma_params = ssc_p->dma_params[dir]; 755 756 switch (cmd) { 757 case SNDRV_PCM_TRIGGER_START: 758 case SNDRV_PCM_TRIGGER_RESUME: 759 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 760 ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_enable); 761 break; 762 default: 763 ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_disable); 764 break; 765 } 766 767 return 0; 768 } 769 770 #ifdef CONFIG_PM 771 static int atmel_ssc_suspend(struct snd_soc_dai *cpu_dai) 772 { 773 struct atmel_ssc_info *ssc_p; 774 struct platform_device *pdev = to_platform_device(cpu_dai->dev); 775 776 if (!cpu_dai->active) 777 return 0; 778 779 ssc_p = &ssc_info[pdev->id]; 780 781 /* Save the status register before disabling transmit and receive */ 782 ssc_p->ssc_state.ssc_sr = ssc_readl(ssc_p->ssc->regs, SR); 783 ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_TXDIS) | SSC_BIT(CR_RXDIS)); 784 785 /* Save the current interrupt mask, then disable unmasked interrupts */ 786 ssc_p->ssc_state.ssc_imr = ssc_readl(ssc_p->ssc->regs, IMR); 787 ssc_writel(ssc_p->ssc->regs, IDR, ssc_p->ssc_state.ssc_imr); 788 789 ssc_p->ssc_state.ssc_cmr = ssc_readl(ssc_p->ssc->regs, CMR); 790 ssc_p->ssc_state.ssc_rcmr = ssc_readl(ssc_p->ssc->regs, RCMR); 791 ssc_p->ssc_state.ssc_rfmr = ssc_readl(ssc_p->ssc->regs, RFMR); 792 ssc_p->ssc_state.ssc_tcmr = ssc_readl(ssc_p->ssc->regs, TCMR); 793 ssc_p->ssc_state.ssc_tfmr = ssc_readl(ssc_p->ssc->regs, TFMR); 794 795 return 0; 796 } 797 798 799 800 static int atmel_ssc_resume(struct snd_soc_dai *cpu_dai) 801 { 802 struct atmel_ssc_info *ssc_p; 803 struct platform_device *pdev = to_platform_device(cpu_dai->dev); 804 u32 cr; 805 806 if (!cpu_dai->active) 807 return 0; 808 809 ssc_p = &ssc_info[pdev->id]; 810 811 /* restore SSC register settings */ 812 ssc_writel(ssc_p->ssc->regs, TFMR, ssc_p->ssc_state.ssc_tfmr); 813 ssc_writel(ssc_p->ssc->regs, TCMR, ssc_p->ssc_state.ssc_tcmr); 814 ssc_writel(ssc_p->ssc->regs, RFMR, ssc_p->ssc_state.ssc_rfmr); 815 ssc_writel(ssc_p->ssc->regs, RCMR, ssc_p->ssc_state.ssc_rcmr); 816 ssc_writel(ssc_p->ssc->regs, CMR, ssc_p->ssc_state.ssc_cmr); 817 818 /* re-enable interrupts */ 819 ssc_writel(ssc_p->ssc->regs, IER, ssc_p->ssc_state.ssc_imr); 820 821 /* Re-enable receive and transmit as appropriate */ 822 cr = 0; 823 cr |= 824 (ssc_p->ssc_state.ssc_sr & SSC_BIT(SR_RXEN)) ? SSC_BIT(CR_RXEN) : 0; 825 cr |= 826 (ssc_p->ssc_state.ssc_sr & SSC_BIT(SR_TXEN)) ? SSC_BIT(CR_TXEN) : 0; 827 ssc_writel(ssc_p->ssc->regs, CR, cr); 828 829 return 0; 830 } 831 #else /* CONFIG_PM */ 832 # define atmel_ssc_suspend NULL 833 # define atmel_ssc_resume NULL 834 #endif /* CONFIG_PM */ 835 836 #define ATMEL_SSC_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |\ 837 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) 838 839 static const struct snd_soc_dai_ops atmel_ssc_dai_ops = { 840 .startup = atmel_ssc_startup, 841 .shutdown = atmel_ssc_shutdown, 842 .prepare = atmel_ssc_prepare, 843 .trigger = atmel_ssc_trigger, 844 .hw_params = atmel_ssc_hw_params, 845 .set_fmt = atmel_ssc_set_dai_fmt, 846 .set_clkdiv = atmel_ssc_set_dai_clkdiv, 847 }; 848 849 static struct snd_soc_dai_driver atmel_ssc_dai = { 850 .suspend = atmel_ssc_suspend, 851 .resume = atmel_ssc_resume, 852 .playback = { 853 .channels_min = 1, 854 .channels_max = 2, 855 .rates = SNDRV_PCM_RATE_CONTINUOUS, 856 .rate_min = 8000, 857 .rate_max = 384000, 858 .formats = ATMEL_SSC_FORMATS,}, 859 .capture = { 860 .channels_min = 1, 861 .channels_max = 2, 862 .rates = SNDRV_PCM_RATE_CONTINUOUS, 863 .rate_min = 8000, 864 .rate_max = 384000, 865 .formats = ATMEL_SSC_FORMATS,}, 866 .ops = &atmel_ssc_dai_ops, 867 }; 868 869 static const struct snd_soc_component_driver atmel_ssc_component = { 870 .name = "atmel-ssc", 871 }; 872 873 static int asoc_ssc_init(struct device *dev) 874 { 875 struct ssc_device *ssc = dev_get_drvdata(dev); 876 int ret; 877 878 ret = devm_snd_soc_register_component(dev, &atmel_ssc_component, 879 &atmel_ssc_dai, 1); 880 if (ret) { 881 dev_err(dev, "Could not register DAI: %d\n", ret); 882 return ret; 883 } 884 885 if (ssc->pdata->use_dma) 886 ret = atmel_pcm_dma_platform_register(dev); 887 else 888 ret = atmel_pcm_pdc_platform_register(dev); 889 890 if (ret) { 891 dev_err(dev, "Could not register PCM: %d\n", ret); 892 return ret; 893 } 894 895 return 0; 896 } 897 898 /** 899 * atmel_ssc_set_audio - Allocate the specified SSC for audio use. 900 */ 901 int atmel_ssc_set_audio(int ssc_id) 902 { 903 struct ssc_device *ssc; 904 int ret; 905 906 /* If we can grab the SSC briefly to parent the DAI device off it */ 907 ssc = ssc_request(ssc_id); 908 if (IS_ERR(ssc)) { 909 pr_err("Unable to parent ASoC SSC DAI on SSC: %ld\n", 910 PTR_ERR(ssc)); 911 return PTR_ERR(ssc); 912 } else { 913 ssc_info[ssc_id].ssc = ssc; 914 } 915 916 ret = asoc_ssc_init(&ssc->pdev->dev); 917 918 return ret; 919 } 920 EXPORT_SYMBOL_GPL(atmel_ssc_set_audio); 921 922 void atmel_ssc_put_audio(int ssc_id) 923 { 924 struct ssc_device *ssc = ssc_info[ssc_id].ssc; 925 926 ssc_free(ssc); 927 } 928 EXPORT_SYMBOL_GPL(atmel_ssc_put_audio); 929 930 /* Module information */ 931 MODULE_AUTHOR("Sedji Gaouaou, sedji.gaouaou@atmel.com, www.atmel.com"); 932 MODULE_DESCRIPTION("ATMEL SSC ASoC Interface"); 933 MODULE_LICENSE("GPL"); 934