blob: 45ba16e2732398a87059ac5e54c0b8560c64a5f8 [file] [log] [blame]
Jerome Anand5dab11d2017-01-25 04:27:52 +05301/*
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
24#define pr_fmt(fmt) "had: " fmt
25
26#include <linux/platform_device.h>
27#include <linux/io.h>
28#include <linux/slab.h>
29#include <linux/module.h>
30#include <linux/acpi.h>
31#include <asm/cacheflush.h>
32#include <sound/pcm.h>
33#include <sound/core.h>
34#include <sound/pcm_params.h>
35#include <sound/initval.h>
36#include <sound/control.h>
37#include <sound/initval.h>
38#include "intel_hdmi_audio.h"
39
40static DEFINE_MUTEX(had_mutex);
41
42/*standard module options for ALSA. This module supports only one card*/
43static int hdmi_card_index = SNDRV_DEFAULT_IDX1;
44static char *hdmi_card_id = SNDRV_DEFAULT_STR1;
Jerome Anand232892f2017-01-25 04:27:53 +053045static int underrun_count;
Jerome Anand5dab11d2017-01-25 04:27:52 +053046
47module_param_named(index, hdmi_card_index, int, 0444);
48MODULE_PARM_DESC(index,
49 "Index value for INTEL Intel HDMI Audio controller.");
50module_param_named(id, hdmi_card_id, charp, 0444);
51MODULE_PARM_DESC(id,
52 "ID string for INTEL Intel HDMI Audio controller.");
53
54/*
55 * ELD SA bits in the CEA Speaker Allocation data block
56 */
57static int eld_speaker_allocation_bits[] = {
58 [0] = FL | FR,
59 [1] = LFE,
60 [2] = FC,
61 [3] = RL | RR,
62 [4] = RC,
63 [5] = FLC | FRC,
64 [6] = RLC | RRC,
65 /* the following are not defined in ELD yet */
66 [7] = 0,
67};
68
69/*
70 * This is an ordered list!
71 *
72 * The preceding ones have better chances to be selected by
73 * hdmi_channel_allocation().
74 */
75static struct cea_channel_speaker_allocation channel_allocations[] = {
76/* channel: 7 6 5 4 3 2 1 0 */
77{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
78 /* 2.1 */
79{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
80 /* Dolby Surround */
81{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
82 /* surround40 */
83{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
84 /* surround41 */
85{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
86 /* surround50 */
87{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
88 /* surround51 */
89{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
90 /* 6.1 */
91{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
92 /* surround71 */
93{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
94
95{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
96{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
97{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
98{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
99{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
100{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
101{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
102{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
103{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
104{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
105{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
106{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
107{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
108{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
109{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
110{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
111{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
112{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
113{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
114{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
115{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
116{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
117{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
118};
119
120static struct channel_map_table map_tables[] = {
121 { SNDRV_CHMAP_FL, 0x00, FL },
122 { SNDRV_CHMAP_FR, 0x01, FR },
123 { SNDRV_CHMAP_RL, 0x04, RL },
124 { SNDRV_CHMAP_RR, 0x05, RR },
125 { SNDRV_CHMAP_LFE, 0x02, LFE },
126 { SNDRV_CHMAP_FC, 0x03, FC },
127 { SNDRV_CHMAP_RLC, 0x06, RLC },
128 { SNDRV_CHMAP_RRC, 0x07, RRC },
129 {} /* terminator */
130};
131
132/* hardware capability structure */
133static const struct snd_pcm_hardware snd_intel_hadstream = {
134 .info = (SNDRV_PCM_INFO_INTERLEAVED |
135 SNDRV_PCM_INFO_DOUBLE |
136 SNDRV_PCM_INFO_MMAP|
137 SNDRV_PCM_INFO_MMAP_VALID |
138 SNDRV_PCM_INFO_BATCH),
139 .formats = (SNDRV_PCM_FMTBIT_S24 |
140 SNDRV_PCM_FMTBIT_U24),
141 .rates = SNDRV_PCM_RATE_32000 |
142 SNDRV_PCM_RATE_44100 |
143 SNDRV_PCM_RATE_48000 |
144 SNDRV_PCM_RATE_88200 |
145 SNDRV_PCM_RATE_96000 |
146 SNDRV_PCM_RATE_176400 |
147 SNDRV_PCM_RATE_192000,
148 .rate_min = HAD_MIN_RATE,
149 .rate_max = HAD_MAX_RATE,
150 .channels_min = HAD_MIN_CHANNEL,
151 .channels_max = HAD_MAX_CHANNEL,
152 .buffer_bytes_max = HAD_MAX_BUFFER,
153 .period_bytes_min = HAD_MIN_PERIOD_BYTES,
154 .period_bytes_max = HAD_MAX_PERIOD_BYTES,
155 .periods_min = HAD_MIN_PERIODS,
156 .periods_max = HAD_MAX_PERIODS,
157 .fifo_size = HAD_FIFO_SIZE,
158};
159
160/* Register access functions */
161
162int had_get_hwstate(struct snd_intelhad *intelhaddata)
163{
164 /* Check for device presence -SW state */
165 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
166 pr_debug("%s:Device not connected:%d\n", __func__,
167 intelhaddata->drv_status);
168 return -ENODEV;
169 }
170
171 return 0;
172}
173
Takashi Iwai79dda752017-01-30 17:23:39 +0100174int had_get_caps(struct snd_intelhad *intelhaddata,
175 enum had_caps_list query, void *caps)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530176{
177 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530178
179 retval = had_get_hwstate(intelhaddata);
180 if (!retval)
Takashi Iwai9eca88c2017-01-30 16:37:06 +0100181 retval = mid_hdmi_audio_get_caps(query, caps);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530182
183 return retval;
184}
185
Takashi Iwai79dda752017-01-30 17:23:39 +0100186int had_set_caps(struct snd_intelhad *intelhaddata,
187 enum had_caps_list set_element, void *caps)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530188{
189 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530190
191 retval = had_get_hwstate(intelhaddata);
192 if (!retval)
Takashi Iwai9eca88c2017-01-30 16:37:06 +0100193 retval = mid_hdmi_audio_set_caps(set_element, caps);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530194
195 return retval;
196}
197
Takashi Iwai79dda752017-01-30 17:23:39 +0100198int had_read_register(struct snd_intelhad *intelhaddata, u32 offset, u32 *data)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530199{
200 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530201
202 retval = had_get_hwstate(intelhaddata);
203 if (!retval)
Takashi Iwaif23df802017-01-30 16:29:39 +0100204 retval = mid_hdmi_audio_read(offset, data);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530205
206 return retval;
207}
208
Takashi Iwai79dda752017-01-30 17:23:39 +0100209int had_write_register(struct snd_intelhad *intelhaddata, u32 offset, u32 data)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530210{
211 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530212
213 retval = had_get_hwstate(intelhaddata);
214 if (!retval)
Takashi Iwaif23df802017-01-30 16:29:39 +0100215 retval = mid_hdmi_audio_write(offset, data);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530216
217 return retval;
218}
219
Takashi Iwai79dda752017-01-30 17:23:39 +0100220int had_read_modify(struct snd_intelhad *intelhaddata, u32 offset,
221 u32 data, u32 mask)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530222{
223 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530224
225 retval = had_get_hwstate(intelhaddata);
226 if (!retval)
Takashi Iwaif23df802017-01-30 16:29:39 +0100227 retval = mid_hdmi_audio_rmw(offset, data, mask);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530228
229 return retval;
230}
231/**
232 * function to read-modify
233 * AUD_CONFIG register on VLV2.The had_read_modify() function should not
234 * directly be used on VLV2 for updating AUD_CONFIG register.
235 * This is because:
236 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
237 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
238 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
239 * register. This field should be 1xy binary for configuration with 6 or
240 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
241 * causes the "channels" field to be updated as 0xy binary resulting in
242 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
243 * appropriate value when doing read-modify of AUD_CONFIG register.
244 *
245 * @substream: the current substream or NULL if no active substream
246 * @data : data to be written
247 * @mask : mask
248 *
249 */
250static int had_read_modify_aud_config_v2(struct snd_pcm_substream *substream,
251 u32 data, u32 mask)
252{
Takashi Iwai79dda752017-01-30 17:23:39 +0100253 struct snd_intelhad *intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530254 union aud_cfg cfg_val = {.cfg_regval = 0};
255 u8 channels;
256
257 /*
258 * If substream is NULL, there is no active stream.
259 * In this case just set channels to 2
260 */
261 if (substream)
262 channels = substream->runtime->channels;
263 else
264 channels = 2;
265 cfg_val.cfg_regx_v2.num_ch = channels - 2;
266
267 data = data | cfg_val.cfg_regval;
268 mask = mask | AUD_CONFIG_CH_MASK_V2;
269
270 pr_debug("%s : data = %x, mask =%x\n", __func__, data, mask);
271
Takashi Iwai79dda752017-01-30 17:23:39 +0100272 return had_read_modify(intelhaddata, AUD_CONFIG, data, mask);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530273}
274
Takashi Iwai76296ef2017-01-30 16:09:11 +0100275void snd_intelhad_enable_audio(struct snd_pcm_substream *substream, u8 enable)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530276{
277 had_read_modify_aud_config_v2(substream, enable, BIT(0));
278}
279
Takashi Iwai79dda752017-01-30 17:23:39 +0100280static void snd_intelhad_reset_audio(struct snd_intelhad *intelhaddata,
281 u8 reset)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530282{
Takashi Iwai79dda752017-01-30 17:23:39 +0100283 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, reset);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530284}
285
286/**
287 * initialize audio channel status registers
288 * This function is called in the prepare callback
289 */
290static int had_prog_status_reg(struct snd_pcm_substream *substream,
291 struct snd_intelhad *intelhaddata)
292{
293 union aud_cfg cfg_val = {.cfg_regval = 0};
294 union aud_ch_status_0 ch_stat0 = {.status_0_regval = 0};
295 union aud_ch_status_1 ch_stat1 = {.status_1_regval = 0};
296 int format;
297
298 pr_debug("Entry %s\n", __func__);
299
300 ch_stat0.status_0_regx.lpcm_id = (intelhaddata->aes_bits &
301 IEC958_AES0_NONAUDIO)>>1;
302 ch_stat0.status_0_regx.clk_acc = (intelhaddata->aes_bits &
303 IEC958_AES3_CON_CLOCK)>>4;
Takashi Iwai4812dcc2017-01-30 15:58:15 +0100304 cfg_val.cfg_regx_v2.val_bit = ch_stat0.status_0_regx.lpcm_id;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530305
306 switch (substream->runtime->rate) {
307 case AUD_SAMPLE_RATE_32:
308 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_32KHZ;
309 break;
310
311 case AUD_SAMPLE_RATE_44_1:
312 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_44KHZ;
313 break;
314 case AUD_SAMPLE_RATE_48:
315 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_48KHZ;
316 break;
317 case AUD_SAMPLE_RATE_88_2:
318 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_88KHZ;
319 break;
320 case AUD_SAMPLE_RATE_96:
321 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_96KHZ;
322 break;
323 case AUD_SAMPLE_RATE_176_4:
324 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_176KHZ;
325 break;
326 case AUD_SAMPLE_RATE_192:
327 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_192KHZ;
328 break;
329
330 default:
331 /* control should never come here */
332 return -EINVAL;
333 break;
334
335 }
Takashi Iwai79dda752017-01-30 17:23:39 +0100336 had_write_register(intelhaddata,
337 AUD_CH_STATUS_0, ch_stat0.status_0_regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530338
339 format = substream->runtime->format;
340
341 if (format == SNDRV_PCM_FORMAT_S16_LE) {
342 ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_20;
343 ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_16BITS;
344 } else if (format == SNDRV_PCM_FORMAT_S24_LE) {
345 ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_24;
346 ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_24BITS;
347 } else {
348 ch_stat1.status_1_regx.max_wrd_len = 0;
349 ch_stat1.status_1_regx.wrd_len = 0;
350 }
Takashi Iwai79dda752017-01-30 17:23:39 +0100351 had_write_register(intelhaddata,
352 AUD_CH_STATUS_1, ch_stat1.status_1_regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530353 return 0;
354}
355
Takashi Iwai76296ef2017-01-30 16:09:11 +0100356/*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530357 * function to initialize audio
358 * registers and buffer confgiuration registers
359 * This function is called in the prepare callback
360 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100361static int snd_intelhad_audio_ctrl(struct snd_pcm_substream *substream,
362 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530363{
364 union aud_cfg cfg_val = {.cfg_regval = 0};
365 union aud_buf_config buf_cfg = {.buf_cfgval = 0};
366 u8 channels;
367
368 had_prog_status_reg(substream, intelhaddata);
369
370 buf_cfg.buf_cfg_regx_v2.audio_fifo_watermark = FIFO_THRESHOLD;
371 buf_cfg.buf_cfg_regx_v2.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
372 buf_cfg.buf_cfg_regx_v2.aud_delay = 0;
Takashi Iwai79dda752017-01-30 17:23:39 +0100373 had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.buf_cfgval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530374
375 channels = substream->runtime->channels;
376 cfg_val.cfg_regx_v2.num_ch = channels - 2;
377 if (channels <= 2)
378 cfg_val.cfg_regx_v2.layout = LAYOUT0;
379 else
380 cfg_val.cfg_regx_v2.layout = LAYOUT1;
381
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600382 cfg_val.cfg_regx_v2.val_bit = 1;
Takashi Iwai79dda752017-01-30 17:23:39 +0100383 had_write_register(intelhaddata, AUD_CONFIG, cfg_val.cfg_regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530384 return 0;
385}
386
Jerome Anand5dab11d2017-01-25 04:27:52 +0530387/*
388 * Compute derived values in channel_allocations[].
389 */
390static void init_channel_allocations(void)
391{
392 int i, j;
393 struct cea_channel_speaker_allocation *p;
394
395 pr_debug("%s: Enter\n", __func__);
396
397 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
398 p = channel_allocations + i;
399 p->channels = 0;
400 p->spk_mask = 0;
401 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
402 if (p->speakers[j]) {
403 p->channels++;
404 p->spk_mask |= p->speakers[j];
405 }
406 }
407}
408
409/*
410 * The transformation takes two steps:
411 *
412 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
413 * spk_mask => (channel_allocations[]) => ai->CA
414 *
415 * TODO: it could select the wrong CA from multiple candidates.
416 */
417static int snd_intelhad_channel_allocation(struct snd_intelhad *intelhaddata,
418 int channels)
419{
420 int i;
421 int ca = 0;
422 int spk_mask = 0;
423
424 /*
425 * CA defaults to 0 for basic stereo audio
426 */
427 if (channels <= 2)
428 return 0;
429
430 /*
431 * expand ELD's speaker allocation mask
432 *
433 * ELD tells the speaker mask in a compact(paired) form,
434 * expand ELD's notions to match the ones used by Audio InfoFrame.
435 */
436
437 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
438 if (intelhaddata->eeld.speaker_allocation_block & (1 << i))
439 spk_mask |= eld_speaker_allocation_bits[i];
440 }
441
442 /* search for the first working match in the CA table */
443 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
444 if (channels == channel_allocations[i].channels &&
445 (spk_mask & channel_allocations[i].spk_mask) ==
446 channel_allocations[i].spk_mask) {
447 ca = channel_allocations[i].ca_index;
448 break;
449 }
450 }
451
452 pr_debug("HDMI: select CA 0x%x for %d\n", ca, channels);
453
454 return ca;
455}
456
457/* from speaker bit mask to ALSA API channel position */
458static int spk_to_chmap(int spk)
459{
460 struct channel_map_table *t = map_tables;
461
462 for (; t->map; t++) {
463 if (t->spk_mask == spk)
464 return t->map;
465 }
466 return 0;
467}
468
469void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
470{
471 int i = 0, c = 0;
472 int spk_mask = 0;
473 struct snd_pcm_chmap_elem *chmap;
474 u8 eld_high, eld_high_mask = 0xF0;
475 u8 high_msb;
476
477 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
478 if (chmap == NULL) {
479 intelhaddata->chmap->chmap = NULL;
480 return;
481 }
482
Takashi Iwai79dda752017-01-30 17:23:39 +0100483 had_get_caps(intelhaddata, HAD_GET_ELD, &intelhaddata->eeld);
484 had_get_caps(intelhaddata, HAD_GET_DP_OUTPUT, &intelhaddata->dp_output);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530485
486 pr_debug("eeld.speaker_allocation_block = %x\n",
487 intelhaddata->eeld.speaker_allocation_block);
488
489 /* WA: Fix the max channel supported to 8 */
490
491 /*
492 * Sink may support more than 8 channels, if eld_high has more than
493 * one bit set. SOC supports max 8 channels.
494 * Refer eld_speaker_allocation_bits, for sink speaker allocation
495 */
496
497 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
498 eld_high = intelhaddata->eeld.speaker_allocation_block & eld_high_mask;
499 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
500 /* eld_high & (eld_high-1): if more than 1 bit set */
501 /* 0x1F: 7 channels */
502 for (i = 1; i < 4; i++) {
503 high_msb = eld_high & (0x80 >> i);
504 if (high_msb) {
505 intelhaddata->eeld.speaker_allocation_block &=
506 high_msb | 0xF;
507 break;
508 }
509 }
510 }
511
512 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
513 if (intelhaddata->eeld.speaker_allocation_block & (1 << i))
514 spk_mask |= eld_speaker_allocation_bits[i];
515 }
516
517 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
518 if (spk_mask == channel_allocations[i].spk_mask) {
519 for (c = 0; c < channel_allocations[i].channels; c++) {
520 chmap->map[c] = spk_to_chmap(
521 channel_allocations[i].speakers[
522 (MAX_SPEAKERS - 1)-c]);
523 }
524 chmap->channels = channel_allocations[i].channels;
525 intelhaddata->chmap->chmap = chmap;
526 break;
527 }
528 }
529 if (i >= ARRAY_SIZE(channel_allocations)) {
530 intelhaddata->chmap->chmap = NULL;
531 kfree(chmap);
532 }
533}
534
535/*
536 * ALSA API channel-map control callbacks
537 */
538static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
539 struct snd_ctl_elem_info *uinfo)
540{
541 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
542 struct snd_intelhad *intelhaddata = info->private_data;
543
544 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
545 return -ENODEV;
546 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
547 uinfo->count = HAD_MAX_CHANNEL;
548 uinfo->value.integer.min = 0;
549 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
550 return 0;
551}
552
553static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
554 struct snd_ctl_elem_value *ucontrol)
555{
556 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
557 struct snd_intelhad *intelhaddata = info->private_data;
558 int i = 0;
559 const struct snd_pcm_chmap_elem *chmap;
560
561 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
562 return -ENODEV;
563 if (intelhaddata->chmap->chmap == NULL)
564 return -ENODATA;
565 chmap = intelhaddata->chmap->chmap;
566 for (i = 0; i < chmap->channels; i++) {
567 ucontrol->value.integer.value[i] = chmap->map[i];
568 pr_debug("chmap->map[%d] = %d\n", i, chmap->map[i]);
569 }
570
571 return 0;
572}
573
574static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
575 struct snd_pcm *pcm)
576{
577 int err = 0;
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;
586 intelhaddata->kctl = intelhaddata->chmap->kctl;
587 intelhaddata->kctl->info = had_chmap_ctl_info;
588 intelhaddata->kctl->get = had_chmap_ctl_get;
589 intelhaddata->chmap->chmap = NULL;
590 return 0;
591}
592
Takashi Iwai76296ef2017-01-30 16:09:11 +0100593/*
594 * snd_intelhad_prog_dip - to initialize Data Island Packets registers
Jerome Anand5dab11d2017-01-25 04:27:52 +0530595 *
596 * @substream:substream for which the prepare function is called
597 * @intelhaddata:substream private data
598 *
599 * This function is called in the prepare callback
600 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100601static void snd_intelhad_prog_dip(struct snd_pcm_substream *substream,
602 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530603{
604 int i;
605 union aud_ctrl_st ctrl_state = {.ctrl_val = 0};
606 union aud_info_frame2 frame2 = {.fr2_val = 0};
607 union aud_info_frame3 frame3 = {.fr3_val = 0};
608 u8 checksum = 0;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600609 u32 info_frame;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530610 int channels;
611
612 channels = substream->runtime->channels;
613
Takashi Iwai79dda752017-01-30 17:23:39 +0100614 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.ctrl_val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530615
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600616 if (intelhaddata->dp_output) {
617 info_frame = DP_INFO_FRAME_WORD1;
618 frame2.fr2_val = 1;
619 } else {
620 info_frame = HDMI_INFO_FRAME_WORD1;
621 frame2.fr2_regx.chnl_cnt = substream->runtime->channels - 1;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530622
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600623 frame3.fr3_regx.chnl_alloc = snd_intelhad_channel_allocation(
624 intelhaddata, channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530625
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600626 /*Calculte the byte wide checksum for all valid DIP words*/
627 for (i = 0; i < BYTES_PER_WORD; i++)
628 checksum += (info_frame >> i*BITS_PER_BYTE) & MASK_BYTE0;
629 for (i = 0; i < BYTES_PER_WORD; i++)
630 checksum += (frame2.fr2_val >> i*BITS_PER_BYTE) & MASK_BYTE0;
631 for (i = 0; i < BYTES_PER_WORD; i++)
632 checksum += (frame3.fr3_val >> i*BITS_PER_BYTE) & MASK_BYTE0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530633
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600634 frame2.fr2_regx.chksum = -(checksum);
635 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530636
Takashi Iwai79dda752017-01-30 17:23:39 +0100637 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, info_frame);
638 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, frame2.fr2_val);
639 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, frame3.fr3_val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530640
641 /* program remaining DIP words with zero */
642 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
Takashi Iwai79dda752017-01-30 17:23:39 +0100643 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, 0x0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530644
645 ctrl_state.ctrl_regx.dip_freq = 1;
646 ctrl_state.ctrl_regx.dip_en_sta = 1;
Takashi Iwai79dda752017-01-30 17:23:39 +0100647 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.ctrl_val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530648}
649
650/**
651 * snd_intelhad_prog_buffer - programs buffer
652 * address and length registers
653 *
654 * @substream:substream for which the prepare function is called
655 * @intelhaddata:substream private data
656 *
657 * This function programs ring buffer address and length into registers.
658 */
659int snd_intelhad_prog_buffer(struct snd_intelhad *intelhaddata,
660 int start, int end)
661{
662 u32 ring_buf_addr, ring_buf_size, period_bytes;
663 u8 i, num_periods;
664 struct snd_pcm_substream *substream;
665
666 substream = intelhaddata->stream_info.had_substream;
667 if (!substream) {
668 pr_err("substream is NULL\n");
669 dump_stack();
670 return 0;
671 }
672
673 ring_buf_addr = substream->runtime->dma_addr;
674 ring_buf_size = snd_pcm_lib_buffer_bytes(substream);
675 intelhaddata->stream_info.ring_buf_size = ring_buf_size;
676 period_bytes = frames_to_bytes(substream->runtime,
677 substream->runtime->period_size);
678 num_periods = substream->runtime->periods;
679
680 /*
681 * buffer addr should be 64 byte aligned, period bytes
682 * will be used to calculate addr offset
683 */
684 period_bytes &= ~0x3F;
685
686 /* Hardware supports MAX_PERIODS buffers */
687 if (end >= HAD_MAX_PERIODS)
688 return -EINVAL;
689
690 for (i = start; i <= end; i++) {
691 /* Program the buf registers with addr and len */
692 intelhaddata->buf_info[i].buf_addr = ring_buf_addr +
693 (i * period_bytes);
694 if (i < num_periods-1)
695 intelhaddata->buf_info[i].buf_size = period_bytes;
696 else
697 intelhaddata->buf_info[i].buf_size = ring_buf_size -
698 (period_bytes*i);
699
Takashi Iwai79dda752017-01-30 17:23:39 +0100700 had_write_register(intelhaddata,
701 AUD_BUF_A_ADDR + (i * HAD_REG_WIDTH),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530702 intelhaddata->buf_info[i].buf_addr |
703 BIT(0) | BIT(1));
Takashi Iwai79dda752017-01-30 17:23:39 +0100704 had_write_register(intelhaddata,
705 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530706 period_bytes);
707 intelhaddata->buf_info[i].is_valid = true;
708 }
709 pr_debug("%s:buf[%d-%d] addr=%#x and size=%d\n", __func__, start, end,
710 intelhaddata->buf_info[start].buf_addr,
711 intelhaddata->buf_info[start].buf_size);
712 intelhaddata->valid_buf_cnt = num_periods;
713 return 0;
714}
715
716int snd_intelhad_read_len(struct snd_intelhad *intelhaddata)
717{
718 int i, retval = 0;
719 u32 len[4];
720
721 for (i = 0; i < 4 ; i++) {
Takashi Iwai79dda752017-01-30 17:23:39 +0100722 had_read_register(intelhaddata,
723 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
724 &len[i]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530725 if (!len[i])
726 retval++;
727 }
728 if (retval != 1) {
729 for (i = 0; i < 4 ; i++)
730 pr_debug("buf[%d] size=%d\n", i, len[i]);
731 }
732
733 return retval;
734}
735
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600736static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
737{
738 u32 maud_val;
739
740 /* Select maud according to DP 1.2 spec*/
741 if (link_rate == DP_2_7_GHZ) {
742 switch (aud_samp_freq) {
743 case AUD_SAMPLE_RATE_32:
744 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
745 break;
746
747 case AUD_SAMPLE_RATE_44_1:
748 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
749 break;
750
751 case AUD_SAMPLE_RATE_48:
752 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
753 break;
754
755 case AUD_SAMPLE_RATE_88_2:
756 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
757 break;
758
759 case AUD_SAMPLE_RATE_96:
760 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
761 break;
762
763 case AUD_SAMPLE_RATE_176_4:
764 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
765 break;
766
767 case HAD_MAX_RATE:
768 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
769 break;
770
771 default:
772 maud_val = -EINVAL;
773 break;
774 }
775 } else if (link_rate == DP_1_62_GHZ) {
776 switch (aud_samp_freq) {
777 case AUD_SAMPLE_RATE_32:
778 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
779 break;
780
781 case AUD_SAMPLE_RATE_44_1:
782 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
783 break;
784
785 case AUD_SAMPLE_RATE_48:
786 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
787 break;
788
789 case AUD_SAMPLE_RATE_88_2:
790 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
791 break;
792
793 case AUD_SAMPLE_RATE_96:
794 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
795 break;
796
797 case AUD_SAMPLE_RATE_176_4:
798 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
799 break;
800
801 case HAD_MAX_RATE:
802 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
803 break;
804
805 default:
806 maud_val = -EINVAL;
807 break;
808 }
809 } else
810 maud_val = -EINVAL;
811
812 return maud_val;
813}
814
Takashi Iwai76296ef2017-01-30 16:09:11 +0100815/*
816 * snd_intelhad_prog_cts - Program HDMI audio CTS value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530817 *
818 * @aud_samp_freq: sampling frequency of audio data
819 * @tmds: sampling frequency of the display data
820 * @n_param: N value, depends on aud_samp_freq
821 * @intelhaddata:substream private data
822 *
823 * Program CTS register based on the audio and display sampling frequency
824 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100825static void snd_intelhad_prog_cts(u32 aud_samp_freq, u32 tmds,
826 u32 link_rate, u32 n_param,
827 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530828{
829 u32 cts_val;
830 u64 dividend, divisor;
831
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600832 if (intelhaddata->dp_output) {
833 /* Substitute cts_val with Maud according to DP 1.2 spec*/
834 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
835 } else {
836 /* Calculate CTS according to HDMI 1.3a spec*/
837 dividend = (u64)tmds * n_param*1000;
838 divisor = 128 * aud_samp_freq;
839 cts_val = div64_u64(dividend, divisor);
840 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530841 pr_debug("TMDS value=%d, N value=%d, CTS Value=%d\n",
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600842 tmds, n_param, cts_val);
Takashi Iwai79dda752017-01-30 17:23:39 +0100843 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530844}
845
846static int had_calculate_n_value(u32 aud_samp_freq)
847{
848 s32 n_val;
849
850 /* Select N according to HDMI 1.3a spec*/
851 switch (aud_samp_freq) {
852 case AUD_SAMPLE_RATE_32:
853 n_val = 4096;
854 break;
855
856 case AUD_SAMPLE_RATE_44_1:
857 n_val = 6272;
858 break;
859
860 case AUD_SAMPLE_RATE_48:
861 n_val = 6144;
862 break;
863
864 case AUD_SAMPLE_RATE_88_2:
865 n_val = 12544;
866 break;
867
868 case AUD_SAMPLE_RATE_96:
869 n_val = 12288;
870 break;
871
872 case AUD_SAMPLE_RATE_176_4:
873 n_val = 25088;
874 break;
875
876 case HAD_MAX_RATE:
877 n_val = 24576;
878 break;
879
880 default:
881 n_val = -EINVAL;
882 break;
883 }
884 return n_val;
885}
886
Takashi Iwai76296ef2017-01-30 16:09:11 +0100887/*
888 * snd_intelhad_prog_n - Program HDMI audio N value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530889 *
890 * @aud_samp_freq: sampling frequency of audio data
891 * @n_param: N value, depends on aud_samp_freq
892 * @intelhaddata:substream private data
893 *
894 * This function is called in the prepare callback.
895 * It programs based on the audio and display sampling frequency
896 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100897static int snd_intelhad_prog_n(u32 aud_samp_freq, u32 *n_param,
898 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530899{
900 s32 n_val;
901
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600902 if (intelhaddata->dp_output) {
903 /*
904 * According to DP specs, Maud and Naud values hold
905 * a relationship, which is stated as:
906 * Maud/Naud = 512 * fs / f_LS_Clk
907 * where, fs is the sampling frequency of the audio stream
908 * and Naud is 32768 for Async clock.
909 */
910
911 n_val = DP_NAUD_VAL;
912 } else
913 n_val = had_calculate_n_value(aud_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530914
915 if (n_val < 0)
916 return n_val;
917
Takashi Iwai79dda752017-01-30 17:23:39 +0100918 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530919 *n_param = n_val;
920 return 0;
921}
922
Takashi Iwai76296ef2017-01-30 16:09:11 +0100923void snd_intelhad_handle_underrun(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530924{
925 u32 hdmi_status, i = 0;
926
927 /* Handle Underrun interrupt within Audio Unit */
Takashi Iwai79dda752017-01-30 17:23:39 +0100928 had_write_register(intelhaddata, AUD_CONFIG, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530929 /* Reset buffer pointers */
Takashi Iwai79dda752017-01-30 17:23:39 +0100930 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, 1);
931 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530932 /**
933 * The interrupt status 'sticky' bits might not be cleared by
934 * setting '1' to that bit once...
935 */
936 do { /* clear bit30, 31 AUD_HDMI_STATUS */
Takashi Iwai79dda752017-01-30 17:23:39 +0100937 had_read_register(intelhaddata, AUD_HDMI_STATUS_v2,
938 &hdmi_status);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530939 pr_debug("HDMI status =0x%x\n", hdmi_status);
940 if (hdmi_status & AUD_CONFIG_MASK_UNDERRUN) {
941 i++;
Takashi Iwai79dda752017-01-30 17:23:39 +0100942 had_write_register(intelhaddata,
943 AUD_HDMI_STATUS_v2, hdmi_status);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530944 } else
945 break;
946 } while (i < MAX_CNT);
947 if (i >= MAX_CNT)
948 pr_err("Unable to clear UNDERRUN bits\n");
949}
950
951/**
952 * snd_intelhad_open - stream initializations are done here
953 * @substream:substream for which the stream function is called
954 *
955 * This function is called whenever a PCM stream is opened
956 */
957static int snd_intelhad_open(struct snd_pcm_substream *substream)
958{
959 struct snd_intelhad *intelhaddata;
960 struct snd_pcm_runtime *runtime;
961 struct had_stream_pvt *stream;
962 struct had_pvt_data *had_stream;
963 int retval;
964
965 pr_debug("snd_intelhad_open called\n");
966 intelhaddata = snd_pcm_substream_chip(substream);
967 had_stream = intelhaddata->private_data;
968 runtime = substream->runtime;
Jerome Anand232892f2017-01-25 04:27:53 +0530969 underrun_count = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530970
971 pm_runtime_get(intelhaddata->dev);
972
973 if (had_get_hwstate(intelhaddata)) {
974 pr_err("%s: HDMI cable plugged-out\n", __func__);
975 retval = -ENODEV;
976 goto exit_put_handle;
977 }
978
979 /* Check, if device already in use */
980 if (runtime->private_data) {
981 pr_err("Device already in use\n");
982 retval = -EBUSY;
983 goto exit_put_handle;
984 }
985
986 /* set the runtime hw parameter with local snd_pcm_hardware struct */
987 runtime->hw = snd_intel_hadstream;
988
989 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
990 if (!stream) {
991 retval = -ENOMEM;
992 goto exit_put_handle;
993 }
994 stream->stream_status = STREAM_INIT;
995 runtime->private_data = stream;
996
997 retval = snd_pcm_hw_constraint_integer(runtime,
998 SNDRV_PCM_HW_PARAM_PERIODS);
999 if (retval < 0)
1000 goto exit_err;
1001
1002 /* Make sure, that the period size is always aligned
1003 * 64byte boundary
1004 */
1005 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1006 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
1007 if (retval < 0) {
1008 pr_err("%s:step_size=64 failed,err=%d\n", __func__, retval);
1009 goto exit_err;
1010 }
1011
1012 return retval;
1013exit_err:
1014 kfree(stream);
1015exit_put_handle:
1016 pm_runtime_put(intelhaddata->dev);
1017 runtime->private_data = NULL;
1018 return retval;
1019}
1020
1021/**
1022 * had_period_elapsed - updates the hardware pointer status
1023 * @had_substream:substream for which the stream function is called
1024 *
1025 */
1026static void had_period_elapsed(void *had_substream)
1027{
1028 struct snd_pcm_substream *substream = had_substream;
1029 struct had_stream_pvt *stream;
1030
1031 /* pr_debug("had_period_elapsed called\n"); */
1032
1033 if (!substream || !substream->runtime)
1034 return;
1035 stream = substream->runtime->private_data;
1036 if (!stream)
1037 return;
1038
1039 if (stream->stream_status != STREAM_RUNNING)
1040 return;
1041 snd_pcm_period_elapsed(substream);
1042}
1043
1044/**
1045 * snd_intelhad_init_stream - internal function to initialize stream info
1046 * @substream:substream for which the stream function is called
1047 *
1048 */
1049static int snd_intelhad_init_stream(struct snd_pcm_substream *substream)
1050{
1051 struct snd_intelhad *intelhaddata = snd_pcm_substream_chip(substream);
1052
1053 pr_debug("snd_intelhad_init_stream called\n");
1054
1055 pr_debug("setting buffer ptr param\n");
1056 intelhaddata->stream_info.period_elapsed = had_period_elapsed;
1057 intelhaddata->stream_info.had_substream = substream;
1058 intelhaddata->stream_info.buffer_ptr = 0;
1059 intelhaddata->stream_info.buffer_rendered = 0;
1060 intelhaddata->stream_info.sfreq = substream->runtime->rate;
1061 return 0;
1062}
1063
1064/**
1065 * snd_intelhad_close- to free parameteres when stream is stopped
1066 *
1067 * @substream: substream for which the function is called
1068 *
1069 * This function is called by ALSA framework when stream is stopped
1070 */
1071static int snd_intelhad_close(struct snd_pcm_substream *substream)
1072{
1073 struct snd_intelhad *intelhaddata;
1074 struct snd_pcm_runtime *runtime;
1075
1076 pr_debug("snd_intelhad_close called\n");
1077
1078 intelhaddata = snd_pcm_substream_chip(substream);
1079 runtime = substream->runtime;
1080
1081 if (!runtime->private_data) {
1082 pr_debug("close() might have called after failed open");
1083 return 0;
1084 }
1085
1086 intelhaddata->stream_info.buffer_rendered = 0;
1087 intelhaddata->stream_info.buffer_ptr = 0;
1088 intelhaddata->stream_info.str_id = 0;
1089 intelhaddata->stream_info.had_substream = NULL;
1090
1091 /* Check if following drv_status modification is required - VA */
1092 if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) {
1093 intelhaddata->drv_status = HAD_DRV_CONNECTED;
1094 pr_debug("%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
1095 __func__, __LINE__);
1096 }
1097 kfree(runtime->private_data);
1098 runtime->private_data = NULL;
1099 pm_runtime_put(intelhaddata->dev);
1100 return 0;
1101}
1102
1103/**
1104 * snd_intelhad_hw_params- to setup the hardware parameters
1105 * like allocating the buffers
1106 *
1107 * @substream: substream for which the function is called
1108 * @hw_params: hardware parameters
1109 *
1110 * This function is called by ALSA framework when hardware params are set
1111 */
1112static int snd_intelhad_hw_params(struct snd_pcm_substream *substream,
1113 struct snd_pcm_hw_params *hw_params)
1114{
1115 unsigned long addr;
1116 int pages, buf_size, retval;
1117
1118 pr_debug("snd_intelhad_hw_params called\n");
1119
1120 if (!hw_params)
1121 return -EINVAL;
1122
1123 buf_size = params_buffer_bytes(hw_params);
1124 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1125 if (retval < 0)
1126 return retval;
1127 pr_debug("%s:allocated memory = %d\n", __func__, buf_size);
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) {
1133 pr_err("set_memory_uc failed.Error:%d\n", retval);
1134 return retval;
1135 }
1136 memset(substream->runtime->dma_area, 0, buf_size);
1137
1138 return retval;
1139}
1140
1141/**
1142 * snd_intelhad_hw_free- to release the resources allocated during
1143 * hardware params setup
1144 *
1145 * @substream: substream for which the function is called
1146 *
1147 * This function is called by ALSA framework before close callback.
1148 *
1149 */
1150static int snd_intelhad_hw_free(struct snd_pcm_substream *substream)
1151{
1152 unsigned long addr;
1153 u32 pages;
1154
1155 pr_debug("snd_intelhad_hw_free called\n");
1156
1157 /* mark back the pages as cached/writeback region before the free */
1158 if (substream->runtime->dma_area != NULL) {
1159 addr = (unsigned long) substream->runtime->dma_area;
1160 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1161 PAGE_SIZE;
1162 set_memory_wb(addr, pages);
1163 return snd_pcm_lib_free_pages(substream);
1164 }
1165 return 0;
1166}
1167
1168/**
1169 * snd_intelhad_pcm_trigger - stream activities are handled here
1170 * @substream:substream for which the stream function is called
1171 * @cmd:the stream commamd thats requested from upper layer
1172 * This function is called whenever an a stream activity is invoked
1173 */
1174static int snd_intelhad_pcm_trigger(struct snd_pcm_substream *substream,
1175 int cmd)
1176{
1177 int caps, retval = 0;
1178 unsigned long flag_irq;
1179 struct snd_intelhad *intelhaddata;
1180 struct had_stream_pvt *stream;
1181 struct had_pvt_data *had_stream;
1182
1183 pr_debug("snd_intelhad_pcm_trigger called\n");
1184
1185 intelhaddata = snd_pcm_substream_chip(substream);
1186 stream = substream->runtime->private_data;
1187 had_stream = intelhaddata->private_data;
1188
1189 switch (cmd) {
1190 case SNDRV_PCM_TRIGGER_START:
1191 pr_debug("Trigger Start\n");
1192
1193 /* Disable local INTRs till register prgmng is done */
1194 if (had_get_hwstate(intelhaddata)) {
1195 pr_err("_START: HDMI cable plugged-out\n");
1196 retval = -ENODEV;
1197 break;
1198 }
1199 stream->stream_status = STREAM_RUNNING;
1200
1201 had_stream->stream_type = HAD_RUNNING_STREAM;
1202
1203 /* Enable Audio */
1204 /*
1205 * ToDo: Need to enable UNDERRUN interrupts as well
1206 * caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
1207 */
1208 caps = HDMI_AUDIO_BUFFER_DONE;
Takashi Iwai79dda752017-01-30 17:23:39 +01001209 retval = had_set_caps(intelhaddata, HAD_SET_ENABLE_AUDIO_INT,
1210 &caps);
1211 retval = had_set_caps(intelhaddata, HAD_SET_ENABLE_AUDIO, NULL);
Takashi Iwai76296ef2017-01-30 16:09:11 +01001212 snd_intelhad_enable_audio(substream, 1);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301213
1214 pr_debug("Processed _Start\n");
1215
1216 break;
1217
1218 case SNDRV_PCM_TRIGGER_STOP:
1219 pr_debug("Trigger Stop\n");
1220 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irq);
1221 intelhaddata->stream_info.str_id = 0;
1222 intelhaddata->curr_buf = 0;
1223
1224 /* Stop reporting BUFFER_DONE/UNDERRUN to above layers*/
1225
1226 had_stream->stream_type = HAD_INIT;
1227 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irq);
1228 /* Disable Audio */
1229 /*
1230 * ToDo: Need to disable UNDERRUN interrupts as well
1231 * caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
1232 */
1233 caps = HDMI_AUDIO_BUFFER_DONE;
Takashi Iwai79dda752017-01-30 17:23:39 +01001234 had_set_caps(intelhaddata, HAD_SET_DISABLE_AUDIO_INT, &caps);
Takashi Iwai76296ef2017-01-30 16:09:11 +01001235 snd_intelhad_enable_audio(substream, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301236 /* Reset buffer pointers */
Takashi Iwai79dda752017-01-30 17:23:39 +01001237 snd_intelhad_reset_audio(intelhaddata, 1);
1238 snd_intelhad_reset_audio(intelhaddata, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301239 stream->stream_status = STREAM_DROPPED;
Takashi Iwai79dda752017-01-30 17:23:39 +01001240 had_set_caps(intelhaddata, HAD_SET_DISABLE_AUDIO, NULL);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301241 break;
1242
1243 default:
1244 retval = -EINVAL;
1245 }
1246 return retval;
1247}
1248
1249/**
1250 * snd_intelhad_pcm_prepare- internal preparation before starting a stream
1251 *
1252 * @substream: substream for which the function is called
1253 *
1254 * This function is called when a stream is started for internal preparation.
1255 */
1256static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream)
1257{
1258 int retval;
1259 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001260 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301261 struct snd_intelhad *intelhaddata;
1262 struct snd_pcm_runtime *runtime;
1263 struct had_pvt_data *had_stream;
1264
1265 pr_debug("snd_intelhad_pcm_prepare called\n");
1266
1267 intelhaddata = snd_pcm_substream_chip(substream);
1268 runtime = substream->runtime;
1269 had_stream = intelhaddata->private_data;
1270
1271 if (had_get_hwstate(intelhaddata)) {
1272 pr_err("%s: HDMI cable plugged-out\n", __func__);
1273 retval = -ENODEV;
1274 goto prep_end;
1275 }
1276
1277 pr_debug("period_size=%d\n",
1278 (int)frames_to_bytes(runtime, runtime->period_size));
1279 pr_debug("periods=%d\n", runtime->periods);
1280 pr_debug("buffer_size=%d\n", (int)snd_pcm_lib_buffer_bytes(substream));
1281 pr_debug("rate=%d\n", runtime->rate);
1282 pr_debug("channels=%d\n", runtime->channels);
1283
1284 if (intelhaddata->stream_info.str_id) {
1285 pr_debug("_prepare is called for existing str_id#%d\n",
1286 intelhaddata->stream_info.str_id);
1287 retval = snd_intelhad_pcm_trigger(substream,
1288 SNDRV_PCM_TRIGGER_STOP);
1289 return retval;
1290 }
1291
1292 retval = snd_intelhad_init_stream(substream);
1293 if (retval)
1294 goto prep_end;
1295
1296
1297 /* Get N value in KHz */
Takashi Iwai79dda752017-01-30 17:23:39 +01001298 retval = had_get_caps(intelhaddata, HAD_GET_DISPLAY_RATE,
1299 &disp_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301300 if (retval) {
1301 pr_err("querying display sampling freq failed %#x\n", retval);
1302 goto prep_end;
1303 }
1304
Takashi Iwai79dda752017-01-30 17:23:39 +01001305 had_get_caps(intelhaddata, HAD_GET_ELD, &intelhaddata->eeld);
1306 had_get_caps(intelhaddata, HAD_GET_DP_OUTPUT, &intelhaddata->dp_output);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301307
Takashi Iwai76296ef2017-01-30 16:09:11 +01001308 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1309 intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301310 if (retval) {
1311 pr_err("programming N value failed %#x\n", retval);
1312 goto prep_end;
1313 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001314
1315 if (intelhaddata->dp_output)
Takashi Iwai79dda752017-01-30 17:23:39 +01001316 had_get_caps(intelhaddata, HAD_GET_LINK_RATE, &link_rate);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001317
1318
Takashi Iwai76296ef2017-01-30 16:09:11 +01001319 snd_intelhad_prog_cts(substream->runtime->rate,
1320 disp_samp_freq, link_rate,
1321 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301322
Takashi Iwai76296ef2017-01-30 16:09:11 +01001323 snd_intelhad_prog_dip(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301324
Takashi Iwai76296ef2017-01-30 16:09:11 +01001325 retval = snd_intelhad_audio_ctrl(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301326
1327 /* Prog buffer address */
1328 retval = snd_intelhad_prog_buffer(intelhaddata,
1329 HAD_BUF_TYPE_A, HAD_BUF_TYPE_D);
1330
1331 /*
1332 * Program channel mapping in following order:
1333 * FL, FR, C, LFE, RL, RR
1334 */
1335
Takashi Iwai79dda752017-01-30 17:23:39 +01001336 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301337
1338prep_end:
1339 return retval;
1340}
1341
1342/**
1343 * snd_intelhad_pcm_pointer- to send the current buffer pointerprocessed by hw
1344 *
1345 * @substream: substream for which the function is called
1346 *
1347 * This function is called by ALSA framework to get the current hw buffer ptr
1348 * when a period is elapsed
1349 */
1350static snd_pcm_uframes_t snd_intelhad_pcm_pointer(
1351 struct snd_pcm_substream *substream)
1352{
1353 struct snd_intelhad *intelhaddata;
1354 u32 bytes_rendered = 0;
1355 u32 t;
1356 int buf_id;
1357
1358 /* pr_debug("snd_intelhad_pcm_pointer called\n"); */
1359
1360 intelhaddata = snd_pcm_substream_chip(substream);
1361
1362 if (intelhaddata->flag_underrun) {
1363 intelhaddata->flag_underrun = 0;
1364 return SNDRV_PCM_POS_XRUN;
1365 }
1366
1367 /* Use a hw register to calculate sub-period position reports.
1368 * This makes PulseAudio happier.
1369 */
1370
1371 buf_id = intelhaddata->curr_buf % 4;
Takashi Iwai79dda752017-01-30 17:23:39 +01001372 had_read_register(intelhaddata,
1373 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH), &t);
Jerome Anand232892f2017-01-25 04:27:53 +05301374
1375 if ((t == 0) || (t == ((u32)-1L))) {
1376 underrun_count++;
1377 pr_debug("discovered buffer done for buf %d, count = %d\n",
1378 buf_id, underrun_count);
1379
1380 if (underrun_count > (HAD_MIN_PERIODS/2)) {
1381 pr_debug("assume audio_codec_reset, underrun = %d - do xrun\n",
1382 underrun_count);
1383 underrun_count = 0;
1384 return SNDRV_PCM_POS_XRUN;
1385 }
1386 } else {
1387 /* Reset Counter */
1388 underrun_count = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301389 }
Jerome Anand232892f2017-01-25 04:27:53 +05301390
Jerome Anand5dab11d2017-01-25 04:27:52 +05301391 t = intelhaddata->buf_info[buf_id].buf_size - t;
1392
1393 if (intelhaddata->stream_info.buffer_rendered)
1394 div_u64_rem(intelhaddata->stream_info.buffer_rendered,
1395 intelhaddata->stream_info.ring_buf_size,
1396 &(bytes_rendered));
1397
1398 intelhaddata->stream_info.buffer_ptr = bytes_to_frames(
1399 substream->runtime,
1400 bytes_rendered + t);
1401 return intelhaddata->stream_info.buffer_ptr;
1402}
1403
1404/**
1405 * snd_intelhad_pcm_mmap- mmaps a kernel buffer to user space for copying data
1406 *
1407 * @substream: substream for which the function is called
1408 * @vma: struct instance of memory VMM memory area
1409 *
1410 * This function is called by OS when a user space component
1411 * tries to get mmap memory from driver
1412 */
1413static int snd_intelhad_pcm_mmap(struct snd_pcm_substream *substream,
1414 struct vm_area_struct *vma)
1415{
1416
1417 pr_debug("snd_intelhad_pcm_mmap called\n");
1418
1419 pr_debug("entry with prot:%s\n", __func__);
1420 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1421 return remap_pfn_range(vma, vma->vm_start,
1422 substream->dma_buffer.addr >> PAGE_SHIFT,
1423 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1424}
1425
1426int hdmi_audio_mode_change(struct snd_pcm_substream *substream)
1427{
1428 int retval = 0;
1429 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001430 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301431 struct snd_intelhad *intelhaddata;
1432
1433 intelhaddata = snd_pcm_substream_chip(substream);
1434
1435 /* Disable Audio */
Takashi Iwai76296ef2017-01-30 16:09:11 +01001436 snd_intelhad_enable_audio(substream, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301437
1438 /* Update CTS value */
Takashi Iwai79dda752017-01-30 17:23:39 +01001439 retval = had_get_caps(intelhaddata, HAD_GET_DISPLAY_RATE,
1440 &disp_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301441 if (retval) {
1442 pr_err("querying display sampling freq failed %#x\n", retval);
1443 goto out;
1444 }
1445
Takashi Iwai76296ef2017-01-30 16:09:11 +01001446 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1447 intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301448 if (retval) {
1449 pr_err("programming N value failed %#x\n", retval);
1450 goto out;
1451 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001452
1453 if (intelhaddata->dp_output)
Takashi Iwai79dda752017-01-30 17:23:39 +01001454 had_get_caps(intelhaddata, HAD_GET_LINK_RATE, &link_rate);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001455
Takashi Iwai76296ef2017-01-30 16:09:11 +01001456 snd_intelhad_prog_cts(substream->runtime->rate,
1457 disp_samp_freq, link_rate,
1458 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301459
1460 /* Enable Audio */
Takashi Iwai76296ef2017-01-30 16:09:11 +01001461 snd_intelhad_enable_audio(substream, 1);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301462
1463out:
1464 return retval;
1465}
1466
1467/*PCM operations structure and the calls back for the same */
1468struct snd_pcm_ops snd_intelhad_playback_ops = {
1469 .open = snd_intelhad_open,
1470 .close = snd_intelhad_close,
1471 .ioctl = snd_pcm_lib_ioctl,
1472 .hw_params = snd_intelhad_hw_params,
1473 .hw_free = snd_intelhad_hw_free,
1474 .prepare = snd_intelhad_pcm_prepare,
1475 .trigger = snd_intelhad_pcm_trigger,
1476 .pointer = snd_intelhad_pcm_pointer,
1477 .mmap = snd_intelhad_pcm_mmap,
1478};
1479
1480/**
1481 * snd_intelhad_create - to crete alsa card instance
1482 *
1483 * @intelhaddata: pointer to internal context
1484 * @card: pointer to card
1485 *
1486 * This function is called when the hdmi cable is plugged in
1487 */
1488static int snd_intelhad_create(
1489 struct snd_intelhad *intelhaddata,
1490 struct snd_card *card)
1491{
1492 int retval;
1493 static struct snd_device_ops ops = {
1494 };
1495
1496 pr_debug("snd_intelhad_create called\n");
1497
1498 if (!intelhaddata)
1499 return -EINVAL;
1500
1501 /* ALSA api to register the device */
1502 retval = snd_device_new(card, SNDRV_DEV_LOWLEVEL, intelhaddata, &ops);
1503 return retval;
1504}
1505/**
1506 * snd_intelhad_pcm_free - to free the memory allocated
1507 *
1508 * @pcm: pointer to pcm instance
1509 * This function is called when the device is removed
1510 */
1511static void snd_intelhad_pcm_free(struct snd_pcm *pcm)
1512{
1513 pr_debug("Freeing PCM preallocated pages\n");
1514 snd_pcm_lib_preallocate_free_for_all(pcm);
1515}
1516
1517static int had_iec958_info(struct snd_kcontrol *kcontrol,
1518 struct snd_ctl_elem_info *uinfo)
1519{
1520 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1521 uinfo->count = 1;
1522 return 0;
1523}
1524
1525static int had_iec958_get(struct snd_kcontrol *kcontrol,
1526 struct snd_ctl_elem_value *ucontrol)
1527{
1528 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1529
1530 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1531 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1532 ucontrol->value.iec958.status[2] =
1533 (intelhaddata->aes_bits >> 16) & 0xff;
1534 ucontrol->value.iec958.status[3] =
1535 (intelhaddata->aes_bits >> 24) & 0xff;
1536 return 0;
1537}
1538static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1539 struct snd_ctl_elem_value *ucontrol)
1540{
1541 ucontrol->value.iec958.status[0] = 0xff;
1542 ucontrol->value.iec958.status[1] = 0xff;
1543 ucontrol->value.iec958.status[2] = 0xff;
1544 ucontrol->value.iec958.status[3] = 0xff;
1545 return 0;
1546}
1547static int had_iec958_put(struct snd_kcontrol *kcontrol,
1548 struct snd_ctl_elem_value *ucontrol)
1549{
1550 unsigned int val;
1551 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1552
1553 pr_debug("entered had_iec958_put\n");
1554 val = (ucontrol->value.iec958.status[0] << 0) |
1555 (ucontrol->value.iec958.status[1] << 8) |
1556 (ucontrol->value.iec958.status[2] << 16) |
1557 (ucontrol->value.iec958.status[3] << 24);
1558 if (intelhaddata->aes_bits != val) {
1559 intelhaddata->aes_bits = val;
1560 return 1;
1561 }
1562 return 1;
1563}
1564
1565static struct snd_kcontrol_new had_control_iec958_mask = {
1566 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1567 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1568 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1569 .info = had_iec958_info, /* shared */
1570 .get = had_iec958_mask_get,
1571};
1572
1573static struct snd_kcontrol_new had_control_iec958 = {
1574 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1575 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1576 .info = had_iec958_info,
1577 .get = had_iec958_get,
1578 .put = had_iec958_put
1579};
1580
Takashi Iwai79dda752017-01-30 17:23:39 +01001581/*
Jerome Anand5dab11d2017-01-25 04:27:52 +05301582 * hdmi_audio_probe - to create sound card instance for HDMI audio playabck
1583 *
Takashi Iwai79dda752017-01-30 17:23:39 +01001584 * @devptr: platform device
1585 * @had_ret: pointer to store the created snd_intelhad object
Jerome Anand5dab11d2017-01-25 04:27:52 +05301586 *
Takashi Iwai79dda752017-01-30 17:23:39 +01001587 * This function is called when the platform device is probed. This function
Jerome Anand5dab11d2017-01-25 04:27:52 +05301588 * creates and registers the sound card with ALSA
1589 */
Takashi Iwai79dda752017-01-30 17:23:39 +01001590int hdmi_audio_probe(struct platform_device *devptr,
1591 struct snd_intelhad **had_ret)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301592{
1593 int retval;
1594 struct snd_pcm *pcm;
1595 struct snd_card *card;
1596 struct had_callback_ops ops_cb;
1597 struct snd_intelhad *intelhaddata;
1598 struct had_pvt_data *had_stream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301599
1600 pr_debug("Enter %s\n", __func__);
1601
1602 pr_debug("hdmi_audio_probe dma_mask: %p\n", devptr->dev.dma_mask);
1603
1604 /* allocate memory for saving internal context and working */
1605 intelhaddata = kzalloc(sizeof(*intelhaddata), GFP_KERNEL);
1606 if (!intelhaddata)
1607 return -ENOMEM;
1608
1609 had_stream = kzalloc(sizeof(*had_stream), GFP_KERNEL);
1610 if (!had_stream) {
1611 retval = -ENOMEM;
1612 goto free_haddata;
1613 }
1614
Jerome Anand5dab11d2017-01-25 04:27:52 +05301615 ops_cb.intel_had_event_call_back = had_event_handler;
1616
1617 /* registering with display driver to get access to display APIs */
1618
Takashi Iwai9eca88c2017-01-30 16:37:06 +01001619 retval = mid_hdmi_audio_setup(ops_cb.intel_had_event_call_back);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301620 if (retval) {
1621 pr_err("querying display driver APIs failed %#x\n", retval);
1622 goto free_hadstream;
1623 }
1624 mutex_lock(&had_mutex);
1625 spin_lock_init(&intelhaddata->had_spinlock);
1626 intelhaddata->drv_status = HAD_DRV_DISCONNECTED;
1627 pr_debug("%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
1628 __func__, __LINE__);
1629
1630 /* create a card instance with ALSA framework */
1631 retval = snd_card_new(&devptr->dev, hdmi_card_index, hdmi_card_id,
1632 THIS_MODULE, 0, &card);
1633
1634 if (retval)
1635 goto unlock_mutex;
1636 intelhaddata->card = card;
1637 intelhaddata->card_id = hdmi_card_id;
1638 intelhaddata->card_index = card->number;
1639 intelhaddata->private_data = had_stream;
1640 intelhaddata->flag_underrun = 0;
1641 intelhaddata->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
1642 strncpy(card->driver, INTEL_HAD, strlen(INTEL_HAD));
1643 strncpy(card->shortname, INTEL_HAD, strlen(INTEL_HAD));
1644
1645 retval = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
1646 MAX_CAP_STREAMS, &pcm);
1647 if (retval)
1648 goto err;
1649
1650 /* setup private data which can be retrieved when required */
1651 pcm->private_data = intelhaddata;
1652 pcm->private_free = snd_intelhad_pcm_free;
1653 pcm->info_flags = 0;
1654 strncpy(pcm->name, card->shortname, strlen(card->shortname));
1655 /* setup the ops for palyabck */
1656 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1657 &snd_intelhad_playback_ops);
1658 /* allocate dma pages for ALSA stream operations
1659 * memory allocated is based on size, not max value
1660 * thus using same argument for max & size
1661 */
1662 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
1663 SNDRV_DMA_TYPE_DEV, NULL,
1664 HAD_MAX_BUFFER, HAD_MAX_BUFFER);
1665
1666 if (card->dev == NULL)
1667 pr_debug("card->dev is NULL!!!!! Should not be this case\n");
1668 else if (card->dev->dma_mask == NULL)
1669 pr_debug("hdmi_audio_probe dma_mask is NULL!!!!!\n");
1670 else
1671 pr_debug("hdmi_audio_probe dma_mask is : %p\n",
1672 card->dev->dma_mask);
1673
1674 if (retval)
1675 goto err;
1676
1677 /* internal function call to register device with ALSA */
1678 retval = snd_intelhad_create(intelhaddata, card);
1679 if (retval)
1680 goto err;
1681
1682 card->private_data = &intelhaddata;
1683 retval = snd_card_register(card);
1684 if (retval)
1685 goto err;
1686
1687 /* IEC958 controls */
1688 retval = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958_mask,
1689 intelhaddata));
1690 if (retval < 0)
1691 goto err;
1692 retval = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958,
1693 intelhaddata));
1694 if (retval < 0)
1695 goto err;
1696
1697 init_channel_allocations();
1698
1699 /* Register channel map controls */
1700 retval = had_register_chmap_ctls(intelhaddata, pcm);
1701 if (retval < 0)
1702 goto err;
1703
1704 intelhaddata->dev = &devptr->dev;
1705 pm_runtime_set_active(intelhaddata->dev);
1706 pm_runtime_enable(intelhaddata->dev);
1707
1708 mutex_unlock(&had_mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301709
1710 intelhaddata->hw_silence = 1;
Takashi Iwai79dda752017-01-30 17:23:39 +01001711 *had_ret = intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301712
Takashi Iwai79dda752017-01-30 17:23:39 +01001713 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301714err:
1715 snd_card_free(card);
1716unlock_mutex:
1717 mutex_unlock(&had_mutex);
1718free_hadstream:
1719 kfree(had_stream);
1720 pm_runtime_disable(intelhaddata->dev);
1721 intelhaddata->dev = NULL;
1722free_haddata:
1723 kfree(intelhaddata);
1724 intelhaddata = NULL;
1725 pr_err("Error returned from %s api %#x\n", __func__, retval);
1726 return retval;
1727}
1728
Takashi Iwai79dda752017-01-30 17:23:39 +01001729/*
Jerome Anand5dab11d2017-01-25 04:27:52 +05301730 * hdmi_audio_remove - removes the alsa card
1731 *
1732 *@haddata: pointer to HAD private data
1733 *
1734 * This function is called when the hdmi cable is un-plugged. This function
1735 * free the sound card.
1736 */
Takashi Iwai79dda752017-01-30 17:23:39 +01001737int hdmi_audio_remove(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301738{
Jerome Anand5dab11d2017-01-25 04:27:52 +05301739 int caps;
1740
1741 pr_debug("Enter %s\n", __func__);
1742
1743 if (!intelhaddata)
1744 return 0;
1745
1746 if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) {
1747 caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
Takashi Iwai79dda752017-01-30 17:23:39 +01001748 had_set_caps(intelhaddata, HAD_SET_DISABLE_AUDIO_INT, &caps);
1749 had_set_caps(intelhaddata, HAD_SET_DISABLE_AUDIO, NULL);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301750 }
1751 snd_card_free(intelhaddata->card);
1752 kfree(intelhaddata->private_data);
1753 kfree(intelhaddata);
1754 return 0;
1755}
1756
1757MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1758MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1759MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1760MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1761MODULE_DESCRIPTION("Intel HDMI Audio driver");
1762MODULE_LICENSE("GPL v2");
1763MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");