blob: ab199b5deaa5dee5d4bdc4c1fb1c2b528a3901f2 [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
Jerome Anand5dab11d2017-01-25 04:27:52 +053024#include <linux/platform_device.h>
25#include <linux/io.h>
26#include <linux/slab.h>
27#include <linux/module.h>
Takashi Iwaida864802017-01-31 13:52:22 +010028#include <linux/interrupt.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053029#include <linux/acpi.h>
30#include <asm/cacheflush.h>
31#include <sound/pcm.h>
32#include <sound/core.h>
33#include <sound/pcm_params.h>
34#include <sound/initval.h>
35#include <sound/control.h>
36#include <sound/initval.h>
Takashi Iwaida864802017-01-31 13:52:22 +010037#include <drm/intel_lpe_audio.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053038#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 */
Takashi Iwai372d8552017-01-31 13:57:58 +0100158static int had_get_hwstate(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530159{
160 /* Check for device presence -SW state */
Takashi Iwaic75b0472017-01-31 15:49:15 +0100161 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530162 return -ENODEV;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530163
164 return 0;
165}
166
Takashi Iwaida864802017-01-31 13:52:22 +0100167static inline void
168mid_hdmi_audio_read(struct snd_intelhad *ctx, u32 reg, u32 *val)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530169{
Takashi Iwaida864802017-01-31 13:52:22 +0100170 *val = ioread32(ctx->mmio_start + ctx->had_config_offset + reg);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530171}
172
Takashi Iwaida864802017-01-31 13:52:22 +0100173static inline void
174mid_hdmi_audio_write(struct snd_intelhad *ctx, u32 reg, u32 val)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530175{
Takashi Iwaida864802017-01-31 13:52:22 +0100176 iowrite32(val, ctx->mmio_start + ctx->had_config_offset + reg);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530177}
178
Takashi Iwai372d8552017-01-31 13:57:58 +0100179static int had_read_register(struct snd_intelhad *intelhaddata,
180 u32 offset, u32 *data)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530181{
182 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530183
184 retval = had_get_hwstate(intelhaddata);
Takashi Iwaida864802017-01-31 13:52:22 +0100185 if (retval)
186 return retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530187
Takashi Iwaida864802017-01-31 13:52:22 +0100188 mid_hdmi_audio_read(intelhaddata, offset, data);
189 return 0;
190}
191
192static void fixup_dp_config(struct snd_intelhad *intelhaddata,
193 u32 offset, u32 *data)
194{
195 if (intelhaddata->dp_output) {
196 if (offset == AUD_CONFIG && (*data & AUD_CONFIG_VALID_BIT))
197 *data |= AUD_CONFIG_DP_MODE | AUD_CONFIG_BLOCK_BIT;
198 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530199}
200
Takashi Iwai372d8552017-01-31 13:57:58 +0100201static int had_write_register(struct snd_intelhad *intelhaddata,
202 u32 offset, u32 data)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530203{
204 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530205
206 retval = had_get_hwstate(intelhaddata);
Takashi Iwaida864802017-01-31 13:52:22 +0100207 if (retval)
208 return retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530209
Takashi Iwaida864802017-01-31 13:52:22 +0100210 fixup_dp_config(intelhaddata, offset, &data);
211 mid_hdmi_audio_write(intelhaddata, offset, data);
212 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530213}
214
Takashi Iwai372d8552017-01-31 13:57:58 +0100215static int had_read_modify(struct snd_intelhad *intelhaddata, u32 offset,
216 u32 data, u32 mask)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530217{
Takashi Iwaida864802017-01-31 13:52:22 +0100218 u32 val_tmp;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530219 int retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530220
221 retval = had_get_hwstate(intelhaddata);
Takashi Iwaida864802017-01-31 13:52:22 +0100222 if (retval)
223 return retval;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530224
Takashi Iwaida864802017-01-31 13:52:22 +0100225 mid_hdmi_audio_read(intelhaddata, offset, &val_tmp);
226 val_tmp &= ~mask;
227 val_tmp |= (data & mask);
228
229 fixup_dp_config(intelhaddata, offset, &val_tmp);
230 mid_hdmi_audio_write(intelhaddata, offset, val_tmp);
231 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530232}
Takashi Iwaida864802017-01-31 13:52:22 +0100233
234/*
235 * function to read-modify AUD_CONFIG register on VLV2.
236 * The had_read_modify() function should not directly be used on VLV2 for
237 * updating AUD_CONFIG register.
Jerome Anand5dab11d2017-01-25 04:27:52 +0530238 * This is because:
239 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
240 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
241 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
242 * register. This field should be 1xy binary for configuration with 6 or
243 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
244 * causes the "channels" field to be updated as 0xy binary resulting in
245 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
246 * appropriate value when doing read-modify of AUD_CONFIG register.
247 *
248 * @substream: the current substream or NULL if no active substream
249 * @data : data to be written
250 * @mask : mask
251 *
252 */
Takashi Iwaida864802017-01-31 13:52:22 +0100253static int had_read_modify_aud_config_v2(struct snd_intelhad *intelhaddata,
Jerome Anand5dab11d2017-01-25 04:27:52 +0530254 u32 data, u32 mask)
255{
Takashi Iwaida864802017-01-31 13:52:22 +0100256 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530257 union aud_cfg cfg_val = {.cfg_regval = 0};
258 u8 channels;
259
260 /*
261 * If substream is NULL, there is no active stream.
262 * In this case just set channels to 2
263 */
Takashi Iwaida864802017-01-31 13:52:22 +0100264 substream = intelhaddata->stream_info.had_substream;
265 if (substream && substream->runtime)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530266 channels = substream->runtime->channels;
267 else
268 channels = 2;
269 cfg_val.cfg_regx_v2.num_ch = channels - 2;
270
271 data = data | cfg_val.cfg_regval;
272 mask = mask | AUD_CONFIG_CH_MASK_V2;
273
Takashi Iwaic75b0472017-01-31 15:49:15 +0100274 dev_dbg(intelhaddata->dev, "%s : data = %x, mask =%x\n",
275 __func__, data, mask);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530276
Takashi Iwai79dda752017-01-30 17:23:39 +0100277 return had_read_modify(intelhaddata, AUD_CONFIG, data, mask);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530278}
279
Takashi Iwai372d8552017-01-31 13:57:58 +0100280static void snd_intelhad_enable_audio_int(struct snd_intelhad *ctx, bool enable)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530281{
Takashi Iwaida864802017-01-31 13:52:22 +0100282 u32 status_reg;
283
284 if (enable) {
285 mid_hdmi_audio_read(ctx, AUD_HDMI_STATUS_v2, &status_reg);
286 status_reg |= HDMI_AUDIO_BUFFER_DONE | HDMI_AUDIO_UNDERRUN;
287 mid_hdmi_audio_write(ctx, AUD_HDMI_STATUS_v2, status_reg);
288 mid_hdmi_audio_read(ctx, AUD_HDMI_STATUS_v2, &status_reg);
289 }
290}
291
Takashi Iwai372d8552017-01-31 13:57:58 +0100292static void snd_intelhad_enable_audio(struct snd_intelhad *intelhaddata,
293 bool enable)
Takashi Iwaida864802017-01-31 13:52:22 +0100294{
295 had_read_modify_aud_config_v2(intelhaddata, enable ? BIT(0) : 0,
296 BIT(0));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530297}
298
Takashi Iwai79dda752017-01-30 17:23:39 +0100299static void snd_intelhad_reset_audio(struct snd_intelhad *intelhaddata,
300 u8 reset)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530301{
Takashi Iwai79dda752017-01-30 17:23:39 +0100302 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, reset);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530303}
304
305/**
306 * initialize audio channel status registers
307 * This function is called in the prepare callback
308 */
309static int had_prog_status_reg(struct snd_pcm_substream *substream,
310 struct snd_intelhad *intelhaddata)
311{
312 union aud_cfg cfg_val = {.cfg_regval = 0};
313 union aud_ch_status_0 ch_stat0 = {.status_0_regval = 0};
314 union aud_ch_status_1 ch_stat1 = {.status_1_regval = 0};
315 int format;
316
Jerome Anand5dab11d2017-01-25 04:27:52 +0530317 ch_stat0.status_0_regx.lpcm_id = (intelhaddata->aes_bits &
318 IEC958_AES0_NONAUDIO)>>1;
319 ch_stat0.status_0_regx.clk_acc = (intelhaddata->aes_bits &
320 IEC958_AES3_CON_CLOCK)>>4;
Takashi Iwai4812dcc2017-01-30 15:58:15 +0100321 cfg_val.cfg_regx_v2.val_bit = ch_stat0.status_0_regx.lpcm_id;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530322
323 switch (substream->runtime->rate) {
324 case AUD_SAMPLE_RATE_32:
325 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_32KHZ;
326 break;
327
328 case AUD_SAMPLE_RATE_44_1:
329 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_44KHZ;
330 break;
331 case AUD_SAMPLE_RATE_48:
332 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_48KHZ;
333 break;
334 case AUD_SAMPLE_RATE_88_2:
335 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_88KHZ;
336 break;
337 case AUD_SAMPLE_RATE_96:
338 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_96KHZ;
339 break;
340 case AUD_SAMPLE_RATE_176_4:
341 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_176KHZ;
342 break;
343 case AUD_SAMPLE_RATE_192:
344 ch_stat0.status_0_regx.samp_freq = CH_STATUS_MAP_192KHZ;
345 break;
346
347 default:
348 /* control should never come here */
349 return -EINVAL;
350 break;
351
352 }
Takashi Iwai79dda752017-01-30 17:23:39 +0100353 had_write_register(intelhaddata,
354 AUD_CH_STATUS_0, ch_stat0.status_0_regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530355
356 format = substream->runtime->format;
357
358 if (format == SNDRV_PCM_FORMAT_S16_LE) {
359 ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_20;
360 ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_16BITS;
361 } else if (format == SNDRV_PCM_FORMAT_S24_LE) {
362 ch_stat1.status_1_regx.max_wrd_len = MAX_SMPL_WIDTH_24;
363 ch_stat1.status_1_regx.wrd_len = SMPL_WIDTH_24BITS;
364 } else {
365 ch_stat1.status_1_regx.max_wrd_len = 0;
366 ch_stat1.status_1_regx.wrd_len = 0;
367 }
Takashi Iwai79dda752017-01-30 17:23:39 +0100368 had_write_register(intelhaddata,
369 AUD_CH_STATUS_1, ch_stat1.status_1_regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530370 return 0;
371}
372
Takashi Iwai76296ef2017-01-30 16:09:11 +0100373/*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530374 * function to initialize audio
375 * registers and buffer confgiuration registers
376 * This function is called in the prepare callback
377 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100378static int snd_intelhad_audio_ctrl(struct snd_pcm_substream *substream,
379 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530380{
381 union aud_cfg cfg_val = {.cfg_regval = 0};
382 union aud_buf_config buf_cfg = {.buf_cfgval = 0};
383 u8 channels;
384
385 had_prog_status_reg(substream, intelhaddata);
386
387 buf_cfg.buf_cfg_regx_v2.audio_fifo_watermark = FIFO_THRESHOLD;
388 buf_cfg.buf_cfg_regx_v2.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
389 buf_cfg.buf_cfg_regx_v2.aud_delay = 0;
Takashi Iwai79dda752017-01-30 17:23:39 +0100390 had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.buf_cfgval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530391
392 channels = substream->runtime->channels;
393 cfg_val.cfg_regx_v2.num_ch = channels - 2;
394 if (channels <= 2)
395 cfg_val.cfg_regx_v2.layout = LAYOUT0;
396 else
397 cfg_val.cfg_regx_v2.layout = LAYOUT1;
398
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600399 cfg_val.cfg_regx_v2.val_bit = 1;
Takashi Iwai79dda752017-01-30 17:23:39 +0100400 had_write_register(intelhaddata, AUD_CONFIG, cfg_val.cfg_regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530401 return 0;
402}
403
Jerome Anand5dab11d2017-01-25 04:27:52 +0530404/*
405 * Compute derived values in channel_allocations[].
406 */
407static void init_channel_allocations(void)
408{
409 int i, j;
410 struct cea_channel_speaker_allocation *p;
411
Jerome Anand5dab11d2017-01-25 04:27:52 +0530412 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
413 p = channel_allocations + i;
414 p->channels = 0;
415 p->spk_mask = 0;
416 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
417 if (p->speakers[j]) {
418 p->channels++;
419 p->spk_mask |= p->speakers[j];
420 }
421 }
422}
423
424/*
425 * The transformation takes two steps:
426 *
427 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
428 * spk_mask => (channel_allocations[]) => ai->CA
429 *
430 * TODO: it could select the wrong CA from multiple candidates.
431 */
432static int snd_intelhad_channel_allocation(struct snd_intelhad *intelhaddata,
433 int channels)
434{
435 int i;
436 int ca = 0;
437 int spk_mask = 0;
438
439 /*
440 * CA defaults to 0 for basic stereo audio
441 */
442 if (channels <= 2)
443 return 0;
444
445 /*
446 * expand ELD's speaker allocation mask
447 *
448 * ELD tells the speaker mask in a compact(paired) form,
449 * expand ELD's notions to match the ones used by Audio InfoFrame.
450 */
451
452 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
Takashi Iwaida864802017-01-31 13:52:22 +0100453 if (intelhaddata->eld.speaker_allocation_block & (1 << i))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530454 spk_mask |= eld_speaker_allocation_bits[i];
455 }
456
457 /* search for the first working match in the CA table */
458 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
459 if (channels == channel_allocations[i].channels &&
460 (spk_mask & channel_allocations[i].spk_mask) ==
461 channel_allocations[i].spk_mask) {
462 ca = channel_allocations[i].ca_index;
463 break;
464 }
465 }
466
Takashi Iwaic75b0472017-01-31 15:49:15 +0100467 dev_dbg(intelhaddata->dev, "select CA 0x%x for %d\n", ca, channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530468
469 return ca;
470}
471
472/* from speaker bit mask to ALSA API channel position */
473static int spk_to_chmap(int spk)
474{
475 struct channel_map_table *t = map_tables;
476
477 for (; t->map; t++) {
478 if (t->spk_mask == spk)
479 return t->map;
480 }
481 return 0;
482}
483
Takashi Iwai372d8552017-01-31 13:57:58 +0100484static void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530485{
486 int i = 0, c = 0;
487 int spk_mask = 0;
488 struct snd_pcm_chmap_elem *chmap;
489 u8 eld_high, eld_high_mask = 0xF0;
490 u8 high_msb;
491
492 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
493 if (chmap == NULL) {
494 intelhaddata->chmap->chmap = NULL;
495 return;
496 }
497
Takashi Iwaic75b0472017-01-31 15:49:15 +0100498 dev_dbg(intelhaddata->dev, "eld.speaker_allocation_block = %x\n",
Takashi Iwaida864802017-01-31 13:52:22 +0100499 intelhaddata->eld.speaker_allocation_block);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530500
501 /* WA: Fix the max channel supported to 8 */
502
503 /*
504 * Sink may support more than 8 channels, if eld_high has more than
505 * one bit set. SOC supports max 8 channels.
506 * Refer eld_speaker_allocation_bits, for sink speaker allocation
507 */
508
509 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
Takashi Iwaida864802017-01-31 13:52:22 +0100510 eld_high = intelhaddata->eld.speaker_allocation_block & eld_high_mask;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530511 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
512 /* eld_high & (eld_high-1): if more than 1 bit set */
513 /* 0x1F: 7 channels */
514 for (i = 1; i < 4; i++) {
515 high_msb = eld_high & (0x80 >> i);
516 if (high_msb) {
Takashi Iwaida864802017-01-31 13:52:22 +0100517 intelhaddata->eld.speaker_allocation_block &=
Jerome Anand5dab11d2017-01-25 04:27:52 +0530518 high_msb | 0xF;
519 break;
520 }
521 }
522 }
523
524 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
Takashi Iwaida864802017-01-31 13:52:22 +0100525 if (intelhaddata->eld.speaker_allocation_block & (1 << i))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530526 spk_mask |= eld_speaker_allocation_bits[i];
527 }
528
529 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
530 if (spk_mask == channel_allocations[i].spk_mask) {
531 for (c = 0; c < channel_allocations[i].channels; c++) {
532 chmap->map[c] = spk_to_chmap(
533 channel_allocations[i].speakers[
534 (MAX_SPEAKERS - 1)-c]);
535 }
536 chmap->channels = channel_allocations[i].channels;
537 intelhaddata->chmap->chmap = chmap;
538 break;
539 }
540 }
541 if (i >= ARRAY_SIZE(channel_allocations)) {
542 intelhaddata->chmap->chmap = NULL;
543 kfree(chmap);
544 }
545}
546
547/*
548 * ALSA API channel-map control callbacks
549 */
550static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
551 struct snd_ctl_elem_info *uinfo)
552{
553 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
554 struct snd_intelhad *intelhaddata = info->private_data;
555
556 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
557 return -ENODEV;
558 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
559 uinfo->count = HAD_MAX_CHANNEL;
560 uinfo->value.integer.min = 0;
561 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
562 return 0;
563}
564
565static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
566 struct snd_ctl_elem_value *ucontrol)
567{
568 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
569 struct snd_intelhad *intelhaddata = info->private_data;
570 int i = 0;
571 const struct snd_pcm_chmap_elem *chmap;
572
573 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
574 return -ENODEV;
575 if (intelhaddata->chmap->chmap == NULL)
576 return -ENODATA;
577 chmap = intelhaddata->chmap->chmap;
Takashi Iwaic75b0472017-01-31 15:49:15 +0100578 for (i = 0; i < chmap->channels; i++)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530579 ucontrol->value.integer.value[i] = chmap->map[i];
Jerome Anand5dab11d2017-01-25 04:27:52 +0530580
581 return 0;
582}
583
584static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
585 struct snd_pcm *pcm)
586{
587 int err = 0;
588
589 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
590 NULL, 0, (unsigned long)intelhaddata,
591 &intelhaddata->chmap);
592 if (err < 0)
593 return err;
594
595 intelhaddata->chmap->private_data = intelhaddata;
Takashi Iwaie9d65ab2017-01-31 16:11:27 +0100596 intelhaddata->chmap->kctl->info = had_chmap_ctl_info;
597 intelhaddata->chmap->kctl->get = had_chmap_ctl_get;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530598 intelhaddata->chmap->chmap = NULL;
599 return 0;
600}
601
Takashi Iwai76296ef2017-01-30 16:09:11 +0100602/*
603 * snd_intelhad_prog_dip - to initialize Data Island Packets registers
Jerome Anand5dab11d2017-01-25 04:27:52 +0530604 *
605 * @substream:substream for which the prepare function is called
606 * @intelhaddata:substream private data
607 *
608 * This function is called in the prepare callback
609 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100610static void snd_intelhad_prog_dip(struct snd_pcm_substream *substream,
611 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530612{
613 int i;
614 union aud_ctrl_st ctrl_state = {.ctrl_val = 0};
615 union aud_info_frame2 frame2 = {.fr2_val = 0};
616 union aud_info_frame3 frame3 = {.fr3_val = 0};
617 u8 checksum = 0;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600618 u32 info_frame;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530619 int channels;
620
621 channels = substream->runtime->channels;
622
Takashi Iwai79dda752017-01-30 17:23:39 +0100623 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.ctrl_val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530624
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600625 if (intelhaddata->dp_output) {
626 info_frame = DP_INFO_FRAME_WORD1;
627 frame2.fr2_val = 1;
628 } else {
629 info_frame = HDMI_INFO_FRAME_WORD1;
630 frame2.fr2_regx.chnl_cnt = substream->runtime->channels - 1;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530631
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600632 frame3.fr3_regx.chnl_alloc = snd_intelhad_channel_allocation(
633 intelhaddata, channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530634
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600635 /*Calculte the byte wide checksum for all valid DIP words*/
636 for (i = 0; i < BYTES_PER_WORD; i++)
637 checksum += (info_frame >> i*BITS_PER_BYTE) & MASK_BYTE0;
638 for (i = 0; i < BYTES_PER_WORD; i++)
639 checksum += (frame2.fr2_val >> i*BITS_PER_BYTE) & MASK_BYTE0;
640 for (i = 0; i < BYTES_PER_WORD; i++)
641 checksum += (frame3.fr3_val >> i*BITS_PER_BYTE) & MASK_BYTE0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530642
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600643 frame2.fr2_regx.chksum = -(checksum);
644 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530645
Takashi Iwai79dda752017-01-30 17:23:39 +0100646 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, info_frame);
647 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, frame2.fr2_val);
648 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, frame3.fr3_val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530649
650 /* program remaining DIP words with zero */
651 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
Takashi Iwai79dda752017-01-30 17:23:39 +0100652 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, 0x0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530653
654 ctrl_state.ctrl_regx.dip_freq = 1;
655 ctrl_state.ctrl_regx.dip_en_sta = 1;
Takashi Iwai79dda752017-01-30 17:23:39 +0100656 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.ctrl_val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530657}
658
659/**
660 * snd_intelhad_prog_buffer - programs buffer
661 * address and length registers
662 *
663 * @substream:substream for which the prepare function is called
664 * @intelhaddata:substream private data
665 *
666 * This function programs ring buffer address and length into registers.
667 */
Takashi Iwai372d8552017-01-31 13:57:58 +0100668static int snd_intelhad_prog_buffer(struct snd_intelhad *intelhaddata,
Jerome Anand5dab11d2017-01-25 04:27:52 +0530669 int start, int end)
670{
671 u32 ring_buf_addr, ring_buf_size, period_bytes;
672 u8 i, num_periods;
673 struct snd_pcm_substream *substream;
674
675 substream = intelhaddata->stream_info.had_substream;
Takashi Iwaic75b0472017-01-31 15:49:15 +0100676 if (WARN_ON(!substream))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530677 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530678
679 ring_buf_addr = substream->runtime->dma_addr;
680 ring_buf_size = snd_pcm_lib_buffer_bytes(substream);
681 intelhaddata->stream_info.ring_buf_size = ring_buf_size;
682 period_bytes = frames_to_bytes(substream->runtime,
683 substream->runtime->period_size);
684 num_periods = substream->runtime->periods;
685
686 /*
687 * buffer addr should be 64 byte aligned, period bytes
688 * will be used to calculate addr offset
689 */
690 period_bytes &= ~0x3F;
691
692 /* Hardware supports MAX_PERIODS buffers */
693 if (end >= HAD_MAX_PERIODS)
694 return -EINVAL;
695
696 for (i = start; i <= end; i++) {
697 /* Program the buf registers with addr and len */
698 intelhaddata->buf_info[i].buf_addr = ring_buf_addr +
699 (i * period_bytes);
700 if (i < num_periods-1)
701 intelhaddata->buf_info[i].buf_size = period_bytes;
702 else
703 intelhaddata->buf_info[i].buf_size = ring_buf_size -
704 (period_bytes*i);
705
Takashi Iwai79dda752017-01-30 17:23:39 +0100706 had_write_register(intelhaddata,
707 AUD_BUF_A_ADDR + (i * HAD_REG_WIDTH),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530708 intelhaddata->buf_info[i].buf_addr |
709 BIT(0) | BIT(1));
Takashi Iwai79dda752017-01-30 17:23:39 +0100710 had_write_register(intelhaddata,
711 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530712 period_bytes);
713 intelhaddata->buf_info[i].is_valid = true;
714 }
Takashi Iwaic75b0472017-01-31 15:49:15 +0100715 dev_dbg(intelhaddata->dev, "%s:buf[%d-%d] addr=%#x and size=%d\n",
716 __func__, start, end,
717 intelhaddata->buf_info[start].buf_addr,
718 intelhaddata->buf_info[start].buf_size);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530719 intelhaddata->valid_buf_cnt = num_periods;
720 return 0;
721}
722
Takashi Iwai372d8552017-01-31 13:57:58 +0100723static int snd_intelhad_read_len(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530724{
725 int i, retval = 0;
726 u32 len[4];
727
728 for (i = 0; i < 4 ; i++) {
Takashi Iwai79dda752017-01-30 17:23:39 +0100729 had_read_register(intelhaddata,
730 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
731 &len[i]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530732 if (!len[i])
733 retval++;
734 }
735 if (retval != 1) {
736 for (i = 0; i < 4 ; i++)
Takashi Iwaic75b0472017-01-31 15:49:15 +0100737 dev_dbg(intelhaddata->dev, "buf[%d] size=%d\n",
738 i, len[i]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530739 }
740
741 return retval;
742}
743
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600744static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
745{
746 u32 maud_val;
747
748 /* Select maud according to DP 1.2 spec*/
749 if (link_rate == DP_2_7_GHZ) {
750 switch (aud_samp_freq) {
751 case AUD_SAMPLE_RATE_32:
752 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
753 break;
754
755 case AUD_SAMPLE_RATE_44_1:
756 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
757 break;
758
759 case AUD_SAMPLE_RATE_48:
760 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
761 break;
762
763 case AUD_SAMPLE_RATE_88_2:
764 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
765 break;
766
767 case AUD_SAMPLE_RATE_96:
768 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
769 break;
770
771 case AUD_SAMPLE_RATE_176_4:
772 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
773 break;
774
775 case HAD_MAX_RATE:
776 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
777 break;
778
779 default:
780 maud_val = -EINVAL;
781 break;
782 }
783 } else if (link_rate == DP_1_62_GHZ) {
784 switch (aud_samp_freq) {
785 case AUD_SAMPLE_RATE_32:
786 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
787 break;
788
789 case AUD_SAMPLE_RATE_44_1:
790 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
791 break;
792
793 case AUD_SAMPLE_RATE_48:
794 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
795 break;
796
797 case AUD_SAMPLE_RATE_88_2:
798 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
799 break;
800
801 case AUD_SAMPLE_RATE_96:
802 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
803 break;
804
805 case AUD_SAMPLE_RATE_176_4:
806 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
807 break;
808
809 case HAD_MAX_RATE:
810 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
811 break;
812
813 default:
814 maud_val = -EINVAL;
815 break;
816 }
817 } else
818 maud_val = -EINVAL;
819
820 return maud_val;
821}
822
Takashi Iwai76296ef2017-01-30 16:09:11 +0100823/*
824 * snd_intelhad_prog_cts - Program HDMI audio CTS value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530825 *
826 * @aud_samp_freq: sampling frequency of audio data
827 * @tmds: sampling frequency of the display data
828 * @n_param: N value, depends on aud_samp_freq
829 * @intelhaddata:substream private data
830 *
831 * Program CTS register based on the audio and display sampling frequency
832 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100833static void snd_intelhad_prog_cts(u32 aud_samp_freq, u32 tmds,
834 u32 link_rate, u32 n_param,
835 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530836{
837 u32 cts_val;
838 u64 dividend, divisor;
839
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600840 if (intelhaddata->dp_output) {
841 /* Substitute cts_val with Maud according to DP 1.2 spec*/
842 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
843 } else {
844 /* Calculate CTS according to HDMI 1.3a spec*/
845 dividend = (u64)tmds * n_param*1000;
846 divisor = 128 * aud_samp_freq;
847 cts_val = div64_u64(dividend, divisor);
848 }
Takashi Iwaic75b0472017-01-31 15:49:15 +0100849 dev_dbg(intelhaddata->dev, "TMDS value=%d, N value=%d, CTS Value=%d\n",
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600850 tmds, n_param, cts_val);
Takashi Iwai79dda752017-01-30 17:23:39 +0100851 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530852}
853
854static int had_calculate_n_value(u32 aud_samp_freq)
855{
856 s32 n_val;
857
858 /* Select N according to HDMI 1.3a spec*/
859 switch (aud_samp_freq) {
860 case AUD_SAMPLE_RATE_32:
861 n_val = 4096;
862 break;
863
864 case AUD_SAMPLE_RATE_44_1:
865 n_val = 6272;
866 break;
867
868 case AUD_SAMPLE_RATE_48:
869 n_val = 6144;
870 break;
871
872 case AUD_SAMPLE_RATE_88_2:
873 n_val = 12544;
874 break;
875
876 case AUD_SAMPLE_RATE_96:
877 n_val = 12288;
878 break;
879
880 case AUD_SAMPLE_RATE_176_4:
881 n_val = 25088;
882 break;
883
884 case HAD_MAX_RATE:
885 n_val = 24576;
886 break;
887
888 default:
889 n_val = -EINVAL;
890 break;
891 }
892 return n_val;
893}
894
Takashi Iwai76296ef2017-01-30 16:09:11 +0100895/*
896 * snd_intelhad_prog_n - Program HDMI audio N value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530897 *
898 * @aud_samp_freq: sampling frequency of audio data
899 * @n_param: N value, depends on aud_samp_freq
900 * @intelhaddata:substream private data
901 *
902 * This function is called in the prepare callback.
903 * It programs based on the audio and display sampling frequency
904 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100905static int snd_intelhad_prog_n(u32 aud_samp_freq, u32 *n_param,
906 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530907{
908 s32 n_val;
909
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600910 if (intelhaddata->dp_output) {
911 /*
912 * According to DP specs, Maud and Naud values hold
913 * a relationship, which is stated as:
914 * Maud/Naud = 512 * fs / f_LS_Clk
915 * where, fs is the sampling frequency of the audio stream
916 * and Naud is 32768 for Async clock.
917 */
918
919 n_val = DP_NAUD_VAL;
920 } else
921 n_val = had_calculate_n_value(aud_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530922
923 if (n_val < 0)
924 return n_val;
925
Takashi Iwai79dda752017-01-30 17:23:39 +0100926 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530927 *n_param = n_val;
928 return 0;
929}
930
Takashi Iwai372d8552017-01-31 13:57:58 +0100931static void snd_intelhad_handle_underrun(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530932{
933 u32 hdmi_status, i = 0;
934
935 /* Handle Underrun interrupt within Audio Unit */
Takashi Iwai79dda752017-01-30 17:23:39 +0100936 had_write_register(intelhaddata, AUD_CONFIG, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530937 /* Reset buffer pointers */
Takashi Iwai79dda752017-01-30 17:23:39 +0100938 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, 1);
939 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530940 /**
941 * The interrupt status 'sticky' bits might not be cleared by
942 * setting '1' to that bit once...
943 */
944 do { /* clear bit30, 31 AUD_HDMI_STATUS */
Takashi Iwai79dda752017-01-30 17:23:39 +0100945 had_read_register(intelhaddata, AUD_HDMI_STATUS_v2,
946 &hdmi_status);
Takashi Iwaic75b0472017-01-31 15:49:15 +0100947 dev_dbg(intelhaddata->dev, "HDMI status =0x%x\n", hdmi_status);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530948 if (hdmi_status & AUD_CONFIG_MASK_UNDERRUN) {
949 i++;
Takashi Iwai79dda752017-01-30 17:23:39 +0100950 had_write_register(intelhaddata,
951 AUD_HDMI_STATUS_v2, hdmi_status);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530952 } else
953 break;
954 } while (i < MAX_CNT);
955 if (i >= MAX_CNT)
Takashi Iwaic75b0472017-01-31 15:49:15 +0100956 dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n");
Jerome Anand5dab11d2017-01-25 04:27:52 +0530957}
958
959/**
960 * snd_intelhad_open - stream initializations are done here
961 * @substream:substream for which the stream function is called
962 *
963 * This function is called whenever a PCM stream is opened
964 */
965static int snd_intelhad_open(struct snd_pcm_substream *substream)
966{
967 struct snd_intelhad *intelhaddata;
968 struct snd_pcm_runtime *runtime;
969 struct had_stream_pvt *stream;
Takashi Iwai5647aec2017-01-31 08:14:34 +0100970 struct had_stream_data *had_stream;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530971 int retval;
972
Jerome Anand5dab11d2017-01-25 04:27:52 +0530973 intelhaddata = snd_pcm_substream_chip(substream);
Takashi Iwai5647aec2017-01-31 08:14:34 +0100974 had_stream = &intelhaddata->stream_data;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530975 runtime = substream->runtime;
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +0100976 intelhaddata->underrun_count = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530977
978 pm_runtime_get(intelhaddata->dev);
979
980 if (had_get_hwstate(intelhaddata)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +0100981 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
982 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530983 retval = -ENODEV;
984 goto exit_put_handle;
985 }
986
987 /* Check, if device already in use */
988 if (runtime->private_data) {
Takashi Iwaic75b0472017-01-31 15:49:15 +0100989 dev_dbg(intelhaddata->dev, "Device already in use\n");
Jerome Anand5dab11d2017-01-25 04:27:52 +0530990 retval = -EBUSY;
991 goto exit_put_handle;
992 }
993
994 /* set the runtime hw parameter with local snd_pcm_hardware struct */
995 runtime->hw = snd_intel_hadstream;
996
997 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
998 if (!stream) {
999 retval = -ENOMEM;
1000 goto exit_put_handle;
1001 }
1002 stream->stream_status = STREAM_INIT;
1003 runtime->private_data = stream;
1004
1005 retval = snd_pcm_hw_constraint_integer(runtime,
1006 SNDRV_PCM_HW_PARAM_PERIODS);
1007 if (retval < 0)
1008 goto exit_err;
1009
1010 /* Make sure, that the period size is always aligned
1011 * 64byte boundary
1012 */
1013 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1014 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
1015 if (retval < 0) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001016 dev_dbg(intelhaddata->dev, "%s:step_size=64 failed,err=%d\n",
1017 __func__, retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301018 goto exit_err;
1019 }
1020
1021 return retval;
1022exit_err:
1023 kfree(stream);
1024exit_put_handle:
1025 pm_runtime_put(intelhaddata->dev);
1026 runtime->private_data = NULL;
1027 return retval;
1028}
1029
Takashi Iwaidf76df12017-01-31 16:04:10 +01001030/*
Jerome Anand5dab11d2017-01-25 04:27:52 +05301031 * had_period_elapsed - updates the hardware pointer status
Takashi Iwaidf76df12017-01-31 16:04:10 +01001032 * @had_substream: substream for which the stream function is called
Jerome Anand5dab11d2017-01-25 04:27:52 +05301033 */
Takashi Iwaidf76df12017-01-31 16:04:10 +01001034static void had_period_elapsed(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301035{
Jerome Anand5dab11d2017-01-25 04:27:52 +05301036 struct had_stream_pvt *stream;
1037
Jerome Anand5dab11d2017-01-25 04:27:52 +05301038 if (!substream || !substream->runtime)
1039 return;
1040 stream = substream->runtime->private_data;
1041 if (!stream)
1042 return;
1043
1044 if (stream->stream_status != STREAM_RUNNING)
1045 return;
1046 snd_pcm_period_elapsed(substream);
1047}
1048
1049/**
1050 * snd_intelhad_init_stream - internal function to initialize stream info
1051 * @substream:substream for which the stream function is called
1052 *
1053 */
1054static int snd_intelhad_init_stream(struct snd_pcm_substream *substream)
1055{
1056 struct snd_intelhad *intelhaddata = snd_pcm_substream_chip(substream);
1057
Jerome Anand5dab11d2017-01-25 04:27:52 +05301058 intelhaddata->stream_info.had_substream = substream;
1059 intelhaddata->stream_info.buffer_ptr = 0;
1060 intelhaddata->stream_info.buffer_rendered = 0;
1061 intelhaddata->stream_info.sfreq = substream->runtime->rate;
1062 return 0;
1063}
1064
1065/**
1066 * snd_intelhad_close- to free parameteres when stream is stopped
1067 *
1068 * @substream: substream for which the function is called
1069 *
1070 * This function is called by ALSA framework when stream is stopped
1071 */
1072static int snd_intelhad_close(struct snd_pcm_substream *substream)
1073{
1074 struct snd_intelhad *intelhaddata;
1075 struct snd_pcm_runtime *runtime;
1076
Jerome Anand5dab11d2017-01-25 04:27:52 +05301077 intelhaddata = snd_pcm_substream_chip(substream);
1078 runtime = substream->runtime;
1079
Takashi Iwaic75b0472017-01-31 15:49:15 +01001080 if (WARN_ON(!runtime->private_data))
Jerome Anand5dab11d2017-01-25 04:27:52 +05301081 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301082
1083 intelhaddata->stream_info.buffer_rendered = 0;
1084 intelhaddata->stream_info.buffer_ptr = 0;
1085 intelhaddata->stream_info.str_id = 0;
1086 intelhaddata->stream_info.had_substream = NULL;
1087
1088 /* Check if following drv_status modification is required - VA */
1089 if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) {
1090 intelhaddata->drv_status = HAD_DRV_CONNECTED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001091 dev_dbg(intelhaddata->dev,
1092 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301093 __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{
Takashi Iwaic75b0472017-01-31 15:49:15 +01001113 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301114 unsigned long addr;
1115 int pages, buf_size, retval;
1116
Jerome Anand5dab11d2017-01-25 04:27:52 +05301117 if (!hw_params)
1118 return -EINVAL;
1119
Takashi Iwaic75b0472017-01-31 15:49:15 +01001120 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301121 buf_size = params_buffer_bytes(hw_params);
1122 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1123 if (retval < 0)
1124 return retval;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001125 dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
1126 __func__, buf_size);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301127 /* mark the pages as uncached region */
1128 addr = (unsigned long) substream->runtime->dma_area;
1129 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1130 retval = set_memory_uc(addr, pages);
1131 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001132 dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
1133 retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301134 return retval;
1135 }
1136 memset(substream->runtime->dma_area, 0, buf_size);
1137
1138 return retval;
1139}
1140
1141/**
1142 * snd_intelhad_hw_free- to release the resources allocated during
1143 * hardware params setup
1144 *
1145 * @substream: substream for which the function is called
1146 *
1147 * This function is called by ALSA framework before close callback.
1148 *
1149 */
1150static int snd_intelhad_hw_free(struct snd_pcm_substream *substream)
1151{
1152 unsigned long addr;
1153 u32 pages;
1154
Jerome Anand5dab11d2017-01-25 04:27:52 +05301155 /* 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{
Takashi Iwaida864802017-01-31 13:52:22 +01001175 int retval = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301176 unsigned long flag_irq;
1177 struct snd_intelhad *intelhaddata;
1178 struct had_stream_pvt *stream;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001179 struct had_stream_data *had_stream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301180
Jerome Anand5dab11d2017-01-25 04:27:52 +05301181 intelhaddata = snd_pcm_substream_chip(substream);
1182 stream = substream->runtime->private_data;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001183 had_stream = &intelhaddata->stream_data;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301184
1185 switch (cmd) {
1186 case SNDRV_PCM_TRIGGER_START:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301187 /* Disable local INTRs till register prgmng is done */
1188 if (had_get_hwstate(intelhaddata)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001189 dev_dbg(intelhaddata->dev,
1190 "_START: HDMI cable plugged-out\n");
Jerome Anand5dab11d2017-01-25 04:27:52 +05301191 retval = -ENODEV;
1192 break;
1193 }
1194 stream->stream_status = STREAM_RUNNING;
1195
1196 had_stream->stream_type = HAD_RUNNING_STREAM;
1197
1198 /* Enable Audio */
Takashi Iwaida864802017-01-31 13:52:22 +01001199 snd_intelhad_enable_audio_int(intelhaddata, true);
1200 snd_intelhad_enable_audio(intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301201 break;
1202
1203 case SNDRV_PCM_TRIGGER_STOP:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301204 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irq);
1205 intelhaddata->stream_info.str_id = 0;
1206 intelhaddata->curr_buf = 0;
1207
Takashi Iwaic75b0472017-01-31 15:49:15 +01001208 /* Stop reporting BUFFER_DONE/UNDERRUN to above layers */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301209
1210 had_stream->stream_type = HAD_INIT;
1211 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irq);
1212 /* Disable Audio */
Takashi Iwaida864802017-01-31 13:52:22 +01001213 snd_intelhad_enable_audio_int(intelhaddata, false);
1214 snd_intelhad_enable_audio(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301215 /* Reset buffer pointers */
Takashi Iwai79dda752017-01-30 17:23:39 +01001216 snd_intelhad_reset_audio(intelhaddata, 1);
1217 snd_intelhad_reset_audio(intelhaddata, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301218 stream->stream_status = STREAM_DROPPED;
Takashi Iwaida864802017-01-31 13:52:22 +01001219 snd_intelhad_enable_audio_int(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301220 break;
1221
1222 default:
1223 retval = -EINVAL;
1224 }
1225 return retval;
1226}
1227
1228/**
1229 * snd_intelhad_pcm_prepare- internal preparation before starting a stream
1230 *
1231 * @substream: substream for which the function is called
1232 *
1233 * This function is called when a stream is started for internal preparation.
1234 */
1235static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream)
1236{
1237 int retval;
1238 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001239 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301240 struct snd_intelhad *intelhaddata;
1241 struct snd_pcm_runtime *runtime;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001242 struct had_stream_data *had_stream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301243
Jerome Anand5dab11d2017-01-25 04:27:52 +05301244 intelhaddata = snd_pcm_substream_chip(substream);
1245 runtime = substream->runtime;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001246 had_stream = &intelhaddata->stream_data;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301247
1248 if (had_get_hwstate(intelhaddata)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001249 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
1250 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301251 retval = -ENODEV;
1252 goto prep_end;
1253 }
1254
Takashi Iwaic75b0472017-01-31 15:49:15 +01001255 dev_dbg(intelhaddata->dev, "period_size=%d\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301256 (int)frames_to_bytes(runtime, runtime->period_size));
Takashi Iwaic75b0472017-01-31 15:49:15 +01001257 dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods);
1258 dev_dbg(intelhaddata->dev, "buffer_size=%d\n",
1259 (int)snd_pcm_lib_buffer_bytes(substream));
1260 dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
1261 dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301262
1263 if (intelhaddata->stream_info.str_id) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001264 dev_dbg(intelhaddata->dev,
1265 "_prepare is called for existing str_id#%d\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301266 intelhaddata->stream_info.str_id);
1267 retval = snd_intelhad_pcm_trigger(substream,
1268 SNDRV_PCM_TRIGGER_STOP);
1269 return retval;
1270 }
1271
1272 retval = snd_intelhad_init_stream(substream);
1273 if (retval)
1274 goto prep_end;
1275
1276
1277 /* Get N value in KHz */
Takashi Iwaida864802017-01-31 13:52:22 +01001278 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301279
Takashi Iwai76296ef2017-01-30 16:09:11 +01001280 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1281 intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301282 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001283 dev_err(intelhaddata->dev,
1284 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301285 goto prep_end;
1286 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001287
1288 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001289 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001290
Takashi Iwai76296ef2017-01-30 16:09:11 +01001291 snd_intelhad_prog_cts(substream->runtime->rate,
1292 disp_samp_freq, link_rate,
1293 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301294
Takashi Iwai76296ef2017-01-30 16:09:11 +01001295 snd_intelhad_prog_dip(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301296
Takashi Iwai76296ef2017-01-30 16:09:11 +01001297 retval = snd_intelhad_audio_ctrl(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301298
1299 /* Prog buffer address */
1300 retval = snd_intelhad_prog_buffer(intelhaddata,
1301 HAD_BUF_TYPE_A, HAD_BUF_TYPE_D);
1302
1303 /*
1304 * Program channel mapping in following order:
1305 * FL, FR, C, LFE, RL, RR
1306 */
1307
Takashi Iwai79dda752017-01-30 17:23:39 +01001308 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301309
1310prep_end:
1311 return retval;
1312}
1313
1314/**
1315 * snd_intelhad_pcm_pointer- to send the current buffer pointerprocessed by hw
1316 *
1317 * @substream: substream for which the function is called
1318 *
1319 * This function is called by ALSA framework to get the current hw buffer ptr
1320 * when a period is elapsed
1321 */
1322static snd_pcm_uframes_t snd_intelhad_pcm_pointer(
1323 struct snd_pcm_substream *substream)
1324{
1325 struct snd_intelhad *intelhaddata;
1326 u32 bytes_rendered = 0;
1327 u32 t;
1328 int buf_id;
1329
Jerome Anand5dab11d2017-01-25 04:27:52 +05301330 intelhaddata = snd_pcm_substream_chip(substream);
1331
1332 if (intelhaddata->flag_underrun) {
Takashi Iwaie9d65ab2017-01-31 16:11:27 +01001333 intelhaddata->flag_underrun = false;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301334 return SNDRV_PCM_POS_XRUN;
1335 }
1336
1337 /* Use a hw register to calculate sub-period position reports.
1338 * This makes PulseAudio happier.
1339 */
1340
1341 buf_id = intelhaddata->curr_buf % 4;
Takashi Iwai79dda752017-01-30 17:23:39 +01001342 had_read_register(intelhaddata,
1343 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH), &t);
Jerome Anand232892f2017-01-25 04:27:53 +05301344
1345 if ((t == 0) || (t == ((u32)-1L))) {
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001346 intelhaddata->underrun_count++;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001347 dev_dbg(intelhaddata->dev,
1348 "discovered buffer done for buf %d, count = %d\n",
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001349 buf_id, intelhaddata->underrun_count);
Jerome Anand232892f2017-01-25 04:27:53 +05301350
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001351 if (intelhaddata->underrun_count > (HAD_MIN_PERIODS/2)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001352 dev_dbg(intelhaddata->dev,
1353 "assume audio_codec_reset, underrun = %d - do xrun\n",
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001354 intelhaddata->underrun_count);
1355 intelhaddata->underrun_count = 0;
Jerome Anand232892f2017-01-25 04:27:53 +05301356 return SNDRV_PCM_POS_XRUN;
1357 }
1358 } else {
1359 /* Reset Counter */
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001360 intelhaddata->underrun_count = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301361 }
Jerome Anand232892f2017-01-25 04:27:53 +05301362
Jerome Anand5dab11d2017-01-25 04:27:52 +05301363 t = intelhaddata->buf_info[buf_id].buf_size - t;
1364
1365 if (intelhaddata->stream_info.buffer_rendered)
1366 div_u64_rem(intelhaddata->stream_info.buffer_rendered,
1367 intelhaddata->stream_info.ring_buf_size,
1368 &(bytes_rendered));
1369
1370 intelhaddata->stream_info.buffer_ptr = bytes_to_frames(
1371 substream->runtime,
1372 bytes_rendered + t);
1373 return intelhaddata->stream_info.buffer_ptr;
1374}
1375
1376/**
1377 * snd_intelhad_pcm_mmap- mmaps a kernel buffer to user space for copying data
1378 *
1379 * @substream: substream for which the function is called
1380 * @vma: struct instance of memory VMM memory area
1381 *
1382 * This function is called by OS when a user space component
1383 * tries to get mmap memory from driver
1384 */
1385static int snd_intelhad_pcm_mmap(struct snd_pcm_substream *substream,
1386 struct vm_area_struct *vma)
1387{
Jerome Anand5dab11d2017-01-25 04:27:52 +05301388 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1389 return remap_pfn_range(vma, vma->vm_start,
1390 substream->dma_buffer.addr >> PAGE_SHIFT,
1391 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1392}
1393
Takashi Iwaida864802017-01-31 13:52:22 +01001394static int hdmi_audio_mode_change(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301395{
Takashi Iwaida864802017-01-31 13:52:22 +01001396 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301397 int retval = 0;
1398 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001399 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301400
Takashi Iwaida864802017-01-31 13:52:22 +01001401 substream = intelhaddata->stream_info.had_substream;
1402 if (!substream || !substream->runtime)
1403 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301404
1405 /* Disable Audio */
Takashi Iwaida864802017-01-31 13:52:22 +01001406 snd_intelhad_enable_audio(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301407
1408 /* Update CTS value */
Takashi Iwaida864802017-01-31 13:52:22 +01001409 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301410
Takashi Iwai76296ef2017-01-30 16:09:11 +01001411 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1412 intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301413 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001414 dev_err(intelhaddata->dev,
1415 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301416 goto out;
1417 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001418
1419 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001420 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001421
Takashi Iwai76296ef2017-01-30 16:09:11 +01001422 snd_intelhad_prog_cts(substream->runtime->rate,
1423 disp_samp_freq, link_rate,
1424 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301425
1426 /* Enable Audio */
Takashi Iwaida864802017-01-31 13:52:22 +01001427 snd_intelhad_enable_audio(intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301428
1429out:
1430 return retval;
1431}
1432
Takashi Iwai372d8552017-01-31 13:57:58 +01001433/*
1434 * hdmi_lpe_audio_suspend - power management suspend function
1435 *
1436 * @pdev: platform device
1437 *
1438 * This function is called by client driver to suspend the
1439 * hdmi audio.
1440 */
1441static int hdmi_lpe_audio_suspend(struct platform_device *pdev,
1442 pm_message_t state)
1443{
1444 struct had_stream_data *had_stream;
1445 unsigned long flag_irqs;
1446 struct snd_pcm_substream *substream;
1447 struct snd_intelhad *intelhaddata = platform_get_drvdata(pdev);
1448
Takashi Iwai372d8552017-01-31 13:57:58 +01001449 had_stream = &intelhaddata->stream_data;
1450 substream = intelhaddata->stream_info.had_substream;
1451
Takashi Iwaie29c0f92017-02-01 17:27:48 +01001452 if (!pm_runtime_status_suspended(intelhaddata->dev)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001453 dev_err(intelhaddata->dev, "audio stream is active\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001454 return -EAGAIN;
1455 }
1456
1457
1458 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1459 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
1460 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001461 dev_dbg(intelhaddata->dev, "had not connected\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001462 return 0;
1463 }
1464
1465 if (intelhaddata->drv_status == HAD_DRV_SUSPENDED) {
1466 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001467 dev_dbg(intelhaddata->dev, "had already suspended\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001468 return 0;
1469 }
1470
1471 intelhaddata->drv_status = HAD_DRV_SUSPENDED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001472 dev_dbg(intelhaddata->dev,
1473 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_SUSPENDED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001474 __func__, __LINE__);
1475
1476 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1477 snd_intelhad_enable_audio_int(intelhaddata, false);
Takashi Iwai372d8552017-01-31 13:57:58 +01001478 return 0;
1479}
1480
1481/*
1482 * hdmi_lpe_audio_resume - power management resume function
1483 *
1484 *@pdev: platform device
1485 *
1486 * This function is called by client driver to resume the
1487 * hdmi audio.
1488 */
1489static int hdmi_lpe_audio_resume(struct platform_device *pdev)
1490{
1491 struct snd_intelhad *intelhaddata = platform_get_drvdata(pdev);
1492 unsigned long flag_irqs;
1493
Takashi Iwai372d8552017-01-31 13:57:58 +01001494 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1495 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
1496 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001497 dev_dbg(intelhaddata->dev, "had not connected\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001498 return 0;
1499 }
1500
1501 if (intelhaddata->drv_status != HAD_DRV_SUSPENDED) {
1502 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001503 dev_err(intelhaddata->dev, "had is not in suspended state\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001504 return 0;
1505 }
1506
1507 if (had_get_hwstate(intelhaddata)) {
1508 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001509 dev_err(intelhaddata->dev,
1510 "Failed to resume. Device not accessible\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001511 return -ENODEV;
1512 }
1513
1514 intelhaddata->drv_status = HAD_DRV_CONNECTED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001515 dev_dbg(intelhaddata->dev,
1516 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001517 __func__, __LINE__);
1518 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1519 snd_intelhad_enable_audio_int(intelhaddata, true);
Takashi Iwai372d8552017-01-31 13:57:58 +01001520 return 0;
1521}
1522
1523static inline int had_chk_intrmiss(struct snd_intelhad *intelhaddata,
1524 enum intel_had_aud_buf_type buf_id)
1525{
1526 int i, intr_count = 0;
1527 enum intel_had_aud_buf_type buff_done;
1528 u32 buf_size, buf_addr;
1529 struct had_stream_data *had_stream;
1530 unsigned long flag_irqs;
1531
1532 had_stream = &intelhaddata->stream_data;
1533
1534 buff_done = buf_id;
1535
1536 intr_count = snd_intelhad_read_len(intelhaddata);
1537 if (intr_count > 1) {
1538 /* In case of active playback */
Takashi Iwaic75b0472017-01-31 15:49:15 +01001539 dev_err(intelhaddata->dev,
1540 "Driver detected %d missed buffer done interrupt(s)\n",
1541 (intr_count - 1));
Takashi Iwai372d8552017-01-31 13:57:58 +01001542 if (intr_count > 3)
1543 return intr_count;
1544
1545 buf_id += (intr_count - 1);
1546 /* Reprogram registers*/
1547 for (i = buff_done; i < buf_id; i++) {
1548 int j = i % 4;
1549
1550 buf_size = intelhaddata->buf_info[j].buf_size;
1551 buf_addr = intelhaddata->buf_info[j].buf_addr;
1552 had_write_register(intelhaddata,
1553 AUD_BUF_A_LENGTH +
1554 (j * HAD_REG_WIDTH), buf_size);
1555 had_write_register(intelhaddata,
1556 AUD_BUF_A_ADDR+(j * HAD_REG_WIDTH),
1557 (buf_addr | BIT(0) | BIT(1)));
1558 }
1559 buf_id = buf_id % 4;
1560 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1561 intelhaddata->buff_done = buf_id;
1562 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1563 }
1564
1565 return intr_count;
1566}
1567
1568static int had_process_buffer_done(struct snd_intelhad *intelhaddata)
1569{
1570 u32 len = 1;
1571 enum intel_had_aud_buf_type buf_id;
1572 enum intel_had_aud_buf_type buff_done;
1573 struct pcm_stream_info *stream;
1574 u32 buf_size;
1575 struct had_stream_data *had_stream;
1576 int intr_count;
1577 enum had_status_stream stream_type;
1578 unsigned long flag_irqs;
1579
1580 had_stream = &intelhaddata->stream_data;
1581 stream = &intelhaddata->stream_info;
1582 intr_count = 1;
1583
1584 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1585 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
1586 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001587 dev_dbg(intelhaddata->dev,
1588 "%s:Device already disconnected\n", __func__);
Takashi Iwai372d8552017-01-31 13:57:58 +01001589 return 0;
1590 }
1591 buf_id = intelhaddata->curr_buf;
1592 intelhaddata->buff_done = buf_id;
1593 buff_done = intelhaddata->buff_done;
1594 buf_size = intelhaddata->buf_info[buf_id].buf_size;
1595 stream_type = had_stream->stream_type;
1596
Takashi Iwai372d8552017-01-31 13:57:58 +01001597 /* Every debug statement has an implication
1598 * of ~5msec. Thus, avoid having >3 debug statements
1599 * for each buffer_done handling.
1600 */
1601
1602 /* Check for any intr_miss in case of active playback */
1603 if (had_stream->stream_type == HAD_RUNNING_STREAM) {
1604 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1605 intr_count = had_chk_intrmiss(intelhaddata, buf_id);
1606 if (!intr_count || (intr_count > 3)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001607 dev_err(intelhaddata->dev,
1608 "HAD SW state in non-recoverable mode\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001609 return 0;
1610 }
1611 buf_id += (intr_count - 1);
1612 buf_id = buf_id % 4;
1613 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1614 }
1615
1616 intelhaddata->buf_info[buf_id].is_valid = true;
1617 if (intelhaddata->valid_buf_cnt-1 == buf_id) {
1618 if (had_stream->stream_type >= HAD_RUNNING_STREAM)
1619 intelhaddata->curr_buf = HAD_BUF_TYPE_A;
1620 } else
1621 intelhaddata->curr_buf = buf_id + 1;
1622
1623 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1624
1625 if (had_get_hwstate(intelhaddata)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001626 dev_dbg(intelhaddata->dev, "HDMI cable plugged-out\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001627 return 0;
1628 }
1629
1630 /*Reprogram the registers with addr and length*/
1631 had_write_register(intelhaddata,
1632 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH),
1633 buf_size);
1634 had_write_register(intelhaddata,
1635 AUD_BUF_A_ADDR + (buf_id * HAD_REG_WIDTH),
1636 intelhaddata->buf_info[buf_id].buf_addr |
1637 BIT(0) | BIT(1));
1638
1639 had_read_register(intelhaddata,
1640 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH),
1641 &len);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001642 dev_dbg(intelhaddata->dev, "%s:Enabled buf[%d]\n", __func__, buf_id);
Takashi Iwai372d8552017-01-31 13:57:58 +01001643
1644 /* In case of actual data,
1645 * report buffer_done to above ALSA layer
1646 */
Takashi Iwaidf76df12017-01-31 16:04:10 +01001647 buf_size = intelhaddata->buf_info[buf_id].buf_size;
Takashi Iwai372d8552017-01-31 13:57:58 +01001648 if (stream_type >= HAD_RUNNING_STREAM) {
1649 intelhaddata->stream_info.buffer_rendered +=
1650 (intr_count * buf_size);
Takashi Iwaidf76df12017-01-31 16:04:10 +01001651 had_period_elapsed(stream->had_substream);
Takashi Iwai372d8552017-01-31 13:57:58 +01001652 }
1653
1654 return 0;
1655}
1656
1657static int had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
1658{
1659 enum intel_had_aud_buf_type buf_id;
1660 struct pcm_stream_info *stream;
1661 struct had_stream_data *had_stream;
1662 enum had_status_stream stream_type;
1663 unsigned long flag_irqs;
1664 int drv_status;
1665
1666 had_stream = &intelhaddata->stream_data;
1667 stream = &intelhaddata->stream_info;
1668
1669 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1670 buf_id = intelhaddata->curr_buf;
1671 stream_type = had_stream->stream_type;
1672 intelhaddata->buff_done = buf_id;
1673 drv_status = intelhaddata->drv_status;
1674 if (stream_type == HAD_RUNNING_STREAM)
1675 intelhaddata->curr_buf = HAD_BUF_TYPE_A;
1676
1677 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1678
Takashi Iwaic75b0472017-01-31 15:49:15 +01001679 dev_dbg(intelhaddata->dev, "Enter:%s buf_id=%d, stream_type=%d\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001680 __func__, buf_id, stream_type);
1681
1682 snd_intelhad_handle_underrun(intelhaddata);
1683
1684 if (drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001685 dev_dbg(intelhaddata->dev,
1686 "%s:Device already disconnected\n", __func__);
Takashi Iwai372d8552017-01-31 13:57:58 +01001687 return 0;
1688 }
1689
1690 if (stream_type == HAD_RUNNING_STREAM) {
1691 /* Report UNDERRUN error to above layers */
Takashi Iwaie9d65ab2017-01-31 16:11:27 +01001692 intelhaddata->flag_underrun = true;
Takashi Iwaidf76df12017-01-31 16:04:10 +01001693 had_period_elapsed(stream->had_substream);
Takashi Iwai372d8552017-01-31 13:57:58 +01001694 }
1695
1696 return 0;
1697}
1698
1699static int had_process_hot_plug(struct snd_intelhad *intelhaddata)
1700{
1701 enum intel_had_aud_buf_type buf_id;
1702 struct snd_pcm_substream *substream;
1703 struct had_stream_data *had_stream;
1704 unsigned long flag_irqs;
1705
Takashi Iwai372d8552017-01-31 13:57:58 +01001706 substream = intelhaddata->stream_info.had_substream;
1707 had_stream = &intelhaddata->stream_data;
1708
1709 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1710 if (intelhaddata->drv_status == HAD_DRV_CONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001711 dev_dbg(intelhaddata->dev, "Device already connected\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001712 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1713 return 0;
1714 }
1715 buf_id = intelhaddata->curr_buf;
1716 intelhaddata->buff_done = buf_id;
1717 intelhaddata->drv_status = HAD_DRV_CONNECTED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001718 dev_dbg(intelhaddata->dev,
1719 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001720 __func__, __LINE__);
1721 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1722
Takashi Iwaic75b0472017-01-31 15:49:15 +01001723 dev_dbg(intelhaddata->dev, "Processing HOT_PLUG, buf_id = %d\n",
1724 buf_id);
Takashi Iwai372d8552017-01-31 13:57:58 +01001725
1726 /* Safety check */
1727 if (substream) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001728 dev_dbg(intelhaddata->dev,
1729 "Force to stop the active stream by disconnection\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001730 /* Set runtime->state to hw_params done */
1731 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1732 }
1733
1734 had_build_channel_allocation_map(intelhaddata);
1735
1736 return 0;
1737}
1738
1739static int had_process_hot_unplug(struct snd_intelhad *intelhaddata)
1740{
1741 enum intel_had_aud_buf_type buf_id;
1742 struct had_stream_data *had_stream;
1743 unsigned long flag_irqs;
1744
Takashi Iwai372d8552017-01-31 13:57:58 +01001745 had_stream = &intelhaddata->stream_data;
1746 buf_id = intelhaddata->curr_buf;
1747
1748 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1749
1750 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001751 dev_dbg(intelhaddata->dev, "Device already disconnected\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001752 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1753 return 0;
1754
1755 } else {
1756 /* Disable Audio */
1757 snd_intelhad_enable_audio_int(intelhaddata, false);
1758 snd_intelhad_enable_audio(intelhaddata, false);
1759 }
1760
1761 intelhaddata->drv_status = HAD_DRV_DISCONNECTED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001762 dev_dbg(intelhaddata->dev,
1763 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001764 __func__, __LINE__);
1765
1766 /* Report to above ALSA layer */
1767 if (intelhaddata->stream_info.had_substream != NULL) {
1768 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
Takashi Iwai372d8552017-01-31 13:57:58 +01001769 snd_pcm_stop(intelhaddata->stream_info.had_substream,
1770 SNDRV_PCM_STATE_SETUP);
1771 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1772 }
1773
1774 had_stream->stream_type = HAD_INIT;
1775 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1776 kfree(intelhaddata->chmap->chmap);
1777 intelhaddata->chmap->chmap = NULL;
Takashi Iwai372d8552017-01-31 13:57:58 +01001778
1779 return 0;
1780}
1781
1782/* PCM operations structure and the calls back for the same */
1783static struct snd_pcm_ops snd_intelhad_playback_ops = {
Jerome Anand5dab11d2017-01-25 04:27:52 +05301784 .open = snd_intelhad_open,
1785 .close = snd_intelhad_close,
1786 .ioctl = snd_pcm_lib_ioctl,
1787 .hw_params = snd_intelhad_hw_params,
1788 .hw_free = snd_intelhad_hw_free,
1789 .prepare = snd_intelhad_pcm_prepare,
1790 .trigger = snd_intelhad_pcm_trigger,
1791 .pointer = snd_intelhad_pcm_pointer,
1792 .mmap = snd_intelhad_pcm_mmap,
1793};
1794
Jerome Anand5dab11d2017-01-25 04:27:52 +05301795static int had_iec958_info(struct snd_kcontrol *kcontrol,
1796 struct snd_ctl_elem_info *uinfo)
1797{
1798 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1799 uinfo->count = 1;
1800 return 0;
1801}
1802
1803static int had_iec958_get(struct snd_kcontrol *kcontrol,
1804 struct snd_ctl_elem_value *ucontrol)
1805{
1806 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1807
1808 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1809 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1810 ucontrol->value.iec958.status[2] =
1811 (intelhaddata->aes_bits >> 16) & 0xff;
1812 ucontrol->value.iec958.status[3] =
1813 (intelhaddata->aes_bits >> 24) & 0xff;
1814 return 0;
1815}
Takashi Iwai372d8552017-01-31 13:57:58 +01001816
Jerome Anand5dab11d2017-01-25 04:27:52 +05301817static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1818 struct snd_ctl_elem_value *ucontrol)
1819{
1820 ucontrol->value.iec958.status[0] = 0xff;
1821 ucontrol->value.iec958.status[1] = 0xff;
1822 ucontrol->value.iec958.status[2] = 0xff;
1823 ucontrol->value.iec958.status[3] = 0xff;
1824 return 0;
1825}
Takashi Iwai372d8552017-01-31 13:57:58 +01001826
Jerome Anand5dab11d2017-01-25 04:27:52 +05301827static int had_iec958_put(struct snd_kcontrol *kcontrol,
1828 struct snd_ctl_elem_value *ucontrol)
1829{
1830 unsigned int val;
1831 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1832
Jerome Anand5dab11d2017-01-25 04:27:52 +05301833 val = (ucontrol->value.iec958.status[0] << 0) |
1834 (ucontrol->value.iec958.status[1] << 8) |
1835 (ucontrol->value.iec958.status[2] << 16) |
1836 (ucontrol->value.iec958.status[3] << 24);
1837 if (intelhaddata->aes_bits != val) {
1838 intelhaddata->aes_bits = val;
1839 return 1;
1840 }
1841 return 1;
1842}
1843
1844static struct snd_kcontrol_new had_control_iec958_mask = {
1845 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1846 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1847 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1848 .info = had_iec958_info, /* shared */
1849 .get = had_iec958_mask_get,
1850};
1851
1852static struct snd_kcontrol_new had_control_iec958 = {
1853 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1854 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1855 .info = had_iec958_info,
1856 .get = had_iec958_get,
1857 .put = had_iec958_put
1858};
1859
Takashi Iwaida864802017-01-31 13:52:22 +01001860static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id)
1861{
1862 struct snd_intelhad *ctx = dev_id;
1863 u32 audio_stat, audio_reg;
1864
1865 audio_reg = AUD_HDMI_STATUS_v2;
1866 mid_hdmi_audio_read(ctx, audio_reg, &audio_stat);
1867
1868 if (audio_stat & HDMI_AUDIO_UNDERRUN) {
1869 mid_hdmi_audio_write(ctx, audio_reg, HDMI_AUDIO_UNDERRUN);
1870 had_process_buffer_underrun(ctx);
1871 }
1872
1873 if (audio_stat & HDMI_AUDIO_BUFFER_DONE) {
1874 mid_hdmi_audio_write(ctx, audio_reg, HDMI_AUDIO_BUFFER_DONE);
1875 had_process_buffer_done(ctx);
1876 }
1877
1878 return IRQ_HANDLED;
1879}
1880
1881static void notify_audio_lpe(struct platform_device *pdev)
1882{
1883 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Takashi Iwaida864802017-01-31 13:52:22 +01001884
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001885 schedule_work(&ctx->hdmi_audio_wq);
1886}
Takashi Iwaida864802017-01-31 13:52:22 +01001887
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001888static void had_audio_wq(struct work_struct *work)
1889{
1890 struct snd_intelhad *ctx =
1891 container_of(work, struct snd_intelhad, hdmi_audio_wq);
1892 struct intel_hdmi_lpe_audio_pdata *pdata = ctx->dev->platform_data;
1893
1894 if (!pdata->hdmi_connected) {
1895 dev_dbg(ctx->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001896 __func__);
1897
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001898 if (ctx->state != hdmi_connector_status_connected) {
1899 dev_dbg(ctx->dev, "%s: Already Unplugged!\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001900 __func__);
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001901 return;
1902 }
1903
1904 ctx->state = hdmi_connector_status_disconnected;
1905 had_process_hot_unplug(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001906
1907 } else {
1908 struct intel_hdmi_lpe_audio_eld *eld = &pdata->eld;
1909
1910 switch (eld->pipe_id) {
1911 case 0:
1912 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1913 break;
1914 case 1:
1915 ctx->had_config_offset = AUDIO_HDMI_CONFIG_B;
1916 break;
1917 case 2:
1918 ctx->had_config_offset = AUDIO_HDMI_CONFIG_C;
1919 break;
1920 default:
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001921 dev_dbg(ctx->dev, "Invalid pipe %d\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001922 eld->pipe_id);
1923 break;
1924 }
1925
1926 memcpy(&ctx->eld, eld->eld_data, sizeof(ctx->eld));
1927
1928 had_process_hot_plug(ctx);
1929
1930 ctx->state = hdmi_connector_status_connected;
1931
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001932 dev_dbg(ctx->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001933 __func__, eld->port_id, pdata->tmds_clock_speed);
1934
1935 if (pdata->tmds_clock_speed) {
1936 ctx->tmds_clock_speed = pdata->tmds_clock_speed;
1937 ctx->dp_output = pdata->dp_output;
1938 ctx->link_rate = pdata->link_rate;
1939
1940 /* Process mode change if stream is active */
1941 if (ctx->stream_data.stream_type == HAD_RUNNING_STREAM)
1942 hdmi_audio_mode_change(ctx);
1943 }
1944 }
1945}
1946
1947/* release resources */
1948static void hdmi_lpe_audio_free(struct snd_card *card)
1949{
1950 struct snd_intelhad *ctx = card->private_data;
1951
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001952 cancel_work_sync(&ctx->hdmi_audio_wq);
1953
Takashi Iwaida864802017-01-31 13:52:22 +01001954 if (ctx->mmio_start)
1955 iounmap(ctx->mmio_start);
1956 if (ctx->irq >= 0)
1957 free_irq(ctx->irq, ctx);
1958}
1959
1960/*
1961 * hdmi_lpe_audio_probe - start bridge with i915
1962 *
1963 * This function is called when the i915 driver creates the
1964 * hdmi-lpe-audio platform device. Card creation is deferred until a
1965 * hot plug event is received
1966 */
1967static int hdmi_lpe_audio_probe(struct platform_device *pdev)
1968{
1969 struct snd_card *card;
1970 struct snd_intelhad *ctx;
1971 struct snd_pcm *pcm;
1972 struct intel_hdmi_lpe_audio_pdata *pdata;
1973 int irq;
1974 struct resource *res_mmio;
1975 int ret;
1976 unsigned long flags;
1977
Takashi Iwaida864802017-01-31 13:52:22 +01001978 dev_dbg(&pdev->dev, "dma_mask: %p\n", pdev->dev.dma_mask);
1979
1980 pdata = pdev->dev.platform_data;
1981 if (!pdata) {
1982 dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
1983 return -EINVAL;
1984 }
1985
1986 /* get resources */
1987 irq = platform_get_irq(pdev, 0);
1988 if (irq < 0) {
1989 dev_err(&pdev->dev, "Could not get irq resource\n");
1990 return -ENODEV;
1991 }
1992
1993 res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1994 if (!res_mmio) {
1995 dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
1996 return -ENXIO;
1997 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301998
Takashi Iwai5647aec2017-01-31 08:14:34 +01001999 /* create a card instance with ALSA framework */
Takashi Iwaida864802017-01-31 13:52:22 +01002000 ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
2001 THIS_MODULE, sizeof(*ctx), &card);
2002 if (ret)
2003 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05302004
Takashi Iwaida864802017-01-31 13:52:22 +01002005 ctx = card->private_data;
2006 spin_lock_init(&ctx->had_spinlock);
2007 ctx->drv_status = HAD_DRV_DISCONNECTED;
2008 ctx->dev = &pdev->dev;
2009 ctx->card = card;
Takashi Iwaie9d65ab2017-01-31 16:11:27 +01002010 ctx->flag_underrun = false;
Takashi Iwaida864802017-01-31 13:52:22 +01002011 ctx->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
2012 strcpy(card->driver, INTEL_HAD);
2013 strcpy(card->shortname, INTEL_HAD);
Jerome Anand5dab11d2017-01-25 04:27:52 +05302014
Takashi Iwaida864802017-01-31 13:52:22 +01002015 ctx->irq = -1;
2016 ctx->tmds_clock_speed = DIS_SAMPLE_RATE_148_5;
Takashi Iwai99b2ab92017-01-31 16:26:10 +01002017 INIT_WORK(&ctx->hdmi_audio_wq, had_audio_wq);
Takashi Iwaida864802017-01-31 13:52:22 +01002018 ctx->state = hdmi_connector_status_disconnected;
Jerome Anand5dab11d2017-01-25 04:27:52 +05302019
Takashi Iwaida864802017-01-31 13:52:22 +01002020 card->private_free = hdmi_lpe_audio_free;
2021
2022 /* assume pipe A as default */
2023 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
2024
2025 platform_set_drvdata(pdev, ctx);
2026
2027 dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
2028 __func__, (unsigned int)res_mmio->start,
2029 (unsigned int)res_mmio->end);
2030
2031 ctx->mmio_start = ioremap_nocache(res_mmio->start,
2032 (size_t)(resource_size(res_mmio)));
2033 if (!ctx->mmio_start) {
2034 dev_err(&pdev->dev, "Could not get ioremap\n");
2035 ret = -EACCES;
2036 goto err;
2037 }
2038
2039 /* setup interrupt handler */
2040 ret = request_irq(irq, display_pipe_interrupt_handler, 0,
2041 pdev->name, ctx);
2042 if (ret < 0) {
2043 dev_err(&pdev->dev, "request_irq failed\n");
2044 goto err;
2045 }
2046
2047 ctx->irq = irq;
2048
2049 ret = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
2050 MAX_CAP_STREAMS, &pcm);
2051 if (ret)
Jerome Anand5dab11d2017-01-25 04:27:52 +05302052 goto err;
2053
2054 /* setup private data which can be retrieved when required */
Takashi Iwaida864802017-01-31 13:52:22 +01002055 pcm->private_data = ctx;
Jerome Anand5dab11d2017-01-25 04:27:52 +05302056 pcm->info_flags = 0;
2057 strncpy(pcm->name, card->shortname, strlen(card->shortname));
Takashi Iwaida864802017-01-31 13:52:22 +01002058 /* setup the ops for playabck */
Jerome Anand5dab11d2017-01-25 04:27:52 +05302059 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2060 &snd_intelhad_playback_ops);
2061 /* allocate dma pages for ALSA stream operations
2062 * memory allocated is based on size, not max value
2063 * thus using same argument for max & size
2064 */
Takashi Iwaida864802017-01-31 13:52:22 +01002065 snd_pcm_lib_preallocate_pages_for_all(pcm,
Jerome Anand5dab11d2017-01-25 04:27:52 +05302066 SNDRV_DMA_TYPE_DEV, NULL,
2067 HAD_MAX_BUFFER, HAD_MAX_BUFFER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05302068
Jerome Anand5dab11d2017-01-25 04:27:52 +05302069 /* IEC958 controls */
Takashi Iwaida864802017-01-31 13:52:22 +01002070 ret = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958_mask, ctx));
2071 if (ret < 0)
Jerome Anand5dab11d2017-01-25 04:27:52 +05302072 goto err;
Takashi Iwaida864802017-01-31 13:52:22 +01002073 ret = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958, ctx));
2074 if (ret < 0)
Jerome Anand5dab11d2017-01-25 04:27:52 +05302075 goto err;
2076
2077 init_channel_allocations();
2078
2079 /* Register channel map controls */
Takashi Iwaida864802017-01-31 13:52:22 +01002080 ret = had_register_chmap_ctls(ctx, pcm);
2081 if (ret < 0)
Jerome Anand5dab11d2017-01-25 04:27:52 +05302082 goto err;
2083
Takashi Iwaida864802017-01-31 13:52:22 +01002084 ret = snd_card_register(card);
2085 if (ret)
Takashi Iwai36ec0d92017-01-31 08:47:05 +01002086 goto err;
2087
Takashi Iwaida864802017-01-31 13:52:22 +01002088 spin_lock_irqsave(&pdata->lpe_audio_slock, flags);
2089 pdata->notify_audio_lpe = notify_audio_lpe;
Takashi Iwai99b2ab92017-01-31 16:26:10 +01002090 pdata->notify_pending = false;
Takashi Iwaida864802017-01-31 13:52:22 +01002091 spin_unlock_irqrestore(&pdata->lpe_audio_slock, flags);
2092
2093 pm_runtime_set_active(&pdev->dev);
2094 pm_runtime_enable(&pdev->dev);
2095
Takashi Iwai99b2ab92017-01-31 16:26:10 +01002096 dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
Takashi Iwaida864802017-01-31 13:52:22 +01002097 schedule_work(&ctx->hdmi_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05302098
Takashi Iwai79dda752017-01-30 17:23:39 +01002099 return 0;
Takashi Iwai5647aec2017-01-31 08:14:34 +01002100
Jerome Anand5dab11d2017-01-25 04:27:52 +05302101err:
2102 snd_card_free(card);
Takashi Iwaida864802017-01-31 13:52:22 +01002103 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05302104}
2105
Takashi Iwai79dda752017-01-30 17:23:39 +01002106/*
Takashi Iwaida864802017-01-31 13:52:22 +01002107 * hdmi_lpe_audio_remove - stop bridge with i915
Jerome Anand5dab11d2017-01-25 04:27:52 +05302108 *
Takashi Iwaida864802017-01-31 13:52:22 +01002109 * This function is called when the platform device is destroyed. The sound
2110 * card should have been removed on hot plug event.
Jerome Anand5dab11d2017-01-25 04:27:52 +05302111 */
Takashi Iwaida864802017-01-31 13:52:22 +01002112static int hdmi_lpe_audio_remove(struct platform_device *pdev)
Jerome Anand5dab11d2017-01-25 04:27:52 +05302113{
Takashi Iwaida864802017-01-31 13:52:22 +01002114 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05302115
Takashi Iwaida864802017-01-31 13:52:22 +01002116 if (ctx->drv_status != HAD_DRV_DISCONNECTED)
2117 snd_intelhad_enable_audio_int(ctx, false);
2118 snd_card_free(ctx->card);
Jerome Anand5dab11d2017-01-25 04:27:52 +05302119 return 0;
2120}
2121
Takashi Iwaida864802017-01-31 13:52:22 +01002122static struct platform_driver hdmi_lpe_audio_driver = {
2123 .driver = {
2124 .name = "hdmi-lpe-audio",
2125 },
2126 .probe = hdmi_lpe_audio_probe,
2127 .remove = hdmi_lpe_audio_remove,
2128 .suspend = hdmi_lpe_audio_suspend,
2129 .resume = hdmi_lpe_audio_resume
2130};
2131
2132module_platform_driver(hdmi_lpe_audio_driver);
2133MODULE_ALIAS("platform:hdmi_lpe_audio");
2134
Jerome Anand5dab11d2017-01-25 04:27:52 +05302135MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
2136MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
2137MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
2138MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
2139MODULE_DESCRIPTION("Intel HDMI Audio driver");
2140MODULE_LICENSE("GPL v2");
2141MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");