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
10 #include <linux/mfd/syscon.h>
12 #include <sound/asoundef.h>
13 #include <sound/soc.h>
18 * Some hardware-related definitions
21 /* sys config registers definitions */
22 #define SYS_CFG_AUDIO_GLUE 0xA4
25 * Driver specific types.
28 #define UNIPERIF_PLAYER_CLK_ADJ_MIN -999999
29 #define UNIPERIF_PLAYER_CLK_ADJ_MAX 1000000
30 #define UNIPERIF_PLAYER_I2S_OUT 1 /* player id connected to I2S/TDM TX bus */
33 * Note: snd_pcm_hardware is linked to DMA controller but is declared here to
34 * integrate DAI_CPU capability in term of rate and supported channels
36 static const struct snd_pcm_hardware uni_player_pcm_hw
= {
37 .info
= SNDRV_PCM_INFO_INTERLEAVED
| SNDRV_PCM_INFO_BLOCK_TRANSFER
|
38 SNDRV_PCM_INFO_PAUSE
| SNDRV_PCM_INFO_MMAP
|
39 SNDRV_PCM_INFO_MMAP_VALID
,
40 .formats
= SNDRV_PCM_FMTBIT_S32_LE
| SNDRV_PCM_FMTBIT_S16_LE
,
42 .rates
= SNDRV_PCM_RATE_CONTINUOUS
,
52 .period_bytes_min
= 128,
53 .period_bytes_max
= 64 * PAGE_SIZE
,
54 .buffer_bytes_max
= 256 * PAGE_SIZE
58 * uni_player_irq_handler
59 * In case of error audio stream is stopped; stop action is protected via PCM
60 * stream lock to avoid race condition with trigger callback.
62 static irqreturn_t
uni_player_irq_handler(int irq
, void *dev_id
)
64 irqreturn_t ret
= IRQ_NONE
;
65 struct uniperif
*player
= dev_id
;
69 if (player
->state
== UNIPERIF_STATE_STOPPED
) {
70 /* Unexpected IRQ: do nothing */
74 /* Get interrupt status & clear them immediately */
75 status
= GET_UNIPERIF_ITS(player
);
76 SET_UNIPERIF_ITS_BCLR(player
, status
);
78 /* Check for fifo error (underrun) */
79 if (unlikely(status
& UNIPERIF_ITS_FIFO_ERROR_MASK(player
))) {
80 dev_err(player
->dev
, "FIFO underflow error detected\n");
82 /* Interrupt is just for information when underflow recovery */
83 if (player
->underflow_enabled
) {
84 /* Update state to underflow */
85 player
->state
= UNIPERIF_STATE_UNDERFLOW
;
88 /* Disable interrupt so doesn't continually fire */
89 SET_UNIPERIF_ITM_BCLR_FIFO_ERROR(player
);
92 snd_pcm_stream_lock(player
->substream
);
93 snd_pcm_stop(player
->substream
, SNDRV_PCM_STATE_XRUN
);
94 snd_pcm_stream_unlock(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_stream_lock(player
->substream
);
109 snd_pcm_stop(player
->substream
, SNDRV_PCM_STATE_XRUN
);
110 snd_pcm_stream_unlock(player
->substream
);
115 /* Check for underflow recovery done */
116 if (unlikely(status
& UNIPERIF_ITM_UNDERFLOW_REC_DONE_MASK(player
))) {
117 if (!player
->underflow_enabled
) {
119 "unexpected Underflow recovering\n");
122 /* Read the underflow recovery duration */
123 tmp
= GET_UNIPERIF_STATUS_1_UNDERFLOW_DURATION(player
);
124 dev_dbg(player
->dev
, "Underflow recovered (%d LR clocks max)\n",
127 /* Clear the underflow recovery duration */
128 SET_UNIPERIF_BIT_CONTROL_CLR_UNDERFLOW_DURATION(player
);
130 /* Update state to started */
131 player
->state
= UNIPERIF_STATE_STARTED
;
136 /* Check if underflow recovery failed */
137 if (unlikely(status
&
138 UNIPERIF_ITM_UNDERFLOW_REC_FAILED_MASK(player
))) {
139 dev_err(player
->dev
, "Underflow recovery failed\n");
141 /* Stop the player */
142 snd_pcm_stream_lock(player
->substream
);
143 snd_pcm_stop(player
->substream
, SNDRV_PCM_STATE_XRUN
);
144 snd_pcm_stream_unlock(player
->substream
);
152 static int uni_player_clk_set_rate(struct uniperif
*player
, unsigned long rate
)
154 int rate_adjusted
, rate_achieved
, delta
, ret
;
155 int adjustment
= player
->clk_adj
;
159 * F = f + --------- * f = f + d
168 * a - adjustment in ppm (parts per milion)
169 * F - rate to be set in synthesizer
170 * d - delta (difference) between f and F
172 if (adjustment
< 0) {
173 /* div64_64 operates on unsigned values... */
175 adjustment
= -adjustment
;
179 /* 500000 ppm is 0.5, which is used to round up values */
180 delta
*= (int)div64_u64((uint64_t)rate
*
181 (uint64_t)adjustment
+ 500000, 1000000);
182 rate_adjusted
= rate
+ delta
;
184 /* Adjusted rate should never be == 0 */
188 ret
= clk_set_rate(player
->clk
, rate_adjusted
);
192 rate_achieved
= clk_get_rate(player
->clk
);
194 /* If value is 0 means that clock or parent not valid */
198 * Using ALSA's adjustment control, we can modify the rate to be up
199 * to twice as much as requested, but no more
201 delta
= rate_achieved
- rate
;
203 /* div64_64 operates on unsigned values... */
209 /* Frequency/2 is added to round up result */
210 adjustment
*= (int)div64_u64((uint64_t)delta
* 1000000 + rate
/ 2,
212 player
->clk_adj
= adjustment
;
216 static void uni_player_set_channel_status(struct uniperif
*player
,
217 struct snd_pcm_runtime
*runtime
)
223 * Some AVRs and TVs require the channel status to contain a correct
224 * sampling frequency. If no sample rate is already specified, then
227 mutex_lock(&player
->ctrl_lock
);
229 switch (runtime
->rate
) {
231 player
->stream_settings
.iec958
.status
[3] =
232 IEC958_AES3_CON_FS_22050
;
235 player
->stream_settings
.iec958
.status
[3] =
236 IEC958_AES3_CON_FS_44100
;
239 player
->stream_settings
.iec958
.status
[3] =
240 IEC958_AES3_CON_FS_88200
;
243 player
->stream_settings
.iec958
.status
[3] =
244 IEC958_AES3_CON_FS_176400
;
247 player
->stream_settings
.iec958
.status
[3] =
248 IEC958_AES3_CON_FS_24000
;
251 player
->stream_settings
.iec958
.status
[3] =
252 IEC958_AES3_CON_FS_48000
;
255 player
->stream_settings
.iec958
.status
[3] =
256 IEC958_AES3_CON_FS_96000
;
259 player
->stream_settings
.iec958
.status
[3] =
260 IEC958_AES3_CON_FS_192000
;
263 player
->stream_settings
.iec958
.status
[3] =
264 IEC958_AES3_CON_FS_32000
;
267 /* Mark as sampling frequency not indicated */
268 player
->stream_settings
.iec958
.status
[3] =
269 IEC958_AES3_CON_FS_NOTID
;
275 * Use audio mode status to select PCM or encoded mode
277 if (player
->stream_settings
.iec958
.status
[0] & IEC958_AES0_NONAUDIO
)
278 player
->stream_settings
.encoding_mode
=
279 UNIPERIF_IEC958_ENCODING_MODE_ENCODED
;
281 player
->stream_settings
.encoding_mode
=
282 UNIPERIF_IEC958_ENCODING_MODE_PCM
;
284 if (player
->stream_settings
.encoding_mode
==
285 UNIPERIF_IEC958_ENCODING_MODE_PCM
)
286 /* Clear user validity bits */
287 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player
, 0);
289 /* Set user validity bits */
290 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player
, 1);
292 /* Program the new channel status */
293 for (n
= 0; n
< 6; ++n
) {
295 player
->stream_settings
.iec958
.status
[0 + (n
* 4)] & 0xf;
297 player
->stream_settings
.iec958
.status
[1 + (n
* 4)] << 8;
299 player
->stream_settings
.iec958
.status
[2 + (n
* 4)] << 16;
301 player
->stream_settings
.iec958
.status
[3 + (n
* 4)] << 24;
302 SET_UNIPERIF_CHANNEL_STA_REGN(player
, n
, status
);
304 mutex_unlock(&player
->ctrl_lock
);
306 /* Update the channel status */
307 if (player
->ver
< SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
)
308 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player
);
310 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player
);
313 static int uni_player_prepare_iec958(struct uniperif
*player
,
314 struct snd_pcm_runtime
*runtime
)
318 clk_div
= player
->mclk
/ runtime
->rate
;
320 /* Oversampling must be multiple of 128 as iec958 frame is 32-bits */
321 if ((clk_div
% 128) || (clk_div
<= 0)) {
322 dev_err(player
->dev
, "%s: invalid clk_div %d\n",
327 switch (runtime
->format
) {
328 case SNDRV_PCM_FORMAT_S16_LE
:
329 /* 16/16 memory format */
330 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player
);
331 /* 16-bits per sub-frame */
332 SET_UNIPERIF_I2S_FMT_NBIT_32(player
);
333 /* Set 16-bit sample precision */
334 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player
);
336 case SNDRV_PCM_FORMAT_S32_LE
:
337 /* 16/0 memory format */
338 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player
);
339 /* 32-bits per sub-frame */
340 SET_UNIPERIF_I2S_FMT_NBIT_32(player
);
341 /* Set 24-bit sample precision */
342 SET_UNIPERIF_I2S_FMT_DATA_SIZE_24(player
);
345 dev_err(player
->dev
, "format not supported\n");
349 /* Set parity to be calculated by the hardware */
350 SET_UNIPERIF_CONFIG_PARITY_CNTR_BY_HW(player
);
352 /* Set channel status bits to be inserted by the hardware */
353 SET_UNIPERIF_CONFIG_CHANNEL_STA_CNTR_BY_HW(player
);
355 /* Set user data bits to be inserted by the hardware */
356 SET_UNIPERIF_CONFIG_USER_DAT_CNTR_BY_HW(player
);
358 /* Set validity bits to be inserted by the hardware */
359 SET_UNIPERIF_CONFIG_VALIDITY_DAT_CNTR_BY_HW(player
);
361 /* Set full software control to disabled */
362 SET_UNIPERIF_CONFIG_SPDIF_SW_CTRL_DISABLE(player
);
364 SET_UNIPERIF_CTRL_ZERO_STUFF_HW(player
);
366 /* Update the channel status */
367 uni_player_set_channel_status(player
, runtime
);
369 /* Clear the user validity user bits */
370 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player
, 0);
372 /* Disable one-bit audio mode */
373 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player
);
375 /* Enable consecutive frames repetition of Z preamble (not for HBRA) */
376 SET_UNIPERIF_CONFIG_REPEAT_CHL_STS_ENABLE(player
);
378 /* Change to SUF0_SUBF1 and left/right channels swap! */
379 SET_UNIPERIF_CONFIG_SUBFRAME_SEL_SUBF1_SUBF0(player
);
381 /* Set data output as MSB first */
382 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player
);
384 if (player
->stream_settings
.encoding_mode
==
385 UNIPERIF_IEC958_ENCODING_MODE_ENCODED
)
386 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_ON(player
);
388 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_OFF(player
);
390 SET_UNIPERIF_I2S_FMT_NUM_CH(player
, runtime
->channels
/ 2);
392 /* Set rounding to off */
393 SET_UNIPERIF_CTRL_ROUNDING_OFF(player
);
395 /* Set clock divisor */
396 SET_UNIPERIF_CTRL_DIVIDER(player
, clk_div
/ 128);
398 /* Set the spdif latency to not wait before starting player */
399 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player
);
402 * Ensure iec958 formatting is off. It will be enabled in function
403 * uni_player_start() at the same time as the operation
404 * mode is set to work around a silicon issue.
406 if (player
->ver
< SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
)
407 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player
);
409 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player
);
414 static int uni_player_prepare_pcm(struct uniperif
*player
,
415 struct snd_pcm_runtime
*runtime
)
417 int output_frame_size
, slot_width
, clk_div
;
419 /* Force slot width to 32 in I2S mode (HW constraint) */
420 if ((player
->daifmt
& SND_SOC_DAIFMT_FORMAT_MASK
) ==
424 slot_width
= snd_pcm_format_width(runtime
->format
);
426 output_frame_size
= slot_width
* runtime
->channels
;
428 clk_div
= player
->mclk
/ runtime
->rate
;
430 * For 32 bits subframe clk_div must be a multiple of 128,
431 * for 16 bits must be a multiple of 64
433 if ((slot_width
== 32) && (clk_div
% 128)) {
434 dev_err(player
->dev
, "%s: invalid clk_div\n", __func__
);
438 if ((slot_width
== 16) && (clk_div
% 64)) {
439 dev_err(player
->dev
, "%s: invalid clk_div\n", __func__
);
444 * Number of bits per subframe (which is one channel sample)
445 * on output - Transfer 16 or 32 bits from FIFO
447 switch (slot_width
) {
449 SET_UNIPERIF_I2S_FMT_NBIT_32(player
);
450 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player
);
453 SET_UNIPERIF_I2S_FMT_NBIT_16(player
);
454 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player
);
457 dev_err(player
->dev
, "subframe format not supported\n");
461 /* Configure data memory format */
462 switch (runtime
->format
) {
463 case SNDRV_PCM_FORMAT_S16_LE
:
464 /* One data word contains two samples */
465 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player
);
468 case SNDRV_PCM_FORMAT_S32_LE
:
470 * Actually "16 bits/0 bits" means "32/28/24/20/18/16 bits
471 * on the left than zeros (if less than 32 bytes)"... ;-)
473 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player
);
477 dev_err(player
->dev
, "format not supported\n");
481 /* Set rounding to off */
482 SET_UNIPERIF_CTRL_ROUNDING_OFF(player
);
484 /* Set clock divisor */
485 SET_UNIPERIF_CTRL_DIVIDER(player
, clk_div
/ (2 * output_frame_size
));
487 /* Number of channelsmust be even*/
488 if ((runtime
->channels
% 2) || (runtime
->channels
< 2) ||
489 (runtime
->channels
> 10)) {
490 dev_err(player
->dev
, "%s: invalid nb of channels\n", __func__
);
494 SET_UNIPERIF_I2S_FMT_NUM_CH(player
, runtime
->channels
/ 2);
496 /* Set 1-bit audio format to disabled */
497 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player
);
499 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player
);
501 /* No iec958 formatting as outputting to DAC */
502 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player
);
507 static int uni_player_prepare_tdm(struct uniperif
*player
,
508 struct snd_pcm_runtime
*runtime
)
510 int tdm_frame_size
; /* unip tdm frame size in bytes */
511 int user_frame_size
; /* user tdm frame size in bytes */
512 /* default unip TDM_WORD_POS_X_Y */
513 unsigned int word_pos
[4] = {
514 0x04060002, 0x0C0E080A, 0x14161012, 0x1C1E181A};
518 sti_uniperiph_get_unip_tdm_frame_size(player
);
520 sti_uniperiph_get_user_frame_size(runtime
);
522 /* fix 16/0 format */
523 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player
);
524 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player
);
526 /* number of words inserted on the TDM line */
527 SET_UNIPERIF_I2S_FMT_NUM_CH(player
, user_frame_size
/ 4 / 2);
529 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player
);
530 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player
);
532 /* Enable the tdm functionality */
533 SET_UNIPERIF_TDM_ENABLE_TDM_ENABLE(player
);
535 /* number of 8 bits timeslots avail in unip tdm frame */
536 SET_UNIPERIF_TDM_FS_REF_DIV_NUM_TIMESLOT(player
, tdm_frame_size
);
538 /* set the timeslot allocation for words in FIFO */
539 sti_uniperiph_get_tdm_word_pos(player
, word_pos
);
540 SET_UNIPERIF_TDM_WORD_POS(player
, 1_2
, word_pos
[WORD_1_2
]);
541 SET_UNIPERIF_TDM_WORD_POS(player
, 3_4
, word_pos
[WORD_3_4
]);
542 SET_UNIPERIF_TDM_WORD_POS(player
, 5_6
, word_pos
[WORD_5_6
]);
543 SET_UNIPERIF_TDM_WORD_POS(player
, 7_8
, word_pos
[WORD_7_8
]);
545 /* set unip clk rate (not done vai set_sysclk ops) */
546 freq
= runtime
->rate
* tdm_frame_size
* 8;
547 mutex_lock(&player
->ctrl_lock
);
548 ret
= uni_player_clk_set_rate(player
, freq
);
551 mutex_unlock(&player
->ctrl_lock
);
557 * ALSA uniperipheral iec958 controls
559 static int uni_player_ctl_iec958_info(struct snd_kcontrol
*kcontrol
,
560 struct snd_ctl_elem_info
*uinfo
)
562 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
568 static int uni_player_ctl_iec958_get(struct snd_kcontrol
*kcontrol
,
569 struct snd_ctl_elem_value
*ucontrol
)
571 struct snd_soc_dai
*dai
= snd_kcontrol_chip(kcontrol
);
572 struct sti_uniperiph_data
*priv
= snd_soc_dai_get_drvdata(dai
);
573 struct uniperif
*player
= priv
->dai_data
.uni
;
574 struct snd_aes_iec958
*iec958
= &player
->stream_settings
.iec958
;
576 mutex_lock(&player
->ctrl_lock
);
577 ucontrol
->value
.iec958
.status
[0] = iec958
->status
[0];
578 ucontrol
->value
.iec958
.status
[1] = iec958
->status
[1];
579 ucontrol
->value
.iec958
.status
[2] = iec958
->status
[2];
580 ucontrol
->value
.iec958
.status
[3] = iec958
->status
[3];
581 mutex_unlock(&player
->ctrl_lock
);
585 static int uni_player_ctl_iec958_put(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 iec958
->status
[0] = ucontrol
->value
.iec958
.status
[0];
595 iec958
->status
[1] = ucontrol
->value
.iec958
.status
[1];
596 iec958
->status
[2] = ucontrol
->value
.iec958
.status
[2];
597 iec958
->status
[3] = ucontrol
->value
.iec958
.status
[3];
598 mutex_unlock(&player
->ctrl_lock
);
600 uni_player_set_channel_status(player
, NULL
);
605 static struct snd_kcontrol_new uni_player_iec958_ctl
= {
606 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
607 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
608 .info
= uni_player_ctl_iec958_info
,
609 .get
= uni_player_ctl_iec958_get
,
610 .put
= uni_player_ctl_iec958_put
,
614 * uniperif rate adjustement control
616 static int snd_sti_clk_adjustment_info(struct snd_kcontrol
*kcontrol
,
617 struct snd_ctl_elem_info
*uinfo
)
619 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
621 uinfo
->value
.integer
.min
= UNIPERIF_PLAYER_CLK_ADJ_MIN
;
622 uinfo
->value
.integer
.max
= UNIPERIF_PLAYER_CLK_ADJ_MAX
;
623 uinfo
->value
.integer
.step
= 1;
628 static int snd_sti_clk_adjustment_get(struct snd_kcontrol
*kcontrol
,
629 struct snd_ctl_elem_value
*ucontrol
)
631 struct snd_soc_dai
*dai
= snd_kcontrol_chip(kcontrol
);
632 struct sti_uniperiph_data
*priv
= snd_soc_dai_get_drvdata(dai
);
633 struct uniperif
*player
= priv
->dai_data
.uni
;
635 mutex_lock(&player
->ctrl_lock
);
636 ucontrol
->value
.integer
.value
[0] = player
->clk_adj
;
637 mutex_unlock(&player
->ctrl_lock
);
642 static int snd_sti_clk_adjustment_put(struct snd_kcontrol
*kcontrol
,
643 struct snd_ctl_elem_value
*ucontrol
)
645 struct snd_soc_dai
*dai
= snd_kcontrol_chip(kcontrol
);
646 struct sti_uniperiph_data
*priv
= snd_soc_dai_get_drvdata(dai
);
647 struct uniperif
*player
= priv
->dai_data
.uni
;
650 if ((ucontrol
->value
.integer
.value
[0] < UNIPERIF_PLAYER_CLK_ADJ_MIN
) ||
651 (ucontrol
->value
.integer
.value
[0] > UNIPERIF_PLAYER_CLK_ADJ_MAX
))
654 mutex_lock(&player
->ctrl_lock
);
655 player
->clk_adj
= ucontrol
->value
.integer
.value
[0];
658 ret
= uni_player_clk_set_rate(player
, player
->mclk
);
659 mutex_unlock(&player
->ctrl_lock
);
664 static struct snd_kcontrol_new uni_player_clk_adj_ctl
= {
665 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
666 .name
= "PCM Playback Oversampling Freq. Adjustment",
667 .info
= snd_sti_clk_adjustment_info
,
668 .get
= snd_sti_clk_adjustment_get
,
669 .put
= snd_sti_clk_adjustment_put
,
672 static struct snd_kcontrol_new
*snd_sti_pcm_ctl
[] = {
673 &uni_player_clk_adj_ctl
,
676 static struct snd_kcontrol_new
*snd_sti_iec_ctl
[] = {
677 &uni_player_iec958_ctl
,
678 &uni_player_clk_adj_ctl
,
681 static int uni_player_startup(struct snd_pcm_substream
*substream
,
682 struct snd_soc_dai
*dai
)
684 struct sti_uniperiph_data
*priv
= snd_soc_dai_get_drvdata(dai
);
685 struct uniperif
*player
= priv
->dai_data
.uni
;
688 player
->substream
= substream
;
692 if (!UNIPERIF_TYPE_IS_TDM(player
))
695 /* refine hw constraint in tdm mode */
696 ret
= snd_pcm_hw_rule_add(substream
->runtime
, 0,
697 SNDRV_PCM_HW_PARAM_CHANNELS
,
698 sti_uniperiph_fix_tdm_chan
,
699 player
, SNDRV_PCM_HW_PARAM_CHANNELS
,
704 return snd_pcm_hw_rule_add(substream
->runtime
, 0,
705 SNDRV_PCM_HW_PARAM_FORMAT
,
706 sti_uniperiph_fix_tdm_format
,
707 player
, SNDRV_PCM_HW_PARAM_FORMAT
,
711 static int uni_player_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
712 unsigned int freq
, int dir
)
714 struct sti_uniperiph_data
*priv
= snd_soc_dai_get_drvdata(dai
);
715 struct uniperif
*player
= priv
->dai_data
.uni
;
718 if (UNIPERIF_TYPE_IS_TDM(player
) || (dir
== SND_SOC_CLOCK_IN
))
724 mutex_lock(&player
->ctrl_lock
);
725 ret
= uni_player_clk_set_rate(player
, freq
);
728 mutex_unlock(&player
->ctrl_lock
);
733 static int uni_player_prepare(struct snd_pcm_substream
*substream
,
734 struct snd_soc_dai
*dai
)
736 struct sti_uniperiph_data
*priv
= snd_soc_dai_get_drvdata(dai
);
737 struct uniperif
*player
= priv
->dai_data
.uni
;
738 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
739 int transfer_size
, trigger_limit
;
742 /* The player should be stopped */
743 if (player
->state
!= UNIPERIF_STATE_STOPPED
) {
744 dev_err(player
->dev
, "%s: invalid player state %d\n", __func__
,
749 /* Calculate transfer size (in fifo cells and bytes) for frame count */
750 if (player
->type
== SND_ST_UNIPERIF_TYPE_TDM
) {
751 /* transfer size = user frame size (in 32 bits FIFO cell) */
753 sti_uniperiph_get_user_frame_size(runtime
) / 4;
755 transfer_size
= runtime
->channels
* UNIPERIF_FIFO_FRAMES
;
758 /* Calculate number of empty cells available before asserting DREQ */
759 if (player
->ver
< SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
) {
760 trigger_limit
= UNIPERIF_FIFO_SIZE
- transfer_size
;
763 * Since SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
764 * FDMA_TRIGGER_LIMIT also controls when the state switches
765 * from OFF or STANDBY to AUDIO DATA.
767 trigger_limit
= transfer_size
;
770 /* Trigger limit must be an even number */
771 if ((!trigger_limit
% 2) || (trigger_limit
!= 1 && transfer_size
% 2) ||
772 (trigger_limit
> UNIPERIF_CONFIG_DMA_TRIG_LIMIT_MASK(player
))) {
773 dev_err(player
->dev
, "invalid trigger limit %d\n",
778 SET_UNIPERIF_CONFIG_DMA_TRIG_LIMIT(player
, trigger_limit
);
780 /* Uniperipheral setup depends on player type */
781 switch (player
->type
) {
782 case SND_ST_UNIPERIF_TYPE_HDMI
:
783 ret
= uni_player_prepare_iec958(player
, runtime
);
785 case SND_ST_UNIPERIF_TYPE_PCM
:
786 ret
= uni_player_prepare_pcm(player
, runtime
);
788 case SND_ST_UNIPERIF_TYPE_SPDIF
:
789 ret
= uni_player_prepare_iec958(player
, runtime
);
791 case SND_ST_UNIPERIF_TYPE_TDM
:
792 ret
= uni_player_prepare_tdm(player
, runtime
);
795 dev_err(player
->dev
, "invalid player type\n");
802 switch (player
->daifmt
& SND_SOC_DAIFMT_INV_MASK
) {
803 case SND_SOC_DAIFMT_NB_NF
:
804 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player
);
805 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player
);
807 case SND_SOC_DAIFMT_NB_IF
:
808 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player
);
809 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player
);
811 case SND_SOC_DAIFMT_IB_NF
:
812 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player
);
813 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player
);
815 case SND_SOC_DAIFMT_IB_IF
:
816 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player
);
817 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player
);
821 switch (player
->daifmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
822 case SND_SOC_DAIFMT_I2S
:
823 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player
);
824 SET_UNIPERIF_I2S_FMT_PADDING_I2S_MODE(player
);
826 case SND_SOC_DAIFMT_LEFT_J
:
827 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player
);
828 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player
);
830 case SND_SOC_DAIFMT_RIGHT_J
:
831 SET_UNIPERIF_I2S_FMT_ALIGN_RIGHT(player
);
832 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player
);
835 dev_err(player
->dev
, "format not supported\n");
839 SET_UNIPERIF_I2S_FMT_NO_OF_SAMPLES_TO_READ(player
, 0);
842 return sti_uniperiph_reset(player
);
845 static int uni_player_start(struct uniperif
*player
)
849 /* The player should be stopped */
850 if (player
->state
!= UNIPERIF_STATE_STOPPED
) {
851 dev_err(player
->dev
, "%s: invalid player state\n", __func__
);
855 ret
= clk_prepare_enable(player
->clk
);
857 dev_err(player
->dev
, "%s: Failed to enable clock\n", __func__
);
861 /* Clear any pending interrupts */
862 SET_UNIPERIF_ITS_BCLR(player
, GET_UNIPERIF_ITS(player
));
864 /* Set the interrupt mask */
865 SET_UNIPERIF_ITM_BSET_DMA_ERROR(player
);
866 SET_UNIPERIF_ITM_BSET_FIFO_ERROR(player
);
868 /* Enable underflow recovery interrupts */
869 if (player
->underflow_enabled
) {
870 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_DONE(player
);
871 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_FAILED(player
);
874 ret
= sti_uniperiph_reset(player
);
876 clk_disable_unprepare(player
->clk
);
881 * Does not use IEC61937 features of the uniperipheral hardware.
882 * Instead it performs IEC61937 in software and inserts it directly
883 * into the audio data stream. As such, when encoded mode is selected,
884 * linear pcm mode is still used, but with the differences of the
885 * channel status bits set for encoded mode and the validity bits set.
887 SET_UNIPERIF_CTRL_OPERATION_PCM_DATA(player
);
890 * If iec958 formatting is required for hdmi or spdif, then it must be
891 * enabled after the operation mode is set. If set prior to this, it
892 * will not take affect and hang the player.
894 if (player
->ver
< SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
)
895 if (UNIPERIF_TYPE_IS_IEC958(player
))
896 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player
);
898 /* Force channel status update (no update if clk disable) */
899 if (player
->ver
< SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
)
900 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player
);
902 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player
);
904 /* Update state to started */
905 player
->state
= UNIPERIF_STATE_STARTED
;
910 static int uni_player_stop(struct uniperif
*player
)
914 /* The player should not be in stopped state */
915 if (player
->state
== UNIPERIF_STATE_STOPPED
) {
916 dev_err(player
->dev
, "%s: invalid player state\n", __func__
);
920 /* Turn the player off */
921 SET_UNIPERIF_CTRL_OPERATION_OFF(player
);
923 ret
= sti_uniperiph_reset(player
);
927 /* Disable interrupts */
928 SET_UNIPERIF_ITM_BCLR(player
, GET_UNIPERIF_ITM(player
));
931 clk_disable_unprepare(player
->clk
);
933 /* Update state to stopped and return */
934 player
->state
= UNIPERIF_STATE_STOPPED
;
939 int uni_player_resume(struct uniperif
*player
)
943 /* Select the frequency synthesizer clock */
944 if (player
->clk_sel
) {
945 ret
= regmap_field_write(player
->clk_sel
, 1);
948 "%s: Failed to select freq synth clock\n",
954 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player
);
955 SET_UNIPERIF_CTRL_ROUNDING_OFF(player
);
956 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player
);
957 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player
);
961 EXPORT_SYMBOL_GPL(uni_player_resume
);
963 static int uni_player_trigger(struct snd_pcm_substream
*substream
,
964 int cmd
, struct snd_soc_dai
*dai
)
966 struct sti_uniperiph_data
*priv
= snd_soc_dai_get_drvdata(dai
);
967 struct uniperif
*player
= priv
->dai_data
.uni
;
970 case SNDRV_PCM_TRIGGER_START
:
971 return uni_player_start(player
);
972 case SNDRV_PCM_TRIGGER_STOP
:
973 return uni_player_stop(player
);
974 case SNDRV_PCM_TRIGGER_RESUME
:
975 return uni_player_resume(player
);
981 static void uni_player_shutdown(struct snd_pcm_substream
*substream
,
982 struct snd_soc_dai
*dai
)
984 struct sti_uniperiph_data
*priv
= snd_soc_dai_get_drvdata(dai
);
985 struct uniperif
*player
= priv
->dai_data
.uni
;
987 if (player
->state
!= UNIPERIF_STATE_STOPPED
)
988 /* Stop the player */
989 uni_player_stop(player
);
991 player
->substream
= NULL
;
994 static int uni_player_parse_dt_audio_glue(struct platform_device
*pdev
,
995 struct uniperif
*player
)
997 struct device_node
*node
= pdev
->dev
.of_node
;
998 struct regmap
*regmap
;
999 struct reg_field regfield
[2] = {
1001 REG_FIELD(SYS_CFG_AUDIO_GLUE
,
1004 /* PCMP_VALID_SEL */
1005 REG_FIELD(SYS_CFG_AUDIO_GLUE
, 0, 1)
1008 regmap
= syscon_regmap_lookup_by_phandle(node
, "st,syscfg");
1010 if (IS_ERR(regmap
)) {
1011 dev_err(&pdev
->dev
, "sti-audio-clk-glue syscf not found\n");
1012 return PTR_ERR(regmap
);
1015 player
->clk_sel
= regmap_field_alloc(regmap
, regfield
[0]);
1016 player
->valid_sel
= regmap_field_alloc(regmap
, regfield
[1]);
1021 static const struct snd_soc_dai_ops uni_player_dai_ops
= {
1022 .startup
= uni_player_startup
,
1023 .shutdown
= uni_player_shutdown
,
1024 .prepare
= uni_player_prepare
,
1025 .trigger
= uni_player_trigger
,
1026 .hw_params
= sti_uniperiph_dai_hw_params
,
1027 .set_fmt
= sti_uniperiph_dai_set_fmt
,
1028 .set_sysclk
= uni_player_set_sysclk
,
1029 .set_tdm_slot
= sti_uniperiph_set_tdm_slot
1032 int uni_player_init(struct platform_device
*pdev
,
1033 struct uniperif
*player
)
1037 player
->dev
= &pdev
->dev
;
1038 player
->state
= UNIPERIF_STATE_STOPPED
;
1039 player
->dai_ops
= &uni_player_dai_ops
;
1041 /* Get PCM_CLK_SEL & PCMP_VALID_SEL from audio-glue-ctrl SoC reg */
1042 ret
= uni_player_parse_dt_audio_glue(pdev
, player
);
1045 dev_err(player
->dev
, "Failed to parse DeviceTree\n");
1049 /* Underflow recovery is only supported on later ip revisions */
1050 if (player
->ver
>= SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
)
1051 player
->underflow_enabled
= 1;
1053 if (UNIPERIF_TYPE_IS_TDM(player
))
1054 player
->hw
= &uni_tdm_hw
;
1056 player
->hw
= &uni_player_pcm_hw
;
1058 /* Get uniperif resource */
1059 player
->clk
= of_clk_get(pdev
->dev
.of_node
, 0);
1060 if (IS_ERR(player
->clk
)) {
1061 dev_err(player
->dev
, "Failed to get clock\n");
1062 ret
= PTR_ERR(player
->clk
);
1065 /* Select the frequency synthesizer clock */
1066 if (player
->clk_sel
) {
1067 ret
= regmap_field_write(player
->clk_sel
, 1);
1069 dev_err(player
->dev
,
1070 "%s: Failed to select freq synth clock\n",
1076 /* connect to I2S/TDM TX bus */
1077 if (player
->valid_sel
&&
1078 (player
->id
== UNIPERIF_PLAYER_I2S_OUT
)) {
1079 ret
= regmap_field_write(player
->valid_sel
, player
->id
);
1081 dev_err(player
->dev
,
1082 "%s: unable to connect to tdm bus\n", __func__
);
1087 ret
= devm_request_irq(&pdev
->dev
, player
->irq
,
1088 uni_player_irq_handler
, IRQF_SHARED
,
1089 dev_name(&pdev
->dev
), player
);
1091 dev_err(player
->dev
, "unable to request IRQ %d\n", player
->irq
);
1095 mutex_init(&player
->ctrl_lock
);
1097 /* Ensure that disabled by default */
1098 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player
);
1099 SET_UNIPERIF_CTRL_ROUNDING_OFF(player
);
1100 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player
);
1101 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player
);
1103 if (UNIPERIF_TYPE_IS_IEC958(player
)) {
1104 /* Set default iec958 status bits */
1106 /* Consumer, PCM, copyright, 2ch, mode 0 */
1107 player
->stream_settings
.iec958
.status
[0] = 0x00;
1108 /* Broadcast reception category */
1109 player
->stream_settings
.iec958
.status
[1] =
1110 IEC958_AES1_CON_GENERAL
;
1111 /* Do not take into account source or channel number */
1112 player
->stream_settings
.iec958
.status
[2] =
1113 IEC958_AES2_CON_SOURCE_UNSPEC
;
1114 /* Sampling frequency not indicated */
1115 player
->stream_settings
.iec958
.status
[3] =
1116 IEC958_AES3_CON_FS_NOTID
;
1117 /* Max sample word 24-bit, sample word length not indicated */
1118 player
->stream_settings
.iec958
.status
[4] =
1119 IEC958_AES4_CON_MAX_WORDLEN_24
|
1120 IEC958_AES4_CON_WORDLEN_24_20
;
1122 player
->num_ctrls
= ARRAY_SIZE(snd_sti_iec_ctl
);
1123 player
->snd_ctrls
= snd_sti_iec_ctl
[0];
1125 player
->num_ctrls
= ARRAY_SIZE(snd_sti_pcm_ctl
);
1126 player
->snd_ctrls
= snd_sti_pcm_ctl
[0];
1131 EXPORT_SYMBOL_GPL(uni_player_init
);