patch_hdmi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /*
  2. *
  3. * patch_hdmi.c - routines for HDMI/DisplayPort codecs
  4. *
  5. * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
  6. *
  7. * Authors:
  8. * Wu Fengguang <wfg@linux.intel.com>
  9. *
  10. * Maintained by:
  11. * Wu Fengguang <wfg@linux.intel.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the Free
  15. * Software Foundation; either version 2 of the License, or (at your option)
  16. * any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful, but
  19. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  20. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  21. * for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software Foundation,
  25. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  26. */
  27. struct hdmi_spec {
  28. int num_cvts;
  29. int num_pins;
  30. hda_nid_t cvt[MAX_HDMI_CVTS+1]; /* audio sources */
  31. hda_nid_t pin[MAX_HDMI_PINS+1]; /* audio sinks */
  32. /*
  33. * source connection for each pin
  34. */
  35. hda_nid_t pin_cvt[MAX_HDMI_PINS+1];
  36. /*
  37. * HDMI sink attached to each pin
  38. */
  39. struct hdmi_eld sink_eld[MAX_HDMI_PINS];
  40. /*
  41. * export one pcm per pipe
  42. */
  43. struct hda_pcm pcm_rec[MAX_HDMI_CVTS];
  44. /*
  45. * nvhdmi specific
  46. */
  47. struct hda_multi_out multiout;
  48. unsigned int codec_type;
  49. };
  50. struct hdmi_audio_infoframe {
  51. u8 type; /* 0x84 */
  52. u8 ver; /* 0x01 */
  53. u8 len; /* 0x0a */
  54. u8 checksum; /* PB0 */
  55. u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
  56. u8 SS01_SF24;
  57. u8 CXT04;
  58. u8 CA;
  59. u8 LFEPBL01_LSV36_DM_INH7;
  60. u8 reserved[5]; /* PB6 - PB10 */
  61. };
  62. /*
  63. * CEA speaker placement:
  64. *
  65. * FLH FCH FRH
  66. * FLW FL FLC FC FRC FR FRW
  67. *
  68. * LFE
  69. * TC
  70. *
  71. * RL RLC RC RRC RR
  72. *
  73. * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
  74. * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
  75. */
  76. enum cea_speaker_placement {
  77. FL = (1 << 0), /* Front Left */
  78. FC = (1 << 1), /* Front Center */
  79. FR = (1 << 2), /* Front Right */
  80. FLC = (1 << 3), /* Front Left Center */
  81. FRC = (1 << 4), /* Front Right Center */
  82. RL = (1 << 5), /* Rear Left */
  83. RC = (1 << 6), /* Rear Center */
  84. RR = (1 << 7), /* Rear Right */
  85. RLC = (1 << 8), /* Rear Left Center */
  86. RRC = (1 << 9), /* Rear Right Center */
  87. LFE = (1 << 10), /* Low Frequency Effect */
  88. FLW = (1 << 11), /* Front Left Wide */
  89. FRW = (1 << 12), /* Front Right Wide */
  90. FLH = (1 << 13), /* Front Left High */
  91. FCH = (1 << 14), /* Front Center High */
  92. FRH = (1 << 15), /* Front Right High */
  93. TC = (1 << 16), /* Top Center */
  94. };
  95. /*
  96. * ELD SA bits in the CEA Speaker Allocation data block
  97. */
  98. static int eld_speaker_allocation_bits[] = {
  99. [0] = FL | FR,
  100. [1] = LFE,
  101. [2] = FC,
  102. [3] = RL | RR,
  103. [4] = RC,
  104. [5] = FLC | FRC,
  105. [6] = RLC | RRC,
  106. /* the following are not defined in ELD yet */
  107. [7] = FLW | FRW,
  108. [8] = FLH | FRH,
  109. [9] = TC,
  110. [10] = FCH,
  111. };
  112. struct cea_channel_speaker_allocation {
  113. int ca_index;
  114. int speakers[8];
  115. /* derived values, just for convenience */
  116. int channels;
  117. int spk_mask;
  118. };
  119. /*
  120. * ALSA sequence is:
  121. *
  122. * surround40 surround41 surround50 surround51 surround71
  123. * ch0 front left = = = =
  124. * ch1 front right = = = =
  125. * ch2 rear left = = = =
  126. * ch3 rear right = = = =
  127. * ch4 LFE center center center
  128. * ch5 LFE LFE
  129. * ch6 side left
  130. * ch7 side right
  131. *
  132. * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
  133. */
  134. static int hdmi_channel_mapping[0x32][8] = {
  135. /* stereo */
  136. [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
  137. /* 2.1 */
  138. [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
  139. /* Dolby Surround */
  140. [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
  141. /* surround40 */
  142. [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
  143. /* 4ch */
  144. [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
  145. /* surround41 */
  146. [0x09] = { 0x00, 0x11, 0x24, 0x34, 0x43, 0xf2, 0xf6, 0xf7 },
  147. /* surround50 */
  148. [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
  149. /* surround51 */
  150. [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
  151. /* 7.1 */
  152. [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
  153. };
  154. /*
  155. * This is an ordered list!
  156. *
  157. * The preceding ones have better chances to be selected by
  158. * hdmi_setup_channel_allocation().
  159. */
  160. static struct cea_channel_speaker_allocation channel_allocations[] = {
  161. /* channel: 7 6 5 4 3 2 1 0 */
  162. { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
  163. /* 2.1 */
  164. { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
  165. /* Dolby Surround */
  166. { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
  167. /* surround40 */
  168. { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
  169. /* surround41 */
  170. { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
  171. /* surround50 */
  172. { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
  173. /* surround51 */
  174. { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
  175. /* 6.1 */
  176. { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
  177. /* surround71 */
  178. { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
  179. { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
  180. { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
  181. { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
  182. { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
  183. { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
  184. { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
  185. { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
  186. { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
  187. { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
  188. { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
  189. { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
  190. { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
  191. { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
  192. { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
  193. { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
  194. { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
  195. { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
  196. { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
  197. { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
  198. { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
  199. { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
  200. { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
  201. { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
  202. { .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
  203. { .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
  204. { .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
  205. { .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
  206. { .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
  207. { .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
  208. { .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
  209. { .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
  210. { .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
  211. { .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
  212. { .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
  213. { .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
  214. { .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
  215. { .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
  216. { .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
  217. { .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
  218. { .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
  219. { .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
  220. };
  221. /*
  222. * HDMI routines
  223. */
  224. static int hda_node_index(hda_nid_t *nids, hda_nid_t nid)
  225. {
  226. int i;
  227. for (i = 0; nids[i]; i++)
  228. if (nids[i] == nid)
  229. return i;
  230. snd_printk(KERN_WARNING "HDMI: nid %d not registered\n", nid);
  231. return -EINVAL;
  232. }
  233. static void hdmi_get_show_eld(struct hda_codec *codec, hda_nid_t pin_nid,
  234. struct hdmi_eld *eld)
  235. {
  236. if (!snd_hdmi_get_eld(eld, codec, pin_nid))
  237. snd_hdmi_show_eld(eld);
  238. }
  239. #ifdef BE_PARANOID
  240. static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
  241. int *packet_index, int *byte_index)
  242. {
  243. int val;
  244. val = snd_hda_codec_read(codec, pin_nid, 0,
  245. AC_VERB_GET_HDMI_DIP_INDEX, 0);
  246. *packet_index = val >> 5;
  247. *byte_index = val & 0x1f;
  248. }
  249. #endif
  250. static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
  251. int packet_index, int byte_index)
  252. {
  253. int val;
  254. val = (packet_index << 5) | (byte_index & 0x1f);
  255. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
  256. }
  257. static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
  258. unsigned char val)
  259. {
  260. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
  261. }
  262. static void hdmi_enable_output(struct hda_codec *codec, hda_nid_t pin_nid)
  263. {
  264. /* Unmute */
  265. if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
  266. snd_hda_codec_write(codec, pin_nid, 0,
  267. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
  268. /* Enable pin out */
  269. snd_hda_codec_write(codec, pin_nid, 0,
  270. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  271. }
  272. static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t nid)
  273. {
  274. return 1 + snd_hda_codec_read(codec, nid, 0,
  275. AC_VERB_GET_CVT_CHAN_COUNT, 0);
  276. }
  277. static void hdmi_set_channel_count(struct hda_codec *codec,
  278. hda_nid_t nid, int chs)
  279. {
  280. if (chs != hdmi_get_channel_count(codec, nid))
  281. snd_hda_codec_write(codec, nid, 0,
  282. AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
  283. }
  284. /*
  285. * Channel mapping routines
  286. */
  287. /*
  288. * Compute derived values in channel_allocations[].
  289. */
  290. static void init_channel_allocations(void)
  291. {
  292. int i, j;
  293. struct cea_channel_speaker_allocation *p;
  294. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  295. p = channel_allocations + i;
  296. p->channels = 0;
  297. p->spk_mask = 0;
  298. for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
  299. if (p->speakers[j]) {
  300. p->channels++;
  301. p->spk_mask |= p->speakers[j];
  302. }
  303. }
  304. }
  305. /*
  306. * The transformation takes two steps:
  307. *
  308. * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
  309. * spk_mask => (channel_allocations[]) => ai->CA
  310. *
  311. * TODO: it could select the wrong CA from multiple candidates.
  312. */
  313. static int hdmi_setup_channel_allocation(struct hda_codec *codec, hda_nid_t nid,
  314. struct hdmi_audio_infoframe *ai)
  315. {
  316. struct hdmi_spec *spec = codec->spec;
  317. struct hdmi_eld *eld;
  318. int i;
  319. int spk_mask = 0;
  320. int channels = 1 + (ai->CC02_CT47 & 0x7);
  321. char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
  322. /*
  323. * CA defaults to 0 for basic stereo audio
  324. */
  325. if (channels <= 2)
  326. return 0;
  327. i = hda_node_index(spec->pin_cvt, nid);
  328. if (i < 0)
  329. return 0;
  330. eld = &spec->sink_eld[i];
  331. /*
  332. * HDMI sink's ELD info cannot always be retrieved for now, e.g.
  333. * in console or for audio devices. Assume the highest speakers
  334. * configuration, to _not_ prohibit multi-channel audio playback.
  335. */
  336. if (!eld->spk_alloc)
  337. eld->spk_alloc = 0xffff;
  338. /*
  339. * expand ELD's speaker allocation mask
  340. *
  341. * ELD tells the speaker mask in a compact(paired) form,
  342. * expand ELD's notions to match the ones used by Audio InfoFrame.
  343. */
  344. for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
  345. if (eld->spk_alloc & (1 << i))
  346. spk_mask |= eld_speaker_allocation_bits[i];
  347. }
  348. /* search for the first working match in the CA table */
  349. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  350. if (channels == channel_allocations[i].channels &&
  351. (spk_mask & channel_allocations[i].spk_mask) ==
  352. channel_allocations[i].spk_mask) {
  353. ai->CA = channel_allocations[i].ca_index;
  354. break;
  355. }
  356. }
  357. snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
  358. snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
  359. ai->CA, channels, buf);
  360. return ai->CA;
  361. }
  362. static void hdmi_debug_channel_mapping(struct hda_codec *codec,
  363. hda_nid_t pin_nid)
  364. {
  365. #ifdef CONFIG_SND_DEBUG_VERBOSE
  366. int i;
  367. int slot;
  368. for (i = 0; i < 8; i++) {
  369. slot = snd_hda_codec_read(codec, pin_nid, 0,
  370. AC_VERB_GET_HDMI_CHAN_SLOT, i);
  371. printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
  372. slot >> 4, slot & 0xf);
  373. }
  374. #endif
  375. }
  376. static void hdmi_setup_channel_mapping(struct hda_codec *codec,
  377. hda_nid_t pin_nid,
  378. struct hdmi_audio_infoframe *ai)
  379. {
  380. int i;
  381. int ca = ai->CA;
  382. int err;
  383. if (hdmi_channel_mapping[ca][1] == 0) {
  384. for (i = 0; i < channel_allocations[ca].channels; i++)
  385. hdmi_channel_mapping[ca][i] = i | (i << 4);
  386. for (; i < 8; i++)
  387. hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
  388. }
  389. for (i = 0; i < 8; i++) {
  390. err = snd_hda_codec_write(codec, pin_nid, 0,
  391. AC_VERB_SET_HDMI_CHAN_SLOT,
  392. hdmi_channel_mapping[ca][i]);
  393. if (err) {
  394. snd_printdd(KERN_NOTICE
  395. "HDMI: channel mapping failed\n");
  396. break;
  397. }
  398. }
  399. hdmi_debug_channel_mapping(codec, pin_nid);
  400. }
  401. /*
  402. * Audio InfoFrame routines
  403. */
  404. /*
  405. * Enable Audio InfoFrame Transmission
  406. */
  407. static void hdmi_start_infoframe_trans(struct hda_codec *codec,
  408. hda_nid_t pin_nid)
  409. {
  410. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  411. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  412. AC_DIPXMIT_BEST);
  413. }
  414. /*
  415. * Disable Audio InfoFrame Transmission
  416. */
  417. static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
  418. hda_nid_t pin_nid)
  419. {
  420. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  421. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  422. AC_DIPXMIT_DISABLE);
  423. }
  424. static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
  425. {
  426. #ifdef CONFIG_SND_DEBUG_VERBOSE
  427. int i;
  428. int size;
  429. size = snd_hdmi_get_eld_size(codec, pin_nid);
  430. printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
  431. for (i = 0; i < 8; i++) {
  432. size = snd_hda_codec_read(codec, pin_nid, 0,
  433. AC_VERB_GET_HDMI_DIP_SIZE, i);
  434. printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
  435. }
  436. #endif
  437. }
  438. static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
  439. {
  440. #ifdef BE_PARANOID
  441. int i, j;
  442. int size;
  443. int pi, bi;
  444. for (i = 0; i < 8; i++) {
  445. size = snd_hda_codec_read(codec, pin_nid, 0,
  446. AC_VERB_GET_HDMI_DIP_SIZE, i);
  447. if (size == 0)
  448. continue;
  449. hdmi_set_dip_index(codec, pin_nid, i, 0x0);
  450. for (j = 1; j < 1000; j++) {
  451. hdmi_write_dip_byte(codec, pin_nid, 0x0);
  452. hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
  453. if (pi != i)
  454. snd_printd(KERN_INFO "dip index %d: %d != %d\n",
  455. bi, pi, i);
  456. if (bi == 0) /* byte index wrapped around */
  457. break;
  458. }
  459. snd_printd(KERN_INFO
  460. "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
  461. i, size, j);
  462. }
  463. #endif
  464. }
  465. static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *ai)
  466. {
  467. u8 *bytes = (u8 *)ai;
  468. u8 sum = 0;
  469. int i;
  470. ai->checksum = 0;
  471. for (i = 0; i < sizeof(*ai); i++)
  472. sum += bytes[i];
  473. ai->checksum = -sum;
  474. }
  475. static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
  476. hda_nid_t pin_nid,
  477. struct hdmi_audio_infoframe *ai)
  478. {
  479. u8 *bytes = (u8 *)ai;
  480. int i;
  481. hdmi_debug_dip_size(codec, pin_nid);
  482. hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
  483. hdmi_checksum_audio_infoframe(ai);
  484. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  485. for (i = 0; i < sizeof(*ai); i++)
  486. hdmi_write_dip_byte(codec, pin_nid, bytes[i]);
  487. }
  488. static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
  489. struct hdmi_audio_infoframe *ai)
  490. {
  491. u8 *bytes = (u8 *)ai;
  492. u8 val;
  493. int i;
  494. if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
  495. != AC_DIPXMIT_BEST)
  496. return false;
  497. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  498. for (i = 0; i < sizeof(*ai); i++) {
  499. val = snd_hda_codec_read(codec, pin_nid, 0,
  500. AC_VERB_GET_HDMI_DIP_DATA, 0);
  501. if (val != bytes[i])
  502. return false;
  503. }
  504. return true;
  505. }
  506. static void hdmi_setup_audio_infoframe(struct hda_codec *codec, hda_nid_t nid,
  507. struct snd_pcm_substream *substream)
  508. {
  509. struct hdmi_spec *spec = codec->spec;
  510. hda_nid_t pin_nid;
  511. int i;
  512. struct hdmi_audio_infoframe ai = {
  513. .type = 0x84,
  514. .ver = 0x01,
  515. .len = 0x0a,
  516. .CC02_CT47 = substream->runtime->channels - 1,
  517. };
  518. hdmi_setup_channel_allocation(codec, nid, &ai);
  519. for (i = 0; i < spec->num_pins; i++) {
  520. if (spec->pin_cvt[i] != nid)
  521. continue;
  522. if (!spec->sink_eld[i].monitor_present)
  523. continue;
  524. pin_nid = spec->pin[i];
  525. if (!hdmi_infoframe_uptodate(codec, pin_nid, &ai)) {
  526. snd_printdd("hdmi_setup_audio_infoframe: "
  527. "cvt=%d pin=%d channels=%d\n",
  528. nid, pin_nid,
  529. substream->runtime->channels);
  530. hdmi_setup_channel_mapping(codec, pin_nid, &ai);
  531. hdmi_stop_infoframe_trans(codec, pin_nid);
  532. hdmi_fill_audio_infoframe(codec, pin_nid, &ai);
  533. hdmi_start_infoframe_trans(codec, pin_nid);
  534. }
  535. }
  536. }
  537. /*
  538. * Unsolicited events
  539. */
  540. static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
  541. {
  542. struct hdmi_spec *spec = codec->spec;
  543. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  544. int pind = !!(res & AC_UNSOL_RES_PD);
  545. int eldv = !!(res & AC_UNSOL_RES_ELDV);
  546. int index;
  547. printk(KERN_INFO
  548. "HDMI hot plug event: Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  549. tag, pind, eldv);
  550. index = hda_node_index(spec->pin, tag);
  551. if (index < 0)
  552. return;
  553. spec->sink_eld[index].monitor_present = pind;
  554. spec->sink_eld[index].eld_valid = eldv;
  555. if (pind && eldv) {
  556. hdmi_get_show_eld(codec, spec->pin[index],
  557. &spec->sink_eld[index]);
  558. /* TODO: do real things about ELD */
  559. }
  560. }
  561. static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
  562. {
  563. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  564. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  565. int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
  566. int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
  567. printk(KERN_INFO
  568. "HDMI CP event: PIN=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
  569. tag,
  570. subtag,
  571. cp_state,
  572. cp_ready);
  573. /* TODO */
  574. if (cp_state)
  575. ;
  576. if (cp_ready)
  577. ;
  578. }
  579. static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
  580. {
  581. struct hdmi_spec *spec = codec->spec;
  582. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  583. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  584. if (hda_node_index(spec->pin, tag) < 0) {
  585. snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
  586. return;
  587. }
  588. if (subtag == 0)
  589. hdmi_intrinsic_event(codec, res);
  590. else
  591. hdmi_non_intrinsic_event(codec, res);
  592. }
  593. /*
  594. * Callbacks
  595. */
  596. static void hdmi_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  597. u32 stream_tag, int format)
  598. {
  599. int tag;
  600. int fmt;
  601. tag = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0) >> 4;
  602. fmt = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_STREAM_FORMAT, 0);
  603. snd_printdd("hdmi_setup_stream: "
  604. "NID=0x%x, %sstream=0x%x, %sformat=0x%x\n",
  605. nid,
  606. tag == stream_tag ? "" : "new-",
  607. stream_tag,
  608. fmt == format ? "" : "new-",
  609. format);
  610. if (tag != stream_tag)
  611. snd_hda_codec_write(codec, nid, 0,
  612. AC_VERB_SET_CHANNEL_STREAMID,
  613. stream_tag << 4);
  614. if (fmt != format)
  615. snd_hda_codec_write(codec, nid, 0,
  616. AC_VERB_SET_STREAM_FORMAT, format);
  617. }
  618. /*
  619. * HDA/HDMI auto parsing
  620. */
  621. static int hdmi_read_pin_conn(struct hda_codec *codec, hda_nid_t pin_nid)
  622. {
  623. struct hdmi_spec *spec = codec->spec;
  624. hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
  625. int conn_len, curr;
  626. int index;
  627. if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
  628. snd_printk(KERN_WARNING
  629. "HDMI: pin %d wcaps %#x "
  630. "does not support connection list\n",
  631. pin_nid, get_wcaps(codec, pin_nid));
  632. return -EINVAL;
  633. }
  634. conn_len = snd_hda_get_connections(codec, pin_nid, conn_list,
  635. HDA_MAX_CONNECTIONS);
  636. if (conn_len > 1)
  637. curr = snd_hda_codec_read(codec, pin_nid, 0,
  638. AC_VERB_GET_CONNECT_SEL, 0);
  639. else
  640. curr = 0;
  641. index = hda_node_index(spec->pin, pin_nid);
  642. if (index < 0)
  643. return -EINVAL;
  644. spec->pin_cvt[index] = conn_list[curr];
  645. return 0;
  646. }
  647. static void hdmi_present_sense(struct hda_codec *codec, hda_nid_t pin_nid,
  648. struct hdmi_eld *eld)
  649. {
  650. int present = snd_hda_pin_sense(codec, pin_nid);
  651. eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
  652. eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
  653. if (present & AC_PINSENSE_ELDV)
  654. hdmi_get_show_eld(codec, pin_nid, eld);
  655. }
  656. static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  657. {
  658. struct hdmi_spec *spec = codec->spec;
  659. if (spec->num_pins >= MAX_HDMI_PINS) {
  660. snd_printk(KERN_WARNING
  661. "HDMI: no space for pin %d\n", pin_nid);
  662. return -E2BIG;
  663. }
  664. hdmi_present_sense(codec, pin_nid, &spec->sink_eld[spec->num_pins]);
  665. spec->pin[spec->num_pins] = pin_nid;
  666. spec->num_pins++;
  667. /*
  668. * It is assumed that converter nodes come first in the node list and
  669. * hence have been registered and usable now.
  670. */
  671. return hdmi_read_pin_conn(codec, pin_nid);
  672. }
  673. static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t nid)
  674. {
  675. struct hdmi_spec *spec = codec->spec;
  676. if (spec->num_cvts >= MAX_HDMI_CVTS) {
  677. snd_printk(KERN_WARNING
  678. "HDMI: no space for converter %d\n", nid);
  679. return -E2BIG;
  680. }
  681. spec->cvt[spec->num_cvts] = nid;
  682. spec->num_cvts++;
  683. return 0;
  684. }
  685. static int hdmi_parse_codec(struct hda_codec *codec)
  686. {
  687. hda_nid_t nid;
  688. int i, nodes;
  689. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
  690. if (!nid || nodes < 0) {
  691. snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
  692. return -EINVAL;
  693. }
  694. for (i = 0; i < nodes; i++, nid++) {
  695. unsigned int caps;
  696. unsigned int type;
  697. caps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
  698. type = get_wcaps_type(caps);
  699. if (!(caps & AC_WCAP_DIGITAL))
  700. continue;
  701. switch (type) {
  702. case AC_WID_AUD_OUT:
  703. hdmi_add_cvt(codec, nid);
  704. break;
  705. case AC_WID_PIN:
  706. caps = snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
  707. if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
  708. continue;
  709. hdmi_add_pin(codec, nid);
  710. break;
  711. }
  712. }
  713. /*
  714. * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
  715. * can be lost and presence sense verb will become inaccurate if the
  716. * HDA link is powered off at hot plug or hw initialization time.
  717. */
  718. #ifdef CONFIG_SND_HDA_POWER_SAVE
  719. if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
  720. AC_PWRST_EPSS))
  721. codec->bus->power_keep_link_on = 1;
  722. #endif
  723. return 0;
  724. }