blob: 7165f14d522998bed0365eb71dae3abcdda14ba2 [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
Jerome Anand5dab11d2017-01-25 04:27:52 +053040/*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;
Jerome Anand232892f2017-01-25 04:27:53 +053043static int underrun_count;
Jerome Anand5dab11d2017-01-25 04:27:52 +053044
45module_param_named(index, hdmi_card_index, int, 0444);
46MODULE_PARM_DESC(index,
47 "Index value for INTEL Intel HDMI Audio controller.");
48module_param_named(id, hdmi_card_id, charp, 0444);
49MODULE_PARM_DESC(id,
50 "ID string for INTEL Intel HDMI Audio controller.");
51
52/*
53 * ELD SA bits in the CEA Speaker Allocation data block
54 */
55static int eld_speaker_allocation_bits[] = {
56 [0] = FL | FR,
57 [1] = LFE,
58 [2] = FC,
59 [3] = RL | RR,
60 [4] = RC,
61 [5] = FLC | FRC,
62 [6] = RLC | RRC,
63 /* the following are not defined in ELD yet */
64 [7] = 0,
65};
66
67/*
68 * This is an ordered list!
69 *
70 * The preceding ones have better chances to be selected by
71 * hdmi_channel_allocation().
72 */
73static struct cea_channel_speaker_allocation channel_allocations[] = {
74/* channel: 7 6 5 4 3 2 1 0 */
75{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
76 /* 2.1 */
77{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
78 /* Dolby Surround */
79{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
80 /* surround40 */
81{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
82 /* surround41 */
83{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
84 /* surround50 */
85{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
86 /* surround51 */
87{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
88 /* 6.1 */
89{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
90 /* surround71 */
91{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
92
93{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
94{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
95{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
96{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
97{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
98{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
99{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
100{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
101{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
102{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
103{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
104{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
105{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
106{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
107{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
108{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
109{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
110{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
111{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
112{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
113{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
114{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
115{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
116};
117
118static struct channel_map_table map_tables[] = {
119 { SNDRV_CHMAP_FL, 0x00, FL },
120 { SNDRV_CHMAP_FR, 0x01, FR },
121 { SNDRV_CHMAP_RL, 0x04, RL },
122 { SNDRV_CHMAP_RR, 0x05, RR },
123 { SNDRV_CHMAP_LFE, 0x02, LFE },
124 { SNDRV_CHMAP_FC, 0x03, FC },
125 { SNDRV_CHMAP_RLC, 0x06, RLC },
126 { SNDRV_CHMAP_RRC, 0x07, RRC },
127 {} /* terminator */
128};
129
130/* hardware capability structure */
131static const struct snd_pcm_hardware snd_intel_hadstream = {
132 .info = (SNDRV_PCM_INFO_INTERLEAVED |
133 SNDRV_PCM_INFO_DOUBLE |
134 SNDRV_PCM_INFO_MMAP|
135 SNDRV_PCM_INFO_MMAP_VALID |
136 SNDRV_PCM_INFO_BATCH),
137 .formats = (SNDRV_PCM_FMTBIT_S24 |
138 SNDRV_PCM_FMTBIT_U24),
139 .rates = SNDRV_PCM_RATE_32000 |
140 SNDRV_PCM_RATE_44100 |
141 SNDRV_PCM_RATE_48000 |
142 SNDRV_PCM_RATE_88200 |
143 SNDRV_PCM_RATE_96000 |
144 SNDRV_PCM_RATE_176400 |
145 SNDRV_PCM_RATE_192000,
146 .rate_min = HAD_MIN_RATE,
147 .rate_max = HAD_MAX_RATE,
148 .channels_min = HAD_MIN_CHANNEL,
149 .channels_max = HAD_MAX_CHANNEL,
150 .buffer_bytes_max = HAD_MAX_BUFFER,
151 .period_bytes_min = HAD_MIN_PERIOD_BYTES,
152 .period_bytes_max = HAD_MAX_PERIOD_BYTES,
153 .periods_min = HAD_MIN_PERIODS,
154 .periods_max = HAD_MAX_PERIODS,
155 .fifo_size = HAD_FIFO_SIZE,
156};
157
158/* Register access functions */
159
160int had_get_hwstate(struct snd_intelhad *intelhaddata)
161{
162 /* Check for device presence -SW state */
163 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
164 pr_debug("%s:Device not connected:%d\n", __func__,
165 intelhaddata->drv_status);
166 return -ENODEV;
167 }
168
169 return 0;
170}
171
Takashi Iwai79dda752017-01-30 17:23:39 +0100172int had_get_caps(struct snd_intelhad *intelhaddata,
173 enum had_caps_list query, void *caps)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530174{
175 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530176
177 retval = had_get_hwstate(intelhaddata);
178 if (!retval)
Takashi Iwai9eca88c2017-01-30 16:37:06 +0100179 retval = mid_hdmi_audio_get_caps(query, caps);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530180
181 return retval;
182}
183
Takashi Iwai79dda752017-01-30 17:23:39 +0100184int had_set_caps(struct snd_intelhad *intelhaddata,
185 enum had_caps_list set_element, void *caps)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530186{
187 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530188
189 retval = had_get_hwstate(intelhaddata);
190 if (!retval)
Takashi Iwai9eca88c2017-01-30 16:37:06 +0100191 retval = mid_hdmi_audio_set_caps(set_element, caps);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530192
193 return retval;
194}
195
Takashi Iwai79dda752017-01-30 17:23:39 +0100196int had_read_register(struct snd_intelhad *intelhaddata, u32 offset, u32 *data)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530197{
198 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530199
200 retval = had_get_hwstate(intelhaddata);
201 if (!retval)
Takashi Iwaif23df802017-01-30 16:29:39 +0100202 retval = mid_hdmi_audio_read(offset, data);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530203
204 return retval;
205}
206
Takashi Iwai79dda752017-01-30 17:23:39 +0100207int had_write_register(struct snd_intelhad *intelhaddata, u32 offset, u32 data)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530208{
209 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530210
211 retval = had_get_hwstate(intelhaddata);
212 if (!retval)
Takashi Iwaif23df802017-01-30 16:29:39 +0100213 retval = mid_hdmi_audio_write(offset, data);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530214
215 return retval;
216}
217
Takashi Iwai79dda752017-01-30 17:23:39 +0100218int had_read_modify(struct snd_intelhad *intelhaddata, u32 offset,
219 u32 data, u32 mask)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530220{
221 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530222
223 retval = had_get_hwstate(intelhaddata);
224 if (!retval)
Takashi Iwaif23df802017-01-30 16:29:39 +0100225 retval = mid_hdmi_audio_rmw(offset, data, mask);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530226
227 return retval;
228}
229/**
230 * function to read-modify
231 * AUD_CONFIG register on VLV2.The had_read_modify() function should not
232 * directly be used on VLV2 for updating AUD_CONFIG register.
233 * This is because:
234 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
235 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
236 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
237 * register. This field should be 1xy binary for configuration with 6 or
238 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
239 * causes the "channels" field to be updated as 0xy binary resulting in
240 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
241 * appropriate value when doing read-modify of AUD_CONFIG register.
242 *
243 * @substream: the current substream or NULL if no active substream
244 * @data : data to be written
245 * @mask : mask
246 *
247 */
248static int had_read_modify_aud_config_v2(struct snd_pcm_substream *substream,
249 u32 data, u32 mask)
250{
Takashi Iwai79dda752017-01-30 17:23:39 +0100251 struct snd_intelhad *intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530252 union aud_cfg cfg_val = {.cfg_regval = 0};
253 u8 channels;
254
255 /*
256 * If substream is NULL, there is no active stream.
257 * In this case just set channels to 2
258 */
259 if (substream)
260 channels = substream->runtime->channels;
261 else
262 channels = 2;
263 cfg_val.cfg_regx_v2.num_ch = channels - 2;
264
265 data = data | cfg_val.cfg_regval;
266 mask = mask | AUD_CONFIG_CH_MASK_V2;
267
268 pr_debug("%s : data = %x, mask =%x\n", __func__, data, mask);
269
Takashi Iwai79dda752017-01-30 17:23:39 +0100270 return had_read_modify(intelhaddata, AUD_CONFIG, data, mask);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530271}
272
Takashi Iwai76296ef2017-01-30 16:09:11 +0100273void snd_intelhad_enable_audio(struct snd_pcm_substream *substream, u8 enable)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530274{
275 had_read_modify_aud_config_v2(substream, enable, BIT(0));
276}
277
Takashi Iwai79dda752017-01-30 17:23:39 +0100278static void snd_intelhad_reset_audio(struct snd_intelhad *intelhaddata,
279 u8 reset)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530280{
Takashi Iwai79dda752017-01-30 17:23:39 +0100281 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, reset);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530282}
283
284/**
285 * initialize audio channel status registers
286 * This function is called in the prepare callback
287 */
288static int had_prog_status_reg(struct snd_pcm_substream *substream,
289 struct snd_intelhad *intelhaddata)
290{
291 union aud_cfg cfg_val = {.cfg_regval = 0};
292 union aud_ch_status_0 ch_stat0 = {.status_0_regval = 0};
293 union aud_ch_status_1 ch_stat1 = {.status_1_regval = 0};
294 int format;
295
296 pr_debug("Entry %s\n", __func__);
297
298 ch_stat0.status_0_regx.lpcm_id = (intelhaddata->aes_bits &
299 IEC958_AES0_NONAUDIO)>>1;
300 ch_stat0.status_0_regx.clk_acc = (intelhaddata->aes_bits &
301 IEC958_AES3_CON_CLOCK)>>4;
Takashi Iwai4812dcc2017-01-30 15:58:15 +0100302 cfg_val.cfg_regx_v2.val_bit = ch_stat0.status_0_regx.lpcm_id;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530303
304 switch (substream->runtime->rate) {
305 case AUD_SAMPLE_RATE_32:
306 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_32KHZ;
307 break;
308
309 case AUD_SAMPLE_RATE_44_1:
310 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_44KHZ;
311 break;
312 case AUD_SAMPLE_RATE_48:
313 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_48KHZ;
314 break;
315 case AUD_SAMPLE_RATE_88_2:
316 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_88KHZ;
317 break;
318 case AUD_SAMPLE_RATE_96:
319 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_96KHZ;
320 break;
321 case AUD_SAMPLE_RATE_176_4:
322 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_176KHZ;
323 break;
324 case AUD_SAMPLE_RATE_192:
325 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_192KHZ;
326 break;
327
328 default:
329 /* control should never come here */
330 return -EINVAL;
331 break;
332
333 }
Takashi Iwai79dda752017-01-30 17:23:39 +0100334 had_write_register(intelhaddata,
335 AUD_CH_STATUS_0, ch_stat0.status_0_regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530336
337 format = substream->runtime->format;
338
339 if (format == SNDRV_PCM_FORMAT_S16_LE) {
340 ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_20;
341 ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_16BITS;
342 } else if (format == SNDRV_PCM_FORMAT_S24_LE) {
343 ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_24;
344 ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_24BITS;
345 } else {
346 ch_stat1.status_1_regx.max_wrd_len = 0;
347 ch_stat1.status_1_regx.wrd_len = 0;
348 }
Takashi Iwai79dda752017-01-30 17:23:39 +0100349 had_write_register(intelhaddata,
350 AUD_CH_STATUS_1, ch_stat1.status_1_regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530351 return 0;
352}
353
Takashi Iwai76296ef2017-01-30 16:09:11 +0100354/*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530355 * function to initialize audio
356 * registers and buffer confgiuration registers
357 * This function is called in the prepare callback
358 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100359static int snd_intelhad_audio_ctrl(struct snd_pcm_substream *substream,
360 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530361{
362 union aud_cfg cfg_val = {.cfg_regval = 0};
363 union aud_buf_config buf_cfg = {.buf_cfgval = 0};
364 u8 channels;
365
366 had_prog_status_reg(substream, intelhaddata);
367
368 buf_cfg.buf_cfg_regx_v2.audio_fifo_watermark = FIFO_THRESHOLD;
369 buf_cfg.buf_cfg_regx_v2.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
370 buf_cfg.buf_cfg_regx_v2.aud_delay = 0;
Takashi Iwai79dda752017-01-30 17:23:39 +0100371 had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.buf_cfgval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530372
373 channels = substream->runtime->channels;
374 cfg_val.cfg_regx_v2.num_ch = channels - 2;
375 if (channels <= 2)
376 cfg_val.cfg_regx_v2.layout = LAYOUT0;
377 else
378 cfg_val.cfg_regx_v2.layout = LAYOUT1;
379
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600380 cfg_val.cfg_regx_v2.val_bit = 1;
Takashi Iwai79dda752017-01-30 17:23:39 +0100381 had_write_register(intelhaddata, AUD_CONFIG, cfg_val.cfg_regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530382 return 0;
383}
384
Jerome Anand5dab11d2017-01-25 04:27:52 +0530385/*
386 * Compute derived values in channel_allocations[].
387 */
388static void init_channel_allocations(void)
389{
390 int i, j;
391 struct cea_channel_speaker_allocation *p;
392
393 pr_debug("%s: Enter\n", __func__);
394
395 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
396 p = channel_allocations + i;
397 p->channels = 0;
398 p->spk_mask = 0;
399 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
400 if (p->speakers[j]) {
401 p->channels++;
402 p->spk_mask |= p->speakers[j];
403 }
404 }
405}
406
407/*
408 * The transformation takes two steps:
409 *
410 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
411 * spk_mask => (channel_allocations[]) => ai->CA
412 *
413 * TODO: it could select the wrong CA from multiple candidates.
414 */
415static int snd_intelhad_channel_allocation(struct snd_intelhad *intelhaddata,
416 int channels)
417{
418 int i;
419 int ca = 0;
420 int spk_mask = 0;
421
422 /*
423 * CA defaults to 0 for basic stereo audio
424 */
425 if (channels <= 2)
426 return 0;
427
428 /*
429 * expand ELD's speaker allocation mask
430 *
431 * ELD tells the speaker mask in a compact(paired) form,
432 * expand ELD's notions to match the ones used by Audio InfoFrame.
433 */
434
435 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
436 if (intelhaddata->eeld.speaker_allocation_block & (1 << i))
437 spk_mask |= eld_speaker_allocation_bits[i];
438 }
439
440 /* search for the first working match in the CA table */
441 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
442 if (channels == channel_allocations[i].channels &&
443 (spk_mask & channel_allocations[i].spk_mask) ==
444 channel_allocations[i].spk_mask) {
445 ca = channel_allocations[i].ca_index;
446 break;
447 }
448 }
449
450 pr_debug("HDMI: select CA 0x%x for %d\n", ca, channels);
451
452 return ca;
453}
454
455/* from speaker bit mask to ALSA API channel position */
456static int spk_to_chmap(int spk)
457{
458 struct channel_map_table *t = map_tables;
459
460 for (; t->map; t++) {
461 if (t->spk_mask == spk)
462 return t->map;
463 }
464 return 0;
465}
466
467void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
468{
469 int i = 0, c = 0;
470 int spk_mask = 0;
471 struct snd_pcm_chmap_elem *chmap;
472 u8 eld_high, eld_high_mask = 0xF0;
473 u8 high_msb;
474
475 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
476 if (chmap == NULL) {
477 intelhaddata->chmap->chmap = NULL;
478 return;
479 }
480
Takashi Iwai79dda752017-01-30 17:23:39 +0100481 had_get_caps(intelhaddata, HAD_GET_ELD, &intelhaddata->eeld);
482 had_get_caps(intelhaddata, HAD_GET_DP_OUTPUT, &intelhaddata->dp_output);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530483
484 pr_debug("eeld.speaker_allocation_block = %x\n",
485 intelhaddata->eeld.speaker_allocation_block);
486
487 /* WA: Fix the max channel supported to 8 */
488
489 /*
490 * Sink may support more than 8 channels, if eld_high has more than
491 * one bit set. SOC supports max 8 channels.
492 * Refer eld_speaker_allocation_bits, for sink speaker allocation
493 */
494
495 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
496 eld_high = intelhaddata->eeld.speaker_allocation_block & eld_high_mask;
497 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
498 /* eld_high & (eld_high-1): if more than 1 bit set */
499 /* 0x1F: 7 channels */
500 for (i = 1; i < 4; i++) {
501 high_msb = eld_high & (0x80 >> i);
502 if (high_msb) {
503 intelhaddata->eeld.speaker_allocation_block &=
504 high_msb | 0xF;
505 break;
506 }
507 }
508 }
509
510 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
511 if (intelhaddata->eeld.speaker_allocation_block & (1 << i))
512 spk_mask |= eld_speaker_allocation_bits[i];
513 }
514
515 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
516 if (spk_mask == channel_allocations[i].spk_mask) {
517 for (c = 0; c < channel_allocations[i].channels; c++) {
518 chmap->map[c] = spk_to_chmap(
519 channel_allocations[i].speakers[
520 (MAX_SPEAKERS - 1)-c]);
521 }
522 chmap->channels = channel_allocations[i].channels;
523 intelhaddata->chmap->chmap = chmap;
524 break;
525 }
526 }
527 if (i >= ARRAY_SIZE(channel_allocations)) {
528 intelhaddata->chmap->chmap = NULL;
529 kfree(chmap);
530 }
531}
532
533/*
534 * ALSA API channel-map control callbacks
535 */
536static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
537 struct snd_ctl_elem_info *uinfo)
538{
539 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
540 struct snd_intelhad *intelhaddata = info->private_data;
541
542 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
543 return -ENODEV;
544 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
545 uinfo->count = HAD_MAX_CHANNEL;
546 uinfo->value.integer.min = 0;
547 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
548 return 0;
549}
550
551static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
552 struct snd_ctl_elem_value *ucontrol)
553{
554 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
555 struct snd_intelhad *intelhaddata = info->private_data;
556 int i = 0;
557 const struct snd_pcm_chmap_elem *chmap;
558
559 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
560 return -ENODEV;
561 if (intelhaddata->chmap->chmap == NULL)
562 return -ENODATA;
563 chmap = intelhaddata->chmap->chmap;
564 for (i = 0; i < chmap->channels; i++) {
565 ucontrol->value.integer.value[i] = chmap->map[i];
566 pr_debug("chmap->map[%d] = %d\n", i, chmap->map[i]);
567 }
568
569 return 0;
570}
571
572static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
573 struct snd_pcm *pcm)
574{
575 int err = 0;
576
577 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
578 NULL, 0, (unsigned long)intelhaddata,
579 &intelhaddata->chmap);
580 if (err < 0)
581 return err;
582
583 intelhaddata->chmap->private_data = intelhaddata;
584 intelhaddata->kctl = intelhaddata->chmap->kctl;
585 intelhaddata->kctl->info = had_chmap_ctl_info;
586 intelhaddata->kctl->get = had_chmap_ctl_get;
587 intelhaddata->chmap->chmap = NULL;
588 return 0;
589}
590
Takashi Iwai76296ef2017-01-30 16:09:11 +0100591/*
592 * snd_intelhad_prog_dip - to initialize Data Island Packets registers
Jerome Anand5dab11d2017-01-25 04:27:52 +0530593 *
594 * @substream:substream for which the prepare function is called
595 * @intelhaddata:substream private data
596 *
597 * This function is called in the prepare callback
598 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100599static void snd_intelhad_prog_dip(struct snd_pcm_substream *substream,
600 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530601{
602 int i;
603 union aud_ctrl_st ctrl_state = {.ctrl_val = 0};
604 union aud_info_frame2 frame2 = {.fr2_val = 0};
605 union aud_info_frame3 frame3 = {.fr3_val = 0};
606 u8 checksum = 0;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600607 u32 info_frame;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530608 int channels;
609
610 channels = substream->runtime->channels;
611
Takashi Iwai79dda752017-01-30 17:23:39 +0100612 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.ctrl_val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530613
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600614 if (intelhaddata->dp_output) {
615 info_frame = DP_INFO_FRAME_WORD1;
616 frame2.fr2_val = 1;
617 } else {
618 info_frame = HDMI_INFO_FRAME_WORD1;
619 frame2.fr2_regx.chnl_cnt = substream->runtime->channels - 1;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530620
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600621 frame3.fr3_regx.chnl_alloc = snd_intelhad_channel_allocation(
622 intelhaddata, channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530623
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600624 /*Calculte the byte wide checksum for all valid DIP words*/
625 for (i = 0; i < BYTES_PER_WORD; i++)
626 checksum += (info_frame >> i*BITS_PER_BYTE) & MASK_BYTE0;
627 for (i = 0; i < BYTES_PER_WORD; i++)
628 checksum += (frame2.fr2_val >> i*BITS_PER_BYTE) & MASK_BYTE0;
629 for (i = 0; i < BYTES_PER_WORD; i++)
630 checksum += (frame3.fr3_val >> i*BITS_PER_BYTE) & MASK_BYTE0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530631
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600632 frame2.fr2_regx.chksum = -(checksum);
633 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530634
Takashi Iwai79dda752017-01-30 17:23:39 +0100635 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, info_frame);
636 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, frame2.fr2_val);
637 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, frame3.fr3_val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530638
639 /* program remaining DIP words with zero */
640 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
Takashi Iwai79dda752017-01-30 17:23:39 +0100641 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, 0x0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530642
643 ctrl_state.ctrl_regx.dip_freq = 1;
644 ctrl_state.ctrl_regx.dip_en_sta = 1;
Takashi Iwai79dda752017-01-30 17:23:39 +0100645 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.ctrl_val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530646}
647
648/**
649 * snd_intelhad_prog_buffer - programs buffer
650 * address and length registers
651 *
652 * @substream:substream for which the prepare function is called
653 * @intelhaddata:substream private data
654 *
655 * This function programs ring buffer address and length into registers.
656 */
657int snd_intelhad_prog_buffer(struct snd_intelhad *intelhaddata,
658 int start, int end)
659{
660 u32 ring_buf_addr, ring_buf_size, period_bytes;
661 u8 i, num_periods;
662 struct snd_pcm_substream *substream;
663
664 substream = intelhaddata->stream_info.had_substream;
665 if (!substream) {
666 pr_err("substream is NULL\n");
667 dump_stack();
668 return 0;
669 }
670
671 ring_buf_addr = substream->runtime->dma_addr;
672 ring_buf_size = snd_pcm_lib_buffer_bytes(substream);
673 intelhaddata->stream_info.ring_buf_size = ring_buf_size;
674 period_bytes = frames_to_bytes(substream->runtime,
675 substream->runtime->period_size);
676 num_periods = substream->runtime->periods;
677
678 /*
679 * buffer addr should be 64 byte aligned, period bytes
680 * will be used to calculate addr offset
681 */
682 period_bytes &= ~0x3F;
683
684 /* Hardware supports MAX_PERIODS buffers */
685 if (end >= HAD_MAX_PERIODS)
686 return -EINVAL;
687
688 for (i = start; i <= end; i++) {
689 /* Program the buf registers with addr and len */
690 intelhaddata->buf_info[i].buf_addr = ring_buf_addr +
691 (i * period_bytes);
692 if (i < num_periods-1)
693 intelhaddata->buf_info[i].buf_size = period_bytes;
694 else
695 intelhaddata->buf_info[i].buf_size = ring_buf_size -
696 (period_bytes*i);
697
Takashi Iwai79dda752017-01-30 17:23:39 +0100698 had_write_register(intelhaddata,
699 AUD_BUF_A_ADDR + (i * HAD_REG_WIDTH),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530700 intelhaddata->buf_info[i].buf_addr |
701 BIT(0) | BIT(1));
Takashi Iwai79dda752017-01-30 17:23:39 +0100702 had_write_register(intelhaddata,
703 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530704 period_bytes);
705 intelhaddata->buf_info[i].is_valid = true;
706 }
707 pr_debug("%s:buf[%d-%d] addr=%#x and size=%d\n", __func__, start, end,
708 intelhaddata->buf_info[start].buf_addr,
709 intelhaddata->buf_info[start].buf_size);
710 intelhaddata->valid_buf_cnt = num_periods;
711 return 0;
712}
713
714int snd_intelhad_read_len(struct snd_intelhad *intelhaddata)
715{
716 int i, retval = 0;
717 u32 len[4];
718
719 for (i = 0; i < 4 ; i++) {
Takashi Iwai79dda752017-01-30 17:23:39 +0100720 had_read_register(intelhaddata,
721 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
722 &len[i]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530723 if (!len[i])
724 retval++;
725 }
726 if (retval != 1) {
727 for (i = 0; i < 4 ; i++)
728 pr_debug("buf[%d] size=%d\n", i, len[i]);
729 }
730
731 return retval;
732}
733
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600734static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
735{
736 u32 maud_val;
737
738 /* Select maud according to DP 1.2 spec*/
739 if (link_rate == DP_2_7_GHZ) {
740 switch (aud_samp_freq) {
741 case AUD_SAMPLE_RATE_32:
742 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
743 break;
744
745 case AUD_SAMPLE_RATE_44_1:
746 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
747 break;
748
749 case AUD_SAMPLE_RATE_48:
750 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
751 break;
752
753 case AUD_SAMPLE_RATE_88_2:
754 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
755 break;
756
757 case AUD_SAMPLE_RATE_96:
758 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
759 break;
760
761 case AUD_SAMPLE_RATE_176_4:
762 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
763 break;
764
765 case HAD_MAX_RATE:
766 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
767 break;
768
769 default:
770 maud_val = -EINVAL;
771 break;
772 }
773 } else if (link_rate == DP_1_62_GHZ) {
774 switch (aud_samp_freq) {
775 case AUD_SAMPLE_RATE_32:
776 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
777 break;
778
779 case AUD_SAMPLE_RATE_44_1:
780 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
781 break;
782
783 case AUD_SAMPLE_RATE_48:
784 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
785 break;
786
787 case AUD_SAMPLE_RATE_88_2:
788 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
789 break;
790
791 case AUD_SAMPLE_RATE_96:
792 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
793 break;
794
795 case AUD_SAMPLE_RATE_176_4:
796 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
797 break;
798
799 case HAD_MAX_RATE:
800 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
801 break;
802
803 default:
804 maud_val = -EINVAL;
805 break;
806 }
807 } else
808 maud_val = -EINVAL;
809
810 return maud_val;
811}
812
Takashi Iwai76296ef2017-01-30 16:09:11 +0100813/*
814 * snd_intelhad_prog_cts - Program HDMI audio CTS value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530815 *
816 * @aud_samp_freq: sampling frequency of audio data
817 * @tmds: sampling frequency of the display data
818 * @n_param: N value, depends on aud_samp_freq
819 * @intelhaddata:substream private data
820 *
821 * Program CTS register based on the audio and display sampling frequency
822 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100823static void snd_intelhad_prog_cts(u32 aud_samp_freq, u32 tmds,
824 u32 link_rate, u32 n_param,
825 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530826{
827 u32 cts_val;
828 u64 dividend, divisor;
829
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600830 if (intelhaddata->dp_output) {
831 /* Substitute cts_val with Maud according to DP 1.2 spec*/
832 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
833 } else {
834 /* Calculate CTS according to HDMI 1.3a spec*/
835 dividend = (u64)tmds * n_param*1000;
836 divisor = 128 * aud_samp_freq;
837 cts_val = div64_u64(dividend, divisor);
838 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530839 pr_debug("TMDS value=%d, N value=%d, CTS Value=%d\n",
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600840 tmds, n_param, cts_val);
Takashi Iwai79dda752017-01-30 17:23:39 +0100841 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530842}
843
844static int had_calculate_n_value(u32 aud_samp_freq)
845{
846 s32 n_val;
847
848 /* Select N according to HDMI 1.3a spec*/
849 switch (aud_samp_freq) {
850 case AUD_SAMPLE_RATE_32:
851 n_val = 4096;
852 break;
853
854 case AUD_SAMPLE_RATE_44_1:
855 n_val = 6272;
856 break;
857
858 case AUD_SAMPLE_RATE_48:
859 n_val = 6144;
860 break;
861
862 case AUD_SAMPLE_RATE_88_2:
863 n_val = 12544;
864 break;
865
866 case AUD_SAMPLE_RATE_96:
867 n_val = 12288;
868 break;
869
870 case AUD_SAMPLE_RATE_176_4:
871 n_val = 25088;
872 break;
873
874 case HAD_MAX_RATE:
875 n_val = 24576;
876 break;
877
878 default:
879 n_val = -EINVAL;
880 break;
881 }
882 return n_val;
883}
884
Takashi Iwai76296ef2017-01-30 16:09:11 +0100885/*
886 * snd_intelhad_prog_n - Program HDMI audio N value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530887 *
888 * @aud_samp_freq: sampling frequency of audio data
889 * @n_param: N value, depends on aud_samp_freq
890 * @intelhaddata:substream private data
891 *
892 * This function is called in the prepare callback.
893 * It programs based on the audio and display sampling frequency
894 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100895static int snd_intelhad_prog_n(u32 aud_samp_freq, u32 *n_param,
896 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530897{
898 s32 n_val;
899
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600900 if (intelhaddata->dp_output) {
901 /*
902 * According to DP specs, Maud and Naud values hold
903 * a relationship, which is stated as:
904 * Maud/Naud = 512 * fs / f_LS_Clk
905 * where, fs is the sampling frequency of the audio stream
906 * and Naud is 32768 for Async clock.
907 */
908
909 n_val = DP_NAUD_VAL;
910 } else
911 n_val = had_calculate_n_value(aud_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530912
913 if (n_val < 0)
914 return n_val;
915
Takashi Iwai79dda752017-01-30 17:23:39 +0100916 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530917 *n_param = n_val;
918 return 0;
919}
920
Takashi Iwai76296ef2017-01-30 16:09:11 +0100921void snd_intelhad_handle_underrun(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530922{
923 u32 hdmi_status, i = 0;
924
925 /* Handle Underrun interrupt within Audio Unit */
Takashi Iwai79dda752017-01-30 17:23:39 +0100926 had_write_register(intelhaddata, AUD_CONFIG, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530927 /* Reset buffer pointers */
Takashi Iwai79dda752017-01-30 17:23:39 +0100928 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, 1);
929 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530930 /**
931 * The interrupt status 'sticky' bits might not be cleared by
932 * setting '1' to that bit once...
933 */
934 do { /* clear bit30, 31 AUD_HDMI_STATUS */
Takashi Iwai79dda752017-01-30 17:23:39 +0100935 had_read_register(intelhaddata, AUD_HDMI_STATUS_v2,
936 &hdmi_status);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530937 pr_debug("HDMI status =0x%x\n", hdmi_status);
938 if (hdmi_status & AUD_CONFIG_MASK_UNDERRUN) {
939 i++;
Takashi Iwai79dda752017-01-30 17:23:39 +0100940 had_write_register(intelhaddata,
941 AUD_HDMI_STATUS_v2, hdmi_status);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530942 } else
943 break;
944 } while (i < MAX_CNT);
945 if (i >= MAX_CNT)
946 pr_err("Unable to clear UNDERRUN bits\n");
947}
948
949/**
950 * snd_intelhad_open - stream initializations are done here
951 * @substream:substream for which the stream function is called
952 *
953 * This function is called whenever a PCM stream is opened
954 */
955static int snd_intelhad_open(struct snd_pcm_substream *substream)
956{
957 struct snd_intelhad *intelhaddata;
958 struct snd_pcm_runtime *runtime;
959 struct had_stream_pvt *stream;
960 struct had_pvt_data *had_stream;
961 int retval;
962
963 pr_debug("snd_intelhad_open called\n");
964 intelhaddata = snd_pcm_substream_chip(substream);
965 had_stream = intelhaddata->private_data;
966 runtime = substream->runtime;
Jerome Anand232892f2017-01-25 04:27:53 +0530967 underrun_count = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530968
969 pm_runtime_get(intelhaddata->dev);
970
971 if (had_get_hwstate(intelhaddata)) {
972 pr_err("%s: HDMI cable plugged-out\n", __func__);
973 retval = -ENODEV;
974 goto exit_put_handle;
975 }
976
977 /* Check, if device already in use */
978 if (runtime->private_data) {
979 pr_err("Device already in use\n");
980 retval = -EBUSY;
981 goto exit_put_handle;
982 }
983
984 /* set the runtime hw parameter with local snd_pcm_hardware struct */
985 runtime->hw = snd_intel_hadstream;
986
987 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
988 if (!stream) {
989 retval = -ENOMEM;
990 goto exit_put_handle;
991 }
992 stream->stream_status = STREAM_INIT;
993 runtime->private_data = stream;
994
995 retval = snd_pcm_hw_constraint_integer(runtime,
996 SNDRV_PCM_HW_PARAM_PERIODS);
997 if (retval < 0)
998 goto exit_err;
999
1000 /* Make sure, that the period size is always aligned
1001 * 64byte boundary
1002 */
1003 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1004 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
1005 if (retval < 0) {
1006 pr_err("%s:step_size=64 failed,err=%d\n", __func__, retval);
1007 goto exit_err;
1008 }
1009
1010 return retval;
1011exit_err:
1012 kfree(stream);
1013exit_put_handle:
1014 pm_runtime_put(intelhaddata->dev);
1015 runtime->private_data = NULL;
1016 return retval;
1017}
1018
1019/**
1020 * had_period_elapsed - updates the hardware pointer status
1021 * @had_substream:substream for which the stream function is called
1022 *
1023 */
1024static void had_period_elapsed(void *had_substream)
1025{
1026 struct snd_pcm_substream *substream = had_substream;
1027 struct had_stream_pvt *stream;
1028
1029 /* pr_debug("had_period_elapsed called\n"); */
1030
1031 if (!substream || !substream->runtime)
1032 return;
1033 stream = substream->runtime->private_data;
1034 if (!stream)
1035 return;
1036
1037 if (stream->stream_status != STREAM_RUNNING)
1038 return;
1039 snd_pcm_period_elapsed(substream);
1040}
1041
1042/**
1043 * snd_intelhad_init_stream - internal function to initialize stream info
1044 * @substream:substream for which the stream function is called
1045 *
1046 */
1047static int snd_intelhad_init_stream(struct snd_pcm_substream *substream)
1048{
1049 struct snd_intelhad *intelhaddata = snd_pcm_substream_chip(substream);
1050
1051 pr_debug("snd_intelhad_init_stream called\n");
1052
1053 pr_debug("setting buffer ptr param\n");
1054 intelhaddata->stream_info.period_elapsed = had_period_elapsed;
1055 intelhaddata->stream_info.had_substream = substream;
1056 intelhaddata->stream_info.buffer_ptr = 0;
1057 intelhaddata->stream_info.buffer_rendered = 0;
1058 intelhaddata->stream_info.sfreq = substream->runtime->rate;
1059 return 0;
1060}
1061
1062/**
1063 * snd_intelhad_close- to free parameteres when stream is stopped
1064 *
1065 * @substream: substream for which the function is called
1066 *
1067 * This function is called by ALSA framework when stream is stopped
1068 */
1069static int snd_intelhad_close(struct snd_pcm_substream *substream)
1070{
1071 struct snd_intelhad *intelhaddata;
1072 struct snd_pcm_runtime *runtime;
1073
1074 pr_debug("snd_intelhad_close called\n");
1075
1076 intelhaddata = snd_pcm_substream_chip(substream);
1077 runtime = substream->runtime;
1078
1079 if (!runtime->private_data) {
1080 pr_debug("close() might have called after failed open");
1081 return 0;
1082 }
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;
1092 pr_debug("%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
1093 __func__, __LINE__);
1094 }
1095 kfree(runtime->private_data);
1096 runtime->private_data = NULL;
1097 pm_runtime_put(intelhaddata->dev);
1098 return 0;
1099}
1100
1101/**
1102 * snd_intelhad_hw_params- to setup the hardware parameters
1103 * like allocating the buffers
1104 *
1105 * @substream: substream for which the function is called
1106 * @hw_params: hardware parameters
1107 *
1108 * This function is called by ALSA framework when hardware params are set
1109 */
1110static int snd_intelhad_hw_params(struct snd_pcm_substream *substream,
1111 struct snd_pcm_hw_params *hw_params)
1112{
1113 unsigned long addr;
1114 int pages, buf_size, retval;
1115
1116 pr_debug("snd_intelhad_hw_params called\n");
1117
1118 if (!hw_params)
1119 return -EINVAL;
1120
1121 buf_size = params_buffer_bytes(hw_params);
1122 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1123 if (retval < 0)
1124 return retval;
1125 pr_debug("%s:allocated memory = %d\n", __func__, buf_size);
1126 /* mark the pages as uncached region */
1127 addr = (unsigned long) substream->runtime->dma_area;
1128 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1129 retval = set_memory_uc(addr, pages);
1130 if (retval) {
1131 pr_err("set_memory_uc failed.Error:%d\n", retval);
1132 return retval;
1133 }
1134 memset(substream->runtime->dma_area, 0, buf_size);
1135
1136 return retval;
1137}
1138
1139/**
1140 * snd_intelhad_hw_free- to release the resources allocated during
1141 * hardware params setup
1142 *
1143 * @substream: substream for which the function is called
1144 *
1145 * This function is called by ALSA framework before close callback.
1146 *
1147 */
1148static int snd_intelhad_hw_free(struct snd_pcm_substream *substream)
1149{
1150 unsigned long addr;
1151 u32 pages;
1152
1153 pr_debug("snd_intelhad_hw_free called\n");
1154
1155 /* mark back the pages as cached/writeback region before the free */
1156 if (substream->runtime->dma_area != NULL) {
1157 addr = (unsigned long) substream->runtime->dma_area;
1158 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1159 PAGE_SIZE;
1160 set_memory_wb(addr, pages);
1161 return snd_pcm_lib_free_pages(substream);
1162 }
1163 return 0;
1164}
1165
1166/**
1167 * snd_intelhad_pcm_trigger - stream activities are handled here
1168 * @substream:substream for which the stream function is called
1169 * @cmd:the stream commamd thats requested from upper layer
1170 * This function is called whenever an a stream activity is invoked
1171 */
1172static int snd_intelhad_pcm_trigger(struct snd_pcm_substream *substream,
1173 int cmd)
1174{
1175 int caps, retval = 0;
1176 unsigned long flag_irq;
1177 struct snd_intelhad *intelhaddata;
1178 struct had_stream_pvt *stream;
1179 struct had_pvt_data *had_stream;
1180
1181 pr_debug("snd_intelhad_pcm_trigger called\n");
1182
1183 intelhaddata = snd_pcm_substream_chip(substream);
1184 stream = substream->runtime->private_data;
1185 had_stream = intelhaddata->private_data;
1186
1187 switch (cmd) {
1188 case SNDRV_PCM_TRIGGER_START:
1189 pr_debug("Trigger Start\n");
1190
1191 /* Disable local INTRs till register prgmng is done */
1192 if (had_get_hwstate(intelhaddata)) {
1193 pr_err("_START: HDMI cable plugged-out\n");
1194 retval = -ENODEV;
1195 break;
1196 }
1197 stream->stream_status = STREAM_RUNNING;
1198
1199 had_stream->stream_type = HAD_RUNNING_STREAM;
1200
1201 /* Enable Audio */
1202 /*
1203 * ToDo: Need to enable UNDERRUN interrupts as well
1204 * caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
1205 */
1206 caps = HDMI_AUDIO_BUFFER_DONE;
Takashi Iwai79dda752017-01-30 17:23:39 +01001207 retval = had_set_caps(intelhaddata, HAD_SET_ENABLE_AUDIO_INT,
1208 &caps);
1209 retval = had_set_caps(intelhaddata, HAD_SET_ENABLE_AUDIO, NULL);
Takashi Iwai76296ef2017-01-30 16:09:11 +01001210 snd_intelhad_enable_audio(substream, 1);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301211
1212 pr_debug("Processed _Start\n");
1213
1214 break;
1215
1216 case SNDRV_PCM_TRIGGER_STOP:
1217 pr_debug("Trigger Stop\n");
1218 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irq);
1219 intelhaddata->stream_info.str_id = 0;
1220 intelhaddata->curr_buf = 0;
1221
1222 /* Stop reporting BUFFER_DONE/UNDERRUN to above layers*/
1223
1224 had_stream->stream_type = HAD_INIT;
1225 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irq);
1226 /* Disable Audio */
1227 /*
1228 * ToDo: Need to disable UNDERRUN interrupts as well
1229 * caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
1230 */
1231 caps = HDMI_AUDIO_BUFFER_DONE;
Takashi Iwai79dda752017-01-30 17:23:39 +01001232 had_set_caps(intelhaddata, HAD_SET_DISABLE_AUDIO_INT, &caps);
Takashi Iwai76296ef2017-01-30 16:09:11 +01001233 snd_intelhad_enable_audio(substream, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301234 /* Reset buffer pointers */
Takashi Iwai79dda752017-01-30 17:23:39 +01001235 snd_intelhad_reset_audio(intelhaddata, 1);
1236 snd_intelhad_reset_audio(intelhaddata, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301237 stream->stream_status = STREAM_DROPPED;
Takashi Iwai79dda752017-01-30 17:23:39 +01001238 had_set_caps(intelhaddata, HAD_SET_DISABLE_AUDIO, NULL);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301239 break;
1240
1241 default:
1242 retval = -EINVAL;
1243 }
1244 return retval;
1245}
1246
1247/**
1248 * snd_intelhad_pcm_prepare- internal preparation before starting a stream
1249 *
1250 * @substream: substream for which the function is called
1251 *
1252 * This function is called when a stream is started for internal preparation.
1253 */
1254static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream)
1255{
1256 int retval;
1257 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001258 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301259 struct snd_intelhad *intelhaddata;
1260 struct snd_pcm_runtime *runtime;
1261 struct had_pvt_data *had_stream;
1262
1263 pr_debug("snd_intelhad_pcm_prepare called\n");
1264
1265 intelhaddata = snd_pcm_substream_chip(substream);
1266 runtime = substream->runtime;
1267 had_stream = intelhaddata->private_data;
1268
1269 if (had_get_hwstate(intelhaddata)) {
1270 pr_err("%s: HDMI cable plugged-out\n", __func__);
1271 retval = -ENODEV;
1272 goto prep_end;
1273 }
1274
1275 pr_debug("period_size=%d\n",
1276 (int)frames_to_bytes(runtime, runtime->period_size));
1277 pr_debug("periods=%d\n", runtime->periods);
1278 pr_debug("buffer_size=%d\n", (int)snd_pcm_lib_buffer_bytes(substream));
1279 pr_debug("rate=%d\n", runtime->rate);
1280 pr_debug("channels=%d\n", runtime->channels);
1281
1282 if (intelhaddata->stream_info.str_id) {
1283 pr_debug("_prepare is called for existing str_id#%d\n",
1284 intelhaddata->stream_info.str_id);
1285 retval = snd_intelhad_pcm_trigger(substream,
1286 SNDRV_PCM_TRIGGER_STOP);
1287 return retval;
1288 }
1289
1290 retval = snd_intelhad_init_stream(substream);
1291 if (retval)
1292 goto prep_end;
1293
1294
1295 /* Get N value in KHz */
Takashi Iwai79dda752017-01-30 17:23:39 +01001296 retval = had_get_caps(intelhaddata, HAD_GET_DISPLAY_RATE,
1297 &disp_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301298 if (retval) {
1299 pr_err("querying display sampling freq failed %#x\n", retval);
1300 goto prep_end;
1301 }
1302
Takashi Iwai79dda752017-01-30 17:23:39 +01001303 had_get_caps(intelhaddata, HAD_GET_ELD, &intelhaddata->eeld);
1304 had_get_caps(intelhaddata, HAD_GET_DP_OUTPUT, &intelhaddata->dp_output);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301305
Takashi Iwai76296ef2017-01-30 16:09:11 +01001306 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1307 intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301308 if (retval) {
1309 pr_err("programming N value failed %#x\n", retval);
1310 goto prep_end;
1311 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001312
1313 if (intelhaddata->dp_output)
Takashi Iwai79dda752017-01-30 17:23:39 +01001314 had_get_caps(intelhaddata, HAD_GET_LINK_RATE, &link_rate);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001315
1316
Takashi Iwai76296ef2017-01-30 16:09:11 +01001317 snd_intelhad_prog_cts(substream->runtime->rate,
1318 disp_samp_freq, link_rate,
1319 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301320
Takashi Iwai76296ef2017-01-30 16:09:11 +01001321 snd_intelhad_prog_dip(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301322
Takashi Iwai76296ef2017-01-30 16:09:11 +01001323 retval = snd_intelhad_audio_ctrl(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301324
1325 /* Prog buffer address */
1326 retval = snd_intelhad_prog_buffer(intelhaddata,
1327 HAD_BUF_TYPE_A, HAD_BUF_TYPE_D);
1328
1329 /*
1330 * Program channel mapping in following order:
1331 * FL, FR, C, LFE, RL, RR
1332 */
1333
Takashi Iwai79dda752017-01-30 17:23:39 +01001334 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301335
1336prep_end:
1337 return retval;
1338}
1339
1340/**
1341 * snd_intelhad_pcm_pointer- to send the current buffer pointerprocessed by hw
1342 *
1343 * @substream: substream for which the function is called
1344 *
1345 * This function is called by ALSA framework to get the current hw buffer ptr
1346 * when a period is elapsed
1347 */
1348static snd_pcm_uframes_t snd_intelhad_pcm_pointer(
1349 struct snd_pcm_substream *substream)
1350{
1351 struct snd_intelhad *intelhaddata;
1352 u32 bytes_rendered = 0;
1353 u32 t;
1354 int buf_id;
1355
1356 /* pr_debug("snd_intelhad_pcm_pointer called\n"); */
1357
1358 intelhaddata = snd_pcm_substream_chip(substream);
1359
1360 if (intelhaddata->flag_underrun) {
1361 intelhaddata->flag_underrun = 0;
1362 return SNDRV_PCM_POS_XRUN;
1363 }
1364
1365 /* Use a hw register to calculate sub-period position reports.
1366 * This makes PulseAudio happier.
1367 */
1368
1369 buf_id = intelhaddata->curr_buf % 4;
Takashi Iwai79dda752017-01-30 17:23:39 +01001370 had_read_register(intelhaddata,
1371 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH), &t);
Jerome Anand232892f2017-01-25 04:27:53 +05301372
1373 if ((t == 0) || (t == ((u32)-1L))) {
1374 underrun_count++;
1375 pr_debug("discovered buffer done for buf %d, count = %d\n",
1376 buf_id, underrun_count);
1377
1378 if (underrun_count > (HAD_MIN_PERIODS/2)) {
1379 pr_debug("assume audio_codec_reset, underrun = %d - do xrun\n",
1380 underrun_count);
1381 underrun_count = 0;
1382 return SNDRV_PCM_POS_XRUN;
1383 }
1384 } else {
1385 /* Reset Counter */
1386 underrun_count = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301387 }
Jerome Anand232892f2017-01-25 04:27:53 +05301388
Jerome Anand5dab11d2017-01-25 04:27:52 +05301389 t = intelhaddata->buf_info[buf_id].buf_size - t;
1390
1391 if (intelhaddata->stream_info.buffer_rendered)
1392 div_u64_rem(intelhaddata->stream_info.buffer_rendered,
1393 intelhaddata->stream_info.ring_buf_size,
1394 &(bytes_rendered));
1395
1396 intelhaddata->stream_info.buffer_ptr = bytes_to_frames(
1397 substream->runtime,
1398 bytes_rendered + t);
1399 return intelhaddata->stream_info.buffer_ptr;
1400}
1401
1402/**
1403 * snd_intelhad_pcm_mmap- mmaps a kernel buffer to user space for copying data
1404 *
1405 * @substream: substream for which the function is called
1406 * @vma: struct instance of memory VMM memory area
1407 *
1408 * This function is called by OS when a user space component
1409 * tries to get mmap memory from driver
1410 */
1411static int snd_intelhad_pcm_mmap(struct snd_pcm_substream *substream,
1412 struct vm_area_struct *vma)
1413{
1414
1415 pr_debug("snd_intelhad_pcm_mmap called\n");
1416
1417 pr_debug("entry with prot:%s\n", __func__);
1418 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1419 return remap_pfn_range(vma, vma->vm_start,
1420 substream->dma_buffer.addr >> PAGE_SHIFT,
1421 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1422}
1423
1424int hdmi_audio_mode_change(struct snd_pcm_substream *substream)
1425{
1426 int retval = 0;
1427 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001428 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301429 struct snd_intelhad *intelhaddata;
1430
1431 intelhaddata = snd_pcm_substream_chip(substream);
1432
1433 /* Disable Audio */
Takashi Iwai76296ef2017-01-30 16:09:11 +01001434 snd_intelhad_enable_audio(substream, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301435
1436 /* Update CTS value */
Takashi Iwai79dda752017-01-30 17:23:39 +01001437 retval = had_get_caps(intelhaddata, HAD_GET_DISPLAY_RATE,
1438 &disp_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301439 if (retval) {
1440 pr_err("querying display sampling freq failed %#x\n", retval);
1441 goto out;
1442 }
1443
Takashi Iwai76296ef2017-01-30 16:09:11 +01001444 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1445 intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301446 if (retval) {
1447 pr_err("programming N value failed %#x\n", retval);
1448 goto out;
1449 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001450
1451 if (intelhaddata->dp_output)
Takashi Iwai79dda752017-01-30 17:23:39 +01001452 had_get_caps(intelhaddata, HAD_GET_LINK_RATE, &link_rate);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001453
Takashi Iwai76296ef2017-01-30 16:09:11 +01001454 snd_intelhad_prog_cts(substream->runtime->rate,
1455 disp_samp_freq, link_rate,
1456 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301457
1458 /* Enable Audio */
Takashi Iwai76296ef2017-01-30 16:09:11 +01001459 snd_intelhad_enable_audio(substream, 1);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301460
1461out:
1462 return retval;
1463}
1464
1465/*PCM operations structure and the calls back for the same */
1466struct snd_pcm_ops snd_intelhad_playback_ops = {
1467 .open = snd_intelhad_open,
1468 .close = snd_intelhad_close,
1469 .ioctl = snd_pcm_lib_ioctl,
1470 .hw_params = snd_intelhad_hw_params,
1471 .hw_free = snd_intelhad_hw_free,
1472 .prepare = snd_intelhad_pcm_prepare,
1473 .trigger = snd_intelhad_pcm_trigger,
1474 .pointer = snd_intelhad_pcm_pointer,
1475 .mmap = snd_intelhad_pcm_mmap,
1476};
1477
1478/**
1479 * snd_intelhad_create - to crete alsa card instance
1480 *
1481 * @intelhaddata: pointer to internal context
1482 * @card: pointer to card
1483 *
1484 * This function is called when the hdmi cable is plugged in
1485 */
1486static int snd_intelhad_create(
1487 struct snd_intelhad *intelhaddata,
1488 struct snd_card *card)
1489{
1490 int retval;
1491 static struct snd_device_ops ops = {
1492 };
1493
1494 pr_debug("snd_intelhad_create called\n");
1495
1496 if (!intelhaddata)
1497 return -EINVAL;
1498
1499 /* ALSA api to register the device */
1500 retval = snd_device_new(card, SNDRV_DEV_LOWLEVEL, intelhaddata, &ops);
1501 return retval;
1502}
1503/**
1504 * snd_intelhad_pcm_free - to free the memory allocated
1505 *
1506 * @pcm: pointer to pcm instance
1507 * This function is called when the device is removed
1508 */
1509static void snd_intelhad_pcm_free(struct snd_pcm *pcm)
1510{
1511 pr_debug("Freeing PCM preallocated pages\n");
1512 snd_pcm_lib_preallocate_free_for_all(pcm);
1513}
1514
1515static int had_iec958_info(struct snd_kcontrol *kcontrol,
1516 struct snd_ctl_elem_info *uinfo)
1517{
1518 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1519 uinfo->count = 1;
1520 return 0;
1521}
1522
1523static int had_iec958_get(struct snd_kcontrol *kcontrol,
1524 struct snd_ctl_elem_value *ucontrol)
1525{
1526 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1527
1528 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1529 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1530 ucontrol->value.iec958.status[2] =
1531 (intelhaddata->aes_bits >> 16) & 0xff;
1532 ucontrol->value.iec958.status[3] =
1533 (intelhaddata->aes_bits >> 24) & 0xff;
1534 return 0;
1535}
1536static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1537 struct snd_ctl_elem_value *ucontrol)
1538{
1539 ucontrol->value.iec958.status[0] = 0xff;
1540 ucontrol->value.iec958.status[1] = 0xff;
1541 ucontrol->value.iec958.status[2] = 0xff;
1542 ucontrol->value.iec958.status[3] = 0xff;
1543 return 0;
1544}
1545static int had_iec958_put(struct snd_kcontrol *kcontrol,
1546 struct snd_ctl_elem_value *ucontrol)
1547{
1548 unsigned int val;
1549 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1550
1551 pr_debug("entered had_iec958_put\n");
1552 val = (ucontrol->value.iec958.status[0] << 0) |
1553 (ucontrol->value.iec958.status[1] << 8) |
1554 (ucontrol->value.iec958.status[2] << 16) |
1555 (ucontrol->value.iec958.status[3] << 24);
1556 if (intelhaddata->aes_bits != val) {
1557 intelhaddata->aes_bits = val;
1558 return 1;
1559 }
1560 return 1;
1561}
1562
1563static struct snd_kcontrol_new had_control_iec958_mask = {
1564 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1565 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1566 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1567 .info = had_iec958_info, /* shared */
1568 .get = had_iec958_mask_get,
1569};
1570
1571static struct snd_kcontrol_new had_control_iec958 = {
1572 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1573 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1574 .info = had_iec958_info,
1575 .get = had_iec958_get,
1576 .put = had_iec958_put
1577};
1578
Takashi Iwai79dda752017-01-30 17:23:39 +01001579/*
Jerome Anand5dab11d2017-01-25 04:27:52 +05301580 * hdmi_audio_probe - to create sound card instance for HDMI audio playabck
1581 *
Takashi Iwai79dda752017-01-30 17:23:39 +01001582 * @devptr: platform device
1583 * @had_ret: pointer to store the created snd_intelhad object
Jerome Anand5dab11d2017-01-25 04:27:52 +05301584 *
Takashi Iwai79dda752017-01-30 17:23:39 +01001585 * This function is called when the platform device is probed. This function
Jerome Anand5dab11d2017-01-25 04:27:52 +05301586 * creates and registers the sound card with ALSA
1587 */
Takashi Iwai79dda752017-01-30 17:23:39 +01001588int hdmi_audio_probe(struct platform_device *devptr,
1589 struct snd_intelhad **had_ret)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301590{
1591 int retval;
1592 struct snd_pcm *pcm;
1593 struct snd_card *card;
1594 struct had_callback_ops ops_cb;
1595 struct snd_intelhad *intelhaddata;
1596 struct had_pvt_data *had_stream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301597
1598 pr_debug("Enter %s\n", __func__);
1599
1600 pr_debug("hdmi_audio_probe dma_mask: %p\n", devptr->dev.dma_mask);
1601
1602 /* allocate memory for saving internal context and working */
1603 intelhaddata = kzalloc(sizeof(*intelhaddata), GFP_KERNEL);
1604 if (!intelhaddata)
1605 return -ENOMEM;
1606
1607 had_stream = kzalloc(sizeof(*had_stream), GFP_KERNEL);
1608 if (!had_stream) {
1609 retval = -ENOMEM;
1610 goto free_haddata;
1611 }
1612
Jerome Anand5dab11d2017-01-25 04:27:52 +05301613 ops_cb.intel_had_event_call_back = had_event_handler;
1614
1615 /* registering with display driver to get access to display APIs */
1616
Takashi Iwai9eca88c2017-01-30 16:37:06 +01001617 retval = mid_hdmi_audio_setup(ops_cb.intel_had_event_call_back);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301618 if (retval) {
1619 pr_err("querying display driver APIs failed %#x\n", retval);
1620 goto free_hadstream;
1621 }
Takashi Iwaidd895f22017-01-30 17:31:29 +01001622
Jerome Anand5dab11d2017-01-25 04:27:52 +05301623 spin_lock_init(&intelhaddata->had_spinlock);
1624 intelhaddata->drv_status = HAD_DRV_DISCONNECTED;
1625 pr_debug("%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
1626 __func__, __LINE__);
1627
1628 /* create a card instance with ALSA framework */
1629 retval = snd_card_new(&devptr->dev, hdmi_card_index, hdmi_card_id,
1630 THIS_MODULE, 0, &card);
1631
1632 if (retval)
Takashi Iwaidd895f22017-01-30 17:31:29 +01001633 goto free_hadstream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301634 intelhaddata->card = card;
1635 intelhaddata->card_id = hdmi_card_id;
1636 intelhaddata->card_index = card->number;
1637 intelhaddata->private_data = had_stream;
1638 intelhaddata->flag_underrun = 0;
1639 intelhaddata->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
1640 strncpy(card->driver, INTEL_HAD, strlen(INTEL_HAD));
1641 strncpy(card->shortname, INTEL_HAD, strlen(INTEL_HAD));
1642
1643 retval = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
1644 MAX_CAP_STREAMS, &pcm);
1645 if (retval)
1646 goto err;
1647
1648 /* setup private data which can be retrieved when required */
1649 pcm->private_data = intelhaddata;
1650 pcm->private_free = snd_intelhad_pcm_free;
1651 pcm->info_flags = 0;
1652 strncpy(pcm->name, card->shortname, strlen(card->shortname));
1653 /* setup the ops for palyabck */
1654 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1655 &snd_intelhad_playback_ops);
1656 /* allocate dma pages for ALSA stream operations
1657 * memory allocated is based on size, not max value
1658 * thus using same argument for max & size
1659 */
1660 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
1661 SNDRV_DMA_TYPE_DEV, NULL,
1662 HAD_MAX_BUFFER, HAD_MAX_BUFFER);
1663
1664 if (card->dev == NULL)
1665 pr_debug("card->dev is NULL!!!!! Should not be this case\n");
1666 else if (card->dev->dma_mask == NULL)
1667 pr_debug("hdmi_audio_probe dma_mask is NULL!!!!!\n");
1668 else
1669 pr_debug("hdmi_audio_probe dma_mask is : %p\n",
1670 card->dev->dma_mask);
1671
1672 if (retval)
1673 goto err;
1674
1675 /* internal function call to register device with ALSA */
1676 retval = snd_intelhad_create(intelhaddata, card);
1677 if (retval)
1678 goto err;
1679
1680 card->private_data = &intelhaddata;
1681 retval = snd_card_register(card);
1682 if (retval)
1683 goto err;
1684
1685 /* IEC958 controls */
1686 retval = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958_mask,
1687 intelhaddata));
1688 if (retval < 0)
1689 goto err;
1690 retval = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958,
1691 intelhaddata));
1692 if (retval < 0)
1693 goto err;
1694
1695 init_channel_allocations();
1696
1697 /* Register channel map controls */
1698 retval = had_register_chmap_ctls(intelhaddata, pcm);
1699 if (retval < 0)
1700 goto err;
1701
1702 intelhaddata->dev = &devptr->dev;
1703 pm_runtime_set_active(intelhaddata->dev);
1704 pm_runtime_enable(intelhaddata->dev);
1705
Jerome Anand5dab11d2017-01-25 04:27:52 +05301706 intelhaddata->hw_silence = 1;
Takashi Iwai79dda752017-01-30 17:23:39 +01001707 *had_ret = intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301708
Takashi Iwai79dda752017-01-30 17:23:39 +01001709 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301710err:
1711 snd_card_free(card);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301712free_hadstream:
1713 kfree(had_stream);
1714 pm_runtime_disable(intelhaddata->dev);
1715 intelhaddata->dev = NULL;
1716free_haddata:
1717 kfree(intelhaddata);
1718 intelhaddata = NULL;
1719 pr_err("Error returned from %s api %#x\n", __func__, retval);
1720 return retval;
1721}
1722
Takashi Iwai79dda752017-01-30 17:23:39 +01001723/*
Jerome Anand5dab11d2017-01-25 04:27:52 +05301724 * hdmi_audio_remove - removes the alsa card
1725 *
1726 *@haddata: pointer to HAD private data
1727 *
1728 * This function is called when the hdmi cable is un-plugged. This function
1729 * free the sound card.
1730 */
Takashi Iwai79dda752017-01-30 17:23:39 +01001731int hdmi_audio_remove(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301732{
Jerome Anand5dab11d2017-01-25 04:27:52 +05301733 int caps;
1734
1735 pr_debug("Enter %s\n", __func__);
1736
1737 if (!intelhaddata)
1738 return 0;
1739
1740 if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) {
1741 caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
Takashi Iwai79dda752017-01-30 17:23:39 +01001742 had_set_caps(intelhaddata, HAD_SET_DISABLE_AUDIO_INT, &caps);
1743 had_set_caps(intelhaddata, HAD_SET_DISABLE_AUDIO, NULL);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301744 }
1745 snd_card_free(intelhaddata->card);
1746 kfree(intelhaddata->private_data);
1747 kfree(intelhaddata);
1748 return 0;
1749}
1750
1751MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1752MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1753MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1754MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1755MODULE_DESCRIPTION("Intel HDMI Audio driver");
1756MODULE_LICENSE("GPL v2");
1757MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");