blob: 4ed9d0c41843888d6e9c4aa7f79b980c3e0ac74b [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 Iwai313d9f282017-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 Iwai313d9f282017-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_cfg cfg_val = {.regval = 0};
294 union aud_ch_status_0 ch_stat0 = {.regval = 0};
295 union aud_ch_status_1 ch_stat1 = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530296
Takashi Iwai7ceba752017-02-02 15:58:35 +0100297 ch_stat0.regx.lpcm_id = (intelhaddata->aes_bits &
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100298 IEC958_AES0_NONAUDIO) >> 1;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100299 ch_stat0.regx.clk_acc = (intelhaddata->aes_bits &
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100300 IEC958_AES3_CON_CLOCK) >> 4;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100301 cfg_val.regx.val_bit = ch_stat0.regx.lpcm_id;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530302
303 switch (substream->runtime->rate) {
304 case AUD_SAMPLE_RATE_32:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100305 ch_stat0.regx.samp_freq = CH_STATUS_MAP_32KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530306 break;
307
308 case AUD_SAMPLE_RATE_44_1:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100309 ch_stat0.regx.samp_freq = CH_STATUS_MAP_44KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530310 break;
311 case AUD_SAMPLE_RATE_48:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100312 ch_stat0.regx.samp_freq = CH_STATUS_MAP_48KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530313 break;
314 case AUD_SAMPLE_RATE_88_2:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100315 ch_stat0.regx.samp_freq = CH_STATUS_MAP_88KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530316 break;
317 case AUD_SAMPLE_RATE_96:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100318 ch_stat0.regx.samp_freq = CH_STATUS_MAP_96KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530319 break;
320 case AUD_SAMPLE_RATE_176_4:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100321 ch_stat0.regx.samp_freq = CH_STATUS_MAP_176KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530322 break;
323 case AUD_SAMPLE_RATE_192:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100324 ch_stat0.regx.samp_freq = CH_STATUS_MAP_192KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530325 break;
326
327 default:
328 /* control should never come here */
329 return -EINVAL;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530330 }
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100331
Takashi Iwai79dda752017-01-30 17:23:39 +0100332 had_write_register(intelhaddata,
Takashi Iwai7ceba752017-02-02 15:58:35 +0100333 AUD_CH_STATUS_0, ch_stat0.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530334
Takashi Iwai85bd8742017-02-07 13:33:17 +0100335 switch (substream->runtime->format) {
Takashi Iwai85bd8742017-02-07 13:33:17 +0100336 case SNDRV_PCM_FORMAT_S16_LE:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100337 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_20;
338 ch_stat1.regx.wrd_len = SMPL_WIDTH_16BITS;
Takashi Iwai85bd8742017-02-07 13:33:17 +0100339 break;
Takashi Iwai85bd8742017-02-07 13:33:17 +0100340 case SNDRV_PCM_FORMAT_S24_LE:
341 case SNDRV_PCM_FORMAT_S32_LE:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100342 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_24;
343 ch_stat1.regx.wrd_len = SMPL_WIDTH_24BITS;
Takashi Iwai85bd8742017-02-07 13:33:17 +0100344 break;
345 default:
346 return -EINVAL;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530347 }
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100348
Takashi Iwai79dda752017-01-30 17:23:39 +0100349 had_write_register(intelhaddata,
Takashi Iwai7ceba752017-02-02 15:58:35 +0100350 AUD_CH_STATUS_1, ch_stat1.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530351 return 0;
352}
353
Takashi Iwai76296ef2017-01-30 16:09:11 +0100354/*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530355 * function to initialize audio
356 * registers and buffer confgiuration registers
357 * This function is called in the prepare callback
358 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100359static int had_init_audio_ctrl(struct snd_pcm_substream *substream,
360 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530361{
Takashi Iwai7ceba752017-02-02 15:58:35 +0100362 union aud_cfg cfg_val = {.regval = 0};
363 union aud_buf_config buf_cfg = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530364 u8 channels;
365
366 had_prog_status_reg(substream, intelhaddata);
367
Takashi Iwai7ceba752017-02-02 15:58:35 +0100368 buf_cfg.regx.audio_fifo_watermark = FIFO_THRESHOLD;
369 buf_cfg.regx.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
370 buf_cfg.regx.aud_delay = 0;
371 had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530372
373 channels = substream->runtime->channels;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100374 cfg_val.regx.num_ch = channels - 2;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530375 if (channels <= 2)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100376 cfg_val.regx.layout = LAYOUT0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530377 else
Takashi Iwai7ceba752017-02-02 15:58:35 +0100378 cfg_val.regx.layout = LAYOUT1;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530379
Takashi Iwai3fe2cf72017-02-07 13:53:42 +0100380 if (substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE)
381 cfg_val.regx.packet_mode = 1;
382
Takashi Iwai85bd8742017-02-07 13:33:17 +0100383 if (substream->runtime->format == SNDRV_PCM_FORMAT_S32_LE)
384 cfg_val.regx.left_align = 1;
385
Takashi Iwai7ceba752017-02-02 15:58:35 +0100386 cfg_val.regx.val_bit = 1;
Takashi Iwai83af57d2017-02-03 08:50:06 +0100387
388 /* fix up the DP bits */
389 if (intelhaddata->dp_output) {
390 cfg_val.regx.dp_modei = 1;
391 cfg_val.regx.set = 1;
392 }
393
Takashi Iwai7ceba752017-02-02 15:58:35 +0100394 had_write_register(intelhaddata, AUD_CONFIG, cfg_val.regval);
Takashi Iwai40ce4b52017-02-07 16:17:06 +0100395 intelhaddata->aud_config = cfg_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530396 return 0;
397}
398
Jerome Anand5dab11d2017-01-25 04:27:52 +0530399/*
400 * Compute derived values in channel_allocations[].
401 */
402static void init_channel_allocations(void)
403{
404 int i, j;
405 struct cea_channel_speaker_allocation *p;
406
Jerome Anand5dab11d2017-01-25 04:27:52 +0530407 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
408 p = channel_allocations + i;
409 p->channels = 0;
410 p->spk_mask = 0;
411 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
412 if (p->speakers[j]) {
413 p->channels++;
414 p->spk_mask |= p->speakers[j];
415 }
416 }
417}
418
419/*
420 * The transformation takes two steps:
421 *
422 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
423 * spk_mask => (channel_allocations[]) => ai->CA
424 *
425 * TODO: it could select the wrong CA from multiple candidates.
426 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100427static int had_channel_allocation(struct snd_intelhad *intelhaddata,
428 int channels)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530429{
430 int i;
431 int ca = 0;
432 int spk_mask = 0;
433
434 /*
435 * CA defaults to 0 for basic stereo audio
436 */
437 if (channels <= 2)
438 return 0;
439
440 /*
441 * expand ELD's speaker allocation mask
442 *
443 * ELD tells the speaker mask in a compact(paired) form,
444 * expand ELD's notions to match the ones used by Audio InfoFrame.
445 */
446
447 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100448 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530449 spk_mask |= eld_speaker_allocation_bits[i];
450 }
451
452 /* search for the first working match in the CA table */
453 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
454 if (channels == channel_allocations[i].channels &&
455 (spk_mask & channel_allocations[i].spk_mask) ==
456 channel_allocations[i].spk_mask) {
457 ca = channel_allocations[i].ca_index;
458 break;
459 }
460 }
461
Takashi Iwaic75b0472017-01-31 15:49:15 +0100462 dev_dbg(intelhaddata->dev, "select CA 0x%x for %d\n", ca, channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530463
464 return ca;
465}
466
467/* from speaker bit mask to ALSA API channel position */
468static int spk_to_chmap(int spk)
469{
Takashi Iwai4a5ddb22017-02-01 16:45:38 +0100470 const struct channel_map_table *t = map_tables;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530471
472 for (; t->map; t++) {
473 if (t->spk_mask == spk)
474 return t->map;
475 }
476 return 0;
477}
478
Takashi Iwai372d8552017-01-31 13:57:58 +0100479static void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530480{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100481 int i, c;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530482 int spk_mask = 0;
483 struct snd_pcm_chmap_elem *chmap;
484 u8 eld_high, eld_high_mask = 0xF0;
485 u8 high_msb;
486
Takashi Iwai18353192017-02-15 21:42:20 +0100487 kfree(intelhaddata->chmap->chmap);
488 intelhaddata->chmap->chmap = NULL;
489
Jerome Anand5dab11d2017-01-25 04:27:52 +0530490 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
Takashi Iwai18353192017-02-15 21:42:20 +0100491 if (!chmap)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530492 return;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530493
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100494 dev_dbg(intelhaddata->dev, "eld speaker = %x\n",
495 intelhaddata->eld[DRM_ELD_SPEAKER]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530496
497 /* WA: Fix the max channel supported to 8 */
498
499 /*
500 * Sink may support more than 8 channels, if eld_high has more than
501 * one bit set. SOC supports max 8 channels.
502 * Refer eld_speaker_allocation_bits, for sink speaker allocation
503 */
504
505 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100506 eld_high = intelhaddata->eld[DRM_ELD_SPEAKER] & eld_high_mask;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530507 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
508 /* eld_high & (eld_high-1): if more than 1 bit set */
509 /* 0x1F: 7 channels */
510 for (i = 1; i < 4; i++) {
511 high_msb = eld_high & (0x80 >> i);
512 if (high_msb) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100513 intelhaddata->eld[DRM_ELD_SPEAKER] &=
Jerome Anand5dab11d2017-01-25 04:27:52 +0530514 high_msb | 0xF;
515 break;
516 }
517 }
518 }
519
520 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100521 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530522 spk_mask |= eld_speaker_allocation_bits[i];
523 }
524
525 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
526 if (spk_mask == channel_allocations[i].spk_mask) {
527 for (c = 0; c < channel_allocations[i].channels; c++) {
528 chmap->map[c] = spk_to_chmap(
529 channel_allocations[i].speakers[
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100530 (MAX_SPEAKERS - 1) - c]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530531 }
532 chmap->channels = channel_allocations[i].channels;
533 intelhaddata->chmap->chmap = chmap;
534 break;
535 }
536 }
Takashi Iwai18353192017-02-15 21:42:20 +0100537 if (i >= ARRAY_SIZE(channel_allocations))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530538 kfree(chmap);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530539}
540
541/*
542 * ALSA API channel-map control callbacks
543 */
544static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
545 struct snd_ctl_elem_info *uinfo)
546{
Jerome Anand5dab11d2017-01-25 04:27:52 +0530547 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
548 uinfo->count = HAD_MAX_CHANNEL;
549 uinfo->value.integer.min = 0;
550 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
551 return 0;
552}
553
554static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
555 struct snd_ctl_elem_value *ucontrol)
556{
557 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
558 struct snd_intelhad *intelhaddata = info->private_data;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100559 int i;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530560 const struct snd_pcm_chmap_elem *chmap;
561
Takashi Iwaia72ccfb2017-02-15 21:45:06 +0100562 memset(ucontrol->value.integer.value, 0,
563 sizeof(long) * HAD_MAX_CHANNEL);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100564 mutex_lock(&intelhaddata->mutex);
565 if (!intelhaddata->chmap->chmap) {
566 mutex_unlock(&intelhaddata->mutex);
Takashi Iwaia72ccfb2017-02-15 21:45:06 +0100567 return 0;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100568 }
569
Jerome Anand5dab11d2017-01-25 04:27:52 +0530570 chmap = intelhaddata->chmap->chmap;
Takashi Iwaic75b0472017-01-31 15:49:15 +0100571 for (i = 0; i < chmap->channels; i++)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530572 ucontrol->value.integer.value[i] = chmap->map[i];
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100573 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530574
575 return 0;
576}
577
578static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
579 struct snd_pcm *pcm)
580{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100581 int err;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530582
583 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
584 NULL, 0, (unsigned long)intelhaddata,
585 &intelhaddata->chmap);
586 if (err < 0)
587 return err;
588
589 intelhaddata->chmap->private_data = intelhaddata;
Takashi Iwaie9d65ab2017-01-31 16:11:27 +0100590 intelhaddata->chmap->kctl->info = had_chmap_ctl_info;
591 intelhaddata->chmap->kctl->get = had_chmap_ctl_get;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530592 intelhaddata->chmap->chmap = NULL;
593 return 0;
594}
595
Takashi Iwai76296ef2017-01-30 16:09:11 +0100596/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100597 * Initialize Data Island Packets registers
Jerome Anand5dab11d2017-01-25 04:27:52 +0530598 * This function is called in the prepare callback
599 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100600static void had_prog_dip(struct snd_pcm_substream *substream,
601 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530602{
603 int i;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100604 union aud_ctrl_st ctrl_state = {.regval = 0};
605 union aud_info_frame2 frame2 = {.regval = 0};
606 union aud_info_frame3 frame3 = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530607 u8 checksum = 0;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600608 u32 info_frame;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530609 int channels;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100610 int ca;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530611
612 channels = substream->runtime->channels;
613
Takashi Iwai7ceba752017-02-02 15:58:35 +0100614 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530615
Takashi Iwaib5562902017-02-04 22:05:33 +0100616 ca = had_channel_allocation(intelhaddata, channels);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600617 if (intelhaddata->dp_output) {
618 info_frame = DP_INFO_FRAME_WORD1;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100619 frame2.regval = (substream->runtime->channels - 1) | (ca << 24);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600620 } else {
621 info_frame = HDMI_INFO_FRAME_WORD1;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100622 frame2.regx.chnl_cnt = substream->runtime->channels - 1;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100623 frame3.regx.chnl_alloc = ca;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530624
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100625 /* Calculte the byte wide checksum for all valid DIP words */
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 += (info_frame >> (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 += (frame2.regval >> (i * 8)) & 0xff;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600630 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100631 checksum += (frame3.regval >> (i * 8)) & 0xff;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530632
Takashi Iwai7ceba752017-02-02 15:58:35 +0100633 frame2.regx.chksum = -(checksum);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600634 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530635
Takashi Iwai4151ee82017-01-31 18:14:15 +0100636 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, info_frame);
Takashi Iwai7ceba752017-02-02 15:58:35 +0100637 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame2.regval);
638 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame3.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530639
640 /* program remaining DIP words with zero */
641 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
Takashi Iwai4151ee82017-01-31 18:14:15 +0100642 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, 0x0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530643
Takashi Iwai7ceba752017-02-02 15:58:35 +0100644 ctrl_state.regx.dip_freq = 1;
645 ctrl_state.regx.dip_en_sta = 1;
646 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530647}
648
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600649static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
650{
651 u32 maud_val;
652
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100653 /* Select maud according to DP 1.2 spec */
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600654 if (link_rate == DP_2_7_GHZ) {
655 switch (aud_samp_freq) {
656 case AUD_SAMPLE_RATE_32:
657 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
658 break;
659
660 case AUD_SAMPLE_RATE_44_1:
661 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
662 break;
663
664 case AUD_SAMPLE_RATE_48:
665 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
666 break;
667
668 case AUD_SAMPLE_RATE_88_2:
669 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
670 break;
671
672 case AUD_SAMPLE_RATE_96:
673 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
674 break;
675
676 case AUD_SAMPLE_RATE_176_4:
677 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
678 break;
679
680 case HAD_MAX_RATE:
681 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
682 break;
683
684 default:
685 maud_val = -EINVAL;
686 break;
687 }
688 } else if (link_rate == DP_1_62_GHZ) {
689 switch (aud_samp_freq) {
690 case AUD_SAMPLE_RATE_32:
691 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
692 break;
693
694 case AUD_SAMPLE_RATE_44_1:
695 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
696 break;
697
698 case AUD_SAMPLE_RATE_48:
699 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
700 break;
701
702 case AUD_SAMPLE_RATE_88_2:
703 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
704 break;
705
706 case AUD_SAMPLE_RATE_96:
707 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
708 break;
709
710 case AUD_SAMPLE_RATE_176_4:
711 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
712 break;
713
714 case HAD_MAX_RATE:
715 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
716 break;
717
718 default:
719 maud_val = -EINVAL;
720 break;
721 }
722 } else
723 maud_val = -EINVAL;
724
725 return maud_val;
726}
727
Takashi Iwai76296ef2017-01-30 16:09:11 +0100728/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100729 * Program HDMI audio CTS value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530730 *
731 * @aud_samp_freq: sampling frequency of audio data
732 * @tmds: sampling frequency of the display data
Takashi Iwaib5562902017-02-04 22:05:33 +0100733 * @link_rate: DP link rate
Jerome Anand5dab11d2017-01-25 04:27:52 +0530734 * @n_param: N value, depends on aud_samp_freq
Takashi Iwaib5562902017-02-04 22:05:33 +0100735 * @intelhaddata: substream private data
Jerome Anand5dab11d2017-01-25 04:27:52 +0530736 *
737 * Program CTS register based on the audio and display sampling frequency
738 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100739static void had_prog_cts(u32 aud_samp_freq, u32 tmds, u32 link_rate,
740 u32 n_param, struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530741{
742 u32 cts_val;
743 u64 dividend, divisor;
744
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600745 if (intelhaddata->dp_output) {
746 /* Substitute cts_val with Maud according to DP 1.2 spec*/
747 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
748 } else {
749 /* Calculate CTS according to HDMI 1.3a spec*/
750 dividend = (u64)tmds * n_param*1000;
751 divisor = 128 * aud_samp_freq;
752 cts_val = div64_u64(dividend, divisor);
753 }
Takashi Iwaic75b0472017-01-31 15:49:15 +0100754 dev_dbg(intelhaddata->dev, "TMDS value=%d, N value=%d, CTS Value=%d\n",
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600755 tmds, n_param, cts_val);
Takashi Iwai79dda752017-01-30 17:23:39 +0100756 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530757}
758
759static int had_calculate_n_value(u32 aud_samp_freq)
760{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100761 int n_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530762
763 /* Select N according to HDMI 1.3a spec*/
764 switch (aud_samp_freq) {
765 case AUD_SAMPLE_RATE_32:
766 n_val = 4096;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100767 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530768
769 case AUD_SAMPLE_RATE_44_1:
770 n_val = 6272;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100771 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530772
773 case AUD_SAMPLE_RATE_48:
774 n_val = 6144;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100775 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530776
777 case AUD_SAMPLE_RATE_88_2:
778 n_val = 12544;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100779 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530780
781 case AUD_SAMPLE_RATE_96:
782 n_val = 12288;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100783 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530784
785 case AUD_SAMPLE_RATE_176_4:
786 n_val = 25088;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100787 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530788
789 case HAD_MAX_RATE:
790 n_val = 24576;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100791 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530792
793 default:
794 n_val = -EINVAL;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100795 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530796 }
797 return n_val;
798}
799
Takashi Iwai76296ef2017-01-30 16:09:11 +0100800/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100801 * Program HDMI audio N value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530802 *
803 * @aud_samp_freq: sampling frequency of audio data
804 * @n_param: N value, depends on aud_samp_freq
Takashi Iwaib5562902017-02-04 22:05:33 +0100805 * @intelhaddata: substream private data
Jerome Anand5dab11d2017-01-25 04:27:52 +0530806 *
807 * This function is called in the prepare callback.
808 * It programs based on the audio and display sampling frequency
809 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100810static int had_prog_n(u32 aud_samp_freq, u32 *n_param,
811 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530812{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100813 int n_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530814
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600815 if (intelhaddata->dp_output) {
816 /*
817 * According to DP specs, Maud and Naud values hold
818 * a relationship, which is stated as:
819 * Maud/Naud = 512 * fs / f_LS_Clk
820 * where, fs is the sampling frequency of the audio stream
821 * and Naud is 32768 for Async clock.
822 */
823
824 n_val = DP_NAUD_VAL;
825 } else
826 n_val = had_calculate_n_value(aud_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530827
828 if (n_val < 0)
829 return n_val;
830
Takashi Iwai79dda752017-01-30 17:23:39 +0100831 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530832 *n_param = n_val;
833 return 0;
834}
835
Takashi Iwaie1b239f32017-02-03 00:01:18 +0100836/*
837 * PCM ring buffer handling
838 *
839 * The hardware provides a ring buffer with the fixed 4 buffer descriptors
840 * (BDs). The driver maps these 4 BDs onto the PCM ring buffer. The mapping
841 * moves at each period elapsed. The below illustrates how it works:
842 *
843 * At time=0
844 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
845 * BD | 0 | 1 | 2 | 3 |
846 *
847 * At time=1 (period elapsed)
848 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
849 * BD | 1 | 2 | 3 | 0 |
850 *
851 * At time=2 (second period elapsed)
852 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
853 * BD | 2 | 3 | 0 | 1 |
854 *
855 * The bd_head field points to the index of the BD to be read. It's also the
856 * position to be filled at next. The pcm_head and the pcm_filled fields
857 * point to the indices of the current position and of the next position to
858 * be filled, respectively. For PCM buffer there are both _head and _filled
859 * because they may be difference when nperiods > 4. For example, in the
860 * example above at t=1, bd_head=1 and pcm_head=1 while pcm_filled=5:
861 *
862 * pcm_head (=1) --v v-- pcm_filled (=5)
863 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
864 * BD | 1 | 2 | 3 | 0 |
865 * bd_head (=1) --^ ^-- next to fill (= bd_head)
866 *
867 * For nperiods < 4, the remaining BDs out of 4 are marked as invalid, so that
868 * the hardware skips those BDs in the loop.
Takashi Iwai8d48c012017-02-07 08:05:46 +0100869 *
870 * An exceptional setup is the case with nperiods=1. Since we have to update
871 * BDs after finishing one BD processing, we'd need at least two BDs, where
872 * both BDs point to the same content, the same address, the same size of the
873 * whole PCM buffer.
Takashi Iwaie1b239f32017-02-03 00:01:18 +0100874 */
875
876#define AUD_BUF_ADDR(x) (AUD_BUF_A_ADDR + (x) * HAD_REG_WIDTH)
877#define AUD_BUF_LEN(x) (AUD_BUF_A_LENGTH + (x) * HAD_REG_WIDTH)
878
879/* Set up a buffer descriptor at the "filled" position */
880static void had_prog_bd(struct snd_pcm_substream *substream,
881 struct snd_intelhad *intelhaddata)
882{
883 int idx = intelhaddata->bd_head;
884 int ofs = intelhaddata->pcmbuf_filled * intelhaddata->period_bytes;
885 u32 addr = substream->runtime->dma_addr + ofs;
886
Takashi Iwaie8de9852017-02-07 08:09:12 +0100887 addr |= AUD_BUF_VALID;
888 if (!substream->runtime->no_period_wakeup)
889 addr |= AUD_BUF_INTR_EN;
Takashi Iwaie1b239f32017-02-03 00:01:18 +0100890 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), addr);
891 had_write_register(intelhaddata, AUD_BUF_LEN(idx),
892 intelhaddata->period_bytes);
893
894 /* advance the indices to the next */
895 intelhaddata->bd_head++;
896 intelhaddata->bd_head %= intelhaddata->num_bds;
897 intelhaddata->pcmbuf_filled++;
898 intelhaddata->pcmbuf_filled %= substream->runtime->periods;
899}
900
901/* invalidate a buffer descriptor with the given index */
902static void had_invalidate_bd(struct snd_intelhad *intelhaddata,
903 int idx)
904{
905 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), 0);
906 had_write_register(intelhaddata, AUD_BUF_LEN(idx), 0);
907}
908
909/* Initial programming of ring buffer */
910static void had_init_ringbuf(struct snd_pcm_substream *substream,
911 struct snd_intelhad *intelhaddata)
912{
913 struct snd_pcm_runtime *runtime = substream->runtime;
914 int i, num_periods;
915
916 num_periods = runtime->periods;
917 intelhaddata->num_bds = min(num_periods, HAD_NUM_OF_RING_BUFS);
Takashi Iwai8d48c012017-02-07 08:05:46 +0100918 /* set the minimum 2 BDs for num_periods=1 */
919 intelhaddata->num_bds = max(intelhaddata->num_bds, 2U);
Takashi Iwaie1b239f32017-02-03 00:01:18 +0100920 intelhaddata->period_bytes =
921 frames_to_bytes(runtime, runtime->period_size);
922 WARN_ON(intelhaddata->period_bytes & 0x3f);
923
924 intelhaddata->bd_head = 0;
925 intelhaddata->pcmbuf_head = 0;
926 intelhaddata->pcmbuf_filled = 0;
927
928 for (i = 0; i < HAD_NUM_OF_RING_BUFS; i++) {
Takashi Iwai8d48c012017-02-07 08:05:46 +0100929 if (i < intelhaddata->num_bds)
Takashi Iwaie1b239f32017-02-03 00:01:18 +0100930 had_prog_bd(substream, intelhaddata);
931 else /* invalidate the rest */
932 had_invalidate_bd(intelhaddata, i);
933 }
934
935 intelhaddata->bd_head = 0; /* reset at head again before starting */
936}
937
938/* process a bd, advance to the next */
939static void had_advance_ringbuf(struct snd_pcm_substream *substream,
940 struct snd_intelhad *intelhaddata)
941{
942 int num_periods = substream->runtime->periods;
943
944 /* reprogram the next buffer */
945 had_prog_bd(substream, intelhaddata);
946
947 /* proceed to next */
948 intelhaddata->pcmbuf_head++;
949 intelhaddata->pcmbuf_head %= num_periods;
950}
951
952/* process the current BD(s);
953 * returns the current PCM buffer byte position, or -EPIPE for underrun.
954 */
955static int had_process_ringbuf(struct snd_pcm_substream *substream,
956 struct snd_intelhad *intelhaddata)
957{
958 int len, processed;
959 unsigned long flags;
960
961 processed = 0;
962 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
963 for (;;) {
964 /* get the remaining bytes on the buffer */
965 had_read_register(intelhaddata,
966 AUD_BUF_LEN(intelhaddata->bd_head),
967 &len);
968 if (len < 0 || len > intelhaddata->period_bytes) {
969 dev_dbg(intelhaddata->dev, "Invalid buf length %d\n",
970 len);
971 len = -EPIPE;
972 goto out;
973 }
974
975 if (len > 0) /* OK, this is the current buffer */
976 break;
977
978 /* len=0 => already empty, check the next buffer */
979 if (++processed >= intelhaddata->num_bds) {
980 len = -EPIPE; /* all empty? - report underrun */
981 goto out;
982 }
983 had_advance_ringbuf(substream, intelhaddata);
984 }
985
986 len = intelhaddata->period_bytes - len;
987 len += intelhaddata->period_bytes * intelhaddata->pcmbuf_head;
988 out:
989 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
990 return len;
991}
992
993/* called from irq handler */
994static void had_process_buffer_done(struct snd_intelhad *intelhaddata)
995{
996 struct snd_pcm_substream *substream;
997
Takashi Iwaie1b239f32017-02-03 00:01:18 +0100998 substream = had_substream_get(intelhaddata);
999 if (!substream)
1000 return; /* no stream? - bail out */
1001
Takashi Iwaibe9a2e92017-02-15 22:05:34 +01001002 if (!intelhaddata->connected) {
1003 snd_pcm_stop_xrun(substream);
1004 goto out; /* disconnected? - bail out */
1005 }
1006
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001007 /* process or stop the stream */
1008 if (had_process_ringbuf(substream, intelhaddata) < 0)
1009 snd_pcm_stop_xrun(substream);
1010 else
1011 snd_pcm_period_elapsed(substream);
1012
Takashi Iwaibe9a2e92017-02-15 22:05:34 +01001013 out:
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001014 had_substream_put(intelhaddata);
1015}
1016
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001017/*
1018 * The interrupt status 'sticky' bits might not be cleared by
1019 * setting '1' to that bit once...
1020 */
1021static void wait_clear_underrun_bit(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301022{
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001023 int i;
1024 u32 val;
1025
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001026 for (i = 0; i < 100; i++) {
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001027 /* clear bit30, 31 AUD_HDMI_STATUS */
1028 had_read_register(intelhaddata, AUD_HDMI_STATUS, &val);
Takashi Iwai77531be2017-02-07 12:17:23 +01001029 if (!(val & AUD_HDMI_STATUS_MASK_UNDERRUN))
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001030 return;
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001031 udelay(100);
1032 cond_resched();
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001033 had_write_register(intelhaddata, AUD_HDMI_STATUS, val);
1034 }
1035 dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n");
1036}
1037
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001038/* Perform some reset procedure but only when need_reset is set;
1039 * this is called from prepare or hw_free callbacks once after trigger STOP
1040 * or underrun has been processed in order to settle down the h/w state.
1041 */
1042static void had_do_reset(struct snd_intelhad *intelhaddata)
1043{
Takashi Iwai28ed1252017-02-15 22:02:10 +01001044 if (!intelhaddata->need_reset || !intelhaddata->connected)
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001045 return;
1046
1047 /* Reset buffer pointers */
1048 had_reset_audio(intelhaddata);
1049 wait_clear_underrun_bit(intelhaddata);
1050 intelhaddata->need_reset = false;
1051}
1052
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001053/* called from irq handler */
1054static void had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
1055{
1056 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301057
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001058 /* Report UNDERRUN error to above layers */
1059 substream = had_substream_get(intelhaddata);
1060 if (substream) {
1061 snd_pcm_stop_xrun(substream);
1062 had_substream_put(intelhaddata);
1063 }
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001064 intelhaddata->need_reset = true;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301065}
1066
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001067/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001068 * ALSA PCM open callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301069 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001070static int had_pcm_open(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301071{
1072 struct snd_intelhad *intelhaddata;
1073 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301074 int retval;
1075
Jerome Anand5dab11d2017-01-25 04:27:52 +05301076 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301077 runtime = substream->runtime;
1078
Takashi Iwai182cdf22017-02-02 14:43:39 +01001079 pm_runtime_get_sync(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301080
Jerome Anand5dab11d2017-01-25 04:27:52 +05301081 /* set the runtime hw parameter with local snd_pcm_hardware struct */
Takashi Iwaib5562902017-02-04 22:05:33 +01001082 runtime->hw = had_pcm_hardware;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301083
Jerome Anand5dab11d2017-01-25 04:27:52 +05301084 retval = snd_pcm_hw_constraint_integer(runtime,
1085 SNDRV_PCM_HW_PARAM_PERIODS);
1086 if (retval < 0)
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001087 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301088
1089 /* Make sure, that the period size is always aligned
1090 * 64byte boundary
1091 */
1092 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1093 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
Takashi Iwai73997b02017-02-02 17:38:50 +01001094 if (retval < 0)
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001095 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301096
Takashi Iwai85bd8742017-02-07 13:33:17 +01001097 retval = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1098 if (retval < 0)
1099 goto error;
1100
Takashi Iwai73997b02017-02-02 17:38:50 +01001101 /* expose PCM substream */
Takashi Iwai313d9f282017-02-02 13:00:12 +01001102 spin_lock_irq(&intelhaddata->had_spinlock);
1103 intelhaddata->stream_info.substream = substream;
1104 intelhaddata->stream_info.substream_refcount++;
1105 spin_unlock_irq(&intelhaddata->had_spinlock);
1106
Jerome Anand5dab11d2017-01-25 04:27:52 +05301107 return retval;
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001108 error:
Takashi Iwai3002b9f2017-02-13 09:52:44 +01001109 pm_runtime_mark_last_busy(intelhaddata->dev);
1110 pm_runtime_put_autosuspend(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301111 return retval;
1112}
1113
Takashi Iwaidf76df12017-01-31 16:04:10 +01001114/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001115 * ALSA PCM close callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301116 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001117static int had_pcm_close(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301118{
1119 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301120
Jerome Anand5dab11d2017-01-25 04:27:52 +05301121 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301122
Takashi Iwai73997b02017-02-02 17:38:50 +01001123 /* unreference and sync with the pending PCM accesses */
Takashi Iwai313d9f282017-02-02 13:00:12 +01001124 spin_lock_irq(&intelhaddata->had_spinlock);
1125 intelhaddata->stream_info.substream = NULL;
1126 intelhaddata->stream_info.substream_refcount--;
1127 while (intelhaddata->stream_info.substream_refcount > 0) {
1128 spin_unlock_irq(&intelhaddata->had_spinlock);
1129 cpu_relax();
1130 spin_lock_irq(&intelhaddata->had_spinlock);
1131 }
1132 spin_unlock_irq(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301133
Takashi Iwai3002b9f2017-02-13 09:52:44 +01001134 pm_runtime_mark_last_busy(intelhaddata->dev);
1135 pm_runtime_put_autosuspend(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301136 return 0;
1137}
1138
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001139/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001140 * ALSA PCM hw_params callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301141 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001142static int had_pcm_hw_params(struct snd_pcm_substream *substream,
1143 struct snd_pcm_hw_params *hw_params)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301144{
Takashi Iwaic75b0472017-01-31 15:49:15 +01001145 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301146 unsigned long addr;
1147 int pages, buf_size, retval;
1148
Takashi Iwaic75b0472017-01-31 15:49:15 +01001149 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301150 buf_size = params_buffer_bytes(hw_params);
1151 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1152 if (retval < 0)
1153 return retval;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001154 dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
1155 __func__, buf_size);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301156 /* mark the pages as uncached region */
1157 addr = (unsigned long) substream->runtime->dma_area;
1158 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1159 retval = set_memory_uc(addr, pages);
1160 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001161 dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
1162 retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301163 return retval;
1164 }
1165 memset(substream->runtime->dma_area, 0, buf_size);
1166
1167 return retval;
1168}
1169
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001170/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001171 * ALSA PCM hw_free callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301172 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001173static int had_pcm_hw_free(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301174{
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001175 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301176 unsigned long addr;
1177 u32 pages;
1178
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001179 intelhaddata = snd_pcm_substream_chip(substream);
1180 had_do_reset(intelhaddata);
1181
Jerome Anand5dab11d2017-01-25 04:27:52 +05301182 /* mark back the pages as cached/writeback region before the free */
1183 if (substream->runtime->dma_area != NULL) {
1184 addr = (unsigned long) substream->runtime->dma_area;
1185 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1186 PAGE_SIZE;
1187 set_memory_wb(addr, pages);
1188 return snd_pcm_lib_free_pages(substream);
1189 }
1190 return 0;
1191}
1192
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001193/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001194 * ALSA PCM trigger callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301195 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001196static int had_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301197{
Takashi Iwaida864802017-01-31 13:52:22 +01001198 int retval = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301199 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301200
Jerome Anand5dab11d2017-01-25 04:27:52 +05301201 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301202
Takashi Iwaidf42cb42017-02-12 11:35:44 +01001203 spin_lock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301204 switch (cmd) {
1205 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001206 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1207 case SNDRV_PCM_TRIGGER_RESUME:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301208 /* Enable Audio */
Takashi Iwai075a1d42017-02-07 07:55:27 +01001209 had_ack_irqs(intelhaddata); /* FIXME: do we need this? */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001210 had_enable_audio(intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301211 break;
1212
1213 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001214 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301215 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001216 had_enable_audio(intelhaddata, false);
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001217 intelhaddata->need_reset = true;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301218 break;
1219
1220 default:
1221 retval = -EINVAL;
1222 }
Takashi Iwaidf42cb42017-02-12 11:35:44 +01001223 spin_unlock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301224 return retval;
1225}
1226
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001227/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001228 * ALSA PCM prepare callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301229 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001230static int had_pcm_prepare(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301231{
1232 int retval;
1233 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001234 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301235 struct snd_intelhad *intelhaddata;
1236 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301237
Jerome Anand5dab11d2017-01-25 04:27:52 +05301238 intelhaddata = snd_pcm_substream_chip(substream);
1239 runtime = substream->runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301240
Takashi Iwaic75b0472017-01-31 15:49:15 +01001241 dev_dbg(intelhaddata->dev, "period_size=%d\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301242 (int)frames_to_bytes(runtime, runtime->period_size));
Takashi Iwaic75b0472017-01-31 15:49:15 +01001243 dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods);
1244 dev_dbg(intelhaddata->dev, "buffer_size=%d\n",
1245 (int)snd_pcm_lib_buffer_bytes(substream));
1246 dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
1247 dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301248
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001249 had_do_reset(intelhaddata);
1250
Jerome Anand5dab11d2017-01-25 04:27:52 +05301251 /* Get N value in KHz */
Takashi Iwaida864802017-01-31 13:52:22 +01001252 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301253
Takashi Iwaib5562902017-02-04 22:05:33 +01001254 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301255 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001256 dev_err(intelhaddata->dev,
1257 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301258 goto prep_end;
1259 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001260
1261 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001262 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001263
Takashi Iwaib5562902017-02-04 22:05:33 +01001264 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1265 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301266
Takashi Iwaib5562902017-02-04 22:05:33 +01001267 had_prog_dip(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301268
Takashi Iwaib5562902017-02-04 22:05:33 +01001269 retval = had_init_audio_ctrl(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301270
1271 /* Prog buffer address */
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001272 had_init_ringbuf(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301273
1274 /*
1275 * Program channel mapping in following order:
1276 * FL, FR, C, LFE, RL, RR
1277 */
1278
Takashi Iwai79dda752017-01-30 17:23:39 +01001279 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301280
1281prep_end:
1282 return retval;
1283}
1284
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001285/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001286 * ALSA PCM pointer callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301287 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001288static snd_pcm_uframes_t had_pcm_pointer(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301289{
1290 struct snd_intelhad *intelhaddata;
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001291 int len;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301292
Jerome Anand5dab11d2017-01-25 04:27:52 +05301293 intelhaddata = snd_pcm_substream_chip(substream);
1294
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001295 if (!intelhaddata->connected)
Takashi Iwai79f439e2017-01-31 16:46:44 +01001296 return SNDRV_PCM_POS_XRUN;
1297
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001298 len = had_process_ringbuf(substream, intelhaddata);
1299 if (len < 0)
1300 return SNDRV_PCM_POS_XRUN;
Takashi Iwai8d48c012017-02-07 08:05:46 +01001301 len = bytes_to_frames(substream->runtime, len);
1302 /* wrapping may happen when periods=1 */
1303 len %= substream->runtime->buffer_size;
1304 return len;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301305}
1306
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001307/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001308 * ALSA PCM mmap callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301309 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001310static int had_pcm_mmap(struct snd_pcm_substream *substream,
1311 struct vm_area_struct *vma)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301312{
Jerome Anand5dab11d2017-01-25 04:27:52 +05301313 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1314 return remap_pfn_range(vma, vma->vm_start,
1315 substream->dma_buffer.addr >> PAGE_SHIFT,
1316 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1317}
1318
Takashi Iwai73997b02017-02-02 17:38:50 +01001319/*
1320 * ALSA PCM ops
1321 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001322static const struct snd_pcm_ops had_pcm_ops = {
1323 .open = had_pcm_open,
1324 .close = had_pcm_close,
Takashi Iwai73997b02017-02-02 17:38:50 +01001325 .ioctl = snd_pcm_lib_ioctl,
Takashi Iwaib5562902017-02-04 22:05:33 +01001326 .hw_params = had_pcm_hw_params,
1327 .hw_free = had_pcm_hw_free,
1328 .prepare = had_pcm_prepare,
1329 .trigger = had_pcm_trigger,
1330 .pointer = had_pcm_pointer,
1331 .mmap = had_pcm_mmap,
Takashi Iwai73997b02017-02-02 17:38:50 +01001332};
1333
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001334/* process mode change of the running stream; called in mutex */
Takashi Iwaib5562902017-02-04 22:05:33 +01001335static int had_process_mode_change(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301336{
Takashi Iwaida864802017-01-31 13:52:22 +01001337 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301338 int retval = 0;
1339 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001340 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301341
Takashi Iwai313d9f282017-02-02 13:00:12 +01001342 substream = had_substream_get(intelhaddata);
1343 if (!substream)
Takashi Iwaida864802017-01-31 13:52:22 +01001344 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301345
1346 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001347 had_enable_audio(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301348
1349 /* Update CTS value */
Takashi Iwaida864802017-01-31 13:52:22 +01001350 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301351
Takashi Iwaib5562902017-02-04 22:05:33 +01001352 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301353 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001354 dev_err(intelhaddata->dev,
1355 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301356 goto out;
1357 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001358
1359 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001360 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001361
Takashi Iwaib5562902017-02-04 22:05:33 +01001362 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1363 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301364
1365 /* Enable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001366 had_enable_audio(intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301367
1368out:
Takashi Iwai313d9f282017-02-02 13:00:12 +01001369 had_substream_put(intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301370 return retval;
1371}
1372
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001373/* process hot plug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001374static void had_process_hot_plug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001375{
Takashi Iwai372d8552017-01-31 13:57:58 +01001376 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001377
Takashi Iwaibcce7752017-02-01 17:18:20 +01001378 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001379 if (intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001380 dev_dbg(intelhaddata->dev, "Device already connected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001381 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001382 return;
Takashi Iwai372d8552017-01-31 13:57:58 +01001383 }
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001384
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001385 /* Disable Audio */
1386 had_enable_audio(intelhaddata, false);
1387
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001388 intelhaddata->connected = true;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001389 dev_dbg(intelhaddata->dev,
1390 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001391 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001392 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001393
Takashi Iwai2d42c032017-02-15 22:08:21 +01001394 had_build_channel_allocation_map(intelhaddata);
1395
1396 /* Report to above ALSA layer */
Takashi Iwai313d9f282017-02-02 13:00:12 +01001397 substream = had_substream_get(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001398 if (substream) {
Takashi Iwai5def9012017-02-15 21:36:38 +01001399 snd_pcm_stop_xrun(substream);
Takashi Iwai313d9f282017-02-02 13:00:12 +01001400 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001401 }
1402
Takashi Iwaib9bacf22017-02-14 12:29:38 +01001403 snd_jack_report(intelhaddata->jack, SND_JACK_AVOUT);
Takashi Iwai372d8552017-01-31 13:57:58 +01001404}
1405
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001406/* process hot unplug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001407static void had_process_hot_unplug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001408{
Takashi Iwai313d9f282017-02-02 13:00:12 +01001409 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001410
Takashi Iwaibcce7752017-02-01 17:18:20 +01001411 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001412 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001413 dev_dbg(intelhaddata->dev, "Device already disconnected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001414 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai2d42c032017-02-15 22:08:21 +01001415 return;
Takashi Iwai372d8552017-01-31 13:57:58 +01001416
Takashi Iwai372d8552017-01-31 13:57:58 +01001417 }
1418
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001419 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001420 had_enable_audio(intelhaddata, false);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001421
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001422 intelhaddata->connected = false;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001423 dev_dbg(intelhaddata->dev,
1424 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001425 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001426 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai313d9f282017-02-02 13:00:12 +01001427
Takashi Iwai372d8552017-01-31 13:57:58 +01001428 kfree(intelhaddata->chmap->chmap);
1429 intelhaddata->chmap->chmap = NULL;
Takashi Iwai2d42c032017-02-15 22:08:21 +01001430
1431 /* Report to above ALSA layer */
1432 substream = had_substream_get(intelhaddata);
1433 if (substream) {
1434 snd_pcm_stop_xrun(substream);
1435 had_substream_put(intelhaddata);
1436 }
1437
1438 snd_jack_report(intelhaddata->jack, 0);
Takashi Iwai372d8552017-01-31 13:57:58 +01001439}
1440
Takashi Iwai73997b02017-02-02 17:38:50 +01001441/*
1442 * ALSA iec958 and ELD controls
1443 */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301444
Jerome Anand5dab11d2017-01-25 04:27:52 +05301445static int had_iec958_info(struct snd_kcontrol *kcontrol,
1446 struct snd_ctl_elem_info *uinfo)
1447{
1448 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1449 uinfo->count = 1;
1450 return 0;
1451}
1452
1453static int had_iec958_get(struct snd_kcontrol *kcontrol,
1454 struct snd_ctl_elem_value *ucontrol)
1455{
1456 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1457
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001458 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301459 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1460 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1461 ucontrol->value.iec958.status[2] =
1462 (intelhaddata->aes_bits >> 16) & 0xff;
1463 ucontrol->value.iec958.status[3] =
1464 (intelhaddata->aes_bits >> 24) & 0xff;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001465 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301466 return 0;
1467}
Takashi Iwai372d8552017-01-31 13:57:58 +01001468
Jerome Anand5dab11d2017-01-25 04:27:52 +05301469static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1470 struct snd_ctl_elem_value *ucontrol)
1471{
1472 ucontrol->value.iec958.status[0] = 0xff;
1473 ucontrol->value.iec958.status[1] = 0xff;
1474 ucontrol->value.iec958.status[2] = 0xff;
1475 ucontrol->value.iec958.status[3] = 0xff;
1476 return 0;
1477}
Takashi Iwai372d8552017-01-31 13:57:58 +01001478
Jerome Anand5dab11d2017-01-25 04:27:52 +05301479static int had_iec958_put(struct snd_kcontrol *kcontrol,
1480 struct snd_ctl_elem_value *ucontrol)
1481{
1482 unsigned int val;
1483 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001484 int changed = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301485
Jerome Anand5dab11d2017-01-25 04:27:52 +05301486 val = (ucontrol->value.iec958.status[0] << 0) |
1487 (ucontrol->value.iec958.status[1] << 8) |
1488 (ucontrol->value.iec958.status[2] << 16) |
1489 (ucontrol->value.iec958.status[3] << 24);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001490 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301491 if (intelhaddata->aes_bits != val) {
1492 intelhaddata->aes_bits = val;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001493 changed = 1;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301494 }
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001495 mutex_unlock(&intelhaddata->mutex);
1496 return changed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301497}
1498
Takashi Iwai4aedb942017-02-02 16:38:39 +01001499static int had_ctl_eld_info(struct snd_kcontrol *kcontrol,
1500 struct snd_ctl_elem_info *uinfo)
1501{
1502 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1503 uinfo->count = HDMI_MAX_ELD_BYTES;
1504 return 0;
1505}
1506
1507static int had_ctl_eld_get(struct snd_kcontrol *kcontrol,
1508 struct snd_ctl_elem_value *ucontrol)
1509{
1510 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1511
1512 mutex_lock(&intelhaddata->mutex);
1513 memcpy(ucontrol->value.bytes.data, intelhaddata->eld,
1514 HDMI_MAX_ELD_BYTES);
1515 mutex_unlock(&intelhaddata->mutex);
1516 return 0;
1517}
1518
Takashi Iwai73997b02017-02-02 17:38:50 +01001519static const struct snd_kcontrol_new had_controls[] = {
Takashi Iwai4aedb942017-02-02 16:38:39 +01001520 {
1521 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1522 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1523 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1524 .info = had_iec958_info, /* shared */
1525 .get = had_iec958_mask_get,
1526 },
1527 {
1528 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1529 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1530 .info = had_iec958_info,
1531 .get = had_iec958_get,
1532 .put = had_iec958_put,
1533 },
1534 {
1535 .access = (SNDRV_CTL_ELEM_ACCESS_READ |
1536 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1537 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1538 .name = "ELD",
1539 .info = had_ctl_eld_info,
1540 .get = had_ctl_eld_get,
1541 },
Jerome Anand5dab11d2017-01-25 04:27:52 +05301542};
1543
Takashi Iwai73997b02017-02-02 17:38:50 +01001544/*
1545 * audio interrupt handler
1546 */
Takashi Iwaida864802017-01-31 13:52:22 +01001547static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id)
1548{
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001549 struct snd_intelhad_card *card_ctx = dev_id;
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001550 u32 audio_stat[3] = {};
1551 int pipe, port;
Takashi Iwaida864802017-01-31 13:52:22 +01001552
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001553 for_each_pipe(card_ctx, pipe) {
1554 /* use raw register access to ack IRQs even while disconnected */
1555 audio_stat[pipe] = had_read_register_raw(card_ctx, pipe,
1556 AUD_HDMI_STATUS) &
1557 (HDMI_AUDIO_UNDERRUN | HDMI_AUDIO_BUFFER_DONE);
Takashi Iwaida864802017-01-31 13:52:22 +01001558
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001559 if (audio_stat[pipe])
1560 had_write_register_raw(card_ctx, pipe,
1561 AUD_HDMI_STATUS, audio_stat[pipe]);
Takashi Iwaida864802017-01-31 13:52:22 +01001562 }
1563
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001564 for_each_port(card_ctx, port) {
1565 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001566 int pipe = ctx->pipe;
Takashi Iwaida864802017-01-31 13:52:22 +01001567
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001568 if (pipe < 0)
1569 continue;
Takashi Iwaida864802017-01-31 13:52:22 +01001570
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001571 if (audio_stat[pipe] & HDMI_AUDIO_BUFFER_DONE)
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001572 had_process_buffer_done(ctx);
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001573 if (audio_stat[pipe] & HDMI_AUDIO_UNDERRUN)
1574 had_process_buffer_underrun(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001575 }
1576
1577 return IRQ_HANDLED;
1578}
1579
Takashi Iwai73997b02017-02-02 17:38:50 +01001580/*
1581 * monitor plug/unplug notification from i915; just kick off the work
1582 */
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001583static void notify_audio_lpe(struct platform_device *pdev, int port)
Takashi Iwaida864802017-01-31 13:52:22 +01001584{
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001585 struct snd_intelhad_card *card_ctx = platform_get_drvdata(pdev);
Takashi Iwai7229b122018-02-20 17:33:45 +01001586 struct snd_intelhad *ctx;
1587
1588 ctx = &card_ctx->pcm_ctx[single_port ? 0 : port];
1589 if (single_port)
1590 ctx->port = port;
Takashi Iwaida864802017-01-31 13:52:22 +01001591
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001592 schedule_work(&ctx->hdmi_audio_wq);
1593}
Takashi Iwaida864802017-01-31 13:52:22 +01001594
Takashi Iwai73997b02017-02-02 17:38:50 +01001595/* the work to handle monitor hot plug/unplug */
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001596static void had_audio_wq(struct work_struct *work)
1597{
1598 struct snd_intelhad *ctx =
1599 container_of(work, struct snd_intelhad, hdmi_audio_wq);
1600 struct intel_hdmi_lpe_audio_pdata *pdata = ctx->dev->platform_data;
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001601 struct intel_hdmi_lpe_audio_port_pdata *ppdata = &pdata->port[ctx->port];
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001602
Takashi Iwai182cdf22017-02-02 14:43:39 +01001603 pm_runtime_get_sync(ctx->dev);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001604 mutex_lock(&ctx->mutex);
Ville Syrjäläa8562e42017-04-27 19:02:27 +03001605 if (ppdata->pipe < 0) {
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001606 dev_dbg(ctx->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG : port = %d\n",
1607 __func__, ctx->port);
1608
Takashi Iwai4aedb942017-02-02 16:38:39 +01001609 memset(ctx->eld, 0, sizeof(ctx->eld)); /* clear the old ELD */
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001610
1611 ctx->dp_output = false;
1612 ctx->tmds_clock_speed = 0;
1613 ctx->link_rate = 0;
1614
1615 /* Shut down the stream */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001616 had_process_hot_unplug(ctx);
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001617
1618 ctx->pipe = -1;
Takashi Iwaida864802017-01-31 13:52:22 +01001619 } else {
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001620 dev_dbg(ctx->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001621 __func__, ctx->port, ppdata->ls_clock);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001622
Ville Syrjäläa8562e42017-04-27 19:02:27 +03001623 memcpy(ctx->eld, ppdata->eld, sizeof(ctx->eld));
Takashi Iwaida864802017-01-31 13:52:22 +01001624
Ville Syrjäläa8562e42017-04-27 19:02:27 +03001625 ctx->dp_output = ppdata->dp_output;
Ville Syrjäläc98ec5b2017-04-27 19:02:24 +03001626 if (ctx->dp_output) {
1627 ctx->tmds_clock_speed = 0;
Ville Syrjäläa8562e42017-04-27 19:02:27 +03001628 ctx->link_rate = ppdata->ls_clock;
Ville Syrjäläc98ec5b2017-04-27 19:02:24 +03001629 } else {
Ville Syrjäläa8562e42017-04-27 19:02:27 +03001630 ctx->tmds_clock_speed = ppdata->ls_clock;
Ville Syrjäläc98ec5b2017-04-27 19:02:24 +03001631 ctx->link_rate = 0;
Takashi Iwaida864802017-01-31 13:52:22 +01001632 }
1633
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001634 /*
1635 * Shut down the stream before we change
1636 * the pipe assignment for this pcm device
1637 */
Takashi Iwaida864802017-01-31 13:52:22 +01001638 had_process_hot_plug(ctx);
1639
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001640 ctx->pipe = ppdata->pipe;
1641
1642 /* Restart the stream if necessary */
Takashi Iwaib5562902017-02-04 22:05:33 +01001643 had_process_mode_change(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001644 }
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001645
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001646 mutex_unlock(&ctx->mutex);
Takashi Iwai3002b9f2017-02-13 09:52:44 +01001647 pm_runtime_mark_last_busy(ctx->dev);
1648 pm_runtime_put_autosuspend(ctx->dev);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001649}
1650
1651/*
Takashi Iwaib9bacf22017-02-14 12:29:38 +01001652 * Jack interface
1653 */
Ville Syrjäläbb4ac5a2017-04-27 19:02:28 +03001654static int had_create_jack(struct snd_intelhad *ctx,
1655 struct snd_pcm *pcm)
Takashi Iwaib9bacf22017-02-14 12:29:38 +01001656{
Ville Syrjäläbb4ac5a2017-04-27 19:02:28 +03001657 char hdmi_str[32];
Takashi Iwaib9bacf22017-02-14 12:29:38 +01001658 int err;
1659
Ville Syrjäläbb4ac5a2017-04-27 19:02:28 +03001660 snprintf(hdmi_str, sizeof(hdmi_str),
1661 "HDMI/DP,pcm=%d", pcm->device);
1662
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001663 err = snd_jack_new(ctx->card_ctx->card, hdmi_str,
1664 SND_JACK_AVOUT, &ctx->jack,
Takashi Iwaib9bacf22017-02-14 12:29:38 +01001665 true, false);
1666 if (err < 0)
1667 return err;
1668 ctx->jack->private_data = ctx;
1669 return 0;
1670}
1671
1672/*
Takashi Iwai182cdf22017-02-02 14:43:39 +01001673 * PM callbacks
1674 */
1675
1676static int hdmi_lpe_audio_runtime_suspend(struct device *dev)
1677{
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001678 struct snd_intelhad_card *card_ctx = dev_get_drvdata(dev);
1679 int port;
Takashi Iwai182cdf22017-02-02 14:43:39 +01001680
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001681 for_each_port(card_ctx, port) {
1682 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
1683 struct snd_pcm_substream *substream;
1684
1685 substream = had_substream_get(ctx);
1686 if (substream) {
1687 snd_pcm_suspend(substream);
1688 had_substream_put(ctx);
1689 }
Takashi Iwai182cdf22017-02-02 14:43:39 +01001690 }
1691
1692 return 0;
1693}
1694
Arnd Bergmann1df98922017-02-07 14:38:51 +01001695static int __maybe_unused hdmi_lpe_audio_suspend(struct device *dev)
Takashi Iwai182cdf22017-02-02 14:43:39 +01001696{
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001697 struct snd_intelhad_card *card_ctx = dev_get_drvdata(dev);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001698 int err;
1699
1700 err = hdmi_lpe_audio_runtime_suspend(dev);
1701 if (!err)
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001702 snd_power_change_state(card_ctx->card, SNDRV_CTL_POWER_D3hot);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001703 return err;
1704}
1705
Takashi Iwai3002b9f2017-02-13 09:52:44 +01001706static int hdmi_lpe_audio_runtime_resume(struct device *dev)
1707{
1708 pm_runtime_mark_last_busy(dev);
1709 return 0;
1710}
1711
Arnd Bergmann1df98922017-02-07 14:38:51 +01001712static int __maybe_unused hdmi_lpe_audio_resume(struct device *dev)
Takashi Iwai182cdf22017-02-02 14:43:39 +01001713{
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001714 struct snd_intelhad_card *card_ctx = dev_get_drvdata(dev);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001715
Takashi Iwai3002b9f2017-02-13 09:52:44 +01001716 hdmi_lpe_audio_runtime_resume(dev);
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001717 snd_power_change_state(card_ctx->card, SNDRV_CTL_POWER_D0);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001718 return 0;
Takashi Iwaida864802017-01-31 13:52:22 +01001719}
1720
1721/* release resources */
1722static void hdmi_lpe_audio_free(struct snd_card *card)
1723{
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001724 struct snd_intelhad_card *card_ctx = card->private_data;
1725 struct intel_hdmi_lpe_audio_pdata *pdata = card_ctx->dev->platform_data;
1726 int port;
Takashi Iwaida864802017-01-31 13:52:22 +01001727
Ville Syrjälä8d5c3032017-04-27 19:02:21 +03001728 spin_lock_irq(&pdata->lpe_audio_slock);
1729 pdata->notify_audio_lpe = NULL;
1730 spin_unlock_irq(&pdata->lpe_audio_slock);
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001731
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001732 for_each_port(card_ctx, port) {
1733 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
Takashi Iwaida864802017-01-31 13:52:22 +01001734
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001735 cancel_work_sync(&ctx->hdmi_audio_wq);
1736 }
1737
1738 if (card_ctx->mmio_start)
1739 iounmap(card_ctx->mmio_start);
1740 if (card_ctx->irq >= 0)
1741 free_irq(card_ctx->irq, card_ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001742}
1743
1744/*
1745 * hdmi_lpe_audio_probe - start bridge with i915
1746 *
1747 * This function is called when the i915 driver creates the
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001748 * hdmi-lpe-audio platform device.
Takashi Iwaida864802017-01-31 13:52:22 +01001749 */
1750static int hdmi_lpe_audio_probe(struct platform_device *pdev)
1751{
1752 struct snd_card *card;
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001753 struct snd_intelhad_card *card_ctx;
Takashi Iwaic77a6ed2018-02-28 08:46:00 +01001754 struct snd_intelhad *ctx;
Takashi Iwaida864802017-01-31 13:52:22 +01001755 struct snd_pcm *pcm;
1756 struct intel_hdmi_lpe_audio_pdata *pdata;
1757 int irq;
1758 struct resource *res_mmio;
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001759 int port, ret;
Takashi Iwaida864802017-01-31 13:52:22 +01001760
Takashi Iwaida864802017-01-31 13:52:22 +01001761 pdata = pdev->dev.platform_data;
1762 if (!pdata) {
1763 dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
1764 return -EINVAL;
1765 }
1766
1767 /* get resources */
1768 irq = platform_get_irq(pdev, 0);
1769 if (irq < 0) {
Gustavo A. R. Silva19671582017-06-30 15:18:41 -05001770 dev_err(&pdev->dev, "Could not get irq resource: %d\n", irq);
1771 return irq;
Takashi Iwaida864802017-01-31 13:52:22 +01001772 }
1773
1774 res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1775 if (!res_mmio) {
1776 dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
1777 return -ENXIO;
1778 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301779
Takashi Iwai5647aec2017-01-31 08:14:34 +01001780 /* create a card instance with ALSA framework */
Takashi Iwaida864802017-01-31 13:52:22 +01001781 ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001782 THIS_MODULE, sizeof(*card_ctx), &card);
Takashi Iwaida864802017-01-31 13:52:22 +01001783 if (ret)
1784 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301785
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001786 card_ctx = card->private_data;
1787 card_ctx->dev = &pdev->dev;
1788 card_ctx->card = card;
Takashi Iwaida864802017-01-31 13:52:22 +01001789 strcpy(card->driver, INTEL_HAD);
Takashi Iwai873ab032017-02-07 12:14:04 +01001790 strcpy(card->shortname, "Intel HDMI/DP LPE Audio");
1791 strcpy(card->longname, "Intel HDMI/DP LPE Audio");
Jerome Anand5dab11d2017-01-25 04:27:52 +05301792
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001793 card_ctx->irq = -1;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301794
Takashi Iwaida864802017-01-31 13:52:22 +01001795 card->private_free = hdmi_lpe_audio_free;
1796
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001797 platform_set_drvdata(pdev, card_ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001798
Takashi Iwaic77a6ed2018-02-28 08:46:00 +01001799 card_ctx->num_pipes = pdata->num_pipes;
1800 card_ctx->num_ports = single_port ? 1 : pdata->num_ports;
1801
1802 for_each_port(card_ctx, port) {
1803 ctx = &card_ctx->pcm_ctx[port];
1804 ctx->card_ctx = card_ctx;
1805 ctx->dev = card_ctx->dev;
1806 ctx->port = single_port ? -1 : port;
1807 ctx->pipe = -1;
1808
1809 spin_lock_init(&ctx->had_spinlock);
1810 mutex_init(&ctx->mutex);
1811 INIT_WORK(&ctx->hdmi_audio_wq, had_audio_wq);
1812 }
1813
Takashi Iwaida864802017-01-31 13:52:22 +01001814 dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
1815 __func__, (unsigned int)res_mmio->start,
1816 (unsigned int)res_mmio->end);
1817
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001818 card_ctx->mmio_start = ioremap_nocache(res_mmio->start,
1819 (size_t)(resource_size(res_mmio)));
1820 if (!card_ctx->mmio_start) {
Takashi Iwaida864802017-01-31 13:52:22 +01001821 dev_err(&pdev->dev, "Could not get ioremap\n");
1822 ret = -EACCES;
1823 goto err;
1824 }
1825
1826 /* setup interrupt handler */
1827 ret = request_irq(irq, display_pipe_interrupt_handler, 0,
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001828 pdev->name, card_ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001829 if (ret < 0) {
1830 dev_err(&pdev->dev, "request_irq failed\n");
1831 goto err;
1832 }
1833
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001834 card_ctx->irq = irq;
Takashi Iwai412bbe7d52017-02-02 22:03:22 +01001835
1836 /* only 32bit addressable */
1837 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
1838 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1839
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001840 init_channel_allocations();
Jerome Anand5dab11d2017-01-25 04:27:52 +05301841
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001842 card_ctx->num_pipes = pdata->num_pipes;
Takashi Iwai7229b122018-02-20 17:33:45 +01001843 card_ctx->num_ports = single_port ? 1 : pdata->num_ports;
Ville Syrjäläbb4ac5a2017-04-27 19:02:28 +03001844
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001845 for_each_port(card_ctx, port) {
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001846 int i;
1847
Takashi Iwaic77a6ed2018-02-28 08:46:00 +01001848 ctx = &card_ctx->pcm_ctx[port];
Ville Syrjälä8a2d6ae2017-04-27 19:02:30 +03001849 ret = snd_pcm_new(card, INTEL_HAD, port, MAX_PB_STREAMS,
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001850 MAX_CAP_STREAMS, &pcm);
1851 if (ret)
Ville Syrjäläbb4ac5a2017-04-27 19:02:28 +03001852 goto err;
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001853
1854 /* setup private data which can be retrieved when required */
1855 pcm->private_data = ctx;
1856 pcm->info_flags = 0;
1857 strncpy(pcm->name, card->shortname, strlen(card->shortname));
1858 /* setup the ops for playabck */
1859 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &had_pcm_ops);
1860
1861 /* allocate dma pages;
1862 * try to allocate 600k buffer as default which is large enough
1863 */
1864 snd_pcm_lib_preallocate_pages_for_all(pcm,
1865 SNDRV_DMA_TYPE_DEV, NULL,
1866 HAD_DEFAULT_BUFFER, HAD_MAX_BUFFER);
1867
1868 /* create controls */
1869 for (i = 0; i < ARRAY_SIZE(had_controls); i++) {
1870 struct snd_kcontrol *kctl;
1871
1872 kctl = snd_ctl_new1(&had_controls[i], ctx);
1873 if (!kctl) {
1874 ret = -ENOMEM;
1875 goto err;
1876 }
1877
1878 kctl->id.device = pcm->device;
1879
1880 ret = snd_ctl_add(card, kctl);
1881 if (ret < 0)
1882 goto err;
Ville Syrjäläbb4ac5a2017-04-27 19:02:28 +03001883 }
1884
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001885 /* Register channel map controls */
1886 ret = had_register_chmap_ctls(ctx, pcm);
1887 if (ret < 0)
1888 goto err;
Ville Syrjäläbb4ac5a2017-04-27 19:02:28 +03001889
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001890 ret = had_create_jack(ctx, pcm);
Takashi Iwai4aedb942017-02-02 16:38:39 +01001891 if (ret < 0)
1892 goto err;
1893 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301894
Takashi Iwaida864802017-01-31 13:52:22 +01001895 ret = snd_card_register(card);
1896 if (ret)
Takashi Iwai36ec0d92017-01-31 08:47:05 +01001897 goto err;
1898
Takashi Iwaibcce7752017-02-01 17:18:20 +01001899 spin_lock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001900 pdata->notify_audio_lpe = notify_audio_lpe;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001901 spin_unlock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001902
Takashi Iwai3002b9f2017-02-13 09:52:44 +01001903 pm_runtime_use_autosuspend(&pdev->dev);
1904 pm_runtime_mark_last_busy(&pdev->dev);
Takashi Iwaida864802017-01-31 13:52:22 +01001905 pm_runtime_set_active(&pdev->dev);
Takashi Iwaida864802017-01-31 13:52:22 +01001906
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001907 dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001908 for_each_port(card_ctx, port) {
1909 struct snd_intelhad *ctx = &card_ctx->pcm_ctx[port];
1910
1911 schedule_work(&ctx->hdmi_audio_wq);
1912 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301913
Takashi Iwai79dda752017-01-30 17:23:39 +01001914 return 0;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001915
Jerome Anand5dab11d2017-01-25 04:27:52 +05301916err:
1917 snd_card_free(card);
Takashi Iwaida864802017-01-31 13:52:22 +01001918 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301919}
1920
Takashi Iwai79dda752017-01-30 17:23:39 +01001921/*
Takashi Iwaida864802017-01-31 13:52:22 +01001922 * hdmi_lpe_audio_remove - stop bridge with i915
Jerome Anand5dab11d2017-01-25 04:27:52 +05301923 *
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001924 * This function is called when the platform device is destroyed.
Jerome Anand5dab11d2017-01-25 04:27:52 +05301925 */
Takashi Iwaida864802017-01-31 13:52:22 +01001926static int hdmi_lpe_audio_remove(struct platform_device *pdev)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301927{
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001928 struct snd_intelhad_card *card_ctx = platform_get_drvdata(pdev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301929
Ville Syrjäläb4eb0d52017-04-27 19:02:29 +03001930 snd_card_free(card_ctx->card);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301931 return 0;
1932}
1933
Takashi Iwai182cdf22017-02-02 14:43:39 +01001934static const struct dev_pm_ops hdmi_lpe_audio_pm = {
1935 SET_SYSTEM_SLEEP_PM_OPS(hdmi_lpe_audio_suspend, hdmi_lpe_audio_resume)
Takashi Iwai3002b9f2017-02-13 09:52:44 +01001936 SET_RUNTIME_PM_OPS(hdmi_lpe_audio_runtime_suspend,
1937 hdmi_lpe_audio_runtime_resume, NULL)
Takashi Iwai182cdf22017-02-02 14:43:39 +01001938};
1939
Takashi Iwaida864802017-01-31 13:52:22 +01001940static struct platform_driver hdmi_lpe_audio_driver = {
1941 .driver = {
1942 .name = "hdmi-lpe-audio",
Takashi Iwai182cdf22017-02-02 14:43:39 +01001943 .pm = &hdmi_lpe_audio_pm,
Takashi Iwaida864802017-01-31 13:52:22 +01001944 },
1945 .probe = hdmi_lpe_audio_probe,
1946 .remove = hdmi_lpe_audio_remove,
Takashi Iwaida864802017-01-31 13:52:22 +01001947};
1948
1949module_platform_driver(hdmi_lpe_audio_driver);
1950MODULE_ALIAS("platform:hdmi_lpe_audio");
1951
Jerome Anand5dab11d2017-01-25 04:27:52 +05301952MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1953MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1954MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1955MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1956MODULE_DESCRIPTION("Intel HDMI Audio driver");
1957MODULE_LICENSE("GPL v2");
1958MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");