1 /* 2 * Copyright (C) STMicroelectronics SA 2015 3 * Authors: Arnaud Pouliquen <arnaud.pouliquen@st.com> 4 * for STMicroelectronics. 5 * License terms: GNU General Public License (GPL), version 2 6 */ 7 8 #include <linux/clk.h> 9 #include <linux/delay.h> 10 #include <linux/io.h> 11 #include <linux/mfd/syscon.h> 12 13 #include <sound/asoundef.h> 14 #include <sound/soc.h> 15 16 #include "uniperif.h" 17 18 /* 19 * Some hardware-related definitions 20 */ 21 22 /* sys config registers definitions */ 23 #define SYS_CFG_AUDIO_GLUE 0xA4 24 #define SYS_CFG_AUDI0_GLUE_PCM_CLKX 8 25 26 /* 27 * Driver specific types. 28 */ 29 #define UNIPERIF_PLAYER_TYPE_IS_HDMI(p) \ 30 ((p)->info->player_type == SND_ST_UNIPERIF_PLAYER_TYPE_HDMI) 31 #define UNIPERIF_PLAYER_TYPE_IS_PCM(p) \ 32 ((p)->info->player_type == SND_ST_UNIPERIF_PLAYER_TYPE_PCM) 33 #define UNIPERIF_PLAYER_TYPE_IS_SPDIF(p) \ 34 ((p)->info->player_type == SND_ST_UNIPERIF_PLAYER_TYPE_SPDIF) 35 #define UNIPERIF_PLAYER_TYPE_IS_IEC958(p) \ 36 (UNIPERIF_PLAYER_TYPE_IS_HDMI(p) || \ 37 UNIPERIF_PLAYER_TYPE_IS_SPDIF(p)) 38 39 #define UNIPERIF_PLAYER_CLK_ADJ_MIN -999999 40 #define UNIPERIF_PLAYER_CLK_ADJ_MAX 1000000 41 42 /* 43 * Note: snd_pcm_hardware is linked to DMA controller but is declared here to 44 * integrate DAI_CPU capability in term of rate and supported channels 45 */ 46 static const struct snd_pcm_hardware uni_player_pcm_hw = { 47 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER | 48 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP | 49 SNDRV_PCM_INFO_MMAP_VALID, 50 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE, 51 52 .rates = SNDRV_PCM_RATE_CONTINUOUS, 53 .rate_min = 8000, 54 .rate_max = 192000, 55 56 .channels_min = 2, 57 .channels_max = 8, 58 59 .periods_min = 2, 60 .periods_max = 48, 61 62 .period_bytes_min = 128, 63 .period_bytes_max = 64 * PAGE_SIZE, 64 .buffer_bytes_max = 256 * PAGE_SIZE 65 }; 66 67 static inline int reset_player(struct uniperif *player) 68 { 69 int count = 10; 70 71 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) { 72 while (GET_UNIPERIF_SOFT_RST_SOFT_RST(player) && count) { 73 udelay(5); 74 count--; 75 } 76 } 77 78 if (!count) { 79 dev_err(player->dev, "Failed to reset uniperif"); 80 return -EIO; 81 } 82 83 return 0; 84 } 85 86 /* 87 * uni_player_irq_handler 88 * In case of error audio stream is stopped; stop action is protected via PCM 89 * stream lock to avoid race condition with trigger callback. 90 */ 91 static irqreturn_t uni_player_irq_handler(int irq, void *dev_id) 92 { 93 irqreturn_t ret = IRQ_NONE; 94 struct uniperif *player = dev_id; 95 unsigned int status; 96 unsigned int tmp; 97 98 if (player->state == UNIPERIF_STATE_STOPPED) { 99 /* Unexpected IRQ: do nothing */ 100 return IRQ_NONE; 101 } 102 103 /* Get interrupt status & clear them immediately */ 104 status = GET_UNIPERIF_ITS(player); 105 SET_UNIPERIF_ITS_BCLR(player, status); 106 107 /* Check for fifo error (underrun) */ 108 if (unlikely(status & UNIPERIF_ITS_FIFO_ERROR_MASK(player))) { 109 dev_err(player->dev, "FIFO underflow error detected"); 110 111 /* Interrupt is just for information when underflow recovery */ 112 if (player->info->underflow_enabled) { 113 /* Update state to underflow */ 114 player->state = UNIPERIF_STATE_UNDERFLOW; 115 116 } else { 117 /* Disable interrupt so doesn't continually fire */ 118 SET_UNIPERIF_ITM_BCLR_FIFO_ERROR(player); 119 120 /* Stop the player */ 121 snd_pcm_stream_lock(player->substream); 122 snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN); 123 snd_pcm_stream_unlock(player->substream); 124 } 125 126 ret = IRQ_HANDLED; 127 } 128 129 /* Check for dma error (overrun) */ 130 if (unlikely(status & UNIPERIF_ITS_DMA_ERROR_MASK(player))) { 131 dev_err(player->dev, "DMA error detected"); 132 133 /* Disable interrupt so doesn't continually fire */ 134 SET_UNIPERIF_ITM_BCLR_DMA_ERROR(player); 135 136 /* Stop the player */ 137 snd_pcm_stream_lock(player->substream); 138 snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN); 139 snd_pcm_stream_unlock(player->substream); 140 141 ret = IRQ_HANDLED; 142 } 143 144 /* Check for underflow recovery done */ 145 if (unlikely(status & UNIPERIF_ITM_UNDERFLOW_REC_DONE_MASK(player))) { 146 if (!player->info->underflow_enabled) { 147 dev_err(player->dev, "unexpected Underflow recovering"); 148 return -EPERM; 149 } 150 /* Read the underflow recovery duration */ 151 tmp = GET_UNIPERIF_STATUS_1_UNDERFLOW_DURATION(player); 152 153 /* Clear the underflow recovery duration */ 154 SET_UNIPERIF_BIT_CONTROL_CLR_UNDERFLOW_DURATION(player); 155 156 /* Update state to started */ 157 player->state = UNIPERIF_STATE_STARTED; 158 159 ret = IRQ_HANDLED; 160 } 161 162 /* Check if underflow recovery failed */ 163 if (unlikely(status & 164 UNIPERIF_ITM_UNDERFLOW_REC_FAILED_MASK(player))) { 165 dev_err(player->dev, "Underflow recovery failed"); 166 167 /* Stop the player */ 168 snd_pcm_stream_lock(player->substream); 169 snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN); 170 snd_pcm_stream_unlock(player->substream); 171 172 ret = IRQ_HANDLED; 173 } 174 175 return ret; 176 } 177 178 static int uni_player_clk_set_rate(struct uniperif *player, unsigned long rate) 179 { 180 int rate_adjusted, rate_achieved, delta, ret; 181 int adjustment = player->clk_adj; 182 183 /* 184 * a 185 * F = f + --------- * f = f + d 186 * 1000000 187 * 188 * a 189 * d = --------- * f 190 * 1000000 191 * 192 * where: 193 * f - nominal rate 194 * a - adjustment in ppm (parts per milion) 195 * F - rate to be set in synthesizer 196 * d - delta (difference) between f and F 197 */ 198 if (adjustment < 0) { 199 /* div64_64 operates on unsigned values... */ 200 delta = -1; 201 adjustment = -adjustment; 202 } else { 203 delta = 1; 204 } 205 /* 500000 ppm is 0.5, which is used to round up values */ 206 delta *= (int)div64_u64((uint64_t)rate * 207 (uint64_t)adjustment + 500000, 1000000); 208 rate_adjusted = rate + delta; 209 210 /* Adjusted rate should never be == 0 */ 211 if (!rate_adjusted) 212 return -EINVAL; 213 214 ret = clk_set_rate(player->clk, rate_adjusted); 215 if (ret < 0) 216 return ret; 217 218 rate_achieved = clk_get_rate(player->clk); 219 if (!rate_achieved) 220 /* If value is 0 means that clock or parent not valid */ 221 return -EINVAL; 222 223 /* 224 * Using ALSA's adjustment control, we can modify the rate to be up 225 * to twice as much as requested, but no more 226 */ 227 delta = rate_achieved - rate; 228 if (delta < 0) { 229 /* div64_64 operates on unsigned values... */ 230 delta = -delta; 231 adjustment = -1; 232 } else { 233 adjustment = 1; 234 } 235 /* Frequency/2 is added to round up result */ 236 adjustment *= (int)div64_u64((uint64_t)delta * 1000000 + rate / 2, 237 rate); 238 player->clk_adj = adjustment; 239 return 0; 240 } 241 242 static void uni_player_set_channel_status(struct uniperif *player, 243 struct snd_pcm_runtime *runtime) 244 { 245 int n; 246 unsigned int status; 247 248 /* 249 * Some AVRs and TVs require the channel status to contain a correct 250 * sampling frequency. If no sample rate is already specified, then 251 * set one. 252 */ 253 mutex_lock(&player->ctrl_lock); 254 if (runtime && (player->stream_settings.iec958.status[3] 255 == IEC958_AES3_CON_FS_NOTID)) { 256 switch (runtime->rate) { 257 case 22050: 258 player->stream_settings.iec958.status[3] = 259 IEC958_AES3_CON_FS_22050; 260 break; 261 case 44100: 262 player->stream_settings.iec958.status[3] = 263 IEC958_AES3_CON_FS_44100; 264 break; 265 case 88200: 266 player->stream_settings.iec958.status[3] = 267 IEC958_AES3_CON_FS_88200; 268 break; 269 case 176400: 270 player->stream_settings.iec958.status[3] = 271 IEC958_AES3_CON_FS_176400; 272 break; 273 case 24000: 274 player->stream_settings.iec958.status[3] = 275 IEC958_AES3_CON_FS_24000; 276 break; 277 case 48000: 278 player->stream_settings.iec958.status[3] = 279 IEC958_AES3_CON_FS_48000; 280 break; 281 case 96000: 282 player->stream_settings.iec958.status[3] = 283 IEC958_AES3_CON_FS_96000; 284 break; 285 case 192000: 286 player->stream_settings.iec958.status[3] = 287 IEC958_AES3_CON_FS_192000; 288 break; 289 case 32000: 290 player->stream_settings.iec958.status[3] = 291 IEC958_AES3_CON_FS_32000; 292 break; 293 default: 294 /* Mark as sampling frequency not indicated */ 295 player->stream_settings.iec958.status[3] = 296 IEC958_AES3_CON_FS_NOTID; 297 break; 298 } 299 } 300 301 /* Audio mode: 302 * Use audio mode status to select PCM or encoded mode 303 */ 304 if (player->stream_settings.iec958.status[0] & IEC958_AES0_NONAUDIO) 305 player->stream_settings.encoding_mode = 306 UNIPERIF_IEC958_ENCODING_MODE_ENCODED; 307 else 308 player->stream_settings.encoding_mode = 309 UNIPERIF_IEC958_ENCODING_MODE_PCM; 310 311 if (player->stream_settings.encoding_mode == 312 UNIPERIF_IEC958_ENCODING_MODE_PCM) 313 /* Clear user validity bits */ 314 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0); 315 else 316 /* Set user validity bits */ 317 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 1); 318 319 /* Program the new channel status */ 320 for (n = 0; n < 6; ++n) { 321 status = 322 player->stream_settings.iec958.status[0 + (n * 4)] & 0xf; 323 status |= 324 player->stream_settings.iec958.status[1 + (n * 4)] << 8; 325 status |= 326 player->stream_settings.iec958.status[2 + (n * 4)] << 16; 327 status |= 328 player->stream_settings.iec958.status[3 + (n * 4)] << 24; 329 SET_UNIPERIF_CHANNEL_STA_REGN(player, n, status); 330 } 331 mutex_unlock(&player->ctrl_lock); 332 333 /* Update the channel status */ 334 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) 335 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player); 336 else 337 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player); 338 } 339 340 static int uni_player_prepare_iec958(struct uniperif *player, 341 struct snd_pcm_runtime *runtime) 342 { 343 int clk_div; 344 345 clk_div = player->mclk / runtime->rate; 346 347 /* Oversampling must be multiple of 128 as iec958 frame is 32-bits */ 348 if ((clk_div % 128) || (clk_div <= 0)) { 349 dev_err(player->dev, "%s: invalid clk_div %d", 350 __func__, clk_div); 351 return -EINVAL; 352 } 353 354 switch (runtime->format) { 355 case SNDRV_PCM_FORMAT_S16_LE: 356 /* 16/16 memory format */ 357 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player); 358 /* 16-bits per sub-frame */ 359 SET_UNIPERIF_I2S_FMT_NBIT_32(player); 360 /* Set 16-bit sample precision */ 361 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player); 362 break; 363 case SNDRV_PCM_FORMAT_S32_LE: 364 /* 16/0 memory format */ 365 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player); 366 /* 32-bits per sub-frame */ 367 SET_UNIPERIF_I2S_FMT_NBIT_32(player); 368 /* Set 24-bit sample precision */ 369 SET_UNIPERIF_I2S_FMT_DATA_SIZE_24(player); 370 break; 371 default: 372 dev_err(player->dev, "format not supported"); 373 return -EINVAL; 374 } 375 376 /* Set parity to be calculated by the hardware */ 377 SET_UNIPERIF_CONFIG_PARITY_CNTR_BY_HW(player); 378 379 /* Set channel status bits to be inserted by the hardware */ 380 SET_UNIPERIF_CONFIG_CHANNEL_STA_CNTR_BY_HW(player); 381 382 /* Set user data bits to be inserted by the hardware */ 383 SET_UNIPERIF_CONFIG_USER_DAT_CNTR_BY_HW(player); 384 385 /* Set validity bits to be inserted by the hardware */ 386 SET_UNIPERIF_CONFIG_VALIDITY_DAT_CNTR_BY_HW(player); 387 388 /* Set full software control to disabled */ 389 SET_UNIPERIF_CONFIG_SPDIF_SW_CTRL_DISABLE(player); 390 391 SET_UNIPERIF_CTRL_ZERO_STUFF_HW(player); 392 393 /* Update the channel status */ 394 uni_player_set_channel_status(player, runtime); 395 396 /* Clear the user validity user bits */ 397 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0); 398 399 /* Disable one-bit audio mode */ 400 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player); 401 402 /* Enable consecutive frames repetition of Z preamble (not for HBRA) */ 403 SET_UNIPERIF_CONFIG_REPEAT_CHL_STS_ENABLE(player); 404 405 /* Change to SUF0_SUBF1 and left/right channels swap! */ 406 SET_UNIPERIF_CONFIG_SUBFRAME_SEL_SUBF1_SUBF0(player); 407 408 /* Set data output as MSB first */ 409 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player); 410 411 if (player->stream_settings.encoding_mode == 412 UNIPERIF_IEC958_ENCODING_MODE_ENCODED) 413 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_ON(player); 414 else 415 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_OFF(player); 416 417 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2); 418 419 /* Set rounding to off */ 420 SET_UNIPERIF_CTRL_ROUNDING_OFF(player); 421 422 /* Set clock divisor */ 423 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / 128); 424 425 /* Set the spdif latency to not wait before starting player */ 426 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player); 427 428 /* 429 * Ensure iec958 formatting is off. It will be enabled in function 430 * uni_player_start() at the same time as the operation 431 * mode is set to work around a silicon issue. 432 */ 433 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) 434 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player); 435 else 436 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player); 437 438 return 0; 439 } 440 441 static int uni_player_prepare_pcm(struct uniperif *player, 442 struct snd_pcm_runtime *runtime) 443 { 444 int output_frame_size, slot_width, clk_div; 445 446 /* Force slot width to 32 in I2S mode (HW constraint) */ 447 if ((player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) == 448 SND_SOC_DAIFMT_I2S) { 449 slot_width = 32; 450 } else { 451 switch (runtime->format) { 452 case SNDRV_PCM_FORMAT_S16_LE: 453 slot_width = 16; 454 break; 455 default: 456 slot_width = 32; 457 break; 458 } 459 } 460 output_frame_size = slot_width * runtime->channels; 461 462 clk_div = player->mclk / runtime->rate; 463 /* 464 * For 32 bits subframe clk_div must be a multiple of 128, 465 * for 16 bits must be a multiple of 64 466 */ 467 if ((slot_width == 32) && (clk_div % 128)) { 468 dev_err(player->dev, "%s: invalid clk_div", __func__); 469 return -EINVAL; 470 } 471 472 if ((slot_width == 16) && (clk_div % 64)) { 473 dev_err(player->dev, "%s: invalid clk_div", __func__); 474 return -EINVAL; 475 } 476 477 /* 478 * Number of bits per subframe (which is one channel sample) 479 * on output - Transfer 16 or 32 bits from FIFO 480 */ 481 switch (slot_width) { 482 case 32: 483 SET_UNIPERIF_I2S_FMT_NBIT_32(player); 484 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player); 485 break; 486 case 16: 487 SET_UNIPERIF_I2S_FMT_NBIT_16(player); 488 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player); 489 break; 490 default: 491 dev_err(player->dev, "subframe format not supported"); 492 return -EINVAL; 493 } 494 495 /* Configure data memory format */ 496 switch (runtime->format) { 497 case SNDRV_PCM_FORMAT_S16_LE: 498 /* One data word contains two samples */ 499 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player); 500 break; 501 502 case SNDRV_PCM_FORMAT_S32_LE: 503 /* 504 * Actually "16 bits/0 bits" means "32/28/24/20/18/16 bits 505 * on the left than zeros (if less than 32 bytes)"... ;-) 506 */ 507 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player); 508 break; 509 510 default: 511 dev_err(player->dev, "format not supported"); 512 return -EINVAL; 513 } 514 515 /* Set rounding to off */ 516 SET_UNIPERIF_CTRL_ROUNDING_OFF(player); 517 518 /* Set clock divisor */ 519 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / (2 * output_frame_size)); 520 521 /* Number of channelsmust be even*/ 522 if ((runtime->channels % 2) || (runtime->channels < 2) || 523 (runtime->channels > 10)) { 524 dev_err(player->dev, "%s: invalid nb of channels", __func__); 525 return -EINVAL; 526 } 527 528 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2); 529 530 /* Set 1-bit audio format to disabled */ 531 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player); 532 533 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player); 534 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player); 535 536 /* No iec958 formatting as outputting to DAC */ 537 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player); 538 539 return 0; 540 } 541 542 /* 543 * ALSA uniperipheral iec958 controls 544 */ 545 static int uni_player_ctl_iec958_info(struct snd_kcontrol *kcontrol, 546 struct snd_ctl_elem_info *uinfo) 547 { 548 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; 549 uinfo->count = 1; 550 551 return 0; 552 } 553 554 static int uni_player_ctl_iec958_get(struct snd_kcontrol *kcontrol, 555 struct snd_ctl_elem_value *ucontrol) 556 { 557 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol); 558 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 559 struct uniperif *player = priv->dai_data.uni; 560 struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958; 561 562 mutex_lock(&player->ctrl_lock); 563 ucontrol->value.iec958.status[0] = iec958->status[0]; 564 ucontrol->value.iec958.status[1] = iec958->status[1]; 565 ucontrol->value.iec958.status[2] = iec958->status[2]; 566 ucontrol->value.iec958.status[3] = iec958->status[3]; 567 mutex_unlock(&player->ctrl_lock); 568 return 0; 569 } 570 571 static int uni_player_ctl_iec958_put(struct snd_kcontrol *kcontrol, 572 struct snd_ctl_elem_value *ucontrol) 573 { 574 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol); 575 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 576 struct uniperif *player = priv->dai_data.uni; 577 struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958; 578 579 mutex_lock(&player->ctrl_lock); 580 iec958->status[0] = ucontrol->value.iec958.status[0]; 581 iec958->status[1] = ucontrol->value.iec958.status[1]; 582 iec958->status[2] = ucontrol->value.iec958.status[2]; 583 iec958->status[3] = ucontrol->value.iec958.status[3]; 584 mutex_unlock(&player->ctrl_lock); 585 586 uni_player_set_channel_status(player, NULL); 587 588 return 0; 589 } 590 591 static struct snd_kcontrol_new uni_player_iec958_ctl = { 592 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 593 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), 594 .info = uni_player_ctl_iec958_info, 595 .get = uni_player_ctl_iec958_get, 596 .put = uni_player_ctl_iec958_put, 597 }; 598 599 /* 600 * uniperif rate adjustement control 601 */ 602 static int snd_sti_clk_adjustment_info(struct snd_kcontrol *kcontrol, 603 struct snd_ctl_elem_info *uinfo) 604 { 605 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 606 uinfo->count = 1; 607 uinfo->value.integer.min = UNIPERIF_PLAYER_CLK_ADJ_MIN; 608 uinfo->value.integer.max = UNIPERIF_PLAYER_CLK_ADJ_MAX; 609 uinfo->value.integer.step = 1; 610 611 return 0; 612 } 613 614 static int snd_sti_clk_adjustment_get(struct snd_kcontrol *kcontrol, 615 struct snd_ctl_elem_value *ucontrol) 616 { 617 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol); 618 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 619 struct uniperif *player = priv->dai_data.uni; 620 621 mutex_lock(&player->ctrl_lock); 622 ucontrol->value.integer.value[0] = player->clk_adj; 623 mutex_unlock(&player->ctrl_lock); 624 625 return 0; 626 } 627 628 static int snd_sti_clk_adjustment_put(struct snd_kcontrol *kcontrol, 629 struct snd_ctl_elem_value *ucontrol) 630 { 631 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol); 632 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 633 struct uniperif *player = priv->dai_data.uni; 634 int ret = 0; 635 636 if ((ucontrol->value.integer.value[0] < UNIPERIF_PLAYER_CLK_ADJ_MIN) || 637 (ucontrol->value.integer.value[0] > UNIPERIF_PLAYER_CLK_ADJ_MAX)) 638 return -EINVAL; 639 640 mutex_lock(&player->ctrl_lock); 641 player->clk_adj = ucontrol->value.integer.value[0]; 642 643 if (player->mclk) 644 ret = uni_player_clk_set_rate(player, player->mclk); 645 mutex_unlock(&player->ctrl_lock); 646 647 return ret; 648 } 649 650 static struct snd_kcontrol_new uni_player_clk_adj_ctl = { 651 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 652 .name = "PCM Playback Oversampling Freq. Adjustment", 653 .info = snd_sti_clk_adjustment_info, 654 .get = snd_sti_clk_adjustment_get, 655 .put = snd_sti_clk_adjustment_put, 656 }; 657 658 static struct snd_kcontrol_new *snd_sti_pcm_ctl[] = { 659 &uni_player_clk_adj_ctl, 660 }; 661 662 static struct snd_kcontrol_new *snd_sti_iec_ctl[] = { 663 &uni_player_iec958_ctl, 664 &uni_player_clk_adj_ctl, 665 }; 666 667 static int uni_player_startup(struct snd_pcm_substream *substream, 668 struct snd_soc_dai *dai) 669 { 670 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 671 struct uniperif *player = priv->dai_data.uni; 672 673 player->clk_adj = 0; 674 675 return 0; 676 } 677 678 static int uni_player_set_sysclk(struct snd_soc_dai *dai, int clk_id, 679 unsigned int freq, int dir) 680 { 681 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 682 struct uniperif *player = priv->dai_data.uni; 683 int ret; 684 685 if (dir == SND_SOC_CLOCK_IN) 686 return 0; 687 688 if (clk_id != 0) 689 return -EINVAL; 690 691 mutex_lock(&player->ctrl_lock); 692 ret = uni_player_clk_set_rate(player, freq); 693 if (!ret) 694 player->mclk = freq; 695 mutex_unlock(&player->ctrl_lock); 696 697 return ret; 698 } 699 700 static int uni_player_prepare(struct snd_pcm_substream *substream, 701 struct snd_soc_dai *dai) 702 { 703 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 704 struct uniperif *player = priv->dai_data.uni; 705 struct snd_pcm_runtime *runtime = substream->runtime; 706 int transfer_size, trigger_limit; 707 int ret; 708 709 /* The player should be stopped */ 710 if (player->state != UNIPERIF_STATE_STOPPED) { 711 dev_err(player->dev, "%s: invalid player state %d", __func__, 712 player->state); 713 return -EINVAL; 714 } 715 716 /* Calculate transfer size (in fifo cells and bytes) for frame count */ 717 transfer_size = runtime->channels * UNIPERIF_FIFO_FRAMES; 718 719 /* Calculate number of empty cells available before asserting DREQ */ 720 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) { 721 trigger_limit = UNIPERIF_FIFO_SIZE - transfer_size; 722 } else { 723 /* 724 * Since SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0 725 * FDMA_TRIGGER_LIMIT also controls when the state switches 726 * from OFF or STANDBY to AUDIO DATA. 727 */ 728 trigger_limit = transfer_size; 729 } 730 731 /* Trigger limit must be an even number */ 732 if ((!trigger_limit % 2) || (trigger_limit != 1 && transfer_size % 2) || 733 (trigger_limit > UNIPERIF_CONFIG_DMA_TRIG_LIMIT_MASK(player))) { 734 dev_err(player->dev, "invalid trigger limit %d", trigger_limit); 735 return -EINVAL; 736 } 737 738 SET_UNIPERIF_CONFIG_DMA_TRIG_LIMIT(player, trigger_limit); 739 740 /* Uniperipheral setup depends on player type */ 741 switch (player->info->player_type) { 742 case SND_ST_UNIPERIF_PLAYER_TYPE_HDMI: 743 ret = uni_player_prepare_iec958(player, runtime); 744 break; 745 case SND_ST_UNIPERIF_PLAYER_TYPE_PCM: 746 ret = uni_player_prepare_pcm(player, runtime); 747 break; 748 case SND_ST_UNIPERIF_PLAYER_TYPE_SPDIF: 749 ret = uni_player_prepare_iec958(player, runtime); 750 break; 751 default: 752 dev_err(player->dev, "invalid player type"); 753 return -EINVAL; 754 } 755 756 if (ret) 757 return ret; 758 759 switch (player->daifmt & SND_SOC_DAIFMT_INV_MASK) { 760 case SND_SOC_DAIFMT_NB_NF: 761 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player); 762 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player); 763 break; 764 case SND_SOC_DAIFMT_NB_IF: 765 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player); 766 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player); 767 break; 768 case SND_SOC_DAIFMT_IB_NF: 769 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player); 770 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player); 771 break; 772 case SND_SOC_DAIFMT_IB_IF: 773 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player); 774 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player); 775 break; 776 } 777 778 switch (player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) { 779 case SND_SOC_DAIFMT_I2S: 780 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player); 781 SET_UNIPERIF_I2S_FMT_PADDING_I2S_MODE(player); 782 break; 783 case SND_SOC_DAIFMT_LEFT_J: 784 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player); 785 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player); 786 break; 787 case SND_SOC_DAIFMT_RIGHT_J: 788 SET_UNIPERIF_I2S_FMT_ALIGN_RIGHT(player); 789 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player); 790 break; 791 default: 792 dev_err(player->dev, "format not supported"); 793 return -EINVAL; 794 } 795 796 SET_UNIPERIF_I2S_FMT_NO_OF_SAMPLES_TO_READ(player, 0); 797 798 /* Reset uniperipheral player */ 799 SET_UNIPERIF_SOFT_RST_SOFT_RST(player); 800 801 return reset_player(player); 802 } 803 804 static int uni_player_start(struct uniperif *player) 805 { 806 int ret; 807 808 /* The player should be stopped */ 809 if (player->state != UNIPERIF_STATE_STOPPED) { 810 dev_err(player->dev, "%s: invalid player state", __func__); 811 return -EINVAL; 812 } 813 814 ret = clk_prepare_enable(player->clk); 815 if (ret) { 816 dev_err(player->dev, "%s: Failed to enable clock", __func__); 817 return ret; 818 } 819 820 /* Clear any pending interrupts */ 821 SET_UNIPERIF_ITS_BCLR(player, GET_UNIPERIF_ITS(player)); 822 823 /* Set the interrupt mask */ 824 SET_UNIPERIF_ITM_BSET_DMA_ERROR(player); 825 SET_UNIPERIF_ITM_BSET_FIFO_ERROR(player); 826 827 /* Enable underflow recovery interrupts */ 828 if (player->info->underflow_enabled) { 829 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_DONE(player); 830 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_FAILED(player); 831 } 832 833 /* Reset uniperipheral player */ 834 SET_UNIPERIF_SOFT_RST_SOFT_RST(player); 835 836 ret = reset_player(player); 837 if (ret < 0) 838 return ret; 839 840 /* 841 * Does not use IEC61937 features of the uniperipheral hardware. 842 * Instead it performs IEC61937 in software and inserts it directly 843 * into the audio data stream. As such, when encoded mode is selected, 844 * linear pcm mode is still used, but with the differences of the 845 * channel status bits set for encoded mode and the validity bits set. 846 */ 847 SET_UNIPERIF_CTRL_OPERATION_PCM_DATA(player); 848 849 /* 850 * If iec958 formatting is required for hdmi or spdif, then it must be 851 * enabled after the operation mode is set. If set prior to this, it 852 * will not take affect and hang the player. 853 */ 854 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) 855 if (UNIPERIF_PLAYER_TYPE_IS_IEC958(player)) 856 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player); 857 858 /* Force channel status update (no update if clk disable) */ 859 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) 860 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player); 861 else 862 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player); 863 864 /* Update state to started */ 865 player->state = UNIPERIF_STATE_STARTED; 866 867 return 0; 868 } 869 870 static int uni_player_stop(struct uniperif *player) 871 { 872 int ret; 873 874 /* The player should not be in stopped state */ 875 if (player->state == UNIPERIF_STATE_STOPPED) { 876 dev_err(player->dev, "%s: invalid player state", __func__); 877 return -EINVAL; 878 } 879 880 /* Turn the player off */ 881 SET_UNIPERIF_CTRL_OPERATION_OFF(player); 882 883 /* Soft reset the player */ 884 SET_UNIPERIF_SOFT_RST_SOFT_RST(player); 885 886 ret = reset_player(player); 887 if (ret < 0) 888 return ret; 889 890 /* Disable interrupts */ 891 SET_UNIPERIF_ITM_BCLR(player, GET_UNIPERIF_ITM(player)); 892 893 /* Disable clock */ 894 clk_disable_unprepare(player->clk); 895 896 /* Update state to stopped and return */ 897 player->state = UNIPERIF_STATE_STOPPED; 898 899 return 0; 900 } 901 902 int uni_player_resume(struct uniperif *player) 903 { 904 int ret; 905 906 /* Select the frequency synthesizer clock */ 907 if (player->clk_sel) { 908 ret = regmap_field_write(player->clk_sel, 1); 909 if (ret) { 910 dev_err(player->dev, 911 "%s: Failed to select freq synth clock", 912 __func__); 913 return ret; 914 } 915 } 916 917 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player); 918 SET_UNIPERIF_CTRL_ROUNDING_OFF(player); 919 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player); 920 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player); 921 922 return 0; 923 } 924 EXPORT_SYMBOL_GPL(uni_player_resume); 925 926 static int uni_player_trigger(struct snd_pcm_substream *substream, 927 int cmd, struct snd_soc_dai *dai) 928 { 929 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 930 struct uniperif *player = priv->dai_data.uni; 931 932 switch (cmd) { 933 case SNDRV_PCM_TRIGGER_START: 934 return uni_player_start(player); 935 case SNDRV_PCM_TRIGGER_STOP: 936 return uni_player_stop(player); 937 case SNDRV_PCM_TRIGGER_RESUME: 938 return uni_player_resume(player); 939 default: 940 return -EINVAL; 941 } 942 } 943 944 static void uni_player_shutdown(struct snd_pcm_substream *substream, 945 struct snd_soc_dai *dai) 946 { 947 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 948 struct uniperif *player = priv->dai_data.uni; 949 950 if (player->state != UNIPERIF_STATE_STOPPED) 951 /* Stop the player */ 952 uni_player_stop(player); 953 } 954 955 static int uni_player_parse_dt_clk_glue(struct platform_device *pdev, 956 struct uniperif *player) 957 { 958 int bit_offset; 959 struct device_node *node = pdev->dev.of_node; 960 struct regmap *regmap; 961 962 bit_offset = SYS_CFG_AUDI0_GLUE_PCM_CLKX + player->info->id; 963 964 regmap = syscon_regmap_lookup_by_phandle(node, "st,syscfg"); 965 966 if (regmap) { 967 struct reg_field regfield = 968 REG_FIELD(SYS_CFG_AUDIO_GLUE, bit_offset, bit_offset); 969 970 player->clk_sel = regmap_field_alloc(regmap, regfield); 971 } else { 972 dev_err(&pdev->dev, "sti-audio-clk-glue syscf not found\n"); 973 return -EINVAL; 974 } 975 976 return 0; 977 } 978 979 static int uni_player_parse_dt(struct platform_device *pdev, 980 struct uniperif *player) 981 { 982 struct uniperif_info *info; 983 struct device *dev = &pdev->dev; 984 struct device_node *pnode = pdev->dev.of_node; 985 const char *mode; 986 987 /* Allocate memory for the info structure */ 988 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); 989 if (!info) 990 return -ENOMEM; 991 992 if (of_property_read_u32(pnode, "version", &player->ver) || 993 player->ver == SND_ST_UNIPERIF_VERSION_UNKNOWN) { 994 dev_err(dev, "Unknown uniperipheral version "); 995 return -EINVAL; 996 } 997 /* Underflow recovery is only supported on later ip revisions */ 998 if (player->ver >= SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) 999 info->underflow_enabled = 1; 1000 1001 if (of_property_read_u32(pnode, "uniperiph-id", &info->id)) { 1002 dev_err(dev, "uniperipheral id not defined"); 1003 return -EINVAL; 1004 } 1005 1006 /* Read the device mode property */ 1007 if (of_property_read_string(pnode, "mode", &mode)) { 1008 dev_err(dev, "uniperipheral mode not defined"); 1009 return -EINVAL; 1010 } 1011 1012 if (strcasecmp(mode, "hdmi") == 0) 1013 info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_HDMI; 1014 else if (strcasecmp(mode, "pcm") == 0) 1015 info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_PCM; 1016 else if (strcasecmp(mode, "spdif") == 0) 1017 info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_SPDIF; 1018 else 1019 info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_NONE; 1020 1021 /* Save the info structure */ 1022 player->info = info; 1023 1024 /* Get the PCM_CLK_SEL bit from audio-glue-ctrl SoC register */ 1025 if (uni_player_parse_dt_clk_glue(pdev, player)) 1026 return -EINVAL; 1027 1028 return 0; 1029 } 1030 1031 static const struct snd_soc_dai_ops uni_player_dai_ops = { 1032 .startup = uni_player_startup, 1033 .shutdown = uni_player_shutdown, 1034 .prepare = uni_player_prepare, 1035 .trigger = uni_player_trigger, 1036 .hw_params = sti_uniperiph_dai_hw_params, 1037 .set_fmt = sti_uniperiph_dai_set_fmt, 1038 .set_sysclk = uni_player_set_sysclk 1039 }; 1040 1041 int uni_player_init(struct platform_device *pdev, 1042 struct uniperif *player) 1043 { 1044 int ret = 0; 1045 1046 player->dev = &pdev->dev; 1047 player->state = UNIPERIF_STATE_STOPPED; 1048 player->hw = &uni_player_pcm_hw; 1049 player->dai_ops = &uni_player_dai_ops; 1050 1051 ret = uni_player_parse_dt(pdev, player); 1052 1053 if (ret < 0) { 1054 dev_err(player->dev, "Failed to parse DeviceTree"); 1055 return ret; 1056 } 1057 1058 /* Get uniperif resource */ 1059 player->clk = of_clk_get(pdev->dev.of_node, 0); 1060 if (IS_ERR(player->clk)) 1061 ret = PTR_ERR(player->clk); 1062 1063 /* Select the frequency synthesizer clock */ 1064 if (player->clk_sel) { 1065 ret = regmap_field_write(player->clk_sel, 1); 1066 if (ret) { 1067 dev_err(player->dev, 1068 "%s: Failed to select freq synth clock", 1069 __func__); 1070 return ret; 1071 } 1072 } 1073 1074 ret = devm_request_irq(&pdev->dev, player->irq, 1075 uni_player_irq_handler, IRQF_SHARED, 1076 dev_name(&pdev->dev), player); 1077 if (ret < 0) 1078 return ret; 1079 1080 mutex_init(&player->ctrl_lock); 1081 1082 /* Ensure that disabled by default */ 1083 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player); 1084 SET_UNIPERIF_CTRL_ROUNDING_OFF(player); 1085 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player); 1086 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player); 1087 1088 if (UNIPERIF_PLAYER_TYPE_IS_IEC958(player)) { 1089 /* Set default iec958 status bits */ 1090 1091 /* Consumer, PCM, copyright, 2ch, mode 0 */ 1092 player->stream_settings.iec958.status[0] = 0x00; 1093 /* Broadcast reception category */ 1094 player->stream_settings.iec958.status[1] = 1095 IEC958_AES1_CON_GENERAL; 1096 /* Do not take into account source or channel number */ 1097 player->stream_settings.iec958.status[2] = 1098 IEC958_AES2_CON_SOURCE_UNSPEC; 1099 /* Sampling frequency not indicated */ 1100 player->stream_settings.iec958.status[3] = 1101 IEC958_AES3_CON_FS_NOTID; 1102 /* Max sample word 24-bit, sample word length not indicated */ 1103 player->stream_settings.iec958.status[4] = 1104 IEC958_AES4_CON_MAX_WORDLEN_24 | 1105 IEC958_AES4_CON_WORDLEN_24_20; 1106 1107 player->num_ctrls = ARRAY_SIZE(snd_sti_iec_ctl); 1108 player->snd_ctrls = snd_sti_iec_ctl[0]; 1109 } else { 1110 player->num_ctrls = ARRAY_SIZE(snd_sti_pcm_ctl); 1111 player->snd_ctrls = snd_sti_pcm_ctl[0]; 1112 } 1113 1114 return 0; 1115 } 1116 EXPORT_SYMBOL_GPL(uni_player_init); 1117