blob: 05af2299579b04c31bab63d27027d1abc20321cc [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
Jeeja KP754695f2017-02-06 12:09:14 +053045#define HDA_MAX_PORTS 3
Subhransu S. Prusty148569f2016-02-12 07:46:07 +053046
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053047#define ELD_MAX_SIZE 256
48#define ELD_FIXED_BYTES 20
49
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +053050#define ELD_VER_CEA_861D 2
51#define ELD_VER_PARTIAL 31
52#define ELD_MAX_MNL 16
53
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053054struct hdac_hdmi_cvt_params {
55 unsigned int channels_min;
56 unsigned int channels_max;
57 u32 rates;
58 u64 formats;
59 unsigned int maxbps;
60};
61
62struct hdac_hdmi_cvt {
Subhransu S. Prusty15b91442015-12-09 21:46:10 +053063 struct list_head head;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053064 hda_nid_t nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +053065 const char *name;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053066 struct hdac_hdmi_cvt_params params;
67};
68
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +053069/* Currently only spk_alloc, more to be added */
70struct hdac_hdmi_parsed_eld {
71 u8 spk_alloc;
72};
73
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053074struct hdac_hdmi_eld {
75 bool monitor_present;
76 bool eld_valid;
77 int eld_size;
78 char eld_buffer[ELD_MAX_SIZE];
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +053079 struct hdac_hdmi_parsed_eld info;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053080};
81
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053082struct hdac_hdmi_pin {
Subhransu S. Prusty15b91442015-12-09 21:46:10 +053083 struct list_head head;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053084 hda_nid_t nid;
Jeeja KP2acd8302017-02-06 12:09:18 +053085 bool mst_capable;
Jeeja KP754695f2017-02-06 12:09:14 +053086 struct hdac_hdmi_port *ports;
87 int num_ports;
88 struct hdac_ext_device *edev;
89};
90
91struct hdac_hdmi_port {
Jeeja KPe0e5d3e2017-02-07 19:09:48 +053092 struct list_head head;
Jeeja KP754695f2017-02-06 12:09:14 +053093 int id;
94 struct hdac_hdmi_pin *pin;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053095 int num_mux_nids;
96 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053097 struct hdac_hdmi_eld eld;
Jeeja KP0324e512017-02-07 19:09:55 +053098 const char *jack_pin;
99 struct snd_soc_dapm_context *dapm;
100 const char *output_pin;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530101};
102
Jeeja KP4a3478d2016-02-12 07:46:06 +0530103struct hdac_hdmi_pcm {
104 struct list_head head;
105 int pcm_id;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530106 struct list_head port_list;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530107 struct hdac_hdmi_cvt *cvt;
Jeeja KP62490012017-02-07 19:09:49 +0530108 struct snd_soc_jack *jack;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530109 int stream_tag;
110 int channels;
111 int format;
Jeeja KPab1eea12017-01-24 21:49:05 +0530112 bool chmap_set;
113 unsigned char chmap[8]; /* ALSA API channel-map */
114 struct mutex lock;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530115 int jack_event;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530116};
117
Jeeja KP754695f2017-02-06 12:09:14 +0530118struct hdac_hdmi_dai_port_map {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530119 int dai_id;
Jeeja KP754695f2017-02-06 12:09:14 +0530120 struct hdac_hdmi_port *port;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530121 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530122};
123
Pradeep Tewani5622bc92017-07-20 11:40:56 +0530124struct hdac_hdmi_drv_data {
125 unsigned int vendor_nid;
126};
127
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530128struct hdac_hdmi_priv {
Jeeja KP754695f2017-02-06 12:09:14 +0530129 struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530130 struct list_head pin_list;
131 struct list_head cvt_list;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530132 struct list_head pcm_list;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530133 int num_pin;
134 int num_cvt;
Jeeja KP754695f2017-02-06 12:09:14 +0530135 int num_ports;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530136 struct mutex pin_mutex;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530137 struct hdac_chmap chmap;
Pradeep Tewani5622bc92017-07-20 11:40:56 +0530138 struct hdac_hdmi_drv_data *drv_data;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530139};
140
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +0530141#define hdev_to_hdmi_priv(_hdev) ((to_ehdac_device(_hdev))->private_data)
142
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530143static struct hdac_hdmi_pcm *
144hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
145 struct hdac_hdmi_cvt *cvt)
146{
147 struct hdac_hdmi_pcm *pcm = NULL;
Jeeja KP1de777f2017-01-10 17:57:48 +0530148
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530149 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
150 if (pcm->cvt == cvt)
151 break;
152 }
153
154 return pcm;
155}
Jeeja KP1de777f2017-01-10 17:57:48 +0530156
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530157static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
158 struct hdac_hdmi_port *port, bool is_connect)
159{
160 struct hdac_ext_device *edev = port->pin->edev;
161
Jeeja KP0324e512017-02-07 19:09:55 +0530162 if (is_connect)
163 snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
164 else
165 snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
166
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530167 if (is_connect) {
168 /*
169 * Report Jack connect event when a device is connected
170 * for the first time where same PCM is attached to multiple
171 * ports.
172 */
173 if (pcm->jack_event == 0) {
174 dev_dbg(&edev->hdac.dev,
175 "jack report for pcm=%d\n",
176 pcm->pcm_id);
Jeeja KP62490012017-02-07 19:09:49 +0530177 snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
178 SND_JACK_AVOUT);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530179 }
180 pcm->jack_event++;
181 } else {
182 /*
183 * Report Jack disconnect event when a device is disconnected
184 * is the only last connected device when same PCM is attached
185 * to multiple ports.
186 */
187 if (pcm->jack_event == 1)
Jeeja KP62490012017-02-07 19:09:49 +0530188 snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530189 if (pcm->jack_event > 0)
190 pcm->jack_event--;
191 }
Jeeja KP0324e512017-02-07 19:09:55 +0530192
193 snd_soc_dapm_sync(port->dapm);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530194}
195
Jeeja KPfc181b02017-02-07 19:09:45 +0530196/* MST supported verbs */
197/*
198 * Get the no devices that can be connected to a port on the Pin widget.
199 */
200static int hdac_hdmi_get_port_len(struct hdac_ext_device *hdac, hda_nid_t nid)
201{
202 unsigned int caps;
203 unsigned int type, param;
204
205 caps = get_wcaps(&hdac->hdac, nid);
206 type = get_wcaps_type(caps);
207
208 if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
209 return 0;
210
211 param = snd_hdac_read_parm_uncached(&hdac->hdac, nid,
212 AC_PAR_DEVLIST_LEN);
213 if (param == -1)
214 return param;
215
216 return param & AC_DEV_LIST_LEN_MASK;
217}
218
219/*
220 * Get the port entry select on the pin. Return the port entry
221 * id selected on the pin. Return 0 means the first port entry
222 * is selected or MST is not supported.
223 */
224static int hdac_hdmi_port_select_get(struct hdac_ext_device *hdac,
225 struct hdac_hdmi_port *port)
226{
227 return snd_hdac_codec_read(&hdac->hdac, port->pin->nid,
228 0, AC_VERB_GET_DEVICE_SEL, 0);
229}
230
231/*
232 * Sets the selected port entry for the configuring Pin widget verb.
233 * returns error if port set is not equal to port get otherwise success
234 */
235static int hdac_hdmi_port_select_set(struct hdac_ext_device *hdac,
236 struct hdac_hdmi_port *port)
237{
238 int num_ports;
239
240 if (!port->pin->mst_capable)
241 return 0;
242
243 /* AC_PAR_DEVLIST_LEN is 0 based. */
244 num_ports = hdac_hdmi_get_port_len(hdac, port->pin->nid);
245
246 if (num_ports < 0)
247 return -EIO;
248 /*
249 * Device List Length is a 0 based integer value indicating the
250 * number of sink device that a MST Pin Widget can support.
251 */
252 if (num_ports + 1 < port->id)
253 return 0;
254
255 snd_hdac_codec_write(&hdac->hdac, port->pin->nid, 0,
256 AC_VERB_SET_DEVICE_SEL, port->id);
257
258 if (port->id != hdac_hdmi_port_select_get(hdac, port))
259 return -EIO;
260
261 dev_dbg(&hdac->hdac.dev, "Selected the port=%d\n", port->id);
262
263 return 0;
264}
265
Subhransu S. Prusty28890992016-04-14 10:07:34 +0530266static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
267 int pcm_idx)
268{
269 struct hdac_hdmi_pcm *pcm;
270
271 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
272 if (pcm->pcm_id == pcm_idx)
273 return pcm;
274 }
275
276 return NULL;
277}
278
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530279static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
280{
Geliang Tang51b2c422015-12-28 22:47:13 +0800281 struct hdac_device *hdac = dev_to_hdac_dev(dev);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530282
Geliang Tang51b2c422015-12-28 22:47:13 +0800283 return to_ehdac_device(hdac);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530284}
285
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530286static unsigned int sad_format(const u8 *sad)
287{
288 return ((sad[0] >> 0x3) & 0x1f);
289}
290
291static unsigned int sad_sample_bits_lpcm(const u8 *sad)
292{
293 return (sad[2] & 7);
294}
295
296static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
297 void *eld)
298{
299 u64 formats = SNDRV_PCM_FMTBIT_S16;
300 int i;
301 const u8 *sad, *eld_buf = eld;
302
303 sad = drm_eld_sad(eld_buf);
304 if (!sad)
305 goto format_constraint;
306
307 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
308 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
309
310 /*
311 * the controller support 20 and 24 bits in 32 bit
312 * container so we set S32
313 */
314 if (sad_sample_bits_lpcm(sad) & 0x6)
315 formats |= SNDRV_PCM_FMTBIT_S32;
316 }
317 }
318
319format_constraint:
320 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
321 formats);
322
323}
324
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530325static void
326hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
327 int packet_index, int byte_index)
328{
329 int val;
330
331 val = (packet_index << 5) | (byte_index & 0x1f);
332
333 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
334 AC_VERB_SET_HDMI_DIP_INDEX, val);
335}
336
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530337struct dp_audio_infoframe {
338 u8 type; /* 0x84 */
339 u8 len; /* 0x1b */
340 u8 ver; /* 0x11 << 2 */
341
342 u8 CC02_CT47; /* match with HDMI infoframe from this on */
343 u8 SS01_SF24;
344 u8 CXT04;
345 u8 CA;
346 u8 LFEPBL01_LSV36_DM_INH7;
347};
348
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530349static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
Jeeja KP754695f2017-02-06 12:09:14 +0530350 struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530351{
352 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
353 struct hdmi_audio_infoframe frame;
Jeeja KP754695f2017-02-06 12:09:14 +0530354 struct hdac_hdmi_pin *pin = port->pin;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530355 struct dp_audio_infoframe dp_ai;
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +0530356 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&hdac->hdac);
Jeeja KPab1eea12017-01-24 21:49:05 +0530357 struct hdac_hdmi_cvt *cvt = pcm->cvt;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530358 u8 *dip;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530359 int ret;
360 int i;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530361 const u8 *eld_buf;
362 u8 conn_type;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530363 int channels, ca;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530364
Jeeja KP754695f2017-02-06 12:09:14 +0530365 ca = snd_hdac_channel_allocation(&hdac->hdac, port->eld.info.spk_alloc,
Jeeja KPab1eea12017-01-24 21:49:05 +0530366 pcm->channels, pcm->chmap_set, true, pcm->chmap);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530367
368 channels = snd_hdac_get_active_channels(ca);
Jeeja KPab1eea12017-01-24 21:49:05 +0530369 hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt->nid, channels);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530370
371 snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
Jeeja KPab1eea12017-01-24 21:49:05 +0530372 pcm->channels, pcm->chmap, pcm->chmap_set);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530373
Jeeja KP754695f2017-02-06 12:09:14 +0530374 eld_buf = port->eld.eld_buffer;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530375 conn_type = drm_eld_get_conn_type(eld_buf);
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530376
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530377 switch (conn_type) {
378 case DRM_ELD_CONN_TYPE_HDMI:
379 hdmi_audio_infoframe_init(&frame);
380
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530381 frame.channels = channels;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530382 frame.channel_allocation = ca;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530383
384 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
385 if (ret < 0)
386 return ret;
387
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530388 break;
389
390 case DRM_ELD_CONN_TYPE_DP:
391 memset(&dp_ai, 0, sizeof(dp_ai));
392 dp_ai.type = 0x84;
393 dp_ai.len = 0x1b;
394 dp_ai.ver = 0x11 << 2;
395 dp_ai.CC02_CT47 = channels - 1;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530396 dp_ai.CA = ca;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530397
398 dip = (u8 *)&dp_ai;
399 break;
400
401 default:
402 dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
403 conn_type);
404 return -EIO;
405 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530406
407 /* stop infoframe transmission */
Jeeja KPab1eea12017-01-24 21:49:05 +0530408 hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
409 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530410 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
411
412
413 /* Fill infoframe. Index auto-incremented */
Jeeja KPab1eea12017-01-24 21:49:05 +0530414 hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530415 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
Subhransu S. Prusty391005e2016-03-10 09:04:07 +0530416 for (i = 0; i < sizeof(buffer); i++)
Jeeja KPab1eea12017-01-24 21:49:05 +0530417 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
Subhransu S. Prusty391005e2016-03-10 09:04:07 +0530418 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530419 } else {
420 for (i = 0; i < sizeof(dp_ai); i++)
Jeeja KPab1eea12017-01-24 21:49:05 +0530421 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530422 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
423 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530424
425 /* Start infoframe */
Jeeja KPab1eea12017-01-24 21:49:05 +0530426 hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
427 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530428 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
429
430 return 0;
431}
432
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530433static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
434 unsigned int tx_mask, unsigned int rx_mask,
435 int slots, int slot_width)
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530436{
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530437 struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +0530438 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Jeeja KP754695f2017-02-06 12:09:14 +0530439 struct hdac_hdmi_dai_port_map *dai_map;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530440 struct hdac_hdmi_pcm *pcm;
441
442 dev_dbg(&edev->hdac.dev, "%s: strm_tag: %d\n", __func__, tx_mask);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530443
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530444 dai_map = &hdmi->dai_map[dai->id];
445
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530446 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530447
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530448 if (pcm)
449 pcm->stream_tag = (tx_mask << 4);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530450
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530451 return 0;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530452}
453
454static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
455 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
456{
457 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +0530458 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&hdac->hdac);
Jeeja KP754695f2017-02-06 12:09:14 +0530459 struct hdac_hdmi_dai_port_map *dai_map;
460 struct hdac_hdmi_port *port;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530461 struct hdac_hdmi_pcm *pcm;
462 int format;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530463
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530464 dai_map = &hdmi->dai_map[dai->id];
Jeeja KP754695f2017-02-06 12:09:14 +0530465 port = dai_map->port;
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530466
Jeeja KP754695f2017-02-06 12:09:14 +0530467 if (!port)
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530468 return -ENODEV;
469
Jeeja KP754695f2017-02-06 12:09:14 +0530470 if ((!port->eld.monitor_present) || (!port->eld.eld_valid)) {
471 dev_err(&hdac->hdac.dev,
472 "device is not configured for this pin:port%d:%d\n",
473 port->pin->nid, port->id);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530474 return -ENODEV;
475 }
476
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530477 format = snd_hdac_calc_stream_format(params_rate(hparams),
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530478 params_channels(hparams), params_format(hparams),
Jeeja KP66d6bbc2017-03-24 23:10:26 +0530479 dai->driver->playback.sig_bits, 0);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530480
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530481 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
482 if (!pcm)
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530483 return -EIO;
484
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530485 pcm->format = format;
486 pcm->channels = params_channels(hparams);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530487
488 return 0;
489}
490
Jeeja KP754695f2017-02-06 12:09:14 +0530491static int hdac_hdmi_query_port_connlist(struct hdac_ext_device *hdac,
492 struct hdac_hdmi_pin *pin,
493 struct hdac_hdmi_port *port)
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530494{
495 if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
496 dev_warn(&hdac->hdac.dev,
497 "HDMI: pin %d wcaps %#x does not support connection list\n",
498 pin->nid, get_wcaps(&hdac->hdac, pin->nid));
499 return -EINVAL;
500 }
501
Jeeja KP1b46ebd2017-02-07 19:09:47 +0530502 if (hdac_hdmi_port_select_set(hdac, port) < 0)
503 return -EIO;
504
Jeeja KP754695f2017-02-06 12:09:14 +0530505 port->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
506 port->mux_nids, HDA_MAX_CONNECTIONS);
507 if (port->num_mux_nids == 0)
508 dev_warn(&hdac->hdac.dev,
509 "No connections found for pin:port %d:%d\n",
510 pin->nid, port->id);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530511
Jeeja KP754695f2017-02-06 12:09:14 +0530512 dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin:port %d:%d\n",
513 port->num_mux_nids, pin->nid, port->id);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530514
Jeeja KP754695f2017-02-06 12:09:14 +0530515 return port->num_mux_nids;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530516}
517
518/*
Jeeja KP754695f2017-02-06 12:09:14 +0530519 * Query pcm list and return port to which stream is routed.
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530520 *
Jeeja KP754695f2017-02-06 12:09:14 +0530521 * Also query connection list of the pin, to validate the cvt to port map.
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530522 *
Jeeja KP754695f2017-02-06 12:09:14 +0530523 * Same stream rendering to multiple ports simultaneously can be done
524 * possibly, but not supported for now in driver. So return the first port
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530525 * connected.
526 */
Jeeja KP754695f2017-02-06 12:09:14 +0530527static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530528 struct hdac_ext_device *edev,
529 struct hdac_hdmi_priv *hdmi,
530 struct hdac_hdmi_cvt *cvt)
531{
532 struct hdac_hdmi_pcm *pcm;
Jeeja KP754695f2017-02-06 12:09:14 +0530533 struct hdac_hdmi_port *port = NULL;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530534 int ret, i;
535
536 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
537 if (pcm->cvt == cvt) {
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530538 if (list_empty(&pcm->port_list))
539 continue;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530540
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530541 list_for_each_entry(port, &pcm->port_list, head) {
542 mutex_lock(&pcm->lock);
543 ret = hdac_hdmi_query_port_connlist(edev,
544 port->pin, port);
545 mutex_unlock(&pcm->lock);
546 if (ret < 0)
547 continue;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530548
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530549 for (i = 0; i < port->num_mux_nids; i++) {
550 if (port->mux_nids[i] == cvt->nid &&
551 port->eld.monitor_present &&
552 port->eld.eld_valid)
553 return port;
554 }
555 }
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530556 }
557 }
558
559 return NULL;
560}
561
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530562/*
563 * This tries to get a valid pin and set the HW constraints based on the
564 * ELD. Even if a valid pin is not found return success so that device open
565 * doesn't fail.
566 */
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530567static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
568 struct snd_soc_dai *dai)
569{
570 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +0530571 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&hdac->hdac);
Jeeja KP754695f2017-02-06 12:09:14 +0530572 struct hdac_hdmi_dai_port_map *dai_map;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530573 struct hdac_hdmi_cvt *cvt;
Jeeja KP754695f2017-02-06 12:09:14 +0530574 struct hdac_hdmi_port *port;
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530575 int ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530576
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530577 dai_map = &hdmi->dai_map[dai->id];
578
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530579 cvt = dai_map->cvt;
Jeeja KP754695f2017-02-06 12:09:14 +0530580 port = hdac_hdmi_get_port_from_cvt(hdac, hdmi, cvt);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530581
582 /*
583 * To make PA and other userland happy.
584 * userland scans devices so returning error does not help.
585 */
Jeeja KP754695f2017-02-06 12:09:14 +0530586 if (!port)
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530587 return 0;
Jeeja KP754695f2017-02-06 12:09:14 +0530588 if ((!port->eld.monitor_present) ||
589 (!port->eld.eld_valid)) {
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530590
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530591 dev_warn(&hdac->hdac.dev,
Jeeja KP754695f2017-02-06 12:09:14 +0530592 "Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
593 port->eld.monitor_present, port->eld.eld_valid,
594 port->pin->nid, port->id);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530595
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530596 return 0;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530597 }
598
Jeeja KP754695f2017-02-06 12:09:14 +0530599 dai_map->port = port;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530600
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530601 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
Jeeja KP754695f2017-02-06 12:09:14 +0530602 port->eld.eld_buffer);
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530603 if (ret < 0)
604 return ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530605
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530606 return snd_pcm_hw_constraint_eld(substream->runtime,
Jeeja KP754695f2017-02-06 12:09:14 +0530607 port->eld.eld_buffer);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530608}
609
610static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
611 struct snd_soc_dai *dai)
612{
613 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +0530614 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&hdac->hdac);
Jeeja KP754695f2017-02-06 12:09:14 +0530615 struct hdac_hdmi_dai_port_map *dai_map;
Jeeja KPab1eea12017-01-24 21:49:05 +0530616 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530617
618 dai_map = &hdmi->dai_map[dai->id];
619
Jeeja KPab1eea12017-01-24 21:49:05 +0530620 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530621
Jeeja KPab1eea12017-01-24 21:49:05 +0530622 if (pcm) {
623 mutex_lock(&pcm->lock);
624 pcm->chmap_set = false;
625 memset(pcm->chmap, 0, sizeof(pcm->chmap));
626 pcm->channels = 0;
627 mutex_unlock(&pcm->lock);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530628 }
Jeeja KPab1eea12017-01-24 21:49:05 +0530629
Jeeja KP754695f2017-02-06 12:09:14 +0530630 if (dai_map->port)
631 dai_map->port = NULL;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530632}
633
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530634static int
635hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
636{
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530637 unsigned int chans;
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +0530638 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdac);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530639 int err;
640
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530641 chans = get_wcaps(hdac, cvt->nid);
642 chans = get_wcaps_channels(chans);
643
644 cvt->params.channels_min = 2;
645
646 cvt->params.channels_max = chans;
647 if (chans > hdmi->chmap.channels_max)
648 hdmi->chmap.channels_max = chans;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530649
650 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
651 &cvt->params.rates,
652 &cvt->params.formats,
653 &cvt->params.maxbps);
654 if (err < 0)
655 dev_err(&hdac->dev,
656 "Failed to query pcm params for nid %d: %d\n",
657 cvt->nid, err);
658
659 return err;
660}
661
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530662static int hdac_hdmi_fill_widget_info(struct device *dev,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530663 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
664 void *priv, const char *wname, const char *stream,
665 struct snd_kcontrol_new *wc, int numkc,
666 int (*event)(struct snd_soc_dapm_widget *,
667 struct snd_kcontrol *, int), unsigned short event_flags)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530668{
669 w->id = id;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530670 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
671 if (!w->name)
672 return -ENOMEM;
673
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530674 w->sname = stream;
675 w->reg = SND_SOC_NOPM;
676 w->shift = 0;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530677 w->kcontrol_news = wc;
678 w->num_kcontrols = numkc;
679 w->priv = priv;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530680 w->event = event;
681 w->event_flags = event_flags;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530682
683 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530684}
685
686static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530687 const char *sink, const char *control, const char *src,
688 int (*handler)(struct snd_soc_dapm_widget *src,
689 struct snd_soc_dapm_widget *sink))
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530690{
691 route->sink = sink;
692 route->source = src;
693 route->control = control;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530694 route->connected = handler;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530695}
696
Jeeja KP4a3478d2016-02-12 07:46:06 +0530697static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
Jeeja KP754695f2017-02-06 12:09:14 +0530698 struct hdac_hdmi_port *port)
Jeeja KP4a3478d2016-02-12 07:46:06 +0530699{
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +0530700 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Jeeja KP4a3478d2016-02-12 07:46:06 +0530701 struct hdac_hdmi_pcm *pcm = NULL;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530702 struct hdac_hdmi_port *p;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530703
704 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530705 if (list_empty(&pcm->port_list))
Jeeja KP754695f2017-02-06 12:09:14 +0530706 continue;
707
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530708 list_for_each_entry(p, &pcm->port_list, head) {
709 if (p->id == port->id && port->pin == p->pin)
710 return pcm;
711 }
Jeeja KP4a3478d2016-02-12 07:46:06 +0530712 }
713
714 return NULL;
715}
716
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530717static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
718 hda_nid_t nid, unsigned int pwr_state)
719{
720 if (get_wcaps(&edev->hdac, nid) & AC_WCAP_POWER) {
721 if (!snd_hdac_check_power_state(&edev->hdac, nid, pwr_state))
722 snd_hdac_codec_write(&edev->hdac, nid, 0,
723 AC_VERB_SET_POWER_STATE, pwr_state);
724 }
725}
726
727static void hdac_hdmi_set_amp(struct hdac_ext_device *edev,
728 hda_nid_t nid, int val)
729{
730 if (get_wcaps(&edev->hdac, nid) & AC_WCAP_OUT_AMP)
731 snd_hdac_codec_write(&edev->hdac, nid, 0,
732 AC_VERB_SET_AMP_GAIN_MUTE, val);
733}
734
735
736static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
737 struct snd_kcontrol *kc, int event)
738{
Jeeja KP754695f2017-02-06 12:09:14 +0530739 struct hdac_hdmi_port *port = w->priv;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530740 struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
741 struct hdac_hdmi_pcm *pcm;
742
743 dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
744 __func__, w->name, event);
745
Jeeja KP754695f2017-02-06 12:09:14 +0530746 pcm = hdac_hdmi_get_pcm(edev, port);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530747 if (!pcm)
748 return -EIO;
749
Jeeja KP1b46ebd2017-02-07 19:09:47 +0530750 /* set the device if pin is mst_capable */
751 if (hdac_hdmi_port_select_set(edev, port) < 0)
752 return -EIO;
753
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530754 switch (event) {
755 case SND_SOC_DAPM_PRE_PMU:
Jeeja KP754695f2017-02-06 12:09:14 +0530756 hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D0);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530757
758 /* Enable out path for this pin widget */
Jeeja KP754695f2017-02-06 12:09:14 +0530759 snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530760 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
761
Jeeja KP754695f2017-02-06 12:09:14 +0530762 hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_UNMUTE);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530763
Jeeja KP754695f2017-02-06 12:09:14 +0530764 return hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530765
766 case SND_SOC_DAPM_POST_PMD:
Jeeja KP754695f2017-02-06 12:09:14 +0530767 hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_MUTE);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530768
769 /* Disable out path for this pin widget */
Jeeja KP754695f2017-02-06 12:09:14 +0530770 snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530771 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
772
Jeeja KP754695f2017-02-06 12:09:14 +0530773 hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D3);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530774 break;
775
776 }
777
778 return 0;
779}
780
781static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
782 struct snd_kcontrol *kc, int event)
783{
784 struct hdac_hdmi_cvt *cvt = w->priv;
785 struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +0530786 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530787 struct hdac_hdmi_pcm *pcm;
788
789 dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
790 __func__, w->name, event);
791
792 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
793 if (!pcm)
794 return -EIO;
795
796 switch (event) {
797 case SND_SOC_DAPM_PRE_PMU:
798 hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D0);
799
800 /* Enable transmission */
801 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
802 AC_VERB_SET_DIGI_CONVERT_1, 1);
803
804 /* Category Code (CC) to zero */
805 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
806 AC_VERB_SET_DIGI_CONVERT_2, 0);
807
808 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
809 AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
810 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
811 AC_VERB_SET_STREAM_FORMAT, pcm->format);
812 break;
813
814 case SND_SOC_DAPM_POST_PMD:
815 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
816 AC_VERB_SET_CHANNEL_STREAMID, 0);
817 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
818 AC_VERB_SET_STREAM_FORMAT, 0);
819
820 hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D3);
821 break;
822
823 }
824
825 return 0;
826}
827
828static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
829 struct snd_kcontrol *kc, int event)
830{
Jeeja KP754695f2017-02-06 12:09:14 +0530831 struct hdac_hdmi_port *port = w->priv;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530832 struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
833 int mux_idx;
834
835 dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
836 __func__, w->name, event);
837
838 if (!kc)
839 kc = w->kcontrols[0];
840
841 mux_idx = dapm_kcontrol_get_value(kc);
Jeeja KP1b46ebd2017-02-07 19:09:47 +0530842
843 /* set the device if pin is mst_capable */
844 if (hdac_hdmi_port_select_set(edev, port) < 0)
845 return -EIO;
846
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530847 if (mux_idx > 0) {
Jeeja KP754695f2017-02-06 12:09:14 +0530848 snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530849 AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
850 }
851
852 return 0;
853}
854
Jeeja KP4a3478d2016-02-12 07:46:06 +0530855/*
856 * Based on user selection, map the PINs with the PCMs.
857 */
Jeeja KP754695f2017-02-06 12:09:14 +0530858static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
Jeeja KP4a3478d2016-02-12 07:46:06 +0530859 struct snd_ctl_elem_value *ucontrol)
860{
861 int ret;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530862 struct hdac_hdmi_port *p, *p_next;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530863 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
864 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
865 struct snd_soc_dapm_context *dapm = w->dapm;
Jeeja KP754695f2017-02-06 12:09:14 +0530866 struct hdac_hdmi_port *port = w->priv;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530867 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +0530868 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Jeeja KP4a3478d2016-02-12 07:46:06 +0530869 struct hdac_hdmi_pcm *pcm = NULL;
870 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
871
872 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
873 if (ret < 0)
874 return ret;
875
Jeeja KP754695f2017-02-06 12:09:14 +0530876 if (port == NULL)
877 return -EINVAL;
878
Jeeja KP4a3478d2016-02-12 07:46:06 +0530879 mutex_lock(&hdmi->pin_mutex);
880 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530881 if (list_empty(&pcm->port_list))
882 continue;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530883
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530884 list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
885 if (p == port && p->id == port->id &&
886 p->pin == port->pin) {
887 hdac_hdmi_jack_report(pcm, port, false);
888 list_del(&p->head);
Jeeja KP4a3478d2016-02-12 07:46:06 +0530889 }
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530890 }
891 }
892
893 /*
894 * Jack status is not reported during device probe as the
895 * PCMs are not registered by then. So report it here.
896 */
897 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
898 if (!strcmp(cvt_name, pcm->cvt->name)) {
899 list_add_tail(&port->head, &pcm->port_list);
900 if (port->eld.monitor_present && port->eld.eld_valid) {
901 hdac_hdmi_jack_report(pcm, port, true);
902 mutex_unlock(&hdmi->pin_mutex);
903 return ret;
904 }
Jeeja KP4a3478d2016-02-12 07:46:06 +0530905 }
906 }
907 mutex_unlock(&hdmi->pin_mutex);
908
909 return ret;
910}
911
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530912/*
913 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
914 * the display driver seem to be programming the connection list for the pin
915 * widget runtime.
916 *
917 * So programming all the possible inputs for the mux, the user has to take
918 * care of selecting the right one and leaving all other inputs selected to
919 * "NONE"
920 */
Jeeja KP754695f2017-02-06 12:09:14 +0530921static int hdac_hdmi_create_pin_port_muxs(struct hdac_ext_device *edev,
922 struct hdac_hdmi_port *port,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530923 struct snd_soc_dapm_widget *widget,
924 const char *widget_name)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530925{
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +0530926 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Jeeja KP754695f2017-02-06 12:09:14 +0530927 struct hdac_hdmi_pin *pin = port->pin;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530928 struct snd_kcontrol_new *kc;
929 struct hdac_hdmi_cvt *cvt;
930 struct soc_enum *se;
931 char kc_name[NAME_SIZE];
932 char mux_items[NAME_SIZE];
933 /* To hold inputs to the Pin mux */
934 char *items[HDA_MAX_CONNECTIONS];
935 int i = 0;
936 int num_items = hdmi->num_cvt + 1;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530937
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530938 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
939 if (!kc)
940 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530941
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530942 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
943 if (!se)
944 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530945
Subhransu S. Prusty70e97a22017-11-07 16:16:24 +0530946 snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
947 pin->nid, port->id);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530948 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
949 if (!kc->name)
950 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530951
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530952 kc->private_value = (long)se;
953 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
954 kc->access = 0;
955 kc->info = snd_soc_info_enum_double;
Jeeja KP754695f2017-02-06 12:09:14 +0530956 kc->put = hdac_hdmi_set_pin_port_mux;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530957 kc->get = snd_soc_dapm_get_enum_double;
958
959 se->reg = SND_SOC_NOPM;
960
961 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
962 se->items = num_items;
963 se->mask = roundup_pow_of_two(se->items) - 1;
964
965 sprintf(mux_items, "NONE");
966 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
967 if (!items[i])
968 return -ENOMEM;
969
970 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
971 i++;
972 sprintf(mux_items, "cvt %d", cvt->nid);
973 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
974 if (!items[i])
975 return -ENOMEM;
976 }
977
978 se->texts = devm_kmemdup(&edev->hdac.dev, items,
979 (num_items * sizeof(char *)), GFP_KERNEL);
980 if (!se->texts)
981 return -ENOMEM;
982
983 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
Jeeja KP754695f2017-02-06 12:09:14 +0530984 snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530985 hdac_hdmi_pin_mux_widget_event,
986 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530987}
988
989/* Add cvt <- input <- mux route map */
990static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
991 struct snd_soc_dapm_widget *widgets,
992 struct snd_soc_dapm_route *route, int rindex)
993{
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +0530994 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530995 const struct snd_kcontrol_new *kc;
996 struct soc_enum *se;
Jeeja KP754695f2017-02-06 12:09:14 +0530997 int mux_index = hdmi->num_cvt + hdmi->num_ports;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530998 int i, j;
999
Jeeja KP754695f2017-02-06 12:09:14 +05301000 for (i = 0; i < hdmi->num_ports; i++) {
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301001 kc = widgets[mux_index].kcontrol_news;
1002 se = (struct soc_enum *)kc->private_value;
1003 for (j = 0; j < hdmi->num_cvt; j++) {
1004 hdac_hdmi_fill_route(&route[rindex],
1005 widgets[mux_index].name,
1006 se->texts[j + 1],
1007 widgets[j].name, NULL);
1008
1009 rindex++;
1010 }
1011
1012 mux_index++;
1013 }
1014}
1015
1016/*
1017 * Widgets are added in the below sequence
1018 * Converter widgets for num converters enumerated
Jeeja KP754695f2017-02-06 12:09:14 +05301019 * Pin-port widgets for num ports for Pins enumerated
1020 * Pin-port mux widgets to represent connenction list of pin widget
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301021 *
Jeeja KP754695f2017-02-06 12:09:14 +05301022 * For each port, one Mux and One output widget is added
1023 * Total widgets elements = num_cvt + (num_ports * 2);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301024 *
1025 * Routes are added as below:
Jeeja KP754695f2017-02-06 12:09:14 +05301026 * pin-port mux -> pin (based on num_ports)
1027 * cvt -> "Input sel control" -> pin-port_mux
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301028 *
1029 * Total route elements:
Jeeja KP754695f2017-02-06 12:09:14 +05301030 * num_ports + (pin_muxes * num_cvt)
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301031 */
1032static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
1033{
1034 struct snd_soc_dapm_widget *widgets;
1035 struct snd_soc_dapm_route *route;
1036 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301037 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301038 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
1039 char widget_name[NAME_SIZE];
1040 struct hdac_hdmi_cvt *cvt;
1041 struct hdac_hdmi_pin *pin;
Jeeja KP754695f2017-02-06 12:09:14 +05301042 int ret, i = 0, num_routes = 0, j;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301043
1044 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
1045 return -EINVAL;
1046
Jeeja KP754695f2017-02-06 12:09:14 +05301047 widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1048 ((2 * hdmi->num_ports) + hdmi->num_cvt)),
1049 GFP_KERNEL);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301050
1051 if (!widgets)
1052 return -ENOMEM;
1053
1054 /* DAPM widgets to represent each converter widget */
1055 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1056 sprintf(widget_name, "Converter %d", cvt->nid);
1057 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
Jeeja KPc9bfb5d2017-01-24 21:49:03 +05301058 snd_soc_dapm_aif_in, cvt,
1059 widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1060 hdac_hdmi_cvt_output_widget_event,
1061 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301062 if (ret < 0)
1063 return ret;
1064 i++;
1065 }
1066
1067 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301068 for (j = 0; j < pin->num_ports; j++) {
1069 sprintf(widget_name, "hif%d-%d Output",
1070 pin->nid, pin->ports[j].id);
1071 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1072 snd_soc_dapm_output, &pin->ports[j],
1073 widget_name, NULL, NULL, 0,
1074 hdac_hdmi_pin_output_widget_event,
1075 SND_SOC_DAPM_PRE_PMU |
1076 SND_SOC_DAPM_POST_PMD);
1077 if (ret < 0)
1078 return ret;
Jeeja KP0324e512017-02-07 19:09:55 +05301079 pin->ports[j].output_pin = widgets[i].name;
Jeeja KP754695f2017-02-06 12:09:14 +05301080 i++;
1081 }
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301082 }
1083
1084 /* DAPM widgets to represent the connection list to pin widget */
1085 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301086 for (j = 0; j < pin->num_ports; j++) {
1087 sprintf(widget_name, "Pin%d-Port%d Mux",
1088 pin->nid, pin->ports[j].id);
1089 ret = hdac_hdmi_create_pin_port_muxs(edev,
1090 &pin->ports[j], &widgets[i],
1091 widget_name);
1092 if (ret < 0)
1093 return ret;
1094 i++;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301095
Jeeja KP754695f2017-02-06 12:09:14 +05301096 /* For cvt to pin_mux mapping */
1097 num_routes += hdmi->num_cvt;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301098
Jeeja KP754695f2017-02-06 12:09:14 +05301099 /* For pin_mux to pin mapping */
1100 num_routes++;
1101 }
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301102 }
1103
1104 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
1105 GFP_KERNEL);
1106 if (!route)
1107 return -ENOMEM;
1108
1109 i = 0;
1110 /* Add pin <- NULL <- mux route map */
1111 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301112 for (j = 0; j < pin->num_ports; j++) {
1113 int sink_index = i + hdmi->num_cvt;
1114 int src_index = sink_index + pin->num_ports *
1115 hdmi->num_pin;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301116
Jeeja KP754695f2017-02-06 12:09:14 +05301117 hdac_hdmi_fill_route(&route[i],
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301118 widgets[sink_index].name, NULL,
1119 widgets[src_index].name, NULL);
Jeeja KP754695f2017-02-06 12:09:14 +05301120 i++;
1121 }
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301122 }
1123
1124 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
1125
1126 snd_soc_dapm_new_controls(dapm, widgets,
Jeeja KP754695f2017-02-06 12:09:14 +05301127 ((2 * hdmi->num_ports) + hdmi->num_cvt));
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301128
1129 snd_soc_dapm_add_routes(dapm, route, num_routes);
1130 snd_soc_dapm_new_widgets(dapm->card);
1131
1132 return 0;
1133
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301134}
1135
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301136static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301137{
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301138 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Jeeja KP754695f2017-02-06 12:09:14 +05301139 struct hdac_hdmi_dai_port_map *dai_map;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301140 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301141 int dai_id = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301142
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301143 if (list_empty(&hdmi->cvt_list))
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301144 return -EINVAL;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301145
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301146 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1147 dai_map = &hdmi->dai_map[dai_id];
1148 dai_map->dai_id = dai_id;
1149 dai_map->cvt = cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301150
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301151 dai_id++;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301152
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301153 if (dai_id == HDA_MAX_CVTS) {
1154 dev_warn(&edev->hdac.dev,
1155 "Max dais supported: %d\n", dai_id);
1156 break;
1157 }
1158 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301159
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301160 return 0;
1161}
1162
1163static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
1164{
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301165 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301166 struct hdac_hdmi_cvt *cvt;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301167 char name[NAME_SIZE];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301168
1169 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
1170 if (!cvt)
1171 return -ENOMEM;
1172
1173 cvt->nid = nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301174 sprintf(name, "cvt %d", cvt->nid);
1175 cvt->name = kstrdup(name, GFP_KERNEL);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301176
1177 list_add_tail(&cvt->head, &hdmi->cvt_list);
1178 hdmi->num_cvt++;
1179
1180 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
1181}
1182
Jeeja KP754695f2017-02-06 12:09:14 +05301183static int hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1184 struct hdac_hdmi_port *port)
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301185{
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301186 unsigned int ver, mnl;
1187
Jeeja KP754695f2017-02-06 12:09:14 +05301188 ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301189 >> DRM_ELD_VER_SHIFT;
1190
1191 if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1192 dev_err(&edev->hdac.dev, "HDMI: Unknown ELD version %d\n", ver);
1193 return -EINVAL;
1194 }
1195
Jeeja KP754695f2017-02-06 12:09:14 +05301196 mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301197 DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1198
1199 if (mnl > ELD_MAX_MNL) {
1200 dev_err(&edev->hdac.dev, "HDMI: MNL Invalid %d\n", mnl);
1201 return -EINVAL;
1202 }
1203
Jeeja KP754695f2017-02-06 12:09:14 +05301204 port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301205
1206 return 0;
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301207}
1208
Jeeja KP754695f2017-02-06 12:09:14 +05301209static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1210 struct hdac_hdmi_port *port)
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301211{
1212 struct hdac_ext_device *edev = pin->edev;
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301213 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301214 struct hdac_hdmi_pcm *pcm;
Jeeja KP754695f2017-02-06 12:09:14 +05301215 int size = 0;
Jeeja KP2acd8302017-02-06 12:09:18 +05301216 int port_id = -1;
Jeeja KP754695f2017-02-06 12:09:14 +05301217
1218 if (!hdmi)
1219 return;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301220
Jeeja KP2acd8302017-02-06 12:09:18 +05301221 /*
1222 * In case of non MST pin, get_eld info API expectes port
1223 * to be -1.
1224 */
Jeeja KP4a3478d2016-02-12 07:46:06 +05301225 mutex_lock(&hdmi->pin_mutex);
Jeeja KP754695f2017-02-06 12:09:14 +05301226 port->eld.monitor_present = false;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301227
Jeeja KP2acd8302017-02-06 12:09:18 +05301228 if (pin->mst_capable)
1229 port_id = port->id;
1230
1231 size = snd_hdac_acomp_get_eld(&edev->hdac, pin->nid, port_id,
Jeeja KP754695f2017-02-06 12:09:14 +05301232 &port->eld.monitor_present,
1233 port->eld.eld_buffer,
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301234 ELD_MAX_SIZE);
1235
1236 if (size > 0) {
1237 size = min(size, ELD_MAX_SIZE);
Jeeja KP754695f2017-02-06 12:09:14 +05301238 if (hdac_hdmi_parse_eld(edev, port) < 0)
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301239 size = -EINVAL;
1240 }
1241
1242 if (size > 0) {
Jeeja KP754695f2017-02-06 12:09:14 +05301243 port->eld.eld_valid = true;
1244 port->eld.eld_size = size;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301245 } else {
Jeeja KP754695f2017-02-06 12:09:14 +05301246 port->eld.eld_valid = false;
1247 port->eld.eld_size = 0;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301248 }
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301249
Jeeja KP754695f2017-02-06 12:09:14 +05301250 pcm = hdac_hdmi_get_pcm(edev, port);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301251
Jeeja KP754695f2017-02-06 12:09:14 +05301252 if (!port->eld.monitor_present || !port->eld.eld_valid) {
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301253
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301254 dev_err(&edev->hdac.dev, "%s: disconnect for pin:port %d:%d\n",
Jeeja KP754695f2017-02-06 12:09:14 +05301255 __func__, pin->nid, port->id);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301256
1257 /*
1258 * PCMs are not registered during device probe, so don't
1259 * report jack here. It will be done in usermode mux
1260 * control select.
1261 */
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301262 if (pcm)
1263 hdac_hdmi_jack_report(pcm, port, false);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301264
1265 mutex_unlock(&hdmi->pin_mutex);
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301266 return;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301267 }
1268
Jeeja KP754695f2017-02-06 12:09:14 +05301269 if (port->eld.monitor_present && port->eld.eld_valid) {
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301270 if (pcm)
1271 hdac_hdmi_jack_report(pcm, port, true);
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301272
1273 print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
Jeeja KP754695f2017-02-06 12:09:14 +05301274 port->eld.eld_buffer, port->eld.eld_size, false);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301275
Jeeja KP754695f2017-02-06 12:09:14 +05301276 }
Jeeja KP4a3478d2016-02-12 07:46:06 +05301277 mutex_unlock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301278}
1279
Jeeja KP754695f2017-02-06 12:09:14 +05301280static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi,
1281 struct hdac_hdmi_pin *pin)
1282{
1283 struct hdac_hdmi_port *ports;
1284 int max_ports = HDA_MAX_PORTS;
1285 int i;
1286
1287 /*
1288 * FIXME: max_port may vary for each platform, so pass this as
1289 * as driver data or query from i915 interface when this API is
1290 * implemented.
1291 */
1292
1293 ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL);
1294 if (!ports)
1295 return -ENOMEM;
1296
1297 for (i = 0; i < max_ports; i++) {
1298 ports[i].id = i;
1299 ports[i].pin = pin;
1300 }
1301 pin->ports = ports;
1302 pin->num_ports = max_ports;
1303 return 0;
1304}
1305
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301306static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1307{
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301308 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301309 struct hdac_hdmi_pin *pin;
Jeeja KP754695f2017-02-06 12:09:14 +05301310 int ret;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301311
1312 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1313 if (!pin)
1314 return -ENOMEM;
1315
1316 pin->nid = nid;
Jeeja KP2acd8302017-02-06 12:09:18 +05301317 pin->mst_capable = false;
Jeeja KP754695f2017-02-06 12:09:14 +05301318 pin->edev = edev;
1319 ret = hdac_hdmi_add_ports(hdmi, pin);
1320 if (ret < 0)
1321 return ret;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301322
1323 list_add_tail(&pin->head, &hdmi->pin_list);
1324 hdmi->num_pin++;
Jeeja KP754695f2017-02-06 12:09:14 +05301325 hdmi->num_ports += pin->num_ports;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301326
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301327 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301328}
1329
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301330#define INTEL_VENDOR_NID 0x08
Pradeep Tewani5622bc92017-07-20 11:40:56 +05301331#define INTEL_GLK_VENDOR_NID 0x0b
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301332#define INTEL_GET_VENDOR_VERB 0xf81
1333#define INTEL_SET_VENDOR_VERB 0x781
1334#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1335#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1336
1337static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1338{
1339 unsigned int vendor_param;
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301340 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdac);
Pradeep Tewani5622bc92017-07-20 11:40:56 +05301341 unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301342
Pradeep Tewani5622bc92017-07-20 11:40:56 +05301343 vendor_param = snd_hdac_codec_read(hdac, vendor_nid, 0,
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301344 INTEL_GET_VENDOR_VERB, 0);
1345 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1346 return;
1347
1348 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
Pradeep Tewani5622bc92017-07-20 11:40:56 +05301349 vendor_param = snd_hdac_codec_read(hdac, vendor_nid, 0,
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301350 INTEL_SET_VENDOR_VERB, vendor_param);
1351 if (vendor_param == -1)
1352 return;
1353}
1354
1355static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1356{
1357 unsigned int vendor_param;
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301358 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdac);
Pradeep Tewani5622bc92017-07-20 11:40:56 +05301359 unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301360
Pradeep Tewani5622bc92017-07-20 11:40:56 +05301361 vendor_param = snd_hdac_codec_read(hdac, vendor_nid, 0,
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301362 INTEL_GET_VENDOR_VERB, 0);
1363 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1364 return;
1365
1366 /* enable DP1.2 mode */
1367 vendor_param |= INTEL_EN_DP12;
Pradeep Tewani5622bc92017-07-20 11:40:56 +05301368 vendor_param = snd_hdac_codec_read(hdac, vendor_nid, 0,
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301369 INTEL_SET_VENDOR_VERB, vendor_param);
1370 if (vendor_param == -1)
1371 return;
1372
1373}
1374
Gustavo A. R. Silva61b3b3c2017-07-13 15:38:38 -05001375static const struct snd_soc_dai_ops hdmi_dai_ops = {
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301376 .startup = hdac_hdmi_pcm_open,
1377 .shutdown = hdac_hdmi_pcm_close,
1378 .hw_params = hdac_hdmi_set_hw_params,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +05301379 .set_tdm_slot = hdac_hdmi_set_tdm_slot,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301380};
1381
1382/*
1383 * Each converter can support a stream independently. So a dai is created
1384 * based on the number of converter queried.
1385 */
1386static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1387 struct snd_soc_dai_driver **dais,
1388 struct hdac_hdmi_priv *hdmi, int num_dais)
1389{
1390 struct snd_soc_dai_driver *hdmi_dais;
1391 struct hdac_hdmi_cvt *cvt;
1392 char name[NAME_SIZE], dai_name[NAME_SIZE];
1393 int i = 0;
1394 u32 rates, bps;
1395 unsigned int rate_max = 384000, rate_min = 8000;
1396 u64 formats;
1397 int ret;
1398
1399 hdmi_dais = devm_kzalloc(&hdac->dev,
1400 (sizeof(*hdmi_dais) * num_dais),
1401 GFP_KERNEL);
1402 if (!hdmi_dais)
1403 return -ENOMEM;
1404
1405 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1406 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1407 &rates, &formats, &bps);
1408 if (ret)
1409 return ret;
1410
1411 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1412 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1413 dai_name, GFP_KERNEL);
1414
1415 if (!hdmi_dais[i].name)
1416 return -ENOMEM;
1417
1418 snprintf(name, sizeof(name), "hifi%d", i+1);
1419 hdmi_dais[i].playback.stream_name =
1420 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1421 if (!hdmi_dais[i].playback.stream_name)
1422 return -ENOMEM;
1423
1424 /*
1425 * Set caps based on capability queried from the converter.
1426 * It will be constrained runtime based on ELD queried.
1427 */
1428 hdmi_dais[i].playback.formats = formats;
1429 hdmi_dais[i].playback.rates = rates;
1430 hdmi_dais[i].playback.rate_max = rate_max;
1431 hdmi_dais[i].playback.rate_min = rate_min;
1432 hdmi_dais[i].playback.channels_min = 2;
1433 hdmi_dais[i].playback.channels_max = 2;
Jeeja KP66d6bbc2017-03-24 23:10:26 +05301434 hdmi_dais[i].playback.sig_bits = bps;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301435 hdmi_dais[i].ops = &hdmi_dai_ops;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301436 i++;
1437 }
1438
1439 *dais = hdmi_dais;
1440
1441 return 0;
1442}
1443
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301444/*
1445 * Parse all nodes and store the cvt/pin nids in array
1446 * Add one time initialization for pin and cvt widgets
1447 */
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301448static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1449 struct snd_soc_dai_driver **dais, int *num_dais)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301450{
1451 hda_nid_t nid;
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301452 int i, num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301453 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty1c0a7de2017-11-07 16:16:26 +05301454 struct hdac_hdmi_cvt *temp_cvt, *cvt_next;
1455 struct hdac_hdmi_pin *temp_pin, *pin_next;
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301456 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdac);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301457 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301458
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301459 hdac_hdmi_skl_enable_all_pins(hdac);
1460 hdac_hdmi_skl_enable_dp12(hdac);
1461
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301462 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
Subhransu S. Prusty541140d2015-12-09 21:46:08 +05301463 if (!nid || num_nodes <= 0) {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301464 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1465 return -EINVAL;
1466 }
1467
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301468 hdac->num_nodes = num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301469 hdac->start_nid = nid;
1470
1471 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1472 unsigned int caps;
1473 unsigned int type;
1474
1475 caps = get_wcaps(hdac, nid);
1476 type = get_wcaps_type(caps);
1477
1478 if (!(caps & AC_WCAP_DIGITAL))
1479 continue;
1480
1481 switch (type) {
1482
1483 case AC_WID_AUD_OUT:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301484 ret = hdac_hdmi_add_cvt(edev, nid);
1485 if (ret < 0)
Subhransu S. Prusty1c0a7de2017-11-07 16:16:26 +05301486 goto free_widgets;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301487 break;
1488
1489 case AC_WID_PIN:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301490 ret = hdac_hdmi_add_pin(edev, nid);
1491 if (ret < 0)
Subhransu S. Prusty1c0a7de2017-11-07 16:16:26 +05301492 goto free_widgets;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301493 break;
1494 }
1495 }
1496
1497 hdac->end_nid = nid;
1498
Subhransu S. Prusty1c0a7de2017-11-07 16:16:26 +05301499 if (!hdmi->num_pin || !hdmi->num_cvt) {
1500 ret = -EIO;
1501 goto free_widgets;
1502 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301503
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301504 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1505 if (ret) {
1506 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1507 ret);
Subhransu S. Prusty1c0a7de2017-11-07 16:16:26 +05301508 goto free_widgets;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301509 }
1510
1511 *num_dais = hdmi->num_cvt;
Subhransu S. Prusty1c0a7de2017-11-07 16:16:26 +05301512 ret = hdac_hdmi_init_dai_map(edev);
1513 if (ret < 0)
1514 goto free_widgets;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301515
Subhransu S. Prusty1c0a7de2017-11-07 16:16:26 +05301516 return ret;
1517
1518free_widgets:
1519 list_for_each_entry_safe(temp_cvt, cvt_next, &hdmi->cvt_list, head) {
1520 list_del(&temp_cvt->head);
1521 kfree(temp_cvt->name);
1522 kfree(temp_cvt);
1523 }
1524
1525 list_for_each_entry_safe(temp_pin, pin_next, &hdmi->pin_list, head) {
1526 for (i = 0; i < temp_pin->num_ports; i++)
1527 temp_pin->ports[i].pin = NULL;
1528 kfree(temp_pin->ports);
1529 list_del(&temp_pin->head);
1530 kfree(temp_pin);
1531 }
1532
1533 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301534}
1535
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -07001536static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301537{
1538 struct hdac_ext_device *edev = aptr;
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301539 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Jeeja KP754695f2017-02-06 12:09:14 +05301540 struct hdac_hdmi_pin *pin = NULL;
1541 struct hdac_hdmi_port *hport = NULL;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301542 struct snd_soc_codec *codec = edev->scodec;
Jeeja KP2acd8302017-02-06 12:09:18 +05301543 int i;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301544
1545 /* Don't know how this mapping is derived */
1546 hda_nid_t pin_nid = port + 0x04;
1547
Jeeja KP754695f2017-02-06 12:09:14 +05301548 dev_dbg(&edev->hdac.dev, "%s: for pin:%d port=%d\n", __func__,
1549 pin_nid, pipe);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301550
1551 /*
1552 * skip notification during system suspend (but not in runtime PM);
1553 * the state will be updated at resume. Also since the ELD and
1554 * connection states are updated in anyway at the end of the resume,
1555 * we can skip it when received during PM process.
1556 */
1557 if (snd_power_get_state(codec->component.card->snd_card) !=
1558 SNDRV_CTL_POWER_D0)
1559 return;
1560
1561 if (atomic_read(&edev->hdac.in_pm))
1562 return;
1563
1564 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301565 if (pin->nid != pin_nid)
1566 continue;
1567
1568 /* In case of non MST pin, pipe is -1 */
1569 if (pipe == -1) {
Jeeja KP2acd8302017-02-06 12:09:18 +05301570 pin->mst_capable = false;
Jeeja KP754695f2017-02-06 12:09:14 +05301571 /* if not MST, default is port[0] */
1572 hport = &pin->ports[0];
Jeeja KP2acd8302017-02-06 12:09:18 +05301573 } else {
1574 for (i = 0; i < pin->num_ports; i++) {
1575 pin->mst_capable = true;
1576 if (pin->ports[i].id == pipe) {
1577 hport = &pin->ports[i];
Jeeja KP04c8f2b2017-03-01 22:41:23 +05301578 break;
Jeeja KP2acd8302017-02-06 12:09:18 +05301579 }
1580 }
Jeeja KP754695f2017-02-06 12:09:14 +05301581 }
Jeeja KP04c8f2b2017-03-01 22:41:23 +05301582
1583 if (hport)
1584 hdac_hdmi_present_sense(pin, hport);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301585 }
Jeeja KP754695f2017-02-06 12:09:14 +05301586
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301587}
1588
1589static struct i915_audio_component_audio_ops aops = {
1590 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1591};
1592
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301593static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
1594 int device)
1595{
1596 struct snd_soc_pcm_runtime *rtd;
1597
1598 list_for_each_entry(rtd, &card->rtd_list, list) {
1599 if (rtd->pcm && (rtd->pcm->device == device))
1600 return rtd->pcm;
1601 }
1602
1603 return NULL;
1604}
1605
Jeeja KP0324e512017-02-07 19:09:55 +05301606/* create jack pin kcontrols */
1607static int create_fill_jack_kcontrols(struct snd_soc_card *card,
1608 struct hdac_ext_device *edev)
1609{
1610 struct hdac_hdmi_pin *pin;
1611 struct snd_kcontrol_new *kc;
1612 char kc_name[NAME_SIZE], xname[NAME_SIZE];
1613 char *name;
1614 int i = 0, j;
1615 struct snd_soc_codec *codec = edev->scodec;
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301616 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Jeeja KP0324e512017-02-07 19:09:55 +05301617
1618 kc = devm_kcalloc(codec->dev, hdmi->num_ports,
1619 sizeof(*kc), GFP_KERNEL);
1620
1621 if (!kc)
1622 return -ENOMEM;
1623
1624 list_for_each_entry(pin, &hdmi->pin_list, head) {
1625 for (j = 0; j < pin->num_ports; j++) {
1626 snprintf(xname, sizeof(xname), "hif%d-%d Jack",
1627 pin->nid, pin->ports[j].id);
1628 name = devm_kstrdup(codec->dev, xname, GFP_KERNEL);
1629 if (!name)
1630 return -ENOMEM;
1631 snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
1632 kc[i].name = devm_kstrdup(codec->dev, kc_name,
1633 GFP_KERNEL);
1634 if (!kc[i].name)
1635 return -ENOMEM;
1636
1637 kc[i].private_value = (unsigned long)name;
1638 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1639 kc[i].access = 0;
1640 kc[i].info = snd_soc_dapm_info_pin_switch;
1641 kc[i].put = snd_soc_dapm_put_pin_switch;
1642 kc[i].get = snd_soc_dapm_get_pin_switch;
1643 i++;
1644 }
1645 }
1646
1647 return snd_soc_add_card_controls(card, kc, i);
1648}
1649
1650int hdac_hdmi_jack_port_init(struct snd_soc_codec *codec,
1651 struct snd_soc_dapm_context *dapm)
1652{
1653 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301654 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Jeeja KP0324e512017-02-07 19:09:55 +05301655 struct hdac_hdmi_pin *pin;
1656 struct snd_soc_dapm_widget *widgets;
1657 struct snd_soc_dapm_route *route;
1658 char w_name[NAME_SIZE];
1659 int i = 0, j, ret;
1660
1661 widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
1662 sizeof(*widgets), GFP_KERNEL);
1663
1664 if (!widgets)
1665 return -ENOMEM;
1666
1667 route = devm_kcalloc(dapm->dev, hdmi->num_ports,
1668 sizeof(*route), GFP_KERNEL);
1669 if (!route)
1670 return -ENOMEM;
1671
1672 /* create Jack DAPM widget */
1673 list_for_each_entry(pin, &hdmi->pin_list, head) {
1674 for (j = 0; j < pin->num_ports; j++) {
1675 snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
1676 pin->nid, pin->ports[j].id);
1677
1678 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1679 snd_soc_dapm_spk, NULL,
1680 w_name, NULL, NULL, 0, NULL, 0);
1681 if (ret < 0)
1682 return ret;
1683
1684 pin->ports[j].jack_pin = widgets[i].name;
1685 pin->ports[j].dapm = dapm;
1686
1687 /* add to route from Jack widget to output */
1688 hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
1689 NULL, pin->ports[j].output_pin, NULL);
1690
1691 i++;
1692 }
1693 }
1694
1695 /* Add Route from Jack widget to the output widget */
1696 ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
1697 if (ret < 0)
1698 return ret;
1699
1700 ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
1701 if (ret < 0)
1702 return ret;
1703
1704 ret = snd_soc_dapm_new_widgets(dapm->card);
1705 if (ret < 0)
1706 return ret;
1707
1708 /* Add Jack Pin switch Kcontrol */
1709 ret = create_fill_jack_kcontrols(dapm->card, edev);
1710
1711 if (ret < 0)
1712 return ret;
1713
1714 /* default set the Jack Pin switch to OFF */
1715 list_for_each_entry(pin, &hdmi->pin_list, head) {
1716 for (j = 0; j < pin->num_ports; j++)
1717 snd_soc_dapm_disable_pin(pin->ports[j].dapm,
1718 pin->ports[j].jack_pin);
1719 }
1720
1721 return 0;
1722}
1723EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
1724
Jeeja KP62490012017-02-07 19:09:49 +05301725int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
1726 struct snd_soc_jack *jack)
Jeeja KP4a3478d2016-02-12 07:46:06 +05301727{
Jeeja KP4a3478d2016-02-12 07:46:06 +05301728 struct snd_soc_codec *codec = dai->codec;
1729 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301730 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301731 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301732 struct snd_pcm *snd_pcm;
1733 int err;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301734
1735 /*
1736 * this is a new PCM device, create new pcm and
1737 * add to the pcm list
1738 */
1739 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1740 if (!pcm)
1741 return -ENOMEM;
1742 pcm->pcm_id = device;
1743 pcm->cvt = hdmi->dai_map[dai->id].cvt;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301744 pcm->jack_event = 0;
Jeeja KP62490012017-02-07 19:09:49 +05301745 pcm->jack = jack;
Jeeja KPab1eea12017-01-24 21:49:05 +05301746 mutex_init(&pcm->lock);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301747 INIT_LIST_HEAD(&pcm->port_list);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301748 snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
1749 if (snd_pcm) {
1750 err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
1751 if (err < 0) {
1752 dev_err(&edev->hdac.dev,
1753 "chmap control add failed with err: %d for pcm: %d\n",
1754 err, device);
1755 kfree(pcm);
1756 return err;
1757 }
1758 }
1759
Jeeja KP4a3478d2016-02-12 07:46:06 +05301760 list_add_tail(&pcm->head, &hdmi->pcm_list);
1761
Jeeja KP62490012017-02-07 19:09:49 +05301762 return 0;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301763}
1764EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1765
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301766static void hdac_hdmi_present_sense_all_pins(struct hdac_ext_device *edev,
1767 struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1768{
1769 int i;
1770 struct hdac_hdmi_pin *pin;
1771
1772 list_for_each_entry(pin, &hdmi->pin_list, head) {
1773 if (detect_pin_caps) {
1774
1775 if (hdac_hdmi_get_port_len(edev, pin->nid) == 0)
1776 pin->mst_capable = false;
1777 else
1778 pin->mst_capable = true;
1779 }
1780
1781 for (i = 0; i < pin->num_ports; i++) {
1782 if (!pin->mst_capable && i > 0)
1783 continue;
1784
1785 hdac_hdmi_present_sense(pin, &pin->ports[i]);
1786 }
1787 }
1788}
1789
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301790static int hdmi_codec_probe(struct snd_soc_codec *codec)
1791{
1792 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301793 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301794 struct snd_soc_dapm_context *dapm =
1795 snd_soc_component_get_dapm(&codec->component);
Vinod Koulb2047e92016-05-12 08:58:55 +05301796 struct hdac_ext_link *hlink = NULL;
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301797 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301798
1799 edev->scodec = codec;
1800
Vinod Koulb2047e92016-05-12 08:58:55 +05301801 /*
1802 * hold the ref while we probe, also no need to drop the ref on
1803 * exit, we call pm_runtime_suspend() so that will do for us
1804 */
1805 hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301806 if (!hlink) {
1807 dev_err(&edev->hdac.dev, "hdac link not found\n");
1808 return -EIO;
1809 }
1810
Vinod Koulb2047e92016-05-12 08:58:55 +05301811 snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1812
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301813 ret = create_fill_widget_route_map(dapm);
1814 if (ret < 0)
1815 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301816
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301817 aops.audio_ptr = edev;
1818 ret = snd_hdac_i915_register_notifier(&aops);
1819 if (ret < 0) {
1820 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1821 ret);
1822 return ret;
1823 }
1824
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301825 hdac_hdmi_present_sense_all_pins(edev, hdmi, true);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301826 /* Imp: Store the card pointer in hda_codec */
1827 edev->card = dapm->card->snd_card;
1828
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301829 /*
1830 * hdac_device core already sets the state to active and calls
1831 * get_noresume. So enable runtime and set the device to suspend.
1832 */
1833 pm_runtime_enable(&edev->hdac.dev);
1834 pm_runtime_put(&edev->hdac.dev);
1835 pm_runtime_suspend(&edev->hdac.dev);
1836
1837 return 0;
1838}
1839
1840static int hdmi_codec_remove(struct snd_soc_codec *codec)
1841{
1842 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1843
1844 pm_runtime_disable(&edev->hdac.dev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301845 return 0;
1846}
1847
Jeeja KP571d5072016-02-22 07:50:33 +05301848#ifdef CONFIG_PM
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301849static int hdmi_codec_prepare(struct device *dev)
1850{
1851 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1852 struct hdac_device *hdac = &edev->hdac;
1853
1854 pm_runtime_get_sync(&edev->hdac.dev);
1855
1856 /*
1857 * Power down afg.
1858 * codec_read is preferred over codec_write to set the power state.
1859 * This way verb is send to set the power state and response
1860 * is received. So setting power state is ensured without using loop
1861 * to read the state.
1862 */
1863 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1864 AC_PWRST_D3);
1865
1866 return 0;
1867}
1868
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301869static void hdmi_codec_complete(struct device *dev)
Jeeja KP571d5072016-02-22 07:50:33 +05301870{
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301871 struct hdac_ext_device *edev = to_hda_ext_device(dev);
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301872 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Jeeja KP571d5072016-02-22 07:50:33 +05301873 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301874
1875 /* Power up afg */
1876 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1877 AC_PWRST_D0);
Jeeja KP571d5072016-02-22 07:50:33 +05301878
1879 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1880 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1881
Jeeja KP571d5072016-02-22 07:50:33 +05301882 /*
1883 * As the ELD notify callback request is not entertained while the
1884 * device is in suspend state. Need to manually check detection of
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301885 * all pins here. pin capablity change is not support, so use the
1886 * already set pin caps.
Jeeja KP571d5072016-02-22 07:50:33 +05301887 */
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301888 hdac_hdmi_present_sense_all_pins(edev, hdmi, false);
Jeeja KP571d5072016-02-22 07:50:33 +05301889
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301890 pm_runtime_put_sync(&edev->hdac.dev);
Jeeja KP571d5072016-02-22 07:50:33 +05301891}
1892#else
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301893#define hdmi_codec_prepare NULL
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301894#define hdmi_codec_complete NULL
Jeeja KP571d5072016-02-22 07:50:33 +05301895#endif
1896
Bhumika Goyala180ba42017-08-03 21:30:19 +05301897static const struct snd_soc_codec_driver hdmi_hda_codec = {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301898 .probe = hdmi_codec_probe,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301899 .remove = hdmi_codec_remove,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301900 .idle_bias_off = true,
1901};
1902
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301903static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
1904 unsigned char *chmap)
1905{
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301906 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdac);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301907 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301908
Jeeja KPab1eea12017-01-24 21:49:05 +05301909 memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301910}
1911
1912static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
1913 unsigned char *chmap, int prepared)
1914{
1915 struct hdac_ext_device *edev = to_ehdac_device(hdac);
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301916 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdac);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301917 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301918 struct hdac_hdmi_port *port;
1919
Subhransu S. Prustyeb50fa12017-11-07 16:16:25 +05301920 if (!pcm)
1921 return;
1922
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301923 if (list_empty(&pcm->port_list))
1924 return;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301925
Jeeja KPab1eea12017-01-24 21:49:05 +05301926 mutex_lock(&pcm->lock);
1927 pcm->chmap_set = true;
1928 memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301929 list_for_each_entry(port, &pcm->port_list, head)
1930 if (prepared)
1931 hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
Jeeja KPab1eea12017-01-24 21:49:05 +05301932 mutex_unlock(&pcm->lock);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301933}
1934
1935static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
1936{
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301937 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdac);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301938 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301939
Subhransu S. Prustyeb50fa12017-11-07 16:16:25 +05301940 if (!pcm)
1941 return false;
1942
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301943 if (list_empty(&pcm->port_list))
1944 return false;
1945
1946 return true;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301947}
1948
1949static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
1950{
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05301951 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdac);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301952 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301953 struct hdac_hdmi_port *port;
1954
Subhransu S. Prustyeb50fa12017-11-07 16:16:25 +05301955 if (!pcm)
1956 return 0;
1957
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301958 if (list_empty(&pcm->port_list))
1959 return 0;
1960
1961 port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1962
1963 if (!port)
1964 return 0;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301965
Jeeja KP754695f2017-02-06 12:09:14 +05301966 if (!port || !port->eld.eld_valid)
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301967 return 0;
1968
Jeeja KP754695f2017-02-06 12:09:14 +05301969 return port->eld.info.spk_alloc;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301970}
1971
Pradeep Tewani5622bc92017-07-20 11:40:56 +05301972static struct hdac_hdmi_drv_data intel_glk_drv_data = {
1973 .vendor_nid = INTEL_GLK_VENDOR_NID,
1974};
1975
1976static struct hdac_hdmi_drv_data intel_drv_data = {
1977 .vendor_nid = INTEL_VENDOR_NID,
1978};
1979
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301980static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1981{
1982 struct hdac_device *codec = &edev->hdac;
1983 struct hdac_hdmi_priv *hdmi_priv;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301984 struct snd_soc_dai_driver *hdmi_dais = NULL;
Vinod Koulb2047e92016-05-12 08:58:55 +05301985 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301986 int num_dais = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301987 int ret = 0;
Pradeep Tewani5622bc92017-07-20 11:40:56 +05301988 struct hdac_driver *hdrv = drv_to_hdac_driver(codec->dev.driver);
1989 const struct hda_device_id *hdac_id = hdac_get_device_id(codec, hdrv);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301990
Vinod Koulb2047e92016-05-12 08:58:55 +05301991 /* hold the ref while we probe */
1992 hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301993 if (!hlink) {
1994 dev_err(&edev->hdac.dev, "hdac link not found\n");
1995 return -EIO;
1996 }
1997
Vinod Koulb2047e92016-05-12 08:58:55 +05301998 snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1999
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302000 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
2001 if (hdmi_priv == NULL)
2002 return -ENOMEM;
2003
2004 edev->private_data = hdmi_priv;
Subhransu S. Prustybcced702016-04-14 10:07:30 +05302005 snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05302006 hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
2007 hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
2008 hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
2009 hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302010
Subhransu S. Prustyeb50fa12017-11-07 16:16:25 +05302011 if (!hdac_id)
2012 return -ENODEV;
2013
Pradeep Tewani5622bc92017-07-20 11:40:56 +05302014 if (hdac_id->driver_data)
2015 hdmi_priv->drv_data =
2016 (struct hdac_hdmi_drv_data *)hdac_id->driver_data;
2017 else
2018 hdmi_priv->drv_data = &intel_drv_data;
2019
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302020 dev_set_drvdata(&codec->dev, edev);
2021
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05302022 INIT_LIST_HEAD(&hdmi_priv->pin_list);
2023 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
Jeeja KP4a3478d2016-02-12 07:46:06 +05302024 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
2025 mutex_init(&hdmi_priv->pin_mutex);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05302026
Ramesh Babuaeaccef2016-02-17 21:34:01 +05302027 /*
2028 * Turned off in the runtime_suspend during the first explicit
2029 * pm_runtime_suspend call.
2030 */
2031 ret = snd_hdac_display_power(edev->hdac.bus, true);
2032 if (ret < 0) {
2033 dev_err(&edev->hdac.dev,
2034 "Cannot turn on display power on i915 err: %d\n",
2035 ret);
2036 return ret;
2037 }
2038
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05302039 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
2040 if (ret < 0) {
2041 dev_err(&codec->dev,
2042 "Failed in parse and map nid with err: %d\n", ret);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302043 return ret;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05302044 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302045
2046 /* ASoC specific initialization */
Vinod Koulb2047e92016-05-12 08:58:55 +05302047 ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
2048 hdmi_dais, num_dais);
2049
2050 snd_hdac_ext_bus_link_put(edev->ebus, hlink);
2051
2052 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302053}
2054
2055static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
2056{
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +05302057 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(&edev->hdac);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05302058 struct hdac_hdmi_pin *pin, *pin_next;
2059 struct hdac_hdmi_cvt *cvt, *cvt_next;
Jeeja KP4a3478d2016-02-12 07:46:06 +05302060 struct hdac_hdmi_pcm *pcm, *pcm_next;
Jeeja KP2fe42dd2017-03-01 22:41:24 +05302061 struct hdac_hdmi_port *port, *port_next;
Jeeja KP754695f2017-02-06 12:09:14 +05302062 int i;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05302063
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302064 snd_soc_unregister_codec(&edev->hdac.dev);
2065
Jeeja KP4a3478d2016-02-12 07:46:06 +05302066 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
2067 pcm->cvt = NULL;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05302068 if (list_empty(&pcm->port_list))
2069 continue;
2070
Jeeja KP2fe42dd2017-03-01 22:41:24 +05302071 list_for_each_entry_safe(port, port_next,
2072 &pcm->port_list, head)
2073 list_del(&port->head);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05302074
Jeeja KP4a3478d2016-02-12 07:46:06 +05302075 list_del(&pcm->head);
2076 kfree(pcm);
2077 }
2078
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05302079 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
2080 list_del(&cvt->head);
Jeeja KP4a3478d2016-02-12 07:46:06 +05302081 kfree(cvt->name);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05302082 kfree(cvt);
2083 }
2084
2085 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05302086 for (i = 0; i < pin->num_ports; i++)
2087 pin->ports[i].pin = NULL;
2088 kfree(pin->ports);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05302089 list_del(&pin->head);
2090 kfree(pin);
2091 }
2092
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302093 return 0;
2094}
2095
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05302096#ifdef CONFIG_PM
2097static int hdac_hdmi_runtime_suspend(struct device *dev)
2098{
2099 struct hdac_ext_device *edev = to_hda_ext_device(dev);
2100 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05302101 struct hdac_bus *bus = hdac->bus;
Vinod Koulb2047e92016-05-12 08:58:55 +05302102 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
2103 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05302104 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05302105
2106 dev_dbg(dev, "Enter: %s\n", __func__);
2107
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05302108 /* controller may not have been initialized for the first time */
2109 if (!bus)
2110 return 0;
2111
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05302112 /*
2113 * Power down afg.
2114 * codec_read is preferred over codec_write to set the power state.
2115 * This way verb is send to set the power state and response
2116 * is received. So setting power state is ensured without using loop
2117 * to read the state.
2118 */
2119 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
2120 AC_PWRST_D3);
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05302121 err = snd_hdac_display_power(bus, false);
2122 if (err < 0) {
2123 dev_err(bus->dev, "Cannot turn on display power on i915\n");
2124 return err;
2125 }
2126
Vinod Koulb2047e92016-05-12 08:58:55 +05302127 hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05302128 if (!hlink) {
2129 dev_err(dev, "hdac link not found\n");
2130 return -EIO;
2131 }
2132
Vinod Koulb2047e92016-05-12 08:58:55 +05302133 snd_hdac_ext_bus_link_put(ebus, hlink);
2134
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05302135 return 0;
2136}
2137
2138static int hdac_hdmi_runtime_resume(struct device *dev)
2139{
2140 struct hdac_ext_device *edev = to_hda_ext_device(dev);
2141 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05302142 struct hdac_bus *bus = hdac->bus;
Vinod Koulb2047e92016-05-12 08:58:55 +05302143 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
2144 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05302145 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05302146
2147 dev_dbg(dev, "Enter: %s\n", __func__);
2148
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05302149 /* controller may not have been initialized for the first time */
2150 if (!bus)
2151 return 0;
2152
Vinod Koulb2047e92016-05-12 08:58:55 +05302153 hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05302154 if (!hlink) {
2155 dev_err(dev, "hdac link not found\n");
2156 return -EIO;
2157 }
2158
Vinod Koulb2047e92016-05-12 08:58:55 +05302159 snd_hdac_ext_bus_link_get(ebus, hlink);
2160
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05302161 err = snd_hdac_display_power(bus, true);
2162 if (err < 0) {
2163 dev_err(bus->dev, "Cannot turn on display power on i915\n");
2164 return err;
2165 }
2166
Subhransu S. Prustyab85f5b2016-02-17 21:34:02 +05302167 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
2168 hdac_hdmi_skl_enable_dp12(&edev->hdac);
2169
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05302170 /* Power up afg */
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05302171 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
2172 AC_PWRST_D0);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05302173
2174 return 0;
2175}
2176#else
2177#define hdac_hdmi_runtime_suspend NULL
2178#define hdac_hdmi_runtime_resume NULL
2179#endif
2180
2181static const struct dev_pm_ops hdac_hdmi_pm = {
2182 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05302183 .prepare = hdmi_codec_prepare,
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05302184 .complete = hdmi_codec_complete,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05302185};
2186
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302187static const struct hda_device_id hdmi_list[] = {
2188 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
Jeeja KPe2304802016-03-11 10:12:55 +05302189 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
Shreyas NCcc216882016-07-11 22:02:09 +05302190 HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
Pradeep Tewani5622bc92017-07-20 11:40:56 +05302191 HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
2192 &intel_glk_drv_data),
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302193 {}
2194};
2195
2196MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
2197
2198static struct hdac_ext_driver hdmi_driver = {
2199 . hdac = {
2200 .driver = {
2201 .name = "HDMI HDA Codec",
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05302202 .pm = &hdac_hdmi_pm,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302203 },
2204 .id_table = hdmi_list,
2205 },
2206 .probe = hdac_hdmi_dev_probe,
2207 .remove = hdac_hdmi_dev_remove,
2208};
2209
2210static int __init hdmi_init(void)
2211{
2212 return snd_hda_ext_driver_register(&hdmi_driver);
2213}
2214
2215static void __exit hdmi_exit(void)
2216{
2217 snd_hda_ext_driver_unregister(&hdmi_driver);
2218}
2219
2220module_init(hdmi_init);
2221module_exit(hdmi_exit);
2222
2223MODULE_LICENSE("GPL v2");
2224MODULE_DESCRIPTION("HDMI HD codec");
2225MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
2226MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");