blob: fbfbf5e2b5ad40f21a45687c23ed1da2a280dff6 [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;
596 intelhaddata->kctl = intelhaddata->chmap->kctl;
597 intelhaddata->kctl->info = had_chmap_ctl_info;
598 intelhaddata->kctl->get = had_chmap_ctl_get;
599 intelhaddata->chmap->chmap = NULL;
600 return 0;
601}
602
Takashi Iwai76296ef2017-01-30 16:09:11 +0100603/*
604 * snd_intelhad_prog_dip - to initialize Data Island Packets registers
Jerome Anand5dab11d2017-01-25 04:27:52 +0530605 *
606 * @substream:substream for which the prepare function is called
607 * @intelhaddata:substream private data
608 *
609 * This function is called in the prepare callback
610 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100611static void snd_intelhad_prog_dip(struct snd_pcm_substream *substream,
612 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530613{
614 int i;
615 union aud_ctrl_st ctrl_state = {.ctrl_val = 0};
616 union aud_info_frame2 frame2 = {.fr2_val = 0};
617 union aud_info_frame3 frame3 = {.fr3_val = 0};
618 u8 checksum = 0;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600619 u32 info_frame;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530620 int channels;
621
622 channels = substream->runtime->channels;
623
Takashi Iwai79dda752017-01-30 17:23:39 +0100624 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.ctrl_val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530625
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600626 if (intelhaddata->dp_output) {
627 info_frame = DP_INFO_FRAME_WORD1;
628 frame2.fr2_val = 1;
629 } else {
630 info_frame = HDMI_INFO_FRAME_WORD1;
631 frame2.fr2_regx.chnl_cnt = substream->runtime->channels - 1;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530632
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600633 frame3.fr3_regx.chnl_alloc = snd_intelhad_channel_allocation(
634 intelhaddata, channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530635
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600636 /*Calculte the byte wide checksum for all valid DIP words*/
637 for (i = 0; i < BYTES_PER_WORD; i++)
638 checksum += (info_frame >> i*BITS_PER_BYTE) & MASK_BYTE0;
639 for (i = 0; i < BYTES_PER_WORD; i++)
640 checksum += (frame2.fr2_val >> i*BITS_PER_BYTE) & MASK_BYTE0;
641 for (i = 0; i < BYTES_PER_WORD; i++)
642 checksum += (frame3.fr3_val >> i*BITS_PER_BYTE) & MASK_BYTE0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530643
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600644 frame2.fr2_regx.chksum = -(checksum);
645 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530646
Takashi Iwai79dda752017-01-30 17:23:39 +0100647 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, info_frame);
648 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, frame2.fr2_val);
649 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, frame3.fr3_val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530650
651 /* program remaining DIP words with zero */
652 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
Takashi Iwai79dda752017-01-30 17:23:39 +0100653 had_write_register(intelhaddata, AUD_HDMIW_INFOFR_v2, 0x0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530654
655 ctrl_state.ctrl_regx.dip_freq = 1;
656 ctrl_state.ctrl_regx.dip_en_sta = 1;
Takashi Iwai79dda752017-01-30 17:23:39 +0100657 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.ctrl_val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530658}
659
660/**
661 * snd_intelhad_prog_buffer - programs buffer
662 * address and length registers
663 *
664 * @substream:substream for which the prepare function is called
665 * @intelhaddata:substream private data
666 *
667 * This function programs ring buffer address and length into registers.
668 */
Takashi Iwai372d8552017-01-31 13:57:58 +0100669static int snd_intelhad_prog_buffer(struct snd_intelhad *intelhaddata,
Jerome Anand5dab11d2017-01-25 04:27:52 +0530670 int start, int end)
671{
672 u32 ring_buf_addr, ring_buf_size, period_bytes;
673 u8 i, num_periods;
674 struct snd_pcm_substream *substream;
675
676 substream = intelhaddata->stream_info.had_substream;
Takashi Iwaic75b0472017-01-31 15:49:15 +0100677 if (WARN_ON(!substream))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530678 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530679
680 ring_buf_addr = substream->runtime->dma_addr;
681 ring_buf_size = snd_pcm_lib_buffer_bytes(substream);
682 intelhaddata->stream_info.ring_buf_size = ring_buf_size;
683 period_bytes = frames_to_bytes(substream->runtime,
684 substream->runtime->period_size);
685 num_periods = substream->runtime->periods;
686
687 /*
688 * buffer addr should be 64 byte aligned, period bytes
689 * will be used to calculate addr offset
690 */
691 period_bytes &= ~0x3F;
692
693 /* Hardware supports MAX_PERIODS buffers */
694 if (end >= HAD_MAX_PERIODS)
695 return -EINVAL;
696
697 for (i = start; i <= end; i++) {
698 /* Program the buf registers with addr and len */
699 intelhaddata->buf_info[i].buf_addr = ring_buf_addr +
700 (i * period_bytes);
701 if (i < num_periods-1)
702 intelhaddata->buf_info[i].buf_size = period_bytes;
703 else
704 intelhaddata->buf_info[i].buf_size = ring_buf_size -
705 (period_bytes*i);
706
Takashi Iwai79dda752017-01-30 17:23:39 +0100707 had_write_register(intelhaddata,
708 AUD_BUF_A_ADDR + (i * HAD_REG_WIDTH),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530709 intelhaddata->buf_info[i].buf_addr |
710 BIT(0) | BIT(1));
Takashi Iwai79dda752017-01-30 17:23:39 +0100711 had_write_register(intelhaddata,
712 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530713 period_bytes);
714 intelhaddata->buf_info[i].is_valid = true;
715 }
Takashi Iwaic75b0472017-01-31 15:49:15 +0100716 dev_dbg(intelhaddata->dev, "%s:buf[%d-%d] addr=%#x and size=%d\n",
717 __func__, start, end,
718 intelhaddata->buf_info[start].buf_addr,
719 intelhaddata->buf_info[start].buf_size);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530720 intelhaddata->valid_buf_cnt = num_periods;
721 return 0;
722}
723
Takashi Iwai372d8552017-01-31 13:57:58 +0100724static int snd_intelhad_read_len(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530725{
726 int i, retval = 0;
727 u32 len[4];
728
729 for (i = 0; i < 4 ; i++) {
Takashi Iwai79dda752017-01-30 17:23:39 +0100730 had_read_register(intelhaddata,
731 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
732 &len[i]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530733 if (!len[i])
734 retval++;
735 }
736 if (retval != 1) {
737 for (i = 0; i < 4 ; i++)
Takashi Iwaic75b0472017-01-31 15:49:15 +0100738 dev_dbg(intelhaddata->dev, "buf[%d] size=%d\n",
739 i, len[i]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530740 }
741
742 return retval;
743}
744
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600745static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
746{
747 u32 maud_val;
748
749 /* Select maud according to DP 1.2 spec*/
750 if (link_rate == DP_2_7_GHZ) {
751 switch (aud_samp_freq) {
752 case AUD_SAMPLE_RATE_32:
753 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
754 break;
755
756 case AUD_SAMPLE_RATE_44_1:
757 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
758 break;
759
760 case AUD_SAMPLE_RATE_48:
761 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
762 break;
763
764 case AUD_SAMPLE_RATE_88_2:
765 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
766 break;
767
768 case AUD_SAMPLE_RATE_96:
769 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
770 break;
771
772 case AUD_SAMPLE_RATE_176_4:
773 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
774 break;
775
776 case HAD_MAX_RATE:
777 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
778 break;
779
780 default:
781 maud_val = -EINVAL;
782 break;
783 }
784 } else if (link_rate == DP_1_62_GHZ) {
785 switch (aud_samp_freq) {
786 case AUD_SAMPLE_RATE_32:
787 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
788 break;
789
790 case AUD_SAMPLE_RATE_44_1:
791 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
792 break;
793
794 case AUD_SAMPLE_RATE_48:
795 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
796 break;
797
798 case AUD_SAMPLE_RATE_88_2:
799 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
800 break;
801
802 case AUD_SAMPLE_RATE_96:
803 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
804 break;
805
806 case AUD_SAMPLE_RATE_176_4:
807 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
808 break;
809
810 case HAD_MAX_RATE:
811 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
812 break;
813
814 default:
815 maud_val = -EINVAL;
816 break;
817 }
818 } else
819 maud_val = -EINVAL;
820
821 return maud_val;
822}
823
Takashi Iwai76296ef2017-01-30 16:09:11 +0100824/*
825 * snd_intelhad_prog_cts - Program HDMI audio CTS value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530826 *
827 * @aud_samp_freq: sampling frequency of audio data
828 * @tmds: sampling frequency of the display data
829 * @n_param: N value, depends on aud_samp_freq
830 * @intelhaddata:substream private data
831 *
832 * Program CTS register based on the audio and display sampling frequency
833 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100834static void snd_intelhad_prog_cts(u32 aud_samp_freq, u32 tmds,
835 u32 link_rate, u32 n_param,
836 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530837{
838 u32 cts_val;
839 u64 dividend, divisor;
840
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600841 if (intelhaddata->dp_output) {
842 /* Substitute cts_val with Maud according to DP 1.2 spec*/
843 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
844 } else {
845 /* Calculate CTS according to HDMI 1.3a spec*/
846 dividend = (u64)tmds * n_param*1000;
847 divisor = 128 * aud_samp_freq;
848 cts_val = div64_u64(dividend, divisor);
849 }
Takashi Iwaic75b0472017-01-31 15:49:15 +0100850 dev_dbg(intelhaddata->dev, "TMDS value=%d, N value=%d, CTS Value=%d\n",
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600851 tmds, n_param, cts_val);
Takashi Iwai79dda752017-01-30 17:23:39 +0100852 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530853}
854
855static int had_calculate_n_value(u32 aud_samp_freq)
856{
857 s32 n_val;
858
859 /* Select N according to HDMI 1.3a spec*/
860 switch (aud_samp_freq) {
861 case AUD_SAMPLE_RATE_32:
862 n_val = 4096;
863 break;
864
865 case AUD_SAMPLE_RATE_44_1:
866 n_val = 6272;
867 break;
868
869 case AUD_SAMPLE_RATE_48:
870 n_val = 6144;
871 break;
872
873 case AUD_SAMPLE_RATE_88_2:
874 n_val = 12544;
875 break;
876
877 case AUD_SAMPLE_RATE_96:
878 n_val = 12288;
879 break;
880
881 case AUD_SAMPLE_RATE_176_4:
882 n_val = 25088;
883 break;
884
885 case HAD_MAX_RATE:
886 n_val = 24576;
887 break;
888
889 default:
890 n_val = -EINVAL;
891 break;
892 }
893 return n_val;
894}
895
Takashi Iwai76296ef2017-01-30 16:09:11 +0100896/*
897 * snd_intelhad_prog_n - Program HDMI audio N value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530898 *
899 * @aud_samp_freq: sampling frequency of audio data
900 * @n_param: N value, depends on aud_samp_freq
901 * @intelhaddata:substream private data
902 *
903 * This function is called in the prepare callback.
904 * It programs based on the audio and display sampling frequency
905 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100906static int snd_intelhad_prog_n(u32 aud_samp_freq, u32 *n_param,
907 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530908{
909 s32 n_val;
910
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600911 if (intelhaddata->dp_output) {
912 /*
913 * According to DP specs, Maud and Naud values hold
914 * a relationship, which is stated as:
915 * Maud/Naud = 512 * fs / f_LS_Clk
916 * where, fs is the sampling frequency of the audio stream
917 * and Naud is 32768 for Async clock.
918 */
919
920 n_val = DP_NAUD_VAL;
921 } else
922 n_val = had_calculate_n_value(aud_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530923
924 if (n_val < 0)
925 return n_val;
926
Takashi Iwai79dda752017-01-30 17:23:39 +0100927 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530928 *n_param = n_val;
929 return 0;
930}
931
Takashi Iwai372d8552017-01-31 13:57:58 +0100932static void snd_intelhad_handle_underrun(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530933{
934 u32 hdmi_status, i = 0;
935
936 /* Handle Underrun interrupt within Audio Unit */
Takashi Iwai79dda752017-01-30 17:23:39 +0100937 had_write_register(intelhaddata, AUD_CONFIG, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530938 /* Reset buffer pointers */
Takashi Iwai79dda752017-01-30 17:23:39 +0100939 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, 1);
940 had_write_register(intelhaddata, AUD_HDMI_STATUS_v2, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530941 /**
942 * The interrupt status 'sticky' bits might not be cleared by
943 * setting '1' to that bit once...
944 */
945 do { /* clear bit30, 31 AUD_HDMI_STATUS */
Takashi Iwai79dda752017-01-30 17:23:39 +0100946 had_read_register(intelhaddata, AUD_HDMI_STATUS_v2,
947 &hdmi_status);
Takashi Iwaic75b0472017-01-31 15:49:15 +0100948 dev_dbg(intelhaddata->dev, "HDMI status =0x%x\n", hdmi_status);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530949 if (hdmi_status & AUD_CONFIG_MASK_UNDERRUN) {
950 i++;
Takashi Iwai79dda752017-01-30 17:23:39 +0100951 had_write_register(intelhaddata,
952 AUD_HDMI_STATUS_v2, hdmi_status);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530953 } else
954 break;
955 } while (i < MAX_CNT);
956 if (i >= MAX_CNT)
Takashi Iwaic75b0472017-01-31 15:49:15 +0100957 dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n");
Jerome Anand5dab11d2017-01-25 04:27:52 +0530958}
959
960/**
961 * snd_intelhad_open - stream initializations are done here
962 * @substream:substream for which the stream function is called
963 *
964 * This function is called whenever a PCM stream is opened
965 */
966static int snd_intelhad_open(struct snd_pcm_substream *substream)
967{
968 struct snd_intelhad *intelhaddata;
969 struct snd_pcm_runtime *runtime;
970 struct had_stream_pvt *stream;
Takashi Iwai5647aec2017-01-31 08:14:34 +0100971 struct had_stream_data *had_stream;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530972 int retval;
973
Jerome Anand5dab11d2017-01-25 04:27:52 +0530974 intelhaddata = snd_pcm_substream_chip(substream);
Takashi Iwai5647aec2017-01-31 08:14:34 +0100975 had_stream = &intelhaddata->stream_data;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530976 runtime = substream->runtime;
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +0100977 intelhaddata->underrun_count = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530978
979 pm_runtime_get(intelhaddata->dev);
980
981 if (had_get_hwstate(intelhaddata)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +0100982 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
983 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530984 retval = -ENODEV;
985 goto exit_put_handle;
986 }
987
988 /* Check, if device already in use */
989 if (runtime->private_data) {
Takashi Iwaic75b0472017-01-31 15:49:15 +0100990 dev_dbg(intelhaddata->dev, "Device already in use\n");
Jerome Anand5dab11d2017-01-25 04:27:52 +0530991 retval = -EBUSY;
992 goto exit_put_handle;
993 }
994
995 /* set the runtime hw parameter with local snd_pcm_hardware struct */
996 runtime->hw = snd_intel_hadstream;
997
998 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
999 if (!stream) {
1000 retval = -ENOMEM;
1001 goto exit_put_handle;
1002 }
1003 stream->stream_status = STREAM_INIT;
1004 runtime->private_data = stream;
1005
1006 retval = snd_pcm_hw_constraint_integer(runtime,
1007 SNDRV_PCM_HW_PARAM_PERIODS);
1008 if (retval < 0)
1009 goto exit_err;
1010
1011 /* Make sure, that the period size is always aligned
1012 * 64byte boundary
1013 */
1014 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1015 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
1016 if (retval < 0) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001017 dev_dbg(intelhaddata->dev, "%s:step_size=64 failed,err=%d\n",
1018 __func__, retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301019 goto exit_err;
1020 }
1021
1022 return retval;
1023exit_err:
1024 kfree(stream);
1025exit_put_handle:
1026 pm_runtime_put(intelhaddata->dev);
1027 runtime->private_data = NULL;
1028 return retval;
1029}
1030
1031/**
1032 * had_period_elapsed - updates the hardware pointer status
1033 * @had_substream:substream for which the stream function is called
1034 *
1035 */
1036static void had_period_elapsed(void *had_substream)
1037{
1038 struct snd_pcm_substream *substream = had_substream;
1039 struct had_stream_pvt *stream;
1040
Jerome Anand5dab11d2017-01-25 04:27:52 +05301041 if (!substream || !substream->runtime)
1042 return;
1043 stream = substream->runtime->private_data;
1044 if (!stream)
1045 return;
1046
1047 if (stream->stream_status != STREAM_RUNNING)
1048 return;
1049 snd_pcm_period_elapsed(substream);
1050}
1051
1052/**
1053 * snd_intelhad_init_stream - internal function to initialize stream info
1054 * @substream:substream for which the stream function is called
1055 *
1056 */
1057static int snd_intelhad_init_stream(struct snd_pcm_substream *substream)
1058{
1059 struct snd_intelhad *intelhaddata = snd_pcm_substream_chip(substream);
1060
Jerome Anand5dab11d2017-01-25 04:27:52 +05301061 intelhaddata->stream_info.period_elapsed = had_period_elapsed;
1062 intelhaddata->stream_info.had_substream = substream;
1063 intelhaddata->stream_info.buffer_ptr = 0;
1064 intelhaddata->stream_info.buffer_rendered = 0;
1065 intelhaddata->stream_info.sfreq = substream->runtime->rate;
1066 return 0;
1067}
1068
1069/**
1070 * snd_intelhad_close- to free parameteres when stream is stopped
1071 *
1072 * @substream: substream for which the function is called
1073 *
1074 * This function is called by ALSA framework when stream is stopped
1075 */
1076static int snd_intelhad_close(struct snd_pcm_substream *substream)
1077{
1078 struct snd_intelhad *intelhaddata;
1079 struct snd_pcm_runtime *runtime;
1080
Jerome Anand5dab11d2017-01-25 04:27:52 +05301081 intelhaddata = snd_pcm_substream_chip(substream);
1082 runtime = substream->runtime;
1083
Takashi Iwaic75b0472017-01-31 15:49:15 +01001084 if (WARN_ON(!runtime->private_data))
Jerome Anand5dab11d2017-01-25 04:27:52 +05301085 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301086
1087 intelhaddata->stream_info.buffer_rendered = 0;
1088 intelhaddata->stream_info.buffer_ptr = 0;
1089 intelhaddata->stream_info.str_id = 0;
1090 intelhaddata->stream_info.had_substream = NULL;
1091
1092 /* Check if following drv_status modification is required - VA */
1093 if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) {
1094 intelhaddata->drv_status = HAD_DRV_CONNECTED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001095 dev_dbg(intelhaddata->dev,
1096 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301097 __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{
Takashi Iwaic75b0472017-01-31 15:49:15 +01001117 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301118 unsigned long addr;
1119 int pages, buf_size, retval;
1120
Jerome Anand5dab11d2017-01-25 04:27:52 +05301121 if (!hw_params)
1122 return -EINVAL;
1123
Takashi Iwaic75b0472017-01-31 15:49:15 +01001124 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301125 buf_size = params_buffer_bytes(hw_params);
1126 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1127 if (retval < 0)
1128 return retval;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001129 dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
1130 __func__, buf_size);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301131 /* mark the pages as uncached region */
1132 addr = (unsigned long) substream->runtime->dma_area;
1133 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1134 retval = set_memory_uc(addr, pages);
1135 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001136 dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
1137 retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301138 return retval;
1139 }
1140 memset(substream->runtime->dma_area, 0, buf_size);
1141
1142 return retval;
1143}
1144
1145/**
1146 * snd_intelhad_hw_free- to release the resources allocated during
1147 * hardware params setup
1148 *
1149 * @substream: substream for which the function is called
1150 *
1151 * This function is called by ALSA framework before close callback.
1152 *
1153 */
1154static int snd_intelhad_hw_free(struct snd_pcm_substream *substream)
1155{
1156 unsigned long addr;
1157 u32 pages;
1158
Jerome Anand5dab11d2017-01-25 04:27:52 +05301159 /* 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{
Takashi Iwaida864802017-01-31 13:52:22 +01001179 int retval = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301180 unsigned long flag_irq;
1181 struct snd_intelhad *intelhaddata;
1182 struct had_stream_pvt *stream;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001183 struct had_stream_data *had_stream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301184
Jerome Anand5dab11d2017-01-25 04:27:52 +05301185 intelhaddata = snd_pcm_substream_chip(substream);
1186 stream = substream->runtime->private_data;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001187 had_stream = &intelhaddata->stream_data;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301188
1189 switch (cmd) {
1190 case SNDRV_PCM_TRIGGER_START:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301191 /* Disable local INTRs till register prgmng is done */
1192 if (had_get_hwstate(intelhaddata)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001193 dev_dbg(intelhaddata->dev,
1194 "_START: HDMI cable plugged-out\n");
Jerome Anand5dab11d2017-01-25 04:27:52 +05301195 retval = -ENODEV;
1196 break;
1197 }
1198 stream->stream_status = STREAM_RUNNING;
1199
1200 had_stream->stream_type = HAD_RUNNING_STREAM;
1201
1202 /* Enable Audio */
Takashi Iwaida864802017-01-31 13:52:22 +01001203 snd_intelhad_enable_audio_int(intelhaddata, true);
1204 snd_intelhad_enable_audio(intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301205 break;
1206
1207 case SNDRV_PCM_TRIGGER_STOP:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301208 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irq);
1209 intelhaddata->stream_info.str_id = 0;
1210 intelhaddata->curr_buf = 0;
1211
Takashi Iwaic75b0472017-01-31 15:49:15 +01001212 /* Stop reporting BUFFER_DONE/UNDERRUN to above layers */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301213
1214 had_stream->stream_type = HAD_INIT;
1215 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irq);
1216 /* Disable Audio */
Takashi Iwaida864802017-01-31 13:52:22 +01001217 snd_intelhad_enable_audio_int(intelhaddata, false);
1218 snd_intelhad_enable_audio(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301219 /* Reset buffer pointers */
Takashi Iwai79dda752017-01-30 17:23:39 +01001220 snd_intelhad_reset_audio(intelhaddata, 1);
1221 snd_intelhad_reset_audio(intelhaddata, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301222 stream->stream_status = STREAM_DROPPED;
Takashi Iwaida864802017-01-31 13:52:22 +01001223 snd_intelhad_enable_audio_int(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301224 break;
1225
1226 default:
1227 retval = -EINVAL;
1228 }
1229 return retval;
1230}
1231
1232/**
1233 * snd_intelhad_pcm_prepare- internal preparation before starting a stream
1234 *
1235 * @substream: substream for which the function is called
1236 *
1237 * This function is called when a stream is started for internal preparation.
1238 */
1239static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream)
1240{
1241 int retval;
1242 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001243 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301244 struct snd_intelhad *intelhaddata;
1245 struct snd_pcm_runtime *runtime;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001246 struct had_stream_data *had_stream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301247
Jerome Anand5dab11d2017-01-25 04:27:52 +05301248 intelhaddata = snd_pcm_substream_chip(substream);
1249 runtime = substream->runtime;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001250 had_stream = &intelhaddata->stream_data;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301251
1252 if (had_get_hwstate(intelhaddata)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001253 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
1254 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301255 retval = -ENODEV;
1256 goto prep_end;
1257 }
1258
Takashi Iwaic75b0472017-01-31 15:49:15 +01001259 dev_dbg(intelhaddata->dev, "period_size=%d\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301260 (int)frames_to_bytes(runtime, runtime->period_size));
Takashi Iwaic75b0472017-01-31 15:49:15 +01001261 dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods);
1262 dev_dbg(intelhaddata->dev, "buffer_size=%d\n",
1263 (int)snd_pcm_lib_buffer_bytes(substream));
1264 dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
1265 dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301266
1267 if (intelhaddata->stream_info.str_id) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001268 dev_dbg(intelhaddata->dev,
1269 "_prepare is called for existing str_id#%d\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301270 intelhaddata->stream_info.str_id);
1271 retval = snd_intelhad_pcm_trigger(substream,
1272 SNDRV_PCM_TRIGGER_STOP);
1273 return retval;
1274 }
1275
1276 retval = snd_intelhad_init_stream(substream);
1277 if (retval)
1278 goto prep_end;
1279
1280
1281 /* Get N value in KHz */
Takashi Iwaida864802017-01-31 13:52:22 +01001282 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301283
Takashi Iwai76296ef2017-01-30 16:09:11 +01001284 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1285 intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301286 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001287 dev_err(intelhaddata->dev,
1288 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301289 goto prep_end;
1290 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001291
1292 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001293 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001294
Takashi Iwai76296ef2017-01-30 16:09:11 +01001295 snd_intelhad_prog_cts(substream->runtime->rate,
1296 disp_samp_freq, link_rate,
1297 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301298
Takashi Iwai76296ef2017-01-30 16:09:11 +01001299 snd_intelhad_prog_dip(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301300
Takashi Iwai76296ef2017-01-30 16:09:11 +01001301 retval = snd_intelhad_audio_ctrl(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301302
1303 /* Prog buffer address */
1304 retval = snd_intelhad_prog_buffer(intelhaddata,
1305 HAD_BUF_TYPE_A, HAD_BUF_TYPE_D);
1306
1307 /*
1308 * Program channel mapping in following order:
1309 * FL, FR, C, LFE, RL, RR
1310 */
1311
Takashi Iwai79dda752017-01-30 17:23:39 +01001312 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301313
1314prep_end:
1315 return retval;
1316}
1317
1318/**
1319 * snd_intelhad_pcm_pointer- to send the current buffer pointerprocessed by hw
1320 *
1321 * @substream: substream for which the function is called
1322 *
1323 * This function is called by ALSA framework to get the current hw buffer ptr
1324 * when a period is elapsed
1325 */
1326static snd_pcm_uframes_t snd_intelhad_pcm_pointer(
1327 struct snd_pcm_substream *substream)
1328{
1329 struct snd_intelhad *intelhaddata;
1330 u32 bytes_rendered = 0;
1331 u32 t;
1332 int buf_id;
1333
Jerome Anand5dab11d2017-01-25 04:27:52 +05301334 intelhaddata = snd_pcm_substream_chip(substream);
1335
1336 if (intelhaddata->flag_underrun) {
1337 intelhaddata->flag_underrun = 0;
1338 return SNDRV_PCM_POS_XRUN;
1339 }
1340
1341 /* Use a hw register to calculate sub-period position reports.
1342 * This makes PulseAudio happier.
1343 */
1344
1345 buf_id = intelhaddata->curr_buf % 4;
Takashi Iwai79dda752017-01-30 17:23:39 +01001346 had_read_register(intelhaddata,
1347 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH), &t);
Jerome Anand232892f2017-01-25 04:27:53 +05301348
1349 if ((t == 0) || (t == ((u32)-1L))) {
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001350 intelhaddata->underrun_count++;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001351 dev_dbg(intelhaddata->dev,
1352 "discovered buffer done for buf %d, count = %d\n",
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001353 buf_id, intelhaddata->underrun_count);
Jerome Anand232892f2017-01-25 04:27:53 +05301354
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001355 if (intelhaddata->underrun_count > (HAD_MIN_PERIODS/2)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001356 dev_dbg(intelhaddata->dev,
1357 "assume audio_codec_reset, underrun = %d - do xrun\n",
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001358 intelhaddata->underrun_count);
1359 intelhaddata->underrun_count = 0;
Jerome Anand232892f2017-01-25 04:27:53 +05301360 return SNDRV_PCM_POS_XRUN;
1361 }
1362 } else {
1363 /* Reset Counter */
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001364 intelhaddata->underrun_count = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301365 }
Jerome Anand232892f2017-01-25 04:27:53 +05301366
Jerome Anand5dab11d2017-01-25 04:27:52 +05301367 t = intelhaddata->buf_info[buf_id].buf_size - t;
1368
1369 if (intelhaddata->stream_info.buffer_rendered)
1370 div_u64_rem(intelhaddata->stream_info.buffer_rendered,
1371 intelhaddata->stream_info.ring_buf_size,
1372 &(bytes_rendered));
1373
1374 intelhaddata->stream_info.buffer_ptr = bytes_to_frames(
1375 substream->runtime,
1376 bytes_rendered + t);
1377 return intelhaddata->stream_info.buffer_ptr;
1378}
1379
1380/**
1381 * snd_intelhad_pcm_mmap- mmaps a kernel buffer to user space for copying data
1382 *
1383 * @substream: substream for which the function is called
1384 * @vma: struct instance of memory VMM memory area
1385 *
1386 * This function is called by OS when a user space component
1387 * tries to get mmap memory from driver
1388 */
1389static int snd_intelhad_pcm_mmap(struct snd_pcm_substream *substream,
1390 struct vm_area_struct *vma)
1391{
Jerome Anand5dab11d2017-01-25 04:27:52 +05301392 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1393 return remap_pfn_range(vma, vma->vm_start,
1394 substream->dma_buffer.addr >> PAGE_SHIFT,
1395 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1396}
1397
Takashi Iwaida864802017-01-31 13:52:22 +01001398static int hdmi_audio_mode_change(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301399{
Takashi Iwaida864802017-01-31 13:52:22 +01001400 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301401 int retval = 0;
1402 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001403 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301404
Takashi Iwaida864802017-01-31 13:52:22 +01001405 substream = intelhaddata->stream_info.had_substream;
1406 if (!substream || !substream->runtime)
1407 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301408
1409 /* Disable Audio */
Takashi Iwaida864802017-01-31 13:52:22 +01001410 snd_intelhad_enable_audio(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301411
1412 /* Update CTS value */
Takashi Iwaida864802017-01-31 13:52:22 +01001413 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301414
Takashi Iwai76296ef2017-01-30 16:09:11 +01001415 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1416 intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301417 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001418 dev_err(intelhaddata->dev,
1419 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301420 goto out;
1421 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001422
1423 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001424 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001425
Takashi Iwai76296ef2017-01-30 16:09:11 +01001426 snd_intelhad_prog_cts(substream->runtime->rate,
1427 disp_samp_freq, link_rate,
1428 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301429
1430 /* Enable Audio */
Takashi Iwaida864802017-01-31 13:52:22 +01001431 snd_intelhad_enable_audio(intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301432
1433out:
1434 return retval;
1435}
1436
Takashi Iwai372d8552017-01-31 13:57:58 +01001437/*
1438 * hdmi_lpe_audio_suspend - power management suspend function
1439 *
1440 * @pdev: platform device
1441 *
1442 * This function is called by client driver to suspend the
1443 * hdmi audio.
1444 */
1445static int hdmi_lpe_audio_suspend(struct platform_device *pdev,
1446 pm_message_t state)
1447{
1448 struct had_stream_data *had_stream;
1449 unsigned long flag_irqs;
1450 struct snd_pcm_substream *substream;
1451 struct snd_intelhad *intelhaddata = platform_get_drvdata(pdev);
1452
Takashi Iwai372d8552017-01-31 13:57:58 +01001453 had_stream = &intelhaddata->stream_data;
1454 substream = intelhaddata->stream_info.had_substream;
1455
Takashi Iwaie29c0f92017-02-01 17:27:48 +01001456 if (!pm_runtime_status_suspended(intelhaddata->dev)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001457 dev_err(intelhaddata->dev, "audio stream is active\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001458 return -EAGAIN;
1459 }
1460
1461
1462 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1463 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
1464 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001465 dev_dbg(intelhaddata->dev, "had not connected\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001466 return 0;
1467 }
1468
1469 if (intelhaddata->drv_status == HAD_DRV_SUSPENDED) {
1470 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001471 dev_dbg(intelhaddata->dev, "had already suspended\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001472 return 0;
1473 }
1474
1475 intelhaddata->drv_status = HAD_DRV_SUSPENDED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001476 dev_dbg(intelhaddata->dev,
1477 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_SUSPENDED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001478 __func__, __LINE__);
1479
1480 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1481 snd_intelhad_enable_audio_int(intelhaddata, false);
Takashi Iwai372d8552017-01-31 13:57:58 +01001482 return 0;
1483}
1484
1485/*
1486 * hdmi_lpe_audio_resume - power management resume function
1487 *
1488 *@pdev: platform device
1489 *
1490 * This function is called by client driver to resume the
1491 * hdmi audio.
1492 */
1493static int hdmi_lpe_audio_resume(struct platform_device *pdev)
1494{
1495 struct snd_intelhad *intelhaddata = platform_get_drvdata(pdev);
1496 unsigned long flag_irqs;
1497
Takashi Iwai372d8552017-01-31 13:57:58 +01001498 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1499 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
1500 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001501 dev_dbg(intelhaddata->dev, "had not connected\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001502 return 0;
1503 }
1504
1505 if (intelhaddata->drv_status != HAD_DRV_SUSPENDED) {
1506 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001507 dev_err(intelhaddata->dev, "had is not in suspended state\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001508 return 0;
1509 }
1510
1511 if (had_get_hwstate(intelhaddata)) {
1512 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001513 dev_err(intelhaddata->dev,
1514 "Failed to resume. Device not accessible\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001515 return -ENODEV;
1516 }
1517
1518 intelhaddata->drv_status = HAD_DRV_CONNECTED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001519 dev_dbg(intelhaddata->dev,
1520 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001521 __func__, __LINE__);
1522 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1523 snd_intelhad_enable_audio_int(intelhaddata, true);
Takashi Iwai372d8552017-01-31 13:57:58 +01001524 return 0;
1525}
1526
1527static inline int had_chk_intrmiss(struct snd_intelhad *intelhaddata,
1528 enum intel_had_aud_buf_type buf_id)
1529{
1530 int i, intr_count = 0;
1531 enum intel_had_aud_buf_type buff_done;
1532 u32 buf_size, buf_addr;
1533 struct had_stream_data *had_stream;
1534 unsigned long flag_irqs;
1535
1536 had_stream = &intelhaddata->stream_data;
1537
1538 buff_done = buf_id;
1539
1540 intr_count = snd_intelhad_read_len(intelhaddata);
1541 if (intr_count > 1) {
1542 /* In case of active playback */
Takashi Iwaic75b0472017-01-31 15:49:15 +01001543 dev_err(intelhaddata->dev,
1544 "Driver detected %d missed buffer done interrupt(s)\n",
1545 (intr_count - 1));
Takashi Iwai372d8552017-01-31 13:57:58 +01001546 if (intr_count > 3)
1547 return intr_count;
1548
1549 buf_id += (intr_count - 1);
1550 /* Reprogram registers*/
1551 for (i = buff_done; i < buf_id; i++) {
1552 int j = i % 4;
1553
1554 buf_size = intelhaddata->buf_info[j].buf_size;
1555 buf_addr = intelhaddata->buf_info[j].buf_addr;
1556 had_write_register(intelhaddata,
1557 AUD_BUF_A_LENGTH +
1558 (j * HAD_REG_WIDTH), buf_size);
1559 had_write_register(intelhaddata,
1560 AUD_BUF_A_ADDR+(j * HAD_REG_WIDTH),
1561 (buf_addr | BIT(0) | BIT(1)));
1562 }
1563 buf_id = buf_id % 4;
1564 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1565 intelhaddata->buff_done = buf_id;
1566 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1567 }
1568
1569 return intr_count;
1570}
1571
1572static int had_process_buffer_done(struct snd_intelhad *intelhaddata)
1573{
1574 u32 len = 1;
1575 enum intel_had_aud_buf_type buf_id;
1576 enum intel_had_aud_buf_type buff_done;
1577 struct pcm_stream_info *stream;
1578 u32 buf_size;
1579 struct had_stream_data *had_stream;
1580 int intr_count;
1581 enum had_status_stream stream_type;
1582 unsigned long flag_irqs;
1583
1584 had_stream = &intelhaddata->stream_data;
1585 stream = &intelhaddata->stream_info;
1586 intr_count = 1;
1587
1588 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1589 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
1590 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001591 dev_dbg(intelhaddata->dev,
1592 "%s:Device already disconnected\n", __func__);
Takashi Iwai372d8552017-01-31 13:57:58 +01001593 return 0;
1594 }
1595 buf_id = intelhaddata->curr_buf;
1596 intelhaddata->buff_done = buf_id;
1597 buff_done = intelhaddata->buff_done;
1598 buf_size = intelhaddata->buf_info[buf_id].buf_size;
1599 stream_type = had_stream->stream_type;
1600
Takashi Iwai372d8552017-01-31 13:57:58 +01001601 /* Every debug statement has an implication
1602 * of ~5msec. Thus, avoid having >3 debug statements
1603 * for each buffer_done handling.
1604 */
1605
1606 /* Check for any intr_miss in case of active playback */
1607 if (had_stream->stream_type == HAD_RUNNING_STREAM) {
1608 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1609 intr_count = had_chk_intrmiss(intelhaddata, buf_id);
1610 if (!intr_count || (intr_count > 3)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001611 dev_err(intelhaddata->dev,
1612 "HAD SW state in non-recoverable mode\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001613 return 0;
1614 }
1615 buf_id += (intr_count - 1);
1616 buf_id = buf_id % 4;
1617 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1618 }
1619
1620 intelhaddata->buf_info[buf_id].is_valid = true;
1621 if (intelhaddata->valid_buf_cnt-1 == buf_id) {
1622 if (had_stream->stream_type >= HAD_RUNNING_STREAM)
1623 intelhaddata->curr_buf = HAD_BUF_TYPE_A;
1624 } else
1625 intelhaddata->curr_buf = buf_id + 1;
1626
1627 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1628
1629 if (had_get_hwstate(intelhaddata)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001630 dev_dbg(intelhaddata->dev, "HDMI cable plugged-out\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001631 return 0;
1632 }
1633
1634 /*Reprogram the registers with addr and length*/
1635 had_write_register(intelhaddata,
1636 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH),
1637 buf_size);
1638 had_write_register(intelhaddata,
1639 AUD_BUF_A_ADDR + (buf_id * HAD_REG_WIDTH),
1640 intelhaddata->buf_info[buf_id].buf_addr |
1641 BIT(0) | BIT(1));
1642
1643 had_read_register(intelhaddata,
1644 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH),
1645 &len);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001646 dev_dbg(intelhaddata->dev, "%s:Enabled buf[%d]\n", __func__, buf_id);
Takashi Iwai372d8552017-01-31 13:57:58 +01001647
1648 /* In case of actual data,
1649 * report buffer_done to above ALSA layer
1650 */
1651 buf_size = intelhaddata->buf_info[buf_id].buf_size;
1652 if (stream_type >= HAD_RUNNING_STREAM) {
1653 intelhaddata->stream_info.buffer_rendered +=
1654 (intr_count * buf_size);
1655 stream->period_elapsed(stream->had_substream);
1656 }
1657
1658 return 0;
1659}
1660
1661static int had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
1662{
1663 enum intel_had_aud_buf_type buf_id;
1664 struct pcm_stream_info *stream;
1665 struct had_stream_data *had_stream;
1666 enum had_status_stream stream_type;
1667 unsigned long flag_irqs;
1668 int drv_status;
1669
1670 had_stream = &intelhaddata->stream_data;
1671 stream = &intelhaddata->stream_info;
1672
1673 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1674 buf_id = intelhaddata->curr_buf;
1675 stream_type = had_stream->stream_type;
1676 intelhaddata->buff_done = buf_id;
1677 drv_status = intelhaddata->drv_status;
1678 if (stream_type == HAD_RUNNING_STREAM)
1679 intelhaddata->curr_buf = HAD_BUF_TYPE_A;
1680
1681 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1682
Takashi Iwaic75b0472017-01-31 15:49:15 +01001683 dev_dbg(intelhaddata->dev, "Enter:%s buf_id=%d, stream_type=%d\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001684 __func__, buf_id, stream_type);
1685
1686 snd_intelhad_handle_underrun(intelhaddata);
1687
1688 if (drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001689 dev_dbg(intelhaddata->dev,
1690 "%s:Device already disconnected\n", __func__);
Takashi Iwai372d8552017-01-31 13:57:58 +01001691 return 0;
1692 }
1693
1694 if (stream_type == HAD_RUNNING_STREAM) {
1695 /* Report UNDERRUN error to above layers */
1696 intelhaddata->flag_underrun = 1;
1697 stream->period_elapsed(stream->had_substream);
1698 }
1699
1700 return 0;
1701}
1702
1703static int had_process_hot_plug(struct snd_intelhad *intelhaddata)
1704{
1705 enum intel_had_aud_buf_type buf_id;
1706 struct snd_pcm_substream *substream;
1707 struct had_stream_data *had_stream;
1708 unsigned long flag_irqs;
1709
Takashi Iwai372d8552017-01-31 13:57:58 +01001710 substream = intelhaddata->stream_info.had_substream;
1711 had_stream = &intelhaddata->stream_data;
1712
1713 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1714 if (intelhaddata->drv_status == HAD_DRV_CONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001715 dev_dbg(intelhaddata->dev, "Device already connected\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001716 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1717 return 0;
1718 }
1719 buf_id = intelhaddata->curr_buf;
1720 intelhaddata->buff_done = buf_id;
1721 intelhaddata->drv_status = HAD_DRV_CONNECTED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001722 dev_dbg(intelhaddata->dev,
1723 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001724 __func__, __LINE__);
1725 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1726
Takashi Iwaic75b0472017-01-31 15:49:15 +01001727 dev_dbg(intelhaddata->dev, "Processing HOT_PLUG, buf_id = %d\n",
1728 buf_id);
Takashi Iwai372d8552017-01-31 13:57:58 +01001729
1730 /* Safety check */
1731 if (substream) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001732 dev_dbg(intelhaddata->dev,
1733 "Force to stop the active stream by disconnection\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001734 /* Set runtime->state to hw_params done */
1735 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1736 }
1737
1738 had_build_channel_allocation_map(intelhaddata);
1739
1740 return 0;
1741}
1742
1743static int had_process_hot_unplug(struct snd_intelhad *intelhaddata)
1744{
1745 enum intel_had_aud_buf_type buf_id;
1746 struct had_stream_data *had_stream;
1747 unsigned long flag_irqs;
1748
Takashi Iwai372d8552017-01-31 13:57:58 +01001749 had_stream = &intelhaddata->stream_data;
1750 buf_id = intelhaddata->curr_buf;
1751
1752 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1753
1754 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001755 dev_dbg(intelhaddata->dev, "Device already disconnected\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001756 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1757 return 0;
1758
1759 } else {
1760 /* Disable Audio */
1761 snd_intelhad_enable_audio_int(intelhaddata, false);
1762 snd_intelhad_enable_audio(intelhaddata, false);
1763 }
1764
1765 intelhaddata->drv_status = HAD_DRV_DISCONNECTED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001766 dev_dbg(intelhaddata->dev,
1767 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001768 __func__, __LINE__);
1769
1770 /* Report to above ALSA layer */
1771 if (intelhaddata->stream_info.had_substream != NULL) {
1772 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
Takashi Iwai372d8552017-01-31 13:57:58 +01001773 snd_pcm_stop(intelhaddata->stream_info.had_substream,
1774 SNDRV_PCM_STATE_SETUP);
1775 spin_lock_irqsave(&intelhaddata->had_spinlock, flag_irqs);
1776 }
1777
1778 had_stream->stream_type = HAD_INIT;
1779 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flag_irqs);
1780 kfree(intelhaddata->chmap->chmap);
1781 intelhaddata->chmap->chmap = NULL;
1782 intelhaddata->audio_reg_base = NULL;
Takashi Iwai372d8552017-01-31 13:57:58 +01001783
1784 return 0;
1785}
1786
1787/* PCM operations structure and the calls back for the same */
1788static struct snd_pcm_ops snd_intelhad_playback_ops = {
Jerome Anand5dab11d2017-01-25 04:27:52 +05301789 .open = snd_intelhad_open,
1790 .close = snd_intelhad_close,
1791 .ioctl = snd_pcm_lib_ioctl,
1792 .hw_params = snd_intelhad_hw_params,
1793 .hw_free = snd_intelhad_hw_free,
1794 .prepare = snd_intelhad_pcm_prepare,
1795 .trigger = snd_intelhad_pcm_trigger,
1796 .pointer = snd_intelhad_pcm_pointer,
1797 .mmap = snd_intelhad_pcm_mmap,
1798};
1799
Takashi Iwai372d8552017-01-31 13:57:58 +01001800/*
Jerome Anand5dab11d2017-01-25 04:27:52 +05301801 * snd_intelhad_pcm_free - to free the memory allocated
1802 *
1803 * @pcm: pointer to pcm instance
1804 * This function is called when the device is removed
1805 */
1806static void snd_intelhad_pcm_free(struct snd_pcm *pcm)
1807{
Jerome Anand5dab11d2017-01-25 04:27:52 +05301808 snd_pcm_lib_preallocate_free_for_all(pcm);
1809}
1810
1811static int had_iec958_info(struct snd_kcontrol *kcontrol,
1812 struct snd_ctl_elem_info *uinfo)
1813{
1814 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1815 uinfo->count = 1;
1816 return 0;
1817}
1818
1819static int had_iec958_get(struct snd_kcontrol *kcontrol,
1820 struct snd_ctl_elem_value *ucontrol)
1821{
1822 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1823
1824 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1825 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1826 ucontrol->value.iec958.status[2] =
1827 (intelhaddata->aes_bits >> 16) & 0xff;
1828 ucontrol->value.iec958.status[3] =
1829 (intelhaddata->aes_bits >> 24) & 0xff;
1830 return 0;
1831}
Takashi Iwai372d8552017-01-31 13:57:58 +01001832
Jerome Anand5dab11d2017-01-25 04:27:52 +05301833static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1834 struct snd_ctl_elem_value *ucontrol)
1835{
1836 ucontrol->value.iec958.status[0] = 0xff;
1837 ucontrol->value.iec958.status[1] = 0xff;
1838 ucontrol->value.iec958.status[2] = 0xff;
1839 ucontrol->value.iec958.status[3] = 0xff;
1840 return 0;
1841}
Takashi Iwai372d8552017-01-31 13:57:58 +01001842
Jerome Anand5dab11d2017-01-25 04:27:52 +05301843static int had_iec958_put(struct snd_kcontrol *kcontrol,
1844 struct snd_ctl_elem_value *ucontrol)
1845{
1846 unsigned int val;
1847 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1848
Jerome Anand5dab11d2017-01-25 04:27:52 +05301849 val = (ucontrol->value.iec958.status[0] << 0) |
1850 (ucontrol->value.iec958.status[1] << 8) |
1851 (ucontrol->value.iec958.status[2] << 16) |
1852 (ucontrol->value.iec958.status[3] << 24);
1853 if (intelhaddata->aes_bits != val) {
1854 intelhaddata->aes_bits = val;
1855 return 1;
1856 }
1857 return 1;
1858}
1859
1860static struct snd_kcontrol_new had_control_iec958_mask = {
1861 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1862 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1863 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1864 .info = had_iec958_info, /* shared */
1865 .get = had_iec958_mask_get,
1866};
1867
1868static struct snd_kcontrol_new had_control_iec958 = {
1869 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1870 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1871 .info = had_iec958_info,
1872 .get = had_iec958_get,
1873 .put = had_iec958_put
1874};
1875
Takashi Iwaida864802017-01-31 13:52:22 +01001876static void _had_wq(struct work_struct *work)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301877{
Takashi Iwaida864802017-01-31 13:52:22 +01001878 struct snd_intelhad *ctx =
1879 container_of(work, struct snd_intelhad, hdmi_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301880
Takashi Iwaida864802017-01-31 13:52:22 +01001881 had_process_hot_plug(ctx);
1882}
1883
1884static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id)
1885{
1886 struct snd_intelhad *ctx = dev_id;
1887 u32 audio_stat, audio_reg;
1888
1889 audio_reg = AUD_HDMI_STATUS_v2;
1890 mid_hdmi_audio_read(ctx, audio_reg, &audio_stat);
1891
1892 if (audio_stat & HDMI_AUDIO_UNDERRUN) {
1893 mid_hdmi_audio_write(ctx, audio_reg, HDMI_AUDIO_UNDERRUN);
1894 had_process_buffer_underrun(ctx);
1895 }
1896
1897 if (audio_stat & HDMI_AUDIO_BUFFER_DONE) {
1898 mid_hdmi_audio_write(ctx, audio_reg, HDMI_AUDIO_BUFFER_DONE);
1899 had_process_buffer_done(ctx);
1900 }
1901
1902 return IRQ_HANDLED;
1903}
1904
1905static void notify_audio_lpe(struct platform_device *pdev)
1906{
1907 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
1908 struct intel_hdmi_lpe_audio_pdata *pdata = pdev->dev.platform_data;
1909
1910 if (pdata->hdmi_connected != true) {
1911
1912 dev_dbg(&pdev->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG\n",
1913 __func__);
1914
1915 if (ctx->state == hdmi_connector_status_connected) {
1916
1917 ctx->state = hdmi_connector_status_disconnected;
1918
1919 had_process_hot_unplug(ctx);
1920 } else
1921 dev_dbg(&pdev->dev, "%s: Already Unplugged!\n",
1922 __func__);
1923
1924 } else {
1925 struct intel_hdmi_lpe_audio_eld *eld = &pdata->eld;
1926
1927 switch (eld->pipe_id) {
1928 case 0:
1929 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1930 break;
1931 case 1:
1932 ctx->had_config_offset = AUDIO_HDMI_CONFIG_B;
1933 break;
1934 case 2:
1935 ctx->had_config_offset = AUDIO_HDMI_CONFIG_C;
1936 break;
1937 default:
1938 dev_dbg(&pdev->dev, "Invalid pipe %d\n",
1939 eld->pipe_id);
1940 break;
1941 }
1942
1943 memcpy(&ctx->eld, eld->eld_data, sizeof(ctx->eld));
1944
1945 had_process_hot_plug(ctx);
1946
1947 ctx->state = hdmi_connector_status_connected;
1948
1949 dev_dbg(&pdev->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
1950 __func__, eld->port_id, pdata->tmds_clock_speed);
1951
1952 if (pdata->tmds_clock_speed) {
1953 ctx->tmds_clock_speed = pdata->tmds_clock_speed;
1954 ctx->dp_output = pdata->dp_output;
1955 ctx->link_rate = pdata->link_rate;
1956
1957 /* Process mode change if stream is active */
1958 if (ctx->stream_data.stream_type == HAD_RUNNING_STREAM)
1959 hdmi_audio_mode_change(ctx);
1960 }
1961 }
1962}
1963
1964/* release resources */
1965static void hdmi_lpe_audio_free(struct snd_card *card)
1966{
1967 struct snd_intelhad *ctx = card->private_data;
1968
1969 if (ctx->mmio_start)
1970 iounmap(ctx->mmio_start);
1971 if (ctx->irq >= 0)
1972 free_irq(ctx->irq, ctx);
1973}
1974
1975/*
1976 * hdmi_lpe_audio_probe - start bridge with i915
1977 *
1978 * This function is called when the i915 driver creates the
1979 * hdmi-lpe-audio platform device. Card creation is deferred until a
1980 * hot plug event is received
1981 */
1982static int hdmi_lpe_audio_probe(struct platform_device *pdev)
1983{
1984 struct snd_card *card;
1985 struct snd_intelhad *ctx;
1986 struct snd_pcm *pcm;
1987 struct intel_hdmi_lpe_audio_pdata *pdata;
1988 int irq;
1989 struct resource *res_mmio;
1990 int ret;
1991 unsigned long flags;
1992
Takashi Iwaida864802017-01-31 13:52:22 +01001993 dev_dbg(&pdev->dev, "dma_mask: %p\n", pdev->dev.dma_mask);
1994
1995 pdata = pdev->dev.platform_data;
1996 if (!pdata) {
1997 dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
1998 return -EINVAL;
1999 }
2000
2001 /* get resources */
2002 irq = platform_get_irq(pdev, 0);
2003 if (irq < 0) {
2004 dev_err(&pdev->dev, "Could not get irq resource\n");
2005 return -ENODEV;
2006 }
2007
2008 res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2009 if (!res_mmio) {
2010 dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
2011 return -ENXIO;
2012 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05302013
Takashi Iwai5647aec2017-01-31 08:14:34 +01002014 /* create a card instance with ALSA framework */
Takashi Iwaida864802017-01-31 13:52:22 +01002015 ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
2016 THIS_MODULE, sizeof(*ctx), &card);
2017 if (ret)
2018 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05302019
Takashi Iwaida864802017-01-31 13:52:22 +01002020 ctx = card->private_data;
2021 spin_lock_init(&ctx->had_spinlock);
2022 ctx->drv_status = HAD_DRV_DISCONNECTED;
2023 ctx->dev = &pdev->dev;
2024 ctx->card = card;
2025 ctx->card_id = hdmi_card_id;
2026 ctx->card_index = card->number;
2027 ctx->flag_underrun = 0;
2028 ctx->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
2029 strcpy(card->driver, INTEL_HAD);
2030 strcpy(card->shortname, INTEL_HAD);
Jerome Anand5dab11d2017-01-25 04:27:52 +05302031
Takashi Iwaida864802017-01-31 13:52:22 +01002032 ctx->irq = -1;
2033 ctx->tmds_clock_speed = DIS_SAMPLE_RATE_148_5;
2034 INIT_WORK(&ctx->hdmi_audio_wq, _had_wq);
2035 ctx->state = hdmi_connector_status_disconnected;
Jerome Anand5dab11d2017-01-25 04:27:52 +05302036
Takashi Iwaida864802017-01-31 13:52:22 +01002037 card->private_free = hdmi_lpe_audio_free;
2038
2039 /* assume pipe A as default */
2040 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
2041
2042 platform_set_drvdata(pdev, ctx);
2043
2044 dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
2045 __func__, (unsigned int)res_mmio->start,
2046 (unsigned int)res_mmio->end);
2047
2048 ctx->mmio_start = ioremap_nocache(res_mmio->start,
2049 (size_t)(resource_size(res_mmio)));
2050 if (!ctx->mmio_start) {
2051 dev_err(&pdev->dev, "Could not get ioremap\n");
2052 ret = -EACCES;
2053 goto err;
2054 }
2055
2056 /* setup interrupt handler */
2057 ret = request_irq(irq, display_pipe_interrupt_handler, 0,
2058 pdev->name, ctx);
2059 if (ret < 0) {
2060 dev_err(&pdev->dev, "request_irq failed\n");
2061 goto err;
2062 }
2063
2064 ctx->irq = irq;
2065
2066 ret = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
2067 MAX_CAP_STREAMS, &pcm);
2068 if (ret)
Jerome Anand5dab11d2017-01-25 04:27:52 +05302069 goto err;
2070
2071 /* setup private data which can be retrieved when required */
Takashi Iwaida864802017-01-31 13:52:22 +01002072 pcm->private_data = ctx;
Jerome Anand5dab11d2017-01-25 04:27:52 +05302073 pcm->private_free = snd_intelhad_pcm_free;
2074 pcm->info_flags = 0;
2075 strncpy(pcm->name, card->shortname, strlen(card->shortname));
Takashi Iwaida864802017-01-31 13:52:22 +01002076 /* setup the ops for playabck */
Jerome Anand5dab11d2017-01-25 04:27:52 +05302077 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
2078 &snd_intelhad_playback_ops);
2079 /* allocate dma pages for ALSA stream operations
2080 * memory allocated is based on size, not max value
2081 * thus using same argument for max & size
2082 */
Takashi Iwaida864802017-01-31 13:52:22 +01002083 snd_pcm_lib_preallocate_pages_for_all(pcm,
Jerome Anand5dab11d2017-01-25 04:27:52 +05302084 SNDRV_DMA_TYPE_DEV, NULL,
2085 HAD_MAX_BUFFER, HAD_MAX_BUFFER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05302086
Jerome Anand5dab11d2017-01-25 04:27:52 +05302087 /* IEC958 controls */
Takashi Iwaida864802017-01-31 13:52:22 +01002088 ret = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958_mask, ctx));
2089 if (ret < 0)
Jerome Anand5dab11d2017-01-25 04:27:52 +05302090 goto err;
Takashi Iwaida864802017-01-31 13:52:22 +01002091 ret = snd_ctl_add(card, snd_ctl_new1(&had_control_iec958, ctx));
2092 if (ret < 0)
Jerome Anand5dab11d2017-01-25 04:27:52 +05302093 goto err;
2094
2095 init_channel_allocations();
2096
2097 /* Register channel map controls */
Takashi Iwaida864802017-01-31 13:52:22 +01002098 ret = had_register_chmap_ctls(ctx, pcm);
2099 if (ret < 0)
Jerome Anand5dab11d2017-01-25 04:27:52 +05302100 goto err;
2101
Takashi Iwaida864802017-01-31 13:52:22 +01002102 ret = snd_card_register(card);
2103 if (ret)
Takashi Iwai36ec0d92017-01-31 08:47:05 +01002104 goto err;
2105
Takashi Iwaida864802017-01-31 13:52:22 +01002106 spin_lock_irqsave(&pdata->lpe_audio_slock, flags);
2107 pdata->notify_audio_lpe = notify_audio_lpe;
2108 if (pdata->notify_pending) {
Jerome Anand5dab11d2017-01-25 04:27:52 +05302109
Takashi Iwaida864802017-01-31 13:52:22 +01002110 dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
2111 notify_audio_lpe(pdev);
2112 pdata->notify_pending = false;
2113 }
2114 spin_unlock_irqrestore(&pdata->lpe_audio_slock, flags);
2115
2116 pm_runtime_set_active(&pdev->dev);
2117 pm_runtime_enable(&pdev->dev);
2118
2119 schedule_work(&ctx->hdmi_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05302120
Takashi Iwai79dda752017-01-30 17:23:39 +01002121 return 0;
Takashi Iwai5647aec2017-01-31 08:14:34 +01002122
Jerome Anand5dab11d2017-01-25 04:27:52 +05302123err:
2124 snd_card_free(card);
Takashi Iwaida864802017-01-31 13:52:22 +01002125 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05302126}
2127
Takashi Iwai79dda752017-01-30 17:23:39 +01002128/*
Takashi Iwaida864802017-01-31 13:52:22 +01002129 * hdmi_lpe_audio_remove - stop bridge with i915
Jerome Anand5dab11d2017-01-25 04:27:52 +05302130 *
Takashi Iwaida864802017-01-31 13:52:22 +01002131 * This function is called when the platform device is destroyed. The sound
2132 * card should have been removed on hot plug event.
Jerome Anand5dab11d2017-01-25 04:27:52 +05302133 */
Takashi Iwaida864802017-01-31 13:52:22 +01002134static int hdmi_lpe_audio_remove(struct platform_device *pdev)
Jerome Anand5dab11d2017-01-25 04:27:52 +05302135{
Takashi Iwaida864802017-01-31 13:52:22 +01002136 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05302137
Takashi Iwaida864802017-01-31 13:52:22 +01002138 if (ctx->drv_status != HAD_DRV_DISCONNECTED)
2139 snd_intelhad_enable_audio_int(ctx, false);
2140 snd_card_free(ctx->card);
Jerome Anand5dab11d2017-01-25 04:27:52 +05302141 return 0;
2142}
2143
Takashi Iwaida864802017-01-31 13:52:22 +01002144static struct platform_driver hdmi_lpe_audio_driver = {
2145 .driver = {
2146 .name = "hdmi-lpe-audio",
2147 },
2148 .probe = hdmi_lpe_audio_probe,
2149 .remove = hdmi_lpe_audio_remove,
2150 .suspend = hdmi_lpe_audio_suspend,
2151 .resume = hdmi_lpe_audio_resume
2152};
2153
2154module_platform_driver(hdmi_lpe_audio_driver);
2155MODULE_ALIAS("platform:hdmi_lpe_audio");
2156
Jerome Anand5dab11d2017-01-25 04:27:52 +05302157MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
2158MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
2159MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
2160MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
2161MODULE_DESCRIPTION("Intel HDMI Audio driver");
2162MODULE_LICENSE("GPL v2");
2163MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");