blob: e08691110a488f7350afbe95f8e2ec393e9c6bec [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 Anand5dab11d2017-01-25 04:27:52 +053043
44module_param_named(index, hdmi_card_index, int, 0444);
45MODULE_PARM_DESC(index,
46 "Index value for INTEL Intel HDMI Audio controller.");
47module_param_named(id, hdmi_card_id, charp, 0444);
48MODULE_PARM_DESC(id,
49 "ID string for INTEL Intel HDMI Audio controller.");
50
51/*
52 * ELD SA bits in the CEA Speaker Allocation data block
53 */
54static int eld_speaker_allocation_bits[] = {
55 [0] = FL | FR,
56 [1] = LFE,
57 [2] = FC,
58 [3] = RL | RR,
59 [4] = RC,
60 [5] = FLC | FRC,
61 [6] = RLC | RRC,
62 /* the following are not defined in ELD yet */
63 [7] = 0,
64};
65
66/*
67 * This is an ordered list!
68 *
69 * The preceding ones have better chances to be selected by
70 * hdmi_channel_allocation().
71 */
72static struct cea_channel_speaker_allocation channel_allocations[] = {
73/* channel: 7 6 5 4 3 2 1 0 */
74{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
75 /* 2.1 */
76{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
77 /* Dolby Surround */
78{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
79 /* surround40 */
80{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
81 /* surround41 */
82{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
83 /* surround50 */
84{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
85 /* surround51 */
86{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
87 /* 6.1 */
88{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
89 /* surround71 */
90{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
91
92{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
93{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
94{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
95{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
96{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
97{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
98{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
99{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
100{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
101{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
102{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
103{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
104{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
105{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
106{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
107{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
108{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
109{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
110{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
111{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
112{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
113{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
114{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
115};
116
117static struct channel_map_table map_tables[] = {
118 { SNDRV_CHMAP_FL, 0x00, FL },
119 { SNDRV_CHMAP_FR, 0x01, FR },
120 { SNDRV_CHMAP_RL, 0x04, RL },
121 { SNDRV_CHMAP_RR, 0x05, RR },
122 { SNDRV_CHMAP_LFE, 0x02, LFE },
123 { SNDRV_CHMAP_FC, 0x03, FC },
124 { SNDRV_CHMAP_RLC, 0x06, RLC },
125 { SNDRV_CHMAP_RRC, 0x07, RRC },
126 {} /* terminator */
127};
128
129/* hardware capability structure */
130static const struct snd_pcm_hardware snd_intel_hadstream = {
131 .info = (SNDRV_PCM_INFO_INTERLEAVED |
132 SNDRV_PCM_INFO_DOUBLE |
133 SNDRV_PCM_INFO_MMAP|
134 SNDRV_PCM_INFO_MMAP_VALID |
135 SNDRV_PCM_INFO_BATCH),
136 .formats = (SNDRV_PCM_FMTBIT_S24 |
137 SNDRV_PCM_FMTBIT_U24),
138 .rates = SNDRV_PCM_RATE_32000 |
139 SNDRV_PCM_RATE_44100 |
140 SNDRV_PCM_RATE_48000 |
141 SNDRV_PCM_RATE_88200 |
142 SNDRV_PCM_RATE_96000 |
143 SNDRV_PCM_RATE_176400 |
144 SNDRV_PCM_RATE_192000,
145 .rate_min = HAD_MIN_RATE,
146 .rate_max = HAD_MAX_RATE,
147 .channels_min = HAD_MIN_CHANNEL,
148 .channels_max = HAD_MAX_CHANNEL,
149 .buffer_bytes_max = HAD_MAX_BUFFER,
150 .period_bytes_min = HAD_MIN_PERIOD_BYTES,
151 .period_bytes_max = HAD_MAX_PERIOD_BYTES,
152 .periods_min = HAD_MIN_PERIODS,
153 .periods_max = HAD_MAX_PERIODS,
154 .fifo_size = HAD_FIFO_SIZE,
155};
156
157/* Register access functions */
158
159int had_get_hwstate(struct snd_intelhad *intelhaddata)
160{
161 /* Check for device presence -SW state */
162 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
163 pr_debug("%s:Device not connected:%d\n", __func__,
164 intelhaddata->drv_status);
165 return -ENODEV;
166 }
167
168 return 0;
169}
170
Takashi Iwai79dda752017-01-30 17:23:39 +0100171int had_get_caps(struct snd_intelhad *intelhaddata,
172 enum had_caps_list query, void *caps)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530173{
Takashi Iwaibf8b24f2017-01-30 18:09:01 +0100174 struct platform_device *pdev = to_platform_device(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530175 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530176
177 retval = had_get_hwstate(intelhaddata);
178 if (!retval)
Takashi Iwaibf8b24f2017-01-30 18:09:01 +0100179 retval = mid_hdmi_audio_get_caps(pdev, 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{
Takashi Iwaibf8b24f2017-01-30 18:09:01 +0100187 struct platform_device *pdev = to_platform_device(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530188 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530189
190 retval = had_get_hwstate(intelhaddata);
191 if (!retval)
Takashi Iwaibf8b24f2017-01-30 18:09:01 +0100192 retval = mid_hdmi_audio_set_caps(pdev, set_element, caps);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530193
194 return retval;
195}
196
Takashi Iwai79dda752017-01-30 17:23:39 +0100197int had_read_register(struct snd_intelhad *intelhaddata, u32 offset, u32 *data)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530198{
Takashi Iwaibf8b24f2017-01-30 18:09:01 +0100199 struct platform_device *pdev = to_platform_device(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530200 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530201
202 retval = had_get_hwstate(intelhaddata);
203 if (!retval)
Takashi Iwaibf8b24f2017-01-30 18:09:01 +0100204 retval = mid_hdmi_audio_read(pdev, 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{
Takashi Iwaibf8b24f2017-01-30 18:09:01 +0100211 struct platform_device *pdev = to_platform_device(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530212 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530213
214 retval = had_get_hwstate(intelhaddata);
215 if (!retval)
Takashi Iwaibf8b24f2017-01-30 18:09:01 +0100216 retval = mid_hdmi_audio_write(pdev, offset, data);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530217
218 return retval;
219}
220
Takashi Iwai79dda752017-01-30 17:23:39 +0100221int had_read_modify(struct snd_intelhad *intelhaddata, u32 offset,
222 u32 data, u32 mask)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530223{
Takashi Iwaibf8b24f2017-01-30 18:09:01 +0100224 struct platform_device *pdev = to_platform_device(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530225 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530226
227 retval = had_get_hwstate(intelhaddata);
228 if (!retval)
Takashi Iwaibf8b24f2017-01-30 18:09:01 +0100229 retval = mid_hdmi_audio_rmw(pdev, offset, data, mask);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530230
231 return retval;
232}
233/**
234 * function to read-modify
235 * AUD_CONFIG register on VLV2.The had_read_modify() function should not
236 * directly be used on VLV2 for updating AUD_CONFIG register.
237 * This is because:
238 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
239 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
240 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
241 * register. This field should be 1xy binary for configuration with 6 or
242 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
243 * causes the "channels" field to be updated as 0xy binary resulting in
244 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
245 * appropriate value when doing read-modify of AUD_CONFIG register.
246 *
247 * @substream: the current substream or NULL if no active substream
248 * @data : data to be written
249 * @mask : mask
250 *
251 */
252static int had_read_modify_aud_config_v2(struct snd_pcm_substream *substream,
253 u32 data, u32 mask)
254{
Takashi Iwai79dda752017-01-30 17:23:39 +0100255 struct snd_intelhad *intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530256 union aud_cfg cfg_val = {.cfg_regval = 0};
257 u8 channels;
258
259 /*
260 * If substream is NULL, there is no active stream.
261 * In this case just set channels to 2
262 */
263 if (substream)
264 channels = substream->runtime->channels;
265 else
266 channels = 2;
267 cfg_val.cfg_regx_v2.num_ch = channels - 2;
268
269 data = data | cfg_val.cfg_regval;
270 mask = mask | AUD_CONFIG_CH_MASK_V2;
271
272 pr_debug("%s : data = %x, mask =%x\n", __func__, data, mask);
273
Takashi Iwai79dda752017-01-30 17:23:39 +0100274 return had_read_modify(intelhaddata, AUD_CONFIG, data, mask);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530275}
276
Takashi Iwai76296ef2017-01-30 16:09:11 +0100277void snd_intelhad_enable_audio(struct snd_pcm_substream *substream, u8 enable)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530278{
279 had_read_modify_aud_config_v2(substream, enable, BIT(0));
280}
281
Takashi Iwai79dda752017-01-30 17:23:39 +0100282static void snd_intelhad_reset_audio(struct snd_intelhad *intelhaddata,
283 u8 reset)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530284{
Takashi Iwai79dda752017-01-30 17:23:39 +0100285 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, reset);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530286}
287
288/**
289 * initialize audio channel status registers
290 * This function is called in the prepare callback
291 */
292static int had_prog_status_reg(struct snd_pcm_substream *substream,
293 struct snd_intelhad *intelhaddata)
294{
295 union aud_cfg cfg_val = {.cfg_regval = 0};
296 union aud_ch_status_0 ch_stat0 = {.status_0_regval = 0};
297 union aud_ch_status_1 ch_stat1 = {.status_1_regval = 0};
298 int format;
299
300 pr_debug("Entry %s\n", __func__);
301
302 ch_stat0.status_0_regx.lpcm_id = (intelhaddata->aes_bits &
303 IEC958_AES0_NONAUDIO)>>1;
304 ch_stat0.status_0_regx.clk_acc = (intelhaddata->aes_bits &
305 IEC958_AES3_CON_CLOCK)>>4;
Takashi Iwai4812dcc2017-01-30 15:58:15 +0100306 cfg_val.cfg_regx_v2.val_bit = ch_stat0.status_0_regx.lpcm_id;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530307
308 switch (substream->runtime->rate) {
309 case AUD_SAMPLE_RATE_32:
310 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_32KHZ;
311 break;
312
313 case AUD_SAMPLE_RATE_44_1:
314 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_44KHZ;
315 break;
316 case AUD_SAMPLE_RATE_48:
317 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_48KHZ;
318 break;
319 case AUD_SAMPLE_RATE_88_2:
320 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_88KHZ;
321 break;
322 case AUD_SAMPLE_RATE_96:
323 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_96KHZ;
324 break;
325 case AUD_SAMPLE_RATE_176_4:
326 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_176KHZ;
327 break;
328 case AUD_SAMPLE_RATE_192:
329 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_192KHZ;
330 break;
331
332 default:
333 /* control should never come here */
334 return -EINVAL;
335 break;
336
337 }
Takashi Iwai79dda752017-01-30 17:23:39 +0100338 had_write_register(intelhaddata,
339 AUD_CH_STATUS_0, ch_stat0.status_0_regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530340
341 format = substream->runtime->format;
342
343 if (format == SNDRV_PCM_FORMAT_S16_LE) {
344 ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_20;
345 ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_16BITS;
346 } else if (format == SNDRV_PCM_FORMAT_S24_LE) {
347 ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_24;
348 ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_24BITS;
349 } else {
350 ch_stat1.status_1_regx.max_wrd_len = 0;
351 ch_stat1.status_1_regx.wrd_len = 0;
352 }
Takashi Iwai79dda752017-01-30 17:23:39 +0100353 had_write_register(intelhaddata,
354 AUD_CH_STATUS_1, ch_stat1.status_1_regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530355 return 0;
356}
357
Takashi Iwai76296ef2017-01-30 16:09:11 +0100358/*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530359 * function to initialize audio
360 * registers and buffer confgiuration registers
361 * This function is called in the prepare callback
362 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100363static int snd_intelhad_audio_ctrl(struct snd_pcm_substream *substream,
364 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530365{
366 union aud_cfg cfg_val = {.cfg_regval = 0};
367 union aud_buf_config buf_cfg = {.buf_cfgval = 0};
368 u8 channels;
369
370 had_prog_status_reg(substream, intelhaddata);
371
372 buf_cfg.buf_cfg_regx_v2.audio_fifo_watermark = FIFO_THRESHOLD;
373 buf_cfg.buf_cfg_regx_v2.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
374 buf_cfg.buf_cfg_regx_v2.aud_delay = 0;
Takashi Iwai79dda752017-01-30 17:23:39 +0100375 had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.buf_cfgval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530376
377 channels = substream->runtime->channels;
378 cfg_val.cfg_regx_v2.num_ch = channels - 2;
379 if (channels <= 2)
380 cfg_val.cfg_regx_v2.layout = LAYOUT0;
381 else
382 cfg_val.cfg_regx_v2.layout = LAYOUT1;
383
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600384 cfg_val.cfg_regx_v2.val_bit = 1;
Takashi Iwai79dda752017-01-30 17:23:39 +0100385 had_write_register(intelhaddata, AUD_CONFIG, cfg_val.cfg_regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530386 return 0;
387}
388
Jerome Anand5dab11d2017-01-25 04:27:52 +0530389/*
390 * Compute derived values in channel_allocations[].
391 */
392static void init_channel_allocations(void)
393{
394 int i, j;
395 struct cea_channel_speaker_allocation *p;
396
397 pr_debug("%s: Enter\n", __func__);
398
399 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
400 p = channel_allocations + i;
401 p->channels = 0;
402 p->spk_mask = 0;
403 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
404 if (p->speakers[j]) {
405 p->channels++;
406 p->spk_mask |= p->speakers[j];
407 }
408 }
409}
410
411/*
412 * The transformation takes two steps:
413 *
414 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
415 * spk_mask => (channel_allocations[]) => ai->CA
416 *
417 * TODO: it could select the wrong CA from multiple candidates.
418 */
419static int snd_intelhad_channel_allocation(struct snd_intelhad *intelhaddata,
420 int channels)
421{
422 int i;
423 int ca = 0;
424 int spk_mask = 0;
425
426 /*
427 * CA defaults to 0 for basic stereo audio
428 */
429 if (channels <= 2)
430 return 0;
431
432 /*
433 * expand ELD's speaker allocation mask
434 *
435 * ELD tells the speaker mask in a compact(paired) form,
436 * expand ELD's notions to match the ones used by Audio InfoFrame.
437 */
438
439 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
440 if (intelhaddata->eeld.speaker_allocation_block & (1 << i))
441 spk_mask |= eld_speaker_allocation_bits[i];
442 }
443
444 /* search for the first working match in the CA table */
445 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
446 if (channels == channel_allocations[i].channels &&
447 (spk_mask & channel_allocations[i].spk_mask) ==
448 channel_allocations[i].spk_mask) {
449 ca = channel_allocations[i].ca_index;
450 break;
451 }
452 }
453
454 pr_debug("HDMI: select CA 0x%x for %d\n", ca, channels);
455
456 return ca;
457}
458
459/* from speaker bit mask to ALSA API channel position */
460static int spk_to_chmap(int spk)
461{
462 struct channel_map_table *t = map_tables;
463
464 for (; t->map; t++) {
465 if (t->spk_mask == spk)
466 return t->map;
467 }
468 return 0;
469}
470
471void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
472{
473 int i = 0, c = 0;
474 int spk_mask = 0;
475 struct snd_pcm_chmap_elem *chmap;
476 u8 eld_high, eld_high_mask = 0xF0;
477 u8 high_msb;
478
479 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
480 if (chmap == NULL) {
481 intelhaddata->chmap->chmap = NULL;
482 return;
483 }
484
Takashi Iwai79dda752017-01-30 17:23:39 +0100485 had_get_caps(intelhaddata, HAD_GET_ELD, &intelhaddata->eeld);
486 had_get_caps(intelhaddata, HAD_GET_DP_OUTPUT, &intelhaddata->dp_output);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530487
488 pr_debug("eeld.speaker_allocation_block = %x\n",
489 intelhaddata->eeld.speaker_allocation_block);
490
491 /* WA: Fix the max channel supported to 8 */
492
493 /*
494 * Sink may support more than 8 channels, if eld_high has more than
495 * one bit set. SOC supports max 8 channels.
496 * Refer eld_speaker_allocation_bits, for sink speaker allocation
497 */
498
499 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
500 eld_high = intelhaddata->eeld.speaker_allocation_block & eld_high_mask;
501 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
502 /* eld_high & (eld_high-1): if more than 1 bit set */
503 /* 0x1F: 7 channels */
504 for (i = 1; i < 4; i++) {
505 high_msb = eld_high & (0x80 >> i);
506 if (high_msb) {
507 intelhaddata->eeld.speaker_allocation_block &=
508 high_msb | 0xF;
509 break;
510 }
511 }
512 }
513
514 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
515 if (intelhaddata->eeld.speaker_allocation_block & (1 << i))
516 spk_mask |= eld_speaker_allocation_bits[i];
517 }
518
519 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
520 if (spk_mask == channel_allocations[i].spk_mask) {
521 for (c = 0; c < channel_allocations[i].channels; c++) {
522 chmap->map[c] = spk_to_chmap(
523 channel_allocations[i].speakers[
524 (MAX_SPEAKERS - 1)-c]);
525 }
526 chmap->channels = channel_allocations[i].channels;
527 intelhaddata->chmap->chmap = chmap;
528 break;
529 }
530 }
531 if (i >= ARRAY_SIZE(channel_allocations)) {
532 intelhaddata->chmap->chmap = NULL;
533 kfree(chmap);
534 }
535}
536
537/*
538 * ALSA API channel-map control callbacks
539 */
540static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
541 struct snd_ctl_elem_info *uinfo)
542{
543 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
544 struct snd_intelhad *intelhaddata = info->private_data;
545
546 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
547 return -ENODEV;
548 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
549 uinfo->count = HAD_MAX_CHANNEL;
550 uinfo->value.integer.min = 0;
551 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
552 return 0;
553}
554
555static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
556 struct snd_ctl_elem_value *ucontrol)
557{
558 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
559 struct snd_intelhad *intelhaddata = info->private_data;
560 int i = 0;
561 const struct snd_pcm_chmap_elem *chmap;
562
563 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
564 return -ENODEV;
565 if (intelhaddata->chmap->chmap == NULL)
566 return -ENODATA;
567 chmap = intelhaddata->chmap->chmap;
568 for (i = 0; i < chmap->channels; i++) {
569 ucontrol->value.integer.value[i] = chmap->map[i];
570 pr_debug("chmap->map[%d] = %d\n", i, chmap->map[i]);
571 }
572
573 return 0;
574}
575
576static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
577 struct snd_pcm *pcm)
578{
579 int err = 0;
580
581 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
582 NULL, 0, (unsigned long)intelhaddata,
583 &intelhaddata->chmap);
584 if (err < 0)
585 return err;
586
587 intelhaddata->chmap->private_data = intelhaddata;
588 intelhaddata->kctl = intelhaddata->chmap->kctl;
589 intelhaddata->kctl->info = had_chmap_ctl_info;
590 intelhaddata->kctl->get = had_chmap_ctl_get;
591 intelhaddata->chmap->chmap = NULL;
592 return 0;
593}
594
Takashi Iwai76296ef2017-01-30 16:09:11 +0100595/*
596 * snd_intelhad_prog_dip - to initialize Data Island Packets registers
Jerome Anand5dab11d2017-01-25 04:27:52 +0530597 *
598 * @substream:substream for which the prepare function is called
599 * @intelhaddata:substream private data
600 *
601 * This function is called in the prepare callback
602 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100603static void snd_intelhad_prog_dip(struct snd_pcm_substream *substream,
604 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530605{
606 int i;
607 union aud_ctrl_st ctrl_state = {.ctrl_val = 0};
608 union aud_info_frame2 frame2 = {.fr2_val = 0};
609 union aud_info_frame3 frame3 = {.fr3_val = 0};
610 u8 checksum = 0;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600611 u32 info_frame;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530612 int channels;
613
614 channels = substream->runtime->channels;
615
Takashi Iwai79dda752017-01-30 17:23:39 +0100616 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.ctrl_val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530617
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600618 if (intelhaddata->dp_output) {
619 info_frame = DP_INFO_FRAME_WORD1;
620 frame2.fr2_val = 1;
621 } else {
622 info_frame = HDMI_INFO_FRAME_WORD1;
623 frame2.fr2_regx.chnl_cnt = substream->runtime->channels - 1;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530624
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600625 frame3.fr3_regx.chnl_alloc = snd_intelhad_channel_allocation(
626 intelhaddata, channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530627
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600628 /*Calculte the byte wide checksum for all valid DIP words*/
629 for (i = 0; i < BYTES_PER_WORD; i++)
630 checksum += (info_frame >> i*BITS_PER_BYTE) & MASK_BYTE0;
631 for (i = 0; i < BYTES_PER_WORD; i++)
632 checksum += (frame2.fr2_val >> i*BITS_PER_BYTE) & MASK_BYTE0;
633 for (i = 0; i < BYTES_PER_WORD; i++)
634 checksum += (frame3.fr3_val >> i*BITS_PER_BYTE) & MASK_BYTE0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530635
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600636 frame2.fr2_regx.chksum = -(checksum);
637 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530638
Takashi Iwai79dda752017-01-30 17:23:39 +0100639 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, info_frame);
640 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, frame2.fr2_val);
641 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, frame3.fr3_val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530642
643 /* program remaining DIP words with zero */
644 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
Takashi Iwai79dda752017-01-30 17:23:39 +0100645 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, 0x0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530646
647 ctrl_state.ctrl_regx.dip_freq = 1;
648 ctrl_state.ctrl_regx.dip_en_sta = 1;
Takashi Iwai79dda752017-01-30 17:23:39 +0100649 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.ctrl_val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530650}
651
652/**
653 * snd_intelhad_prog_buffer - programs buffer
654 * address and length registers
655 *
656 * @substream:substream for which the prepare function is called
657 * @intelhaddata:substream private data
658 *
659 * This function programs ring buffer address and length into registers.
660 */
661int snd_intelhad_prog_buffer(struct snd_intelhad *intelhaddata,
662 int start, int end)
663{
664 u32 ring_buf_addr, ring_buf_size, period_bytes;
665 u8 i, num_periods;
666 struct snd_pcm_substream *substream;
667
668 substream = intelhaddata->stream_info.had_substream;
669 if (!substream) {
670 pr_err("substream is NULL\n");
671 dump_stack();
672 return 0;
673 }
674
675 ring_buf_addr = substream->runtime->dma_addr;
676 ring_buf_size = snd_pcm_lib_buffer_bytes(substream);
677 intelhaddata->stream_info.ring_buf_size = ring_buf_size;
678 period_bytes = frames_to_bytes(substream->runtime,
679 substream->runtime->period_size);
680 num_periods = substream->runtime->periods;
681
682 /*
683 * buffer addr should be 64 byte aligned, period bytes
684 * will be used to calculate addr offset
685 */
686 period_bytes &= ~0x3F;
687
688 /* Hardware supports MAX_PERIODS buffers */
689 if (end >= HAD_MAX_PERIODS)
690 return -EINVAL;
691
692 for (i = start; i <= end; i++) {
693 /* Program the buf registers with addr and len */
694 intelhaddata->buf_info[i].buf_addr = ring_buf_addr +
695 (i * period_bytes);
696 if (i < num_periods-1)
697 intelhaddata->buf_info[i].buf_size = period_bytes;
698 else
699 intelhaddata->buf_info[i].buf_size = ring_buf_size -
700 (period_bytes*i);
701
Takashi Iwai79dda752017-01-30 17:23:39 +0100702 had_write_register(intelhaddata,
703 AUD_BUF_A_ADDR + (i * HAD_REG_WIDTH),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530704 intelhaddata->buf_info[i].buf_addr |
705 BIT(0) | BIT(1));
Takashi Iwai79dda752017-01-30 17:23:39 +0100706 had_write_register(intelhaddata,
707 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530708 period_bytes);
709 intelhaddata->buf_info[i].is_valid = true;
710 }
711 pr_debug("%s:buf[%d-%d] addr=%#x and size=%d\n", __func__, start, end,
712 intelhaddata->buf_info[start].buf_addr,
713 intelhaddata->buf_info[start].buf_size);
714 intelhaddata->valid_buf_cnt = num_periods;
715 return 0;
716}
717
718int snd_intelhad_read_len(struct snd_intelhad *intelhaddata)
719{
720 int i, retval = 0;
721 u32 len[4];
722
723 for (i = 0; i < 4 ; i++) {
Takashi Iwai79dda752017-01-30 17:23:39 +0100724 had_read_register(intelhaddata,
725 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
726 &len[i]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530727 if (!len[i])
728 retval++;
729 }
730 if (retval != 1) {
731 for (i = 0; i < 4 ; i++)
732 pr_debug("buf[%d] size=%d\n", i, len[i]);
733 }
734
735 return retval;
736}
737
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600738static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
739{
740 u32 maud_val;
741
742 /* Select maud according to DP 1.2 spec*/
743 if (link_rate == DP_2_7_GHZ) {
744 switch (aud_samp_freq) {
745 case AUD_SAMPLE_RATE_32:
746 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
747 break;
748
749 case AUD_SAMPLE_RATE_44_1:
750 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
751 break;
752
753 case AUD_SAMPLE_RATE_48:
754 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
755 break;
756
757 case AUD_SAMPLE_RATE_88_2:
758 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
759 break;
760
761 case AUD_SAMPLE_RATE_96:
762 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
763 break;
764
765 case AUD_SAMPLE_RATE_176_4:
766 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
767 break;
768
769 case HAD_MAX_RATE:
770 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
771 break;
772
773 default:
774 maud_val = -EINVAL;
775 break;
776 }
777 } else if (link_rate == DP_1_62_GHZ) {
778 switch (aud_samp_freq) {
779 case AUD_SAMPLE_RATE_32:
780 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
781 break;
782
783 case AUD_SAMPLE_RATE_44_1:
784 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
785 break;
786
787 case AUD_SAMPLE_RATE_48:
788 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
789 break;
790
791 case AUD_SAMPLE_RATE_88_2:
792 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
793 break;
794
795 case AUD_SAMPLE_RATE_96:
796 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
797 break;
798
799 case AUD_SAMPLE_RATE_176_4:
800 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
801 break;
802
803 case HAD_MAX_RATE:
804 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
805 break;
806
807 default:
808 maud_val = -EINVAL;
809 break;
810 }
811 } else
812 maud_val = -EINVAL;
813
814 return maud_val;
815}
816
Takashi Iwai76296ef2017-01-30 16:09:11 +0100817/*
818 * snd_intelhad_prog_cts - Program HDMI audio CTS value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530819 *
820 * @aud_samp_freq: sampling frequency of audio data
821 * @tmds: sampling frequency of the display data
822 * @n_param: N value, depends on aud_samp_freq
823 * @intelhaddata:substream private data
824 *
825 * Program CTS register based on the audio and display sampling frequency
826 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100827static void snd_intelhad_prog_cts(u32 aud_samp_freq, u32 tmds,
828 u32 link_rate, u32 n_param,
829 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530830{
831 u32 cts_val;
832 u64 dividend, divisor;
833
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600834 if (intelhaddata->dp_output) {
835 /* Substitute cts_val with Maud according to DP 1.2 spec*/
836 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
837 } else {
838 /* Calculate CTS according to HDMI 1.3a spec*/
839 dividend = (u64)tmds * n_param*1000;
840 divisor = 128 * aud_samp_freq;
841 cts_val = div64_u64(dividend, divisor);
842 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530843 pr_debug("TMDS value=%d, N value=%d, CTS Value=%d\n",
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600844 tmds, n_param, cts_val);
Takashi Iwai79dda752017-01-30 17:23:39 +0100845 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530846}
847
848static int had_calculate_n_value(u32 aud_samp_freq)
849{
850 s32 n_val;
851
852 /* Select N according to HDMI 1.3a spec*/
853 switch (aud_samp_freq) {
854 case AUD_SAMPLE_RATE_32:
855 n_val = 4096;
856 break;
857
858 case AUD_SAMPLE_RATE_44_1:
859 n_val = 6272;
860 break;
861
862 case AUD_SAMPLE_RATE_48:
863 n_val = 6144;
864 break;
865
866 case AUD_SAMPLE_RATE_88_2:
867 n_val = 12544;
868 break;
869
870 case AUD_SAMPLE_RATE_96:
871 n_val = 12288;
872 break;
873
874 case AUD_SAMPLE_RATE_176_4:
875 n_val = 25088;
876 break;
877
878 case HAD_MAX_RATE:
879 n_val = 24576;
880 break;
881
882 default:
883 n_val = -EINVAL;
884 break;
885 }
886 return n_val;
887}
888
Takashi Iwai76296ef2017-01-30 16:09:11 +0100889/*
890 * snd_intelhad_prog_n - Program HDMI audio N value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530891 *
892 * @aud_samp_freq: sampling frequency of audio data
893 * @n_param: N value, depends on aud_samp_freq
894 * @intelhaddata:substream private data
895 *
896 * This function is called in the prepare callback.
897 * It programs based on the audio and display sampling frequency
898 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100899static int snd_intelhad_prog_n(u32 aud_samp_freq, u32 *n_param,
900 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530901{
902 s32 n_val;
903
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600904 if (intelhaddata->dp_output) {
905 /*
906 * According to DP specs, Maud and Naud values hold
907 * a relationship, which is stated as:
908 * Maud/Naud = 512 * fs / f_LS_Clk
909 * where, fs is the sampling frequency of the audio stream
910 * and Naud is 32768 for Async clock.
911 */
912
913 n_val = DP_NAUD_VAL;
914 } else
915 n_val = had_calculate_n_value(aud_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530916
917 if (n_val < 0)
918 return n_val;
919
Takashi Iwai79dda752017-01-30 17:23:39 +0100920 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530921 *n_param = n_val;
922 return 0;
923}
924
Takashi Iwai76296ef2017-01-30 16:09:11 +0100925void snd_intelhad_handle_underrun(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530926{
927 u32 hdmi_status, i = 0;
928
929 /* Handle Underrun interrupt within Audio Unit */
Takashi Iwai79dda752017-01-30 17:23:39 +0100930 had_write_register(intelhaddata, AUD_CONFIG, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530931 /* Reset buffer pointers */
Takashi Iwai79dda752017-01-30 17:23:39 +0100932 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, 1);
933 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530934 /**
935 * The interrupt status 'sticky' bits might not be cleared by
936 * setting '1' to that bit once...
937 */
938 do { /* clear bit30, 31 AUD_HDMI_STATUS */
Takashi Iwai79dda752017-01-30 17:23:39 +0100939 had_read_register(intelhaddata, AUD_HDMI_STATUS_v2,
940 &hdmi_status);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530941 pr_debug("HDMI status =0x%x\n", hdmi_status);
942 if (hdmi_status & AUD_CONFIG_MASK_UNDERRUN) {
943 i++;
Takashi Iwai79dda752017-01-30 17:23:39 +0100944 had_write_register(intelhaddata,
945 AUD_HDMI_STATUS_v2, hdmi_status);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530946 } else
947 break;
948 } while (i < MAX_CNT);
949 if (i >= MAX_CNT)
950 pr_err("Unable to clear UNDERRUN bits\n");
951}
952
953/**
954 * snd_intelhad_open - stream initializations are done here
955 * @substream:substream for which the stream function is called
956 *
957 * This function is called whenever a PCM stream is opened
958 */
959static int snd_intelhad_open(struct snd_pcm_substream *substream)
960{
961 struct snd_intelhad *intelhaddata;
962 struct snd_pcm_runtime *runtime;
963 struct had_stream_pvt *stream;
964 struct had_pvt_data *had_stream;
965 int retval;
966
967 pr_debug("snd_intelhad_open called\n");
968 intelhaddata = snd_pcm_substream_chip(substream);
969 had_stream = intelhaddata->private_data;
970 runtime = substream->runtime;
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +0100971 intelhaddata->underrun_count = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530972
973 pm_runtime_get(intelhaddata->dev);
974
975 if (had_get_hwstate(intelhaddata)) {
976 pr_err("%s: HDMI cable plugged-out\n", __func__);
977 retval = -ENODEV;
978 goto exit_put_handle;
979 }
980
981 /* Check, if device already in use */
982 if (runtime->private_data) {
983 pr_err("Device already in use\n");
984 retval = -EBUSY;
985 goto exit_put_handle;
986 }
987
988 /* set the runtime hw parameter with local snd_pcm_hardware struct */
989 runtime->hw = snd_intel_hadstream;
990
991 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
992 if (!stream) {
993 retval = -ENOMEM;
994 goto exit_put_handle;
995 }
996 stream->stream_status = STREAM_INIT;
997 runtime->private_data = stream;
998
999 retval = snd_pcm_hw_constraint_integer(runtime,
1000 SNDRV_PCM_HW_PARAM_PERIODS);
1001 if (retval < 0)
1002 goto exit_err;
1003
1004 /* Make sure, that the period size is always aligned
1005 * 64byte boundary
1006 */
1007 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1008 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
1009 if (retval < 0) {
1010 pr_err("%s:step_size=64 failed,err=%d\n", __func__, retval);
1011 goto exit_err;
1012 }
1013
1014 return retval;
1015exit_err:
1016 kfree(stream);
1017exit_put_handle:
1018 pm_runtime_put(intelhaddata->dev);
1019 runtime->private_data = NULL;
1020 return retval;
1021}
1022
1023/**
1024 * had_period_elapsed - updates the hardware pointer status
1025 * @had_substream:substream for which the stream function is called
1026 *
1027 */
1028static void had_period_elapsed(void *had_substream)
1029{
1030 struct snd_pcm_substream *substream = had_substream;
1031 struct had_stream_pvt *stream;
1032
1033 /* pr_debug("had_period_elapsed called\n"); */
1034
1035 if (!substream || !substream->runtime)
1036 return;
1037 stream = substream->runtime->private_data;
1038 if (!stream)
1039 return;
1040
1041 if (stream->stream_status != STREAM_RUNNING)
1042 return;
1043 snd_pcm_period_elapsed(substream);
1044}
1045
1046/**
1047 * snd_intelhad_init_stream - internal function to initialize stream info
1048 * @substream:substream for which the stream function is called
1049 *
1050 */
1051static int snd_intelhad_init_stream(struct snd_pcm_substream *substream)
1052{
1053 struct snd_intelhad *intelhaddata = snd_pcm_substream_chip(substream);
1054
1055 pr_debug("snd_intelhad_init_stream called\n");
1056
1057 pr_debug("setting buffer ptr param\n");
1058 intelhaddata->stream_info.period_elapsed = had_period_elapsed;
1059 intelhaddata->stream_info.had_substream = substream;
1060 intelhaddata->stream_info.buffer_ptr = 0;
1061 intelhaddata->stream_info.buffer_rendered = 0;
1062 intelhaddata->stream_info.sfreq = substream->runtime->rate;
1063 return 0;
1064}
1065
1066/**
1067 * snd_intelhad_close- to free parameteres when stream is stopped
1068 *
1069 * @substream: substream for which the function is called
1070 *
1071 * This function is called by ALSA framework when stream is stopped
1072 */
1073static int snd_intelhad_close(struct snd_pcm_substream *substream)
1074{
1075 struct snd_intelhad *intelhaddata;
1076 struct snd_pcm_runtime *runtime;
1077
1078 pr_debug("snd_intelhad_close called\n");
1079
1080 intelhaddata = snd_pcm_substream_chip(substream);
1081 runtime = substream->runtime;
1082
1083 if (!runtime->private_data) {
1084 pr_debug("close() might have called after failed open");
1085 return 0;
1086 }
1087
1088 intelhaddata->stream_info.buffer_rendered = 0;
1089 intelhaddata->stream_info.buffer_ptr = 0;
1090 intelhaddata->stream_info.str_id = 0;
1091 intelhaddata->stream_info.had_substream = NULL;
1092
1093 /* Check if following drv_status modification is required - VA */
1094 if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) {
1095 intelhaddata->drv_status = HAD_DRV_CONNECTED;
1096 pr_debug("%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
1097 __func__, __LINE__);
1098 }
1099 kfree(runtime->private_data);
1100 runtime->private_data = NULL;
1101 pm_runtime_put(intelhaddata->dev);
1102 return 0;
1103}
1104
1105/**
1106 * snd_intelhad_hw_params- to setup the hardware parameters
1107 * like allocating the buffers
1108 *
1109 * @substream: substream for which the function is called
1110 * @hw_params: hardware parameters
1111 *
1112 * This function is called by ALSA framework when hardware params are set
1113 */
1114static int snd_intelhad_hw_params(struct snd_pcm_substream *substream,
1115 struct snd_pcm_hw_params *hw_params)
1116{
1117 unsigned long addr;
1118 int pages, buf_size, retval;
1119
1120 pr_debug("snd_intelhad_hw_params called\n");
1121
1122 if (!hw_params)
1123 return -EINVAL;
1124
1125 buf_size = params_buffer_bytes(hw_params);
1126 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1127 if (retval < 0)
1128 return retval;
1129 pr_debug("%s:allocated memory = %d\n", __func__, buf_size);
1130 /* mark the pages as uncached region */
1131 addr = (unsigned long) substream->runtime->dma_area;
1132 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1133 retval = set_memory_uc(addr, pages);
1134 if (retval) {
1135 pr_err("set_memory_uc failed.Error:%d\n", retval);
1136 return retval;
1137 }
1138 memset(substream->runtime->dma_area, 0, buf_size);
1139
1140 return retval;
1141}
1142
1143/**
1144 * snd_intelhad_hw_free- to release the resources allocated during
1145 * hardware params setup
1146 *
1147 * @substream: substream for which the function is called
1148 *
1149 * This function is called by ALSA framework before close callback.
1150 *
1151 */
1152static int snd_intelhad_hw_free(struct snd_pcm_substream *substream)
1153{
1154 unsigned long addr;
1155 u32 pages;
1156
1157 pr_debug("snd_intelhad_hw_free called\n");
1158
1159 /* mark back the pages as cached/writeback region before the free */
1160 if (substream->runtime->dma_area != NULL) {
1161 addr = (unsigned long) substream->runtime->dma_area;
1162 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1163 PAGE_SIZE;
1164 set_memory_wb(addr, pages);
1165 return snd_pcm_lib_free_pages(substream);
1166 }
1167 return 0;
1168}
1169
1170/**
1171 * snd_intelhad_pcm_trigger - stream activities are handled here
1172 * @substream:substream for which the stream function is called
1173 * @cmd:the stream commamd thats requested from upper layer
1174 * This function is called whenever an a stream activity is invoked
1175 */
1176static int snd_intelhad_pcm_trigger(struct snd_pcm_substream *substream,
1177 int cmd)
1178{
1179 int caps, retval = 0;
1180 unsigned long flag_irq;
1181 struct snd_intelhad *intelhaddata;
1182 struct had_stream_pvt *stream;
1183 struct had_pvt_data *had_stream;
1184
1185 pr_debug("snd_intelhad_pcm_trigger called\n");
1186
1187 intelhaddata = snd_pcm_substream_chip(substream);
1188 stream = substream->runtime->private_data;
1189 had_stream = intelhaddata->private_data;
1190
1191 switch (cmd) {
1192 case SNDRV_PCM_TRIGGER_START:
1193 pr_debug("Trigger Start\n");
1194
1195 /* Disable local INTRs till register prgmng is done */
1196 if (had_get_hwstate(intelhaddata)) {
1197 pr_err("_START: HDMI cable plugged-out\n");
1198 retval = -ENODEV;
1199 break;
1200 }
1201 stream->stream_status = STREAM_RUNNING;
1202
1203 had_stream->stream_type = HAD_RUNNING_STREAM;
1204
1205 /* Enable Audio */
1206 /*
1207 * ToDo: Need to enable UNDERRUN interrupts as well
1208 * caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
1209 */
1210 caps = HDMI_AUDIO_BUFFER_DONE;
Takashi Iwai79dda752017-01-30 17:23:39 +01001211 retval = had_set_caps(intelhaddata, HAD_SET_ENABLE_AUDIO_INT,
1212 &caps);
1213 retval = had_set_caps(intelhaddata, HAD_SET_ENABLE_AUDIO, NULL);
Takashi Iwai76296ef2017-01-30 16:09:11 +01001214 snd_intelhad_enable_audio(substream, 1);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301215
1216 pr_debug("Processed _Start\n");
1217
1218 break;
1219
1220 case SNDRV_PCM_TRIGGER_STOP:
1221 pr_debug("Trigger Stop\n");
1222 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irq);
1223 intelhaddata->stream_info.str_id = 0;
1224 intelhaddata->curr_buf = 0;
1225
1226 /* Stop reporting BUFFER_DONE/UNDERRUN to above layers*/
1227
1228 had_stream->stream_type = HAD_INIT;
1229 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irq);
1230 /* Disable Audio */
1231 /*
1232 * ToDo: Need to disable UNDERRUN interrupts as well
1233 * caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
1234 */
1235 caps = HDMI_AUDIO_BUFFER_DONE;
Takashi Iwai79dda752017-01-30 17:23:39 +01001236 had_set_caps(intelhaddata, HAD_SET_DISABLE_AUDIO_INT, &caps);
Takashi Iwai76296ef2017-01-30 16:09:11 +01001237 snd_intelhad_enable_audio(substream, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301238 /* Reset buffer pointers */
Takashi Iwai79dda752017-01-30 17:23:39 +01001239 snd_intelhad_reset_audio(intelhaddata, 1);
1240 snd_intelhad_reset_audio(intelhaddata, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301241 stream->stream_status = STREAM_DROPPED;
Takashi Iwai79dda752017-01-30 17:23:39 +01001242 had_set_caps(intelhaddata, HAD_SET_DISABLE_AUDIO, NULL);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301243 break;
1244
1245 default:
1246 retval = -EINVAL;
1247 }
1248 return retval;
1249}
1250
1251/**
1252 * snd_intelhad_pcm_prepare- internal preparation before starting a stream
1253 *
1254 * @substream: substream for which the function is called
1255 *
1256 * This function is called when a stream is started for internal preparation.
1257 */
1258static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream)
1259{
1260 int retval;
1261 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001262 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301263 struct snd_intelhad *intelhaddata;
1264 struct snd_pcm_runtime *runtime;
1265 struct had_pvt_data *had_stream;
1266
1267 pr_debug("snd_intelhad_pcm_prepare called\n");
1268
1269 intelhaddata = snd_pcm_substream_chip(substream);
1270 runtime = substream->runtime;
1271 had_stream = intelhaddata->private_data;
1272
1273 if (had_get_hwstate(intelhaddata)) {
1274 pr_err("%s: HDMI cable plugged-out\n", __func__);
1275 retval = -ENODEV;
1276 goto prep_end;
1277 }
1278
1279 pr_debug("period_size=%d\n",
1280 (int)frames_to_bytes(runtime, runtime->period_size));
1281 pr_debug("periods=%d\n", runtime->periods);
1282 pr_debug("buffer_size=%d\n", (int)snd_pcm_lib_buffer_bytes(substream));
1283 pr_debug("rate=%d\n", runtime->rate);
1284 pr_debug("channels=%d\n", runtime->channels);
1285
1286 if (intelhaddata->stream_info.str_id) {
1287 pr_debug("_prepare is called for existing str_id#%d\n",
1288 intelhaddata->stream_info.str_id);
1289 retval = snd_intelhad_pcm_trigger(substream,
1290 SNDRV_PCM_TRIGGER_STOP);
1291 return retval;
1292 }
1293
1294 retval = snd_intelhad_init_stream(substream);
1295 if (retval)
1296 goto prep_end;
1297
1298
1299 /* Get N value in KHz */
Takashi Iwai79dda752017-01-30 17:23:39 +01001300 retval = had_get_caps(intelhaddata, HAD_GET_DISPLAY_RATE,
1301 &disp_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301302 if (retval) {
1303 pr_err("querying display sampling freq failed %#x\n", retval);
1304 goto prep_end;
1305 }
1306
Takashi Iwai79dda752017-01-30 17:23:39 +01001307 had_get_caps(intelhaddata, HAD_GET_ELD, &intelhaddata->eeld);
1308 had_get_caps(intelhaddata, HAD_GET_DP_OUTPUT, &intelhaddata->dp_output);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301309
Takashi Iwai76296ef2017-01-30 16:09:11 +01001310 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1311 intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301312 if (retval) {
1313 pr_err("programming N value failed %#x\n", retval);
1314 goto prep_end;
1315 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001316
1317 if (intelhaddata->dp_output)
Takashi Iwai79dda752017-01-30 17:23:39 +01001318 had_get_caps(intelhaddata, HAD_GET_LINK_RATE, &link_rate);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001319
1320
Takashi Iwai76296ef2017-01-30 16:09:11 +01001321 snd_intelhad_prog_cts(substream->runtime->rate,
1322 disp_samp_freq, link_rate,
1323 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301324
Takashi Iwai76296ef2017-01-30 16:09:11 +01001325 snd_intelhad_prog_dip(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301326
Takashi Iwai76296ef2017-01-30 16:09:11 +01001327 retval = snd_intelhad_audio_ctrl(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301328
1329 /* Prog buffer address */
1330 retval = snd_intelhad_prog_buffer(intelhaddata,
1331 HAD_BUF_TYPE_A, HAD_BUF_TYPE_D);
1332
1333 /*
1334 * Program channel mapping in following order:
1335 * FL, FR, C, LFE, RL, RR
1336 */
1337
Takashi Iwai79dda752017-01-30 17:23:39 +01001338 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301339
1340prep_end:
1341 return retval;
1342}
1343
1344/**
1345 * snd_intelhad_pcm_pointer- to send the current buffer pointerprocessed by hw
1346 *
1347 * @substream: substream for which the function is called
1348 *
1349 * This function is called by ALSA framework to get the current hw buffer ptr
1350 * when a period is elapsed
1351 */
1352static snd_pcm_uframes_t snd_intelhad_pcm_pointer(
1353 struct snd_pcm_substream *substream)
1354{
1355 struct snd_intelhad *intelhaddata;
1356 u32 bytes_rendered = 0;
1357 u32 t;
1358 int buf_id;
1359
1360 /* pr_debug("snd_intelhad_pcm_pointer called\n"); */
1361
1362 intelhaddata = snd_pcm_substream_chip(substream);
1363
1364 if (intelhaddata->flag_underrun) {
1365 intelhaddata->flag_underrun = 0;
1366 return SNDRV_PCM_POS_XRUN;
1367 }
1368
1369 /* Use a hw register to calculate sub-period position reports.
1370 * This makes PulseAudio happier.
1371 */
1372
1373 buf_id = intelhaddata->curr_buf % 4;
Takashi Iwai79dda752017-01-30 17:23:39 +01001374 had_read_register(intelhaddata,
1375 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH), &t);
Jerome Anand232892f2017-01-25 04:27:53 +05301376
1377 if ((t == 0) || (t == ((u32)-1L))) {
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001378 intelhaddata->underrun_count++;
Jerome Anand232892f2017-01-25 04:27:53 +05301379 pr_debug("discovered buffer done for buf %d, count = %d\n",
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001380 buf_id, intelhaddata->underrun_count);
Jerome Anand232892f2017-01-25 04:27:53 +05301381
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001382 if (intelhaddata->underrun_count > (HAD_MIN_PERIODS/2)) {
Jerome Anand232892f2017-01-25 04:27:53 +05301383 pr_debug("assume audio_codec_reset, underrun = %d - do xrun\n",
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001384 intelhaddata->underrun_count);
1385 intelhaddata->underrun_count = 0;
Jerome Anand232892f2017-01-25 04:27:53 +05301386 return SNDRV_PCM_POS_XRUN;
1387 }
1388 } else {
1389 /* Reset Counter */
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001390 intelhaddata->underrun_count = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301391 }
Jerome Anand232892f2017-01-25 04:27:53 +05301392
Jerome Anand5dab11d2017-01-25 04:27:52 +05301393 t = intelhaddata->buf_info[buf_id].buf_size - t;
1394
1395 if (intelhaddata->stream_info.buffer_rendered)
1396 div_u64_rem(intelhaddata->stream_info.buffer_rendered,
1397 intelhaddata->stream_info.ring_buf_size,
1398 &(bytes_rendered));
1399
1400 intelhaddata->stream_info.buffer_ptr = bytes_to_frames(
1401 substream->runtime,
1402 bytes_rendered + t);
1403 return intelhaddata->stream_info.buffer_ptr;
1404}
1405
1406/**
1407 * snd_intelhad_pcm_mmap- mmaps a kernel buffer to user space for copying data
1408 *
1409 * @substream: substream for which the function is called
1410 * @vma: struct instance of memory VMM memory area
1411 *
1412 * This function is called by OS when a user space component
1413 * tries to get mmap memory from driver
1414 */
1415static int snd_intelhad_pcm_mmap(struct snd_pcm_substream *substream,
1416 struct vm_area_struct *vma)
1417{
1418
1419 pr_debug("snd_intelhad_pcm_mmap called\n");
1420
1421 pr_debug("entry with prot:%s\n", __func__);
1422 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1423 return remap_pfn_range(vma, vma->vm_start,
1424 substream->dma_buffer.addr >> PAGE_SHIFT,
1425 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1426}
1427
1428int hdmi_audio_mode_change(struct snd_pcm_substream *substream)
1429{
1430 int retval = 0;
1431 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001432 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301433 struct snd_intelhad *intelhaddata;
1434
1435 intelhaddata = snd_pcm_substream_chip(substream);
1436
1437 /* Disable Audio */
Takashi Iwai76296ef2017-01-30 16:09:11 +01001438 snd_intelhad_enable_audio(substream, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301439
1440 /* Update CTS value */
Takashi Iwai79dda752017-01-30 17:23:39 +01001441 retval = had_get_caps(intelhaddata, HAD_GET_DISPLAY_RATE,
1442 &disp_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301443 if (retval) {
1444 pr_err("querying display sampling freq failed %#x\n", retval);
1445 goto out;
1446 }
1447
Takashi Iwai76296ef2017-01-30 16:09:11 +01001448 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1449 intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301450 if (retval) {
1451 pr_err("programming N value failed %#x\n", retval);
1452 goto out;
1453 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001454
1455 if (intelhaddata->dp_output)
Takashi Iwai79dda752017-01-30 17:23:39 +01001456 had_get_caps(intelhaddata, HAD_GET_LINK_RATE, &link_rate);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001457
Takashi Iwai76296ef2017-01-30 16:09:11 +01001458 snd_intelhad_prog_cts(substream->runtime->rate,
1459 disp_samp_freq, link_rate,
1460 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301461
1462 /* Enable Audio */
Takashi Iwai76296ef2017-01-30 16:09:11 +01001463 snd_intelhad_enable_audio(substream, 1);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301464
1465out:
1466 return retval;
1467}
1468
1469/*PCM operations structure and the calls back for the same */
1470struct snd_pcm_ops snd_intelhad_playback_ops = {
1471 .open = snd_intelhad_open,
1472 .close = snd_intelhad_close,
1473 .ioctl = snd_pcm_lib_ioctl,
1474 .hw_params = snd_intelhad_hw_params,
1475 .hw_free = snd_intelhad_hw_free,
1476 .prepare = snd_intelhad_pcm_prepare,
1477 .trigger = snd_intelhad_pcm_trigger,
1478 .pointer = snd_intelhad_pcm_pointer,
1479 .mmap = snd_intelhad_pcm_mmap,
1480};
1481
1482/**
1483 * snd_intelhad_create - to crete alsa card instance
1484 *
1485 * @intelhaddata: pointer to internal context
1486 * @card: pointer to card
1487 *
1488 * This function is called when the hdmi cable is plugged in
1489 */
1490static int snd_intelhad_create(
1491 struct snd_intelhad *intelhaddata,
1492 struct snd_card *card)
1493{
1494 int retval;
1495 static struct snd_device_ops ops = {
1496 };
1497
1498 pr_debug("snd_intelhad_create called\n");
1499
1500 if (!intelhaddata)
1501 return -EINVAL;
1502
1503 /* ALSA api to register the device */
1504 retval = snd_device_new(card, SNDRV_DEV_LOWLEVEL, intelhaddata, &ops);
1505 return retval;
1506}
1507/**
1508 * snd_intelhad_pcm_free - to free the memory allocated
1509 *
1510 * @pcm: pointer to pcm instance
1511 * This function is called when the device is removed
1512 */
1513static void snd_intelhad_pcm_free(struct snd_pcm *pcm)
1514{
1515 pr_debug("Freeing PCM preallocated pages\n");
1516 snd_pcm_lib_preallocate_free_for_all(pcm);
1517}
1518
1519static int had_iec958_info(struct snd_kcontrol *kcontrol,
1520 struct snd_ctl_elem_info *uinfo)
1521{
1522 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1523 uinfo->count = 1;
1524 return 0;
1525}
1526
1527static int had_iec958_get(struct snd_kcontrol *kcontrol,
1528 struct snd_ctl_elem_value *ucontrol)
1529{
1530 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1531
1532 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1533 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1534 ucontrol->value.iec958.status[2] =
1535 (intelhaddata->aes_bits >> 16) & 0xff;
1536 ucontrol->value.iec958.status[3] =
1537 (intelhaddata->aes_bits >> 24) & 0xff;
1538 return 0;
1539}
1540static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1541 struct snd_ctl_elem_value *ucontrol)
1542{
1543 ucontrol->value.iec958.status[0] = 0xff;
1544 ucontrol->value.iec958.status[1] = 0xff;
1545 ucontrol->value.iec958.status[2] = 0xff;
1546 ucontrol->value.iec958.status[3] = 0xff;
1547 return 0;
1548}
1549static int had_iec958_put(struct snd_kcontrol *kcontrol,
1550 struct snd_ctl_elem_value *ucontrol)
1551{
1552 unsigned int val;
1553 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1554
1555 pr_debug("entered had_iec958_put\n");
1556 val = (ucontrol->value.iec958.status[0] << 0) |
1557 (ucontrol->value.iec958.status[1] << 8) |
1558 (ucontrol->value.iec958.status[2] << 16) |
1559 (ucontrol->value.iec958.status[3] << 24);
1560 if (intelhaddata->aes_bits != val) {
1561 intelhaddata->aes_bits = val;
1562 return 1;
1563 }
1564 return 1;
1565}
1566
1567static struct snd_kcontrol_new had_control_iec958_mask = {
1568 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1569 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1570 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1571 .info = had_iec958_info, /* shared */
1572 .get = had_iec958_mask_get,
1573};
1574
1575static struct snd_kcontrol_new had_control_iec958 = {
1576 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1577 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1578 .info = had_iec958_info,
1579 .get = had_iec958_get,
1580 .put = had_iec958_put
1581};
1582
Takashi Iwai79dda752017-01-30 17:23:39 +01001583/*
Jerome Anand5dab11d2017-01-25 04:27:52 +05301584 * hdmi_audio_probe - to create sound card instance for HDMI audio playabck
1585 *
Takashi Iwai79dda752017-01-30 17:23:39 +01001586 * @devptr: platform device
1587 * @had_ret: pointer to store the created snd_intelhad object
Jerome Anand5dab11d2017-01-25 04:27:52 +05301588 *
Takashi Iwai79dda752017-01-30 17:23:39 +01001589 * This function is called when the platform device is probed. This function
Jerome Anand5dab11d2017-01-25 04:27:52 +05301590 * creates and registers the sound card with ALSA
1591 */
Takashi Iwai79dda752017-01-30 17:23:39 +01001592int hdmi_audio_probe(struct platform_device *devptr,
1593 struct snd_intelhad **had_ret)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301594{
1595 int retval;
1596 struct snd_pcm *pcm;
1597 struct snd_card *card;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301598 struct snd_intelhad *intelhaddata;
1599 struct had_pvt_data *had_stream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301600
1601 pr_debug("Enter %s\n", __func__);
1602
1603 pr_debug("hdmi_audio_probe dma_mask: %p\n", devptr->dev.dma_mask);
1604
1605 /* allocate memory for saving internal context and working */
1606 intelhaddata = kzalloc(sizeof(*intelhaddata), GFP_KERNEL);
1607 if (!intelhaddata)
1608 return -ENOMEM;
1609
1610 had_stream = kzalloc(sizeof(*had_stream), GFP_KERNEL);
1611 if (!had_stream) {
1612 retval = -ENOMEM;
1613 goto free_haddata;
1614 }
1615
Jerome Anand5dab11d2017-01-25 04:27:52 +05301616 spin_lock_init(&intelhaddata->had_spinlock);
1617 intelhaddata->drv_status = HAD_DRV_DISCONNECTED;
1618 pr_debug("%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
1619 __func__, __LINE__);
1620
1621 /* create a card instance with ALSA framework */
1622 retval = snd_card_new(&devptr->dev, hdmi_card_index, hdmi_card_id,
1623 THIS_MODULE, 0, &card);
1624
1625 if (retval)
Takashi Iwaidd895f22017-01-30 17:31:29 +01001626 goto free_hadstream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301627 intelhaddata->card = card;
1628 intelhaddata->card_id = hdmi_card_id;
1629 intelhaddata->card_index = card->number;
1630 intelhaddata->private_data = had_stream;
1631 intelhaddata->flag_underrun = 0;
1632 intelhaddata->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
1633 strncpy(card->driver, INTEL_HAD, strlen(INTEL_HAD));
1634 strncpy(card->shortname, INTEL_HAD, strlen(INTEL_HAD));
1635
1636 retval = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
1637 MAX_CAP_STREAMS, &pcm);
1638 if (retval)
1639 goto err;
1640
1641 /* setup private data which can be retrieved when required */
1642 pcm->private_data = intelhaddata;
1643 pcm->private_free = snd_intelhad_pcm_free;
1644 pcm->info_flags = 0;
1645 strncpy(pcm->name, card->shortname, strlen(card->shortname));
1646 /* setup the ops for palyabck */
1647 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1648 &snd_intelhad_playback_ops);
1649 /* allocate dma pages for ALSA stream operations
1650 * memory allocated is based on size, not max value
1651 * thus using same argument for max & size
1652 */
1653 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
1654 SNDRV_DMA_TYPE_DEV, NULL,
1655 HAD_MAX_BUFFER, HAD_MAX_BUFFER);
1656
1657 if (card->dev == NULL)
1658 pr_debug("card->dev is NULL!!!!! Should not be this case\n");
1659 else if (card->dev->dma_mask == NULL)
1660 pr_debug("hdmi_audio_probe dma_mask is NULL!!!!!\n");
1661 else
1662 pr_debug("hdmi_audio_probe dma_mask is : %p\n",
1663 card->dev->dma_mask);
1664
1665 if (retval)
1666 goto err;
1667
1668 /* internal function call to register device with ALSA */
1669 retval = snd_intelhad_create(intelhaddata, card);
1670 if (retval)
1671 goto err;
1672
1673 card->private_data = &intelhaddata;
1674 retval = snd_card_register(card);
1675 if (retval)
1676 goto err;
1677
1678 /* IEC958 controls */
1679 retval = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958_mask,
1680 intelhaddata));
1681 if (retval < 0)
1682 goto err;
1683 retval = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958,
1684 intelhaddata));
1685 if (retval < 0)
1686 goto err;
1687
1688 init_channel_allocations();
1689
1690 /* Register channel map controls */
1691 retval = had_register_chmap_ctls(intelhaddata, pcm);
1692 if (retval < 0)
1693 goto err;
1694
1695 intelhaddata->dev = &devptr->dev;
1696 pm_runtime_set_active(intelhaddata->dev);
1697 pm_runtime_enable(intelhaddata->dev);
1698
Jerome Anand5dab11d2017-01-25 04:27:52 +05301699 intelhaddata->hw_silence = 1;
Takashi Iwai79dda752017-01-30 17:23:39 +01001700 *had_ret = intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301701
Takashi Iwai79dda752017-01-30 17:23:39 +01001702 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301703err:
1704 snd_card_free(card);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301705free_hadstream:
1706 kfree(had_stream);
1707 pm_runtime_disable(intelhaddata->dev);
1708 intelhaddata->dev = NULL;
1709free_haddata:
1710 kfree(intelhaddata);
1711 intelhaddata = NULL;
1712 pr_err("Error returned from %s api %#x\n", __func__, retval);
1713 return retval;
1714}
1715
Takashi Iwai79dda752017-01-30 17:23:39 +01001716/*
Jerome Anand5dab11d2017-01-25 04:27:52 +05301717 * hdmi_audio_remove - removes the alsa card
1718 *
1719 *@haddata: pointer to HAD private data
1720 *
1721 * This function is called when the hdmi cable is un-plugged. This function
1722 * free the sound card.
1723 */
Takashi Iwai79dda752017-01-30 17:23:39 +01001724int hdmi_audio_remove(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301725{
Jerome Anand5dab11d2017-01-25 04:27:52 +05301726 int caps;
1727
1728 pr_debug("Enter %s\n", __func__);
1729
1730 if (!intelhaddata)
1731 return 0;
1732
1733 if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) {
1734 caps = HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE;
Takashi Iwai79dda752017-01-30 17:23:39 +01001735 had_set_caps(intelhaddata, HAD_SET_DISABLE_AUDIO_INT, &caps);
1736 had_set_caps(intelhaddata, HAD_SET_DISABLE_AUDIO, NULL);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301737 }
1738 snd_card_free(intelhaddata->card);
1739 kfree(intelhaddata->private_data);
1740 kfree(intelhaddata);
1741 return 0;
1742}
1743
1744MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1745MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1746MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1747MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1748MODULE_DESCRIPTION("Intel HDMI Audio driver");
1749MODULE_LICENSE("GPL v2");
1750MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");