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) { 255 switch (runtime->rate) { 256 case 22050: 257 player->stream_settings.iec958.status[3] = 258 IEC958_AES3_CON_FS_22050; 259 break; 260 case 44100: 261 player->stream_settings.iec958.status[3] = 262 IEC958_AES3_CON_FS_44100; 263 break; 264 case 88200: 265 player->stream_settings.iec958.status[3] = 266 IEC958_AES3_CON_FS_88200; 267 break; 268 case 176400: 269 player->stream_settings.iec958.status[3] = 270 IEC958_AES3_CON_FS_176400; 271 break; 272 case 24000: 273 player->stream_settings.iec958.status[3] = 274 IEC958_AES3_CON_FS_24000; 275 break; 276 case 48000: 277 player->stream_settings.iec958.status[3] = 278 IEC958_AES3_CON_FS_48000; 279 break; 280 case 96000: 281 player->stream_settings.iec958.status[3] = 282 IEC958_AES3_CON_FS_96000; 283 break; 284 case 192000: 285 player->stream_settings.iec958.status[3] = 286 IEC958_AES3_CON_FS_192000; 287 break; 288 case 32000: 289 player->stream_settings.iec958.status[3] = 290 IEC958_AES3_CON_FS_32000; 291 break; 292 default: 293 /* Mark as sampling frequency not indicated */ 294 player->stream_settings.iec958.status[3] = 295 IEC958_AES3_CON_FS_NOTID; 296 break; 297 } 298 } 299 300 /* Audio mode: 301 * Use audio mode status to select PCM or encoded mode 302 */ 303 if (player->stream_settings.iec958.status[0] & IEC958_AES0_NONAUDIO) 304 player->stream_settings.encoding_mode = 305 UNIPERIF_IEC958_ENCODING_MODE_ENCODED; 306 else 307 player->stream_settings.encoding_mode = 308 UNIPERIF_IEC958_ENCODING_MODE_PCM; 309 310 if (player->stream_settings.encoding_mode == 311 UNIPERIF_IEC958_ENCODING_MODE_PCM) 312 /* Clear user validity bits */ 313 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0); 314 else 315 /* Set user validity bits */ 316 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 1); 317 318 /* Program the new channel status */ 319 for (n = 0; n < 6; ++n) { 320 status = 321 player->stream_settings.iec958.status[0 + (n * 4)] & 0xf; 322 status |= 323 player->stream_settings.iec958.status[1 + (n * 4)] << 8; 324 status |= 325 player->stream_settings.iec958.status[2 + (n * 4)] << 16; 326 status |= 327 player->stream_settings.iec958.status[3 + (n * 4)] << 24; 328 SET_UNIPERIF_CHANNEL_STA_REGN(player, n, status); 329 } 330 mutex_unlock(&player->ctrl_lock); 331 332 /* Update the channel status */ 333 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) 334 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player); 335 else 336 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player); 337 } 338 339 static int uni_player_prepare_iec958(struct uniperif *player, 340 struct snd_pcm_runtime *runtime) 341 { 342 int clk_div; 343 344 clk_div = player->mclk / runtime->rate; 345 346 /* Oversampling must be multiple of 128 as iec958 frame is 32-bits */ 347 if ((clk_div % 128) || (clk_div <= 0)) { 348 dev_err(player->dev, "%s: invalid clk_div %d", 349 __func__, clk_div); 350 return -EINVAL; 351 } 352 353 switch (runtime->format) { 354 case SNDRV_PCM_FORMAT_S16_LE: 355 /* 16/16 memory format */ 356 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player); 357 /* 16-bits per sub-frame */ 358 SET_UNIPERIF_I2S_FMT_NBIT_32(player); 359 /* Set 16-bit sample precision */ 360 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player); 361 break; 362 case SNDRV_PCM_FORMAT_S32_LE: 363 /* 16/0 memory format */ 364 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player); 365 /* 32-bits per sub-frame */ 366 SET_UNIPERIF_I2S_FMT_NBIT_32(player); 367 /* Set 24-bit sample precision */ 368 SET_UNIPERIF_I2S_FMT_DATA_SIZE_24(player); 369 break; 370 default: 371 dev_err(player->dev, "format not supported"); 372 return -EINVAL; 373 } 374 375 /* Set parity to be calculated by the hardware */ 376 SET_UNIPERIF_CONFIG_PARITY_CNTR_BY_HW(player); 377 378 /* Set channel status bits to be inserted by the hardware */ 379 SET_UNIPERIF_CONFIG_CHANNEL_STA_CNTR_BY_HW(player); 380 381 /* Set user data bits to be inserted by the hardware */ 382 SET_UNIPERIF_CONFIG_USER_DAT_CNTR_BY_HW(player); 383 384 /* Set validity bits to be inserted by the hardware */ 385 SET_UNIPERIF_CONFIG_VALIDITY_DAT_CNTR_BY_HW(player); 386 387 /* Set full software control to disabled */ 388 SET_UNIPERIF_CONFIG_SPDIF_SW_CTRL_DISABLE(player); 389 390 SET_UNIPERIF_CTRL_ZERO_STUFF_HW(player); 391 392 /* Update the channel status */ 393 uni_player_set_channel_status(player, runtime); 394 395 /* Clear the user validity user bits */ 396 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0); 397 398 /* Disable one-bit audio mode */ 399 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player); 400 401 /* Enable consecutive frames repetition of Z preamble (not for HBRA) */ 402 SET_UNIPERIF_CONFIG_REPEAT_CHL_STS_ENABLE(player); 403 404 /* Change to SUF0_SUBF1 and left/right channels swap! */ 405 SET_UNIPERIF_CONFIG_SUBFRAME_SEL_SUBF1_SUBF0(player); 406 407 /* Set data output as MSB first */ 408 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player); 409 410 if (player->stream_settings.encoding_mode == 411 UNIPERIF_IEC958_ENCODING_MODE_ENCODED) 412 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_ON(player); 413 else 414 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_OFF(player); 415 416 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2); 417 418 /* Set rounding to off */ 419 SET_UNIPERIF_CTRL_ROUNDING_OFF(player); 420 421 /* Set clock divisor */ 422 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / 128); 423 424 /* Set the spdif latency to not wait before starting player */ 425 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player); 426 427 /* 428 * Ensure iec958 formatting is off. It will be enabled in function 429 * uni_player_start() at the same time as the operation 430 * mode is set to work around a silicon issue. 431 */ 432 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) 433 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player); 434 else 435 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player); 436 437 return 0; 438 } 439 440 static int uni_player_prepare_pcm(struct uniperif *player, 441 struct snd_pcm_runtime *runtime) 442 { 443 int output_frame_size, slot_width, clk_div; 444 445 /* Force slot width to 32 in I2S mode (HW constraint) */ 446 if ((player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) == 447 SND_SOC_DAIFMT_I2S) { 448 slot_width = 32; 449 } else { 450 switch (runtime->format) { 451 case SNDRV_PCM_FORMAT_S16_LE: 452 slot_width = 16; 453 break; 454 default: 455 slot_width = 32; 456 break; 457 } 458 } 459 output_frame_size = slot_width * runtime->channels; 460 461 clk_div = player->mclk / runtime->rate; 462 /* 463 * For 32 bits subframe clk_div must be a multiple of 128, 464 * for 16 bits must be a multiple of 64 465 */ 466 if ((slot_width == 32) && (clk_div % 128)) { 467 dev_err(player->dev, "%s: invalid clk_div", __func__); 468 return -EINVAL; 469 } 470 471 if ((slot_width == 16) && (clk_div % 64)) { 472 dev_err(player->dev, "%s: invalid clk_div", __func__); 473 return -EINVAL; 474 } 475 476 /* 477 * Number of bits per subframe (which is one channel sample) 478 * on output - Transfer 16 or 32 bits from FIFO 479 */ 480 switch (slot_width) { 481 case 32: 482 SET_UNIPERIF_I2S_FMT_NBIT_32(player); 483 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player); 484 break; 485 case 16: 486 SET_UNIPERIF_I2S_FMT_NBIT_16(player); 487 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player); 488 break; 489 default: 490 dev_err(player->dev, "subframe format not supported"); 491 return -EINVAL; 492 } 493 494 /* Configure data memory format */ 495 switch (runtime->format) { 496 case SNDRV_PCM_FORMAT_S16_LE: 497 /* One data word contains two samples */ 498 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player); 499 break; 500 501 case SNDRV_PCM_FORMAT_S32_LE: 502 /* 503 * Actually "16 bits/0 bits" means "32/28/24/20/18/16 bits 504 * on the left than zeros (if less than 32 bytes)"... ;-) 505 */ 506 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player); 507 break; 508 509 default: 510 dev_err(player->dev, "format not supported"); 511 return -EINVAL; 512 } 513 514 /* Set rounding to off */ 515 SET_UNIPERIF_CTRL_ROUNDING_OFF(player); 516 517 /* Set clock divisor */ 518 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / (2 * output_frame_size)); 519 520 /* Number of channelsmust be even*/ 521 if ((runtime->channels % 2) || (runtime->channels < 2) || 522 (runtime->channels > 10)) { 523 dev_err(player->dev, "%s: invalid nb of channels", __func__); 524 return -EINVAL; 525 } 526 527 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2); 528 529 /* Set 1-bit audio format to disabled */ 530 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player); 531 532 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player); 533 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player); 534 535 /* No iec958 formatting as outputting to DAC */ 536 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player); 537 538 return 0; 539 } 540 541 /* 542 * ALSA uniperipheral iec958 controls 543 */ 544 static int uni_player_ctl_iec958_info(struct snd_kcontrol *kcontrol, 545 struct snd_ctl_elem_info *uinfo) 546 { 547 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; 548 uinfo->count = 1; 549 550 return 0; 551 } 552 553 static int uni_player_ctl_iec958_get(struct snd_kcontrol *kcontrol, 554 struct snd_ctl_elem_value *ucontrol) 555 { 556 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol); 557 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 558 struct uniperif *player = priv->dai_data.uni; 559 struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958; 560 561 mutex_lock(&player->ctrl_lock); 562 ucontrol->value.iec958.status[0] = iec958->status[0]; 563 ucontrol->value.iec958.status[1] = iec958->status[1]; 564 ucontrol->value.iec958.status[2] = iec958->status[2]; 565 ucontrol->value.iec958.status[3] = iec958->status[3]; 566 mutex_unlock(&player->ctrl_lock); 567 return 0; 568 } 569 570 static int uni_player_ctl_iec958_put(struct snd_kcontrol *kcontrol, 571 struct snd_ctl_elem_value *ucontrol) 572 { 573 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol); 574 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 575 struct uniperif *player = priv->dai_data.uni; 576 struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958; 577 578 mutex_lock(&player->ctrl_lock); 579 iec958->status[0] = ucontrol->value.iec958.status[0]; 580 iec958->status[1] = ucontrol->value.iec958.status[1]; 581 iec958->status[2] = ucontrol->value.iec958.status[2]; 582 iec958->status[3] = ucontrol->value.iec958.status[3]; 583 mutex_unlock(&player->ctrl_lock); 584 585 uni_player_set_channel_status(player, NULL); 586 587 return 0; 588 } 589 590 static struct snd_kcontrol_new uni_player_iec958_ctl = { 591 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 592 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT), 593 .info = uni_player_ctl_iec958_info, 594 .get = uni_player_ctl_iec958_get, 595 .put = uni_player_ctl_iec958_put, 596 }; 597 598 /* 599 * uniperif rate adjustement control 600 */ 601 static int snd_sti_clk_adjustment_info(struct snd_kcontrol *kcontrol, 602 struct snd_ctl_elem_info *uinfo) 603 { 604 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 605 uinfo->count = 1; 606 uinfo->value.integer.min = UNIPERIF_PLAYER_CLK_ADJ_MIN; 607 uinfo->value.integer.max = UNIPERIF_PLAYER_CLK_ADJ_MAX; 608 uinfo->value.integer.step = 1; 609 610 return 0; 611 } 612 613 static int snd_sti_clk_adjustment_get(struct snd_kcontrol *kcontrol, 614 struct snd_ctl_elem_value *ucontrol) 615 { 616 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol); 617 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 618 struct uniperif *player = priv->dai_data.uni; 619 620 mutex_lock(&player->ctrl_lock); 621 ucontrol->value.integer.value[0] = player->clk_adj; 622 mutex_unlock(&player->ctrl_lock); 623 624 return 0; 625 } 626 627 static int snd_sti_clk_adjustment_put(struct snd_kcontrol *kcontrol, 628 struct snd_ctl_elem_value *ucontrol) 629 { 630 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol); 631 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 632 struct uniperif *player = priv->dai_data.uni; 633 int ret = 0; 634 635 if ((ucontrol->value.integer.value[0] < UNIPERIF_PLAYER_CLK_ADJ_MIN) || 636 (ucontrol->value.integer.value[0] > UNIPERIF_PLAYER_CLK_ADJ_MAX)) 637 return -EINVAL; 638 639 mutex_lock(&player->ctrl_lock); 640 player->clk_adj = ucontrol->value.integer.value[0]; 641 642 if (player->mclk) 643 ret = uni_player_clk_set_rate(player, player->mclk); 644 mutex_unlock(&player->ctrl_lock); 645 646 return ret; 647 } 648 649 static struct snd_kcontrol_new uni_player_clk_adj_ctl = { 650 .iface = SNDRV_CTL_ELEM_IFACE_PCM, 651 .name = "PCM Playback Oversampling Freq. Adjustment", 652 .info = snd_sti_clk_adjustment_info, 653 .get = snd_sti_clk_adjustment_get, 654 .put = snd_sti_clk_adjustment_put, 655 }; 656 657 static struct snd_kcontrol_new *snd_sti_pcm_ctl[] = { 658 &uni_player_clk_adj_ctl, 659 }; 660 661 static struct snd_kcontrol_new *snd_sti_iec_ctl[] = { 662 &uni_player_iec958_ctl, 663 &uni_player_clk_adj_ctl, 664 }; 665 666 static int uni_player_startup(struct snd_pcm_substream *substream, 667 struct snd_soc_dai *dai) 668 { 669 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai); 670 struct uniperif *player = priv->dai_data.uni; 671 player->substream = substream; 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 player->substream = NULL; 955 } 956 957 static int uni_player_parse_dt_clk_glue(struct platform_device *pdev, 958 struct uniperif *player) 959 { 960 int bit_offset; 961 struct device_node *node = pdev->dev.of_node; 962 struct regmap *regmap; 963 964 bit_offset = SYS_CFG_AUDI0_GLUE_PCM_CLKX + player->info->id; 965 966 regmap = syscon_regmap_lookup_by_phandle(node, "st,syscfg"); 967 968 if (regmap) { 969 struct reg_field regfield = 970 REG_FIELD(SYS_CFG_AUDIO_GLUE, bit_offset, bit_offset); 971 972 player->clk_sel = regmap_field_alloc(regmap, regfield); 973 } else { 974 dev_err(&pdev->dev, "sti-audio-clk-glue syscf not found\n"); 975 return -EINVAL; 976 } 977 978 return 0; 979 } 980 981 static int uni_player_parse_dt(struct platform_device *pdev, 982 struct uniperif *player) 983 { 984 struct uniperif_info *info; 985 struct device *dev = &pdev->dev; 986 struct device_node *pnode = pdev->dev.of_node; 987 const char *mode; 988 989 /* Allocate memory for the info structure */ 990 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); 991 if (!info) 992 return -ENOMEM; 993 994 if (of_property_read_u32(pnode, "st,version", &player->ver) || 995 player->ver == SND_ST_UNIPERIF_VERSION_UNKNOWN) { 996 dev_err(dev, "Unknown uniperipheral version "); 997 return -EINVAL; 998 } 999 /* Underflow recovery is only supported on later ip revisions */ 1000 if (player->ver >= SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) 1001 info->underflow_enabled = 1; 1002 1003 if (of_property_read_u32(pnode, "st,uniperiph-id", &info->id)) { 1004 dev_err(dev, "uniperipheral id not defined"); 1005 return -EINVAL; 1006 } 1007 1008 /* Read the device mode property */ 1009 if (of_property_read_string(pnode, "st,mode", &mode)) { 1010 dev_err(dev, "uniperipheral mode not defined"); 1011 return -EINVAL; 1012 } 1013 1014 if (strcasecmp(mode, "hdmi") == 0) 1015 info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_HDMI; 1016 else if (strcasecmp(mode, "pcm") == 0) 1017 info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_PCM; 1018 else if (strcasecmp(mode, "spdif") == 0) 1019 info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_SPDIF; 1020 else 1021 info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_NONE; 1022 1023 /* Save the info structure */ 1024 player->info = info; 1025 1026 /* Get the PCM_CLK_SEL bit from audio-glue-ctrl SoC register */ 1027 if (uni_player_parse_dt_clk_glue(pdev, player)) 1028 return -EINVAL; 1029 1030 return 0; 1031 } 1032 1033 static const struct snd_soc_dai_ops uni_player_dai_ops = { 1034 .startup = uni_player_startup, 1035 .shutdown = uni_player_shutdown, 1036 .prepare = uni_player_prepare, 1037 .trigger = uni_player_trigger, 1038 .hw_params = sti_uniperiph_dai_hw_params, 1039 .set_fmt = sti_uniperiph_dai_set_fmt, 1040 .set_sysclk = uni_player_set_sysclk 1041 }; 1042 1043 int uni_player_init(struct platform_device *pdev, 1044 struct uniperif *player) 1045 { 1046 int ret = 0; 1047 1048 player->dev = &pdev->dev; 1049 player->state = UNIPERIF_STATE_STOPPED; 1050 player->hw = &uni_player_pcm_hw; 1051 player->dai_ops = &uni_player_dai_ops; 1052 1053 ret = uni_player_parse_dt(pdev, player); 1054 1055 if (ret < 0) { 1056 dev_err(player->dev, "Failed to parse DeviceTree"); 1057 return ret; 1058 } 1059 1060 /* Get uniperif resource */ 1061 player->clk = of_clk_get(pdev->dev.of_node, 0); 1062 if (IS_ERR(player->clk)) 1063 ret = PTR_ERR(player->clk); 1064 1065 /* Select the frequency synthesizer clock */ 1066 if (player->clk_sel) { 1067 ret = regmap_field_write(player->clk_sel, 1); 1068 if (ret) { 1069 dev_err(player->dev, 1070 "%s: Failed to select freq synth clock", 1071 __func__); 1072 return ret; 1073 } 1074 } 1075 1076 ret = devm_request_irq(&pdev->dev, player->irq, 1077 uni_player_irq_handler, IRQF_SHARED, 1078 dev_name(&pdev->dev), player); 1079 if (ret < 0) 1080 return ret; 1081 1082 mutex_init(&player->ctrl_lock); 1083 1084 /* Ensure that disabled by default */ 1085 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player); 1086 SET_UNIPERIF_CTRL_ROUNDING_OFF(player); 1087 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player); 1088 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player); 1089 1090 if (UNIPERIF_PLAYER_TYPE_IS_IEC958(player)) { 1091 /* Set default iec958 status bits */ 1092 1093 /* Consumer, PCM, copyright, 2ch, mode 0 */ 1094 player->stream_settings.iec958.status[0] = 0x00; 1095 /* Broadcast reception category */ 1096 player->stream_settings.iec958.status[1] = 1097 IEC958_AES1_CON_GENERAL; 1098 /* Do not take into account source or channel number */ 1099 player->stream_settings.iec958.status[2] = 1100 IEC958_AES2_CON_SOURCE_UNSPEC; 1101 /* Sampling frequency not indicated */ 1102 player->stream_settings.iec958.status[3] = 1103 IEC958_AES3_CON_FS_NOTID; 1104 /* Max sample word 24-bit, sample word length not indicated */ 1105 player->stream_settings.iec958.status[4] = 1106 IEC958_AES4_CON_MAX_WORDLEN_24 | 1107 IEC958_AES4_CON_WORDLEN_24_20; 1108 1109 player->num_ctrls = ARRAY_SIZE(snd_sti_iec_ctl); 1110 player->snd_ctrls = snd_sti_iec_ctl[0]; 1111 } else { 1112 player->num_ctrls = ARRAY_SIZE(snd_sti_pcm_ctl); 1113 player->snd_ctrls = snd_sti_pcm_ctl[0]; 1114 } 1115 1116 return 0; 1117 } 1118 EXPORT_SYMBOL_GPL(uni_player_init); 1119