0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/init.h>
0014 #include <linux/delay.h>
0015 #include <linux/module.h>
0016 #include <linux/pm_runtime.h>
0017 #include <linux/hdmi.h>
0018 #include <drm/drm_edid.h>
0019 #include <sound/pcm_params.h>
0020 #include <sound/jack.h>
0021 #include <sound/soc.h>
0022 #include <sound/hdaudio_ext.h>
0023 #include <sound/hda_i915.h>
0024 #include <sound/pcm_drm_eld.h>
0025 #include <sound/hda_chmap.h>
0026 #include "../../hda/local.h"
0027 #include "hdac_hdmi.h"
0028
0029 #define NAME_SIZE 32
0030
0031 #define AMP_OUT_MUTE 0xb080
0032 #define AMP_OUT_UNMUTE 0xb000
0033 #define PIN_OUT (AC_PINCTL_OUT_EN)
0034
0035 #define HDA_MAX_CONNECTIONS 32
0036
0037 #define HDA_MAX_CVTS 3
0038 #define HDA_MAX_PORTS 3
0039
0040 #define ELD_MAX_SIZE 256
0041 #define ELD_FIXED_BYTES 20
0042
0043 #define ELD_VER_CEA_861D 2
0044 #define ELD_VER_PARTIAL 31
0045 #define ELD_MAX_MNL 16
0046
0047 struct hdac_hdmi_cvt_params {
0048 unsigned int channels_min;
0049 unsigned int channels_max;
0050 u32 rates;
0051 u64 formats;
0052 unsigned int maxbps;
0053 };
0054
0055 struct hdac_hdmi_cvt {
0056 struct list_head head;
0057 hda_nid_t nid;
0058 const char *name;
0059 struct hdac_hdmi_cvt_params params;
0060 };
0061
0062
0063 struct hdac_hdmi_parsed_eld {
0064 u8 spk_alloc;
0065 };
0066
0067 struct hdac_hdmi_eld {
0068 bool monitor_present;
0069 bool eld_valid;
0070 int eld_size;
0071 char eld_buffer[ELD_MAX_SIZE];
0072 struct hdac_hdmi_parsed_eld info;
0073 };
0074
0075 struct hdac_hdmi_pin {
0076 struct list_head head;
0077 hda_nid_t nid;
0078 bool mst_capable;
0079 struct hdac_hdmi_port *ports;
0080 int num_ports;
0081 struct hdac_device *hdev;
0082 };
0083
0084 struct hdac_hdmi_port {
0085 struct list_head head;
0086 int id;
0087 struct hdac_hdmi_pin *pin;
0088 int num_mux_nids;
0089 hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
0090 struct hdac_hdmi_eld eld;
0091 const char *jack_pin;
0092 bool is_connect;
0093 struct snd_soc_dapm_context *dapm;
0094 const char *output_pin;
0095 struct work_struct dapm_work;
0096 };
0097
0098 struct hdac_hdmi_pcm {
0099 struct list_head head;
0100 int pcm_id;
0101 struct list_head port_list;
0102 struct hdac_hdmi_cvt *cvt;
0103 struct snd_soc_jack *jack;
0104 int stream_tag;
0105 int channels;
0106 int format;
0107 bool chmap_set;
0108 unsigned char chmap[8];
0109 struct mutex lock;
0110 int jack_event;
0111 struct snd_kcontrol *eld_ctl;
0112 };
0113
0114 struct hdac_hdmi_dai_port_map {
0115 int dai_id;
0116 struct hdac_hdmi_port *port;
0117 struct hdac_hdmi_cvt *cvt;
0118 };
0119
0120 struct hdac_hdmi_drv_data {
0121 unsigned int vendor_nid;
0122 };
0123
0124 struct hdac_hdmi_priv {
0125 struct hdac_device *hdev;
0126 struct snd_soc_component *component;
0127 struct snd_card *card;
0128 struct hdac_hdmi_dai_port_map dai_map[HDA_MAX_CVTS];
0129 struct list_head pin_list;
0130 struct list_head cvt_list;
0131 struct list_head pcm_list;
0132 int num_pin;
0133 int num_cvt;
0134 int num_ports;
0135 struct mutex pin_mutex;
0136 struct hdac_chmap chmap;
0137 struct hdac_hdmi_drv_data *drv_data;
0138 struct snd_soc_dai_driver *dai_drv;
0139 };
0140
0141 #define hdev_to_hdmi_priv(_hdev) dev_get_drvdata(&(_hdev)->dev)
0142
0143 static struct hdac_hdmi_pcm *
0144 hdac_hdmi_get_pcm_from_cvt(struct hdac_hdmi_priv *hdmi,
0145 struct hdac_hdmi_cvt *cvt)
0146 {
0147 struct hdac_hdmi_pcm *pcm;
0148
0149 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
0150 if (pcm->cvt == cvt)
0151 return pcm;
0152 }
0153
0154 return NULL;
0155 }
0156
0157 static void hdac_hdmi_jack_report(struct hdac_hdmi_pcm *pcm,
0158 struct hdac_hdmi_port *port, bool is_connect)
0159 {
0160 struct hdac_device *hdev = port->pin->hdev;
0161
0162 port->is_connect = is_connect;
0163 if (is_connect) {
0164
0165
0166
0167
0168
0169 if (pcm->jack_event == 0) {
0170 dev_dbg(&hdev->dev,
0171 "jack report for pcm=%d\n",
0172 pcm->pcm_id);
0173 snd_soc_jack_report(pcm->jack, SND_JACK_AVOUT,
0174 SND_JACK_AVOUT);
0175 }
0176 pcm->jack_event++;
0177 } else {
0178
0179
0180
0181
0182
0183 if (pcm->jack_event == 1)
0184 snd_soc_jack_report(pcm->jack, 0, SND_JACK_AVOUT);
0185 if (pcm->jack_event > 0)
0186 pcm->jack_event--;
0187 }
0188 }
0189
0190 static void hdac_hdmi_port_dapm_update(struct hdac_hdmi_port *port)
0191 {
0192 if (port->is_connect)
0193 snd_soc_dapm_enable_pin(port->dapm, port->jack_pin);
0194 else
0195 snd_soc_dapm_disable_pin(port->dapm, port->jack_pin);
0196 snd_soc_dapm_sync(port->dapm);
0197 }
0198
0199 static void hdac_hdmi_jack_dapm_work(struct work_struct *work)
0200 {
0201 struct hdac_hdmi_port *port;
0202
0203 port = container_of(work, struct hdac_hdmi_port, dapm_work);
0204 hdac_hdmi_port_dapm_update(port);
0205 }
0206
0207 static void hdac_hdmi_jack_report_sync(struct hdac_hdmi_pcm *pcm,
0208 struct hdac_hdmi_port *port, bool is_connect)
0209 {
0210 hdac_hdmi_jack_report(pcm, port, is_connect);
0211 hdac_hdmi_port_dapm_update(port);
0212 }
0213
0214
0215
0216
0217
0218 static int hdac_hdmi_get_port_len(struct hdac_device *hdev, hda_nid_t nid)
0219 {
0220 unsigned int caps;
0221 unsigned int type, param;
0222
0223 caps = get_wcaps(hdev, nid);
0224 type = get_wcaps_type(caps);
0225
0226 if (!(caps & AC_WCAP_DIGITAL) || (type != AC_WID_PIN))
0227 return 0;
0228
0229 param = snd_hdac_read_parm_uncached(hdev, nid, AC_PAR_DEVLIST_LEN);
0230 if (param == -1)
0231 return param;
0232
0233 return param & AC_DEV_LIST_LEN_MASK;
0234 }
0235
0236
0237
0238
0239
0240
0241 static int hdac_hdmi_port_select_get(struct hdac_device *hdev,
0242 struct hdac_hdmi_port *port)
0243 {
0244 return snd_hdac_codec_read(hdev, port->pin->nid,
0245 0, AC_VERB_GET_DEVICE_SEL, 0);
0246 }
0247
0248
0249
0250
0251
0252 static int hdac_hdmi_port_select_set(struct hdac_device *hdev,
0253 struct hdac_hdmi_port *port)
0254 {
0255 int num_ports;
0256
0257 if (!port->pin->mst_capable)
0258 return 0;
0259
0260
0261 num_ports = hdac_hdmi_get_port_len(hdev, port->pin->nid);
0262 if (num_ports < 0)
0263 return -EIO;
0264
0265
0266
0267
0268 if (num_ports + 1 < port->id)
0269 return 0;
0270
0271 snd_hdac_codec_write(hdev, port->pin->nid, 0,
0272 AC_VERB_SET_DEVICE_SEL, port->id);
0273
0274 if (port->id != hdac_hdmi_port_select_get(hdev, port))
0275 return -EIO;
0276
0277 dev_dbg(&hdev->dev, "Selected the port=%d\n", port->id);
0278
0279 return 0;
0280 }
0281
0282 static struct hdac_hdmi_pcm *get_hdmi_pcm_from_id(struct hdac_hdmi_priv *hdmi,
0283 int pcm_idx)
0284 {
0285 struct hdac_hdmi_pcm *pcm;
0286
0287 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
0288 if (pcm->pcm_id == pcm_idx)
0289 return pcm;
0290 }
0291
0292 return NULL;
0293 }
0294
0295 static unsigned int sad_format(const u8 *sad)
0296 {
0297 return ((sad[0] >> 0x3) & 0x1f);
0298 }
0299
0300 static unsigned int sad_sample_bits_lpcm(const u8 *sad)
0301 {
0302 return (sad[2] & 7);
0303 }
0304
0305 static int hdac_hdmi_eld_limit_formats(struct snd_pcm_runtime *runtime,
0306 void *eld)
0307 {
0308 u64 formats = SNDRV_PCM_FMTBIT_S16;
0309 int i;
0310 const u8 *sad, *eld_buf = eld;
0311
0312 sad = drm_eld_sad(eld_buf);
0313 if (!sad)
0314 goto format_constraint;
0315
0316 for (i = drm_eld_sad_count(eld_buf); i > 0; i--, sad += 3) {
0317 if (sad_format(sad) == 1) {
0318
0319
0320
0321
0322
0323 if (sad_sample_bits_lpcm(sad) & 0x6)
0324 formats |= SNDRV_PCM_FMTBIT_S32;
0325 }
0326 }
0327
0328 format_constraint:
0329 return snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
0330 formats);
0331
0332 }
0333
0334 static void
0335 hdac_hdmi_set_dip_index(struct hdac_device *hdev, hda_nid_t pin_nid,
0336 int packet_index, int byte_index)
0337 {
0338 int val;
0339
0340 val = (packet_index << 5) | (byte_index & 0x1f);
0341 snd_hdac_codec_write(hdev, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
0342 }
0343
0344 struct dp_audio_infoframe {
0345 u8 type;
0346 u8 len;
0347 u8 ver;
0348
0349 u8 CC02_CT47;
0350 u8 SS01_SF24;
0351 u8 CXT04;
0352 u8 CA;
0353 u8 LFEPBL01_LSV36_DM_INH7;
0354 };
0355
0356 static int hdac_hdmi_setup_audio_infoframe(struct hdac_device *hdev,
0357 struct hdac_hdmi_pcm *pcm, struct hdac_hdmi_port *port)
0358 {
0359 uint8_t buffer[HDMI_INFOFRAME_HEADER_SIZE + HDMI_AUDIO_INFOFRAME_SIZE];
0360 struct hdmi_audio_infoframe frame;
0361 struct hdac_hdmi_pin *pin = port->pin;
0362 struct dp_audio_infoframe dp_ai;
0363 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
0364 struct hdac_hdmi_cvt *cvt = pcm->cvt;
0365 u8 *dip;
0366 int ret;
0367 int i;
0368 const u8 *eld_buf;
0369 u8 conn_type;
0370 int channels, ca;
0371
0372 ca = snd_hdac_channel_allocation(hdev, port->eld.info.spk_alloc,
0373 pcm->channels, pcm->chmap_set, true, pcm->chmap);
0374
0375 channels = snd_hdac_get_active_channels(ca);
0376 hdmi->chmap.ops.set_channel_count(hdev, cvt->nid, channels);
0377
0378 snd_hdac_setup_channel_mapping(&hdmi->chmap, pin->nid, false, ca,
0379 pcm->channels, pcm->chmap, pcm->chmap_set);
0380
0381 eld_buf = port->eld.eld_buffer;
0382 conn_type = drm_eld_get_conn_type(eld_buf);
0383
0384 switch (conn_type) {
0385 case DRM_ELD_CONN_TYPE_HDMI:
0386 hdmi_audio_infoframe_init(&frame);
0387
0388 frame.channels = channels;
0389 frame.channel_allocation = ca;
0390
0391 ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer));
0392 if (ret < 0)
0393 return ret;
0394
0395 break;
0396
0397 case DRM_ELD_CONN_TYPE_DP:
0398 memset(&dp_ai, 0, sizeof(dp_ai));
0399 dp_ai.type = 0x84;
0400 dp_ai.len = 0x1b;
0401 dp_ai.ver = 0x11 << 2;
0402 dp_ai.CC02_CT47 = channels - 1;
0403 dp_ai.CA = ca;
0404
0405 dip = (u8 *)&dp_ai;
0406 break;
0407
0408 default:
0409 dev_err(&hdev->dev, "Invalid connection type: %d\n", conn_type);
0410 return -EIO;
0411 }
0412
0413
0414 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
0415 snd_hdac_codec_write(hdev, pin->nid, 0,
0416 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_DISABLE);
0417
0418
0419
0420 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
0421 if (conn_type == DRM_ELD_CONN_TYPE_HDMI) {
0422 for (i = 0; i < sizeof(buffer); i++)
0423 snd_hdac_codec_write(hdev, pin->nid, 0,
0424 AC_VERB_SET_HDMI_DIP_DATA, buffer[i]);
0425 } else {
0426 for (i = 0; i < sizeof(dp_ai); i++)
0427 snd_hdac_codec_write(hdev, pin->nid, 0,
0428 AC_VERB_SET_HDMI_DIP_DATA, dip[i]);
0429 }
0430
0431
0432 hdac_hdmi_set_dip_index(hdev, pin->nid, 0x0, 0x0);
0433 snd_hdac_codec_write(hdev, pin->nid, 0,
0434 AC_VERB_SET_HDMI_DIP_XMIT, AC_DIPXMIT_BEST);
0435
0436 return 0;
0437 }
0438
0439 static int hdac_hdmi_set_tdm_slot(struct snd_soc_dai *dai,
0440 unsigned int tx_mask, unsigned int rx_mask,
0441 int slots, int slot_width)
0442 {
0443 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
0444 struct hdac_device *hdev = hdmi->hdev;
0445 struct hdac_hdmi_dai_port_map *dai_map;
0446 struct hdac_hdmi_pcm *pcm;
0447
0448 dev_dbg(&hdev->dev, "%s: strm_tag: %d\n", __func__, tx_mask);
0449
0450 dai_map = &hdmi->dai_map[dai->id];
0451
0452 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
0453
0454 if (pcm)
0455 pcm->stream_tag = (tx_mask << 4);
0456
0457 return 0;
0458 }
0459
0460 static int hdac_hdmi_set_hw_params(struct snd_pcm_substream *substream,
0461 struct snd_pcm_hw_params *hparams, struct snd_soc_dai *dai)
0462 {
0463 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
0464 struct hdac_hdmi_dai_port_map *dai_map;
0465 struct hdac_hdmi_pcm *pcm;
0466 int format;
0467
0468 dai_map = &hdmi->dai_map[dai->id];
0469
0470 format = snd_hdac_calc_stream_format(params_rate(hparams),
0471 params_channels(hparams), params_format(hparams),
0472 dai->driver->playback.sig_bits, 0);
0473
0474 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
0475 if (!pcm)
0476 return -EIO;
0477
0478 pcm->format = format;
0479 pcm->channels = params_channels(hparams);
0480
0481 return 0;
0482 }
0483
0484 static int hdac_hdmi_query_port_connlist(struct hdac_device *hdev,
0485 struct hdac_hdmi_pin *pin,
0486 struct hdac_hdmi_port *port)
0487 {
0488 if (!(get_wcaps(hdev, pin->nid) & AC_WCAP_CONN_LIST)) {
0489 dev_warn(&hdev->dev,
0490 "HDMI: pin %d wcaps %#x does not support connection list\n",
0491 pin->nid, get_wcaps(hdev, pin->nid));
0492 return -EINVAL;
0493 }
0494
0495 if (hdac_hdmi_port_select_set(hdev, port) < 0)
0496 return -EIO;
0497
0498 port->num_mux_nids = snd_hdac_get_connections(hdev, pin->nid,
0499 port->mux_nids, HDA_MAX_CONNECTIONS);
0500 if (port->num_mux_nids == 0)
0501 dev_warn(&hdev->dev,
0502 "No connections found for pin:port %d:%d\n",
0503 pin->nid, port->id);
0504
0505 dev_dbg(&hdev->dev, "num_mux_nids %d for pin:port %d:%d\n",
0506 port->num_mux_nids, pin->nid, port->id);
0507
0508 return port->num_mux_nids;
0509 }
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520 static struct hdac_hdmi_port *hdac_hdmi_get_port_from_cvt(
0521 struct hdac_device *hdev,
0522 struct hdac_hdmi_priv *hdmi,
0523 struct hdac_hdmi_cvt *cvt)
0524 {
0525 struct hdac_hdmi_pcm *pcm;
0526 struct hdac_hdmi_port *port;
0527 int ret, i;
0528
0529 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
0530 if (pcm->cvt == cvt) {
0531 if (list_empty(&pcm->port_list))
0532 continue;
0533
0534 list_for_each_entry(port, &pcm->port_list, head) {
0535 mutex_lock(&pcm->lock);
0536 ret = hdac_hdmi_query_port_connlist(hdev,
0537 port->pin, port);
0538 mutex_unlock(&pcm->lock);
0539 if (ret < 0)
0540 continue;
0541
0542 for (i = 0; i < port->num_mux_nids; i++) {
0543 if (port->mux_nids[i] == cvt->nid &&
0544 port->eld.monitor_present &&
0545 port->eld.eld_valid)
0546 return port;
0547 }
0548 }
0549 }
0550 }
0551
0552 return NULL;
0553 }
0554
0555
0556
0557
0558
0559 static void hdac_hdmi_verify_connect_sel_all_pins(struct hdac_device *hdev)
0560 {
0561 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
0562 struct hdac_hdmi_port *port;
0563 struct hdac_hdmi_cvt *cvt;
0564 int cvt_idx = 0;
0565
0566 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
0567 port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
0568 if (port && port->pin) {
0569 snd_hdac_codec_write(hdev, port->pin->nid, 0,
0570 AC_VERB_SET_CONNECT_SEL, cvt_idx);
0571 dev_dbg(&hdev->dev, "%s: %s set connect %d -> %d\n",
0572 __func__, cvt->name, port->pin->nid, cvt_idx);
0573 }
0574 ++cvt_idx;
0575 }
0576 }
0577
0578
0579
0580
0581
0582
0583 static int hdac_hdmi_pcm_open(struct snd_pcm_substream *substream,
0584 struct snd_soc_dai *dai)
0585 {
0586 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
0587 struct hdac_device *hdev = hdmi->hdev;
0588 struct hdac_hdmi_dai_port_map *dai_map;
0589 struct hdac_hdmi_cvt *cvt;
0590 struct hdac_hdmi_port *port;
0591 int ret;
0592
0593 dai_map = &hdmi->dai_map[dai->id];
0594
0595 cvt = dai_map->cvt;
0596 port = hdac_hdmi_get_port_from_cvt(hdev, hdmi, cvt);
0597
0598
0599
0600
0601
0602 if (!port)
0603 return 0;
0604 if ((!port->eld.monitor_present) ||
0605 (!port->eld.eld_valid)) {
0606
0607 dev_warn(&hdev->dev,
0608 "Failed: present?:%d ELD valid?:%d pin:port: %d:%d\n",
0609 port->eld.monitor_present, port->eld.eld_valid,
0610 port->pin->nid, port->id);
0611
0612 return 0;
0613 }
0614
0615 dai_map->port = port;
0616
0617 ret = hdac_hdmi_eld_limit_formats(substream->runtime,
0618 port->eld.eld_buffer);
0619 if (ret < 0)
0620 return ret;
0621
0622 return snd_pcm_hw_constraint_eld(substream->runtime,
0623 port->eld.eld_buffer);
0624 }
0625
0626 static void hdac_hdmi_pcm_close(struct snd_pcm_substream *substream,
0627 struct snd_soc_dai *dai)
0628 {
0629 struct hdac_hdmi_priv *hdmi = snd_soc_dai_get_drvdata(dai);
0630 struct hdac_hdmi_dai_port_map *dai_map;
0631 struct hdac_hdmi_pcm *pcm;
0632
0633 dai_map = &hdmi->dai_map[dai->id];
0634
0635 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, dai_map->cvt);
0636
0637 if (pcm) {
0638 mutex_lock(&pcm->lock);
0639 pcm->chmap_set = false;
0640 memset(pcm->chmap, 0, sizeof(pcm->chmap));
0641 pcm->channels = 0;
0642 mutex_unlock(&pcm->lock);
0643 }
0644
0645 if (dai_map->port)
0646 dai_map->port = NULL;
0647 }
0648
0649 static int
0650 hdac_hdmi_query_cvt_params(struct hdac_device *hdev, struct hdac_hdmi_cvt *cvt)
0651 {
0652 unsigned int chans;
0653 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
0654 int err;
0655
0656 chans = get_wcaps(hdev, cvt->nid);
0657 chans = get_wcaps_channels(chans);
0658
0659 cvt->params.channels_min = 2;
0660
0661 cvt->params.channels_max = chans;
0662 if (chans > hdmi->chmap.channels_max)
0663 hdmi->chmap.channels_max = chans;
0664
0665 err = snd_hdac_query_supported_pcm(hdev, cvt->nid,
0666 &cvt->params.rates,
0667 &cvt->params.formats,
0668 &cvt->params.maxbps);
0669 if (err < 0)
0670 dev_err(&hdev->dev,
0671 "Failed to query pcm params for nid %d: %d\n",
0672 cvt->nid, err);
0673
0674 return err;
0675 }
0676
0677 static int hdac_hdmi_fill_widget_info(struct device *dev,
0678 struct snd_soc_dapm_widget *w, enum snd_soc_dapm_type id,
0679 void *priv, const char *wname, const char *stream,
0680 struct snd_kcontrol_new *wc, int numkc,
0681 int (*event)(struct snd_soc_dapm_widget *,
0682 struct snd_kcontrol *, int), unsigned short event_flags)
0683 {
0684 w->id = id;
0685 w->name = devm_kstrdup(dev, wname, GFP_KERNEL);
0686 if (!w->name)
0687 return -ENOMEM;
0688
0689 w->sname = stream;
0690 w->reg = SND_SOC_NOPM;
0691 w->shift = 0;
0692 w->kcontrol_news = wc;
0693 w->num_kcontrols = numkc;
0694 w->priv = priv;
0695 w->event = event;
0696 w->event_flags = event_flags;
0697
0698 return 0;
0699 }
0700
0701 static void hdac_hdmi_fill_route(struct snd_soc_dapm_route *route,
0702 const char *sink, const char *control, const char *src,
0703 int (*handler)(struct snd_soc_dapm_widget *src,
0704 struct snd_soc_dapm_widget *sink))
0705 {
0706 route->sink = sink;
0707 route->source = src;
0708 route->control = control;
0709 route->connected = handler;
0710 }
0711
0712 static struct hdac_hdmi_pcm *hdac_hdmi_get_pcm(struct hdac_device *hdev,
0713 struct hdac_hdmi_port *port)
0714 {
0715 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
0716 struct hdac_hdmi_pcm *pcm;
0717 struct hdac_hdmi_port *p;
0718
0719 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
0720 if (list_empty(&pcm->port_list))
0721 continue;
0722
0723 list_for_each_entry(p, &pcm->port_list, head) {
0724 if (p->id == port->id && port->pin == p->pin)
0725 return pcm;
0726 }
0727 }
0728
0729 return NULL;
0730 }
0731
0732 static void hdac_hdmi_set_power_state(struct hdac_device *hdev,
0733 hda_nid_t nid, unsigned int pwr_state)
0734 {
0735 int count;
0736 unsigned int state;
0737
0738 if (get_wcaps(hdev, nid) & AC_WCAP_POWER) {
0739 if (!snd_hdac_check_power_state(hdev, nid, pwr_state)) {
0740 for (count = 0; count < 10; count++) {
0741 snd_hdac_codec_read(hdev, nid, 0,
0742 AC_VERB_SET_POWER_STATE,
0743 pwr_state);
0744 state = snd_hdac_sync_power_state(hdev,
0745 nid, pwr_state);
0746 if (!(state & AC_PWRST_ERROR))
0747 break;
0748 }
0749 }
0750 }
0751 }
0752
0753 static void hdac_hdmi_set_amp(struct hdac_device *hdev,
0754 hda_nid_t nid, int val)
0755 {
0756 if (get_wcaps(hdev, nid) & AC_WCAP_OUT_AMP)
0757 snd_hdac_codec_write(hdev, nid, 0,
0758 AC_VERB_SET_AMP_GAIN_MUTE, val);
0759 }
0760
0761
0762 static int hdac_hdmi_pin_output_widget_event(struct snd_soc_dapm_widget *w,
0763 struct snd_kcontrol *kc, int event)
0764 {
0765 struct hdac_hdmi_port *port = w->priv;
0766 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
0767 struct hdac_hdmi_pcm *pcm;
0768
0769 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
0770 __func__, w->name, event);
0771
0772 pcm = hdac_hdmi_get_pcm(hdev, port);
0773 if (!pcm)
0774 return -EIO;
0775
0776
0777 if (hdac_hdmi_port_select_set(hdev, port) < 0)
0778 return -EIO;
0779
0780 switch (event) {
0781 case SND_SOC_DAPM_PRE_PMU:
0782 hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D0);
0783
0784
0785 snd_hdac_codec_write(hdev, port->pin->nid, 0,
0786 AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
0787
0788 hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_UNMUTE);
0789
0790 return hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
0791
0792 case SND_SOC_DAPM_POST_PMD:
0793 hdac_hdmi_set_amp(hdev, port->pin->nid, AMP_OUT_MUTE);
0794
0795
0796 snd_hdac_codec_write(hdev, port->pin->nid, 0,
0797 AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
0798
0799 hdac_hdmi_set_power_state(hdev, port->pin->nid, AC_PWRST_D3);
0800 break;
0801
0802 }
0803
0804 return 0;
0805 }
0806
0807 static int hdac_hdmi_cvt_output_widget_event(struct snd_soc_dapm_widget *w,
0808 struct snd_kcontrol *kc, int event)
0809 {
0810 struct hdac_hdmi_cvt *cvt = w->priv;
0811 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
0812 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
0813 struct hdac_hdmi_pcm *pcm;
0814
0815 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
0816 __func__, w->name, event);
0817
0818 pcm = hdac_hdmi_get_pcm_from_cvt(hdmi, cvt);
0819 if (!pcm)
0820 return -EIO;
0821
0822 switch (event) {
0823 case SND_SOC_DAPM_PRE_PMU:
0824 hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D0);
0825
0826
0827 snd_hdac_codec_write(hdev, cvt->nid, 0,
0828 AC_VERB_SET_DIGI_CONVERT_1, 1);
0829
0830
0831 snd_hdac_codec_write(hdev, cvt->nid, 0,
0832 AC_VERB_SET_DIGI_CONVERT_2, 0);
0833
0834 snd_hdac_codec_write(hdev, cvt->nid, 0,
0835 AC_VERB_SET_CHANNEL_STREAMID, pcm->stream_tag);
0836 snd_hdac_codec_write(hdev, cvt->nid, 0,
0837 AC_VERB_SET_STREAM_FORMAT, pcm->format);
0838
0839
0840
0841
0842
0843
0844 hdac_hdmi_verify_connect_sel_all_pins(hdev);
0845
0846 break;
0847
0848 case SND_SOC_DAPM_POST_PMD:
0849 snd_hdac_codec_write(hdev, cvt->nid, 0,
0850 AC_VERB_SET_CHANNEL_STREAMID, 0);
0851 snd_hdac_codec_write(hdev, cvt->nid, 0,
0852 AC_VERB_SET_STREAM_FORMAT, 0);
0853
0854 hdac_hdmi_set_power_state(hdev, cvt->nid, AC_PWRST_D3);
0855 break;
0856
0857 }
0858
0859 return 0;
0860 }
0861
0862 static int hdac_hdmi_pin_mux_widget_event(struct snd_soc_dapm_widget *w,
0863 struct snd_kcontrol *kc, int event)
0864 {
0865 struct hdac_hdmi_port *port = w->priv;
0866 struct hdac_device *hdev = dev_to_hdac_dev(w->dapm->dev);
0867 int mux_idx;
0868
0869 dev_dbg(&hdev->dev, "%s: widget: %s event: %x\n",
0870 __func__, w->name, event);
0871
0872 if (!kc)
0873 kc = w->kcontrols[0];
0874
0875 mux_idx = dapm_kcontrol_get_value(kc);
0876
0877
0878 if (hdac_hdmi_port_select_set(hdev, port) < 0)
0879 return -EIO;
0880
0881 if (mux_idx > 0) {
0882 snd_hdac_codec_write(hdev, port->pin->nid, 0,
0883 AC_VERB_SET_CONNECT_SEL, (mux_idx - 1));
0884 }
0885
0886 return 0;
0887 }
0888
0889
0890
0891
0892 static int hdac_hdmi_set_pin_port_mux(struct snd_kcontrol *kcontrol,
0893 struct snd_ctl_elem_value *ucontrol)
0894 {
0895 int ret;
0896 struct hdac_hdmi_port *p, *p_next;
0897 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
0898 struct snd_soc_dapm_widget *w = snd_soc_dapm_kcontrol_widget(kcontrol);
0899 struct snd_soc_dapm_context *dapm = w->dapm;
0900 struct hdac_hdmi_port *port = w->priv;
0901 struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
0902 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
0903 struct hdac_hdmi_pcm *pcm;
0904 const char *cvt_name = e->texts[ucontrol->value.enumerated.item[0]];
0905
0906 ret = snd_soc_dapm_put_enum_double(kcontrol, ucontrol);
0907 if (ret < 0)
0908 return ret;
0909
0910 if (port == NULL)
0911 return -EINVAL;
0912
0913 mutex_lock(&hdmi->pin_mutex);
0914 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
0915 if (list_empty(&pcm->port_list))
0916 continue;
0917
0918 list_for_each_entry_safe(p, p_next, &pcm->port_list, head) {
0919 if (p == port && p->id == port->id &&
0920 p->pin == port->pin) {
0921 hdac_hdmi_jack_report_sync(pcm, port, false);
0922 list_del(&p->head);
0923 }
0924 }
0925 }
0926
0927
0928
0929
0930
0931 list_for_each_entry(pcm, &hdmi->pcm_list, head) {
0932 if (!strcmp(cvt_name, pcm->cvt->name)) {
0933 list_add_tail(&port->head, &pcm->port_list);
0934 if (port->eld.monitor_present && port->eld.eld_valid) {
0935 hdac_hdmi_jack_report_sync(pcm, port, true);
0936 mutex_unlock(&hdmi->pin_mutex);
0937 return ret;
0938 }
0939 }
0940 }
0941 mutex_unlock(&hdmi->pin_mutex);
0942
0943 return ret;
0944 }
0945
0946
0947
0948
0949
0950
0951
0952
0953
0954
0955 static int hdac_hdmi_create_pin_port_muxs(struct hdac_device *hdev,
0956 struct hdac_hdmi_port *port,
0957 struct snd_soc_dapm_widget *widget,
0958 const char *widget_name)
0959 {
0960 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
0961 struct hdac_hdmi_pin *pin = port->pin;
0962 struct snd_kcontrol_new *kc;
0963 struct hdac_hdmi_cvt *cvt;
0964 struct soc_enum *se;
0965 char kc_name[NAME_SIZE];
0966 char mux_items[NAME_SIZE];
0967
0968 char *items[HDA_MAX_CONNECTIONS];
0969 int i = 0;
0970 int num_items = hdmi->num_cvt + 1;
0971
0972 kc = devm_kzalloc(&hdev->dev, sizeof(*kc), GFP_KERNEL);
0973 if (!kc)
0974 return -ENOMEM;
0975
0976 se = devm_kzalloc(&hdev->dev, sizeof(*se), GFP_KERNEL);
0977 if (!se)
0978 return -ENOMEM;
0979
0980 snprintf(kc_name, NAME_SIZE, "Pin %d port %d Input",
0981 pin->nid, port->id);
0982 kc->name = devm_kstrdup(&hdev->dev, kc_name, GFP_KERNEL);
0983 if (!kc->name)
0984 return -ENOMEM;
0985
0986 kc->private_value = (long)se;
0987 kc->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
0988 kc->access = 0;
0989 kc->info = snd_soc_info_enum_double;
0990 kc->put = hdac_hdmi_set_pin_port_mux;
0991 kc->get = snd_soc_dapm_get_enum_double;
0992
0993 se->reg = SND_SOC_NOPM;
0994
0995
0996 se->items = num_items;
0997 se->mask = roundup_pow_of_two(se->items) - 1;
0998
0999 sprintf(mux_items, "NONE");
1000 items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
1001 if (!items[i])
1002 return -ENOMEM;
1003
1004 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1005 i++;
1006 sprintf(mux_items, "cvt %d", cvt->nid);
1007 items[i] = devm_kstrdup(&hdev->dev, mux_items, GFP_KERNEL);
1008 if (!items[i])
1009 return -ENOMEM;
1010 }
1011
1012 se->texts = devm_kmemdup(&hdev->dev, items,
1013 (num_items * sizeof(char *)), GFP_KERNEL);
1014 if (!se->texts)
1015 return -ENOMEM;
1016
1017 return hdac_hdmi_fill_widget_info(&hdev->dev, widget,
1018 snd_soc_dapm_mux, port, widget_name, NULL, kc, 1,
1019 hdac_hdmi_pin_mux_widget_event,
1020 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_REG);
1021 }
1022
1023
1024 static void hdac_hdmi_add_pinmux_cvt_route(struct hdac_device *hdev,
1025 struct snd_soc_dapm_widget *widgets,
1026 struct snd_soc_dapm_route *route, int rindex)
1027 {
1028 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1029 const struct snd_kcontrol_new *kc;
1030 struct soc_enum *se;
1031 int mux_index = hdmi->num_cvt + hdmi->num_ports;
1032 int i, j;
1033
1034 for (i = 0; i < hdmi->num_ports; i++) {
1035 kc = widgets[mux_index].kcontrol_news;
1036 se = (struct soc_enum *)kc->private_value;
1037 for (j = 0; j < hdmi->num_cvt; j++) {
1038 hdac_hdmi_fill_route(&route[rindex],
1039 widgets[mux_index].name,
1040 se->texts[j + 1],
1041 widgets[j].name, NULL);
1042
1043 rindex++;
1044 }
1045
1046 mux_index++;
1047 }
1048 }
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066 static int create_fill_widget_route_map(struct snd_soc_dapm_context *dapm)
1067 {
1068 struct snd_soc_dapm_widget *widgets;
1069 struct snd_soc_dapm_route *route;
1070 struct hdac_device *hdev = dev_to_hdac_dev(dapm->dev);
1071 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1072 struct snd_soc_dai_driver *dai_drv = hdmi->dai_drv;
1073 char widget_name[NAME_SIZE];
1074 struct hdac_hdmi_cvt *cvt;
1075 struct hdac_hdmi_pin *pin;
1076 int ret, i = 0, num_routes = 0, j;
1077
1078 if (list_empty(&hdmi->cvt_list) || list_empty(&hdmi->pin_list))
1079 return -EINVAL;
1080
1081 widgets = devm_kzalloc(dapm->dev, (sizeof(*widgets) *
1082 ((2 * hdmi->num_ports) + hdmi->num_cvt)),
1083 GFP_KERNEL);
1084
1085 if (!widgets)
1086 return -ENOMEM;
1087
1088
1089 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1090 sprintf(widget_name, "Converter %d", cvt->nid);
1091 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1092 snd_soc_dapm_aif_in, cvt,
1093 widget_name, dai_drv[i].playback.stream_name, NULL, 0,
1094 hdac_hdmi_cvt_output_widget_event,
1095 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD);
1096 if (ret < 0)
1097 return ret;
1098 i++;
1099 }
1100
1101 list_for_each_entry(pin, &hdmi->pin_list, head) {
1102 for (j = 0; j < pin->num_ports; j++) {
1103 sprintf(widget_name, "hif%d-%d Output",
1104 pin->nid, pin->ports[j].id);
1105 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1106 snd_soc_dapm_output, &pin->ports[j],
1107 widget_name, NULL, NULL, 0,
1108 hdac_hdmi_pin_output_widget_event,
1109 SND_SOC_DAPM_PRE_PMU |
1110 SND_SOC_DAPM_POST_PMD);
1111 if (ret < 0)
1112 return ret;
1113 pin->ports[j].output_pin = widgets[i].name;
1114 i++;
1115 }
1116 }
1117
1118
1119 list_for_each_entry(pin, &hdmi->pin_list, head) {
1120 for (j = 0; j < pin->num_ports; j++) {
1121 sprintf(widget_name, "Pin%d-Port%d Mux",
1122 pin->nid, pin->ports[j].id);
1123 ret = hdac_hdmi_create_pin_port_muxs(hdev,
1124 &pin->ports[j], &widgets[i],
1125 widget_name);
1126 if (ret < 0)
1127 return ret;
1128 i++;
1129
1130
1131 num_routes += hdmi->num_cvt;
1132
1133
1134 num_routes++;
1135 }
1136 }
1137
1138 route = devm_kzalloc(dapm->dev, (sizeof(*route) * num_routes),
1139 GFP_KERNEL);
1140 if (!route)
1141 return -ENOMEM;
1142
1143 i = 0;
1144
1145 list_for_each_entry(pin, &hdmi->pin_list, head) {
1146 for (j = 0; j < pin->num_ports; j++) {
1147 int sink_index = i + hdmi->num_cvt;
1148 int src_index = sink_index + pin->num_ports *
1149 hdmi->num_pin;
1150
1151 hdac_hdmi_fill_route(&route[i],
1152 widgets[sink_index].name, NULL,
1153 widgets[src_index].name, NULL);
1154 i++;
1155 }
1156 }
1157
1158 hdac_hdmi_add_pinmux_cvt_route(hdev, widgets, route, i);
1159
1160 snd_soc_dapm_new_controls(dapm, widgets,
1161 ((2 * hdmi->num_ports) + hdmi->num_cvt));
1162
1163 snd_soc_dapm_add_routes(dapm, route, num_routes);
1164 snd_soc_dapm_new_widgets(dapm->card);
1165
1166 return 0;
1167
1168 }
1169
1170 static int hdac_hdmi_init_dai_map(struct hdac_device *hdev)
1171 {
1172 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1173 struct hdac_hdmi_dai_port_map *dai_map;
1174 struct hdac_hdmi_cvt *cvt;
1175 int dai_id = 0;
1176
1177 if (list_empty(&hdmi->cvt_list))
1178 return -EINVAL;
1179
1180 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1181 dai_map = &hdmi->dai_map[dai_id];
1182 dai_map->dai_id = dai_id;
1183 dai_map->cvt = cvt;
1184
1185 dai_id++;
1186
1187 if (dai_id == HDA_MAX_CVTS) {
1188 dev_warn(&hdev->dev,
1189 "Max dais supported: %d\n", dai_id);
1190 break;
1191 }
1192 }
1193
1194 return 0;
1195 }
1196
1197 static int hdac_hdmi_add_cvt(struct hdac_device *hdev, hda_nid_t nid)
1198 {
1199 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1200 struct hdac_hdmi_cvt *cvt;
1201 char name[NAME_SIZE];
1202
1203 cvt = devm_kzalloc(&hdev->dev, sizeof(*cvt), GFP_KERNEL);
1204 if (!cvt)
1205 return -ENOMEM;
1206
1207 cvt->nid = nid;
1208 sprintf(name, "cvt %d", cvt->nid);
1209 cvt->name = devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1210 if (!cvt->name)
1211 return -ENOMEM;
1212
1213 list_add_tail(&cvt->head, &hdmi->cvt_list);
1214 hdmi->num_cvt++;
1215
1216 return hdac_hdmi_query_cvt_params(hdev, cvt);
1217 }
1218
1219 static int hdac_hdmi_parse_eld(struct hdac_device *hdev,
1220 struct hdac_hdmi_port *port)
1221 {
1222 unsigned int ver, mnl;
1223
1224 ver = (port->eld.eld_buffer[DRM_ELD_VER] & DRM_ELD_VER_MASK)
1225 >> DRM_ELD_VER_SHIFT;
1226
1227 if (ver != ELD_VER_CEA_861D && ver != ELD_VER_PARTIAL) {
1228 dev_err(&hdev->dev, "HDMI: Unknown ELD version %d\n", ver);
1229 return -EINVAL;
1230 }
1231
1232 mnl = (port->eld.eld_buffer[DRM_ELD_CEA_EDID_VER_MNL] &
1233 DRM_ELD_MNL_MASK) >> DRM_ELD_MNL_SHIFT;
1234
1235 if (mnl > ELD_MAX_MNL) {
1236 dev_err(&hdev->dev, "HDMI: MNL Invalid %d\n", mnl);
1237 return -EINVAL;
1238 }
1239
1240 port->eld.info.spk_alloc = port->eld.eld_buffer[DRM_ELD_SPEAKER];
1241
1242 return 0;
1243 }
1244
1245 static void hdac_hdmi_present_sense(struct hdac_hdmi_pin *pin,
1246 struct hdac_hdmi_port *port)
1247 {
1248 struct hdac_device *hdev = pin->hdev;
1249 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1250 struct hdac_hdmi_pcm *pcm;
1251 int size = 0;
1252 int port_id = -1;
1253 bool eld_valid, eld_changed;
1254
1255 if (!hdmi)
1256 return;
1257
1258
1259
1260
1261
1262 mutex_lock(&hdmi->pin_mutex);
1263 port->eld.monitor_present = false;
1264
1265 if (pin->mst_capable)
1266 port_id = port->id;
1267
1268 size = snd_hdac_acomp_get_eld(hdev, pin->nid, port_id,
1269 &port->eld.monitor_present,
1270 port->eld.eld_buffer,
1271 ELD_MAX_SIZE);
1272
1273 if (size > 0) {
1274 size = min(size, ELD_MAX_SIZE);
1275 if (hdac_hdmi_parse_eld(hdev, port) < 0)
1276 size = -EINVAL;
1277 }
1278
1279 eld_valid = port->eld.eld_valid;
1280
1281 if (size > 0) {
1282 port->eld.eld_valid = true;
1283 port->eld.eld_size = size;
1284 } else {
1285 port->eld.eld_valid = false;
1286 port->eld.eld_size = 0;
1287 }
1288
1289 eld_changed = (eld_valid != port->eld.eld_valid);
1290
1291 pcm = hdac_hdmi_get_pcm(hdev, port);
1292
1293 if (!port->eld.monitor_present || !port->eld.eld_valid) {
1294
1295 dev_err(&hdev->dev, "%s: disconnect for pin:port %d:%d\n",
1296 __func__, pin->nid, port->id);
1297
1298
1299
1300
1301
1302
1303 if (pcm) {
1304 hdac_hdmi_jack_report(pcm, port, false);
1305 schedule_work(&port->dapm_work);
1306 }
1307
1308 mutex_unlock(&hdmi->pin_mutex);
1309 return;
1310 }
1311
1312 if (port->eld.monitor_present && port->eld.eld_valid) {
1313 if (pcm) {
1314 hdac_hdmi_jack_report(pcm, port, true);
1315 schedule_work(&port->dapm_work);
1316 }
1317
1318 print_hex_dump_debug("ELD: ", DUMP_PREFIX_OFFSET, 16, 1,
1319 port->eld.eld_buffer, port->eld.eld_size, false);
1320
1321 }
1322 mutex_unlock(&hdmi->pin_mutex);
1323
1324 if (eld_changed && pcm)
1325 snd_ctl_notify(hdmi->card,
1326 SNDRV_CTL_EVENT_MASK_VALUE |
1327 SNDRV_CTL_EVENT_MASK_INFO,
1328 &pcm->eld_ctl->id);
1329 }
1330
1331 static int hdac_hdmi_add_ports(struct hdac_device *hdev,
1332 struct hdac_hdmi_pin *pin)
1333 {
1334 struct hdac_hdmi_port *ports;
1335 int max_ports = HDA_MAX_PORTS;
1336 int i;
1337
1338
1339
1340
1341
1342
1343
1344 ports = devm_kcalloc(&hdev->dev, max_ports, sizeof(*ports), GFP_KERNEL);
1345 if (!ports)
1346 return -ENOMEM;
1347
1348 for (i = 0; i < max_ports; i++) {
1349 ports[i].id = i;
1350 ports[i].pin = pin;
1351 INIT_WORK(&ports[i].dapm_work, hdac_hdmi_jack_dapm_work);
1352 }
1353 pin->ports = ports;
1354 pin->num_ports = max_ports;
1355 return 0;
1356 }
1357
1358 static int hdac_hdmi_add_pin(struct hdac_device *hdev, hda_nid_t nid)
1359 {
1360 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1361 struct hdac_hdmi_pin *pin;
1362 int ret;
1363
1364 pin = devm_kzalloc(&hdev->dev, sizeof(*pin), GFP_KERNEL);
1365 if (!pin)
1366 return -ENOMEM;
1367
1368 pin->nid = nid;
1369 pin->mst_capable = false;
1370 pin->hdev = hdev;
1371 ret = hdac_hdmi_add_ports(hdev, pin);
1372 if (ret < 0)
1373 return ret;
1374
1375 list_add_tail(&pin->head, &hdmi->pin_list);
1376 hdmi->num_pin++;
1377 hdmi->num_ports += pin->num_ports;
1378
1379 return 0;
1380 }
1381
1382 #define INTEL_VENDOR_NID 0x08
1383 #define INTEL_GLK_VENDOR_NID 0x0b
1384 #define INTEL_GET_VENDOR_VERB 0xf81
1385 #define INTEL_SET_VENDOR_VERB 0x781
1386 #define INTEL_EN_DP12 0x02
1387 #define INTEL_EN_ALL_PIN_CVTS 0x01
1388
1389 static void hdac_hdmi_skl_enable_all_pins(struct hdac_device *hdev)
1390 {
1391 unsigned int vendor_param;
1392 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1393 unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1394
1395 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1396 INTEL_GET_VENDOR_VERB, 0);
1397 if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
1398 return;
1399
1400 vendor_param |= INTEL_EN_ALL_PIN_CVTS;
1401 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1402 INTEL_SET_VENDOR_VERB, vendor_param);
1403 if (vendor_param == -1)
1404 return;
1405 }
1406
1407 static void hdac_hdmi_skl_enable_dp12(struct hdac_device *hdev)
1408 {
1409 unsigned int vendor_param;
1410 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1411 unsigned int vendor_nid = hdmi->drv_data->vendor_nid;
1412
1413 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1414 INTEL_GET_VENDOR_VERB, 0);
1415 if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
1416 return;
1417
1418
1419 vendor_param |= INTEL_EN_DP12;
1420 vendor_param = snd_hdac_codec_read(hdev, vendor_nid, 0,
1421 INTEL_SET_VENDOR_VERB, vendor_param);
1422 if (vendor_param == -1)
1423 return;
1424
1425 }
1426
1427 static int hdac_hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
1428 struct snd_ctl_elem_info *uinfo)
1429 {
1430 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1431 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1432 struct hdac_hdmi_pcm *pcm;
1433 struct hdac_hdmi_port *port;
1434 struct hdac_hdmi_eld *eld;
1435
1436 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
1437 uinfo->count = 0;
1438
1439 pcm = get_hdmi_pcm_from_id(hdmi, kcontrol->id.device);
1440 if (!pcm) {
1441 dev_dbg(component->dev, "%s: no pcm, device %d\n", __func__,
1442 kcontrol->id.device);
1443 return 0;
1444 }
1445
1446 if (list_empty(&pcm->port_list)) {
1447 dev_dbg(component->dev, "%s: empty port list, device %d\n",
1448 __func__, kcontrol->id.device);
1449 return 0;
1450 }
1451
1452 mutex_lock(&hdmi->pin_mutex);
1453
1454 list_for_each_entry(port, &pcm->port_list, head) {
1455 eld = &port->eld;
1456
1457 if (eld->eld_valid) {
1458 uinfo->count = eld->eld_size;
1459 break;
1460 }
1461 }
1462
1463 mutex_unlock(&hdmi->pin_mutex);
1464
1465 return 0;
1466 }
1467
1468 static int hdac_hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
1469 struct snd_ctl_elem_value *ucontrol)
1470 {
1471 struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1472 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1473 struct hdac_hdmi_pcm *pcm;
1474 struct hdac_hdmi_port *port;
1475 struct hdac_hdmi_eld *eld;
1476
1477 memset(ucontrol->value.bytes.data, 0, sizeof(ucontrol->value.bytes.data));
1478
1479 pcm = get_hdmi_pcm_from_id(hdmi, kcontrol->id.device);
1480 if (!pcm) {
1481 dev_dbg(component->dev, "%s: no pcm, device %d\n", __func__,
1482 kcontrol->id.device);
1483 return 0;
1484 }
1485
1486 if (list_empty(&pcm->port_list)) {
1487 dev_dbg(component->dev, "%s: empty port list, device %d\n",
1488 __func__, kcontrol->id.device);
1489 return 0;
1490 }
1491
1492 mutex_lock(&hdmi->pin_mutex);
1493
1494 list_for_each_entry(port, &pcm->port_list, head) {
1495 eld = &port->eld;
1496
1497 if (!eld->eld_valid)
1498 continue;
1499
1500 if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data) ||
1501 eld->eld_size > ELD_MAX_SIZE) {
1502 mutex_unlock(&hdmi->pin_mutex);
1503
1504 dev_err(component->dev, "%s: buffer too small, device %d eld_size %d\n",
1505 __func__, kcontrol->id.device, eld->eld_size);
1506 snd_BUG();
1507 return -EINVAL;
1508 }
1509
1510 memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
1511 eld->eld_size);
1512 break;
1513 }
1514
1515 mutex_unlock(&hdmi->pin_mutex);
1516
1517 return 0;
1518 }
1519
1520 static int hdac_hdmi_create_eld_ctl(struct snd_soc_component *component, struct hdac_hdmi_pcm *pcm)
1521 {
1522 struct snd_kcontrol *kctl;
1523 struct snd_kcontrol_new hdmi_eld_ctl = {
1524 .access = SNDRV_CTL_ELEM_ACCESS_READ |
1525 SNDRV_CTL_ELEM_ACCESS_VOLATILE,
1526 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
1527 .name = "ELD",
1528 .info = hdac_hdmi_eld_ctl_info,
1529 .get = hdac_hdmi_eld_ctl_get,
1530 .device = pcm->pcm_id,
1531 };
1532
1533
1534 kctl = snd_ctl_new1(&hdmi_eld_ctl, component);
1535 if (!kctl)
1536 return -ENOMEM;
1537
1538 pcm->eld_ctl = kctl;
1539
1540 return snd_ctl_add(component->card->snd_card, kctl);
1541 }
1542
1543 static const struct snd_soc_dai_ops hdmi_dai_ops = {
1544 .startup = hdac_hdmi_pcm_open,
1545 .shutdown = hdac_hdmi_pcm_close,
1546 .hw_params = hdac_hdmi_set_hw_params,
1547 .set_tdm_slot = hdac_hdmi_set_tdm_slot,
1548 };
1549
1550
1551
1552
1553
1554 static int hdac_hdmi_create_dais(struct hdac_device *hdev,
1555 struct snd_soc_dai_driver **dais,
1556 struct hdac_hdmi_priv *hdmi, int num_dais)
1557 {
1558 struct snd_soc_dai_driver *hdmi_dais;
1559 struct hdac_hdmi_cvt *cvt;
1560 char name[NAME_SIZE], dai_name[NAME_SIZE];
1561 int i = 0;
1562 u32 rates, bps;
1563 unsigned int rate_max = 384000, rate_min = 8000;
1564 u64 formats;
1565 int ret;
1566
1567 hdmi_dais = devm_kzalloc(&hdev->dev,
1568 (sizeof(*hdmi_dais) * num_dais),
1569 GFP_KERNEL);
1570 if (!hdmi_dais)
1571 return -ENOMEM;
1572
1573 list_for_each_entry(cvt, &hdmi->cvt_list, head) {
1574 ret = snd_hdac_query_supported_pcm(hdev, cvt->nid,
1575 &rates, &formats, &bps);
1576 if (ret)
1577 return ret;
1578
1579
1580 rates &= ~(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_88200 |
1581 SNDRV_PCM_RATE_176400);
1582 if (!rates)
1583 return -EINVAL;
1584
1585 sprintf(dai_name, "intel-hdmi-hifi%d", i+1);
1586 hdmi_dais[i].name = devm_kstrdup(&hdev->dev,
1587 dai_name, GFP_KERNEL);
1588
1589 if (!hdmi_dais[i].name)
1590 return -ENOMEM;
1591
1592 snprintf(name, sizeof(name), "hifi%d", i+1);
1593 hdmi_dais[i].playback.stream_name =
1594 devm_kstrdup(&hdev->dev, name, GFP_KERNEL);
1595 if (!hdmi_dais[i].playback.stream_name)
1596 return -ENOMEM;
1597
1598
1599
1600
1601
1602 hdmi_dais[i].playback.formats = formats;
1603 hdmi_dais[i].playback.rates = rates;
1604 hdmi_dais[i].playback.rate_max = rate_max;
1605 hdmi_dais[i].playback.rate_min = rate_min;
1606 hdmi_dais[i].playback.channels_min = 2;
1607 hdmi_dais[i].playback.channels_max = 2;
1608 hdmi_dais[i].playback.sig_bits = bps;
1609 hdmi_dais[i].ops = &hdmi_dai_ops;
1610 i++;
1611 }
1612
1613 *dais = hdmi_dais;
1614 hdmi->dai_drv = hdmi_dais;
1615
1616 return 0;
1617 }
1618
1619
1620
1621
1622
1623 static int hdac_hdmi_parse_and_map_nid(struct hdac_device *hdev,
1624 struct snd_soc_dai_driver **dais, int *num_dais)
1625 {
1626 hda_nid_t nid;
1627 int i, num_nodes;
1628 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1629 int ret;
1630
1631 hdac_hdmi_skl_enable_all_pins(hdev);
1632 hdac_hdmi_skl_enable_dp12(hdev);
1633
1634 num_nodes = snd_hdac_get_sub_nodes(hdev, hdev->afg, &nid);
1635 if (!nid || num_nodes <= 0) {
1636 dev_warn(&hdev->dev, "HDMI: failed to get afg sub nodes\n");
1637 return -EINVAL;
1638 }
1639
1640 for (i = 0; i < num_nodes; i++, nid++) {
1641 unsigned int caps;
1642 unsigned int type;
1643
1644 caps = get_wcaps(hdev, nid);
1645 type = get_wcaps_type(caps);
1646
1647 if (!(caps & AC_WCAP_DIGITAL))
1648 continue;
1649
1650 switch (type) {
1651
1652 case AC_WID_AUD_OUT:
1653 ret = hdac_hdmi_add_cvt(hdev, nid);
1654 if (ret < 0)
1655 return ret;
1656 break;
1657
1658 case AC_WID_PIN:
1659 ret = hdac_hdmi_add_pin(hdev, nid);
1660 if (ret < 0)
1661 return ret;
1662 break;
1663 }
1664 }
1665
1666 if (!hdmi->num_pin || !hdmi->num_cvt) {
1667 ret = -EIO;
1668 dev_err(&hdev->dev, "Bad pin/cvt setup in %s\n", __func__);
1669 return ret;
1670 }
1671
1672 ret = hdac_hdmi_create_dais(hdev, dais, hdmi, hdmi->num_cvt);
1673 if (ret) {
1674 dev_err(&hdev->dev, "Failed to create dais with err: %d\n",
1675 ret);
1676 return ret;
1677 }
1678
1679 *num_dais = hdmi->num_cvt;
1680 ret = hdac_hdmi_init_dai_map(hdev);
1681 if (ret < 0)
1682 dev_err(&hdev->dev, "Failed to init DAI map with err: %d\n",
1683 ret);
1684 return ret;
1685 }
1686
1687 static int hdac_hdmi_pin2port(void *aptr, int pin)
1688 {
1689 return pin - 4;
1690 }
1691
1692 static void hdac_hdmi_eld_notify_cb(void *aptr, int port, int pipe)
1693 {
1694 struct hdac_device *hdev = aptr;
1695 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1696 struct hdac_hdmi_pin *pin;
1697 struct hdac_hdmi_port *hport = NULL;
1698 struct snd_soc_component *component = hdmi->component;
1699 int i;
1700
1701
1702 hda_nid_t pin_nid = port + 0x04;
1703
1704 dev_dbg(&hdev->dev, "%s: for pin:%d port=%d\n", __func__,
1705 pin_nid, pipe);
1706
1707
1708
1709
1710
1711
1712
1713 if (snd_power_get_state(component->card->snd_card) !=
1714 SNDRV_CTL_POWER_D0)
1715 return;
1716
1717 if (atomic_read(&hdev->in_pm))
1718 return;
1719
1720 list_for_each_entry(pin, &hdmi->pin_list, head) {
1721 if (pin->nid != pin_nid)
1722 continue;
1723
1724
1725 if (pipe == -1) {
1726 pin->mst_capable = false;
1727
1728 hport = &pin->ports[0];
1729 } else {
1730 for (i = 0; i < pin->num_ports; i++) {
1731 pin->mst_capable = true;
1732 if (pin->ports[i].id == pipe) {
1733 hport = &pin->ports[i];
1734 break;
1735 }
1736 }
1737 }
1738
1739 if (hport)
1740 hdac_hdmi_present_sense(pin, hport);
1741 }
1742
1743 }
1744
1745 static struct drm_audio_component_audio_ops aops = {
1746 .pin2port = hdac_hdmi_pin2port,
1747 .pin_eld_notify = hdac_hdmi_eld_notify_cb,
1748 };
1749
1750 static struct snd_pcm *hdac_hdmi_get_pcm_from_id(struct snd_soc_card *card,
1751 int device)
1752 {
1753 struct snd_soc_pcm_runtime *rtd;
1754
1755 for_each_card_rtds(card, rtd) {
1756 if (rtd->pcm && (rtd->pcm->device == device))
1757 return rtd->pcm;
1758 }
1759
1760 return NULL;
1761 }
1762
1763
1764 static int create_fill_jack_kcontrols(struct snd_soc_card *card,
1765 struct hdac_device *hdev)
1766 {
1767 struct hdac_hdmi_pin *pin;
1768 struct snd_kcontrol_new *kc;
1769 char kc_name[NAME_SIZE], xname[NAME_SIZE];
1770 char *name;
1771 int i = 0, j;
1772 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
1773 struct snd_soc_component *component = hdmi->component;
1774
1775 kc = devm_kcalloc(component->dev, hdmi->num_ports,
1776 sizeof(*kc), GFP_KERNEL);
1777
1778 if (!kc)
1779 return -ENOMEM;
1780
1781 list_for_each_entry(pin, &hdmi->pin_list, head) {
1782 for (j = 0; j < pin->num_ports; j++) {
1783 snprintf(xname, sizeof(xname), "hif%d-%d Jack",
1784 pin->nid, pin->ports[j].id);
1785 name = devm_kstrdup(component->dev, xname, GFP_KERNEL);
1786 if (!name)
1787 return -ENOMEM;
1788 snprintf(kc_name, sizeof(kc_name), "%s Switch", xname);
1789 kc[i].name = devm_kstrdup(component->dev, kc_name,
1790 GFP_KERNEL);
1791 if (!kc[i].name)
1792 return -ENOMEM;
1793
1794 kc[i].private_value = (unsigned long)name;
1795 kc[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1796 kc[i].access = 0;
1797 kc[i].info = snd_soc_dapm_info_pin_switch;
1798 kc[i].put = snd_soc_dapm_put_pin_switch;
1799 kc[i].get = snd_soc_dapm_get_pin_switch;
1800 i++;
1801 }
1802 }
1803
1804 return snd_soc_add_card_controls(card, kc, i);
1805 }
1806
1807 int hdac_hdmi_jack_port_init(struct snd_soc_component *component,
1808 struct snd_soc_dapm_context *dapm)
1809 {
1810 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1811 struct hdac_device *hdev = hdmi->hdev;
1812 struct hdac_hdmi_pin *pin;
1813 struct snd_soc_dapm_widget *widgets;
1814 struct snd_soc_dapm_route *route;
1815 char w_name[NAME_SIZE];
1816 int i = 0, j, ret;
1817
1818 widgets = devm_kcalloc(dapm->dev, hdmi->num_ports,
1819 sizeof(*widgets), GFP_KERNEL);
1820
1821 if (!widgets)
1822 return -ENOMEM;
1823
1824 route = devm_kcalloc(dapm->dev, hdmi->num_ports,
1825 sizeof(*route), GFP_KERNEL);
1826 if (!route)
1827 return -ENOMEM;
1828
1829
1830 list_for_each_entry(pin, &hdmi->pin_list, head) {
1831 for (j = 0; j < pin->num_ports; j++) {
1832 snprintf(w_name, sizeof(w_name), "hif%d-%d Jack",
1833 pin->nid, pin->ports[j].id);
1834
1835 ret = hdac_hdmi_fill_widget_info(dapm->dev, &widgets[i],
1836 snd_soc_dapm_spk, NULL,
1837 w_name, NULL, NULL, 0, NULL, 0);
1838 if (ret < 0)
1839 return ret;
1840
1841 pin->ports[j].jack_pin = widgets[i].name;
1842 pin->ports[j].dapm = dapm;
1843
1844
1845 hdac_hdmi_fill_route(&route[i], pin->ports[j].jack_pin,
1846 NULL, pin->ports[j].output_pin, NULL);
1847
1848 i++;
1849 }
1850 }
1851
1852
1853 ret = snd_soc_dapm_new_controls(dapm, widgets, hdmi->num_ports);
1854 if (ret < 0)
1855 return ret;
1856
1857 ret = snd_soc_dapm_add_routes(dapm, route, hdmi->num_ports);
1858 if (ret < 0)
1859 return ret;
1860
1861 ret = snd_soc_dapm_new_widgets(dapm->card);
1862 if (ret < 0)
1863 return ret;
1864
1865
1866 ret = create_fill_jack_kcontrols(dapm->card, hdev);
1867
1868 if (ret < 0)
1869 return ret;
1870
1871
1872 list_for_each_entry(pin, &hdmi->pin_list, head) {
1873 for (j = 0; j < pin->num_ports; j++)
1874 snd_soc_dapm_disable_pin(pin->ports[j].dapm,
1875 pin->ports[j].jack_pin);
1876 }
1877
1878 return 0;
1879 }
1880 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_port_init);
1881
1882 int hdac_hdmi_jack_init(struct snd_soc_dai *dai, int device,
1883 struct snd_soc_jack *jack)
1884 {
1885 struct snd_soc_component *component = dai->component;
1886 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1887 struct hdac_device *hdev = hdmi->hdev;
1888 struct hdac_hdmi_pcm *pcm;
1889 struct snd_pcm *snd_pcm;
1890 int err;
1891
1892
1893
1894
1895
1896 pcm = devm_kzalloc(&hdev->dev, sizeof(*pcm), GFP_KERNEL);
1897 if (!pcm)
1898 return -ENOMEM;
1899 pcm->pcm_id = device;
1900 pcm->cvt = hdmi->dai_map[dai->id].cvt;
1901 pcm->jack_event = 0;
1902 pcm->jack = jack;
1903 mutex_init(&pcm->lock);
1904 INIT_LIST_HEAD(&pcm->port_list);
1905 snd_pcm = hdac_hdmi_get_pcm_from_id(dai->component->card, device);
1906 if (snd_pcm) {
1907 err = snd_hdac_add_chmap_ctls(snd_pcm, device, &hdmi->chmap);
1908 if (err < 0) {
1909 dev_err(&hdev->dev,
1910 "chmap control add failed with err: %d for pcm: %d\n",
1911 err, device);
1912 return err;
1913 }
1914 }
1915
1916
1917 err = hdac_hdmi_create_eld_ctl(component, pcm);
1918 if (err < 0) {
1919 dev_err(&hdev->dev,
1920 "eld control add failed with err: %d for pcm: %d\n",
1921 err, device);
1922 return err;
1923 }
1924
1925 list_add_tail(&pcm->head, &hdmi->pcm_list);
1926
1927 return 0;
1928 }
1929 EXPORT_SYMBOL_GPL(hdac_hdmi_jack_init);
1930
1931 static void hdac_hdmi_present_sense_all_pins(struct hdac_device *hdev,
1932 struct hdac_hdmi_priv *hdmi, bool detect_pin_caps)
1933 {
1934 int i;
1935 struct hdac_hdmi_pin *pin;
1936
1937 list_for_each_entry(pin, &hdmi->pin_list, head) {
1938 if (detect_pin_caps) {
1939
1940 if (hdac_hdmi_get_port_len(hdev, pin->nid) == 0)
1941 pin->mst_capable = false;
1942 else
1943 pin->mst_capable = true;
1944 }
1945
1946 for (i = 0; i < pin->num_ports; i++) {
1947 if (!pin->mst_capable && i > 0)
1948 continue;
1949
1950 hdac_hdmi_present_sense(pin, &pin->ports[i]);
1951 }
1952 }
1953 }
1954
1955 static int hdmi_codec_probe(struct snd_soc_component *component)
1956 {
1957 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
1958 struct hdac_device *hdev = hdmi->hdev;
1959 struct snd_soc_dapm_context *dapm =
1960 snd_soc_component_get_dapm(component);
1961 struct hdac_ext_link *hlink;
1962 int ret;
1963
1964 hdmi->component = component;
1965
1966
1967
1968
1969
1970 hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
1971 if (!hlink) {
1972 dev_err(&hdev->dev, "hdac link not found\n");
1973 return -EIO;
1974 }
1975
1976 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
1977
1978 ret = create_fill_widget_route_map(dapm);
1979 if (ret < 0)
1980 return ret;
1981
1982 aops.audio_ptr = hdev;
1983 ret = snd_hdac_acomp_register_notifier(hdev->bus, &aops);
1984 if (ret < 0) {
1985 dev_err(&hdev->dev, "notifier register failed: err: %d\n", ret);
1986 return ret;
1987 }
1988
1989 hdac_hdmi_present_sense_all_pins(hdev, hdmi, true);
1990
1991 hdmi->card = dapm->card->snd_card;
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002 device_link_add(component->card->dev, &hdev->dev, DL_FLAG_RPM_ACTIVE |
2003 DL_FLAG_AUTOREMOVE_CONSUMER);
2004
2005
2006
2007
2008 pm_runtime_enable(&hdev->dev);
2009 pm_runtime_put(&hdev->dev);
2010 pm_runtime_suspend(&hdev->dev);
2011
2012 return 0;
2013 }
2014
2015 static void hdmi_codec_remove(struct snd_soc_component *component)
2016 {
2017 struct hdac_hdmi_priv *hdmi = snd_soc_component_get_drvdata(component);
2018 struct hdac_device *hdev = hdmi->hdev;
2019 int ret;
2020
2021 ret = snd_hdac_acomp_register_notifier(hdev->bus, NULL);
2022 if (ret < 0)
2023 dev_err(&hdev->dev, "notifier unregister failed: err: %d\n",
2024 ret);
2025
2026 pm_runtime_disable(&hdev->dev);
2027 }
2028
2029 #ifdef CONFIG_PM_SLEEP
2030 static int hdmi_codec_resume(struct device *dev)
2031 {
2032 struct hdac_device *hdev = dev_to_hdac_dev(dev);
2033 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2034 int ret;
2035
2036 ret = pm_runtime_force_resume(dev);
2037 if (ret < 0)
2038 return ret;
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049 hdac_hdmi_present_sense_all_pins(hdev, hdmi, false);
2050 return 0;
2051 }
2052 #else
2053 #define hdmi_codec_resume NULL
2054 #endif
2055
2056 static const struct snd_soc_component_driver hdmi_hda_codec = {
2057 .probe = hdmi_codec_probe,
2058 .remove = hdmi_codec_remove,
2059 .use_pmdown_time = 1,
2060 .endianness = 1,
2061 };
2062
2063 static void hdac_hdmi_get_chmap(struct hdac_device *hdev, int pcm_idx,
2064 unsigned char *chmap)
2065 {
2066 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2067 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
2068
2069 memcpy(chmap, pcm->chmap, ARRAY_SIZE(pcm->chmap));
2070 }
2071
2072 static void hdac_hdmi_set_chmap(struct hdac_device *hdev, int pcm_idx,
2073 unsigned char *chmap, int prepared)
2074 {
2075 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2076 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
2077 struct hdac_hdmi_port *port;
2078
2079 if (!pcm)
2080 return;
2081
2082 if (list_empty(&pcm->port_list))
2083 return;
2084
2085 mutex_lock(&pcm->lock);
2086 pcm->chmap_set = true;
2087 memcpy(pcm->chmap, chmap, ARRAY_SIZE(pcm->chmap));
2088 list_for_each_entry(port, &pcm->port_list, head)
2089 if (prepared)
2090 hdac_hdmi_setup_audio_infoframe(hdev, pcm, port);
2091 mutex_unlock(&pcm->lock);
2092 }
2093
2094 static bool is_hdac_hdmi_pcm_attached(struct hdac_device *hdev, int pcm_idx)
2095 {
2096 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2097 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
2098
2099 if (!pcm)
2100 return false;
2101
2102 if (list_empty(&pcm->port_list))
2103 return false;
2104
2105 return true;
2106 }
2107
2108 static int hdac_hdmi_get_spk_alloc(struct hdac_device *hdev, int pcm_idx)
2109 {
2110 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2111 struct hdac_hdmi_pcm *pcm = get_hdmi_pcm_from_id(hdmi, pcm_idx);
2112 struct hdac_hdmi_port *port;
2113
2114 if (!pcm)
2115 return 0;
2116
2117 if (list_empty(&pcm->port_list))
2118 return 0;
2119
2120 port = list_first_entry(&pcm->port_list, struct hdac_hdmi_port, head);
2121
2122 if (!port || !port->eld.eld_valid)
2123 return 0;
2124
2125 return port->eld.info.spk_alloc;
2126 }
2127
2128 static struct hdac_hdmi_drv_data intel_glk_drv_data = {
2129 .vendor_nid = INTEL_GLK_VENDOR_NID,
2130 };
2131
2132 static struct hdac_hdmi_drv_data intel_drv_data = {
2133 .vendor_nid = INTEL_VENDOR_NID,
2134 };
2135
2136 static int hdac_hdmi_dev_probe(struct hdac_device *hdev)
2137 {
2138 struct hdac_hdmi_priv *hdmi_priv;
2139 struct snd_soc_dai_driver *hdmi_dais = NULL;
2140 struct hdac_ext_link *hlink;
2141 int num_dais = 0;
2142 int ret;
2143 struct hdac_driver *hdrv = drv_to_hdac_driver(hdev->dev.driver);
2144 const struct hda_device_id *hdac_id = hdac_get_device_id(hdev, hdrv);
2145
2146
2147 hlink = snd_hdac_ext_bus_get_link(hdev->bus, dev_name(&hdev->dev));
2148 if (!hlink) {
2149 dev_err(&hdev->dev, "hdac link not found\n");
2150 return -EIO;
2151 }
2152
2153 snd_hdac_ext_bus_link_get(hdev->bus, hlink);
2154
2155 hdmi_priv = devm_kzalloc(&hdev->dev, sizeof(*hdmi_priv), GFP_KERNEL);
2156 if (hdmi_priv == NULL)
2157 return -ENOMEM;
2158
2159 snd_hdac_register_chmap_ops(hdev, &hdmi_priv->chmap);
2160 hdmi_priv->chmap.ops.get_chmap = hdac_hdmi_get_chmap;
2161 hdmi_priv->chmap.ops.set_chmap = hdac_hdmi_set_chmap;
2162 hdmi_priv->chmap.ops.is_pcm_attached = is_hdac_hdmi_pcm_attached;
2163 hdmi_priv->chmap.ops.get_spk_alloc = hdac_hdmi_get_spk_alloc;
2164 hdmi_priv->hdev = hdev;
2165
2166 if (!hdac_id)
2167 return -ENODEV;
2168
2169 if (hdac_id->driver_data)
2170 hdmi_priv->drv_data =
2171 (struct hdac_hdmi_drv_data *)hdac_id->driver_data;
2172 else
2173 hdmi_priv->drv_data = &intel_drv_data;
2174
2175 dev_set_drvdata(&hdev->dev, hdmi_priv);
2176
2177 INIT_LIST_HEAD(&hdmi_priv->pin_list);
2178 INIT_LIST_HEAD(&hdmi_priv->cvt_list);
2179 INIT_LIST_HEAD(&hdmi_priv->pcm_list);
2180 mutex_init(&hdmi_priv->pin_mutex);
2181
2182
2183
2184
2185
2186 snd_hdac_display_power(hdev->bus, hdev->addr, true);
2187
2188 ret = hdac_hdmi_parse_and_map_nid(hdev, &hdmi_dais, &num_dais);
2189 if (ret < 0) {
2190 dev_err(&hdev->dev,
2191 "Failed in parse and map nid with err: %d\n", ret);
2192 return ret;
2193 }
2194 snd_hdac_refresh_widgets(hdev);
2195
2196
2197 ret = devm_snd_soc_register_component(&hdev->dev, &hdmi_hda_codec,
2198 hdmi_dais, num_dais);
2199
2200 snd_hdac_ext_bus_link_put(hdev->bus, hlink);
2201
2202 return ret;
2203 }
2204
2205 static void clear_dapm_works(struct hdac_device *hdev)
2206 {
2207 struct hdac_hdmi_priv *hdmi = hdev_to_hdmi_priv(hdev);
2208 struct hdac_hdmi_pin *pin;
2209 int i;
2210
2211 list_for_each_entry(pin, &hdmi->pin_list, head)
2212 for (i = 0; i < pin->num_ports; i++)
2213 cancel_work_sync(&pin->ports[i].dapm_work);
2214 }
2215
2216 static int hdac_hdmi_dev_remove(struct hdac_device *hdev)
2217 {
2218 clear_dapm_works(hdev);
2219 snd_hdac_display_power(hdev->bus, hdev->addr, false);
2220
2221 return 0;
2222 }
2223
2224 #ifdef CONFIG_PM
2225 static int hdac_hdmi_runtime_suspend(struct device *dev)
2226 {
2227 struct hdac_device *hdev = dev_to_hdac_dev(dev);
2228 struct hdac_bus *bus = hdev->bus;
2229 struct hdac_ext_link *hlink;
2230
2231 dev_dbg(dev, "Enter: %s\n", __func__);
2232
2233
2234 if (!bus)
2235 return 0;
2236
2237
2238
2239
2240
2241
2242
2243
2244 snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
2245 AC_PWRST_D3);
2246
2247 hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2248 if (!hlink) {
2249 dev_err(dev, "hdac link not found\n");
2250 return -EIO;
2251 }
2252
2253 snd_hdac_codec_link_down(hdev);
2254 snd_hdac_ext_bus_link_put(bus, hlink);
2255
2256 snd_hdac_display_power(bus, hdev->addr, false);
2257
2258 return 0;
2259 }
2260
2261 static int hdac_hdmi_runtime_resume(struct device *dev)
2262 {
2263 struct hdac_device *hdev = dev_to_hdac_dev(dev);
2264 struct hdac_bus *bus = hdev->bus;
2265 struct hdac_ext_link *hlink;
2266
2267 dev_dbg(dev, "Enter: %s\n", __func__);
2268
2269
2270 if (!bus)
2271 return 0;
2272
2273 hlink = snd_hdac_ext_bus_get_link(bus, dev_name(dev));
2274 if (!hlink) {
2275 dev_err(dev, "hdac link not found\n");
2276 return -EIO;
2277 }
2278
2279 snd_hdac_ext_bus_link_get(bus, hlink);
2280 snd_hdac_codec_link_up(hdev);
2281
2282 snd_hdac_display_power(bus, hdev->addr, true);
2283
2284 hdac_hdmi_skl_enable_all_pins(hdev);
2285 hdac_hdmi_skl_enable_dp12(hdev);
2286
2287
2288 snd_hdac_codec_read(hdev, hdev->afg, 0, AC_VERB_SET_POWER_STATE,
2289 AC_PWRST_D0);
2290
2291 return 0;
2292 }
2293 #else
2294 #define hdac_hdmi_runtime_suspend NULL
2295 #define hdac_hdmi_runtime_resume NULL
2296 #endif
2297
2298 static const struct dev_pm_ops hdac_hdmi_pm = {
2299 SET_RUNTIME_PM_OPS(hdac_hdmi_runtime_suspend, hdac_hdmi_runtime_resume, NULL)
2300 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, hdmi_codec_resume)
2301 };
2302
2303 static const struct hda_device_id hdmi_list[] = {
2304 HDA_CODEC_EXT_ENTRY(0x80862809, 0x100000, "Skylake HDMI", 0),
2305 HDA_CODEC_EXT_ENTRY(0x8086280a, 0x100000, "Broxton HDMI", 0),
2306 HDA_CODEC_EXT_ENTRY(0x8086280b, 0x100000, "Kabylake HDMI", 0),
2307 HDA_CODEC_EXT_ENTRY(0x8086280c, 0x100000, "Cannonlake HDMI",
2308 &intel_glk_drv_data),
2309 HDA_CODEC_EXT_ENTRY(0x8086280d, 0x100000, "Geminilake HDMI",
2310 &intel_glk_drv_data),
2311 {}
2312 };
2313
2314 MODULE_DEVICE_TABLE(hdaudio, hdmi_list);
2315
2316 static struct hdac_driver hdmi_driver = {
2317 .driver = {
2318 .name = "HDMI HDA Codec",
2319 .pm = &hdac_hdmi_pm,
2320 },
2321 .id_table = hdmi_list,
2322 .probe = hdac_hdmi_dev_probe,
2323 .remove = hdac_hdmi_dev_remove,
2324 };
2325
2326 static int __init hdmi_init(void)
2327 {
2328 return snd_hda_ext_driver_register(&hdmi_driver);
2329 }
2330
2331 static void __exit hdmi_exit(void)
2332 {
2333 snd_hda_ext_driver_unregister(&hdmi_driver);
2334 }
2335
2336 module_init(hdmi_init);
2337 module_exit(hdmi_exit);
2338
2339 MODULE_LICENSE("GPL v2");
2340 MODULE_DESCRIPTION("HDMI HD codec");
2341 MODULE_AUTHOR("Samreen Nilofer<samreen.nilofer@intel.com>");
2342 MODULE_AUTHOR("Subhransu S. Prusty<subhransu.s.prusty@intel.com>");