patch_intelhdmi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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. static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t nid)
  369. {
  370. return 1 + snd_hda_codec_read(codec, nid, 0,
  371. AC_VERB_GET_CVT_CHAN_COUNT, 0);
  372. }
  373. static void hdmi_set_channel_count(struct hda_codec *codec,
  374. hda_nid_t nid, int chs)
  375. {
  376. if (chs != hdmi_get_channel_count(codec, nid))
  377. snd_hda_codec_write(codec, nid, 0,
  378. AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
  379. }
  380. static void hdmi_debug_channel_mapping(struct hda_codec *codec, hda_nid_t nid)
  381. {
  382. #ifdef CONFIG_SND_DEBUG_VERBOSE
  383. int i;
  384. int slot;
  385. for (i = 0; i < 8; i++) {
  386. slot = snd_hda_codec_read(codec, nid, 0,
  387. AC_VERB_GET_HDMI_CHAN_SLOT, i);
  388. printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
  389. slot >> 4, slot & 0xf);
  390. }
  391. #endif
  392. }
  393. /*
  394. * Audio InfoFrame routines
  395. */
  396. static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
  397. {
  398. #ifdef CONFIG_SND_DEBUG_VERBOSE
  399. int i;
  400. int size;
  401. size = snd_hdmi_get_eld_size(codec, pin_nid);
  402. printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
  403. for (i = 0; i < 8; i++) {
  404. size = snd_hda_codec_read(codec, pin_nid, 0,
  405. AC_VERB_GET_HDMI_DIP_SIZE, i);
  406. printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
  407. }
  408. #endif
  409. }
  410. static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
  411. {
  412. #ifdef BE_PARANOID
  413. int i, j;
  414. int size;
  415. int pi, bi;
  416. for (i = 0; i < 8; i++) {
  417. size = snd_hda_codec_read(codec, pin_nid, 0,
  418. AC_VERB_GET_HDMI_DIP_SIZE, i);
  419. if (size == 0)
  420. continue;
  421. hdmi_set_dip_index(codec, pin_nid, i, 0x0);
  422. for (j = 1; j < 1000; j++) {
  423. hdmi_write_dip_byte(codec, pin_nid, 0x0);
  424. hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
  425. if (pi != i)
  426. snd_printd(KERN_INFO "dip index %d: %d != %d\n",
  427. bi, pi, i);
  428. if (bi == 0) /* byte index wrapped around */
  429. break;
  430. }
  431. snd_printd(KERN_INFO
  432. "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
  433. i, size, j);
  434. }
  435. #endif
  436. }
  437. static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *ai)
  438. {
  439. u8 *bytes = (u8 *)ai;
  440. u8 sum = 0;
  441. int i;
  442. ai->checksum = 0;
  443. for (i = 0; i < sizeof(*ai); i++)
  444. sum += bytes[i];
  445. ai->checksum = - sum;
  446. }
  447. static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
  448. hda_nid_t pin_nid,
  449. struct hdmi_audio_infoframe *ai)
  450. {
  451. u8 *bytes = (u8 *)ai;
  452. int i;
  453. hdmi_debug_dip_size(codec, pin_nid);
  454. hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
  455. hdmi_checksum_audio_infoframe(ai);
  456. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  457. for (i = 0; i < sizeof(*ai); i++)
  458. hdmi_write_dip_byte(codec, pin_nid, bytes[i]);
  459. }
  460. /*
  461. * Compute derived values in channel_allocations[].
  462. */
  463. static void init_channel_allocations(void)
  464. {
  465. int i, j;
  466. struct cea_channel_speaker_allocation *p;
  467. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  468. p = channel_allocations + i;
  469. p->channels = 0;
  470. p->spk_mask = 0;
  471. for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
  472. if (p->speakers[j]) {
  473. p->channels++;
  474. p->spk_mask |= p->speakers[j];
  475. }
  476. }
  477. }
  478. /*
  479. * The transformation takes two steps:
  480. *
  481. * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
  482. * spk_mask => (channel_allocations[]) => ai->CA
  483. *
  484. * TODO: it could select the wrong CA from multiple candidates.
  485. */
  486. static int hdmi_setup_channel_allocation(struct hda_codec *codec, hda_nid_t nid,
  487. struct hdmi_audio_infoframe *ai)
  488. {
  489. struct intel_hdmi_spec *spec = codec->spec;
  490. struct hdmi_eld *eld;
  491. int i;
  492. int spk_mask = 0;
  493. int channels = 1 + (ai->CC02_CT47 & 0x7);
  494. char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
  495. /*
  496. * CA defaults to 0 for basic stereo audio
  497. */
  498. if (channels <= 2)
  499. return 0;
  500. i = hda_node_index(spec->pin_cvt, nid);
  501. if (i < 0)
  502. return 0;
  503. eld = &spec->sink_eld[i];
  504. /*
  505. * HDMI sink's ELD info cannot always be retrieved for now, e.g.
  506. * in console or for audio devices. Assume the highest speakers
  507. * configuration, to _not_ prohibit multi-channel audio playback.
  508. */
  509. if (!eld->spk_alloc)
  510. eld->spk_alloc = 0xffff;
  511. /*
  512. * expand ELD's speaker allocation mask
  513. *
  514. * ELD tells the speaker mask in a compact(paired) form,
  515. * expand ELD's notions to match the ones used by Audio InfoFrame.
  516. */
  517. for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
  518. if (eld->spk_alloc & (1 << i))
  519. spk_mask |= eld_speaker_allocation_bits[i];
  520. }
  521. /* search for the first working match in the CA table */
  522. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  523. if (channels == channel_allocations[i].channels &&
  524. (spk_mask & channel_allocations[i].spk_mask) ==
  525. channel_allocations[i].spk_mask) {
  526. ai->CA = channel_allocations[i].ca_index;
  527. break;
  528. }
  529. }
  530. snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
  531. snd_printdd(KERN_INFO
  532. "HDMI: select CA 0x%x for %d-channel allocation: %s\n",
  533. ai->CA, channels, buf);
  534. return ai->CA;
  535. }
  536. static void hdmi_setup_channel_mapping(struct hda_codec *codec, hda_nid_t nid,
  537. struct hdmi_audio_infoframe *ai)
  538. {
  539. int i;
  540. if (!ai->CA)
  541. return;
  542. /*
  543. * TODO: adjust channel mapping if necessary
  544. * ALSA sequence is front/surr/clfe/side?
  545. */
  546. for (i = 0; i < 8; i++)
  547. snd_hda_codec_write(codec, nid, 0,
  548. AC_VERB_SET_HDMI_CHAN_SLOT,
  549. (i << 4) | i);
  550. hdmi_debug_channel_mapping(codec, nid);
  551. }
  552. static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
  553. struct hdmi_audio_infoframe *ai)
  554. {
  555. u8 *bytes = (u8 *)ai;
  556. u8 val;
  557. int i;
  558. if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
  559. != AC_DIPXMIT_BEST)
  560. return false;
  561. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  562. for (i = 0; i < sizeof(*ai); i++) {
  563. val = snd_hda_codec_read(codec, pin_nid, 0,
  564. AC_VERB_GET_HDMI_DIP_DATA, 0);
  565. if (val != bytes[i])
  566. return false;
  567. }
  568. return true;
  569. }
  570. static void hdmi_setup_audio_infoframe(struct hda_codec *codec, hda_nid_t nid,
  571. struct snd_pcm_substream *substream)
  572. {
  573. struct intel_hdmi_spec *spec = codec->spec;
  574. hda_nid_t pin_nid;
  575. int i;
  576. struct hdmi_audio_infoframe ai = {
  577. .type = 0x84,
  578. .ver = 0x01,
  579. .len = 0x0a,
  580. .CC02_CT47 = substream->runtime->channels - 1,
  581. };
  582. hdmi_setup_channel_allocation(codec, nid, &ai);
  583. hdmi_setup_channel_mapping(codec, nid, &ai);
  584. for (i = 0; i < spec->num_pins; i++) {
  585. if (spec->pin_cvt[i] != nid)
  586. continue;
  587. if (!spec->sink_eld[i].monitor_present)
  588. continue;
  589. pin_nid = spec->pin[i];
  590. if (!hdmi_infoframe_uptodate(codec, pin_nid, &ai)) {
  591. hdmi_stop_infoframe_trans(codec, pin_nid);
  592. hdmi_fill_audio_infoframe(codec, pin_nid, &ai);
  593. hdmi_start_infoframe_trans(codec, pin_nid);
  594. }
  595. }
  596. }
  597. /*
  598. * Unsolicited events
  599. */
  600. static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
  601. {
  602. struct intel_hdmi_spec *spec = codec->spec;
  603. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  604. int pind = !!(res & AC_UNSOL_RES_PD);
  605. int eldv = !!(res & AC_UNSOL_RES_ELDV);
  606. int index;
  607. printk(KERN_INFO
  608. "HDMI hot plug event: Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  609. tag, pind, eldv);
  610. index = hda_node_index(spec->pin, tag);
  611. if (index < 0)
  612. return;
  613. spec->sink_eld[index].monitor_present = pind;
  614. spec->sink_eld[index].eld_valid = eldv;
  615. if (pind && eldv) {
  616. hdmi_get_show_eld(codec, spec->pin[index], &spec->sink_eld[index]);
  617. /* TODO: do real things about ELD */
  618. }
  619. }
  620. static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
  621. {
  622. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  623. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  624. int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
  625. int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
  626. printk(KERN_INFO
  627. "HDMI CP event: PIN=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
  628. tag,
  629. subtag,
  630. cp_state,
  631. cp_ready);
  632. /* TODO */
  633. if (cp_state)
  634. ;
  635. if (cp_ready)
  636. ;
  637. }
  638. static void intel_hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
  639. {
  640. struct intel_hdmi_spec *spec = codec->spec;
  641. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  642. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  643. if (hda_node_index(spec->pin, tag) < 0) {
  644. snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
  645. return;
  646. }
  647. if (subtag == 0)
  648. hdmi_intrinsic_event(codec, res);
  649. else
  650. hdmi_non_intrinsic_event(codec, res);
  651. }
  652. /*
  653. * Callbacks
  654. */
  655. static void hdmi_setup_stream(struct hda_codec *codec, hda_nid_t nid,
  656. u32 stream_tag, int format)
  657. {
  658. int tag;
  659. int fmt;
  660. tag = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_CONV, 0) >> 4;
  661. fmt = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_STREAM_FORMAT, 0);
  662. snd_printdd("hdmi_setup_stream: "
  663. "NID=0x%x, %sstream=0x%x, %sformat=0x%x\n",
  664. nid,
  665. tag == stream_tag ? "" : "new-",
  666. stream_tag,
  667. fmt == format ? "" : "new-",
  668. format);
  669. if (tag != stream_tag)
  670. snd_hda_codec_write(codec, nid, 0,
  671. AC_VERB_SET_CHANNEL_STREAMID, stream_tag << 4);
  672. if (fmt != format)
  673. snd_hda_codec_write(codec, nid, 0,
  674. AC_VERB_SET_STREAM_FORMAT, format);
  675. }
  676. static int intel_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  677. struct hda_codec *codec,
  678. unsigned int stream_tag,
  679. unsigned int format,
  680. struct snd_pcm_substream *substream)
  681. {
  682. hdmi_set_channel_count(codec, hinfo->nid,
  683. substream->runtime->channels);
  684. hdmi_setup_audio_infoframe(codec, hinfo->nid, substream);
  685. hdmi_setup_stream(codec, hinfo->nid, stream_tag, format);
  686. return 0;
  687. }
  688. static int intel_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  689. struct hda_codec *codec,
  690. struct snd_pcm_substream *substream)
  691. {
  692. return 0;
  693. }
  694. static struct hda_pcm_stream intel_hdmi_pcm_playback = {
  695. .substreams = 1,
  696. .channels_min = 2,
  697. .ops = {
  698. .prepare = intel_hdmi_playback_pcm_prepare,
  699. .cleanup = intel_hdmi_playback_pcm_cleanup,
  700. },
  701. };
  702. static int intel_hdmi_build_pcms(struct hda_codec *codec)
  703. {
  704. struct intel_hdmi_spec *spec = codec->spec;
  705. struct hda_pcm *info = spec->pcm_rec;
  706. int i;
  707. codec->num_pcms = spec->num_cvts;
  708. codec->pcm_info = info;
  709. for (i = 0; i < codec->num_pcms; i++, info++) {
  710. unsigned int chans;
  711. chans = get_wcaps(codec, spec->cvt[i]);
  712. chans = get_wcaps_channels(chans);
  713. info->name = intel_hdmi_pcm_names[i];
  714. info->pcm_type = HDA_PCM_TYPE_HDMI;
  715. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  716. intel_hdmi_pcm_playback;
  717. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->cvt[i];
  718. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max = chans;
  719. }
  720. return 0;
  721. }
  722. static int intel_hdmi_build_controls(struct hda_codec *codec)
  723. {
  724. struct intel_hdmi_spec *spec = codec->spec;
  725. int err;
  726. int i;
  727. for (i = 0; i < codec->num_pcms; i++) {
  728. err = snd_hda_create_spdif_out_ctls(codec, spec->cvt[i]);
  729. if (err < 0)
  730. return err;
  731. }
  732. return 0;
  733. }
  734. static int intel_hdmi_init(struct hda_codec *codec)
  735. {
  736. struct intel_hdmi_spec *spec = codec->spec;
  737. int i;
  738. for (i = 0; spec->pin[i]; i++) {
  739. hdmi_enable_output(codec, spec->pin[i]);
  740. snd_hda_codec_write(codec, spec->pin[i], 0,
  741. AC_VERB_SET_UNSOLICITED_ENABLE,
  742. AC_USRSP_EN | spec->pin[i]);
  743. }
  744. return 0;
  745. }
  746. static void intel_hdmi_free(struct hda_codec *codec)
  747. {
  748. struct intel_hdmi_spec *spec = codec->spec;
  749. int i;
  750. for (i = 0; i < spec->num_pins; i++)
  751. snd_hda_eld_proc_free(codec, &spec->sink_eld[i]);
  752. kfree(spec);
  753. }
  754. static struct hda_codec_ops intel_hdmi_patch_ops = {
  755. .init = intel_hdmi_init,
  756. .free = intel_hdmi_free,
  757. .build_pcms = intel_hdmi_build_pcms,
  758. .build_controls = intel_hdmi_build_controls,
  759. .unsol_event = intel_hdmi_unsol_event,
  760. };
  761. static int patch_intel_hdmi(struct hda_codec *codec)
  762. {
  763. struct intel_hdmi_spec *spec;
  764. int i;
  765. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  766. if (spec == NULL)
  767. return -ENOMEM;
  768. codec->spec = spec;
  769. if (intel_hdmi_parse_codec(codec) < 0) {
  770. codec->spec = NULL;
  771. kfree(spec);
  772. return -EINVAL;
  773. }
  774. codec->patch_ops = intel_hdmi_patch_ops;
  775. for (i = 0; i < spec->num_pins; i++)
  776. snd_hda_eld_proc_new(codec, &spec->sink_eld[i], i);
  777. init_channel_allocations();
  778. return 0;
  779. }
  780. static struct hda_codec_preset snd_hda_preset_intelhdmi[] = {
  781. { .id = 0x808629fb, .name = "G45 DEVCL", .patch = patch_intel_hdmi },
  782. { .id = 0x80862801, .name = "G45 DEVBLC", .patch = patch_intel_hdmi },
  783. { .id = 0x80862802, .name = "G45 DEVCTG", .patch = patch_intel_hdmi },
  784. { .id = 0x80862803, .name = "G45 DEVELK", .patch = patch_intel_hdmi },
  785. { .id = 0x80862804, .name = "G45 DEVIBX", .patch = patch_intel_hdmi },
  786. { .id = 0x80860054, .name = "Q57 DEVIBX", .patch = patch_intel_hdmi },
  787. { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_intel_hdmi },
  788. {} /* terminator */
  789. };
  790. MODULE_ALIAS("snd-hda-codec-id:808629fb");
  791. MODULE_ALIAS("snd-hda-codec-id:80862801");
  792. MODULE_ALIAS("snd-hda-codec-id:80862802");
  793. MODULE_ALIAS("snd-hda-codec-id:80862803");
  794. MODULE_ALIAS("snd-hda-codec-id:80862804");
  795. MODULE_ALIAS("snd-hda-codec-id:80860054");
  796. MODULE_ALIAS("snd-hda-codec-id:10951392");
  797. MODULE_LICENSE("GPL");
  798. MODULE_DESCRIPTION("Intel HDMI HD-audio codec");
  799. static struct hda_codec_preset_list intel_list = {
  800. .preset = snd_hda_preset_intelhdmi,
  801. .owner = THIS_MODULE,
  802. };
  803. static int __init patch_intelhdmi_init(void)
  804. {
  805. return snd_hda_add_codec_preset(&intel_list);
  806. }
  807. static void __exit patch_intelhdmi_exit(void)
  808. {
  809. snd_hda_delete_codec_preset(&intel_list);
  810. }
  811. module_init(patch_intelhdmi_init)
  812. module_exit(patch_intelhdmi_exit)