blob: fa7dca5a68c897ebd201953f4be7040f9124786c [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>
Takashi Iwai412bbe7d52017-02-02 22:03:22 +010031#include <linux/dma-mapping.h>
Takashi Iwaie2acecf2017-02-11 08:21:56 +010032#include <linux/delay.h>
Laura Abbott7f80f512017-05-08 15:58:35 -070033#include <asm/set_memory.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053034#include <sound/core.h>
Takashi Iwai03c34372017-02-02 16:19:03 +010035#include <sound/asoundef.h>
36#include <sound/pcm.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053037#include <sound/pcm_params.h>
38#include <sound/initval.h>
39#include <sound/control.h>
Takashi Iwaib9bacf22017-02-14 12:29:38 +010040#include <sound/jack.h>
Takashi Iwai03c34372017-02-02 16:19:03 +010041#include <drm/drm_edid.h>
Takashi Iwaida864802017-01-31 13:52:22 +010042#include <drm/intel_lpe_audio.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053043#include "intel_hdmi_audio.h"
44
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +030045#define for_each_pipe(card_ctx, pipe) \
46 for ((pipe) = 0; (pipe) < (card_ctx)->num_pipes; (pipe)++)
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +030047#define for_each_port(card_ctx, port) \
48 for ((port) = 0; (port) < (card_ctx)->num_ports; (port)++)
49
Jerome Anand5dab11d2017-01-25 04:27:52 +053050/*standard module options for ALSA. This module supports only one card*/
51static int hdmi_card_index = SNDRV_DEFAULT_IDX1;
52static char *hdmi_card_id = SNDRV_DEFAULT_STR1;
Takashi Iwai7229b122018-02-20 17:33:45 +010053static bool single_port;
Jerome Anand5dab11d2017-01-25 04:27:52 +053054
55module_param_named(index, hdmi_card_index, int, 0444);
56MODULE_PARM_DESC(index,
57 "Index value for INTEL Intel HDMI Audio controller.");
58module_param_named(id, hdmi_card_id, charp, 0444);
59MODULE_PARM_DESC(id,
60 "ID string for INTEL Intel HDMI Audio controller.");
Takashi Iwai7229b122018-02-20 17:33:45 +010061module_param(single_port, bool, 0444);
62MODULE_PARM_DESC(single_port,
63 "Single-port mode (for compatibility)");
Jerome Anand5dab11d2017-01-25 04:27:52 +053064
65/*
66 * ELD SA bits in the CEA Speaker Allocation data block
67 */
Takashi Iwai4a5ddb22017-02-01 16:45:38 +010068static const int eld_speaker_allocation_bits[] = {
Jerome Anand5dab11d2017-01-25 04:27:52 +053069 [0] = FL | FR,
70 [1] = LFE,
71 [2] = FC,
72 [3] = RL | RR,
73 [4] = RC,
74 [5] = FLC | FRC,
75 [6] = RLC | RRC,
76 /* the following are not defined in ELD yet */
77 [7] = 0,
78};
79
80/*
81 * This is an ordered list!
82 *
83 * The preceding ones have better chances to be selected by
84 * hdmi_channel_allocation().
85 */
86static struct cea_channel_speaker_allocation channel_allocations[] = {
87/* channel: 7 6 5 4 3 2 1 0 */
88{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
89 /* 2.1 */
90{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
91 /* Dolby Surround */
92{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
93 /* surround40 */
94{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
95 /* surround41 */
96{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
97 /* surround50 */
98{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
99 /* surround51 */
100{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
101 /* 6.1 */
102{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
103 /* surround71 */
104{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
105
106{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
107{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
108{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
109{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
110{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
111{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
112{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
113{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
114{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
115{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
116{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
117{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
118{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
119{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
120{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
121{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
122{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
123{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
124{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
125{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
126{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
127{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
128{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
129};
130
Takashi Iwai4a5ddb22017-02-01 16:45:38 +0100131static const struct channel_map_table map_tables[] = {
Jerome Anand5dab11d2017-01-25 04:27:52 +0530132 { SNDRV_CHMAP_FL, 0x00, FL },
133 { SNDRV_CHMAP_FR, 0x01, FR },
134 { SNDRV_CHMAP_RL, 0x04, RL },
135 { SNDRV_CHMAP_RR, 0x05, RR },
136 { SNDRV_CHMAP_LFE, 0x02, LFE },
137 { SNDRV_CHMAP_FC, 0x03, FC },
138 { SNDRV_CHMAP_RLC, 0x06, RLC },
139 { SNDRV_CHMAP_RRC, 0x07, RRC },
140 {} /* terminator */
141};
142
143/* hardware capability structure */
Takashi Iwaib5562902017-02-04 22:05:33 +0100144static const struct snd_pcm_hardware had_pcm_hardware = {
Jerome Anand5dab11d2017-01-25 04:27:52 +0530145 .info = (SNDRV_PCM_INFO_INTERLEAVED |
Takashi Iwaia9ebdd02017-02-02 21:33:54 +0100146 SNDRV_PCM_INFO_MMAP |
Takashi Iwaie8de9852017-02-07 08:09:12 +0100147 SNDRV_PCM_INFO_MMAP_VALID |
148 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
Takashi Iwai3fe2cf72017-02-07 13:53:42 +0100149 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
150 SNDRV_PCM_FMTBIT_S24_LE |
Takashi Iwai85bd8742017-02-07 13:33:17 +0100151 SNDRV_PCM_FMTBIT_S32_LE),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530152 .rates = SNDRV_PCM_RATE_32000 |
153 SNDRV_PCM_RATE_44100 |
154 SNDRV_PCM_RATE_48000 |
155 SNDRV_PCM_RATE_88200 |
156 SNDRV_PCM_RATE_96000 |
157 SNDRV_PCM_RATE_176400 |
158 SNDRV_PCM_RATE_192000,
159 .rate_min = HAD_MIN_RATE,
160 .rate_max = HAD_MAX_RATE,
161 .channels_min = HAD_MIN_CHANNEL,
162 .channels_max = HAD_MAX_CHANNEL,
163 .buffer_bytes_max = HAD_MAX_BUFFER,
164 .period_bytes_min = HAD_MIN_PERIOD_BYTES,
165 .period_bytes_max = HAD_MAX_PERIOD_BYTES,
166 .periods_min = HAD_MIN_PERIODS,
167 .periods_max = HAD_MAX_PERIODS,
168 .fifo_size = HAD_FIFO_SIZE,
169};
170
Takashi Iwai313d9f22017-02-02 13:00:12 +0100171/* Get the active PCM substream;
172 * Call had_substream_put() for unreferecing.
173 * Don't call this inside had_spinlock, as it takes by itself
174 */
175static struct snd_pcm_substream *
176had_substream_get(struct snd_intelhad *intelhaddata)
177{
178 struct snd_pcm_substream *substream;
179 unsigned long flags;
180
181 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
182 substream = intelhaddata->stream_info.substream;
183 if (substream)
184 intelhaddata->stream_info.substream_refcount++;
185 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
186 return substream;
187}
188
189/* Unref the active PCM substream;
190 * Don't call this inside had_spinlock, as it takes by itself
191 */
192static void had_substream_put(struct snd_intelhad *intelhaddata)
193{
194 unsigned long flags;
195
196 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
197 intelhaddata->stream_info.substream_refcount--;
198 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
199}
200
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +0300201static u32 had_config_offset(int pipe)
Takashi Iwai28ed1252017-02-15 22:02:10 +0100202{
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +0300203 switch (pipe) {
204 default:
205 case 0:
206 return AUDIO_HDMI_CONFIG_A;
207 case 1:
208 return AUDIO_HDMI_CONFIG_B;
209 case 2:
210 return AUDIO_HDMI_CONFIG_C;
211 }
Takashi Iwai28ed1252017-02-15 22:02:10 +0100212}
213
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +0300214/* Register access functions */
215static u32 had_read_register_raw(struct snd_intelhad_card *card_ctx,
216 int pipe, u32 reg)
Takashi Iwai28ed1252017-02-15 22:02:10 +0100217{
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +0300218 return ioread32(card_ctx->mmio_start + had_config_offset(pipe) + reg);
219}
220
221static void had_write_register_raw(struct snd_intelhad_card *card_ctx,
222 int pipe, u32 reg, u32 val)
223{
224 iowrite32(val, card_ctx->mmio_start + had_config_offset(pipe) + reg);
Takashi Iwai28ed1252017-02-15 22:02:10 +0100225}
226
Takashi Iwai83af57d2017-02-03 08:50:06 +0100227static void had_read_register(struct snd_intelhad *ctx, u32 reg, u32 *val)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530228{
Takashi Iwai28ed1252017-02-15 22:02:10 +0100229 if (!ctx->connected)
230 *val = 0;
231 else
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +0300232 *val = had_read_register_raw(ctx->card_ctx, ctx->pipe, reg);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530233}
234
Takashi Iwai83af57d2017-02-03 08:50:06 +0100235static void had_write_register(struct snd_intelhad *ctx, u32 reg, u32 val)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530236{
Takashi Iwai28ed1252017-02-15 22:02:10 +0100237 if (ctx->connected)
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +0300238 had_write_register_raw(ctx->card_ctx, ctx->pipe, reg, val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530239}
240
Takashi Iwaida864802017-01-31 13:52:22 +0100241/*
Takashi Iwai313d9f22017-02-02 13:00:12 +0100242 * enable / disable audio configuration
243 *
Takashi Iwai83af57d2017-02-03 08:50:06 +0100244 * The normal read/modify should not directly be used on VLV2 for
Takashi Iwaida864802017-01-31 13:52:22 +0100245 * updating AUD_CONFIG register.
Jerome Anand5dab11d2017-01-25 04:27:52 +0530246 * This is because:
247 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
248 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
249 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
250 * register. This field should be 1xy binary for configuration with 6 or
251 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
252 * causes the "channels" field to be updated as 0xy binary resulting in
253 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
254 * appropriate value when doing read-modify of AUD_CONFIG register.
Jerome Anand5dab11d2017-01-25 04:27:52 +0530255 */
Takashi Iwai40ce4b52017-02-07 16:17:06 +0100256static void had_enable_audio(struct snd_intelhad *intelhaddata,
Takashi Iwaib5562902017-02-04 22:05:33 +0100257 bool enable)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530258{
Takashi Iwai40ce4b52017-02-07 16:17:06 +0100259 /* update the cached value */
260 intelhaddata->aud_config.regx.aud_en = enable;
261 had_write_register(intelhaddata, AUD_CONFIG,
262 intelhaddata->aud_config.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530263}
264
Takashi Iwai075a1d42017-02-07 07:55:27 +0100265/* forcibly ACKs to both BUFFER_DONE and BUFFER_UNDERRUN interrupts */
266static void had_ack_irqs(struct snd_intelhad *ctx)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530267{
Takashi Iwaida864802017-01-31 13:52:22 +0100268 u32 status_reg;
269
Takashi Iwai28ed1252017-02-15 22:02:10 +0100270 if (!ctx->connected)
271 return;
Takashi Iwai075a1d42017-02-07 07:55:27 +0100272 had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
273 status_reg |= HDMI_AUDIO_BUFFER_DONE | HDMI_AUDIO_UNDERRUN;
274 had_write_register(ctx, AUD_HDMI_STATUS, status_reg);
275 had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
Takashi Iwaida864802017-01-31 13:52:22 +0100276}
277
Takashi Iwaif4566aa2017-02-04 21:39:56 +0100278/* Reset buffer pointers */
279static void had_reset_audio(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530280{
Takashi Iwai77531be2017-02-07 12:17:23 +0100281 had_write_register(intelhaddata, AUD_HDMI_STATUS,
282 AUD_HDMI_STATUSG_MASK_FUNCRST);
Takashi Iwaif4566aa2017-02-04 21:39:56 +0100283 had_write_register(intelhaddata, AUD_HDMI_STATUS, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530284}
285
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100286/*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530287 * initialize audio channel status registers
288 * This function is called in the prepare callback
289 */
290static int had_prog_status_reg(struct snd_pcm_substream *substream,
291 struct snd_intelhad *intelhaddata)
292{
Takashi Iwai7ceba752017-02-02 15:58:35 +0100293 union aud_ch_status_0 ch_stat0 = {.regval = 0};
294 union aud_ch_status_1 ch_stat1 = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530295
Takashi Iwai7ceba752017-02-02 15:58:35 +0100296 ch_stat0.regx.lpcm_id = (intelhaddata->aes_bits &
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100297 IEC958_AES0_NONAUDIO) >> 1;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100298 ch_stat0.regx.clk_acc = (intelhaddata->aes_bits &
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100299 IEC958_AES3_CON_CLOCK) >> 4;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530300
301 switch (substream->runtime->rate) {
302 case AUD_SAMPLE_RATE_32:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100303 ch_stat0.regx.samp_freq = CH_STATUS_MAP_32KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530304 break;
305
306 case AUD_SAMPLE_RATE_44_1:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100307 ch_stat0.regx.samp_freq = CH_STATUS_MAP_44KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530308 break;
309 case AUD_SAMPLE_RATE_48:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100310 ch_stat0.regx.samp_freq = CH_STATUS_MAP_48KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530311 break;
312 case AUD_SAMPLE_RATE_88_2:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100313 ch_stat0.regx.samp_freq = CH_STATUS_MAP_88KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530314 break;
315 case AUD_SAMPLE_RATE_96:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100316 ch_stat0.regx.samp_freq = CH_STATUS_MAP_96KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530317 break;
318 case AUD_SAMPLE_RATE_176_4:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100319 ch_stat0.regx.samp_freq = CH_STATUS_MAP_176KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530320 break;
321 case AUD_SAMPLE_RATE_192:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100322 ch_stat0.regx.samp_freq = CH_STATUS_MAP_192KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530323 break;
324
325 default:
326 /* control should never come here */
327 return -EINVAL;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530328 }
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100329
Takashi Iwai79dda752017-01-30 17:23:39 +0100330 had_write_register(intelhaddata,
Takashi Iwai7ceba752017-02-02 15:58:35 +0100331 AUD_CH_STATUS_0, ch_stat0.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530332
Takashi Iwai85bd8742017-02-07 13:33:17 +0100333 switch (substream->runtime->format) {
Takashi Iwai85bd8742017-02-07 13:33:17 +0100334 case SNDRV_PCM_FORMAT_S16_LE:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100335 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_20;
336 ch_stat1.regx.wrd_len = SMPL_WIDTH_16BITS;
Takashi Iwai85bd8742017-02-07 13:33:17 +0100337 break;
Takashi Iwai85bd8742017-02-07 13:33:17 +0100338 case SNDRV_PCM_FORMAT_S24_LE:
339 case SNDRV_PCM_FORMAT_S32_LE:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100340 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_24;
341 ch_stat1.regx.wrd_len = SMPL_WIDTH_24BITS;
Takashi Iwai85bd8742017-02-07 13:33:17 +0100342 break;
343 default:
344 return -EINVAL;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530345 }
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100346
Takashi Iwai79dda752017-01-30 17:23:39 +0100347 had_write_register(intelhaddata,
Takashi Iwai7ceba752017-02-02 15:58:35 +0100348 AUD_CH_STATUS_1, ch_stat1.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530349 return 0;
350}
351
Takashi Iwai76296ef2017-01-30 16:09:11 +0100352/*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530353 * function to initialize audio
354 * registers and buffer confgiuration registers
355 * This function is called in the prepare callback
356 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100357static int had_init_audio_ctrl(struct snd_pcm_substream *substream,
358 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530359{
Takashi Iwai7ceba752017-02-02 15:58:35 +0100360 union aud_cfg cfg_val = {.regval = 0};
361 union aud_buf_config buf_cfg = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530362 u8 channels;
363
364 had_prog_status_reg(substream, intelhaddata);
365
Takashi Iwai7ceba752017-02-02 15:58:35 +0100366 buf_cfg.regx.audio_fifo_watermark = FIFO_THRESHOLD;
367 buf_cfg.regx.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
368 buf_cfg.regx.aud_delay = 0;
369 had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530370
371 channels = substream->runtime->channels;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100372 cfg_val.regx.num_ch = channels - 2;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530373 if (channels <= 2)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100374 cfg_val.regx.layout = LAYOUT0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530375 else
Takashi Iwai7ceba752017-02-02 15:58:35 +0100376 cfg_val.regx.layout = LAYOUT1;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530377
Takashi Iwai3fe2cf72017-02-07 13:53:42 +0100378 if (substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE)
379 cfg_val.regx.packet_mode = 1;
380
Takashi Iwai85bd8742017-02-07 13:33:17 +0100381 if (substream->runtime->format == SNDRV_PCM_FORMAT_S32_LE)
382 cfg_val.regx.left_align = 1;
383
Takashi Iwai7ceba752017-02-02 15:58:35 +0100384 cfg_val.regx.val_bit = 1;
Takashi Iwai83af57d2017-02-03 08:50:06 +0100385
386 /* fix up the DP bits */
387 if (intelhaddata->dp_output) {
388 cfg_val.regx.dp_modei = 1;
389 cfg_val.regx.set = 1;
390 }
391
Takashi Iwai7ceba752017-02-02 15:58:35 +0100392 had_write_register(intelhaddata, AUD_CONFIG, cfg_val.regval);
Takashi Iwai40ce4b52017-02-07 16:17:06 +0100393 intelhaddata->aud_config = cfg_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530394 return 0;
395}
396
Jerome Anand5dab11d2017-01-25 04:27:52 +0530397/*
398 * Compute derived values in channel_allocations[].
399 */
400static void init_channel_allocations(void)
401{
402 int i, j;
403 struct cea_channel_speaker_allocation *p;
404
Jerome Anand5dab11d2017-01-25 04:27:52 +0530405 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
406 p = channel_allocations + i;
407 p->channels = 0;
408 p->spk_mask = 0;
409 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
410 if (p->speakers[j]) {
411 p->channels++;
412 p->spk_mask |= p->speakers[j];
413 }
414 }
415}
416
417/*
418 * The transformation takes two steps:
419 *
420 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
421 * spk_mask => (channel_allocations[]) => ai->CA
422 *
423 * TODO: it could select the wrong CA from multiple candidates.
424 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100425static int had_channel_allocation(struct snd_intelhad *intelhaddata,
426 int channels)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530427{
428 int i;
429 int ca = 0;
430 int spk_mask = 0;
431
432 /*
433 * CA defaults to 0 for basic stereo audio
434 */
435 if (channels <= 2)
436 return 0;
437
438 /*
439 * expand ELD's speaker allocation mask
440 *
441 * ELD tells the speaker mask in a compact(paired) form,
442 * expand ELD's notions to match the ones used by Audio InfoFrame.
443 */
444
445 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100446 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530447 spk_mask |= eld_speaker_allocation_bits[i];
448 }
449
450 /* search for the first working match in the CA table */
451 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
452 if (channels == channel_allocations[i].channels &&
453 (spk_mask & channel_allocations[i].spk_mask) ==
454 channel_allocations[i].spk_mask) {
455 ca = channel_allocations[i].ca_index;
456 break;
457 }
458 }
459
Takashi Iwaic75b0472017-01-31 15:49:15 +0100460 dev_dbg(intelhaddata->dev, "select CA 0x%x for %d\n", ca, channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530461
462 return ca;
463}
464
465/* from speaker bit mask to ALSA API channel position */
466static int spk_to_chmap(int spk)
467{
Takashi Iwai4a5ddb22017-02-01 16:45:38 +0100468 const struct channel_map_table *t = map_tables;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530469
470 for (; t->map; t++) {
471 if (t->spk_mask == spk)
472 return t->map;
473 }
474 return 0;
475}
476
Takashi Iwai372d8552017-01-31 13:57:58 +0100477static void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530478{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100479 int i, c;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530480 int spk_mask = 0;
481 struct snd_pcm_chmap_elem *chmap;
482 u8 eld_high, eld_high_mask = 0xF0;
483 u8 high_msb;
484
Takashi Iwai18353192017-02-15 21:42:20 +0100485 kfree(intelhaddata->chmap->chmap);
486 intelhaddata->chmap->chmap = NULL;
487
Jerome Anand5dab11d2017-01-25 04:27:52 +0530488 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
Takashi Iwai18353192017-02-15 21:42:20 +0100489 if (!chmap)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530490 return;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530491
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100492 dev_dbg(intelhaddata->dev, "eld speaker = %x\n",
493 intelhaddata->eld[DRM_ELD_SPEAKER]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530494
495 /* WA: Fix the max channel supported to 8 */
496
497 /*
498 * Sink may support more than 8 channels, if eld_high has more than
499 * one bit set. SOC supports max 8 channels.
500 * Refer eld_speaker_allocation_bits, for sink speaker allocation
501 */
502
503 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100504 eld_high = intelhaddata->eld[DRM_ELD_SPEAKER] & eld_high_mask;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530505 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
506 /* eld_high & (eld_high-1): if more than 1 bit set */
507 /* 0x1F: 7 channels */
508 for (i = 1; i < 4; i++) {
509 high_msb = eld_high & (0x80 >> i);
510 if (high_msb) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100511 intelhaddata->eld[DRM_ELD_SPEAKER] &=
Jerome Anand5dab11d2017-01-25 04:27:52 +0530512 high_msb | 0xF;
513 break;
514 }
515 }
516 }
517
518 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100519 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530520 spk_mask |= eld_speaker_allocation_bits[i];
521 }
522
523 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
524 if (spk_mask == channel_allocations[i].spk_mask) {
525 for (c = 0; c < channel_allocations[i].channels; c++) {
526 chmap->map[c] = spk_to_chmap(
527 channel_allocations[i].speakers[
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100528 (MAX_SPEAKERS - 1) - c]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530529 }
530 chmap->channels = channel_allocations[i].channels;
531 intelhaddata->chmap->chmap = chmap;
532 break;
533 }
534 }
Takashi Iwai18353192017-02-15 21:42:20 +0100535 if (i >= ARRAY_SIZE(channel_allocations))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530536 kfree(chmap);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530537}
538
539/*
540 * ALSA API channel-map control callbacks
541 */
542static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
543 struct snd_ctl_elem_info *uinfo)
544{
Jerome Anand5dab11d2017-01-25 04:27:52 +0530545 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
546 uinfo->count = HAD_MAX_CHANNEL;
547 uinfo->value.integer.min = 0;
548 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
549 return 0;
550}
551
552static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
553 struct snd_ctl_elem_value *ucontrol)
554{
555 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
556 struct snd_intelhad *intelhaddata = info->private_data;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100557 int i;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530558 const struct snd_pcm_chmap_elem *chmap;
559
Takashi Iwaia72ccfb2017-02-15 21:45:06 +0100560 memset(ucontrol->value.integer.value, 0,
561 sizeof(long) * HAD_MAX_CHANNEL);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100562 mutex_lock(&intelhaddata->mutex);
563 if (!intelhaddata->chmap->chmap) {
564 mutex_unlock(&intelhaddata->mutex);
Takashi Iwaia72ccfb2017-02-15 21:45:06 +0100565 return 0;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100566 }
567
Jerome Anand5dab11d2017-01-25 04:27:52 +0530568 chmap = intelhaddata->chmap->chmap;
Takashi Iwaic75b0472017-01-31 15:49:15 +0100569 for (i = 0; i < chmap->channels; i++)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530570 ucontrol->value.integer.value[i] = chmap->map[i];
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100571 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530572
573 return 0;
574}
575
576static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
577 struct snd_pcm *pcm)
578{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100579 int err;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530580
581 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
582 NULL, 0, (unsigned long)intelhaddata,
583 &intelhaddata->chmap);
584 if (err < 0)
585 return err;
586
587 intelhaddata->chmap->private_data = intelhaddata;
Takashi Iwaie9d65ab2017-01-31 16:11:27 +0100588 intelhaddata->chmap->kctl->info = had_chmap_ctl_info;
589 intelhaddata->chmap->kctl->get = had_chmap_ctl_get;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530590 intelhaddata->chmap->chmap = NULL;
591 return 0;
592}
593
Takashi Iwai76296ef2017-01-30 16:09:11 +0100594/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100595 * Initialize Data Island Packets registers
Jerome Anand5dab11d2017-01-25 04:27:52 +0530596 * This function is called in the prepare callback
597 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100598static void had_prog_dip(struct snd_pcm_substream *substream,
599 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530600{
601 int i;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100602 union aud_ctrl_st ctrl_state = {.regval = 0};
603 union aud_info_frame2 frame2 = {.regval = 0};
604 union aud_info_frame3 frame3 = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530605 u8 checksum = 0;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600606 u32 info_frame;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530607 int channels;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100608 int ca;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530609
610 channels = substream->runtime->channels;
611
Takashi Iwai7ceba752017-02-02 15:58:35 +0100612 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530613
Takashi Iwaib5562902017-02-04 22:05:33 +0100614 ca = had_channel_allocation(intelhaddata, channels);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600615 if (intelhaddata->dp_output) {
616 info_frame = DP_INFO_FRAME_WORD1;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100617 frame2.regval = (substream->runtime->channels - 1) | (ca << 24);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600618 } else {
619 info_frame = HDMI_INFO_FRAME_WORD1;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100620 frame2.regx.chnl_cnt = substream->runtime->channels - 1;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100621 frame3.regx.chnl_alloc = ca;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530622
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100623 /* Calculte the byte wide checksum for all valid DIP words */
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600624 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100625 checksum += (info_frame >> (i * 8)) & 0xff;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600626 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100627 checksum += (frame2.regval >> (i * 8)) & 0xff;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600628 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100629 checksum += (frame3.regval >> (i * 8)) & 0xff;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530630
Takashi Iwai7ceba752017-02-02 15:58:35 +0100631 frame2.regx.chksum = -(checksum);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600632 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530633
Takashi Iwai4151ee82017-01-31 18:14:15 +0100634 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, info_frame);
Takashi Iwai7ceba752017-02-02 15:58:35 +0100635 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame2.regval);
636 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame3.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530637
638 /* program remaining DIP words with zero */
639 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
Takashi Iwai4151ee82017-01-31 18:14:15 +0100640 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, 0x0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530641
Takashi Iwai7ceba752017-02-02 15:58:35 +0100642 ctrl_state.regx.dip_freq = 1;
643 ctrl_state.regx.dip_en_sta = 1;
644 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530645}
646
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600647static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
648{
649 u32 maud_val;
650
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100651 /* Select maud according to DP 1.2 spec */
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600652 if (link_rate == DP_2_7_GHZ) {
653 switch (aud_samp_freq) {
654 case AUD_SAMPLE_RATE_32:
655 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
656 break;
657
658 case AUD_SAMPLE_RATE_44_1:
659 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
660 break;
661
662 case AUD_SAMPLE_RATE_48:
663 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
664 break;
665
666 case AUD_SAMPLE_RATE_88_2:
667 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
668 break;
669
670 case AUD_SAMPLE_RATE_96:
671 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
672 break;
673
674 case AUD_SAMPLE_RATE_176_4:
675 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
676 break;
677
678 case HAD_MAX_RATE:
679 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
680 break;
681
682 default:
683 maud_val = -EINVAL;
684 break;
685 }
686 } else if (link_rate == DP_1_62_GHZ) {
687 switch (aud_samp_freq) {
688 case AUD_SAMPLE_RATE_32:
689 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
690 break;
691
692 case AUD_SAMPLE_RATE_44_1:
693 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
694 break;
695
696 case AUD_SAMPLE_RATE_48:
697 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
698 break;
699
700 case AUD_SAMPLE_RATE_88_2:
701 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
702 break;
703
704 case AUD_SAMPLE_RATE_96:
705 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
706 break;
707
708 case AUD_SAMPLE_RATE_176_4:
709 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
710 break;
711
712 case HAD_MAX_RATE:
713 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
714 break;
715
716 default:
717 maud_val = -EINVAL;
718 break;
719 }
720 } else
721 maud_val = -EINVAL;
722
723 return maud_val;
724}
725
Takashi Iwai76296ef2017-01-30 16:09:11 +0100726/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100727 * Program HDMI audio CTS value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530728 *
729 * @aud_samp_freq: sampling frequency of audio data
730 * @tmds: sampling frequency of the display data
Takashi Iwaib5562902017-02-04 22:05:33 +0100731 * @link_rate: DP link rate
Jerome Anand5dab11d2017-01-25 04:27:52 +0530732 * @n_param: N value, depends on aud_samp_freq
Takashi Iwaib5562902017-02-04 22:05:33 +0100733 * @intelhaddata: substream private data
Jerome Anand5dab11d2017-01-25 04:27:52 +0530734 *
735 * Program CTS register based on the audio and display sampling frequency
736 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100737static void had_prog_cts(u32 aud_samp_freq, u32 tmds, u32 link_rate,
738 u32 n_param, struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530739{
740 u32 cts_val;
741 u64 dividend, divisor;
742
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600743 if (intelhaddata->dp_output) {
744 /* Substitute cts_val with Maud according to DP 1.2 spec*/
745 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
746 } else {
747 /* Calculate CTS according to HDMI 1.3a spec*/
748 dividend = (u64)tmds * n_param*1000;
749 divisor = 128 * aud_samp_freq;
750 cts_val = div64_u64(dividend, divisor);
751 }
Takashi Iwaic75b0472017-01-31 15:49:15 +0100752 dev_dbg(intelhaddata->dev, "TMDS value=%d, N value=%d, CTS Value=%d\n",
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600753 tmds, n_param, cts_val);
Takashi Iwai79dda752017-01-30 17:23:39 +0100754 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530755}
756
757static int had_calculate_n_value(u32 aud_samp_freq)
758{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100759 int n_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530760
761 /* Select N according to HDMI 1.3a spec*/
762 switch (aud_samp_freq) {
763 case AUD_SAMPLE_RATE_32:
764 n_val = 4096;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100765 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530766
767 case AUD_SAMPLE_RATE_44_1:
768 n_val = 6272;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100769 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530770
771 case AUD_SAMPLE_RATE_48:
772 n_val = 6144;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100773 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530774
775 case AUD_SAMPLE_RATE_88_2:
776 n_val = 12544;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100777 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530778
779 case AUD_SAMPLE_RATE_96:
780 n_val = 12288;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100781 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530782
783 case AUD_SAMPLE_RATE_176_4:
784 n_val = 25088;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100785 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530786
787 case HAD_MAX_RATE:
788 n_val = 24576;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100789 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530790
791 default:
792 n_val = -EINVAL;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100793 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530794 }
795 return n_val;
796}
797
Takashi Iwai76296ef2017-01-30 16:09:11 +0100798/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100799 * Program HDMI audio N value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530800 *
801 * @aud_samp_freq: sampling frequency of audio data
802 * @n_param: N value, depends on aud_samp_freq
Takashi Iwaib5562902017-02-04 22:05:33 +0100803 * @intelhaddata: substream private data
Jerome Anand5dab11d2017-01-25 04:27:52 +0530804 *
805 * This function is called in the prepare callback.
806 * It programs based on the audio and display sampling frequency
807 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100808static int had_prog_n(u32 aud_samp_freq, u32 *n_param,
809 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530810{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100811 int n_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530812
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600813 if (intelhaddata->dp_output) {
814 /*
815 * According to DP specs, Maud and Naud values hold
816 * a relationship, which is stated as:
817 * Maud/Naud = 512 * fs / f_LS_Clk
818 * where, fs is the sampling frequency of the audio stream
819 * and Naud is 32768 for Async clock.
820 */
821
822 n_val = DP_NAUD_VAL;
823 } else
824 n_val = had_calculate_n_value(aud_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530825
826 if (n_val < 0)
827 return n_val;
828
Takashi Iwai79dda752017-01-30 17:23:39 +0100829 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530830 *n_param = n_val;
831 return 0;
832}
833
Takashi Iwaie1b239f32017-02-03 00:01:18 +0100834/*
835 * PCM ring buffer handling
836 *
837 * The hardware provides a ring buffer with the fixed 4 buffer descriptors
838 * (BDs). The driver maps these 4 BDs onto the PCM ring buffer. The mapping
839 * moves at each period elapsed. The below illustrates how it works:
840 *
841 * At time=0
842 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
843 * BD | 0 | 1 | 2 | 3 |
844 *
845 * At time=1 (period elapsed)
846 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
847 * BD | 1 | 2 | 3 | 0 |
848 *
849 * At time=2 (second period elapsed)
850 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
851 * BD | 2 | 3 | 0 | 1 |
852 *
853 * The bd_head field points to the index of the BD to be read. It's also the
854 * position to be filled at next. The pcm_head and the pcm_filled fields
855 * point to the indices of the current position and of the next position to
856 * be filled, respectively. For PCM buffer there are both _head and _filled
857 * because they may be difference when nperiods > 4. For example, in the
858 * example above at t=1, bd_head=1 and pcm_head=1 while pcm_filled=5:
859 *
860 * pcm_head (=1) --v v-- pcm_filled (=5)
861 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
862 * BD | 1 | 2 | 3 | 0 |
863 * bd_head (=1) --^ ^-- next to fill (= bd_head)
864 *
865 * For nperiods < 4, the remaining BDs out of 4 are marked as invalid, so that
866 * the hardware skips those BDs in the loop.
Takashi Iwai8d48c012017-02-07 08:05:46 +0100867 *
868 * An exceptional setup is the case with nperiods=1. Since we have to update
869 * BDs after finishing one BD processing, we'd need at least two BDs, where
870 * both BDs point to the same content, the same address, the same size of the
871 * whole PCM buffer.
Takashi Iwaie1b239f32017-02-03 00:01:18 +0100872 */
873
874#define AUD_BUF_ADDR(x) (AUD_BUF_A_ADDR + (x) * HAD_REG_WIDTH)
875#define AUD_BUF_LEN(x) (AUD_BUF_A_LENGTH + (x) * HAD_REG_WIDTH)
876
877/* Set up a buffer descriptor at the "filled" position */
878static void had_prog_bd(struct snd_pcm_substream *substream,
879 struct snd_intelhad *intelhaddata)
880{
881 int idx = intelhaddata->bd_head;
882 int ofs = intelhaddata->pcmbuf_filled * intelhaddata->period_bytes;
883 u32 addr = substream->runtime->dma_addr + ofs;
884
Takashi Iwaie8de9852017-02-07 08:09:12 +0100885 addr |= AUD_BUF_VALID;
886 if (!substream->runtime->no_period_wakeup)
887 addr |= AUD_BUF_INTR_EN;
Takashi Iwaie1b239f32017-02-03 00:01:18 +0100888 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), addr);
889 had_write_register(intelhaddata, AUD_BUF_LEN(idx),
890 intelhaddata->period_bytes);
891
892 /* advance the indices to the next */
893 intelhaddata->bd_head++;
894 intelhaddata->bd_head %= intelhaddata->num_bds;
895 intelhaddata->pcmbuf_filled++;
896 intelhaddata->pcmbuf_filled %= substream->runtime->periods;
897}
898
899/* invalidate a buffer descriptor with the given index */
900static void had_invalidate_bd(struct snd_intelhad *intelhaddata,
901 int idx)
902{
903 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), 0);
904 had_write_register(intelhaddata, AUD_BUF_LEN(idx), 0);
905}
906
907/* Initial programming of ring buffer */
908static void had_init_ringbuf(struct snd_pcm_substream *substream,
909 struct snd_intelhad *intelhaddata)
910{
911 struct snd_pcm_runtime *runtime = substream->runtime;
912 int i, num_periods;
913
914 num_periods = runtime->periods;
915 intelhaddata->num_bds = min(num_periods, HAD_NUM_OF_RING_BUFS);
Takashi Iwai8d48c012017-02-07 08:05:46 +0100916 /* set the minimum 2 BDs for num_periods=1 */
917 intelhaddata->num_bds = max(intelhaddata->num_bds, 2U);
Takashi Iwaie1b239f32017-02-03 00:01:18 +0100918 intelhaddata->period_bytes =
919 frames_to_bytes(runtime, runtime->period_size);
920 WARN_ON(intelhaddata->period_bytes & 0x3f);
921
922 intelhaddata->bd_head = 0;
923 intelhaddata->pcmbuf_head = 0;
924 intelhaddata->pcmbuf_filled = 0;
925
926 for (i = 0; i < HAD_NUM_OF_RING_BUFS; i++) {
Takashi Iwai8d48c012017-02-07 08:05:46 +0100927 if (i < intelhaddata->num_bds)
Takashi Iwaie1b239f32017-02-03 00:01:18 +0100928 had_prog_bd(substream, intelhaddata);
929 else /* invalidate the rest */
930 had_invalidate_bd(intelhaddata, i);
931 }
932
933 intelhaddata->bd_head = 0; /* reset at head again before starting */
934}
935
936/* process a bd, advance to the next */
937static void had_advance_ringbuf(struct snd_pcm_substream *substream,
938 struct snd_intelhad *intelhaddata)
939{
940 int num_periods = substream->runtime->periods;
941
942 /* reprogram the next buffer */
943 had_prog_bd(substream, intelhaddata);
944
945 /* proceed to next */
946 intelhaddata->pcmbuf_head++;
947 intelhaddata->pcmbuf_head %= num_periods;
948}
949
950/* process the current BD(s);
951 * returns the current PCM buffer byte position, or -EPIPE for underrun.
952 */
953static int had_process_ringbuf(struct snd_pcm_substream *substream,
954 struct snd_intelhad *intelhaddata)
955{
956 int len, processed;
957 unsigned long flags;
958
959 processed = 0;
960 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
961 for (;;) {
962 /* get the remaining bytes on the buffer */
963 had_read_register(intelhaddata,
964 AUD_BUF_LEN(intelhaddata->bd_head),
965 &len);
966 if (len < 0 || len > intelhaddata->period_bytes) {
967 dev_dbg(intelhaddata->dev, "Invalid buf length %d\n",
968 len);
969 len = -EPIPE;
970 goto out;
971 }
972
973 if (len > 0) /* OK, this is the current buffer */
974 break;
975
976 /* len=0 => already empty, check the next buffer */
977 if (++processed >= intelhaddata->num_bds) {
978 len = -EPIPE; /* all empty? - report underrun */
979 goto out;
980 }
981 had_advance_ringbuf(substream, intelhaddata);
982 }
983
984 len = intelhaddata->period_bytes - len;
985 len += intelhaddata->period_bytes * intelhaddata->pcmbuf_head;
986 out:
987 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
988 return len;
989}
990
991/* called from irq handler */
992static void had_process_buffer_done(struct snd_intelhad *intelhaddata)
993{
994 struct snd_pcm_substream *substream;
995
Takashi Iwaie1b239f32017-02-03 00:01:18 +0100996 substream = had_substream_get(intelhaddata);
997 if (!substream)
998 return; /* no stream? - bail out */
999
Takashi Iwaibe9a2e92017-02-15 22:05:34 +01001000 if (!intelhaddata->connected) {
1001 snd_pcm_stop_xrun(substream);
1002 goto out; /* disconnected? - bail out */
1003 }
1004
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001005 /* process or stop the stream */
1006 if (had_process_ringbuf(substream, intelhaddata) < 0)
1007 snd_pcm_stop_xrun(substream);
1008 else
1009 snd_pcm_period_elapsed(substream);
1010
Takashi Iwaibe9a2e92017-02-15 22:05:34 +01001011 out:
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001012 had_substream_put(intelhaddata);
1013}
1014
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001015/*
1016 * The interrupt status 'sticky' bits might not be cleared by
1017 * setting '1' to that bit once...
1018 */
1019static void wait_clear_underrun_bit(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301020{
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001021 int i;
1022 u32 val;
1023
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001024 for (i = 0; i < 100; i++) {
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001025 /* clear bit30, 31 AUD_HDMI_STATUS */
1026 had_read_register(intelhaddata, AUD_HDMI_STATUS, &val);
Takashi Iwai77531be2017-02-07 12:17:23 +01001027 if (!(val & AUD_HDMI_STATUS_MASK_UNDERRUN))
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001028 return;
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001029 udelay(100);
1030 cond_resched();
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001031 had_write_register(intelhaddata, AUD_HDMI_STATUS, val);
1032 }
1033 dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n");
1034}
1035
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001036/* Perform some reset procedure but only when need_reset is set;
1037 * this is called from prepare or hw_free callbacks once after trigger STOP
1038 * or underrun has been processed in order to settle down the h/w state.
1039 */
1040static void had_do_reset(struct snd_intelhad *intelhaddata)
1041{
Takashi Iwai28ed1252017-02-15 22:02:10 +01001042 if (!intelhaddata->need_reset || !intelhaddata->connected)
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001043 return;
1044
1045 /* Reset buffer pointers */
1046 had_reset_audio(intelhaddata);
1047 wait_clear_underrun_bit(intelhaddata);
1048 intelhaddata->need_reset = false;
1049}
1050
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001051/* called from irq handler */
1052static void had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
1053{
1054 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301055
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001056 /* Report UNDERRUN error to above layers */
1057 substream = had_substream_get(intelhaddata);
1058 if (substream) {
1059 snd_pcm_stop_xrun(substream);
1060 had_substream_put(intelhaddata);
1061 }
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001062 intelhaddata->need_reset = true;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301063}
1064
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001065/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001066 * ALSA PCM open callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301067 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001068static int had_pcm_open(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301069{
1070 struct snd_intelhad *intelhaddata;
1071 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301072 int retval;
1073
Jerome Anand5dab11d2017-01-25 04:27:52 +05301074 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301075 runtime = substream->runtime;
1076
Takashi Iwai182cdf22017-02-02 14:43:39 +01001077 pm_runtime_get_sync(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301078
Jerome Anand5dab11d2017-01-25 04:27:52 +05301079 /* set the runtime hw parameter with local snd_pcm_hardware struct */
Takashi Iwaib5562902017-02-04 22:05:33 +01001080 runtime->hw = had_pcm_hardware;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301081
Jerome Anand5dab11d2017-01-25 04:27:52 +05301082 retval = snd_pcm_hw_constraint_integer(runtime,
1083 SNDRV_PCM_HW_PARAM_PERIODS);
1084 if (retval < 0)
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001085 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301086
1087 /* Make sure, that the period size is always aligned
1088 * 64byte boundary
1089 */
1090 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1091 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
Takashi Iwai73997b02017-02-02 17:38:50 +01001092 if (retval < 0)
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001093 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301094
Takashi Iwai85bd8742017-02-07 13:33:17 +01001095 retval = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1096 if (retval < 0)
1097 goto error;
1098
Takashi Iwai73997b02017-02-02 17:38:50 +01001099 /* expose PCM substream */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001100 spin_lock_irq(&intelhaddata->had_spinlock);
1101 intelhaddata->stream_info.substream = substream;
1102 intelhaddata->stream_info.substream_refcount++;
1103 spin_unlock_irq(&intelhaddata->had_spinlock);
1104
Jerome Anand5dab11d2017-01-25 04:27:52 +05301105 return retval;
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001106 error:
Takashi Iwai3002b9f2017-02-13 09:52:44 +01001107 pm_runtime_mark_last_busy(intelhaddata->dev);
1108 pm_runtime_put_autosuspend(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301109 return retval;
1110}
1111
Takashi Iwaidf76df12017-01-31 16:04:10 +01001112/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001113 * ALSA PCM close callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301114 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001115static int had_pcm_close(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301116{
1117 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301118
Jerome Anand5dab11d2017-01-25 04:27:52 +05301119 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301120
Takashi Iwai73997b02017-02-02 17:38:50 +01001121 /* unreference and sync with the pending PCM accesses */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001122 spin_lock_irq(&intelhaddata->had_spinlock);
1123 intelhaddata->stream_info.substream = NULL;
1124 intelhaddata->stream_info.substream_refcount--;
1125 while (intelhaddata->stream_info.substream_refcount > 0) {
1126 spin_unlock_irq(&intelhaddata->had_spinlock);
1127 cpu_relax();
1128 spin_lock_irq(&intelhaddata->had_spinlock);
1129 }
1130 spin_unlock_irq(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301131
Takashi Iwai3002b9f2017-02-13 09:52:44 +01001132 pm_runtime_mark_last_busy(intelhaddata->dev);
1133 pm_runtime_put_autosuspend(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301134 return 0;
1135}
1136
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001137/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001138 * ALSA PCM hw_params callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301139 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001140static int had_pcm_hw_params(struct snd_pcm_substream *substream,
1141 struct snd_pcm_hw_params *hw_params)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301142{
Takashi Iwaic75b0472017-01-31 15:49:15 +01001143 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301144 unsigned long addr;
1145 int pages, buf_size, retval;
1146
Takashi Iwaic75b0472017-01-31 15:49:15 +01001147 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301148 buf_size = params_buffer_bytes(hw_params);
1149 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1150 if (retval < 0)
1151 return retval;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001152 dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
1153 __func__, buf_size);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301154 /* mark the pages as uncached region */
1155 addr = (unsigned long) substream->runtime->dma_area;
1156 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1157 retval = set_memory_uc(addr, pages);
1158 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001159 dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
1160 retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301161 return retval;
1162 }
1163 memset(substream->runtime->dma_area, 0, buf_size);
1164
1165 return retval;
1166}
1167
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001168/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001169 * ALSA PCM hw_free callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301170 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001171static int had_pcm_hw_free(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301172{
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001173 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301174 unsigned long addr;
1175 u32 pages;
1176
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001177 intelhaddata = snd_pcm_substream_chip(substream);
1178 had_do_reset(intelhaddata);
1179
Jerome Anand5dab11d2017-01-25 04:27:52 +05301180 /* mark back the pages as cached/writeback region before the free */
1181 if (substream->runtime->dma_area != NULL) {
1182 addr = (unsigned long) substream->runtime->dma_area;
1183 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1184 PAGE_SIZE;
1185 set_memory_wb(addr, pages);
1186 return snd_pcm_lib_free_pages(substream);
1187 }
1188 return 0;
1189}
1190
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001191/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001192 * ALSA PCM trigger callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301193 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001194static int had_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301195{
Takashi Iwaida864802017-01-31 13:52:22 +01001196 int retval = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301197 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301198
Jerome Anand5dab11d2017-01-25 04:27:52 +05301199 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301200
Takashi Iwaidf42cb42017-02-12 11:35:44 +01001201 spin_lock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301202 switch (cmd) {
1203 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001204 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1205 case SNDRV_PCM_TRIGGER_RESUME:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301206 /* Enable Audio */
Takashi Iwai075a1d42017-02-07 07:55:27 +01001207 had_ack_irqs(intelhaddata); /* FIXME: do we need this? */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001208 had_enable_audio(intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301209 break;
1210
1211 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001212 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301213 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001214 had_enable_audio(intelhaddata, false);
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001215 intelhaddata->need_reset = true;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301216 break;
1217
1218 default:
1219 retval = -EINVAL;
1220 }
Takashi Iwaidf42cb42017-02-12 11:35:44 +01001221 spin_unlock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301222 return retval;
1223}
1224
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001225/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001226 * ALSA PCM prepare callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301227 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001228static int had_pcm_prepare(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301229{
1230 int retval;
1231 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001232 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301233 struct snd_intelhad *intelhaddata;
1234 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301235
Jerome Anand5dab11d2017-01-25 04:27:52 +05301236 intelhaddata = snd_pcm_substream_chip(substream);
1237 runtime = substream->runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301238
Takashi Iwaic75b0472017-01-31 15:49:15 +01001239 dev_dbg(intelhaddata->dev, "period_size=%d\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301240 (int)frames_to_bytes(runtime, runtime->period_size));
Takashi Iwaic75b0472017-01-31 15:49:15 +01001241 dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods);
1242 dev_dbg(intelhaddata->dev, "buffer_size=%d\n",
1243 (int)snd_pcm_lib_buffer_bytes(substream));
1244 dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
1245 dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301246
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001247 had_do_reset(intelhaddata);
1248
Jerome Anand5dab11d2017-01-25 04:27:52 +05301249 /* Get N value in KHz */
Takashi Iwaida864802017-01-31 13:52:22 +01001250 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301251
Takashi Iwaib5562902017-02-04 22:05:33 +01001252 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301253 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001254 dev_err(intelhaddata->dev,
1255 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301256 goto prep_end;
1257 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001258
1259 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001260 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001261
Takashi Iwaib5562902017-02-04 22:05:33 +01001262 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1263 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301264
Takashi Iwaib5562902017-02-04 22:05:33 +01001265 had_prog_dip(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301266
Takashi Iwaib5562902017-02-04 22:05:33 +01001267 retval = had_init_audio_ctrl(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301268
1269 /* Prog buffer address */
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001270 had_init_ringbuf(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301271
1272 /*
1273 * Program channel mapping in following order:
1274 * FL, FR, C, LFE, RL, RR
1275 */
1276
Takashi Iwai79dda752017-01-30 17:23:39 +01001277 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301278
1279prep_end:
1280 return retval;
1281}
1282
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001283/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001284 * ALSA PCM pointer callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301285 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001286static snd_pcm_uframes_t had_pcm_pointer(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301287{
1288 struct snd_intelhad *intelhaddata;
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001289 int len;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301290
Jerome Anand5dab11d2017-01-25 04:27:52 +05301291 intelhaddata = snd_pcm_substream_chip(substream);
1292
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001293 if (!intelhaddata->connected)
Takashi Iwai79f439e2017-01-31 16:46:44 +01001294 return SNDRV_PCM_POS_XRUN;
1295
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001296 len = had_process_ringbuf(substream, intelhaddata);
1297 if (len < 0)
1298 return SNDRV_PCM_POS_XRUN;
Takashi Iwai8d48c012017-02-07 08:05:46 +01001299 len = bytes_to_frames(substream->runtime, len);
1300 /* wrapping may happen when periods=1 */
1301 len %= substream->runtime->buffer_size;
1302 return len;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301303}
1304
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001305/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001306 * ALSA PCM mmap callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301307 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001308static int had_pcm_mmap(struct snd_pcm_substream *substream,
1309 struct vm_area_struct *vma)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301310{
Jerome Anand5dab11d2017-01-25 04:27:52 +05301311 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1312 return remap_pfn_range(vma, vma->vm_start,
1313 substream->dma_buffer.addr >> PAGE_SHIFT,
1314 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1315}
1316
Takashi Iwai73997b02017-02-02 17:38:50 +01001317/*
1318 * ALSA PCM ops
1319 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001320static const struct snd_pcm_ops had_pcm_ops = {
1321 .open = had_pcm_open,
1322 .close = had_pcm_close,
Takashi Iwai73997b02017-02-02 17:38:50 +01001323 .ioctl = snd_pcm_lib_ioctl,
Takashi Iwaib5562902017-02-04 22:05:33 +01001324 .hw_params = had_pcm_hw_params,
1325 .hw_free = had_pcm_hw_free,
1326 .prepare = had_pcm_prepare,
1327 .trigger = had_pcm_trigger,
1328 .pointer = had_pcm_pointer,
1329 .mmap = had_pcm_mmap,
Takashi Iwai73997b02017-02-02 17:38:50 +01001330};
1331
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001332/* process mode change of the running stream; called in mutex */
Takashi Iwaib5562902017-02-04 22:05:33 +01001333static int had_process_mode_change(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301334{
Takashi Iwaida864802017-01-31 13:52:22 +01001335 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301336 int retval = 0;
1337 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001338 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301339
Takashi Iwai313d9f22017-02-02 13:00:12 +01001340 substream = had_substream_get(intelhaddata);
1341 if (!substream)
Takashi Iwaida864802017-01-31 13:52:22 +01001342 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301343
1344 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001345 had_enable_audio(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301346
1347 /* Update CTS value */
Takashi Iwaida864802017-01-31 13:52:22 +01001348 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301349
Takashi Iwaib5562902017-02-04 22:05:33 +01001350 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301351 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001352 dev_err(intelhaddata->dev,
1353 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301354 goto out;
1355 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001356
1357 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001358 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001359
Takashi Iwaib5562902017-02-04 22:05:33 +01001360 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1361 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301362
1363 /* Enable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001364 had_enable_audio(intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301365
1366out:
Takashi Iwai313d9f22017-02-02 13:00:12 +01001367 had_substream_put(intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301368 return retval;
1369}
1370
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001371/* process hot plug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001372static void had_process_hot_plug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001373{
Takashi Iwai372d8552017-01-31 13:57:58 +01001374 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001375
Takashi Iwaibcce7752017-02-01 17:18:20 +01001376 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001377 if (intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001378 dev_dbg(intelhaddata->dev, "Device already connected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001379 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001380 return;
Takashi Iwai372d8552017-01-31 13:57:58 +01001381 }
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001382
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001383 /* Disable Audio */
1384 had_enable_audio(intelhaddata, false);
1385
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001386 intelhaddata->connected = true;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001387 dev_dbg(intelhaddata->dev,
1388 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001389 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001390 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001391
Takashi Iwai2d42c032017-02-15 22:08:21 +01001392 had_build_channel_allocation_map(intelhaddata);
1393
1394 /* Report to above ALSA layer */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001395 substream = had_substream_get(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001396 if (substream) {
Takashi Iwai5def9012017-02-15 21:36:38 +01001397 snd_pcm_stop_xrun(substream);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001398 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001399 }
1400
Takashi Iwaib9bacf22017-02-14 12:29:38 +01001401 snd_jack_report(intelhaddata->jack, SND_JACK_AVOUT);
Takashi Iwai372d8552017-01-31 13:57:58 +01001402}
1403
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001404/* process hot unplug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001405static void had_process_hot_unplug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001406{
Takashi Iwai313d9f22017-02-02 13:00:12 +01001407 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001408
Takashi Iwaibcce7752017-02-01 17:18:20 +01001409 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001410 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001411 dev_dbg(intelhaddata->dev, "Device already disconnected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001412 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai2d42c032017-02-15 22:08:21 +01001413 return;
Takashi Iwai372d8552017-01-31 13:57:58 +01001414
Takashi Iwai372d8552017-01-31 13:57:58 +01001415 }
1416
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001417 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001418 had_enable_audio(intelhaddata, false);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001419
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001420 intelhaddata->connected = false;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001421 dev_dbg(intelhaddata->dev,
1422 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001423 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001424 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001425
Takashi Iwai372d8552017-01-31 13:57:58 +01001426 kfree(intelhaddata->chmap->chmap);
1427 intelhaddata->chmap->chmap = NULL;
Takashi Iwai2d42c032017-02-15 22:08:21 +01001428
1429 /* Report to above ALSA layer */
1430 substream = had_substream_get(intelhaddata);
1431 if (substream) {
1432 snd_pcm_stop_xrun(substream);
1433 had_substream_put(intelhaddata);
1434 }
1435
1436 snd_jack_report(intelhaddata->jack, 0);
Takashi Iwai372d8552017-01-31 13:57:58 +01001437}
1438
Takashi Iwai73997b02017-02-02 17:38:50 +01001439/*
1440 * ALSA iec958 and ELD controls
1441 */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301442
Jerome Anand5dab11d2017-01-25 04:27:52 +05301443static int had_iec958_info(struct snd_kcontrol *kcontrol,
1444 struct snd_ctl_elem_info *uinfo)
1445{
1446 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1447 uinfo->count = 1;
1448 return 0;
1449}
1450
1451static int had_iec958_get(struct snd_kcontrol *kcontrol,
1452 struct snd_ctl_elem_value *ucontrol)
1453{
1454 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1455
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001456 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301457 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1458 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1459 ucontrol->value.iec958.status[2] =
1460 (intelhaddata->aes_bits >> 16) & 0xff;
1461 ucontrol->value.iec958.status[3] =
1462 (intelhaddata->aes_bits >> 24) & 0xff;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001463 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301464 return 0;
1465}
Takashi Iwai372d8552017-01-31 13:57:58 +01001466
Jerome Anand5dab11d2017-01-25 04:27:52 +05301467static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1468 struct snd_ctl_elem_value *ucontrol)
1469{
1470 ucontrol->value.iec958.status[0] = 0xff;
1471 ucontrol->value.iec958.status[1] = 0xff;
1472 ucontrol->value.iec958.status[2] = 0xff;
1473 ucontrol->value.iec958.status[3] = 0xff;
1474 return 0;
1475}
Takashi Iwai372d8552017-01-31 13:57:58 +01001476
Jerome Anand5dab11d2017-01-25 04:27:52 +05301477static int had_iec958_put(struct snd_kcontrol *kcontrol,
1478 struct snd_ctl_elem_value *ucontrol)
1479{
1480 unsigned int val;
1481 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001482 int changed = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301483
Jerome Anand5dab11d2017-01-25 04:27:52 +05301484 val = (ucontrol->value.iec958.status[0] << 0) |
1485 (ucontrol->value.iec958.status[1] << 8) |
1486 (ucontrol->value.iec958.status[2] << 16) |
1487 (ucontrol->value.iec958.status[3] << 24);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001488 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301489 if (intelhaddata->aes_bits != val) {
1490 intelhaddata->aes_bits = val;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001491 changed = 1;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301492 }
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001493 mutex_unlock(&intelhaddata->mutex);
1494 return changed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301495}
1496
Takashi Iwai4aedb942017-02-02 16:38:39 +01001497static int had_ctl_eld_info(struct snd_kcontrol *kcontrol,
1498 struct snd_ctl_elem_info *uinfo)
1499{
1500 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1501 uinfo->count = HDMI_MAX_ELD_BYTES;
1502 return 0;
1503}
1504
1505static int had_ctl_eld_get(struct snd_kcontrol *kcontrol,
1506 struct snd_ctl_elem_value *ucontrol)
1507{
1508 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1509
1510 mutex_lock(&intelhaddata->mutex);
1511 memcpy(ucontrol->value.bytes.data, intelhaddata->eld,
1512 HDMI_MAX_ELD_BYTES);
1513 mutex_unlock(&intelhaddata->mutex);
1514 return 0;
1515}
1516
Takashi Iwai73997b02017-02-02 17:38:50 +01001517static const struct snd_kcontrol_new had_controls[] = {
Takashi Iwai4aedb942017-02-02 16:38:39 +01001518 {
1519 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1520 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1521 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1522 .info = had_iec958_info, /* shared */
1523 .get = had_iec958_mask_get,
1524 },
1525 {
1526 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1527 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1528 .info = had_iec958_info,
1529 .get = had_iec958_get,
1530 .put = had_iec958_put,
1531 },
1532 {
1533 .access = (SNDRV_CTL_ELEM_ACCESS_READ |
1534 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1535 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1536 .name = "ELD",
1537 .info = had_ctl_eld_info,
1538 .get = had_ctl_eld_get,
1539 },
Jerome Anand5dab11d2017-01-25 04:27:52 +05301540};
1541
Takashi Iwai73997b02017-02-02 17:38:50 +01001542/*
1543 * audio interrupt handler
1544 */
Takashi Iwaida864802017-01-31 13:52:22 +01001545static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id)
1546{
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001547 struct snd_intelhad_card *card_ctx = dev_id;
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001548 u32 audio_stat[3] = {};
1549 int pipe, port;
Takashi Iwaida864802017-01-31 13:52:22 +01001550
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001551 for_each_pipe(card_ctx, pipe) {
1552 /* use raw register access to ack IRQs even while disconnected */
1553 audio_stat[pipe] = had_read_register_raw(card_ctx, pipe,
1554 AUD_HDMI_STATUS) &
1555 (HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE);
Takashi Iwaida864802017-01-31 13:52:22 +01001556
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001557 if (audio_stat[pipe])
1558 had_write_register_raw(card_ctx, pipe,
1559 AUD_HDMI_STATUS, audio_stat[pipe]);
Takashi Iwaida864802017-01-31 13:52:22 +01001560 }
1561
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001562 for_each_port(card_ctx, port) {
1563 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001564 int pipe = ctx->pipe;
Takashi Iwaida864802017-01-31 13:52:22 +01001565
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001566 if (pipe < 0)
1567 continue;
Takashi Iwaida864802017-01-31 13:52:22 +01001568
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001569 if (audio_stat[pipe] & HDMI_AUDIO_BUFFER_DONE)
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001570 had_process_buffer_done(ctx);
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001571 if (audio_stat[pipe] & HDMI_AUDIO_UNDERRUN)
1572 had_process_buffer_underrun(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001573 }
1574
1575 return IRQ_HANDLED;
1576}
1577
Takashi Iwai73997b02017-02-02 17:38:50 +01001578/*
1579 * monitor plug/unplug notification from i915; just kick off the work
1580 */
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001581static void notify_audio_lpe(struct platform_device *pdev, int port)
Takashi Iwaida864802017-01-31 13:52:22 +01001582{
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001583 struct snd_intelhad_card *card_ctx = platform_get_drvdata(pdev);
Takashi Iwai7229b122018-02-20 17:33:45 +01001584 struct snd_intelhad *ctx;
1585
1586 ctx = &card_ctx->pcm_ctx[single_port ? 0 : port];
1587 if (single_port)
1588 ctx->port = port;
Takashi Iwaida864802017-01-31 13:52:22 +01001589
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001590 schedule_work(&ctx->hdmi_audio_wq);
1591}
Takashi Iwaida864802017-01-31 13:52:22 +01001592
Takashi Iwai73997b02017-02-02 17:38:50 +01001593/* the work to handle monitor hot plug/unplug */
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001594static void had_audio_wq(struct work_struct *work)
1595{
1596 struct snd_intelhad *ctx =
1597 container_of(work, struct snd_intelhad, hdmi_audio_wq);
1598 struct intel_hdmi_lpe_audio_pdata *pdata = ctx->dev->platform_data;
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001599 struct intel_hdmi_lpe_audio_port_pdata *ppdata = &pdata->port[ctx->port];
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001600
Takashi Iwai182cdf22017-02-02 14:43:39 +01001601 pm_runtime_get_sync(ctx->dev);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001602 mutex_lock(&ctx->mutex);
Ville Syrjäläa8562e42017-04-27 19:02:27 +03001603 if (ppdata->pipe < 0) {
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001604 dev_dbg(ctx->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG : port = %d\n",
1605 __func__, ctx->port);
1606
Takashi Iwai4aedb942017-02-02 16:38:39 +01001607 memset(ctx->eld, 0, sizeof(ctx->eld)); /* clear the old ELD */
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001608
1609 ctx->dp_output = false;
1610 ctx->tmds_clock_speed = 0;
1611 ctx->link_rate = 0;
1612
1613 /* Shut down the stream */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001614 had_process_hot_unplug(ctx);
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001615
1616 ctx->pipe = -1;
Takashi Iwaida864802017-01-31 13:52:22 +01001617 } else {
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001618 dev_dbg(ctx->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001619 __func__, ctx->port, ppdata->ls_clock);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001620
Ville Syrjäläa8562e42017-04-27 19:02:27 +03001621 memcpy(ctx->eld, ppdata->eld, sizeof(ctx->eld));
Takashi Iwaida864802017-01-31 13:52:22 +01001622
Ville Syrjäläa8562e42017-04-27 19:02:27 +03001623 ctx->dp_output = ppdata->dp_output;
Ville Syrjäläc98ec5b2017-04-27 19:02:24 +03001624 if (ctx->dp_output) {
1625 ctx->tmds_clock_speed = 0;
Ville Syrjäläa8562e42017-04-27 19:02:27 +03001626 ctx->link_rate = ppdata->ls_clock;
Ville Syrjäläc98ec5b2017-04-27 19:02:24 +03001627 } else {
Ville Syrjäläa8562e42017-04-27 19:02:27 +03001628 ctx->tmds_clock_speed = ppdata->ls_clock;
Ville Syrjäläc98ec5b2017-04-27 19:02:24 +03001629 ctx->link_rate = 0;
Takashi Iwaida864802017-01-31 13:52:22 +01001630 }
1631
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001632 /*
1633 * Shut down the stream before we change
1634 * the pipe assignment for this pcm device
1635 */
Takashi Iwaida864802017-01-31 13:52:22 +01001636 had_process_hot_plug(ctx);
1637
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001638 ctx->pipe = ppdata->pipe;
1639
1640 /* Restart the stream if necessary */
Takashi Iwaib5562902017-02-04 22:05:33 +01001641 had_process_mode_change(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001642 }
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001643
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001644 mutex_unlock(&ctx->mutex);
Takashi Iwai3002b9f2017-02-13 09:52:44 +01001645 pm_runtime_mark_last_busy(ctx->dev);
1646 pm_runtime_put_autosuspend(ctx->dev);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001647}
1648
1649/*
Takashi Iwaib9bacf22017-02-14 12:29:38 +01001650 * Jack interface
1651 */
Ville Syrjäläbb4ac5a2017-04-27 19:02:28 +03001652static int had_create_jack(struct snd_intelhad *ctx,
1653 struct snd_pcm *pcm)
Takashi Iwaib9bacf22017-02-14 12:29:38 +01001654{
Ville Syrjäläbb4ac5a2017-04-27 19:02:28 +03001655 char hdmi_str[32];
Takashi Iwaib9bacf22017-02-14 12:29:38 +01001656 int err;
1657
Ville Syrjäläbb4ac5a2017-04-27 19:02:28 +03001658 snprintf(hdmi_str, sizeof(hdmi_str),
1659 "HDMI/DP,pcm=%d", pcm->device);
1660
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001661 err = snd_jack_new(ctx->card_ctx->card, hdmi_str,
1662 SND_JACK_AVOUT, &ctx->jack,
Takashi Iwaib9bacf22017-02-14 12:29:38 +01001663 true, false);
1664 if (err < 0)
1665 return err;
1666 ctx->jack->private_data = ctx;
1667 return 0;
1668}
1669
1670/*
Takashi Iwai182cdf22017-02-02 14:43:39 +01001671 * PM callbacks
1672 */
1673
1674static int hdmi_lpe_audio_runtime_suspend(struct device *dev)
1675{
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001676 struct snd_intelhad_card *card_ctx = dev_get_drvdata(dev);
1677 int port;
Takashi Iwai182cdf22017-02-02 14:43:39 +01001678
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001679 for_each_port(card_ctx, port) {
1680 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
1681 struct snd_pcm_substream *substream;
1682
1683 substream = had_substream_get(ctx);
1684 if (substream) {
1685 snd_pcm_suspend(substream);
1686 had_substream_put(ctx);
1687 }
Takashi Iwai182cdf22017-02-02 14:43:39 +01001688 }
1689
1690 return 0;
1691}
1692
Arnd Bergmann1df98922017-02-07 14:38:51 +01001693static int __maybe_unused hdmi_lpe_audio_suspend(struct device *dev)
Takashi Iwai182cdf22017-02-02 14:43:39 +01001694{
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001695 struct snd_intelhad_card *card_ctx = dev_get_drvdata(dev);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001696 int err;
1697
1698 err = hdmi_lpe_audio_runtime_suspend(dev);
1699 if (!err)
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001700 snd_power_change_state(card_ctx->card, SNDRV_CTL_POWER_D3hot);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001701 return err;
1702}
1703
Takashi Iwai3002b9f2017-02-13 09:52:44 +01001704static int hdmi_lpe_audio_runtime_resume(struct device *dev)
1705{
1706 pm_runtime_mark_last_busy(dev);
1707 return 0;
1708}
1709
Arnd Bergmann1df98922017-02-07 14:38:51 +01001710static int __maybe_unused hdmi_lpe_audio_resume(struct device *dev)
Takashi Iwai182cdf22017-02-02 14:43:39 +01001711{
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001712 struct snd_intelhad_card *card_ctx = dev_get_drvdata(dev);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001713
Takashi Iwai3002b9f2017-02-13 09:52:44 +01001714 hdmi_lpe_audio_runtime_resume(dev);
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001715 snd_power_change_state(card_ctx->card, SNDRV_CTL_POWER_D0);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001716 return 0;
Takashi Iwaida864802017-01-31 13:52:22 +01001717}
1718
1719/* release resources */
1720static void hdmi_lpe_audio_free(struct snd_card *card)
1721{
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001722 struct snd_intelhad_card *card_ctx = card->private_data;
1723 struct intel_hdmi_lpe_audio_pdata *pdata = card_ctx->dev->platform_data;
1724 int port;
Takashi Iwaida864802017-01-31 13:52:22 +01001725
Ville Syrjälä8d5c3032017-04-27 19:02:21 +03001726 spin_lock_irq(&pdata->lpe_audio_slock);
1727 pdata->notify_audio_lpe = NULL;
1728 spin_unlock_irq(&pdata->lpe_audio_slock);
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001729
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001730 for_each_port(card_ctx, port) {
1731 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
Takashi Iwaida864802017-01-31 13:52:22 +01001732
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001733 cancel_work_sync(&ctx->hdmi_audio_wq);
1734 }
1735
1736 if (card_ctx->mmio_start)
1737 iounmap(card_ctx->mmio_start);
1738 if (card_ctx->irq >= 0)
1739 free_irq(card_ctx->irq, card_ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001740}
1741
1742/*
1743 * hdmi_lpe_audio_probe - start bridge with i915
1744 *
1745 * This function is called when the i915 driver creates the
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001746 * hdmi-lpe-audio platform device.
Takashi Iwaida864802017-01-31 13:52:22 +01001747 */
1748static int hdmi_lpe_audio_probe(struct platform_device *pdev)
1749{
1750 struct snd_card *card;
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001751 struct snd_intelhad_card *card_ctx;
Takashi Iwaic77a6ed2018-02-28 08:46:00 +01001752 struct snd_intelhad *ctx;
Takashi Iwaida864802017-01-31 13:52:22 +01001753 struct snd_pcm *pcm;
1754 struct intel_hdmi_lpe_audio_pdata *pdata;
1755 int irq;
1756 struct resource *res_mmio;
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001757 int port, ret;
Takashi Iwaida864802017-01-31 13:52:22 +01001758
Takashi Iwaida864802017-01-31 13:52:22 +01001759 pdata = pdev->dev.platform_data;
1760 if (!pdata) {
1761 dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
1762 return -EINVAL;
1763 }
1764
1765 /* get resources */
1766 irq = platform_get_irq(pdev, 0);
1767 if (irq < 0) {
Gustavo A. R. Silva19671582017-06-30 15:18:41 -05001768 dev_err(&pdev->dev, "Could not get irq resource: %d\n", irq);
1769 return irq;
Takashi Iwaida864802017-01-31 13:52:22 +01001770 }
1771
1772 res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1773 if (!res_mmio) {
1774 dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
1775 return -ENXIO;
1776 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301777
Takashi Iwai5647aec2017-01-31 08:14:34 +01001778 /* create a card instance with ALSA framework */
Takashi Iwaida864802017-01-31 13:52:22 +01001779 ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001780 THIS_MODULE, sizeof(*card_ctx), &card);
Takashi Iwaida864802017-01-31 13:52:22 +01001781 if (ret)
1782 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301783
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001784 card_ctx = card->private_data;
1785 card_ctx->dev = &pdev->dev;
1786 card_ctx->card = card;
Takashi Iwaida864802017-01-31 13:52:22 +01001787 strcpy(card->driver, INTEL_HAD);
Takashi Iwai873ab032017-02-07 12:14:04 +01001788 strcpy(card->shortname, "Intel HDMI/DP LPE Audio");
1789 strcpy(card->longname, "Intel HDMI/DP LPE Audio");
Jerome Anand5dab11d2017-01-25 04:27:52 +05301790
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001791 card_ctx->irq = -1;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301792
Takashi Iwaida864802017-01-31 13:52:22 +01001793 card->private_free = hdmi_lpe_audio_free;
1794
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001795 platform_set_drvdata(pdev, card_ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001796
Takashi Iwaic77a6ed2018-02-28 08:46:00 +01001797 card_ctx->num_pipes = pdata->num_pipes;
1798 card_ctx->num_ports = single_port ? 1 : pdata->num_ports;
1799
1800 for_each_port(card_ctx, port) {
1801 ctx = &card_ctx->pcm_ctx[port];
1802 ctx->card_ctx = card_ctx;
1803 ctx->dev = card_ctx->dev;
1804 ctx->port = single_port ? -1 : port;
1805 ctx->pipe = -1;
1806
1807 spin_lock_init(&ctx->had_spinlock);
1808 mutex_init(&ctx->mutex);
1809 INIT_WORK(&ctx->hdmi_audio_wq, had_audio_wq);
1810 }
1811
Takashi Iwaida864802017-01-31 13:52:22 +01001812 dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
1813 __func__, (unsigned int)res_mmio->start,
1814 (unsigned int)res_mmio->end);
1815
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001816 card_ctx->mmio_start = ioremap_nocache(res_mmio->start,
1817 (size_t)(resource_size(res_mmio)));
1818 if (!card_ctx->mmio_start) {
Takashi Iwaida864802017-01-31 13:52:22 +01001819 dev_err(&pdev->dev, "Could not get ioremap\n");
1820 ret = -EACCES;
1821 goto err;
1822 }
1823
1824 /* setup interrupt handler */
1825 ret = request_irq(irq, display_pipe_interrupt_handler, 0,
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001826 pdev->name, card_ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001827 if (ret < 0) {
1828 dev_err(&pdev->dev, "request_irq failed\n");
1829 goto err;
1830 }
1831
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001832 card_ctx->irq = irq;
Takashi Iwai412bbe7d52017-02-02 22:03:22 +01001833
1834 /* only 32bit addressable */
1835 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
1836 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1837
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001838 init_channel_allocations();
Jerome Anand5dab11d2017-01-25 04:27:52 +05301839
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001840 card_ctx->num_pipes = pdata->num_pipes;
Takashi Iwai7229b122018-02-20 17:33:45 +01001841 card_ctx->num_ports = single_port ? 1 : pdata->num_ports;
Ville Syrjäläbb4ac5a2017-04-27 19:02:28 +03001842
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001843 for_each_port(card_ctx, port) {
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001844 int i;
1845
Takashi Iwaic77a6ed2018-02-28 08:46:00 +01001846 ctx = &card_ctx->pcm_ctx[port];
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001847 ret = snd_pcm_new(card, INTEL_HAD, port, MAX_PB_STREAMS,
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001848 MAX_CAP_STREAMS, &pcm);
1849 if (ret)
Ville Syrjäläbb4ac5a2017-04-27 19:02:28 +03001850 goto err;
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001851
1852 /* setup private data which can be retrieved when required */
1853 pcm->private_data = ctx;
1854 pcm->info_flags = 0;
Takashi Iwaic2882482018-06-27 14:59:00 +02001855 strlcpy(pcm->name, card->shortname, strlen(card->shortname));
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001856 /* setup the ops for playabck */
1857 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &had_pcm_ops);
1858
1859 /* allocate dma pages;
1860 * try to allocate 600k buffer as default which is large enough
1861 */
1862 snd_pcm_lib_preallocate_pages_for_all(pcm,
1863 SNDRV_DMA_TYPE_DEV, NULL,
1864 HAD_DEFAULT_BUFFER, HAD_MAX_BUFFER);
1865
1866 /* create controls */
1867 for (i = 0; i < ARRAY_SIZE(had_controls); i++) {
1868 struct snd_kcontrol *kctl;
1869
1870 kctl = snd_ctl_new1(&had_controls[i], ctx);
1871 if (!kctl) {
1872 ret = -ENOMEM;
1873 goto err;
1874 }
1875
1876 kctl->id.device = pcm->device;
1877
1878 ret = snd_ctl_add(card, kctl);
1879 if (ret < 0)
1880 goto err;
Ville Syrjäläbb4ac5a2017-04-27 19:02:28 +03001881 }
1882
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001883 /* Register channel map controls */
1884 ret = had_register_chmap_ctls(ctx, pcm);
1885 if (ret < 0)
1886 goto err;
Ville Syrjäläbb4ac5a2017-04-27 19:02:28 +03001887
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001888 ret = had_create_jack(ctx, pcm);
Takashi Iwai4aedb942017-02-02 16:38:39 +01001889 if (ret < 0)
1890 goto err;
1891 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301892
Takashi Iwaida864802017-01-31 13:52:22 +01001893 ret = snd_card_register(card);
1894 if (ret)
Takashi Iwai36ec0d92017-01-31 08:47:05 +01001895 goto err;
1896
Takashi Iwaibcce7752017-02-01 17:18:20 +01001897 spin_lock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001898 pdata->notify_audio_lpe = notify_audio_lpe;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001899 spin_unlock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001900
Takashi Iwai3002b9f2017-02-13 09:52:44 +01001901 pm_runtime_use_autosuspend(&pdev->dev);
1902 pm_runtime_mark_last_busy(&pdev->dev);
Takashi Iwaida864802017-01-31 13:52:22 +01001903 pm_runtime_set_active(&pdev->dev);
Takashi Iwaida864802017-01-31 13:52:22 +01001904
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001905 dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001906 for_each_port(card_ctx, port) {
1907 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
1908
1909 schedule_work(&ctx->hdmi_audio_wq);
1910 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301911
Takashi Iwai79dda752017-01-30 17:23:39 +01001912 return 0;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001913
Jerome Anand5dab11d2017-01-25 04:27:52 +05301914err:
1915 snd_card_free(card);
Takashi Iwaida864802017-01-31 13:52:22 +01001916 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301917}
1918
Takashi Iwai79dda752017-01-30 17:23:39 +01001919/*
Takashi Iwaida864802017-01-31 13:52:22 +01001920 * hdmi_lpe_audio_remove - stop bridge with i915
Jerome Anand5dab11d2017-01-25 04:27:52 +05301921 *
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001922 * This function is called when the platform device is destroyed.
Jerome Anand5dab11d2017-01-25 04:27:52 +05301923 */
Takashi Iwaida864802017-01-31 13:52:22 +01001924static int hdmi_lpe_audio_remove(struct platform_device *pdev)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301925{
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001926 struct snd_intelhad_card *card_ctx = platform_get_drvdata(pdev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301927
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001928 snd_card_free(card_ctx->card);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301929 return 0;
1930}
1931
Takashi Iwai182cdf22017-02-02 14:43:39 +01001932static const struct dev_pm_ops hdmi_lpe_audio_pm = {
1933 SET_SYSTEM_SLEEP_PM_OPS(hdmi_lpe_audio_suspend, hdmi_lpe_audio_resume)
Takashi Iwai3002b9f2017-02-13 09:52:44 +01001934 SET_RUNTIME_PM_OPS(hdmi_lpe_audio_runtime_suspend,
1935 hdmi_lpe_audio_runtime_resume, NULL)
Takashi Iwai182cdf22017-02-02 14:43:39 +01001936};
1937
Takashi Iwaida864802017-01-31 13:52:22 +01001938static struct platform_driver hdmi_lpe_audio_driver = {
1939 .driver = {
1940 .name = "hdmi-lpe-audio",
Takashi Iwai182cdf22017-02-02 14:43:39 +01001941 .pm = &hdmi_lpe_audio_pm,
Takashi Iwaida864802017-01-31 13:52:22 +01001942 },
1943 .probe = hdmi_lpe_audio_probe,
1944 .remove = hdmi_lpe_audio_remove,
Takashi Iwaida864802017-01-31 13:52:22 +01001945};
1946
1947module_platform_driver(hdmi_lpe_audio_driver);
1948MODULE_ALIAS("platform:hdmi_lpe_audio");
1949
Jerome Anand5dab11d2017-01-25 04:27:52 +05301950MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1951MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1952MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1953MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1954MODULE_DESCRIPTION("Intel HDMI Audio driver");
1955MODULE_LICENSE("GPL v2");
1956MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");