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 <linux/delay.h>
33 #include <asm/cacheflush.h>
34 #include <sound/core.h>
35 #include <sound/asoundef.h>
36 #include <sound/pcm.h>
37 #include <sound/pcm_params.h>
38 #include <sound/initval.h>
39 #include <sound/control.h>
40 #include <sound/jack.h>
41 #include <drm/drm_edid.h>
42 #include <drm/intel_lpe_audio.h>
43 #include "intel_hdmi_audio.h"
45 #define for_each_port(card_ctx, port) \
46 for ((port) = 0; (port) < (card_ctx)->num_ports; (port)++)
48 /*standard module options for ALSA. This module supports only one card*/
49 static int hdmi_card_index = SNDRV_DEFAULT_IDX1;
50 static char *hdmi_card_id = SNDRV_DEFAULT_STR1;
52 module_param_named(index, hdmi_card_index, int, 0444);
53 MODULE_PARM_DESC(index,
54 "Index value for INTEL Intel HDMI Audio controller.");
55 module_param_named(id, hdmi_card_id, charp, 0444);
57 "ID string for INTEL Intel HDMI Audio controller.");
60 * ELD SA bits in the CEA Speaker Allocation data block
62 static const int eld_speaker_allocation_bits[] = {
70 /* the following are not defined in ELD yet */
75 * This is an ordered list!
77 * The preceding ones have better chances to be selected by
78 * hdmi_channel_allocation().
80 static struct cea_channel_speaker_allocation channel_allocations[] = {
81 /* channel: 7 6 5 4 3 2 1 0 */
82 { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
84 { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
86 { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
88 { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
90 { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
92 { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
94 { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
96 { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
98 { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
100 { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
101 { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
102 { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
103 { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
104 { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
105 { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
106 { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
107 { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
108 { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
109 { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
110 { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
111 { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
112 { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
113 { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
114 { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
115 { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
116 { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
117 { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
118 { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
119 { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
120 { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
121 { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
122 { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
125 static const struct channel_map_table map_tables[] = {
126 { SNDRV_CHMAP_FL, 0x00, FL },
127 { SNDRV_CHMAP_FR, 0x01, FR },
128 { SNDRV_CHMAP_RL, 0x04, RL },
129 { SNDRV_CHMAP_RR, 0x05, RR },
130 { SNDRV_CHMAP_LFE, 0x02, LFE },
131 { SNDRV_CHMAP_FC, 0x03, FC },
132 { SNDRV_CHMAP_RLC, 0x06, RLC },
133 { SNDRV_CHMAP_RRC, 0x07, RRC },
137 /* hardware capability structure */
138 static const struct snd_pcm_hardware had_pcm_hardware = {
139 .info = (SNDRV_PCM_INFO_INTERLEAVED |
140 SNDRV_PCM_INFO_MMAP |
141 SNDRV_PCM_INFO_MMAP_VALID |
142 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
143 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
144 SNDRV_PCM_FMTBIT_S24_LE |
145 SNDRV_PCM_FMTBIT_S32_LE),
146 .rates = SNDRV_PCM_RATE_32000 |
147 SNDRV_PCM_RATE_44100 |
148 SNDRV_PCM_RATE_48000 |
149 SNDRV_PCM_RATE_88200 |
150 SNDRV_PCM_RATE_96000 |
151 SNDRV_PCM_RATE_176400 |
152 SNDRV_PCM_RATE_192000,
153 .rate_min = HAD_MIN_RATE,
154 .rate_max = HAD_MAX_RATE,
155 .channels_min = HAD_MIN_CHANNEL,
156 .channels_max = HAD_MAX_CHANNEL,
157 .buffer_bytes_max = HAD_MAX_BUFFER,
158 .period_bytes_min = HAD_MIN_PERIOD_BYTES,
159 .period_bytes_max = HAD_MAX_PERIOD_BYTES,
160 .periods_min = HAD_MIN_PERIODS,
161 .periods_max = HAD_MAX_PERIODS,
162 .fifo_size = HAD_FIFO_SIZE,
165 /* Get the active PCM substream;
166 * Call had_substream_put() for unreferecing.
167 * Don't call this inside had_spinlock, as it takes by itself
169 static struct snd_pcm_substream *
170 had_substream_get(struct snd_intelhad *intelhaddata)
172 struct snd_pcm_substream *substream;
175 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
176 substream = intelhaddata->stream_info.substream;
178 intelhaddata->stream_info.substream_refcount++;
179 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
183 /* Unref the active PCM substream;
184 * Don't call this inside had_spinlock, as it takes by itself
186 static void had_substream_put(struct snd_intelhad *intelhaddata)
190 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
191 intelhaddata->stream_info.substream_refcount--;
192 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
195 /* Register access functions */
196 static u32 had_read_register_raw(struct snd_intelhad *ctx, u32 reg)
198 return ioread32(ctx->card_ctx->mmio_start + ctx->had_config_offset + reg);
201 static void had_write_register_raw(struct snd_intelhad *ctx, u32 reg, u32 val)
203 iowrite32(val, ctx->card_ctx->mmio_start + ctx->had_config_offset + reg);
206 static void had_read_register(struct snd_intelhad *ctx, u32 reg, u32 *val)
211 *val = had_read_register_raw(ctx, reg);
214 static void had_write_register(struct snd_intelhad *ctx, u32 reg, u32 val)
217 had_write_register_raw(ctx, reg, val);
221 * enable / disable audio configuration
223 * The normal read/modify should not directly be used on VLV2 for
224 * updating AUD_CONFIG register.
226 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
227 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
228 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
229 * register. This field should be 1xy binary for configuration with 6 or
230 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
231 * causes the "channels" field to be updated as 0xy binary resulting in
232 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
233 * appropriate value when doing read-modify of AUD_CONFIG register.
235 static void had_enable_audio(struct snd_intelhad *intelhaddata,
238 /* update the cached value */
239 intelhaddata->aud_config.regx.aud_en = enable;
240 had_write_register(intelhaddata, AUD_CONFIG,
241 intelhaddata->aud_config.regval);
244 /* forcibly ACKs to both BUFFER_DONE and BUFFER_UNDERRUN interrupts */
245 static void had_ack_irqs(struct snd_intelhad *ctx)
251 had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
252 status_reg |= HDMI_AUDIO_BUFFER_DONE | HDMI_AUDIO_UNDERRUN;
253 had_write_register(ctx, AUD_HDMI_STATUS, status_reg);
254 had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
257 /* Reset buffer pointers */
258 static void had_reset_audio(struct snd_intelhad *intelhaddata)
260 had_write_register(intelhaddata, AUD_HDMI_STATUS,
261 AUD_HDMI_STATUSG_MASK_FUNCRST);
262 had_write_register(intelhaddata, AUD_HDMI_STATUS, 0);
266 * initialize audio channel status registers
267 * This function is called in the prepare callback
269 static int had_prog_status_reg(struct snd_pcm_substream *substream,
270 struct snd_intelhad *intelhaddata)
272 union aud_cfg cfg_val = {.regval = 0};
273 union aud_ch_status_0 ch_stat0 = {.regval = 0};
274 union aud_ch_status_1 ch_stat1 = {.regval = 0};
276 ch_stat0.regx.lpcm_id = (intelhaddata->aes_bits &
277 IEC958_AES0_NONAUDIO) >> 1;
278 ch_stat0.regx.clk_acc = (intelhaddata->aes_bits &
279 IEC958_AES3_CON_CLOCK) >> 4;
280 cfg_val.regx.val_bit = ch_stat0.regx.lpcm_id;
282 switch (substream->runtime->rate) {
283 case AUD_SAMPLE_RATE_32:
284 ch_stat0.regx.samp_freq = CH_STATUS_MAP_32KHZ;
287 case AUD_SAMPLE_RATE_44_1:
288 ch_stat0.regx.samp_freq = CH_STATUS_MAP_44KHZ;
290 case AUD_SAMPLE_RATE_48:
291 ch_stat0.regx.samp_freq = CH_STATUS_MAP_48KHZ;
293 case AUD_SAMPLE_RATE_88_2:
294 ch_stat0.regx.samp_freq = CH_STATUS_MAP_88KHZ;
296 case AUD_SAMPLE_RATE_96:
297 ch_stat0.regx.samp_freq = CH_STATUS_MAP_96KHZ;
299 case AUD_SAMPLE_RATE_176_4:
300 ch_stat0.regx.samp_freq = CH_STATUS_MAP_176KHZ;
302 case AUD_SAMPLE_RATE_192:
303 ch_stat0.regx.samp_freq = CH_STATUS_MAP_192KHZ;
307 /* control should never come here */
311 had_write_register(intelhaddata,
312 AUD_CH_STATUS_0, ch_stat0.regval);
314 switch (substream->runtime->format) {
315 case SNDRV_PCM_FORMAT_S16_LE:
316 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_20;
317 ch_stat1.regx.wrd_len = SMPL_WIDTH_16BITS;
319 case SNDRV_PCM_FORMAT_S24_LE:
320 case SNDRV_PCM_FORMAT_S32_LE:
321 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_24;
322 ch_stat1.regx.wrd_len = SMPL_WIDTH_24BITS;
328 had_write_register(intelhaddata,
329 AUD_CH_STATUS_1, ch_stat1.regval);
334 * function to initialize audio
335 * registers and buffer confgiuration registers
336 * This function is called in the prepare callback
338 static int had_init_audio_ctrl(struct snd_pcm_substream *substream,
339 struct snd_intelhad *intelhaddata)
341 union aud_cfg cfg_val = {.regval = 0};
342 union aud_buf_config buf_cfg = {.regval = 0};
345 had_prog_status_reg(substream, intelhaddata);
347 buf_cfg.regx.audio_fifo_watermark = FIFO_THRESHOLD;
348 buf_cfg.regx.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
349 buf_cfg.regx.aud_delay = 0;
350 had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.regval);
352 channels = substream->runtime->channels;
353 cfg_val.regx.num_ch = channels - 2;
355 cfg_val.regx.layout = LAYOUT0;
357 cfg_val.regx.layout = LAYOUT1;
359 if (substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE)
360 cfg_val.regx.packet_mode = 1;
362 if (substream->runtime->format == SNDRV_PCM_FORMAT_S32_LE)
363 cfg_val.regx.left_align = 1;
365 cfg_val.regx.val_bit = 1;
367 /* fix up the DP bits */
368 if (intelhaddata->dp_output) {
369 cfg_val.regx.dp_modei = 1;
370 cfg_val.regx.set = 1;
373 had_write_register(intelhaddata, AUD_CONFIG, cfg_val.regval);
374 intelhaddata->aud_config = cfg_val;
379 * Compute derived values in channel_allocations[].
381 static void init_channel_allocations(void)
384 struct cea_channel_speaker_allocation *p;
386 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
387 p = channel_allocations + i;
390 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
391 if (p->speakers[j]) {
393 p->spk_mask |= p->speakers[j];
399 * The transformation takes two steps:
401 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
402 * spk_mask => (channel_allocations[]) => ai->CA
404 * TODO: it could select the wrong CA from multiple candidates.
406 static int had_channel_allocation(struct snd_intelhad *intelhaddata,
414 * CA defaults to 0 for basic stereo audio
420 * expand ELD's speaker allocation mask
422 * ELD tells the speaker mask in a compact(paired) form,
423 * expand ELD's notions to match the ones used by Audio InfoFrame.
426 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
427 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
428 spk_mask |= eld_speaker_allocation_bits[i];
431 /* search for the first working match in the CA table */
432 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
433 if (channels == channel_allocations[i].channels &&
434 (spk_mask & channel_allocations[i].spk_mask) ==
435 channel_allocations[i].spk_mask) {
436 ca = channel_allocations[i].ca_index;
441 dev_dbg(intelhaddata->dev, "select CA 0x%x for %d\n", ca, channels);
446 /* from speaker bit mask to ALSA API channel position */
447 static int spk_to_chmap(int spk)
449 const struct channel_map_table *t = map_tables;
451 for (; t->map; t++) {
452 if (t->spk_mask == spk)
458 static void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
462 struct snd_pcm_chmap_elem *chmap;
463 u8 eld_high, eld_high_mask = 0xF0;
466 kfree(intelhaddata->chmap->chmap);
467 intelhaddata->chmap->chmap = NULL;
469 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
473 dev_dbg(intelhaddata->dev, "eld speaker = %x\n",
474 intelhaddata->eld[DRM_ELD_SPEAKER]);
476 /* WA: Fix the max channel supported to 8 */
479 * Sink may support more than 8 channels, if eld_high has more than
480 * one bit set. SOC supports max 8 channels.
481 * Refer eld_speaker_allocation_bits, for sink speaker allocation
484 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
485 eld_high = intelhaddata->eld[DRM_ELD_SPEAKER] & eld_high_mask;
486 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
487 /* eld_high & (eld_high-1): if more than 1 bit set */
488 /* 0x1F: 7 channels */
489 for (i = 1; i < 4; i++) {
490 high_msb = eld_high & (0x80 >> i);
492 intelhaddata->eld[DRM_ELD_SPEAKER] &=
499 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
500 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
501 spk_mask |= eld_speaker_allocation_bits[i];
504 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
505 if (spk_mask == channel_allocations[i].spk_mask) {
506 for (c = 0; c < channel_allocations[i].channels; c++) {
507 chmap->map[c] = spk_to_chmap(
508 channel_allocations[i].speakers[
509 (MAX_SPEAKERS - 1) - c]);
511 chmap->channels = channel_allocations[i].channels;
512 intelhaddata->chmap->chmap = chmap;
516 if (i >= ARRAY_SIZE(channel_allocations))
521 * ALSA API channel-map control callbacks
523 static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
524 struct snd_ctl_elem_info *uinfo)
526 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
527 uinfo->count = HAD_MAX_CHANNEL;
528 uinfo->value.integer.min = 0;
529 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
533 static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
534 struct snd_ctl_elem_value *ucontrol)
536 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
537 struct snd_intelhad *intelhaddata = info->private_data;
539 const struct snd_pcm_chmap_elem *chmap;
541 memset(ucontrol->value.integer.value, 0,
542 sizeof(long) * HAD_MAX_CHANNEL);
543 mutex_lock(&intelhaddata->mutex);
544 if (!intelhaddata->chmap->chmap) {
545 mutex_unlock(&intelhaddata->mutex);
549 chmap = intelhaddata->chmap->chmap;
550 for (i = 0; i < chmap->channels; i++)
551 ucontrol->value.integer.value[i] = chmap->map[i];
552 mutex_unlock(&intelhaddata->mutex);
557 static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
562 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
563 NULL, 0, (unsigned long)intelhaddata,
564 &intelhaddata->chmap);
568 intelhaddata->chmap->private_data = intelhaddata;
569 intelhaddata->chmap->kctl->info = had_chmap_ctl_info;
570 intelhaddata->chmap->kctl->get = had_chmap_ctl_get;
571 intelhaddata->chmap->chmap = NULL;
576 * Initialize Data Island Packets registers
577 * This function is called in the prepare callback
579 static void had_prog_dip(struct snd_pcm_substream *substream,
580 struct snd_intelhad *intelhaddata)
583 union aud_ctrl_st ctrl_state = {.regval = 0};
584 union aud_info_frame2 frame2 = {.regval = 0};
585 union aud_info_frame3 frame3 = {.regval = 0};
591 channels = substream->runtime->channels;
593 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
595 ca = had_channel_allocation(intelhaddata, channels);
596 if (intelhaddata->dp_output) {
597 info_frame = DP_INFO_FRAME_WORD1;
598 frame2.regval = (substream->runtime->channels - 1) | (ca << 24);
600 info_frame = HDMI_INFO_FRAME_WORD1;
601 frame2.regx.chnl_cnt = substream->runtime->channels - 1;
602 frame3.regx.chnl_alloc = ca;
604 /* Calculte the byte wide checksum for all valid DIP words */
605 for (i = 0; i < BYTES_PER_WORD; i++)
606 checksum += (info_frame >> (i * 8)) & 0xff;
607 for (i = 0; i < BYTES_PER_WORD; i++)
608 checksum += (frame2.regval >> (i * 8)) & 0xff;
609 for (i = 0; i < BYTES_PER_WORD; i++)
610 checksum += (frame3.regval >> (i * 8)) & 0xff;
612 frame2.regx.chksum = -(checksum);
615 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, info_frame);
616 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame2.regval);
617 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame3.regval);
619 /* program remaining DIP words with zero */
620 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
621 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, 0x0);
623 ctrl_state.regx.dip_freq = 1;
624 ctrl_state.regx.dip_en_sta = 1;
625 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
628 static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
632 /* Select maud according to DP 1.2 spec */
633 if (link_rate == DP_2_7_GHZ) {
634 switch (aud_samp_freq) {
635 case AUD_SAMPLE_RATE_32:
636 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
639 case AUD_SAMPLE_RATE_44_1:
640 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
643 case AUD_SAMPLE_RATE_48:
644 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
647 case AUD_SAMPLE_RATE_88_2:
648 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
651 case AUD_SAMPLE_RATE_96:
652 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
655 case AUD_SAMPLE_RATE_176_4:
656 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
660 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
667 } else if (link_rate == DP_1_62_GHZ) {
668 switch (aud_samp_freq) {
669 case AUD_SAMPLE_RATE_32:
670 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
673 case AUD_SAMPLE_RATE_44_1:
674 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
677 case AUD_SAMPLE_RATE_48:
678 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
681 case AUD_SAMPLE_RATE_88_2:
682 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
685 case AUD_SAMPLE_RATE_96:
686 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
689 case AUD_SAMPLE_RATE_176_4:
690 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
694 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
708 * Program HDMI audio CTS value
710 * @aud_samp_freq: sampling frequency of audio data
711 * @tmds: sampling frequency of the display data
712 * @link_rate: DP link rate
713 * @n_param: N value, depends on aud_samp_freq
714 * @intelhaddata: substream private data
716 * Program CTS register based on the audio and display sampling frequency
718 static void had_prog_cts(u32 aud_samp_freq, u32 tmds, u32 link_rate,
719 u32 n_param, struct snd_intelhad *intelhaddata)
722 u64 dividend, divisor;
724 if (intelhaddata->dp_output) {
725 /* Substitute cts_val with Maud according to DP 1.2 spec*/
726 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
728 /* Calculate CTS according to HDMI 1.3a spec*/
729 dividend = (u64)tmds * n_param*1000;
730 divisor = 128 * aud_samp_freq;
731 cts_val = div64_u64(dividend, divisor);
733 dev_dbg(intelhaddata->dev, "TMDS value=%d, N value=%d, CTS Value=%d\n",
734 tmds, n_param, cts_val);
735 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
738 static int had_calculate_n_value(u32 aud_samp_freq)
742 /* Select N according to HDMI 1.3a spec*/
743 switch (aud_samp_freq) {
744 case AUD_SAMPLE_RATE_32:
748 case AUD_SAMPLE_RATE_44_1:
752 case AUD_SAMPLE_RATE_48:
756 case AUD_SAMPLE_RATE_88_2:
760 case AUD_SAMPLE_RATE_96:
764 case AUD_SAMPLE_RATE_176_4:
780 * Program HDMI audio N value
782 * @aud_samp_freq: sampling frequency of audio data
783 * @n_param: N value, depends on aud_samp_freq
784 * @intelhaddata: substream private data
786 * This function is called in the prepare callback.
787 * It programs based on the audio and display sampling frequency
789 static int had_prog_n(u32 aud_samp_freq, u32 *n_param,
790 struct snd_intelhad *intelhaddata)
794 if (intelhaddata->dp_output) {
796 * According to DP specs, Maud and Naud values hold
797 * a relationship, which is stated as:
798 * Maud/Naud = 512 * fs / f_LS_Clk
799 * where, fs is the sampling frequency of the audio stream
800 * and Naud is 32768 for Async clock.
805 n_val = had_calculate_n_value(aud_samp_freq);
810 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
816 * PCM ring buffer handling
818 * The hardware provides a ring buffer with the fixed 4 buffer descriptors
819 * (BDs). The driver maps these 4 BDs onto the PCM ring buffer. The mapping
820 * moves at each period elapsed. The below illustrates how it works:
823 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
824 * BD | 0 | 1 | 2 | 3 |
826 * At time=1 (period elapsed)
827 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
828 * BD | 1 | 2 | 3 | 0 |
830 * At time=2 (second period elapsed)
831 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
832 * BD | 2 | 3 | 0 | 1 |
834 * The bd_head field points to the index of the BD to be read. It's also the
835 * position to be filled at next. The pcm_head and the pcm_filled fields
836 * point to the indices of the current position and of the next position to
837 * be filled, respectively. For PCM buffer there are both _head and _filled
838 * because they may be difference when nperiods > 4. For example, in the
839 * example above at t=1, bd_head=1 and pcm_head=1 while pcm_filled=5:
841 * pcm_head (=1) --v v-- pcm_filled (=5)
842 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
843 * BD | 1 | 2 | 3 | 0 |
844 * bd_head (=1) --^ ^-- next to fill (= bd_head)
846 * For nperiods < 4, the remaining BDs out of 4 are marked as invalid, so that
847 * the hardware skips those BDs in the loop.
849 * An exceptional setup is the case with nperiods=1. Since we have to update
850 * BDs after finishing one BD processing, we'd need at least two BDs, where
851 * both BDs point to the same content, the same address, the same size of the
855 #define AUD_BUF_ADDR(x) (AUD_BUF_A_ADDR + (x) * HAD_REG_WIDTH)
856 #define AUD_BUF_LEN(x) (AUD_BUF_A_LENGTH + (x) * HAD_REG_WIDTH)
858 /* Set up a buffer descriptor at the "filled" position */
859 static void had_prog_bd(struct snd_pcm_substream *substream,
860 struct snd_intelhad *intelhaddata)
862 int idx = intelhaddata->bd_head;
863 int ofs = intelhaddata->pcmbuf_filled * intelhaddata->period_bytes;
864 u32 addr = substream->runtime->dma_addr + ofs;
866 addr |= AUD_BUF_VALID;
867 if (!substream->runtime->no_period_wakeup)
868 addr |= AUD_BUF_INTR_EN;
869 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), addr);
870 had_write_register(intelhaddata, AUD_BUF_LEN(idx),
871 intelhaddata->period_bytes);
873 /* advance the indices to the next */
874 intelhaddata->bd_head++;
875 intelhaddata->bd_head %= intelhaddata->num_bds;
876 intelhaddata->pcmbuf_filled++;
877 intelhaddata->pcmbuf_filled %= substream->runtime->periods;
880 /* invalidate a buffer descriptor with the given index */
881 static void had_invalidate_bd(struct snd_intelhad *intelhaddata,
884 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), 0);
885 had_write_register(intelhaddata, AUD_BUF_LEN(idx), 0);
888 /* Initial programming of ring buffer */
889 static void had_init_ringbuf(struct snd_pcm_substream *substream,
890 struct snd_intelhad *intelhaddata)
892 struct snd_pcm_runtime *runtime = substream->runtime;
895 num_periods = runtime->periods;
896 intelhaddata->num_bds = min(num_periods, HAD_NUM_OF_RING_BUFS);
897 /* set the minimum 2 BDs for num_periods=1 */
898 intelhaddata->num_bds = max(intelhaddata->num_bds, 2U);
899 intelhaddata->period_bytes =
900 frames_to_bytes(runtime, runtime->period_size);
901 WARN_ON(intelhaddata->period_bytes & 0x3f);
903 intelhaddata->bd_head = 0;
904 intelhaddata->pcmbuf_head = 0;
905 intelhaddata->pcmbuf_filled = 0;
907 for (i = 0; i < HAD_NUM_OF_RING_BUFS; i++) {
908 if (i < intelhaddata->num_bds)
909 had_prog_bd(substream, intelhaddata);
910 else /* invalidate the rest */
911 had_invalidate_bd(intelhaddata, i);
914 intelhaddata->bd_head = 0; /* reset at head again before starting */
917 /* process a bd, advance to the next */
918 static void had_advance_ringbuf(struct snd_pcm_substream *substream,
919 struct snd_intelhad *intelhaddata)
921 int num_periods = substream->runtime->periods;
923 /* reprogram the next buffer */
924 had_prog_bd(substream, intelhaddata);
926 /* proceed to next */
927 intelhaddata->pcmbuf_head++;
928 intelhaddata->pcmbuf_head %= num_periods;
931 /* process the current BD(s);
932 * returns the current PCM buffer byte position, or -EPIPE for underrun.
934 static int had_process_ringbuf(struct snd_pcm_substream *substream,
935 struct snd_intelhad *intelhaddata)
941 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
943 /* get the remaining bytes on the buffer */
944 had_read_register(intelhaddata,
945 AUD_BUF_LEN(intelhaddata->bd_head),
947 if (len < 0 || len > intelhaddata->period_bytes) {
948 dev_dbg(intelhaddata->dev, "Invalid buf length %d\n",
954 if (len > 0) /* OK, this is the current buffer */
957 /* len=0 => already empty, check the next buffer */
958 if (++processed >= intelhaddata->num_bds) {
959 len = -EPIPE; /* all empty? - report underrun */
962 had_advance_ringbuf(substream, intelhaddata);
965 len = intelhaddata->period_bytes - len;
966 len += intelhaddata->period_bytes * intelhaddata->pcmbuf_head;
968 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
972 /* called from irq handler */
973 static void had_process_buffer_done(struct snd_intelhad *intelhaddata)
975 struct snd_pcm_substream *substream;
977 substream = had_substream_get(intelhaddata);
979 return; /* no stream? - bail out */
981 if (!intelhaddata->connected) {
982 snd_pcm_stop_xrun(substream);
983 goto out; /* disconnected? - bail out */
986 /* process or stop the stream */
987 if (had_process_ringbuf(substream, intelhaddata) < 0)
988 snd_pcm_stop_xrun(substream);
990 snd_pcm_period_elapsed(substream);
993 had_substream_put(intelhaddata);
997 * The interrupt status 'sticky' bits might not be cleared by
998 * setting '1' to that bit once...
1000 static void wait_clear_underrun_bit(struct snd_intelhad *intelhaddata)
1005 for (i = 0; i < 100; i++) {
1006 /* clear bit30, 31 AUD_HDMI_STATUS */
1007 had_read_register(intelhaddata, AUD_HDMI_STATUS, &val);
1008 if (!(val & AUD_HDMI_STATUS_MASK_UNDERRUN))
1012 had_write_register(intelhaddata, AUD_HDMI_STATUS, val);
1014 dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n");
1017 /* Perform some reset procedure but only when need_reset is set;
1018 * this is called from prepare or hw_free callbacks once after trigger STOP
1019 * or underrun has been processed in order to settle down the h/w state.
1021 static void had_do_reset(struct snd_intelhad *intelhaddata)
1023 if (!intelhaddata->need_reset || !intelhaddata->connected)
1026 /* Reset buffer pointers */
1027 had_reset_audio(intelhaddata);
1028 wait_clear_underrun_bit(intelhaddata);
1029 intelhaddata->need_reset = false;
1032 /* called from irq handler */
1033 static void had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
1035 struct snd_pcm_substream *substream;
1037 /* Report UNDERRUN error to above layers */
1038 substream = had_substream_get(intelhaddata);
1040 snd_pcm_stop_xrun(substream);
1041 had_substream_put(intelhaddata);
1043 intelhaddata->need_reset = true;
1047 * ALSA PCM open callback
1049 static int had_pcm_open(struct snd_pcm_substream *substream)
1051 struct snd_intelhad *intelhaddata;
1052 struct snd_pcm_runtime *runtime;
1055 intelhaddata = snd_pcm_substream_chip(substream);
1056 runtime = substream->runtime;
1058 pm_runtime_get_sync(intelhaddata->dev);
1060 /* set the runtime hw parameter with local snd_pcm_hardware struct */
1061 runtime->hw = had_pcm_hardware;
1063 retval = snd_pcm_hw_constraint_integer(runtime,
1064 SNDRV_PCM_HW_PARAM_PERIODS);
1068 /* Make sure, that the period size is always aligned
1071 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1072 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
1076 retval = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1080 /* expose PCM substream */
1081 spin_lock_irq(&intelhaddata->had_spinlock);
1082 intelhaddata->stream_info.substream = substream;
1083 intelhaddata->stream_info.substream_refcount++;
1084 spin_unlock_irq(&intelhaddata->had_spinlock);
1088 pm_runtime_mark_last_busy(intelhaddata->dev);
1089 pm_runtime_put_autosuspend(intelhaddata->dev);
1094 * ALSA PCM close callback
1096 static int had_pcm_close(struct snd_pcm_substream *substream)
1098 struct snd_intelhad *intelhaddata;
1100 intelhaddata = snd_pcm_substream_chip(substream);
1102 /* unreference and sync with the pending PCM accesses */
1103 spin_lock_irq(&intelhaddata->had_spinlock);
1104 intelhaddata->stream_info.substream = NULL;
1105 intelhaddata->stream_info.substream_refcount--;
1106 while (intelhaddata->stream_info.substream_refcount > 0) {
1107 spin_unlock_irq(&intelhaddata->had_spinlock);
1109 spin_lock_irq(&intelhaddata->had_spinlock);
1111 spin_unlock_irq(&intelhaddata->had_spinlock);
1113 pm_runtime_mark_last_busy(intelhaddata->dev);
1114 pm_runtime_put_autosuspend(intelhaddata->dev);
1119 * ALSA PCM hw_params callback
1121 static int had_pcm_hw_params(struct snd_pcm_substream *substream,
1122 struct snd_pcm_hw_params *hw_params)
1124 struct snd_intelhad *intelhaddata;
1126 int pages, buf_size, retval;
1128 intelhaddata = snd_pcm_substream_chip(substream);
1129 buf_size = params_buffer_bytes(hw_params);
1130 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1133 dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
1134 __func__, buf_size);
1135 /* mark the pages as uncached region */
1136 addr = (unsigned long) substream->runtime->dma_area;
1137 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1138 retval = set_memory_uc(addr, pages);
1140 dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
1144 memset(substream->runtime->dma_area, 0, buf_size);
1150 * ALSA PCM hw_free callback
1152 static int had_pcm_hw_free(struct snd_pcm_substream *substream)
1154 struct snd_intelhad *intelhaddata;
1158 intelhaddata = snd_pcm_substream_chip(substream);
1159 had_do_reset(intelhaddata);
1161 /* mark back the pages as cached/writeback region before the free */
1162 if (substream->runtime->dma_area != NULL) {
1163 addr = (unsigned long) substream->runtime->dma_area;
1164 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1166 set_memory_wb(addr, pages);
1167 return snd_pcm_lib_free_pages(substream);
1173 * ALSA PCM trigger callback
1175 static int had_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1178 struct snd_intelhad *intelhaddata;
1180 intelhaddata = snd_pcm_substream_chip(substream);
1182 spin_lock(&intelhaddata->had_spinlock);
1184 case SNDRV_PCM_TRIGGER_START:
1185 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1186 case SNDRV_PCM_TRIGGER_RESUME:
1188 had_ack_irqs(intelhaddata); /* FIXME: do we need this? */
1189 had_enable_audio(intelhaddata, true);
1192 case SNDRV_PCM_TRIGGER_STOP:
1193 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1195 had_enable_audio(intelhaddata, false);
1196 intelhaddata->need_reset = true;
1202 spin_unlock(&intelhaddata->had_spinlock);
1207 * ALSA PCM prepare callback
1209 static int had_pcm_prepare(struct snd_pcm_substream *substream)
1212 u32 disp_samp_freq, n_param;
1214 struct snd_intelhad *intelhaddata;
1215 struct snd_pcm_runtime *runtime;
1217 intelhaddata = snd_pcm_substream_chip(substream);
1218 runtime = substream->runtime;
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 had_do_reset(intelhaddata);
1230 /* Get N value in KHz */
1231 disp_samp_freq = intelhaddata->tmds_clock_speed;
1233 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
1235 dev_err(intelhaddata->dev,
1236 "programming N value failed %#x\n", retval);
1240 if (intelhaddata->dp_output)
1241 link_rate = intelhaddata->link_rate;
1243 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1244 n_param, intelhaddata);
1246 had_prog_dip(substream, intelhaddata);
1248 retval = had_init_audio_ctrl(substream, intelhaddata);
1250 /* Prog buffer address */
1251 had_init_ringbuf(substream, intelhaddata);
1254 * Program channel mapping in following order:
1255 * FL, FR, C, LFE, RL, RR
1258 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
1265 * ALSA PCM pointer callback
1267 static snd_pcm_uframes_t had_pcm_pointer(struct snd_pcm_substream *substream)
1269 struct snd_intelhad *intelhaddata;
1272 intelhaddata = snd_pcm_substream_chip(substream);
1274 if (!intelhaddata->connected)
1275 return SNDRV_PCM_POS_XRUN;
1277 len = had_process_ringbuf(substream, intelhaddata);
1279 return SNDRV_PCM_POS_XRUN;
1280 len = bytes_to_frames(substream->runtime, len);
1281 /* wrapping may happen when periods=1 */
1282 len %= substream->runtime->buffer_size;
1287 * ALSA PCM mmap callback
1289 static int had_pcm_mmap(struct snd_pcm_substream *substream,
1290 struct vm_area_struct *vma)
1292 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1293 return remap_pfn_range(vma, vma->vm_start,
1294 substream->dma_buffer.addr >> PAGE_SHIFT,
1295 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1301 static const struct snd_pcm_ops had_pcm_ops = {
1302 .open = had_pcm_open,
1303 .close = had_pcm_close,
1304 .ioctl = snd_pcm_lib_ioctl,
1305 .hw_params = had_pcm_hw_params,
1306 .hw_free = had_pcm_hw_free,
1307 .prepare = had_pcm_prepare,
1308 .trigger = had_pcm_trigger,
1309 .pointer = had_pcm_pointer,
1310 .mmap = had_pcm_mmap,
1313 /* process mode change of the running stream; called in mutex */
1314 static int had_process_mode_change(struct snd_intelhad *intelhaddata)
1316 struct snd_pcm_substream *substream;
1318 u32 disp_samp_freq, n_param;
1321 substream = had_substream_get(intelhaddata);
1326 had_enable_audio(intelhaddata, false);
1328 /* Update CTS value */
1329 disp_samp_freq = intelhaddata->tmds_clock_speed;
1331 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
1333 dev_err(intelhaddata->dev,
1334 "programming N value failed %#x\n", retval);
1338 if (intelhaddata->dp_output)
1339 link_rate = intelhaddata->link_rate;
1341 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1342 n_param, intelhaddata);
1345 had_enable_audio(intelhaddata, true);
1348 had_substream_put(intelhaddata);
1352 /* process hot plug, called from wq with mutex locked */
1353 static void had_process_hot_plug(struct snd_intelhad *intelhaddata)
1355 struct snd_pcm_substream *substream;
1357 spin_lock_irq(&intelhaddata->had_spinlock);
1358 if (intelhaddata->connected) {
1359 dev_dbg(intelhaddata->dev, "Device already connected\n");
1360 spin_unlock_irq(&intelhaddata->had_spinlock);
1364 intelhaddata->connected = true;
1365 dev_dbg(intelhaddata->dev,
1366 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
1367 __func__, __LINE__);
1368 spin_unlock_irq(&intelhaddata->had_spinlock);
1370 had_build_channel_allocation_map(intelhaddata);
1372 /* Report to above ALSA layer */
1373 substream = had_substream_get(intelhaddata);
1375 snd_pcm_stop_xrun(substream);
1376 had_substream_put(intelhaddata);
1379 snd_jack_report(intelhaddata->jack, SND_JACK_AVOUT);
1382 /* process hot unplug, called from wq with mutex locked */
1383 static void had_process_hot_unplug(struct snd_intelhad *intelhaddata)
1385 struct snd_pcm_substream *substream;
1387 spin_lock_irq(&intelhaddata->had_spinlock);
1388 if (!intelhaddata->connected) {
1389 dev_dbg(intelhaddata->dev, "Device already disconnected\n");
1390 spin_unlock_irq(&intelhaddata->had_spinlock);
1396 had_enable_audio(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 kfree(intelhaddata->chmap->chmap);
1405 intelhaddata->chmap->chmap = NULL;
1407 /* Report to above ALSA layer */
1408 substream = had_substream_get(intelhaddata);
1410 snd_pcm_stop_xrun(substream);
1411 had_substream_put(intelhaddata);
1414 snd_jack_report(intelhaddata->jack, 0);
1418 * ALSA iec958 and ELD controls
1421 static int had_iec958_info(struct snd_kcontrol *kcontrol,
1422 struct snd_ctl_elem_info *uinfo)
1424 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1429 static int had_iec958_get(struct snd_kcontrol *kcontrol,
1430 struct snd_ctl_elem_value *ucontrol)
1432 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1434 mutex_lock(&intelhaddata->mutex);
1435 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1436 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1437 ucontrol->value.iec958.status[2] =
1438 (intelhaddata->aes_bits >> 16) & 0xff;
1439 ucontrol->value.iec958.status[3] =
1440 (intelhaddata->aes_bits >> 24) & 0xff;
1441 mutex_unlock(&intelhaddata->mutex);
1445 static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1446 struct snd_ctl_elem_value *ucontrol)
1448 ucontrol->value.iec958.status[0] = 0xff;
1449 ucontrol->value.iec958.status[1] = 0xff;
1450 ucontrol->value.iec958.status[2] = 0xff;
1451 ucontrol->value.iec958.status[3] = 0xff;
1455 static int had_iec958_put(struct snd_kcontrol *kcontrol,
1456 struct snd_ctl_elem_value *ucontrol)
1459 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1462 val = (ucontrol->value.iec958.status[0] << 0) |
1463 (ucontrol->value.iec958.status[1] << 8) |
1464 (ucontrol->value.iec958.status[2] << 16) |
1465 (ucontrol->value.iec958.status[3] << 24);
1466 mutex_lock(&intelhaddata->mutex);
1467 if (intelhaddata->aes_bits != val) {
1468 intelhaddata->aes_bits = val;
1471 mutex_unlock(&intelhaddata->mutex);
1475 static int had_ctl_eld_info(struct snd_kcontrol *kcontrol,
1476 struct snd_ctl_elem_info *uinfo)
1478 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1479 uinfo->count = HDMI_MAX_ELD_BYTES;
1483 static int had_ctl_eld_get(struct snd_kcontrol *kcontrol,
1484 struct snd_ctl_elem_value *ucontrol)
1486 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1488 mutex_lock(&intelhaddata->mutex);
1489 memcpy(ucontrol->value.bytes.data, intelhaddata->eld,
1490 HDMI_MAX_ELD_BYTES);
1491 mutex_unlock(&intelhaddata->mutex);
1495 static const struct snd_kcontrol_new had_controls[] = {
1497 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1498 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1499 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1500 .info = had_iec958_info, /* shared */
1501 .get = had_iec958_mask_get,
1504 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1505 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1506 .info = had_iec958_info,
1507 .get = had_iec958_get,
1508 .put = had_iec958_put,
1511 .access = (SNDRV_CTL_ELEM_ACCESS_READ |
1512 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1513 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1515 .info = had_ctl_eld_info,
1516 .get = had_ctl_eld_get,
1521 * audio interrupt handler
1523 static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id)
1525 struct snd_intelhad_card *card_ctx = dev_id;
1528 for_each_port(card_ctx, port) {
1529 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
1532 /* use raw register access to ack IRQs even while disconnected */
1533 audio_stat = had_read_register_raw(ctx, AUD_HDMI_STATUS);
1535 if (audio_stat & HDMI_AUDIO_UNDERRUN) {
1536 had_write_register_raw(ctx, AUD_HDMI_STATUS,
1537 HDMI_AUDIO_UNDERRUN);
1538 had_process_buffer_underrun(ctx);
1541 if (audio_stat & HDMI_AUDIO_BUFFER_DONE) {
1542 had_write_register_raw(ctx, AUD_HDMI_STATUS,
1543 HDMI_AUDIO_BUFFER_DONE);
1544 had_process_buffer_done(ctx);
1552 * monitor plug/unplug notification from i915; just kick off the work
1554 static void notify_audio_lpe(struct platform_device *pdev)
1556 struct snd_intelhad_card *card_ctx = platform_get_drvdata(pdev);
1559 for_each_port(card_ctx, port) {
1560 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
1562 schedule_work(&ctx->hdmi_audio_wq);
1566 /* the work to handle monitor hot plug/unplug */
1567 static void had_audio_wq(struct work_struct *work)
1569 struct snd_intelhad *ctx =
1570 container_of(work, struct snd_intelhad, hdmi_audio_wq);
1571 struct intel_hdmi_lpe_audio_pdata *pdata = ctx->dev->platform_data;
1572 struct intel_hdmi_lpe_audio_port_pdata *ppdata = &pdata->port;
1574 pm_runtime_get_sync(ctx->dev);
1575 mutex_lock(&ctx->mutex);
1576 if (ppdata->pipe < 0) {
1577 dev_dbg(ctx->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG\n",
1579 memset(ctx->eld, 0, sizeof(ctx->eld)); /* clear the old ELD */
1580 had_process_hot_unplug(ctx);
1582 dev_dbg(ctx->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
1583 __func__, ppdata->port, ppdata->ls_clock);
1585 switch (ppdata->pipe) {
1587 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1590 ctx->had_config_offset = AUDIO_HDMI_CONFIG_B;
1593 ctx->had_config_offset = AUDIO_HDMI_CONFIG_C;
1596 dev_dbg(ctx->dev, "Invalid pipe %d\n",
1601 memcpy(ctx->eld, ppdata->eld, sizeof(ctx->eld));
1603 ctx->dp_output = ppdata->dp_output;
1604 if (ctx->dp_output) {
1605 ctx->tmds_clock_speed = 0;
1606 ctx->link_rate = ppdata->ls_clock;
1608 ctx->tmds_clock_speed = ppdata->ls_clock;
1612 had_process_hot_plug(ctx);
1614 /* Process mode change if stream is active */
1615 had_process_mode_change(ctx);
1617 mutex_unlock(&ctx->mutex);
1618 pm_runtime_mark_last_busy(ctx->dev);
1619 pm_runtime_put_autosuspend(ctx->dev);
1625 static int had_create_jack(struct snd_intelhad *ctx,
1626 struct snd_pcm *pcm)
1631 snprintf(hdmi_str, sizeof(hdmi_str),
1632 "HDMI/DP,pcm=%d", pcm->device);
1634 err = snd_jack_new(ctx->card_ctx->card, hdmi_str,
1635 SND_JACK_AVOUT, &ctx->jack,
1639 ctx->jack->private_data = ctx;
1647 static int hdmi_lpe_audio_runtime_suspend(struct device *dev)
1649 struct snd_intelhad_card *card_ctx = dev_get_drvdata(dev);
1652 for_each_port(card_ctx, port) {
1653 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
1654 struct snd_pcm_substream *substream;
1656 substream = had_substream_get(ctx);
1658 snd_pcm_suspend(substream);
1659 had_substream_put(ctx);
1666 static int __maybe_unused hdmi_lpe_audio_suspend(struct device *dev)
1668 struct snd_intelhad_card *card_ctx = dev_get_drvdata(dev);
1671 err = hdmi_lpe_audio_runtime_suspend(dev);
1673 snd_power_change_state(card_ctx->card, SNDRV_CTL_POWER_D3hot);
1677 static int hdmi_lpe_audio_runtime_resume(struct device *dev)
1679 pm_runtime_mark_last_busy(dev);
1683 static int __maybe_unused hdmi_lpe_audio_resume(struct device *dev)
1685 struct snd_intelhad_card *card_ctx = dev_get_drvdata(dev);
1687 hdmi_lpe_audio_runtime_resume(dev);
1688 snd_power_change_state(card_ctx->card, SNDRV_CTL_POWER_D0);
1692 /* release resources */
1693 static void hdmi_lpe_audio_free(struct snd_card *card)
1695 struct snd_intelhad_card *card_ctx = card->private_data;
1696 struct intel_hdmi_lpe_audio_pdata *pdata = card_ctx->dev->platform_data;
1699 spin_lock_irq(&pdata->lpe_audio_slock);
1700 pdata->notify_audio_lpe = NULL;
1701 spin_unlock_irq(&pdata->lpe_audio_slock);
1703 for_each_port(card_ctx, port) {
1704 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
1706 cancel_work_sync(&ctx->hdmi_audio_wq);
1709 if (card_ctx->mmio_start)
1710 iounmap(card_ctx->mmio_start);
1711 if (card_ctx->irq >= 0)
1712 free_irq(card_ctx->irq, card_ctx);
1716 * hdmi_lpe_audio_probe - start bridge with i915
1718 * This function is called when the i915 driver creates the
1719 * hdmi-lpe-audio platform device.
1721 static int hdmi_lpe_audio_probe(struct platform_device *pdev)
1723 struct snd_card *card;
1724 struct snd_intelhad_card *card_ctx;
1725 struct snd_pcm *pcm;
1726 struct intel_hdmi_lpe_audio_pdata *pdata;
1728 struct resource *res_mmio;
1731 pdata = pdev->dev.platform_data;
1733 dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
1738 irq = platform_get_irq(pdev, 0);
1740 dev_err(&pdev->dev, "Could not get irq resource\n");
1744 res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1746 dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
1750 /* create a card instance with ALSA framework */
1751 ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
1752 THIS_MODULE, sizeof(*card_ctx), &card);
1756 card_ctx = card->private_data;
1757 card_ctx->dev = &pdev->dev;
1758 card_ctx->card = card;
1759 strcpy(card->driver, INTEL_HAD);
1760 strcpy(card->shortname, "Intel HDMI/DP LPE Audio");
1761 strcpy(card->longname, "Intel HDMI/DP LPE Audio");
1765 card->private_free = hdmi_lpe_audio_free;
1767 platform_set_drvdata(pdev, card_ctx);
1769 dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
1770 __func__, (unsigned int)res_mmio->start,
1771 (unsigned int)res_mmio->end);
1773 card_ctx->mmio_start = ioremap_nocache(res_mmio->start,
1774 (size_t)(resource_size(res_mmio)));
1775 if (!card_ctx->mmio_start) {
1776 dev_err(&pdev->dev, "Could not get ioremap\n");
1781 /* setup interrupt handler */
1782 ret = request_irq(irq, display_pipe_interrupt_handler, 0,
1783 pdev->name, card_ctx);
1785 dev_err(&pdev->dev, "request_irq failed\n");
1789 card_ctx->irq = irq;
1791 /* only 32bit addressable */
1792 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
1793 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1795 init_channel_allocations();
1797 card_ctx->num_ports = 1;
1799 for_each_port(card_ctx, port) {
1800 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
1803 ctx->card_ctx = card_ctx;
1804 ctx->dev = card_ctx->dev;
1806 INIT_WORK(&ctx->hdmi_audio_wq, had_audio_wq);
1808 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1810 ret = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
1811 MAX_CAP_STREAMS, &pcm);
1815 /* setup private data which can be retrieved when required */
1816 pcm->private_data = ctx;
1817 pcm->info_flags = 0;
1818 strncpy(pcm->name, card->shortname, strlen(card->shortname));
1819 /* setup the ops for playabck */
1820 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &had_pcm_ops);
1822 /* allocate dma pages;
1823 * try to allocate 600k buffer as default which is large enough
1825 snd_pcm_lib_preallocate_pages_for_all(pcm,
1826 SNDRV_DMA_TYPE_DEV, NULL,
1827 HAD_DEFAULT_BUFFER, HAD_MAX_BUFFER);
1829 /* create controls */
1830 for (i = 0; i < ARRAY_SIZE(had_controls); i++) {
1831 struct snd_kcontrol *kctl;
1833 kctl = snd_ctl_new1(&had_controls[i], ctx);
1839 kctl->id.device = pcm->device;
1841 ret = snd_ctl_add(card, kctl);
1846 /* Register channel map controls */
1847 ret = had_register_chmap_ctls(ctx, pcm);
1851 ret = had_create_jack(ctx, pcm);
1856 ret = snd_card_register(card);
1860 spin_lock_irq(&pdata->lpe_audio_slock);
1861 pdata->notify_audio_lpe = notify_audio_lpe;
1862 spin_unlock_irq(&pdata->lpe_audio_slock);
1864 pm_runtime_use_autosuspend(&pdev->dev);
1865 pm_runtime_mark_last_busy(&pdev->dev);
1866 pm_runtime_set_active(&pdev->dev);
1868 dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
1869 for_each_port(card_ctx, port) {
1870 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
1872 schedule_work(&ctx->hdmi_audio_wq);
1878 snd_card_free(card);
1883 * hdmi_lpe_audio_remove - stop bridge with i915
1885 * This function is called when the platform device is destroyed.
1887 static int hdmi_lpe_audio_remove(struct platform_device *pdev)
1889 struct snd_intelhad_card *card_ctx = platform_get_drvdata(pdev);
1891 snd_card_free(card_ctx->card);
1895 static const struct dev_pm_ops hdmi_lpe_audio_pm = {
1896 SET_SYSTEM_SLEEP_PM_OPS(hdmi_lpe_audio_suspend, hdmi_lpe_audio_resume)
1897 SET_RUNTIME_PM_OPS(hdmi_lpe_audio_runtime_suspend,
1898 hdmi_lpe_audio_runtime_resume, NULL)
1901 static struct platform_driver hdmi_lpe_audio_driver = {
1903 .name = "hdmi-lpe-audio",
1904 .pm = &hdmi_lpe_audio_pm,
1906 .probe = hdmi_lpe_audio_probe,
1907 .remove = hdmi_lpe_audio_remove,
1910 module_platform_driver(hdmi_lpe_audio_driver);
1911 MODULE_ALIAS("platform:hdmi_lpe_audio");
1913 MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1914 MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1915 MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1916 MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1917 MODULE_DESCRIPTION("Intel HDMI Audio driver");
1918 MODULE_LICENSE("GPL v2");
1919 MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");