patch_intelhdmi.c 20 KB

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