md/raid: only permit hot-add of compatible integrity profiles
[linux/fpc-iii.git] / sound / soc / sti / uniperif_player.c
blob5c2bc53f0a9b721fd6a2f0b285c4d782d76d5bd6
1 /*
2 * Copyright (C) STMicroelectronics SA 2015
3 * Authors: Arnaud Pouliquen <arnaud.pouliquen@st.com>
4 * for STMicroelectronics.
5 * License terms: GNU General Public License (GPL), version 2
6 */
8 #include <linux/clk.h>
9 #include <linux/delay.h>
10 #include <linux/io.h>
11 #include <linux/mfd/syscon.h>
13 #include <sound/asoundef.h>
14 #include <sound/soc.h>
16 #include "uniperif.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))
39 #define UNIPERIF_PLAYER_CLK_ADJ_MIN -999999
40 #define UNIPERIF_PLAYER_CLK_ADJ_MAX 1000000
43 * Note: snd_pcm_hardware is linked to DMA controller but is declared here to
44 * integrate DAI_CPU capability in term of rate and supported channels
46 static const struct snd_pcm_hardware uni_player_pcm_hw = {
47 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
48 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP |
49 SNDRV_PCM_INFO_MMAP_VALID,
50 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE,
52 .rates = SNDRV_PCM_RATE_CONTINUOUS,
53 .rate_min = 8000,
54 .rate_max = 192000,
56 .channels_min = 2,
57 .channels_max = 8,
59 .periods_min = 2,
60 .periods_max = 48,
62 .period_bytes_min = 128,
63 .period_bytes_max = 64 * PAGE_SIZE,
64 .buffer_bytes_max = 256 * PAGE_SIZE
67 static inline int reset_player(struct uniperif *player)
69 int count = 10;
71 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
72 while (GET_UNIPERIF_SOFT_RST_SOFT_RST(player) && count) {
73 udelay(5);
74 count--;
78 if (!count) {
79 dev_err(player->dev, "Failed to reset uniperif");
80 return -EIO;
83 return 0;
87 * uni_player_irq_handler
88 * In case of error audio stream is stopped; stop action is protected via PCM
89 * stream lock to avoid race condition with trigger callback.
91 static irqreturn_t uni_player_irq_handler(int irq, void *dev_id)
93 irqreturn_t ret = IRQ_NONE;
94 struct uniperif *player = dev_id;
95 unsigned int status;
96 unsigned int tmp;
98 if (player->state == UNIPERIF_STATE_STOPPED) {
99 /* Unexpected IRQ: do nothing */
100 return IRQ_NONE;
103 /* Get interrupt status & clear them immediately */
104 status = GET_UNIPERIF_ITS(player);
105 SET_UNIPERIF_ITS_BCLR(player, status);
107 /* Check for fifo error (underrun) */
108 if (unlikely(status & UNIPERIF_ITS_FIFO_ERROR_MASK(player))) {
109 dev_err(player->dev, "FIFO underflow error detected");
111 /* Interrupt is just for information when underflow recovery */
112 if (player->info->underflow_enabled) {
113 /* Update state to underflow */
114 player->state = UNIPERIF_STATE_UNDERFLOW;
116 } else {
117 /* Disable interrupt so doesn't continually fire */
118 SET_UNIPERIF_ITM_BCLR_FIFO_ERROR(player);
120 /* Stop the player */
121 snd_pcm_stream_lock(player->substream);
122 snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN);
123 snd_pcm_stream_unlock(player->substream);
126 ret = IRQ_HANDLED;
129 /* Check for dma error (overrun) */
130 if (unlikely(status & UNIPERIF_ITS_DMA_ERROR_MASK(player))) {
131 dev_err(player->dev, "DMA error detected");
133 /* Disable interrupt so doesn't continually fire */
134 SET_UNIPERIF_ITM_BCLR_DMA_ERROR(player);
136 /* Stop the player */
137 snd_pcm_stream_lock(player->substream);
138 snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN);
139 snd_pcm_stream_unlock(player->substream);
141 ret = IRQ_HANDLED;
144 /* Check for underflow recovery done */
145 if (unlikely(status & UNIPERIF_ITM_UNDERFLOW_REC_DONE_MASK(player))) {
146 if (!player->info->underflow_enabled) {
147 dev_err(player->dev, "unexpected Underflow recovering");
148 return -EPERM;
150 /* Read the underflow recovery duration */
151 tmp = GET_UNIPERIF_STATUS_1_UNDERFLOW_DURATION(player);
153 /* Clear the underflow recovery duration */
154 SET_UNIPERIF_BIT_CONTROL_CLR_UNDERFLOW_DURATION(player);
156 /* Update state to started */
157 player->state = UNIPERIF_STATE_STARTED;
159 ret = IRQ_HANDLED;
162 /* Check if underflow recovery failed */
163 if (unlikely(status &
164 UNIPERIF_ITM_UNDERFLOW_REC_FAILED_MASK(player))) {
165 dev_err(player->dev, "Underflow recovery failed");
167 /* Stop the player */
168 snd_pcm_stream_lock(player->substream);
169 snd_pcm_stop(player->substream, SNDRV_PCM_STATE_XRUN);
170 snd_pcm_stream_unlock(player->substream);
172 ret = IRQ_HANDLED;
175 return ret;
178 static int uni_player_clk_set_rate(struct uniperif *player, unsigned long rate)
180 int rate_adjusted, rate_achieved, delta, ret;
181 int adjustment = player->clk_adj;
185 * F = f + --------- * f = f + d
186 * 1000000
189 * d = --------- * f
190 * 1000000
192 * where:
193 * f - nominal rate
194 * a - adjustment in ppm (parts per milion)
195 * F - rate to be set in synthesizer
196 * d - delta (difference) between f and F
198 if (adjustment < 0) {
199 /* div64_64 operates on unsigned values... */
200 delta = -1;
201 adjustment = -adjustment;
202 } else {
203 delta = 1;
205 /* 500000 ppm is 0.5, which is used to round up values */
206 delta *= (int)div64_u64((uint64_t)rate *
207 (uint64_t)adjustment + 500000, 1000000);
208 rate_adjusted = rate + delta;
210 /* Adjusted rate should never be == 0 */
211 if (!rate_adjusted)
212 return -EINVAL;
214 ret = clk_set_rate(player->clk, rate_adjusted);
215 if (ret < 0)
216 return ret;
218 rate_achieved = clk_get_rate(player->clk);
219 if (!rate_achieved)
220 /* If value is 0 means that clock or parent not valid */
221 return -EINVAL;
224 * Using ALSA's adjustment control, we can modify the rate to be up
225 * to twice as much as requested, but no more
227 delta = rate_achieved - rate;
228 if (delta < 0) {
229 /* div64_64 operates on unsigned values... */
230 delta = -delta;
231 adjustment = -1;
232 } else {
233 adjustment = 1;
235 /* Frequency/2 is added to round up result */
236 adjustment *= (int)div64_u64((uint64_t)delta * 1000000 + rate / 2,
237 rate);
238 player->clk_adj = adjustment;
239 return 0;
242 static void uni_player_set_channel_status(struct uniperif *player,
243 struct snd_pcm_runtime *runtime)
245 int n;
246 unsigned int status;
249 * Some AVRs and TVs require the channel status to contain a correct
250 * sampling frequency. If no sample rate is already specified, then
251 * set one.
253 mutex_lock(&player->ctrl_lock);
254 if (runtime && (player->stream_settings.iec958.status[3]
255 == IEC958_AES3_CON_FS_NOTID)) {
256 switch (runtime->rate) {
257 case 22050:
258 player->stream_settings.iec958.status[3] =
259 IEC958_AES3_CON_FS_22050;
260 break;
261 case 44100:
262 player->stream_settings.iec958.status[3] =
263 IEC958_AES3_CON_FS_44100;
264 break;
265 case 88200:
266 player->stream_settings.iec958.status[3] =
267 IEC958_AES3_CON_FS_88200;
268 break;
269 case 176400:
270 player->stream_settings.iec958.status[3] =
271 IEC958_AES3_CON_FS_176400;
272 break;
273 case 24000:
274 player->stream_settings.iec958.status[3] =
275 IEC958_AES3_CON_FS_24000;
276 break;
277 case 48000:
278 player->stream_settings.iec958.status[3] =
279 IEC958_AES3_CON_FS_48000;
280 break;
281 case 96000:
282 player->stream_settings.iec958.status[3] =
283 IEC958_AES3_CON_FS_96000;
284 break;
285 case 192000:
286 player->stream_settings.iec958.status[3] =
287 IEC958_AES3_CON_FS_192000;
288 break;
289 case 32000:
290 player->stream_settings.iec958.status[3] =
291 IEC958_AES3_CON_FS_32000;
292 break;
293 default:
294 /* Mark as sampling frequency not indicated */
295 player->stream_settings.iec958.status[3] =
296 IEC958_AES3_CON_FS_NOTID;
297 break;
301 /* Audio mode:
302 * Use audio mode status to select PCM or encoded mode
304 if (player->stream_settings.iec958.status[0] & IEC958_AES0_NONAUDIO)
305 player->stream_settings.encoding_mode =
306 UNIPERIF_IEC958_ENCODING_MODE_ENCODED;
307 else
308 player->stream_settings.encoding_mode =
309 UNIPERIF_IEC958_ENCODING_MODE_PCM;
311 if (player->stream_settings.encoding_mode ==
312 UNIPERIF_IEC958_ENCODING_MODE_PCM)
313 /* Clear user validity bits */
314 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0);
315 else
316 /* Set user validity bits */
317 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 1);
319 /* Program the new channel status */
320 for (n = 0; n < 6; ++n) {
321 status =
322 player->stream_settings.iec958.status[0 + (n * 4)] & 0xf;
323 status |=
324 player->stream_settings.iec958.status[1 + (n * 4)] << 8;
325 status |=
326 player->stream_settings.iec958.status[2 + (n * 4)] << 16;
327 status |=
328 player->stream_settings.iec958.status[3 + (n * 4)] << 24;
329 SET_UNIPERIF_CHANNEL_STA_REGN(player, n, status);
331 mutex_unlock(&player->ctrl_lock);
333 /* Update the channel status */
334 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
335 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player);
336 else
337 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player);
340 static int uni_player_prepare_iec958(struct uniperif *player,
341 struct snd_pcm_runtime *runtime)
343 int clk_div;
345 clk_div = player->mclk / runtime->rate;
347 /* Oversampling must be multiple of 128 as iec958 frame is 32-bits */
348 if ((clk_div % 128) || (clk_div <= 0)) {
349 dev_err(player->dev, "%s: invalid clk_div %d",
350 __func__, clk_div);
351 return -EINVAL;
354 switch (runtime->format) {
355 case SNDRV_PCM_FORMAT_S16_LE:
356 /* 16/16 memory format */
357 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player);
358 /* 16-bits per sub-frame */
359 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
360 /* Set 16-bit sample precision */
361 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player);
362 break;
363 case SNDRV_PCM_FORMAT_S32_LE:
364 /* 16/0 memory format */
365 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
366 /* 32-bits per sub-frame */
367 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
368 /* Set 24-bit sample precision */
369 SET_UNIPERIF_I2S_FMT_DATA_SIZE_24(player);
370 break;
371 default:
372 dev_err(player->dev, "format not supported");
373 return -EINVAL;
376 /* Set parity to be calculated by the hardware */
377 SET_UNIPERIF_CONFIG_PARITY_CNTR_BY_HW(player);
379 /* Set channel status bits to be inserted by the hardware */
380 SET_UNIPERIF_CONFIG_CHANNEL_STA_CNTR_BY_HW(player);
382 /* Set user data bits to be inserted by the hardware */
383 SET_UNIPERIF_CONFIG_USER_DAT_CNTR_BY_HW(player);
385 /* Set validity bits to be inserted by the hardware */
386 SET_UNIPERIF_CONFIG_VALIDITY_DAT_CNTR_BY_HW(player);
388 /* Set full software control to disabled */
389 SET_UNIPERIF_CONFIG_SPDIF_SW_CTRL_DISABLE(player);
391 SET_UNIPERIF_CTRL_ZERO_STUFF_HW(player);
393 /* Update the channel status */
394 uni_player_set_channel_status(player, runtime);
396 /* Clear the user validity user bits */
397 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0);
399 /* Disable one-bit audio mode */
400 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player);
402 /* Enable consecutive frames repetition of Z preamble (not for HBRA) */
403 SET_UNIPERIF_CONFIG_REPEAT_CHL_STS_ENABLE(player);
405 /* Change to SUF0_SUBF1 and left/right channels swap! */
406 SET_UNIPERIF_CONFIG_SUBFRAME_SEL_SUBF1_SUBF0(player);
408 /* Set data output as MSB first */
409 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
411 if (player->stream_settings.encoding_mode ==
412 UNIPERIF_IEC958_ENCODING_MODE_ENCODED)
413 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_ON(player);
414 else
415 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_OFF(player);
417 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2);
419 /* Set rounding to off */
420 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
422 /* Set clock divisor */
423 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / 128);
425 /* Set the spdif latency to not wait before starting player */
426 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
429 * Ensure iec958 formatting is off. It will be enabled in function
430 * uni_player_start() at the same time as the operation
431 * mode is set to work around a silicon issue.
433 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
434 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player);
435 else
436 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player);
438 return 0;
441 static int uni_player_prepare_pcm(struct uniperif *player,
442 struct snd_pcm_runtime *runtime)
444 int output_frame_size, slot_width, clk_div;
446 /* Force slot width to 32 in I2S mode (HW constraint) */
447 if ((player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
448 SND_SOC_DAIFMT_I2S) {
449 slot_width = 32;
450 } else {
451 switch (runtime->format) {
452 case SNDRV_PCM_FORMAT_S16_LE:
453 slot_width = 16;
454 break;
455 default:
456 slot_width = 32;
457 break;
460 output_frame_size = slot_width * runtime->channels;
462 clk_div = player->mclk / runtime->rate;
464 * For 32 bits subframe clk_div must be a multiple of 128,
465 * for 16 bits must be a multiple of 64
467 if ((slot_width == 32) && (clk_div % 128)) {
468 dev_err(player->dev, "%s: invalid clk_div", __func__);
469 return -EINVAL;
472 if ((slot_width == 16) && (clk_div % 64)) {
473 dev_err(player->dev, "%s: invalid clk_div", __func__);
474 return -EINVAL;
478 * Number of bits per subframe (which is one channel sample)
479 * on output - Transfer 16 or 32 bits from FIFO
481 switch (slot_width) {
482 case 32:
483 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
484 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player);
485 break;
486 case 16:
487 SET_UNIPERIF_I2S_FMT_NBIT_16(player);
488 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player);
489 break;
490 default:
491 dev_err(player->dev, "subframe format not supported");
492 return -EINVAL;
495 /* Configure data memory format */
496 switch (runtime->format) {
497 case SNDRV_PCM_FORMAT_S16_LE:
498 /* One data word contains two samples */
499 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player);
500 break;
502 case SNDRV_PCM_FORMAT_S32_LE:
504 * Actually "16 bits/0 bits" means "32/28/24/20/18/16 bits
505 * on the left than zeros (if less than 32 bytes)"... ;-)
507 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
508 break;
510 default:
511 dev_err(player->dev, "format not supported");
512 return -EINVAL;
515 /* Set rounding to off */
516 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
518 /* Set clock divisor */
519 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / (2 * output_frame_size));
521 /* Number of channelsmust be even*/
522 if ((runtime->channels % 2) || (runtime->channels < 2) ||
523 (runtime->channels > 10)) {
524 dev_err(player->dev, "%s: invalid nb of channels", __func__);
525 return -EINVAL;
528 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2);
530 /* Set 1-bit audio format to disabled */
531 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player);
533 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
534 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
536 /* No iec958 formatting as outputting to DAC */
537 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player);
539 return 0;
543 * ALSA uniperipheral iec958 controls
545 static int uni_player_ctl_iec958_info(struct snd_kcontrol *kcontrol,
546 struct snd_ctl_elem_info *uinfo)
548 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
549 uinfo->count = 1;
551 return 0;
554 static int uni_player_ctl_iec958_get(struct snd_kcontrol *kcontrol,
555 struct snd_ctl_elem_value *ucontrol)
557 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
558 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
559 struct uniperif *player = priv->dai_data.uni;
560 struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958;
562 mutex_lock(&player->ctrl_lock);
563 ucontrol->value.iec958.status[0] = iec958->status[0];
564 ucontrol->value.iec958.status[1] = iec958->status[1];
565 ucontrol->value.iec958.status[2] = iec958->status[2];
566 ucontrol->value.iec958.status[3] = iec958->status[3];
567 mutex_unlock(&player->ctrl_lock);
568 return 0;
571 static int uni_player_ctl_iec958_put(struct snd_kcontrol *kcontrol,
572 struct snd_ctl_elem_value *ucontrol)
574 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
575 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
576 struct uniperif *player = priv->dai_data.uni;
577 struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958;
579 mutex_lock(&player->ctrl_lock);
580 iec958->status[0] = ucontrol->value.iec958.status[0];
581 iec958->status[1] = ucontrol->value.iec958.status[1];
582 iec958->status[2] = ucontrol->value.iec958.status[2];
583 iec958->status[3] = ucontrol->value.iec958.status[3];
584 mutex_unlock(&player->ctrl_lock);
586 uni_player_set_channel_status(player, NULL);
588 return 0;
591 static struct snd_kcontrol_new uni_player_iec958_ctl = {
592 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
593 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
594 .info = uni_player_ctl_iec958_info,
595 .get = uni_player_ctl_iec958_get,
596 .put = uni_player_ctl_iec958_put,
600 * uniperif rate adjustement control
602 static int snd_sti_clk_adjustment_info(struct snd_kcontrol *kcontrol,
603 struct snd_ctl_elem_info *uinfo)
605 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
606 uinfo->count = 1;
607 uinfo->value.integer.min = UNIPERIF_PLAYER_CLK_ADJ_MIN;
608 uinfo->value.integer.max = UNIPERIF_PLAYER_CLK_ADJ_MAX;
609 uinfo->value.integer.step = 1;
611 return 0;
614 static int snd_sti_clk_adjustment_get(struct snd_kcontrol *kcontrol,
615 struct snd_ctl_elem_value *ucontrol)
617 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
618 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
619 struct uniperif *player = priv->dai_data.uni;
621 mutex_lock(&player->ctrl_lock);
622 ucontrol->value.integer.value[0] = player->clk_adj;
623 mutex_unlock(&player->ctrl_lock);
625 return 0;
628 static int snd_sti_clk_adjustment_put(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;
634 int ret = 0;
636 if ((ucontrol->value.integer.value[0] < UNIPERIF_PLAYER_CLK_ADJ_MIN) ||
637 (ucontrol->value.integer.value[0] > UNIPERIF_PLAYER_CLK_ADJ_MAX))
638 return -EINVAL;
640 mutex_lock(&player->ctrl_lock);
641 player->clk_adj = ucontrol->value.integer.value[0];
643 if (player->mclk)
644 ret = uni_player_clk_set_rate(player, player->mclk);
645 mutex_unlock(&player->ctrl_lock);
647 return ret;
650 static struct snd_kcontrol_new uni_player_clk_adj_ctl = {
651 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
652 .name = "PCM Playback Oversampling Freq. Adjustment",
653 .info = snd_sti_clk_adjustment_info,
654 .get = snd_sti_clk_adjustment_get,
655 .put = snd_sti_clk_adjustment_put,
658 static struct snd_kcontrol_new *snd_sti_pcm_ctl[] = {
659 &uni_player_clk_adj_ctl,
662 static struct snd_kcontrol_new *snd_sti_iec_ctl[] = {
663 &uni_player_iec958_ctl,
664 &uni_player_clk_adj_ctl,
667 static int uni_player_startup(struct snd_pcm_substream *substream,
668 struct snd_soc_dai *dai)
670 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
671 struct uniperif *player = priv->dai_data.uni;
672 player->substream = substream;
674 player->clk_adj = 0;
676 return 0;
679 static int uni_player_set_sysclk(struct snd_soc_dai *dai, int clk_id,
680 unsigned int freq, int dir)
682 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
683 struct uniperif *player = priv->dai_data.uni;
684 int ret;
686 if (dir == SND_SOC_CLOCK_IN)
687 return 0;
689 if (clk_id != 0)
690 return -EINVAL;
692 mutex_lock(&player->ctrl_lock);
693 ret = uni_player_clk_set_rate(player, freq);
694 if (!ret)
695 player->mclk = freq;
696 mutex_unlock(&player->ctrl_lock);
698 return ret;
701 static int uni_player_prepare(struct snd_pcm_substream *substream,
702 struct snd_soc_dai *dai)
704 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
705 struct uniperif *player = priv->dai_data.uni;
706 struct snd_pcm_runtime *runtime = substream->runtime;
707 int transfer_size, trigger_limit;
708 int ret;
710 /* The player should be stopped */
711 if (player->state != UNIPERIF_STATE_STOPPED) {
712 dev_err(player->dev, "%s: invalid player state %d", __func__,
713 player->state);
714 return -EINVAL;
717 /* Calculate transfer size (in fifo cells and bytes) for frame count */
718 transfer_size = runtime->channels * UNIPERIF_FIFO_FRAMES;
720 /* Calculate number of empty cells available before asserting DREQ */
721 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
722 trigger_limit = UNIPERIF_FIFO_SIZE - transfer_size;
723 } else {
725 * Since SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
726 * FDMA_TRIGGER_LIMIT also controls when the state switches
727 * from OFF or STANDBY to AUDIO DATA.
729 trigger_limit = transfer_size;
732 /* Trigger limit must be an even number */
733 if ((!trigger_limit % 2) || (trigger_limit != 1 && transfer_size % 2) ||
734 (trigger_limit > UNIPERIF_CONFIG_DMA_TRIG_LIMIT_MASK(player))) {
735 dev_err(player->dev, "invalid trigger limit %d", trigger_limit);
736 return -EINVAL;
739 SET_UNIPERIF_CONFIG_DMA_TRIG_LIMIT(player, trigger_limit);
741 /* Uniperipheral setup depends on player type */
742 switch (player->info->player_type) {
743 case SND_ST_UNIPERIF_PLAYER_TYPE_HDMI:
744 ret = uni_player_prepare_iec958(player, runtime);
745 break;
746 case SND_ST_UNIPERIF_PLAYER_TYPE_PCM:
747 ret = uni_player_prepare_pcm(player, runtime);
748 break;
749 case SND_ST_UNIPERIF_PLAYER_TYPE_SPDIF:
750 ret = uni_player_prepare_iec958(player, runtime);
751 break;
752 default:
753 dev_err(player->dev, "invalid player type");
754 return -EINVAL;
757 if (ret)
758 return ret;
760 switch (player->daifmt & SND_SOC_DAIFMT_INV_MASK) {
761 case SND_SOC_DAIFMT_NB_NF:
762 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
763 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
764 break;
765 case SND_SOC_DAIFMT_NB_IF:
766 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
767 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
768 break;
769 case SND_SOC_DAIFMT_IB_NF:
770 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
771 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
772 break;
773 case SND_SOC_DAIFMT_IB_IF:
774 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
775 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
776 break;
779 switch (player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) {
780 case SND_SOC_DAIFMT_I2S:
781 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
782 SET_UNIPERIF_I2S_FMT_PADDING_I2S_MODE(player);
783 break;
784 case SND_SOC_DAIFMT_LEFT_J:
785 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
786 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
787 break;
788 case SND_SOC_DAIFMT_RIGHT_J:
789 SET_UNIPERIF_I2S_FMT_ALIGN_RIGHT(player);
790 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
791 break;
792 default:
793 dev_err(player->dev, "format not supported");
794 return -EINVAL;
797 SET_UNIPERIF_I2S_FMT_NO_OF_SAMPLES_TO_READ(player, 0);
799 /* Reset uniperipheral player */
800 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
802 return reset_player(player);
805 static int uni_player_start(struct uniperif *player)
807 int ret;
809 /* The player should be stopped */
810 if (player->state != UNIPERIF_STATE_STOPPED) {
811 dev_err(player->dev, "%s: invalid player state", __func__);
812 return -EINVAL;
815 ret = clk_prepare_enable(player->clk);
816 if (ret) {
817 dev_err(player->dev, "%s: Failed to enable clock", __func__);
818 return ret;
821 /* Clear any pending interrupts */
822 SET_UNIPERIF_ITS_BCLR(player, GET_UNIPERIF_ITS(player));
824 /* Set the interrupt mask */
825 SET_UNIPERIF_ITM_BSET_DMA_ERROR(player);
826 SET_UNIPERIF_ITM_BSET_FIFO_ERROR(player);
828 /* Enable underflow recovery interrupts */
829 if (player->info->underflow_enabled) {
830 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_DONE(player);
831 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_FAILED(player);
834 /* Reset uniperipheral player */
835 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
837 ret = reset_player(player);
838 if (ret < 0)
839 return ret;
842 * Does not use IEC61937 features of the uniperipheral hardware.
843 * Instead it performs IEC61937 in software and inserts it directly
844 * into the audio data stream. As such, when encoded mode is selected,
845 * linear pcm mode is still used, but with the differences of the
846 * channel status bits set for encoded mode and the validity bits set.
848 SET_UNIPERIF_CTRL_OPERATION_PCM_DATA(player);
851 * If iec958 formatting is required for hdmi or spdif, then it must be
852 * enabled after the operation mode is set. If set prior to this, it
853 * will not take affect and hang the player.
855 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
856 if (UNIPERIF_PLAYER_TYPE_IS_IEC958(player))
857 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player);
859 /* Force channel status update (no update if clk disable) */
860 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
861 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player);
862 else
863 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player);
865 /* Update state to started */
866 player->state = UNIPERIF_STATE_STARTED;
868 return 0;
871 static int uni_player_stop(struct uniperif *player)
873 int ret;
875 /* The player should not be in stopped state */
876 if (player->state == UNIPERIF_STATE_STOPPED) {
877 dev_err(player->dev, "%s: invalid player state", __func__);
878 return -EINVAL;
881 /* Turn the player off */
882 SET_UNIPERIF_CTRL_OPERATION_OFF(player);
884 /* Soft reset the player */
885 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
887 ret = reset_player(player);
888 if (ret < 0)
889 return ret;
891 /* Disable interrupts */
892 SET_UNIPERIF_ITM_BCLR(player, GET_UNIPERIF_ITM(player));
894 /* Disable clock */
895 clk_disable_unprepare(player->clk);
897 /* Update state to stopped and return */
898 player->state = UNIPERIF_STATE_STOPPED;
900 return 0;
903 int uni_player_resume(struct uniperif *player)
905 int ret;
907 /* Select the frequency synthesizer clock */
908 if (player->clk_sel) {
909 ret = regmap_field_write(player->clk_sel, 1);
910 if (ret) {
911 dev_err(player->dev,
912 "%s: Failed to select freq synth clock",
913 __func__);
914 return ret;
918 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
919 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
920 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
921 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
923 return 0;
925 EXPORT_SYMBOL_GPL(uni_player_resume);
927 static int uni_player_trigger(struct snd_pcm_substream *substream,
928 int cmd, struct snd_soc_dai *dai)
930 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
931 struct uniperif *player = priv->dai_data.uni;
933 switch (cmd) {
934 case SNDRV_PCM_TRIGGER_START:
935 return uni_player_start(player);
936 case SNDRV_PCM_TRIGGER_STOP:
937 return uni_player_stop(player);
938 case SNDRV_PCM_TRIGGER_RESUME:
939 return uni_player_resume(player);
940 default:
941 return -EINVAL;
945 static void uni_player_shutdown(struct snd_pcm_substream *substream,
946 struct snd_soc_dai *dai)
948 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
949 struct uniperif *player = priv->dai_data.uni;
951 if (player->state != UNIPERIF_STATE_STOPPED)
952 /* Stop the player */
953 uni_player_stop(player);
955 player->substream = NULL;
958 static int uni_player_parse_dt_clk_glue(struct platform_device *pdev,
959 struct uniperif *player)
961 int bit_offset;
962 struct device_node *node = pdev->dev.of_node;
963 struct regmap *regmap;
965 bit_offset = SYS_CFG_AUDI0_GLUE_PCM_CLKX + player->info->id;
967 regmap = syscon_regmap_lookup_by_phandle(node, "st,syscfg");
969 if (regmap) {
970 struct reg_field regfield =
971 REG_FIELD(SYS_CFG_AUDIO_GLUE, bit_offset, bit_offset);
973 player->clk_sel = regmap_field_alloc(regmap, regfield);
974 } else {
975 dev_err(&pdev->dev, "sti-audio-clk-glue syscf not found\n");
976 return -EINVAL;
979 return 0;
982 static int uni_player_parse_dt(struct platform_device *pdev,
983 struct uniperif *player)
985 struct uniperif_info *info;
986 struct device *dev = &pdev->dev;
987 struct device_node *pnode = pdev->dev.of_node;
988 const char *mode;
990 /* Allocate memory for the info structure */
991 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
992 if (!info)
993 return -ENOMEM;
995 if (of_property_read_u32(pnode, "st,version", &player->ver) ||
996 player->ver == SND_ST_UNIPERIF_VERSION_UNKNOWN) {
997 dev_err(dev, "Unknown uniperipheral version ");
998 return -EINVAL;
1000 /* Underflow recovery is only supported on later ip revisions */
1001 if (player->ver >= SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
1002 info->underflow_enabled = 1;
1004 if (of_property_read_u32(pnode, "st,uniperiph-id", &info->id)) {
1005 dev_err(dev, "uniperipheral id not defined");
1006 return -EINVAL;
1009 /* Read the device mode property */
1010 if (of_property_read_string(pnode, "st,mode", &mode)) {
1011 dev_err(dev, "uniperipheral mode not defined");
1012 return -EINVAL;
1015 if (strcasecmp(mode, "hdmi") == 0)
1016 info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_HDMI;
1017 else if (strcasecmp(mode, "pcm") == 0)
1018 info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_PCM;
1019 else if (strcasecmp(mode, "spdif") == 0)
1020 info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_SPDIF;
1021 else
1022 info->player_type = SND_ST_UNIPERIF_PLAYER_TYPE_NONE;
1024 /* Save the info structure */
1025 player->info = info;
1027 /* Get the PCM_CLK_SEL bit from audio-glue-ctrl SoC register */
1028 if (uni_player_parse_dt_clk_glue(pdev, player))
1029 return -EINVAL;
1031 return 0;
1034 static const struct snd_soc_dai_ops uni_player_dai_ops = {
1035 .startup = uni_player_startup,
1036 .shutdown = uni_player_shutdown,
1037 .prepare = uni_player_prepare,
1038 .trigger = uni_player_trigger,
1039 .hw_params = sti_uniperiph_dai_hw_params,
1040 .set_fmt = sti_uniperiph_dai_set_fmt,
1041 .set_sysclk = uni_player_set_sysclk
1044 int uni_player_init(struct platform_device *pdev,
1045 struct uniperif *player)
1047 int ret = 0;
1049 player->dev = &pdev->dev;
1050 player->state = UNIPERIF_STATE_STOPPED;
1051 player->hw = &uni_player_pcm_hw;
1052 player->dai_ops = &uni_player_dai_ops;
1054 ret = uni_player_parse_dt(pdev, player);
1056 if (ret < 0) {
1057 dev_err(player->dev, "Failed to parse DeviceTree");
1058 return ret;
1061 /* Get uniperif resource */
1062 player->clk = of_clk_get(pdev->dev.of_node, 0);
1063 if (IS_ERR(player->clk))
1064 ret = PTR_ERR(player->clk);
1066 /* Select the frequency synthesizer clock */
1067 if (player->clk_sel) {
1068 ret = regmap_field_write(player->clk_sel, 1);
1069 if (ret) {
1070 dev_err(player->dev,
1071 "%s: Failed to select freq synth clock",
1072 __func__);
1073 return ret;
1077 ret = devm_request_irq(&pdev->dev, player->irq,
1078 uni_player_irq_handler, IRQF_SHARED,
1079 dev_name(&pdev->dev), player);
1080 if (ret < 0)
1081 return ret;
1083 mutex_init(&player->ctrl_lock);
1085 /* Ensure that disabled by default */
1086 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
1087 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
1088 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
1089 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
1091 if (UNIPERIF_PLAYER_TYPE_IS_IEC958(player)) {
1092 /* Set default iec958 status bits */
1094 /* Consumer, PCM, copyright, 2ch, mode 0 */
1095 player->stream_settings.iec958.status[0] = 0x00;
1096 /* Broadcast reception category */
1097 player->stream_settings.iec958.status[1] =
1098 IEC958_AES1_CON_GENERAL;
1099 /* Do not take into account source or channel number */
1100 player->stream_settings.iec958.status[2] =
1101 IEC958_AES2_CON_SOURCE_UNSPEC;
1102 /* Sampling frequency not indicated */
1103 player->stream_settings.iec958.status[3] =
1104 IEC958_AES3_CON_FS_NOTID;
1105 /* Max sample word 24-bit, sample word length not indicated */
1106 player->stream_settings.iec958.status[4] =
1107 IEC958_AES4_CON_MAX_WORDLEN_24 |
1108 IEC958_AES4_CON_WORDLEN_24_20;
1110 player->num_ctrls = ARRAY_SIZE(snd_sti_iec_ctl);
1111 player->snd_ctrls = snd_sti_iec_ctl[0];
1112 } else {
1113 player->num_ctrls = ARRAY_SIZE(snd_sti_pcm_ctl);
1114 player->snd_ctrls = snd_sti_pcm_ctl[0];
1117 return 0;
1119 EXPORT_SYMBOL_GPL(uni_player_init);