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 #include <linux/types.h>
25 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/interrupt.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/dma-mapping.h>
32 #include <asm/cacheflush.h>
33 #include <sound/core.h>
34 #include <sound/asoundef.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
37 #include <sound/initval.h>
38 #include <sound/control.h>
39 #include <drm/drm_edid.h>
40 #include <drm/intel_lpe_audio.h>
41 #include "intel_hdmi_audio.h"
43 /*standard module options for ALSA. This module supports only one card*/
44 static int hdmi_card_index = SNDRV_DEFAULT_IDX1;
45 static char *hdmi_card_id = SNDRV_DEFAULT_STR1;
47 module_param_named(index, hdmi_card_index, int, 0444);
48 MODULE_PARM_DESC(index,
49 "Index value for INTEL Intel HDMI Audio controller.");
50 module_param_named(id, hdmi_card_id, charp, 0444);
52 "ID string for INTEL Intel HDMI Audio controller.");
55 * ELD SA bits in the CEA Speaker Allocation data block
57 static const int eld_speaker_allocation_bits[] = {
65 /* the following are not defined in ELD yet */
70 * This is an ordered list!
72 * The preceding ones have better chances to be selected by
73 * hdmi_channel_allocation().
75 static struct cea_channel_speaker_allocation channel_allocations[] = {
76 /* channel: 7 6 5 4 3 2 1 0 */
77 { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
79 { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
81 { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
83 { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
85 { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
87 { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
89 { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
91 { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
93 { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
95 { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
96 { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
97 { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
98 { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
99 { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
100 { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
101 { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
102 { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
103 { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
104 { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
105 { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
106 { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
107 { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
108 { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
109 { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
110 { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
111 { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
112 { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
113 { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
114 { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
115 { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
116 { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
117 { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
120 static const struct channel_map_table map_tables[] = {
121 { SNDRV_CHMAP_FL, 0x00, FL },
122 { SNDRV_CHMAP_FR, 0x01, FR },
123 { SNDRV_CHMAP_RL, 0x04, RL },
124 { SNDRV_CHMAP_RR, 0x05, RR },
125 { SNDRV_CHMAP_LFE, 0x02, LFE },
126 { SNDRV_CHMAP_FC, 0x03, FC },
127 { SNDRV_CHMAP_RLC, 0x06, RLC },
128 { SNDRV_CHMAP_RRC, 0x07, RRC },
132 /* hardware capability structure */
133 static const struct snd_pcm_hardware had_pcm_hardware = {
134 .info = (SNDRV_PCM_INFO_INTERLEAVED |
135 SNDRV_PCM_INFO_DOUBLE |
137 SNDRV_PCM_INFO_MMAP_VALID |
138 SNDRV_PCM_INFO_BATCH),
139 .formats = SNDRV_PCM_FMTBIT_S24,
140 .rates = SNDRV_PCM_RATE_32000 |
141 SNDRV_PCM_RATE_44100 |
142 SNDRV_PCM_RATE_48000 |
143 SNDRV_PCM_RATE_88200 |
144 SNDRV_PCM_RATE_96000 |
145 SNDRV_PCM_RATE_176400 |
146 SNDRV_PCM_RATE_192000,
147 .rate_min = HAD_MIN_RATE,
148 .rate_max = HAD_MAX_RATE,
149 .channels_min = HAD_MIN_CHANNEL,
150 .channels_max = HAD_MAX_CHANNEL,
151 .buffer_bytes_max = HAD_MAX_BUFFER,
152 .period_bytes_min = HAD_MIN_PERIOD_BYTES,
153 .period_bytes_max = HAD_MAX_PERIOD_BYTES,
154 .periods_min = HAD_MIN_PERIODS,
155 .periods_max = HAD_MAX_PERIODS,
156 .fifo_size = HAD_FIFO_SIZE,
159 /* Get the active PCM substream;
160 * Call had_substream_put() for unreferecing.
161 * Don't call this inside had_spinlock, as it takes by itself
163 static struct snd_pcm_substream *
164 had_substream_get(struct snd_intelhad *intelhaddata)
166 struct snd_pcm_substream *substream;
169 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
170 substream = intelhaddata->stream_info.substream;
172 intelhaddata->stream_info.substream_refcount++;
173 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
177 /* Unref the active PCM substream;
178 * Don't call this inside had_spinlock, as it takes by itself
180 static void had_substream_put(struct snd_intelhad *intelhaddata)
184 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
185 intelhaddata->stream_info.substream_refcount--;
186 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
189 /* Register access functions */
190 static void had_read_register(struct snd_intelhad *ctx, u32 reg, u32 *val)
192 *val = ioread32(ctx->mmio_start + ctx->had_config_offset + reg);
195 static void had_write_register(struct snd_intelhad *ctx, u32 reg, u32 val)
197 iowrite32(val, ctx->mmio_start + ctx->had_config_offset + reg);
201 * enable / disable audio configuration
203 * The normal read/modify should not directly be used on VLV2 for
204 * updating AUD_CONFIG register.
206 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
207 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
208 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
209 * register. This field should be 1xy binary for configuration with 6 or
210 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
211 * causes the "channels" field to be updated as 0xy binary resulting in
212 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
213 * appropriate value when doing read-modify of AUD_CONFIG register.
215 static void had_enable_audio(struct snd_pcm_substream *substream,
216 struct snd_intelhad *intelhaddata,
219 union aud_cfg cfg_val = {.regval = 0};
224 * If substream is NULL, there is no active stream.
225 * In this case just set channels to 2
227 channels = substream ? substream->runtime->channels : 2;
228 dev_dbg(intelhaddata->dev, "enable %d, ch=%d\n", enable, channels);
230 cfg_val.regx.num_ch = channels - 2;
232 cfg_val.regx.aud_en = 1;
233 mask = AUD_CONFIG_CH_MASK | 1;
235 had_read_register(intelhaddata, AUD_CONFIG, &val);
237 val |= cfg_val.regval;
238 had_write_register(intelhaddata, AUD_CONFIG, val);
241 /* enable / disable the audio interface */
242 static void had_enable_audio_int(struct snd_intelhad *ctx, bool enable)
247 had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
248 status_reg |= HDMI_AUDIO_BUFFER_DONE | HDMI_AUDIO_UNDERRUN;
249 had_write_register(ctx, AUD_HDMI_STATUS, status_reg);
250 had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
254 /* Reset buffer pointers */
255 static void had_reset_audio(struct snd_intelhad *intelhaddata)
257 had_write_register(intelhaddata, AUD_HDMI_STATUS, 1);
258 had_write_register(intelhaddata, AUD_HDMI_STATUS, 0);
262 * initialize audio channel status registers
263 * This function is called in the prepare callback
265 static int had_prog_status_reg(struct snd_pcm_substream *substream,
266 struct snd_intelhad *intelhaddata)
268 union aud_cfg cfg_val = {.regval = 0};
269 union aud_ch_status_0 ch_stat0 = {.regval = 0};
270 union aud_ch_status_1 ch_stat1 = {.regval = 0};
273 ch_stat0.regx.lpcm_id = (intelhaddata->aes_bits &
274 IEC958_AES0_NONAUDIO) >> 1;
275 ch_stat0.regx.clk_acc = (intelhaddata->aes_bits &
276 IEC958_AES3_CON_CLOCK) >> 4;
277 cfg_val.regx.val_bit = ch_stat0.regx.lpcm_id;
279 switch (substream->runtime->rate) {
280 case AUD_SAMPLE_RATE_32:
281 ch_stat0.regx.samp_freq = CH_STATUS_MAP_32KHZ;
284 case AUD_SAMPLE_RATE_44_1:
285 ch_stat0.regx.samp_freq = CH_STATUS_MAP_44KHZ;
287 case AUD_SAMPLE_RATE_48:
288 ch_stat0.regx.samp_freq = CH_STATUS_MAP_48KHZ;
290 case AUD_SAMPLE_RATE_88_2:
291 ch_stat0.regx.samp_freq = CH_STATUS_MAP_88KHZ;
293 case AUD_SAMPLE_RATE_96:
294 ch_stat0.regx.samp_freq = CH_STATUS_MAP_96KHZ;
296 case AUD_SAMPLE_RATE_176_4:
297 ch_stat0.regx.samp_freq = CH_STATUS_MAP_176KHZ;
299 case AUD_SAMPLE_RATE_192:
300 ch_stat0.regx.samp_freq = CH_STATUS_MAP_192KHZ;
304 /* control should never come here */
308 had_write_register(intelhaddata,
309 AUD_CH_STATUS_0, ch_stat0.regval);
311 format = substream->runtime->format;
313 if (format == SNDRV_PCM_FORMAT_S16_LE) {
314 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_20;
315 ch_stat1.regx.wrd_len = SMPL_WIDTH_16BITS;
316 } else if (format == SNDRV_PCM_FORMAT_S24_LE) {
317 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_24;
318 ch_stat1.regx.wrd_len = SMPL_WIDTH_24BITS;
320 ch_stat1.regx.max_wrd_len = 0;
321 ch_stat1.regx.wrd_len = 0;
324 had_write_register(intelhaddata,
325 AUD_CH_STATUS_1, ch_stat1.regval);
330 * function to initialize audio
331 * registers and buffer confgiuration registers
332 * This function is called in the prepare callback
334 static int had_init_audio_ctrl(struct snd_pcm_substream *substream,
335 struct snd_intelhad *intelhaddata)
337 union aud_cfg cfg_val = {.regval = 0};
338 union aud_buf_config buf_cfg = {.regval = 0};
341 had_prog_status_reg(substream, intelhaddata);
343 buf_cfg.regx.audio_fifo_watermark = FIFO_THRESHOLD;
344 buf_cfg.regx.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
345 buf_cfg.regx.aud_delay = 0;
346 had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.regval);
348 channels = substream->runtime->channels;
349 cfg_val.regx.num_ch = channels - 2;
351 cfg_val.regx.layout = LAYOUT0;
353 cfg_val.regx.layout = LAYOUT1;
355 cfg_val.regx.val_bit = 1;
357 /* fix up the DP bits */
358 if (intelhaddata->dp_output) {
359 cfg_val.regx.dp_modei = 1;
360 cfg_val.regx.set = 1;
363 had_write_register(intelhaddata, AUD_CONFIG, cfg_val.regval);
368 * Compute derived values in channel_allocations[].
370 static void init_channel_allocations(void)
373 struct cea_channel_speaker_allocation *p;
375 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
376 p = channel_allocations + i;
379 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
380 if (p->speakers[j]) {
382 p->spk_mask |= p->speakers[j];
388 * The transformation takes two steps:
390 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
391 * spk_mask => (channel_allocations[]) => ai->CA
393 * TODO: it could select the wrong CA from multiple candidates.
395 static int had_channel_allocation(struct snd_intelhad *intelhaddata,
403 * CA defaults to 0 for basic stereo audio
409 * expand ELD's speaker allocation mask
411 * ELD tells the speaker mask in a compact(paired) form,
412 * expand ELD's notions to match the ones used by Audio InfoFrame.
415 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
416 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
417 spk_mask |= eld_speaker_allocation_bits[i];
420 /* search for the first working match in the CA table */
421 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
422 if (channels == channel_allocations[i].channels &&
423 (spk_mask & channel_allocations[i].spk_mask) ==
424 channel_allocations[i].spk_mask) {
425 ca = channel_allocations[i].ca_index;
430 dev_dbg(intelhaddata->dev, "select CA 0x%x for %d\n", ca, channels);
435 /* from speaker bit mask to ALSA API channel position */
436 static int spk_to_chmap(int spk)
438 const struct channel_map_table *t = map_tables;
440 for (; t->map; t++) {
441 if (t->spk_mask == spk)
447 static void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
451 struct snd_pcm_chmap_elem *chmap;
452 u8 eld_high, eld_high_mask = 0xF0;
455 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
457 intelhaddata->chmap->chmap = NULL;
461 dev_dbg(intelhaddata->dev, "eld speaker = %x\n",
462 intelhaddata->eld[DRM_ELD_SPEAKER]);
464 /* WA: Fix the max channel supported to 8 */
467 * Sink may support more than 8 channels, if eld_high has more than
468 * one bit set. SOC supports max 8 channels.
469 * Refer eld_speaker_allocation_bits, for sink speaker allocation
472 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
473 eld_high = intelhaddata->eld[DRM_ELD_SPEAKER] & eld_high_mask;
474 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
475 /* eld_high & (eld_high-1): if more than 1 bit set */
476 /* 0x1F: 7 channels */
477 for (i = 1; i < 4; i++) {
478 high_msb = eld_high & (0x80 >> i);
480 intelhaddata->eld[DRM_ELD_SPEAKER] &=
487 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
488 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
489 spk_mask |= eld_speaker_allocation_bits[i];
492 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
493 if (spk_mask == channel_allocations[i].spk_mask) {
494 for (c = 0; c < channel_allocations[i].channels; c++) {
495 chmap->map[c] = spk_to_chmap(
496 channel_allocations[i].speakers[
497 (MAX_SPEAKERS - 1) - c]);
499 chmap->channels = channel_allocations[i].channels;
500 intelhaddata->chmap->chmap = chmap;
504 if (i >= ARRAY_SIZE(channel_allocations)) {
505 intelhaddata->chmap->chmap = NULL;
511 * ALSA API channel-map control callbacks
513 static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
514 struct snd_ctl_elem_info *uinfo)
516 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
517 struct snd_intelhad *intelhaddata = info->private_data;
519 if (!intelhaddata->connected)
521 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
522 uinfo->count = HAD_MAX_CHANNEL;
523 uinfo->value.integer.min = 0;
524 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
528 static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
529 struct snd_ctl_elem_value *ucontrol)
531 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
532 struct snd_intelhad *intelhaddata = info->private_data;
534 const struct snd_pcm_chmap_elem *chmap;
536 if (!intelhaddata->connected)
539 mutex_lock(&intelhaddata->mutex);
540 if (!intelhaddata->chmap->chmap) {
541 mutex_unlock(&intelhaddata->mutex);
545 chmap = intelhaddata->chmap->chmap;
546 for (i = 0; i < chmap->channels; i++)
547 ucontrol->value.integer.value[i] = chmap->map[i];
548 mutex_unlock(&intelhaddata->mutex);
553 static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
558 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
559 NULL, 0, (unsigned long)intelhaddata,
560 &intelhaddata->chmap);
564 intelhaddata->chmap->private_data = intelhaddata;
565 intelhaddata->chmap->kctl->info = had_chmap_ctl_info;
566 intelhaddata->chmap->kctl->get = had_chmap_ctl_get;
567 intelhaddata->chmap->chmap = NULL;
572 * Initialize Data Island Packets registers
573 * This function is called in the prepare callback
575 static void had_prog_dip(struct snd_pcm_substream *substream,
576 struct snd_intelhad *intelhaddata)
579 union aud_ctrl_st ctrl_state = {.regval = 0};
580 union aud_info_frame2 frame2 = {.regval = 0};
581 union aud_info_frame3 frame3 = {.regval = 0};
587 channels = substream->runtime->channels;
589 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
591 ca = had_channel_allocation(intelhaddata, channels);
592 if (intelhaddata->dp_output) {
593 info_frame = DP_INFO_FRAME_WORD1;
594 frame2.regval = (substream->runtime->channels - 1) | (ca << 24);
596 info_frame = HDMI_INFO_FRAME_WORD1;
597 frame2.regx.chnl_cnt = substream->runtime->channels - 1;
598 frame3.regx.chnl_alloc = ca;
600 /* Calculte the byte wide checksum for all valid DIP words */
601 for (i = 0; i < BYTES_PER_WORD; i++)
602 checksum += (info_frame >> (i * 8)) & 0xff;
603 for (i = 0; i < BYTES_PER_WORD; i++)
604 checksum += (frame2.regval >> (i * 8)) & 0xff;
605 for (i = 0; i < BYTES_PER_WORD; i++)
606 checksum += (frame3.regval >> (i * 8)) & 0xff;
608 frame2.regx.chksum = -(checksum);
611 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, info_frame);
612 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame2.regval);
613 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame3.regval);
615 /* program remaining DIP words with zero */
616 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
617 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, 0x0);
619 ctrl_state.regx.dip_freq = 1;
620 ctrl_state.regx.dip_en_sta = 1;
621 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
624 static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
628 /* Select maud according to DP 1.2 spec */
629 if (link_rate == DP_2_7_GHZ) {
630 switch (aud_samp_freq) {
631 case AUD_SAMPLE_RATE_32:
632 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
635 case AUD_SAMPLE_RATE_44_1:
636 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
639 case AUD_SAMPLE_RATE_48:
640 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
643 case AUD_SAMPLE_RATE_88_2:
644 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
647 case AUD_SAMPLE_RATE_96:
648 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
651 case AUD_SAMPLE_RATE_176_4:
652 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
656 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
663 } else if (link_rate == DP_1_62_GHZ) {
664 switch (aud_samp_freq) {
665 case AUD_SAMPLE_RATE_32:
666 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
669 case AUD_SAMPLE_RATE_44_1:
670 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
673 case AUD_SAMPLE_RATE_48:
674 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
677 case AUD_SAMPLE_RATE_88_2:
678 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
681 case AUD_SAMPLE_RATE_96:
682 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
685 case AUD_SAMPLE_RATE_176_4:
686 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
690 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
704 * Program HDMI audio CTS value
706 * @aud_samp_freq: sampling frequency of audio data
707 * @tmds: sampling frequency of the display data
708 * @link_rate: DP link rate
709 * @n_param: N value, depends on aud_samp_freq
710 * @intelhaddata: substream private data
712 * Program CTS register based on the audio and display sampling frequency
714 static void had_prog_cts(u32 aud_samp_freq, u32 tmds, u32 link_rate,
715 u32 n_param, struct snd_intelhad *intelhaddata)
718 u64 dividend, divisor;
720 if (intelhaddata->dp_output) {
721 /* Substitute cts_val with Maud according to DP 1.2 spec*/
722 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
724 /* Calculate CTS according to HDMI 1.3a spec*/
725 dividend = (u64)tmds * n_param*1000;
726 divisor = 128 * aud_samp_freq;
727 cts_val = div64_u64(dividend, divisor);
729 dev_dbg(intelhaddata->dev, "TMDS value=%d, N value=%d, CTS Value=%d\n",
730 tmds, n_param, cts_val);
731 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
734 static int had_calculate_n_value(u32 aud_samp_freq)
738 /* Select N according to HDMI 1.3a spec*/
739 switch (aud_samp_freq) {
740 case AUD_SAMPLE_RATE_32:
744 case AUD_SAMPLE_RATE_44_1:
748 case AUD_SAMPLE_RATE_48:
752 case AUD_SAMPLE_RATE_88_2:
756 case AUD_SAMPLE_RATE_96:
760 case AUD_SAMPLE_RATE_176_4:
776 * Program HDMI audio N value
778 * @aud_samp_freq: sampling frequency of audio data
779 * @n_param: N value, depends on aud_samp_freq
780 * @intelhaddata: substream private data
782 * This function is called in the prepare callback.
783 * It programs based on the audio and display sampling frequency
785 static int had_prog_n(u32 aud_samp_freq, u32 *n_param,
786 struct snd_intelhad *intelhaddata)
790 if (intelhaddata->dp_output) {
792 * According to DP specs, Maud and Naud values hold
793 * a relationship, which is stated as:
794 * Maud/Naud = 512 * fs / f_LS_Clk
795 * where, fs is the sampling frequency of the audio stream
796 * and Naud is 32768 for Async clock.
801 n_val = had_calculate_n_value(aud_samp_freq);
806 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
812 * PCM ring buffer handling
814 * The hardware provides a ring buffer with the fixed 4 buffer descriptors
815 * (BDs). The driver maps these 4 BDs onto the PCM ring buffer. The mapping
816 * moves at each period elapsed. The below illustrates how it works:
819 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
820 * BD | 0 | 1 | 2 | 3 |
822 * At time=1 (period elapsed)
823 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
824 * BD | 1 | 2 | 3 | 0 |
826 * At time=2 (second period elapsed)
827 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
828 * BD | 2 | 3 | 0 | 1 |
830 * The bd_head field points to the index of the BD to be read. It's also the
831 * position to be filled at next. The pcm_head and the pcm_filled fields
832 * point to the indices of the current position and of the next position to
833 * be filled, respectively. For PCM buffer there are both _head and _filled
834 * because they may be difference when nperiods > 4. For example, in the
835 * example above at t=1, bd_head=1 and pcm_head=1 while pcm_filled=5:
837 * pcm_head (=1) --v v-- pcm_filled (=5)
838 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
839 * BD | 1 | 2 | 3 | 0 |
840 * bd_head (=1) --^ ^-- next to fill (= bd_head)
842 * For nperiods < 4, the remaining BDs out of 4 are marked as invalid, so that
843 * the hardware skips those BDs in the loop.
846 #define AUD_BUF_ADDR(x) (AUD_BUF_A_ADDR + (x) * HAD_REG_WIDTH)
847 #define AUD_BUF_LEN(x) (AUD_BUF_A_LENGTH + (x) * HAD_REG_WIDTH)
849 /* Set up a buffer descriptor at the "filled" position */
850 static void had_prog_bd(struct snd_pcm_substream *substream,
851 struct snd_intelhad *intelhaddata)
853 int idx = intelhaddata->bd_head;
854 int ofs = intelhaddata->pcmbuf_filled * intelhaddata->period_bytes;
855 u32 addr = substream->runtime->dma_addr + ofs;
857 addr |= AUD_BUF_VALID | AUD_BUF_INTR_EN;
858 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), addr);
859 had_write_register(intelhaddata, AUD_BUF_LEN(idx),
860 intelhaddata->period_bytes);
862 /* advance the indices to the next */
863 intelhaddata->bd_head++;
864 intelhaddata->bd_head %= intelhaddata->num_bds;
865 intelhaddata->pcmbuf_filled++;
866 intelhaddata->pcmbuf_filled %= substream->runtime->periods;
869 /* invalidate a buffer descriptor with the given index */
870 static void had_invalidate_bd(struct snd_intelhad *intelhaddata,
873 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), 0);
874 had_write_register(intelhaddata, AUD_BUF_LEN(idx), 0);
877 /* Initial programming of ring buffer */
878 static void had_init_ringbuf(struct snd_pcm_substream *substream,
879 struct snd_intelhad *intelhaddata)
881 struct snd_pcm_runtime *runtime = substream->runtime;
884 num_periods = runtime->periods;
885 intelhaddata->num_bds = min(num_periods, HAD_NUM_OF_RING_BUFS);
886 intelhaddata->period_bytes =
887 frames_to_bytes(runtime, runtime->period_size);
888 WARN_ON(intelhaddata->period_bytes & 0x3f);
890 intelhaddata->bd_head = 0;
891 intelhaddata->pcmbuf_head = 0;
892 intelhaddata->pcmbuf_filled = 0;
894 for (i = 0; i < HAD_NUM_OF_RING_BUFS; i++) {
896 had_prog_bd(substream, intelhaddata);
897 else /* invalidate the rest */
898 had_invalidate_bd(intelhaddata, i);
901 intelhaddata->bd_head = 0; /* reset at head again before starting */
904 /* process a bd, advance to the next */
905 static void had_advance_ringbuf(struct snd_pcm_substream *substream,
906 struct snd_intelhad *intelhaddata)
908 int num_periods = substream->runtime->periods;
910 /* reprogram the next buffer */
911 had_prog_bd(substream, intelhaddata);
913 /* proceed to next */
914 intelhaddata->pcmbuf_head++;
915 intelhaddata->pcmbuf_head %= num_periods;
918 /* process the current BD(s);
919 * returns the current PCM buffer byte position, or -EPIPE for underrun.
921 static int had_process_ringbuf(struct snd_pcm_substream *substream,
922 struct snd_intelhad *intelhaddata)
928 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
930 /* get the remaining bytes on the buffer */
931 had_read_register(intelhaddata,
932 AUD_BUF_LEN(intelhaddata->bd_head),
934 if (len < 0 || len > intelhaddata->period_bytes) {
935 dev_dbg(intelhaddata->dev, "Invalid buf length %d\n",
941 if (len > 0) /* OK, this is the current buffer */
944 /* len=0 => already empty, check the next buffer */
945 if (++processed >= intelhaddata->num_bds) {
946 len = -EPIPE; /* all empty? - report underrun */
949 had_advance_ringbuf(substream, intelhaddata);
952 len = intelhaddata->period_bytes - len;
953 len += intelhaddata->period_bytes * intelhaddata->pcmbuf_head;
955 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
959 /* called from irq handler */
960 static void had_process_buffer_done(struct snd_intelhad *intelhaddata)
962 struct snd_pcm_substream *substream;
964 if (!intelhaddata->connected)
965 return; /* disconnected? - bail out */
967 substream = had_substream_get(intelhaddata);
969 return; /* no stream? - bail out */
971 /* process or stop the stream */
972 if (had_process_ringbuf(substream, intelhaddata) < 0)
973 snd_pcm_stop_xrun(substream);
975 snd_pcm_period_elapsed(substream);
977 had_substream_put(intelhaddata);
983 * The interrupt status 'sticky' bits might not be cleared by
984 * setting '1' to that bit once...
986 static void wait_clear_underrun_bit(struct snd_intelhad *intelhaddata)
991 for (i = 0; i < MAX_CNT; i++) {
992 /* clear bit30, 31 AUD_HDMI_STATUS */
993 had_read_register(intelhaddata, AUD_HDMI_STATUS, &val);
994 if (!(val & AUD_CONFIG_MASK_UNDERRUN))
996 had_write_register(intelhaddata, AUD_HDMI_STATUS, val);
998 dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n");
1001 /* called from irq handler */
1002 static void had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
1004 struct snd_pcm_substream *substream;
1006 /* Handle Underrun interrupt within Audio Unit */
1007 had_write_register(intelhaddata, AUD_CONFIG, 0);
1008 /* Reset buffer pointers */
1009 had_reset_audio(intelhaddata);
1011 wait_clear_underrun_bit(intelhaddata);
1013 if (!intelhaddata->connected)
1014 return; /* disconnected? - bail out */
1016 /* Report UNDERRUN error to above layers */
1017 substream = had_substream_get(intelhaddata);
1019 snd_pcm_stop_xrun(substream);
1020 had_substream_put(intelhaddata);
1025 * ALSA PCM open callback
1027 static int had_pcm_open(struct snd_pcm_substream *substream)
1029 struct snd_intelhad *intelhaddata;
1030 struct snd_pcm_runtime *runtime;
1033 intelhaddata = snd_pcm_substream_chip(substream);
1034 runtime = substream->runtime;
1036 pm_runtime_get_sync(intelhaddata->dev);
1038 if (!intelhaddata->connected) {
1039 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
1045 /* set the runtime hw parameter with local snd_pcm_hardware struct */
1046 runtime->hw = had_pcm_hardware;
1048 retval = snd_pcm_hw_constraint_integer(runtime,
1049 SNDRV_PCM_HW_PARAM_PERIODS);
1053 /* Make sure, that the period size is always aligned
1056 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1057 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
1061 /* expose PCM substream */
1062 spin_lock_irq(&intelhaddata->had_spinlock);
1063 intelhaddata->stream_info.substream = substream;
1064 intelhaddata->stream_info.substream_refcount++;
1065 spin_unlock_irq(&intelhaddata->had_spinlock);
1069 pm_runtime_put(intelhaddata->dev);
1074 * ALSA PCM close callback
1076 static int had_pcm_close(struct snd_pcm_substream *substream)
1078 struct snd_intelhad *intelhaddata;
1080 intelhaddata = snd_pcm_substream_chip(substream);
1082 /* unreference and sync with the pending PCM accesses */
1083 spin_lock_irq(&intelhaddata->had_spinlock);
1084 intelhaddata->stream_info.substream = NULL;
1085 intelhaddata->stream_info.substream_refcount--;
1086 while (intelhaddata->stream_info.substream_refcount > 0) {
1087 spin_unlock_irq(&intelhaddata->had_spinlock);
1089 spin_lock_irq(&intelhaddata->had_spinlock);
1091 spin_unlock_irq(&intelhaddata->had_spinlock);
1093 pm_runtime_put(intelhaddata->dev);
1098 * ALSA PCM hw_params callback
1100 static int had_pcm_hw_params(struct snd_pcm_substream *substream,
1101 struct snd_pcm_hw_params *hw_params)
1103 struct snd_intelhad *intelhaddata;
1105 int pages, buf_size, retval;
1107 intelhaddata = snd_pcm_substream_chip(substream);
1108 buf_size = params_buffer_bytes(hw_params);
1109 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1112 dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
1113 __func__, buf_size);
1114 /* mark the pages as uncached region */
1115 addr = (unsigned long) substream->runtime->dma_area;
1116 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1117 retval = set_memory_uc(addr, pages);
1119 dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
1123 memset(substream->runtime->dma_area, 0, buf_size);
1129 * ALSA PCM hw_free callback
1131 static int had_pcm_hw_free(struct snd_pcm_substream *substream)
1136 /* mark back the pages as cached/writeback region before the free */
1137 if (substream->runtime->dma_area != NULL) {
1138 addr = (unsigned long) substream->runtime->dma_area;
1139 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1141 set_memory_wb(addr, pages);
1142 return snd_pcm_lib_free_pages(substream);
1148 * ALSA PCM trigger callback
1150 static int had_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1153 struct snd_intelhad *intelhaddata;
1155 intelhaddata = snd_pcm_substream_chip(substream);
1158 case SNDRV_PCM_TRIGGER_START:
1159 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1160 case SNDRV_PCM_TRIGGER_RESUME:
1161 /* Disable local INTRs till register prgmng is done */
1162 if (!intelhaddata->connected) {
1163 dev_dbg(intelhaddata->dev,
1164 "_START: HDMI cable plugged-out\n");
1169 intelhaddata->stream_info.running = true;
1172 had_enable_audio_int(intelhaddata, true);
1173 had_enable_audio(substream, intelhaddata, true);
1176 case SNDRV_PCM_TRIGGER_STOP:
1177 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1178 case SNDRV_PCM_TRIGGER_SUSPEND:
1179 spin_lock(&intelhaddata->had_spinlock);
1181 /* Stop reporting BUFFER_DONE/UNDERRUN to above layers */
1183 intelhaddata->stream_info.running = false;
1184 spin_unlock(&intelhaddata->had_spinlock);
1186 had_enable_audio_int(intelhaddata, false);
1187 had_enable_audio(substream, intelhaddata, false);
1188 /* Reset buffer pointers */
1189 had_reset_audio(intelhaddata);
1190 had_enable_audio_int(intelhaddata, false);
1200 * ALSA PCM prepare callback
1202 static int had_pcm_prepare(struct snd_pcm_substream *substream)
1205 u32 disp_samp_freq, n_param;
1207 struct snd_intelhad *intelhaddata;
1208 struct snd_pcm_runtime *runtime;
1210 intelhaddata = snd_pcm_substream_chip(substream);
1211 runtime = substream->runtime;
1213 if (!intelhaddata->connected) {
1214 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
1220 dev_dbg(intelhaddata->dev, "period_size=%d\n",
1221 (int)frames_to_bytes(runtime, runtime->period_size));
1222 dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods);
1223 dev_dbg(intelhaddata->dev, "buffer_size=%d\n",
1224 (int)snd_pcm_lib_buffer_bytes(substream));
1225 dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
1226 dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
1228 /* Get N value in KHz */
1229 disp_samp_freq = intelhaddata->tmds_clock_speed;
1231 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
1233 dev_err(intelhaddata->dev,
1234 "programming N value failed %#x\n", retval);
1238 if (intelhaddata->dp_output)
1239 link_rate = intelhaddata->link_rate;
1241 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1242 n_param, intelhaddata);
1244 had_prog_dip(substream, intelhaddata);
1246 retval = had_init_audio_ctrl(substream, intelhaddata);
1248 /* Prog buffer address */
1249 had_init_ringbuf(substream, intelhaddata);
1252 * Program channel mapping in following order:
1253 * FL, FR, C, LFE, RL, RR
1256 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
1263 * ALSA PCM pointer callback
1265 static snd_pcm_uframes_t had_pcm_pointer(struct snd_pcm_substream *substream)
1267 struct snd_intelhad *intelhaddata;
1270 intelhaddata = snd_pcm_substream_chip(substream);
1272 if (!intelhaddata->connected)
1273 return SNDRV_PCM_POS_XRUN;
1275 len = had_process_ringbuf(substream, intelhaddata);
1277 return SNDRV_PCM_POS_XRUN;
1278 return bytes_to_frames(substream->runtime, len);
1282 * ALSA PCM mmap callback
1284 static int had_pcm_mmap(struct snd_pcm_substream *substream,
1285 struct vm_area_struct *vma)
1287 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1288 return remap_pfn_range(vma, vma->vm_start,
1289 substream->dma_buffer.addr >> PAGE_SHIFT,
1290 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1296 static const struct snd_pcm_ops had_pcm_ops = {
1297 .open = had_pcm_open,
1298 .close = had_pcm_close,
1299 .ioctl = snd_pcm_lib_ioctl,
1300 .hw_params = had_pcm_hw_params,
1301 .hw_free = had_pcm_hw_free,
1302 .prepare = had_pcm_prepare,
1303 .trigger = had_pcm_trigger,
1304 .pointer = had_pcm_pointer,
1305 .mmap = had_pcm_mmap,
1308 /* process mode change of the running stream; called in mutex */
1309 static int had_process_mode_change(struct snd_intelhad *intelhaddata)
1311 struct snd_pcm_substream *substream;
1313 u32 disp_samp_freq, n_param;
1316 substream = had_substream_get(intelhaddata);
1321 had_enable_audio(substream, intelhaddata, false);
1323 /* Update CTS value */
1324 disp_samp_freq = intelhaddata->tmds_clock_speed;
1326 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
1328 dev_err(intelhaddata->dev,
1329 "programming N value failed %#x\n", retval);
1333 if (intelhaddata->dp_output)
1334 link_rate = intelhaddata->link_rate;
1336 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1337 n_param, intelhaddata);
1340 had_enable_audio(substream, intelhaddata, true);
1343 had_substream_put(intelhaddata);
1347 /* process hot plug, called from wq with mutex locked */
1348 static void had_process_hot_plug(struct snd_intelhad *intelhaddata)
1350 struct snd_pcm_substream *substream;
1352 spin_lock_irq(&intelhaddata->had_spinlock);
1353 if (intelhaddata->connected) {
1354 dev_dbg(intelhaddata->dev, "Device already connected\n");
1355 spin_unlock_irq(&intelhaddata->had_spinlock);
1359 intelhaddata->connected = true;
1360 dev_dbg(intelhaddata->dev,
1361 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
1362 __func__, __LINE__);
1363 spin_unlock_irq(&intelhaddata->had_spinlock);
1366 substream = had_substream_get(intelhaddata);
1368 dev_dbg(intelhaddata->dev,
1369 "Force to stop the active stream by disconnection\n");
1370 /* Set runtime->state to hw_params done */
1371 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1372 had_substream_put(intelhaddata);
1375 had_build_channel_allocation_map(intelhaddata);
1378 /* process hot unplug, called from wq with mutex locked */
1379 static void had_process_hot_unplug(struct snd_intelhad *intelhaddata)
1381 struct snd_pcm_substream *substream;
1383 substream = had_substream_get(intelhaddata);
1385 spin_lock_irq(&intelhaddata->had_spinlock);
1387 if (!intelhaddata->connected) {
1388 dev_dbg(intelhaddata->dev, "Device already disconnected\n");
1389 spin_unlock_irq(&intelhaddata->had_spinlock);
1395 had_enable_audio_int(intelhaddata, false);
1396 had_enable_audio(substream, intelhaddata, false);
1398 intelhaddata->connected = false;
1399 dev_dbg(intelhaddata->dev,
1400 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
1401 __func__, __LINE__);
1402 spin_unlock_irq(&intelhaddata->had_spinlock);
1404 /* Report to above ALSA layer */
1406 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1410 had_substream_put(intelhaddata);
1411 kfree(intelhaddata->chmap->chmap);
1412 intelhaddata->chmap->chmap = NULL;
1416 * ALSA iec958 and ELD controls
1419 static int had_iec958_info(struct snd_kcontrol *kcontrol,
1420 struct snd_ctl_elem_info *uinfo)
1422 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1427 static int had_iec958_get(struct snd_kcontrol *kcontrol,
1428 struct snd_ctl_elem_value *ucontrol)
1430 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1432 mutex_lock(&intelhaddata->mutex);
1433 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1434 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1435 ucontrol->value.iec958.status[2] =
1436 (intelhaddata->aes_bits >> 16) & 0xff;
1437 ucontrol->value.iec958.status[3] =
1438 (intelhaddata->aes_bits >> 24) & 0xff;
1439 mutex_unlock(&intelhaddata->mutex);
1443 static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1444 struct snd_ctl_elem_value *ucontrol)
1446 ucontrol->value.iec958.status[0] = 0xff;
1447 ucontrol->value.iec958.status[1] = 0xff;
1448 ucontrol->value.iec958.status[2] = 0xff;
1449 ucontrol->value.iec958.status[3] = 0xff;
1453 static int had_iec958_put(struct snd_kcontrol *kcontrol,
1454 struct snd_ctl_elem_value *ucontrol)
1457 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1460 val = (ucontrol->value.iec958.status[0] << 0) |
1461 (ucontrol->value.iec958.status[1] << 8) |
1462 (ucontrol->value.iec958.status[2] << 16) |
1463 (ucontrol->value.iec958.status[3] << 24);
1464 mutex_lock(&intelhaddata->mutex);
1465 if (intelhaddata->aes_bits != val) {
1466 intelhaddata->aes_bits = val;
1469 mutex_unlock(&intelhaddata->mutex);
1473 static int had_ctl_eld_info(struct snd_kcontrol *kcontrol,
1474 struct snd_ctl_elem_info *uinfo)
1476 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1477 uinfo->count = HDMI_MAX_ELD_BYTES;
1481 static int had_ctl_eld_get(struct snd_kcontrol *kcontrol,
1482 struct snd_ctl_elem_value *ucontrol)
1484 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1486 mutex_lock(&intelhaddata->mutex);
1487 memcpy(ucontrol->value.bytes.data, intelhaddata->eld,
1488 HDMI_MAX_ELD_BYTES);
1489 mutex_unlock(&intelhaddata->mutex);
1493 static const struct snd_kcontrol_new had_controls[] = {
1495 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1496 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1497 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1498 .info = had_iec958_info, /* shared */
1499 .get = had_iec958_mask_get,
1502 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1503 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1504 .info = had_iec958_info,
1505 .get = had_iec958_get,
1506 .put = had_iec958_put,
1509 .access = (SNDRV_CTL_ELEM_ACCESS_READ |
1510 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1511 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1513 .info = had_ctl_eld_info,
1514 .get = had_ctl_eld_get,
1519 * audio interrupt handler
1521 static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id)
1523 struct snd_intelhad *ctx = dev_id;
1524 u32 audio_stat, audio_reg;
1526 audio_reg = AUD_HDMI_STATUS;
1527 had_read_register(ctx, audio_reg, &audio_stat);
1529 if (audio_stat & HDMI_AUDIO_UNDERRUN) {
1530 had_write_register(ctx, audio_reg, HDMI_AUDIO_UNDERRUN);
1531 had_process_buffer_underrun(ctx);
1534 if (audio_stat & HDMI_AUDIO_BUFFER_DONE) {
1535 had_write_register(ctx, audio_reg, HDMI_AUDIO_BUFFER_DONE);
1536 had_process_buffer_done(ctx);
1543 * monitor plug/unplug notification from i915; just kick off the work
1545 static void notify_audio_lpe(struct platform_device *pdev)
1547 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
1549 schedule_work(&ctx->hdmi_audio_wq);
1552 /* the work to handle monitor hot plug/unplug */
1553 static void had_audio_wq(struct work_struct *work)
1555 struct snd_intelhad *ctx =
1556 container_of(work, struct snd_intelhad, hdmi_audio_wq);
1557 struct intel_hdmi_lpe_audio_pdata *pdata = ctx->dev->platform_data;
1559 pm_runtime_get_sync(ctx->dev);
1560 mutex_lock(&ctx->mutex);
1561 if (!pdata->hdmi_connected) {
1562 dev_dbg(ctx->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG\n",
1564 memset(ctx->eld, 0, sizeof(ctx->eld)); /* clear the old ELD */
1565 had_process_hot_unplug(ctx);
1567 struct intel_hdmi_lpe_audio_eld *eld = &pdata->eld;
1569 dev_dbg(ctx->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
1570 __func__, eld->port_id, pdata->tmds_clock_speed);
1572 switch (eld->pipe_id) {
1574 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1577 ctx->had_config_offset = AUDIO_HDMI_CONFIG_B;
1580 ctx->had_config_offset = AUDIO_HDMI_CONFIG_C;
1583 dev_dbg(ctx->dev, "Invalid pipe %d\n",
1588 memcpy(ctx->eld, eld->eld_data, sizeof(ctx->eld));
1590 ctx->dp_output = pdata->dp_output;
1591 ctx->tmds_clock_speed = pdata->tmds_clock_speed;
1592 ctx->link_rate = pdata->link_rate;
1594 had_process_hot_plug(ctx);
1596 /* Process mode change if stream is active */
1597 had_process_mode_change(ctx);
1599 mutex_unlock(&ctx->mutex);
1600 pm_runtime_put(ctx->dev);
1607 static int hdmi_lpe_audio_runtime_suspend(struct device *dev)
1609 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1610 struct snd_pcm_substream *substream;
1612 substream = had_substream_get(ctx);
1614 snd_pcm_suspend(substream);
1615 had_substream_put(ctx);
1621 static int hdmi_lpe_audio_suspend(struct device *dev)
1623 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1626 err = hdmi_lpe_audio_runtime_suspend(dev);
1628 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D3hot);
1632 static int hdmi_lpe_audio_resume(struct device *dev)
1634 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1636 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D0);
1640 /* release resources */
1641 static void hdmi_lpe_audio_free(struct snd_card *card)
1643 struct snd_intelhad *ctx = card->private_data;
1645 cancel_work_sync(&ctx->hdmi_audio_wq);
1647 if (ctx->mmio_start)
1648 iounmap(ctx->mmio_start);
1650 free_irq(ctx->irq, ctx);
1654 * hdmi_lpe_audio_probe - start bridge with i915
1656 * This function is called when the i915 driver creates the
1657 * hdmi-lpe-audio platform device.
1659 static int hdmi_lpe_audio_probe(struct platform_device *pdev)
1661 struct snd_card *card;
1662 struct snd_intelhad *ctx;
1663 struct snd_pcm *pcm;
1664 struct intel_hdmi_lpe_audio_pdata *pdata;
1666 struct resource *res_mmio;
1669 pdata = pdev->dev.platform_data;
1671 dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
1676 irq = platform_get_irq(pdev, 0);
1678 dev_err(&pdev->dev, "Could not get irq resource\n");
1682 res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1684 dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
1688 /* create a card instance with ALSA framework */
1689 ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
1690 THIS_MODULE, sizeof(*ctx), &card);
1694 ctx = card->private_data;
1695 spin_lock_init(&ctx->had_spinlock);
1696 mutex_init(&ctx->mutex);
1697 ctx->connected = false;
1698 ctx->dev = &pdev->dev;
1700 ctx->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
1701 strcpy(card->driver, INTEL_HAD);
1702 strcpy(card->shortname, INTEL_HAD);
1705 ctx->tmds_clock_speed = DIS_SAMPLE_RATE_148_5;
1706 INIT_WORK(&ctx->hdmi_audio_wq, had_audio_wq);
1708 card->private_free = hdmi_lpe_audio_free;
1710 /* assume pipe A as default */
1711 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1713 platform_set_drvdata(pdev, ctx);
1715 dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
1716 __func__, (unsigned int)res_mmio->start,
1717 (unsigned int)res_mmio->end);
1719 ctx->mmio_start = ioremap_nocache(res_mmio->start,
1720 (size_t)(resource_size(res_mmio)));
1721 if (!ctx->mmio_start) {
1722 dev_err(&pdev->dev, "Could not get ioremap\n");
1727 /* setup interrupt handler */
1728 ret = request_irq(irq, display_pipe_interrupt_handler, 0,
1731 dev_err(&pdev->dev, "request_irq failed\n");
1737 ret = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
1738 MAX_CAP_STREAMS, &pcm);
1742 /* setup private data which can be retrieved when required */
1743 pcm->private_data = ctx;
1744 pcm->info_flags = 0;
1745 strncpy(pcm->name, card->shortname, strlen(card->shortname));
1746 /* setup the ops for playabck */
1747 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &had_pcm_ops);
1749 /* only 32bit addressable */
1750 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
1751 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1753 /* allocate dma pages;
1754 * try to allocate 600k buffer as default which is large enough
1756 snd_pcm_lib_preallocate_pages_for_all(pcm,
1757 SNDRV_DMA_TYPE_DEV, NULL,
1758 HAD_DEFAULT_BUFFER, HAD_MAX_BUFFER);
1760 /* create controls */
1761 for (i = 0; i < ARRAY_SIZE(had_controls); i++) {
1762 ret = snd_ctl_add(card, snd_ctl_new1(&had_controls[i], ctx));
1767 init_channel_allocations();
1769 /* Register channel map controls */
1770 ret = had_register_chmap_ctls(ctx, pcm);
1774 ret = snd_card_register(card);
1778 spin_lock_irq(&pdata->lpe_audio_slock);
1779 pdata->notify_audio_lpe = notify_audio_lpe;
1780 pdata->notify_pending = false;
1781 spin_unlock_irq(&pdata->lpe_audio_slock);
1783 pm_runtime_set_active(&pdev->dev);
1784 pm_runtime_enable(&pdev->dev);
1786 dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
1787 schedule_work(&ctx->hdmi_audio_wq);
1792 snd_card_free(card);
1797 * hdmi_lpe_audio_remove - stop bridge with i915
1799 * This function is called when the platform device is destroyed.
1801 static int hdmi_lpe_audio_remove(struct platform_device *pdev)
1803 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
1806 had_enable_audio_int(ctx, false);
1807 snd_card_free(ctx->card);
1811 static const struct dev_pm_ops hdmi_lpe_audio_pm = {
1812 SET_SYSTEM_SLEEP_PM_OPS(hdmi_lpe_audio_suspend, hdmi_lpe_audio_resume)
1813 SET_RUNTIME_PM_OPS(hdmi_lpe_audio_runtime_suspend, NULL, NULL)
1816 static struct platform_driver hdmi_lpe_audio_driver = {
1818 .name = "hdmi-lpe-audio",
1819 .pm = &hdmi_lpe_audio_pm,
1821 .probe = hdmi_lpe_audio_probe,
1822 .remove = hdmi_lpe_audio_remove,
1825 module_platform_driver(hdmi_lpe_audio_driver);
1826 MODULE_ALIAS("platform:hdmi_lpe_audio");
1828 MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1829 MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1830 MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1831 MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1832 MODULE_DESCRIPTION("Intel HDMI Audio driver");
1833 MODULE_LICENSE("GPL v2");
1834 MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");