aboutsummaryrefslogtreecommitdiffstats
path: root/sound/soc/codecs/hdac_hdmi.c
diff options
context:
space:
mode:
Diffstat (limited to 'sound/soc/codecs/hdac_hdmi.c')
-rw-r--r--sound/soc/codecs/hdac_hdmi.c1219
1 files changed, 1096 insertions, 123 deletions
diff --git a/sound/soc/codecs/hdac_hdmi.c b/sound/soc/codecs/hdac_hdmi.c
index 5a1ec0f7a1a6..26f9459cb3bc 100644
--- a/sound/soc/codecs/hdac_hdmi.c
+++ b/sound/soc/codecs/hdac_hdmi.c
@@ -22,11 +22,17 @@
22#include <linux/module.h> 22#include <linux/module.h>
23#include <linux/pm_runtime.h> 23#include <linux/pm_runtime.h>
24#include <linux/hdmi.h> 24#include <linux/hdmi.h>
25#include <drm/drm_edid.h>
25#include <sound/pcm_params.h> 26#include <sound/pcm_params.h>
27#include <sound/jack.h>
26#include <sound/soc.h> 28#include <sound/soc.h>
27#include <sound/hdaudio_ext.h> 29#include <sound/hdaudio_ext.h>
28#include <sound/hda_i915.h> 30#include <sound/hda_i915.h>
31#include <sound/pcm_drm_eld.h>
29#include "../../hda/local.h" 32#include "../../hda/local.h"
33#include "hdac_hdmi.h"
34
35#define NAME_SIZE 32
30 36
31#define AMP_OUT_MUTE 0xb080 37#define AMP_OUT_MUTE 0xb080
32#define AMP_OUT_UNMUTE 0xb000 38#define AMP_OUT_UNMUTE 0xb000
@@ -34,6 +40,11 @@
34 40
35#define HDA_MAX_CONNECTIONS 32 41#define HDA_MAX_CONNECTIONS 32
36 42
43#define HDA_MAX_CVTS 3
44
45#define ELD_MAX_SIZE 256
46#define ELD_FIXED_BYTES 20
47
37struct hdac_hdmi_cvt_params { 48struct hdac_hdmi_cvt_params {
38 unsigned int channels_min; 49 unsigned int channels_min;
39 unsigned int channels_max; 50 unsigned int channels_max;
@@ -45,14 +56,34 @@ struct hdac_hdmi_cvt_params {
45struct hdac_hdmi_cvt { 56struct hdac_hdmi_cvt {
46 struct list_head head; 57 struct list_head head;
47 hda_nid_t nid; 58 hda_nid_t nid;
59 const char *name;
48 struct hdac_hdmi_cvt_params params; 60 struct hdac_hdmi_cvt_params params;
49}; 61};
50 62
63struct hdac_hdmi_eld {
64 bool monitor_present;
65 bool eld_valid;
66 int eld_size;
67 char eld_buffer[ELD_MAX_SIZE];
68};
69
51struct hdac_hdmi_pin { 70struct hdac_hdmi_pin {
52 struct list_head head; 71 struct list_head head;
53 hda_nid_t nid; 72 hda_nid_t nid;
54 int num_mux_nids; 73 int num_mux_nids;
55 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS]; 74 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
75 struct hdac_hdmi_eld eld;
76 struct hdac_ext_device *edev;
77 int repoll_count;
78 struct delayed_work work;
79};
80
81struct hdac_hdmi_pcm {
82 struct list_head head;
83 int pcm_id;
84 struct hdac_hdmi_pin *pin;
85 struct hdac_hdmi_cvt *cvt;
86 struct snd_jack *jack;
56}; 87};
57 88
58struct hdac_hdmi_dai_pin_map { 89struct hdac_hdmi_dai_pin_map {
@@ -62,11 +93,13 @@ struct hdac_hdmi_dai_pin_map {
62}; 93};
63 94
64struct hdac_hdmi_priv { 95struct hdac_hdmi_priv {
65 struct hdac_hdmi_dai_pin_map dai_map[3]; 96 struct hdac_hdmi_dai_pin_map dai_map[HDA_MAX_CVTS];
66 struct list_head pin_list; 97 struct list_head pin_list;
67 struct list_head cvt_list; 98 struct list_head cvt_list;
99 struct list_head pcm_list;
68 int num_pin; 100 int num_pin;
69 int num_cvt; 101 int num_cvt;
102 struct mutex pin_mutex;
70}; 103};
71 104
72static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev) 105static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
@@ -76,6 +109,119 @@ static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
76 return to_ehdac_device(hdac); 109 return to_ehdac_device(hdac);
77} 110}
78 111
112static unsigned int sad_format(const u8 *sad)
113{
114 return ((sad[0] >> 0x3) & 0x1f);
115}
116
117static unsigned int sad_sample_bits_lpcm(const u8 *sad)
118{
119 return (sad[2] & 7);
120}
121
122static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
123 void *eld)
124{
125 u64 formats = SNDRV_PCM_FMTBIT_S16;
126 int i;
127 const u8 *sad, *eld_buf = eld;
128
129 sad = drm_eld_sad(eld_buf);
130 if (!sad)
131 goto format_constraint;
132
133 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
134 if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */
135
136 /*
137 * the controller support 20 and 24 bits in 32 bit
138 * container so we set S32
139 */
140 if (sad_sample_bits_lpcm(sad) & 0x6)
141 formats |= SNDRV_PCM_FMTBIT_S32;
142 }
143 }
144
145format_constraint:
146 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
147 formats);
148
149}
150
151 /* HDMI ELD routines */
152static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
153 hda_nid_t nid, int byte_index)
154{
155 unsigned int val;
156
157 val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
158 byte_index);
159
160 dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
161 byte_index, val);
162
163 return val;
164}
165
166static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
167{
168 return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
169 AC_DIPSIZE_ELD_BUF);
170}
171
172/*
173 * This function queries the ELD size and ELD data and fills in the buffer
174 * passed by user
175 */
176static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
177 unsigned char *buf, int *eld_size)
178{
179 int i, size, ret = 0;
180
181 /*
182 * ELD size is initialized to zero in caller function. If no errors and
183 * ELD is valid, actual eld_size is assigned.
184 */
185
186 size = hdac_hdmi_get_eld_size(codec, nid);
187 if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
188 dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size);
189 return -ERANGE;
190 }
191
192 /* set ELD buffer */
193 for (i = 0; i < size; i++) {
194 unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
195 /*
196 * Graphics driver might be writing to ELD buffer right now.
197 * Just abort. The caller will repoll after a while.
198 */
199 if (!(val & AC_ELDD_ELD_VALID)) {
200 dev_err(&codec->dev,
201 "HDMI: invalid ELD data byte %d\n", i);
202 ret = -EINVAL;
203 goto error;
204 }
205 val &= AC_ELDD_ELD_DATA;
206 /*
207 * The first byte cannot be zero. This can happen on some DVI
208 * connections. Some Intel chips may also need some 250ms delay
209 * to return non-zero ELD data, even when the graphics driver
210 * correctly writes ELD content before setting ELD_valid bit.
211 */
212 if (!val && !i) {
213 dev_err(&codec->dev, "HDMI: 0 ELD data\n");
214 ret = -EINVAL;
215 goto error;
216 }
217 buf[i] = val;
218 }
219
220 *eld_size = size;
221error:
222 return ret;
223}
224
79static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac, 225static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
80 hda_nid_t cvt_nid, hda_nid_t pin_nid, 226 hda_nid_t cvt_nid, hda_nid_t pin_nid,
81 u32 stream_tag, int format) 227 u32 stream_tag, int format)
@@ -107,27 +253,74 @@ hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
107 AC_VERB_SET_HDMI_DIP_INDEX, val); 253 AC_VERB_SET_HDMI_DIP_INDEX, val);
108} 254}
109 255
256struct dp_audio_infoframe {
257 u8 type; /* 0x84 */
258 u8 len; /* 0x1b */
259 u8 ver; /* 0x11 << 2 */
260
261 u8 CC02_CT47; /* match with HDMI infoframe from this on */
262 u8 SS01_SF24;
263 u8 CXT04;
264 u8 CA;
265 u8 LFEPBL01_LSV36_DM_INH7;
266};
267
110static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac, 268static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
111 hda_nid_t cvt_nid, hda_nid_t pin_nid) 269 hda_nid_t cvt_nid, hda_nid_t pin_nid)
112{ 270{
113 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE]; 271 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
114 struct hdmi_audio_infoframe frame; 272 struct hdmi_audio_infoframe frame;
115 u8 *dip = (u8 *)&frame; 273 struct dp_audio_infoframe dp_ai;
274 struct hdac_hdmi_priv *hdmi = hdac->private_data;
275 struct hdac_hdmi_pin *pin;
276 u8 *dip;
116 int ret; 277 int ret;
117 int i; 278 int i;
279 const u8 *eld_buf;
280 u8 conn_type;
281 int channels = 2;
118 282
119 hdmi_audio_infoframe_init(&frame); 283 list_for_each_entry(pin, &hdmi->pin_list, head) {
284 if (pin->nid == pin_nid)
285 break;
286 }
120 287
121 /* Default stereo for now */ 288 eld_buf = pin->eld.eld_buffer;
122 frame.channels = 2; 289 conn_type = drm_eld_get_conn_type(eld_buf);
123 290
124 /* setup channel count */ 291 /* setup channel count */
125 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0, 292 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
126 AC_VERB_SET_CVT_CHAN_COUNT, frame.channels - 1); 293 AC_VERB_SET_CVT_CHAN_COUNT, channels - 1);
127 294
128 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer)); 295 switch (conn_type) {
129 if (ret < 0) 296 case DRM_ELD_CONN_TYPE_HDMI:
130 return ret; 297 hdmi_audio_infoframe_init(&frame);
298
299 /* Default stereo for now */
300 frame.channels = channels;
301
302 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
303 if (ret < 0)
304 return ret;
305
306 break;
307
308 case DRM_ELD_CONN_TYPE_DP:
309 memset(&dp_ai, 0, sizeof(dp_ai));
310 dp_ai.type = 0x84;
311 dp_ai.len = 0x1b;
312 dp_ai.ver = 0x11 << 2;
313 dp_ai.CC02_CT47 = channels - 1;
314 dp_ai.CA = 0;
315
316 dip = (u8 *)&dp_ai;
317 break;
318
319 default:
320 dev_err(&hdac->hdac.dev, "Invalid connection type: %d\n",
321 conn_type);
322 return -EIO;
323 }
131 324
132 /* stop infoframe transmission */ 325 /* stop infoframe transmission */
133 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0); 326 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
@@ -137,9 +330,15 @@ static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
137 330
138 /* Fill infoframe. Index auto-incremented */ 331 /* Fill infoframe. Index auto-incremented */
139 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0); 332 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
140 for (i = 0; i < sizeof(frame); i++) 333 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
141 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0, 334 for (i = 0; i < sizeof(buffer); i++)
335 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
336 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
337 } else {
338 for (i = 0; i < sizeof(dp_ai); i++)
339 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
142 AC_VERB_SET_HDMI_DIP_DATA, dip[i]); 340 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
341 }
143 342
144 /* Start infoframe */ 343 /* Start infoframe */
145 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0); 344 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
@@ -174,11 +373,6 @@ static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
174 struct hdac_ext_dma_params *dd; 373 struct hdac_ext_dma_params *dd;
175 int ret; 374 int ret;
176 375
177 if (dai->id > 0) {
178 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
179 return -ENODEV;
180 }
181
182 dai_map = &hdmi->dai_map[dai->id]; 376 dai_map = &hdmi->dai_map[dai->id];
183 377
184 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream); 378 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
@@ -198,16 +392,30 @@ static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
198 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai) 392 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
199{ 393{
200 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai); 394 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
395 struct hdac_hdmi_priv *hdmi = hdac->private_data;
396 struct hdac_hdmi_dai_pin_map *dai_map;
397 struct hdac_hdmi_pin *pin;
201 struct hdac_ext_dma_params *dd; 398 struct hdac_ext_dma_params *dd;
202 399
203 if (dai->id > 0) { 400 dai_map = &hdmi->dai_map[dai->id];
204 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n"); 401 pin = dai_map->pin;
402
403 if (!pin)
404 return -ENODEV;
405
406 if ((!pin->eld.monitor_present) || (!pin->eld.eld_valid)) {
407 dev_err(&hdac->hdac.dev, "device is not configured for this pin: %d\n",
408 pin->nid);
205 return -ENODEV; 409 return -ENODEV;
206 } 410 }
207 411
208 dd = kzalloc(sizeof(*dd), GFP_KERNEL); 412 dd = snd_soc_dai_get_dma_data(dai, substream);
209 if (!dd) 413 if (!dd) {
210 return -ENOMEM; 414 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
415 if (!dd)
416 return -ENOMEM;
417 }
418
211 dd->format = snd_hdac_calc_stream_format(params_rate(hparams), 419 dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
212 params_channels(hparams), params_format(hparams), 420 params_channels(hparams), params_format(hparams),
213 24, 0); 421 24, 0);
@@ -227,50 +435,187 @@ static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
227 435
228 dai_map = &hdmi->dai_map[dai->id]; 436 dai_map = &hdmi->dai_map[dai->id];
229 437
438 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
439
440 if (dd) {
441 snd_soc_dai_set_dma_data(dai, substream, NULL);
442 kfree(dd);
443 }
444
445 return 0;
446}
447
448static void hdac_hdmi_enable_cvt(struct hdac_ext_device *edev,
449 struct hdac_hdmi_dai_pin_map *dai_map)
450{
451 /* Enable transmission */
230 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0, 452 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
231 AC_VERB_SET_CHANNEL_STREAMID, 0); 453 AC_VERB_SET_DIGI_CONVERT_1, 1);
454
455 /* Category Code (CC) to zero */
232 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0, 456 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
233 AC_VERB_SET_STREAM_FORMAT, 0); 457 AC_VERB_SET_DIGI_CONVERT_2, 0);
458}
234 459
235 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream); 460static int hdac_hdmi_enable_pin(struct hdac_ext_device *hdac,
236 snd_soc_dai_set_dma_data(dai, substream, NULL); 461 struct hdac_hdmi_dai_pin_map *dai_map)
462{
463 int mux_idx;
464 struct hdac_hdmi_pin *pin = dai_map->pin;
465
466 for (mux_idx = 0; mux_idx < pin->num_mux_nids; mux_idx++) {
467 if (pin->mux_nids[mux_idx] == dai_map->cvt->nid) {
468 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
469 AC_VERB_SET_CONNECT_SEL, mux_idx);
470 break;
471 }
472 }
473
474 if (mux_idx == pin->num_mux_nids)
475 return -EIO;
476
477 /* Enable out path for this pin widget */
478 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
479 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
237 480
238 kfree(dd); 481 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
482
483 snd_hdac_codec_write(&hdac->hdac, pin->nid, 0,
484 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
239 485
240 return 0; 486 return 0;
241} 487}
242 488
489static int hdac_hdmi_query_pin_connlist(struct hdac_ext_device *hdac,
490 struct hdac_hdmi_pin *pin)
491{
492 if (!(get_wcaps(&hdac->hdac, pin->nid) & AC_WCAP_CONN_LIST)) {
493 dev_warn(&hdac->hdac.dev,
494 "HDMI: pin %d wcaps %#x does not support connection list\n",
495 pin->nid, get_wcaps(&hdac->hdac, pin->nid));
496 return -EINVAL;
497 }
498
499 pin->num_mux_nids = snd_hdac_get_connections(&hdac->hdac, pin->nid,
500 pin->mux_nids, HDA_MAX_CONNECTIONS);
501 if (pin->num_mux_nids == 0)
502 dev_warn(&hdac->hdac.dev, "No connections found for pin: %d\n",
503 pin->nid);
504
505 dev_dbg(&hdac->hdac.dev, "num_mux_nids %d for pin: %d\n",
506 pin->num_mux_nids, pin->nid);
507
508 return pin->num_mux_nids;
509}
510
511/*
512 * Query pcm list and return pin widget to which stream is routed.
513 *
514 * Also query connection list of the pin, to validate the cvt to pin map.
515 *
516 * Same stream rendering to multiple pins simultaneously can be done
517 * possibly, but not supported for now in driver. So return the first pin
518 * connected.
519 */
520static struct hdac_hdmi_pin *hdac_hdmi_get_pin_from_cvt(
521 struct hdac_ext_device *edev,
522 struct hdac_hdmi_priv *hdmi,
523 struct hdac_hdmi_cvt *cvt)
524{
525 struct hdac_hdmi_pcm *pcm;
526 struct hdac_hdmi_pin *pin = NULL;
527 int ret, i;
528
529 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
530 if (pcm->cvt == cvt) {
531 pin = pcm->pin;
532 break;
533 }
534 }
535
536 if (pin) {
537 ret = hdac_hdmi_query_pin_connlist(edev, pin);
538 if (ret < 0)
539 return NULL;
540
541 for (i = 0; i < pin->num_mux_nids; i++) {
542 if (pin->mux_nids[i] == cvt->nid)
543 return pin;
544 }
545 }
546
547 return NULL;
548}
549
550/*
551 * This tries to get a valid pin and set the HW constraints based on the
552 * ELD. Even if a valid pin is not found return success so that device open
553 * doesn't fail.
554 */
243static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream, 555static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
244 struct snd_soc_dai *dai) 556 struct snd_soc_dai *dai)
245{ 557{
246 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai); 558 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
247 struct hdac_hdmi_priv *hdmi = hdac->private_data; 559 struct hdac_hdmi_priv *hdmi = hdac->private_data;
248 struct hdac_hdmi_dai_pin_map *dai_map; 560 struct hdac_hdmi_dai_pin_map *dai_map;
249 int val; 561 struct hdac_hdmi_cvt *cvt;
250 562 struct hdac_hdmi_pin *pin;
251 if (dai->id > 0) { 563 int ret;
252 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
253 return -ENODEV;
254 }
255 564
256 dai_map = &hdmi->dai_map[dai->id]; 565 dai_map = &hdmi->dai_map[dai->id];
257 566
258 val = snd_hdac_codec_read(&hdac->hdac, dai_map->pin->nid, 0, 567 cvt = dai_map->cvt;
259 AC_VERB_GET_PIN_SENSE, 0); 568 pin = hdac_hdmi_get_pin_from_cvt(hdac, hdmi, cvt);
260 dev_info(&hdac->hdac.dev, "Val for AC_VERB_GET_PIN_SENSE: %x\n", val);
261 569
262 if ((!(val & AC_PINSENSE_PRESENCE)) || (!(val & AC_PINSENSE_ELDV))) { 570 /*
263 dev_err(&hdac->hdac.dev, "Monitor presence invalid with val: %x\n", val); 571 * To make PA and other userland happy.
264 return -ENODEV; 572 * userland scans devices so returning error does not help.
573 */
574 if (!pin)
575 return 0;
576
577 if ((!pin->eld.monitor_present) ||
578 (!pin->eld.eld_valid)) {
579
580 dev_warn(&hdac->hdac.dev,
581 "Failed: montior present? %d ELD valid?: %d for pin: %d\n",
582 pin->eld.monitor_present, pin->eld.eld_valid, pin->nid);
583
584 return 0;
265 } 585 }
266 586
267 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0); 587 dai_map->pin = pin;
268 588
269 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0, 589 hdac_hdmi_enable_cvt(hdac, dai_map);
270 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE); 590 ret = hdac_hdmi_enable_pin(hdac, dai_map);
591 if (ret < 0)
592 return ret;
271 593
272 snd_pcm_hw_constraint_step(substream->runtime, 0, 594 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
273 SNDRV_PCM_HW_PARAM_CHANNELS, 2); 595 pin->eld.eld_buffer);
596 if (ret < 0)
597 return ret;
598
599 return snd_pcm_hw_constraint_eld(substream->runtime,
600 pin->eld.eld_buffer);
601}
602
603static int hdac_hdmi_trigger(struct snd_pcm_substream *substream, int cmd,
604 struct snd_soc_dai *dai)
605{
606 struct hdac_hdmi_dai_pin_map *dai_map;
607 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
608 struct hdac_hdmi_priv *hdmi = hdac->private_data;
609 int ret;
610
611 dai_map = &hdmi->dai_map[dai->id];
612 if (cmd == SNDRV_PCM_TRIGGER_RESUME) {
613 ret = hdac_hdmi_enable_pin(hdac, dai_map);
614 if (ret < 0)
615 return ret;
616
617 return hdac_hdmi_playback_prepare(substream, dai);
618 }
274 619
275 return 0; 620 return 0;
276} 621}
@@ -284,10 +629,19 @@ static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
284 629
285 dai_map = &hdmi->dai_map[dai->id]; 630 dai_map = &hdmi->dai_map[dai->id];
286 631
287 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3); 632 if (dai_map->pin) {
633 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
634 AC_VERB_SET_CHANNEL_STREAMID, 0);
635 snd_hdac_codec_write(&hdac->hdac, dai_map->cvt->nid, 0,
636 AC_VERB_SET_STREAM_FORMAT, 0);
637
638 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
288 639
289 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0, 640 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
290 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE); 641 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
642
643 dai_map->pin = NULL;
644 }
291} 645}
292 646
293static int 647static int
@@ -310,85 +664,326 @@ hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
310 return err; 664 return err;
311} 665}
312 666
313static void hdac_hdmi_fill_widget_info(struct snd_soc_dapm_widget *w, 667static int hdac_hdmi_fill_widget_info(struct device *dev,
314 enum snd_soc_dapm_type id, 668 struct snd_soc_dapm_widget *w,
315 const char *wname, const char *stream) 669 enum snd_soc_dapm_type id, void *priv,
670 const char *wname, const char *stream,
671 struct snd_kcontrol_new *wc, int numkc)
316{ 672{
317 w->id = id; 673 w->id = id;
318 w->name = wname; 674 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
675 if (!w->name)
676 return -ENOMEM;
677
319 w->sname = stream; 678 w->sname = stream;
320 w->reg = SND_SOC_NOPM; 679 w->reg = SND_SOC_NOPM;
321 w->shift = 0; 680 w->shift = 0;
322 w->kcontrol_news = NULL; 681 w->kcontrol_news = wc;
323 w->num_kcontrols = 0; 682 w->num_kcontrols = numkc;
324 w->priv = NULL; 683 w->priv = priv;
684
685 return 0;
325} 686}
326 687
327static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route, 688static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
328 const char *sink, const char *control, const char *src) 689 const char *sink, const char *control, const char *src,
690 int (*handler)(struct snd_soc_dapm_widget *src,
691 struct snd_soc_dapm_widget *sink))
329{ 692{
330 route->sink = sink; 693 route->sink = sink;
331 route->source = src; 694 route->source = src;
332 route->control = control; 695 route->control = control;
333 route->connected = NULL; 696 route->connected = handler;
334} 697}
335 698
336static void create_fill_widget_route_map(struct snd_soc_dapm_context *dapm, 699static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_ext_device *edev,
337 struct hdac_hdmi_dai_pin_map *dai_map) 700 struct hdac_hdmi_pin *pin)
338{ 701{
339 struct snd_soc_dapm_route route[1]; 702 struct hdac_hdmi_priv *hdmi = edev->private_data;
340 struct snd_soc_dapm_widget widgets[2] = { {0} }; 703 struct hdac_hdmi_pcm *pcm = NULL;
341 704
342 memset(&route, 0, sizeof(route)); 705 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
706 if (pcm->pin == pin)
707 return pcm;
708 }
343 709
344 hdac_hdmi_fill_widget_info(&widgets[0], snd_soc_dapm_output, 710 return NULL;
345 "hif1 Output", NULL); 711}
346 hdac_hdmi_fill_widget_info(&widgets[1], snd_soc_dapm_aif_in,
347 "Coverter 1", "hif1");
348 712
349 hdac_hdmi_fill_route(&route[0], "hif1 Output", NULL, "Coverter 1"); 713/*
714 * Based on user selection, map the PINs with the PCMs.
715 */
716static int hdac_hdmi_set_pin_mux(struct snd_kcontrol *kcontrol,
717 struct snd_ctl_elem_value *ucontrol)
718{
719 int ret;
720 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
721 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
722 struct snd_soc_dapm_context *dapm = w->dapm;
723 struct hdac_hdmi_pin *pin = w->priv;
724 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
725 struct hdac_hdmi_priv *hdmi = edev->private_data;
726 struct hdac_hdmi_pcm *pcm = NULL;
727 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
350 728
351 snd_soc_dapm_new_controls(dapm, widgets, ARRAY_SIZE(widgets)); 729 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
352 snd_soc_dapm_add_routes(dapm, route, ARRAY_SIZE(route)); 730 if (ret < 0)
731 return ret;
732
733 mutex_lock(&hdmi->pin_mutex);
734 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
735 if (pcm->pin == pin)
736 pcm->pin = NULL;
737
738 /*
739 * Jack status is not reported during device probe as the
740 * PCMs are not registered by then. So report it here.
741 */
742 if (!strcmp(cvt_name, pcm->cvt->name) && !pcm->pin) {
743 pcm->pin = pin;
744 if (pin->eld.monitor_present && pin->eld.eld_valid) {
745 dev_dbg(&edev->hdac.dev,
746 "jack report for pcm=%d\n",
747 pcm->pcm_id);
748
749 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
750 }
751 mutex_unlock(&hdmi->pin_mutex);
752 return ret;
753 }
754 }
755 mutex_unlock(&hdmi->pin_mutex);
756
757 return ret;
353} 758}
354 759
355static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev) 760/*
761 * Ideally the Mux inputs should be based on the num_muxs enumerated, but
762 * the display driver seem to be programming the connection list for the pin
763 * widget runtime.
764 *
765 * So programming all the possible inputs for the mux, the user has to take
766 * care of selecting the right one and leaving all other inputs selected to
767 * "NONE"
768 */
769static int hdac_hdmi_create_pin_muxs(struct hdac_ext_device *edev,
770 struct hdac_hdmi_pin *pin,
771 struct snd_soc_dapm_widget *widget,
772 const char *widget_name)
773{
774 struct hdac_hdmi_priv *hdmi = edev->private_data;
775 struct snd_kcontrol_new *kc;
776 struct hdac_hdmi_cvt *cvt;
777 struct soc_enum *se;
778 char kc_name[NAME_SIZE];
779 char mux_items[NAME_SIZE];
780 /* To hold inputs to the Pin mux */
781 char *items[HDA_MAX_CONNECTIONS];
782 int i = 0;
783 int num_items = hdmi->num_cvt + 1;
784
785 kc = devm_kzalloc(&edev->hdac.dev, sizeof(*kc), GFP_KERNEL);
786 if (!kc)
787 return -ENOMEM;
788
789 se = devm_kzalloc(&edev->hdac.dev, sizeof(*se), GFP_KERNEL);
790 if (!se)
791 return -ENOMEM;
792
793 sprintf(kc_name, "Pin %d Input", pin->nid);
794 kc->name = devm_kstrdup(&edev->hdac.dev, kc_name, GFP_KERNEL);
795 if (!kc->name)
796 return -ENOMEM;
797
798 kc->private_value = (long)se;
799 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
800 kc->access = 0;
801 kc->info = snd_soc_info_enum_double;
802 kc->put = hdac_hdmi_set_pin_mux;
803 kc->get = snd_soc_dapm_get_enum_double;
804
805 se->reg = SND_SOC_NOPM;
806
807 /* enum texts: ["NONE", "cvt #", "cvt #", ...] */
808 se->items = num_items;
809 se->mask = roundup_pow_of_two(se->items) - 1;
810
811 sprintf(mux_items, "NONE");
812 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
813 if (!items[i])
814 return -ENOMEM;
815
816 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
817 i++;
818 sprintf(mux_items, "cvt %d", cvt->nid);
819 items[i] = devm_kstrdup(&edev->hdac.dev, mux_items, GFP_KERNEL);
820 if (!items[i])
821 return -ENOMEM;
822 }
823
824 se->texts = devm_kmemdup(&edev->hdac.dev, items,
825 (num_items * sizeof(char *)), GFP_KERNEL);
826 if (!se->texts)
827 return -ENOMEM;
828
829 return hdac_hdmi_fill_widget_info(&edev->hdac.dev, widget,
830 snd_soc_dapm_mux, pin, widget_name, NULL, kc, 1);
831}
832
833/* Add cvt <- input <- mux route map */
834static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_ext_device *edev,
835 struct snd_soc_dapm_widget *widgets,
836 struct snd_soc_dapm_route *route, int rindex)
837{
838 struct hdac_hdmi_priv *hdmi = edev->private_data;
839 const struct snd_kcontrol_new *kc;
840 struct soc_enum *se;
841 int mux_index = hdmi->num_cvt + hdmi->num_pin;
842 int i, j;
843
844 for (i = 0; i < hdmi->num_pin; i++) {
845 kc = widgets[mux_index].kcontrol_news;
846 se = (struct soc_enum *)kc->private_value;
847 for (j = 0; j < hdmi->num_cvt; j++) {
848 hdac_hdmi_fill_route(&route[rindex],
849 widgets[mux_index].name,
850 se->texts[j + 1],
851 widgets[j].name, NULL);
852
853 rindex++;
854 }
855
856 mux_index++;
857 }
858}
859
860/*
861 * Widgets are added in the below sequence
862 * Converter widgets for num converters enumerated
863 * Pin widgets for num pins enumerated
864 * Pin mux widgets to represent connenction list of pin widget
865 *
866 * Total widgets elements = num_cvt + num_pin + num_pin;
867 *
868 * Routes are added as below:
869 * pin mux -> pin (based on num_pins)
870 * cvt -> "Input sel control" -> pin_mux
871 *
872 * Total route elements:
873 * num_pins + (pin_muxes * num_cvt)
874 */
875static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
356{ 876{
877 struct snd_soc_dapm_widget *widgets;
878 struct snd_soc_dapm_route *route;
879 struct hdac_ext_device *edev = to_hda_ext_device(dapm->dev);
357 struct hdac_hdmi_priv *hdmi = edev->private_data; 880 struct hdac_hdmi_priv *hdmi = edev->private_data;
358 struct hdac_hdmi_dai_pin_map *dai_map = &hdmi->dai_map[0]; 881 struct snd_soc_dai_driver *dai_drv = dapm->component->dai_drv;
882 char widget_name[NAME_SIZE];
359 struct hdac_hdmi_cvt *cvt; 883 struct hdac_hdmi_cvt *cvt;
360 struct hdac_hdmi_pin *pin; 884 struct hdac_hdmi_pin *pin;
885 int ret, i = 0, num_routes = 0;
361 886
362 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list)) 887 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
363 return -EINVAL; 888 return -EINVAL;
364 889
365 /* 890 widgets = devm_kzalloc(dapm->dev,
366 * Currently on board only 1 pin and 1 converter is enabled for 891 (sizeof(*widgets) * ((2 * hdmi->num_pin) + hdmi->num_cvt)),
367 * simplification, more will be added eventually 892 GFP_KERNEL);
368 * So using fixed map for dai_id:pin:cvt
369 */
370 cvt = list_first_entry(&hdmi->cvt_list, struct hdac_hdmi_cvt, head);
371 pin = list_first_entry(&hdmi->pin_list, struct hdac_hdmi_pin, head);
372 893
373 dai_map->dai_id = 0; 894 if (!widgets)
374 dai_map->pin = pin; 895 return -ENOMEM;
375 896
376 dai_map->cvt = cvt; 897 /* DAPM widgets to represent each converter widget */
898 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
899 sprintf(widget_name, "Converter %d", cvt->nid);
900 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
901 snd_soc_dapm_aif_in, &cvt->nid,
902 widget_name, dai_drv[i].playback.stream_name, NULL, 0);
903 if (ret < 0)
904 return ret;
905 i++;
906 }
377 907
378 /* Enable out path for this pin widget */ 908 list_for_each_entry(pin, &hdmi->pin_list, head) {
379 snd_hdac_codec_write(&edev->hdac, pin->nid, 0, 909 sprintf(widget_name, "hif%d Output", pin->nid);
380 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT); 910 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
911 snd_soc_dapm_output, &pin->nid,
912 widget_name, NULL, NULL, 0);
913 if (ret < 0)
914 return ret;
915 i++;
916 }
381 917
382 /* Enable transmission */ 918 /* DAPM widgets to represent the connection list to pin widget */
383 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0, 919 list_for_each_entry(pin, &hdmi->pin_list, head) {
384 AC_VERB_SET_DIGI_CONVERT_1, 1); 920 sprintf(widget_name, "Pin %d Mux", pin->nid);
921 ret = hdac_hdmi_create_pin_muxs(edev, pin, &widgets[i],
922 widget_name);
923 if (ret < 0)
924 return ret;
925 i++;
385 926
386 /* Category Code (CC) to zero */ 927 /* For cvt to pin_mux mapping */
387 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0, 928 num_routes += hdmi->num_cvt;
388 AC_VERB_SET_DIGI_CONVERT_2, 0); 929
930 /* For pin_mux to pin mapping */
931 num_routes++;
932 }
389 933
390 snd_hdac_codec_write(&edev->hdac, pin->nid, 0, 934 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
391 AC_VERB_SET_CONNECT_SEL, 0); 935 GFP_KERNEL);
936 if (!route)
937 return -ENOMEM;
938
939 i = 0;
940 /* Add pin <- NULL <- mux route map */
941 list_for_each_entry(pin, &hdmi->pin_list, head) {
942 int sink_index = i + hdmi->num_cvt;
943 int src_index = sink_index + hdmi->num_pin;
944
945 hdac_hdmi_fill_route(&route[i],
946 widgets[sink_index].name, NULL,
947 widgets[src_index].name, NULL);
948 i++;
949
950 }
951
952 hdac_hdmi_add_pinmux_cvt_route(edev, widgets, route, i);
953
954 snd_soc_dapm_new_controls(dapm, widgets,
955 ((2 * hdmi->num_pin) + hdmi->num_cvt));
956
957 snd_soc_dapm_add_routes(dapm, route, num_routes);
958 snd_soc_dapm_new_widgets(dapm->card);
959
960 return 0;
961
962}
963
964static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
965{
966 struct hdac_hdmi_priv *hdmi = edev->private_data;
967 struct hdac_hdmi_dai_pin_map *dai_map;
968 struct hdac_hdmi_cvt *cvt;
969 int dai_id = 0;
970
971 if (list_empty(&hdmi->cvt_list))
972 return -EINVAL;
973
974 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
975 dai_map = &hdmi->dai_map[dai_id];
976 dai_map->dai_id = dai_id;
977 dai_map->cvt = cvt;
978
979 dai_id++;
980
981 if (dai_id == HDA_MAX_CVTS) {
982 dev_warn(&edev->hdac.dev,
983 "Max dais supported: %d\n", dai_id);
984 break;
985 }
986 }
392 987
393 return 0; 988 return 0;
394} 989}
@@ -397,12 +992,15 @@ static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
397{ 992{
398 struct hdac_hdmi_priv *hdmi = edev->private_data; 993 struct hdac_hdmi_priv *hdmi = edev->private_data;
399 struct hdac_hdmi_cvt *cvt; 994 struct hdac_hdmi_cvt *cvt;
995 char name[NAME_SIZE];
400 996
401 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL); 997 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
402 if (!cvt) 998 if (!cvt)
403 return -ENOMEM; 999 return -ENOMEM;
404 1000
405 cvt->nid = nid; 1001 cvt->nid = nid;
1002 sprintf(name, "cvt %d", cvt->nid);
1003 cvt->name = kstrdup(name, GFP_KERNEL);
406 1004
407 list_add_tail(&cvt->head, &hdmi->cvt_list); 1005 list_add_tail(&cvt->head, &hdmi->cvt_list);
408 hdmi->num_cvt++; 1006 hdmi->num_cvt++;
@@ -410,6 +1008,106 @@ static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
410 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt); 1008 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
411} 1009}
412 1010
1011static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
1012{
1013 struct hdac_ext_device *edev = pin->edev;
1014 struct hdac_hdmi_priv *hdmi = edev->private_data;
1015 struct hdac_hdmi_pcm *pcm;
1016 int val;
1017
1018 pin->repoll_count = repoll;
1019
1020 pm_runtime_get_sync(&edev->hdac.dev);
1021 val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
1022 AC_VERB_GET_PIN_SENSE, 0);
1023
1024 dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
1025 val, pin->nid);
1026
1027
1028 mutex_lock(&hdmi->pin_mutex);
1029 pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
1030 pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
1031
1032 pcm = hdac_hdmi_get_pcm(edev, pin);
1033
1034 if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
1035
1036 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
1037 __func__, pin->nid);
1038
1039 /*
1040 * PCMs are not registered during device probe, so don't
1041 * report jack here. It will be done in usermode mux
1042 * control select.
1043 */
1044 if (pcm) {
1045 dev_dbg(&edev->hdac.dev,
1046 "jack report for pcm=%d\n", pcm->pcm_id);
1047
1048 snd_jack_report(pcm->jack, 0);
1049 }
1050
1051 mutex_unlock(&hdmi->pin_mutex);
1052 goto put_hdac_device;
1053 }
1054
1055 if (pin->eld.monitor_present && pin->eld.eld_valid) {
1056 /* TODO: use i915 component for reading ELD later */
1057 if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
1058 pin->eld.eld_buffer,
1059 &pin->eld.eld_size) == 0) {
1060
1061 if (pcm) {
1062 dev_dbg(&edev->hdac.dev,
1063 "jack report for pcm=%d\n",
1064 pcm->pcm_id);
1065
1066 snd_jack_report(pcm->jack, SND_JACK_AVOUT);
1067 }
1068
1069 print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
1070 pin->eld.eld_buffer, pin->eld.eld_size);
1071 } else {
1072 pin->eld.monitor_present = false;
1073 pin->eld.eld_valid = false;
1074
1075 if (pcm) {
1076 dev_dbg(&edev->hdac.dev,
1077 "jack report for pcm=%d\n",
1078 pcm->pcm_id);
1079
1080 snd_jack_report(pcm->jack, 0);
1081 }
1082 }
1083 }
1084
1085 mutex_unlock(&hdmi->pin_mutex);
1086
1087 /*
1088 * Sometimes the pin_sense may present invalid monitor
1089 * present and eld_valid. If ELD data is not valid, loop few
1090 * more times to get correct pin sense and valid ELD.
1091 */
1092 if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
1093 schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
1094
1095put_hdac_device:
1096 pm_runtime_put_sync(&edev->hdac.dev);
1097}
1098
1099static void hdac_hdmi_repoll_eld(struct work_struct *work)
1100{
1101 struct hdac_hdmi_pin *pin =
1102 container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
1103
1104 /* picked from legacy HDA driver */
1105 if (pin->repoll_count++ > 6)
1106 pin->repoll_count = 0;
1107
1108 hdac_hdmi_present_sense(pin, pin->repoll_count);
1109}
1110
413static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid) 1111static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
414{ 1112{
415 struct hdac_hdmi_priv *hdmi = edev->private_data; 1113 struct hdac_hdmi_priv *hdmi = edev->private_data;
@@ -424,6 +1122,120 @@ static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
424 list_add_tail(&pin->head, &hdmi->pin_list); 1122 list_add_tail(&pin->head, &hdmi->pin_list);
425 hdmi->num_pin++; 1123 hdmi->num_pin++;
426 1124
1125 pin->edev = edev;
1126 INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
1127
1128 return 0;
1129}
1130
1131#define INTEL_VENDOR_NID 0x08
1132#define INTEL_GET_VENDOR_VERB 0xf81
1133#define INTEL_SET_VENDOR_VERB 0x781
1134#define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
1135#define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
1136
1137static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdac)
1138{
1139 unsigned int vendor_param;
1140
1141 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1142 INTEL_GET_VENDOR_VERB, 0);
1143 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1144 return;
1145
1146 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1147 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1148 INTEL_SET_VENDOR_VERB, vendor_param);
1149 if (vendor_param == -1)
1150 return;
1151}
1152
1153static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdac)
1154{
1155 unsigned int vendor_param;
1156
1157 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1158 INTEL_GET_VENDOR_VERB, 0);
1159 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1160 return;
1161
1162 /* enable DP1.2 mode */
1163 vendor_param |= INTEL_EN_DP12;
1164 vendor_param = snd_hdac_codec_read(hdac, INTEL_VENDOR_NID, 0,
1165 INTEL_SET_VENDOR_VERB, vendor_param);
1166 if (vendor_param == -1)
1167 return;
1168
1169}
1170
1171static struct snd_soc_dai_ops hdmi_dai_ops = {
1172 .startup = hdac_hdmi_pcm_open,
1173 .shutdown = hdac_hdmi_pcm_close,
1174 .hw_params = hdac_hdmi_set_hw_params,
1175 .prepare = hdac_hdmi_playback_prepare,
1176 .trigger = hdac_hdmi_trigger,
1177 .hw_free = hdac_hdmi_playback_cleanup,
1178};
1179
1180/*
1181 * Each converter can support a stream independently. So a dai is created
1182 * based on the number of converter queried.
1183 */
1184static int hdac_hdmi_create_dais(struct hdac_device *hdac,
1185 struct snd_soc_dai_driver **dais,
1186 struct hdac_hdmi_priv *hdmi, int num_dais)
1187{
1188 struct snd_soc_dai_driver *hdmi_dais;
1189 struct hdac_hdmi_cvt *cvt;
1190 char name[NAME_SIZE], dai_name[NAME_SIZE];
1191 int i = 0;
1192 u32 rates, bps;
1193 unsigned int rate_max = 384000, rate_min = 8000;
1194 u64 formats;
1195 int ret;
1196
1197 hdmi_dais = devm_kzalloc(&hdac->dev,
1198 (sizeof(*hdmi_dais) * num_dais),
1199 GFP_KERNEL);
1200 if (!hdmi_dais)
1201 return -ENOMEM;
1202
1203 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1204 ret = snd_hdac_query_supported_pcm(hdac, cvt->nid,
1205 &rates, &formats, &bps);
1206 if (ret)
1207 return ret;
1208
1209 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1210 hdmi_dais[i].name = devm_kstrdup(&hdac->dev,
1211 dai_name, GFP_KERNEL);
1212
1213 if (!hdmi_dais[i].name)
1214 return -ENOMEM;
1215
1216 snprintf(name, sizeof(name), "hifi%d", i+1);
1217 hdmi_dais[i].playback.stream_name =
1218 devm_kstrdup(&hdac->dev, name, GFP_KERNEL);
1219 if (!hdmi_dais[i].playback.stream_name)
1220 return -ENOMEM;
1221
1222 /*
1223 * Set caps based on capability queried from the converter.
1224 * It will be constrained runtime based on ELD queried.
1225 */
1226 hdmi_dais[i].playback.formats = formats;
1227 hdmi_dais[i].playback.rates = rates;
1228 hdmi_dais[i].playback.rate_max = rate_max;
1229 hdmi_dais[i].playback.rate_min = rate_min;
1230 hdmi_dais[i].playback.channels_min = 2;
1231 hdmi_dais[i].playback.channels_max = 2;
1232 hdmi_dais[i].ops = &hdmi_dai_ops;
1233
1234 i++;
1235 }
1236
1237 *dais = hdmi_dais;
1238
427 return 0; 1239 return 0;
428} 1240}
429 1241
@@ -431,7 +1243,8 @@ static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
431 * Parse all nodes and store the cvt/pin nids in array 1243 * Parse all nodes and store the cvt/pin nids in array
432 * Add one time initialization for pin and cvt widgets 1244 * Add one time initialization for pin and cvt widgets
433 */ 1245 */
434static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev) 1246static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev,
1247 struct snd_soc_dai_driver **dais, int *num_dais)
435{ 1248{
436 hda_nid_t nid; 1249 hda_nid_t nid;
437 int i, num_nodes; 1250 int i, num_nodes;
@@ -439,6 +1252,9 @@ static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev)
439 struct hdac_hdmi_priv *hdmi = edev->private_data; 1252 struct hdac_hdmi_priv *hdmi = edev->private_data;
440 int ret; 1253 int ret;
441 1254
1255 hdac_hdmi_skl_enable_all_pins(hdac);
1256 hdac_hdmi_skl_enable_dp12(hdac);
1257
442 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid); 1258 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
443 if (!nid || num_nodes <= 0) { 1259 if (!nid || num_nodes <= 0) {
444 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n"); 1260 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
@@ -479,19 +1295,107 @@ static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev)
479 if (!hdmi->num_pin || !hdmi->num_cvt) 1295 if (!hdmi->num_pin || !hdmi->num_cvt)
480 return -EIO; 1296 return -EIO;
481 1297
1298 ret = hdac_hdmi_create_dais(hdac, dais, hdmi, hdmi->num_cvt);
1299 if (ret) {
1300 dev_err(&hdac->dev, "Failed to create dais with err: %d\n",
1301 ret);
1302 return ret;
1303 }
1304
1305 *num_dais = hdmi->num_cvt;
1306
482 return hdac_hdmi_init_dai_map(edev); 1307 return hdac_hdmi_init_dai_map(edev);
483} 1308}
484 1309
1310static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
1311{
1312 struct hdac_ext_device *edev = aptr;
1313 struct hdac_hdmi_priv *hdmi = edev->private_data;
1314 struct hdac_hdmi_pin *pin;
1315 struct snd_soc_codec *codec = edev->scodec;
1316
1317 /* Don't know how this mapping is derived */
1318 hda_nid_t pin_nid = port + 0x04;
1319
1320 dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
1321
1322 /*
1323 * skip notification during system suspend (but not in runtime PM);
1324 * the state will be updated at resume. Also since the ELD and
1325 * connection states are updated in anyway at the end of the resume,
1326 * we can skip it when received during PM process.
1327 */
1328 if (snd_power_get_state(codec->component.card->snd_card) !=
1329 SNDRV_CTL_POWER_D0)
1330 return;
1331
1332 if (atomic_read(&edev->hdac.in_pm))
1333 return;
1334
1335 list_for_each_entry(pin, &hdmi->pin_list, head) {
1336 if (pin->nid == pin_nid)
1337 hdac_hdmi_present_sense(pin, 1);
1338 }
1339}
1340
1341static struct i915_audio_component_audio_ops aops = {
1342 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1343};
1344
1345int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device)
1346{
1347 char jack_name[NAME_SIZE];
1348 struct snd_soc_codec *codec = dai->codec;
1349 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1350 struct snd_soc_dapm_context *dapm =
1351 snd_soc_component_get_dapm(&codec->component);
1352 struct hdac_hdmi_priv *hdmi = edev->private_data;
1353 struct hdac_hdmi_pcm *pcm;
1354
1355 /*
1356 * this is a new PCM device, create new pcm and
1357 * add to the pcm list
1358 */
1359 pcm = kzalloc(sizeof(*pcm), GFP_KERNEL);
1360 if (!pcm)
1361 return -ENOMEM;
1362 pcm->pcm_id = device;
1363 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1364
1365 list_add_tail(&pcm->head, &hdmi->pcm_list);
1366
1367 sprintf(jack_name, "HDMI/DP, pcm=%d Jack", device);
1368
1369 return snd_jack_new(dapm->card->snd_card, jack_name,
1370 SND_JACK_AVOUT, &pcm->jack, true, false);
1371}
1372EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1373
485static int hdmi_codec_probe(struct snd_soc_codec *codec) 1374static int hdmi_codec_probe(struct snd_soc_codec *codec)
486{ 1375{
487 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec); 1376 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
488 struct hdac_hdmi_priv *hdmi = edev->private_data; 1377 struct hdac_hdmi_priv *hdmi = edev->private_data;
489 struct snd_soc_dapm_context *dapm = 1378 struct snd_soc_dapm_context *dapm =
490 snd_soc_component_get_dapm(&codec->component); 1379 snd_soc_component_get_dapm(&codec->component);
1380 struct hdac_hdmi_pin *pin;
1381 int ret;
491 1382
492 edev->scodec = codec; 1383 edev->scodec = codec;
493 1384
494 create_fill_widget_route_map(dapm, &hdmi->dai_map[0]); 1385 ret = create_fill_widget_route_map(dapm);
1386 if (ret < 0)
1387 return ret;
1388
1389 aops.audio_ptr = edev;
1390 ret = snd_hdac_i915_register_notifier(&aops);
1391 if (ret < 0) {
1392 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
1393 ret);
1394 return ret;
1395 }
1396
1397 list_for_each_entry(pin, &hdmi->pin_list, head)
1398 hdac_hdmi_present_sense(pin, 1);
495 1399
496 /* Imp: Store the card pointer in hda_codec */ 1400 /* Imp: Store the card pointer in hda_codec */
497 edev->card = dapm->card->snd_card; 1401 edev->card = dapm->card->snd_card;
@@ -515,44 +1419,73 @@ static int hdmi_codec_remove(struct snd_soc_codec *codec)
515 return 0; 1419 return 0;
516} 1420}
517 1421
1422#ifdef CONFIG_PM
1423static int hdmi_codec_resume(struct snd_soc_codec *codec)
1424{
1425 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
1426 struct hdac_hdmi_priv *hdmi = edev->private_data;
1427 struct hdac_hdmi_pin *pin;
1428 struct hdac_device *hdac = &edev->hdac;
1429 struct hdac_bus *bus = hdac->bus;
1430 int err;
1431 unsigned long timeout;
1432
1433 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1434 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1435
1436 /* Power up afg */
1437 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0)) {
1438
1439 snd_hdac_codec_write(hdac, hdac->afg, 0,
1440 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
1441
1442 /* Wait till power state is set to D0 */
1443 timeout = jiffies + msecs_to_jiffies(1000);
1444 while (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0)
1445 && time_before(jiffies, timeout)) {
1446 msleep(50);
1447 }
1448 }
1449
1450 /*
1451 * As the ELD notify callback request is not entertained while the
1452 * device is in suspend state. Need to manually check detection of
1453 * all pins here.
1454 */
1455 list_for_each_entry(pin, &hdmi->pin_list, head)
1456 hdac_hdmi_present_sense(pin, 1);
1457
1458 /*
1459 * Codec power is turned ON during controller resume.
1460 * Turn it OFF here
1461 */
1462 err = snd_hdac_display_power(bus, false);
1463 if (err < 0) {
1464 dev_err(bus->dev,
1465 "Cannot turn OFF display power on i915, err: %d\n",
1466 err);
1467 return err;
1468 }
1469
1470 return 0;
1471}
1472#else
1473#define hdmi_codec_resume NULL
1474#endif
1475
518static struct snd_soc_codec_driver hdmi_hda_codec = { 1476static struct snd_soc_codec_driver hdmi_hda_codec = {
519 .probe = hdmi_codec_probe, 1477 .probe = hdmi_codec_probe,
520 .remove = hdmi_codec_remove, 1478 .remove = hdmi_codec_remove,
1479 .resume = hdmi_codec_resume,
521 .idle_bias_off = true, 1480 .idle_bias_off = true,
522}; 1481};
523 1482
524static struct snd_soc_dai_ops hdmi_dai_ops = {
525 .startup = hdac_hdmi_pcm_open,
526 .shutdown = hdac_hdmi_pcm_close,
527 .hw_params = hdac_hdmi_set_hw_params,
528 .prepare = hdac_hdmi_playback_prepare,
529 .hw_free = hdac_hdmi_playback_cleanup,
530};
531
532static struct snd_soc_dai_driver hdmi_dais[] = {
533 { .name = "intel-hdmi-hif1",
534 .playback = {
535 .stream_name = "hif1",
536 .channels_min = 2,
537 .channels_max = 2,
538 .rates = SNDRV_PCM_RATE_32000 |
539 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
540 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
541 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
542 .formats = SNDRV_PCM_FMTBIT_S16_LE |
543 SNDRV_PCM_FMTBIT_S20_3LE |
544 SNDRV_PCM_FMTBIT_S24_LE |
545 SNDRV_PCM_FMTBIT_S32_LE,
546
547 },
548 .ops = &hdmi_dai_ops,
549 },
550};
551
552static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev) 1483static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
553{ 1484{
554 struct hdac_device *codec = &edev->hdac; 1485 struct hdac_device *codec = &edev->hdac;
555 struct hdac_hdmi_priv *hdmi_priv; 1486 struct hdac_hdmi_priv *hdmi_priv;
1487 struct snd_soc_dai_driver *hdmi_dais = NULL;
1488 int num_dais = 0;
556 int ret = 0; 1489 int ret = 0;
557 1490
558 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL); 1491 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
@@ -565,14 +1498,31 @@ static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
565 1498
566 INIT_LIST_HEAD(&hdmi_priv->pin_list); 1499 INIT_LIST_HEAD(&hdmi_priv->pin_list);
567 INIT_LIST_HEAD(&hdmi_priv->cvt_list); 1500 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
1501 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
1502 mutex_init(&hdmi_priv->pin_mutex);
568 1503
569 ret = hdac_hdmi_parse_and_map_nid(edev); 1504 /*
570 if (ret < 0) 1505 * Turned off in the runtime_suspend during the first explicit
1506 * pm_runtime_suspend call.
1507 */
1508 ret = snd_hdac_display_power(edev->hdac.bus, true);
1509 if (ret < 0) {
1510 dev_err(&edev->hdac.dev,
1511 "Cannot turn on display power on i915 err: %d\n",
1512 ret);
571 return ret; 1513 return ret;
1514 }
1515
1516 ret = hdac_hdmi_parse_and_map_nid(edev, &hdmi_dais, &num_dais);
1517 if (ret < 0) {
1518 dev_err(&codec->dev,
1519 "Failed in parse and map nid with err: %d\n", ret);
1520 return ret;
1521 }
572 1522
573 /* ASoC specific initialization */ 1523 /* ASoC specific initialization */
574 return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec, 1524 return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
575 hdmi_dais, ARRAY_SIZE(hdmi_dais)); 1525 hdmi_dais, num_dais);
576} 1526}
577 1527
578static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev) 1528static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
@@ -580,11 +1530,20 @@ static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
580 struct hdac_hdmi_priv *hdmi = edev->private_data; 1530 struct hdac_hdmi_priv *hdmi = edev->private_data;
581 struct hdac_hdmi_pin *pin, *pin_next; 1531 struct hdac_hdmi_pin *pin, *pin_next;
582 struct hdac_hdmi_cvt *cvt, *cvt_next; 1532 struct hdac_hdmi_cvt *cvt, *cvt_next;
1533 struct hdac_hdmi_pcm *pcm, *pcm_next;
583 1534
584 snd_soc_unregister_codec(&edev->hdac.dev); 1535 snd_soc_unregister_codec(&edev->hdac.dev);
585 1536
1537 list_for_each_entry_safe(pcm, pcm_next, &hdmi->pcm_list, head) {
1538 pcm->cvt = NULL;
1539 pcm->pin = NULL;
1540 list_del(&pcm->head);
1541 kfree(pcm);
1542 }
1543
586 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) { 1544 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
587 list_del(&cvt->head); 1545 list_del(&cvt->head);
1546 kfree(cvt->name);
588 kfree(cvt); 1547 kfree(cvt);
589 } 1548 }
590 1549
@@ -602,6 +1561,7 @@ static int hdac_hdmi_runtime_suspend(struct device *dev)
602 struct hdac_ext_device *edev = to_hda_ext_device(dev); 1561 struct hdac_ext_device *edev = to_hda_ext_device(dev);
603 struct hdac_device *hdac = &edev->hdac; 1562 struct hdac_device *hdac = &edev->hdac;
604 struct hdac_bus *bus = hdac->bus; 1563 struct hdac_bus *bus = hdac->bus;
1564 unsigned long timeout;
605 int err; 1565 int err;
606 1566
607 dev_dbg(dev, "Enter: %s\n", __func__); 1567 dev_dbg(dev, "Enter: %s\n", __func__);
@@ -611,10 +1571,19 @@ static int hdac_hdmi_runtime_suspend(struct device *dev)
611 return 0; 1571 return 0;
612 1572
613 /* Power down afg */ 1573 /* Power down afg */
614 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)) 1574 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)) {
615 snd_hdac_codec_write(hdac, hdac->afg, 0, 1575 snd_hdac_codec_write(hdac, hdac->afg, 0,
616 AC_VERB_SET_POWER_STATE, AC_PWRST_D3); 1576 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
617 1577
1578 /* Wait till power state is set to D3 */
1579 timeout = jiffies + msecs_to_jiffies(1000);
1580 while (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)
1581 && time_before(jiffies, timeout)) {
1582
1583 msleep(50);
1584 }
1585 }
1586
618 err = snd_hdac_display_power(bus, false); 1587 err = snd_hdac_display_power(bus, false);
619 if (err < 0) { 1588 if (err < 0) {
620 dev_err(bus->dev, "Cannot turn on display power on i915\n"); 1589 dev_err(bus->dev, "Cannot turn on display power on i915\n");
@@ -643,6 +1612,9 @@ static int hdac_hdmi_runtime_resume(struct device *dev)
643 return err; 1612 return err;
644 } 1613 }
645 1614
1615 hdac_hdmi_skl_enable_all_pins(&edev->hdac);
1616 hdac_hdmi_skl_enable_dp12(&edev->hdac);
1617
646 /* Power up afg */ 1618 /* Power up afg */
647 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0)) 1619 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
648 snd_hdac_codec_write(hdac, hdac->afg, 0, 1620 snd_hdac_codec_write(hdac, hdac->afg, 0,
@@ -661,6 +1633,7 @@ static const struct dev_pm_ops hdac_hdmi_pm = {
661 1633
662static const struct hda_device_id hdmi_list[] = { 1634static const struct hda_device_id hdmi_list[] = {
663 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0), 1635 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
1636 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
664 {} 1637 {}
665}; 1638};
666 1639