1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
4 *
5 * Copyright (C) 2014-2015 Intel Corp
6 * Author: Samreen Nilofer <samreen.nilofer@intel.com>
7 * Subhransu S. Prusty <subhransu.s.prusty@intel.com>
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 *
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 */
12
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/module.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/hdmi.h>
18 #include <drm/drm_edid.h>
19 #include <drm/drm_eld.h>
20 #include <sound/pcm_params.h>
21 #include <sound/jack.h>
22 #include <sound/soc.h>
23 #include <sound/hdaudio_ext.h>
24 #include <sound/hda_i915.h>
25 #include <sound/pcm_drm_eld.h>
26 #include <sound/hda_chmap.h>
27
28 #define NAME_SIZE 32
29
30 #define AMP_OUT_MUTE 0xb080
31 #define AMP_OUT_UNMUTE 0xb000
32 #define PIN_OUT (AC_PINCTL_OUT_EN)
33
34 #define HDA_MAX_CONNECTIONS 32
35
36 #define HDA_MAX_CVTS 3
37 #define HDA_MAX_PORTS 3
38
39 #define ELD_MAX_SIZE 256
40 #define ELD_FIXED_BYTES 20
41
42 #define ELD_VER_CEA_861D 2
43 #define ELD_VER_PARTIAL 31
44 #define ELD_MAX_MNL 16
45
46 struct hdac_hdmi_cvt_params {
47 unsigned int channels_min;
48 unsigned int channels_max;
49 u32 rates;
50 u64 formats;
51 unsigned int maxbps;
52 };
53
54 struct hdac_hdmi_cvt {
55 struct list_head head;
56 hda_nid_t nid;
57 const char *name;
58 struct hdac_hdmi_cvt_params params;
59 };
60
61 /* Currently only spk_alloc, more to be added */
62 struct hdac_hdmi_parsed_eld {
63 u8 spk_alloc;
64 };
65
66 struct hdac_hdmi_eld {
67 bool monitor_present;
68 bool eld_valid;
69 int eld_size;
70 char eld_buffer[ELD_MAX_SIZE];
71 struct hdac_hdmi_parsed_eld info;
72 };
73
74 struct hdac_hdmi_pin {
75 struct list_head head;
76 hda_nid_t nid;
77 bool mst_capable;
78 struct hdac_hdmi_port *ports;
79 int num_ports;
80 struct hdac_device *hdev;
81 };
82
83 struct hdac_hdmi_port {
84 struct list_head head;
85 int id;
86 struct hdac_hdmi_pin *pin;
87 int num_mux_nids;
88 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
89 struct hdac_hdmi_eld eld;
90 const char *jack_pin;
91 bool is_connect;
92 struct snd_soc_dapm_context *dapm;
93 const char *output_pin;
94 struct work_struct dapm_work;
95 };
96
97 struct hdac_hdmi_pcm {
98 struct list_head head;
99 int pcm_id;
100 struct list_head port_list;
101 struct hdac_hdmi_cvt *cvt;
102 struct snd_soc_jack *jack;
103 int stream_tag;
104 int channels;
105 int format;
106 bool chmap_set;
107 unsigned char chmap[8]; /* ALSA API channel-map */
108 struct mutex lock;
109 int jack_event;
110 struct snd_kcontrol *eld_ctl;
111 };
112
113 struct hdac_hdmi_dai_port_map {
114 int dai_id;
115 struct hdac_hdmi_port *port;
116 struct hdac_hdmi_cvt *cvt;
117 };
118
119 struct hdac_hdmi_drv_data {
120 unsigned int vendor_nid;
121 };
122
123 struct hdac_hdmi_priv {
124 struct hdac_device *hdev;
125 struct snd_soc_component *component;
126 struct snd_card *card;
127 struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
128 struct list_head pin_list;
129 struct list_head cvt_list;
130 struct list_head pcm_list;
131 int num_pin;
132 int num_cvt;
133 int num_ports;
134 struct mutex pin_mutex;
135 struct hdac_chmap chmap;
136 struct hdac_hdmi_drv_data *drv_data;
137 struct snd_soc_dai_driver *dai_drv;
138 };
139
140 #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
141
142 static struct hdac_hdmi_pcm *
hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv * hdmi,struct hdac_hdmi_cvt * cvt)143 hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
144 struct hdac_hdmi_cvt *cvt)
145 {
146 struct hdac_hdmi_pcm *pcm;
147
148 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
149 if (pcm->cvt == cvt)
150 return pcm;
151 }
152
153 return NULL;
154 }
155
hdac_hdmi_jack_report(struct hdac_hdmi_pcm * pcm,struct hdac_hdmi_port * port,bool is_connect)156 static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
157 struct hdac_hdmi_port *port, bool is_connect)
158 {
159 struct hdac_device *hdev = port->pin->hdev;
160
161 port->is_connect = is_connect;
162 if (is_connect) {
163 /*
164 * Report Jack connect event when a device is connected
165 * for the first time where same PCM is attached to multiple
166 * ports.
167 */
168 if (pcm->jack_event == 0) {
169 dev_dbg(&hdev->dev,
170 "jack report for pcm=%d\n",
171 pcm->pcm_id);
172 snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
173 SND_JACK_AVOUT);
174 }
175 pcm->jack_event++;
176 } else {
177 /*
178 * Report Jack disconnect event when a device is disconnected
179 * is the only last connected device when same PCM is attached
180 * to multiple ports.
181 */
182 if (pcm->jack_event == 1)
183 snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
184 if (pcm->jack_event > 0)
185 pcm->jack_event--;
186 }
187 }
188
hdac_hdmi_port_dapm_update(struct hdac_hdmi_port * port)189 static void hdac_hdmi_port_dapm_update(struct hdac_hdmi_port *port)
190 {
191 if (port->is_connect)
192 snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
193 else
194 snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
195 snd_soc_dapm_sync(port->dapm);
196 }
197
hdac_hdmi_jack_dapm_work(struct work_struct * work)198 static void hdac_hdmi_jack_dapm_work(struct work_struct *work)
199 {
200 struct hdac_hdmi_port *port;
201
202 port = container_of(work, struct hdac_hdmi_port, dapm_work);
203 hdac_hdmi_port_dapm_update(port);
204 }
205
hdac_hdmi_jack_report_sync(struct hdac_hdmi_pcm * pcm,struct hdac_hdmi_port * port,bool is_connect)206 static void hdac_hdmi_jack_report_sync(struct hdac_hdmi_pcm *pcm,
207 struct hdac_hdmi_port *port, bool is_connect)
208 {
209 hdac_hdmi_jack_report(pcm, port, is_connect);
210 hdac_hdmi_port_dapm_update(port);
211 }
212
213 /* MST supported verbs */
214 /*
215 * Get the no devices that can be connected to a port on the Pin widget.
216 */
hdac_hdmi_get_port_len(struct hdac_device * hdev,hda_nid_t nid)217 static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
218 {
219 unsigned int caps;
220 unsigned int type, param;
221
222 caps = snd_hdac_get_wcaps(hdev, nid);
223 type = snd_hdac_get_wcaps_type(caps);
224
225 if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
226 return 0;
227
228 param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
229 if (param == -1)
230 return param;
231
232 return param & AC_DEV_LIST_LEN_MASK;
233 }
234
235 /*
236 * Get the port entry select on the pin. Return the port entry
237 * id selected on the pin. Return 0 means the first port entry
238 * is selected or MST is not supported.
239 */
hdac_hdmi_port_select_get(struct hdac_device * hdev,struct hdac_hdmi_port * port)240 static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
241 struct hdac_hdmi_port *port)
242 {
243 return snd_hdac_codec_read(hdev, port->pin->nid,
244 0, AC_VERB_GET_DEVICE_SEL, 0);
245 }
246
247 /*
248 * Sets the selected port entry for the configuring Pin widget verb.
249 * returns error if port set is not equal to port get otherwise success
250 */
hdac_hdmi_port_select_set(struct hdac_device * hdev,struct hdac_hdmi_port * port)251 static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
252 struct hdac_hdmi_port *port)
253 {
254 int num_ports;
255
256 if (!port->pin->mst_capable)
257 return 0;
258
259 /* AC_PAR_DEVLIST_LEN is 0 based. */
260 num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
261 if (num_ports < 0)
262 return -EIO;
263 /*
264 * Device List Length is a 0 based integer value indicating the
265 * number of sink device that a MST Pin Widget can support.
266 */
267 if (num_ports + 1 < port->id)
268 return 0;
269
270 snd_hdac_codec_write(hdev, port->pin->nid, 0,
271 AC_VERB_SET_DEVICE_SEL, port->id);
272
273 if (port->id != hdac_hdmi_port_select_get(hdev, port))
274 return -EIO;
275
276 dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
277
278 return 0;
279 }
280
get_hdmi_pcm_from_id(struct hdac_hdmi_priv * hdmi,int pcm_idx)281 static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
282 int pcm_idx)
283 {
284 struct hdac_hdmi_pcm *pcm;
285
286 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
287 if (pcm->pcm_id == pcm_idx)
288 return pcm;
289 }
290
291 return NULL;
292 }
293
sad_format(const u8 * sad)294 static unsigned int sad_format(const u8 *sad)
295 {
296 return ((sad[0] >> 0x3) & 0x1f);
297 }
298
sad_sample_bits_lpcm(const u8 * sad)299 static unsigned int sad_sample_bits_lpcm(const u8 *sad)
300 {
301 return (sad[2] & 7);
302 }
303
hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime * runtime,void * eld)304 static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
305 void *eld)
306 {
307 u64 formats = SNDRV_PCM_FMTBIT_S16;
308 int i;
309 const u8 *sad, *eld_buf = eld;
310
311 sad = drm_eld_sad(eld_buf);
312 if (!sad)
313 goto format_constraint;
314
315 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
316 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
317
318 /*
319 * the controller support 20 and 24 bits in 32 bit
320 * container so we set S32
321 */
322 if (sad_sample_bits_lpcm(sad) & 0x6)
323 formats |= SNDRV_PCM_FMTBIT_S32;
324 }
325 }
326
327 format_constraint:
328 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
329 formats);
330
331 }
332
333 static void
hdac_hdmi_set_dip_index(struct hdac_device * hdev,hda_nid_t pin_nid,int packet_index,int byte_index)334 hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
335 int packet_index, int byte_index)
336 {
337 int val;
338
339 val = (packet_index << 5) | (byte_index & 0x1f);
340 snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
341 }
342
343 struct dp_audio_infoframe {
344 u8 type; /* 0x84 */
345 u8 len; /* 0x1b */
346 u8 ver; /* 0x11 << 2 */
347
348 u8 CC02_CT47; /* match with HDMI infoframe from this on */
349 u8 SS01_SF24;
350 u8 CXT04;
351 u8 CA;
352 u8 LFEPBL01_LSV36_DM_INH7;
353 };
354
hdac_hdmi_setup_audio_infoframe(struct hdac_device * hdev,struct hdac_hdmi_pcm * pcm,struct hdac_hdmi_port * port)355 static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
356 struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
357 {
358 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
359 struct hdmi_audio_infoframe frame;
360 struct hdac_hdmi_pin *pin = port->pin;
361 struct dp_audio_infoframe dp_ai;
362 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
363 struct hdac_hdmi_cvt *cvt = pcm->cvt;
364 u8 *dip;
365 int ret;
366 int i;
367 const u8 *eld_buf;
368 u8 conn_type;
369 int channels, ca;
370
371 ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc,
372 pcm->channels, pcm->chmap_set, true, pcm->chmap);
373
374 channels = snd_hdac_get_active_channels(ca);
375 hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels);
376
377 snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
378 pcm->channels, pcm->chmap, pcm->chmap_set);
379
380 eld_buf = port->eld.eld_buffer;
381 conn_type = drm_eld_get_conn_type(eld_buf);
382
383 switch (conn_type) {
384 case DRM_ELD_CONN_TYPE_HDMI:
385 hdmi_audio_infoframe_init(&frame);
386
387 frame.channels = channels;
388 frame.channel_allocation = ca;
389
390 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
391 if (ret < 0)
392 return ret;
393
394 break;
395
396 case DRM_ELD_CONN_TYPE_DP:
397 memset(&dp_ai, 0, sizeof(dp_ai));
398 dp_ai.type = 0x84;
399 dp_ai.len = 0x1b;
400 dp_ai.ver = 0x11 << 2;
401 dp_ai.CC02_CT47 = channels - 1;
402 dp_ai.CA = ca;
403
404 dip = (u8 *)&dp_ai;
405 break;
406
407 default:
408 dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
409 return -EIO;
410 }
411
412 /* stop infoframe transmission */
413 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
414 snd_hdac_codec_write(hdev, pin->nid, 0,
415 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
416
417
418 /* Fill infoframe. Index auto-incremented */
419 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
420 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
421 for (i = 0; i < sizeof(buffer); i++)
422 snd_hdac_codec_write(hdev, pin->nid, 0,
423 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
424 } else {
425 for (i = 0; i < sizeof(dp_ai); i++)
426 snd_hdac_codec_write(hdev, pin->nid, 0,
427 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
428 }
429
430 /* Start infoframe */
431 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
432 snd_hdac_codec_write(hdev, pin->nid, 0,
433 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
434
435 return 0;
436 }
437
hdac_hdmi_set_stream(struct snd_soc_dai * dai,void * stream,int direction)438 static int hdac_hdmi_set_stream(struct snd_soc_dai *dai,
439 void *stream, int direction)
440 {
441 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
442 struct hdac_device *hdev = hdmi->hdev;
443 struct hdac_hdmi_dai_port_map *dai_map;
444 struct hdac_hdmi_pcm *pcm;
445 struct hdac_stream *hstream;
446
447 if (!stream)
448 return -EINVAL;
449
450 hstream = (struct hdac_stream *)stream;
451
452 dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, hstream->stream_tag);
453
454 dai_map = &hdmi->dai_map[dai->id];
455
456 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
457
458 if (pcm)
459 pcm->stream_tag = (hstream->stream_tag << 4);
460
461 return 0;
462 }
463
hdac_hdmi_set_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * hparams,struct snd_soc_dai * dai)464 static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
465 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
466 {
467 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
468 struct hdac_hdmi_dai_port_map *dai_map;
469 struct hdac_hdmi_pcm *pcm;
470 unsigned int bits;
471 int format;
472
473 dai_map = &hdmi->dai_map[dai->id];
474
475 bits = snd_hdac_stream_format_bits(params_format(hparams), SNDRV_PCM_SUBFORMAT_STD,
476 dai->driver->playback.sig_bits);
477 format = snd_hdac_stream_format(params_channels(hparams), bits, params_rate(hparams));
478
479 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
480 if (!pcm)
481 return -EIO;
482
483 pcm->format = format;
484 pcm->channels = params_channels(hparams);
485
486 return 0;
487 }
488
hdac_hdmi_query_port_connlist(struct hdac_device * hdev,struct hdac_hdmi_pin * pin,struct hdac_hdmi_port * port)489 static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
490 struct hdac_hdmi_pin *pin,
491 struct hdac_hdmi_port *port)
492 {
493 if (!(snd_hdac_get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
494 dev_warn(&hdev->dev,
495 "HDMI: pin %d wcaps %#x does not support connection list\n",
496 pin->nid, snd_hdac_get_wcaps(hdev, pin->nid));
497 return -EINVAL;
498 }
499
500 if (hdac_hdmi_port_select_set(hdev, port) < 0)
501 return -EIO;
502
503 port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
504 port->mux_nids, HDA_MAX_CONNECTIONS);
505 if (port->num_mux_nids == 0)
506 dev_warn(&hdev->dev,
507 "No connections found for pin:port %d:%d\n",
508 pin->nid, port->id);
509
510 dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
511 port->num_mux_nids, pin->nid, port->id);
512
513 return port->num_mux_nids;
514 }
515
516 /*
517 * Query pcm list and return port to which stream is routed.
518 *
519 * Also query connection list of the pin, to validate the cvt to port map.
520 *
521 * Same stream rendering to multiple ports simultaneously can be done
522 * possibly, but not supported for now in driver. So return the first port
523 * connected.
524 */
hdac_hdmi_get_port_from_cvt(struct hdac_device * hdev,struct hdac_hdmi_priv * hdmi,struct hdac_hdmi_cvt * cvt)525 static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
526 struct hdac_device *hdev,
527 struct hdac_hdmi_priv *hdmi,
528 struct hdac_hdmi_cvt *cvt)
529 {
530 struct hdac_hdmi_pcm *pcm;
531 struct hdac_hdmi_port *port;
532 int ret, i;
533
534 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
535 if (pcm->cvt == cvt) {
536 if (list_empty(&pcm->port_list))
537 continue;
538
539 list_for_each_entry(port, &pcm->port_list, head) {
540 mutex_lock(&pcm->lock);
541 ret = hdac_hdmi_query_port_connlist(hdev,
542 port->pin, port);
543 mutex_unlock(&pcm->lock);
544 if (ret < 0)
545 continue;
546
547 for (i = 0; i < port->num_mux_nids; i++) {
548 if (port->mux_nids[i] == cvt->nid &&
549 port->eld.monitor_present &&
550 port->eld.eld_valid)
551 return port;
552 }
553 }
554 }
555 }
556
557 return NULL;
558 }
559
560 /*
561 * Go through all converters and ensure connection is set to
562 * the correct pin as set via kcontrols.
563 */
hdac_hdmi_verify_connect_sel_all_pins(struct hdac_device * hdev)564 static void hdac_hdmi_verify_connect_sel_all_pins(struct hdac_device *hdev)
565 {
566 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
567 struct hdac_hdmi_port *port;
568 struct hdac_hdmi_cvt *cvt;
569 int cvt_idx = 0;
570
571 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
572 port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
573 if (port && port->pin) {
574 snd_hdac_codec_write(hdev, port->pin->nid, 0,
575 AC_VERB_SET_CONNECT_SEL, cvt_idx);
576 dev_dbg(&hdev->dev, "%s: %s set connect %d -> %d\n",
577 __func__, cvt->name, port->pin->nid, cvt_idx);
578 }
579 ++cvt_idx;
580 }
581 }
582
583 /*
584 * This tries to get a valid pin and set the HW constraints based on the
585 * ELD. Even if a valid pin is not found return success so that device open
586 * doesn't fail.
587 */
hdac_hdmi_pcm_open(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)588 static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
589 struct snd_soc_dai *dai)
590 {
591 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
592 struct hdac_device *hdev = hdmi->hdev;
593 struct hdac_hdmi_dai_port_map *dai_map;
594 struct hdac_hdmi_cvt *cvt;
595 struct hdac_hdmi_port *port;
596 int ret;
597
598 dai_map = &hdmi->dai_map[dai->id];
599
600 cvt = dai_map->cvt;
601 port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
602
603 /*
604 * To make PA and other userland happy.
605 * userland scans devices so returning error does not help.
606 */
607 if (!port)
608 return 0;
609 if ((!port->eld.monitor_present) ||
610 (!port->eld.eld_valid)) {
611
612 dev_warn(&hdev->dev,
613 "Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
614 port->eld.monitor_present, port->eld.eld_valid,
615 port->pin->nid, port->id);
616
617 return 0;
618 }
619
620 dai_map->port = port;
621
622 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
623 port->eld.eld_buffer);
624 if (ret < 0)
625 return ret;
626
627 return snd_pcm_hw_constraint_eld(substream->runtime,
628 port->eld.eld_buffer);
629 }
630
hdac_hdmi_pcm_close(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)631 static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
632 struct snd_soc_dai *dai)
633 {
634 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
635 struct hdac_hdmi_dai_port_map *dai_map;
636 struct hdac_hdmi_pcm *pcm;
637
638 dai_map = &hdmi->dai_map[dai->id];
639
640 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
641
642 if (pcm) {
643 mutex_lock(&pcm->lock);
644 pcm->chmap_set = false;
645 memset(pcm->chmap, 0, sizeof(pcm->chmap));
646 pcm->channels = 0;
647 mutex_unlock(&pcm->lock);
648 }
649
650 if (dai_map->port)
651 dai_map->port = NULL;
652 }
653
654 static int
hdac_hdmi_query_cvt_params(struct hdac_device * hdev,struct hdac_hdmi_cvt * cvt)655 hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
656 {
657 unsigned int chans;
658 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
659 int err;
660
661 chans = snd_hdac_get_wcaps(hdev, cvt->nid);
662 chans = snd_hdac_get_wcaps_channels(chans);
663
664 cvt->params.channels_min = 2;
665
666 cvt->params.channels_max = chans;
667 if (chans > hdmi->chmap.channels_max)
668 hdmi->chmap.channels_max = chans;
669
670 err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
671 &cvt->params.rates,
672 &cvt->params.formats,
673 NULL,
674 &cvt->params.maxbps);
675 if (err < 0)
676 dev_err(&hdev->dev,
677 "Failed to query pcm params for nid %d: %d\n",
678 cvt->nid, err);
679
680 return err;
681 }
682
hdac_hdmi_fill_widget_info(struct device * dev,struct snd_soc_dapm_widget * w,enum snd_soc_dapm_type id,void * priv,const char * wname,const char * stream,struct snd_kcontrol_new * wc,int numkc,int (* event)(struct snd_soc_dapm_widget *,struct snd_kcontrol *,int),unsigned short event_flags)683 static int hdac_hdmi_fill_widget_info(struct device *dev,
684 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
685 void *priv, const char *wname, const char *stream,
686 struct snd_kcontrol_new *wc, int numkc,
687 int (*event)(struct snd_soc_dapm_widget *,
688 struct snd_kcontrol *, int), unsigned short event_flags)
689 {
690 w->id = id;
691 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
692 if (!w->name)
693 return -ENOMEM;
694
695 w->sname = stream;
696 w->reg = SND_SOC_NOPM;
697 w->shift = 0;
698 w->kcontrol_news = wc;
699 w->num_kcontrols = numkc;
700 w->priv = priv;
701 w->event = event;
702 w->event_flags = event_flags;
703
704 return 0;
705 }
706
hdac_hdmi_fill_route(struct snd_soc_dapm_route * route,const char * sink,const char * control,const char * src,int (* handler)(struct snd_soc_dapm_widget * src,struct snd_soc_dapm_widget * sink))707 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
708 const char *sink, const char *control, const char *src,
709 int (*handler)(struct snd_soc_dapm_widget *src,
710 struct snd_soc_dapm_widget *sink))
711 {
712 route->sink = sink;
713 route->source = src;
714 route->control = control;
715 route->connected = handler;
716 }
717
hdac_hdmi_get_pcm(struct hdac_device * hdev,struct hdac_hdmi_port * port)718 static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
719 struct hdac_hdmi_port *port)
720 {
721 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
722 struct hdac_hdmi_pcm *pcm;
723 struct hdac_hdmi_port *p;
724
725 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
726 if (list_empty(&pcm->port_list))
727 continue;
728
729 list_for_each_entry(p, &pcm->port_list, head) {
730 if (p->id == port->id && port->pin == p->pin)
731 return pcm;
732 }
733 }
734
735 return NULL;
736 }
737
hdac_hdmi_set_power_state(struct hdac_device * hdev,hda_nid_t nid,unsigned int pwr_state)738 static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
739 hda_nid_t nid, unsigned int pwr_state)
740 {
741 int count;
742 unsigned int state;
743
744 if (snd_hdac_get_wcaps(hdev, nid) & AC_WCAP_POWER) {
745 if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
746 for (count = 0; count < 10; count++) {
747 snd_hdac_codec_read(hdev, nid, 0,
748 AC_VERB_SET_POWER_STATE,
749 pwr_state);
750 state = snd_hdac_sync_power_state(hdev,
751 nid, pwr_state);
752 if (!(state & AC_PWRST_ERROR))
753 break;
754 }
755 }
756 }
757 }
758
hdac_hdmi_set_amp(struct hdac_device * hdev,hda_nid_t nid,int val)759 static void hdac_hdmi_set_amp(struct hdac_device *hdev,
760 hda_nid_t nid, int val)
761 {
762 if (snd_hdac_get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
763 snd_hdac_codec_write(hdev, nid, 0,
764 AC_VERB_SET_AMP_GAIN_MUTE, val);
765 }
766
767
hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kc,int event)768 static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
769 struct snd_kcontrol *kc, int event)
770 {
771 struct hdac_hdmi_port *port = w->priv;
772 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
773 struct hdac_hdmi_pcm *pcm;
774
775 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
776 __func__, w->name, event);
777
778 pcm = hdac_hdmi_get_pcm(hdev, port);
779 if (!pcm)
780 return -EIO;
781
782 /* set the device if pin is mst_capable */
783 if (hdac_hdmi_port_select_set(hdev, port) < 0)
784 return -EIO;
785
786 switch (event) {
787 case SND_SOC_DAPM_PRE_PMU:
788 hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
789
790 /* Enable out path for this pin widget */
791 snd_hdac_codec_write(hdev, port->pin->nid, 0,
792 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
793
794 hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
795
796 return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
797
798 case SND_SOC_DAPM_POST_PMD:
799 hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
800
801 /* Disable out path for this pin widget */
802 snd_hdac_codec_write(hdev, port->pin->nid, 0,
803 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
804
805 hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
806 break;
807
808 }
809
810 return 0;
811 }
812
hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kc,int event)813 static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
814 struct snd_kcontrol *kc, int event)
815 {
816 struct hdac_hdmi_cvt *cvt = w->priv;
817 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
818 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
819 struct hdac_hdmi_pcm *pcm;
820
821 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
822 __func__, w->name, event);
823
824 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
825 if (!pcm)
826 return -EIO;
827
828 switch (event) {
829 case SND_SOC_DAPM_PRE_PMU:
830 hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
831
832 /* Enable transmission */
833 snd_hdac_codec_write(hdev, cvt->nid, 0,
834 AC_VERB_SET_DIGI_CONVERT_1, 1);
835
836 /* Category Code (CC) to zero */
837 snd_hdac_codec_write(hdev, cvt->nid, 0,
838 AC_VERB_SET_DIGI_CONVERT_2, 0);
839
840 snd_hdac_codec_write(hdev, cvt->nid, 0,
841 AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
842 snd_hdac_codec_write(hdev, cvt->nid, 0,
843 AC_VERB_SET_STREAM_FORMAT, pcm->format);
844
845 /*
846 * The connection indices are shared by all converters and
847 * may interfere with each other. Ensure correct
848 * routing for all converters at stream start.
849 */
850 hdac_hdmi_verify_connect_sel_all_pins(hdev);
851
852 break;
853
854 case SND_SOC_DAPM_POST_PMD:
855 snd_hdac_codec_write(hdev, cvt->nid, 0,
856 AC_VERB_SET_CHANNEL_STREAMID, 0);
857 snd_hdac_codec_write(hdev, cvt->nid, 0,
858 AC_VERB_SET_STREAM_FORMAT, 0);
859
860 hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
861 break;
862
863 }
864
865 return 0;
866 }
867
hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kc,int event)868 static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
869 struct snd_kcontrol *kc, int event)
870 {
871 struct hdac_hdmi_port *port = w->priv;
872 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
873 int mux_idx;
874
875 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
876 __func__, w->name, event);
877
878 if (!kc)
879 kc = w->kcontrols[0];
880
881 mux_idx = dapm_kcontrol_get_value(kc);
882
883 /* set the device if pin is mst_capable */
884 if (hdac_hdmi_port_select_set(hdev, port) < 0)
885 return -EIO;
886
887 if (mux_idx > 0) {
888 snd_hdac_codec_write(hdev, port->pin->nid, 0,
889 AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
890 }
891
892 return 0;
893 }
894
895 /*
896 * Based on user selection, map the PINs with the PCMs.
897 */
hdac_hdmi_set_pin_port_mux(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)898 static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
899 struct snd_ctl_elem_value *ucontrol)
900 {
901 int ret;
902 struct hdac_hdmi_port *p, *p_next;
903 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
904 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
905 struct snd_soc_dapm_context *dapm = w->dapm;
906 struct hdac_hdmi_port *port = w->priv;
907 struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
908 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
909 struct hdac_hdmi_pcm *pcm;
910 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
911
912 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
913 if (ret < 0)
914 return ret;
915
916 if (port == NULL)
917 return -EINVAL;
918
919 mutex_lock(&hdmi->pin_mutex);
920 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
921 if (list_empty(&pcm->port_list))
922 continue;
923
924 list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
925 if (p == port && p->id == port->id &&
926 p->pin == port->pin) {
927 hdac_hdmi_jack_report_sync(pcm, port, false);
928 list_del(&p->head);
929 }
930 }
931 }
932
933 /*
934 * Jack status is not reported during device probe as the
935 * PCMs are not registered by then. So report it here.
936 */
937 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
938 if (!strcmp(cvt_name, pcm->cvt->name)) {
939 list_add_tail(&port->head, &pcm->port_list);
940 if (port->eld.monitor_present && port->eld.eld_valid) {
941 hdac_hdmi_jack_report_sync(pcm, port, true);
942 mutex_unlock(&hdmi->pin_mutex);
943 return ret;
944 }
945 }
946 }
947 mutex_unlock(&hdmi->pin_mutex);
948
949 return ret;
950 }
951
952 /*
953 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
954 * the display driver seem to be programming the connection list for the pin
955 * widget runtime.
956 *
957 * So programming all the possible inputs for the mux, the user has to take
958 * care of selecting the right one and leaving all other inputs selected to
959 * "NONE"
960 */
hdac_hdmi_create_pin_port_muxs(struct hdac_device * hdev,struct hdac_hdmi_port * port,struct snd_soc_dapm_widget * widget,const char * widget_name)961 static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
962 struct hdac_hdmi_port *port,
963 struct snd_soc_dapm_widget *widget,
964 const char *widget_name)
965 {
966 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
967 struct hdac_hdmi_pin *pin = port->pin;
968 struct snd_kcontrol_new *kc;
969 struct hdac_hdmi_cvt *cvt;
970 struct soc_enum *se;
971 char kc_name[NAME_SIZE];
972 char mux_items[NAME_SIZE];
973 /* To hold inputs to the Pin mux */
974 char *items[HDA_MAX_CONNECTIONS];
975 int i = 0;
976 int num_items = hdmi->num_cvt + 1;
977
978 kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
979 if (!kc)
980 return -ENOMEM;
981
982 se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
983 if (!se)
984 return -ENOMEM;
985
986 snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
987 pin->nid, port->id);
988 kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
989 if (!kc->name)
990 return -ENOMEM;
991
992 kc->private_value = (long)se;
993 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
994 kc->access = 0;
995 kc->info = snd_soc_info_enum_double;
996 kc->put = hdac_hdmi_set_pin_port_mux;
997 kc->get = snd_soc_dapm_get_enum_double;
998
999 se->reg = SND_SOC_NOPM;
1000
1001 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
1002 se->items = num_items;
1003 se->mask = roundup_pow_of_two(se->items) - 1;
1004
1005 sprintf(mux_items, "NONE");
1006 items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
1007 if (!items[i])
1008 return -ENOMEM;
1009
1010 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1011 i++;
1012 sprintf(mux_items, "cvt %d", cvt->nid);
1013 items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
1014 if (!items[i])
1015 return -ENOMEM;
1016 }
1017
1018 se->texts = devm_kmemdup_array(&hdev->dev, items, num_items, sizeof(items[0]), GFP_KERNEL);
1019 if (!se->texts)
1020 return -ENOMEM;
1021
1022 return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
1023 snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
1024 hdac_hdmi_pin_mux_widget_event,
1025 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
1026 }
1027
1028 /* Add cvt <- input <- mux route map */
hdac_hdmi_add_pinmux_cvt_route(struct hdac_device * hdev,struct snd_soc_dapm_widget * widgets,struct snd_soc_dapm_route * route,int rindex)1029 static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
1030 struct snd_soc_dapm_widget *widgets,
1031 struct snd_soc_dapm_route *route, int rindex)
1032 {
1033 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1034 const struct snd_kcontrol_new *kc;
1035 struct soc_enum *se;
1036 int mux_index = hdmi->num_cvt + hdmi->num_ports;
1037 int i, j;
1038
1039 for (i = 0; i < hdmi->num_ports; i++) {
1040 kc = widgets[mux_index].kcontrol_news;
1041 se = (struct soc_enum *)kc->private_value;
1042 for (j = 0; j < hdmi->num_cvt; j++) {
1043 hdac_hdmi_fill_route(&route[rindex],
1044 widgets[mux_index].name,
1045 se->texts[j + 1],
1046 widgets[j].name, NULL);
1047
1048 rindex++;
1049 }
1050
1051 mux_index++;
1052 }
1053 }
1054
1055 /*
1056 * Widgets are added in the below sequence
1057 * Converter widgets for num converters enumerated
1058 * Pin-port widgets for num ports for Pins enumerated
1059 * Pin-port mux widgets to represent connenction list of pin widget
1060 *
1061 * For each port, one Mux and One output widget is added
1062 * Total widgets elements = num_cvt + (num_ports * 2);
1063 *
1064 * Routes are added as below:
1065 * pin-port mux -> pin (based on num_ports)
1066 * cvt -> "Input sel control" -> pin-port_mux
1067 *
1068 * Total route elements:
1069 * num_ports + (pin_muxes * num_cvt)
1070 */
create_fill_widget_route_map(struct snd_soc_dapm_context * dapm)1071 static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
1072 {
1073 struct snd_soc_dapm_widget *widgets;
1074 struct snd_soc_dapm_route *route;
1075 struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
1076 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1077 struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
1078 char widget_name[NAME_SIZE];
1079 struct hdac_hdmi_cvt *cvt;
1080 struct hdac_hdmi_pin *pin;
1081 int ret, i = 0, num_routes = 0, j;
1082
1083 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
1084 return -EINVAL;
1085
1086 widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1087 ((2 * hdmi->num_ports) + hdmi->num_cvt)),
1088 GFP_KERNEL);
1089
1090 if (!widgets)
1091 return -ENOMEM;
1092
1093 /* DAPM widgets to represent each converter widget */
1094 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1095 sprintf(widget_name, "Converter %d", cvt->nid);
1096 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1097 snd_soc_dapm_aif_in, cvt,
1098 widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1099 hdac_hdmi_cvt_output_widget_event,
1100 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
1101 if (ret < 0)
1102 return ret;
1103 i++;
1104 }
1105
1106 list_for_each_entry(pin, &hdmi->pin_list, head) {
1107 for (j = 0; j < pin->num_ports; j++) {
1108 sprintf(widget_name, "hif%d-%d Output",
1109 pin->nid, pin->ports[j].id);
1110 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1111 snd_soc_dapm_output, &pin->ports[j],
1112 widget_name, NULL, NULL, 0,
1113 hdac_hdmi_pin_output_widget_event,
1114 SND_SOC_DAPM_PRE_PMU |
1115 SND_SOC_DAPM_POST_PMD);
1116 if (ret < 0)
1117 return ret;
1118 pin->ports[j].output_pin = widgets[i].name;
1119 i++;
1120 }
1121 }
1122
1123 /* DAPM widgets to represent the connection list to pin widget */
1124 list_for_each_entry(pin, &hdmi->pin_list, head) {
1125 for (j = 0; j < pin->num_ports; j++) {
1126 sprintf(widget_name, "Pin%d-Port%d Mux",
1127 pin->nid, pin->ports[j].id);
1128 ret = hdac_hdmi_create_pin_port_muxs(hdev,
1129 &pin->ports[j], &widgets[i],
1130 widget_name);
1131 if (ret < 0)
1132 return ret;
1133 i++;
1134
1135 /* For cvt to pin_mux mapping */
1136 num_routes += hdmi->num_cvt;
1137
1138 /* For pin_mux to pin mapping */
1139 num_routes++;
1140 }
1141 }
1142
1143 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
1144 GFP_KERNEL);
1145 if (!route)
1146 return -ENOMEM;
1147
1148 i = 0;
1149 /* Add pin <- NULL <- mux route map */
1150 list_for_each_entry(pin, &hdmi->pin_list, head) {
1151 for (j = 0; j < pin->num_ports; j++) {
1152 int sink_index = i + hdmi->num_cvt;
1153 int src_index = sink_index + pin->num_ports *
1154 hdmi->num_pin;
1155
1156 hdac_hdmi_fill_route(&route[i],
1157 widgets[sink_index].name, NULL,
1158 widgets[src_index].name, NULL);
1159 i++;
1160 }
1161 }
1162
1163 hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
1164
1165 snd_soc_dapm_new_controls(dapm, widgets,
1166 ((2 * hdmi->num_ports) + hdmi->num_cvt));
1167
1168 snd_soc_dapm_add_routes(dapm, route, num_routes);
1169 snd_soc_dapm_new_widgets(dapm->card);
1170
1171 return 0;
1172
1173 }
1174
hdac_hdmi_init_dai_map(struct hdac_device * hdev)1175 static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
1176 {
1177 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1178 struct hdac_hdmi_dai_port_map *dai_map;
1179 struct hdac_hdmi_cvt *cvt;
1180 int dai_id = 0;
1181
1182 if (list_empty(&hdmi->cvt_list))
1183 return -EINVAL;
1184
1185 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1186 dai_map = &hdmi->dai_map[dai_id];
1187 dai_map->dai_id = dai_id;
1188 dai_map->cvt = cvt;
1189
1190 dai_id++;
1191
1192 if (dai_id == HDA_MAX_CVTS) {
1193 dev_warn(&hdev->dev,
1194 "Max dais supported: %d\n", dai_id);
1195 break;
1196 }
1197 }
1198
1199 return 0;
1200 }
1201
hdac_hdmi_add_cvt(struct hdac_device * hdev,hda_nid_t nid)1202 static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
1203 {
1204 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1205 struct hdac_hdmi_cvt *cvt;
1206 char name[NAME_SIZE];
1207
1208 cvt = devm_kzalloc(&hdev->dev, sizeof(*cvt), GFP_KERNEL);
1209 if (!cvt)
1210 return -ENOMEM;
1211
1212 cvt->nid = nid;
1213 sprintf(name, "cvt %d", cvt->nid);
1214 cvt->name = devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1215 if (!cvt->name)
1216 return -ENOMEM;
1217
1218 list_add_tail(&cvt->head, &hdmi->cvt_list);
1219 hdmi->num_cvt++;
1220
1221 return hdac_hdmi_query_cvt_params(hdev, cvt);
1222 }
1223
hdac_hdmi_parse_eld(struct hdac_device * hdev,struct hdac_hdmi_port * port)1224 static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
1225 struct hdac_hdmi_port *port)
1226 {
1227 unsigned int ver, mnl;
1228
1229 ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1230 >> DRM_ELD_VER_SHIFT;
1231
1232 if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1233 dev_err_ratelimited(&hdev->dev,
1234 "HDMI: Unknown ELD version %d\n", ver);
1235 return -EINVAL;
1236 }
1237
1238 mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1239 DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1240
1241 if (mnl > ELD_MAX_MNL) {
1242 dev_err_ratelimited(&hdev->dev,
1243 "HDMI: MNL Invalid %d\n", mnl);
1244 return -EINVAL;
1245 }
1246
1247 port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1248
1249 return 0;
1250 }
1251
hdac_hdmi_present_sense(struct hdac_hdmi_pin * pin,struct hdac_hdmi_port * port)1252 static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1253 struct hdac_hdmi_port *port)
1254 {
1255 struct hdac_device *hdev = pin->hdev;
1256 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1257 struct hdac_hdmi_pcm *pcm;
1258 int size = 0;
1259 int port_id = -1;
1260 bool eld_valid, eld_changed;
1261
1262 if (!hdmi)
1263 return;
1264
1265 /*
1266 * In case of non MST pin, get_eld info API expectes port
1267 * to be -1.
1268 */
1269 mutex_lock(&hdmi->pin_mutex);
1270 port->eld.monitor_present = false;
1271
1272 if (pin->mst_capable)
1273 port_id = port->id;
1274
1275 size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
1276 &port->eld.monitor_present,
1277 port->eld.eld_buffer,
1278 ELD_MAX_SIZE);
1279
1280 if (size > 0) {
1281 size = min(size, ELD_MAX_SIZE);
1282 if (hdac_hdmi_parse_eld(hdev, port) < 0)
1283 size = -EINVAL;
1284 }
1285
1286 eld_valid = port->eld.eld_valid;
1287
1288 if (size > 0) {
1289 port->eld.eld_valid = true;
1290 port->eld.eld_size = size;
1291 } else {
1292 port->eld.eld_valid = false;
1293 port->eld.eld_size = 0;
1294 }
1295
1296 eld_changed = (eld_valid != port->eld.eld_valid);
1297
1298 pcm = hdac_hdmi_get_pcm(hdev, port);
1299
1300 if (!port->eld.monitor_present || !port->eld.eld_valid) {
1301
1302 dev_dbg(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
1303 __func__, pin->nid, port->id);
1304
1305 /*
1306 * PCMs are not registered during device probe, so don't
1307 * report jack here. It will be done in usermode mux
1308 * control select.
1309 */
1310 if (pcm) {
1311 hdac_hdmi_jack_report(pcm, port, false);
1312 schedule_work(&port->dapm_work);
1313 }
1314
1315 mutex_unlock(&hdmi->pin_mutex);
1316 return;
1317 }
1318
1319 if (port->eld.monitor_present && port->eld.eld_valid) {
1320 if (pcm) {
1321 hdac_hdmi_jack_report(pcm, port, true);
1322 schedule_work(&port->dapm_work);
1323 }
1324
1325 print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1326 port->eld.eld_buffer, port->eld.eld_size, false);
1327
1328 }
1329 mutex_unlock(&hdmi->pin_mutex);
1330
1331 if (eld_changed && pcm)
1332 snd_ctl_notify(hdmi->card,
1333 SNDRV_CTL_EVENT_MASK_VALUE |
1334 SNDRV_CTL_EVENT_MASK_INFO,
1335 &pcm->eld_ctl->id);
1336 }
1337
hdac_hdmi_add_ports(struct hdac_device * hdev,struct hdac_hdmi_pin * pin)1338 static int hdac_hdmi_add_ports(struct hdac_device *hdev,
1339 struct hdac_hdmi_pin *pin)
1340 {
1341 struct hdac_hdmi_port *ports;
1342 int max_ports = HDA_MAX_PORTS;
1343 int i;
1344
1345 /*
1346 * FIXME: max_port may vary for each platform, so pass this as
1347 * as driver data or query from i915 interface when this API is
1348 * implemented.
1349 */
1350
1351 ports = devm_kcalloc(&hdev->dev, max_ports, sizeof(*ports), GFP_KERNEL);
1352 if (!ports)
1353 return -ENOMEM;
1354
1355 for (i = 0; i < max_ports; i++) {
1356 ports[i].id = i;
1357 ports[i].pin = pin;
1358 INIT_WORK(&ports[i].dapm_work, hdac_hdmi_jack_dapm_work);
1359 }
1360 pin->ports = ports;
1361 pin->num_ports = max_ports;
1362 return 0;
1363 }
1364
hdac_hdmi_add_pin(struct hdac_device * hdev,hda_nid_t nid)1365 static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
1366 {
1367 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1368 struct hdac_hdmi_pin *pin;
1369 int ret;
1370
1371 pin = devm_kzalloc(&hdev->dev, sizeof(*pin), GFP_KERNEL);
1372 if (!pin)
1373 return -ENOMEM;
1374
1375 pin->nid = nid;
1376 pin->mst_capable = false;
1377 pin->hdev = hdev;
1378 ret = hdac_hdmi_add_ports(hdev, pin);
1379 if (ret < 0)
1380 return ret;
1381
1382 list_add_tail(&pin->head, &hdmi->pin_list);
1383 hdmi->num_pin++;
1384 hdmi->num_ports += pin->num_ports;
1385
1386 return 0;
1387 }
1388
1389 #define INTEL_VENDOR_NID 0x08
1390 #define INTEL_GLK_VENDOR_NID 0x0b
1391 #define INTEL_GET_VENDOR_VERB 0xf81
1392 #define INTEL_SET_VENDOR_VERB 0x781
1393 #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1394 #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1395
hdac_hdmi_skl_enable_all_pins(struct hdac_device * hdev)1396 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
1397 {
1398 unsigned int vendor_param;
1399 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1400 unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1401
1402 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1403 INTEL_GET_VENDOR_VERB, 0);
1404 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1405 return;
1406
1407 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1408 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1409 INTEL_SET_VENDOR_VERB, vendor_param);
1410 if (vendor_param == -1)
1411 return;
1412 }
1413
hdac_hdmi_skl_enable_dp12(struct hdac_device * hdev)1414 static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1415 {
1416 unsigned int vendor_param;
1417 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1418 unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1419
1420 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1421 INTEL_GET_VENDOR_VERB, 0);
1422 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1423 return;
1424
1425 /* enable DP1.2 mode */
1426 vendor_param |= INTEL_EN_DP12;
1427 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1428 INTEL_SET_VENDOR_VERB, vendor_param);
1429 if (vendor_param == -1)
1430 return;
1431
1432 }
1433
1434 static const struct snd_soc_dai_ops hdmi_dai_ops = {
1435 .startup = hdac_hdmi_pcm_open,
1436 .shutdown = hdac_hdmi_pcm_close,
1437 .hw_params = hdac_hdmi_set_hw_params,
1438 .set_stream = hdac_hdmi_set_stream,
1439 };
1440
1441 /*
1442 * Each converter can support a stream independently. So a dai is created
1443 * based on the number of converter queried.
1444 */
hdac_hdmi_create_dais(struct hdac_device * hdev,struct snd_soc_dai_driver ** dais,struct hdac_hdmi_priv * hdmi,int num_dais)1445 static int hdac_hdmi_create_dais(struct hdac_device *hdev,
1446 struct snd_soc_dai_driver **dais,
1447 struct hdac_hdmi_priv *hdmi, int num_dais)
1448 {
1449 struct snd_soc_dai_driver *hdmi_dais;
1450 struct hdac_hdmi_cvt *cvt;
1451 char name[NAME_SIZE], dai_name[NAME_SIZE];
1452 int i = 0;
1453 u32 rates, bps;
1454 unsigned int rate_max = 384000, rate_min = 8000;
1455 u64 formats;
1456 int ret;
1457
1458 hdmi_dais = devm_kzalloc(&hdev->dev,
1459 (sizeof(*hdmi_dais) * num_dais),
1460 GFP_KERNEL);
1461 if (!hdmi_dais)
1462 return -ENOMEM;
1463
1464 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1465 ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
1466 &rates, &formats, NULL, &bps);
1467 if (ret)
1468 return ret;
1469
1470 /* Filter out 44.1, 88.2 and 176.4Khz */
1471 rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |
1472 SNDRV_PCM_RATE_176400);
1473 if (!rates)
1474 return -EINVAL;
1475
1476 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1477 hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
1478 dai_name, GFP_KERNEL);
1479
1480 if (!hdmi_dais[i].name)
1481 return -ENOMEM;
1482
1483 snprintf(name, sizeof(name), "hifi%d", i+1);
1484 hdmi_dais[i].playback.stream_name =
1485 devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1486 if (!hdmi_dais[i].playback.stream_name)
1487 return -ENOMEM;
1488
1489 /*
1490 * Set caps based on capability queried from the converter.
1491 * It will be constrained runtime based on ELD queried.
1492 */
1493 hdmi_dais[i].playback.formats = formats;
1494 hdmi_dais[i].playback.rates = rates;
1495 hdmi_dais[i].playback.rate_max = rate_max;
1496 hdmi_dais[i].playback.rate_min = rate_min;
1497 hdmi_dais[i].playback.channels_min = 2;
1498 hdmi_dais[i].playback.channels_max = 2;
1499 hdmi_dais[i].playback.sig_bits = bps;
1500 hdmi_dais[i].ops = &hdmi_dai_ops;
1501 i++;
1502 }
1503
1504 *dais = hdmi_dais;
1505 hdmi->dai_drv = hdmi_dais;
1506
1507 return 0;
1508 }
1509
1510 /*
1511 * Parse all nodes and store the cvt/pin nids in array
1512 * Add one time initialization for pin and cvt widgets
1513 */
hdac_hdmi_parse_and_map_nid(struct hdac_device * hdev,struct snd_soc_dai_driver ** dais,int * num_dais)1514 static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
1515 struct snd_soc_dai_driver **dais, int *num_dais)
1516 {
1517 hda_nid_t nid;
1518 int i, num_nodes;
1519 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1520 int ret;
1521
1522 hdac_hdmi_skl_enable_all_pins(hdev);
1523 hdac_hdmi_skl_enable_dp12(hdev);
1524
1525 num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1526 if (!nid || num_nodes <= 0) {
1527 dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
1528 return -EINVAL;
1529 }
1530
1531 for (i = 0; i < num_nodes; i++, nid++) {
1532 unsigned int caps;
1533 unsigned int type;
1534
1535 caps = snd_hdac_get_wcaps(hdev, nid);
1536 type = snd_hdac_get_wcaps_type(caps);
1537
1538 if (!(caps & AC_WCAP_DIGITAL))
1539 continue;
1540
1541 switch (type) {
1542
1543 case AC_WID_AUD_OUT:
1544 ret = hdac_hdmi_add_cvt(hdev, nid);
1545 if (ret < 0)
1546 return ret;
1547 break;
1548
1549 case AC_WID_PIN:
1550 ret = hdac_hdmi_add_pin(hdev, nid);
1551 if (ret < 0)
1552 return ret;
1553 break;
1554 }
1555 }
1556
1557 if (!hdmi->num_pin || !hdmi->num_cvt) {
1558 ret = -EIO;
1559 dev_err(&hdev->dev, "Bad pin/cvt setup in %s\n", __func__);
1560 return ret;
1561 }
1562
1563 ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
1564 if (ret) {
1565 dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
1566 ret);
1567 return ret;
1568 }
1569
1570 *num_dais = hdmi->num_cvt;
1571 ret = hdac_hdmi_init_dai_map(hdev);
1572 if (ret < 0)
1573 dev_err(&hdev->dev, "Failed to init DAI map with err: %d\n",
1574 ret);
1575 return ret;
1576 }
1577
hdac_hdmi_pin2port(void * aptr,int pin)1578 static int hdac_hdmi_pin2port(void *aptr, int pin)
1579 {
1580 return pin - 4; /* map NID 0x05 -> port #1 */
1581 }
1582
hdac_hdmi_eld_notify_cb(void * aptr,int port,int pipe)1583 static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1584 {
1585 struct hdac_device *hdev = aptr;
1586 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1587 struct hdac_hdmi_pin *pin;
1588 struct hdac_hdmi_port *hport = NULL;
1589 struct snd_soc_component *component = hdmi->component;
1590 int i;
1591
1592 /* Don't know how this mapping is derived */
1593 hda_nid_t pin_nid = port + 0x04;
1594
1595 dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
1596 pin_nid, pipe);
1597
1598 /*
1599 * skip notification during system suspend (but not in runtime PM);
1600 * the state will be updated at resume. Also since the ELD and
1601 * connection states are updated in anyway at the end of the resume,
1602 * we can skip it when received during PM process.
1603 */
1604 if (snd_power_get_state(component->card->snd_card) !=
1605 SNDRV_CTL_POWER_D0)
1606 return;
1607
1608 if (atomic_read(&hdev->in_pm))
1609 return;
1610
1611 list_for_each_entry(pin, &hdmi->pin_list, head) {
1612 if (pin->nid != pin_nid)
1613 continue;
1614
1615 /* In case of non MST pin, pipe is -1 */
1616 if (pipe == -1) {
1617 pin->mst_capable = false;
1618 /* if not MST, default is port[0] */
1619 hport = &pin->ports[0];
1620 } else {
1621 for (i = 0; i < pin->num_ports; i++) {
1622 pin->mst_capable = true;
1623 if (pin->ports[i].id == pipe) {
1624 hport = &pin->ports[i];
1625 break;
1626 }
1627 }
1628 }
1629
1630 if (hport)
1631 hdac_hdmi_present_sense(pin, hport);
1632 }
1633
1634 }
1635
1636 static struct drm_audio_component_audio_ops aops = {
1637 .pin2port = hdac_hdmi_pin2port,
1638 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1639 };
1640
hdac_hdmi_present_sense_all_pins(struct hdac_device * hdev,struct hdac_hdmi_priv * hdmi,bool detect_pin_caps)1641 static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
1642 struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1643 {
1644 int i;
1645 struct hdac_hdmi_pin *pin;
1646
1647 list_for_each_entry(pin, &hdmi->pin_list, head) {
1648 if (detect_pin_caps) {
1649
1650 if (hdac_hdmi_get_port_len(hdev, pin->nid) == 0)
1651 pin->mst_capable = false;
1652 else
1653 pin->mst_capable = true;
1654 }
1655
1656 for (i = 0; i < pin->num_ports; i++) {
1657 if (!pin->mst_capable && i > 0)
1658 continue;
1659
1660 hdac_hdmi_present_sense(pin, &pin->ports[i]);
1661 }
1662 }
1663 }
1664
hdmi_codec_probe(struct snd_soc_component * component)1665 static int hdmi_codec_probe(struct snd_soc_component *component)
1666 {
1667 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1668 struct hdac_device *hdev = hdmi->hdev;
1669 struct snd_soc_dapm_context *dapm =
1670 snd_soc_component_get_dapm(component);
1671 struct hdac_ext_link *hlink;
1672 int ret;
1673
1674 hdmi->component = component;
1675
1676 /*
1677 * hold the ref while we probe, also no need to drop the ref on
1678 * exit, we call pm_runtime_suspend() so that will do for us
1679 */
1680 hlink = snd_hdac_ext_bus_get_hlink_by_name(hdev->bus, dev_name(&hdev->dev));
1681 if (!hlink) {
1682 dev_err(&hdev->dev, "hdac link not found\n");
1683 return -EIO;
1684 }
1685
1686 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1687
1688 ret = create_fill_widget_route_map(dapm);
1689 if (ret < 0)
1690 return ret;
1691
1692 aops.audio_ptr = hdev;
1693 ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
1694 if (ret < 0) {
1695 dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
1696 return ret;
1697 }
1698
1699 hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
1700 /* Imp: Store the card pointer in hda_codec */
1701 hdmi->card = dapm->card->snd_card;
1702
1703 /*
1704 * Setup a device_link between card device and HDMI codec device.
1705 * The card device is the consumer and the HDMI codec device is
1706 * the supplier. With this setting, we can make sure that the audio
1707 * domain in display power will be always turned on before operating
1708 * on the HDMI audio codec registers.
1709 * Let's use the flag DL_FLAG_AUTOREMOVE_CONSUMER. This can make
1710 * sure the device link is freed when the machine driver is removed.
1711 */
1712 device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE |
1713 DL_FLAG_AUTOREMOVE_CONSUMER);
1714 /*
1715 * hdac_device core already sets the state to active and calls
1716 * get_noresume. So enable runtime and set the device to suspend.
1717 */
1718 pm_runtime_enable(&hdev->dev);
1719 pm_runtime_put(&hdev->dev);
1720 pm_runtime_suspend(&hdev->dev);
1721
1722 return 0;
1723 }
1724
hdmi_codec_remove(struct snd_soc_component * component)1725 static void hdmi_codec_remove(struct snd_soc_component *component)
1726 {
1727 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1728 struct hdac_device *hdev = hdmi->hdev;
1729 int ret;
1730
1731 ret = snd_hdac_acomp_register_notifier(hdev->bus, NULL);
1732 if (ret < 0)
1733 dev_err(&hdev->dev, "notifier unregister failed: err: %d\n",
1734 ret);
1735
1736 pm_runtime_disable(&hdev->dev);
1737 }
1738
hdmi_codec_resume(struct device * dev)1739 static int hdmi_codec_resume(struct device *dev)
1740 {
1741 struct hdac_device *hdev = dev_to_hdac_dev(dev);
1742 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1743 int ret;
1744
1745 ret = pm_runtime_force_resume(dev);
1746 if (ret < 0)
1747 return ret;
1748 /*
1749 * As the ELD notify callback request is not entertained while the
1750 * device is in suspend state. Need to manually check detection of
1751 * all pins here. pin capablity change is not support, so use the
1752 * already set pin caps.
1753 *
1754 * NOTE: this is safe to call even if the codec doesn't actually resume.
1755 * The pin check involves only with DRM audio component hooks, so it
1756 * works even if the HD-audio side is still dreaming peacefully.
1757 */
1758 hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
1759 return 0;
1760 }
1761
1762 static const struct snd_soc_component_driver hdmi_hda_codec = {
1763 .probe = hdmi_codec_probe,
1764 .remove = hdmi_codec_remove,
1765 .use_pmdown_time = 1,
1766 .endianness = 1,
1767 };
1768
hdac_hdmi_get_chmap(struct hdac_device * hdev,int pcm_idx,unsigned char * chmap)1769 static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
1770 unsigned char *chmap)
1771 {
1772 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1773 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1774
1775 memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
1776 }
1777
hdac_hdmi_set_chmap(struct hdac_device * hdev,int pcm_idx,unsigned char * chmap,int prepared)1778 static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
1779 unsigned char *chmap, int prepared)
1780 {
1781 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1782 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1783 struct hdac_hdmi_port *port;
1784
1785 if (!pcm)
1786 return;
1787
1788 if (list_empty(&pcm->port_list))
1789 return;
1790
1791 mutex_lock(&pcm->lock);
1792 pcm->chmap_set = true;
1793 memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
1794 list_for_each_entry(port, &pcm->port_list, head)
1795 if (prepared)
1796 hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
1797 mutex_unlock(&pcm->lock);
1798 }
1799
is_hdac_hdmi_pcm_attached(struct hdac_device * hdev,int pcm_idx)1800 static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
1801 {
1802 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1803 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1804
1805 if (!pcm)
1806 return false;
1807
1808 if (list_empty(&pcm->port_list))
1809 return false;
1810
1811 return true;
1812 }
1813
hdac_hdmi_get_spk_alloc(struct hdac_device * hdev,int pcm_idx)1814 static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
1815 {
1816 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1817 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
1818 struct hdac_hdmi_port *port;
1819
1820 if (!pcm)
1821 return 0;
1822
1823 if (list_empty(&pcm->port_list))
1824 return 0;
1825
1826 port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
1827
1828 if (!port || !port->eld.eld_valid)
1829 return 0;
1830
1831 return port->eld.info.spk_alloc;
1832 }
1833
1834 static struct hdac_hdmi_drv_data intel_glk_drv_data = {
1835 .vendor_nid = INTEL_GLK_VENDOR_NID,
1836 };
1837
1838 static struct hdac_hdmi_drv_data intel_drv_data = {
1839 .vendor_nid = INTEL_VENDOR_NID,
1840 };
1841
hdac_hdmi_dev_probe(struct hdac_device * hdev)1842 static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
1843 {
1844 struct hdac_hdmi_priv *hdmi_priv;
1845 struct snd_soc_dai_driver *hdmi_dais = NULL;
1846 struct hdac_ext_link *hlink;
1847 int num_dais = 0;
1848 int ret;
1849 struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
1850 const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
1851
1852 /* hold the ref while we probe */
1853 hlink = snd_hdac_ext_bus_get_hlink_by_name(hdev->bus, dev_name(&hdev->dev));
1854 if (!hlink) {
1855 dev_err(&hdev->dev, "hdac link not found\n");
1856 return -EIO;
1857 }
1858
1859 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1860
1861 hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
1862 if (hdmi_priv == NULL)
1863 return -ENOMEM;
1864
1865 snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
1866 hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
1867 hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
1868 hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
1869 hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
1870 hdmi_priv->hdev = hdev;
1871
1872 if (!hdac_id)
1873 return -ENODEV;
1874
1875 if (hdac_id->driver_data)
1876 hdmi_priv->drv_data =
1877 (struct hdac_hdmi_drv_data *)hdac_id->driver_data;
1878 else
1879 hdmi_priv->drv_data = &intel_drv_data;
1880
1881 dev_set_drvdata(&hdev->dev, hdmi_priv);
1882
1883 INIT_LIST_HEAD(&hdmi_priv->pin_list);
1884 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
1885 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1886 mutex_init(&hdmi_priv->pin_mutex);
1887
1888 /*
1889 * Turned off in the runtime_suspend during the first explicit
1890 * pm_runtime_suspend call.
1891 */
1892 snd_hdac_display_power(hdev->bus, hdev->addr, true);
1893
1894 ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
1895 if (ret < 0) {
1896 dev_err(&hdev->dev,
1897 "Failed in parse and map nid with err: %d\n", ret);
1898 return ret;
1899 }
1900 snd_hdac_refresh_widgets(hdev);
1901
1902 /* ASoC specific initialization */
1903 ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
1904 hdmi_dais, num_dais);
1905
1906 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
1907
1908 return ret;
1909 }
1910
clear_dapm_works(struct hdac_device * hdev)1911 static void clear_dapm_works(struct hdac_device *hdev)
1912 {
1913 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1914 struct hdac_hdmi_pin *pin;
1915 int i;
1916
1917 list_for_each_entry(pin, &hdmi->pin_list, head)
1918 for (i = 0; i < pin->num_ports; i++)
1919 cancel_work_sync(&pin->ports[i].dapm_work);
1920 }
1921
hdac_hdmi_dev_remove(struct hdac_device * hdev)1922 static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
1923 {
1924 clear_dapm_works(hdev);
1925 snd_hdac_display_power(hdev->bus, hdev->addr, false);
1926
1927 return 0;
1928 }
1929
hdac_hdmi_runtime_suspend(struct device * dev)1930 static int hdac_hdmi_runtime_suspend(struct device *dev)
1931 {
1932 struct hdac_device *hdev = dev_to_hdac_dev(dev);
1933 struct hdac_bus *bus = hdev->bus;
1934 struct hdac_ext_link *hlink;
1935
1936 dev_dbg(dev, "Enter: %s\n", __func__);
1937
1938 /* controller may not have been initialized for the first time */
1939 if (!bus)
1940 return 0;
1941
1942 /*
1943 * Power down afg.
1944 * codec_read is preferred over codec_write to set the power state.
1945 * This way verb is send to set the power state and response
1946 * is received. So setting power state is ensured without using loop
1947 * to read the state.
1948 */
1949 snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
1950 AC_PWRST_D3);
1951
1952 hlink = snd_hdac_ext_bus_get_hlink_by_name(bus, dev_name(dev));
1953 if (!hlink) {
1954 dev_err(dev, "hdac link not found\n");
1955 return -EIO;
1956 }
1957
1958 snd_hdac_codec_link_down(hdev);
1959 snd_hdac_ext_bus_link_put(bus, hlink);
1960
1961 snd_hdac_display_power(bus, hdev->addr, false);
1962
1963 return 0;
1964 }
1965
hdac_hdmi_runtime_resume(struct device * dev)1966 static int hdac_hdmi_runtime_resume(struct device *dev)
1967 {
1968 struct hdac_device *hdev = dev_to_hdac_dev(dev);
1969 struct hdac_bus *bus = hdev->bus;
1970 struct hdac_ext_link *hlink;
1971
1972 dev_dbg(dev, "Enter: %s\n", __func__);
1973
1974 /* controller may not have been initialized for the first time */
1975 if (!bus)
1976 return 0;
1977
1978 hlink = snd_hdac_ext_bus_get_hlink_by_name(bus, dev_name(dev));
1979 if (!hlink) {
1980 dev_err(dev, "hdac link not found\n");
1981 return -EIO;
1982 }
1983
1984 snd_hdac_ext_bus_link_get(bus, hlink);
1985 snd_hdac_codec_link_up(hdev);
1986
1987 snd_hdac_display_power(bus, hdev->addr, true);
1988
1989 hdac_hdmi_skl_enable_all_pins(hdev);
1990 hdac_hdmi_skl_enable_dp12(hdev);
1991
1992 /* Power up afg */
1993 snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
1994 AC_PWRST_D0);
1995
1996 return 0;
1997 }
1998
1999 static const struct dev_pm_ops hdac_hdmi_pm = {
2000 RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
2001 SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, hdmi_codec_resume)
2002 };
2003
2004 static const struct hda_device_id hdmi_list[] = {
2005 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2006 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2007 HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
2008 HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
2009 &intel_glk_drv_data),
2010 HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
2011 &intel_glk_drv_data),
2012 {}
2013 };
2014
2015 MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
2016
2017 static struct hdac_driver hdmi_driver = {
2018 .driver = {
2019 .name = "HDMI HDA Codec",
2020 .pm = pm_ptr(&hdac_hdmi_pm),
2021 },
2022 .id_table = hdmi_list,
2023 .probe = hdac_hdmi_dev_probe,
2024 .remove = hdac_hdmi_dev_remove,
2025 };
2026
hdmi_init(void)2027 static int __init hdmi_init(void)
2028 {
2029 return snd_hda_ext_driver_register(&hdmi_driver);
2030 }
2031
hdmi_exit(void)2032 static void __exit hdmi_exit(void)
2033 {
2034 snd_hda_ext_driver_unregister(&hdmi_driver);
2035 }
2036
2037 module_init(hdmi_init);
2038 module_exit(hdmi_exit);
2039
2040 MODULE_LICENSE("GPL v2");
2041 MODULE_DESCRIPTION("HDMI HD codec");
2042 MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
2043 MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");
2044