1
2
3
4
5
6
7
8#include <linux/clk.h>
9#include <linux/mfd/syscon.h>
10
11#include <sound/asoundef.h>
12#include <sound/soc.h>
13
14#include "uniperif.h"
15
16
17
18
19
20
21#define SYS_CFG_AUDIO_GLUE 0xA4
22
23
24
25
26
27#define UNIPERIF_PLAYER_CLK_ADJ_MIN -999999
28#define UNIPERIF_PLAYER_CLK_ADJ_MAX 1000000
29#define UNIPERIF_PLAYER_I2S_OUT 1
30
31
32
33
34
35static const struct snd_pcm_hardware uni_player_pcm_hw = {
36 .info = SNDRV_PCM_INFO_INTERLEAVED | SNDRV_PCM_INFO_BLOCK_TRANSFER |
37 SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_MMAP |
38 SNDRV_PCM_INFO_MMAP_VALID,
39 .formats = SNDRV_PCM_FMTBIT_S32_LE | SNDRV_PCM_FMTBIT_S16_LE,
40
41 .rates = SNDRV_PCM_RATE_CONTINUOUS,
42 .rate_min = 8000,
43 .rate_max = 192000,
44
45 .channels_min = 2,
46 .channels_max = 8,
47
48 .periods_min = 2,
49 .periods_max = 48,
50
51 .period_bytes_min = 128,
52 .period_bytes_max = 64 * PAGE_SIZE,
53 .buffer_bytes_max = 256 * PAGE_SIZE
54};
55
56
57
58
59
60
61static irqreturn_t uni_player_irq_handler(int irq, void *dev_id)
62{
63 irqreturn_t ret = IRQ_NONE;
64 struct uniperif *player = dev_id;
65 unsigned int status;
66 unsigned int tmp;
67
68 spin_lock(&player->irq_lock);
69 if (!player->substream)
70 goto irq_spin_unlock;
71
72 snd_pcm_stream_lock(player->substream);
73 if (player->state == UNIPERIF_STATE_STOPPED)
74 goto stream_unlock;
75
76
77 status = GET_UNIPERIF_ITS(player);
78 SET_UNIPERIF_ITS_BCLR(player, status);
79
80
81 if (unlikely(status & UNIPERIF_ITS_FIFO_ERROR_MASK(player))) {
82 dev_err(player->dev, "FIFO underflow error detected\n");
83
84
85 if (player->underflow_enabled) {
86
87 player->state = UNIPERIF_STATE_UNDERFLOW;
88
89 } else {
90
91 SET_UNIPERIF_ITM_BCLR_FIFO_ERROR(player);
92
93
94 snd_pcm_stop_xrun(player->substream);
95 }
96
97 ret = IRQ_HANDLED;
98 }
99
100
101 if (unlikely(status & UNIPERIF_ITS_DMA_ERROR_MASK(player))) {
102 dev_err(player->dev, "DMA error detected\n");
103
104
105 SET_UNIPERIF_ITM_BCLR_DMA_ERROR(player);
106
107
108 snd_pcm_stop_xrun(player->substream);
109
110 ret = IRQ_HANDLED;
111 }
112
113
114 if (unlikely(status & UNIPERIF_ITM_UNDERFLOW_REC_DONE_MASK(player))) {
115 if (!player->underflow_enabled) {
116 dev_err(player->dev,
117 "unexpected Underflow recovering\n");
118 ret = -EPERM;
119 goto stream_unlock;
120 }
121
122 tmp = GET_UNIPERIF_STATUS_1_UNDERFLOW_DURATION(player);
123 dev_dbg(player->dev, "Underflow recovered (%d LR clocks max)\n",
124 tmp);
125
126
127 SET_UNIPERIF_BIT_CONTROL_CLR_UNDERFLOW_DURATION(player);
128
129
130 player->state = UNIPERIF_STATE_STARTED;
131
132 ret = IRQ_HANDLED;
133 }
134
135
136 if (unlikely(status &
137 UNIPERIF_ITM_UNDERFLOW_REC_FAILED_MASK(player))) {
138 dev_err(player->dev, "Underflow recovery failed\n");
139
140
141 snd_pcm_stop_xrun(player->substream);
142
143 ret = IRQ_HANDLED;
144 }
145
146stream_unlock:
147 snd_pcm_stream_unlock(player->substream);
148irq_spin_unlock:
149 spin_unlock(&player->irq_lock);
150
151 return ret;
152}
153
154static int uni_player_clk_set_rate(struct uniperif *player, unsigned long rate)
155{
156 int rate_adjusted, rate_achieved, delta, ret;
157 int adjustment = player->clk_adj;
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174 if (adjustment < 0) {
175
176 delta = -1;
177 adjustment = -adjustment;
178 } else {
179 delta = 1;
180 }
181
182 delta *= (int)div64_u64((uint64_t)rate *
183 (uint64_t)adjustment + 500000, 1000000);
184 rate_adjusted = rate + delta;
185
186
187 if (!rate_adjusted)
188 return -EINVAL;
189
190 ret = clk_set_rate(player->clk, rate_adjusted);
191 if (ret < 0)
192 return ret;
193
194 rate_achieved = clk_get_rate(player->clk);
195 if (!rate_achieved)
196
197 return -EINVAL;
198
199
200
201
202
203 delta = rate_achieved - rate;
204 if (delta < 0) {
205
206 delta = -delta;
207 adjustment = -1;
208 } else {
209 adjustment = 1;
210 }
211
212 adjustment *= (int)div64_u64((uint64_t)delta * 1000000 + rate / 2,
213 rate);
214 player->clk_adj = adjustment;
215 return 0;
216}
217
218static void uni_player_set_channel_status(struct uniperif *player,
219 struct snd_pcm_runtime *runtime)
220{
221 int n;
222 unsigned int status;
223
224
225
226
227
228
229 mutex_lock(&player->ctrl_lock);
230 if (runtime) {
231 switch (runtime->rate) {
232 case 22050:
233 player->stream_settings.iec958.status[3] =
234 IEC958_AES3_CON_FS_22050;
235 break;
236 case 44100:
237 player->stream_settings.iec958.status[3] =
238 IEC958_AES3_CON_FS_44100;
239 break;
240 case 88200:
241 player->stream_settings.iec958.status[3] =
242 IEC958_AES3_CON_FS_88200;
243 break;
244 case 176400:
245 player->stream_settings.iec958.status[3] =
246 IEC958_AES3_CON_FS_176400;
247 break;
248 case 24000:
249 player->stream_settings.iec958.status[3] =
250 IEC958_AES3_CON_FS_24000;
251 break;
252 case 48000:
253 player->stream_settings.iec958.status[3] =
254 IEC958_AES3_CON_FS_48000;
255 break;
256 case 96000:
257 player->stream_settings.iec958.status[3] =
258 IEC958_AES3_CON_FS_96000;
259 break;
260 case 192000:
261 player->stream_settings.iec958.status[3] =
262 IEC958_AES3_CON_FS_192000;
263 break;
264 case 32000:
265 player->stream_settings.iec958.status[3] =
266 IEC958_AES3_CON_FS_32000;
267 break;
268 default:
269
270 player->stream_settings.iec958.status[3] =
271 IEC958_AES3_CON_FS_NOTID;
272 break;
273 }
274 }
275
276
277
278
279 if (player->stream_settings.iec958.status[0] & IEC958_AES0_NONAUDIO)
280 player->stream_settings.encoding_mode =
281 UNIPERIF_IEC958_ENCODING_MODE_ENCODED;
282 else
283 player->stream_settings.encoding_mode =
284 UNIPERIF_IEC958_ENCODING_MODE_PCM;
285
286 if (player->stream_settings.encoding_mode ==
287 UNIPERIF_IEC958_ENCODING_MODE_PCM)
288
289 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0);
290 else
291
292 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 1);
293
294
295 for (n = 0; n < 6; ++n) {
296 status =
297 player->stream_settings.iec958.status[0 + (n * 4)] & 0xf;
298 status |=
299 player->stream_settings.iec958.status[1 + (n * 4)] << 8;
300 status |=
301 player->stream_settings.iec958.status[2 + (n * 4)] << 16;
302 status |=
303 player->stream_settings.iec958.status[3 + (n * 4)] << 24;
304 SET_UNIPERIF_CHANNEL_STA_REGN(player, n, status);
305 }
306 mutex_unlock(&player->ctrl_lock);
307
308
309 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
310 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player);
311 else
312 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player);
313}
314
315static int uni_player_prepare_iec958(struct uniperif *player,
316 struct snd_pcm_runtime *runtime)
317{
318 int clk_div;
319
320 clk_div = player->mclk / runtime->rate;
321
322
323 if ((clk_div % 128) || (clk_div <= 0)) {
324 dev_err(player->dev, "%s: invalid clk_div %d\n",
325 __func__, clk_div);
326 return -EINVAL;
327 }
328
329 switch (runtime->format) {
330 case SNDRV_PCM_FORMAT_S16_LE:
331
332 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player);
333
334 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
335
336 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player);
337 break;
338 case SNDRV_PCM_FORMAT_S32_LE:
339
340 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
341
342 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
343
344 SET_UNIPERIF_I2S_FMT_DATA_SIZE_24(player);
345 break;
346 default:
347 dev_err(player->dev, "format not supported\n");
348 return -EINVAL;
349 }
350
351
352 SET_UNIPERIF_CONFIG_PARITY_CNTR_BY_HW(player);
353
354
355 SET_UNIPERIF_CONFIG_CHANNEL_STA_CNTR_BY_HW(player);
356
357
358 SET_UNIPERIF_CONFIG_USER_DAT_CNTR_BY_HW(player);
359
360
361 SET_UNIPERIF_CONFIG_VALIDITY_DAT_CNTR_BY_HW(player);
362
363
364 SET_UNIPERIF_CONFIG_SPDIF_SW_CTRL_DISABLE(player);
365
366 SET_UNIPERIF_CTRL_ZERO_STUFF_HW(player);
367
368
369 uni_player_set_channel_status(player, runtime);
370
371
372 SET_UNIPERIF_USER_VALIDITY_VALIDITY_LR(player, 0);
373
374
375 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player);
376
377
378 SET_UNIPERIF_CONFIG_REPEAT_CHL_STS_ENABLE(player);
379
380
381 SET_UNIPERIF_CONFIG_SUBFRAME_SEL_SUBF1_SUBF0(player);
382
383
384 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
385
386 if (player->stream_settings.encoding_mode ==
387 UNIPERIF_IEC958_ENCODING_MODE_ENCODED)
388 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_ON(player);
389 else
390 SET_UNIPERIF_CTRL_EXIT_STBY_ON_EOBLOCK_OFF(player);
391
392 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2);
393
394
395 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
396
397
398 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / 128);
399
400
401 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
402
403
404
405
406
407
408 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
409 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player);
410 else
411 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player);
412
413 return 0;
414}
415
416static int uni_player_prepare_pcm(struct uniperif *player,
417 struct snd_pcm_runtime *runtime)
418{
419 int output_frame_size, slot_width, clk_div;
420
421
422 if ((player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) ==
423 SND_SOC_DAIFMT_I2S)
424 slot_width = 32;
425 else
426 slot_width = snd_pcm_format_width(runtime->format);
427
428 output_frame_size = slot_width * runtime->channels;
429
430 clk_div = player->mclk / runtime->rate;
431
432
433
434
435 if ((slot_width == 32) && (clk_div % 128)) {
436 dev_err(player->dev, "%s: invalid clk_div\n", __func__);
437 return -EINVAL;
438 }
439
440 if ((slot_width == 16) && (clk_div % 64)) {
441 dev_err(player->dev, "%s: invalid clk_div\n", __func__);
442 return -EINVAL;
443 }
444
445
446
447
448
449 switch (slot_width) {
450 case 32:
451 SET_UNIPERIF_I2S_FMT_NBIT_32(player);
452 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player);
453 break;
454 case 16:
455 SET_UNIPERIF_I2S_FMT_NBIT_16(player);
456 SET_UNIPERIF_I2S_FMT_DATA_SIZE_16(player);
457 break;
458 default:
459 dev_err(player->dev, "subframe format not supported\n");
460 return -EINVAL;
461 }
462
463
464 switch (runtime->format) {
465 case SNDRV_PCM_FORMAT_S16_LE:
466
467 SET_UNIPERIF_CONFIG_MEM_FMT_16_16(player);
468 break;
469
470 case SNDRV_PCM_FORMAT_S32_LE:
471
472
473
474
475 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
476 break;
477
478 default:
479 dev_err(player->dev, "format not supported\n");
480 return -EINVAL;
481 }
482
483
484 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
485
486
487 SET_UNIPERIF_CTRL_DIVIDER(player, clk_div / (2 * output_frame_size));
488
489
490 if ((runtime->channels % 2) || (runtime->channels < 2) ||
491 (runtime->channels > 10)) {
492 dev_err(player->dev, "%s: invalid nb of channels\n", __func__);
493 return -EINVAL;
494 }
495
496 SET_UNIPERIF_I2S_FMT_NUM_CH(player, runtime->channels / 2);
497
498
499 SET_UNIPERIF_CONFIG_ONE_BIT_AUD_DISABLE(player);
500
501 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
502
503
504 SET_UNIPERIF_CTRL_SPDIF_FMT_OFF(player);
505
506 return 0;
507}
508
509static int uni_player_prepare_tdm(struct uniperif *player,
510 struct snd_pcm_runtime *runtime)
511{
512 int tdm_frame_size;
513 int user_frame_size;
514
515 unsigned int word_pos[4] = {
516 0x04060002, 0x0C0E080A, 0x14161012, 0x1C1E181A};
517 int freq, ret;
518
519 tdm_frame_size =
520 sti_uniperiph_get_unip_tdm_frame_size(player);
521 user_frame_size =
522 sti_uniperiph_get_user_frame_size(runtime);
523
524
525 SET_UNIPERIF_CONFIG_MEM_FMT_16_0(player);
526 SET_UNIPERIF_I2S_FMT_DATA_SIZE_32(player);
527
528
529 SET_UNIPERIF_I2S_FMT_NUM_CH(player, user_frame_size / 4 / 2);
530
531 SET_UNIPERIF_I2S_FMT_ORDER_MSB(player);
532 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
533
534
535 SET_UNIPERIF_TDM_ENABLE_TDM_ENABLE(player);
536
537
538 SET_UNIPERIF_TDM_FS_REF_DIV_NUM_TIMESLOT(player, tdm_frame_size);
539
540
541 sti_uniperiph_get_tdm_word_pos(player, word_pos);
542 SET_UNIPERIF_TDM_WORD_POS(player, 1_2, word_pos[WORD_1_2]);
543 SET_UNIPERIF_TDM_WORD_POS(player, 3_4, word_pos[WORD_3_4]);
544 SET_UNIPERIF_TDM_WORD_POS(player, 5_6, word_pos[WORD_5_6]);
545 SET_UNIPERIF_TDM_WORD_POS(player, 7_8, word_pos[WORD_7_8]);
546
547
548 freq = runtime->rate * tdm_frame_size * 8;
549 mutex_lock(&player->ctrl_lock);
550 ret = uni_player_clk_set_rate(player, freq);
551 if (!ret)
552 player->mclk = freq;
553 mutex_unlock(&player->ctrl_lock);
554
555 return 0;
556}
557
558
559
560
561static int uni_player_ctl_iec958_info(struct snd_kcontrol *kcontrol,
562 struct snd_ctl_elem_info *uinfo)
563{
564 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
565 uinfo->count = 1;
566
567 return 0;
568}
569
570static int uni_player_ctl_iec958_get(struct snd_kcontrol *kcontrol,
571 struct snd_ctl_elem_value *ucontrol)
572{
573 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
574 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
575 struct uniperif *player = priv->dai_data.uni;
576 struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958;
577
578 mutex_lock(&player->ctrl_lock);
579 ucontrol->value.iec958.status[0] = iec958->status[0];
580 ucontrol->value.iec958.status[1] = iec958->status[1];
581 ucontrol->value.iec958.status[2] = iec958->status[2];
582 ucontrol->value.iec958.status[3] = iec958->status[3];
583 mutex_unlock(&player->ctrl_lock);
584 return 0;
585}
586
587static int uni_player_ctl_iec958_put(struct snd_kcontrol *kcontrol,
588 struct snd_ctl_elem_value *ucontrol)
589{
590 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
591 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
592 struct uniperif *player = priv->dai_data.uni;
593 struct snd_aes_iec958 *iec958 = &player->stream_settings.iec958;
594 unsigned long flags;
595
596 mutex_lock(&player->ctrl_lock);
597 iec958->status[0] = ucontrol->value.iec958.status[0];
598 iec958->status[1] = ucontrol->value.iec958.status[1];
599 iec958->status[2] = ucontrol->value.iec958.status[2];
600 iec958->status[3] = ucontrol->value.iec958.status[3];
601 mutex_unlock(&player->ctrl_lock);
602
603 spin_lock_irqsave(&player->irq_lock, flags);
604 if (player->substream && player->substream->runtime)
605 uni_player_set_channel_status(player,
606 player->substream->runtime);
607 else
608 uni_player_set_channel_status(player, NULL);
609
610 spin_unlock_irqrestore(&player->irq_lock, flags);
611 return 0;
612}
613
614static struct snd_kcontrol_new uni_player_iec958_ctl = {
615 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
616 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
617 .info = uni_player_ctl_iec958_info,
618 .get = uni_player_ctl_iec958_get,
619 .put = uni_player_ctl_iec958_put,
620};
621
622
623
624
625static int snd_sti_clk_adjustment_info(struct snd_kcontrol *kcontrol,
626 struct snd_ctl_elem_info *uinfo)
627{
628 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
629 uinfo->count = 1;
630 uinfo->value.integer.min = UNIPERIF_PLAYER_CLK_ADJ_MIN;
631 uinfo->value.integer.max = UNIPERIF_PLAYER_CLK_ADJ_MAX;
632 uinfo->value.integer.step = 1;
633
634 return 0;
635}
636
637static int snd_sti_clk_adjustment_get(struct snd_kcontrol *kcontrol,
638 struct snd_ctl_elem_value *ucontrol)
639{
640 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
641 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
642 struct uniperif *player = priv->dai_data.uni;
643
644 mutex_lock(&player->ctrl_lock);
645 ucontrol->value.integer.value[0] = player->clk_adj;
646 mutex_unlock(&player->ctrl_lock);
647
648 return 0;
649}
650
651static int snd_sti_clk_adjustment_put(struct snd_kcontrol *kcontrol,
652 struct snd_ctl_elem_value *ucontrol)
653{
654 struct snd_soc_dai *dai = snd_kcontrol_chip(kcontrol);
655 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
656 struct uniperif *player = priv->dai_data.uni;
657 int ret = 0;
658
659 if ((ucontrol->value.integer.value[0] < UNIPERIF_PLAYER_CLK_ADJ_MIN) ||
660 (ucontrol->value.integer.value[0] > UNIPERIF_PLAYER_CLK_ADJ_MAX))
661 return -EINVAL;
662
663 mutex_lock(&player->ctrl_lock);
664 player->clk_adj = ucontrol->value.integer.value[0];
665
666 if (player->mclk)
667 ret = uni_player_clk_set_rate(player, player->mclk);
668 mutex_unlock(&player->ctrl_lock);
669
670 return ret;
671}
672
673static struct snd_kcontrol_new uni_player_clk_adj_ctl = {
674 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
675 .name = "PCM Playback Oversampling Freq. Adjustment",
676 .info = snd_sti_clk_adjustment_info,
677 .get = snd_sti_clk_adjustment_get,
678 .put = snd_sti_clk_adjustment_put,
679};
680
681static struct snd_kcontrol_new *snd_sti_pcm_ctl[] = {
682 &uni_player_clk_adj_ctl,
683};
684
685static struct snd_kcontrol_new *snd_sti_iec_ctl[] = {
686 &uni_player_iec958_ctl,
687 &uni_player_clk_adj_ctl,
688};
689
690static int uni_player_startup(struct snd_pcm_substream *substream,
691 struct snd_soc_dai *dai)
692{
693 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
694 struct uniperif *player = priv->dai_data.uni;
695 unsigned long flags;
696 int ret;
697
698 spin_lock_irqsave(&player->irq_lock, flags);
699 player->substream = substream;
700 spin_unlock_irqrestore(&player->irq_lock, flags);
701
702 player->clk_adj = 0;
703
704 if (!UNIPERIF_TYPE_IS_TDM(player))
705 return 0;
706
707
708 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
709 SNDRV_PCM_HW_PARAM_CHANNELS,
710 sti_uniperiph_fix_tdm_chan,
711 player, SNDRV_PCM_HW_PARAM_CHANNELS,
712 -1);
713 if (ret < 0)
714 return ret;
715
716 return snd_pcm_hw_rule_add(substream->runtime, 0,
717 SNDRV_PCM_HW_PARAM_FORMAT,
718 sti_uniperiph_fix_tdm_format,
719 player, SNDRV_PCM_HW_PARAM_FORMAT,
720 -1);
721}
722
723static int uni_player_set_sysclk(struct snd_soc_dai *dai, int clk_id,
724 unsigned int freq, int dir)
725{
726 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
727 struct uniperif *player = priv->dai_data.uni;
728 int ret;
729
730 if (UNIPERIF_TYPE_IS_TDM(player) || (dir == SND_SOC_CLOCK_IN))
731 return 0;
732
733 if (clk_id != 0)
734 return -EINVAL;
735
736 mutex_lock(&player->ctrl_lock);
737 ret = uni_player_clk_set_rate(player, freq);
738 if (!ret)
739 player->mclk = freq;
740 mutex_unlock(&player->ctrl_lock);
741
742 return ret;
743}
744
745static int uni_player_prepare(struct snd_pcm_substream *substream,
746 struct snd_soc_dai *dai)
747{
748 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
749 struct uniperif *player = priv->dai_data.uni;
750 struct snd_pcm_runtime *runtime = substream->runtime;
751 int transfer_size, trigger_limit;
752 int ret;
753
754
755 if (player->state != UNIPERIF_STATE_STOPPED) {
756 dev_err(player->dev, "%s: invalid player state %d\n", __func__,
757 player->state);
758 return -EINVAL;
759 }
760
761
762 if (player->type == SND_ST_UNIPERIF_TYPE_TDM) {
763
764 transfer_size =
765 sti_uniperiph_get_user_frame_size(runtime) / 4;
766 } else {
767 transfer_size = runtime->channels * UNIPERIF_FIFO_FRAMES;
768 }
769
770
771 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0) {
772 trigger_limit = UNIPERIF_FIFO_SIZE - transfer_size;
773 } else {
774
775
776
777
778
779 trigger_limit = transfer_size;
780 }
781
782
783 if ((!trigger_limit % 2) || (trigger_limit != 1 && transfer_size % 2) ||
784 (trigger_limit > UNIPERIF_CONFIG_DMA_TRIG_LIMIT_MASK(player))) {
785 dev_err(player->dev, "invalid trigger limit %d\n",
786 trigger_limit);
787 return -EINVAL;
788 }
789
790 SET_UNIPERIF_CONFIG_DMA_TRIG_LIMIT(player, trigger_limit);
791
792
793 switch (player->type) {
794 case SND_ST_UNIPERIF_TYPE_HDMI:
795 ret = uni_player_prepare_iec958(player, runtime);
796 break;
797 case SND_ST_UNIPERIF_TYPE_PCM:
798 ret = uni_player_prepare_pcm(player, runtime);
799 break;
800 case SND_ST_UNIPERIF_TYPE_SPDIF:
801 ret = uni_player_prepare_iec958(player, runtime);
802 break;
803 case SND_ST_UNIPERIF_TYPE_TDM:
804 ret = uni_player_prepare_tdm(player, runtime);
805 break;
806 default:
807 dev_err(player->dev, "invalid player type\n");
808 return -EINVAL;
809 }
810
811 if (ret)
812 return ret;
813
814 switch (player->daifmt & SND_SOC_DAIFMT_INV_MASK) {
815 case SND_SOC_DAIFMT_NB_NF:
816 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
817 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
818 break;
819 case SND_SOC_DAIFMT_NB_IF:
820 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
821 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_RISING(player);
822 break;
823 case SND_SOC_DAIFMT_IB_NF:
824 SET_UNIPERIF_I2S_FMT_LR_POL_LOW(player);
825 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
826 break;
827 case SND_SOC_DAIFMT_IB_IF:
828 SET_UNIPERIF_I2S_FMT_LR_POL_HIG(player);
829 SET_UNIPERIF_I2S_FMT_SCLK_EDGE_FALLING(player);
830 break;
831 }
832
833 switch (player->daifmt & SND_SOC_DAIFMT_FORMAT_MASK) {
834 case SND_SOC_DAIFMT_I2S:
835 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
836 SET_UNIPERIF_I2S_FMT_PADDING_I2S_MODE(player);
837 break;
838 case SND_SOC_DAIFMT_LEFT_J:
839 SET_UNIPERIF_I2S_FMT_ALIGN_LEFT(player);
840 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
841 break;
842 case SND_SOC_DAIFMT_RIGHT_J:
843 SET_UNIPERIF_I2S_FMT_ALIGN_RIGHT(player);
844 SET_UNIPERIF_I2S_FMT_PADDING_SONY_MODE(player);
845 break;
846 default:
847 dev_err(player->dev, "format not supported\n");
848 return -EINVAL;
849 }
850
851 SET_UNIPERIF_I2S_FMT_NO_OF_SAMPLES_TO_READ(player, 0);
852
853
854 return sti_uniperiph_reset(player);
855}
856
857static int uni_player_start(struct uniperif *player)
858{
859 int ret;
860
861
862 if (player->state != UNIPERIF_STATE_STOPPED) {
863 dev_err(player->dev, "%s: invalid player state\n", __func__);
864 return -EINVAL;
865 }
866
867 ret = clk_prepare_enable(player->clk);
868 if (ret) {
869 dev_err(player->dev, "%s: Failed to enable clock\n", __func__);
870 return ret;
871 }
872
873
874 SET_UNIPERIF_ITS_BCLR(player, GET_UNIPERIF_ITS(player));
875
876
877 SET_UNIPERIF_ITM_BSET_DMA_ERROR(player);
878 SET_UNIPERIF_ITM_BSET_FIFO_ERROR(player);
879
880
881 if (player->underflow_enabled) {
882 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_DONE(player);
883 SET_UNIPERIF_ITM_BSET_UNDERFLOW_REC_FAILED(player);
884 }
885
886 ret = sti_uniperiph_reset(player);
887 if (ret < 0) {
888 clk_disable_unprepare(player->clk);
889 return ret;
890 }
891
892
893
894
895
896
897
898
899 SET_UNIPERIF_CTRL_OPERATION_PCM_DATA(player);
900
901
902
903
904
905
906 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
907 if (UNIPERIF_TYPE_IS_IEC958(player))
908 SET_UNIPERIF_CTRL_SPDIF_FMT_ON(player);
909
910
911 if (player->ver < SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
912 SET_UNIPERIF_CONFIG_CHL_STS_UPDATE(player);
913 else
914 SET_UNIPERIF_BIT_CONTROL_CHL_STS_UPDATE(player);
915
916
917 player->state = UNIPERIF_STATE_STARTED;
918
919 return 0;
920}
921
922static int uni_player_stop(struct uniperif *player)
923{
924 int ret;
925
926
927 if (player->state == UNIPERIF_STATE_STOPPED) {
928 dev_err(player->dev, "%s: invalid player state\n", __func__);
929 return -EINVAL;
930 }
931
932
933 SET_UNIPERIF_CTRL_OPERATION_OFF(player);
934
935 ret = sti_uniperiph_reset(player);
936 if (ret < 0)
937 return ret;
938
939
940 SET_UNIPERIF_ITM_BCLR(player, GET_UNIPERIF_ITM(player));
941
942
943 clk_disable_unprepare(player->clk);
944
945
946 player->state = UNIPERIF_STATE_STOPPED;
947
948 return 0;
949}
950
951int uni_player_resume(struct uniperif *player)
952{
953 int ret;
954
955
956 if (player->clk_sel) {
957 ret = regmap_field_write(player->clk_sel, 1);
958 if (ret) {
959 dev_err(player->dev,
960 "%s: Failed to select freq synth clock\n",
961 __func__);
962 return ret;
963 }
964 }
965
966 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
967 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
968 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
969 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
970
971 return 0;
972}
973EXPORT_SYMBOL_GPL(uni_player_resume);
974
975static int uni_player_trigger(struct snd_pcm_substream *substream,
976 int cmd, struct snd_soc_dai *dai)
977{
978 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
979 struct uniperif *player = priv->dai_data.uni;
980
981 switch (cmd) {
982 case SNDRV_PCM_TRIGGER_START:
983 return uni_player_start(player);
984 case SNDRV_PCM_TRIGGER_STOP:
985 return uni_player_stop(player);
986 case SNDRV_PCM_TRIGGER_RESUME:
987 return uni_player_resume(player);
988 default:
989 return -EINVAL;
990 }
991}
992
993static void uni_player_shutdown(struct snd_pcm_substream *substream,
994 struct snd_soc_dai *dai)
995{
996 struct sti_uniperiph_data *priv = snd_soc_dai_get_drvdata(dai);
997 struct uniperif *player = priv->dai_data.uni;
998 unsigned long flags;
999
1000 spin_lock_irqsave(&player->irq_lock, flags);
1001 if (player->state != UNIPERIF_STATE_STOPPED)
1002
1003 uni_player_stop(player);
1004
1005 player->substream = NULL;
1006 spin_unlock_irqrestore(&player->irq_lock, flags);
1007}
1008
1009static int uni_player_parse_dt_audio_glue(struct platform_device *pdev,
1010 struct uniperif *player)
1011{
1012 struct device_node *node = pdev->dev.of_node;
1013 struct regmap *regmap;
1014 struct reg_field regfield[2] = {
1015
1016 REG_FIELD(SYS_CFG_AUDIO_GLUE,
1017 8 + player->id,
1018 8 + player->id),
1019
1020 REG_FIELD(SYS_CFG_AUDIO_GLUE, 0, 1)
1021 };
1022
1023 regmap = syscon_regmap_lookup_by_phandle(node, "st,syscfg");
1024
1025 if (IS_ERR(regmap)) {
1026 dev_err(&pdev->dev, "sti-audio-clk-glue syscf not found\n");
1027 return PTR_ERR(regmap);
1028 }
1029
1030 player->clk_sel = regmap_field_alloc(regmap, regfield[0]);
1031 player->valid_sel = regmap_field_alloc(regmap, regfield[1]);
1032
1033 return 0;
1034}
1035
1036static const struct snd_soc_dai_ops uni_player_dai_ops = {
1037 .startup = uni_player_startup,
1038 .shutdown = uni_player_shutdown,
1039 .prepare = uni_player_prepare,
1040 .trigger = uni_player_trigger,
1041 .hw_params = sti_uniperiph_dai_hw_params,
1042 .set_fmt = sti_uniperiph_dai_set_fmt,
1043 .set_sysclk = uni_player_set_sysclk,
1044 .set_tdm_slot = sti_uniperiph_set_tdm_slot
1045};
1046
1047int uni_player_init(struct platform_device *pdev,
1048 struct uniperif *player)
1049{
1050 int ret = 0;
1051
1052 player->dev = &pdev->dev;
1053 player->state = UNIPERIF_STATE_STOPPED;
1054 player->dai_ops = &uni_player_dai_ops;
1055
1056
1057 ret = uni_player_parse_dt_audio_glue(pdev, player);
1058
1059 if (ret < 0) {
1060 dev_err(player->dev, "Failed to parse DeviceTree\n");
1061 return ret;
1062 }
1063
1064
1065 if (player->ver >= SND_ST_UNIPERIF_VERSION_UNI_PLR_TOP_1_0)
1066 player->underflow_enabled = 1;
1067
1068 if (UNIPERIF_TYPE_IS_TDM(player))
1069 player->hw = &uni_tdm_hw;
1070 else
1071 player->hw = &uni_player_pcm_hw;
1072
1073
1074 player->clk = of_clk_get(pdev->dev.of_node, 0);
1075 if (IS_ERR(player->clk)) {
1076 dev_err(player->dev, "Failed to get clock\n");
1077 return PTR_ERR(player->clk);
1078 }
1079
1080
1081 if (player->clk_sel) {
1082 ret = regmap_field_write(player->clk_sel, 1);
1083 if (ret) {
1084 dev_err(player->dev,
1085 "%s: Failed to select freq synth clock\n",
1086 __func__);
1087 return ret;
1088 }
1089 }
1090
1091
1092 if (player->valid_sel &&
1093 (player->id == UNIPERIF_PLAYER_I2S_OUT)) {
1094 ret = regmap_field_write(player->valid_sel, player->id);
1095 if (ret) {
1096 dev_err(player->dev,
1097 "%s: unable to connect to tdm bus\n", __func__);
1098 return ret;
1099 }
1100 }
1101
1102 ret = devm_request_irq(&pdev->dev, player->irq,
1103 uni_player_irq_handler, IRQF_SHARED,
1104 dev_name(&pdev->dev), player);
1105 if (ret < 0) {
1106 dev_err(player->dev, "unable to request IRQ %d\n", player->irq);
1107 return ret;
1108 }
1109
1110 mutex_init(&player->ctrl_lock);
1111 spin_lock_init(&player->irq_lock);
1112
1113
1114 SET_UNIPERIF_CONFIG_BACK_STALL_REQ_DISABLE(player);
1115 SET_UNIPERIF_CTRL_ROUNDING_OFF(player);
1116 SET_UNIPERIF_CTRL_SPDIF_LAT_OFF(player);
1117 SET_UNIPERIF_CONFIG_IDLE_MOD_DISABLE(player);
1118
1119 if (UNIPERIF_TYPE_IS_IEC958(player)) {
1120
1121
1122
1123 player->stream_settings.iec958.status[0] = 0x00;
1124
1125 player->stream_settings.iec958.status[1] =
1126 IEC958_AES1_CON_GENERAL;
1127
1128 player->stream_settings.iec958.status[2] =
1129 IEC958_AES2_CON_SOURCE_UNSPEC;
1130
1131 player->stream_settings.iec958.status[3] =
1132 IEC958_AES3_CON_FS_NOTID;
1133
1134 player->stream_settings.iec958.status[4] =
1135 IEC958_AES4_CON_MAX_WORDLEN_24 |
1136 IEC958_AES4_CON_WORDLEN_24_20;
1137
1138 player->num_ctrls = ARRAY_SIZE(snd_sti_iec_ctl);
1139 player->snd_ctrls = snd_sti_iec_ctl[0];
1140 } else {
1141 player->num_ctrls = ARRAY_SIZE(snd_sti_pcm_ctl);
1142 player->snd_ctrls = snd_sti_pcm_ctl[0];
1143 }
1144
1145 return 0;
1146}
1147EXPORT_SYMBOL_GPL(uni_player_init);
1148