18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Generic routines and proc interface for ELD(EDID Like Data) information
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright(c) 2008 Intel Corporation.
68c2ecf20Sopenharmony_ci * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Authors:
98c2ecf20Sopenharmony_ci * 		Wu Fengguang <wfg@linux.intel.com>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/slab.h>
148c2ecf20Sopenharmony_ci#include <sound/core.h>
158c2ecf20Sopenharmony_ci#include <asm/unaligned.h>
168c2ecf20Sopenharmony_ci#include <sound/hda_chmap.h>
178c2ecf20Sopenharmony_ci#include <sound/hda_codec.h>
188c2ecf20Sopenharmony_ci#include "hda_local.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cienum eld_versions {
218c2ecf20Sopenharmony_ci	ELD_VER_CEA_861D	= 2,
228c2ecf20Sopenharmony_ci	ELD_VER_PARTIAL		= 31,
238c2ecf20Sopenharmony_ci};
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_cienum cea_edid_versions {
268c2ecf20Sopenharmony_ci	CEA_EDID_VER_NONE	= 0,
278c2ecf20Sopenharmony_ci	CEA_EDID_VER_CEA861	= 1,
288c2ecf20Sopenharmony_ci	CEA_EDID_VER_CEA861A	= 2,
298c2ecf20Sopenharmony_ci	CEA_EDID_VER_CEA861BCD	= 3,
308c2ecf20Sopenharmony_ci	CEA_EDID_VER_RESERVED	= 4,
318c2ecf20Sopenharmony_ci};
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic const char * const eld_connection_type_names[4] = {
348c2ecf20Sopenharmony_ci	"HDMI",
358c2ecf20Sopenharmony_ci	"DisplayPort",
368c2ecf20Sopenharmony_ci	"2-reserved",
378c2ecf20Sopenharmony_ci	"3-reserved"
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cienum cea_audio_coding_types {
418c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_REF_STREAM_HEADER	=  0,
428c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_LPCM			=  1,
438c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_AC3			=  2,
448c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_MPEG1			=  3,
458c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_MP3			=  4,
468c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_MPEG2			=  5,
478c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_AACLC			=  6,
488c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_DTS			=  7,
498c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_ATRAC			=  8,
508c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_SACD			=  9,
518c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_EAC3			= 10,
528c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_DTS_HD		= 11,
538c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_MLP			= 12,
548c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_DST			= 13,
558c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_WMAPRO		= 14,
568c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_REF_CXT		= 15,
578c2ecf20Sopenharmony_ci	/* also include valid xtypes below */
588c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_HE_AAC		= 15,
598c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_HE_AAC2		= 16,
608c2ecf20Sopenharmony_ci	AUDIO_CODING_TYPE_MPEG_SURROUND		= 17,
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cienum cea_audio_coding_xtypes {
648c2ecf20Sopenharmony_ci	AUDIO_CODING_XTYPE_HE_REF_CT		= 0,
658c2ecf20Sopenharmony_ci	AUDIO_CODING_XTYPE_HE_AAC		= 1,
668c2ecf20Sopenharmony_ci	AUDIO_CODING_XTYPE_HE_AAC2		= 2,
678c2ecf20Sopenharmony_ci	AUDIO_CODING_XTYPE_MPEG_SURROUND	= 3,
688c2ecf20Sopenharmony_ci	AUDIO_CODING_XTYPE_FIRST_RESERVED	= 4,
698c2ecf20Sopenharmony_ci};
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_cistatic const char * const cea_audio_coding_type_names[] = {
728c2ecf20Sopenharmony_ci	/*  0 */ "undefined",
738c2ecf20Sopenharmony_ci	/*  1 */ "LPCM",
748c2ecf20Sopenharmony_ci	/*  2 */ "AC-3",
758c2ecf20Sopenharmony_ci	/*  3 */ "MPEG1",
768c2ecf20Sopenharmony_ci	/*  4 */ "MP3",
778c2ecf20Sopenharmony_ci	/*  5 */ "MPEG2",
788c2ecf20Sopenharmony_ci	/*  6 */ "AAC-LC",
798c2ecf20Sopenharmony_ci	/*  7 */ "DTS",
808c2ecf20Sopenharmony_ci	/*  8 */ "ATRAC",
818c2ecf20Sopenharmony_ci	/*  9 */ "DSD (One Bit Audio)",
828c2ecf20Sopenharmony_ci	/* 10 */ "E-AC-3/DD+ (Dolby Digital Plus)",
838c2ecf20Sopenharmony_ci	/* 11 */ "DTS-HD",
848c2ecf20Sopenharmony_ci	/* 12 */ "MLP (Dolby TrueHD)",
858c2ecf20Sopenharmony_ci	/* 13 */ "DST",
868c2ecf20Sopenharmony_ci	/* 14 */ "WMAPro",
878c2ecf20Sopenharmony_ci	/* 15 */ "HE-AAC",
888c2ecf20Sopenharmony_ci	/* 16 */ "HE-AACv2",
898c2ecf20Sopenharmony_ci	/* 17 */ "MPEG Surround",
908c2ecf20Sopenharmony_ci};
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/*
938c2ecf20Sopenharmony_ci * The following two lists are shared between
948c2ecf20Sopenharmony_ci * 	- HDMI audio InfoFrame (source to sink)
958c2ecf20Sopenharmony_ci * 	- CEA E-EDID Extension (sink to source)
968c2ecf20Sopenharmony_ci */
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci/*
998c2ecf20Sopenharmony_ci * SS1:SS0 index => sample size
1008c2ecf20Sopenharmony_ci */
1018c2ecf20Sopenharmony_cistatic const int cea_sample_sizes[4] = {
1028c2ecf20Sopenharmony_ci	0,	 		/* 0: Refer to Stream Header */
1038c2ecf20Sopenharmony_ci	AC_SUPPCM_BITS_16,	/* 1: 16 bits */
1048c2ecf20Sopenharmony_ci	AC_SUPPCM_BITS_20,	/* 2: 20 bits */
1058c2ecf20Sopenharmony_ci	AC_SUPPCM_BITS_24,	/* 3: 24 bits */
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/*
1098c2ecf20Sopenharmony_ci * SF2:SF1:SF0 index => sampling frequency
1108c2ecf20Sopenharmony_ci */
1118c2ecf20Sopenharmony_cistatic const int cea_sampling_frequencies[8] = {
1128c2ecf20Sopenharmony_ci	0,			/* 0: Refer to Stream Header */
1138c2ecf20Sopenharmony_ci	SNDRV_PCM_RATE_32000,	/* 1:  32000Hz */
1148c2ecf20Sopenharmony_ci	SNDRV_PCM_RATE_44100,	/* 2:  44100Hz */
1158c2ecf20Sopenharmony_ci	SNDRV_PCM_RATE_48000,	/* 3:  48000Hz */
1168c2ecf20Sopenharmony_ci	SNDRV_PCM_RATE_88200,	/* 4:  88200Hz */
1178c2ecf20Sopenharmony_ci	SNDRV_PCM_RATE_96000,	/* 5:  96000Hz */
1188c2ecf20Sopenharmony_ci	SNDRV_PCM_RATE_176400,	/* 6: 176400Hz */
1198c2ecf20Sopenharmony_ci	SNDRV_PCM_RATE_192000,	/* 7: 192000Hz */
1208c2ecf20Sopenharmony_ci};
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistatic unsigned int hdmi_get_eld_data(struct hda_codec *codec, hda_nid_t nid,
1238c2ecf20Sopenharmony_ci					int byte_index)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	unsigned int val;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	val = snd_hda_codec_read(codec, nid, 0,
1288c2ecf20Sopenharmony_ci					AC_VERB_GET_HDMI_ELDD, byte_index);
1298c2ecf20Sopenharmony_ci#ifdef BE_PARANOID
1308c2ecf20Sopenharmony_ci	codec_info(codec, "HDMI: ELD data byte %d: 0x%x\n", byte_index, val);
1318c2ecf20Sopenharmony_ci#endif
1328c2ecf20Sopenharmony_ci	return val;
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci#define GRAB_BITS(buf, byte, lowbit, bits) 		\
1368c2ecf20Sopenharmony_ci({							\
1378c2ecf20Sopenharmony_ci	BUILD_BUG_ON(lowbit > 7);			\
1388c2ecf20Sopenharmony_ci	BUILD_BUG_ON(bits > 8);				\
1398c2ecf20Sopenharmony_ci	BUILD_BUG_ON(bits <= 0);			\
1408c2ecf20Sopenharmony_ci							\
1418c2ecf20Sopenharmony_ci	(buf[byte] >> (lowbit)) & ((1 << (bits)) - 1);	\
1428c2ecf20Sopenharmony_ci})
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_cistatic void hdmi_update_short_audio_desc(struct hda_codec *codec,
1458c2ecf20Sopenharmony_ci					 struct cea_sad *a,
1468c2ecf20Sopenharmony_ci					 const unsigned char *buf)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	int i;
1498c2ecf20Sopenharmony_ci	int val;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	val = GRAB_BITS(buf, 1, 0, 7);
1528c2ecf20Sopenharmony_ci	a->rates = 0;
1538c2ecf20Sopenharmony_ci	for (i = 0; i < 7; i++)
1548c2ecf20Sopenharmony_ci		if (val & (1 << i))
1558c2ecf20Sopenharmony_ci			a->rates |= cea_sampling_frequencies[i + 1];
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	a->channels = GRAB_BITS(buf, 0, 0, 3);
1588c2ecf20Sopenharmony_ci	a->channels++;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	a->sample_bits = 0;
1618c2ecf20Sopenharmony_ci	a->max_bitrate = 0;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	a->format = GRAB_BITS(buf, 0, 3, 4);
1648c2ecf20Sopenharmony_ci	switch (a->format) {
1658c2ecf20Sopenharmony_ci	case AUDIO_CODING_TYPE_REF_STREAM_HEADER:
1668c2ecf20Sopenharmony_ci		codec_info(codec, "HDMI: audio coding type 0 not expected\n");
1678c2ecf20Sopenharmony_ci		break;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	case AUDIO_CODING_TYPE_LPCM:
1708c2ecf20Sopenharmony_ci		val = GRAB_BITS(buf, 2, 0, 3);
1718c2ecf20Sopenharmony_ci		for (i = 0; i < 3; i++)
1728c2ecf20Sopenharmony_ci			if (val & (1 << i))
1738c2ecf20Sopenharmony_ci				a->sample_bits |= cea_sample_sizes[i + 1];
1748c2ecf20Sopenharmony_ci		break;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	case AUDIO_CODING_TYPE_AC3:
1778c2ecf20Sopenharmony_ci	case AUDIO_CODING_TYPE_MPEG1:
1788c2ecf20Sopenharmony_ci	case AUDIO_CODING_TYPE_MP3:
1798c2ecf20Sopenharmony_ci	case AUDIO_CODING_TYPE_MPEG2:
1808c2ecf20Sopenharmony_ci	case AUDIO_CODING_TYPE_AACLC:
1818c2ecf20Sopenharmony_ci	case AUDIO_CODING_TYPE_DTS:
1828c2ecf20Sopenharmony_ci	case AUDIO_CODING_TYPE_ATRAC:
1838c2ecf20Sopenharmony_ci		a->max_bitrate = GRAB_BITS(buf, 2, 0, 8);
1848c2ecf20Sopenharmony_ci		a->max_bitrate *= 8000;
1858c2ecf20Sopenharmony_ci		break;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	case AUDIO_CODING_TYPE_SACD:
1888c2ecf20Sopenharmony_ci		break;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	case AUDIO_CODING_TYPE_EAC3:
1918c2ecf20Sopenharmony_ci		break;
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	case AUDIO_CODING_TYPE_DTS_HD:
1948c2ecf20Sopenharmony_ci		break;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	case AUDIO_CODING_TYPE_MLP:
1978c2ecf20Sopenharmony_ci		break;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	case AUDIO_CODING_TYPE_DST:
2008c2ecf20Sopenharmony_ci		break;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	case AUDIO_CODING_TYPE_WMAPRO:
2038c2ecf20Sopenharmony_ci		a->profile = GRAB_BITS(buf, 2, 0, 3);
2048c2ecf20Sopenharmony_ci		break;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	case AUDIO_CODING_TYPE_REF_CXT:
2078c2ecf20Sopenharmony_ci		a->format = GRAB_BITS(buf, 2, 3, 5);
2088c2ecf20Sopenharmony_ci		if (a->format == AUDIO_CODING_XTYPE_HE_REF_CT ||
2098c2ecf20Sopenharmony_ci		    a->format >= AUDIO_CODING_XTYPE_FIRST_RESERVED) {
2108c2ecf20Sopenharmony_ci			codec_info(codec,
2118c2ecf20Sopenharmony_ci				   "HDMI: audio coding xtype %d not expected\n",
2128c2ecf20Sopenharmony_ci				   a->format);
2138c2ecf20Sopenharmony_ci			a->format = 0;
2148c2ecf20Sopenharmony_ci		} else
2158c2ecf20Sopenharmony_ci			a->format += AUDIO_CODING_TYPE_HE_AAC -
2168c2ecf20Sopenharmony_ci				     AUDIO_CODING_XTYPE_HE_AAC;
2178c2ecf20Sopenharmony_ci		break;
2188c2ecf20Sopenharmony_ci	}
2198c2ecf20Sopenharmony_ci}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci/*
2228c2ecf20Sopenharmony_ci * Be careful, ELD buf could be totally rubbish!
2238c2ecf20Sopenharmony_ci */
2248c2ecf20Sopenharmony_ciint snd_hdmi_parse_eld(struct hda_codec *codec, struct parsed_hdmi_eld *e,
2258c2ecf20Sopenharmony_ci			  const unsigned char *buf, int size)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	int mnl;
2288c2ecf20Sopenharmony_ci	int i;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	memset(e, 0, sizeof(*e));
2318c2ecf20Sopenharmony_ci	e->eld_ver = GRAB_BITS(buf, 0, 3, 5);
2328c2ecf20Sopenharmony_ci	if (e->eld_ver != ELD_VER_CEA_861D &&
2338c2ecf20Sopenharmony_ci	    e->eld_ver != ELD_VER_PARTIAL) {
2348c2ecf20Sopenharmony_ci		codec_info(codec, "HDMI: Unknown ELD version %d\n", e->eld_ver);
2358c2ecf20Sopenharmony_ci		goto out_fail;
2368c2ecf20Sopenharmony_ci	}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	e->baseline_len = GRAB_BITS(buf, 2, 0, 8);
2398c2ecf20Sopenharmony_ci	mnl		= GRAB_BITS(buf, 4, 0, 5);
2408c2ecf20Sopenharmony_ci	e->cea_edid_ver	= GRAB_BITS(buf, 4, 5, 3);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	e->support_hdcp	= GRAB_BITS(buf, 5, 0, 1);
2438c2ecf20Sopenharmony_ci	e->support_ai	= GRAB_BITS(buf, 5, 1, 1);
2448c2ecf20Sopenharmony_ci	e->conn_type	= GRAB_BITS(buf, 5, 2, 2);
2458c2ecf20Sopenharmony_ci	e->sad_count	= GRAB_BITS(buf, 5, 4, 4);
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	e->aud_synch_delay = GRAB_BITS(buf, 6, 0, 8) * 2;
2488c2ecf20Sopenharmony_ci	e->spk_alloc	= GRAB_BITS(buf, 7, 0, 7);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	e->port_id	  = get_unaligned_le64(buf + 8);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	/* not specified, but the spec's tendency is little endian */
2538c2ecf20Sopenharmony_ci	e->manufacture_id = get_unaligned_le16(buf + 16);
2548c2ecf20Sopenharmony_ci	e->product_id	  = get_unaligned_le16(buf + 18);
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	if (mnl > ELD_MAX_MNL) {
2578c2ecf20Sopenharmony_ci		codec_info(codec, "HDMI: MNL is reserved value %d\n", mnl);
2588c2ecf20Sopenharmony_ci		goto out_fail;
2598c2ecf20Sopenharmony_ci	} else if (ELD_FIXED_BYTES + mnl > size) {
2608c2ecf20Sopenharmony_ci		codec_info(codec, "HDMI: out of range MNL %d\n", mnl);
2618c2ecf20Sopenharmony_ci		goto out_fail;
2628c2ecf20Sopenharmony_ci	} else
2638c2ecf20Sopenharmony_ci		strlcpy(e->monitor_name, buf + ELD_FIXED_BYTES, mnl + 1);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	for (i = 0; i < e->sad_count; i++) {
2668c2ecf20Sopenharmony_ci		if (ELD_FIXED_BYTES + mnl + 3 * (i + 1) > size) {
2678c2ecf20Sopenharmony_ci			codec_info(codec, "HDMI: out of range SAD %d\n", i);
2688c2ecf20Sopenharmony_ci			goto out_fail;
2698c2ecf20Sopenharmony_ci		}
2708c2ecf20Sopenharmony_ci		hdmi_update_short_audio_desc(codec, e->sad + i,
2718c2ecf20Sopenharmony_ci					buf + ELD_FIXED_BYTES + mnl + 3 * i);
2728c2ecf20Sopenharmony_ci	}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	/*
2758c2ecf20Sopenharmony_ci	 * HDMI sink's ELD info cannot always be retrieved for now, e.g.
2768c2ecf20Sopenharmony_ci	 * in console or for audio devices. Assume the highest speakers
2778c2ecf20Sopenharmony_ci	 * configuration, to _not_ prohibit multi-channel audio playback.
2788c2ecf20Sopenharmony_ci	 */
2798c2ecf20Sopenharmony_ci	if (!e->spk_alloc)
2808c2ecf20Sopenharmony_ci		e->spk_alloc = 0xffff;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	return 0;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ciout_fail:
2858c2ecf20Sopenharmony_ci	return -EINVAL;
2868c2ecf20Sopenharmony_ci}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ciint snd_hdmi_get_eld_size(struct hda_codec *codec, hda_nid_t nid)
2898c2ecf20Sopenharmony_ci{
2908c2ecf20Sopenharmony_ci	return snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
2918c2ecf20Sopenharmony_ci						 AC_DIPSIZE_ELD_BUF);
2928c2ecf20Sopenharmony_ci}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ciint snd_hdmi_get_eld(struct hda_codec *codec, hda_nid_t nid,
2958c2ecf20Sopenharmony_ci		     unsigned char *buf, int *eld_size)
2968c2ecf20Sopenharmony_ci{
2978c2ecf20Sopenharmony_ci	int i;
2988c2ecf20Sopenharmony_ci	int ret = 0;
2998c2ecf20Sopenharmony_ci	int size;
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	/*
3028c2ecf20Sopenharmony_ci	 * ELD size is initialized to zero in caller function. If no errors and
3038c2ecf20Sopenharmony_ci	 * ELD is valid, actual eld_size is assigned.
3048c2ecf20Sopenharmony_ci	 */
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	size = snd_hdmi_get_eld_size(codec, nid);
3078c2ecf20Sopenharmony_ci	if (size == 0) {
3088c2ecf20Sopenharmony_ci		/* wfg: workaround for ASUS P5E-VM HDMI board */
3098c2ecf20Sopenharmony_ci		codec_info(codec, "HDMI: ELD buf size is 0, force 128\n");
3108c2ecf20Sopenharmony_ci		size = 128;
3118c2ecf20Sopenharmony_ci	}
3128c2ecf20Sopenharmony_ci	if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
3138c2ecf20Sopenharmony_ci		codec_info(codec, "HDMI: invalid ELD buf size %d\n", size);
3148c2ecf20Sopenharmony_ci		return -ERANGE;
3158c2ecf20Sopenharmony_ci	}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	/* set ELD buffer */
3188c2ecf20Sopenharmony_ci	for (i = 0; i < size; i++) {
3198c2ecf20Sopenharmony_ci		unsigned int val = hdmi_get_eld_data(codec, nid, i);
3208c2ecf20Sopenharmony_ci		/*
3218c2ecf20Sopenharmony_ci		 * Graphics driver might be writing to ELD buffer right now.
3228c2ecf20Sopenharmony_ci		 * Just abort. The caller will repoll after a while.
3238c2ecf20Sopenharmony_ci		 */
3248c2ecf20Sopenharmony_ci		if (!(val & AC_ELDD_ELD_VALID)) {
3258c2ecf20Sopenharmony_ci			codec_info(codec, "HDMI: invalid ELD data byte %d\n", i);
3268c2ecf20Sopenharmony_ci			ret = -EINVAL;
3278c2ecf20Sopenharmony_ci			goto error;
3288c2ecf20Sopenharmony_ci		}
3298c2ecf20Sopenharmony_ci		val &= AC_ELDD_ELD_DATA;
3308c2ecf20Sopenharmony_ci		/*
3318c2ecf20Sopenharmony_ci		 * The first byte cannot be zero. This can happen on some DVI
3328c2ecf20Sopenharmony_ci		 * connections. Some Intel chips may also need some 250ms delay
3338c2ecf20Sopenharmony_ci		 * to return non-zero ELD data, even when the graphics driver
3348c2ecf20Sopenharmony_ci		 * correctly writes ELD content before setting ELD_valid bit.
3358c2ecf20Sopenharmony_ci		 */
3368c2ecf20Sopenharmony_ci		if (!val && !i) {
3378c2ecf20Sopenharmony_ci			codec_dbg(codec, "HDMI: 0 ELD data\n");
3388c2ecf20Sopenharmony_ci			ret = -EINVAL;
3398c2ecf20Sopenharmony_ci			goto error;
3408c2ecf20Sopenharmony_ci		}
3418c2ecf20Sopenharmony_ci		buf[i] = val;
3428c2ecf20Sopenharmony_ci	}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	*eld_size = size;
3458c2ecf20Sopenharmony_cierror:
3468c2ecf20Sopenharmony_ci	return ret;
3478c2ecf20Sopenharmony_ci}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci/*
3508c2ecf20Sopenharmony_ci * SNDRV_PCM_RATE_* and AC_PAR_PCM values don't match, print correct rates with
3518c2ecf20Sopenharmony_ci * hdmi-specific routine.
3528c2ecf20Sopenharmony_ci */
3538c2ecf20Sopenharmony_cistatic void hdmi_print_pcm_rates(int pcm, char *buf, int buflen)
3548c2ecf20Sopenharmony_ci{
3558c2ecf20Sopenharmony_ci	static const unsigned int alsa_rates[] = {
3568c2ecf20Sopenharmony_ci		5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
3578c2ecf20Sopenharmony_ci		88200, 96000, 176400, 192000, 384000
3588c2ecf20Sopenharmony_ci	};
3598c2ecf20Sopenharmony_ci	int i, j;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	for (i = 0, j = 0; i < ARRAY_SIZE(alsa_rates); i++)
3628c2ecf20Sopenharmony_ci		if (pcm & (1 << i))
3638c2ecf20Sopenharmony_ci			j += scnprintf(buf + j, buflen - j,  " %d",
3648c2ecf20Sopenharmony_ci				alsa_rates[i]);
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	buf[j] = '\0'; /* necessary when j == 0 */
3678c2ecf20Sopenharmony_ci}
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci#define SND_PRINT_RATES_ADVISED_BUFSIZE	80
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_cistatic void hdmi_show_short_audio_desc(struct hda_codec *codec,
3728c2ecf20Sopenharmony_ci				       struct cea_sad *a)
3738c2ecf20Sopenharmony_ci{
3748c2ecf20Sopenharmony_ci	char buf[SND_PRINT_RATES_ADVISED_BUFSIZE];
3758c2ecf20Sopenharmony_ci	char buf2[8 + SND_PRINT_BITS_ADVISED_BUFSIZE] = ", bits =";
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	if (!a->format)
3788c2ecf20Sopenharmony_ci		return;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	hdmi_print_pcm_rates(a->rates, buf, sizeof(buf));
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci	if (a->format == AUDIO_CODING_TYPE_LPCM)
3838c2ecf20Sopenharmony_ci		snd_print_pcm_bits(a->sample_bits, buf2 + 8, sizeof(buf2) - 8);
3848c2ecf20Sopenharmony_ci	else if (a->max_bitrate)
3858c2ecf20Sopenharmony_ci		snprintf(buf2, sizeof(buf2),
3868c2ecf20Sopenharmony_ci				", max bitrate = %d", a->max_bitrate);
3878c2ecf20Sopenharmony_ci	else
3888c2ecf20Sopenharmony_ci		buf2[0] = '\0';
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	codec_dbg(codec,
3918c2ecf20Sopenharmony_ci		  "HDMI: supports coding type %s: channels = %d, rates =%s%s\n",
3928c2ecf20Sopenharmony_ci		  cea_audio_coding_type_names[a->format],
3938c2ecf20Sopenharmony_ci		  a->channels, buf, buf2);
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_civoid snd_hdmi_show_eld(struct hda_codec *codec, struct parsed_hdmi_eld *e)
3978c2ecf20Sopenharmony_ci{
3988c2ecf20Sopenharmony_ci	int i;
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	codec_dbg(codec, "HDMI: detected monitor %s at connection type %s\n",
4018c2ecf20Sopenharmony_ci			e->monitor_name,
4028c2ecf20Sopenharmony_ci			eld_connection_type_names[e->conn_type]);
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	if (e->spk_alloc) {
4058c2ecf20Sopenharmony_ci		char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
4068c2ecf20Sopenharmony_ci		snd_hdac_print_channel_allocation(e->spk_alloc, buf, sizeof(buf));
4078c2ecf20Sopenharmony_ci		codec_dbg(codec, "HDMI: available speakers:%s\n", buf);
4088c2ecf20Sopenharmony_ci	}
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	for (i = 0; i < e->sad_count; i++)
4118c2ecf20Sopenharmony_ci		hdmi_show_short_audio_desc(codec, e->sad + i);
4128c2ecf20Sopenharmony_ci}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci#ifdef CONFIG_SND_PROC_FS
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_cistatic void hdmi_print_sad_info(int i, struct cea_sad *a,
4178c2ecf20Sopenharmony_ci				struct snd_info_buffer *buffer)
4188c2ecf20Sopenharmony_ci{
4198c2ecf20Sopenharmony_ci	char buf[SND_PRINT_RATES_ADVISED_BUFSIZE];
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "sad%d_coding_type\t[0x%x] %s\n",
4228c2ecf20Sopenharmony_ci			i, a->format, cea_audio_coding_type_names[a->format]);
4238c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "sad%d_channels\t\t%d\n", i, a->channels);
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	hdmi_print_pcm_rates(a->rates, buf, sizeof(buf));
4268c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "sad%d_rates\t\t[0x%x]%s\n", i, a->rates, buf);
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	if (a->format == AUDIO_CODING_TYPE_LPCM) {
4298c2ecf20Sopenharmony_ci		snd_print_pcm_bits(a->sample_bits, buf, sizeof(buf));
4308c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "sad%d_bits\t\t[0x%x]%s\n",
4318c2ecf20Sopenharmony_ci							i, a->sample_bits, buf);
4328c2ecf20Sopenharmony_ci	}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	if (a->max_bitrate)
4358c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "sad%d_max_bitrate\t%d\n",
4368c2ecf20Sopenharmony_ci							i, a->max_bitrate);
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	if (a->profile)
4398c2ecf20Sopenharmony_ci		snd_iprintf(buffer, "sad%d_profile\t\t%d\n", i, a->profile);
4408c2ecf20Sopenharmony_ci}
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_civoid snd_hdmi_print_eld_info(struct hdmi_eld *eld,
4438c2ecf20Sopenharmony_ci			     struct snd_info_buffer *buffer)
4448c2ecf20Sopenharmony_ci{
4458c2ecf20Sopenharmony_ci	struct parsed_hdmi_eld *e = &eld->info;
4468c2ecf20Sopenharmony_ci	char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
4478c2ecf20Sopenharmony_ci	int i;
4488c2ecf20Sopenharmony_ci	static const char * const eld_version_names[32] = {
4498c2ecf20Sopenharmony_ci		"reserved",
4508c2ecf20Sopenharmony_ci		"reserved",
4518c2ecf20Sopenharmony_ci		"CEA-861D or below",
4528c2ecf20Sopenharmony_ci		[3 ... 30] = "reserved",
4538c2ecf20Sopenharmony_ci		[31] = "partial"
4548c2ecf20Sopenharmony_ci	};
4558c2ecf20Sopenharmony_ci	static const char * const cea_edid_version_names[8] = {
4568c2ecf20Sopenharmony_ci		"no CEA EDID Timing Extension block present",
4578c2ecf20Sopenharmony_ci		"CEA-861",
4588c2ecf20Sopenharmony_ci		"CEA-861-A",
4598c2ecf20Sopenharmony_ci		"CEA-861-B, C or D",
4608c2ecf20Sopenharmony_ci		[4 ... 7] = "reserved"
4618c2ecf20Sopenharmony_ci	};
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "monitor_present\t\t%d\n", eld->monitor_present);
4648c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "eld_valid\t\t%d\n", eld->eld_valid);
4658c2ecf20Sopenharmony_ci	if (!eld->eld_valid)
4668c2ecf20Sopenharmony_ci		return;
4678c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "monitor_name\t\t%s\n", e->monitor_name);
4688c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "connection_type\t\t%s\n",
4698c2ecf20Sopenharmony_ci				eld_connection_type_names[e->conn_type]);
4708c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "eld_version\t\t[0x%x] %s\n", e->eld_ver,
4718c2ecf20Sopenharmony_ci					eld_version_names[e->eld_ver]);
4728c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "edid_version\t\t[0x%x] %s\n", e->cea_edid_ver,
4738c2ecf20Sopenharmony_ci				cea_edid_version_names[e->cea_edid_ver]);
4748c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "manufacture_id\t\t0x%x\n", e->manufacture_id);
4758c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "product_id\t\t0x%x\n", e->product_id);
4768c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "port_id\t\t\t0x%llx\n", (long long)e->port_id);
4778c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "support_hdcp\t\t%d\n", e->support_hdcp);
4788c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "support_ai\t\t%d\n", e->support_ai);
4798c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "audio_sync_delay\t%d\n", e->aud_synch_delay);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	snd_hdac_print_channel_allocation(e->spk_alloc, buf, sizeof(buf));
4828c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "speakers\t\t[0x%x]%s\n", e->spk_alloc, buf);
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	snd_iprintf(buffer, "sad_count\t\t%d\n", e->sad_count);
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	for (i = 0; i < e->sad_count; i++)
4878c2ecf20Sopenharmony_ci		hdmi_print_sad_info(i, e->sad + i, buffer);
4888c2ecf20Sopenharmony_ci}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_civoid snd_hdmi_write_eld_info(struct hdmi_eld *eld,
4918c2ecf20Sopenharmony_ci			     struct snd_info_buffer *buffer)
4928c2ecf20Sopenharmony_ci{
4938c2ecf20Sopenharmony_ci	struct parsed_hdmi_eld *e = &eld->info;
4948c2ecf20Sopenharmony_ci	char line[64];
4958c2ecf20Sopenharmony_ci	char name[64];
4968c2ecf20Sopenharmony_ci	char *sname;
4978c2ecf20Sopenharmony_ci	long long val;
4988c2ecf20Sopenharmony_ci	unsigned int n;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	while (!snd_info_get_line(buffer, line, sizeof(line))) {
5018c2ecf20Sopenharmony_ci		if (sscanf(line, "%s %llx", name, &val) != 2)
5028c2ecf20Sopenharmony_ci			continue;
5038c2ecf20Sopenharmony_ci		/*
5048c2ecf20Sopenharmony_ci		 * We don't allow modification to these fields:
5058c2ecf20Sopenharmony_ci		 * 	monitor_name manufacture_id product_id
5068c2ecf20Sopenharmony_ci		 * 	eld_version edid_version
5078c2ecf20Sopenharmony_ci		 */
5088c2ecf20Sopenharmony_ci		if (!strcmp(name, "monitor_present"))
5098c2ecf20Sopenharmony_ci			eld->monitor_present = val;
5108c2ecf20Sopenharmony_ci		else if (!strcmp(name, "eld_valid"))
5118c2ecf20Sopenharmony_ci			eld->eld_valid = val;
5128c2ecf20Sopenharmony_ci		else if (!strcmp(name, "connection_type"))
5138c2ecf20Sopenharmony_ci			e->conn_type = val;
5148c2ecf20Sopenharmony_ci		else if (!strcmp(name, "port_id"))
5158c2ecf20Sopenharmony_ci			e->port_id = val;
5168c2ecf20Sopenharmony_ci		else if (!strcmp(name, "support_hdcp"))
5178c2ecf20Sopenharmony_ci			e->support_hdcp = val;
5188c2ecf20Sopenharmony_ci		else if (!strcmp(name, "support_ai"))
5198c2ecf20Sopenharmony_ci			e->support_ai = val;
5208c2ecf20Sopenharmony_ci		else if (!strcmp(name, "audio_sync_delay"))
5218c2ecf20Sopenharmony_ci			e->aud_synch_delay = val;
5228c2ecf20Sopenharmony_ci		else if (!strcmp(name, "speakers"))
5238c2ecf20Sopenharmony_ci			e->spk_alloc = val;
5248c2ecf20Sopenharmony_ci		else if (!strcmp(name, "sad_count"))
5258c2ecf20Sopenharmony_ci			e->sad_count = val;
5268c2ecf20Sopenharmony_ci		else if (!strncmp(name, "sad", 3)) {
5278c2ecf20Sopenharmony_ci			sname = name + 4;
5288c2ecf20Sopenharmony_ci			n = name[3] - '0';
5298c2ecf20Sopenharmony_ci			if (name[4] >= '0' && name[4] <= '9') {
5308c2ecf20Sopenharmony_ci				sname++;
5318c2ecf20Sopenharmony_ci				n = 10 * n + name[4] - '0';
5328c2ecf20Sopenharmony_ci			}
5338c2ecf20Sopenharmony_ci			if (n >= ELD_MAX_SAD)
5348c2ecf20Sopenharmony_ci				continue;
5358c2ecf20Sopenharmony_ci			if (!strcmp(sname, "_coding_type"))
5368c2ecf20Sopenharmony_ci				e->sad[n].format = val;
5378c2ecf20Sopenharmony_ci			else if (!strcmp(sname, "_channels"))
5388c2ecf20Sopenharmony_ci				e->sad[n].channels = val;
5398c2ecf20Sopenharmony_ci			else if (!strcmp(sname, "_rates"))
5408c2ecf20Sopenharmony_ci				e->sad[n].rates = val;
5418c2ecf20Sopenharmony_ci			else if (!strcmp(sname, "_bits"))
5428c2ecf20Sopenharmony_ci				e->sad[n].sample_bits = val;
5438c2ecf20Sopenharmony_ci			else if (!strcmp(sname, "_max_bitrate"))
5448c2ecf20Sopenharmony_ci				e->sad[n].max_bitrate = val;
5458c2ecf20Sopenharmony_ci			else if (!strcmp(sname, "_profile"))
5468c2ecf20Sopenharmony_ci				e->sad[n].profile = val;
5478c2ecf20Sopenharmony_ci			if (n >= e->sad_count)
5488c2ecf20Sopenharmony_ci				e->sad_count = n + 1;
5498c2ecf20Sopenharmony_ci		}
5508c2ecf20Sopenharmony_ci	}
5518c2ecf20Sopenharmony_ci}
5528c2ecf20Sopenharmony_ci#endif /* CONFIG_SND_PROC_FS */
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci/* update PCM info based on ELD */
5558c2ecf20Sopenharmony_civoid snd_hdmi_eld_update_pcm_info(struct parsed_hdmi_eld *e,
5568c2ecf20Sopenharmony_ci			      struct hda_pcm_stream *hinfo)
5578c2ecf20Sopenharmony_ci{
5588c2ecf20Sopenharmony_ci	u32 rates;
5598c2ecf20Sopenharmony_ci	u64 formats;
5608c2ecf20Sopenharmony_ci	unsigned int maxbps;
5618c2ecf20Sopenharmony_ci	unsigned int channels_max;
5628c2ecf20Sopenharmony_ci	int i;
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	/* assume basic audio support (the basic audio flag is not in ELD;
5658c2ecf20Sopenharmony_ci	 * however, all audio capable sinks are required to support basic
5668c2ecf20Sopenharmony_ci	 * audio) */
5678c2ecf20Sopenharmony_ci	rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
5688c2ecf20Sopenharmony_ci		SNDRV_PCM_RATE_48000;
5698c2ecf20Sopenharmony_ci	formats = SNDRV_PCM_FMTBIT_S16_LE;
5708c2ecf20Sopenharmony_ci	maxbps = 16;
5718c2ecf20Sopenharmony_ci	channels_max = 2;
5728c2ecf20Sopenharmony_ci	for (i = 0; i < e->sad_count; i++) {
5738c2ecf20Sopenharmony_ci		struct cea_sad *a = &e->sad[i];
5748c2ecf20Sopenharmony_ci		rates |= a->rates;
5758c2ecf20Sopenharmony_ci		if (a->channels > channels_max)
5768c2ecf20Sopenharmony_ci			channels_max = a->channels;
5778c2ecf20Sopenharmony_ci		if (a->format == AUDIO_CODING_TYPE_LPCM) {
5788c2ecf20Sopenharmony_ci			if (a->sample_bits & AC_SUPPCM_BITS_20) {
5798c2ecf20Sopenharmony_ci				formats |= SNDRV_PCM_FMTBIT_S32_LE;
5808c2ecf20Sopenharmony_ci				if (maxbps < 20)
5818c2ecf20Sopenharmony_ci					maxbps = 20;
5828c2ecf20Sopenharmony_ci			}
5838c2ecf20Sopenharmony_ci			if (a->sample_bits & AC_SUPPCM_BITS_24) {
5848c2ecf20Sopenharmony_ci				formats |= SNDRV_PCM_FMTBIT_S32_LE;
5858c2ecf20Sopenharmony_ci				if (maxbps < 24)
5868c2ecf20Sopenharmony_ci					maxbps = 24;
5878c2ecf20Sopenharmony_ci			}
5888c2ecf20Sopenharmony_ci		}
5898c2ecf20Sopenharmony_ci	}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	/* restrict the parameters by the values the codec provides */
5928c2ecf20Sopenharmony_ci	hinfo->rates &= rates;
5938c2ecf20Sopenharmony_ci	hinfo->formats &= formats;
5948c2ecf20Sopenharmony_ci	hinfo->maxbps = min(hinfo->maxbps, maxbps);
5958c2ecf20Sopenharmony_ci	hinfo->channels_max = min(hinfo->channels_max, channels_max);
5968c2ecf20Sopenharmony_ci}
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci/* ATI/AMD specific stuff (ELD emulation) */
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci#define ATI_VERB_SET_AUDIO_DESCRIPTOR	0x776
6028c2ecf20Sopenharmony_ci#define ATI_VERB_SET_SINK_INFO_INDEX	0x780
6038c2ecf20Sopenharmony_ci#define ATI_VERB_GET_SPEAKER_ALLOCATION	0xf70
6048c2ecf20Sopenharmony_ci#define ATI_VERB_GET_AUDIO_DESCRIPTOR	0xf76
6058c2ecf20Sopenharmony_ci#define ATI_VERB_GET_AUDIO_VIDEO_DELAY	0xf7b
6068c2ecf20Sopenharmony_ci#define ATI_VERB_GET_SINK_INFO_INDEX	0xf80
6078c2ecf20Sopenharmony_ci#define ATI_VERB_GET_SINK_INFO_DATA	0xf81
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci#define ATI_SPKALLOC_SPKALLOC		0x007f
6108c2ecf20Sopenharmony_ci#define ATI_SPKALLOC_TYPE_HDMI		0x0100
6118c2ecf20Sopenharmony_ci#define ATI_SPKALLOC_TYPE_DISPLAYPORT	0x0200
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci/* first three bytes are just standard SAD */
6148c2ecf20Sopenharmony_ci#define ATI_AUDIODESC_CHANNELS		0x00000007
6158c2ecf20Sopenharmony_ci#define ATI_AUDIODESC_RATES		0x0000ff00
6168c2ecf20Sopenharmony_ci#define ATI_AUDIODESC_LPCM_STEREO_RATES	0xff000000
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci/* in standard HDMI VSDB format */
6198c2ecf20Sopenharmony_ci#define ATI_DELAY_VIDEO_LATENCY		0x000000ff
6208c2ecf20Sopenharmony_ci#define ATI_DELAY_AUDIO_LATENCY		0x0000ff00
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_cienum ati_sink_info_idx {
6238c2ecf20Sopenharmony_ci	ATI_INFO_IDX_MANUFACTURER_ID	= 0,
6248c2ecf20Sopenharmony_ci	ATI_INFO_IDX_PRODUCT_ID		= 1,
6258c2ecf20Sopenharmony_ci	ATI_INFO_IDX_SINK_DESC_LEN	= 2,
6268c2ecf20Sopenharmony_ci	ATI_INFO_IDX_PORT_ID_LOW	= 3,
6278c2ecf20Sopenharmony_ci	ATI_INFO_IDX_PORT_ID_HIGH	= 4,
6288c2ecf20Sopenharmony_ci	ATI_INFO_IDX_SINK_DESC_FIRST	= 5,
6298c2ecf20Sopenharmony_ci	ATI_INFO_IDX_SINK_DESC_LAST	= 22, /* max len 18 bytes */
6308c2ecf20Sopenharmony_ci};
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ciint snd_hdmi_get_eld_ati(struct hda_codec *codec, hda_nid_t nid,
6338c2ecf20Sopenharmony_ci			 unsigned char *buf, int *eld_size, bool rev3_or_later)
6348c2ecf20Sopenharmony_ci{
6358c2ecf20Sopenharmony_ci	int spkalloc, ati_sad, aud_synch;
6368c2ecf20Sopenharmony_ci	int sink_desc_len = 0;
6378c2ecf20Sopenharmony_ci	int pos, i;
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_ci	/* ATI/AMD does not have ELD, emulate it */
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	spkalloc = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SPEAKER_ALLOCATION, 0);
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	if (spkalloc <= 0) {
6448c2ecf20Sopenharmony_ci		codec_info(codec, "HDMI ATI/AMD: no speaker allocation for ELD\n");
6458c2ecf20Sopenharmony_ci		return -EINVAL;
6468c2ecf20Sopenharmony_ci	}
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	memset(buf, 0, ELD_FIXED_BYTES + ELD_MAX_MNL + ELD_MAX_SAD * 3);
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	/* version */
6518c2ecf20Sopenharmony_ci	buf[0] = ELD_VER_CEA_861D << 3;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	/* speaker allocation from EDID */
6548c2ecf20Sopenharmony_ci	buf[7] = spkalloc & ATI_SPKALLOC_SPKALLOC;
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	/* is DisplayPort? */
6578c2ecf20Sopenharmony_ci	if (spkalloc & ATI_SPKALLOC_TYPE_DISPLAYPORT)
6588c2ecf20Sopenharmony_ci		buf[5] |= 0x04;
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	pos = ELD_FIXED_BYTES;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	if (rev3_or_later) {
6638c2ecf20Sopenharmony_ci		int sink_info;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci		snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_PORT_ID_LOW);
6668c2ecf20Sopenharmony_ci		sink_info = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
6678c2ecf20Sopenharmony_ci		put_unaligned_le32(sink_info, buf + 8);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci		snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_PORT_ID_HIGH);
6708c2ecf20Sopenharmony_ci		sink_info = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
6718c2ecf20Sopenharmony_ci		put_unaligned_le32(sink_info, buf + 12);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci		snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_MANUFACTURER_ID);
6748c2ecf20Sopenharmony_ci		sink_info = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
6758c2ecf20Sopenharmony_ci		put_unaligned_le16(sink_info, buf + 16);
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci		snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_PRODUCT_ID);
6788c2ecf20Sopenharmony_ci		sink_info = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
6798c2ecf20Sopenharmony_ci		put_unaligned_le16(sink_info, buf + 18);
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_ci		snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_SINK_DESC_LEN);
6828c2ecf20Sopenharmony_ci		sink_desc_len = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci		if (sink_desc_len > ELD_MAX_MNL) {
6858c2ecf20Sopenharmony_ci			codec_info(codec, "HDMI ATI/AMD: Truncating HDMI sink description with length %d\n",
6868c2ecf20Sopenharmony_ci				   sink_desc_len);
6878c2ecf20Sopenharmony_ci			sink_desc_len = ELD_MAX_MNL;
6888c2ecf20Sopenharmony_ci		}
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci		buf[4] |= sink_desc_len;
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci		for (i = 0; i < sink_desc_len; i++) {
6938c2ecf20Sopenharmony_ci			snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_SINK_DESC_FIRST + i);
6948c2ecf20Sopenharmony_ci			buf[pos++] = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
6958c2ecf20Sopenharmony_ci		}
6968c2ecf20Sopenharmony_ci	}
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	for (i = AUDIO_CODING_TYPE_LPCM; i <= AUDIO_CODING_TYPE_WMAPRO; i++) {
6998c2ecf20Sopenharmony_ci		if (i == AUDIO_CODING_TYPE_SACD || i == AUDIO_CODING_TYPE_DST)
7008c2ecf20Sopenharmony_ci			continue; /* not handled by ATI/AMD */
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci		snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_AUDIO_DESCRIPTOR, i << 3);
7038c2ecf20Sopenharmony_ci		ati_sad = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_AUDIO_DESCRIPTOR, 0);
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci		if (ati_sad <= 0)
7068c2ecf20Sopenharmony_ci			continue;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci		if (ati_sad & ATI_AUDIODESC_RATES) {
7098c2ecf20Sopenharmony_ci			/* format is supported, copy SAD as-is */
7108c2ecf20Sopenharmony_ci			buf[pos++] = (ati_sad & 0x0000ff) >> 0;
7118c2ecf20Sopenharmony_ci			buf[pos++] = (ati_sad & 0x00ff00) >> 8;
7128c2ecf20Sopenharmony_ci			buf[pos++] = (ati_sad & 0xff0000) >> 16;
7138c2ecf20Sopenharmony_ci		}
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci		if (i == AUDIO_CODING_TYPE_LPCM
7168c2ecf20Sopenharmony_ci		    && (ati_sad & ATI_AUDIODESC_LPCM_STEREO_RATES)
7178c2ecf20Sopenharmony_ci		    && (ati_sad & ATI_AUDIODESC_LPCM_STEREO_RATES) >> 16 != (ati_sad & ATI_AUDIODESC_RATES)) {
7188c2ecf20Sopenharmony_ci			/* for PCM there is a separate stereo rate mask */
7198c2ecf20Sopenharmony_ci			buf[pos++] = ((ati_sad & 0x000000ff) & ~ATI_AUDIODESC_CHANNELS) | 0x1;
7208c2ecf20Sopenharmony_ci			/* rates from the extra byte */
7218c2ecf20Sopenharmony_ci			buf[pos++] = (ati_sad & 0xff000000) >> 24;
7228c2ecf20Sopenharmony_ci			buf[pos++] = (ati_sad & 0x00ff0000) >> 16;
7238c2ecf20Sopenharmony_ci		}
7248c2ecf20Sopenharmony_ci	}
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	if (pos == ELD_FIXED_BYTES + sink_desc_len) {
7278c2ecf20Sopenharmony_ci		codec_info(codec, "HDMI ATI/AMD: no audio descriptors for ELD\n");
7288c2ecf20Sopenharmony_ci		return -EINVAL;
7298c2ecf20Sopenharmony_ci	}
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	/*
7328c2ecf20Sopenharmony_ci	 * HDMI VSDB latency format:
7338c2ecf20Sopenharmony_ci	 * separately for both audio and video:
7348c2ecf20Sopenharmony_ci	 *  0          field not valid or unknown latency
7358c2ecf20Sopenharmony_ci	 *  [1..251]   msecs = (x-1)*2  (max 500ms with x = 251 = 0xfb)
7368c2ecf20Sopenharmony_ci	 *  255        audio/video not supported
7378c2ecf20Sopenharmony_ci	 *
7388c2ecf20Sopenharmony_ci	 * HDA latency format:
7398c2ecf20Sopenharmony_ci	 * single value indicating video latency relative to audio:
7408c2ecf20Sopenharmony_ci	 *  0          unknown or 0ms
7418c2ecf20Sopenharmony_ci	 *  [1..250]   msecs = x*2  (max 500ms with x = 250 = 0xfa)
7428c2ecf20Sopenharmony_ci	 *  [251..255] reserved
7438c2ecf20Sopenharmony_ci	 */
7448c2ecf20Sopenharmony_ci	aud_synch = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_AUDIO_VIDEO_DELAY, 0);
7458c2ecf20Sopenharmony_ci	if ((aud_synch & ATI_DELAY_VIDEO_LATENCY) && (aud_synch & ATI_DELAY_AUDIO_LATENCY)) {
7468c2ecf20Sopenharmony_ci		int video_latency_hdmi = (aud_synch & ATI_DELAY_VIDEO_LATENCY);
7478c2ecf20Sopenharmony_ci		int audio_latency_hdmi = (aud_synch & ATI_DELAY_AUDIO_LATENCY) >> 8;
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci		if (video_latency_hdmi <= 0xfb && audio_latency_hdmi <= 0xfb &&
7508c2ecf20Sopenharmony_ci		    video_latency_hdmi > audio_latency_hdmi)
7518c2ecf20Sopenharmony_ci			buf[6] = video_latency_hdmi - audio_latency_hdmi;
7528c2ecf20Sopenharmony_ci		/* else unknown/invalid or 0ms or video ahead of audio, so use zero */
7538c2ecf20Sopenharmony_ci	}
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	/* SAD count */
7568c2ecf20Sopenharmony_ci	buf[5] |= ((pos - ELD_FIXED_BYTES - sink_desc_len) / 3) << 4;
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	/* Baseline ELD block length is 4-byte aligned */
7598c2ecf20Sopenharmony_ci	pos = round_up(pos, 4);
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	/* Baseline ELD length (4-byte header is not counted in) */
7628c2ecf20Sopenharmony_ci	buf[2] = (pos - 4) / 4;
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	*eld_size = pos;
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	return 0;
7678c2ecf20Sopenharmony_ci}
768