blob: 73a662d20201d21cbb9885c9220b57d9ef07cf85 [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 Iwaie1b239f32017-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.
825 */
826
827#define AUD_BUF_ADDR(x) (AUD_BUF_A_ADDR + (x) * HAD_REG_WIDTH)
828#define AUD_BUF_LEN(x) (AUD_BUF_A_LENGTH + (x) * HAD_REG_WIDTH)
829
830/* Set up a buffer descriptor at the "filled" position */
831static void had_prog_bd(struct snd_pcm_substream *substream,
832 struct snd_intelhad *intelhaddata)
833{
834 int idx = intelhaddata->bd_head;
835 int ofs = intelhaddata->pcmbuf_filled * intelhaddata->period_bytes;
836 u32 addr = substream->runtime->dma_addr + ofs;
837
838 addr |= AUD_BUF_VALID | AUD_BUF_INTR_EN;
839 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), addr);
840 had_write_register(intelhaddata, AUD_BUF_LEN(idx),
841 intelhaddata->period_bytes);
842
843 /* advance the indices to the next */
844 intelhaddata->bd_head++;
845 intelhaddata->bd_head %= intelhaddata->num_bds;
846 intelhaddata->pcmbuf_filled++;
847 intelhaddata->pcmbuf_filled %= substream->runtime->periods;
848}
849
850/* invalidate a buffer descriptor with the given index */
851static void had_invalidate_bd(struct snd_intelhad *intelhaddata,
852 int idx)
853{
854 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), 0);
855 had_write_register(intelhaddata, AUD_BUF_LEN(idx), 0);
856}
857
858/* Initial programming of ring buffer */
859static void had_init_ringbuf(struct snd_pcm_substream *substream,
860 struct snd_intelhad *intelhaddata)
861{
862 struct snd_pcm_runtime *runtime = substream->runtime;
863 int i, num_periods;
864
865 num_periods = runtime->periods;
866 intelhaddata->num_bds = min(num_periods, HAD_NUM_OF_RING_BUFS);
867 intelhaddata->period_bytes =
868 frames_to_bytes(runtime, runtime->period_size);
869 WARN_ON(intelhaddata->period_bytes & 0x3f);
870
871 intelhaddata->bd_head = 0;
872 intelhaddata->pcmbuf_head = 0;
873 intelhaddata->pcmbuf_filled = 0;
874
875 for (i = 0; i < HAD_NUM_OF_RING_BUFS; i++) {
876 if (i < num_periods)
877 had_prog_bd(substream, intelhaddata);
878 else /* invalidate the rest */
879 had_invalidate_bd(intelhaddata, i);
880 }
881
882 intelhaddata->bd_head = 0; /* reset at head again before starting */
883}
884
885/* process a bd, advance to the next */
886static void had_advance_ringbuf(struct snd_pcm_substream *substream,
887 struct snd_intelhad *intelhaddata)
888{
889 int num_periods = substream->runtime->periods;
890
891 /* reprogram the next buffer */
892 had_prog_bd(substream, intelhaddata);
893
894 /* proceed to next */
895 intelhaddata->pcmbuf_head++;
896 intelhaddata->pcmbuf_head %= num_periods;
897}
898
899/* process the current BD(s);
900 * returns the current PCM buffer byte position, or -EPIPE for underrun.
901 */
902static int had_process_ringbuf(struct snd_pcm_substream *substream,
903 struct snd_intelhad *intelhaddata)
904{
905 int len, processed;
906 unsigned long flags;
907
908 processed = 0;
909 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
910 for (;;) {
911 /* get the remaining bytes on the buffer */
912 had_read_register(intelhaddata,
913 AUD_BUF_LEN(intelhaddata->bd_head),
914 &len);
915 if (len < 0 || len > intelhaddata->period_bytes) {
916 dev_dbg(intelhaddata->dev, "Invalid buf length %d\n",
917 len);
918 len = -EPIPE;
919 goto out;
920 }
921
922 if (len > 0) /* OK, this is the current buffer */
923 break;
924
925 /* len=0 => already empty, check the next buffer */
926 if (++processed >= intelhaddata->num_bds) {
927 len = -EPIPE; /* all empty? - report underrun */
928 goto out;
929 }
930 had_advance_ringbuf(substream, intelhaddata);
931 }
932
933 len = intelhaddata->period_bytes - len;
934 len += intelhaddata->period_bytes * intelhaddata->pcmbuf_head;
935 out:
936 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
937 return len;
938}
939
940/* called from irq handler */
941static void had_process_buffer_done(struct snd_intelhad *intelhaddata)
942{
943 struct snd_pcm_substream *substream;
944
945 if (!intelhaddata->connected)
946 return; /* disconnected? - bail out */
947
948 substream = had_substream_get(intelhaddata);
949 if (!substream)
950 return; /* no stream? - bail out */
951
952 /* process or stop the stream */
953 if (had_process_ringbuf(substream, intelhaddata) < 0)
954 snd_pcm_stop_xrun(substream);
955 else
956 snd_pcm_period_elapsed(substream);
957
958 had_substream_put(intelhaddata);
959}
960
Takashi Iwai03c34372017-02-02 16:19:03 +0100961#define MAX_CNT 0xFF
962
Takashi Iwaie1b239f32017-02-03 00:01:18 +0100963/*
964 * The interrupt status 'sticky' bits might not be cleared by
965 * setting '1' to that bit once...
966 */
967static void wait_clear_underrun_bit(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530968{
Takashi Iwaie1b239f32017-02-03 00:01:18 +0100969 int i;
970 u32 val;
971
972 for (i = 0; i < MAX_CNT; i++) {
973 /* clear bit30, 31 AUD_HDMI_STATUS */
974 had_read_register(intelhaddata, AUD_HDMI_STATUS, &val);
Takashi Iwai77531be2017-02-07 12:17:23 +0100975 if (!(val & AUD_HDMI_STATUS_MASK_UNDERRUN))
Takashi Iwaie1b239f32017-02-03 00:01:18 +0100976 return;
977 had_write_register(intelhaddata, AUD_HDMI_STATUS, val);
978 }
979 dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n");
980}
981
982/* called from irq handler */
983static void had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
984{
985 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530986
987 /* Handle Underrun interrupt within Audio Unit */
Takashi Iwai79dda752017-01-30 17:23:39 +0100988 had_write_register(intelhaddata, AUD_CONFIG, 0);
Takashi Iwai40ce4b52017-02-07 16:17:06 +0100989 intelhaddata->aud_config.regval = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530990 /* Reset buffer pointers */
Takashi Iwaif4566aa2017-02-04 21:39:56 +0100991 had_reset_audio(intelhaddata);
Takashi Iwaie1b239f32017-02-03 00:01:18 +0100992
993 wait_clear_underrun_bit(intelhaddata);
994
995 if (!intelhaddata->connected)
996 return; /* disconnected? - bail out */
997
998 /* Report UNDERRUN error to above layers */
999 substream = had_substream_get(intelhaddata);
1000 if (substream) {
1001 snd_pcm_stop_xrun(substream);
1002 had_substream_put(intelhaddata);
1003 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301004}
1005
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001006/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001007 * ALSA PCM open callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301008 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001009static int had_pcm_open(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301010{
1011 struct snd_intelhad *intelhaddata;
1012 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301013 int retval;
1014
Jerome Anand5dab11d2017-01-25 04:27:52 +05301015 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301016 runtime = substream->runtime;
1017
Takashi Iwai182cdf22017-02-02 14:43:39 +01001018 pm_runtime_get_sync(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301019
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001020 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001021 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
1022 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301023 retval = -ENODEV;
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001024 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301025 }
1026
1027 /* set the runtime hw parameter with local snd_pcm_hardware struct */
Takashi Iwaib5562902017-02-04 22:05:33 +01001028 runtime->hw = had_pcm_hardware;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301029
Jerome Anand5dab11d2017-01-25 04:27:52 +05301030 retval = snd_pcm_hw_constraint_integer(runtime,
1031 SNDRV_PCM_HW_PARAM_PERIODS);
1032 if (retval < 0)
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001033 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301034
1035 /* Make sure, that the period size is always aligned
1036 * 64byte boundary
1037 */
1038 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1039 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
Takashi Iwai73997b02017-02-02 17:38:50 +01001040 if (retval < 0)
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001041 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301042
Takashi Iwai73997b02017-02-02 17:38:50 +01001043 /* expose PCM substream */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001044 spin_lock_irq(&intelhaddata->had_spinlock);
1045 intelhaddata->stream_info.substream = substream;
1046 intelhaddata->stream_info.substream_refcount++;
1047 spin_unlock_irq(&intelhaddata->had_spinlock);
1048
Jerome Anand5dab11d2017-01-25 04:27:52 +05301049 return retval;
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001050 error:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301051 pm_runtime_put(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301052 return retval;
1053}
1054
Takashi Iwaidf76df12017-01-31 16:04:10 +01001055/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001056 * ALSA PCM close callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301057 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001058static int had_pcm_close(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301059{
1060 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301061
Jerome Anand5dab11d2017-01-25 04:27:52 +05301062 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301063
Takashi Iwai73997b02017-02-02 17:38:50 +01001064 /* unreference and sync with the pending PCM accesses */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001065 spin_lock_irq(&intelhaddata->had_spinlock);
1066 intelhaddata->stream_info.substream = NULL;
1067 intelhaddata->stream_info.substream_refcount--;
1068 while (intelhaddata->stream_info.substream_refcount > 0) {
1069 spin_unlock_irq(&intelhaddata->had_spinlock);
1070 cpu_relax();
1071 spin_lock_irq(&intelhaddata->had_spinlock);
1072 }
1073 spin_unlock_irq(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301074
Jerome Anand5dab11d2017-01-25 04:27:52 +05301075 pm_runtime_put(intelhaddata->dev);
1076 return 0;
1077}
1078
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001079/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001080 * ALSA PCM hw_params callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301081 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001082static int had_pcm_hw_params(struct snd_pcm_substream *substream,
1083 struct snd_pcm_hw_params *hw_params)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301084{
Takashi Iwaic75b0472017-01-31 15:49:15 +01001085 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301086 unsigned long addr;
1087 int pages, buf_size, retval;
1088
Takashi Iwaic75b0472017-01-31 15:49:15 +01001089 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301090 buf_size = params_buffer_bytes(hw_params);
1091 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1092 if (retval < 0)
1093 return retval;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001094 dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
1095 __func__, buf_size);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301096 /* mark the pages as uncached region */
1097 addr = (unsigned long) substream->runtime->dma_area;
1098 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1099 retval = set_memory_uc(addr, pages);
1100 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001101 dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
1102 retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301103 return retval;
1104 }
1105 memset(substream->runtime->dma_area, 0, buf_size);
1106
1107 return retval;
1108}
1109
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001110/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001111 * ALSA PCM hw_free callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301112 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001113static int had_pcm_hw_free(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301114{
1115 unsigned long addr;
1116 u32 pages;
1117
Jerome Anand5dab11d2017-01-25 04:27:52 +05301118 /* mark back the pages as cached/writeback region before the free */
1119 if (substream->runtime->dma_area != NULL) {
1120 addr = (unsigned long) substream->runtime->dma_area;
1121 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1122 PAGE_SIZE;
1123 set_memory_wb(addr, pages);
1124 return snd_pcm_lib_free_pages(substream);
1125 }
1126 return 0;
1127}
1128
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001129/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001130 * ALSA PCM trigger callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301131 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001132static int had_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301133{
Takashi Iwaida864802017-01-31 13:52:22 +01001134 int retval = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301135 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301136
Jerome Anand5dab11d2017-01-25 04:27:52 +05301137 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301138
1139 switch (cmd) {
1140 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001141 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1142 case SNDRV_PCM_TRIGGER_RESUME:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301143 /* Disable local INTRs till register prgmng is done */
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001144 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001145 dev_dbg(intelhaddata->dev,
1146 "_START: HDMI cable plugged-out\n");
Jerome Anand5dab11d2017-01-25 04:27:52 +05301147 retval = -ENODEV;
1148 break;
1149 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301150
Takashi Iwaif69bd102017-02-02 14:57:22 +01001151 intelhaddata->stream_info.running = true;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301152
1153 /* Enable Audio */
Takashi Iwai075a1d42017-02-07 07:55:27 +01001154 had_ack_irqs(intelhaddata); /* FIXME: do we need this? */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001155 had_enable_audio(intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301156 break;
1157
1158 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001159 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1160 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwaibcce7752017-02-01 17:18:20 +01001161 spin_lock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301162
Takashi Iwaic75b0472017-01-31 15:49:15 +01001163 /* Stop reporting BUFFER_DONE/UNDERRUN to above layers */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301164
Takashi Iwaif69bd102017-02-02 14:57:22 +01001165 intelhaddata->stream_info.running = false;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001166 spin_unlock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301167 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001168 had_enable_audio(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301169 /* Reset buffer pointers */
Takashi Iwaif4566aa2017-02-04 21:39:56 +01001170 had_reset_audio(intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301171 break;
1172
1173 default:
1174 retval = -EINVAL;
1175 }
1176 return retval;
1177}
1178
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001179/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001180 * ALSA PCM prepare callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301181 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001182static int had_pcm_prepare(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301183{
1184 int retval;
1185 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001186 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301187 struct snd_intelhad *intelhaddata;
1188 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301189
Jerome Anand5dab11d2017-01-25 04:27:52 +05301190 intelhaddata = snd_pcm_substream_chip(substream);
1191 runtime = substream->runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301192
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001193 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001194 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
1195 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301196 retval = -ENODEV;
1197 goto prep_end;
1198 }
1199
Takashi Iwaic75b0472017-01-31 15:49:15 +01001200 dev_dbg(intelhaddata->dev, "period_size=%d\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301201 (int)frames_to_bytes(runtime, runtime->period_size));
Takashi Iwaic75b0472017-01-31 15:49:15 +01001202 dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods);
1203 dev_dbg(intelhaddata->dev, "buffer_size=%d\n",
1204 (int)snd_pcm_lib_buffer_bytes(substream));
1205 dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
1206 dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301207
Jerome Anand5dab11d2017-01-25 04:27:52 +05301208 /* Get N value in KHz */
Takashi Iwaida864802017-01-31 13:52:22 +01001209 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301210
Takashi Iwaib5562902017-02-04 22:05:33 +01001211 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301212 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001213 dev_err(intelhaddata->dev,
1214 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301215 goto prep_end;
1216 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001217
1218 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001219 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001220
Takashi Iwaib5562902017-02-04 22:05:33 +01001221 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1222 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301223
Takashi Iwaib5562902017-02-04 22:05:33 +01001224 had_prog_dip(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301225
Takashi Iwaib5562902017-02-04 22:05:33 +01001226 retval = had_init_audio_ctrl(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301227
1228 /* Prog buffer address */
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001229 had_init_ringbuf(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301230
1231 /*
1232 * Program channel mapping in following order:
1233 * FL, FR, C, LFE, RL, RR
1234 */
1235
Takashi Iwai79dda752017-01-30 17:23:39 +01001236 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301237
1238prep_end:
1239 return retval;
1240}
1241
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001242/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001243 * ALSA PCM pointer callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301244 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001245static snd_pcm_uframes_t had_pcm_pointer(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301246{
1247 struct snd_intelhad *intelhaddata;
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001248 int len;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301249
Jerome Anand5dab11d2017-01-25 04:27:52 +05301250 intelhaddata = snd_pcm_substream_chip(substream);
1251
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001252 if (!intelhaddata->connected)
Takashi Iwai79f439e2017-01-31 16:46:44 +01001253 return SNDRV_PCM_POS_XRUN;
1254
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001255 len = had_process_ringbuf(substream, intelhaddata);
1256 if (len < 0)
1257 return SNDRV_PCM_POS_XRUN;
1258 return bytes_to_frames(substream->runtime, len);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301259}
1260
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001261/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001262 * ALSA PCM mmap callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301263 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001264static int had_pcm_mmap(struct snd_pcm_substream *substream,
1265 struct vm_area_struct *vma)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301266{
Jerome Anand5dab11d2017-01-25 04:27:52 +05301267 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1268 return remap_pfn_range(vma, vma->vm_start,
1269 substream->dma_buffer.addr >> PAGE_SHIFT,
1270 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1271}
1272
Takashi Iwai73997b02017-02-02 17:38:50 +01001273/*
1274 * ALSA PCM ops
1275 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001276static const struct snd_pcm_ops had_pcm_ops = {
1277 .open = had_pcm_open,
1278 .close = had_pcm_close,
Takashi Iwai73997b02017-02-02 17:38:50 +01001279 .ioctl = snd_pcm_lib_ioctl,
Takashi Iwaib5562902017-02-04 22:05:33 +01001280 .hw_params = had_pcm_hw_params,
1281 .hw_free = had_pcm_hw_free,
1282 .prepare = had_pcm_prepare,
1283 .trigger = had_pcm_trigger,
1284 .pointer = had_pcm_pointer,
1285 .mmap = had_pcm_mmap,
Takashi Iwai73997b02017-02-02 17:38:50 +01001286};
1287
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001288/* process mode change of the running stream; called in mutex */
Takashi Iwaib5562902017-02-04 22:05:33 +01001289static int had_process_mode_change(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301290{
Takashi Iwaida864802017-01-31 13:52:22 +01001291 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301292 int retval = 0;
1293 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001294 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301295
Takashi Iwai313d9f22017-02-02 13:00:12 +01001296 substream = had_substream_get(intelhaddata);
1297 if (!substream)
Takashi Iwaida864802017-01-31 13:52:22 +01001298 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301299
1300 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001301 had_enable_audio(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301302
1303 /* Update CTS value */
Takashi Iwaida864802017-01-31 13:52:22 +01001304 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301305
Takashi Iwaib5562902017-02-04 22:05:33 +01001306 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301307 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001308 dev_err(intelhaddata->dev,
1309 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301310 goto out;
1311 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001312
1313 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001314 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001315
Takashi Iwaib5562902017-02-04 22:05:33 +01001316 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1317 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301318
1319 /* Enable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001320 had_enable_audio(intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301321
1322out:
Takashi Iwai313d9f22017-02-02 13:00:12 +01001323 had_substream_put(intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301324 return retval;
1325}
1326
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001327/* process hot plug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001328static void had_process_hot_plug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001329{
Takashi Iwai372d8552017-01-31 13:57:58 +01001330 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001331
Takashi Iwaibcce7752017-02-01 17:18:20 +01001332 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001333 if (intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001334 dev_dbg(intelhaddata->dev, "Device already connected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001335 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001336 return;
Takashi Iwai372d8552017-01-31 13:57:58 +01001337 }
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001338
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001339 intelhaddata->connected = true;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001340 dev_dbg(intelhaddata->dev,
1341 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001342 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001343 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001344
Takashi Iwai372d8552017-01-31 13:57:58 +01001345 /* Safety check */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001346 substream = had_substream_get(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001347 if (substream) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001348 dev_dbg(intelhaddata->dev,
1349 "Force to stop the active stream by disconnection\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001350 /* Set runtime->state to hw_params done */
1351 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001352 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001353 }
1354
1355 had_build_channel_allocation_map(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001356}
1357
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001358/* process hot unplug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001359static void had_process_hot_unplug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001360{
Takashi Iwai313d9f22017-02-02 13:00:12 +01001361 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001362
Takashi Iwai313d9f22017-02-02 13:00:12 +01001363 substream = had_substream_get(intelhaddata);
1364
Takashi Iwaibcce7752017-02-01 17:18:20 +01001365 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001366
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001367 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001368 dev_dbg(intelhaddata->dev, "Device already disconnected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001369 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001370 goto out;
Takashi Iwai372d8552017-01-31 13:57:58 +01001371
Takashi Iwai372d8552017-01-31 13:57:58 +01001372 }
1373
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001374 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001375 had_enable_audio(intelhaddata, false);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001376
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001377 intelhaddata->connected = false;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001378 dev_dbg(intelhaddata->dev,
1379 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001380 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001381 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001382
1383 /* Report to above ALSA layer */
1384 if (substream)
1385 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1386
1387 out:
1388 if (substream)
1389 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001390 kfree(intelhaddata->chmap->chmap);
1391 intelhaddata->chmap->chmap = NULL;
Takashi Iwai372d8552017-01-31 13:57:58 +01001392}
1393
Takashi Iwai73997b02017-02-02 17:38:50 +01001394/*
1395 * ALSA iec958 and ELD controls
1396 */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301397
Jerome Anand5dab11d2017-01-25 04:27:52 +05301398static int had_iec958_info(struct snd_kcontrol *kcontrol,
1399 struct snd_ctl_elem_info *uinfo)
1400{
1401 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1402 uinfo->count = 1;
1403 return 0;
1404}
1405
1406static int had_iec958_get(struct snd_kcontrol *kcontrol,
1407 struct snd_ctl_elem_value *ucontrol)
1408{
1409 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1410
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001411 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301412 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1413 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1414 ucontrol->value.iec958.status[2] =
1415 (intelhaddata->aes_bits >> 16) & 0xff;
1416 ucontrol->value.iec958.status[3] =
1417 (intelhaddata->aes_bits >> 24) & 0xff;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001418 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301419 return 0;
1420}
Takashi Iwai372d8552017-01-31 13:57:58 +01001421
Jerome Anand5dab11d2017-01-25 04:27:52 +05301422static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1423 struct snd_ctl_elem_value *ucontrol)
1424{
1425 ucontrol->value.iec958.status[0] = 0xff;
1426 ucontrol->value.iec958.status[1] = 0xff;
1427 ucontrol->value.iec958.status[2] = 0xff;
1428 ucontrol->value.iec958.status[3] = 0xff;
1429 return 0;
1430}
Takashi Iwai372d8552017-01-31 13:57:58 +01001431
Jerome Anand5dab11d2017-01-25 04:27:52 +05301432static int had_iec958_put(struct snd_kcontrol *kcontrol,
1433 struct snd_ctl_elem_value *ucontrol)
1434{
1435 unsigned int val;
1436 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001437 int changed = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301438
Jerome Anand5dab11d2017-01-25 04:27:52 +05301439 val = (ucontrol->value.iec958.status[0] << 0) |
1440 (ucontrol->value.iec958.status[1] << 8) |
1441 (ucontrol->value.iec958.status[2] << 16) |
1442 (ucontrol->value.iec958.status[3] << 24);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001443 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301444 if (intelhaddata->aes_bits != val) {
1445 intelhaddata->aes_bits = val;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001446 changed = 1;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301447 }
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001448 mutex_unlock(&intelhaddata->mutex);
1449 return changed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301450}
1451
Takashi Iwai4aedb942017-02-02 16:38:39 +01001452static int had_ctl_eld_info(struct snd_kcontrol *kcontrol,
1453 struct snd_ctl_elem_info *uinfo)
1454{
1455 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1456 uinfo->count = HDMI_MAX_ELD_BYTES;
1457 return 0;
1458}
1459
1460static int had_ctl_eld_get(struct snd_kcontrol *kcontrol,
1461 struct snd_ctl_elem_value *ucontrol)
1462{
1463 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1464
1465 mutex_lock(&intelhaddata->mutex);
1466 memcpy(ucontrol->value.bytes.data, intelhaddata->eld,
1467 HDMI_MAX_ELD_BYTES);
1468 mutex_unlock(&intelhaddata->mutex);
1469 return 0;
1470}
1471
Takashi Iwai73997b02017-02-02 17:38:50 +01001472static const struct snd_kcontrol_new had_controls[] = {
Takashi Iwai4aedb942017-02-02 16:38:39 +01001473 {
1474 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1475 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1476 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1477 .info = had_iec958_info, /* shared */
1478 .get = had_iec958_mask_get,
1479 },
1480 {
1481 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1482 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1483 .info = had_iec958_info,
1484 .get = had_iec958_get,
1485 .put = had_iec958_put,
1486 },
1487 {
1488 .access = (SNDRV_CTL_ELEM_ACCESS_READ |
1489 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1490 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1491 .name = "ELD",
1492 .info = had_ctl_eld_info,
1493 .get = had_ctl_eld_get,
1494 },
Jerome Anand5dab11d2017-01-25 04:27:52 +05301495};
1496
Takashi Iwai73997b02017-02-02 17:38:50 +01001497/*
1498 * audio interrupt handler
1499 */
Takashi Iwaida864802017-01-31 13:52:22 +01001500static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id)
1501{
1502 struct snd_intelhad *ctx = dev_id;
1503 u32 audio_stat, audio_reg;
1504
Takashi Iwai4151ee82017-01-31 18:14:15 +01001505 audio_reg = AUD_HDMI_STATUS;
Takashi Iwai83af57d2017-02-03 08:50:06 +01001506 had_read_register(ctx, audio_reg, &audio_stat);
Takashi Iwaida864802017-01-31 13:52:22 +01001507
1508 if (audio_stat & HDMI_AUDIO_UNDERRUN) {
Takashi Iwai83af57d2017-02-03 08:50:06 +01001509 had_write_register(ctx, audio_reg, HDMI_AUDIO_UNDERRUN);
Takashi Iwaida864802017-01-31 13:52:22 +01001510 had_process_buffer_underrun(ctx);
1511 }
1512
1513 if (audio_stat & HDMI_AUDIO_BUFFER_DONE) {
Takashi Iwai83af57d2017-02-03 08:50:06 +01001514 had_write_register(ctx, audio_reg, HDMI_AUDIO_BUFFER_DONE);
Takashi Iwaida864802017-01-31 13:52:22 +01001515 had_process_buffer_done(ctx);
1516 }
1517
1518 return IRQ_HANDLED;
1519}
1520
Takashi Iwai73997b02017-02-02 17:38:50 +01001521/*
1522 * monitor plug/unplug notification from i915; just kick off the work
1523 */
Takashi Iwaida864802017-01-31 13:52:22 +01001524static void notify_audio_lpe(struct platform_device *pdev)
1525{
1526 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Takashi Iwaida864802017-01-31 13:52:22 +01001527
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001528 schedule_work(&ctx->hdmi_audio_wq);
1529}
Takashi Iwaida864802017-01-31 13:52:22 +01001530
Takashi Iwai73997b02017-02-02 17:38:50 +01001531/* the work to handle monitor hot plug/unplug */
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001532static void had_audio_wq(struct work_struct *work)
1533{
1534 struct snd_intelhad *ctx =
1535 container_of(work, struct snd_intelhad, hdmi_audio_wq);
1536 struct intel_hdmi_lpe_audio_pdata *pdata = ctx->dev->platform_data;
1537
Takashi Iwai182cdf22017-02-02 14:43:39 +01001538 pm_runtime_get_sync(ctx->dev);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001539 mutex_lock(&ctx->mutex);
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001540 if (!pdata->hdmi_connected) {
1541 dev_dbg(ctx->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001542 __func__);
Takashi Iwai4aedb942017-02-02 16:38:39 +01001543 memset(ctx->eld, 0, sizeof(ctx->eld)); /* clear the old ELD */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001544 had_process_hot_unplug(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001545 } else {
1546 struct intel_hdmi_lpe_audio_eld *eld = &pdata->eld;
1547
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001548 dev_dbg(ctx->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
1549 __func__, eld->port_id, pdata->tmds_clock_speed);
1550
Takashi Iwaida864802017-01-31 13:52:22 +01001551 switch (eld->pipe_id) {
1552 case 0:
1553 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1554 break;
1555 case 1:
1556 ctx->had_config_offset = AUDIO_HDMI_CONFIG_B;
1557 break;
1558 case 2:
1559 ctx->had_config_offset = AUDIO_HDMI_CONFIG_C;
1560 break;
1561 default:
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001562 dev_dbg(ctx->dev, "Invalid pipe %d\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001563 eld->pipe_id);
1564 break;
1565 }
1566
Takashi Iwaidf0435d2017-02-02 15:37:11 +01001567 memcpy(ctx->eld, eld->eld_data, sizeof(ctx->eld));
Takashi Iwaida864802017-01-31 13:52:22 +01001568
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001569 ctx->dp_output = pdata->dp_output;
1570 ctx->tmds_clock_speed = pdata->tmds_clock_speed;
1571 ctx->link_rate = pdata->link_rate;
1572
Takashi Iwaida864802017-01-31 13:52:22 +01001573 had_process_hot_plug(ctx);
1574
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001575 /* Process mode change if stream is active */
Takashi Iwaib5562902017-02-04 22:05:33 +01001576 had_process_mode_change(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001577 }
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001578 mutex_unlock(&ctx->mutex);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001579 pm_runtime_put(ctx->dev);
1580}
1581
1582/*
1583 * PM callbacks
1584 */
1585
1586static int hdmi_lpe_audio_runtime_suspend(struct device *dev)
1587{
1588 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1589 struct snd_pcm_substream *substream;
1590
1591 substream = had_substream_get(ctx);
1592 if (substream) {
1593 snd_pcm_suspend(substream);
1594 had_substream_put(ctx);
1595 }
1596
1597 return 0;
1598}
1599
Arnd Bergmann1df98922017-02-07 14:38:51 +01001600static int __maybe_unused hdmi_lpe_audio_suspend(struct device *dev)
Takashi Iwai182cdf22017-02-02 14:43:39 +01001601{
1602 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1603 int err;
1604
1605 err = hdmi_lpe_audio_runtime_suspend(dev);
1606 if (!err)
1607 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D3hot);
1608 return err;
1609}
1610
Arnd Bergmann1df98922017-02-07 14:38:51 +01001611static int __maybe_unused hdmi_lpe_audio_resume(struct device *dev)
Takashi Iwai182cdf22017-02-02 14:43:39 +01001612{
1613 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1614
1615 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D0);
1616 return 0;
Takashi Iwaida864802017-01-31 13:52:22 +01001617}
1618
1619/* release resources */
1620static void hdmi_lpe_audio_free(struct snd_card *card)
1621{
1622 struct snd_intelhad *ctx = card->private_data;
1623
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001624 cancel_work_sync(&ctx->hdmi_audio_wq);
1625
Takashi Iwaida864802017-01-31 13:52:22 +01001626 if (ctx->mmio_start)
1627 iounmap(ctx->mmio_start);
1628 if (ctx->irq >= 0)
1629 free_irq(ctx->irq, ctx);
1630}
1631
1632/*
1633 * hdmi_lpe_audio_probe - start bridge with i915
1634 *
1635 * This function is called when the i915 driver creates the
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001636 * hdmi-lpe-audio platform device.
Takashi Iwaida864802017-01-31 13:52:22 +01001637 */
1638static int hdmi_lpe_audio_probe(struct platform_device *pdev)
1639{
1640 struct snd_card *card;
1641 struct snd_intelhad *ctx;
1642 struct snd_pcm *pcm;
1643 struct intel_hdmi_lpe_audio_pdata *pdata;
1644 int irq;
1645 struct resource *res_mmio;
Takashi Iwai4aedb942017-02-02 16:38:39 +01001646 int i, ret;
Takashi Iwaida864802017-01-31 13:52:22 +01001647
Takashi Iwaida864802017-01-31 13:52:22 +01001648 pdata = pdev->dev.platform_data;
1649 if (!pdata) {
1650 dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
1651 return -EINVAL;
1652 }
1653
1654 /* get resources */
1655 irq = platform_get_irq(pdev, 0);
1656 if (irq < 0) {
1657 dev_err(&pdev->dev, "Could not get irq resource\n");
1658 return -ENODEV;
1659 }
1660
1661 res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1662 if (!res_mmio) {
1663 dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
1664 return -ENXIO;
1665 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301666
Takashi Iwai5647aec2017-01-31 08:14:34 +01001667 /* create a card instance with ALSA framework */
Takashi Iwaida864802017-01-31 13:52:22 +01001668 ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
1669 THIS_MODULE, sizeof(*ctx), &card);
1670 if (ret)
1671 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301672
Takashi Iwaida864802017-01-31 13:52:22 +01001673 ctx = card->private_data;
1674 spin_lock_init(&ctx->had_spinlock);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001675 mutex_init(&ctx->mutex);
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001676 ctx->connected = false;
Takashi Iwaida864802017-01-31 13:52:22 +01001677 ctx->dev = &pdev->dev;
1678 ctx->card = card;
Takashi Iwaida864802017-01-31 13:52:22 +01001679 ctx->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
1680 strcpy(card->driver, INTEL_HAD);
Takashi Iwai873ab032017-02-07 12:14:04 +01001681 strcpy(card->shortname, "Intel HDMI/DP LPE Audio");
1682 strcpy(card->longname, "Intel HDMI/DP LPE Audio");
Jerome Anand5dab11d2017-01-25 04:27:52 +05301683
Takashi Iwaida864802017-01-31 13:52:22 +01001684 ctx->irq = -1;
1685 ctx->tmds_clock_speed = DIS_SAMPLE_RATE_148_5;
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001686 INIT_WORK(&ctx->hdmi_audio_wq, had_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301687
Takashi Iwaida864802017-01-31 13:52:22 +01001688 card->private_free = hdmi_lpe_audio_free;
1689
1690 /* assume pipe A as default */
1691 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1692
1693 platform_set_drvdata(pdev, ctx);
1694
1695 dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
1696 __func__, (unsigned int)res_mmio->start,
1697 (unsigned int)res_mmio->end);
1698
1699 ctx->mmio_start = ioremap_nocache(res_mmio->start,
1700 (size_t)(resource_size(res_mmio)));
1701 if (!ctx->mmio_start) {
1702 dev_err(&pdev->dev, "Could not get ioremap\n");
1703 ret = -EACCES;
1704 goto err;
1705 }
1706
1707 /* setup interrupt handler */
1708 ret = request_irq(irq, display_pipe_interrupt_handler, 0,
1709 pdev->name, ctx);
1710 if (ret < 0) {
1711 dev_err(&pdev->dev, "request_irq failed\n");
1712 goto err;
1713 }
1714
1715 ctx->irq = irq;
1716
1717 ret = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
1718 MAX_CAP_STREAMS, &pcm);
1719 if (ret)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301720 goto err;
1721
1722 /* setup private data which can be retrieved when required */
Takashi Iwaida864802017-01-31 13:52:22 +01001723 pcm->private_data = ctx;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301724 pcm->info_flags = 0;
1725 strncpy(pcm->name, card->shortname, strlen(card->shortname));
Takashi Iwaida864802017-01-31 13:52:22 +01001726 /* setup the ops for playabck */
Takashi Iwaib5562902017-02-04 22:05:33 +01001727 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &had_pcm_ops);
Takashi Iwai412bbe7d52017-02-02 22:03:22 +01001728
1729 /* only 32bit addressable */
1730 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
1731 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1732
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001733 /* allocate dma pages;
1734 * try to allocate 600k buffer as default which is large enough
Jerome Anand5dab11d2017-01-25 04:27:52 +05301735 */
Takashi Iwaida864802017-01-31 13:52:22 +01001736 snd_pcm_lib_preallocate_pages_for_all(pcm,
Jerome Anand5dab11d2017-01-25 04:27:52 +05301737 SNDRV_DMA_TYPE_DEV, NULL,
Takashi Iwaie1b239f32017-02-03 00:01:18 +01001738 HAD_DEFAULT_BUFFER, HAD_MAX_BUFFER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301739
Takashi Iwai4aedb942017-02-02 16:38:39 +01001740 /* create controls */
1741 for (i = 0; i < ARRAY_SIZE(had_controls); i++) {
1742 ret = snd_ctl_add(card, snd_ctl_new1(&had_controls[i], ctx));
1743 if (ret < 0)
1744 goto err;
1745 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301746
1747 init_channel_allocations();
1748
1749 /* Register channel map controls */
Takashi Iwaida864802017-01-31 13:52:22 +01001750 ret = had_register_chmap_ctls(ctx, pcm);
1751 if (ret < 0)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301752 goto err;
1753
Takashi Iwaida864802017-01-31 13:52:22 +01001754 ret = snd_card_register(card);
1755 if (ret)
Takashi Iwai36ec0d92017-01-31 08:47:05 +01001756 goto err;
1757
Takashi Iwaibcce7752017-02-01 17:18:20 +01001758 spin_lock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001759 pdata->notify_audio_lpe = notify_audio_lpe;
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001760 pdata->notify_pending = false;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001761 spin_unlock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001762
1763 pm_runtime_set_active(&pdev->dev);
1764 pm_runtime_enable(&pdev->dev);
1765
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001766 dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
Takashi Iwaida864802017-01-31 13:52:22 +01001767 schedule_work(&ctx->hdmi_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301768
Takashi Iwai79dda752017-01-30 17:23:39 +01001769 return 0;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001770
Jerome Anand5dab11d2017-01-25 04:27:52 +05301771err:
1772 snd_card_free(card);
Takashi Iwaida864802017-01-31 13:52:22 +01001773 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301774}
1775
Takashi Iwai79dda752017-01-30 17:23:39 +01001776/*
Takashi Iwaida864802017-01-31 13:52:22 +01001777 * hdmi_lpe_audio_remove - stop bridge with i915
Jerome Anand5dab11d2017-01-25 04:27:52 +05301778 *
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001779 * This function is called when the platform device is destroyed.
Jerome Anand5dab11d2017-01-25 04:27:52 +05301780 */
Takashi Iwaida864802017-01-31 13:52:22 +01001781static int hdmi_lpe_audio_remove(struct platform_device *pdev)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301782{
Takashi Iwaida864802017-01-31 13:52:22 +01001783 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301784
Takashi Iwaida864802017-01-31 13:52:22 +01001785 snd_card_free(ctx->card);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301786 return 0;
1787}
1788
Takashi Iwai182cdf22017-02-02 14:43:39 +01001789static const struct dev_pm_ops hdmi_lpe_audio_pm = {
1790 SET_SYSTEM_SLEEP_PM_OPS(hdmi_lpe_audio_suspend, hdmi_lpe_audio_resume)
1791 SET_RUNTIME_PM_OPS(hdmi_lpe_audio_runtime_suspend, NULL, NULL)
1792};
1793
Takashi Iwaida864802017-01-31 13:52:22 +01001794static struct platform_driver hdmi_lpe_audio_driver = {
1795 .driver = {
1796 .name = "hdmi-lpe-audio",
Takashi Iwai182cdf22017-02-02 14:43:39 +01001797 .pm = &hdmi_lpe_audio_pm,
Takashi Iwaida864802017-01-31 13:52:22 +01001798 },
1799 .probe = hdmi_lpe_audio_probe,
1800 .remove = hdmi_lpe_audio_remove,
Takashi Iwaida864802017-01-31 13:52:22 +01001801};
1802
1803module_platform_driver(hdmi_lpe_audio_driver);
1804MODULE_ALIAS("platform:hdmi_lpe_audio");
1805
Jerome Anand5dab11d2017-01-25 04:27:52 +05301806MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1807MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1808MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1809MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1810MODULE_DESCRIPTION("Intel HDMI Audio driver");
1811MODULE_LICENSE("GPL v2");
1812MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");