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