2 * intel_hdmi_audio.c - Intel HDMI audio driver
4 * Copyright (C) 2016 Intel Corp
5 * Authors: Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>
6 * Ramesh Babu K V <ramesh.babu@intel.com>
7 * Vaibhav Agarwal <vaibhav.agarwal@intel.com>
8 * Jerome Anand <jerome.anand@intel.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 * ALSA driver for Intel HDMI audio
24 #define pr_fmt(fmt) "had: " fmt
26 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <linux/module.h>
30 #include <linux/acpi.h>
31 #include <asm/cacheflush.h>
32 #include <sound/pcm.h>
33 #include <sound/core.h>
34 #include <sound/pcm_params.h>
35 #include <sound/initval.h>
36 #include <sound/control.h>
37 #include <sound/initval.h>
38 #include "intel_hdmi_audio.h"
40 static DEFINE_MUTEX(had_mutex
);
42 /*standard module options for ALSA. This module supports only one card*/
43 static int hdmi_card_index
= SNDRV_DEFAULT_IDX1
;
44 static char *hdmi_card_id
= SNDRV_DEFAULT_STR1
;
45 static struct snd_intelhad
*had_data
;
46 static int underrun_count
;
48 module_param_named(index
, hdmi_card_index
, int, 0444);
49 MODULE_PARM_DESC(index
,
50 "Index value for INTEL Intel HDMI Audio controller.");
51 module_param_named(id
, hdmi_card_id
, charp
, 0444);
53 "ID string for INTEL Intel HDMI Audio controller.");
56 * ELD SA bits in the CEA Speaker Allocation data block
58 static int eld_speaker_allocation_bits
[] = {
66 /* the following are not defined in ELD yet */
71 * This is an ordered list!
73 * The preceding ones have better chances to be selected by
74 * hdmi_channel_allocation().
76 static struct cea_channel_speaker_allocation channel_allocations
[] = {
77 /* channel: 7 6 5 4 3 2 1 0 */
78 { .ca_index
= 0x00, .speakers
= { 0, 0, 0, 0, 0, 0, FR
, FL
} },
80 { .ca_index
= 0x01, .speakers
= { 0, 0, 0, 0, 0, LFE
, FR
, FL
} },
82 { .ca_index
= 0x02, .speakers
= { 0, 0, 0, 0, FC
, 0, FR
, FL
} },
84 { .ca_index
= 0x08, .speakers
= { 0, 0, RR
, RL
, 0, 0, FR
, FL
} },
86 { .ca_index
= 0x09, .speakers
= { 0, 0, RR
, RL
, 0, LFE
, FR
, FL
} },
88 { .ca_index
= 0x0a, .speakers
= { 0, 0, RR
, RL
, FC
, 0, FR
, FL
} },
90 { .ca_index
= 0x0b, .speakers
= { 0, 0, RR
, RL
, FC
, LFE
, FR
, FL
} },
92 { .ca_index
= 0x0f, .speakers
= { 0, RC
, RR
, RL
, FC
, LFE
, FR
, FL
} },
94 { .ca_index
= 0x13, .speakers
= { RRC
, RLC
, RR
, RL
, FC
, LFE
, FR
, FL
} },
96 { .ca_index
= 0x03, .speakers
= { 0, 0, 0, 0, FC
, LFE
, FR
, FL
} },
97 { .ca_index
= 0x04, .speakers
= { 0, 0, 0, RC
, 0, 0, FR
, FL
} },
98 { .ca_index
= 0x05, .speakers
= { 0, 0, 0, RC
, 0, LFE
, FR
, FL
} },
99 { .ca_index
= 0x06, .speakers
= { 0, 0, 0, RC
, FC
, 0, FR
, FL
} },
100 { .ca_index
= 0x07, .speakers
= { 0, 0, 0, RC
, FC
, LFE
, FR
, FL
} },
101 { .ca_index
= 0x0c, .speakers
= { 0, RC
, RR
, RL
, 0, 0, FR
, FL
} },
102 { .ca_index
= 0x0d, .speakers
= { 0, RC
, RR
, RL
, 0, LFE
, FR
, FL
} },
103 { .ca_index
= 0x0e, .speakers
= { 0, RC
, RR
, RL
, FC
, 0, FR
, FL
} },
104 { .ca_index
= 0x10, .speakers
= { RRC
, RLC
, RR
, RL
, 0, 0, FR
, FL
} },
105 { .ca_index
= 0x11, .speakers
= { RRC
, RLC
, RR
, RL
, 0, LFE
, FR
, FL
} },
106 { .ca_index
= 0x12, .speakers
= { RRC
, RLC
, RR
, RL
, FC
, 0, FR
, FL
} },
107 { .ca_index
= 0x14, .speakers
= { FRC
, FLC
, 0, 0, 0, 0, FR
, FL
} },
108 { .ca_index
= 0x15, .speakers
= { FRC
, FLC
, 0, 0, 0, LFE
, FR
, FL
} },
109 { .ca_index
= 0x16, .speakers
= { FRC
, FLC
, 0, 0, FC
, 0, FR
, FL
} },
110 { .ca_index
= 0x17, .speakers
= { FRC
, FLC
, 0, 0, FC
, LFE
, FR
, FL
} },
111 { .ca_index
= 0x18, .speakers
= { FRC
, FLC
, 0, RC
, 0, 0, FR
, FL
} },
112 { .ca_index
= 0x19, .speakers
= { FRC
, FLC
, 0, RC
, 0, LFE
, FR
, FL
} },
113 { .ca_index
= 0x1a, .speakers
= { FRC
, FLC
, 0, RC
, FC
, 0, FR
, FL
} },
114 { .ca_index
= 0x1b, .speakers
= { FRC
, FLC
, 0, RC
, FC
, LFE
, FR
, FL
} },
115 { .ca_index
= 0x1c, .speakers
= { FRC
, FLC
, RR
, RL
, 0, 0, FR
, FL
} },
116 { .ca_index
= 0x1d, .speakers
= { FRC
, FLC
, RR
, RL
, 0, LFE
, FR
, FL
} },
117 { .ca_index
= 0x1e, .speakers
= { FRC
, FLC
, RR
, RL
, FC
, 0, FR
, FL
} },
118 { .ca_index
= 0x1f, .speakers
= { FRC
, FLC
, RR
, RL
, FC
, LFE
, FR
, FL
} },
121 static struct channel_map_table map_tables
[] = {
122 { SNDRV_CHMAP_FL
, 0x00, FL
},
123 { SNDRV_CHMAP_FR
, 0x01, FR
},
124 { SNDRV_CHMAP_RL
, 0x04, RL
},
125 { SNDRV_CHMAP_RR
, 0x05, RR
},
126 { SNDRV_CHMAP_LFE
, 0x02, LFE
},
127 { SNDRV_CHMAP_FC
, 0x03, FC
},
128 { SNDRV_CHMAP_RLC
, 0x06, RLC
},
129 { SNDRV_CHMAP_RRC
, 0x07, RRC
},
133 /* hardware capability structure */
134 static const struct snd_pcm_hardware snd_intel_hadstream
= {
135 .info
= (SNDRV_PCM_INFO_INTERLEAVED
|
136 SNDRV_PCM_INFO_DOUBLE
|
138 SNDRV_PCM_INFO_MMAP_VALID
|
139 SNDRV_PCM_INFO_BATCH
),
140 .formats
= (SNDRV_PCM_FMTBIT_S24
|
141 SNDRV_PCM_FMTBIT_U24
),
142 .rates
= SNDRV_PCM_RATE_32000
|
143 SNDRV_PCM_RATE_44100
|
144 SNDRV_PCM_RATE_48000
|
145 SNDRV_PCM_RATE_88200
|
146 SNDRV_PCM_RATE_96000
|
147 SNDRV_PCM_RATE_176400
|
148 SNDRV_PCM_RATE_192000
,
149 .rate_min
= HAD_MIN_RATE
,
150 .rate_max
= HAD_MAX_RATE
,
151 .channels_min
= HAD_MIN_CHANNEL
,
152 .channels_max
= HAD_MAX_CHANNEL
,
153 .buffer_bytes_max
= HAD_MAX_BUFFER
,
154 .period_bytes_min
= HAD_MIN_PERIOD_BYTES
,
155 .period_bytes_max
= HAD_MAX_PERIOD_BYTES
,
156 .periods_min
= HAD_MIN_PERIODS
,
157 .periods_max
= HAD_MAX_PERIODS
,
158 .fifo_size
= HAD_FIFO_SIZE
,
161 /* Register access functions */
163 int had_get_hwstate(struct snd_intelhad
*intelhaddata
)
165 /* Check for device presence -SW state */
166 if (intelhaddata
->drv_status
== HAD_DRV_DISCONNECTED
) {
167 pr_debug("%s:Device not connected:%d\n", __func__
,
168 intelhaddata
->drv_status
);
175 int had_get_caps(enum had_caps_list query
, void *caps
)
178 struct snd_intelhad
*intelhaddata
= had_data
;
180 retval
= had_get_hwstate(intelhaddata
);
182 retval
= intelhaddata
->query_ops
.hdmi_audio_get_caps(query
,
188 int had_set_caps(enum had_caps_list set_element
, void *caps
)
191 struct snd_intelhad
*intelhaddata
= had_data
;
193 retval
= had_get_hwstate(intelhaddata
);
195 retval
= intelhaddata
->query_ops
.hdmi_audio_set_caps(
201 int had_read_register(u32 offset
, u32
*data
)
204 struct snd_intelhad
*intelhaddata
= had_data
;
206 retval
= had_get_hwstate(intelhaddata
);
208 retval
= intelhaddata
->reg_ops
.hdmi_audio_read_register(
209 offset
+ intelhaddata
->audio_cfg_offset
, data
);
214 int had_write_register(u32 offset
, u32 data
)
217 struct snd_intelhad
*intelhaddata
= had_data
;
219 retval
= had_get_hwstate(intelhaddata
);
221 retval
= intelhaddata
->reg_ops
.hdmi_audio_write_register(
222 offset
+ intelhaddata
->audio_cfg_offset
, data
);
227 int had_read_modify(u32 offset
, u32 data
, u32 mask
)
230 struct snd_intelhad
*intelhaddata
= had_data
;
232 retval
= had_get_hwstate(intelhaddata
);
234 retval
= intelhaddata
->reg_ops
.hdmi_audio_read_modify(
235 offset
+ intelhaddata
->audio_cfg_offset
,
241 * function to read-modify
242 * AUD_CONFIG register on VLV2.The had_read_modify() function should not
243 * directly be used on VLV2 for updating AUD_CONFIG register.
245 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
246 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
247 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
248 * register. This field should be 1xy binary for configuration with 6 or
249 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
250 * causes the "channels" field to be updated as 0xy binary resulting in
251 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
252 * appropriate value when doing read-modify of AUD_CONFIG register.
254 * @substream: the current substream or NULL if no active substream
255 * @data : data to be written
259 static int had_read_modify_aud_config_v2(struct snd_pcm_substream
*substream
,
262 union aud_cfg cfg_val
= {.cfg_regval
= 0};
266 * If substream is NULL, there is no active stream.
267 * In this case just set channels to 2
270 channels
= substream
->runtime
->channels
;
273 cfg_val
.cfg_regx_v2
.num_ch
= channels
- 2;
275 data
= data
| cfg_val
.cfg_regval
;
276 mask
= mask
| AUD_CONFIG_CH_MASK_V2
;
278 pr_debug("%s : data = %x, mask =%x\n", __func__
, data
, mask
);
280 return had_read_modify(AUD_CONFIG
, data
, mask
);
283 static void snd_intelhad_enable_audio_v1(struct snd_pcm_substream
*substream
,
286 had_read_modify(AUD_CONFIG
, enable
, BIT(0));
289 static void snd_intelhad_enable_audio_v2(struct snd_pcm_substream
*substream
,
292 had_read_modify_aud_config_v2(substream
, enable
, BIT(0));
295 static void snd_intelhad_reset_audio_v1(u8 reset
)
297 had_write_register(AUD_HDMI_STATUS
, reset
);
300 static void snd_intelhad_reset_audio_v2(u8 reset
)
302 had_write_register(AUD_HDMI_STATUS_v2
, reset
);
306 * initialize audio channel status registers
307 * This function is called in the prepare callback
309 static int had_prog_status_reg(struct snd_pcm_substream
*substream
,
310 struct snd_intelhad
*intelhaddata
)
312 union aud_cfg cfg_val
= {.cfg_regval
= 0};
313 union aud_ch_status_0 ch_stat0
= {.status_0_regval
= 0};
314 union aud_ch_status_1 ch_stat1
= {.status_1_regval
= 0};
317 pr_debug("Entry %s\n", __func__
);
319 ch_stat0
.status_0_regx
.lpcm_id
= (intelhaddata
->aes_bits
&
320 IEC958_AES0_NONAUDIO
)>>1;
321 ch_stat0
.status_0_regx
.clk_acc
= (intelhaddata
->aes_bits
&
322 IEC958_AES3_CON_CLOCK
)>>4;
323 cfg_val
.cfg_regx
.val_bit
= ch_stat0
.status_0_regx
.lpcm_id
;
325 switch (substream
->runtime
->rate
) {
326 case AUD_SAMPLE_RATE_32
:
327 ch_stat0
.status_0_regx
.samp_freq
= CH_STATUS_MAP_32KHZ
;
330 case AUD_SAMPLE_RATE_44_1
:
331 ch_stat0
.status_0_regx
.samp_freq
= CH_STATUS_MAP_44KHZ
;
333 case AUD_SAMPLE_RATE_48
:
334 ch_stat0
.status_0_regx
.samp_freq
= CH_STATUS_MAP_48KHZ
;
336 case AUD_SAMPLE_RATE_88_2
:
337 ch_stat0
.status_0_regx
.samp_freq
= CH_STATUS_MAP_88KHZ
;
339 case AUD_SAMPLE_RATE_96
:
340 ch_stat0
.status_0_regx
.samp_freq
= CH_STATUS_MAP_96KHZ
;
342 case AUD_SAMPLE_RATE_176_4
:
343 ch_stat0
.status_0_regx
.samp_freq
= CH_STATUS_MAP_176KHZ
;
345 case AUD_SAMPLE_RATE_192
:
346 ch_stat0
.status_0_regx
.samp_freq
= CH_STATUS_MAP_192KHZ
;
350 /* control should never come here */
355 had_write_register(AUD_CH_STATUS_0
, ch_stat0
.status_0_regval
);
357 format
= substream
->runtime
->format
;
359 if (format
== SNDRV_PCM_FORMAT_S16_LE
) {
360 ch_stat1
.status_1_regx
.max_wrd_len
= MAX_SMPL_WIDTH_20
;
361 ch_stat1
.status_1_regx
.wrd_len
= SMPL_WIDTH_16BITS
;
362 } else if (format
== SNDRV_PCM_FORMAT_S24_LE
) {
363 ch_stat1
.status_1_regx
.max_wrd_len
= MAX_SMPL_WIDTH_24
;
364 ch_stat1
.status_1_regx
.wrd_len
= SMPL_WIDTH_24BITS
;
366 ch_stat1
.status_1_regx
.max_wrd_len
= 0;
367 ch_stat1
.status_1_regx
.wrd_len
= 0;
369 had_write_register(AUD_CH_STATUS_1
, ch_stat1
.status_1_regval
);
374 * function to initialize audio
375 * registers and buffer confgiuration registers
376 * This function is called in the prepare callback
378 static int snd_intelhad_prog_audio_ctrl_v2(struct snd_pcm_substream
*substream
,
379 struct snd_intelhad
*intelhaddata
)
381 union aud_cfg cfg_val
= {.cfg_regval
= 0};
382 union aud_buf_config buf_cfg
= {.buf_cfgval
= 0};
385 had_prog_status_reg(substream
, intelhaddata
);
387 buf_cfg
.buf_cfg_regx_v2
.audio_fifo_watermark
= FIFO_THRESHOLD
;
388 buf_cfg
.buf_cfg_regx_v2
.dma_fifo_watermark
= DMA_FIFO_THRESHOLD
;
389 buf_cfg
.buf_cfg_regx_v2
.aud_delay
= 0;
390 had_write_register(AUD_BUF_CONFIG
, buf_cfg
.buf_cfgval
);
392 channels
= substream
->runtime
->channels
;
393 cfg_val
.cfg_regx_v2
.num_ch
= channels
- 2;
395 cfg_val
.cfg_regx_v2
.layout
= LAYOUT0
;
397 cfg_val
.cfg_regx_v2
.layout
= LAYOUT1
;
399 cfg_val
.cfg_regx_v2
.val_bit
= 1;
400 had_write_register(AUD_CONFIG
, cfg_val
.cfg_regval
);
405 * function to initialize audio
406 * registers and buffer confgiuration registers
407 * This function is called in the prepare callback
409 static int snd_intelhad_prog_audio_ctrl_v1(struct snd_pcm_substream
*substream
,
410 struct snd_intelhad
*intelhaddata
)
412 union aud_cfg cfg_val
= {.cfg_regval
= 0};
413 union aud_buf_config buf_cfg
= {.buf_cfgval
= 0};
416 had_prog_status_reg(substream
, intelhaddata
);
418 buf_cfg
.buf_cfg_regx
.fifo_width
= FIFO_THRESHOLD
;
419 buf_cfg
.buf_cfg_regx
.aud_delay
= 0;
420 had_write_register(AUD_BUF_CONFIG
, buf_cfg
.buf_cfgval
);
422 channels
= substream
->runtime
->channels
;
427 cfg_val
.cfg_regx
.num_ch
= CH_STEREO
;
428 cfg_val
.cfg_regx
.layout
= LAYOUT0
;
433 cfg_val
.cfg_regx
.num_ch
= CH_THREE_FOUR
;
434 cfg_val
.cfg_regx
.layout
= LAYOUT1
;
439 cfg_val
.cfg_regx
.num_ch
= CH_FIVE_SIX
;
440 cfg_val
.cfg_regx
.layout
= LAYOUT1
;
445 cfg_val
.cfg_regx
.num_ch
= CH_SEVEN_EIGHT
;
446 cfg_val
.cfg_regx
.layout
= LAYOUT1
;
451 cfg_val
.cfg_regx
.val_bit
= 1;
452 had_write_register(AUD_CONFIG
, cfg_val
.cfg_regval
);
457 * Compute derived values in channel_allocations[].
459 static void init_channel_allocations(void)
462 struct cea_channel_speaker_allocation
*p
;
464 pr_debug("%s: Enter\n", __func__
);
466 for (i
= 0; i
< ARRAY_SIZE(channel_allocations
); i
++) {
467 p
= channel_allocations
+ i
;
470 for (j
= 0; j
< ARRAY_SIZE(p
->speakers
); j
++)
471 if (p
->speakers
[j
]) {
473 p
->spk_mask
|= p
->speakers
[j
];
479 * The transformation takes two steps:
481 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
482 * spk_mask => (channel_allocations[]) => ai->CA
484 * TODO: it could select the wrong CA from multiple candidates.
486 static int snd_intelhad_channel_allocation(struct snd_intelhad
*intelhaddata
,
494 * CA defaults to 0 for basic stereo audio
500 * expand ELD's speaker allocation mask
502 * ELD tells the speaker mask in a compact(paired) form,
503 * expand ELD's notions to match the ones used by Audio InfoFrame.
506 for (i
= 0; i
< ARRAY_SIZE(eld_speaker_allocation_bits
); i
++) {
507 if (intelhaddata
->eeld
.speaker_allocation_block
& (1 << i
))
508 spk_mask
|= eld_speaker_allocation_bits
[i
];
511 /* search for the first working match in the CA table */
512 for (i
= 0; i
< ARRAY_SIZE(channel_allocations
); i
++) {
513 if (channels
== channel_allocations
[i
].channels
&&
514 (spk_mask
& channel_allocations
[i
].spk_mask
) ==
515 channel_allocations
[i
].spk_mask
) {
516 ca
= channel_allocations
[i
].ca_index
;
521 pr_debug("HDMI: select CA 0x%x for %d\n", ca
, channels
);
526 /* from speaker bit mask to ALSA API channel position */
527 static int spk_to_chmap(int spk
)
529 struct channel_map_table
*t
= map_tables
;
531 for (; t
->map
; t
++) {
532 if (t
->spk_mask
== spk
)
538 void had_build_channel_allocation_map(struct snd_intelhad
*intelhaddata
)
542 struct snd_pcm_chmap_elem
*chmap
;
543 u8 eld_high
, eld_high_mask
= 0xF0;
546 chmap
= kzalloc(sizeof(*chmap
), GFP_KERNEL
);
548 intelhaddata
->chmap
->chmap
= NULL
;
552 had_get_caps(HAD_GET_ELD
, &intelhaddata
->eeld
);
553 had_get_caps(HAD_GET_DP_OUTPUT
, &intelhaddata
->dp_output
);
555 pr_debug("eeld.speaker_allocation_block = %x\n",
556 intelhaddata
->eeld
.speaker_allocation_block
);
558 /* WA: Fix the max channel supported to 8 */
561 * Sink may support more than 8 channels, if eld_high has more than
562 * one bit set. SOC supports max 8 channels.
563 * Refer eld_speaker_allocation_bits, for sink speaker allocation
566 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
567 eld_high
= intelhaddata
->eeld
.speaker_allocation_block
& eld_high_mask
;
568 if ((eld_high
& (eld_high
-1)) && (eld_high
> 0x1F)) {
569 /* eld_high & (eld_high-1): if more than 1 bit set */
570 /* 0x1F: 7 channels */
571 for (i
= 1; i
< 4; i
++) {
572 high_msb
= eld_high
& (0x80 >> i
);
574 intelhaddata
->eeld
.speaker_allocation_block
&=
581 for (i
= 0; i
< ARRAY_SIZE(eld_speaker_allocation_bits
); i
++) {
582 if (intelhaddata
->eeld
.speaker_allocation_block
& (1 << i
))
583 spk_mask
|= eld_speaker_allocation_bits
[i
];
586 for (i
= 0; i
< ARRAY_SIZE(channel_allocations
); i
++) {
587 if (spk_mask
== channel_allocations
[i
].spk_mask
) {
588 for (c
= 0; c
< channel_allocations
[i
].channels
; c
++) {
589 chmap
->map
[c
] = spk_to_chmap(
590 channel_allocations
[i
].speakers
[
591 (MAX_SPEAKERS
- 1)-c
]);
593 chmap
->channels
= channel_allocations
[i
].channels
;
594 intelhaddata
->chmap
->chmap
= chmap
;
598 if (i
>= ARRAY_SIZE(channel_allocations
)) {
599 intelhaddata
->chmap
->chmap
= NULL
;
605 * ALSA API channel-map control callbacks
607 static int had_chmap_ctl_info(struct snd_kcontrol
*kcontrol
,
608 struct snd_ctl_elem_info
*uinfo
)
610 struct snd_pcm_chmap
*info
= snd_kcontrol_chip(kcontrol
);
611 struct snd_intelhad
*intelhaddata
= info
->private_data
;
613 if (intelhaddata
->drv_status
== HAD_DRV_DISCONNECTED
)
615 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
616 uinfo
->count
= HAD_MAX_CHANNEL
;
617 uinfo
->value
.integer
.min
= 0;
618 uinfo
->value
.integer
.max
= SNDRV_CHMAP_LAST
;
622 static int had_chmap_ctl_get(struct snd_kcontrol
*kcontrol
,
623 struct snd_ctl_elem_value
*ucontrol
)
625 struct snd_pcm_chmap
*info
= snd_kcontrol_chip(kcontrol
);
626 struct snd_intelhad
*intelhaddata
= info
->private_data
;
628 const struct snd_pcm_chmap_elem
*chmap
;
630 if (intelhaddata
->drv_status
== HAD_DRV_DISCONNECTED
)
632 if (intelhaddata
->chmap
->chmap
== NULL
)
634 chmap
= intelhaddata
->chmap
->chmap
;
635 for (i
= 0; i
< chmap
->channels
; i
++) {
636 ucontrol
->value
.integer
.value
[i
] = chmap
->map
[i
];
637 pr_debug("chmap->map[%d] = %d\n", i
, chmap
->map
[i
]);
643 static int had_register_chmap_ctls(struct snd_intelhad
*intelhaddata
,
648 err
= snd_pcm_add_chmap_ctls(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
649 NULL
, 0, (unsigned long)intelhaddata
,
650 &intelhaddata
->chmap
);
654 intelhaddata
->chmap
->private_data
= intelhaddata
;
655 intelhaddata
->kctl
= intelhaddata
->chmap
->kctl
;
656 intelhaddata
->kctl
->info
= had_chmap_ctl_info
;
657 intelhaddata
->kctl
->get
= had_chmap_ctl_get
;
658 intelhaddata
->chmap
->chmap
= NULL
;
663 * snd_intelhad_prog_dip_v1 - to initialize Data Island Packets registers
665 * @substream:substream for which the prepare function is called
666 * @intelhaddata:substream private data
668 * This function is called in the prepare callback
670 static void snd_intelhad_prog_dip_v1(struct snd_pcm_substream
*substream
,
671 struct snd_intelhad
*intelhaddata
)
674 union aud_ctrl_st ctrl_state
= {.ctrl_val
= 0};
675 union aud_info_frame2 frame2
= {.fr2_val
= 0};
676 union aud_info_frame3 frame3
= {.fr3_val
= 0};
680 channels
= substream
->runtime
->channels
;
682 had_write_register(AUD_CNTL_ST
, ctrl_state
.ctrl_val
);
684 frame2
.fr2_regx
.chnl_cnt
= substream
->runtime
->channels
- 1;
686 frame3
.fr3_regx
.chnl_alloc
= snd_intelhad_channel_allocation(
687 intelhaddata
, channels
);
689 /*Calculte the byte wide checksum for all valid DIP words*/
690 for (i
= 0; i
< BYTES_PER_WORD
; i
++)
691 checksum
+= (HDMI_INFO_FRAME_WORD1
>> i
*BITS_PER_BYTE
) & MASK_BYTE0
;
692 for (i
= 0; i
< BYTES_PER_WORD
; i
++)
693 checksum
+= (frame2
.fr2_val
>> i
*BITS_PER_BYTE
) & MASK_BYTE0
;
694 for (i
= 0; i
< BYTES_PER_WORD
; i
++)
695 checksum
+= (frame3
.fr3_val
>> i
*BITS_PER_BYTE
) & MASK_BYTE0
;
697 frame2
.fr2_regx
.chksum
= -(checksum
);
699 had_write_register(AUD_HDMIW_INFOFR
, HDMI_INFO_FRAME_WORD1
);
700 had_write_register(AUD_HDMIW_INFOFR
, frame2
.fr2_val
);
701 had_write_register(AUD_HDMIW_INFOFR
, frame3
.fr3_val
);
703 /* program remaining DIP words with zero */
704 for (i
= 0; i
< HAD_MAX_DIP_WORDS
-VALID_DIP_WORDS
; i
++)
705 had_write_register(AUD_HDMIW_INFOFR
, 0x0);
707 ctrl_state
.ctrl_regx
.dip_freq
= 1;
708 ctrl_state
.ctrl_regx
.dip_en_sta
= 1;
709 had_write_register(AUD_CNTL_ST
, ctrl_state
.ctrl_val
);
713 * snd_intelhad_prog_dip_v2 - to initialize Data Island Packets registers
715 * @substream:substream for which the prepare function is called
716 * @intelhaddata:substream private data
718 * This function is called in the prepare callback
720 static void snd_intelhad_prog_dip_v2(struct snd_pcm_substream
*substream
,
721 struct snd_intelhad
*intelhaddata
)
724 union aud_ctrl_st ctrl_state
= {.ctrl_val
= 0};
725 union aud_info_frame2 frame2
= {.fr2_val
= 0};
726 union aud_info_frame3 frame3
= {.fr3_val
= 0};
731 channels
= substream
->runtime
->channels
;
733 had_write_register(AUD_CNTL_ST
, ctrl_state
.ctrl_val
);
735 if (intelhaddata
->dp_output
) {
736 info_frame
= DP_INFO_FRAME_WORD1
;
739 info_frame
= HDMI_INFO_FRAME_WORD1
;
740 frame2
.fr2_regx
.chnl_cnt
= substream
->runtime
->channels
- 1;
742 frame3
.fr3_regx
.chnl_alloc
= snd_intelhad_channel_allocation(
743 intelhaddata
, channels
);
745 /*Calculte the byte wide checksum for all valid DIP words*/
746 for (i
= 0; i
< BYTES_PER_WORD
; i
++)
747 checksum
+= (info_frame
>> i
*BITS_PER_BYTE
) & MASK_BYTE0
;
748 for (i
= 0; i
< BYTES_PER_WORD
; i
++)
749 checksum
+= (frame2
.fr2_val
>> i
*BITS_PER_BYTE
) & MASK_BYTE0
;
750 for (i
= 0; i
< BYTES_PER_WORD
; i
++)
751 checksum
+= (frame3
.fr3_val
>> i
*BITS_PER_BYTE
) & MASK_BYTE0
;
753 frame2
.fr2_regx
.chksum
= -(checksum
);
756 had_write_register(AUD_HDMIW_INFOFR_v2
, info_frame
);
757 had_write_register(AUD_HDMIW_INFOFR_v2
, frame2
.fr2_val
);
758 had_write_register(AUD_HDMIW_INFOFR_v2
, frame3
.fr3_val
);
760 /* program remaining DIP words with zero */
761 for (i
= 0; i
< HAD_MAX_DIP_WORDS
-VALID_DIP_WORDS
; i
++)
762 had_write_register(AUD_HDMIW_INFOFR_v2
, 0x0);
764 ctrl_state
.ctrl_regx
.dip_freq
= 1;
765 ctrl_state
.ctrl_regx
.dip_en_sta
= 1;
766 had_write_register(AUD_CNTL_ST
, ctrl_state
.ctrl_val
);
770 * snd_intelhad_prog_buffer - programs buffer
771 * address and length registers
773 * @substream:substream for which the prepare function is called
774 * @intelhaddata:substream private data
776 * This function programs ring buffer address and length into registers.
778 int snd_intelhad_prog_buffer(struct snd_intelhad
*intelhaddata
,
781 u32 ring_buf_addr
, ring_buf_size
, period_bytes
;
783 struct snd_pcm_substream
*substream
;
785 substream
= intelhaddata
->stream_info
.had_substream
;
787 pr_err("substream is NULL\n");
792 ring_buf_addr
= substream
->runtime
->dma_addr
;
793 ring_buf_size
= snd_pcm_lib_buffer_bytes(substream
);
794 intelhaddata
->stream_info
.ring_buf_size
= ring_buf_size
;
795 period_bytes
= frames_to_bytes(substream
->runtime
,
796 substream
->runtime
->period_size
);
797 num_periods
= substream
->runtime
->periods
;
800 * buffer addr should be 64 byte aligned, period bytes
801 * will be used to calculate addr offset
803 period_bytes
&= ~0x3F;
805 /* Hardware supports MAX_PERIODS buffers */
806 if (end
>= HAD_MAX_PERIODS
)
809 for (i
= start
; i
<= end
; i
++) {
810 /* Program the buf registers with addr and len */
811 intelhaddata
->buf_info
[i
].buf_addr
= ring_buf_addr
+
813 if (i
< num_periods
-1)
814 intelhaddata
->buf_info
[i
].buf_size
= period_bytes
;
816 intelhaddata
->buf_info
[i
].buf_size
= ring_buf_size
-
819 had_write_register(AUD_BUF_A_ADDR
+ (i
* HAD_REG_WIDTH
),
820 intelhaddata
->buf_info
[i
].buf_addr
|
822 had_write_register(AUD_BUF_A_LENGTH
+ (i
* HAD_REG_WIDTH
),
824 intelhaddata
->buf_info
[i
].is_valid
= true;
826 pr_debug("%s:buf[%d-%d] addr=%#x and size=%d\n", __func__
, start
, end
,
827 intelhaddata
->buf_info
[start
].buf_addr
,
828 intelhaddata
->buf_info
[start
].buf_size
);
829 intelhaddata
->valid_buf_cnt
= num_periods
;
833 int snd_intelhad_read_len(struct snd_intelhad
*intelhaddata
)
838 for (i
= 0; i
< 4 ; i
++) {
839 had_read_register(AUD_BUF_A_LENGTH
+ (i
* HAD_REG_WIDTH
),
845 for (i
= 0; i
< 4 ; i
++)
846 pr_debug("buf[%d] size=%d\n", i
, len
[i
]);
852 static int had_calculate_maud_value(u32 aud_samp_freq
, u32 link_rate
)
856 /* Select maud according to DP 1.2 spec*/
857 if (link_rate
== DP_2_7_GHZ
) {
858 switch (aud_samp_freq
) {
859 case AUD_SAMPLE_RATE_32
:
860 maud_val
= AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL
;
863 case AUD_SAMPLE_RATE_44_1
:
864 maud_val
= AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL
;
867 case AUD_SAMPLE_RATE_48
:
868 maud_val
= AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL
;
871 case AUD_SAMPLE_RATE_88_2
:
872 maud_val
= AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL
;
875 case AUD_SAMPLE_RATE_96
:
876 maud_val
= AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL
;
879 case AUD_SAMPLE_RATE_176_4
:
880 maud_val
= AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL
;
884 maud_val
= HAD_MAX_RATE_DP_2_7_MAUD_VAL
;
891 } else if (link_rate
== DP_1_62_GHZ
) {
892 switch (aud_samp_freq
) {
893 case AUD_SAMPLE_RATE_32
:
894 maud_val
= AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL
;
897 case AUD_SAMPLE_RATE_44_1
:
898 maud_val
= AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL
;
901 case AUD_SAMPLE_RATE_48
:
902 maud_val
= AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL
;
905 case AUD_SAMPLE_RATE_88_2
:
906 maud_val
= AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL
;
909 case AUD_SAMPLE_RATE_96
:
910 maud_val
= AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL
;
913 case AUD_SAMPLE_RATE_176_4
:
914 maud_val
= AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL
;
918 maud_val
= HAD_MAX_RATE_DP_1_62_MAUD_VAL
;
932 * snd_intelhad_prog_cts_v1 - Program HDMI audio CTS value
934 * @aud_samp_freq: sampling frequency of audio data
935 * @tmds: sampling frequency of the display data
936 * @n_param: N value, depends on aud_samp_freq
937 * @intelhaddata:substream private data
939 * Program CTS register based on the audio and display sampling frequency
941 static void snd_intelhad_prog_cts_v1(u32 aud_samp_freq
, u32 tmds
,
942 u32 link_rate
, u32 n_param
,
943 struct snd_intelhad
*intelhaddata
)
946 u64 dividend
, divisor
;
948 /* Calculate CTS according to HDMI 1.3a spec*/
949 dividend
= (u64
)tmds
* n_param
*1000;
950 divisor
= 128 * aud_samp_freq
;
951 cts_val
= div64_u64(dividend
, divisor
);
952 pr_debug("TMDS value=%d, N value=%d, CTS Value=%d\n",
953 tmds
, n_param
, cts_val
);
954 had_write_register(AUD_HDMI_CTS
, (BIT(20) | cts_val
));
958 * snd_intelhad_prog_cts_v2 - Program HDMI audio CTS value
960 * @aud_samp_freq: sampling frequency of audio data
961 * @tmds: sampling frequency of the display data
962 * @n_param: N value, depends on aud_samp_freq
963 * @intelhaddata:substream private data
965 * Program CTS register based on the audio and display sampling frequency
967 static void snd_intelhad_prog_cts_v2(u32 aud_samp_freq
, u32 tmds
,
968 u32 link_rate
, u32 n_param
,
969 struct snd_intelhad
*intelhaddata
)
972 u64 dividend
, divisor
;
974 if (intelhaddata
->dp_output
) {
975 /* Substitute cts_val with Maud according to DP 1.2 spec*/
976 cts_val
= had_calculate_maud_value(aud_samp_freq
, link_rate
);
978 /* Calculate CTS according to HDMI 1.3a spec*/
979 dividend
= (u64
)tmds
* n_param
*1000;
980 divisor
= 128 * aud_samp_freq
;
981 cts_val
= div64_u64(dividend
, divisor
);
983 pr_debug("TMDS value=%d, N value=%d, CTS Value=%d\n",
984 tmds
, n_param
, cts_val
);
985 had_write_register(AUD_HDMI_CTS
, (BIT(24) | cts_val
));
988 static int had_calculate_n_value(u32 aud_samp_freq
)
992 /* Select N according to HDMI 1.3a spec*/
993 switch (aud_samp_freq
) {
994 case AUD_SAMPLE_RATE_32
:
998 case AUD_SAMPLE_RATE_44_1
:
1002 case AUD_SAMPLE_RATE_48
:
1006 case AUD_SAMPLE_RATE_88_2
:
1010 case AUD_SAMPLE_RATE_96
:
1014 case AUD_SAMPLE_RATE_176_4
:
1030 * snd_intelhad_prog_n_v1 - Program HDMI audio N value
1032 * @aud_samp_freq: sampling frequency of audio data
1033 * @n_param: N value, depends on aud_samp_freq
1034 * @intelhaddata:substream private data
1036 * This function is called in the prepare callback.
1037 * It programs based on the audio and display sampling frequency
1039 static int snd_intelhad_prog_n_v1(u32 aud_samp_freq
, u32
*n_param
,
1040 struct snd_intelhad
*intelhaddata
)
1044 n_val
= had_calculate_n_value(aud_samp_freq
);
1049 had_write_register(AUD_N_ENABLE
, (BIT(20) | n_val
));
1055 * snd_intelhad_prog_n_v2 - Program HDMI audio N value
1057 * @aud_samp_freq: sampling frequency of audio data
1058 * @n_param: N value, depends on aud_samp_freq
1059 * @intelhaddata:substream private data
1061 * This function is called in the prepare callback.
1062 * It programs based on the audio and display sampling frequency
1064 static int snd_intelhad_prog_n_v2(u32 aud_samp_freq
, u32
*n_param
,
1065 struct snd_intelhad
*intelhaddata
)
1069 if (intelhaddata
->dp_output
) {
1071 * According to DP specs, Maud and Naud values hold
1072 * a relationship, which is stated as:
1073 * Maud/Naud = 512 * fs / f_LS_Clk
1074 * where, fs is the sampling frequency of the audio stream
1075 * and Naud is 32768 for Async clock.
1078 n_val
= DP_NAUD_VAL
;
1080 n_val
= had_calculate_n_value(aud_samp_freq
);
1085 had_write_register(AUD_N_ENABLE
, (BIT(24) | n_val
));
1090 static void had_clear_underrun_intr_v1(struct snd_intelhad
*intelhaddata
)
1092 u32 hdmi_status
, i
= 0;
1094 /* Handle Underrun interrupt within Audio Unit */
1095 had_write_register(AUD_CONFIG
, 0);
1096 /* Reset buffer pointers */
1097 had_write_register(AUD_HDMI_STATUS
, 1);
1098 had_write_register(AUD_HDMI_STATUS
, 0);
1100 * The interrupt status 'sticky' bits might not be cleared by
1101 * setting '1' to that bit once...
1103 do { /* clear bit30, 31 AUD_HDMI_STATUS */
1104 had_read_register(AUD_HDMI_STATUS
, &hdmi_status
);
1105 pr_debug("HDMI status =0x%x\n", hdmi_status
);
1106 if (hdmi_status
& AUD_CONFIG_MASK_UNDERRUN
) {
1108 hdmi_status
&= (AUD_CONFIG_MASK_SRDBG
|
1109 AUD_CONFIG_MASK_FUNCRST
);
1110 hdmi_status
|= ~AUD_CONFIG_MASK_UNDERRUN
;
1111 had_write_register(AUD_HDMI_STATUS
, hdmi_status
);
1114 } while (i
< MAX_CNT
);
1116 pr_err("Unable to clear UNDERRUN bits\n");
1119 static void had_clear_underrun_intr_v2(struct snd_intelhad
*intelhaddata
)
1121 u32 hdmi_status
, i
= 0;
1123 /* Handle Underrun interrupt within Audio Unit */
1124 had_write_register(AUD_CONFIG
, 0);
1125 /* Reset buffer pointers */
1126 had_write_register(AUD_HDMI_STATUS_v2
, 1);
1127 had_write_register(AUD_HDMI_STATUS_v2
, 0);
1129 * The interrupt status 'sticky' bits might not be cleared by
1130 * setting '1' to that bit once...
1132 do { /* clear bit30, 31 AUD_HDMI_STATUS */
1133 had_read_register(AUD_HDMI_STATUS_v2
, &hdmi_status
);
1134 pr_debug("HDMI status =0x%x\n", hdmi_status
);
1135 if (hdmi_status
& AUD_CONFIG_MASK_UNDERRUN
) {
1137 had_write_register(AUD_HDMI_STATUS_v2
, hdmi_status
);
1140 } while (i
< MAX_CNT
);
1142 pr_err("Unable to clear UNDERRUN bits\n");
1146 * snd_intelhad_open - stream initializations are done here
1147 * @substream:substream for which the stream function is called
1149 * This function is called whenever a PCM stream is opened
1151 static int snd_intelhad_open(struct snd_pcm_substream
*substream
)
1153 struct snd_intelhad
*intelhaddata
;
1154 struct snd_pcm_runtime
*runtime
;
1155 struct had_stream_pvt
*stream
;
1156 struct had_pvt_data
*had_stream
;
1159 pr_debug("snd_intelhad_open called\n");
1160 intelhaddata
= snd_pcm_substream_chip(substream
);
1161 had_stream
= intelhaddata
->private_data
;
1162 runtime
= substream
->runtime
;
1165 pm_runtime_get(intelhaddata
->dev
);
1167 if (had_get_hwstate(intelhaddata
)) {
1168 pr_err("%s: HDMI cable plugged-out\n", __func__
);
1170 goto exit_put_handle
;
1173 /* Check, if device already in use */
1174 if (runtime
->private_data
) {
1175 pr_err("Device already in use\n");
1177 goto exit_put_handle
;
1180 /* set the runtime hw parameter with local snd_pcm_hardware struct */
1181 runtime
->hw
= snd_intel_hadstream
;
1183 stream
= kzalloc(sizeof(*stream
), GFP_KERNEL
);
1186 goto exit_put_handle
;
1188 stream
->stream_status
= STREAM_INIT
;
1189 runtime
->private_data
= stream
;
1191 retval
= snd_pcm_hw_constraint_integer(runtime
,
1192 SNDRV_PCM_HW_PARAM_PERIODS
);
1196 /* Make sure, that the period size is always aligned
1199 retval
= snd_pcm_hw_constraint_step(substream
->runtime
, 0,
1200 SNDRV_PCM_HW_PARAM_PERIOD_BYTES
, 64);
1202 pr_err("%s:step_size=64 failed,err=%d\n", __func__
, retval
);
1210 pm_runtime_put(intelhaddata
->dev
);
1211 runtime
->private_data
= NULL
;
1216 * had_period_elapsed - updates the hardware pointer status
1217 * @had_substream:substream for which the stream function is called
1220 static void had_period_elapsed(void *had_substream
)
1222 struct snd_pcm_substream
*substream
= had_substream
;
1223 struct had_stream_pvt
*stream
;
1225 /* pr_debug("had_period_elapsed called\n"); */
1227 if (!substream
|| !substream
->runtime
)
1229 stream
= substream
->runtime
->private_data
;
1233 if (stream
->stream_status
!= STREAM_RUNNING
)
1235 snd_pcm_period_elapsed(substream
);
1239 * snd_intelhad_init_stream - internal function to initialize stream info
1240 * @substream:substream for which the stream function is called
1243 static int snd_intelhad_init_stream(struct snd_pcm_substream
*substream
)
1245 struct snd_intelhad
*intelhaddata
= snd_pcm_substream_chip(substream
);
1247 pr_debug("snd_intelhad_init_stream called\n");
1249 pr_debug("setting buffer ptr param\n");
1250 intelhaddata
->stream_info
.period_elapsed
= had_period_elapsed
;
1251 intelhaddata
->stream_info
.had_substream
= substream
;
1252 intelhaddata
->stream_info
.buffer_ptr
= 0;
1253 intelhaddata
->stream_info
.buffer_rendered
= 0;
1254 intelhaddata
->stream_info
.sfreq
= substream
->runtime
->rate
;
1259 * snd_intelhad_close- to free parameteres when stream is stopped
1261 * @substream: substream for which the function is called
1263 * This function is called by ALSA framework when stream is stopped
1265 static int snd_intelhad_close(struct snd_pcm_substream
*substream
)
1267 struct snd_intelhad
*intelhaddata
;
1268 struct snd_pcm_runtime
*runtime
;
1270 pr_debug("snd_intelhad_close called\n");
1272 intelhaddata
= snd_pcm_substream_chip(substream
);
1273 runtime
= substream
->runtime
;
1275 if (!runtime
->private_data
) {
1276 pr_debug("close() might have called after failed open");
1280 intelhaddata
->stream_info
.buffer_rendered
= 0;
1281 intelhaddata
->stream_info
.buffer_ptr
= 0;
1282 intelhaddata
->stream_info
.str_id
= 0;
1283 intelhaddata
->stream_info
.had_substream
= NULL
;
1285 /* Check if following drv_status modification is required - VA */
1286 if (intelhaddata
->drv_status
!= HAD_DRV_DISCONNECTED
) {
1287 intelhaddata
->drv_status
= HAD_DRV_CONNECTED
;
1288 pr_debug("%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
1289 __func__
, __LINE__
);
1291 kfree(runtime
->private_data
);
1292 runtime
->private_data
= NULL
;
1293 pm_runtime_put(intelhaddata
->dev
);
1298 * snd_intelhad_hw_params- to setup the hardware parameters
1299 * like allocating the buffers
1301 * @substream: substream for which the function is called
1302 * @hw_params: hardware parameters
1304 * This function is called by ALSA framework when hardware params are set
1306 static int snd_intelhad_hw_params(struct snd_pcm_substream
*substream
,
1307 struct snd_pcm_hw_params
*hw_params
)
1310 int pages
, buf_size
, retval
;
1312 pr_debug("snd_intelhad_hw_params called\n");
1317 buf_size
= params_buffer_bytes(hw_params
);
1318 retval
= snd_pcm_lib_malloc_pages(substream
, buf_size
);
1321 pr_debug("%s:allocated memory = %d\n", __func__
, buf_size
);
1322 /* mark the pages as uncached region */
1323 addr
= (unsigned long) substream
->runtime
->dma_area
;
1324 pages
= (substream
->runtime
->dma_bytes
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1325 retval
= set_memory_uc(addr
, pages
);
1327 pr_err("set_memory_uc failed.Error:%d\n", retval
);
1330 memset(substream
->runtime
->dma_area
, 0, buf_size
);
1336 * snd_intelhad_hw_free- to release the resources allocated during
1337 * hardware params setup
1339 * @substream: substream for which the function is called
1341 * This function is called by ALSA framework before close callback.
1344 static int snd_intelhad_hw_free(struct snd_pcm_substream
*substream
)
1349 pr_debug("snd_intelhad_hw_free called\n");
1351 /* mark back the pages as cached/writeback region before the free */
1352 if (substream
->runtime
->dma_area
!= NULL
) {
1353 addr
= (unsigned long) substream
->runtime
->dma_area
;
1354 pages
= (substream
->runtime
->dma_bytes
+ PAGE_SIZE
- 1) /
1356 set_memory_wb(addr
, pages
);
1357 return snd_pcm_lib_free_pages(substream
);
1363 * snd_intelhad_pcm_trigger - stream activities are handled here
1364 * @substream:substream for which the stream function is called
1365 * @cmd:the stream commamd thats requested from upper layer
1366 * This function is called whenever an a stream activity is invoked
1368 static int snd_intelhad_pcm_trigger(struct snd_pcm_substream
*substream
,
1371 int caps
, retval
= 0;
1372 unsigned long flag_irq
;
1373 struct snd_intelhad
*intelhaddata
;
1374 struct had_stream_pvt
*stream
;
1375 struct had_pvt_data
*had_stream
;
1377 pr_debug("snd_intelhad_pcm_trigger called\n");
1379 intelhaddata
= snd_pcm_substream_chip(substream
);
1380 stream
= substream
->runtime
->private_data
;
1381 had_stream
= intelhaddata
->private_data
;
1384 case SNDRV_PCM_TRIGGER_START
:
1385 pr_debug("Trigger Start\n");
1387 /* Disable local INTRs till register prgmng is done */
1388 if (had_get_hwstate(intelhaddata
)) {
1389 pr_err("_START: HDMI cable plugged-out\n");
1393 stream
->stream_status
= STREAM_RUNNING
;
1395 had_stream
->stream_type
= HAD_RUNNING_STREAM
;
1399 * ToDo: Need to enable UNDERRUN interrupts as well
1400 * caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
1402 caps
= HDMI_AUDIO_BUFFER_DONE
;
1403 retval
= had_set_caps(HAD_SET_ENABLE_AUDIO_INT
, &caps
);
1404 retval
= had_set_caps(HAD_SET_ENABLE_AUDIO
, NULL
);
1405 intelhaddata
->ops
->enable_audio(substream
, 1);
1407 pr_debug("Processed _Start\n");
1411 case SNDRV_PCM_TRIGGER_STOP
:
1412 pr_debug("Trigger Stop\n");
1413 spin_lock_irqsave(&intelhaddata
->had_spinlock
, flag_irq
);
1414 intelhaddata
->stream_info
.str_id
= 0;
1415 intelhaddata
->curr_buf
= 0;
1417 /* Stop reporting BUFFER_DONE/UNDERRUN to above layers*/
1419 had_stream
->stream_type
= HAD_INIT
;
1420 spin_unlock_irqrestore(&intelhaddata
->had_spinlock
, flag_irq
);
1423 * ToDo: Need to disable UNDERRUN interrupts as well
1424 * caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
1426 caps
= HDMI_AUDIO_BUFFER_DONE
;
1427 had_set_caps(HAD_SET_DISABLE_AUDIO_INT
, &caps
);
1428 intelhaddata
->ops
->enable_audio(substream
, 0);
1429 /* Reset buffer pointers */
1430 intelhaddata
->ops
->reset_audio(1);
1431 intelhaddata
->ops
->reset_audio(0);
1432 stream
->stream_status
= STREAM_DROPPED
;
1433 had_set_caps(HAD_SET_DISABLE_AUDIO
, NULL
);
1443 * snd_intelhad_pcm_prepare- internal preparation before starting a stream
1445 * @substream: substream for which the function is called
1447 * This function is called when a stream is started for internal preparation.
1449 static int snd_intelhad_pcm_prepare(struct snd_pcm_substream
*substream
)
1452 u32 disp_samp_freq
, n_param
;
1454 struct snd_intelhad
*intelhaddata
;
1455 struct snd_pcm_runtime
*runtime
;
1456 struct had_pvt_data
*had_stream
;
1458 pr_debug("snd_intelhad_pcm_prepare called\n");
1460 intelhaddata
= snd_pcm_substream_chip(substream
);
1461 runtime
= substream
->runtime
;
1462 had_stream
= intelhaddata
->private_data
;
1464 if (had_get_hwstate(intelhaddata
)) {
1465 pr_err("%s: HDMI cable plugged-out\n", __func__
);
1470 pr_debug("period_size=%d\n",
1471 (int)frames_to_bytes(runtime
, runtime
->period_size
));
1472 pr_debug("periods=%d\n", runtime
->periods
);
1473 pr_debug("buffer_size=%d\n", (int)snd_pcm_lib_buffer_bytes(substream
));
1474 pr_debug("rate=%d\n", runtime
->rate
);
1475 pr_debug("channels=%d\n", runtime
->channels
);
1477 if (intelhaddata
->stream_info
.str_id
) {
1478 pr_debug("_prepare is called for existing str_id#%d\n",
1479 intelhaddata
->stream_info
.str_id
);
1480 retval
= snd_intelhad_pcm_trigger(substream
,
1481 SNDRV_PCM_TRIGGER_STOP
);
1485 retval
= snd_intelhad_init_stream(substream
);
1490 /* Get N value in KHz */
1491 retval
= had_get_caps(HAD_GET_DISPLAY_RATE
, &disp_samp_freq
);
1493 pr_err("querying display sampling freq failed %#x\n", retval
);
1497 had_get_caps(HAD_GET_ELD
, &intelhaddata
->eeld
);
1498 had_get_caps(HAD_GET_DP_OUTPUT
, &intelhaddata
->dp_output
);
1500 retval
= intelhaddata
->ops
->prog_n(substream
->runtime
->rate
, &n_param
,
1503 pr_err("programming N value failed %#x\n", retval
);
1507 if (intelhaddata
->dp_output
)
1508 had_get_caps(HAD_GET_LINK_RATE
, &link_rate
);
1511 intelhaddata
->ops
->prog_cts(substream
->runtime
->rate
,
1512 disp_samp_freq
, link_rate
,
1513 n_param
, intelhaddata
);
1515 intelhaddata
->ops
->prog_dip(substream
, intelhaddata
);
1517 retval
= intelhaddata
->ops
->audio_ctrl(substream
, intelhaddata
);
1519 /* Prog buffer address */
1520 retval
= snd_intelhad_prog_buffer(intelhaddata
,
1521 HAD_BUF_TYPE_A
, HAD_BUF_TYPE_D
);
1524 * Program channel mapping in following order:
1525 * FL, FR, C, LFE, RL, RR
1528 had_write_register(AUD_BUF_CH_SWAP
, SWAP_LFE_CENTER
);
1535 * snd_intelhad_pcm_pointer- to send the current buffer pointerprocessed by hw
1537 * @substream: substream for which the function is called
1539 * This function is called by ALSA framework to get the current hw buffer ptr
1540 * when a period is elapsed
1542 static snd_pcm_uframes_t
snd_intelhad_pcm_pointer(
1543 struct snd_pcm_substream
*substream
)
1545 struct snd_intelhad
*intelhaddata
;
1546 u32 bytes_rendered
= 0;
1550 /* pr_debug("snd_intelhad_pcm_pointer called\n"); */
1552 intelhaddata
= snd_pcm_substream_chip(substream
);
1554 if (intelhaddata
->flag_underrun
) {
1555 intelhaddata
->flag_underrun
= 0;
1556 return SNDRV_PCM_POS_XRUN
;
1559 /* Use a hw register to calculate sub-period position reports.
1560 * This makes PulseAudio happier.
1563 buf_id
= intelhaddata
->curr_buf
% 4;
1564 had_read_register(AUD_BUF_A_LENGTH
+ (buf_id
* HAD_REG_WIDTH
), &t
);
1566 if ((t
== 0) || (t
== ((u32
)-1L))) {
1568 pr_debug("discovered buffer done for buf %d, count = %d\n",
1569 buf_id
, underrun_count
);
1571 if (underrun_count
> (HAD_MIN_PERIODS
/2)) {
1572 pr_debug("assume audio_codec_reset, underrun = %d - do xrun\n",
1575 return SNDRV_PCM_POS_XRUN
;
1582 t
= intelhaddata
->buf_info
[buf_id
].buf_size
- t
;
1584 if (intelhaddata
->stream_info
.buffer_rendered
)
1585 div_u64_rem(intelhaddata
->stream_info
.buffer_rendered
,
1586 intelhaddata
->stream_info
.ring_buf_size
,
1589 intelhaddata
->stream_info
.buffer_ptr
= bytes_to_frames(
1591 bytes_rendered
+ t
);
1592 return intelhaddata
->stream_info
.buffer_ptr
;
1596 * snd_intelhad_pcm_mmap- mmaps a kernel buffer to user space for copying data
1598 * @substream: substream for which the function is called
1599 * @vma: struct instance of memory VMM memory area
1601 * This function is called by OS when a user space component
1602 * tries to get mmap memory from driver
1604 static int snd_intelhad_pcm_mmap(struct snd_pcm_substream
*substream
,
1605 struct vm_area_struct
*vma
)
1608 pr_debug("snd_intelhad_pcm_mmap called\n");
1610 pr_debug("entry with prot:%s\n", __func__
);
1611 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1612 return remap_pfn_range(vma
, vma
->vm_start
,
1613 substream
->dma_buffer
.addr
>> PAGE_SHIFT
,
1614 vma
->vm_end
- vma
->vm_start
, vma
->vm_page_prot
);
1617 int hdmi_audio_mode_change(struct snd_pcm_substream
*substream
)
1620 u32 disp_samp_freq
, n_param
;
1622 struct snd_intelhad
*intelhaddata
;
1624 intelhaddata
= snd_pcm_substream_chip(substream
);
1627 intelhaddata
->ops
->enable_audio(substream
, 0);
1629 /* Update CTS value */
1630 retval
= had_get_caps(HAD_GET_DISPLAY_RATE
, &disp_samp_freq
);
1632 pr_err("querying display sampling freq failed %#x\n", retval
);
1636 retval
= intelhaddata
->ops
->prog_n(substream
->runtime
->rate
, &n_param
,
1639 pr_err("programming N value failed %#x\n", retval
);
1643 if (intelhaddata
->dp_output
)
1644 had_get_caps(HAD_GET_LINK_RATE
, &link_rate
);
1646 intelhaddata
->ops
->prog_cts(substream
->runtime
->rate
,
1647 disp_samp_freq
, link_rate
,
1648 n_param
, intelhaddata
);
1651 intelhaddata
->ops
->enable_audio(substream
, 1);
1657 /*PCM operations structure and the calls back for the same */
1658 struct snd_pcm_ops snd_intelhad_playback_ops
= {
1659 .open
= snd_intelhad_open
,
1660 .close
= snd_intelhad_close
,
1661 .ioctl
= snd_pcm_lib_ioctl
,
1662 .hw_params
= snd_intelhad_hw_params
,
1663 .hw_free
= snd_intelhad_hw_free
,
1664 .prepare
= snd_intelhad_pcm_prepare
,
1665 .trigger
= snd_intelhad_pcm_trigger
,
1666 .pointer
= snd_intelhad_pcm_pointer
,
1667 .mmap
= snd_intelhad_pcm_mmap
,
1671 * snd_intelhad_create - to crete alsa card instance
1673 * @intelhaddata: pointer to internal context
1674 * @card: pointer to card
1676 * This function is called when the hdmi cable is plugged in
1678 static int snd_intelhad_create(
1679 struct snd_intelhad
*intelhaddata
,
1680 struct snd_card
*card
)
1683 static struct snd_device_ops ops
= {
1686 pr_debug("snd_intelhad_create called\n");
1691 /* ALSA api to register the device */
1692 retval
= snd_device_new(card
, SNDRV_DEV_LOWLEVEL
, intelhaddata
, &ops
);
1696 * snd_intelhad_pcm_free - to free the memory allocated
1698 * @pcm: pointer to pcm instance
1699 * This function is called when the device is removed
1701 static void snd_intelhad_pcm_free(struct snd_pcm
*pcm
)
1703 pr_debug("Freeing PCM preallocated pages\n");
1704 snd_pcm_lib_preallocate_free_for_all(pcm
);
1707 static int had_iec958_info(struct snd_kcontrol
*kcontrol
,
1708 struct snd_ctl_elem_info
*uinfo
)
1710 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_IEC958
;
1715 static int had_iec958_get(struct snd_kcontrol
*kcontrol
,
1716 struct snd_ctl_elem_value
*ucontrol
)
1718 struct snd_intelhad
*intelhaddata
= snd_kcontrol_chip(kcontrol
);
1720 ucontrol
->value
.iec958
.status
[0] = (intelhaddata
->aes_bits
>> 0) & 0xff;
1721 ucontrol
->value
.iec958
.status
[1] = (intelhaddata
->aes_bits
>> 8) & 0xff;
1722 ucontrol
->value
.iec958
.status
[2] =
1723 (intelhaddata
->aes_bits
>> 16) & 0xff;
1724 ucontrol
->value
.iec958
.status
[3] =
1725 (intelhaddata
->aes_bits
>> 24) & 0xff;
1728 static int had_iec958_mask_get(struct snd_kcontrol
*kcontrol
,
1729 struct snd_ctl_elem_value
*ucontrol
)
1731 ucontrol
->value
.iec958
.status
[0] = 0xff;
1732 ucontrol
->value
.iec958
.status
[1] = 0xff;
1733 ucontrol
->value
.iec958
.status
[2] = 0xff;
1734 ucontrol
->value
.iec958
.status
[3] = 0xff;
1737 static int had_iec958_put(struct snd_kcontrol
*kcontrol
,
1738 struct snd_ctl_elem_value
*ucontrol
)
1741 struct snd_intelhad
*intelhaddata
= snd_kcontrol_chip(kcontrol
);
1743 pr_debug("entered had_iec958_put\n");
1744 val
= (ucontrol
->value
.iec958
.status
[0] << 0) |
1745 (ucontrol
->value
.iec958
.status
[1] << 8) |
1746 (ucontrol
->value
.iec958
.status
[2] << 16) |
1747 (ucontrol
->value
.iec958
.status
[3] << 24);
1748 if (intelhaddata
->aes_bits
!= val
) {
1749 intelhaddata
->aes_bits
= val
;
1755 static struct snd_kcontrol_new had_control_iec958_mask
= {
1756 .access
= SNDRV_CTL_ELEM_ACCESS_READ
,
1757 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
1758 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, MASK
),
1759 .info
= had_iec958_info
, /* shared */
1760 .get
= had_iec958_mask_get
,
1763 static struct snd_kcontrol_new had_control_iec958
= {
1764 .iface
= SNDRV_CTL_ELEM_IFACE_PCM
,
1765 .name
= SNDRV_CTL_NAME_IEC958("", PLAYBACK
, DEFAULT
),
1766 .info
= had_iec958_info
,
1767 .get
= had_iec958_get
,
1768 .put
= had_iec958_put
1771 static struct snd_intel_had_interface had_interface
= {
1772 .name
= "hdmi-audio",
1773 .query
= hdmi_audio_query
,
1774 .suspend
= hdmi_audio_suspend
,
1775 .resume
= hdmi_audio_resume
,
1778 static struct had_ops had_ops_v1
= {
1779 .enable_audio
= snd_intelhad_enable_audio_v1
,
1780 .reset_audio
= snd_intelhad_reset_audio_v1
,
1781 .prog_n
= snd_intelhad_prog_n_v1
,
1782 .prog_cts
= snd_intelhad_prog_cts_v1
,
1783 .audio_ctrl
= snd_intelhad_prog_audio_ctrl_v1
,
1784 .prog_dip
= snd_intelhad_prog_dip_v1
,
1785 .handle_underrun
= had_clear_underrun_intr_v1
,
1788 static struct had_ops had_ops_v2
= {
1789 .enable_audio
= snd_intelhad_enable_audio_v2
,
1790 .reset_audio
= snd_intelhad_reset_audio_v2
,
1791 .prog_n
= snd_intelhad_prog_n_v2
,
1792 .prog_cts
= snd_intelhad_prog_cts_v2
,
1793 .audio_ctrl
= snd_intelhad_prog_audio_ctrl_v2
,
1794 .prog_dip
= snd_intelhad_prog_dip_v2
,
1795 .handle_underrun
= had_clear_underrun_intr_v2
,
1798 * hdmi_audio_probe - to create sound card instance for HDMI audio playabck
1800 *@haddata: pointer to HAD private data
1801 *@card_id: card for which probe is called
1803 * This function is called when the hdmi cable is plugged in. This function
1804 * creates and registers the sound card with ALSA
1806 int hdmi_audio_probe(void *deviceptr
)
1809 struct snd_pcm
*pcm
;
1810 struct snd_card
*card
;
1811 struct had_callback_ops ops_cb
;
1812 struct snd_intelhad
*intelhaddata
;
1813 struct had_pvt_data
*had_stream
;
1814 struct platform_device
*devptr
= deviceptr
;
1816 pr_debug("Enter %s\n", __func__
);
1818 pr_debug("hdmi_audio_probe dma_mask: %p\n", devptr
->dev
.dma_mask
);
1820 /* allocate memory for saving internal context and working */
1821 intelhaddata
= kzalloc(sizeof(*intelhaddata
), GFP_KERNEL
);
1825 had_stream
= kzalloc(sizeof(*had_stream
), GFP_KERNEL
);
1831 had_data
= intelhaddata
;
1832 ops_cb
.intel_had_event_call_back
= had_event_handler
;
1834 /* registering with display driver to get access to display APIs */
1836 retval
= mid_hdmi_audio_setup(
1837 ops_cb
.intel_had_event_call_back
,
1838 &(intelhaddata
->reg_ops
),
1839 &(intelhaddata
->query_ops
));
1841 pr_err("querying display driver APIs failed %#x\n", retval
);
1842 goto free_hadstream
;
1844 mutex_lock(&had_mutex
);
1845 spin_lock_init(&intelhaddata
->had_spinlock
);
1846 intelhaddata
->drv_status
= HAD_DRV_DISCONNECTED
;
1847 pr_debug("%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
1848 __func__
, __LINE__
);
1850 /* create a card instance with ALSA framework */
1851 retval
= snd_card_new(&devptr
->dev
, hdmi_card_index
, hdmi_card_id
,
1852 THIS_MODULE
, 0, &card
);
1856 intelhaddata
->card
= card
;
1857 intelhaddata
->card_id
= hdmi_card_id
;
1858 intelhaddata
->card_index
= card
->number
;
1859 intelhaddata
->private_data
= had_stream
;
1860 intelhaddata
->flag_underrun
= 0;
1861 intelhaddata
->aes_bits
= SNDRV_PCM_DEFAULT_CON_SPDIF
;
1862 strncpy(card
->driver
, INTEL_HAD
, strlen(INTEL_HAD
));
1863 strncpy(card
->shortname
, INTEL_HAD
, strlen(INTEL_HAD
));
1865 retval
= snd_pcm_new(card
, INTEL_HAD
, PCM_INDEX
, MAX_PB_STREAMS
,
1866 MAX_CAP_STREAMS
, &pcm
);
1870 /* setup private data which can be retrieved when required */
1871 pcm
->private_data
= intelhaddata
;
1872 pcm
->private_free
= snd_intelhad_pcm_free
;
1873 pcm
->info_flags
= 0;
1874 strncpy(pcm
->name
, card
->shortname
, strlen(card
->shortname
));
1875 /* setup the ops for palyabck */
1876 snd_pcm_set_ops(pcm
, SNDRV_PCM_STREAM_PLAYBACK
,
1877 &snd_intelhad_playback_ops
);
1878 /* allocate dma pages for ALSA stream operations
1879 * memory allocated is based on size, not max value
1880 * thus using same argument for max & size
1882 retval
= snd_pcm_lib_preallocate_pages_for_all(pcm
,
1883 SNDRV_DMA_TYPE_DEV
, NULL
,
1884 HAD_MAX_BUFFER
, HAD_MAX_BUFFER
);
1886 if (card
->dev
== NULL
)
1887 pr_debug("card->dev is NULL!!!!! Should not be this case\n");
1888 else if (card
->dev
->dma_mask
== NULL
)
1889 pr_debug("hdmi_audio_probe dma_mask is NULL!!!!!\n");
1891 pr_debug("hdmi_audio_probe dma_mask is : %p\n",
1892 card
->dev
->dma_mask
);
1897 /* internal function call to register device with ALSA */
1898 retval
= snd_intelhad_create(intelhaddata
, card
);
1902 card
->private_data
= &intelhaddata
;
1903 retval
= snd_card_register(card
);
1907 /* IEC958 controls */
1908 retval
= snd_ctl_add(card
, snd_ctl_new1(&had_control_iec958_mask
,
1912 retval
= snd_ctl_add(card
, snd_ctl_new1(&had_control_iec958
,
1917 init_channel_allocations();
1919 /* Register channel map controls */
1920 retval
= had_register_chmap_ctls(intelhaddata
, pcm
);
1924 intelhaddata
->dev
= &devptr
->dev
;
1925 pm_runtime_set_active(intelhaddata
->dev
);
1926 pm_runtime_enable(intelhaddata
->dev
);
1928 mutex_unlock(&had_mutex
);
1929 retval
= mid_hdmi_audio_register(&had_interface
, intelhaddata
);
1931 pr_err("registering with display driver failed %#x\n", retval
);
1932 snd_card_free(card
);
1933 goto free_hadstream
;
1936 intelhaddata
->hw_silence
= 1;
1937 had_ops_v1
= had_ops_v1
; /* unused */
1938 intelhaddata
->ops
= &had_ops_v2
;
1942 snd_card_free(card
);
1944 mutex_unlock(&had_mutex
);
1947 pm_runtime_disable(intelhaddata
->dev
);
1948 intelhaddata
->dev
= NULL
;
1950 kfree(intelhaddata
);
1951 intelhaddata
= NULL
;
1952 pr_err("Error returned from %s api %#x\n", __func__
, retval
);
1957 * hdmi_audio_remove - removes the alsa card
1959 *@haddata: pointer to HAD private data
1961 * This function is called when the hdmi cable is un-plugged. This function
1962 * free the sound card.
1964 int hdmi_audio_remove(void *pdevptr
)
1966 struct snd_intelhad
*intelhaddata
= had_data
;
1969 pr_debug("Enter %s\n", __func__
);
1974 if (intelhaddata
->drv_status
!= HAD_DRV_DISCONNECTED
) {
1975 caps
= HDMI_AUDIO_UNDERRUN
| HDMI_AUDIO_BUFFER_DONE
;
1976 had_set_caps(HAD_SET_DISABLE_AUDIO_INT
, &caps
);
1977 had_set_caps(HAD_SET_DISABLE_AUDIO
, NULL
);
1979 snd_card_free(intelhaddata
->card
);
1980 kfree(intelhaddata
->private_data
);
1981 kfree(intelhaddata
);
1985 MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1986 MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1987 MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1988 MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1989 MODULE_DESCRIPTION("Intel HDMI Audio driver");
1990 MODULE_LICENSE("GPL v2");
1991 MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");