blob: f032610d12875721412bbed212d4170af3285a8d [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;
968
Takashi Iwai182cdf22017-02-02 14:43:39 +0100969 pm_runtime_get_sync(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530970
Takashi Iwai79f439e2017-01-31 16:46:44 +0100971 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +0100972 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
973 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530974 retval = -ENODEV;
Takashi Iwaifa5dfe62017-02-01 22:03:26 +0100975 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530976 }
977
978 /* set the runtime hw parameter with local snd_pcm_hardware struct */
979 runtime->hw = snd_intel_hadstream;
980
Jerome Anand5dab11d2017-01-25 04:27:52 +0530981 retval = snd_pcm_hw_constraint_integer(runtime,
982 SNDRV_PCM_HW_PARAM_PERIODS);
983 if (retval < 0)
Takashi Iwaifa5dfe62017-02-01 22:03:26 +0100984 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530985
986 /* Make sure, that the period size is always aligned
987 * 64byte boundary
988 */
989 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
990 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
Takashi Iwai73997b02017-02-02 17:38:50 +0100991 if (retval < 0)
Takashi Iwaifa5dfe62017-02-01 22:03:26 +0100992 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530993
Takashi Iwai73997b02017-02-02 17:38:50 +0100994 /* expose PCM substream */
Takashi Iwai313d9f22017-02-02 13:00:12 +0100995 spin_lock_irq(&intelhaddata->had_spinlock);
996 intelhaddata->stream_info.substream = substream;
997 intelhaddata->stream_info.substream_refcount++;
998 spin_unlock_irq(&intelhaddata->had_spinlock);
999
Takashi Iwai73997b02017-02-02 17:38:50 +01001000 /* these are cleared in prepare callback, but just to be sure */
1001 intelhaddata->curr_buf = 0;
1002 intelhaddata->underrun_count = 0;
1003 intelhaddata->stream_info.buffer_rendered = 0;
1004
Jerome Anand5dab11d2017-01-25 04:27:52 +05301005 return retval;
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001006 error:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301007 pm_runtime_put(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301008 return retval;
1009}
1010
Takashi Iwaidf76df12017-01-31 16:04:10 +01001011/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001012 * ALSA PCM close callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301013 */
1014static int snd_intelhad_close(struct snd_pcm_substream *substream)
1015{
1016 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301017
Jerome Anand5dab11d2017-01-25 04:27:52 +05301018 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301019
Takashi Iwai73997b02017-02-02 17:38:50 +01001020 /* unreference and sync with the pending PCM accesses */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001021 spin_lock_irq(&intelhaddata->had_spinlock);
1022 intelhaddata->stream_info.substream = NULL;
1023 intelhaddata->stream_info.substream_refcount--;
1024 while (intelhaddata->stream_info.substream_refcount > 0) {
1025 spin_unlock_irq(&intelhaddata->had_spinlock);
1026 cpu_relax();
1027 spin_lock_irq(&intelhaddata->had_spinlock);
1028 }
1029 spin_unlock_irq(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301030
Jerome Anand5dab11d2017-01-25 04:27:52 +05301031 pm_runtime_put(intelhaddata->dev);
1032 return 0;
1033}
1034
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001035/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001036 * ALSA PCM hw_params callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301037 */
1038static int snd_intelhad_hw_params(struct snd_pcm_substream *substream,
1039 struct snd_pcm_hw_params *hw_params)
1040{
Takashi Iwaic75b0472017-01-31 15:49:15 +01001041 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301042 unsigned long addr;
1043 int pages, buf_size, retval;
1044
Takashi Iwaic75b0472017-01-31 15:49:15 +01001045 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301046 buf_size = params_buffer_bytes(hw_params);
1047 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1048 if (retval < 0)
1049 return retval;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001050 dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
1051 __func__, buf_size);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301052 /* mark the pages as uncached region */
1053 addr = (unsigned long) substream->runtime->dma_area;
1054 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1055 retval = set_memory_uc(addr, pages);
1056 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001057 dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
1058 retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301059 return retval;
1060 }
1061 memset(substream->runtime->dma_area, 0, buf_size);
1062
1063 return retval;
1064}
1065
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001066/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001067 * ALSA PCM hw_free callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301068 */
1069static int snd_intelhad_hw_free(struct snd_pcm_substream *substream)
1070{
1071 unsigned long addr;
1072 u32 pages;
1073
Jerome Anand5dab11d2017-01-25 04:27:52 +05301074 /* mark back the pages as cached/writeback region before the free */
1075 if (substream->runtime->dma_area != NULL) {
1076 addr = (unsigned long) substream->runtime->dma_area;
1077 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1078 PAGE_SIZE;
1079 set_memory_wb(addr, pages);
1080 return snd_pcm_lib_free_pages(substream);
1081 }
1082 return 0;
1083}
1084
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001085/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001086 * ALSA PCM trigger callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301087 */
1088static int snd_intelhad_pcm_trigger(struct snd_pcm_substream *substream,
1089 int cmd)
1090{
Takashi Iwaida864802017-01-31 13:52:22 +01001091 int retval = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301092 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301093
Jerome Anand5dab11d2017-01-25 04:27:52 +05301094 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301095
1096 switch (cmd) {
1097 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001098 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1099 case SNDRV_PCM_TRIGGER_RESUME:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301100 /* Disable local INTRs till register prgmng is done */
Takashi Iwai79f439e2017-01-31 16:46:44 +01001101 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001102 dev_dbg(intelhaddata->dev,
1103 "_START: HDMI cable plugged-out\n");
Jerome Anand5dab11d2017-01-25 04:27:52 +05301104 retval = -ENODEV;
1105 break;
1106 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301107
Takashi Iwaif69bd102017-02-02 14:57:22 +01001108 intelhaddata->stream_info.running = true;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301109
1110 /* Enable Audio */
Takashi Iwaida864802017-01-31 13:52:22 +01001111 snd_intelhad_enable_audio_int(intelhaddata, true);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001112 snd_intelhad_enable_audio(substream, intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301113 break;
1114
1115 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001116 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1117 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwaibcce7752017-02-01 17:18:20 +01001118 spin_lock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301119
Takashi Iwaic75b0472017-01-31 15:49:15 +01001120 /* Stop reporting BUFFER_DONE/UNDERRUN to above layers */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301121
Takashi Iwaif69bd102017-02-02 14:57:22 +01001122 intelhaddata->stream_info.running = false;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001123 spin_unlock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301124 /* Disable Audio */
Takashi Iwaida864802017-01-31 13:52:22 +01001125 snd_intelhad_enable_audio_int(intelhaddata, false);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001126 snd_intelhad_enable_audio(substream, intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301127 /* Reset buffer pointers */
Takashi Iwai79dda752017-01-30 17:23:39 +01001128 snd_intelhad_reset_audio(intelhaddata, 1);
1129 snd_intelhad_reset_audio(intelhaddata, 0);
Takashi Iwaida864802017-01-31 13:52:22 +01001130 snd_intelhad_enable_audio_int(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301131 break;
1132
1133 default:
1134 retval = -EINVAL;
1135 }
1136 return retval;
1137}
1138
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001139/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001140 * ALSA PCM prepare callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301141 */
1142static int snd_intelhad_pcm_prepare(struct snd_pcm_substream *substream)
1143{
1144 int retval;
1145 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001146 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301147 struct snd_intelhad *intelhaddata;
1148 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301149
Jerome Anand5dab11d2017-01-25 04:27:52 +05301150 intelhaddata = snd_pcm_substream_chip(substream);
1151 runtime = substream->runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301152
Takashi Iwai79f439e2017-01-31 16:46:44 +01001153 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001154 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
1155 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301156 retval = -ENODEV;
1157 goto prep_end;
1158 }
1159
Takashi Iwaic75b0472017-01-31 15:49:15 +01001160 dev_dbg(intelhaddata->dev, "period_size=%d\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301161 (int)frames_to_bytes(runtime, runtime->period_size));
Takashi Iwaic75b0472017-01-31 15:49:15 +01001162 dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods);
1163 dev_dbg(intelhaddata->dev, "buffer_size=%d\n",
1164 (int)snd_pcm_lib_buffer_bytes(substream));
1165 dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
1166 dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301167
Takashi Iwai73997b02017-02-02 17:38:50 +01001168 intelhaddata->curr_buf = 0;
1169 intelhaddata->underrun_count = 0;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001170 intelhaddata->stream_info.buffer_rendered = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301171
1172 /* Get N value in KHz */
Takashi Iwaida864802017-01-31 13:52:22 +01001173 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301174
Takashi Iwai76296ef2017-01-30 16:09:11 +01001175 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1176 intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301177 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001178 dev_err(intelhaddata->dev,
1179 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301180 goto prep_end;
1181 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001182
1183 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001184 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001185
Takashi Iwai76296ef2017-01-30 16:09:11 +01001186 snd_intelhad_prog_cts(substream->runtime->rate,
1187 disp_samp_freq, link_rate,
1188 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301189
Takashi Iwai76296ef2017-01-30 16:09:11 +01001190 snd_intelhad_prog_dip(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301191
Takashi Iwai76296ef2017-01-30 16:09:11 +01001192 retval = snd_intelhad_audio_ctrl(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301193
1194 /* Prog buffer address */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001195 retval = snd_intelhad_prog_buffer(substream, intelhaddata,
Jerome Anand5dab11d2017-01-25 04:27:52 +05301196 HAD_BUF_TYPE_A, HAD_BUF_TYPE_D);
1197
1198 /*
1199 * Program channel mapping in following order:
1200 * FL, FR, C, LFE, RL, RR
1201 */
1202
Takashi Iwai79dda752017-01-30 17:23:39 +01001203 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301204
1205prep_end:
1206 return retval;
1207}
1208
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001209/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001210 * ALSA PCM pointer callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301211 */
Takashi Iwai44684f62017-02-02 17:27:40 +01001212static snd_pcm_uframes_t
1213snd_intelhad_pcm_pointer(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301214{
1215 struct snd_intelhad *intelhaddata;
1216 u32 bytes_rendered = 0;
1217 u32 t;
1218 int buf_id;
1219
Jerome Anand5dab11d2017-01-25 04:27:52 +05301220 intelhaddata = snd_pcm_substream_chip(substream);
1221
Takashi Iwai79f439e2017-01-31 16:46:44 +01001222 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED)
1223 return SNDRV_PCM_POS_XRUN;
1224
Jerome Anand5dab11d2017-01-25 04:27:52 +05301225 /* Use a hw register to calculate sub-period position reports.
1226 * This makes PulseAudio happier.
1227 */
1228
1229 buf_id = intelhaddata->curr_buf % 4;
Takashi Iwai79dda752017-01-30 17:23:39 +01001230 had_read_register(intelhaddata,
1231 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH), &t);
Jerome Anand232892f2017-01-25 04:27:53 +05301232
1233 if ((t == 0) || (t == ((u32)-1L))) {
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001234 intelhaddata->underrun_count++;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001235 dev_dbg(intelhaddata->dev,
1236 "discovered buffer done for buf %d, count = %d\n",
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001237 buf_id, intelhaddata->underrun_count);
Jerome Anand232892f2017-01-25 04:27:53 +05301238
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001239 if (intelhaddata->underrun_count > (HAD_MIN_PERIODS/2)) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001240 dev_dbg(intelhaddata->dev,
1241 "assume audio_codec_reset, underrun = %d - do xrun\n",
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001242 intelhaddata->underrun_count);
Jerome Anand232892f2017-01-25 04:27:53 +05301243 return SNDRV_PCM_POS_XRUN;
1244 }
1245 } else {
1246 /* Reset Counter */
Takashi Iwai6ddb3ab2017-01-30 18:17:44 +01001247 intelhaddata->underrun_count = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301248 }
Jerome Anand232892f2017-01-25 04:27:53 +05301249
Jerome Anand5dab11d2017-01-25 04:27:52 +05301250 t = intelhaddata->buf_info[buf_id].buf_size - t;
1251
1252 if (intelhaddata->stream_info.buffer_rendered)
1253 div_u64_rem(intelhaddata->stream_info.buffer_rendered,
1254 intelhaddata->stream_info.ring_buf_size,
1255 &(bytes_rendered));
1256
Takashi Iwai7d9e7982017-02-01 22:25:58 +01001257 return bytes_to_frames(substream->runtime, bytes_rendered + t);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301258}
1259
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001260/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001261 * ALSA PCM mmap callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301262 */
1263static int snd_intelhad_pcm_mmap(struct snd_pcm_substream *substream,
1264 struct vm_area_struct *vma)
1265{
Jerome Anand5dab11d2017-01-25 04:27:52 +05301266 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1267 return remap_pfn_range(vma, vma->vm_start,
1268 substream->dma_buffer.addr >> PAGE_SHIFT,
1269 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1270}
1271
Takashi Iwai73997b02017-02-02 17:38:50 +01001272/*
1273 * ALSA PCM ops
1274 */
1275static const struct snd_pcm_ops snd_intelhad_playback_ops = {
1276 .open = snd_intelhad_open,
1277 .close = snd_intelhad_close,
1278 .ioctl = snd_pcm_lib_ioctl,
1279 .hw_params = snd_intelhad_hw_params,
1280 .hw_free = snd_intelhad_hw_free,
1281 .prepare = snd_intelhad_pcm_prepare,
1282 .trigger = snd_intelhad_pcm_trigger,
1283 .pointer = snd_intelhad_pcm_pointer,
1284 .mmap = snd_intelhad_pcm_mmap,
1285};
1286
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001287/* process mode change of the running stream; called in mutex */
Takashi Iwaida864802017-01-31 13:52:22 +01001288static int hdmi_audio_mode_change(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301289{
Takashi Iwaida864802017-01-31 13:52:22 +01001290 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301291 int retval = 0;
1292 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001293 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301294
Takashi Iwai313d9f22017-02-02 13:00:12 +01001295 substream = had_substream_get(intelhaddata);
1296 if (!substream)
Takashi Iwaida864802017-01-31 13:52:22 +01001297 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301298
1299 /* Disable Audio */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001300 snd_intelhad_enable_audio(substream, intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301301
1302 /* Update CTS value */
Takashi Iwaida864802017-01-31 13:52:22 +01001303 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301304
Takashi Iwai76296ef2017-01-30 16:09:11 +01001305 retval = snd_intelhad_prog_n(substream->runtime->rate, &n_param,
1306 intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301307 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001308 dev_err(intelhaddata->dev,
1309 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301310 goto out;
1311 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001312
1313 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001314 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001315
Takashi Iwai76296ef2017-01-30 16:09:11 +01001316 snd_intelhad_prog_cts(substream->runtime->rate,
1317 disp_samp_freq, link_rate,
1318 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301319
1320 /* Enable Audio */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001321 snd_intelhad_enable_audio(substream, intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301322
1323out:
Takashi Iwai313d9f22017-02-02 13:00:12 +01001324 had_substream_put(intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301325 return retval;
1326}
1327
Takashi Iwai372d8552017-01-31 13:57:58 +01001328static inline int had_chk_intrmiss(struct snd_intelhad *intelhaddata,
1329 enum intel_had_aud_buf_type buf_id)
1330{
1331 int i, intr_count = 0;
1332 enum intel_had_aud_buf_type buff_done;
1333 u32 buf_size, buf_addr;
Takashi Iwai372d8552017-01-31 13:57:58 +01001334
1335 buff_done = buf_id;
1336
1337 intr_count = snd_intelhad_read_len(intelhaddata);
1338 if (intr_count > 1) {
1339 /* In case of active playback */
Takashi Iwaic75b0472017-01-31 15:49:15 +01001340 dev_err(intelhaddata->dev,
1341 "Driver detected %d missed buffer done interrupt(s)\n",
1342 (intr_count - 1));
Takashi Iwai372d8552017-01-31 13:57:58 +01001343 if (intr_count > 3)
1344 return intr_count;
1345
1346 buf_id += (intr_count - 1);
1347 /* Reprogram registers*/
1348 for (i = buff_done; i < buf_id; i++) {
1349 int j = i % 4;
1350
1351 buf_size = intelhaddata->buf_info[j].buf_size;
1352 buf_addr = intelhaddata->buf_info[j].buf_addr;
1353 had_write_register(intelhaddata,
1354 AUD_BUF_A_LENGTH +
1355 (j * HAD_REG_WIDTH), buf_size);
1356 had_write_register(intelhaddata,
1357 AUD_BUF_A_ADDR+(j * HAD_REG_WIDTH),
1358 (buf_addr | BIT(0) | BIT(1)));
1359 }
1360 buf_id = buf_id % 4;
Takashi Iwai372d8552017-01-31 13:57:58 +01001361 intelhaddata->buff_done = buf_id;
Takashi Iwai372d8552017-01-31 13:57:58 +01001362 }
1363
1364 return intr_count;
1365}
1366
Takashi Iwaibcce7752017-02-01 17:18:20 +01001367/* called from irq handler */
Takashi Iwai372d8552017-01-31 13:57:58 +01001368static int had_process_buffer_done(struct snd_intelhad *intelhaddata)
1369{
1370 u32 len = 1;
1371 enum intel_had_aud_buf_type buf_id;
1372 enum intel_had_aud_buf_type buff_done;
1373 struct pcm_stream_info *stream;
Takashi Iwai313d9f22017-02-02 13:00:12 +01001374 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001375 u32 buf_size;
Takashi Iwai372d8552017-01-31 13:57:58 +01001376 int intr_count;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001377 unsigned long flags;
Takashi Iwai372d8552017-01-31 13:57:58 +01001378
Takashi Iwai372d8552017-01-31 13:57:58 +01001379 stream = &intelhaddata->stream_info;
1380 intr_count = 1;
1381
Takashi Iwaibcce7752017-02-01 17:18:20 +01001382 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
Takashi Iwai372d8552017-01-31 13:57:58 +01001383 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaibcce7752017-02-01 17:18:20 +01001384 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001385 dev_dbg(intelhaddata->dev,
1386 "%s:Device already disconnected\n", __func__);
Takashi Iwai372d8552017-01-31 13:57:58 +01001387 return 0;
1388 }
1389 buf_id = intelhaddata->curr_buf;
1390 intelhaddata->buff_done = buf_id;
1391 buff_done = intelhaddata->buff_done;
1392 buf_size = intelhaddata->buf_info[buf_id].buf_size;
Takashi Iwai372d8552017-01-31 13:57:58 +01001393
Takashi Iwai372d8552017-01-31 13:57:58 +01001394 /* Every debug statement has an implication
1395 * of ~5msec. Thus, avoid having >3 debug statements
1396 * for each buffer_done handling.
1397 */
1398
1399 /* Check for any intr_miss in case of active playback */
Takashi Iwaif69bd102017-02-02 14:57:22 +01001400 if (stream->running) {
Takashi Iwai372d8552017-01-31 13:57:58 +01001401 intr_count = had_chk_intrmiss(intelhaddata, buf_id);
1402 if (!intr_count || (intr_count > 3)) {
Takashi Iwaibcce7752017-02-01 17:18:20 +01001403 spin_unlock_irqrestore(&intelhaddata->had_spinlock,
1404 flags);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001405 dev_err(intelhaddata->dev,
1406 "HAD SW state in non-recoverable mode\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001407 return 0;
1408 }
1409 buf_id += (intr_count - 1);
1410 buf_id = buf_id % 4;
Takashi Iwai372d8552017-01-31 13:57:58 +01001411 }
1412
1413 intelhaddata->buf_info[buf_id].is_valid = true;
1414 if (intelhaddata->valid_buf_cnt-1 == buf_id) {
Takashi Iwaif69bd102017-02-02 14:57:22 +01001415 if (stream->running)
Takashi Iwai372d8552017-01-31 13:57:58 +01001416 intelhaddata->curr_buf = HAD_BUF_TYPE_A;
1417 } else
1418 intelhaddata->curr_buf = buf_id + 1;
1419
Takashi Iwaibcce7752017-02-01 17:18:20 +01001420 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
Takashi Iwai372d8552017-01-31 13:57:58 +01001421
Takashi Iwai79f439e2017-01-31 16:46:44 +01001422 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001423 dev_dbg(intelhaddata->dev, "HDMI cable plugged-out\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001424 return 0;
1425 }
1426
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001427 /* Reprogram the registers with addr and length */
Takashi Iwai372d8552017-01-31 13:57:58 +01001428 had_write_register(intelhaddata,
1429 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH),
1430 buf_size);
1431 had_write_register(intelhaddata,
1432 AUD_BUF_A_ADDR + (buf_id * HAD_REG_WIDTH),
1433 intelhaddata->buf_info[buf_id].buf_addr |
1434 BIT(0) | BIT(1));
1435
1436 had_read_register(intelhaddata,
1437 AUD_BUF_A_LENGTH + (buf_id * HAD_REG_WIDTH),
1438 &len);
Takashi Iwaic75b0472017-01-31 15:49:15 +01001439 dev_dbg(intelhaddata->dev, "%s:Enabled buf[%d]\n", __func__, buf_id);
Takashi Iwai372d8552017-01-31 13:57:58 +01001440
1441 /* In case of actual data,
1442 * report buffer_done to above ALSA layer
1443 */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001444 substream = had_substream_get(intelhaddata);
1445 if (substream) {
1446 buf_size = intelhaddata->buf_info[buf_id].buf_size;
Takashi Iwai372d8552017-01-31 13:57:58 +01001447 intelhaddata->stream_info.buffer_rendered +=
1448 (intr_count * buf_size);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001449 snd_pcm_period_elapsed(substream);
1450 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001451 }
1452
1453 return 0;
1454}
1455
Takashi Iwaibcce7752017-02-01 17:18:20 +01001456/* called from irq handler */
Takashi Iwai372d8552017-01-31 13:57:58 +01001457static int had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
1458{
1459 enum intel_had_aud_buf_type buf_id;
1460 struct pcm_stream_info *stream;
Takashi Iwai313d9f22017-02-02 13:00:12 +01001461 struct snd_pcm_substream *substream;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001462 unsigned long flags;
Takashi Iwai372d8552017-01-31 13:57:58 +01001463 int drv_status;
1464
Takashi Iwai372d8552017-01-31 13:57:58 +01001465 stream = &intelhaddata->stream_info;
1466
Takashi Iwaibcce7752017-02-01 17:18:20 +01001467 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
Takashi Iwai372d8552017-01-31 13:57:58 +01001468 buf_id = intelhaddata->curr_buf;
Takashi Iwai372d8552017-01-31 13:57:58 +01001469 intelhaddata->buff_done = buf_id;
1470 drv_status = intelhaddata->drv_status;
Takashi Iwaif69bd102017-02-02 14:57:22 +01001471 if (stream->running)
Takashi Iwai372d8552017-01-31 13:57:58 +01001472 intelhaddata->curr_buf = HAD_BUF_TYPE_A;
1473
Takashi Iwaibcce7752017-02-01 17:18:20 +01001474 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
Takashi Iwai372d8552017-01-31 13:57:58 +01001475
Takashi Iwaif69bd102017-02-02 14:57:22 +01001476 dev_dbg(intelhaddata->dev, "Enter:%s buf_id=%d, stream_running=%d\n",
1477 __func__, buf_id, stream->running);
Takashi Iwai372d8552017-01-31 13:57:58 +01001478
1479 snd_intelhad_handle_underrun(intelhaddata);
1480
1481 if (drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001482 dev_dbg(intelhaddata->dev,
1483 "%s:Device already disconnected\n", __func__);
Takashi Iwai372d8552017-01-31 13:57:58 +01001484 return 0;
1485 }
1486
Takashi Iwaif69bd102017-02-02 14:57:22 +01001487 /* Report UNDERRUN error to above layers */
1488 substream = had_substream_get(intelhaddata);
1489 if (substream) {
1490 snd_pcm_stop_xrun(substream);
1491 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001492 }
1493
1494 return 0;
1495}
1496
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001497/* process hot plug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001498static void had_process_hot_plug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001499{
1500 enum intel_had_aud_buf_type buf_id;
1501 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001502
Takashi Iwaibcce7752017-02-01 17:18:20 +01001503 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001504 if (intelhaddata->drv_status == HAD_DRV_CONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001505 dev_dbg(intelhaddata->dev, "Device already connected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001506 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001507 return;
Takashi Iwai372d8552017-01-31 13:57:58 +01001508 }
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001509
Takashi Iwai372d8552017-01-31 13:57:58 +01001510 buf_id = intelhaddata->curr_buf;
1511 intelhaddata->buff_done = buf_id;
1512 intelhaddata->drv_status = HAD_DRV_CONNECTED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001513 dev_dbg(intelhaddata->dev,
1514 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001515 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001516 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001517
Takashi Iwaic75b0472017-01-31 15:49:15 +01001518 dev_dbg(intelhaddata->dev, "Processing HOT_PLUG, buf_id = %d\n",
1519 buf_id);
Takashi Iwai372d8552017-01-31 13:57:58 +01001520
1521 /* Safety check */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001522 substream = had_substream_get(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001523 if (substream) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001524 dev_dbg(intelhaddata->dev,
1525 "Force to stop the active stream by disconnection\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001526 /* Set runtime->state to hw_params done */
1527 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001528 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001529 }
1530
1531 had_build_channel_allocation_map(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001532}
1533
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001534/* process hot unplug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001535static void had_process_hot_unplug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001536{
1537 enum intel_had_aud_buf_type buf_id;
Takashi Iwai313d9f22017-02-02 13:00:12 +01001538 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001539
Takashi Iwai372d8552017-01-31 13:57:58 +01001540 buf_id = intelhaddata->curr_buf;
1541
Takashi Iwai313d9f22017-02-02 13:00:12 +01001542 substream = had_substream_get(intelhaddata);
1543
Takashi Iwaibcce7752017-02-01 17:18:20 +01001544 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001545
1546 if (intelhaddata->drv_status == HAD_DRV_DISCONNECTED) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001547 dev_dbg(intelhaddata->dev, "Device already disconnected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001548 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001549 goto out;
Takashi Iwai372d8552017-01-31 13:57:58 +01001550
Takashi Iwai372d8552017-01-31 13:57:58 +01001551 }
1552
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001553 /* Disable Audio */
1554 snd_intelhad_enable_audio_int(intelhaddata, false);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001555 snd_intelhad_enable_audio(substream, intelhaddata, false);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001556
Takashi Iwai372d8552017-01-31 13:57:58 +01001557 intelhaddata->drv_status = HAD_DRV_DISCONNECTED;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001558 dev_dbg(intelhaddata->dev,
1559 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001560 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001561 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001562
1563 /* Report to above ALSA layer */
1564 if (substream)
1565 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1566
1567 out:
1568 if (substream)
1569 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001570 kfree(intelhaddata->chmap->chmap);
1571 intelhaddata->chmap->chmap = NULL;
Takashi Iwai372d8552017-01-31 13:57:58 +01001572}
1573
Takashi Iwai73997b02017-02-02 17:38:50 +01001574/*
1575 * ALSA iec958 and ELD controls
1576 */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301577
Jerome Anand5dab11d2017-01-25 04:27:52 +05301578static int had_iec958_info(struct snd_kcontrol *kcontrol,
1579 struct snd_ctl_elem_info *uinfo)
1580{
1581 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1582 uinfo->count = 1;
1583 return 0;
1584}
1585
1586static int had_iec958_get(struct snd_kcontrol *kcontrol,
1587 struct snd_ctl_elem_value *ucontrol)
1588{
1589 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1590
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001591 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301592 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1593 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1594 ucontrol->value.iec958.status[2] =
1595 (intelhaddata->aes_bits >> 16) & 0xff;
1596 ucontrol->value.iec958.status[3] =
1597 (intelhaddata->aes_bits >> 24) & 0xff;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001598 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301599 return 0;
1600}
Takashi Iwai372d8552017-01-31 13:57:58 +01001601
Jerome Anand5dab11d2017-01-25 04:27:52 +05301602static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1603 struct snd_ctl_elem_value *ucontrol)
1604{
1605 ucontrol->value.iec958.status[0] = 0xff;
1606 ucontrol->value.iec958.status[1] = 0xff;
1607 ucontrol->value.iec958.status[2] = 0xff;
1608 ucontrol->value.iec958.status[3] = 0xff;
1609 return 0;
1610}
Takashi Iwai372d8552017-01-31 13:57:58 +01001611
Jerome Anand5dab11d2017-01-25 04:27:52 +05301612static int had_iec958_put(struct snd_kcontrol *kcontrol,
1613 struct snd_ctl_elem_value *ucontrol)
1614{
1615 unsigned int val;
1616 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001617 int changed = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301618
Jerome Anand5dab11d2017-01-25 04:27:52 +05301619 val = (ucontrol->value.iec958.status[0] << 0) |
1620 (ucontrol->value.iec958.status[1] << 8) |
1621 (ucontrol->value.iec958.status[2] << 16) |
1622 (ucontrol->value.iec958.status[3] << 24);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001623 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301624 if (intelhaddata->aes_bits != val) {
1625 intelhaddata->aes_bits = val;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001626 changed = 1;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301627 }
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001628 mutex_unlock(&intelhaddata->mutex);
1629 return changed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301630}
1631
Takashi Iwai4aedb942017-02-02 16:38:39 +01001632static int had_ctl_eld_info(struct snd_kcontrol *kcontrol,
1633 struct snd_ctl_elem_info *uinfo)
1634{
1635 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1636 uinfo->count = HDMI_MAX_ELD_BYTES;
1637 return 0;
1638}
1639
1640static int had_ctl_eld_get(struct snd_kcontrol *kcontrol,
1641 struct snd_ctl_elem_value *ucontrol)
1642{
1643 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1644
1645 mutex_lock(&intelhaddata->mutex);
1646 memcpy(ucontrol->value.bytes.data, intelhaddata->eld,
1647 HDMI_MAX_ELD_BYTES);
1648 mutex_unlock(&intelhaddata->mutex);
1649 return 0;
1650}
1651
Takashi Iwai73997b02017-02-02 17:38:50 +01001652static const struct snd_kcontrol_new had_controls[] = {
Takashi Iwai4aedb942017-02-02 16:38:39 +01001653 {
1654 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1655 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1656 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1657 .info = had_iec958_info, /* shared */
1658 .get = had_iec958_mask_get,
1659 },
1660 {
1661 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1662 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1663 .info = had_iec958_info,
1664 .get = had_iec958_get,
1665 .put = had_iec958_put,
1666 },
1667 {
1668 .access = (SNDRV_CTL_ELEM_ACCESS_READ |
1669 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1670 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1671 .name = "ELD",
1672 .info = had_ctl_eld_info,
1673 .get = had_ctl_eld_get,
1674 },
Jerome Anand5dab11d2017-01-25 04:27:52 +05301675};
1676
Takashi Iwai73997b02017-02-02 17:38:50 +01001677/*
1678 * audio interrupt handler
1679 */
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
Takashi Iwai73997b02017-02-02 17:38:50 +01001701/*
1702 * monitor plug/unplug notification from i915; just kick off the work
1703 */
Takashi Iwaida864802017-01-31 13:52:22 +01001704static void notify_audio_lpe(struct platform_device *pdev)
1705{
1706 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Takashi Iwaida864802017-01-31 13:52:22 +01001707
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001708 schedule_work(&ctx->hdmi_audio_wq);
1709}
Takashi Iwaida864802017-01-31 13:52:22 +01001710
Takashi Iwai73997b02017-02-02 17:38:50 +01001711/* the work to handle monitor hot plug/unplug */
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001712static void had_audio_wq(struct work_struct *work)
1713{
1714 struct snd_intelhad *ctx =
1715 container_of(work, struct snd_intelhad, hdmi_audio_wq);
1716 struct intel_hdmi_lpe_audio_pdata *pdata = ctx->dev->platform_data;
1717
Takashi Iwai182cdf22017-02-02 14:43:39 +01001718 pm_runtime_get_sync(ctx->dev);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001719 mutex_lock(&ctx->mutex);
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001720 if (!pdata->hdmi_connected) {
1721 dev_dbg(ctx->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001722 __func__);
Takashi Iwai4aedb942017-02-02 16:38:39 +01001723 memset(ctx->eld, 0, sizeof(ctx->eld)); /* clear the old ELD */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001724 had_process_hot_unplug(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001725 } else {
1726 struct intel_hdmi_lpe_audio_eld *eld = &pdata->eld;
1727
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001728 dev_dbg(ctx->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
1729 __func__, eld->port_id, pdata->tmds_clock_speed);
1730
Takashi Iwaida864802017-01-31 13:52:22 +01001731 switch (eld->pipe_id) {
1732 case 0:
1733 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1734 break;
1735 case 1:
1736 ctx->had_config_offset = AUDIO_HDMI_CONFIG_B;
1737 break;
1738 case 2:
1739 ctx->had_config_offset = AUDIO_HDMI_CONFIG_C;
1740 break;
1741 default:
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001742 dev_dbg(ctx->dev, "Invalid pipe %d\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001743 eld->pipe_id);
1744 break;
1745 }
1746
Takashi Iwaidf0435d2017-02-02 15:37:11 +01001747 memcpy(ctx->eld, eld->eld_data, sizeof(ctx->eld));
Takashi Iwaida864802017-01-31 13:52:22 +01001748
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001749 ctx->dp_output = pdata->dp_output;
1750 ctx->tmds_clock_speed = pdata->tmds_clock_speed;
1751 ctx->link_rate = pdata->link_rate;
1752
Takashi Iwaida864802017-01-31 13:52:22 +01001753 had_process_hot_plug(ctx);
1754
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001755 /* Process mode change if stream is active */
Takashi Iwaif69bd102017-02-02 14:57:22 +01001756 hdmi_audio_mode_change(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001757 }
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001758 mutex_unlock(&ctx->mutex);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001759 pm_runtime_put(ctx->dev);
1760}
1761
1762/*
1763 * PM callbacks
1764 */
1765
1766static int hdmi_lpe_audio_runtime_suspend(struct device *dev)
1767{
1768 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1769 struct snd_pcm_substream *substream;
1770
1771 substream = had_substream_get(ctx);
1772 if (substream) {
1773 snd_pcm_suspend(substream);
1774 had_substream_put(ctx);
1775 }
1776
1777 return 0;
1778}
1779
1780static int hdmi_lpe_audio_suspend(struct device *dev)
1781{
1782 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1783 int err;
1784
1785 err = hdmi_lpe_audio_runtime_suspend(dev);
1786 if (!err)
1787 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D3hot);
1788 return err;
1789}
1790
1791static int hdmi_lpe_audio_resume(struct device *dev)
1792{
1793 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1794
1795 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D0);
1796 return 0;
Takashi Iwaida864802017-01-31 13:52:22 +01001797}
1798
1799/* release resources */
1800static void hdmi_lpe_audio_free(struct snd_card *card)
1801{
1802 struct snd_intelhad *ctx = card->private_data;
1803
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001804 cancel_work_sync(&ctx->hdmi_audio_wq);
1805
Takashi Iwaida864802017-01-31 13:52:22 +01001806 if (ctx->mmio_start)
1807 iounmap(ctx->mmio_start);
1808 if (ctx->irq >= 0)
1809 free_irq(ctx->irq, ctx);
1810}
1811
1812/*
1813 * hdmi_lpe_audio_probe - start bridge with i915
1814 *
1815 * This function is called when the i915 driver creates the
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001816 * hdmi-lpe-audio platform device.
Takashi Iwaida864802017-01-31 13:52:22 +01001817 */
1818static int hdmi_lpe_audio_probe(struct platform_device *pdev)
1819{
1820 struct snd_card *card;
1821 struct snd_intelhad *ctx;
1822 struct snd_pcm *pcm;
1823 struct intel_hdmi_lpe_audio_pdata *pdata;
1824 int irq;
1825 struct resource *res_mmio;
Takashi Iwai4aedb942017-02-02 16:38:39 +01001826 int i, ret;
Takashi Iwaida864802017-01-31 13:52:22 +01001827
Takashi Iwaida864802017-01-31 13:52:22 +01001828 dev_dbg(&pdev->dev, "dma_mask: %p\n", pdev->dev.dma_mask);
1829
1830 pdata = pdev->dev.platform_data;
1831 if (!pdata) {
1832 dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
1833 return -EINVAL;
1834 }
1835
1836 /* get resources */
1837 irq = platform_get_irq(pdev, 0);
1838 if (irq < 0) {
1839 dev_err(&pdev->dev, "Could not get irq resource\n");
1840 return -ENODEV;
1841 }
1842
1843 res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1844 if (!res_mmio) {
1845 dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
1846 return -ENXIO;
1847 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301848
Takashi Iwai5647aec2017-01-31 08:14:34 +01001849 /* create a card instance with ALSA framework */
Takashi Iwaida864802017-01-31 13:52:22 +01001850 ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
1851 THIS_MODULE, sizeof(*ctx), &card);
1852 if (ret)
1853 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301854
Takashi Iwaida864802017-01-31 13:52:22 +01001855 ctx = card->private_data;
1856 spin_lock_init(&ctx->had_spinlock);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001857 mutex_init(&ctx->mutex);
Takashi Iwaida864802017-01-31 13:52:22 +01001858 ctx->drv_status = HAD_DRV_DISCONNECTED;
1859 ctx->dev = &pdev->dev;
1860 ctx->card = card;
Takashi Iwaida864802017-01-31 13:52:22 +01001861 ctx->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
1862 strcpy(card->driver, INTEL_HAD);
1863 strcpy(card->shortname, INTEL_HAD);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301864
Takashi Iwaida864802017-01-31 13:52:22 +01001865 ctx->irq = -1;
1866 ctx->tmds_clock_speed = DIS_SAMPLE_RATE_148_5;
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001867 INIT_WORK(&ctx->hdmi_audio_wq, had_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301868
Takashi Iwaida864802017-01-31 13:52:22 +01001869 card->private_free = hdmi_lpe_audio_free;
1870
1871 /* assume pipe A as default */
1872 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1873
1874 platform_set_drvdata(pdev, ctx);
1875
1876 dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
1877 __func__, (unsigned int)res_mmio->start,
1878 (unsigned int)res_mmio->end);
1879
1880 ctx->mmio_start = ioremap_nocache(res_mmio->start,
1881 (size_t)(resource_size(res_mmio)));
1882 if (!ctx->mmio_start) {
1883 dev_err(&pdev->dev, "Could not get ioremap\n");
1884 ret = -EACCES;
1885 goto err;
1886 }
1887
1888 /* setup interrupt handler */
1889 ret = request_irq(irq, display_pipe_interrupt_handler, 0,
1890 pdev->name, ctx);
1891 if (ret < 0) {
1892 dev_err(&pdev->dev, "request_irq failed\n");
1893 goto err;
1894 }
1895
1896 ctx->irq = irq;
1897
1898 ret = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
1899 MAX_CAP_STREAMS, &pcm);
1900 if (ret)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301901 goto err;
1902
1903 /* setup private data which can be retrieved when required */
Takashi Iwaida864802017-01-31 13:52:22 +01001904 pcm->private_data = ctx;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301905 pcm->info_flags = 0;
1906 strncpy(pcm->name, card->shortname, strlen(card->shortname));
Takashi Iwaida864802017-01-31 13:52:22 +01001907 /* setup the ops for playabck */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301908 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK,
1909 &snd_intelhad_playback_ops);
1910 /* allocate dma pages for ALSA stream operations
1911 * memory allocated is based on size, not max value
1912 * thus using same argument for max & size
1913 */
Takashi Iwaida864802017-01-31 13:52:22 +01001914 snd_pcm_lib_preallocate_pages_for_all(pcm,
Jerome Anand5dab11d2017-01-25 04:27:52 +05301915 SNDRV_DMA_TYPE_DEV, NULL,
1916 HAD_MAX_BUFFER, HAD_MAX_BUFFER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301917
Takashi Iwai4aedb942017-02-02 16:38:39 +01001918 /* create controls */
1919 for (i = 0; i < ARRAY_SIZE(had_controls); i++) {
1920 ret = snd_ctl_add(card, snd_ctl_new1(&had_controls[i], ctx));
1921 if (ret < 0)
1922 goto err;
1923 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301924
1925 init_channel_allocations();
1926
1927 /* Register channel map controls */
Takashi Iwaida864802017-01-31 13:52:22 +01001928 ret = had_register_chmap_ctls(ctx, pcm);
1929 if (ret < 0)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301930 goto err;
1931
Takashi Iwaida864802017-01-31 13:52:22 +01001932 ret = snd_card_register(card);
1933 if (ret)
Takashi Iwai36ec0d92017-01-31 08:47:05 +01001934 goto err;
1935
Takashi Iwaibcce7752017-02-01 17:18:20 +01001936 spin_lock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001937 pdata->notify_audio_lpe = notify_audio_lpe;
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001938 pdata->notify_pending = false;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001939 spin_unlock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001940
1941 pm_runtime_set_active(&pdev->dev);
1942 pm_runtime_enable(&pdev->dev);
1943
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001944 dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
Takashi Iwaida864802017-01-31 13:52:22 +01001945 schedule_work(&ctx->hdmi_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301946
Takashi Iwai79dda752017-01-30 17:23:39 +01001947 return 0;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001948
Jerome Anand5dab11d2017-01-25 04:27:52 +05301949err:
1950 snd_card_free(card);
Takashi Iwaida864802017-01-31 13:52:22 +01001951 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301952}
1953
Takashi Iwai79dda752017-01-30 17:23:39 +01001954/*
Takashi Iwaida864802017-01-31 13:52:22 +01001955 * hdmi_lpe_audio_remove - stop bridge with i915
Jerome Anand5dab11d2017-01-25 04:27:52 +05301956 *
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001957 * This function is called when the platform device is destroyed.
Jerome Anand5dab11d2017-01-25 04:27:52 +05301958 */
Takashi Iwaida864802017-01-31 13:52:22 +01001959static int hdmi_lpe_audio_remove(struct platform_device *pdev)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301960{
Takashi Iwaida864802017-01-31 13:52:22 +01001961 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301962
Takashi Iwaida864802017-01-31 13:52:22 +01001963 if (ctx->drv_status != HAD_DRV_DISCONNECTED)
1964 snd_intelhad_enable_audio_int(ctx, false);
1965 snd_card_free(ctx->card);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301966 return 0;
1967}
1968
Takashi Iwai182cdf22017-02-02 14:43:39 +01001969static const struct dev_pm_ops hdmi_lpe_audio_pm = {
1970 SET_SYSTEM_SLEEP_PM_OPS(hdmi_lpe_audio_suspend, hdmi_lpe_audio_resume)
1971 SET_RUNTIME_PM_OPS(hdmi_lpe_audio_runtime_suspend, NULL, NULL)
1972};
1973
Takashi Iwaida864802017-01-31 13:52:22 +01001974static struct platform_driver hdmi_lpe_audio_driver = {
1975 .driver = {
1976 .name = "hdmi-lpe-audio",
Takashi Iwai182cdf22017-02-02 14:43:39 +01001977 .pm = &hdmi_lpe_audio_pm,
Takashi Iwaida864802017-01-31 13:52:22 +01001978 },
1979 .probe = hdmi_lpe_audio_probe,
1980 .remove = hdmi_lpe_audio_remove,
Takashi Iwaida864802017-01-31 13:52:22 +01001981};
1982
1983module_platform_driver(hdmi_lpe_audio_driver);
1984MODULE_ALIAS("platform:hdmi_lpe_audio");
1985
Jerome Anand5dab11d2017-01-25 04:27:52 +05301986MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1987MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1988MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1989MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1990MODULE_DESCRIPTION("Intel HDMI Audio driver");
1991MODULE_LICENSE("GPL v2");
1992MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");