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