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