ALSA: x86: Remove indirect call of snd_pcm_period_elapsed()
[linux-block.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
5dab11d8
JA
24#include <linux/platform_device.h>
25#include <linux/io.h>
26#include <linux/slab.h>
27#include <linux/module.h>
da864809 28#include <linux/interrupt.h>
5dab11d8
JA
29#include <linux/acpi.h>
30#include <asm/cacheflush.h>
31#include <sound/pcm.h>
32#include <sound/core.h>
33#include <sound/pcm_params.h>
34#include <sound/initval.h>
35#include <sound/control.h>
36#include <sound/initval.h>
da864809 37#include <drm/intel_lpe_audio.h>
5dab11d8
JA
38#include "intel_hdmi_audio.h"
39
5dab11d8
JA
40/*standard module options for ALSA. This module supports only one card*/
41static int hdmi_card_index = SNDRV_DEFAULT_IDX1;
42static char *hdmi_card_id = SNDRV_DEFAULT_STR1;
5dab11d8
JA
43
44module_param_named(index, hdmi_card_index, int, 0444);
45MODULE_PARM_DESC(index,
46 "Index value for INTEL Intel HDMI Audio controller.");
47module_param_named(id, hdmi_card_id, charp, 0444);
48MODULE_PARM_DESC(id,
49 "ID string for INTEL Intel HDMI Audio controller.");
50
51/*
52 * ELD SA bits in the CEA Speaker Allocation data block
53 */
54static int eld_speaker_allocation_bits[] = {
55 [0] = FL | FR,
56 [1] = LFE,
57 [2] = FC,
58 [3] = RL | RR,
59 [4] = RC,
60 [5] = FLC | FRC,
61 [6] = RLC | RRC,
62 /* the following are not defined in ELD yet */
63 [7] = 0,
64};
65
66/*
67 * This is an ordered list!
68 *
69 * The preceding ones have better chances to be selected by
70 * hdmi_channel_allocation().
71 */
72static struct cea_channel_speaker_allocation channel_allocations[] = {
73/* channel: 7 6 5 4 3 2 1 0 */
74{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
75 /* 2.1 */
76{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
77 /* Dolby Surround */
78{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
79 /* surround40 */
80{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
81 /* surround41 */
82{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
83 /* surround50 */
84{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
85 /* surround51 */
86{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
87 /* 6.1 */
88{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
89 /* surround71 */
90{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
91
92{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
93{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
94{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
95{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
96{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
97{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
98{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
99{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
100{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
101{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
102{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
103{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
104{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
105{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
106{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
107{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
108{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
109{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
110{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
111{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
112{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
113{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
114{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
115};
116
117static struct channel_map_table map_tables[] = {
118 { SNDRV_CHMAP_FL, 0x00, FL },
119 { SNDRV_CHMAP_FR, 0x01, FR },
120 { SNDRV_CHMAP_RL, 0x04, RL },
121 { SNDRV_CHMAP_RR, 0x05, RR },
122 { SNDRV_CHMAP_LFE, 0x02, LFE },
123 { SNDRV_CHMAP_FC, 0x03, FC },
124 { SNDRV_CHMAP_RLC, 0x06, RLC },
125 { SNDRV_CHMAP_RRC, 0x07, RRC },
126 {} /* terminator */
127};
128
129/* hardware capability structure */
130static const struct snd_pcm_hardware snd_intel_hadstream = {
131 .info = (SNDRV_PCM_INFO_INTERLEAVED |
132 SNDRV_PCM_INFO_DOUBLE |
133 SNDRV_PCM_INFO_MMAP|
134 SNDRV_PCM_INFO_MMAP_VALID |
135 SNDRV_PCM_INFO_BATCH),
136 .formats = (SNDRV_PCM_FMTBIT_S24 |
137 SNDRV_PCM_FMTBIT_U24),
138 .rates = SNDRV_PCM_RATE_32000 |
139 SNDRV_PCM_RATE_44100 |
140 SNDRV_PCM_RATE_48000 |
141 SNDRV_PCM_RATE_88200 |
142 SNDRV_PCM_RATE_96000 |
143 SNDRV_PCM_RATE_176400 |
144 SNDRV_PCM_RATE_192000,
145 .rate_min = HAD_MIN_RATE,
146 .rate_max = HAD_MAX_RATE,
147 .channels_min = HAD_MIN_CHANNEL,
148 .channels_max = HAD_MAX_CHANNEL,
149 .buffer_bytes_max = HAD_MAX_BUFFER,
150 .period_bytes_min = HAD_MIN_PERIOD_BYTES,
151 .period_bytes_max = HAD_MAX_PERIOD_BYTES,
152 .periods_min = HAD_MIN_PERIODS,
153 .periods_max = HAD_MAX_PERIODS,
154 .fifo_size = HAD_FIFO_SIZE,
155};
156
157/* Register access functions */
372d855f 158static int had_get_hwstate(struct snd_intelhad *intelhaddata)
5dab11d8
JA
159{
160 /* Check for device presence -SW state */
c75b0476 161 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
5dab11d8 162 return -ENODEV;
5dab11d8
JA
163
164 return 0;
165}
166
da864809
TI
167static inline void
168mid_hdmi_audio_read(struct snd_intelhad *ctx, u32 reg, u32 *val)
5dab11d8 169{
da864809 170 *val = ioread32(ctx->mmio_start + ctx->had_config_offset + reg);
5dab11d8
JA
171}
172
da864809
TI
173static inline void
174mid_hdmi_audio_write(struct snd_intelhad *ctx, u32 reg, u32 val)
5dab11d8 175{
da864809 176 iowrite32(val, ctx->mmio_start + ctx->had_config_offset + reg);
5dab11d8
JA
177}
178
372d855f
TI
179static int had_read_register(struct snd_intelhad *intelhaddata,
180 u32 offset, u32 *data)
5dab11d8
JA
181{
182 int retval;
5dab11d8
JA
183
184 retval = had_get_hwstate(intelhaddata);
da864809
TI
185 if (retval)
186 return retval;
5dab11d8 187
da864809
TI
188 mid_hdmi_audio_read(intelhaddata, offset, data);
189 return 0;
190}
191
192static void fixup_dp_config(struct snd_intelhad *intelhaddata,
193 u32 offset, u32 *data)
194{
195 if (intelhaddata->dp_output) {
196 if (offset == AUD_CONFIG && (*data & AUD_CONFIG_VALID_BIT))
197 *data |= AUD_CONFIG_DP_MODE | AUD_CONFIG_BLOCK_BIT;
198 }
5dab11d8
JA
199}
200
372d855f
TI
201static int had_write_register(struct snd_intelhad *intelhaddata,
202 u32 offset, u32 data)
5dab11d8
JA
203{
204 int retval;
5dab11d8
JA
205
206 retval = had_get_hwstate(intelhaddata);
da864809
TI
207 if (retval)
208 return retval;
5dab11d8 209
da864809
TI
210 fixup_dp_config(intelhaddata, offset, &data);
211 mid_hdmi_audio_write(intelhaddata, offset, data);
212 return 0;
5dab11d8
JA
213}
214
372d855f
TI
215static int had_read_modify(struct snd_intelhad *intelhaddata, u32 offset,
216 u32 data, u32 mask)
5dab11d8 217{
da864809 218 u32 val_tmp;
5dab11d8 219 int retval;
5dab11d8
JA
220
221 retval = had_get_hwstate(intelhaddata);
da864809
TI
222 if (retval)
223 return retval;
5dab11d8 224
da864809
TI
225 mid_hdmi_audio_read(intelhaddata, offset, &val_tmp);
226 val_tmp &= ~mask;
227 val_tmp |= (data & mask);
228
229 fixup_dp_config(intelhaddata, offset, &val_tmp);
230 mid_hdmi_audio_write(intelhaddata, offset, val_tmp);
231 return 0;
5dab11d8 232}
da864809
TI
233
234/*
235 * function to read-modify AUD_CONFIG register on VLV2.
236 * The had_read_modify() function should not directly be used on VLV2 for
237 * updating AUD_CONFIG register.
5dab11d8
JA
238 * This is because:
239 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
240 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
241 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
242 * register. This field should be 1xy binary for configuration with 6 or
243 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
244 * causes the "channels" field to be updated as 0xy binary resulting in
245 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
246 * appropriate value when doing read-modify of AUD_CONFIG register.
247 *
248 * @substream: the current substream or NULL if no active substream
249 * @data : data to be written
250 * @mask : mask
251 *
252 */
da864809 253static int had_read_modify_aud_config_v2(struct snd_intelhad *intelhaddata,
5dab11d8
JA
254 u32 data, u32 mask)
255{
da864809 256 struct snd_pcm_substream *substream;
5dab11d8
JA
257 union aud_cfg cfg_val = {.cfg_regval = 0};
258 u8 channels;
259
260 /*
261 * If substream is NULL, there is no active stream.
262 * In this case just set channels to 2
263 */
da864809
TI
264 substream = intelhaddata->stream_info.had_substream;
265 if (substream && substream->runtime)
5dab11d8
JA
266 channels = substream->runtime->channels;
267 else
268 channels = 2;
269 cfg_val.cfg_regx_v2.num_ch = channels - 2;
270
271 data = data | cfg_val.cfg_regval;
272 mask = mask | AUD_CONFIG_CH_MASK_V2;
273
c75b0476
TI
274 dev_dbg(intelhaddata->dev, "%s : data = %x, mask =%x\n",
275 __func__, data, mask);
5dab11d8 276
79dda75a 277 return had_read_modify(intelhaddata, AUD_CONFIG, data, mask);
5dab11d8
JA
278}
279
372d855f 280static void snd_intelhad_enable_audio_int(struct snd_intelhad *ctx, bool enable)
da864809
TI
281{
282 u32 status_reg;
283
284 if (enable) {
285 mid_hdmi_audio_read(ctx, AUD_HDMI_STATUS_v2, &status_reg);
286 status_reg |= HDMI_AUDIO_BUFFER_DONE | HDMI_AUDIO_UNDERRUN;
287 mid_hdmi_audio_write(ctx, AUD_HDMI_STATUS_v2, status_reg);
288 mid_hdmi_audio_read(ctx, AUD_HDMI_STATUS_v2, &status_reg);
289 }
290}
291
372d855f
TI
292static void snd_intelhad_enable_audio(struct snd_intelhad *intelhaddata,
293 bool enable)
5dab11d8 294{
da864809
TI
295 had_read_modify_aud_config_v2(intelhaddata, enable ? BIT(0) : 0,
296 BIT(0));
5dab11d8
JA
297}
298
79dda75a
TI
299static void snd_intelhad_reset_audio(struct snd_intelhad *intelhaddata,
300 u8 reset)
5dab11d8 301{
79dda75a 302 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, reset);
5dab11d8
JA
303}
304
305/**
306 * initialize audio channel status registers
307 * This function is called in the prepare callback
308 */
309static int had_prog_status_reg(struct snd_pcm_substream *substream,
310 struct snd_intelhad *intelhaddata)
311{
312 union aud_cfg cfg_val = {.cfg_regval = 0};
313 union aud_ch_status_0 ch_stat0 = {.status_0_regval = 0};
314 union aud_ch_status_1 ch_stat1 = {.status_1_regval = 0};
315 int format;
316
5dab11d8
JA
317 ch_stat0.status_0_regx.lpcm_id = (intelhaddata->aes_bits &
318 IEC958_AES0_NONAUDIO)>>1;
319 ch_stat0.status_0_regx.clk_acc = (intelhaddata->aes_bits &
320 IEC958_AES3_CON_CLOCK)>>4;
4812dcc4 321 cfg_val.cfg_regx_v2.val_bit = ch_stat0.status_0_regx.lpcm_id;
5dab11d8
JA
322
323 switch (substream->runtime->rate) {
324 case AUD_SAMPLE_RATE_32:
325 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_32KHZ;
326 break;
327
328 case AUD_SAMPLE_RATE_44_1:
329 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_44KHZ;
330 break;
331 case AUD_SAMPLE_RATE_48:
332 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_48KHZ;
333 break;
334 case AUD_SAMPLE_RATE_88_2:
335 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_88KHZ;
336 break;
337 case AUD_SAMPLE_RATE_96:
338 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_96KHZ;
339 break;
340 case AUD_SAMPLE_RATE_176_4:
341 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_176KHZ;
342 break;
343 case AUD_SAMPLE_RATE_192:
344 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_192KHZ;
345 break;
346
347 default:
348 /* control should never come here */
349 return -EINVAL;
350 break;
351
352 }
79dda75a
TI
353 had_write_register(intelhaddata,
354 AUD_CH_STATUS_0, ch_stat0.status_0_regval);
5dab11d8
JA
355
356 format = substream->runtime->format;
357
358 if (format == SNDRV_PCM_FORMAT_S16_LE) {
359 ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_20;
360 ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_16BITS;
361 } else if (format == SNDRV_PCM_FORMAT_S24_LE) {
362 ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_24;
363 ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_24BITS;
364 } else {
365 ch_stat1.status_1_regx.max_wrd_len = 0;
366 ch_stat1.status_1_regx.wrd_len = 0;
367 }
79dda75a
TI
368 had_write_register(intelhaddata,
369 AUD_CH_STATUS_1, ch_stat1.status_1_regval);
5dab11d8
JA
370 return 0;
371}
372
76296ef0 373/*
5dab11d8
JA
374 * function to initialize audio
375 * registers and buffer confgiuration registers
376 * This function is called in the prepare callback
377 */
76296ef0
TI
378static int snd_intelhad_audio_ctrl(struct snd_pcm_substream *substream,
379 struct snd_intelhad *intelhaddata)
5dab11d8
JA
380{
381 union aud_cfg cfg_val = {.cfg_regval = 0};
382 union aud_buf_config buf_cfg = {.buf_cfgval = 0};
383 u8 channels;
384
385 had_prog_status_reg(substream, intelhaddata);
386
387 buf_cfg.buf_cfg_regx_v2.audio_fifo_watermark = FIFO_THRESHOLD;
388 buf_cfg.buf_cfg_regx_v2.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
389 buf_cfg.buf_cfg_regx_v2.aud_delay = 0;
79dda75a 390 had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.buf_cfgval);
5dab11d8
JA
391
392 channels = substream->runtime->channels;
393 cfg_val.cfg_regx_v2.num_ch = channels - 2;
394 if (channels <= 2)
395 cfg_val.cfg_regx_v2.layout = LAYOUT0;
396 else
397 cfg_val.cfg_regx_v2.layout = LAYOUT1;
398
964ca808 399 cfg_val.cfg_regx_v2.val_bit = 1;
79dda75a 400 had_write_register(intelhaddata, AUD_CONFIG, cfg_val.cfg_regval);
5dab11d8
JA
401 return 0;
402}
403
5dab11d8
JA
404/*
405 * Compute derived values in channel_allocations[].
406 */
407static void init_channel_allocations(void)
408{
409 int i, j;
410 struct cea_channel_speaker_allocation *p;
411
5dab11d8
JA
412 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
413 p = channel_allocations + i;
414 p->channels = 0;
415 p->spk_mask = 0;
416 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
417 if (p->speakers[j]) {
418 p->channels++;
419 p->spk_mask |= p->speakers[j];
420 }
421 }
422}
423
424/*
425 * The transformation takes two steps:
426 *
427 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
428 * spk_mask => (channel_allocations[]) => ai->CA
429 *
430 * TODO: it could select the wrong CA from multiple candidates.
431 */
432static int snd_intelhad_channel_allocation(struct snd_intelhad *intelhaddata,
433 int channels)
434{
435 int i;
436 int ca = 0;
437 int spk_mask = 0;
438
439 /*
440 * CA defaults to 0 for basic stereo audio
441 */
442 if (channels <= 2)
443 return 0;
444
445 /*
446 * expand ELD's speaker allocation mask
447 *
448 * ELD tells the speaker mask in a compact(paired) form,
449 * expand ELD's notions to match the ones used by Audio InfoFrame.
450 */
451
452 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
da864809 453 if (intelhaddata->eld.speaker_allocation_block & (1 << i))
5dab11d8
JA
454 spk_mask |= eld_speaker_allocation_bits[i];
455 }
456
457 /* search for the first working match in the CA table */
458 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
459 if (channels == channel_allocations[i].channels &&
460 (spk_mask & channel_allocations[i].spk_mask) ==
461 channel_allocations[i].spk_mask) {
462 ca = channel_allocations[i].ca_index;
463 break;
464 }
465 }
466
c75b0476 467 dev_dbg(intelhaddata->dev, "select CA 0x%x for %d\n", ca, channels);
5dab11d8
JA
468
469 return ca;
470}
471
472/* from speaker bit mask to ALSA API channel position */
473static int spk_to_chmap(int spk)
474{
475 struct channel_map_table *t = map_tables;
476
477 for (; t->map; t++) {
478 if (t->spk_mask == spk)
479 return t->map;
480 }
481 return 0;
482}
483
372d855f 484static void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
5dab11d8
JA
485{
486 int i = 0, c = 0;
487 int spk_mask = 0;
488 struct snd_pcm_chmap_elem *chmap;
489 u8 eld_high, eld_high_mask = 0xF0;
490 u8 high_msb;
491
492 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
493 if (chmap == NULL) {
494 intelhaddata->chmap->chmap = NULL;
495 return;
496 }
497
c75b0476 498 dev_dbg(intelhaddata->dev, "eld.speaker_allocation_block = %x\n",
da864809 499 intelhaddata->eld.speaker_allocation_block);
5dab11d8
JA
500
501 /* WA: Fix the max channel supported to 8 */
502
503 /*
504 * Sink may support more than 8 channels, if eld_high has more than
505 * one bit set. SOC supports max 8 channels.
506 * Refer eld_speaker_allocation_bits, for sink speaker allocation
507 */
508
509 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
da864809 510 eld_high = intelhaddata->eld.speaker_allocation_block & eld_high_mask;
5dab11d8
JA
511 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
512 /* eld_high & (eld_high-1): if more than 1 bit set */
513 /* 0x1F: 7 channels */
514 for (i = 1; i < 4; i++) {
515 high_msb = eld_high & (0x80 >> i);
516 if (high_msb) {
da864809 517 intelhaddata->eld.speaker_allocation_block &=
5dab11d8
JA
518 high_msb | 0xF;
519 break;
520 }
521 }
522 }
523
524 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
da864809 525 if (intelhaddata->eld.speaker_allocation_block & (1 << i))
5dab11d8
JA
526 spk_mask |= eld_speaker_allocation_bits[i];
527 }
528
529 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
530 if (spk_mask == channel_allocations[i].spk_mask) {
531 for (c = 0; c < channel_allocations[i].channels; c++) {
532 chmap->map[c] = spk_to_chmap(
533 channel_allocations[i].speakers[
534 (MAX_SPEAKERS - 1)-c]);
535 }
536 chmap->channels = channel_allocations[i].channels;
537 intelhaddata->chmap->chmap = chmap;
538 break;
539 }
540 }
541 if (i >= ARRAY_SIZE(channel_allocations)) {
542 intelhaddata->chmap->chmap = NULL;
543 kfree(chmap);
544 }
545}
546
547/*
548 * ALSA API channel-map control callbacks
549 */
550static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
551 struct snd_ctl_elem_info *uinfo)
552{
553 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
554 struct snd_intelhad *intelhaddata = info->private_data;
555
556 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
557 return -ENODEV;
558 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
559 uinfo->count = HAD_MAX_CHANNEL;
560 uinfo->value.integer.min = 0;
561 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
562 return 0;
563}
564
565static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
566 struct snd_ctl_elem_value *ucontrol)
567{
568 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
569 struct snd_intelhad *intelhaddata = info->private_data;
570 int i = 0;
571 const struct snd_pcm_chmap_elem *chmap;
572
573 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
574 return -ENODEV;
575 if (intelhaddata->chmap->chmap == NULL)
576 return -ENODATA;
577 chmap = intelhaddata->chmap->chmap;
c75b0476 578 for (i = 0; i < chmap->channels; i++)
5dab11d8 579 ucontrol->value.integer.value[i] = chmap->map[i];
5dab11d8
JA
580
581 return 0;
582}
583
584static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
585 struct snd_pcm *pcm)
586{
587 int err = 0;
588
589 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
590 NULL, 0, (unsigned long)intelhaddata,
591 &intelhaddata->chmap);
592 if (err < 0)
593 return err;
594
595 intelhaddata->chmap->private_data = intelhaddata;
596 intelhaddata->kctl = intelhaddata->chmap->kctl;
597 intelhaddata->kctl->info = had_chmap_ctl_info;
598 intelhaddata->kctl->get = had_chmap_ctl_get;
599 intelhaddata->chmap->chmap = NULL;
600 return 0;
601}
602
76296ef0
TI
603/*
604 * snd_intelhad_prog_dip - to initialize Data Island Packets registers
5dab11d8
JA
605 *
606 * @substream:substream for which the prepare function is called
607 * @intelhaddata:substream private data
608 *
609 * This function is called in the prepare callback
610 */
76296ef0
TI
611static void snd_intelhad_prog_dip(struct snd_pcm_substream *substream,
612 struct snd_intelhad *intelhaddata)
5dab11d8
JA
613{
614 int i;
615 union aud_ctrl_st ctrl_state = {.ctrl_val = 0};
616 union aud_info_frame2 frame2 = {.fr2_val = 0};
617 union aud_info_frame3 frame3 = {.fr3_val = 0};
618 u8 checksum = 0;
964ca808 619 u32 info_frame;
5dab11d8
JA
620 int channels;
621
622 channels = substream->runtime->channels;
623
79dda75a 624 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.ctrl_val);
5dab11d8 625
964ca808
PLB
626 if (intelhaddata->dp_output) {
627 info_frame = DP_INFO_FRAME_WORD1;
628 frame2.fr2_val = 1;
629 } else {
630 info_frame = HDMI_INFO_FRAME_WORD1;
631 frame2.fr2_regx.chnl_cnt = substream->runtime->channels - 1;
5dab11d8 632
964ca808
PLB
633 frame3.fr3_regx.chnl_alloc = snd_intelhad_channel_allocation(
634 intelhaddata, channels);
5dab11d8 635
964ca808
PLB
636 /*Calculte the byte wide checksum for all valid DIP words*/
637 for (i = 0; i < BYTES_PER_WORD; i++)
638 checksum += (info_frame >> i*BITS_PER_BYTE) & MASK_BYTE0;
639 for (i = 0; i < BYTES_PER_WORD; i++)
640 checksum += (frame2.fr2_val >> i*BITS_PER_BYTE) & MASK_BYTE0;
641 for (i = 0; i < BYTES_PER_WORD; i++)
642 checksum += (frame3.fr3_val >> i*BITS_PER_BYTE) & MASK_BYTE0;
5dab11d8 643
964ca808
PLB
644 frame2.fr2_regx.chksum = -(checksum);
645 }
5dab11d8 646
79dda75a
TI
647 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, info_frame);
648 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, frame2.fr2_val);
649 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, frame3.fr3_val);
5dab11d8
JA
650
651 /* program remaining DIP words with zero */
652 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
79dda75a 653 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, 0x0);
5dab11d8
JA
654
655 ctrl_state.ctrl_regx.dip_freq = 1;
656 ctrl_state.ctrl_regx.dip_en_sta = 1;
79dda75a 657 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.ctrl_val);
5dab11d8
JA
658}
659
660/**
661 * snd_intelhad_prog_buffer - programs buffer
662 * address and length registers
663 *
664 * @substream:substream for which the prepare function is called
665 * @intelhaddata:substream private data
666 *
667 * This function programs ring buffer address and length into registers.
668 */
372d855f 669static int snd_intelhad_prog_buffer(struct snd_intelhad *intelhaddata,
5dab11d8
JA
670 int start, int end)
671{
672 u32 ring_buf_addr, ring_buf_size, period_bytes;
673 u8 i, num_periods;
674 struct snd_pcm_substream *substream;
675
676 substream = intelhaddata->stream_info.had_substream;
c75b0476 677 if (WARN_ON(!substream))
5dab11d8 678 return 0;
5dab11d8
JA
679
680 ring_buf_addr = substream->runtime->dma_addr;
681 ring_buf_size = snd_pcm_lib_buffer_bytes(substream);
682 intelhaddata->stream_info.ring_buf_size = ring_buf_size;
683 period_bytes = frames_to_bytes(substream->runtime,
684 substream->runtime->period_size);
685 num_periods = substream->runtime->periods;
686
687 /*
688 * buffer addr should be 64 byte aligned, period bytes
689 * will be used to calculate addr offset
690 */
691 period_bytes &= ~0x3F;
692
693 /* Hardware supports MAX_PERIODS buffers */
694 if (end >= HAD_MAX_PERIODS)
695 return -EINVAL;
696
697 for (i = start; i <= end; i++) {
698 /* Program the buf registers with addr and len */
699 intelhaddata->buf_info[i].buf_addr = ring_buf_addr +
700 (i * period_bytes);
701 if (i < num_periods-1)
702 intelhaddata->buf_info[i].buf_size = period_bytes;
703 else
704 intelhaddata->buf_info[i].buf_size = ring_buf_size -
705 (period_bytes*i);
706
79dda75a
TI
707 had_write_register(intelhaddata,
708 AUD_BUF_A_ADDR + (i * HAD_REG_WIDTH),
5dab11d8
JA
709 intelhaddata->buf_info[i].buf_addr |
710 BIT(0) | BIT(1));
79dda75a
TI
711 had_write_register(intelhaddata,
712 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
5dab11d8
JA
713 period_bytes);
714 intelhaddata->buf_info[i].is_valid = true;
715 }
c75b0476
TI
716 dev_dbg(intelhaddata->dev, "%s:buf[%d-%d] addr=%#x and size=%d\n",
717 __func__, start, end,
718 intelhaddata->buf_info[start].buf_addr,
719 intelhaddata->buf_info[start].buf_size);
5dab11d8
JA
720 intelhaddata->valid_buf_cnt = num_periods;
721 return 0;
722}
723
372d855f 724static int snd_intelhad_read_len(struct snd_intelhad *intelhaddata)
5dab11d8
JA
725{
726 int i, retval = 0;
727 u32 len[4];
728
729 for (i = 0; i < 4 ; i++) {
79dda75a
TI
730 had_read_register(intelhaddata,
731 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
732 &len[i]);
5dab11d8
JA
733 if (!len[i])
734 retval++;
735 }
736 if (retval != 1) {
737 for (i = 0; i < 4 ; i++)
c75b0476
TI
738 dev_dbg(intelhaddata->dev, "buf[%d] size=%d\n",
739 i, len[i]);
5dab11d8
JA
740 }
741
742 return retval;
743}
744
964ca808
PLB
745static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
746{
747 u32 maud_val;
748
749 /* Select maud according to DP 1.2 spec*/
750 if (link_rate == DP_2_7_GHZ) {
751 switch (aud_samp_freq) {
752 case AUD_SAMPLE_RATE_32:
753 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
754 break;
755
756 case AUD_SAMPLE_RATE_44_1:
757 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
758 break;
759
760 case AUD_SAMPLE_RATE_48:
761 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
762 break;
763
764 case AUD_SAMPLE_RATE_88_2:
765 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
766 break;
767
768 case AUD_SAMPLE_RATE_96:
769 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
770 break;
771
772 case AUD_SAMPLE_RATE_176_4:
773 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
774 break;
775
776 case HAD_MAX_RATE:
777 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
778 break;
779
780 default:
781 maud_val = -EINVAL;
782 break;
783 }
784 } else if (link_rate == DP_1_62_GHZ) {
785 switch (aud_samp_freq) {
786 case AUD_SAMPLE_RATE_32:
787 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
788 break;
789
790 case AUD_SAMPLE_RATE_44_1:
791 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
792 break;
793
794 case AUD_SAMPLE_RATE_48:
795 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
796 break;
797
798 case AUD_SAMPLE_RATE_88_2:
799 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
800 break;
801
802 case AUD_SAMPLE_RATE_96:
803 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
804 break;
805
806 case AUD_SAMPLE_RATE_176_4:
807 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
808 break;
809
810 case HAD_MAX_RATE:
811 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
812 break;
813
814 default:
815 maud_val = -EINVAL;
816 break;
817 }
818 } else
819 maud_val = -EINVAL;
820
821 return maud_val;
822}
823
76296ef0
TI
824/*
825 * snd_intelhad_prog_cts - Program HDMI audio CTS value
5dab11d8
JA
826 *
827 * @aud_samp_freq: sampling frequency of audio data
828 * @tmds: sampling frequency of the display data
829 * @n_param: N value, depends on aud_samp_freq
830 * @intelhaddata:substream private data
831 *
832 * Program CTS register based on the audio and display sampling frequency
833 */
76296ef0
TI
834static void snd_intelhad_prog_cts(u32 aud_samp_freq, u32 tmds,
835 u32 link_rate, u32 n_param,
836 struct snd_intelhad *intelhaddata)
5dab11d8
JA
837{
838 u32 cts_val;
839 u64 dividend, divisor;
840
964ca808
PLB
841 if (intelhaddata->dp_output) {
842 /* Substitute cts_val with Maud according to DP 1.2 spec*/
843 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
844 } else {
845 /* Calculate CTS according to HDMI 1.3a spec*/
846 dividend = (u64)tmds * n_param*1000;
847 divisor = 128 * aud_samp_freq;
848 cts_val = div64_u64(dividend, divisor);
849 }
c75b0476 850 dev_dbg(intelhaddata->dev, "TMDS value=%d, N value=%d, CTS Value=%d\n",
964ca808 851 tmds, n_param, cts_val);
79dda75a 852 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
5dab11d8
JA
853}
854
855static int had_calculate_n_value(u32 aud_samp_freq)
856{
857 s32 n_val;
858
859 /* Select N according to HDMI 1.3a spec*/
860 switch (aud_samp_freq) {
861 case AUD_SAMPLE_RATE_32:
862 n_val = 4096;
863 break;
864
865 case AUD_SAMPLE_RATE_44_1:
866 n_val = 6272;
867 break;
868
869 case AUD_SAMPLE_RATE_48:
870 n_val = 6144;
871 break;
872
873 case AUD_SAMPLE_RATE_88_2:
874 n_val = 12544;
875 break;
876
877 case AUD_SAMPLE_RATE_96:
878 n_val = 12288;
879 break;
880
881 case AUD_SAMPLE_RATE_176_4:
882 n_val = 25088;
883 break;
884
885 case HAD_MAX_RATE:
886 n_val = 24576;
887 break;
888
889 default:
890 n_val = -EINVAL;
891 break;
892 }
893 return n_val;
894}
895
76296ef0
TI
896/*
897 * snd_intelhad_prog_n - Program HDMI audio N value
5dab11d8
JA
898 *
899 * @aud_samp_freq: sampling frequency of audio data
900 * @n_param: N value, depends on aud_samp_freq
901 * @intelhaddata:substream private data
902 *
903 * This function is called in the prepare callback.
904 * It programs based on the audio and display sampling frequency
905 */
76296ef0
TI
906static int snd_intelhad_prog_n(u32 aud_samp_freq, u32 *n_param,
907 struct snd_intelhad *intelhaddata)
5dab11d8
JA
908{
909 s32 n_val;
910
964ca808
PLB
911 if (intelhaddata->dp_output) {
912 /*
913 * According to DP specs, Maud and Naud values hold
914 * a relationship, which is stated as:
915 * Maud/Naud = 512 * fs / f_LS_Clk
916 * where, fs is the sampling frequency of the audio stream
917 * and Naud is 32768 for Async clock.
918 */
919
920 n_val = DP_NAUD_VAL;
921 } else
922 n_val = had_calculate_n_value(aud_samp_freq);
5dab11d8
JA
923
924 if (n_val < 0)
925 return n_val;
926
79dda75a 927 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
5dab11d8
JA
928 *n_param = n_val;
929 return 0;
930}
931
372d855f 932static void snd_intelhad_handle_underrun(struct snd_intelhad *intelhaddata)
5dab11d8
JA
933{
934 u32 hdmi_status, i = 0;
935
936 /* Handle Underrun interrupt within Audio Unit */
79dda75a 937 had_write_register(intelhaddata, AUD_CONFIG, 0);
5dab11d8 938 /* Reset buffer pointers */
79dda75a
TI
939 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, 1);
940 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, 0);
5dab11d8
JA
941 /**
942 * The interrupt status 'sticky' bits might not be cleared by
943 * setting '1' to that bit once...
944 */
945 do { /* clear bit30, 31 AUD_HDMI_STATUS */
79dda75a
TI
946 had_read_register(intelhaddata, AUD_HDMI_STATUS_v2,
947 &hdmi_status);
c75b0476 948 dev_dbg(intelhaddata->dev, "HDMI status =0x%x\n", hdmi_status);
5dab11d8
JA
949 if (hdmi_status & AUD_CONFIG_MASK_UNDERRUN) {
950 i++;
79dda75a
TI
951 had_write_register(intelhaddata,
952 AUD_HDMI_STATUS_v2, hdmi_status);
5dab11d8
JA
953 } else
954 break;
955 } while (i < MAX_CNT);
956 if (i >= MAX_CNT)
c75b0476 957 dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n");
5dab11d8
JA
958}
959
960/**
961 * snd_intelhad_open - stream initializations are done here
962 * @substream:substream for which the stream function is called
963 *
964 * This function is called whenever a PCM stream is opened
965 */
966static int snd_intelhad_open(struct snd_pcm_substream *substream)
967{
968 struct snd_intelhad *intelhaddata;
969 struct snd_pcm_runtime *runtime;
970 struct had_stream_pvt *stream;
5647aec2 971 struct had_stream_data *had_stream;
5dab11d8
JA
972 int retval;
973
5dab11d8 974 intelhaddata = snd_pcm_substream_chip(substream);
5647aec2 975 had_stream = &intelhaddata->stream_data;
5dab11d8 976 runtime = substream->runtime;
6ddb3ab6 977 intelhaddata->underrun_count = 0;
5dab11d8
JA
978
979 pm_runtime_get(intelhaddata->dev);
980
981 if (had_get_hwstate(intelhaddata)) {
c75b0476
TI
982 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
983 __func__);
5dab11d8
JA
984 retval = -ENODEV;
985 goto exit_put_handle;
986 }
987
988 /* Check, if device already in use */
989 if (runtime->private_data) {
c75b0476 990 dev_dbg(intelhaddata->dev, "Device already in use\n");
5dab11d8
JA
991 retval = -EBUSY;
992 goto exit_put_handle;
993 }
994
995 /* set the runtime hw parameter with local snd_pcm_hardware struct */
996 runtime->hw = snd_intel_hadstream;
997
998 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
999 if (!stream) {
1000 retval = -ENOMEM;
1001 goto exit_put_handle;
1002 }
1003 stream->stream_status = STREAM_INIT;
1004 runtime->private_data = stream;
1005
1006 retval = snd_pcm_hw_constraint_integer(runtime,
1007 SNDRV_PCM_HW_PARAM_PERIODS);
1008 if (retval < 0)
1009 goto exit_err;
1010
1011 /* Make sure, that the period size is always aligned
1012 * 64byte boundary
1013 */
1014 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1015 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
1016 if (retval < 0) {
c75b0476
TI
1017 dev_dbg(intelhaddata->dev, "%s:step_size=64 failed,err=%d\n",
1018 __func__, retval);
5dab11d8
JA
1019 goto exit_err;
1020 }
1021
1022 return retval;
1023exit_err:
1024 kfree(stream);
1025exit_put_handle:
1026 pm_runtime_put(intelhaddata->dev);
1027 runtime->private_data = NULL;
1028 return retval;
1029}
1030
df76df12 1031/*
5dab11d8 1032 * had_period_elapsed - updates the hardware pointer status
df76df12 1033 * @had_substream: substream for which the stream function is called
5dab11d8 1034 */
df76df12 1035static void had_period_elapsed(struct snd_pcm_substream *substream)
5dab11d8 1036{
5dab11d8
JA
1037 struct had_stream_pvt *stream;
1038
5dab11d8
JA
1039 if (!substream || !substream->runtime)
1040 return;
1041 stream = substream->runtime->private_data;
1042 if (!stream)
1043 return;
1044
1045 if (stream->stream_status != STREAM_RUNNING)
1046 return;
1047 snd_pcm_period_elapsed(substream);
1048}
1049
1050/**
1051 * snd_intelhad_init_stream - internal function to initialize stream info
1052 * @substream:substream for which the stream function is called
1053 *
1054 */
1055static int snd_intelhad_init_stream(struct snd_pcm_substream *substream)
1056{
1057 struct snd_intelhad *intelhaddata = snd_pcm_substream_chip(substream);
1058
5dab11d8
JA
1059 intelhaddata->stream_info.had_substream = substream;
1060 intelhaddata->stream_info.buffer_ptr = 0;
1061 intelhaddata->stream_info.buffer_rendered = 0;
1062 intelhaddata->stream_info.sfreq = substream->runtime->rate;
1063 return 0;
1064}
1065
1066/**
1067 * snd_intelhad_close- to free parameteres when stream is stopped
1068 *
1069 * @substream: substream for which the function is called
1070 *
1071 * This function is called by ALSA framework when stream is stopped
1072 */
1073static int snd_intelhad_close(struct snd_pcm_substream *substream)
1074{
1075 struct snd_intelhad *intelhaddata;
1076 struct snd_pcm_runtime *runtime;
1077
5dab11d8
JA
1078 intelhaddata = snd_pcm_substream_chip(substream);
1079 runtime = substream->runtime;
1080
c75b0476 1081 if (WARN_ON(!runtime->private_data))
5dab11d8 1082 return 0;
5dab11d8
JA
1083
1084 intelhaddata->stream_info.buffer_rendered = 0;
1085 intelhaddata->stream_info.buffer_ptr = 0;
1086 intelhaddata->stream_info.str_id = 0;
1087 intelhaddata->stream_info.had_substream = NULL;
1088
1089 /* Check if following drv_status modification is required - VA */
1090 if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) {
1091 intelhaddata->drv_status = HAD_DRV_CONNECTED;
c75b0476
TI
1092 dev_dbg(intelhaddata->dev,
1093 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
5dab11d8
JA
1094 __func__, __LINE__);
1095 }
1096 kfree(runtime->private_data);
1097 runtime->private_data = NULL;
1098 pm_runtime_put(intelhaddata->dev);
1099 return 0;
1100}
1101
1102/**
1103 * snd_intelhad_hw_params- to setup the hardware parameters
1104 * like allocating the buffers
1105 *
1106 * @substream: substream for which the function is called
1107 * @hw_params: hardware parameters
1108 *
1109 * This function is called by ALSA framework when hardware params are set
1110 */
1111static int snd_intelhad_hw_params(struct snd_pcm_substream *substream,
1112 struct snd_pcm_hw_params *hw_params)
1113{
c75b0476 1114 struct snd_intelhad *intelhaddata;
5dab11d8
JA
1115 unsigned long addr;
1116 int pages, buf_size, retval;
1117
5dab11d8
JA
1118 if (!hw_params)
1119 return -EINVAL;
1120
c75b0476 1121 intelhaddata = snd_pcm_substream_chip(substream);
5dab11d8
JA
1122 buf_size = params_buffer_bytes(hw_params);
1123 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1124 if (retval < 0)
1125 return retval;
c75b0476
TI
1126 dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
1127 __func__, buf_size);
5dab11d8
JA
1128 /* mark the pages as uncached region */
1129 addr = (unsigned long) substream->runtime->dma_area;
1130 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1131 retval = set_memory_uc(addr, pages);
1132 if (retval) {
c75b0476
TI
1133 dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
1134 retval);
5dab11d8
JA
1135 return retval;
1136 }
1137 memset(substream->runtime->dma_area, 0, buf_size);
1138
1139 return retval;
1140}
1141
1142/**
1143 * snd_intelhad_hw_free- to release the resources allocated during
1144 * hardware params setup
1145 *
1146 * @substream: substream for which the function is called
1147 *
1148 * This function is called by ALSA framework before close callback.
1149 *
1150 */
1151static int snd_intelhad_hw_free(struct snd_pcm_substream *substream)
1152{
1153 unsigned long addr;
1154 u32 pages;
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
1167/**
1168 * snd_intelhad_pcm_trigger - stream activities are handled here
1169 * @substream:substream for which the stream function is called
1170 * @cmd:the stream commamd thats requested from upper layer
1171 * This function is called whenever an a stream activity is invoked
1172 */
1173static int snd_intelhad_pcm_trigger(struct snd_pcm_substream *substream,
1174 int cmd)
1175{
da864809 1176 int retval = 0;
5dab11d8
JA
1177 unsigned long flag_irq;
1178 struct snd_intelhad *intelhaddata;
1179 struct had_stream_pvt *stream;
5647aec2 1180 struct had_stream_data *had_stream;
5dab11d8 1181
5dab11d8
JA
1182 intelhaddata = snd_pcm_substream_chip(substream);
1183 stream = substream->runtime->private_data;
5647aec2 1184 had_stream = &intelhaddata->stream_data;
5dab11d8
JA
1185
1186 switch (cmd) {
1187 case SNDRV_PCM_TRIGGER_START:
5dab11d8
JA
1188 /* Disable local INTRs till register prgmng is done */
1189 if (had_get_hwstate(intelhaddata)) {
c75b0476
TI
1190 dev_dbg(intelhaddata->dev,
1191 "_START: HDMI cable plugged-out\n");
5dab11d8
JA
1192 retval = -ENODEV;
1193 break;
1194 }
1195 stream->stream_status = STREAM_RUNNING;
1196
1197 had_stream->stream_type = HAD_RUNNING_STREAM;
1198
1199 /* Enable Audio */
da864809
TI
1200 snd_intelhad_enable_audio_int(intelhaddata, true);
1201 snd_intelhad_enable_audio(intelhaddata, true);
5dab11d8
JA
1202 break;
1203
1204 case SNDRV_PCM_TRIGGER_STOP:
5dab11d8
JA
1205 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irq);
1206 intelhaddata->stream_info.str_id = 0;
1207 intelhaddata->curr_buf = 0;
1208
c75b0476 1209 /* Stop reporting BUFFER_DONE/UNDERRUN to above layers */
5dab11d8
JA
1210
1211 had_stream->stream_type = HAD_INIT;
1212 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irq);
1213 /* Disable Audio */
da864809
TI
1214 snd_intelhad_enable_audio_int(intelhaddata, false);
1215 snd_intelhad_enable_audio(intelhaddata, false);
5dab11d8 1216 /* Reset buffer pointers */
79dda75a
TI
1217 snd_intelhad_reset_audio(intelhaddata, 1);
1218 snd_intelhad_reset_audio(intelhaddata, 0);
5dab11d8 1219 stream->stream_status = STREAM_DROPPED;
da864809 1220 snd_intelhad_enable_audio_int(intelhaddata, false);
5dab11d8
JA
1221 break;
1222
1223 default:
1224 retval = -EINVAL;
1225 }
1226 return retval;
1227}
1228
1229/**
1230 * snd_intelhad_pcm_prepare- internal preparation before starting a stream
1231 *
1232 * @substream: substream for which the function is called
1233 *
1234 * This function is called when a stream is started for internal preparation.
1235 */
1236static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream)
1237{
1238 int retval;
1239 u32 disp_samp_freq, n_param;
964ca808 1240 u32 link_rate = 0;
5dab11d8
JA
1241 struct snd_intelhad *intelhaddata;
1242 struct snd_pcm_runtime *runtime;
5647aec2 1243 struct had_stream_data *had_stream;
5dab11d8 1244
5dab11d8
JA
1245 intelhaddata = snd_pcm_substream_chip(substream);
1246 runtime = substream->runtime;
5647aec2 1247 had_stream = &intelhaddata->stream_data;
5dab11d8
JA
1248
1249 if (had_get_hwstate(intelhaddata)) {
c75b0476
TI
1250 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
1251 __func__);
5dab11d8
JA
1252 retval = -ENODEV;
1253 goto prep_end;
1254 }
1255
c75b0476 1256 dev_dbg(intelhaddata->dev, "period_size=%d\n",
5dab11d8 1257 (int)frames_to_bytes(runtime, runtime->period_size));
c75b0476
TI
1258 dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods);
1259 dev_dbg(intelhaddata->dev, "buffer_size=%d\n",
1260 (int)snd_pcm_lib_buffer_bytes(substream));
1261 dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
1262 dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
5dab11d8
JA
1263
1264 if (intelhaddata->stream_info.str_id) {
c75b0476
TI
1265 dev_dbg(intelhaddata->dev,
1266 "_prepare is called for existing str_id#%d\n",
5dab11d8
JA
1267 intelhaddata->stream_info.str_id);
1268 retval = snd_intelhad_pcm_trigger(substream,
1269 SNDRV_PCM_TRIGGER_STOP);
1270 return retval;
1271 }
1272
1273 retval = snd_intelhad_init_stream(substream);
1274 if (retval)
1275 goto prep_end;
1276
1277
1278 /* Get N value in KHz */
da864809 1279 disp_samp_freq = intelhaddata->tmds_clock_speed;
5dab11d8 1280
76296ef0
TI
1281 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1282 intelhaddata);
5dab11d8 1283 if (retval) {
c75b0476
TI
1284 dev_err(intelhaddata->dev,
1285 "programming N value failed %#x\n", retval);
5dab11d8
JA
1286 goto prep_end;
1287 }
964ca808
PLB
1288
1289 if (intelhaddata->dp_output)
da864809 1290 link_rate = intelhaddata->link_rate;
964ca808 1291
76296ef0
TI
1292 snd_intelhad_prog_cts(substream->runtime->rate,
1293 disp_samp_freq, link_rate,
1294 n_param, intelhaddata);
5dab11d8 1295
76296ef0 1296 snd_intelhad_prog_dip(substream, intelhaddata);
5dab11d8 1297
76296ef0 1298 retval = snd_intelhad_audio_ctrl(substream, intelhaddata);
5dab11d8
JA
1299
1300 /* Prog buffer address */
1301 retval = snd_intelhad_prog_buffer(intelhaddata,
1302 HAD_BUF_TYPE_A, HAD_BUF_TYPE_D);
1303
1304 /*
1305 * Program channel mapping in following order:
1306 * FL, FR, C, LFE, RL, RR
1307 */
1308
79dda75a 1309 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
5dab11d8
JA
1310
1311prep_end:
1312 return retval;
1313}
1314
1315/**
1316 * snd_intelhad_pcm_pointer- to send the current buffer pointerprocessed by hw
1317 *
1318 * @substream: substream for which the function is called
1319 *
1320 * This function is called by ALSA framework to get the current hw buffer ptr
1321 * when a period is elapsed
1322 */
1323static snd_pcm_uframes_t snd_intelhad_pcm_pointer(
1324 struct snd_pcm_substream *substream)
1325{
1326 struct snd_intelhad *intelhaddata;
1327 u32 bytes_rendered = 0;
1328 u32 t;
1329 int buf_id;
1330
5dab11d8
JA
1331 intelhaddata = snd_pcm_substream_chip(substream);
1332
1333 if (intelhaddata->flag_underrun) {
1334 intelhaddata->flag_underrun = 0;
1335 return SNDRV_PCM_POS_XRUN;
1336 }
1337
1338 /* Use a hw register to calculate sub-period position reports.
1339 * This makes PulseAudio happier.
1340 */
1341
1342 buf_id = intelhaddata->curr_buf % 4;
79dda75a
TI
1343 had_read_register(intelhaddata,
1344 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH), &t);
232892fb
JA
1345
1346 if ((t == 0) || (t == ((u32)-1L))) {
6ddb3ab6 1347 intelhaddata->underrun_count++;
c75b0476
TI
1348 dev_dbg(intelhaddata->dev,
1349 "discovered buffer done for buf %d, count = %d\n",
6ddb3ab6 1350 buf_id, intelhaddata->underrun_count);
232892fb 1351
6ddb3ab6 1352 if (intelhaddata->underrun_count > (HAD_MIN_PERIODS/2)) {
c75b0476
TI
1353 dev_dbg(intelhaddata->dev,
1354 "assume audio_codec_reset, underrun = %d - do xrun\n",
6ddb3ab6
TI
1355 intelhaddata->underrun_count);
1356 intelhaddata->underrun_count = 0;
232892fb
JA
1357 return SNDRV_PCM_POS_XRUN;
1358 }
1359 } else {
1360 /* Reset Counter */
6ddb3ab6 1361 intelhaddata->underrun_count = 0;
5dab11d8 1362 }
232892fb 1363
5dab11d8
JA
1364 t = intelhaddata->buf_info[buf_id].buf_size - t;
1365
1366 if (intelhaddata->stream_info.buffer_rendered)
1367 div_u64_rem(intelhaddata->stream_info.buffer_rendered,
1368 intelhaddata->stream_info.ring_buf_size,
1369 &(bytes_rendered));
1370
1371 intelhaddata->stream_info.buffer_ptr = bytes_to_frames(
1372 substream->runtime,
1373 bytes_rendered + t);
1374 return intelhaddata->stream_info.buffer_ptr;
1375}
1376
1377/**
1378 * snd_intelhad_pcm_mmap- mmaps a kernel buffer to user space for copying data
1379 *
1380 * @substream: substream for which the function is called
1381 * @vma: struct instance of memory VMM memory area
1382 *
1383 * This function is called by OS when a user space component
1384 * tries to get mmap memory from driver
1385 */
1386static int snd_intelhad_pcm_mmap(struct snd_pcm_substream *substream,
1387 struct vm_area_struct *vma)
1388{
5dab11d8
JA
1389 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1390 return remap_pfn_range(vma, vma->vm_start,
1391 substream->dma_buffer.addr >> PAGE_SHIFT,
1392 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1393}
1394
da864809 1395static int hdmi_audio_mode_change(struct snd_intelhad *intelhaddata)
5dab11d8 1396{
da864809 1397 struct snd_pcm_substream *substream;
5dab11d8
JA
1398 int retval = 0;
1399 u32 disp_samp_freq, n_param;
964ca808 1400 u32 link_rate = 0;
5dab11d8 1401
da864809
TI
1402 substream = intelhaddata->stream_info.had_substream;
1403 if (!substream || !substream->runtime)
1404 return 0;
5dab11d8
JA
1405
1406 /* Disable Audio */
da864809 1407 snd_intelhad_enable_audio(intelhaddata, false);
5dab11d8
JA
1408
1409 /* Update CTS value */
da864809 1410 disp_samp_freq = intelhaddata->tmds_clock_speed;
5dab11d8 1411
76296ef0
TI
1412 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1413 intelhaddata);
5dab11d8 1414 if (retval) {
c75b0476
TI
1415 dev_err(intelhaddata->dev,
1416 "programming N value failed %#x\n", retval);
5dab11d8
JA
1417 goto out;
1418 }
964ca808
PLB
1419
1420 if (intelhaddata->dp_output)
da864809 1421 link_rate = intelhaddata->link_rate;
964ca808 1422
76296ef0
TI
1423 snd_intelhad_prog_cts(substream->runtime->rate,
1424 disp_samp_freq, link_rate,
1425 n_param, intelhaddata);
5dab11d8
JA
1426
1427 /* Enable Audio */
da864809 1428 snd_intelhad_enable_audio(intelhaddata, true);
5dab11d8
JA
1429
1430out:
1431 return retval;
1432}
1433
372d855f
TI
1434/*
1435 * hdmi_lpe_audio_suspend - power management suspend function
1436 *
1437 * @pdev: platform device
1438 *
1439 * This function is called by client driver to suspend the
1440 * hdmi audio.
1441 */
1442static int hdmi_lpe_audio_suspend(struct platform_device *pdev,
1443 pm_message_t state)
1444{
1445 struct had_stream_data *had_stream;
1446 unsigned long flag_irqs;
1447 struct snd_pcm_substream *substream;
1448 struct snd_intelhad *intelhaddata = platform_get_drvdata(pdev);
1449
372d855f
TI
1450 had_stream = &intelhaddata->stream_data;
1451 substream = intelhaddata->stream_info.had_substream;
1452
e29c0f96 1453 if (!pm_runtime_status_suspended(intelhaddata->dev)) {
c75b0476 1454 dev_err(intelhaddata->dev, "audio stream is active\n");
372d855f
TI
1455 return -EAGAIN;
1456 }
1457
1458
1459 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1460 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
1461 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
c75b0476 1462 dev_dbg(intelhaddata->dev, "had not connected\n");
372d855f
TI
1463 return 0;
1464 }
1465
1466 if (intelhaddata->drv_status == HAD_DRV_SUSPENDED) {
1467 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
c75b0476 1468 dev_dbg(intelhaddata->dev, "had already suspended\n");
372d855f
TI
1469 return 0;
1470 }
1471
1472 intelhaddata->drv_status = HAD_DRV_SUSPENDED;
c75b0476
TI
1473 dev_dbg(intelhaddata->dev,
1474 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_SUSPENDED\n",
372d855f
TI
1475 __func__, __LINE__);
1476
1477 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1478 snd_intelhad_enable_audio_int(intelhaddata, false);
372d855f
TI
1479 return 0;
1480}
1481
1482/*
1483 * hdmi_lpe_audio_resume - power management resume function
1484 *
1485 *@pdev: platform device
1486 *
1487 * This function is called by client driver to resume the
1488 * hdmi audio.
1489 */
1490static int hdmi_lpe_audio_resume(struct platform_device *pdev)
1491{
1492 struct snd_intelhad *intelhaddata = platform_get_drvdata(pdev);
1493 unsigned long flag_irqs;
1494
372d855f
TI
1495 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1496 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
1497 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
c75b0476 1498 dev_dbg(intelhaddata->dev, "had not connected\n");
372d855f
TI
1499 return 0;
1500 }
1501
1502 if (intelhaddata->drv_status != HAD_DRV_SUSPENDED) {
1503 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
c75b0476 1504 dev_err(intelhaddata->dev, "had is not in suspended state\n");
372d855f
TI
1505 return 0;
1506 }
1507
1508 if (had_get_hwstate(intelhaddata)) {
1509 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
c75b0476
TI
1510 dev_err(intelhaddata->dev,
1511 "Failed to resume. Device not accessible\n");
372d855f
TI
1512 return -ENODEV;
1513 }
1514
1515 intelhaddata->drv_status = HAD_DRV_CONNECTED;
c75b0476
TI
1516 dev_dbg(intelhaddata->dev,
1517 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
372d855f
TI
1518 __func__, __LINE__);
1519 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1520 snd_intelhad_enable_audio_int(intelhaddata, true);
372d855f
TI
1521 return 0;
1522}
1523
1524static inline int had_chk_intrmiss(struct snd_intelhad *intelhaddata,
1525 enum intel_had_aud_buf_type buf_id)
1526{
1527 int i, intr_count = 0;
1528 enum intel_had_aud_buf_type buff_done;
1529 u32 buf_size, buf_addr;
1530 struct had_stream_data *had_stream;
1531 unsigned long flag_irqs;
1532
1533 had_stream = &intelhaddata->stream_data;
1534
1535 buff_done = buf_id;
1536
1537 intr_count = snd_intelhad_read_len(intelhaddata);
1538 if (intr_count > 1) {
1539 /* In case of active playback */
c75b0476
TI
1540 dev_err(intelhaddata->dev,
1541 "Driver detected %d missed buffer done interrupt(s)\n",
1542 (intr_count - 1));
372d855f
TI
1543 if (intr_count > 3)
1544 return intr_count;
1545
1546 buf_id += (intr_count - 1);
1547 /* Reprogram registers*/
1548 for (i = buff_done; i < buf_id; i++) {
1549 int j = i % 4;
1550
1551 buf_size = intelhaddata->buf_info[j].buf_size;
1552 buf_addr = intelhaddata->buf_info[j].buf_addr;
1553 had_write_register(intelhaddata,
1554 AUD_BUF_A_LENGTH +
1555 (j * HAD_REG_WIDTH), buf_size);
1556 had_write_register(intelhaddata,
1557 AUD_BUF_A_ADDR+(j * HAD_REG_WIDTH),
1558 (buf_addr | BIT(0) | BIT(1)));
1559 }
1560 buf_id = buf_id % 4;
1561 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1562 intelhaddata->buff_done = buf_id;
1563 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1564 }
1565
1566 return intr_count;
1567}
1568
1569static int had_process_buffer_done(struct snd_intelhad *intelhaddata)
1570{
1571 u32 len = 1;
1572 enum intel_had_aud_buf_type buf_id;
1573 enum intel_had_aud_buf_type buff_done;
1574 struct pcm_stream_info *stream;
1575 u32 buf_size;
1576 struct had_stream_data *had_stream;
1577 int intr_count;
1578 enum had_status_stream stream_type;
1579 unsigned long flag_irqs;
1580
1581 had_stream = &intelhaddata->stream_data;
1582 stream = &intelhaddata->stream_info;
1583 intr_count = 1;
1584
1585 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1586 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
1587 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
c75b0476
TI
1588 dev_dbg(intelhaddata->dev,
1589 "%s:Device already disconnected\n", __func__);
372d855f
TI
1590 return 0;
1591 }
1592 buf_id = intelhaddata->curr_buf;
1593 intelhaddata->buff_done = buf_id;
1594 buff_done = intelhaddata->buff_done;
1595 buf_size = intelhaddata->buf_info[buf_id].buf_size;
1596 stream_type = had_stream->stream_type;
1597
372d855f
TI
1598 /* Every debug statement has an implication
1599 * of ~5msec. Thus, avoid having >3 debug statements
1600 * for each buffer_done handling.
1601 */
1602
1603 /* Check for any intr_miss in case of active playback */
1604 if (had_stream->stream_type == HAD_RUNNING_STREAM) {
1605 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1606 intr_count = had_chk_intrmiss(intelhaddata, buf_id);
1607 if (!intr_count || (intr_count > 3)) {
c75b0476
TI
1608 dev_err(intelhaddata->dev,
1609 "HAD SW state in non-recoverable mode\n");
372d855f
TI
1610 return 0;
1611 }
1612 buf_id += (intr_count - 1);
1613 buf_id = buf_id % 4;
1614 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1615 }
1616
1617 intelhaddata->buf_info[buf_id].is_valid = true;
1618 if (intelhaddata->valid_buf_cnt-1 == buf_id) {
1619 if (had_stream->stream_type >= HAD_RUNNING_STREAM)
1620 intelhaddata->curr_buf = HAD_BUF_TYPE_A;
1621 } else
1622 intelhaddata->curr_buf = buf_id + 1;
1623
1624 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1625
1626 if (had_get_hwstate(intelhaddata)) {
c75b0476 1627 dev_dbg(intelhaddata->dev, "HDMI cable plugged-out\n");
372d855f
TI
1628 return 0;
1629 }
1630
1631 /*Reprogram the registers with addr and length*/
1632 had_write_register(intelhaddata,
1633 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH),
1634 buf_size);
1635 had_write_register(intelhaddata,
1636 AUD_BUF_A_ADDR + (buf_id * HAD_REG_WIDTH),
1637 intelhaddata->buf_info[buf_id].buf_addr |
1638 BIT(0) | BIT(1));
1639
1640 had_read_register(intelhaddata,
1641 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH),
1642 &len);
c75b0476 1643 dev_dbg(intelhaddata->dev, "%s:Enabled buf[%d]\n", __func__, buf_id);
372d855f
TI
1644
1645 /* In case of actual data,
1646 * report buffer_done to above ALSA layer
1647 */
df76df12 1648 buf_size = intelhaddata->buf_info[buf_id].buf_size;
372d855f
TI
1649 if (stream_type >= HAD_RUNNING_STREAM) {
1650 intelhaddata->stream_info.buffer_rendered +=
1651 (intr_count * buf_size);
df76df12 1652 had_period_elapsed(stream->had_substream);
372d855f
TI
1653 }
1654
1655 return 0;
1656}
1657
1658static int had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
1659{
1660 enum intel_had_aud_buf_type buf_id;
1661 struct pcm_stream_info *stream;
1662 struct had_stream_data *had_stream;
1663 enum had_status_stream stream_type;
1664 unsigned long flag_irqs;
1665 int drv_status;
1666
1667 had_stream = &intelhaddata->stream_data;
1668 stream = &intelhaddata->stream_info;
1669
1670 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1671 buf_id = intelhaddata->curr_buf;
1672 stream_type = had_stream->stream_type;
1673 intelhaddata->buff_done = buf_id;
1674 drv_status = intelhaddata->drv_status;
1675 if (stream_type == HAD_RUNNING_STREAM)
1676 intelhaddata->curr_buf = HAD_BUF_TYPE_A;
1677
1678 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1679
c75b0476 1680 dev_dbg(intelhaddata->dev, "Enter:%s buf_id=%d, stream_type=%d\n",
372d855f
TI
1681 __func__, buf_id, stream_type);
1682
1683 snd_intelhad_handle_underrun(intelhaddata);
1684
1685 if (drv_status == HAD_DRV_DISCONNECTED) {
c75b0476
TI
1686 dev_dbg(intelhaddata->dev,
1687 "%s:Device already disconnected\n", __func__);
372d855f
TI
1688 return 0;
1689 }
1690
1691 if (stream_type == HAD_RUNNING_STREAM) {
1692 /* Report UNDERRUN error to above layers */
1693 intelhaddata->flag_underrun = 1;
df76df12 1694 had_period_elapsed(stream->had_substream);
372d855f
TI
1695 }
1696
1697 return 0;
1698}
1699
1700static int had_process_hot_plug(struct snd_intelhad *intelhaddata)
1701{
1702 enum intel_had_aud_buf_type buf_id;
1703 struct snd_pcm_substream *substream;
1704 struct had_stream_data *had_stream;
1705 unsigned long flag_irqs;
1706
372d855f
TI
1707 substream = intelhaddata->stream_info.had_substream;
1708 had_stream = &intelhaddata->stream_data;
1709
1710 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1711 if (intelhaddata->drv_status == HAD_DRV_CONNECTED) {
c75b0476 1712 dev_dbg(intelhaddata->dev, "Device already connected\n");
372d855f
TI
1713 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1714 return 0;
1715 }
1716 buf_id = intelhaddata->curr_buf;
1717 intelhaddata->buff_done = buf_id;
1718 intelhaddata->drv_status = HAD_DRV_CONNECTED;
c75b0476
TI
1719 dev_dbg(intelhaddata->dev,
1720 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
372d855f
TI
1721 __func__, __LINE__);
1722 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1723
c75b0476
TI
1724 dev_dbg(intelhaddata->dev, "Processing HOT_PLUG, buf_id = %d\n",
1725 buf_id);
372d855f
TI
1726
1727 /* Safety check */
1728 if (substream) {
c75b0476
TI
1729 dev_dbg(intelhaddata->dev,
1730 "Force to stop the active stream by disconnection\n");
372d855f
TI
1731 /* Set runtime->state to hw_params done */
1732 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1733 }
1734
1735 had_build_channel_allocation_map(intelhaddata);
1736
1737 return 0;
1738}
1739
1740static int had_process_hot_unplug(struct snd_intelhad *intelhaddata)
1741{
1742 enum intel_had_aud_buf_type buf_id;
1743 struct had_stream_data *had_stream;
1744 unsigned long flag_irqs;
1745
372d855f
TI
1746 had_stream = &intelhaddata->stream_data;
1747 buf_id = intelhaddata->curr_buf;
1748
1749 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1750
1751 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
c75b0476 1752 dev_dbg(intelhaddata->dev, "Device already disconnected\n");
372d855f
TI
1753 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1754 return 0;
1755
1756 } else {
1757 /* Disable Audio */
1758 snd_intelhad_enable_audio_int(intelhaddata, false);
1759 snd_intelhad_enable_audio(intelhaddata, false);
1760 }
1761
1762 intelhaddata->drv_status = HAD_DRV_DISCONNECTED;
c75b0476
TI
1763 dev_dbg(intelhaddata->dev,
1764 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
372d855f
TI
1765 __func__, __LINE__);
1766
1767 /* Report to above ALSA layer */
1768 if (intelhaddata->stream_info.had_substream != NULL) {
1769 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
372d855f
TI
1770 snd_pcm_stop(intelhaddata->stream_info.had_substream,
1771 SNDRV_PCM_STATE_SETUP);
1772 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1773 }
1774
1775 had_stream->stream_type = HAD_INIT;
1776 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1777 kfree(intelhaddata->chmap->chmap);
1778 intelhaddata->chmap->chmap = NULL;
1779 intelhaddata->audio_reg_base = NULL;
372d855f
TI
1780
1781 return 0;
1782}
1783
1784/* PCM operations structure and the calls back for the same */
1785static struct snd_pcm_ops snd_intelhad_playback_ops = {
5dab11d8
JA
1786 .open = snd_intelhad_open,
1787 .close = snd_intelhad_close,
1788 .ioctl = snd_pcm_lib_ioctl,
1789 .hw_params = snd_intelhad_hw_params,
1790 .hw_free = snd_intelhad_hw_free,
1791 .prepare = snd_intelhad_pcm_prepare,
1792 .trigger = snd_intelhad_pcm_trigger,
1793 .pointer = snd_intelhad_pcm_pointer,
1794 .mmap = snd_intelhad_pcm_mmap,
1795};
1796
372d855f 1797/*
5dab11d8
JA
1798 * snd_intelhad_pcm_free - to free the memory allocated
1799 *
1800 * @pcm: pointer to pcm instance
1801 * This function is called when the device is removed
1802 */
1803static void snd_intelhad_pcm_free(struct snd_pcm *pcm)
1804{
5dab11d8
JA
1805 snd_pcm_lib_preallocate_free_for_all(pcm);
1806}
1807
1808static int had_iec958_info(struct snd_kcontrol *kcontrol,
1809 struct snd_ctl_elem_info *uinfo)
1810{
1811 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1812 uinfo->count = 1;
1813 return 0;
1814}
1815
1816static int had_iec958_get(struct snd_kcontrol *kcontrol,
1817 struct snd_ctl_elem_value *ucontrol)
1818{
1819 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1820
1821 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1822 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1823 ucontrol->value.iec958.status[2] =
1824 (intelhaddata->aes_bits >> 16) & 0xff;
1825 ucontrol->value.iec958.status[3] =
1826 (intelhaddata->aes_bits >> 24) & 0xff;
1827 return 0;
1828}
372d855f 1829
5dab11d8
JA
1830static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1831 struct snd_ctl_elem_value *ucontrol)
1832{
1833 ucontrol->value.iec958.status[0] = 0xff;
1834 ucontrol->value.iec958.status[1] = 0xff;
1835 ucontrol->value.iec958.status[2] = 0xff;
1836 ucontrol->value.iec958.status[3] = 0xff;
1837 return 0;
1838}
372d855f 1839
5dab11d8
JA
1840static int had_iec958_put(struct snd_kcontrol *kcontrol,
1841 struct snd_ctl_elem_value *ucontrol)
1842{
1843 unsigned int val;
1844 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1845
5dab11d8
JA
1846 val = (ucontrol->value.iec958.status[0] << 0) |
1847 (ucontrol->value.iec958.status[1] << 8) |
1848 (ucontrol->value.iec958.status[2] << 16) |
1849 (ucontrol->value.iec958.status[3] << 24);
1850 if (intelhaddata->aes_bits != val) {
1851 intelhaddata->aes_bits = val;
1852 return 1;
1853 }
1854 return 1;
1855}
1856
1857static struct snd_kcontrol_new had_control_iec958_mask = {
1858 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1859 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1860 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1861 .info = had_iec958_info, /* shared */
1862 .get = had_iec958_mask_get,
1863};
1864
1865static struct snd_kcontrol_new had_control_iec958 = {
1866 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1867 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1868 .info = had_iec958_info,
1869 .get = had_iec958_get,
1870 .put = had_iec958_put
1871};
1872
da864809
TI
1873static void _had_wq(struct work_struct *work)
1874{
1875 struct snd_intelhad *ctx =
1876 container_of(work, struct snd_intelhad, hdmi_audio_wq);
1877
1878 had_process_hot_plug(ctx);
1879}
1880
1881static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id)
1882{
1883 struct snd_intelhad *ctx = dev_id;
1884 u32 audio_stat, audio_reg;
1885
1886 audio_reg = AUD_HDMI_STATUS_v2;
1887 mid_hdmi_audio_read(ctx, audio_reg, &audio_stat);
1888
1889 if (audio_stat & HDMI_AUDIO_UNDERRUN) {
1890 mid_hdmi_audio_write(ctx, audio_reg, HDMI_AUDIO_UNDERRUN);
1891 had_process_buffer_underrun(ctx);
1892 }
1893
1894 if (audio_stat & HDMI_AUDIO_BUFFER_DONE) {
1895 mid_hdmi_audio_write(ctx, audio_reg, HDMI_AUDIO_BUFFER_DONE);
1896 had_process_buffer_done(ctx);
1897 }
1898
1899 return IRQ_HANDLED;
1900}
1901
1902static void notify_audio_lpe(struct platform_device *pdev)
1903{
1904 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
1905 struct intel_hdmi_lpe_audio_pdata *pdata = pdev->dev.platform_data;
1906
1907 if (pdata->hdmi_connected != true) {
1908
1909 dev_dbg(&pdev->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG\n",
1910 __func__);
1911
1912 if (ctx->state == hdmi_connector_status_connected) {
1913
1914 ctx->state = hdmi_connector_status_disconnected;
1915
1916 had_process_hot_unplug(ctx);
1917 } else
1918 dev_dbg(&pdev->dev, "%s: Already Unplugged!\n",
1919 __func__);
1920
1921 } else {
1922 struct intel_hdmi_lpe_audio_eld *eld = &pdata->eld;
1923
1924 switch (eld->pipe_id) {
1925 case 0:
1926 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1927 break;
1928 case 1:
1929 ctx->had_config_offset = AUDIO_HDMI_CONFIG_B;
1930 break;
1931 case 2:
1932 ctx->had_config_offset = AUDIO_HDMI_CONFIG_C;
1933 break;
1934 default:
1935 dev_dbg(&pdev->dev, "Invalid pipe %d\n",
1936 eld->pipe_id);
1937 break;
1938 }
1939
1940 memcpy(&ctx->eld, eld->eld_data, sizeof(ctx->eld));
1941
1942 had_process_hot_plug(ctx);
1943
1944 ctx->state = hdmi_connector_status_connected;
1945
1946 dev_dbg(&pdev->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
1947 __func__, eld->port_id, pdata->tmds_clock_speed);
1948
1949 if (pdata->tmds_clock_speed) {
1950 ctx->tmds_clock_speed = pdata->tmds_clock_speed;
1951 ctx->dp_output = pdata->dp_output;
1952 ctx->link_rate = pdata->link_rate;
1953
1954 /* Process mode change if stream is active */
1955 if (ctx->stream_data.stream_type == HAD_RUNNING_STREAM)
1956 hdmi_audio_mode_change(ctx);
1957 }
1958 }
1959}
1960
1961/* release resources */
1962static void hdmi_lpe_audio_free(struct snd_card *card)
1963{
1964 struct snd_intelhad *ctx = card->private_data;
1965
1966 if (ctx->mmio_start)
1967 iounmap(ctx->mmio_start);
1968 if (ctx->irq >= 0)
1969 free_irq(ctx->irq, ctx);
1970}
1971
79dda75a 1972/*
da864809 1973 * hdmi_lpe_audio_probe - start bridge with i915
5dab11d8 1974 *
da864809
TI
1975 * This function is called when the i915 driver creates the
1976 * hdmi-lpe-audio platform device. Card creation is deferred until a
1977 * hot plug event is received
5dab11d8 1978 */
da864809 1979static int hdmi_lpe_audio_probe(struct platform_device *pdev)
5dab11d8 1980{
5dab11d8 1981 struct snd_card *card;
da864809
TI
1982 struct snd_intelhad *ctx;
1983 struct snd_pcm *pcm;
1984 struct intel_hdmi_lpe_audio_pdata *pdata;
1985 int irq;
1986 struct resource *res_mmio;
1987 int ret;
1988 unsigned long flags;
1989
da864809
TI
1990 dev_dbg(&pdev->dev, "dma_mask: %p\n", pdev->dev.dma_mask);
1991
1992 pdata = pdev->dev.platform_data;
1993 if (!pdata) {
1994 dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
1995 return -EINVAL;
1996 }
5dab11d8 1997
da864809
TI
1998 /* get resources */
1999 irq = platform_get_irq(pdev, 0);
2000 if (irq < 0) {
2001 dev_err(&pdev->dev, "Could not get irq resource\n");
2002 return -ENODEV;
2003 }
2004
2005 res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2006 if (!res_mmio) {
2007 dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
2008 return -ENXIO;
2009 }
5dab11d8 2010
5647aec2 2011 /* create a card instance with ALSA framework */
da864809
TI
2012 ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
2013 THIS_MODULE, sizeof(*ctx), &card);
2014 if (ret)
2015 return ret;
2016
2017 ctx = card->private_data;
2018 spin_lock_init(&ctx->had_spinlock);
2019 ctx->drv_status = HAD_DRV_DISCONNECTED;
2020 ctx->dev = &pdev->dev;
2021 ctx->card = card;
2022 ctx->card_id = hdmi_card_id;
2023 ctx->card_index = card->number;
2024 ctx->flag_underrun = 0;
2025 ctx->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
2026 strcpy(card->driver, INTEL_HAD);
2027 strcpy(card->shortname, INTEL_HAD);
2028
2029 ctx->irq = -1;
2030 ctx->tmds_clock_speed = DIS_SAMPLE_RATE_148_5;
2031 INIT_WORK(&ctx->hdmi_audio_wq, _had_wq);
2032 ctx->state = hdmi_connector_status_disconnected;
2033
2034 card->private_free = hdmi_lpe_audio_free;
2035
2036 /* assume pipe A as default */
2037 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
2038
2039 platform_set_drvdata(pdev, ctx);
2040
2041 dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
2042 __func__, (unsigned int)res_mmio->start,
2043 (unsigned int)res_mmio->end);
2044
2045 ctx->mmio_start = ioremap_nocache(res_mmio->start,
2046 (size_t)(resource_size(res_mmio)));
2047 if (!ctx->mmio_start) {
2048 dev_err(&pdev->dev, "Could not get ioremap\n");
2049 ret = -EACCES;
2050 goto err;
2051 }
5dab11d8 2052
da864809
TI
2053 /* setup interrupt handler */
2054 ret = request_irq(irq, display_pipe_interrupt_handler, 0,
2055 pdev->name, ctx);
2056 if (ret < 0) {
2057 dev_err(&pdev->dev, "request_irq failed\n");
2058 goto err;
2059 }
5dab11d8 2060
da864809
TI
2061 ctx->irq = irq;
2062
2063 ret = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
2064 MAX_CAP_STREAMS, &pcm);
2065 if (ret)
5dab11d8
JA
2066 goto err;
2067
2068 /* setup private data which can be retrieved when required */
da864809 2069 pcm->private_data = ctx;
5dab11d8
JA
2070 pcm->private_free = snd_intelhad_pcm_free;
2071 pcm->info_flags = 0;
2072 strncpy(pcm->name, card->shortname, strlen(card->shortname));
da864809 2073 /* setup the ops for playabck */
5dab11d8
JA
2074 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2075 &snd_intelhad_playback_ops);
2076 /* allocate dma pages for ALSA stream operations
2077 * memory allocated is based on size, not max value
2078 * thus using same argument for max & size
2079 */
da864809 2080 snd_pcm_lib_preallocate_pages_for_all(pcm,
5dab11d8
JA
2081 SNDRV_DMA_TYPE_DEV, NULL,
2082 HAD_MAX_BUFFER, HAD_MAX_BUFFER);
5dab11d8 2083
5dab11d8 2084 /* IEC958 controls */
da864809
TI
2085 ret = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958_mask, ctx));
2086 if (ret < 0)
5dab11d8 2087 goto err;
da864809
TI
2088 ret = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958, ctx));
2089 if (ret < 0)
5dab11d8
JA
2090 goto err;
2091
2092 init_channel_allocations();
2093
2094 /* Register channel map controls */
da864809
TI
2095 ret = had_register_chmap_ctls(ctx, pcm);
2096 if (ret < 0)
5dab11d8
JA
2097 goto err;
2098
da864809
TI
2099 ret = snd_card_register(card);
2100 if (ret)
36ec0d99
TI
2101 goto err;
2102
da864809
TI
2103 spin_lock_irqsave(&pdata->lpe_audio_slock, flags);
2104 pdata->notify_audio_lpe = notify_audio_lpe;
2105 if (pdata->notify_pending) {
5dab11d8 2106
da864809
TI
2107 dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
2108 notify_audio_lpe(pdev);
2109 pdata->notify_pending = false;
2110 }
2111 spin_unlock_irqrestore(&pdata->lpe_audio_slock, flags);
2112
2113 pm_runtime_set_active(&pdev->dev);
2114 pm_runtime_enable(&pdev->dev);
2115
2116 schedule_work(&ctx->hdmi_audio_wq);
5dab11d8 2117
79dda75a 2118 return 0;
5647aec2 2119
5dab11d8
JA
2120err:
2121 snd_card_free(card);
da864809 2122 return ret;
5dab11d8
JA
2123}
2124
79dda75a 2125/*
da864809 2126 * hdmi_lpe_audio_remove - stop bridge with i915
5dab11d8 2127 *
da864809
TI
2128 * This function is called when the platform device is destroyed. The sound
2129 * card should have been removed on hot plug event.
5dab11d8 2130 */
da864809 2131static int hdmi_lpe_audio_remove(struct platform_device *pdev)
5dab11d8 2132{
da864809 2133 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
5dab11d8 2134
da864809
TI
2135 if (ctx->drv_status != HAD_DRV_DISCONNECTED)
2136 snd_intelhad_enable_audio_int(ctx, false);
2137 snd_card_free(ctx->card);
5dab11d8
JA
2138 return 0;
2139}
2140
da864809
TI
2141static struct platform_driver hdmi_lpe_audio_driver = {
2142 .driver = {
2143 .name = "hdmi-lpe-audio",
2144 },
2145 .probe = hdmi_lpe_audio_probe,
2146 .remove = hdmi_lpe_audio_remove,
2147 .suspend = hdmi_lpe_audio_suspend,
2148 .resume = hdmi_lpe_audio_resume
2149};
2150
2151module_platform_driver(hdmi_lpe_audio_driver);
2152MODULE_ALIAS("platform:hdmi_lpe_audio");
2153
5dab11d8
JA
2154MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
2155MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
2156MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
2157MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
2158MODULE_DESCRIPTION("Intel HDMI Audio driver");
2159MODULE_LICENSE("GPL v2");
2160MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");