2 * intel_hdmi_audio.c - Intel HDMI audio driver
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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 * ALSA driver for Intel HDMI audio
24 #define pr_fmt(fmt) "had: " fmt
26 #include <linux/platform_device.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"
40 static DEFINE_MUTEX(had_mutex);
42 /*standard module options for ALSA. This module supports only one card*/
43 static int hdmi_card_index = SNDRV_DEFAULT_IDX1;
44 static char *hdmi_card_id = SNDRV_DEFAULT_STR1;
45 static struct snd_intelhad *had_data;
46 static int underrun_count;
48 module_param_named(index, hdmi_card_index, int, 0444);
49 MODULE_PARM_DESC(index,
50 "Index value for INTEL Intel HDMI Audio controller.");
51 module_param_named(id, hdmi_card_id, charp, 0444);
53 "ID string for INTEL Intel HDMI Audio controller.");
56 * ELD SA bits in the CEA Speaker Allocation data block
58 static int eld_speaker_allocation_bits[] = {
66 /* the following are not defined in ELD yet */
71 * This is an ordered list!
73 * The preceding ones have better chances to be selected by
74 * hdmi_channel_allocation().
76 static struct cea_channel_speaker_allocation channel_allocations[] = {
77 /* channel: 7 6 5 4 3 2 1 0 */
78 { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
80 { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
82 { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
84 { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
86 { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
88 { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
90 { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
92 { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
94 { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
96 { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
97 { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
98 { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
99 { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
100 { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
101 { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
102 { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
103 { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
104 { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
105 { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
106 { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
107 { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
108 { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
109 { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
110 { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
111 { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
112 { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
113 { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
114 { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
115 { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
116 { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
117 { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
118 { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
121 static struct channel_map_table map_tables[] = {
122 { SNDRV_CHMAP_FL, 0x00, FL },
123 { SNDRV_CHMAP_FR, 0x01, FR },
124 { SNDRV_CHMAP_RL, 0x04, RL },
125 { SNDRV_CHMAP_RR, 0x05, RR },
126 { SNDRV_CHMAP_LFE, 0x02, LFE },
127 { SNDRV_CHMAP_FC, 0x03, FC },
128 { SNDRV_CHMAP_RLC, 0x06, RLC },
129 { SNDRV_CHMAP_RRC, 0x07, RRC },
133 /* hardware capability structure */
134 static const struct snd_pcm_hardware snd_intel_hadstream = {
135 .info = (SNDRV_PCM_INFO_INTERLEAVED |
136 SNDRV_PCM_INFO_DOUBLE |
138 SNDRV_PCM_INFO_MMAP_VALID |
139 SNDRV_PCM_INFO_BATCH),
140 .formats = (SNDRV_PCM_FMTBIT_S24 |
141 SNDRV_PCM_FMTBIT_U24),
142 .rates = SNDRV_PCM_RATE_32000 |
143 SNDRV_PCM_RATE_44100 |
144 SNDRV_PCM_RATE_48000 |
145 SNDRV_PCM_RATE_88200 |
146 SNDRV_PCM_RATE_96000 |
147 SNDRV_PCM_RATE_176400 |
148 SNDRV_PCM_RATE_192000,
149 .rate_min = HAD_MIN_RATE,
150 .rate_max = HAD_MAX_RATE,
151 .channels_min = HAD_MIN_CHANNEL,
152 .channels_max = HAD_MAX_CHANNEL,
153 .buffer_bytes_max = HAD_MAX_BUFFER,
154 .period_bytes_min = HAD_MIN_PERIOD_BYTES,
155 .period_bytes_max = HAD_MAX_PERIOD_BYTES,
156 .periods_min = HAD_MIN_PERIODS,
157 .periods_max = HAD_MAX_PERIODS,
158 .fifo_size = HAD_FIFO_SIZE,
161 /* Register access functions */
163 int had_get_hwstate(struct snd_intelhad *intelhaddata)
165 /* Check for device presence -SW state */
166 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
167 pr_debug("%s:Device not connected:%d\n", __func__,
168 intelhaddata->drv_status);
175 int had_get_caps(enum had_caps_list query, void *caps)
178 struct snd_intelhad *intelhaddata = had_data;
180 retval = had_get_hwstate(intelhaddata);
182 retval = mid_hdmi_audio_get_caps(query, caps);
187 int had_set_caps(enum had_caps_list set_element, void *caps)
190 struct snd_intelhad *intelhaddata = had_data;
192 retval = had_get_hwstate(intelhaddata);
194 retval = mid_hdmi_audio_set_caps(set_element, caps);
199 int had_read_register(u32 offset, u32 *data)
202 struct snd_intelhad *intelhaddata = had_data;
204 retval = had_get_hwstate(intelhaddata);
206 retval = mid_hdmi_audio_read(offset, data);
211 int had_write_register(u32 offset, u32 data)
214 struct snd_intelhad *intelhaddata = had_data;
216 retval = had_get_hwstate(intelhaddata);
218 retval = mid_hdmi_audio_write(offset, data);
223 int had_read_modify(u32 offset, u32 data, u32 mask)
226 struct snd_intelhad *intelhaddata = had_data;
228 retval = had_get_hwstate(intelhaddata);
230 retval = mid_hdmi_audio_rmw(offset, data, mask);
235 * function to read-modify
236 * AUD_CONFIG register on VLV2.The had_read_modify() function should not
237 * directly be used on VLV2 for updating AUD_CONFIG register.
239 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
240 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
241 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
242 * register. This field should be 1xy binary for configuration with 6 or
243 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
244 * causes the "channels" field to be updated as 0xy binary resulting in
245 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
246 * appropriate value when doing read-modify of AUD_CONFIG register.
248 * @substream: the current substream or NULL if no active substream
249 * @data : data to be written
253 static int had_read_modify_aud_config_v2(struct snd_pcm_substream *substream,
256 union aud_cfg cfg_val = {.cfg_regval = 0};
260 * If substream is NULL, there is no active stream.
261 * In this case just set channels to 2
264 channels = substream->runtime->channels;
267 cfg_val.cfg_regx_v2.num_ch = channels - 2;
269 data = data | cfg_val.cfg_regval;
270 mask = mask | AUD_CONFIG_CH_MASK_V2;
272 pr_debug("%s : data = %x, mask =%x\n", __func__, data, mask);
274 return had_read_modify(AUD_CONFIG, data, mask);
277 void snd_intelhad_enable_audio(struct snd_pcm_substream *substream, u8 enable)
279 had_read_modify_aud_config_v2(substream, enable, BIT(0));
282 static void snd_intelhad_reset_audio(u8 reset)
284 had_write_register(AUD_HDMI_STATUS_v2, reset);
288 * initialize audio channel status registers
289 * This function is called in the prepare callback
291 static int had_prog_status_reg(struct snd_pcm_substream *substream,
292 struct snd_intelhad *intelhaddata)
294 union aud_cfg cfg_val = {.cfg_regval = 0};
295 union aud_ch_status_0 ch_stat0 = {.status_0_regval = 0};
296 union aud_ch_status_1 ch_stat1 = {.status_1_regval = 0};
299 pr_debug("Entry %s\n", __func__);
301 ch_stat0.status_0_regx.lpcm_id = (intelhaddata->aes_bits &
302 IEC958_AES0_NONAUDIO)>>1;
303 ch_stat0.status_0_regx.clk_acc = (intelhaddata->aes_bits &
304 IEC958_AES3_CON_CLOCK)>>4;
305 cfg_val.cfg_regx_v2.val_bit = ch_stat0.status_0_regx.lpcm_id;
307 switch (substream->runtime->rate) {
308 case AUD_SAMPLE_RATE_32:
309 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_32KHZ;
312 case AUD_SAMPLE_RATE_44_1:
313 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_44KHZ;
315 case AUD_SAMPLE_RATE_48:
316 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_48KHZ;
318 case AUD_SAMPLE_RATE_88_2:
319 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_88KHZ;
321 case AUD_SAMPLE_RATE_96:
322 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_96KHZ;
324 case AUD_SAMPLE_RATE_176_4:
325 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_176KHZ;
327 case AUD_SAMPLE_RATE_192:
328 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_192KHZ;
332 /* control should never come here */
337 had_write_register(AUD_CH_STATUS_0, ch_stat0.status_0_regval);
339 format = substream->runtime->format;
341 if (format == SNDRV_PCM_FORMAT_S16_LE) {
342 ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_20;
343 ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_16BITS;
344 } else if (format == SNDRV_PCM_FORMAT_S24_LE) {
345 ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_24;
346 ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_24BITS;
348 ch_stat1.status_1_regx.max_wrd_len = 0;
349 ch_stat1.status_1_regx.wrd_len = 0;
351 had_write_register(AUD_CH_STATUS_1, ch_stat1.status_1_regval);
356 * function to initialize audio
357 * registers and buffer confgiuration registers
358 * This function is called in the prepare callback
360 static int snd_intelhad_audio_ctrl(struct snd_pcm_substream *substream,
361 struct snd_intelhad *intelhaddata)
363 union aud_cfg cfg_val = {.cfg_regval = 0};
364 union aud_buf_config buf_cfg = {.buf_cfgval = 0};
367 had_prog_status_reg(substream, intelhaddata);
369 buf_cfg.buf_cfg_regx_v2.audio_fifo_watermark = FIFO_THRESHOLD;
370 buf_cfg.buf_cfg_regx_v2.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
371 buf_cfg.buf_cfg_regx_v2.aud_delay = 0;
372 had_write_register(AUD_BUF_CONFIG, buf_cfg.buf_cfgval);
374 channels = substream->runtime->channels;
375 cfg_val.cfg_regx_v2.num_ch = channels - 2;
377 cfg_val.cfg_regx_v2.layout = LAYOUT0;
379 cfg_val.cfg_regx_v2.layout = LAYOUT1;
381 cfg_val.cfg_regx_v2.val_bit = 1;
382 had_write_register(AUD_CONFIG, cfg_val.cfg_regval);
387 * Compute derived values in channel_allocations[].
389 static void init_channel_allocations(void)
392 struct cea_channel_speaker_allocation *p;
394 pr_debug("%s: Enter\n", __func__);
396 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
397 p = channel_allocations + i;
400 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
401 if (p->speakers[j]) {
403 p->spk_mask |= p->speakers[j];
409 * The transformation takes two steps:
411 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
412 * spk_mask => (channel_allocations[]) => ai->CA
414 * TODO: it could select the wrong CA from multiple candidates.
416 static int snd_intelhad_channel_allocation(struct snd_intelhad *intelhaddata,
424 * CA defaults to 0 for basic stereo audio
430 * expand ELD's speaker allocation mask
432 * ELD tells the speaker mask in a compact(paired) form,
433 * expand ELD's notions to match the ones used by Audio InfoFrame.
436 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
437 if (intelhaddata->eeld.speaker_allocation_block & (1 << i))
438 spk_mask |= eld_speaker_allocation_bits[i];
441 /* search for the first working match in the CA table */
442 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
443 if (channels == channel_allocations[i].channels &&
444 (spk_mask & channel_allocations[i].spk_mask) ==
445 channel_allocations[i].spk_mask) {
446 ca = channel_allocations[i].ca_index;
451 pr_debug("HDMI: select CA 0x%x for %d\n", ca, channels);
456 /* from speaker bit mask to ALSA API channel position */
457 static int spk_to_chmap(int spk)
459 struct channel_map_table *t = map_tables;
461 for (; t->map; t++) {
462 if (t->spk_mask == spk)
468 void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
472 struct snd_pcm_chmap_elem *chmap;
473 u8 eld_high, eld_high_mask = 0xF0;
476 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
478 intelhaddata->chmap->chmap = NULL;
482 had_get_caps(HAD_GET_ELD, &intelhaddata->eeld);
483 had_get_caps(HAD_GET_DP_OUTPUT, &intelhaddata->dp_output);
485 pr_debug("eeld.speaker_allocation_block = %x\n",
486 intelhaddata->eeld.speaker_allocation_block);
488 /* WA: Fix the max channel supported to 8 */
491 * Sink may support more than 8 channels, if eld_high has more than
492 * one bit set. SOC supports max 8 channels.
493 * Refer eld_speaker_allocation_bits, for sink speaker allocation
496 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
497 eld_high = intelhaddata->eeld.speaker_allocation_block & eld_high_mask;
498 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
499 /* eld_high & (eld_high-1): if more than 1 bit set */
500 /* 0x1F: 7 channels */
501 for (i = 1; i < 4; i++) {
502 high_msb = eld_high & (0x80 >> i);
504 intelhaddata->eeld.speaker_allocation_block &=
511 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
512 if (intelhaddata->eeld.speaker_allocation_block & (1 << i))
513 spk_mask |= eld_speaker_allocation_bits[i];
516 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
517 if (spk_mask == channel_allocations[i].spk_mask) {
518 for (c = 0; c < channel_allocations[i].channels; c++) {
519 chmap->map[c] = spk_to_chmap(
520 channel_allocations[i].speakers[
521 (MAX_SPEAKERS - 1)-c]);
523 chmap->channels = channel_allocations[i].channels;
524 intelhaddata->chmap->chmap = chmap;
528 if (i >= ARRAY_SIZE(channel_allocations)) {
529 intelhaddata->chmap->chmap = NULL;
535 * ALSA API channel-map control callbacks
537 static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
538 struct snd_ctl_elem_info *uinfo)
540 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
541 struct snd_intelhad *intelhaddata = info->private_data;
543 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
545 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
546 uinfo->count = HAD_MAX_CHANNEL;
547 uinfo->value.integer.min = 0;
548 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
552 static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
553 struct snd_ctl_elem_value *ucontrol)
555 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
556 struct snd_intelhad *intelhaddata = info->private_data;
558 const struct snd_pcm_chmap_elem *chmap;
560 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
562 if (intelhaddata->chmap->chmap == NULL)
564 chmap = intelhaddata->chmap->chmap;
565 for (i = 0; i < chmap->channels; i++) {
566 ucontrol->value.integer.value[i] = chmap->map[i];
567 pr_debug("chmap->map[%d] = %d\n", i, chmap->map[i]);
573 static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
578 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
579 NULL, 0, (unsigned long)intelhaddata,
580 &intelhaddata->chmap);
584 intelhaddata->chmap->private_data = intelhaddata;
585 intelhaddata->kctl = intelhaddata->chmap->kctl;
586 intelhaddata->kctl->info = had_chmap_ctl_info;
587 intelhaddata->kctl->get = had_chmap_ctl_get;
588 intelhaddata->chmap->chmap = NULL;
593 * snd_intelhad_prog_dip - to initialize Data Island Packets registers
595 * @substream:substream for which the prepare function is called
596 * @intelhaddata:substream private data
598 * This function is called in the prepare callback
600 static void snd_intelhad_prog_dip(struct snd_pcm_substream *substream,
601 struct snd_intelhad *intelhaddata)
604 union aud_ctrl_st ctrl_state = {.ctrl_val = 0};
605 union aud_info_frame2 frame2 = {.fr2_val = 0};
606 union aud_info_frame3 frame3 = {.fr3_val = 0};
611 channels = substream->runtime->channels;
613 had_write_register(AUD_CNTL_ST, ctrl_state.ctrl_val);
615 if (intelhaddata->dp_output) {
616 info_frame = DP_INFO_FRAME_WORD1;
619 info_frame = HDMI_INFO_FRAME_WORD1;
620 frame2.fr2_regx.chnl_cnt = substream->runtime->channels - 1;
622 frame3.fr3_regx.chnl_alloc = snd_intelhad_channel_allocation(
623 intelhaddata, channels);
625 /*Calculte the byte wide checksum for all valid DIP words*/
626 for (i = 0; i < BYTES_PER_WORD; i++)
627 checksum += (info_frame >> i*BITS_PER_BYTE) & MASK_BYTE0;
628 for (i = 0; i < BYTES_PER_WORD; i++)
629 checksum += (frame2.fr2_val >> i*BITS_PER_BYTE) & MASK_BYTE0;
630 for (i = 0; i < BYTES_PER_WORD; i++)
631 checksum += (frame3.fr3_val >> i*BITS_PER_BYTE) & MASK_BYTE0;
633 frame2.fr2_regx.chksum = -(checksum);
636 had_write_register(AUD_HDMIW_INFOFR_v2, info_frame);
637 had_write_register(AUD_HDMIW_INFOFR_v2, frame2.fr2_val);
638 had_write_register(AUD_HDMIW_INFOFR_v2, frame3.fr3_val);
640 /* program remaining DIP words with zero */
641 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
642 had_write_register(AUD_HDMIW_INFOFR_v2, 0x0);
644 ctrl_state.ctrl_regx.dip_freq = 1;
645 ctrl_state.ctrl_regx.dip_en_sta = 1;
646 had_write_register(AUD_CNTL_ST, ctrl_state.ctrl_val);
650 * snd_intelhad_prog_buffer - programs buffer
651 * address and length registers
653 * @substream:substream for which the prepare function is called
654 * @intelhaddata:substream private data
656 * This function programs ring buffer address and length into registers.
658 int snd_intelhad_prog_buffer(struct snd_intelhad *intelhaddata,
661 u32 ring_buf_addr, ring_buf_size, period_bytes;
663 struct snd_pcm_substream *substream;
665 substream = intelhaddata->stream_info.had_substream;
667 pr_err("substream is NULL\n");
672 ring_buf_addr = substream->runtime->dma_addr;
673 ring_buf_size = snd_pcm_lib_buffer_bytes(substream);
674 intelhaddata->stream_info.ring_buf_size = ring_buf_size;
675 period_bytes = frames_to_bytes(substream->runtime,
676 substream->runtime->period_size);
677 num_periods = substream->runtime->periods;
680 * buffer addr should be 64 byte aligned, period bytes
681 * will be used to calculate addr offset
683 period_bytes &= ~0x3F;
685 /* Hardware supports MAX_PERIODS buffers */
686 if (end >= HAD_MAX_PERIODS)
689 for (i = start; i <= end; i++) {
690 /* Program the buf registers with addr and len */
691 intelhaddata->buf_info[i].buf_addr = ring_buf_addr +
693 if (i < num_periods-1)
694 intelhaddata->buf_info[i].buf_size = period_bytes;
696 intelhaddata->buf_info[i].buf_size = ring_buf_size -
699 had_write_register(AUD_BUF_A_ADDR + (i * HAD_REG_WIDTH),
700 intelhaddata->buf_info[i].buf_addr |
702 had_write_register(AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
704 intelhaddata->buf_info[i].is_valid = true;
706 pr_debug("%s:buf[%d-%d] addr=%#x and size=%d\n", __func__, start, end,
707 intelhaddata->buf_info[start].buf_addr,
708 intelhaddata->buf_info[start].buf_size);
709 intelhaddata->valid_buf_cnt = num_periods;
713 int snd_intelhad_read_len(struct snd_intelhad *intelhaddata)
718 for (i = 0; i < 4 ; i++) {
719 had_read_register(AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
725 for (i = 0; i < 4 ; i++)
726 pr_debug("buf[%d] size=%d\n", i, len[i]);
732 static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
736 /* Select maud according to DP 1.2 spec*/
737 if (link_rate == DP_2_7_GHZ) {
738 switch (aud_samp_freq) {
739 case AUD_SAMPLE_RATE_32:
740 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
743 case AUD_SAMPLE_RATE_44_1:
744 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
747 case AUD_SAMPLE_RATE_48:
748 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
751 case AUD_SAMPLE_RATE_88_2:
752 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
755 case AUD_SAMPLE_RATE_96:
756 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
759 case AUD_SAMPLE_RATE_176_4:
760 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
764 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
771 } else if (link_rate == DP_1_62_GHZ) {
772 switch (aud_samp_freq) {
773 case AUD_SAMPLE_RATE_32:
774 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
777 case AUD_SAMPLE_RATE_44_1:
778 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
781 case AUD_SAMPLE_RATE_48:
782 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
785 case AUD_SAMPLE_RATE_88_2:
786 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
789 case AUD_SAMPLE_RATE_96:
790 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
793 case AUD_SAMPLE_RATE_176_4:
794 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
798 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
812 * snd_intelhad_prog_cts - Program HDMI audio CTS value
814 * @aud_samp_freq: sampling frequency of audio data
815 * @tmds: sampling frequency of the display data
816 * @n_param: N value, depends on aud_samp_freq
817 * @intelhaddata:substream private data
819 * Program CTS register based on the audio and display sampling frequency
821 static void snd_intelhad_prog_cts(u32 aud_samp_freq, u32 tmds,
822 u32 link_rate, u32 n_param,
823 struct snd_intelhad *intelhaddata)
826 u64 dividend, divisor;
828 if (intelhaddata->dp_output) {
829 /* Substitute cts_val with Maud according to DP 1.2 spec*/
830 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
832 /* Calculate CTS according to HDMI 1.3a spec*/
833 dividend = (u64)tmds * n_param*1000;
834 divisor = 128 * aud_samp_freq;
835 cts_val = div64_u64(dividend, divisor);
837 pr_debug("TMDS value=%d, N value=%d, CTS Value=%d\n",
838 tmds, n_param, cts_val);
839 had_write_register(AUD_HDMI_CTS, (BIT(24) | cts_val));
842 static int had_calculate_n_value(u32 aud_samp_freq)
846 /* Select N according to HDMI 1.3a spec*/
847 switch (aud_samp_freq) {
848 case AUD_SAMPLE_RATE_32:
852 case AUD_SAMPLE_RATE_44_1:
856 case AUD_SAMPLE_RATE_48:
860 case AUD_SAMPLE_RATE_88_2:
864 case AUD_SAMPLE_RATE_96:
868 case AUD_SAMPLE_RATE_176_4:
884 * snd_intelhad_prog_n - Program HDMI audio N value
886 * @aud_samp_freq: sampling frequency of audio data
887 * @n_param: N value, depends on aud_samp_freq
888 * @intelhaddata:substream private data
890 * This function is called in the prepare callback.
891 * It programs based on the audio and display sampling frequency
893 static int snd_intelhad_prog_n(u32 aud_samp_freq, u32 *n_param,
894 struct snd_intelhad *intelhaddata)
898 if (intelhaddata->dp_output) {
900 * According to DP specs, Maud and Naud values hold
901 * a relationship, which is stated as:
902 * Maud/Naud = 512 * fs / f_LS_Clk
903 * where, fs is the sampling frequency of the audio stream
904 * and Naud is 32768 for Async clock.
909 n_val = had_calculate_n_value(aud_samp_freq);
914 had_write_register(AUD_N_ENABLE, (BIT(24) | n_val));
919 void snd_intelhad_handle_underrun(struct snd_intelhad *intelhaddata)
921 u32 hdmi_status, i = 0;
923 /* Handle Underrun interrupt within Audio Unit */
924 had_write_register(AUD_CONFIG, 0);
925 /* Reset buffer pointers */
926 had_write_register(AUD_HDMI_STATUS_v2, 1);
927 had_write_register(AUD_HDMI_STATUS_v2, 0);
929 * The interrupt status 'sticky' bits might not be cleared by
930 * setting '1' to that bit once...
932 do { /* clear bit30, 31 AUD_HDMI_STATUS */
933 had_read_register(AUD_HDMI_STATUS_v2, &hdmi_status);
934 pr_debug("HDMI status =0x%x\n", hdmi_status);
935 if (hdmi_status & AUD_CONFIG_MASK_UNDERRUN) {
937 had_write_register(AUD_HDMI_STATUS_v2, hdmi_status);
940 } while (i < MAX_CNT);
942 pr_err("Unable to clear UNDERRUN bits\n");
946 * snd_intelhad_open - stream initializations are done here
947 * @substream:substream for which the stream function is called
949 * This function is called whenever a PCM stream is opened
951 static int snd_intelhad_open(struct snd_pcm_substream *substream)
953 struct snd_intelhad *intelhaddata;
954 struct snd_pcm_runtime *runtime;
955 struct had_stream_pvt *stream;
956 struct had_pvt_data *had_stream;
959 pr_debug("snd_intelhad_open called\n");
960 intelhaddata = snd_pcm_substream_chip(substream);
961 had_stream = intelhaddata->private_data;
962 runtime = substream->runtime;
965 pm_runtime_get(intelhaddata->dev);
967 if (had_get_hwstate(intelhaddata)) {
968 pr_err("%s: HDMI cable plugged-out\n", __func__);
970 goto exit_put_handle;
973 /* Check, if device already in use */
974 if (runtime->private_data) {
975 pr_err("Device already in use\n");
977 goto exit_put_handle;
980 /* set the runtime hw parameter with local snd_pcm_hardware struct */
981 runtime->hw = snd_intel_hadstream;
983 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
986 goto exit_put_handle;
988 stream->stream_status = STREAM_INIT;
989 runtime->private_data = stream;
991 retval = snd_pcm_hw_constraint_integer(runtime,
992 SNDRV_PCM_HW_PARAM_PERIODS);
996 /* Make sure, that the period size is always aligned
999 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1000 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
1002 pr_err("%s:step_size=64 failed,err=%d\n", __func__, retval);
1010 pm_runtime_put(intelhaddata->dev);
1011 runtime->private_data = NULL;
1016 * had_period_elapsed - updates the hardware pointer status
1017 * @had_substream:substream for which the stream function is called
1020 static void had_period_elapsed(void *had_substream)
1022 struct snd_pcm_substream *substream = had_substream;
1023 struct had_stream_pvt *stream;
1025 /* pr_debug("had_period_elapsed called\n"); */
1027 if (!substream || !substream->runtime)
1029 stream = substream->runtime->private_data;
1033 if (stream->stream_status != STREAM_RUNNING)
1035 snd_pcm_period_elapsed(substream);
1039 * snd_intelhad_init_stream - internal function to initialize stream info
1040 * @substream:substream for which the stream function is called
1043 static int snd_intelhad_init_stream(struct snd_pcm_substream *substream)
1045 struct snd_intelhad *intelhaddata = snd_pcm_substream_chip(substream);
1047 pr_debug("snd_intelhad_init_stream called\n");
1049 pr_debug("setting buffer ptr param\n");
1050 intelhaddata->stream_info.period_elapsed = had_period_elapsed;
1051 intelhaddata->stream_info.had_substream = substream;
1052 intelhaddata->stream_info.buffer_ptr = 0;
1053 intelhaddata->stream_info.buffer_rendered = 0;
1054 intelhaddata->stream_info.sfreq = substream->runtime->rate;
1059 * snd_intelhad_close- to free parameteres when stream is stopped
1061 * @substream: substream for which the function is called
1063 * This function is called by ALSA framework when stream is stopped
1065 static int snd_intelhad_close(struct snd_pcm_substream *substream)
1067 struct snd_intelhad *intelhaddata;
1068 struct snd_pcm_runtime *runtime;
1070 pr_debug("snd_intelhad_close called\n");
1072 intelhaddata = snd_pcm_substream_chip(substream);
1073 runtime = substream->runtime;
1075 if (!runtime->private_data) {
1076 pr_debug("close() might have called after failed open");
1080 intelhaddata->stream_info.buffer_rendered = 0;
1081 intelhaddata->stream_info.buffer_ptr = 0;
1082 intelhaddata->stream_info.str_id = 0;
1083 intelhaddata->stream_info.had_substream = NULL;
1085 /* Check if following drv_status modification is required - VA */
1086 if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) {
1087 intelhaddata->drv_status = HAD_DRV_CONNECTED;
1088 pr_debug("%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
1089 __func__, __LINE__);
1091 kfree(runtime->private_data);
1092 runtime->private_data = NULL;
1093 pm_runtime_put(intelhaddata->dev);
1098 * snd_intelhad_hw_params- to setup the hardware parameters
1099 * like allocating the buffers
1101 * @substream: substream for which the function is called
1102 * @hw_params: hardware parameters
1104 * This function is called by ALSA framework when hardware params are set
1106 static int snd_intelhad_hw_params(struct snd_pcm_substream *substream,
1107 struct snd_pcm_hw_params *hw_params)
1110 int pages, buf_size, retval;
1112 pr_debug("snd_intelhad_hw_params called\n");
1117 buf_size = params_buffer_bytes(hw_params);
1118 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1121 pr_debug("%s:allocated memory = %d\n", __func__, buf_size);
1122 /* mark the pages as uncached region */
1123 addr = (unsigned long) substream->runtime->dma_area;
1124 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1125 retval = set_memory_uc(addr, pages);
1127 pr_err("set_memory_uc failed.Error:%d\n", retval);
1130 memset(substream->runtime->dma_area, 0, buf_size);
1136 * snd_intelhad_hw_free- to release the resources allocated during
1137 * hardware params setup
1139 * @substream: substream for which the function is called
1141 * This function is called by ALSA framework before close callback.
1144 static int snd_intelhad_hw_free(struct snd_pcm_substream *substream)
1149 pr_debug("snd_intelhad_hw_free called\n");
1151 /* mark back the pages as cached/writeback region before the free */
1152 if (substream->runtime->dma_area != NULL) {
1153 addr = (unsigned long) substream->runtime->dma_area;
1154 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1156 set_memory_wb(addr, pages);
1157 return snd_pcm_lib_free_pages(substream);
1163 * snd_intelhad_pcm_trigger - stream activities are handled here
1164 * @substream:substream for which the stream function is called
1165 * @cmd:the stream commamd thats requested from upper layer
1166 * This function is called whenever an a stream activity is invoked
1168 static int snd_intelhad_pcm_trigger(struct snd_pcm_substream *substream,
1171 int caps, retval = 0;
1172 unsigned long flag_irq;
1173 struct snd_intelhad *intelhaddata;
1174 struct had_stream_pvt *stream;
1175 struct had_pvt_data *had_stream;
1177 pr_debug("snd_intelhad_pcm_trigger called\n");
1179 intelhaddata = snd_pcm_substream_chip(substream);
1180 stream = substream->runtime->private_data;
1181 had_stream = intelhaddata->private_data;
1184 case SNDRV_PCM_TRIGGER_START:
1185 pr_debug("Trigger Start\n");
1187 /* Disable local INTRs till register prgmng is done */
1188 if (had_get_hwstate(intelhaddata)) {
1189 pr_err("_START: HDMI cable plugged-out\n");
1193 stream->stream_status = STREAM_RUNNING;
1195 had_stream->stream_type = HAD_RUNNING_STREAM;
1199 * ToDo: Need to enable UNDERRUN interrupts as well
1200 * caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
1202 caps = HDMI_AUDIO_BUFFER_DONE;
1203 retval = had_set_caps(HAD_SET_ENABLE_AUDIO_INT, &caps);
1204 retval = had_set_caps(HAD_SET_ENABLE_AUDIO, NULL);
1205 snd_intelhad_enable_audio(substream, 1);
1207 pr_debug("Processed _Start\n");
1211 case SNDRV_PCM_TRIGGER_STOP:
1212 pr_debug("Trigger Stop\n");
1213 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irq);
1214 intelhaddata->stream_info.str_id = 0;
1215 intelhaddata->curr_buf = 0;
1217 /* Stop reporting BUFFER_DONE/UNDERRUN to above layers*/
1219 had_stream->stream_type = HAD_INIT;
1220 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irq);
1223 * ToDo: Need to disable UNDERRUN interrupts as well
1224 * caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
1226 caps = HDMI_AUDIO_BUFFER_DONE;
1227 had_set_caps(HAD_SET_DISABLE_AUDIO_INT, &caps);
1228 snd_intelhad_enable_audio(substream, 0);
1229 /* Reset buffer pointers */
1230 snd_intelhad_reset_audio(1);
1231 snd_intelhad_reset_audio(0);
1232 stream->stream_status = STREAM_DROPPED;
1233 had_set_caps(HAD_SET_DISABLE_AUDIO, NULL);
1243 * snd_intelhad_pcm_prepare- internal preparation before starting a stream
1245 * @substream: substream for which the function is called
1247 * This function is called when a stream is started for internal preparation.
1249 static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream)
1252 u32 disp_samp_freq, n_param;
1254 struct snd_intelhad *intelhaddata;
1255 struct snd_pcm_runtime *runtime;
1256 struct had_pvt_data *had_stream;
1258 pr_debug("snd_intelhad_pcm_prepare called\n");
1260 intelhaddata = snd_pcm_substream_chip(substream);
1261 runtime = substream->runtime;
1262 had_stream = intelhaddata->private_data;
1264 if (had_get_hwstate(intelhaddata)) {
1265 pr_err("%s: HDMI cable plugged-out\n", __func__);
1270 pr_debug("period_size=%d\n",
1271 (int)frames_to_bytes(runtime, runtime->period_size));
1272 pr_debug("periods=%d\n", runtime->periods);
1273 pr_debug("buffer_size=%d\n", (int)snd_pcm_lib_buffer_bytes(substream));
1274 pr_debug("rate=%d\n", runtime->rate);
1275 pr_debug("channels=%d\n", runtime->channels);
1277 if (intelhaddata->stream_info.str_id) {
1278 pr_debug("_prepare is called for existing str_id#%d\n",
1279 intelhaddata->stream_info.str_id);
1280 retval = snd_intelhad_pcm_trigger(substream,
1281 SNDRV_PCM_TRIGGER_STOP);
1285 retval = snd_intelhad_init_stream(substream);
1290 /* Get N value in KHz */
1291 retval = had_get_caps(HAD_GET_DISPLAY_RATE, &disp_samp_freq);
1293 pr_err("querying display sampling freq failed %#x\n", retval);
1297 had_get_caps(HAD_GET_ELD, &intelhaddata->eeld);
1298 had_get_caps(HAD_GET_DP_OUTPUT, &intelhaddata->dp_output);
1300 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1303 pr_err("programming N value failed %#x\n", retval);
1307 if (intelhaddata->dp_output)
1308 had_get_caps(HAD_GET_LINK_RATE, &link_rate);
1311 snd_intelhad_prog_cts(substream->runtime->rate,
1312 disp_samp_freq, link_rate,
1313 n_param, intelhaddata);
1315 snd_intelhad_prog_dip(substream, intelhaddata);
1317 retval = snd_intelhad_audio_ctrl(substream, intelhaddata);
1319 /* Prog buffer address */
1320 retval = snd_intelhad_prog_buffer(intelhaddata,
1321 HAD_BUF_TYPE_A, HAD_BUF_TYPE_D);
1324 * Program channel mapping in following order:
1325 * FL, FR, C, LFE, RL, RR
1328 had_write_register(AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
1335 * snd_intelhad_pcm_pointer- to send the current buffer pointerprocessed by hw
1337 * @substream: substream for which the function is called
1339 * This function is called by ALSA framework to get the current hw buffer ptr
1340 * when a period is elapsed
1342 static snd_pcm_uframes_t snd_intelhad_pcm_pointer(
1343 struct snd_pcm_substream *substream)
1345 struct snd_intelhad *intelhaddata;
1346 u32 bytes_rendered = 0;
1350 /* pr_debug("snd_intelhad_pcm_pointer called\n"); */
1352 intelhaddata = snd_pcm_substream_chip(substream);
1354 if (intelhaddata->flag_underrun) {
1355 intelhaddata->flag_underrun = 0;
1356 return SNDRV_PCM_POS_XRUN;
1359 /* Use a hw register to calculate sub-period position reports.
1360 * This makes PulseAudio happier.
1363 buf_id = intelhaddata->curr_buf % 4;
1364 had_read_register(AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH), &t);
1366 if ((t == 0) || (t == ((u32)-1L))) {
1368 pr_debug("discovered buffer done for buf %d, count = %d\n",
1369 buf_id, underrun_count);
1371 if (underrun_count > (HAD_MIN_PERIODS/2)) {
1372 pr_debug("assume audio_codec_reset, underrun = %d - do xrun\n",
1375 return SNDRV_PCM_POS_XRUN;
1382 t = intelhaddata->buf_info[buf_id].buf_size - t;
1384 if (intelhaddata->stream_info.buffer_rendered)
1385 div_u64_rem(intelhaddata->stream_info.buffer_rendered,
1386 intelhaddata->stream_info.ring_buf_size,
1389 intelhaddata->stream_info.buffer_ptr = bytes_to_frames(
1391 bytes_rendered + t);
1392 return intelhaddata->stream_info.buffer_ptr;
1396 * snd_intelhad_pcm_mmap- mmaps a kernel buffer to user space for copying data
1398 * @substream: substream for which the function is called
1399 * @vma: struct instance of memory VMM memory area
1401 * This function is called by OS when a user space component
1402 * tries to get mmap memory from driver
1404 static int snd_intelhad_pcm_mmap(struct snd_pcm_substream *substream,
1405 struct vm_area_struct *vma)
1408 pr_debug("snd_intelhad_pcm_mmap called\n");
1410 pr_debug("entry with prot:%s\n", __func__);
1411 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1412 return remap_pfn_range(vma, vma->vm_start,
1413 substream->dma_buffer.addr >> PAGE_SHIFT,
1414 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1417 int hdmi_audio_mode_change(struct snd_pcm_substream *substream)
1420 u32 disp_samp_freq, n_param;
1422 struct snd_intelhad *intelhaddata;
1424 intelhaddata = snd_pcm_substream_chip(substream);
1427 snd_intelhad_enable_audio(substream, 0);
1429 /* Update CTS value */
1430 retval = had_get_caps(HAD_GET_DISPLAY_RATE, &disp_samp_freq);
1432 pr_err("querying display sampling freq failed %#x\n", retval);
1436 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1439 pr_err("programming N value failed %#x\n", retval);
1443 if (intelhaddata->dp_output)
1444 had_get_caps(HAD_GET_LINK_RATE, &link_rate);
1446 snd_intelhad_prog_cts(substream->runtime->rate,
1447 disp_samp_freq, link_rate,
1448 n_param, intelhaddata);
1451 snd_intelhad_enable_audio(substream, 1);
1457 /*PCM operations structure and the calls back for the same */
1458 struct snd_pcm_ops snd_intelhad_playback_ops = {
1459 .open = snd_intelhad_open,
1460 .close = snd_intelhad_close,
1461 .ioctl = snd_pcm_lib_ioctl,
1462 .hw_params = snd_intelhad_hw_params,
1463 .hw_free = snd_intelhad_hw_free,
1464 .prepare = snd_intelhad_pcm_prepare,
1465 .trigger = snd_intelhad_pcm_trigger,
1466 .pointer = snd_intelhad_pcm_pointer,
1467 .mmap = snd_intelhad_pcm_mmap,
1471 * snd_intelhad_create - to crete alsa card instance
1473 * @intelhaddata: pointer to internal context
1474 * @card: pointer to card
1476 * This function is called when the hdmi cable is plugged in
1478 static int snd_intelhad_create(
1479 struct snd_intelhad *intelhaddata,
1480 struct snd_card *card)
1483 static struct snd_device_ops ops = {
1486 pr_debug("snd_intelhad_create called\n");
1491 /* ALSA api to register the device */
1492 retval = snd_device_new(card, SNDRV_DEV_LOWLEVEL, intelhaddata, &ops);
1496 * snd_intelhad_pcm_free - to free the memory allocated
1498 * @pcm: pointer to pcm instance
1499 * This function is called when the device is removed
1501 static void snd_intelhad_pcm_free(struct snd_pcm *pcm)
1503 pr_debug("Freeing PCM preallocated pages\n");
1504 snd_pcm_lib_preallocate_free_for_all(pcm);
1507 static int had_iec958_info(struct snd_kcontrol *kcontrol,
1508 struct snd_ctl_elem_info *uinfo)
1510 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1515 static int had_iec958_get(struct snd_kcontrol *kcontrol,
1516 struct snd_ctl_elem_value *ucontrol)
1518 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1520 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1521 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1522 ucontrol->value.iec958.status[2] =
1523 (intelhaddata->aes_bits >> 16) & 0xff;
1524 ucontrol->value.iec958.status[3] =
1525 (intelhaddata->aes_bits >> 24) & 0xff;
1528 static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1529 struct snd_ctl_elem_value *ucontrol)
1531 ucontrol->value.iec958.status[0] = 0xff;
1532 ucontrol->value.iec958.status[1] = 0xff;
1533 ucontrol->value.iec958.status[2] = 0xff;
1534 ucontrol->value.iec958.status[3] = 0xff;
1537 static int had_iec958_put(struct snd_kcontrol *kcontrol,
1538 struct snd_ctl_elem_value *ucontrol)
1541 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1543 pr_debug("entered had_iec958_put\n");
1544 val = (ucontrol->value.iec958.status[0] << 0) |
1545 (ucontrol->value.iec958.status[1] << 8) |
1546 (ucontrol->value.iec958.status[2] << 16) |
1547 (ucontrol->value.iec958.status[3] << 24);
1548 if (intelhaddata->aes_bits != val) {
1549 intelhaddata->aes_bits = val;
1555 static struct snd_kcontrol_new had_control_iec958_mask = {
1556 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1557 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1558 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1559 .info = had_iec958_info, /* shared */
1560 .get = had_iec958_mask_get,
1563 static struct snd_kcontrol_new had_control_iec958 = {
1564 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1565 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1566 .info = had_iec958_info,
1567 .get = had_iec958_get,
1568 .put = had_iec958_put
1571 static struct snd_intel_had_interface had_interface = {
1572 .name = "hdmi-audio",
1573 .query = hdmi_audio_query,
1574 .suspend = hdmi_audio_suspend,
1575 .resume = hdmi_audio_resume,
1579 * hdmi_audio_probe - to create sound card instance for HDMI audio playabck
1581 *@haddata: pointer to HAD private data
1582 *@card_id: card for which probe is called
1584 * This function is called when the hdmi cable is plugged in. This function
1585 * creates and registers the sound card with ALSA
1587 int hdmi_audio_probe(void *deviceptr)
1590 struct snd_pcm *pcm;
1591 struct snd_card *card;
1592 struct had_callback_ops ops_cb;
1593 struct snd_intelhad *intelhaddata;
1594 struct had_pvt_data *had_stream;
1595 struct platform_device *devptr = deviceptr;
1597 pr_debug("Enter %s\n", __func__);
1599 pr_debug("hdmi_audio_probe dma_mask: %p\n", devptr->dev.dma_mask);
1601 /* allocate memory for saving internal context and working */
1602 intelhaddata = kzalloc(sizeof(*intelhaddata), GFP_KERNEL);
1606 had_stream = kzalloc(sizeof(*had_stream), GFP_KERNEL);
1612 had_data = intelhaddata;
1613 ops_cb.intel_had_event_call_back = had_event_handler;
1615 /* registering with display driver to get access to display APIs */
1617 retval = mid_hdmi_audio_setup(ops_cb.intel_had_event_call_back);
1619 pr_err("querying display driver APIs failed %#x\n", retval);
1620 goto free_hadstream;
1622 mutex_lock(&had_mutex);
1623 spin_lock_init(&intelhaddata->had_spinlock);
1624 intelhaddata->drv_status = HAD_DRV_DISCONNECTED;
1625 pr_debug("%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
1626 __func__, __LINE__);
1628 /* create a card instance with ALSA framework */
1629 retval = snd_card_new(&devptr->dev, hdmi_card_index, hdmi_card_id,
1630 THIS_MODULE, 0, &card);
1634 intelhaddata->card = card;
1635 intelhaddata->card_id = hdmi_card_id;
1636 intelhaddata->card_index = card->number;
1637 intelhaddata->private_data = had_stream;
1638 intelhaddata->flag_underrun = 0;
1639 intelhaddata->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
1640 strncpy(card->driver, INTEL_HAD, strlen(INTEL_HAD));
1641 strncpy(card->shortname, INTEL_HAD, strlen(INTEL_HAD));
1643 retval = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
1644 MAX_CAP_STREAMS, &pcm);
1648 /* setup private data which can be retrieved when required */
1649 pcm->private_data = intelhaddata;
1650 pcm->private_free = snd_intelhad_pcm_free;
1651 pcm->info_flags = 0;
1652 strncpy(pcm->name, card->shortname, strlen(card->shortname));
1653 /* setup the ops for palyabck */
1654 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1655 &snd_intelhad_playback_ops);
1656 /* allocate dma pages for ALSA stream operations
1657 * memory allocated is based on size, not max value
1658 * thus using same argument for max & size
1660 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
1661 SNDRV_DMA_TYPE_DEV, NULL,
1662 HAD_MAX_BUFFER, HAD_MAX_BUFFER);
1664 if (card->dev == NULL)
1665 pr_debug("card->dev is NULL!!!!! Should not be this case\n");
1666 else if (card->dev->dma_mask == NULL)
1667 pr_debug("hdmi_audio_probe dma_mask is NULL!!!!!\n");
1669 pr_debug("hdmi_audio_probe dma_mask is : %p\n",
1670 card->dev->dma_mask);
1675 /* internal function call to register device with ALSA */
1676 retval = snd_intelhad_create(intelhaddata, card);
1680 card->private_data = &intelhaddata;
1681 retval = snd_card_register(card);
1685 /* IEC958 controls */
1686 retval = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958_mask,
1690 retval = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958,
1695 init_channel_allocations();
1697 /* Register channel map controls */
1698 retval = had_register_chmap_ctls(intelhaddata, pcm);
1702 intelhaddata->dev = &devptr->dev;
1703 pm_runtime_set_active(intelhaddata->dev);
1704 pm_runtime_enable(intelhaddata->dev);
1706 mutex_unlock(&had_mutex);
1707 retval = mid_hdmi_audio_register(&had_interface, intelhaddata);
1709 pr_err("registering with display driver failed %#x\n", retval);
1710 snd_card_free(card);
1711 goto free_hadstream;
1714 intelhaddata->hw_silence = 1;
1718 snd_card_free(card);
1720 mutex_unlock(&had_mutex);
1723 pm_runtime_disable(intelhaddata->dev);
1724 intelhaddata->dev = NULL;
1726 kfree(intelhaddata);
1727 intelhaddata = NULL;
1728 pr_err("Error returned from %s api %#x\n", __func__, retval);
1733 * hdmi_audio_remove - removes the alsa card
1735 *@haddata: pointer to HAD private data
1737 * This function is called when the hdmi cable is un-plugged. This function
1738 * free the sound card.
1740 int hdmi_audio_remove(void *pdevptr)
1742 struct snd_intelhad *intelhaddata = had_data;
1745 pr_debug("Enter %s\n", __func__);
1750 if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) {
1751 caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
1752 had_set_caps(HAD_SET_DISABLE_AUDIO_INT, &caps);
1753 had_set_caps(HAD_SET_DISABLE_AUDIO, NULL);
1755 snd_card_free(intelhaddata->card);
1756 kfree(intelhaddata->private_data);
1757 kfree(intelhaddata);
1761 MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1762 MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1763 MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1764 MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1765 MODULE_DESCRIPTION("Intel HDMI Audio driver");
1766 MODULE_LICENSE("GPL v2");
1767 MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");