patch_intelhdmi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. /*
  2. *
  3. * patch_intelhdmi.c - Patch for Intel HDMI codecs
  4. *
  5. * Copyright(c) 2008 Intel Corporation. All rights reserved.
  6. *
  7. * Authors:
  8. * Jiang Zhe <zhe.jiang@intel.com>
  9. * Wu Fengguang <wfg@linux.intel.com>
  10. *
  11. * Maintained by:
  12. * Wu Fengguang <wfg@linux.intel.com>
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the Free
  16. * Software Foundation; either version 2 of the License, or (at your option)
  17. * any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  21. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  22. * for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software Foundation,
  26. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  27. */
  28. #include <linux/init.h>
  29. #include <linux/delay.h>
  30. #include <linux/slab.h>
  31. #include <sound/core.h>
  32. #include <asm/unaligned.h>
  33. #include "hda_codec.h"
  34. #include "hda_local.h"
  35. #include "hda_patch.h"
  36. #define CVT_NID 0x02 /* audio converter */
  37. #define PIN_NID 0x03 /* HDMI output pin */
  38. #define INTEL_HDMI_EVENT_TAG 0x08
  39. /*
  40. * CEA Short Audio Descriptor data
  41. */
  42. struct cea_sad {
  43. int channels;
  44. int format; /* (format == 0) indicates invalid SAD */
  45. int rates;
  46. int sample_bits; /* for LPCM */
  47. int max_bitrate; /* for AC3...ATRAC */
  48. int profile; /* for WMAPRO */
  49. };
  50. #define ELD_FIXED_BYTES 20
  51. #define ELD_MAX_MNL 16
  52. #define ELD_MAX_SAD 16
  53. /*
  54. * ELD: EDID Like Data
  55. */
  56. struct sink_eld {
  57. int eld_size;
  58. int baseline_len;
  59. int eld_ver; /* (eld_ver == 0) indicates invalid ELD */
  60. int cea_edid_ver;
  61. char monitor_name[ELD_MAX_MNL + 1];
  62. int manufacture_id;
  63. int product_id;
  64. u64 port_id;
  65. int support_hdcp;
  66. int support_ai;
  67. int conn_type;
  68. int aud_synch_delay;
  69. int spk_alloc;
  70. int sad_count;
  71. struct cea_sad sad[ELD_MAX_SAD];
  72. };
  73. struct intel_hdmi_spec {
  74. struct hda_multi_out multiout;
  75. struct hda_pcm pcm_rec;
  76. struct sink_eld sink;
  77. };
  78. static struct hda_verb pinout_enable_verb[] = {
  79. {PIN_NID, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
  80. {} /* terminator */
  81. };
  82. static struct hda_verb pinout_disable_verb[] = {
  83. {PIN_NID, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x00},
  84. {}
  85. };
  86. static struct hda_verb unsolicited_response_verb[] = {
  87. {PIN_NID, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN |
  88. INTEL_HDMI_EVENT_TAG},
  89. {}
  90. };
  91. static struct hda_verb def_chan_map[] = {
  92. {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x00},
  93. {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x11},
  94. {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x22},
  95. {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x33},
  96. {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x44},
  97. {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x55},
  98. {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x66},
  99. {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x77},
  100. {}
  101. };
  102. struct hdmi_audio_infoframe {
  103. u8 type; /* 0x84 */
  104. u8 ver; /* 0x01 */
  105. u8 len; /* 0x0a */
  106. u8 checksum; /* PB0 */
  107. u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
  108. u8 SS01_SF24;
  109. u8 CXT04;
  110. u8 CA;
  111. u8 LFEPBL01_LSV36_DM_INH7;
  112. u8 reserved[5]; /* PB6 - PB10 */
  113. };
  114. /*
  115. * SS1:SS0 index => sample size
  116. */
  117. static int cea_sample_sizes[4] = {
  118. 0, /* 0: Refer to Stream Header */
  119. AC_SUPPCM_BITS_16, /* 1: 16 bits */
  120. AC_SUPPCM_BITS_20, /* 2: 20 bits */
  121. AC_SUPPCM_BITS_24, /* 3: 24 bits */
  122. };
  123. /*
  124. * SF2:SF1:SF0 index => sampling frequency
  125. */
  126. static int cea_sampling_frequencies[8] = {
  127. 0, /* 0: Refer to Stream Header */
  128. SNDRV_PCM_RATE_32000, /* 1: 32000Hz */
  129. SNDRV_PCM_RATE_44100, /* 2: 44100Hz */
  130. SNDRV_PCM_RATE_48000, /* 3: 48000Hz */
  131. SNDRV_PCM_RATE_88200, /* 4: 88200Hz */
  132. SNDRV_PCM_RATE_96000, /* 5: 96000Hz */
  133. SNDRV_PCM_RATE_176400, /* 6: 176400Hz */
  134. SNDRV_PCM_RATE_192000, /* 7: 192000Hz */
  135. };
  136. enum eld_versions {
  137. ELD_VER_CEA_861D = 2,
  138. ELD_VER_PARTIAL = 31,
  139. };
  140. static char *eld_versoin_names[32] = {
  141. "0-reserved",
  142. "1-reserved",
  143. "CEA-861D or below",
  144. "3-reserved",
  145. [4 ... 30] = "reserved",
  146. [31] = "partial"
  147. };
  148. enum cea_edid_versions {
  149. CEA_EDID_VER_NONE = 0,
  150. CEA_EDID_VER_CEA861 = 1,
  151. CEA_EDID_VER_CEA861A = 2,
  152. CEA_EDID_VER_CEA861BCD = 3,
  153. CEA_EDID_VER_RESERVED = 4,
  154. };
  155. static char *cea_edid_version_names[8] = {
  156. "no CEA EDID Timing Extension block present",
  157. "CEA-861",
  158. "CEA-861-A",
  159. "CEA-861-B, C or D",
  160. "4-reserved",
  161. [5 ... 7] = "reserved"
  162. };
  163. /*
  164. * CEA Speaker Allocation data block bits
  165. */
  166. #define CEA_SA_FLR (0 << 0)
  167. #define CEA_SA_LFE (1 << 1)
  168. #define CEA_SA_FC (1 << 2)
  169. #define CEA_SA_RLR (1 << 3)
  170. #define CEA_SA_RC (1 << 4)
  171. #define CEA_SA_FLRC (1 << 5)
  172. #define CEA_SA_RLRC (1 << 6)
  173. /* the following are not defined in ELD yet */
  174. #define CEA_SA_FLRW (1 << 7)
  175. #define CEA_SA_FLRH (1 << 8)
  176. #define CEA_SA_TC (1 << 9)
  177. #define CEA_SA_FCH (1 << 10)
  178. static char *cea_speaker_allocation_names[] = {
  179. /* 0 */ "FL/FR",
  180. /* 1 */ "LFE",
  181. /* 2 */ "FC",
  182. /* 3 */ "RL/RR",
  183. /* 4 */ "RC",
  184. /* 5 */ "FLC/FRC",
  185. /* 6 */ "RLC/RRC",
  186. /* 7 */ "FLW/FRW",
  187. /* 8 */ "FLH/FRH",
  188. /* 9 */ "TC",
  189. /* 10 */ "FCH",
  190. };
  191. static char *eld_connection_type_names[4] = {
  192. "HDMI",
  193. "Display Port",
  194. "2-reserved",
  195. "3-reserved"
  196. };
  197. enum cea_audio_coding_types {
  198. AUDIO_CODING_TYPE_REF_STREAM_HEADER = 0,
  199. AUDIO_CODING_TYPE_LPCM = 1,
  200. AUDIO_CODING_TYPE_AC3 = 2,
  201. AUDIO_CODING_TYPE_MPEG1 = 3,
  202. AUDIO_CODING_TYPE_MP3 = 4,
  203. AUDIO_CODING_TYPE_MPEG2 = 5,
  204. AUDIO_CODING_TYPE_AACLC = 6,
  205. AUDIO_CODING_TYPE_DTS = 7,
  206. AUDIO_CODING_TYPE_ATRAC = 8,
  207. AUDIO_CODING_TYPE_SACD = 9,
  208. AUDIO_CODING_TYPE_EAC3 = 10,
  209. AUDIO_CODING_TYPE_DTS_HD = 11,
  210. AUDIO_CODING_TYPE_MLP = 12,
  211. AUDIO_CODING_TYPE_DST = 13,
  212. AUDIO_CODING_TYPE_WMAPRO = 14,
  213. AUDIO_CODING_TYPE_REF_CXT = 15,
  214. /* also include valid xtypes below */
  215. AUDIO_CODING_TYPE_HE_AAC = 15,
  216. AUDIO_CODING_TYPE_HE_AAC2 = 16,
  217. AUDIO_CODING_TYPE_MPEG_SURROUND = 17,
  218. };
  219. enum cea_audio_coding_xtypes {
  220. AUDIO_CODING_XTYPE_HE_REF_CT = 0,
  221. AUDIO_CODING_XTYPE_HE_AAC = 1,
  222. AUDIO_CODING_XTYPE_HE_AAC2 = 2,
  223. AUDIO_CODING_XTYPE_MPEG_SURROUND = 3,
  224. AUDIO_CODING_XTYPE_FIRST_RESERVED = 4,
  225. };
  226. static char *cea_audio_coding_type_names[] = {
  227. /* 0 */ "undefined",
  228. /* 1 */ "LPCM",
  229. /* 2 */ "AC-3",
  230. /* 3 */ "MPEG1",
  231. /* 4 */ "MP3",
  232. /* 5 */ "MPEG2",
  233. /* 6 */ "AAC-LC",
  234. /* 7 */ "DTS",
  235. /* 8 */ "ATRAC",
  236. /* 9 */ "DSD(1-bit audio)",
  237. /* 10 */ "Dolby Digital Plus(E-AC-3/DD+)",
  238. /* 11 */ "DTS-HD",
  239. /* 12 */ "Dolby TrueHD(MLP)",
  240. /* 13 */ "DST",
  241. /* 14 */ "WMAPro",
  242. /* 15 */ "HE-AAC",
  243. /* 16 */ "HE-AACv2",
  244. /* 17 */ "MPEG Surround",
  245. };
  246. /*
  247. * HDMI routines
  248. */
  249. static int hdmi_get_eld_size(struct hda_codec *codec, hda_nid_t nid)
  250. {
  251. return snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_SIZE,
  252. AC_DIPSIZE_ELD_BUF);
  253. }
  254. static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t nid,
  255. int *packet_index, int *byte_index)
  256. {
  257. int val;
  258. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_INDEX, 0);
  259. *packet_index = val >> 5;
  260. *byte_index = val & 0x1f;
  261. }
  262. static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t nid,
  263. int packet_index, int byte_index)
  264. {
  265. int val;
  266. val = (packet_index << 5) | (byte_index & 0x1f);
  267. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
  268. }
  269. static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t nid,
  270. unsigned char val)
  271. {
  272. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
  273. }
  274. static void hdmi_enable_output(struct hda_codec *codec)
  275. {
  276. /* Enable pin out and unmute */
  277. snd_hda_sequence_write(codec, pinout_enable_verb);
  278. if (get_wcaps(codec, PIN_NID) & AC_WCAP_OUT_AMP)
  279. snd_hda_codec_write(codec, PIN_NID, 0,
  280. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
  281. /* Enable Audio InfoFrame Transmission */
  282. hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
  283. snd_hda_codec_write(codec, PIN_NID, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  284. AC_DIPXMIT_BEST);
  285. }
  286. static void hdmi_disable_output(struct hda_codec *codec)
  287. {
  288. snd_hda_sequence_write(codec, pinout_disable_verb);
  289. if (get_wcaps(codec, PIN_NID) & AC_WCAP_OUT_AMP)
  290. snd_hda_codec_write(codec, PIN_NID, 0,
  291. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  292. /*
  293. * FIXME: noises may arise when playing music after reloading the
  294. * kernel module, until the next X restart or monitor repower.
  295. */
  296. }
  297. static int hdmi_get_channel_count(struct hda_codec *codec)
  298. {
  299. return 1 + snd_hda_codec_read(codec, CVT_NID, 0,
  300. AC_VERB_GET_CVT_CHAN_COUNT, 0);
  301. }
  302. static void hdmi_set_channel_count(struct hda_codec *codec, int chs)
  303. {
  304. snd_hda_codec_write(codec, CVT_NID, 0,
  305. AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
  306. if (chs != hdmi_get_channel_count(codec))
  307. snd_printd(KERN_INFO "Channel count expect=%d, real=%d\n",
  308. chs, hdmi_get_channel_count(codec));
  309. }
  310. static void hdmi_debug_slot_mapping(struct hda_codec *codec)
  311. {
  312. #ifdef CONFIG_SND_DEBUG_VERBOSE
  313. int i;
  314. int slot;
  315. for (i = 0; i < 8; i++) {
  316. slot = snd_hda_codec_read(codec, CVT_NID, 0,
  317. AC_VERB_GET_HDMI_CHAN_SLOT, i);
  318. printk(KERN_DEBUG "ASP channel %d => slot %d\n",
  319. slot >> 4, slot & 0x7);
  320. }
  321. #endif
  322. }
  323. static void hdmi_setup_channel_mapping(struct hda_codec *codec)
  324. {
  325. snd_hda_sequence_write(codec, def_chan_map);
  326. hdmi_debug_slot_mapping(codec);
  327. }
  328. /*
  329. * ELD(EDID Like Data) routines
  330. */
  331. static int hdmi_present_sense(struct hda_codec *codec, hda_nid_t nid)
  332. {
  333. return snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_PIN_SENSE, 0);
  334. }
  335. static void hdmi_debug_present_sense(struct hda_codec *codec)
  336. {
  337. #ifdef CONFIG_SND_DEBUG_VERBOSE
  338. int eldv;
  339. int present;
  340. present = hdmi_present_sense(codec, PIN_NID);
  341. eldv = (present & AC_PINSENSE_ELDV);
  342. present = (present & AC_PINSENSE_PRESENCE);
  343. printk(KERN_INFO "pinp = %d, eldv = %d\n", !!present, !!eldv);
  344. #endif
  345. }
  346. static unsigned char hdmi_get_eld_byte(struct hda_codec *codec, int byte_index)
  347. {
  348. unsigned int val;
  349. val = snd_hda_codec_read(codec, PIN_NID, 0,
  350. AC_VERB_GET_HDMI_ELDD, byte_index);
  351. #ifdef BE_PARANOID
  352. printk(KERN_INFO "ELD data byte %d: 0x%x\n", byte_index, val);
  353. #endif
  354. if ((val & AC_ELDD_ELD_VALID) == 0) {
  355. snd_printd(KERN_INFO "Invalid ELD data byte %d\n",
  356. byte_index);
  357. val = 0;
  358. }
  359. return val & AC_ELDD_ELD_DATA;
  360. }
  361. static inline unsigned char grab_bits(const unsigned char *buf,
  362. int byte, int lowbit, int bits)
  363. {
  364. BUG_ON(lowbit > 7);
  365. BUG_ON(bits > 8);
  366. BUG_ON(bits <= 0);
  367. return (buf[byte] >> lowbit) & ((1 << bits) - 1);
  368. }
  369. static void hdmi_update_short_audio_desc(struct cea_sad *a,
  370. const unsigned char *buf)
  371. {
  372. int i;
  373. int val;
  374. val = grab_bits(buf, 1, 0, 7);
  375. a->rates = 0;
  376. for (i = 0; i < 7; i++)
  377. if (val & (1 << i))
  378. a->rates |= cea_sampling_frequencies[i + 1];
  379. a->channels = grab_bits(buf, 0, 0, 3);
  380. a->channels++;
  381. a->format = grab_bits(buf, 0, 3, 4);
  382. switch (a->format) {
  383. case AUDIO_CODING_TYPE_REF_STREAM_HEADER:
  384. snd_printd(KERN_INFO
  385. "audio coding type 0 not expected in ELD\n");
  386. break;
  387. case AUDIO_CODING_TYPE_LPCM:
  388. val = grab_bits(buf, 2, 0, 3);
  389. a->sample_bits = 0;
  390. for (i = 0; i < 3; i++)
  391. if (val & (1 << i))
  392. a->sample_bits |= cea_sample_sizes[i + 1];
  393. break;
  394. case AUDIO_CODING_TYPE_AC3:
  395. case AUDIO_CODING_TYPE_MPEG1:
  396. case AUDIO_CODING_TYPE_MP3:
  397. case AUDIO_CODING_TYPE_MPEG2:
  398. case AUDIO_CODING_TYPE_AACLC:
  399. case AUDIO_CODING_TYPE_DTS:
  400. case AUDIO_CODING_TYPE_ATRAC:
  401. a->max_bitrate = grab_bits(buf, 2, 0, 8);
  402. a->max_bitrate *= 8000;
  403. break;
  404. case AUDIO_CODING_TYPE_SACD:
  405. break;
  406. case AUDIO_CODING_TYPE_EAC3:
  407. break;
  408. case AUDIO_CODING_TYPE_DTS_HD:
  409. break;
  410. case AUDIO_CODING_TYPE_MLP:
  411. break;
  412. case AUDIO_CODING_TYPE_DST:
  413. break;
  414. case AUDIO_CODING_TYPE_WMAPRO:
  415. a->profile = grab_bits(buf, 2, 0, 3);
  416. break;
  417. case AUDIO_CODING_TYPE_REF_CXT:
  418. a->format = grab_bits(buf, 2, 3, 5);
  419. if (a->format == AUDIO_CODING_XTYPE_HE_REF_CT ||
  420. a->format >= AUDIO_CODING_XTYPE_FIRST_RESERVED) {
  421. snd_printd(KERN_INFO
  422. "audio coding xtype %d not expected in ELD\n",
  423. a->format);
  424. a->format = 0;
  425. } else
  426. a->format += AUDIO_CODING_TYPE_HE_AAC -
  427. AUDIO_CODING_XTYPE_HE_AAC;
  428. break;
  429. }
  430. }
  431. static int hdmi_update_sink_eld(struct hda_codec *codec,
  432. const unsigned char *buf, int size)
  433. {
  434. struct intel_hdmi_spec *spec = codec->spec;
  435. struct sink_eld *e = &spec->sink;
  436. int mnl;
  437. int i;
  438. e->eld_ver = grab_bits(buf, 0, 3, 5);
  439. if (e->eld_ver != ELD_VER_CEA_861D &&
  440. e->eld_ver != ELD_VER_PARTIAL) {
  441. snd_printd(KERN_INFO "Unknown ELD version %d\n", e->eld_ver);
  442. goto out_fail;
  443. }
  444. e->eld_size = size;
  445. e->baseline_len = grab_bits(buf, 2, 0, 8);
  446. mnl = grab_bits(buf, 4, 0, 5);
  447. e->cea_edid_ver = grab_bits(buf, 4, 5, 3);
  448. e->support_hdcp = grab_bits(buf, 5, 0, 1);
  449. e->support_ai = grab_bits(buf, 5, 1, 1);
  450. e->conn_type = grab_bits(buf, 5, 2, 2);
  451. e->sad_count = grab_bits(buf, 5, 4, 4);
  452. e->aud_synch_delay = grab_bits(buf, 6, 0, 8);
  453. e->spk_alloc = grab_bits(buf, 7, 0, 7);
  454. e->port_id = get_unaligned_le64(buf + 8);
  455. /* not specified, but the spec's tendency is little endian */
  456. e->manufacture_id = get_unaligned_le16(buf + 16);
  457. e->product_id = get_unaligned_le16(buf + 18);
  458. if (mnl > ELD_MAX_MNL) {
  459. snd_printd(KERN_INFO "MNL is reserved value %d\n", mnl);
  460. goto out_fail;
  461. } else if (ELD_FIXED_BYTES + mnl > size) {
  462. snd_printd(KERN_INFO "out of range MNL %d\n", mnl);
  463. goto out_fail;
  464. } else
  465. strlcpy(e->monitor_name, buf + ELD_FIXED_BYTES, mnl);
  466. for (i = 0; i < e->sad_count; i++) {
  467. if (ELD_FIXED_BYTES + mnl + 3 * (i + 1) > size) {
  468. snd_printd(KERN_INFO "out of range SAD %d\n", i);
  469. goto out_fail;
  470. }
  471. hdmi_update_short_audio_desc(e->sad + i,
  472. buf + ELD_FIXED_BYTES + mnl + 3 * i);
  473. }
  474. return 0;
  475. out_fail:
  476. e->eld_ver = 0;
  477. return -EINVAL;
  478. }
  479. static int hdmi_get_eld(struct hda_codec *codec)
  480. {
  481. int i;
  482. int ret;
  483. int size;
  484. unsigned char *buf;
  485. i = hdmi_present_sense(codec, PIN_NID) & AC_PINSENSE_ELDV;
  486. if (!i)
  487. return -ENOENT;
  488. size = hdmi_get_eld_size(codec, PIN_NID);
  489. if (size == 0) {
  490. /* wfg: workaround for ASUS P5E-VM HDMI board */
  491. snd_printd(KERN_INFO "ELD buf size is 0, force 128\n");
  492. size = 128;
  493. }
  494. if (size < ELD_FIXED_BYTES || size > PAGE_SIZE) {
  495. snd_printd(KERN_INFO "Invalid ELD buf size %d\n", size);
  496. return -ERANGE;
  497. }
  498. buf = kmalloc(size, GFP_KERNEL);
  499. if (!buf)
  500. return -ENOMEM;
  501. for (i = 0; i < size; i++)
  502. buf[i] = hdmi_get_eld_byte(codec, i);
  503. ret = hdmi_update_sink_eld(codec, buf, size);
  504. kfree(buf);
  505. return ret;
  506. }
  507. static void hdmi_show_short_audio_desc(struct cea_sad *a)
  508. {
  509. printk(KERN_INFO "coding type: %s\n",
  510. cea_audio_coding_type_names[a->format]);
  511. printk(KERN_INFO "channels: %d\n", a->channels);
  512. printk(KERN_INFO "sampling frequencies: 0x%x\n", a->rates);
  513. if (a->format == AUDIO_CODING_TYPE_LPCM)
  514. printk(KERN_INFO "sample bits: 0x%x\n", a->sample_bits);
  515. if (a->max_bitrate)
  516. printk(KERN_INFO "max bitrate: %d HZ\n", a->max_bitrate);
  517. if (a->profile)
  518. printk(KERN_INFO "profile: %d\n", a->profile);
  519. }
  520. static void hdmi_show_eld(struct hda_codec *codec)
  521. {
  522. int i;
  523. int j;
  524. struct intel_hdmi_spec *spec = codec->spec;
  525. struct sink_eld *e = &spec->sink;
  526. char buf[80];
  527. printk(KERN_INFO "ELD buffer size is %d\n", e->eld_size);
  528. printk(KERN_INFO "ELD baseline len is %d*4\n", e->baseline_len);
  529. printk(KERN_INFO "vendor block len is %d\n",
  530. e->eld_size - e->baseline_len * 4 - 4);
  531. printk(KERN_INFO "ELD version is %s\n",
  532. eld_versoin_names[e->eld_ver]);
  533. printk(KERN_INFO "CEA EDID version is %s\n",
  534. cea_edid_version_names[e->cea_edid_ver]);
  535. printk(KERN_INFO "manufacture id is 0x%x\n", e->manufacture_id);
  536. printk(KERN_INFO "product id is 0x%x\n", e->product_id);
  537. printk(KERN_INFO "port id is 0x%llx\n", (long long)e->port_id);
  538. printk(KERN_INFO "HDCP support is %d\n", e->support_hdcp);
  539. printk(KERN_INFO "AI support is %d\n", e->support_ai);
  540. printk(KERN_INFO "SAD count is %d\n", e->sad_count);
  541. printk(KERN_INFO "audio sync delay is %x\n", e->aud_synch_delay);
  542. printk(KERN_INFO "connection type is %s\n",
  543. eld_connection_type_names[e->conn_type]);
  544. printk(KERN_INFO "monitor name is %s\n", e->monitor_name);
  545. j = 0;
  546. for (i = 0; i < ARRAY_SIZE(cea_speaker_allocation_names); i++) {
  547. if (e->spk_alloc & (1 << i))
  548. j += snprintf(buf + j, sizeof(buf) - j, " %s",
  549. cea_speaker_allocation_names[i]);
  550. }
  551. buf[j] = '\0'; /* necessary when j == 0 */
  552. printk(KERN_INFO "speaker allocations: (0x%x)%s\n", e->spk_alloc, buf);
  553. for (i = 0; i < e->sad_count; i++)
  554. hdmi_show_short_audio_desc(e->sad + i);
  555. }
  556. /*
  557. * Be careful, ELD buf could be totally rubbish!
  558. */
  559. static void hdmi_parse_eld(struct hda_codec *codec)
  560. {
  561. hdmi_debug_present_sense(codec);
  562. if (!hdmi_get_eld(codec))
  563. hdmi_show_eld(codec);
  564. }
  565. /*
  566. * Audio Infoframe routines
  567. */
  568. static void hdmi_debug_dip_size(struct hda_codec *codec)
  569. {
  570. #ifdef CONFIG_SND_DEBUG_VERBOSE
  571. int i;
  572. int size;
  573. size = hdmi_get_eld_size(codec, PIN_NID);
  574. printk(KERN_DEBUG "ELD buf size is %d\n", size);
  575. for (i = 0; i < 8; i++) {
  576. size = snd_hda_codec_read(codec, PIN_NID, 0,
  577. AC_VERB_GET_HDMI_DIP_SIZE, i);
  578. printk(KERN_DEBUG "DIP GP[%d] buf size is %d\n", i, size);
  579. }
  580. #endif
  581. }
  582. static void hdmi_clear_dip_buffers(struct hda_codec *codec)
  583. {
  584. #ifdef BE_PARANOID
  585. int i, j;
  586. int size;
  587. int pi, bi;
  588. for (i = 0; i < 8; i++) {
  589. size = snd_hda_codec_read(codec, PIN_NID, 0,
  590. AC_VERB_GET_HDMI_DIP_SIZE, i);
  591. if (size == 0)
  592. continue;
  593. hdmi_set_dip_index(codec, PIN_NID, i, 0x0);
  594. for (j = 1; j < 1000; j++) {
  595. hdmi_write_dip_byte(codec, PIN_NID, 0x0);
  596. hdmi_get_dip_index(codec, PIN_NID, &pi, &bi);
  597. if (pi != i)
  598. snd_printd(KERN_INFO "dip index %d: %d != %d\n",
  599. bi, pi, i);
  600. if (bi == 0) /* byte index wrapped around */
  601. break;
  602. }
  603. snd_printd(KERN_INFO
  604. "DIP GP[%d] buf reported size=%d, written=%d\n",
  605. i, size, j);
  606. }
  607. #endif
  608. }
  609. static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
  610. struct snd_pcm_substream *substream)
  611. {
  612. struct hdmi_audio_infoframe audio_infoframe = {
  613. .type = 0x84,
  614. .ver = 0x01,
  615. .len = 0x0a,
  616. .CC02_CT47 = substream->runtime->channels - 1,
  617. };
  618. u8 *params = (u8 *)&audio_infoframe;
  619. int i;
  620. hdmi_debug_dip_size(codec);
  621. hdmi_clear_dip_buffers(codec); /* be paranoid */
  622. hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
  623. for (i = 0; i < sizeof(audio_infoframe); i++)
  624. hdmi_write_dip_byte(codec, PIN_NID, params[i]);
  625. }
  626. /*
  627. * Unsolicited events
  628. */
  629. static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
  630. {
  631. int pind = !!(res & AC_UNSOL_RES_PD);
  632. int eldv = !!(res & AC_UNSOL_RES_ELDV);
  633. printk(KERN_INFO "HDMI intrinsic event: PD=%d ELDV=%d\n", pind, eldv);
  634. if (pind && eldv) {
  635. hdmi_parse_eld(codec);
  636. /* TODO: do real things about ELD */
  637. }
  638. }
  639. static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
  640. {
  641. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  642. int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
  643. int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
  644. printk(KERN_INFO "HDMI non-intrinsic event: "
  645. "SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
  646. subtag,
  647. cp_state,
  648. cp_ready);
  649. /* who cares? */
  650. if (cp_state)
  651. ;
  652. if (cp_ready)
  653. ;
  654. }
  655. static void intel_hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
  656. {
  657. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  658. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  659. if (tag != INTEL_HDMI_EVENT_TAG) {
  660. snd_printd(KERN_INFO
  661. "Unexpected HDMI unsolicited event tag 0x%x\n",
  662. tag);
  663. return;
  664. }
  665. if (subtag == 0)
  666. hdmi_intrinsic_event(codec, res);
  667. else
  668. hdmi_non_intrinsic_event(codec, res);
  669. }
  670. /*
  671. * Callbacks
  672. */
  673. static int intel_hdmi_playback_pcm_open(struct hda_pcm_stream *hinfo,
  674. struct hda_codec *codec,
  675. struct snd_pcm_substream *substream)
  676. {
  677. struct intel_hdmi_spec *spec = codec->spec;
  678. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  679. }
  680. static int intel_hdmi_playback_pcm_close(struct hda_pcm_stream *hinfo,
  681. struct hda_codec *codec,
  682. struct snd_pcm_substream *substream)
  683. {
  684. struct intel_hdmi_spec *spec = codec->spec;
  685. hdmi_disable_output(codec);
  686. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  687. }
  688. static int intel_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  689. struct hda_codec *codec,
  690. unsigned int stream_tag,
  691. unsigned int format,
  692. struct snd_pcm_substream *substream)
  693. {
  694. struct intel_hdmi_spec *spec = codec->spec;
  695. snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
  696. format, substream);
  697. hdmi_set_channel_count(codec, substream->runtime->channels);
  698. /* wfg: channel mapping not supported by DEVCTG */
  699. hdmi_setup_channel_mapping(codec);
  700. hdmi_setup_audio_infoframe(codec, substream);
  701. hdmi_enable_output(codec);
  702. return 0;
  703. }
  704. static struct hda_pcm_stream intel_hdmi_pcm_playback = {
  705. .substreams = 1,
  706. .channels_min = 2,
  707. .channels_max = 8,
  708. .nid = CVT_NID, /* NID to query formats and rates and setup streams */
  709. .ops = {
  710. .open = intel_hdmi_playback_pcm_open,
  711. .close = intel_hdmi_playback_pcm_close,
  712. .prepare = intel_hdmi_playback_pcm_prepare
  713. },
  714. };
  715. static int intel_hdmi_build_pcms(struct hda_codec *codec)
  716. {
  717. struct intel_hdmi_spec *spec = codec->spec;
  718. struct hda_pcm *info = &spec->pcm_rec;
  719. codec->num_pcms = 1;
  720. codec->pcm_info = info;
  721. info->name = "INTEL HDMI";
  722. info->pcm_type = HDA_PCM_TYPE_HDMI;
  723. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = intel_hdmi_pcm_playback;
  724. return 0;
  725. }
  726. static int intel_hdmi_build_controls(struct hda_codec *codec)
  727. {
  728. struct intel_hdmi_spec *spec = codec->spec;
  729. int err;
  730. err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
  731. if (err < 0)
  732. return err;
  733. return 0;
  734. }
  735. static int intel_hdmi_init(struct hda_codec *codec)
  736. {
  737. /* disable audio output as early as possible */
  738. hdmi_disable_output(codec);
  739. snd_hda_sequence_write(codec, unsolicited_response_verb);
  740. return 0;
  741. }
  742. static void intel_hdmi_free(struct hda_codec *codec)
  743. {
  744. kfree(codec->spec);
  745. }
  746. static struct hda_codec_ops intel_hdmi_patch_ops = {
  747. .init = intel_hdmi_init,
  748. .free = intel_hdmi_free,
  749. .build_pcms = intel_hdmi_build_pcms,
  750. .build_controls = intel_hdmi_build_controls,
  751. .unsol_event = intel_hdmi_unsol_event,
  752. };
  753. static int patch_intel_hdmi(struct hda_codec *codec)
  754. {
  755. struct intel_hdmi_spec *spec;
  756. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  757. if (spec == NULL)
  758. return -ENOMEM;
  759. spec->multiout.num_dacs = 0; /* no analog */
  760. spec->multiout.max_channels = 8;
  761. spec->multiout.dig_out_nid = CVT_NID;
  762. codec->spec = spec;
  763. codec->patch_ops = intel_hdmi_patch_ops;
  764. return 0;
  765. }
  766. struct hda_codec_preset snd_hda_preset_intelhdmi[] = {
  767. { .id = 0x808629fb, .name = "INTEL G45 DEVCL", .patch = patch_intel_hdmi },
  768. { .id = 0x80862801, .name = "INTEL G45 DEVBLC", .patch = patch_intel_hdmi },
  769. { .id = 0x80862802, .name = "INTEL G45 DEVCTG", .patch = patch_intel_hdmi },
  770. { .id = 0x80862803, .name = "INTEL G45 DEVELK", .patch = patch_intel_hdmi },
  771. {} /* terminator */
  772. };