blob: c0b49f4b70744c9db0d3c0473e656f20f501c4f5 [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;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530100 int stream_tag;
101 int channels;
102 int format;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530103};
104
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530105struct hdac_hdmi_dai_pin_map {
106 int dai_id;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530107 struct hdac_hdmi_pin *pin;
108 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530109};
110
111struct hdac_hdmi_priv {
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530112 struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530113 struct list_head pin_list;
114 struct list_head cvt_list;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530115 struct list_head pcm_list;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530116 int num_pin;
117 int num_cvt;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530118 struct mutex pin_mutex;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530119 struct hdac_chmap chmap;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530120};
121
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530122static struct hdac_hdmi_pcm *
123hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
124 struct hdac_hdmi_cvt *cvt)
125{
126 struct hdac_hdmi_pcm *pcm = NULL;
Jeeja KP1de777f2017-01-10 17:57:48 +0530127
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530128 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
129 if (pcm->cvt == cvt)
130 break;
131 }
132
133 return pcm;
134}
Jeeja KP1de777f2017-01-10 17:57:48 +0530135
Subhransu S. Prusty28890992016-04-14 10:07:34 +0530136static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
137 int pcm_idx)
138{
139 struct hdac_hdmi_pcm *pcm;
140
141 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
142 if (pcm->pcm_id == pcm_idx)
143 return pcm;
144 }
145
146 return NULL;
147}
148
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530149static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
150{
Geliang Tang51b2c422015-12-28 22:47:13 +0800151 struct hdac_device *hdac = dev_to_hdac_dev(dev);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530152
Geliang Tang51b2c422015-12-28 22:47:13 +0800153 return to_ehdac_device(hdac);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530154}
155
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530156static unsigned int sad_format(const u8 *sad)
157{
158 return ((sad[0] >> 0x3) & 0x1f);
159}
160
161static unsigned int sad_sample_bits_lpcm(const u8 *sad)
162{
163 return (sad[2] & 7);
164}
165
166static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
167 void *eld)
168{
169 u64 formats = SNDRV_PCM_FMTBIT_S16;
170 int i;
171 const u8 *sad, *eld_buf = eld;
172
173 sad = drm_eld_sad(eld_buf);
174 if (!sad)
175 goto format_constraint;
176
177 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
178 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
179
180 /*
181 * the controller support 20 and 24 bits in 32 bit
182 * container so we set S32
183 */
184 if (sad_sample_bits_lpcm(sad) & 0x6)
185 formats |= SNDRV_PCM_FMTBIT_S32;
186 }
187 }
188
189format_constraint:
190 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
191 formats);
192
193}
194
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530195static void
196hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
197 int packet_index, int byte_index)
198{
199 int val;
200
201 val = (packet_index << 5) | (byte_index & 0x1f);
202
203 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
204 AC_VERB_SET_HDMI_DIP_INDEX, val);
205}
206
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530207struct dp_audio_infoframe {
208 u8 type; /* 0x84 */
209 u8 len; /* 0x1b */
210 u8 ver; /* 0x11 << 2 */
211
212 u8 CC02_CT47; /* match with HDMI infoframe from this on */
213 u8 SS01_SF24;
214 u8 CXT04;
215 u8 CA;
216 u8 LFEPBL01_LSV36_DM_INH7;
217};
218
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530219static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
220 hda_nid_t cvt_nid, hda_nid_t pin_nid)
221{
222 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
223 struct hdmi_audio_infoframe frame;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530224 struct dp_audio_infoframe dp_ai;
225 struct hdac_hdmi_priv *hdmi = hdac->private_data;
226 struct hdac_hdmi_pin *pin;
227 u8 *dip;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530228 int ret;
229 int i;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530230 const u8 *eld_buf;
231 u8 conn_type;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530232 int channels, ca;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530233
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530234 list_for_each_entry(pin, &hdmi->pin_list, head) {
235 if (pin->nid == pin_nid)
236 break;
237 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530238
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530239 ca = snd_hdac_channel_allocation(&hdac->hdac, pin->eld.info.spk_alloc,
240 pin->channels, pin->chmap_set, true, pin->chmap);
241
242 channels = snd_hdac_get_active_channels(ca);
243 hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt_nid, channels);
244
245 snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
246 pin->channels, pin->chmap, pin->chmap_set);
247
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530248 eld_buf = pin->eld.eld_buffer;
249 conn_type = drm_eld_get_conn_type(eld_buf);
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530250
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530251 switch (conn_type) {
252 case DRM_ELD_CONN_TYPE_HDMI:
253 hdmi_audio_infoframe_init(&frame);
254
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530255 frame.channels = channels;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530256 frame.channel_allocation = ca;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530257
258 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
259 if (ret < 0)
260 return ret;
261
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530262 break;
263
264 case DRM_ELD_CONN_TYPE_DP:
265 memset(&dp_ai, 0, sizeof(dp_ai));
266 dp_ai.type = 0x84;
267 dp_ai.len = 0x1b;
268 dp_ai.ver = 0x11 << 2;
269 dp_ai.CC02_CT47 = channels - 1;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530270 dp_ai.CA = ca;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530271
272 dip = (u8 *)&dp_ai;
273 break;
274
275 default:
276 dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
277 conn_type);
278 return -EIO;
279 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530280
281 /* stop infoframe transmission */
282 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
283 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
284 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
285
286
287 /* Fill infoframe. Index auto-incremented */
288 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530289 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
Subhransu S. Prusty391005e2016-03-10 09:04:07 +0530290 for (i = 0; i < sizeof(buffer); i++)
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530291 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
Subhransu S. Prusty391005e2016-03-10 09:04:07 +0530292 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530293 } else {
294 for (i = 0; i < sizeof(dp_ai); i++)
295 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
296 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
297 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530298
299 /* Start infoframe */
300 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
301 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
302 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
303
304 return 0;
305}
306
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530307static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
308 unsigned int tx_mask, unsigned int rx_mask,
309 int slots, int slot_width)
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530310{
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530311 struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
312 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530313 struct hdac_hdmi_dai_pin_map *dai_map;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530314 struct hdac_hdmi_pcm *pcm;
315
316 dev_dbg(&edev->hdac.dev, "%s: strm_tag: %d\n", __func__, tx_mask);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530317
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530318 dai_map = &hdmi->dai_map[dai->id];
319
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530320 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530321
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530322 if (pcm)
323 pcm->stream_tag = (tx_mask << 4);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530324
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530325 return 0;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530326}
327
328static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
329 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
330{
331 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530332 struct hdac_hdmi_priv *hdmi = hdac->private_data;
333 struct hdac_hdmi_dai_pin_map *dai_map;
334 struct hdac_hdmi_pin *pin;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530335 struct hdac_hdmi_pcm *pcm;
336 int format;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530337
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530338 dai_map = &hdmi->dai_map[dai->id];
339 pin = dai_map->pin;
340
341 if (!pin)
342 return -ENODEV;
343
344 if ((!pin->eld.monitor_present) || (!pin->eld.eld_valid)) {
345 dev_err(&hdac->hdac.dev, "device is not configured for this pin: %d\n",
346 pin->nid);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530347 return -ENODEV;
348 }
349
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530350 format = snd_hdac_calc_stream_format(params_rate(hparams),
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530351 params_channels(hparams), params_format(hparams),
352 24, 0);
353
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530354 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
355 if (!pcm)
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530356 return -EIO;
357
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530358 pcm->format = format;
359 pcm->channels = params_channels(hparams);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530360
361 return 0;
362}
363
364static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
365 struct hdac_hdmi_pin *pin)
366{
367 if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
368 dev_warn(&hdac->hdac.dev,
369 "HDMI: pin %d wcaps %#x does not support connection list\n",
370 pin->nid, get_wcaps(&hdac->hdac, pin->nid));
371 return -EINVAL;
372 }
373
374 pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
375 pin->mux_nids, HDA_MAX_CONNECTIONS);
376 if (pin->num_mux_nids == 0)
377 dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
378 pin->nid);
379
380 dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
381 pin->num_mux_nids, pin->nid);
382
383 return pin->num_mux_nids;
384}
385
386/*
387 * Query pcm list and return pin widget to which stream is routed.
388 *
389 * Also query connection list of the pin, to validate the cvt to pin map.
390 *
391 * Same stream rendering to multiple pins simultaneously can be done
392 * possibly, but not supported for now in driver. So return the first pin
393 * connected.
394 */
395static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
396 struct hdac_ext_device *edev,
397 struct hdac_hdmi_priv *hdmi,
398 struct hdac_hdmi_cvt *cvt)
399{
400 struct hdac_hdmi_pcm *pcm;
401 struct hdac_hdmi_pin *pin = NULL;
402 int ret, i;
403
404 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
405 if (pcm->cvt == cvt) {
406 pin = pcm->pin;
407 break;
408 }
409 }
410
411 if (pin) {
412 ret = hdac_hdmi_query_pin_connlist(edev, pin);
413 if (ret < 0)
414 return NULL;
415
416 for (i = 0; i < pin->num_mux_nids; i++) {
417 if (pin->mux_nids[i] == cvt->nid)
418 return pin;
419 }
420 }
421
422 return NULL;
423}
424
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530425/*
426 * This tries to get a valid pin and set the HW constraints based on the
427 * ELD. Even if a valid pin is not found return success so that device open
428 * doesn't fail.
429 */
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530430static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
431 struct snd_soc_dai *dai)
432{
433 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
434 struct hdac_hdmi_priv *hdmi = hdac->private_data;
435 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530436 struct hdac_hdmi_cvt *cvt;
437 struct hdac_hdmi_pin *pin;
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530438 int ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530439
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530440 dai_map = &hdmi->dai_map[dai->id];
441
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530442 cvt = dai_map->cvt;
443 pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530444
445 /*
446 * To make PA and other userland happy.
447 * userland scans devices so returning error does not help.
448 */
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530449 if (!pin)
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530450 return 0;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530451
452 if ((!pin->eld.monitor_present) ||
453 (!pin->eld.eld_valid)) {
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530454
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530455 dev_warn(&hdac->hdac.dev,
Colin Ian King3fb7b4e2016-08-26 19:17:25 +0100456 "Failed: monitor present? %d ELD valid?: %d for pin: %d\n",
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530457 pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530458
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530459 return 0;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530460 }
461
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530462 dai_map->pin = pin;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530463
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530464 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530465 pin->eld.eld_buffer);
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530466 if (ret < 0)
467 return ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530468
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530469 return snd_pcm_hw_constraint_eld(substream->runtime,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530470 pin->eld.eld_buffer);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530471}
472
473static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
474 struct snd_soc_dai *dai)
475{
476 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
477 struct hdac_hdmi_priv *hdmi = hdac->private_data;
478 struct hdac_hdmi_dai_pin_map *dai_map;
479
480 dai_map = &hdmi->dai_map[dai->id];
481
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530482 if (dai_map->pin) {
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530483 mutex_lock(&dai_map->pin->lock);
Subhransu S. Prusty28890992016-04-14 10:07:34 +0530484 dai_map->pin->chmap_set = false;
485 memset(dai_map->pin->chmap, 0, sizeof(dai_map->pin->chmap));
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530486 dai_map->pin->channels = 0;
487 mutex_unlock(&dai_map->pin->lock);
488
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530489 dai_map->pin = NULL;
490 }
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530491}
492
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530493static int
494hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
495{
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530496 unsigned int chans;
497 struct hdac_ext_device *edev = to_ehdac_device(hdac);
498 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530499 int err;
500
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530501 chans = get_wcaps(hdac, cvt->nid);
502 chans = get_wcaps_channels(chans);
503
504 cvt->params.channels_min = 2;
505
506 cvt->params.channels_max = chans;
507 if (chans > hdmi->chmap.channels_max)
508 hdmi->chmap.channels_max = chans;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530509
510 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
511 &cvt->params.rates,
512 &cvt->params.formats,
513 &cvt->params.maxbps);
514 if (err < 0)
515 dev_err(&hdac->dev,
516 "Failed to query pcm params for nid %d: %d\n",
517 cvt->nid, err);
518
519 return err;
520}
521
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530522static int hdac_hdmi_fill_widget_info(struct device *dev,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530523 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
524 void *priv, const char *wname, const char *stream,
525 struct snd_kcontrol_new *wc, int numkc,
526 int (*event)(struct snd_soc_dapm_widget *,
527 struct snd_kcontrol *, int), unsigned short event_flags)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530528{
529 w->id = id;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530530 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
531 if (!w->name)
532 return -ENOMEM;
533
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530534 w->sname = stream;
535 w->reg = SND_SOC_NOPM;
536 w->shift = 0;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530537 w->kcontrol_news = wc;
538 w->num_kcontrols = numkc;
539 w->priv = priv;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530540 w->event = event;
541 w->event_flags = event_flags;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530542
543 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530544}
545
546static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530547 const char *sink, const char *control, const char *src,
548 int (*handler)(struct snd_soc_dapm_widget *src,
549 struct snd_soc_dapm_widget *sink))
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530550{
551 route->sink = sink;
552 route->source = src;
553 route->control = control;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530554 route->connected = handler;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530555}
556
Jeeja KP4a3478d2016-02-12 07:46:06 +0530557static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
558 struct hdac_hdmi_pin *pin)
559{
560 struct hdac_hdmi_priv *hdmi = edev->private_data;
561 struct hdac_hdmi_pcm *pcm = NULL;
562
563 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
564 if (pcm->pin == pin)
565 return pcm;
566 }
567
568 return NULL;
569}
570
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530571static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
572 hda_nid_t nid, unsigned int pwr_state)
573{
574 if (get_wcaps(&edev->hdac, nid) & AC_WCAP_POWER) {
575 if (!snd_hdac_check_power_state(&edev->hdac, nid, pwr_state))
576 snd_hdac_codec_write(&edev->hdac, nid, 0,
577 AC_VERB_SET_POWER_STATE, pwr_state);
578 }
579}
580
581static void hdac_hdmi_set_amp(struct hdac_ext_device *edev,
582 hda_nid_t nid, int val)
583{
584 if (get_wcaps(&edev->hdac, nid) & AC_WCAP_OUT_AMP)
585 snd_hdac_codec_write(&edev->hdac, nid, 0,
586 AC_VERB_SET_AMP_GAIN_MUTE, val);
587}
588
589
590static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
591 struct snd_kcontrol *kc, int event)
592{
593 struct hdac_hdmi_pin *pin = w->priv;
594 struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
595 struct hdac_hdmi_pcm *pcm;
596
597 dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
598 __func__, w->name, event);
599
600 pcm = hdac_hdmi_get_pcm(edev, pin);
601 if (!pcm)
602 return -EIO;
603
604 switch (event) {
605 case SND_SOC_DAPM_PRE_PMU:
606 hdac_hdmi_set_power_state(edev, pin->nid, AC_PWRST_D0);
607
608 /* Enable out path for this pin widget */
609 snd_hdac_codec_write(&edev->hdac, pin->nid, 0,
610 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
611
612 hdac_hdmi_set_amp(edev, pin->nid, AMP_OUT_UNMUTE);
613
614 return hdac_hdmi_setup_audio_infoframe(edev, pcm->cvt->nid,
615 pin->nid);
616
617 case SND_SOC_DAPM_POST_PMD:
618 hdac_hdmi_set_amp(edev, pin->nid, AMP_OUT_MUTE);
619
620 /* Disable out path for this pin widget */
621 snd_hdac_codec_write(&edev->hdac, pin->nid, 0,
622 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
623
624 hdac_hdmi_set_power_state(edev, pin->nid, AC_PWRST_D3);
625 break;
626
627 }
628
629 return 0;
630}
631
632static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
633 struct snd_kcontrol *kc, int event)
634{
635 struct hdac_hdmi_cvt *cvt = w->priv;
636 struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
637 struct hdac_hdmi_priv *hdmi = edev->private_data;
638 struct hdac_hdmi_pcm *pcm;
639
640 dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
641 __func__, w->name, event);
642
643 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
644 if (!pcm)
645 return -EIO;
646
647 switch (event) {
648 case SND_SOC_DAPM_PRE_PMU:
649 hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D0);
650
651 /* Enable transmission */
652 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
653 AC_VERB_SET_DIGI_CONVERT_1, 1);
654
655 /* Category Code (CC) to zero */
656 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
657 AC_VERB_SET_DIGI_CONVERT_2, 0);
658
659 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
660 AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
661 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
662 AC_VERB_SET_STREAM_FORMAT, pcm->format);
663 break;
664
665 case SND_SOC_DAPM_POST_PMD:
666 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
667 AC_VERB_SET_CHANNEL_STREAMID, 0);
668 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
669 AC_VERB_SET_STREAM_FORMAT, 0);
670
671 hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D3);
672 break;
673
674 }
675
676 return 0;
677}
678
679static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
680 struct snd_kcontrol *kc, int event)
681{
682 struct hdac_hdmi_pin *pin = w->priv;
683 struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
684 int mux_idx;
685
686 dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
687 __func__, w->name, event);
688
689 if (!kc)
690 kc = w->kcontrols[0];
691
692 mux_idx = dapm_kcontrol_get_value(kc);
693 if (mux_idx > 0) {
694 snd_hdac_codec_write(&edev->hdac, pin->nid, 0,
695 AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
696 }
697
698 return 0;
699}
700
Jeeja KP4a3478d2016-02-12 07:46:06 +0530701/*
702 * Based on user selection, map the PINs with the PCMs.
703 */
704static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
705 struct snd_ctl_elem_value *ucontrol)
706{
707 int ret;
708 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
709 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
710 struct snd_soc_dapm_context *dapm = w->dapm;
711 struct hdac_hdmi_pin *pin = w->priv;
712 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
713 struct hdac_hdmi_priv *hdmi = edev->private_data;
714 struct hdac_hdmi_pcm *pcm = NULL;
715 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
716
717 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
718 if (ret < 0)
719 return ret;
720
721 mutex_lock(&hdmi->pin_mutex);
722 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
723 if (pcm->pin == pin)
724 pcm->pin = NULL;
725
726 /*
727 * Jack status is not reported during device probe as the
728 * PCMs are not registered by then. So report it here.
729 */
730 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
731 pcm->pin = pin;
732 if (pin->eld.monitor_present && pin->eld.eld_valid) {
733 dev_dbg(&edev->hdac.dev,
734 "jack report for pcm=%d\n",
735 pcm->pcm_id);
736
737 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
738 }
739 mutex_unlock(&hdmi->pin_mutex);
740 return ret;
741 }
742 }
743 mutex_unlock(&hdmi->pin_mutex);
744
745 return ret;
746}
747
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530748/*
749 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
750 * the display driver seem to be programming the connection list for the pin
751 * widget runtime.
752 *
753 * So programming all the possible inputs for the mux, the user has to take
754 * care of selecting the right one and leaving all other inputs selected to
755 * "NONE"
756 */
757static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
758 struct hdac_hdmi_pin *pin,
759 struct snd_soc_dapm_widget *widget,
760 const char *widget_name)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530761{
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530762 struct hdac_hdmi_priv *hdmi = edev->private_data;
763 struct snd_kcontrol_new *kc;
764 struct hdac_hdmi_cvt *cvt;
765 struct soc_enum *se;
766 char kc_name[NAME_SIZE];
767 char mux_items[NAME_SIZE];
768 /* To hold inputs to the Pin mux */
769 char *items[HDA_MAX_CONNECTIONS];
770 int i = 0;
771 int num_items = hdmi->num_cvt + 1;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530772
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530773 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
774 if (!kc)
775 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530776
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530777 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
778 if (!se)
779 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530780
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530781 sprintf(kc_name, "Pin %d Input", pin->nid);
782 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
783 if (!kc->name)
784 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530785
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530786 kc->private_value = (long)se;
787 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
788 kc->access = 0;
789 kc->info = snd_soc_info_enum_double;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530790 kc->put = hdac_hdmi_set_pin_mux;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530791 kc->get = snd_soc_dapm_get_enum_double;
792
793 se->reg = SND_SOC_NOPM;
794
795 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
796 se->items = num_items;
797 se->mask = roundup_pow_of_two(se->items) - 1;
798
799 sprintf(mux_items, "NONE");
800 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
801 if (!items[i])
802 return -ENOMEM;
803
804 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
805 i++;
806 sprintf(mux_items, "cvt %d", cvt->nid);
807 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
808 if (!items[i])
809 return -ENOMEM;
810 }
811
812 se->texts = devm_kmemdup(&edev->hdac.dev, items,
813 (num_items * sizeof(char *)), GFP_KERNEL);
814 if (!se->texts)
815 return -ENOMEM;
816
817 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530818 snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1,
819 hdac_hdmi_pin_mux_widget_event,
820 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530821}
822
823/* Add cvt <- input <- mux route map */
824static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
825 struct snd_soc_dapm_widget *widgets,
826 struct snd_soc_dapm_route *route, int rindex)
827{
828 struct hdac_hdmi_priv *hdmi = edev->private_data;
829 const struct snd_kcontrol_new *kc;
830 struct soc_enum *se;
831 int mux_index = hdmi->num_cvt + hdmi->num_pin;
832 int i, j;
833
834 for (i = 0; i < hdmi->num_pin; i++) {
835 kc = widgets[mux_index].kcontrol_news;
836 se = (struct soc_enum *)kc->private_value;
837 for (j = 0; j < hdmi->num_cvt; j++) {
838 hdac_hdmi_fill_route(&route[rindex],
839 widgets[mux_index].name,
840 se->texts[j + 1],
841 widgets[j].name, NULL);
842
843 rindex++;
844 }
845
846 mux_index++;
847 }
848}
849
850/*
851 * Widgets are added in the below sequence
852 * Converter widgets for num converters enumerated
853 * Pin widgets for num pins enumerated
854 * Pin mux widgets to represent connenction list of pin widget
855 *
856 * Total widgets elements = num_cvt + num_pin + num_pin;
857 *
858 * Routes are added as below:
859 * pin mux -> pin (based on num_pins)
860 * cvt -> "Input sel control" -> pin_mux
861 *
862 * Total route elements:
863 * num_pins + (pin_muxes * num_cvt)
864 */
865static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
866{
867 struct snd_soc_dapm_widget *widgets;
868 struct snd_soc_dapm_route *route;
869 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
870 struct hdac_hdmi_priv *hdmi = edev->private_data;
871 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
872 char widget_name[NAME_SIZE];
873 struct hdac_hdmi_cvt *cvt;
874 struct hdac_hdmi_pin *pin;
875 int ret, i = 0, num_routes = 0;
876
877 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
878 return -EINVAL;
879
880 widgets = devm_kzalloc(dapm->dev,
881 (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
882 GFP_KERNEL);
883
884 if (!widgets)
885 return -ENOMEM;
886
887 /* DAPM widgets to represent each converter widget */
888 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
889 sprintf(widget_name, "Converter %d", cvt->nid);
890 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530891 snd_soc_dapm_aif_in, cvt,
892 widget_name, dai_drv[i].playback.stream_name, NULL, 0,
893 hdac_hdmi_cvt_output_widget_event,
894 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530895 if (ret < 0)
896 return ret;
897 i++;
898 }
899
900 list_for_each_entry(pin, &hdmi->pin_list, head) {
901 sprintf(widget_name, "hif%d Output", pin->nid);
902 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530903 snd_soc_dapm_output, pin,
904 widget_name, NULL, NULL, 0,
905 hdac_hdmi_pin_output_widget_event,
906 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530907 if (ret < 0)
908 return ret;
909 i++;
910 }
911
912 /* DAPM widgets to represent the connection list to pin widget */
913 list_for_each_entry(pin, &hdmi->pin_list, head) {
914 sprintf(widget_name, "Pin %d Mux", pin->nid);
915 ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
916 widget_name);
917 if (ret < 0)
918 return ret;
919 i++;
920
921 /* For cvt to pin_mux mapping */
922 num_routes += hdmi->num_cvt;
923
924 /* For pin_mux to pin mapping */
925 num_routes++;
926 }
927
928 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
929 GFP_KERNEL);
930 if (!route)
931 return -ENOMEM;
932
933 i = 0;
934 /* Add pin <- NULL <- mux route map */
935 list_for_each_entry(pin, &hdmi->pin_list, head) {
936 int sink_index = i + hdmi->num_cvt;
937 int src_index = sink_index + hdmi->num_pin;
938
939 hdac_hdmi_fill_route(&route[i],
940 widgets[sink_index].name, NULL,
941 widgets[src_index].name, NULL);
942 i++;
943
944 }
945
946 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
947
948 snd_soc_dapm_new_controls(dapm, widgets,
949 ((2 * hdmi->num_pin) + hdmi->num_cvt));
950
951 snd_soc_dapm_add_routes(dapm, route, num_routes);
952 snd_soc_dapm_new_widgets(dapm->card);
953
954 return 0;
955
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530956}
957
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530958static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530959{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530960 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530961 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530962 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530963 int dai_id = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530964
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530965 if (list_empty(&hdmi->cvt_list))
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530966 return -EINVAL;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530967
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530968 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
969 dai_map = &hdmi->dai_map[dai_id];
970 dai_map->dai_id = dai_id;
971 dai_map->cvt = cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530972
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530973 dai_id++;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530974
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530975 if (dai_id == HDA_MAX_CVTS) {
976 dev_warn(&edev->hdac.dev,
977 "Max dais supported: %d\n", dai_id);
978 break;
979 }
980 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530981
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530982 return 0;
983}
984
985static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
986{
987 struct hdac_hdmi_priv *hdmi = edev->private_data;
988 struct hdac_hdmi_cvt *cvt;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530989 char name[NAME_SIZE];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530990
991 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
992 if (!cvt)
993 return -ENOMEM;
994
995 cvt->nid = nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530996 sprintf(name, "cvt %d", cvt->nid);
997 cvt->name = kstrdup(name, GFP_KERNEL);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530998
999 list_add_tail(&cvt->head, &hdmi->cvt_list);
1000 hdmi->num_cvt++;
1001
1002 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
1003}
1004
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301005static int hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301006 struct hdac_hdmi_pin *pin)
1007{
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301008 unsigned int ver, mnl;
1009
1010 ver = (pin->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1011 >> DRM_ELD_VER_SHIFT;
1012
1013 if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1014 dev_err(&edev->hdac.dev, "HDMI: Unknown ELD version %d\n", ver);
1015 return -EINVAL;
1016 }
1017
1018 mnl = (pin->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1019 DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1020
1021 if (mnl > ELD_MAX_MNL) {
1022 dev_err(&edev->hdac.dev, "HDMI: MNL Invalid %d\n", mnl);
1023 return -EINVAL;
1024 }
1025
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301026 pin->eld.info.spk_alloc = pin->eld.eld_buffer[DRM_ELD_SPEAKER];
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301027
1028 return 0;
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301029}
1030
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301031static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin)
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301032{
1033 struct hdac_ext_device *edev = pin->edev;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301034 struct hdac_hdmi_priv *hdmi = edev->private_data;
1035 struct hdac_hdmi_pcm *pcm;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301036 int size;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301037
1038 mutex_lock(&hdmi->pin_mutex);
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301039 pin->eld.monitor_present = false;
1040
1041 size = snd_hdac_acomp_get_eld(&edev->hdac, pin->nid, -1,
1042 &pin->eld.monitor_present, pin->eld.eld_buffer,
1043 ELD_MAX_SIZE);
1044
1045 if (size > 0) {
1046 size = min(size, ELD_MAX_SIZE);
1047 if (hdac_hdmi_parse_eld(edev, pin) < 0)
1048 size = -EINVAL;
1049 }
1050
1051 if (size > 0) {
1052 pin->eld.eld_valid = true;
1053 pin->eld.eld_size = size;
1054 } else {
1055 pin->eld.eld_valid = false;
1056 pin->eld.eld_size = 0;
1057 }
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301058
Jeeja KP4a3478d2016-02-12 07:46:06 +05301059 pcm = hdac_hdmi_get_pcm(edev, pin);
1060
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301061 if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
1062
1063 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
1064 __func__, pin->nid);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301065
1066 /*
1067 * PCMs are not registered during device probe, so don't
1068 * report jack here. It will be done in usermode mux
1069 * control select.
1070 */
1071 if (pcm) {
1072 dev_dbg(&edev->hdac.dev,
1073 "jack report for pcm=%d\n", pcm->pcm_id);
1074
1075 snd_jack_report(pcm->jack, 0);
1076 }
1077
1078 mutex_unlock(&hdmi->pin_mutex);
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301079 return;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301080 }
1081
1082 if (pin->eld.monitor_present && pin->eld.eld_valid) {
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301083 if (pcm) {
1084 dev_dbg(&edev->hdac.dev,
1085 "jack report for pcm=%d\n",
1086 pcm->pcm_id);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301087
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301088 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301089 }
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301090
1091 print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1092 pin->eld.eld_buffer, pin->eld.eld_size, false);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301093 }
1094
Jeeja KP4a3478d2016-02-12 07:46:06 +05301095 mutex_unlock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301096}
1097
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301098static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1099{
1100 struct hdac_hdmi_priv *hdmi = edev->private_data;
1101 struct hdac_hdmi_pin *pin;
1102
1103 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1104 if (!pin)
1105 return -ENOMEM;
1106
1107 pin->nid = nid;
1108
1109 list_add_tail(&pin->head, &hdmi->pin_list);
1110 hdmi->num_pin++;
1111
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301112 pin->edev = edev;
Subhransu S. Prustybcced702016-04-14 10:07:30 +05301113 mutex_init(&pin->lock);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301114
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301115 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301116}
1117
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301118#define INTEL_VENDOR_NID 0x08
1119#define INTEL_GET_VENDOR_VERB 0xf81
1120#define INTEL_SET_VENDOR_VERB 0x781
1121#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1122#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1123
1124static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1125{
1126 unsigned int vendor_param;
1127
1128 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1129 INTEL_GET_VENDOR_VERB, 0);
1130 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1131 return;
1132
1133 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1134 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1135 INTEL_SET_VENDOR_VERB, vendor_param);
1136 if (vendor_param == -1)
1137 return;
1138}
1139
1140static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1141{
1142 unsigned int vendor_param;
1143
1144 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1145 INTEL_GET_VENDOR_VERB, 0);
1146 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1147 return;
1148
1149 /* enable DP1.2 mode */
1150 vendor_param |= INTEL_EN_DP12;
1151 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1152 INTEL_SET_VENDOR_VERB, vendor_param);
1153 if (vendor_param == -1)
1154 return;
1155
1156}
1157
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301158static struct snd_soc_dai_ops hdmi_dai_ops = {
1159 .startup = hdac_hdmi_pcm_open,
1160 .shutdown = hdac_hdmi_pcm_close,
1161 .hw_params = hdac_hdmi_set_hw_params,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +05301162 .set_tdm_slot = hdac_hdmi_set_tdm_slot,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301163};
1164
1165/*
1166 * Each converter can support a stream independently. So a dai is created
1167 * based on the number of converter queried.
1168 */
1169static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1170 struct snd_soc_dai_driver **dais,
1171 struct hdac_hdmi_priv *hdmi, int num_dais)
1172{
1173 struct snd_soc_dai_driver *hdmi_dais;
1174 struct hdac_hdmi_cvt *cvt;
1175 char name[NAME_SIZE], dai_name[NAME_SIZE];
1176 int i = 0;
1177 u32 rates, bps;
1178 unsigned int rate_max = 384000, rate_min = 8000;
1179 u64 formats;
1180 int ret;
1181
1182 hdmi_dais = devm_kzalloc(&hdac->dev,
1183 (sizeof(*hdmi_dais) * num_dais),
1184 GFP_KERNEL);
1185 if (!hdmi_dais)
1186 return -ENOMEM;
1187
1188 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1189 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1190 &rates, &formats, &bps);
1191 if (ret)
1192 return ret;
1193
1194 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1195 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1196 dai_name, GFP_KERNEL);
1197
1198 if (!hdmi_dais[i].name)
1199 return -ENOMEM;
1200
1201 snprintf(name, sizeof(name), "hifi%d", i+1);
1202 hdmi_dais[i].playback.stream_name =
1203 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1204 if (!hdmi_dais[i].playback.stream_name)
1205 return -ENOMEM;
1206
1207 /*
1208 * Set caps based on capability queried from the converter.
1209 * It will be constrained runtime based on ELD queried.
1210 */
1211 hdmi_dais[i].playback.formats = formats;
1212 hdmi_dais[i].playback.rates = rates;
1213 hdmi_dais[i].playback.rate_max = rate_max;
1214 hdmi_dais[i].playback.rate_min = rate_min;
1215 hdmi_dais[i].playback.channels_min = 2;
1216 hdmi_dais[i].playback.channels_max = 2;
1217 hdmi_dais[i].ops = &hdmi_dai_ops;
1218
1219 i++;
1220 }
1221
1222 *dais = hdmi_dais;
1223
1224 return 0;
1225}
1226
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301227/*
1228 * Parse all nodes and store the cvt/pin nids in array
1229 * Add one time initialization for pin and cvt widgets
1230 */
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301231static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1232 struct snd_soc_dai_driver **dais, int *num_dais)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301233{
1234 hda_nid_t nid;
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301235 int i, num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301236 struct hdac_device *hdac = &edev->hdac;
1237 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301238 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301239
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301240 hdac_hdmi_skl_enable_all_pins(hdac);
1241 hdac_hdmi_skl_enable_dp12(hdac);
1242
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301243 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
Subhransu S. Prusty541140d2015-12-09 21:46:08 +05301244 if (!nid || num_nodes <= 0) {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301245 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1246 return -EINVAL;
1247 }
1248
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301249 hdac->num_nodes = num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301250 hdac->start_nid = nid;
1251
1252 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1253 unsigned int caps;
1254 unsigned int type;
1255
1256 caps = get_wcaps(hdac, nid);
1257 type = get_wcaps_type(caps);
1258
1259 if (!(caps & AC_WCAP_DIGITAL))
1260 continue;
1261
1262 switch (type) {
1263
1264 case AC_WID_AUD_OUT:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301265 ret = hdac_hdmi_add_cvt(edev, nid);
1266 if (ret < 0)
1267 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301268 break;
1269
1270 case AC_WID_PIN:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301271 ret = hdac_hdmi_add_pin(edev, nid);
1272 if (ret < 0)
1273 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301274 break;
1275 }
1276 }
1277
1278 hdac->end_nid = nid;
1279
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301280 if (!hdmi->num_pin || !hdmi->num_cvt)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301281 return -EIO;
1282
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301283 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1284 if (ret) {
1285 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1286 ret);
1287 return ret;
1288 }
1289
1290 *num_dais = hdmi->num_cvt;
1291
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301292 return hdac_hdmi_init_dai_map(edev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301293}
1294
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -07001295static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301296{
1297 struct hdac_ext_device *edev = aptr;
1298 struct hdac_hdmi_priv *hdmi = edev->private_data;
1299 struct hdac_hdmi_pin *pin;
1300 struct snd_soc_codec *codec = edev->scodec;
1301
1302 /* Don't know how this mapping is derived */
1303 hda_nid_t pin_nid = port + 0x04;
1304
1305 dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1306
1307 /*
1308 * skip notification during system suspend (but not in runtime PM);
1309 * the state will be updated at resume. Also since the ELD and
1310 * connection states are updated in anyway at the end of the resume,
1311 * we can skip it when received during PM process.
1312 */
1313 if (snd_power_get_state(codec->component.card->snd_card) !=
1314 SNDRV_CTL_POWER_D0)
1315 return;
1316
1317 if (atomic_read(&edev->hdac.in_pm))
1318 return;
1319
1320 list_for_each_entry(pin, &hdmi->pin_list, head) {
1321 if (pin->nid == pin_nid)
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301322 hdac_hdmi_present_sense(pin);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301323 }
1324}
1325
1326static struct i915_audio_component_audio_ops aops = {
1327 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1328};
1329
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301330static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
1331 int device)
1332{
1333 struct snd_soc_pcm_runtime *rtd;
1334
1335 list_for_each_entry(rtd, &card->rtd_list, list) {
1336 if (rtd->pcm && (rtd->pcm->device == device))
1337 return rtd->pcm;
1338 }
1339
1340 return NULL;
1341}
1342
Jeeja KP4a3478d2016-02-12 07:46:06 +05301343int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1344{
1345 char jack_name[NAME_SIZE];
1346 struct snd_soc_codec *codec = dai->codec;
1347 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1348 struct snd_soc_dapm_context *dapm =
1349 snd_soc_component_get_dapm(&codec->component);
1350 struct hdac_hdmi_priv *hdmi = edev->private_data;
1351 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301352 struct snd_pcm *snd_pcm;
1353 int err;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301354
1355 /*
1356 * this is a new PCM device, create new pcm and
1357 * add to the pcm list
1358 */
1359 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1360 if (!pcm)
1361 return -ENOMEM;
1362 pcm->pcm_id = device;
1363 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1364
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301365 snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
1366 if (snd_pcm) {
1367 err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
1368 if (err < 0) {
1369 dev_err(&edev->hdac.dev,
1370 "chmap control add failed with err: %d for pcm: %d\n",
1371 err, device);
1372 kfree(pcm);
1373 return err;
1374 }
1375 }
1376
Jeeja KP4a3478d2016-02-12 07:46:06 +05301377 list_add_tail(&pcm->head, &hdmi->pcm_list);
1378
1379 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1380
1381 return snd_jack_new(dapm->card->snd_card, jack_name,
1382 SND_JACK_AVOUT, &pcm->jack, true, false);
1383}
1384EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1385
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301386static int hdmi_codec_probe(struct snd_soc_codec *codec)
1387{
1388 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1389 struct hdac_hdmi_priv *hdmi = edev->private_data;
1390 struct snd_soc_dapm_context *dapm =
1391 snd_soc_component_get_dapm(&codec->component);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301392 struct hdac_hdmi_pin *pin;
Vinod Koulb2047e92016-05-12 08:58:55 +05301393 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301394 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301395
1396 edev->scodec = codec;
1397
Vinod Koulb2047e92016-05-12 08:58:55 +05301398 /*
1399 * hold the ref while we probe, also no need to drop the ref on
1400 * exit, we call pm_runtime_suspend() so that will do for us
1401 */
1402 hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301403 if (!hlink) {
1404 dev_err(&edev->hdac.dev, "hdac link not found\n");
1405 return -EIO;
1406 }
1407
Vinod Koulb2047e92016-05-12 08:58:55 +05301408 snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1409
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301410 ret = create_fill_widget_route_map(dapm);
1411 if (ret < 0)
1412 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301413
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301414 aops.audio_ptr = edev;
1415 ret = snd_hdac_i915_register_notifier(&aops);
1416 if (ret < 0) {
1417 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1418 ret);
1419 return ret;
1420 }
1421
1422 list_for_each_entry(pin, &hdmi->pin_list, head)
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301423 hdac_hdmi_present_sense(pin);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301424
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301425 /* Imp: Store the card pointer in hda_codec */
1426 edev->card = dapm->card->snd_card;
1427
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301428 /*
1429 * hdac_device core already sets the state to active and calls
1430 * get_noresume. So enable runtime and set the device to suspend.
1431 */
1432 pm_runtime_enable(&edev->hdac.dev);
1433 pm_runtime_put(&edev->hdac.dev);
1434 pm_runtime_suspend(&edev->hdac.dev);
1435
1436 return 0;
1437}
1438
1439static int hdmi_codec_remove(struct snd_soc_codec *codec)
1440{
1441 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1442
1443 pm_runtime_disable(&edev->hdac.dev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301444 return 0;
1445}
1446
Jeeja KP571d5072016-02-22 07:50:33 +05301447#ifdef CONFIG_PM
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301448static int hdmi_codec_prepare(struct device *dev)
1449{
1450 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1451 struct hdac_device *hdac = &edev->hdac;
1452
1453 pm_runtime_get_sync(&edev->hdac.dev);
1454
1455 /*
1456 * Power down afg.
1457 * codec_read is preferred over codec_write to set the power state.
1458 * This way verb is send to set the power state and response
1459 * is received. So setting power state is ensured without using loop
1460 * to read the state.
1461 */
1462 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1463 AC_PWRST_D3);
1464
1465 return 0;
1466}
1467
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301468static void hdmi_codec_complete(struct device *dev)
Jeeja KP571d5072016-02-22 07:50:33 +05301469{
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301470 struct hdac_ext_device *edev = to_hda_ext_device(dev);
Jeeja KP571d5072016-02-22 07:50:33 +05301471 struct hdac_hdmi_priv *hdmi = edev->private_data;
1472 struct hdac_hdmi_pin *pin;
1473 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301474
1475 /* Power up afg */
1476 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1477 AC_PWRST_D0);
Jeeja KP571d5072016-02-22 07:50:33 +05301478
1479 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1480 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1481
Jeeja KP571d5072016-02-22 07:50:33 +05301482 /*
1483 * As the ELD notify callback request is not entertained while the
1484 * device is in suspend state. Need to manually check detection of
1485 * all pins here.
1486 */
1487 list_for_each_entry(pin, &hdmi->pin_list, head)
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301488 hdac_hdmi_present_sense(pin);
Jeeja KP571d5072016-02-22 07:50:33 +05301489
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301490 pm_runtime_put_sync(&edev->hdac.dev);
Jeeja KP571d5072016-02-22 07:50:33 +05301491}
1492#else
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301493#define hdmi_codec_prepare NULL
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301494#define hdmi_codec_complete NULL
Jeeja KP571d5072016-02-22 07:50:33 +05301495#endif
1496
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301497static struct snd_soc_codec_driver hdmi_hda_codec = {
1498 .probe = hdmi_codec_probe,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301499 .remove = hdmi_codec_remove,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301500 .idle_bias_off = true,
1501};
1502
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301503static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
1504 unsigned char *chmap)
1505{
1506 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1507 struct hdac_hdmi_priv *hdmi = edev->private_data;
1508 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1509 struct hdac_hdmi_pin *pin = pcm->pin;
1510
1511 /* chmap is already set to 0 in caller */
1512 if (!pin)
1513 return;
1514
1515 memcpy(chmap, pin->chmap, ARRAY_SIZE(pin->chmap));
1516}
1517
1518static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
1519 unsigned char *chmap, int prepared)
1520{
1521 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1522 struct hdac_hdmi_priv *hdmi = edev->private_data;
1523 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1524 struct hdac_hdmi_pin *pin = pcm->pin;
1525
1526 mutex_lock(&pin->lock);
1527 pin->chmap_set = true;
1528 memcpy(pin->chmap, chmap, ARRAY_SIZE(pin->chmap));
1529 if (prepared)
1530 hdac_hdmi_setup_audio_infoframe(edev, pcm->cvt->nid, pin->nid);
1531 mutex_unlock(&pin->lock);
1532}
1533
1534static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
1535{
1536 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1537 struct hdac_hdmi_priv *hdmi = edev->private_data;
1538 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1539 struct hdac_hdmi_pin *pin = pcm->pin;
1540
1541 return pin ? true:false;
1542}
1543
1544static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
1545{
1546 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1547 struct hdac_hdmi_priv *hdmi = edev->private_data;
1548 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1549 struct hdac_hdmi_pin *pin = pcm->pin;
1550
Dan Carpenter8f658812016-05-03 10:42:58 +03001551 if (!pin || !pin->eld.eld_valid)
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301552 return 0;
1553
1554 return pin->eld.info.spk_alloc;
1555}
1556
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301557static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1558{
1559 struct hdac_device *codec = &edev->hdac;
1560 struct hdac_hdmi_priv *hdmi_priv;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301561 struct snd_soc_dai_driver *hdmi_dais = NULL;
Vinod Koulb2047e92016-05-12 08:58:55 +05301562 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301563 int num_dais = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301564 int ret = 0;
1565
Vinod Koulb2047e92016-05-12 08:58:55 +05301566 /* hold the ref while we probe */
1567 hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301568 if (!hlink) {
1569 dev_err(&edev->hdac.dev, "hdac link not found\n");
1570 return -EIO;
1571 }
1572
Vinod Koulb2047e92016-05-12 08:58:55 +05301573 snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1574
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301575 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1576 if (hdmi_priv == NULL)
1577 return -ENOMEM;
1578
1579 edev->private_data = hdmi_priv;
Subhransu S. Prustybcced702016-04-14 10:07:30 +05301580 snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301581 hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
1582 hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
1583 hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
1584 hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301585
1586 dev_set_drvdata(&codec->dev, edev);
1587
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301588 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1589 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301590 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1591 mutex_init(&hdmi_priv->pin_mutex);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301592
Ramesh Babuaeaccef2016-02-17 21:34:01 +05301593 /*
1594 * Turned off in the runtime_suspend during the first explicit
1595 * pm_runtime_suspend call.
1596 */
1597 ret = snd_hdac_display_power(edev->hdac.bus, true);
1598 if (ret < 0) {
1599 dev_err(&edev->hdac.dev,
1600 "Cannot turn on display power on i915 err: %d\n",
1601 ret);
1602 return ret;
1603 }
1604
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301605 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1606 if (ret < 0) {
1607 dev_err(&codec->dev,
1608 "Failed in parse and map nid with err: %d\n", ret);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301609 return ret;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301610 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301611
1612 /* ASoC specific initialization */
Vinod Koulb2047e92016-05-12 08:58:55 +05301613 ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
1614 hdmi_dais, num_dais);
1615
1616 snd_hdac_ext_bus_link_put(edev->ebus, hlink);
1617
1618 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301619}
1620
1621static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1622{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301623 struct hdac_hdmi_priv *hdmi = edev->private_data;
1624 struct hdac_hdmi_pin *pin, *pin_next;
1625 struct hdac_hdmi_cvt *cvt, *cvt_next;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301626 struct hdac_hdmi_pcm *pcm, *pcm_next;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301627
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301628 snd_soc_unregister_codec(&edev->hdac.dev);
1629
Jeeja KP4a3478d2016-02-12 07:46:06 +05301630 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1631 pcm->cvt = NULL;
1632 pcm->pin = NULL;
1633 list_del(&pcm->head);
1634 kfree(pcm);
1635 }
1636
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301637 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1638 list_del(&cvt->head);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301639 kfree(cvt->name);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301640 kfree(cvt);
1641 }
1642
1643 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
1644 list_del(&pin->head);
1645 kfree(pin);
1646 }
1647
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301648 return 0;
1649}
1650
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301651#ifdef CONFIG_PM
1652static int hdac_hdmi_runtime_suspend(struct device *dev)
1653{
1654 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1655 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301656 struct hdac_bus *bus = hdac->bus;
Vinod Koulb2047e92016-05-12 08:58:55 +05301657 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1658 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301659 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301660
1661 dev_dbg(dev, "Enter: %s\n", __func__);
1662
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301663 /* controller may not have been initialized for the first time */
1664 if (!bus)
1665 return 0;
1666
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301667 /*
1668 * Power down afg.
1669 * codec_read is preferred over codec_write to set the power state.
1670 * This way verb is send to set the power state and response
1671 * is received. So setting power state is ensured without using loop
1672 * to read the state.
1673 */
1674 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1675 AC_PWRST_D3);
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301676 err = snd_hdac_display_power(bus, false);
1677 if (err < 0) {
1678 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1679 return err;
1680 }
1681
Vinod Koulb2047e92016-05-12 08:58:55 +05301682 hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301683 if (!hlink) {
1684 dev_err(dev, "hdac link not found\n");
1685 return -EIO;
1686 }
1687
Vinod Koulb2047e92016-05-12 08:58:55 +05301688 snd_hdac_ext_bus_link_put(ebus, hlink);
1689
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301690 return 0;
1691}
1692
1693static int hdac_hdmi_runtime_resume(struct device *dev)
1694{
1695 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1696 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301697 struct hdac_bus *bus = hdac->bus;
Vinod Koulb2047e92016-05-12 08:58:55 +05301698 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1699 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301700 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301701
1702 dev_dbg(dev, "Enter: %s\n", __func__);
1703
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301704 /* controller may not have been initialized for the first time */
1705 if (!bus)
1706 return 0;
1707
Vinod Koulb2047e92016-05-12 08:58:55 +05301708 hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301709 if (!hlink) {
1710 dev_err(dev, "hdac link not found\n");
1711 return -EIO;
1712 }
1713
Vinod Koulb2047e92016-05-12 08:58:55 +05301714 snd_hdac_ext_bus_link_get(ebus, hlink);
1715
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301716 err = snd_hdac_display_power(bus, true);
1717 if (err < 0) {
1718 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1719 return err;
1720 }
1721
Subhransu S. Prustyab85f5b2016-02-17 21:34:02 +05301722 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1723 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1724
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301725 /* Power up afg */
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301726 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1727 AC_PWRST_D0);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301728
1729 return 0;
1730}
1731#else
1732#define hdac_hdmi_runtime_suspend NULL
1733#define hdac_hdmi_runtime_resume NULL
1734#endif
1735
1736static const struct dev_pm_ops hdac_hdmi_pm = {
1737 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301738 .prepare = hdmi_codec_prepare,
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301739 .complete = hdmi_codec_complete,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301740};
1741
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301742static const struct hda_device_id hdmi_list[] = {
1743 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
Jeeja KPe2304802016-03-11 10:12:55 +05301744 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
Shreyas NCcc216882016-07-11 22:02:09 +05301745 HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301746 {}
1747};
1748
1749MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1750
1751static struct hdac_ext_driver hdmi_driver = {
1752 . hdac = {
1753 .driver = {
1754 .name = "HDMI HDA Codec",
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301755 .pm = &hdac_hdmi_pm,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301756 },
1757 .id_table = hdmi_list,
1758 },
1759 .probe = hdac_hdmi_dev_probe,
1760 .remove = hdac_hdmi_dev_remove,
1761};
1762
1763static int __init hdmi_init(void)
1764{
1765 return snd_hda_ext_driver_register(&hdmi_driver);
1766}
1767
1768static void __exit hdmi_exit(void)
1769{
1770 snd_hda_ext_driver_unregister(&hdmi_driver);
1771}
1772
1773module_init(hdmi_init);
1774module_exit(hdmi_exit);
1775
1776MODULE_LICENSE("GPL v2");
1777MODULE_DESCRIPTION("HDMI HD codec");
1778MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1779MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");