]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - sound/x86/intel_hdmi_audio.c
ALSA: usb-audio: localize function without external linkage
[mirror_ubuntu-jammy-kernel.git] / sound / x86 / intel_hdmi_audio.c
CommitLineData
5dab11d8
JA
1/*
2 * intel_hdmi_audio.c - Intel HDMI audio driver
3 *
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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 *
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.
14 *
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.
19 *
20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 * ALSA driver for Intel HDMI audio
22 */
23
03c34377 24#include <linux/types.h>
5dab11d8
JA
25#include <linux/platform_device.h>
26#include <linux/io.h>
27#include <linux/slab.h>
28#include <linux/module.h>
da864809 29#include <linux/interrupt.h>
03c34377 30#include <linux/pm_runtime.h>
412bbe7d 31#include <linux/dma-mapping.h>
e2acecf2 32#include <linux/delay.h>
5dab11d8 33#include <asm/cacheflush.h>
5dab11d8 34#include <sound/core.h>
03c34377
TI
35#include <sound/asoundef.h>
36#include <sound/pcm.h>
5dab11d8
JA
37#include <sound/pcm_params.h>
38#include <sound/initval.h>
39#include <sound/control.h>
b9bacf27 40#include <sound/jack.h>
03c34377 41#include <drm/drm_edid.h>
da864809 42#include <drm/intel_lpe_audio.h>
5dab11d8
JA
43#include "intel_hdmi_audio.h"
44
5dab11d8
JA
45/*standard module options for ALSA. This module supports only one card*/
46static int hdmi_card_index = SNDRV_DEFAULT_IDX1;
47static char *hdmi_card_id = SNDRV_DEFAULT_STR1;
5dab11d8
JA
48
49module_param_named(index, hdmi_card_index, int, 0444);
50MODULE_PARM_DESC(index,
51 "Index value for INTEL Intel HDMI Audio controller.");
52module_param_named(id, hdmi_card_id, charp, 0444);
53MODULE_PARM_DESC(id,
54 "ID string for INTEL Intel HDMI Audio controller.");
55
56/*
57 * ELD SA bits in the CEA Speaker Allocation data block
58 */
4a5ddb2c 59static const int eld_speaker_allocation_bits[] = {
5dab11d8
JA
60 [0] = FL | FR,
61 [1] = LFE,
62 [2] = FC,
63 [3] = RL | RR,
64 [4] = RC,
65 [5] = FLC | FRC,
66 [6] = RLC | RRC,
67 /* the following are not defined in ELD yet */
68 [7] = 0,
69};
70
71/*
72 * This is an ordered list!
73 *
74 * The preceding ones have better chances to be selected by
75 * hdmi_channel_allocation().
76 */
77static struct cea_channel_speaker_allocation channel_allocations[] = {
78/* channel: 7 6 5 4 3 2 1 0 */
79{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
80 /* 2.1 */
81{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
82 /* Dolby Surround */
83{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
84 /* surround40 */
85{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
86 /* surround41 */
87{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
88 /* surround50 */
89{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
90 /* surround51 */
91{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
92 /* 6.1 */
93{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
94 /* surround71 */
95{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
96
97{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
98{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
99{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
100{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
101{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
102{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
103{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
104{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
105{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
106{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
107{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
108{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
109{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
110{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
111{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
112{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
113{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
114{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
115{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
116{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
117{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
118{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
119{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
120};
121
4a5ddb2c 122static const struct channel_map_table map_tables[] = {
5dab11d8
JA
123 { SNDRV_CHMAP_FL, 0x00, FL },
124 { SNDRV_CHMAP_FR, 0x01, FR },
125 { SNDRV_CHMAP_RL, 0x04, RL },
126 { SNDRV_CHMAP_RR, 0x05, RR },
127 { SNDRV_CHMAP_LFE, 0x02, LFE },
128 { SNDRV_CHMAP_FC, 0x03, FC },
129 { SNDRV_CHMAP_RLC, 0x06, RLC },
130 { SNDRV_CHMAP_RRC, 0x07, RRC },
131 {} /* terminator */
132};
133
134/* hardware capability structure */
b556290f 135static const struct snd_pcm_hardware had_pcm_hardware = {
5dab11d8 136 .info = (SNDRV_PCM_INFO_INTERLEAVED |
a9ebdd0e 137 SNDRV_PCM_INFO_MMAP |
e8de9859
TI
138 SNDRV_PCM_INFO_MMAP_VALID |
139 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
3fe2cf7e
TI
140 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
141 SNDRV_PCM_FMTBIT_S24_LE |
85bd8748 142 SNDRV_PCM_FMTBIT_S32_LE),
5dab11d8
JA
143 .rates = SNDRV_PCM_RATE_32000 |
144 SNDRV_PCM_RATE_44100 |
145 SNDRV_PCM_RATE_48000 |
146 SNDRV_PCM_RATE_88200 |
147 SNDRV_PCM_RATE_96000 |
148 SNDRV_PCM_RATE_176400 |
149 SNDRV_PCM_RATE_192000,
150 .rate_min = HAD_MIN_RATE,
151 .rate_max = HAD_MAX_RATE,
152 .channels_min = HAD_MIN_CHANNEL,
153 .channels_max = HAD_MAX_CHANNEL,
154 .buffer_bytes_max = HAD_MAX_BUFFER,
155 .period_bytes_min = HAD_MIN_PERIOD_BYTES,
156 .period_bytes_max = HAD_MAX_PERIOD_BYTES,
157 .periods_min = HAD_MIN_PERIODS,
158 .periods_max = HAD_MAX_PERIODS,
159 .fifo_size = HAD_FIFO_SIZE,
160};
161
313d9f28
TI
162/* Get the active PCM substream;
163 * Call had_substream_put() for unreferecing.
164 * Don't call this inside had_spinlock, as it takes by itself
165 */
166static struct snd_pcm_substream *
167had_substream_get(struct snd_intelhad *intelhaddata)
168{
169 struct snd_pcm_substream *substream;
170 unsigned long flags;
171
172 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
173 substream = intelhaddata->stream_info.substream;
174 if (substream)
175 intelhaddata->stream_info.substream_refcount++;
176 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
177 return substream;
178}
179
180/* Unref the active PCM substream;
181 * Don't call this inside had_spinlock, as it takes by itself
182 */
183static void had_substream_put(struct snd_intelhad *intelhaddata)
184{
185 unsigned long flags;
186
187 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
188 intelhaddata->stream_info.substream_refcount--;
189 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
190}
191
5dab11d8 192/* Register access functions */
28ed125b
TI
193static u32 had_read_register_raw(struct snd_intelhad *ctx, u32 reg)
194{
195 return ioread32(ctx->mmio_start + ctx->had_config_offset + reg);
196}
197
198static void had_write_register_raw(struct snd_intelhad *ctx, u32 reg, u32 val)
199{
200 iowrite32(val, ctx->mmio_start + ctx->had_config_offset + reg);
201}
202
83af57dd 203static void had_read_register(struct snd_intelhad *ctx, u32 reg, u32 *val)
5dab11d8 204{
28ed125b
TI
205 if (!ctx->connected)
206 *val = 0;
207 else
208 *val = had_read_register_raw(ctx, reg);
5dab11d8
JA
209}
210
83af57dd 211static void had_write_register(struct snd_intelhad *ctx, u32 reg, u32 val)
5dab11d8 212{
28ed125b
TI
213 if (ctx->connected)
214 had_write_register_raw(ctx, reg, val);
5dab11d8
JA
215}
216
da864809 217/*
313d9f28
TI
218 * enable / disable audio configuration
219 *
83af57dd 220 * The normal read/modify should not directly be used on VLV2 for
da864809 221 * updating AUD_CONFIG register.
5dab11d8
JA
222 * This is because:
223 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
224 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
225 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
226 * register. This field should be 1xy binary for configuration with 6 or
227 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
228 * causes the "channels" field to be updated as 0xy binary resulting in
229 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
230 * appropriate value when doing read-modify of AUD_CONFIG register.
5dab11d8 231 */
40ce4b5d 232static void had_enable_audio(struct snd_intelhad *intelhaddata,
b556290f 233 bool enable)
5dab11d8 234{
40ce4b5d
TI
235 /* update the cached value */
236 intelhaddata->aud_config.regx.aud_en = enable;
237 had_write_register(intelhaddata, AUD_CONFIG,
238 intelhaddata->aud_config.regval);
5dab11d8
JA
239}
240
075a1d46
TI
241/* forcibly ACKs to both BUFFER_DONE and BUFFER_UNDERRUN interrupts */
242static void had_ack_irqs(struct snd_intelhad *ctx)
da864809
TI
243{
244 u32 status_reg;
245
28ed125b
TI
246 if (!ctx->connected)
247 return;
075a1d46
TI
248 had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
249 status_reg |= HDMI_AUDIO_BUFFER_DONE | HDMI_AUDIO_UNDERRUN;
250 had_write_register(ctx, AUD_HDMI_STATUS, status_reg);
251 had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
da864809
TI
252}
253
f4566aa1
TI
254/* Reset buffer pointers */
255static void had_reset_audio(struct snd_intelhad *intelhaddata)
5dab11d8 256{
77531bee
TI
257 had_write_register(intelhaddata, AUD_HDMI_STATUS,
258 AUD_HDMI_STATUSG_MASK_FUNCRST);
f4566aa1 259 had_write_register(intelhaddata, AUD_HDMI_STATUS, 0);
5dab11d8
JA
260}
261
2e52f5e5 262/*
5dab11d8
JA
263 * initialize audio channel status registers
264 * This function is called in the prepare callback
265 */
266static int had_prog_status_reg(struct snd_pcm_substream *substream,
267 struct snd_intelhad *intelhaddata)
268{
7ceba75f
TI
269 union aud_cfg cfg_val = {.regval = 0};
270 union aud_ch_status_0 ch_stat0 = {.regval = 0};
271 union aud_ch_status_1 ch_stat1 = {.regval = 0};
5dab11d8 272
7ceba75f 273 ch_stat0.regx.lpcm_id = (intelhaddata->aes_bits &
2e52f5e5 274 IEC958_AES0_NONAUDIO) >> 1;
7ceba75f 275 ch_stat0.regx.clk_acc = (intelhaddata->aes_bits &
2e52f5e5 276 IEC958_AES3_CON_CLOCK) >> 4;
7ceba75f 277 cfg_val.regx.val_bit = ch_stat0.regx.lpcm_id;
5dab11d8
JA
278
279 switch (substream->runtime->rate) {
280 case AUD_SAMPLE_RATE_32:
7ceba75f 281 ch_stat0.regx.samp_freq = CH_STATUS_MAP_32KHZ;
5dab11d8
JA
282 break;
283
284 case AUD_SAMPLE_RATE_44_1:
7ceba75f 285 ch_stat0.regx.samp_freq = CH_STATUS_MAP_44KHZ;
5dab11d8
JA
286 break;
287 case AUD_SAMPLE_RATE_48:
7ceba75f 288 ch_stat0.regx.samp_freq = CH_STATUS_MAP_48KHZ;
5dab11d8
JA
289 break;
290 case AUD_SAMPLE_RATE_88_2:
7ceba75f 291 ch_stat0.regx.samp_freq = CH_STATUS_MAP_88KHZ;
5dab11d8
JA
292 break;
293 case AUD_SAMPLE_RATE_96:
7ceba75f 294 ch_stat0.regx.samp_freq = CH_STATUS_MAP_96KHZ;
5dab11d8
JA
295 break;
296 case AUD_SAMPLE_RATE_176_4:
7ceba75f 297 ch_stat0.regx.samp_freq = CH_STATUS_MAP_176KHZ;
5dab11d8
JA
298 break;
299 case AUD_SAMPLE_RATE_192:
7ceba75f 300 ch_stat0.regx.samp_freq = CH_STATUS_MAP_192KHZ;
5dab11d8
JA
301 break;
302
303 default:
304 /* control should never come here */
305 return -EINVAL;
5dab11d8 306 }
2e52f5e5 307
79dda75a 308 had_write_register(intelhaddata,
7ceba75f 309 AUD_CH_STATUS_0, ch_stat0.regval);
5dab11d8 310
85bd8748 311 switch (substream->runtime->format) {
85bd8748 312 case SNDRV_PCM_FORMAT_S16_LE:
7ceba75f
TI
313 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_20;
314 ch_stat1.regx.wrd_len = SMPL_WIDTH_16BITS;
85bd8748 315 break;
85bd8748
TI
316 case SNDRV_PCM_FORMAT_S24_LE:
317 case SNDRV_PCM_FORMAT_S32_LE:
7ceba75f
TI
318 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_24;
319 ch_stat1.regx.wrd_len = SMPL_WIDTH_24BITS;
85bd8748
TI
320 break;
321 default:
322 return -EINVAL;
5dab11d8 323 }
2e52f5e5 324
79dda75a 325 had_write_register(intelhaddata,
7ceba75f 326 AUD_CH_STATUS_1, ch_stat1.regval);
5dab11d8
JA
327 return 0;
328}
329
76296ef0 330/*
5dab11d8
JA
331 * function to initialize audio
332 * registers and buffer confgiuration registers
333 * This function is called in the prepare callback
334 */
b556290f
TI
335static int had_init_audio_ctrl(struct snd_pcm_substream *substream,
336 struct snd_intelhad *intelhaddata)
5dab11d8 337{
7ceba75f
TI
338 union aud_cfg cfg_val = {.regval = 0};
339 union aud_buf_config buf_cfg = {.regval = 0};
5dab11d8
JA
340 u8 channels;
341
342 had_prog_status_reg(substream, intelhaddata);
343
7ceba75f
TI
344 buf_cfg.regx.audio_fifo_watermark = FIFO_THRESHOLD;
345 buf_cfg.regx.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
346 buf_cfg.regx.aud_delay = 0;
347 had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.regval);
5dab11d8
JA
348
349 channels = substream->runtime->channels;
7ceba75f 350 cfg_val.regx.num_ch = channels - 2;
5dab11d8 351 if (channels <= 2)
7ceba75f 352 cfg_val.regx.layout = LAYOUT0;
5dab11d8 353 else
7ceba75f 354 cfg_val.regx.layout = LAYOUT1;
5dab11d8 355
3fe2cf7e
TI
356 if (substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE)
357 cfg_val.regx.packet_mode = 1;
358
85bd8748
TI
359 if (substream->runtime->format == SNDRV_PCM_FORMAT_S32_LE)
360 cfg_val.regx.left_align = 1;
361
7ceba75f 362 cfg_val.regx.val_bit = 1;
83af57dd
TI
363
364 /* fix up the DP bits */
365 if (intelhaddata->dp_output) {
366 cfg_val.regx.dp_modei = 1;
367 cfg_val.regx.set = 1;
368 }
369
7ceba75f 370 had_write_register(intelhaddata, AUD_CONFIG, cfg_val.regval);
40ce4b5d 371 intelhaddata->aud_config = cfg_val;
5dab11d8
JA
372 return 0;
373}
374
5dab11d8
JA
375/*
376 * Compute derived values in channel_allocations[].
377 */
378static void init_channel_allocations(void)
379{
380 int i, j;
381 struct cea_channel_speaker_allocation *p;
382
5dab11d8
JA
383 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
384 p = channel_allocations + i;
385 p->channels = 0;
386 p->spk_mask = 0;
387 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
388 if (p->speakers[j]) {
389 p->channels++;
390 p->spk_mask |= p->speakers[j];
391 }
392 }
393}
394
395/*
396 * The transformation takes two steps:
397 *
398 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
399 * spk_mask => (channel_allocations[]) => ai->CA
400 *
401 * TODO: it could select the wrong CA from multiple candidates.
402 */
b556290f
TI
403static int had_channel_allocation(struct snd_intelhad *intelhaddata,
404 int channels)
5dab11d8
JA
405{
406 int i;
407 int ca = 0;
408 int spk_mask = 0;
409
410 /*
411 * CA defaults to 0 for basic stereo audio
412 */
413 if (channels <= 2)
414 return 0;
415
416 /*
417 * expand ELD's speaker allocation mask
418 *
419 * ELD tells the speaker mask in a compact(paired) form,
420 * expand ELD's notions to match the ones used by Audio InfoFrame.
421 */
422
423 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
df0435db 424 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
5dab11d8
JA
425 spk_mask |= eld_speaker_allocation_bits[i];
426 }
427
428 /* search for the first working match in the CA table */
429 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
430 if (channels == channel_allocations[i].channels &&
431 (spk_mask & channel_allocations[i].spk_mask) ==
432 channel_allocations[i].spk_mask) {
433 ca = channel_allocations[i].ca_index;
434 break;
435 }
436 }
437
c75b0476 438 dev_dbg(intelhaddata->dev, "select CA 0x%x for %d\n", ca, channels);
5dab11d8
JA
439
440 return ca;
441}
442
443/* from speaker bit mask to ALSA API channel position */
444static int spk_to_chmap(int spk)
445{
4a5ddb2c 446 const struct channel_map_table *t = map_tables;
5dab11d8
JA
447
448 for (; t->map; t++) {
449 if (t->spk_mask == spk)
450 return t->map;
451 }
452 return 0;
453}
454
372d855f 455static void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
5dab11d8 456{
2e52f5e5 457 int i, c;
5dab11d8
JA
458 int spk_mask = 0;
459 struct snd_pcm_chmap_elem *chmap;
460 u8 eld_high, eld_high_mask = 0xF0;
461 u8 high_msb;
462
18353192
TI
463 kfree(intelhaddata->chmap->chmap);
464 intelhaddata->chmap->chmap = NULL;
465
5dab11d8 466 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
18353192 467 if (!chmap)
5dab11d8 468 return;
5dab11d8 469
df0435db
TI
470 dev_dbg(intelhaddata->dev, "eld speaker = %x\n",
471 intelhaddata->eld[DRM_ELD_SPEAKER]);
5dab11d8
JA
472
473 /* WA: Fix the max channel supported to 8 */
474
475 /*
476 * Sink may support more than 8 channels, if eld_high has more than
477 * one bit set. SOC supports max 8 channels.
478 * Refer eld_speaker_allocation_bits, for sink speaker allocation
479 */
480
481 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
df0435db 482 eld_high = intelhaddata->eld[DRM_ELD_SPEAKER] & eld_high_mask;
5dab11d8
JA
483 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
484 /* eld_high & (eld_high-1): if more than 1 bit set */
485 /* 0x1F: 7 channels */
486 for (i = 1; i < 4; i++) {
487 high_msb = eld_high & (0x80 >> i);
488 if (high_msb) {
df0435db 489 intelhaddata->eld[DRM_ELD_SPEAKER] &=
5dab11d8
JA
490 high_msb | 0xF;
491 break;
492 }
493 }
494 }
495
496 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
df0435db 497 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
5dab11d8
JA
498 spk_mask |= eld_speaker_allocation_bits[i];
499 }
500
501 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
502 if (spk_mask == channel_allocations[i].spk_mask) {
503 for (c = 0; c < channel_allocations[i].channels; c++) {
504 chmap->map[c] = spk_to_chmap(
505 channel_allocations[i].speakers[
2e52f5e5 506 (MAX_SPEAKERS - 1) - c]);
5dab11d8
JA
507 }
508 chmap->channels = channel_allocations[i].channels;
509 intelhaddata->chmap->chmap = chmap;
510 break;
511 }
512 }
18353192 513 if (i >= ARRAY_SIZE(channel_allocations))
5dab11d8 514 kfree(chmap);
5dab11d8
JA
515}
516
517/*
518 * ALSA API channel-map control callbacks
519 */
520static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
521 struct snd_ctl_elem_info *uinfo)
522{
5dab11d8
JA
523 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
524 uinfo->count = HAD_MAX_CHANNEL;
525 uinfo->value.integer.min = 0;
526 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
527 return 0;
528}
529
530static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
531 struct snd_ctl_elem_value *ucontrol)
532{
533 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
534 struct snd_intelhad *intelhaddata = info->private_data;
2e52f5e5 535 int i;
5dab11d8
JA
536 const struct snd_pcm_chmap_elem *chmap;
537
a72ccfba
TI
538 memset(ucontrol->value.integer.value, 0,
539 sizeof(long) * HAD_MAX_CHANNEL);
8f8d1d7f
TI
540 mutex_lock(&intelhaddata->mutex);
541 if (!intelhaddata->chmap->chmap) {
542 mutex_unlock(&intelhaddata->mutex);
a72ccfba 543 return 0;
8f8d1d7f
TI
544 }
545
5dab11d8 546 chmap = intelhaddata->chmap->chmap;
c75b0476 547 for (i = 0; i < chmap->channels; i++)
5dab11d8 548 ucontrol->value.integer.value[i] = chmap->map[i];
8f8d1d7f 549 mutex_unlock(&intelhaddata->mutex);
5dab11d8
JA
550
551 return 0;
552}
553
554static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
555 struct snd_pcm *pcm)
556{
2e52f5e5 557 int err;
5dab11d8
JA
558
559 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
560 NULL, 0, (unsigned long)intelhaddata,
561 &intelhaddata->chmap);
562 if (err < 0)
563 return err;
564
565 intelhaddata->chmap->private_data = intelhaddata;
e9d65abf
TI
566 intelhaddata->chmap->kctl->info = had_chmap_ctl_info;
567 intelhaddata->chmap->kctl->get = had_chmap_ctl_get;
5dab11d8
JA
568 intelhaddata->chmap->chmap = NULL;
569 return 0;
570}
571
76296ef0 572/*
44684f61 573 * Initialize Data Island Packets registers
5dab11d8
JA
574 * This function is called in the prepare callback
575 */
b556290f
TI
576static void had_prog_dip(struct snd_pcm_substream *substream,
577 struct snd_intelhad *intelhaddata)
5dab11d8
JA
578{
579 int i;
7ceba75f
TI
580 union aud_ctrl_st ctrl_state = {.regval = 0};
581 union aud_info_frame2 frame2 = {.regval = 0};
582 union aud_info_frame3 frame3 = {.regval = 0};
5dab11d8 583 u8 checksum = 0;
964ca808 584 u32 info_frame;
5dab11d8 585 int channels;
36ed3466 586 int ca;
5dab11d8
JA
587
588 channels = substream->runtime->channels;
589
7ceba75f 590 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
5dab11d8 591
b556290f 592 ca = had_channel_allocation(intelhaddata, channels);
964ca808
PLB
593 if (intelhaddata->dp_output) {
594 info_frame = DP_INFO_FRAME_WORD1;
36ed3466 595 frame2.regval = (substream->runtime->channels - 1) | (ca << 24);
964ca808
PLB
596 } else {
597 info_frame = HDMI_INFO_FRAME_WORD1;
7ceba75f 598 frame2.regx.chnl_cnt = substream->runtime->channels - 1;
36ed3466 599 frame3.regx.chnl_alloc = ca;
5dab11d8 600
2e52f5e5 601 /* Calculte the byte wide checksum for all valid DIP words */
964ca808 602 for (i = 0; i < BYTES_PER_WORD; i++)
7ceba75f 603 checksum += (info_frame >> (i * 8)) & 0xff;
964ca808 604 for (i = 0; i < BYTES_PER_WORD; i++)
7ceba75f 605 checksum += (frame2.regval >> (i * 8)) & 0xff;
964ca808 606 for (i = 0; i < BYTES_PER_WORD; i++)
7ceba75f 607 checksum += (frame3.regval >> (i * 8)) & 0xff;
5dab11d8 608
7ceba75f 609 frame2.regx.chksum = -(checksum);
964ca808 610 }
5dab11d8 611
4151ee84 612 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, info_frame);
7ceba75f
TI
613 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame2.regval);
614 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame3.regval);
5dab11d8
JA
615
616 /* program remaining DIP words with zero */
617 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
4151ee84 618 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, 0x0);
5dab11d8 619
7ceba75f
TI
620 ctrl_state.regx.dip_freq = 1;
621 ctrl_state.regx.dip_en_sta = 1;
622 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
5dab11d8
JA
623}
624
964ca808
PLB
625static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
626{
627 u32 maud_val;
628
2e52f5e5 629 /* Select maud according to DP 1.2 spec */
964ca808
PLB
630 if (link_rate == DP_2_7_GHZ) {
631 switch (aud_samp_freq) {
632 case AUD_SAMPLE_RATE_32:
633 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
634 break;
635
636 case AUD_SAMPLE_RATE_44_1:
637 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
638 break;
639
640 case AUD_SAMPLE_RATE_48:
641 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
642 break;
643
644 case AUD_SAMPLE_RATE_88_2:
645 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
646 break;
647
648 case AUD_SAMPLE_RATE_96:
649 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
650 break;
651
652 case AUD_SAMPLE_RATE_176_4:
653 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
654 break;
655
656 case HAD_MAX_RATE:
657 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
658 break;
659
660 default:
661 maud_val = -EINVAL;
662 break;
663 }
664 } else if (link_rate == DP_1_62_GHZ) {
665 switch (aud_samp_freq) {
666 case AUD_SAMPLE_RATE_32:
667 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
668 break;
669
670 case AUD_SAMPLE_RATE_44_1:
671 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
672 break;
673
674 case AUD_SAMPLE_RATE_48:
675 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
676 break;
677
678 case AUD_SAMPLE_RATE_88_2:
679 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
680 break;
681
682 case AUD_SAMPLE_RATE_96:
683 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
684 break;
685
686 case AUD_SAMPLE_RATE_176_4:
687 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
688 break;
689
690 case HAD_MAX_RATE:
691 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
692 break;
693
694 default:
695 maud_val = -EINVAL;
696 break;
697 }
698 } else
699 maud_val = -EINVAL;
700
701 return maud_val;
702}
703
76296ef0 704/*
44684f61 705 * Program HDMI audio CTS value
5dab11d8
JA
706 *
707 * @aud_samp_freq: sampling frequency of audio data
708 * @tmds: sampling frequency of the display data
b556290f 709 * @link_rate: DP link rate
5dab11d8 710 * @n_param: N value, depends on aud_samp_freq
b556290f 711 * @intelhaddata: substream private data
5dab11d8
JA
712 *
713 * Program CTS register based on the audio and display sampling frequency
714 */
b556290f
TI
715static void had_prog_cts(u32 aud_samp_freq, u32 tmds, u32 link_rate,
716 u32 n_param, struct snd_intelhad *intelhaddata)
5dab11d8
JA
717{
718 u32 cts_val;
719 u64 dividend, divisor;
720
964ca808
PLB
721 if (intelhaddata->dp_output) {
722 /* Substitute cts_val with Maud according to DP 1.2 spec*/
723 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
724 } else {
725 /* Calculate CTS according to HDMI 1.3a spec*/
726 dividend = (u64)tmds * n_param*1000;
727 divisor = 128 * aud_samp_freq;
728 cts_val = div64_u64(dividend, divisor);
729 }
c75b0476 730 dev_dbg(intelhaddata->dev, "TMDS value=%d, N value=%d, CTS Value=%d\n",
964ca808 731 tmds, n_param, cts_val);
79dda75a 732 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
5dab11d8
JA
733}
734
735static int had_calculate_n_value(u32 aud_samp_freq)
736{
2e52f5e5 737 int n_val;
5dab11d8
JA
738
739 /* Select N according to HDMI 1.3a spec*/
740 switch (aud_samp_freq) {
741 case AUD_SAMPLE_RATE_32:
742 n_val = 4096;
2e52f5e5 743 break;
5dab11d8
JA
744
745 case AUD_SAMPLE_RATE_44_1:
746 n_val = 6272;
2e52f5e5 747 break;
5dab11d8
JA
748
749 case AUD_SAMPLE_RATE_48:
750 n_val = 6144;
2e52f5e5 751 break;
5dab11d8
JA
752
753 case AUD_SAMPLE_RATE_88_2:
754 n_val = 12544;
2e52f5e5 755 break;
5dab11d8
JA
756
757 case AUD_SAMPLE_RATE_96:
758 n_val = 12288;
2e52f5e5 759 break;
5dab11d8
JA
760
761 case AUD_SAMPLE_RATE_176_4:
762 n_val = 25088;
2e52f5e5 763 break;
5dab11d8
JA
764
765 case HAD_MAX_RATE:
766 n_val = 24576;
2e52f5e5 767 break;
5dab11d8
JA
768
769 default:
770 n_val = -EINVAL;
2e52f5e5 771 break;
5dab11d8
JA
772 }
773 return n_val;
774}
775
76296ef0 776/*
44684f61 777 * Program HDMI audio N value
5dab11d8
JA
778 *
779 * @aud_samp_freq: sampling frequency of audio data
780 * @n_param: N value, depends on aud_samp_freq
b556290f 781 * @intelhaddata: substream private data
5dab11d8
JA
782 *
783 * This function is called in the prepare callback.
784 * It programs based on the audio and display sampling frequency
785 */
b556290f
TI
786static int had_prog_n(u32 aud_samp_freq, u32 *n_param,
787 struct snd_intelhad *intelhaddata)
5dab11d8 788{
2e52f5e5 789 int n_val;
5dab11d8 790
964ca808
PLB
791 if (intelhaddata->dp_output) {
792 /*
793 * According to DP specs, Maud and Naud values hold
794 * a relationship, which is stated as:
795 * Maud/Naud = 512 * fs / f_LS_Clk
796 * where, fs is the sampling frequency of the audio stream
797 * and Naud is 32768 for Async clock.
798 */
799
800 n_val = DP_NAUD_VAL;
801 } else
802 n_val = had_calculate_n_value(aud_samp_freq);
5dab11d8
JA
803
804 if (n_val < 0)
805 return n_val;
806
79dda75a 807 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
5dab11d8
JA
808 *n_param = n_val;
809 return 0;
810}
811
e1b239f3
TI
812/*
813 * PCM ring buffer handling
814 *
815 * The hardware provides a ring buffer with the fixed 4 buffer descriptors
816 * (BDs). The driver maps these 4 BDs onto the PCM ring buffer. The mapping
817 * moves at each period elapsed. The below illustrates how it works:
818 *
819 * At time=0
820 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
821 * BD | 0 | 1 | 2 | 3 |
822 *
823 * At time=1 (period elapsed)
824 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
825 * BD | 1 | 2 | 3 | 0 |
826 *
827 * At time=2 (second period elapsed)
828 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
829 * BD | 2 | 3 | 0 | 1 |
830 *
831 * The bd_head field points to the index of the BD to be read. It's also the
832 * position to be filled at next. The pcm_head and the pcm_filled fields
833 * point to the indices of the current position and of the next position to
834 * be filled, respectively. For PCM buffer there are both _head and _filled
835 * because they may be difference when nperiods > 4. For example, in the
836 * example above at t=1, bd_head=1 and pcm_head=1 while pcm_filled=5:
837 *
838 * pcm_head (=1) --v v-- pcm_filled (=5)
839 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
840 * BD | 1 | 2 | 3 | 0 |
841 * bd_head (=1) --^ ^-- next to fill (= bd_head)
842 *
843 * For nperiods < 4, the remaining BDs out of 4 are marked as invalid, so that
844 * the hardware skips those BDs in the loop.
8d48c016
TI
845 *
846 * An exceptional setup is the case with nperiods=1. Since we have to update
847 * BDs after finishing one BD processing, we'd need at least two BDs, where
848 * both BDs point to the same content, the same address, the same size of the
849 * whole PCM buffer.
e1b239f3
TI
850 */
851
852#define AUD_BUF_ADDR(x) (AUD_BUF_A_ADDR + (x) * HAD_REG_WIDTH)
853#define AUD_BUF_LEN(x) (AUD_BUF_A_LENGTH + (x) * HAD_REG_WIDTH)
854
855/* Set up a buffer descriptor at the "filled" position */
856static void had_prog_bd(struct snd_pcm_substream *substream,
857 struct snd_intelhad *intelhaddata)
858{
859 int idx = intelhaddata->bd_head;
860 int ofs = intelhaddata->pcmbuf_filled * intelhaddata->period_bytes;
861 u32 addr = substream->runtime->dma_addr + ofs;
862
e8de9859
TI
863 addr |= AUD_BUF_VALID;
864 if (!substream->runtime->no_period_wakeup)
865 addr |= AUD_BUF_INTR_EN;
e1b239f3
TI
866 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), addr);
867 had_write_register(intelhaddata, AUD_BUF_LEN(idx),
868 intelhaddata->period_bytes);
869
870 /* advance the indices to the next */
871 intelhaddata->bd_head++;
872 intelhaddata->bd_head %= intelhaddata->num_bds;
873 intelhaddata->pcmbuf_filled++;
874 intelhaddata->pcmbuf_filled %= substream->runtime->periods;
875}
876
877/* invalidate a buffer descriptor with the given index */
878static void had_invalidate_bd(struct snd_intelhad *intelhaddata,
879 int idx)
880{
881 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), 0);
882 had_write_register(intelhaddata, AUD_BUF_LEN(idx), 0);
883}
884
885/* Initial programming of ring buffer */
886static void had_init_ringbuf(struct snd_pcm_substream *substream,
887 struct snd_intelhad *intelhaddata)
888{
889 struct snd_pcm_runtime *runtime = substream->runtime;
890 int i, num_periods;
891
892 num_periods = runtime->periods;
893 intelhaddata->num_bds = min(num_periods, HAD_NUM_OF_RING_BUFS);
8d48c016
TI
894 /* set the minimum 2 BDs for num_periods=1 */
895 intelhaddata->num_bds = max(intelhaddata->num_bds, 2U);
e1b239f3
TI
896 intelhaddata->period_bytes =
897 frames_to_bytes(runtime, runtime->period_size);
898 WARN_ON(intelhaddata->period_bytes & 0x3f);
899
900 intelhaddata->bd_head = 0;
901 intelhaddata->pcmbuf_head = 0;
902 intelhaddata->pcmbuf_filled = 0;
903
904 for (i = 0; i < HAD_NUM_OF_RING_BUFS; i++) {
8d48c016 905 if (i < intelhaddata->num_bds)
e1b239f3
TI
906 had_prog_bd(substream, intelhaddata);
907 else /* invalidate the rest */
908 had_invalidate_bd(intelhaddata, i);
909 }
910
911 intelhaddata->bd_head = 0; /* reset at head again before starting */
912}
913
914/* process a bd, advance to the next */
915static void had_advance_ringbuf(struct snd_pcm_substream *substream,
916 struct snd_intelhad *intelhaddata)
917{
918 int num_periods = substream->runtime->periods;
919
920 /* reprogram the next buffer */
921 had_prog_bd(substream, intelhaddata);
922
923 /* proceed to next */
924 intelhaddata->pcmbuf_head++;
925 intelhaddata->pcmbuf_head %= num_periods;
926}
927
928/* process the current BD(s);
929 * returns the current PCM buffer byte position, or -EPIPE for underrun.
930 */
931static int had_process_ringbuf(struct snd_pcm_substream *substream,
932 struct snd_intelhad *intelhaddata)
933{
934 int len, processed;
935 unsigned long flags;
936
937 processed = 0;
938 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
939 for (;;) {
940 /* get the remaining bytes on the buffer */
941 had_read_register(intelhaddata,
942 AUD_BUF_LEN(intelhaddata->bd_head),
943 &len);
944 if (len < 0 || len > intelhaddata->period_bytes) {
945 dev_dbg(intelhaddata->dev, "Invalid buf length %d\n",
946 len);
947 len = -EPIPE;
948 goto out;
949 }
950
951 if (len > 0) /* OK, this is the current buffer */
952 break;
953
954 /* len=0 => already empty, check the next buffer */
955 if (++processed >= intelhaddata->num_bds) {
956 len = -EPIPE; /* all empty? - report underrun */
957 goto out;
958 }
959 had_advance_ringbuf(substream, intelhaddata);
960 }
961
962 len = intelhaddata->period_bytes - len;
963 len += intelhaddata->period_bytes * intelhaddata->pcmbuf_head;
964 out:
965 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
966 return len;
967}
968
969/* called from irq handler */
970static void had_process_buffer_done(struct snd_intelhad *intelhaddata)
971{
972 struct snd_pcm_substream *substream;
973
e1b239f3
TI
974 substream = had_substream_get(intelhaddata);
975 if (!substream)
976 return; /* no stream? - bail out */
977
be9a2e93
TI
978 if (!intelhaddata->connected) {
979 snd_pcm_stop_xrun(substream);
980 goto out; /* disconnected? - bail out */
981 }
982
e1b239f3
TI
983 /* process or stop the stream */
984 if (had_process_ringbuf(substream, intelhaddata) < 0)
985 snd_pcm_stop_xrun(substream);
986 else
987 snd_pcm_period_elapsed(substream);
988
be9a2e93 989 out:
e1b239f3
TI
990 had_substream_put(intelhaddata);
991}
992
e1b239f3
TI
993/*
994 * The interrupt status 'sticky' bits might not be cleared by
995 * setting '1' to that bit once...
996 */
997static void wait_clear_underrun_bit(struct snd_intelhad *intelhaddata)
998{
999 int i;
1000 u32 val;
1001
e2acecf2 1002 for (i = 0; i < 100; i++) {
e1b239f3
TI
1003 /* clear bit30, 31 AUD_HDMI_STATUS */
1004 had_read_register(intelhaddata, AUD_HDMI_STATUS, &val);
77531bee 1005 if (!(val & AUD_HDMI_STATUS_MASK_UNDERRUN))
e1b239f3 1006 return;
e2acecf2
TI
1007 udelay(100);
1008 cond_resched();
e1b239f3
TI
1009 had_write_register(intelhaddata, AUD_HDMI_STATUS, val);
1010 }
1011 dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n");
1012}
1013
e2acecf2
TI
1014/* Perform some reset procedure but only when need_reset is set;
1015 * this is called from prepare or hw_free callbacks once after trigger STOP
1016 * or underrun has been processed in order to settle down the h/w state.
1017 */
1018static void had_do_reset(struct snd_intelhad *intelhaddata)
5dab11d8 1019{
28ed125b 1020 if (!intelhaddata->need_reset || !intelhaddata->connected)
e2acecf2 1021 return;
5dab11d8 1022
5dab11d8 1023 /* Reset buffer pointers */
f4566aa1 1024 had_reset_audio(intelhaddata);
e1b239f3 1025 wait_clear_underrun_bit(intelhaddata);
e2acecf2
TI
1026 intelhaddata->need_reset = false;
1027}
e1b239f3 1028
e2acecf2
TI
1029/* called from irq handler */
1030static void had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
1031{
1032 struct snd_pcm_substream *substream;
e1b239f3
TI
1033
1034 /* Report UNDERRUN error to above layers */
1035 substream = had_substream_get(intelhaddata);
1036 if (substream) {
1037 snd_pcm_stop_xrun(substream);
1038 had_substream_put(intelhaddata);
1039 }
e2acecf2 1040 intelhaddata->need_reset = true;
5dab11d8
JA
1041}
1042
2e52f5e5 1043/*
44684f61 1044 * ALSA PCM open callback
5dab11d8 1045 */
b556290f 1046static int had_pcm_open(struct snd_pcm_substream *substream)
5dab11d8
JA
1047{
1048 struct snd_intelhad *intelhaddata;
1049 struct snd_pcm_runtime *runtime;
5dab11d8
JA
1050 int retval;
1051
5dab11d8 1052 intelhaddata = snd_pcm_substream_chip(substream);
5dab11d8
JA
1053 runtime = substream->runtime;
1054
182cdf23 1055 pm_runtime_get_sync(intelhaddata->dev);
5dab11d8 1056
5dab11d8 1057 /* set the runtime hw parameter with local snd_pcm_hardware struct */
b556290f 1058 runtime->hw = had_pcm_hardware;
5dab11d8 1059
5dab11d8
JA
1060 retval = snd_pcm_hw_constraint_integer(runtime,
1061 SNDRV_PCM_HW_PARAM_PERIODS);
1062 if (retval < 0)
fa5dfe6a 1063 goto error;
5dab11d8
JA
1064
1065 /* Make sure, that the period size is always aligned
1066 * 64byte boundary
1067 */
1068 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1069 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
73997b05 1070 if (retval < 0)
fa5dfe6a 1071 goto error;
5dab11d8 1072
85bd8748
TI
1073 retval = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1074 if (retval < 0)
1075 goto error;
1076
73997b05 1077 /* expose PCM substream */
313d9f28
TI
1078 spin_lock_irq(&intelhaddata->had_spinlock);
1079 intelhaddata->stream_info.substream = substream;
1080 intelhaddata->stream_info.substream_refcount++;
1081 spin_unlock_irq(&intelhaddata->had_spinlock);
1082
5dab11d8 1083 return retval;
fa5dfe6a 1084 error:
5dab11d8 1085 pm_runtime_put(intelhaddata->dev);
5dab11d8
JA
1086 return retval;
1087}
1088
2e52f5e5 1089/*
44684f61 1090 * ALSA PCM close callback
5dab11d8 1091 */
b556290f 1092static int had_pcm_close(struct snd_pcm_substream *substream)
5dab11d8
JA
1093{
1094 struct snd_intelhad *intelhaddata;
5dab11d8 1095
5dab11d8 1096 intelhaddata = snd_pcm_substream_chip(substream);
5dab11d8 1097
73997b05 1098 /* unreference and sync with the pending PCM accesses */
313d9f28
TI
1099 spin_lock_irq(&intelhaddata->had_spinlock);
1100 intelhaddata->stream_info.substream = NULL;
1101 intelhaddata->stream_info.substream_refcount--;
1102 while (intelhaddata->stream_info.substream_refcount > 0) {
1103 spin_unlock_irq(&intelhaddata->had_spinlock);
1104 cpu_relax();
1105 spin_lock_irq(&intelhaddata->had_spinlock);
1106 }
1107 spin_unlock_irq(&intelhaddata->had_spinlock);
5dab11d8 1108
5dab11d8
JA
1109 pm_runtime_put(intelhaddata->dev);
1110 return 0;
1111}
1112
2e52f5e5 1113/*
44684f61 1114 * ALSA PCM hw_params callback
5dab11d8 1115 */
b556290f
TI
1116static int had_pcm_hw_params(struct snd_pcm_substream *substream,
1117 struct snd_pcm_hw_params *hw_params)
5dab11d8 1118{
c75b0476 1119 struct snd_intelhad *intelhaddata;
5dab11d8
JA
1120 unsigned long addr;
1121 int pages, buf_size, retval;
1122
c75b0476 1123 intelhaddata = snd_pcm_substream_chip(substream);
5dab11d8
JA
1124 buf_size = params_buffer_bytes(hw_params);
1125 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1126 if (retval < 0)
1127 return retval;
c75b0476
TI
1128 dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
1129 __func__, buf_size);
5dab11d8
JA
1130 /* mark the pages as uncached region */
1131 addr = (unsigned long) substream->runtime->dma_area;
1132 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1133 retval = set_memory_uc(addr, pages);
1134 if (retval) {
c75b0476
TI
1135 dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
1136 retval);
5dab11d8
JA
1137 return retval;
1138 }
1139 memset(substream->runtime->dma_area, 0, buf_size);
1140
1141 return retval;
1142}
1143
2e52f5e5 1144/*
44684f61 1145 * ALSA PCM hw_free callback
5dab11d8 1146 */
b556290f 1147static int had_pcm_hw_free(struct snd_pcm_substream *substream)
5dab11d8 1148{
e2acecf2 1149 struct snd_intelhad *intelhaddata;
5dab11d8
JA
1150 unsigned long addr;
1151 u32 pages;
1152
e2acecf2
TI
1153 intelhaddata = snd_pcm_substream_chip(substream);
1154 had_do_reset(intelhaddata);
1155
5dab11d8
JA
1156 /* mark back the pages as cached/writeback region before the free */
1157 if (substream->runtime->dma_area != NULL) {
1158 addr = (unsigned long) substream->runtime->dma_area;
1159 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1160 PAGE_SIZE;
1161 set_memory_wb(addr, pages);
1162 return snd_pcm_lib_free_pages(substream);
1163 }
1164 return 0;
1165}
1166
2e52f5e5 1167/*
44684f61 1168 * ALSA PCM trigger callback
5dab11d8 1169 */
b556290f 1170static int had_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
5dab11d8 1171{
da864809 1172 int retval = 0;
5dab11d8 1173 struct snd_intelhad *intelhaddata;
5dab11d8 1174
5dab11d8 1175 intelhaddata = snd_pcm_substream_chip(substream);
5dab11d8 1176
df42cb49 1177 spin_lock(&intelhaddata->had_spinlock);
5dab11d8
JA
1178 switch (cmd) {
1179 case SNDRV_PCM_TRIGGER_START:
182cdf23
TI
1180 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1181 case SNDRV_PCM_TRIGGER_RESUME:
5dab11d8 1182 /* Enable Audio */
075a1d46 1183 had_ack_irqs(intelhaddata); /* FIXME: do we need this? */
40ce4b5d 1184 had_enable_audio(intelhaddata, true);
5dab11d8
JA
1185 break;
1186
1187 case SNDRV_PCM_TRIGGER_STOP:
182cdf23 1188 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
5dab11d8 1189 /* Disable Audio */
40ce4b5d 1190 had_enable_audio(intelhaddata, false);
e2acecf2 1191 intelhaddata->need_reset = true;
5dab11d8
JA
1192 break;
1193
1194 default:
1195 retval = -EINVAL;
1196 }
df42cb49 1197 spin_unlock(&intelhaddata->had_spinlock);
5dab11d8
JA
1198 return retval;
1199}
1200
2e52f5e5 1201/*
44684f61 1202 * ALSA PCM prepare callback
5dab11d8 1203 */
b556290f 1204static int had_pcm_prepare(struct snd_pcm_substream *substream)
5dab11d8
JA
1205{
1206 int retval;
1207 u32 disp_samp_freq, n_param;
964ca808 1208 u32 link_rate = 0;
5dab11d8
JA
1209 struct snd_intelhad *intelhaddata;
1210 struct snd_pcm_runtime *runtime;
5dab11d8 1211
5dab11d8
JA
1212 intelhaddata = snd_pcm_substream_chip(substream);
1213 runtime = substream->runtime;
5dab11d8 1214
c75b0476 1215 dev_dbg(intelhaddata->dev, "period_size=%d\n",
5dab11d8 1216 (int)frames_to_bytes(runtime, runtime->period_size));
c75b0476
TI
1217 dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods);
1218 dev_dbg(intelhaddata->dev, "buffer_size=%d\n",
1219 (int)snd_pcm_lib_buffer_bytes(substream));
1220 dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
1221 dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
5dab11d8 1222
e2acecf2
TI
1223 had_do_reset(intelhaddata);
1224
5dab11d8 1225 /* Get N value in KHz */
da864809 1226 disp_samp_freq = intelhaddata->tmds_clock_speed;
5dab11d8 1227
b556290f 1228 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
5dab11d8 1229 if (retval) {
c75b0476
TI
1230 dev_err(intelhaddata->dev,
1231 "programming N value failed %#x\n", retval);
5dab11d8
JA
1232 goto prep_end;
1233 }
964ca808
PLB
1234
1235 if (intelhaddata->dp_output)
da864809 1236 link_rate = intelhaddata->link_rate;
964ca808 1237
b556290f
TI
1238 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1239 n_param, intelhaddata);
5dab11d8 1240
b556290f 1241 had_prog_dip(substream, intelhaddata);
5dab11d8 1242
b556290f 1243 retval = had_init_audio_ctrl(substream, intelhaddata);
5dab11d8
JA
1244
1245 /* Prog buffer address */
e1b239f3 1246 had_init_ringbuf(substream, intelhaddata);
5dab11d8
JA
1247
1248 /*
1249 * Program channel mapping in following order:
1250 * FL, FR, C, LFE, RL, RR
1251 */
1252
79dda75a 1253 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
5dab11d8
JA
1254
1255prep_end:
1256 return retval;
1257}
1258
2e52f5e5 1259/*
44684f61 1260 * ALSA PCM pointer callback
5dab11d8 1261 */
b556290f 1262static snd_pcm_uframes_t had_pcm_pointer(struct snd_pcm_substream *substream)
5dab11d8
JA
1263{
1264 struct snd_intelhad *intelhaddata;
e1b239f3 1265 int len;
5dab11d8 1266
5dab11d8
JA
1267 intelhaddata = snd_pcm_substream_chip(substream);
1268
91b0cb0c 1269 if (!intelhaddata->connected)
79f439ea
TI
1270 return SNDRV_PCM_POS_XRUN;
1271
e1b239f3
TI
1272 len = had_process_ringbuf(substream, intelhaddata);
1273 if (len < 0)
1274 return SNDRV_PCM_POS_XRUN;
8d48c016
TI
1275 len = bytes_to_frames(substream->runtime, len);
1276 /* wrapping may happen when periods=1 */
1277 len %= substream->runtime->buffer_size;
1278 return len;
5dab11d8
JA
1279}
1280
2e52f5e5 1281/*
44684f61 1282 * ALSA PCM mmap callback
5dab11d8 1283 */
b556290f
TI
1284static int had_pcm_mmap(struct snd_pcm_substream *substream,
1285 struct vm_area_struct *vma)
5dab11d8 1286{
5dab11d8
JA
1287 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1288 return remap_pfn_range(vma, vma->vm_start,
1289 substream->dma_buffer.addr >> PAGE_SHIFT,
1290 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1291}
1292
73997b05
TI
1293/*
1294 * ALSA PCM ops
1295 */
b556290f
TI
1296static const struct snd_pcm_ops had_pcm_ops = {
1297 .open = had_pcm_open,
1298 .close = had_pcm_close,
73997b05 1299 .ioctl = snd_pcm_lib_ioctl,
b556290f
TI
1300 .hw_params = had_pcm_hw_params,
1301 .hw_free = had_pcm_hw_free,
1302 .prepare = had_pcm_prepare,
1303 .trigger = had_pcm_trigger,
1304 .pointer = had_pcm_pointer,
1305 .mmap = had_pcm_mmap,
73997b05
TI
1306};
1307
8f8d1d7f 1308/* process mode change of the running stream; called in mutex */
b556290f 1309static int had_process_mode_change(struct snd_intelhad *intelhaddata)
5dab11d8 1310{
da864809 1311 struct snd_pcm_substream *substream;
5dab11d8
JA
1312 int retval = 0;
1313 u32 disp_samp_freq, n_param;
964ca808 1314 u32 link_rate = 0;
5dab11d8 1315
313d9f28
TI
1316 substream = had_substream_get(intelhaddata);
1317 if (!substream)
da864809 1318 return 0;
5dab11d8
JA
1319
1320 /* Disable Audio */
40ce4b5d 1321 had_enable_audio(intelhaddata, false);
5dab11d8
JA
1322
1323 /* Update CTS value */
da864809 1324 disp_samp_freq = intelhaddata->tmds_clock_speed;
5dab11d8 1325
b556290f 1326 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
5dab11d8 1327 if (retval) {
c75b0476
TI
1328 dev_err(intelhaddata->dev,
1329 "programming N value failed %#x\n", retval);
5dab11d8
JA
1330 goto out;
1331 }
964ca808
PLB
1332
1333 if (intelhaddata->dp_output)
da864809 1334 link_rate = intelhaddata->link_rate;
964ca808 1335
b556290f
TI
1336 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1337 n_param, intelhaddata);
5dab11d8
JA
1338
1339 /* Enable Audio */
40ce4b5d 1340 had_enable_audio(intelhaddata, true);
5dab11d8
JA
1341
1342out:
313d9f28 1343 had_substream_put(intelhaddata);
5dab11d8
JA
1344 return retval;
1345}
1346
8f8d1d7f 1347/* process hot plug, called from wq with mutex locked */
0e9c67d7 1348static void had_process_hot_plug(struct snd_intelhad *intelhaddata)
372d855f 1349{
372d855f 1350 struct snd_pcm_substream *substream;
372d855f 1351
bcce775c 1352 spin_lock_irq(&intelhaddata->had_spinlock);
91b0cb0c 1353 if (intelhaddata->connected) {
c75b0476 1354 dev_dbg(intelhaddata->dev, "Device already connected\n");
bcce775c 1355 spin_unlock_irq(&intelhaddata->had_spinlock);
0e9c67d7 1356 return;
372d855f 1357 }
0e9c67d7 1358
91b0cb0c 1359 intelhaddata->connected = true;
c75b0476
TI
1360 dev_dbg(intelhaddata->dev,
1361 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
372d855f 1362 __func__, __LINE__);
bcce775c 1363 spin_unlock_irq(&intelhaddata->had_spinlock);
372d855f 1364
2d42c033
TI
1365 had_build_channel_allocation_map(intelhaddata);
1366
1367 /* Report to above ALSA layer */
313d9f28 1368 substream = had_substream_get(intelhaddata);
372d855f 1369 if (substream) {
5def9019 1370 snd_pcm_stop_xrun(substream);
313d9f28 1371 had_substream_put(intelhaddata);
372d855f
TI
1372 }
1373
b9bacf27 1374 snd_jack_report(intelhaddata->jack, SND_JACK_AVOUT);
372d855f
TI
1375}
1376
8f8d1d7f 1377/* process hot unplug, called from wq with mutex locked */
0e9c67d7 1378static void had_process_hot_unplug(struct snd_intelhad *intelhaddata)
372d855f 1379{
313d9f28 1380 struct snd_pcm_substream *substream;
372d855f 1381
bcce775c 1382 spin_lock_irq(&intelhaddata->had_spinlock);
91b0cb0c 1383 if (!intelhaddata->connected) {
c75b0476 1384 dev_dbg(intelhaddata->dev, "Device already disconnected\n");
bcce775c 1385 spin_unlock_irq(&intelhaddata->had_spinlock);
2d42c033 1386 return;
372d855f 1387
372d855f
TI
1388 }
1389
0e9c67d7 1390 /* Disable Audio */
40ce4b5d 1391 had_enable_audio(intelhaddata, false);
0e9c67d7 1392
91b0cb0c 1393 intelhaddata->connected = false;
c75b0476
TI
1394 dev_dbg(intelhaddata->dev,
1395 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
372d855f 1396 __func__, __LINE__);
313d9f28 1397 spin_unlock_irq(&intelhaddata->had_spinlock);
372d855f 1398
2d42c033
TI
1399 kfree(intelhaddata->chmap->chmap);
1400 intelhaddata->chmap->chmap = NULL;
1401
372d855f 1402 /* Report to above ALSA layer */
2d42c033
TI
1403 substream = had_substream_get(intelhaddata);
1404 if (substream) {
5def9019 1405 snd_pcm_stop_xrun(substream);
2d42c033
TI
1406 had_substream_put(intelhaddata);
1407 }
372d855f 1408
b9bacf27 1409 snd_jack_report(intelhaddata->jack, 0);
372d855f
TI
1410}
1411
73997b05
TI
1412/*
1413 * ALSA iec958 and ELD controls
1414 */
5dab11d8 1415
5dab11d8
JA
1416static int had_iec958_info(struct snd_kcontrol *kcontrol,
1417 struct snd_ctl_elem_info *uinfo)
1418{
1419 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1420 uinfo->count = 1;
1421 return 0;
1422}
1423
1424static int had_iec958_get(struct snd_kcontrol *kcontrol,
1425 struct snd_ctl_elem_value *ucontrol)
1426{
1427 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1428
8f8d1d7f 1429 mutex_lock(&intelhaddata->mutex);
5dab11d8
JA
1430 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1431 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1432 ucontrol->value.iec958.status[2] =
1433 (intelhaddata->aes_bits >> 16) & 0xff;
1434 ucontrol->value.iec958.status[3] =
1435 (intelhaddata->aes_bits >> 24) & 0xff;
8f8d1d7f 1436 mutex_unlock(&intelhaddata->mutex);
5dab11d8
JA
1437 return 0;
1438}
372d855f 1439
5dab11d8
JA
1440static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1441 struct snd_ctl_elem_value *ucontrol)
1442{
1443 ucontrol->value.iec958.status[0] = 0xff;
1444 ucontrol->value.iec958.status[1] = 0xff;
1445 ucontrol->value.iec958.status[2] = 0xff;
1446 ucontrol->value.iec958.status[3] = 0xff;
1447 return 0;
1448}
372d855f 1449
5dab11d8
JA
1450static int had_iec958_put(struct snd_kcontrol *kcontrol,
1451 struct snd_ctl_elem_value *ucontrol)
1452{
1453 unsigned int val;
1454 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
8f8d1d7f 1455 int changed = 0;
5dab11d8 1456
5dab11d8
JA
1457 val = (ucontrol->value.iec958.status[0] << 0) |
1458 (ucontrol->value.iec958.status[1] << 8) |
1459 (ucontrol->value.iec958.status[2] << 16) |
1460 (ucontrol->value.iec958.status[3] << 24);
8f8d1d7f 1461 mutex_lock(&intelhaddata->mutex);
5dab11d8
JA
1462 if (intelhaddata->aes_bits != val) {
1463 intelhaddata->aes_bits = val;
8f8d1d7f 1464 changed = 1;
5dab11d8 1465 }
8f8d1d7f
TI
1466 mutex_unlock(&intelhaddata->mutex);
1467 return changed;
5dab11d8
JA
1468}
1469
4aedb946
TI
1470static int had_ctl_eld_info(struct snd_kcontrol *kcontrol,
1471 struct snd_ctl_elem_info *uinfo)
1472{
1473 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1474 uinfo->count = HDMI_MAX_ELD_BYTES;
1475 return 0;
1476}
1477
1478static int had_ctl_eld_get(struct snd_kcontrol *kcontrol,
1479 struct snd_ctl_elem_value *ucontrol)
1480{
1481 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1482
1483 mutex_lock(&intelhaddata->mutex);
1484 memcpy(ucontrol->value.bytes.data, intelhaddata->eld,
1485 HDMI_MAX_ELD_BYTES);
1486 mutex_unlock(&intelhaddata->mutex);
1487 return 0;
1488}
5dab11d8 1489
73997b05 1490static const struct snd_kcontrol_new had_controls[] = {
4aedb946
TI
1491 {
1492 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1493 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1494 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1495 .info = had_iec958_info, /* shared */
1496 .get = had_iec958_mask_get,
1497 },
1498 {
1499 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1500 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1501 .info = had_iec958_info,
1502 .get = had_iec958_get,
1503 .put = had_iec958_put,
1504 },
1505 {
1506 .access = (SNDRV_CTL_ELEM_ACCESS_READ |
1507 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1508 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1509 .name = "ELD",
1510 .info = had_ctl_eld_info,
1511 .get = had_ctl_eld_get,
1512 },
5dab11d8
JA
1513};
1514
73997b05
TI
1515/*
1516 * audio interrupt handler
1517 */
da864809
TI
1518static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id)
1519{
1520 struct snd_intelhad *ctx = dev_id;
28ed125b 1521 u32 audio_stat;
da864809 1522
28ed125b
TI
1523 /* use raw register access to ack IRQs even while disconnected */
1524 audio_stat = had_read_register_raw(ctx, AUD_HDMI_STATUS);
da864809
TI
1525
1526 if (audio_stat & HDMI_AUDIO_UNDERRUN) {
28ed125b
TI
1527 had_write_register_raw(ctx, AUD_HDMI_STATUS,
1528 HDMI_AUDIO_UNDERRUN);
da864809
TI
1529 had_process_buffer_underrun(ctx);
1530 }
1531
1532 if (audio_stat & HDMI_AUDIO_BUFFER_DONE) {
28ed125b
TI
1533 had_write_register_raw(ctx, AUD_HDMI_STATUS,
1534 HDMI_AUDIO_BUFFER_DONE);
da864809
TI
1535 had_process_buffer_done(ctx);
1536 }
1537
1538 return IRQ_HANDLED;
1539}
1540
73997b05
TI
1541/*
1542 * monitor plug/unplug notification from i915; just kick off the work
1543 */
da864809
TI
1544static void notify_audio_lpe(struct platform_device *pdev)
1545{
1546 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
da864809 1547
99b2ab9d
TI
1548 schedule_work(&ctx->hdmi_audio_wq);
1549}
da864809 1550
73997b05 1551/* the work to handle monitor hot plug/unplug */
99b2ab9d
TI
1552static void had_audio_wq(struct work_struct *work)
1553{
1554 struct snd_intelhad *ctx =
1555 container_of(work, struct snd_intelhad, hdmi_audio_wq);
1556 struct intel_hdmi_lpe_audio_pdata *pdata = ctx->dev->platform_data;
da864809 1557
182cdf23 1558 pm_runtime_get_sync(ctx->dev);
8f8d1d7f 1559 mutex_lock(&ctx->mutex);
99b2ab9d
TI
1560 if (!pdata->hdmi_connected) {
1561 dev_dbg(ctx->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG\n",
1562 __func__);
4aedb946 1563 memset(ctx->eld, 0, sizeof(ctx->eld)); /* clear the old ELD */
0e9c67d7 1564 had_process_hot_unplug(ctx);
da864809
TI
1565 } else {
1566 struct intel_hdmi_lpe_audio_eld *eld = &pdata->eld;
1567
0e9c67d7
TI
1568 dev_dbg(ctx->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
1569 __func__, eld->port_id, pdata->tmds_clock_speed);
1570
da864809
TI
1571 switch (eld->pipe_id) {
1572 case 0:
1573 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1574 break;
1575 case 1:
1576 ctx->had_config_offset = AUDIO_HDMI_CONFIG_B;
1577 break;
1578 case 2:
1579 ctx->had_config_offset = AUDIO_HDMI_CONFIG_C;
1580 break;
1581 default:
99b2ab9d 1582 dev_dbg(ctx->dev, "Invalid pipe %d\n",
da864809
TI
1583 eld->pipe_id);
1584 break;
1585 }
1586
df0435db 1587 memcpy(ctx->eld, eld->eld_data, sizeof(ctx->eld));
da864809 1588
0e9c67d7
TI
1589 ctx->dp_output = pdata->dp_output;
1590 ctx->tmds_clock_speed = pdata->tmds_clock_speed;
1591 ctx->link_rate = pdata->link_rate;
da864809 1592
0e9c67d7 1593 had_process_hot_plug(ctx);
da864809 1594
0e9c67d7 1595 /* Process mode change if stream is active */
b556290f 1596 had_process_mode_change(ctx);
da864809 1597 }
8f8d1d7f 1598 mutex_unlock(&ctx->mutex);
182cdf23
TI
1599 pm_runtime_put(ctx->dev);
1600}
1601
b9bacf27
TI
1602/*
1603 * Jack interface
1604 */
1605static int had_create_jack(struct snd_intelhad *ctx)
1606{
1607 int err;
1608
1609 err = snd_jack_new(ctx->card, "HDMI/DP", SND_JACK_AVOUT, &ctx->jack,
1610 true, false);
1611 if (err < 0)
1612 return err;
1613 ctx->jack->private_data = ctx;
1614 return 0;
1615}
1616
182cdf23
TI
1617/*
1618 * PM callbacks
1619 */
1620
1621static int hdmi_lpe_audio_runtime_suspend(struct device *dev)
1622{
1623 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1624 struct snd_pcm_substream *substream;
1625
1626 substream = had_substream_get(ctx);
1627 if (substream) {
1628 snd_pcm_suspend(substream);
1629 had_substream_put(ctx);
1630 }
1631
1632 return 0;
1633}
1634
1df98924 1635static int __maybe_unused hdmi_lpe_audio_suspend(struct device *dev)
182cdf23
TI
1636{
1637 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1638 int err;
1639
1640 err = hdmi_lpe_audio_runtime_suspend(dev);
1641 if (!err)
1642 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D3hot);
1643 return err;
1644}
1645
1df98924 1646static int __maybe_unused hdmi_lpe_audio_resume(struct device *dev)
182cdf23
TI
1647{
1648 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1649
1650 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D0);
1651 return 0;
da864809
TI
1652}
1653
1654/* release resources */
1655static void hdmi_lpe_audio_free(struct snd_card *card)
1656{
1657 struct snd_intelhad *ctx = card->private_data;
1658
99b2ab9d
TI
1659 cancel_work_sync(&ctx->hdmi_audio_wq);
1660
da864809
TI
1661 if (ctx->mmio_start)
1662 iounmap(ctx->mmio_start);
1663 if (ctx->irq >= 0)
1664 free_irq(ctx->irq, ctx);
1665}
1666
79dda75a 1667/*
da864809 1668 * hdmi_lpe_audio_probe - start bridge with i915
5dab11d8 1669 *
da864809 1670 * This function is called when the i915 driver creates the
2e52f5e5 1671 * hdmi-lpe-audio platform device.
5dab11d8 1672 */
da864809 1673static int hdmi_lpe_audio_probe(struct platform_device *pdev)
5dab11d8 1674{
5dab11d8 1675 struct snd_card *card;
da864809
TI
1676 struct snd_intelhad *ctx;
1677 struct snd_pcm *pcm;
1678 struct intel_hdmi_lpe_audio_pdata *pdata;
1679 int irq;
1680 struct resource *res_mmio;
4aedb946 1681 int i, ret;
da864809 1682
da864809
TI
1683 pdata = pdev->dev.platform_data;
1684 if (!pdata) {
1685 dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
1686 return -EINVAL;
1687 }
5dab11d8 1688
da864809
TI
1689 /* get resources */
1690 irq = platform_get_irq(pdev, 0);
1691 if (irq < 0) {
1692 dev_err(&pdev->dev, "Could not get irq resource\n");
1693 return -ENODEV;
1694 }
1695
1696 res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1697 if (!res_mmio) {
1698 dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
1699 return -ENXIO;
1700 }
5dab11d8 1701
5647aec2 1702 /* create a card instance with ALSA framework */
da864809
TI
1703 ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
1704 THIS_MODULE, sizeof(*ctx), &card);
1705 if (ret)
1706 return ret;
1707
1708 ctx = card->private_data;
1709 spin_lock_init(&ctx->had_spinlock);
8f8d1d7f 1710 mutex_init(&ctx->mutex);
91b0cb0c 1711 ctx->connected = false;
da864809
TI
1712 ctx->dev = &pdev->dev;
1713 ctx->card = card;
da864809
TI
1714 ctx->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
1715 strcpy(card->driver, INTEL_HAD);
873ab035
TI
1716 strcpy(card->shortname, "Intel HDMI/DP LPE Audio");
1717 strcpy(card->longname, "Intel HDMI/DP LPE Audio");
da864809
TI
1718
1719 ctx->irq = -1;
1720 ctx->tmds_clock_speed = DIS_SAMPLE_RATE_148_5;
99b2ab9d 1721 INIT_WORK(&ctx->hdmi_audio_wq, had_audio_wq);
da864809
TI
1722
1723 card->private_free = hdmi_lpe_audio_free;
1724
1725 /* assume pipe A as default */
1726 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1727
1728 platform_set_drvdata(pdev, ctx);
1729
1730 dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
1731 __func__, (unsigned int)res_mmio->start,
1732 (unsigned int)res_mmio->end);
1733
1734 ctx->mmio_start = ioremap_nocache(res_mmio->start,
1735 (size_t)(resource_size(res_mmio)));
1736 if (!ctx->mmio_start) {
1737 dev_err(&pdev->dev, "Could not get ioremap\n");
1738 ret = -EACCES;
1739 goto err;
1740 }
5dab11d8 1741
da864809
TI
1742 /* setup interrupt handler */
1743 ret = request_irq(irq, display_pipe_interrupt_handler, 0,
1744 pdev->name, ctx);
1745 if (ret < 0) {
1746 dev_err(&pdev->dev, "request_irq failed\n");
1747 goto err;
1748 }
5dab11d8 1749
da864809
TI
1750 ctx->irq = irq;
1751
1752 ret = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
1753 MAX_CAP_STREAMS, &pcm);
1754 if (ret)
5dab11d8
JA
1755 goto err;
1756
1757 /* setup private data which can be retrieved when required */
da864809 1758 pcm->private_data = ctx;
5dab11d8
JA
1759 pcm->info_flags = 0;
1760 strncpy(pcm->name, card->shortname, strlen(card->shortname));
da864809 1761 /* setup the ops for playabck */
b556290f 1762 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &had_pcm_ops);
412bbe7d
TI
1763
1764 /* only 32bit addressable */
1765 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
1766 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1767
e1b239f3
TI
1768 /* allocate dma pages;
1769 * try to allocate 600k buffer as default which is large enough
5dab11d8 1770 */
da864809 1771 snd_pcm_lib_preallocate_pages_for_all(pcm,
5dab11d8 1772 SNDRV_DMA_TYPE_DEV, NULL,
e1b239f3 1773 HAD_DEFAULT_BUFFER, HAD_MAX_BUFFER);
5dab11d8 1774
4aedb946
TI
1775 /* create controls */
1776 for (i = 0; i < ARRAY_SIZE(had_controls); i++) {
1777 ret = snd_ctl_add(card, snd_ctl_new1(&had_controls[i], ctx));
1778 if (ret < 0)
1779 goto err;
1780 }
5dab11d8
JA
1781
1782 init_channel_allocations();
1783
1784 /* Register channel map controls */
da864809
TI
1785 ret = had_register_chmap_ctls(ctx, pcm);
1786 if (ret < 0)
5dab11d8
JA
1787 goto err;
1788
b9bacf27
TI
1789 ret = had_create_jack(ctx);
1790 if (ret < 0)
1791 goto err;
1792
da864809
TI
1793 ret = snd_card_register(card);
1794 if (ret)
36ec0d99
TI
1795 goto err;
1796
bcce775c 1797 spin_lock_irq(&pdata->lpe_audio_slock);
da864809 1798 pdata->notify_audio_lpe = notify_audio_lpe;
99b2ab9d 1799 pdata->notify_pending = false;
bcce775c 1800 spin_unlock_irq(&pdata->lpe_audio_slock);
da864809
TI
1801
1802 pm_runtime_set_active(&pdev->dev);
1803 pm_runtime_enable(&pdev->dev);
1804
99b2ab9d 1805 dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
da864809 1806 schedule_work(&ctx->hdmi_audio_wq);
5dab11d8 1807
79dda75a 1808 return 0;
5647aec2 1809
5dab11d8
JA
1810err:
1811 snd_card_free(card);
da864809 1812 return ret;
5dab11d8
JA
1813}
1814
79dda75a 1815/*
da864809 1816 * hdmi_lpe_audio_remove - stop bridge with i915
5dab11d8 1817 *
2e52f5e5 1818 * This function is called when the platform device is destroyed.
5dab11d8 1819 */
da864809 1820static int hdmi_lpe_audio_remove(struct platform_device *pdev)
5dab11d8 1821{
da864809 1822 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
5dab11d8 1823
da864809 1824 snd_card_free(ctx->card);
5dab11d8
JA
1825 return 0;
1826}
1827
182cdf23
TI
1828static const struct dev_pm_ops hdmi_lpe_audio_pm = {
1829 SET_SYSTEM_SLEEP_PM_OPS(hdmi_lpe_audio_suspend, hdmi_lpe_audio_resume)
1830 SET_RUNTIME_PM_OPS(hdmi_lpe_audio_runtime_suspend, NULL, NULL)
1831};
1832
da864809
TI
1833static struct platform_driver hdmi_lpe_audio_driver = {
1834 .driver = {
1835 .name = "hdmi-lpe-audio",
182cdf23 1836 .pm = &hdmi_lpe_audio_pm,
da864809
TI
1837 },
1838 .probe = hdmi_lpe_audio_probe,
1839 .remove = hdmi_lpe_audio_remove,
da864809
TI
1840};
1841
1842module_platform_driver(hdmi_lpe_audio_driver);
1843MODULE_ALIAS("platform:hdmi_lpe_audio");
1844
5dab11d8
JA
1845MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1846MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1847MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1848MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1849MODULE_DESCRIPTION("Intel HDMI Audio driver");
1850MODULE_LICENSE("GPL v2");
1851MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");