patch_hdmi.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441
  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. * Copyright (c) 2013 Anssi Hannula <anssi.hannula@iki.fi>
  10. *
  11. * Authors:
  12. * Wu Fengguang <wfg@linux.intel.com>
  13. *
  14. * Maintained by:
  15. * Wu Fengguang <wfg@linux.intel.com>
  16. *
  17. * This program is free software; you can redistribute it and/or modify it
  18. * under the terms of the GNU General Public License as published by the Free
  19. * Software Foundation; either version 2 of the License, or (at your option)
  20. * any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful, but
  23. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  24. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  25. * for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software Foundation,
  29. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  30. */
  31. #include <linux/init.h>
  32. #include <linux/delay.h>
  33. #include <linux/slab.h>
  34. #include <linux/module.h>
  35. #include <sound/core.h>
  36. #include <sound/jack.h>
  37. #include <sound/asoundef.h>
  38. #include <sound/tlv.h>
  39. #include "hda_codec.h"
  40. #include "hda_local.h"
  41. #include "hda_jack.h"
  42. static bool static_hdmi_pcm;
  43. module_param(static_hdmi_pcm, bool, 0644);
  44. MODULE_PARM_DESC(static_hdmi_pcm, "Don't restrict PCM parameters per ELD info");
  45. #define is_haswell(codec) ((codec)->vendor_id == 0x80862807)
  46. #define is_broadwell(codec) ((codec)->vendor_id == 0x80862808)
  47. #define is_haswell_plus(codec) (is_haswell(codec) || is_broadwell(codec))
  48. #define is_valleyview(codec) ((codec)->vendor_id == 0x80862882)
  49. struct hdmi_spec_per_cvt {
  50. hda_nid_t cvt_nid;
  51. int assigned;
  52. unsigned int channels_min;
  53. unsigned int channels_max;
  54. u32 rates;
  55. u64 formats;
  56. unsigned int maxbps;
  57. };
  58. /* max. connections to a widget */
  59. #define HDA_MAX_CONNECTIONS 32
  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. int mux_idx;
  65. hda_nid_t cvt_nid;
  66. struct hda_codec *codec;
  67. struct hdmi_eld sink_eld;
  68. struct mutex lock;
  69. struct delayed_work work;
  70. struct snd_kcontrol *eld_ctl;
  71. int repoll_count;
  72. bool setup; /* the stream has been set up by prepare callback */
  73. int channels; /* current number of channels */
  74. bool non_pcm;
  75. bool chmap_set; /* channel-map override by ALSA API? */
  76. unsigned char chmap[8]; /* ALSA API channel-map */
  77. char pcm_name[8]; /* filled in build_pcm callbacks */
  78. #ifdef CONFIG_PROC_FS
  79. struct snd_info_entry *proc_entry;
  80. #endif
  81. };
  82. struct cea_channel_speaker_allocation;
  83. /* operations used by generic code that can be overridden by patches */
  84. struct hdmi_ops {
  85. int (*pin_get_eld)(struct hda_codec *codec, hda_nid_t pin_nid,
  86. unsigned char *buf, int *eld_size);
  87. /* get and set channel assigned to each HDMI ASP (audio sample packet) slot */
  88. int (*pin_get_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
  89. int asp_slot);
  90. int (*pin_set_slot_channel)(struct hda_codec *codec, hda_nid_t pin_nid,
  91. int asp_slot, int channel);
  92. void (*pin_setup_infoframe)(struct hda_codec *codec, hda_nid_t pin_nid,
  93. int ca, int active_channels, int conn_type);
  94. /* enable/disable HBR (HD passthrough) */
  95. int (*pin_hbr_setup)(struct hda_codec *codec, hda_nid_t pin_nid, bool hbr);
  96. int (*setup_stream)(struct hda_codec *codec, hda_nid_t cvt_nid,
  97. hda_nid_t pin_nid, u32 stream_tag, int format);
  98. /* Helpers for producing the channel map TLVs. These can be overridden
  99. * for devices that have non-standard mapping requirements. */
  100. int (*chmap_cea_alloc_validate_get_type)(struct cea_channel_speaker_allocation *cap,
  101. int channels);
  102. void (*cea_alloc_to_tlv_chmap)(struct cea_channel_speaker_allocation *cap,
  103. unsigned int *chmap, int channels);
  104. /* check that the user-given chmap is supported */
  105. int (*chmap_validate)(int ca, int channels, unsigned char *chmap);
  106. };
  107. struct hdmi_spec {
  108. int num_cvts;
  109. struct snd_array cvts; /* struct hdmi_spec_per_cvt */
  110. hda_nid_t cvt_nids[4]; /* only for haswell fix */
  111. int num_pins;
  112. struct snd_array pins; /* struct hdmi_spec_per_pin */
  113. struct snd_array pcm_rec; /* struct hda_pcm */
  114. unsigned int channels_max; /* max over all cvts */
  115. struct hdmi_eld temp_eld;
  116. struct hdmi_ops ops;
  117. bool dyn_pin_out;
  118. /*
  119. * Non-generic VIA/NVIDIA specific
  120. */
  121. struct hda_multi_out multiout;
  122. struct hda_pcm_stream pcm_playback;
  123. };
  124. struct hdmi_audio_infoframe {
  125. u8 type; /* 0x84 */
  126. u8 ver; /* 0x01 */
  127. u8 len; /* 0x0a */
  128. u8 checksum;
  129. u8 CC02_CT47; /* CC in bits 0:2, CT in 4:7 */
  130. u8 SS01_SF24;
  131. u8 CXT04;
  132. u8 CA;
  133. u8 LFEPBL01_LSV36_DM_INH7;
  134. };
  135. struct dp_audio_infoframe {
  136. u8 type; /* 0x84 */
  137. u8 len; /* 0x1b */
  138. u8 ver; /* 0x11 << 2 */
  139. u8 CC02_CT47; /* match with HDMI infoframe from this on */
  140. u8 SS01_SF24;
  141. u8 CXT04;
  142. u8 CA;
  143. u8 LFEPBL01_LSV36_DM_INH7;
  144. };
  145. union audio_infoframe {
  146. struct hdmi_audio_infoframe hdmi;
  147. struct dp_audio_infoframe dp;
  148. u8 bytes[0];
  149. };
  150. /*
  151. * CEA speaker placement:
  152. *
  153. * FLH FCH FRH
  154. * FLW FL FLC FC FRC FR FRW
  155. *
  156. * LFE
  157. * TC
  158. *
  159. * RL RLC RC RRC RR
  160. *
  161. * The Left/Right Surround channel _notions_ LS/RS in SMPTE 320M corresponds to
  162. * CEA RL/RR; The SMPTE channel _assignment_ C/LFE is swapped to CEA LFE/FC.
  163. */
  164. enum cea_speaker_placement {
  165. FL = (1 << 0), /* Front Left */
  166. FC = (1 << 1), /* Front Center */
  167. FR = (1 << 2), /* Front Right */
  168. FLC = (1 << 3), /* Front Left Center */
  169. FRC = (1 << 4), /* Front Right Center */
  170. RL = (1 << 5), /* Rear Left */
  171. RC = (1 << 6), /* Rear Center */
  172. RR = (1 << 7), /* Rear Right */
  173. RLC = (1 << 8), /* Rear Left Center */
  174. RRC = (1 << 9), /* Rear Right Center */
  175. LFE = (1 << 10), /* Low Frequency Effect */
  176. FLW = (1 << 11), /* Front Left Wide */
  177. FRW = (1 << 12), /* Front Right Wide */
  178. FLH = (1 << 13), /* Front Left High */
  179. FCH = (1 << 14), /* Front Center High */
  180. FRH = (1 << 15), /* Front Right High */
  181. TC = (1 << 16), /* Top Center */
  182. };
  183. /*
  184. * ELD SA bits in the CEA Speaker Allocation data block
  185. */
  186. static int eld_speaker_allocation_bits[] = {
  187. [0] = FL | FR,
  188. [1] = LFE,
  189. [2] = FC,
  190. [3] = RL | RR,
  191. [4] = RC,
  192. [5] = FLC | FRC,
  193. [6] = RLC | RRC,
  194. /* the following are not defined in ELD yet */
  195. [7] = FLW | FRW,
  196. [8] = FLH | FRH,
  197. [9] = TC,
  198. [10] = FCH,
  199. };
  200. struct cea_channel_speaker_allocation {
  201. int ca_index;
  202. int speakers[8];
  203. /* derived values, just for convenience */
  204. int channels;
  205. int spk_mask;
  206. };
  207. /*
  208. * ALSA sequence is:
  209. *
  210. * surround40 surround41 surround50 surround51 surround71
  211. * ch0 front left = = = =
  212. * ch1 front right = = = =
  213. * ch2 rear left = = = =
  214. * ch3 rear right = = = =
  215. * ch4 LFE center center center
  216. * ch5 LFE LFE
  217. * ch6 side left
  218. * ch7 side right
  219. *
  220. * surround71 = {FL, FR, RLC, RRC, FC, LFE, RL, RR}
  221. */
  222. static int hdmi_channel_mapping[0x32][8] = {
  223. /* stereo */
  224. [0x00] = { 0x00, 0x11, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
  225. /* 2.1 */
  226. [0x01] = { 0x00, 0x11, 0x22, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 },
  227. /* Dolby Surround */
  228. [0x02] = { 0x00, 0x11, 0x23, 0xf2, 0xf4, 0xf5, 0xf6, 0xf7 },
  229. /* surround40 */
  230. [0x08] = { 0x00, 0x11, 0x24, 0x35, 0xf3, 0xf2, 0xf6, 0xf7 },
  231. /* 4ch */
  232. [0x03] = { 0x00, 0x11, 0x23, 0x32, 0x44, 0xf5, 0xf6, 0xf7 },
  233. /* surround41 */
  234. [0x09] = { 0x00, 0x11, 0x24, 0x35, 0x42, 0xf3, 0xf6, 0xf7 },
  235. /* surround50 */
  236. [0x0a] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0xf2, 0xf6, 0xf7 },
  237. /* surround51 */
  238. [0x0b] = { 0x00, 0x11, 0x24, 0x35, 0x43, 0x52, 0xf6, 0xf7 },
  239. /* 7.1 */
  240. [0x13] = { 0x00, 0x11, 0x26, 0x37, 0x43, 0x52, 0x64, 0x75 },
  241. };
  242. /*
  243. * This is an ordered list!
  244. *
  245. * The preceding ones have better chances to be selected by
  246. * hdmi_channel_allocation().
  247. */
  248. static struct cea_channel_speaker_allocation channel_allocations[] = {
  249. /* channel: 7 6 5 4 3 2 1 0 */
  250. { .ca_index = 0x00, .speakers = { 0, 0, 0, 0, 0, 0, FR, FL } },
  251. /* 2.1 */
  252. { .ca_index = 0x01, .speakers = { 0, 0, 0, 0, 0, LFE, FR, FL } },
  253. /* Dolby Surround */
  254. { .ca_index = 0x02, .speakers = { 0, 0, 0, 0, FC, 0, FR, FL } },
  255. /* surround40 */
  256. { .ca_index = 0x08, .speakers = { 0, 0, RR, RL, 0, 0, FR, FL } },
  257. /* surround41 */
  258. { .ca_index = 0x09, .speakers = { 0, 0, RR, RL, 0, LFE, FR, FL } },
  259. /* surround50 */
  260. { .ca_index = 0x0a, .speakers = { 0, 0, RR, RL, FC, 0, FR, FL } },
  261. /* surround51 */
  262. { .ca_index = 0x0b, .speakers = { 0, 0, RR, RL, FC, LFE, FR, FL } },
  263. /* 6.1 */
  264. { .ca_index = 0x0f, .speakers = { 0, RC, RR, RL, FC, LFE, FR, FL } },
  265. /* surround71 */
  266. { .ca_index = 0x13, .speakers = { RRC, RLC, RR, RL, FC, LFE, FR, FL } },
  267. { .ca_index = 0x03, .speakers = { 0, 0, 0, 0, FC, LFE, FR, FL } },
  268. { .ca_index = 0x04, .speakers = { 0, 0, 0, RC, 0, 0, FR, FL } },
  269. { .ca_index = 0x05, .speakers = { 0, 0, 0, RC, 0, LFE, FR, FL } },
  270. { .ca_index = 0x06, .speakers = { 0, 0, 0, RC, FC, 0, FR, FL } },
  271. { .ca_index = 0x07, .speakers = { 0, 0, 0, RC, FC, LFE, FR, FL } },
  272. { .ca_index = 0x0c, .speakers = { 0, RC, RR, RL, 0, 0, FR, FL } },
  273. { .ca_index = 0x0d, .speakers = { 0, RC, RR, RL, 0, LFE, FR, FL } },
  274. { .ca_index = 0x0e, .speakers = { 0, RC, RR, RL, FC, 0, FR, FL } },
  275. { .ca_index = 0x10, .speakers = { RRC, RLC, RR, RL, 0, 0, FR, FL } },
  276. { .ca_index = 0x11, .speakers = { RRC, RLC, RR, RL, 0, LFE, FR, FL } },
  277. { .ca_index = 0x12, .speakers = { RRC, RLC, RR, RL, FC, 0, FR, FL } },
  278. { .ca_index = 0x14, .speakers = { FRC, FLC, 0, 0, 0, 0, FR, FL } },
  279. { .ca_index = 0x15, .speakers = { FRC, FLC, 0, 0, 0, LFE, FR, FL } },
  280. { .ca_index = 0x16, .speakers = { FRC, FLC, 0, 0, FC, 0, FR, FL } },
  281. { .ca_index = 0x17, .speakers = { FRC, FLC, 0, 0, FC, LFE, FR, FL } },
  282. { .ca_index = 0x18, .speakers = { FRC, FLC, 0, RC, 0, 0, FR, FL } },
  283. { .ca_index = 0x19, .speakers = { FRC, FLC, 0, RC, 0, LFE, FR, FL } },
  284. { .ca_index = 0x1a, .speakers = { FRC, FLC, 0, RC, FC, 0, FR, FL } },
  285. { .ca_index = 0x1b, .speakers = { FRC, FLC, 0, RC, FC, LFE, FR, FL } },
  286. { .ca_index = 0x1c, .speakers = { FRC, FLC, RR, RL, 0, 0, FR, FL } },
  287. { .ca_index = 0x1d, .speakers = { FRC, FLC, RR, RL, 0, LFE, FR, FL } },
  288. { .ca_index = 0x1e, .speakers = { FRC, FLC, RR, RL, FC, 0, FR, FL } },
  289. { .ca_index = 0x1f, .speakers = { FRC, FLC, RR, RL, FC, LFE, FR, FL } },
  290. { .ca_index = 0x20, .speakers = { 0, FCH, RR, RL, FC, 0, FR, FL } },
  291. { .ca_index = 0x21, .speakers = { 0, FCH, RR, RL, FC, LFE, FR, FL } },
  292. { .ca_index = 0x22, .speakers = { TC, 0, RR, RL, FC, 0, FR, FL } },
  293. { .ca_index = 0x23, .speakers = { TC, 0, RR, RL, FC, LFE, FR, FL } },
  294. { .ca_index = 0x24, .speakers = { FRH, FLH, RR, RL, 0, 0, FR, FL } },
  295. { .ca_index = 0x25, .speakers = { FRH, FLH, RR, RL, 0, LFE, FR, FL } },
  296. { .ca_index = 0x26, .speakers = { FRW, FLW, RR, RL, 0, 0, FR, FL } },
  297. { .ca_index = 0x27, .speakers = { FRW, FLW, RR, RL, 0, LFE, FR, FL } },
  298. { .ca_index = 0x28, .speakers = { TC, RC, RR, RL, FC, 0, FR, FL } },
  299. { .ca_index = 0x29, .speakers = { TC, RC, RR, RL, FC, LFE, FR, FL } },
  300. { .ca_index = 0x2a, .speakers = { FCH, RC, RR, RL, FC, 0, FR, FL } },
  301. { .ca_index = 0x2b, .speakers = { FCH, RC, RR, RL, FC, LFE, FR, FL } },
  302. { .ca_index = 0x2c, .speakers = { TC, FCH, RR, RL, FC, 0, FR, FL } },
  303. { .ca_index = 0x2d, .speakers = { TC, FCH, RR, RL, FC, LFE, FR, FL } },
  304. { .ca_index = 0x2e, .speakers = { FRH, FLH, RR, RL, FC, 0, FR, FL } },
  305. { .ca_index = 0x2f, .speakers = { FRH, FLH, RR, RL, FC, LFE, FR, FL } },
  306. { .ca_index = 0x30, .speakers = { FRW, FLW, RR, RL, FC, 0, FR, FL } },
  307. { .ca_index = 0x31, .speakers = { FRW, FLW, RR, RL, FC, LFE, FR, FL } },
  308. };
  309. /*
  310. * HDMI routines
  311. */
  312. #define get_pin(spec, idx) \
  313. ((struct hdmi_spec_per_pin *)snd_array_elem(&spec->pins, idx))
  314. #define get_cvt(spec, idx) \
  315. ((struct hdmi_spec_per_cvt *)snd_array_elem(&spec->cvts, idx))
  316. #define get_pcm_rec(spec, idx) \
  317. ((struct hda_pcm *)snd_array_elem(&spec->pcm_rec, idx))
  318. static int pin_nid_to_pin_index(struct hda_codec *codec, hda_nid_t pin_nid)
  319. {
  320. struct hdmi_spec *spec = codec->spec;
  321. int pin_idx;
  322. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
  323. if (get_pin(spec, pin_idx)->pin_nid == pin_nid)
  324. return pin_idx;
  325. codec_warn(codec, "HDMI: pin nid %d not registered\n", pin_nid);
  326. return -EINVAL;
  327. }
  328. static int hinfo_to_pin_index(struct hda_codec *codec,
  329. struct hda_pcm_stream *hinfo)
  330. {
  331. struct hdmi_spec *spec = codec->spec;
  332. int pin_idx;
  333. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++)
  334. if (get_pcm_rec(spec, pin_idx)->stream == hinfo)
  335. return pin_idx;
  336. codec_warn(codec, "HDMI: hinfo %p not registered\n", hinfo);
  337. return -EINVAL;
  338. }
  339. static int cvt_nid_to_cvt_index(struct hda_codec *codec, hda_nid_t cvt_nid)
  340. {
  341. struct hdmi_spec *spec = codec->spec;
  342. int cvt_idx;
  343. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++)
  344. if (get_cvt(spec, cvt_idx)->cvt_nid == cvt_nid)
  345. return cvt_idx;
  346. codec_warn(codec, "HDMI: cvt nid %d not registered\n", cvt_nid);
  347. return -EINVAL;
  348. }
  349. static int hdmi_eld_ctl_info(struct snd_kcontrol *kcontrol,
  350. struct snd_ctl_elem_info *uinfo)
  351. {
  352. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  353. struct hdmi_spec *spec = codec->spec;
  354. struct hdmi_spec_per_pin *per_pin;
  355. struct hdmi_eld *eld;
  356. int pin_idx;
  357. uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
  358. pin_idx = kcontrol->private_value;
  359. per_pin = get_pin(spec, pin_idx);
  360. eld = &per_pin->sink_eld;
  361. mutex_lock(&per_pin->lock);
  362. uinfo->count = eld->eld_valid ? eld->eld_size : 0;
  363. mutex_unlock(&per_pin->lock);
  364. return 0;
  365. }
  366. static int hdmi_eld_ctl_get(struct snd_kcontrol *kcontrol,
  367. struct snd_ctl_elem_value *ucontrol)
  368. {
  369. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  370. struct hdmi_spec *spec = codec->spec;
  371. struct hdmi_spec_per_pin *per_pin;
  372. struct hdmi_eld *eld;
  373. int pin_idx;
  374. pin_idx = kcontrol->private_value;
  375. per_pin = get_pin(spec, pin_idx);
  376. eld = &per_pin->sink_eld;
  377. mutex_lock(&per_pin->lock);
  378. if (eld->eld_size > ARRAY_SIZE(ucontrol->value.bytes.data)) {
  379. mutex_unlock(&per_pin->lock);
  380. snd_BUG();
  381. return -EINVAL;
  382. }
  383. memset(ucontrol->value.bytes.data, 0,
  384. ARRAY_SIZE(ucontrol->value.bytes.data));
  385. if (eld->eld_valid)
  386. memcpy(ucontrol->value.bytes.data, eld->eld_buffer,
  387. eld->eld_size);
  388. mutex_unlock(&per_pin->lock);
  389. return 0;
  390. }
  391. static struct snd_kcontrol_new eld_bytes_ctl = {
  392. .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
  393. .iface = SNDRV_CTL_ELEM_IFACE_PCM,
  394. .name = "ELD",
  395. .info = hdmi_eld_ctl_info,
  396. .get = hdmi_eld_ctl_get,
  397. };
  398. static int hdmi_create_eld_ctl(struct hda_codec *codec, int pin_idx,
  399. int device)
  400. {
  401. struct snd_kcontrol *kctl;
  402. struct hdmi_spec *spec = codec->spec;
  403. int err;
  404. kctl = snd_ctl_new1(&eld_bytes_ctl, codec);
  405. if (!kctl)
  406. return -ENOMEM;
  407. kctl->private_value = pin_idx;
  408. kctl->id.device = device;
  409. err = snd_hda_ctl_add(codec, get_pin(spec, pin_idx)->pin_nid, kctl);
  410. if (err < 0)
  411. return err;
  412. get_pin(spec, pin_idx)->eld_ctl = kctl;
  413. return 0;
  414. }
  415. #ifdef BE_PARANOID
  416. static void hdmi_get_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
  417. int *packet_index, int *byte_index)
  418. {
  419. int val;
  420. val = snd_hda_codec_read(codec, pin_nid, 0,
  421. AC_VERB_GET_HDMI_DIP_INDEX, 0);
  422. *packet_index = val >> 5;
  423. *byte_index = val & 0x1f;
  424. }
  425. #endif
  426. static void hdmi_set_dip_index(struct hda_codec *codec, hda_nid_t pin_nid,
  427. int packet_index, int byte_index)
  428. {
  429. int val;
  430. val = (packet_index << 5) | (byte_index & 0x1f);
  431. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_INDEX, val);
  432. }
  433. static void hdmi_write_dip_byte(struct hda_codec *codec, hda_nid_t pin_nid,
  434. unsigned char val)
  435. {
  436. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_DATA, val);
  437. }
  438. static void hdmi_init_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  439. {
  440. struct hdmi_spec *spec = codec->spec;
  441. int pin_out;
  442. /* Unmute */
  443. if (get_wcaps(codec, pin_nid) & AC_WCAP_OUT_AMP)
  444. snd_hda_codec_write(codec, pin_nid, 0,
  445. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_UNMUTE);
  446. if (spec->dyn_pin_out)
  447. /* Disable pin out until stream is active */
  448. pin_out = 0;
  449. else
  450. /* Enable pin out: some machines with GM965 gets broken output
  451. * when the pin is disabled or changed while using with HDMI
  452. */
  453. pin_out = PIN_OUT;
  454. snd_hda_codec_write(codec, pin_nid, 0,
  455. AC_VERB_SET_PIN_WIDGET_CONTROL, pin_out);
  456. }
  457. static int hdmi_get_channel_count(struct hda_codec *codec, hda_nid_t cvt_nid)
  458. {
  459. return 1 + snd_hda_codec_read(codec, cvt_nid, 0,
  460. AC_VERB_GET_CVT_CHAN_COUNT, 0);
  461. }
  462. static void hdmi_set_channel_count(struct hda_codec *codec,
  463. hda_nid_t cvt_nid, int chs)
  464. {
  465. if (chs != hdmi_get_channel_count(codec, cvt_nid))
  466. snd_hda_codec_write(codec, cvt_nid, 0,
  467. AC_VERB_SET_CVT_CHAN_COUNT, chs - 1);
  468. }
  469. /*
  470. * ELD proc files
  471. */
  472. #ifdef CONFIG_PROC_FS
  473. static void print_eld_info(struct snd_info_entry *entry,
  474. struct snd_info_buffer *buffer)
  475. {
  476. struct hdmi_spec_per_pin *per_pin = entry->private_data;
  477. mutex_lock(&per_pin->lock);
  478. snd_hdmi_print_eld_info(&per_pin->sink_eld, buffer);
  479. mutex_unlock(&per_pin->lock);
  480. }
  481. static void write_eld_info(struct snd_info_entry *entry,
  482. struct snd_info_buffer *buffer)
  483. {
  484. struct hdmi_spec_per_pin *per_pin = entry->private_data;
  485. mutex_lock(&per_pin->lock);
  486. snd_hdmi_write_eld_info(&per_pin->sink_eld, buffer);
  487. mutex_unlock(&per_pin->lock);
  488. }
  489. static int eld_proc_new(struct hdmi_spec_per_pin *per_pin, int index)
  490. {
  491. char name[32];
  492. struct hda_codec *codec = per_pin->codec;
  493. struct snd_info_entry *entry;
  494. int err;
  495. snprintf(name, sizeof(name), "eld#%d.%d", codec->addr, index);
  496. err = snd_card_proc_new(codec->bus->card, name, &entry);
  497. if (err < 0)
  498. return err;
  499. snd_info_set_text_ops(entry, per_pin, print_eld_info);
  500. entry->c.text.write = write_eld_info;
  501. entry->mode |= S_IWUSR;
  502. per_pin->proc_entry = entry;
  503. return 0;
  504. }
  505. static void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
  506. {
  507. if (!per_pin->codec->bus->shutdown && per_pin->proc_entry) {
  508. snd_device_free(per_pin->codec->bus->card, per_pin->proc_entry);
  509. per_pin->proc_entry = NULL;
  510. }
  511. }
  512. #else
  513. static inline int eld_proc_new(struct hdmi_spec_per_pin *per_pin,
  514. int index)
  515. {
  516. return 0;
  517. }
  518. static inline void eld_proc_free(struct hdmi_spec_per_pin *per_pin)
  519. {
  520. }
  521. #endif
  522. /*
  523. * Channel mapping routines
  524. */
  525. /*
  526. * Compute derived values in channel_allocations[].
  527. */
  528. static void init_channel_allocations(void)
  529. {
  530. int i, j;
  531. struct cea_channel_speaker_allocation *p;
  532. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  533. p = channel_allocations + i;
  534. p->channels = 0;
  535. p->spk_mask = 0;
  536. for (j = 0; j < ARRAY_SIZE(p->speakers); j++)
  537. if (p->speakers[j]) {
  538. p->channels++;
  539. p->spk_mask |= p->speakers[j];
  540. }
  541. }
  542. }
  543. static int get_channel_allocation_order(int ca)
  544. {
  545. int i;
  546. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  547. if (channel_allocations[i].ca_index == ca)
  548. break;
  549. }
  550. return i;
  551. }
  552. /*
  553. * The transformation takes two steps:
  554. *
  555. * eld->spk_alloc => (eld_speaker_allocation_bits[]) => spk_mask
  556. * spk_mask => (channel_allocations[]) => ai->CA
  557. *
  558. * TODO: it could select the wrong CA from multiple candidates.
  559. */
  560. static int hdmi_channel_allocation(struct hdmi_eld *eld, int channels)
  561. {
  562. int i;
  563. int ca = 0;
  564. int spk_mask = 0;
  565. char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
  566. /*
  567. * CA defaults to 0 for basic stereo audio
  568. */
  569. if (channels <= 2)
  570. return 0;
  571. /*
  572. * expand ELD's speaker allocation mask
  573. *
  574. * ELD tells the speaker mask in a compact(paired) form,
  575. * expand ELD's notions to match the ones used by Audio InfoFrame.
  576. */
  577. for (i = 0; i < ARRAY_SIZE(eld_speaker_allocation_bits); i++) {
  578. if (eld->info.spk_alloc & (1 << i))
  579. spk_mask |= eld_speaker_allocation_bits[i];
  580. }
  581. /* search for the first working match in the CA table */
  582. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  583. if (channels == channel_allocations[i].channels &&
  584. (spk_mask & channel_allocations[i].spk_mask) ==
  585. channel_allocations[i].spk_mask) {
  586. ca = channel_allocations[i].ca_index;
  587. break;
  588. }
  589. }
  590. if (!ca) {
  591. /* if there was no match, select the regular ALSA channel
  592. * allocation with the matching number of channels */
  593. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  594. if (channels == channel_allocations[i].channels) {
  595. ca = channel_allocations[i].ca_index;
  596. break;
  597. }
  598. }
  599. }
  600. snd_print_channel_allocation(eld->info.spk_alloc, buf, sizeof(buf));
  601. snd_printdd("HDMI: select CA 0x%x for %d-channel allocation: %s\n",
  602. ca, channels, buf);
  603. return ca;
  604. }
  605. static void hdmi_debug_channel_mapping(struct hda_codec *codec,
  606. hda_nid_t pin_nid)
  607. {
  608. #ifdef CONFIG_SND_DEBUG_VERBOSE
  609. struct hdmi_spec *spec = codec->spec;
  610. int i;
  611. int channel;
  612. for (i = 0; i < 8; i++) {
  613. channel = spec->ops.pin_get_slot_channel(codec, pin_nid, i);
  614. codec_dbg(codec, "HDMI: ASP channel %d => slot %d\n",
  615. channel, i);
  616. }
  617. #endif
  618. }
  619. static void hdmi_std_setup_channel_mapping(struct hda_codec *codec,
  620. hda_nid_t pin_nid,
  621. bool non_pcm,
  622. int ca)
  623. {
  624. struct hdmi_spec *spec = codec->spec;
  625. struct cea_channel_speaker_allocation *ch_alloc;
  626. int i;
  627. int err;
  628. int order;
  629. int non_pcm_mapping[8];
  630. order = get_channel_allocation_order(ca);
  631. ch_alloc = &channel_allocations[order];
  632. if (hdmi_channel_mapping[ca][1] == 0) {
  633. int hdmi_slot = 0;
  634. /* fill actual channel mappings in ALSA channel (i) order */
  635. for (i = 0; i < ch_alloc->channels; i++) {
  636. while (!ch_alloc->speakers[7 - hdmi_slot] && !WARN_ON(hdmi_slot >= 8))
  637. hdmi_slot++; /* skip zero slots */
  638. hdmi_channel_mapping[ca][i] = (i << 4) | hdmi_slot++;
  639. }
  640. /* fill the rest of the slots with ALSA channel 0xf */
  641. for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++)
  642. if (!ch_alloc->speakers[7 - hdmi_slot])
  643. hdmi_channel_mapping[ca][i++] = (0xf << 4) | hdmi_slot;
  644. }
  645. if (non_pcm) {
  646. for (i = 0; i < ch_alloc->channels; i++)
  647. non_pcm_mapping[i] = (i << 4) | i;
  648. for (; i < 8; i++)
  649. non_pcm_mapping[i] = (0xf << 4) | i;
  650. }
  651. for (i = 0; i < 8; i++) {
  652. int slotsetup = non_pcm ? non_pcm_mapping[i] : hdmi_channel_mapping[ca][i];
  653. int hdmi_slot = slotsetup & 0x0f;
  654. int channel = (slotsetup & 0xf0) >> 4;
  655. err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot, channel);
  656. if (err) {
  657. codec_dbg(codec, "HDMI: channel mapping failed\n");
  658. break;
  659. }
  660. }
  661. }
  662. struct channel_map_table {
  663. unsigned char map; /* ALSA API channel map position */
  664. int spk_mask; /* speaker position bit mask */
  665. };
  666. static struct channel_map_table map_tables[] = {
  667. { SNDRV_CHMAP_FL, FL },
  668. { SNDRV_CHMAP_FR, FR },
  669. { SNDRV_CHMAP_RL, RL },
  670. { SNDRV_CHMAP_RR, RR },
  671. { SNDRV_CHMAP_LFE, LFE },
  672. { SNDRV_CHMAP_FC, FC },
  673. { SNDRV_CHMAP_RLC, RLC },
  674. { SNDRV_CHMAP_RRC, RRC },
  675. { SNDRV_CHMAP_RC, RC },
  676. { SNDRV_CHMAP_FLC, FLC },
  677. { SNDRV_CHMAP_FRC, FRC },
  678. { SNDRV_CHMAP_TFL, FLH },
  679. { SNDRV_CHMAP_TFR, FRH },
  680. { SNDRV_CHMAP_FLW, FLW },
  681. { SNDRV_CHMAP_FRW, FRW },
  682. { SNDRV_CHMAP_TC, TC },
  683. { SNDRV_CHMAP_TFC, FCH },
  684. {} /* terminator */
  685. };
  686. /* from ALSA API channel position to speaker bit mask */
  687. static int to_spk_mask(unsigned char c)
  688. {
  689. struct channel_map_table *t = map_tables;
  690. for (; t->map; t++) {
  691. if (t->map == c)
  692. return t->spk_mask;
  693. }
  694. return 0;
  695. }
  696. /* from ALSA API channel position to CEA slot */
  697. static int to_cea_slot(int ordered_ca, unsigned char pos)
  698. {
  699. int mask = to_spk_mask(pos);
  700. int i;
  701. if (mask) {
  702. for (i = 0; i < 8; i++) {
  703. if (channel_allocations[ordered_ca].speakers[7 - i] == mask)
  704. return i;
  705. }
  706. }
  707. return -1;
  708. }
  709. /* from speaker bit mask to ALSA API channel position */
  710. static int spk_to_chmap(int spk)
  711. {
  712. struct channel_map_table *t = map_tables;
  713. for (; t->map; t++) {
  714. if (t->spk_mask == spk)
  715. return t->map;
  716. }
  717. return 0;
  718. }
  719. /* from CEA slot to ALSA API channel position */
  720. static int from_cea_slot(int ordered_ca, unsigned char slot)
  721. {
  722. int mask = channel_allocations[ordered_ca].speakers[7 - slot];
  723. return spk_to_chmap(mask);
  724. }
  725. /* get the CA index corresponding to the given ALSA API channel map */
  726. static int hdmi_manual_channel_allocation(int chs, unsigned char *map)
  727. {
  728. int i, spks = 0, spk_mask = 0;
  729. for (i = 0; i < chs; i++) {
  730. int mask = to_spk_mask(map[i]);
  731. if (mask) {
  732. spk_mask |= mask;
  733. spks++;
  734. }
  735. }
  736. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++) {
  737. if ((chs == channel_allocations[i].channels ||
  738. spks == channel_allocations[i].channels) &&
  739. (spk_mask & channel_allocations[i].spk_mask) ==
  740. channel_allocations[i].spk_mask)
  741. return channel_allocations[i].ca_index;
  742. }
  743. return -1;
  744. }
  745. /* set up the channel slots for the given ALSA API channel map */
  746. static int hdmi_manual_setup_channel_mapping(struct hda_codec *codec,
  747. hda_nid_t pin_nid,
  748. int chs, unsigned char *map,
  749. int ca)
  750. {
  751. struct hdmi_spec *spec = codec->spec;
  752. int ordered_ca = get_channel_allocation_order(ca);
  753. int alsa_pos, hdmi_slot;
  754. int assignments[8] = {[0 ... 7] = 0xf};
  755. for (alsa_pos = 0; alsa_pos < chs; alsa_pos++) {
  756. hdmi_slot = to_cea_slot(ordered_ca, map[alsa_pos]);
  757. if (hdmi_slot < 0)
  758. continue; /* unassigned channel */
  759. assignments[hdmi_slot] = alsa_pos;
  760. }
  761. for (hdmi_slot = 0; hdmi_slot < 8; hdmi_slot++) {
  762. int err;
  763. err = spec->ops.pin_set_slot_channel(codec, pin_nid, hdmi_slot,
  764. assignments[hdmi_slot]);
  765. if (err)
  766. return -EINVAL;
  767. }
  768. return 0;
  769. }
  770. /* store ALSA API channel map from the current default map */
  771. static void hdmi_setup_fake_chmap(unsigned char *map, int ca)
  772. {
  773. int i;
  774. int ordered_ca = get_channel_allocation_order(ca);
  775. for (i = 0; i < 8; i++) {
  776. if (i < channel_allocations[ordered_ca].channels)
  777. map[i] = from_cea_slot(ordered_ca, hdmi_channel_mapping[ca][i] & 0x0f);
  778. else
  779. map[i] = 0;
  780. }
  781. }
  782. static void hdmi_setup_channel_mapping(struct hda_codec *codec,
  783. hda_nid_t pin_nid, bool non_pcm, int ca,
  784. int channels, unsigned char *map,
  785. bool chmap_set)
  786. {
  787. if (!non_pcm && chmap_set) {
  788. hdmi_manual_setup_channel_mapping(codec, pin_nid,
  789. channels, map, ca);
  790. } else {
  791. hdmi_std_setup_channel_mapping(codec, pin_nid, non_pcm, ca);
  792. hdmi_setup_fake_chmap(map, ca);
  793. }
  794. hdmi_debug_channel_mapping(codec, pin_nid);
  795. }
  796. static int hdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
  797. int asp_slot, int channel)
  798. {
  799. return snd_hda_codec_write(codec, pin_nid, 0,
  800. AC_VERB_SET_HDMI_CHAN_SLOT,
  801. (channel << 4) | asp_slot);
  802. }
  803. static int hdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
  804. int asp_slot)
  805. {
  806. return (snd_hda_codec_read(codec, pin_nid, 0,
  807. AC_VERB_GET_HDMI_CHAN_SLOT,
  808. asp_slot) & 0xf0) >> 4;
  809. }
  810. /*
  811. * Audio InfoFrame routines
  812. */
  813. /*
  814. * Enable Audio InfoFrame Transmission
  815. */
  816. static void hdmi_start_infoframe_trans(struct hda_codec *codec,
  817. hda_nid_t pin_nid)
  818. {
  819. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  820. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  821. AC_DIPXMIT_BEST);
  822. }
  823. /*
  824. * Disable Audio InfoFrame Transmission
  825. */
  826. static void hdmi_stop_infoframe_trans(struct hda_codec *codec,
  827. hda_nid_t pin_nid)
  828. {
  829. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  830. snd_hda_codec_write(codec, pin_nid, 0, AC_VERB_SET_HDMI_DIP_XMIT,
  831. AC_DIPXMIT_DISABLE);
  832. }
  833. static void hdmi_debug_dip_size(struct hda_codec *codec, hda_nid_t pin_nid)
  834. {
  835. #ifdef CONFIG_SND_DEBUG_VERBOSE
  836. int i;
  837. int size;
  838. size = snd_hdmi_get_eld_size(codec, pin_nid);
  839. codec_dbg(codec, "HDMI: ELD buf size is %d\n", size);
  840. for (i = 0; i < 8; i++) {
  841. size = snd_hda_codec_read(codec, pin_nid, 0,
  842. AC_VERB_GET_HDMI_DIP_SIZE, i);
  843. codec_dbg(codec, "HDMI: DIP GP[%d] buf size is %d\n", i, size);
  844. }
  845. #endif
  846. }
  847. static void hdmi_clear_dip_buffers(struct hda_codec *codec, hda_nid_t pin_nid)
  848. {
  849. #ifdef BE_PARANOID
  850. int i, j;
  851. int size;
  852. int pi, bi;
  853. for (i = 0; i < 8; i++) {
  854. size = snd_hda_codec_read(codec, pin_nid, 0,
  855. AC_VERB_GET_HDMI_DIP_SIZE, i);
  856. if (size == 0)
  857. continue;
  858. hdmi_set_dip_index(codec, pin_nid, i, 0x0);
  859. for (j = 1; j < 1000; j++) {
  860. hdmi_write_dip_byte(codec, pin_nid, 0x0);
  861. hdmi_get_dip_index(codec, pin_nid, &pi, &bi);
  862. if (pi != i)
  863. codec_dbg(codec, "dip index %d: %d != %d\n",
  864. bi, pi, i);
  865. if (bi == 0) /* byte index wrapped around */
  866. break;
  867. }
  868. codec_dbg(codec,
  869. "HDMI: DIP GP[%d] buf reported size=%d, written=%d\n",
  870. i, size, j);
  871. }
  872. #endif
  873. }
  874. static void hdmi_checksum_audio_infoframe(struct hdmi_audio_infoframe *hdmi_ai)
  875. {
  876. u8 *bytes = (u8 *)hdmi_ai;
  877. u8 sum = 0;
  878. int i;
  879. hdmi_ai->checksum = 0;
  880. for (i = 0; i < sizeof(*hdmi_ai); i++)
  881. sum += bytes[i];
  882. hdmi_ai->checksum = -sum;
  883. }
  884. static void hdmi_fill_audio_infoframe(struct hda_codec *codec,
  885. hda_nid_t pin_nid,
  886. u8 *dip, int size)
  887. {
  888. int i;
  889. hdmi_debug_dip_size(codec, pin_nid);
  890. hdmi_clear_dip_buffers(codec, pin_nid); /* be paranoid */
  891. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  892. for (i = 0; i < size; i++)
  893. hdmi_write_dip_byte(codec, pin_nid, dip[i]);
  894. }
  895. static bool hdmi_infoframe_uptodate(struct hda_codec *codec, hda_nid_t pin_nid,
  896. u8 *dip, int size)
  897. {
  898. u8 val;
  899. int i;
  900. if (snd_hda_codec_read(codec, pin_nid, 0, AC_VERB_GET_HDMI_DIP_XMIT, 0)
  901. != AC_DIPXMIT_BEST)
  902. return false;
  903. hdmi_set_dip_index(codec, pin_nid, 0x0, 0x0);
  904. for (i = 0; i < size; i++) {
  905. val = snd_hda_codec_read(codec, pin_nid, 0,
  906. AC_VERB_GET_HDMI_DIP_DATA, 0);
  907. if (val != dip[i])
  908. return false;
  909. }
  910. return true;
  911. }
  912. static void hdmi_pin_setup_infoframe(struct hda_codec *codec,
  913. hda_nid_t pin_nid,
  914. int ca, int active_channels,
  915. int conn_type)
  916. {
  917. union audio_infoframe ai;
  918. memset(&ai, 0, sizeof(ai));
  919. if (conn_type == 0) { /* HDMI */
  920. struct hdmi_audio_infoframe *hdmi_ai = &ai.hdmi;
  921. hdmi_ai->type = 0x84;
  922. hdmi_ai->ver = 0x01;
  923. hdmi_ai->len = 0x0a;
  924. hdmi_ai->CC02_CT47 = active_channels - 1;
  925. hdmi_ai->CA = ca;
  926. hdmi_checksum_audio_infoframe(hdmi_ai);
  927. } else if (conn_type == 1) { /* DisplayPort */
  928. struct dp_audio_infoframe *dp_ai = &ai.dp;
  929. dp_ai->type = 0x84;
  930. dp_ai->len = 0x1b;
  931. dp_ai->ver = 0x11 << 2;
  932. dp_ai->CC02_CT47 = active_channels - 1;
  933. dp_ai->CA = ca;
  934. } else {
  935. codec_dbg(codec, "HDMI: unknown connection type at pin %d\n",
  936. pin_nid);
  937. return;
  938. }
  939. /*
  940. * sizeof(ai) is used instead of sizeof(*hdmi_ai) or
  941. * sizeof(*dp_ai) to avoid partial match/update problems when
  942. * the user switches between HDMI/DP monitors.
  943. */
  944. if (!hdmi_infoframe_uptodate(codec, pin_nid, ai.bytes,
  945. sizeof(ai))) {
  946. codec_dbg(codec,
  947. "hdmi_pin_setup_infoframe: pin=%d channels=%d ca=0x%02x\n",
  948. pin_nid,
  949. active_channels, ca);
  950. hdmi_stop_infoframe_trans(codec, pin_nid);
  951. hdmi_fill_audio_infoframe(codec, pin_nid,
  952. ai.bytes, sizeof(ai));
  953. hdmi_start_infoframe_trans(codec, pin_nid);
  954. }
  955. }
  956. static void hdmi_setup_audio_infoframe(struct hda_codec *codec,
  957. struct hdmi_spec_per_pin *per_pin,
  958. bool non_pcm)
  959. {
  960. struct hdmi_spec *spec = codec->spec;
  961. hda_nid_t pin_nid = per_pin->pin_nid;
  962. int channels = per_pin->channels;
  963. int active_channels;
  964. struct hdmi_eld *eld;
  965. int ca, ordered_ca;
  966. if (!channels)
  967. return;
  968. if (is_haswell_plus(codec))
  969. snd_hda_codec_write(codec, pin_nid, 0,
  970. AC_VERB_SET_AMP_GAIN_MUTE,
  971. AMP_OUT_UNMUTE);
  972. eld = &per_pin->sink_eld;
  973. if (!non_pcm && per_pin->chmap_set)
  974. ca = hdmi_manual_channel_allocation(channels, per_pin->chmap);
  975. else
  976. ca = hdmi_channel_allocation(eld, channels);
  977. if (ca < 0)
  978. ca = 0;
  979. ordered_ca = get_channel_allocation_order(ca);
  980. active_channels = channel_allocations[ordered_ca].channels;
  981. hdmi_set_channel_count(codec, per_pin->cvt_nid, active_channels);
  982. /*
  983. * always configure channel mapping, it may have been changed by the
  984. * user in the meantime
  985. */
  986. hdmi_setup_channel_mapping(codec, pin_nid, non_pcm, ca,
  987. channels, per_pin->chmap,
  988. per_pin->chmap_set);
  989. spec->ops.pin_setup_infoframe(codec, pin_nid, ca, active_channels,
  990. eld->info.conn_type);
  991. per_pin->non_pcm = non_pcm;
  992. }
  993. /*
  994. * Unsolicited events
  995. */
  996. static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll);
  997. static void jack_callback(struct hda_codec *codec, struct hda_jack_tbl *jack)
  998. {
  999. struct hdmi_spec *spec = codec->spec;
  1000. int pin_idx = pin_nid_to_pin_index(codec, jack->nid);
  1001. if (pin_idx < 0)
  1002. return;
  1003. if (hdmi_present_sense(get_pin(spec, pin_idx), 1))
  1004. snd_hda_jack_report_sync(codec);
  1005. }
  1006. static void hdmi_intrinsic_event(struct hda_codec *codec, unsigned int res)
  1007. {
  1008. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  1009. struct hda_jack_tbl *jack;
  1010. int dev_entry = (res & AC_UNSOL_RES_DE) >> AC_UNSOL_RES_DE_SHIFT;
  1011. jack = snd_hda_jack_tbl_get_from_tag(codec, tag);
  1012. if (!jack)
  1013. return;
  1014. jack->jack_dirty = 1;
  1015. codec_dbg(codec,
  1016. "HDMI hot plug event: Codec=%d Pin=%d Device=%d Inactive=%d Presence_Detect=%d ELD_Valid=%d\n",
  1017. codec->addr, jack->nid, dev_entry, !!(res & AC_UNSOL_RES_IA),
  1018. !!(res & AC_UNSOL_RES_PD), !!(res & AC_UNSOL_RES_ELDV));
  1019. jack_callback(codec, jack);
  1020. }
  1021. static void hdmi_non_intrinsic_event(struct hda_codec *codec, unsigned int res)
  1022. {
  1023. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  1024. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  1025. int cp_state = !!(res & AC_UNSOL_RES_CP_STATE);
  1026. int cp_ready = !!(res & AC_UNSOL_RES_CP_READY);
  1027. codec_info(codec,
  1028. "HDMI CP event: CODEC=%d TAG=%d SUBTAG=0x%x CP_STATE=%d CP_READY=%d\n",
  1029. codec->addr,
  1030. tag,
  1031. subtag,
  1032. cp_state,
  1033. cp_ready);
  1034. /* TODO */
  1035. if (cp_state)
  1036. ;
  1037. if (cp_ready)
  1038. ;
  1039. }
  1040. static void hdmi_unsol_event(struct hda_codec *codec, unsigned int res)
  1041. {
  1042. int tag = res >> AC_UNSOL_RES_TAG_SHIFT;
  1043. int subtag = (res & AC_UNSOL_RES_SUBTAG) >> AC_UNSOL_RES_SUBTAG_SHIFT;
  1044. if (!snd_hda_jack_tbl_get_from_tag(codec, tag)) {
  1045. codec_dbg(codec, "Unexpected HDMI event tag 0x%x\n", tag);
  1046. return;
  1047. }
  1048. if (subtag == 0)
  1049. hdmi_intrinsic_event(codec, res);
  1050. else
  1051. hdmi_non_intrinsic_event(codec, res);
  1052. }
  1053. static void haswell_verify_D0(struct hda_codec *codec,
  1054. hda_nid_t cvt_nid, hda_nid_t nid)
  1055. {
  1056. int pwr;
  1057. /* For Haswell, the converter 1/2 may keep in D3 state after bootup,
  1058. * thus pins could only choose converter 0 for use. Make sure the
  1059. * converters are in correct power state */
  1060. if (!snd_hda_check_power_state(codec, cvt_nid, AC_PWRST_D0))
  1061. snd_hda_codec_write(codec, cvt_nid, 0, AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
  1062. if (!snd_hda_check_power_state(codec, nid, AC_PWRST_D0)) {
  1063. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE,
  1064. AC_PWRST_D0);
  1065. msleep(40);
  1066. pwr = snd_hda_codec_read(codec, nid, 0, AC_VERB_GET_POWER_STATE, 0);
  1067. pwr = (pwr & AC_PWRST_ACTUAL) >> AC_PWRST_ACTUAL_SHIFT;
  1068. codec_dbg(codec, "Haswell HDMI audio: Power for pin 0x%x is now D%d\n", nid, pwr);
  1069. }
  1070. }
  1071. /*
  1072. * Callbacks
  1073. */
  1074. /* HBR should be Non-PCM, 8 channels */
  1075. #define is_hbr_format(format) \
  1076. ((format & AC_FMT_TYPE_NON_PCM) && (format & AC_FMT_CHAN_MASK) == 7)
  1077. static int hdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
  1078. bool hbr)
  1079. {
  1080. int pinctl, new_pinctl;
  1081. if (snd_hda_query_pin_caps(codec, pin_nid) & AC_PINCAP_HBR) {
  1082. pinctl = snd_hda_codec_read(codec, pin_nid, 0,
  1083. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1084. if (pinctl < 0)
  1085. return hbr ? -EINVAL : 0;
  1086. new_pinctl = pinctl & ~AC_PINCTL_EPT;
  1087. if (hbr)
  1088. new_pinctl |= AC_PINCTL_EPT_HBR;
  1089. else
  1090. new_pinctl |= AC_PINCTL_EPT_NATIVE;
  1091. codec_dbg(codec,
  1092. "hdmi_pin_hbr_setup: NID=0x%x, %spinctl=0x%x\n",
  1093. pin_nid,
  1094. pinctl == new_pinctl ? "" : "new-",
  1095. new_pinctl);
  1096. if (pinctl != new_pinctl)
  1097. snd_hda_codec_write(codec, pin_nid, 0,
  1098. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1099. new_pinctl);
  1100. } else if (hbr)
  1101. return -EINVAL;
  1102. return 0;
  1103. }
  1104. static int hdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
  1105. hda_nid_t pin_nid, u32 stream_tag, int format)
  1106. {
  1107. struct hdmi_spec *spec = codec->spec;
  1108. int err;
  1109. if (is_haswell_plus(codec))
  1110. haswell_verify_D0(codec, cvt_nid, pin_nid);
  1111. err = spec->ops.pin_hbr_setup(codec, pin_nid, is_hbr_format(format));
  1112. if (err) {
  1113. codec_dbg(codec, "hdmi_setup_stream: HBR is not supported\n");
  1114. return err;
  1115. }
  1116. snd_hda_codec_setup_stream(codec, cvt_nid, stream_tag, 0, format);
  1117. return 0;
  1118. }
  1119. static int hdmi_choose_cvt(struct hda_codec *codec,
  1120. int pin_idx, int *cvt_id, int *mux_id)
  1121. {
  1122. struct hdmi_spec *spec = codec->spec;
  1123. struct hdmi_spec_per_pin *per_pin;
  1124. struct hdmi_spec_per_cvt *per_cvt = NULL;
  1125. int cvt_idx, mux_idx = 0;
  1126. per_pin = get_pin(spec, pin_idx);
  1127. /* Dynamically assign converter to stream */
  1128. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
  1129. per_cvt = get_cvt(spec, cvt_idx);
  1130. /* Must not already be assigned */
  1131. if (per_cvt->assigned)
  1132. continue;
  1133. /* Must be in pin's mux's list of converters */
  1134. for (mux_idx = 0; mux_idx < per_pin->num_mux_nids; mux_idx++)
  1135. if (per_pin->mux_nids[mux_idx] == per_cvt->cvt_nid)
  1136. break;
  1137. /* Not in mux list */
  1138. if (mux_idx == per_pin->num_mux_nids)
  1139. continue;
  1140. break;
  1141. }
  1142. /* No free converters */
  1143. if (cvt_idx == spec->num_cvts)
  1144. return -ENODEV;
  1145. per_pin->mux_idx = mux_idx;
  1146. if (cvt_id)
  1147. *cvt_id = cvt_idx;
  1148. if (mux_id)
  1149. *mux_id = mux_idx;
  1150. return 0;
  1151. }
  1152. /* Assure the pin select the right convetor */
  1153. static void intel_verify_pin_cvt_connect(struct hda_codec *codec,
  1154. struct hdmi_spec_per_pin *per_pin)
  1155. {
  1156. hda_nid_t pin_nid = per_pin->pin_nid;
  1157. int mux_idx, curr;
  1158. mux_idx = per_pin->mux_idx;
  1159. curr = snd_hda_codec_read(codec, pin_nid, 0,
  1160. AC_VERB_GET_CONNECT_SEL, 0);
  1161. if (curr != mux_idx)
  1162. snd_hda_codec_write_cache(codec, pin_nid, 0,
  1163. AC_VERB_SET_CONNECT_SEL,
  1164. mux_idx);
  1165. }
  1166. /* Intel HDMI workaround to fix audio routing issue:
  1167. * For some Intel display codecs, pins share the same connection list.
  1168. * So a conveter can be selected by multiple pins and playback on any of these
  1169. * pins will generate sound on the external display, because audio flows from
  1170. * the same converter to the display pipeline. Also muting one pin may make
  1171. * other pins have no sound output.
  1172. * So this function assures that an assigned converter for a pin is not selected
  1173. * by any other pins.
  1174. */
  1175. static void intel_not_share_assigned_cvt(struct hda_codec *codec,
  1176. hda_nid_t pin_nid, int mux_idx)
  1177. {
  1178. struct hdmi_spec *spec = codec->spec;
  1179. hda_nid_t nid, end_nid;
  1180. int cvt_idx, curr;
  1181. struct hdmi_spec_per_cvt *per_cvt;
  1182. /* configure all pins, including "no physical connection" ones */
  1183. end_nid = codec->start_nid + codec->num_nodes;
  1184. for (nid = codec->start_nid; nid < end_nid; nid++) {
  1185. unsigned int wid_caps = get_wcaps(codec, nid);
  1186. unsigned int wid_type = get_wcaps_type(wid_caps);
  1187. if (wid_type != AC_WID_PIN)
  1188. continue;
  1189. if (nid == pin_nid)
  1190. continue;
  1191. curr = snd_hda_codec_read(codec, nid, 0,
  1192. AC_VERB_GET_CONNECT_SEL, 0);
  1193. if (curr != mux_idx)
  1194. continue;
  1195. /* choose an unassigned converter. The conveters in the
  1196. * connection list are in the same order as in the codec.
  1197. */
  1198. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
  1199. per_cvt = get_cvt(spec, cvt_idx);
  1200. if (!per_cvt->assigned) {
  1201. codec_dbg(codec,
  1202. "choose cvt %d for pin nid %d\n",
  1203. cvt_idx, nid);
  1204. snd_hda_codec_write_cache(codec, nid, 0,
  1205. AC_VERB_SET_CONNECT_SEL,
  1206. cvt_idx);
  1207. break;
  1208. }
  1209. }
  1210. }
  1211. }
  1212. /*
  1213. * HDA PCM callbacks
  1214. */
  1215. static int hdmi_pcm_open(struct hda_pcm_stream *hinfo,
  1216. struct hda_codec *codec,
  1217. struct snd_pcm_substream *substream)
  1218. {
  1219. struct hdmi_spec *spec = codec->spec;
  1220. struct snd_pcm_runtime *runtime = substream->runtime;
  1221. int pin_idx, cvt_idx, mux_idx = 0;
  1222. struct hdmi_spec_per_pin *per_pin;
  1223. struct hdmi_eld *eld;
  1224. struct hdmi_spec_per_cvt *per_cvt = NULL;
  1225. int err;
  1226. /* Validate hinfo */
  1227. pin_idx = hinfo_to_pin_index(codec, hinfo);
  1228. if (snd_BUG_ON(pin_idx < 0))
  1229. return -EINVAL;
  1230. per_pin = get_pin(spec, pin_idx);
  1231. eld = &per_pin->sink_eld;
  1232. err = hdmi_choose_cvt(codec, pin_idx, &cvt_idx, &mux_idx);
  1233. if (err < 0)
  1234. return err;
  1235. per_cvt = get_cvt(spec, cvt_idx);
  1236. /* Claim converter */
  1237. per_cvt->assigned = 1;
  1238. per_pin->cvt_nid = per_cvt->cvt_nid;
  1239. hinfo->nid = per_cvt->cvt_nid;
  1240. snd_hda_codec_write_cache(codec, per_pin->pin_nid, 0,
  1241. AC_VERB_SET_CONNECT_SEL,
  1242. mux_idx);
  1243. /* configure unused pins to choose other converters */
  1244. if (is_haswell_plus(codec) || is_valleyview(codec))
  1245. intel_not_share_assigned_cvt(codec, per_pin->pin_nid, mux_idx);
  1246. snd_hda_spdif_ctls_assign(codec, pin_idx, per_cvt->cvt_nid);
  1247. /* Initially set the converter's capabilities */
  1248. hinfo->channels_min = per_cvt->channels_min;
  1249. hinfo->channels_max = per_cvt->channels_max;
  1250. hinfo->rates = per_cvt->rates;
  1251. hinfo->formats = per_cvt->formats;
  1252. hinfo->maxbps = per_cvt->maxbps;
  1253. /* Restrict capabilities by ELD if this isn't disabled */
  1254. if (!static_hdmi_pcm && eld->eld_valid) {
  1255. snd_hdmi_eld_update_pcm_info(&eld->info, hinfo);
  1256. if (hinfo->channels_min > hinfo->channels_max ||
  1257. !hinfo->rates || !hinfo->formats) {
  1258. per_cvt->assigned = 0;
  1259. hinfo->nid = 0;
  1260. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1261. return -ENODEV;
  1262. }
  1263. }
  1264. /* Store the updated parameters */
  1265. runtime->hw.channels_min = hinfo->channels_min;
  1266. runtime->hw.channels_max = hinfo->channels_max;
  1267. runtime->hw.formats = hinfo->formats;
  1268. runtime->hw.rates = hinfo->rates;
  1269. snd_pcm_hw_constraint_step(substream->runtime, 0,
  1270. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  1271. return 0;
  1272. }
  1273. /*
  1274. * HDA/HDMI auto parsing
  1275. */
  1276. static int hdmi_read_pin_conn(struct hda_codec *codec, int pin_idx)
  1277. {
  1278. struct hdmi_spec *spec = codec->spec;
  1279. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1280. hda_nid_t pin_nid = per_pin->pin_nid;
  1281. if (!(get_wcaps(codec, pin_nid) & AC_WCAP_CONN_LIST)) {
  1282. codec_warn(codec,
  1283. "HDMI: pin %d wcaps %#x does not support connection list\n",
  1284. pin_nid, get_wcaps(codec, pin_nid));
  1285. return -EINVAL;
  1286. }
  1287. per_pin->num_mux_nids = snd_hda_get_connections(codec, pin_nid,
  1288. per_pin->mux_nids,
  1289. HDA_MAX_CONNECTIONS);
  1290. return 0;
  1291. }
  1292. static bool hdmi_present_sense(struct hdmi_spec_per_pin *per_pin, int repoll)
  1293. {
  1294. struct hda_jack_tbl *jack;
  1295. struct hda_codec *codec = per_pin->codec;
  1296. struct hdmi_spec *spec = codec->spec;
  1297. struct hdmi_eld *eld = &spec->temp_eld;
  1298. struct hdmi_eld *pin_eld = &per_pin->sink_eld;
  1299. hda_nid_t pin_nid = per_pin->pin_nid;
  1300. /*
  1301. * Always execute a GetPinSense verb here, even when called from
  1302. * hdmi_intrinsic_event; for some NVIDIA HW, the unsolicited
  1303. * response's PD bit is not the real PD value, but indicates that
  1304. * the real PD value changed. An older version of the HD-audio
  1305. * specification worked this way. Hence, we just ignore the data in
  1306. * the unsolicited response to avoid custom WARs.
  1307. */
  1308. int present;
  1309. bool update_eld = false;
  1310. bool eld_changed = false;
  1311. bool ret;
  1312. snd_hda_power_up(codec);
  1313. present = snd_hda_pin_sense(codec, pin_nid);
  1314. mutex_lock(&per_pin->lock);
  1315. pin_eld->monitor_present = !!(present & AC_PINSENSE_PRESENCE);
  1316. if (pin_eld->monitor_present)
  1317. eld->eld_valid = !!(present & AC_PINSENSE_ELDV);
  1318. else
  1319. eld->eld_valid = false;
  1320. codec_dbg(codec,
  1321. "HDMI status: Codec=%d Pin=%d Presence_Detect=%d ELD_Valid=%d\n",
  1322. codec->addr, pin_nid, pin_eld->monitor_present, eld->eld_valid);
  1323. if (eld->eld_valid) {
  1324. if (spec->ops.pin_get_eld(codec, pin_nid, eld->eld_buffer,
  1325. &eld->eld_size) < 0)
  1326. eld->eld_valid = false;
  1327. else {
  1328. memset(&eld->info, 0, sizeof(struct parsed_hdmi_eld));
  1329. if (snd_hdmi_parse_eld(&eld->info, eld->eld_buffer,
  1330. eld->eld_size) < 0)
  1331. eld->eld_valid = false;
  1332. }
  1333. if (eld->eld_valid) {
  1334. snd_hdmi_show_eld(&eld->info);
  1335. update_eld = true;
  1336. }
  1337. else if (repoll) {
  1338. queue_delayed_work(codec->bus->workq,
  1339. &per_pin->work,
  1340. msecs_to_jiffies(300));
  1341. goto unlock;
  1342. }
  1343. }
  1344. if (pin_eld->eld_valid && !eld->eld_valid) {
  1345. update_eld = true;
  1346. eld_changed = true;
  1347. }
  1348. if (update_eld) {
  1349. bool old_eld_valid = pin_eld->eld_valid;
  1350. pin_eld->eld_valid = eld->eld_valid;
  1351. eld_changed = pin_eld->eld_size != eld->eld_size ||
  1352. memcmp(pin_eld->eld_buffer, eld->eld_buffer,
  1353. eld->eld_size) != 0;
  1354. if (eld_changed)
  1355. memcpy(pin_eld->eld_buffer, eld->eld_buffer,
  1356. eld->eld_size);
  1357. pin_eld->eld_size = eld->eld_size;
  1358. pin_eld->info = eld->info;
  1359. /*
  1360. * Re-setup pin and infoframe. This is needed e.g. when
  1361. * - sink is first plugged-in (infoframe is not set up if !monitor_present)
  1362. * - transcoder can change during stream playback on Haswell
  1363. * and this can make HW reset converter selection on a pin.
  1364. */
  1365. if (eld->eld_valid && !old_eld_valid && per_pin->setup) {
  1366. if (is_haswell_plus(codec) || is_valleyview(codec)) {
  1367. intel_verify_pin_cvt_connect(codec, per_pin);
  1368. intel_not_share_assigned_cvt(codec, pin_nid,
  1369. per_pin->mux_idx);
  1370. }
  1371. hdmi_setup_audio_infoframe(codec, per_pin,
  1372. per_pin->non_pcm);
  1373. }
  1374. }
  1375. if (eld_changed)
  1376. snd_ctl_notify(codec->bus->card,
  1377. SNDRV_CTL_EVENT_MASK_VALUE | SNDRV_CTL_EVENT_MASK_INFO,
  1378. &per_pin->eld_ctl->id);
  1379. unlock:
  1380. ret = !repoll || !pin_eld->monitor_present || pin_eld->eld_valid;
  1381. jack = snd_hda_jack_tbl_get(codec, pin_nid);
  1382. if (jack)
  1383. jack->block_report = !ret;
  1384. mutex_unlock(&per_pin->lock);
  1385. snd_hda_power_down(codec);
  1386. return ret;
  1387. }
  1388. static void hdmi_repoll_eld(struct work_struct *work)
  1389. {
  1390. struct hdmi_spec_per_pin *per_pin =
  1391. container_of(to_delayed_work(work), struct hdmi_spec_per_pin, work);
  1392. if (per_pin->repoll_count++ > 6)
  1393. per_pin->repoll_count = 0;
  1394. if (hdmi_present_sense(per_pin, per_pin->repoll_count))
  1395. snd_hda_jack_report_sync(per_pin->codec);
  1396. }
  1397. static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
  1398. hda_nid_t nid);
  1399. static int hdmi_add_pin(struct hda_codec *codec, hda_nid_t pin_nid)
  1400. {
  1401. struct hdmi_spec *spec = codec->spec;
  1402. unsigned int caps, config;
  1403. int pin_idx;
  1404. struct hdmi_spec_per_pin *per_pin;
  1405. int err;
  1406. caps = snd_hda_query_pin_caps(codec, pin_nid);
  1407. if (!(caps & (AC_PINCAP_HDMI | AC_PINCAP_DP)))
  1408. return 0;
  1409. config = snd_hda_codec_get_pincfg(codec, pin_nid);
  1410. if (get_defcfg_connect(config) == AC_JACK_PORT_NONE)
  1411. return 0;
  1412. if (is_haswell_plus(codec))
  1413. intel_haswell_fixup_connect_list(codec, pin_nid);
  1414. pin_idx = spec->num_pins;
  1415. per_pin = snd_array_new(&spec->pins);
  1416. if (!per_pin)
  1417. return -ENOMEM;
  1418. per_pin->pin_nid = pin_nid;
  1419. per_pin->non_pcm = false;
  1420. err = hdmi_read_pin_conn(codec, pin_idx);
  1421. if (err < 0)
  1422. return err;
  1423. spec->num_pins++;
  1424. return 0;
  1425. }
  1426. static int hdmi_add_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
  1427. {
  1428. struct hdmi_spec *spec = codec->spec;
  1429. struct hdmi_spec_per_cvt *per_cvt;
  1430. unsigned int chans;
  1431. int err;
  1432. chans = get_wcaps(codec, cvt_nid);
  1433. chans = get_wcaps_channels(chans);
  1434. per_cvt = snd_array_new(&spec->cvts);
  1435. if (!per_cvt)
  1436. return -ENOMEM;
  1437. per_cvt->cvt_nid = cvt_nid;
  1438. per_cvt->channels_min = 2;
  1439. if (chans <= 16) {
  1440. per_cvt->channels_max = chans;
  1441. if (chans > spec->channels_max)
  1442. spec->channels_max = chans;
  1443. }
  1444. err = snd_hda_query_supported_pcm(codec, cvt_nid,
  1445. &per_cvt->rates,
  1446. &per_cvt->formats,
  1447. &per_cvt->maxbps);
  1448. if (err < 0)
  1449. return err;
  1450. if (spec->num_cvts < ARRAY_SIZE(spec->cvt_nids))
  1451. spec->cvt_nids[spec->num_cvts] = cvt_nid;
  1452. spec->num_cvts++;
  1453. return 0;
  1454. }
  1455. static int hdmi_parse_codec(struct hda_codec *codec)
  1456. {
  1457. hda_nid_t nid;
  1458. int i, nodes;
  1459. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid);
  1460. if (!nid || nodes < 0) {
  1461. codec_warn(codec, "HDMI: failed to get afg sub nodes\n");
  1462. return -EINVAL;
  1463. }
  1464. for (i = 0; i < nodes; i++, nid++) {
  1465. unsigned int caps;
  1466. unsigned int type;
  1467. caps = get_wcaps(codec, nid);
  1468. type = get_wcaps_type(caps);
  1469. if (!(caps & AC_WCAP_DIGITAL))
  1470. continue;
  1471. switch (type) {
  1472. case AC_WID_AUD_OUT:
  1473. hdmi_add_cvt(codec, nid);
  1474. break;
  1475. case AC_WID_PIN:
  1476. hdmi_add_pin(codec, nid);
  1477. break;
  1478. }
  1479. }
  1480. return 0;
  1481. }
  1482. /*
  1483. */
  1484. static bool check_non_pcm_per_cvt(struct hda_codec *codec, hda_nid_t cvt_nid)
  1485. {
  1486. struct hda_spdif_out *spdif;
  1487. bool non_pcm;
  1488. mutex_lock(&codec->spdif_mutex);
  1489. spdif = snd_hda_spdif_out_of_nid(codec, cvt_nid);
  1490. non_pcm = !!(spdif->status & IEC958_AES0_NONAUDIO);
  1491. mutex_unlock(&codec->spdif_mutex);
  1492. return non_pcm;
  1493. }
  1494. /*
  1495. * HDMI callbacks
  1496. */
  1497. static int generic_hdmi_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1498. struct hda_codec *codec,
  1499. unsigned int stream_tag,
  1500. unsigned int format,
  1501. struct snd_pcm_substream *substream)
  1502. {
  1503. hda_nid_t cvt_nid = hinfo->nid;
  1504. struct hdmi_spec *spec = codec->spec;
  1505. int pin_idx = hinfo_to_pin_index(codec, hinfo);
  1506. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1507. hda_nid_t pin_nid = per_pin->pin_nid;
  1508. bool non_pcm;
  1509. int pinctl;
  1510. if (is_haswell_plus(codec) || is_valleyview(codec)) {
  1511. /* Verify pin:cvt selections to avoid silent audio after S3.
  1512. * After S3, the audio driver restores pin:cvt selections
  1513. * but this can happen before gfx is ready and such selection
  1514. * is overlooked by HW. Thus multiple pins can share a same
  1515. * default convertor and mute control will affect each other,
  1516. * which can cause a resumed audio playback become silent
  1517. * after S3.
  1518. */
  1519. intel_verify_pin_cvt_connect(codec, per_pin);
  1520. intel_not_share_assigned_cvt(codec, pin_nid, per_pin->mux_idx);
  1521. }
  1522. non_pcm = check_non_pcm_per_cvt(codec, cvt_nid);
  1523. mutex_lock(&per_pin->lock);
  1524. per_pin->channels = substream->runtime->channels;
  1525. per_pin->setup = true;
  1526. hdmi_setup_audio_infoframe(codec, per_pin, non_pcm);
  1527. mutex_unlock(&per_pin->lock);
  1528. if (spec->dyn_pin_out) {
  1529. pinctl = snd_hda_codec_read(codec, pin_nid, 0,
  1530. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1531. snd_hda_codec_write(codec, pin_nid, 0,
  1532. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1533. pinctl | PIN_OUT);
  1534. }
  1535. return spec->ops.setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
  1536. }
  1537. static int generic_hdmi_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  1538. struct hda_codec *codec,
  1539. struct snd_pcm_substream *substream)
  1540. {
  1541. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  1542. return 0;
  1543. }
  1544. static int hdmi_pcm_close(struct hda_pcm_stream *hinfo,
  1545. struct hda_codec *codec,
  1546. struct snd_pcm_substream *substream)
  1547. {
  1548. struct hdmi_spec *spec = codec->spec;
  1549. int cvt_idx, pin_idx;
  1550. struct hdmi_spec_per_cvt *per_cvt;
  1551. struct hdmi_spec_per_pin *per_pin;
  1552. int pinctl;
  1553. if (hinfo->nid) {
  1554. cvt_idx = cvt_nid_to_cvt_index(codec, hinfo->nid);
  1555. if (snd_BUG_ON(cvt_idx < 0))
  1556. return -EINVAL;
  1557. per_cvt = get_cvt(spec, cvt_idx);
  1558. snd_BUG_ON(!per_cvt->assigned);
  1559. per_cvt->assigned = 0;
  1560. hinfo->nid = 0;
  1561. pin_idx = hinfo_to_pin_index(codec, hinfo);
  1562. if (snd_BUG_ON(pin_idx < 0))
  1563. return -EINVAL;
  1564. per_pin = get_pin(spec, pin_idx);
  1565. if (spec->dyn_pin_out) {
  1566. pinctl = snd_hda_codec_read(codec, per_pin->pin_nid, 0,
  1567. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1568. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  1569. AC_VERB_SET_PIN_WIDGET_CONTROL,
  1570. pinctl & ~PIN_OUT);
  1571. }
  1572. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1573. mutex_lock(&per_pin->lock);
  1574. per_pin->chmap_set = false;
  1575. memset(per_pin->chmap, 0, sizeof(per_pin->chmap));
  1576. per_pin->setup = false;
  1577. per_pin->channels = 0;
  1578. mutex_unlock(&per_pin->lock);
  1579. }
  1580. return 0;
  1581. }
  1582. static const struct hda_pcm_ops generic_ops = {
  1583. .open = hdmi_pcm_open,
  1584. .close = hdmi_pcm_close,
  1585. .prepare = generic_hdmi_playback_pcm_prepare,
  1586. .cleanup = generic_hdmi_playback_pcm_cleanup,
  1587. };
  1588. /*
  1589. * ALSA API channel-map control callbacks
  1590. */
  1591. static int hdmi_chmap_ctl_info(struct snd_kcontrol *kcontrol,
  1592. struct snd_ctl_elem_info *uinfo)
  1593. {
  1594. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1595. struct hda_codec *codec = info->private_data;
  1596. struct hdmi_spec *spec = codec->spec;
  1597. uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  1598. uinfo->count = spec->channels_max;
  1599. uinfo->value.integer.min = 0;
  1600. uinfo->value.integer.max = SNDRV_CHMAP_LAST;
  1601. return 0;
  1602. }
  1603. static int hdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
  1604. int channels)
  1605. {
  1606. /* If the speaker allocation matches the channel count, it is OK.*/
  1607. if (cap->channels != channels)
  1608. return -1;
  1609. /* all channels are remappable freely */
  1610. return SNDRV_CTL_TLVT_CHMAP_VAR;
  1611. }
  1612. static void hdmi_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
  1613. unsigned int *chmap, int channels)
  1614. {
  1615. int count = 0;
  1616. int c;
  1617. for (c = 7; c >= 0; c--) {
  1618. int spk = cap->speakers[c];
  1619. if (!spk)
  1620. continue;
  1621. chmap[count++] = spk_to_chmap(spk);
  1622. }
  1623. WARN_ON(count != channels);
  1624. }
  1625. static int hdmi_chmap_ctl_tlv(struct snd_kcontrol *kcontrol, int op_flag,
  1626. unsigned int size, unsigned int __user *tlv)
  1627. {
  1628. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1629. struct hda_codec *codec = info->private_data;
  1630. struct hdmi_spec *spec = codec->spec;
  1631. unsigned int __user *dst;
  1632. int chs, count = 0;
  1633. if (size < 8)
  1634. return -ENOMEM;
  1635. if (put_user(SNDRV_CTL_TLVT_CONTAINER, tlv))
  1636. return -EFAULT;
  1637. size -= 8;
  1638. dst = tlv + 2;
  1639. for (chs = 2; chs <= spec->channels_max; chs++) {
  1640. int i;
  1641. struct cea_channel_speaker_allocation *cap;
  1642. cap = channel_allocations;
  1643. for (i = 0; i < ARRAY_SIZE(channel_allocations); i++, cap++) {
  1644. int chs_bytes = chs * 4;
  1645. int type = spec->ops.chmap_cea_alloc_validate_get_type(cap, chs);
  1646. unsigned int tlv_chmap[8];
  1647. if (type < 0)
  1648. continue;
  1649. if (size < 8)
  1650. return -ENOMEM;
  1651. if (put_user(type, dst) ||
  1652. put_user(chs_bytes, dst + 1))
  1653. return -EFAULT;
  1654. dst += 2;
  1655. size -= 8;
  1656. count += 8;
  1657. if (size < chs_bytes)
  1658. return -ENOMEM;
  1659. size -= chs_bytes;
  1660. count += chs_bytes;
  1661. spec->ops.cea_alloc_to_tlv_chmap(cap, tlv_chmap, chs);
  1662. if (copy_to_user(dst, tlv_chmap, chs_bytes))
  1663. return -EFAULT;
  1664. dst += chs;
  1665. }
  1666. }
  1667. if (put_user(count, tlv + 1))
  1668. return -EFAULT;
  1669. return 0;
  1670. }
  1671. static int hdmi_chmap_ctl_get(struct snd_kcontrol *kcontrol,
  1672. struct snd_ctl_elem_value *ucontrol)
  1673. {
  1674. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1675. struct hda_codec *codec = info->private_data;
  1676. struct hdmi_spec *spec = codec->spec;
  1677. int pin_idx = kcontrol->private_value;
  1678. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1679. int i;
  1680. for (i = 0; i < ARRAY_SIZE(per_pin->chmap); i++)
  1681. ucontrol->value.integer.value[i] = per_pin->chmap[i];
  1682. return 0;
  1683. }
  1684. static int hdmi_chmap_ctl_put(struct snd_kcontrol *kcontrol,
  1685. struct snd_ctl_elem_value *ucontrol)
  1686. {
  1687. struct snd_pcm_chmap *info = snd_kcontrol_chip(kcontrol);
  1688. struct hda_codec *codec = info->private_data;
  1689. struct hdmi_spec *spec = codec->spec;
  1690. int pin_idx = kcontrol->private_value;
  1691. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1692. unsigned int ctl_idx;
  1693. struct snd_pcm_substream *substream;
  1694. unsigned char chmap[8];
  1695. int i, err, ca, prepared = 0;
  1696. ctl_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1697. substream = snd_pcm_chmap_substream(info, ctl_idx);
  1698. if (!substream || !substream->runtime)
  1699. return 0; /* just for avoiding error from alsactl restore */
  1700. switch (substream->runtime->status->state) {
  1701. case SNDRV_PCM_STATE_OPEN:
  1702. case SNDRV_PCM_STATE_SETUP:
  1703. break;
  1704. case SNDRV_PCM_STATE_PREPARED:
  1705. prepared = 1;
  1706. break;
  1707. default:
  1708. return -EBUSY;
  1709. }
  1710. memset(chmap, 0, sizeof(chmap));
  1711. for (i = 0; i < ARRAY_SIZE(chmap); i++)
  1712. chmap[i] = ucontrol->value.integer.value[i];
  1713. if (!memcmp(chmap, per_pin->chmap, sizeof(chmap)))
  1714. return 0;
  1715. ca = hdmi_manual_channel_allocation(ARRAY_SIZE(chmap), chmap);
  1716. if (ca < 0)
  1717. return -EINVAL;
  1718. if (spec->ops.chmap_validate) {
  1719. err = spec->ops.chmap_validate(ca, ARRAY_SIZE(chmap), chmap);
  1720. if (err)
  1721. return err;
  1722. }
  1723. mutex_lock(&per_pin->lock);
  1724. per_pin->chmap_set = true;
  1725. memcpy(per_pin->chmap, chmap, sizeof(chmap));
  1726. if (prepared)
  1727. hdmi_setup_audio_infoframe(codec, per_pin, per_pin->non_pcm);
  1728. mutex_unlock(&per_pin->lock);
  1729. return 0;
  1730. }
  1731. static int generic_hdmi_build_pcms(struct hda_codec *codec)
  1732. {
  1733. struct hdmi_spec *spec = codec->spec;
  1734. int pin_idx;
  1735. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1736. struct hda_pcm *info;
  1737. struct hda_pcm_stream *pstr;
  1738. struct hdmi_spec_per_pin *per_pin;
  1739. per_pin = get_pin(spec, pin_idx);
  1740. sprintf(per_pin->pcm_name, "HDMI %d", pin_idx);
  1741. info = snd_array_new(&spec->pcm_rec);
  1742. if (!info)
  1743. return -ENOMEM;
  1744. info->name = per_pin->pcm_name;
  1745. info->pcm_type = HDA_PCM_TYPE_HDMI;
  1746. info->own_chmap = true;
  1747. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  1748. pstr->substreams = 1;
  1749. pstr->ops = generic_ops;
  1750. /* other pstr fields are set in open */
  1751. }
  1752. codec->num_pcms = spec->num_pins;
  1753. codec->pcm_info = spec->pcm_rec.list;
  1754. return 0;
  1755. }
  1756. static int generic_hdmi_build_jack(struct hda_codec *codec, int pin_idx)
  1757. {
  1758. char hdmi_str[32] = "HDMI/DP";
  1759. struct hdmi_spec *spec = codec->spec;
  1760. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1761. int pcmdev = get_pcm_rec(spec, pin_idx)->device;
  1762. if (pcmdev > 0)
  1763. sprintf(hdmi_str + strlen(hdmi_str), ",pcm=%d", pcmdev);
  1764. if (!is_jack_detectable(codec, per_pin->pin_nid))
  1765. strncat(hdmi_str, " Phantom",
  1766. sizeof(hdmi_str) - strlen(hdmi_str) - 1);
  1767. return snd_hda_jack_add_kctl(codec, per_pin->pin_nid, hdmi_str, 0);
  1768. }
  1769. static int generic_hdmi_build_controls(struct hda_codec *codec)
  1770. {
  1771. struct hdmi_spec *spec = codec->spec;
  1772. int err;
  1773. int pin_idx;
  1774. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1775. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1776. err = generic_hdmi_build_jack(codec, pin_idx);
  1777. if (err < 0)
  1778. return err;
  1779. err = snd_hda_create_dig_out_ctls(codec,
  1780. per_pin->pin_nid,
  1781. per_pin->mux_nids[0],
  1782. HDA_PCM_TYPE_HDMI);
  1783. if (err < 0)
  1784. return err;
  1785. snd_hda_spdif_ctls_unassign(codec, pin_idx);
  1786. /* add control for ELD Bytes */
  1787. err = hdmi_create_eld_ctl(codec, pin_idx,
  1788. get_pcm_rec(spec, pin_idx)->device);
  1789. if (err < 0)
  1790. return err;
  1791. hdmi_present_sense(per_pin, 0);
  1792. }
  1793. /* add channel maps */
  1794. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1795. struct snd_pcm_chmap *chmap;
  1796. struct snd_kcontrol *kctl;
  1797. int i;
  1798. if (!codec->pcm_info[pin_idx].pcm)
  1799. break;
  1800. err = snd_pcm_add_chmap_ctls(codec->pcm_info[pin_idx].pcm,
  1801. SNDRV_PCM_STREAM_PLAYBACK,
  1802. NULL, 0, pin_idx, &chmap);
  1803. if (err < 0)
  1804. return err;
  1805. /* override handlers */
  1806. chmap->private_data = codec;
  1807. kctl = chmap->kctl;
  1808. for (i = 0; i < kctl->count; i++)
  1809. kctl->vd[i].access |= SNDRV_CTL_ELEM_ACCESS_WRITE;
  1810. kctl->info = hdmi_chmap_ctl_info;
  1811. kctl->get = hdmi_chmap_ctl_get;
  1812. kctl->put = hdmi_chmap_ctl_put;
  1813. kctl->tlv.c = hdmi_chmap_ctl_tlv;
  1814. }
  1815. return 0;
  1816. }
  1817. static int generic_hdmi_init_per_pins(struct hda_codec *codec)
  1818. {
  1819. struct hdmi_spec *spec = codec->spec;
  1820. int pin_idx;
  1821. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1822. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1823. per_pin->codec = codec;
  1824. mutex_init(&per_pin->lock);
  1825. INIT_DELAYED_WORK(&per_pin->work, hdmi_repoll_eld);
  1826. eld_proc_new(per_pin, pin_idx);
  1827. }
  1828. return 0;
  1829. }
  1830. static int generic_hdmi_init(struct hda_codec *codec)
  1831. {
  1832. struct hdmi_spec *spec = codec->spec;
  1833. int pin_idx;
  1834. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1835. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1836. hda_nid_t pin_nid = per_pin->pin_nid;
  1837. hdmi_init_pin(codec, pin_nid);
  1838. snd_hda_jack_detect_enable_callback(codec, pin_nid, pin_nid,
  1839. codec->jackpoll_interval > 0 ? jack_callback : NULL);
  1840. }
  1841. return 0;
  1842. }
  1843. static void hdmi_array_init(struct hdmi_spec *spec, int nums)
  1844. {
  1845. snd_array_init(&spec->pins, sizeof(struct hdmi_spec_per_pin), nums);
  1846. snd_array_init(&spec->cvts, sizeof(struct hdmi_spec_per_cvt), nums);
  1847. snd_array_init(&spec->pcm_rec, sizeof(struct hda_pcm), nums);
  1848. }
  1849. static void hdmi_array_free(struct hdmi_spec *spec)
  1850. {
  1851. snd_array_free(&spec->pins);
  1852. snd_array_free(&spec->cvts);
  1853. snd_array_free(&spec->pcm_rec);
  1854. }
  1855. static void generic_hdmi_free(struct hda_codec *codec)
  1856. {
  1857. struct hdmi_spec *spec = codec->spec;
  1858. int pin_idx;
  1859. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1860. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1861. cancel_delayed_work(&per_pin->work);
  1862. eld_proc_free(per_pin);
  1863. }
  1864. flush_workqueue(codec->bus->workq);
  1865. hdmi_array_free(spec);
  1866. kfree(spec);
  1867. }
  1868. #ifdef CONFIG_PM
  1869. static int generic_hdmi_resume(struct hda_codec *codec)
  1870. {
  1871. struct hdmi_spec *spec = codec->spec;
  1872. int pin_idx;
  1873. codec->patch_ops.init(codec);
  1874. snd_hda_codec_resume_amp(codec);
  1875. snd_hda_codec_resume_cache(codec);
  1876. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  1877. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  1878. hdmi_present_sense(per_pin, 1);
  1879. }
  1880. return 0;
  1881. }
  1882. #endif
  1883. static const struct hda_codec_ops generic_hdmi_patch_ops = {
  1884. .init = generic_hdmi_init,
  1885. .free = generic_hdmi_free,
  1886. .build_pcms = generic_hdmi_build_pcms,
  1887. .build_controls = generic_hdmi_build_controls,
  1888. .unsol_event = hdmi_unsol_event,
  1889. #ifdef CONFIG_PM
  1890. .resume = generic_hdmi_resume,
  1891. #endif
  1892. };
  1893. static const struct hdmi_ops generic_standard_hdmi_ops = {
  1894. .pin_get_eld = snd_hdmi_get_eld,
  1895. .pin_get_slot_channel = hdmi_pin_get_slot_channel,
  1896. .pin_set_slot_channel = hdmi_pin_set_slot_channel,
  1897. .pin_setup_infoframe = hdmi_pin_setup_infoframe,
  1898. .pin_hbr_setup = hdmi_pin_hbr_setup,
  1899. .setup_stream = hdmi_setup_stream,
  1900. .chmap_cea_alloc_validate_get_type = hdmi_chmap_cea_alloc_validate_get_type,
  1901. .cea_alloc_to_tlv_chmap = hdmi_cea_alloc_to_tlv_chmap,
  1902. };
  1903. static void intel_haswell_fixup_connect_list(struct hda_codec *codec,
  1904. hda_nid_t nid)
  1905. {
  1906. struct hdmi_spec *spec = codec->spec;
  1907. hda_nid_t conns[4];
  1908. int nconns;
  1909. nconns = snd_hda_get_connections(codec, nid, conns, ARRAY_SIZE(conns));
  1910. if (nconns == spec->num_cvts &&
  1911. !memcmp(conns, spec->cvt_nids, spec->num_cvts * sizeof(hda_nid_t)))
  1912. return;
  1913. /* override pins connection list */
  1914. codec_dbg(codec, "hdmi: haswell: override pin connection 0x%x\n", nid);
  1915. snd_hda_override_conn_list(codec, nid, spec->num_cvts, spec->cvt_nids);
  1916. }
  1917. #define INTEL_VENDOR_NID 0x08
  1918. #define INTEL_GET_VENDOR_VERB 0xf81
  1919. #define INTEL_SET_VENDOR_VERB 0x781
  1920. #define INTEL_EN_DP12 0x02 /* enable DP 1.2 features */
  1921. #define INTEL_EN_ALL_PIN_CVTS 0x01 /* enable 2nd & 3rd pins and convertors */
  1922. static void intel_haswell_enable_all_pins(struct hda_codec *codec,
  1923. bool update_tree)
  1924. {
  1925. unsigned int vendor_param;
  1926. vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
  1927. INTEL_GET_VENDOR_VERB, 0);
  1928. if (vendor_param == -1 || vendor_param & INTEL_EN_ALL_PIN_CVTS)
  1929. return;
  1930. vendor_param |= INTEL_EN_ALL_PIN_CVTS;
  1931. vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
  1932. INTEL_SET_VENDOR_VERB, vendor_param);
  1933. if (vendor_param == -1)
  1934. return;
  1935. if (update_tree)
  1936. snd_hda_codec_update_widgets(codec);
  1937. }
  1938. static void intel_haswell_fixup_enable_dp12(struct hda_codec *codec)
  1939. {
  1940. unsigned int vendor_param;
  1941. vendor_param = snd_hda_codec_read(codec, INTEL_VENDOR_NID, 0,
  1942. INTEL_GET_VENDOR_VERB, 0);
  1943. if (vendor_param == -1 || vendor_param & INTEL_EN_DP12)
  1944. return;
  1945. /* enable DP1.2 mode */
  1946. vendor_param |= INTEL_EN_DP12;
  1947. snd_hda_codec_write_cache(codec, INTEL_VENDOR_NID, 0,
  1948. INTEL_SET_VENDOR_VERB, vendor_param);
  1949. }
  1950. /* Haswell needs to re-issue the vendor-specific verbs before turning to D0.
  1951. * Otherwise you may get severe h/w communication errors.
  1952. */
  1953. static void haswell_set_power_state(struct hda_codec *codec, hda_nid_t fg,
  1954. unsigned int power_state)
  1955. {
  1956. if (power_state == AC_PWRST_D0) {
  1957. intel_haswell_enable_all_pins(codec, false);
  1958. intel_haswell_fixup_enable_dp12(codec);
  1959. }
  1960. snd_hda_codec_read(codec, fg, 0, AC_VERB_SET_POWER_STATE, power_state);
  1961. snd_hda_codec_set_power_to_all(codec, fg, power_state);
  1962. }
  1963. static int patch_generic_hdmi(struct hda_codec *codec)
  1964. {
  1965. struct hdmi_spec *spec;
  1966. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  1967. if (spec == NULL)
  1968. return -ENOMEM;
  1969. spec->ops = generic_standard_hdmi_ops;
  1970. codec->spec = spec;
  1971. hdmi_array_init(spec, 4);
  1972. if (is_haswell_plus(codec)) {
  1973. intel_haswell_enable_all_pins(codec, true);
  1974. intel_haswell_fixup_enable_dp12(codec);
  1975. }
  1976. if (is_haswell(codec) || is_valleyview(codec)) {
  1977. codec->depop_delay = 0;
  1978. }
  1979. if (hdmi_parse_codec(codec) < 0) {
  1980. codec->spec = NULL;
  1981. kfree(spec);
  1982. return -EINVAL;
  1983. }
  1984. codec->patch_ops = generic_hdmi_patch_ops;
  1985. if (is_haswell_plus(codec)) {
  1986. codec->patch_ops.set_power_state = haswell_set_power_state;
  1987. codec->dp_mst = true;
  1988. }
  1989. generic_hdmi_init_per_pins(codec);
  1990. init_channel_allocations();
  1991. return 0;
  1992. }
  1993. /*
  1994. * Shared non-generic implementations
  1995. */
  1996. static int simple_playback_build_pcms(struct hda_codec *codec)
  1997. {
  1998. struct hdmi_spec *spec = codec->spec;
  1999. struct hda_pcm *info;
  2000. unsigned int chans;
  2001. struct hda_pcm_stream *pstr;
  2002. struct hdmi_spec_per_cvt *per_cvt;
  2003. per_cvt = get_cvt(spec, 0);
  2004. chans = get_wcaps(codec, per_cvt->cvt_nid);
  2005. chans = get_wcaps_channels(chans);
  2006. info = snd_array_new(&spec->pcm_rec);
  2007. if (!info)
  2008. return -ENOMEM;
  2009. info->name = get_pin(spec, 0)->pcm_name;
  2010. sprintf(info->name, "HDMI 0");
  2011. info->pcm_type = HDA_PCM_TYPE_HDMI;
  2012. pstr = &info->stream[SNDRV_PCM_STREAM_PLAYBACK];
  2013. *pstr = spec->pcm_playback;
  2014. pstr->nid = per_cvt->cvt_nid;
  2015. if (pstr->channels_max <= 2 && chans && chans <= 16)
  2016. pstr->channels_max = chans;
  2017. codec->num_pcms = 1;
  2018. codec->pcm_info = info;
  2019. return 0;
  2020. }
  2021. /* unsolicited event for jack sensing */
  2022. static void simple_hdmi_unsol_event(struct hda_codec *codec,
  2023. unsigned int res)
  2024. {
  2025. snd_hda_jack_set_dirty_all(codec);
  2026. snd_hda_jack_report_sync(codec);
  2027. }
  2028. /* generic_hdmi_build_jack can be used for simple_hdmi, too,
  2029. * as long as spec->pins[] is set correctly
  2030. */
  2031. #define simple_hdmi_build_jack generic_hdmi_build_jack
  2032. static int simple_playback_build_controls(struct hda_codec *codec)
  2033. {
  2034. struct hdmi_spec *spec = codec->spec;
  2035. struct hdmi_spec_per_cvt *per_cvt;
  2036. int err;
  2037. per_cvt = get_cvt(spec, 0);
  2038. err = snd_hda_create_dig_out_ctls(codec, per_cvt->cvt_nid,
  2039. per_cvt->cvt_nid,
  2040. HDA_PCM_TYPE_HDMI);
  2041. if (err < 0)
  2042. return err;
  2043. return simple_hdmi_build_jack(codec, 0);
  2044. }
  2045. static int simple_playback_init(struct hda_codec *codec)
  2046. {
  2047. struct hdmi_spec *spec = codec->spec;
  2048. struct hdmi_spec_per_pin *per_pin = get_pin(spec, 0);
  2049. hda_nid_t pin = per_pin->pin_nid;
  2050. snd_hda_codec_write(codec, pin, 0,
  2051. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT);
  2052. /* some codecs require to unmute the pin */
  2053. if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
  2054. snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  2055. AMP_OUT_UNMUTE);
  2056. snd_hda_jack_detect_enable(codec, pin, pin);
  2057. return 0;
  2058. }
  2059. static void simple_playback_free(struct hda_codec *codec)
  2060. {
  2061. struct hdmi_spec *spec = codec->spec;
  2062. hdmi_array_free(spec);
  2063. kfree(spec);
  2064. }
  2065. /*
  2066. * Nvidia specific implementations
  2067. */
  2068. #define Nv_VERB_SET_Channel_Allocation 0xF79
  2069. #define Nv_VERB_SET_Info_Frame_Checksum 0xF7A
  2070. #define Nv_VERB_SET_Audio_Protection_On 0xF98
  2071. #define Nv_VERB_SET_Audio_Protection_Off 0xF99
  2072. #define nvhdmi_master_con_nid_7x 0x04
  2073. #define nvhdmi_master_pin_nid_7x 0x05
  2074. static const hda_nid_t nvhdmi_con_nids_7x[4] = {
  2075. /*front, rear, clfe, rear_surr */
  2076. 0x6, 0x8, 0xa, 0xc,
  2077. };
  2078. static const struct hda_verb nvhdmi_basic_init_7x_2ch[] = {
  2079. /* set audio protect on */
  2080. { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
  2081. /* enable digital output on pin widget */
  2082. { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  2083. {} /* terminator */
  2084. };
  2085. static const struct hda_verb nvhdmi_basic_init_7x_8ch[] = {
  2086. /* set audio protect on */
  2087. { 0x1, Nv_VERB_SET_Audio_Protection_On, 0x1},
  2088. /* enable digital output on pin widget */
  2089. { 0x5, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  2090. { 0x7, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  2091. { 0x9, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  2092. { 0xb, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  2093. { 0xd, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT | 0x5 },
  2094. {} /* terminator */
  2095. };
  2096. #ifdef LIMITED_RATE_FMT_SUPPORT
  2097. /* support only the safe format and rate */
  2098. #define SUPPORTED_RATES SNDRV_PCM_RATE_48000
  2099. #define SUPPORTED_MAXBPS 16
  2100. #define SUPPORTED_FORMATS SNDRV_PCM_FMTBIT_S16_LE
  2101. #else
  2102. /* support all rates and formats */
  2103. #define SUPPORTED_RATES \
  2104. (SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |\
  2105. SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |\
  2106. SNDRV_PCM_RATE_192000)
  2107. #define SUPPORTED_MAXBPS 24
  2108. #define SUPPORTED_FORMATS \
  2109. (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
  2110. #endif
  2111. static int nvhdmi_7x_init_2ch(struct hda_codec *codec)
  2112. {
  2113. snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_2ch);
  2114. return 0;
  2115. }
  2116. static int nvhdmi_7x_init_8ch(struct hda_codec *codec)
  2117. {
  2118. snd_hda_sequence_write(codec, nvhdmi_basic_init_7x_8ch);
  2119. return 0;
  2120. }
  2121. static unsigned int channels_2_6_8[] = {
  2122. 2, 6, 8
  2123. };
  2124. static unsigned int channels_2_8[] = {
  2125. 2, 8
  2126. };
  2127. static struct snd_pcm_hw_constraint_list hw_constraints_2_6_8_channels = {
  2128. .count = ARRAY_SIZE(channels_2_6_8),
  2129. .list = channels_2_6_8,
  2130. .mask = 0,
  2131. };
  2132. static struct snd_pcm_hw_constraint_list hw_constraints_2_8_channels = {
  2133. .count = ARRAY_SIZE(channels_2_8),
  2134. .list = channels_2_8,
  2135. .mask = 0,
  2136. };
  2137. static int simple_playback_pcm_open(struct hda_pcm_stream *hinfo,
  2138. struct hda_codec *codec,
  2139. struct snd_pcm_substream *substream)
  2140. {
  2141. struct hdmi_spec *spec = codec->spec;
  2142. struct snd_pcm_hw_constraint_list *hw_constraints_channels = NULL;
  2143. switch (codec->preset->id) {
  2144. case 0x10de0002:
  2145. case 0x10de0003:
  2146. case 0x10de0005:
  2147. case 0x10de0006:
  2148. hw_constraints_channels = &hw_constraints_2_8_channels;
  2149. break;
  2150. case 0x10de0007:
  2151. hw_constraints_channels = &hw_constraints_2_6_8_channels;
  2152. break;
  2153. default:
  2154. break;
  2155. }
  2156. if (hw_constraints_channels != NULL) {
  2157. snd_pcm_hw_constraint_list(substream->runtime, 0,
  2158. SNDRV_PCM_HW_PARAM_CHANNELS,
  2159. hw_constraints_channels);
  2160. } else {
  2161. snd_pcm_hw_constraint_step(substream->runtime, 0,
  2162. SNDRV_PCM_HW_PARAM_CHANNELS, 2);
  2163. }
  2164. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  2165. }
  2166. static int simple_playback_pcm_close(struct hda_pcm_stream *hinfo,
  2167. struct hda_codec *codec,
  2168. struct snd_pcm_substream *substream)
  2169. {
  2170. struct hdmi_spec *spec = codec->spec;
  2171. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  2172. }
  2173. static int simple_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  2174. struct hda_codec *codec,
  2175. unsigned int stream_tag,
  2176. unsigned int format,
  2177. struct snd_pcm_substream *substream)
  2178. {
  2179. struct hdmi_spec *spec = codec->spec;
  2180. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  2181. stream_tag, format, substream);
  2182. }
  2183. static const struct hda_pcm_stream simple_pcm_playback = {
  2184. .substreams = 1,
  2185. .channels_min = 2,
  2186. .channels_max = 2,
  2187. .ops = {
  2188. .open = simple_playback_pcm_open,
  2189. .close = simple_playback_pcm_close,
  2190. .prepare = simple_playback_pcm_prepare
  2191. },
  2192. };
  2193. static const struct hda_codec_ops simple_hdmi_patch_ops = {
  2194. .build_controls = simple_playback_build_controls,
  2195. .build_pcms = simple_playback_build_pcms,
  2196. .init = simple_playback_init,
  2197. .free = simple_playback_free,
  2198. .unsol_event = simple_hdmi_unsol_event,
  2199. };
  2200. static int patch_simple_hdmi(struct hda_codec *codec,
  2201. hda_nid_t cvt_nid, hda_nid_t pin_nid)
  2202. {
  2203. struct hdmi_spec *spec;
  2204. struct hdmi_spec_per_cvt *per_cvt;
  2205. struct hdmi_spec_per_pin *per_pin;
  2206. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  2207. if (!spec)
  2208. return -ENOMEM;
  2209. codec->spec = spec;
  2210. hdmi_array_init(spec, 1);
  2211. spec->multiout.num_dacs = 0; /* no analog */
  2212. spec->multiout.max_channels = 2;
  2213. spec->multiout.dig_out_nid = cvt_nid;
  2214. spec->num_cvts = 1;
  2215. spec->num_pins = 1;
  2216. per_pin = snd_array_new(&spec->pins);
  2217. per_cvt = snd_array_new(&spec->cvts);
  2218. if (!per_pin || !per_cvt) {
  2219. simple_playback_free(codec);
  2220. return -ENOMEM;
  2221. }
  2222. per_cvt->cvt_nid = cvt_nid;
  2223. per_pin->pin_nid = pin_nid;
  2224. spec->pcm_playback = simple_pcm_playback;
  2225. codec->patch_ops = simple_hdmi_patch_ops;
  2226. return 0;
  2227. }
  2228. static void nvhdmi_8ch_7x_set_info_frame_parameters(struct hda_codec *codec,
  2229. int channels)
  2230. {
  2231. unsigned int chanmask;
  2232. int chan = channels ? (channels - 1) : 1;
  2233. switch (channels) {
  2234. default:
  2235. case 0:
  2236. case 2:
  2237. chanmask = 0x00;
  2238. break;
  2239. case 4:
  2240. chanmask = 0x08;
  2241. break;
  2242. case 6:
  2243. chanmask = 0x0b;
  2244. break;
  2245. case 8:
  2246. chanmask = 0x13;
  2247. break;
  2248. }
  2249. /* Set the audio infoframe channel allocation and checksum fields. The
  2250. * channel count is computed implicitly by the hardware. */
  2251. snd_hda_codec_write(codec, 0x1, 0,
  2252. Nv_VERB_SET_Channel_Allocation, chanmask);
  2253. snd_hda_codec_write(codec, 0x1, 0,
  2254. Nv_VERB_SET_Info_Frame_Checksum,
  2255. (0x71 - chan - chanmask));
  2256. }
  2257. static int nvhdmi_8ch_7x_pcm_close(struct hda_pcm_stream *hinfo,
  2258. struct hda_codec *codec,
  2259. struct snd_pcm_substream *substream)
  2260. {
  2261. struct hdmi_spec *spec = codec->spec;
  2262. int i;
  2263. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x,
  2264. 0, AC_VERB_SET_CHANNEL_STREAMID, 0);
  2265. for (i = 0; i < 4; i++) {
  2266. /* set the stream id */
  2267. snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
  2268. AC_VERB_SET_CHANNEL_STREAMID, 0);
  2269. /* set the stream format */
  2270. snd_hda_codec_write(codec, nvhdmi_con_nids_7x[i], 0,
  2271. AC_VERB_SET_STREAM_FORMAT, 0);
  2272. }
  2273. /* The audio hardware sends a channel count of 0x7 (8ch) when all the
  2274. * streams are disabled. */
  2275. nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
  2276. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  2277. }
  2278. static int nvhdmi_8ch_7x_pcm_prepare(struct hda_pcm_stream *hinfo,
  2279. struct hda_codec *codec,
  2280. unsigned int stream_tag,
  2281. unsigned int format,
  2282. struct snd_pcm_substream *substream)
  2283. {
  2284. int chs;
  2285. unsigned int dataDCC2, channel_id;
  2286. int i;
  2287. struct hdmi_spec *spec = codec->spec;
  2288. struct hda_spdif_out *spdif;
  2289. struct hdmi_spec_per_cvt *per_cvt;
  2290. mutex_lock(&codec->spdif_mutex);
  2291. per_cvt = get_cvt(spec, 0);
  2292. spdif = snd_hda_spdif_out_of_nid(codec, per_cvt->cvt_nid);
  2293. chs = substream->runtime->channels;
  2294. dataDCC2 = 0x2;
  2295. /* turn off SPDIF once; otherwise the IEC958 bits won't be updated */
  2296. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE))
  2297. snd_hda_codec_write(codec,
  2298. nvhdmi_master_con_nid_7x,
  2299. 0,
  2300. AC_VERB_SET_DIGI_CONVERT_1,
  2301. spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
  2302. /* set the stream id */
  2303. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
  2304. AC_VERB_SET_CHANNEL_STREAMID, (stream_tag << 4) | 0x0);
  2305. /* set the stream format */
  2306. snd_hda_codec_write(codec, nvhdmi_master_con_nid_7x, 0,
  2307. AC_VERB_SET_STREAM_FORMAT, format);
  2308. /* turn on again (if needed) */
  2309. /* enable and set the channel status audio/data flag */
  2310. if (codec->spdif_status_reset && (spdif->ctls & AC_DIG1_ENABLE)) {
  2311. snd_hda_codec_write(codec,
  2312. nvhdmi_master_con_nid_7x,
  2313. 0,
  2314. AC_VERB_SET_DIGI_CONVERT_1,
  2315. spdif->ctls & 0xff);
  2316. snd_hda_codec_write(codec,
  2317. nvhdmi_master_con_nid_7x,
  2318. 0,
  2319. AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
  2320. }
  2321. for (i = 0; i < 4; i++) {
  2322. if (chs == 2)
  2323. channel_id = 0;
  2324. else
  2325. channel_id = i * 2;
  2326. /* turn off SPDIF once;
  2327. *otherwise the IEC958 bits won't be updated
  2328. */
  2329. if (codec->spdif_status_reset &&
  2330. (spdif->ctls & AC_DIG1_ENABLE))
  2331. snd_hda_codec_write(codec,
  2332. nvhdmi_con_nids_7x[i],
  2333. 0,
  2334. AC_VERB_SET_DIGI_CONVERT_1,
  2335. spdif->ctls & ~AC_DIG1_ENABLE & 0xff);
  2336. /* set the stream id */
  2337. snd_hda_codec_write(codec,
  2338. nvhdmi_con_nids_7x[i],
  2339. 0,
  2340. AC_VERB_SET_CHANNEL_STREAMID,
  2341. (stream_tag << 4) | channel_id);
  2342. /* set the stream format */
  2343. snd_hda_codec_write(codec,
  2344. nvhdmi_con_nids_7x[i],
  2345. 0,
  2346. AC_VERB_SET_STREAM_FORMAT,
  2347. format);
  2348. /* turn on again (if needed) */
  2349. /* enable and set the channel status audio/data flag */
  2350. if (codec->spdif_status_reset &&
  2351. (spdif->ctls & AC_DIG1_ENABLE)) {
  2352. snd_hda_codec_write(codec,
  2353. nvhdmi_con_nids_7x[i],
  2354. 0,
  2355. AC_VERB_SET_DIGI_CONVERT_1,
  2356. spdif->ctls & 0xff);
  2357. snd_hda_codec_write(codec,
  2358. nvhdmi_con_nids_7x[i],
  2359. 0,
  2360. AC_VERB_SET_DIGI_CONVERT_2, dataDCC2);
  2361. }
  2362. }
  2363. nvhdmi_8ch_7x_set_info_frame_parameters(codec, chs);
  2364. mutex_unlock(&codec->spdif_mutex);
  2365. return 0;
  2366. }
  2367. static const struct hda_pcm_stream nvhdmi_pcm_playback_8ch_7x = {
  2368. .substreams = 1,
  2369. .channels_min = 2,
  2370. .channels_max = 8,
  2371. .nid = nvhdmi_master_con_nid_7x,
  2372. .rates = SUPPORTED_RATES,
  2373. .maxbps = SUPPORTED_MAXBPS,
  2374. .formats = SUPPORTED_FORMATS,
  2375. .ops = {
  2376. .open = simple_playback_pcm_open,
  2377. .close = nvhdmi_8ch_7x_pcm_close,
  2378. .prepare = nvhdmi_8ch_7x_pcm_prepare
  2379. },
  2380. };
  2381. static int patch_nvhdmi_2ch(struct hda_codec *codec)
  2382. {
  2383. struct hdmi_spec *spec;
  2384. int err = patch_simple_hdmi(codec, nvhdmi_master_con_nid_7x,
  2385. nvhdmi_master_pin_nid_7x);
  2386. if (err < 0)
  2387. return err;
  2388. codec->patch_ops.init = nvhdmi_7x_init_2ch;
  2389. /* override the PCM rates, etc, as the codec doesn't give full list */
  2390. spec = codec->spec;
  2391. spec->pcm_playback.rates = SUPPORTED_RATES;
  2392. spec->pcm_playback.maxbps = SUPPORTED_MAXBPS;
  2393. spec->pcm_playback.formats = SUPPORTED_FORMATS;
  2394. return 0;
  2395. }
  2396. static int nvhdmi_7x_8ch_build_pcms(struct hda_codec *codec)
  2397. {
  2398. struct hdmi_spec *spec = codec->spec;
  2399. int err = simple_playback_build_pcms(codec);
  2400. if (!err) {
  2401. struct hda_pcm *info = get_pcm_rec(spec, 0);
  2402. info->own_chmap = true;
  2403. }
  2404. return err;
  2405. }
  2406. static int nvhdmi_7x_8ch_build_controls(struct hda_codec *codec)
  2407. {
  2408. struct hdmi_spec *spec = codec->spec;
  2409. struct hda_pcm *info;
  2410. struct snd_pcm_chmap *chmap;
  2411. int err;
  2412. err = simple_playback_build_controls(codec);
  2413. if (err < 0)
  2414. return err;
  2415. /* add channel maps */
  2416. info = get_pcm_rec(spec, 0);
  2417. err = snd_pcm_add_chmap_ctls(info->pcm,
  2418. SNDRV_PCM_STREAM_PLAYBACK,
  2419. snd_pcm_alt_chmaps, 8, 0, &chmap);
  2420. if (err < 0)
  2421. return err;
  2422. switch (codec->preset->id) {
  2423. case 0x10de0002:
  2424. case 0x10de0003:
  2425. case 0x10de0005:
  2426. case 0x10de0006:
  2427. chmap->channel_mask = (1U << 2) | (1U << 8);
  2428. break;
  2429. case 0x10de0007:
  2430. chmap->channel_mask = (1U << 2) | (1U << 6) | (1U << 8);
  2431. }
  2432. return 0;
  2433. }
  2434. static int patch_nvhdmi_8ch_7x(struct hda_codec *codec)
  2435. {
  2436. struct hdmi_spec *spec;
  2437. int err = patch_nvhdmi_2ch(codec);
  2438. if (err < 0)
  2439. return err;
  2440. spec = codec->spec;
  2441. spec->multiout.max_channels = 8;
  2442. spec->pcm_playback = nvhdmi_pcm_playback_8ch_7x;
  2443. codec->patch_ops.init = nvhdmi_7x_init_8ch;
  2444. codec->patch_ops.build_pcms = nvhdmi_7x_8ch_build_pcms;
  2445. codec->patch_ops.build_controls = nvhdmi_7x_8ch_build_controls;
  2446. /* Initialize the audio infoframe channel mask and checksum to something
  2447. * valid */
  2448. nvhdmi_8ch_7x_set_info_frame_parameters(codec, 8);
  2449. return 0;
  2450. }
  2451. /*
  2452. * NVIDIA codecs ignore ASP mapping for 2ch - confirmed on:
  2453. * - 0x10de0015
  2454. * - 0x10de0040
  2455. */
  2456. static int nvhdmi_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
  2457. int channels)
  2458. {
  2459. if (cap->ca_index == 0x00 && channels == 2)
  2460. return SNDRV_CTL_TLVT_CHMAP_FIXED;
  2461. return hdmi_chmap_cea_alloc_validate_get_type(cap, channels);
  2462. }
  2463. static int nvhdmi_chmap_validate(int ca, int chs, unsigned char *map)
  2464. {
  2465. if (ca == 0x00 && (map[0] != SNDRV_CHMAP_FL || map[1] != SNDRV_CHMAP_FR))
  2466. return -EINVAL;
  2467. return 0;
  2468. }
  2469. static int patch_nvhdmi(struct hda_codec *codec)
  2470. {
  2471. struct hdmi_spec *spec;
  2472. int err;
  2473. err = patch_generic_hdmi(codec);
  2474. if (err)
  2475. return err;
  2476. spec = codec->spec;
  2477. spec->dyn_pin_out = true;
  2478. spec->ops.chmap_cea_alloc_validate_get_type =
  2479. nvhdmi_chmap_cea_alloc_validate_get_type;
  2480. spec->ops.chmap_validate = nvhdmi_chmap_validate;
  2481. return 0;
  2482. }
  2483. /*
  2484. * ATI/AMD-specific implementations
  2485. */
  2486. #define is_amdhdmi_rev3_or_later(codec) \
  2487. ((codec)->vendor_id == 0x1002aa01 && ((codec)->revision_id & 0xff00) >= 0x0300)
  2488. #define has_amd_full_remap_support(codec) is_amdhdmi_rev3_or_later(codec)
  2489. /* ATI/AMD specific HDA pin verbs, see the AMD HDA Verbs specification */
  2490. #define ATI_VERB_SET_CHANNEL_ALLOCATION 0x771
  2491. #define ATI_VERB_SET_DOWNMIX_INFO 0x772
  2492. #define ATI_VERB_SET_MULTICHANNEL_01 0x777
  2493. #define ATI_VERB_SET_MULTICHANNEL_23 0x778
  2494. #define ATI_VERB_SET_MULTICHANNEL_45 0x779
  2495. #define ATI_VERB_SET_MULTICHANNEL_67 0x77a
  2496. #define ATI_VERB_SET_HBR_CONTROL 0x77c
  2497. #define ATI_VERB_SET_MULTICHANNEL_1 0x785
  2498. #define ATI_VERB_SET_MULTICHANNEL_3 0x786
  2499. #define ATI_VERB_SET_MULTICHANNEL_5 0x787
  2500. #define ATI_VERB_SET_MULTICHANNEL_7 0x788
  2501. #define ATI_VERB_SET_MULTICHANNEL_MODE 0x789
  2502. #define ATI_VERB_GET_CHANNEL_ALLOCATION 0xf71
  2503. #define ATI_VERB_GET_DOWNMIX_INFO 0xf72
  2504. #define ATI_VERB_GET_MULTICHANNEL_01 0xf77
  2505. #define ATI_VERB_GET_MULTICHANNEL_23 0xf78
  2506. #define ATI_VERB_GET_MULTICHANNEL_45 0xf79
  2507. #define ATI_VERB_GET_MULTICHANNEL_67 0xf7a
  2508. #define ATI_VERB_GET_HBR_CONTROL 0xf7c
  2509. #define ATI_VERB_GET_MULTICHANNEL_1 0xf85
  2510. #define ATI_VERB_GET_MULTICHANNEL_3 0xf86
  2511. #define ATI_VERB_GET_MULTICHANNEL_5 0xf87
  2512. #define ATI_VERB_GET_MULTICHANNEL_7 0xf88
  2513. #define ATI_VERB_GET_MULTICHANNEL_MODE 0xf89
  2514. /* AMD specific HDA cvt verbs */
  2515. #define ATI_VERB_SET_RAMP_RATE 0x770
  2516. #define ATI_VERB_GET_RAMP_RATE 0xf70
  2517. #define ATI_OUT_ENABLE 0x1
  2518. #define ATI_MULTICHANNEL_MODE_PAIRED 0
  2519. #define ATI_MULTICHANNEL_MODE_SINGLE 1
  2520. #define ATI_HBR_CAPABLE 0x01
  2521. #define ATI_HBR_ENABLE 0x10
  2522. static int atihdmi_pin_get_eld(struct hda_codec *codec, hda_nid_t nid,
  2523. unsigned char *buf, int *eld_size)
  2524. {
  2525. /* call hda_eld.c ATI/AMD-specific function */
  2526. return snd_hdmi_get_eld_ati(codec, nid, buf, eld_size,
  2527. is_amdhdmi_rev3_or_later(codec));
  2528. }
  2529. static void atihdmi_pin_setup_infoframe(struct hda_codec *codec, hda_nid_t pin_nid, int ca,
  2530. int active_channels, int conn_type)
  2531. {
  2532. snd_hda_codec_write(codec, pin_nid, 0, ATI_VERB_SET_CHANNEL_ALLOCATION, ca);
  2533. }
  2534. static int atihdmi_paired_swap_fc_lfe(int pos)
  2535. {
  2536. /*
  2537. * ATI/AMD have automatic FC/LFE swap built-in
  2538. * when in pairwise mapping mode.
  2539. */
  2540. switch (pos) {
  2541. /* see channel_allocations[].speakers[] */
  2542. case 2: return 3;
  2543. case 3: return 2;
  2544. default: break;
  2545. }
  2546. return pos;
  2547. }
  2548. static int atihdmi_paired_chmap_validate(int ca, int chs, unsigned char *map)
  2549. {
  2550. struct cea_channel_speaker_allocation *cap;
  2551. int i, j;
  2552. /* check that only channel pairs need to be remapped on old pre-rev3 ATI/AMD */
  2553. cap = &channel_allocations[get_channel_allocation_order(ca)];
  2554. for (i = 0; i < chs; ++i) {
  2555. int mask = to_spk_mask(map[i]);
  2556. bool ok = false;
  2557. bool companion_ok = false;
  2558. if (!mask)
  2559. continue;
  2560. for (j = 0 + i % 2; j < 8; j += 2) {
  2561. int chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j);
  2562. if (cap->speakers[chan_idx] == mask) {
  2563. /* channel is in a supported position */
  2564. ok = true;
  2565. if (i % 2 == 0 && i + 1 < chs) {
  2566. /* even channel, check the odd companion */
  2567. int comp_chan_idx = 7 - atihdmi_paired_swap_fc_lfe(j + 1);
  2568. int comp_mask_req = to_spk_mask(map[i+1]);
  2569. int comp_mask_act = cap->speakers[comp_chan_idx];
  2570. if (comp_mask_req == comp_mask_act)
  2571. companion_ok = true;
  2572. else
  2573. return -EINVAL;
  2574. }
  2575. break;
  2576. }
  2577. }
  2578. if (!ok)
  2579. return -EINVAL;
  2580. if (companion_ok)
  2581. i++; /* companion channel already checked */
  2582. }
  2583. return 0;
  2584. }
  2585. static int atihdmi_pin_set_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
  2586. int hdmi_slot, int stream_channel)
  2587. {
  2588. int verb;
  2589. int ati_channel_setup = 0;
  2590. if (hdmi_slot > 7)
  2591. return -EINVAL;
  2592. if (!has_amd_full_remap_support(codec)) {
  2593. hdmi_slot = atihdmi_paired_swap_fc_lfe(hdmi_slot);
  2594. /* In case this is an odd slot but without stream channel, do not
  2595. * disable the slot since the corresponding even slot could have a
  2596. * channel. In case neither have a channel, the slot pair will be
  2597. * disabled when this function is called for the even slot. */
  2598. if (hdmi_slot % 2 != 0 && stream_channel == 0xf)
  2599. return 0;
  2600. hdmi_slot -= hdmi_slot % 2;
  2601. if (stream_channel != 0xf)
  2602. stream_channel -= stream_channel % 2;
  2603. }
  2604. verb = ATI_VERB_SET_MULTICHANNEL_01 + hdmi_slot/2 + (hdmi_slot % 2) * 0x00e;
  2605. /* ati_channel_setup format: [7..4] = stream_channel_id, [1] = mute, [0] = enable */
  2606. if (stream_channel != 0xf)
  2607. ati_channel_setup = (stream_channel << 4) | ATI_OUT_ENABLE;
  2608. return snd_hda_codec_write(codec, pin_nid, 0, verb, ati_channel_setup);
  2609. }
  2610. static int atihdmi_pin_get_slot_channel(struct hda_codec *codec, hda_nid_t pin_nid,
  2611. int asp_slot)
  2612. {
  2613. bool was_odd = false;
  2614. int ati_asp_slot = asp_slot;
  2615. int verb;
  2616. int ati_channel_setup;
  2617. if (asp_slot > 7)
  2618. return -EINVAL;
  2619. if (!has_amd_full_remap_support(codec)) {
  2620. ati_asp_slot = atihdmi_paired_swap_fc_lfe(asp_slot);
  2621. if (ati_asp_slot % 2 != 0) {
  2622. ati_asp_slot -= 1;
  2623. was_odd = true;
  2624. }
  2625. }
  2626. verb = ATI_VERB_GET_MULTICHANNEL_01 + ati_asp_slot/2 + (ati_asp_slot % 2) * 0x00e;
  2627. ati_channel_setup = snd_hda_codec_read(codec, pin_nid, 0, verb, 0);
  2628. if (!(ati_channel_setup & ATI_OUT_ENABLE))
  2629. return 0xf;
  2630. return ((ati_channel_setup & 0xf0) >> 4) + !!was_odd;
  2631. }
  2632. static int atihdmi_paired_chmap_cea_alloc_validate_get_type(struct cea_channel_speaker_allocation *cap,
  2633. int channels)
  2634. {
  2635. int c;
  2636. /*
  2637. * Pre-rev3 ATI/AMD codecs operate in a paired channel mode, so
  2638. * we need to take that into account (a single channel may take 2
  2639. * channel slots if we need to carry a silent channel next to it).
  2640. * On Rev3+ AMD codecs this function is not used.
  2641. */
  2642. int chanpairs = 0;
  2643. /* We only produce even-numbered channel count TLVs */
  2644. if ((channels % 2) != 0)
  2645. return -1;
  2646. for (c = 0; c < 7; c += 2) {
  2647. if (cap->speakers[c] || cap->speakers[c+1])
  2648. chanpairs++;
  2649. }
  2650. if (chanpairs * 2 != channels)
  2651. return -1;
  2652. return SNDRV_CTL_TLVT_CHMAP_PAIRED;
  2653. }
  2654. static void atihdmi_paired_cea_alloc_to_tlv_chmap(struct cea_channel_speaker_allocation *cap,
  2655. unsigned int *chmap, int channels)
  2656. {
  2657. /* produce paired maps for pre-rev3 ATI/AMD codecs */
  2658. int count = 0;
  2659. int c;
  2660. for (c = 7; c >= 0; c--) {
  2661. int chan = 7 - atihdmi_paired_swap_fc_lfe(7 - c);
  2662. int spk = cap->speakers[chan];
  2663. if (!spk) {
  2664. /* add N/A channel if the companion channel is occupied */
  2665. if (cap->speakers[chan + (chan % 2 ? -1 : 1)])
  2666. chmap[count++] = SNDRV_CHMAP_NA;
  2667. continue;
  2668. }
  2669. chmap[count++] = spk_to_chmap(spk);
  2670. }
  2671. WARN_ON(count != channels);
  2672. }
  2673. static int atihdmi_pin_hbr_setup(struct hda_codec *codec, hda_nid_t pin_nid,
  2674. bool hbr)
  2675. {
  2676. int hbr_ctl, hbr_ctl_new;
  2677. hbr_ctl = snd_hda_codec_read(codec, pin_nid, 0, ATI_VERB_GET_HBR_CONTROL, 0);
  2678. if (hbr_ctl >= 0 && (hbr_ctl & ATI_HBR_CAPABLE)) {
  2679. if (hbr)
  2680. hbr_ctl_new = hbr_ctl | ATI_HBR_ENABLE;
  2681. else
  2682. hbr_ctl_new = hbr_ctl & ~ATI_HBR_ENABLE;
  2683. codec_dbg(codec,
  2684. "atihdmi_pin_hbr_setup: NID=0x%x, %shbr-ctl=0x%x\n",
  2685. pin_nid,
  2686. hbr_ctl == hbr_ctl_new ? "" : "new-",
  2687. hbr_ctl_new);
  2688. if (hbr_ctl != hbr_ctl_new)
  2689. snd_hda_codec_write(codec, pin_nid, 0,
  2690. ATI_VERB_SET_HBR_CONTROL,
  2691. hbr_ctl_new);
  2692. } else if (hbr)
  2693. return -EINVAL;
  2694. return 0;
  2695. }
  2696. static int atihdmi_setup_stream(struct hda_codec *codec, hda_nid_t cvt_nid,
  2697. hda_nid_t pin_nid, u32 stream_tag, int format)
  2698. {
  2699. if (is_amdhdmi_rev3_or_later(codec)) {
  2700. int ramp_rate = 180; /* default as per AMD spec */
  2701. /* disable ramp-up/down for non-pcm as per AMD spec */
  2702. if (format & AC_FMT_TYPE_NON_PCM)
  2703. ramp_rate = 0;
  2704. snd_hda_codec_write(codec, cvt_nid, 0, ATI_VERB_SET_RAMP_RATE, ramp_rate);
  2705. }
  2706. return hdmi_setup_stream(codec, cvt_nid, pin_nid, stream_tag, format);
  2707. }
  2708. static int atihdmi_init(struct hda_codec *codec)
  2709. {
  2710. struct hdmi_spec *spec = codec->spec;
  2711. int pin_idx, err;
  2712. err = generic_hdmi_init(codec);
  2713. if (err)
  2714. return err;
  2715. for (pin_idx = 0; pin_idx < spec->num_pins; pin_idx++) {
  2716. struct hdmi_spec_per_pin *per_pin = get_pin(spec, pin_idx);
  2717. /* make sure downmix information in infoframe is zero */
  2718. snd_hda_codec_write(codec, per_pin->pin_nid, 0, ATI_VERB_SET_DOWNMIX_INFO, 0);
  2719. /* enable channel-wise remap mode if supported */
  2720. if (has_amd_full_remap_support(codec))
  2721. snd_hda_codec_write(codec, per_pin->pin_nid, 0,
  2722. ATI_VERB_SET_MULTICHANNEL_MODE,
  2723. ATI_MULTICHANNEL_MODE_SINGLE);
  2724. }
  2725. return 0;
  2726. }
  2727. static int patch_atihdmi(struct hda_codec *codec)
  2728. {
  2729. struct hdmi_spec *spec;
  2730. struct hdmi_spec_per_cvt *per_cvt;
  2731. int err, cvt_idx;
  2732. err = patch_generic_hdmi(codec);
  2733. if (err)
  2734. return err;
  2735. codec->patch_ops.init = atihdmi_init;
  2736. spec = codec->spec;
  2737. spec->ops.pin_get_eld = atihdmi_pin_get_eld;
  2738. spec->ops.pin_get_slot_channel = atihdmi_pin_get_slot_channel;
  2739. spec->ops.pin_set_slot_channel = atihdmi_pin_set_slot_channel;
  2740. spec->ops.pin_setup_infoframe = atihdmi_pin_setup_infoframe;
  2741. spec->ops.pin_hbr_setup = atihdmi_pin_hbr_setup;
  2742. spec->ops.setup_stream = atihdmi_setup_stream;
  2743. if (!has_amd_full_remap_support(codec)) {
  2744. /* override to ATI/AMD-specific versions with pairwise mapping */
  2745. spec->ops.chmap_cea_alloc_validate_get_type =
  2746. atihdmi_paired_chmap_cea_alloc_validate_get_type;
  2747. spec->ops.cea_alloc_to_tlv_chmap = atihdmi_paired_cea_alloc_to_tlv_chmap;
  2748. spec->ops.chmap_validate = atihdmi_paired_chmap_validate;
  2749. }
  2750. /* ATI/AMD converters do not advertise all of their capabilities */
  2751. for (cvt_idx = 0; cvt_idx < spec->num_cvts; cvt_idx++) {
  2752. per_cvt = get_cvt(spec, cvt_idx);
  2753. per_cvt->channels_max = max(per_cvt->channels_max, 8u);
  2754. per_cvt->rates |= SUPPORTED_RATES;
  2755. per_cvt->formats |= SUPPORTED_FORMATS;
  2756. per_cvt->maxbps = max(per_cvt->maxbps, 24u);
  2757. }
  2758. spec->channels_max = max(spec->channels_max, 8u);
  2759. return 0;
  2760. }
  2761. /* VIA HDMI Implementation */
  2762. #define VIAHDMI_CVT_NID 0x02 /* audio converter1 */
  2763. #define VIAHDMI_PIN_NID 0x03 /* HDMI output pin1 */
  2764. static int patch_via_hdmi(struct hda_codec *codec)
  2765. {
  2766. return patch_simple_hdmi(codec, VIAHDMI_CVT_NID, VIAHDMI_PIN_NID);
  2767. }
  2768. /*
  2769. * called from hda_codec.c for generic HDMI support
  2770. */
  2771. int snd_hda_parse_hdmi_codec(struct hda_codec *codec)
  2772. {
  2773. return patch_generic_hdmi(codec);
  2774. }
  2775. EXPORT_SYMBOL_GPL(snd_hda_parse_hdmi_codec);
  2776. /*
  2777. * patch entries
  2778. */
  2779. static const struct hda_codec_preset snd_hda_preset_hdmi[] = {
  2780. { .id = 0x1002793c, .name = "RS600 HDMI", .patch = patch_atihdmi },
  2781. { .id = 0x10027919, .name = "RS600 HDMI", .patch = patch_atihdmi },
  2782. { .id = 0x1002791a, .name = "RS690/780 HDMI", .patch = patch_atihdmi },
  2783. { .id = 0x1002aa01, .name = "R6xx HDMI", .patch = patch_atihdmi },
  2784. { .id = 0x10951390, .name = "SiI1390 HDMI", .patch = patch_generic_hdmi },
  2785. { .id = 0x10951392, .name = "SiI1392 HDMI", .patch = patch_generic_hdmi },
  2786. { .id = 0x17e80047, .name = "Chrontel HDMI", .patch = patch_generic_hdmi },
  2787. { .id = 0x10de0002, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  2788. { .id = 0x10de0003, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  2789. { .id = 0x10de0005, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  2790. { .id = 0x10de0006, .name = "MCP77/78 HDMI", .patch = patch_nvhdmi_8ch_7x },
  2791. { .id = 0x10de0007, .name = "MCP79/7A HDMI", .patch = patch_nvhdmi_8ch_7x },
  2792. { .id = 0x10de000a, .name = "GPU 0a HDMI/DP", .patch = patch_nvhdmi },
  2793. { .id = 0x10de000b, .name = "GPU 0b HDMI/DP", .patch = patch_nvhdmi },
  2794. { .id = 0x10de000c, .name = "MCP89 HDMI", .patch = patch_nvhdmi },
  2795. { .id = 0x10de000d, .name = "GPU 0d HDMI/DP", .patch = patch_nvhdmi },
  2796. { .id = 0x10de0010, .name = "GPU 10 HDMI/DP", .patch = patch_nvhdmi },
  2797. { .id = 0x10de0011, .name = "GPU 11 HDMI/DP", .patch = patch_nvhdmi },
  2798. { .id = 0x10de0012, .name = "GPU 12 HDMI/DP", .patch = patch_nvhdmi },
  2799. { .id = 0x10de0013, .name = "GPU 13 HDMI/DP", .patch = patch_nvhdmi },
  2800. { .id = 0x10de0014, .name = "GPU 14 HDMI/DP", .patch = patch_nvhdmi },
  2801. { .id = 0x10de0015, .name = "GPU 15 HDMI/DP", .patch = patch_nvhdmi },
  2802. { .id = 0x10de0016, .name = "GPU 16 HDMI/DP", .patch = patch_nvhdmi },
  2803. /* 17 is known to be absent */
  2804. { .id = 0x10de0018, .name = "GPU 18 HDMI/DP", .patch = patch_nvhdmi },
  2805. { .id = 0x10de0019, .name = "GPU 19 HDMI/DP", .patch = patch_nvhdmi },
  2806. { .id = 0x10de001a, .name = "GPU 1a HDMI/DP", .patch = patch_nvhdmi },
  2807. { .id = 0x10de001b, .name = "GPU 1b HDMI/DP", .patch = patch_nvhdmi },
  2808. { .id = 0x10de001c, .name = "GPU 1c HDMI/DP", .patch = patch_nvhdmi },
  2809. { .id = 0x10de0028, .name = "Tegra12x HDMI", .patch = patch_nvhdmi },
  2810. { .id = 0x10de0040, .name = "GPU 40 HDMI/DP", .patch = patch_nvhdmi },
  2811. { .id = 0x10de0041, .name = "GPU 41 HDMI/DP", .patch = patch_nvhdmi },
  2812. { .id = 0x10de0042, .name = "GPU 42 HDMI/DP", .patch = patch_nvhdmi },
  2813. { .id = 0x10de0043, .name = "GPU 43 HDMI/DP", .patch = patch_nvhdmi },
  2814. { .id = 0x10de0044, .name = "GPU 44 HDMI/DP", .patch = patch_nvhdmi },
  2815. { .id = 0x10de0051, .name = "GPU 51 HDMI/DP", .patch = patch_nvhdmi },
  2816. { .id = 0x10de0060, .name = "GPU 60 HDMI/DP", .patch = patch_nvhdmi },
  2817. { .id = 0x10de0067, .name = "MCP67 HDMI", .patch = patch_nvhdmi_2ch },
  2818. { .id = 0x10de0070, .name = "GPU 70 HDMI/DP", .patch = patch_nvhdmi },
  2819. { .id = 0x10de0071, .name = "GPU 71 HDMI/DP", .patch = patch_nvhdmi },
  2820. { .id = 0x10de8001, .name = "MCP73 HDMI", .patch = patch_nvhdmi_2ch },
  2821. { .id = 0x11069f80, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
  2822. { .id = 0x11069f81, .name = "VX900 HDMI/DP", .patch = patch_via_hdmi },
  2823. { .id = 0x11069f84, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
  2824. { .id = 0x11069f85, .name = "VX11 HDMI/DP", .patch = patch_generic_hdmi },
  2825. { .id = 0x80860054, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
  2826. { .id = 0x80862801, .name = "Bearlake HDMI", .patch = patch_generic_hdmi },
  2827. { .id = 0x80862802, .name = "Cantiga HDMI", .patch = patch_generic_hdmi },
  2828. { .id = 0x80862803, .name = "Eaglelake HDMI", .patch = patch_generic_hdmi },
  2829. { .id = 0x80862804, .name = "IbexPeak HDMI", .patch = patch_generic_hdmi },
  2830. { .id = 0x80862805, .name = "CougarPoint HDMI", .patch = patch_generic_hdmi },
  2831. { .id = 0x80862806, .name = "PantherPoint HDMI", .patch = patch_generic_hdmi },
  2832. { .id = 0x80862807, .name = "Haswell HDMI", .patch = patch_generic_hdmi },
  2833. { .id = 0x80862808, .name = "Broadwell HDMI", .patch = patch_generic_hdmi },
  2834. { .id = 0x80862880, .name = "CedarTrail HDMI", .patch = patch_generic_hdmi },
  2835. { .id = 0x80862882, .name = "Valleyview2 HDMI", .patch = patch_generic_hdmi },
  2836. { .id = 0x808629fb, .name = "Crestline HDMI", .patch = patch_generic_hdmi },
  2837. {} /* terminator */
  2838. };
  2839. MODULE_ALIAS("snd-hda-codec-id:1002793c");
  2840. MODULE_ALIAS("snd-hda-codec-id:10027919");
  2841. MODULE_ALIAS("snd-hda-codec-id:1002791a");
  2842. MODULE_ALIAS("snd-hda-codec-id:1002aa01");
  2843. MODULE_ALIAS("snd-hda-codec-id:10951390");
  2844. MODULE_ALIAS("snd-hda-codec-id:10951392");
  2845. MODULE_ALIAS("snd-hda-codec-id:10de0002");
  2846. MODULE_ALIAS("snd-hda-codec-id:10de0003");
  2847. MODULE_ALIAS("snd-hda-codec-id:10de0005");
  2848. MODULE_ALIAS("snd-hda-codec-id:10de0006");
  2849. MODULE_ALIAS("snd-hda-codec-id:10de0007");
  2850. MODULE_ALIAS("snd-hda-codec-id:10de000a");
  2851. MODULE_ALIAS("snd-hda-codec-id:10de000b");
  2852. MODULE_ALIAS("snd-hda-codec-id:10de000c");
  2853. MODULE_ALIAS("snd-hda-codec-id:10de000d");
  2854. MODULE_ALIAS("snd-hda-codec-id:10de0010");
  2855. MODULE_ALIAS("snd-hda-codec-id:10de0011");
  2856. MODULE_ALIAS("snd-hda-codec-id:10de0012");
  2857. MODULE_ALIAS("snd-hda-codec-id:10de0013");
  2858. MODULE_ALIAS("snd-hda-codec-id:10de0014");
  2859. MODULE_ALIAS("snd-hda-codec-id:10de0015");
  2860. MODULE_ALIAS("snd-hda-codec-id:10de0016");
  2861. MODULE_ALIAS("snd-hda-codec-id:10de0018");
  2862. MODULE_ALIAS("snd-hda-codec-id:10de0019");
  2863. MODULE_ALIAS("snd-hda-codec-id:10de001a");
  2864. MODULE_ALIAS("snd-hda-codec-id:10de001b");
  2865. MODULE_ALIAS("snd-hda-codec-id:10de001c");
  2866. MODULE_ALIAS("snd-hda-codec-id:10de0028");
  2867. MODULE_ALIAS("snd-hda-codec-id:10de0040");
  2868. MODULE_ALIAS("snd-hda-codec-id:10de0041");
  2869. MODULE_ALIAS("snd-hda-codec-id:10de0042");
  2870. MODULE_ALIAS("snd-hda-codec-id:10de0043");
  2871. MODULE_ALIAS("snd-hda-codec-id:10de0044");
  2872. MODULE_ALIAS("snd-hda-codec-id:10de0051");
  2873. MODULE_ALIAS("snd-hda-codec-id:10de0060");
  2874. MODULE_ALIAS("snd-hda-codec-id:10de0067");
  2875. MODULE_ALIAS("snd-hda-codec-id:10de0070");
  2876. MODULE_ALIAS("snd-hda-codec-id:10de0071");
  2877. MODULE_ALIAS("snd-hda-codec-id:10de8001");
  2878. MODULE_ALIAS("snd-hda-codec-id:11069f80");
  2879. MODULE_ALIAS("snd-hda-codec-id:11069f81");
  2880. MODULE_ALIAS("snd-hda-codec-id:11069f84");
  2881. MODULE_ALIAS("snd-hda-codec-id:11069f85");
  2882. MODULE_ALIAS("snd-hda-codec-id:17e80047");
  2883. MODULE_ALIAS("snd-hda-codec-id:80860054");
  2884. MODULE_ALIAS("snd-hda-codec-id:80862801");
  2885. MODULE_ALIAS("snd-hda-codec-id:80862802");
  2886. MODULE_ALIAS("snd-hda-codec-id:80862803");
  2887. MODULE_ALIAS("snd-hda-codec-id:80862804");
  2888. MODULE_ALIAS("snd-hda-codec-id:80862805");
  2889. MODULE_ALIAS("snd-hda-codec-id:80862806");
  2890. MODULE_ALIAS("snd-hda-codec-id:80862807");
  2891. MODULE_ALIAS("snd-hda-codec-id:80862808");
  2892. MODULE_ALIAS("snd-hda-codec-id:80862880");
  2893. MODULE_ALIAS("snd-hda-codec-id:80862882");
  2894. MODULE_ALIAS("snd-hda-codec-id:808629fb");
  2895. MODULE_LICENSE("GPL");
  2896. MODULE_DESCRIPTION("HDMI HD-audio codec");
  2897. MODULE_ALIAS("snd-hda-codec-intelhdmi");
  2898. MODULE_ALIAS("snd-hda-codec-nvhdmi");
  2899. MODULE_ALIAS("snd-hda-codec-atihdmi");
  2900. static struct hda_codec_preset_list intel_list = {
  2901. .preset = snd_hda_preset_hdmi,
  2902. .owner = THIS_MODULE,
  2903. };
  2904. static int __init patch_hdmi_init(void)
  2905. {
  2906. return snd_hda_add_codec_preset(&intel_list);
  2907. }
  2908. static void __exit patch_hdmi_exit(void)
  2909. {
  2910. snd_hda_delete_codec_preset(&intel_list);
  2911. }
  2912. module_init(patch_hdmi_init)
  2913. module_exit(patch_hdmi_exit)