hda_eld.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Generic routines and proc interface for ELD(EDID Like Data) information
  4. *
  5. * Copyright(c) 2008 Intel Corporation.
  6. * Copyright (c) 2013 Anssi Hannula <[email protected]>
  7. *
  8. * Authors:
  9. * Wu Fengguang <[email protected]>
  10. */
  11. #include <linux/init.h>
  12. #include <linux/slab.h>
  13. #include <sound/core.h>
  14. #include <asm/unaligned.h>
  15. #include <sound/hda_chmap.h>
  16. #include <sound/hda_codec.h>
  17. #include "hda_local.h"
  18. enum eld_versions {
  19. ELD_VER_CEA_861D = 2,
  20. ELD_VER_PARTIAL = 31,
  21. };
  22. enum cea_edid_versions {
  23. CEA_EDID_VER_NONE = 0,
  24. CEA_EDID_VER_CEA861 = 1,
  25. CEA_EDID_VER_CEA861A = 2,
  26. CEA_EDID_VER_CEA861BCD = 3,
  27. CEA_EDID_VER_RESERVED = 4,
  28. };
  29. static const char * const eld_connection_type_names[4] = {
  30. "HDMI",
  31. "DisplayPort",
  32. "2-reserved",
  33. "3-reserved"
  34. };
  35. enum cea_audio_coding_types {
  36. AUDIO_CODING_TYPE_REF_STREAM_HEADER = 0,
  37. AUDIO_CODING_TYPE_LPCM = 1,
  38. AUDIO_CODING_TYPE_AC3 = 2,
  39. AUDIO_CODING_TYPE_MPEG1 = 3,
  40. AUDIO_CODING_TYPE_MP3 = 4,
  41. AUDIO_CODING_TYPE_MPEG2 = 5,
  42. AUDIO_CODING_TYPE_AACLC = 6,
  43. AUDIO_CODING_TYPE_DTS = 7,
  44. AUDIO_CODING_TYPE_ATRAC = 8,
  45. AUDIO_CODING_TYPE_SACD = 9,
  46. AUDIO_CODING_TYPE_EAC3 = 10,
  47. AUDIO_CODING_TYPE_DTS_HD = 11,
  48. AUDIO_CODING_TYPE_MLP = 12,
  49. AUDIO_CODING_TYPE_DST = 13,
  50. AUDIO_CODING_TYPE_WMAPRO = 14,
  51. AUDIO_CODING_TYPE_REF_CXT = 15,
  52. /* also include valid xtypes below */
  53. AUDIO_CODING_TYPE_HE_AAC = 15,
  54. AUDIO_CODING_TYPE_HE_AAC2 = 16,
  55. AUDIO_CODING_TYPE_MPEG_SURROUND = 17,
  56. };
  57. enum cea_audio_coding_xtypes {
  58. AUDIO_CODING_XTYPE_HE_REF_CT = 0,
  59. AUDIO_CODING_XTYPE_HE_AAC = 1,
  60. AUDIO_CODING_XTYPE_HE_AAC2 = 2,
  61. AUDIO_CODING_XTYPE_MPEG_SURROUND = 3,
  62. AUDIO_CODING_XTYPE_FIRST_RESERVED = 4,
  63. };
  64. static const char * const cea_audio_coding_type_names[] = {
  65. /* 0 */ "undefined",
  66. /* 1 */ "LPCM",
  67. /* 2 */ "AC-3",
  68. /* 3 */ "MPEG1",
  69. /* 4 */ "MP3",
  70. /* 5 */ "MPEG2",
  71. /* 6 */ "AAC-LC",
  72. /* 7 */ "DTS",
  73. /* 8 */ "ATRAC",
  74. /* 9 */ "DSD (One Bit Audio)",
  75. /* 10 */ "E-AC-3/DD+ (Dolby Digital Plus)",
  76. /* 11 */ "DTS-HD",
  77. /* 12 */ "MLP (Dolby TrueHD)",
  78. /* 13 */ "DST",
  79. /* 14 */ "WMAPro",
  80. /* 15 */ "HE-AAC",
  81. /* 16 */ "HE-AACv2",
  82. /* 17 */ "MPEG Surround",
  83. };
  84. /*
  85. * The following two lists are shared between
  86. * - HDMI audio InfoFrame (source to sink)
  87. * - CEA E-EDID Extension (sink to source)
  88. */
  89. /*
  90. * SS1:SS0 index => sample size
  91. */
  92. static const int cea_sample_sizes[4] = {
  93. 0, /* 0: Refer to Stream Header */
  94. AC_SUPPCM_BITS_16, /* 1: 16 bits */
  95. AC_SUPPCM_BITS_20, /* 2: 20 bits */
  96. AC_SUPPCM_BITS_24, /* 3: 24 bits */
  97. };
  98. /*
  99. * SF2:SF1:SF0 index => sampling frequency
  100. */
  101. static const int cea_sampling_frequencies[8] = {
  102. 0, /* 0: Refer to Stream Header */
  103. SNDRV_PCM_RATE_32000, /* 1: 32000Hz */
  104. SNDRV_PCM_RATE_44100, /* 2: 44100Hz */
  105. SNDRV_PCM_RATE_48000, /* 3: 48000Hz */
  106. SNDRV_PCM_RATE_88200, /* 4: 88200Hz */
  107. SNDRV_PCM_RATE_96000, /* 5: 96000Hz */
  108. SNDRV_PCM_RATE_176400, /* 6: 176400Hz */
  109. SNDRV_PCM_RATE_192000, /* 7: 192000Hz */
  110. };
  111. static unsigned int hdmi_get_eld_data(struct hda_codec *codec, hda_nid_t nid,
  112. int byte_index)
  113. {
  114. unsigned int val;
  115. val = snd_hda_codec_read(codec, nid, 0,
  116. AC_VERB_GET_HDMI_ELDD, byte_index);
  117. #ifdef BE_PARANOID
  118. codec_info(codec, "HDMI: ELD data byte %d: 0x%x\n", byte_index, val);
  119. #endif
  120. return val;
  121. }
  122. #define GRAB_BITS(buf, byte, lowbit, bits) \
  123. ({ \
  124. BUILD_BUG_ON(lowbit > 7); \
  125. BUILD_BUG_ON(bits > 8); \
  126. BUILD_BUG_ON(bits <= 0); \
  127. \
  128. (buf[byte] >> (lowbit)) & ((1 << (bits)) - 1); \
  129. })
  130. static void hdmi_update_short_audio_desc(struct hda_codec *codec,
  131. struct cea_sad *a,
  132. const unsigned char *buf)
  133. {
  134. int i;
  135. int val;
  136. val = GRAB_BITS(buf, 1, 0, 7);
  137. a->rates = 0;
  138. for (i = 0; i < 7; i++)
  139. if (val & (1 << i))
  140. a->rates |= cea_sampling_frequencies[i + 1];
  141. a->channels = GRAB_BITS(buf, 0, 0, 3);
  142. a->channels++;
  143. a->sample_bits = 0;
  144. a->max_bitrate = 0;
  145. a->format = GRAB_BITS(buf, 0, 3, 4);
  146. switch (a->format) {
  147. case AUDIO_CODING_TYPE_REF_STREAM_HEADER:
  148. codec_info(codec, "HDMI: audio coding type 0 not expected\n");
  149. break;
  150. case AUDIO_CODING_TYPE_LPCM:
  151. val = GRAB_BITS(buf, 2, 0, 3);
  152. for (i = 0; i < 3; i++)
  153. if (val & (1 << i))
  154. a->sample_bits |= cea_sample_sizes[i + 1];
  155. break;
  156. case AUDIO_CODING_TYPE_AC3:
  157. case AUDIO_CODING_TYPE_MPEG1:
  158. case AUDIO_CODING_TYPE_MP3:
  159. case AUDIO_CODING_TYPE_MPEG2:
  160. case AUDIO_CODING_TYPE_AACLC:
  161. case AUDIO_CODING_TYPE_DTS:
  162. case AUDIO_CODING_TYPE_ATRAC:
  163. a->max_bitrate = GRAB_BITS(buf, 2, 0, 8);
  164. a->max_bitrate *= 8000;
  165. break;
  166. case AUDIO_CODING_TYPE_SACD:
  167. break;
  168. case AUDIO_CODING_TYPE_EAC3:
  169. break;
  170. case AUDIO_CODING_TYPE_DTS_HD:
  171. break;
  172. case AUDIO_CODING_TYPE_MLP:
  173. break;
  174. case AUDIO_CODING_TYPE_DST:
  175. break;
  176. case AUDIO_CODING_TYPE_WMAPRO:
  177. a->profile = GRAB_BITS(buf, 2, 0, 3);
  178. break;
  179. case AUDIO_CODING_TYPE_REF_CXT:
  180. a->format = GRAB_BITS(buf, 2, 3, 5);
  181. if (a->format == AUDIO_CODING_XTYPE_HE_REF_CT ||
  182. a->format >= AUDIO_CODING_XTYPE_FIRST_RESERVED) {
  183. codec_info(codec,
  184. "HDMI: audio coding xtype %d not expected\n",
  185. a->format);
  186. a->format = 0;
  187. } else
  188. a->format += AUDIO_CODING_TYPE_HE_AAC -
  189. AUDIO_CODING_XTYPE_HE_AAC;
  190. break;
  191. }
  192. }
  193. /*
  194. * Be careful, ELD buf could be totally rubbish!
  195. */
  196. int snd_hdmi_parse_eld(struct hda_codec *codec, struct parsed_hdmi_eld *e,
  197. const unsigned char *buf, int size)
  198. {
  199. int mnl;
  200. int i;
  201. memset(e, 0, sizeof(*e));
  202. e->eld_ver = GRAB_BITS(buf, 0, 3, 5);
  203. if (e->eld_ver != ELD_VER_CEA_861D &&
  204. e->eld_ver != ELD_VER_PARTIAL) {
  205. codec_info(codec, "HDMI: Unknown ELD version %d\n", e->eld_ver);
  206. goto out_fail;
  207. }
  208. e->baseline_len = GRAB_BITS(buf, 2, 0, 8);
  209. mnl = GRAB_BITS(buf, 4, 0, 5);
  210. e->cea_edid_ver = GRAB_BITS(buf, 4, 5, 3);
  211. e->support_hdcp = GRAB_BITS(buf, 5, 0, 1);
  212. e->support_ai = GRAB_BITS(buf, 5, 1, 1);
  213. e->conn_type = GRAB_BITS(buf, 5, 2, 2);
  214. e->sad_count = GRAB_BITS(buf, 5, 4, 4);
  215. e->aud_synch_delay = GRAB_BITS(buf, 6, 0, 8) * 2;
  216. e->spk_alloc = GRAB_BITS(buf, 7, 0, 7);
  217. e->port_id = get_unaligned_le64(buf + 8);
  218. /* not specified, but the spec's tendency is little endian */
  219. e->manufacture_id = get_unaligned_le16(buf + 16);
  220. e->product_id = get_unaligned_le16(buf + 18);
  221. if (mnl > ELD_MAX_MNL) {
  222. codec_info(codec, "HDMI: MNL is reserved value %d\n", mnl);
  223. goto out_fail;
  224. } else if (ELD_FIXED_BYTES + mnl > size) {
  225. codec_info(codec, "HDMI: out of range MNL %d\n", mnl);
  226. goto out_fail;
  227. } else
  228. strscpy(e->monitor_name, buf + ELD_FIXED_BYTES, mnl + 1);
  229. for (i = 0; i < e->sad_count; i++) {
  230. if (ELD_FIXED_BYTES + mnl + 3 * (i + 1) > size) {
  231. codec_info(codec, "HDMI: out of range SAD %d\n", i);
  232. goto out_fail;
  233. }
  234. hdmi_update_short_audio_desc(codec, e->sad + i,
  235. buf + ELD_FIXED_BYTES + mnl + 3 * i);
  236. }
  237. /*
  238. * HDMI sink's ELD info cannot always be retrieved for now, e.g.
  239. * in console or for audio devices. Assume the highest speakers
  240. * configuration, to _not_ prohibit multi-channel audio playback.
  241. */
  242. if (!e->spk_alloc)
  243. e->spk_alloc = 0xffff;
  244. return 0;
  245. out_fail:
  246. return -EINVAL;
  247. }
  248. int snd_hdmi_get_eld_size(struct hda_codec *codec, hda_nid_t nid)
  249. {
  250. return snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
  251. AC_DIPSIZE_ELD_BUF);
  252. }
  253. int snd_hdmi_get_eld(struct hda_codec *codec, hda_nid_t nid,
  254. unsigned char *buf, int *eld_size)
  255. {
  256. int i;
  257. int ret = 0;
  258. int size;
  259. /*
  260. * ELD size is initialized to zero in caller function. If no errors and
  261. * ELD is valid, actual eld_size is assigned.
  262. */
  263. size = snd_hdmi_get_eld_size(codec, nid);
  264. if (size == 0) {
  265. /* wfg: workaround for ASUS P5E-VM HDMI board */
  266. codec_info(codec, "HDMI: ELD buf size is 0, force 128\n");
  267. size = 128;
  268. }
  269. if (size < ELD_FIXED_BYTES || size > ELD_MAX_SIZE) {
  270. codec_info(codec, "HDMI: invalid ELD buf size %d\n", size);
  271. return -ERANGE;
  272. }
  273. /* set ELD buffer */
  274. for (i = 0; i < size; i++) {
  275. unsigned int val = hdmi_get_eld_data(codec, nid, i);
  276. /*
  277. * Graphics driver might be writing to ELD buffer right now.
  278. * Just abort. The caller will repoll after a while.
  279. */
  280. if (!(val & AC_ELDD_ELD_VALID)) {
  281. codec_info(codec, "HDMI: invalid ELD data byte %d\n", i);
  282. ret = -EINVAL;
  283. goto error;
  284. }
  285. val &= AC_ELDD_ELD_DATA;
  286. /*
  287. * The first byte cannot be zero. This can happen on some DVI
  288. * connections. Some Intel chips may also need some 250ms delay
  289. * to return non-zero ELD data, even when the graphics driver
  290. * correctly writes ELD content before setting ELD_valid bit.
  291. */
  292. if (!val && !i) {
  293. codec_dbg(codec, "HDMI: 0 ELD data\n");
  294. ret = -EINVAL;
  295. goto error;
  296. }
  297. buf[i] = val;
  298. }
  299. *eld_size = size;
  300. error:
  301. return ret;
  302. }
  303. /*
  304. * SNDRV_PCM_RATE_* and AC_PAR_PCM values don't match, print correct rates with
  305. * hdmi-specific routine.
  306. */
  307. static void hdmi_print_pcm_rates(int pcm, char *buf, int buflen)
  308. {
  309. static const unsigned int alsa_rates[] = {
  310. 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
  311. 88200, 96000, 176400, 192000, 384000
  312. };
  313. int i, j;
  314. for (i = 0, j = 0; i < ARRAY_SIZE(alsa_rates); i++)
  315. if (pcm & (1 << i))
  316. j += scnprintf(buf + j, buflen - j, " %d",
  317. alsa_rates[i]);
  318. buf[j] = '\0'; /* necessary when j == 0 */
  319. }
  320. #define SND_PRINT_RATES_ADVISED_BUFSIZE 80
  321. static void hdmi_show_short_audio_desc(struct hda_codec *codec,
  322. struct cea_sad *a)
  323. {
  324. char buf[SND_PRINT_RATES_ADVISED_BUFSIZE];
  325. char buf2[8 + SND_PRINT_BITS_ADVISED_BUFSIZE] = ", bits =";
  326. if (!a->format)
  327. return;
  328. hdmi_print_pcm_rates(a->rates, buf, sizeof(buf));
  329. if (a->format == AUDIO_CODING_TYPE_LPCM)
  330. snd_print_pcm_bits(a->sample_bits, buf2 + 8, sizeof(buf2) - 8);
  331. else if (a->max_bitrate)
  332. snprintf(buf2, sizeof(buf2),
  333. ", max bitrate = %d", a->max_bitrate);
  334. else
  335. buf2[0] = '\0';
  336. codec_dbg(codec,
  337. "HDMI: supports coding type %s: channels = %d, rates =%s%s\n",
  338. cea_audio_coding_type_names[a->format],
  339. a->channels, buf, buf2);
  340. }
  341. void snd_hdmi_show_eld(struct hda_codec *codec, struct parsed_hdmi_eld *e)
  342. {
  343. int i;
  344. codec_dbg(codec, "HDMI: detected monitor %s at connection type %s\n",
  345. e->monitor_name,
  346. eld_connection_type_names[e->conn_type]);
  347. if (e->spk_alloc) {
  348. char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
  349. snd_hdac_print_channel_allocation(e->spk_alloc, buf, sizeof(buf));
  350. codec_dbg(codec, "HDMI: available speakers:%s\n", buf);
  351. }
  352. for (i = 0; i < e->sad_count; i++)
  353. hdmi_show_short_audio_desc(codec, e->sad + i);
  354. }
  355. #ifdef CONFIG_SND_PROC_FS
  356. static void hdmi_print_sad_info(int i, struct cea_sad *a,
  357. struct snd_info_buffer *buffer)
  358. {
  359. char buf[SND_PRINT_RATES_ADVISED_BUFSIZE];
  360. snd_iprintf(buffer, "sad%d_coding_type\t[0x%x] %s\n",
  361. i, a->format, cea_audio_coding_type_names[a->format]);
  362. snd_iprintf(buffer, "sad%d_channels\t\t%d\n", i, a->channels);
  363. hdmi_print_pcm_rates(a->rates, buf, sizeof(buf));
  364. snd_iprintf(buffer, "sad%d_rates\t\t[0x%x]%s\n", i, a->rates, buf);
  365. if (a->format == AUDIO_CODING_TYPE_LPCM) {
  366. snd_print_pcm_bits(a->sample_bits, buf, sizeof(buf));
  367. snd_iprintf(buffer, "sad%d_bits\t\t[0x%x]%s\n",
  368. i, a->sample_bits, buf);
  369. }
  370. if (a->max_bitrate)
  371. snd_iprintf(buffer, "sad%d_max_bitrate\t%d\n",
  372. i, a->max_bitrate);
  373. if (a->profile)
  374. snd_iprintf(buffer, "sad%d_profile\t\t%d\n", i, a->profile);
  375. }
  376. void snd_hdmi_print_eld_info(struct hdmi_eld *eld,
  377. struct snd_info_buffer *buffer,
  378. hda_nid_t pin_nid, int dev_id, hda_nid_t cvt_nid)
  379. {
  380. struct parsed_hdmi_eld *e = &eld->info;
  381. char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
  382. int i;
  383. static const char * const eld_version_names[32] = {
  384. "reserved",
  385. "reserved",
  386. "CEA-861D or below",
  387. [3 ... 30] = "reserved",
  388. [31] = "partial"
  389. };
  390. static const char * const cea_edid_version_names[8] = {
  391. "no CEA EDID Timing Extension block present",
  392. "CEA-861",
  393. "CEA-861-A",
  394. "CEA-861-B, C or D",
  395. [4 ... 7] = "reserved"
  396. };
  397. snd_iprintf(buffer, "monitor_present\t\t%d\n", eld->monitor_present);
  398. snd_iprintf(buffer, "eld_valid\t\t%d\n", eld->eld_valid);
  399. snd_iprintf(buffer, "codec_pin_nid\t\t0x%x\n", pin_nid);
  400. snd_iprintf(buffer, "codec_dev_id\t\t0x%x\n", dev_id);
  401. snd_iprintf(buffer, "codec_cvt_nid\t\t0x%x\n", cvt_nid);
  402. if (!eld->eld_valid)
  403. return;
  404. snd_iprintf(buffer, "monitor_name\t\t%s\n", e->monitor_name);
  405. snd_iprintf(buffer, "connection_type\t\t%s\n",
  406. eld_connection_type_names[e->conn_type]);
  407. snd_iprintf(buffer, "eld_version\t\t[0x%x] %s\n", e->eld_ver,
  408. eld_version_names[e->eld_ver]);
  409. snd_iprintf(buffer, "edid_version\t\t[0x%x] %s\n", e->cea_edid_ver,
  410. cea_edid_version_names[e->cea_edid_ver]);
  411. snd_iprintf(buffer, "manufacture_id\t\t0x%x\n", e->manufacture_id);
  412. snd_iprintf(buffer, "product_id\t\t0x%x\n", e->product_id);
  413. snd_iprintf(buffer, "port_id\t\t\t0x%llx\n", (long long)e->port_id);
  414. snd_iprintf(buffer, "support_hdcp\t\t%d\n", e->support_hdcp);
  415. snd_iprintf(buffer, "support_ai\t\t%d\n", e->support_ai);
  416. snd_iprintf(buffer, "audio_sync_delay\t%d\n", e->aud_synch_delay);
  417. snd_hdac_print_channel_allocation(e->spk_alloc, buf, sizeof(buf));
  418. snd_iprintf(buffer, "speakers\t\t[0x%x]%s\n", e->spk_alloc, buf);
  419. snd_iprintf(buffer, "sad_count\t\t%d\n", e->sad_count);
  420. for (i = 0; i < e->sad_count; i++)
  421. hdmi_print_sad_info(i, e->sad + i, buffer);
  422. }
  423. void snd_hdmi_write_eld_info(struct hdmi_eld *eld,
  424. struct snd_info_buffer *buffer)
  425. {
  426. struct parsed_hdmi_eld *e = &eld->info;
  427. char line[64];
  428. char name[64];
  429. char *sname;
  430. long long val;
  431. unsigned int n;
  432. while (!snd_info_get_line(buffer, line, sizeof(line))) {
  433. if (sscanf(line, "%s %llx", name, &val) != 2)
  434. continue;
  435. /*
  436. * We don't allow modification to these fields:
  437. * monitor_name manufacture_id product_id
  438. * eld_version edid_version
  439. */
  440. if (!strcmp(name, "monitor_present"))
  441. eld->monitor_present = val;
  442. else if (!strcmp(name, "eld_valid"))
  443. eld->eld_valid = val;
  444. else if (!strcmp(name, "connection_type"))
  445. e->conn_type = val;
  446. else if (!strcmp(name, "port_id"))
  447. e->port_id = val;
  448. else if (!strcmp(name, "support_hdcp"))
  449. e->support_hdcp = val;
  450. else if (!strcmp(name, "support_ai"))
  451. e->support_ai = val;
  452. else if (!strcmp(name, "audio_sync_delay"))
  453. e->aud_synch_delay = val;
  454. else if (!strcmp(name, "speakers"))
  455. e->spk_alloc = val;
  456. else if (!strcmp(name, "sad_count"))
  457. e->sad_count = val;
  458. else if (!strncmp(name, "sad", 3)) {
  459. sname = name + 4;
  460. n = name[3] - '0';
  461. if (name[4] >= '0' && name[4] <= '9') {
  462. sname++;
  463. n = 10 * n + name[4] - '0';
  464. }
  465. if (n >= ELD_MAX_SAD)
  466. continue;
  467. if (!strcmp(sname, "_coding_type"))
  468. e->sad[n].format = val;
  469. else if (!strcmp(sname, "_channels"))
  470. e->sad[n].channels = val;
  471. else if (!strcmp(sname, "_rates"))
  472. e->sad[n].rates = val;
  473. else if (!strcmp(sname, "_bits"))
  474. e->sad[n].sample_bits = val;
  475. else if (!strcmp(sname, "_max_bitrate"))
  476. e->sad[n].max_bitrate = val;
  477. else if (!strcmp(sname, "_profile"))
  478. e->sad[n].profile = val;
  479. if (n >= e->sad_count)
  480. e->sad_count = n + 1;
  481. }
  482. }
  483. }
  484. #endif /* CONFIG_SND_PROC_FS */
  485. /* update PCM info based on ELD */
  486. void snd_hdmi_eld_update_pcm_info(struct parsed_hdmi_eld *e,
  487. struct hda_pcm_stream *hinfo)
  488. {
  489. u32 rates;
  490. u64 formats;
  491. unsigned int maxbps;
  492. unsigned int channels_max;
  493. int i;
  494. /* assume basic audio support (the basic audio flag is not in ELD;
  495. * however, all audio capable sinks are required to support basic
  496. * audio) */
  497. rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  498. SNDRV_PCM_RATE_48000;
  499. formats = SNDRV_PCM_FMTBIT_S16_LE;
  500. maxbps = 16;
  501. channels_max = 2;
  502. for (i = 0; i < e->sad_count; i++) {
  503. struct cea_sad *a = &e->sad[i];
  504. rates |= a->rates;
  505. if (a->channels > channels_max)
  506. channels_max = a->channels;
  507. if (a->format == AUDIO_CODING_TYPE_LPCM) {
  508. if (a->sample_bits & AC_SUPPCM_BITS_20) {
  509. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  510. if (maxbps < 20)
  511. maxbps = 20;
  512. }
  513. if (a->sample_bits & AC_SUPPCM_BITS_24) {
  514. formats |= SNDRV_PCM_FMTBIT_S32_LE;
  515. if (maxbps < 24)
  516. maxbps = 24;
  517. }
  518. }
  519. }
  520. /* restrict the parameters by the values the codec provides */
  521. hinfo->rates &= rates;
  522. hinfo->formats &= formats;
  523. hinfo->maxbps = min(hinfo->maxbps, maxbps);
  524. hinfo->channels_max = min(hinfo->channels_max, channels_max);
  525. }
  526. /* ATI/AMD specific stuff (ELD emulation) */
  527. #define ATI_VERB_SET_AUDIO_DESCRIPTOR 0x776
  528. #define ATI_VERB_SET_SINK_INFO_INDEX 0x780
  529. #define ATI_VERB_GET_SPEAKER_ALLOCATION 0xf70
  530. #define ATI_VERB_GET_AUDIO_DESCRIPTOR 0xf76
  531. #define ATI_VERB_GET_AUDIO_VIDEO_DELAY 0xf7b
  532. #define ATI_VERB_GET_SINK_INFO_INDEX 0xf80
  533. #define ATI_VERB_GET_SINK_INFO_DATA 0xf81
  534. #define ATI_SPKALLOC_SPKALLOC 0x007f
  535. #define ATI_SPKALLOC_TYPE_HDMI 0x0100
  536. #define ATI_SPKALLOC_TYPE_DISPLAYPORT 0x0200
  537. /* first three bytes are just standard SAD */
  538. #define ATI_AUDIODESC_CHANNELS 0x00000007
  539. #define ATI_AUDIODESC_RATES 0x0000ff00
  540. #define ATI_AUDIODESC_LPCM_STEREO_RATES 0xff000000
  541. /* in standard HDMI VSDB format */
  542. #define ATI_DELAY_VIDEO_LATENCY 0x000000ff
  543. #define ATI_DELAY_AUDIO_LATENCY 0x0000ff00
  544. enum ati_sink_info_idx {
  545. ATI_INFO_IDX_MANUFACTURER_ID = 0,
  546. ATI_INFO_IDX_PRODUCT_ID = 1,
  547. ATI_INFO_IDX_SINK_DESC_LEN = 2,
  548. ATI_INFO_IDX_PORT_ID_LOW = 3,
  549. ATI_INFO_IDX_PORT_ID_HIGH = 4,
  550. ATI_INFO_IDX_SINK_DESC_FIRST = 5,
  551. ATI_INFO_IDX_SINK_DESC_LAST = 22, /* max len 18 bytes */
  552. };
  553. int snd_hdmi_get_eld_ati(struct hda_codec *codec, hda_nid_t nid,
  554. unsigned char *buf, int *eld_size, bool rev3_or_later)
  555. {
  556. int spkalloc, ati_sad, aud_synch;
  557. int sink_desc_len = 0;
  558. int pos, i;
  559. /* ATI/AMD does not have ELD, emulate it */
  560. spkalloc = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SPEAKER_ALLOCATION, 0);
  561. if (spkalloc <= 0) {
  562. codec_info(codec, "HDMI ATI/AMD: no speaker allocation for ELD\n");
  563. return -EINVAL;
  564. }
  565. memset(buf, 0, ELD_FIXED_BYTES + ELD_MAX_MNL + ELD_MAX_SAD * 3);
  566. /* version */
  567. buf[0] = ELD_VER_CEA_861D << 3;
  568. /* speaker allocation from EDID */
  569. buf[7] = spkalloc & ATI_SPKALLOC_SPKALLOC;
  570. /* is DisplayPort? */
  571. if (spkalloc & ATI_SPKALLOC_TYPE_DISPLAYPORT)
  572. buf[5] |= 0x04;
  573. pos = ELD_FIXED_BYTES;
  574. if (rev3_or_later) {
  575. int sink_info;
  576. snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_PORT_ID_LOW);
  577. sink_info = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
  578. put_unaligned_le32(sink_info, buf + 8);
  579. snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_PORT_ID_HIGH);
  580. sink_info = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
  581. put_unaligned_le32(sink_info, buf + 12);
  582. snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_MANUFACTURER_ID);
  583. sink_info = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
  584. put_unaligned_le16(sink_info, buf + 16);
  585. snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_PRODUCT_ID);
  586. sink_info = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
  587. put_unaligned_le16(sink_info, buf + 18);
  588. snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_SINK_DESC_LEN);
  589. sink_desc_len = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
  590. if (sink_desc_len > ELD_MAX_MNL) {
  591. codec_info(codec, "HDMI ATI/AMD: Truncating HDMI sink description with length %d\n",
  592. sink_desc_len);
  593. sink_desc_len = ELD_MAX_MNL;
  594. }
  595. buf[4] |= sink_desc_len;
  596. for (i = 0; i < sink_desc_len; i++) {
  597. snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_SINK_INFO_INDEX, ATI_INFO_IDX_SINK_DESC_FIRST + i);
  598. buf[pos++] = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_SINK_INFO_DATA, 0);
  599. }
  600. }
  601. for (i = AUDIO_CODING_TYPE_LPCM; i <= AUDIO_CODING_TYPE_WMAPRO; i++) {
  602. if (i == AUDIO_CODING_TYPE_SACD || i == AUDIO_CODING_TYPE_DST)
  603. continue; /* not handled by ATI/AMD */
  604. snd_hda_codec_write(codec, nid, 0, ATI_VERB_SET_AUDIO_DESCRIPTOR, i << 3);
  605. ati_sad = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_AUDIO_DESCRIPTOR, 0);
  606. if (ati_sad <= 0)
  607. continue;
  608. if (ati_sad & ATI_AUDIODESC_RATES) {
  609. /* format is supported, copy SAD as-is */
  610. buf[pos++] = (ati_sad & 0x0000ff) >> 0;
  611. buf[pos++] = (ati_sad & 0x00ff00) >> 8;
  612. buf[pos++] = (ati_sad & 0xff0000) >> 16;
  613. }
  614. if (i == AUDIO_CODING_TYPE_LPCM
  615. && (ati_sad & ATI_AUDIODESC_LPCM_STEREO_RATES)
  616. && (ati_sad & ATI_AUDIODESC_LPCM_STEREO_RATES) >> 16 != (ati_sad & ATI_AUDIODESC_RATES)) {
  617. /* for PCM there is a separate stereo rate mask */
  618. buf[pos++] = ((ati_sad & 0x000000ff) & ~ATI_AUDIODESC_CHANNELS) | 0x1;
  619. /* rates from the extra byte */
  620. buf[pos++] = (ati_sad & 0xff000000) >> 24;
  621. buf[pos++] = (ati_sad & 0x00ff0000) >> 16;
  622. }
  623. }
  624. if (pos == ELD_FIXED_BYTES + sink_desc_len) {
  625. codec_info(codec, "HDMI ATI/AMD: no audio descriptors for ELD\n");
  626. return -EINVAL;
  627. }
  628. /*
  629. * HDMI VSDB latency format:
  630. * separately for both audio and video:
  631. * 0 field not valid or unknown latency
  632. * [1..251] msecs = (x-1)*2 (max 500ms with x = 251 = 0xfb)
  633. * 255 audio/video not supported
  634. *
  635. * HDA latency format:
  636. * single value indicating video latency relative to audio:
  637. * 0 unknown or 0ms
  638. * [1..250] msecs = x*2 (max 500ms with x = 250 = 0xfa)
  639. * [251..255] reserved
  640. */
  641. aud_synch = snd_hda_codec_read(codec, nid, 0, ATI_VERB_GET_AUDIO_VIDEO_DELAY, 0);
  642. if ((aud_synch & ATI_DELAY_VIDEO_LATENCY) && (aud_synch & ATI_DELAY_AUDIO_LATENCY)) {
  643. int video_latency_hdmi = (aud_synch & ATI_DELAY_VIDEO_LATENCY);
  644. int audio_latency_hdmi = (aud_synch & ATI_DELAY_AUDIO_LATENCY) >> 8;
  645. if (video_latency_hdmi <= 0xfb && audio_latency_hdmi <= 0xfb &&
  646. video_latency_hdmi > audio_latency_hdmi)
  647. buf[6] = video_latency_hdmi - audio_latency_hdmi;
  648. /* else unknown/invalid or 0ms or video ahead of audio, so use zero */
  649. }
  650. /* SAD count */
  651. buf[5] |= ((pos - ELD_FIXED_BYTES - sink_desc_len) / 3) << 4;
  652. /* Baseline ELD block length is 4-byte aligned */
  653. pos = round_up(pos, 4);
  654. /* Baseline ELD length (4-byte header is not counted in) */
  655. buf[2] = (pos - 4) / 4;
  656. *eld_size = pos;
  657. return 0;
  658. }