patch_intelhdmi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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. #ifdef BE_PARANOID
  255. static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t nid,
  256. int *packet_index, int *byte_index)
  257. {
  258. int val;
  259. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_INDEX, 0);
  260. *packet_index = val >> 5;
  261. *byte_index = val & 0x1f;
  262. }
  263. #endif
  264. static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t nid,
  265. int packet_index, int byte_index)
  266. {
  267. int val;
  268. val = (packet_index << 5) | (byte_index & 0x1f);
  269. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
  270. }
  271. static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t nid,
  272. unsigned char val)
  273. {
  274. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
  275. }
  276. static void hdmi_enable_output(struct hda_codec *codec)
  277. {
  278. /* Enable pin out and unmute */
  279. snd_hda_sequence_write(codec, pinout_enable_verb);
  280. if (get_wcaps(codec, PIN_NID) & AC_WCAP_OUT_AMP)
  281. snd_hda_codec_write(codec, PIN_NID, 0,
  282. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
  283. /* Enable Audio InfoFrame Transmission */
  284. hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
  285. snd_hda_codec_write(codec, PIN_NID, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  286. AC_DIPXMIT_BEST);
  287. }
  288. static void hdmi_disable_output(struct hda_codec *codec)
  289. {
  290. snd_hda_sequence_write(codec, pinout_disable_verb);
  291. if (get_wcaps(codec, PIN_NID) & AC_WCAP_OUT_AMP)
  292. snd_hda_codec_write(codec, PIN_NID, 0,
  293. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_MUTE);
  294. /*
  295. * FIXME: noises may arise when playing music after reloading the
  296. * kernel module, until the next X restart or monitor repower.
  297. */
  298. }
  299. static int hdmi_get_channel_count(struct hda_codec *codec)
  300. {
  301. return 1 + snd_hda_codec_read(codec, CVT_NID, 0,
  302. AC_VERB_GET_CVT_CHAN_COUNT, 0);
  303. }
  304. static void hdmi_set_channel_count(struct hda_codec *codec, int chs)
  305. {
  306. snd_hda_codec_write(codec, CVT_NID, 0,
  307. AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
  308. if (chs != hdmi_get_channel_count(codec))
  309. snd_printd(KERN_INFO "Channel count expect=%d, real=%d\n",
  310. chs, hdmi_get_channel_count(codec));
  311. }
  312. static void hdmi_debug_slot_mapping(struct hda_codec *codec)
  313. {
  314. #ifdef CONFIG_SND_DEBUG_VERBOSE
  315. int i;
  316. int slot;
  317. for (i = 0; i < 8; i++) {
  318. slot = snd_hda_codec_read(codec, CVT_NID, 0,
  319. AC_VERB_GET_HDMI_CHAN_SLOT, i);
  320. printk(KERN_DEBUG "ASP channel %d => slot %d\n",
  321. slot >> 4, slot & 0x7);
  322. }
  323. #endif
  324. }
  325. static void hdmi_setup_channel_mapping(struct hda_codec *codec)
  326. {
  327. snd_hda_sequence_write(codec, def_chan_map);
  328. hdmi_debug_slot_mapping(codec);
  329. }
  330. /*
  331. * ELD(EDID Like Data) routines
  332. */
  333. static int hdmi_present_sense(struct hda_codec *codec, hda_nid_t nid)
  334. {
  335. return snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_PIN_SENSE, 0);
  336. }
  337. static void hdmi_debug_present_sense(struct hda_codec *codec)
  338. {
  339. #ifdef CONFIG_SND_DEBUG_VERBOSE
  340. int eldv;
  341. int present;
  342. present = hdmi_present_sense(codec, PIN_NID);
  343. eldv = (present & AC_PINSENSE_ELDV);
  344. present = (present & AC_PINSENSE_PRESENCE);
  345. printk(KERN_INFO "pinp = %d, eldv = %d\n", !!present, !!eldv);
  346. #endif
  347. }
  348. static unsigned char hdmi_get_eld_byte(struct hda_codec *codec, int byte_index)
  349. {
  350. unsigned int val;
  351. val = snd_hda_codec_read(codec, PIN_NID, 0,
  352. AC_VERB_GET_HDMI_ELDD, byte_index);
  353. #ifdef BE_PARANOID
  354. printk(KERN_INFO "ELD data byte %d: 0x%x\n", byte_index, val);
  355. #endif
  356. if ((val & AC_ELDD_ELD_VALID) == 0) {
  357. snd_printd(KERN_INFO "Invalid ELD data byte %d\n",
  358. byte_index);
  359. val = 0;
  360. }
  361. return val & AC_ELDD_ELD_DATA;
  362. }
  363. static inline unsigned char grab_bits(const unsigned char *buf,
  364. int byte, int lowbit, int bits)
  365. {
  366. BUG_ON(lowbit > 7);
  367. BUG_ON(bits > 8);
  368. BUG_ON(bits <= 0);
  369. return (buf[byte] >> lowbit) & ((1 << bits) - 1);
  370. }
  371. static void hdmi_update_short_audio_desc(struct cea_sad *a,
  372. const unsigned char *buf)
  373. {
  374. int i;
  375. int val;
  376. val = grab_bits(buf, 1, 0, 7);
  377. a->rates = 0;
  378. for (i = 0; i < 7; i++)
  379. if (val & (1 << i))
  380. a->rates |= cea_sampling_frequencies[i + 1];
  381. a->channels = grab_bits(buf, 0, 0, 3);
  382. a->channels++;
  383. a->format = grab_bits(buf, 0, 3, 4);
  384. switch (a->format) {
  385. case AUDIO_CODING_TYPE_REF_STREAM_HEADER:
  386. snd_printd(KERN_INFO
  387. "audio coding type 0 not expected in ELD\n");
  388. break;
  389. case AUDIO_CODING_TYPE_LPCM:
  390. val = grab_bits(buf, 2, 0, 3);
  391. a->sample_bits = 0;
  392. for (i = 0; i < 3; i++)
  393. if (val & (1 << i))
  394. a->sample_bits |= cea_sample_sizes[i + 1];
  395. break;
  396. case AUDIO_CODING_TYPE_AC3:
  397. case AUDIO_CODING_TYPE_MPEG1:
  398. case AUDIO_CODING_TYPE_MP3:
  399. case AUDIO_CODING_TYPE_MPEG2:
  400. case AUDIO_CODING_TYPE_AACLC:
  401. case AUDIO_CODING_TYPE_DTS:
  402. case AUDIO_CODING_TYPE_ATRAC:
  403. a->max_bitrate = grab_bits(buf, 2, 0, 8);
  404. a->max_bitrate *= 8000;
  405. break;
  406. case AUDIO_CODING_TYPE_SACD:
  407. break;
  408. case AUDIO_CODING_TYPE_EAC3:
  409. break;
  410. case AUDIO_CODING_TYPE_DTS_HD:
  411. break;
  412. case AUDIO_CODING_TYPE_MLP:
  413. break;
  414. case AUDIO_CODING_TYPE_DST:
  415. break;
  416. case AUDIO_CODING_TYPE_WMAPRO:
  417. a->profile = grab_bits(buf, 2, 0, 3);
  418. break;
  419. case AUDIO_CODING_TYPE_REF_CXT:
  420. a->format = grab_bits(buf, 2, 3, 5);
  421. if (a->format == AUDIO_CODING_XTYPE_HE_REF_CT ||
  422. a->format >= AUDIO_CODING_XTYPE_FIRST_RESERVED) {
  423. snd_printd(KERN_INFO
  424. "audio coding xtype %d not expected in ELD\n",
  425. a->format);
  426. a->format = 0;
  427. } else
  428. a->format += AUDIO_CODING_TYPE_HE_AAC -
  429. AUDIO_CODING_XTYPE_HE_AAC;
  430. break;
  431. }
  432. }
  433. static int hdmi_update_sink_eld(struct hda_codec *codec,
  434. const unsigned char *buf, int size)
  435. {
  436. struct intel_hdmi_spec *spec = codec->spec;
  437. struct sink_eld *e = &spec->sink;
  438. int mnl;
  439. int i;
  440. e->eld_ver = grab_bits(buf, 0, 3, 5);
  441. if (e->eld_ver != ELD_VER_CEA_861D &&
  442. e->eld_ver != ELD_VER_PARTIAL) {
  443. snd_printd(KERN_INFO "Unknown ELD version %d\n", e->eld_ver);
  444. goto out_fail;
  445. }
  446. e->eld_size = size;
  447. e->baseline_len = grab_bits(buf, 2, 0, 8);
  448. mnl = grab_bits(buf, 4, 0, 5);
  449. e->cea_edid_ver = grab_bits(buf, 4, 5, 3);
  450. e->support_hdcp = grab_bits(buf, 5, 0, 1);
  451. e->support_ai = grab_bits(buf, 5, 1, 1);
  452. e->conn_type = grab_bits(buf, 5, 2, 2);
  453. e->sad_count = grab_bits(buf, 5, 4, 4);
  454. e->aud_synch_delay = grab_bits(buf, 6, 0, 8);
  455. e->spk_alloc = grab_bits(buf, 7, 0, 7);
  456. e->port_id = get_unaligned_le64(buf + 8);
  457. /* not specified, but the spec's tendency is little endian */
  458. e->manufacture_id = get_unaligned_le16(buf + 16);
  459. e->product_id = get_unaligned_le16(buf + 18);
  460. if (mnl > ELD_MAX_MNL) {
  461. snd_printd(KERN_INFO "MNL is reserved value %d\n", mnl);
  462. goto out_fail;
  463. } else if (ELD_FIXED_BYTES + mnl > size) {
  464. snd_printd(KERN_INFO "out of range MNL %d\n", mnl);
  465. goto out_fail;
  466. } else
  467. strlcpy(e->monitor_name, buf + ELD_FIXED_BYTES, mnl);
  468. for (i = 0; i < e->sad_count; i++) {
  469. if (ELD_FIXED_BYTES + mnl + 3 * (i + 1) > size) {
  470. snd_printd(KERN_INFO "out of range SAD %d\n", i);
  471. goto out_fail;
  472. }
  473. hdmi_update_short_audio_desc(e->sad + i,
  474. buf + ELD_FIXED_BYTES + mnl + 3 * i);
  475. }
  476. return 0;
  477. out_fail:
  478. e->eld_ver = 0;
  479. return -EINVAL;
  480. }
  481. static int hdmi_get_eld(struct hda_codec *codec)
  482. {
  483. int i;
  484. int ret;
  485. int size;
  486. unsigned char *buf;
  487. i = hdmi_present_sense(codec, PIN_NID) & AC_PINSENSE_ELDV;
  488. if (!i)
  489. return -ENOENT;
  490. size = hdmi_get_eld_size(codec, PIN_NID);
  491. if (size == 0) {
  492. /* wfg: workaround for ASUS P5E-VM HDMI board */
  493. snd_printd(KERN_INFO "ELD buf size is 0, force 128\n");
  494. size = 128;
  495. }
  496. if (size < ELD_FIXED_BYTES || size > PAGE_SIZE) {
  497. snd_printd(KERN_INFO "Invalid ELD buf size %d\n", size);
  498. return -ERANGE;
  499. }
  500. buf = kmalloc(size, GFP_KERNEL);
  501. if (!buf)
  502. return -ENOMEM;
  503. for (i = 0; i < size; i++)
  504. buf[i] = hdmi_get_eld_byte(codec, i);
  505. ret = hdmi_update_sink_eld(codec, buf, size);
  506. kfree(buf);
  507. return ret;
  508. }
  509. static void hdmi_show_short_audio_desc(struct cea_sad *a)
  510. {
  511. printk(KERN_INFO "coding type: %s\n",
  512. cea_audio_coding_type_names[a->format]);
  513. printk(KERN_INFO "channels: %d\n", a->channels);
  514. printk(KERN_INFO "sampling frequencies: 0x%x\n", a->rates);
  515. if (a->format == AUDIO_CODING_TYPE_LPCM)
  516. printk(KERN_INFO "sample bits: 0x%x\n", a->sample_bits);
  517. if (a->max_bitrate)
  518. printk(KERN_INFO "max bitrate: %d HZ\n", a->max_bitrate);
  519. if (a->profile)
  520. printk(KERN_INFO "profile: %d\n", a->profile);
  521. }
  522. static void hdmi_show_eld(struct hda_codec *codec)
  523. {
  524. int i;
  525. int j;
  526. struct intel_hdmi_spec *spec = codec->spec;
  527. struct sink_eld *e = &spec->sink;
  528. char buf[80];
  529. printk(KERN_INFO "ELD buffer size is %d\n", e->eld_size);
  530. printk(KERN_INFO "ELD baseline len is %d*4\n", e->baseline_len);
  531. printk(KERN_INFO "vendor block len is %d\n",
  532. e->eld_size - e->baseline_len * 4 - 4);
  533. printk(KERN_INFO "ELD version is %s\n",
  534. eld_versoin_names[e->eld_ver]);
  535. printk(KERN_INFO "CEA EDID version is %s\n",
  536. cea_edid_version_names[e->cea_edid_ver]);
  537. printk(KERN_INFO "manufacture id is 0x%x\n", e->manufacture_id);
  538. printk(KERN_INFO "product id is 0x%x\n", e->product_id);
  539. printk(KERN_INFO "port id is 0x%llx\n", (long long)e->port_id);
  540. printk(KERN_INFO "HDCP support is %d\n", e->support_hdcp);
  541. printk(KERN_INFO "AI support is %d\n", e->support_ai);
  542. printk(KERN_INFO "SAD count is %d\n", e->sad_count);
  543. printk(KERN_INFO "audio sync delay is %x\n", e->aud_synch_delay);
  544. printk(KERN_INFO "connection type is %s\n",
  545. eld_connection_type_names[e->conn_type]);
  546. printk(KERN_INFO "monitor name is %s\n", e->monitor_name);
  547. j = 0;
  548. for (i = 0; i < ARRAY_SIZE(cea_speaker_allocation_names); i++) {
  549. if (e->spk_alloc & (1 << i))
  550. j += snprintf(buf + j, sizeof(buf) - j, " %s",
  551. cea_speaker_allocation_names[i]);
  552. }
  553. buf[j] = '\0'; /* necessary when j == 0 */
  554. printk(KERN_INFO "speaker allocations: (0x%x)%s\n", e->spk_alloc, buf);
  555. for (i = 0; i < e->sad_count; i++)
  556. hdmi_show_short_audio_desc(e->sad + i);
  557. }
  558. /*
  559. * Be careful, ELD buf could be totally rubbish!
  560. */
  561. static void hdmi_parse_eld(struct hda_codec *codec)
  562. {
  563. hdmi_debug_present_sense(codec);
  564. if (!hdmi_get_eld(codec))
  565. hdmi_show_eld(codec);
  566. }
  567. /*
  568. * Audio Infoframe routines
  569. */
  570. static void hdmi_debug_dip_size(struct hda_codec *codec)
  571. {
  572. #ifdef CONFIG_SND_DEBUG_VERBOSE
  573. int i;
  574. int size;
  575. size = hdmi_get_eld_size(codec, PIN_NID);
  576. printk(KERN_DEBUG "ELD buf size is %d\n", size);
  577. for (i = 0; i < 8; i++) {
  578. size = snd_hda_codec_read(codec, PIN_NID, 0,
  579. AC_VERB_GET_HDMI_DIP_SIZE, i);
  580. printk(KERN_DEBUG "DIP GP[%d] buf size is %d\n", i, size);
  581. }
  582. #endif
  583. }
  584. static void hdmi_clear_dip_buffers(struct hda_codec *codec)
  585. {
  586. #ifdef BE_PARANOID
  587. int i, j;
  588. int size;
  589. int pi, bi;
  590. for (i = 0; i < 8; i++) {
  591. size = snd_hda_codec_read(codec, PIN_NID, 0,
  592. AC_VERB_GET_HDMI_DIP_SIZE, i);
  593. if (size == 0)
  594. continue;
  595. hdmi_set_dip_index(codec, PIN_NID, i, 0x0);
  596. for (j = 1; j < 1000; j++) {
  597. hdmi_write_dip_byte(codec, PIN_NID, 0x0);
  598. hdmi_get_dip_index(codec, PIN_NID, &pi, &bi);
  599. if (pi != i)
  600. snd_printd(KERN_INFO "dip index %d: %d != %d\n",
  601. bi, pi, i);
  602. if (bi == 0) /* byte index wrapped around */
  603. break;
  604. }
  605. snd_printd(KERN_INFO
  606. "DIP GP[%d] buf reported size=%d, written=%d\n",
  607. i, size, j);
  608. }
  609. #endif
  610. }
  611. static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
  612. struct snd_pcm_substream *substream)
  613. {
  614. struct hdmi_audio_infoframe audio_infoframe = {
  615. .type = 0x84,
  616. .ver = 0x01,
  617. .len = 0x0a,
  618. .CC02_CT47 = substream->runtime->channels - 1,
  619. };
  620. u8 *params = (u8 *)&audio_infoframe;
  621. int i;
  622. hdmi_debug_dip_size(codec);
  623. hdmi_clear_dip_buffers(codec); /* be paranoid */
  624. hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
  625. for (i = 0; i < sizeof(audio_infoframe); i++)
  626. hdmi_write_dip_byte(codec, PIN_NID, params[i]);
  627. }
  628. /*
  629. * Unsolicited events
  630. */
  631. static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
  632. {
  633. int pind = !!(res & AC_UNSOL_RES_PD);
  634. int eldv = !!(res & AC_UNSOL_RES_ELDV);
  635. printk(KERN_INFO "HDMI intrinsic event: PD=%d ELDV=%d\n", pind, eldv);
  636. if (pind && eldv) {
  637. hdmi_parse_eld(codec);
  638. /* TODO: do real things about ELD */
  639. }
  640. }
  641. static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
  642. {
  643. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  644. int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
  645. int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
  646. printk(KERN_INFO "HDMI non-intrinsic event: "
  647. "SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
  648. subtag,
  649. cp_state,
  650. cp_ready);
  651. /* who cares? */
  652. if (cp_state)
  653. ;
  654. if (cp_ready)
  655. ;
  656. }
  657. static void intel_hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
  658. {
  659. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  660. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  661. if (tag != INTEL_HDMI_EVENT_TAG) {
  662. snd_printd(KERN_INFO
  663. "Unexpected HDMI unsolicited event tag 0x%x\n",
  664. tag);
  665. return;
  666. }
  667. if (subtag == 0)
  668. hdmi_intrinsic_event(codec, res);
  669. else
  670. hdmi_non_intrinsic_event(codec, res);
  671. }
  672. /*
  673. * Callbacks
  674. */
  675. static int intel_hdmi_playback_pcm_open(struct hda_pcm_stream *hinfo,
  676. struct hda_codec *codec,
  677. struct snd_pcm_substream *substream)
  678. {
  679. struct intel_hdmi_spec *spec = codec->spec;
  680. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  681. }
  682. static int intel_hdmi_playback_pcm_close(struct hda_pcm_stream *hinfo,
  683. struct hda_codec *codec,
  684. struct snd_pcm_substream *substream)
  685. {
  686. struct intel_hdmi_spec *spec = codec->spec;
  687. hdmi_disable_output(codec);
  688. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  689. }
  690. static int intel_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  691. struct hda_codec *codec,
  692. unsigned int stream_tag,
  693. unsigned int format,
  694. struct snd_pcm_substream *substream)
  695. {
  696. struct intel_hdmi_spec *spec = codec->spec;
  697. snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
  698. format, substream);
  699. hdmi_set_channel_count(codec, substream->runtime->channels);
  700. /* wfg: channel mapping not supported by DEVCTG */
  701. hdmi_setup_channel_mapping(codec);
  702. hdmi_setup_audio_infoframe(codec, substream);
  703. hdmi_enable_output(codec);
  704. return 0;
  705. }
  706. static struct hda_pcm_stream intel_hdmi_pcm_playback = {
  707. .substreams = 1,
  708. .channels_min = 2,
  709. .channels_max = 8,
  710. .nid = CVT_NID, /* NID to query formats and rates and setup streams */
  711. .ops = {
  712. .open = intel_hdmi_playback_pcm_open,
  713. .close = intel_hdmi_playback_pcm_close,
  714. .prepare = intel_hdmi_playback_pcm_prepare
  715. },
  716. };
  717. static int intel_hdmi_build_pcms(struct hda_codec *codec)
  718. {
  719. struct intel_hdmi_spec *spec = codec->spec;
  720. struct hda_pcm *info = &spec->pcm_rec;
  721. codec->num_pcms = 1;
  722. codec->pcm_info = info;
  723. info->name = "INTEL HDMI";
  724. info->pcm_type = HDA_PCM_TYPE_HDMI;
  725. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = intel_hdmi_pcm_playback;
  726. return 0;
  727. }
  728. static int intel_hdmi_build_controls(struct hda_codec *codec)
  729. {
  730. struct intel_hdmi_spec *spec = codec->spec;
  731. int err;
  732. err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
  733. if (err < 0)
  734. return err;
  735. return 0;
  736. }
  737. static int intel_hdmi_init(struct hda_codec *codec)
  738. {
  739. /* disable audio output as early as possible */
  740. hdmi_disable_output(codec);
  741. snd_hda_sequence_write(codec, unsolicited_response_verb);
  742. return 0;
  743. }
  744. static void intel_hdmi_free(struct hda_codec *codec)
  745. {
  746. kfree(codec->spec);
  747. }
  748. static struct hda_codec_ops intel_hdmi_patch_ops = {
  749. .init = intel_hdmi_init,
  750. .free = intel_hdmi_free,
  751. .build_pcms = intel_hdmi_build_pcms,
  752. .build_controls = intel_hdmi_build_controls,
  753. .unsol_event = intel_hdmi_unsol_event,
  754. };
  755. static int patch_intel_hdmi(struct hda_codec *codec)
  756. {
  757. struct intel_hdmi_spec *spec;
  758. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  759. if (spec == NULL)
  760. return -ENOMEM;
  761. spec->multiout.num_dacs = 0; /* no analog */
  762. spec->multiout.max_channels = 8;
  763. spec->multiout.dig_out_nid = CVT_NID;
  764. codec->spec = spec;
  765. codec->patch_ops = intel_hdmi_patch_ops;
  766. return 0;
  767. }
  768. struct hda_codec_preset snd_hda_preset_intelhdmi[] = {
  769. { .id = 0x808629fb, .name = "INTEL G45 DEVCL", .patch = patch_intel_hdmi },
  770. { .id = 0x80862801, .name = "INTEL G45 DEVBLC", .patch = patch_intel_hdmi },
  771. { .id = 0x80862802, .name = "INTEL G45 DEVCTG", .patch = patch_intel_hdmi },
  772. { .id = 0x80862803, .name = "INTEL G45 DEVELK", .patch = patch_intel_hdmi },
  773. { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_intel_hdmi },
  774. {} /* terminator */
  775. };