]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - sound/x86/intel_hdmi_audio.c
ALSA: x86: Drop unused hdmi_audio_query()
[mirror_ubuntu-bionic-kernel.git] / sound / x86 / intel_hdmi_audio.c
CommitLineData
5dab11d8
JA
1/*
2 * intel_hdmi_audio.c - Intel HDMI audio driver
3 *
4 * Copyright (C) 2016 Intel Corp
5 * Authors: Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>
6 * Ramesh Babu K V <ramesh.babu@intel.com>
7 * Vaibhav Agarwal <vaibhav.agarwal@intel.com>
8 * Jerome Anand <jerome.anand@intel.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 * ALSA driver for Intel HDMI audio
22 */
23
24#define pr_fmt(fmt) "had: " fmt
25
26#include <linux/platform_device.h>
27#include <linux/io.h>
28#include <linux/slab.h>
29#include <linux/module.h>
30#include <linux/acpi.h>
31#include <asm/cacheflush.h>
32#include <sound/pcm.h>
33#include <sound/core.h>
34#include <sound/pcm_params.h>
35#include <sound/initval.h>
36#include <sound/control.h>
37#include <sound/initval.h>
38#include "intel_hdmi_audio.h"
39
5dab11d8
JA
40/*standard module options for ALSA. This module supports only one card*/
41static int hdmi_card_index = SNDRV_DEFAULT_IDX1;
42static char *hdmi_card_id = SNDRV_DEFAULT_STR1;
5dab11d8
JA
43
44module_param_named(index, hdmi_card_index, int, 0444);
45MODULE_PARM_DESC(index,
46 "Index value for INTEL Intel HDMI Audio controller.");
47module_param_named(id, hdmi_card_id, charp, 0444);
48MODULE_PARM_DESC(id,
49 "ID string for INTEL Intel HDMI Audio controller.");
50
51/*
52 * ELD SA bits in the CEA Speaker Allocation data block
53 */
54static int eld_speaker_allocation_bits[] = {
55 [0] = FL | FR,
56 [1] = LFE,
57 [2] = FC,
58 [3] = RL | RR,
59 [4] = RC,
60 [5] = FLC | FRC,
61 [6] = RLC | RRC,
62 /* the following are not defined in ELD yet */
63 [7] = 0,
64};
65
66/*
67 * This is an ordered list!
68 *
69 * The preceding ones have better chances to be selected by
70 * hdmi_channel_allocation().
71 */
72static struct cea_channel_speaker_allocation channel_allocations[] = {
73/* channel: 7 6 5 4 3 2 1 0 */
74{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
75 /* 2.1 */
76{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
77 /* Dolby Surround */
78{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
79 /* surround40 */
80{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
81 /* surround41 */
82{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
83 /* surround50 */
84{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
85 /* surround51 */
86{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
87 /* 6.1 */
88{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
89 /* surround71 */
90{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
91
92{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
93{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
94{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
95{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
96{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
97{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
98{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
99{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
100{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
101{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
102{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
103{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
104{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
105{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
106{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
107{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
108{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
109{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
110{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
111{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
112{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
113{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
114{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
115};
116
117static struct channel_map_table map_tables[] = {
118 { SNDRV_CHMAP_FL, 0x00, FL },
119 { SNDRV_CHMAP_FR, 0x01, FR },
120 { SNDRV_CHMAP_RL, 0x04, RL },
121 { SNDRV_CHMAP_RR, 0x05, RR },
122 { SNDRV_CHMAP_LFE, 0x02, LFE },
123 { SNDRV_CHMAP_FC, 0x03, FC },
124 { SNDRV_CHMAP_RLC, 0x06, RLC },
125 { SNDRV_CHMAP_RRC, 0x07, RRC },
126 {} /* terminator */
127};
128
129/* hardware capability structure */
130static const struct snd_pcm_hardware snd_intel_hadstream = {
131 .info = (SNDRV_PCM_INFO_INTERLEAVED |
132 SNDRV_PCM_INFO_DOUBLE |
133 SNDRV_PCM_INFO_MMAP|
134 SNDRV_PCM_INFO_MMAP_VALID |
135 SNDRV_PCM_INFO_BATCH),
136 .formats = (SNDRV_PCM_FMTBIT_S24 |
137 SNDRV_PCM_FMTBIT_U24),
138 .rates = SNDRV_PCM_RATE_32000 |
139 SNDRV_PCM_RATE_44100 |
140 SNDRV_PCM_RATE_48000 |
141 SNDRV_PCM_RATE_88200 |
142 SNDRV_PCM_RATE_96000 |
143 SNDRV_PCM_RATE_176400 |
144 SNDRV_PCM_RATE_192000,
145 .rate_min = HAD_MIN_RATE,
146 .rate_max = HAD_MAX_RATE,
147 .channels_min = HAD_MIN_CHANNEL,
148 .channels_max = HAD_MAX_CHANNEL,
149 .buffer_bytes_max = HAD_MAX_BUFFER,
150 .period_bytes_min = HAD_MIN_PERIOD_BYTES,
151 .period_bytes_max = HAD_MAX_PERIOD_BYTES,
152 .periods_min = HAD_MIN_PERIODS,
153 .periods_max = HAD_MAX_PERIODS,
154 .fifo_size = HAD_FIFO_SIZE,
155};
156
157/* Register access functions */
158
159int had_get_hwstate(struct snd_intelhad *intelhaddata)
160{
161 /* Check for device presence -SW state */
162 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
163 pr_debug("%s:Device not connected:%d\n", __func__,
164 intelhaddata->drv_status);
165 return -ENODEV;
166 }
167
168 return 0;
169}
170
79dda75a
TI
171int had_get_caps(struct snd_intelhad *intelhaddata,
172 enum had_caps_list query, void *caps)
5dab11d8 173{
bf8b24f8 174 struct platform_device *pdev = to_platform_device(intelhaddata->dev);
5dab11d8 175 int retval;
5dab11d8
JA
176
177 retval = had_get_hwstate(intelhaddata);
178 if (!retval)
bf8b24f8 179 retval = mid_hdmi_audio_get_caps(pdev, query, caps);
5dab11d8
JA
180
181 return retval;
182}
183
79dda75a
TI
184int had_set_caps(struct snd_intelhad *intelhaddata,
185 enum had_caps_list set_element, void *caps)
5dab11d8 186{
bf8b24f8 187 struct platform_device *pdev = to_platform_device(intelhaddata->dev);
5dab11d8 188 int retval;
5dab11d8
JA
189
190 retval = had_get_hwstate(intelhaddata);
191 if (!retval)
bf8b24f8 192 retval = mid_hdmi_audio_set_caps(pdev, set_element, caps);
5dab11d8
JA
193
194 return retval;
195}
196
79dda75a 197int had_read_register(struct snd_intelhad *intelhaddata, u32 offset, u32 *data)
5dab11d8 198{
bf8b24f8 199 struct platform_device *pdev = to_platform_device(intelhaddata->dev);
5dab11d8 200 int retval;
5dab11d8
JA
201
202 retval = had_get_hwstate(intelhaddata);
203 if (!retval)
bf8b24f8 204 retval = mid_hdmi_audio_read(pdev, offset, data);
5dab11d8
JA
205
206 return retval;
207}
208
79dda75a 209int had_write_register(struct snd_intelhad *intelhaddata, u32 offset, u32 data)
5dab11d8 210{
bf8b24f8 211 struct platform_device *pdev = to_platform_device(intelhaddata->dev);
5dab11d8 212 int retval;
5dab11d8
JA
213
214 retval = had_get_hwstate(intelhaddata);
215 if (!retval)
bf8b24f8 216 retval = mid_hdmi_audio_write(pdev, offset, data);
5dab11d8
JA
217
218 return retval;
219}
220
79dda75a
TI
221int had_read_modify(struct snd_intelhad *intelhaddata, u32 offset,
222 u32 data, u32 mask)
5dab11d8 223{
bf8b24f8 224 struct platform_device *pdev = to_platform_device(intelhaddata->dev);
5dab11d8 225 int retval;
5dab11d8
JA
226
227 retval = had_get_hwstate(intelhaddata);
228 if (!retval)
bf8b24f8 229 retval = mid_hdmi_audio_rmw(pdev, offset, data, mask);
5dab11d8
JA
230
231 return retval;
232}
233/**
234 * function to read-modify
235 * AUD_CONFIG register on VLV2.The had_read_modify() function should not
236 * directly be used on VLV2 for updating AUD_CONFIG register.
237 * This is because:
238 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
239 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
240 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
241 * register. This field should be 1xy binary for configuration with 6 or
242 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
243 * causes the "channels" field to be updated as 0xy binary resulting in
244 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
245 * appropriate value when doing read-modify of AUD_CONFIG register.
246 *
247 * @substream: the current substream or NULL if no active substream
248 * @data : data to be written
249 * @mask : mask
250 *
251 */
252static int had_read_modify_aud_config_v2(struct snd_pcm_substream *substream,
253 u32 data, u32 mask)
254{
79dda75a 255 struct snd_intelhad *intelhaddata = snd_pcm_substream_chip(substream);
5dab11d8
JA
256 union aud_cfg cfg_val = {.cfg_regval = 0};
257 u8 channels;
258
259 /*
260 * If substream is NULL, there is no active stream.
261 * In this case just set channels to 2
262 */
263 if (substream)
264 channels = substream->runtime->channels;
265 else
266 channels = 2;
267 cfg_val.cfg_regx_v2.num_ch = channels - 2;
268
269 data = data | cfg_val.cfg_regval;
270 mask = mask | AUD_CONFIG_CH_MASK_V2;
271
272 pr_debug("%s : data = %x, mask =%x\n", __func__, data, mask);
273
79dda75a 274 return had_read_modify(intelhaddata, AUD_CONFIG, data, mask);
5dab11d8
JA
275}
276
76296ef0 277void snd_intelhad_enable_audio(struct snd_pcm_substream *substream, u8 enable)
5dab11d8
JA
278{
279 had_read_modify_aud_config_v2(substream, enable, BIT(0));
280}
281
79dda75a
TI
282static void snd_intelhad_reset_audio(struct snd_intelhad *intelhaddata,
283 u8 reset)
5dab11d8 284{
79dda75a 285 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, reset);
5dab11d8
JA
286}
287
288/**
289 * initialize audio channel status registers
290 * This function is called in the prepare callback
291 */
292static int had_prog_status_reg(struct snd_pcm_substream *substream,
293 struct snd_intelhad *intelhaddata)
294{
295 union aud_cfg cfg_val = {.cfg_regval = 0};
296 union aud_ch_status_0 ch_stat0 = {.status_0_regval = 0};
297 union aud_ch_status_1 ch_stat1 = {.status_1_regval = 0};
298 int format;
299
300 pr_debug("Entry %s\n", __func__);
301
302 ch_stat0.status_0_regx.lpcm_id = (intelhaddata->aes_bits &
303 IEC958_AES0_NONAUDIO)>>1;
304 ch_stat0.status_0_regx.clk_acc = (intelhaddata->aes_bits &
305 IEC958_AES3_CON_CLOCK)>>4;
4812dcc4 306 cfg_val.cfg_regx_v2.val_bit = ch_stat0.status_0_regx.lpcm_id;
5dab11d8
JA
307
308 switch (substream->runtime->rate) {
309 case AUD_SAMPLE_RATE_32:
310 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_32KHZ;
311 break;
312
313 case AUD_SAMPLE_RATE_44_1:
314 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_44KHZ;
315 break;
316 case AUD_SAMPLE_RATE_48:
317 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_48KHZ;
318 break;
319 case AUD_SAMPLE_RATE_88_2:
320 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_88KHZ;
321 break;
322 case AUD_SAMPLE_RATE_96:
323 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_96KHZ;
324 break;
325 case AUD_SAMPLE_RATE_176_4:
326 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_176KHZ;
327 break;
328 case AUD_SAMPLE_RATE_192:
329 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_192KHZ;
330 break;
331
332 default:
333 /* control should never come here */
334 return -EINVAL;
335 break;
336
337 }
79dda75a
TI
338 had_write_register(intelhaddata,
339 AUD_CH_STATUS_0, ch_stat0.status_0_regval);
5dab11d8
JA
340
341 format = substream->runtime->format;
342
343 if (format == SNDRV_PCM_FORMAT_S16_LE) {
344 ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_20;
345 ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_16BITS;
346 } else if (format == SNDRV_PCM_FORMAT_S24_LE) {
347 ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_24;
348 ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_24BITS;
349 } else {
350 ch_stat1.status_1_regx.max_wrd_len = 0;
351 ch_stat1.status_1_regx.wrd_len = 0;
352 }
79dda75a
TI
353 had_write_register(intelhaddata,
354 AUD_CH_STATUS_1, ch_stat1.status_1_regval);
5dab11d8
JA
355 return 0;
356}
357
76296ef0 358/*
5dab11d8
JA
359 * function to initialize audio
360 * registers and buffer confgiuration registers
361 * This function is called in the prepare callback
362 */
76296ef0
TI
363static int snd_intelhad_audio_ctrl(struct snd_pcm_substream *substream,
364 struct snd_intelhad *intelhaddata)
5dab11d8
JA
365{
366 union aud_cfg cfg_val = {.cfg_regval = 0};
367 union aud_buf_config buf_cfg = {.buf_cfgval = 0};
368 u8 channels;
369
370 had_prog_status_reg(substream, intelhaddata);
371
372 buf_cfg.buf_cfg_regx_v2.audio_fifo_watermark = FIFO_THRESHOLD;
373 buf_cfg.buf_cfg_regx_v2.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
374 buf_cfg.buf_cfg_regx_v2.aud_delay = 0;
79dda75a 375 had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.buf_cfgval);
5dab11d8
JA
376
377 channels = substream->runtime->channels;
378 cfg_val.cfg_regx_v2.num_ch = channels - 2;
379 if (channels <= 2)
380 cfg_val.cfg_regx_v2.layout = LAYOUT0;
381 else
382 cfg_val.cfg_regx_v2.layout = LAYOUT1;
383
964ca808 384 cfg_val.cfg_regx_v2.val_bit = 1;
79dda75a 385 had_write_register(intelhaddata, AUD_CONFIG, cfg_val.cfg_regval);
5dab11d8
JA
386 return 0;
387}
388
5dab11d8
JA
389/*
390 * Compute derived values in channel_allocations[].
391 */
392static void init_channel_allocations(void)
393{
394 int i, j;
395 struct cea_channel_speaker_allocation *p;
396
397 pr_debug("%s: Enter\n", __func__);
398
399 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
400 p = channel_allocations + i;
401 p->channels = 0;
402 p->spk_mask = 0;
403 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
404 if (p->speakers[j]) {
405 p->channels++;
406 p->spk_mask |= p->speakers[j];
407 }
408 }
409}
410
411/*
412 * The transformation takes two steps:
413 *
414 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
415 * spk_mask => (channel_allocations[]) => ai->CA
416 *
417 * TODO: it could select the wrong CA from multiple candidates.
418 */
419static int snd_intelhad_channel_allocation(struct snd_intelhad *intelhaddata,
420 int channels)
421{
422 int i;
423 int ca = 0;
424 int spk_mask = 0;
425
426 /*
427 * CA defaults to 0 for basic stereo audio
428 */
429 if (channels <= 2)
430 return 0;
431
432 /*
433 * expand ELD's speaker allocation mask
434 *
435 * ELD tells the speaker mask in a compact(paired) form,
436 * expand ELD's notions to match the ones used by Audio InfoFrame.
437 */
438
439 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
440 if (intelhaddata->eeld.speaker_allocation_block & (1 << i))
441 spk_mask |= eld_speaker_allocation_bits[i];
442 }
443
444 /* search for the first working match in the CA table */
445 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
446 if (channels == channel_allocations[i].channels &&
447 (spk_mask & channel_allocations[i].spk_mask) ==
448 channel_allocations[i].spk_mask) {
449 ca = channel_allocations[i].ca_index;
450 break;
451 }
452 }
453
454 pr_debug("HDMI: select CA 0x%x for %d\n", ca, channels);
455
456 return ca;
457}
458
459/* from speaker bit mask to ALSA API channel position */
460static int spk_to_chmap(int spk)
461{
462 struct channel_map_table *t = map_tables;
463
464 for (; t->map; t++) {
465 if (t->spk_mask == spk)
466 return t->map;
467 }
468 return 0;
469}
470
471void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
472{
473 int i = 0, c = 0;
474 int spk_mask = 0;
475 struct snd_pcm_chmap_elem *chmap;
476 u8 eld_high, eld_high_mask = 0xF0;
477 u8 high_msb;
478
479 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
480 if (chmap == NULL) {
481 intelhaddata->chmap->chmap = NULL;
482 return;
483 }
484
79dda75a
TI
485 had_get_caps(intelhaddata, HAD_GET_ELD, &intelhaddata->eeld);
486 had_get_caps(intelhaddata, HAD_GET_DP_OUTPUT, &intelhaddata->dp_output);
5dab11d8
JA
487
488 pr_debug("eeld.speaker_allocation_block = %x\n",
489 intelhaddata->eeld.speaker_allocation_block);
490
491 /* WA: Fix the max channel supported to 8 */
492
493 /*
494 * Sink may support more than 8 channels, if eld_high has more than
495 * one bit set. SOC supports max 8 channels.
496 * Refer eld_speaker_allocation_bits, for sink speaker allocation
497 */
498
499 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
500 eld_high = intelhaddata->eeld.speaker_allocation_block & eld_high_mask;
501 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
502 /* eld_high & (eld_high-1): if more than 1 bit set */
503 /* 0x1F: 7 channels */
504 for (i = 1; i < 4; i++) {
505 high_msb = eld_high & (0x80 >> i);
506 if (high_msb) {
507 intelhaddata->eeld.speaker_allocation_block &=
508 high_msb | 0xF;
509 break;
510 }
511 }
512 }
513
514 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
515 if (intelhaddata->eeld.speaker_allocation_block & (1 << i))
516 spk_mask |= eld_speaker_allocation_bits[i];
517 }
518
519 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
520 if (spk_mask == channel_allocations[i].spk_mask) {
521 for (c = 0; c < channel_allocations[i].channels; c++) {
522 chmap->map[c] = spk_to_chmap(
523 channel_allocations[i].speakers[
524 (MAX_SPEAKERS - 1)-c]);
525 }
526 chmap->channels = channel_allocations[i].channels;
527 intelhaddata->chmap->chmap = chmap;
528 break;
529 }
530 }
531 if (i >= ARRAY_SIZE(channel_allocations)) {
532 intelhaddata->chmap->chmap = NULL;
533 kfree(chmap);
534 }
535}
536
537/*
538 * ALSA API channel-map control callbacks
539 */
540static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
541 struct snd_ctl_elem_info *uinfo)
542{
543 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
544 struct snd_intelhad *intelhaddata = info->private_data;
545
546 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
547 return -ENODEV;
548 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
549 uinfo->count = HAD_MAX_CHANNEL;
550 uinfo->value.integer.min = 0;
551 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
552 return 0;
553}
554
555static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
556 struct snd_ctl_elem_value *ucontrol)
557{
558 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
559 struct snd_intelhad *intelhaddata = info->private_data;
560 int i = 0;
561 const struct snd_pcm_chmap_elem *chmap;
562
563 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
564 return -ENODEV;
565 if (intelhaddata->chmap->chmap == NULL)
566 return -ENODATA;
567 chmap = intelhaddata->chmap->chmap;
568 for (i = 0; i < chmap->channels; i++) {
569 ucontrol->value.integer.value[i] = chmap->map[i];
570 pr_debug("chmap->map[%d] = %d\n", i, chmap->map[i]);
571 }
572
573 return 0;
574}
575
576static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
577 struct snd_pcm *pcm)
578{
579 int err = 0;
580
581 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
582 NULL, 0, (unsigned long)intelhaddata,
583 &intelhaddata->chmap);
584 if (err < 0)
585 return err;
586
587 intelhaddata->chmap->private_data = intelhaddata;
588 intelhaddata->kctl = intelhaddata->chmap->kctl;
589 intelhaddata->kctl->info = had_chmap_ctl_info;
590 intelhaddata->kctl->get = had_chmap_ctl_get;
591 intelhaddata->chmap->chmap = NULL;
592 return 0;
593}
594
76296ef0
TI
595/*
596 * snd_intelhad_prog_dip - to initialize Data Island Packets registers
5dab11d8
JA
597 *
598 * @substream:substream for which the prepare function is called
599 * @intelhaddata:substream private data
600 *
601 * This function is called in the prepare callback
602 */
76296ef0
TI
603static void snd_intelhad_prog_dip(struct snd_pcm_substream *substream,
604 struct snd_intelhad *intelhaddata)
5dab11d8
JA
605{
606 int i;
607 union aud_ctrl_st ctrl_state = {.ctrl_val = 0};
608 union aud_info_frame2 frame2 = {.fr2_val = 0};
609 union aud_info_frame3 frame3 = {.fr3_val = 0};
610 u8 checksum = 0;
964ca808 611 u32 info_frame;
5dab11d8
JA
612 int channels;
613
614 channels = substream->runtime->channels;
615
79dda75a 616 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.ctrl_val);
5dab11d8 617
964ca808
PLB
618 if (intelhaddata->dp_output) {
619 info_frame = DP_INFO_FRAME_WORD1;
620 frame2.fr2_val = 1;
621 } else {
622 info_frame = HDMI_INFO_FRAME_WORD1;
623 frame2.fr2_regx.chnl_cnt = substream->runtime->channels - 1;
5dab11d8 624
964ca808
PLB
625 frame3.fr3_regx.chnl_alloc = snd_intelhad_channel_allocation(
626 intelhaddata, channels);
5dab11d8 627
964ca808
PLB
628 /*Calculte the byte wide checksum for all valid DIP words*/
629 for (i = 0; i < BYTES_PER_WORD; i++)
630 checksum += (info_frame >> i*BITS_PER_BYTE) & MASK_BYTE0;
631 for (i = 0; i < BYTES_PER_WORD; i++)
632 checksum += (frame2.fr2_val >> i*BITS_PER_BYTE) & MASK_BYTE0;
633 for (i = 0; i < BYTES_PER_WORD; i++)
634 checksum += (frame3.fr3_val >> i*BITS_PER_BYTE) & MASK_BYTE0;
5dab11d8 635
964ca808
PLB
636 frame2.fr2_regx.chksum = -(checksum);
637 }
5dab11d8 638
79dda75a
TI
639 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, info_frame);
640 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, frame2.fr2_val);
641 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, frame3.fr3_val);
5dab11d8
JA
642
643 /* program remaining DIP words with zero */
644 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
79dda75a 645 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, 0x0);
5dab11d8
JA
646
647 ctrl_state.ctrl_regx.dip_freq = 1;
648 ctrl_state.ctrl_regx.dip_en_sta = 1;
79dda75a 649 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.ctrl_val);
5dab11d8
JA
650}
651
652/**
653 * snd_intelhad_prog_buffer - programs buffer
654 * address and length registers
655 *
656 * @substream:substream for which the prepare function is called
657 * @intelhaddata:substream private data
658 *
659 * This function programs ring buffer address and length into registers.
660 */
661int snd_intelhad_prog_buffer(struct snd_intelhad *intelhaddata,
662 int start, int end)
663{
664 u32 ring_buf_addr, ring_buf_size, period_bytes;
665 u8 i, num_periods;
666 struct snd_pcm_substream *substream;
667
668 substream = intelhaddata->stream_info.had_substream;
669 if (!substream) {
670 pr_err("substream is NULL\n");
671 dump_stack();
672 return 0;
673 }
674
675 ring_buf_addr = substream->runtime->dma_addr;
676 ring_buf_size = snd_pcm_lib_buffer_bytes(substream);
677 intelhaddata->stream_info.ring_buf_size = ring_buf_size;
678 period_bytes = frames_to_bytes(substream->runtime,
679 substream->runtime->period_size);
680 num_periods = substream->runtime->periods;
681
682 /*
683 * buffer addr should be 64 byte aligned, period bytes
684 * will be used to calculate addr offset
685 */
686 period_bytes &= ~0x3F;
687
688 /* Hardware supports MAX_PERIODS buffers */
689 if (end >= HAD_MAX_PERIODS)
690 return -EINVAL;
691
692 for (i = start; i <= end; i++) {
693 /* Program the buf registers with addr and len */
694 intelhaddata->buf_info[i].buf_addr = ring_buf_addr +
695 (i * period_bytes);
696 if (i < num_periods-1)
697 intelhaddata->buf_info[i].buf_size = period_bytes;
698 else
699 intelhaddata->buf_info[i].buf_size = ring_buf_size -
700 (period_bytes*i);
701
79dda75a
TI
702 had_write_register(intelhaddata,
703 AUD_BUF_A_ADDR + (i * HAD_REG_WIDTH),
5dab11d8
JA
704 intelhaddata->buf_info[i].buf_addr |
705 BIT(0) | BIT(1));
79dda75a
TI
706 had_write_register(intelhaddata,
707 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
5dab11d8
JA
708 period_bytes);
709 intelhaddata->buf_info[i].is_valid = true;
710 }
711 pr_debug("%s:buf[%d-%d] addr=%#x and size=%d\n", __func__, start, end,
712 intelhaddata->buf_info[start].buf_addr,
713 intelhaddata->buf_info[start].buf_size);
714 intelhaddata->valid_buf_cnt = num_periods;
715 return 0;
716}
717
718int snd_intelhad_read_len(struct snd_intelhad *intelhaddata)
719{
720 int i, retval = 0;
721 u32 len[4];
722
723 for (i = 0; i < 4 ; i++) {
79dda75a
TI
724 had_read_register(intelhaddata,
725 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
726 &len[i]);
5dab11d8
JA
727 if (!len[i])
728 retval++;
729 }
730 if (retval != 1) {
731 for (i = 0; i < 4 ; i++)
732 pr_debug("buf[%d] size=%d\n", i, len[i]);
733 }
734
735 return retval;
736}
737
964ca808
PLB
738static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
739{
740 u32 maud_val;
741
742 /* Select maud according to DP 1.2 spec*/
743 if (link_rate == DP_2_7_GHZ) {
744 switch (aud_samp_freq) {
745 case AUD_SAMPLE_RATE_32:
746 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
747 break;
748
749 case AUD_SAMPLE_RATE_44_1:
750 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
751 break;
752
753 case AUD_SAMPLE_RATE_48:
754 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
755 break;
756
757 case AUD_SAMPLE_RATE_88_2:
758 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
759 break;
760
761 case AUD_SAMPLE_RATE_96:
762 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
763 break;
764
765 case AUD_SAMPLE_RATE_176_4:
766 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
767 break;
768
769 case HAD_MAX_RATE:
770 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
771 break;
772
773 default:
774 maud_val = -EINVAL;
775 break;
776 }
777 } else if (link_rate == DP_1_62_GHZ) {
778 switch (aud_samp_freq) {
779 case AUD_SAMPLE_RATE_32:
780 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
781 break;
782
783 case AUD_SAMPLE_RATE_44_1:
784 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
785 break;
786
787 case AUD_SAMPLE_RATE_48:
788 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
789 break;
790
791 case AUD_SAMPLE_RATE_88_2:
792 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
793 break;
794
795 case AUD_SAMPLE_RATE_96:
796 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
797 break;
798
799 case AUD_SAMPLE_RATE_176_4:
800 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
801 break;
802
803 case HAD_MAX_RATE:
804 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
805 break;
806
807 default:
808 maud_val = -EINVAL;
809 break;
810 }
811 } else
812 maud_val = -EINVAL;
813
814 return maud_val;
815}
816
76296ef0
TI
817/*
818 * snd_intelhad_prog_cts - Program HDMI audio CTS value
5dab11d8
JA
819 *
820 * @aud_samp_freq: sampling frequency of audio data
821 * @tmds: sampling frequency of the display data
822 * @n_param: N value, depends on aud_samp_freq
823 * @intelhaddata:substream private data
824 *
825 * Program CTS register based on the audio and display sampling frequency
826 */
76296ef0
TI
827static void snd_intelhad_prog_cts(u32 aud_samp_freq, u32 tmds,
828 u32 link_rate, u32 n_param,
829 struct snd_intelhad *intelhaddata)
5dab11d8
JA
830{
831 u32 cts_val;
832 u64 dividend, divisor;
833
964ca808
PLB
834 if (intelhaddata->dp_output) {
835 /* Substitute cts_val with Maud according to DP 1.2 spec*/
836 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
837 } else {
838 /* Calculate CTS according to HDMI 1.3a spec*/
839 dividend = (u64)tmds * n_param*1000;
840 divisor = 128 * aud_samp_freq;
841 cts_val = div64_u64(dividend, divisor);
842 }
5dab11d8 843 pr_debug("TMDS value=%d, N value=%d, CTS Value=%d\n",
964ca808 844 tmds, n_param, cts_val);
79dda75a 845 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
5dab11d8
JA
846}
847
848static int had_calculate_n_value(u32 aud_samp_freq)
849{
850 s32 n_val;
851
852 /* Select N according to HDMI 1.3a spec*/
853 switch (aud_samp_freq) {
854 case AUD_SAMPLE_RATE_32:
855 n_val = 4096;
856 break;
857
858 case AUD_SAMPLE_RATE_44_1:
859 n_val = 6272;
860 break;
861
862 case AUD_SAMPLE_RATE_48:
863 n_val = 6144;
864 break;
865
866 case AUD_SAMPLE_RATE_88_2:
867 n_val = 12544;
868 break;
869
870 case AUD_SAMPLE_RATE_96:
871 n_val = 12288;
872 break;
873
874 case AUD_SAMPLE_RATE_176_4:
875 n_val = 25088;
876 break;
877
878 case HAD_MAX_RATE:
879 n_val = 24576;
880 break;
881
882 default:
883 n_val = -EINVAL;
884 break;
885 }
886 return n_val;
887}
888
76296ef0
TI
889/*
890 * snd_intelhad_prog_n - Program HDMI audio N value
5dab11d8
JA
891 *
892 * @aud_samp_freq: sampling frequency of audio data
893 * @n_param: N value, depends on aud_samp_freq
894 * @intelhaddata:substream private data
895 *
896 * This function is called in the prepare callback.
897 * It programs based on the audio and display sampling frequency
898 */
76296ef0
TI
899static int snd_intelhad_prog_n(u32 aud_samp_freq, u32 *n_param,
900 struct snd_intelhad *intelhaddata)
5dab11d8
JA
901{
902 s32 n_val;
903
964ca808
PLB
904 if (intelhaddata->dp_output) {
905 /*
906 * According to DP specs, Maud and Naud values hold
907 * a relationship, which is stated as:
908 * Maud/Naud = 512 * fs / f_LS_Clk
909 * where, fs is the sampling frequency of the audio stream
910 * and Naud is 32768 for Async clock.
911 */
912
913 n_val = DP_NAUD_VAL;
914 } else
915 n_val = had_calculate_n_value(aud_samp_freq);
5dab11d8
JA
916
917 if (n_val < 0)
918 return n_val;
919
79dda75a 920 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
5dab11d8
JA
921 *n_param = n_val;
922 return 0;
923}
924
76296ef0 925void snd_intelhad_handle_underrun(struct snd_intelhad *intelhaddata)
5dab11d8
JA
926{
927 u32 hdmi_status, i = 0;
928
929 /* Handle Underrun interrupt within Audio Unit */
79dda75a 930 had_write_register(intelhaddata, AUD_CONFIG, 0);
5dab11d8 931 /* Reset buffer pointers */
79dda75a
TI
932 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, 1);
933 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, 0);
5dab11d8
JA
934 /**
935 * The interrupt status 'sticky' bits might not be cleared by
936 * setting '1' to that bit once...
937 */
938 do { /* clear bit30, 31 AUD_HDMI_STATUS */
79dda75a
TI
939 had_read_register(intelhaddata, AUD_HDMI_STATUS_v2,
940 &hdmi_status);
5dab11d8
JA
941 pr_debug("HDMI status =0x%x\n", hdmi_status);
942 if (hdmi_status & AUD_CONFIG_MASK_UNDERRUN) {
943 i++;
79dda75a
TI
944 had_write_register(intelhaddata,
945 AUD_HDMI_STATUS_v2, hdmi_status);
5dab11d8
JA
946 } else
947 break;
948 } while (i < MAX_CNT);
949 if (i >= MAX_CNT)
950 pr_err("Unable to clear UNDERRUN bits\n");
951}
952
953/**
954 * snd_intelhad_open - stream initializations are done here
955 * @substream:substream for which the stream function is called
956 *
957 * This function is called whenever a PCM stream is opened
958 */
959static int snd_intelhad_open(struct snd_pcm_substream *substream)
960{
961 struct snd_intelhad *intelhaddata;
962 struct snd_pcm_runtime *runtime;
963 struct had_stream_pvt *stream;
5647aec2 964 struct had_stream_data *had_stream;
5dab11d8
JA
965 int retval;
966
967 pr_debug("snd_intelhad_open called\n");
968 intelhaddata = snd_pcm_substream_chip(substream);
5647aec2 969 had_stream = &intelhaddata->stream_data;
5dab11d8 970 runtime = substream->runtime;
6ddb3ab6 971 intelhaddata->underrun_count = 0;
5dab11d8
JA
972
973 pm_runtime_get(intelhaddata->dev);
974
975 if (had_get_hwstate(intelhaddata)) {
976 pr_err("%s: HDMI cable plugged-out\n", __func__);
977 retval = -ENODEV;
978 goto exit_put_handle;
979 }
980
981 /* Check, if device already in use */
982 if (runtime->private_data) {
983 pr_err("Device already in use\n");
984 retval = -EBUSY;
985 goto exit_put_handle;
986 }
987
988 /* set the runtime hw parameter with local snd_pcm_hardware struct */
989 runtime->hw = snd_intel_hadstream;
990
991 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
992 if (!stream) {
993 retval = -ENOMEM;
994 goto exit_put_handle;
995 }
996 stream->stream_status = STREAM_INIT;
997 runtime->private_data = stream;
998
999 retval = snd_pcm_hw_constraint_integer(runtime,
1000 SNDRV_PCM_HW_PARAM_PERIODS);
1001 if (retval < 0)
1002 goto exit_err;
1003
1004 /* Make sure, that the period size is always aligned
1005 * 64byte boundary
1006 */
1007 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1008 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
1009 if (retval < 0) {
1010 pr_err("%s:step_size=64 failed,err=%d\n", __func__, retval);
1011 goto exit_err;
1012 }
1013
1014 return retval;
1015exit_err:
1016 kfree(stream);
1017exit_put_handle:
1018 pm_runtime_put(intelhaddata->dev);
1019 runtime->private_data = NULL;
1020 return retval;
1021}
1022
1023/**
1024 * had_period_elapsed - updates the hardware pointer status
1025 * @had_substream:substream for which the stream function is called
1026 *
1027 */
1028static void had_period_elapsed(void *had_substream)
1029{
1030 struct snd_pcm_substream *substream = had_substream;
1031 struct had_stream_pvt *stream;
1032
1033 /* pr_debug("had_period_elapsed called\n"); */
1034
1035 if (!substream || !substream->runtime)
1036 return;
1037 stream = substream->runtime->private_data;
1038 if (!stream)
1039 return;
1040
1041 if (stream->stream_status != STREAM_RUNNING)
1042 return;
1043 snd_pcm_period_elapsed(substream);
1044}
1045
1046/**
1047 * snd_intelhad_init_stream - internal function to initialize stream info
1048 * @substream:substream for which the stream function is called
1049 *
1050 */
1051static int snd_intelhad_init_stream(struct snd_pcm_substream *substream)
1052{
1053 struct snd_intelhad *intelhaddata = snd_pcm_substream_chip(substream);
1054
1055 pr_debug("snd_intelhad_init_stream called\n");
1056
1057 pr_debug("setting buffer ptr param\n");
1058 intelhaddata->stream_info.period_elapsed = had_period_elapsed;
1059 intelhaddata->stream_info.had_substream = substream;
1060 intelhaddata->stream_info.buffer_ptr = 0;
1061 intelhaddata->stream_info.buffer_rendered = 0;
1062 intelhaddata->stream_info.sfreq = substream->runtime->rate;
1063 return 0;
1064}
1065
1066/**
1067 * snd_intelhad_close- to free parameteres when stream is stopped
1068 *
1069 * @substream: substream for which the function is called
1070 *
1071 * This function is called by ALSA framework when stream is stopped
1072 */
1073static int snd_intelhad_close(struct snd_pcm_substream *substream)
1074{
1075 struct snd_intelhad *intelhaddata;
1076 struct snd_pcm_runtime *runtime;
1077
1078 pr_debug("snd_intelhad_close called\n");
1079
1080 intelhaddata = snd_pcm_substream_chip(substream);
1081 runtime = substream->runtime;
1082
1083 if (!runtime->private_data) {
1084 pr_debug("close() might have called after failed open");
1085 return 0;
1086 }
1087
1088 intelhaddata->stream_info.buffer_rendered = 0;
1089 intelhaddata->stream_info.buffer_ptr = 0;
1090 intelhaddata->stream_info.str_id = 0;
1091 intelhaddata->stream_info.had_substream = NULL;
1092
1093 /* Check if following drv_status modification is required - VA */
1094 if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) {
1095 intelhaddata->drv_status = HAD_DRV_CONNECTED;
1096 pr_debug("%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
1097 __func__, __LINE__);
1098 }
1099 kfree(runtime->private_data);
1100 runtime->private_data = NULL;
1101 pm_runtime_put(intelhaddata->dev);
1102 return 0;
1103}
1104
1105/**
1106 * snd_intelhad_hw_params- to setup the hardware parameters
1107 * like allocating the buffers
1108 *
1109 * @substream: substream for which the function is called
1110 * @hw_params: hardware parameters
1111 *
1112 * This function is called by ALSA framework when hardware params are set
1113 */
1114static int snd_intelhad_hw_params(struct snd_pcm_substream *substream,
1115 struct snd_pcm_hw_params *hw_params)
1116{
1117 unsigned long addr;
1118 int pages, buf_size, retval;
1119
1120 pr_debug("snd_intelhad_hw_params called\n");
1121
1122 if (!hw_params)
1123 return -EINVAL;
1124
1125 buf_size = params_buffer_bytes(hw_params);
1126 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1127 if (retval < 0)
1128 return retval;
1129 pr_debug("%s:allocated memory = %d\n", __func__, buf_size);
1130 /* mark the pages as uncached region */
1131 addr = (unsigned long) substream->runtime->dma_area;
1132 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1133 retval = set_memory_uc(addr, pages);
1134 if (retval) {
1135 pr_err("set_memory_uc failed.Error:%d\n", retval);
1136 return retval;
1137 }
1138 memset(substream->runtime->dma_area, 0, buf_size);
1139
1140 return retval;
1141}
1142
1143/**
1144 * snd_intelhad_hw_free- to release the resources allocated during
1145 * hardware params setup
1146 *
1147 * @substream: substream for which the function is called
1148 *
1149 * This function is called by ALSA framework before close callback.
1150 *
1151 */
1152static int snd_intelhad_hw_free(struct snd_pcm_substream *substream)
1153{
1154 unsigned long addr;
1155 u32 pages;
1156
1157 pr_debug("snd_intelhad_hw_free called\n");
1158
1159 /* mark back the pages as cached/writeback region before the free */
1160 if (substream->runtime->dma_area != NULL) {
1161 addr = (unsigned long) substream->runtime->dma_area;
1162 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1163 PAGE_SIZE;
1164 set_memory_wb(addr, pages);
1165 return snd_pcm_lib_free_pages(substream);
1166 }
1167 return 0;
1168}
1169
1170/**
1171 * snd_intelhad_pcm_trigger - stream activities are handled here
1172 * @substream:substream for which the stream function is called
1173 * @cmd:the stream commamd thats requested from upper layer
1174 * This function is called whenever an a stream activity is invoked
1175 */
1176static int snd_intelhad_pcm_trigger(struct snd_pcm_substream *substream,
1177 int cmd)
1178{
1179 int caps, retval = 0;
1180 unsigned long flag_irq;
1181 struct snd_intelhad *intelhaddata;
1182 struct had_stream_pvt *stream;
5647aec2 1183 struct had_stream_data *had_stream;
5dab11d8
JA
1184
1185 pr_debug("snd_intelhad_pcm_trigger called\n");
1186
1187 intelhaddata = snd_pcm_substream_chip(substream);
1188 stream = substream->runtime->private_data;
5647aec2 1189 had_stream = &intelhaddata->stream_data;
5dab11d8
JA
1190
1191 switch (cmd) {
1192 case SNDRV_PCM_TRIGGER_START:
1193 pr_debug("Trigger Start\n");
1194
1195 /* Disable local INTRs till register prgmng is done */
1196 if (had_get_hwstate(intelhaddata)) {
1197 pr_err("_START: HDMI cable plugged-out\n");
1198 retval = -ENODEV;
1199 break;
1200 }
1201 stream->stream_status = STREAM_RUNNING;
1202
1203 had_stream->stream_type = HAD_RUNNING_STREAM;
1204
1205 /* Enable Audio */
1206 /*
1207 * ToDo: Need to enable UNDERRUN interrupts as well
1208 * caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
1209 */
1210 caps = HDMI_AUDIO_BUFFER_DONE;
79dda75a
TI
1211 retval = had_set_caps(intelhaddata, HAD_SET_ENABLE_AUDIO_INT,
1212 &caps);
1213 retval = had_set_caps(intelhaddata, HAD_SET_ENABLE_AUDIO, NULL);
76296ef0 1214 snd_intelhad_enable_audio(substream, 1);
5dab11d8
JA
1215
1216 pr_debug("Processed _Start\n");
1217
1218 break;
1219
1220 case SNDRV_PCM_TRIGGER_STOP:
1221 pr_debug("Trigger Stop\n");
1222 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irq);
1223 intelhaddata->stream_info.str_id = 0;
1224 intelhaddata->curr_buf = 0;
1225
1226 /* Stop reporting BUFFER_DONE/UNDERRUN to above layers*/
1227
1228 had_stream->stream_type = HAD_INIT;
1229 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irq);
1230 /* Disable Audio */
1231 /*
1232 * ToDo: Need to disable UNDERRUN interrupts as well
1233 * caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
1234 */
1235 caps = HDMI_AUDIO_BUFFER_DONE;
79dda75a 1236 had_set_caps(intelhaddata, HAD_SET_DISABLE_AUDIO_INT, &caps);
76296ef0 1237 snd_intelhad_enable_audio(substream, 0);
5dab11d8 1238 /* Reset buffer pointers */
79dda75a
TI
1239 snd_intelhad_reset_audio(intelhaddata, 1);
1240 snd_intelhad_reset_audio(intelhaddata, 0);
5dab11d8 1241 stream->stream_status = STREAM_DROPPED;
79dda75a 1242 had_set_caps(intelhaddata, HAD_SET_DISABLE_AUDIO, NULL);
5dab11d8
JA
1243 break;
1244
1245 default:
1246 retval = -EINVAL;
1247 }
1248 return retval;
1249}
1250
1251/**
1252 * snd_intelhad_pcm_prepare- internal preparation before starting a stream
1253 *
1254 * @substream: substream for which the function is called
1255 *
1256 * This function is called when a stream is started for internal preparation.
1257 */
1258static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream)
1259{
1260 int retval;
1261 u32 disp_samp_freq, n_param;
964ca808 1262 u32 link_rate = 0;
5dab11d8
JA
1263 struct snd_intelhad *intelhaddata;
1264 struct snd_pcm_runtime *runtime;
5647aec2 1265 struct had_stream_data *had_stream;
5dab11d8
JA
1266
1267 pr_debug("snd_intelhad_pcm_prepare called\n");
1268
1269 intelhaddata = snd_pcm_substream_chip(substream);
1270 runtime = substream->runtime;
5647aec2 1271 had_stream = &intelhaddata->stream_data;
5dab11d8
JA
1272
1273 if (had_get_hwstate(intelhaddata)) {
1274 pr_err("%s: HDMI cable plugged-out\n", __func__);
1275 retval = -ENODEV;
1276 goto prep_end;
1277 }
1278
1279 pr_debug("period_size=%d\n",
1280 (int)frames_to_bytes(runtime, runtime->period_size));
1281 pr_debug("periods=%d\n", runtime->periods);
1282 pr_debug("buffer_size=%d\n", (int)snd_pcm_lib_buffer_bytes(substream));
1283 pr_debug("rate=%d\n", runtime->rate);
1284 pr_debug("channels=%d\n", runtime->channels);
1285
1286 if (intelhaddata->stream_info.str_id) {
1287 pr_debug("_prepare is called for existing str_id#%d\n",
1288 intelhaddata->stream_info.str_id);
1289 retval = snd_intelhad_pcm_trigger(substream,
1290 SNDRV_PCM_TRIGGER_STOP);
1291 return retval;
1292 }
1293
1294 retval = snd_intelhad_init_stream(substream);
1295 if (retval)
1296 goto prep_end;
1297
1298
1299 /* Get N value in KHz */
79dda75a
TI
1300 retval = had_get_caps(intelhaddata, HAD_GET_DISPLAY_RATE,
1301 &disp_samp_freq);
5dab11d8
JA
1302 if (retval) {
1303 pr_err("querying display sampling freq failed %#x\n", retval);
1304 goto prep_end;
1305 }
1306
79dda75a
TI
1307 had_get_caps(intelhaddata, HAD_GET_ELD, &intelhaddata->eeld);
1308 had_get_caps(intelhaddata, HAD_GET_DP_OUTPUT, &intelhaddata->dp_output);
5dab11d8 1309
76296ef0
TI
1310 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1311 intelhaddata);
5dab11d8
JA
1312 if (retval) {
1313 pr_err("programming N value failed %#x\n", retval);
1314 goto prep_end;
1315 }
964ca808
PLB
1316
1317 if (intelhaddata->dp_output)
79dda75a 1318 had_get_caps(intelhaddata, HAD_GET_LINK_RATE, &link_rate);
964ca808
PLB
1319
1320
76296ef0
TI
1321 snd_intelhad_prog_cts(substream->runtime->rate,
1322 disp_samp_freq, link_rate,
1323 n_param, intelhaddata);
5dab11d8 1324
76296ef0 1325 snd_intelhad_prog_dip(substream, intelhaddata);
5dab11d8 1326
76296ef0 1327 retval = snd_intelhad_audio_ctrl(substream, intelhaddata);
5dab11d8
JA
1328
1329 /* Prog buffer address */
1330 retval = snd_intelhad_prog_buffer(intelhaddata,
1331 HAD_BUF_TYPE_A, HAD_BUF_TYPE_D);
1332
1333 /*
1334 * Program channel mapping in following order:
1335 * FL, FR, C, LFE, RL, RR
1336 */
1337
79dda75a 1338 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
5dab11d8
JA
1339
1340prep_end:
1341 return retval;
1342}
1343
1344/**
1345 * snd_intelhad_pcm_pointer- to send the current buffer pointerprocessed by hw
1346 *
1347 * @substream: substream for which the function is called
1348 *
1349 * This function is called by ALSA framework to get the current hw buffer ptr
1350 * when a period is elapsed
1351 */
1352static snd_pcm_uframes_t snd_intelhad_pcm_pointer(
1353 struct snd_pcm_substream *substream)
1354{
1355 struct snd_intelhad *intelhaddata;
1356 u32 bytes_rendered = 0;
1357 u32 t;
1358 int buf_id;
1359
1360 /* pr_debug("snd_intelhad_pcm_pointer called\n"); */
1361
1362 intelhaddata = snd_pcm_substream_chip(substream);
1363
1364 if (intelhaddata->flag_underrun) {
1365 intelhaddata->flag_underrun = 0;
1366 return SNDRV_PCM_POS_XRUN;
1367 }
1368
1369 /* Use a hw register to calculate sub-period position reports.
1370 * This makes PulseAudio happier.
1371 */
1372
1373 buf_id = intelhaddata->curr_buf % 4;
79dda75a
TI
1374 had_read_register(intelhaddata,
1375 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH), &t);
232892fb
JA
1376
1377 if ((t == 0) || (t == ((u32)-1L))) {
6ddb3ab6 1378 intelhaddata->underrun_count++;
232892fb 1379 pr_debug("discovered buffer done for buf %d, count = %d\n",
6ddb3ab6 1380 buf_id, intelhaddata->underrun_count);
232892fb 1381
6ddb3ab6 1382 if (intelhaddata->underrun_count > (HAD_MIN_PERIODS/2)) {
232892fb 1383 pr_debug("assume audio_codec_reset, underrun = %d - do xrun\n",
6ddb3ab6
TI
1384 intelhaddata->underrun_count);
1385 intelhaddata->underrun_count = 0;
232892fb
JA
1386 return SNDRV_PCM_POS_XRUN;
1387 }
1388 } else {
1389 /* Reset Counter */
6ddb3ab6 1390 intelhaddata->underrun_count = 0;
5dab11d8 1391 }
232892fb 1392
5dab11d8
JA
1393 t = intelhaddata->buf_info[buf_id].buf_size - t;
1394
1395 if (intelhaddata->stream_info.buffer_rendered)
1396 div_u64_rem(intelhaddata->stream_info.buffer_rendered,
1397 intelhaddata->stream_info.ring_buf_size,
1398 &(bytes_rendered));
1399
1400 intelhaddata->stream_info.buffer_ptr = bytes_to_frames(
1401 substream->runtime,
1402 bytes_rendered + t);
1403 return intelhaddata->stream_info.buffer_ptr;
1404}
1405
1406/**
1407 * snd_intelhad_pcm_mmap- mmaps a kernel buffer to user space for copying data
1408 *
1409 * @substream: substream for which the function is called
1410 * @vma: struct instance of memory VMM memory area
1411 *
1412 * This function is called by OS when a user space component
1413 * tries to get mmap memory from driver
1414 */
1415static int snd_intelhad_pcm_mmap(struct snd_pcm_substream *substream,
1416 struct vm_area_struct *vma)
1417{
1418
1419 pr_debug("snd_intelhad_pcm_mmap called\n");
1420
1421 pr_debug("entry with prot:%s\n", __func__);
1422 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1423 return remap_pfn_range(vma, vma->vm_start,
1424 substream->dma_buffer.addr >> PAGE_SHIFT,
1425 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1426}
1427
1428int hdmi_audio_mode_change(struct snd_pcm_substream *substream)
1429{
1430 int retval = 0;
1431 u32 disp_samp_freq, n_param;
964ca808 1432 u32 link_rate = 0;
5dab11d8
JA
1433 struct snd_intelhad *intelhaddata;
1434
1435 intelhaddata = snd_pcm_substream_chip(substream);
1436
1437 /* Disable Audio */
76296ef0 1438 snd_intelhad_enable_audio(substream, 0);
5dab11d8
JA
1439
1440 /* Update CTS value */
79dda75a
TI
1441 retval = had_get_caps(intelhaddata, HAD_GET_DISPLAY_RATE,
1442 &disp_samp_freq);
5dab11d8
JA
1443 if (retval) {
1444 pr_err("querying display sampling freq failed %#x\n", retval);
1445 goto out;
1446 }
1447
76296ef0
TI
1448 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1449 intelhaddata);
5dab11d8
JA
1450 if (retval) {
1451 pr_err("programming N value failed %#x\n", retval);
1452 goto out;
1453 }
964ca808
PLB
1454
1455 if (intelhaddata->dp_output)
79dda75a 1456 had_get_caps(intelhaddata, HAD_GET_LINK_RATE, &link_rate);
964ca808 1457
76296ef0
TI
1458 snd_intelhad_prog_cts(substream->runtime->rate,
1459 disp_samp_freq, link_rate,
1460 n_param, intelhaddata);
5dab11d8
JA
1461
1462 /* Enable Audio */
76296ef0 1463 snd_intelhad_enable_audio(substream, 1);
5dab11d8
JA
1464
1465out:
1466 return retval;
1467}
1468
1469/*PCM operations structure and the calls back for the same */
1470struct snd_pcm_ops snd_intelhad_playback_ops = {
1471 .open = snd_intelhad_open,
1472 .close = snd_intelhad_close,
1473 .ioctl = snd_pcm_lib_ioctl,
1474 .hw_params = snd_intelhad_hw_params,
1475 .hw_free = snd_intelhad_hw_free,
1476 .prepare = snd_intelhad_pcm_prepare,
1477 .trigger = snd_intelhad_pcm_trigger,
1478 .pointer = snd_intelhad_pcm_pointer,
1479 .mmap = snd_intelhad_pcm_mmap,
1480};
1481
5dab11d8
JA
1482/**
1483 * snd_intelhad_pcm_free - to free the memory allocated
1484 *
1485 * @pcm: pointer to pcm instance
1486 * This function is called when the device is removed
1487 */
1488static void snd_intelhad_pcm_free(struct snd_pcm *pcm)
1489{
1490 pr_debug("Freeing PCM preallocated pages\n");
1491 snd_pcm_lib_preallocate_free_for_all(pcm);
1492}
1493
1494static int had_iec958_info(struct snd_kcontrol *kcontrol,
1495 struct snd_ctl_elem_info *uinfo)
1496{
1497 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1498 uinfo->count = 1;
1499 return 0;
1500}
1501
1502static int had_iec958_get(struct snd_kcontrol *kcontrol,
1503 struct snd_ctl_elem_value *ucontrol)
1504{
1505 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1506
1507 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1508 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1509 ucontrol->value.iec958.status[2] =
1510 (intelhaddata->aes_bits >> 16) & 0xff;
1511 ucontrol->value.iec958.status[3] =
1512 (intelhaddata->aes_bits >> 24) & 0xff;
1513 return 0;
1514}
1515static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1516 struct snd_ctl_elem_value *ucontrol)
1517{
1518 ucontrol->value.iec958.status[0] = 0xff;
1519 ucontrol->value.iec958.status[1] = 0xff;
1520 ucontrol->value.iec958.status[2] = 0xff;
1521 ucontrol->value.iec958.status[3] = 0xff;
1522 return 0;
1523}
1524static int had_iec958_put(struct snd_kcontrol *kcontrol,
1525 struct snd_ctl_elem_value *ucontrol)
1526{
1527 unsigned int val;
1528 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1529
1530 pr_debug("entered had_iec958_put\n");
1531 val = (ucontrol->value.iec958.status[0] << 0) |
1532 (ucontrol->value.iec958.status[1] << 8) |
1533 (ucontrol->value.iec958.status[2] << 16) |
1534 (ucontrol->value.iec958.status[3] << 24);
1535 if (intelhaddata->aes_bits != val) {
1536 intelhaddata->aes_bits = val;
1537 return 1;
1538 }
1539 return 1;
1540}
1541
1542static struct snd_kcontrol_new had_control_iec958_mask = {
1543 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1544 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1545 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1546 .info = had_iec958_info, /* shared */
1547 .get = had_iec958_mask_get,
1548};
1549
1550static struct snd_kcontrol_new had_control_iec958 = {
1551 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1552 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1553 .info = had_iec958_info,
1554 .get = had_iec958_get,
1555 .put = had_iec958_put
1556};
1557
79dda75a 1558/*
5dab11d8
JA
1559 * hdmi_audio_probe - to create sound card instance for HDMI audio playabck
1560 *
79dda75a
TI
1561 * @devptr: platform device
1562 * @had_ret: pointer to store the created snd_intelhad object
5dab11d8 1563 *
79dda75a 1564 * This function is called when the platform device is probed. This function
5dab11d8
JA
1565 * creates and registers the sound card with ALSA
1566 */
79dda75a
TI
1567int hdmi_audio_probe(struct platform_device *devptr,
1568 struct snd_intelhad **had_ret)
5dab11d8
JA
1569{
1570 int retval;
1571 struct snd_pcm *pcm;
1572 struct snd_card *card;
5dab11d8 1573 struct snd_intelhad *intelhaddata;
5dab11d8
JA
1574
1575 pr_debug("Enter %s\n", __func__);
1576
5647aec2
TI
1577 /* create a card instance with ALSA framework */
1578 retval = snd_card_new(&devptr->dev, hdmi_card_index, hdmi_card_id,
1579 THIS_MODULE, sizeof(*intelhaddata), &card);
1580 if (retval)
1581 return retval;
5dab11d8 1582
5647aec2 1583 intelhaddata = card->private_data;
5dab11d8
JA
1584 spin_lock_init(&intelhaddata->had_spinlock);
1585 intelhaddata->drv_status = HAD_DRV_DISCONNECTED;
1586 pr_debug("%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
1587 __func__, __LINE__);
1588
36ec0d99 1589 intelhaddata->dev = &devptr->dev;
5dab11d8
JA
1590 intelhaddata->card = card;
1591 intelhaddata->card_id = hdmi_card_id;
1592 intelhaddata->card_index = card->number;
5dab11d8
JA
1593 intelhaddata->flag_underrun = 0;
1594 intelhaddata->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
1595 strncpy(card->driver, INTEL_HAD, strlen(INTEL_HAD));
1596 strncpy(card->shortname, INTEL_HAD, strlen(INTEL_HAD));
1597
1598 retval = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
1599 MAX_CAP_STREAMS, &pcm);
1600 if (retval)
1601 goto err;
1602
1603 /* setup private data which can be retrieved when required */
1604 pcm->private_data = intelhaddata;
1605 pcm->private_free = snd_intelhad_pcm_free;
1606 pcm->info_flags = 0;
1607 strncpy(pcm->name, card->shortname, strlen(card->shortname));
1608 /* setup the ops for palyabck */
1609 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1610 &snd_intelhad_playback_ops);
1611 /* allocate dma pages for ALSA stream operations
1612 * memory allocated is based on size, not max value
1613 * thus using same argument for max & size
1614 */
1615 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
1616 SNDRV_DMA_TYPE_DEV, NULL,
1617 HAD_MAX_BUFFER, HAD_MAX_BUFFER);
5dab11d8
JA
1618 if (retval)
1619 goto err;
1620
5dab11d8
JA
1621 /* IEC958 controls */
1622 retval = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958_mask,
1623 intelhaddata));
1624 if (retval < 0)
1625 goto err;
1626 retval = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958,
1627 intelhaddata));
1628 if (retval < 0)
1629 goto err;
1630
1631 init_channel_allocations();
1632
1633 /* Register channel map controls */
1634 retval = had_register_chmap_ctls(intelhaddata, pcm);
1635 if (retval < 0)
1636 goto err;
1637
36ec0d99
TI
1638 retval = snd_card_register(card);
1639 if (retval)
1640 goto err;
1641
5dab11d8
JA
1642 pm_runtime_set_active(intelhaddata->dev);
1643 pm_runtime_enable(intelhaddata->dev);
1644
79dda75a 1645 *had_ret = intelhaddata;
5dab11d8 1646
79dda75a 1647 return 0;
5647aec2 1648
5dab11d8
JA
1649err:
1650 snd_card_free(card);
5dab11d8
JA
1651 pr_err("Error returned from %s api %#x\n", __func__, retval);
1652 return retval;
1653}
1654
79dda75a 1655/*
5dab11d8
JA
1656 * hdmi_audio_remove - removes the alsa card
1657 *
1658 *@haddata: pointer to HAD private data
1659 *
1660 * This function is called when the hdmi cable is un-plugged. This function
1661 * free the sound card.
1662 */
79dda75a 1663int hdmi_audio_remove(struct snd_intelhad *intelhaddata)
5dab11d8 1664{
5dab11d8
JA
1665 int caps;
1666
1667 pr_debug("Enter %s\n", __func__);
1668
1669 if (!intelhaddata)
1670 return 0;
1671
1672 if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) {
1673 caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
79dda75a
TI
1674 had_set_caps(intelhaddata, HAD_SET_DISABLE_AUDIO_INT, &caps);
1675 had_set_caps(intelhaddata, HAD_SET_DISABLE_AUDIO, NULL);
5dab11d8
JA
1676 }
1677 snd_card_free(intelhaddata->card);
5dab11d8
JA
1678 return 0;
1679}
1680
1681MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1682MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1683MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1684MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1685MODULE_DESCRIPTION("Intel HDMI Audio driver");
1686MODULE_LICENSE("GPL v2");
1687MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");