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/set_memory.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_pipe(card_ctx, pipe) \
46 for ((pipe) = 0; (pipe) < (card_ctx)->num_pipes; (pipe)++)
47 #define for_each_port(card_ctx, port) \
48 for ((port) = 0; (port) < (card_ctx)->num_ports; (port)++)
50 /*standard module options for ALSA. This module supports only one card*/
51 static int hdmi_card_index = SNDRV_DEFAULT_IDX1;
52 static char *hdmi_card_id = SNDRV_DEFAULT_STR1;
54 module_param_named(index, hdmi_card_index, int, 0444);
55 MODULE_PARM_DESC(index,
56 "Index value for INTEL Intel HDMI Audio controller.");
57 module_param_named(id, hdmi_card_id, charp, 0444);
59 "ID string for INTEL Intel HDMI Audio controller.");
62 * ELD SA bits in the CEA Speaker Allocation data block
64 static const int eld_speaker_allocation_bits[] = {
72 /* the following are not defined in ELD yet */
77 * This is an ordered list!
79 * The preceding ones have better chances to be selected by
80 * hdmi_channel_allocation().
82 static struct cea_channel_speaker_allocation channel_allocations[] = {
83 /* channel: 7 6 5 4 3 2 1 0 */
84 { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
86 { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
88 { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
90 { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
92 { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
94 { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
96 { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
98 { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
100 { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
102 { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
103 { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
104 { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
105 { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
106 { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
107 { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
108 { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
109 { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
110 { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
111 { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
112 { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
113 { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
114 { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
115 { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
116 { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
117 { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
118 { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
119 { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
120 { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
121 { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
122 { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
123 { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
124 { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
127 static const struct channel_map_table map_tables[] = {
128 { SNDRV_CHMAP_FL, 0x00, FL },
129 { SNDRV_CHMAP_FR, 0x01, FR },
130 { SNDRV_CHMAP_RL, 0x04, RL },
131 { SNDRV_CHMAP_RR, 0x05, RR },
132 { SNDRV_CHMAP_LFE, 0x02, LFE },
133 { SNDRV_CHMAP_FC, 0x03, FC },
134 { SNDRV_CHMAP_RLC, 0x06, RLC },
135 { SNDRV_CHMAP_RRC, 0x07, RRC },
139 /* hardware capability structure */
140 static const struct snd_pcm_hardware had_pcm_hardware = {
141 .info = (SNDRV_PCM_INFO_INTERLEAVED |
142 SNDRV_PCM_INFO_MMAP |
143 SNDRV_PCM_INFO_MMAP_VALID |
144 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
145 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
146 SNDRV_PCM_FMTBIT_S24_LE |
147 SNDRV_PCM_FMTBIT_S32_LE),
148 .rates = SNDRV_PCM_RATE_32000 |
149 SNDRV_PCM_RATE_44100 |
150 SNDRV_PCM_RATE_48000 |
151 SNDRV_PCM_RATE_88200 |
152 SNDRV_PCM_RATE_96000 |
153 SNDRV_PCM_RATE_176400 |
154 SNDRV_PCM_RATE_192000,
155 .rate_min = HAD_MIN_RATE,
156 .rate_max = HAD_MAX_RATE,
157 .channels_min = HAD_MIN_CHANNEL,
158 .channels_max = HAD_MAX_CHANNEL,
159 .buffer_bytes_max = HAD_MAX_BUFFER,
160 .period_bytes_min = HAD_MIN_PERIOD_BYTES,
161 .period_bytes_max = HAD_MAX_PERIOD_BYTES,
162 .periods_min = HAD_MIN_PERIODS,
163 .periods_max = HAD_MAX_PERIODS,
164 .fifo_size = HAD_FIFO_SIZE,
167 /* Get the active PCM substream;
168 * Call had_substream_put() for unreferecing.
169 * Don't call this inside had_spinlock, as it takes by itself
171 static struct snd_pcm_substream *
172 had_substream_get(struct snd_intelhad *intelhaddata)
174 struct snd_pcm_substream *substream;
177 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
178 substream = intelhaddata->stream_info.substream;
180 intelhaddata->stream_info.substream_refcount++;
181 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
185 /* Unref the active PCM substream;
186 * Don't call this inside had_spinlock, as it takes by itself
188 static void had_substream_put(struct snd_intelhad *intelhaddata)
192 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
193 intelhaddata->stream_info.substream_refcount--;
194 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
197 static u32 had_config_offset(int pipe)
202 return AUDIO_HDMI_CONFIG_A;
204 return AUDIO_HDMI_CONFIG_B;
206 return AUDIO_HDMI_CONFIG_C;
210 /* Register access functions */
211 static u32 had_read_register_raw(struct snd_intelhad_card *card_ctx,
214 return ioread32(card_ctx->mmio_start + had_config_offset(pipe) + reg);
217 static void had_write_register_raw(struct snd_intelhad_card *card_ctx,
218 int pipe, u32 reg, u32 val)
220 iowrite32(val, card_ctx->mmio_start + had_config_offset(pipe) + reg);
223 static void had_read_register(struct snd_intelhad *ctx, u32 reg, u32 *val)
228 *val = had_read_register_raw(ctx->card_ctx, ctx->pipe, reg);
231 static void had_write_register(struct snd_intelhad *ctx, u32 reg, u32 val)
234 had_write_register_raw(ctx->card_ctx, ctx->pipe, reg, val);
238 * enable / disable audio configuration
240 * The normal read/modify should not directly be used on VLV2 for
241 * updating AUD_CONFIG register.
243 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
244 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
245 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
246 * register. This field should be 1xy binary for configuration with 6 or
247 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
248 * causes the "channels" field to be updated as 0xy binary resulting in
249 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
250 * appropriate value when doing read-modify of AUD_CONFIG register.
252 static void had_enable_audio(struct snd_intelhad *intelhaddata,
255 /* update the cached value */
256 intelhaddata->aud_config.regx.aud_en = enable;
257 had_write_register(intelhaddata, AUD_CONFIG,
258 intelhaddata->aud_config.regval);
261 /* forcibly ACKs to both BUFFER_DONE and BUFFER_UNDERRUN interrupts */
262 static void had_ack_irqs(struct snd_intelhad *ctx)
268 had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
269 status_reg |= HDMI_AUDIO_BUFFER_DONE | HDMI_AUDIO_UNDERRUN;
270 had_write_register(ctx, AUD_HDMI_STATUS, status_reg);
271 had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
274 /* Reset buffer pointers */
275 static void had_reset_audio(struct snd_intelhad *intelhaddata)
277 had_write_register(intelhaddata, AUD_HDMI_STATUS,
278 AUD_HDMI_STATUSG_MASK_FUNCRST);
279 had_write_register(intelhaddata, AUD_HDMI_STATUS, 0);
283 * initialize audio channel status registers
284 * This function is called in the prepare callback
286 static int had_prog_status_reg(struct snd_pcm_substream *substream,
287 struct snd_intelhad *intelhaddata)
289 union aud_cfg cfg_val = {.regval = 0};
290 union aud_ch_status_0 ch_stat0 = {.regval = 0};
291 union aud_ch_status_1 ch_stat1 = {.regval = 0};
293 ch_stat0.regx.lpcm_id = (intelhaddata->aes_bits &
294 IEC958_AES0_NONAUDIO) >> 1;
295 ch_stat0.regx.clk_acc = (intelhaddata->aes_bits &
296 IEC958_AES3_CON_CLOCK) >> 4;
297 cfg_val.regx.val_bit = ch_stat0.regx.lpcm_id;
299 switch (substream->runtime->rate) {
300 case AUD_SAMPLE_RATE_32:
301 ch_stat0.regx.samp_freq = CH_STATUS_MAP_32KHZ;
304 case AUD_SAMPLE_RATE_44_1:
305 ch_stat0.regx.samp_freq = CH_STATUS_MAP_44KHZ;
307 case AUD_SAMPLE_RATE_48:
308 ch_stat0.regx.samp_freq = CH_STATUS_MAP_48KHZ;
310 case AUD_SAMPLE_RATE_88_2:
311 ch_stat0.regx.samp_freq = CH_STATUS_MAP_88KHZ;
313 case AUD_SAMPLE_RATE_96:
314 ch_stat0.regx.samp_freq = CH_STATUS_MAP_96KHZ;
316 case AUD_SAMPLE_RATE_176_4:
317 ch_stat0.regx.samp_freq = CH_STATUS_MAP_176KHZ;
319 case AUD_SAMPLE_RATE_192:
320 ch_stat0.regx.samp_freq = CH_STATUS_MAP_192KHZ;
324 /* control should never come here */
328 had_write_register(intelhaddata,
329 AUD_CH_STATUS_0, ch_stat0.regval);
331 switch (substream->runtime->format) {
332 case SNDRV_PCM_FORMAT_S16_LE:
333 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_20;
334 ch_stat1.regx.wrd_len = SMPL_WIDTH_16BITS;
336 case SNDRV_PCM_FORMAT_S24_LE:
337 case SNDRV_PCM_FORMAT_S32_LE:
338 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_24;
339 ch_stat1.regx.wrd_len = SMPL_WIDTH_24BITS;
345 had_write_register(intelhaddata,
346 AUD_CH_STATUS_1, ch_stat1.regval);
351 * function to initialize audio
352 * registers and buffer confgiuration registers
353 * This function is called in the prepare callback
355 static int had_init_audio_ctrl(struct snd_pcm_substream *substream,
356 struct snd_intelhad *intelhaddata)
358 union aud_cfg cfg_val = {.regval = 0};
359 union aud_buf_config buf_cfg = {.regval = 0};
362 had_prog_status_reg(substream, intelhaddata);
364 buf_cfg.regx.audio_fifo_watermark = FIFO_THRESHOLD;
365 buf_cfg.regx.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
366 buf_cfg.regx.aud_delay = 0;
367 had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.regval);
369 channels = substream->runtime->channels;
370 cfg_val.regx.num_ch = channels - 2;
372 cfg_val.regx.layout = LAYOUT0;
374 cfg_val.regx.layout = LAYOUT1;
376 if (substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE)
377 cfg_val.regx.packet_mode = 1;
379 if (substream->runtime->format == SNDRV_PCM_FORMAT_S32_LE)
380 cfg_val.regx.left_align = 1;
382 cfg_val.regx.val_bit = 1;
384 /* fix up the DP bits */
385 if (intelhaddata->dp_output) {
386 cfg_val.regx.dp_modei = 1;
387 cfg_val.regx.set = 1;
390 had_write_register(intelhaddata, AUD_CONFIG, cfg_val.regval);
391 intelhaddata->aud_config = cfg_val;
396 * Compute derived values in channel_allocations[].
398 static void init_channel_allocations(void)
401 struct cea_channel_speaker_allocation *p;
403 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
404 p = channel_allocations + i;
407 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
408 if (p->speakers[j]) {
410 p->spk_mask |= p->speakers[j];
416 * The transformation takes two steps:
418 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
419 * spk_mask => (channel_allocations[]) => ai->CA
421 * TODO: it could select the wrong CA from multiple candidates.
423 static int had_channel_allocation(struct snd_intelhad *intelhaddata,
431 * CA defaults to 0 for basic stereo audio
437 * expand ELD's speaker allocation mask
439 * ELD tells the speaker mask in a compact(paired) form,
440 * expand ELD's notions to match the ones used by Audio InfoFrame.
443 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
444 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
445 spk_mask |= eld_speaker_allocation_bits[i];
448 /* search for the first working match in the CA table */
449 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
450 if (channels == channel_allocations[i].channels &&
451 (spk_mask & channel_allocations[i].spk_mask) ==
452 channel_allocations[i].spk_mask) {
453 ca = channel_allocations[i].ca_index;
458 dev_dbg(intelhaddata->dev, "select CA 0x%x for %d\n", ca, channels);
463 /* from speaker bit mask to ALSA API channel position */
464 static int spk_to_chmap(int spk)
466 const struct channel_map_table *t = map_tables;
468 for (; t->map; t++) {
469 if (t->spk_mask == spk)
475 static void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
479 struct snd_pcm_chmap_elem *chmap;
480 u8 eld_high, eld_high_mask = 0xF0;
483 kfree(intelhaddata->chmap->chmap);
484 intelhaddata->chmap->chmap = NULL;
486 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
490 dev_dbg(intelhaddata->dev, "eld speaker = %x\n",
491 intelhaddata->eld[DRM_ELD_SPEAKER]);
493 /* WA: Fix the max channel supported to 8 */
496 * Sink may support more than 8 channels, if eld_high has more than
497 * one bit set. SOC supports max 8 channels.
498 * Refer eld_speaker_allocation_bits, for sink speaker allocation
501 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
502 eld_high = intelhaddata->eld[DRM_ELD_SPEAKER] & eld_high_mask;
503 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
504 /* eld_high & (eld_high-1): if more than 1 bit set */
505 /* 0x1F: 7 channels */
506 for (i = 1; i < 4; i++) {
507 high_msb = eld_high & (0x80 >> i);
509 intelhaddata->eld[DRM_ELD_SPEAKER] &=
516 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
517 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
518 spk_mask |= eld_speaker_allocation_bits[i];
521 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
522 if (spk_mask == channel_allocations[i].spk_mask) {
523 for (c = 0; c < channel_allocations[i].channels; c++) {
524 chmap->map[c] = spk_to_chmap(
525 channel_allocations[i].speakers[
526 (MAX_SPEAKERS - 1) - c]);
528 chmap->channels = channel_allocations[i].channels;
529 intelhaddata->chmap->chmap = chmap;
533 if (i >= ARRAY_SIZE(channel_allocations))
538 * ALSA API channel-map control callbacks
540 static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
541 struct snd_ctl_elem_info *uinfo)
543 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
544 uinfo->count = HAD_MAX_CHANNEL;
545 uinfo->value.integer.min = 0;
546 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
550 static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
551 struct snd_ctl_elem_value *ucontrol)
553 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
554 struct snd_intelhad *intelhaddata = info->private_data;
556 const struct snd_pcm_chmap_elem *chmap;
558 memset(ucontrol->value.integer.value, 0,
559 sizeof(long) * HAD_MAX_CHANNEL);
560 mutex_lock(&intelhaddata->mutex);
561 if (!intelhaddata->chmap->chmap) {
562 mutex_unlock(&intelhaddata->mutex);
566 chmap = intelhaddata->chmap->chmap;
567 for (i = 0; i < chmap->channels; i++)
568 ucontrol->value.integer.value[i] = chmap->map[i];
569 mutex_unlock(&intelhaddata->mutex);
574 static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
579 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
580 NULL, 0, (unsigned long)intelhaddata,
581 &intelhaddata->chmap);
585 intelhaddata->chmap->private_data = intelhaddata;
586 intelhaddata->chmap->kctl->info = had_chmap_ctl_info;
587 intelhaddata->chmap->kctl->get = had_chmap_ctl_get;
588 intelhaddata->chmap->chmap = NULL;
593 * Initialize Data Island Packets registers
594 * This function is called in the prepare callback
596 static void had_prog_dip(struct snd_pcm_substream *substream,
597 struct snd_intelhad *intelhaddata)
600 union aud_ctrl_st ctrl_state = {.regval = 0};
601 union aud_info_frame2 frame2 = {.regval = 0};
602 union aud_info_frame3 frame3 = {.regval = 0};
608 channels = substream->runtime->channels;
610 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
612 ca = had_channel_allocation(intelhaddata, channels);
613 if (intelhaddata->dp_output) {
614 info_frame = DP_INFO_FRAME_WORD1;
615 frame2.regval = (substream->runtime->channels - 1) | (ca << 24);
617 info_frame = HDMI_INFO_FRAME_WORD1;
618 frame2.regx.chnl_cnt = substream->runtime->channels - 1;
619 frame3.regx.chnl_alloc = ca;
621 /* Calculte the byte wide checksum for all valid DIP words */
622 for (i = 0; i < BYTES_PER_WORD; i++)
623 checksum += (info_frame >> (i * 8)) & 0xff;
624 for (i = 0; i < BYTES_PER_WORD; i++)
625 checksum += (frame2.regval >> (i * 8)) & 0xff;
626 for (i = 0; i < BYTES_PER_WORD; i++)
627 checksum += (frame3.regval >> (i * 8)) & 0xff;
629 frame2.regx.chksum = -(checksum);
632 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, info_frame);
633 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame2.regval);
634 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame3.regval);
636 /* program remaining DIP words with zero */
637 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
638 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, 0x0);
640 ctrl_state.regx.dip_freq = 1;
641 ctrl_state.regx.dip_en_sta = 1;
642 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
645 static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
649 /* Select maud according to DP 1.2 spec */
650 if (link_rate == DP_2_7_GHZ) {
651 switch (aud_samp_freq) {
652 case AUD_SAMPLE_RATE_32:
653 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
656 case AUD_SAMPLE_RATE_44_1:
657 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
660 case AUD_SAMPLE_RATE_48:
661 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
664 case AUD_SAMPLE_RATE_88_2:
665 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
668 case AUD_SAMPLE_RATE_96:
669 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
672 case AUD_SAMPLE_RATE_176_4:
673 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
677 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
684 } else if (link_rate == DP_1_62_GHZ) {
685 switch (aud_samp_freq) {
686 case AUD_SAMPLE_RATE_32:
687 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
690 case AUD_SAMPLE_RATE_44_1:
691 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
694 case AUD_SAMPLE_RATE_48:
695 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
698 case AUD_SAMPLE_RATE_88_2:
699 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
702 case AUD_SAMPLE_RATE_96:
703 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
706 case AUD_SAMPLE_RATE_176_4:
707 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
711 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
725 * Program HDMI audio CTS value
727 * @aud_samp_freq: sampling frequency of audio data
728 * @tmds: sampling frequency of the display data
729 * @link_rate: DP link rate
730 * @n_param: N value, depends on aud_samp_freq
731 * @intelhaddata: substream private data
733 * Program CTS register based on the audio and display sampling frequency
735 static void had_prog_cts(u32 aud_samp_freq, u32 tmds, u32 link_rate,
736 u32 n_param, struct snd_intelhad *intelhaddata)
739 u64 dividend, divisor;
741 if (intelhaddata->dp_output) {
742 /* Substitute cts_val with Maud according to DP 1.2 spec*/
743 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
745 /* Calculate CTS according to HDMI 1.3a spec*/
746 dividend = (u64)tmds * n_param*1000;
747 divisor = 128 * aud_samp_freq;
748 cts_val = div64_u64(dividend, divisor);
750 dev_dbg(intelhaddata->dev, "TMDS value=%d, N value=%d, CTS Value=%d\n",
751 tmds, n_param, cts_val);
752 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
755 static int had_calculate_n_value(u32 aud_samp_freq)
759 /* Select N according to HDMI 1.3a spec*/
760 switch (aud_samp_freq) {
761 case AUD_SAMPLE_RATE_32:
765 case AUD_SAMPLE_RATE_44_1:
769 case AUD_SAMPLE_RATE_48:
773 case AUD_SAMPLE_RATE_88_2:
777 case AUD_SAMPLE_RATE_96:
781 case AUD_SAMPLE_RATE_176_4:
797 * Program HDMI audio N value
799 * @aud_samp_freq: sampling frequency of audio data
800 * @n_param: N value, depends on aud_samp_freq
801 * @intelhaddata: substream private data
803 * This function is called in the prepare callback.
804 * It programs based on the audio and display sampling frequency
806 static int had_prog_n(u32 aud_samp_freq, u32 *n_param,
807 struct snd_intelhad *intelhaddata)
811 if (intelhaddata->dp_output) {
813 * According to DP specs, Maud and Naud values hold
814 * a relationship, which is stated as:
815 * Maud/Naud = 512 * fs / f_LS_Clk
816 * where, fs is the sampling frequency of the audio stream
817 * and Naud is 32768 for Async clock.
822 n_val = had_calculate_n_value(aud_samp_freq);
827 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
833 * PCM ring buffer handling
835 * The hardware provides a ring buffer with the fixed 4 buffer descriptors
836 * (BDs). The driver maps these 4 BDs onto the PCM ring buffer. The mapping
837 * moves at each period elapsed. The below illustrates how it works:
840 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
841 * BD | 0 | 1 | 2 | 3 |
843 * At time=1 (period elapsed)
844 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
845 * BD | 1 | 2 | 3 | 0 |
847 * At time=2 (second period elapsed)
848 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
849 * BD | 2 | 3 | 0 | 1 |
851 * The bd_head field points to the index of the BD to be read. It's also the
852 * position to be filled at next. The pcm_head and the pcm_filled fields
853 * point to the indices of the current position and of the next position to
854 * be filled, respectively. For PCM buffer there are both _head and _filled
855 * because they may be difference when nperiods > 4. For example, in the
856 * example above at t=1, bd_head=1 and pcm_head=1 while pcm_filled=5:
858 * pcm_head (=1) --v v-- pcm_filled (=5)
859 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
860 * BD | 1 | 2 | 3 | 0 |
861 * bd_head (=1) --^ ^-- next to fill (= bd_head)
863 * For nperiods < 4, the remaining BDs out of 4 are marked as invalid, so that
864 * the hardware skips those BDs in the loop.
866 * An exceptional setup is the case with nperiods=1. Since we have to update
867 * BDs after finishing one BD processing, we'd need at least two BDs, where
868 * both BDs point to the same content, the same address, the same size of the
872 #define AUD_BUF_ADDR(x) (AUD_BUF_A_ADDR + (x) * HAD_REG_WIDTH)
873 #define AUD_BUF_LEN(x) (AUD_BUF_A_LENGTH + (x) * HAD_REG_WIDTH)
875 /* Set up a buffer descriptor at the "filled" position */
876 static void had_prog_bd(struct snd_pcm_substream *substream,
877 struct snd_intelhad *intelhaddata)
879 int idx = intelhaddata->bd_head;
880 int ofs = intelhaddata->pcmbuf_filled * intelhaddata->period_bytes;
881 u32 addr = substream->runtime->dma_addr + ofs;
883 addr |= AUD_BUF_VALID;
884 if (!substream->runtime->no_period_wakeup)
885 addr |= AUD_BUF_INTR_EN;
886 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), addr);
887 had_write_register(intelhaddata, AUD_BUF_LEN(idx),
888 intelhaddata->period_bytes);
890 /* advance the indices to the next */
891 intelhaddata->bd_head++;
892 intelhaddata->bd_head %= intelhaddata->num_bds;
893 intelhaddata->pcmbuf_filled++;
894 intelhaddata->pcmbuf_filled %= substream->runtime->periods;
897 /* invalidate a buffer descriptor with the given index */
898 static void had_invalidate_bd(struct snd_intelhad *intelhaddata,
901 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), 0);
902 had_write_register(intelhaddata, AUD_BUF_LEN(idx), 0);
905 /* Initial programming of ring buffer */
906 static void had_init_ringbuf(struct snd_pcm_substream *substream,
907 struct snd_intelhad *intelhaddata)
909 struct snd_pcm_runtime *runtime = substream->runtime;
912 num_periods = runtime->periods;
913 intelhaddata->num_bds = min(num_periods, HAD_NUM_OF_RING_BUFS);
914 /* set the minimum 2 BDs for num_periods=1 */
915 intelhaddata->num_bds = max(intelhaddata->num_bds, 2U);
916 intelhaddata->period_bytes =
917 frames_to_bytes(runtime, runtime->period_size);
918 WARN_ON(intelhaddata->period_bytes & 0x3f);
920 intelhaddata->bd_head = 0;
921 intelhaddata->pcmbuf_head = 0;
922 intelhaddata->pcmbuf_filled = 0;
924 for (i = 0; i < HAD_NUM_OF_RING_BUFS; i++) {
925 if (i < intelhaddata->num_bds)
926 had_prog_bd(substream, intelhaddata);
927 else /* invalidate the rest */
928 had_invalidate_bd(intelhaddata, i);
931 intelhaddata->bd_head = 0; /* reset at head again before starting */
934 /* process a bd, advance to the next */
935 static void had_advance_ringbuf(struct snd_pcm_substream *substream,
936 struct snd_intelhad *intelhaddata)
938 int num_periods = substream->runtime->periods;
940 /* reprogram the next buffer */
941 had_prog_bd(substream, intelhaddata);
943 /* proceed to next */
944 intelhaddata->pcmbuf_head++;
945 intelhaddata->pcmbuf_head %= num_periods;
948 /* process the current BD(s);
949 * returns the current PCM buffer byte position, or -EPIPE for underrun.
951 static int had_process_ringbuf(struct snd_pcm_substream *substream,
952 struct snd_intelhad *intelhaddata)
958 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
960 /* get the remaining bytes on the buffer */
961 had_read_register(intelhaddata,
962 AUD_BUF_LEN(intelhaddata->bd_head),
964 if (len < 0 || len > intelhaddata->period_bytes) {
965 dev_dbg(intelhaddata->dev, "Invalid buf length %d\n",
971 if (len > 0) /* OK, this is the current buffer */
974 /* len=0 => already empty, check the next buffer */
975 if (++processed >= intelhaddata->num_bds) {
976 len = -EPIPE; /* all empty? - report underrun */
979 had_advance_ringbuf(substream, intelhaddata);
982 len = intelhaddata->period_bytes - len;
983 len += intelhaddata->period_bytes * intelhaddata->pcmbuf_head;
985 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
989 /* called from irq handler */
990 static void had_process_buffer_done(struct snd_intelhad *intelhaddata)
992 struct snd_pcm_substream *substream;
994 substream = had_substream_get(intelhaddata);
996 return; /* no stream? - bail out */
998 if (!intelhaddata->connected) {
999 snd_pcm_stop_xrun(substream);
1000 goto out; /* disconnected? - bail out */
1003 /* process or stop the stream */
1004 if (had_process_ringbuf(substream, intelhaddata) < 0)
1005 snd_pcm_stop_xrun(substream);
1007 snd_pcm_period_elapsed(substream);
1010 had_substream_put(intelhaddata);
1014 * The interrupt status 'sticky' bits might not be cleared by
1015 * setting '1' to that bit once...
1017 static void wait_clear_underrun_bit(struct snd_intelhad *intelhaddata)
1022 for (i = 0; i < 100; i++) {
1023 /* clear bit30, 31 AUD_HDMI_STATUS */
1024 had_read_register(intelhaddata, AUD_HDMI_STATUS, &val);
1025 if (!(val & AUD_HDMI_STATUS_MASK_UNDERRUN))
1029 had_write_register(intelhaddata, AUD_HDMI_STATUS, val);
1031 dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n");
1034 /* Perform some reset procedure but only when need_reset is set;
1035 * this is called from prepare or hw_free callbacks once after trigger STOP
1036 * or underrun has been processed in order to settle down the h/w state.
1038 static void had_do_reset(struct snd_intelhad *intelhaddata)
1040 if (!intelhaddata->need_reset || !intelhaddata->connected)
1043 /* Reset buffer pointers */
1044 had_reset_audio(intelhaddata);
1045 wait_clear_underrun_bit(intelhaddata);
1046 intelhaddata->need_reset = false;
1049 /* called from irq handler */
1050 static void had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
1052 struct snd_pcm_substream *substream;
1054 /* Report UNDERRUN error to above layers */
1055 substream = had_substream_get(intelhaddata);
1057 snd_pcm_stop_xrun(substream);
1058 had_substream_put(intelhaddata);
1060 intelhaddata->need_reset = true;
1064 * ALSA PCM open callback
1066 static int had_pcm_open(struct snd_pcm_substream *substream)
1068 struct snd_intelhad *intelhaddata;
1069 struct snd_pcm_runtime *runtime;
1072 intelhaddata = snd_pcm_substream_chip(substream);
1073 runtime = substream->runtime;
1075 pm_runtime_get_sync(intelhaddata->dev);
1077 /* set the runtime hw parameter with local snd_pcm_hardware struct */
1078 runtime->hw = had_pcm_hardware;
1080 retval = snd_pcm_hw_constraint_integer(runtime,
1081 SNDRV_PCM_HW_PARAM_PERIODS);
1085 /* Make sure, that the period size is always aligned
1088 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1089 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
1093 retval = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1097 /* expose PCM substream */
1098 spin_lock_irq(&intelhaddata->had_spinlock);
1099 intelhaddata->stream_info.substream = substream;
1100 intelhaddata->stream_info.substream_refcount++;
1101 spin_unlock_irq(&intelhaddata->had_spinlock);
1105 pm_runtime_mark_last_busy(intelhaddata->dev);
1106 pm_runtime_put_autosuspend(intelhaddata->dev);
1111 * ALSA PCM close callback
1113 static int had_pcm_close(struct snd_pcm_substream *substream)
1115 struct snd_intelhad *intelhaddata;
1117 intelhaddata = snd_pcm_substream_chip(substream);
1119 /* unreference and sync with the pending PCM accesses */
1120 spin_lock_irq(&intelhaddata->had_spinlock);
1121 intelhaddata->stream_info.substream = NULL;
1122 intelhaddata->stream_info.substream_refcount--;
1123 while (intelhaddata->stream_info.substream_refcount > 0) {
1124 spin_unlock_irq(&intelhaddata->had_spinlock);
1126 spin_lock_irq(&intelhaddata->had_spinlock);
1128 spin_unlock_irq(&intelhaddata->had_spinlock);
1130 pm_runtime_mark_last_busy(intelhaddata->dev);
1131 pm_runtime_put_autosuspend(intelhaddata->dev);
1136 * ALSA PCM hw_params callback
1138 static int had_pcm_hw_params(struct snd_pcm_substream *substream,
1139 struct snd_pcm_hw_params *hw_params)
1141 struct snd_intelhad *intelhaddata;
1143 int pages, buf_size, retval;
1145 intelhaddata = snd_pcm_substream_chip(substream);
1146 buf_size = params_buffer_bytes(hw_params);
1147 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1150 dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
1151 __func__, buf_size);
1152 /* mark the pages as uncached region */
1153 addr = (unsigned long) substream->runtime->dma_area;
1154 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1155 retval = set_memory_uc(addr, pages);
1157 dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
1161 memset(substream->runtime->dma_area, 0, buf_size);
1167 * ALSA PCM hw_free callback
1169 static int had_pcm_hw_free(struct snd_pcm_substream *substream)
1171 struct snd_intelhad *intelhaddata;
1175 intelhaddata = snd_pcm_substream_chip(substream);
1176 had_do_reset(intelhaddata);
1178 /* mark back the pages as cached/writeback region before the free */
1179 if (substream->runtime->dma_area != NULL) {
1180 addr = (unsigned long) substream->runtime->dma_area;
1181 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1183 set_memory_wb(addr, pages);
1184 return snd_pcm_lib_free_pages(substream);
1190 * ALSA PCM trigger callback
1192 static int had_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
1195 struct snd_intelhad *intelhaddata;
1197 intelhaddata = snd_pcm_substream_chip(substream);
1199 spin_lock(&intelhaddata->had_spinlock);
1201 case SNDRV_PCM_TRIGGER_START:
1202 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1203 case SNDRV_PCM_TRIGGER_RESUME:
1205 had_ack_irqs(intelhaddata); /* FIXME: do we need this? */
1206 had_enable_audio(intelhaddata, true);
1209 case SNDRV_PCM_TRIGGER_STOP:
1210 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1212 had_enable_audio(intelhaddata, false);
1213 intelhaddata->need_reset = true;
1219 spin_unlock(&intelhaddata->had_spinlock);
1224 * ALSA PCM prepare callback
1226 static int had_pcm_prepare(struct snd_pcm_substream *substream)
1229 u32 disp_samp_freq, n_param;
1231 struct snd_intelhad *intelhaddata;
1232 struct snd_pcm_runtime *runtime;
1234 intelhaddata = snd_pcm_substream_chip(substream);
1235 runtime = substream->runtime;
1237 dev_dbg(intelhaddata->dev, "period_size=%d\n",
1238 (int)frames_to_bytes(runtime, runtime->period_size));
1239 dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods);
1240 dev_dbg(intelhaddata->dev, "buffer_size=%d\n",
1241 (int)snd_pcm_lib_buffer_bytes(substream));
1242 dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
1243 dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
1245 had_do_reset(intelhaddata);
1247 /* Get N value in KHz */
1248 disp_samp_freq = intelhaddata->tmds_clock_speed;
1250 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
1252 dev_err(intelhaddata->dev,
1253 "programming N value failed %#x\n", retval);
1257 if (intelhaddata->dp_output)
1258 link_rate = intelhaddata->link_rate;
1260 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1261 n_param, intelhaddata);
1263 had_prog_dip(substream, intelhaddata);
1265 retval = had_init_audio_ctrl(substream, intelhaddata);
1267 /* Prog buffer address */
1268 had_init_ringbuf(substream, intelhaddata);
1271 * Program channel mapping in following order:
1272 * FL, FR, C, LFE, RL, RR
1275 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
1282 * ALSA PCM pointer callback
1284 static snd_pcm_uframes_t had_pcm_pointer(struct snd_pcm_substream *substream)
1286 struct snd_intelhad *intelhaddata;
1289 intelhaddata = snd_pcm_substream_chip(substream);
1291 if (!intelhaddata->connected)
1292 return SNDRV_PCM_POS_XRUN;
1294 len = had_process_ringbuf(substream, intelhaddata);
1296 return SNDRV_PCM_POS_XRUN;
1297 len = bytes_to_frames(substream->runtime, len);
1298 /* wrapping may happen when periods=1 */
1299 len %= substream->runtime->buffer_size;
1304 * ALSA PCM mmap callback
1306 static int had_pcm_mmap(struct snd_pcm_substream *substream,
1307 struct vm_area_struct *vma)
1309 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1310 return remap_pfn_range(vma, vma->vm_start,
1311 substream->dma_buffer.addr >> PAGE_SHIFT,
1312 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1318 static const struct snd_pcm_ops had_pcm_ops = {
1319 .open = had_pcm_open,
1320 .close = had_pcm_close,
1321 .ioctl = snd_pcm_lib_ioctl,
1322 .hw_params = had_pcm_hw_params,
1323 .hw_free = had_pcm_hw_free,
1324 .prepare = had_pcm_prepare,
1325 .trigger = had_pcm_trigger,
1326 .pointer = had_pcm_pointer,
1327 .mmap = had_pcm_mmap,
1330 /* process mode change of the running stream; called in mutex */
1331 static int had_process_mode_change(struct snd_intelhad *intelhaddata)
1333 struct snd_pcm_substream *substream;
1335 u32 disp_samp_freq, n_param;
1338 substream = had_substream_get(intelhaddata);
1343 had_enable_audio(intelhaddata, false);
1345 /* Update CTS value */
1346 disp_samp_freq = intelhaddata->tmds_clock_speed;
1348 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
1350 dev_err(intelhaddata->dev,
1351 "programming N value failed %#x\n", retval);
1355 if (intelhaddata->dp_output)
1356 link_rate = intelhaddata->link_rate;
1358 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1359 n_param, intelhaddata);
1362 had_enable_audio(intelhaddata, true);
1365 had_substream_put(intelhaddata);
1369 /* process hot plug, called from wq with mutex locked */
1370 static void had_process_hot_plug(struct snd_intelhad *intelhaddata)
1372 struct snd_pcm_substream *substream;
1374 spin_lock_irq(&intelhaddata->had_spinlock);
1375 if (intelhaddata->connected) {
1376 dev_dbg(intelhaddata->dev, "Device already connected\n");
1377 spin_unlock_irq(&intelhaddata->had_spinlock);
1382 had_enable_audio(intelhaddata, false);
1384 intelhaddata->connected = true;
1385 dev_dbg(intelhaddata->dev,
1386 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
1387 __func__, __LINE__);
1388 spin_unlock_irq(&intelhaddata->had_spinlock);
1390 had_build_channel_allocation_map(intelhaddata);
1392 /* Report to above ALSA layer */
1393 substream = had_substream_get(intelhaddata);
1395 snd_pcm_stop_xrun(substream);
1396 had_substream_put(intelhaddata);
1399 snd_jack_report(intelhaddata->jack, SND_JACK_AVOUT);
1402 /* process hot unplug, called from wq with mutex locked */
1403 static void had_process_hot_unplug(struct snd_intelhad *intelhaddata)
1405 struct snd_pcm_substream *substream;
1407 spin_lock_irq(&intelhaddata->had_spinlock);
1408 if (!intelhaddata->connected) {
1409 dev_dbg(intelhaddata->dev, "Device already disconnected\n");
1410 spin_unlock_irq(&intelhaddata->had_spinlock);
1416 had_enable_audio(intelhaddata, false);
1418 intelhaddata->connected = false;
1419 dev_dbg(intelhaddata->dev,
1420 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
1421 __func__, __LINE__);
1422 spin_unlock_irq(&intelhaddata->had_spinlock);
1424 kfree(intelhaddata->chmap->chmap);
1425 intelhaddata->chmap->chmap = NULL;
1427 /* Report to above ALSA layer */
1428 substream = had_substream_get(intelhaddata);
1430 snd_pcm_stop_xrun(substream);
1431 had_substream_put(intelhaddata);
1434 snd_jack_report(intelhaddata->jack, 0);
1438 * ALSA iec958 and ELD controls
1441 static int had_iec958_info(struct snd_kcontrol *kcontrol,
1442 struct snd_ctl_elem_info *uinfo)
1444 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1449 static int had_iec958_get(struct snd_kcontrol *kcontrol,
1450 struct snd_ctl_elem_value *ucontrol)
1452 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1454 mutex_lock(&intelhaddata->mutex);
1455 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1456 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1457 ucontrol->value.iec958.status[2] =
1458 (intelhaddata->aes_bits >> 16) & 0xff;
1459 ucontrol->value.iec958.status[3] =
1460 (intelhaddata->aes_bits >> 24) & 0xff;
1461 mutex_unlock(&intelhaddata->mutex);
1465 static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1466 struct snd_ctl_elem_value *ucontrol)
1468 ucontrol->value.iec958.status[0] = 0xff;
1469 ucontrol->value.iec958.status[1] = 0xff;
1470 ucontrol->value.iec958.status[2] = 0xff;
1471 ucontrol->value.iec958.status[3] = 0xff;
1475 static int had_iec958_put(struct snd_kcontrol *kcontrol,
1476 struct snd_ctl_elem_value *ucontrol)
1479 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1482 val = (ucontrol->value.iec958.status[0] << 0) |
1483 (ucontrol->value.iec958.status[1] << 8) |
1484 (ucontrol->value.iec958.status[2] << 16) |
1485 (ucontrol->value.iec958.status[3] << 24);
1486 mutex_lock(&intelhaddata->mutex);
1487 if (intelhaddata->aes_bits != val) {
1488 intelhaddata->aes_bits = val;
1491 mutex_unlock(&intelhaddata->mutex);
1495 static int had_ctl_eld_info(struct snd_kcontrol *kcontrol,
1496 struct snd_ctl_elem_info *uinfo)
1498 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1499 uinfo->count = HDMI_MAX_ELD_BYTES;
1503 static int had_ctl_eld_get(struct snd_kcontrol *kcontrol,
1504 struct snd_ctl_elem_value *ucontrol)
1506 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1508 mutex_lock(&intelhaddata->mutex);
1509 memcpy(ucontrol->value.bytes.data, intelhaddata->eld,
1510 HDMI_MAX_ELD_BYTES);
1511 mutex_unlock(&intelhaddata->mutex);
1515 static const struct snd_kcontrol_new had_controls[] = {
1517 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1518 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1519 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1520 .info = had_iec958_info, /* shared */
1521 .get = had_iec958_mask_get,
1524 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1525 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1526 .info = had_iec958_info,
1527 .get = had_iec958_get,
1528 .put = had_iec958_put,
1531 .access = (SNDRV_CTL_ELEM_ACCESS_READ |
1532 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1533 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1535 .info = had_ctl_eld_info,
1536 .get = had_ctl_eld_get,
1541 * audio interrupt handler
1543 static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id)
1545 struct snd_intelhad_card *card_ctx = dev_id;
1546 u32 audio_stat[3] = {};
1549 for_each_pipe(card_ctx, pipe) {
1550 /* use raw register access to ack IRQs even while disconnected */
1551 audio_stat[pipe] = had_read_register_raw(card_ctx, pipe,
1553 (HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE);
1555 if (audio_stat[pipe])
1556 had_write_register_raw(card_ctx, pipe,
1557 AUD_HDMI_STATUS, audio_stat[pipe]);
1560 for_each_port(card_ctx, port) {
1561 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
1562 int pipe = ctx->pipe;
1567 if (audio_stat[pipe] & HDMI_AUDIO_BUFFER_DONE)
1568 had_process_buffer_done(ctx);
1569 if (audio_stat[pipe] & HDMI_AUDIO_UNDERRUN)
1570 had_process_buffer_underrun(ctx);
1577 * monitor plug/unplug notification from i915; just kick off the work
1579 static void notify_audio_lpe(struct platform_device *pdev, int port)
1581 struct snd_intelhad_card *card_ctx = platform_get_drvdata(pdev);
1582 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
1584 schedule_work(&ctx->hdmi_audio_wq);
1587 /* the work to handle monitor hot plug/unplug */
1588 static void had_audio_wq(struct work_struct *work)
1590 struct snd_intelhad *ctx =
1591 container_of(work, struct snd_intelhad, hdmi_audio_wq);
1592 struct intel_hdmi_lpe_audio_pdata *pdata = ctx->dev->platform_data;
1593 struct intel_hdmi_lpe_audio_port_pdata *ppdata = &pdata->port[ctx->port];
1595 pm_runtime_get_sync(ctx->dev);
1596 mutex_lock(&ctx->mutex);
1597 if (ppdata->pipe < 0) {
1598 dev_dbg(ctx->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG : port = %d\n",
1599 __func__, ctx->port);
1601 memset(ctx->eld, 0, sizeof(ctx->eld)); /* clear the old ELD */
1603 ctx->dp_output = false;
1604 ctx->tmds_clock_speed = 0;
1607 /* Shut down the stream */
1608 had_process_hot_unplug(ctx);
1612 dev_dbg(ctx->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
1613 __func__, ctx->port, ppdata->ls_clock);
1615 memcpy(ctx->eld, ppdata->eld, sizeof(ctx->eld));
1617 ctx->dp_output = ppdata->dp_output;
1618 if (ctx->dp_output) {
1619 ctx->tmds_clock_speed = 0;
1620 ctx->link_rate = ppdata->ls_clock;
1622 ctx->tmds_clock_speed = ppdata->ls_clock;
1627 * Shut down the stream before we change
1628 * the pipe assignment for this pcm device
1630 had_process_hot_plug(ctx);
1632 ctx->pipe = ppdata->pipe;
1634 /* Restart the stream if necessary */
1635 had_process_mode_change(ctx);
1638 mutex_unlock(&ctx->mutex);
1639 pm_runtime_mark_last_busy(ctx->dev);
1640 pm_runtime_put_autosuspend(ctx->dev);
1646 static int had_create_jack(struct snd_intelhad *ctx,
1647 struct snd_pcm *pcm)
1652 snprintf(hdmi_str, sizeof(hdmi_str),
1653 "HDMI/DP,pcm=%d", pcm->device);
1655 err = snd_jack_new(ctx->card_ctx->card, hdmi_str,
1656 SND_JACK_AVOUT, &ctx->jack,
1660 ctx->jack->private_data = ctx;
1668 static int hdmi_lpe_audio_runtime_suspend(struct device *dev)
1670 struct snd_intelhad_card *card_ctx = dev_get_drvdata(dev);
1673 for_each_port(card_ctx, port) {
1674 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
1675 struct snd_pcm_substream *substream;
1677 substream = had_substream_get(ctx);
1679 snd_pcm_suspend(substream);
1680 had_substream_put(ctx);
1687 static int __maybe_unused hdmi_lpe_audio_suspend(struct device *dev)
1689 struct snd_intelhad_card *card_ctx = dev_get_drvdata(dev);
1692 err = hdmi_lpe_audio_runtime_suspend(dev);
1694 snd_power_change_state(card_ctx->card, SNDRV_CTL_POWER_D3hot);
1698 static int hdmi_lpe_audio_runtime_resume(struct device *dev)
1700 pm_runtime_mark_last_busy(dev);
1704 static int __maybe_unused hdmi_lpe_audio_resume(struct device *dev)
1706 struct snd_intelhad_card *card_ctx = dev_get_drvdata(dev);
1708 hdmi_lpe_audio_runtime_resume(dev);
1709 snd_power_change_state(card_ctx->card, SNDRV_CTL_POWER_D0);
1713 /* release resources */
1714 static void hdmi_lpe_audio_free(struct snd_card *card)
1716 struct snd_intelhad_card *card_ctx = card->private_data;
1717 struct intel_hdmi_lpe_audio_pdata *pdata = card_ctx->dev->platform_data;
1720 spin_lock_irq(&pdata->lpe_audio_slock);
1721 pdata->notify_audio_lpe = NULL;
1722 spin_unlock_irq(&pdata->lpe_audio_slock);
1724 for_each_port(card_ctx, port) {
1725 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
1727 cancel_work_sync(&ctx->hdmi_audio_wq);
1730 if (card_ctx->mmio_start)
1731 iounmap(card_ctx->mmio_start);
1732 if (card_ctx->irq >= 0)
1733 free_irq(card_ctx->irq, card_ctx);
1737 * hdmi_lpe_audio_probe - start bridge with i915
1739 * This function is called when the i915 driver creates the
1740 * hdmi-lpe-audio platform device.
1742 static int hdmi_lpe_audio_probe(struct platform_device *pdev)
1744 struct snd_card *card;
1745 struct snd_intelhad_card *card_ctx;
1746 struct snd_pcm *pcm;
1747 struct intel_hdmi_lpe_audio_pdata *pdata;
1749 struct resource *res_mmio;
1752 pdata = pdev->dev.platform_data;
1754 dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
1759 irq = platform_get_irq(pdev, 0);
1761 dev_err(&pdev->dev, "Could not get irq resource: %d\n", irq);
1765 res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1767 dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
1771 /* create a card instance with ALSA framework */
1772 ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
1773 THIS_MODULE, sizeof(*card_ctx), &card);
1777 card_ctx = card->private_data;
1778 card_ctx->dev = &pdev->dev;
1779 card_ctx->card = card;
1780 strcpy(card->driver, INTEL_HAD);
1781 strcpy(card->shortname, "Intel HDMI/DP LPE Audio");
1782 strcpy(card->longname, "Intel HDMI/DP LPE Audio");
1786 card->private_free = hdmi_lpe_audio_free;
1788 platform_set_drvdata(pdev, card_ctx);
1790 dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
1791 __func__, (unsigned int)res_mmio->start,
1792 (unsigned int)res_mmio->end);
1794 card_ctx->mmio_start = ioremap_nocache(res_mmio->start,
1795 (size_t)(resource_size(res_mmio)));
1796 if (!card_ctx->mmio_start) {
1797 dev_err(&pdev->dev, "Could not get ioremap\n");
1802 /* setup interrupt handler */
1803 ret = request_irq(irq, display_pipe_interrupt_handler, 0,
1804 pdev->name, card_ctx);
1806 dev_err(&pdev->dev, "request_irq failed\n");
1810 card_ctx->irq = irq;
1812 /* only 32bit addressable */
1813 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
1814 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1816 init_channel_allocations();
1818 card_ctx->num_pipes = pdata->num_pipes;
1819 card_ctx->num_ports = pdata->num_ports;
1821 for_each_port(card_ctx, port) {
1822 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
1825 ctx->card_ctx = card_ctx;
1826 ctx->dev = card_ctx->dev;
1830 spin_lock_init(&ctx->had_spinlock);
1831 mutex_init(&ctx->mutex);
1832 INIT_WORK(&ctx->hdmi_audio_wq, had_audio_wq);
1834 ret = snd_pcm_new(card, INTEL_HAD, port, MAX_PB_STREAMS,
1835 MAX_CAP_STREAMS, &pcm);
1839 /* setup private data which can be retrieved when required */
1840 pcm->private_data = ctx;
1841 pcm->info_flags = 0;
1842 strncpy(pcm->name, card->shortname, strlen(card->shortname));
1843 /* setup the ops for playabck */
1844 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &had_pcm_ops);
1846 /* allocate dma pages;
1847 * try to allocate 600k buffer as default which is large enough
1849 snd_pcm_lib_preallocate_pages_for_all(pcm,
1850 SNDRV_DMA_TYPE_DEV, NULL,
1851 HAD_DEFAULT_BUFFER, HAD_MAX_BUFFER);
1853 /* create controls */
1854 for (i = 0; i < ARRAY_SIZE(had_controls); i++) {
1855 struct snd_kcontrol *kctl;
1857 kctl = snd_ctl_new1(&had_controls[i], ctx);
1863 kctl->id.device = pcm->device;
1865 ret = snd_ctl_add(card, kctl);
1870 /* Register channel map controls */
1871 ret = had_register_chmap_ctls(ctx, pcm);
1875 ret = had_create_jack(ctx, pcm);
1880 ret = snd_card_register(card);
1884 spin_lock_irq(&pdata->lpe_audio_slock);
1885 pdata->notify_audio_lpe = notify_audio_lpe;
1886 spin_unlock_irq(&pdata->lpe_audio_slock);
1888 pm_runtime_use_autosuspend(&pdev->dev);
1889 pm_runtime_mark_last_busy(&pdev->dev);
1890 pm_runtime_set_active(&pdev->dev);
1892 dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
1893 for_each_port(card_ctx, port) {
1894 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
1896 schedule_work(&ctx->hdmi_audio_wq);
1902 snd_card_free(card);
1907 * hdmi_lpe_audio_remove - stop bridge with i915
1909 * This function is called when the platform device is destroyed.
1911 static int hdmi_lpe_audio_remove(struct platform_device *pdev)
1913 struct snd_intelhad_card *card_ctx = platform_get_drvdata(pdev);
1915 snd_card_free(card_ctx->card);
1919 static const struct dev_pm_ops hdmi_lpe_audio_pm = {
1920 SET_SYSTEM_SLEEP_PM_OPS(hdmi_lpe_audio_suspend, hdmi_lpe_audio_resume)
1921 SET_RUNTIME_PM_OPS(hdmi_lpe_audio_runtime_suspend,
1922 hdmi_lpe_audio_runtime_resume, NULL)
1925 static struct platform_driver hdmi_lpe_audio_driver = {
1927 .name = "hdmi-lpe-audio",
1928 .pm = &hdmi_lpe_audio_pm,
1930 .probe = hdmi_lpe_audio_probe,
1931 .remove = hdmi_lpe_audio_remove,
1934 module_platform_driver(hdmi_lpe_audio_driver);
1935 MODULE_ALIAS("platform:hdmi_lpe_audio");
1937 MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1938 MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1939 MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1940 MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1941 MODULE_DESCRIPTION("Intel HDMI Audio driver");
1942 MODULE_LICENSE("GPL v2");
1943 MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");