blob: 015d57cd9725ae4f39f4d0c1d15a4ae265b6c9e8 [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 Iwai03c34372017-02-02 16:19:03 +010040#include <drm/drm_edid.h>
Takashi Iwaida864802017-01-31 13:52:22 +010041#include <drm/intel_lpe_audio.h>
Jerome Anand5dab11d2017-01-25 04:27:52 +053042#include "intel_hdmi_audio.h"
43
Jerome Anand5dab11d2017-01-25 04:27:52 +053044/*standard module options for ALSA. This module supports only one card*/
45static int hdmi_card_index = SNDRV_DEFAULT_IDX1;
46static char *hdmi_card_id = SNDRV_DEFAULT_STR1;
Jerome Anand5dab11d2017-01-25 04:27:52 +053047
48module_param_named(index, hdmi_card_index, int, 0444);
49MODULE_PARM_DESC(index,
50 "Index value for INTEL Intel HDMI Audio controller.");
51module_param_named(id, hdmi_card_id, charp, 0444);
52MODULE_PARM_DESC(id,
53 "ID string for INTEL Intel HDMI Audio controller.");
54
55/*
56 * ELD SA bits in the CEA Speaker Allocation data block
57 */
Takashi Iwai4a5ddb22017-02-01 16:45:38 +010058static const int eld_speaker_allocation_bits[] = {
Jerome Anand5dab11d2017-01-25 04:27:52 +053059 [0] = FL | FR,
60 [1] = LFE,
61 [2] = FC,
62 [3] = RL | RR,
63 [4] = RC,
64 [5] = FLC | FRC,
65 [6] = RLC | RRC,
66 /* the following are not defined in ELD yet */
67 [7] = 0,
68};
69
70/*
71 * This is an ordered list!
72 *
73 * The preceding ones have better chances to be selected by
74 * hdmi_channel_allocation().
75 */
76static struct cea_channel_speaker_allocation channel_allocations[] = {
77/* channel: 7 6 5 4 3 2 1 0 */
78{ .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
79 /* 2.1 */
80{ .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
81 /* Dolby Surround */
82{ .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
83 /* surround40 */
84{ .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
85 /* surround41 */
86{ .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
87 /* surround50 */
88{ .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
89 /* surround51 */
90{ .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
91 /* 6.1 */
92{ .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
93 /* surround71 */
94{ .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
95
96{ .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
97{ .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
98{ .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
99{ .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
100{ .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
101{ .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
102{ .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
103{ .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
104{ .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
105{ .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
106{ .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
107{ .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
108{ .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
109{ .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
110{ .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
111{ .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
112{ .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
113{ .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
114{ .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
115{ .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
116{ .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
117{ .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
118{ .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
119};
120
Takashi Iwai4a5ddb22017-02-01 16:45:38 +0100121static const struct channel_map_table map_tables[] = {
Jerome Anand5dab11d2017-01-25 04:27:52 +0530122 { SNDRV_CHMAP_FL, 0x00, FL },
123 { SNDRV_CHMAP_FR, 0x01, FR },
124 { SNDRV_CHMAP_RL, 0x04, RL },
125 { SNDRV_CHMAP_RR, 0x05, RR },
126 { SNDRV_CHMAP_LFE, 0x02, LFE },
127 { SNDRV_CHMAP_FC, 0x03, FC },
128 { SNDRV_CHMAP_RLC, 0x06, RLC },
129 { SNDRV_CHMAP_RRC, 0x07, RRC },
130 {} /* terminator */
131};
132
133/* hardware capability structure */
Takashi Iwaib5562902017-02-04 22:05:33 +0100134static const struct snd_pcm_hardware had_pcm_hardware = {
Jerome Anand5dab11d2017-01-25 04:27:52 +0530135 .info = (SNDRV_PCM_INFO_INTERLEAVED |
Takashi Iwaia9ebdd02017-02-02 21:33:54 +0100136 SNDRV_PCM_INFO_MMAP |
Takashi Iwaie8de9852017-02-07 08:09:12 +0100137 SNDRV_PCM_INFO_MMAP_VALID |
138 SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
Takashi Iwai3fe2cf72017-02-07 13:53:42 +0100139 .formats = (SNDRV_PCM_FMTBIT_S16_LE |
140 SNDRV_PCM_FMTBIT_S24_LE |
Takashi Iwai85bd8742017-02-07 13:33:17 +0100141 SNDRV_PCM_FMTBIT_S32_LE),
Jerome Anand5dab11d2017-01-25 04:27:52 +0530142 .rates = SNDRV_PCM_RATE_32000 |
143 SNDRV_PCM_RATE_44100 |
144 SNDRV_PCM_RATE_48000 |
145 SNDRV_PCM_RATE_88200 |
146 SNDRV_PCM_RATE_96000 |
147 SNDRV_PCM_RATE_176400 |
148 SNDRV_PCM_RATE_192000,
149 .rate_min = HAD_MIN_RATE,
150 .rate_max = HAD_MAX_RATE,
151 .channels_min = HAD_MIN_CHANNEL,
152 .channels_max = HAD_MAX_CHANNEL,
153 .buffer_bytes_max = HAD_MAX_BUFFER,
154 .period_bytes_min = HAD_MIN_PERIOD_BYTES,
155 .period_bytes_max = HAD_MAX_PERIOD_BYTES,
156 .periods_min = HAD_MIN_PERIODS,
157 .periods_max = HAD_MAX_PERIODS,
158 .fifo_size = HAD_FIFO_SIZE,
159};
160
Takashi Iwai313d9f22017-02-02 13:00:12 +0100161/* Get the active PCM substream;
162 * Call had_substream_put() for unreferecing.
163 * Don't call this inside had_spinlock, as it takes by itself
164 */
165static struct snd_pcm_substream *
166had_substream_get(struct snd_intelhad *intelhaddata)
167{
168 struct snd_pcm_substream *substream;
169 unsigned long flags;
170
171 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
172 substream = intelhaddata->stream_info.substream;
173 if (substream)
174 intelhaddata->stream_info.substream_refcount++;
175 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
176 return substream;
177}
178
179/* Unref the active PCM substream;
180 * Don't call this inside had_spinlock, as it takes by itself
181 */
182static void had_substream_put(struct snd_intelhad *intelhaddata)
183{
184 unsigned long flags;
185
186 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
187 intelhaddata->stream_info.substream_refcount--;
188 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
189}
190
Jerome Anand5dab11d2017-01-25 04:27:52 +0530191/* Register access functions */
Takashi Iwai83af57d2017-02-03 08:50:06 +0100192static void had_read_register(struct snd_intelhad *ctx, u32 reg, u32 *val)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530193{
Takashi Iwaida864802017-01-31 13:52:22 +0100194 *val = ioread32(ctx->mmio_start + ctx->had_config_offset + reg);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530195}
196
Takashi Iwai83af57d2017-02-03 08:50:06 +0100197static void had_write_register(struct snd_intelhad *ctx, u32 reg, u32 val)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530198{
Takashi Iwaida864802017-01-31 13:52:22 +0100199 iowrite32(val, ctx->mmio_start + ctx->had_config_offset + reg);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530200}
201
Takashi Iwaida864802017-01-31 13:52:22 +0100202/*
Takashi Iwai313d9f22017-02-02 13:00:12 +0100203 * enable / disable audio configuration
204 *
Takashi Iwai83af57d2017-02-03 08:50:06 +0100205 * The normal read/modify should not directly be used on VLV2 for
Takashi Iwaida864802017-01-31 13:52:22 +0100206 * updating AUD_CONFIG register.
Jerome Anand5dab11d2017-01-25 04:27:52 +0530207 * This is because:
208 * Bit6 of AUD_CONFIG register is writeonly due to a silicon bug on VLV2
209 * HDMI IP. As a result a read-modify of AUD_CONFIG regiter will always
210 * clear bit6. AUD_CONFIG[6:4] represents the "channels" field of the
211 * register. This field should be 1xy binary for configuration with 6 or
212 * more channels. Read-modify of AUD_CONFIG (Eg. for enabling audio)
213 * causes the "channels" field to be updated as 0xy binary resulting in
214 * bad audio. The fix is to always write the AUD_CONFIG[6:4] with
215 * appropriate value when doing read-modify of AUD_CONFIG register.
Jerome Anand5dab11d2017-01-25 04:27:52 +0530216 */
Takashi Iwai40ce4b52017-02-07 16:17:06 +0100217static void had_enable_audio(struct snd_intelhad *intelhaddata,
Takashi Iwaib5562902017-02-04 22:05:33 +0100218 bool enable)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530219{
Takashi Iwai40ce4b52017-02-07 16:17:06 +0100220 /* update the cached value */
221 intelhaddata->aud_config.regx.aud_en = enable;
222 had_write_register(intelhaddata, AUD_CONFIG,
223 intelhaddata->aud_config.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530224}
225
Takashi Iwai075a1d42017-02-07 07:55:27 +0100226/* forcibly ACKs to both BUFFER_DONE and BUFFER_UNDERRUN interrupts */
227static void had_ack_irqs(struct snd_intelhad *ctx)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530228{
Takashi Iwaida864802017-01-31 13:52:22 +0100229 u32 status_reg;
230
Takashi Iwai075a1d42017-02-07 07:55:27 +0100231 had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
232 status_reg |= HDMI_AUDIO_BUFFER_DONE | HDMI_AUDIO_UNDERRUN;
233 had_write_register(ctx, AUD_HDMI_STATUS, status_reg);
234 had_read_register(ctx, AUD_HDMI_STATUS, &status_reg);
Takashi Iwaida864802017-01-31 13:52:22 +0100235}
236
Takashi Iwaif4566aa2017-02-04 21:39:56 +0100237/* Reset buffer pointers */
238static void had_reset_audio(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530239{
Takashi Iwai77531be2017-02-07 12:17:23 +0100240 had_write_register(intelhaddata, AUD_HDMI_STATUS,
241 AUD_HDMI_STATUSG_MASK_FUNCRST);
Takashi Iwaif4566aa2017-02-04 21:39:56 +0100242 had_write_register(intelhaddata, AUD_HDMI_STATUS, 0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530243}
244
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100245/*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530246 * initialize audio channel status registers
247 * This function is called in the prepare callback
248 */
249static int had_prog_status_reg(struct snd_pcm_substream *substream,
250 struct snd_intelhad *intelhaddata)
251{
Takashi Iwai7ceba752017-02-02 15:58:35 +0100252 union aud_cfg cfg_val = {.regval = 0};
253 union aud_ch_status_0 ch_stat0 = {.regval = 0};
254 union aud_ch_status_1 ch_stat1 = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530255
Takashi Iwai7ceba752017-02-02 15:58:35 +0100256 ch_stat0.regx.lpcm_id = (intelhaddata->aes_bits &
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100257 IEC958_AES0_NONAUDIO) >> 1;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100258 ch_stat0.regx.clk_acc = (intelhaddata->aes_bits &
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100259 IEC958_AES3_CON_CLOCK) >> 4;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100260 cfg_val.regx.val_bit = ch_stat0.regx.lpcm_id;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530261
262 switch (substream->runtime->rate) {
263 case AUD_SAMPLE_RATE_32:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100264 ch_stat0.regx.samp_freq = CH_STATUS_MAP_32KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530265 break;
266
267 case AUD_SAMPLE_RATE_44_1:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100268 ch_stat0.regx.samp_freq = CH_STATUS_MAP_44KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530269 break;
270 case AUD_SAMPLE_RATE_48:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100271 ch_stat0.regx.samp_freq = CH_STATUS_MAP_48KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530272 break;
273 case AUD_SAMPLE_RATE_88_2:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100274 ch_stat0.regx.samp_freq = CH_STATUS_MAP_88KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530275 break;
276 case AUD_SAMPLE_RATE_96:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100277 ch_stat0.regx.samp_freq = CH_STATUS_MAP_96KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530278 break;
279 case AUD_SAMPLE_RATE_176_4:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100280 ch_stat0.regx.samp_freq = CH_STATUS_MAP_176KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530281 break;
282 case AUD_SAMPLE_RATE_192:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100283 ch_stat0.regx.samp_freq = CH_STATUS_MAP_192KHZ;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530284 break;
285
286 default:
287 /* control should never come here */
288 return -EINVAL;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530289 }
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100290
Takashi Iwai79dda752017-01-30 17:23:39 +0100291 had_write_register(intelhaddata,
Takashi Iwai7ceba752017-02-02 15:58:35 +0100292 AUD_CH_STATUS_0, ch_stat0.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530293
Takashi Iwai85bd8742017-02-07 13:33:17 +0100294 switch (substream->runtime->format) {
Takashi Iwai85bd8742017-02-07 13:33:17 +0100295 case SNDRV_PCM_FORMAT_S16_LE:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100296 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_20;
297 ch_stat1.regx.wrd_len = SMPL_WIDTH_16BITS;
Takashi Iwai85bd8742017-02-07 13:33:17 +0100298 break;
Takashi Iwai85bd8742017-02-07 13:33:17 +0100299 case SNDRV_PCM_FORMAT_S24_LE:
300 case SNDRV_PCM_FORMAT_S32_LE:
Takashi Iwai7ceba752017-02-02 15:58:35 +0100301 ch_stat1.regx.max_wrd_len = MAX_SMPL_WIDTH_24;
302 ch_stat1.regx.wrd_len = SMPL_WIDTH_24BITS;
Takashi Iwai85bd8742017-02-07 13:33:17 +0100303 break;
304 default:
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_1, ch_stat1.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530310 return 0;
311}
312
Takashi Iwai76296ef2017-01-30 16:09:11 +0100313/*
Jerome Anand5dab11d2017-01-25 04:27:52 +0530314 * function to initialize audio
315 * registers and buffer confgiuration registers
316 * This function is called in the prepare callback
317 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100318static int had_init_audio_ctrl(struct snd_pcm_substream *substream,
319 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530320{
Takashi Iwai7ceba752017-02-02 15:58:35 +0100321 union aud_cfg cfg_val = {.regval = 0};
322 union aud_buf_config buf_cfg = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530323 u8 channels;
324
325 had_prog_status_reg(substream, intelhaddata);
326
Takashi Iwai7ceba752017-02-02 15:58:35 +0100327 buf_cfg.regx.audio_fifo_watermark = FIFO_THRESHOLD;
328 buf_cfg.regx.dma_fifo_watermark = DMA_FIFO_THRESHOLD;
329 buf_cfg.regx.aud_delay = 0;
330 had_write_register(intelhaddata, AUD_BUF_CONFIG, buf_cfg.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530331
332 channels = substream->runtime->channels;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100333 cfg_val.regx.num_ch = channels - 2;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530334 if (channels <= 2)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100335 cfg_val.regx.layout = LAYOUT0;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530336 else
Takashi Iwai7ceba752017-02-02 15:58:35 +0100337 cfg_val.regx.layout = LAYOUT1;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530338
Takashi Iwai3fe2cf72017-02-07 13:53:42 +0100339 if (substream->runtime->format == SNDRV_PCM_FORMAT_S16_LE)
340 cfg_val.regx.packet_mode = 1;
341
Takashi Iwai85bd8742017-02-07 13:33:17 +0100342 if (substream->runtime->format == SNDRV_PCM_FORMAT_S32_LE)
343 cfg_val.regx.left_align = 1;
344
Takashi Iwai7ceba752017-02-02 15:58:35 +0100345 cfg_val.regx.val_bit = 1;
Takashi Iwai83af57d2017-02-03 08:50:06 +0100346
347 /* fix up the DP bits */
348 if (intelhaddata->dp_output) {
349 cfg_val.regx.dp_modei = 1;
350 cfg_val.regx.set = 1;
351 }
352
Takashi Iwai7ceba752017-02-02 15:58:35 +0100353 had_write_register(intelhaddata, AUD_CONFIG, cfg_val.regval);
Takashi Iwai40ce4b52017-02-07 16:17:06 +0100354 intelhaddata->aud_config = cfg_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530355 return 0;
356}
357
Jerome Anand5dab11d2017-01-25 04:27:52 +0530358/*
359 * Compute derived values in channel_allocations[].
360 */
361static void init_channel_allocations(void)
362{
363 int i, j;
364 struct cea_channel_speaker_allocation *p;
365
Jerome Anand5dab11d2017-01-25 04:27:52 +0530366 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
367 p = channel_allocations + i;
368 p->channels = 0;
369 p->spk_mask = 0;
370 for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
371 if (p->speakers[j]) {
372 p->channels++;
373 p->spk_mask |= p->speakers[j];
374 }
375 }
376}
377
378/*
379 * The transformation takes two steps:
380 *
381 * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
382 * spk_mask => (channel_allocations[]) => ai->CA
383 *
384 * TODO: it could select the wrong CA from multiple candidates.
385 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100386static int had_channel_allocation(struct snd_intelhad *intelhaddata,
387 int channels)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530388{
389 int i;
390 int ca = 0;
391 int spk_mask = 0;
392
393 /*
394 * CA defaults to 0 for basic stereo audio
395 */
396 if (channels <= 2)
397 return 0;
398
399 /*
400 * expand ELD's speaker allocation mask
401 *
402 * ELD tells the speaker mask in a compact(paired) form,
403 * expand ELD's notions to match the ones used by Audio InfoFrame.
404 */
405
406 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100407 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530408 spk_mask |= eld_speaker_allocation_bits[i];
409 }
410
411 /* search for the first working match in the CA table */
412 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
413 if (channels == channel_allocations[i].channels &&
414 (spk_mask & channel_allocations[i].spk_mask) ==
415 channel_allocations[i].spk_mask) {
416 ca = channel_allocations[i].ca_index;
417 break;
418 }
419 }
420
Takashi Iwaic75b0472017-01-31 15:49:15 +0100421 dev_dbg(intelhaddata->dev, "select CA 0x%x for %d\n", ca, channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530422
423 return ca;
424}
425
426/* from speaker bit mask to ALSA API channel position */
427static int spk_to_chmap(int spk)
428{
Takashi Iwai4a5ddb22017-02-01 16:45:38 +0100429 const struct channel_map_table *t = map_tables;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530430
431 for (; t->map; t++) {
432 if (t->spk_mask == spk)
433 return t->map;
434 }
435 return 0;
436}
437
Takashi Iwai372d8552017-01-31 13:57:58 +0100438static void had_build_channel_allocation_map(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530439{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100440 int i, c;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530441 int spk_mask = 0;
442 struct snd_pcm_chmap_elem *chmap;
443 u8 eld_high, eld_high_mask = 0xF0;
444 u8 high_msb;
445
446 chmap = kzalloc(sizeof(*chmap), GFP_KERNEL);
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100447 if (!chmap) {
Jerome Anand5dab11d2017-01-25 04:27:52 +0530448 intelhaddata->chmap->chmap = NULL;
449 return;
450 }
451
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100452 dev_dbg(intelhaddata->dev, "eld speaker = %x\n",
453 intelhaddata->eld[DRM_ELD_SPEAKER]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530454
455 /* WA: Fix the max channel supported to 8 */
456
457 /*
458 * Sink may support more than 8 channels, if eld_high has more than
459 * one bit set. SOC supports max 8 channels.
460 * Refer eld_speaker_allocation_bits, for sink speaker allocation
461 */
462
463 /* if 0x2F < eld < 0x4F fall back to 0x2f, else fall back to 0x4F */
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100464 eld_high = intelhaddata->eld[DRM_ELD_SPEAKER] & eld_high_mask;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530465 if ((eld_high & (eld_high-1)) && (eld_high > 0x1F)) {
466 /* eld_high & (eld_high-1): if more than 1 bit set */
467 /* 0x1F: 7 channels */
468 for (i = 1; i < 4; i++) {
469 high_msb = eld_high & (0x80 >> i);
470 if (high_msb) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100471 intelhaddata->eld[DRM_ELD_SPEAKER] &=
Jerome Anand5dab11d2017-01-25 04:27:52 +0530472 high_msb | 0xF;
473 break;
474 }
475 }
476 }
477
478 for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
Takashi Iwaidf0435d2017-02-02 15:37:11 +0100479 if (intelhaddata->eld[DRM_ELD_SPEAKER] & (1 << i))
Jerome Anand5dab11d2017-01-25 04:27:52 +0530480 spk_mask |= eld_speaker_allocation_bits[i];
481 }
482
483 for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
484 if (spk_mask == channel_allocations[i].spk_mask) {
485 for (c = 0; c < channel_allocations[i].channels; c++) {
486 chmap->map[c] = spk_to_chmap(
487 channel_allocations[i].speakers[
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100488 (MAX_SPEAKERS - 1) - c]);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530489 }
490 chmap->channels = channel_allocations[i].channels;
491 intelhaddata->chmap->chmap = chmap;
492 break;
493 }
494 }
495 if (i >= ARRAY_SIZE(channel_allocations)) {
496 intelhaddata->chmap->chmap = NULL;
497 kfree(chmap);
498 }
499}
500
501/*
502 * ALSA API channel-map control callbacks
503 */
504static int had_chmap_ctl_info(struct snd_kcontrol *kcontrol,
505 struct snd_ctl_elem_info *uinfo)
506{
507 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
508 struct snd_intelhad *intelhaddata = info->private_data;
509
Takashi Iwai91b0cb02017-02-02 17:46:49 +0100510 if (!intelhaddata->connected)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530511 return -ENODEV;
512 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
513 uinfo->count = HAD_MAX_CHANNEL;
514 uinfo->value.integer.min = 0;
515 uinfo->value.integer.max = SNDRV_CHMAP_LAST;
516 return 0;
517}
518
519static int had_chmap_ctl_get(struct snd_kcontrol *kcontrol,
520 struct snd_ctl_elem_value *ucontrol)
521{
522 struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
523 struct snd_intelhad *intelhaddata = info->private_data;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100524 int i;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530525 const struct snd_pcm_chmap_elem *chmap;
526
Takashi Iwai91b0cb02017-02-02 17:46:49 +0100527 if (!intelhaddata->connected)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530528 return -ENODEV;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100529
530 mutex_lock(&intelhaddata->mutex);
531 if (!intelhaddata->chmap->chmap) {
532 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530533 return -ENODATA;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100534 }
535
Jerome Anand5dab11d2017-01-25 04:27:52 +0530536 chmap = intelhaddata->chmap->chmap;
Takashi Iwaic75b0472017-01-31 15:49:15 +0100537 for (i = 0; i < chmap->channels; i++)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530538 ucontrol->value.integer.value[i] = chmap->map[i];
Takashi Iwai8f8d1d72017-02-01 17:24:02 +0100539 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530540
541 return 0;
542}
543
544static int had_register_chmap_ctls(struct snd_intelhad *intelhaddata,
545 struct snd_pcm *pcm)
546{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100547 int err;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530548
549 err = snd_pcm_add_chmap_ctls(pcm, SNDRV_PCM_STREAM_PLAYBACK,
550 NULL, 0, (unsigned long)intelhaddata,
551 &intelhaddata->chmap);
552 if (err < 0)
553 return err;
554
555 intelhaddata->chmap->private_data = intelhaddata;
Takashi Iwaie9d65ab2017-01-31 16:11:27 +0100556 intelhaddata->chmap->kctl->info = had_chmap_ctl_info;
557 intelhaddata->chmap->kctl->get = had_chmap_ctl_get;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530558 intelhaddata->chmap->chmap = NULL;
559 return 0;
560}
561
Takashi Iwai76296ef2017-01-30 16:09:11 +0100562/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100563 * Initialize Data Island Packets registers
Jerome Anand5dab11d2017-01-25 04:27:52 +0530564 * This function is called in the prepare callback
565 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100566static void had_prog_dip(struct snd_pcm_substream *substream,
567 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530568{
569 int i;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100570 union aud_ctrl_st ctrl_state = {.regval = 0};
571 union aud_info_frame2 frame2 = {.regval = 0};
572 union aud_info_frame3 frame3 = {.regval = 0};
Jerome Anand5dab11d2017-01-25 04:27:52 +0530573 u8 checksum = 0;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600574 u32 info_frame;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530575 int channels;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100576 int ca;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530577
578 channels = substream->runtime->channels;
579
Takashi Iwai7ceba752017-02-02 15:58:35 +0100580 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530581
Takashi Iwaib5562902017-02-04 22:05:33 +0100582 ca = had_channel_allocation(intelhaddata, channels);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600583 if (intelhaddata->dp_output) {
584 info_frame = DP_INFO_FRAME_WORD1;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100585 frame2.regval = (substream->runtime->channels - 1) | (ca << 24);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600586 } else {
587 info_frame = HDMI_INFO_FRAME_WORD1;
Takashi Iwai7ceba752017-02-02 15:58:35 +0100588 frame2.regx.chnl_cnt = substream->runtime->channels - 1;
Takashi Iwai36ed3462017-02-02 17:06:38 +0100589 frame3.regx.chnl_alloc = ca;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530590
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100591 /* Calculte the byte wide checksum for all valid DIP words */
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600592 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100593 checksum += (info_frame >> (i * 8)) & 0xff;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600594 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100595 checksum += (frame2.regval >> (i * 8)) & 0xff;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600596 for (i = 0; i < BYTES_PER_WORD; i++)
Takashi Iwai7ceba752017-02-02 15:58:35 +0100597 checksum += (frame3.regval >> (i * 8)) & 0xff;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530598
Takashi Iwai7ceba752017-02-02 15:58:35 +0100599 frame2.regx.chksum = -(checksum);
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600600 }
Jerome Anand5dab11d2017-01-25 04:27:52 +0530601
Takashi Iwai4151ee82017-01-31 18:14:15 +0100602 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, info_frame);
Takashi Iwai7ceba752017-02-02 15:58:35 +0100603 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame2.regval);
604 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, frame3.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530605
606 /* program remaining DIP words with zero */
607 for (i = 0; i < HAD_MAX_DIP_WORDS-VALID_DIP_WORDS; i++)
Takashi Iwai4151ee82017-01-31 18:14:15 +0100608 had_write_register(intelhaddata, AUD_HDMIW_INFOFR, 0x0);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530609
Takashi Iwai7ceba752017-02-02 15:58:35 +0100610 ctrl_state.regx.dip_freq = 1;
611 ctrl_state.regx.dip_en_sta = 1;
612 had_write_register(intelhaddata, AUD_CNTL_ST, ctrl_state.regval);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530613}
614
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600615static int had_calculate_maud_value(u32 aud_samp_freq, u32 link_rate)
616{
617 u32 maud_val;
618
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100619 /* Select maud according to DP 1.2 spec */
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600620 if (link_rate == DP_2_7_GHZ) {
621 switch (aud_samp_freq) {
622 case AUD_SAMPLE_RATE_32:
623 maud_val = AUD_SAMPLE_RATE_32_DP_2_7_MAUD_VAL;
624 break;
625
626 case AUD_SAMPLE_RATE_44_1:
627 maud_val = AUD_SAMPLE_RATE_44_1_DP_2_7_MAUD_VAL;
628 break;
629
630 case AUD_SAMPLE_RATE_48:
631 maud_val = AUD_SAMPLE_RATE_48_DP_2_7_MAUD_VAL;
632 break;
633
634 case AUD_SAMPLE_RATE_88_2:
635 maud_val = AUD_SAMPLE_RATE_88_2_DP_2_7_MAUD_VAL;
636 break;
637
638 case AUD_SAMPLE_RATE_96:
639 maud_val = AUD_SAMPLE_RATE_96_DP_2_7_MAUD_VAL;
640 break;
641
642 case AUD_SAMPLE_RATE_176_4:
643 maud_val = AUD_SAMPLE_RATE_176_4_DP_2_7_MAUD_VAL;
644 break;
645
646 case HAD_MAX_RATE:
647 maud_val = HAD_MAX_RATE_DP_2_7_MAUD_VAL;
648 break;
649
650 default:
651 maud_val = -EINVAL;
652 break;
653 }
654 } else if (link_rate == DP_1_62_GHZ) {
655 switch (aud_samp_freq) {
656 case AUD_SAMPLE_RATE_32:
657 maud_val = AUD_SAMPLE_RATE_32_DP_1_62_MAUD_VAL;
658 break;
659
660 case AUD_SAMPLE_RATE_44_1:
661 maud_val = AUD_SAMPLE_RATE_44_1_DP_1_62_MAUD_VAL;
662 break;
663
664 case AUD_SAMPLE_RATE_48:
665 maud_val = AUD_SAMPLE_RATE_48_DP_1_62_MAUD_VAL;
666 break;
667
668 case AUD_SAMPLE_RATE_88_2:
669 maud_val = AUD_SAMPLE_RATE_88_2_DP_1_62_MAUD_VAL;
670 break;
671
672 case AUD_SAMPLE_RATE_96:
673 maud_val = AUD_SAMPLE_RATE_96_DP_1_62_MAUD_VAL;
674 break;
675
676 case AUD_SAMPLE_RATE_176_4:
677 maud_val = AUD_SAMPLE_RATE_176_4_DP_1_62_MAUD_VAL;
678 break;
679
680 case HAD_MAX_RATE:
681 maud_val = HAD_MAX_RATE_DP_1_62_MAUD_VAL;
682 break;
683
684 default:
685 maud_val = -EINVAL;
686 break;
687 }
688 } else
689 maud_val = -EINVAL;
690
691 return maud_val;
692}
693
Takashi Iwai76296ef2017-01-30 16:09:11 +0100694/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100695 * Program HDMI audio CTS value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530696 *
697 * @aud_samp_freq: sampling frequency of audio data
698 * @tmds: sampling frequency of the display data
Takashi Iwaib5562902017-02-04 22:05:33 +0100699 * @link_rate: DP link rate
Jerome Anand5dab11d2017-01-25 04:27:52 +0530700 * @n_param: N value, depends on aud_samp_freq
Takashi Iwaib5562902017-02-04 22:05:33 +0100701 * @intelhaddata: substream private data
Jerome Anand5dab11d2017-01-25 04:27:52 +0530702 *
703 * Program CTS register based on the audio and display sampling frequency
704 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100705static void had_prog_cts(u32 aud_samp_freq, u32 tmds, u32 link_rate,
706 u32 n_param, struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530707{
708 u32 cts_val;
709 u64 dividend, divisor;
710
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600711 if (intelhaddata->dp_output) {
712 /* Substitute cts_val with Maud according to DP 1.2 spec*/
713 cts_val = had_calculate_maud_value(aud_samp_freq, link_rate);
714 } else {
715 /* Calculate CTS according to HDMI 1.3a spec*/
716 dividend = (u64)tmds * n_param*1000;
717 divisor = 128 * aud_samp_freq;
718 cts_val = div64_u64(dividend, divisor);
719 }
Takashi Iwaic75b0472017-01-31 15:49:15 +0100720 dev_dbg(intelhaddata->dev, "TMDS value=%d, N value=%d, CTS Value=%d\n",
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600721 tmds, n_param, cts_val);
Takashi Iwai79dda752017-01-30 17:23:39 +0100722 had_write_register(intelhaddata, AUD_HDMI_CTS, (BIT(24) | cts_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530723}
724
725static int had_calculate_n_value(u32 aud_samp_freq)
726{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100727 int n_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530728
729 /* Select N according to HDMI 1.3a spec*/
730 switch (aud_samp_freq) {
731 case AUD_SAMPLE_RATE_32:
732 n_val = 4096;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100733 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530734
735 case AUD_SAMPLE_RATE_44_1:
736 n_val = 6272;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100737 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530738
739 case AUD_SAMPLE_RATE_48:
740 n_val = 6144;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100741 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530742
743 case AUD_SAMPLE_RATE_88_2:
744 n_val = 12544;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100745 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530746
747 case AUD_SAMPLE_RATE_96:
748 n_val = 12288;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100749 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530750
751 case AUD_SAMPLE_RATE_176_4:
752 n_val = 25088;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100753 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530754
755 case HAD_MAX_RATE:
756 n_val = 24576;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100757 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530758
759 default:
760 n_val = -EINVAL;
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100761 break;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530762 }
763 return n_val;
764}
765
Takashi Iwai76296ef2017-01-30 16:09:11 +0100766/*
Takashi Iwai44684f62017-02-02 17:27:40 +0100767 * Program HDMI audio N value
Jerome Anand5dab11d2017-01-25 04:27:52 +0530768 *
769 * @aud_samp_freq: sampling frequency of audio data
770 * @n_param: N value, depends on aud_samp_freq
Takashi Iwaib5562902017-02-04 22:05:33 +0100771 * @intelhaddata: substream private data
Jerome Anand5dab11d2017-01-25 04:27:52 +0530772 *
773 * This function is called in the prepare callback.
774 * It programs based on the audio and display sampling frequency
775 */
Takashi Iwaib5562902017-02-04 22:05:33 +0100776static int had_prog_n(u32 aud_samp_freq, u32 *n_param,
777 struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530778{
Takashi Iwai2e52f5e2017-01-31 17:09:13 +0100779 int n_val;
Jerome Anand5dab11d2017-01-25 04:27:52 +0530780
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -0600781 if (intelhaddata->dp_output) {
782 /*
783 * According to DP specs, Maud and Naud values hold
784 * a relationship, which is stated as:
785 * Maud/Naud = 512 * fs / f_LS_Clk
786 * where, fs is the sampling frequency of the audio stream
787 * and Naud is 32768 for Async clock.
788 */
789
790 n_val = DP_NAUD_VAL;
791 } else
792 n_val = had_calculate_n_value(aud_samp_freq);
Jerome Anand5dab11d2017-01-25 04:27:52 +0530793
794 if (n_val < 0)
795 return n_val;
796
Takashi Iwai79dda752017-01-30 17:23:39 +0100797 had_write_register(intelhaddata, AUD_N_ENABLE, (BIT(24) | n_val));
Jerome Anand5dab11d2017-01-25 04:27:52 +0530798 *n_param = n_val;
799 return 0;
800}
801
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100802/*
803 * PCM ring buffer handling
804 *
805 * The hardware provides a ring buffer with the fixed 4 buffer descriptors
806 * (BDs). The driver maps these 4 BDs onto the PCM ring buffer. The mapping
807 * moves at each period elapsed. The below illustrates how it works:
808 *
809 * At time=0
810 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
811 * BD | 0 | 1 | 2 | 3 |
812 *
813 * At time=1 (period elapsed)
814 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
815 * BD | 1 | 2 | 3 | 0 |
816 *
817 * At time=2 (second period elapsed)
818 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
819 * BD | 2 | 3 | 0 | 1 |
820 *
821 * The bd_head field points to the index of the BD to be read. It's also the
822 * position to be filled at next. The pcm_head and the pcm_filled fields
823 * point to the indices of the current position and of the next position to
824 * be filled, respectively. For PCM buffer there are both _head and _filled
825 * because they may be difference when nperiods > 4. For example, in the
826 * example above at t=1, bd_head=1 and pcm_head=1 while pcm_filled=5:
827 *
828 * pcm_head (=1) --v v-- pcm_filled (=5)
829 * PCM | 0 | 1 | 2 | 3 | 4 | 5 | .... |n-1|
830 * BD | 1 | 2 | 3 | 0 |
831 * bd_head (=1) --^ ^-- next to fill (= bd_head)
832 *
833 * For nperiods < 4, the remaining BDs out of 4 are marked as invalid, so that
834 * the hardware skips those BDs in the loop.
Takashi Iwai8d48c012017-02-07 08:05:46 +0100835 *
836 * An exceptional setup is the case with nperiods=1. Since we have to update
837 * BDs after finishing one BD processing, we'd need at least two BDs, where
838 * both BDs point to the same content, the same address, the same size of the
839 * whole PCM buffer.
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100840 */
841
842#define AUD_BUF_ADDR(x) (AUD_BUF_A_ADDR + (x) * HAD_REG_WIDTH)
843#define AUD_BUF_LEN(x) (AUD_BUF_A_LENGTH + (x) * HAD_REG_WIDTH)
844
845/* Set up a buffer descriptor at the "filled" position */
846static void had_prog_bd(struct snd_pcm_substream *substream,
847 struct snd_intelhad *intelhaddata)
848{
849 int idx = intelhaddata->bd_head;
850 int ofs = intelhaddata->pcmbuf_filled * intelhaddata->period_bytes;
851 u32 addr = substream->runtime->dma_addr + ofs;
852
Takashi Iwaie8de9852017-02-07 08:09:12 +0100853 addr |= AUD_BUF_VALID;
854 if (!substream->runtime->no_period_wakeup)
855 addr |= AUD_BUF_INTR_EN;
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100856 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), addr);
857 had_write_register(intelhaddata, AUD_BUF_LEN(idx),
858 intelhaddata->period_bytes);
859
860 /* advance the indices to the next */
861 intelhaddata->bd_head++;
862 intelhaddata->bd_head %= intelhaddata->num_bds;
863 intelhaddata->pcmbuf_filled++;
864 intelhaddata->pcmbuf_filled %= substream->runtime->periods;
865}
866
867/* invalidate a buffer descriptor with the given index */
868static void had_invalidate_bd(struct snd_intelhad *intelhaddata,
869 int idx)
870{
871 had_write_register(intelhaddata, AUD_BUF_ADDR(idx), 0);
872 had_write_register(intelhaddata, AUD_BUF_LEN(idx), 0);
873}
874
875/* Initial programming of ring buffer */
876static void had_init_ringbuf(struct snd_pcm_substream *substream,
877 struct snd_intelhad *intelhaddata)
878{
879 struct snd_pcm_runtime *runtime = substream->runtime;
880 int i, num_periods;
881
882 num_periods = runtime->periods;
883 intelhaddata->num_bds = min(num_periods, HAD_NUM_OF_RING_BUFS);
Takashi Iwai8d48c012017-02-07 08:05:46 +0100884 /* set the minimum 2 BDs for num_periods=1 */
885 intelhaddata->num_bds = max(intelhaddata->num_bds, 2U);
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100886 intelhaddata->period_bytes =
887 frames_to_bytes(runtime, runtime->period_size);
888 WARN_ON(intelhaddata->period_bytes & 0x3f);
889
890 intelhaddata->bd_head = 0;
891 intelhaddata->pcmbuf_head = 0;
892 intelhaddata->pcmbuf_filled = 0;
893
894 for (i = 0; i < HAD_NUM_OF_RING_BUFS; i++) {
Takashi Iwai8d48c012017-02-07 08:05:46 +0100895 if (i < intelhaddata->num_bds)
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100896 had_prog_bd(substream, intelhaddata);
897 else /* invalidate the rest */
898 had_invalidate_bd(intelhaddata, i);
899 }
900
901 intelhaddata->bd_head = 0; /* reset at head again before starting */
902}
903
904/* process a bd, advance to the next */
905static void had_advance_ringbuf(struct snd_pcm_substream *substream,
906 struct snd_intelhad *intelhaddata)
907{
908 int num_periods = substream->runtime->periods;
909
910 /* reprogram the next buffer */
911 had_prog_bd(substream, intelhaddata);
912
913 /* proceed to next */
914 intelhaddata->pcmbuf_head++;
915 intelhaddata->pcmbuf_head %= num_periods;
916}
917
918/* process the current BD(s);
919 * returns the current PCM buffer byte position, or -EPIPE for underrun.
920 */
921static int had_process_ringbuf(struct snd_pcm_substream *substream,
922 struct snd_intelhad *intelhaddata)
923{
924 int len, processed;
925 unsigned long flags;
926
927 processed = 0;
928 spin_lock_irqsave(&intelhaddata->had_spinlock, flags);
929 for (;;) {
930 /* get the remaining bytes on the buffer */
931 had_read_register(intelhaddata,
932 AUD_BUF_LEN(intelhaddata->bd_head),
933 &len);
934 if (len < 0 || len > intelhaddata->period_bytes) {
935 dev_dbg(intelhaddata->dev, "Invalid buf length %d\n",
936 len);
937 len = -EPIPE;
938 goto out;
939 }
940
941 if (len > 0) /* OK, this is the current buffer */
942 break;
943
944 /* len=0 => already empty, check the next buffer */
945 if (++processed >= intelhaddata->num_bds) {
946 len = -EPIPE; /* all empty? - report underrun */
947 goto out;
948 }
949 had_advance_ringbuf(substream, intelhaddata);
950 }
951
952 len = intelhaddata->period_bytes - len;
953 len += intelhaddata->period_bytes * intelhaddata->pcmbuf_head;
954 out:
955 spin_unlock_irqrestore(&intelhaddata->had_spinlock, flags);
956 return len;
957}
958
959/* called from irq handler */
960static void had_process_buffer_done(struct snd_intelhad *intelhaddata)
961{
962 struct snd_pcm_substream *substream;
963
964 if (!intelhaddata->connected)
965 return; /* disconnected? - bail out */
966
967 substream = had_substream_get(intelhaddata);
968 if (!substream)
969 return; /* no stream? - bail out */
970
971 /* process or stop the stream */
972 if (had_process_ringbuf(substream, intelhaddata) < 0)
973 snd_pcm_stop_xrun(substream);
974 else
975 snd_pcm_period_elapsed(substream);
976
977 had_substream_put(intelhaddata);
978}
979
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100980/*
981 * The interrupt status 'sticky' bits might not be cleared by
982 * setting '1' to that bit once...
983 */
984static void wait_clear_underrun_bit(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +0530985{
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100986 int i;
987 u32 val;
988
Takashi Iwaie2acecf2017-02-11 08:21:56 +0100989 for (i = 0; i < 100; i++) {
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100990 /* clear bit30, 31 AUD_HDMI_STATUS */
991 had_read_register(intelhaddata, AUD_HDMI_STATUS, &val);
Takashi Iwai77531be2017-02-07 12:17:23 +0100992 if (!(val & AUD_HDMI_STATUS_MASK_UNDERRUN))
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100993 return;
Takashi Iwaie2acecf2017-02-11 08:21:56 +0100994 udelay(100);
995 cond_resched();
Takashi Iwaie1b239f2017-02-03 00:01:18 +0100996 had_write_register(intelhaddata, AUD_HDMI_STATUS, val);
997 }
998 dev_err(intelhaddata->dev, "Unable to clear UNDERRUN bits\n");
999}
1000
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001001/* Perform some reset procedure but only when need_reset is set;
1002 * this is called from prepare or hw_free callbacks once after trigger STOP
1003 * or underrun has been processed in order to settle down the h/w state.
1004 */
1005static void had_do_reset(struct snd_intelhad *intelhaddata)
1006{
1007 if (!intelhaddata->need_reset)
1008 return;
1009
1010 /* Reset buffer pointers */
1011 had_reset_audio(intelhaddata);
1012 wait_clear_underrun_bit(intelhaddata);
1013 intelhaddata->need_reset = false;
1014}
1015
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001016/* called from irq handler */
1017static void had_process_buffer_underrun(struct snd_intelhad *intelhaddata)
1018{
1019 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301020
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001021 /* Report UNDERRUN error to above layers */
1022 substream = had_substream_get(intelhaddata);
1023 if (substream) {
1024 snd_pcm_stop_xrun(substream);
1025 had_substream_put(intelhaddata);
1026 }
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001027 intelhaddata->need_reset = true;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301028}
1029
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001030/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001031 * ALSA PCM open callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301032 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001033static int had_pcm_open(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301034{
1035 struct snd_intelhad *intelhaddata;
1036 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301037 int retval;
1038
Jerome Anand5dab11d2017-01-25 04:27:52 +05301039 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301040 runtime = substream->runtime;
1041
Takashi Iwai182cdf22017-02-02 14:43:39 +01001042 pm_runtime_get_sync(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301043
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001044 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001045 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
1046 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301047 retval = -ENODEV;
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001048 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301049 }
1050
1051 /* set the runtime hw parameter with local snd_pcm_hardware struct */
Takashi Iwaib5562902017-02-04 22:05:33 +01001052 runtime->hw = had_pcm_hardware;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301053
Jerome Anand5dab11d2017-01-25 04:27:52 +05301054 retval = snd_pcm_hw_constraint_integer(runtime,
1055 SNDRV_PCM_HW_PARAM_PERIODS);
1056 if (retval < 0)
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001057 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301058
1059 /* Make sure, that the period size is always aligned
1060 * 64byte boundary
1061 */
1062 retval = snd_pcm_hw_constraint_step(substream->runtime, 0,
1063 SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 64);
Takashi Iwai73997b02017-02-02 17:38:50 +01001064 if (retval < 0)
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001065 goto error;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301066
Takashi Iwai85bd8742017-02-07 13:33:17 +01001067 retval = snd_pcm_hw_constraint_msbits(runtime, 0, 32, 24);
1068 if (retval < 0)
1069 goto error;
1070
Takashi Iwai73997b02017-02-02 17:38:50 +01001071 /* expose PCM substream */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001072 spin_lock_irq(&intelhaddata->had_spinlock);
1073 intelhaddata->stream_info.substream = substream;
1074 intelhaddata->stream_info.substream_refcount++;
1075 spin_unlock_irq(&intelhaddata->had_spinlock);
1076
Jerome Anand5dab11d2017-01-25 04:27:52 +05301077 return retval;
Takashi Iwaifa5dfe62017-02-01 22:03:26 +01001078 error:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301079 pm_runtime_put(intelhaddata->dev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301080 return retval;
1081}
1082
Takashi Iwaidf76df12017-01-31 16:04:10 +01001083/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001084 * ALSA PCM close callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301085 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001086static int had_pcm_close(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301087{
1088 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301089
Jerome Anand5dab11d2017-01-25 04:27:52 +05301090 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301091
Takashi Iwai73997b02017-02-02 17:38:50 +01001092 /* unreference and sync with the pending PCM accesses */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001093 spin_lock_irq(&intelhaddata->had_spinlock);
1094 intelhaddata->stream_info.substream = NULL;
1095 intelhaddata->stream_info.substream_refcount--;
1096 while (intelhaddata->stream_info.substream_refcount > 0) {
1097 spin_unlock_irq(&intelhaddata->had_spinlock);
1098 cpu_relax();
1099 spin_lock_irq(&intelhaddata->had_spinlock);
1100 }
1101 spin_unlock_irq(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301102
Jerome Anand5dab11d2017-01-25 04:27:52 +05301103 pm_runtime_put(intelhaddata->dev);
1104 return 0;
1105}
1106
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001107/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001108 * ALSA PCM hw_params callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301109 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001110static int had_pcm_hw_params(struct snd_pcm_substream *substream,
1111 struct snd_pcm_hw_params *hw_params)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301112{
Takashi Iwaic75b0472017-01-31 15:49:15 +01001113 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301114 unsigned long addr;
1115 int pages, buf_size, retval;
1116
Takashi Iwaic75b0472017-01-31 15:49:15 +01001117 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301118 buf_size = params_buffer_bytes(hw_params);
1119 retval = snd_pcm_lib_malloc_pages(substream, buf_size);
1120 if (retval < 0)
1121 return retval;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001122 dev_dbg(intelhaddata->dev, "%s:allocated memory = %d\n",
1123 __func__, buf_size);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301124 /* mark the pages as uncached region */
1125 addr = (unsigned long) substream->runtime->dma_area;
1126 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1127 retval = set_memory_uc(addr, pages);
1128 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001129 dev_err(intelhaddata->dev, "set_memory_uc failed.Error:%d\n",
1130 retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301131 return retval;
1132 }
1133 memset(substream->runtime->dma_area, 0, buf_size);
1134
1135 return retval;
1136}
1137
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001138/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001139 * ALSA PCM hw_free callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301140 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001141static int had_pcm_hw_free(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301142{
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001143 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301144 unsigned long addr;
1145 u32 pages;
1146
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001147 intelhaddata = snd_pcm_substream_chip(substream);
1148 had_do_reset(intelhaddata);
1149
Jerome Anand5dab11d2017-01-25 04:27:52 +05301150 /* mark back the pages as cached/writeback region before the free */
1151 if (substream->runtime->dma_area != NULL) {
1152 addr = (unsigned long) substream->runtime->dma_area;
1153 pages = (substream->runtime->dma_bytes + PAGE_SIZE - 1) /
1154 PAGE_SIZE;
1155 set_memory_wb(addr, pages);
1156 return snd_pcm_lib_free_pages(substream);
1157 }
1158 return 0;
1159}
1160
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001161/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001162 * ALSA PCM trigger callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301163 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001164static int had_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301165{
Takashi Iwaida864802017-01-31 13:52:22 +01001166 int retval = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301167 struct snd_intelhad *intelhaddata;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301168
Jerome Anand5dab11d2017-01-25 04:27:52 +05301169 intelhaddata = snd_pcm_substream_chip(substream);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301170
1171 switch (cmd) {
1172 case SNDRV_PCM_TRIGGER_START:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001173 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1174 case SNDRV_PCM_TRIGGER_RESUME:
Jerome Anand5dab11d2017-01-25 04:27:52 +05301175 /* Disable local INTRs till register prgmng is done */
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001176 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001177 dev_dbg(intelhaddata->dev,
1178 "_START: HDMI cable plugged-out\n");
Jerome Anand5dab11d2017-01-25 04:27:52 +05301179 retval = -ENODEV;
1180 break;
1181 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301182
Takashi Iwaif69bd102017-02-02 14:57:22 +01001183 intelhaddata->stream_info.running = true;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301184
1185 /* Enable Audio */
Takashi Iwai075a1d42017-02-07 07:55:27 +01001186 had_ack_irqs(intelhaddata); /* FIXME: do we need this? */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001187 had_enable_audio(intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301188 break;
1189
1190 case SNDRV_PCM_TRIGGER_STOP:
Takashi Iwai182cdf22017-02-02 14:43:39 +01001191 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1192 case SNDRV_PCM_TRIGGER_SUSPEND:
Takashi Iwaibcce7752017-02-01 17:18:20 +01001193 spin_lock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301194
Takashi Iwaic75b0472017-01-31 15:49:15 +01001195 /* Stop reporting BUFFER_DONE/UNDERRUN to above layers */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301196
Takashi Iwaif69bd102017-02-02 14:57:22 +01001197 intelhaddata->stream_info.running = false;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001198 spin_unlock(&intelhaddata->had_spinlock);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301199 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001200 had_enable_audio(intelhaddata, false);
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001201 intelhaddata->need_reset = true;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301202 break;
1203
1204 default:
1205 retval = -EINVAL;
1206 }
1207 return retval;
1208}
1209
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001210/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001211 * ALSA PCM prepare callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301212 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001213static int had_pcm_prepare(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301214{
1215 int retval;
1216 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001217 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301218 struct snd_intelhad *intelhaddata;
1219 struct snd_pcm_runtime *runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301220
Jerome Anand5dab11d2017-01-25 04:27:52 +05301221 intelhaddata = snd_pcm_substream_chip(substream);
1222 runtime = substream->runtime;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301223
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001224 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001225 dev_dbg(intelhaddata->dev, "%s: HDMI cable plugged-out\n",
1226 __func__);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301227 retval = -ENODEV;
1228 goto prep_end;
1229 }
1230
Takashi Iwaic75b0472017-01-31 15:49:15 +01001231 dev_dbg(intelhaddata->dev, "period_size=%d\n",
Jerome Anand5dab11d2017-01-25 04:27:52 +05301232 (int)frames_to_bytes(runtime, runtime->period_size));
Takashi Iwaic75b0472017-01-31 15:49:15 +01001233 dev_dbg(intelhaddata->dev, "periods=%d\n", runtime->periods);
1234 dev_dbg(intelhaddata->dev, "buffer_size=%d\n",
1235 (int)snd_pcm_lib_buffer_bytes(substream));
1236 dev_dbg(intelhaddata->dev, "rate=%d\n", runtime->rate);
1237 dev_dbg(intelhaddata->dev, "channels=%d\n", runtime->channels);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301238
Takashi Iwaie2acecf2017-02-11 08:21:56 +01001239 had_do_reset(intelhaddata);
1240
Jerome Anand5dab11d2017-01-25 04:27:52 +05301241 /* Get N value in KHz */
Takashi Iwaida864802017-01-31 13:52:22 +01001242 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301243
Takashi Iwaib5562902017-02-04 22:05:33 +01001244 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301245 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001246 dev_err(intelhaddata->dev,
1247 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301248 goto prep_end;
1249 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001250
1251 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001252 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001253
Takashi Iwaib5562902017-02-04 22:05:33 +01001254 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1255 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301256
Takashi Iwaib5562902017-02-04 22:05:33 +01001257 had_prog_dip(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301258
Takashi Iwaib5562902017-02-04 22:05:33 +01001259 retval = had_init_audio_ctrl(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301260
1261 /* Prog buffer address */
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001262 had_init_ringbuf(substream, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301263
1264 /*
1265 * Program channel mapping in following order:
1266 * FL, FR, C, LFE, RL, RR
1267 */
1268
Takashi Iwai79dda752017-01-30 17:23:39 +01001269 had_write_register(intelhaddata, AUD_BUF_CH_SWAP, SWAP_LFE_CENTER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301270
1271prep_end:
1272 return retval;
1273}
1274
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001275/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001276 * ALSA PCM pointer callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301277 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001278static snd_pcm_uframes_t had_pcm_pointer(struct snd_pcm_substream *substream)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301279{
1280 struct snd_intelhad *intelhaddata;
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001281 int len;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301282
Jerome Anand5dab11d2017-01-25 04:27:52 +05301283 intelhaddata = snd_pcm_substream_chip(substream);
1284
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001285 if (!intelhaddata->connected)
Takashi Iwai79f439e2017-01-31 16:46:44 +01001286 return SNDRV_PCM_POS_XRUN;
1287
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001288 len = had_process_ringbuf(substream, intelhaddata);
1289 if (len < 0)
1290 return SNDRV_PCM_POS_XRUN;
Takashi Iwai8d48c012017-02-07 08:05:46 +01001291 len = bytes_to_frames(substream->runtime, len);
1292 /* wrapping may happen when periods=1 */
1293 len %= substream->runtime->buffer_size;
1294 return len;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301295}
1296
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001297/*
Takashi Iwai44684f62017-02-02 17:27:40 +01001298 * ALSA PCM mmap callback
Jerome Anand5dab11d2017-01-25 04:27:52 +05301299 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001300static int had_pcm_mmap(struct snd_pcm_substream *substream,
1301 struct vm_area_struct *vma)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301302{
Jerome Anand5dab11d2017-01-25 04:27:52 +05301303 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1304 return remap_pfn_range(vma, vma->vm_start,
1305 substream->dma_buffer.addr >> PAGE_SHIFT,
1306 vma->vm_end - vma->vm_start, vma->vm_page_prot);
1307}
1308
Takashi Iwai73997b02017-02-02 17:38:50 +01001309/*
1310 * ALSA PCM ops
1311 */
Takashi Iwaib5562902017-02-04 22:05:33 +01001312static const struct snd_pcm_ops had_pcm_ops = {
1313 .open = had_pcm_open,
1314 .close = had_pcm_close,
Takashi Iwai73997b02017-02-02 17:38:50 +01001315 .ioctl = snd_pcm_lib_ioctl,
Takashi Iwaib5562902017-02-04 22:05:33 +01001316 .hw_params = had_pcm_hw_params,
1317 .hw_free = had_pcm_hw_free,
1318 .prepare = had_pcm_prepare,
1319 .trigger = had_pcm_trigger,
1320 .pointer = had_pcm_pointer,
1321 .mmap = had_pcm_mmap,
Takashi Iwai73997b02017-02-02 17:38:50 +01001322};
1323
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001324/* process mode change of the running stream; called in mutex */
Takashi Iwaib5562902017-02-04 22:05:33 +01001325static int had_process_mode_change(struct snd_intelhad *intelhaddata)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301326{
Takashi Iwaida864802017-01-31 13:52:22 +01001327 struct snd_pcm_substream *substream;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301328 int retval = 0;
1329 u32 disp_samp_freq, n_param;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001330 u32 link_rate = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301331
Takashi Iwai313d9f22017-02-02 13:00:12 +01001332 substream = had_substream_get(intelhaddata);
1333 if (!substream)
Takashi Iwaida864802017-01-31 13:52:22 +01001334 return 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301335
1336 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001337 had_enable_audio(intelhaddata, false);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301338
1339 /* Update CTS value */
Takashi Iwaida864802017-01-31 13:52:22 +01001340 disp_samp_freq = intelhaddata->tmds_clock_speed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301341
Takashi Iwaib5562902017-02-04 22:05:33 +01001342 retval = had_prog_n(substream->runtime->rate, &n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301343 if (retval) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001344 dev_err(intelhaddata->dev,
1345 "programming N value failed %#x\n", retval);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301346 goto out;
1347 }
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001348
1349 if (intelhaddata->dp_output)
Takashi Iwaida864802017-01-31 13:52:22 +01001350 link_rate = intelhaddata->link_rate;
Pierre-Louis Bossart964ca802017-01-31 14:16:52 -06001351
Takashi Iwaib5562902017-02-04 22:05:33 +01001352 had_prog_cts(substream->runtime->rate, disp_samp_freq, link_rate,
1353 n_param, intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301354
1355 /* Enable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001356 had_enable_audio(intelhaddata, true);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301357
1358out:
Takashi Iwai313d9f22017-02-02 13:00:12 +01001359 had_substream_put(intelhaddata);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301360 return retval;
1361}
1362
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001363/* process hot plug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001364static void had_process_hot_plug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001365{
Takashi Iwai372d8552017-01-31 13:57:58 +01001366 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001367
Takashi Iwaibcce7752017-02-01 17:18:20 +01001368 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001369 if (intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001370 dev_dbg(intelhaddata->dev, "Device already connected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001371 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001372 return;
Takashi Iwai372d8552017-01-31 13:57:58 +01001373 }
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001374
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001375 intelhaddata->connected = true;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001376 dev_dbg(intelhaddata->dev,
1377 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_CONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001378 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001379 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001380
Takashi Iwai372d8552017-01-31 13:57:58 +01001381 /* Safety check */
Takashi Iwai313d9f22017-02-02 13:00:12 +01001382 substream = had_substream_get(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001383 if (substream) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001384 dev_dbg(intelhaddata->dev,
1385 "Force to stop the active stream by disconnection\n");
Takashi Iwai372d8552017-01-31 13:57:58 +01001386 /* Set runtime->state to hw_params done */
1387 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001388 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001389 }
1390
1391 had_build_channel_allocation_map(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001392}
1393
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001394/* process hot unplug, called from wq with mutex locked */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001395static void had_process_hot_unplug(struct snd_intelhad *intelhaddata)
Takashi Iwai372d8552017-01-31 13:57:58 +01001396{
Takashi Iwai313d9f22017-02-02 13:00:12 +01001397 struct snd_pcm_substream *substream;
Takashi Iwai372d8552017-01-31 13:57:58 +01001398
Takashi Iwai313d9f22017-02-02 13:00:12 +01001399 substream = had_substream_get(intelhaddata);
1400
Takashi Iwaibcce7752017-02-01 17:18:20 +01001401 spin_lock_irq(&intelhaddata->had_spinlock);
Takashi Iwai372d8552017-01-31 13:57:58 +01001402
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001403 if (!intelhaddata->connected) {
Takashi Iwaic75b0472017-01-31 15:49:15 +01001404 dev_dbg(intelhaddata->dev, "Device already disconnected\n");
Takashi Iwaibcce7752017-02-01 17:18:20 +01001405 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001406 goto out;
Takashi Iwai372d8552017-01-31 13:57:58 +01001407
Takashi Iwai372d8552017-01-31 13:57:58 +01001408 }
1409
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001410 /* Disable Audio */
Takashi Iwai40ce4b52017-02-07 16:17:06 +01001411 had_enable_audio(intelhaddata, false);
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001412
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001413 intelhaddata->connected = false;
Takashi Iwaic75b0472017-01-31 15:49:15 +01001414 dev_dbg(intelhaddata->dev,
1415 "%s @ %d:DEBUG PLUG/UNPLUG : HAD_DRV_DISCONNECTED\n",
Takashi Iwai372d8552017-01-31 13:57:58 +01001416 __func__, __LINE__);
Takashi Iwaibcce7752017-02-01 17:18:20 +01001417 spin_unlock_irq(&intelhaddata->had_spinlock);
Takashi Iwai313d9f22017-02-02 13:00:12 +01001418
1419 /* Report to above ALSA layer */
1420 if (substream)
1421 snd_pcm_stop(substream, SNDRV_PCM_STATE_SETUP);
1422
1423 out:
1424 if (substream)
1425 had_substream_put(intelhaddata);
Takashi Iwai372d8552017-01-31 13:57:58 +01001426 kfree(intelhaddata->chmap->chmap);
1427 intelhaddata->chmap->chmap = NULL;
Takashi Iwai372d8552017-01-31 13:57:58 +01001428}
1429
Takashi Iwai73997b02017-02-02 17:38:50 +01001430/*
1431 * ALSA iec958 and ELD controls
1432 */
Jerome Anand5dab11d2017-01-25 04:27:52 +05301433
Jerome Anand5dab11d2017-01-25 04:27:52 +05301434static int had_iec958_info(struct snd_kcontrol *kcontrol,
1435 struct snd_ctl_elem_info *uinfo)
1436{
1437 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
1438 uinfo->count = 1;
1439 return 0;
1440}
1441
1442static int had_iec958_get(struct snd_kcontrol *kcontrol,
1443 struct snd_ctl_elem_value *ucontrol)
1444{
1445 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1446
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001447 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301448 ucontrol->value.iec958.status[0] = (intelhaddata->aes_bits >> 0) & 0xff;
1449 ucontrol->value.iec958.status[1] = (intelhaddata->aes_bits >> 8) & 0xff;
1450 ucontrol->value.iec958.status[2] =
1451 (intelhaddata->aes_bits >> 16) & 0xff;
1452 ucontrol->value.iec958.status[3] =
1453 (intelhaddata->aes_bits >> 24) & 0xff;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001454 mutex_unlock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301455 return 0;
1456}
Takashi Iwai372d8552017-01-31 13:57:58 +01001457
Jerome Anand5dab11d2017-01-25 04:27:52 +05301458static int had_iec958_mask_get(struct snd_kcontrol *kcontrol,
1459 struct snd_ctl_elem_value *ucontrol)
1460{
1461 ucontrol->value.iec958.status[0] = 0xff;
1462 ucontrol->value.iec958.status[1] = 0xff;
1463 ucontrol->value.iec958.status[2] = 0xff;
1464 ucontrol->value.iec958.status[3] = 0xff;
1465 return 0;
1466}
Takashi Iwai372d8552017-01-31 13:57:58 +01001467
Jerome Anand5dab11d2017-01-25 04:27:52 +05301468static int had_iec958_put(struct snd_kcontrol *kcontrol,
1469 struct snd_ctl_elem_value *ucontrol)
1470{
1471 unsigned int val;
1472 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001473 int changed = 0;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301474
Jerome Anand5dab11d2017-01-25 04:27:52 +05301475 val = (ucontrol->value.iec958.status[0] << 0) |
1476 (ucontrol->value.iec958.status[1] << 8) |
1477 (ucontrol->value.iec958.status[2] << 16) |
1478 (ucontrol->value.iec958.status[3] << 24);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001479 mutex_lock(&intelhaddata->mutex);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301480 if (intelhaddata->aes_bits != val) {
1481 intelhaddata->aes_bits = val;
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001482 changed = 1;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301483 }
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001484 mutex_unlock(&intelhaddata->mutex);
1485 return changed;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301486}
1487
Takashi Iwai4aedb942017-02-02 16:38:39 +01001488static int had_ctl_eld_info(struct snd_kcontrol *kcontrol,
1489 struct snd_ctl_elem_info *uinfo)
1490{
1491 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1492 uinfo->count = HDMI_MAX_ELD_BYTES;
1493 return 0;
1494}
1495
1496static int had_ctl_eld_get(struct snd_kcontrol *kcontrol,
1497 struct snd_ctl_elem_value *ucontrol)
1498{
1499 struct snd_intelhad *intelhaddata = snd_kcontrol_chip(kcontrol);
1500
1501 mutex_lock(&intelhaddata->mutex);
1502 memcpy(ucontrol->value.bytes.data, intelhaddata->eld,
1503 HDMI_MAX_ELD_BYTES);
1504 mutex_unlock(&intelhaddata->mutex);
1505 return 0;
1506}
1507
Takashi Iwai73997b02017-02-02 17:38:50 +01001508static const struct snd_kcontrol_new had_controls[] = {
Takashi Iwai4aedb942017-02-02 16:38:39 +01001509 {
1510 .access = SNDRV_CTL_ELEM_ACCESS_READ,
1511 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1512 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, MASK),
1513 .info = had_iec958_info, /* shared */
1514 .get = had_iec958_mask_get,
1515 },
1516 {
1517 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1518 .name = SNDRV_CTL_NAME_IEC958("", PLAYBACK, DEFAULT),
1519 .info = had_iec958_info,
1520 .get = had_iec958_get,
1521 .put = had_iec958_put,
1522 },
1523 {
1524 .access = (SNDRV_CTL_ELEM_ACCESS_READ |
1525 SNDRV_CTL_ELEM_ACCESS_VOLATILE),
1526 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1527 .name = "ELD",
1528 .info = had_ctl_eld_info,
1529 .get = had_ctl_eld_get,
1530 },
Jerome Anand5dab11d2017-01-25 04:27:52 +05301531};
1532
Takashi Iwai73997b02017-02-02 17:38:50 +01001533/*
1534 * audio interrupt handler
1535 */
Takashi Iwaida864802017-01-31 13:52:22 +01001536static irqreturn_t display_pipe_interrupt_handler(int irq, void *dev_id)
1537{
1538 struct snd_intelhad *ctx = dev_id;
1539 u32 audio_stat, audio_reg;
1540
Takashi Iwai4151ee82017-01-31 18:14:15 +01001541 audio_reg = AUD_HDMI_STATUS;
Takashi Iwai83af57d2017-02-03 08:50:06 +01001542 had_read_register(ctx, audio_reg, &audio_stat);
Takashi Iwaida864802017-01-31 13:52:22 +01001543
1544 if (audio_stat & HDMI_AUDIO_UNDERRUN) {
Takashi Iwai83af57d2017-02-03 08:50:06 +01001545 had_write_register(ctx, audio_reg, HDMI_AUDIO_UNDERRUN);
Takashi Iwaida864802017-01-31 13:52:22 +01001546 had_process_buffer_underrun(ctx);
1547 }
1548
1549 if (audio_stat & HDMI_AUDIO_BUFFER_DONE) {
Takashi Iwai83af57d2017-02-03 08:50:06 +01001550 had_write_register(ctx, audio_reg, HDMI_AUDIO_BUFFER_DONE);
Takashi Iwaida864802017-01-31 13:52:22 +01001551 had_process_buffer_done(ctx);
1552 }
1553
1554 return IRQ_HANDLED;
1555}
1556
Takashi Iwai73997b02017-02-02 17:38:50 +01001557/*
1558 * monitor plug/unplug notification from i915; just kick off the work
1559 */
Takashi Iwaida864802017-01-31 13:52:22 +01001560static void notify_audio_lpe(struct platform_device *pdev)
1561{
1562 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Takashi Iwaida864802017-01-31 13:52:22 +01001563
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001564 schedule_work(&ctx->hdmi_audio_wq);
1565}
Takashi Iwaida864802017-01-31 13:52:22 +01001566
Takashi Iwai73997b02017-02-02 17:38:50 +01001567/* the work to handle monitor hot plug/unplug */
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001568static void had_audio_wq(struct work_struct *work)
1569{
1570 struct snd_intelhad *ctx =
1571 container_of(work, struct snd_intelhad, hdmi_audio_wq);
1572 struct intel_hdmi_lpe_audio_pdata *pdata = ctx->dev->platform_data;
1573
Takashi Iwai182cdf22017-02-02 14:43:39 +01001574 pm_runtime_get_sync(ctx->dev);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001575 mutex_lock(&ctx->mutex);
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001576 if (!pdata->hdmi_connected) {
1577 dev_dbg(ctx->dev, "%s: Event: HAD_NOTIFY_HOT_UNPLUG\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001578 __func__);
Takashi Iwai4aedb942017-02-02 16:38:39 +01001579 memset(ctx->eld, 0, sizeof(ctx->eld)); /* clear the old ELD */
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001580 had_process_hot_unplug(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001581 } else {
1582 struct intel_hdmi_lpe_audio_eld *eld = &pdata->eld;
1583
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001584 dev_dbg(ctx->dev, "%s: HAD_NOTIFY_ELD : port = %d, tmds = %d\n",
1585 __func__, eld->port_id, pdata->tmds_clock_speed);
1586
Takashi Iwaida864802017-01-31 13:52:22 +01001587 switch (eld->pipe_id) {
1588 case 0:
1589 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1590 break;
1591 case 1:
1592 ctx->had_config_offset = AUDIO_HDMI_CONFIG_B;
1593 break;
1594 case 2:
1595 ctx->had_config_offset = AUDIO_HDMI_CONFIG_C;
1596 break;
1597 default:
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001598 dev_dbg(ctx->dev, "Invalid pipe %d\n",
Takashi Iwaida864802017-01-31 13:52:22 +01001599 eld->pipe_id);
1600 break;
1601 }
1602
Takashi Iwaidf0435d2017-02-02 15:37:11 +01001603 memcpy(ctx->eld, eld->eld_data, sizeof(ctx->eld));
Takashi Iwaida864802017-01-31 13:52:22 +01001604
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001605 ctx->dp_output = pdata->dp_output;
1606 ctx->tmds_clock_speed = pdata->tmds_clock_speed;
1607 ctx->link_rate = pdata->link_rate;
1608
Takashi Iwaida864802017-01-31 13:52:22 +01001609 had_process_hot_plug(ctx);
1610
Takashi Iwai0e9c67d2017-02-01 17:53:19 +01001611 /* Process mode change if stream is active */
Takashi Iwaib5562902017-02-04 22:05:33 +01001612 had_process_mode_change(ctx);
Takashi Iwaida864802017-01-31 13:52:22 +01001613 }
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001614 mutex_unlock(&ctx->mutex);
Takashi Iwai182cdf22017-02-02 14:43:39 +01001615 pm_runtime_put(ctx->dev);
1616}
1617
1618/*
1619 * PM callbacks
1620 */
1621
1622static int hdmi_lpe_audio_runtime_suspend(struct device *dev)
1623{
1624 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1625 struct snd_pcm_substream *substream;
1626
1627 substream = had_substream_get(ctx);
1628 if (substream) {
1629 snd_pcm_suspend(substream);
1630 had_substream_put(ctx);
1631 }
1632
1633 return 0;
1634}
1635
Arnd Bergmann1df98922017-02-07 14:38:51 +01001636static int __maybe_unused hdmi_lpe_audio_suspend(struct device *dev)
Takashi Iwai182cdf22017-02-02 14:43:39 +01001637{
1638 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1639 int err;
1640
1641 err = hdmi_lpe_audio_runtime_suspend(dev);
1642 if (!err)
1643 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D3hot);
1644 return err;
1645}
1646
Arnd Bergmann1df98922017-02-07 14:38:51 +01001647static int __maybe_unused hdmi_lpe_audio_resume(struct device *dev)
Takashi Iwai182cdf22017-02-02 14:43:39 +01001648{
1649 struct snd_intelhad *ctx = dev_get_drvdata(dev);
1650
1651 snd_power_change_state(ctx->card, SNDRV_CTL_POWER_D0);
1652 return 0;
Takashi Iwaida864802017-01-31 13:52:22 +01001653}
1654
1655/* release resources */
1656static void hdmi_lpe_audio_free(struct snd_card *card)
1657{
1658 struct snd_intelhad *ctx = card->private_data;
1659
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001660 cancel_work_sync(&ctx->hdmi_audio_wq);
1661
Takashi Iwaida864802017-01-31 13:52:22 +01001662 if (ctx->mmio_start)
1663 iounmap(ctx->mmio_start);
1664 if (ctx->irq >= 0)
1665 free_irq(ctx->irq, ctx);
1666}
1667
1668/*
1669 * hdmi_lpe_audio_probe - start bridge with i915
1670 *
1671 * This function is called when the i915 driver creates the
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001672 * hdmi-lpe-audio platform device.
Takashi Iwaida864802017-01-31 13:52:22 +01001673 */
1674static int hdmi_lpe_audio_probe(struct platform_device *pdev)
1675{
1676 struct snd_card *card;
1677 struct snd_intelhad *ctx;
1678 struct snd_pcm *pcm;
1679 struct intel_hdmi_lpe_audio_pdata *pdata;
1680 int irq;
1681 struct resource *res_mmio;
Takashi Iwai4aedb942017-02-02 16:38:39 +01001682 int i, ret;
Takashi Iwaida864802017-01-31 13:52:22 +01001683
Takashi Iwaida864802017-01-31 13:52:22 +01001684 pdata = pdev->dev.platform_data;
1685 if (!pdata) {
1686 dev_err(&pdev->dev, "%s: quit: pdata not allocated by i915!!\n", __func__);
1687 return -EINVAL;
1688 }
1689
1690 /* get resources */
1691 irq = platform_get_irq(pdev, 0);
1692 if (irq < 0) {
1693 dev_err(&pdev->dev, "Could not get irq resource\n");
1694 return -ENODEV;
1695 }
1696
1697 res_mmio = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1698 if (!res_mmio) {
1699 dev_err(&pdev->dev, "Could not get IO_MEM resources\n");
1700 return -ENXIO;
1701 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301702
Takashi Iwai5647aec2017-01-31 08:14:34 +01001703 /* create a card instance with ALSA framework */
Takashi Iwaida864802017-01-31 13:52:22 +01001704 ret = snd_card_new(&pdev->dev, hdmi_card_index, hdmi_card_id,
1705 THIS_MODULE, sizeof(*ctx), &card);
1706 if (ret)
1707 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301708
Takashi Iwaida864802017-01-31 13:52:22 +01001709 ctx = card->private_data;
1710 spin_lock_init(&ctx->had_spinlock);
Takashi Iwai8f8d1d72017-02-01 17:24:02 +01001711 mutex_init(&ctx->mutex);
Takashi Iwai91b0cb02017-02-02 17:46:49 +01001712 ctx->connected = false;
Takashi Iwaida864802017-01-31 13:52:22 +01001713 ctx->dev = &pdev->dev;
1714 ctx->card = card;
Takashi Iwaida864802017-01-31 13:52:22 +01001715 ctx->aes_bits = SNDRV_PCM_DEFAULT_CON_SPDIF;
1716 strcpy(card->driver, INTEL_HAD);
Takashi Iwai873ab032017-02-07 12:14:04 +01001717 strcpy(card->shortname, "Intel HDMI/DP LPE Audio");
1718 strcpy(card->longname, "Intel HDMI/DP LPE Audio");
Jerome Anand5dab11d2017-01-25 04:27:52 +05301719
Takashi Iwaida864802017-01-31 13:52:22 +01001720 ctx->irq = -1;
1721 ctx->tmds_clock_speed = DIS_SAMPLE_RATE_148_5;
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001722 INIT_WORK(&ctx->hdmi_audio_wq, had_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301723
Takashi Iwaida864802017-01-31 13:52:22 +01001724 card->private_free = hdmi_lpe_audio_free;
1725
1726 /* assume pipe A as default */
1727 ctx->had_config_offset = AUDIO_HDMI_CONFIG_A;
1728
1729 platform_set_drvdata(pdev, ctx);
1730
1731 dev_dbg(&pdev->dev, "%s: mmio_start = 0x%x, mmio_end = 0x%x\n",
1732 __func__, (unsigned int)res_mmio->start,
1733 (unsigned int)res_mmio->end);
1734
1735 ctx->mmio_start = ioremap_nocache(res_mmio->start,
1736 (size_t)(resource_size(res_mmio)));
1737 if (!ctx->mmio_start) {
1738 dev_err(&pdev->dev, "Could not get ioremap\n");
1739 ret = -EACCES;
1740 goto err;
1741 }
1742
1743 /* setup interrupt handler */
1744 ret = request_irq(irq, display_pipe_interrupt_handler, 0,
1745 pdev->name, ctx);
1746 if (ret < 0) {
1747 dev_err(&pdev->dev, "request_irq failed\n");
1748 goto err;
1749 }
1750
1751 ctx->irq = irq;
1752
1753 ret = snd_pcm_new(card, INTEL_HAD, PCM_INDEX, MAX_PB_STREAMS,
1754 MAX_CAP_STREAMS, &pcm);
1755 if (ret)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301756 goto err;
1757
1758 /* setup private data which can be retrieved when required */
Takashi Iwaida864802017-01-31 13:52:22 +01001759 pcm->private_data = ctx;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301760 pcm->info_flags = 0;
1761 strncpy(pcm->name, card->shortname, strlen(card->shortname));
Takashi Iwaida864802017-01-31 13:52:22 +01001762 /* setup the ops for playabck */
Takashi Iwaib5562902017-02-04 22:05:33 +01001763 snd_pcm_set_ops(pcm, SNDRV_PCM_STREAM_PLAYBACK, &had_pcm_ops);
Takashi Iwai412bbe7d52017-02-02 22:03:22 +01001764
1765 /* only 32bit addressable */
1766 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
1767 dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1768
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001769 /* allocate dma pages;
1770 * try to allocate 600k buffer as default which is large enough
Jerome Anand5dab11d2017-01-25 04:27:52 +05301771 */
Takashi Iwaida864802017-01-31 13:52:22 +01001772 snd_pcm_lib_preallocate_pages_for_all(pcm,
Jerome Anand5dab11d2017-01-25 04:27:52 +05301773 SNDRV_DMA_TYPE_DEV, NULL,
Takashi Iwaie1b239f2017-02-03 00:01:18 +01001774 HAD_DEFAULT_BUFFER, HAD_MAX_BUFFER);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301775
Takashi Iwai4aedb942017-02-02 16:38:39 +01001776 /* create controls */
1777 for (i = 0; i < ARRAY_SIZE(had_controls); i++) {
1778 ret = snd_ctl_add(card, snd_ctl_new1(&had_controls[i], ctx));
1779 if (ret < 0)
1780 goto err;
1781 }
Jerome Anand5dab11d2017-01-25 04:27:52 +05301782
1783 init_channel_allocations();
1784
1785 /* Register channel map controls */
Takashi Iwaida864802017-01-31 13:52:22 +01001786 ret = had_register_chmap_ctls(ctx, pcm);
1787 if (ret < 0)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301788 goto err;
1789
Takashi Iwaida864802017-01-31 13:52:22 +01001790 ret = snd_card_register(card);
1791 if (ret)
Takashi Iwai36ec0d92017-01-31 08:47:05 +01001792 goto err;
1793
Takashi Iwaibcce7752017-02-01 17:18:20 +01001794 spin_lock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001795 pdata->notify_audio_lpe = notify_audio_lpe;
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001796 pdata->notify_pending = false;
Takashi Iwaibcce7752017-02-01 17:18:20 +01001797 spin_unlock_irq(&pdata->lpe_audio_slock);
Takashi Iwaida864802017-01-31 13:52:22 +01001798
1799 pm_runtime_set_active(&pdev->dev);
1800 pm_runtime_enable(&pdev->dev);
1801
Takashi Iwai99b2ab92017-01-31 16:26:10 +01001802 dev_dbg(&pdev->dev, "%s: handle pending notification\n", __func__);
Takashi Iwaida864802017-01-31 13:52:22 +01001803 schedule_work(&ctx->hdmi_audio_wq);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301804
Takashi Iwai79dda752017-01-30 17:23:39 +01001805 return 0;
Takashi Iwai5647aec2017-01-31 08:14:34 +01001806
Jerome Anand5dab11d2017-01-25 04:27:52 +05301807err:
1808 snd_card_free(card);
Takashi Iwaida864802017-01-31 13:52:22 +01001809 return ret;
Jerome Anand5dab11d2017-01-25 04:27:52 +05301810}
1811
Takashi Iwai79dda752017-01-30 17:23:39 +01001812/*
Takashi Iwaida864802017-01-31 13:52:22 +01001813 * hdmi_lpe_audio_remove - stop bridge with i915
Jerome Anand5dab11d2017-01-25 04:27:52 +05301814 *
Takashi Iwai2e52f5e2017-01-31 17:09:13 +01001815 * This function is called when the platform device is destroyed.
Jerome Anand5dab11d2017-01-25 04:27:52 +05301816 */
Takashi Iwaida864802017-01-31 13:52:22 +01001817static int hdmi_lpe_audio_remove(struct platform_device *pdev)
Jerome Anand5dab11d2017-01-25 04:27:52 +05301818{
Takashi Iwaida864802017-01-31 13:52:22 +01001819 struct snd_intelhad *ctx = platform_get_drvdata(pdev);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301820
Takashi Iwaida864802017-01-31 13:52:22 +01001821 snd_card_free(ctx->card);
Jerome Anand5dab11d2017-01-25 04:27:52 +05301822 return 0;
1823}
1824
Takashi Iwai182cdf22017-02-02 14:43:39 +01001825static const struct dev_pm_ops hdmi_lpe_audio_pm = {
1826 SET_SYSTEM_SLEEP_PM_OPS(hdmi_lpe_audio_suspend, hdmi_lpe_audio_resume)
1827 SET_RUNTIME_PM_OPS(hdmi_lpe_audio_runtime_suspend, NULL, NULL)
1828};
1829
Takashi Iwaida864802017-01-31 13:52:22 +01001830static struct platform_driver hdmi_lpe_audio_driver = {
1831 .driver = {
1832 .name = "hdmi-lpe-audio",
Takashi Iwai182cdf22017-02-02 14:43:39 +01001833 .pm = &hdmi_lpe_audio_pm,
Takashi Iwaida864802017-01-31 13:52:22 +01001834 },
1835 .probe = hdmi_lpe_audio_probe,
1836 .remove = hdmi_lpe_audio_remove,
Takashi Iwaida864802017-01-31 13:52:22 +01001837};
1838
1839module_platform_driver(hdmi_lpe_audio_driver);
1840MODULE_ALIAS("platform:hdmi_lpe_audio");
1841
Jerome Anand5dab11d2017-01-25 04:27:52 +05301842MODULE_AUTHOR("Sailaja Bandarupalli <sailaja.bandarupalli@intel.com>");
1843MODULE_AUTHOR("Ramesh Babu K V <ramesh.babu@intel.com>");
1844MODULE_AUTHOR("Vaibhav Agarwal <vaibhav.agarwal@intel.com>");
1845MODULE_AUTHOR("Jerome Anand <jerome.anand@intel.com>");
1846MODULE_DESCRIPTION("Intel HDMI Audio driver");
1847MODULE_LICENSE("GPL v2");
1848MODULE_SUPPORTED_DEVICE("{Intel,Intel_HAD}");