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
24 #define SYS_CFG_AUDI0_GLUE_PCM_CLKX 8
27 * Driver specific types.
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))
40 * Note: snd_pcm_hardware is linked to DMA controller but is declared here to
41 * integrate DAI_CPU capability in term of rate and supported channels
43 const struct snd_pcm_hardware uni_player_pcm_hw = {
44 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
45 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP |
46 SNDRV_PCM_INFO_MMAP_VALID,
47 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE,
49 .rates = SNDRV_PCM_RATE_CONTINUOUS,
59 .period_bytes_min = 128,
60 .period_bytes_max = 64 * PAGE_SIZE,
61 .buffer_bytes_max = 256 * PAGE_SIZE
64 static inline int reset_player(struct uniperif *player)
68 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
69 while (GET_UNIPERIF_SOFT_RST_SOFT_RST(player) && count) {
76 dev_err(player->dev, "Failed to reset uniperif");
84 * uni_player_irq_handler
85 * In case of error audio stream is stopped; stop action is protected via PCM
86 * stream lock to avoid race condition with trigger callback.
88 static irqreturn_t uni_player_irq_handler(int irq, void *dev_id)
90 irqreturn_t ret = IRQ_NONE;
91 struct uniperif *player = dev_id;
95 if (player->state == UNIPERIF_STATE_STOPPED) {
96 /* Unexpected IRQ: do nothing */
100 /* Get interrupt status & clear them immediately */
101 status = GET_UNIPERIF_ITS(player);
102 SET_UNIPERIF_ITS_BCLR(player, status);
104 /* Check for fifo error (underrun) */
105 if (unlikely(status & UNIPERIF_ITS_FIFO_ERROR_MASK(player))) {
106 dev_err(player->dev, "FIFO underflow error detected");
108 /* Interrupt is just for information when underflow recovery */
109 if (player->info->underflow_enabled) {
110 /* Update state to underflow */
111 player->state = UNIPERIF_STATE_UNDERFLOW;
114 /* Disable interrupt so doesn't continually fire */
115 SET_UNIPERIF_ITM_BCLR_FIFO_ERROR(player);
117 /* Stop the player */
118 snd_pcm_stream_lock(player->substream);
119 snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN);
120 snd_pcm_stream_unlock(player->substream);
126 /* Check for dma error (overrun) */
127 if (unlikely(status & UNIPERIF_ITS_DMA_ERROR_MASK(player))) {
128 dev_err(player->dev, "DMA error detected");
130 /* Disable interrupt so doesn't continually fire */
131 SET_UNIPERIF_ITM_BCLR_DMA_ERROR(player);
133 /* Stop the player */
134 snd_pcm_stream_lock(player->substream);
135 snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN);
136 snd_pcm_stream_unlock(player->substream);
141 /* Check for underflow recovery done */
142 if (unlikely(status & UNIPERIF_ITM_UNDERFLOW_REC_DONE_MASK(player))) {
143 if (!player->info->underflow_enabled) {
144 dev_err(player->dev, "unexpected Underflow recovering");
147 /* Read the underflow recovery duration */
148 tmp = GET_UNIPERIF_STATUS_1_UNDERFLOW_DURATION(player);
150 /* Clear the underflow recovery duration */
151 SET_UNIPERIF_BIT_CONTROL_CLR_UNDERFLOW_DURATION(player);
153 /* Update state to started */
154 player->state = UNIPERIF_STATE_STARTED;
159 /* Check if underflow recovery failed */
160 if (unlikely(status &
161 UNIPERIF_ITM_UNDERFLOW_REC_FAILED_MASK(player))) {
162 dev_err(player->dev, "Underflow recovery failed");
164 /* Stop the player */
165 snd_pcm_stream_lock(player->substream);
166 snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN);
167 snd_pcm_stream_unlock(player->substream);
175 static void uni_player_set_channel_status(struct uniperif *player,
176 struct snd_pcm_runtime *runtime)
182 * Some AVRs and TVs require the channel status to contain a correct
183 * sampling frequency. If no sample rate is already specified, then
186 if (runtime && (player->stream_settings.iec958.status[3]
187 == IEC958_AES3_CON_FS_NOTID)) {
188 switch (runtime->rate) {
190 player->stream_settings.iec958.status[3] =
191 IEC958_AES3_CON_FS_22050;
194 player->stream_settings.iec958.status[3] =
195 IEC958_AES3_CON_FS_44100;
198 player->stream_settings.iec958.status[3] =
199 IEC958_AES3_CON_FS_88200;
202 player->stream_settings.iec958.status[3] =
203 IEC958_AES3_CON_FS_176400;
206 player->stream_settings.iec958.status[3] =
207 IEC958_AES3_CON_FS_24000;
210 player->stream_settings.iec958.status[3] =
211 IEC958_AES3_CON_FS_48000;
214 player->stream_settings.iec958.status[3] =
215 IEC958_AES3_CON_FS_96000;
218 player->stream_settings.iec958.status[3] =
219 IEC958_AES3_CON_FS_192000;
222 player->stream_settings.iec958.status[3] =
223 IEC958_AES3_CON_FS_32000;
226 /* Mark as sampling frequency not indicated */
227 player->stream_settings.iec958.status[3] =
228 IEC958_AES3_CON_FS_NOTID;
234 * Use audio mode status to select PCM or encoded mode
236 if (player->stream_settings.iec958.status[0] & IEC958_AES0_NONAUDIO)
237 player->stream_settings.encoding_mode =
238 UNIPERIF_IEC958_ENCODING_MODE_ENCODED;
240 player->stream_settings.encoding_mode =
241 UNIPERIF_IEC958_ENCODING_MODE_PCM;
243 if (player->stream_settings.encoding_mode ==
244 UNIPERIF_IEC958_ENCODING_MODE_PCM)
245 /* Clear user validity bits */
246 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0);
248 /* Set user validity bits */
249 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 1);
251 /* Program the new channel status */
252 for (n = 0; n < 6; ++n) {
254 player->stream_settings.iec958.status[0 + (n * 4)] & 0xf;
256 player->stream_settings.iec958.status[1 + (n * 4)] << 8;
258 player->stream_settings.iec958.status[2 + (n * 4)] << 16;
260 player->stream_settings.iec958.status[3 + (n * 4)] << 24;
261 SET_UNIPERIF_CHANNEL_STA_REGN(player, n, status);
264 /* Update the channel status */
265 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
266 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player);
268 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player);
271 static int uni_player_prepare_iec958(struct uniperif *player,
272 struct snd_pcm_runtime *runtime)
276 clk_div = player->mclk / runtime->rate;
278 /* Oversampling must be multiple of 128 as iec958 frame is 32-bits */
279 if ((clk_div % 128) || (clk_div <= 0)) {
280 dev_err(player->dev, "%s: invalid clk_div %d",
285 switch (runtime->format) {
286 case SNDRV_PCM_FORMAT_S16_LE:
287 /* 16/16 memory format */
288 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player);
289 /* 16-bits per sub-frame */
290 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
291 /* Set 16-bit sample precision */
292 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player);
294 case SNDRV_PCM_FORMAT_S32_LE:
295 /* 16/0 memory format */
296 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
297 /* 32-bits per sub-frame */
298 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
299 /* Set 24-bit sample precision */
300 SET_UNIPERIF_I2S_FMT_DATA_SIZE_24(player);
303 dev_err(player->dev, "format not supported");
307 /* Set parity to be calculated by the hardware */
308 SET_UNIPERIF_CONFIG_PARITY_CNTR_BY_HW(player);
310 /* Set channel status bits to be inserted by the hardware */
311 SET_UNIPERIF_CONFIG_CHANNEL_STA_CNTR_BY_HW(player);
313 /* Set user data bits to be inserted by the hardware */
314 SET_UNIPERIF_CONFIG_USER_DAT_CNTR_BY_HW(player);
316 /* Set validity bits to be inserted by the hardware */
317 SET_UNIPERIF_CONFIG_VALIDITY_DAT_CNTR_BY_HW(player);
319 /* Set full software control to disabled */
320 SET_UNIPERIF_CONFIG_SPDIF_SW_CTRL_DISABLE(player);
322 SET_UNIPERIF_CTRL_ZERO_STUFF_HW(player);
324 /* Update the channel status */
325 uni_player_set_channel_status(player, runtime);
327 /* Clear the user validity user bits */
328 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0);
330 /* Disable one-bit audio mode */
331 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player);
333 /* Enable consecutive frames repetition of Z preamble (not for HBRA) */
334 SET_UNIPERIF_CONFIG_REPEAT_CHL_STS_ENABLE(player);
336 /* Change to SUF0_SUBF1 and left/right channels swap! */
337 SET_UNIPERIF_CONFIG_SUBFRAME_SEL_SUBF1_SUBF0(player);
339 /* Set data output as MSB first */
340 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
342 if (player->stream_settings.encoding_mode ==
343 UNIPERIF_IEC958_ENCODING_MODE_ENCODED)
344 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_ON(player);
346 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_OFF(player);
348 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2);
350 /* Set rounding to off */
351 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
353 /* Set clock divisor */
354 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / 128);
356 /* Set the spdif latency to not wait before starting player */
357 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
360 * Ensure iec958 formatting is off. It will be enabled in function
361 * uni_player_start() at the same time as the operation
362 * mode is set to work around a silicon issue.
364 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
365 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player);
367 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player);
372 static int uni_player_prepare_pcm(struct uniperif *player,
373 struct snd_pcm_runtime *runtime)
375 int output_frame_size, slot_width, clk_div;
377 /* Force slot width to 32 in I2S mode (HW constraint) */
378 if ((player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
379 SND_SOC_DAIFMT_I2S) {
382 switch (runtime->format) {
383 case SNDRV_PCM_FORMAT_S16_LE:
391 output_frame_size = slot_width * runtime->channels;
393 clk_div = player->mclk / runtime->rate;
395 * For 32 bits subframe clk_div must be a multiple of 128,
396 * for 16 bits must be a multiple of 64
398 if ((slot_width == 32) && (clk_div % 128)) {
399 dev_err(player->dev, "%s: invalid clk_div", __func__);
403 if ((slot_width == 16) && (clk_div % 64)) {
404 dev_err(player->dev, "%s: invalid clk_div", __func__);
409 * Number of bits per subframe (which is one channel sample)
410 * on output - Transfer 16 or 32 bits from FIFO
412 switch (slot_width) {
414 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
415 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player);
418 SET_UNIPERIF_I2S_FMT_NBIT_16(player);
419 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player);
422 dev_err(player->dev, "subframe format not supported");
426 /* Configure data memory format */
427 switch (runtime->format) {
428 case SNDRV_PCM_FORMAT_S16_LE:
429 /* One data word contains two samples */
430 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player);
433 case SNDRV_PCM_FORMAT_S32_LE:
435 * Actually "16 bits/0 bits" means "32/28/24/20/18/16 bits
436 * on the left than zeros (if less than 32 bytes)"... ;-)
438 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
442 dev_err(player->dev, "format not supported");
446 /* Set rounding to off */
447 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
449 /* Set clock divisor */
450 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / (2 * output_frame_size));
452 /* Number of channelsmust be even*/
453 if ((runtime->channels % 2) || (runtime->channels < 2) ||
454 (runtime->channels > 10)) {
455 dev_err(player->dev, "%s: invalid nb of channels", __func__);
459 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2);
461 /* Set 1-bit audio format to disabled */
462 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player);
464 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
465 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
467 /* No iec958 formatting as outputting to DAC */
468 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player);
473 static int uni_player_set_sysclk(struct snd_soc_dai *dai, int clk_id,
474 unsigned int freq, int dir)
476 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
477 struct uniperif *player = priv->dai_data.uni;
480 if (dir == SND_SOC_CLOCK_IN)
486 ret = clk_set_rate(player->clk, freq);
493 static int uni_player_prepare(struct snd_pcm_substream *substream,
494 struct snd_soc_dai *dai)
496 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
497 struct uniperif *player = priv->dai_data.uni;
498 struct snd_pcm_runtime *runtime = substream->runtime;
499 int transfer_size, trigger_limit;
502 /* The player should be stopped */
503 if (player->state != UNIPERIF_STATE_STOPPED) {
504 dev_err(player->dev, "%s: invalid player state %d", __func__,
509 /* Calculate transfer size (in fifo cells and bytes) for frame count */
510 transfer_size = runtime->channels * UNIPERIF_FIFO_FRAMES;
512 /* Calculate number of empty cells available before asserting DREQ */
513 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
514 trigger_limit = UNIPERIF_FIFO_SIZE - transfer_size;
517 * Since SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
518 * FDMA_TRIGGER_LIMIT also controls when the state switches
519 * from OFF or STANDBY to AUDIO DATA.
521 trigger_limit = transfer_size;
524 /* Trigger limit must be an even number */
525 if ((!trigger_limit % 2) || (trigger_limit != 1 && transfer_size % 2) ||
526 (trigger_limit > UNIPERIF_CONFIG_DMA_TRIG_LIMIT_MASK(player))) {
527 dev_err(player->dev, "invalid trigger limit %d", trigger_limit);
531 SET_UNIPERIF_CONFIG_DMA_TRIG_LIMIT(player, trigger_limit);
533 /* Uniperipheral setup depends on player type */
534 switch (player->info->player_type) {
535 case SND_ST_UNIPERIF_PLAYER_TYPE_HDMI:
536 ret = uni_player_prepare_iec958(player, runtime);
538 case SND_ST_UNIPERIF_PLAYER_TYPE_PCM:
539 ret = uni_player_prepare_pcm(player, runtime);
541 case SND_ST_UNIPERIF_PLAYER_TYPE_SPDIF:
542 ret = uni_player_prepare_iec958(player, runtime);
545 dev_err(player->dev, "invalid player type");
552 switch (player->daifmt & SND_SOC_DAIFMT_INV_MASK) {
553 case SND_SOC_DAIFMT_NB_NF:
554 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
555 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
557 case SND_SOC_DAIFMT_NB_IF:
558 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
559 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
561 case SND_SOC_DAIFMT_IB_NF:
562 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
563 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
565 case SND_SOC_DAIFMT_IB_IF:
566 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
567 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
571 switch (player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) {
572 case SND_SOC_DAIFMT_I2S:
573 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
574 SET_UNIPERIF_I2S_FMT_PADDING_I2S_MODE(player);
576 case SND_SOC_DAIFMT_LEFT_J:
577 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
578 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
580 case SND_SOC_DAIFMT_RIGHT_J:
581 SET_UNIPERIF_I2S_FMT_ALIGN_RIGHT(player);
582 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
585 dev_err(player->dev, "format not supported");
589 SET_UNIPERIF_I2S_FMT_NO_OF_SAMPLES_TO_READ(player, 0);
591 /* Reset uniperipheral player */
592 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
594 return reset_player(player);
597 static int uni_player_start(struct uniperif *player)
601 /* The player should be stopped */
602 if (player->state != UNIPERIF_STATE_STOPPED) {
603 dev_err(player->dev, "%s: invalid player state", __func__);
607 ret = clk_prepare_enable(player->clk);
609 dev_err(player->dev, "%s: Failed to enable clock", __func__);
613 /* Clear any pending interrupts */
614 SET_UNIPERIF_ITS_BCLR(player, GET_UNIPERIF_ITS(player));
616 /* Set the interrupt mask */
617 SET_UNIPERIF_ITM_BSET_DMA_ERROR(player);
618 SET_UNIPERIF_ITM_BSET_FIFO_ERROR(player);
620 /* Enable underflow recovery interrupts */
621 if (player->info->underflow_enabled) {
622 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_DONE(player);
623 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_FAILED(player);
626 /* Reset uniperipheral player */
627 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
629 ret = reset_player(player);
634 * Does not use IEC61937 features of the uniperipheral hardware.
635 * Instead it performs IEC61937 in software and inserts it directly
636 * into the audio data stream. As such, when encoded mode is selected,
637 * linear pcm mode is still used, but with the differences of the
638 * channel status bits set for encoded mode and the validity bits set.
640 SET_UNIPERIF_CTRL_OPERATION_PCM_DATA(player);
643 * If iec958 formatting is required for hdmi or spdif, then it must be
644 * enabled after the operation mode is set. If set prior to this, it
645 * will not take affect and hang the player.
647 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
648 if (UNIPERIF_PLAYER_TYPE_IS_IEC958(player))
649 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player);
651 /* Force channel status update (no update if clk disable) */
652 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
653 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player);
655 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player);
657 /* Update state to started */
658 player->state = UNIPERIF_STATE_STARTED;
663 static int uni_player_stop(struct uniperif *player)
667 /* The player should not be in stopped state */
668 if (player->state == UNIPERIF_STATE_STOPPED) {
669 dev_err(player->dev, "%s: invalid player state", __func__);
673 /* Turn the player off */
674 SET_UNIPERIF_CTRL_OPERATION_OFF(player);
676 /* Soft reset the player */
677 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
679 ret = reset_player(player);
683 /* Disable interrupts */
684 SET_UNIPERIF_ITM_BCLR(player, GET_UNIPERIF_ITM(player));
687 clk_disable_unprepare(player->clk);
689 /* Update state to stopped and return */
690 player->state = UNIPERIF_STATE_STOPPED;
695 int uni_player_resume(struct uniperif *player)
699 /* Select the frequency synthesizer clock */
700 if (player->clk_sel) {
701 ret = regmap_field_write(player->clk_sel, 1);
704 "%s: Failed to select freq synth clock",
710 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
711 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
712 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
713 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
717 EXPORT_SYMBOL_GPL(uni_player_resume);
719 static int uni_player_trigger(struct snd_pcm_substream *substream,
720 int cmd, struct snd_soc_dai *dai)
722 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
723 struct uniperif *player = priv->dai_data.uni;
726 case SNDRV_PCM_TRIGGER_START:
727 return uni_player_start(player);
728 case SNDRV_PCM_TRIGGER_STOP:
729 return uni_player_stop(player);
730 case SNDRV_PCM_TRIGGER_RESUME:
731 return uni_player_resume(player);
737 static void uni_player_shutdown(struct snd_pcm_substream *substream,
738 struct snd_soc_dai *dai)
740 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
741 struct uniperif *player = priv->dai_data.uni;
743 if (player->state != UNIPERIF_STATE_STOPPED)
744 /* Stop the player */
745 uni_player_stop(player);
748 static int uni_player_parse_dt_clk_glue(struct platform_device *pdev,
749 struct uniperif *player)
752 struct device_node *node = pdev->dev.of_node;
753 struct regmap *regmap;
755 bit_offset = SYS_CFG_AUDI0_GLUE_PCM_CLKX + player->info->id;
757 regmap = syscon_regmap_lookup_by_phandle(node, "st,syscfg");
760 struct reg_field regfield =
761 REG_FIELD(SYS_CFG_AUDIO_GLUE, bit_offset, bit_offset);
763 player->clk_sel = regmap_field_alloc(regmap, regfield);
765 dev_err(&pdev->dev, "sti-audio-clk-glue syscf not found\n");
772 static int uni_player_parse_dt(struct platform_device *pdev,
773 struct uniperif *player)
775 struct uniperif_info *info;
776 struct device *dev = &pdev->dev;
777 struct device_node *pnode = pdev->dev.of_node;
780 /* Allocate memory for the info structure */
781 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
785 of_property_read_u32(pnode, "version", &player->ver);
786 if (player->ver == SND_ST_UNIPERIF_VERSION_UNKNOWN) {
787 dev_err(dev, "Unknown uniperipheral version ");
790 /* Underflow recovery is only supported on later ip revisions */
791 if (player->ver >= SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
792 info->underflow_enabled = 1;
794 of_property_read_u32(pnode, "uniperiph-id", &info->id);
796 /* Read the device mode property */
797 of_property_read_string(pnode, "mode", &mode);
799 if (strcasecmp(mode, "hdmi") == 0)
800 info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_HDMI;
801 else if (strcasecmp(mode, "pcm") == 0)
802 info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_PCM;
803 else if (strcasecmp(mode, "spdif") == 0)
804 info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_SPDIF;
806 info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_NONE;
808 /* Save the info structure */
811 /* Get the PCM_CLK_SEL bit from audio-glue-ctrl SoC register */
812 if (uni_player_parse_dt_clk_glue(pdev, player))
818 const struct snd_soc_dai_ops uni_player_dai_ops = {
819 .shutdown = uni_player_shutdown,
820 .prepare = uni_player_prepare,
821 .trigger = uni_player_trigger,
822 .hw_params = sti_uniperiph_dai_hw_params,
823 .set_fmt = sti_uniperiph_dai_set_fmt,
824 .set_sysclk = uni_player_set_sysclk
827 int uni_player_init(struct platform_device *pdev,
828 struct uniperif *player)
832 player->dev = &pdev->dev;
833 player->state = UNIPERIF_STATE_STOPPED;
834 player->hw = &uni_player_pcm_hw;
835 player->dai_ops = &uni_player_dai_ops;
837 ret = uni_player_parse_dt(pdev, player);
840 dev_err(player->dev, "Failed to parse DeviceTree");
844 /* Get uniperif resource */
845 player->clk = of_clk_get(pdev->dev.of_node, 0);
846 if (IS_ERR(player->clk))
847 ret = PTR_ERR(player->clk);
849 /* Select the frequency synthesizer clock */
850 if (player->clk_sel) {
851 ret = regmap_field_write(player->clk_sel, 1);
854 "%s: Failed to select freq synth clock",
860 ret = devm_request_irq(&pdev->dev, player->irq,
861 uni_player_irq_handler, IRQF_SHARED,
862 dev_name(&pdev->dev), player);
866 /* Ensure that disabled by default */
867 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
868 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
869 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
870 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
872 if (UNIPERIF_PLAYER_TYPE_IS_IEC958(player)) {
873 /* Set default iec958 status bits */
875 /* Consumer, PCM, copyright, 2ch, mode 0 */
876 player->stream_settings.iec958.status[0] = 0x00;
877 /* Broadcast reception category */
878 player->stream_settings.iec958.status[1] =
879 IEC958_AES1_CON_GENERAL;
880 /* Do not take into account source or channel number */
881 player->stream_settings.iec958.status[2] =
882 IEC958_AES2_CON_SOURCE_UNSPEC;
883 /* Sampling frequency not indicated */
884 player->stream_settings.iec958.status[3] =
885 IEC958_AES3_CON_FS_NOTID;
886 /* Max sample word 24-bit, sample word length not indicated */
887 player->stream_settings.iec958.status[4] =
888 IEC958_AES4_CON_MAX_WORDLEN_24 |
889 IEC958_AES4_CON_WORDLEN_24_20;
894 EXPORT_SYMBOL_GPL(uni_player_init);