1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) STMicroelectronics SA 2015
4 * Authors: Arnaud Pouliquen <arnaud.pouliquen@st.com>
5 * for STMicroelectronics.
9 #include <linux/mfd/syscon.h>
11 #include <sound/asoundef.h>
12 #include <sound/soc.h>
17 * Some hardware-related definitions
20 /* sys config registers definitions */
21 #define SYS_CFG_AUDIO_GLUE 0xA4
24 * Driver specific types.
27 #define UNIPERIF_PLAYER_CLK_ADJ_MIN -999999
28 #define UNIPERIF_PLAYER_CLK_ADJ_MAX 1000000
29 #define UNIPERIF_PLAYER_I2S_OUT 1 /* player id connected to I2S/TDM TX bus */
32 * Note: snd_pcm_hardware is linked to DMA controller but is declared here to
33 * integrate DAI_CPU capability in term of rate and supported channels
35 static const struct snd_pcm_hardware uni_player_pcm_hw = {
36 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
37 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP |
38 SNDRV_PCM_INFO_MMAP_VALID,
39 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE,
41 .rates = SNDRV_PCM_RATE_CONTINUOUS,
51 .period_bytes_min = 128,
52 .period_bytes_max = 64 * PAGE_SIZE,
53 .buffer_bytes_max = 256 * PAGE_SIZE
57 * uni_player_irq_handler
58 * In case of error audio stream is stopped; stop action is protected via PCM
59 * stream lock to avoid race condition with trigger callback.
61 static irqreturn_t uni_player_irq_handler(int irq, void *dev_id)
63 irqreturn_t ret = IRQ_NONE;
64 struct uniperif *player = dev_id;
68 spin_lock(&player->irq_lock);
69 if (!player->substream)
72 snd_pcm_stream_lock(player->substream);
73 if (player->state == UNIPERIF_STATE_STOPPED)
76 /* Get interrupt status & clear them immediately */
77 status = GET_UNIPERIF_ITS(player);
78 SET_UNIPERIF_ITS_BCLR(player, status);
80 /* Check for fifo error (underrun) */
81 if (unlikely(status & UNIPERIF_ITS_FIFO_ERROR_MASK(player))) {
82 dev_err(player->dev, "FIFO underflow error detected\n");
84 /* Interrupt is just for information when underflow recovery */
85 if (player->underflow_enabled) {
86 /* Update state to underflow */
87 player->state = UNIPERIF_STATE_UNDERFLOW;
90 /* Disable interrupt so doesn't continually fire */
91 SET_UNIPERIF_ITM_BCLR_FIFO_ERROR(player);
94 snd_pcm_stop_xrun(player->substream);
100 /* Check for dma error (overrun) */
101 if (unlikely(status & UNIPERIF_ITS_DMA_ERROR_MASK(player))) {
102 dev_err(player->dev, "DMA error detected\n");
104 /* Disable interrupt so doesn't continually fire */
105 SET_UNIPERIF_ITM_BCLR_DMA_ERROR(player);
107 /* Stop the player */
108 snd_pcm_stop_xrun(player->substream);
113 /* Check for underflow recovery done */
114 if (unlikely(status & UNIPERIF_ITM_UNDERFLOW_REC_DONE_MASK(player))) {
115 if (!player->underflow_enabled) {
117 "unexpected Underflow recovering\n");
121 /* Read the underflow recovery duration */
122 tmp = GET_UNIPERIF_STATUS_1_UNDERFLOW_DURATION(player);
123 dev_dbg(player->dev, "Underflow recovered (%d LR clocks max)\n",
126 /* Clear the underflow recovery duration */
127 SET_UNIPERIF_BIT_CONTROL_CLR_UNDERFLOW_DURATION(player);
129 /* Update state to started */
130 player->state = UNIPERIF_STATE_STARTED;
135 /* Check if underflow recovery failed */
136 if (unlikely(status &
137 UNIPERIF_ITM_UNDERFLOW_REC_FAILED_MASK(player))) {
138 dev_err(player->dev, "Underflow recovery failed\n");
140 /* Stop the player */
141 snd_pcm_stop_xrun(player->substream);
147 snd_pcm_stream_unlock(player->substream);
149 spin_unlock(&player->irq_lock);
154 static int uni_player_clk_set_rate(struct uniperif *player, unsigned long rate)
156 int rate_adjusted, rate_achieved, delta, ret;
157 int adjustment = player->clk_adj;
161 * F = f + --------- * f = f + d
170 * a - adjustment in ppm (parts per milion)
171 * F - rate to be set in synthesizer
172 * d - delta (difference) between f and F
174 if (adjustment < 0) {
175 /* div64_64 operates on unsigned values... */
177 adjustment = -adjustment;
181 /* 500000 ppm is 0.5, which is used to round up values */
182 delta *= (int)div64_u64((uint64_t)rate *
183 (uint64_t)adjustment + 500000, 1000000);
184 rate_adjusted = rate + delta;
186 /* Adjusted rate should never be == 0 */
190 ret = clk_set_rate(player->clk, rate_adjusted);
194 rate_achieved = clk_get_rate(player->clk);
196 /* If value is 0 means that clock or parent not valid */
200 * Using ALSA's adjustment control, we can modify the rate to be up
201 * to twice as much as requested, but no more
203 delta = rate_achieved - rate;
205 /* div64_64 operates on unsigned values... */
211 /* Frequency/2 is added to round up result */
212 adjustment *= (int)div64_u64((uint64_t)delta * 1000000 + rate / 2,
214 player->clk_adj = adjustment;
218 static void uni_player_set_channel_status(struct uniperif *player,
219 struct snd_pcm_runtime *runtime)
225 * Some AVRs and TVs require the channel status to contain a correct
226 * sampling frequency. If no sample rate is already specified, then
229 mutex_lock(&player->ctrl_lock);
231 switch (runtime->rate) {
233 player->stream_settings.iec958.status[3] =
234 IEC958_AES3_CON_FS_22050;
237 player->stream_settings.iec958.status[3] =
238 IEC958_AES3_CON_FS_44100;
241 player->stream_settings.iec958.status[3] =
242 IEC958_AES3_CON_FS_88200;
245 player->stream_settings.iec958.status[3] =
246 IEC958_AES3_CON_FS_176400;
249 player->stream_settings.iec958.status[3] =
250 IEC958_AES3_CON_FS_24000;
253 player->stream_settings.iec958.status[3] =
254 IEC958_AES3_CON_FS_48000;
257 player->stream_settings.iec958.status[3] =
258 IEC958_AES3_CON_FS_96000;
261 player->stream_settings.iec958.status[3] =
262 IEC958_AES3_CON_FS_192000;
265 player->stream_settings.iec958.status[3] =
266 IEC958_AES3_CON_FS_32000;
269 /* Mark as sampling frequency not indicated */
270 player->stream_settings.iec958.status[3] =
271 IEC958_AES3_CON_FS_NOTID;
277 * Use audio mode status to select PCM or encoded mode
279 if (player->stream_settings.iec958.status[0] & IEC958_AES0_NONAUDIO)
280 player->stream_settings.encoding_mode =
281 UNIPERIF_IEC958_ENCODING_MODE_ENCODED;
283 player->stream_settings.encoding_mode =
284 UNIPERIF_IEC958_ENCODING_MODE_PCM;
286 if (player->stream_settings.encoding_mode ==
287 UNIPERIF_IEC958_ENCODING_MODE_PCM)
288 /* Clear user validity bits */
289 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0);
291 /* Set user validity bits */
292 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 1);
294 /* Program the new channel status */
295 for (n = 0; n < 6; ++n) {
297 player->stream_settings.iec958.status[0 + (n * 4)] & 0xf;
299 player->stream_settings.iec958.status[1 + (n * 4)] << 8;
301 player->stream_settings.iec958.status[2 + (n * 4)] << 16;
303 player->stream_settings.iec958.status[3 + (n * 4)] << 24;
304 SET_UNIPERIF_CHANNEL_STA_REGN(player, n, status);
306 mutex_unlock(&player->ctrl_lock);
308 /* Update the channel status */
309 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
310 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player);
312 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player);
315 static int uni_player_prepare_iec958(struct uniperif *player,
316 struct snd_pcm_runtime *runtime)
320 clk_div = player->mclk / runtime->rate;
322 /* Oversampling must be multiple of 128 as iec958 frame is 32-bits */
323 if ((clk_div % 128) || (clk_div <= 0)) {
324 dev_err(player->dev, "%s: invalid clk_div %d\n",
329 switch (runtime->format) {
330 case SNDRV_PCM_FORMAT_S16_LE:
331 /* 16/16 memory format */
332 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player);
333 /* 16-bits per sub-frame */
334 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
335 /* Set 16-bit sample precision */
336 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player);
338 case SNDRV_PCM_FORMAT_S32_LE:
339 /* 16/0 memory format */
340 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
341 /* 32-bits per sub-frame */
342 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
343 /* Set 24-bit sample precision */
344 SET_UNIPERIF_I2S_FMT_DATA_SIZE_24(player);
347 dev_err(player->dev, "format not supported\n");
351 /* Set parity to be calculated by the hardware */
352 SET_UNIPERIF_CONFIG_PARITY_CNTR_BY_HW(player);
354 /* Set channel status bits to be inserted by the hardware */
355 SET_UNIPERIF_CONFIG_CHANNEL_STA_CNTR_BY_HW(player);
357 /* Set user data bits to be inserted by the hardware */
358 SET_UNIPERIF_CONFIG_USER_DAT_CNTR_BY_HW(player);
360 /* Set validity bits to be inserted by the hardware */
361 SET_UNIPERIF_CONFIG_VALIDITY_DAT_CNTR_BY_HW(player);
363 /* Set full software control to disabled */
364 SET_UNIPERIF_CONFIG_SPDIF_SW_CTRL_DISABLE(player);
366 SET_UNIPERIF_CTRL_ZERO_STUFF_HW(player);
368 /* Update the channel status */
369 uni_player_set_channel_status(player, runtime);
371 /* Clear the user validity user bits */
372 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0);
374 /* Disable one-bit audio mode */
375 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player);
377 /* Enable consecutive frames repetition of Z preamble (not for HBRA) */
378 SET_UNIPERIF_CONFIG_REPEAT_CHL_STS_ENABLE(player);
380 /* Change to SUF0_SUBF1 and left/right channels swap! */
381 SET_UNIPERIF_CONFIG_SUBFRAME_SEL_SUBF1_SUBF0(player);
383 /* Set data output as MSB first */
384 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
386 if (player->stream_settings.encoding_mode ==
387 UNIPERIF_IEC958_ENCODING_MODE_ENCODED)
388 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_ON(player);
390 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_OFF(player);
392 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2);
394 /* Set rounding to off */
395 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
397 /* Set clock divisor */
398 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / 128);
400 /* Set the spdif latency to not wait before starting player */
401 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
404 * Ensure iec958 formatting is off. It will be enabled in function
405 * uni_player_start() at the same time as the operation
406 * mode is set to work around a silicon issue.
408 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
409 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player);
411 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player);
416 static int uni_player_prepare_pcm(struct uniperif *player,
417 struct snd_pcm_runtime *runtime)
419 int output_frame_size, slot_width, clk_div;
421 /* Force slot width to 32 in I2S mode (HW constraint) */
422 if ((player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
426 slot_width = snd_pcm_format_width(runtime->format);
428 output_frame_size = slot_width * runtime->channels;
430 clk_div = player->mclk / runtime->rate;
432 * For 32 bits subframe clk_div must be a multiple of 128,
433 * for 16 bits must be a multiple of 64
435 if ((slot_width == 32) && (clk_div % 128)) {
436 dev_err(player->dev, "%s: invalid clk_div\n", __func__);
440 if ((slot_width == 16) && (clk_div % 64)) {
441 dev_err(player->dev, "%s: invalid clk_div\n", __func__);
446 * Number of bits per subframe (which is one channel sample)
447 * on output - Transfer 16 or 32 bits from FIFO
449 switch (slot_width) {
451 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
452 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player);
455 SET_UNIPERIF_I2S_FMT_NBIT_16(player);
456 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player);
459 dev_err(player->dev, "subframe format not supported\n");
463 /* Configure data memory format */
464 switch (runtime->format) {
465 case SNDRV_PCM_FORMAT_S16_LE:
466 /* One data word contains two samples */
467 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player);
470 case SNDRV_PCM_FORMAT_S32_LE:
472 * Actually "16 bits/0 bits" means "32/28/24/20/18/16 bits
473 * on the left than zeros (if less than 32 bytes)"... ;-)
475 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
479 dev_err(player->dev, "format not supported\n");
483 /* Set rounding to off */
484 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
486 /* Set clock divisor */
487 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / (2 * output_frame_size));
489 /* Number of channelsmust be even*/
490 if ((runtime->channels % 2) || (runtime->channels < 2) ||
491 (runtime->channels > 10)) {
492 dev_err(player->dev, "%s: invalid nb of channels\n", __func__);
496 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2);
498 /* Set 1-bit audio format to disabled */
499 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player);
501 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
503 /* No iec958 formatting as outputting to DAC */
504 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player);
509 static int uni_player_prepare_tdm(struct uniperif *player,
510 struct snd_pcm_runtime *runtime)
512 int tdm_frame_size; /* unip tdm frame size in bytes */
513 int user_frame_size; /* user tdm frame size in bytes */
514 /* default unip TDM_WORD_POS_X_Y */
515 unsigned int word_pos[4] = {
516 0x04060002, 0x0C0E080A, 0x14161012, 0x1C1E181A};
520 sti_uniperiph_get_unip_tdm_frame_size(player);
522 sti_uniperiph_get_user_frame_size(runtime);
524 /* fix 16/0 format */
525 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
526 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player);
528 /* number of words inserted on the TDM line */
529 SET_UNIPERIF_I2S_FMT_NUM_CH(player, user_frame_size / 4 / 2);
531 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
532 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
534 /* Enable the tdm functionality */
535 SET_UNIPERIF_TDM_ENABLE_TDM_ENABLE(player);
537 /* number of 8 bits timeslots avail in unip tdm frame */
538 SET_UNIPERIF_TDM_FS_REF_DIV_NUM_TIMESLOT(player, tdm_frame_size);
540 /* set the timeslot allocation for words in FIFO */
541 sti_uniperiph_get_tdm_word_pos(player, word_pos);
542 SET_UNIPERIF_TDM_WORD_POS(player, 1_2, word_pos[WORD_1_2]);
543 SET_UNIPERIF_TDM_WORD_POS(player, 3_4, word_pos[WORD_3_4]);
544 SET_UNIPERIF_TDM_WORD_POS(player, 5_6, word_pos[WORD_5_6]);
545 SET_UNIPERIF_TDM_WORD_POS(player, 7_8, word_pos[WORD_7_8]);
547 /* set unip clk rate (not done vai set_sysclk ops) */
548 freq = runtime->rate * tdm_frame_size * 8;
549 mutex_lock(&player->ctrl_lock);
550 ret = uni_player_clk_set_rate(player, freq);
553 mutex_unlock(&player->ctrl_lock);
559 * ALSA uniperipheral iec958 controls
561 static int uni_player_ctl_iec958_info(struct snd_kcontrol *kcontrol,
562 struct snd_ctl_elem_info *uinfo)
564 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
570 static int uni_player_ctl_iec958_get(struct snd_kcontrol *kcontrol,
571 struct snd_ctl_elem_value *ucontrol)
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;
578 mutex_lock(&player->ctrl_lock);
579 ucontrol->value.iec958.status[0] = iec958->status[0];
580 ucontrol->value.iec958.status[1] = iec958->status[1];
581 ucontrol->value.iec958.status[2] = iec958->status[2];
582 ucontrol->value.iec958.status[3] = iec958->status[3];
583 mutex_unlock(&player->ctrl_lock);
587 static int uni_player_ctl_iec958_put(struct snd_kcontrol *kcontrol,
588 struct snd_ctl_elem_value *ucontrol)
590 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
591 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
592 struct uniperif *player = priv->dai_data.uni;
593 struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958;
596 mutex_lock(&player->ctrl_lock);
597 iec958->status[0] = ucontrol->value.iec958.status[0];
598 iec958->status[1] = ucontrol->value.iec958.status[1];
599 iec958->status[2] = ucontrol->value.iec958.status[2];
600 iec958->status[3] = ucontrol->value.iec958.status[3];
601 mutex_unlock(&player->ctrl_lock);
603 spin_lock_irqsave(&player->irq_lock, flags);
604 if (player->substream && player->substream->runtime)
605 uni_player_set_channel_status(player,
606 player->substream->runtime);
608 uni_player_set_channel_status(player, NULL);
610 spin_unlock_irqrestore(&player->irq_lock, flags);
614 static struct snd_kcontrol_new uni_player_iec958_ctl = {
615 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
616 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
617 .info = uni_player_ctl_iec958_info,
618 .get = uni_player_ctl_iec958_get,
619 .put = uni_player_ctl_iec958_put,
623 * uniperif rate adjustement control
625 static int snd_sti_clk_adjustment_info(struct snd_kcontrol *kcontrol,
626 struct snd_ctl_elem_info *uinfo)
628 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
630 uinfo->value.integer.min = UNIPERIF_PLAYER_CLK_ADJ_MIN;
631 uinfo->value.integer.max = UNIPERIF_PLAYER_CLK_ADJ_MAX;
632 uinfo->value.integer.step = 1;
637 static int snd_sti_clk_adjustment_get(struct snd_kcontrol *kcontrol,
638 struct snd_ctl_elem_value *ucontrol)
640 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
641 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
642 struct uniperif *player = priv->dai_data.uni;
644 mutex_lock(&player->ctrl_lock);
645 ucontrol->value.integer.value[0] = player->clk_adj;
646 mutex_unlock(&player->ctrl_lock);
651 static int snd_sti_clk_adjustment_put(struct snd_kcontrol *kcontrol,
652 struct snd_ctl_elem_value *ucontrol)
654 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
655 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
656 struct uniperif *player = priv->dai_data.uni;
659 if ((ucontrol->value.integer.value[0] < UNIPERIF_PLAYER_CLK_ADJ_MIN) ||
660 (ucontrol->value.integer.value[0] > UNIPERIF_PLAYER_CLK_ADJ_MAX))
663 mutex_lock(&player->ctrl_lock);
664 player->clk_adj = ucontrol->value.integer.value[0];
667 ret = uni_player_clk_set_rate(player, player->mclk);
668 mutex_unlock(&player->ctrl_lock);
673 static struct snd_kcontrol_new uni_player_clk_adj_ctl = {
674 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
675 .name = "PCM Playback Oversampling Freq. Adjustment",
676 .info = snd_sti_clk_adjustment_info,
677 .get = snd_sti_clk_adjustment_get,
678 .put = snd_sti_clk_adjustment_put,
681 static struct snd_kcontrol_new *snd_sti_pcm_ctl[] = {
682 &uni_player_clk_adj_ctl,
685 static struct snd_kcontrol_new *snd_sti_iec_ctl[] = {
686 &uni_player_iec958_ctl,
687 &uni_player_clk_adj_ctl,
690 static int uni_player_startup(struct snd_pcm_substream *substream,
691 struct snd_soc_dai *dai)
693 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
694 struct uniperif *player = priv->dai_data.uni;
698 spin_lock_irqsave(&player->irq_lock, flags);
699 player->substream = substream;
700 spin_unlock_irqrestore(&player->irq_lock, flags);
704 if (!UNIPERIF_TYPE_IS_TDM(player))
707 /* refine hw constraint in tdm mode */
708 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
709 SNDRV_PCM_HW_PARAM_CHANNELS,
710 sti_uniperiph_fix_tdm_chan,
711 player, SNDRV_PCM_HW_PARAM_CHANNELS,
716 return snd_pcm_hw_rule_add(substream->runtime, 0,
717 SNDRV_PCM_HW_PARAM_FORMAT,
718 sti_uniperiph_fix_tdm_format,
719 player, SNDRV_PCM_HW_PARAM_FORMAT,
723 static int uni_player_set_sysclk(struct snd_soc_dai *dai, int clk_id,
724 unsigned int freq, int dir)
726 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
727 struct uniperif *player = priv->dai_data.uni;
730 if (UNIPERIF_TYPE_IS_TDM(player) || (dir == SND_SOC_CLOCK_IN))
736 mutex_lock(&player->ctrl_lock);
737 ret = uni_player_clk_set_rate(player, freq);
740 mutex_unlock(&player->ctrl_lock);
745 static int uni_player_prepare(struct snd_pcm_substream *substream,
746 struct snd_soc_dai *dai)
748 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
749 struct uniperif *player = priv->dai_data.uni;
750 struct snd_pcm_runtime *runtime = substream->runtime;
751 int transfer_size, trigger_limit;
754 /* The player should be stopped */
755 if (player->state != UNIPERIF_STATE_STOPPED) {
756 dev_err(player->dev, "%s: invalid player state %d\n", __func__,
761 /* Calculate transfer size (in fifo cells and bytes) for frame count */
762 if (player->type == SND_ST_UNIPERIF_TYPE_TDM) {
763 /* transfer size = user frame size (in 32 bits FIFO cell) */
765 sti_uniperiph_get_user_frame_size(runtime) / 4;
767 transfer_size = runtime->channels * UNIPERIF_FIFO_FRAMES;
770 /* Calculate number of empty cells available before asserting DREQ */
771 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
772 trigger_limit = UNIPERIF_FIFO_SIZE - transfer_size;
775 * Since SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
776 * FDMA_TRIGGER_LIMIT also controls when the state switches
777 * from OFF or STANDBY to AUDIO DATA.
779 trigger_limit = transfer_size;
782 /* Trigger limit must be an even number */
783 if ((!trigger_limit % 2) || (trigger_limit != 1 && transfer_size % 2) ||
784 (trigger_limit > UNIPERIF_CONFIG_DMA_TRIG_LIMIT_MASK(player))) {
785 dev_err(player->dev, "invalid trigger limit %d\n",
790 SET_UNIPERIF_CONFIG_DMA_TRIG_LIMIT(player, trigger_limit);
792 /* Uniperipheral setup depends on player type */
793 switch (player->type) {
794 case SND_ST_UNIPERIF_TYPE_HDMI:
795 ret = uni_player_prepare_iec958(player, runtime);
797 case SND_ST_UNIPERIF_TYPE_PCM:
798 ret = uni_player_prepare_pcm(player, runtime);
800 case SND_ST_UNIPERIF_TYPE_SPDIF:
801 ret = uni_player_prepare_iec958(player, runtime);
803 case SND_ST_UNIPERIF_TYPE_TDM:
804 ret = uni_player_prepare_tdm(player, runtime);
807 dev_err(player->dev, "invalid player type\n");
814 switch (player->daifmt & SND_SOC_DAIFMT_INV_MASK) {
815 case SND_SOC_DAIFMT_NB_NF:
816 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
817 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
819 case SND_SOC_DAIFMT_NB_IF:
820 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
821 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
823 case SND_SOC_DAIFMT_IB_NF:
824 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
825 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
827 case SND_SOC_DAIFMT_IB_IF:
828 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
829 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
833 switch (player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) {
834 case SND_SOC_DAIFMT_I2S:
835 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
836 SET_UNIPERIF_I2S_FMT_PADDING_I2S_MODE(player);
838 case SND_SOC_DAIFMT_LEFT_J:
839 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
840 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
842 case SND_SOC_DAIFMT_RIGHT_J:
843 SET_UNIPERIF_I2S_FMT_ALIGN_RIGHT(player);
844 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
847 dev_err(player->dev, "format not supported\n");
851 SET_UNIPERIF_I2S_FMT_NO_OF_SAMPLES_TO_READ(player, 0);
854 return sti_uniperiph_reset(player);
857 static int uni_player_start(struct uniperif *player)
861 /* The player should be stopped */
862 if (player->state != UNIPERIF_STATE_STOPPED) {
863 dev_err(player->dev, "%s: invalid player state\n", __func__);
867 ret = clk_prepare_enable(player->clk);
869 dev_err(player->dev, "%s: Failed to enable clock\n", __func__);
873 /* Clear any pending interrupts */
874 SET_UNIPERIF_ITS_BCLR(player, GET_UNIPERIF_ITS(player));
876 /* Set the interrupt mask */
877 SET_UNIPERIF_ITM_BSET_DMA_ERROR(player);
878 SET_UNIPERIF_ITM_BSET_FIFO_ERROR(player);
880 /* Enable underflow recovery interrupts */
881 if (player->underflow_enabled) {
882 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_DONE(player);
883 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_FAILED(player);
886 ret = sti_uniperiph_reset(player);
888 clk_disable_unprepare(player->clk);
893 * Does not use IEC61937 features of the uniperipheral hardware.
894 * Instead it performs IEC61937 in software and inserts it directly
895 * into the audio data stream. As such, when encoded mode is selected,
896 * linear pcm mode is still used, but with the differences of the
897 * channel status bits set for encoded mode and the validity bits set.
899 SET_UNIPERIF_CTRL_OPERATION_PCM_DATA(player);
902 * If iec958 formatting is required for hdmi or spdif, then it must be
903 * enabled after the operation mode is set. If set prior to this, it
904 * will not take affect and hang the player.
906 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
907 if (UNIPERIF_TYPE_IS_IEC958(player))
908 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player);
910 /* Force channel status update (no update if clk disable) */
911 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
912 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player);
914 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player);
916 /* Update state to started */
917 player->state = UNIPERIF_STATE_STARTED;
922 static int uni_player_stop(struct uniperif *player)
926 /* The player should not be in stopped state */
927 if (player->state == UNIPERIF_STATE_STOPPED) {
928 dev_err(player->dev, "%s: invalid player state\n", __func__);
932 /* Turn the player off */
933 SET_UNIPERIF_CTRL_OPERATION_OFF(player);
935 ret = sti_uniperiph_reset(player);
939 /* Disable interrupts */
940 SET_UNIPERIF_ITM_BCLR(player, GET_UNIPERIF_ITM(player));
943 clk_disable_unprepare(player->clk);
945 /* Update state to stopped and return */
946 player->state = UNIPERIF_STATE_STOPPED;
951 int uni_player_resume(struct uniperif *player)
955 /* Select the frequency synthesizer clock */
956 if (player->clk_sel) {
957 ret = regmap_field_write(player->clk_sel, 1);
960 "%s: Failed to select freq synth clock\n",
966 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
967 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
968 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
969 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
973 EXPORT_SYMBOL_GPL(uni_player_resume);
975 static int uni_player_trigger(struct snd_pcm_substream *substream,
976 int cmd, struct snd_soc_dai *dai)
978 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
979 struct uniperif *player = priv->dai_data.uni;
982 case SNDRV_PCM_TRIGGER_START:
983 return uni_player_start(player);
984 case SNDRV_PCM_TRIGGER_STOP:
985 return uni_player_stop(player);
986 case SNDRV_PCM_TRIGGER_RESUME:
987 return uni_player_resume(player);
993 static void uni_player_shutdown(struct snd_pcm_substream *substream,
994 struct snd_soc_dai *dai)
996 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
997 struct uniperif *player = priv->dai_data.uni;
1000 spin_lock_irqsave(&player->irq_lock, flags);
1001 if (player->state != UNIPERIF_STATE_STOPPED)
1002 /* Stop the player */
1003 uni_player_stop(player);
1005 player->substream = NULL;
1006 spin_unlock_irqrestore(&player->irq_lock, flags);
1009 static int uni_player_parse_dt_audio_glue(struct platform_device *pdev,
1010 struct uniperif *player)
1012 struct device_node *node = pdev->dev.of_node;
1013 struct regmap *regmap;
1014 struct reg_field regfield[2] = {
1016 REG_FIELD(SYS_CFG_AUDIO_GLUE,
1019 /* PCMP_VALID_SEL */
1020 REG_FIELD(SYS_CFG_AUDIO_GLUE, 0, 1)
1023 regmap = syscon_regmap_lookup_by_phandle(node, "st,syscfg");
1025 if (IS_ERR(regmap)) {
1026 dev_err(&pdev->dev, "sti-audio-clk-glue syscf not found\n");
1027 return PTR_ERR(regmap);
1030 player->clk_sel = regmap_field_alloc(regmap, regfield[0]);
1031 player->valid_sel = regmap_field_alloc(regmap, regfield[1]);
1036 static const struct snd_soc_dai_ops uni_player_dai_ops = {
1037 .startup = uni_player_startup,
1038 .shutdown = uni_player_shutdown,
1039 .prepare = uni_player_prepare,
1040 .trigger = uni_player_trigger,
1041 .hw_params = sti_uniperiph_dai_hw_params,
1042 .set_fmt = sti_uniperiph_dai_set_fmt,
1043 .set_sysclk = uni_player_set_sysclk,
1044 .set_tdm_slot = sti_uniperiph_set_tdm_slot
1047 int uni_player_init(struct platform_device *pdev,
1048 struct uniperif *player)
1052 player->dev = &pdev->dev;
1053 player->state = UNIPERIF_STATE_STOPPED;
1054 player->dai_ops = &uni_player_dai_ops;
1056 /* Get PCM_CLK_SEL & PCMP_VALID_SEL from audio-glue-ctrl SoC reg */
1057 ret = uni_player_parse_dt_audio_glue(pdev, player);
1060 dev_err(player->dev, "Failed to parse DeviceTree\n");
1064 /* Underflow recovery is only supported on later ip revisions */
1065 if (player->ver >= SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
1066 player->underflow_enabled = 1;
1068 if (UNIPERIF_TYPE_IS_TDM(player))
1069 player->hw = &uni_tdm_hw;
1071 player->hw = &uni_player_pcm_hw;
1073 /* Get uniperif resource */
1074 player->clk = of_clk_get(pdev->dev.of_node, 0);
1075 if (IS_ERR(player->clk)) {
1076 dev_err(player->dev, "Failed to get clock\n");
1077 return PTR_ERR(player->clk);
1080 /* Select the frequency synthesizer clock */
1081 if (player->clk_sel) {
1082 ret = regmap_field_write(player->clk_sel, 1);
1084 dev_err(player->dev,
1085 "%s: Failed to select freq synth clock\n",
1091 /* connect to I2S/TDM TX bus */
1092 if (player->valid_sel &&
1093 (player->id == UNIPERIF_PLAYER_I2S_OUT)) {
1094 ret = regmap_field_write(player->valid_sel, player->id);
1096 dev_err(player->dev,
1097 "%s: unable to connect to tdm bus\n", __func__);
1102 ret = devm_request_irq(&pdev->dev, player->irq,
1103 uni_player_irq_handler, IRQF_SHARED,
1104 dev_name(&pdev->dev), player);
1106 dev_err(player->dev, "unable to request IRQ %d\n", player->irq);
1110 mutex_init(&player->ctrl_lock);
1111 spin_lock_init(&player->irq_lock);
1113 /* Ensure that disabled by default */
1114 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
1115 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
1116 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
1117 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
1119 if (UNIPERIF_TYPE_IS_IEC958(player)) {
1120 /* Set default iec958 status bits */
1122 /* Consumer, PCM, copyright, 2ch, mode 0 */
1123 player->stream_settings.iec958.status[0] = 0x00;
1124 /* Broadcast reception category */
1125 player->stream_settings.iec958.status[1] =
1126 IEC958_AES1_CON_GENERAL;
1127 /* Do not take into account source or channel number */
1128 player->stream_settings.iec958.status[2] =
1129 IEC958_AES2_CON_SOURCE_UNSPEC;
1130 /* Sampling frequency not indicated */
1131 player->stream_settings.iec958.status[3] =
1132 IEC958_AES3_CON_FS_NOTID;
1133 /* Max sample word 24-bit, sample word length not indicated */
1134 player->stream_settings.iec958.status[4] =
1135 IEC958_AES4_CON_MAX_WORDLEN_24 |
1136 IEC958_AES4_CON_WORDLEN_24_20;
1138 player->num_ctrls = ARRAY_SIZE(snd_sti_iec_ctl);
1139 player->snd_ctrls = snd_sti_iec_ctl[0];
1141 player->num_ctrls = ARRAY_SIZE(snd_sti_pcm_ctl);
1142 player->snd_ctrls = snd_sti_pcm_ctl[0];
1147 EXPORT_SYMBOL_GPL(uni_player_init);