blob: d2136498defe02601fc2556ab4b99fa840583fe9 [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>
Jerome Anand5dab11d2017-01-25 04:27:52 +053032#include <asm/cacheflush.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053033#include <sound/core.h>
Takashi Iwai03c34372017-02-02 16:19:03 +010034#include <sound/asoundef.h>
35#include <sound/pcm.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053036#include <sound/pcm_params.h>
37#include <sound/initval.h>
38#include <sound/control.h>
Takashi Iwai03c34372017-02-02 16:19:03 +010039#include <drm/drm_edid.h>
Takashi Iwaida864802017-01-31 13:52:22 +010040#include <drm/intel_lpe_audio.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053041#include "intel_hdmi_audio.h"
42
Jerome Anand5dab11d2017-01-25 04:27:52 +053043/*standard module options for ALSA. This module supports only one card*/
44static int hdmi_card_index = SNDRV_DEFAULT_IDX1;
45static char *hdmi_card_id = SNDRV_DEFAULT_STR1;
Jerome Anand5dab11d2017-01-25 04:27:52 +053046
47module_param_named(index, hdmi_card_index, int, 0444);
48MODULE_PARM_DESC(index,
49 "Index value for INTEL Intel HDMI Audio controller.");
50module_param_named(id, hdmi_card_id, charp, 0444);
51MODULE_PARM_DESC(id,
52 "ID string for INTEL Intel HDMI Audio controller.");
53
54/*
55 * ELD SA bits in the CEA Speaker Allocation data block
56 */
Takashi Iwai4a5ddb22017-02-01 16:45:38 +010057static const int eld_speaker_allocation_bits[] = {
Jerome Anand5dab11d2017-01-25 04:27:52 +053058 [0] = FL | FR,
59 [1] = LFE,
60 [2] = FC,
61 [3] = RL | RR,
62 [4] = RC,
63 [5] = FLC | FRC,
64 [6] = RLC | RRC,
65 /* the following are not defined in ELD yet */
66 [7] = 0,
67};
68
69/*
70 * This is an ordered list!
71 *
72 * The preceding ones have better chances to be selected by
73 * hdmi_channel_allocation().
74 */
75static struct cea_channel_speaker_allocation channel_allocations[] = {
76/* channel: 7 6 5 4 3 2 1 0 */
77{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
78 /* 2.1 */
79{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
80 /* Dolby Surround */
81{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
82 /* surround40 */
83{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
84 /* surround41 */
85{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
86 /* surround50 */
87{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
88 /* surround51 */
89{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
90 /* 6.1 */
91{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
92 /* surround71 */
93{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
94
95{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
96{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
97{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
98{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
99{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
100{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
101{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
102{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
103{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
104{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
105{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
106{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
107{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
108{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
109{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
110{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
111{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
112{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
113{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
114{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
115{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
116{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
117{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
118};
119
Takashi Iwai4a5ddb22017-02-01 16:45:38 +0100120static const struct channel_map_table map_tables[] = {
Jerome Anand5dab11d2017-01-25 04:27:52 +0530121 { SNDRV_CHMAP_FL, 0x00, FL },
122 { SNDRV_CHMAP_FR, 0x01, FR },
123 { SNDRV_CHMAP_RL, 0x04, RL },
124 { SNDRV_CHMAP_RR, 0x05, RR },
125 { SNDRV_CHMAP_LFE, 0x02, LFE },
126 { SNDRV_CHMAP_FC, 0x03, FC },
127 { SNDRV_CHMAP_RLC, 0x06, RLC },
128 { SNDRV_CHMAP_RRC, 0x07, RRC },
129 {} /* terminator */
130};
131
132/* hardware capability structure */
Takashi Iwaib5562902017-02-04 22:05:33 +0100133static const struct snd_pcm_hardware had_pcm_hardware = {
Jerome Anand5dab11d2017-01-25 04:27:52 +0530134 .info = (SNDRV_PCM_INFO_INTERLEAVED |
Takashi Iwaia9ebdd02017-02-02 21:33:54 +0100135 SNDRV_PCM_INFO_MMAP |
136 SNDRV_PCM_INFO_MMAP_VALID),
Takashi Iwai3e21a762017-02-07 08:13:50 +0100137 .formats = SNDRV_PCM_FMTBIT_S24,
Jerome Anand5dab11d2017-01-25 04:27:52 +0530138 .rates = SNDRV_PCM_RATE_32000 |
139 SNDRV_PCM_RATE_44100 |
140 SNDRV_PCM_RATE_48000 |
141 SNDRV_PCM_RATE_88200 |
142 SNDRV_PCM_RATE_96000 |
143 SNDRV_PCM_RATE_176400 |
144 SNDRV_PCM_RATE_192000,
145 .rate_min = HAD_MIN_RATE,
146 .rate_max = HAD_MAX_RATE,
147 .channels_min = HAD_MIN_CHANNEL,
148 .channels_max = HAD_MAX_CHANNEL,
149 .buffer_bytes_max = HAD_MAX_BUFFER,
150 .period_bytes_min = HAD_MIN_PERIOD_BYTES,
151 .period_bytes_max = HAD_MAX_PERIOD_BYTES,
152 .periods_min = HAD_MIN_PERIODS,
153 .periods_max = HAD_MAX_PERIODS,
154 .fifo_size = HAD_FIFO_SIZE,
155};
156
Takashi Iwai313d9f22017-02-02 13:00:12 +0100157/* Get the active PCM substream;
158 * Call had_substream_put() for unreferecing.
159 * Don't call this inside had_spinlock, as it takes by itself
160 */
161static struct snd_pcm_substream *
162had_substream_get(struct snd_intelhad *intelhaddata)
163{
164 struct snd_pcm_substream *substream;
165 unsigned long flags;
166
167 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
168 substream = intelhaddata->stream_info.substream;
169 if (substream)
170 intelhaddata->stream_info.substream_refcount++;
171 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
172 return substream;
173}
174
175/* Unref the active PCM substream;
176 * Don't call this inside had_spinlock, as it takes by itself
177 */
178static void had_substream_put(struct snd_intelhad *intelhaddata)
179{
180 unsigned long flags;
181
182 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
183 intelhaddata->stream_info.substream_refcount--;
184 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
185}
186
Jerome Anand5dab11d2017-01-25 04:27:52 +0530187/* Register access functions */
Takashi Iwai83af57d2017-02-03 08:50:06 +0100188static void had_read_register(struct snd_intelhad *ctx, u32 reg, u32 *val)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530189{
Takashi Iwaida864802017-01-31 13:52:22 +0100190 *val = ioread32(ctx->mmio_start + ctx->had_config_offset + reg);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530191}
192
Takashi Iwai83af57d2017-02-03 08:50:06 +0100193static void had_write_register(struct snd_intelhad *ctx, u32 reg, u32 val)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530194{
Takashi Iwaida864802017-01-31 13:52:22 +0100195 iowrite32(val, ctx->mmio_start + ctx->had_config_offset + reg);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530196}
197
Takashi Iwaida864802017-01-31 13:52:22 +0100198/*
Takashi Iwai313d9f22017-02-02 13:00:12 +0100199 * enable / disable audio configuration
200 *
Takashi Iwai83af57d2017-02-03 08:50:06 +0100201 * The normal read/modify should not directly be used on VLV2 for
Takashi Iwaida864802017-01-31 13:52:22 +0100202 * updating AUD_CONFIG register.
Jerome Anand5dab11d2017-01-25 04:27:52 +0530203 * This is because:
204 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
205 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
206 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
207 * register. This field should be 1xy binary for configuration with 6 or
208 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
209 * causes the "channels" field to be updated as 0xy binary resulting in
210 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
211 * appropriate value when doing read-modify of AUD_CONFIG register.
Jerome Anand5dab11d2017-01-25 04:27:52 +0530212 */
Takashi Iwai40ce4b52017-02-07 16:17:06 +0100213static void had_enable_audio(struct snd_intelhad *intelhaddata,
Takashi Iwaib5562902017-02-04 22:05:33 +0100214 bool enable)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530215{
Takashi Iwai40ce4b52017-02-07 16:17:06 +0100216 /* update the cached value */
217 intelhaddata->aud_config.regx.aud_en = enable;
218 had_write_register(intelhaddata, AUD_CONFIG,
219 intelhaddata->aud_config.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530220}
221
Takashi Iwai075a1d42017-02-07 07:55:27 +0100222/* forcibly ACKs to both BUFFER_DONE and BUFFER_UNDERRUN interrupts */
223static void had_ack_irqs(struct snd_intelhad *ctx)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530224{
Takashi Iwaida864802017-01-31 13:52:22 +0100225 u32 status_reg;
226
Takashi Iwai075a1d42017-02-07 07:55:27 +0100227 had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
228 status_reg |= HDMI_AUDIO_BUFFER_DONE | HDMI_AUDIO_UNDERRUN;
229 had_write_register(ctx, AUD_HDMI_STATUS, status_reg);
230 had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
Takashi Iwaida864802017-01-31 13:52:22 +0100231}
232
Takashi Iwaif4566aa2017-02-04 21:39:56 +0100233/* Reset buffer pointers */
234static void had_reset_audio(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530235{
Takashi Iwai77531be2017-02-07 12:17:23 +0100236 had_write_register(intelhaddata, AUD_HDMI_STATUS,
237 AUD_HDMI_STATUSG_MASK_FUNCRST);
Takashi Iwaif4566aa2017-02-04 21:39:56 +0100238 had_write_register(intelhaddata, AUD_HDMI_STATUS, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530239}
240
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100241/*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530242 * initialize audio channel status registers
243 * This function is called in the prepare callback
244 */
245static int had_prog_status_reg(struct snd_pcm_substream *substream,
246 struct snd_intelhad *intelhaddata)
247{
Takashi Iwai7ceba752017-02-02 15:58:35 +0100248 union aud_cfg cfg_val = {.regval = 0};
249 union aud_ch_status_0 ch_stat0 = {.regval = 0};
250 union aud_ch_status_1 ch_stat1 = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530251 int format;
252
Takashi Iwai7ceba752017-02-02 15:58:35 +0100253 ch_stat0.regx.lpcm_id = (intelhaddata->aes_bits &
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100254 IEC958_AES0_NONAUDIO) >> 1;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100255 ch_stat0.regx.clk_acc = (intelhaddata->aes_bits &
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100256 IEC958_AES3_CON_CLOCK) >> 4;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100257 cfg_val.regx.val_bit = ch_stat0.regx.lpcm_id;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530258
259 switch (substream->runtime->rate) {
260 case AUD_SAMPLE_RATE_32:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100261 ch_stat0.regx.samp_freq = CH_STATUS_MAP_32KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530262 break;
263
264 case AUD_SAMPLE_RATE_44_1:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100265 ch_stat0.regx.samp_freq = CH_STATUS_MAP_44KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530266 break;
267 case AUD_SAMPLE_RATE_48:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100268 ch_stat0.regx.samp_freq = CH_STATUS_MAP_48KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530269 break;
270 case AUD_SAMPLE_RATE_88_2:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100271 ch_stat0.regx.samp_freq = CH_STATUS_MAP_88KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530272 break;
273 case AUD_SAMPLE_RATE_96:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100274 ch_stat0.regx.samp_freq = CH_STATUS_MAP_96KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530275 break;
276 case AUD_SAMPLE_RATE_176_4:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100277 ch_stat0.regx.samp_freq = CH_STATUS_MAP_176KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530278 break;
279 case AUD_SAMPLE_RATE_192:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100280 ch_stat0.regx.samp_freq = CH_STATUS_MAP_192KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530281 break;
282
283 default:
284 /* control should never come here */
285 return -EINVAL;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530286 }
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100287
Takashi Iwai79dda752017-01-30 17:23:39 +0100288 had_write_register(intelhaddata,
Takashi Iwai7ceba752017-02-02 15:58:35 +0100289 AUD_CH_STATUS_0, ch_stat0.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530290
291 format = substream->runtime->format;
292
293 if (format == SNDRV_PCM_FORMAT_S16_LE) {
Takashi Iwai7ceba752017-02-02 15:58:35 +0100294 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_20;
295 ch_stat1.regx.wrd_len = SMPL_WIDTH_16BITS;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530296 } else if (format == SNDRV_PCM_FORMAT_S24_LE) {
Takashi Iwai7ceba752017-02-02 15:58:35 +0100297 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_24;
298 ch_stat1.regx.wrd_len = SMPL_WIDTH_24BITS;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530299 } else {
Takashi Iwai7ceba752017-02-02 15:58:35 +0100300 ch_stat1.regx.max_wrd_len = 0;
301 ch_stat1.regx.wrd_len = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530302 }
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100303
Takashi Iwai79dda752017-01-30 17:23:39 +0100304 had_write_register(intelhaddata,
Takashi Iwai7ceba752017-02-02 15:58:35 +0100305 AUD_CH_STATUS_1, ch_stat1.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530306 return 0;
307}
308
Takashi Iwai76296ef2017-01-30 16:09:11 +0100309/*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530310 * function to initialize audio
311 * registers and buffer confgiuration registers
312 * This function is called in the prepare callback
313 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100314static int had_init_audio_ctrl(struct snd_pcm_substream *substream,
315 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530316{
Takashi Iwai7ceba752017-02-02 15:58:35 +0100317 union aud_cfg cfg_val = {.regval = 0};
318 union aud_buf_config buf_cfg = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530319 u8 channels;
320
321 had_prog_status_reg(substream, intelhaddata);
322
Takashi Iwai7ceba752017-02-02 15:58:35 +0100323 buf_cfg.regx.audio_fifo_watermark = FIFO_THRESHOLD;
324 buf_cfg.regx.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
325 buf_cfg.regx.aud_delay = 0;
326 had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530327
328 channels = substream->runtime->channels;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100329 cfg_val.regx.num_ch = channels - 2;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530330 if (channels <= 2)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100331 cfg_val.regx.layout = LAYOUT0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530332 else
Takashi Iwai7ceba752017-02-02 15:58:35 +0100333 cfg_val.regx.layout = LAYOUT1;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530334
Takashi Iwai7ceba752017-02-02 15:58:35 +0100335 cfg_val.regx.val_bit = 1;
Takashi Iwai83af57d2017-02-03 08:50:06 +0100336
337 /* fix up the DP bits */
338 if (intelhaddata->dp_output) {
339 cfg_val.regx.dp_modei = 1;
340 cfg_val.regx.set = 1;
341 }
342
Takashi Iwai7ceba752017-02-02 15:58:35 +0100343 had_write_register(intelhaddata, AUD_CONFIG, cfg_val.regval);
Takashi Iwai40ce4b52017-02-07 16:17:06 +0100344 intelhaddata->aud_config = cfg_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530345 return 0;
346}
347
Jerome Anand5dab11d2017-01-25 04:27:52 +0530348/*
349 * Compute derived values in channel_allocations[].
350 */
351static void init_channel_allocations(void)
352{
353 int i, j;
354 struct cea_channel_speaker_allocation *p;
355
Jerome Anand5dab11d2017-01-25 04:27:52 +0530356 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
357 p = channel_allocations + i;
358 p->channels = 0;
359 p->spk_mask = 0;
360 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
361 if (p->speakers[j]) {
362 p->channels++;
363 p->spk_mask |= p->speakers[j];
364 }
365 }
366}
367
368/*
369 * The transformation takes two steps:
370 *
371 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
372 * spk_mask => (channel_allocations[]) => ai->CA
373 *
374 * TODO: it could select the wrong CA from multiple candidates.
375 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100376static int had_channel_allocation(struct snd_intelhad *intelhaddata,
377 int channels)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530378{
379 int i;
380 int ca = 0;
381 int spk_mask = 0;
382
383 /*
384 * CA defaults to 0 for basic stereo audio
385 */
386 if (channels <= 2)
387 return 0;
388
389 /*
390 * expand ELD's speaker allocation mask
391 *
392 * ELD tells the speaker mask in a compact(paired) form,
393 * expand ELD's notions to match the ones used by Audio InfoFrame.
394 */
395
396 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100397 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530398 spk_mask |= eld_speaker_allocation_bits[i];
399 }
400
401 /* search for the first working match in the CA table */
402 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
403 if (channels == channel_allocations[i].channels &&
404 (spk_mask & channel_allocations[i].spk_mask) ==
405 channel_allocations[i].spk_mask) {
406 ca = channel_allocations[i].ca_index;
407 break;
408 }
409 }
410
Takashi Iwaic75b0472017-01-31 15:49:15 +0100411 dev_dbg(intelhaddata->dev, "select CA 0x%x for %d\n", ca, channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530412
413 return ca;
414}
415
416/* from speaker bit mask to ALSA API channel position */
417static int spk_to_chmap(int spk)
418{
Takashi Iwai4a5ddb22017-02-01 16:45:38 +0100419 const struct channel_map_table *t = map_tables;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530420
421 for (; t->map; t++) {
422 if (t->spk_mask == spk)
423 return t->map;
424 }
425 return 0;
426}
427
Takashi Iwai372d8552017-01-31 13:57:58 +0100428static void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530429{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100430 int i, c;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530431 int spk_mask = 0;
432 struct snd_pcm_chmap_elem *chmap;
433 u8 eld_high, eld_high_mask = 0xF0;
434 u8 high_msb;
435
436 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100437 if (!chmap) {
Jerome Anand5dab11d2017-01-25 04:27:52 +0530438 intelhaddata->chmap->chmap = NULL;
439 return;
440 }
441
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100442 dev_dbg(intelhaddata->dev, "eld speaker = %x\n",
443 intelhaddata->eld[DRM_ELD_SPEAKER]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530444
445 /* WA: Fix the max channel supported to 8 */
446
447 /*
448 * Sink may support more than 8 channels, if eld_high has more than
449 * one bit set. SOC supports max 8 channels.
450 * Refer eld_speaker_allocation_bits, for sink speaker allocation
451 */
452
453 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100454 eld_high = intelhaddata->eld[DRM_ELD_SPEAKER] & eld_high_mask;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530455 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
456 /* eld_high & (eld_high-1): if more than 1 bit set */
457 /* 0x1F: 7 channels */
458 for (i = 1; i < 4; i++) {
459 high_msb = eld_high & (0x80 >> i);
460 if (high_msb) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100461 intelhaddata->eld[DRM_ELD_SPEAKER] &=
Jerome Anand5dab11d2017-01-25 04:27:52 +0530462 high_msb | 0xF;
463 break;
464 }
465 }
466 }
467
468 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100469 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530470 spk_mask |= eld_speaker_allocation_bits[i];
471 }
472
473 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
474 if (spk_mask == channel_allocations[i].spk_mask) {
475 for (c = 0; c < channel_allocations[i].channels; c++) {
476 chmap->map[c] = spk_to_chmap(
477 channel_allocations[i].speakers[
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100478 (MAX_SPEAKERS - 1) - c]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530479 }
480 chmap->channels = channel_allocations[i].channels;
481 intelhaddata->chmap->chmap = chmap;
482 break;
483 }
484 }
485 if (i >= ARRAY_SIZE(channel_allocations)) {
486 intelhaddata->chmap->chmap = NULL;
487 kfree(chmap);
488 }
489}
490
491/*
492 * ALSA API channel-map control callbacks
493 */
494static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
495 struct snd_ctl_elem_info *uinfo)
496{
497 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
498 struct snd_intelhad *intelhaddata = info->private_data;
499
Takashi Iwai91b0cb02017-02-02 17:46:49 +0100500 if (!intelhaddata->connected)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530501 return -ENODEV;
502 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
503 uinfo->count = HAD_MAX_CHANNEL;
504 uinfo->value.integer.min = 0;
505 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
506 return 0;
507}
508
509static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
510 struct snd_ctl_elem_value *ucontrol)
511{
512 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
513 struct snd_intelhad *intelhaddata = info->private_data;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100514 int i;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530515 const struct snd_pcm_chmap_elem *chmap;
516
Takashi Iwai91b0cb02017-02-02 17:46:49 +0100517 if (!intelhaddata->connected)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530518 return -ENODEV;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100519
520 mutex_lock(&intelhaddata->mutex);
521 if (!intelhaddata->chmap->chmap) {
522 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530523 return -ENODATA;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100524 }
525
Jerome Anand5dab11d2017-01-25 04:27:52 +0530526 chmap = intelhaddata->chmap->chmap;
Takashi Iwaic75b0472017-01-31 15:49:15 +0100527 for (i = 0; i < chmap->channels; i++)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530528 ucontrol->value.integer.value[i] = chmap->map[i];
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100529 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530530
531 return 0;
532}
533
534static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
535 struct snd_pcm *pcm)
536{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100537 int err;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530538
539 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
540 NULL, 0, (unsigned long)intelhaddata,
541 &intelhaddata->chmap);
542 if (err < 0)
543 return err;
544
545 intelhaddata->chmap->private_data = intelhaddata;
Takashi Iwaie9d65ab2017-01-31 16:11:27 +0100546 intelhaddata->chmap->kctl->info = had_chmap_ctl_info;
547 intelhaddata->chmap->kctl->get = had_chmap_ctl_get;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530548 intelhaddata->chmap->chmap = NULL;
549 return 0;
550}
551
Takashi Iwai76296ef2017-01-30 16:09:11 +0100552/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100553 * Initialize Data Island Packets registers
Jerome Anand5dab11d2017-01-25 04:27:52 +0530554 * This function is called in the prepare callback
555 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100556static void had_prog_dip(struct snd_pcm_substream *substream,
557 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530558{
559 int i;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100560 union aud_ctrl_st ctrl_state = {.regval = 0};
561 union aud_info_frame2 frame2 = {.regval = 0};
562 union aud_info_frame3 frame3 = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530563 u8 checksum = 0;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600564 u32 info_frame;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530565 int channels;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100566 int ca;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530567
568 channels = substream->runtime->channels;
569
Takashi Iwai7ceba752017-02-02 15:58:35 +0100570 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530571
Takashi Iwaib5562902017-02-04 22:05:33 +0100572 ca = had_channel_allocation(intelhaddata, channels);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600573 if (intelhaddata->dp_output) {
574 info_frame = DP_INFO_FRAME_WORD1;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100575 frame2.regval = (substream->runtime->channels - 1) | (ca << 24);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600576 } else {
577 info_frame = HDMI_INFO_FRAME_WORD1;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100578 frame2.regx.chnl_cnt = substream->runtime->channels - 1;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100579 frame3.regx.chnl_alloc = ca;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530580
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100581 /* Calculte the byte wide checksum for all valid DIP words */
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600582 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100583 checksum += (info_frame >> (i * 8)) & 0xff;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600584 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100585 checksum += (frame2.regval >> (i * 8)) & 0xff;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600586 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100587 checksum += (frame3.regval >> (i * 8)) & 0xff;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530588
Takashi Iwai7ceba752017-02-02 15:58:35 +0100589 frame2.regx.chksum = -(checksum);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600590 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530591
Takashi Iwai4151ee82017-01-31 18:14:15 +0100592 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, info_frame);
Takashi Iwai7ceba752017-02-02 15:58:35 +0100593 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame2.regval);
594 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame3.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530595
596 /* program remaining DIP words with zero */
597 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
Takashi Iwai4151ee82017-01-31 18:14:15 +0100598 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, 0x0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530599
Takashi Iwai7ceba752017-02-02 15:58:35 +0100600 ctrl_state.regx.dip_freq = 1;
601 ctrl_state.regx.dip_en_sta = 1;
602 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530603}
604
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600605static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
606{
607 u32 maud_val;
608
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100609 /* Select maud according to DP 1.2 spec */
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600610 if (link_rate == DP_2_7_GHZ) {
611 switch (aud_samp_freq) {
612 case AUD_SAMPLE_RATE_32:
613 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
614 break;
615
616 case AUD_SAMPLE_RATE_44_1:
617 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
618 break;
619
620 case AUD_SAMPLE_RATE_48:
621 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
622 break;
623
624 case AUD_SAMPLE_RATE_88_2:
625 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
626 break;
627
628 case AUD_SAMPLE_RATE_96:
629 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
630 break;
631
632 case AUD_SAMPLE_RATE_176_4:
633 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
634 break;
635
636 case HAD_MAX_RATE:
637 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
638 break;
639
640 default:
641 maud_val = -EINVAL;
642 break;
643 }
644 } else if (link_rate == DP_1_62_GHZ) {
645 switch (aud_samp_freq) {
646 case AUD_SAMPLE_RATE_32:
647 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
648 break;
649
650 case AUD_SAMPLE_RATE_44_1:
651 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
652 break;
653
654 case AUD_SAMPLE_RATE_48:
655 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
656 break;
657
658 case AUD_SAMPLE_RATE_88_2:
659 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
660 break;
661
662 case AUD_SAMPLE_RATE_96:
663 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
664 break;
665
666 case AUD_SAMPLE_RATE_176_4:
667 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
668 break;
669
670 case HAD_MAX_RATE:
671 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
672 break;
673
674 default:
675 maud_val = -EINVAL;
676 break;
677 }
678 } else
679 maud_val = -EINVAL;
680
681 return maud_val;
682}
683
Takashi Iwai76296ef2017-01-30 16:09:11 +0100684/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100685 * Program HDMI audio CTS value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530686 *
687 * @aud_samp_freq: sampling frequency of audio data
688 * @tmds: sampling frequency of the display data
Takashi Iwaib5562902017-02-04 22:05:33 +0100689 * @link_rate: DP link rate
Jerome Anand5dab11d2017-01-25 04:27:52 +0530690 * @n_param: N value, depends on aud_samp_freq
Takashi Iwaib5562902017-02-04 22:05:33 +0100691 * @intelhaddata: substream private data
Jerome Anand5dab11d2017-01-25 04:27:52 +0530692 *
693 * Program CTS register based on the audio and display sampling frequency
694 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100695static void had_prog_cts(u32 aud_samp_freq, u32 tmds, u32 link_rate,
696 u32 n_param, struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530697{
698 u32 cts_val;
699 u64 dividend, divisor;
700
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600701 if (intelhaddata->dp_output) {
702 /* Substitute cts_val with Maud according to DP 1.2 spec*/
703 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
704 } else {
705 /* Calculate CTS according to HDMI 1.3a spec*/
706 dividend = (u64)tmds * n_param*1000;
707 divisor = 128 * aud_samp_freq;
708 cts_val = div64_u64(dividend, divisor);
709 }
Takashi Iwaic75b0472017-01-31 15:49:15 +0100710 dev_dbg(intelhaddata->dev, "TMDS value=%d, N value=%d, CTS Value=%d\n",
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600711 tmds, n_param, cts_val);
Takashi Iwai79dda752017-01-30 17:23:39 +0100712 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530713}
714
715static int had_calculate_n_value(u32 aud_samp_freq)
716{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100717 int n_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530718
719 /* Select N according to HDMI 1.3a spec*/
720 switch (aud_samp_freq) {
721 case AUD_SAMPLE_RATE_32:
722 n_val = 4096;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100723 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530724
725 case AUD_SAMPLE_RATE_44_1:
726 n_val = 6272;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100727 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530728
729 case AUD_SAMPLE_RATE_48:
730 n_val = 6144;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100731 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530732
733 case AUD_SAMPLE_RATE_88_2:
734 n_val = 12544;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100735 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530736
737 case AUD_SAMPLE_RATE_96:
738 n_val = 12288;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100739 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530740
741 case AUD_SAMPLE_RATE_176_4:
742 n_val = 25088;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100743 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530744
745 case HAD_MAX_RATE:
746 n_val = 24576;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100747 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530748
749 default:
750 n_val = -EINVAL;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100751 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530752 }
753 return n_val;
754}
755
Takashi Iwai76296ef2017-01-30 16:09:11 +0100756/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100757 * Program HDMI audio N value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530758 *
759 * @aud_samp_freq: sampling frequency of audio data
760 * @n_param: N value, depends on aud_samp_freq
Takashi Iwaib5562902017-02-04 22:05:33 +0100761 * @intelhaddata: substream private data
Jerome Anand5dab11d2017-01-25 04:27:52 +0530762 *
763 * This function is called in the prepare callback.
764 * It programs based on the audio and display sampling frequency
765 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100766static int had_prog_n(u32 aud_samp_freq, u32 *n_param,
767 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530768{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100769 int n_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530770
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600771 if (intelhaddata->dp_output) {
772 /*
773 * According to DP specs, Maud and Naud values hold
774 * a relationship, which is stated as:
775 * Maud/Naud = 512 * fs / f_LS_Clk
776 * where, fs is the sampling frequency of the audio stream
777 * and Naud is 32768 for Async clock.
778 */
779
780 n_val = DP_NAUD_VAL;
781 } else
782 n_val = had_calculate_n_value(aud_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530783
784 if (n_val < 0)
785 return n_val;
786
Takashi Iwai79dda752017-01-30 17:23:39 +0100787 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530788 *n_param = n_val;
789 return 0;
790}
791
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100792/*
793 * PCM ring buffer handling
794 *
795 * The hardware provides a ring buffer with the fixed 4 buffer descriptors
796 * (BDs). The driver maps these 4 BDs onto the PCM ring buffer. The mapping
797 * moves at each period elapsed. The below illustrates how it works:
798 *
799 * At time=0
800 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
801 * BD | 0 | 1 | 2 | 3 |
802 *
803 * At time=1 (period elapsed)
804 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
805 * BD | 1 | 2 | 3 | 0 |
806 *
807 * At time=2 (second period elapsed)
808 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
809 * BD | 2 | 3 | 0 | 1 |
810 *
811 * The bd_head field points to the index of the BD to be read. It's also the
812 * position to be filled at next. The pcm_head and the pcm_filled fields
813 * point to the indices of the current position and of the next position to
814 * be filled, respectively. For PCM buffer there are both _head and _filled
815 * because they may be difference when nperiods > 4. For example, in the
816 * example above at t=1, bd_head=1 and pcm_head=1 while pcm_filled=5:
817 *
818 * pcm_head (=1) --v v-- pcm_filled (=5)
819 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
820 * BD | 1 | 2 | 3 | 0 |
821 * bd_head (=1) --^ ^-- next to fill (= bd_head)
822 *
823 * For nperiods < 4, the remaining BDs out of 4 are marked as invalid, so that
824 * the hardware skips those BDs in the loop.
Takashi Iwai8d48c012017-02-07 08:05:46 +0100825 *
826 * An exceptional setup is the case with nperiods=1. Since we have to update
827 * BDs after finishing one BD processing, we'd need at least two BDs, where
828 * both BDs point to the same content, the same address, the same size of the
829 * whole PCM buffer.
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100830 */
831
832#define AUD_BUF_ADDR(x) (AUD_BUF_A_ADDR + (x) * HAD_REG_WIDTH)
833#define AUD_BUF_LEN(x) (AUD_BUF_A_LENGTH + (x) * HAD_REG_WIDTH)
834
835/* Set up a buffer descriptor at the "filled" position */
836static void had_prog_bd(struct snd_pcm_substream *substream,
837 struct snd_intelhad *intelhaddata)
838{
839 int idx = intelhaddata->bd_head;
840 int ofs = intelhaddata->pcmbuf_filled * intelhaddata->period_bytes;
841 u32 addr = substream->runtime->dma_addr + ofs;
842
843 addr |= AUD_BUF_VALID | AUD_BUF_INTR_EN;
844 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), addr);
845 had_write_register(intelhaddata, AUD_BUF_LEN(idx),
846 intelhaddata->period_bytes);
847
848 /* advance the indices to the next */
849 intelhaddata->bd_head++;
850 intelhaddata->bd_head %= intelhaddata->num_bds;
851 intelhaddata->pcmbuf_filled++;
852 intelhaddata->pcmbuf_filled %= substream->runtime->periods;
853}
854
855/* invalidate a buffer descriptor with the given index */
856static void had_invalidate_bd(struct snd_intelhad *intelhaddata,
857 int idx)
858{
859 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), 0);
860 had_write_register(intelhaddata, AUD_BUF_LEN(idx), 0);
861}
862
863/* Initial programming of ring buffer */
864static void had_init_ringbuf(struct snd_pcm_substream *substream,
865 struct snd_intelhad *intelhaddata)
866{
867 struct snd_pcm_runtime *runtime = substream->runtime;
868 int i, num_periods;
869
870 num_periods = runtime->periods;
871 intelhaddata->num_bds = min(num_periods, HAD_NUM_OF_RING_BUFS);
Takashi Iwai8d48c012017-02-07 08:05:46 +0100872 /* set the minimum 2 BDs for num_periods=1 */
873 intelhaddata->num_bds = max(intelhaddata->num_bds, 2U);
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100874 intelhaddata->period_bytes =
875 frames_to_bytes(runtime, runtime->period_size);
876 WARN_ON(intelhaddata->period_bytes & 0x3f);
877
878 intelhaddata->bd_head = 0;
879 intelhaddata->pcmbuf_head = 0;
880 intelhaddata->pcmbuf_filled = 0;
881
882 for (i = 0; i < HAD_NUM_OF_RING_BUFS; i++) {
Takashi Iwai8d48c012017-02-07 08:05:46 +0100883 if (i < intelhaddata->num_bds)
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100884 had_prog_bd(substream, intelhaddata);
885 else /* invalidate the rest */
886 had_invalidate_bd(intelhaddata, i);
887 }
888
889 intelhaddata->bd_head = 0; /* reset at head again before starting */
890}
891
892/* process a bd, advance to the next */
893static void had_advance_ringbuf(struct snd_pcm_substream *substream,
894 struct snd_intelhad *intelhaddata)
895{
896 int num_periods = substream->runtime->periods;
897
898 /* reprogram the next buffer */
899 had_prog_bd(substream, intelhaddata);
900
901 /* proceed to next */
902 intelhaddata->pcmbuf_head++;
903 intelhaddata->pcmbuf_head %= num_periods;
904}
905
906/* process the current BD(s);
907 * returns the current PCM buffer byte position, or -EPIPE for underrun.
908 */
909static int had_process_ringbuf(struct snd_pcm_substream *substream,
910 struct snd_intelhad *intelhaddata)
911{
912 int len, processed;
913 unsigned long flags;
914
915 processed = 0;
916 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
917 for (;;) {
918 /* get the remaining bytes on the buffer */
919 had_read_register(intelhaddata,
920 AUD_BUF_LEN(intelhaddata->bd_head),
921 &len);
922 if (len < 0 || len > intelhaddata->period_bytes) {
923 dev_dbg(intelhaddata->dev, "Invalid buf length %d\n",
924 len);
925 len = -EPIPE;
926 goto out;
927 }
928
929 if (len > 0) /* OK, this is the current buffer */
930 break;
931
932 /* len=0 => already empty, check the next buffer */
933 if (++processed >= intelhaddata->num_bds) {
934 len = -EPIPE; /* all empty? - report underrun */
935 goto out;
936 }
937 had_advance_ringbuf(substream, intelhaddata);
938 }
939
940 len = intelhaddata->period_bytes - len;
941 len += intelhaddata->period_bytes * intelhaddata->pcmbuf_head;
942 out:
943 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
944 return len;
945}
946
947/* called from irq handler */
948static void had_process_buffer_done(struct snd_intelhad *intelhaddata)
949{
950 struct snd_pcm_substream *substream;
951
952 if (!intelhaddata->connected)
953 return; /* disconnected? - bail out */
954
955 substream = had_substream_get(intelhaddata);
956 if (!substream)
957 return; /* no stream? - bail out */
958
959 /* process or stop the stream */
960 if (had_process_ringbuf(substream, intelhaddata) < 0)
961 snd_pcm_stop_xrun(substream);
962 else
963 snd_pcm_period_elapsed(substream);
964
965 had_substream_put(intelhaddata);
966}
967
Takashi Iwai03c34372017-02-02 16:19:03 +0100968#define MAX_CNT 0xFF
969
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100970/*
971 * The interrupt status 'sticky' bits might not be cleared by
972 * setting '1' to that bit once...
973 */
974static void wait_clear_underrun_bit(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530975{
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100976 int i;
977 u32 val;
978
979 for (i = 0; i < MAX_CNT; i++) {
980 /* clear bit30, 31 AUD_HDMI_STATUS */
981 had_read_register(intelhaddata, AUD_HDMI_STATUS, &val);
Takashi Iwai77531be2017-02-07 12:17:23 +0100982 if (!(val & AUD_HDMI_STATUS_MASK_UNDERRUN))
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100983 return;
984 had_write_register(intelhaddata, AUD_HDMI_STATUS, val);
985 }
986 dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n");
987}
988
989/* called from irq handler */
990static void had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
991{
992 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530993
994 /* Handle Underrun interrupt within Audio Unit */
Takashi Iwai79dda752017-01-30 17:23:39 +0100995 had_write_register(intelhaddata, AUD_CONFIG, 0);
Takashi Iwai40ce4b52017-02-07 16:17:06 +0100996 intelhaddata->aud_config.regval = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530997 /* Reset buffer pointers */
Takashi Iwaif4566aa2017-02-04 21:39:56 +0100998 had_reset_audio(intelhaddata);
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100999
1000 wait_clear_underrun_bit(intelhaddata);
1001
1002 if (!intelhaddata->connected)
1003 return; /* disconnected? - bail out */
1004
1005 /* Report UNDERRUN error to above layers */
1006 substream = had_substream_get(intelhaddata);
1007 if (substream) {
1008 snd_pcm_stop_xrun(substream);
1009 had_substream_put(intelhaddata);
1010 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301011}
1012
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001013/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001014 * ALSA PCM open callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301015 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001016static int had_pcm_open(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301017{
1018 struct snd_intelhad *intelhaddata;
1019 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301020 int retval;
1021
Jerome Anand5dab11d2017-01-25 04:27:52 +05301022 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301023 runtime = substream->runtime;
1024
Takashi Iwai182cdf22017-02-02 14:43:39 +01001025 pm_runtime_get_sync(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301026
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001027 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001028 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
1029 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301030 retval = -ENODEV;
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001031 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301032 }
1033
1034 /* set the runtime hw parameter with local snd_pcm_hardware struct */
Takashi Iwaib5562902017-02-04 22:05:33 +01001035 runtime->hw = had_pcm_hardware;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301036
Jerome Anand5dab11d2017-01-25 04:27:52 +05301037 retval = snd_pcm_hw_constraint_integer(runtime,
1038 SNDRV_PCM_HW_PARAM_PERIODS);
1039 if (retval < 0)
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001040 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301041
1042 /* Make sure, that the period size is always aligned
1043 * 64byte boundary
1044 */
1045 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1046 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
Takashi Iwai73997b02017-02-02 17:38:50 +01001047 if (retval < 0)
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001048 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301049
Takashi Iwai73997b02017-02-02 17:38:50 +01001050 /* expose PCM substream */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001051 spin_lock_irq(&intelhaddata->had_spinlock);
1052 intelhaddata->stream_info.substream = substream;
1053 intelhaddata->stream_info.substream_refcount++;
1054 spin_unlock_irq(&intelhaddata->had_spinlock);
1055
Jerome Anand5dab11d2017-01-25 04:27:52 +05301056 return retval;
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001057 error:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301058 pm_runtime_put(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301059 return retval;
1060}
1061
Takashi Iwaidf76df12017-01-31 16:04:10 +01001062/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001063 * ALSA PCM close callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301064 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001065static int had_pcm_close(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301066{
1067 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301068
Jerome Anand5dab11d2017-01-25 04:27:52 +05301069 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301070
Takashi Iwai73997b02017-02-02 17:38:50 +01001071 /* unreference and sync with the pending PCM accesses */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001072 spin_lock_irq(&intelhaddata->had_spinlock);
1073 intelhaddata->stream_info.substream = NULL;
1074 intelhaddata->stream_info.substream_refcount--;
1075 while (intelhaddata->stream_info.substream_refcount > 0) {
1076 spin_unlock_irq(&intelhaddata->had_spinlock);
1077 cpu_relax();
1078 spin_lock_irq(&intelhaddata->had_spinlock);
1079 }
1080 spin_unlock_irq(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301081
Jerome Anand5dab11d2017-01-25 04:27:52 +05301082 pm_runtime_put(intelhaddata->dev);
1083 return 0;
1084}
1085
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001086/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001087 * ALSA PCM hw_params callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301088 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001089static int had_pcm_hw_params(struct snd_pcm_substream *substream,
1090 struct snd_pcm_hw_params *hw_params)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301091{
Takashi Iwaic75b0472017-01-31 15:49:15 +01001092 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301093 unsigned long addr;
1094 int pages, buf_size, retval;
1095
Takashi Iwaic75b0472017-01-31 15:49:15 +01001096 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301097 buf_size = params_buffer_bytes(hw_params);
1098 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1099 if (retval < 0)
1100 return retval;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001101 dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
1102 __func__, buf_size);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301103 /* mark the pages as uncached region */
1104 addr = (unsigned long) substream->runtime->dma_area;
1105 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1106 retval = set_memory_uc(addr, pages);
1107 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001108 dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
1109 retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301110 return retval;
1111 }
1112 memset(substream->runtime->dma_area, 0, buf_size);
1113
1114 return retval;
1115}
1116
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001117/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001118 * ALSA PCM hw_free callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301119 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001120static int had_pcm_hw_free(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301121{
1122 unsigned long addr;
1123 u32 pages;
1124
Jerome Anand5dab11d2017-01-25 04:27:52 +05301125 /* mark back the pages as cached/writeback region before the free */
1126 if (substream->runtime->dma_area != NULL) {
1127 addr = (unsigned long) substream->runtime->dma_area;
1128 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1129 PAGE_SIZE;
1130 set_memory_wb(addr, pages);
1131 return snd_pcm_lib_free_pages(substream);
1132 }
1133 return 0;
1134}
1135
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001136/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001137 * ALSA PCM trigger callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301138 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001139static int had_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301140{
Takashi Iwaida864802017-01-31 13:52:22 +01001141 int retval = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301142 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301143
Jerome Anand5dab11d2017-01-25 04:27:52 +05301144 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301145
1146 switch (cmd) {
1147 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001148 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1149 case SNDRV_PCM_TRIGGER_RESUME:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301150 /* Disable local INTRs till register prgmng is done */
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001151 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001152 dev_dbg(intelhaddata->dev,
1153 "_START: HDMI cable plugged-out\n");
Jerome Anand5dab11d2017-01-25 04:27:52 +05301154 retval = -ENODEV;
1155 break;
1156 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301157
Takashi Iwaif69bd102017-02-02 14:57:22 +01001158 intelhaddata->stream_info.running = true;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301159
1160 /* Enable Audio */
Takashi Iwai075a1d42017-02-07 07:55:27 +01001161 had_ack_irqs(intelhaddata); /* FIXME: do we need this? */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001162 had_enable_audio(intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301163 break;
1164
1165 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001166 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1167 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwaibcce7752017-02-01 17:18:20 +01001168 spin_lock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301169
Takashi Iwaic75b0472017-01-31 15:49:15 +01001170 /* Stop reporting BUFFER_DONE/UNDERRUN to above layers */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301171
Takashi Iwaif69bd102017-02-02 14:57:22 +01001172 intelhaddata->stream_info.running = false;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001173 spin_unlock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301174 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001175 had_enable_audio(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301176 /* Reset buffer pointers */
Takashi Iwaif4566aa2017-02-04 21:39:56 +01001177 had_reset_audio(intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301178 break;
1179
1180 default:
1181 retval = -EINVAL;
1182 }
1183 return retval;
1184}
1185
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001186/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001187 * ALSA PCM prepare callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301188 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001189static int had_pcm_prepare(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301190{
1191 int retval;
1192 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001193 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301194 struct snd_intelhad *intelhaddata;
1195 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301196
Jerome Anand5dab11d2017-01-25 04:27:52 +05301197 intelhaddata = snd_pcm_substream_chip(substream);
1198 runtime = substream->runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301199
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001200 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001201 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
1202 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301203 retval = -ENODEV;
1204 goto prep_end;
1205 }
1206
Takashi Iwaic75b0472017-01-31 15:49:15 +01001207 dev_dbg(intelhaddata->dev, "period_size=%d\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301208 (int)frames_to_bytes(runtime, runtime->period_size));
Takashi Iwaic75b0472017-01-31 15:49:15 +01001209 dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods);
1210 dev_dbg(intelhaddata->dev, "buffer_size=%d\n",
1211 (int)snd_pcm_lib_buffer_bytes(substream));
1212 dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
1213 dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301214
Jerome Anand5dab11d2017-01-25 04:27:52 +05301215 /* Get N value in KHz */
Takashi Iwaida864802017-01-31 13:52:22 +01001216 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301217
Takashi Iwaib5562902017-02-04 22:05:33 +01001218 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301219 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001220 dev_err(intelhaddata->dev,
1221 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301222 goto prep_end;
1223 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001224
1225 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001226 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001227
Takashi Iwaib5562902017-02-04 22:05:33 +01001228 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1229 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301230
Takashi Iwaib5562902017-02-04 22:05:33 +01001231 had_prog_dip(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301232
Takashi Iwaib5562902017-02-04 22:05:33 +01001233 retval = had_init_audio_ctrl(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301234
1235 /* Prog buffer address */
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001236 had_init_ringbuf(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301237
1238 /*
1239 * Program channel mapping in following order:
1240 * FL, FR, C, LFE, RL, RR
1241 */
1242
Takashi Iwai79dda752017-01-30 17:23:39 +01001243 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301244
1245prep_end:
1246 return retval;
1247}
1248
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001249/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001250 * ALSA PCM pointer callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301251 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001252static snd_pcm_uframes_t had_pcm_pointer(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301253{
1254 struct snd_intelhad *intelhaddata;
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001255 int len;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301256
Jerome Anand5dab11d2017-01-25 04:27:52 +05301257 intelhaddata = snd_pcm_substream_chip(substream);
1258
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001259 if (!intelhaddata->connected)
Takashi Iwai79f439e2017-01-31 16:46:44 +01001260 return SNDRV_PCM_POS_XRUN;
1261
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001262 len = had_process_ringbuf(substream, intelhaddata);
1263 if (len < 0)
1264 return SNDRV_PCM_POS_XRUN;
Takashi Iwai8d48c012017-02-07 08:05:46 +01001265 len = bytes_to_frames(substream->runtime, len);
1266 /* wrapping may happen when periods=1 */
1267 len %= substream->runtime->buffer_size;
1268 return len;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301269}
1270
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001271/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001272 * ALSA PCM mmap callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301273 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001274static int had_pcm_mmap(struct snd_pcm_substream *substream,
1275 struct vm_area_struct *vma)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301276{
Jerome Anand5dab11d2017-01-25 04:27:52 +05301277 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1278 return remap_pfn_range(vma, vma->vm_start,
1279 substream->dma_buffer.addr >> PAGE_SHIFT,
1280 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1281}
1282
Takashi Iwai73997b02017-02-02 17:38:50 +01001283/*
1284 * ALSA PCM ops
1285 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001286static const struct snd_pcm_ops had_pcm_ops = {
1287 .open = had_pcm_open,
1288 .close = had_pcm_close,
Takashi Iwai73997b02017-02-02 17:38:50 +01001289 .ioctl = snd_pcm_lib_ioctl,
Takashi Iwaib5562902017-02-04 22:05:33 +01001290 .hw_params = had_pcm_hw_params,
1291 .hw_free = had_pcm_hw_free,
1292 .prepare = had_pcm_prepare,
1293 .trigger = had_pcm_trigger,
1294 .pointer = had_pcm_pointer,
1295 .mmap = had_pcm_mmap,
Takashi Iwai73997b02017-02-02 17:38:50 +01001296};
1297
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001298/* process mode change of the running stream; called in mutex */
Takashi Iwaib5562902017-02-04 22:05:33 +01001299static int had_process_mode_change(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301300{
Takashi Iwaida864802017-01-31 13:52:22 +01001301 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301302 int retval = 0;
1303 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001304 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301305
Takashi Iwai313d9f22017-02-02 13:00:12 +01001306 substream = had_substream_get(intelhaddata);
1307 if (!substream)
Takashi Iwaida864802017-01-31 13:52:22 +01001308 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301309
1310 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001311 had_enable_audio(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301312
1313 /* Update CTS value */
Takashi Iwaida864802017-01-31 13:52:22 +01001314 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301315
Takashi Iwaib5562902017-02-04 22:05:33 +01001316 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301317 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001318 dev_err(intelhaddata->dev,
1319 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301320 goto out;
1321 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001322
1323 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001324 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001325
Takashi Iwaib5562902017-02-04 22:05:33 +01001326 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1327 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301328
1329 /* Enable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001330 had_enable_audio(intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301331
1332out:
Takashi Iwai313d9f22017-02-02 13:00:12 +01001333 had_substream_put(intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301334 return retval;
1335}
1336
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001337/* process hot plug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001338static void had_process_hot_plug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001339{
Takashi Iwai372d8552017-01-31 13:57:58 +01001340 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001341
Takashi Iwaibcce7752017-02-01 17:18:20 +01001342 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001343 if (intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001344 dev_dbg(intelhaddata->dev, "Device already connected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001345 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001346 return;
Takashi Iwai372d8552017-01-31 13:57:58 +01001347 }
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001348
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001349 intelhaddata->connected = true;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001350 dev_dbg(intelhaddata->dev,
1351 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001352 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001353 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001354
Takashi Iwai372d8552017-01-31 13:57:58 +01001355 /* Safety check */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001356 substream = had_substream_get(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001357 if (substream) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001358 dev_dbg(intelhaddata->dev,
1359 "Force to stop the active stream by disconnection\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001360 /* Set runtime->state to hw_params done */
1361 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001362 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001363 }
1364
1365 had_build_channel_allocation_map(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001366}
1367
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001368/* process hot unplug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001369static void had_process_hot_unplug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001370{
Takashi Iwai313d9f22017-02-02 13:00:12 +01001371 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001372
Takashi Iwai313d9f22017-02-02 13:00:12 +01001373 substream = had_substream_get(intelhaddata);
1374
Takashi Iwaibcce7752017-02-01 17:18:20 +01001375 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001376
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 disconnected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001379 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001380 goto out;
Takashi Iwai372d8552017-01-31 13:57:58 +01001381
Takashi Iwai372d8552017-01-31 13:57:58 +01001382 }
1383
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001384 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001385 had_enable_audio(intelhaddata, false);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001386
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001387 intelhaddata->connected = false;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001388 dev_dbg(intelhaddata->dev,
1389 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001390 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001391 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001392
1393 /* Report to above ALSA layer */
1394 if (substream)
1395 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1396
1397 out:
1398 if (substream)
1399 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001400 kfree(intelhaddata->chmap->chmap);
1401 intelhaddata->chmap->chmap = NULL;
Takashi Iwai372d8552017-01-31 13:57:58 +01001402}
1403
Takashi Iwai73997b02017-02-02 17:38:50 +01001404/*
1405 * ALSA iec958 and ELD controls
1406 */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301407
Jerome Anand5dab11d2017-01-25 04:27:52 +05301408static int had_iec958_info(struct snd_kcontrol *kcontrol,
1409 struct snd_ctl_elem_info *uinfo)
1410{
1411 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1412 uinfo->count = 1;
1413 return 0;
1414}
1415
1416static int had_iec958_get(struct snd_kcontrol *kcontrol,
1417 struct snd_ctl_elem_value *ucontrol)
1418{
1419 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1420
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001421 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301422 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1423 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1424 ucontrol->value.iec958.status[2] =
1425 (intelhaddata->aes_bits >> 16) & 0xff;
1426 ucontrol->value.iec958.status[3] =
1427 (intelhaddata->aes_bits >> 24) & 0xff;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001428 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301429 return 0;
1430}
Takashi Iwai372d8552017-01-31 13:57:58 +01001431
Jerome Anand5dab11d2017-01-25 04:27:52 +05301432static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1433 struct snd_ctl_elem_value *ucontrol)
1434{
1435 ucontrol->value.iec958.status[0] = 0xff;
1436 ucontrol->value.iec958.status[1] = 0xff;
1437 ucontrol->value.iec958.status[2] = 0xff;
1438 ucontrol->value.iec958.status[3] = 0xff;
1439 return 0;
1440}
Takashi Iwai372d8552017-01-31 13:57:58 +01001441
Jerome Anand5dab11d2017-01-25 04:27:52 +05301442static int had_iec958_put(struct snd_kcontrol *kcontrol,
1443 struct snd_ctl_elem_value *ucontrol)
1444{
1445 unsigned int val;
1446 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001447 int changed = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301448
Jerome Anand5dab11d2017-01-25 04:27:52 +05301449 val = (ucontrol->value.iec958.status[0] << 0) |
1450 (ucontrol->value.iec958.status[1] << 8) |
1451 (ucontrol->value.iec958.status[2] << 16) |
1452 (ucontrol->value.iec958.status[3] << 24);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001453 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301454 if (intelhaddata->aes_bits != val) {
1455 intelhaddata->aes_bits = val;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001456 changed = 1;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301457 }
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001458 mutex_unlock(&intelhaddata->mutex);
1459 return changed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301460}
1461
Takashi Iwai4aedb942017-02-02 16:38:39 +01001462static int had_ctl_eld_info(struct snd_kcontrol *kcontrol,
1463 struct snd_ctl_elem_info *uinfo)
1464{
1465 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1466 uinfo->count = HDMI_MAX_ELD_BYTES;
1467 return 0;
1468}
1469
1470static int had_ctl_eld_get(struct snd_kcontrol *kcontrol,
1471 struct snd_ctl_elem_value *ucontrol)
1472{
1473 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1474
1475 mutex_lock(&intelhaddata->mutex);
1476 memcpy(ucontrol->value.bytes.data, intelhaddata->eld,
1477 HDMI_MAX_ELD_BYTES);
1478 mutex_unlock(&intelhaddata->mutex);
1479 return 0;
1480}
1481
Takashi Iwai73997b02017-02-02 17:38:50 +01001482static const struct snd_kcontrol_new had_controls[] = {
Takashi Iwai4aedb942017-02-02 16:38:39 +01001483 {
1484 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1485 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1486 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1487 .info = had_iec958_info, /* shared */
1488 .get = had_iec958_mask_get,
1489 },
1490 {
1491 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1492 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1493 .info = had_iec958_info,
1494 .get = had_iec958_get,
1495 .put = had_iec958_put,
1496 },
1497 {
1498 .access = (SNDRV_CTL_ELEM_ACCESS_READ |
1499 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1500 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1501 .name = "ELD",
1502 .info = had_ctl_eld_info,
1503 .get = had_ctl_eld_get,
1504 },
Jerome Anand5dab11d2017-01-25 04:27:52 +05301505};
1506
Takashi Iwai73997b02017-02-02 17:38:50 +01001507/*
1508 * audio interrupt handler
1509 */
Takashi Iwaida864802017-01-31 13:52:22 +01001510static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id)
1511{
1512 struct snd_intelhad *ctx = dev_id;
1513 u32 audio_stat, audio_reg;
1514
Takashi Iwai4151ee82017-01-31 18:14:15 +01001515 audio_reg = AUD_HDMI_STATUS;
Takashi Iwai83af57d2017-02-03 08:50:06 +01001516 had_read_register(ctx, audio_reg, &audio_stat);
Takashi Iwaida864802017-01-31 13:52:22 +01001517
1518 if (audio_stat & HDMI_AUDIO_UNDERRUN) {
Takashi Iwai83af57d2017-02-03 08:50:06 +01001519 had_write_register(ctx, audio_reg, HDMI_AUDIO_UNDERRUN);
Takashi Iwaida864802017-01-31 13:52:22 +01001520 had_process_buffer_underrun(ctx);
1521 }
1522
1523 if (audio_stat & HDMI_AUDIO_BUFFER_DONE) {
Takashi Iwai83af57d2017-02-03 08:50:06 +01001524 had_write_register(ctx, audio_reg, HDMI_AUDIO_BUFFER_DONE);
Takashi Iwaida864802017-01-31 13:52:22 +01001525 had_process_buffer_done(ctx);
1526 }
1527
1528 return IRQ_HANDLED;
1529}
1530
Takashi Iwai73997b02017-02-02 17:38:50 +01001531/*
1532 * monitor plug/unplug notification from i915; just kick off the work
1533 */
Takashi Iwaida864802017-01-31 13:52:22 +01001534static void notify_audio_lpe(struct platform_device *pdev)
1535{
1536 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Takashi Iwaida864802017-01-31 13:52:22 +01001537
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001538 schedule_work(&ctx->hdmi_audio_wq);
1539}
Takashi Iwaida864802017-01-31 13:52:22 +01001540
Takashi Iwai73997b02017-02-02 17:38:50 +01001541/* the work to handle monitor hot plug/unplug */
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001542static void had_audio_wq(struct work_struct *work)
1543{
1544 struct snd_intelhad *ctx =
1545 container_of(work, struct snd_intelhad, hdmi_audio_wq);
1546 struct intel_hdmi_lpe_audio_pdata *pdata = ctx->dev->platform_data;
1547
Takashi Iwai182cdf22017-02-02 14:43:39 +01001548 pm_runtime_get_sync(ctx->dev);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001549 mutex_lock(&ctx->mutex);
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001550 if (!pdata->hdmi_connected) {
1551 dev_dbg(ctx->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001552 __func__);
Takashi Iwai4aedb942017-02-02 16:38:39 +01001553 memset(ctx->eld, 0, sizeof(ctx->eld)); /* clear the old ELD */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001554 had_process_hot_unplug(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001555 } else {
1556 struct intel_hdmi_lpe_audio_eld *eld = &pdata->eld;
1557
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001558 dev_dbg(ctx->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
1559 __func__, eld->port_id, pdata->tmds_clock_speed);
1560
Takashi Iwaida864802017-01-31 13:52:22 +01001561 switch (eld->pipe_id) {
1562 case 0:
1563 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1564 break;
1565 case 1:
1566 ctx->had_config_offset = AUDIO_HDMI_CONFIG_B;
1567 break;
1568 case 2:
1569 ctx->had_config_offset = AUDIO_HDMI_CONFIG_C;
1570 break;
1571 default:
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001572 dev_dbg(ctx->dev, "Invalid pipe %d\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001573 eld->pipe_id);
1574 break;
1575 }
1576
Takashi Iwaidf0435d2017-02-02 15:37:11 +01001577 memcpy(ctx->eld, eld->eld_data, sizeof(ctx->eld));
Takashi Iwaida864802017-01-31 13:52:22 +01001578
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001579 ctx->dp_output = pdata->dp_output;
1580 ctx->tmds_clock_speed = pdata->tmds_clock_speed;
1581 ctx->link_rate = pdata->link_rate;
1582
Takashi Iwaida864802017-01-31 13:52:22 +01001583 had_process_hot_plug(ctx);
1584
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001585 /* Process mode change if stream is active */
Takashi Iwaib5562902017-02-04 22:05:33 +01001586 had_process_mode_change(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001587 }
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001588 mutex_unlock(&ctx->mutex);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001589 pm_runtime_put(ctx->dev);
1590}
1591
1592/*
1593 * PM callbacks
1594 */
1595
1596static int hdmi_lpe_audio_runtime_suspend(struct device *dev)
1597{
1598 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1599 struct snd_pcm_substream *substream;
1600
1601 substream = had_substream_get(ctx);
1602 if (substream) {
1603 snd_pcm_suspend(substream);
1604 had_substream_put(ctx);
1605 }
1606
1607 return 0;
1608}
1609
Arnd Bergmann1df98922017-02-07 14:38:51 +01001610static int __maybe_unused hdmi_lpe_audio_suspend(struct device *dev)
Takashi Iwai182cdf22017-02-02 14:43:39 +01001611{
1612 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1613 int err;
1614
1615 err = hdmi_lpe_audio_runtime_suspend(dev);
1616 if (!err)
1617 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D3hot);
1618 return err;
1619}
1620
Arnd Bergmann1df98922017-02-07 14:38:51 +01001621static int __maybe_unused hdmi_lpe_audio_resume(struct device *dev)
Takashi Iwai182cdf22017-02-02 14:43:39 +01001622{
1623 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1624
1625 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D0);
1626 return 0;
Takashi Iwaida864802017-01-31 13:52:22 +01001627}
1628
1629/* release resources */
1630static void hdmi_lpe_audio_free(struct snd_card *card)
1631{
1632 struct snd_intelhad *ctx = card->private_data;
1633
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001634 cancel_work_sync(&ctx->hdmi_audio_wq);
1635
Takashi Iwaida864802017-01-31 13:52:22 +01001636 if (ctx->mmio_start)
1637 iounmap(ctx->mmio_start);
1638 if (ctx->irq >= 0)
1639 free_irq(ctx->irq, ctx);
1640}
1641
1642/*
1643 * hdmi_lpe_audio_probe - start bridge with i915
1644 *
1645 * This function is called when the i915 driver creates the
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001646 * hdmi-lpe-audio platform device.
Takashi Iwaida864802017-01-31 13:52:22 +01001647 */
1648static int hdmi_lpe_audio_probe(struct platform_device *pdev)
1649{
1650 struct snd_card *card;
1651 struct snd_intelhad *ctx;
1652 struct snd_pcm *pcm;
1653 struct intel_hdmi_lpe_audio_pdata *pdata;
1654 int irq;
1655 struct resource *res_mmio;
Takashi Iwai4aedb942017-02-02 16:38:39 +01001656 int i, ret;
Takashi Iwaida864802017-01-31 13:52:22 +01001657
Takashi Iwaida864802017-01-31 13:52:22 +01001658 pdata = pdev->dev.platform_data;
1659 if (!pdata) {
1660 dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
1661 return -EINVAL;
1662 }
1663
1664 /* get resources */
1665 irq = platform_get_irq(pdev, 0);
1666 if (irq < 0) {
1667 dev_err(&pdev->dev, "Could not get irq resource\n");
1668 return -ENODEV;
1669 }
1670
1671 res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1672 if (!res_mmio) {
1673 dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
1674 return -ENXIO;
1675 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301676
Takashi Iwai5647aec2017-01-31 08:14:34 +01001677 /* create a card instance with ALSA framework */
Takashi Iwaida864802017-01-31 13:52:22 +01001678 ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
1679 THIS_MODULE, sizeof(*ctx), &card);
1680 if (ret)
1681 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301682
Takashi Iwaida864802017-01-31 13:52:22 +01001683 ctx = card->private_data;
1684 spin_lock_init(&ctx->had_spinlock);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001685 mutex_init(&ctx->mutex);
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001686 ctx->connected = false;
Takashi Iwaida864802017-01-31 13:52:22 +01001687 ctx->dev = &pdev->dev;
1688 ctx->card = card;
Takashi Iwaida864802017-01-31 13:52:22 +01001689 ctx->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
1690 strcpy(card->driver, INTEL_HAD);
Takashi Iwai873ab032017-02-07 12:14:04 +01001691 strcpy(card->shortname, "Intel HDMI/DP LPE Audio");
1692 strcpy(card->longname, "Intel HDMI/DP LPE Audio");
Jerome Anand5dab11d2017-01-25 04:27:52 +05301693
Takashi Iwaida864802017-01-31 13:52:22 +01001694 ctx->irq = -1;
1695 ctx->tmds_clock_speed = DIS_SAMPLE_RATE_148_5;
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001696 INIT_WORK(&ctx->hdmi_audio_wq, had_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301697
Takashi Iwaida864802017-01-31 13:52:22 +01001698 card->private_free = hdmi_lpe_audio_free;
1699
1700 /* assume pipe A as default */
1701 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1702
1703 platform_set_drvdata(pdev, ctx);
1704
1705 dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
1706 __func__, (unsigned int)res_mmio->start,
1707 (unsigned int)res_mmio->end);
1708
1709 ctx->mmio_start = ioremap_nocache(res_mmio->start,
1710 (size_t)(resource_size(res_mmio)));
1711 if (!ctx->mmio_start) {
1712 dev_err(&pdev->dev, "Could not get ioremap\n");
1713 ret = -EACCES;
1714 goto err;
1715 }
1716
1717 /* setup interrupt handler */
1718 ret = request_irq(irq, display_pipe_interrupt_handler, 0,
1719 pdev->name, ctx);
1720 if (ret < 0) {
1721 dev_err(&pdev->dev, "request_irq failed\n");
1722 goto err;
1723 }
1724
1725 ctx->irq = irq;
1726
1727 ret = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
1728 MAX_CAP_STREAMS, &pcm);
1729 if (ret)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301730 goto err;
1731
1732 /* setup private data which can be retrieved when required */
Takashi Iwaida864802017-01-31 13:52:22 +01001733 pcm->private_data = ctx;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301734 pcm->info_flags = 0;
1735 strncpy(pcm->name, card->shortname, strlen(card->shortname));
Takashi Iwaida864802017-01-31 13:52:22 +01001736 /* setup the ops for playabck */
Takashi Iwaib5562902017-02-04 22:05:33 +01001737 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &had_pcm_ops);
Takashi Iwai412bbe7d52017-02-02 22:03:22 +01001738
1739 /* only 32bit addressable */
1740 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
1741 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1742
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001743 /* allocate dma pages;
1744 * try to allocate 600k buffer as default which is large enough
Jerome Anand5dab11d2017-01-25 04:27:52 +05301745 */
Takashi Iwaida864802017-01-31 13:52:22 +01001746 snd_pcm_lib_preallocate_pages_for_all(pcm,
Jerome Anand5dab11d2017-01-25 04:27:52 +05301747 SNDRV_DMA_TYPE_DEV, NULL,
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001748 HAD_DEFAULT_BUFFER, HAD_MAX_BUFFER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301749
Takashi Iwai4aedb942017-02-02 16:38:39 +01001750 /* create controls */
1751 for (i = 0; i < ARRAY_SIZE(had_controls); i++) {
1752 ret = snd_ctl_add(card, snd_ctl_new1(&had_controls[i], ctx));
1753 if (ret < 0)
1754 goto err;
1755 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301756
1757 init_channel_allocations();
1758
1759 /* Register channel map controls */
Takashi Iwaida864802017-01-31 13:52:22 +01001760 ret = had_register_chmap_ctls(ctx, pcm);
1761 if (ret < 0)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301762 goto err;
1763
Takashi Iwaida864802017-01-31 13:52:22 +01001764 ret = snd_card_register(card);
1765 if (ret)
Takashi Iwai36ec0d92017-01-31 08:47:05 +01001766 goto err;
1767
Takashi Iwaibcce7752017-02-01 17:18:20 +01001768 spin_lock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001769 pdata->notify_audio_lpe = notify_audio_lpe;
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001770 pdata->notify_pending = false;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001771 spin_unlock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001772
1773 pm_runtime_set_active(&pdev->dev);
1774 pm_runtime_enable(&pdev->dev);
1775
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001776 dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
Takashi Iwaida864802017-01-31 13:52:22 +01001777 schedule_work(&ctx->hdmi_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301778
Takashi Iwai79dda752017-01-30 17:23:39 +01001779 return 0;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001780
Jerome Anand5dab11d2017-01-25 04:27:52 +05301781err:
1782 snd_card_free(card);
Takashi Iwaida864802017-01-31 13:52:22 +01001783 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301784}
1785
Takashi Iwai79dda752017-01-30 17:23:39 +01001786/*
Takashi Iwaida864802017-01-31 13:52:22 +01001787 * hdmi_lpe_audio_remove - stop bridge with i915
Jerome Anand5dab11d2017-01-25 04:27:52 +05301788 *
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001789 * This function is called when the platform device is destroyed.
Jerome Anand5dab11d2017-01-25 04:27:52 +05301790 */
Takashi Iwaida864802017-01-31 13:52:22 +01001791static int hdmi_lpe_audio_remove(struct platform_device *pdev)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301792{
Takashi Iwaida864802017-01-31 13:52:22 +01001793 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301794
Takashi Iwaida864802017-01-31 13:52:22 +01001795 snd_card_free(ctx->card);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301796 return 0;
1797}
1798
Takashi Iwai182cdf22017-02-02 14:43:39 +01001799static const struct dev_pm_ops hdmi_lpe_audio_pm = {
1800 SET_SYSTEM_SLEEP_PM_OPS(hdmi_lpe_audio_suspend, hdmi_lpe_audio_resume)
1801 SET_RUNTIME_PM_OPS(hdmi_lpe_audio_runtime_suspend, NULL, NULL)
1802};
1803
Takashi Iwaida864802017-01-31 13:52:22 +01001804static struct platform_driver hdmi_lpe_audio_driver = {
1805 .driver = {
1806 .name = "hdmi-lpe-audio",
Takashi Iwai182cdf22017-02-02 14:43:39 +01001807 .pm = &hdmi_lpe_audio_pm,
Takashi Iwaida864802017-01-31 13:52:22 +01001808 },
1809 .probe = hdmi_lpe_audio_probe,
1810 .remove = hdmi_lpe_audio_remove,
Takashi Iwaida864802017-01-31 13:52:22 +01001811};
1812
1813module_platform_driver(hdmi_lpe_audio_driver);
1814MODULE_ALIAS("platform:hdmi_lpe_audio");
1815
Jerome Anand5dab11d2017-01-25 04:27:52 +05301816MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1817MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1818MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1819MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1820MODULE_DESCRIPTION("Intel HDMI Audio driver");
1821MODULE_LICENSE("GPL v2");
1822MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");