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_I2S: 568 fs_osync = SSC_FSOS_NEGATIVE; 569 fs_edge = SSC_START_FALLING_RF; 570 571 rcmr = SSC_BF(RCMR_STTDLY, 1); 572 tcmr = SSC_BF(TCMR_STTDLY, 1); 573 574 break; 575 576 case SND_SOC_DAIFMT_DSP_A: 577 /* 578 * DSP/PCM Mode A format 579 * 580 * Data is transferred on first BCLK after LRC pulse rising 581 * edge.If stereo, the right channel data is contiguous with 582 * the left channel data. 583 */ 584 fs_osync = SSC_FSOS_POSITIVE; 585 fs_edge = SSC_START_RISING_RF; 586 fslen = fslen_ext = 0; 587 588 rcmr = SSC_BF(RCMR_STTDLY, 1); 589 tcmr = SSC_BF(TCMR_STTDLY, 1); 590 591 break; 592 593 default: 594 printk(KERN_WARNING "atmel_ssc_dai: unsupported DAI format 0x%x\n", 595 ssc_p->daifmt); 596 return -EINVAL; 597 } 598 599 if (!atmel_ssc_cfs(ssc_p)) { 600 fslen = fslen_ext = 0; 601 rcmr_period = tcmr_period = 0; 602 fs_osync = SSC_FSOS_NONE; 603 } 604 605 rcmr |= SSC_BF(RCMR_START, fs_edge); 606 tcmr |= SSC_BF(TCMR_START, fs_edge); 607 608 if (atmel_ssc_cbs(ssc_p)) { 609 /* 610 * SSC provides BCLK 611 * 612 * The SSC transmit and receive clocks are generated from the 613 * MCK divider, and the BCLK signal is output 614 * on the SSC TK line. 615 */ 616 rcmr |= SSC_BF(RCMR_CKS, SSC_CKS_DIV) 617 | SSC_BF(RCMR_CKO, SSC_CKO_NONE); 618 619 tcmr |= SSC_BF(TCMR_CKS, SSC_CKS_DIV) 620 | SSC_BF(TCMR_CKO, SSC_CKO_CONTINUOUS); 621 } else { 622 rcmr |= SSC_BF(RCMR_CKS, ssc->clk_from_rk_pin ? 623 SSC_CKS_PIN : SSC_CKS_CLOCK) 624 | SSC_BF(RCMR_CKO, SSC_CKO_NONE); 625 626 tcmr |= SSC_BF(TCMR_CKS, ssc->clk_from_rk_pin ? 627 SSC_CKS_CLOCK : SSC_CKS_PIN) 628 | SSC_BF(TCMR_CKO, SSC_CKO_NONE); 629 } 630 631 rcmr |= SSC_BF(RCMR_PERIOD, rcmr_period) 632 | SSC_BF(RCMR_CKI, SSC_CKI_RISING); 633 634 tcmr |= SSC_BF(TCMR_PERIOD, tcmr_period) 635 | SSC_BF(TCMR_CKI, SSC_CKI_FALLING); 636 637 rfmr = SSC_BF(RFMR_FSLEN_EXT, fslen_ext) 638 | SSC_BF(RFMR_FSEDGE, SSC_FSEDGE_POSITIVE) 639 | SSC_BF(RFMR_FSOS, fs_osync) 640 | SSC_BF(RFMR_FSLEN, fslen) 641 | SSC_BF(RFMR_DATNB, (channels - 1)) 642 | SSC_BIT(RFMR_MSBF) 643 | SSC_BF(RFMR_LOOP, 0) 644 | SSC_BF(RFMR_DATLEN, (bits - 1)); 645 646 tfmr = SSC_BF(TFMR_FSLEN_EXT, fslen_ext) 647 | SSC_BF(TFMR_FSEDGE, SSC_FSEDGE_POSITIVE) 648 | SSC_BF(TFMR_FSDEN, 0) 649 | SSC_BF(TFMR_FSOS, fs_osync) 650 | SSC_BF(TFMR_FSLEN, fslen) 651 | SSC_BF(TFMR_DATNB, (channels - 1)) 652 | SSC_BIT(TFMR_MSBF) 653 | SSC_BF(TFMR_DATDEF, 0) 654 | SSC_BF(TFMR_DATLEN, (bits - 1)); 655 656 if (fslen_ext && !ssc->pdata->has_fslen_ext) { 657 dev_err(dai->dev, "sample size %d is too large for SSC device\n", 658 bits); 659 return -EINVAL; 660 } 661 662 pr_debug("atmel_ssc_hw_params: " 663 "RCMR=%08x RFMR=%08x TCMR=%08x TFMR=%08x\n", 664 rcmr, rfmr, tcmr, tfmr); 665 666 if (!ssc_p->initialized) { 667 if (!ssc_p->ssc->pdata->use_dma) { 668 ssc_writel(ssc_p->ssc->regs, PDC_RPR, 0); 669 ssc_writel(ssc_p->ssc->regs, PDC_RCR, 0); 670 ssc_writel(ssc_p->ssc->regs, PDC_RNPR, 0); 671 ssc_writel(ssc_p->ssc->regs, PDC_RNCR, 0); 672 673 ssc_writel(ssc_p->ssc->regs, PDC_TPR, 0); 674 ssc_writel(ssc_p->ssc->regs, PDC_TCR, 0); 675 ssc_writel(ssc_p->ssc->regs, PDC_TNPR, 0); 676 ssc_writel(ssc_p->ssc->regs, PDC_TNCR, 0); 677 } 678 679 ret = request_irq(ssc_p->ssc->irq, atmel_ssc_interrupt, 0, 680 ssc_p->name, ssc_p); 681 if (ret < 0) { 682 printk(KERN_WARNING 683 "atmel_ssc_dai: request_irq failure\n"); 684 pr_debug("Atmel_ssc_dai: Stopping clock\n"); 685 clk_disable(ssc_p->ssc->clk); 686 return ret; 687 } 688 689 ssc_p->initialized = 1; 690 } 691 692 /* set SSC clock mode register */ 693 ssc_writel(ssc_p->ssc->regs, CMR, cmr_div); 694 695 /* set receive clock mode and format */ 696 ssc_writel(ssc_p->ssc->regs, RCMR, rcmr); 697 ssc_writel(ssc_p->ssc->regs, RFMR, rfmr); 698 699 /* set transmit clock mode and format */ 700 ssc_writel(ssc_p->ssc->regs, TCMR, tcmr); 701 ssc_writel(ssc_p->ssc->regs, TFMR, tfmr); 702 703 pr_debug("atmel_ssc_dai,hw_params: SSC initialized\n"); 704 return 0; 705 } 706 707 708 static int atmel_ssc_prepare(struct snd_pcm_substream *substream, 709 struct snd_soc_dai *dai) 710 { 711 struct platform_device *pdev = to_platform_device(dai->dev); 712 struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id]; 713 struct atmel_pcm_dma_params *dma_params; 714 int dir; 715 716 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 717 dir = 0; 718 else 719 dir = 1; 720 721 dma_params = ssc_p->dma_params[dir]; 722 723 ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_disable); 724 ssc_writel(ssc_p->ssc->regs, IDR, dma_params->mask->ssc_error); 725 726 pr_debug("%s enabled SSC_SR=0x%08x\n", 727 dir ? "receive" : "transmit", 728 ssc_readl(ssc_p->ssc->regs, SR)); 729 return 0; 730 } 731 732 static int atmel_ssc_trigger(struct snd_pcm_substream *substream, 733 int cmd, struct snd_soc_dai *dai) 734 { 735 struct platform_device *pdev = to_platform_device(dai->dev); 736 struct atmel_ssc_info *ssc_p = &ssc_info[pdev->id]; 737 struct atmel_pcm_dma_params *dma_params; 738 int dir; 739 740 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 741 dir = 0; 742 else 743 dir = 1; 744 745 dma_params = ssc_p->dma_params[dir]; 746 747 switch (cmd) { 748 case SNDRV_PCM_TRIGGER_START: 749 case SNDRV_PCM_TRIGGER_RESUME: 750 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 751 ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_enable); 752 break; 753 default: 754 ssc_writel(ssc_p->ssc->regs, CR, dma_params->mask->ssc_disable); 755 break; 756 } 757 758 return 0; 759 } 760 761 #ifdef CONFIG_PM 762 static int atmel_ssc_suspend(struct snd_soc_dai *cpu_dai) 763 { 764 struct atmel_ssc_info *ssc_p; 765 struct platform_device *pdev = to_platform_device(cpu_dai->dev); 766 767 if (!cpu_dai->active) 768 return 0; 769 770 ssc_p = &ssc_info[pdev->id]; 771 772 /* Save the status register before disabling transmit and receive */ 773 ssc_p->ssc_state.ssc_sr = ssc_readl(ssc_p->ssc->regs, SR); 774 ssc_writel(ssc_p->ssc->regs, CR, SSC_BIT(CR_TXDIS) | SSC_BIT(CR_RXDIS)); 775 776 /* Save the current interrupt mask, then disable unmasked interrupts */ 777 ssc_p->ssc_state.ssc_imr = ssc_readl(ssc_p->ssc->regs, IMR); 778 ssc_writel(ssc_p->ssc->regs, IDR, ssc_p->ssc_state.ssc_imr); 779 780 ssc_p->ssc_state.ssc_cmr = ssc_readl(ssc_p->ssc->regs, CMR); 781 ssc_p->ssc_state.ssc_rcmr = ssc_readl(ssc_p->ssc->regs, RCMR); 782 ssc_p->ssc_state.ssc_rfmr = ssc_readl(ssc_p->ssc->regs, RFMR); 783 ssc_p->ssc_state.ssc_tcmr = ssc_readl(ssc_p->ssc->regs, TCMR); 784 ssc_p->ssc_state.ssc_tfmr = ssc_readl(ssc_p->ssc->regs, TFMR); 785 786 return 0; 787 } 788 789 790 791 static int atmel_ssc_resume(struct snd_soc_dai *cpu_dai) 792 { 793 struct atmel_ssc_info *ssc_p; 794 struct platform_device *pdev = to_platform_device(cpu_dai->dev); 795 u32 cr; 796 797 if (!cpu_dai->active) 798 return 0; 799 800 ssc_p = &ssc_info[pdev->id]; 801 802 /* restore SSC register settings */ 803 ssc_writel(ssc_p->ssc->regs, TFMR, ssc_p->ssc_state.ssc_tfmr); 804 ssc_writel(ssc_p->ssc->regs, TCMR, ssc_p->ssc_state.ssc_tcmr); 805 ssc_writel(ssc_p->ssc->regs, RFMR, ssc_p->ssc_state.ssc_rfmr); 806 ssc_writel(ssc_p->ssc->regs, RCMR, ssc_p->ssc_state.ssc_rcmr); 807 ssc_writel(ssc_p->ssc->regs, CMR, ssc_p->ssc_state.ssc_cmr); 808 809 /* re-enable interrupts */ 810 ssc_writel(ssc_p->ssc->regs, IER, ssc_p->ssc_state.ssc_imr); 811 812 /* Re-enable receive and transmit as appropriate */ 813 cr = 0; 814 cr |= 815 (ssc_p->ssc_state.ssc_sr & SSC_BIT(SR_RXEN)) ? SSC_BIT(CR_RXEN) : 0; 816 cr |= 817 (ssc_p->ssc_state.ssc_sr & SSC_BIT(SR_TXEN)) ? SSC_BIT(CR_TXEN) : 0; 818 ssc_writel(ssc_p->ssc->regs, CR, cr); 819 820 return 0; 821 } 822 #else /* CONFIG_PM */ 823 # define atmel_ssc_suspend NULL 824 # define atmel_ssc_resume NULL 825 #endif /* CONFIG_PM */ 826 827 #define ATMEL_SSC_FORMATS (SNDRV_PCM_FMTBIT_S8 | SNDRV_PCM_FMTBIT_S16_LE |\ 828 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) 829 830 static const struct snd_soc_dai_ops atmel_ssc_dai_ops = { 831 .startup = atmel_ssc_startup, 832 .shutdown = atmel_ssc_shutdown, 833 .prepare = atmel_ssc_prepare, 834 .trigger = atmel_ssc_trigger, 835 .hw_params = atmel_ssc_hw_params, 836 .set_fmt = atmel_ssc_set_dai_fmt, 837 .set_clkdiv = atmel_ssc_set_dai_clkdiv, 838 }; 839 840 static struct snd_soc_dai_driver atmel_ssc_dai = { 841 .suspend = atmel_ssc_suspend, 842 .resume = atmel_ssc_resume, 843 .playback = { 844 .channels_min = 1, 845 .channels_max = 2, 846 .rates = SNDRV_PCM_RATE_CONTINUOUS, 847 .rate_min = 8000, 848 .rate_max = 384000, 849 .formats = ATMEL_SSC_FORMATS,}, 850 .capture = { 851 .channels_min = 1, 852 .channels_max = 2, 853 .rates = SNDRV_PCM_RATE_CONTINUOUS, 854 .rate_min = 8000, 855 .rate_max = 384000, 856 .formats = ATMEL_SSC_FORMATS,}, 857 .ops = &atmel_ssc_dai_ops, 858 }; 859 860 static const struct snd_soc_component_driver atmel_ssc_component = { 861 .name = "atmel-ssc", 862 }; 863 864 static int asoc_ssc_init(struct device *dev) 865 { 866 struct ssc_device *ssc = dev_get_drvdata(dev); 867 int ret; 868 869 ret = devm_snd_soc_register_component(dev, &atmel_ssc_component, 870 &atmel_ssc_dai, 1); 871 if (ret) { 872 dev_err(dev, "Could not register DAI: %d\n", ret); 873 return ret; 874 } 875 876 if (ssc->pdata->use_dma) 877 ret = atmel_pcm_dma_platform_register(dev); 878 else 879 ret = atmel_pcm_pdc_platform_register(dev); 880 881 if (ret) { 882 dev_err(dev, "Could not register PCM: %d\n", ret); 883 return ret; 884 } 885 886 return 0; 887 } 888 889 /** 890 * atmel_ssc_set_audio - Allocate the specified SSC for audio use. 891 */ 892 int atmel_ssc_set_audio(int ssc_id) 893 { 894 struct ssc_device *ssc; 895 int ret; 896 897 /* If we can grab the SSC briefly to parent the DAI device off it */ 898 ssc = ssc_request(ssc_id); 899 if (IS_ERR(ssc)) { 900 pr_err("Unable to parent ASoC SSC DAI on SSC: %ld\n", 901 PTR_ERR(ssc)); 902 return PTR_ERR(ssc); 903 } else { 904 ssc_info[ssc_id].ssc = ssc; 905 } 906 907 ret = asoc_ssc_init(&ssc->pdev->dev); 908 909 return ret; 910 } 911 EXPORT_SYMBOL_GPL(atmel_ssc_set_audio); 912 913 void atmel_ssc_put_audio(int ssc_id) 914 { 915 struct ssc_device *ssc = ssc_info[ssc_id].ssc; 916 917 ssc_free(ssc); 918 } 919 EXPORT_SYMBOL_GPL(atmel_ssc_put_audio); 920 921 /* Module information */ 922 MODULE_AUTHOR("Sedji Gaouaou, sedji.gaouaou@atmel.com, www.atmel.com"); 923 MODULE_DESCRIPTION("ATMEL SSC ASoC Interface"); 924 MODULE_LICENSE("GPL"); 925