blob: 261c31890827af06fed01ec6cef7d0429226e09c [file] [log] [blame]
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301/*
2 * hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
3 *
4 * Copyright (C) 2014-2015 Intel Corp
5 * Author: Samreen Nilofer <samreen.nilofer@intel.com>
6 * Subhransu S. Prusty <subhransu.s.prusty@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 */
20#include <linux/init.h>
21#include <linux/delay.h>
22#include <linux/module.h>
23#include <linux/pm_runtime.h>
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +053024#include <linux/hdmi.h>
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +053025#include <drm/drm_edid.h>
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053026#include <sound/pcm_params.h>
Jeeja KP4a3478d2016-02-12 07:46:06 +053027#include <sound/jack.h>
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053028#include <sound/soc.h>
29#include <sound/hdaudio_ext.h>
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +053030#include <sound/hda_i915.h>
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +053031#include <sound/pcm_drm_eld.h>
Subhransu S. Prustybcced702016-04-14 10:07:30 +053032#include <sound/hda_chmap.h>
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053033#include "../../hda/local.h"
Jeeja KP4a3478d2016-02-12 07:46:06 +053034#include "hdac_hdmi.h"
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053035
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +053036#define NAME_SIZE 32
37
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +053038#define AMP_OUT_MUTE 0xb080
39#define AMP_OUT_UNMUTE 0xb000
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053040#define PIN_OUT (AC_PINCTL_OUT_EN)
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +053041
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053042#define HDA_MAX_CONNECTIONS 32
43
Subhransu S. Prusty148569f2016-02-12 07:46:07 +053044#define HDA_MAX_CVTS 3
45
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053046#define ELD_MAX_SIZE 256
47#define ELD_FIXED_BYTES 20
48
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +053049#define ELD_VER_CEA_861D 2
50#define ELD_VER_PARTIAL 31
51#define ELD_MAX_MNL 16
52
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053053struct hdac_hdmi_cvt_params {
54 unsigned int channels_min;
55 unsigned int channels_max;
56 u32 rates;
57 u64 formats;
58 unsigned int maxbps;
59};
60
61struct hdac_hdmi_cvt {
Subhransu S. Prusty15b91442015-12-09 21:46:10 +053062 struct list_head head;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053063 hda_nid_t nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +053064 const char *name;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053065 struct hdac_hdmi_cvt_params params;
66};
67
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +053068/* Currently only spk_alloc, more to be added */
69struct hdac_hdmi_parsed_eld {
70 u8 spk_alloc;
71};
72
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053073struct hdac_hdmi_eld {
74 bool monitor_present;
75 bool eld_valid;
76 int eld_size;
77 char eld_buffer[ELD_MAX_SIZE];
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +053078 struct hdac_hdmi_parsed_eld info;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053079};
80
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053081struct hdac_hdmi_pin {
Subhransu S. Prusty15b91442015-12-09 21:46:10 +053082 struct list_head head;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053083 hda_nid_t nid;
84 int num_mux_nids;
85 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053086 struct hdac_hdmi_eld eld;
87 struct hdac_ext_device *edev;
Subhransu S. Prustybcced702016-04-14 10:07:30 +053088 struct mutex lock;
89 bool chmap_set;
90 unsigned char chmap[8]; /* ALSA API channel-map */
91 int channels; /* current number of channels */
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053092};
93
Jeeja KP4a3478d2016-02-12 07:46:06 +053094struct hdac_hdmi_pcm {
95 struct list_head head;
96 int pcm_id;
97 struct hdac_hdmi_pin *pin;
98 struct hdac_hdmi_cvt *cvt;
99 struct snd_jack *jack;
100};
101
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530102struct hdac_hdmi_dai_pin_map {
103 int dai_id;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530104 struct hdac_hdmi_pin *pin;
105 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530106};
107
108struct hdac_hdmi_priv {
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530109 struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530110 struct list_head pin_list;
111 struct list_head cvt_list;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530112 struct list_head pcm_list;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530113 int num_pin;
114 int num_cvt;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530115 struct mutex pin_mutex;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530116 struct hdac_chmap chmap;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530117};
118
Jeeja KP1de777f2017-01-10 17:57:48 +0530119static void hdac_hdmi_enable_cvt(struct hdac_ext_device *edev,
120 struct hdac_hdmi_dai_pin_map *dai_map);
121
122static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
123 struct hdac_hdmi_dai_pin_map *dai_map);
124
Subhransu S. Prusty28890992016-04-14 10:07:34 +0530125static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
126 int pcm_idx)
127{
128 struct hdac_hdmi_pcm *pcm;
129
130 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
131 if (pcm->pcm_id == pcm_idx)
132 return pcm;
133 }
134
135 return NULL;
136}
137
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530138static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
139{
Geliang Tang51b2c422015-12-28 22:47:13 +0800140 struct hdac_device *hdac = dev_to_hdac_dev(dev);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530141
Geliang Tang51b2c422015-12-28 22:47:13 +0800142 return to_ehdac_device(hdac);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530143}
144
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530145static unsigned int sad_format(const u8 *sad)
146{
147 return ((sad[0] >> 0x3) & 0x1f);
148}
149
150static unsigned int sad_sample_bits_lpcm(const u8 *sad)
151{
152 return (sad[2] & 7);
153}
154
155static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
156 void *eld)
157{
158 u64 formats = SNDRV_PCM_FMTBIT_S16;
159 int i;
160 const u8 *sad, *eld_buf = eld;
161
162 sad = drm_eld_sad(eld_buf);
163 if (!sad)
164 goto format_constraint;
165
166 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
167 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
168
169 /*
170 * the controller support 20 and 24 bits in 32 bit
171 * container so we set S32
172 */
173 if (sad_sample_bits_lpcm(sad) & 0x6)
174 formats |= SNDRV_PCM_FMTBIT_S32;
175 }
176 }
177
178format_constraint:
179 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
180 formats);
181
182}
183
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530184static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
185 hda_nid_t cvt_nid, hda_nid_t pin_nid,
186 u32 stream_tag, int format)
187{
188 unsigned int val;
189
190 dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
191 cvt_nid, pin_nid, stream_tag, format);
192
193 val = (stream_tag << 4);
194
195 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
196 AC_VERB_SET_CHANNEL_STREAMID, val);
197 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
198 AC_VERB_SET_STREAM_FORMAT, format);
199
200 return 0;
201}
202
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530203static void
204hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
205 int packet_index, int byte_index)
206{
207 int val;
208
209 val = (packet_index << 5) | (byte_index & 0x1f);
210
211 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
212 AC_VERB_SET_HDMI_DIP_INDEX, val);
213}
214
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530215struct dp_audio_infoframe {
216 u8 type; /* 0x84 */
217 u8 len; /* 0x1b */
218 u8 ver; /* 0x11 << 2 */
219
220 u8 CC02_CT47; /* match with HDMI infoframe from this on */
221 u8 SS01_SF24;
222 u8 CXT04;
223 u8 CA;
224 u8 LFEPBL01_LSV36_DM_INH7;
225};
226
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530227static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
228 hda_nid_t cvt_nid, hda_nid_t pin_nid)
229{
230 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
231 struct hdmi_audio_infoframe frame;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530232 struct dp_audio_infoframe dp_ai;
233 struct hdac_hdmi_priv *hdmi = hdac->private_data;
234 struct hdac_hdmi_pin *pin;
235 u8 *dip;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530236 int ret;
237 int i;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530238 const u8 *eld_buf;
239 u8 conn_type;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530240 int channels, ca;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530241
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530242 list_for_each_entry(pin, &hdmi->pin_list, head) {
243 if (pin->nid == pin_nid)
244 break;
245 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530246
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530247 ca = snd_hdac_channel_allocation(&hdac->hdac, pin->eld.info.spk_alloc,
248 pin->channels, pin->chmap_set, true, pin->chmap);
249
250 channels = snd_hdac_get_active_channels(ca);
251 hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt_nid, channels);
252
253 snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
254 pin->channels, pin->chmap, pin->chmap_set);
255
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530256 eld_buf = pin->eld.eld_buffer;
257 conn_type = drm_eld_get_conn_type(eld_buf);
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530258
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530259 switch (conn_type) {
260 case DRM_ELD_CONN_TYPE_HDMI:
261 hdmi_audio_infoframe_init(&frame);
262
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530263 frame.channels = channels;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530264 frame.channel_allocation = ca;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530265
266 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
267 if (ret < 0)
268 return ret;
269
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530270 break;
271
272 case DRM_ELD_CONN_TYPE_DP:
273 memset(&dp_ai, 0, sizeof(dp_ai));
274 dp_ai.type = 0x84;
275 dp_ai.len = 0x1b;
276 dp_ai.ver = 0x11 << 2;
277 dp_ai.CC02_CT47 = channels - 1;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530278 dp_ai.CA = ca;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530279
280 dip = (u8 *)&dp_ai;
281 break;
282
283 default:
284 dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
285 conn_type);
286 return -EIO;
287 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530288
289 /* stop infoframe transmission */
290 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
291 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
292 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
293
294
295 /* Fill infoframe. Index auto-incremented */
296 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530297 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
Subhransu S. Prusty391005e2016-03-10 09:04:07 +0530298 for (i = 0; i < sizeof(buffer); i++)
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530299 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
Subhransu S. Prusty391005e2016-03-10 09:04:07 +0530300 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530301 } else {
302 for (i = 0; i < sizeof(dp_ai); i++)
303 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
304 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
305 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530306
307 /* Start infoframe */
308 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
309 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
310 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
311
312 return 0;
313}
314
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530315static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
316 struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
317{
318 /* Power up pin widget */
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530319 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
320 pwr_state))
321 snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530322 AC_VERB_SET_POWER_STATE, pwr_state);
323
324 /* Power up converter */
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530325 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
326 pwr_state))
327 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530328 AC_VERB_SET_POWER_STATE, pwr_state);
329}
330
331static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
332 struct snd_soc_dai *dai)
333{
334 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
335 struct hdac_hdmi_priv *hdmi = hdac->private_data;
336 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530337 struct hdac_hdmi_pin *pin;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530338 struct hdac_ext_dma_params *dd;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530339 int ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530340
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530341 dai_map = &hdmi->dai_map[dai->id];
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530342 pin = dai_map->pin;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530343
344 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
345 dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
346 dd->stream_tag, dd->format);
347
Jeeja KP1de777f2017-01-10 17:57:48 +0530348 hdac_hdmi_enable_cvt(hdac, dai_map);
349 ret = hdac_hdmi_enable_pin(hdac, dai_map);
350 if (ret < 0)
351 return ret;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530352 mutex_lock(&pin->lock);
353 pin->channels = substream->runtime->channels;
354
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530355 ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
356 dai_map->pin->nid);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530357 mutex_unlock(&pin->lock);
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530358 if (ret < 0)
359 return ret;
360
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530361 return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
362 dai_map->pin->nid, dd->stream_tag, dd->format);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530363}
364
365static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
366 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
367{
368 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530369 struct hdac_hdmi_priv *hdmi = hdac->private_data;
370 struct hdac_hdmi_dai_pin_map *dai_map;
371 struct hdac_hdmi_pin *pin;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530372 struct hdac_ext_dma_params *dd;
373
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530374 dai_map = &hdmi->dai_map[dai->id];
375 pin = dai_map->pin;
376
377 if (!pin)
378 return -ENODEV;
379
380 if ((!pin->eld.monitor_present) || (!pin->eld.eld_valid)) {
381 dev_err(&hdac->hdac.dev, "device is not configured for this pin: %d\n",
382 pin->nid);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530383 return -ENODEV;
384 }
385
Subhransu S. Prusty6793a3d72016-02-17 21:33:59 +0530386 dd = snd_soc_dai_get_dma_data(dai, substream);
387 if (!dd) {
388 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
389 if (!dd)
390 return -ENOMEM;
391 }
392
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530393 dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
394 params_channels(hparams), params_format(hparams),
395 24, 0);
396
397 snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
398
399 return 0;
400}
401
402static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
403 struct snd_soc_dai *dai)
404{
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530405 struct hdac_ext_dma_params *dd;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530406
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530407 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530408
Subhransu S. Prusty6793a3d72016-02-17 21:33:59 +0530409 if (dd) {
410 snd_soc_dai_set_dma_data(dai, substream, NULL);
411 kfree(dd);
412 }
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530413
414 return 0;
415}
416
Subhransu S. Prustyab85f5b2016-02-17 21:34:02 +0530417static void hdac_hdmi_enable_cvt(struct hdac_ext_device *edev,
418 struct hdac_hdmi_dai_pin_map *dai_map)
419{
420 /* Enable transmission */
421 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
422 AC_VERB_SET_DIGI_CONVERT_1, 1);
423
424 /* Category Code (CC) to zero */
425 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
426 AC_VERB_SET_DIGI_CONVERT_2, 0);
427}
428
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530429static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
430 struct hdac_hdmi_dai_pin_map *dai_map)
431{
432 int mux_idx;
433 struct hdac_hdmi_pin *pin = dai_map->pin;
434
435 for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
436 if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
437 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
438 AC_VERB_SET_CONNECT_SEL, mux_idx);
439 break;
440 }
441 }
442
443 if (mux_idx == pin->num_mux_nids)
444 return -EIO;
445
446 /* Enable out path for this pin widget */
447 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
448 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
449
450 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
451
452 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
453 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
454
455 return 0;
456}
457
458static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
459 struct hdac_hdmi_pin *pin)
460{
461 if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
462 dev_warn(&hdac->hdac.dev,
463 "HDMI: pin %d wcaps %#x does not support connection list\n",
464 pin->nid, get_wcaps(&hdac->hdac, pin->nid));
465 return -EINVAL;
466 }
467
468 pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
469 pin->mux_nids, HDA_MAX_CONNECTIONS);
470 if (pin->num_mux_nids == 0)
471 dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
472 pin->nid);
473
474 dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
475 pin->num_mux_nids, pin->nid);
476
477 return pin->num_mux_nids;
478}
479
480/*
481 * Query pcm list and return pin widget to which stream is routed.
482 *
483 * Also query connection list of the pin, to validate the cvt to pin map.
484 *
485 * Same stream rendering to multiple pins simultaneously can be done
486 * possibly, but not supported for now in driver. So return the first pin
487 * connected.
488 */
489static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
490 struct hdac_ext_device *edev,
491 struct hdac_hdmi_priv *hdmi,
492 struct hdac_hdmi_cvt *cvt)
493{
494 struct hdac_hdmi_pcm *pcm;
495 struct hdac_hdmi_pin *pin = NULL;
496 int ret, i;
497
498 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
499 if (pcm->cvt == cvt) {
500 pin = pcm->pin;
501 break;
502 }
503 }
504
505 if (pin) {
506 ret = hdac_hdmi_query_pin_connlist(edev, pin);
507 if (ret < 0)
508 return NULL;
509
510 for (i = 0; i < pin->num_mux_nids; i++) {
511 if (pin->mux_nids[i] == cvt->nid)
512 return pin;
513 }
514 }
515
516 return NULL;
517}
518
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530519/*
520 * This tries to get a valid pin and set the HW constraints based on the
521 * ELD. Even if a valid pin is not found return success so that device open
522 * doesn't fail.
523 */
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530524static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
525 struct snd_soc_dai *dai)
526{
527 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
528 struct hdac_hdmi_priv *hdmi = hdac->private_data;
529 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530530 struct hdac_hdmi_cvt *cvt;
531 struct hdac_hdmi_pin *pin;
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530532 int ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530533
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530534 dai_map = &hdmi->dai_map[dai->id];
535
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530536 cvt = dai_map->cvt;
537 pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530538
539 /*
540 * To make PA and other userland happy.
541 * userland scans devices so returning error does not help.
542 */
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530543 if (!pin)
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530544 return 0;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530545
546 if ((!pin->eld.monitor_present) ||
547 (!pin->eld.eld_valid)) {
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530548
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530549 dev_warn(&hdac->hdac.dev,
Colin Ian King3fb7b4e2016-08-26 19:17:25 +0100550 "Failed: monitor present? %d ELD valid?: %d for pin: %d\n",
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530551 pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530552
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530553 return 0;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530554 }
555
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530556 dai_map->pin = pin;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530557
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530558 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530559 pin->eld.eld_buffer);
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530560 if (ret < 0)
561 return ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530562
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530563 return snd_pcm_hw_constraint_eld(substream->runtime,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530564 pin->eld.eld_buffer);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530565}
566
Jeeja KP571d5072016-02-22 07:50:33 +0530567static int hdac_hdmi_trigger(struct snd_pcm_substream *substream, int cmd,
568 struct snd_soc_dai *dai)
569{
Jeeja KP571d5072016-02-22 07:50:33 +0530570
Jeeja KP1de777f2017-01-10 17:57:48 +0530571 switch (cmd) {
572 case SNDRV_PCM_TRIGGER_RESUME:
573 case SNDRV_PCM_TRIGGER_START:
574 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
Jeeja KP571d5072016-02-22 07:50:33 +0530575 return hdac_hdmi_playback_prepare(substream, dai);
Jeeja KP1de777f2017-01-10 17:57:48 +0530576
577 default:
578 return 0;
Jeeja KP571d5072016-02-22 07:50:33 +0530579 }
580
581 return 0;
582}
583
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530584static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
585 struct snd_soc_dai *dai)
586{
587 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
588 struct hdac_hdmi_priv *hdmi = hdac->private_data;
589 struct hdac_hdmi_dai_pin_map *dai_map;
590
591 dai_map = &hdmi->dai_map[dai->id];
592
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530593 if (dai_map->pin) {
594 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
595 AC_VERB_SET_CHANNEL_STREAMID, 0);
596 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
597 AC_VERB_SET_STREAM_FORMAT, 0);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530598
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530599 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
600
601 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530602 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530603
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530604 mutex_lock(&dai_map->pin->lock);
Subhransu S. Prusty28890992016-04-14 10:07:34 +0530605 dai_map->pin->chmap_set = false;
606 memset(dai_map->pin->chmap, 0, sizeof(dai_map->pin->chmap));
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530607 dai_map->pin->channels = 0;
608 mutex_unlock(&dai_map->pin->lock);
609
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530610 dai_map->pin = NULL;
611 }
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530612}
613
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530614static int
615hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
616{
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530617 unsigned int chans;
618 struct hdac_ext_device *edev = to_ehdac_device(hdac);
619 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530620 int err;
621
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530622 chans = get_wcaps(hdac, cvt->nid);
623 chans = get_wcaps_channels(chans);
624
625 cvt->params.channels_min = 2;
626
627 cvt->params.channels_max = chans;
628 if (chans > hdmi->chmap.channels_max)
629 hdmi->chmap.channels_max = chans;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530630
631 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
632 &cvt->params.rates,
633 &cvt->params.formats,
634 &cvt->params.maxbps);
635 if (err < 0)
636 dev_err(&hdac->dev,
637 "Failed to query pcm params for nid %d: %d\n",
638 cvt->nid, err);
639
640 return err;
641}
642
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530643static int hdac_hdmi_fill_widget_info(struct device *dev,
644 struct snd_soc_dapm_widget *w,
645 enum snd_soc_dapm_type id, void *priv,
646 const char *wname, const char *stream,
647 struct snd_kcontrol_new *wc, int numkc)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530648{
649 w->id = id;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530650 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
651 if (!w->name)
652 return -ENOMEM;
653
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530654 w->sname = stream;
655 w->reg = SND_SOC_NOPM;
656 w->shift = 0;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530657 w->kcontrol_news = wc;
658 w->num_kcontrols = numkc;
659 w->priv = priv;
660
661 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530662}
663
664static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530665 const char *sink, const char *control, const char *src,
666 int (*handler)(struct snd_soc_dapm_widget *src,
667 struct snd_soc_dapm_widget *sink))
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530668{
669 route->sink = sink;
670 route->source = src;
671 route->control = control;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530672 route->connected = handler;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530673}
674
Jeeja KP4a3478d2016-02-12 07:46:06 +0530675static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
676 struct hdac_hdmi_pin *pin)
677{
678 struct hdac_hdmi_priv *hdmi = edev->private_data;
679 struct hdac_hdmi_pcm *pcm = NULL;
680
681 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
682 if (pcm->pin == pin)
683 return pcm;
684 }
685
686 return NULL;
687}
688
689/*
690 * Based on user selection, map the PINs with the PCMs.
691 */
692static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
693 struct snd_ctl_elem_value *ucontrol)
694{
695 int ret;
696 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
697 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
698 struct snd_soc_dapm_context *dapm = w->dapm;
699 struct hdac_hdmi_pin *pin = w->priv;
700 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
701 struct hdac_hdmi_priv *hdmi = edev->private_data;
702 struct hdac_hdmi_pcm *pcm = NULL;
703 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
704
705 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
706 if (ret < 0)
707 return ret;
708
709 mutex_lock(&hdmi->pin_mutex);
710 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
711 if (pcm->pin == pin)
712 pcm->pin = NULL;
713
714 /*
715 * Jack status is not reported during device probe as the
716 * PCMs are not registered by then. So report it here.
717 */
718 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
719 pcm->pin = pin;
720 if (pin->eld.monitor_present && pin->eld.eld_valid) {
721 dev_dbg(&edev->hdac.dev,
722 "jack report for pcm=%d\n",
723 pcm->pcm_id);
724
725 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
726 }
727 mutex_unlock(&hdmi->pin_mutex);
728 return ret;
729 }
730 }
731 mutex_unlock(&hdmi->pin_mutex);
732
733 return ret;
734}
735
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530736/*
737 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
738 * the display driver seem to be programming the connection list for the pin
739 * widget runtime.
740 *
741 * So programming all the possible inputs for the mux, the user has to take
742 * care of selecting the right one and leaving all other inputs selected to
743 * "NONE"
744 */
745static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
746 struct hdac_hdmi_pin *pin,
747 struct snd_soc_dapm_widget *widget,
748 const char *widget_name)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530749{
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530750 struct hdac_hdmi_priv *hdmi = edev->private_data;
751 struct snd_kcontrol_new *kc;
752 struct hdac_hdmi_cvt *cvt;
753 struct soc_enum *se;
754 char kc_name[NAME_SIZE];
755 char mux_items[NAME_SIZE];
756 /* To hold inputs to the Pin mux */
757 char *items[HDA_MAX_CONNECTIONS];
758 int i = 0;
759 int num_items = hdmi->num_cvt + 1;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530760
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530761 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
762 if (!kc)
763 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530764
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530765 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
766 if (!se)
767 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530768
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530769 sprintf(kc_name, "Pin %d Input", pin->nid);
770 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
771 if (!kc->name)
772 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530773
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530774 kc->private_value = (long)se;
775 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
776 kc->access = 0;
777 kc->info = snd_soc_info_enum_double;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530778 kc->put = hdac_hdmi_set_pin_mux;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530779 kc->get = snd_soc_dapm_get_enum_double;
780
781 se->reg = SND_SOC_NOPM;
782
783 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
784 se->items = num_items;
785 se->mask = roundup_pow_of_two(se->items) - 1;
786
787 sprintf(mux_items, "NONE");
788 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
789 if (!items[i])
790 return -ENOMEM;
791
792 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
793 i++;
794 sprintf(mux_items, "cvt %d", cvt->nid);
795 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
796 if (!items[i])
797 return -ENOMEM;
798 }
799
800 se->texts = devm_kmemdup(&edev->hdac.dev, items,
801 (num_items * sizeof(char *)), GFP_KERNEL);
802 if (!se->texts)
803 return -ENOMEM;
804
805 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
Jeeja KP4a3478d2016-02-12 07:46:06 +0530806 snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530807}
808
809/* Add cvt <- input <- mux route map */
810static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
811 struct snd_soc_dapm_widget *widgets,
812 struct snd_soc_dapm_route *route, int rindex)
813{
814 struct hdac_hdmi_priv *hdmi = edev->private_data;
815 const struct snd_kcontrol_new *kc;
816 struct soc_enum *se;
817 int mux_index = hdmi->num_cvt + hdmi->num_pin;
818 int i, j;
819
820 for (i = 0; i < hdmi->num_pin; i++) {
821 kc = widgets[mux_index].kcontrol_news;
822 se = (struct soc_enum *)kc->private_value;
823 for (j = 0; j < hdmi->num_cvt; j++) {
824 hdac_hdmi_fill_route(&route[rindex],
825 widgets[mux_index].name,
826 se->texts[j + 1],
827 widgets[j].name, NULL);
828
829 rindex++;
830 }
831
832 mux_index++;
833 }
834}
835
836/*
837 * Widgets are added in the below sequence
838 * Converter widgets for num converters enumerated
839 * Pin widgets for num pins enumerated
840 * Pin mux widgets to represent connenction list of pin widget
841 *
842 * Total widgets elements = num_cvt + num_pin + num_pin;
843 *
844 * Routes are added as below:
845 * pin mux -> pin (based on num_pins)
846 * cvt -> "Input sel control" -> pin_mux
847 *
848 * Total route elements:
849 * num_pins + (pin_muxes * num_cvt)
850 */
851static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
852{
853 struct snd_soc_dapm_widget *widgets;
854 struct snd_soc_dapm_route *route;
855 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
856 struct hdac_hdmi_priv *hdmi = edev->private_data;
857 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
858 char widget_name[NAME_SIZE];
859 struct hdac_hdmi_cvt *cvt;
860 struct hdac_hdmi_pin *pin;
861 int ret, i = 0, num_routes = 0;
862
863 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
864 return -EINVAL;
865
866 widgets = devm_kzalloc(dapm->dev,
867 (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
868 GFP_KERNEL);
869
870 if (!widgets)
871 return -ENOMEM;
872
873 /* DAPM widgets to represent each converter widget */
874 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
875 sprintf(widget_name, "Converter %d", cvt->nid);
876 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
877 snd_soc_dapm_aif_in, &cvt->nid,
878 widget_name, dai_drv[i].playback.stream_name, NULL, 0);
879 if (ret < 0)
880 return ret;
881 i++;
882 }
883
884 list_for_each_entry(pin, &hdmi->pin_list, head) {
885 sprintf(widget_name, "hif%d Output", pin->nid);
886 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
887 snd_soc_dapm_output, &pin->nid,
888 widget_name, NULL, NULL, 0);
889 if (ret < 0)
890 return ret;
891 i++;
892 }
893
894 /* DAPM widgets to represent the connection list to pin widget */
895 list_for_each_entry(pin, &hdmi->pin_list, head) {
896 sprintf(widget_name, "Pin %d Mux", pin->nid);
897 ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
898 widget_name);
899 if (ret < 0)
900 return ret;
901 i++;
902
903 /* For cvt to pin_mux mapping */
904 num_routes += hdmi->num_cvt;
905
906 /* For pin_mux to pin mapping */
907 num_routes++;
908 }
909
910 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
911 GFP_KERNEL);
912 if (!route)
913 return -ENOMEM;
914
915 i = 0;
916 /* Add pin <- NULL <- mux route map */
917 list_for_each_entry(pin, &hdmi->pin_list, head) {
918 int sink_index = i + hdmi->num_cvt;
919 int src_index = sink_index + hdmi->num_pin;
920
921 hdac_hdmi_fill_route(&route[i],
922 widgets[sink_index].name, NULL,
923 widgets[src_index].name, NULL);
924 i++;
925
926 }
927
928 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
929
930 snd_soc_dapm_new_controls(dapm, widgets,
931 ((2 * hdmi->num_pin) + hdmi->num_cvt));
932
933 snd_soc_dapm_add_routes(dapm, route, num_routes);
934 snd_soc_dapm_new_widgets(dapm->card);
935
936 return 0;
937
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530938}
939
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530940static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530941{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530942 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530943 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530944 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530945 int dai_id = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530946
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530947 if (list_empty(&hdmi->cvt_list))
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530948 return -EINVAL;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530949
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530950 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
951 dai_map = &hdmi->dai_map[dai_id];
952 dai_map->dai_id = dai_id;
953 dai_map->cvt = cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530954
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530955 dai_id++;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530956
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530957 if (dai_id == HDA_MAX_CVTS) {
958 dev_warn(&edev->hdac.dev,
959 "Max dais supported: %d\n", dai_id);
960 break;
961 }
962 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530963
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530964 return 0;
965}
966
967static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
968{
969 struct hdac_hdmi_priv *hdmi = edev->private_data;
970 struct hdac_hdmi_cvt *cvt;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530971 char name[NAME_SIZE];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530972
973 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
974 if (!cvt)
975 return -ENOMEM;
976
977 cvt->nid = nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530978 sprintf(name, "cvt %d", cvt->nid);
979 cvt->name = kstrdup(name, GFP_KERNEL);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530980
981 list_add_tail(&cvt->head, &hdmi->cvt_list);
982 hdmi->num_cvt++;
983
984 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
985}
986
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +0530987static int hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +0530988 struct hdac_hdmi_pin *pin)
989{
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +0530990 unsigned int ver, mnl;
991
992 ver = (pin->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
993 >> DRM_ELD_VER_SHIFT;
994
995 if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
996 dev_err(&edev->hdac.dev, "HDMI: Unknown ELD version %d\n", ver);
997 return -EINVAL;
998 }
999
1000 mnl = (pin->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1001 DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1002
1003 if (mnl > ELD_MAX_MNL) {
1004 dev_err(&edev->hdac.dev, "HDMI: MNL Invalid %d\n", mnl);
1005 return -EINVAL;
1006 }
1007
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301008 pin->eld.info.spk_alloc = pin->eld.eld_buffer[DRM_ELD_SPEAKER];
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301009
1010 return 0;
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301011}
1012
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301013static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin)
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301014{
1015 struct hdac_ext_device *edev = pin->edev;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301016 struct hdac_hdmi_priv *hdmi = edev->private_data;
1017 struct hdac_hdmi_pcm *pcm;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301018 int size;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301019
1020 mutex_lock(&hdmi->pin_mutex);
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301021 pin->eld.monitor_present = false;
1022
1023 size = snd_hdac_acomp_get_eld(&edev->hdac, pin->nid, -1,
1024 &pin->eld.monitor_present, pin->eld.eld_buffer,
1025 ELD_MAX_SIZE);
1026
1027 if (size > 0) {
1028 size = min(size, ELD_MAX_SIZE);
1029 if (hdac_hdmi_parse_eld(edev, pin) < 0)
1030 size = -EINVAL;
1031 }
1032
1033 if (size > 0) {
1034 pin->eld.eld_valid = true;
1035 pin->eld.eld_size = size;
1036 } else {
1037 pin->eld.eld_valid = false;
1038 pin->eld.eld_size = 0;
1039 }
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301040
Jeeja KP4a3478d2016-02-12 07:46:06 +05301041 pcm = hdac_hdmi_get_pcm(edev, pin);
1042
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301043 if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
1044
1045 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
1046 __func__, pin->nid);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301047
1048 /*
1049 * PCMs are not registered during device probe, so don't
1050 * report jack here. It will be done in usermode mux
1051 * control select.
1052 */
1053 if (pcm) {
1054 dev_dbg(&edev->hdac.dev,
1055 "jack report for pcm=%d\n", pcm->pcm_id);
1056
1057 snd_jack_report(pcm->jack, 0);
1058 }
1059
1060 mutex_unlock(&hdmi->pin_mutex);
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301061 return;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301062 }
1063
1064 if (pin->eld.monitor_present && pin->eld.eld_valid) {
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301065 if (pcm) {
1066 dev_dbg(&edev->hdac.dev,
1067 "jack report for pcm=%d\n",
1068 pcm->pcm_id);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301069
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301070 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301071 }
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301072
1073 print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1074 pin->eld.eld_buffer, pin->eld.eld_size, false);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301075 }
1076
Jeeja KP4a3478d2016-02-12 07:46:06 +05301077 mutex_unlock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301078}
1079
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301080static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1081{
1082 struct hdac_hdmi_priv *hdmi = edev->private_data;
1083 struct hdac_hdmi_pin *pin;
1084
1085 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1086 if (!pin)
1087 return -ENOMEM;
1088
1089 pin->nid = nid;
1090
1091 list_add_tail(&pin->head, &hdmi->pin_list);
1092 hdmi->num_pin++;
1093
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301094 pin->edev = edev;
Subhransu S. Prustybcced702016-04-14 10:07:30 +05301095 mutex_init(&pin->lock);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301096
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301097 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301098}
1099
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301100#define INTEL_VENDOR_NID 0x08
1101#define INTEL_GET_VENDOR_VERB 0xf81
1102#define INTEL_SET_VENDOR_VERB 0x781
1103#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1104#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1105
1106static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1107{
1108 unsigned int vendor_param;
1109
1110 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1111 INTEL_GET_VENDOR_VERB, 0);
1112 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1113 return;
1114
1115 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1116 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1117 INTEL_SET_VENDOR_VERB, vendor_param);
1118 if (vendor_param == -1)
1119 return;
1120}
1121
1122static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1123{
1124 unsigned int vendor_param;
1125
1126 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1127 INTEL_GET_VENDOR_VERB, 0);
1128 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1129 return;
1130
1131 /* enable DP1.2 mode */
1132 vendor_param |= INTEL_EN_DP12;
1133 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1134 INTEL_SET_VENDOR_VERB, vendor_param);
1135 if (vendor_param == -1)
1136 return;
1137
1138}
1139
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301140static struct snd_soc_dai_ops hdmi_dai_ops = {
1141 .startup = hdac_hdmi_pcm_open,
1142 .shutdown = hdac_hdmi_pcm_close,
1143 .hw_params = hdac_hdmi_set_hw_params,
1144 .prepare = hdac_hdmi_playback_prepare,
Jeeja KP571d5072016-02-22 07:50:33 +05301145 .trigger = hdac_hdmi_trigger,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301146 .hw_free = hdac_hdmi_playback_cleanup,
1147};
1148
1149/*
1150 * Each converter can support a stream independently. So a dai is created
1151 * based on the number of converter queried.
1152 */
1153static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1154 struct snd_soc_dai_driver **dais,
1155 struct hdac_hdmi_priv *hdmi, int num_dais)
1156{
1157 struct snd_soc_dai_driver *hdmi_dais;
1158 struct hdac_hdmi_cvt *cvt;
1159 char name[NAME_SIZE], dai_name[NAME_SIZE];
1160 int i = 0;
1161 u32 rates, bps;
1162 unsigned int rate_max = 384000, rate_min = 8000;
1163 u64 formats;
1164 int ret;
1165
1166 hdmi_dais = devm_kzalloc(&hdac->dev,
1167 (sizeof(*hdmi_dais) * num_dais),
1168 GFP_KERNEL);
1169 if (!hdmi_dais)
1170 return -ENOMEM;
1171
1172 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1173 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1174 &rates, &formats, &bps);
1175 if (ret)
1176 return ret;
1177
1178 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1179 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1180 dai_name, GFP_KERNEL);
1181
1182 if (!hdmi_dais[i].name)
1183 return -ENOMEM;
1184
1185 snprintf(name, sizeof(name), "hifi%d", i+1);
1186 hdmi_dais[i].playback.stream_name =
1187 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1188 if (!hdmi_dais[i].playback.stream_name)
1189 return -ENOMEM;
1190
1191 /*
1192 * Set caps based on capability queried from the converter.
1193 * It will be constrained runtime based on ELD queried.
1194 */
1195 hdmi_dais[i].playback.formats = formats;
1196 hdmi_dais[i].playback.rates = rates;
1197 hdmi_dais[i].playback.rate_max = rate_max;
1198 hdmi_dais[i].playback.rate_min = rate_min;
1199 hdmi_dais[i].playback.channels_min = 2;
1200 hdmi_dais[i].playback.channels_max = 2;
1201 hdmi_dais[i].ops = &hdmi_dai_ops;
1202
1203 i++;
1204 }
1205
1206 *dais = hdmi_dais;
1207
1208 return 0;
1209}
1210
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301211/*
1212 * Parse all nodes and store the cvt/pin nids in array
1213 * Add one time initialization for pin and cvt widgets
1214 */
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301215static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1216 struct snd_soc_dai_driver **dais, int *num_dais)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301217{
1218 hda_nid_t nid;
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301219 int i, num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301220 struct hdac_device *hdac = &edev->hdac;
1221 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301222 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301223
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301224 hdac_hdmi_skl_enable_all_pins(hdac);
1225 hdac_hdmi_skl_enable_dp12(hdac);
1226
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301227 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
Subhransu S. Prusty541140d2015-12-09 21:46:08 +05301228 if (!nid || num_nodes <= 0) {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301229 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1230 return -EINVAL;
1231 }
1232
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301233 hdac->num_nodes = num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301234 hdac->start_nid = nid;
1235
1236 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1237 unsigned int caps;
1238 unsigned int type;
1239
1240 caps = get_wcaps(hdac, nid);
1241 type = get_wcaps_type(caps);
1242
1243 if (!(caps & AC_WCAP_DIGITAL))
1244 continue;
1245
1246 switch (type) {
1247
1248 case AC_WID_AUD_OUT:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301249 ret = hdac_hdmi_add_cvt(edev, nid);
1250 if (ret < 0)
1251 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301252 break;
1253
1254 case AC_WID_PIN:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301255 ret = hdac_hdmi_add_pin(edev, nid);
1256 if (ret < 0)
1257 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301258 break;
1259 }
1260 }
1261
1262 hdac->end_nid = nid;
1263
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301264 if (!hdmi->num_pin || !hdmi->num_cvt)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301265 return -EIO;
1266
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301267 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1268 if (ret) {
1269 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1270 ret);
1271 return ret;
1272 }
1273
1274 *num_dais = hdmi->num_cvt;
1275
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301276 return hdac_hdmi_init_dai_map(edev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301277}
1278
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301279static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1280{
1281 struct hdac_ext_device *edev = aptr;
1282 struct hdac_hdmi_priv *hdmi = edev->private_data;
1283 struct hdac_hdmi_pin *pin;
1284 struct snd_soc_codec *codec = edev->scodec;
1285
1286 /* Don't know how this mapping is derived */
1287 hda_nid_t pin_nid = port + 0x04;
1288
1289 dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1290
1291 /*
1292 * skip notification during system suspend (but not in runtime PM);
1293 * the state will be updated at resume. Also since the ELD and
1294 * connection states are updated in anyway at the end of the resume,
1295 * we can skip it when received during PM process.
1296 */
1297 if (snd_power_get_state(codec->component.card->snd_card) !=
1298 SNDRV_CTL_POWER_D0)
1299 return;
1300
1301 if (atomic_read(&edev->hdac.in_pm))
1302 return;
1303
1304 list_for_each_entry(pin, &hdmi->pin_list, head) {
1305 if (pin->nid == pin_nid)
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301306 hdac_hdmi_present_sense(pin);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301307 }
1308}
1309
1310static struct i915_audio_component_audio_ops aops = {
1311 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1312};
1313
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301314static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
1315 int device)
1316{
1317 struct snd_soc_pcm_runtime *rtd;
1318
1319 list_for_each_entry(rtd, &card->rtd_list, list) {
1320 if (rtd->pcm && (rtd->pcm->device == device))
1321 return rtd->pcm;
1322 }
1323
1324 return NULL;
1325}
1326
Jeeja KP4a3478d2016-02-12 07:46:06 +05301327int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1328{
1329 char jack_name[NAME_SIZE];
1330 struct snd_soc_codec *codec = dai->codec;
1331 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1332 struct snd_soc_dapm_context *dapm =
1333 snd_soc_component_get_dapm(&codec->component);
1334 struct hdac_hdmi_priv *hdmi = edev->private_data;
1335 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301336 struct snd_pcm *snd_pcm;
1337 int err;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301338
1339 /*
1340 * this is a new PCM device, create new pcm and
1341 * add to the pcm list
1342 */
1343 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1344 if (!pcm)
1345 return -ENOMEM;
1346 pcm->pcm_id = device;
1347 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1348
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301349 snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
1350 if (snd_pcm) {
1351 err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
1352 if (err < 0) {
1353 dev_err(&edev->hdac.dev,
1354 "chmap control add failed with err: %d for pcm: %d\n",
1355 err, device);
1356 kfree(pcm);
1357 return err;
1358 }
1359 }
1360
Jeeja KP4a3478d2016-02-12 07:46:06 +05301361 list_add_tail(&pcm->head, &hdmi->pcm_list);
1362
1363 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1364
1365 return snd_jack_new(dapm->card->snd_card, jack_name,
1366 SND_JACK_AVOUT, &pcm->jack, true, false);
1367}
1368EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1369
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301370static int hdmi_codec_probe(struct snd_soc_codec *codec)
1371{
1372 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1373 struct hdac_hdmi_priv *hdmi = edev->private_data;
1374 struct snd_soc_dapm_context *dapm =
1375 snd_soc_component_get_dapm(&codec->component);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301376 struct hdac_hdmi_pin *pin;
Vinod Koulb2047e92016-05-12 08:58:55 +05301377 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301378 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301379
1380 edev->scodec = codec;
1381
Vinod Koulb2047e92016-05-12 08:58:55 +05301382 /*
1383 * hold the ref while we probe, also no need to drop the ref on
1384 * exit, we call pm_runtime_suspend() so that will do for us
1385 */
1386 hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301387 if (!hlink) {
1388 dev_err(&edev->hdac.dev, "hdac link not found\n");
1389 return -EIO;
1390 }
1391
Vinod Koulb2047e92016-05-12 08:58:55 +05301392 snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1393
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301394 ret = create_fill_widget_route_map(dapm);
1395 if (ret < 0)
1396 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301397
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301398 aops.audio_ptr = edev;
1399 ret = snd_hdac_i915_register_notifier(&aops);
1400 if (ret < 0) {
1401 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1402 ret);
1403 return ret;
1404 }
1405
1406 list_for_each_entry(pin, &hdmi->pin_list, head)
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301407 hdac_hdmi_present_sense(pin);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301408
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301409 /* Imp: Store the card pointer in hda_codec */
1410 edev->card = dapm->card->snd_card;
1411
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301412 /*
1413 * hdac_device core already sets the state to active and calls
1414 * get_noresume. So enable runtime and set the device to suspend.
1415 */
1416 pm_runtime_enable(&edev->hdac.dev);
1417 pm_runtime_put(&edev->hdac.dev);
1418 pm_runtime_suspend(&edev->hdac.dev);
1419
1420 return 0;
1421}
1422
1423static int hdmi_codec_remove(struct snd_soc_codec *codec)
1424{
1425 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1426
1427 pm_runtime_disable(&edev->hdac.dev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301428 return 0;
1429}
1430
Jeeja KP571d5072016-02-22 07:50:33 +05301431#ifdef CONFIG_PM
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301432static int hdmi_codec_prepare(struct device *dev)
1433{
1434 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1435 struct hdac_device *hdac = &edev->hdac;
1436
1437 pm_runtime_get_sync(&edev->hdac.dev);
1438
1439 /*
1440 * Power down afg.
1441 * codec_read is preferred over codec_write to set the power state.
1442 * This way verb is send to set the power state and response
1443 * is received. So setting power state is ensured without using loop
1444 * to read the state.
1445 */
1446 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1447 AC_PWRST_D3);
1448
1449 return 0;
1450}
1451
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301452static void hdmi_codec_complete(struct device *dev)
Jeeja KP571d5072016-02-22 07:50:33 +05301453{
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301454 struct hdac_ext_device *edev = to_hda_ext_device(dev);
Jeeja KP571d5072016-02-22 07:50:33 +05301455 struct hdac_hdmi_priv *hdmi = edev->private_data;
1456 struct hdac_hdmi_pin *pin;
1457 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301458
1459 /* Power up afg */
1460 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1461 AC_PWRST_D0);
Jeeja KP571d5072016-02-22 07:50:33 +05301462
1463 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1464 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1465
Jeeja KP571d5072016-02-22 07:50:33 +05301466 /*
1467 * As the ELD notify callback request is not entertained while the
1468 * device is in suspend state. Need to manually check detection of
1469 * all pins here.
1470 */
1471 list_for_each_entry(pin, &hdmi->pin_list, head)
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301472 hdac_hdmi_present_sense(pin);
Jeeja KP571d5072016-02-22 07:50:33 +05301473
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301474 pm_runtime_put_sync(&edev->hdac.dev);
Jeeja KP571d5072016-02-22 07:50:33 +05301475}
1476#else
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301477#define hdmi_codec_prepare NULL
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301478#define hdmi_codec_complete NULL
Jeeja KP571d5072016-02-22 07:50:33 +05301479#endif
1480
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301481static struct snd_soc_codec_driver hdmi_hda_codec = {
1482 .probe = hdmi_codec_probe,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301483 .remove = hdmi_codec_remove,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301484 .idle_bias_off = true,
1485};
1486
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301487static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
1488 unsigned char *chmap)
1489{
1490 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1491 struct hdac_hdmi_priv *hdmi = edev->private_data;
1492 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1493 struct hdac_hdmi_pin *pin = pcm->pin;
1494
1495 /* chmap is already set to 0 in caller */
1496 if (!pin)
1497 return;
1498
1499 memcpy(chmap, pin->chmap, ARRAY_SIZE(pin->chmap));
1500}
1501
1502static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
1503 unsigned char *chmap, int prepared)
1504{
1505 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1506 struct hdac_hdmi_priv *hdmi = edev->private_data;
1507 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1508 struct hdac_hdmi_pin *pin = pcm->pin;
1509
1510 mutex_lock(&pin->lock);
1511 pin->chmap_set = true;
1512 memcpy(pin->chmap, chmap, ARRAY_SIZE(pin->chmap));
1513 if (prepared)
1514 hdac_hdmi_setup_audio_infoframe(edev, pcm->cvt->nid, pin->nid);
1515 mutex_unlock(&pin->lock);
1516}
1517
1518static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
1519{
1520 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1521 struct hdac_hdmi_priv *hdmi = edev->private_data;
1522 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1523 struct hdac_hdmi_pin *pin = pcm->pin;
1524
1525 return pin ? true:false;
1526}
1527
1528static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
1529{
1530 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1531 struct hdac_hdmi_priv *hdmi = edev->private_data;
1532 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1533 struct hdac_hdmi_pin *pin = pcm->pin;
1534
Dan Carpenter8f658812016-05-03 10:42:58 +03001535 if (!pin || !pin->eld.eld_valid)
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301536 return 0;
1537
1538 return pin->eld.info.spk_alloc;
1539}
1540
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301541static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1542{
1543 struct hdac_device *codec = &edev->hdac;
1544 struct hdac_hdmi_priv *hdmi_priv;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301545 struct snd_soc_dai_driver *hdmi_dais = NULL;
Vinod Koulb2047e92016-05-12 08:58:55 +05301546 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301547 int num_dais = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301548 int ret = 0;
1549
Vinod Koulb2047e92016-05-12 08:58:55 +05301550 /* hold the ref while we probe */
1551 hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301552 if (!hlink) {
1553 dev_err(&edev->hdac.dev, "hdac link not found\n");
1554 return -EIO;
1555 }
1556
Vinod Koulb2047e92016-05-12 08:58:55 +05301557 snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1558
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301559 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1560 if (hdmi_priv == NULL)
1561 return -ENOMEM;
1562
1563 edev->private_data = hdmi_priv;
Subhransu S. Prustybcced702016-04-14 10:07:30 +05301564 snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301565 hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
1566 hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
1567 hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
1568 hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301569
1570 dev_set_drvdata(&codec->dev, edev);
1571
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301572 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1573 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301574 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1575 mutex_init(&hdmi_priv->pin_mutex);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301576
Ramesh Babuaeaccef2016-02-17 21:34:01 +05301577 /*
1578 * Turned off in the runtime_suspend during the first explicit
1579 * pm_runtime_suspend call.
1580 */
1581 ret = snd_hdac_display_power(edev->hdac.bus, true);
1582 if (ret < 0) {
1583 dev_err(&edev->hdac.dev,
1584 "Cannot turn on display power on i915 err: %d\n",
1585 ret);
1586 return ret;
1587 }
1588
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301589 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1590 if (ret < 0) {
1591 dev_err(&codec->dev,
1592 "Failed in parse and map nid with err: %d\n", ret);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301593 return ret;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301594 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301595
1596 /* ASoC specific initialization */
Vinod Koulb2047e92016-05-12 08:58:55 +05301597 ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
1598 hdmi_dais, num_dais);
1599
1600 snd_hdac_ext_bus_link_put(edev->ebus, hlink);
1601
1602 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301603}
1604
1605static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1606{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301607 struct hdac_hdmi_priv *hdmi = edev->private_data;
1608 struct hdac_hdmi_pin *pin, *pin_next;
1609 struct hdac_hdmi_cvt *cvt, *cvt_next;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301610 struct hdac_hdmi_pcm *pcm, *pcm_next;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301611
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301612 snd_soc_unregister_codec(&edev->hdac.dev);
1613
Jeeja KP4a3478d2016-02-12 07:46:06 +05301614 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1615 pcm->cvt = NULL;
1616 pcm->pin = NULL;
1617 list_del(&pcm->head);
1618 kfree(pcm);
1619 }
1620
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301621 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1622 list_del(&cvt->head);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301623 kfree(cvt->name);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301624 kfree(cvt);
1625 }
1626
1627 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1628 list_del(&pin->head);
1629 kfree(pin);
1630 }
1631
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301632 return 0;
1633}
1634
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301635#ifdef CONFIG_PM
1636static int hdac_hdmi_runtime_suspend(struct device *dev)
1637{
1638 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1639 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301640 struct hdac_bus *bus = hdac->bus;
Vinod Koulb2047e92016-05-12 08:58:55 +05301641 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1642 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301643 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301644
1645 dev_dbg(dev, "Enter: %s\n", __func__);
1646
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301647 /* controller may not have been initialized for the first time */
1648 if (!bus)
1649 return 0;
1650
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301651 /*
1652 * Power down afg.
1653 * codec_read is preferred over codec_write to set the power state.
1654 * This way verb is send to set the power state and response
1655 * is received. So setting power state is ensured without using loop
1656 * to read the state.
1657 */
1658 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1659 AC_PWRST_D3);
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301660 err = snd_hdac_display_power(bus, false);
1661 if (err < 0) {
1662 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1663 return err;
1664 }
1665
Vinod Koulb2047e92016-05-12 08:58:55 +05301666 hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301667 if (!hlink) {
1668 dev_err(dev, "hdac link not found\n");
1669 return -EIO;
1670 }
1671
Vinod Koulb2047e92016-05-12 08:58:55 +05301672 snd_hdac_ext_bus_link_put(ebus, hlink);
1673
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301674 return 0;
1675}
1676
1677static int hdac_hdmi_runtime_resume(struct device *dev)
1678{
1679 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1680 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301681 struct hdac_bus *bus = hdac->bus;
Vinod Koulb2047e92016-05-12 08:58:55 +05301682 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1683 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301684 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301685
1686 dev_dbg(dev, "Enter: %s\n", __func__);
1687
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301688 /* controller may not have been initialized for the first time */
1689 if (!bus)
1690 return 0;
1691
Vinod Koulb2047e92016-05-12 08:58:55 +05301692 hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301693 if (!hlink) {
1694 dev_err(dev, "hdac link not found\n");
1695 return -EIO;
1696 }
1697
Vinod Koulb2047e92016-05-12 08:58:55 +05301698 snd_hdac_ext_bus_link_get(ebus, hlink);
1699
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301700 err = snd_hdac_display_power(bus, true);
1701 if (err < 0) {
1702 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1703 return err;
1704 }
1705
Subhransu S. Prustyab85f5b2016-02-17 21:34:02 +05301706 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1707 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1708
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301709 /* Power up afg */
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301710 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1711 AC_PWRST_D0);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301712
1713 return 0;
1714}
1715#else
1716#define hdac_hdmi_runtime_suspend NULL
1717#define hdac_hdmi_runtime_resume NULL
1718#endif
1719
1720static const struct dev_pm_ops hdac_hdmi_pm = {
1721 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301722 .prepare = hdmi_codec_prepare,
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301723 .complete = hdmi_codec_complete,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301724};
1725
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301726static const struct hda_device_id hdmi_list[] = {
1727 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
Jeeja KPe2304802016-03-11 10:12:55 +05301728 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
Shreyas NCcc216882016-07-11 22:02:09 +05301729 HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301730 {}
1731};
1732
1733MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1734
1735static struct hdac_ext_driver hdmi_driver = {
1736 . hdac = {
1737 .driver = {
1738 .name = "HDMI HDA Codec",
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301739 .pm = &hdac_hdmi_pm,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301740 },
1741 .id_table = hdmi_list,
1742 },
1743 .probe = hdac_hdmi_dev_probe,
1744 .remove = hdac_hdmi_dev_remove,
1745};
1746
1747static int __init hdmi_init(void)
1748{
1749 return snd_hda_ext_driver_register(&hdmi_driver);
1750}
1751
1752static void __exit hdmi_exit(void)
1753{
1754 snd_hda_ext_driver_unregister(&hdmi_driver);
1755}
1756
1757module_init(hdmi_init);
1758module_exit(hdmi_exit);
1759
1760MODULE_LICENSE("GPL v2");
1761MODULE_DESCRIPTION("HDMI HD codec");
1762MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1763MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");