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
9 #include <linux/delay.h>
11 #include <linux/mfd/syscon.h>
13 #include <sound/asoundef.h>
14 #include <sound/soc.h>
19 * Some hardware-related definitions
22 /* sys config registers definitions */
23 #define SYS_CFG_AUDIO_GLUE 0xA4
26 * Driver specific types.
29 #define UNIPERIF_PLAYER_CLK_ADJ_MIN -999999
30 #define UNIPERIF_PLAYER_CLK_ADJ_MAX 1000000
31 #define UNIPERIF_PLAYER_I2S_OUT 1 /* player id connected to I2S/TDM TX bus */
34 * Note: snd_pcm_hardware is linked to DMA controller but is declared here to
35 * integrate DAI_CPU capability in term of rate and supported channels
37 static const struct snd_pcm_hardware uni_player_pcm_hw = {
38 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
39 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP |
40 SNDRV_PCM_INFO_MMAP_VALID,
41 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE,
43 .rates = SNDRV_PCM_RATE_CONTINUOUS,
53 .period_bytes_min = 128,
54 .period_bytes_max = 64 * PAGE_SIZE,
55 .buffer_bytes_max = 256 * PAGE_SIZE
58 static inline int reset_player(struct uniperif *player)
62 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
63 while (GET_UNIPERIF_SOFT_RST_SOFT_RST(player) && count) {
70 dev_err(player->dev, "Failed to reset uniperif");
78 * uni_player_irq_handler
79 * In case of error audio stream is stopped; stop action is protected via PCM
80 * stream lock to avoid race condition with trigger callback.
82 static irqreturn_t uni_player_irq_handler(int irq, void *dev_id)
84 irqreturn_t ret = IRQ_NONE;
85 struct uniperif *player = dev_id;
89 if (player->state == UNIPERIF_STATE_STOPPED) {
90 /* Unexpected IRQ: do nothing */
94 /* Get interrupt status & clear them immediately */
95 status = GET_UNIPERIF_ITS(player);
96 SET_UNIPERIF_ITS_BCLR(player, status);
98 /* Check for fifo error (underrun) */
99 if (unlikely(status & UNIPERIF_ITS_FIFO_ERROR_MASK(player))) {
100 dev_err(player->dev, "FIFO underflow error detected");
102 /* Interrupt is just for information when underflow recovery */
103 if (player->info->underflow_enabled) {
104 /* Update state to underflow */
105 player->state = UNIPERIF_STATE_UNDERFLOW;
108 /* Disable interrupt so doesn't continually fire */
109 SET_UNIPERIF_ITM_BCLR_FIFO_ERROR(player);
111 /* Stop the player */
112 snd_pcm_stream_lock(player->substream);
113 snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN);
114 snd_pcm_stream_unlock(player->substream);
120 /* Check for dma error (overrun) */
121 if (unlikely(status & UNIPERIF_ITS_DMA_ERROR_MASK(player))) {
122 dev_err(player->dev, "DMA error detected");
124 /* Disable interrupt so doesn't continually fire */
125 SET_UNIPERIF_ITM_BCLR_DMA_ERROR(player);
127 /* Stop the player */
128 snd_pcm_stream_lock(player->substream);
129 snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN);
130 snd_pcm_stream_unlock(player->substream);
135 /* Check for underflow recovery done */
136 if (unlikely(status & UNIPERIF_ITM_UNDERFLOW_REC_DONE_MASK(player))) {
137 if (!player->info->underflow_enabled) {
138 dev_err(player->dev, "unexpected Underflow recovering");
141 /* Read the underflow recovery duration */
142 tmp = GET_UNIPERIF_STATUS_1_UNDERFLOW_DURATION(player);
144 /* Clear the underflow recovery duration */
145 SET_UNIPERIF_BIT_CONTROL_CLR_UNDERFLOW_DURATION(player);
147 /* Update state to started */
148 player->state = UNIPERIF_STATE_STARTED;
153 /* Check if underflow recovery failed */
154 if (unlikely(status &
155 UNIPERIF_ITM_UNDERFLOW_REC_FAILED_MASK(player))) {
156 dev_err(player->dev, "Underflow recovery failed");
158 /* Stop the player */
159 snd_pcm_stream_lock(player->substream);
160 snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN);
161 snd_pcm_stream_unlock(player->substream);
169 static int uni_player_clk_set_rate(struct uniperif *player, unsigned long rate)
171 int rate_adjusted, rate_achieved, delta, ret;
172 int adjustment = player->clk_adj;
176 * F = f + --------- * f = f + d
185 * a - adjustment in ppm (parts per milion)
186 * F - rate to be set in synthesizer
187 * d - delta (difference) between f and F
189 if (adjustment < 0) {
190 /* div64_64 operates on unsigned values... */
192 adjustment = -adjustment;
196 /* 500000 ppm is 0.5, which is used to round up values */
197 delta *= (int)div64_u64((uint64_t)rate *
198 (uint64_t)adjustment + 500000, 1000000);
199 rate_adjusted = rate + delta;
201 /* Adjusted rate should never be == 0 */
205 ret = clk_set_rate(player->clk, rate_adjusted);
209 rate_achieved = clk_get_rate(player->clk);
211 /* If value is 0 means that clock or parent not valid */
215 * Using ALSA's adjustment control, we can modify the rate to be up
216 * to twice as much as requested, but no more
218 delta = rate_achieved - rate;
220 /* div64_64 operates on unsigned values... */
226 /* Frequency/2 is added to round up result */
227 adjustment *= (int)div64_u64((uint64_t)delta * 1000000 + rate / 2,
229 player->clk_adj = adjustment;
233 static void uni_player_set_channel_status(struct uniperif *player,
234 struct snd_pcm_runtime *runtime)
240 * Some AVRs and TVs require the channel status to contain a correct
241 * sampling frequency. If no sample rate is already specified, then
244 mutex_lock(&player->ctrl_lock);
246 switch (runtime->rate) {
248 player->stream_settings.iec958.status[3] =
249 IEC958_AES3_CON_FS_22050;
252 player->stream_settings.iec958.status[3] =
253 IEC958_AES3_CON_FS_44100;
256 player->stream_settings.iec958.status[3] =
257 IEC958_AES3_CON_FS_88200;
260 player->stream_settings.iec958.status[3] =
261 IEC958_AES3_CON_FS_176400;
264 player->stream_settings.iec958.status[3] =
265 IEC958_AES3_CON_FS_24000;
268 player->stream_settings.iec958.status[3] =
269 IEC958_AES3_CON_FS_48000;
272 player->stream_settings.iec958.status[3] =
273 IEC958_AES3_CON_FS_96000;
276 player->stream_settings.iec958.status[3] =
277 IEC958_AES3_CON_FS_192000;
280 player->stream_settings.iec958.status[3] =
281 IEC958_AES3_CON_FS_32000;
284 /* Mark as sampling frequency not indicated */
285 player->stream_settings.iec958.status[3] =
286 IEC958_AES3_CON_FS_NOTID;
292 * Use audio mode status to select PCM or encoded mode
294 if (player->stream_settings.iec958.status[0] & IEC958_AES0_NONAUDIO)
295 player->stream_settings.encoding_mode =
296 UNIPERIF_IEC958_ENCODING_MODE_ENCODED;
298 player->stream_settings.encoding_mode =
299 UNIPERIF_IEC958_ENCODING_MODE_PCM;
301 if (player->stream_settings.encoding_mode ==
302 UNIPERIF_IEC958_ENCODING_MODE_PCM)
303 /* Clear user validity bits */
304 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0);
306 /* Set user validity bits */
307 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 1);
309 /* Program the new channel status */
310 for (n = 0; n < 6; ++n) {
312 player->stream_settings.iec958.status[0 + (n * 4)] & 0xf;
314 player->stream_settings.iec958.status[1 + (n * 4)] << 8;
316 player->stream_settings.iec958.status[2 + (n * 4)] << 16;
318 player->stream_settings.iec958.status[3 + (n * 4)] << 24;
319 SET_UNIPERIF_CHANNEL_STA_REGN(player, n, status);
321 mutex_unlock(&player->ctrl_lock);
323 /* Update the channel status */
324 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
325 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player);
327 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player);
330 static int uni_player_prepare_iec958(struct uniperif *player,
331 struct snd_pcm_runtime *runtime)
335 clk_div = player->mclk / runtime->rate;
337 /* Oversampling must be multiple of 128 as iec958 frame is 32-bits */
338 if ((clk_div % 128) || (clk_div <= 0)) {
339 dev_err(player->dev, "%s: invalid clk_div %d",
344 switch (runtime->format) {
345 case SNDRV_PCM_FORMAT_S16_LE:
346 /* 16/16 memory format */
347 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player);
348 /* 16-bits per sub-frame */
349 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
350 /* Set 16-bit sample precision */
351 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player);
353 case SNDRV_PCM_FORMAT_S32_LE:
354 /* 16/0 memory format */
355 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
356 /* 32-bits per sub-frame */
357 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
358 /* Set 24-bit sample precision */
359 SET_UNIPERIF_I2S_FMT_DATA_SIZE_24(player);
362 dev_err(player->dev, "format not supported");
366 /* Set parity to be calculated by the hardware */
367 SET_UNIPERIF_CONFIG_PARITY_CNTR_BY_HW(player);
369 /* Set channel status bits to be inserted by the hardware */
370 SET_UNIPERIF_CONFIG_CHANNEL_STA_CNTR_BY_HW(player);
372 /* Set user data bits to be inserted by the hardware */
373 SET_UNIPERIF_CONFIG_USER_DAT_CNTR_BY_HW(player);
375 /* Set validity bits to be inserted by the hardware */
376 SET_UNIPERIF_CONFIG_VALIDITY_DAT_CNTR_BY_HW(player);
378 /* Set full software control to disabled */
379 SET_UNIPERIF_CONFIG_SPDIF_SW_CTRL_DISABLE(player);
381 SET_UNIPERIF_CTRL_ZERO_STUFF_HW(player);
383 /* Update the channel status */
384 uni_player_set_channel_status(player, runtime);
386 /* Clear the user validity user bits */
387 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0);
389 /* Disable one-bit audio mode */
390 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player);
392 /* Enable consecutive frames repetition of Z preamble (not for HBRA) */
393 SET_UNIPERIF_CONFIG_REPEAT_CHL_STS_ENABLE(player);
395 /* Change to SUF0_SUBF1 and left/right channels swap! */
396 SET_UNIPERIF_CONFIG_SUBFRAME_SEL_SUBF1_SUBF0(player);
398 /* Set data output as MSB first */
399 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
401 if (player->stream_settings.encoding_mode ==
402 UNIPERIF_IEC958_ENCODING_MODE_ENCODED)
403 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_ON(player);
405 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_OFF(player);
407 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2);
409 /* Set rounding to off */
410 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
412 /* Set clock divisor */
413 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / 128);
415 /* Set the spdif latency to not wait before starting player */
416 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
419 * Ensure iec958 formatting is off. It will be enabled in function
420 * uni_player_start() at the same time as the operation
421 * mode is set to work around a silicon issue.
423 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
424 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player);
426 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player);
431 static int uni_player_prepare_pcm(struct uniperif *player,
432 struct snd_pcm_runtime *runtime)
434 int output_frame_size, slot_width, clk_div;
436 /* Force slot width to 32 in I2S mode (HW constraint) */
437 if ((player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
441 slot_width = snd_pcm_format_width(runtime->format);
443 output_frame_size = slot_width * runtime->channels;
445 clk_div = player->mclk / runtime->rate;
447 * For 32 bits subframe clk_div must be a multiple of 128,
448 * for 16 bits must be a multiple of 64
450 if ((slot_width == 32) && (clk_div % 128)) {
451 dev_err(player->dev, "%s: invalid clk_div", __func__);
455 if ((slot_width == 16) && (clk_div % 64)) {
456 dev_err(player->dev, "%s: invalid clk_div", __func__);
461 * Number of bits per subframe (which is one channel sample)
462 * on output - Transfer 16 or 32 bits from FIFO
464 switch (slot_width) {
466 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
467 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player);
470 SET_UNIPERIF_I2S_FMT_NBIT_16(player);
471 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player);
474 dev_err(player->dev, "subframe format not supported");
478 /* Configure data memory format */
479 switch (runtime->format) {
480 case SNDRV_PCM_FORMAT_S16_LE:
481 /* One data word contains two samples */
482 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player);
485 case SNDRV_PCM_FORMAT_S32_LE:
487 * Actually "16 bits/0 bits" means "32/28/24/20/18/16 bits
488 * on the left than zeros (if less than 32 bytes)"... ;-)
490 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
494 dev_err(player->dev, "format not supported");
498 /* Set rounding to off */
499 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
501 /* Set clock divisor */
502 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / (2 * output_frame_size));
504 /* Number of channelsmust be even*/
505 if ((runtime->channels % 2) || (runtime->channels < 2) ||
506 (runtime->channels > 10)) {
507 dev_err(player->dev, "%s: invalid nb of channels", __func__);
511 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2);
513 /* Set 1-bit audio format to disabled */
514 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player);
516 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
518 /* No iec958 formatting as outputting to DAC */
519 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player);
524 static int uni_player_prepare_tdm(struct uniperif *player,
525 struct snd_pcm_runtime *runtime)
527 int tdm_frame_size; /* unip tdm frame size in bytes */
528 int user_frame_size; /* user tdm frame size in bytes */
529 /* default unip TDM_WORD_POS_X_Y */
530 unsigned int word_pos[4] = {
531 0x04060002, 0x0C0E080A, 0x14161012, 0x1C1E181A};
535 sti_uniperiph_get_unip_tdm_frame_size(player);
537 sti_uniperiph_get_user_frame_size(runtime);
539 /* fix 16/0 format */
540 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
541 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player);
543 /* number of words inserted on the TDM line */
544 SET_UNIPERIF_I2S_FMT_NUM_CH(player, user_frame_size / 4 / 2);
546 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
547 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
549 /* Enable the tdm functionality */
550 SET_UNIPERIF_TDM_ENABLE_TDM_ENABLE(player);
552 /* number of 8 bits timeslots avail in unip tdm frame */
553 SET_UNIPERIF_TDM_FS_REF_DIV_NUM_TIMESLOT(player, tdm_frame_size);
555 /* set the timeslot allocation for words in FIFO */
556 sti_uniperiph_get_tdm_word_pos(player, word_pos);
557 SET_UNIPERIF_TDM_WORD_POS(player, 1_2, word_pos[WORD_1_2]);
558 SET_UNIPERIF_TDM_WORD_POS(player, 3_4, word_pos[WORD_3_4]);
559 SET_UNIPERIF_TDM_WORD_POS(player, 5_6, word_pos[WORD_5_6]);
560 SET_UNIPERIF_TDM_WORD_POS(player, 7_8, word_pos[WORD_7_8]);
562 /* set unip clk rate (not done vai set_sysclk ops) */
563 freq = runtime->rate * tdm_frame_size * 8;
564 mutex_lock(&player->ctrl_lock);
565 ret = uni_player_clk_set_rate(player, freq);
568 mutex_unlock(&player->ctrl_lock);
574 * ALSA uniperipheral iec958 controls
576 static int uni_player_ctl_iec958_info(struct snd_kcontrol *kcontrol,
577 struct snd_ctl_elem_info *uinfo)
579 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
585 static int uni_player_ctl_iec958_get(struct snd_kcontrol *kcontrol,
586 struct snd_ctl_elem_value *ucontrol)
588 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
589 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
590 struct uniperif *player = priv->dai_data.uni;
591 struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958;
593 mutex_lock(&player->ctrl_lock);
594 ucontrol->value.iec958.status[0] = iec958->status[0];
595 ucontrol->value.iec958.status[1] = iec958->status[1];
596 ucontrol->value.iec958.status[2] = iec958->status[2];
597 ucontrol->value.iec958.status[3] = iec958->status[3];
598 mutex_unlock(&player->ctrl_lock);
602 static int uni_player_ctl_iec958_put(struct snd_kcontrol *kcontrol,
603 struct snd_ctl_elem_value *ucontrol)
605 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
606 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
607 struct uniperif *player = priv->dai_data.uni;
608 struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958;
610 mutex_lock(&player->ctrl_lock);
611 iec958->status[0] = ucontrol->value.iec958.status[0];
612 iec958->status[1] = ucontrol->value.iec958.status[1];
613 iec958->status[2] = ucontrol->value.iec958.status[2];
614 iec958->status[3] = ucontrol->value.iec958.status[3];
615 mutex_unlock(&player->ctrl_lock);
617 uni_player_set_channel_status(player, NULL);
622 static struct snd_kcontrol_new uni_player_iec958_ctl = {
623 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
624 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
625 .info = uni_player_ctl_iec958_info,
626 .get = uni_player_ctl_iec958_get,
627 .put = uni_player_ctl_iec958_put,
631 * uniperif rate adjustement control
633 static int snd_sti_clk_adjustment_info(struct snd_kcontrol *kcontrol,
634 struct snd_ctl_elem_info *uinfo)
636 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
638 uinfo->value.integer.min = UNIPERIF_PLAYER_CLK_ADJ_MIN;
639 uinfo->value.integer.max = UNIPERIF_PLAYER_CLK_ADJ_MAX;
640 uinfo->value.integer.step = 1;
645 static int snd_sti_clk_adjustment_get(struct snd_kcontrol *kcontrol,
646 struct snd_ctl_elem_value *ucontrol)
648 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
649 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
650 struct uniperif *player = priv->dai_data.uni;
652 mutex_lock(&player->ctrl_lock);
653 ucontrol->value.integer.value[0] = player->clk_adj;
654 mutex_unlock(&player->ctrl_lock);
659 static int snd_sti_clk_adjustment_put(struct snd_kcontrol *kcontrol,
660 struct snd_ctl_elem_value *ucontrol)
662 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
663 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
664 struct uniperif *player = priv->dai_data.uni;
667 if ((ucontrol->value.integer.value[0] < UNIPERIF_PLAYER_CLK_ADJ_MIN) ||
668 (ucontrol->value.integer.value[0] > UNIPERIF_PLAYER_CLK_ADJ_MAX))
671 mutex_lock(&player->ctrl_lock);
672 player->clk_adj = ucontrol->value.integer.value[0];
675 ret = uni_player_clk_set_rate(player, player->mclk);
676 mutex_unlock(&player->ctrl_lock);
681 static struct snd_kcontrol_new uni_player_clk_adj_ctl = {
682 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
683 .name = "PCM Playback Oversampling Freq. Adjustment",
684 .info = snd_sti_clk_adjustment_info,
685 .get = snd_sti_clk_adjustment_get,
686 .put = snd_sti_clk_adjustment_put,
689 static struct snd_kcontrol_new *snd_sti_pcm_ctl[] = {
690 &uni_player_clk_adj_ctl,
693 static struct snd_kcontrol_new *snd_sti_iec_ctl[] = {
694 &uni_player_iec958_ctl,
695 &uni_player_clk_adj_ctl,
698 static int uni_player_startup(struct snd_pcm_substream *substream,
699 struct snd_soc_dai *dai)
701 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
702 struct uniperif *player = priv->dai_data.uni;
705 player->substream = substream;
709 if (!UNIPERIF_TYPE_IS_TDM(player))
712 /* refine hw constraint in tdm mode */
713 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
714 SNDRV_PCM_HW_PARAM_CHANNELS,
715 sti_uniperiph_fix_tdm_chan,
716 player, SNDRV_PCM_HW_PARAM_CHANNELS,
721 return snd_pcm_hw_rule_add(substream->runtime, 0,
722 SNDRV_PCM_HW_PARAM_FORMAT,
723 sti_uniperiph_fix_tdm_format,
724 player, SNDRV_PCM_HW_PARAM_FORMAT,
728 static int uni_player_set_sysclk(struct snd_soc_dai *dai, int clk_id,
729 unsigned int freq, int dir)
731 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
732 struct uniperif *player = priv->dai_data.uni;
735 if (UNIPERIF_TYPE_IS_TDM(player) || (dir == SND_SOC_CLOCK_IN))
741 mutex_lock(&player->ctrl_lock);
742 ret = uni_player_clk_set_rate(player, freq);
745 mutex_unlock(&player->ctrl_lock);
750 static int uni_player_prepare(struct snd_pcm_substream *substream,
751 struct snd_soc_dai *dai)
753 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
754 struct uniperif *player = priv->dai_data.uni;
755 struct snd_pcm_runtime *runtime = substream->runtime;
756 int transfer_size, trigger_limit;
759 /* The player should be stopped */
760 if (player->state != UNIPERIF_STATE_STOPPED) {
761 dev_err(player->dev, "%s: invalid player state %d", __func__,
766 /* Calculate transfer size (in fifo cells and bytes) for frame count */
767 if (player->info->type == SND_ST_UNIPERIF_TYPE_TDM) {
768 /* transfer size = user frame size (in 32 bits FIFO cell) */
770 sti_uniperiph_get_user_frame_size(runtime) / 4;
772 transfer_size = runtime->channels * UNIPERIF_FIFO_FRAMES;
775 /* Calculate number of empty cells available before asserting DREQ */
776 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
777 trigger_limit = UNIPERIF_FIFO_SIZE - transfer_size;
780 * Since SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
781 * FDMA_TRIGGER_LIMIT also controls when the state switches
782 * from OFF or STANDBY to AUDIO DATA.
784 trigger_limit = transfer_size;
787 /* Trigger limit must be an even number */
788 if ((!trigger_limit % 2) || (trigger_limit != 1 && transfer_size % 2) ||
789 (trigger_limit > UNIPERIF_CONFIG_DMA_TRIG_LIMIT_MASK(player))) {
790 dev_err(player->dev, "invalid trigger limit %d", trigger_limit);
794 SET_UNIPERIF_CONFIG_DMA_TRIG_LIMIT(player, trigger_limit);
796 /* Uniperipheral setup depends on player type */
797 switch (player->info->type) {
798 case SND_ST_UNIPERIF_TYPE_HDMI:
799 ret = uni_player_prepare_iec958(player, runtime);
801 case SND_ST_UNIPERIF_TYPE_PCM:
802 ret = uni_player_prepare_pcm(player, runtime);
804 case SND_ST_UNIPERIF_TYPE_SPDIF:
805 ret = uni_player_prepare_iec958(player, runtime);
807 case SND_ST_UNIPERIF_TYPE_TDM:
808 ret = uni_player_prepare_tdm(player, runtime);
811 dev_err(player->dev, "invalid player type");
818 switch (player->daifmt & SND_SOC_DAIFMT_INV_MASK) {
819 case SND_SOC_DAIFMT_NB_NF:
820 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
821 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
823 case SND_SOC_DAIFMT_NB_IF:
824 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
825 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
827 case SND_SOC_DAIFMT_IB_NF:
828 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
829 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
831 case SND_SOC_DAIFMT_IB_IF:
832 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
833 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
837 switch (player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) {
838 case SND_SOC_DAIFMT_I2S:
839 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
840 SET_UNIPERIF_I2S_FMT_PADDING_I2S_MODE(player);
842 case SND_SOC_DAIFMT_LEFT_J:
843 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
844 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
846 case SND_SOC_DAIFMT_RIGHT_J:
847 SET_UNIPERIF_I2S_FMT_ALIGN_RIGHT(player);
848 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
851 dev_err(player->dev, "format not supported");
855 SET_UNIPERIF_I2S_FMT_NO_OF_SAMPLES_TO_READ(player, 0);
857 /* Reset uniperipheral player */
858 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
860 return reset_player(player);
863 static int uni_player_start(struct uniperif *player)
867 /* The player should be stopped */
868 if (player->state != UNIPERIF_STATE_STOPPED) {
869 dev_err(player->dev, "%s: invalid player state", __func__);
873 ret = clk_prepare_enable(player->clk);
875 dev_err(player->dev, "%s: Failed to enable clock", __func__);
879 /* Clear any pending interrupts */
880 SET_UNIPERIF_ITS_BCLR(player, GET_UNIPERIF_ITS(player));
882 /* Set the interrupt mask */
883 SET_UNIPERIF_ITM_BSET_DMA_ERROR(player);
884 SET_UNIPERIF_ITM_BSET_FIFO_ERROR(player);
886 /* Enable underflow recovery interrupts */
887 if (player->info->underflow_enabled) {
888 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_DONE(player);
889 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_FAILED(player);
892 /* Reset uniperipheral player */
893 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
895 ret = reset_player(player);
900 * Does not use IEC61937 features of the uniperipheral hardware.
901 * Instead it performs IEC61937 in software and inserts it directly
902 * into the audio data stream. As such, when encoded mode is selected,
903 * linear pcm mode is still used, but with the differences of the
904 * channel status bits set for encoded mode and the validity bits set.
906 SET_UNIPERIF_CTRL_OPERATION_PCM_DATA(player);
909 * If iec958 formatting is required for hdmi or spdif, then it must be
910 * enabled after the operation mode is set. If set prior to this, it
911 * will not take affect and hang the player.
913 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
914 if (UNIPERIF_TYPE_IS_IEC958(player))
915 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player);
917 /* Force channel status update (no update if clk disable) */
918 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
919 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player);
921 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player);
923 /* Update state to started */
924 player->state = UNIPERIF_STATE_STARTED;
929 static int uni_player_stop(struct uniperif *player)
933 /* The player should not be in stopped state */
934 if (player->state == UNIPERIF_STATE_STOPPED) {
935 dev_err(player->dev, "%s: invalid player state", __func__);
939 /* Turn the player off */
940 SET_UNIPERIF_CTRL_OPERATION_OFF(player);
942 /* Soft reset the player */
943 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
945 ret = reset_player(player);
949 /* Disable interrupts */
950 SET_UNIPERIF_ITM_BCLR(player, GET_UNIPERIF_ITM(player));
953 clk_disable_unprepare(player->clk);
955 /* Update state to stopped and return */
956 player->state = UNIPERIF_STATE_STOPPED;
961 int uni_player_resume(struct uniperif *player)
965 /* Select the frequency synthesizer clock */
966 if (player->clk_sel) {
967 ret = regmap_field_write(player->clk_sel, 1);
970 "%s: Failed to select freq synth clock",
976 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
977 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
978 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
979 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
983 EXPORT_SYMBOL_GPL(uni_player_resume);
985 static int uni_player_trigger(struct snd_pcm_substream *substream,
986 int cmd, struct snd_soc_dai *dai)
988 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
989 struct uniperif *player = priv->dai_data.uni;
992 case SNDRV_PCM_TRIGGER_START:
993 return uni_player_start(player);
994 case SNDRV_PCM_TRIGGER_STOP:
995 return uni_player_stop(player);
996 case SNDRV_PCM_TRIGGER_RESUME:
997 return uni_player_resume(player);
1003 static void uni_player_shutdown(struct snd_pcm_substream *substream,
1004 struct snd_soc_dai *dai)
1006 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
1007 struct uniperif *player = priv->dai_data.uni;
1009 if (player->state != UNIPERIF_STATE_STOPPED)
1010 /* Stop the player */
1011 uni_player_stop(player);
1013 player->substream = NULL;
1016 static int uni_player_parse_dt_audio_glue(struct platform_device *pdev,
1017 struct uniperif *player)
1019 struct device_node *node = pdev->dev.of_node;
1020 struct regmap *regmap;
1021 struct reg_field regfield[2] = {
1023 REG_FIELD(SYS_CFG_AUDIO_GLUE,
1024 8 + player->info->id,
1025 8 + player->info->id),
1026 /* PCMP_VALID_SEL */
1027 REG_FIELD(SYS_CFG_AUDIO_GLUE, 0, 1)
1030 regmap = syscon_regmap_lookup_by_phandle(node, "st,syscfg");
1032 if (IS_ERR(regmap)) {
1033 dev_err(&pdev->dev, "sti-audio-clk-glue syscf not found\n");
1034 return PTR_ERR(regmap);
1037 player->clk_sel = regmap_field_alloc(regmap, regfield[0]);
1038 player->valid_sel = regmap_field_alloc(regmap, regfield[1]);
1043 static int uni_player_parse_dt(struct platform_device *pdev,
1044 struct uniperif *player)
1046 struct uniperif_info *info;
1047 struct device *dev = &pdev->dev;
1048 struct device_node *pnode = pdev->dev.of_node;
1051 /* Allocate memory for the info structure */
1052 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1056 if (of_property_read_u32(pnode, "st,version", &player->ver) ||
1057 player->ver == SND_ST_UNIPERIF_VERSION_UNKNOWN) {
1058 dev_err(dev, "Unknown uniperipheral version ");
1061 /* Underflow recovery is only supported on later ip revisions */
1062 if (player->ver >= SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
1063 info->underflow_enabled = 1;
1065 if (of_property_read_u32(pnode, "st,uniperiph-id", &info->id)) {
1066 dev_err(dev, "uniperipheral id not defined");
1070 /* Read the device mode property */
1071 if (of_property_read_string(pnode, "st,mode", &mode)) {
1072 dev_err(dev, "uniperipheral mode not defined");
1076 if (strcasecmp(mode, "hdmi") == 0)
1077 info->type = SND_ST_UNIPERIF_TYPE_HDMI;
1078 else if (strcasecmp(mode, "pcm") == 0)
1079 info->type = SND_ST_UNIPERIF_TYPE_PCM;
1080 else if (strcasecmp(mode, "spdif") == 0)
1081 info->type = SND_ST_UNIPERIF_TYPE_SPDIF;
1082 else if (strcasecmp(mode, "tdm") == 0)
1083 info->type = SND_ST_UNIPERIF_TYPE_TDM;
1085 info->type = SND_ST_UNIPERIF_TYPE_NONE;
1087 /* Save the info structure */
1088 player->info = info;
1090 /* Get PCM_CLK_SEL & PCMP_VALID_SEL from audio-glue-ctrl SoC reg */
1091 if (uni_player_parse_dt_audio_glue(pdev, player))
1097 static const struct snd_soc_dai_ops uni_player_dai_ops = {
1098 .startup = uni_player_startup,
1099 .shutdown = uni_player_shutdown,
1100 .prepare = uni_player_prepare,
1101 .trigger = uni_player_trigger,
1102 .hw_params = sti_uniperiph_dai_hw_params,
1103 .set_fmt = sti_uniperiph_dai_set_fmt,
1104 .set_sysclk = uni_player_set_sysclk,
1105 .set_tdm_slot = sti_uniperiph_set_tdm_slot
1108 int uni_player_init(struct platform_device *pdev,
1109 struct uniperif *player)
1113 player->dev = &pdev->dev;
1114 player->state = UNIPERIF_STATE_STOPPED;
1115 player->dai_ops = &uni_player_dai_ops;
1117 ret = uni_player_parse_dt(pdev, player);
1120 dev_err(player->dev, "Failed to parse DeviceTree");
1124 if (UNIPERIF_TYPE_IS_TDM(player))
1125 player->hw = &uni_tdm_hw;
1127 player->hw = &uni_player_pcm_hw;
1129 /* Get uniperif resource */
1130 player->clk = of_clk_get(pdev->dev.of_node, 0);
1131 if (IS_ERR(player->clk))
1132 ret = PTR_ERR(player->clk);
1134 /* Select the frequency synthesizer clock */
1135 if (player->clk_sel) {
1136 ret = regmap_field_write(player->clk_sel, 1);
1138 dev_err(player->dev,
1139 "%s: Failed to select freq synth clock",
1145 /* connect to I2S/TDM TX bus */
1146 if (player->valid_sel &&
1147 (player->info->id == UNIPERIF_PLAYER_I2S_OUT)) {
1148 ret = regmap_field_write(player->valid_sel, player->info->id);
1150 dev_err(player->dev,
1151 "%s: unable to connect to tdm bus", __func__);
1156 ret = devm_request_irq(&pdev->dev, player->irq,
1157 uni_player_irq_handler, IRQF_SHARED,
1158 dev_name(&pdev->dev), player);
1162 mutex_init(&player->ctrl_lock);
1164 /* Ensure that disabled by default */
1165 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
1166 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
1167 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
1168 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
1170 if (UNIPERIF_TYPE_IS_IEC958(player)) {
1171 /* Set default iec958 status bits */
1173 /* Consumer, PCM, copyright, 2ch, mode 0 */
1174 player->stream_settings.iec958.status[0] = 0x00;
1175 /* Broadcast reception category */
1176 player->stream_settings.iec958.status[1] =
1177 IEC958_AES1_CON_GENERAL;
1178 /* Do not take into account source or channel number */
1179 player->stream_settings.iec958.status[2] =
1180 IEC958_AES2_CON_SOURCE_UNSPEC;
1181 /* Sampling frequency not indicated */
1182 player->stream_settings.iec958.status[3] =
1183 IEC958_AES3_CON_FS_NOTID;
1184 /* Max sample word 24-bit, sample word length not indicated */
1185 player->stream_settings.iec958.status[4] =
1186 IEC958_AES4_CON_MAX_WORDLEN_24 |
1187 IEC958_AES4_CON_WORDLEN_24_20;
1189 player->num_ctrls = ARRAY_SIZE(snd_sti_iec_ctl);
1190 player->snd_ctrls = snd_sti_iec_ctl[0];
1192 player->num_ctrls = ARRAY_SIZE(snd_sti_pcm_ctl);
1193 player->snd_ctrls = snd_sti_pcm_ctl[0];
1198 EXPORT_SYMBOL_GPL(uni_player_init);