blob: b19d7a3e7a2cc07827808ac2fe92797deac56a37 [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;
Rakesh Ughreja3787a392018-06-01 22:53:49 -050088 struct hdac_device *hdev;
Jeeja KP754695f2017-02-06 12:09:14 +053089};
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
Bard liao019033c2018-11-11 05:18:46 +0800124/*
125 * pin to port mapping table where the value indicate the pin number and
126 * the index indicate the port number with 1 base.
127 */
128static const int icl_pin2port_map[] = {0x4, 0x6, 0x8, 0xa, 0xb};
129
Pradeep Tewani5622bc92017-07-20 11:40:56 +0530130struct hdac_hdmi_drv_data {
131 unsigned int vendor_nid;
Bard liao019033c2018-11-11 05:18:46 +0800132 const int *port_map; /* pin to port mapping table */
133 int port_num;
Pradeep Tewani5622bc92017-07-20 11:40:56 +0530134};
135
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530136struct hdac_hdmi_priv {
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500137 struct hdac_device *hdev;
138 struct snd_soc_component *component;
139 struct snd_card *card;
Jeeja KP754695f2017-02-06 12:09:14 +0530140 struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530141 struct list_head pin_list;
142 struct list_head cvt_list;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530143 struct list_head pcm_list;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530144 int num_pin;
145 int num_cvt;
Jeeja KP754695f2017-02-06 12:09:14 +0530146 int num_ports;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530147 struct mutex pin_mutex;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530148 struct hdac_chmap chmap;
Pradeep Tewani5622bc92017-07-20 11:40:56 +0530149 struct hdac_hdmi_drv_data *drv_data;
Kuninori Morimoto1e02dac2017-12-20 01:48:13 +0000150 struct snd_soc_dai_driver *dai_drv;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530151};
152
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500153#define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
Ughreja, Rakesh Ab09b1c32017-12-01 14:43:17 +0530154
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530155static struct hdac_hdmi_pcm *
156hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
157 struct hdac_hdmi_cvt *cvt)
158{
159 struct hdac_hdmi_pcm *pcm = NULL;
Jeeja KP1de777f2017-01-10 17:57:48 +0530160
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530161 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
162 if (pcm->cvt == cvt)
163 break;
164 }
165
166 return pcm;
167}
Jeeja KP1de777f2017-01-10 17:57:48 +0530168
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530169static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
170 struct hdac_hdmi_port *port, bool is_connect)
171{
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500172 struct hdac_device *hdev = port->pin->hdev;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530173
Jeeja KP0324e512017-02-07 19:09:55 +0530174 if (is_connect)
175 snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
176 else
177 snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
178
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530179 if (is_connect) {
180 /*
181 * Report Jack connect event when a device is connected
182 * for the first time where same PCM is attached to multiple
183 * ports.
184 */
185 if (pcm->jack_event == 0) {
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500186 dev_dbg(&hdev->dev,
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530187 "jack report for pcm=%d\n",
188 pcm->pcm_id);
Jeeja KP62490012017-02-07 19:09:49 +0530189 snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
190 SND_JACK_AVOUT);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530191 }
192 pcm->jack_event++;
193 } else {
194 /*
195 * Report Jack disconnect event when a device is disconnected
196 * is the only last connected device when same PCM is attached
197 * to multiple ports.
198 */
199 if (pcm->jack_event == 1)
Jeeja KP62490012017-02-07 19:09:49 +0530200 snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530201 if (pcm->jack_event > 0)
202 pcm->jack_event--;
203 }
Jeeja KP0324e512017-02-07 19:09:55 +0530204
205 snd_soc_dapm_sync(port->dapm);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530206}
207
Jeeja KPfc181b02017-02-07 19:09:45 +0530208/* MST supported verbs */
209/*
210 * Get the no devices that can be connected to a port on the Pin widget.
211 */
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500212static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
Jeeja KPfc181b02017-02-07 19:09:45 +0530213{
214 unsigned int caps;
215 unsigned int type, param;
216
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500217 caps = get_wcaps(hdev, nid);
Jeeja KPfc181b02017-02-07 19:09:45 +0530218 type = get_wcaps_type(caps);
219
220 if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
221 return 0;
222
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500223 param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
Jeeja KPfc181b02017-02-07 19:09:45 +0530224 if (param == -1)
225 return param;
226
227 return param & AC_DEV_LIST_LEN_MASK;
228}
229
230/*
231 * Get the port entry select on the pin. Return the port entry
232 * id selected on the pin. Return 0 means the first port entry
233 * is selected or MST is not supported.
234 */
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500235static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
Jeeja KPfc181b02017-02-07 19:09:45 +0530236 struct hdac_hdmi_port *port)
237{
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500238 return snd_hdac_codec_read(hdev, port->pin->nid,
Jeeja KPfc181b02017-02-07 19:09:45 +0530239 0, AC_VERB_GET_DEVICE_SEL, 0);
240}
241
242/*
243 * Sets the selected port entry for the configuring Pin widget verb.
244 * returns error if port set is not equal to port get otherwise success
245 */
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500246static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
Jeeja KPfc181b02017-02-07 19:09:45 +0530247 struct hdac_hdmi_port *port)
248{
249 int num_ports;
250
251 if (!port->pin->mst_capable)
252 return 0;
253
254 /* AC_PAR_DEVLIST_LEN is 0 based. */
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500255 num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
Jeeja KPfc181b02017-02-07 19:09:45 +0530256 if (num_ports < 0)
257 return -EIO;
258 /*
259 * Device List Length is a 0 based integer value indicating the
260 * number of sink device that a MST Pin Widget can support.
261 */
262 if (num_ports + 1 < port->id)
263 return 0;
264
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500265 snd_hdac_codec_write(hdev, port->pin->nid, 0,
Jeeja KPfc181b02017-02-07 19:09:45 +0530266 AC_VERB_SET_DEVICE_SEL, port->id);
267
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500268 if (port->id != hdac_hdmi_port_select_get(hdev, port))
Jeeja KPfc181b02017-02-07 19:09:45 +0530269 return -EIO;
270
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500271 dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
Jeeja KPfc181b02017-02-07 19:09:45 +0530272
273 return 0;
274}
275
Subhransu S. Prusty28890992016-04-14 10:07:34 +0530276static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
277 int pcm_idx)
278{
279 struct hdac_hdmi_pcm *pcm;
280
281 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
282 if (pcm->pcm_id == pcm_idx)
283 return pcm;
284 }
285
286 return NULL;
287}
288
Subhransu S. Prusty2428bca2016-02-12 07:46:02 +0530289static unsigned int sad_format(const u8 *sad)
290{
291 return ((sad[0] >> 0x3) & 0x1f);
292}
293
294static unsigned int sad_sample_bits_lpcm(const u8 *sad)
295{
296 return (sad[2] & 7);
297}
298
299static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
300 void *eld)
301{
302 u64 formats = SNDRV_PCM_FMTBIT_S16;
303 int i;
304 const u8 *sad, *eld_buf = eld;
305
306 sad = drm_eld_sad(eld_buf);
307 if (!sad)
308 goto format_constraint;
309
310 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
311 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
312
313 /*
314 * the controller support 20 and 24 bits in 32 bit
315 * container so we set S32
316 */
317 if (sad_sample_bits_lpcm(sad) & 0x6)
318 formats |= SNDRV_PCM_FMTBIT_S32;
319 }
320 }
321
322format_constraint:
323 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
324 formats);
325
326}
327
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530328static void
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500329hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530330 int packet_index, int byte_index)
331{
332 int val;
333
334 val = (packet_index << 5) | (byte_index & 0x1f);
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500335 snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530336}
337
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530338struct dp_audio_infoframe {
339 u8 type; /* 0x84 */
340 u8 len; /* 0x1b */
341 u8 ver; /* 0x11 << 2 */
342
343 u8 CC02_CT47; /* match with HDMI infoframe from this on */
344 u8 SS01_SF24;
345 u8 CXT04;
346 u8 CA;
347 u8 LFEPBL01_LSV36_DM_INH7;
348};
349
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500350static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
Jeeja KP754695f2017-02-06 12:09:14 +0530351 struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530352{
353 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
354 struct hdmi_audio_infoframe frame;
Jeeja KP754695f2017-02-06 12:09:14 +0530355 struct hdac_hdmi_pin *pin = port->pin;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530356 struct dp_audio_infoframe dp_ai;
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500357 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Jeeja KPab1eea12017-01-24 21:49:05 +0530358 struct hdac_hdmi_cvt *cvt = pcm->cvt;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530359 u8 *dip;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530360 int ret;
361 int i;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530362 const u8 *eld_buf;
363 u8 conn_type;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530364 int channels, ca;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530365
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500366 ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc,
Jeeja KPab1eea12017-01-24 21:49:05 +0530367 pcm->channels, pcm->chmap_set, true, pcm->chmap);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530368
369 channels = snd_hdac_get_active_channels(ca);
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500370 hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530371
372 snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
Jeeja KPab1eea12017-01-24 21:49:05 +0530373 pcm->channels, pcm->chmap, pcm->chmap_set);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530374
Jeeja KP754695f2017-02-06 12:09:14 +0530375 eld_buf = port->eld.eld_buffer;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530376 conn_type = drm_eld_get_conn_type(eld_buf);
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530377
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530378 switch (conn_type) {
379 case DRM_ELD_CONN_TYPE_HDMI:
380 hdmi_audio_infoframe_init(&frame);
381
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530382 frame.channels = channels;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530383 frame.channel_allocation = ca;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530384
385 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
386 if (ret < 0)
387 return ret;
388
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530389 break;
390
391 case DRM_ELD_CONN_TYPE_DP:
392 memset(&dp_ai, 0, sizeof(dp_ai));
393 dp_ai.type = 0x84;
394 dp_ai.len = 0x1b;
395 dp_ai.ver = 0x11 << 2;
396 dp_ai.CC02_CT47 = channels - 1;
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530397 dp_ai.CA = ca;
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530398
399 dip = (u8 *)&dp_ai;
400 break;
401
402 default:
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500403 dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
Subhransu S. Prusty478f544e2016-02-12 07:46:09 +0530404 return -EIO;
405 }
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530406
407 /* stop infoframe transmission */
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500408 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
409 snd_hdac_codec_write(hdev, 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 */
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500414 hdac_hdmi_set_dip_index(hdev, 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++)
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500417 snd_hdac_codec_write(hdev, 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++)
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500421 snd_hdac_codec_write(hdev, 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 */
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500426 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
427 snd_hdac_codec_write(hdev, 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{
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500437 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
438 struct hdac_device *hdev = hdmi->hdev;
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
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500442 dev_dbg(&hdev->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{
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500457 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
458 struct hdac_device *hdev = hdmi->hdev;
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)) {
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500471 dev_err(&hdev->dev,
Jeeja KP754695f2017-02-06 12:09:14 +0530472 "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
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500491static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
Jeeja KP754695f2017-02-06 12:09:14 +0530492 struct hdac_hdmi_pin *pin,
493 struct hdac_hdmi_port *port)
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530494{
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500495 if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
496 dev_warn(&hdev->dev,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530497 "HDMI: pin %d wcaps %#x does not support connection list\n",
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500498 pin->nid, get_wcaps(hdev, pin->nid));
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530499 return -EINVAL;
500 }
501
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500502 if (hdac_hdmi_port_select_set(hdev, port) < 0)
Jeeja KP1b46ebd2017-02-07 19:09:47 +0530503 return -EIO;
504
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500505 port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
Jeeja KP754695f2017-02-06 12:09:14 +0530506 port->mux_nids, HDA_MAX_CONNECTIONS);
507 if (port->num_mux_nids == 0)
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500508 dev_warn(&hdev->dev,
Jeeja KP754695f2017-02-06 12:09:14 +0530509 "No connections found for pin:port %d:%d\n",
510 pin->nid, port->id);
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530511
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500512 dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
Jeeja KP754695f2017-02-06 12:09:14 +0530513 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(
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500528 struct hdac_device *hdev,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +0530529 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);
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500543 ret = hdac_hdmi_query_port_connlist(hdev,
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530544 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{
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500570 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
571 struct hdac_device *hdev = hdmi->hdev;
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;
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500580 port = hdac_hdmi_get_port_from_cvt(hdev, 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
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500591 dev_warn(&hdev->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{
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500613 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
Jeeja KP754695f2017-02-06 12:09:14 +0530614 struct hdac_hdmi_dai_port_map *dai_map;
Jeeja KPab1eea12017-01-24 21:49:05 +0530615 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530616
617 dai_map = &hdmi->dai_map[dai->id];
618
Jeeja KPab1eea12017-01-24 21:49:05 +0530619 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530620
Jeeja KPab1eea12017-01-24 21:49:05 +0530621 if (pcm) {
622 mutex_lock(&pcm->lock);
623 pcm->chmap_set = false;
624 memset(pcm->chmap, 0, sizeof(pcm->chmap));
625 pcm->channels = 0;
626 mutex_unlock(&pcm->lock);
Subhransu S. Prusty54dfa1e2016-02-17 21:34:00 +0530627 }
Jeeja KPab1eea12017-01-24 21:49:05 +0530628
Jeeja KP754695f2017-02-06 12:09:14 +0530629 if (dai_map->port)
630 dai_map->port = NULL;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530631}
632
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530633static int
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +0530634hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530635{
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530636 unsigned int chans;
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +0530637 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530638 int err;
639
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +0530640 chans = get_wcaps(hdev, cvt->nid);
Subhransu S. Prustybcced702016-04-14 10:07:30 +0530641 chans = get_wcaps_channels(chans);
642
643 cvt->params.channels_min = 2;
644
645 cvt->params.channels_max = chans;
646 if (chans > hdmi->chmap.channels_max)
647 hdmi->chmap.channels_max = chans;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530648
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +0530649 err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530650 &cvt->params.rates,
651 &cvt->params.formats,
652 &cvt->params.maxbps);
653 if (err < 0)
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +0530654 dev_err(&hdev->dev,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530655 "Failed to query pcm params for nid %d: %d\n",
656 cvt->nid, err);
657
658 return err;
659}
660
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530661static int hdac_hdmi_fill_widget_info(struct device *dev,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530662 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
663 void *priv, const char *wname, const char *stream,
664 struct snd_kcontrol_new *wc, int numkc,
665 int (*event)(struct snd_soc_dapm_widget *,
666 struct snd_kcontrol *, int), unsigned short event_flags)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530667{
668 w->id = id;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530669 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
670 if (!w->name)
671 return -ENOMEM;
672
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530673 w->sname = stream;
674 w->reg = SND_SOC_NOPM;
675 w->shift = 0;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530676 w->kcontrol_news = wc;
677 w->num_kcontrols = numkc;
678 w->priv = priv;
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530679 w->event = event;
680 w->event_flags = event_flags;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530681
682 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530683}
684
685static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530686 const char *sink, const char *control, const char *src,
687 int (*handler)(struct snd_soc_dapm_widget *src,
688 struct snd_soc_dapm_widget *sink))
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530689{
690 route->sink = sink;
691 route->source = src;
692 route->control = control;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530693 route->connected = handler;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530694}
695
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500696static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
Jeeja KP754695f2017-02-06 12:09:14 +0530697 struct hdac_hdmi_port *port)
Jeeja KP4a3478d2016-02-12 07:46:06 +0530698{
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500699 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Jeeja KP4a3478d2016-02-12 07:46:06 +0530700 struct hdac_hdmi_pcm *pcm = NULL;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530701 struct hdac_hdmi_port *p;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530702
703 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530704 if (list_empty(&pcm->port_list))
Jeeja KP754695f2017-02-06 12:09:14 +0530705 continue;
706
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530707 list_for_each_entry(p, &pcm->port_list, head) {
708 if (p->id == port->id && port->pin == p->pin)
709 return pcm;
710 }
Jeeja KP4a3478d2016-02-12 07:46:06 +0530711 }
712
713 return NULL;
714}
715
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500716static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530717 hda_nid_t nid, unsigned int pwr_state)
718{
Abhijeet Kumar753597f2018-02-15 14:05:38 +0530719 int count;
720 unsigned int state;
721
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500722 if (get_wcaps(hdev, nid) & AC_WCAP_POWER) {
723 if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
Abhijeet Kumar753597f2018-02-15 14:05:38 +0530724 for (count = 0; count < 10; count++) {
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500725 snd_hdac_codec_read(hdev, nid, 0,
Abhijeet Kumar753597f2018-02-15 14:05:38 +0530726 AC_VERB_SET_POWER_STATE,
727 pwr_state);
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500728 state = snd_hdac_sync_power_state(hdev,
Abhijeet Kumar753597f2018-02-15 14:05:38 +0530729 nid, pwr_state);
730 if (!(state & AC_PWRST_ERROR))
731 break;
732 }
733 }
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530734 }
735}
736
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500737static void hdac_hdmi_set_amp(struct hdac_device *hdev,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530738 hda_nid_t nid, int val)
739{
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500740 if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
741 snd_hdac_codec_write(hdev, nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530742 AC_VERB_SET_AMP_GAIN_MUTE, val);
743}
744
745
746static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
747 struct snd_kcontrol *kc, int event)
748{
Jeeja KP754695f2017-02-06 12:09:14 +0530749 struct hdac_hdmi_port *port = w->priv;
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500750 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530751 struct hdac_hdmi_pcm *pcm;
752
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500753 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530754 __func__, w->name, event);
755
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500756 pcm = hdac_hdmi_get_pcm(hdev, port);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530757 if (!pcm)
758 return -EIO;
759
Jeeja KP1b46ebd2017-02-07 19:09:47 +0530760 /* set the device if pin is mst_capable */
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500761 if (hdac_hdmi_port_select_set(hdev, port) < 0)
Jeeja KP1b46ebd2017-02-07 19:09:47 +0530762 return -EIO;
763
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530764 switch (event) {
765 case SND_SOC_DAPM_PRE_PMU:
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500766 hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530767
768 /* Enable out path for this pin widget */
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500769 snd_hdac_codec_write(hdev, port->pin->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530770 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
771
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500772 hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530773
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500774 return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530775
776 case SND_SOC_DAPM_POST_PMD:
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500777 hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530778
779 /* Disable out path for this pin widget */
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500780 snd_hdac_codec_write(hdev, port->pin->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530781 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
782
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500783 hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530784 break;
785
786 }
787
788 return 0;
789}
790
791static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
792 struct snd_kcontrol *kc, int event)
793{
794 struct hdac_hdmi_cvt *cvt = w->priv;
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500795 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
796 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530797 struct hdac_hdmi_pcm *pcm;
798
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500799 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530800 __func__, w->name, event);
801
802 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
803 if (!pcm)
804 return -EIO;
805
806 switch (event) {
807 case SND_SOC_DAPM_PRE_PMU:
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500808 hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530809
810 /* Enable transmission */
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500811 snd_hdac_codec_write(hdev, cvt->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530812 AC_VERB_SET_DIGI_CONVERT_1, 1);
813
814 /* Category Code (CC) to zero */
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500815 snd_hdac_codec_write(hdev, cvt->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530816 AC_VERB_SET_DIGI_CONVERT_2, 0);
817
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500818 snd_hdac_codec_write(hdev, cvt->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530819 AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500820 snd_hdac_codec_write(hdev, cvt->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530821 AC_VERB_SET_STREAM_FORMAT, pcm->format);
822 break;
823
824 case SND_SOC_DAPM_POST_PMD:
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500825 snd_hdac_codec_write(hdev, cvt->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530826 AC_VERB_SET_CHANNEL_STREAMID, 0);
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500827 snd_hdac_codec_write(hdev, cvt->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530828 AC_VERB_SET_STREAM_FORMAT, 0);
829
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500830 hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530831 break;
832
833 }
834
835 return 0;
836}
837
838static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
839 struct snd_kcontrol *kc, int event)
840{
Jeeja KP754695f2017-02-06 12:09:14 +0530841 struct hdac_hdmi_port *port = w->priv;
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500842 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530843 int mux_idx;
844
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500845 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530846 __func__, w->name, event);
847
848 if (!kc)
849 kc = w->kcontrols[0];
850
851 mux_idx = dapm_kcontrol_get_value(kc);
Jeeja KP1b46ebd2017-02-07 19:09:47 +0530852
853 /* set the device if pin is mst_capable */
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500854 if (hdac_hdmi_port_select_set(hdev, port) < 0)
Jeeja KP1b46ebd2017-02-07 19:09:47 +0530855 return -EIO;
856
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530857 if (mux_idx > 0) {
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500858 snd_hdac_codec_write(hdev, port->pin->nid, 0,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530859 AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
860 }
861
862 return 0;
863}
864
Jeeja KP4a3478d2016-02-12 07:46:06 +0530865/*
866 * Based on user selection, map the PINs with the PCMs.
867 */
Jeeja KP754695f2017-02-06 12:09:14 +0530868static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
Jeeja KP4a3478d2016-02-12 07:46:06 +0530869 struct snd_ctl_elem_value *ucontrol)
870{
871 int ret;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530872 struct hdac_hdmi_port *p, *p_next;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530873 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
874 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
875 struct snd_soc_dapm_context *dapm = w->dapm;
Jeeja KP754695f2017-02-06 12:09:14 +0530876 struct hdac_hdmi_port *port = w->priv;
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500877 struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
878 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Jeeja KP4a3478d2016-02-12 07:46:06 +0530879 struct hdac_hdmi_pcm *pcm = NULL;
880 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
881
882 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
883 if (ret < 0)
884 return ret;
885
Jeeja KP754695f2017-02-06 12:09:14 +0530886 if (port == NULL)
887 return -EINVAL;
888
Jeeja KP4a3478d2016-02-12 07:46:06 +0530889 mutex_lock(&hdmi->pin_mutex);
890 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530891 if (list_empty(&pcm->port_list))
892 continue;
Jeeja KP4a3478d2016-02-12 07:46:06 +0530893
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530894 list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
895 if (p == port && p->id == port->id &&
896 p->pin == port->pin) {
897 hdac_hdmi_jack_report(pcm, port, false);
898 list_del(&p->head);
Jeeja KP4a3478d2016-02-12 07:46:06 +0530899 }
Jeeja KPe0e5d3e2017-02-07 19:09:48 +0530900 }
901 }
902
903 /*
904 * Jack status is not reported during device probe as the
905 * PCMs are not registered by then. So report it here.
906 */
907 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
908 if (!strcmp(cvt_name, pcm->cvt->name)) {
909 list_add_tail(&port->head, &pcm->port_list);
910 if (port->eld.monitor_present && port->eld.eld_valid) {
911 hdac_hdmi_jack_report(pcm, port, true);
912 mutex_unlock(&hdmi->pin_mutex);
913 return ret;
914 }
Jeeja KP4a3478d2016-02-12 07:46:06 +0530915 }
916 }
917 mutex_unlock(&hdmi->pin_mutex);
918
919 return ret;
920}
921
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530922/*
923 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
924 * the display driver seem to be programming the connection list for the pin
925 * widget runtime.
926 *
927 * So programming all the possible inputs for the mux, the user has to take
928 * care of selecting the right one and leaving all other inputs selected to
929 * "NONE"
930 */
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500931static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
Jeeja KP754695f2017-02-06 12:09:14 +0530932 struct hdac_hdmi_port *port,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530933 struct snd_soc_dapm_widget *widget,
934 const char *widget_name)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530935{
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500936 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Jeeja KP754695f2017-02-06 12:09:14 +0530937 struct hdac_hdmi_pin *pin = port->pin;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530938 struct snd_kcontrol_new *kc;
939 struct hdac_hdmi_cvt *cvt;
940 struct soc_enum *se;
941 char kc_name[NAME_SIZE];
942 char mux_items[NAME_SIZE];
943 /* To hold inputs to the Pin mux */
944 char *items[HDA_MAX_CONNECTIONS];
945 int i = 0;
946 int num_items = hdmi->num_cvt + 1;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530947
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500948 kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530949 if (!kc)
950 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530951
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500952 se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530953 if (!se)
954 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530955
Subhransu S. Prusty70e97a22017-11-07 16:16:24 +0530956 snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
957 pin->nid, port->id);
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500958 kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530959 if (!kc->name)
960 return -ENOMEM;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530961
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530962 kc->private_value = (long)se;
963 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
964 kc->access = 0;
965 kc->info = snd_soc_info_enum_double;
Jeeja KP754695f2017-02-06 12:09:14 +0530966 kc->put = hdac_hdmi_set_pin_port_mux;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530967 kc->get = snd_soc_dapm_get_enum_double;
968
969 se->reg = SND_SOC_NOPM;
970
971 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
972 se->items = num_items;
973 se->mask = roundup_pow_of_two(se->items) - 1;
974
975 sprintf(mux_items, "NONE");
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500976 items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530977 if (!items[i])
978 return -ENOMEM;
979
980 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
981 i++;
982 sprintf(mux_items, "cvt %d", cvt->nid);
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500983 items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530984 if (!items[i])
985 return -ENOMEM;
986 }
987
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500988 se->texts = devm_kmemdup(&hdev->dev, items,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530989 (num_items * sizeof(char *)), GFP_KERNEL);
990 if (!se->texts)
991 return -ENOMEM;
992
Rakesh Ughreja3787a392018-06-01 22:53:49 -0500993 return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
Jeeja KP754695f2017-02-06 12:09:14 +0530994 snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +0530995 hdac_hdmi_pin_mux_widget_event,
996 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +0530997}
998
999/* Add cvt <- input <- mux route map */
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001000static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301001 struct snd_soc_dapm_widget *widgets,
1002 struct snd_soc_dapm_route *route, int rindex)
1003{
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001004 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301005 const struct snd_kcontrol_new *kc;
1006 struct soc_enum *se;
Jeeja KP754695f2017-02-06 12:09:14 +05301007 int mux_index = hdmi->num_cvt + hdmi->num_ports;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301008 int i, j;
1009
Jeeja KP754695f2017-02-06 12:09:14 +05301010 for (i = 0; i < hdmi->num_ports; i++) {
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301011 kc = widgets[mux_index].kcontrol_news;
1012 se = (struct soc_enum *)kc->private_value;
1013 for (j = 0; j < hdmi->num_cvt; j++) {
1014 hdac_hdmi_fill_route(&route[rindex],
1015 widgets[mux_index].name,
1016 se->texts[j + 1],
1017 widgets[j].name, NULL);
1018
1019 rindex++;
1020 }
1021
1022 mux_index++;
1023 }
1024}
1025
1026/*
1027 * Widgets are added in the below sequence
1028 * Converter widgets for num converters enumerated
Jeeja KP754695f2017-02-06 12:09:14 +05301029 * Pin-port widgets for num ports for Pins enumerated
1030 * Pin-port mux widgets to represent connenction list of pin widget
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301031 *
Jeeja KP754695f2017-02-06 12:09:14 +05301032 * For each port, one Mux and One output widget is added
1033 * Total widgets elements = num_cvt + (num_ports * 2);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301034 *
1035 * Routes are added as below:
Jeeja KP754695f2017-02-06 12:09:14 +05301036 * pin-port mux -> pin (based on num_ports)
1037 * cvt -> "Input sel control" -> pin-port_mux
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301038 *
1039 * Total route elements:
Jeeja KP754695f2017-02-06 12:09:14 +05301040 * num_ports + (pin_muxes * num_cvt)
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301041 */
1042static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
1043{
1044 struct snd_soc_dapm_widget *widgets;
1045 struct snd_soc_dapm_route *route;
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001046 struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
1047 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Kuninori Morimoto1e02dac2017-12-20 01:48:13 +00001048 struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301049 char widget_name[NAME_SIZE];
1050 struct hdac_hdmi_cvt *cvt;
1051 struct hdac_hdmi_pin *pin;
Jeeja KP754695f2017-02-06 12:09:14 +05301052 int ret, i = 0, num_routes = 0, j;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301053
1054 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
1055 return -EINVAL;
1056
Jeeja KP754695f2017-02-06 12:09:14 +05301057 widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1058 ((2 * hdmi->num_ports) + hdmi->num_cvt)),
1059 GFP_KERNEL);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301060
1061 if (!widgets)
1062 return -ENOMEM;
1063
1064 /* DAPM widgets to represent each converter widget */
1065 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1066 sprintf(widget_name, "Converter %d", cvt->nid);
1067 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
Jeeja KPc9bfb5d2017-01-24 21:49:03 +05301068 snd_soc_dapm_aif_in, cvt,
1069 widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1070 hdac_hdmi_cvt_output_widget_event,
1071 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301072 if (ret < 0)
1073 return ret;
1074 i++;
1075 }
1076
1077 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301078 for (j = 0; j < pin->num_ports; j++) {
1079 sprintf(widget_name, "hif%d-%d Output",
1080 pin->nid, pin->ports[j].id);
1081 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1082 snd_soc_dapm_output, &pin->ports[j],
1083 widget_name, NULL, NULL, 0,
1084 hdac_hdmi_pin_output_widget_event,
1085 SND_SOC_DAPM_PRE_PMU |
1086 SND_SOC_DAPM_POST_PMD);
1087 if (ret < 0)
1088 return ret;
Jeeja KP0324e512017-02-07 19:09:55 +05301089 pin->ports[j].output_pin = widgets[i].name;
Jeeja KP754695f2017-02-06 12:09:14 +05301090 i++;
1091 }
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301092 }
1093
1094 /* DAPM widgets to represent the connection list to pin widget */
1095 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301096 for (j = 0; j < pin->num_ports; j++) {
1097 sprintf(widget_name, "Pin%d-Port%d Mux",
1098 pin->nid, pin->ports[j].id);
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001099 ret = hdac_hdmi_create_pin_port_muxs(hdev,
Jeeja KP754695f2017-02-06 12:09:14 +05301100 &pin->ports[j], &widgets[i],
1101 widget_name);
1102 if (ret < 0)
1103 return ret;
1104 i++;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301105
Jeeja KP754695f2017-02-06 12:09:14 +05301106 /* For cvt to pin_mux mapping */
1107 num_routes += hdmi->num_cvt;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301108
Jeeja KP754695f2017-02-06 12:09:14 +05301109 /* For pin_mux to pin mapping */
1110 num_routes++;
1111 }
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301112 }
1113
1114 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
1115 GFP_KERNEL);
1116 if (!route)
1117 return -ENOMEM;
1118
1119 i = 0;
1120 /* Add pin <- NULL <- mux route map */
1121 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301122 for (j = 0; j < pin->num_ports; j++) {
1123 int sink_index = i + hdmi->num_cvt;
1124 int src_index = sink_index + pin->num_ports *
1125 hdmi->num_pin;
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301126
Jeeja KP754695f2017-02-06 12:09:14 +05301127 hdac_hdmi_fill_route(&route[i],
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301128 widgets[sink_index].name, NULL,
1129 widgets[src_index].name, NULL);
Jeeja KP754695f2017-02-06 12:09:14 +05301130 i++;
1131 }
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301132 }
1133
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001134 hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301135
1136 snd_soc_dapm_new_controls(dapm, widgets,
Jeeja KP754695f2017-02-06 12:09:14 +05301137 ((2 * hdmi->num_ports) + hdmi->num_cvt));
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301138
1139 snd_soc_dapm_add_routes(dapm, route, num_routes);
1140 snd_soc_dapm_new_widgets(dapm->card);
1141
1142 return 0;
1143
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301144}
1145
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001146static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301147{
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001148 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Jeeja KP754695f2017-02-06 12:09:14 +05301149 struct hdac_hdmi_dai_port_map *dai_map;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301150 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301151 int dai_id = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301152
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301153 if (list_empty(&hdmi->cvt_list))
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301154 return -EINVAL;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301155
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301156 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1157 dai_map = &hdmi->dai_map[dai_id];
1158 dai_map->dai_id = dai_id;
1159 dai_map->cvt = cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301160
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301161 dai_id++;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301162
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301163 if (dai_id == HDA_MAX_CVTS) {
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001164 dev_warn(&hdev->dev,
Subhransu S. Prusty148569f2016-02-12 07:46:07 +05301165 "Max dais supported: %d\n", dai_id);
1166 break;
1167 }
1168 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301169
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301170 return 0;
1171}
1172
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001173static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301174{
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001175 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301176 struct hdac_hdmi_cvt *cvt;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301177 char name[NAME_SIZE];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301178
1179 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
1180 if (!cvt)
1181 return -ENOMEM;
1182
1183 cvt->nid = nid;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301184 sprintf(name, "cvt %d", cvt->nid);
1185 cvt->name = kstrdup(name, GFP_KERNEL);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301186
1187 list_add_tail(&cvt->head, &hdmi->cvt_list);
1188 hdmi->num_cvt++;
1189
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001190 return hdac_hdmi_query_cvt_params(hdev, cvt);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301191}
1192
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001193static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
Jeeja KP754695f2017-02-06 12:09:14 +05301194 struct hdac_hdmi_port *port)
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301195{
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301196 unsigned int ver, mnl;
1197
Jeeja KP754695f2017-02-06 12:09:14 +05301198 ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301199 >> DRM_ELD_VER_SHIFT;
1200
1201 if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001202 dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver);
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301203 return -EINVAL;
1204 }
1205
Jeeja KP754695f2017-02-06 12:09:14 +05301206 mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301207 DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1208
1209 if (mnl > ELD_MAX_MNL) {
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001210 dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl);
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301211 return -EINVAL;
1212 }
1213
Jeeja KP754695f2017-02-06 12:09:14 +05301214 port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301215
1216 return 0;
Subhransu S. Prustyb7756ed2016-04-14 10:07:28 +05301217}
1218
Jeeja KP754695f2017-02-06 12:09:14 +05301219static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1220 struct hdac_hdmi_port *port)
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301221{
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001222 struct hdac_device *hdev = pin->hdev;
1223 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301224 struct hdac_hdmi_pcm *pcm;
Jeeja KP754695f2017-02-06 12:09:14 +05301225 int size = 0;
Jeeja KP2acd8302017-02-06 12:09:18 +05301226 int port_id = -1;
Jeeja KP754695f2017-02-06 12:09:14 +05301227
1228 if (!hdmi)
1229 return;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301230
Jeeja KP2acd8302017-02-06 12:09:18 +05301231 /*
1232 * In case of non MST pin, get_eld info API expectes port
1233 * to be -1.
1234 */
Jeeja KP4a3478d2016-02-12 07:46:06 +05301235 mutex_lock(&hdmi->pin_mutex);
Jeeja KP754695f2017-02-06 12:09:14 +05301236 port->eld.monitor_present = false;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301237
Jeeja KP2acd8302017-02-06 12:09:18 +05301238 if (pin->mst_capable)
1239 port_id = port->id;
1240
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001241 size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
Jeeja KP754695f2017-02-06 12:09:14 +05301242 &port->eld.monitor_present,
1243 port->eld.eld_buffer,
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301244 ELD_MAX_SIZE);
1245
1246 if (size > 0) {
1247 size = min(size, ELD_MAX_SIZE);
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001248 if (hdac_hdmi_parse_eld(hdev, port) < 0)
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301249 size = -EINVAL;
1250 }
1251
1252 if (size > 0) {
Jeeja KP754695f2017-02-06 12:09:14 +05301253 port->eld.eld_valid = true;
1254 port->eld.eld_size = size;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301255 } else {
Jeeja KP754695f2017-02-06 12:09:14 +05301256 port->eld.eld_valid = false;
1257 port->eld.eld_size = 0;
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301258 }
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301259
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001260 pcm = hdac_hdmi_get_pcm(hdev, port);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301261
Jeeja KP754695f2017-02-06 12:09:14 +05301262 if (!port->eld.monitor_present || !port->eld.eld_valid) {
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301263
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001264 dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
Jeeja KP754695f2017-02-06 12:09:14 +05301265 __func__, pin->nid, port->id);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301266
1267 /*
1268 * PCMs are not registered during device probe, so don't
1269 * report jack here. It will be done in usermode mux
1270 * control select.
1271 */
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301272 if (pcm)
1273 hdac_hdmi_jack_report(pcm, port, false);
Jeeja KP4a3478d2016-02-12 07:46:06 +05301274
1275 mutex_unlock(&hdmi->pin_mutex);
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301276 return;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301277 }
1278
Jeeja KP754695f2017-02-06 12:09:14 +05301279 if (port->eld.monitor_present && port->eld.eld_valid) {
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301280 if (pcm)
1281 hdac_hdmi_jack_report(pcm, port, true);
Sandeep Tayalf6fa11a2017-01-18 21:34:41 +05301282
1283 print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
Jeeja KP754695f2017-02-06 12:09:14 +05301284 port->eld.eld_buffer, port->eld.eld_size, false);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301285
Jeeja KP754695f2017-02-06 12:09:14 +05301286 }
Jeeja KP4a3478d2016-02-12 07:46:06 +05301287 mutex_unlock(&hdmi->pin_mutex);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301288}
1289
Jeeja KP754695f2017-02-06 12:09:14 +05301290static int hdac_hdmi_add_ports(struct hdac_hdmi_priv *hdmi,
1291 struct hdac_hdmi_pin *pin)
1292{
1293 struct hdac_hdmi_port *ports;
1294 int max_ports = HDA_MAX_PORTS;
1295 int i;
1296
1297 /*
1298 * FIXME: max_port may vary for each platform, so pass this as
1299 * as driver data or query from i915 interface when this API is
1300 * implemented.
1301 */
1302
1303 ports = kcalloc(max_ports, sizeof(*ports), GFP_KERNEL);
1304 if (!ports)
1305 return -ENOMEM;
1306
1307 for (i = 0; i < max_ports; i++) {
1308 ports[i].id = i;
1309 ports[i].pin = pin;
1310 }
1311 pin->ports = ports;
1312 pin->num_ports = max_ports;
1313 return 0;
1314}
1315
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001316static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301317{
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001318 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301319 struct hdac_hdmi_pin *pin;
Jeeja KP754695f2017-02-06 12:09:14 +05301320 int ret;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301321
1322 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
1323 if (!pin)
1324 return -ENOMEM;
1325
1326 pin->nid = nid;
Jeeja KP2acd8302017-02-06 12:09:18 +05301327 pin->mst_capable = false;
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001328 pin->hdev = hdev;
Jeeja KP754695f2017-02-06 12:09:14 +05301329 ret = hdac_hdmi_add_ports(hdmi, pin);
1330 if (ret < 0)
1331 return ret;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301332
1333 list_add_tail(&pin->head, &hdmi->pin_list);
1334 hdmi->num_pin++;
Jeeja KP754695f2017-02-06 12:09:14 +05301335 hdmi->num_ports += pin->num_ports;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301336
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301337 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301338}
1339
Bard liao019033c2018-11-11 05:18:46 +08001340#define INTEL_VENDOR_NID_0x2 0x02
1341#define INTEL_VENDOR_NID_0x8 0x08
1342#define INTEL_VENDOR_NID_0xb 0x0b
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301343#define INTEL_GET_VENDOR_VERB 0xf81
1344#define INTEL_SET_VENDOR_VERB 0x781
Bard liao019033c2018-11-11 05:18:46 +08001345#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301346#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1347
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301348static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301349{
1350 unsigned int vendor_param;
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301351 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Pradeep Tewani5622bc92017-07-20 11:40:56 +05301352 unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301353
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301354 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301355 INTEL_GET_VENDOR_VERB, 0);
1356 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1357 return;
1358
1359 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301360 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301361 INTEL_SET_VENDOR_VERB, vendor_param);
1362 if (vendor_param == -1)
1363 return;
1364}
1365
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301366static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301367{
1368 unsigned int vendor_param;
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301369 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Pradeep Tewani5622bc92017-07-20 11:40:56 +05301370 unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301371
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301372 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301373 INTEL_GET_VENDOR_VERB, 0);
1374 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1375 return;
1376
1377 /* enable DP1.2 mode */
1378 vendor_param |= INTEL_EN_DP12;
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301379 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301380 INTEL_SET_VENDOR_VERB, vendor_param);
1381 if (vendor_param == -1)
1382 return;
1383
1384}
1385
Gustavo A. R. Silva61b3b3c2017-07-13 15:38:38 -05001386static const struct snd_soc_dai_ops hdmi_dai_ops = {
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301387 .startup = hdac_hdmi_pcm_open,
1388 .shutdown = hdac_hdmi_pcm_close,
1389 .hw_params = hdac_hdmi_set_hw_params,
Jeeja KPc9bfb5d2017-01-24 21:49:03 +05301390 .set_tdm_slot = hdac_hdmi_set_tdm_slot,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301391};
1392
1393/*
1394 * Each converter can support a stream independently. So a dai is created
1395 * based on the number of converter queried.
1396 */
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301397static int hdac_hdmi_create_dais(struct hdac_device *hdev,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301398 struct snd_soc_dai_driver **dais,
1399 struct hdac_hdmi_priv *hdmi, int num_dais)
1400{
1401 struct snd_soc_dai_driver *hdmi_dais;
1402 struct hdac_hdmi_cvt *cvt;
1403 char name[NAME_SIZE], dai_name[NAME_SIZE];
1404 int i = 0;
1405 u32 rates, bps;
1406 unsigned int rate_max = 384000, rate_min = 8000;
1407 u64 formats;
1408 int ret;
1409
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301410 hdmi_dais = devm_kzalloc(&hdev->dev,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301411 (sizeof(*hdmi_dais) * num_dais),
1412 GFP_KERNEL);
1413 if (!hdmi_dais)
1414 return -ENOMEM;
1415
1416 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301417 ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301418 &rates, &formats, &bps);
1419 if (ret)
1420 return ret;
1421
Yong Zhi3b857472018-08-07 12:19:16 -05001422 /* Filter out 44.1, 88.2 and 176.4Khz */
1423 rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |
1424 SNDRV_PCM_RATE_176400);
1425 if (!rates)
1426 return -EINVAL;
1427
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301428 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301429 hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301430 dai_name, GFP_KERNEL);
1431
1432 if (!hdmi_dais[i].name)
1433 return -ENOMEM;
1434
1435 snprintf(name, sizeof(name), "hifi%d", i+1);
1436 hdmi_dais[i].playback.stream_name =
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301437 devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301438 if (!hdmi_dais[i].playback.stream_name)
1439 return -ENOMEM;
1440
1441 /*
1442 * Set caps based on capability queried from the converter.
1443 * It will be constrained runtime based on ELD queried.
1444 */
1445 hdmi_dais[i].playback.formats = formats;
1446 hdmi_dais[i].playback.rates = rates;
1447 hdmi_dais[i].playback.rate_max = rate_max;
1448 hdmi_dais[i].playback.rate_min = rate_min;
1449 hdmi_dais[i].playback.channels_min = 2;
1450 hdmi_dais[i].playback.channels_max = 2;
Jeeja KP66d6bbc2017-03-24 23:10:26 +05301451 hdmi_dais[i].playback.sig_bits = bps;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301452 hdmi_dais[i].ops = &hdmi_dai_ops;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301453 i++;
1454 }
1455
1456 *dais = hdmi_dais;
Kuninori Morimoto1e02dac2017-12-20 01:48:13 +00001457 hdmi->dai_drv = hdmi_dais;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301458
1459 return 0;
1460}
1461
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301462/*
1463 * Parse all nodes and store the cvt/pin nids in array
1464 * Add one time initialization for pin and cvt widgets
1465 */
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001466static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301467 struct snd_soc_dai_driver **dais, int *num_dais)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301468{
1469 hda_nid_t nid;
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +05301470 int i, num_nodes;
Subhransu S. Prusty1c0a7de2017-11-07 16:16:26 +05301471 struct hdac_hdmi_cvt *temp_cvt, *cvt_next;
1472 struct hdac_hdmi_pin *temp_pin, *pin_next;
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001473 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301474 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301475
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301476 hdac_hdmi_skl_enable_all_pins(hdev);
1477 hdac_hdmi_skl_enable_dp12(hdev);
Subhransu S. Prusty211caab2016-02-12 07:46:03 +05301478
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301479 num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
Subhransu S. Prusty541140d2015-12-09 21:46:08 +05301480 if (!nid || num_nodes <= 0) {
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301481 dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301482 return -EINVAL;
1483 }
1484
Puneeth Prabhu45a60082017-12-06 16:34:06 +05301485 for (i = 0; i < num_nodes; i++, nid++) {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301486 unsigned int caps;
1487 unsigned int type;
1488
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301489 caps = get_wcaps(hdev, nid);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301490 type = get_wcaps_type(caps);
1491
1492 if (!(caps & AC_WCAP_DIGITAL))
1493 continue;
1494
1495 switch (type) {
1496
1497 case AC_WID_AUD_OUT:
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001498 ret = hdac_hdmi_add_cvt(hdev, nid);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301499 if (ret < 0)
Subhransu S. Prusty1c0a7de2017-11-07 16:16:26 +05301500 goto free_widgets;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301501 break;
1502
1503 case AC_WID_PIN:
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001504 ret = hdac_hdmi_add_pin(hdev, nid);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05301505 if (ret < 0)
Subhransu S. Prusty1c0a7de2017-11-07 16:16:26 +05301506 goto free_widgets;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301507 break;
1508 }
1509 }
1510
Subhransu S. Prusty1c0a7de2017-11-07 16:16:26 +05301511 if (!hdmi->num_pin || !hdmi->num_cvt) {
1512 ret = -EIO;
1513 goto free_widgets;
1514 }
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301515
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301516 ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301517 if (ret) {
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301518 dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301519 ret);
Subhransu S. Prusty1c0a7de2017-11-07 16:16:26 +05301520 goto free_widgets;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301521 }
1522
1523 *num_dais = hdmi->num_cvt;
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001524 ret = hdac_hdmi_init_dai_map(hdev);
Subhransu S. Prusty1c0a7de2017-11-07 16:16:26 +05301525 if (ret < 0)
1526 goto free_widgets;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05301527
Subhransu S. Prusty1c0a7de2017-11-07 16:16:26 +05301528 return ret;
1529
1530free_widgets:
1531 list_for_each_entry_safe(temp_cvt, cvt_next, &hdmi->cvt_list, head) {
1532 list_del(&temp_cvt->head);
1533 kfree(temp_cvt->name);
1534 kfree(temp_cvt);
1535 }
1536
1537 list_for_each_entry_safe(temp_pin, pin_next, &hdmi->pin_list, head) {
1538 for (i = 0; i < temp_pin->num_ports; i++)
1539 temp_pin->ports[i].pin = NULL;
1540 kfree(temp_pin->ports);
1541 list_del(&temp_pin->head);
1542 kfree(temp_pin);
1543 }
1544
1545 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301546}
1547
Takashi Iwaia57942b2018-07-11 16:23:16 +02001548static int hdac_hdmi_pin2port(void *aptr, int pin)
1549{
Bard liao019033c2018-11-11 05:18:46 +08001550 struct hdac_device *hdev = aptr;
1551 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1552 const int *map = hdmi->drv_data->port_map;
1553 int i;
1554
1555 if (!hdmi->drv_data->port_num)
1556 return pin - 4; /* map NID 0x05 -> port #1 */
1557
1558 /*
1559 * looking for the pin number in the mapping table and return
1560 * the index which indicate the port number
1561 */
1562 for (i = 0; i < hdmi->drv_data->port_num; i++) {
1563 if (pin == map[i])
1564 return i + 1;
1565 }
1566
1567 /* return -1 if pin number exceeds our expectation */
1568 dev_err(&hdev->dev, "Can't find the port for pin %d\n", pin);
1569 return -1;
Takashi Iwaia57942b2018-07-11 16:23:16 +02001570}
1571
Pandiyan, Dhinakaranf9318942016-09-21 13:02:48 -07001572static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301573{
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001574 struct hdac_device *hdev = aptr;
1575 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Jeeja KP754695f2017-02-06 12:09:14 +05301576 struct hdac_hdmi_pin *pin = NULL;
1577 struct hdac_hdmi_port *hport = NULL;
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001578 struct snd_soc_component *component = hdmi->component;
Jeeja KP2acd8302017-02-06 12:09:18 +05301579 int i;
Bard liao019033c2018-11-11 05:18:46 +08001580 hda_nid_t pin_nid;
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301581
Bard liao019033c2018-11-11 05:18:46 +08001582 if (!hdmi->drv_data->port_num) {
1583 /* for legacy platforms */
1584 pin_nid = port + 0x04;
1585 } else if (port < hdmi->drv_data->port_num) {
1586 /* get pin number from the pin2port mapping table */
1587 pin_nid = hdmi->drv_data->port_map[port - 1];
1588 } else {
1589 dev_err(&hdev->dev, "Can't find the pin for port %d\n", port);
1590 return;
1591 }
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301592
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001593 dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
Jeeja KP754695f2017-02-06 12:09:14 +05301594 pin_nid, pipe);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301595
1596 /*
1597 * skip notification during system suspend (but not in runtime PM);
1598 * the state will be updated at resume. Also since the ELD and
1599 * connection states are updated in anyway at the end of the resume,
1600 * we can skip it when received during PM process.
1601 */
Kuninori Morimoto45101122018-01-29 04:36:54 +00001602 if (snd_power_get_state(component->card->snd_card) !=
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301603 SNDRV_CTL_POWER_D0)
1604 return;
1605
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001606 if (atomic_read(&hdev->in_pm))
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301607 return;
1608
1609 list_for_each_entry(pin, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05301610 if (pin->nid != pin_nid)
1611 continue;
1612
1613 /* In case of non MST pin, pipe is -1 */
1614 if (pipe == -1) {
Jeeja KP2acd8302017-02-06 12:09:18 +05301615 pin->mst_capable = false;
Jeeja KP754695f2017-02-06 12:09:14 +05301616 /* if not MST, default is port[0] */
1617 hport = &pin->ports[0];
Jeeja KP2acd8302017-02-06 12:09:18 +05301618 } else {
1619 for (i = 0; i < pin->num_ports; i++) {
1620 pin->mst_capable = true;
1621 if (pin->ports[i].id == pipe) {
1622 hport = &pin->ports[i];
Jeeja KP04c8f2b2017-03-01 22:41:23 +05301623 break;
Jeeja KP2acd8302017-02-06 12:09:18 +05301624 }
1625 }
Jeeja KP754695f2017-02-06 12:09:14 +05301626 }
Jeeja KP04c8f2b2017-03-01 22:41:23 +05301627
1628 if (hport)
1629 hdac_hdmi_present_sense(pin, hport);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301630 }
Jeeja KP754695f2017-02-06 12:09:14 +05301631
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301632}
1633
Takashi Iwaiae891ab2018-07-11 15:17:22 +02001634static struct drm_audio_component_audio_ops aops = {
Takashi Iwaia57942b2018-07-11 16:23:16 +02001635 .pin2port = hdac_hdmi_pin2port,
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301636 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1637};
1638
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301639static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
1640 int device)
1641{
1642 struct snd_soc_pcm_runtime *rtd;
1643
Kuninori Morimotobcb1fd12018-09-18 01:29:35 +00001644 for_each_card_rtds(card, rtd) {
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301645 if (rtd->pcm && (rtd->pcm->device == device))
1646 return rtd->pcm;
1647 }
1648
1649 return NULL;
1650}
1651
Jeeja KP0324e512017-02-07 19:09:55 +05301652/* create jack pin kcontrols */
1653static int create_fill_jack_kcontrols(struct snd_soc_card *card,
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001654 struct hdac_device *hdev)
Jeeja KP0324e512017-02-07 19:09:55 +05301655{
1656 struct hdac_hdmi_pin *pin;
1657 struct snd_kcontrol_new *kc;
1658 char kc_name[NAME_SIZE], xname[NAME_SIZE];
1659 char *name;
1660 int i = 0, j;
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001661 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1662 struct snd_soc_component *component = hdmi->component;
Jeeja KP0324e512017-02-07 19:09:55 +05301663
Kuninori Morimoto45101122018-01-29 04:36:54 +00001664 kc = devm_kcalloc(component->dev, hdmi->num_ports,
Jeeja KP0324e512017-02-07 19:09:55 +05301665 sizeof(*kc), GFP_KERNEL);
1666
1667 if (!kc)
1668 return -ENOMEM;
1669
1670 list_for_each_entry(pin, &hdmi->pin_list, head) {
1671 for (j = 0; j < pin->num_ports; j++) {
1672 snprintf(xname, sizeof(xname), "hif%d-%d Jack",
1673 pin->nid, pin->ports[j].id);
Kuninori Morimoto45101122018-01-29 04:36:54 +00001674 name = devm_kstrdup(component->dev, xname, GFP_KERNEL);
Jeeja KP0324e512017-02-07 19:09:55 +05301675 if (!name)
1676 return -ENOMEM;
1677 snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
Kuninori Morimoto45101122018-01-29 04:36:54 +00001678 kc[i].name = devm_kstrdup(component->dev, kc_name,
Jeeja KP0324e512017-02-07 19:09:55 +05301679 GFP_KERNEL);
1680 if (!kc[i].name)
1681 return -ENOMEM;
1682
1683 kc[i].private_value = (unsigned long)name;
1684 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1685 kc[i].access = 0;
1686 kc[i].info = snd_soc_dapm_info_pin_switch;
1687 kc[i].put = snd_soc_dapm_put_pin_switch;
1688 kc[i].get = snd_soc_dapm_get_pin_switch;
1689 i++;
1690 }
1691 }
1692
1693 return snd_soc_add_card_controls(card, kc, i);
1694}
1695
Kuninori Morimoto45101122018-01-29 04:36:54 +00001696int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
Jeeja KP0324e512017-02-07 19:09:55 +05301697 struct snd_soc_dapm_context *dapm)
1698{
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001699 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1700 struct hdac_device *hdev = hdmi->hdev;
Jeeja KP0324e512017-02-07 19:09:55 +05301701 struct hdac_hdmi_pin *pin;
1702 struct snd_soc_dapm_widget *widgets;
1703 struct snd_soc_dapm_route *route;
1704 char w_name[NAME_SIZE];
1705 int i = 0, j, ret;
1706
1707 widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
1708 sizeof(*widgets), GFP_KERNEL);
1709
1710 if (!widgets)
1711 return -ENOMEM;
1712
1713 route = devm_kcalloc(dapm->dev, hdmi->num_ports,
1714 sizeof(*route), GFP_KERNEL);
1715 if (!route)
1716 return -ENOMEM;
1717
1718 /* create Jack DAPM widget */
1719 list_for_each_entry(pin, &hdmi->pin_list, head) {
1720 for (j = 0; j < pin->num_ports; j++) {
1721 snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
1722 pin->nid, pin->ports[j].id);
1723
1724 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1725 snd_soc_dapm_spk, NULL,
1726 w_name, NULL, NULL, 0, NULL, 0);
1727 if (ret < 0)
1728 return ret;
1729
1730 pin->ports[j].jack_pin = widgets[i].name;
1731 pin->ports[j].dapm = dapm;
1732
1733 /* add to route from Jack widget to output */
1734 hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
1735 NULL, pin->ports[j].output_pin, NULL);
1736
1737 i++;
1738 }
1739 }
1740
1741 /* Add Route from Jack widget to the output widget */
1742 ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
1743 if (ret < 0)
1744 return ret;
1745
1746 ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
1747 if (ret < 0)
1748 return ret;
1749
1750 ret = snd_soc_dapm_new_widgets(dapm->card);
1751 if (ret < 0)
1752 return ret;
1753
1754 /* Add Jack Pin switch Kcontrol */
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001755 ret = create_fill_jack_kcontrols(dapm->card, hdev);
Jeeja KP0324e512017-02-07 19:09:55 +05301756
1757 if (ret < 0)
1758 return ret;
1759
1760 /* default set the Jack Pin switch to OFF */
1761 list_for_each_entry(pin, &hdmi->pin_list, head) {
1762 for (j = 0; j < pin->num_ports; j++)
1763 snd_soc_dapm_disable_pin(pin->ports[j].dapm,
1764 pin->ports[j].jack_pin);
1765 }
1766
1767 return 0;
1768}
1769EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
1770
Jeeja KP62490012017-02-07 19:09:49 +05301771int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
1772 struct snd_soc_jack *jack)
Jeeja KP4a3478d2016-02-12 07:46:06 +05301773{
Kuninori Morimoto45101122018-01-29 04:36:54 +00001774 struct snd_soc_component *component = dai->component;
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001775 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1776 struct hdac_device *hdev = hdmi->hdev;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301777 struct hdac_hdmi_pcm *pcm;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301778 struct snd_pcm *snd_pcm;
1779 int err;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301780
1781 /*
1782 * this is a new PCM device, create new pcm and
1783 * add to the pcm list
1784 */
1785 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1786 if (!pcm)
1787 return -ENOMEM;
1788 pcm->pcm_id = device;
1789 pcm->cvt = hdmi->dai_map[dai->id].cvt;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301790 pcm->jack_event = 0;
Jeeja KP62490012017-02-07 19:09:49 +05301791 pcm->jack = jack;
Jeeja KPab1eea12017-01-24 21:49:05 +05301792 mutex_init(&pcm->lock);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301793 INIT_LIST_HEAD(&pcm->port_list);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301794 snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
1795 if (snd_pcm) {
1796 err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
1797 if (err < 0) {
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001798 dev_err(&hdev->dev,
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301799 "chmap control add failed with err: %d for pcm: %d\n",
1800 err, device);
1801 kfree(pcm);
1802 return err;
1803 }
1804 }
1805
Jeeja KP4a3478d2016-02-12 07:46:06 +05301806 list_add_tail(&pcm->head, &hdmi->pcm_list);
1807
Jeeja KP62490012017-02-07 19:09:49 +05301808 return 0;
Jeeja KP4a3478d2016-02-12 07:46:06 +05301809}
1810EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1811
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001812static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301813 struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1814{
1815 int i;
1816 struct hdac_hdmi_pin *pin;
1817
1818 list_for_each_entry(pin, &hdmi->pin_list, head) {
1819 if (detect_pin_caps) {
1820
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001821 if (hdac_hdmi_get_port_len(hdev, pin->nid) == 0)
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301822 pin->mst_capable = false;
1823 else
1824 pin->mst_capable = true;
1825 }
1826
1827 for (i = 0; i < pin->num_ports; i++) {
1828 if (!pin->mst_capable && i > 0)
1829 continue;
1830
1831 hdac_hdmi_present_sense(pin, &pin->ports[i]);
1832 }
1833 }
1834}
1835
Kuninori Morimoto45101122018-01-29 04:36:54 +00001836static int hdmi_codec_probe(struct snd_soc_component *component)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301837{
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001838 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1839 struct hdac_device *hdev = hdmi->hdev;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301840 struct snd_soc_dapm_context *dapm =
Kuninori Morimoto45101122018-01-29 04:36:54 +00001841 snd_soc_component_get_dapm(component);
Vinod Koulb2047e92016-05-12 08:58:55 +05301842 struct hdac_ext_link *hlink = NULL;
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301843 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301844
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001845 hdmi->component = component;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301846
Vinod Koulb2047e92016-05-12 08:58:55 +05301847 /*
1848 * hold the ref while we probe, also no need to drop the ref on
1849 * exit, we call pm_runtime_suspend() so that will do for us
1850 */
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -05001851 hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05301852 if (!hlink) {
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001853 dev_err(&hdev->dev, "hdac link not found\n");
Vinod Koul500e06b2016-05-31 19:09:55 +05301854 return -EIO;
1855 }
1856
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -05001857 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
Vinod Koulb2047e92016-05-12 08:58:55 +05301858
Subhransu S. Prusty79f4e922016-02-12 07:46:05 +05301859 ret = create_fill_widget_route_map(dapm);
1860 if (ret < 0)
1861 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301862
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001863 aops.audio_ptr = hdev;
Takashi Iwaia57942b2018-07-11 16:23:16 +02001864 ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301865 if (ret < 0) {
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001866 dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +05301867 return ret;
1868 }
1869
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001870 hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301871 /* Imp: Store the card pointer in hda_codec */
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001872 hdmi->card = dapm->card->snd_card;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301873
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301874 /*
1875 * hdac_device core already sets the state to active and calls
1876 * get_noresume. So enable runtime and set the device to suspend.
1877 */
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001878 pm_runtime_enable(&hdev->dev);
1879 pm_runtime_put(&hdev->dev);
1880 pm_runtime_suspend(&hdev->dev);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301881
1882 return 0;
1883}
1884
Kuninori Morimoto45101122018-01-29 04:36:54 +00001885static void hdmi_codec_remove(struct snd_soc_component *component)
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301886{
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001887 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1888 struct hdac_device *hdev = hdmi->hdev;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05301889
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001890 pm_runtime_disable(&hdev->dev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301891}
1892
Takashi Iwai687ae9e2019-01-08 11:37:31 +01001893#ifdef CONFIG_PM_SLEEP
1894static int hdmi_codec_resume(struct device *dev)
Jeeja KP571d5072016-02-22 07:50:33 +05301895{
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001896 struct hdac_device *hdev = dev_to_hdac_dev(dev);
1897 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Takashi Iwai687ae9e2019-01-08 11:37:31 +01001898 int ret;
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05301899
Takashi Iwai687ae9e2019-01-08 11:37:31 +01001900 ret = pm_runtime_force_resume(dev);
1901 if (ret < 0)
1902 return ret;
Jeeja KP571d5072016-02-22 07:50:33 +05301903 /*
1904 * As the ELD notify callback request is not entertained while the
1905 * device is in suspend state. Need to manually check detection of
Jeeja KPa9ce96b2017-02-07 19:09:46 +05301906 * all pins here. pin capablity change is not support, so use the
1907 * already set pin caps.
Takashi Iwai687ae9e2019-01-08 11:37:31 +01001908 *
1909 * NOTE: this is safe to call even if the codec doesn't actually resume.
1910 * The pin check involves only with DRM audio component hooks, so it
1911 * works even if the HD-audio side is still dreaming peacefully.
Jeeja KP571d5072016-02-22 07:50:33 +05301912 */
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001913 hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
Takashi Iwai687ae9e2019-01-08 11:37:31 +01001914 return 0;
Jeeja KP571d5072016-02-22 07:50:33 +05301915}
1916#else
Takashi Iwai687ae9e2019-01-08 11:37:31 +01001917#define hdmi_codec_resume NULL
Jeeja KP571d5072016-02-22 07:50:33 +05301918#endif
1919
Kuninori Morimoto45101122018-01-29 04:36:54 +00001920static const struct snd_soc_component_driver hdmi_hda_codec = {
1921 .probe = hdmi_codec_probe,
1922 .remove = hdmi_codec_remove,
1923 .use_pmdown_time = 1,
1924 .endianness = 1,
1925 .non_legacy_dai_naming = 1,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301926};
1927
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301928static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301929 unsigned char *chmap)
1930{
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301931 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301932 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301933
Jeeja KPab1eea12017-01-24 21:49:05 +05301934 memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301935}
1936
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301937static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301938 unsigned char *chmap, int prepared)
1939{
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301940 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301941 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301942 struct hdac_hdmi_port *port;
1943
Subhransu S. Prustyeb50fa12017-11-07 16:16:25 +05301944 if (!pcm)
1945 return;
1946
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301947 if (list_empty(&pcm->port_list))
1948 return;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301949
Jeeja KPab1eea12017-01-24 21:49:05 +05301950 mutex_lock(&pcm->lock);
1951 pcm->chmap_set = true;
1952 memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301953 list_for_each_entry(port, &pcm->port_list, head)
1954 if (prepared)
Rakesh Ughreja3787a392018-06-01 22:53:49 -05001955 hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
Jeeja KPab1eea12017-01-24 21:49:05 +05301956 mutex_unlock(&pcm->lock);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301957}
1958
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301959static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301960{
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301961 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301962 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301963
Subhransu S. Prustyeb50fa12017-11-07 16:16:25 +05301964 if (!pcm)
1965 return false;
1966
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301967 if (list_empty(&pcm->port_list))
1968 return false;
1969
1970 return true;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301971}
1972
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301973static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301974{
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05301975 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301976 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301977 struct hdac_hdmi_port *port;
1978
Subhransu S. Prustyeb50fa12017-11-07 16:16:25 +05301979 if (!pcm)
1980 return 0;
1981
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05301982 if (list_empty(&pcm->port_list))
1983 return 0;
1984
1985 port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1986
Jeeja KP754695f2017-02-06 12:09:14 +05301987 if (!port || !port->eld.eld_valid)
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301988 return 0;
1989
Jeeja KP754695f2017-02-06 12:09:14 +05301990 return port->eld.info.spk_alloc;
Subhransu S. Prusty28890992016-04-14 10:07:34 +05301991}
1992
Bard liao019033c2018-11-11 05:18:46 +08001993static struct hdac_hdmi_drv_data intel_icl_drv_data = {
1994 .vendor_nid = INTEL_VENDOR_NID_0x2,
1995 .port_map = icl_pin2port_map,
1996 .port_num = ARRAY_SIZE(icl_pin2port_map),
1997};
1998
Pradeep Tewani5622bc92017-07-20 11:40:56 +05301999static struct hdac_hdmi_drv_data intel_glk_drv_data = {
Bard liao019033c2018-11-11 05:18:46 +08002000 .vendor_nid = INTEL_VENDOR_NID_0xb,
Pradeep Tewani5622bc92017-07-20 11:40:56 +05302001};
2002
2003static struct hdac_hdmi_drv_data intel_drv_data = {
Bard liao019033c2018-11-11 05:18:46 +08002004 .vendor_nid = INTEL_VENDOR_NID_0x8,
Pradeep Tewani5622bc92017-07-20 11:40:56 +05302005};
2006
Rakesh Ughreja3787a392018-06-01 22:53:49 -05002007static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302008{
Rakesh Ughreja3787a392018-06-01 22:53:49 -05002009 struct hdac_hdmi_priv *hdmi_priv = NULL;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05302010 struct snd_soc_dai_driver *hdmi_dais = NULL;
Vinod Koulb2047e92016-05-12 08:58:55 +05302011 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05302012 int num_dais = 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302013 int ret = 0;
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05302014 struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
2015 const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302016
Vinod Koulb2047e92016-05-12 08:58:55 +05302017 /* hold the ref while we probe */
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -05002018 hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05302019 if (!hlink) {
Rakesh Ughreja3787a392018-06-01 22:53:49 -05002020 dev_err(&hdev->dev, "hdac link not found\n");
Vinod Koul500e06b2016-05-31 19:09:55 +05302021 return -EIO;
2022 }
2023
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -05002024 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
Vinod Koulb2047e92016-05-12 08:58:55 +05302025
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05302026 hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302027 if (hdmi_priv == NULL)
2028 return -ENOMEM;
2029
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05302030 snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
Subhransu S. Prusty28890992016-04-14 10:07:34 +05302031 hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
2032 hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
2033 hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
2034 hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
Rakesh Ughreja3787a392018-06-01 22:53:49 -05002035 hdmi_priv->hdev = hdev;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302036
Subhransu S. Prustyeb50fa12017-11-07 16:16:25 +05302037 if (!hdac_id)
2038 return -ENODEV;
2039
Pradeep Tewani5622bc92017-07-20 11:40:56 +05302040 if (hdac_id->driver_data)
2041 hdmi_priv->drv_data =
2042 (struct hdac_hdmi_drv_data *)hdac_id->driver_data;
2043 else
2044 hdmi_priv->drv_data = &intel_drv_data;
2045
Rakesh Ughreja3787a392018-06-01 22:53:49 -05002046 dev_set_drvdata(&hdev->dev, hdmi_priv);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302047
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05302048 INIT_LIST_HEAD(&hdmi_priv->pin_list);
2049 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
Jeeja KP4a3478d2016-02-12 07:46:06 +05302050 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
2051 mutex_init(&hdmi_priv->pin_mutex);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05302052
Ramesh Babuaeaccef2016-02-17 21:34:01 +05302053 /*
2054 * Turned off in the runtime_suspend during the first explicit
2055 * pm_runtime_suspend call.
2056 */
Takashi Iwai4f799e72018-12-09 10:04:25 +01002057 snd_hdac_display_power(hdev->bus, hdev->addr, true);
2058
Rakesh Ughreja3787a392018-06-01 22:53:49 -05002059 ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05302060 if (ret < 0) {
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05302061 dev_err(&hdev->dev,
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05302062 "Failed in parse and map nid with err: %d\n", ret);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302063 return ret;
Subhransu S. Prusty17a42c42016-02-12 07:46:04 +05302064 }
Puneeth Prabhu0fb02ba2017-12-06 16:34:05 +05302065 snd_hdac_refresh_widgets(hdev, true);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302066
2067 /* ASoC specific initialization */
Kuninori Morimoto45101122018-01-29 04:36:54 +00002068 ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
Vinod Koulb2047e92016-05-12 08:58:55 +05302069 hdmi_dais, num_dais);
2070
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -05002071 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
Vinod Koulb2047e92016-05-12 08:58:55 +05302072
2073 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302074}
2075
Rakesh Ughreja3787a392018-06-01 22:53:49 -05002076static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302077{
Rakesh Ughreja3787a392018-06-01 22:53:49 -05002078 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05302079 struct hdac_hdmi_pin *pin, *pin_next;
2080 struct hdac_hdmi_cvt *cvt, *cvt_next;
Jeeja KP4a3478d2016-02-12 07:46:06 +05302081 struct hdac_hdmi_pcm *pcm, *pcm_next;
Jeeja KP2fe42dd2017-03-01 22:41:24 +05302082 struct hdac_hdmi_port *port, *port_next;
Jeeja KP754695f2017-02-06 12:09:14 +05302083 int i;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05302084
Takashi Iwai77a49672018-12-09 10:06:59 +01002085 snd_hdac_display_power(hdev->bus, hdev->addr, false);
2086
Jeeja KP4a3478d2016-02-12 07:46:06 +05302087 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
2088 pcm->cvt = NULL;
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05302089 if (list_empty(&pcm->port_list))
2090 continue;
2091
Jeeja KP2fe42dd2017-03-01 22:41:24 +05302092 list_for_each_entry_safe(port, port_next,
2093 &pcm->port_list, head)
2094 list_del(&port->head);
Jeeja KPe0e5d3e2017-02-07 19:09:48 +05302095
Jeeja KP4a3478d2016-02-12 07:46:06 +05302096 list_del(&pcm->head);
2097 kfree(pcm);
2098 }
2099
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05302100 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
2101 list_del(&cvt->head);
Jeeja KP4a3478d2016-02-12 07:46:06 +05302102 kfree(cvt->name);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05302103 kfree(cvt);
2104 }
2105
2106 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
Jeeja KP754695f2017-02-06 12:09:14 +05302107 for (i = 0; i < pin->num_ports; i++)
2108 pin->ports[i].pin = NULL;
2109 kfree(pin->ports);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +05302110 list_del(&pin->head);
2111 kfree(pin);
2112 }
2113
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302114 return 0;
2115}
2116
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05302117#ifdef CONFIG_PM
2118static int hdac_hdmi_runtime_suspend(struct device *dev)
2119{
Rakesh Ughreja3787a392018-06-01 22:53:49 -05002120 struct hdac_device *hdev = dev_to_hdac_dev(dev);
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05302121 struct hdac_bus *bus = hdev->bus;
Vinod Koulb2047e92016-05-12 08:58:55 +05302122 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05302123
2124 dev_dbg(dev, "Enter: %s\n", __func__);
2125
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05302126 /* controller may not have been initialized for the first time */
2127 if (!bus)
2128 return 0;
2129
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05302130 /*
2131 * Power down afg.
2132 * codec_read is preferred over codec_write to set the power state.
2133 * This way verb is send to set the power state and response
2134 * is received. So setting power state is ensured without using loop
2135 * to read the state.
2136 */
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05302137 snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05302138 AC_PWRST_D3);
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05302139
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -05002140 hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05302141 if (!hlink) {
2142 dev_err(dev, "hdac link not found\n");
2143 return -EIO;
2144 }
2145
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -05002146 snd_hdac_ext_bus_link_put(bus, hlink);
Vinod Koulb2047e92016-05-12 08:58:55 +05302147
Takashi Iwai4f799e72018-12-09 10:04:25 +01002148 snd_hdac_display_power(bus, hdev->addr, false);
Pierre-Louis Bossart4c104732018-11-09 13:39:23 -06002149
Takashi Iwai4f799e72018-12-09 10:04:25 +01002150 return 0;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05302151}
2152
2153static int hdac_hdmi_runtime_resume(struct device *dev)
2154{
Rakesh Ughreja3787a392018-06-01 22:53:49 -05002155 struct hdac_device *hdev = dev_to_hdac_dev(dev);
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05302156 struct hdac_bus *bus = hdev->bus;
Vinod Koulb2047e92016-05-12 08:58:55 +05302157 struct hdac_ext_link *hlink = NULL;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05302158
2159 dev_dbg(dev, "Enter: %s\n", __func__);
2160
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05302161 /* controller may not have been initialized for the first time */
2162 if (!bus)
2163 return 0;
2164
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -05002165 hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
Vinod Koul500e06b2016-05-31 19:09:55 +05302166 if (!hlink) {
2167 dev_err(dev, "hdac link not found\n");
2168 return -EIO;
2169 }
2170
Rakesh Ughreja76f56fa2018-06-01 22:53:50 -05002171 snd_hdac_ext_bus_link_get(bus, hlink);
Vinod Koulb2047e92016-05-12 08:58:55 +05302172
Takashi Iwai4f799e72018-12-09 10:04:25 +01002173 snd_hdac_display_power(bus, hdev->addr, true);
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +05302174
Rakesh Ughreja3787a392018-06-01 22:53:49 -05002175 hdac_hdmi_skl_enable_all_pins(hdev);
2176 hdac_hdmi_skl_enable_dp12(hdev);
Subhransu S. Prustyab85f5b2016-02-17 21:34:02 +05302177
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05302178 /* Power up afg */
Ughreja, Rakesh Af0c5ebe2017-12-01 14:43:19 +05302179 snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
Subhransu S. Prusty1b377cc2016-04-01 13:36:26 +05302180 AC_PWRST_D0);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05302181
2182 return 0;
2183}
2184#else
2185#define hdac_hdmi_runtime_suspend NULL
2186#define hdac_hdmi_runtime_resume NULL
2187#endif
2188
2189static const struct dev_pm_ops hdac_hdmi_pm = {
2190 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
Takashi Iwai687ae9e2019-01-08 11:37:31 +01002191 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, hdmi_codec_resume)
Subhransu S. Prustye342ac02015-11-10 18:42:07 +05302192};
2193
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302194static const struct hda_device_id hdmi_list[] = {
2195 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
Jeeja KPe2304802016-03-11 10:12:55 +05302196 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
Shreyas NCcc216882016-07-11 22:02:09 +05302197 HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
Guneshwor Singh5fb6e0a2017-12-21 08:45:29 +05302198 HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
2199 &intel_glk_drv_data),
Pradeep Tewani5622bc92017-07-20 11:40:56 +05302200 HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
2201 &intel_glk_drv_data),
Bard liao019033c2018-11-11 05:18:46 +08002202 HDA_CODEC_EXT_ENTRY(0x8086280f, 0x100000, "Icelake HDMI",
2203 &intel_icl_drv_data),
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302204 {}
2205};
2206
2207MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
2208
Rakesh Ughrejae1df9312018-06-01 22:53:51 -05002209static struct hdac_driver hdmi_driver = {
2210 .driver = {
2211 .name = "HDMI HDA Codec",
2212 .pm = &hdac_hdmi_pm,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302213 },
Rakesh Ughrejae1df9312018-06-01 22:53:51 -05002214 .id_table = hdmi_list,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05302215 .probe = hdac_hdmi_dev_probe,
2216 .remove = hdac_hdmi_dev_remove,
2217};
2218
2219static int __init hdmi_init(void)
2220{
2221 return snd_hda_ext_driver_register(&hdmi_driver);
2222}
2223
2224static void __exit hdmi_exit(void)
2225{
2226 snd_hda_ext_driver_unregister(&hdmi_driver);
2227}
2228
2229module_init(hdmi_init);
2230module_exit(hdmi_exit);
2231
2232MODULE_LICENSE("GPL v2");
2233MODULE_DESCRIPTION("HDMI HD codec");
2234MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
2235MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");