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