blob: fff225d6539c6cd70381b6667e12010a9240201c [file] [log] [blame]
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +05301/*
2 * hdac_hdmi.c - ASoc HDA-HDMI codec driver for Intel platforms
3 *
4 * Copyright (C) 2014-2015 Intel Corp
5 * Author: Samreen Nilofer <samreen.nilofer@intel.com>
6 * Subhransu S. Prusty <subhransu.s.prusty@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 */
20#include <linux/init.h>
21#include <linux/delay.h>
22#include <linux/module.h>
23#include <linux/pm_runtime.h>
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +053024#include <linux/hdmi.h>
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053025#include <sound/pcm_params.h>
26#include <sound/soc.h>
27#include <sound/hdaudio_ext.h>
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +053028#include <sound/hda_i915.h>
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053029#include "../../hda/local.h"
30
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +053031#define AMP_OUT_MUTE 0xb080
32#define AMP_OUT_UNMUTE 0xb000
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053033#define PIN_OUT (AC_PINCTL_OUT_EN)
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +053034
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053035#define HDA_MAX_CONNECTIONS 32
36
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053037#define ELD_MAX_SIZE 256
38#define ELD_FIXED_BYTES 20
39
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053040struct hdac_hdmi_cvt_params {
41 unsigned int channels_min;
42 unsigned int channels_max;
43 u32 rates;
44 u64 formats;
45 unsigned int maxbps;
46};
47
48struct hdac_hdmi_cvt {
Subhransu S. Prusty15b91442015-12-09 21:46:10 +053049 struct list_head head;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053050 hda_nid_t nid;
51 struct hdac_hdmi_cvt_params params;
52};
53
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053054struct hdac_hdmi_eld {
55 bool monitor_present;
56 bool eld_valid;
57 int eld_size;
58 char eld_buffer[ELD_MAX_SIZE];
59};
60
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053061struct hdac_hdmi_pin {
Subhransu S. Prusty15b91442015-12-09 21:46:10 +053062 struct list_head head;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053063 hda_nid_t nid;
64 int num_mux_nids;
65 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053066 struct hdac_hdmi_eld eld;
67 struct hdac_ext_device *edev;
68 int repoll_count;
69 struct delayed_work work;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053070};
71
72struct hdac_hdmi_dai_pin_map {
73 int dai_id;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +053074 struct hdac_hdmi_pin *pin;
75 struct hdac_hdmi_cvt *cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053076};
77
78struct hdac_hdmi_priv {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053079 struct hdac_hdmi_dai_pin_map dai_map[3];
Subhransu S. Prusty15b91442015-12-09 21:46:10 +053080 struct list_head pin_list;
81 struct list_head cvt_list;
82 int num_pin;
83 int num_cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +053084};
85
Subhransu S. Prustye342ac02015-11-10 18:42:07 +053086static inline struct hdac_ext_device *to_hda_ext_device(struct device *dev)
87{
Geliang Tang51b2c422015-12-28 22:47:13 +080088 struct hdac_device *hdac = dev_to_hdac_dev(dev);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +053089
Geliang Tang51b2c422015-12-28 22:47:13 +080090 return to_ehdac_device(hdac);
Subhransu S. Prustye342ac02015-11-10 18:42:07 +053091}
92
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +053093 /* HDMI ELD routines */
94static unsigned int hdac_hdmi_get_eld_data(struct hdac_device *codec,
95 hda_nid_t nid, int byte_index)
96{
97 unsigned int val;
98
99 val = snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_ELDD,
100 byte_index);
101
102 dev_dbg(&codec->dev, "HDMI: ELD data byte %d: 0x%x\n",
103 byte_index, val);
104
105 return val;
106}
107
108static int hdac_hdmi_get_eld_size(struct hdac_device *codec, hda_nid_t nid)
109{
110 return snd_hdac_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
111 AC_DIPSIZE_ELD_BUF);
112}
113
114/*
115 * This function queries the ELD size and ELD data and fills in the buffer
116 * passed by user
117 */
118static int hdac_hdmi_get_eld(struct hdac_device *codec, hda_nid_t nid,
119 unsigned char *buf, int *eld_size)
120{
121 int i, size, ret = 0;
122
123 /*
124 * ELD size is initialized to zero in caller function. If no errors and
125 * ELD is valid, actual eld_size is assigned.
126 */
127
128 size = hdac_hdmi_get_eld_size(codec, nid);
129 if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
130 dev_err(&codec->dev, "HDMI: invalid ELD buf size %d\n", size);
131 return -ERANGE;
132 }
133
134 /* set ELD buffer */
135 for (i = 0; i < size; i++) {
136 unsigned int val = hdac_hdmi_get_eld_data(codec, nid, i);
137 /*
138 * Graphics driver might be writing to ELD buffer right now.
139 * Just abort. The caller will repoll after a while.
140 */
141 if (!(val & AC_ELDD_ELD_VALID)) {
142 dev_err(&codec->dev,
143 "HDMI: invalid ELD data byte %d\n", i);
144 ret = -EINVAL;
145 goto error;
146 }
147 val &= AC_ELDD_ELD_DATA;
148 /*
149 * The first byte cannot be zero. This can happen on some DVI
150 * connections. Some Intel chips may also need some 250ms delay
151 * to return non-zero ELD data, even when the graphics driver
152 * correctly writes ELD content before setting ELD_valid bit.
153 */
154 if (!val && !i) {
155 dev_err(&codec->dev, "HDMI: 0 ELD data\n");
156 ret = -EINVAL;
157 goto error;
158 }
159 buf[i] = val;
160 }
161
162 *eld_size = size;
163error:
164 return ret;
165}
166
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530167static int hdac_hdmi_setup_stream(struct hdac_ext_device *hdac,
168 hda_nid_t cvt_nid, hda_nid_t pin_nid,
169 u32 stream_tag, int format)
170{
171 unsigned int val;
172
173 dev_dbg(&hdac->hdac.dev, "cvt nid %d pnid %d stream %d format 0x%x\n",
174 cvt_nid, pin_nid, stream_tag, format);
175
176 val = (stream_tag << 4);
177
178 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
179 AC_VERB_SET_CHANNEL_STREAMID, val);
180 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
181 AC_VERB_SET_STREAM_FORMAT, format);
182
183 return 0;
184}
185
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530186static void
187hdac_hdmi_set_dip_index(struct hdac_ext_device *hdac, hda_nid_t pin_nid,
188 int packet_index, int byte_index)
189{
190 int val;
191
192 val = (packet_index << 5) | (byte_index & 0x1f);
193
194 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
195 AC_VERB_SET_HDMI_DIP_INDEX, val);
196}
197
198static int hdac_hdmi_setup_audio_infoframe(struct hdac_ext_device *hdac,
199 hda_nid_t cvt_nid, hda_nid_t pin_nid)
200{
201 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
202 struct hdmi_audio_infoframe frame;
203 u8 *dip = (u8 *)&frame;
204 int ret;
205 int i;
206
207 hdmi_audio_infoframe_init(&frame);
208
209 /* Default stereo for now */
210 frame.channels = 2;
211
212 /* setup channel count */
213 snd_hdac_codec_write(&hdac->hdac, cvt_nid, 0,
214 AC_VERB_SET_CVT_CHAN_COUNT, frame.channels - 1);
215
216 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
217 if (ret < 0)
218 return ret;
219
220 /* stop infoframe transmission */
221 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
222 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
223 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
224
225
226 /* Fill infoframe. Index auto-incremented */
227 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
228 for (i = 0; i < sizeof(frame); i++)
229 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
230 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
231
232 /* Start infoframe */
233 hdac_hdmi_set_dip_index(hdac, pin_nid, 0x0, 0x0);
234 snd_hdac_codec_write(&hdac->hdac, pin_nid, 0,
235 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
236
237 return 0;
238}
239
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530240static void hdac_hdmi_set_power_state(struct hdac_ext_device *edev,
241 struct hdac_hdmi_dai_pin_map *dai_map, unsigned int pwr_state)
242{
243 /* Power up pin widget */
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530244 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->pin->nid,
245 pwr_state))
246 snd_hdac_codec_write(&edev->hdac, dai_map->pin->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530247 AC_VERB_SET_POWER_STATE, pwr_state);
248
249 /* Power up converter */
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530250 if (!snd_hdac_check_power_state(&edev->hdac, dai_map->cvt->nid,
251 pwr_state))
252 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530253 AC_VERB_SET_POWER_STATE, pwr_state);
254}
255
256static int hdac_hdmi_playback_prepare(struct snd_pcm_substream *substream,
257 struct snd_soc_dai *dai)
258{
259 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
260 struct hdac_hdmi_priv *hdmi = hdac->private_data;
261 struct hdac_hdmi_dai_pin_map *dai_map;
262 struct hdac_ext_dma_params *dd;
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530263 int ret;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530264
265 if (dai->id > 0) {
266 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
267 return -ENODEV;
268 }
269
270 dai_map = &hdmi->dai_map[dai->id];
271
272 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
273 dev_dbg(&hdac->hdac.dev, "stream tag from cpu dai %d format in cvt 0x%x\n",
274 dd->stream_tag, dd->format);
275
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530276 ret = hdac_hdmi_setup_audio_infoframe(hdac, dai_map->cvt->nid,
277 dai_map->pin->nid);
Subhransu S. Prustya657f1d2015-11-10 18:42:09 +0530278 if (ret < 0)
279 return ret;
280
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530281 return hdac_hdmi_setup_stream(hdac, dai_map->cvt->nid,
282 dai_map->pin->nid, dd->stream_tag, dd->format);
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530283}
284
285static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
286 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
287{
288 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
289 struct hdac_ext_dma_params *dd;
290
291 if (dai->id > 0) {
292 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
293 return -ENODEV;
294 }
295
296 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
Sudip Mukherjee8d33ab22015-11-23 17:45:13 +0530297 if (!dd)
298 return -ENOMEM;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530299 dd->format = snd_hdac_calc_stream_format(params_rate(hparams),
300 params_channels(hparams), params_format(hparams),
301 24, 0);
302
303 snd_soc_dai_set_dma_data(dai, substream, (void *)dd);
304
305 return 0;
306}
307
308static int hdac_hdmi_playback_cleanup(struct snd_pcm_substream *substream,
309 struct snd_soc_dai *dai)
310{
311 struct hdac_ext_device *edev = snd_soc_dai_get_drvdata(dai);
312 struct hdac_ext_dma_params *dd;
313 struct hdac_hdmi_priv *hdmi = edev->private_data;
314 struct hdac_hdmi_dai_pin_map *dai_map;
315
316 dai_map = &hdmi->dai_map[dai->id];
317
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530318 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530319 AC_VERB_SET_CHANNEL_STREAMID, 0);
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530320 snd_hdac_codec_write(&edev->hdac, dai_map->cvt->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530321 AC_VERB_SET_STREAM_FORMAT, 0);
322
323 dd = (struct hdac_ext_dma_params *)snd_soc_dai_get_dma_data(dai, substream);
324 snd_soc_dai_set_dma_data(dai, substream, NULL);
325
326 kfree(dd);
327
328 return 0;
329}
330
331static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
332 struct snd_soc_dai *dai)
333{
334 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
335 struct hdac_hdmi_priv *hdmi = hdac->private_data;
336 struct hdac_hdmi_dai_pin_map *dai_map;
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530337
338 if (dai->id > 0) {
339 dev_err(&hdac->hdac.dev, "Only one dai supported as of now\n");
340 return -ENODEV;
341 }
342
343 dai_map = &hdmi->dai_map[dai->id];
344
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530345 if ((!dai_map->pin->eld.monitor_present) ||
346 (!dai_map->pin->eld.eld_valid)) {
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530347
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530348 dev_err(&hdac->hdac.dev,
349 "Failed: montior present? %d ELD valid?: %d\n",
350 dai_map->pin->eld.monitor_present,
351 dai_map->pin->eld.eld_valid);
352
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530353 return -ENODEV;
354 }
355
356 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D0);
357
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530358 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530359 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
360
361 snd_pcm_hw_constraint_step(substream->runtime, 0,
362 SNDRV_PCM_HW_PARAM_CHANNELS, 2);
363
364 return 0;
365}
366
367static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
368 struct snd_soc_dai *dai)
369{
370 struct hdac_ext_device *hdac = snd_soc_dai_get_drvdata(dai);
371 struct hdac_hdmi_priv *hdmi = hdac->private_data;
372 struct hdac_hdmi_dai_pin_map *dai_map;
373
374 dai_map = &hdmi->dai_map[dai->id];
375
376 hdac_hdmi_set_power_state(hdac, dai_map, AC_PWRST_D3);
377
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530378 snd_hdac_codec_write(&hdac->hdac, dai_map->pin->nid, 0,
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530379 AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
380}
381
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530382static int
383hdac_hdmi_query_cvt_params(struct hdac_device *hdac, struct hdac_hdmi_cvt *cvt)
384{
385 int err;
386
387 /* Only stereo supported as of now */
388 cvt->params.channels_min = cvt->params.channels_max = 2;
389
390 err = snd_hdac_query_supported_pcm(hdac, cvt->nid,
391 &cvt->params.rates,
392 &cvt->params.formats,
393 &cvt->params.maxbps);
394 if (err < 0)
395 dev_err(&hdac->dev,
396 "Failed to query pcm params for nid %d: %d\n",
397 cvt->nid, err);
398
399 return err;
400}
401
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530402static void hdac_hdmi_fill_widget_info(struct snd_soc_dapm_widget *w,
403 enum snd_soc_dapm_type id,
404 const char *wname, const char *stream)
405{
406 w->id = id;
407 w->name = wname;
408 w->sname = stream;
409 w->reg = SND_SOC_NOPM;
410 w->shift = 0;
411 w->kcontrol_news = NULL;
412 w->num_kcontrols = 0;
413 w->priv = NULL;
414}
415
416static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
417 const char *sink, const char *control, const char *src)
418{
419 route->sink = sink;
420 route->source = src;
421 route->control = control;
422 route->connected = NULL;
423}
424
425static void create_fill_widget_route_map(struct snd_soc_dapm_context *dapm,
426 struct hdac_hdmi_dai_pin_map *dai_map)
427{
428 struct snd_soc_dapm_route route[1];
429 struct snd_soc_dapm_widget widgets[2] = { {0} };
430
431 memset(&route, 0, sizeof(route));
432
433 hdac_hdmi_fill_widget_info(&widgets[0], snd_soc_dapm_output,
434 "hif1 Output", NULL);
435 hdac_hdmi_fill_widget_info(&widgets[1], snd_soc_dapm_aif_in,
436 "Coverter 1", "hif1");
437
438 hdac_hdmi_fill_route(&route[0], "hif1 Output", NULL, "Coverter 1");
439
440 snd_soc_dapm_new_controls(dapm, widgets, ARRAY_SIZE(widgets));
441 snd_soc_dapm_add_routes(dapm, route, ARRAY_SIZE(route));
442}
443
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530444static int hdac_hdmi_init_dai_map(struct hdac_ext_device *edev)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530445{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530446 struct hdac_hdmi_priv *hdmi = edev->private_data;
447 struct hdac_hdmi_dai_pin_map *dai_map = &hdmi->dai_map[0];
448 struct hdac_hdmi_cvt *cvt;
449 struct hdac_hdmi_pin *pin;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530450
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530451 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
452 return -EINVAL;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530453
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530454 /*
455 * Currently on board only 1 pin and 1 converter is enabled for
456 * simplification, more will be added eventually
457 * So using fixed map for dai_id:pin:cvt
458 */
459 cvt = list_first_entry(&hdmi->cvt_list, struct hdac_hdmi_cvt, head);
460 pin = list_first_entry(&hdmi->pin_list, struct hdac_hdmi_pin, head);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530461
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530462 dai_map->dai_id = 0;
463 dai_map->pin = pin;
464
465 dai_map->cvt = cvt;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530466
467 /* Enable out path for this pin widget */
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530468 snd_hdac_codec_write(&edev->hdac, pin->nid, 0,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530469 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
470
471 /* Enable transmission */
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530472 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530473 AC_VERB_SET_DIGI_CONVERT_1, 1);
474
475 /* Category Code (CC) to zero */
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530476 snd_hdac_codec_write(&edev->hdac, cvt->nid, 0,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530477 AC_VERB_SET_DIGI_CONVERT_2, 0);
478
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530479 snd_hdac_codec_write(&edev->hdac, pin->nid, 0,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530480 AC_VERB_SET_CONNECT_SEL, 0);
481
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530482 return 0;
483}
484
485static int hdac_hdmi_add_cvt(struct hdac_ext_device *edev, hda_nid_t nid)
486{
487 struct hdac_hdmi_priv *hdmi = edev->private_data;
488 struct hdac_hdmi_cvt *cvt;
489
490 cvt = kzalloc(sizeof(*cvt), GFP_KERNEL);
491 if (!cvt)
492 return -ENOMEM;
493
494 cvt->nid = nid;
495
496 list_add_tail(&cvt->head, &hdmi->cvt_list);
497 hdmi->num_cvt++;
498
499 return hdac_hdmi_query_cvt_params(&edev->hdac, cvt);
500}
501
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530502static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin, int repoll)
503{
504 struct hdac_ext_device *edev = pin->edev;
505 int val;
506
507 if (!edev)
508 return;
509
510 pin->repoll_count = repoll;
511
512 pm_runtime_get_sync(&edev->hdac.dev);
513 val = snd_hdac_codec_read(&edev->hdac, pin->nid, 0,
514 AC_VERB_GET_PIN_SENSE, 0);
515
516 dev_dbg(&edev->hdac.dev, "Pin sense val %x for pin: %d\n",
517 val, pin->nid);
518
519 pin->eld.monitor_present = !!(val & AC_PINSENSE_PRESENCE);
520 pin->eld.eld_valid = !!(val & AC_PINSENSE_ELDV);
521
522 if (!pin->eld.monitor_present || !pin->eld.eld_valid) {
523
524 dev_dbg(&edev->hdac.dev, "%s: disconnect for pin %d\n",
525 __func__, pin->nid);
526 goto put_hdac_device;
527 }
528
529 if (pin->eld.monitor_present && pin->eld.eld_valid) {
530 /* TODO: use i915 component for reading ELD later */
531 if (hdac_hdmi_get_eld(&edev->hdac, pin->nid,
532 pin->eld.eld_buffer,
533 &pin->eld.eld_size) == 0) {
534
535 print_hex_dump_bytes("ELD: ", DUMP_PREFIX_OFFSET,
536 pin->eld.eld_buffer, pin->eld.eld_size);
537 } else {
538 pin->eld.monitor_present = false;
539 pin->eld.eld_valid = false;
540 }
541 }
542
543 /*
544 * Sometimes the pin_sense may present invalid monitor
545 * present and eld_valid. If ELD data is not valid, loop few
546 * more times to get correct pin sense and valid ELD.
547 */
548 if ((!pin->eld.monitor_present || !pin->eld.eld_valid) && repoll)
549 schedule_delayed_work(&pin->work, msecs_to_jiffies(300));
550
551put_hdac_device:
552 pm_runtime_put_sync(&edev->hdac.dev);
553}
554
555static void hdac_hdmi_repoll_eld(struct work_struct *work)
556{
557 struct hdac_hdmi_pin *pin =
558 container_of(to_delayed_work(work), struct hdac_hdmi_pin, work);
559
560 /* picked from legacy HDA driver */
561 if (pin->repoll_count++ > 6)
562 pin->repoll_count = 0;
563
564 hdac_hdmi_present_sense(pin, pin->repoll_count);
565}
566
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530567static int hdac_hdmi_add_pin(struct hdac_ext_device *edev, hda_nid_t nid)
568{
569 struct hdac_hdmi_priv *hdmi = edev->private_data;
570 struct hdac_hdmi_pin *pin;
571
572 pin = kzalloc(sizeof(*pin), GFP_KERNEL);
573 if (!pin)
574 return -ENOMEM;
575
576 pin->nid = nid;
577
578 list_add_tail(&pin->head, &hdmi->pin_list);
579 hdmi->num_pin++;
580
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530581 pin->edev = edev;
582 INIT_DELAYED_WORK(&pin->work, hdac_hdmi_repoll_eld);
583
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530584 return 0;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530585}
586
587/*
588 * Parse all nodes and store the cvt/pin nids in array
589 * Add one time initialization for pin and cvt widgets
590 */
591static int hdac_hdmi_parse_and_map_nid(struct hdac_ext_device *edev)
592{
593 hda_nid_t nid;
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +0530594 int i, num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530595 struct hdac_device *hdac = &edev->hdac;
596 struct hdac_hdmi_priv *hdmi = edev->private_data;
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530597 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530598
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +0530599 num_nodes = snd_hdac_get_sub_nodes(hdac, hdac->afg, &nid);
Subhransu S. Prusty541140d2015-12-09 21:46:08 +0530600 if (!nid || num_nodes <= 0) {
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530601 dev_warn(&hdac->dev, "HDMI: failed to get afg sub nodes\n");
602 return -EINVAL;
603 }
604
Sudip Mukherjee3c83ac22015-12-01 14:29:35 +0530605 hdac->num_nodes = num_nodes;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530606 hdac->start_nid = nid;
607
608 for (i = 0; i < hdac->num_nodes; i++, nid++) {
609 unsigned int caps;
610 unsigned int type;
611
612 caps = get_wcaps(hdac, nid);
613 type = get_wcaps_type(caps);
614
615 if (!(caps & AC_WCAP_DIGITAL))
616 continue;
617
618 switch (type) {
619
620 case AC_WID_AUD_OUT:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530621 ret = hdac_hdmi_add_cvt(edev, nid);
622 if (ret < 0)
623 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530624 break;
625
626 case AC_WID_PIN:
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530627 ret = hdac_hdmi_add_pin(edev, nid);
628 if (ret < 0)
629 return ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530630 break;
631 }
632 }
633
634 hdac->end_nid = nid;
635
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530636 if (!hdmi->num_pin || !hdmi->num_cvt)
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530637 return -EIO;
638
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530639 return hdac_hdmi_init_dai_map(edev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530640}
641
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530642static void hdac_hdmi_eld_notify_cb(void *aptr, int port)
643{
644 struct hdac_ext_device *edev = aptr;
645 struct hdac_hdmi_priv *hdmi = edev->private_data;
646 struct hdac_hdmi_pin *pin;
647 struct snd_soc_codec *codec = edev->scodec;
648
649 /* Don't know how this mapping is derived */
650 hda_nid_t pin_nid = port + 0x04;
651
652 dev_dbg(&edev->hdac.dev, "%s: for pin: %d\n", __func__, pin_nid);
653
654 /*
655 * skip notification during system suspend (but not in runtime PM);
656 * the state will be updated at resume. Also since the ELD and
657 * connection states are updated in anyway at the end of the resume,
658 * we can skip it when received during PM process.
659 */
660 if (snd_power_get_state(codec->component.card->snd_card) !=
661 SNDRV_CTL_POWER_D0)
662 return;
663
664 if (atomic_read(&edev->hdac.in_pm))
665 return;
666
667 list_for_each_entry(pin, &hdmi->pin_list, head) {
668 if (pin->nid == pin_nid)
669 hdac_hdmi_present_sense(pin, 1);
670 }
671}
672
673static struct i915_audio_component_audio_ops aops = {
674 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
675};
676
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530677static int hdmi_codec_probe(struct snd_soc_codec *codec)
678{
679 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
680 struct hdac_hdmi_priv *hdmi = edev->private_data;
681 struct snd_soc_dapm_context *dapm =
682 snd_soc_component_get_dapm(&codec->component);
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530683 struct hdac_hdmi_pin *pin;
684 int ret;
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530685
686 edev->scodec = codec;
687
688 create_fill_widget_route_map(dapm, &hdmi->dai_map[0]);
689
Subhransu S. Prustyb8a54542016-02-12 07:46:01 +0530690 aops.audio_ptr = edev;
691 ret = snd_hdac_i915_register_notifier(&aops);
692 if (ret < 0) {
693 dev_err(&edev->hdac.dev, "notifier register failed: err: %d\n",
694 ret);
695 return ret;
696 }
697
698 list_for_each_entry(pin, &hdmi->pin_list, head)
699 hdac_hdmi_present_sense(pin, 1);
700
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530701 /* Imp: Store the card pointer in hda_codec */
702 edev->card = dapm->card->snd_card;
703
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530704 /*
705 * hdac_device core already sets the state to active and calls
706 * get_noresume. So enable runtime and set the device to suspend.
707 */
708 pm_runtime_enable(&edev->hdac.dev);
709 pm_runtime_put(&edev->hdac.dev);
710 pm_runtime_suspend(&edev->hdac.dev);
711
712 return 0;
713}
714
715static int hdmi_codec_remove(struct snd_soc_codec *codec)
716{
717 struct hdac_ext_device *edev = snd_soc_codec_get_drvdata(codec);
718
719 pm_runtime_disable(&edev->hdac.dev);
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530720 return 0;
721}
722
723static struct snd_soc_codec_driver hdmi_hda_codec = {
724 .probe = hdmi_codec_probe,
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530725 .remove = hdmi_codec_remove,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530726 .idle_bias_off = true,
727};
728
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530729static struct snd_soc_dai_ops hdmi_dai_ops = {
730 .startup = hdac_hdmi_pcm_open,
731 .shutdown = hdac_hdmi_pcm_close,
732 .hw_params = hdac_hdmi_set_hw_params,
733 .prepare = hdac_hdmi_playback_prepare,
734 .hw_free = hdac_hdmi_playback_cleanup,
735};
736
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530737static struct snd_soc_dai_driver hdmi_dais[] = {
738 { .name = "intel-hdmi-hif1",
739 .playback = {
740 .stream_name = "hif1",
741 .channels_min = 2,
742 .channels_max = 2,
743 .rates = SNDRV_PCM_RATE_32000 |
744 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
745 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
746 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
747 .formats = SNDRV_PCM_FMTBIT_S16_LE |
748 SNDRV_PCM_FMTBIT_S20_3LE |
749 SNDRV_PCM_FMTBIT_S24_LE |
750 SNDRV_PCM_FMTBIT_S32_LE,
751
752 },
Subhransu S. Prustyb0362ad2015-11-10 18:42:08 +0530753 .ops = &hdmi_dai_ops,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530754 },
755};
756
757static int hdac_hdmi_dev_probe(struct hdac_ext_device *edev)
758{
759 struct hdac_device *codec = &edev->hdac;
760 struct hdac_hdmi_priv *hdmi_priv;
761 int ret = 0;
762
763 hdmi_priv = devm_kzalloc(&codec->dev, sizeof(*hdmi_priv), GFP_KERNEL);
764 if (hdmi_priv == NULL)
765 return -ENOMEM;
766
767 edev->private_data = hdmi_priv;
768
769 dev_set_drvdata(&codec->dev, edev);
770
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530771 INIT_LIST_HEAD(&hdmi_priv->pin_list);
772 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
773
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530774 ret = hdac_hdmi_parse_and_map_nid(edev);
775 if (ret < 0)
776 return ret;
777
778 /* ASoC specific initialization */
779 return snd_soc_register_codec(&codec->dev, &hdmi_hda_codec,
780 hdmi_dais, ARRAY_SIZE(hdmi_dais));
781}
782
783static int hdac_hdmi_dev_remove(struct hdac_ext_device *edev)
784{
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530785 struct hdac_hdmi_priv *hdmi = edev->private_data;
786 struct hdac_hdmi_pin *pin, *pin_next;
787 struct hdac_hdmi_cvt *cvt, *cvt_next;
788
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530789 snd_soc_unregister_codec(&edev->hdac.dev);
790
Subhransu S. Prusty15b91442015-12-09 21:46:10 +0530791 list_for_each_entry_safe(cvt, cvt_next, &hdmi->cvt_list, head) {
792 list_del(&cvt->head);
793 kfree(cvt);
794 }
795
796 list_for_each_entry_safe(pin, pin_next, &hdmi->pin_list, head) {
797 list_del(&pin->head);
798 kfree(pin);
799 }
800
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530801 return 0;
802}
803
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530804#ifdef CONFIG_PM
805static int hdac_hdmi_runtime_suspend(struct device *dev)
806{
807 struct hdac_ext_device *edev = to_hda_ext_device(dev);
808 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +0530809 struct hdac_bus *bus = hdac->bus;
810 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530811
812 dev_dbg(dev, "Enter: %s\n", __func__);
813
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +0530814 /* controller may not have been initialized for the first time */
815 if (!bus)
816 return 0;
817
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530818 /* Power down afg */
819 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D3))
820 snd_hdac_codec_write(hdac, hdac->afg, 0,
821 AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
822
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +0530823 err = snd_hdac_display_power(bus, false);
824 if (err < 0) {
825 dev_err(bus->dev, "Cannot turn on display power on i915\n");
826 return err;
827 }
828
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530829 return 0;
830}
831
832static int hdac_hdmi_runtime_resume(struct device *dev)
833{
834 struct hdac_ext_device *edev = to_hda_ext_device(dev);
835 struct hdac_device *hdac = &edev->hdac;
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +0530836 struct hdac_bus *bus = hdac->bus;
837 int err;
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530838
839 dev_dbg(dev, "Enter: %s\n", __func__);
840
Subhransu S. Prusty07f083a2015-11-10 18:42:10 +0530841 /* controller may not have been initialized for the first time */
842 if (!bus)
843 return 0;
844
845 err = snd_hdac_display_power(bus, true);
846 if (err < 0) {
847 dev_err(bus->dev, "Cannot turn on display power on i915\n");
848 return err;
849 }
850
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530851 /* Power up afg */
852 if (!snd_hdac_check_power_state(hdac, hdac->afg, AC_PWRST_D0))
853 snd_hdac_codec_write(hdac, hdac->afg, 0,
854 AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
855
856 return 0;
857}
858#else
859#define hdac_hdmi_runtime_suspend NULL
860#define hdac_hdmi_runtime_resume NULL
861#endif
862
863static const struct dev_pm_ops hdac_hdmi_pm = {
864 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
865};
866
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530867static const struct hda_device_id hdmi_list[] = {
868 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
869 {}
870};
871
872MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
873
874static struct hdac_ext_driver hdmi_driver = {
875 . hdac = {
876 .driver = {
877 .name = "HDMI HDA Codec",
Subhransu S. Prustye342ac02015-11-10 18:42:07 +0530878 .pm = &hdac_hdmi_pm,
Subhransu S. Prusty18382ea2015-11-10 18:42:06 +0530879 },
880 .id_table = hdmi_list,
881 },
882 .probe = hdac_hdmi_dev_probe,
883 .remove = hdac_hdmi_dev_remove,
884};
885
886static int __init hdmi_init(void)
887{
888 return snd_hda_ext_driver_register(&hdmi_driver);
889}
890
891static void __exit hdmi_exit(void)
892{
893 snd_hda_ext_driver_unregister(&hdmi_driver);
894}
895
896module_init(hdmi_init);
897module_exit(hdmi_exit);
898
899MODULE_LICENSE("GPL v2");
900MODULE_DESCRIPTION("HDMI HD codec");
901MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
902MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");