blob: f49520117dd60dd8b6bd5bc38ede965c54baeccd [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
Takashi Iwai03c34372017-02-02 16:19:03 +010024#include <linux/types.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053025#include <linux/platform_device.h>
26#include <linux/io.h>
27#include <linux/slab.h>
28#include <linux/module.h>
Takashi Iwaida864802017-01-31 13:52:22 +010029#include <linux/interrupt.h>
Takashi Iwai03c34372017-02-02 16:19:03 +010030#include <linux/pm_runtime.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053031#include <asm/cacheflush.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053032#include <sound/core.h>
Takashi Iwai03c34372017-02-02 16:19:03 +010033#include <sound/asoundef.h>
34#include <sound/pcm.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053035#include <sound/pcm_params.h>
36#include <sound/initval.h>
37#include <sound/control.h>
Takashi Iwai03c34372017-02-02 16:19:03 +010038#include <drm/drm_edid.h>
Takashi Iwaida864802017-01-31 13:52:22 +010039#include <drm/intel_lpe_audio.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053040#include "intel_hdmi_audio.h"
41
Jerome Anand5dab11d2017-01-25 04:27:52 +053042/*standard module options for ALSA. This module supports only one card*/
43static int hdmi_card_index = SNDRV_DEFAULT_IDX1;
44static char *hdmi_card_id = SNDRV_DEFAULT_STR1;
Jerome Anand5dab11d2017-01-25 04:27:52 +053045
46module_param_named(index, hdmi_card_index, int, 0444);
47MODULE_PARM_DESC(index,
48 "Index value for INTEL Intel HDMI Audio controller.");
49module_param_named(id, hdmi_card_id, charp, 0444);
50MODULE_PARM_DESC(id,
51 "ID string for INTEL Intel HDMI Audio controller.");
52
53/*
54 * ELD SA bits in the CEA Speaker Allocation data block
55 */
Takashi Iwai4a5ddb22017-02-01 16:45:38 +010056static const int eld_speaker_allocation_bits[] = {
Jerome Anand5dab11d2017-01-25 04:27:52 +053057 [0] = FL | FR,
58 [1] = LFE,
59 [2] = FC,
60 [3] = RL | RR,
61 [4] = RC,
62 [5] = FLC | FRC,
63 [6] = RLC | RRC,
64 /* the following are not defined in ELD yet */
65 [7] = 0,
66};
67
68/*
69 * This is an ordered list!
70 *
71 * The preceding ones have better chances to be selected by
72 * hdmi_channel_allocation().
73 */
74static struct cea_channel_speaker_allocation channel_allocations[] = {
75/* channel: 7 6 5 4 3 2 1 0 */
76{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
77 /* 2.1 */
78{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
79 /* Dolby Surround */
80{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
81 /* surround40 */
82{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
83 /* surround41 */
84{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
85 /* surround50 */
86{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
87 /* surround51 */
88{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
89 /* 6.1 */
90{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
91 /* surround71 */
92{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
93
94{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
95{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
96{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
97{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
98{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
99{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
100{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
101{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
102{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
103{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
104{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
105{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
106{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
107{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
108{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
109{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
110{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
111{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
112{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
113{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
114{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
115{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
116{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
117};
118
Takashi Iwai4a5ddb22017-02-01 16:45:38 +0100119static const struct channel_map_table map_tables[] = {
Jerome Anand5dab11d2017-01-25 04:27:52 +0530120 { SNDRV_CHMAP_FL, 0x00, FL },
121 { SNDRV_CHMAP_FR, 0x01, FR },
122 { SNDRV_CHMAP_RL, 0x04, RL },
123 { SNDRV_CHMAP_RR, 0x05, RR },
124 { SNDRV_CHMAP_LFE, 0x02, LFE },
125 { SNDRV_CHMAP_FC, 0x03, FC },
126 { SNDRV_CHMAP_RLC, 0x06, RLC },
127 { SNDRV_CHMAP_RRC, 0x07, RRC },
128 {} /* terminator */
129};
130
131/* hardware capability structure */
132static const struct snd_pcm_hardware snd_intel_hadstream = {
133 .info = (SNDRV_PCM_INFO_INTERLEAVED |
134 SNDRV_PCM_INFO_DOUBLE |
135 SNDRV_PCM_INFO_MMAP|
136 SNDRV_PCM_INFO_MMAP_VALID |
137 SNDRV_PCM_INFO_BATCH),
138 .formats = (SNDRV_PCM_FMTBIT_S24 |
139 SNDRV_PCM_FMTBIT_U24),
140 .rates = SNDRV_PCM_RATE_32000 |
141 SNDRV_PCM_RATE_44100 |
142 SNDRV_PCM_RATE_48000 |
143 SNDRV_PCM_RATE_88200 |
144 SNDRV_PCM_RATE_96000 |
145 SNDRV_PCM_RATE_176400 |
146 SNDRV_PCM_RATE_192000,
147 .rate_min = HAD_MIN_RATE,
148 .rate_max = HAD_MAX_RATE,
149 .channels_min = HAD_MIN_CHANNEL,
150 .channels_max = HAD_MAX_CHANNEL,
151 .buffer_bytes_max = HAD_MAX_BUFFER,
152 .period_bytes_min = HAD_MIN_PERIOD_BYTES,
153 .period_bytes_max = HAD_MAX_PERIOD_BYTES,
154 .periods_min = HAD_MIN_PERIODS,
155 .periods_max = HAD_MAX_PERIODS,
156 .fifo_size = HAD_FIFO_SIZE,
157};
158
Takashi Iwai313d9f22017-02-02 13:00:12 +0100159/* Get the active PCM substream;
160 * Call had_substream_put() for unreferecing.
161 * Don't call this inside had_spinlock, as it takes by itself
162 */
163static struct snd_pcm_substream *
164had_substream_get(struct snd_intelhad *intelhaddata)
165{
166 struct snd_pcm_substream *substream;
167 unsigned long flags;
168
169 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
170 substream = intelhaddata->stream_info.substream;
171 if (substream)
172 intelhaddata->stream_info.substream_refcount++;
173 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
174 return substream;
175}
176
177/* Unref the active PCM substream;
178 * Don't call this inside had_spinlock, as it takes by itself
179 */
180static void had_substream_put(struct snd_intelhad *intelhaddata)
181{
182 unsigned long flags;
183
184 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
185 intelhaddata->stream_info.substream_refcount--;
186 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
187}
188
Jerome Anand5dab11d2017-01-25 04:27:52 +0530189/* Register access functions */
Takashi Iwaida864802017-01-31 13:52:22 +0100190static inline void
191mid_hdmi_audio_read(struct snd_intelhad *ctx, u32 reg, u32 *val)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530192{
Takashi Iwaida864802017-01-31 13:52:22 +0100193 *val = ioread32(ctx->mmio_start + ctx->had_config_offset + reg);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530194}
195
Takashi Iwaida864802017-01-31 13:52:22 +0100196static inline void
197mid_hdmi_audio_write(struct snd_intelhad *ctx, u32 reg, u32 val)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530198{
Takashi Iwaida864802017-01-31 13:52:22 +0100199 iowrite32(val, ctx->mmio_start + ctx->had_config_offset + reg);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530200}
201
Takashi Iwai372d8552017-01-31 13:57:58 +0100202static int had_read_register(struct snd_intelhad *intelhaddata,
203 u32 offset, u32 *data)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530204{
Takashi Iwai79f439e2017-01-31 16:46:44 +0100205 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
206 return -ENODEV;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530207
Takashi Iwaida864802017-01-31 13:52:22 +0100208 mid_hdmi_audio_read(intelhaddata, offset, data);
209 return 0;
210}
211
212static void fixup_dp_config(struct snd_intelhad *intelhaddata,
213 u32 offset, u32 *data)
214{
215 if (intelhaddata->dp_output) {
216 if (offset == AUD_CONFIG && (*data & AUD_CONFIG_VALID_BIT))
217 *data |= AUD_CONFIG_DP_MODE | AUD_CONFIG_BLOCK_BIT;
218 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530219}
220
Takashi Iwai372d8552017-01-31 13:57:58 +0100221static int had_write_register(struct snd_intelhad *intelhaddata,
222 u32 offset, u32 data)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530223{
Takashi Iwai79f439e2017-01-31 16:46:44 +0100224 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
225 return -ENODEV;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530226
Takashi Iwaida864802017-01-31 13:52:22 +0100227 fixup_dp_config(intelhaddata, offset, &data);
228 mid_hdmi_audio_write(intelhaddata, offset, data);
229 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530230}
231
Takashi Iwai372d8552017-01-31 13:57:58 +0100232static int had_read_modify(struct snd_intelhad *intelhaddata, u32 offset,
233 u32 data, u32 mask)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530234{
Takashi Iwaida864802017-01-31 13:52:22 +0100235 u32 val_tmp;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530236
Takashi Iwai79f439e2017-01-31 16:46:44 +0100237 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
238 return -ENODEV;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530239
Takashi Iwaida864802017-01-31 13:52:22 +0100240 mid_hdmi_audio_read(intelhaddata, offset, &val_tmp);
241 val_tmp &= ~mask;
242 val_tmp |= (data & mask);
243
244 fixup_dp_config(intelhaddata, offset, &val_tmp);
245 mid_hdmi_audio_write(intelhaddata, offset, val_tmp);
246 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530247}
Takashi Iwaida864802017-01-31 13:52:22 +0100248
249/*
Takashi Iwai313d9f22017-02-02 13:00:12 +0100250 * enable / disable audio configuration
251 *
Takashi Iwaida864802017-01-31 13:52:22 +0100252 * The had_read_modify() function should not directly be used on VLV2 for
253 * updating AUD_CONFIG register.
Jerome Anand5dab11d2017-01-25 04:27:52 +0530254 * This is because:
255 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
256 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
257 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
258 * register. This field should be 1xy binary for configuration with 6 or
259 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
260 * causes the "channels" field to be updated as 0xy binary resulting in
261 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
262 * appropriate value when doing read-modify of AUD_CONFIG register.
Jerome Anand5dab11d2017-01-25 04:27:52 +0530263 */
Takashi Iwai313d9f22017-02-02 13:00:12 +0100264static void snd_intelhad_enable_audio(struct snd_pcm_substream *substream,
265 struct snd_intelhad *intelhaddata,
266 bool enable)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530267{
Takashi Iwai7ceba752017-02-02 15:58:35 +0100268 union aud_cfg cfg_val = {.regval = 0};
Takashi Iwai313d9f22017-02-02 13:00:12 +0100269 u8 channels, data, mask;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530270
271 /*
272 * If substream is NULL, there is no active stream.
273 * In this case just set channels to 2
274 */
Takashi Iwai313d9f22017-02-02 13:00:12 +0100275 channels = substream ? substream->runtime->channels : 2;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100276 cfg_val.regx.num_ch = channels - 2;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530277
Takashi Iwai7ceba752017-02-02 15:58:35 +0100278 data = cfg_val.regval;
Takashi Iwai313d9f22017-02-02 13:00:12 +0100279 if (enable)
280 data |= 1;
281 mask = AUD_CONFIG_CH_MASK | 1;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530282
Takashi Iwaic75b0472017-01-31 15:49:15 +0100283 dev_dbg(intelhaddata->dev, "%s : data = %x, mask =%x\n",
284 __func__, data, mask);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530285
Takashi Iwai313d9f22017-02-02 13:00:12 +0100286 had_read_modify(intelhaddata, AUD_CONFIG, data, mask);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530287}
288
Takashi Iwai313d9f22017-02-02 13:00:12 +0100289/* enable / disable the audio interface */
Takashi Iwai372d8552017-01-31 13:57:58 +0100290static void snd_intelhad_enable_audio_int(struct snd_intelhad *ctx, bool enable)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530291{
Takashi Iwaida864802017-01-31 13:52:22 +0100292 u32 status_reg;
293
294 if (enable) {
Takashi Iwai4151ee82017-01-31 18:14:15 +0100295 mid_hdmi_audio_read(ctx, AUD_HDMI_STATUS, &status_reg);
Takashi Iwaida864802017-01-31 13:52:22 +0100296 status_reg |= HDMI_AUDIO_BUFFER_DONE | HDMI_AUDIO_UNDERRUN;
Takashi Iwai4151ee82017-01-31 18:14:15 +0100297 mid_hdmi_audio_write(ctx, AUD_HDMI_STATUS, status_reg);
298 mid_hdmi_audio_read(ctx, AUD_HDMI_STATUS, &status_reg);
Takashi Iwaida864802017-01-31 13:52:22 +0100299 }
300}
301
Takashi Iwai79dda752017-01-30 17:23:39 +0100302static void snd_intelhad_reset_audio(struct snd_intelhad *intelhaddata,
303 u8 reset)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530304{
Takashi Iwai4151ee82017-01-31 18:14:15 +0100305 had_write_register(intelhaddata, AUD_HDMI_STATUS, reset);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530306}
307
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100308/*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530309 * initialize audio channel status registers
310 * This function is called in the prepare callback
311 */
312static int had_prog_status_reg(struct snd_pcm_substream *substream,
313 struct snd_intelhad *intelhaddata)
314{
Takashi Iwai7ceba752017-02-02 15:58:35 +0100315 union aud_cfg cfg_val = {.regval = 0};
316 union aud_ch_status_0 ch_stat0 = {.regval = 0};
317 union aud_ch_status_1 ch_stat1 = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530318 int format;
319
Takashi Iwai7ceba752017-02-02 15:58:35 +0100320 ch_stat0.regx.lpcm_id = (intelhaddata->aes_bits &
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100321 IEC958_AES0_NONAUDIO) >> 1;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100322 ch_stat0.regx.clk_acc = (intelhaddata->aes_bits &
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100323 IEC958_AES3_CON_CLOCK) >> 4;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100324 cfg_val.regx.val_bit = ch_stat0.regx.lpcm_id;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530325
326 switch (substream->runtime->rate) {
327 case AUD_SAMPLE_RATE_32:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100328 ch_stat0.regx.samp_freq = CH_STATUS_MAP_32KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530329 break;
330
331 case AUD_SAMPLE_RATE_44_1:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100332 ch_stat0.regx.samp_freq = CH_STATUS_MAP_44KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530333 break;
334 case AUD_SAMPLE_RATE_48:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100335 ch_stat0.regx.samp_freq = CH_STATUS_MAP_48KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530336 break;
337 case AUD_SAMPLE_RATE_88_2:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100338 ch_stat0.regx.samp_freq = CH_STATUS_MAP_88KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530339 break;
340 case AUD_SAMPLE_RATE_96:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100341 ch_stat0.regx.samp_freq = CH_STATUS_MAP_96KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530342 break;
343 case AUD_SAMPLE_RATE_176_4:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100344 ch_stat0.regx.samp_freq = CH_STATUS_MAP_176KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530345 break;
346 case AUD_SAMPLE_RATE_192:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100347 ch_stat0.regx.samp_freq = CH_STATUS_MAP_192KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530348 break;
349
350 default:
351 /* control should never come here */
352 return -EINVAL;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530353 }
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100354
Takashi Iwai79dda752017-01-30 17:23:39 +0100355 had_write_register(intelhaddata,
Takashi Iwai7ceba752017-02-02 15:58:35 +0100356 AUD_CH_STATUS_0, ch_stat0.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530357
358 format = substream->runtime->format;
359
360 if (format == SNDRV_PCM_FORMAT_S16_LE) {
Takashi Iwai7ceba752017-02-02 15:58:35 +0100361 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_20;
362 ch_stat1.regx.wrd_len = SMPL_WIDTH_16BITS;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530363 } else if (format == SNDRV_PCM_FORMAT_S24_LE) {
Takashi Iwai7ceba752017-02-02 15:58:35 +0100364 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_24;
365 ch_stat1.regx.wrd_len = SMPL_WIDTH_24BITS;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530366 } else {
Takashi Iwai7ceba752017-02-02 15:58:35 +0100367 ch_stat1.regx.max_wrd_len = 0;
368 ch_stat1.regx.wrd_len = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530369 }
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100370
Takashi Iwai79dda752017-01-30 17:23:39 +0100371 had_write_register(intelhaddata,
Takashi Iwai7ceba752017-02-02 15:58:35 +0100372 AUD_CH_STATUS_1, ch_stat1.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530373 return 0;
374}
375
Takashi Iwai76296ef2017-01-30 16:09:11 +0100376/*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530377 * function to initialize audio
378 * registers and buffer confgiuration registers
379 * This function is called in the prepare callback
380 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100381static int snd_intelhad_audio_ctrl(struct snd_pcm_substream *substream,
382 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530383{
Takashi Iwai7ceba752017-02-02 15:58:35 +0100384 union aud_cfg cfg_val = {.regval = 0};
385 union aud_buf_config buf_cfg = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530386 u8 channels;
387
388 had_prog_status_reg(substream, intelhaddata);
389
Takashi Iwai7ceba752017-02-02 15:58:35 +0100390 buf_cfg.regx.audio_fifo_watermark = FIFO_THRESHOLD;
391 buf_cfg.regx.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
392 buf_cfg.regx.aud_delay = 0;
393 had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530394
395 channels = substream->runtime->channels;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100396 cfg_val.regx.num_ch = channels - 2;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530397 if (channels <= 2)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100398 cfg_val.regx.layout = LAYOUT0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530399 else
Takashi Iwai7ceba752017-02-02 15:58:35 +0100400 cfg_val.regx.layout = LAYOUT1;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530401
Takashi Iwai7ceba752017-02-02 15:58:35 +0100402 cfg_val.regx.val_bit = 1;
403 had_write_register(intelhaddata, AUD_CONFIG, cfg_val.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530404 return 0;
405}
406
Jerome Anand5dab11d2017-01-25 04:27:52 +0530407/*
408 * Compute derived values in channel_allocations[].
409 */
410static void init_channel_allocations(void)
411{
412 int i, j;
413 struct cea_channel_speaker_allocation *p;
414
Jerome Anand5dab11d2017-01-25 04:27:52 +0530415 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
416 p = channel_allocations + i;
417 p->channels = 0;
418 p->spk_mask = 0;
419 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
420 if (p->speakers[j]) {
421 p->channels++;
422 p->spk_mask |= p->speakers[j];
423 }
424 }
425}
426
427/*
428 * The transformation takes two steps:
429 *
430 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
431 * spk_mask => (channel_allocations[]) => ai->CA
432 *
433 * TODO: it could select the wrong CA from multiple candidates.
434 */
435static int snd_intelhad_channel_allocation(struct snd_intelhad *intelhaddata,
436 int channels)
437{
438 int i;
439 int ca = 0;
440 int spk_mask = 0;
441
442 /*
443 * CA defaults to 0 for basic stereo audio
444 */
445 if (channels <= 2)
446 return 0;
447
448 /*
449 * expand ELD's speaker allocation mask
450 *
451 * ELD tells the speaker mask in a compact(paired) form,
452 * expand ELD's notions to match the ones used by Audio InfoFrame.
453 */
454
455 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100456 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530457 spk_mask |= eld_speaker_allocation_bits[i];
458 }
459
460 /* search for the first working match in the CA table */
461 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
462 if (channels == channel_allocations[i].channels &&
463 (spk_mask & channel_allocations[i].spk_mask) ==
464 channel_allocations[i].spk_mask) {
465 ca = channel_allocations[i].ca_index;
466 break;
467 }
468 }
469
Takashi Iwaic75b0472017-01-31 15:49:15 +0100470 dev_dbg(intelhaddata->dev, "select CA 0x%x for %d\n", ca, channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530471
472 return ca;
473}
474
475/* from speaker bit mask to ALSA API channel position */
476static int spk_to_chmap(int spk)
477{
Takashi Iwai4a5ddb22017-02-01 16:45:38 +0100478 const struct channel_map_table *t = map_tables;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530479
480 for (; t->map; t++) {
481 if (t->spk_mask == spk)
482 return t->map;
483 }
484 return 0;
485}
486
Takashi Iwai372d8552017-01-31 13:57:58 +0100487static void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530488{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100489 int i, c;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530490 int spk_mask = 0;
491 struct snd_pcm_chmap_elem *chmap;
492 u8 eld_high, eld_high_mask = 0xF0;
493 u8 high_msb;
494
495 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100496 if (!chmap) {
Jerome Anand5dab11d2017-01-25 04:27:52 +0530497 intelhaddata->chmap->chmap = NULL;
498 return;
499 }
500
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100501 dev_dbg(intelhaddata->dev, "eld speaker = %x\n",
502 intelhaddata->eld[DRM_ELD_SPEAKER]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530503
504 /* WA: Fix the max channel supported to 8 */
505
506 /*
507 * Sink may support more than 8 channels, if eld_high has more than
508 * one bit set. SOC supports max 8 channels.
509 * Refer eld_speaker_allocation_bits, for sink speaker allocation
510 */
511
512 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100513 eld_high = intelhaddata->eld[DRM_ELD_SPEAKER] & eld_high_mask;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530514 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
515 /* eld_high & (eld_high-1): if more than 1 bit set */
516 /* 0x1F: 7 channels */
517 for (i = 1; i < 4; i++) {
518 high_msb = eld_high & (0x80 >> i);
519 if (high_msb) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100520 intelhaddata->eld[DRM_ELD_SPEAKER] &=
Jerome Anand5dab11d2017-01-25 04:27:52 +0530521 high_msb | 0xF;
522 break;
523 }
524 }
525 }
526
527 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100528 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530529 spk_mask |= eld_speaker_allocation_bits[i];
530 }
531
532 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
533 if (spk_mask == channel_allocations[i].spk_mask) {
534 for (c = 0; c < channel_allocations[i].channels; c++) {
535 chmap->map[c] = spk_to_chmap(
536 channel_allocations[i].speakers[
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100537 (MAX_SPEAKERS - 1) - c]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530538 }
539 chmap->channels = channel_allocations[i].channels;
540 intelhaddata->chmap->chmap = chmap;
541 break;
542 }
543 }
544 if (i >= ARRAY_SIZE(channel_allocations)) {
545 intelhaddata->chmap->chmap = NULL;
546 kfree(chmap);
547 }
548}
549
550/*
551 * ALSA API channel-map control callbacks
552 */
553static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
554 struct snd_ctl_elem_info *uinfo)
555{
556 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
557 struct snd_intelhad *intelhaddata = info->private_data;
558
559 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
560 return -ENODEV;
561 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
562 uinfo->count = HAD_MAX_CHANNEL;
563 uinfo->value.integer.min = 0;
564 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
565 return 0;
566}
567
568static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
569 struct snd_ctl_elem_value *ucontrol)
570{
571 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
572 struct snd_intelhad *intelhaddata = info->private_data;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100573 int i;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530574 const struct snd_pcm_chmap_elem *chmap;
575
576 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
577 return -ENODEV;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100578
579 mutex_lock(&intelhaddata->mutex);
580 if (!intelhaddata->chmap->chmap) {
581 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530582 return -ENODATA;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100583 }
584
Jerome Anand5dab11d2017-01-25 04:27:52 +0530585 chmap = intelhaddata->chmap->chmap;
Takashi Iwaic75b0472017-01-31 15:49:15 +0100586 for (i = 0; i < chmap->channels; i++)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530587 ucontrol->value.integer.value[i] = chmap->map[i];
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100588 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530589
590 return 0;
591}
592
593static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
594 struct snd_pcm *pcm)
595{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100596 int err;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530597
598 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
599 NULL, 0, (unsigned long)intelhaddata,
600 &intelhaddata->chmap);
601 if (err < 0)
602 return err;
603
604 intelhaddata->chmap->private_data = intelhaddata;
Takashi Iwaie9d65ab2017-01-31 16:11:27 +0100605 intelhaddata->chmap->kctl->info = had_chmap_ctl_info;
606 intelhaddata->chmap->kctl->get = had_chmap_ctl_get;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530607 intelhaddata->chmap->chmap = NULL;
608 return 0;
609}
610
Takashi Iwai76296ef2017-01-30 16:09:11 +0100611/*
612 * snd_intelhad_prog_dip - to initialize Data Island Packets registers
Jerome Anand5dab11d2017-01-25 04:27:52 +0530613 *
614 * @substream:substream for which the prepare function is called
615 * @intelhaddata:substream private data
616 *
617 * This function is called in the prepare callback
618 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100619static void snd_intelhad_prog_dip(struct snd_pcm_substream *substream,
620 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530621{
622 int i;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100623 union aud_ctrl_st ctrl_state = {.regval = 0};
624 union aud_info_frame2 frame2 = {.regval = 0};
625 union aud_info_frame3 frame3 = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530626 u8 checksum = 0;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600627 u32 info_frame;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530628 int channels;
629
630 channels = substream->runtime->channels;
631
Takashi Iwai7ceba752017-02-02 15:58:35 +0100632 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530633
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600634 if (intelhaddata->dp_output) {
635 info_frame = DP_INFO_FRAME_WORD1;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100636 frame2.regval = 1;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600637 } else {
638 info_frame = HDMI_INFO_FRAME_WORD1;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100639 frame2.regx.chnl_cnt = substream->runtime->channels - 1;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530640
Takashi Iwai7ceba752017-02-02 15:58:35 +0100641 frame3.regx.chnl_alloc = snd_intelhad_channel_allocation(
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600642 intelhaddata, channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530643
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100644 /* Calculte the byte wide checksum for all valid DIP words */
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600645 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100646 checksum += (info_frame >> (i * 8)) & 0xff;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600647 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100648 checksum += (frame2.regval >> (i * 8)) & 0xff;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600649 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100650 checksum += (frame3.regval >> (i * 8)) & 0xff;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530651
Takashi Iwai7ceba752017-02-02 15:58:35 +0100652 frame2.regx.chksum = -(checksum);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600653 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530654
Takashi Iwai4151ee82017-01-31 18:14:15 +0100655 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, info_frame);
Takashi Iwai7ceba752017-02-02 15:58:35 +0100656 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame2.regval);
657 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame3.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530658
659 /* program remaining DIP words with zero */
660 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
Takashi Iwai4151ee82017-01-31 18:14:15 +0100661 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, 0x0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530662
Takashi Iwai7ceba752017-02-02 15:58:35 +0100663 ctrl_state.regx.dip_freq = 1;
664 ctrl_state.regx.dip_en_sta = 1;
665 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530666}
667
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100668/*
669 * snd_intelhad_prog_buffer - programs buffer address and length registers
Takashi Iwai313d9f22017-02-02 13:00:12 +0100670 * @substream: substream for which the prepare function is called
671 * @intelhaddata: substream private data
Jerome Anand5dab11d2017-01-25 04:27:52 +0530672 *
673 * This function programs ring buffer address and length into registers.
674 */
Takashi Iwai313d9f22017-02-02 13:00:12 +0100675static int snd_intelhad_prog_buffer(struct snd_pcm_substream *substream,
676 struct snd_intelhad *intelhaddata,
677 int start, int end)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530678{
679 u32 ring_buf_addr, ring_buf_size, period_bytes;
680 u8 i, num_periods;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530681
682 ring_buf_addr = substream->runtime->dma_addr;
683 ring_buf_size = snd_pcm_lib_buffer_bytes(substream);
684 intelhaddata->stream_info.ring_buf_size = ring_buf_size;
685 period_bytes = frames_to_bytes(substream->runtime,
686 substream->runtime->period_size);
687 num_periods = substream->runtime->periods;
688
689 /*
690 * buffer addr should be 64 byte aligned, period bytes
691 * will be used to calculate addr offset
692 */
693 period_bytes &= ~0x3F;
694
695 /* Hardware supports MAX_PERIODS buffers */
696 if (end >= HAD_MAX_PERIODS)
697 return -EINVAL;
698
699 for (i = start; i <= end; i++) {
700 /* Program the buf registers with addr and len */
701 intelhaddata->buf_info[i].buf_addr = ring_buf_addr +
702 (i * period_bytes);
703 if (i < num_periods-1)
704 intelhaddata->buf_info[i].buf_size = period_bytes;
705 else
706 intelhaddata->buf_info[i].buf_size = ring_buf_size -
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100707 (i * period_bytes);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530708
Takashi Iwai79dda752017-01-30 17:23:39 +0100709 had_write_register(intelhaddata,
710 AUD_BUF_A_ADDR + (i * HAD_REG_WIDTH),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530711 intelhaddata->buf_info[i].buf_addr |
712 BIT(0) | BIT(1));
Takashi Iwai79dda752017-01-30 17:23:39 +0100713 had_write_register(intelhaddata,
714 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530715 period_bytes);
716 intelhaddata->buf_info[i].is_valid = true;
717 }
Takashi Iwaic75b0472017-01-31 15:49:15 +0100718 dev_dbg(intelhaddata->dev, "%s:buf[%d-%d] addr=%#x and size=%d\n",
719 __func__, start, end,
720 intelhaddata->buf_info[start].buf_addr,
721 intelhaddata->buf_info[start].buf_size);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530722 intelhaddata->valid_buf_cnt = num_periods;
723 return 0;
724}
725
Takashi Iwai372d8552017-01-31 13:57:58 +0100726static int snd_intelhad_read_len(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530727{
728 int i, retval = 0;
729 u32 len[4];
730
731 for (i = 0; i < 4 ; i++) {
Takashi Iwai79dda752017-01-30 17:23:39 +0100732 had_read_register(intelhaddata,
733 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
734 &len[i]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530735 if (!len[i])
736 retval++;
737 }
738 if (retval != 1) {
739 for (i = 0; i < 4 ; i++)
Takashi Iwaic75b0472017-01-31 15:49:15 +0100740 dev_dbg(intelhaddata->dev, "buf[%d] size=%d\n",
741 i, len[i]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530742 }
743
744 return retval;
745}
746
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600747static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
748{
749 u32 maud_val;
750
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100751 /* Select maud according to DP 1.2 spec */
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600752 if (link_rate == DP_2_7_GHZ) {
753 switch (aud_samp_freq) {
754 case AUD_SAMPLE_RATE_32:
755 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
756 break;
757
758 case AUD_SAMPLE_RATE_44_1:
759 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
760 break;
761
762 case AUD_SAMPLE_RATE_48:
763 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
764 break;
765
766 case AUD_SAMPLE_RATE_88_2:
767 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
768 break;
769
770 case AUD_SAMPLE_RATE_96:
771 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
772 break;
773
774 case AUD_SAMPLE_RATE_176_4:
775 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
776 break;
777
778 case HAD_MAX_RATE:
779 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
780 break;
781
782 default:
783 maud_val = -EINVAL;
784 break;
785 }
786 } else if (link_rate == DP_1_62_GHZ) {
787 switch (aud_samp_freq) {
788 case AUD_SAMPLE_RATE_32:
789 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
790 break;
791
792 case AUD_SAMPLE_RATE_44_1:
793 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
794 break;
795
796 case AUD_SAMPLE_RATE_48:
797 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
798 break;
799
800 case AUD_SAMPLE_RATE_88_2:
801 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
802 break;
803
804 case AUD_SAMPLE_RATE_96:
805 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
806 break;
807
808 case AUD_SAMPLE_RATE_176_4:
809 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
810 break;
811
812 case HAD_MAX_RATE:
813 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
814 break;
815
816 default:
817 maud_val = -EINVAL;
818 break;
819 }
820 } else
821 maud_val = -EINVAL;
822
823 return maud_val;
824}
825
Takashi Iwai76296ef2017-01-30 16:09:11 +0100826/*
827 * snd_intelhad_prog_cts - Program HDMI audio CTS value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530828 *
829 * @aud_samp_freq: sampling frequency of audio data
830 * @tmds: sampling frequency of the display data
831 * @n_param: N value, depends on aud_samp_freq
832 * @intelhaddata:substream private data
833 *
834 * Program CTS register based on the audio and display sampling frequency
835 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100836static void snd_intelhad_prog_cts(u32 aud_samp_freq, u32 tmds,
837 u32 link_rate, u32 n_param,
838 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530839{
840 u32 cts_val;
841 u64 dividend, divisor;
842
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600843 if (intelhaddata->dp_output) {
844 /* Substitute cts_val with Maud according to DP 1.2 spec*/
845 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
846 } else {
847 /* Calculate CTS according to HDMI 1.3a spec*/
848 dividend = (u64)tmds * n_param*1000;
849 divisor = 128 * aud_samp_freq;
850 cts_val = div64_u64(dividend, divisor);
851 }
Takashi Iwaic75b0472017-01-31 15:49:15 +0100852 dev_dbg(intelhaddata->dev, "TMDS value=%d, N value=%d, CTS Value=%d\n",
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600853 tmds, n_param, cts_val);
Takashi Iwai79dda752017-01-30 17:23:39 +0100854 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530855}
856
857static int had_calculate_n_value(u32 aud_samp_freq)
858{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100859 int n_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530860
861 /* Select N according to HDMI 1.3a spec*/
862 switch (aud_samp_freq) {
863 case AUD_SAMPLE_RATE_32:
864 n_val = 4096;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100865 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530866
867 case AUD_SAMPLE_RATE_44_1:
868 n_val = 6272;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100869 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530870
871 case AUD_SAMPLE_RATE_48:
872 n_val = 6144;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100873 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530874
875 case AUD_SAMPLE_RATE_88_2:
876 n_val = 12544;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100877 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530878
879 case AUD_SAMPLE_RATE_96:
880 n_val = 12288;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100881 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530882
883 case AUD_SAMPLE_RATE_176_4:
884 n_val = 25088;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100885 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530886
887 case HAD_MAX_RATE:
888 n_val = 24576;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100889 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530890
891 default:
892 n_val = -EINVAL;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100893 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530894 }
895 return n_val;
896}
897
Takashi Iwai76296ef2017-01-30 16:09:11 +0100898/*
899 * snd_intelhad_prog_n - Program HDMI audio N value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530900 *
901 * @aud_samp_freq: sampling frequency of audio data
902 * @n_param: N value, depends on aud_samp_freq
903 * @intelhaddata:substream private data
904 *
905 * This function is called in the prepare callback.
906 * It programs based on the audio and display sampling frequency
907 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100908static int snd_intelhad_prog_n(u32 aud_samp_freq, u32 *n_param,
909 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530910{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100911 int n_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530912
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600913 if (intelhaddata->dp_output) {
914 /*
915 * According to DP specs, Maud and Naud values hold
916 * a relationship, which is stated as:
917 * Maud/Naud = 512 * fs / f_LS_Clk
918 * where, fs is the sampling frequency of the audio stream
919 * and Naud is 32768 for Async clock.
920 */
921
922 n_val = DP_NAUD_VAL;
923 } else
924 n_val = had_calculate_n_value(aud_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530925
926 if (n_val < 0)
927 return n_val;
928
Takashi Iwai79dda752017-01-30 17:23:39 +0100929 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530930 *n_param = n_val;
931 return 0;
932}
933
Takashi Iwai03c34372017-02-02 16:19:03 +0100934#define MAX_CNT 0xFF
935
Takashi Iwai372d8552017-01-31 13:57:58 +0100936static void snd_intelhad_handle_underrun(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530937{
Takashi Iwai79f439e2017-01-31 16:46:44 +0100938 u32 hdmi_status = 0, i = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530939
940 /* Handle Underrun interrupt within Audio Unit */
Takashi Iwai79dda752017-01-30 17:23:39 +0100941 had_write_register(intelhaddata, AUD_CONFIG, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530942 /* Reset buffer pointers */
Takashi Iwai4151ee82017-01-31 18:14:15 +0100943 had_write_register(intelhaddata, AUD_HDMI_STATUS, 1);
944 had_write_register(intelhaddata, AUD_HDMI_STATUS, 0);
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100945 /*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530946 * The interrupt status 'sticky' bits might not be cleared by
947 * setting '1' to that bit once...
948 */
949 do { /* clear bit30, 31 AUD_HDMI_STATUS */
Takashi Iwai4151ee82017-01-31 18:14:15 +0100950 had_read_register(intelhaddata, AUD_HDMI_STATUS,
Takashi Iwai79dda752017-01-30 17:23:39 +0100951 &hdmi_status);
Takashi Iwaic75b0472017-01-31 15:49:15 +0100952 dev_dbg(intelhaddata->dev, "HDMI status =0x%x\n", hdmi_status);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530953 if (hdmi_status & AUD_CONFIG_MASK_UNDERRUN) {
954 i++;
Takashi Iwai79dda752017-01-30 17:23:39 +0100955 had_write_register(intelhaddata,
Takashi Iwai4151ee82017-01-31 18:14:15 +0100956 AUD_HDMI_STATUS, hdmi_status);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530957 } else
958 break;
959 } while (i < MAX_CNT);
960 if (i >= MAX_CNT)
Takashi Iwaic75b0472017-01-31 15:49:15 +0100961 dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n");
Jerome Anand5dab11d2017-01-25 04:27:52 +0530962}
963
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100964/*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530965 * snd_intelhad_open - stream initializations are done here
966 * @substream:substream for which the stream function is called
967 *
968 * This function is called whenever a PCM stream is opened
969 */
970static int snd_intelhad_open(struct snd_pcm_substream *substream)
971{
972 struct snd_intelhad *intelhaddata;
973 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530974 int retval;
975
Jerome Anand5dab11d2017-01-25 04:27:52 +0530976 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530977 runtime = substream->runtime;
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +0100978 intelhaddata->underrun_count = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530979
Takashi Iwai182cdf22017-02-02 14:43:39 +0100980 pm_runtime_get_sync(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530981
Takashi Iwai79f439e2017-01-31 16:46:44 +0100982 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +0100983 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
984 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530985 retval = -ENODEV;
Takashi Iwaifa5dfe62017-02-01 22:03:26 +0100986 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530987 }
988
989 /* set the runtime hw parameter with local snd_pcm_hardware struct */
990 runtime->hw = snd_intel_hadstream;
991
Jerome Anand5dab11d2017-01-25 04:27:52 +0530992 retval = snd_pcm_hw_constraint_integer(runtime,
993 SNDRV_PCM_HW_PARAM_PERIODS);
994 if (retval < 0)
Takashi Iwaifa5dfe62017-02-01 22:03:26 +0100995 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530996
997 /* Make sure, that the period size is always aligned
998 * 64byte boundary
999 */
1000 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1001 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
1002 if (retval < 0) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001003 dev_dbg(intelhaddata->dev, "%s:step_size=64 failed,err=%d\n",
1004 __func__, retval);
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001005 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301006 }
1007
Takashi Iwai313d9f22017-02-02 13:00:12 +01001008 spin_lock_irq(&intelhaddata->had_spinlock);
1009 intelhaddata->stream_info.substream = substream;
1010 intelhaddata->stream_info.substream_refcount++;
1011 spin_unlock_irq(&intelhaddata->had_spinlock);
1012
Jerome Anand5dab11d2017-01-25 04:27:52 +05301013 return retval;
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001014 error:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301015 pm_runtime_put(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301016 return retval;
1017}
1018
Takashi Iwaidf76df12017-01-31 16:04:10 +01001019/*
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001020 * snd_intelhad_close - to free parameteres when stream is stopped
Jerome Anand5dab11d2017-01-25 04:27:52 +05301021 * @substream: substream for which the function is called
1022 *
1023 * This function is called by ALSA framework when stream is stopped
1024 */
1025static int snd_intelhad_close(struct snd_pcm_substream *substream)
1026{
1027 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301028
Jerome Anand5dab11d2017-01-25 04:27:52 +05301029 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301030
1031 intelhaddata->stream_info.buffer_rendered = 0;
Takashi Iwai313d9f22017-02-02 13:00:12 +01001032 spin_lock_irq(&intelhaddata->had_spinlock);
1033 intelhaddata->stream_info.substream = NULL;
1034 intelhaddata->stream_info.substream_refcount--;
1035 while (intelhaddata->stream_info.substream_refcount > 0) {
1036 spin_unlock_irq(&intelhaddata->had_spinlock);
1037 cpu_relax();
1038 spin_lock_irq(&intelhaddata->had_spinlock);
1039 }
1040 spin_unlock_irq(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301041
1042 /* Check if following drv_status modification is required - VA */
1043 if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) {
1044 intelhaddata->drv_status = HAD_DRV_CONNECTED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001045 dev_dbg(intelhaddata->dev,
1046 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301047 __func__, __LINE__);
1048 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301049 pm_runtime_put(intelhaddata->dev);
1050 return 0;
1051}
1052
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001053/*
1054 * snd_intelhad_hw_params - to setup the hardware parameters
1055 * like allocating the buffers
1056 * @substream: substream for which the function is called
Jerome Anand5dab11d2017-01-25 04:27:52 +05301057 * @hw_params: hardware parameters
1058 *
1059 * This function is called by ALSA framework when hardware params are set
1060 */
1061static int snd_intelhad_hw_params(struct snd_pcm_substream *substream,
1062 struct snd_pcm_hw_params *hw_params)
1063{
Takashi Iwaic75b0472017-01-31 15:49:15 +01001064 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301065 unsigned long addr;
1066 int pages, buf_size, retval;
1067
Jerome Anand5dab11d2017-01-25 04:27:52 +05301068 if (!hw_params)
1069 return -EINVAL;
1070
Takashi Iwaic75b0472017-01-31 15:49:15 +01001071 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301072 buf_size = params_buffer_bytes(hw_params);
1073 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1074 if (retval < 0)
1075 return retval;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001076 dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
1077 __func__, buf_size);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301078 /* mark the pages as uncached region */
1079 addr = (unsigned long) substream->runtime->dma_area;
1080 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1081 retval = set_memory_uc(addr, pages);
1082 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001083 dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
1084 retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301085 return retval;
1086 }
1087 memset(substream->runtime->dma_area, 0, buf_size);
1088
1089 return retval;
1090}
1091
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001092/*
1093 * snd_intelhad_hw_free - to release the resources allocated during
1094 * hardware params setup
Jerome Anand5dab11d2017-01-25 04:27:52 +05301095 * @substream: substream for which the function is called
1096 *
1097 * This function is called by ALSA framework before close callback.
Jerome Anand5dab11d2017-01-25 04:27:52 +05301098 */
1099static int snd_intelhad_hw_free(struct snd_pcm_substream *substream)
1100{
1101 unsigned long addr;
1102 u32 pages;
1103
Jerome Anand5dab11d2017-01-25 04:27:52 +05301104 /* mark back the pages as cached/writeback region before the free */
1105 if (substream->runtime->dma_area != NULL) {
1106 addr = (unsigned long) substream->runtime->dma_area;
1107 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1108 PAGE_SIZE;
1109 set_memory_wb(addr, pages);
1110 return snd_pcm_lib_free_pages(substream);
1111 }
1112 return 0;
1113}
1114
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001115/*
Jerome Anand5dab11d2017-01-25 04:27:52 +05301116 * snd_intelhad_pcm_trigger - stream activities are handled here
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001117 * @substream: substream for which the stream function is called
1118 * @cmd: the stream commamd thats requested from upper layer
1119 *
Jerome Anand5dab11d2017-01-25 04:27:52 +05301120 * This function is called whenever an a stream activity is invoked
1121 */
1122static int snd_intelhad_pcm_trigger(struct snd_pcm_substream *substream,
1123 int cmd)
1124{
Takashi Iwaida864802017-01-31 13:52:22 +01001125 int retval = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301126 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301127
Jerome Anand5dab11d2017-01-25 04:27:52 +05301128 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301129
1130 switch (cmd) {
1131 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001132 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1133 case SNDRV_PCM_TRIGGER_RESUME:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301134 /* Disable local INTRs till register prgmng is done */
Takashi Iwai79f439e2017-01-31 16:46:44 +01001135 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001136 dev_dbg(intelhaddata->dev,
1137 "_START: HDMI cable plugged-out\n");
Jerome Anand5dab11d2017-01-25 04:27:52 +05301138 retval = -ENODEV;
1139 break;
1140 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301141
Takashi Iwaif69bd102017-02-02 14:57:22 +01001142 intelhaddata->stream_info.running = true;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301143
1144 /* Enable Audio */
Takashi Iwaida864802017-01-31 13:52:22 +01001145 snd_intelhad_enable_audio_int(intelhaddata, true);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001146 snd_intelhad_enable_audio(substream, intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301147 break;
1148
1149 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001150 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1151 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwaibcce7752017-02-01 17:18:20 +01001152 spin_lock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301153 intelhaddata->curr_buf = 0;
1154
Takashi Iwaic75b0472017-01-31 15:49:15 +01001155 /* Stop reporting BUFFER_DONE/UNDERRUN to above layers */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301156
Takashi Iwaif69bd102017-02-02 14:57:22 +01001157 intelhaddata->stream_info.running = false;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001158 spin_unlock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301159 /* Disable Audio */
Takashi Iwaida864802017-01-31 13:52:22 +01001160 snd_intelhad_enable_audio_int(intelhaddata, false);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001161 snd_intelhad_enable_audio(substream, intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301162 /* Reset buffer pointers */
Takashi Iwai79dda752017-01-30 17:23:39 +01001163 snd_intelhad_reset_audio(intelhaddata, 1);
1164 snd_intelhad_reset_audio(intelhaddata, 0);
Takashi Iwaida864802017-01-31 13:52:22 +01001165 snd_intelhad_enable_audio_int(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301166 break;
1167
1168 default:
1169 retval = -EINVAL;
1170 }
1171 return retval;
1172}
1173
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001174/*
1175 * snd_intelhad_pcm_prepare - internal preparation before starting a stream
1176 * @substream: substream for which the function is called
Jerome Anand5dab11d2017-01-25 04:27:52 +05301177 *
1178 * This function is called when a stream is started for internal preparation.
1179 */
1180static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream)
1181{
1182 int retval;
1183 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001184 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301185 struct snd_intelhad *intelhaddata;
1186 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301187
Jerome Anand5dab11d2017-01-25 04:27:52 +05301188 intelhaddata = snd_pcm_substream_chip(substream);
1189 runtime = substream->runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301190
Takashi Iwai79f439e2017-01-31 16:46:44 +01001191 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001192 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
1193 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301194 retval = -ENODEV;
1195 goto prep_end;
1196 }
1197
Takashi Iwaic75b0472017-01-31 15:49:15 +01001198 dev_dbg(intelhaddata->dev, "period_size=%d\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301199 (int)frames_to_bytes(runtime, runtime->period_size));
Takashi Iwaic75b0472017-01-31 15:49:15 +01001200 dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods);
1201 dev_dbg(intelhaddata->dev, "buffer_size=%d\n",
1202 (int)snd_pcm_lib_buffer_bytes(substream));
1203 dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
1204 dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301205
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001206 intelhaddata->stream_info.buffer_rendered = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301207
1208 /* Get N value in KHz */
Takashi Iwaida864802017-01-31 13:52:22 +01001209 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301210
Takashi Iwai76296ef2017-01-30 16:09:11 +01001211 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1212 intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301213 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001214 dev_err(intelhaddata->dev,
1215 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301216 goto prep_end;
1217 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001218
1219 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001220 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001221
Takashi Iwai76296ef2017-01-30 16:09:11 +01001222 snd_intelhad_prog_cts(substream->runtime->rate,
1223 disp_samp_freq, link_rate,
1224 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301225
Takashi Iwai76296ef2017-01-30 16:09:11 +01001226 snd_intelhad_prog_dip(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301227
Takashi Iwai76296ef2017-01-30 16:09:11 +01001228 retval = snd_intelhad_audio_ctrl(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301229
1230 /* Prog buffer address */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001231 retval = snd_intelhad_prog_buffer(substream, intelhaddata,
Jerome Anand5dab11d2017-01-25 04:27:52 +05301232 HAD_BUF_TYPE_A, HAD_BUF_TYPE_D);
1233
1234 /*
1235 * Program channel mapping in following order:
1236 * FL, FR, C, LFE, RL, RR
1237 */
1238
Takashi Iwai79dda752017-01-30 17:23:39 +01001239 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301240
1241prep_end:
1242 return retval;
1243}
1244
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001245/*
Jerome Anand5dab11d2017-01-25 04:27:52 +05301246 * snd_intelhad_pcm_pointer- to send the current buffer pointerprocessed by hw
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001247 * @substream: substream for which the function is called
Jerome Anand5dab11d2017-01-25 04:27:52 +05301248 *
1249 * This function is called by ALSA framework to get the current hw buffer ptr
1250 * when a period is elapsed
1251 */
1252static snd_pcm_uframes_t snd_intelhad_pcm_pointer(
1253 struct snd_pcm_substream *substream)
1254{
1255 struct snd_intelhad *intelhaddata;
1256 u32 bytes_rendered = 0;
1257 u32 t;
1258 int buf_id;
1259
Jerome Anand5dab11d2017-01-25 04:27:52 +05301260 intelhaddata = snd_pcm_substream_chip(substream);
1261
Takashi Iwai79f439e2017-01-31 16:46:44 +01001262 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
1263 return SNDRV_PCM_POS_XRUN;
1264
Jerome Anand5dab11d2017-01-25 04:27:52 +05301265 /* Use a hw register to calculate sub-period position reports.
1266 * This makes PulseAudio happier.
1267 */
1268
1269 buf_id = intelhaddata->curr_buf % 4;
Takashi Iwai79dda752017-01-30 17:23:39 +01001270 had_read_register(intelhaddata,
1271 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH), &t);
Jerome Anand232892f2017-01-25 04:27:53 +05301272
1273 if ((t == 0) || (t == ((u32)-1L))) {
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001274 intelhaddata->underrun_count++;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001275 dev_dbg(intelhaddata->dev,
1276 "discovered buffer done for buf %d, count = %d\n",
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001277 buf_id, intelhaddata->underrun_count);
Jerome Anand232892f2017-01-25 04:27:53 +05301278
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001279 if (intelhaddata->underrun_count > (HAD_MIN_PERIODS/2)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001280 dev_dbg(intelhaddata->dev,
1281 "assume audio_codec_reset, underrun = %d - do xrun\n",
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001282 intelhaddata->underrun_count);
1283 intelhaddata->underrun_count = 0;
Jerome Anand232892f2017-01-25 04:27:53 +05301284 return SNDRV_PCM_POS_XRUN;
1285 }
1286 } else {
1287 /* Reset Counter */
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001288 intelhaddata->underrun_count = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301289 }
Jerome Anand232892f2017-01-25 04:27:53 +05301290
Jerome Anand5dab11d2017-01-25 04:27:52 +05301291 t = intelhaddata->buf_info[buf_id].buf_size - t;
1292
1293 if (intelhaddata->stream_info.buffer_rendered)
1294 div_u64_rem(intelhaddata->stream_info.buffer_rendered,
1295 intelhaddata->stream_info.ring_buf_size,
1296 &(bytes_rendered));
1297
Takashi Iwai7d9e7982017-02-01 22:25:58 +01001298 return bytes_to_frames(substream->runtime, bytes_rendered + t);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301299}
1300
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001301/*
Jerome Anand5dab11d2017-01-25 04:27:52 +05301302 * snd_intelhad_pcm_mmap- mmaps a kernel buffer to user space for copying data
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001303 * @substream: substream for which the function is called
1304 * @vma: struct instance of memory VMM memory area
Jerome Anand5dab11d2017-01-25 04:27:52 +05301305 *
1306 * This function is called by OS when a user space component
1307 * tries to get mmap memory from driver
1308 */
1309static int snd_intelhad_pcm_mmap(struct snd_pcm_substream *substream,
1310 struct vm_area_struct *vma)
1311{
Jerome Anand5dab11d2017-01-25 04:27:52 +05301312 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1313 return remap_pfn_range(vma, vma->vm_start,
1314 substream->dma_buffer.addr >> PAGE_SHIFT,
1315 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1316}
1317
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001318/* process mode change of the running stream; called in mutex */
Takashi Iwaida864802017-01-31 13:52:22 +01001319static int hdmi_audio_mode_change(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301320{
Takashi Iwaida864802017-01-31 13:52:22 +01001321 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301322 int retval = 0;
1323 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001324 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301325
Takashi Iwai313d9f22017-02-02 13:00:12 +01001326 substream = had_substream_get(intelhaddata);
1327 if (!substream)
Takashi Iwaida864802017-01-31 13:52:22 +01001328 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301329
1330 /* Disable Audio */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001331 snd_intelhad_enable_audio(substream, intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301332
1333 /* Update CTS value */
Takashi Iwaida864802017-01-31 13:52:22 +01001334 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301335
Takashi Iwai76296ef2017-01-30 16:09:11 +01001336 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1337 intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301338 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001339 dev_err(intelhaddata->dev,
1340 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301341 goto out;
1342 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001343
1344 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001345 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001346
Takashi Iwai76296ef2017-01-30 16:09:11 +01001347 snd_intelhad_prog_cts(substream->runtime->rate,
1348 disp_samp_freq, link_rate,
1349 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301350
1351 /* Enable Audio */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001352 snd_intelhad_enable_audio(substream, intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301353
1354out:
Takashi Iwai313d9f22017-02-02 13:00:12 +01001355 had_substream_put(intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301356 return retval;
1357}
1358
Takashi Iwai372d8552017-01-31 13:57:58 +01001359static inline int had_chk_intrmiss(struct snd_intelhad *intelhaddata,
1360 enum intel_had_aud_buf_type buf_id)
1361{
1362 int i, intr_count = 0;
1363 enum intel_had_aud_buf_type buff_done;
1364 u32 buf_size, buf_addr;
Takashi Iwai372d8552017-01-31 13:57:58 +01001365
1366 buff_done = buf_id;
1367
1368 intr_count = snd_intelhad_read_len(intelhaddata);
1369 if (intr_count > 1) {
1370 /* In case of active playback */
Takashi Iwaic75b0472017-01-31 15:49:15 +01001371 dev_err(intelhaddata->dev,
1372 "Driver detected %d missed buffer done interrupt(s)\n",
1373 (intr_count - 1));
Takashi Iwai372d8552017-01-31 13:57:58 +01001374 if (intr_count > 3)
1375 return intr_count;
1376
1377 buf_id += (intr_count - 1);
1378 /* Reprogram registers*/
1379 for (i = buff_done; i < buf_id; i++) {
1380 int j = i % 4;
1381
1382 buf_size = intelhaddata->buf_info[j].buf_size;
1383 buf_addr = intelhaddata->buf_info[j].buf_addr;
1384 had_write_register(intelhaddata,
1385 AUD_BUF_A_LENGTH +
1386 (j * HAD_REG_WIDTH), buf_size);
1387 had_write_register(intelhaddata,
1388 AUD_BUF_A_ADDR+(j * HAD_REG_WIDTH),
1389 (buf_addr | BIT(0) | BIT(1)));
1390 }
1391 buf_id = buf_id % 4;
Takashi Iwai372d8552017-01-31 13:57:58 +01001392 intelhaddata->buff_done = buf_id;
Takashi Iwai372d8552017-01-31 13:57:58 +01001393 }
1394
1395 return intr_count;
1396}
1397
Takashi Iwaibcce7752017-02-01 17:18:20 +01001398/* called from irq handler */
Takashi Iwai372d8552017-01-31 13:57:58 +01001399static int had_process_buffer_done(struct snd_intelhad *intelhaddata)
1400{
1401 u32 len = 1;
1402 enum intel_had_aud_buf_type buf_id;
1403 enum intel_had_aud_buf_type buff_done;
1404 struct pcm_stream_info *stream;
Takashi Iwai313d9f22017-02-02 13:00:12 +01001405 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001406 u32 buf_size;
Takashi Iwai372d8552017-01-31 13:57:58 +01001407 int intr_count;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001408 unsigned long flags;
Takashi Iwai372d8552017-01-31 13:57:58 +01001409
Takashi Iwai372d8552017-01-31 13:57:58 +01001410 stream = &intelhaddata->stream_info;
1411 intr_count = 1;
1412
Takashi Iwaibcce7752017-02-01 17:18:20 +01001413 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
Takashi Iwai372d8552017-01-31 13:57:58 +01001414 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaibcce7752017-02-01 17:18:20 +01001415 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001416 dev_dbg(intelhaddata->dev,
1417 "%s:Device already disconnected\n", __func__);
Takashi Iwai372d8552017-01-31 13:57:58 +01001418 return 0;
1419 }
1420 buf_id = intelhaddata->curr_buf;
1421 intelhaddata->buff_done = buf_id;
1422 buff_done = intelhaddata->buff_done;
1423 buf_size = intelhaddata->buf_info[buf_id].buf_size;
Takashi Iwai372d8552017-01-31 13:57:58 +01001424
Takashi Iwai372d8552017-01-31 13:57:58 +01001425 /* Every debug statement has an implication
1426 * of ~5msec. Thus, avoid having >3 debug statements
1427 * for each buffer_done handling.
1428 */
1429
1430 /* Check for any intr_miss in case of active playback */
Takashi Iwaif69bd102017-02-02 14:57:22 +01001431 if (stream->running) {
Takashi Iwai372d8552017-01-31 13:57:58 +01001432 intr_count = had_chk_intrmiss(intelhaddata, buf_id);
1433 if (!intr_count || (intr_count > 3)) {
Takashi Iwaibcce7752017-02-01 17:18:20 +01001434 spin_unlock_irqrestore(&intelhaddata->had_spinlock,
1435 flags);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001436 dev_err(intelhaddata->dev,
1437 "HAD SW state in non-recoverable mode\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001438 return 0;
1439 }
1440 buf_id += (intr_count - 1);
1441 buf_id = buf_id % 4;
Takashi Iwai372d8552017-01-31 13:57:58 +01001442 }
1443
1444 intelhaddata->buf_info[buf_id].is_valid = true;
1445 if (intelhaddata->valid_buf_cnt-1 == buf_id) {
Takashi Iwaif69bd102017-02-02 14:57:22 +01001446 if (stream->running)
Takashi Iwai372d8552017-01-31 13:57:58 +01001447 intelhaddata->curr_buf = HAD_BUF_TYPE_A;
1448 } else
1449 intelhaddata->curr_buf = buf_id + 1;
1450
Takashi Iwaibcce7752017-02-01 17:18:20 +01001451 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
Takashi Iwai372d8552017-01-31 13:57:58 +01001452
Takashi Iwai79f439e2017-01-31 16:46:44 +01001453 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001454 dev_dbg(intelhaddata->dev, "HDMI cable plugged-out\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001455 return 0;
1456 }
1457
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001458 /* Reprogram the registers with addr and length */
Takashi Iwai372d8552017-01-31 13:57:58 +01001459 had_write_register(intelhaddata,
1460 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH),
1461 buf_size);
1462 had_write_register(intelhaddata,
1463 AUD_BUF_A_ADDR + (buf_id * HAD_REG_WIDTH),
1464 intelhaddata->buf_info[buf_id].buf_addr |
1465 BIT(0) | BIT(1));
1466
1467 had_read_register(intelhaddata,
1468 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH),
1469 &len);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001470 dev_dbg(intelhaddata->dev, "%s:Enabled buf[%d]\n", __func__, buf_id);
Takashi Iwai372d8552017-01-31 13:57:58 +01001471
1472 /* In case of actual data,
1473 * report buffer_done to above ALSA layer
1474 */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001475 substream = had_substream_get(intelhaddata);
1476 if (substream) {
1477 buf_size = intelhaddata->buf_info[buf_id].buf_size;
Takashi Iwai372d8552017-01-31 13:57:58 +01001478 intelhaddata->stream_info.buffer_rendered +=
1479 (intr_count * buf_size);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001480 snd_pcm_period_elapsed(substream);
1481 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001482 }
1483
1484 return 0;
1485}
1486
Takashi Iwaibcce7752017-02-01 17:18:20 +01001487/* called from irq handler */
Takashi Iwai372d8552017-01-31 13:57:58 +01001488static int had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
1489{
1490 enum intel_had_aud_buf_type buf_id;
1491 struct pcm_stream_info *stream;
Takashi Iwai313d9f22017-02-02 13:00:12 +01001492 struct snd_pcm_substream *substream;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001493 unsigned long flags;
Takashi Iwai372d8552017-01-31 13:57:58 +01001494 int drv_status;
1495
Takashi Iwai372d8552017-01-31 13:57:58 +01001496 stream = &intelhaddata->stream_info;
1497
Takashi Iwaibcce7752017-02-01 17:18:20 +01001498 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
Takashi Iwai372d8552017-01-31 13:57:58 +01001499 buf_id = intelhaddata->curr_buf;
Takashi Iwai372d8552017-01-31 13:57:58 +01001500 intelhaddata->buff_done = buf_id;
1501 drv_status = intelhaddata->drv_status;
Takashi Iwaif69bd102017-02-02 14:57:22 +01001502 if (stream->running)
Takashi Iwai372d8552017-01-31 13:57:58 +01001503 intelhaddata->curr_buf = HAD_BUF_TYPE_A;
1504
Takashi Iwaibcce7752017-02-01 17:18:20 +01001505 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
Takashi Iwai372d8552017-01-31 13:57:58 +01001506
Takashi Iwaif69bd102017-02-02 14:57:22 +01001507 dev_dbg(intelhaddata->dev, "Enter:%s buf_id=%d, stream_running=%d\n",
1508 __func__, buf_id, stream->running);
Takashi Iwai372d8552017-01-31 13:57:58 +01001509
1510 snd_intelhad_handle_underrun(intelhaddata);
1511
1512 if (drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001513 dev_dbg(intelhaddata->dev,
1514 "%s:Device already disconnected\n", __func__);
Takashi Iwai372d8552017-01-31 13:57:58 +01001515 return 0;
1516 }
1517
Takashi Iwaif69bd102017-02-02 14:57:22 +01001518 /* Report UNDERRUN error to above layers */
1519 substream = had_substream_get(intelhaddata);
1520 if (substream) {
1521 snd_pcm_stop_xrun(substream);
1522 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001523 }
1524
1525 return 0;
1526}
1527
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001528/* process hot plug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001529static void had_process_hot_plug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001530{
1531 enum intel_had_aud_buf_type buf_id;
1532 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001533
Takashi Iwaibcce7752017-02-01 17:18:20 +01001534 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001535 if (intelhaddata->drv_status == HAD_DRV_CONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001536 dev_dbg(intelhaddata->dev, "Device already connected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001537 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001538 return;
Takashi Iwai372d8552017-01-31 13:57:58 +01001539 }
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001540
Takashi Iwai372d8552017-01-31 13:57:58 +01001541 buf_id = intelhaddata->curr_buf;
1542 intelhaddata->buff_done = buf_id;
1543 intelhaddata->drv_status = HAD_DRV_CONNECTED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001544 dev_dbg(intelhaddata->dev,
1545 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001546 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001547 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001548
Takashi Iwaic75b0472017-01-31 15:49:15 +01001549 dev_dbg(intelhaddata->dev, "Processing HOT_PLUG, buf_id = %d\n",
1550 buf_id);
Takashi Iwai372d8552017-01-31 13:57:58 +01001551
1552 /* Safety check */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001553 substream = had_substream_get(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001554 if (substream) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001555 dev_dbg(intelhaddata->dev,
1556 "Force to stop the active stream by disconnection\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001557 /* Set runtime->state to hw_params done */
1558 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001559 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001560 }
1561
1562 had_build_channel_allocation_map(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001563}
1564
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001565/* process hot unplug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001566static void had_process_hot_unplug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001567{
1568 enum intel_had_aud_buf_type buf_id;
Takashi Iwai313d9f22017-02-02 13:00:12 +01001569 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001570
Takashi Iwai372d8552017-01-31 13:57:58 +01001571 buf_id = intelhaddata->curr_buf;
1572
Takashi Iwai313d9f22017-02-02 13:00:12 +01001573 substream = had_substream_get(intelhaddata);
1574
Takashi Iwaibcce7752017-02-01 17:18:20 +01001575 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001576
1577 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001578 dev_dbg(intelhaddata->dev, "Device already disconnected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001579 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001580 goto out;
Takashi Iwai372d8552017-01-31 13:57:58 +01001581
Takashi Iwai372d8552017-01-31 13:57:58 +01001582 }
1583
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001584 /* Disable Audio */
1585 snd_intelhad_enable_audio_int(intelhaddata, false);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001586 snd_intelhad_enable_audio(substream, intelhaddata, false);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001587
Takashi Iwai372d8552017-01-31 13:57:58 +01001588 intelhaddata->drv_status = HAD_DRV_DISCONNECTED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001589 dev_dbg(intelhaddata->dev,
1590 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001591 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001592 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001593
1594 /* Report to above ALSA layer */
1595 if (substream)
1596 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1597
1598 out:
1599 if (substream)
1600 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001601 kfree(intelhaddata->chmap->chmap);
1602 intelhaddata->chmap->chmap = NULL;
Takashi Iwai372d8552017-01-31 13:57:58 +01001603}
1604
1605/* PCM operations structure and the calls back for the same */
1606static struct snd_pcm_ops snd_intelhad_playback_ops = {
Jerome Anand5dab11d2017-01-25 04:27:52 +05301607 .open = snd_intelhad_open,
1608 .close = snd_intelhad_close,
1609 .ioctl = snd_pcm_lib_ioctl,
1610 .hw_params = snd_intelhad_hw_params,
1611 .hw_free = snd_intelhad_hw_free,
1612 .prepare = snd_intelhad_pcm_prepare,
1613 .trigger = snd_intelhad_pcm_trigger,
1614 .pointer = snd_intelhad_pcm_pointer,
1615 .mmap = snd_intelhad_pcm_mmap,
1616};
1617
Jerome Anand5dab11d2017-01-25 04:27:52 +05301618static int had_iec958_info(struct snd_kcontrol *kcontrol,
1619 struct snd_ctl_elem_info *uinfo)
1620{
1621 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1622 uinfo->count = 1;
1623 return 0;
1624}
1625
1626static int had_iec958_get(struct snd_kcontrol *kcontrol,
1627 struct snd_ctl_elem_value *ucontrol)
1628{
1629 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1630
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001631 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301632 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1633 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1634 ucontrol->value.iec958.status[2] =
1635 (intelhaddata->aes_bits >> 16) & 0xff;
1636 ucontrol->value.iec958.status[3] =
1637 (intelhaddata->aes_bits >> 24) & 0xff;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001638 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301639 return 0;
1640}
Takashi Iwai372d8552017-01-31 13:57:58 +01001641
Jerome Anand5dab11d2017-01-25 04:27:52 +05301642static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1643 struct snd_ctl_elem_value *ucontrol)
1644{
1645 ucontrol->value.iec958.status[0] = 0xff;
1646 ucontrol->value.iec958.status[1] = 0xff;
1647 ucontrol->value.iec958.status[2] = 0xff;
1648 ucontrol->value.iec958.status[3] = 0xff;
1649 return 0;
1650}
Takashi Iwai372d8552017-01-31 13:57:58 +01001651
Jerome Anand5dab11d2017-01-25 04:27:52 +05301652static int had_iec958_put(struct snd_kcontrol *kcontrol,
1653 struct snd_ctl_elem_value *ucontrol)
1654{
1655 unsigned int val;
1656 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001657 int changed = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301658
Jerome Anand5dab11d2017-01-25 04:27:52 +05301659 val = (ucontrol->value.iec958.status[0] << 0) |
1660 (ucontrol->value.iec958.status[1] << 8) |
1661 (ucontrol->value.iec958.status[2] << 16) |
1662 (ucontrol->value.iec958.status[3] << 24);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001663 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301664 if (intelhaddata->aes_bits != val) {
1665 intelhaddata->aes_bits = val;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001666 changed = 1;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301667 }
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001668 mutex_unlock(&intelhaddata->mutex);
1669 return changed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301670}
1671
Takashi Iwai4aedb942017-02-02 16:38:39 +01001672static int had_ctl_eld_info(struct snd_kcontrol *kcontrol,
1673 struct snd_ctl_elem_info *uinfo)
1674{
1675 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1676 uinfo->count = HDMI_MAX_ELD_BYTES;
1677 return 0;
1678}
1679
1680static int had_ctl_eld_get(struct snd_kcontrol *kcontrol,
1681 struct snd_ctl_elem_value *ucontrol)
1682{
1683 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1684
1685 mutex_lock(&intelhaddata->mutex);
1686 memcpy(ucontrol->value.bytes.data, intelhaddata->eld,
1687 HDMI_MAX_ELD_BYTES);
1688 mutex_unlock(&intelhaddata->mutex);
1689 return 0;
1690}
1691
1692static struct snd_kcontrol_new had_controls[] = {
1693 {
1694 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1695 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1696 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1697 .info = had_iec958_info, /* shared */
1698 .get = had_iec958_mask_get,
1699 },
1700 {
1701 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1702 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1703 .info = had_iec958_info,
1704 .get = had_iec958_get,
1705 .put = had_iec958_put,
1706 },
1707 {
1708 .access = (SNDRV_CTL_ELEM_ACCESS_READ |
1709 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1710 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1711 .name = "ELD",
1712 .info = had_ctl_eld_info,
1713 .get = had_ctl_eld_get,
1714 },
Jerome Anand5dab11d2017-01-25 04:27:52 +05301715};
1716
Jerome Anand5dab11d2017-01-25 04:27:52 +05301717
Takashi Iwaida864802017-01-31 13:52:22 +01001718static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id)
1719{
1720 struct snd_intelhad *ctx = dev_id;
1721 u32 audio_stat, audio_reg;
1722
Takashi Iwai4151ee82017-01-31 18:14:15 +01001723 audio_reg = AUD_HDMI_STATUS;
Takashi Iwaida864802017-01-31 13:52:22 +01001724 mid_hdmi_audio_read(ctx, audio_reg, &audio_stat);
1725
1726 if (audio_stat & HDMI_AUDIO_UNDERRUN) {
1727 mid_hdmi_audio_write(ctx, audio_reg, HDMI_AUDIO_UNDERRUN);
1728 had_process_buffer_underrun(ctx);
1729 }
1730
1731 if (audio_stat & HDMI_AUDIO_BUFFER_DONE) {
1732 mid_hdmi_audio_write(ctx, audio_reg, HDMI_AUDIO_BUFFER_DONE);
1733 had_process_buffer_done(ctx);
1734 }
1735
1736 return IRQ_HANDLED;
1737}
1738
1739static void notify_audio_lpe(struct platform_device *pdev)
1740{
1741 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Takashi Iwaida864802017-01-31 13:52:22 +01001742
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001743 schedule_work(&ctx->hdmi_audio_wq);
1744}
Takashi Iwaida864802017-01-31 13:52:22 +01001745
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001746static void had_audio_wq(struct work_struct *work)
1747{
1748 struct snd_intelhad *ctx =
1749 container_of(work, struct snd_intelhad, hdmi_audio_wq);
1750 struct intel_hdmi_lpe_audio_pdata *pdata = ctx->dev->platform_data;
1751
Takashi Iwai182cdf22017-02-02 14:43:39 +01001752 pm_runtime_get_sync(ctx->dev);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001753 mutex_lock(&ctx->mutex);
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001754 if (!pdata->hdmi_connected) {
1755 dev_dbg(ctx->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001756 __func__);
Takashi Iwai4aedb942017-02-02 16:38:39 +01001757 memset(ctx->eld, 0, sizeof(ctx->eld)); /* clear the old ELD */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001758 had_process_hot_unplug(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001759 } else {
1760 struct intel_hdmi_lpe_audio_eld *eld = &pdata->eld;
1761
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001762 dev_dbg(ctx->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
1763 __func__, eld->port_id, pdata->tmds_clock_speed);
1764
Takashi Iwaida864802017-01-31 13:52:22 +01001765 switch (eld->pipe_id) {
1766 case 0:
1767 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1768 break;
1769 case 1:
1770 ctx->had_config_offset = AUDIO_HDMI_CONFIG_B;
1771 break;
1772 case 2:
1773 ctx->had_config_offset = AUDIO_HDMI_CONFIG_C;
1774 break;
1775 default:
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001776 dev_dbg(ctx->dev, "Invalid pipe %d\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001777 eld->pipe_id);
1778 break;
1779 }
1780
Takashi Iwaidf0435d2017-02-02 15:37:11 +01001781 memcpy(ctx->eld, eld->eld_data, sizeof(ctx->eld));
Takashi Iwaida864802017-01-31 13:52:22 +01001782
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001783 ctx->dp_output = pdata->dp_output;
1784 ctx->tmds_clock_speed = pdata->tmds_clock_speed;
1785 ctx->link_rate = pdata->link_rate;
1786
Takashi Iwaida864802017-01-31 13:52:22 +01001787 had_process_hot_plug(ctx);
1788
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001789 /* Process mode change if stream is active */
Takashi Iwaif69bd102017-02-02 14:57:22 +01001790 hdmi_audio_mode_change(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001791 }
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001792 mutex_unlock(&ctx->mutex);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001793 pm_runtime_put(ctx->dev);
1794}
1795
1796/*
1797 * PM callbacks
1798 */
1799
1800static int hdmi_lpe_audio_runtime_suspend(struct device *dev)
1801{
1802 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1803 struct snd_pcm_substream *substream;
1804
1805 substream = had_substream_get(ctx);
1806 if (substream) {
1807 snd_pcm_suspend(substream);
1808 had_substream_put(ctx);
1809 }
1810
1811 return 0;
1812}
1813
1814static int hdmi_lpe_audio_suspend(struct device *dev)
1815{
1816 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1817 int err;
1818
1819 err = hdmi_lpe_audio_runtime_suspend(dev);
1820 if (!err)
1821 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D3hot);
1822 return err;
1823}
1824
1825static int hdmi_lpe_audio_resume(struct device *dev)
1826{
1827 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1828
1829 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D0);
1830 return 0;
Takashi Iwaida864802017-01-31 13:52:22 +01001831}
1832
1833/* release resources */
1834static void hdmi_lpe_audio_free(struct snd_card *card)
1835{
1836 struct snd_intelhad *ctx = card->private_data;
1837
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001838 cancel_work_sync(&ctx->hdmi_audio_wq);
1839
Takashi Iwaida864802017-01-31 13:52:22 +01001840 if (ctx->mmio_start)
1841 iounmap(ctx->mmio_start);
1842 if (ctx->irq >= 0)
1843 free_irq(ctx->irq, ctx);
1844}
1845
1846/*
1847 * hdmi_lpe_audio_probe - start bridge with i915
1848 *
1849 * This function is called when the i915 driver creates the
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001850 * hdmi-lpe-audio platform device.
Takashi Iwaida864802017-01-31 13:52:22 +01001851 */
1852static int hdmi_lpe_audio_probe(struct platform_device *pdev)
1853{
1854 struct snd_card *card;
1855 struct snd_intelhad *ctx;
1856 struct snd_pcm *pcm;
1857 struct intel_hdmi_lpe_audio_pdata *pdata;
1858 int irq;
1859 struct resource *res_mmio;
Takashi Iwai4aedb942017-02-02 16:38:39 +01001860 int i, ret;
Takashi Iwaida864802017-01-31 13:52:22 +01001861
Takashi Iwaida864802017-01-31 13:52:22 +01001862 dev_dbg(&pdev->dev, "dma_mask: %p\n", pdev->dev.dma_mask);
1863
1864 pdata = pdev->dev.platform_data;
1865 if (!pdata) {
1866 dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
1867 return -EINVAL;
1868 }
1869
1870 /* get resources */
1871 irq = platform_get_irq(pdev, 0);
1872 if (irq < 0) {
1873 dev_err(&pdev->dev, "Could not get irq resource\n");
1874 return -ENODEV;
1875 }
1876
1877 res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1878 if (!res_mmio) {
1879 dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
1880 return -ENXIO;
1881 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301882
Takashi Iwai5647aec2017-01-31 08:14:34 +01001883 /* create a card instance with ALSA framework */
Takashi Iwaida864802017-01-31 13:52:22 +01001884 ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
1885 THIS_MODULE, sizeof(*ctx), &card);
1886 if (ret)
1887 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301888
Takashi Iwaida864802017-01-31 13:52:22 +01001889 ctx = card->private_data;
1890 spin_lock_init(&ctx->had_spinlock);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001891 mutex_init(&ctx->mutex);
Takashi Iwaida864802017-01-31 13:52:22 +01001892 ctx->drv_status = HAD_DRV_DISCONNECTED;
1893 ctx->dev = &pdev->dev;
1894 ctx->card = card;
Takashi Iwaida864802017-01-31 13:52:22 +01001895 ctx->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
1896 strcpy(card->driver, INTEL_HAD);
1897 strcpy(card->shortname, INTEL_HAD);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301898
Takashi Iwaida864802017-01-31 13:52:22 +01001899 ctx->irq = -1;
1900 ctx->tmds_clock_speed = DIS_SAMPLE_RATE_148_5;
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001901 INIT_WORK(&ctx->hdmi_audio_wq, had_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301902
Takashi Iwaida864802017-01-31 13:52:22 +01001903 card->private_free = hdmi_lpe_audio_free;
1904
1905 /* assume pipe A as default */
1906 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1907
1908 platform_set_drvdata(pdev, ctx);
1909
1910 dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
1911 __func__, (unsigned int)res_mmio->start,
1912 (unsigned int)res_mmio->end);
1913
1914 ctx->mmio_start = ioremap_nocache(res_mmio->start,
1915 (size_t)(resource_size(res_mmio)));
1916 if (!ctx->mmio_start) {
1917 dev_err(&pdev->dev, "Could not get ioremap\n");
1918 ret = -EACCES;
1919 goto err;
1920 }
1921
1922 /* setup interrupt handler */
1923 ret = request_irq(irq, display_pipe_interrupt_handler, 0,
1924 pdev->name, ctx);
1925 if (ret < 0) {
1926 dev_err(&pdev->dev, "request_irq failed\n");
1927 goto err;
1928 }
1929
1930 ctx->irq = irq;
1931
1932 ret = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
1933 MAX_CAP_STREAMS, &pcm);
1934 if (ret)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301935 goto err;
1936
1937 /* setup private data which can be retrieved when required */
Takashi Iwaida864802017-01-31 13:52:22 +01001938 pcm->private_data = ctx;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301939 pcm->info_flags = 0;
1940 strncpy(pcm->name, card->shortname, strlen(card->shortname));
Takashi Iwaida864802017-01-31 13:52:22 +01001941 /* setup the ops for playabck */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301942 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1943 &snd_intelhad_playback_ops);
1944 /* allocate dma pages for ALSA stream operations
1945 * memory allocated is based on size, not max value
1946 * thus using same argument for max & size
1947 */
Takashi Iwaida864802017-01-31 13:52:22 +01001948 snd_pcm_lib_preallocate_pages_for_all(pcm,
Jerome Anand5dab11d2017-01-25 04:27:52 +05301949 SNDRV_DMA_TYPE_DEV, NULL,
1950 HAD_MAX_BUFFER, HAD_MAX_BUFFER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301951
Takashi Iwai4aedb942017-02-02 16:38:39 +01001952 /* create controls */
1953 for (i = 0; i < ARRAY_SIZE(had_controls); i++) {
1954 ret = snd_ctl_add(card, snd_ctl_new1(&had_controls[i], ctx));
1955 if (ret < 0)
1956 goto err;
1957 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301958
1959 init_channel_allocations();
1960
1961 /* Register channel map controls */
Takashi Iwaida864802017-01-31 13:52:22 +01001962 ret = had_register_chmap_ctls(ctx, pcm);
1963 if (ret < 0)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301964 goto err;
1965
Takashi Iwaida864802017-01-31 13:52:22 +01001966 ret = snd_card_register(card);
1967 if (ret)
Takashi Iwai36ec0d92017-01-31 08:47:05 +01001968 goto err;
1969
Takashi Iwaibcce7752017-02-01 17:18:20 +01001970 spin_lock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001971 pdata->notify_audio_lpe = notify_audio_lpe;
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001972 pdata->notify_pending = false;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001973 spin_unlock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001974
1975 pm_runtime_set_active(&pdev->dev);
1976 pm_runtime_enable(&pdev->dev);
1977
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001978 dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
Takashi Iwaida864802017-01-31 13:52:22 +01001979 schedule_work(&ctx->hdmi_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301980
Takashi Iwai79dda752017-01-30 17:23:39 +01001981 return 0;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001982
Jerome Anand5dab11d2017-01-25 04:27:52 +05301983err:
1984 snd_card_free(card);
Takashi Iwaida864802017-01-31 13:52:22 +01001985 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301986}
1987
Takashi Iwai79dda752017-01-30 17:23:39 +01001988/*
Takashi Iwaida864802017-01-31 13:52:22 +01001989 * hdmi_lpe_audio_remove - stop bridge with i915
Jerome Anand5dab11d2017-01-25 04:27:52 +05301990 *
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001991 * This function is called when the platform device is destroyed.
Jerome Anand5dab11d2017-01-25 04:27:52 +05301992 */
Takashi Iwaida864802017-01-31 13:52:22 +01001993static int hdmi_lpe_audio_remove(struct platform_device *pdev)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301994{
Takashi Iwaida864802017-01-31 13:52:22 +01001995 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301996
Takashi Iwaida864802017-01-31 13:52:22 +01001997 if (ctx->drv_status != HAD_DRV_DISCONNECTED)
1998 snd_intelhad_enable_audio_int(ctx, false);
1999 snd_card_free(ctx->card);
Jerome Anand5dab11d2017-01-25 04:27:52 +05302000 return 0;
2001}
2002
Takashi Iwai182cdf22017-02-02 14:43:39 +01002003static const struct dev_pm_ops hdmi_lpe_audio_pm = {
2004 SET_SYSTEM_SLEEP_PM_OPS(hdmi_lpe_audio_suspend, hdmi_lpe_audio_resume)
2005 SET_RUNTIME_PM_OPS(hdmi_lpe_audio_runtime_suspend, NULL, NULL)
2006};
2007
Takashi Iwaida864802017-01-31 13:52:22 +01002008static struct platform_driver hdmi_lpe_audio_driver = {
2009 .driver = {
2010 .name = "hdmi-lpe-audio",
Takashi Iwai182cdf22017-02-02 14:43:39 +01002011 .pm = &hdmi_lpe_audio_pm,
Takashi Iwaida864802017-01-31 13:52:22 +01002012 },
2013 .probe = hdmi_lpe_audio_probe,
2014 .remove = hdmi_lpe_audio_remove,
Takashi Iwaida864802017-01-31 13:52:22 +01002015};
2016
2017module_platform_driver(hdmi_lpe_audio_driver);
2018MODULE_ALIAS("platform:hdmi_lpe_audio");
2019
Jerome Anand5dab11d2017-01-25 04:27:52 +05302020MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
2021MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
2022MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
2023MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
2024MODULE_DESCRIPTION("Intel HDMI Audio driver");
2025MODULE_LICENSE("GPL v2");
2026MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");