blob: 8d67031e8429522c187523f3ef36f8ee0b645b10 [file] [log] [blame]
Jerome Anand5dab11d2017-01-25 04:27:52 +05301/*
2 * intel_hdmi_audio.c - Intel HDMI audio driver
3 *
4 * Copyright (C) 2016 Intel Corp
5 * Authors: Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>
6 * Ramesh Babu K V <ramesh.babu@intel.com>
7 * Vaibhav Agarwal <vaibhav.agarwal@intel.com>
8 * Jerome Anand <jerome.anand@intel.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 * ALSA driver for Intel HDMI audio
22 */
23
Takashi Iwai03c34372017-02-02 16:19:03 +010024#include <linux/types.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053025#include <linux/platform_device.h>
26#include <linux/io.h>
27#include <linux/slab.h>
28#include <linux/module.h>
Takashi Iwaida864802017-01-31 13:52:22 +010029#include <linux/interrupt.h>
Takashi Iwai03c34372017-02-02 16:19:03 +010030#include <linux/pm_runtime.h>
Takashi Iwai412bbe7d52017-02-02 22:03:22 +010031#include <linux/dma-mapping.h>
Takashi Iwaie2acecf2017-02-11 08:21:56 +010032#include <linux/delay.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053033#include <asm/cacheflush.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053034#include <sound/core.h>
Takashi Iwai03c34372017-02-02 16:19:03 +010035#include <sound/asoundef.h>
36#include <sound/pcm.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053037#include <sound/pcm_params.h>
38#include <sound/initval.h>
39#include <sound/control.h>
Takashi Iwaib9bacf22017-02-14 12:29:38 +010040#include <sound/jack.h>
Takashi Iwai03c34372017-02-02 16:19:03 +010041#include <drm/drm_edid.h>
Takashi Iwaida864802017-01-31 13:52:22 +010042#include <drm/intel_lpe_audio.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053043#include "intel_hdmi_audio.h"
44
Jerome Anand5dab11d2017-01-25 04:27:52 +053045/*standard module options for ALSA. This module supports only one card*/
46static int hdmi_card_index = SNDRV_DEFAULT_IDX1;
47static char *hdmi_card_id = SNDRV_DEFAULT_STR1;
Jerome Anand5dab11d2017-01-25 04:27:52 +053048
49module_param_named(index, hdmi_card_index, int, 0444);
50MODULE_PARM_DESC(index,
51 "Index value for INTEL Intel HDMI Audio controller.");
52module_param_named(id, hdmi_card_id, charp, 0444);
53MODULE_PARM_DESC(id,
54 "ID string for INTEL Intel HDMI Audio controller.");
55
56/*
57 * ELD SA bits in the CEA Speaker Allocation data block
58 */
Takashi Iwai4a5ddb22017-02-01 16:45:38 +010059static const int eld_speaker_allocation_bits[] = {
Jerome Anand5dab11d2017-01-25 04:27:52 +053060 [0] = FL | FR,
61 [1] = LFE,
62 [2] = FC,
63 [3] = RL | RR,
64 [4] = RC,
65 [5] = FLC | FRC,
66 [6] = RLC | RRC,
67 /* the following are not defined in ELD yet */
68 [7] = 0,
69};
70
71/*
72 * This is an ordered list!
73 *
74 * The preceding ones have better chances to be selected by
75 * hdmi_channel_allocation().
76 */
77static struct cea_channel_speaker_allocation channel_allocations[] = {
78/* channel: 7 6 5 4 3 2 1 0 */
79{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
80 /* 2.1 */
81{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
82 /* Dolby Surround */
83{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
84 /* surround40 */
85{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
86 /* surround41 */
87{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
88 /* surround50 */
89{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
90 /* surround51 */
91{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
92 /* 6.1 */
93{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
94 /* surround71 */
95{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
96
97{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
98{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
99{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
100{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
101{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
102{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
103{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
104{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
105{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
106{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
107{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
108{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
109{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
110{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
111{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
112{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
113{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
114{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
115{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
116{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
117{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
118{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
119{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
120};
121
Takashi Iwai4a5ddb22017-02-01 16:45:38 +0100122static const struct channel_map_table map_tables[] = {
Jerome Anand5dab11d2017-01-25 04:27:52 +0530123 { SNDRV_CHMAP_FL, 0x00, FL },
124 { SNDRV_CHMAP_FR, 0x01, FR },
125 { SNDRV_CHMAP_RL, 0x04, RL },
126 { SNDRV_CHMAP_RR, 0x05, RR },
127 { SNDRV_CHMAP_LFE, 0x02, LFE },
128 { SNDRV_CHMAP_FC, 0x03, FC },
129 { SNDRV_CHMAP_RLC, 0x06, RLC },
130 { SNDRV_CHMAP_RRC, 0x07, RRC },
131 {} /* terminator */
132};
133
134/* hardware capability structure */
Takashi Iwaib5562902017-02-04 22:05:33 +0100135static const struct snd_pcm_hardware had_pcm_hardware = {
Jerome Anand5dab11d2017-01-25 04:27:52 +0530136 .info = (SNDRV_PCM_INFO_INTERLEAVED |
Takashi Iwaia9ebdd02017-02-02 21:33:54 +0100137 SNDRV_PCM_INFO_MMAP |
Takashi Iwaie8de9852017-02-07 08:09:12 +0100138 SNDRV_PCM_INFO_MMAP_VALID |
139 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
Takashi Iwai3fe2cf72017-02-07 13:53:42 +0100140 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
141 SNDRV_PCM_FMTBIT_S24_LE |
Takashi Iwai85bd8742017-02-07 13:33:17 +0100142 SNDRV_PCM_FMTBIT_S32_LE),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530143 .rates = SNDRV_PCM_RATE_32000 |
144 SNDRV_PCM_RATE_44100 |
145 SNDRV_PCM_RATE_48000 |
146 SNDRV_PCM_RATE_88200 |
147 SNDRV_PCM_RATE_96000 |
148 SNDRV_PCM_RATE_176400 |
149 SNDRV_PCM_RATE_192000,
150 .rate_min = HAD_MIN_RATE,
151 .rate_max = HAD_MAX_RATE,
152 .channels_min = HAD_MIN_CHANNEL,
153 .channels_max = HAD_MAX_CHANNEL,
154 .buffer_bytes_max = HAD_MAX_BUFFER,
155 .period_bytes_min = HAD_MIN_PERIOD_BYTES,
156 .period_bytes_max = HAD_MAX_PERIOD_BYTES,
157 .periods_min = HAD_MIN_PERIODS,
158 .periods_max = HAD_MAX_PERIODS,
159 .fifo_size = HAD_FIFO_SIZE,
160};
161
Takashi Iwai313d9f22017-02-02 13:00:12 +0100162/* Get the active PCM substream;
163 * Call had_substream_put() for unreferecing.
164 * Don't call this inside had_spinlock, as it takes by itself
165 */
166static struct snd_pcm_substream *
167had_substream_get(struct snd_intelhad *intelhaddata)
168{
169 struct snd_pcm_substream *substream;
170 unsigned long flags;
171
172 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
173 substream = intelhaddata->stream_info.substream;
174 if (substream)
175 intelhaddata->stream_info.substream_refcount++;
176 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
177 return substream;
178}
179
180/* Unref the active PCM substream;
181 * Don't call this inside had_spinlock, as it takes by itself
182 */
183static void had_substream_put(struct snd_intelhad *intelhaddata)
184{
185 unsigned long flags;
186
187 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
188 intelhaddata->stream_info.substream_refcount--;
189 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
190}
191
Jerome Anand5dab11d2017-01-25 04:27:52 +0530192/* Register access functions */
Takashi Iwai28ed1252017-02-15 22:02:10 +0100193static u32 had_read_register_raw(struct snd_intelhad *ctx, u32 reg)
194{
195 return ioread32(ctx->mmio_start + ctx->had_config_offset + reg);
196}
197
198static void had_write_register_raw(struct snd_intelhad *ctx, u32 reg, u32 val)
199{
200 iowrite32(val, ctx->mmio_start + ctx->had_config_offset + reg);
201}
202
Takashi Iwai83af57d2017-02-03 08:50:06 +0100203static void had_read_register(struct snd_intelhad *ctx, u32 reg, u32 *val)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530204{
Takashi Iwai28ed1252017-02-15 22:02:10 +0100205 if (!ctx->connected)
206 *val = 0;
207 else
208 *val = had_read_register_raw(ctx, reg);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530209}
210
Takashi Iwai83af57d2017-02-03 08:50:06 +0100211static void had_write_register(struct snd_intelhad *ctx, u32 reg, u32 val)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530212{
Takashi Iwai28ed1252017-02-15 22:02:10 +0100213 if (ctx->connected)
214 had_write_register_raw(ctx, reg, val);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530215}
216
Takashi Iwaida864802017-01-31 13:52:22 +0100217/*
Takashi Iwai313d9f22017-02-02 13:00:12 +0100218 * enable / disable audio configuration
219 *
Takashi Iwai83af57d2017-02-03 08:50:06 +0100220 * The normal read/modify should not directly be used on VLV2 for
Takashi Iwaida864802017-01-31 13:52:22 +0100221 * updating AUD_CONFIG register.
Jerome Anand5dab11d2017-01-25 04:27:52 +0530222 * This is because:
223 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
224 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
225 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
226 * register. This field should be 1xy binary for configuration with 6 or
227 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
228 * causes the "channels" field to be updated as 0xy binary resulting in
229 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
230 * appropriate value when doing read-modify of AUD_CONFIG register.
Jerome Anand5dab11d2017-01-25 04:27:52 +0530231 */
Takashi Iwai40ce4b52017-02-07 16:17:06 +0100232static void had_enable_audio(struct snd_intelhad *intelhaddata,
Takashi Iwaib5562902017-02-04 22:05:33 +0100233 bool enable)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530234{
Takashi Iwai40ce4b52017-02-07 16:17:06 +0100235 /* update the cached value */
236 intelhaddata->aud_config.regx.aud_en = enable;
237 had_write_register(intelhaddata, AUD_CONFIG,
238 intelhaddata->aud_config.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530239}
240
Takashi Iwai075a1d42017-02-07 07:55:27 +0100241/* forcibly ACKs to both BUFFER_DONE and BUFFER_UNDERRUN interrupts */
242static void had_ack_irqs(struct snd_intelhad *ctx)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530243{
Takashi Iwaida864802017-01-31 13:52:22 +0100244 u32 status_reg;
245
Takashi Iwai28ed1252017-02-15 22:02:10 +0100246 if (!ctx->connected)
247 return;
Takashi Iwai075a1d42017-02-07 07:55:27 +0100248 had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
249 status_reg |= HDMI_AUDIO_BUFFER_DONE | HDMI_AUDIO_UNDERRUN;
250 had_write_register(ctx, AUD_HDMI_STATUS, status_reg);
251 had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
Takashi Iwaida864802017-01-31 13:52:22 +0100252}
253
Takashi Iwaif4566aa2017-02-04 21:39:56 +0100254/* Reset buffer pointers */
255static void had_reset_audio(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530256{
Takashi Iwai77531be2017-02-07 12:17:23 +0100257 had_write_register(intelhaddata, AUD_HDMI_STATUS,
258 AUD_HDMI_STATUSG_MASK_FUNCRST);
Takashi Iwaif4566aa2017-02-04 21:39:56 +0100259 had_write_register(intelhaddata, AUD_HDMI_STATUS, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530260}
261
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100262/*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530263 * initialize audio channel status registers
264 * This function is called in the prepare callback
265 */
266static int had_prog_status_reg(struct snd_pcm_substream *substream,
267 struct snd_intelhad *intelhaddata)
268{
Takashi Iwai7ceba752017-02-02 15:58:35 +0100269 union aud_cfg cfg_val = {.regval = 0};
270 union aud_ch_status_0 ch_stat0 = {.regval = 0};
271 union aud_ch_status_1 ch_stat1 = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530272
Takashi Iwai7ceba752017-02-02 15:58:35 +0100273 ch_stat0.regx.lpcm_id = (intelhaddata->aes_bits &
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100274 IEC958_AES0_NONAUDIO) >> 1;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100275 ch_stat0.regx.clk_acc = (intelhaddata->aes_bits &
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100276 IEC958_AES3_CON_CLOCK) >> 4;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100277 cfg_val.regx.val_bit = ch_stat0.regx.lpcm_id;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530278
279 switch (substream->runtime->rate) {
280 case AUD_SAMPLE_RATE_32:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100281 ch_stat0.regx.samp_freq = CH_STATUS_MAP_32KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530282 break;
283
284 case AUD_SAMPLE_RATE_44_1:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100285 ch_stat0.regx.samp_freq = CH_STATUS_MAP_44KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530286 break;
287 case AUD_SAMPLE_RATE_48:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100288 ch_stat0.regx.samp_freq = CH_STATUS_MAP_48KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530289 break;
290 case AUD_SAMPLE_RATE_88_2:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100291 ch_stat0.regx.samp_freq = CH_STATUS_MAP_88KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530292 break;
293 case AUD_SAMPLE_RATE_96:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100294 ch_stat0.regx.samp_freq = CH_STATUS_MAP_96KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530295 break;
296 case AUD_SAMPLE_RATE_176_4:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100297 ch_stat0.regx.samp_freq = CH_STATUS_MAP_176KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530298 break;
299 case AUD_SAMPLE_RATE_192:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100300 ch_stat0.regx.samp_freq = CH_STATUS_MAP_192KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530301 break;
302
303 default:
304 /* control should never come here */
305 return -EINVAL;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530306 }
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100307
Takashi Iwai79dda752017-01-30 17:23:39 +0100308 had_write_register(intelhaddata,
Takashi Iwai7ceba752017-02-02 15:58:35 +0100309 AUD_CH_STATUS_0, ch_stat0.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530310
Takashi Iwai85bd8742017-02-07 13:33:17 +0100311 switch (substream->runtime->format) {
Takashi Iwai85bd8742017-02-07 13:33:17 +0100312 case SNDRV_PCM_FORMAT_S16_LE:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100313 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_20;
314 ch_stat1.regx.wrd_len = SMPL_WIDTH_16BITS;
Takashi Iwai85bd8742017-02-07 13:33:17 +0100315 break;
Takashi Iwai85bd8742017-02-07 13:33:17 +0100316 case SNDRV_PCM_FORMAT_S24_LE:
317 case SNDRV_PCM_FORMAT_S32_LE:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100318 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_24;
319 ch_stat1.regx.wrd_len = SMPL_WIDTH_24BITS;
Takashi Iwai85bd8742017-02-07 13:33:17 +0100320 break;
321 default:
322 return -EINVAL;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530323 }
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100324
Takashi Iwai79dda752017-01-30 17:23:39 +0100325 had_write_register(intelhaddata,
Takashi Iwai7ceba752017-02-02 15:58:35 +0100326 AUD_CH_STATUS_1, ch_stat1.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530327 return 0;
328}
329
Takashi Iwai76296ef2017-01-30 16:09:11 +0100330/*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530331 * function to initialize audio
332 * registers and buffer confgiuration registers
333 * This function is called in the prepare callback
334 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100335static int had_init_audio_ctrl(struct snd_pcm_substream *substream,
336 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530337{
Takashi Iwai7ceba752017-02-02 15:58:35 +0100338 union aud_cfg cfg_val = {.regval = 0};
339 union aud_buf_config buf_cfg = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530340 u8 channels;
341
342 had_prog_status_reg(substream, intelhaddata);
343
Takashi Iwai7ceba752017-02-02 15:58:35 +0100344 buf_cfg.regx.audio_fifo_watermark = FIFO_THRESHOLD;
345 buf_cfg.regx.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
346 buf_cfg.regx.aud_delay = 0;
347 had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530348
349 channels = substream->runtime->channels;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100350 cfg_val.regx.num_ch = channels - 2;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530351 if (channels <= 2)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100352 cfg_val.regx.layout = LAYOUT0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530353 else
Takashi Iwai7ceba752017-02-02 15:58:35 +0100354 cfg_val.regx.layout = LAYOUT1;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530355
Takashi Iwai3fe2cf72017-02-07 13:53:42 +0100356 if (substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE)
357 cfg_val.regx.packet_mode = 1;
358
Takashi Iwai85bd8742017-02-07 13:33:17 +0100359 if (substream->runtime->format == SNDRV_PCM_FORMAT_S32_LE)
360 cfg_val.regx.left_align = 1;
361
Takashi Iwai7ceba752017-02-02 15:58:35 +0100362 cfg_val.regx.val_bit = 1;
Takashi Iwai83af57d2017-02-03 08:50:06 +0100363
364 /* fix up the DP bits */
365 if (intelhaddata->dp_output) {
366 cfg_val.regx.dp_modei = 1;
367 cfg_val.regx.set = 1;
368 }
369
Takashi Iwai7ceba752017-02-02 15:58:35 +0100370 had_write_register(intelhaddata, AUD_CONFIG, cfg_val.regval);
Takashi Iwai40ce4b52017-02-07 16:17:06 +0100371 intelhaddata->aud_config = cfg_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530372 return 0;
373}
374
Jerome Anand5dab11d2017-01-25 04:27:52 +0530375/*
376 * Compute derived values in channel_allocations[].
377 */
378static void init_channel_allocations(void)
379{
380 int i, j;
381 struct cea_channel_speaker_allocation *p;
382
Jerome Anand5dab11d2017-01-25 04:27:52 +0530383 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
384 p = channel_allocations + i;
385 p->channels = 0;
386 p->spk_mask = 0;
387 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
388 if (p->speakers[j]) {
389 p->channels++;
390 p->spk_mask |= p->speakers[j];
391 }
392 }
393}
394
395/*
396 * The transformation takes two steps:
397 *
398 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
399 * spk_mask => (channel_allocations[]) => ai->CA
400 *
401 * TODO: it could select the wrong CA from multiple candidates.
402 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100403static int had_channel_allocation(struct snd_intelhad *intelhaddata,
404 int channels)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530405{
406 int i;
407 int ca = 0;
408 int spk_mask = 0;
409
410 /*
411 * CA defaults to 0 for basic stereo audio
412 */
413 if (channels <= 2)
414 return 0;
415
416 /*
417 * expand ELD's speaker allocation mask
418 *
419 * ELD tells the speaker mask in a compact(paired) form,
420 * expand ELD's notions to match the ones used by Audio InfoFrame.
421 */
422
423 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100424 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530425 spk_mask |= eld_speaker_allocation_bits[i];
426 }
427
428 /* search for the first working match in the CA table */
429 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
430 if (channels == channel_allocations[i].channels &&
431 (spk_mask & channel_allocations[i].spk_mask) ==
432 channel_allocations[i].spk_mask) {
433 ca = channel_allocations[i].ca_index;
434 break;
435 }
436 }
437
Takashi Iwaic75b0472017-01-31 15:49:15 +0100438 dev_dbg(intelhaddata->dev, "select CA 0x%x for %d\n", ca, channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530439
440 return ca;
441}
442
443/* from speaker bit mask to ALSA API channel position */
444static int spk_to_chmap(int spk)
445{
Takashi Iwai4a5ddb22017-02-01 16:45:38 +0100446 const struct channel_map_table *t = map_tables;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530447
448 for (; t->map; t++) {
449 if (t->spk_mask == spk)
450 return t->map;
451 }
452 return 0;
453}
454
Takashi Iwai372d8552017-01-31 13:57:58 +0100455static void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530456{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100457 int i, c;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530458 int spk_mask = 0;
459 struct snd_pcm_chmap_elem *chmap;
460 u8 eld_high, eld_high_mask = 0xF0;
461 u8 high_msb;
462
Takashi Iwai18353192017-02-15 21:42:20 +0100463 kfree(intelhaddata->chmap->chmap);
464 intelhaddata->chmap->chmap = NULL;
465
Jerome Anand5dab11d2017-01-25 04:27:52 +0530466 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
Takashi Iwai18353192017-02-15 21:42:20 +0100467 if (!chmap)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530468 return;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530469
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100470 dev_dbg(intelhaddata->dev, "eld speaker = %x\n",
471 intelhaddata->eld[DRM_ELD_SPEAKER]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530472
473 /* WA: Fix the max channel supported to 8 */
474
475 /*
476 * Sink may support more than 8 channels, if eld_high has more than
477 * one bit set. SOC supports max 8 channels.
478 * Refer eld_speaker_allocation_bits, for sink speaker allocation
479 */
480
481 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100482 eld_high = intelhaddata->eld[DRM_ELD_SPEAKER] & eld_high_mask;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530483 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
484 /* eld_high & (eld_high-1): if more than 1 bit set */
485 /* 0x1F: 7 channels */
486 for (i = 1; i < 4; i++) {
487 high_msb = eld_high & (0x80 >> i);
488 if (high_msb) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100489 intelhaddata->eld[DRM_ELD_SPEAKER] &=
Jerome Anand5dab11d2017-01-25 04:27:52 +0530490 high_msb | 0xF;
491 break;
492 }
493 }
494 }
495
496 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100497 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530498 spk_mask |= eld_speaker_allocation_bits[i];
499 }
500
501 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
502 if (spk_mask == channel_allocations[i].spk_mask) {
503 for (c = 0; c < channel_allocations[i].channels; c++) {
504 chmap->map[c] = spk_to_chmap(
505 channel_allocations[i].speakers[
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100506 (MAX_SPEAKERS - 1) - c]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530507 }
508 chmap->channels = channel_allocations[i].channels;
509 intelhaddata->chmap->chmap = chmap;
510 break;
511 }
512 }
Takashi Iwai18353192017-02-15 21:42:20 +0100513 if (i >= ARRAY_SIZE(channel_allocations))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530514 kfree(chmap);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530515}
516
517/*
518 * ALSA API channel-map control callbacks
519 */
520static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
521 struct snd_ctl_elem_info *uinfo)
522{
Jerome Anand5dab11d2017-01-25 04:27:52 +0530523 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
524 uinfo->count = HAD_MAX_CHANNEL;
525 uinfo->value.integer.min = 0;
526 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
527 return 0;
528}
529
530static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
531 struct snd_ctl_elem_value *ucontrol)
532{
533 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
534 struct snd_intelhad *intelhaddata = info->private_data;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100535 int i;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530536 const struct snd_pcm_chmap_elem *chmap;
537
Takashi Iwaia72ccfb2017-02-15 21:45:06 +0100538 memset(ucontrol->value.integer.value, 0,
539 sizeof(long) * HAD_MAX_CHANNEL);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100540 mutex_lock(&intelhaddata->mutex);
541 if (!intelhaddata->chmap->chmap) {
542 mutex_unlock(&intelhaddata->mutex);
Takashi Iwaia72ccfb2017-02-15 21:45:06 +0100543 return 0;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100544 }
545
Jerome Anand5dab11d2017-01-25 04:27:52 +0530546 chmap = intelhaddata->chmap->chmap;
Takashi Iwaic75b0472017-01-31 15:49:15 +0100547 for (i = 0; i < chmap->channels; i++)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530548 ucontrol->value.integer.value[i] = chmap->map[i];
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100549 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530550
551 return 0;
552}
553
554static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
555 struct snd_pcm *pcm)
556{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100557 int err;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530558
559 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
560 NULL, 0, (unsigned long)intelhaddata,
561 &intelhaddata->chmap);
562 if (err < 0)
563 return err;
564
565 intelhaddata->chmap->private_data = intelhaddata;
Takashi Iwaie9d65ab2017-01-31 16:11:27 +0100566 intelhaddata->chmap->kctl->info = had_chmap_ctl_info;
567 intelhaddata->chmap->kctl->get = had_chmap_ctl_get;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530568 intelhaddata->chmap->chmap = NULL;
569 return 0;
570}
571
Takashi Iwai76296ef2017-01-30 16:09:11 +0100572/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100573 * Initialize Data Island Packets registers
Jerome Anand5dab11d2017-01-25 04:27:52 +0530574 * This function is called in the prepare callback
575 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100576static void had_prog_dip(struct snd_pcm_substream *substream,
577 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530578{
579 int i;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100580 union aud_ctrl_st ctrl_state = {.regval = 0};
581 union aud_info_frame2 frame2 = {.regval = 0};
582 union aud_info_frame3 frame3 = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530583 u8 checksum = 0;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600584 u32 info_frame;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530585 int channels;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100586 int ca;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530587
588 channels = substream->runtime->channels;
589
Takashi Iwai7ceba752017-02-02 15:58:35 +0100590 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530591
Takashi Iwaib5562902017-02-04 22:05:33 +0100592 ca = had_channel_allocation(intelhaddata, channels);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600593 if (intelhaddata->dp_output) {
594 info_frame = DP_INFO_FRAME_WORD1;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100595 frame2.regval = (substream->runtime->channels - 1) | (ca << 24);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600596 } else {
597 info_frame = HDMI_INFO_FRAME_WORD1;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100598 frame2.regx.chnl_cnt = substream->runtime->channels - 1;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100599 frame3.regx.chnl_alloc = ca;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530600
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100601 /* Calculte the byte wide checksum for all valid DIP words */
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600602 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100603 checksum += (info_frame >> (i * 8)) & 0xff;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600604 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100605 checksum += (frame2.regval >> (i * 8)) & 0xff;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600606 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100607 checksum += (frame3.regval >> (i * 8)) & 0xff;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530608
Takashi Iwai7ceba752017-02-02 15:58:35 +0100609 frame2.regx.chksum = -(checksum);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600610 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530611
Takashi Iwai4151ee82017-01-31 18:14:15 +0100612 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, info_frame);
Takashi Iwai7ceba752017-02-02 15:58:35 +0100613 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame2.regval);
614 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame3.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530615
616 /* program remaining DIP words with zero */
617 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
Takashi Iwai4151ee82017-01-31 18:14:15 +0100618 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, 0x0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530619
Takashi Iwai7ceba752017-02-02 15:58:35 +0100620 ctrl_state.regx.dip_freq = 1;
621 ctrl_state.regx.dip_en_sta = 1;
622 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530623}
624
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600625static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
626{
627 u32 maud_val;
628
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100629 /* Select maud according to DP 1.2 spec */
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600630 if (link_rate == DP_2_7_GHZ) {
631 switch (aud_samp_freq) {
632 case AUD_SAMPLE_RATE_32:
633 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
634 break;
635
636 case AUD_SAMPLE_RATE_44_1:
637 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
638 break;
639
640 case AUD_SAMPLE_RATE_48:
641 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
642 break;
643
644 case AUD_SAMPLE_RATE_88_2:
645 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
646 break;
647
648 case AUD_SAMPLE_RATE_96:
649 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
650 break;
651
652 case AUD_SAMPLE_RATE_176_4:
653 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
654 break;
655
656 case HAD_MAX_RATE:
657 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
658 break;
659
660 default:
661 maud_val = -EINVAL;
662 break;
663 }
664 } else if (link_rate == DP_1_62_GHZ) {
665 switch (aud_samp_freq) {
666 case AUD_SAMPLE_RATE_32:
667 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
668 break;
669
670 case AUD_SAMPLE_RATE_44_1:
671 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
672 break;
673
674 case AUD_SAMPLE_RATE_48:
675 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
676 break;
677
678 case AUD_SAMPLE_RATE_88_2:
679 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
680 break;
681
682 case AUD_SAMPLE_RATE_96:
683 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
684 break;
685
686 case AUD_SAMPLE_RATE_176_4:
687 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
688 break;
689
690 case HAD_MAX_RATE:
691 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
692 break;
693
694 default:
695 maud_val = -EINVAL;
696 break;
697 }
698 } else
699 maud_val = -EINVAL;
700
701 return maud_val;
702}
703
Takashi Iwai76296ef2017-01-30 16:09:11 +0100704/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100705 * Program HDMI audio CTS value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530706 *
707 * @aud_samp_freq: sampling frequency of audio data
708 * @tmds: sampling frequency of the display data
Takashi Iwaib5562902017-02-04 22:05:33 +0100709 * @link_rate: DP link rate
Jerome Anand5dab11d2017-01-25 04:27:52 +0530710 * @n_param: N value, depends on aud_samp_freq
Takashi Iwaib5562902017-02-04 22:05:33 +0100711 * @intelhaddata: substream private data
Jerome Anand5dab11d2017-01-25 04:27:52 +0530712 *
713 * Program CTS register based on the audio and display sampling frequency
714 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100715static void had_prog_cts(u32 aud_samp_freq, u32 tmds, u32 link_rate,
716 u32 n_param, struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530717{
718 u32 cts_val;
719 u64 dividend, divisor;
720
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600721 if (intelhaddata->dp_output) {
722 /* Substitute cts_val with Maud according to DP 1.2 spec*/
723 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
724 } else {
725 /* Calculate CTS according to HDMI 1.3a spec*/
726 dividend = (u64)tmds * n_param*1000;
727 divisor = 128 * aud_samp_freq;
728 cts_val = div64_u64(dividend, divisor);
729 }
Takashi Iwaic75b0472017-01-31 15:49:15 +0100730 dev_dbg(intelhaddata->dev, "TMDS value=%d, N value=%d, CTS Value=%d\n",
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600731 tmds, n_param, cts_val);
Takashi Iwai79dda752017-01-30 17:23:39 +0100732 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530733}
734
735static int had_calculate_n_value(u32 aud_samp_freq)
736{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100737 int n_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530738
739 /* Select N according to HDMI 1.3a spec*/
740 switch (aud_samp_freq) {
741 case AUD_SAMPLE_RATE_32:
742 n_val = 4096;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100743 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530744
745 case AUD_SAMPLE_RATE_44_1:
746 n_val = 6272;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100747 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530748
749 case AUD_SAMPLE_RATE_48:
750 n_val = 6144;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100751 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530752
753 case AUD_SAMPLE_RATE_88_2:
754 n_val = 12544;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100755 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530756
757 case AUD_SAMPLE_RATE_96:
758 n_val = 12288;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100759 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530760
761 case AUD_SAMPLE_RATE_176_4:
762 n_val = 25088;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100763 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530764
765 case HAD_MAX_RATE:
766 n_val = 24576;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100767 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530768
769 default:
770 n_val = -EINVAL;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100771 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530772 }
773 return n_val;
774}
775
Takashi Iwai76296ef2017-01-30 16:09:11 +0100776/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100777 * Program HDMI audio N value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530778 *
779 * @aud_samp_freq: sampling frequency of audio data
780 * @n_param: N value, depends on aud_samp_freq
Takashi Iwaib5562902017-02-04 22:05:33 +0100781 * @intelhaddata: substream private data
Jerome Anand5dab11d2017-01-25 04:27:52 +0530782 *
783 * This function is called in the prepare callback.
784 * It programs based on the audio and display sampling frequency
785 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100786static int had_prog_n(u32 aud_samp_freq, u32 *n_param,
787 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530788{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100789 int n_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530790
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600791 if (intelhaddata->dp_output) {
792 /*
793 * According to DP specs, Maud and Naud values hold
794 * a relationship, which is stated as:
795 * Maud/Naud = 512 * fs / f_LS_Clk
796 * where, fs is the sampling frequency of the audio stream
797 * and Naud is 32768 for Async clock.
798 */
799
800 n_val = DP_NAUD_VAL;
801 } else
802 n_val = had_calculate_n_value(aud_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530803
804 if (n_val < 0)
805 return n_val;
806
Takashi Iwai79dda752017-01-30 17:23:39 +0100807 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530808 *n_param = n_val;
809 return 0;
810}
811
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100812/*
813 * PCM ring buffer handling
814 *
815 * The hardware provides a ring buffer with the fixed 4 buffer descriptors
816 * (BDs). The driver maps these 4 BDs onto the PCM ring buffer. The mapping
817 * moves at each period elapsed. The below illustrates how it works:
818 *
819 * At time=0
820 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
821 * BD | 0 | 1 | 2 | 3 |
822 *
823 * At time=1 (period elapsed)
824 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
825 * BD | 1 | 2 | 3 | 0 |
826 *
827 * At time=2 (second period elapsed)
828 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
829 * BD | 2 | 3 | 0 | 1 |
830 *
831 * The bd_head field points to the index of the BD to be read. It's also the
832 * position to be filled at next. The pcm_head and the pcm_filled fields
833 * point to the indices of the current position and of the next position to
834 * be filled, respectively. For PCM buffer there are both _head and _filled
835 * because they may be difference when nperiods > 4. For example, in the
836 * example above at t=1, bd_head=1 and pcm_head=1 while pcm_filled=5:
837 *
838 * pcm_head (=1) --v v-- pcm_filled (=5)
839 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
840 * BD | 1 | 2 | 3 | 0 |
841 * bd_head (=1) --^ ^-- next to fill (= bd_head)
842 *
843 * For nperiods < 4, the remaining BDs out of 4 are marked as invalid, so that
844 * the hardware skips those BDs in the loop.
Takashi Iwai8d48c012017-02-07 08:05:46 +0100845 *
846 * An exceptional setup is the case with nperiods=1. Since we have to update
847 * BDs after finishing one BD processing, we'd need at least two BDs, where
848 * both BDs point to the same content, the same address, the same size of the
849 * whole PCM buffer.
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100850 */
851
852#define AUD_BUF_ADDR(x) (AUD_BUF_A_ADDR + (x) * HAD_REG_WIDTH)
853#define AUD_BUF_LEN(x) (AUD_BUF_A_LENGTH + (x) * HAD_REG_WIDTH)
854
855/* Set up a buffer descriptor at the "filled" position */
856static void had_prog_bd(struct snd_pcm_substream *substream,
857 struct snd_intelhad *intelhaddata)
858{
859 int idx = intelhaddata->bd_head;
860 int ofs = intelhaddata->pcmbuf_filled * intelhaddata->period_bytes;
861 u32 addr = substream->runtime->dma_addr + ofs;
862
Takashi Iwaie8de9852017-02-07 08:09:12 +0100863 addr |= AUD_BUF_VALID;
864 if (!substream->runtime->no_period_wakeup)
865 addr |= AUD_BUF_INTR_EN;
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100866 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), addr);
867 had_write_register(intelhaddata, AUD_BUF_LEN(idx),
868 intelhaddata->period_bytes);
869
870 /* advance the indices to the next */
871 intelhaddata->bd_head++;
872 intelhaddata->bd_head %= intelhaddata->num_bds;
873 intelhaddata->pcmbuf_filled++;
874 intelhaddata->pcmbuf_filled %= substream->runtime->periods;
875}
876
877/* invalidate a buffer descriptor with the given index */
878static void had_invalidate_bd(struct snd_intelhad *intelhaddata,
879 int idx)
880{
881 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), 0);
882 had_write_register(intelhaddata, AUD_BUF_LEN(idx), 0);
883}
884
885/* Initial programming of ring buffer */
886static void had_init_ringbuf(struct snd_pcm_substream *substream,
887 struct snd_intelhad *intelhaddata)
888{
889 struct snd_pcm_runtime *runtime = substream->runtime;
890 int i, num_periods;
891
892 num_periods = runtime->periods;
893 intelhaddata->num_bds = min(num_periods, HAD_NUM_OF_RING_BUFS);
Takashi Iwai8d48c012017-02-07 08:05:46 +0100894 /* set the minimum 2 BDs for num_periods=1 */
895 intelhaddata->num_bds = max(intelhaddata->num_bds, 2U);
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100896 intelhaddata->period_bytes =
897 frames_to_bytes(runtime, runtime->period_size);
898 WARN_ON(intelhaddata->period_bytes & 0x3f);
899
900 intelhaddata->bd_head = 0;
901 intelhaddata->pcmbuf_head = 0;
902 intelhaddata->pcmbuf_filled = 0;
903
904 for (i = 0; i < HAD_NUM_OF_RING_BUFS; i++) {
Takashi Iwai8d48c012017-02-07 08:05:46 +0100905 if (i < intelhaddata->num_bds)
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100906 had_prog_bd(substream, intelhaddata);
907 else /* invalidate the rest */
908 had_invalidate_bd(intelhaddata, i);
909 }
910
911 intelhaddata->bd_head = 0; /* reset at head again before starting */
912}
913
914/* process a bd, advance to the next */
915static void had_advance_ringbuf(struct snd_pcm_substream *substream,
916 struct snd_intelhad *intelhaddata)
917{
918 int num_periods = substream->runtime->periods;
919
920 /* reprogram the next buffer */
921 had_prog_bd(substream, intelhaddata);
922
923 /* proceed to next */
924 intelhaddata->pcmbuf_head++;
925 intelhaddata->pcmbuf_head %= num_periods;
926}
927
928/* process the current BD(s);
929 * returns the current PCM buffer byte position, or -EPIPE for underrun.
930 */
931static int had_process_ringbuf(struct snd_pcm_substream *substream,
932 struct snd_intelhad *intelhaddata)
933{
934 int len, processed;
935 unsigned long flags;
936
937 processed = 0;
938 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
939 for (;;) {
940 /* get the remaining bytes on the buffer */
941 had_read_register(intelhaddata,
942 AUD_BUF_LEN(intelhaddata->bd_head),
943 &len);
944 if (len < 0 || len > intelhaddata->period_bytes) {
945 dev_dbg(intelhaddata->dev, "Invalid buf length %d\n",
946 len);
947 len = -EPIPE;
948 goto out;
949 }
950
951 if (len > 0) /* OK, this is the current buffer */
952 break;
953
954 /* len=0 => already empty, check the next buffer */
955 if (++processed >= intelhaddata->num_bds) {
956 len = -EPIPE; /* all empty? - report underrun */
957 goto out;
958 }
959 had_advance_ringbuf(substream, intelhaddata);
960 }
961
962 len = intelhaddata->period_bytes - len;
963 len += intelhaddata->period_bytes * intelhaddata->pcmbuf_head;
964 out:
965 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
966 return len;
967}
968
969/* called from irq handler */
970static void had_process_buffer_done(struct snd_intelhad *intelhaddata)
971{
972 struct snd_pcm_substream *substream;
973
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100974 substream = had_substream_get(intelhaddata);
975 if (!substream)
976 return; /* no stream? - bail out */
977
Takashi Iwaibe9a2e92017-02-15 22:05:34 +0100978 if (!intelhaddata->connected) {
979 snd_pcm_stop_xrun(substream);
980 goto out; /* disconnected? - bail out */
981 }
982
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100983 /* process or stop the stream */
984 if (had_process_ringbuf(substream, intelhaddata) < 0)
985 snd_pcm_stop_xrun(substream);
986 else
987 snd_pcm_period_elapsed(substream);
988
Takashi Iwaibe9a2e92017-02-15 22:05:34 +0100989 out:
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100990 had_substream_put(intelhaddata);
991}
992
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100993/*
994 * The interrupt status 'sticky' bits might not be cleared by
995 * setting '1' to that bit once...
996 */
997static void wait_clear_underrun_bit(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530998{
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100999 int i;
1000 u32 val;
1001
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001002 for (i = 0; i < 100; i++) {
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001003 /* clear bit30, 31 AUD_HDMI_STATUS */
1004 had_read_register(intelhaddata, AUD_HDMI_STATUS, &val);
Takashi Iwai77531be2017-02-07 12:17:23 +01001005 if (!(val & AUD_HDMI_STATUS_MASK_UNDERRUN))
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001006 return;
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001007 udelay(100);
1008 cond_resched();
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001009 had_write_register(intelhaddata, AUD_HDMI_STATUS, val);
1010 }
1011 dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n");
1012}
1013
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001014/* Perform some reset procedure but only when need_reset is set;
1015 * this is called from prepare or hw_free callbacks once after trigger STOP
1016 * or underrun has been processed in order to settle down the h/w state.
1017 */
1018static void had_do_reset(struct snd_intelhad *intelhaddata)
1019{
Takashi Iwai28ed1252017-02-15 22:02:10 +01001020 if (!intelhaddata->need_reset || !intelhaddata->connected)
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001021 return;
1022
1023 /* Reset buffer pointers */
1024 had_reset_audio(intelhaddata);
1025 wait_clear_underrun_bit(intelhaddata);
1026 intelhaddata->need_reset = false;
1027}
1028
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001029/* called from irq handler */
1030static void had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
1031{
1032 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301033
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001034 /* Report UNDERRUN error to above layers */
1035 substream = had_substream_get(intelhaddata);
1036 if (substream) {
1037 snd_pcm_stop_xrun(substream);
1038 had_substream_put(intelhaddata);
1039 }
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001040 intelhaddata->need_reset = true;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301041}
1042
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001043/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001044 * ALSA PCM open callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301045 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001046static int had_pcm_open(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301047{
1048 struct snd_intelhad *intelhaddata;
1049 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301050 int retval;
1051
Jerome Anand5dab11d2017-01-25 04:27:52 +05301052 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301053 runtime = substream->runtime;
1054
Takashi Iwai182cdf22017-02-02 14:43:39 +01001055 pm_runtime_get_sync(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301056
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001057 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001058 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
1059 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301060 retval = -ENODEV;
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001061 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301062 }
1063
1064 /* set the runtime hw parameter with local snd_pcm_hardware struct */
Takashi Iwaib5562902017-02-04 22:05:33 +01001065 runtime->hw = had_pcm_hardware;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301066
Jerome Anand5dab11d2017-01-25 04:27:52 +05301067 retval = snd_pcm_hw_constraint_integer(runtime,
1068 SNDRV_PCM_HW_PARAM_PERIODS);
1069 if (retval < 0)
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001070 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301071
1072 /* Make sure, that the period size is always aligned
1073 * 64byte boundary
1074 */
1075 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1076 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
Takashi Iwai73997b02017-02-02 17:38:50 +01001077 if (retval < 0)
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001078 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301079
Takashi Iwai85bd8742017-02-07 13:33:17 +01001080 retval = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1081 if (retval < 0)
1082 goto error;
1083
Takashi Iwai73997b02017-02-02 17:38:50 +01001084 /* expose PCM substream */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001085 spin_lock_irq(&intelhaddata->had_spinlock);
1086 intelhaddata->stream_info.substream = substream;
1087 intelhaddata->stream_info.substream_refcount++;
1088 spin_unlock_irq(&intelhaddata->had_spinlock);
1089
Jerome Anand5dab11d2017-01-25 04:27:52 +05301090 return retval;
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001091 error:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301092 pm_runtime_put(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301093 return retval;
1094}
1095
Takashi Iwaidf76df12017-01-31 16:04:10 +01001096/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001097 * ALSA PCM close callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301098 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001099static int had_pcm_close(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301100{
1101 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301102
Jerome Anand5dab11d2017-01-25 04:27:52 +05301103 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301104
Takashi Iwai73997b02017-02-02 17:38:50 +01001105 /* unreference and sync with the pending PCM accesses */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001106 spin_lock_irq(&intelhaddata->had_spinlock);
1107 intelhaddata->stream_info.substream = NULL;
1108 intelhaddata->stream_info.substream_refcount--;
1109 while (intelhaddata->stream_info.substream_refcount > 0) {
1110 spin_unlock_irq(&intelhaddata->had_spinlock);
1111 cpu_relax();
1112 spin_lock_irq(&intelhaddata->had_spinlock);
1113 }
1114 spin_unlock_irq(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301115
Jerome Anand5dab11d2017-01-25 04:27:52 +05301116 pm_runtime_put(intelhaddata->dev);
1117 return 0;
1118}
1119
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001120/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001121 * ALSA PCM hw_params callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301122 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001123static int had_pcm_hw_params(struct snd_pcm_substream *substream,
1124 struct snd_pcm_hw_params *hw_params)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301125{
Takashi Iwaic75b0472017-01-31 15:49:15 +01001126 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301127 unsigned long addr;
1128 int pages, buf_size, retval;
1129
Takashi Iwaic75b0472017-01-31 15:49:15 +01001130 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301131 buf_size = params_buffer_bytes(hw_params);
1132 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1133 if (retval < 0)
1134 return retval;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001135 dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
1136 __func__, buf_size);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301137 /* mark the pages as uncached region */
1138 addr = (unsigned long) substream->runtime->dma_area;
1139 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1140 retval = set_memory_uc(addr, pages);
1141 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001142 dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
1143 retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301144 return retval;
1145 }
1146 memset(substream->runtime->dma_area, 0, buf_size);
1147
1148 return retval;
1149}
1150
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001151/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001152 * ALSA PCM hw_free callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301153 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001154static int had_pcm_hw_free(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301155{
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001156 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301157 unsigned long addr;
1158 u32 pages;
1159
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001160 intelhaddata = snd_pcm_substream_chip(substream);
1161 had_do_reset(intelhaddata);
1162
Jerome Anand5dab11d2017-01-25 04:27:52 +05301163 /* mark back the pages as cached/writeback region before the free */
1164 if (substream->runtime->dma_area != NULL) {
1165 addr = (unsigned long) substream->runtime->dma_area;
1166 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1167 PAGE_SIZE;
1168 set_memory_wb(addr, pages);
1169 return snd_pcm_lib_free_pages(substream);
1170 }
1171 return 0;
1172}
1173
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001174/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001175 * ALSA PCM trigger callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301176 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001177static int had_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301178{
Takashi Iwaida864802017-01-31 13:52:22 +01001179 int retval = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301180 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301181
Jerome Anand5dab11d2017-01-25 04:27:52 +05301182 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301183
Takashi Iwaidf42cb42017-02-12 11:35:44 +01001184 spin_lock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301185 switch (cmd) {
1186 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001187 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1188 case SNDRV_PCM_TRIGGER_RESUME:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301189 /* Disable local INTRs till register prgmng is done */
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001190 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001191 dev_dbg(intelhaddata->dev,
1192 "_START: HDMI cable plugged-out\n");
Jerome Anand5dab11d2017-01-25 04:27:52 +05301193 retval = -ENODEV;
1194 break;
1195 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301196
Jerome Anand5dab11d2017-01-25 04:27:52 +05301197 /* Enable Audio */
Takashi Iwai075a1d42017-02-07 07:55:27 +01001198 had_ack_irqs(intelhaddata); /* FIXME: do we need this? */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001199 had_enable_audio(intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301200 break;
1201
1202 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001203 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301204 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001205 had_enable_audio(intelhaddata, false);
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001206 intelhaddata->need_reset = true;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301207 break;
1208
1209 default:
1210 retval = -EINVAL;
1211 }
Takashi Iwaidf42cb42017-02-12 11:35:44 +01001212 spin_unlock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301213 return retval;
1214}
1215
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001216/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001217 * ALSA PCM prepare callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301218 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001219static int had_pcm_prepare(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301220{
1221 int retval;
1222 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001223 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301224 struct snd_intelhad *intelhaddata;
1225 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301226
Jerome Anand5dab11d2017-01-25 04:27:52 +05301227 intelhaddata = snd_pcm_substream_chip(substream);
1228 runtime = substream->runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301229
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001230 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001231 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
1232 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301233 retval = -ENODEV;
1234 goto prep_end;
1235 }
1236
Takashi Iwaic75b0472017-01-31 15:49:15 +01001237 dev_dbg(intelhaddata->dev, "period_size=%d\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301238 (int)frames_to_bytes(runtime, runtime->period_size));
Takashi Iwaic75b0472017-01-31 15:49:15 +01001239 dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods);
1240 dev_dbg(intelhaddata->dev, "buffer_size=%d\n",
1241 (int)snd_pcm_lib_buffer_bytes(substream));
1242 dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
1243 dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301244
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001245 had_do_reset(intelhaddata);
1246
Jerome Anand5dab11d2017-01-25 04:27:52 +05301247 /* Get N value in KHz */
Takashi Iwaida864802017-01-31 13:52:22 +01001248 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301249
Takashi Iwaib5562902017-02-04 22:05:33 +01001250 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301251 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001252 dev_err(intelhaddata->dev,
1253 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301254 goto prep_end;
1255 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001256
1257 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001258 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001259
Takashi Iwaib5562902017-02-04 22:05:33 +01001260 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1261 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301262
Takashi Iwaib5562902017-02-04 22:05:33 +01001263 had_prog_dip(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301264
Takashi Iwaib5562902017-02-04 22:05:33 +01001265 retval = had_init_audio_ctrl(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301266
1267 /* Prog buffer address */
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001268 had_init_ringbuf(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301269
1270 /*
1271 * Program channel mapping in following order:
1272 * FL, FR, C, LFE, RL, RR
1273 */
1274
Takashi Iwai79dda752017-01-30 17:23:39 +01001275 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301276
1277prep_end:
1278 return retval;
1279}
1280
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001281/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001282 * ALSA PCM pointer callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301283 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001284static snd_pcm_uframes_t had_pcm_pointer(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301285{
1286 struct snd_intelhad *intelhaddata;
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001287 int len;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301288
Jerome Anand5dab11d2017-01-25 04:27:52 +05301289 intelhaddata = snd_pcm_substream_chip(substream);
1290
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001291 if (!intelhaddata->connected)
Takashi Iwai79f439e2017-01-31 16:46:44 +01001292 return SNDRV_PCM_POS_XRUN;
1293
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001294 len = had_process_ringbuf(substream, intelhaddata);
1295 if (len < 0)
1296 return SNDRV_PCM_POS_XRUN;
Takashi Iwai8d48c012017-02-07 08:05:46 +01001297 len = bytes_to_frames(substream->runtime, len);
1298 /* wrapping may happen when periods=1 */
1299 len %= substream->runtime->buffer_size;
1300 return len;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301301}
1302
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001303/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001304 * ALSA PCM mmap callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301305 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001306static int had_pcm_mmap(struct snd_pcm_substream *substream,
1307 struct vm_area_struct *vma)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301308{
Jerome Anand5dab11d2017-01-25 04:27:52 +05301309 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1310 return remap_pfn_range(vma, vma->vm_start,
1311 substream->dma_buffer.addr >> PAGE_SHIFT,
1312 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1313}
1314
Takashi Iwai73997b02017-02-02 17:38:50 +01001315/*
1316 * ALSA PCM ops
1317 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001318static const struct snd_pcm_ops had_pcm_ops = {
1319 .open = had_pcm_open,
1320 .close = had_pcm_close,
Takashi Iwai73997b02017-02-02 17:38:50 +01001321 .ioctl = snd_pcm_lib_ioctl,
Takashi Iwaib5562902017-02-04 22:05:33 +01001322 .hw_params = had_pcm_hw_params,
1323 .hw_free = had_pcm_hw_free,
1324 .prepare = had_pcm_prepare,
1325 .trigger = had_pcm_trigger,
1326 .pointer = had_pcm_pointer,
1327 .mmap = had_pcm_mmap,
Takashi Iwai73997b02017-02-02 17:38:50 +01001328};
1329
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001330/* process mode change of the running stream; called in mutex */
Takashi Iwaib5562902017-02-04 22:05:33 +01001331static int had_process_mode_change(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301332{
Takashi Iwaida864802017-01-31 13:52:22 +01001333 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301334 int retval = 0;
1335 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001336 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301337
Takashi Iwai313d9f22017-02-02 13:00:12 +01001338 substream = had_substream_get(intelhaddata);
1339 if (!substream)
Takashi Iwaida864802017-01-31 13:52:22 +01001340 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301341
1342 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001343 had_enable_audio(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301344
1345 /* Update CTS value */
Takashi Iwaida864802017-01-31 13:52:22 +01001346 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301347
Takashi Iwaib5562902017-02-04 22:05:33 +01001348 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301349 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001350 dev_err(intelhaddata->dev,
1351 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301352 goto out;
1353 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001354
1355 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001356 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001357
Takashi Iwaib5562902017-02-04 22:05:33 +01001358 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1359 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301360
1361 /* Enable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001362 had_enable_audio(intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301363
1364out:
Takashi Iwai313d9f22017-02-02 13:00:12 +01001365 had_substream_put(intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301366 return retval;
1367}
1368
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001369/* process hot plug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001370static void had_process_hot_plug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001371{
Takashi Iwai372d8552017-01-31 13:57:58 +01001372 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001373
Takashi Iwaibcce7752017-02-01 17:18:20 +01001374 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001375 if (intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001376 dev_dbg(intelhaddata->dev, "Device already connected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001377 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001378 return;
Takashi Iwai372d8552017-01-31 13:57:58 +01001379 }
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001380
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001381 intelhaddata->connected = true;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001382 dev_dbg(intelhaddata->dev,
1383 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001384 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001385 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001386
Takashi Iwai372d8552017-01-31 13:57:58 +01001387 /* Safety check */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001388 substream = had_substream_get(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001389 if (substream) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001390 dev_dbg(intelhaddata->dev,
1391 "Force to stop the active stream by disconnection\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001392 /* Set runtime->state to hw_params done */
Takashi Iwai5def9012017-02-15 21:36:38 +01001393 snd_pcm_stop_xrun(substream);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001394 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001395 }
1396
1397 had_build_channel_allocation_map(intelhaddata);
Takashi Iwaib9bacf22017-02-14 12:29:38 +01001398
1399 snd_jack_report(intelhaddata->jack, SND_JACK_AVOUT);
Takashi Iwai372d8552017-01-31 13:57:58 +01001400}
1401
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001402/* process hot unplug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001403static void had_process_hot_unplug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001404{
Takashi Iwai313d9f22017-02-02 13:00:12 +01001405 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001406
Takashi Iwai313d9f22017-02-02 13:00:12 +01001407 substream = had_substream_get(intelhaddata);
1408
Takashi Iwaibcce7752017-02-01 17:18:20 +01001409 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001410
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001411 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001412 dev_dbg(intelhaddata->dev, "Device already disconnected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001413 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001414 goto out;
Takashi Iwai372d8552017-01-31 13:57:58 +01001415
Takashi Iwai372d8552017-01-31 13:57:58 +01001416 }
1417
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001418 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001419 had_enable_audio(intelhaddata, false);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001420
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001421 intelhaddata->connected = false;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001422 dev_dbg(intelhaddata->dev,
1423 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001424 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001425 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001426
1427 /* Report to above ALSA layer */
1428 if (substream)
Takashi Iwai5def9012017-02-15 21:36:38 +01001429 snd_pcm_stop_xrun(substream);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001430
1431 out:
Takashi Iwaib9bacf22017-02-14 12:29:38 +01001432 snd_jack_report(intelhaddata->jack, 0);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001433 if (substream)
1434 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001435 kfree(intelhaddata->chmap->chmap);
1436 intelhaddata->chmap->chmap = NULL;
Takashi Iwai372d8552017-01-31 13:57:58 +01001437}
1438
Takashi Iwai73997b02017-02-02 17:38:50 +01001439/*
1440 * ALSA iec958 and ELD controls
1441 */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301442
Jerome Anand5dab11d2017-01-25 04:27:52 +05301443static int had_iec958_info(struct snd_kcontrol *kcontrol,
1444 struct snd_ctl_elem_info *uinfo)
1445{
1446 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1447 uinfo->count = 1;
1448 return 0;
1449}
1450
1451static int had_iec958_get(struct snd_kcontrol *kcontrol,
1452 struct snd_ctl_elem_value *ucontrol)
1453{
1454 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1455
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001456 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301457 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1458 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1459 ucontrol->value.iec958.status[2] =
1460 (intelhaddata->aes_bits >> 16) & 0xff;
1461 ucontrol->value.iec958.status[3] =
1462 (intelhaddata->aes_bits >> 24) & 0xff;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001463 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301464 return 0;
1465}
Takashi Iwai372d8552017-01-31 13:57:58 +01001466
Jerome Anand5dab11d2017-01-25 04:27:52 +05301467static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1468 struct snd_ctl_elem_value *ucontrol)
1469{
1470 ucontrol->value.iec958.status[0] = 0xff;
1471 ucontrol->value.iec958.status[1] = 0xff;
1472 ucontrol->value.iec958.status[2] = 0xff;
1473 ucontrol->value.iec958.status[3] = 0xff;
1474 return 0;
1475}
Takashi Iwai372d8552017-01-31 13:57:58 +01001476
Jerome Anand5dab11d2017-01-25 04:27:52 +05301477static int had_iec958_put(struct snd_kcontrol *kcontrol,
1478 struct snd_ctl_elem_value *ucontrol)
1479{
1480 unsigned int val;
1481 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001482 int changed = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301483
Jerome Anand5dab11d2017-01-25 04:27:52 +05301484 val = (ucontrol->value.iec958.status[0] << 0) |
1485 (ucontrol->value.iec958.status[1] << 8) |
1486 (ucontrol->value.iec958.status[2] << 16) |
1487 (ucontrol->value.iec958.status[3] << 24);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001488 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301489 if (intelhaddata->aes_bits != val) {
1490 intelhaddata->aes_bits = val;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001491 changed = 1;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301492 }
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001493 mutex_unlock(&intelhaddata->mutex);
1494 return changed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301495}
1496
Takashi Iwai4aedb942017-02-02 16:38:39 +01001497static int had_ctl_eld_info(struct snd_kcontrol *kcontrol,
1498 struct snd_ctl_elem_info *uinfo)
1499{
1500 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1501 uinfo->count = HDMI_MAX_ELD_BYTES;
1502 return 0;
1503}
1504
1505static int had_ctl_eld_get(struct snd_kcontrol *kcontrol,
1506 struct snd_ctl_elem_value *ucontrol)
1507{
1508 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1509
1510 mutex_lock(&intelhaddata->mutex);
1511 memcpy(ucontrol->value.bytes.data, intelhaddata->eld,
1512 HDMI_MAX_ELD_BYTES);
1513 mutex_unlock(&intelhaddata->mutex);
1514 return 0;
1515}
1516
Takashi Iwai73997b02017-02-02 17:38:50 +01001517static const struct snd_kcontrol_new had_controls[] = {
Takashi Iwai4aedb942017-02-02 16:38:39 +01001518 {
1519 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1520 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1521 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1522 .info = had_iec958_info, /* shared */
1523 .get = had_iec958_mask_get,
1524 },
1525 {
1526 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1527 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1528 .info = had_iec958_info,
1529 .get = had_iec958_get,
1530 .put = had_iec958_put,
1531 },
1532 {
1533 .access = (SNDRV_CTL_ELEM_ACCESS_READ |
1534 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1535 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1536 .name = "ELD",
1537 .info = had_ctl_eld_info,
1538 .get = had_ctl_eld_get,
1539 },
Jerome Anand5dab11d2017-01-25 04:27:52 +05301540};
1541
Takashi Iwai73997b02017-02-02 17:38:50 +01001542/*
1543 * audio interrupt handler
1544 */
Takashi Iwaida864802017-01-31 13:52:22 +01001545static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id)
1546{
1547 struct snd_intelhad *ctx = dev_id;
Takashi Iwai28ed1252017-02-15 22:02:10 +01001548 u32 audio_stat;
Takashi Iwaida864802017-01-31 13:52:22 +01001549
Takashi Iwai28ed1252017-02-15 22:02:10 +01001550 /* use raw register access to ack IRQs even while disconnected */
1551 audio_stat = had_read_register_raw(ctx, AUD_HDMI_STATUS);
Takashi Iwaida864802017-01-31 13:52:22 +01001552
1553 if (audio_stat & HDMI_AUDIO_UNDERRUN) {
Takashi Iwai28ed1252017-02-15 22:02:10 +01001554 had_write_register_raw(ctx, AUD_HDMI_STATUS,
1555 HDMI_AUDIO_UNDERRUN);
Takashi Iwaida864802017-01-31 13:52:22 +01001556 had_process_buffer_underrun(ctx);
1557 }
1558
1559 if (audio_stat & HDMI_AUDIO_BUFFER_DONE) {
Takashi Iwai28ed1252017-02-15 22:02:10 +01001560 had_write_register_raw(ctx, AUD_HDMI_STATUS,
1561 HDMI_AUDIO_BUFFER_DONE);
Takashi Iwaida864802017-01-31 13:52:22 +01001562 had_process_buffer_done(ctx);
1563 }
1564
1565 return IRQ_HANDLED;
1566}
1567
Takashi Iwai73997b02017-02-02 17:38:50 +01001568/*
1569 * monitor plug/unplug notification from i915; just kick off the work
1570 */
Takashi Iwaida864802017-01-31 13:52:22 +01001571static void notify_audio_lpe(struct platform_device *pdev)
1572{
1573 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Takashi Iwaida864802017-01-31 13:52:22 +01001574
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001575 schedule_work(&ctx->hdmi_audio_wq);
1576}
Takashi Iwaida864802017-01-31 13:52:22 +01001577
Takashi Iwai73997b02017-02-02 17:38:50 +01001578/* the work to handle monitor hot plug/unplug */
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001579static void had_audio_wq(struct work_struct *work)
1580{
1581 struct snd_intelhad *ctx =
1582 container_of(work, struct snd_intelhad, hdmi_audio_wq);
1583 struct intel_hdmi_lpe_audio_pdata *pdata = ctx->dev->platform_data;
1584
Takashi Iwai182cdf22017-02-02 14:43:39 +01001585 pm_runtime_get_sync(ctx->dev);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001586 mutex_lock(&ctx->mutex);
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001587 if (!pdata->hdmi_connected) {
1588 dev_dbg(ctx->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001589 __func__);
Takashi Iwai4aedb942017-02-02 16:38:39 +01001590 memset(ctx->eld, 0, sizeof(ctx->eld)); /* clear the old ELD */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001591 had_process_hot_unplug(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001592 } else {
1593 struct intel_hdmi_lpe_audio_eld *eld = &pdata->eld;
1594
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001595 dev_dbg(ctx->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
1596 __func__, eld->port_id, pdata->tmds_clock_speed);
1597
Takashi Iwaida864802017-01-31 13:52:22 +01001598 switch (eld->pipe_id) {
1599 case 0:
1600 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1601 break;
1602 case 1:
1603 ctx->had_config_offset = AUDIO_HDMI_CONFIG_B;
1604 break;
1605 case 2:
1606 ctx->had_config_offset = AUDIO_HDMI_CONFIG_C;
1607 break;
1608 default:
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001609 dev_dbg(ctx->dev, "Invalid pipe %d\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001610 eld->pipe_id);
1611 break;
1612 }
1613
Takashi Iwaidf0435d2017-02-02 15:37:11 +01001614 memcpy(ctx->eld, eld->eld_data, sizeof(ctx->eld));
Takashi Iwaida864802017-01-31 13:52:22 +01001615
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001616 ctx->dp_output = pdata->dp_output;
1617 ctx->tmds_clock_speed = pdata->tmds_clock_speed;
1618 ctx->link_rate = pdata->link_rate;
1619
Takashi Iwaida864802017-01-31 13:52:22 +01001620 had_process_hot_plug(ctx);
1621
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001622 /* Process mode change if stream is active */
Takashi Iwaib5562902017-02-04 22:05:33 +01001623 had_process_mode_change(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001624 }
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001625 mutex_unlock(&ctx->mutex);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001626 pm_runtime_put(ctx->dev);
1627}
1628
1629/*
Takashi Iwaib9bacf22017-02-14 12:29:38 +01001630 * Jack interface
1631 */
1632static int had_create_jack(struct snd_intelhad *ctx)
1633{
1634 int err;
1635
1636 err = snd_jack_new(ctx->card, "HDMI/DP", SND_JACK_AVOUT, &ctx->jack,
1637 true, false);
1638 if (err < 0)
1639 return err;
1640 ctx->jack->private_data = ctx;
1641 return 0;
1642}
1643
1644/*
Takashi Iwai182cdf22017-02-02 14:43:39 +01001645 * PM callbacks
1646 */
1647
1648static int hdmi_lpe_audio_runtime_suspend(struct device *dev)
1649{
1650 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1651 struct snd_pcm_substream *substream;
1652
1653 substream = had_substream_get(ctx);
1654 if (substream) {
1655 snd_pcm_suspend(substream);
1656 had_substream_put(ctx);
1657 }
1658
1659 return 0;
1660}
1661
Arnd Bergmann1df98922017-02-07 14:38:51 +01001662static int __maybe_unused hdmi_lpe_audio_suspend(struct device *dev)
Takashi Iwai182cdf22017-02-02 14:43:39 +01001663{
1664 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1665 int err;
1666
1667 err = hdmi_lpe_audio_runtime_suspend(dev);
1668 if (!err)
1669 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D3hot);
1670 return err;
1671}
1672
Arnd Bergmann1df98922017-02-07 14:38:51 +01001673static int __maybe_unused hdmi_lpe_audio_resume(struct device *dev)
Takashi Iwai182cdf22017-02-02 14:43:39 +01001674{
1675 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1676
1677 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D0);
1678 return 0;
Takashi Iwaida864802017-01-31 13:52:22 +01001679}
1680
1681/* release resources */
1682static void hdmi_lpe_audio_free(struct snd_card *card)
1683{
1684 struct snd_intelhad *ctx = card->private_data;
1685
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001686 cancel_work_sync(&ctx->hdmi_audio_wq);
1687
Takashi Iwaida864802017-01-31 13:52:22 +01001688 if (ctx->mmio_start)
1689 iounmap(ctx->mmio_start);
1690 if (ctx->irq >= 0)
1691 free_irq(ctx->irq, ctx);
1692}
1693
1694/*
1695 * hdmi_lpe_audio_probe - start bridge with i915
1696 *
1697 * This function is called when the i915 driver creates the
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001698 * hdmi-lpe-audio platform device.
Takashi Iwaida864802017-01-31 13:52:22 +01001699 */
1700static int hdmi_lpe_audio_probe(struct platform_device *pdev)
1701{
1702 struct snd_card *card;
1703 struct snd_intelhad *ctx;
1704 struct snd_pcm *pcm;
1705 struct intel_hdmi_lpe_audio_pdata *pdata;
1706 int irq;
1707 struct resource *res_mmio;
Takashi Iwai4aedb942017-02-02 16:38:39 +01001708 int i, ret;
Takashi Iwaida864802017-01-31 13:52:22 +01001709
Takashi Iwaida864802017-01-31 13:52:22 +01001710 pdata = pdev->dev.platform_data;
1711 if (!pdata) {
1712 dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
1713 return -EINVAL;
1714 }
1715
1716 /* get resources */
1717 irq = platform_get_irq(pdev, 0);
1718 if (irq < 0) {
1719 dev_err(&pdev->dev, "Could not get irq resource\n");
1720 return -ENODEV;
1721 }
1722
1723 res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1724 if (!res_mmio) {
1725 dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
1726 return -ENXIO;
1727 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301728
Takashi Iwai5647aec2017-01-31 08:14:34 +01001729 /* create a card instance with ALSA framework */
Takashi Iwaida864802017-01-31 13:52:22 +01001730 ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
1731 THIS_MODULE, sizeof(*ctx), &card);
1732 if (ret)
1733 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301734
Takashi Iwaida864802017-01-31 13:52:22 +01001735 ctx = card->private_data;
1736 spin_lock_init(&ctx->had_spinlock);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001737 mutex_init(&ctx->mutex);
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001738 ctx->connected = false;
Takashi Iwaida864802017-01-31 13:52:22 +01001739 ctx->dev = &pdev->dev;
1740 ctx->card = card;
Takashi Iwaida864802017-01-31 13:52:22 +01001741 ctx->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
1742 strcpy(card->driver, INTEL_HAD);
Takashi Iwai873ab032017-02-07 12:14:04 +01001743 strcpy(card->shortname, "Intel HDMI/DP LPE Audio");
1744 strcpy(card->longname, "Intel HDMI/DP LPE Audio");
Jerome Anand5dab11d2017-01-25 04:27:52 +05301745
Takashi Iwaida864802017-01-31 13:52:22 +01001746 ctx->irq = -1;
1747 ctx->tmds_clock_speed = DIS_SAMPLE_RATE_148_5;
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001748 INIT_WORK(&ctx->hdmi_audio_wq, had_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301749
Takashi Iwaida864802017-01-31 13:52:22 +01001750 card->private_free = hdmi_lpe_audio_free;
1751
1752 /* assume pipe A as default */
1753 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1754
1755 platform_set_drvdata(pdev, ctx);
1756
1757 dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
1758 __func__, (unsigned int)res_mmio->start,
1759 (unsigned int)res_mmio->end);
1760
1761 ctx->mmio_start = ioremap_nocache(res_mmio->start,
1762 (size_t)(resource_size(res_mmio)));
1763 if (!ctx->mmio_start) {
1764 dev_err(&pdev->dev, "Could not get ioremap\n");
1765 ret = -EACCES;
1766 goto err;
1767 }
1768
1769 /* setup interrupt handler */
1770 ret = request_irq(irq, display_pipe_interrupt_handler, 0,
1771 pdev->name, ctx);
1772 if (ret < 0) {
1773 dev_err(&pdev->dev, "request_irq failed\n");
1774 goto err;
1775 }
1776
1777 ctx->irq = irq;
1778
1779 ret = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
1780 MAX_CAP_STREAMS, &pcm);
1781 if (ret)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301782 goto err;
1783
1784 /* setup private data which can be retrieved when required */
Takashi Iwaida864802017-01-31 13:52:22 +01001785 pcm->private_data = ctx;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301786 pcm->info_flags = 0;
1787 strncpy(pcm->name, card->shortname, strlen(card->shortname));
Takashi Iwaida864802017-01-31 13:52:22 +01001788 /* setup the ops for playabck */
Takashi Iwaib5562902017-02-04 22:05:33 +01001789 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &had_pcm_ops);
Takashi Iwai412bbe7d52017-02-02 22:03:22 +01001790
1791 /* only 32bit addressable */
1792 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
1793 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1794
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001795 /* allocate dma pages;
1796 * try to allocate 600k buffer as default which is large enough
Jerome Anand5dab11d2017-01-25 04:27:52 +05301797 */
Takashi Iwaida864802017-01-31 13:52:22 +01001798 snd_pcm_lib_preallocate_pages_for_all(pcm,
Jerome Anand5dab11d2017-01-25 04:27:52 +05301799 SNDRV_DMA_TYPE_DEV, NULL,
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001800 HAD_DEFAULT_BUFFER, HAD_MAX_BUFFER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301801
Takashi Iwai4aedb942017-02-02 16:38:39 +01001802 /* create controls */
1803 for (i = 0; i < ARRAY_SIZE(had_controls); i++) {
1804 ret = snd_ctl_add(card, snd_ctl_new1(&had_controls[i], ctx));
1805 if (ret < 0)
1806 goto err;
1807 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301808
1809 init_channel_allocations();
1810
1811 /* Register channel map controls */
Takashi Iwaida864802017-01-31 13:52:22 +01001812 ret = had_register_chmap_ctls(ctx, pcm);
1813 if (ret < 0)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301814 goto err;
1815
Takashi Iwaib9bacf22017-02-14 12:29:38 +01001816 ret = had_create_jack(ctx);
1817 if (ret < 0)
1818 goto err;
1819
Takashi Iwaida864802017-01-31 13:52:22 +01001820 ret = snd_card_register(card);
1821 if (ret)
Takashi Iwai36ec0d92017-01-31 08:47:05 +01001822 goto err;
1823
Takashi Iwaibcce7752017-02-01 17:18:20 +01001824 spin_lock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001825 pdata->notify_audio_lpe = notify_audio_lpe;
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001826 pdata->notify_pending = false;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001827 spin_unlock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001828
1829 pm_runtime_set_active(&pdev->dev);
1830 pm_runtime_enable(&pdev->dev);
1831
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001832 dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
Takashi Iwaida864802017-01-31 13:52:22 +01001833 schedule_work(&ctx->hdmi_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301834
Takashi Iwai79dda752017-01-30 17:23:39 +01001835 return 0;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001836
Jerome Anand5dab11d2017-01-25 04:27:52 +05301837err:
1838 snd_card_free(card);
Takashi Iwaida864802017-01-31 13:52:22 +01001839 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301840}
1841
Takashi Iwai79dda752017-01-30 17:23:39 +01001842/*
Takashi Iwaida864802017-01-31 13:52:22 +01001843 * hdmi_lpe_audio_remove - stop bridge with i915
Jerome Anand5dab11d2017-01-25 04:27:52 +05301844 *
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001845 * This function is called when the platform device is destroyed.
Jerome Anand5dab11d2017-01-25 04:27:52 +05301846 */
Takashi Iwaida864802017-01-31 13:52:22 +01001847static int hdmi_lpe_audio_remove(struct platform_device *pdev)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301848{
Takashi Iwaida864802017-01-31 13:52:22 +01001849 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301850
Takashi Iwaida864802017-01-31 13:52:22 +01001851 snd_card_free(ctx->card);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301852 return 0;
1853}
1854
Takashi Iwai182cdf22017-02-02 14:43:39 +01001855static const struct dev_pm_ops hdmi_lpe_audio_pm = {
1856 SET_SYSTEM_SLEEP_PM_OPS(hdmi_lpe_audio_suspend, hdmi_lpe_audio_resume)
1857 SET_RUNTIME_PM_OPS(hdmi_lpe_audio_runtime_suspend, NULL, NULL)
1858};
1859
Takashi Iwaida864802017-01-31 13:52:22 +01001860static struct platform_driver hdmi_lpe_audio_driver = {
1861 .driver = {
1862 .name = "hdmi-lpe-audio",
Takashi Iwai182cdf22017-02-02 14:43:39 +01001863 .pm = &hdmi_lpe_audio_pm,
Takashi Iwaida864802017-01-31 13:52:22 +01001864 },
1865 .probe = hdmi_lpe_audio_probe,
1866 .remove = hdmi_lpe_audio_remove,
Takashi Iwaida864802017-01-31 13:52:22 +01001867};
1868
1869module_platform_driver(hdmi_lpe_audio_driver);
1870MODULE_ALIAS("platform:hdmi_lpe_audio");
1871
Jerome Anand5dab11d2017-01-25 04:27:52 +05301872MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1873MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1874MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1875MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1876MODULE_DESCRIPTION("Intel HDMI Audio driver");
1877MODULE_LICENSE("GPL v2");
1878MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");