blob: 1bc8ebc2528eb1bdf0c1df6832e943d8eebaa29f [file] [log] [blame]
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001/*
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
Arnaud Pouliquen76c21452015-06-22 16:31:07 +020024
25/*
26 * Driver specific types.
27 */
Arnaud Pouliquen76c21452015-06-22 16:31:07 +020028
Arnaud Pouliquenfa050792015-07-16 11:36:06 +020029#define UNIPERIF_PLAYER_CLK_ADJ_MIN -999999
30#define UNIPERIF_PLAYER_CLK_ADJ_MAX 1000000
Moise Gergaud3ee15ca2016-04-14 15:29:35 +020031#define UNIPERIF_PLAYER_I2S_OUT 1 /* player id connected to I2S/TDM TX bus */
Arnaud Pouliquenfa050792015-07-16 11:36:06 +020032
Arnaud Pouliquen76c21452015-06-22 16:31:07 +020033/*
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 */
kbuild test robotf2da4542015-07-17 07:44:09 +080037static const struct snd_pcm_hardware uni_player_pcm_hw = {
Arnaud Pouliquen76c21452015-06-22 16:31:07 +020038 .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 */
Arnaud Pouliquen5a4326d2016-09-13 09:58:23 +0200103 if (player->underflow_enabled) {
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200104 /* 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))) {
Arnaud Pouliquen5a4326d2016-09-13 09:58:23 +0200137 if (!player->underflow_enabled) {
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200138 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
kbuild test robotf2da4542015-07-17 07:44:09 +0800169static int uni_player_clk_set_rate(struct uniperif *player, unsigned long rate)
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200170{
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
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200233static 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 */
Arnaud Pouliquen36cc0932015-07-16 11:36:07 +0200244 mutex_lock(&player->ctrl_lock);
Moise Gergaud0d3f3c92015-11-24 14:16:35 +0100245 if (runtime) {
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200246 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 }
Arnaud Pouliquen36cc0932015-07-16 11:36:07 +0200321 mutex_unlock(&player->ctrl_lock);
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200322
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) ==
Moise Gergaud8d8b1e22016-04-07 11:25:35 +0200438 SND_SOC_DAIFMT_I2S)
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200439 slot_width = 32;
Moise Gergaud8d8b1e22016-04-07 11:25:35 +0200440 else
441 slot_width = snd_pcm_format_width(runtime->format);
442
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200443 output_frame_size = slot_width * runtime->channels;
444
445 clk_div = player->mclk / runtime->rate;
446 /*
447 * For 32 bits subframe clk_div must be a multiple of 128,
448 * for 16 bits must be a multiple of 64
449 */
450 if ((slot_width == 32) && (clk_div % 128)) {
451 dev_err(player->dev, "%s: invalid clk_div", __func__);
452 return -EINVAL;
453 }
454
455 if ((slot_width == 16) && (clk_div % 64)) {
456 dev_err(player->dev, "%s: invalid clk_div", __func__);
457 return -EINVAL;
458 }
459
460 /*
461 * Number of bits per subframe (which is one channel sample)
462 * on output - Transfer 16 or 32 bits from FIFO
463 */
464 switch (slot_width) {
465 case 32:
466 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
467 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player);
468 break;
469 case 16:
470 SET_UNIPERIF_I2S_FMT_NBIT_16(player);
471 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player);
472 break;
473 default:
474 dev_err(player->dev, "subframe format not supported");
475 return -EINVAL;
476 }
477
478 /* Configure data memory format */
479 switch (runtime->format) {
480 case SNDRV_PCM_FORMAT_S16_LE:
481 /* One data word contains two samples */
482 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player);
483 break;
484
485 case SNDRV_PCM_FORMAT_S32_LE:
486 /*
487 * Actually "16 bits/0 bits" means "32/28/24/20/18/16 bits
488 * on the left than zeros (if less than 32 bytes)"... ;-)
489 */
490 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
491 break;
492
493 default:
494 dev_err(player->dev, "format not supported");
495 return -EINVAL;
496 }
497
498 /* Set rounding to off */
499 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
500
501 /* Set clock divisor */
502 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / (2 * output_frame_size));
503
504 /* Number of channelsmust be even*/
505 if ((runtime->channels % 2) || (runtime->channels < 2) ||
506 (runtime->channels > 10)) {
507 dev_err(player->dev, "%s: invalid nb of channels", __func__);
508 return -EINVAL;
509 }
510
511 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2);
512
513 /* Set 1-bit audio format to disabled */
514 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player);
515
516 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200517
518 /* No iec958 formatting as outputting to DAC */
519 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player);
520
521 return 0;
522}
523
Moise Gergaud8d8b1e22016-04-07 11:25:35 +0200524static int uni_player_prepare_tdm(struct uniperif *player,
525 struct snd_pcm_runtime *runtime)
526{
527 int tdm_frame_size; /* unip tdm frame size in bytes */
528 int user_frame_size; /* user tdm frame size in bytes */
529 /* default unip TDM_WORD_POS_X_Y */
530 unsigned int word_pos[4] = {
531 0x04060002, 0x0C0E080A, 0x14161012, 0x1C1E181A};
532 int freq, ret;
533
534 tdm_frame_size =
535 sti_uniperiph_get_unip_tdm_frame_size(player);
536 user_frame_size =
537 sti_uniperiph_get_user_frame_size(runtime);
538
539 /* fix 16/0 format */
540 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
541 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player);
542
543 /* number of words inserted on the TDM line */
544 SET_UNIPERIF_I2S_FMT_NUM_CH(player, user_frame_size / 4 / 2);
545
546 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
547 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
548
549 /* Enable the tdm functionality */
550 SET_UNIPERIF_TDM_ENABLE_TDM_ENABLE(player);
551
552 /* number of 8 bits timeslots avail in unip tdm frame */
553 SET_UNIPERIF_TDM_FS_REF_DIV_NUM_TIMESLOT(player, tdm_frame_size);
554
555 /* set the timeslot allocation for words in FIFO */
556 sti_uniperiph_get_tdm_word_pos(player, word_pos);
557 SET_UNIPERIF_TDM_WORD_POS(player, 1_2, word_pos[WORD_1_2]);
558 SET_UNIPERIF_TDM_WORD_POS(player, 3_4, word_pos[WORD_3_4]);
559 SET_UNIPERIF_TDM_WORD_POS(player, 5_6, word_pos[WORD_5_6]);
560 SET_UNIPERIF_TDM_WORD_POS(player, 7_8, word_pos[WORD_7_8]);
561
562 /* set unip clk rate (not done vai set_sysclk ops) */
563 freq = runtime->rate * tdm_frame_size * 8;
564 mutex_lock(&player->ctrl_lock);
565 ret = uni_player_clk_set_rate(player, freq);
566 if (!ret)
567 player->mclk = freq;
568 mutex_unlock(&player->ctrl_lock);
569
570 return 0;
571}
572
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200573/*
Arnaud Pouliquen36cc0932015-07-16 11:36:07 +0200574 * ALSA uniperipheral iec958 controls
575 */
576static int uni_player_ctl_iec958_info(struct snd_kcontrol *kcontrol,
577 struct snd_ctl_elem_info *uinfo)
578{
579 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
580 uinfo->count = 1;
581
582 return 0;
583}
584
585static int uni_player_ctl_iec958_get(struct snd_kcontrol *kcontrol,
586 struct snd_ctl_elem_value *ucontrol)
587{
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;
592
593 mutex_lock(&player->ctrl_lock);
594 ucontrol->value.iec958.status[0] = iec958->status[0];
595 ucontrol->value.iec958.status[1] = iec958->status[1];
596 ucontrol->value.iec958.status[2] = iec958->status[2];
597 ucontrol->value.iec958.status[3] = iec958->status[3];
598 mutex_unlock(&player->ctrl_lock);
599 return 0;
600}
601
602static int uni_player_ctl_iec958_put(struct snd_kcontrol *kcontrol,
603 struct snd_ctl_elem_value *ucontrol)
604{
605 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
606 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
607 struct uniperif *player = priv->dai_data.uni;
608 struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958;
609
610 mutex_lock(&player->ctrl_lock);
611 iec958->status[0] = ucontrol->value.iec958.status[0];
612 iec958->status[1] = ucontrol->value.iec958.status[1];
613 iec958->status[2] = ucontrol->value.iec958.status[2];
614 iec958->status[3] = ucontrol->value.iec958.status[3];
615 mutex_unlock(&player->ctrl_lock);
616
617 uni_player_set_channel_status(player, NULL);
618
619 return 0;
620}
621
622static struct snd_kcontrol_new uni_player_iec958_ctl = {
623 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
624 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
625 .info = uni_player_ctl_iec958_info,
626 .get = uni_player_ctl_iec958_get,
627 .put = uni_player_ctl_iec958_put,
628};
629
630/*
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200631 * uniperif rate adjustement control
632 */
633static int snd_sti_clk_adjustment_info(struct snd_kcontrol *kcontrol,
634 struct snd_ctl_elem_info *uinfo)
635{
636 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
637 uinfo->count = 1;
638 uinfo->value.integer.min = UNIPERIF_PLAYER_CLK_ADJ_MIN;
639 uinfo->value.integer.max = UNIPERIF_PLAYER_CLK_ADJ_MAX;
640 uinfo->value.integer.step = 1;
641
642 return 0;
643}
644
645static int snd_sti_clk_adjustment_get(struct snd_kcontrol *kcontrol,
646 struct snd_ctl_elem_value *ucontrol)
647{
648 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
649 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
650 struct uniperif *player = priv->dai_data.uni;
651
Arnaud Pouliquen36cc0932015-07-16 11:36:07 +0200652 mutex_lock(&player->ctrl_lock);
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200653 ucontrol->value.integer.value[0] = player->clk_adj;
Arnaud Pouliquen36cc0932015-07-16 11:36:07 +0200654 mutex_unlock(&player->ctrl_lock);
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200655
656 return 0;
657}
658
659static int snd_sti_clk_adjustment_put(struct snd_kcontrol *kcontrol,
660 struct snd_ctl_elem_value *ucontrol)
661{
662 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
663 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
664 struct uniperif *player = priv->dai_data.uni;
665 int ret = 0;
666
667 if ((ucontrol->value.integer.value[0] < UNIPERIF_PLAYER_CLK_ADJ_MIN) ||
668 (ucontrol->value.integer.value[0] > UNIPERIF_PLAYER_CLK_ADJ_MAX))
669 return -EINVAL;
670
671 mutex_lock(&player->ctrl_lock);
672 player->clk_adj = ucontrol->value.integer.value[0];
673
674 if (player->mclk)
675 ret = uni_player_clk_set_rate(player, player->mclk);
676 mutex_unlock(&player->ctrl_lock);
677
678 return ret;
679}
680
681static struct snd_kcontrol_new uni_player_clk_adj_ctl = {
682 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
683 .name = "PCM Playback Oversampling Freq. Adjustment",
684 .info = snd_sti_clk_adjustment_info,
685 .get = snd_sti_clk_adjustment_get,
686 .put = snd_sti_clk_adjustment_put,
687};
688
Arnaud Pouliquen36cc0932015-07-16 11:36:07 +0200689static struct snd_kcontrol_new *snd_sti_pcm_ctl[] = {
690 &uni_player_clk_adj_ctl,
691};
692
693static struct snd_kcontrol_new *snd_sti_iec_ctl[] = {
694 &uni_player_iec958_ctl,
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200695 &uni_player_clk_adj_ctl,
696};
697
698static int uni_player_startup(struct snd_pcm_substream *substream,
699 struct snd_soc_dai *dai)
700{
701 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
702 struct uniperif *player = priv->dai_data.uni;
Moise Gergaud8d8b1e22016-04-07 11:25:35 +0200703 int ret;
704
Moise Gergaud36a65e22015-11-19 14:54:09 +0100705 player->substream = substream;
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200706
707 player->clk_adj = 0;
708
Moise Gergaud8d8b1e22016-04-07 11:25:35 +0200709 if (!UNIPERIF_TYPE_IS_TDM(player))
710 return 0;
711
712 /* refine hw constraint in tdm mode */
713 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
714 SNDRV_PCM_HW_PARAM_CHANNELS,
715 sti_uniperiph_fix_tdm_chan,
716 player, SNDRV_PCM_HW_PARAM_CHANNELS,
717 -1);
718 if (ret < 0)
719 return ret;
720
721 return snd_pcm_hw_rule_add(substream->runtime, 0,
722 SNDRV_PCM_HW_PARAM_FORMAT,
723 sti_uniperiph_fix_tdm_format,
724 player, SNDRV_PCM_HW_PARAM_FORMAT,
725 -1);
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200726}
727
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200728static int uni_player_set_sysclk(struct snd_soc_dai *dai, int clk_id,
729 unsigned int freq, int dir)
730{
731 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
732 struct uniperif *player = priv->dai_data.uni;
Arnaud Pouliquened6c75f2015-07-16 11:36:02 +0200733 int ret;
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200734
Moise Gergaud8d8b1e22016-04-07 11:25:35 +0200735 if (UNIPERIF_TYPE_IS_TDM(player) || (dir == SND_SOC_CLOCK_IN))
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200736 return 0;
737
738 if (clk_id != 0)
739 return -EINVAL;
740
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200741 mutex_lock(&player->ctrl_lock);
742 ret = uni_player_clk_set_rate(player, freq);
Arnaud Pouliquened6c75f2015-07-16 11:36:02 +0200743 if (!ret)
744 player->mclk = freq;
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200745 mutex_unlock(&player->ctrl_lock);
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200746
Arnaud Pouliquened6c75f2015-07-16 11:36:02 +0200747 return ret;
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200748}
749
750static int uni_player_prepare(struct snd_pcm_substream *substream,
751 struct snd_soc_dai *dai)
752{
753 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
754 struct uniperif *player = priv->dai_data.uni;
755 struct snd_pcm_runtime *runtime = substream->runtime;
756 int transfer_size, trigger_limit;
757 int ret;
758
759 /* The player should be stopped */
760 if (player->state != UNIPERIF_STATE_STOPPED) {
761 dev_err(player->dev, "%s: invalid player state %d", __func__,
762 player->state);
763 return -EINVAL;
764 }
765
766 /* Calculate transfer size (in fifo cells and bytes) for frame count */
Arnaud Pouliquen5a4326d2016-09-13 09:58:23 +0200767 if (player->type == SND_ST_UNIPERIF_TYPE_TDM) {
Moise Gergaud8d8b1e22016-04-07 11:25:35 +0200768 /* transfer size = user frame size (in 32 bits FIFO cell) */
769 transfer_size =
770 sti_uniperiph_get_user_frame_size(runtime) / 4;
771 } else {
772 transfer_size = runtime->channels * UNIPERIF_FIFO_FRAMES;
773 }
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200774
775 /* Calculate number of empty cells available before asserting DREQ */
776 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
777 trigger_limit = UNIPERIF_FIFO_SIZE - transfer_size;
778 } else {
779 /*
780 * Since SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
781 * FDMA_TRIGGER_LIMIT also controls when the state switches
782 * from OFF or STANDBY to AUDIO DATA.
783 */
784 trigger_limit = transfer_size;
785 }
786
787 /* Trigger limit must be an even number */
788 if ((!trigger_limit % 2) || (trigger_limit != 1 && transfer_size % 2) ||
789 (trigger_limit > UNIPERIF_CONFIG_DMA_TRIG_LIMIT_MASK(player))) {
790 dev_err(player->dev, "invalid trigger limit %d", trigger_limit);
791 return -EINVAL;
792 }
793
794 SET_UNIPERIF_CONFIG_DMA_TRIG_LIMIT(player, trigger_limit);
795
796 /* Uniperipheral setup depends on player type */
Arnaud Pouliquen5a4326d2016-09-13 09:58:23 +0200797 switch (player->type) {
Moise Gergaud5295a0d2016-04-07 11:25:31 +0200798 case SND_ST_UNIPERIF_TYPE_HDMI:
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200799 ret = uni_player_prepare_iec958(player, runtime);
800 break;
Moise Gergaud5295a0d2016-04-07 11:25:31 +0200801 case SND_ST_UNIPERIF_TYPE_PCM:
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200802 ret = uni_player_prepare_pcm(player, runtime);
803 break;
Moise Gergaud5295a0d2016-04-07 11:25:31 +0200804 case SND_ST_UNIPERIF_TYPE_SPDIF:
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200805 ret = uni_player_prepare_iec958(player, runtime);
806 break;
Moise Gergaud8d8b1e22016-04-07 11:25:35 +0200807 case SND_ST_UNIPERIF_TYPE_TDM:
808 ret = uni_player_prepare_tdm(player, runtime);
809 break;
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200810 default:
811 dev_err(player->dev, "invalid player type");
812 return -EINVAL;
813 }
814
815 if (ret)
816 return ret;
817
818 switch (player->daifmt & SND_SOC_DAIFMT_INV_MASK) {
819 case SND_SOC_DAIFMT_NB_NF:
820 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
821 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
822 break;
823 case SND_SOC_DAIFMT_NB_IF:
824 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
825 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
826 break;
827 case SND_SOC_DAIFMT_IB_NF:
828 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
829 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
830 break;
831 case SND_SOC_DAIFMT_IB_IF:
832 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
833 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
Arnaud Pouliquened6c75f2015-07-16 11:36:02 +0200834 break;
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200835 }
836
837 switch (player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) {
838 case SND_SOC_DAIFMT_I2S:
839 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
840 SET_UNIPERIF_I2S_FMT_PADDING_I2S_MODE(player);
841 break;
842 case SND_SOC_DAIFMT_LEFT_J:
843 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
844 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
845 break;
846 case SND_SOC_DAIFMT_RIGHT_J:
847 SET_UNIPERIF_I2S_FMT_ALIGN_RIGHT(player);
848 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
849 break;
850 default:
851 dev_err(player->dev, "format not supported");
852 return -EINVAL;
853 }
854
855 SET_UNIPERIF_I2S_FMT_NO_OF_SAMPLES_TO_READ(player, 0);
856
857 /* Reset uniperipheral player */
858 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
859
860 return reset_player(player);
861}
862
863static int uni_player_start(struct uniperif *player)
864{
865 int ret;
866
867 /* The player should be stopped */
868 if (player->state != UNIPERIF_STATE_STOPPED) {
869 dev_err(player->dev, "%s: invalid player state", __func__);
870 return -EINVAL;
871 }
872
873 ret = clk_prepare_enable(player->clk);
874 if (ret) {
875 dev_err(player->dev, "%s: Failed to enable clock", __func__);
876 return ret;
877 }
878
879 /* Clear any pending interrupts */
880 SET_UNIPERIF_ITS_BCLR(player, GET_UNIPERIF_ITS(player));
881
882 /* Set the interrupt mask */
883 SET_UNIPERIF_ITM_BSET_DMA_ERROR(player);
884 SET_UNIPERIF_ITM_BSET_FIFO_ERROR(player);
885
886 /* Enable underflow recovery interrupts */
Arnaud Pouliquen5a4326d2016-09-13 09:58:23 +0200887 if (player->underflow_enabled) {
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200888 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_DONE(player);
889 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_FAILED(player);
890 }
891
892 /* Reset uniperipheral player */
893 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
894
895 ret = reset_player(player);
Wei Yongjunb7c8c5d2016-09-17 01:34:33 +0000896 if (ret < 0) {
897 clk_disable_unprepare(player->clk);
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200898 return ret;
Wei Yongjunb7c8c5d2016-09-17 01:34:33 +0000899 }
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200900
901 /*
902 * Does not use IEC61937 features of the uniperipheral hardware.
903 * Instead it performs IEC61937 in software and inserts it directly
904 * into the audio data stream. As such, when encoded mode is selected,
905 * linear pcm mode is still used, but with the differences of the
906 * channel status bits set for encoded mode and the validity bits set.
907 */
908 SET_UNIPERIF_CTRL_OPERATION_PCM_DATA(player);
909
910 /*
911 * If iec958 formatting is required for hdmi or spdif, then it must be
912 * enabled after the operation mode is set. If set prior to this, it
913 * will not take affect and hang the player.
914 */
915 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
Moise Gergaud5295a0d2016-04-07 11:25:31 +0200916 if (UNIPERIF_TYPE_IS_IEC958(player))
917 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player);
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200918
919 /* Force channel status update (no update if clk disable) */
920 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
921 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player);
922 else
923 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player);
924
925 /* Update state to started */
926 player->state = UNIPERIF_STATE_STARTED;
927
928 return 0;
929}
930
931static int uni_player_stop(struct uniperif *player)
932{
933 int ret;
934
935 /* The player should not be in stopped state */
936 if (player->state == UNIPERIF_STATE_STOPPED) {
937 dev_err(player->dev, "%s: invalid player state", __func__);
938 return -EINVAL;
939 }
940
941 /* Turn the player off */
942 SET_UNIPERIF_CTRL_OPERATION_OFF(player);
943
944 /* Soft reset the player */
945 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
946
947 ret = reset_player(player);
948 if (ret < 0)
949 return ret;
950
951 /* Disable interrupts */
952 SET_UNIPERIF_ITM_BCLR(player, GET_UNIPERIF_ITM(player));
953
954 /* Disable clock */
955 clk_disable_unprepare(player->clk);
956
957 /* Update state to stopped and return */
958 player->state = UNIPERIF_STATE_STOPPED;
959
960 return 0;
961}
962
963int uni_player_resume(struct uniperif *player)
964{
965 int ret;
966
967 /* Select the frequency synthesizer clock */
968 if (player->clk_sel) {
969 ret = regmap_field_write(player->clk_sel, 1);
970 if (ret) {
971 dev_err(player->dev,
972 "%s: Failed to select freq synth clock",
973 __func__);
974 return ret;
975 }
976 }
977
978 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
979 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
980 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
981 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
982
983 return 0;
984}
985EXPORT_SYMBOL_GPL(uni_player_resume);
986
987static int uni_player_trigger(struct snd_pcm_substream *substream,
988 int cmd, struct snd_soc_dai *dai)
989{
990 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
991 struct uniperif *player = priv->dai_data.uni;
992
993 switch (cmd) {
994 case SNDRV_PCM_TRIGGER_START:
995 return uni_player_start(player);
996 case SNDRV_PCM_TRIGGER_STOP:
997 return uni_player_stop(player);
998 case SNDRV_PCM_TRIGGER_RESUME:
999 return uni_player_resume(player);
1000 default:
1001 return -EINVAL;
1002 }
1003}
1004
1005static void uni_player_shutdown(struct snd_pcm_substream *substream,
1006 struct snd_soc_dai *dai)
1007{
1008 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
1009 struct uniperif *player = priv->dai_data.uni;
1010
1011 if (player->state != UNIPERIF_STATE_STOPPED)
1012 /* Stop the player */
1013 uni_player_stop(player);
Moise Gergaud36a65e22015-11-19 14:54:09 +01001014
1015 player->substream = NULL;
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001016}
1017
Moise Gergaud3ee15ca2016-04-14 15:29:35 +02001018static int uni_player_parse_dt_audio_glue(struct platform_device *pdev,
1019 struct uniperif *player)
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001020{
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001021 struct device_node *node = pdev->dev.of_node;
1022 struct regmap *regmap;
Moise Gergaud3ee15ca2016-04-14 15:29:35 +02001023 struct reg_field regfield[2] = {
1024 /* PCM_CLK_SEL */
1025 REG_FIELD(SYS_CFG_AUDIO_GLUE,
Arnaud Pouliquen5a4326d2016-09-13 09:58:23 +02001026 8 + player->id,
1027 8 + player->id),
Moise Gergaud3ee15ca2016-04-14 15:29:35 +02001028 /* PCMP_VALID_SEL */
1029 REG_FIELD(SYS_CFG_AUDIO_GLUE, 0, 1)
1030 };
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001031
1032 regmap = syscon_regmap_lookup_by_phandle(node, "st,syscfg");
1033
Wei Yongjun7d267dd2016-06-13 14:39:57 +00001034 if (IS_ERR(regmap)) {
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001035 dev_err(&pdev->dev, "sti-audio-clk-glue syscf not found\n");
Wei Yongjun7d267dd2016-06-13 14:39:57 +00001036 return PTR_ERR(regmap);
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001037 }
1038
Moise Gergaud3ee15ca2016-04-14 15:29:35 +02001039 player->clk_sel = regmap_field_alloc(regmap, regfield[0]);
1040 player->valid_sel = regmap_field_alloc(regmap, regfield[1]);
1041
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001042 return 0;
1043}
1044
Lars-Peter Clausen85cf604e2015-07-27 10:56:27 +02001045static const struct snd_soc_dai_ops uni_player_dai_ops = {
Arnaud Pouliquenfa050792015-07-16 11:36:06 +02001046 .startup = uni_player_startup,
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001047 .shutdown = uni_player_shutdown,
1048 .prepare = uni_player_prepare,
1049 .trigger = uni_player_trigger,
1050 .hw_params = sti_uniperiph_dai_hw_params,
1051 .set_fmt = sti_uniperiph_dai_set_fmt,
Moise Gergaud8d8b1e22016-04-07 11:25:35 +02001052 .set_sysclk = uni_player_set_sysclk,
1053 .set_tdm_slot = sti_uniperiph_set_tdm_slot
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001054};
1055
1056int uni_player_init(struct platform_device *pdev,
1057 struct uniperif *player)
1058{
1059 int ret = 0;
1060
1061 player->dev = &pdev->dev;
1062 player->state = UNIPERIF_STATE_STOPPED;
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001063 player->dai_ops = &uni_player_dai_ops;
1064
Arnaud Pouliquen5a4326d2016-09-13 09:58:23 +02001065 /* Get PCM_CLK_SEL & PCMP_VALID_SEL from audio-glue-ctrl SoC reg */
1066 ret = uni_player_parse_dt_audio_glue(pdev, player);
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001067
1068 if (ret < 0) {
1069 dev_err(player->dev, "Failed to parse DeviceTree");
1070 return ret;
1071 }
1072
Arnaud Pouliquen5a4326d2016-09-13 09:58:23 +02001073 /* Underflow recovery is only supported on later ip revisions */
1074 if (player->ver >= SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
1075 player->underflow_enabled = 1;
1076
Moise Gergaud8d8b1e22016-04-07 11:25:35 +02001077 if (UNIPERIF_TYPE_IS_TDM(player))
1078 player->hw = &uni_tdm_hw;
1079 else
1080 player->hw = &uni_player_pcm_hw;
1081
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001082 /* Get uniperif resource */
1083 player->clk = of_clk_get(pdev->dev.of_node, 0);
1084 if (IS_ERR(player->clk))
Arnaud Pouliquened6c75f2015-07-16 11:36:02 +02001085 ret = PTR_ERR(player->clk);
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001086
1087 /* Select the frequency synthesizer clock */
1088 if (player->clk_sel) {
1089 ret = regmap_field_write(player->clk_sel, 1);
1090 if (ret) {
1091 dev_err(player->dev,
1092 "%s: Failed to select freq synth clock",
1093 __func__);
1094 return ret;
1095 }
1096 }
1097
Moise Gergaud3ee15ca2016-04-14 15:29:35 +02001098 /* connect to I2S/TDM TX bus */
1099 if (player->valid_sel &&
Arnaud Pouliquen5a4326d2016-09-13 09:58:23 +02001100 (player->id == UNIPERIF_PLAYER_I2S_OUT)) {
1101 ret = regmap_field_write(player->valid_sel, player->id);
Moise Gergaud3ee15ca2016-04-14 15:29:35 +02001102 if (ret) {
1103 dev_err(player->dev,
1104 "%s: unable to connect to tdm bus", __func__);
1105 return ret;
1106 }
1107 }
1108
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001109 ret = devm_request_irq(&pdev->dev, player->irq,
1110 uni_player_irq_handler, IRQF_SHARED,
1111 dev_name(&pdev->dev), player);
1112 if (ret < 0)
1113 return ret;
1114
Arnaud Pouliquenfa050792015-07-16 11:36:06 +02001115 mutex_init(&player->ctrl_lock);
1116
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001117 /* Ensure that disabled by default */
1118 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
1119 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
1120 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
1121 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
1122
Moise Gergaud5295a0d2016-04-07 11:25:31 +02001123 if (UNIPERIF_TYPE_IS_IEC958(player)) {
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001124 /* Set default iec958 status bits */
1125
1126 /* Consumer, PCM, copyright, 2ch, mode 0 */
1127 player->stream_settings.iec958.status[0] = 0x00;
1128 /* Broadcast reception category */
1129 player->stream_settings.iec958.status[1] =
1130 IEC958_AES1_CON_GENERAL;
1131 /* Do not take into account source or channel number */
1132 player->stream_settings.iec958.status[2] =
1133 IEC958_AES2_CON_SOURCE_UNSPEC;
1134 /* Sampling frequency not indicated */
1135 player->stream_settings.iec958.status[3] =
1136 IEC958_AES3_CON_FS_NOTID;
1137 /* Max sample word 24-bit, sample word length not indicated */
1138 player->stream_settings.iec958.status[4] =
1139 IEC958_AES4_CON_MAX_WORDLEN_24 |
1140 IEC958_AES4_CON_WORDLEN_24_20;
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001141
Arnaud Pouliquen36cc0932015-07-16 11:36:07 +02001142 player->num_ctrls = ARRAY_SIZE(snd_sti_iec_ctl);
1143 player->snd_ctrls = snd_sti_iec_ctl[0];
1144 } else {
1145 player->num_ctrls = ARRAY_SIZE(snd_sti_pcm_ctl);
1146 player->snd_ctrls = snd_sti_pcm_ctl[0];
1147 }
Arnaud Pouliquenfa050792015-07-16 11:36:06 +02001148
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001149 return 0;
1150}
1151EXPORT_SYMBOL_GPL(uni_player_init);