patch_hdmi.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  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. snd_hda_jack_report_sync(codec);
  665. }
  666. static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
  667. {
  668. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  669. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  670. int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
  671. int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
  672. printk(KERN_INFO
  673. "HDMI CP event: CODEC=%d PIN=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
  674. codec->addr,
  675. tag,
  676. subtag,
  677. cp_state,
  678. cp_ready);
  679. /* TODO */
  680. if (cp_state)
  681. ;
  682. if (cp_ready)
  683. ;
  684. }
  685. static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
  686. {
  687. struct hdmi_spec *spec = codec->spec;
  688. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  689. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  690. if (pin_nid_to_pin_index(spec, tag) < 0) {
  691. snd_printd(KERN_INFO "Unexpected HDMI event tag 0x%x\n", tag);
  692. return;
  693. }
  694. if (subtag == 0)
  695. hdmi_intrinsic_event(codec, res);
  696. else
  697. hdmi_non_intrinsic_event(codec, res);
  698. }
  699. /*
  700. * Callbacks
  701. */
  702. /* HBR should be Non-PCM, 8 channels */
  703. #define is_hbr_format(format) \
  704. ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
  705. static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
  706. hda_nid_t pin_nid, u32 stream_tag, int format)
  707. {
  708. int pinctl;
  709. int new_pinctl = 0;
  710. if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
  711. pinctl = snd_hda_codec_read(codec, pin_nid, 0,
  712. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  713. new_pinctl = pinctl & ~AC_PINCTL_EPT;
  714. if (is_hbr_format(format))
  715. new_pinctl |= AC_PINCTL_EPT_HBR;
  716. else
  717. new_pinctl |= AC_PINCTL_EPT_NATIVE;
  718. snd_printdd("hdmi_setup_stream: "
  719. "NID=0x%x, %spinctl=0x%x\n",
  720. pin_nid,
  721. pinctl == new_pinctl ? "" : "new-",
  722. new_pinctl);
  723. if (pinctl != new_pinctl)
  724. snd_hda_codec_write(codec, pin_nid, 0,
  725. AC_VERB_SET_PIN_WIDGET_CONTROL,
  726. new_pinctl);
  727. }
  728. if (is_hbr_format(format) && !new_pinctl) {
  729. snd_printdd("hdmi_setup_stream: HBR is not supported\n");
  730. return -EINVAL;
  731. }
  732. snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
  733. return 0;
  734. }
  735. /*
  736. * HDA PCM callbacks
  737. */
  738. static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
  739. struct hda_codec *codec,
  740. struct snd_pcm_substream *substream)
  741. {
  742. struct hdmi_spec *spec = codec->spec;
  743. struct snd_pcm_runtime *runtime = substream->runtime;
  744. int pin_idx, cvt_idx, mux_idx = 0;
  745. struct hdmi_spec_per_pin *per_pin;
  746. struct hdmi_eld *eld;
  747. struct hdmi_spec_per_cvt *per_cvt = NULL;
  748. int pinctl;
  749. /* Validate hinfo */
  750. pin_idx = hinfo_to_pin_index(spec, hinfo);
  751. if (snd_BUG_ON(pin_idx < 0))
  752. return -EINVAL;
  753. per_pin = &spec->pins[pin_idx];
  754. eld = &per_pin->sink_eld;
  755. /* Dynamically assign converter to stream */
  756. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
  757. per_cvt = &spec->cvts[cvt_idx];
  758. /* Must not already be assigned */
  759. if (per_cvt->assigned)
  760. continue;
  761. /* Must be in pin's mux's list of converters */
  762. for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
  763. if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
  764. break;
  765. /* Not in mux list */
  766. if (mux_idx == per_pin->num_mux_nids)
  767. continue;
  768. break;
  769. }
  770. /* No free converters */
  771. if (cvt_idx == spec->num_cvts)
  772. return -ENODEV;
  773. /* Claim converter */
  774. per_cvt->assigned = 1;
  775. hinfo->nid = per_cvt->cvt_nid;
  776. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  777. AC_VERB_SET_CONNECT_SEL,
  778. mux_idx);
  779. pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
  780. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  781. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  782. AC_VERB_SET_PIN_WIDGET_CONTROL,
  783. pinctl | PIN_OUT);
  784. snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
  785. /* Initially set the converter's capabilities */
  786. hinfo->channels_min = per_cvt->channels_min;
  787. hinfo->channels_max = per_cvt->channels_max;
  788. hinfo->rates = per_cvt->rates;
  789. hinfo->formats = per_cvt->formats;
  790. hinfo->maxbps = per_cvt->maxbps;
  791. /* Restrict capabilities by ELD if this isn't disabled */
  792. if (!static_hdmi_pcm && eld->eld_valid) {
  793. snd_hdmi_eld_update_pcm_info(eld, hinfo);
  794. if (hinfo->channels_min > hinfo->channels_max ||
  795. !hinfo->rates || !hinfo->formats)
  796. return -ENODEV;
  797. }
  798. /* Store the updated parameters */
  799. runtime->hw.channels_min = hinfo->channels_min;
  800. runtime->hw.channels_max = hinfo->channels_max;
  801. runtime->hw.formats = hinfo->formats;
  802. runtime->hw.rates = hinfo->rates;
  803. snd_pcm_hw_constraint_step(substream->runtime, 0,
  804. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  805. return 0;
  806. }
  807. /*
  808. * HDA/HDMI auto parsing
  809. */
  810. static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
  811. {
  812. struct hdmi_spec *spec = codec->spec;
  813. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  814. hda_nid_t pin_nid = per_pin->pin_nid;
  815. if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
  816. snd_printk(KERN_WARNING
  817. "HDMI: pin %d wcaps %#x "
  818. "does not support connection list\n",
  819. pin_nid, get_wcaps(codec, pin_nid));
  820. return -EINVAL;
  821. }
  822. per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
  823. per_pin->mux_nids,
  824. HDA_MAX_CONNECTIONS);
  825. return 0;
  826. }
  827. static void hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, bool retry)
  828. {
  829. struct hda_codec *codec = per_pin->codec;
  830. struct hdmi_eld *eld = &per_pin->sink_eld;
  831. hda_nid_t pin_nid = per_pin->pin_nid;
  832. /*
  833. * Always execute a GetPinSense verb here, even when called from
  834. * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
  835. * response's PD bit is not the real PD value, but indicates that
  836. * the real PD value changed. An older version of the HD-audio
  837. * specification worked this way. Hence, we just ignore the data in
  838. * the unsolicited response to avoid custom WARs.
  839. */
  840. int present = snd_hda_pin_sense(codec, pin_nid);
  841. bool eld_valid = false;
  842. memset(eld, 0, offsetof(struct hdmi_eld, eld_buffer));
  843. eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
  844. if (eld->monitor_present)
  845. eld_valid = !!(present & AC_PINSENSE_ELDV);
  846. printk(KERN_INFO
  847. "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  848. codec->addr, pin_nid, eld->monitor_present, eld_valid);
  849. if (eld_valid) {
  850. if (!snd_hdmi_get_eld(eld, codec, pin_nid))
  851. snd_hdmi_show_eld(eld);
  852. else if (retry) {
  853. queue_delayed_work(codec->bus->workq,
  854. &per_pin->work,
  855. msecs_to_jiffies(300));
  856. }
  857. }
  858. snd_hda_input_jack_report(codec, pin_nid);
  859. }
  860. static void hdmi_repoll_eld(struct work_struct *work)
  861. {
  862. struct hdmi_spec_per_pin *per_pin =
  863. container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
  864. hdmi_present_sense(per_pin, false);
  865. }
  866. static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  867. {
  868. struct hdmi_spec *spec = codec->spec;
  869. unsigned int caps, config;
  870. int pin_idx;
  871. struct hdmi_spec_per_pin *per_pin;
  872. int err;
  873. caps = snd_hda_param_read(codec, pin_nid, AC_PAR_PIN_CAP);
  874. if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
  875. return 0;
  876. config = snd_hda_codec_read(codec, pin_nid, 0,
  877. AC_VERB_GET_CONFIG_DEFAULT, 0);
  878. if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
  879. return 0;
  880. if (snd_BUG_ON(spec->num_pins >= MAX_HDMI_PINS))
  881. return -E2BIG;
  882. pin_idx = spec->num_pins;
  883. per_pin = &spec->pins[pin_idx];
  884. per_pin->pin_nid = pin_nid;
  885. err = hdmi_read_pin_conn(codec, pin_idx);
  886. if (err < 0)
  887. return err;
  888. spec->num_pins++;
  889. return 0;
  890. }
  891. static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
  892. {
  893. struct hdmi_spec *spec = codec->spec;
  894. int cvt_idx;
  895. struct hdmi_spec_per_cvt *per_cvt;
  896. unsigned int chans;
  897. int err;
  898. if (snd_BUG_ON(spec->num_cvts >= MAX_HDMI_CVTS))
  899. return -E2BIG;
  900. chans = get_wcaps(codec, cvt_nid);
  901. chans = get_wcaps_channels(chans);
  902. cvt_idx = spec->num_cvts;
  903. per_cvt = &spec->cvts[cvt_idx];
  904. per_cvt->cvt_nid = cvt_nid;
  905. per_cvt->channels_min = 2;
  906. if (chans <= 16)
  907. per_cvt->channels_max = chans;
  908. err = snd_hda_query_supported_pcm(codec, cvt_nid,
  909. &per_cvt->rates,
  910. &per_cvt->formats,
  911. &per_cvt->maxbps);
  912. if (err < 0)
  913. return err;
  914. spec->num_cvts++;
  915. return 0;
  916. }
  917. static int hdmi_parse_codec(struct hda_codec *codec)
  918. {
  919. hda_nid_t nid;
  920. int i, nodes;
  921. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
  922. if (!nid || nodes < 0) {
  923. snd_printk(KERN_WARNING "HDMI: failed to get afg sub nodes\n");
  924. return -EINVAL;
  925. }
  926. for (i = 0; i < nodes; i++, nid++) {
  927. unsigned int caps;
  928. unsigned int type;
  929. caps = snd_hda_param_read(codec, nid, AC_PAR_AUDIO_WIDGET_CAP);
  930. type = get_wcaps_type(caps);
  931. if (!(caps & AC_WCAP_DIGITAL))
  932. continue;
  933. switch (type) {
  934. case AC_WID_AUD_OUT:
  935. hdmi_add_cvt(codec, nid);
  936. break;
  937. case AC_WID_PIN:
  938. hdmi_add_pin(codec, nid);
  939. break;
  940. }
  941. }
  942. /*
  943. * G45/IbexPeak don't support EPSS: the unsolicited pin hot plug event
  944. * can be lost and presence sense verb will become inaccurate if the
  945. * HDA link is powered off at hot plug or hw initialization time.
  946. */
  947. #ifdef CONFIG_SND_HDA_POWER_SAVE
  948. if (!(snd_hda_param_read(codec, codec->afg, AC_PAR_POWER_STATE) &
  949. AC_PWRST_EPSS))
  950. codec->bus->power_keep_link_on = 1;
  951. #endif
  952. return 0;
  953. }
  954. /*
  955. */
  956. static char *generic_hdmi_pcm_names[MAX_HDMI_PINS] = {
  957. "HDMI 0",
  958. "HDMI 1",
  959. "HDMI 2",
  960. "HDMI 3",
  961. };
  962. /*
  963. * HDMI callbacks
  964. */
  965. static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  966. struct hda_codec *codec,
  967. unsigned int stream_tag,
  968. unsigned int format,
  969. struct snd_pcm_substream *substream)
  970. {
  971. hda_nid_t cvt_nid = hinfo->nid;
  972. struct hdmi_spec *spec = codec->spec;
  973. int pin_idx = hinfo_to_pin_index(spec, hinfo);
  974. hda_nid_t pin_nid = spec->pins[pin_idx].pin_nid;
  975. hdmi_set_channel_count(codec, cvt_nid, substream->runtime->channels);
  976. hdmi_setup_audio_infoframe(codec, pin_idx, substream);
  977. return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
  978. }
  979. static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  980. struct hda_codec *codec,
  981. struct snd_pcm_substream *substream)
  982. {
  983. struct hdmi_spec *spec = codec->spec;
  984. int cvt_idx, pin_idx;
  985. struct hdmi_spec_per_cvt *per_cvt;
  986. struct hdmi_spec_per_pin *per_pin;
  987. int pinctl;
  988. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  989. if (hinfo->nid) {
  990. cvt_idx = cvt_nid_to_cvt_index(spec, hinfo->nid);
  991. if (snd_BUG_ON(cvt_idx < 0))
  992. return -EINVAL;
  993. per_cvt = &spec->cvts[cvt_idx];
  994. snd_BUG_ON(!per_cvt->assigned);
  995. per_cvt->assigned = 0;
  996. hinfo->nid = 0;
  997. pin_idx = hinfo_to_pin_index(spec, hinfo);
  998. if (snd_BUG_ON(pin_idx < 0))
  999. return -EINVAL;
  1000. per_pin = &spec->pins[pin_idx];
  1001. pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
  1002. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1003. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  1004. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1005. pinctl & ~PIN_OUT);
  1006. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1007. }
  1008. return 0;
  1009. }
  1010. static const struct hda_pcm_ops generic_ops = {
  1011. .open = hdmi_pcm_open,
  1012. .prepare = generic_hdmi_playback_pcm_prepare,
  1013. .cleanup = generic_hdmi_playback_pcm_cleanup,
  1014. };
  1015. static int generic_hdmi_build_pcms(struct hda_codec *codec)
  1016. {
  1017. struct hdmi_spec *spec = codec->spec;
  1018. int pin_idx;
  1019. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1020. struct hda_pcm *info;
  1021. struct hda_pcm_stream *pstr;
  1022. info = &spec->pcm_rec[pin_idx];
  1023. info->name = generic_hdmi_pcm_names[pin_idx];
  1024. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1025. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1026. pstr->substreams = 1;
  1027. pstr->ops = generic_ops;
  1028. /* other pstr fields are set in open */
  1029. }
  1030. codec->num_pcms = spec->num_pins;
  1031. codec->pcm_info = spec->pcm_rec;
  1032. return 0;
  1033. }
  1034. static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
  1035. {
  1036. int err;
  1037. char hdmi_str[32];
  1038. struct hdmi_spec *spec = codec->spec;
  1039. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1040. int pcmdev = spec->pcm_rec[pin_idx].device;
  1041. snprintf(hdmi_str, sizeof(hdmi_str), "HDMI/DP,pcm=%d", pcmdev);
  1042. err = snd_hda_input_jack_add(codec, per_pin->pin_nid,
  1043. SND_JACK_VIDEOOUT, pcmdev > 0 ? hdmi_str : NULL);
  1044. if (err < 0)
  1045. return err;
  1046. hdmi_present_sense(per_pin, false);
  1047. return 0;
  1048. }
  1049. static int generic_hdmi_build_controls(struct hda_codec *codec)
  1050. {
  1051. struct hdmi_spec *spec = codec->spec;
  1052. int err;
  1053. int pin_idx;
  1054. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1055. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1056. err = generic_hdmi_build_jack(codec, pin_idx);
  1057. if (err < 0)
  1058. return err;
  1059. err = snd_hda_create_spdif_out_ctls(codec,
  1060. per_pin->pin_nid,
  1061. per_pin->mux_nids[0]);
  1062. if (err < 0)
  1063. return err;
  1064. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1065. /* add control for ELD Bytes */
  1066. err = hdmi_create_eld_ctl(codec,
  1067. pin_idx,
  1068. spec->pcm_rec[pin_idx].device);
  1069. if (err < 0)
  1070. return err;
  1071. err = snd_hda_jack_add_kctl(codec, per_pin->pin_nid,
  1072. "HDMI", pin_idx);
  1073. if (err < 0)
  1074. return err;
  1075. }
  1076. return 0;
  1077. }
  1078. static int generic_hdmi_init(struct hda_codec *codec)
  1079. {
  1080. struct hdmi_spec *spec = codec->spec;
  1081. int pin_idx;
  1082. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1083. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1084. hda_nid_t pin_nid = per_pin->pin_nid;
  1085. struct hdmi_eld *eld = &per_pin->sink_eld;
  1086. hdmi_init_pin(codec, pin_nid);
  1087. snd_hda_jack_detect_enable(codec, pin_nid, pin_nid);
  1088. per_pin->codec = codec;
  1089. INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
  1090. snd_hda_eld_proc_new(codec, eld, pin_idx);
  1091. }
  1092. snd_hda_jack_report_sync(codec);
  1093. return 0;
  1094. }
  1095. static void generic_hdmi_free(struct hda_codec *codec)
  1096. {
  1097. struct hdmi_spec *spec = codec->spec;
  1098. int pin_idx;
  1099. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1100. struct hdmi_spec_per_pin *per_pin = &spec->pins[pin_idx];
  1101. struct hdmi_eld *eld = &per_pin->sink_eld;
  1102. cancel_delayed_work(&per_pin->work);
  1103. snd_hda_eld_proc_free(codec, eld);
  1104. }
  1105. snd_hda_input_jack_free(codec);
  1106. flush_workqueue(codec->bus->workq);
  1107. kfree(spec);
  1108. }
  1109. static const struct hda_codec_ops generic_hdmi_patch_ops = {
  1110. .init = generic_hdmi_init,
  1111. .free = generic_hdmi_free,
  1112. .build_pcms = generic_hdmi_build_pcms,
  1113. .build_controls = generic_hdmi_build_controls,
  1114. .unsol_event = hdmi_unsol_event,
  1115. };
  1116. static int patch_generic_hdmi(struct hda_codec *codec)
  1117. {
  1118. struct hdmi_spec *spec;
  1119. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1120. if (spec == NULL)
  1121. return -ENOMEM;
  1122. codec->spec = spec;
  1123. if (hdmi_parse_codec(codec) < 0) {
  1124. codec->spec = NULL;
  1125. kfree(spec);
  1126. return -EINVAL;
  1127. }
  1128. codec->patch_ops = generic_hdmi_patch_ops;
  1129. init_channel_allocations();
  1130. return 0;
  1131. }
  1132. /*
  1133. * Shared non-generic implementations
  1134. */
  1135. static int simple_playback_build_pcms(struct hda_codec *codec)
  1136. {
  1137. struct hdmi_spec *spec = codec->spec;
  1138. struct hda_pcm *info = spec->pcm_rec;
  1139. int i;
  1140. codec->num_pcms = spec->num_cvts;
  1141. codec->pcm_info = info;
  1142. for (i = 0; i < codec->num_pcms; i++, info++) {
  1143. unsigned int chans;
  1144. struct hda_pcm_stream *pstr;
  1145. chans = get_wcaps(codec, spec->cvts[i].cvt_nid);
  1146. chans = get_wcaps_channels(chans);
  1147. info->name = generic_hdmi_pcm_names[i];
  1148. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1149. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1150. snd_BUG_ON(!spec->pcm_playback);
  1151. *pstr = *spec->pcm_playback;
  1152. pstr->nid = spec->cvts[i].cvt_nid;
  1153. if (pstr->channels_max <= 2 && chans && chans <= 16)
  1154. pstr->channels_max = chans;
  1155. }
  1156. return 0;
  1157. }
  1158. static int simple_playback_build_controls(struct hda_codec *codec)
  1159. {
  1160. struct hdmi_spec *spec = codec->spec;
  1161. int err;
  1162. int i;
  1163. for (i = 0; i < codec->num_pcms; i++) {
  1164. err = snd_hda_create_spdif_out_ctls(codec,
  1165. spec->cvts[i].cvt_nid,
  1166. spec->cvts[i].cvt_nid);
  1167. if (err < 0)
  1168. return err;
  1169. }
  1170. return 0;
  1171. }
  1172. static void simple_playback_free(struct hda_codec *codec)
  1173. {
  1174. struct hdmi_spec *spec = codec->spec;
  1175. kfree(spec);
  1176. }
  1177. /*
  1178. * Nvidia specific implementations
  1179. */
  1180. #define Nv_VERB_SET_Channel_Allocation 0xF79
  1181. #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
  1182. #define Nv_VERB_SET_Audio_Protection_On 0xF98
  1183. #define Nv_VERB_SET_Audio_Protection_Off 0xF99
  1184. #define nvhdmi_master_con_nid_7x 0x04
  1185. #define nvhdmi_master_pin_nid_7x 0x05
  1186. static const hda_nid_t nvhdmi_con_nids_7x[4] = {
  1187. /*front, rear, clfe, rear_surr */
  1188. 0x6, 0x8, 0xa, 0xc,
  1189. };
  1190. static const struct hda_verb nvhdmi_basic_init_7x[] = {
  1191. /* set audio protect on */
  1192. { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
  1193. /* enable digital output on pin widget */
  1194. { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1195. { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1196. { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1197. { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1198. { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  1199. {} /* terminator */
  1200. };
  1201. #ifdef LIMITED_RATE_FMT_SUPPORT
  1202. /* support only the safe format and rate */
  1203. #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
  1204. #define SUPPORTED_MAXBPS 16
  1205. #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
  1206. #else
  1207. /* support all rates and formats */
  1208. #define SUPPORTED_RATES \
  1209. (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  1210. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
  1211. SNDRV_PCM_RATE_192000)
  1212. #define SUPPORTED_MAXBPS 24
  1213. #define SUPPORTED_FORMATS \
  1214. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
  1215. #endif
  1216. static int nvhdmi_7x_init(struct hda_codec *codec)
  1217. {
  1218. snd_hda_sequence_write(codec, nvhdmi_basic_init_7x);
  1219. return 0;
  1220. }
  1221. static unsigned int channels_2_6_8[] = {
  1222. 2, 6, 8
  1223. };
  1224. static unsigned int channels_2_8[] = {
  1225. 2, 8
  1226. };
  1227. static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
  1228. .count = ARRAY_SIZE(channels_2_6_8),
  1229. .list = channels_2_6_8,
  1230. .mask = 0,
  1231. };
  1232. static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
  1233. .count = ARRAY_SIZE(channels_2_8),
  1234. .list = channels_2_8,
  1235. .mask = 0,
  1236. };
  1237. static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
  1238. struct hda_codec *codec,
  1239. struct snd_pcm_substream *substream)
  1240. {
  1241. struct hdmi_spec *spec = codec->spec;
  1242. struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
  1243. switch (codec->preset->id) {
  1244. case 0x10de0002:
  1245. case 0x10de0003:
  1246. case 0x10de0005:
  1247. case 0x10de0006:
  1248. hw_constraints_channels = &hw_constraints_2_8_channels;
  1249. break;
  1250. case 0x10de0007:
  1251. hw_constraints_channels = &hw_constraints_2_6_8_channels;
  1252. break;
  1253. default:
  1254. break;
  1255. }
  1256. if (hw_constraints_channels != NULL) {
  1257. snd_pcm_hw_constraint_list(substream->runtime, 0,
  1258. SNDRV_PCM_HW_PARAM_CHANNELS,
  1259. hw_constraints_channels);
  1260. } else {
  1261. snd_pcm_hw_constraint_step(substream->runtime, 0,
  1262. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  1263. }
  1264. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  1265. }
  1266. static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
  1267. struct hda_codec *codec,
  1268. struct snd_pcm_substream *substream)
  1269. {
  1270. struct hdmi_spec *spec = codec->spec;
  1271. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  1272. }
  1273. static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1274. struct hda_codec *codec,
  1275. unsigned int stream_tag,
  1276. unsigned int format,
  1277. struct snd_pcm_substream *substream)
  1278. {
  1279. struct hdmi_spec *spec = codec->spec;
  1280. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  1281. stream_tag, format, substream);
  1282. }
  1283. static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
  1284. int channels)
  1285. {
  1286. unsigned int chanmask;
  1287. int chan = channels ? (channels - 1) : 1;
  1288. switch (channels) {
  1289. default:
  1290. case 0:
  1291. case 2:
  1292. chanmask = 0x00;
  1293. break;
  1294. case 4:
  1295. chanmask = 0x08;
  1296. break;
  1297. case 6:
  1298. chanmask = 0x0b;
  1299. break;
  1300. case 8:
  1301. chanmask = 0x13;
  1302. break;
  1303. }
  1304. /* Set the audio infoframe channel allocation and checksum fields. The
  1305. * channel count is computed implicitly by the hardware. */
  1306. snd_hda_codec_write(codec, 0x1, 0,
  1307. Nv_VERB_SET_Channel_Allocation, chanmask);
  1308. snd_hda_codec_write(codec, 0x1, 0,
  1309. Nv_VERB_SET_Info_Frame_Checksum,
  1310. (0x71 - chan - chanmask));
  1311. }
  1312. static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
  1313. struct hda_codec *codec,
  1314. struct snd_pcm_substream *substream)
  1315. {
  1316. struct hdmi_spec *spec = codec->spec;
  1317. int i;
  1318. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
  1319. 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  1320. for (i = 0; i < 4; i++) {
  1321. /* set the stream id */
  1322. snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
  1323. AC_VERB_SET_CHANNEL_STREAMID, 0);
  1324. /* set the stream format */
  1325. snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
  1326. AC_VERB_SET_STREAM_FORMAT, 0);
  1327. }
  1328. /* The audio hardware sends a channel count of 0x7 (8ch) when all the
  1329. * streams are disabled. */
  1330. nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
  1331. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  1332. }
  1333. static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
  1334. struct hda_codec *codec,
  1335. unsigned int stream_tag,
  1336. unsigned int format,
  1337. struct snd_pcm_substream *substream)
  1338. {
  1339. int chs;
  1340. unsigned int dataDCC2, channel_id;
  1341. int i;
  1342. struct hdmi_spec *spec = codec->spec;
  1343. struct hda_spdif_out *spdif =
  1344. snd_hda_spdif_out_of_nid(codec, spec->cvts[0].cvt_nid);
  1345. mutex_lock(&codec->spdif_mutex);
  1346. chs = substream->runtime->channels;
  1347. dataDCC2 = 0x2;
  1348. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  1349. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
  1350. snd_hda_codec_write(codec,
  1351. nvhdmi_master_con_nid_7x,
  1352. 0,
  1353. AC_VERB_SET_DIGI_CONVERT_1,
  1354. spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
  1355. /* set the stream id */
  1356. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
  1357. AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
  1358. /* set the stream format */
  1359. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
  1360. AC_VERB_SET_STREAM_FORMAT, format);
  1361. /* turn on again (if needed) */
  1362. /* enable and set the channel status audio/data flag */
  1363. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
  1364. snd_hda_codec_write(codec,
  1365. nvhdmi_master_con_nid_7x,
  1366. 0,
  1367. AC_VERB_SET_DIGI_CONVERT_1,
  1368. spdif->ctls & 0xff);
  1369. snd_hda_codec_write(codec,
  1370. nvhdmi_master_con_nid_7x,
  1371. 0,
  1372. AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
  1373. }
  1374. for (i = 0; i < 4; i++) {
  1375. if (chs == 2)
  1376. channel_id = 0;
  1377. else
  1378. channel_id = i * 2;
  1379. /* turn off SPDIF once;
  1380. *otherwise the IEC958 bits won't be updated
  1381. */
  1382. if (codec->spdif_status_reset &&
  1383. (spdif->ctls & AC_DIG1_ENABLE))
  1384. snd_hda_codec_write(codec,
  1385. nvhdmi_con_nids_7x[i],
  1386. 0,
  1387. AC_VERB_SET_DIGI_CONVERT_1,
  1388. spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
  1389. /* set the stream id */
  1390. snd_hda_codec_write(codec,
  1391. nvhdmi_con_nids_7x[i],
  1392. 0,
  1393. AC_VERB_SET_CHANNEL_STREAMID,
  1394. (stream_tag << 4) | channel_id);
  1395. /* set the stream format */
  1396. snd_hda_codec_write(codec,
  1397. nvhdmi_con_nids_7x[i],
  1398. 0,
  1399. AC_VERB_SET_STREAM_FORMAT,
  1400. format);
  1401. /* turn on again (if needed) */
  1402. /* enable and set the channel status audio/data flag */
  1403. if (codec->spdif_status_reset &&
  1404. (spdif->ctls & AC_DIG1_ENABLE)) {
  1405. snd_hda_codec_write(codec,
  1406. nvhdmi_con_nids_7x[i],
  1407. 0,
  1408. AC_VERB_SET_DIGI_CONVERT_1,
  1409. spdif->ctls & 0xff);
  1410. snd_hda_codec_write(codec,
  1411. nvhdmi_con_nids_7x[i],
  1412. 0,
  1413. AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
  1414. }
  1415. }
  1416. nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
  1417. mutex_unlock(&codec->spdif_mutex);
  1418. return 0;
  1419. }
  1420. static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
  1421. .substreams = 1,
  1422. .channels_min = 2,
  1423. .channels_max = 8,
  1424. .nid = nvhdmi_master_con_nid_7x,
  1425. .rates = SUPPORTED_RATES,
  1426. .maxbps = SUPPORTED_MAXBPS,
  1427. .formats = SUPPORTED_FORMATS,
  1428. .ops = {
  1429. .open = simple_playback_pcm_open,
  1430. .close = nvhdmi_8ch_7x_pcm_close,
  1431. .prepare = nvhdmi_8ch_7x_pcm_prepare
  1432. },
  1433. };
  1434. static const struct hda_pcm_stream nvhdmi_pcm_playback_2ch = {
  1435. .substreams = 1,
  1436. .channels_min = 2,
  1437. .channels_max = 2,
  1438. .nid = nvhdmi_master_con_nid_7x,
  1439. .rates = SUPPORTED_RATES,
  1440. .maxbps = SUPPORTED_MAXBPS,
  1441. .formats = SUPPORTED_FORMATS,
  1442. .ops = {
  1443. .open = simple_playback_pcm_open,
  1444. .close = simple_playback_pcm_close,
  1445. .prepare = simple_playback_pcm_prepare
  1446. },
  1447. };
  1448. static const struct hda_codec_ops nvhdmi_patch_ops_8ch_7x = {
  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 const struct hda_codec_ops nvhdmi_patch_ops_2ch = {
  1455. .build_controls = simple_playback_build_controls,
  1456. .build_pcms = simple_playback_build_pcms,
  1457. .init = nvhdmi_7x_init,
  1458. .free = simple_playback_free,
  1459. };
  1460. static int patch_nvhdmi_2ch(struct hda_codec *codec)
  1461. {
  1462. struct hdmi_spec *spec;
  1463. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1464. if (spec == NULL)
  1465. return -ENOMEM;
  1466. codec->spec = spec;
  1467. spec->multiout.num_dacs = 0; /* no analog */
  1468. spec->multiout.max_channels = 2;
  1469. spec->multiout.dig_out_nid = nvhdmi_master_con_nid_7x;
  1470. spec->num_cvts = 1;
  1471. spec->cvts[0].cvt_nid = nvhdmi_master_con_nid_7x;
  1472. spec->pcm_playback = &nvhdmi_pcm_playback_2ch;
  1473. codec->patch_ops = nvhdmi_patch_ops_2ch;
  1474. return 0;
  1475. }
  1476. static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
  1477. {
  1478. struct hdmi_spec *spec;
  1479. int err = patch_nvhdmi_2ch(codec);
  1480. if (err < 0)
  1481. return err;
  1482. spec = codec->spec;
  1483. spec->multiout.max_channels = 8;
  1484. spec->pcm_playback = &nvhdmi_pcm_playback_8ch_7x;
  1485. codec->patch_ops = nvhdmi_patch_ops_8ch_7x;
  1486. /* Initialize the audio infoframe channel mask and checksum to something
  1487. * valid */
  1488. nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
  1489. return 0;
  1490. }
  1491. /*
  1492. * ATI-specific implementations
  1493. *
  1494. * FIXME: we may omit the whole this and use the generic code once after
  1495. * it's confirmed to work.
  1496. */
  1497. #define ATIHDMI_CVT_NID 0x02 /* audio converter */
  1498. #define ATIHDMI_PIN_NID 0x03 /* HDMI output pin */
  1499. static int atihdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1500. struct hda_codec *codec,
  1501. unsigned int stream_tag,
  1502. unsigned int format,
  1503. struct snd_pcm_substream *substream)
  1504. {
  1505. struct hdmi_spec *spec = codec->spec;
  1506. int chans = substream->runtime->channels;
  1507. int i, err;
  1508. err = simple_playback_pcm_prepare(hinfo, codec, stream_tag, format,
  1509. substream);
  1510. if (err < 0)
  1511. return err;
  1512. snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
  1513. AC_VERB_SET_CVT_CHAN_COUNT, chans - 1);
  1514. /* FIXME: XXX */
  1515. for (i = 0; i < chans; i++) {
  1516. snd_hda_codec_write(codec, spec->cvts[0].cvt_nid, 0,
  1517. AC_VERB_SET_HDMI_CHAN_SLOT,
  1518. (i << 4) | i);
  1519. }
  1520. return 0;
  1521. }
  1522. static const struct hda_pcm_stream atihdmi_pcm_digital_playback = {
  1523. .substreams = 1,
  1524. .channels_min = 2,
  1525. .channels_max = 2,
  1526. .nid = ATIHDMI_CVT_NID,
  1527. .ops = {
  1528. .open = simple_playback_pcm_open,
  1529. .close = simple_playback_pcm_close,
  1530. .prepare = atihdmi_playback_pcm_prepare
  1531. },
  1532. };
  1533. static const struct hda_verb atihdmi_basic_init[] = {
  1534. /* enable digital output on pin widget */
  1535. { 0x03, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
  1536. {} /* terminator */
  1537. };
  1538. static int atihdmi_init(struct hda_codec *codec)
  1539. {
  1540. struct hdmi_spec *spec = codec->spec;
  1541. snd_hda_sequence_write(codec, atihdmi_basic_init);
  1542. /* SI codec requires to unmute the pin */
  1543. if (get_wcaps(codec, spec->pins[0].pin_nid) & AC_WCAP_OUT_AMP)
  1544. snd_hda_codec_write(codec, spec->pins[0].pin_nid, 0,
  1545. AC_VERB_SET_AMP_GAIN_MUTE,
  1546. AMP_OUT_UNMUTE);
  1547. return 0;
  1548. }
  1549. static const struct hda_codec_ops atihdmi_patch_ops = {
  1550. .build_controls = simple_playback_build_controls,
  1551. .build_pcms = simple_playback_build_pcms,
  1552. .init = atihdmi_init,
  1553. .free = simple_playback_free,
  1554. };
  1555. static int patch_atihdmi(struct hda_codec *codec)
  1556. {
  1557. struct hdmi_spec *spec;
  1558. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1559. if (spec == NULL)
  1560. return -ENOMEM;
  1561. codec->spec = spec;
  1562. spec->multiout.num_dacs = 0; /* no analog */
  1563. spec->multiout.max_channels = 2;
  1564. spec->multiout.dig_out_nid = ATIHDMI_CVT_NID;
  1565. spec->num_cvts = 1;
  1566. spec->cvts[0].cvt_nid = ATIHDMI_CVT_NID;
  1567. spec->pins[0].pin_nid = ATIHDMI_PIN_NID;
  1568. spec->pcm_playback = &atihdmi_pcm_digital_playback;
  1569. codec->patch_ops = atihdmi_patch_ops;
  1570. return 0;
  1571. }
  1572. /*
  1573. * patch entries
  1574. */
  1575. static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
  1576. { .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
  1577. { .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
  1578. { .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
  1579. { .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_generic_hdmi },
  1580. { .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
  1581. { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
  1582. { .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
  1583. { .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1584. { .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1585. { .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1586. { .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  1587. { .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
  1588. { .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_generic_hdmi },
  1589. { .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_generic_hdmi },
  1590. { .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_generic_hdmi },
  1591. { .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_generic_hdmi },
  1592. { .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_generic_hdmi },
  1593. { .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_generic_hdmi },
  1594. { .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_generic_hdmi },
  1595. { .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_generic_hdmi },
  1596. { .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_generic_hdmi },
  1597. { .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_generic_hdmi },
  1598. { .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_generic_hdmi },
  1599. /* 17 is known to be absent */
  1600. { .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_generic_hdmi },
  1601. { .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_generic_hdmi },
  1602. { .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_generic_hdmi },
  1603. { .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_generic_hdmi },
  1604. { .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_generic_hdmi },
  1605. { .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_generic_hdmi },
  1606. { .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_generic_hdmi },
  1607. { .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_generic_hdmi },
  1608. { .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_generic_hdmi },
  1609. { .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_generic_hdmi },
  1610. { .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
  1611. { .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
  1612. { .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
  1613. { .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
  1614. { .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
  1615. { .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
  1616. { .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
  1617. { .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
  1618. { .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
  1619. { .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
  1620. {} /* terminator */
  1621. };
  1622. MODULE_ALIAS("snd-hda-codec-id:1002793c");
  1623. MODULE_ALIAS("snd-hda-codec-id:10027919");
  1624. MODULE_ALIAS("snd-hda-codec-id:1002791a");
  1625. MODULE_ALIAS("snd-hda-codec-id:1002aa01");
  1626. MODULE_ALIAS("snd-hda-codec-id:10951390");
  1627. MODULE_ALIAS("snd-hda-codec-id:10951392");
  1628. MODULE_ALIAS("snd-hda-codec-id:10de0002");
  1629. MODULE_ALIAS("snd-hda-codec-id:10de0003");
  1630. MODULE_ALIAS("snd-hda-codec-id:10de0005");
  1631. MODULE_ALIAS("snd-hda-codec-id:10de0006");
  1632. MODULE_ALIAS("snd-hda-codec-id:10de0007");
  1633. MODULE_ALIAS("snd-hda-codec-id:10de000a");
  1634. MODULE_ALIAS("snd-hda-codec-id:10de000b");
  1635. MODULE_ALIAS("snd-hda-codec-id:10de000c");
  1636. MODULE_ALIAS("snd-hda-codec-id:10de000d");
  1637. MODULE_ALIAS("snd-hda-codec-id:10de0010");
  1638. MODULE_ALIAS("snd-hda-codec-id:10de0011");
  1639. MODULE_ALIAS("snd-hda-codec-id:10de0012");
  1640. MODULE_ALIAS("snd-hda-codec-id:10de0013");
  1641. MODULE_ALIAS("snd-hda-codec-id:10de0014");
  1642. MODULE_ALIAS("snd-hda-codec-id:10de0015");
  1643. MODULE_ALIAS("snd-hda-codec-id:10de0016");
  1644. MODULE_ALIAS("snd-hda-codec-id:10de0018");
  1645. MODULE_ALIAS("snd-hda-codec-id:10de0019");
  1646. MODULE_ALIAS("snd-hda-codec-id:10de001a");
  1647. MODULE_ALIAS("snd-hda-codec-id:10de001b");
  1648. MODULE_ALIAS("snd-hda-codec-id:10de001c");
  1649. MODULE_ALIAS("snd-hda-codec-id:10de0040");
  1650. MODULE_ALIAS("snd-hda-codec-id:10de0041");
  1651. MODULE_ALIAS("snd-hda-codec-id:10de0042");
  1652. MODULE_ALIAS("snd-hda-codec-id:10de0043");
  1653. MODULE_ALIAS("snd-hda-codec-id:10de0044");
  1654. MODULE_ALIAS("snd-hda-codec-id:10de0067");
  1655. MODULE_ALIAS("snd-hda-codec-id:10de8001");
  1656. MODULE_ALIAS("snd-hda-codec-id:17e80047");
  1657. MODULE_ALIAS("snd-hda-codec-id:80860054");
  1658. MODULE_ALIAS("snd-hda-codec-id:80862801");
  1659. MODULE_ALIAS("snd-hda-codec-id:80862802");
  1660. MODULE_ALIAS("snd-hda-codec-id:80862803");
  1661. MODULE_ALIAS("snd-hda-codec-id:80862804");
  1662. MODULE_ALIAS("snd-hda-codec-id:80862805");
  1663. MODULE_ALIAS("snd-hda-codec-id:80862806");
  1664. MODULE_ALIAS("snd-hda-codec-id:808629fb");
  1665. MODULE_LICENSE("GPL");
  1666. MODULE_DESCRIPTION("HDMI HD-audio codec");
  1667. MODULE_ALIAS("snd-hda-codec-intelhdmi");
  1668. MODULE_ALIAS("snd-hda-codec-nvhdmi");
  1669. MODULE_ALIAS("snd-hda-codec-atihdmi");
  1670. static struct hda_codec_preset_list intel_list = {
  1671. .preset = snd_hda_preset_hdmi,
  1672. .owner = THIS_MODULE,
  1673. };
  1674. static int __init patch_hdmi_init(void)
  1675. {
  1676. return snd_hda_add_codec_preset(&intel_list);
  1677. }
  1678. static void __exit patch_hdmi_exit(void)
  1679. {
  1680. snd_hda_delete_codec_preset(&intel_list);
  1681. }
  1682. module_init(patch_hdmi_init)
  1683. module_exit(patch_hdmi_exit)