blob: d3858b53d2733494b6e04c5a2b2969c33487bccb [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 KP754695f2017-02-06 12:09:14 +053085 struct hdac_hdmi_port *ports;
86 int num_ports;
87 struct hdac_ext_device *edev;
88};
89
90struct hdac_hdmi_port {
91 int id;
92 struct hdac_hdmi_pin *pin;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053093 int num_mux_nids;
94 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053095 struct hdac_hdmi_eld eld;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053096};
97
Jeeja KP4a3478d2016-02-12 07:46:06 +053098struct hdac_hdmi_pcm {
99 struct list_head head;
100 int pcm_id;
Jeeja KP754695f2017-02-06 12:09:14 +0530101 struct hdac_hdmi_port *port;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530102 struct hdac_hdmi_cvt *cvt;
103 struct snd_jack *jack;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530104 int stream_tag;
105 int channels;
106 int format;
Jeeja KPab1eea12017-01-24 21:49:05 +0530107 bool chmap_set;
108 unsigned char chmap[8]; /* ALSA API channel-map */
109 struct mutex lock;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530110};
111
Jeeja KP754695f2017-02-06 12:09:14 +0530112struct hdac_hdmi_dai_port_map {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530113 int dai_id;
Jeeja KP754695f2017-02-06 12:09:14 +0530114 struct hdac_hdmi_port *port;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530115 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530116};
117
118struct hdac_hdmi_priv {
Jeeja KP754695f2017-02-06 12:09:14 +0530119 struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530120 struct list_head pin_list;
121 struct list_head cvt_list;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530122 struct list_head pcm_list;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530123 int num_pin;
124 int num_cvt;
Jeeja KP754695f2017-02-06 12:09:14 +0530125 int num_ports;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530126 struct mutex pin_mutex;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530127 struct hdac_chmap chmap;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530128};
129
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530130static struct hdac_hdmi_pcm *
131hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
132 struct hdac_hdmi_cvt *cvt)
133{
134 struct hdac_hdmi_pcm *pcm = NULL;
Jeeja KP1de777f2017-01-10 17:57:48 +0530135
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530136 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
137 if (pcm->cvt == cvt)
138 break;
139 }
140
141 return pcm;
142}
Jeeja KP1de777f2017-01-10 17:57:48 +0530143
Subhransu S. Prusty28890992016-04-14 10:07:34 +0530144static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
145 int pcm_idx)
146{
147 struct hdac_hdmi_pcm *pcm;
148
149 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
150 if (pcm->pcm_id == pcm_idx)
151 return pcm;
152 }
153
154 return NULL;
155}
156
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530157static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
158{
Geliang Tang51b2c422015-12-28 22:47:13 +0800159 struct hdac_device *hdac = dev_to_hdac_dev(dev);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530160
Geliang Tang51b2c422015-12-28 22:47:13 +0800161 return to_ehdac_device(hdac);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530162}
163
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530164static unsigned int sad_format(const u8 *sad)
165{
166 return ((sad[0] >> 0x3) & 0x1f);
167}
168
169static unsigned int sad_sample_bits_lpcm(const u8 *sad)
170{
171 return (sad[2] & 7);
172}
173
174static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
175 void *eld)
176{
177 u64 formats = SNDRV_PCM_FMTBIT_S16;
178 int i;
179 const u8 *sad, *eld_buf = eld;
180
181 sad = drm_eld_sad(eld_buf);
182 if (!sad)
183 goto format_constraint;
184
185 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
186 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
187
188 /*
189 * the controller support 20 and 24 bits in 32 bit
190 * container so we set S32
191 */
192 if (sad_sample_bits_lpcm(sad) & 0x6)
193 formats |= SNDRV_PCM_FMTBIT_S32;
194 }
195 }
196
197format_constraint:
198 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
199 formats);
200
201}
202
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530203static void
204hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
205 int packet_index, int byte_index)
206{
207 int val;
208
209 val = (packet_index << 5) | (byte_index & 0x1f);
210
211 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
212 AC_VERB_SET_HDMI_DIP_INDEX, val);
213}
214
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530215struct dp_audio_infoframe {
216 u8 type; /* 0x84 */
217 u8 len; /* 0x1b */
218 u8 ver; /* 0x11 << 2 */
219
220 u8 CC02_CT47; /* match with HDMI infoframe from this on */
221 u8 SS01_SF24;
222 u8 CXT04;
223 u8 CA;
224 u8 LFEPBL01_LSV36_DM_INH7;
225};
226
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530227static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
Jeeja KP754695f2017-02-06 12:09:14 +0530228 struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530229{
230 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
231 struct hdmi_audio_infoframe frame;
Jeeja KP754695f2017-02-06 12:09:14 +0530232 struct hdac_hdmi_pin *pin = port->pin;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530233 struct dp_audio_infoframe dp_ai;
234 struct hdac_hdmi_priv *hdmi = hdac->private_data;
Jeeja KPab1eea12017-01-24 21:49:05 +0530235 struct hdac_hdmi_cvt *cvt = pcm->cvt;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530236 u8 *dip;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530237 int ret;
238 int i;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530239 const u8 *eld_buf;
240 u8 conn_type;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530241 int channels, ca;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530242
Jeeja KP754695f2017-02-06 12:09:14 +0530243 ca = snd_hdac_channel_allocation(&hdac->hdac, port->eld.info.spk_alloc,
Jeeja KPab1eea12017-01-24 21:49:05 +0530244 pcm->channels, pcm->chmap_set, true, pcm->chmap);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530245
246 channels = snd_hdac_get_active_channels(ca);
Jeeja KPab1eea12017-01-24 21:49:05 +0530247 hdmi->chmap.ops.set_channel_count(&hdac->hdac, cvt->nid, channels);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530248
249 snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
Jeeja KPab1eea12017-01-24 21:49:05 +0530250 pcm->channels, pcm->chmap, pcm->chmap_set);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530251
Jeeja KP754695f2017-02-06 12:09:14 +0530252 eld_buf = port->eld.eld_buffer;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530253 conn_type = drm_eld_get_conn_type(eld_buf);
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530254
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530255 switch (conn_type) {
256 case DRM_ELD_CONN_TYPE_HDMI:
257 hdmi_audio_infoframe_init(&frame);
258
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530259 frame.channels = channels;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530260 frame.channel_allocation = ca;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530261
262 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
263 if (ret < 0)
264 return ret;
265
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530266 break;
267
268 case DRM_ELD_CONN_TYPE_DP:
269 memset(&dp_ai, 0, sizeof(dp_ai));
270 dp_ai.type = 0x84;
271 dp_ai.len = 0x1b;
272 dp_ai.ver = 0x11 << 2;
273 dp_ai.CC02_CT47 = channels - 1;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530274 dp_ai.CA = ca;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530275
276 dip = (u8 *)&dp_ai;
277 break;
278
279 default:
280 dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
281 conn_type);
282 return -EIO;
283 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530284
285 /* stop infoframe transmission */
Jeeja KPab1eea12017-01-24 21:49:05 +0530286 hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
287 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530288 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
289
290
291 /* Fill infoframe. Index auto-incremented */
Jeeja KPab1eea12017-01-24 21:49:05 +0530292 hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530293 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
Subhransu S. Prusty391005e2016-03-10 09:04:07 +0530294 for (i = 0; i < sizeof(buffer); i++)
Jeeja KPab1eea12017-01-24 21:49:05 +0530295 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
Subhransu S. Prusty391005e2016-03-10 09:04:07 +0530296 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530297 } else {
298 for (i = 0; i < sizeof(dp_ai); i++)
Jeeja KPab1eea12017-01-24 21:49:05 +0530299 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530300 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
301 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530302
303 /* Start infoframe */
Jeeja KPab1eea12017-01-24 21:49:05 +0530304 hdac_hdmi_set_dip_index(hdac, pin->nid, 0x0, 0x0);
305 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530306 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
307
308 return 0;
309}
310
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530311static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
312 unsigned int tx_mask, unsigned int rx_mask,
313 int slots, int slot_width)
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530314{
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530315 struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
316 struct hdac_hdmi_priv *hdmi = edev->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +0530317 struct hdac_hdmi_dai_port_map *dai_map;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530318 struct hdac_hdmi_pcm *pcm;
319
320 dev_dbg(&edev->hdac.dev, "%s: strm_tag: %d\n", __func__, tx_mask);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530321
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530322 dai_map = &hdmi->dai_map[dai->id];
323
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530324 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530325
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530326 if (pcm)
327 pcm->stream_tag = (tx_mask << 4);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530328
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530329 return 0;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530330}
331
332static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
333 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
334{
335 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530336 struct hdac_hdmi_priv *hdmi = hdac->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +0530337 struct hdac_hdmi_dai_port_map *dai_map;
338 struct hdac_hdmi_port *port;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530339 struct hdac_hdmi_pcm *pcm;
340 int format;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530341
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530342 dai_map = &hdmi->dai_map[dai->id];
Jeeja KP754695f2017-02-06 12:09:14 +0530343 port = dai_map->port;
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530344
Jeeja KP754695f2017-02-06 12:09:14 +0530345 if (!port)
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530346 return -ENODEV;
347
Jeeja KP754695f2017-02-06 12:09:14 +0530348 if ((!port->eld.monitor_present) || (!port->eld.eld_valid)) {
349 dev_err(&hdac->hdac.dev,
350 "device is not configured for this pin:port%d:%d\n",
351 port->pin->nid, port->id);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530352 return -ENODEV;
353 }
354
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530355 format = snd_hdac_calc_stream_format(params_rate(hparams),
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530356 params_channels(hparams), params_format(hparams),
357 24, 0);
358
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530359 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
360 if (!pcm)
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530361 return -EIO;
362
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530363 pcm->format = format;
364 pcm->channels = params_channels(hparams);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530365
366 return 0;
367}
368
Jeeja KP754695f2017-02-06 12:09:14 +0530369static int hdac_hdmi_query_port_connlist(struct hdac_ext_device *hdac,
370 struct hdac_hdmi_pin *pin,
371 struct hdac_hdmi_port *port)
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530372{
373 if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
374 dev_warn(&hdac->hdac.dev,
375 "HDMI: pin %d wcaps %#x does not support connection list\n",
376 pin->nid, get_wcaps(&hdac->hdac, pin->nid));
377 return -EINVAL;
378 }
379
Jeeja KP754695f2017-02-06 12:09:14 +0530380 port->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
381 port->mux_nids, HDA_MAX_CONNECTIONS);
382 if (port->num_mux_nids == 0)
383 dev_warn(&hdac->hdac.dev,
384 "No connections found for pin:port %d:%d\n",
385 pin->nid, port->id);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530386
Jeeja KP754695f2017-02-06 12:09:14 +0530387 dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin:port %d:%d\n",
388 port->num_mux_nids, pin->nid, port->id);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530389
Jeeja KP754695f2017-02-06 12:09:14 +0530390 return port->num_mux_nids;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530391}
392
393/*
Jeeja KP754695f2017-02-06 12:09:14 +0530394 * Query pcm list and return port to which stream is routed.
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530395 *
Jeeja KP754695f2017-02-06 12:09:14 +0530396 * Also query connection list of the pin, to validate the cvt to port map.
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530397 *
Jeeja KP754695f2017-02-06 12:09:14 +0530398 * Same stream rendering to multiple ports simultaneously can be done
399 * possibly, but not supported for now in driver. So return the first port
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530400 * connected.
401 */
Jeeja KP754695f2017-02-06 12:09:14 +0530402static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530403 struct hdac_ext_device *edev,
404 struct hdac_hdmi_priv *hdmi,
405 struct hdac_hdmi_cvt *cvt)
406{
407 struct hdac_hdmi_pcm *pcm;
Jeeja KP754695f2017-02-06 12:09:14 +0530408 struct hdac_hdmi_port *port = NULL;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530409 int ret, i;
410
411 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
412 if (pcm->cvt == cvt) {
Jeeja KP754695f2017-02-06 12:09:14 +0530413 port = pcm->port;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530414 break;
415 }
416 }
417
Jeeja KP754695f2017-02-06 12:09:14 +0530418 if (port) {
419 ret = hdac_hdmi_query_port_connlist(edev, port->pin, port);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530420 if (ret < 0)
421 return NULL;
422
Jeeja KP754695f2017-02-06 12:09:14 +0530423 for (i = 0; i < port->num_mux_nids; i++) {
424 if (port->mux_nids[i] == cvt->nid)
425 return port;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530426 }
427 }
428
429 return NULL;
430}
431
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530432/*
433 * This tries to get a valid pin and set the HW constraints based on the
434 * ELD. Even if a valid pin is not found return success so that device open
435 * doesn't fail.
436 */
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530437static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
438 struct snd_soc_dai *dai)
439{
440 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
441 struct hdac_hdmi_priv *hdmi = hdac->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +0530442 struct hdac_hdmi_dai_port_map *dai_map;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530443 struct hdac_hdmi_cvt *cvt;
Jeeja KP754695f2017-02-06 12:09:14 +0530444 struct hdac_hdmi_port *port;
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530445 int ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530446
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530447 dai_map = &hdmi->dai_map[dai->id];
448
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530449 cvt = dai_map->cvt;
Jeeja KP754695f2017-02-06 12:09:14 +0530450 port = hdac_hdmi_get_port_from_cvt(hdac, hdmi, cvt);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530451
452 /*
453 * To make PA and other userland happy.
454 * userland scans devices so returning error does not help.
455 */
Jeeja KP754695f2017-02-06 12:09:14 +0530456 if (!port)
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530457 return 0;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530458
Jeeja KP754695f2017-02-06 12:09:14 +0530459 if ((!port->eld.monitor_present) ||
460 (!port->eld.eld_valid)) {
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530461
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530462 dev_warn(&hdac->hdac.dev,
Jeeja KP754695f2017-02-06 12:09:14 +0530463 "Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
464 port->eld.monitor_present, port->eld.eld_valid,
465 port->pin->nid, port->id);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530466
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530467 return 0;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530468 }
469
Jeeja KP754695f2017-02-06 12:09:14 +0530470 dai_map->port = port;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530471
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530472 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
Jeeja KP754695f2017-02-06 12:09:14 +0530473 port->eld.eld_buffer);
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530474 if (ret < 0)
475 return ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530476
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530477 return snd_pcm_hw_constraint_eld(substream->runtime,
Jeeja KP754695f2017-02-06 12:09:14 +0530478 port->eld.eld_buffer);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530479}
480
481static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
482 struct snd_soc_dai *dai)
483{
484 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
485 struct hdac_hdmi_priv *hdmi = hdac->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +0530486 struct hdac_hdmi_dai_port_map *dai_map;
Jeeja KPab1eea12017-01-24 21:49:05 +0530487 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530488
489 dai_map = &hdmi->dai_map[dai->id];
490
Jeeja KPab1eea12017-01-24 21:49:05 +0530491 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530492
Jeeja KPab1eea12017-01-24 21:49:05 +0530493 if (pcm) {
494 mutex_lock(&pcm->lock);
495 pcm->chmap_set = false;
496 memset(pcm->chmap, 0, sizeof(pcm->chmap));
497 pcm->channels = 0;
498 mutex_unlock(&pcm->lock);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530499 }
Jeeja KPab1eea12017-01-24 21:49:05 +0530500
Jeeja KP754695f2017-02-06 12:09:14 +0530501 if (dai_map->port)
502 dai_map->port = NULL;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530503}
504
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530505static int
506hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
507{
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530508 unsigned int chans;
509 struct hdac_ext_device *edev = to_ehdac_device(hdac);
510 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530511 int err;
512
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530513 chans = get_wcaps(hdac, cvt->nid);
514 chans = get_wcaps_channels(chans);
515
516 cvt->params.channels_min = 2;
517
518 cvt->params.channels_max = chans;
519 if (chans > hdmi->chmap.channels_max)
520 hdmi->chmap.channels_max = chans;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530521
522 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
523 &cvt->params.rates,
524 &cvt->params.formats,
525 &cvt->params.maxbps);
526 if (err < 0)
527 dev_err(&hdac->dev,
528 "Failed to query pcm params for nid %d: %d\n",
529 cvt->nid, err);
530
531 return err;
532}
533
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530534static int hdac_hdmi_fill_widget_info(struct device *dev,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530535 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
536 void *priv, const char *wname, const char *stream,
537 struct snd_kcontrol_new *wc, int numkc,
538 int (*event)(struct snd_soc_dapm_widget *,
539 struct snd_kcontrol *, int), unsigned short event_flags)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530540{
541 w->id = id;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530542 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
543 if (!w->name)
544 return -ENOMEM;
545
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530546 w->sname = stream;
547 w->reg = SND_SOC_NOPM;
548 w->shift = 0;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530549 w->kcontrol_news = wc;
550 w->num_kcontrols = numkc;
551 w->priv = priv;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530552 w->event = event;
553 w->event_flags = event_flags;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530554
555 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530556}
557
558static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530559 const char *sink, const char *control, const char *src,
560 int (*handler)(struct snd_soc_dapm_widget *src,
561 struct snd_soc_dapm_widget *sink))
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530562{
563 route->sink = sink;
564 route->source = src;
565 route->control = control;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530566 route->connected = handler;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530567}
568
Jeeja KP4a3478d2016-02-12 07:46:06 +0530569static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
Jeeja KP754695f2017-02-06 12:09:14 +0530570 struct hdac_hdmi_port *port)
Jeeja KP4a3478d2016-02-12 07:46:06 +0530571{
572 struct hdac_hdmi_priv *hdmi = edev->private_data;
573 struct hdac_hdmi_pcm *pcm = NULL;
574
575 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +0530576 if (!pcm->port)
577 continue;
578
579 if (pcm->port == port)
Jeeja KP4a3478d2016-02-12 07:46:06 +0530580 return pcm;
581 }
582
583 return NULL;
584}
585
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530586static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
587 hda_nid_t nid, unsigned int pwr_state)
588{
589 if (get_wcaps(&edev->hdac, nid) & AC_WCAP_POWER) {
590 if (!snd_hdac_check_power_state(&edev->hdac, nid, pwr_state))
591 snd_hdac_codec_write(&edev->hdac, nid, 0,
592 AC_VERB_SET_POWER_STATE, pwr_state);
593 }
594}
595
596static void hdac_hdmi_set_amp(struct hdac_ext_device *edev,
597 hda_nid_t nid, int val)
598{
599 if (get_wcaps(&edev->hdac, nid) & AC_WCAP_OUT_AMP)
600 snd_hdac_codec_write(&edev->hdac, nid, 0,
601 AC_VERB_SET_AMP_GAIN_MUTE, val);
602}
603
604
605static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
606 struct snd_kcontrol *kc, int event)
607{
Jeeja KP754695f2017-02-06 12:09:14 +0530608 struct hdac_hdmi_port *port = w->priv;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530609 struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
610 struct hdac_hdmi_pcm *pcm;
611
612 dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
613 __func__, w->name, event);
614
Jeeja KP754695f2017-02-06 12:09:14 +0530615 pcm = hdac_hdmi_get_pcm(edev, port);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530616 if (!pcm)
617 return -EIO;
618
619 switch (event) {
620 case SND_SOC_DAPM_PRE_PMU:
Jeeja KP754695f2017-02-06 12:09:14 +0530621 hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D0);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530622
623 /* Enable out path for this pin widget */
Jeeja KP754695f2017-02-06 12:09:14 +0530624 snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530625 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
626
Jeeja KP754695f2017-02-06 12:09:14 +0530627 hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_UNMUTE);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530628
Jeeja KP754695f2017-02-06 12:09:14 +0530629 return hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530630
631 case SND_SOC_DAPM_POST_PMD:
Jeeja KP754695f2017-02-06 12:09:14 +0530632 hdac_hdmi_set_amp(edev, port->pin->nid, AMP_OUT_MUTE);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530633
634 /* Disable out path for this pin widget */
Jeeja KP754695f2017-02-06 12:09:14 +0530635 snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530636 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
637
Jeeja KP754695f2017-02-06 12:09:14 +0530638 hdac_hdmi_set_power_state(edev, port->pin->nid, AC_PWRST_D3);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530639 break;
640
641 }
642
643 return 0;
644}
645
646static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
647 struct snd_kcontrol *kc, int event)
648{
649 struct hdac_hdmi_cvt *cvt = w->priv;
650 struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
651 struct hdac_hdmi_priv *hdmi = edev->private_data;
652 struct hdac_hdmi_pcm *pcm;
653
654 dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
655 __func__, w->name, event);
656
657 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
658 if (!pcm)
659 return -EIO;
660
661 switch (event) {
662 case SND_SOC_DAPM_PRE_PMU:
663 hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D0);
664
665 /* Enable transmission */
666 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
667 AC_VERB_SET_DIGI_CONVERT_1, 1);
668
669 /* Category Code (CC) to zero */
670 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
671 AC_VERB_SET_DIGI_CONVERT_2, 0);
672
673 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
674 AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
675 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
676 AC_VERB_SET_STREAM_FORMAT, pcm->format);
677 break;
678
679 case SND_SOC_DAPM_POST_PMD:
680 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
681 AC_VERB_SET_CHANNEL_STREAMID, 0);
682 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
683 AC_VERB_SET_STREAM_FORMAT, 0);
684
685 hdac_hdmi_set_power_state(edev, cvt->nid, AC_PWRST_D3);
686 break;
687
688 }
689
690 return 0;
691}
692
693static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
694 struct snd_kcontrol *kc, int event)
695{
Jeeja KP754695f2017-02-06 12:09:14 +0530696 struct hdac_hdmi_port *port = w->priv;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530697 struct hdac_ext_device *edev = to_hda_ext_device(w->dapm->dev);
698 int mux_idx;
699
700 dev_dbg(&edev->hdac.dev, "%s: widget: %s event: %x\n",
701 __func__, w->name, event);
702
703 if (!kc)
704 kc = w->kcontrols[0];
705
706 mux_idx = dapm_kcontrol_get_value(kc);
707 if (mux_idx > 0) {
Jeeja KP754695f2017-02-06 12:09:14 +0530708 snd_hdac_codec_write(&edev->hdac, port->pin->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530709 AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
710 }
711
712 return 0;
713}
714
Jeeja KP4a3478d2016-02-12 07:46:06 +0530715/*
716 * Based on user selection, map the PINs with the PCMs.
717 */
Jeeja KP754695f2017-02-06 12:09:14 +0530718static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
Jeeja KP4a3478d2016-02-12 07:46:06 +0530719 struct snd_ctl_elem_value *ucontrol)
720{
721 int ret;
722 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
723 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
724 struct snd_soc_dapm_context *dapm = w->dapm;
Jeeja KP754695f2017-02-06 12:09:14 +0530725 struct hdac_hdmi_port *port = w->priv;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530726 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
727 struct hdac_hdmi_priv *hdmi = edev->private_data;
728 struct hdac_hdmi_pcm *pcm = NULL;
729 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
730
731 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
732 if (ret < 0)
733 return ret;
734
Jeeja KP754695f2017-02-06 12:09:14 +0530735 if (port == NULL)
736 return -EINVAL;
737
Jeeja KP4a3478d2016-02-12 07:46:06 +0530738 mutex_lock(&hdmi->pin_mutex);
739 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +0530740 if (!pcm->port && pcm->port == port &&
741 pcm->port->id == port->id)
742 pcm->port = NULL;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530743
744 /*
745 * Jack status is not reported during device probe as the
746 * PCMs are not registered by then. So report it here.
747 */
Jeeja KP754695f2017-02-06 12:09:14 +0530748 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->port) {
749 pcm->port = port;
750 if (port->eld.monitor_present && port->eld.eld_valid) {
Jeeja KP4a3478d2016-02-12 07:46:06 +0530751 dev_dbg(&edev->hdac.dev,
752 "jack report for pcm=%d\n",
753 pcm->pcm_id);
754
755 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
756 }
757 mutex_unlock(&hdmi->pin_mutex);
758 return ret;
759 }
760 }
761 mutex_unlock(&hdmi->pin_mutex);
762
763 return ret;
764}
765
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530766/*
767 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
768 * the display driver seem to be programming the connection list for the pin
769 * widget runtime.
770 *
771 * So programming all the possible inputs for the mux, the user has to take
772 * care of selecting the right one and leaving all other inputs selected to
773 * "NONE"
774 */
Jeeja KP754695f2017-02-06 12:09:14 +0530775static int hdac_hdmi_create_pin_port_muxs(struct hdac_ext_device *edev,
776 struct hdac_hdmi_port *port,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530777 struct snd_soc_dapm_widget *widget,
778 const char *widget_name)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530779{
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530780 struct hdac_hdmi_priv *hdmi = edev->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +0530781 struct hdac_hdmi_pin *pin = port->pin;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530782 struct snd_kcontrol_new *kc;
783 struct hdac_hdmi_cvt *cvt;
784 struct soc_enum *se;
785 char kc_name[NAME_SIZE];
786 char mux_items[NAME_SIZE];
787 /* To hold inputs to the Pin mux */
788 char *items[HDA_MAX_CONNECTIONS];
789 int i = 0;
790 int num_items = hdmi->num_cvt + 1;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530791
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530792 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
793 if (!kc)
794 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530795
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530796 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
797 if (!se)
798 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530799
Jeeja KP754695f2017-02-06 12:09:14 +0530800 sprintf(kc_name, "Pin %d port %d Input", pin->nid, port->id);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530801 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
802 if (!kc->name)
803 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530804
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530805 kc->private_value = (long)se;
806 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
807 kc->access = 0;
808 kc->info = snd_soc_info_enum_double;
Jeeja KP754695f2017-02-06 12:09:14 +0530809 kc->put = hdac_hdmi_set_pin_port_mux;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530810 kc->get = snd_soc_dapm_get_enum_double;
811
812 se->reg = SND_SOC_NOPM;
813
814 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
815 se->items = num_items;
816 se->mask = roundup_pow_of_two(se->items) - 1;
817
818 sprintf(mux_items, "NONE");
819 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
820 if (!items[i])
821 return -ENOMEM;
822
823 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
824 i++;
825 sprintf(mux_items, "cvt %d", cvt->nid);
826 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
827 if (!items[i])
828 return -ENOMEM;
829 }
830
831 se->texts = devm_kmemdup(&edev->hdac.dev, items,
832 (num_items * sizeof(char *)), GFP_KERNEL);
833 if (!se->texts)
834 return -ENOMEM;
835
836 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
Jeeja KP754695f2017-02-06 12:09:14 +0530837 snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530838 hdac_hdmi_pin_mux_widget_event,
839 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530840}
841
842/* Add cvt <- input <- mux route map */
843static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
844 struct snd_soc_dapm_widget *widgets,
845 struct snd_soc_dapm_route *route, int rindex)
846{
847 struct hdac_hdmi_priv *hdmi = edev->private_data;
848 const struct snd_kcontrol_new *kc;
849 struct soc_enum *se;
Jeeja KP754695f2017-02-06 12:09:14 +0530850 int mux_index = hdmi->num_cvt + hdmi->num_ports;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530851 int i, j;
852
Jeeja KP754695f2017-02-06 12:09:14 +0530853 for (i = 0; i < hdmi->num_ports; i++) {
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530854 kc = widgets[mux_index].kcontrol_news;
855 se = (struct soc_enum *)kc->private_value;
856 for (j = 0; j < hdmi->num_cvt; j++) {
857 hdac_hdmi_fill_route(&route[rindex],
858 widgets[mux_index].name,
859 se->texts[j + 1],
860 widgets[j].name, NULL);
861
862 rindex++;
863 }
864
865 mux_index++;
866 }
867}
868
869/*
870 * Widgets are added in the below sequence
871 * Converter widgets for num converters enumerated
Jeeja KP754695f2017-02-06 12:09:14 +0530872 * Pin-port widgets for num ports for Pins enumerated
873 * Pin-port mux widgets to represent connenction list of pin widget
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530874 *
Jeeja KP754695f2017-02-06 12:09:14 +0530875 * For each port, one Mux and One output widget is added
876 * Total widgets elements = num_cvt + (num_ports * 2);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530877 *
878 * Routes are added as below:
Jeeja KP754695f2017-02-06 12:09:14 +0530879 * pin-port mux -> pin (based on num_ports)
880 * cvt -> "Input sel control" -> pin-port_mux
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530881 *
882 * Total route elements:
Jeeja KP754695f2017-02-06 12:09:14 +0530883 * num_ports + (pin_muxes * num_cvt)
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530884 */
885static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
886{
887 struct snd_soc_dapm_widget *widgets;
888 struct snd_soc_dapm_route *route;
889 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
890 struct hdac_hdmi_priv *hdmi = edev->private_data;
891 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
892 char widget_name[NAME_SIZE];
893 struct hdac_hdmi_cvt *cvt;
894 struct hdac_hdmi_pin *pin;
Jeeja KP754695f2017-02-06 12:09:14 +0530895 int ret, i = 0, num_routes = 0, j;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530896
897 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
898 return -EINVAL;
899
Jeeja KP754695f2017-02-06 12:09:14 +0530900 widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
901 ((2 * hdmi->num_ports) + hdmi->num_cvt)),
902 GFP_KERNEL);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530903
904 if (!widgets)
905 return -ENOMEM;
906
907 /* DAPM widgets to represent each converter widget */
908 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
909 sprintf(widget_name, "Converter %d", cvt->nid);
910 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530911 snd_soc_dapm_aif_in, cvt,
912 widget_name, dai_drv[i].playback.stream_name, NULL, 0,
913 hdac_hdmi_cvt_output_widget_event,
914 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530915 if (ret < 0)
916 return ret;
917 i++;
918 }
919
920 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +0530921 for (j = 0; j < pin->num_ports; j++) {
922 sprintf(widget_name, "hif%d-%d Output",
923 pin->nid, pin->ports[j].id);
924 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
925 snd_soc_dapm_output, &pin->ports[j],
926 widget_name, NULL, NULL, 0,
927 hdac_hdmi_pin_output_widget_event,
928 SND_SOC_DAPM_PRE_PMU |
929 SND_SOC_DAPM_POST_PMD);
930 if (ret < 0)
931 return ret;
932 i++;
933 }
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530934 }
935
936 /* DAPM widgets to represent the connection list to pin widget */
937 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +0530938 for (j = 0; j < pin->num_ports; j++) {
939 sprintf(widget_name, "Pin%d-Port%d Mux",
940 pin->nid, pin->ports[j].id);
941 ret = hdac_hdmi_create_pin_port_muxs(edev,
942 &pin->ports[j], &widgets[i],
943 widget_name);
944 if (ret < 0)
945 return ret;
946 i++;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530947
Jeeja KP754695f2017-02-06 12:09:14 +0530948 /* For cvt to pin_mux mapping */
949 num_routes += hdmi->num_cvt;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530950
Jeeja KP754695f2017-02-06 12:09:14 +0530951 /* For pin_mux to pin mapping */
952 num_routes++;
953 }
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530954 }
955
956 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
957 GFP_KERNEL);
958 if (!route)
959 return -ENOMEM;
960
961 i = 0;
962 /* Add pin <- NULL <- mux route map */
963 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +0530964 for (j = 0; j < pin->num_ports; j++) {
965 int sink_index = i + hdmi->num_cvt;
966 int src_index = sink_index + pin->num_ports *
967 hdmi->num_pin;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530968
Jeeja KP754695f2017-02-06 12:09:14 +0530969 hdac_hdmi_fill_route(&route[i],
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530970 widgets[sink_index].name, NULL,
971 widgets[src_index].name, NULL);
Jeeja KP754695f2017-02-06 12:09:14 +0530972 i++;
973 }
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530974 }
975
976 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
977
978 snd_soc_dapm_new_controls(dapm, widgets,
Jeeja KP754695f2017-02-06 12:09:14 +0530979 ((2 * hdmi->num_ports) + hdmi->num_cvt));
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530980
981 snd_soc_dapm_add_routes(dapm, route, num_routes);
982 snd_soc_dapm_new_widgets(dapm->card);
983
984 return 0;
985
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530986}
987
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530988static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530989{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530990 struct hdac_hdmi_priv *hdmi = edev->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +0530991 struct hdac_hdmi_dai_port_map *dai_map;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530992 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530993 int dai_id = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530994
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530995 if (list_empty(&hdmi->cvt_list))
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530996 return -EINVAL;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530997
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530998 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
999 dai_map = &hdmi->dai_map[dai_id];
1000 dai_map->dai_id = dai_id;
1001 dai_map->cvt = cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301002
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301003 dai_id++;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301004
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301005 if (dai_id == HDA_MAX_CVTS) {
1006 dev_warn(&edev->hdac.dev,
1007 "Max dais supported: %d\n", dai_id);
1008 break;
1009 }
1010 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301011
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301012 return 0;
1013}
1014
1015static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
1016{
1017 struct hdac_hdmi_priv *hdmi = edev->private_data;
1018 struct hdac_hdmi_cvt *cvt;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301019 char name[NAME_SIZE];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301020
1021 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
1022 if (!cvt)
1023 return -ENOMEM;
1024
1025 cvt->nid = nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301026 sprintf(name, "cvt %d", cvt->nid);
1027 cvt->name = kstrdup(name, GFP_KERNEL);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301028
1029 list_add_tail(&cvt->head, &hdmi->cvt_list);
1030 hdmi->num_cvt++;
1031
1032 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
1033}
1034
Jeeja KP754695f2017-02-06 12:09:14 +05301035static int hdac_hdmi_parse_eld(struct hdac_ext_device *edev,
1036 struct hdac_hdmi_port *port)
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301037{
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301038 unsigned int ver, mnl;
1039
Jeeja KP754695f2017-02-06 12:09:14 +05301040 ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301041 >> DRM_ELD_VER_SHIFT;
1042
1043 if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1044 dev_err(&edev->hdac.dev, "HDMI: Unknown ELD version %d\n", ver);
1045 return -EINVAL;
1046 }
1047
Jeeja KP754695f2017-02-06 12:09:14 +05301048 mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301049 DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1050
1051 if (mnl > ELD_MAX_MNL) {
1052 dev_err(&edev->hdac.dev, "HDMI: MNL Invalid %d\n", mnl);
1053 return -EINVAL;
1054 }
1055
Jeeja KP754695f2017-02-06 12:09:14 +05301056 port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301057
1058 return 0;
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301059}
1060
Jeeja KP754695f2017-02-06 12:09:14 +05301061static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1062 struct hdac_hdmi_port *port)
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301063{
1064 struct hdac_ext_device *edev = pin->edev;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301065 struct hdac_hdmi_priv *hdmi = edev->private_data;
1066 struct hdac_hdmi_pcm *pcm;
Jeeja KP754695f2017-02-06 12:09:14 +05301067 int size = 0;
1068
1069 if (!hdmi)
1070 return;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301071
1072 mutex_lock(&hdmi->pin_mutex);
Jeeja KP754695f2017-02-06 12:09:14 +05301073 port->eld.monitor_present = false;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301074
1075 size = snd_hdac_acomp_get_eld(&edev->hdac, pin->nid, -1,
Jeeja KP754695f2017-02-06 12:09:14 +05301076 &port->eld.monitor_present,
1077 port->eld.eld_buffer,
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301078 ELD_MAX_SIZE);
1079
1080 if (size > 0) {
1081 size = min(size, ELD_MAX_SIZE);
Jeeja KP754695f2017-02-06 12:09:14 +05301082 if (hdac_hdmi_parse_eld(edev, port) < 0)
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301083 size = -EINVAL;
1084 }
1085
1086 if (size > 0) {
Jeeja KP754695f2017-02-06 12:09:14 +05301087 port->eld.eld_valid = true;
1088 port->eld.eld_size = size;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301089 } else {
Jeeja KP754695f2017-02-06 12:09:14 +05301090 port->eld.eld_valid = false;
1091 port->eld.eld_size = 0;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301092 }
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301093
Jeeja KP754695f2017-02-06 12:09:14 +05301094 pcm = hdac_hdmi_get_pcm(edev, port);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301095
Jeeja KP754695f2017-02-06 12:09:14 +05301096 if (!port->eld.monitor_present || !port->eld.eld_valid) {
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301097
Jeeja KP754695f2017-02-06 12:09:14 +05301098 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin:port %d:%d\n",
1099 __func__, pin->nid, port->id);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301100
1101 /*
1102 * PCMs are not registered during device probe, so don't
1103 * report jack here. It will be done in usermode mux
1104 * control select.
1105 */
1106 if (pcm) {
1107 dev_dbg(&edev->hdac.dev,
1108 "jack report for pcm=%d\n", pcm->pcm_id);
1109
1110 snd_jack_report(pcm->jack, 0);
1111 }
1112
1113 mutex_unlock(&hdmi->pin_mutex);
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301114 return;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301115 }
1116
Jeeja KP754695f2017-02-06 12:09:14 +05301117 if (port->eld.monitor_present && port->eld.eld_valid) {
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301118 if (pcm) {
1119 dev_dbg(&edev->hdac.dev,
1120 "jack report for pcm=%d\n",
1121 pcm->pcm_id);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301122
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301123 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301124 }
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301125
1126 print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
Jeeja KP754695f2017-02-06 12:09:14 +05301127 port->eld.eld_buffer, port->eld.eld_size, false);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301128
Jeeja KP754695f2017-02-06 12:09:14 +05301129 }
Jeeja KP4a3478d2016-02-12 07:46:06 +05301130 mutex_unlock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301131}
1132
Jeeja KP754695f2017-02-06 12:09:14 +05301133static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi,
1134 struct hdac_hdmi_pin *pin)
1135{
1136 struct hdac_hdmi_port *ports;
1137 int max_ports = HDA_MAX_PORTS;
1138 int i;
1139
1140 /*
1141 * FIXME: max_port may vary for each platform, so pass this as
1142 * as driver data or query from i915 interface when this API is
1143 * implemented.
1144 */
1145
1146 ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL);
1147 if (!ports)
1148 return -ENOMEM;
1149
1150 for (i = 0; i < max_ports; i++) {
1151 ports[i].id = i;
1152 ports[i].pin = pin;
1153 }
1154 pin->ports = ports;
1155 pin->num_ports = max_ports;
1156 return 0;
1157}
1158
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301159static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
1160{
1161 struct hdac_hdmi_priv *hdmi = edev->private_data;
1162 struct hdac_hdmi_pin *pin;
Jeeja KP754695f2017-02-06 12:09:14 +05301163 int ret;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301164
1165 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1166 if (!pin)
1167 return -ENOMEM;
1168
1169 pin->nid = nid;
Jeeja KP754695f2017-02-06 12:09:14 +05301170 pin->edev = edev;
1171 ret = hdac_hdmi_add_ports(hdmi, pin);
1172 if (ret < 0)
1173 return ret;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301174
1175 list_add_tail(&pin->head, &hdmi->pin_list);
1176 hdmi->num_pin++;
Jeeja KP754695f2017-02-06 12:09:14 +05301177 hdmi->num_ports += pin->num_ports;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301178
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301179 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301180}
1181
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301182#define INTEL_VENDOR_NID 0x08
1183#define INTEL_GET_VENDOR_VERB 0xf81
1184#define INTEL_SET_VENDOR_VERB 0x781
1185#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1186#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1187
1188static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1189{
1190 unsigned int vendor_param;
1191
1192 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1193 INTEL_GET_VENDOR_VERB, 0);
1194 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1195 return;
1196
1197 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1198 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1199 INTEL_SET_VENDOR_VERB, vendor_param);
1200 if (vendor_param == -1)
1201 return;
1202}
1203
1204static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1205{
1206 unsigned int vendor_param;
1207
1208 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1209 INTEL_GET_VENDOR_VERB, 0);
1210 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1211 return;
1212
1213 /* enable DP1.2 mode */
1214 vendor_param |= INTEL_EN_DP12;
1215 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1216 INTEL_SET_VENDOR_VERB, vendor_param);
1217 if (vendor_param == -1)
1218 return;
1219
1220}
1221
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301222static struct snd_soc_dai_ops hdmi_dai_ops = {
1223 .startup = hdac_hdmi_pcm_open,
1224 .shutdown = hdac_hdmi_pcm_close,
1225 .hw_params = hdac_hdmi_set_hw_params,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +05301226 .set_tdm_slot = hdac_hdmi_set_tdm_slot,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301227};
1228
1229/*
1230 * Each converter can support a stream independently. So a dai is created
1231 * based on the number of converter queried.
1232 */
1233static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1234 struct snd_soc_dai_driver **dais,
1235 struct hdac_hdmi_priv *hdmi, int num_dais)
1236{
1237 struct snd_soc_dai_driver *hdmi_dais;
1238 struct hdac_hdmi_cvt *cvt;
1239 char name[NAME_SIZE], dai_name[NAME_SIZE];
1240 int i = 0;
1241 u32 rates, bps;
1242 unsigned int rate_max = 384000, rate_min = 8000;
1243 u64 formats;
1244 int ret;
1245
1246 hdmi_dais = devm_kzalloc(&hdac->dev,
1247 (sizeof(*hdmi_dais) * num_dais),
1248 GFP_KERNEL);
1249 if (!hdmi_dais)
1250 return -ENOMEM;
1251
1252 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1253 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1254 &rates, &formats, &bps);
1255 if (ret)
1256 return ret;
1257
1258 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1259 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1260 dai_name, GFP_KERNEL);
1261
1262 if (!hdmi_dais[i].name)
1263 return -ENOMEM;
1264
1265 snprintf(name, sizeof(name), "hifi%d", i+1);
1266 hdmi_dais[i].playback.stream_name =
1267 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1268 if (!hdmi_dais[i].playback.stream_name)
1269 return -ENOMEM;
1270
1271 /*
1272 * Set caps based on capability queried from the converter.
1273 * It will be constrained runtime based on ELD queried.
1274 */
1275 hdmi_dais[i].playback.formats = formats;
1276 hdmi_dais[i].playback.rates = rates;
1277 hdmi_dais[i].playback.rate_max = rate_max;
1278 hdmi_dais[i].playback.rate_min = rate_min;
1279 hdmi_dais[i].playback.channels_min = 2;
1280 hdmi_dais[i].playback.channels_max = 2;
1281 hdmi_dais[i].ops = &hdmi_dai_ops;
1282
1283 i++;
1284 }
1285
1286 *dais = hdmi_dais;
1287
1288 return 0;
1289}
1290
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301291/*
1292 * Parse all nodes and store the cvt/pin nids in array
1293 * Add one time initialization for pin and cvt widgets
1294 */
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301295static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1296 struct snd_soc_dai_driver **dais, int *num_dais)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301297{
1298 hda_nid_t nid;
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301299 int i, num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301300 struct hdac_device *hdac = &edev->hdac;
1301 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301302 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301303
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301304 hdac_hdmi_skl_enable_all_pins(hdac);
1305 hdac_hdmi_skl_enable_dp12(hdac);
1306
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301307 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
Subhransu S. Prusty541140d2015-12-09 21:46:08 +05301308 if (!nid || num_nodes <= 0) {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301309 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
1310 return -EINVAL;
1311 }
1312
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301313 hdac->num_nodes = num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301314 hdac->start_nid = nid;
1315
1316 for (i = 0; i < hdac->num_nodes; i++, nid++) {
1317 unsigned int caps;
1318 unsigned int type;
1319
1320 caps = get_wcaps(hdac, nid);
1321 type = get_wcaps_type(caps);
1322
1323 if (!(caps & AC_WCAP_DIGITAL))
1324 continue;
1325
1326 switch (type) {
1327
1328 case AC_WID_AUD_OUT:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301329 ret = hdac_hdmi_add_cvt(edev, nid);
1330 if (ret < 0)
1331 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301332 break;
1333
1334 case AC_WID_PIN:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301335 ret = hdac_hdmi_add_pin(edev, nid);
1336 if (ret < 0)
1337 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301338 break;
1339 }
1340 }
1341
1342 hdac->end_nid = nid;
1343
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301344 if (!hdmi->num_pin || !hdmi->num_cvt)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301345 return -EIO;
1346
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301347 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1348 if (ret) {
1349 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1350 ret);
1351 return ret;
1352 }
1353
1354 *num_dais = hdmi->num_cvt;
1355
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301356 return hdac_hdmi_init_dai_map(edev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301357}
1358
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -07001359static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301360{
1361 struct hdac_ext_device *edev = aptr;
1362 struct hdac_hdmi_priv *hdmi = edev->private_data;
Jeeja KP754695f2017-02-06 12:09:14 +05301363 struct hdac_hdmi_pin *pin = NULL;
1364 struct hdac_hdmi_port *hport = NULL;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301365 struct snd_soc_codec *codec = edev->scodec;
1366
1367 /* Don't know how this mapping is derived */
1368 hda_nid_t pin_nid = port + 0x04;
1369
Jeeja KP754695f2017-02-06 12:09:14 +05301370 dev_dbg(&edev->hdac.dev, "%s: for pin:%d port=%d\n", __func__,
1371 pin_nid, pipe);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301372
1373 /*
1374 * skip notification during system suspend (but not in runtime PM);
1375 * the state will be updated at resume. Also since the ELD and
1376 * connection states are updated in anyway at the end of the resume,
1377 * we can skip it when received during PM process.
1378 */
1379 if (snd_power_get_state(codec->component.card->snd_card) !=
1380 SNDRV_CTL_POWER_D0)
1381 return;
1382
1383 if (atomic_read(&edev->hdac.in_pm))
1384 return;
1385
1386 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301387 if (pin->nid != pin_nid)
1388 continue;
1389
1390 /* In case of non MST pin, pipe is -1 */
1391 if (pipe == -1) {
1392 /* if not MST, default is port[0] */
1393 hport = &pin->ports[0];
1394 break;
1395 }
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301396 }
Jeeja KP754695f2017-02-06 12:09:14 +05301397
1398 if (hport)
1399 hdac_hdmi_present_sense(pin, hport);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301400}
1401
1402static struct i915_audio_component_audio_ops aops = {
1403 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1404};
1405
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301406static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
1407 int device)
1408{
1409 struct snd_soc_pcm_runtime *rtd;
1410
1411 list_for_each_entry(rtd, &card->rtd_list, list) {
1412 if (rtd->pcm && (rtd->pcm->device == device))
1413 return rtd->pcm;
1414 }
1415
1416 return NULL;
1417}
1418
Jeeja KP4a3478d2016-02-12 07:46:06 +05301419int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1420{
1421 char jack_name[NAME_SIZE];
1422 struct snd_soc_codec *codec = dai->codec;
1423 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1424 struct snd_soc_dapm_context *dapm =
1425 snd_soc_component_get_dapm(&codec->component);
1426 struct hdac_hdmi_priv *hdmi = edev->private_data;
1427 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301428 struct snd_pcm *snd_pcm;
1429 int err;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301430
1431 /*
1432 * this is a new PCM device, create new pcm and
1433 * add to the pcm list
1434 */
1435 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1436 if (!pcm)
1437 return -ENOMEM;
1438 pcm->pcm_id = device;
1439 pcm->cvt = hdmi->dai_map[dai->id].cvt;
Jeeja KPab1eea12017-01-24 21:49:05 +05301440 mutex_init(&pcm->lock);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301441
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301442 snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
1443 if (snd_pcm) {
1444 err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
1445 if (err < 0) {
1446 dev_err(&edev->hdac.dev,
1447 "chmap control add failed with err: %d for pcm: %d\n",
1448 err, device);
1449 kfree(pcm);
1450 return err;
1451 }
1452 }
1453
Jeeja KP4a3478d2016-02-12 07:46:06 +05301454 list_add_tail(&pcm->head, &hdmi->pcm_list);
1455
1456 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1457
1458 return snd_jack_new(dapm->card->snd_card, jack_name,
1459 SND_JACK_AVOUT, &pcm->jack, true, false);
1460}
1461EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1462
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301463static int hdmi_codec_probe(struct snd_soc_codec *codec)
1464{
1465 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1466 struct hdac_hdmi_priv *hdmi = edev->private_data;
1467 struct snd_soc_dapm_context *dapm =
1468 snd_soc_component_get_dapm(&codec->component);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301469 struct hdac_hdmi_pin *pin;
Vinod Koulb2047e92016-05-12 08:58:55 +05301470 struct hdac_ext_link *hlink = NULL;
Jeeja KP754695f2017-02-06 12:09:14 +05301471 int ret, i;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301472
1473 edev->scodec = codec;
1474
Vinod Koulb2047e92016-05-12 08:58:55 +05301475 /*
1476 * hold the ref while we probe, also no need to drop the ref on
1477 * exit, we call pm_runtime_suspend() so that will do for us
1478 */
1479 hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301480 if (!hlink) {
1481 dev_err(&edev->hdac.dev, "hdac link not found\n");
1482 return -EIO;
1483 }
1484
Vinod Koulb2047e92016-05-12 08:58:55 +05301485 snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1486
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301487 ret = create_fill_widget_route_map(dapm);
1488 if (ret < 0)
1489 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301490
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301491 aops.audio_ptr = edev;
1492 ret = snd_hdac_i915_register_notifier(&aops);
1493 if (ret < 0) {
1494 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1495 ret);
1496 return ret;
1497 }
1498
1499 list_for_each_entry(pin, &hdmi->pin_list, head)
Jeeja KP754695f2017-02-06 12:09:14 +05301500 for (i = 0; i < pin->num_ports; i++)
1501 hdac_hdmi_present_sense(pin, &pin->ports[i]);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301502
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301503 /* Imp: Store the card pointer in hda_codec */
1504 edev->card = dapm->card->snd_card;
1505
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301506 /*
1507 * hdac_device core already sets the state to active and calls
1508 * get_noresume. So enable runtime and set the device to suspend.
1509 */
1510 pm_runtime_enable(&edev->hdac.dev);
1511 pm_runtime_put(&edev->hdac.dev);
1512 pm_runtime_suspend(&edev->hdac.dev);
1513
1514 return 0;
1515}
1516
1517static int hdmi_codec_remove(struct snd_soc_codec *codec)
1518{
1519 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1520
1521 pm_runtime_disable(&edev->hdac.dev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301522 return 0;
1523}
1524
Jeeja KP571d5072016-02-22 07:50:33 +05301525#ifdef CONFIG_PM
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301526static int hdmi_codec_prepare(struct device *dev)
1527{
1528 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1529 struct hdac_device *hdac = &edev->hdac;
1530
1531 pm_runtime_get_sync(&edev->hdac.dev);
1532
1533 /*
1534 * Power down afg.
1535 * codec_read is preferred over codec_write to set the power state.
1536 * This way verb is send to set the power state and response
1537 * is received. So setting power state is ensured without using loop
1538 * to read the state.
1539 */
1540 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1541 AC_PWRST_D3);
1542
1543 return 0;
1544}
1545
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301546static void hdmi_codec_complete(struct device *dev)
Jeeja KP571d5072016-02-22 07:50:33 +05301547{
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301548 struct hdac_ext_device *edev = to_hda_ext_device(dev);
Jeeja KP571d5072016-02-22 07:50:33 +05301549 struct hdac_hdmi_priv *hdmi = edev->private_data;
1550 struct hdac_hdmi_pin *pin;
1551 struct hdac_device *hdac = &edev->hdac;
Jeeja KP754695f2017-02-06 12:09:14 +05301552 int i;
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301553
1554 /* Power up afg */
1555 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1556 AC_PWRST_D0);
Jeeja KP571d5072016-02-22 07:50:33 +05301557
1558 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1559 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1560
Jeeja KP571d5072016-02-22 07:50:33 +05301561 /*
1562 * As the ELD notify callback request is not entertained while the
1563 * device is in suspend state. Need to manually check detection of
1564 * all pins here.
1565 */
1566 list_for_each_entry(pin, &hdmi->pin_list, head)
Jeeja KP754695f2017-02-06 12:09:14 +05301567 for (i = 0; i < pin->num_ports; i++)
1568 hdac_hdmi_present_sense(pin, &pin->ports[i]);
Jeeja KP571d5072016-02-22 07:50:33 +05301569
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301570 pm_runtime_put_sync(&edev->hdac.dev);
Jeeja KP571d5072016-02-22 07:50:33 +05301571}
1572#else
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301573#define hdmi_codec_prepare NULL
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301574#define hdmi_codec_complete NULL
Jeeja KP571d5072016-02-22 07:50:33 +05301575#endif
1576
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301577static struct snd_soc_codec_driver hdmi_hda_codec = {
1578 .probe = hdmi_codec_probe,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301579 .remove = hdmi_codec_remove,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301580 .idle_bias_off = true,
1581};
1582
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301583static void hdac_hdmi_get_chmap(struct hdac_device *hdac, int pcm_idx,
1584 unsigned char *chmap)
1585{
1586 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1587 struct hdac_hdmi_priv *hdmi = edev->private_data;
1588 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301589
Jeeja KPab1eea12017-01-24 21:49:05 +05301590 memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301591}
1592
1593static void hdac_hdmi_set_chmap(struct hdac_device *hdac, int pcm_idx,
1594 unsigned char *chmap, int prepared)
1595{
1596 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1597 struct hdac_hdmi_priv *hdmi = edev->private_data;
1598 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Jeeja KP754695f2017-02-06 12:09:14 +05301599 struct hdac_hdmi_port *port = pcm->port;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301600
Jeeja KPab1eea12017-01-24 21:49:05 +05301601 mutex_lock(&pcm->lock);
1602 pcm->chmap_set = true;
1603 memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301604 if (prepared)
Jeeja KP754695f2017-02-06 12:09:14 +05301605 hdac_hdmi_setup_audio_infoframe(edev, pcm, port);
Jeeja KPab1eea12017-01-24 21:49:05 +05301606 mutex_unlock(&pcm->lock);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301607}
1608
1609static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdac, int pcm_idx)
1610{
1611 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1612 struct hdac_hdmi_priv *hdmi = edev->private_data;
1613 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Jeeja KP754695f2017-02-06 12:09:14 +05301614 struct hdac_hdmi_port *port = pcm->port;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301615
Jeeja KP754695f2017-02-06 12:09:14 +05301616 return port ? true:false;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301617}
1618
1619static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdac, int pcm_idx)
1620{
1621 struct hdac_ext_device *edev = to_ehdac_device(hdac);
1622 struct hdac_hdmi_priv *hdmi = edev->private_data;
1623 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Jeeja KP754695f2017-02-06 12:09:14 +05301624 struct hdac_hdmi_port *port = pcm->port;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301625
Jeeja KP754695f2017-02-06 12:09:14 +05301626 if (!port || !port->eld.eld_valid)
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301627 return 0;
1628
Jeeja KP754695f2017-02-06 12:09:14 +05301629 return port->eld.info.spk_alloc;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301630}
1631
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301632static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
1633{
1634 struct hdac_device *codec = &edev->hdac;
1635 struct hdac_hdmi_priv *hdmi_priv;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301636 struct snd_soc_dai_driver *hdmi_dais = NULL;
Vinod Koulb2047e92016-05-12 08:58:55 +05301637 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301638 int num_dais = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301639 int ret = 0;
1640
Vinod Koulb2047e92016-05-12 08:58:55 +05301641 /* hold the ref while we probe */
1642 hlink = snd_hdac_ext_bus_get_link(edev->ebus, dev_name(&edev->hdac.dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301643 if (!hlink) {
1644 dev_err(&edev->hdac.dev, "hdac link not found\n");
1645 return -EIO;
1646 }
1647
Vinod Koulb2047e92016-05-12 08:58:55 +05301648 snd_hdac_ext_bus_link_get(edev->ebus, hlink);
1649
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301650 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1651 if (hdmi_priv == NULL)
1652 return -ENOMEM;
1653
1654 edev->private_data = hdmi_priv;
Subhransu S. Prustybcced702016-04-14 10:07:30 +05301655 snd_hdac_register_chmap_ops(codec, &hdmi_priv->chmap);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301656 hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
1657 hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
1658 hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
1659 hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301660
1661 dev_set_drvdata(&codec->dev, edev);
1662
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301663 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1664 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301665 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1666 mutex_init(&hdmi_priv->pin_mutex);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301667
Ramesh Babuaeaccef2016-02-17 21:34:01 +05301668 /*
1669 * Turned off in the runtime_suspend during the first explicit
1670 * pm_runtime_suspend call.
1671 */
1672 ret = snd_hdac_display_power(edev->hdac.bus, true);
1673 if (ret < 0) {
1674 dev_err(&edev->hdac.dev,
1675 "Cannot turn on display power on i915 err: %d\n",
1676 ret);
1677 return ret;
1678 }
1679
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301680 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1681 if (ret < 0) {
1682 dev_err(&codec->dev,
1683 "Failed in parse and map nid with err: %d\n", ret);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301684 return ret;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301685 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301686
1687 /* ASoC specific initialization */
Vinod Koulb2047e92016-05-12 08:58:55 +05301688 ret = snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
1689 hdmi_dais, num_dais);
1690
1691 snd_hdac_ext_bus_link_put(edev->ebus, hlink);
1692
1693 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301694}
1695
1696static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
1697{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301698 struct hdac_hdmi_priv *hdmi = edev->private_data;
1699 struct hdac_hdmi_pin *pin, *pin_next;
1700 struct hdac_hdmi_cvt *cvt, *cvt_next;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301701 struct hdac_hdmi_pcm *pcm, *pcm_next;
Jeeja KP754695f2017-02-06 12:09:14 +05301702 int i;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301703
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301704 snd_soc_unregister_codec(&edev->hdac.dev);
1705
Jeeja KP4a3478d2016-02-12 07:46:06 +05301706 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1707 pcm->cvt = NULL;
Jeeja KP754695f2017-02-06 12:09:14 +05301708 pcm->port = NULL;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301709 list_del(&pcm->head);
1710 kfree(pcm);
1711 }
1712
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301713 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
1714 list_del(&cvt->head);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301715 kfree(cvt->name);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301716 kfree(cvt);
1717 }
1718
1719 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301720 for (i = 0; i < pin->num_ports; i++)
1721 pin->ports[i].pin = NULL;
1722 kfree(pin->ports);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301723 list_del(&pin->head);
1724 kfree(pin);
1725 }
1726
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301727 return 0;
1728}
1729
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301730#ifdef CONFIG_PM
1731static int hdac_hdmi_runtime_suspend(struct device *dev)
1732{
1733 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1734 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301735 struct hdac_bus *bus = hdac->bus;
Vinod Koulb2047e92016-05-12 08:58:55 +05301736 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1737 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301738 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301739
1740 dev_dbg(dev, "Enter: %s\n", __func__);
1741
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301742 /* controller may not have been initialized for the first time */
1743 if (!bus)
1744 return 0;
1745
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301746 /*
1747 * Power down afg.
1748 * codec_read is preferred over codec_write to set the power state.
1749 * This way verb is send to set the power state and response
1750 * is received. So setting power state is ensured without using loop
1751 * to read the state.
1752 */
1753 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1754 AC_PWRST_D3);
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301755 err = snd_hdac_display_power(bus, false);
1756 if (err < 0) {
1757 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1758 return err;
1759 }
1760
Vinod Koulb2047e92016-05-12 08:58:55 +05301761 hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301762 if (!hlink) {
1763 dev_err(dev, "hdac link not found\n");
1764 return -EIO;
1765 }
1766
Vinod Koulb2047e92016-05-12 08:58:55 +05301767 snd_hdac_ext_bus_link_put(ebus, hlink);
1768
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301769 return 0;
1770}
1771
1772static int hdac_hdmi_runtime_resume(struct device *dev)
1773{
1774 struct hdac_ext_device *edev = to_hda_ext_device(dev);
1775 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301776 struct hdac_bus *bus = hdac->bus;
Vinod Koulb2047e92016-05-12 08:58:55 +05301777 struct hdac_ext_bus *ebus = hbus_to_ebus(bus);
1778 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301779 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301780
1781 dev_dbg(dev, "Enter: %s\n", __func__);
1782
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301783 /* controller may not have been initialized for the first time */
1784 if (!bus)
1785 return 0;
1786
Vinod Koulb2047e92016-05-12 08:58:55 +05301787 hlink = snd_hdac_ext_bus_get_link(ebus, dev_name(dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301788 if (!hlink) {
1789 dev_err(dev, "hdac link not found\n");
1790 return -EIO;
1791 }
1792
Vinod Koulb2047e92016-05-12 08:58:55 +05301793 snd_hdac_ext_bus_link_get(ebus, hlink);
1794
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05301795 err = snd_hdac_display_power(bus, true);
1796 if (err < 0) {
1797 dev_err(bus->dev, "Cannot turn on display power on i915\n");
1798 return err;
1799 }
1800
Subhransu S. Prustyab85f5b2016-02-17 21:34:02 +05301801 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1802 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1803
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301804 /* Power up afg */
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301805 snd_hdac_codec_read(hdac, hdac->afg, 0, AC_VERB_SET_POWER_STATE,
1806 AC_PWRST_D0);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301807
1808 return 0;
1809}
1810#else
1811#define hdac_hdmi_runtime_suspend NULL
1812#define hdac_hdmi_runtime_resume NULL
1813#endif
1814
1815static const struct dev_pm_ops hdac_hdmi_pm = {
1816 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301817 .prepare = hdmi_codec_prepare,
Subhransu S. Prusty0fee1792016-04-01 13:36:25 +05301818 .complete = hdmi_codec_complete,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301819};
1820
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301821static const struct hda_device_id hdmi_list[] = {
1822 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
Jeeja KPe2304802016-03-11 10:12:55 +05301823 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
Shreyas NCcc216882016-07-11 22:02:09 +05301824 HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301825 {}
1826};
1827
1828MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
1829
1830static struct hdac_ext_driver hdmi_driver = {
1831 . hdac = {
1832 .driver = {
1833 .name = "HDMI HDA Codec",
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301834 .pm = &hdac_hdmi_pm,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301835 },
1836 .id_table = hdmi_list,
1837 },
1838 .probe = hdac_hdmi_dev_probe,
1839 .remove = hdac_hdmi_dev_remove,
1840};
1841
1842static int __init hdmi_init(void)
1843{
1844 return snd_hda_ext_driver_register(&hdmi_driver);
1845}
1846
1847static void __exit hdmi_exit(void)
1848{
1849 snd_hda_ext_driver_unregister(&hdmi_driver);
1850}
1851
1852module_init(hdmi_init);
1853module_exit(hdmi_exit);
1854
1855MODULE_LICENSE("GPL v2");
1856MODULE_DESCRIPTION("HDMI HD codec");
1857MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
1858MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");