patch_intelhdmi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. /*
  35. * The HDMI/DisplayPort configuration can be highly dynamic. A graphics device
  36. * could support two independent pipes, each of them can be connected to one or
  37. * more ports (DVI, HDMI or DisplayPort).
  38. *
  39. * The HDA correspondence of pipes/ports are converter/pin nodes.
  40. */
  41. #define INTEL_HDMI_CVTS 2
  42. #define INTEL_HDMI_PINS 3
  43. static char *intel_hdmi_pcm_names[INTEL_HDMI_CVTS] = {
  44. "INTEL HDMI 0",
  45. "INTEL HDMI 1",
  46. };
  47. struct intel_hdmi_spec {
  48. int num_cvts;
  49. int num_pins;
  50. hda_nid_t cvt[INTEL_HDMI_CVTS+1]; /* audio sources */
  51. hda_nid_t pin[INTEL_HDMI_PINS+1]; /* audio sinks */
  52. /*
  53. * source connection for each pin
  54. */
  55. hda_nid_t pin_cvt[INTEL_HDMI_PINS+1];
  56. /*
  57. * HDMI sink attached to each pin
  58. */
  59. struct hdmi_eld sink_eld[INTEL_HDMI_PINS];
  60. /*
  61. * export one pcm per pipe
  62. */
  63. struct hda_pcm pcm_rec[INTEL_HDMI_CVTS];
  64. };
  65. struct hdmi_audio_infoframe {
  66. u8 type; /* 0x84 */
  67. u8 ver; /* 0x01 */
  68. u8 len; /* 0x0a */
  69. u8 checksum; /* PB0 */
  70. u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
  71. u8 SS01_SF24;
  72. u8 CXT04;
  73. u8 CA;
  74. u8 LFEPBL01_LSV36_DM_INH7;
  75. u8 reserved[5]; /* PB6 - PB10 */
  76. };
  77. /*
  78. * CEA speaker placement:
  79. *
  80. * FLH FCH FRH
  81. * FLW FL FLC FC FRC FR FRW
  82. *
  83. * LFE
  84. * TC
  85. *
  86. * RL RLC RC RRC RR
  87. *
  88. * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
  89. * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
  90. */
  91. enum cea_speaker_placement {
  92. FL = (1 << 0), /* Front Left */
  93. FC = (1 << 1), /* Front Center */
  94. FR = (1 << 2), /* Front Right */
  95. FLC = (1 << 3), /* Front Left Center */
  96. FRC = (1 << 4), /* Front Right Center */
  97. RL = (1 << 5), /* Rear Left */
  98. RC = (1 << 6), /* Rear Center */
  99. RR = (1 << 7), /* Rear Right */
  100. RLC = (1 << 8), /* Rear Left Center */
  101. RRC = (1 << 9), /* Rear Right Center */
  102. LFE = (1 << 10), /* Low Frequency Effect */
  103. FLW = (1 << 11), /* Front Left Wide */
  104. FRW = (1 << 12), /* Front Right Wide */
  105. FLH = (1 << 13), /* Front Left High */
  106. FCH = (1 << 14), /* Front Center High */
  107. FRH = (1 << 15), /* Front Right High */
  108. TC = (1 << 16), /* Top Center */
  109. };
  110. /*
  111. * ELD SA bits in the CEA Speaker Allocation data block
  112. */
  113. static int eld_speaker_allocation_bits[] = {
  114. [0] = FL | FR,
  115. [1] = LFE,
  116. [2] = FC,
  117. [3] = RL | RR,
  118. [4] = RC,
  119. [5] = FLC | FRC,
  120. [6] = RLC | RRC,
  121. /* the following are not defined in ELD yet */
  122. [7] = FLW | FRW,
  123. [8] = FLH | FRH,
  124. [9] = TC,
  125. [10] = FCH,
  126. };
  127. struct cea_channel_speaker_allocation {
  128. int ca_index;
  129. int speakers[8];
  130. /* derived values, just for convenience */
  131. int channels;
  132. int spk_mask;
  133. };
  134. /*
  135. * This is an ordered list!
  136. *
  137. * The preceding ones have better chances to be selected by
  138. * hdmi_setup_channel_allocation().
  139. */
  140. static struct cea_channel_speaker_allocation channel_allocations[] = {
  141. /* channel: 8 7 6 5 4 3 2 1 */
  142. { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
  143. /* 2.1 */
  144. { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
  145. /* Dolby Surround */
  146. { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
  147. { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
  148. { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
  149. { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
  150. { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
  151. { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
  152. { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
  153. { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
  154. { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
  155. /* 5.1 */
  156. { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
  157. { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
  158. { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
  159. { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
  160. /* 6.1 */
  161. { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
  162. { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
  163. { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
  164. { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
  165. /* 7.1 */
  166. { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
  167. { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
  168. { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
  169. { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
  170. { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
  171. { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
  172. { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
  173. { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
  174. { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
  175. { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
  176. { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
  177. { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
  178. { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
  179. { .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
  180. { .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
  181. { .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
  182. { .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
  183. { .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
  184. { .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
  185. { .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
  186. { .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
  187. { .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
  188. { .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
  189. { .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
  190. { .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
  191. { .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
  192. { .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
  193. { .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
  194. { .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
  195. { .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
  196. { .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
  197. };
  198. /*
  199. * HDA/HDMI auto parsing
  200. */
  201. static int hda_node_index(hda_nid_t *nids, hda_nid_t nid)
  202. {
  203. int i;
  204. for (i = 0; nids[i]; i++)
  205. if (nids[i] == nid)
  206. return i;
  207. snd_printk(KERN_WARNING "HDMI: nid %d not registered\n", nid);
  208. return -EINVAL;
  209. }
  210. static int intel_hdmi_read_pin_conn(struct hda_codec *codec, hda_nid_t pin_nid)
  211. {
  212. struct intel_hdmi_spec *spec = codec->spec;
  213. hda_nid_t conn_list[HDA_MAX_CONNECTIONS];
  214. int conn_len, curr;
  215. int index;
  216. if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
  217. snd_printk(KERN_WARNING
  218. "HDMI: pin %d wcaps %#x "
  219. "does not support connection list\n",
  220. pin_nid, get_wcaps(codec, pin_nid));
  221. return -EINVAL;
  222. }
  223. conn_len = snd_hda_get_connections(codec, pin_nid, conn_list,
  224. HDA_MAX_CONNECTIONS);
  225. if (conn_len > 1)
  226. curr = snd_hda_codec_read(codec, pin_nid, 0,
  227. AC_VERB_GET_CONNECT_SEL, 0);
  228. else
  229. curr = 0;
  230. index = hda_node_index(spec->pin, pin_nid);
  231. if (index < 0)
  232. return -EINVAL;
  233. spec->pin_cvt[index] = conn_list[curr];
  234. return 0;
  235. }
  236. static void hdmi_get_show_eld(struct hda_codec *codec, hda_nid_t pin_nid,
  237. struct hdmi_eld *eld)
  238. {
  239. if (!snd_hdmi_get_eld(eld, codec, pin_nid))
  240. snd_hdmi_show_eld(eld);
  241. }
  242. static void hdmi_present_sense(struct hda_codec *codec, hda_nid_t pin_nid,
  243. struct hdmi_eld *eld)
  244. {
  245. int present = snd_hda_pin_sense(codec, pin_nid);
  246. eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
  247. eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
  248. if (present & AC_PINSENSE_ELDV)
  249. hdmi_get_show_eld(codec, pin_nid, eld);
  250. }
  251. static int intel_hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  252. {
  253. struct intel_hdmi_spec *spec = codec->spec;
  254. if (spec->num_pins >= INTEL_HDMI_PINS) {
  255. snd_printk(KERN_WARNING
  256. "HDMI: no space for pin %d \n", pin_nid);
  257. return -EINVAL;
  258. }
  259. hdmi_present_sense(codec, pin_nid, &spec->sink_eld[spec->num_pins]);
  260. spec->pin[spec->num_pins] = pin_nid;
  261. spec->num_pins++;
  262. /*
  263. * It is assumed that converter nodes come first in the node list and
  264. * hence have been registered and usable now.
  265. */
  266. return intel_hdmi_read_pin_conn(codec, pin_nid);
  267. }
  268. static int intel_hdmi_add_cvt(struct hda_codec *codec, hda_nid_t nid)
  269. {
  270. struct intel_hdmi_spec *spec = codec->spec;
  271. if (spec->num_cvts >= INTEL_HDMI_CVTS) {
  272. snd_printk(KERN_WARNING
  273. "HDMI: no space for converter %d \n", nid);
  274. return -EINVAL;
  275. }
  276. spec->cvt[spec->num_cvts] = nid;
  277. spec->num_cvts++;
  278. return 0;
  279. }
  280. static int intel_hdmi_parse_codec(struct hda_codec *codec)
  281. {
  282. hda_nid_t nid;
  283. int i, nodes;
  284. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
  285. if (!nid || nodes < 0) {
  286. snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
  287. return -EINVAL;
  288. }
  289. for (i = 0; i < nodes; i++, nid++) {
  290. unsigned int caps;
  291. unsigned int type;
  292. caps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
  293. type = get_wcaps_type(caps);
  294. if (!(caps & AC_WCAP_DIGITAL))
  295. continue;
  296. switch (type) {
  297. case AC_WID_AUD_OUT:
  298. if (intel_hdmi_add_cvt(codec, nid) < 0)
  299. return -EINVAL;
  300. break;
  301. case AC_WID_PIN:
  302. caps = snd_hda_param_read(codec, nid, AC_PAR_PIN_CAP);
  303. if (!(caps & AC_PINCAP_HDMI))
  304. continue;
  305. if (intel_hdmi_add_pin(codec, nid) < 0)
  306. return -EINVAL;
  307. break;
  308. }
  309. }
  310. return 0;
  311. }
  312. /*
  313. * HDMI routines
  314. */
  315. #ifdef BE_PARANOID
  316. static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
  317. int *packet_index, int *byte_index)
  318. {
  319. int val;
  320. val = snd_hda_codec_read(codec, pin_nid, 0,
  321. AC_VERB_GET_HDMI_DIP_INDEX, 0);
  322. *packet_index = val >> 5;
  323. *byte_index = val & 0x1f;
  324. }
  325. #endif
  326. static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
  327. int packet_index, int byte_index)
  328. {
  329. int val;
  330. val = (packet_index << 5) | (byte_index & 0x1f);
  331. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
  332. }
  333. static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
  334. unsigned char val)
  335. {
  336. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
  337. }
  338. static void hdmi_enable_output(struct hda_codec *codec, hda_nid_t pin_nid)
  339. {
  340. /* Unmute */
  341. if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
  342. snd_hda_codec_write(codec, pin_nid, 0,
  343. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
  344. /* Enable pin out */
  345. snd_hda_codec_write(codec, pin_nid, 0,
  346. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  347. }
  348. /*
  349. * Enable Audio InfoFrame Transmission
  350. */
  351. static void hdmi_start_infoframe_trans(struct hda_codec *codec,
  352. hda_nid_t pin_nid)
  353. {
  354. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  355. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  356. AC_DIPXMIT_BEST);
  357. }
  358. /*
  359. * Disable Audio InfoFrame Transmission
  360. */
  361. static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
  362. hda_nid_t pin_nid)
  363. {
  364. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  365. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  366. AC_DIPXMIT_DISABLE);
  367. }
  368. #ifdef CONFIG_SND_DEBUG_VERBOSE
  369. static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t nid)
  370. {
  371. return 1 + snd_hda_codec_read(codec, nid, 0,
  372. AC_VERB_GET_CVT_CHAN_COUNT, 0);
  373. }
  374. #endif
  375. static void hdmi_set_channel_count(struct hda_codec *codec,
  376. hda_nid_t nid, int chs)
  377. {
  378. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
  379. #ifdef CONFIG_SND_DEBUG_VERBOSE
  380. if (chs != hdmi_get_channel_count(codec, nid))
  381. snd_printd(KERN_INFO "HDMI channel count: expect %d, get %d\n",
  382. chs, hdmi_get_channel_count(codec, nid));
  383. #endif
  384. }
  385. static void hdmi_debug_channel_mapping(struct hda_codec *codec, hda_nid_t nid)
  386. {
  387. #ifdef CONFIG_SND_DEBUG_VERBOSE
  388. int i;
  389. int slot;
  390. for (i = 0; i < 8; i++) {
  391. slot = snd_hda_codec_read(codec, nid, 0,
  392. AC_VERB_GET_HDMI_CHAN_SLOT, i);
  393. printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
  394. slot >> 4, slot & 0xf);
  395. }
  396. #endif
  397. }
  398. /*
  399. * Audio InfoFrame routines
  400. */
  401. static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
  402. {
  403. #ifdef CONFIG_SND_DEBUG_VERBOSE
  404. int i;
  405. int size;
  406. size = snd_hdmi_get_eld_size(codec, pin_nid);
  407. printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
  408. for (i = 0; i < 8; i++) {
  409. size = snd_hda_codec_read(codec, pin_nid, 0,
  410. AC_VERB_GET_HDMI_DIP_SIZE, i);
  411. printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
  412. }
  413. #endif
  414. }
  415. static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
  416. {
  417. #ifdef BE_PARANOID
  418. int i, j;
  419. int size;
  420. int pi, bi;
  421. for (i = 0; i < 8; i++) {
  422. size = snd_hda_codec_read(codec, pin_nid, 0,
  423. AC_VERB_GET_HDMI_DIP_SIZE, i);
  424. if (size == 0)
  425. continue;
  426. hdmi_set_dip_index(codec, pin_nid, i, 0x0);
  427. for (j = 1; j < 1000; j++) {
  428. hdmi_write_dip_byte(codec, pin_nid, 0x0);
  429. hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
  430. if (pi != i)
  431. snd_printd(KERN_INFO "dip index %d: %d != %d\n",
  432. bi, pi, i);
  433. if (bi == 0) /* byte index wrapped around */
  434. break;
  435. }
  436. snd_printd(KERN_INFO
  437. "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
  438. i, size, j);
  439. }
  440. #endif
  441. }
  442. static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
  443. hda_nid_t pin_nid,
  444. struct hdmi_audio_infoframe *ai)
  445. {
  446. u8 *params = (u8 *)ai;
  447. u8 sum = 0;
  448. int i;
  449. hdmi_debug_dip_size(codec, pin_nid);
  450. hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
  451. for (i = 0; i < sizeof(*ai); i++)
  452. sum += params[i];
  453. ai->checksum = - sum;
  454. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  455. for (i = 0; i < sizeof(*ai); i++)
  456. hdmi_write_dip_byte(codec, pin_nid, params[i]);
  457. }
  458. /*
  459. * Compute derived values in channel_allocations[].
  460. */
  461. static void init_channel_allocations(void)
  462. {
  463. int i, j;
  464. struct cea_channel_speaker_allocation *p;
  465. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  466. p = channel_allocations + i;
  467. p->channels = 0;
  468. p->spk_mask = 0;
  469. for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
  470. if (p->speakers[j]) {
  471. p->channels++;
  472. p->spk_mask |= p->speakers[j];
  473. }
  474. }
  475. }
  476. /*
  477. * The transformation takes two steps:
  478. *
  479. * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
  480. * spk_mask => (channel_allocations[]) => ai->CA
  481. *
  482. * TODO: it could select the wrong CA from multiple candidates.
  483. */
  484. static int hdmi_setup_channel_allocation(struct hda_codec *codec, hda_nid_t nid,
  485. struct hdmi_audio_infoframe *ai)
  486. {
  487. struct intel_hdmi_spec *spec = codec->spec;
  488. struct hdmi_eld *eld;
  489. int i;
  490. int spk_mask = 0;
  491. int channels = 1 + (ai->CC02_CT47 & 0x7);
  492. char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
  493. /*
  494. * CA defaults to 0 for basic stereo audio
  495. */
  496. if (channels <= 2)
  497. return 0;
  498. i = hda_node_index(spec->pin_cvt, nid);
  499. if (i < 0)
  500. return 0;
  501. eld = &spec->sink_eld[i];
  502. /*
  503. * HDMI sink's ELD info cannot always be retrieved for now, e.g.
  504. * in console or for audio devices. Assume the highest speakers
  505. * configuration, to _not_ prohibit multi-channel audio playback.
  506. */
  507. if (!eld->spk_alloc)
  508. eld->spk_alloc = 0xffff;
  509. /*
  510. * expand ELD's speaker allocation mask
  511. *
  512. * ELD tells the speaker mask in a compact(paired) form,
  513. * expand ELD's notions to match the ones used by Audio InfoFrame.
  514. */
  515. for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
  516. if (eld->spk_alloc & (1 << i))
  517. spk_mask |= eld_speaker_allocation_bits[i];
  518. }
  519. /* search for the first working match in the CA table */
  520. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  521. if (channels == channel_allocations[i].channels &&
  522. (spk_mask & channel_allocations[i].spk_mask) ==
  523. channel_allocations[i].spk_mask) {
  524. ai->CA = channel_allocations[i].ca_index;
  525. break;
  526. }
  527. }
  528. snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
  529. snd_printdd(KERN_INFO
  530. "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
  531. ai->CA, channels, buf);
  532. return ai->CA;
  533. }
  534. static void hdmi_setup_channel_mapping(struct hda_codec *codec, hda_nid_t nid,
  535. struct hdmi_audio_infoframe *ai)
  536. {
  537. int i;
  538. if (!ai->CA)
  539. return;
  540. /*
  541. * TODO: adjust channel mapping if necessary
  542. * ALSA sequence is front/surr/clfe/side?
  543. */
  544. for (i = 0; i < 8; i++)
  545. snd_hda_codec_write(codec, nid, 0,
  546. AC_VERB_SET_HDMI_CHAN_SLOT,
  547. (i << 4) | i);
  548. hdmi_debug_channel_mapping(codec, nid);
  549. }
  550. static void hdmi_setup_audio_infoframe(struct hda_codec *codec, hda_nid_t nid,
  551. struct snd_pcm_substream *substream)
  552. {
  553. struct intel_hdmi_spec *spec = codec->spec;
  554. hda_nid_t pin_nid;
  555. int i;
  556. struct hdmi_audio_infoframe ai = {
  557. .type = 0x84,
  558. .ver = 0x01,
  559. .len = 0x0a,
  560. .CC02_CT47 = substream->runtime->channels - 1,
  561. };
  562. hdmi_setup_channel_allocation(codec, nid, &ai);
  563. hdmi_setup_channel_mapping(codec, nid, &ai);
  564. for (i = 0; i < spec->num_pins; i++) {
  565. if (spec->pin_cvt[i] != nid)
  566. continue;
  567. if (!spec->sink_eld[i].monitor_present)
  568. continue;
  569. pin_nid = spec->pin[i];
  570. hdmi_fill_audio_infoframe(codec, pin_nid, &ai);
  571. hdmi_start_infoframe_trans(codec, pin_nid);
  572. }
  573. }
  574. /*
  575. * Unsolicited events
  576. */
  577. static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
  578. {
  579. struct intel_hdmi_spec *spec = codec->spec;
  580. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  581. int pind = !!(res & AC_UNSOL_RES_PD);
  582. int eldv = !!(res & AC_UNSOL_RES_ELDV);
  583. int index;
  584. printk(KERN_INFO
  585. "HDMI hot plug event: Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  586. tag, pind, eldv);
  587. index = hda_node_index(spec->pin, tag);
  588. if (index < 0)
  589. return;
  590. spec->sink_eld[index].monitor_present = pind;
  591. spec->sink_eld[index].eld_valid = eldv;
  592. if (pind && eldv) {
  593. hdmi_get_show_eld(codec, spec->pin[index], &spec->sink_eld[index]);
  594. /* TODO: do real things about ELD */
  595. }
  596. }
  597. static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
  598. {
  599. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  600. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  601. int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
  602. int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
  603. printk(KERN_INFO
  604. "HDMI CP event: PIN=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
  605. tag,
  606. subtag,
  607. cp_state,
  608. cp_ready);
  609. /* TODO */
  610. if (cp_state)
  611. ;
  612. if (cp_ready)
  613. ;
  614. }
  615. static void intel_hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
  616. {
  617. struct intel_hdmi_spec *spec = codec->spec;
  618. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  619. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  620. if (hda_node_index(spec->pin, tag) < 0) {
  621. snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
  622. return;
  623. }
  624. if (subtag == 0)
  625. hdmi_intrinsic_event(codec, res);
  626. else
  627. hdmi_non_intrinsic_event(codec, res);
  628. }
  629. /*
  630. * Callbacks
  631. */
  632. static int intel_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  633. struct hda_codec *codec,
  634. unsigned int stream_tag,
  635. unsigned int format,
  636. struct snd_pcm_substream *substream)
  637. {
  638. hdmi_set_channel_count(codec, hinfo->nid,
  639. substream->runtime->channels);
  640. hdmi_setup_audio_infoframe(codec, hinfo->nid, substream);
  641. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  642. return 0;
  643. }
  644. static int intel_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  645. struct hda_codec *codec,
  646. struct snd_pcm_substream *substream)
  647. {
  648. struct intel_hdmi_spec *spec = codec->spec;
  649. int i;
  650. for (i = 0; i < spec->num_pins; i++) {
  651. if (spec->pin_cvt[i] != hinfo->nid)
  652. continue;
  653. hdmi_stop_infoframe_trans(codec, spec->pin[i]);
  654. }
  655. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  656. return 0;
  657. }
  658. static struct hda_pcm_stream intel_hdmi_pcm_playback = {
  659. .substreams = 1,
  660. .channels_min = 2,
  661. .ops = {
  662. .prepare = intel_hdmi_playback_pcm_prepare,
  663. .cleanup = intel_hdmi_playback_pcm_cleanup,
  664. },
  665. };
  666. static int intel_hdmi_build_pcms(struct hda_codec *codec)
  667. {
  668. struct intel_hdmi_spec *spec = codec->spec;
  669. struct hda_pcm *info = spec->pcm_rec;
  670. int i;
  671. codec->num_pcms = spec->num_cvts;
  672. codec->pcm_info = info;
  673. for (i = 0; i < codec->num_pcms; i++, info++) {
  674. unsigned int chans;
  675. chans = get_wcaps(codec, spec->cvt[i]);
  676. chans = get_wcaps_channels(chans);
  677. info->name = intel_hdmi_pcm_names[i];
  678. info->pcm_type = HDA_PCM_TYPE_HDMI;
  679. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  680. intel_hdmi_pcm_playback;
  681. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->cvt[i];
  682. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = chans;
  683. }
  684. return 0;
  685. }
  686. static int intel_hdmi_build_controls(struct hda_codec *codec)
  687. {
  688. struct intel_hdmi_spec *spec = codec->spec;
  689. int err;
  690. int i;
  691. for (i = 0; i < codec->num_pcms; i++) {
  692. err = snd_hda_create_spdif_out_ctls(codec, spec->cvt[i]);
  693. if (err < 0)
  694. return err;
  695. }
  696. return 0;
  697. }
  698. static int intel_hdmi_init(struct hda_codec *codec)
  699. {
  700. struct intel_hdmi_spec *spec = codec->spec;
  701. int i;
  702. for (i = 0; spec->pin[i]; i++) {
  703. hdmi_enable_output(codec, spec->pin[i]);
  704. snd_hda_codec_write(codec, spec->pin[i], 0,
  705. AC_VERB_SET_UNSOLICITED_ENABLE,
  706. AC_USRSP_EN | spec->pin[i]);
  707. }
  708. return 0;
  709. }
  710. static void intel_hdmi_free(struct hda_codec *codec)
  711. {
  712. struct intel_hdmi_spec *spec = codec->spec;
  713. int i;
  714. for (i = 0; i < spec->num_pins; i++)
  715. snd_hda_eld_proc_free(codec, &spec->sink_eld[i]);
  716. kfree(spec);
  717. }
  718. static struct hda_codec_ops intel_hdmi_patch_ops = {
  719. .init = intel_hdmi_init,
  720. .free = intel_hdmi_free,
  721. .build_pcms = intel_hdmi_build_pcms,
  722. .build_controls = intel_hdmi_build_controls,
  723. .unsol_event = intel_hdmi_unsol_event,
  724. };
  725. static int patch_intel_hdmi(struct hda_codec *codec)
  726. {
  727. struct intel_hdmi_spec *spec;
  728. int i;
  729. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  730. if (spec == NULL)
  731. return -ENOMEM;
  732. codec->spec = spec;
  733. if (intel_hdmi_parse_codec(codec) < 0) {
  734. codec->spec = NULL;
  735. kfree(spec);
  736. return -EINVAL;
  737. }
  738. codec->patch_ops = intel_hdmi_patch_ops;
  739. for (i = 0; i < spec->num_pins; i++)
  740. snd_hda_eld_proc_new(codec, &spec->sink_eld[i], i);
  741. init_channel_allocations();
  742. return 0;
  743. }
  744. static struct hda_codec_preset snd_hda_preset_intelhdmi[] = {
  745. { .id = 0x808629fb, .name = "G45 DEVCL", .patch = patch_intel_hdmi },
  746. { .id = 0x80862801, .name = "G45 DEVBLC", .patch = patch_intel_hdmi },
  747. { .id = 0x80862802, .name = "G45 DEVCTG", .patch = patch_intel_hdmi },
  748. { .id = 0x80862803, .name = "G45 DEVELK", .patch = patch_intel_hdmi },
  749. { .id = 0x80862804, .name = "G45 DEVIBX", .patch = patch_intel_hdmi },
  750. { .id = 0x80860054, .name = "Q57 DEVIBX", .patch = patch_intel_hdmi },
  751. { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_intel_hdmi },
  752. {} /* terminator */
  753. };
  754. MODULE_ALIAS("snd-hda-codec-id:808629fb");
  755. MODULE_ALIAS("snd-hda-codec-id:80862801");
  756. MODULE_ALIAS("snd-hda-codec-id:80862802");
  757. MODULE_ALIAS("snd-hda-codec-id:80862803");
  758. MODULE_ALIAS("snd-hda-codec-id:80862804");
  759. MODULE_ALIAS("snd-hda-codec-id:80860054");
  760. MODULE_ALIAS("snd-hda-codec-id:10951392");
  761. MODULE_LICENSE("GPL");
  762. MODULE_DESCRIPTION("Intel HDMI HD-audio codec");
  763. static struct hda_codec_preset_list intel_list = {
  764. .preset = snd_hda_preset_intelhdmi,
  765. .owner = THIS_MODULE,
  766. };
  767. static int __init patch_intelhdmi_init(void)
  768. {
  769. return snd_hda_add_codec_preset(&intel_list);
  770. }
  771. static void __exit patch_intelhdmi_exit(void)
  772. {
  773. snd_hda_delete_codec_preset(&intel_list);
  774. }
  775. module_init(patch_intelhdmi_init)
  776. module_exit(patch_intelhdmi_exit)