blob: ad54d4cf58ada992f6f279cc0e6a00386e8e273e [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 Pouliquen5a4326d12016-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 Pouliquen5a4326d12016-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
Arnaud Pouliquen1e6d3042016-10-24 16:42:52 +0200617 if (player->substream && player->substream->runtime)
618 uni_player_set_channel_status(player,
619 player->substream->runtime);
620 else
621 uni_player_set_channel_status(player, NULL);
Arnaud Pouliquen36cc0932015-07-16 11:36:07 +0200622
623 return 0;
624}
625
626static struct snd_kcontrol_new uni_player_iec958_ctl = {
627 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
628 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
629 .info = uni_player_ctl_iec958_info,
630 .get = uni_player_ctl_iec958_get,
631 .put = uni_player_ctl_iec958_put,
632};
633
634/*
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200635 * uniperif rate adjustement control
636 */
637static int snd_sti_clk_adjustment_info(struct snd_kcontrol *kcontrol,
638 struct snd_ctl_elem_info *uinfo)
639{
640 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
641 uinfo->count = 1;
642 uinfo->value.integer.min = UNIPERIF_PLAYER_CLK_ADJ_MIN;
643 uinfo->value.integer.max = UNIPERIF_PLAYER_CLK_ADJ_MAX;
644 uinfo->value.integer.step = 1;
645
646 return 0;
647}
648
649static int snd_sti_clk_adjustment_get(struct snd_kcontrol *kcontrol,
650 struct snd_ctl_elem_value *ucontrol)
651{
652 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
653 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
654 struct uniperif *player = priv->dai_data.uni;
655
Arnaud Pouliquen36cc0932015-07-16 11:36:07 +0200656 mutex_lock(&player->ctrl_lock);
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200657 ucontrol->value.integer.value[0] = player->clk_adj;
Arnaud Pouliquen36cc0932015-07-16 11:36:07 +0200658 mutex_unlock(&player->ctrl_lock);
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200659
660 return 0;
661}
662
663static int snd_sti_clk_adjustment_put(struct snd_kcontrol *kcontrol,
664 struct snd_ctl_elem_value *ucontrol)
665{
666 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
667 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
668 struct uniperif *player = priv->dai_data.uni;
669 int ret = 0;
670
671 if ((ucontrol->value.integer.value[0] < UNIPERIF_PLAYER_CLK_ADJ_MIN) ||
672 (ucontrol->value.integer.value[0] > UNIPERIF_PLAYER_CLK_ADJ_MAX))
673 return -EINVAL;
674
675 mutex_lock(&player->ctrl_lock);
676 player->clk_adj = ucontrol->value.integer.value[0];
677
678 if (player->mclk)
679 ret = uni_player_clk_set_rate(player, player->mclk);
680 mutex_unlock(&player->ctrl_lock);
681
682 return ret;
683}
684
685static struct snd_kcontrol_new uni_player_clk_adj_ctl = {
686 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
687 .name = "PCM Playback Oversampling Freq. Adjustment",
688 .info = snd_sti_clk_adjustment_info,
689 .get = snd_sti_clk_adjustment_get,
690 .put = snd_sti_clk_adjustment_put,
691};
692
Arnaud Pouliquen36cc0932015-07-16 11:36:07 +0200693static struct snd_kcontrol_new *snd_sti_pcm_ctl[] = {
694 &uni_player_clk_adj_ctl,
695};
696
697static struct snd_kcontrol_new *snd_sti_iec_ctl[] = {
698 &uni_player_iec958_ctl,
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200699 &uni_player_clk_adj_ctl,
700};
701
702static int uni_player_startup(struct snd_pcm_substream *substream,
703 struct snd_soc_dai *dai)
704{
705 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
706 struct uniperif *player = priv->dai_data.uni;
Moise Gergaud8d8b1e22016-04-07 11:25:35 +0200707 int ret;
708
Moise Gergaud36a65e22015-11-19 14:54:09 +0100709 player->substream = substream;
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200710
711 player->clk_adj = 0;
712
Moise Gergaud8d8b1e22016-04-07 11:25:35 +0200713 if (!UNIPERIF_TYPE_IS_TDM(player))
714 return 0;
715
716 /* refine hw constraint in tdm mode */
717 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
718 SNDRV_PCM_HW_PARAM_CHANNELS,
719 sti_uniperiph_fix_tdm_chan,
720 player, SNDRV_PCM_HW_PARAM_CHANNELS,
721 -1);
722 if (ret < 0)
723 return ret;
724
725 return snd_pcm_hw_rule_add(substream->runtime, 0,
726 SNDRV_PCM_HW_PARAM_FORMAT,
727 sti_uniperiph_fix_tdm_format,
728 player, SNDRV_PCM_HW_PARAM_FORMAT,
729 -1);
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200730}
731
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200732static int uni_player_set_sysclk(struct snd_soc_dai *dai, int clk_id,
733 unsigned int freq, int dir)
734{
735 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
736 struct uniperif *player = priv->dai_data.uni;
Arnaud Pouliquened6c75f2015-07-16 11:36:02 +0200737 int ret;
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200738
Moise Gergaud8d8b1e22016-04-07 11:25:35 +0200739 if (UNIPERIF_TYPE_IS_TDM(player) || (dir == SND_SOC_CLOCK_IN))
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200740 return 0;
741
742 if (clk_id != 0)
743 return -EINVAL;
744
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200745 mutex_lock(&player->ctrl_lock);
746 ret = uni_player_clk_set_rate(player, freq);
Arnaud Pouliquened6c75f2015-07-16 11:36:02 +0200747 if (!ret)
748 player->mclk = freq;
Arnaud Pouliquenfa050792015-07-16 11:36:06 +0200749 mutex_unlock(&player->ctrl_lock);
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200750
Arnaud Pouliquened6c75f2015-07-16 11:36:02 +0200751 return ret;
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200752}
753
754static int uni_player_prepare(struct snd_pcm_substream *substream,
755 struct snd_soc_dai *dai)
756{
757 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
758 struct uniperif *player = priv->dai_data.uni;
759 struct snd_pcm_runtime *runtime = substream->runtime;
760 int transfer_size, trigger_limit;
761 int ret;
762
763 /* The player should be stopped */
764 if (player->state != UNIPERIF_STATE_STOPPED) {
765 dev_err(player->dev, "%s: invalid player state %d", __func__,
766 player->state);
767 return -EINVAL;
768 }
769
770 /* Calculate transfer size (in fifo cells and bytes) for frame count */
Arnaud Pouliquen5a4326d12016-09-13 09:58:23 +0200771 if (player->type == SND_ST_UNIPERIF_TYPE_TDM) {
Moise Gergaud8d8b1e22016-04-07 11:25:35 +0200772 /* transfer size = user frame size (in 32 bits FIFO cell) */
773 transfer_size =
774 sti_uniperiph_get_user_frame_size(runtime) / 4;
775 } else {
776 transfer_size = runtime->channels * UNIPERIF_FIFO_FRAMES;
777 }
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200778
779 /* Calculate number of empty cells available before asserting DREQ */
780 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
781 trigger_limit = UNIPERIF_FIFO_SIZE - transfer_size;
782 } else {
783 /*
784 * Since SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0
785 * FDMA_TRIGGER_LIMIT also controls when the state switches
786 * from OFF or STANDBY to AUDIO DATA.
787 */
788 trigger_limit = transfer_size;
789 }
790
791 /* Trigger limit must be an even number */
792 if ((!trigger_limit % 2) || (trigger_limit != 1 && transfer_size % 2) ||
793 (trigger_limit > UNIPERIF_CONFIG_DMA_TRIG_LIMIT_MASK(player))) {
794 dev_err(player->dev, "invalid trigger limit %d", trigger_limit);
795 return -EINVAL;
796 }
797
798 SET_UNIPERIF_CONFIG_DMA_TRIG_LIMIT(player, trigger_limit);
799
800 /* Uniperipheral setup depends on player type */
Arnaud Pouliquen5a4326d12016-09-13 09:58:23 +0200801 switch (player->type) {
Moise Gergaud5295a0d2016-04-07 11:25:31 +0200802 case SND_ST_UNIPERIF_TYPE_HDMI:
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200803 ret = uni_player_prepare_iec958(player, runtime);
804 break;
Moise Gergaud5295a0d2016-04-07 11:25:31 +0200805 case SND_ST_UNIPERIF_TYPE_PCM:
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200806 ret = uni_player_prepare_pcm(player, runtime);
807 break;
Moise Gergaud5295a0d2016-04-07 11:25:31 +0200808 case SND_ST_UNIPERIF_TYPE_SPDIF:
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200809 ret = uni_player_prepare_iec958(player, runtime);
810 break;
Moise Gergaud8d8b1e22016-04-07 11:25:35 +0200811 case SND_ST_UNIPERIF_TYPE_TDM:
812 ret = uni_player_prepare_tdm(player, runtime);
813 break;
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200814 default:
815 dev_err(player->dev, "invalid player type");
816 return -EINVAL;
817 }
818
819 if (ret)
820 return ret;
821
822 switch (player->daifmt & SND_SOC_DAIFMT_INV_MASK) {
823 case SND_SOC_DAIFMT_NB_NF:
824 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
825 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
826 break;
827 case SND_SOC_DAIFMT_NB_IF:
828 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
829 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
830 break;
831 case SND_SOC_DAIFMT_IB_NF:
832 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
833 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
834 break;
835 case SND_SOC_DAIFMT_IB_IF:
836 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
837 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
Arnaud Pouliquened6c75f2015-07-16 11:36:02 +0200838 break;
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200839 }
840
841 switch (player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) {
842 case SND_SOC_DAIFMT_I2S:
843 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
844 SET_UNIPERIF_I2S_FMT_PADDING_I2S_MODE(player);
845 break;
846 case SND_SOC_DAIFMT_LEFT_J:
847 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
848 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
849 break;
850 case SND_SOC_DAIFMT_RIGHT_J:
851 SET_UNIPERIF_I2S_FMT_ALIGN_RIGHT(player);
852 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
853 break;
854 default:
855 dev_err(player->dev, "format not supported");
856 return -EINVAL;
857 }
858
859 SET_UNIPERIF_I2S_FMT_NO_OF_SAMPLES_TO_READ(player, 0);
860
861 /* Reset uniperipheral player */
862 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
863
864 return reset_player(player);
865}
866
867static int uni_player_start(struct uniperif *player)
868{
869 int ret;
870
871 /* The player should be stopped */
872 if (player->state != UNIPERIF_STATE_STOPPED) {
873 dev_err(player->dev, "%s: invalid player state", __func__);
874 return -EINVAL;
875 }
876
877 ret = clk_prepare_enable(player->clk);
878 if (ret) {
879 dev_err(player->dev, "%s: Failed to enable clock", __func__);
880 return ret;
881 }
882
883 /* Clear any pending interrupts */
884 SET_UNIPERIF_ITS_BCLR(player, GET_UNIPERIF_ITS(player));
885
886 /* Set the interrupt mask */
887 SET_UNIPERIF_ITM_BSET_DMA_ERROR(player);
888 SET_UNIPERIF_ITM_BSET_FIFO_ERROR(player);
889
890 /* Enable underflow recovery interrupts */
Arnaud Pouliquen5a4326d12016-09-13 09:58:23 +0200891 if (player->underflow_enabled) {
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200892 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_DONE(player);
893 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_FAILED(player);
894 }
895
896 /* Reset uniperipheral player */
897 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
898
899 ret = reset_player(player);
Wei Yongjunb7c8c5d2016-09-17 01:34:33 +0000900 if (ret < 0) {
901 clk_disable_unprepare(player->clk);
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200902 return ret;
Wei Yongjunb7c8c5d2016-09-17 01:34:33 +0000903 }
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200904
905 /*
906 * Does not use IEC61937 features of the uniperipheral hardware.
907 * Instead it performs IEC61937 in software and inserts it directly
908 * into the audio data stream. As such, when encoded mode is selected,
909 * linear pcm mode is still used, but with the differences of the
910 * channel status bits set for encoded mode and the validity bits set.
911 */
912 SET_UNIPERIF_CTRL_OPERATION_PCM_DATA(player);
913
914 /*
915 * If iec958 formatting is required for hdmi or spdif, then it must be
916 * enabled after the operation mode is set. If set prior to this, it
917 * will not take affect and hang the player.
918 */
919 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
Moise Gergaud5295a0d2016-04-07 11:25:31 +0200920 if (UNIPERIF_TYPE_IS_IEC958(player))
921 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player);
Arnaud Pouliquen76c21452015-06-22 16:31:07 +0200922
923 /* Force channel status update (no update if clk disable) */
924 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
925 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player);
926 else
927 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player);
928
929 /* Update state to started */
930 player->state = UNIPERIF_STATE_STARTED;
931
932 return 0;
933}
934
935static int uni_player_stop(struct uniperif *player)
936{
937 int ret;
938
939 /* The player should not be in stopped state */
940 if (player->state == UNIPERIF_STATE_STOPPED) {
941 dev_err(player->dev, "%s: invalid player state", __func__);
942 return -EINVAL;
943 }
944
945 /* Turn the player off */
946 SET_UNIPERIF_CTRL_OPERATION_OFF(player);
947
948 /* Soft reset the player */
949 SET_UNIPERIF_SOFT_RST_SOFT_RST(player);
950
951 ret = reset_player(player);
952 if (ret < 0)
953 return ret;
954
955 /* Disable interrupts */
956 SET_UNIPERIF_ITM_BCLR(player, GET_UNIPERIF_ITM(player));
957
958 /* Disable clock */
959 clk_disable_unprepare(player->clk);
960
961 /* Update state to stopped and return */
962 player->state = UNIPERIF_STATE_STOPPED;
963
964 return 0;
965}
966
967int uni_player_resume(struct uniperif *player)
968{
969 int ret;
970
971 /* Select the frequency synthesizer clock */
972 if (player->clk_sel) {
973 ret = regmap_field_write(player->clk_sel, 1);
974 if (ret) {
975 dev_err(player->dev,
976 "%s: Failed to select freq synth clock",
977 __func__);
978 return ret;
979 }
980 }
981
982 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
983 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
984 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
985 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
986
987 return 0;
988}
989EXPORT_SYMBOL_GPL(uni_player_resume);
990
991static int uni_player_trigger(struct snd_pcm_substream *substream,
992 int cmd, struct snd_soc_dai *dai)
993{
994 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
995 struct uniperif *player = priv->dai_data.uni;
996
997 switch (cmd) {
998 case SNDRV_PCM_TRIGGER_START:
999 return uni_player_start(player);
1000 case SNDRV_PCM_TRIGGER_STOP:
1001 return uni_player_stop(player);
1002 case SNDRV_PCM_TRIGGER_RESUME:
1003 return uni_player_resume(player);
1004 default:
1005 return -EINVAL;
1006 }
1007}
1008
1009static void uni_player_shutdown(struct snd_pcm_substream *substream,
1010 struct snd_soc_dai *dai)
1011{
1012 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
1013 struct uniperif *player = priv->dai_data.uni;
1014
1015 if (player->state != UNIPERIF_STATE_STOPPED)
1016 /* Stop the player */
1017 uni_player_stop(player);
Moise Gergaud36a65e22015-11-19 14:54:09 +01001018
1019 player->substream = NULL;
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001020}
1021
Moise Gergaud3ee15ca2016-04-14 15:29:35 +02001022static int uni_player_parse_dt_audio_glue(struct platform_device *pdev,
1023 struct uniperif *player)
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001024{
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001025 struct device_node *node = pdev->dev.of_node;
1026 struct regmap *regmap;
Moise Gergaud3ee15ca2016-04-14 15:29:35 +02001027 struct reg_field regfield[2] = {
1028 /* PCM_CLK_SEL */
1029 REG_FIELD(SYS_CFG_AUDIO_GLUE,
Arnaud Pouliquen5a4326d12016-09-13 09:58:23 +02001030 8 + player->id,
1031 8 + player->id),
Moise Gergaud3ee15ca2016-04-14 15:29:35 +02001032 /* PCMP_VALID_SEL */
1033 REG_FIELD(SYS_CFG_AUDIO_GLUE, 0, 1)
1034 };
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001035
1036 regmap = syscon_regmap_lookup_by_phandle(node, "st,syscfg");
1037
Wei Yongjun7d267dd2016-06-13 14:39:57 +00001038 if (IS_ERR(regmap)) {
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001039 dev_err(&pdev->dev, "sti-audio-clk-glue syscf not found\n");
Wei Yongjun7d267dd2016-06-13 14:39:57 +00001040 return PTR_ERR(regmap);
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001041 }
1042
Moise Gergaud3ee15ca2016-04-14 15:29:35 +02001043 player->clk_sel = regmap_field_alloc(regmap, regfield[0]);
1044 player->valid_sel = regmap_field_alloc(regmap, regfield[1]);
1045
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001046 return 0;
1047}
1048
Lars-Peter Clausen85cf604e2015-07-27 10:56:27 +02001049static const struct snd_soc_dai_ops uni_player_dai_ops = {
Arnaud Pouliquenfa050792015-07-16 11:36:06 +02001050 .startup = uni_player_startup,
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001051 .shutdown = uni_player_shutdown,
1052 .prepare = uni_player_prepare,
1053 .trigger = uni_player_trigger,
1054 .hw_params = sti_uniperiph_dai_hw_params,
1055 .set_fmt = sti_uniperiph_dai_set_fmt,
Moise Gergaud8d8b1e22016-04-07 11:25:35 +02001056 .set_sysclk = uni_player_set_sysclk,
1057 .set_tdm_slot = sti_uniperiph_set_tdm_slot
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001058};
1059
1060int uni_player_init(struct platform_device *pdev,
1061 struct uniperif *player)
1062{
1063 int ret = 0;
1064
1065 player->dev = &pdev->dev;
1066 player->state = UNIPERIF_STATE_STOPPED;
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001067 player->dai_ops = &uni_player_dai_ops;
1068
Arnaud Pouliquen5a4326d12016-09-13 09:58:23 +02001069 /* Get PCM_CLK_SEL & PCMP_VALID_SEL from audio-glue-ctrl SoC reg */
1070 ret = uni_player_parse_dt_audio_glue(pdev, player);
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001071
1072 if (ret < 0) {
1073 dev_err(player->dev, "Failed to parse DeviceTree");
1074 return ret;
1075 }
1076
Arnaud Pouliquen5a4326d12016-09-13 09:58:23 +02001077 /* Underflow recovery is only supported on later ip revisions */
1078 if (player->ver >= SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
1079 player->underflow_enabled = 1;
1080
Moise Gergaud8d8b1e22016-04-07 11:25:35 +02001081 if (UNIPERIF_TYPE_IS_TDM(player))
1082 player->hw = &uni_tdm_hw;
1083 else
1084 player->hw = &uni_player_pcm_hw;
1085
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001086 /* Get uniperif resource */
1087 player->clk = of_clk_get(pdev->dev.of_node, 0);
1088 if (IS_ERR(player->clk))
Arnaud Pouliquened6c75f2015-07-16 11:36:02 +02001089 ret = PTR_ERR(player->clk);
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001090
1091 /* Select the frequency synthesizer clock */
1092 if (player->clk_sel) {
1093 ret = regmap_field_write(player->clk_sel, 1);
1094 if (ret) {
1095 dev_err(player->dev,
1096 "%s: Failed to select freq synth clock",
1097 __func__);
1098 return ret;
1099 }
1100 }
1101
Moise Gergaud3ee15ca2016-04-14 15:29:35 +02001102 /* connect to I2S/TDM TX bus */
1103 if (player->valid_sel &&
Arnaud Pouliquen5a4326d12016-09-13 09:58:23 +02001104 (player->id == UNIPERIF_PLAYER_I2S_OUT)) {
1105 ret = regmap_field_write(player->valid_sel, player->id);
Moise Gergaud3ee15ca2016-04-14 15:29:35 +02001106 if (ret) {
1107 dev_err(player->dev,
1108 "%s: unable to connect to tdm bus", __func__);
1109 return ret;
1110 }
1111 }
1112
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001113 ret = devm_request_irq(&pdev->dev, player->irq,
1114 uni_player_irq_handler, IRQF_SHARED,
1115 dev_name(&pdev->dev), player);
1116 if (ret < 0)
1117 return ret;
1118
Arnaud Pouliquenfa050792015-07-16 11:36:06 +02001119 mutex_init(&player->ctrl_lock);
1120
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001121 /* Ensure that disabled by default */
1122 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
1123 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
1124 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
1125 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
1126
Moise Gergaud5295a0d2016-04-07 11:25:31 +02001127 if (UNIPERIF_TYPE_IS_IEC958(player)) {
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001128 /* Set default iec958 status bits */
1129
1130 /* Consumer, PCM, copyright, 2ch, mode 0 */
1131 player->stream_settings.iec958.status[0] = 0x00;
1132 /* Broadcast reception category */
1133 player->stream_settings.iec958.status[1] =
1134 IEC958_AES1_CON_GENERAL;
1135 /* Do not take into account source or channel number */
1136 player->stream_settings.iec958.status[2] =
1137 IEC958_AES2_CON_SOURCE_UNSPEC;
1138 /* Sampling frequency not indicated */
1139 player->stream_settings.iec958.status[3] =
1140 IEC958_AES3_CON_FS_NOTID;
1141 /* Max sample word 24-bit, sample word length not indicated */
1142 player->stream_settings.iec958.status[4] =
1143 IEC958_AES4_CON_MAX_WORDLEN_24 |
1144 IEC958_AES4_CON_WORDLEN_24_20;
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001145
Arnaud Pouliquen36cc0932015-07-16 11:36:07 +02001146 player->num_ctrls = ARRAY_SIZE(snd_sti_iec_ctl);
1147 player->snd_ctrls = snd_sti_iec_ctl[0];
1148 } else {
1149 player->num_ctrls = ARRAY_SIZE(snd_sti_pcm_ctl);
1150 player->snd_ctrls = snd_sti_pcm_ctl[0];
1151 }
Arnaud Pouliquenfa050792015-07-16 11:36:06 +02001152
Arnaud Pouliquen76c21452015-06-22 16:31:07 +02001153 return 0;
1154}
1155EXPORT_SYMBOL_GPL(uni_player_init);