]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - sound/soc/sti/uniperif_player.c
ASoC: sti: helper functions to fix tdm runtime params
[mirror_ubuntu-zesty-kernel.git] / sound / soc / sti / uniperif_player.c
CommitLineData
76c2145d
AP
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 */
7
8#include <linux/clk.h>
9#include <linux/delay.h>
10#include <linux/io.h>
11#include <linux/mfd/syscon.h>
12
13#include <sound/asoundef.h>
14#include <sound/soc.h>
15
16#include "uniperif.h"
17
18/*
19 * Some hardware-related definitions
20 */
21
22/* sys config registers definitions */
23#define SYS_CFG_AUDIO_GLUE 0xA4
24#define SYS_CFG_AUDI0_GLUE_PCM_CLKX 8
25
26/*
27 * Driver specific types.
28 */
76c2145d 29
fa050796
AP
30#define UNIPERIF_PLAYER_CLK_ADJ_MIN -999999
31#define UNIPERIF_PLAYER_CLK_ADJ_MAX 1000000
32
76c2145d
AP
33/*
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
36 */
f2da4542 37static const struct snd_pcm_hardware uni_player_pcm_hw = {
76c2145d
AP
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,
42
43 .rates = SNDRV_PCM_RATE_CONTINUOUS,
44 .rate_min = 8000,
45 .rate_max = 192000,
46
47 .channels_min = 2,
48 .channels_max = 8,
49
50 .periods_min = 2,
51 .periods_max = 48,
52
53 .period_bytes_min = 128,
54 .period_bytes_max = 64 * PAGE_SIZE,
55 .buffer_bytes_max = 256 * PAGE_SIZE
56};
57
58static inline int reset_player(struct uniperif *player)
59{
60 int count = 10;
61
62 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
63 while (GET_UNIPERIF_SOFT_RST_SOFT_RST(player) && count) {
64 udelay(5);
65 count--;
66 }
67 }
68
69 if (!count) {
70 dev_err(player->dev, "Failed to reset uniperif");
71 return -EIO;
72 }
73
74 return 0;
75}
76
77/*
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.
81 */
82static irqreturn_t uni_player_irq_handler(int irq, void *dev_id)
83{
84 irqreturn_t ret = IRQ_NONE;
85 struct uniperif *player = dev_id;
86 unsigned int status;
87 unsigned int tmp;
88
89 if (player->state == UNIPERIF_STATE_STOPPED) {
90 /* Unexpected IRQ: do nothing */
91 return IRQ_NONE;
92 }
93
94 /* Get interrupt status & clear them immediately */
95 status = GET_UNIPERIF_ITS(player);
96 SET_UNIPERIF_ITS_BCLR(player, status);
97
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");
101
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;
106
107 } else {
108 /* Disable interrupt so doesn't continually fire */
109 SET_UNIPERIF_ITM_BCLR_FIFO_ERROR(player);
110
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);
115 }
116
117 ret = IRQ_HANDLED;
118 }
119
120 /* Check for dma error (overrun) */
121 if (unlikely(status & UNIPERIF_ITS_DMA_ERROR_MASK(player))) {
122 dev_err(player->dev, "DMA error detected");
123
124 /* Disable interrupt so doesn't continually fire */
125 SET_UNIPERIF_ITM_BCLR_DMA_ERROR(player);
126
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);
131
132 ret = IRQ_HANDLED;
133 }
134
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");
139 return -EPERM;
140 }
141 /* Read the underflow recovery duration */
142 tmp = GET_UNIPERIF_STATUS_1_UNDERFLOW_DURATION(player);
143
144 /* Clear the underflow recovery duration */
145 SET_UNIPERIF_BIT_CONTROL_CLR_UNDERFLOW_DURATION(player);
146
147 /* Update state to started */
148 player->state = UNIPERIF_STATE_STARTED;
149
150 ret = IRQ_HANDLED;
151 }
152
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");
157
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);
162
163 ret = IRQ_HANDLED;
164 }
165
166 return ret;
167}
168
f2da4542 169static int uni_player_clk_set_rate(struct uniperif *player, unsigned long rate)
fa050796
AP
170{
171 int rate_adjusted, rate_achieved, delta, ret;
172 int adjustment = player->clk_adj;
173
174 /*
175 * a
176 * F = f + --------- * f = f + d
177 * 1000000
178 *
179 * a
180 * d = --------- * f
181 * 1000000
182 *
183 * where:
184 * f - nominal rate
185 * a - adjustment in ppm (parts per milion)
186 * F - rate to be set in synthesizer
187 * d - delta (difference) between f and F
188 */
189 if (adjustment < 0) {
190 /* div64_64 operates on unsigned values... */
191 delta = -1;
192 adjustment = -adjustment;
193 } else {
194 delta = 1;
195 }
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;
200
201 /* Adjusted rate should never be == 0 */
202 if (!rate_adjusted)
203 return -EINVAL;
204
205 ret = clk_set_rate(player->clk, rate_adjusted);
206 if (ret < 0)
207 return ret;
208
209 rate_achieved = clk_get_rate(player->clk);
210 if (!rate_achieved)
211 /* If value is 0 means that clock or parent not valid */
212 return -EINVAL;
213
214 /*
215 * Using ALSA's adjustment control, we can modify the rate to be up
216 * to twice as much as requested, but no more
217 */
218 delta = rate_achieved - rate;
219 if (delta < 0) {
220 /* div64_64 operates on unsigned values... */
221 delta = -delta;
222 adjustment = -1;
223 } else {
224 adjustment = 1;
225 }
226 /* Frequency/2 is added to round up result */
227 adjustment *= (int)div64_u64((uint64_t)delta * 1000000 + rate / 2,
228 rate);
229 player->clk_adj = adjustment;
230 return 0;
231}
232
76c2145d
AP
233static void uni_player_set_channel_status(struct uniperif *player,
234 struct snd_pcm_runtime *runtime)
235{
236 int n;
237 unsigned int status;
238
239 /*
240 * Some AVRs and TVs require the channel status to contain a correct
241 * sampling frequency. If no sample rate is already specified, then
242 * set one.
243 */
36cc0935 244 mutex_lock(&player->ctrl_lock);
0d3f3c9a 245 if (runtime) {
76c2145d
AP
246 switch (runtime->rate) {
247 case 22050:
248 player->stream_settings.iec958.status[3] =
249 IEC958_AES3_CON_FS_22050;
250 break;
251 case 44100:
252 player->stream_settings.iec958.status[3] =
253 IEC958_AES3_CON_FS_44100;
254 break;
255 case 88200:
256 player->stream_settings.iec958.status[3] =
257 IEC958_AES3_CON_FS_88200;
258 break;
259 case 176400:
260 player->stream_settings.iec958.status[3] =
261 IEC958_AES3_CON_FS_176400;
262 break;
263 case 24000:
264 player->stream_settings.iec958.status[3] =
265 IEC958_AES3_CON_FS_24000;
266 break;
267 case 48000:
268 player->stream_settings.iec958.status[3] =
269 IEC958_AES3_CON_FS_48000;
270 break;
271 case 96000:
272 player->stream_settings.iec958.status[3] =
273 IEC958_AES3_CON_FS_96000;
274 break;
275 case 192000:
276 player->stream_settings.iec958.status[3] =
277 IEC958_AES3_CON_FS_192000;
278 break;
279 case 32000:
280 player->stream_settings.iec958.status[3] =
281 IEC958_AES3_CON_FS_32000;
282 break;
283 default:
284 /* Mark as sampling frequency not indicated */
285 player->stream_settings.iec958.status[3] =
286 IEC958_AES3_CON_FS_NOTID;
287 break;
288 }
289 }
290
291 /* Audio mode:
292 * Use audio mode status to select PCM or encoded mode
293 */
294 if (player->stream_settings.iec958.status[0] & IEC958_AES0_NONAUDIO)
295 player->stream_settings.encoding_mode =
296 UNIPERIF_IEC958_ENCODING_MODE_ENCODED;
297 else
298 player->stream_settings.encoding_mode =
299 UNIPERIF_IEC958_ENCODING_MODE_PCM;
300
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);
305 else
306 /* Set user validity bits */
307 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 1);
308
309 /* Program the new channel status */
310 for (n = 0; n < 6; ++n) {
311 status =
312 player->stream_settings.iec958.status[0 + (n * 4)] & 0xf;
313 status |=
314 player->stream_settings.iec958.status[1 + (n * 4)] << 8;
315 status |=
316 player->stream_settings.iec958.status[2 + (n * 4)] << 16;
317 status |=
318 player->stream_settings.iec958.status[3 + (n * 4)] << 24;
319 SET_UNIPERIF_CHANNEL_STA_REGN(player, n, status);
320 }
36cc0935 321 mutex_unlock(&player->ctrl_lock);
76c2145d
AP
322
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);
326 else
327 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player);
328}
329
330static int uni_player_prepare_iec958(struct uniperif *player,
331 struct snd_pcm_runtime *runtime)
332{
333 int clk_div;
334
335 clk_div = player->mclk / runtime->rate;
336
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",
340 __func__, clk_div);
341 return -EINVAL;
342 }
343
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);
352 break;
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);
360 break;
361 default:
362 dev_err(player->dev, "format not supported");
363 return -EINVAL;
364 }
365
366 /* Set parity to be calculated by the hardware */
367 SET_UNIPERIF_CONFIG_PARITY_CNTR_BY_HW(player);
368
369 /* Set channel status bits to be inserted by the hardware */
370 SET_UNIPERIF_CONFIG_CHANNEL_STA_CNTR_BY_HW(player);
371
372 /* Set user data bits to be inserted by the hardware */
373 SET_UNIPERIF_CONFIG_USER_DAT_CNTR_BY_HW(player);
374
375 /* Set validity bits to be inserted by the hardware */
376 SET_UNIPERIF_CONFIG_VALIDITY_DAT_CNTR_BY_HW(player);
377
378 /* Set full software control to disabled */
379 SET_UNIPERIF_CONFIG_SPDIF_SW_CTRL_DISABLE(player);
380
381 SET_UNIPERIF_CTRL_ZERO_STUFF_HW(player);
382
383 /* Update the channel status */
384 uni_player_set_channel_status(player, runtime);
385
386 /* Clear the user validity user bits */
387 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0);
388
389 /* Disable one-bit audio mode */
390 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player);
391
392 /* Enable consecutive frames repetition of Z preamble (not for HBRA) */
393 SET_UNIPERIF_CONFIG_REPEAT_CHL_STS_ENABLE(player);
394
395 /* Change to SUF0_SUBF1 and left/right channels swap! */
396 SET_UNIPERIF_CONFIG_SUBFRAME_SEL_SUBF1_SUBF0(player);
397
398 /* Set data output as MSB first */
399 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
400
401 if (player->stream_settings.encoding_mode ==
402 UNIPERIF_IEC958_ENCODING_MODE_ENCODED)
403 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_ON(player);
404 else
405 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_OFF(player);
406
407 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2);
408
409 /* Set rounding to off */
410 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
411
412 /* Set clock divisor */
413 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / 128);
414
415 /* Set the spdif latency to not wait before starting player */
416 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
417
418 /*
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.
422 */
423 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
424 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player);
425 else
426 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player);
427
428 return 0;
429}
430
431static int uni_player_prepare_pcm(struct uniperif *player,
432 struct snd_pcm_runtime *runtime)
433{
434 int output_frame_size, slot_width, clk_div;
435
436 /* Force slot width to 32 in I2S mode (HW constraint) */
437 if ((player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
438 SND_SOC_DAIFMT_I2S) {
439 slot_width = 32;
440 } else {
441 switch (runtime->format) {
442 case SNDRV_PCM_FORMAT_S16_LE:
443 slot_width = 16;
444 break;
445 default:
446 slot_width = 32;
447 break;
448 }
449 }
450 output_frame_size = slot_width * runtime->channels;
451
452 clk_div = player->mclk / runtime->rate;
453 /*
454 * For 32 bits subframe clk_div must be a multiple of 128,
455 * for 16 bits must be a multiple of 64
456 */
457 if ((slot_width == 32) && (clk_div % 128)) {
458 dev_err(player->dev, "%s: invalid clk_div", __func__);
459 return -EINVAL;
460 }
461
462 if ((slot_width == 16) && (clk_div % 64)) {
463 dev_err(player->dev, "%s: invalid clk_div", __func__);
464 return -EINVAL;
465 }
466
467 /*
468 * Number of bits per subframe (which is one channel sample)
469 * on output - Transfer 16 or 32 bits from FIFO
470 */
471 switch (slot_width) {
472 case 32:
473 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
474 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player);
475 break;
476 case 16:
477 SET_UNIPERIF_I2S_FMT_NBIT_16(player);
478 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player);
479 break;
480 default:
481 dev_err(player->dev, "subframe format not supported");
482 return -EINVAL;
483 }
484
485 /* Configure data memory format */
486 switch (runtime->format) {
487 case SNDRV_PCM_FORMAT_S16_LE:
488 /* One data word contains two samples */
489 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player);
490 break;
491
492 case SNDRV_PCM_FORMAT_S32_LE:
493 /*
494 * Actually "16 bits/0 bits" means "32/28/24/20/18/16 bits
495 * on the left than zeros (if less than 32 bytes)"... ;-)
496 */
497 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
498 break;
499
500 default:
501 dev_err(player->dev, "format not supported");
502 return -EINVAL;
503 }
504
505 /* Set rounding to off */
506 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
507
508 /* Set clock divisor */
509 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / (2 * output_frame_size));
510
511 /* Number of channelsmust be even*/
512 if ((runtime->channels % 2) || (runtime->channels < 2) ||
513 (runtime->channels > 10)) {
514 dev_err(player->dev, "%s: invalid nb of channels", __func__);
515 return -EINVAL;
516 }
517
518 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2);
519
520 /* Set 1-bit audio format to disabled */
521 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player);
522
523 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
524 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
525
526 /* No iec958 formatting as outputting to DAC */
527 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player);
528
529 return 0;
530}
531
36cc0935
AP
532/*
533 * ALSA uniperipheral iec958 controls
534 */
535static int uni_player_ctl_iec958_info(struct snd_kcontrol *kcontrol,
536 struct snd_ctl_elem_info *uinfo)
537{
538 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
539 uinfo->count = 1;
540
541 return 0;
542}
543
544static int uni_player_ctl_iec958_get(struct snd_kcontrol *kcontrol,
545 struct snd_ctl_elem_value *ucontrol)
546{
547 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
548 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
549 struct uniperif *player = priv->dai_data.uni;
550 struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958;
551
552 mutex_lock(&player->ctrl_lock);
553 ucontrol->value.iec958.status[0] = iec958->status[0];
554 ucontrol->value.iec958.status[1] = iec958->status[1];
555 ucontrol->value.iec958.status[2] = iec958->status[2];
556 ucontrol->value.iec958.status[3] = iec958->status[3];
557 mutex_unlock(&player->ctrl_lock);
558 return 0;
559}
560
561static int uni_player_ctl_iec958_put(struct snd_kcontrol *kcontrol,
562 struct snd_ctl_elem_value *ucontrol)
563{
564 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
565 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
566 struct uniperif *player = priv->dai_data.uni;
567 struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958;
568
569 mutex_lock(&player->ctrl_lock);
570 iec958->status[0] = ucontrol->value.iec958.status[0];
571 iec958->status[1] = ucontrol->value.iec958.status[1];
572 iec958->status[2] = ucontrol->value.iec958.status[2];
573 iec958->status[3] = ucontrol->value.iec958.status[3];
574 mutex_unlock(&player->ctrl_lock);
575
576 uni_player_set_channel_status(player, NULL);
577
578 return 0;
579}
580
581static struct snd_kcontrol_new uni_player_iec958_ctl = {
582 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
583 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
584 .info = uni_player_ctl_iec958_info,
585 .get = uni_player_ctl_iec958_get,
586 .put = uni_player_ctl_iec958_put,
587};
588
fa050796
AP
589/*
590 * uniperif rate adjustement control
591 */
592static int snd_sti_clk_adjustment_info(struct snd_kcontrol *kcontrol,
593 struct snd_ctl_elem_info *uinfo)
594{
595 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
596 uinfo->count = 1;
597 uinfo->value.integer.min = UNIPERIF_PLAYER_CLK_ADJ_MIN;
598 uinfo->value.integer.max = UNIPERIF_PLAYER_CLK_ADJ_MAX;
599 uinfo->value.integer.step = 1;
600
601 return 0;
602}
603
604static int snd_sti_clk_adjustment_get(struct snd_kcontrol *kcontrol,
605 struct snd_ctl_elem_value *ucontrol)
606{
607 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
608 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
609 struct uniperif *player = priv->dai_data.uni;
610
36cc0935 611 mutex_lock(&player->ctrl_lock);
fa050796 612 ucontrol->value.integer.value[0] = player->clk_adj;
36cc0935 613 mutex_unlock(&player->ctrl_lock);
fa050796
AP
614
615 return 0;
616}
617
618static int snd_sti_clk_adjustment_put(struct snd_kcontrol *kcontrol,
619 struct snd_ctl_elem_value *ucontrol)
620{
621 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
622 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
623 struct uniperif *player = priv->dai_data.uni;
624 int ret = 0;
625
626 if ((ucontrol->value.integer.value[0] < UNIPERIF_PLAYER_CLK_ADJ_MIN) ||
627 (ucontrol->value.integer.value[0] > UNIPERIF_PLAYER_CLK_ADJ_MAX))
628 return -EINVAL;
629
630 mutex_lock(&player->ctrl_lock);
631 player->clk_adj = ucontrol->value.integer.value[0];
632
633 if (player->mclk)
634 ret = uni_player_clk_set_rate(player, player->mclk);
635 mutex_unlock(&player->ctrl_lock);
636
637 return ret;
638}
639
640static struct snd_kcontrol_new uni_player_clk_adj_ctl = {
641 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
642 .name = "PCM Playback Oversampling Freq. Adjustment",
643 .info = snd_sti_clk_adjustment_info,
644 .get = snd_sti_clk_adjustment_get,
645 .put = snd_sti_clk_adjustment_put,
646};
647
36cc0935
AP
648static struct snd_kcontrol_new *snd_sti_pcm_ctl[] = {
649 &uni_player_clk_adj_ctl,
650};
651
652static struct snd_kcontrol_new *snd_sti_iec_ctl[] = {
653 &uni_player_iec958_ctl,
fa050796
AP
654 &uni_player_clk_adj_ctl,
655};
656
657static int uni_player_startup(struct snd_pcm_substream *substream,
658 struct snd_soc_dai *dai)
659{
660 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
661 struct uniperif *player = priv->dai_data.uni;
36a65e20 662 player->substream = substream;
fa050796
AP
663
664 player->clk_adj = 0;
665
666 return 0;
667}
668
76c2145d
AP
669static int uni_player_set_sysclk(struct snd_soc_dai *dai, int clk_id,
670 unsigned int freq, int dir)
671{
672 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
673 struct uniperif *player = priv->dai_data.uni;
ed6c75f2 674 int ret;
76c2145d
AP
675
676 if (dir == SND_SOC_CLOCK_IN)
677 return 0;
678
679 if (clk_id != 0)
680 return -EINVAL;
681
fa050796
AP
682 mutex_lock(&player->ctrl_lock);
683 ret = uni_player_clk_set_rate(player, freq);
ed6c75f2
AP
684 if (!ret)
685 player->mclk = freq;
fa050796 686 mutex_unlock(&player->ctrl_lock);
76c2145d 687
ed6c75f2 688 return ret;
76c2145d
AP
689}
690
691static int uni_player_prepare(struct snd_pcm_substream *substream,
692 struct snd_soc_dai *dai)
693{
694 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
695 struct uniperif *player = priv->dai_data.uni;
696 struct snd_pcm_runtime *runtime = substream->runtime;
697 int transfer_size, trigger_limit;
698 int ret;
699
700 /* The player should be stopped */
701 if (player->state != UNIPERIF_STATE_STOPPED) {
702 dev_err(player->dev, "%s: invalid player state %d", __func__,
703 player->state);
704 return -EINVAL;
705 }
706
707 /* Calculate transfer size (in fifo cells and bytes) for frame count */
708 transfer_size = runtime->channels * UNIPERIF_FIFO_FRAMES;
709
710 /* Calculate number of empty cells available before asserting DREQ */
711 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
712 trigger_limit = UNIPERIF_FIFO_SIZE - transfer_size;
713 } else {
714 /*
715 * Since SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
716 * FDMA_TRIGGER_LIMIT also controls when the state switches
717 * from OFF or STANDBY to AUDIO DATA.
718 */
719 trigger_limit = transfer_size;
720 }
721
722 /* Trigger limit must be an even number */
723 if ((!trigger_limit % 2) || (trigger_limit != 1 && transfer_size % 2) ||
724 (trigger_limit > UNIPERIF_CONFIG_DMA_TRIG_LIMIT_MASK(player))) {
725 dev_err(player->dev, "invalid trigger limit %d", trigger_limit);
726 return -EINVAL;
727 }
728
729 SET_UNIPERIF_CONFIG_DMA_TRIG_LIMIT(player, trigger_limit);
730
731 /* Uniperipheral setup depends on player type */
5295a0dc
MG
732 switch (player->info->type) {
733 case SND_ST_UNIPERIF_TYPE_HDMI:
76c2145d
AP
734 ret = uni_player_prepare_iec958(player, runtime);
735 break;
5295a0dc 736 case SND_ST_UNIPERIF_TYPE_PCM:
76c2145d
AP
737 ret = uni_player_prepare_pcm(player, runtime);
738 break;
5295a0dc 739 case SND_ST_UNIPERIF_TYPE_SPDIF:
76c2145d
AP
740 ret = uni_player_prepare_iec958(player, runtime);
741 break;
742 default:
743 dev_err(player->dev, "invalid player type");
744 return -EINVAL;
745 }
746
747 if (ret)
748 return ret;
749
750 switch (player->daifmt & SND_SOC_DAIFMT_INV_MASK) {
751 case SND_SOC_DAIFMT_NB_NF:
752 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
753 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
754 break;
755 case SND_SOC_DAIFMT_NB_IF:
756 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
757 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
758 break;
759 case SND_SOC_DAIFMT_IB_NF:
760 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
761 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
762 break;
763 case SND_SOC_DAIFMT_IB_IF:
764 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
765 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
ed6c75f2 766 break;
76c2145d
AP
767 }
768
769 switch (player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) {
770 case SND_SOC_DAIFMT_I2S:
771 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
772 SET_UNIPERIF_I2S_FMT_PADDING_I2S_MODE(player);
773 break;
774 case SND_SOC_DAIFMT_LEFT_J:
775 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
776 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
777 break;
778 case SND_SOC_DAIFMT_RIGHT_J:
779 SET_UNIPERIF_I2S_FMT_ALIGN_RIGHT(player);
780 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
781 break;
782 default:
783 dev_err(player->dev, "format not supported");
784 return -EINVAL;
785 }
786
787 SET_UNIPERIF_I2S_FMT_NO_OF_SAMPLES_TO_READ(player, 0);
788
789 /* Reset uniperipheral player */
790 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
791
792 return reset_player(player);
793}
794
795static int uni_player_start(struct uniperif *player)
796{
797 int ret;
798
799 /* The player should be stopped */
800 if (player->state != UNIPERIF_STATE_STOPPED) {
801 dev_err(player->dev, "%s: invalid player state", __func__);
802 return -EINVAL;
803 }
804
805 ret = clk_prepare_enable(player->clk);
806 if (ret) {
807 dev_err(player->dev, "%s: Failed to enable clock", __func__);
808 return ret;
809 }
810
811 /* Clear any pending interrupts */
812 SET_UNIPERIF_ITS_BCLR(player, GET_UNIPERIF_ITS(player));
813
814 /* Set the interrupt mask */
815 SET_UNIPERIF_ITM_BSET_DMA_ERROR(player);
816 SET_UNIPERIF_ITM_BSET_FIFO_ERROR(player);
817
818 /* Enable underflow recovery interrupts */
819 if (player->info->underflow_enabled) {
820 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_DONE(player);
821 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_FAILED(player);
822 }
823
824 /* Reset uniperipheral player */
825 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
826
827 ret = reset_player(player);
828 if (ret < 0)
829 return ret;
830
831 /*
832 * Does not use IEC61937 features of the uniperipheral hardware.
833 * Instead it performs IEC61937 in software and inserts it directly
834 * into the audio data stream. As such, when encoded mode is selected,
835 * linear pcm mode is still used, but with the differences of the
836 * channel status bits set for encoded mode and the validity bits set.
837 */
838 SET_UNIPERIF_CTRL_OPERATION_PCM_DATA(player);
839
840 /*
841 * If iec958 formatting is required for hdmi or spdif, then it must be
842 * enabled after the operation mode is set. If set prior to this, it
843 * will not take affect and hang the player.
844 */
845 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
5295a0dc
MG
846 if (UNIPERIF_TYPE_IS_IEC958(player))
847 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player);
76c2145d
AP
848
849 /* Force channel status update (no update if clk disable) */
850 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
851 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player);
852 else
853 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player);
854
855 /* Update state to started */
856 player->state = UNIPERIF_STATE_STARTED;
857
858 return 0;
859}
860
861static int uni_player_stop(struct uniperif *player)
862{
863 int ret;
864
865 /* The player should not be in stopped state */
866 if (player->state == UNIPERIF_STATE_STOPPED) {
867 dev_err(player->dev, "%s: invalid player state", __func__);
868 return -EINVAL;
869 }
870
871 /* Turn the player off */
872 SET_UNIPERIF_CTRL_OPERATION_OFF(player);
873
874 /* Soft reset the player */
875 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
876
877 ret = reset_player(player);
878 if (ret < 0)
879 return ret;
880
881 /* Disable interrupts */
882 SET_UNIPERIF_ITM_BCLR(player, GET_UNIPERIF_ITM(player));
883
884 /* Disable clock */
885 clk_disable_unprepare(player->clk);
886
887 /* Update state to stopped and return */
888 player->state = UNIPERIF_STATE_STOPPED;
889
890 return 0;
891}
892
893int uni_player_resume(struct uniperif *player)
894{
895 int ret;
896
897 /* Select the frequency synthesizer clock */
898 if (player->clk_sel) {
899 ret = regmap_field_write(player->clk_sel, 1);
900 if (ret) {
901 dev_err(player->dev,
902 "%s: Failed to select freq synth clock",
903 __func__);
904 return ret;
905 }
906 }
907
908 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
909 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
910 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
911 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
912
913 return 0;
914}
915EXPORT_SYMBOL_GPL(uni_player_resume);
916
917static int uni_player_trigger(struct snd_pcm_substream *substream,
918 int cmd, struct snd_soc_dai *dai)
919{
920 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
921 struct uniperif *player = priv->dai_data.uni;
922
923 switch (cmd) {
924 case SNDRV_PCM_TRIGGER_START:
925 return uni_player_start(player);
926 case SNDRV_PCM_TRIGGER_STOP:
927 return uni_player_stop(player);
928 case SNDRV_PCM_TRIGGER_RESUME:
929 return uni_player_resume(player);
930 default:
931 return -EINVAL;
932 }
933}
934
935static void uni_player_shutdown(struct snd_pcm_substream *substream,
936 struct snd_soc_dai *dai)
937{
938 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
939 struct uniperif *player = priv->dai_data.uni;
940
941 if (player->state != UNIPERIF_STATE_STOPPED)
942 /* Stop the player */
943 uni_player_stop(player);
36a65e20
MG
944
945 player->substream = NULL;
76c2145d
AP
946}
947
948static int uni_player_parse_dt_clk_glue(struct platform_device *pdev,
949 struct uniperif *player)
950{
951 int bit_offset;
952 struct device_node *node = pdev->dev.of_node;
953 struct regmap *regmap;
954
955 bit_offset = SYS_CFG_AUDI0_GLUE_PCM_CLKX + player->info->id;
956
957 regmap = syscon_regmap_lookup_by_phandle(node, "st,syscfg");
958
959 if (regmap) {
960 struct reg_field regfield =
961 REG_FIELD(SYS_CFG_AUDIO_GLUE, bit_offset, bit_offset);
962
963 player->clk_sel = regmap_field_alloc(regmap, regfield);
964 } else {
965 dev_err(&pdev->dev, "sti-audio-clk-glue syscf not found\n");
966 return -EINVAL;
967 }
968
969 return 0;
970}
971
972static int uni_player_parse_dt(struct platform_device *pdev,
973 struct uniperif *player)
974{
975 struct uniperif_info *info;
976 struct device *dev = &pdev->dev;
977 struct device_node *pnode = pdev->dev.of_node;
978 const char *mode;
979
980 /* Allocate memory for the info structure */
981 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
982 if (!info)
983 return -ENOMEM;
984
f9f51973 985 if (of_property_read_u32(pnode, "st,version", &player->ver) ||
3a0e27d8 986 player->ver == SND_ST_UNIPERIF_VERSION_UNKNOWN) {
76c2145d
AP
987 dev_err(dev, "Unknown uniperipheral version ");
988 return -EINVAL;
989 }
990 /* Underflow recovery is only supported on later ip revisions */
991 if (player->ver >= SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
992 info->underflow_enabled = 1;
993
f9f51973 994 if (of_property_read_u32(pnode, "st,uniperiph-id", &info->id)) {
3a0e27d8
AP
995 dev_err(dev, "uniperipheral id not defined");
996 return -EINVAL;
997 }
76c2145d
AP
998
999 /* Read the device mode property */
f9f51973 1000 if (of_property_read_string(pnode, "st,mode", &mode)) {
3a0e27d8
AP
1001 dev_err(dev, "uniperipheral mode not defined");
1002 return -EINVAL;
1003 }
76c2145d
AP
1004
1005 if (strcasecmp(mode, "hdmi") == 0)
5295a0dc 1006 info->type = SND_ST_UNIPERIF_TYPE_HDMI;
76c2145d 1007 else if (strcasecmp(mode, "pcm") == 0)
5295a0dc 1008 info->type = SND_ST_UNIPERIF_TYPE_PCM;
76c2145d 1009 else if (strcasecmp(mode, "spdif") == 0)
5295a0dc 1010 info->type = SND_ST_UNIPERIF_TYPE_SPDIF;
76c2145d 1011 else
5295a0dc 1012 info->type = SND_ST_UNIPERIF_TYPE_NONE;
76c2145d
AP
1013
1014 /* Save the info structure */
1015 player->info = info;
1016
1017 /* Get the PCM_CLK_SEL bit from audio-glue-ctrl SoC register */
1018 if (uni_player_parse_dt_clk_glue(pdev, player))
1019 return -EINVAL;
1020
1021 return 0;
1022}
1023
85cf604e 1024static const struct snd_soc_dai_ops uni_player_dai_ops = {
fa050796 1025 .startup = uni_player_startup,
76c2145d
AP
1026 .shutdown = uni_player_shutdown,
1027 .prepare = uni_player_prepare,
1028 .trigger = uni_player_trigger,
1029 .hw_params = sti_uniperiph_dai_hw_params,
1030 .set_fmt = sti_uniperiph_dai_set_fmt,
1031 .set_sysclk = uni_player_set_sysclk
1032};
1033
1034int uni_player_init(struct platform_device *pdev,
1035 struct uniperif *player)
1036{
1037 int ret = 0;
1038
1039 player->dev = &pdev->dev;
1040 player->state = UNIPERIF_STATE_STOPPED;
1041 player->hw = &uni_player_pcm_hw;
1042 player->dai_ops = &uni_player_dai_ops;
1043
1044 ret = uni_player_parse_dt(pdev, player);
1045
1046 if (ret < 0) {
1047 dev_err(player->dev, "Failed to parse DeviceTree");
1048 return ret;
1049 }
1050
1051 /* Get uniperif resource */
1052 player->clk = of_clk_get(pdev->dev.of_node, 0);
1053 if (IS_ERR(player->clk))
ed6c75f2 1054 ret = PTR_ERR(player->clk);
76c2145d
AP
1055
1056 /* Select the frequency synthesizer clock */
1057 if (player->clk_sel) {
1058 ret = regmap_field_write(player->clk_sel, 1);
1059 if (ret) {
1060 dev_err(player->dev,
1061 "%s: Failed to select freq synth clock",
1062 __func__);
1063 return ret;
1064 }
1065 }
1066
1067 ret = devm_request_irq(&pdev->dev, player->irq,
1068 uni_player_irq_handler, IRQF_SHARED,
1069 dev_name(&pdev->dev), player);
1070 if (ret < 0)
1071 return ret;
1072
fa050796
AP
1073 mutex_init(&player->ctrl_lock);
1074
76c2145d
AP
1075 /* Ensure that disabled by default */
1076 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
1077 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
1078 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
1079 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
1080
5295a0dc 1081 if (UNIPERIF_TYPE_IS_IEC958(player)) {
76c2145d
AP
1082 /* Set default iec958 status bits */
1083
1084 /* Consumer, PCM, copyright, 2ch, mode 0 */
1085 player->stream_settings.iec958.status[0] = 0x00;
1086 /* Broadcast reception category */
1087 player->stream_settings.iec958.status[1] =
1088 IEC958_AES1_CON_GENERAL;
1089 /* Do not take into account source or channel number */
1090 player->stream_settings.iec958.status[2] =
1091 IEC958_AES2_CON_SOURCE_UNSPEC;
1092 /* Sampling frequency not indicated */
1093 player->stream_settings.iec958.status[3] =
1094 IEC958_AES3_CON_FS_NOTID;
1095 /* Max sample word 24-bit, sample word length not indicated */
1096 player->stream_settings.iec958.status[4] =
1097 IEC958_AES4_CON_MAX_WORDLEN_24 |
1098 IEC958_AES4_CON_WORDLEN_24_20;
76c2145d 1099
36cc0935
AP
1100 player->num_ctrls = ARRAY_SIZE(snd_sti_iec_ctl);
1101 player->snd_ctrls = snd_sti_iec_ctl[0];
1102 } else {
1103 player->num_ctrls = ARRAY_SIZE(snd_sti_pcm_ctl);
1104 player->snd_ctrls = snd_sti_pcm_ctl[0];
1105 }
fa050796 1106
76c2145d
AP
1107 return 0;
1108}
1109EXPORT_SYMBOL_GPL(uni_player_init);