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. #include "hda_jack.h"
  39. static bool static_hdmi_pcm;
  40. module_param(static_hdmi_pcm, bool, 0644);
  41. MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
  42. /*
  43. * The HDMI/DisplayPort configuration can be highly dynamic. A graphics device
  44. * could support N independent pipes, each of them can be connected to one or
  45. * more ports (DVI, HDMI or DisplayPort).
  46. *
  47. * The HDA correspondence of pipes/ports are converter/pin nodes.
  48. */
  49. #define MAX_HDMI_CVTS 4
  50. #define MAX_HDMI_PINS 4
  51. struct hdmi_spec_per_cvt {
  52. hda_nid_t cvt_nid;
  53. int assigned;
  54. unsigned int channels_min;
  55. unsigned int channels_max;
  56. u32 rates;
  57. u64 formats;
  58. unsigned int maxbps;
  59. };
  60. struct hdmi_spec_per_pin {
  61. hda_nid_t pin_nid;
  62. int num_mux_nids;
  63. hda_nid_t mux_nids[HDA_MAX_CONNECTIONS];
  64. struct hda_codec *codec;
  65. struct hdmi_eld sink_eld;
  66. struct delayed_work work;
  67. };
  68. struct hdmi_spec {
  69. int num_cvts;
  70. struct hdmi_spec_per_cvt cvts[MAX_HDMI_CVTS];
  71. int num_pins;
  72. struct hdmi_spec_per_pin pins[MAX_HDMI_PINS];
  73. struct hda_pcm pcm_rec[MAX_HDMI_PINS];
  74. /*
  75. * Non-generic ATI/NVIDIA specific
  76. */
  77. struct hda_multi_out multiout;
  78. const struct hda_pcm_stream *pcm_playback;
  79. };
  80. struct hdmi_audio_infoframe {
  81. u8 type; /* 0x84 */
  82. u8 ver; /* 0x01 */
  83. u8 len; /* 0x0a */
  84. u8 checksum;
  85. u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
  86. u8 SS01_SF24;
  87. u8 CXT04;
  88. u8 CA;
  89. u8 LFEPBL01_LSV36_DM_INH7;
  90. };
  91. struct dp_audio_infoframe {
  92. u8 type; /* 0x84 */
  93. u8 len; /* 0x1b */
  94. u8 ver; /* 0x11 << 2 */
  95. u8 CC02_CT47; /* match with HDMI infoframe from this on */
  96. u8 SS01_SF24;
  97. u8 CXT04;
  98. u8 CA;
  99. u8 LFEPBL01_LSV36_DM_INH7;
  100. };
  101. union audio_infoframe {
  102. struct hdmi_audio_infoframe hdmi;
  103. struct dp_audio_infoframe dp;
  104. u8 bytes[0];
  105. };
  106. /*
  107. * CEA speaker placement:
  108. *
  109. * FLH FCH FRH
  110. * FLW FL FLC FC FRC FR FRW
  111. *
  112. * LFE
  113. * TC
  114. *
  115. * RL RLC RC RRC RR
  116. *
  117. * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
  118. * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
  119. */
  120. enum cea_speaker_placement {
  121. FL = (1 << 0), /* Front Left */
  122. FC = (1 << 1), /* Front Center */
  123. FR = (1 << 2), /* Front Right */
  124. FLC = (1 << 3), /* Front Left Center */
  125. FRC = (1 << 4), /* Front Right Center */
  126. RL = (1 << 5), /* Rear Left */
  127. RC = (1 << 6), /* Rear Center */
  128. RR = (1 << 7), /* Rear Right */
  129. RLC = (1 << 8), /* Rear Left Center */
  130. RRC = (1 << 9), /* Rear Right Center */
  131. LFE = (1 << 10), /* Low Frequency Effect */
  132. FLW = (1 << 11), /* Front Left Wide */
  133. FRW = (1 << 12), /* Front Right Wide */
  134. FLH = (1 << 13), /* Front Left High */
  135. FCH = (1 << 14), /* Front Center High */
  136. FRH = (1 << 15), /* Front Right High */
  137. TC = (1 << 16), /* Top Center */
  138. };
  139. /*
  140. * ELD SA bits in the CEA Speaker Allocation data block
  141. */
  142. static int eld_speaker_allocation_bits[] = {
  143. [0] = FL | FR,
  144. [1] = LFE,
  145. [2] = FC,
  146. [3] = RL | RR,
  147. [4] = RC,
  148. [5] = FLC | FRC,
  149. [6] = RLC | RRC,
  150. /* the following are not defined in ELD yet */
  151. [7] = FLW | FRW,
  152. [8] = FLH | FRH,
  153. [9] = TC,
  154. [10] = FCH,
  155. };
  156. struct cea_channel_speaker_allocation {
  157. int ca_index;
  158. int speakers[8];
  159. /* derived values, just for convenience */
  160. int channels;
  161. int spk_mask;
  162. };
  163. /*
  164. * ALSA sequence is:
  165. *
  166. * surround40 surround41 surround50 surround51 surround71
  167. * ch0 front left = = = =
  168. * ch1 front right = = = =
  169. * ch2 rear left = = = =
  170. * ch3 rear right = = = =
  171. * ch4 LFE center center center
  172. * ch5 LFE LFE
  173. * ch6 side left
  174. * ch7 side right
  175. *
  176. * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
  177. */
  178. static int hdmi_channel_mapping[0x32][8] = {
  179. /* stereo */
  180. [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
  181. /* 2.1 */
  182. [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
  183. /* Dolby Surround */
  184. [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
  185. /* surround40 */
  186. [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
  187. /* 4ch */
  188. [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
  189. /* surround41 */
  190. [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
  191. /* surround50 */
  192. [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
  193. /* surround51 */
  194. [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
  195. /* 7.1 */
  196. [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
  197. };
  198. /*
  199. * This is an ordered list!
  200. *
  201. * The preceding ones have better chances to be selected by
  202. * hdmi_channel_allocation().
  203. */
  204. static struct cea_channel_speaker_allocation channel_allocations[] = {
  205. /* channel: 7 6 5 4 3 2 1 0 */
  206. { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
  207. /* 2.1 */
  208. { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
  209. /* Dolby Surround */
  210. { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
  211. /* surround40 */
  212. { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
  213. /* surround41 */
  214. { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
  215. /* surround50 */
  216. { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
  217. /* surround51 */
  218. { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
  219. /* 6.1 */
  220. { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
  221. /* surround71 */
  222. { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
  223. { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
  224. { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
  225. { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
  226. { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
  227. { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
  228. { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
  229. { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
  230. { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
  231. { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
  232. { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
  233. { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
  234. { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
  235. { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
  236. { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
  237. { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
  238. { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
  239. { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
  240. { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
  241. { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
  242. { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
  243. { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
  244. { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
  245. { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
  246. { .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
  247. { .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
  248. { .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
  249. { .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
  250. { .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
  251. { .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
  252. { .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
  253. { .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
  254. { .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
  255. { .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
  256. { .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
  257. { .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
  258. { .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
  259. { .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
  260. { .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
  261. { .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
  262. { .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
  263. { .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
  264. };
  265. /*
  266. * HDMI routines
  267. */
  268. static int pin_nid_to_pin_index(struct hdmi_spec *spec, hda_nid_t pin_nid)
  269. {
  270. int pin_idx;
  271. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
  272. if (spec->pins[pin_idx].pin_nid == pin_nid)
  273. return pin_idx;
  274. snd_printk(KERN_WARNING "HDMI: pin nid %d not registered\n", pin_nid);
  275. return -EINVAL;
  276. }
  277. static int hinfo_to_pin_index(struct hdmi_spec *spec,
  278. struct hda_pcm_stream *hinfo)
  279. {
  280. int pin_idx;
  281. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
  282. if (&spec->pcm_rec[pin_idx].stream[0] == hinfo)
  283. return pin_idx;
  284. snd_printk(KERN_WARNING "HDMI: hinfo %p not registered\n", hinfo);
  285. return -EINVAL;
  286. }
  287. static int cvt_nid_to_cvt_index(struct hdmi_spec *spec, hda_nid_t cvt_nid)
  288. {
  289. int cvt_idx;
  290. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
  291. if (spec->cvts[cvt_idx].cvt_nid == cvt_nid)
  292. return cvt_idx;
  293. snd_printk(KERN_WARNING "HDMI: cvt nid %d not registered\n", cvt_nid);
  294. return -EINVAL;
  295. }
  296. static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
  297. struct snd_ctl_elem_info *uinfo)
  298. {
  299. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  300. struct hdmi_spec *spec;
  301. int pin_idx;
  302. spec = codec->spec;
  303. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  304. pin_idx = kcontrol->private_value;
  305. uinfo->count = spec->pins[pin_idx].sink_eld.eld_size;
  306. return 0;
  307. }
  308. static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
  309. struct snd_ctl_elem_value *ucontrol)
  310. {
  311. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  312. struct hdmi_spec *spec;
  313. int pin_idx;
  314. spec = codec->spec;
  315. pin_idx = kcontrol->private_value;
  316. memcpy(ucontrol->value.bytes.data,
  317. spec->pins[pin_idx].sink_eld.eld_buffer, ELD_MAX_SIZE);
  318. return 0;
  319. }
  320. static struct snd_kcontrol_new eld_bytes_ctl = {
  321. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  322. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  323. .name = "ELD",
  324. .info = hdmi_eld_ctl_info,
  325. .get = hdmi_eld_ctl_get,
  326. };
  327. static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
  328. int device)
  329. {
  330. struct snd_kcontrol *kctl;
  331. struct hdmi_spec *spec = codec->spec;
  332. int err;
  333. kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
  334. if (!kctl)
  335. return -ENOMEM;
  336. kctl->private_value = pin_idx;
  337. kctl->id.device = device;
  338. err = snd_hda_ctl_add(codec, spec->pins[pin_idx].pin_nid, kctl);
  339. if (err < 0)
  340. return err;
  341. return 0;
  342. }
  343. #ifdef BE_PARANOID
  344. static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
  345. int *packet_index, int *byte_index)
  346. {
  347. int val;
  348. val = snd_hda_codec_read(codec, pin_nid, 0,
  349. AC_VERB_GET_HDMI_DIP_INDEX, 0);
  350. *packet_index = val >> 5;
  351. *byte_index = val & 0x1f;
  352. }
  353. #endif
  354. static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
  355. int packet_index, int byte_index)
  356. {
  357. int val;
  358. val = (packet_index << 5) | (byte_index & 0x1f);
  359. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
  360. }
  361. static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
  362. unsigned char val)
  363. {
  364. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
  365. }
  366. static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  367. {
  368. /* Unmute */
  369. if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
  370. snd_hda_codec_write(codec, pin_nid, 0,
  371. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
  372. /* Disable pin out until stream is active*/
  373. snd_hda_codec_write(codec, pin_nid, 0,
  374. AC_VERB_SET_PIN_WIDGET_CONTROL, 0);
  375. }
  376. static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
  377. {
  378. return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
  379. AC_VERB_GET_CVT_CHAN_COUNT, 0);
  380. }
  381. static void hdmi_set_channel_count(struct hda_codec *codec,
  382. hda_nid_t cvt_nid, int chs)
  383. {
  384. if (chs != hdmi_get_channel_count(codec, cvt_nid))
  385. snd_hda_codec_write(codec, cvt_nid, 0,
  386. AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
  387. }
  388. /*
  389. * Channel mapping routines
  390. */
  391. /*
  392. * Compute derived values in channel_allocations[].
  393. */
  394. static void init_channel_allocations(void)
  395. {
  396. int i, j;
  397. struct cea_channel_speaker_allocation *p;
  398. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  399. p = channel_allocations + i;
  400. p->channels = 0;
  401. p->spk_mask = 0;
  402. for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
  403. if (p->speakers[j]) {
  404. p->channels++;
  405. p->spk_mask |= p->speakers[j];
  406. }
  407. }
  408. }
  409. /*
  410. * The transformation takes two steps:
  411. *
  412. * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
  413. * spk_mask => (channel_allocations[]) => ai->CA
  414. *
  415. * TODO: it could select the wrong CA from multiple candidates.
  416. */
  417. static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
  418. {
  419. int i;
  420. int ca = 0;
  421. int spk_mask = 0;
  422. char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
  423. /*
  424. * CA defaults to 0 for basic stereo audio
  425. */
  426. if (channels <= 2)
  427. return 0;
  428. /*
  429. * expand ELD's speaker allocation mask
  430. *
  431. * ELD tells the speaker mask in a compact(paired) form,
  432. * expand ELD's notions to match the ones used by Audio InfoFrame.
  433. */
  434. for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
  435. if (eld->spk_alloc & (1 << i))
  436. spk_mask |= eld_speaker_allocation_bits[i];
  437. }
  438. /* search for the first working match in the CA table */
  439. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  440. if (channels == channel_allocations[i].channels &&
  441. (spk_mask & channel_allocations[i].spk_mask) ==
  442. channel_allocations[i].spk_mask) {
  443. ca = channel_allocations[i].ca_index;
  444. break;
  445. }
  446. }
  447. snd_print_channel_allocation(eld->spk_alloc, buf, sizeof(buf));
  448. snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
  449. ca, channels, buf);
  450. return ca;
  451. }
  452. static void hdmi_debug_channel_mapping(struct hda_codec *codec,
  453. hda_nid_t pin_nid)
  454. {
  455. #ifdef CONFIG_SND_DEBUG_VERBOSE
  456. int i;
  457. int slot;
  458. for (i = 0; i < 8; i++) {
  459. slot = snd_hda_codec_read(codec, pin_nid, 0,
  460. AC_VERB_GET_HDMI_CHAN_SLOT, i);
  461. printk(KERN_DEBUG "HDMI: ASP channel %d => slot %d\n",
  462. slot >> 4, slot & 0xf);
  463. }
  464. #endif
  465. }
  466. static void hdmi_setup_channel_mapping(struct hda_codec *codec,
  467. hda_nid_t pin_nid,
  468. int ca)
  469. {
  470. int i;
  471. int err;
  472. if (hdmi_channel_mapping[ca][1] == 0) {
  473. for (i = 0; i < channel_allocations[ca].channels; i++)
  474. hdmi_channel_mapping[ca][i] = i | (i << 4);
  475. for (; i < 8; i++)
  476. hdmi_channel_mapping[ca][i] = 0xf | (i << 4);
  477. }
  478. for (i = 0; i < 8; i++) {
  479. err = snd_hda_codec_write(codec, pin_nid, 0,
  480. AC_VERB_SET_HDMI_CHAN_SLOT,
  481. hdmi_channel_mapping[ca][i]);
  482. if (err) {
  483. snd_printdd(KERN_NOTICE
  484. "HDMI: channel mapping failed\n");
  485. break;
  486. }
  487. }
  488. hdmi_debug_channel_mapping(codec, pin_nid);
  489. }
  490. /*
  491. * Audio InfoFrame routines
  492. */
  493. /*
  494. * Enable Audio InfoFrame Transmission
  495. */
  496. static void hdmi_start_infoframe_trans(struct hda_codec *codec,
  497. hda_nid_t pin_nid)
  498. {
  499. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  500. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  501. AC_DIPXMIT_BEST);
  502. }
  503. /*
  504. * Disable Audio InfoFrame Transmission
  505. */
  506. static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
  507. hda_nid_t pin_nid)
  508. {
  509. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  510. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  511. AC_DIPXMIT_DISABLE);
  512. }
  513. static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
  514. {
  515. #ifdef CONFIG_SND_DEBUG_VERBOSE
  516. int i;
  517. int size;
  518. size = snd_hdmi_get_eld_size(codec, pin_nid);
  519. printk(KERN_DEBUG "HDMI: ELD buf size is %d\n", size);
  520. for (i = 0; i < 8; i++) {
  521. size = snd_hda_codec_read(codec, pin_nid, 0,
  522. AC_VERB_GET_HDMI_DIP_SIZE, i);
  523. printk(KERN_DEBUG "HDMI: DIP GP[%d] buf size is %d\n", i, size);
  524. }
  525. #endif
  526. }
  527. static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
  528. {
  529. #ifdef BE_PARANOID
  530. int i, j;
  531. int size;
  532. int pi, bi;
  533. for (i = 0; i < 8; i++) {
  534. size = snd_hda_codec_read(codec, pin_nid, 0,
  535. AC_VERB_GET_HDMI_DIP_SIZE, i);
  536. if (size == 0)
  537. continue;
  538. hdmi_set_dip_index(codec, pin_nid, i, 0x0);
  539. for (j = 1; j < 1000; j++) {
  540. hdmi_write_dip_byte(codec, pin_nid, 0x0);
  541. hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
  542. if (pi != i)
  543. snd_printd(KERN_INFO "dip index %d: %d != %d\n",
  544. bi, pi, i);
  545. if (bi == 0) /* byte index wrapped around */
  546. break;
  547. }
  548. snd_printd(KERN_INFO
  549. "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
  550. i, size, j);
  551. }
  552. #endif
  553. }
  554. static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
  555. {
  556. u8 *bytes = (u8 *)hdmi_ai;
  557. u8 sum = 0;
  558. int i;
  559. hdmi_ai->checksum = 0;
  560. for (i = 0; i < sizeof(*hdmi_ai); i++)
  561. sum += bytes[i];
  562. hdmi_ai->checksum = -sum;
  563. }
  564. static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
  565. hda_nid_t pin_nid,
  566. u8 *dip, int size)
  567. {
  568. int i;
  569. hdmi_debug_dip_size(codec, pin_nid);
  570. hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
  571. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  572. for (i = 0; i < size; i++)
  573. hdmi_write_dip_byte(codec, pin_nid, dip[i]);
  574. }
  575. static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
  576. u8 *dip, int size)
  577. {
  578. u8 val;
  579. int i;
  580. if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
  581. != AC_DIPXMIT_BEST)
  582. return false;
  583. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  584. for (i = 0; i < size; i++) {
  585. val = snd_hda_codec_read(codec, pin_nid, 0,
  586. AC_VERB_GET_HDMI_DIP_DATA, 0);
  587. if (val != dip[i])
  588. return false;
  589. }
  590. return true;
  591. }
  592. static void hdmi_setup_audio_infoframe(struct hda_codec *codec, int pin_idx,
  593. struct snd_pcm_substream *substream)
  594. {
  595. struct hdmi_spec *spec = codec->spec;
  596. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  597. hda_nid_t pin_nid = per_pin->pin_nid;
  598. int channels = substream->runtime->channels;
  599. struct hdmi_eld *eld;
  600. int ca;
  601. union audio_infoframe ai;
  602. eld = &spec->pins[pin_idx].sink_eld;
  603. if (!eld->monitor_present)
  604. return;
  605. ca = hdmi_channel_allocation(eld, channels);
  606. memset(&ai, 0, sizeof(ai));
  607. if (eld->conn_type == 0) { /* HDMI */
  608. struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
  609. hdmi_ai->type = 0x84;
  610. hdmi_ai->ver = 0x01;
  611. hdmi_ai->len = 0x0a;
  612. hdmi_ai->CC02_CT47 = channels - 1;
  613. hdmi_ai->CA = ca;
  614. hdmi_checksum_audio_infoframe(hdmi_ai);
  615. } else if (eld->conn_type == 1) { /* DisplayPort */
  616. struct dp_audio_infoframe *dp_ai = &ai.dp;
  617. dp_ai->type = 0x84;
  618. dp_ai->len = 0x1b;
  619. dp_ai->ver = 0x11 << 2;
  620. dp_ai->CC02_CT47 = channels - 1;
  621. dp_ai->CA = ca;
  622. } else {
  623. snd_printd("HDMI: unknown connection type at pin %d\n",
  624. pin_nid);
  625. return;
  626. }
  627. /*
  628. * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
  629. * sizeof(*dp_ai) to avoid partial match/update problems when
  630. * the user switches between HDMI/DP monitors.
  631. */
  632. if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
  633. sizeof(ai))) {
  634. snd_printdd("hdmi_setup_audio_infoframe: "
  635. "pin=%d channels=%d\n",
  636. pin_nid,
  637. channels);
  638. hdmi_setup_channel_mapping(codec, pin_nid, ca);
  639. hdmi_stop_infoframe_trans(codec, pin_nid);
  640. hdmi_fill_audio_infoframe(codec, pin_nid,
  641. ai.bytes, sizeof(ai));
  642. hdmi_start_infoframe_trans(codec, pin_nid);
  643. }
  644. }
  645. /*
  646. * Unsolicited events
  647. */
  648. static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, bool retry);
  649. static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
  650. {
  651. struct hdmi_spec *spec = codec->spec;
  652. int pin_nid = res >> AC_UNSOL_RES_TAG_SHIFT;
  653. int pd = !!(res & AC_UNSOL_RES_PD);
  654. int eldv = !!(res & AC_UNSOL_RES_ELDV);
  655. int pin_idx;
  656. printk(KERN_INFO
  657. "HDMI hot plug event: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  658. codec->addr, pin_nid, pd, eldv);
  659. pin_idx = pin_nid_to_pin_index(spec, pin_nid);
  660. if (pin_idx < 0)
  661. return;
  662. snd_hda_jack_set_dirty(codec, pin_nid);
  663. hdmi_present_sense(&spec->pins[pin_idx], true);
  664. }
  665. static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
  666. {
  667. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  668. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  669. int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
  670. int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
  671. printk(KERN_INFO
  672. "HDMI CP event: CODEC=%d PIN=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
  673. codec->addr,
  674. tag,
  675. subtag,
  676. cp_state,
  677. cp_ready);
  678. /* TODO */
  679. if (cp_state)
  680. ;
  681. if (cp_ready)
  682. ;
  683. }
  684. static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
  685. {
  686. struct hdmi_spec *spec = codec->spec;
  687. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  688. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  689. if (pin_nid_to_pin_index(spec, tag) < 0) {
  690. snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
  691. return;
  692. }
  693. if (subtag == 0)
  694. hdmi_intrinsic_event(codec, res);
  695. else
  696. hdmi_non_intrinsic_event(codec, res);
  697. }
  698. /*
  699. * Callbacks
  700. */
  701. /* HBR should be Non-PCM, 8 channels */
  702. #define is_hbr_format(format) \
  703. ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
  704. static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
  705. hda_nid_t pin_nid, u32 stream_tag, int format)
  706. {
  707. int pinctl;
  708. int new_pinctl = 0;
  709. if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
  710. pinctl = snd_hda_codec_read(codec, pin_nid, 0,
  711. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  712. new_pinctl = pinctl & ~AC_PINCTL_EPT;
  713. if (is_hbr_format(format))
  714. new_pinctl |= AC_PINCTL_EPT_HBR;
  715. else
  716. new_pinctl |= AC_PINCTL_EPT_NATIVE;
  717. snd_printdd("hdmi_setup_stream: "
  718. "NID=0x%x, %spinctl=0x%x\n",
  719. pin_nid,
  720. pinctl == new_pinctl ? "" : "new-",
  721. new_pinctl);
  722. if (pinctl != new_pinctl)
  723. snd_hda_codec_write(codec, pin_nid, 0,
  724. AC_VERB_SET_PIN_WIDGET_CONTROL,
  725. new_pinctl);
  726. }
  727. if (is_hbr_format(format) && !new_pinctl) {
  728. snd_printdd("hdmi_setup_stream: HBR is not supported\n");
  729. return -EINVAL;
  730. }
  731. snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
  732. return 0;
  733. }
  734. /*
  735. * HDA PCM callbacks
  736. */
  737. static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
  738. struct hda_codec *codec,
  739. struct snd_pcm_substream *substream)
  740. {
  741. struct hdmi_spec *spec = codec->spec;
  742. struct snd_pcm_runtime *runtime = substream->runtime;
  743. int pin_idx, cvt_idx, mux_idx = 0;
  744. struct hdmi_spec_per_pin *per_pin;
  745. struct hdmi_eld *eld;
  746. struct hdmi_spec_per_cvt *per_cvt = NULL;
  747. int pinctl;
  748. /* Validate hinfo */
  749. pin_idx = hinfo_to_pin_index(spec, hinfo);
  750. if (snd_BUG_ON(pin_idx < 0))
  751. return -EINVAL;
  752. per_pin = &spec->pins[pin_idx];
  753. eld = &per_pin->sink_eld;
  754. /* Dynamically assign converter to stream */
  755. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
  756. per_cvt = &spec->cvts[cvt_idx];
  757. /* Must not already be assigned */
  758. if (per_cvt->assigned)
  759. continue;
  760. /* Must be in pin's mux's list of converters */
  761. for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
  762. if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
  763. break;
  764. /* Not in mux list */
  765. if (mux_idx == per_pin->num_mux_nids)
  766. continue;
  767. break;
  768. }
  769. /* No free converters */
  770. if (cvt_idx == spec->num_cvts)
  771. return -ENODEV;
  772. /* Claim converter */
  773. per_cvt->assigned = 1;
  774. hinfo->nid = per_cvt->cvt_nid;
  775. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  776. AC_VERB_SET_CONNECT_SEL,
  777. mux_idx);
  778. pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
  779. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  780. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  781. AC_VERB_SET_PIN_WIDGET_CONTROL,
  782. pinctl | PIN_OUT);
  783. snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
  784. /* Initially set the converter's capabilities */
  785. hinfo->channels_min = per_cvt->channels_min;
  786. hinfo->channels_max = per_cvt->channels_max;
  787. hinfo->rates = per_cvt->rates;
  788. hinfo->formats = per_cvt->formats;
  789. hinfo->maxbps = per_cvt->maxbps;
  790. /* Restrict capabilities by ELD if this isn't disabled */
  791. if (!static_hdmi_pcm && eld->eld_valid) {
  792. snd_hdmi_eld_update_pcm_info(eld, hinfo);
  793. if (hinfo->channels_min > hinfo->channels_max ||
  794. !hinfo->rates || !hinfo->formats)
  795. return -ENODEV;
  796. }
  797. /* Store the updated parameters */
  798. runtime->hw.channels_min = hinfo->channels_min;
  799. runtime->hw.channels_max = hinfo->channels_max;
  800. runtime->hw.formats = hinfo->formats;
  801. runtime->hw.rates = hinfo->rates;
  802. snd_pcm_hw_constraint_step(substream->runtime, 0,
  803. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  804. return 0;
  805. }
  806. /*
  807. * HDA/HDMI auto parsing
  808. */
  809. static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
  810. {
  811. struct hdmi_spec *spec = codec->spec;
  812. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  813. hda_nid_t pin_nid = per_pin->pin_nid;
  814. if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
  815. snd_printk(KERN_WARNING
  816. "HDMI: pin %d wcaps %#x "
  817. "does not support connection list\n",
  818. pin_nid, get_wcaps(codec, pin_nid));
  819. return -EINVAL;
  820. }
  821. per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
  822. per_pin->mux_nids,
  823. HDA_MAX_CONNECTIONS);
  824. return 0;
  825. }
  826. static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, bool retry)
  827. {
  828. struct hda_codec *codec = per_pin->codec;
  829. struct hdmi_eld *eld = &per_pin->sink_eld;
  830. hda_nid_t pin_nid = per_pin->pin_nid;
  831. /*
  832. * Always execute a GetPinSense verb here, even when called from
  833. * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
  834. * response's PD bit is not the real PD value, but indicates that
  835. * the real PD value changed. An older version of the HD-audio
  836. * specification worked this way. Hence, we just ignore the data in
  837. * the unsolicited response to avoid custom WARs.
  838. */
  839. int present = snd_hda_pin_sense(codec, pin_nid);
  840. bool eld_valid = false;
  841. memset(eld, 0, offsetof(struct hdmi_eld, eld_buffer));
  842. eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
  843. if (eld->monitor_present)
  844. eld_valid = !!(present & AC_PINSENSE_ELDV);
  845. printk(KERN_INFO
  846. "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  847. codec->addr, pin_nid, eld->monitor_present, eld_valid);
  848. if (eld_valid) {
  849. if (!snd_hdmi_get_eld(eld, codec, pin_nid))
  850. snd_hdmi_show_eld(eld);
  851. else if (retry) {
  852. queue_delayed_work(codec->bus->workq,
  853. &per_pin->work,
  854. msecs_to_jiffies(300));
  855. }
  856. }
  857. snd_hda_input_jack_report(codec, pin_nid);
  858. }
  859. static void hdmi_repoll_eld(struct work_struct *work)
  860. {
  861. struct hdmi_spec_per_pin *per_pin =
  862. container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
  863. hdmi_present_sense(per_pin, false);
  864. }
  865. static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  866. {
  867. struct hdmi_spec *spec = codec->spec;
  868. unsigned int caps, config;
  869. int pin_idx;
  870. struct hdmi_spec_per_pin *per_pin;
  871. int err;
  872. caps = snd_hda_param_read(codec, pin_nid, AC_PAR_PIN_CAP);
  873. if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
  874. return 0;
  875. config = snd_hda_codec_read(codec, pin_nid, 0,
  876. AC_VERB_GET_CONFIG_DEFAULT, 0);
  877. if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
  878. return 0;
  879. if (snd_BUG_ON(spec->num_pins >= MAX_HDMI_PINS))
  880. return -E2BIG;
  881. pin_idx = spec->num_pins;
  882. per_pin = &spec->pins[pin_idx];
  883. per_pin->pin_nid = pin_nid;
  884. err = hdmi_read_pin_conn(codec, pin_idx);
  885. if (err < 0)
  886. return err;
  887. spec->num_pins++;
  888. return 0;
  889. }
  890. static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
  891. {
  892. struct hdmi_spec *spec = codec->spec;
  893. int cvt_idx;
  894. struct hdmi_spec_per_cvt *per_cvt;
  895. unsigned int chans;
  896. int err;
  897. if (snd_BUG_ON(spec->num_cvts >= MAX_HDMI_CVTS))
  898. return -E2BIG;
  899. chans = get_wcaps(codec, cvt_nid);
  900. chans = get_wcaps_channels(chans);
  901. cvt_idx = spec->num_cvts;
  902. per_cvt = &spec->cvts[cvt_idx];
  903. per_cvt->cvt_nid = cvt_nid;
  904. per_cvt->channels_min = 2;
  905. if (chans <= 16)
  906. per_cvt->channels_max = chans;
  907. err = snd_hda_query_supported_pcm(codec, cvt_nid,
  908. &per_cvt->rates,
  909. &per_cvt->formats,
  910. &per_cvt->maxbps);
  911. if (err < 0)
  912. return err;
  913. spec->num_cvts++;
  914. return 0;
  915. }
  916. static int hdmi_parse_codec(struct hda_codec *codec)
  917. {
  918. hda_nid_t nid;
  919. int i, nodes;
  920. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
  921. if (!nid || nodes < 0) {
  922. snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
  923. return -EINVAL;
  924. }
  925. for (i = 0; i < nodes; i++, nid++) {
  926. unsigned int caps;
  927. unsigned int type;
  928. caps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
  929. type = get_wcaps_type(caps);
  930. if (!(caps & AC_WCAP_DIGITAL))
  931. continue;
  932. switch (type) {
  933. case AC_WID_AUD_OUT:
  934. hdmi_add_cvt(codec, nid);
  935. break;
  936. case AC_WID_PIN:
  937. hdmi_add_pin(codec, nid);
  938. break;
  939. }
  940. }
  941. /*
  942. * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
  943. * can be lost and presence sense verb will become inaccurate if the
  944. * HDA link is powered off at hot plug or hw initialization time.
  945. */
  946. #ifdef CONFIG_SND_HDA_POWER_SAVE
  947. if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
  948. AC_PWRST_EPSS))
  949. codec->bus->power_keep_link_on = 1;
  950. #endif
  951. return 0;
  952. }
  953. /*
  954. */
  955. static char *generic_hdmi_pcm_names[MAX_HDMI_PINS] = {
  956. "HDMI 0",
  957. "HDMI 1",
  958. "HDMI 2",
  959. "HDMI 3",
  960. };
  961. /*
  962. * HDMI callbacks
  963. */
  964. static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  965. struct hda_codec *codec,
  966. unsigned int stream_tag,
  967. unsigned int format,
  968. struct snd_pcm_substream *substream)
  969. {
  970. hda_nid_t cvt_nid = hinfo->nid;
  971. struct hdmi_spec *spec = codec->spec;
  972. int pin_idx = hinfo_to_pin_index(spec, hinfo);
  973. hda_nid_t pin_nid = spec->pins[pin_idx].pin_nid;
  974. hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
  975. hdmi_setup_audio_infoframe(codec, pin_idx, substream);
  976. return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
  977. }
  978. static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  979. struct hda_codec *codec,
  980. struct snd_pcm_substream *substream)
  981. {
  982. struct hdmi_spec *spec = codec->spec;
  983. int cvt_idx, pin_idx;
  984. struct hdmi_spec_per_cvt *per_cvt;
  985. struct hdmi_spec_per_pin *per_pin;
  986. int pinctl;
  987. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  988. if (hinfo->nid) {
  989. cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
  990. if (snd_BUG_ON(cvt_idx < 0))
  991. return -EINVAL;
  992. per_cvt = &spec->cvts[cvt_idx];
  993. snd_BUG_ON(!per_cvt->assigned);
  994. per_cvt->assigned = 0;
  995. hinfo->nid = 0;
  996. pin_idx = hinfo_to_pin_index(spec, hinfo);
  997. if (snd_BUG_ON(pin_idx < 0))
  998. return -EINVAL;
  999. per_pin = &spec->pins[pin_idx];
  1000. pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
  1001. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1002. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  1003. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1004. pinctl & ~PIN_OUT);
  1005. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1006. }
  1007. return 0;
  1008. }
  1009. static const struct hda_pcm_ops generic_ops = {
  1010. .open = hdmi_pcm_open,
  1011. .prepare = generic_hdmi_playback_pcm_prepare,
  1012. .cleanup = generic_hdmi_playback_pcm_cleanup,
  1013. };
  1014. static int generic_hdmi_build_pcms(struct hda_codec *codec)
  1015. {
  1016. struct hdmi_spec *spec = codec->spec;
  1017. int pin_idx;
  1018. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1019. struct hda_pcm *info;
  1020. struct hda_pcm_stream *pstr;
  1021. info = &spec->pcm_rec[pin_idx];
  1022. info->name = generic_hdmi_pcm_names[pin_idx];
  1023. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1024. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1025. pstr->substreams = 1;
  1026. pstr->ops = generic_ops;
  1027. /* other pstr fields are set in open */
  1028. }
  1029. codec->num_pcms = spec->num_pins;
  1030. codec->pcm_info = spec->pcm_rec;
  1031. return 0;
  1032. }
  1033. static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
  1034. {
  1035. int err;
  1036. char hdmi_str[32];
  1037. struct hdmi_spec *spec = codec->spec;
  1038. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1039. int pcmdev = spec->pcm_rec[pin_idx].device;
  1040. snprintf(hdmi_str, sizeof(hdmi_str), "HDMI/DP,pcm=%d", pcmdev);
  1041. err = snd_hda_input_jack_add(codec, per_pin->pin_nid,
  1042. SND_JACK_VIDEOOUT, pcmdev > 0 ? hdmi_str : NULL);
  1043. if (err < 0)
  1044. return err;
  1045. hdmi_present_sense(per_pin, false);
  1046. return 0;
  1047. }
  1048. static int generic_hdmi_build_controls(struct hda_codec *codec)
  1049. {
  1050. struct hdmi_spec *spec = codec->spec;
  1051. int err;
  1052. int pin_idx;
  1053. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1054. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1055. err = generic_hdmi_build_jack(codec, pin_idx);
  1056. if (err < 0)
  1057. return err;
  1058. err = snd_hda_create_spdif_out_ctls(codec,
  1059. per_pin->pin_nid,
  1060. per_pin->mux_nids[0]);
  1061. if (err < 0)
  1062. return err;
  1063. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1064. /* add control for ELD Bytes */
  1065. err = hdmi_create_eld_ctl(codec,
  1066. pin_idx,
  1067. spec->pcm_rec[pin_idx].device);
  1068. if (err < 0)
  1069. return err;
  1070. }
  1071. return 0;
  1072. }
  1073. static int generic_hdmi_init(struct hda_codec *codec)
  1074. {
  1075. struct hdmi_spec *spec = codec->spec;
  1076. int pin_idx;
  1077. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1078. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1079. hda_nid_t pin_nid = per_pin->pin_nid;
  1080. struct hdmi_eld *eld = &per_pin->sink_eld;
  1081. hdmi_init_pin(codec, pin_nid);
  1082. snd_hda_jack_detect_enable(codec, pin_nid, 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)