patch_intelhdmi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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 "hda_codec.h"
  33. #include "hda_local.h"
  34. #define CVT_NID 0x02 /* audio converter */
  35. #define PIN_NID 0x03 /* HDMI output pin */
  36. #define INTEL_HDMI_EVENT_TAG 0x08
  37. struct intel_hdmi_spec {
  38. struct hda_multi_out multiout;
  39. struct hda_pcm pcm_rec;
  40. struct hdmi_eld sink_eld;
  41. };
  42. static struct hda_verb pinout_enable_verb[] = {
  43. {PIN_NID, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT},
  44. {} /* terminator */
  45. };
  46. static struct hda_verb unsolicited_response_verb[] = {
  47. {PIN_NID, AC_VERB_SET_UNSOLICITED_ENABLE, AC_USRSP_EN |
  48. INTEL_HDMI_EVENT_TAG},
  49. {}
  50. };
  51. static struct hda_verb def_chan_map[] = {
  52. {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x00},
  53. {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x11},
  54. {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x22},
  55. {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x33},
  56. {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x44},
  57. {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x55},
  58. {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x66},
  59. {CVT_NID, AC_VERB_SET_HDMI_CHAN_SLOT, 0x77},
  60. {}
  61. };
  62. struct hdmi_audio_infoframe {
  63. u8 type; /* 0x84 */
  64. u8 ver; /* 0x01 */
  65. u8 len; /* 0x0a */
  66. u8 checksum; /* PB0 */
  67. u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
  68. u8 SS01_SF24;
  69. u8 CXT04;
  70. u8 CA;
  71. u8 LFEPBL01_LSV36_DM_INH7;
  72. u8 reserved[5]; /* PB6 - PB10 */
  73. };
  74. /*
  75. * CEA speaker placement:
  76. *
  77. * FLH FCH FRH
  78. * FLW FL FLC FC FRC FR FRW
  79. *
  80. * LFE
  81. * TC
  82. *
  83. * RL RLC RC RRC RR
  84. *
  85. * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
  86. * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
  87. */
  88. enum cea_speaker_placement {
  89. FL = (1 << 0), /* Front Left */
  90. FC = (1 << 1), /* Front Center */
  91. FR = (1 << 2), /* Front Right */
  92. FLC = (1 << 3), /* Front Left Center */
  93. FRC = (1 << 4), /* Front Right Center */
  94. RL = (1 << 5), /* Rear Left */
  95. RC = (1 << 6), /* Rear Center */
  96. RR = (1 << 7), /* Rear Right */
  97. RLC = (1 << 8), /* Rear Left Center */
  98. RRC = (1 << 9), /* Rear Right Center */
  99. LFE = (1 << 10), /* Low Frequency Effect */
  100. FLW = (1 << 11), /* Front Left Wide */
  101. FRW = (1 << 12), /* Front Right Wide */
  102. FLH = (1 << 13), /* Front Left High */
  103. FCH = (1 << 14), /* Front Center High */
  104. FRH = (1 << 15), /* Front Right High */
  105. TC = (1 << 16), /* Top Center */
  106. };
  107. /*
  108. * ELD SA bits in the CEA Speaker Allocation data block
  109. */
  110. static int eld_speaker_allocation_bits[] = {
  111. [0] = FL | FR,
  112. [1] = LFE,
  113. [2] = FC,
  114. [3] = RL | RR,
  115. [4] = RC,
  116. [5] = FLC | FRC,
  117. [6] = RLC | RRC,
  118. /* the following are not defined in ELD yet */
  119. [7] = FLW | FRW,
  120. [8] = FLH | FRH,
  121. [9] = TC,
  122. [10] = FCH,
  123. };
  124. struct cea_channel_speaker_allocation {
  125. int ca_index;
  126. int speakers[8];
  127. /* derived values, just for convenience */
  128. int channels;
  129. int spk_mask;
  130. };
  131. /*
  132. * This is an ordered list!
  133. *
  134. * The preceding ones have better chances to be selected by
  135. * hdmi_setup_channel_allocation().
  136. */
  137. static struct cea_channel_speaker_allocation channel_allocations[] = {
  138. /* channel: 8 7 6 5 4 3 2 1 */
  139. { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
  140. /* 2.1 */
  141. { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
  142. /* Dolby Surround */
  143. { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
  144. { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
  145. { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
  146. { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
  147. { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
  148. { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
  149. { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
  150. { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
  151. { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
  152. /* 5.1 */
  153. { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
  154. { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
  155. { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
  156. { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
  157. /* 6.1 */
  158. { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
  159. { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
  160. { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
  161. { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
  162. /* 7.1 */
  163. { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
  164. { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
  165. { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
  166. { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
  167. { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
  168. { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
  169. { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
  170. { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
  171. { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
  172. { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
  173. { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
  174. { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
  175. { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
  176. { .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
  177. { .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
  178. { .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
  179. { .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
  180. { .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
  181. { .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
  182. { .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
  183. { .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
  184. { .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
  185. { .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
  186. { .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
  187. { .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
  188. { .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
  189. { .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
  190. { .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
  191. { .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
  192. { .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
  193. { .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
  194. };
  195. /*
  196. * HDMI routines
  197. */
  198. #ifdef BE_PARANOID
  199. static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t nid,
  200. int *packet_index, int *byte_index)
  201. {
  202. int val;
  203. val = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_HDMI_DIP_INDEX, 0);
  204. *packet_index = val >> 5;
  205. *byte_index = val & 0x1f;
  206. }
  207. #endif
  208. static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t nid,
  209. int packet_index, int byte_index)
  210. {
  211. int val;
  212. val = (packet_index << 5) | (byte_index & 0x1f);
  213. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
  214. }
  215. static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t nid,
  216. unsigned char val)
  217. {
  218. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
  219. }
  220. static void hdmi_enable_output(struct hda_codec *codec)
  221. {
  222. /* Unmute */
  223. if (get_wcaps(codec, PIN_NID) & AC_WCAP_OUT_AMP)
  224. snd_hda_codec_write(codec, PIN_NID, 0,
  225. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
  226. /* Enable pin out */
  227. snd_hda_sequence_write(codec, pinout_enable_verb);
  228. }
  229. /*
  230. * Enable Audio InfoFrame Transmission
  231. */
  232. static void hdmi_start_infoframe_trans(struct hda_codec *codec)
  233. {
  234. hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
  235. snd_hda_codec_write(codec, PIN_NID, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  236. AC_DIPXMIT_BEST);
  237. }
  238. /*
  239. * Disable Audio InfoFrame Transmission
  240. */
  241. static void hdmi_stop_infoframe_trans(struct hda_codec *codec)
  242. {
  243. hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
  244. snd_hda_codec_write(codec, PIN_NID, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  245. AC_DIPXMIT_DISABLE);
  246. }
  247. static int hdmi_get_channel_count(struct hda_codec *codec)
  248. {
  249. return 1 + snd_hda_codec_read(codec, CVT_NID, 0,
  250. AC_VERB_GET_CVT_CHAN_COUNT, 0);
  251. }
  252. static void hdmi_set_channel_count(struct hda_codec *codec, int chs)
  253. {
  254. snd_hda_codec_write(codec, CVT_NID, 0,
  255. AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
  256. if (chs != hdmi_get_channel_count(codec))
  257. snd_printd(KERN_INFO "HDMI channel count: expect %d, get %d\n",
  258. chs, hdmi_get_channel_count(codec));
  259. }
  260. static void hdmi_debug_channel_mapping(struct hda_codec *codec)
  261. {
  262. #ifdef CONFIG_SND_DEBUG_VERBOSE
  263. int i;
  264. int slot;
  265. for (i = 0; i < 8; i++) {
  266. slot = snd_hda_codec_read(codec, CVT_NID, 0,
  267. AC_VERB_GET_HDMI_CHAN_SLOT, i);
  268. printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
  269. slot >> 4, slot & 0x7);
  270. }
  271. #endif
  272. }
  273. static void hdmi_parse_eld(struct hda_codec *codec)
  274. {
  275. struct intel_hdmi_spec *spec = codec->spec;
  276. struct hdmi_eld *eld = &spec->sink_eld;
  277. if (!snd_hdmi_get_eld(eld, codec, PIN_NID))
  278. snd_hdmi_show_eld(eld);
  279. }
  280. /*
  281. * Audio InfoFrame routines
  282. */
  283. static void hdmi_debug_dip_size(struct hda_codec *codec)
  284. {
  285. #ifdef CONFIG_SND_DEBUG_VERBOSE
  286. int i;
  287. int size;
  288. size = snd_hdmi_get_eld_size(codec, PIN_NID);
  289. printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
  290. for (i = 0; i < 8; i++) {
  291. size = snd_hda_codec_read(codec, PIN_NID, 0,
  292. AC_VERB_GET_HDMI_DIP_SIZE, i);
  293. printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
  294. }
  295. #endif
  296. }
  297. static void hdmi_clear_dip_buffers(struct hda_codec *codec)
  298. {
  299. #ifdef BE_PARANOID
  300. int i, j;
  301. int size;
  302. int pi, bi;
  303. for (i = 0; i < 8; i++) {
  304. size = snd_hda_codec_read(codec, PIN_NID, 0,
  305. AC_VERB_GET_HDMI_DIP_SIZE, i);
  306. if (size == 0)
  307. continue;
  308. hdmi_set_dip_index(codec, PIN_NID, i, 0x0);
  309. for (j = 1; j < 1000; j++) {
  310. hdmi_write_dip_byte(codec, PIN_NID, 0x0);
  311. hdmi_get_dip_index(codec, PIN_NID, &pi, &bi);
  312. if (pi != i)
  313. snd_printd(KERN_INFO "dip index %d: %d != %d\n",
  314. bi, pi, i);
  315. if (bi == 0) /* byte index wrapped around */
  316. break;
  317. }
  318. snd_printd(KERN_INFO
  319. "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
  320. i, size, j);
  321. }
  322. #endif
  323. }
  324. static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
  325. struct hdmi_audio_infoframe *ai)
  326. {
  327. u8 *params = (u8 *)ai;
  328. u8 sum = 0;
  329. int i;
  330. hdmi_debug_dip_size(codec);
  331. hdmi_clear_dip_buffers(codec); /* be paranoid */
  332. for (i = 0; i < sizeof(ai); i++)
  333. sum += params[i];
  334. ai->checksum = - sum;
  335. hdmi_set_dip_index(codec, PIN_NID, 0x0, 0x0);
  336. for (i = 0; i < sizeof(ai); i++)
  337. hdmi_write_dip_byte(codec, PIN_NID, params[i]);
  338. }
  339. /*
  340. * Compute derived values in channel_allocations[].
  341. */
  342. static void init_channel_allocations(void)
  343. {
  344. int i, j;
  345. struct cea_channel_speaker_allocation *p;
  346. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  347. p = channel_allocations + i;
  348. p->channels = 0;
  349. p->spk_mask = 0;
  350. for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
  351. if (p->speakers[j]) {
  352. p->channels++;
  353. p->spk_mask |= p->speakers[j];
  354. }
  355. }
  356. }
  357. /*
  358. * The transformation takes two steps:
  359. *
  360. * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
  361. * spk_mask => (channel_allocations[]) => ai->CA
  362. *
  363. * TODO: it could select the wrong CA from multiple candidates.
  364. */
  365. static int hdmi_setup_channel_allocation(struct hda_codec *codec,
  366. struct hdmi_audio_infoframe *ai)
  367. {
  368. struct intel_hdmi_spec *spec = codec->spec;
  369. struct hdmi_eld *eld = &spec->sink_eld;
  370. int i;
  371. int spk_mask = 0;
  372. int channels = 1 + (ai->CC02_CT47 & 0x7);
  373. char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
  374. /*
  375. * CA defaults to 0 for basic stereo audio
  376. */
  377. if (channels <= 2)
  378. return 0;
  379. /*
  380. * HDMI sink's ELD info cannot always be retrieved for now, e.g.
  381. * in console or for audio devices. Assume the highest speakers
  382. * configuration, to _not_ prohibit multi-channel audio playback.
  383. */
  384. if (!eld->spk_alloc)
  385. eld->spk_alloc = 0xffff;
  386. /*
  387. * expand ELD's speaker allocation mask
  388. *
  389. * ELD tells the speaker mask in a compact(paired) form,
  390. * expand ELD's notions to match the ones used by Audio InfoFrame.
  391. */
  392. for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
  393. if (eld->spk_alloc & (1 << i))
  394. spk_mask |= eld_speaker_allocation_bits[i];
  395. }
  396. /* search for the first working match in the CA table */
  397. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  398. if (channels == channel_allocations[i].channels &&
  399. (spk_mask & channel_allocations[i].spk_mask) ==
  400. channel_allocations[i].spk_mask) {
  401. ai->CA = channel_allocations[i].ca_index;
  402. break;
  403. }
  404. }
  405. snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
  406. snd_printdd(KERN_INFO
  407. "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
  408. ai->CA, channels, buf);
  409. return ai->CA;
  410. }
  411. static void hdmi_setup_channel_mapping(struct hda_codec *codec,
  412. struct hdmi_audio_infoframe *ai)
  413. {
  414. if (!ai->CA)
  415. return;
  416. /*
  417. * TODO: adjust channel mapping if necessary
  418. * ALSA sequence is front/surr/clfe/side?
  419. */
  420. snd_hda_sequence_write(codec, def_chan_map);
  421. hdmi_debug_channel_mapping(codec);
  422. }
  423. static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
  424. struct snd_pcm_substream *substream)
  425. {
  426. struct hdmi_audio_infoframe ai = {
  427. .type = 0x84,
  428. .ver = 0x01,
  429. .len = 0x0a,
  430. .CC02_CT47 = substream->runtime->channels - 1,
  431. };
  432. hdmi_setup_channel_allocation(codec, &ai);
  433. hdmi_setup_channel_mapping(codec, &ai);
  434. hdmi_fill_audio_infoframe(codec, &ai);
  435. hdmi_start_infoframe_trans(codec);
  436. }
  437. /*
  438. * Unsolicited events
  439. */
  440. static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
  441. {
  442. int pind = !!(res & AC_UNSOL_RES_PD);
  443. int eldv = !!(res & AC_UNSOL_RES_ELDV);
  444. printk(KERN_INFO
  445. "HDMI hot plug event: Presence_Detect=%d ELD_Valid=%d\n",
  446. pind, eldv);
  447. if (pind && eldv) {
  448. hdmi_parse_eld(codec);
  449. /* TODO: do real things about ELD */
  450. }
  451. }
  452. static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
  453. {
  454. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  455. int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
  456. int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
  457. printk(KERN_INFO
  458. "HDMI content protection event: SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
  459. subtag,
  460. cp_state,
  461. cp_ready);
  462. /* TODO */
  463. if (cp_state)
  464. ;
  465. if (cp_ready)
  466. ;
  467. }
  468. static void intel_hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
  469. {
  470. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  471. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  472. if (tag != INTEL_HDMI_EVENT_TAG) {
  473. snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
  474. return;
  475. }
  476. if (subtag == 0)
  477. hdmi_intrinsic_event(codec, res);
  478. else
  479. hdmi_non_intrinsic_event(codec, res);
  480. }
  481. /*
  482. * Callbacks
  483. */
  484. static int intel_hdmi_playback_pcm_open(struct hda_pcm_stream *hinfo,
  485. struct hda_codec *codec,
  486. struct snd_pcm_substream *substream)
  487. {
  488. struct intel_hdmi_spec *spec = codec->spec;
  489. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  490. }
  491. static int intel_hdmi_playback_pcm_close(struct hda_pcm_stream *hinfo,
  492. struct hda_codec *codec,
  493. struct snd_pcm_substream *substream)
  494. {
  495. struct intel_hdmi_spec *spec = codec->spec;
  496. hdmi_stop_infoframe_trans(codec);
  497. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  498. }
  499. static int intel_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  500. struct hda_codec *codec,
  501. unsigned int stream_tag,
  502. unsigned int format,
  503. struct snd_pcm_substream *substream)
  504. {
  505. struct intel_hdmi_spec *spec = codec->spec;
  506. snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
  507. format, substream);
  508. hdmi_set_channel_count(codec, substream->runtime->channels);
  509. hdmi_setup_audio_infoframe(codec, substream);
  510. return 0;
  511. }
  512. static struct hda_pcm_stream intel_hdmi_pcm_playback = {
  513. .substreams = 1,
  514. .channels_min = 2,
  515. .channels_max = 8,
  516. .nid = CVT_NID, /* NID to query formats and rates and setup streams */
  517. .ops = {
  518. .open = intel_hdmi_playback_pcm_open,
  519. .close = intel_hdmi_playback_pcm_close,
  520. .prepare = intel_hdmi_playback_pcm_prepare
  521. },
  522. };
  523. static int intel_hdmi_build_pcms(struct hda_codec *codec)
  524. {
  525. struct intel_hdmi_spec *spec = codec->spec;
  526. struct hda_pcm *info = &spec->pcm_rec;
  527. codec->num_pcms = 1;
  528. codec->pcm_info = info;
  529. info->name = "INTEL HDMI";
  530. info->pcm_type = HDA_PCM_TYPE_HDMI;
  531. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = intel_hdmi_pcm_playback;
  532. return 0;
  533. }
  534. static int intel_hdmi_build_controls(struct hda_codec *codec)
  535. {
  536. struct intel_hdmi_spec *spec = codec->spec;
  537. int err;
  538. err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
  539. if (err < 0)
  540. return err;
  541. return 0;
  542. }
  543. static int intel_hdmi_init(struct hda_codec *codec)
  544. {
  545. hdmi_enable_output(codec);
  546. snd_hda_sequence_write(codec, unsolicited_response_verb);
  547. return 0;
  548. }
  549. static void intel_hdmi_free(struct hda_codec *codec)
  550. {
  551. struct intel_hdmi_spec *spec = codec->spec;
  552. snd_hda_eld_proc_free(codec, &spec->sink_eld);
  553. kfree(spec);
  554. }
  555. static struct hda_codec_ops intel_hdmi_patch_ops = {
  556. .init = intel_hdmi_init,
  557. .free = intel_hdmi_free,
  558. .build_pcms = intel_hdmi_build_pcms,
  559. .build_controls = intel_hdmi_build_controls,
  560. .unsol_event = intel_hdmi_unsol_event,
  561. };
  562. static int patch_intel_hdmi(struct hda_codec *codec)
  563. {
  564. struct intel_hdmi_spec *spec;
  565. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  566. if (spec == NULL)
  567. return -ENOMEM;
  568. spec->multiout.num_dacs = 0; /* no analog */
  569. spec->multiout.max_channels = 8;
  570. spec->multiout.dig_out_nid = CVT_NID;
  571. codec->spec = spec;
  572. codec->patch_ops = intel_hdmi_patch_ops;
  573. snd_hda_eld_proc_new(codec, &spec->sink_eld);
  574. init_channel_allocations();
  575. return 0;
  576. }
  577. static struct hda_codec_preset snd_hda_preset_intelhdmi[] = {
  578. { .id = 0x808629fb, .name = "G45 DEVCL", .patch = patch_intel_hdmi },
  579. { .id = 0x80862801, .name = "G45 DEVBLC", .patch = patch_intel_hdmi },
  580. { .id = 0x80862802, .name = "G45 DEVCTG", .patch = patch_intel_hdmi },
  581. { .id = 0x80862803, .name = "G45 DEVELK", .patch = patch_intel_hdmi },
  582. { .id = 0x80862804, .name = "G45 DEVIBX", .patch = patch_intel_hdmi },
  583. { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_intel_hdmi },
  584. {} /* terminator */
  585. };
  586. MODULE_ALIAS("snd-hda-codec-id:808629fb");
  587. MODULE_ALIAS("snd-hda-codec-id:80862801");
  588. MODULE_ALIAS("snd-hda-codec-id:80862802");
  589. MODULE_ALIAS("snd-hda-codec-id:80862803");
  590. MODULE_ALIAS("snd-hda-codec-id:80862804");
  591. MODULE_ALIAS("snd-hda-codec-id:10951392");
  592. MODULE_LICENSE("GPL");
  593. MODULE_DESCRIPTION("Intel HDMI HD-audio codec");
  594. static struct hda_codec_preset_list intel_list = {
  595. .preset = snd_hda_preset_intelhdmi,
  596. .owner = THIS_MODULE,
  597. };
  598. static int __init patch_intelhdmi_init(void)
  599. {
  600. return snd_hda_add_codec_preset(&intel_list);
  601. }
  602. static void __exit patch_intelhdmi_exit(void)
  603. {
  604. snd_hda_delete_codec_preset(&intel_list);
  605. }
  606. module_init(patch_intelhdmi_init)
  607. module_exit(patch_intelhdmi_exit)