patch_hdmi.c 101 KB

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