blob: 5613c675ce70a4c18802804e5d68bd431fc09dad [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/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100612 * Initialize Data Island Packets registers
Jerome Anand5dab11d2017-01-25 04:27:52 +0530613 * This function is called in the prepare callback
614 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100615static void snd_intelhad_prog_dip(struct snd_pcm_substream *substream,
616 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530617{
618 int i;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100619 union aud_ctrl_st ctrl_state = {.regval = 0};
620 union aud_info_frame2 frame2 = {.regval = 0};
621 union aud_info_frame3 frame3 = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530622 u8 checksum = 0;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600623 u32 info_frame;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530624 int channels;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100625 int ca;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530626
627 channels = substream->runtime->channels;
628
Takashi Iwai7ceba752017-02-02 15:58:35 +0100629 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530630
Takashi Iwai36ed3462017-02-02 17:06:38 +0100631 ca = snd_intelhad_channel_allocation(intelhaddata, channels);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600632 if (intelhaddata->dp_output) {
633 info_frame = DP_INFO_FRAME_WORD1;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100634 frame2.regval = (substream->runtime->channels - 1) | (ca << 24);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600635 } else {
636 info_frame = HDMI_INFO_FRAME_WORD1;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100637 frame2.regx.chnl_cnt = substream->runtime->channels - 1;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100638 frame3.regx.chnl_alloc = ca;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530639
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100640 /* Calculte the byte wide checksum for all valid DIP words */
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600641 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100642 checksum += (info_frame >> (i * 8)) & 0xff;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600643 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100644 checksum += (frame2.regval >> (i * 8)) & 0xff;
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 += (frame3.regval >> (i * 8)) & 0xff;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530647
Takashi Iwai7ceba752017-02-02 15:58:35 +0100648 frame2.regx.chksum = -(checksum);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600649 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530650
Takashi Iwai4151ee82017-01-31 18:14:15 +0100651 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, info_frame);
Takashi Iwai7ceba752017-02-02 15:58:35 +0100652 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame2.regval);
653 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame3.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530654
655 /* program remaining DIP words with zero */
656 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
Takashi Iwai4151ee82017-01-31 18:14:15 +0100657 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, 0x0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530658
Takashi Iwai7ceba752017-02-02 15:58:35 +0100659 ctrl_state.regx.dip_freq = 1;
660 ctrl_state.regx.dip_en_sta = 1;
661 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530662}
663
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100664/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100665 * Programs buffer address and length registers
Jerome Anand5dab11d2017-01-25 04:27:52 +0530666 * This function programs ring buffer address and length into registers.
667 */
Takashi Iwai313d9f22017-02-02 13:00:12 +0100668static int snd_intelhad_prog_buffer(struct snd_pcm_substream *substream,
669 struct snd_intelhad *intelhaddata,
670 int start, int end)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530671{
672 u32 ring_buf_addr, ring_buf_size, period_bytes;
673 u8 i, num_periods;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530674
675 ring_buf_addr = substream->runtime->dma_addr;
676 ring_buf_size = snd_pcm_lib_buffer_bytes(substream);
677 intelhaddata->stream_info.ring_buf_size = ring_buf_size;
678 period_bytes = frames_to_bytes(substream->runtime,
679 substream->runtime->period_size);
680 num_periods = substream->runtime->periods;
681
682 /*
683 * buffer addr should be 64 byte aligned, period bytes
684 * will be used to calculate addr offset
685 */
686 period_bytes &= ~0x3F;
687
688 /* Hardware supports MAX_PERIODS buffers */
689 if (end >= HAD_MAX_PERIODS)
690 return -EINVAL;
691
692 for (i = start; i <= end; i++) {
693 /* Program the buf registers with addr and len */
694 intelhaddata->buf_info[i].buf_addr = ring_buf_addr +
695 (i * period_bytes);
696 if (i < num_periods-1)
697 intelhaddata->buf_info[i].buf_size = period_bytes;
698 else
699 intelhaddata->buf_info[i].buf_size = ring_buf_size -
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100700 (i * period_bytes);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530701
Takashi Iwai79dda752017-01-30 17:23:39 +0100702 had_write_register(intelhaddata,
703 AUD_BUF_A_ADDR + (i * HAD_REG_WIDTH),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530704 intelhaddata->buf_info[i].buf_addr |
705 BIT(0) | BIT(1));
Takashi Iwai79dda752017-01-30 17:23:39 +0100706 had_write_register(intelhaddata,
707 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530708 period_bytes);
709 intelhaddata->buf_info[i].is_valid = true;
710 }
Takashi Iwaic75b0472017-01-31 15:49:15 +0100711 dev_dbg(intelhaddata->dev, "%s:buf[%d-%d] addr=%#x and size=%d\n",
712 __func__, start, end,
713 intelhaddata->buf_info[start].buf_addr,
714 intelhaddata->buf_info[start].buf_size);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530715 intelhaddata->valid_buf_cnt = num_periods;
716 return 0;
717}
718
Takashi Iwai372d8552017-01-31 13:57:58 +0100719static int snd_intelhad_read_len(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530720{
721 int i, retval = 0;
722 u32 len[4];
723
724 for (i = 0; i < 4 ; i++) {
Takashi Iwai79dda752017-01-30 17:23:39 +0100725 had_read_register(intelhaddata,
726 AUD_BUF_A_LENGTH + (i * HAD_REG_WIDTH),
727 &len[i]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530728 if (!len[i])
729 retval++;
730 }
731 if (retval != 1) {
732 for (i = 0; i < 4 ; i++)
Takashi Iwaic75b0472017-01-31 15:49:15 +0100733 dev_dbg(intelhaddata->dev, "buf[%d] size=%d\n",
734 i, len[i]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530735 }
736
737 return retval;
738}
739
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600740static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
741{
742 u32 maud_val;
743
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100744 /* Select maud according to DP 1.2 spec */
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600745 if (link_rate == DP_2_7_GHZ) {
746 switch (aud_samp_freq) {
747 case AUD_SAMPLE_RATE_32:
748 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
749 break;
750
751 case AUD_SAMPLE_RATE_44_1:
752 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
753 break;
754
755 case AUD_SAMPLE_RATE_48:
756 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
757 break;
758
759 case AUD_SAMPLE_RATE_88_2:
760 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
761 break;
762
763 case AUD_SAMPLE_RATE_96:
764 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
765 break;
766
767 case AUD_SAMPLE_RATE_176_4:
768 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
769 break;
770
771 case HAD_MAX_RATE:
772 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
773 break;
774
775 default:
776 maud_val = -EINVAL;
777 break;
778 }
779 } else if (link_rate == DP_1_62_GHZ) {
780 switch (aud_samp_freq) {
781 case AUD_SAMPLE_RATE_32:
782 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
783 break;
784
785 case AUD_SAMPLE_RATE_44_1:
786 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
787 break;
788
789 case AUD_SAMPLE_RATE_48:
790 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
791 break;
792
793 case AUD_SAMPLE_RATE_88_2:
794 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
795 break;
796
797 case AUD_SAMPLE_RATE_96:
798 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
799 break;
800
801 case AUD_SAMPLE_RATE_176_4:
802 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
803 break;
804
805 case HAD_MAX_RATE:
806 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
807 break;
808
809 default:
810 maud_val = -EINVAL;
811 break;
812 }
813 } else
814 maud_val = -EINVAL;
815
816 return maud_val;
817}
818
Takashi Iwai76296ef2017-01-30 16:09:11 +0100819/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100820 * Program HDMI audio CTS value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530821 *
822 * @aud_samp_freq: sampling frequency of audio data
823 * @tmds: sampling frequency of the display data
824 * @n_param: N value, depends on aud_samp_freq
825 * @intelhaddata:substream private data
826 *
827 * Program CTS register based on the audio and display sampling frequency
828 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100829static void snd_intelhad_prog_cts(u32 aud_samp_freq, u32 tmds,
830 u32 link_rate, u32 n_param,
831 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530832{
833 u32 cts_val;
834 u64 dividend, divisor;
835
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600836 if (intelhaddata->dp_output) {
837 /* Substitute cts_val with Maud according to DP 1.2 spec*/
838 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
839 } else {
840 /* Calculate CTS according to HDMI 1.3a spec*/
841 dividend = (u64)tmds * n_param*1000;
842 divisor = 128 * aud_samp_freq;
843 cts_val = div64_u64(dividend, divisor);
844 }
Takashi Iwaic75b0472017-01-31 15:49:15 +0100845 dev_dbg(intelhaddata->dev, "TMDS value=%d, N value=%d, CTS Value=%d\n",
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600846 tmds, n_param, cts_val);
Takashi Iwai79dda752017-01-30 17:23:39 +0100847 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530848}
849
850static int had_calculate_n_value(u32 aud_samp_freq)
851{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100852 int n_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530853
854 /* Select N according to HDMI 1.3a spec*/
855 switch (aud_samp_freq) {
856 case AUD_SAMPLE_RATE_32:
857 n_val = 4096;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100858 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530859
860 case AUD_SAMPLE_RATE_44_1:
861 n_val = 6272;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100862 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530863
864 case AUD_SAMPLE_RATE_48:
865 n_val = 6144;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100866 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530867
868 case AUD_SAMPLE_RATE_88_2:
869 n_val = 12544;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100870 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530871
872 case AUD_SAMPLE_RATE_96:
873 n_val = 12288;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100874 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530875
876 case AUD_SAMPLE_RATE_176_4:
877 n_val = 25088;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100878 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530879
880 case HAD_MAX_RATE:
881 n_val = 24576;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100882 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530883
884 default:
885 n_val = -EINVAL;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100886 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530887 }
888 return n_val;
889}
890
Takashi Iwai76296ef2017-01-30 16:09:11 +0100891/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100892 * Program HDMI audio N value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530893 *
894 * @aud_samp_freq: sampling frequency of audio data
895 * @n_param: N value, depends on aud_samp_freq
896 * @intelhaddata:substream private data
897 *
898 * This function is called in the prepare callback.
899 * It programs based on the audio and display sampling frequency
900 */
Takashi Iwai76296ef2017-01-30 16:09:11 +0100901static int snd_intelhad_prog_n(u32 aud_samp_freq, u32 *n_param,
902 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530903{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100904 int n_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530905
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600906 if (intelhaddata->dp_output) {
907 /*
908 * According to DP specs, Maud and Naud values hold
909 * a relationship, which is stated as:
910 * Maud/Naud = 512 * fs / f_LS_Clk
911 * where, fs is the sampling frequency of the audio stream
912 * and Naud is 32768 for Async clock.
913 */
914
915 n_val = DP_NAUD_VAL;
916 } else
917 n_val = had_calculate_n_value(aud_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530918
919 if (n_val < 0)
920 return n_val;
921
Takashi Iwai79dda752017-01-30 17:23:39 +0100922 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530923 *n_param = n_val;
924 return 0;
925}
926
Takashi Iwai03c34372017-02-02 16:19:03 +0100927#define MAX_CNT 0xFF
928
Takashi Iwai372d8552017-01-31 13:57:58 +0100929static void snd_intelhad_handle_underrun(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530930{
Takashi Iwai79f439e2017-01-31 16:46:44 +0100931 u32 hdmi_status = 0, i = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530932
933 /* Handle Underrun interrupt within Audio Unit */
Takashi Iwai79dda752017-01-30 17:23:39 +0100934 had_write_register(intelhaddata, AUD_CONFIG, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530935 /* Reset buffer pointers */
Takashi Iwai4151ee82017-01-31 18:14:15 +0100936 had_write_register(intelhaddata, AUD_HDMI_STATUS, 1);
937 had_write_register(intelhaddata, AUD_HDMI_STATUS, 0);
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100938 /*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530939 * The interrupt status 'sticky' bits might not be cleared by
940 * setting '1' to that bit once...
941 */
942 do { /* clear bit30, 31 AUD_HDMI_STATUS */
Takashi Iwai4151ee82017-01-31 18:14:15 +0100943 had_read_register(intelhaddata, AUD_HDMI_STATUS,
Takashi Iwai79dda752017-01-30 17:23:39 +0100944 &hdmi_status);
Takashi Iwaic75b0472017-01-31 15:49:15 +0100945 dev_dbg(intelhaddata->dev, "HDMI status =0x%x\n", hdmi_status);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530946 if (hdmi_status & AUD_CONFIG_MASK_UNDERRUN) {
947 i++;
Takashi Iwai79dda752017-01-30 17:23:39 +0100948 had_write_register(intelhaddata,
Takashi Iwai4151ee82017-01-31 18:14:15 +0100949 AUD_HDMI_STATUS, hdmi_status);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530950 } else
951 break;
952 } while (i < MAX_CNT);
953 if (i >= MAX_CNT)
Takashi Iwaic75b0472017-01-31 15:49:15 +0100954 dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n");
Jerome Anand5dab11d2017-01-25 04:27:52 +0530955}
956
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100957/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100958 * ALSA PCM open callback
Jerome Anand5dab11d2017-01-25 04:27:52 +0530959 */
960static int snd_intelhad_open(struct snd_pcm_substream *substream)
961{
962 struct snd_intelhad *intelhaddata;
963 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530964 int retval;
965
Jerome Anand5dab11d2017-01-25 04:27:52 +0530966 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530967 runtime = substream->runtime;
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +0100968 intelhaddata->underrun_count = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530969
Takashi Iwai182cdf22017-02-02 14:43:39 +0100970 pm_runtime_get_sync(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530971
Takashi Iwai79f439e2017-01-31 16:46:44 +0100972 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +0100973 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
974 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530975 retval = -ENODEV;
Takashi Iwaifa5dfe62017-02-01 22:03:26 +0100976 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530977 }
978
979 /* set the runtime hw parameter with local snd_pcm_hardware struct */
980 runtime->hw = snd_intel_hadstream;
981
Jerome Anand5dab11d2017-01-25 04:27:52 +0530982 retval = snd_pcm_hw_constraint_integer(runtime,
983 SNDRV_PCM_HW_PARAM_PERIODS);
984 if (retval < 0)
Takashi Iwaifa5dfe62017-02-01 22:03:26 +0100985 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530986
987 /* Make sure, that the period size is always aligned
988 * 64byte boundary
989 */
990 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
991 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
992 if (retval < 0) {
Takashi Iwaic75b0472017-01-31 15:49:15 +0100993 dev_dbg(intelhaddata->dev, "%s:step_size=64 failed,err=%d\n",
994 __func__, retval);
Takashi Iwaifa5dfe62017-02-01 22:03:26 +0100995 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530996 }
997
Takashi Iwai313d9f22017-02-02 13:00:12 +0100998 spin_lock_irq(&intelhaddata->had_spinlock);
999 intelhaddata->stream_info.substream = substream;
1000 intelhaddata->stream_info.substream_refcount++;
1001 spin_unlock_irq(&intelhaddata->had_spinlock);
1002
Jerome Anand5dab11d2017-01-25 04:27:52 +05301003 return retval;
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001004 error:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301005 pm_runtime_put(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301006 return retval;
1007}
1008
Takashi Iwaidf76df12017-01-31 16:04:10 +01001009/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001010 * ALSA PCM close callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301011 */
1012static int snd_intelhad_close(struct snd_pcm_substream *substream)
1013{
1014 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301015
Jerome Anand5dab11d2017-01-25 04:27:52 +05301016 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301017
1018 intelhaddata->stream_info.buffer_rendered = 0;
Takashi Iwai313d9f22017-02-02 13:00:12 +01001019 spin_lock_irq(&intelhaddata->had_spinlock);
1020 intelhaddata->stream_info.substream = NULL;
1021 intelhaddata->stream_info.substream_refcount--;
1022 while (intelhaddata->stream_info.substream_refcount > 0) {
1023 spin_unlock_irq(&intelhaddata->had_spinlock);
1024 cpu_relax();
1025 spin_lock_irq(&intelhaddata->had_spinlock);
1026 }
1027 spin_unlock_irq(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301028
1029 /* Check if following drv_status modification is required - VA */
1030 if (intelhaddata->drv_status != HAD_DRV_DISCONNECTED) {
1031 intelhaddata->drv_status = HAD_DRV_CONNECTED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001032 dev_dbg(intelhaddata->dev,
1033 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301034 __func__, __LINE__);
1035 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301036 pm_runtime_put(intelhaddata->dev);
1037 return 0;
1038}
1039
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001040/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001041 * ALSA PCM hw_params callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301042 */
1043static int snd_intelhad_hw_params(struct snd_pcm_substream *substream,
1044 struct snd_pcm_hw_params *hw_params)
1045{
Takashi Iwaic75b0472017-01-31 15:49:15 +01001046 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301047 unsigned long addr;
1048 int pages, buf_size, retval;
1049
Jerome Anand5dab11d2017-01-25 04:27:52 +05301050 if (!hw_params)
1051 return -EINVAL;
1052
Takashi Iwaic75b0472017-01-31 15:49:15 +01001053 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301054 buf_size = params_buffer_bytes(hw_params);
1055 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1056 if (retval < 0)
1057 return retval;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001058 dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
1059 __func__, buf_size);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301060 /* mark the pages as uncached region */
1061 addr = (unsigned long) substream->runtime->dma_area;
1062 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1063 retval = set_memory_uc(addr, pages);
1064 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001065 dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
1066 retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301067 return retval;
1068 }
1069 memset(substream->runtime->dma_area, 0, buf_size);
1070
1071 return retval;
1072}
1073
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001074/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001075 * ALSA PCM hw_free callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301076 */
1077static int snd_intelhad_hw_free(struct snd_pcm_substream *substream)
1078{
1079 unsigned long addr;
1080 u32 pages;
1081
Jerome Anand5dab11d2017-01-25 04:27:52 +05301082 /* mark back the pages as cached/writeback region before the free */
1083 if (substream->runtime->dma_area != NULL) {
1084 addr = (unsigned long) substream->runtime->dma_area;
1085 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1086 PAGE_SIZE;
1087 set_memory_wb(addr, pages);
1088 return snd_pcm_lib_free_pages(substream);
1089 }
1090 return 0;
1091}
1092
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001093/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001094 * ALSA PCM trigger callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301095 */
1096static int snd_intelhad_pcm_trigger(struct snd_pcm_substream *substream,
1097 int cmd)
1098{
Takashi Iwaida864802017-01-31 13:52:22 +01001099 int retval = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301100 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301101
Jerome Anand5dab11d2017-01-25 04:27:52 +05301102 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301103
1104 switch (cmd) {
1105 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001106 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1107 case SNDRV_PCM_TRIGGER_RESUME:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301108 /* Disable local INTRs till register prgmng is done */
Takashi Iwai79f439e2017-01-31 16:46:44 +01001109 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001110 dev_dbg(intelhaddata->dev,
1111 "_START: HDMI cable plugged-out\n");
Jerome Anand5dab11d2017-01-25 04:27:52 +05301112 retval = -ENODEV;
1113 break;
1114 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301115
Takashi Iwaif69bd102017-02-02 14:57:22 +01001116 intelhaddata->stream_info.running = true;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301117
1118 /* Enable Audio */
Takashi Iwaida864802017-01-31 13:52:22 +01001119 snd_intelhad_enable_audio_int(intelhaddata, true);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001120 snd_intelhad_enable_audio(substream, intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301121 break;
1122
1123 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001124 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1125 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwaibcce7752017-02-01 17:18:20 +01001126 spin_lock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301127 intelhaddata->curr_buf = 0;
1128
Takashi Iwaic75b0472017-01-31 15:49:15 +01001129 /* Stop reporting BUFFER_DONE/UNDERRUN to above layers */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301130
Takashi Iwaif69bd102017-02-02 14:57:22 +01001131 intelhaddata->stream_info.running = false;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001132 spin_unlock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301133 /* Disable Audio */
Takashi Iwaida864802017-01-31 13:52:22 +01001134 snd_intelhad_enable_audio_int(intelhaddata, false);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001135 snd_intelhad_enable_audio(substream, intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301136 /* Reset buffer pointers */
Takashi Iwai79dda752017-01-30 17:23:39 +01001137 snd_intelhad_reset_audio(intelhaddata, 1);
1138 snd_intelhad_reset_audio(intelhaddata, 0);
Takashi Iwaida864802017-01-31 13:52:22 +01001139 snd_intelhad_enable_audio_int(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301140 break;
1141
1142 default:
1143 retval = -EINVAL;
1144 }
1145 return retval;
1146}
1147
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001148/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001149 * ALSA PCM prepare callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301150 */
1151static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream)
1152{
1153 int retval;
1154 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001155 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301156 struct snd_intelhad *intelhaddata;
1157 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301158
Jerome Anand5dab11d2017-01-25 04:27:52 +05301159 intelhaddata = snd_pcm_substream_chip(substream);
1160 runtime = substream->runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301161
Takashi Iwai79f439e2017-01-31 16:46:44 +01001162 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001163 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
1164 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301165 retval = -ENODEV;
1166 goto prep_end;
1167 }
1168
Takashi Iwaic75b0472017-01-31 15:49:15 +01001169 dev_dbg(intelhaddata->dev, "period_size=%d\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301170 (int)frames_to_bytes(runtime, runtime->period_size));
Takashi Iwaic75b0472017-01-31 15:49:15 +01001171 dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods);
1172 dev_dbg(intelhaddata->dev, "buffer_size=%d\n",
1173 (int)snd_pcm_lib_buffer_bytes(substream));
1174 dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
1175 dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301176
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001177 intelhaddata->stream_info.buffer_rendered = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301178
1179 /* Get N value in KHz */
Takashi Iwaida864802017-01-31 13:52:22 +01001180 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301181
Takashi Iwai76296ef2017-01-30 16:09:11 +01001182 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1183 intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301184 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001185 dev_err(intelhaddata->dev,
1186 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301187 goto prep_end;
1188 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001189
1190 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001191 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001192
Takashi Iwai76296ef2017-01-30 16:09:11 +01001193 snd_intelhad_prog_cts(substream->runtime->rate,
1194 disp_samp_freq, link_rate,
1195 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301196
Takashi Iwai76296ef2017-01-30 16:09:11 +01001197 snd_intelhad_prog_dip(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301198
Takashi Iwai76296ef2017-01-30 16:09:11 +01001199 retval = snd_intelhad_audio_ctrl(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301200
1201 /* Prog buffer address */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001202 retval = snd_intelhad_prog_buffer(substream, intelhaddata,
Jerome Anand5dab11d2017-01-25 04:27:52 +05301203 HAD_BUF_TYPE_A, HAD_BUF_TYPE_D);
1204
1205 /*
1206 * Program channel mapping in following order:
1207 * FL, FR, C, LFE, RL, RR
1208 */
1209
Takashi Iwai79dda752017-01-30 17:23:39 +01001210 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301211
1212prep_end:
1213 return retval;
1214}
1215
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001216/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001217 * ALSA PCM pointer callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301218 */
Takashi Iwai44684f62017-02-02 17:27:40 +01001219static snd_pcm_uframes_t
1220snd_intelhad_pcm_pointer(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301221{
1222 struct snd_intelhad *intelhaddata;
1223 u32 bytes_rendered = 0;
1224 u32 t;
1225 int buf_id;
1226
Jerome Anand5dab11d2017-01-25 04:27:52 +05301227 intelhaddata = snd_pcm_substream_chip(substream);
1228
Takashi Iwai79f439e2017-01-31 16:46:44 +01001229 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
1230 return SNDRV_PCM_POS_XRUN;
1231
Jerome Anand5dab11d2017-01-25 04:27:52 +05301232 /* Use a hw register to calculate sub-period position reports.
1233 * This makes PulseAudio happier.
1234 */
1235
1236 buf_id = intelhaddata->curr_buf % 4;
Takashi Iwai79dda752017-01-30 17:23:39 +01001237 had_read_register(intelhaddata,
1238 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH), &t);
Jerome Anand232892f2017-01-25 04:27:53 +05301239
1240 if ((t == 0) || (t == ((u32)-1L))) {
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001241 intelhaddata->underrun_count++;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001242 dev_dbg(intelhaddata->dev,
1243 "discovered buffer done for buf %d, count = %d\n",
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001244 buf_id, intelhaddata->underrun_count);
Jerome Anand232892f2017-01-25 04:27:53 +05301245
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001246 if (intelhaddata->underrun_count > (HAD_MIN_PERIODS/2)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001247 dev_dbg(intelhaddata->dev,
1248 "assume audio_codec_reset, underrun = %d - do xrun\n",
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001249 intelhaddata->underrun_count);
1250 intelhaddata->underrun_count = 0;
Jerome Anand232892f2017-01-25 04:27:53 +05301251 return SNDRV_PCM_POS_XRUN;
1252 }
1253 } else {
1254 /* Reset Counter */
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001255 intelhaddata->underrun_count = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301256 }
Jerome Anand232892f2017-01-25 04:27:53 +05301257
Jerome Anand5dab11d2017-01-25 04:27:52 +05301258 t = intelhaddata->buf_info[buf_id].buf_size - t;
1259
1260 if (intelhaddata->stream_info.buffer_rendered)
1261 div_u64_rem(intelhaddata->stream_info.buffer_rendered,
1262 intelhaddata->stream_info.ring_buf_size,
1263 &(bytes_rendered));
1264
Takashi Iwai7d9e7982017-02-01 22:25:58 +01001265 return bytes_to_frames(substream->runtime, bytes_rendered + t);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301266}
1267
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001268/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001269 * ALSA PCM mmap callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301270 */
1271static int snd_intelhad_pcm_mmap(struct snd_pcm_substream *substream,
1272 struct vm_area_struct *vma)
1273{
Jerome Anand5dab11d2017-01-25 04:27:52 +05301274 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1275 return remap_pfn_range(vma, vma->vm_start,
1276 substream->dma_buffer.addr >> PAGE_SHIFT,
1277 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1278}
1279
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001280/* process mode change of the running stream; called in mutex */
Takashi Iwaida864802017-01-31 13:52:22 +01001281static int hdmi_audio_mode_change(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301282{
Takashi Iwaida864802017-01-31 13:52:22 +01001283 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301284 int retval = 0;
1285 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001286 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301287
Takashi Iwai313d9f22017-02-02 13:00:12 +01001288 substream = had_substream_get(intelhaddata);
1289 if (!substream)
Takashi Iwaida864802017-01-31 13:52:22 +01001290 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301291
1292 /* Disable Audio */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001293 snd_intelhad_enable_audio(substream, intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301294
1295 /* Update CTS value */
Takashi Iwaida864802017-01-31 13:52:22 +01001296 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301297
Takashi Iwai76296ef2017-01-30 16:09:11 +01001298 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1299 intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301300 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001301 dev_err(intelhaddata->dev,
1302 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301303 goto out;
1304 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001305
1306 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001307 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001308
Takashi Iwai76296ef2017-01-30 16:09:11 +01001309 snd_intelhad_prog_cts(substream->runtime->rate,
1310 disp_samp_freq, link_rate,
1311 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301312
1313 /* Enable Audio */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001314 snd_intelhad_enable_audio(substream, intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301315
1316out:
Takashi Iwai313d9f22017-02-02 13:00:12 +01001317 had_substream_put(intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301318 return retval;
1319}
1320
Takashi Iwai372d8552017-01-31 13:57:58 +01001321static inline int had_chk_intrmiss(struct snd_intelhad *intelhaddata,
1322 enum intel_had_aud_buf_type buf_id)
1323{
1324 int i, intr_count = 0;
1325 enum intel_had_aud_buf_type buff_done;
1326 u32 buf_size, buf_addr;
Takashi Iwai372d8552017-01-31 13:57:58 +01001327
1328 buff_done = buf_id;
1329
1330 intr_count = snd_intelhad_read_len(intelhaddata);
1331 if (intr_count > 1) {
1332 /* In case of active playback */
Takashi Iwaic75b0472017-01-31 15:49:15 +01001333 dev_err(intelhaddata->dev,
1334 "Driver detected %d missed buffer done interrupt(s)\n",
1335 (intr_count - 1));
Takashi Iwai372d8552017-01-31 13:57:58 +01001336 if (intr_count > 3)
1337 return intr_count;
1338
1339 buf_id += (intr_count - 1);
1340 /* Reprogram registers*/
1341 for (i = buff_done; i < buf_id; i++) {
1342 int j = i % 4;
1343
1344 buf_size = intelhaddata->buf_info[j].buf_size;
1345 buf_addr = intelhaddata->buf_info[j].buf_addr;
1346 had_write_register(intelhaddata,
1347 AUD_BUF_A_LENGTH +
1348 (j * HAD_REG_WIDTH), buf_size);
1349 had_write_register(intelhaddata,
1350 AUD_BUF_A_ADDR+(j * HAD_REG_WIDTH),
1351 (buf_addr | BIT(0) | BIT(1)));
1352 }
1353 buf_id = buf_id % 4;
Takashi Iwai372d8552017-01-31 13:57:58 +01001354 intelhaddata->buff_done = buf_id;
Takashi Iwai372d8552017-01-31 13:57:58 +01001355 }
1356
1357 return intr_count;
1358}
1359
Takashi Iwaibcce7752017-02-01 17:18:20 +01001360/* called from irq handler */
Takashi Iwai372d8552017-01-31 13:57:58 +01001361static int had_process_buffer_done(struct snd_intelhad *intelhaddata)
1362{
1363 u32 len = 1;
1364 enum intel_had_aud_buf_type buf_id;
1365 enum intel_had_aud_buf_type buff_done;
1366 struct pcm_stream_info *stream;
Takashi Iwai313d9f22017-02-02 13:00:12 +01001367 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001368 u32 buf_size;
Takashi Iwai372d8552017-01-31 13:57:58 +01001369 int intr_count;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001370 unsigned long flags;
Takashi Iwai372d8552017-01-31 13:57:58 +01001371
Takashi Iwai372d8552017-01-31 13:57:58 +01001372 stream = &intelhaddata->stream_info;
1373 intr_count = 1;
1374
Takashi Iwaibcce7752017-02-01 17:18:20 +01001375 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
Takashi Iwai372d8552017-01-31 13:57:58 +01001376 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaibcce7752017-02-01 17:18:20 +01001377 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001378 dev_dbg(intelhaddata->dev,
1379 "%s:Device already disconnected\n", __func__);
Takashi Iwai372d8552017-01-31 13:57:58 +01001380 return 0;
1381 }
1382 buf_id = intelhaddata->curr_buf;
1383 intelhaddata->buff_done = buf_id;
1384 buff_done = intelhaddata->buff_done;
1385 buf_size = intelhaddata->buf_info[buf_id].buf_size;
Takashi Iwai372d8552017-01-31 13:57:58 +01001386
Takashi Iwai372d8552017-01-31 13:57:58 +01001387 /* Every debug statement has an implication
1388 * of ~5msec. Thus, avoid having >3 debug statements
1389 * for each buffer_done handling.
1390 */
1391
1392 /* Check for any intr_miss in case of active playback */
Takashi Iwaif69bd102017-02-02 14:57:22 +01001393 if (stream->running) {
Takashi Iwai372d8552017-01-31 13:57:58 +01001394 intr_count = had_chk_intrmiss(intelhaddata, buf_id);
1395 if (!intr_count || (intr_count > 3)) {
Takashi Iwaibcce7752017-02-01 17:18:20 +01001396 spin_unlock_irqrestore(&intelhaddata->had_spinlock,
1397 flags);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001398 dev_err(intelhaddata->dev,
1399 "HAD SW state in non-recoverable mode\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001400 return 0;
1401 }
1402 buf_id += (intr_count - 1);
1403 buf_id = buf_id % 4;
Takashi Iwai372d8552017-01-31 13:57:58 +01001404 }
1405
1406 intelhaddata->buf_info[buf_id].is_valid = true;
1407 if (intelhaddata->valid_buf_cnt-1 == buf_id) {
Takashi Iwaif69bd102017-02-02 14:57:22 +01001408 if (stream->running)
Takashi Iwai372d8552017-01-31 13:57:58 +01001409 intelhaddata->curr_buf = HAD_BUF_TYPE_A;
1410 } else
1411 intelhaddata->curr_buf = buf_id + 1;
1412
Takashi Iwaibcce7752017-02-01 17:18:20 +01001413 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
Takashi Iwai372d8552017-01-31 13:57:58 +01001414
Takashi Iwai79f439e2017-01-31 16:46:44 +01001415 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001416 dev_dbg(intelhaddata->dev, "HDMI cable plugged-out\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001417 return 0;
1418 }
1419
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001420 /* Reprogram the registers with addr and length */
Takashi Iwai372d8552017-01-31 13:57:58 +01001421 had_write_register(intelhaddata,
1422 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH),
1423 buf_size);
1424 had_write_register(intelhaddata,
1425 AUD_BUF_A_ADDR + (buf_id * HAD_REG_WIDTH),
1426 intelhaddata->buf_info[buf_id].buf_addr |
1427 BIT(0) | BIT(1));
1428
1429 had_read_register(intelhaddata,
1430 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH),
1431 &len);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001432 dev_dbg(intelhaddata->dev, "%s:Enabled buf[%d]\n", __func__, buf_id);
Takashi Iwai372d8552017-01-31 13:57:58 +01001433
1434 /* In case of actual data,
1435 * report buffer_done to above ALSA layer
1436 */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001437 substream = had_substream_get(intelhaddata);
1438 if (substream) {
1439 buf_size = intelhaddata->buf_info[buf_id].buf_size;
Takashi Iwai372d8552017-01-31 13:57:58 +01001440 intelhaddata->stream_info.buffer_rendered +=
1441 (intr_count * buf_size);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001442 snd_pcm_period_elapsed(substream);
1443 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001444 }
1445
1446 return 0;
1447}
1448
Takashi Iwaibcce7752017-02-01 17:18:20 +01001449/* called from irq handler */
Takashi Iwai372d8552017-01-31 13:57:58 +01001450static int had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
1451{
1452 enum intel_had_aud_buf_type buf_id;
1453 struct pcm_stream_info *stream;
Takashi Iwai313d9f22017-02-02 13:00:12 +01001454 struct snd_pcm_substream *substream;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001455 unsigned long flags;
Takashi Iwai372d8552017-01-31 13:57:58 +01001456 int drv_status;
1457
Takashi Iwai372d8552017-01-31 13:57:58 +01001458 stream = &intelhaddata->stream_info;
1459
Takashi Iwaibcce7752017-02-01 17:18:20 +01001460 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
Takashi Iwai372d8552017-01-31 13:57:58 +01001461 buf_id = intelhaddata->curr_buf;
Takashi Iwai372d8552017-01-31 13:57:58 +01001462 intelhaddata->buff_done = buf_id;
1463 drv_status = intelhaddata->drv_status;
Takashi Iwaif69bd102017-02-02 14:57:22 +01001464 if (stream->running)
Takashi Iwai372d8552017-01-31 13:57:58 +01001465 intelhaddata->curr_buf = HAD_BUF_TYPE_A;
1466
Takashi Iwaibcce7752017-02-01 17:18:20 +01001467 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
Takashi Iwai372d8552017-01-31 13:57:58 +01001468
Takashi Iwaif69bd102017-02-02 14:57:22 +01001469 dev_dbg(intelhaddata->dev, "Enter:%s buf_id=%d, stream_running=%d\n",
1470 __func__, buf_id, stream->running);
Takashi Iwai372d8552017-01-31 13:57:58 +01001471
1472 snd_intelhad_handle_underrun(intelhaddata);
1473
1474 if (drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001475 dev_dbg(intelhaddata->dev,
1476 "%s:Device already disconnected\n", __func__);
Takashi Iwai372d8552017-01-31 13:57:58 +01001477 return 0;
1478 }
1479
Takashi Iwaif69bd102017-02-02 14:57:22 +01001480 /* Report UNDERRUN error to above layers */
1481 substream = had_substream_get(intelhaddata);
1482 if (substream) {
1483 snd_pcm_stop_xrun(substream);
1484 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001485 }
1486
1487 return 0;
1488}
1489
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001490/* process hot plug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001491static void had_process_hot_plug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001492{
1493 enum intel_had_aud_buf_type buf_id;
1494 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001495
Takashi Iwaibcce7752017-02-01 17:18:20 +01001496 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001497 if (intelhaddata->drv_status == HAD_DRV_CONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001498 dev_dbg(intelhaddata->dev, "Device already connected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001499 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001500 return;
Takashi Iwai372d8552017-01-31 13:57:58 +01001501 }
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001502
Takashi Iwai372d8552017-01-31 13:57:58 +01001503 buf_id = intelhaddata->curr_buf;
1504 intelhaddata->buff_done = buf_id;
1505 intelhaddata->drv_status = HAD_DRV_CONNECTED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001506 dev_dbg(intelhaddata->dev,
1507 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001508 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001509 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001510
Takashi Iwaic75b0472017-01-31 15:49:15 +01001511 dev_dbg(intelhaddata->dev, "Processing HOT_PLUG, buf_id = %d\n",
1512 buf_id);
Takashi Iwai372d8552017-01-31 13:57:58 +01001513
1514 /* Safety check */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001515 substream = had_substream_get(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001516 if (substream) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001517 dev_dbg(intelhaddata->dev,
1518 "Force to stop the active stream by disconnection\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001519 /* Set runtime->state to hw_params done */
1520 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001521 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001522 }
1523
1524 had_build_channel_allocation_map(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001525}
1526
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001527/* process hot unplug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001528static void had_process_hot_unplug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001529{
1530 enum intel_had_aud_buf_type buf_id;
Takashi Iwai313d9f22017-02-02 13:00:12 +01001531 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001532
Takashi Iwai372d8552017-01-31 13:57:58 +01001533 buf_id = intelhaddata->curr_buf;
1534
Takashi Iwai313d9f22017-02-02 13:00:12 +01001535 substream = had_substream_get(intelhaddata);
1536
Takashi Iwaibcce7752017-02-01 17:18:20 +01001537 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001538
1539 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001540 dev_dbg(intelhaddata->dev, "Device already disconnected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001541 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001542 goto out;
Takashi Iwai372d8552017-01-31 13:57:58 +01001543
Takashi Iwai372d8552017-01-31 13:57:58 +01001544 }
1545
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001546 /* Disable Audio */
1547 snd_intelhad_enable_audio_int(intelhaddata, false);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001548 snd_intelhad_enable_audio(substream, intelhaddata, false);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001549
Takashi Iwai372d8552017-01-31 13:57:58 +01001550 intelhaddata->drv_status = HAD_DRV_DISCONNECTED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001551 dev_dbg(intelhaddata->dev,
1552 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001553 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001554 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001555
1556 /* Report to above ALSA layer */
1557 if (substream)
1558 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1559
1560 out:
1561 if (substream)
1562 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001563 kfree(intelhaddata->chmap->chmap);
1564 intelhaddata->chmap->chmap = NULL;
Takashi Iwai372d8552017-01-31 13:57:58 +01001565}
1566
1567/* PCM operations structure and the calls back for the same */
1568static struct snd_pcm_ops snd_intelhad_playback_ops = {
Jerome Anand5dab11d2017-01-25 04:27:52 +05301569 .open = snd_intelhad_open,
1570 .close = snd_intelhad_close,
1571 .ioctl = snd_pcm_lib_ioctl,
1572 .hw_params = snd_intelhad_hw_params,
1573 .hw_free = snd_intelhad_hw_free,
1574 .prepare = snd_intelhad_pcm_prepare,
1575 .trigger = snd_intelhad_pcm_trigger,
1576 .pointer = snd_intelhad_pcm_pointer,
1577 .mmap = snd_intelhad_pcm_mmap,
1578};
1579
Jerome Anand5dab11d2017-01-25 04:27:52 +05301580static int had_iec958_info(struct snd_kcontrol *kcontrol,
1581 struct snd_ctl_elem_info *uinfo)
1582{
1583 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1584 uinfo->count = 1;
1585 return 0;
1586}
1587
1588static int had_iec958_get(struct snd_kcontrol *kcontrol,
1589 struct snd_ctl_elem_value *ucontrol)
1590{
1591 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1592
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001593 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301594 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1595 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1596 ucontrol->value.iec958.status[2] =
1597 (intelhaddata->aes_bits >> 16) & 0xff;
1598 ucontrol->value.iec958.status[3] =
1599 (intelhaddata->aes_bits >> 24) & 0xff;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001600 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301601 return 0;
1602}
Takashi Iwai372d8552017-01-31 13:57:58 +01001603
Jerome Anand5dab11d2017-01-25 04:27:52 +05301604static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1605 struct snd_ctl_elem_value *ucontrol)
1606{
1607 ucontrol->value.iec958.status[0] = 0xff;
1608 ucontrol->value.iec958.status[1] = 0xff;
1609 ucontrol->value.iec958.status[2] = 0xff;
1610 ucontrol->value.iec958.status[3] = 0xff;
1611 return 0;
1612}
Takashi Iwai372d8552017-01-31 13:57:58 +01001613
Jerome Anand5dab11d2017-01-25 04:27:52 +05301614static int had_iec958_put(struct snd_kcontrol *kcontrol,
1615 struct snd_ctl_elem_value *ucontrol)
1616{
1617 unsigned int val;
1618 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001619 int changed = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301620
Jerome Anand5dab11d2017-01-25 04:27:52 +05301621 val = (ucontrol->value.iec958.status[0] << 0) |
1622 (ucontrol->value.iec958.status[1] << 8) |
1623 (ucontrol->value.iec958.status[2] << 16) |
1624 (ucontrol->value.iec958.status[3] << 24);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001625 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301626 if (intelhaddata->aes_bits != val) {
1627 intelhaddata->aes_bits = val;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001628 changed = 1;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301629 }
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001630 mutex_unlock(&intelhaddata->mutex);
1631 return changed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301632}
1633
Takashi Iwai4aedb942017-02-02 16:38:39 +01001634static int had_ctl_eld_info(struct snd_kcontrol *kcontrol,
1635 struct snd_ctl_elem_info *uinfo)
1636{
1637 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1638 uinfo->count = HDMI_MAX_ELD_BYTES;
1639 return 0;
1640}
1641
1642static int had_ctl_eld_get(struct snd_kcontrol *kcontrol,
1643 struct snd_ctl_elem_value *ucontrol)
1644{
1645 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1646
1647 mutex_lock(&intelhaddata->mutex);
1648 memcpy(ucontrol->value.bytes.data, intelhaddata->eld,
1649 HDMI_MAX_ELD_BYTES);
1650 mutex_unlock(&intelhaddata->mutex);
1651 return 0;
1652}
1653
1654static struct snd_kcontrol_new had_controls[] = {
1655 {
1656 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1657 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1658 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1659 .info = had_iec958_info, /* shared */
1660 .get = had_iec958_mask_get,
1661 },
1662 {
1663 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1664 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1665 .info = had_iec958_info,
1666 .get = had_iec958_get,
1667 .put = had_iec958_put,
1668 },
1669 {
1670 .access = (SNDRV_CTL_ELEM_ACCESS_READ |
1671 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1672 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1673 .name = "ELD",
1674 .info = had_ctl_eld_info,
1675 .get = had_ctl_eld_get,
1676 },
Jerome Anand5dab11d2017-01-25 04:27:52 +05301677};
1678
Jerome Anand5dab11d2017-01-25 04:27:52 +05301679
Takashi Iwaida864802017-01-31 13:52:22 +01001680static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id)
1681{
1682 struct snd_intelhad *ctx = dev_id;
1683 u32 audio_stat, audio_reg;
1684
Takashi Iwai4151ee82017-01-31 18:14:15 +01001685 audio_reg = AUD_HDMI_STATUS;
Takashi Iwaida864802017-01-31 13:52:22 +01001686 mid_hdmi_audio_read(ctx, audio_reg, &audio_stat);
1687
1688 if (audio_stat & HDMI_AUDIO_UNDERRUN) {
1689 mid_hdmi_audio_write(ctx, audio_reg, HDMI_AUDIO_UNDERRUN);
1690 had_process_buffer_underrun(ctx);
1691 }
1692
1693 if (audio_stat & HDMI_AUDIO_BUFFER_DONE) {
1694 mid_hdmi_audio_write(ctx, audio_reg, HDMI_AUDIO_BUFFER_DONE);
1695 had_process_buffer_done(ctx);
1696 }
1697
1698 return IRQ_HANDLED;
1699}
1700
1701static void notify_audio_lpe(struct platform_device *pdev)
1702{
1703 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Takashi Iwaida864802017-01-31 13:52:22 +01001704
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001705 schedule_work(&ctx->hdmi_audio_wq);
1706}
Takashi Iwaida864802017-01-31 13:52:22 +01001707
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001708static void had_audio_wq(struct work_struct *work)
1709{
1710 struct snd_intelhad *ctx =
1711 container_of(work, struct snd_intelhad, hdmi_audio_wq);
1712 struct intel_hdmi_lpe_audio_pdata *pdata = ctx->dev->platform_data;
1713
Takashi Iwai182cdf22017-02-02 14:43:39 +01001714 pm_runtime_get_sync(ctx->dev);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001715 mutex_lock(&ctx->mutex);
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001716 if (!pdata->hdmi_connected) {
1717 dev_dbg(ctx->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001718 __func__);
Takashi Iwai4aedb942017-02-02 16:38:39 +01001719 memset(ctx->eld, 0, sizeof(ctx->eld)); /* clear the old ELD */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001720 had_process_hot_unplug(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001721 } else {
1722 struct intel_hdmi_lpe_audio_eld *eld = &pdata->eld;
1723
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001724 dev_dbg(ctx->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
1725 __func__, eld->port_id, pdata->tmds_clock_speed);
1726
Takashi Iwaida864802017-01-31 13:52:22 +01001727 switch (eld->pipe_id) {
1728 case 0:
1729 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1730 break;
1731 case 1:
1732 ctx->had_config_offset = AUDIO_HDMI_CONFIG_B;
1733 break;
1734 case 2:
1735 ctx->had_config_offset = AUDIO_HDMI_CONFIG_C;
1736 break;
1737 default:
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001738 dev_dbg(ctx->dev, "Invalid pipe %d\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001739 eld->pipe_id);
1740 break;
1741 }
1742
Takashi Iwaidf0435d2017-02-02 15:37:11 +01001743 memcpy(ctx->eld, eld->eld_data, sizeof(ctx->eld));
Takashi Iwaida864802017-01-31 13:52:22 +01001744
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001745 ctx->dp_output = pdata->dp_output;
1746 ctx->tmds_clock_speed = pdata->tmds_clock_speed;
1747 ctx->link_rate = pdata->link_rate;
1748
Takashi Iwaida864802017-01-31 13:52:22 +01001749 had_process_hot_plug(ctx);
1750
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001751 /* Process mode change if stream is active */
Takashi Iwaif69bd102017-02-02 14:57:22 +01001752 hdmi_audio_mode_change(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001753 }
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001754 mutex_unlock(&ctx->mutex);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001755 pm_runtime_put(ctx->dev);
1756}
1757
1758/*
1759 * PM callbacks
1760 */
1761
1762static int hdmi_lpe_audio_runtime_suspend(struct device *dev)
1763{
1764 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1765 struct snd_pcm_substream *substream;
1766
1767 substream = had_substream_get(ctx);
1768 if (substream) {
1769 snd_pcm_suspend(substream);
1770 had_substream_put(ctx);
1771 }
1772
1773 return 0;
1774}
1775
1776static int hdmi_lpe_audio_suspend(struct device *dev)
1777{
1778 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1779 int err;
1780
1781 err = hdmi_lpe_audio_runtime_suspend(dev);
1782 if (!err)
1783 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D3hot);
1784 return err;
1785}
1786
1787static int hdmi_lpe_audio_resume(struct device *dev)
1788{
1789 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1790
1791 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D0);
1792 return 0;
Takashi Iwaida864802017-01-31 13:52:22 +01001793}
1794
1795/* release resources */
1796static void hdmi_lpe_audio_free(struct snd_card *card)
1797{
1798 struct snd_intelhad *ctx = card->private_data;
1799
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001800 cancel_work_sync(&ctx->hdmi_audio_wq);
1801
Takashi Iwaida864802017-01-31 13:52:22 +01001802 if (ctx->mmio_start)
1803 iounmap(ctx->mmio_start);
1804 if (ctx->irq >= 0)
1805 free_irq(ctx->irq, ctx);
1806}
1807
1808/*
1809 * hdmi_lpe_audio_probe - start bridge with i915
1810 *
1811 * This function is called when the i915 driver creates the
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001812 * hdmi-lpe-audio platform device.
Takashi Iwaida864802017-01-31 13:52:22 +01001813 */
1814static int hdmi_lpe_audio_probe(struct platform_device *pdev)
1815{
1816 struct snd_card *card;
1817 struct snd_intelhad *ctx;
1818 struct snd_pcm *pcm;
1819 struct intel_hdmi_lpe_audio_pdata *pdata;
1820 int irq;
1821 struct resource *res_mmio;
Takashi Iwai4aedb942017-02-02 16:38:39 +01001822 int i, ret;
Takashi Iwaida864802017-01-31 13:52:22 +01001823
Takashi Iwaida864802017-01-31 13:52:22 +01001824 dev_dbg(&pdev->dev, "dma_mask: %p\n", pdev->dev.dma_mask);
1825
1826 pdata = pdev->dev.platform_data;
1827 if (!pdata) {
1828 dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
1829 return -EINVAL;
1830 }
1831
1832 /* get resources */
1833 irq = platform_get_irq(pdev, 0);
1834 if (irq < 0) {
1835 dev_err(&pdev->dev, "Could not get irq resource\n");
1836 return -ENODEV;
1837 }
1838
1839 res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1840 if (!res_mmio) {
1841 dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
1842 return -ENXIO;
1843 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301844
Takashi Iwai5647aec2017-01-31 08:14:34 +01001845 /* create a card instance with ALSA framework */
Takashi Iwaida864802017-01-31 13:52:22 +01001846 ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
1847 THIS_MODULE, sizeof(*ctx), &card);
1848 if (ret)
1849 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301850
Takashi Iwaida864802017-01-31 13:52:22 +01001851 ctx = card->private_data;
1852 spin_lock_init(&ctx->had_spinlock);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001853 mutex_init(&ctx->mutex);
Takashi Iwaida864802017-01-31 13:52:22 +01001854 ctx->drv_status = HAD_DRV_DISCONNECTED;
1855 ctx->dev = &pdev->dev;
1856 ctx->card = card;
Takashi Iwaida864802017-01-31 13:52:22 +01001857 ctx->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
1858 strcpy(card->driver, INTEL_HAD);
1859 strcpy(card->shortname, INTEL_HAD);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301860
Takashi Iwaida864802017-01-31 13:52:22 +01001861 ctx->irq = -1;
1862 ctx->tmds_clock_speed = DIS_SAMPLE_RATE_148_5;
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001863 INIT_WORK(&ctx->hdmi_audio_wq, had_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301864
Takashi Iwaida864802017-01-31 13:52:22 +01001865 card->private_free = hdmi_lpe_audio_free;
1866
1867 /* assume pipe A as default */
1868 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1869
1870 platform_set_drvdata(pdev, ctx);
1871
1872 dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
1873 __func__, (unsigned int)res_mmio->start,
1874 (unsigned int)res_mmio->end);
1875
1876 ctx->mmio_start = ioremap_nocache(res_mmio->start,
1877 (size_t)(resource_size(res_mmio)));
1878 if (!ctx->mmio_start) {
1879 dev_err(&pdev->dev, "Could not get ioremap\n");
1880 ret = -EACCES;
1881 goto err;
1882 }
1883
1884 /* setup interrupt handler */
1885 ret = request_irq(irq, display_pipe_interrupt_handler, 0,
1886 pdev->name, ctx);
1887 if (ret < 0) {
1888 dev_err(&pdev->dev, "request_irq failed\n");
1889 goto err;
1890 }
1891
1892 ctx->irq = irq;
1893
1894 ret = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
1895 MAX_CAP_STREAMS, &pcm);
1896 if (ret)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301897 goto err;
1898
1899 /* setup private data which can be retrieved when required */
Takashi Iwaida864802017-01-31 13:52:22 +01001900 pcm->private_data = ctx;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301901 pcm->info_flags = 0;
1902 strncpy(pcm->name, card->shortname, strlen(card->shortname));
Takashi Iwaida864802017-01-31 13:52:22 +01001903 /* setup the ops for playabck */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301904 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1905 &snd_intelhad_playback_ops);
1906 /* allocate dma pages for ALSA stream operations
1907 * memory allocated is based on size, not max value
1908 * thus using same argument for max & size
1909 */
Takashi Iwaida864802017-01-31 13:52:22 +01001910 snd_pcm_lib_preallocate_pages_for_all(pcm,
Jerome Anand5dab11d2017-01-25 04:27:52 +05301911 SNDRV_DMA_TYPE_DEV, NULL,
1912 HAD_MAX_BUFFER, HAD_MAX_BUFFER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301913
Takashi Iwai4aedb942017-02-02 16:38:39 +01001914 /* create controls */
1915 for (i = 0; i < ARRAY_SIZE(had_controls); i++) {
1916 ret = snd_ctl_add(card, snd_ctl_new1(&had_controls[i], ctx));
1917 if (ret < 0)
1918 goto err;
1919 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301920
1921 init_channel_allocations();
1922
1923 /* Register channel map controls */
Takashi Iwaida864802017-01-31 13:52:22 +01001924 ret = had_register_chmap_ctls(ctx, pcm);
1925 if (ret < 0)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301926 goto err;
1927
Takashi Iwaida864802017-01-31 13:52:22 +01001928 ret = snd_card_register(card);
1929 if (ret)
Takashi Iwai36ec0d92017-01-31 08:47:05 +01001930 goto err;
1931
Takashi Iwaibcce7752017-02-01 17:18:20 +01001932 spin_lock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001933 pdata->notify_audio_lpe = notify_audio_lpe;
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001934 pdata->notify_pending = false;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001935 spin_unlock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001936
1937 pm_runtime_set_active(&pdev->dev);
1938 pm_runtime_enable(&pdev->dev);
1939
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001940 dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
Takashi Iwaida864802017-01-31 13:52:22 +01001941 schedule_work(&ctx->hdmi_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301942
Takashi Iwai79dda752017-01-30 17:23:39 +01001943 return 0;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001944
Jerome Anand5dab11d2017-01-25 04:27:52 +05301945err:
1946 snd_card_free(card);
Takashi Iwaida864802017-01-31 13:52:22 +01001947 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301948}
1949
Takashi Iwai79dda752017-01-30 17:23:39 +01001950/*
Takashi Iwaida864802017-01-31 13:52:22 +01001951 * hdmi_lpe_audio_remove - stop bridge with i915
Jerome Anand5dab11d2017-01-25 04:27:52 +05301952 *
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001953 * This function is called when the platform device is destroyed.
Jerome Anand5dab11d2017-01-25 04:27:52 +05301954 */
Takashi Iwaida864802017-01-31 13:52:22 +01001955static int hdmi_lpe_audio_remove(struct platform_device *pdev)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301956{
Takashi Iwaida864802017-01-31 13:52:22 +01001957 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301958
Takashi Iwaida864802017-01-31 13:52:22 +01001959 if (ctx->drv_status != HAD_DRV_DISCONNECTED)
1960 snd_intelhad_enable_audio_int(ctx, false);
1961 snd_card_free(ctx->card);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301962 return 0;
1963}
1964
Takashi Iwai182cdf22017-02-02 14:43:39 +01001965static const struct dev_pm_ops hdmi_lpe_audio_pm = {
1966 SET_SYSTEM_SLEEP_PM_OPS(hdmi_lpe_audio_suspend, hdmi_lpe_audio_resume)
1967 SET_RUNTIME_PM_OPS(hdmi_lpe_audio_runtime_suspend, NULL, NULL)
1968};
1969
Takashi Iwaida864802017-01-31 13:52:22 +01001970static struct platform_driver hdmi_lpe_audio_driver = {
1971 .driver = {
1972 .name = "hdmi-lpe-audio",
Takashi Iwai182cdf22017-02-02 14:43:39 +01001973 .pm = &hdmi_lpe_audio_pm,
Takashi Iwaida864802017-01-31 13:52:22 +01001974 },
1975 .probe = hdmi_lpe_audio_probe,
1976 .remove = hdmi_lpe_audio_remove,
Takashi Iwaida864802017-01-31 13:52:22 +01001977};
1978
1979module_platform_driver(hdmi_lpe_audio_driver);
1980MODULE_ALIAS("platform:hdmi_lpe_audio");
1981
Jerome Anand5dab11d2017-01-25 04:27:52 +05301982MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1983MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1984MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1985MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1986MODULE_DESCRIPTION("Intel HDMI Audio driver");
1987MODULE_LICENSE("GPL v2");
1988MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");