patch_hdmi.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980
  1. /*
  2. *
  3. * patch_hdmi.c - routines for HDMI/DisplayPort codecs
  4. *
  5. * Copyright(c) 2008-2010 Intel Corporation. All rights reserved.
  6. * Copyright (c) 2006 ATI Technologies Inc.
  7. * Copyright (c) 2008 NVIDIA Corp. All rights reserved.
  8. * Copyright (c) 2008 Wei Ni <wni@nvidia.com>
  9. *
  10. * Authors:
  11. * Wu Fengguang <wfg@linux.intel.com>
  12. *
  13. * Maintained by:
  14. * Wu Fengguang <wfg@linux.intel.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU General Public License as published by the Free
  18. * Software Foundation; either version 2 of the License, or (at your option)
  19. * any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful, but
  22. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  23. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  24. * for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software Foundation,
  28. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  29. */
  30. #include <linux/init.h>
  31. #include <linux/delay.h>
  32. #include <linux/slab.h>
  33. #include <linux/module.h>
  34. #include <sound/core.h>
  35. #include <sound/jack.h>
  36. #include "hda_codec.h"
  37. #include "hda_local.h"
  38. static bool static_hdmi_pcm;
  39. module_param(static_hdmi_pcm, bool, 0644);
  40. MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
  41. /*
  42. * The HDMI/DisplayPort configuration can be highly dynamic. A graphics device
  43. * could support N independent pipes, each of them can be connected to one or
  44. * more ports (DVI, HDMI or DisplayPort).
  45. *
  46. * The HDA correspondence of pipes/ports are converter/pin nodes.
  47. */
  48. #define MAX_HDMI_CVTS 4
  49. #define MAX_HDMI_PINS 4
  50. struct hdmi_spec_per_cvt {
  51. hda_nid_t cvt_nid;
  52. int assigned;
  53. unsigned int channels_min;
  54. unsigned int channels_max;
  55. u32 rates;
  56. u64 formats;
  57. unsigned int maxbps;
  58. };
  59. struct hdmi_spec_per_pin {
  60. hda_nid_t pin_nid;
  61. int num_mux_nids;
  62. hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
  63. struct hda_codec *codec;
  64. struct hdmi_eld sink_eld;
  65. struct delayed_work work;
  66. };
  67. struct hdmi_spec {
  68. int num_cvts;
  69. struct hdmi_spec_per_cvt cvts[MAX_HDMI_CVTS];
  70. int num_pins;
  71. struct hdmi_spec_per_pin pins[MAX_HDMI_PINS];
  72. struct hda_pcm pcm_rec[MAX_HDMI_PINS];
  73. /*
  74. * Non-generic ATI/NVIDIA specific
  75. */
  76. struct hda_multi_out multiout;
  77. const struct hda_pcm_stream *pcm_playback;
  78. };
  79. struct hdmi_audio_infoframe {
  80. u8 type; /* 0x84 */
  81. u8 ver; /* 0x01 */
  82. u8 len; /* 0x0a */
  83. u8 checksum;
  84. u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
  85. u8 SS01_SF24;
  86. u8 CXT04;
  87. u8 CA;
  88. u8 LFEPBL01_LSV36_DM_INH7;
  89. };
  90. struct dp_audio_infoframe {
  91. u8 type; /* 0x84 */
  92. u8 len; /* 0x1b */
  93. u8 ver; /* 0x11 << 2 */
  94. u8 CC02_CT47; /* match with HDMI infoframe from this on */
  95. u8 SS01_SF24;
  96. u8 CXT04;
  97. u8 CA;
  98. u8 LFEPBL01_LSV36_DM_INH7;
  99. };
  100. union audio_infoframe {
  101. struct hdmi_audio_infoframe hdmi;
  102. struct dp_audio_infoframe dp;
  103. u8 bytes[0];
  104. };
  105. /*
  106. * CEA speaker placement:
  107. *
  108. * FLH FCH FRH
  109. * FLW FL FLC FC FRC FR FRW
  110. *
  111. * LFE
  112. * TC
  113. *
  114. * RL RLC RC RRC RR
  115. *
  116. * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
  117. * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
  118. */
  119. enum cea_speaker_placement {
  120. FL = (1 << 0), /* Front Left */
  121. FC = (1 << 1), /* Front Center */
  122. FR = (1 << 2), /* Front Right */
  123. FLC = (1 << 3), /* Front Left Center */
  124. FRC = (1 << 4), /* Front Right Center */
  125. RL = (1 << 5), /* Rear Left */
  126. RC = (1 << 6), /* Rear Center */
  127. RR = (1 << 7), /* Rear Right */
  128. RLC = (1 << 8), /* Rear Left Center */
  129. RRC = (1 << 9), /* Rear Right Center */
  130. LFE = (1 << 10), /* Low Frequency Effect */
  131. FLW = (1 << 11), /* Front Left Wide */
  132. FRW = (1 << 12), /* Front Right Wide */
  133. FLH = (1 << 13), /* Front Left High */
  134. FCH = (1 << 14), /* Front Center High */
  135. FRH = (1 << 15), /* Front Right High */
  136. TC = (1 << 16), /* Top Center */
  137. };
  138. /*
  139. * ELD SA bits in the CEA Speaker Allocation data block
  140. */
  141. static int eld_speaker_allocation_bits[] = {
  142. [0] = FL | FR,
  143. [1] = LFE,
  144. [2] = FC,
  145. [3] = RL | RR,
  146. [4] = RC,
  147. [5] = FLC | FRC,
  148. [6] = RLC | RRC,
  149. /* the following are not defined in ELD yet */
  150. [7] = FLW | FRW,
  151. [8] = FLH | FRH,
  152. [9] = TC,
  153. [10] = FCH,
  154. };
  155. struct cea_channel_speaker_allocation {
  156. int ca_index;
  157. int speakers[8];
  158. /* derived values, just for convenience */
  159. int channels;
  160. int spk_mask;
  161. };
  162. /*
  163. * ALSA sequence is:
  164. *
  165. * surround40 surround41 surround50 surround51 surround71
  166. * ch0 front left = = = =
  167. * ch1 front right = = = =
  168. * ch2 rear left = = = =
  169. * ch3 rear right = = = =
  170. * ch4 LFE center center center
  171. * ch5 LFE LFE
  172. * ch6 side left
  173. * ch7 side right
  174. *
  175. * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
  176. */
  177. static int hdmi_channel_mapping[0x32][8] = {
  178. /* stereo */
  179. [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
  180. /* 2.1 */
  181. [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
  182. /* Dolby Surround */
  183. [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
  184. /* surround40 */
  185. [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
  186. /* 4ch */
  187. [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
  188. /* surround41 */
  189. [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
  190. /* surround50 */
  191. [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
  192. /* surround51 */
  193. [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
  194. /* 7.1 */
  195. [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
  196. };
  197. /*
  198. * This is an ordered list!
  199. *
  200. * The preceding ones have better chances to be selected by
  201. * hdmi_channel_allocation().
  202. */
  203. static struct cea_channel_speaker_allocation channel_allocations[] = {
  204. /* channel: 7 6 5 4 3 2 1 0 */
  205. { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
  206. /* 2.1 */
  207. { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
  208. /* Dolby Surround */
  209. { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
  210. /* surround40 */
  211. { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
  212. /* surround41 */
  213. { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
  214. /* surround50 */
  215. { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
  216. /* surround51 */
  217. { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
  218. /* 6.1 */
  219. { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
  220. /* surround71 */
  221. { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
  222. { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
  223. { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
  224. { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
  225. { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
  226. { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
  227. { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
  228. { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
  229. { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
  230. { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
  231. { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
  232. { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
  233. { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
  234. { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
  235. { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
  236. { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
  237. { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
  238. { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
  239. { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
  240. { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
  241. { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
  242. { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
  243. { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
  244. { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
  245. { .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
  246. { .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
  247. { .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
  248. { .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
  249. { .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
  250. { .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
  251. { .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
  252. { .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
  253. { .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
  254. { .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
  255. { .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
  256. { .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
  257. { .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
  258. { .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
  259. { .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
  260. { .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
  261. { .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
  262. { .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
  263. };
  264. /*
  265. * HDMI routines
  266. */
  267. static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
  268. {
  269. int pin_idx;
  270. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
  271. if (spec->pins[pin_idx].pin_nid == pin_nid)
  272. return pin_idx;
  273. snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
  274. return -EINVAL;
  275. }
  276. static int hinfo_to_pin_index(struct hdmi_spec *spec,
  277. struct hda_pcm_stream *hinfo)
  278. {
  279. int pin_idx;
  280. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
  281. if (&spec->pcm_rec[pin_idx].stream[0] == hinfo)
  282. return pin_idx;
  283. snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
  284. return -EINVAL;
  285. }
  286. static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
  287. {
  288. int cvt_idx;
  289. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
  290. if (spec->cvts[cvt_idx].cvt_nid == cvt_nid)
  291. return cvt_idx;
  292. snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
  293. return -EINVAL;
  294. }
  295. static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
  296. struct snd_ctl_elem_info *uinfo)
  297. {
  298. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  299. struct hdmi_spec *spec;
  300. int pin_idx;
  301. spec = codec->spec;
  302. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  303. pin_idx = kcontrol->private_value;
  304. uinfo->count = spec->pins[pin_idx].sink_eld.eld_size;
  305. return 0;
  306. }
  307. static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
  308. struct snd_ctl_elem_value *ucontrol)
  309. {
  310. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  311. struct hdmi_spec *spec;
  312. int pin_idx;
  313. spec = codec->spec;
  314. pin_idx = kcontrol->private_value;
  315. memcpy(ucontrol->value.bytes.data,
  316. spec->pins[pin_idx].sink_eld.eld_buffer, ELD_MAX_SIZE);
  317. return 0;
  318. }
  319. static struct snd_kcontrol_new eld_bytes_ctl = {
  320. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  321. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  322. .name = "ELD",
  323. .info = hdmi_eld_ctl_info,
  324. .get = hdmi_eld_ctl_get,
  325. };
  326. static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
  327. int device)
  328. {
  329. struct snd_kcontrol *kctl;
  330. struct hdmi_spec *spec = codec->spec;
  331. int err;
  332. kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
  333. if (!kctl)
  334. return -ENOMEM;
  335. kctl->private_value = pin_idx;
  336. kctl->id.device = device;
  337. err = snd_hda_ctl_add(codec, spec->pins[pin_idx].pin_nid, kctl);
  338. if (err < 0)
  339. return err;
  340. return 0;
  341. }
  342. #ifdef BE_PARANOID
  343. static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
  344. int *packet_index, int *byte_index)
  345. {
  346. int val;
  347. val = snd_hda_codec_read(codec, pin_nid, 0,
  348. AC_VERB_GET_HDMI_DIP_INDEX, 0);
  349. *packet_index = val >> 5;
  350. *byte_index = val & 0x1f;
  351. }
  352. #endif
  353. static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
  354. int packet_index, int byte_index)
  355. {
  356. int val;
  357. val = (packet_index << 5) | (byte_index & 0x1f);
  358. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
  359. }
  360. static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
  361. unsigned char val)
  362. {
  363. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
  364. }
  365. static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  366. {
  367. /* Unmute */
  368. if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
  369. snd_hda_codec_write(codec, pin_nid, 0,
  370. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
  371. /* Disable pin out until stream is active*/
  372. snd_hda_codec_write(codec, pin_nid, 0,
  373. AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
  374. }
  375. static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
  376. {
  377. return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
  378. AC_VERB_GET_CVT_CHAN_COUNT, 0);
  379. }
  380. static void hdmi_set_channel_count(struct hda_codec *codec,
  381. hda_nid_t cvt_nid, int chs)
  382. {
  383. if (chs != hdmi_get_channel_count(codec, cvt_nid))
  384. snd_hda_codec_write(codec, cvt_nid, 0,
  385. AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
  386. }
  387. /*
  388. * Channel mapping routines
  389. */
  390. /*
  391. * Compute derived values in channel_allocations[].
  392. */
  393. static void init_channel_allocations(void)
  394. {
  395. int i, j;
  396. struct cea_channel_speaker_allocation *p;
  397. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  398. p = channel_allocations + i;
  399. p->channels = 0;
  400. p->spk_mask = 0;
  401. for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
  402. if (p->speakers[j]) {
  403. p->channels++;
  404. p->spk_mask |= p->speakers[j];
  405. }
  406. }
  407. }
  408. /*
  409. * The transformation takes two steps:
  410. *
  411. * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
  412. * spk_mask => (channel_allocations[]) => ai->CA
  413. *
  414. * TODO: it could select the wrong CA from multiple candidates.
  415. */
  416. static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
  417. {
  418. int i;
  419. int ca = 0;
  420. int spk_mask = 0;
  421. char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
  422. /*
  423. * CA defaults to 0 for basic stereo audio
  424. */
  425. if (channels <= 2)
  426. return 0;
  427. /*
  428. * expand ELD's speaker allocation mask
  429. *
  430. * ELD tells the speaker mask in a compact(paired) form,
  431. * expand ELD's notions to match the ones used by Audio InfoFrame.
  432. */
  433. for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
  434. if (eld->spk_alloc & (1 << i))
  435. spk_mask |= eld_speaker_allocation_bits[i];
  436. }
  437. /* search for the first working match in the CA table */
  438. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  439. if (channels == channel_allocations[i].channels &&
  440. (spk_mask & channel_allocations[i].spk_mask) ==
  441. channel_allocations[i].spk_mask) {
  442. ca = channel_allocations[i].ca_index;
  443. break;
  444. }
  445. }
  446. snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
  447. snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
  448. ca, channels, buf);
  449. return ca;
  450. }
  451. static void hdmi_debug_channel_mapping(struct hda_codec *codec,
  452. hda_nid_t pin_nid)
  453. {
  454. #ifdef CONFIG_SND_DEBUG_VERBOSE
  455. int i;
  456. int slot;
  457. for (i = 0; i < 8; i++) {
  458. slot = snd_hda_codec_read(codec, pin_nid, 0,
  459. AC_VERB_GET_HDMI_CHAN_SLOT, i);
  460. printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
  461. slot >> 4, slot & 0xf);
  462. }
  463. #endif
  464. }
  465. static void hdmi_setup_channel_mapping(struct hda_codec *codec,
  466. hda_nid_t pin_nid,
  467. int ca)
  468. {
  469. int i;
  470. int err;
  471. if (hdmi_channel_mapping[ca][1] == 0) {
  472. for (i = 0; i < channel_allocations[ca].channels; i++)
  473. hdmi_channel_mapping[ca][i] = i | (i << 4);
  474. for (; i < 8; i++)
  475. hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
  476. }
  477. for (i = 0; i < 8; i++) {
  478. err = snd_hda_codec_write(codec, pin_nid, 0,
  479. AC_VERB_SET_HDMI_CHAN_SLOT,
  480. hdmi_channel_mapping[ca][i]);
  481. if (err) {
  482. snd_printdd(KERN_NOTICE
  483. "HDMI: channel mapping failed\n");
  484. break;
  485. }
  486. }
  487. hdmi_debug_channel_mapping(codec, pin_nid);
  488. }
  489. /*
  490. * Audio InfoFrame routines
  491. */
  492. /*
  493. * Enable Audio InfoFrame Transmission
  494. */
  495. static void hdmi_start_infoframe_trans(struct hda_codec *codec,
  496. hda_nid_t pin_nid)
  497. {
  498. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  499. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  500. AC_DIPXMIT_BEST);
  501. }
  502. /*
  503. * Disable Audio InfoFrame Transmission
  504. */
  505. static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
  506. hda_nid_t pin_nid)
  507. {
  508. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  509. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  510. AC_DIPXMIT_DISABLE);
  511. }
  512. static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
  513. {
  514. #ifdef CONFIG_SND_DEBUG_VERBOSE
  515. int i;
  516. int size;
  517. size = snd_hdmi_get_eld_size(codec, pin_nid);
  518. printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
  519. for (i = 0; i < 8; i++) {
  520. size = snd_hda_codec_read(codec, pin_nid, 0,
  521. AC_VERB_GET_HDMI_DIP_SIZE, i);
  522. printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
  523. }
  524. #endif
  525. }
  526. static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
  527. {
  528. #ifdef BE_PARANOID
  529. int i, j;
  530. int size;
  531. int pi, bi;
  532. for (i = 0; i < 8; i++) {
  533. size = snd_hda_codec_read(codec, pin_nid, 0,
  534. AC_VERB_GET_HDMI_DIP_SIZE, i);
  535. if (size == 0)
  536. continue;
  537. hdmi_set_dip_index(codec, pin_nid, i, 0x0);
  538. for (j = 1; j < 1000; j++) {
  539. hdmi_write_dip_byte(codec, pin_nid, 0x0);
  540. hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
  541. if (pi != i)
  542. snd_printd(KERN_INFO "dip index %d: %d != %d\n",
  543. bi, pi, i);
  544. if (bi == 0) /* byte index wrapped around */
  545. break;
  546. }
  547. snd_printd(KERN_INFO
  548. "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
  549. i, size, j);
  550. }
  551. #endif
  552. }
  553. static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
  554. {
  555. u8 *bytes = (u8 *)hdmi_ai;
  556. u8 sum = 0;
  557. int i;
  558. hdmi_ai->checksum = 0;
  559. for (i = 0; i < sizeof(*hdmi_ai); i++)
  560. sum += bytes[i];
  561. hdmi_ai->checksum = -sum;
  562. }
  563. static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
  564. hda_nid_t pin_nid,
  565. u8 *dip, int size)
  566. {
  567. int i;
  568. hdmi_debug_dip_size(codec, pin_nid);
  569. hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
  570. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  571. for (i = 0; i < size; i++)
  572. hdmi_write_dip_byte(codec, pin_nid, dip[i]);
  573. }
  574. static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
  575. u8 *dip, int size)
  576. {
  577. u8 val;
  578. int i;
  579. if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
  580. != AC_DIPXMIT_BEST)
  581. return false;
  582. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  583. for (i = 0; i < size; i++) {
  584. val = snd_hda_codec_read(codec, pin_nid, 0,
  585. AC_VERB_GET_HDMI_DIP_DATA, 0);
  586. if (val != dip[i])
  587. return false;
  588. }
  589. return true;
  590. }
  591. static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
  592. struct snd_pcm_substream *substream)
  593. {
  594. struct hdmi_spec *spec = codec->spec;
  595. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  596. hda_nid_t pin_nid = per_pin->pin_nid;
  597. int channels = substream->runtime->channels;
  598. struct hdmi_eld *eld;
  599. int ca;
  600. union audio_infoframe ai;
  601. eld = &spec->pins[pin_idx].sink_eld;
  602. if (!eld->monitor_present)
  603. return;
  604. ca = hdmi_channel_allocation(eld, channels);
  605. memset(&ai, 0, sizeof(ai));
  606. if (eld->conn_type == 0) { /* HDMI */
  607. struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
  608. hdmi_ai->type = 0x84;
  609. hdmi_ai->ver = 0x01;
  610. hdmi_ai->len = 0x0a;
  611. hdmi_ai->CC02_CT47 = channels - 1;
  612. hdmi_ai->CA = ca;
  613. hdmi_checksum_audio_infoframe(hdmi_ai);
  614. } else if (eld->conn_type == 1) { /* DisplayPort */
  615. struct dp_audio_infoframe *dp_ai = &ai.dp;
  616. dp_ai->type = 0x84;
  617. dp_ai->len = 0x1b;
  618. dp_ai->ver = 0x11 << 2;
  619. dp_ai->CC02_CT47 = channels - 1;
  620. dp_ai->CA = ca;
  621. } else {
  622. snd_printd("HDMI: unknown connection type at pin %d\n",
  623. pin_nid);
  624. return;
  625. }
  626. /*
  627. * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
  628. * sizeof(*dp_ai) to avoid partial match/update problems when
  629. * the user switches between HDMI/DP monitors.
  630. */
  631. if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
  632. sizeof(ai))) {
  633. snd_printdd("hdmi_setup_audio_infoframe: "
  634. "pin=%d channels=%d\n",
  635. pin_nid,
  636. channels);
  637. hdmi_setup_channel_mapping(codec, pin_nid, ca);
  638. hdmi_stop_infoframe_trans(codec, pin_nid);
  639. hdmi_fill_audio_infoframe(codec, pin_nid,
  640. ai.bytes, sizeof(ai));
  641. hdmi_start_infoframe_trans(codec, pin_nid);
  642. }
  643. }
  644. /*
  645. * Unsolicited events
  646. */
  647. static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, bool retry);
  648. static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
  649. {
  650. struct hdmi_spec *spec = codec->spec;
  651. int pin_nid = res >> AC_UNSOL_RES_TAG_SHIFT;
  652. int pd = !!(res & AC_UNSOL_RES_PD);
  653. int eldv = !!(res & AC_UNSOL_RES_ELDV);
  654. int pin_idx;
  655. printk(KERN_INFO
  656. "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  657. codec->addr, pin_nid, pd, eldv);
  658. pin_idx = pin_nid_to_pin_index(spec, pin_nid);
  659. if (pin_idx < 0)
  660. return;
  661. hdmi_present_sense(&spec->pins[pin_idx], true);
  662. }
  663. static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
  664. {
  665. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  666. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  667. int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
  668. int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
  669. printk(KERN_INFO
  670. "HDMI CP event: CODEC=%d PIN=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
  671. codec->addr,
  672. tag,
  673. subtag,
  674. cp_state,
  675. cp_ready);
  676. /* TODO */
  677. if (cp_state)
  678. ;
  679. if (cp_ready)
  680. ;
  681. }
  682. static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
  683. {
  684. struct hdmi_spec *spec = codec->spec;
  685. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  686. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  687. if (pin_nid_to_pin_index(spec, tag) < 0) {
  688. snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
  689. return;
  690. }
  691. if (subtag == 0)
  692. hdmi_intrinsic_event(codec, res);
  693. else
  694. hdmi_non_intrinsic_event(codec, res);
  695. }
  696. /*
  697. * Callbacks
  698. */
  699. /* HBR should be Non-PCM, 8 channels */
  700. #define is_hbr_format(format) \
  701. ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
  702. static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
  703. hda_nid_t pin_nid, u32 stream_tag, int format)
  704. {
  705. int pinctl;
  706. int new_pinctl = 0;
  707. if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
  708. pinctl = snd_hda_codec_read(codec, pin_nid, 0,
  709. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  710. new_pinctl = pinctl & ~AC_PINCTL_EPT;
  711. if (is_hbr_format(format))
  712. new_pinctl |= AC_PINCTL_EPT_HBR;
  713. else
  714. new_pinctl |= AC_PINCTL_EPT_NATIVE;
  715. snd_printdd("hdmi_setup_stream: "
  716. "NID=0x%x, %spinctl=0x%x\n",
  717. pin_nid,
  718. pinctl == new_pinctl ? "" : "new-",
  719. new_pinctl);
  720. if (pinctl != new_pinctl)
  721. snd_hda_codec_write(codec, pin_nid, 0,
  722. AC_VERB_SET_PIN_WIDGET_CONTROL,
  723. new_pinctl);
  724. }
  725. if (is_hbr_format(format) && !new_pinctl) {
  726. snd_printdd("hdmi_setup_stream: HBR is not supported\n");
  727. return -EINVAL;
  728. }
  729. snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
  730. return 0;
  731. }
  732. /*
  733. * HDA PCM callbacks
  734. */
  735. static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
  736. struct hda_codec *codec,
  737. struct snd_pcm_substream *substream)
  738. {
  739. struct hdmi_spec *spec = codec->spec;
  740. struct snd_pcm_runtime *runtime = substream->runtime;
  741. int pin_idx, cvt_idx, mux_idx = 0;
  742. struct hdmi_spec_per_pin *per_pin;
  743. struct hdmi_eld *eld;
  744. struct hdmi_spec_per_cvt *per_cvt = NULL;
  745. int pinctl;
  746. /* Validate hinfo */
  747. pin_idx = hinfo_to_pin_index(spec, hinfo);
  748. if (snd_BUG_ON(pin_idx < 0))
  749. return -EINVAL;
  750. per_pin = &spec->pins[pin_idx];
  751. eld = &per_pin->sink_eld;
  752. /* Dynamically assign converter to stream */
  753. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
  754. per_cvt = &spec->cvts[cvt_idx];
  755. /* Must not already be assigned */
  756. if (per_cvt->assigned)
  757. continue;
  758. /* Must be in pin's mux's list of converters */
  759. for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
  760. if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
  761. break;
  762. /* Not in mux list */
  763. if (mux_idx == per_pin->num_mux_nids)
  764. continue;
  765. break;
  766. }
  767. /* No free converters */
  768. if (cvt_idx == spec->num_cvts)
  769. return -ENODEV;
  770. /* Claim converter */
  771. per_cvt->assigned = 1;
  772. hinfo->nid = per_cvt->cvt_nid;
  773. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  774. AC_VERB_SET_CONNECT_SEL,
  775. mux_idx);
  776. pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
  777. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  778. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  779. AC_VERB_SET_PIN_WIDGET_CONTROL,
  780. pinctl | PIN_OUT);
  781. snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
  782. /* Initially set the converter's capabilities */
  783. hinfo->channels_min = per_cvt->channels_min;
  784. hinfo->channels_max = per_cvt->channels_max;
  785. hinfo->rates = per_cvt->rates;
  786. hinfo->formats = per_cvt->formats;
  787. hinfo->maxbps = per_cvt->maxbps;
  788. /* Restrict capabilities by ELD if this isn't disabled */
  789. if (!static_hdmi_pcm && eld->eld_valid) {
  790. snd_hdmi_eld_update_pcm_info(eld, hinfo);
  791. if (hinfo->channels_min > hinfo->channels_max ||
  792. !hinfo->rates || !hinfo->formats)
  793. return -ENODEV;
  794. }
  795. /* Store the updated parameters */
  796. runtime->hw.channels_min = hinfo->channels_min;
  797. runtime->hw.channels_max = hinfo->channels_max;
  798. runtime->hw.formats = hinfo->formats;
  799. runtime->hw.rates = hinfo->rates;
  800. snd_pcm_hw_constraint_step(substream->runtime, 0,
  801. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  802. return 0;
  803. }
  804. /*
  805. * HDA/HDMI auto parsing
  806. */
  807. static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
  808. {
  809. struct hdmi_spec *spec = codec->spec;
  810. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  811. hda_nid_t pin_nid = per_pin->pin_nid;
  812. if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
  813. snd_printk(KERN_WARNING
  814. "HDMI: pin %d wcaps %#x "
  815. "does not support connection list\n",
  816. pin_nid, get_wcaps(codec, pin_nid));
  817. return -EINVAL;
  818. }
  819. per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
  820. per_pin->mux_nids,
  821. HDA_MAX_CONNECTIONS);
  822. return 0;
  823. }
  824. static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, bool retry)
  825. {
  826. struct hda_codec *codec = per_pin->codec;
  827. struct hdmi_eld *eld = &per_pin->sink_eld;
  828. hda_nid_t pin_nid = per_pin->pin_nid;
  829. /*
  830. * Always execute a GetPinSense verb here, even when called from
  831. * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
  832. * response's PD bit is not the real PD value, but indicates that
  833. * the real PD value changed. An older version of the HD-audio
  834. * specification worked this way. Hence, we just ignore the data in
  835. * the unsolicited response to avoid custom WARs.
  836. */
  837. int present = snd_hda_pin_sense(codec, pin_nid);
  838. bool eld_valid = false;
  839. memset(eld, 0, offsetof(struct hdmi_eld, eld_buffer));
  840. eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
  841. if (eld->monitor_present)
  842. eld_valid = !!(present & AC_PINSENSE_ELDV);
  843. printk(KERN_INFO
  844. "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  845. codec->addr, pin_nid, eld->monitor_present, eld_valid);
  846. if (eld_valid) {
  847. if (!snd_hdmi_get_eld(eld, codec, pin_nid))
  848. snd_hdmi_show_eld(eld);
  849. else if (retry) {
  850. queue_delayed_work(codec->bus->workq,
  851. &per_pin->work,
  852. msecs_to_jiffies(300));
  853. }
  854. }
  855. snd_hda_input_jack_report(codec, pin_nid);
  856. }
  857. static void hdmi_repoll_eld(struct work_struct *work)
  858. {
  859. struct hdmi_spec_per_pin *per_pin =
  860. container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
  861. hdmi_present_sense(per_pin, false);
  862. }
  863. static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  864. {
  865. struct hdmi_spec *spec = codec->spec;
  866. unsigned int caps, config;
  867. int pin_idx;
  868. struct hdmi_spec_per_pin *per_pin;
  869. int err;
  870. caps = snd_hda_param_read(codec, pin_nid, AC_PAR_PIN_CAP);
  871. if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
  872. return 0;
  873. config = snd_hda_codec_read(codec, pin_nid, 0,
  874. AC_VERB_GET_CONFIG_DEFAULT, 0);
  875. if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
  876. return 0;
  877. if (snd_BUG_ON(spec->num_pins >= MAX_HDMI_PINS))
  878. return -E2BIG;
  879. pin_idx = spec->num_pins;
  880. per_pin = &spec->pins[pin_idx];
  881. per_pin->pin_nid = pin_nid;
  882. err = hdmi_read_pin_conn(codec, pin_idx);
  883. if (err < 0)
  884. return err;
  885. spec->num_pins++;
  886. return 0;
  887. }
  888. static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
  889. {
  890. struct hdmi_spec *spec = codec->spec;
  891. int cvt_idx;
  892. struct hdmi_spec_per_cvt *per_cvt;
  893. unsigned int chans;
  894. int err;
  895. if (snd_BUG_ON(spec->num_cvts >= MAX_HDMI_CVTS))
  896. return -E2BIG;
  897. chans = get_wcaps(codec, cvt_nid);
  898. chans = get_wcaps_channels(chans);
  899. cvt_idx = spec->num_cvts;
  900. per_cvt = &spec->cvts[cvt_idx];
  901. per_cvt->cvt_nid = cvt_nid;
  902. per_cvt->channels_min = 2;
  903. if (chans <= 16)
  904. per_cvt->channels_max = chans;
  905. err = snd_hda_query_supported_pcm(codec, cvt_nid,
  906. &per_cvt->rates,
  907. &per_cvt->formats,
  908. &per_cvt->maxbps);
  909. if (err < 0)
  910. return err;
  911. spec->num_cvts++;
  912. return 0;
  913. }
  914. static int hdmi_parse_codec(struct hda_codec *codec)
  915. {
  916. hda_nid_t nid;
  917. int i, nodes;
  918. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
  919. if (!nid || nodes < 0) {
  920. snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
  921. return -EINVAL;
  922. }
  923. for (i = 0; i < nodes; i++, nid++) {
  924. unsigned int caps;
  925. unsigned int type;
  926. caps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
  927. type = get_wcaps_type(caps);
  928. if (!(caps & AC_WCAP_DIGITAL))
  929. continue;
  930. switch (type) {
  931. case AC_WID_AUD_OUT:
  932. hdmi_add_cvt(codec, nid);
  933. break;
  934. case AC_WID_PIN:
  935. hdmi_add_pin(codec, nid);
  936. break;
  937. }
  938. }
  939. /*
  940. * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
  941. * can be lost and presence sense verb will become inaccurate if the
  942. * HDA link is powered off at hot plug or hw initialization time.
  943. */
  944. #ifdef CONFIG_SND_HDA_POWER_SAVE
  945. if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
  946. AC_PWRST_EPSS))
  947. codec->bus->power_keep_link_on = 1;
  948. #endif
  949. return 0;
  950. }
  951. /*
  952. */
  953. static char *generic_hdmi_pcm_names[MAX_HDMI_PINS] = {
  954. "HDMI 0",
  955. "HDMI 1",
  956. "HDMI 2",
  957. "HDMI 3",
  958. };
  959. /*
  960. * HDMI callbacks
  961. */
  962. static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  963. struct hda_codec *codec,
  964. unsigned int stream_tag,
  965. unsigned int format,
  966. struct snd_pcm_substream *substream)
  967. {
  968. hda_nid_t cvt_nid = hinfo->nid;
  969. struct hdmi_spec *spec = codec->spec;
  970. int pin_idx = hinfo_to_pin_index(spec, hinfo);
  971. hda_nid_t pin_nid = spec->pins[pin_idx].pin_nid;
  972. hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
  973. hdmi_setup_audio_infoframe(codec, pin_idx, substream);
  974. return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
  975. }
  976. static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  977. struct hda_codec *codec,
  978. struct snd_pcm_substream *substream)
  979. {
  980. struct hdmi_spec *spec = codec->spec;
  981. int cvt_idx, pin_idx;
  982. struct hdmi_spec_per_cvt *per_cvt;
  983. struct hdmi_spec_per_pin *per_pin;
  984. int pinctl;
  985. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  986. if (hinfo->nid) {
  987. cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
  988. if (snd_BUG_ON(cvt_idx < 0))
  989. return -EINVAL;
  990. per_cvt = &spec->cvts[cvt_idx];
  991. snd_BUG_ON(!per_cvt->assigned);
  992. per_cvt->assigned = 0;
  993. hinfo->nid = 0;
  994. pin_idx = hinfo_to_pin_index(spec, hinfo);
  995. if (snd_BUG_ON(pin_idx < 0))
  996. return -EINVAL;
  997. per_pin = &spec->pins[pin_idx];
  998. pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
  999. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1000. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  1001. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1002. pinctl & ~PIN_OUT);
  1003. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1004. }
  1005. return 0;
  1006. }
  1007. static const struct hda_pcm_ops generic_ops = {
  1008. .open = hdmi_pcm_open,
  1009. .prepare = generic_hdmi_playback_pcm_prepare,
  1010. .cleanup = generic_hdmi_playback_pcm_cleanup,
  1011. };
  1012. static int generic_hdmi_build_pcms(struct hda_codec *codec)
  1013. {
  1014. struct hdmi_spec *spec = codec->spec;
  1015. int pin_idx;
  1016. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1017. struct hda_pcm *info;
  1018. struct hda_pcm_stream *pstr;
  1019. info = &spec->pcm_rec[pin_idx];
  1020. info->name = generic_hdmi_pcm_names[pin_idx];
  1021. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1022. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1023. pstr->substreams = 1;
  1024. pstr->ops = generic_ops;
  1025. /* other pstr fields are set in open */
  1026. }
  1027. codec->num_pcms = spec->num_pins;
  1028. codec->pcm_info = spec->pcm_rec;
  1029. return 0;
  1030. }
  1031. static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
  1032. {
  1033. int err;
  1034. char hdmi_str[32];
  1035. struct hdmi_spec *spec = codec->spec;
  1036. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1037. int pcmdev = spec->pcm_rec[pin_idx].device;
  1038. snprintf(hdmi_str, sizeof(hdmi_str), "HDMI/DP,pcm=%d", pcmdev);
  1039. err = snd_hda_input_jack_add(codec, per_pin->pin_nid,
  1040. SND_JACK_VIDEOOUT, pcmdev > 0 ? hdmi_str : NULL);
  1041. if (err < 0)
  1042. return err;
  1043. hdmi_present_sense(per_pin, false);
  1044. return 0;
  1045. }
  1046. static int generic_hdmi_build_controls(struct hda_codec *codec)
  1047. {
  1048. struct hdmi_spec *spec = codec->spec;
  1049. int err;
  1050. int pin_idx;
  1051. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1052. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1053. err = generic_hdmi_build_jack(codec, pin_idx);
  1054. if (err < 0)
  1055. return err;
  1056. err = snd_hda_create_spdif_out_ctls(codec,
  1057. per_pin->pin_nid,
  1058. per_pin->mux_nids[0]);
  1059. if (err < 0)
  1060. return err;
  1061. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1062. /* add control for ELD Bytes */
  1063. err = hdmi_create_eld_ctl(codec,
  1064. pin_idx,
  1065. spec->pcm_rec[pin_idx].device);
  1066. if (err < 0)
  1067. return err;
  1068. }
  1069. return 0;
  1070. }
  1071. static int generic_hdmi_init(struct hda_codec *codec)
  1072. {
  1073. struct hdmi_spec *spec = codec->spec;
  1074. int pin_idx;
  1075. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1076. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1077. hda_nid_t pin_nid = per_pin->pin_nid;
  1078. struct hdmi_eld *eld = &per_pin->sink_eld;
  1079. hdmi_init_pin(codec, pin_nid);
  1080. snd_hda_codec_write(codec, pin_nid, 0,
  1081. AC_VERB_SET_UNSOLICITED_ENABLE,
  1082. AC_USRSP_EN | pin_nid);
  1083. per_pin->codec = codec;
  1084. INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
  1085. snd_hda_eld_proc_new(codec, eld, pin_idx);
  1086. }
  1087. return 0;
  1088. }
  1089. static void generic_hdmi_free(struct hda_codec *codec)
  1090. {
  1091. struct hdmi_spec *spec = codec->spec;
  1092. int pin_idx;
  1093. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1094. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1095. struct hdmi_eld *eld = &per_pin->sink_eld;
  1096. cancel_delayed_work(&per_pin->work);
  1097. snd_hda_eld_proc_free(codec, eld);
  1098. }
  1099. snd_hda_input_jack_free(codec);
  1100. flush_workqueue(codec->bus->workq);
  1101. kfree(spec);
  1102. }
  1103. static const struct hda_codec_ops generic_hdmi_patch_ops = {
  1104. .init = generic_hdmi_init,
  1105. .free = generic_hdmi_free,
  1106. .build_pcms = generic_hdmi_build_pcms,
  1107. .build_controls = generic_hdmi_build_controls,
  1108. .unsol_event = hdmi_unsol_event,
  1109. };
  1110. static int patch_generic_hdmi(struct hda_codec *codec)
  1111. {
  1112. struct hdmi_spec *spec;
  1113. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1114. if (spec == NULL)
  1115. return -ENOMEM;
  1116. codec->spec = spec;
  1117. if (hdmi_parse_codec(codec) < 0) {
  1118. codec->spec = NULL;
  1119. kfree(spec);
  1120. return -EINVAL;
  1121. }
  1122. codec->patch_ops = generic_hdmi_patch_ops;
  1123. init_channel_allocations();
  1124. return 0;
  1125. }
  1126. /*
  1127. * Shared non-generic implementations
  1128. */
  1129. static int simple_playback_build_pcms(struct hda_codec *codec)
  1130. {
  1131. struct hdmi_spec *spec = codec->spec;
  1132. struct hda_pcm *info = spec->pcm_rec;
  1133. int i;
  1134. codec->num_pcms = spec->num_cvts;
  1135. codec->pcm_info = info;
  1136. for (i = 0; i < codec->num_pcms; i++, info++) {
  1137. unsigned int chans;
  1138. struct hda_pcm_stream *pstr;
  1139. chans = get_wcaps(codec, spec->cvts[i].cvt_nid);
  1140. chans = get_wcaps_channels(chans);
  1141. info->name = generic_hdmi_pcm_names[i];
  1142. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1143. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1144. snd_BUG_ON(!spec->pcm_playback);
  1145. *pstr = *spec->pcm_playback;
  1146. pstr->nid = spec->cvts[i].cvt_nid;
  1147. if (pstr->channels_max <= 2 && chans && chans <= 16)
  1148. pstr->channels_max = chans;
  1149. }
  1150. return 0;
  1151. }
  1152. static int simple_playback_build_controls(struct hda_codec *codec)
  1153. {
  1154. struct hdmi_spec *spec = codec->spec;
  1155. int err;
  1156. int i;
  1157. for (i = 0; i < codec->num_pcms; i++) {
  1158. err = snd_hda_create_spdif_out_ctls(codec,
  1159. spec->cvts[i].cvt_nid,
  1160. spec->cvts[i].cvt_nid);
  1161. if (err < 0)
  1162. return err;
  1163. }
  1164. return 0;
  1165. }
  1166. static void simple_playback_free(struct hda_codec *codec)
  1167. {
  1168. struct hdmi_spec *spec = codec->spec;
  1169. kfree(spec);
  1170. }
  1171. /*
  1172. * Nvidia specific implementations
  1173. */
  1174. #define Nv_VERB_SET_Channel_Allocation 0xF79
  1175. #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
  1176. #define Nv_VERB_SET_Audio_Protection_On 0xF98
  1177. #define Nv_VERB_SET_Audio_Protection_Off 0xF99
  1178. #define nvhdmi_master_con_nid_7x 0x04
  1179. #define nvhdmi_master_pin_nid_7x 0x05
  1180. static const hda_nid_t nvhdmi_con_nids_7x[4] = {
  1181. /*front, rear, clfe, rear_surr */
  1182. 0x6, 0x8, 0xa, 0xc,
  1183. };
  1184. static const struct hda_verb nvhdmi_basic_init_7x[] = {
  1185. /* set audio protect on */
  1186. { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
  1187. /* enable digital output on pin widget */
  1188. { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1189. { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1190. { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1191. { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1192. { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1193. {} /* terminator */
  1194. };
  1195. #ifdef LIMITED_RATE_FMT_SUPPORT
  1196. /* support only the safe format and rate */
  1197. #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
  1198. #define SUPPORTED_MAXBPS 16
  1199. #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
  1200. #else
  1201. /* support all rates and formats */
  1202. #define SUPPORTED_RATES \
  1203. (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  1204. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
  1205. SNDRV_PCM_RATE_192000)
  1206. #define SUPPORTED_MAXBPS 24
  1207. #define SUPPORTED_FORMATS \
  1208. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1209. #endif
  1210. static int nvhdmi_7x_init(struct hda_codec *codec)
  1211. {
  1212. snd_hda_sequence_write(codec, nvhdmi_basic_init_7x);
  1213. return 0;
  1214. }
  1215. static unsigned int channels_2_6_8[] = {
  1216. 2, 6, 8
  1217. };
  1218. static unsigned int channels_2_8[] = {
  1219. 2, 8
  1220. };
  1221. static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
  1222. .count = ARRAY_SIZE(channels_2_6_8),
  1223. .list = channels_2_6_8,
  1224. .mask = 0,
  1225. };
  1226. static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
  1227. .count = ARRAY_SIZE(channels_2_8),
  1228. .list = channels_2_8,
  1229. .mask = 0,
  1230. };
  1231. static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
  1232. struct hda_codec *codec,
  1233. struct snd_pcm_substream *substream)
  1234. {
  1235. struct hdmi_spec *spec = codec->spec;
  1236. struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
  1237. switch (codec->preset->id) {
  1238. case 0x10de0002:
  1239. case 0x10de0003:
  1240. case 0x10de0005:
  1241. case 0x10de0006:
  1242. hw_constraints_channels = &hw_constraints_2_8_channels;
  1243. break;
  1244. case 0x10de0007:
  1245. hw_constraints_channels = &hw_constraints_2_6_8_channels;
  1246. break;
  1247. default:
  1248. break;
  1249. }
  1250. if (hw_constraints_channels != NULL) {
  1251. snd_pcm_hw_constraint_list(substream->runtime, 0,
  1252. SNDRV_PCM_HW_PARAM_CHANNELS,
  1253. hw_constraints_channels);
  1254. } else {
  1255. snd_pcm_hw_constraint_step(substream->runtime, 0,
  1256. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  1257. }
  1258. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  1259. }
  1260. static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
  1261. struct hda_codec *codec,
  1262. struct snd_pcm_substream *substream)
  1263. {
  1264. struct hdmi_spec *spec = codec->spec;
  1265. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  1266. }
  1267. static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1268. struct hda_codec *codec,
  1269. unsigned int stream_tag,
  1270. unsigned int format,
  1271. struct snd_pcm_substream *substream)
  1272. {
  1273. struct hdmi_spec *spec = codec->spec;
  1274. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  1275. stream_tag, format, substream);
  1276. }
  1277. static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
  1278. int channels)
  1279. {
  1280. unsigned int chanmask;
  1281. int chan = channels ? (channels - 1) : 1;
  1282. switch (channels) {
  1283. default:
  1284. case 0:
  1285. case 2:
  1286. chanmask = 0x00;
  1287. break;
  1288. case 4:
  1289. chanmask = 0x08;
  1290. break;
  1291. case 6:
  1292. chanmask = 0x0b;
  1293. break;
  1294. case 8:
  1295. chanmask = 0x13;
  1296. break;
  1297. }
  1298. /* Set the audio infoframe channel allocation and checksum fields. The
  1299. * channel count is computed implicitly by the hardware. */
  1300. snd_hda_codec_write(codec, 0x1, 0,
  1301. Nv_VERB_SET_Channel_Allocation, chanmask);
  1302. snd_hda_codec_write(codec, 0x1, 0,
  1303. Nv_VERB_SET_Info_Frame_Checksum,
  1304. (0x71 - chan - chanmask));
  1305. }
  1306. static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
  1307. struct hda_codec *codec,
  1308. struct snd_pcm_substream *substream)
  1309. {
  1310. struct hdmi_spec *spec = codec->spec;
  1311. int i;
  1312. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
  1313. 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  1314. for (i = 0; i < 4; i++) {
  1315. /* set the stream id */
  1316. snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
  1317. AC_VERB_SET_CHANNEL_STREAMID, 0);
  1318. /* set the stream format */
  1319. snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
  1320. AC_VERB_SET_STREAM_FORMAT, 0);
  1321. }
  1322. /* The audio hardware sends a channel count of 0x7 (8ch) when all the
  1323. * streams are disabled. */
  1324. nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
  1325. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  1326. }
  1327. static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
  1328. struct hda_codec *codec,
  1329. unsigned int stream_tag,
  1330. unsigned int format,
  1331. struct snd_pcm_substream *substream)
  1332. {
  1333. int chs;
  1334. unsigned int dataDCC2, channel_id;
  1335. int i;
  1336. struct hdmi_spec *spec = codec->spec;
  1337. struct hda_spdif_out *spdif =
  1338. snd_hda_spdif_out_of_nid(codec, spec->cvts[0].cvt_nid);
  1339. mutex_lock(&codec->spdif_mutex);
  1340. chs = substream->runtime->channels;
  1341. dataDCC2 = 0x2;
  1342. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  1343. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
  1344. snd_hda_codec_write(codec,
  1345. nvhdmi_master_con_nid_7x,
  1346. 0,
  1347. AC_VERB_SET_DIGI_CONVERT_1,
  1348. spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
  1349. /* set the stream id */
  1350. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
  1351. AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
  1352. /* set the stream format */
  1353. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
  1354. AC_VERB_SET_STREAM_FORMAT, format);
  1355. /* turn on again (if needed) */
  1356. /* enable and set the channel status audio/data flag */
  1357. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
  1358. snd_hda_codec_write(codec,
  1359. nvhdmi_master_con_nid_7x,
  1360. 0,
  1361. AC_VERB_SET_DIGI_CONVERT_1,
  1362. spdif->ctls & 0xff);
  1363. snd_hda_codec_write(codec,
  1364. nvhdmi_master_con_nid_7x,
  1365. 0,
  1366. AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
  1367. }
  1368. for (i = 0; i < 4; i++) {
  1369. if (chs == 2)
  1370. channel_id = 0;
  1371. else
  1372. channel_id = i * 2;
  1373. /* turn off SPDIF once;
  1374. *otherwise the IEC958 bits won't be updated
  1375. */
  1376. if (codec->spdif_status_reset &&
  1377. (spdif->ctls & AC_DIG1_ENABLE))
  1378. snd_hda_codec_write(codec,
  1379. nvhdmi_con_nids_7x[i],
  1380. 0,
  1381. AC_VERB_SET_DIGI_CONVERT_1,
  1382. spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
  1383. /* set the stream id */
  1384. snd_hda_codec_write(codec,
  1385. nvhdmi_con_nids_7x[i],
  1386. 0,
  1387. AC_VERB_SET_CHANNEL_STREAMID,
  1388. (stream_tag << 4) | channel_id);
  1389. /* set the stream format */
  1390. snd_hda_codec_write(codec,
  1391. nvhdmi_con_nids_7x[i],
  1392. 0,
  1393. AC_VERB_SET_STREAM_FORMAT,
  1394. format);
  1395. /* turn on again (if needed) */
  1396. /* enable and set the channel status audio/data flag */
  1397. if (codec->spdif_status_reset &&
  1398. (spdif->ctls & AC_DIG1_ENABLE)) {
  1399. snd_hda_codec_write(codec,
  1400. nvhdmi_con_nids_7x[i],
  1401. 0,
  1402. AC_VERB_SET_DIGI_CONVERT_1,
  1403. spdif->ctls & 0xff);
  1404. snd_hda_codec_write(codec,
  1405. nvhdmi_con_nids_7x[i],
  1406. 0,
  1407. AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
  1408. }
  1409. }
  1410. nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
  1411. mutex_unlock(&codec->spdif_mutex);
  1412. return 0;
  1413. }
  1414. static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
  1415. .substreams = 1,
  1416. .channels_min = 2,
  1417. .channels_max = 8,
  1418. .nid = nvhdmi_master_con_nid_7x,
  1419. .rates = SUPPORTED_RATES,
  1420. .maxbps = SUPPORTED_MAXBPS,
  1421. .formats = SUPPORTED_FORMATS,
  1422. .ops = {
  1423. .open = simple_playback_pcm_open,
  1424. .close = nvhdmi_8ch_7x_pcm_close,
  1425. .prepare = nvhdmi_8ch_7x_pcm_prepare
  1426. },
  1427. };
  1428. static const struct hda_pcm_stream nvhdmi_pcm_playback_2ch = {
  1429. .substreams = 1,
  1430. .channels_min = 2,
  1431. .channels_max = 2,
  1432. .nid = nvhdmi_master_con_nid_7x,
  1433. .rates = SUPPORTED_RATES,
  1434. .maxbps = SUPPORTED_MAXBPS,
  1435. .formats = SUPPORTED_FORMATS,
  1436. .ops = {
  1437. .open = simple_playback_pcm_open,
  1438. .close = simple_playback_pcm_close,
  1439. .prepare = simple_playback_pcm_prepare
  1440. },
  1441. };
  1442. static const struct hda_codec_ops nvhdmi_patch_ops_8ch_7x = {
  1443. .build_controls = simple_playback_build_controls,
  1444. .build_pcms = simple_playback_build_pcms,
  1445. .init = nvhdmi_7x_init,
  1446. .free = simple_playback_free,
  1447. };
  1448. static const struct hda_codec_ops nvhdmi_patch_ops_2ch = {
  1449. .build_controls = simple_playback_build_controls,
  1450. .build_pcms = simple_playback_build_pcms,
  1451. .init = nvhdmi_7x_init,
  1452. .free = simple_playback_free,
  1453. };
  1454. static int patch_nvhdmi_2ch(struct hda_codec *codec)
  1455. {
  1456. struct hdmi_spec *spec;
  1457. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1458. if (spec == NULL)
  1459. return -ENOMEM;
  1460. codec->spec = spec;
  1461. spec->multiout.num_dacs = 0; /* no analog */
  1462. spec->multiout.max_channels = 2;
  1463. spec->multiout.dig_out_nid = nvhdmi_master_con_nid_7x;
  1464. spec->num_cvts = 1;
  1465. spec->cvts[0].cvt_nid = nvhdmi_master_con_nid_7x;
  1466. spec->pcm_playback = &nvhdmi_pcm_playback_2ch;
  1467. codec->patch_ops = nvhdmi_patch_ops_2ch;
  1468. return 0;
  1469. }
  1470. static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
  1471. {
  1472. struct hdmi_spec *spec;
  1473. int err = patch_nvhdmi_2ch(codec);
  1474. if (err < 0)
  1475. return err;
  1476. spec = codec->spec;
  1477. spec->multiout.max_channels = 8;
  1478. spec->pcm_playback = &nvhdmi_pcm_playback_8ch_7x;
  1479. codec->patch_ops = nvhdmi_patch_ops_8ch_7x;
  1480. /* Initialize the audio infoframe channel mask and checksum to something
  1481. * valid */
  1482. nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
  1483. return 0;
  1484. }
  1485. /*
  1486. * ATI-specific implementations
  1487. *
  1488. * FIXME: we may omit the whole this and use the generic code once after
  1489. * it's confirmed to work.
  1490. */
  1491. #define ATIHDMI_CVT_NID 0x02 /* audio converter */
  1492. #define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
  1493. static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1494. struct hda_codec *codec,
  1495. unsigned int stream_tag,
  1496. unsigned int format,
  1497. struct snd_pcm_substream *substream)
  1498. {
  1499. struct hdmi_spec *spec = codec->spec;
  1500. int chans = substream->runtime->channels;
  1501. int i, err;
  1502. err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
  1503. substream);
  1504. if (err < 0)
  1505. return err;
  1506. snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
  1507. AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
  1508. /* FIXME: XXX */
  1509. for (i = 0; i < chans; i++) {
  1510. snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
  1511. AC_VERB_SET_HDMI_CHAN_SLOT,
  1512. (i << 4) | i);
  1513. }
  1514. return 0;
  1515. }
  1516. static const struct hda_pcm_stream atihdmi_pcm_digital_playback = {
  1517. .substreams = 1,
  1518. .channels_min = 2,
  1519. .channels_max = 2,
  1520. .nid = ATIHDMI_CVT_NID,
  1521. .ops = {
  1522. .open = simple_playback_pcm_open,
  1523. .close = simple_playback_pcm_close,
  1524. .prepare = atihdmi_playback_pcm_prepare
  1525. },
  1526. };
  1527. static const struct hda_verb atihdmi_basic_init[] = {
  1528. /* enable digital output on pin widget */
  1529. { 0x03, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
  1530. {} /* terminator */
  1531. };
  1532. static int atihdmi_init(struct hda_codec *codec)
  1533. {
  1534. struct hdmi_spec *spec = codec->spec;
  1535. snd_hda_sequence_write(codec, atihdmi_basic_init);
  1536. /* SI codec requires to unmute the pin */
  1537. if (get_wcaps(codec, spec->pins[0].pin_nid) & AC_WCAP_OUT_AMP)
  1538. snd_hda_codec_write(codec, spec->pins[0].pin_nid, 0,
  1539. AC_VERB_SET_AMP_GAIN_MUTE,
  1540. AMP_OUT_UNMUTE);
  1541. return 0;
  1542. }
  1543. static const struct hda_codec_ops atihdmi_patch_ops = {
  1544. .build_controls = simple_playback_build_controls,
  1545. .build_pcms = simple_playback_build_pcms,
  1546. .init = atihdmi_init,
  1547. .free = simple_playback_free,
  1548. };
  1549. static int patch_atihdmi(struct hda_codec *codec)
  1550. {
  1551. struct hdmi_spec *spec;
  1552. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1553. if (spec == NULL)
  1554. return -ENOMEM;
  1555. codec->spec = spec;
  1556. spec->multiout.num_dacs = 0; /* no analog */
  1557. spec->multiout.max_channels = 2;
  1558. spec->multiout.dig_out_nid = ATIHDMI_CVT_NID;
  1559. spec->num_cvts = 1;
  1560. spec->cvts[0].cvt_nid = ATIHDMI_CVT_NID;
  1561. spec->pins[0].pin_nid = ATIHDMI_PIN_NID;
  1562. spec->pcm_playback = &atihdmi_pcm_digital_playback;
  1563. codec->patch_ops = atihdmi_patch_ops;
  1564. return 0;
  1565. }
  1566. /*
  1567. * patch entries
  1568. */
  1569. static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
  1570. { .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
  1571. { .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
  1572. { .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
  1573. { .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
  1574. { .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
  1575. { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
  1576. { .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
  1577. { .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1578. { .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1579. { .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1580. { .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1581. { .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
  1582. { .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
  1583. { .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
  1584. { .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
  1585. { .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
  1586. { .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
  1587. { .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
  1588. { .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
  1589. { .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
  1590. { .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
  1591. { .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
  1592. { .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
  1593. /* 17 is known to be absent */
  1594. { .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
  1595. { .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
  1596. { .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
  1597. { .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
  1598. { .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
  1599. { .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
  1600. { .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
  1601. { .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
  1602. { .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
  1603. { .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
  1604. { .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
  1605. { .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
  1606. { .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
  1607. { .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
  1608. { .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
  1609. { .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
  1610. { .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
  1611. { .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
  1612. { .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
  1613. { .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
  1614. {} /* terminator */
  1615. };
  1616. MODULE_ALIAS("snd-hda-codec-id:1002793c");
  1617. MODULE_ALIAS("snd-hda-codec-id:10027919");
  1618. MODULE_ALIAS("snd-hda-codec-id:1002791a");
  1619. MODULE_ALIAS("snd-hda-codec-id:1002aa01");
  1620. MODULE_ALIAS("snd-hda-codec-id:10951390");
  1621. MODULE_ALIAS("snd-hda-codec-id:10951392");
  1622. MODULE_ALIAS("snd-hda-codec-id:10de0002");
  1623. MODULE_ALIAS("snd-hda-codec-id:10de0003");
  1624. MODULE_ALIAS("snd-hda-codec-id:10de0005");
  1625. MODULE_ALIAS("snd-hda-codec-id:10de0006");
  1626. MODULE_ALIAS("snd-hda-codec-id:10de0007");
  1627. MODULE_ALIAS("snd-hda-codec-id:10de000a");
  1628. MODULE_ALIAS("snd-hda-codec-id:10de000b");
  1629. MODULE_ALIAS("snd-hda-codec-id:10de000c");
  1630. MODULE_ALIAS("snd-hda-codec-id:10de000d");
  1631. MODULE_ALIAS("snd-hda-codec-id:10de0010");
  1632. MODULE_ALIAS("snd-hda-codec-id:10de0011");
  1633. MODULE_ALIAS("snd-hda-codec-id:10de0012");
  1634. MODULE_ALIAS("snd-hda-codec-id:10de0013");
  1635. MODULE_ALIAS("snd-hda-codec-id:10de0014");
  1636. MODULE_ALIAS("snd-hda-codec-id:10de0015");
  1637. MODULE_ALIAS("snd-hda-codec-id:10de0016");
  1638. MODULE_ALIAS("snd-hda-codec-id:10de0018");
  1639. MODULE_ALIAS("snd-hda-codec-id:10de0019");
  1640. MODULE_ALIAS("snd-hda-codec-id:10de001a");
  1641. MODULE_ALIAS("snd-hda-codec-id:10de001b");
  1642. MODULE_ALIAS("snd-hda-codec-id:10de001c");
  1643. MODULE_ALIAS("snd-hda-codec-id:10de0040");
  1644. MODULE_ALIAS("snd-hda-codec-id:10de0041");
  1645. MODULE_ALIAS("snd-hda-codec-id:10de0042");
  1646. MODULE_ALIAS("snd-hda-codec-id:10de0043");
  1647. MODULE_ALIAS("snd-hda-codec-id:10de0044");
  1648. MODULE_ALIAS("snd-hda-codec-id:10de0067");
  1649. MODULE_ALIAS("snd-hda-codec-id:10de8001");
  1650. MODULE_ALIAS("snd-hda-codec-id:17e80047");
  1651. MODULE_ALIAS("snd-hda-codec-id:80860054");
  1652. MODULE_ALIAS("snd-hda-codec-id:80862801");
  1653. MODULE_ALIAS("snd-hda-codec-id:80862802");
  1654. MODULE_ALIAS("snd-hda-codec-id:80862803");
  1655. MODULE_ALIAS("snd-hda-codec-id:80862804");
  1656. MODULE_ALIAS("snd-hda-codec-id:80862805");
  1657. MODULE_ALIAS("snd-hda-codec-id:80862806");
  1658. MODULE_ALIAS("snd-hda-codec-id:808629fb");
  1659. MODULE_LICENSE("GPL");
  1660. MODULE_DESCRIPTION("HDMI HD-audio codec");
  1661. MODULE_ALIAS("snd-hda-codec-intelhdmi");
  1662. MODULE_ALIAS("snd-hda-codec-nvhdmi");
  1663. MODULE_ALIAS("snd-hda-codec-atihdmi");
  1664. static struct hda_codec_preset_list intel_list = {
  1665. .preset = snd_hda_preset_hdmi,
  1666. .owner = THIS_MODULE,
  1667. };
  1668. static int __init patch_hdmi_init(void)
  1669. {
  1670. return snd_hda_add_codec_preset(&intel_list);
  1671. }
  1672. static void __exit patch_hdmi_exit(void)
  1673. {
  1674. snd_hda_delete_codec_preset(&intel_list);
  1675. }
  1676. module_init(patch_hdmi_init)
  1677. module_exit(patch_hdmi_exit)