Commit | Line | Data |
---|---|---|
18382ead SP |
1 | /* |
2 | * hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms | |
3 | * | |
4 | * Copyright (C) 2014-2015 Intel Corp | |
5 | * Author: Samreen Nilofer <samreen.nilofer@intel.com> | |
6 | * Subhransu S. Prusty <subhransu.s.prusty@intel.com> | |
7 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; version 2 of the License. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
19 | */ | |
20 | #include <linux/init.h> | |
21 | #include <linux/delay.h> | |
22 | #include <linux/module.h> | |
23 | #include <linux/pm_runtime.h> | |
a657f1d0 | 24 | #include <linux/hdmi.h> |
2428bca3 | 25 | #include <drm/drm_edid.h> |
18382ead SP |
26 | #include <sound/pcm_params.h> |
27 | #include <sound/soc.h> | |
28 | #include <sound/hdaudio_ext.h> | |
07f083ab | 29 | #include <sound/hda_i915.h> |
2428bca3 | 30 | #include <sound/pcm_drm_eld.h> |
18382ead SP |
31 | #include "../../hda/local.h" |
32 | ||
b0362adb SP |
33 | #define AMP_OUT_MUTE 0xb080 |
34 | #define AMP_OUT_UNMUTE 0xb000 | |
18382ead | 35 | #define PIN_OUT (AC_PINCTL_OUT_EN) |
b0362adb | 36 | |
18382ead SP |
37 | #define HDA_MAX_CONNECTIONS 32 |
38 | ||
b8a54545 SP |
39 | #define ELD_MAX_SIZE 256 |
40 | #define ELD_FIXED_BYTES 20 | |
41 | ||
18382ead SP |
42 | struct hdac_hdmi_cvt_params { |
43 | unsigned int channels_min; | |
44 | unsigned int channels_max; | |
45 | u32 rates; | |
46 | u64 formats; | |
47 | unsigned int maxbps; | |
48 | }; | |
49 | ||
50 | struct hdac_hdmi_cvt { | |
15b91447 | 51 | struct list_head head; |
18382ead SP |
52 | hda_nid_t nid; |
53 | struct hdac_hdmi_cvt_params params; | |
54 | }; | |
55 | ||
b8a54545 SP |
56 | struct hdac_hdmi_eld { |
57 | bool monitor_present; | |
58 | bool eld_valid; | |
59 | int eld_size; | |
60 | char eld_buffer[ELD_MAX_SIZE]; | |
61 | }; | |
62 | ||
18382ead | 63 | struct hdac_hdmi_pin { |
15b91447 | 64 | struct list_head head; |
18382ead SP |
65 | hda_nid_t nid; |
66 | int num_mux_nids; | |
67 | hda_nid_t mux_nids[HDA_MAX_CONNECTIONS]; | |
b8a54545 SP |
68 | struct hdac_hdmi_eld eld; |
69 | struct hdac_ext_device *edev; | |
70 | int repoll_count; | |
71 | struct delayed_work work; | |
18382ead SP |
72 | }; |
73 | ||
74 | struct hdac_hdmi_dai_pin_map { | |
75 | int dai_id; | |
15b91447 SP |
76 | struct hdac_hdmi_pin *pin; |
77 | struct hdac_hdmi_cvt *cvt; | |
18382ead SP |
78 | }; |
79 | ||
80 | struct hdac_hdmi_priv { | |
18382ead | 81 | struct hdac_hdmi_dai_pin_map dai_map[3]; |
15b91447 SP |
82 | struct list_head pin_list; |
83 | struct list_head cvt_list; | |
84 | int num_pin; | |
85 | int num_cvt; | |
18382ead SP |
86 | }; |
87 | ||
e342ac08 SP |
88 | static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev) |
89 | { | |
51b2c425 | 90 | struct hdac_device *hdac = dev_to_hdac_dev(dev); |
e342ac08 | 91 | |
51b2c425 | 92 | return to_ehdac_device(hdac); |
e342ac08 SP |
93 | } |
94 | ||
2428bca3 SP |
95 | static unsigned int sad_format(const u8 *sad) |
96 | { | |
97 | return ((sad[0] >> 0x3) & 0x1f); | |
98 | } | |
99 | ||
100 | static unsigned int sad_sample_bits_lpcm(const u8 *sad) | |
101 | { | |
102 | return (sad[2] & 7); | |
103 | } | |
104 | ||
105 | static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime, | |
106 | void *eld) | |
107 | { | |
108 | u64 formats = SNDRV_PCM_FMTBIT_S16; | |
109 | int i; | |
110 | const u8 *sad, *eld_buf = eld; | |
111 | ||
112 | sad = drm_eld_sad(eld_buf); | |
113 | if (!sad) | |
114 | goto format_constraint; | |
115 | ||
116 | for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) { | |
117 | if (sad_format(sad) == 1) { /* AUDIO_CODING_TYPE_LPCM */ | |
118 | ||
119 | /* | |
120 | * the controller support 20 and 24 bits in 32 bit | |
121 | * container so we set S32 | |
122 | */ | |
123 | if (sad_sample_bits_lpcm(sad) & 0x6) | |
124 | formats |= SNDRV_PCM_FMTBIT_S32; | |
125 | } | |
126 | } | |
127 | ||
128 | format_constraint: | |
129 | return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT, | |
130 | formats); | |
131 | ||
132 | } | |
133 | ||
b8a54545 SP |
134 | /* HDMI ELD routines */ |
135 | static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec, | |
136 | hda_nid_t nid, int byte_index) | |
137 | { | |
138 | unsigned int val; | |
139 | ||
140 | val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD, | |
141 | byte_index); | |
142 | ||
143 | dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n", | |
144 | byte_index, val); | |
145 | ||
146 | return val; | |
147 | } | |
148 | ||
149 | static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid) | |
150 | { | |
151 | return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE, | |
152 | AC_DIPSIZE_ELD_BUF); | |
153 | } | |
154 | ||
155 | /* | |
156 | * This function queries the ELD size and ELD data and fills in the buffer | |
157 | * passed by user | |
158 | */ | |
159 | static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid, | |
160 | unsigned char *buf, int *eld_size) | |
161 | { | |
162 | int i, size, ret = 0; | |
163 | ||
164 | /* | |
165 | * ELD size is initialized to zero in caller function. If no errors and | |
166 | * ELD is valid, actual eld_size is assigned. | |
167 | */ | |
168 | ||
169 | size = hdac_hdmi_get_eld_size(codec, nid); | |
170 | if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) { | |
171 | dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size); | |
172 | return -ERANGE; | |
173 | } | |
174 | ||
175 | /* set ELD buffer */ | |
176 | for (i = 0; i < size; i++) { | |
177 | unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i); | |
178 | /* | |
179 | * Graphics driver might be writing to ELD buffer right now. | |
180 | * Just abort. The caller will repoll after a while. | |
181 | */ | |
182 | if (!(val & AC_ELDD_ELD_VALID)) { | |
183 | dev_err(&codec->dev, | |
184 | "HDMI: invalid ELD data byte %d\n", i); | |
185 | ret = -EINVAL; | |
186 | goto error; | |
187 | } | |
188 | val &= AC_ELDD_ELD_DATA; | |
189 | /* | |
190 | * The first byte cannot be zero. This can happen on some DVI | |
191 | * connections. Some Intel chips may also need some 250ms delay | |
192 | * to return non-zero ELD data, even when the graphics driver | |
193 | * correctly writes ELD content before setting ELD_valid bit. | |
194 | */ | |
195 | if (!val && !i) { | |
196 | dev_err(&codec->dev, "HDMI: 0 ELD data\n"); | |
197 | ret = -EINVAL; | |
198 | goto error; | |
199 | } | |
200 | buf[i] = val; | |
201 | } | |
202 | ||
203 | *eld_size = size; | |
204 | error: | |
205 | return ret; | |
206 | } | |
207 | ||
b0362adb SP |
208 | static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac, |
209 | hda_nid_t cvt_nid, hda_nid_t pin_nid, | |
210 | u32 stream_tag, int format) | |
211 | { | |
212 | unsigned int val; | |
213 | ||
214 | dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n", | |
215 | cvt_nid, pin_nid, stream_tag, format); | |
216 | ||
217 | val = (stream_tag << 4); | |
218 | ||
219 | snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0, | |
220 | AC_VERB_SET_CHANNEL_STREAMID, val); | |
221 | snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0, | |
222 | AC_VERB_SET_STREAM_FORMAT, format); | |
223 | ||
224 | return 0; | |
225 | } | |
226 | ||
a657f1d0 SP |
227 | static void |
228 | hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid, | |
229 | int packet_index, int byte_index) | |
230 | { | |
231 | int val; | |
232 | ||
233 | val = (packet_index << 5) | (byte_index & 0x1f); | |
234 | ||
235 | snd_hdac_codec_write(&hdac->hdac, pin_nid, 0, | |
236 | AC_VERB_SET_HDMI_DIP_INDEX, val); | |
237 | } | |
238 | ||
239 | static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac, | |
240 | hda_nid_t cvt_nid, hda_nid_t pin_nid) | |
241 | { | |
242 | uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE]; | |
243 | struct hdmi_audio_infoframe frame; | |
244 | u8 *dip = (u8 *)&frame; | |
245 | int ret; | |
246 | int i; | |
247 | ||
248 | hdmi_audio_infoframe_init(&frame); | |
249 | ||
250 | /* Default stereo for now */ | |
251 | frame.channels = 2; | |
252 | ||
253 | /* setup channel count */ | |
254 | snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0, | |
255 | AC_VERB_SET_CVT_CHAN_COUNT, frame.channels - 1); | |
256 | ||
257 | ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer)); | |
258 | if (ret < 0) | |
259 | return ret; | |
260 | ||
261 | /* stop infoframe transmission */ | |
262 | hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0); | |
263 | snd_hdac_codec_write(&hdac->hdac, pin_nid, 0, | |
264 | AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE); | |
265 | ||
266 | ||
267 | /* Fill infoframe. Index auto-incremented */ | |
268 | hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0); | |
269 | for (i = 0; i < sizeof(frame); i++) | |
270 | snd_hdac_codec_write(&hdac->hdac, pin_nid, 0, | |
271 | AC_VERB_SET_HDMI_DIP_DATA, dip[i]); | |
272 | ||
273 | /* Start infoframe */ | |
274 | hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0); | |
275 | snd_hdac_codec_write(&hdac->hdac, pin_nid, 0, | |
276 | AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST); | |
277 | ||
278 | return 0; | |
279 | } | |
280 | ||
b0362adb SP |
281 | static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev, |
282 | struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state) | |
283 | { | |
284 | /* Power up pin widget */ | |
15b91447 SP |
285 | if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid, |
286 | pwr_state)) | |
287 | snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0, | |
b0362adb SP |
288 | AC_VERB_SET_POWER_STATE, pwr_state); |
289 | ||
290 | /* Power up converter */ | |
15b91447 SP |
291 | if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid, |
292 | pwr_state)) | |
293 | snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0, | |
b0362adb SP |
294 | AC_VERB_SET_POWER_STATE, pwr_state); |
295 | } | |
296 | ||
297 | static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream, | |
298 | struct snd_soc_dai *dai) | |
299 | { | |
300 | struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai); | |
301 | struct hdac_hdmi_priv *hdmi = hdac->private_data; | |
302 | struct hdac_hdmi_dai_pin_map *dai_map; | |
303 | struct hdac_ext_dma_params *dd; | |
a657f1d0 | 304 | int ret; |
b0362adb SP |
305 | |
306 | if (dai->id > 0) { | |
307 | dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n"); | |
308 | return -ENODEV; | |
309 | } | |
310 | ||
311 | dai_map = &hdmi->dai_map[dai->id]; | |
312 | ||
313 | dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream); | |
314 | dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n", | |
315 | dd->stream_tag, dd->format); | |
316 | ||
15b91447 SP |
317 | ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid, |
318 | dai_map->pin->nid); | |
a657f1d0 SP |
319 | if (ret < 0) |
320 | return ret; | |
321 | ||
15b91447 SP |
322 | return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid, |
323 | dai_map->pin->nid, dd->stream_tag, dd->format); | |
b0362adb SP |
324 | } |
325 | ||
326 | static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream, | |
327 | struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai) | |
328 | { | |
329 | struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai); | |
330 | struct hdac_ext_dma_params *dd; | |
331 | ||
332 | if (dai->id > 0) { | |
333 | dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n"); | |
334 | return -ENODEV; | |
335 | } | |
336 | ||
337 | dd = kzalloc(sizeof(*dd), GFP_KERNEL); | |
8d33ab24 SM |
338 | if (!dd) |
339 | return -ENOMEM; | |
b0362adb SP |
340 | dd->format = snd_hdac_calc_stream_format(params_rate(hparams), |
341 | params_channels(hparams), params_format(hparams), | |
342 | 24, 0); | |
343 | ||
344 | snd_soc_dai_set_dma_data(dai, substream, (void *)dd); | |
345 | ||
346 | return 0; | |
347 | } | |
348 | ||
349 | static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream, | |
350 | struct snd_soc_dai *dai) | |
351 | { | |
352 | struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai); | |
353 | struct hdac_ext_dma_params *dd; | |
354 | struct hdac_hdmi_priv *hdmi = edev->private_data; | |
355 | struct hdac_hdmi_dai_pin_map *dai_map; | |
356 | ||
357 | dai_map = &hdmi->dai_map[dai->id]; | |
358 | ||
15b91447 | 359 | snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0, |
b0362adb | 360 | AC_VERB_SET_CHANNEL_STREAMID, 0); |
15b91447 | 361 | snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0, |
b0362adb SP |
362 | AC_VERB_SET_STREAM_FORMAT, 0); |
363 | ||
364 | dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream); | |
365 | snd_soc_dai_set_dma_data(dai, substream, NULL); | |
366 | ||
367 | kfree(dd); | |
368 | ||
369 | return 0; | |
370 | } | |
371 | ||
372 | static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream, | |
373 | struct snd_soc_dai *dai) | |
374 | { | |
375 | struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai); | |
376 | struct hdac_hdmi_priv *hdmi = hdac->private_data; | |
377 | struct hdac_hdmi_dai_pin_map *dai_map; | |
2428bca3 | 378 | int ret; |
b0362adb SP |
379 | |
380 | if (dai->id > 0) { | |
381 | dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n"); | |
382 | return -ENODEV; | |
383 | } | |
384 | ||
385 | dai_map = &hdmi->dai_map[dai->id]; | |
386 | ||
b8a54545 SP |
387 | if ((!dai_map->pin->eld.monitor_present) || |
388 | (!dai_map->pin->eld.eld_valid)) { | |
389 | ||
390 | dev_err(&hdac->hdac.dev, | |
391 | "Failed: montior present? %d ELD valid?: %d\n", | |
392 | dai_map->pin->eld.monitor_present, | |
393 | dai_map->pin->eld.eld_valid); | |
b0362adb | 394 | |
b0362adb SP |
395 | return -ENODEV; |
396 | } | |
397 | ||
398 | hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0); | |
399 | ||
15b91447 | 400 | snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0, |
b0362adb SP |
401 | AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE); |
402 | ||
2428bca3 SP |
403 | ret = hdac_hdmi_eld_limit_formats(substream->runtime, |
404 | dai_map->pin->eld.eld_buffer); | |
405 | if (ret < 0) | |
406 | return ret; | |
b0362adb | 407 | |
2428bca3 SP |
408 | return snd_pcm_hw_constraint_eld(substream->runtime, |
409 | dai_map->pin->eld.eld_buffer); | |
b0362adb SP |
410 | } |
411 | ||
412 | static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream, | |
413 | struct snd_soc_dai *dai) | |
414 | { | |
415 | struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai); | |
416 | struct hdac_hdmi_priv *hdmi = hdac->private_data; | |
417 | struct hdac_hdmi_dai_pin_map *dai_map; | |
418 | ||
419 | dai_map = &hdmi->dai_map[dai->id]; | |
420 | ||
421 | hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3); | |
422 | ||
15b91447 | 423 | snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0, |
b0362adb SP |
424 | AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE); |
425 | } | |
426 | ||
18382ead SP |
427 | static int |
428 | hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt) | |
429 | { | |
430 | int err; | |
431 | ||
432 | /* Only stereo supported as of now */ | |
433 | cvt->params.channels_min = cvt->params.channels_max = 2; | |
434 | ||
435 | err = snd_hdac_query_supported_pcm(hdac, cvt->nid, | |
436 | &cvt->params.rates, | |
437 | &cvt->params.formats, | |
438 | &cvt->params.maxbps); | |
439 | if (err < 0) | |
440 | dev_err(&hdac->dev, | |
441 | "Failed to query pcm params for nid %d: %d\n", | |
442 | cvt->nid, err); | |
443 | ||
444 | return err; | |
445 | } | |
446 | ||
18382ead SP |
447 | static void hdac_hdmi_fill_widget_info(struct snd_soc_dapm_widget *w, |
448 | enum snd_soc_dapm_type id, | |
449 | const char *wname, const char *stream) | |
450 | { | |
451 | w->id = id; | |
452 | w->name = wname; | |
453 | w->sname = stream; | |
454 | w->reg = SND_SOC_NOPM; | |
455 | w->shift = 0; | |
456 | w->kcontrol_news = NULL; | |
457 | w->num_kcontrols = 0; | |
458 | w->priv = NULL; | |
459 | } | |
460 | ||
461 | static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route, | |
462 | const char *sink, const char *control, const char *src) | |
463 | { | |
464 | route->sink = sink; | |
465 | route->source = src; | |
466 | route->control = control; | |
467 | route->connected = NULL; | |
468 | } | |
469 | ||
470 | static void create_fill_widget_route_map(struct snd_soc_dapm_context *dapm, | |
471 | struct hdac_hdmi_dai_pin_map *dai_map) | |
472 | { | |
473 | struct snd_soc_dapm_route route[1]; | |
474 | struct snd_soc_dapm_widget widgets[2] = { {0} }; | |
475 | ||
476 | memset(&route, 0, sizeof(route)); | |
477 | ||
478 | hdac_hdmi_fill_widget_info(&widgets[0], snd_soc_dapm_output, | |
479 | "hif1 Output", NULL); | |
480 | hdac_hdmi_fill_widget_info(&widgets[1], snd_soc_dapm_aif_in, | |
481 | "Coverter 1", "hif1"); | |
482 | ||
483 | hdac_hdmi_fill_route(&route[0], "hif1 Output", NULL, "Coverter 1"); | |
484 | ||
485 | snd_soc_dapm_new_controls(dapm, widgets, ARRAY_SIZE(widgets)); | |
486 | snd_soc_dapm_add_routes(dapm, route, ARRAY_SIZE(route)); | |
487 | } | |
488 | ||
15b91447 | 489 | static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev) |
18382ead | 490 | { |
15b91447 SP |
491 | struct hdac_hdmi_priv *hdmi = edev->private_data; |
492 | struct hdac_hdmi_dai_pin_map *dai_map = &hdmi->dai_map[0]; | |
493 | struct hdac_hdmi_cvt *cvt; | |
494 | struct hdac_hdmi_pin *pin; | |
18382ead | 495 | |
15b91447 SP |
496 | if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list)) |
497 | return -EINVAL; | |
18382ead | 498 | |
15b91447 SP |
499 | /* |
500 | * Currently on board only 1 pin and 1 converter is enabled for | |
501 | * simplification, more will be added eventually | |
502 | * So using fixed map for dai_id:pin:cvt | |
503 | */ | |
504 | cvt = list_first_entry(&hdmi->cvt_list, struct hdac_hdmi_cvt, head); | |
505 | pin = list_first_entry(&hdmi->pin_list, struct hdac_hdmi_pin, head); | |
506 | ||
507 | dai_map->dai_id = 0; | |
508 | dai_map->pin = pin; | |
18382ead | 509 | |
15b91447 | 510 | dai_map->cvt = cvt; |
18382ead SP |
511 | |
512 | /* Enable out path for this pin widget */ | |
15b91447 | 513 | snd_hdac_codec_write(&edev->hdac, pin->nid, 0, |
18382ead SP |
514 | AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT); |
515 | ||
516 | /* Enable transmission */ | |
15b91447 | 517 | snd_hdac_codec_write(&edev->hdac, cvt->nid, 0, |
18382ead SP |
518 | AC_VERB_SET_DIGI_CONVERT_1, 1); |
519 | ||
520 | /* Category Code (CC) to zero */ | |
15b91447 | 521 | snd_hdac_codec_write(&edev->hdac, cvt->nid, 0, |
18382ead SP |
522 | AC_VERB_SET_DIGI_CONVERT_2, 0); |
523 | ||
15b91447 | 524 | snd_hdac_codec_write(&edev->hdac, pin->nid, 0, |
18382ead SP |
525 | AC_VERB_SET_CONNECT_SEL, 0); |
526 | ||
15b91447 SP |
527 | return 0; |
528 | } | |
529 | ||
530 | static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid) | |
531 | { | |
532 | struct hdac_hdmi_priv *hdmi = edev->private_data; | |
533 | struct hdac_hdmi_cvt *cvt; | |
534 | ||
535 | cvt = kzalloc(sizeof(*cvt), GFP_KERNEL); | |
536 | if (!cvt) | |
537 | return -ENOMEM; | |
538 | ||
539 | cvt->nid = nid; | |
540 | ||
541 | list_add_tail(&cvt->head, &hdmi->cvt_list); | |
542 | hdmi->num_cvt++; | |
543 | ||
544 | return hdac_hdmi_query_cvt_params(&edev->hdac, cvt); | |
545 | } | |
546 | ||
b8a54545 SP |
547 | static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll) |
548 | { | |
549 | struct hdac_ext_device *edev = pin->edev; | |
550 | int val; | |
551 | ||
552 | if (!edev) | |
553 | return; | |
554 | ||
555 | pin->repoll_count = repoll; | |
556 | ||
557 | pm_runtime_get_sync(&edev->hdac.dev); | |
558 | val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0, | |
559 | AC_VERB_GET_PIN_SENSE, 0); | |
560 | ||
561 | dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n", | |
562 | val, pin->nid); | |
563 | ||
564 | pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE); | |
565 | pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV); | |
566 | ||
567 | if (!pin->eld.monitor_present || !pin->eld.eld_valid) { | |
568 | ||
569 | dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n", | |
570 | __func__, pin->nid); | |
571 | goto put_hdac_device; | |
572 | } | |
573 | ||
574 | if (pin->eld.monitor_present && pin->eld.eld_valid) { | |
575 | /* TODO: use i915 component for reading ELD later */ | |
576 | if (hdac_hdmi_get_eld(&edev->hdac, pin->nid, | |
577 | pin->eld.eld_buffer, | |
578 | &pin->eld.eld_size) == 0) { | |
579 | ||
580 | print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET, | |
581 | pin->eld.eld_buffer, pin->eld.eld_size); | |
582 | } else { | |
583 | pin->eld.monitor_present = false; | |
584 | pin->eld.eld_valid = false; | |
585 | } | |
586 | } | |
587 | ||
588 | /* | |
589 | * Sometimes the pin_sense may present invalid monitor | |
590 | * present and eld_valid. If ELD data is not valid, loop few | |
591 | * more times to get correct pin sense and valid ELD. | |
592 | */ | |
593 | if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll) | |
594 | schedule_delayed_work(&pin->work, msecs_to_jiffies(300)); | |
595 | ||
596 | put_hdac_device: | |
597 | pm_runtime_put_sync(&edev->hdac.dev); | |
598 | } | |
599 | ||
600 | static void hdac_hdmi_repoll_eld(struct work_struct *work) | |
601 | { | |
602 | struct hdac_hdmi_pin *pin = | |
603 | container_of(to_delayed_work(work), struct hdac_hdmi_pin, work); | |
604 | ||
605 | /* picked from legacy HDA driver */ | |
606 | if (pin->repoll_count++ > 6) | |
607 | pin->repoll_count = 0; | |
608 | ||
609 | hdac_hdmi_present_sense(pin, pin->repoll_count); | |
610 | } | |
611 | ||
15b91447 SP |
612 | static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid) |
613 | { | |
614 | struct hdac_hdmi_priv *hdmi = edev->private_data; | |
615 | struct hdac_hdmi_pin *pin; | |
616 | ||
617 | pin = kzalloc(sizeof(*pin), GFP_KERNEL); | |
618 | if (!pin) | |
619 | return -ENOMEM; | |
620 | ||
621 | pin->nid = nid; | |
622 | ||
623 | list_add_tail(&pin->head, &hdmi->pin_list); | |
624 | hdmi->num_pin++; | |
625 | ||
b8a54545 SP |
626 | pin->edev = edev; |
627 | INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld); | |
628 | ||
15b91447 | 629 | return 0; |
18382ead SP |
630 | } |
631 | ||
632 | /* | |
633 | * Parse all nodes and store the cvt/pin nids in array | |
634 | * Add one time initialization for pin and cvt widgets | |
635 | */ | |
636 | static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev) | |
637 | { | |
638 | hda_nid_t nid; | |
3c83ac23 | 639 | int i, num_nodes; |
18382ead SP |
640 | struct hdac_device *hdac = &edev->hdac; |
641 | struct hdac_hdmi_priv *hdmi = edev->private_data; | |
15b91447 | 642 | int ret; |
18382ead | 643 | |
3c83ac23 | 644 | num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid); |
541140d4 | 645 | if (!nid || num_nodes <= 0) { |
18382ead SP |
646 | dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n"); |
647 | return -EINVAL; | |
648 | } | |
649 | ||
3c83ac23 | 650 | hdac->num_nodes = num_nodes; |
18382ead SP |
651 | hdac->start_nid = nid; |
652 | ||
653 | for (i = 0; i < hdac->num_nodes; i++, nid++) { | |
654 | unsigned int caps; | |
655 | unsigned int type; | |
656 | ||
657 | caps = get_wcaps(hdac, nid); | |
658 | type = get_wcaps_type(caps); | |
659 | ||
660 | if (!(caps & AC_WCAP_DIGITAL)) | |
661 | continue; | |
662 | ||
663 | switch (type) { | |
664 | ||
665 | case AC_WID_AUD_OUT: | |
15b91447 SP |
666 | ret = hdac_hdmi_add_cvt(edev, nid); |
667 | if (ret < 0) | |
668 | return ret; | |
18382ead SP |
669 | break; |
670 | ||
671 | case AC_WID_PIN: | |
15b91447 SP |
672 | ret = hdac_hdmi_add_pin(edev, nid); |
673 | if (ret < 0) | |
674 | return ret; | |
18382ead SP |
675 | break; |
676 | } | |
677 | } | |
678 | ||
679 | hdac->end_nid = nid; | |
680 | ||
15b91447 | 681 | if (!hdmi->num_pin || !hdmi->num_cvt) |
18382ead SP |
682 | return -EIO; |
683 | ||
15b91447 | 684 | return hdac_hdmi_init_dai_map(edev); |
18382ead SP |
685 | } |
686 | ||
b8a54545 SP |
687 | static void hdac_hdmi_eld_notify_cb(void *aptr, int port) |
688 | { | |
689 | struct hdac_ext_device *edev = aptr; | |
690 | struct hdac_hdmi_priv *hdmi = edev->private_data; | |
691 | struct hdac_hdmi_pin *pin; | |
692 | struct snd_soc_codec *codec = edev->scodec; | |
693 | ||
694 | /* Don't know how this mapping is derived */ | |
695 | hda_nid_t pin_nid = port + 0x04; | |
696 | ||
697 | dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid); | |
698 | ||
699 | /* | |
700 | * skip notification during system suspend (but not in runtime PM); | |
701 | * the state will be updated at resume. Also since the ELD and | |
702 | * connection states are updated in anyway at the end of the resume, | |
703 | * we can skip it when received during PM process. | |
704 | */ | |
705 | if (snd_power_get_state(codec->component.card->snd_card) != | |
706 | SNDRV_CTL_POWER_D0) | |
707 | return; | |
708 | ||
709 | if (atomic_read(&edev->hdac.in_pm)) | |
710 | return; | |
711 | ||
712 | list_for_each_entry(pin, &hdmi->pin_list, head) { | |
713 | if (pin->nid == pin_nid) | |
714 | hdac_hdmi_present_sense(pin, 1); | |
715 | } | |
716 | } | |
717 | ||
718 | static struct i915_audio_component_audio_ops aops = { | |
719 | .pin_eld_notify = hdac_hdmi_eld_notify_cb, | |
720 | }; | |
721 | ||
18382ead SP |
722 | static int hdmi_codec_probe(struct snd_soc_codec *codec) |
723 | { | |
724 | struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec); | |
725 | struct hdac_hdmi_priv *hdmi = edev->private_data; | |
726 | struct snd_soc_dapm_context *dapm = | |
727 | snd_soc_component_get_dapm(&codec->component); | |
b8a54545 SP |
728 | struct hdac_hdmi_pin *pin; |
729 | int ret; | |
18382ead SP |
730 | |
731 | edev->scodec = codec; | |
732 | ||
733 | create_fill_widget_route_map(dapm, &hdmi->dai_map[0]); | |
734 | ||
b8a54545 SP |
735 | aops.audio_ptr = edev; |
736 | ret = snd_hdac_i915_register_notifier(&aops); | |
737 | if (ret < 0) { | |
738 | dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n", | |
739 | ret); | |
740 | return ret; | |
741 | } | |
742 | ||
743 | list_for_each_entry(pin, &hdmi->pin_list, head) | |
744 | hdac_hdmi_present_sense(pin, 1); | |
745 | ||
18382ead SP |
746 | /* Imp: Store the card pointer in hda_codec */ |
747 | edev->card = dapm->card->snd_card; | |
748 | ||
e342ac08 SP |
749 | /* |
750 | * hdac_device core already sets the state to active and calls | |
751 | * get_noresume. So enable runtime and set the device to suspend. | |
752 | */ | |
753 | pm_runtime_enable(&edev->hdac.dev); | |
754 | pm_runtime_put(&edev->hdac.dev); | |
755 | pm_runtime_suspend(&edev->hdac.dev); | |
756 | ||
757 | return 0; | |
758 | } | |
759 | ||
760 | static int hdmi_codec_remove(struct snd_soc_codec *codec) | |
761 | { | |
762 | struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec); | |
763 | ||
764 | pm_runtime_disable(&edev->hdac.dev); | |
18382ead SP |
765 | return 0; |
766 | } | |
767 | ||
768 | static struct snd_soc_codec_driver hdmi_hda_codec = { | |
769 | .probe = hdmi_codec_probe, | |
e342ac08 | 770 | .remove = hdmi_codec_remove, |
18382ead SP |
771 | .idle_bias_off = true, |
772 | }; | |
773 | ||
b0362adb SP |
774 | static struct snd_soc_dai_ops hdmi_dai_ops = { |
775 | .startup = hdac_hdmi_pcm_open, | |
776 | .shutdown = hdac_hdmi_pcm_close, | |
777 | .hw_params = hdac_hdmi_set_hw_params, | |
778 | .prepare = hdac_hdmi_playback_prepare, | |
779 | .hw_free = hdac_hdmi_playback_cleanup, | |
780 | }; | |
781 | ||
18382ead SP |
782 | static struct snd_soc_dai_driver hdmi_dais[] = { |
783 | { .name = "intel-hdmi-hif1", | |
784 | .playback = { | |
785 | .stream_name = "hif1", | |
786 | .channels_min = 2, | |
787 | .channels_max = 2, | |
788 | .rates = SNDRV_PCM_RATE_32000 | | |
789 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | | |
790 | SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | | |
791 | SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000, | |
792 | .formats = SNDRV_PCM_FMTBIT_S16_LE | | |
793 | SNDRV_PCM_FMTBIT_S20_3LE | | |
794 | SNDRV_PCM_FMTBIT_S24_LE | | |
795 | SNDRV_PCM_FMTBIT_S32_LE, | |
796 | ||
797 | }, | |
b0362adb | 798 | .ops = &hdmi_dai_ops, |
18382ead SP |
799 | }, |
800 | }; | |
801 | ||
802 | static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev) | |
803 | { | |
804 | struct hdac_device *codec = &edev->hdac; | |
805 | struct hdac_hdmi_priv *hdmi_priv; | |
806 | int ret = 0; | |
807 | ||
808 | hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL); | |
809 | if (hdmi_priv == NULL) | |
810 | return -ENOMEM; | |
811 | ||
812 | edev->private_data = hdmi_priv; | |
813 | ||
814 | dev_set_drvdata(&codec->dev, edev); | |
815 | ||
15b91447 SP |
816 | INIT_LIST_HEAD(&hdmi_priv->pin_list); |
817 | INIT_LIST_HEAD(&hdmi_priv->cvt_list); | |
818 | ||
18382ead SP |
819 | ret = hdac_hdmi_parse_and_map_nid(edev); |
820 | if (ret < 0) | |
821 | return ret; | |
822 | ||
823 | /* ASoC specific initialization */ | |
824 | return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec, | |
825 | hdmi_dais, ARRAY_SIZE(hdmi_dais)); | |
826 | } | |
827 | ||
828 | static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev) | |
829 | { | |
15b91447 SP |
830 | struct hdac_hdmi_priv *hdmi = edev->private_data; |
831 | struct hdac_hdmi_pin *pin, *pin_next; | |
832 | struct hdac_hdmi_cvt *cvt, *cvt_next; | |
833 | ||
18382ead SP |
834 | snd_soc_unregister_codec(&edev->hdac.dev); |
835 | ||
15b91447 SP |
836 | list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) { |
837 | list_del(&cvt->head); | |
838 | kfree(cvt); | |
839 | } | |
840 | ||
841 | list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) { | |
842 | list_del(&pin->head); | |
843 | kfree(pin); | |
844 | } | |
845 | ||
18382ead SP |
846 | return 0; |
847 | } | |
848 | ||
e342ac08 SP |
849 | #ifdef CONFIG_PM |
850 | static int hdac_hdmi_runtime_suspend(struct device *dev) | |
851 | { | |
852 | struct hdac_ext_device *edev = to_hda_ext_device(dev); | |
853 | struct hdac_device *hdac = &edev->hdac; | |
07f083ab SP |
854 | struct hdac_bus *bus = hdac->bus; |
855 | int err; | |
e342ac08 SP |
856 | |
857 | dev_dbg(dev, "Enter: %s\n", __func__); | |
858 | ||
07f083ab SP |
859 | /* controller may not have been initialized for the first time */ |
860 | if (!bus) | |
861 | return 0; | |
862 | ||
e342ac08 SP |
863 | /* Power down afg */ |
864 | if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3)) | |
865 | snd_hdac_codec_write(hdac, hdac->afg, 0, | |
866 | AC_VERB_SET_POWER_STATE, AC_PWRST_D3); | |
867 | ||
07f083ab SP |
868 | err = snd_hdac_display_power(bus, false); |
869 | if (err < 0) { | |
870 | dev_err(bus->dev, "Cannot turn on display power on i915\n"); | |
871 | return err; | |
872 | } | |
873 | ||
e342ac08 SP |
874 | return 0; |
875 | } | |
876 | ||
877 | static int hdac_hdmi_runtime_resume(struct device *dev) | |
878 | { | |
879 | struct hdac_ext_device *edev = to_hda_ext_device(dev); | |
880 | struct hdac_device *hdac = &edev->hdac; | |
07f083ab SP |
881 | struct hdac_bus *bus = hdac->bus; |
882 | int err; | |
e342ac08 SP |
883 | |
884 | dev_dbg(dev, "Enter: %s\n", __func__); | |
885 | ||
07f083ab SP |
886 | /* controller may not have been initialized for the first time */ |
887 | if (!bus) | |
888 | return 0; | |
889 | ||
890 | err = snd_hdac_display_power(bus, true); | |
891 | if (err < 0) { | |
892 | dev_err(bus->dev, "Cannot turn on display power on i915\n"); | |
893 | return err; | |
894 | } | |
895 | ||
e342ac08 SP |
896 | /* Power up afg */ |
897 | if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0)) | |
898 | snd_hdac_codec_write(hdac, hdac->afg, 0, | |
899 | AC_VERB_SET_POWER_STATE, AC_PWRST_D0); | |
900 | ||
901 | return 0; | |
902 | } | |
903 | #else | |
904 | #define hdac_hdmi_runtime_suspend NULL | |
905 | #define hdac_hdmi_runtime_resume NULL | |
906 | #endif | |
907 | ||
908 | static const struct dev_pm_ops hdac_hdmi_pm = { | |
909 | SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL) | |
910 | }; | |
911 | ||
18382ead SP |
912 | static const struct hda_device_id hdmi_list[] = { |
913 | HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0), | |
914 | {} | |
915 | }; | |
916 | ||
917 | MODULE_DEVICE_TABLE(hdaudio, hdmi_list); | |
918 | ||
919 | static struct hdac_ext_driver hdmi_driver = { | |
920 | . hdac = { | |
921 | .driver = { | |
922 | .name = "HDMI HDA Codec", | |
e342ac08 | 923 | .pm = &hdac_hdmi_pm, |
18382ead SP |
924 | }, |
925 | .id_table = hdmi_list, | |
926 | }, | |
927 | .probe = hdac_hdmi_dev_probe, | |
928 | .remove = hdac_hdmi_dev_remove, | |
929 | }; | |
930 | ||
931 | static int __init hdmi_init(void) | |
932 | { | |
933 | return snd_hda_ext_driver_register(&hdmi_driver); | |
934 | } | |
935 | ||
936 | static void __exit hdmi_exit(void) | |
937 | { | |
938 | snd_hda_ext_driver_unregister(&hdmi_driver); | |
939 | } | |
940 | ||
941 | module_init(hdmi_init); | |
942 | module_exit(hdmi_exit); | |
943 | ||
944 | MODULE_LICENSE("GPL v2"); | |
945 | MODULE_DESCRIPTION("HDMI HD codec"); | |
946 | MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>"); | |
947 | MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>"); |