hda_generic.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632
  1. /*
  2. * Universal Interface for Intel High Definition Audio Codec
  3. *
  4. * Generic widget tree parser
  5. *
  6. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  7. *
  8. * This driver is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This driver is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/export.h>
  25. #include <linux/sort.h>
  26. #include <linux/ctype.h>
  27. #include <linux/string.h>
  28. #include <linux/bitops.h>
  29. #include <sound/core.h>
  30. #include <sound/jack.h>
  31. #include "hda_codec.h"
  32. #include "hda_local.h"
  33. #include "hda_auto_parser.h"
  34. #include "hda_jack.h"
  35. #include "hda_generic.h"
  36. /* initialize hda_gen_spec struct */
  37. int snd_hda_gen_spec_init(struct hda_gen_spec *spec)
  38. {
  39. snd_array_init(&spec->kctls, sizeof(struct snd_kcontrol_new), 32);
  40. snd_array_init(&spec->paths, sizeof(struct nid_path), 8);
  41. mutex_init(&spec->pcm_mutex);
  42. return 0;
  43. }
  44. EXPORT_SYMBOL_HDA(snd_hda_gen_spec_init);
  45. struct snd_kcontrol_new *
  46. snd_hda_gen_add_kctl(struct hda_gen_spec *spec, const char *name,
  47. const struct snd_kcontrol_new *temp)
  48. {
  49. struct snd_kcontrol_new *knew = snd_array_new(&spec->kctls);
  50. if (!knew)
  51. return NULL;
  52. *knew = *temp;
  53. if (name)
  54. knew->name = kstrdup(name, GFP_KERNEL);
  55. else if (knew->name)
  56. knew->name = kstrdup(knew->name, GFP_KERNEL);
  57. if (!knew->name)
  58. return NULL;
  59. return knew;
  60. }
  61. EXPORT_SYMBOL_HDA(snd_hda_gen_add_kctl);
  62. static void free_kctls(struct hda_gen_spec *spec)
  63. {
  64. if (spec->kctls.list) {
  65. struct snd_kcontrol_new *kctl = spec->kctls.list;
  66. int i;
  67. for (i = 0; i < spec->kctls.used; i++)
  68. kfree(kctl[i].name);
  69. }
  70. snd_array_free(&spec->kctls);
  71. }
  72. void snd_hda_gen_spec_free(struct hda_gen_spec *spec)
  73. {
  74. if (!spec)
  75. return;
  76. free_kctls(spec);
  77. snd_array_free(&spec->paths);
  78. }
  79. EXPORT_SYMBOL_HDA(snd_hda_gen_spec_free);
  80. /*
  81. * store user hints
  82. */
  83. static void parse_user_hints(struct hda_codec *codec)
  84. {
  85. struct hda_gen_spec *spec = codec->spec;
  86. int val;
  87. val = snd_hda_get_bool_hint(codec, "jack_detect");
  88. if (val >= 0)
  89. codec->no_jack_detect = !val;
  90. val = snd_hda_get_bool_hint(codec, "inv_jack_detect");
  91. if (val >= 0)
  92. codec->inv_jack_detect = !!val;
  93. val = snd_hda_get_bool_hint(codec, "trigger_sense");
  94. if (val >= 0)
  95. codec->no_trigger_sense = !val;
  96. val = snd_hda_get_bool_hint(codec, "inv_eapd");
  97. if (val >= 0)
  98. codec->inv_eapd = !!val;
  99. val = snd_hda_get_bool_hint(codec, "pcm_format_first");
  100. if (val >= 0)
  101. codec->pcm_format_first = !!val;
  102. val = snd_hda_get_bool_hint(codec, "sticky_stream");
  103. if (val >= 0)
  104. codec->no_sticky_stream = !val;
  105. val = snd_hda_get_bool_hint(codec, "spdif_status_reset");
  106. if (val >= 0)
  107. codec->spdif_status_reset = !!val;
  108. val = snd_hda_get_bool_hint(codec, "pin_amp_workaround");
  109. if (val >= 0)
  110. codec->pin_amp_workaround = !!val;
  111. val = snd_hda_get_bool_hint(codec, "single_adc_amp");
  112. if (val >= 0)
  113. codec->single_adc_amp = !!val;
  114. val = snd_hda_get_bool_hint(codec, "auto_mute");
  115. if (val >= 0)
  116. spec->suppress_auto_mute = !val;
  117. val = snd_hda_get_bool_hint(codec, "auto_mic");
  118. if (val >= 0)
  119. spec->suppress_auto_mic = !val;
  120. val = snd_hda_get_bool_hint(codec, "line_in_auto_switch");
  121. if (val >= 0)
  122. spec->line_in_auto_switch = !!val;
  123. val = snd_hda_get_bool_hint(codec, "need_dac_fix");
  124. if (val >= 0)
  125. spec->need_dac_fix = !!val;
  126. val = snd_hda_get_bool_hint(codec, "primary_hp");
  127. if (val >= 0)
  128. spec->no_primary_hp = !val;
  129. val = snd_hda_get_bool_hint(codec, "multi_cap_vol");
  130. if (val >= 0)
  131. spec->multi_cap_vol = !!val;
  132. val = snd_hda_get_bool_hint(codec, "inv_dmic_split");
  133. if (val >= 0)
  134. spec->inv_dmic_split = !!val;
  135. val = snd_hda_get_bool_hint(codec, "indep_hp");
  136. if (val >= 0)
  137. spec->indep_hp = !!val;
  138. val = snd_hda_get_bool_hint(codec, "add_stereo_mix_input");
  139. if (val >= 0)
  140. spec->add_stereo_mix_input = !!val;
  141. val = snd_hda_get_bool_hint(codec, "add_out_jack_modes");
  142. if (val >= 0)
  143. spec->add_out_jack_modes = !!val;
  144. val = snd_hda_get_bool_hint(codec, "add_in_jack_modes");
  145. if (val >= 0)
  146. spec->add_in_jack_modes = !!val;
  147. if (!snd_hda_get_int_hint(codec, "mixer_nid", &val))
  148. spec->mixer_nid = val;
  149. }
  150. /*
  151. * pin control value accesses
  152. */
  153. #define update_pin_ctl(codec, pin, val) \
  154. snd_hda_codec_update_cache(codec, pin, 0, \
  155. AC_VERB_SET_PIN_WIDGET_CONTROL, val)
  156. /* restore the pinctl based on the cached value */
  157. static inline void restore_pin_ctl(struct hda_codec *codec, hda_nid_t pin)
  158. {
  159. update_pin_ctl(codec, pin, snd_hda_codec_get_pin_target(codec, pin));
  160. }
  161. /* set the pinctl target value and write it if requested */
  162. static void set_pin_target(struct hda_codec *codec, hda_nid_t pin,
  163. unsigned int val, bool do_write)
  164. {
  165. if (!pin)
  166. return;
  167. val = snd_hda_correct_pin_ctl(codec, pin, val);
  168. snd_hda_codec_set_pin_target(codec, pin, val);
  169. if (do_write)
  170. update_pin_ctl(codec, pin, val);
  171. }
  172. /* set pinctl target values for all given pins */
  173. static void set_pin_targets(struct hda_codec *codec, int num_pins,
  174. hda_nid_t *pins, unsigned int val)
  175. {
  176. int i;
  177. for (i = 0; i < num_pins; i++)
  178. set_pin_target(codec, pins[i], val, false);
  179. }
  180. /*
  181. * parsing paths
  182. */
  183. /* return the position of NID in the list, or -1 if not found */
  184. static int find_idx_in_nid_list(hda_nid_t nid, const hda_nid_t *list, int nums)
  185. {
  186. int i;
  187. for (i = 0; i < nums; i++)
  188. if (list[i] == nid)
  189. return i;
  190. return -1;
  191. }
  192. /* return true if the given NID is contained in the path */
  193. static bool is_nid_contained(struct nid_path *path, hda_nid_t nid)
  194. {
  195. return find_idx_in_nid_list(nid, path->path, path->depth) >= 0;
  196. }
  197. static struct nid_path *get_nid_path(struct hda_codec *codec,
  198. hda_nid_t from_nid, hda_nid_t to_nid,
  199. int anchor_nid)
  200. {
  201. struct hda_gen_spec *spec = codec->spec;
  202. int i;
  203. for (i = 0; i < spec->paths.used; i++) {
  204. struct nid_path *path = snd_array_elem(&spec->paths, i);
  205. if (path->depth <= 0)
  206. continue;
  207. if ((!from_nid || path->path[0] == from_nid) &&
  208. (!to_nid || path->path[path->depth - 1] == to_nid)) {
  209. if (!anchor_nid ||
  210. (anchor_nid > 0 && is_nid_contained(path, anchor_nid)) ||
  211. (anchor_nid < 0 && !is_nid_contained(path, anchor_nid)))
  212. return path;
  213. }
  214. }
  215. return NULL;
  216. }
  217. /* get the path between the given NIDs;
  218. * passing 0 to either @pin or @dac behaves as a wildcard
  219. */
  220. struct nid_path *snd_hda_get_nid_path(struct hda_codec *codec,
  221. hda_nid_t from_nid, hda_nid_t to_nid)
  222. {
  223. return get_nid_path(codec, from_nid, to_nid, 0);
  224. }
  225. EXPORT_SYMBOL_HDA(snd_hda_get_nid_path);
  226. /* get the index number corresponding to the path instance;
  227. * the index starts from 1, for easier checking the invalid value
  228. */
  229. int snd_hda_get_path_idx(struct hda_codec *codec, struct nid_path *path)
  230. {
  231. struct hda_gen_spec *spec = codec->spec;
  232. struct nid_path *array = spec->paths.list;
  233. ssize_t idx;
  234. if (!spec->paths.used)
  235. return 0;
  236. idx = path - array;
  237. if (idx < 0 || idx >= spec->paths.used)
  238. return 0;
  239. return idx + 1;
  240. }
  241. /* get the path instance corresponding to the given index number */
  242. struct nid_path *snd_hda_get_path_from_idx(struct hda_codec *codec, int idx)
  243. {
  244. struct hda_gen_spec *spec = codec->spec;
  245. if (idx <= 0 || idx > spec->paths.used)
  246. return NULL;
  247. return snd_array_elem(&spec->paths, idx - 1);
  248. }
  249. /* check whether the given DAC is already found in any existing paths */
  250. static bool is_dac_already_used(struct hda_codec *codec, hda_nid_t nid)
  251. {
  252. struct hda_gen_spec *spec = codec->spec;
  253. int i;
  254. for (i = 0; i < spec->paths.used; i++) {
  255. struct nid_path *path = snd_array_elem(&spec->paths, i);
  256. if (path->path[0] == nid)
  257. return true;
  258. }
  259. return false;
  260. }
  261. /* check whether the given two widgets can be connected */
  262. static bool is_reachable_path(struct hda_codec *codec,
  263. hda_nid_t from_nid, hda_nid_t to_nid)
  264. {
  265. if (!from_nid || !to_nid)
  266. return false;
  267. return snd_hda_get_conn_index(codec, to_nid, from_nid, true) >= 0;
  268. }
  269. /* nid, dir and idx */
  270. #define AMP_VAL_COMPARE_MASK (0xffff | (1U << 18) | (0x0f << 19))
  271. /* check whether the given ctl is already assigned in any path elements */
  272. static bool is_ctl_used(struct hda_codec *codec, unsigned int val, int type)
  273. {
  274. struct hda_gen_spec *spec = codec->spec;
  275. int i;
  276. val &= AMP_VAL_COMPARE_MASK;
  277. for (i = 0; i < spec->paths.used; i++) {
  278. struct nid_path *path = snd_array_elem(&spec->paths, i);
  279. if ((path->ctls[type] & AMP_VAL_COMPARE_MASK) == val)
  280. return true;
  281. }
  282. return false;
  283. }
  284. /* check whether a control with the given (nid, dir, idx) was assigned */
  285. static bool is_ctl_associated(struct hda_codec *codec, hda_nid_t nid,
  286. int dir, int idx)
  287. {
  288. unsigned int val = HDA_COMPOSE_AMP_VAL(nid, 3, idx, dir);
  289. return is_ctl_used(codec, val, NID_PATH_VOL_CTL) ||
  290. is_ctl_used(codec, val, NID_PATH_MUTE_CTL);
  291. }
  292. static void print_nid_path(const char *pfx, struct nid_path *path)
  293. {
  294. char buf[40];
  295. int i;
  296. buf[0] = 0;
  297. for (i = 0; i < path->depth; i++) {
  298. char tmp[4];
  299. sprintf(tmp, ":%02x", path->path[i]);
  300. strlcat(buf, tmp, sizeof(buf));
  301. }
  302. snd_printdd("%s path: depth=%d %s\n", pfx, path->depth, buf);
  303. }
  304. /* called recursively */
  305. static bool __parse_nid_path(struct hda_codec *codec,
  306. hda_nid_t from_nid, hda_nid_t to_nid,
  307. int anchor_nid, struct nid_path *path,
  308. int depth)
  309. {
  310. const hda_nid_t *conn;
  311. int i, nums;
  312. if (to_nid == anchor_nid)
  313. anchor_nid = 0; /* anchor passed */
  314. else if (to_nid == (hda_nid_t)(-anchor_nid))
  315. return false; /* hit the exclusive nid */
  316. nums = snd_hda_get_conn_list(codec, to_nid, &conn);
  317. for (i = 0; i < nums; i++) {
  318. if (conn[i] != from_nid) {
  319. /* special case: when from_nid is 0,
  320. * try to find an empty DAC
  321. */
  322. if (from_nid ||
  323. get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT ||
  324. is_dac_already_used(codec, conn[i]))
  325. continue;
  326. }
  327. /* anchor is not requested or already passed? */
  328. if (anchor_nid <= 0)
  329. goto found;
  330. }
  331. if (depth >= MAX_NID_PATH_DEPTH)
  332. return false;
  333. for (i = 0; i < nums; i++) {
  334. unsigned int type;
  335. type = get_wcaps_type(get_wcaps(codec, conn[i]));
  336. if (type == AC_WID_AUD_OUT || type == AC_WID_AUD_IN ||
  337. type == AC_WID_PIN)
  338. continue;
  339. if (__parse_nid_path(codec, from_nid, conn[i],
  340. anchor_nid, path, depth + 1))
  341. goto found;
  342. }
  343. return false;
  344. found:
  345. path->path[path->depth] = conn[i];
  346. path->idx[path->depth + 1] = i;
  347. if (nums > 1 && get_wcaps_type(get_wcaps(codec, to_nid)) != AC_WID_AUD_MIX)
  348. path->multi[path->depth + 1] = 1;
  349. path->depth++;
  350. return true;
  351. }
  352. /* parse the widget path from the given nid to the target nid;
  353. * when @from_nid is 0, try to find an empty DAC;
  354. * when @anchor_nid is set to a positive value, only paths through the widget
  355. * with the given value are evaluated.
  356. * when @anchor_nid is set to a negative value, paths through the widget
  357. * with the negative of given value are excluded, only other paths are chosen.
  358. * when @anchor_nid is zero, no special handling about path selection.
  359. */
  360. bool snd_hda_parse_nid_path(struct hda_codec *codec, hda_nid_t from_nid,
  361. hda_nid_t to_nid, int anchor_nid,
  362. struct nid_path *path)
  363. {
  364. if (__parse_nid_path(codec, from_nid, to_nid, anchor_nid, path, 1)) {
  365. path->path[path->depth] = to_nid;
  366. path->depth++;
  367. return true;
  368. }
  369. return false;
  370. }
  371. EXPORT_SYMBOL_HDA(snd_hda_parse_nid_path);
  372. /*
  373. * parse the path between the given NIDs and add to the path list.
  374. * if no valid path is found, return NULL
  375. */
  376. struct nid_path *
  377. snd_hda_add_new_path(struct hda_codec *codec, hda_nid_t from_nid,
  378. hda_nid_t to_nid, int anchor_nid)
  379. {
  380. struct hda_gen_spec *spec = codec->spec;
  381. struct nid_path *path;
  382. if (from_nid && to_nid && !is_reachable_path(codec, from_nid, to_nid))
  383. return NULL;
  384. /* check whether the path has been already added */
  385. path = get_nid_path(codec, from_nid, to_nid, anchor_nid);
  386. if (path)
  387. return path;
  388. path = snd_array_new(&spec->paths);
  389. if (!path)
  390. return NULL;
  391. memset(path, 0, sizeof(*path));
  392. if (snd_hda_parse_nid_path(codec, from_nid, to_nid, anchor_nid, path))
  393. return path;
  394. /* push back */
  395. spec->paths.used--;
  396. return NULL;
  397. }
  398. EXPORT_SYMBOL_HDA(snd_hda_add_new_path);
  399. /* clear the given path as invalid so that it won't be picked up later */
  400. static void invalidate_nid_path(struct hda_codec *codec, int idx)
  401. {
  402. struct nid_path *path = snd_hda_get_path_from_idx(codec, idx);
  403. if (!path)
  404. return;
  405. memset(path, 0, sizeof(*path));
  406. }
  407. /* look for an empty DAC slot */
  408. static hda_nid_t look_for_dac(struct hda_codec *codec, hda_nid_t pin,
  409. bool is_digital)
  410. {
  411. struct hda_gen_spec *spec = codec->spec;
  412. bool cap_digital;
  413. int i;
  414. for (i = 0; i < spec->num_all_dacs; i++) {
  415. hda_nid_t nid = spec->all_dacs[i];
  416. if (!nid || is_dac_already_used(codec, nid))
  417. continue;
  418. cap_digital = !!(get_wcaps(codec, nid) & AC_WCAP_DIGITAL);
  419. if (is_digital != cap_digital)
  420. continue;
  421. if (is_reachable_path(codec, nid, pin))
  422. return nid;
  423. }
  424. return 0;
  425. }
  426. /* replace the channels in the composed amp value with the given number */
  427. static unsigned int amp_val_replace_channels(unsigned int val, unsigned int chs)
  428. {
  429. val &= ~(0x3U << 16);
  430. val |= chs << 16;
  431. return val;
  432. }
  433. /* check whether the widget has the given amp capability for the direction */
  434. static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid,
  435. int dir, unsigned int bits)
  436. {
  437. if (!nid)
  438. return false;
  439. if (get_wcaps(codec, nid) & (1 << (dir + 1)))
  440. if (query_amp_caps(codec, nid, dir) & bits)
  441. return true;
  442. return false;
  443. }
  444. static bool same_amp_caps(struct hda_codec *codec, hda_nid_t nid1,
  445. hda_nid_t nid2, int dir)
  446. {
  447. if (!(get_wcaps(codec, nid1) & (1 << (dir + 1))))
  448. return !(get_wcaps(codec, nid2) & (1 << (dir + 1)));
  449. return (query_amp_caps(codec, nid1, dir) ==
  450. query_amp_caps(codec, nid2, dir));
  451. }
  452. #define nid_has_mute(codec, nid, dir) \
  453. check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
  454. #define nid_has_volume(codec, nid, dir) \
  455. check_amp_caps(codec, nid, dir, AC_AMPCAP_NUM_STEPS)
  456. /* look for a widget suitable for assigning a mute switch in the path */
  457. static hda_nid_t look_for_out_mute_nid(struct hda_codec *codec,
  458. struct nid_path *path)
  459. {
  460. int i;
  461. for (i = path->depth - 1; i >= 0; i--) {
  462. if (nid_has_mute(codec, path->path[i], HDA_OUTPUT))
  463. return path->path[i];
  464. if (i != path->depth - 1 && i != 0 &&
  465. nid_has_mute(codec, path->path[i], HDA_INPUT))
  466. return path->path[i];
  467. }
  468. return 0;
  469. }
  470. /* look for a widget suitable for assigning a volume ctl in the path */
  471. static hda_nid_t look_for_out_vol_nid(struct hda_codec *codec,
  472. struct nid_path *path)
  473. {
  474. int i;
  475. for (i = path->depth - 1; i >= 0; i--) {
  476. if (nid_has_volume(codec, path->path[i], HDA_OUTPUT))
  477. return path->path[i];
  478. }
  479. return 0;
  480. }
  481. /*
  482. * path activation / deactivation
  483. */
  484. /* can have the amp-in capability? */
  485. static bool has_amp_in(struct hda_codec *codec, struct nid_path *path, int idx)
  486. {
  487. hda_nid_t nid = path->path[idx];
  488. unsigned int caps = get_wcaps(codec, nid);
  489. unsigned int type = get_wcaps_type(caps);
  490. if (!(caps & AC_WCAP_IN_AMP))
  491. return false;
  492. if (type == AC_WID_PIN && idx > 0) /* only for input pins */
  493. return false;
  494. return true;
  495. }
  496. /* can have the amp-out capability? */
  497. static bool has_amp_out(struct hda_codec *codec, struct nid_path *path, int idx)
  498. {
  499. hda_nid_t nid = path->path[idx];
  500. unsigned int caps = get_wcaps(codec, nid);
  501. unsigned int type = get_wcaps_type(caps);
  502. if (!(caps & AC_WCAP_OUT_AMP))
  503. return false;
  504. if (type == AC_WID_PIN && !idx) /* only for output pins */
  505. return false;
  506. return true;
  507. }
  508. /* check whether the given (nid,dir,idx) is active */
  509. static bool is_active_nid(struct hda_codec *codec, hda_nid_t nid,
  510. unsigned int idx, unsigned int dir)
  511. {
  512. struct hda_gen_spec *spec = codec->spec;
  513. int i, n;
  514. for (n = 0; n < spec->paths.used; n++) {
  515. struct nid_path *path = snd_array_elem(&spec->paths, n);
  516. if (!path->active)
  517. continue;
  518. for (i = 0; i < path->depth; i++) {
  519. if (path->path[i] == nid) {
  520. if (dir == HDA_OUTPUT || path->idx[i] == idx)
  521. return true;
  522. break;
  523. }
  524. }
  525. }
  526. return false;
  527. }
  528. /* get the default amp value for the target state */
  529. static int get_amp_val_to_activate(struct hda_codec *codec, hda_nid_t nid,
  530. int dir, bool enable)
  531. {
  532. unsigned int caps;
  533. unsigned int val = 0;
  534. caps = query_amp_caps(codec, nid, dir);
  535. if (caps & AC_AMPCAP_NUM_STEPS) {
  536. /* set to 0dB */
  537. if (enable)
  538. val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
  539. }
  540. if (caps & AC_AMPCAP_MUTE) {
  541. if (!enable)
  542. val |= HDA_AMP_MUTE;
  543. }
  544. return val;
  545. }
  546. /* initialize the amp value (only at the first time) */
  547. static void init_amp(struct hda_codec *codec, hda_nid_t nid, int dir, int idx)
  548. {
  549. int val = get_amp_val_to_activate(codec, nid, dir, false);
  550. snd_hda_codec_amp_init_stereo(codec, nid, dir, idx, 0xff, val);
  551. }
  552. static void activate_amp(struct hda_codec *codec, hda_nid_t nid, int dir,
  553. int idx, bool enable)
  554. {
  555. int val;
  556. if (is_ctl_associated(codec, nid, dir, idx) ||
  557. (!enable && is_active_nid(codec, nid, dir, idx)))
  558. return;
  559. val = get_amp_val_to_activate(codec, nid, dir, enable);
  560. snd_hda_codec_amp_stereo(codec, nid, dir, idx, 0xff, val);
  561. }
  562. static void activate_amp_out(struct hda_codec *codec, struct nid_path *path,
  563. int i, bool enable)
  564. {
  565. hda_nid_t nid = path->path[i];
  566. init_amp(codec, nid, HDA_OUTPUT, 0);
  567. activate_amp(codec, nid, HDA_OUTPUT, 0, enable);
  568. }
  569. static void activate_amp_in(struct hda_codec *codec, struct nid_path *path,
  570. int i, bool enable, bool add_aamix)
  571. {
  572. struct hda_gen_spec *spec = codec->spec;
  573. const hda_nid_t *conn;
  574. int n, nums, idx;
  575. int type;
  576. hda_nid_t nid = path->path[i];
  577. nums = snd_hda_get_conn_list(codec, nid, &conn);
  578. type = get_wcaps_type(get_wcaps(codec, nid));
  579. if (type == AC_WID_PIN ||
  580. (type == AC_WID_AUD_IN && codec->single_adc_amp)) {
  581. nums = 1;
  582. idx = 0;
  583. } else
  584. idx = path->idx[i];
  585. for (n = 0; n < nums; n++)
  586. init_amp(codec, nid, HDA_INPUT, n);
  587. if (is_ctl_associated(codec, nid, HDA_INPUT, idx))
  588. return;
  589. /* here is a little bit tricky in comparison with activate_amp_out();
  590. * when aa-mixer is available, we need to enable the path as well
  591. */
  592. for (n = 0; n < nums; n++) {
  593. if (n != idx && (!add_aamix || conn[n] != spec->mixer_nid))
  594. continue;
  595. activate_amp(codec, nid, HDA_INPUT, n, enable);
  596. }
  597. }
  598. /* activate or deactivate the given path
  599. * if @add_aamix is set, enable the input from aa-mix NID as well (if any)
  600. */
  601. void snd_hda_activate_path(struct hda_codec *codec, struct nid_path *path,
  602. bool enable, bool add_aamix)
  603. {
  604. int i;
  605. if (!enable)
  606. path->active = false;
  607. for (i = path->depth - 1; i >= 0; i--) {
  608. if (enable && path->multi[i])
  609. snd_hda_codec_write_cache(codec, path->path[i], 0,
  610. AC_VERB_SET_CONNECT_SEL,
  611. path->idx[i]);
  612. if (has_amp_in(codec, path, i))
  613. activate_amp_in(codec, path, i, enable, add_aamix);
  614. if (has_amp_out(codec, path, i))
  615. activate_amp_out(codec, path, i, enable);
  616. }
  617. if (enable)
  618. path->active = true;
  619. }
  620. EXPORT_SYMBOL_HDA(snd_hda_activate_path);
  621. /* turn on/off EAPD on the given pin */
  622. static void set_pin_eapd(struct hda_codec *codec, hda_nid_t pin, bool enable)
  623. {
  624. struct hda_gen_spec *spec = codec->spec;
  625. if (spec->own_eapd_ctl ||
  626. !(snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD))
  627. return;
  628. if (codec->inv_eapd)
  629. enable = !enable;
  630. snd_hda_codec_update_cache(codec, pin, 0,
  631. AC_VERB_SET_EAPD_BTLENABLE,
  632. enable ? 0x02 : 0x00);
  633. }
  634. /*
  635. * Helper functions for creating mixer ctl elements
  636. */
  637. enum {
  638. HDA_CTL_WIDGET_VOL,
  639. HDA_CTL_WIDGET_MUTE,
  640. HDA_CTL_BIND_MUTE,
  641. };
  642. static const struct snd_kcontrol_new control_templates[] = {
  643. HDA_CODEC_VOLUME(NULL, 0, 0, 0),
  644. HDA_CODEC_MUTE(NULL, 0, 0, 0),
  645. HDA_BIND_MUTE(NULL, 0, 0, 0),
  646. };
  647. /* add dynamic controls from template */
  648. static int add_control(struct hda_gen_spec *spec, int type, const char *name,
  649. int cidx, unsigned long val)
  650. {
  651. struct snd_kcontrol_new *knew;
  652. knew = snd_hda_gen_add_kctl(spec, name, &control_templates[type]);
  653. if (!knew)
  654. return -ENOMEM;
  655. knew->index = cidx;
  656. if (get_amp_nid_(val))
  657. knew->subdevice = HDA_SUBDEV_AMP_FLAG;
  658. knew->private_value = val;
  659. return 0;
  660. }
  661. static int add_control_with_pfx(struct hda_gen_spec *spec, int type,
  662. const char *pfx, const char *dir,
  663. const char *sfx, int cidx, unsigned long val)
  664. {
  665. char name[32];
  666. snprintf(name, sizeof(name), "%s %s %s", pfx, dir, sfx);
  667. return add_control(spec, type, name, cidx, val);
  668. }
  669. #define add_pb_vol_ctrl(spec, type, pfx, val) \
  670. add_control_with_pfx(spec, type, pfx, "Playback", "Volume", 0, val)
  671. #define add_pb_sw_ctrl(spec, type, pfx, val) \
  672. add_control_with_pfx(spec, type, pfx, "Playback", "Switch", 0, val)
  673. #define __add_pb_vol_ctrl(spec, type, pfx, cidx, val) \
  674. add_control_with_pfx(spec, type, pfx, "Playback", "Volume", cidx, val)
  675. #define __add_pb_sw_ctrl(spec, type, pfx, cidx, val) \
  676. add_control_with_pfx(spec, type, pfx, "Playback", "Switch", cidx, val)
  677. static int add_vol_ctl(struct hda_codec *codec, const char *pfx, int cidx,
  678. unsigned int chs, struct nid_path *path)
  679. {
  680. unsigned int val;
  681. if (!path)
  682. return 0;
  683. val = path->ctls[NID_PATH_VOL_CTL];
  684. if (!val)
  685. return 0;
  686. val = amp_val_replace_channels(val, chs);
  687. return __add_pb_vol_ctrl(codec->spec, HDA_CTL_WIDGET_VOL, pfx, cidx, val);
  688. }
  689. /* return the channel bits suitable for the given path->ctls[] */
  690. static int get_default_ch_nums(struct hda_codec *codec, struct nid_path *path,
  691. int type)
  692. {
  693. int chs = 1; /* mono (left only) */
  694. if (path) {
  695. hda_nid_t nid = get_amp_nid_(path->ctls[type]);
  696. if (nid && (get_wcaps(codec, nid) & AC_WCAP_STEREO))
  697. chs = 3; /* stereo */
  698. }
  699. return chs;
  700. }
  701. static int add_stereo_vol(struct hda_codec *codec, const char *pfx, int cidx,
  702. struct nid_path *path)
  703. {
  704. int chs = get_default_ch_nums(codec, path, NID_PATH_VOL_CTL);
  705. return add_vol_ctl(codec, pfx, cidx, chs, path);
  706. }
  707. /* create a mute-switch for the given mixer widget;
  708. * if it has multiple sources (e.g. DAC and loopback), create a bind-mute
  709. */
  710. static int add_sw_ctl(struct hda_codec *codec, const char *pfx, int cidx,
  711. unsigned int chs, struct nid_path *path)
  712. {
  713. unsigned int val;
  714. int type = HDA_CTL_WIDGET_MUTE;
  715. if (!path)
  716. return 0;
  717. val = path->ctls[NID_PATH_MUTE_CTL];
  718. if (!val)
  719. return 0;
  720. val = amp_val_replace_channels(val, chs);
  721. if (get_amp_direction_(val) == HDA_INPUT) {
  722. hda_nid_t nid = get_amp_nid_(val);
  723. int nums = snd_hda_get_num_conns(codec, nid);
  724. if (nums > 1) {
  725. type = HDA_CTL_BIND_MUTE;
  726. val |= nums << 19;
  727. }
  728. }
  729. return __add_pb_sw_ctrl(codec->spec, type, pfx, cidx, val);
  730. }
  731. static int add_stereo_sw(struct hda_codec *codec, const char *pfx,
  732. int cidx, struct nid_path *path)
  733. {
  734. int chs = get_default_ch_nums(codec, path, NID_PATH_MUTE_CTL);
  735. return add_sw_ctl(codec, pfx, cidx, chs, path);
  736. }
  737. /* any ctl assigned to the path with the given index? */
  738. static bool path_has_mixer(struct hda_codec *codec, int path_idx, int ctl_type)
  739. {
  740. struct nid_path *path = snd_hda_get_path_from_idx(codec, path_idx);
  741. return path && path->ctls[ctl_type];
  742. }
  743. static const char * const channel_name[4] = {
  744. "Front", "Surround", "CLFE", "Side"
  745. };
  746. /* give some appropriate ctl name prefix for the given line out channel */
  747. static const char *get_line_out_pfx(struct hda_codec *codec, int ch,
  748. int *index, int ctl_type)
  749. {
  750. struct hda_gen_spec *spec = codec->spec;
  751. struct auto_pin_cfg *cfg = &spec->autocfg;
  752. *index = 0;
  753. if (cfg->line_outs == 1 && !spec->multi_ios &&
  754. !cfg->hp_outs && !cfg->speaker_outs)
  755. return spec->vmaster_mute.hook ? "PCM" : "Master";
  756. /* if there is really a single DAC used in the whole output paths,
  757. * use it master (or "PCM" if a vmaster hook is present)
  758. */
  759. if (spec->multiout.num_dacs == 1 && !spec->mixer_nid &&
  760. !spec->multiout.hp_out_nid[0] && !spec->multiout.extra_out_nid[0])
  761. return spec->vmaster_mute.hook ? "PCM" : "Master";
  762. /* multi-io channels */
  763. if (ch >= cfg->line_outs)
  764. return channel_name[ch];
  765. switch (cfg->line_out_type) {
  766. case AUTO_PIN_SPEAKER_OUT:
  767. /* if the primary channel vol/mute is shared with HP volume,
  768. * don't name it as Speaker
  769. */
  770. if (!ch && cfg->hp_outs &&
  771. !path_has_mixer(codec, spec->hp_paths[0], ctl_type))
  772. break;
  773. if (cfg->line_outs == 1)
  774. return "Speaker";
  775. if (cfg->line_outs == 2)
  776. return ch ? "Bass Speaker" : "Speaker";
  777. break;
  778. case AUTO_PIN_HP_OUT:
  779. /* if the primary channel vol/mute is shared with spk volume,
  780. * don't name it as Headphone
  781. */
  782. if (!ch && cfg->speaker_outs &&
  783. !path_has_mixer(codec, spec->speaker_paths[0], ctl_type))
  784. break;
  785. /* for multi-io case, only the primary out */
  786. if (ch && spec->multi_ios)
  787. break;
  788. *index = ch;
  789. return "Headphone";
  790. }
  791. /* for a single channel output, we don't have to name the channel */
  792. if (cfg->line_outs == 1 && !spec->multi_ios)
  793. return "PCM";
  794. if (ch >= ARRAY_SIZE(channel_name)) {
  795. snd_BUG();
  796. return "PCM";
  797. }
  798. return channel_name[ch];
  799. }
  800. /*
  801. * Parse output paths
  802. */
  803. /* badness definition */
  804. enum {
  805. /* No primary DAC is found for the main output */
  806. BAD_NO_PRIMARY_DAC = 0x10000,
  807. /* No DAC is found for the extra output */
  808. BAD_NO_DAC = 0x4000,
  809. /* No possible multi-ios */
  810. BAD_MULTI_IO = 0x103,
  811. /* No individual DAC for extra output */
  812. BAD_NO_EXTRA_DAC = 0x102,
  813. /* No individual DAC for extra surrounds */
  814. BAD_NO_EXTRA_SURR_DAC = 0x101,
  815. /* Primary DAC shared with main surrounds */
  816. BAD_SHARED_SURROUND = 0x100,
  817. /* Primary DAC shared with main CLFE */
  818. BAD_SHARED_CLFE = 0x10,
  819. /* Primary DAC shared with extra surrounds */
  820. BAD_SHARED_EXTRA_SURROUND = 0x10,
  821. /* Volume widget is shared */
  822. BAD_SHARED_VOL = 0x10,
  823. };
  824. /* look for widgets in the given path which are appropriate for
  825. * volume and mute controls, and assign the values to ctls[].
  826. *
  827. * When no appropriate widget is found in the path, the badness value
  828. * is incremented depending on the situation. The function returns the
  829. * total badness for both volume and mute controls.
  830. */
  831. static int assign_out_path_ctls(struct hda_codec *codec, struct nid_path *path)
  832. {
  833. hda_nid_t nid;
  834. unsigned int val;
  835. int badness = 0;
  836. if (!path)
  837. return BAD_SHARED_VOL * 2;
  838. if (path->ctls[NID_PATH_VOL_CTL] ||
  839. path->ctls[NID_PATH_MUTE_CTL])
  840. return 0; /* already evaluated */
  841. nid = look_for_out_vol_nid(codec, path);
  842. if (nid) {
  843. val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
  844. if (is_ctl_used(codec, val, NID_PATH_VOL_CTL))
  845. badness += BAD_SHARED_VOL;
  846. else
  847. path->ctls[NID_PATH_VOL_CTL] = val;
  848. } else
  849. badness += BAD_SHARED_VOL;
  850. nid = look_for_out_mute_nid(codec, path);
  851. if (nid) {
  852. unsigned int wid_type = get_wcaps_type(get_wcaps(codec, nid));
  853. if (wid_type == AC_WID_PIN || wid_type == AC_WID_AUD_OUT ||
  854. nid_has_mute(codec, nid, HDA_OUTPUT))
  855. val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
  856. else
  857. val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
  858. if (is_ctl_used(codec, val, NID_PATH_MUTE_CTL))
  859. badness += BAD_SHARED_VOL;
  860. else
  861. path->ctls[NID_PATH_MUTE_CTL] = val;
  862. } else
  863. badness += BAD_SHARED_VOL;
  864. return badness;
  865. }
  866. struct badness_table {
  867. int no_primary_dac; /* no primary DAC */
  868. int no_dac; /* no secondary DACs */
  869. int shared_primary; /* primary DAC is shared with main output */
  870. int shared_surr; /* secondary DAC shared with main or primary */
  871. int shared_clfe; /* third DAC shared with main or primary */
  872. int shared_surr_main; /* secondary DAC sahred with main/DAC0 */
  873. };
  874. static struct badness_table main_out_badness = {
  875. .no_primary_dac = BAD_NO_PRIMARY_DAC,
  876. .no_dac = BAD_NO_DAC,
  877. .shared_primary = BAD_NO_PRIMARY_DAC,
  878. .shared_surr = BAD_SHARED_SURROUND,
  879. .shared_clfe = BAD_SHARED_CLFE,
  880. .shared_surr_main = BAD_SHARED_SURROUND,
  881. };
  882. static struct badness_table extra_out_badness = {
  883. .no_primary_dac = BAD_NO_DAC,
  884. .no_dac = BAD_NO_DAC,
  885. .shared_primary = BAD_NO_EXTRA_DAC,
  886. .shared_surr = BAD_SHARED_EXTRA_SURROUND,
  887. .shared_clfe = BAD_SHARED_EXTRA_SURROUND,
  888. .shared_surr_main = BAD_NO_EXTRA_SURR_DAC,
  889. };
  890. /* get the DAC of the primary output corresponding to the given array index */
  891. static hda_nid_t get_primary_out(struct hda_codec *codec, int idx)
  892. {
  893. struct hda_gen_spec *spec = codec->spec;
  894. struct auto_pin_cfg *cfg = &spec->autocfg;
  895. if (cfg->line_outs > idx)
  896. return spec->private_dac_nids[idx];
  897. idx -= cfg->line_outs;
  898. if (spec->multi_ios > idx)
  899. return spec->multi_io[idx].dac;
  900. return 0;
  901. }
  902. /* return the DAC if it's reachable, otherwise zero */
  903. static inline hda_nid_t try_dac(struct hda_codec *codec,
  904. hda_nid_t dac, hda_nid_t pin)
  905. {
  906. return is_reachable_path(codec, dac, pin) ? dac : 0;
  907. }
  908. /* try to assign DACs to pins and return the resultant badness */
  909. static int try_assign_dacs(struct hda_codec *codec, int num_outs,
  910. const hda_nid_t *pins, hda_nid_t *dacs,
  911. int *path_idx,
  912. const struct badness_table *bad)
  913. {
  914. struct hda_gen_spec *spec = codec->spec;
  915. int i, j;
  916. int badness = 0;
  917. hda_nid_t dac;
  918. if (!num_outs)
  919. return 0;
  920. for (i = 0; i < num_outs; i++) {
  921. struct nid_path *path;
  922. hda_nid_t pin = pins[i];
  923. path = snd_hda_get_path_from_idx(codec, path_idx[i]);
  924. if (path) {
  925. badness += assign_out_path_ctls(codec, path);
  926. continue;
  927. }
  928. dacs[i] = look_for_dac(codec, pin, false);
  929. if (!dacs[i] && !i) {
  930. /* try to steal the DAC of surrounds for the front */
  931. for (j = 1; j < num_outs; j++) {
  932. if (is_reachable_path(codec, dacs[j], pin)) {
  933. dacs[0] = dacs[j];
  934. dacs[j] = 0;
  935. invalidate_nid_path(codec, path_idx[j]);
  936. path_idx[j] = 0;
  937. break;
  938. }
  939. }
  940. }
  941. dac = dacs[i];
  942. if (!dac) {
  943. if (num_outs > 2)
  944. dac = try_dac(codec, get_primary_out(codec, i), pin);
  945. if (!dac)
  946. dac = try_dac(codec, dacs[0], pin);
  947. if (!dac)
  948. dac = try_dac(codec, get_primary_out(codec, i), pin);
  949. if (dac) {
  950. if (!i)
  951. badness += bad->shared_primary;
  952. else if (i == 1)
  953. badness += bad->shared_surr;
  954. else
  955. badness += bad->shared_clfe;
  956. } else if (is_reachable_path(codec, spec->private_dac_nids[0], pin)) {
  957. dac = spec->private_dac_nids[0];
  958. badness += bad->shared_surr_main;
  959. } else if (!i)
  960. badness += bad->no_primary_dac;
  961. else
  962. badness += bad->no_dac;
  963. }
  964. path = snd_hda_add_new_path(codec, dac, pin, -spec->mixer_nid);
  965. if (!path && !i && spec->mixer_nid) {
  966. /* try with aamix */
  967. path = snd_hda_add_new_path(codec, dac, pin, 0);
  968. }
  969. if (!path)
  970. dac = dacs[i] = 0;
  971. else {
  972. print_nid_path("output", path);
  973. path->active = true;
  974. path_idx[i] = snd_hda_get_path_idx(codec, path);
  975. badness += assign_out_path_ctls(codec, path);
  976. }
  977. }
  978. return badness;
  979. }
  980. /* return NID if the given pin has only a single connection to a certain DAC */
  981. static hda_nid_t get_dac_if_single(struct hda_codec *codec, hda_nid_t pin)
  982. {
  983. struct hda_gen_spec *spec = codec->spec;
  984. int i;
  985. hda_nid_t nid_found = 0;
  986. for (i = 0; i < spec->num_all_dacs; i++) {
  987. hda_nid_t nid = spec->all_dacs[i];
  988. if (!nid || is_dac_already_used(codec, nid))
  989. continue;
  990. if (is_reachable_path(codec, nid, pin)) {
  991. if (nid_found)
  992. return 0;
  993. nid_found = nid;
  994. }
  995. }
  996. return nid_found;
  997. }
  998. /* check whether the given pin can be a multi-io pin */
  999. static bool can_be_multiio_pin(struct hda_codec *codec,
  1000. unsigned int location, hda_nid_t nid)
  1001. {
  1002. unsigned int defcfg, caps;
  1003. defcfg = snd_hda_codec_get_pincfg(codec, nid);
  1004. if (get_defcfg_connect(defcfg) != AC_JACK_PORT_COMPLEX)
  1005. return false;
  1006. if (location && get_defcfg_location(defcfg) != location)
  1007. return false;
  1008. caps = snd_hda_query_pin_caps(codec, nid);
  1009. if (!(caps & AC_PINCAP_OUT))
  1010. return false;
  1011. return true;
  1012. }
  1013. /* count the number of input pins that are capable to be multi-io */
  1014. static int count_multiio_pins(struct hda_codec *codec, hda_nid_t reference_pin)
  1015. {
  1016. struct hda_gen_spec *spec = codec->spec;
  1017. struct auto_pin_cfg *cfg = &spec->autocfg;
  1018. unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
  1019. unsigned int location = get_defcfg_location(defcfg);
  1020. int type, i;
  1021. int num_pins = 0;
  1022. for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
  1023. for (i = 0; i < cfg->num_inputs; i++) {
  1024. if (cfg->inputs[i].type != type)
  1025. continue;
  1026. if (can_be_multiio_pin(codec, location,
  1027. cfg->inputs[i].pin))
  1028. num_pins++;
  1029. }
  1030. }
  1031. return num_pins;
  1032. }
  1033. /*
  1034. * multi-io helper
  1035. *
  1036. * When hardwired is set, try to fill ony hardwired pins, and returns
  1037. * zero if any pins are filled, non-zero if nothing found.
  1038. * When hardwired is off, try to fill possible input pins, and returns
  1039. * the badness value.
  1040. */
  1041. static int fill_multi_ios(struct hda_codec *codec,
  1042. hda_nid_t reference_pin,
  1043. bool hardwired)
  1044. {
  1045. struct hda_gen_spec *spec = codec->spec;
  1046. struct auto_pin_cfg *cfg = &spec->autocfg;
  1047. int type, i, j, num_pins, old_pins;
  1048. unsigned int defcfg = snd_hda_codec_get_pincfg(codec, reference_pin);
  1049. unsigned int location = get_defcfg_location(defcfg);
  1050. int badness = 0;
  1051. struct nid_path *path;
  1052. old_pins = spec->multi_ios;
  1053. if (old_pins >= 2)
  1054. goto end_fill;
  1055. num_pins = count_multiio_pins(codec, reference_pin);
  1056. if (num_pins < 2)
  1057. goto end_fill;
  1058. for (type = AUTO_PIN_LINE_IN; type >= AUTO_PIN_MIC; type--) {
  1059. for (i = 0; i < cfg->num_inputs; i++) {
  1060. hda_nid_t nid = cfg->inputs[i].pin;
  1061. hda_nid_t dac = 0;
  1062. if (cfg->inputs[i].type != type)
  1063. continue;
  1064. if (!can_be_multiio_pin(codec, location, nid))
  1065. continue;
  1066. for (j = 0; j < spec->multi_ios; j++) {
  1067. if (nid == spec->multi_io[j].pin)
  1068. break;
  1069. }
  1070. if (j < spec->multi_ios)
  1071. continue;
  1072. if (hardwired)
  1073. dac = get_dac_if_single(codec, nid);
  1074. else if (!dac)
  1075. dac = look_for_dac(codec, nid, false);
  1076. if (!dac) {
  1077. badness++;
  1078. continue;
  1079. }
  1080. path = snd_hda_add_new_path(codec, dac, nid,
  1081. -spec->mixer_nid);
  1082. if (!path) {
  1083. badness++;
  1084. continue;
  1085. }
  1086. print_nid_path("multiio", path);
  1087. spec->multi_io[spec->multi_ios].pin = nid;
  1088. spec->multi_io[spec->multi_ios].dac = dac;
  1089. spec->out_paths[cfg->line_outs + spec->multi_ios] =
  1090. snd_hda_get_path_idx(codec, path);
  1091. spec->multi_ios++;
  1092. if (spec->multi_ios >= 2)
  1093. break;
  1094. }
  1095. }
  1096. end_fill:
  1097. if (badness)
  1098. badness = BAD_MULTI_IO;
  1099. if (old_pins == spec->multi_ios) {
  1100. if (hardwired)
  1101. return 1; /* nothing found */
  1102. else
  1103. return badness; /* no badness if nothing found */
  1104. }
  1105. if (!hardwired && spec->multi_ios < 2) {
  1106. /* cancel newly assigned paths */
  1107. spec->paths.used -= spec->multi_ios - old_pins;
  1108. spec->multi_ios = old_pins;
  1109. return badness;
  1110. }
  1111. /* assign volume and mute controls */
  1112. for (i = old_pins; i < spec->multi_ios; i++) {
  1113. path = snd_hda_get_path_from_idx(codec, spec->out_paths[cfg->line_outs + i]);
  1114. badness += assign_out_path_ctls(codec, path);
  1115. }
  1116. return badness;
  1117. }
  1118. /* map DACs for all pins in the list if they are single connections */
  1119. static bool map_singles(struct hda_codec *codec, int outs,
  1120. const hda_nid_t *pins, hda_nid_t *dacs, int *path_idx)
  1121. {
  1122. struct hda_gen_spec *spec = codec->spec;
  1123. int i;
  1124. bool found = false;
  1125. for (i = 0; i < outs; i++) {
  1126. struct nid_path *path;
  1127. hda_nid_t dac;
  1128. if (dacs[i])
  1129. continue;
  1130. dac = get_dac_if_single(codec, pins[i]);
  1131. if (!dac)
  1132. continue;
  1133. path = snd_hda_add_new_path(codec, dac, pins[i],
  1134. -spec->mixer_nid);
  1135. if (!path && !i && spec->mixer_nid)
  1136. path = snd_hda_add_new_path(codec, dac, pins[i], 0);
  1137. if (path) {
  1138. dacs[i] = dac;
  1139. found = true;
  1140. print_nid_path("output", path);
  1141. path->active = true;
  1142. path_idx[i] = snd_hda_get_path_idx(codec, path);
  1143. }
  1144. }
  1145. return found;
  1146. }
  1147. /* create a new path including aamix if available, and return its index */
  1148. static int check_aamix_out_path(struct hda_codec *codec, int path_idx)
  1149. {
  1150. struct hda_gen_spec *spec = codec->spec;
  1151. struct nid_path *path;
  1152. path = snd_hda_get_path_from_idx(codec, path_idx);
  1153. if (!path || !path->depth ||
  1154. is_nid_contained(path, spec->mixer_nid))
  1155. return 0;
  1156. path = snd_hda_add_new_path(codec, path->path[0],
  1157. path->path[path->depth - 1],
  1158. spec->mixer_nid);
  1159. if (!path)
  1160. return 0;
  1161. print_nid_path("output-aamix", path);
  1162. path->active = false; /* unused as default */
  1163. return snd_hda_get_path_idx(codec, path);
  1164. }
  1165. /* fill the empty entries in the dac array for speaker/hp with the
  1166. * shared dac pointed by the paths
  1167. */
  1168. static void refill_shared_dacs(struct hda_codec *codec, int num_outs,
  1169. hda_nid_t *dacs, int *path_idx)
  1170. {
  1171. struct nid_path *path;
  1172. int i;
  1173. for (i = 0; i < num_outs; i++) {
  1174. if (dacs[i])
  1175. continue;
  1176. path = snd_hda_get_path_from_idx(codec, path_idx[i]);
  1177. if (!path)
  1178. continue;
  1179. dacs[i] = path->path[0];
  1180. }
  1181. }
  1182. /* fill in the dac_nids table from the parsed pin configuration */
  1183. static int fill_and_eval_dacs(struct hda_codec *codec,
  1184. bool fill_hardwired,
  1185. bool fill_mio_first)
  1186. {
  1187. struct hda_gen_spec *spec = codec->spec;
  1188. struct auto_pin_cfg *cfg = &spec->autocfg;
  1189. int i, err, badness;
  1190. unsigned int val;
  1191. /* set num_dacs once to full for look_for_dac() */
  1192. spec->multiout.num_dacs = cfg->line_outs;
  1193. spec->multiout.dac_nids = spec->private_dac_nids;
  1194. memset(spec->private_dac_nids, 0, sizeof(spec->private_dac_nids));
  1195. memset(spec->multiout.hp_out_nid, 0, sizeof(spec->multiout.hp_out_nid));
  1196. memset(spec->multiout.extra_out_nid, 0, sizeof(spec->multiout.extra_out_nid));
  1197. spec->multi_ios = 0;
  1198. snd_array_free(&spec->paths);
  1199. /* clear path indices */
  1200. memset(spec->out_paths, 0, sizeof(spec->out_paths));
  1201. memset(spec->hp_paths, 0, sizeof(spec->hp_paths));
  1202. memset(spec->speaker_paths, 0, sizeof(spec->speaker_paths));
  1203. memset(spec->aamix_out_paths, 0, sizeof(spec->aamix_out_paths));
  1204. memset(spec->digout_paths, 0, sizeof(spec->digout_paths));
  1205. memset(spec->input_paths, 0, sizeof(spec->input_paths));
  1206. memset(spec->loopback_paths, 0, sizeof(spec->loopback_paths));
  1207. memset(&spec->digin_path, 0, sizeof(spec->digin_path));
  1208. badness = 0;
  1209. /* fill hard-wired DACs first */
  1210. if (fill_hardwired) {
  1211. bool mapped;
  1212. do {
  1213. mapped = map_singles(codec, cfg->line_outs,
  1214. cfg->line_out_pins,
  1215. spec->private_dac_nids,
  1216. spec->out_paths);
  1217. mapped |= map_singles(codec, cfg->hp_outs,
  1218. cfg->hp_pins,
  1219. spec->multiout.hp_out_nid,
  1220. spec->hp_paths);
  1221. mapped |= map_singles(codec, cfg->speaker_outs,
  1222. cfg->speaker_pins,
  1223. spec->multiout.extra_out_nid,
  1224. spec->speaker_paths);
  1225. if (fill_mio_first && cfg->line_outs == 1 &&
  1226. cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
  1227. err = fill_multi_ios(codec, cfg->line_out_pins[0], true);
  1228. if (!err)
  1229. mapped = true;
  1230. }
  1231. } while (mapped);
  1232. }
  1233. badness += try_assign_dacs(codec, cfg->line_outs, cfg->line_out_pins,
  1234. spec->private_dac_nids, spec->out_paths,
  1235. &main_out_badness);
  1236. if (fill_mio_first &&
  1237. cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
  1238. /* try to fill multi-io first */
  1239. err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
  1240. if (err < 0)
  1241. return err;
  1242. /* we don't count badness at this stage yet */
  1243. }
  1244. if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
  1245. err = try_assign_dacs(codec, cfg->hp_outs, cfg->hp_pins,
  1246. spec->multiout.hp_out_nid,
  1247. spec->hp_paths,
  1248. &extra_out_badness);
  1249. if (err < 0)
  1250. return err;
  1251. badness += err;
  1252. }
  1253. if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
  1254. err = try_assign_dacs(codec, cfg->speaker_outs,
  1255. cfg->speaker_pins,
  1256. spec->multiout.extra_out_nid,
  1257. spec->speaker_paths,
  1258. &extra_out_badness);
  1259. if (err < 0)
  1260. return err;
  1261. badness += err;
  1262. }
  1263. if (cfg->line_outs == 1 && cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
  1264. err = fill_multi_ios(codec, cfg->line_out_pins[0], false);
  1265. if (err < 0)
  1266. return err;
  1267. badness += err;
  1268. }
  1269. if (spec->mixer_nid) {
  1270. spec->aamix_out_paths[0] =
  1271. check_aamix_out_path(codec, spec->out_paths[0]);
  1272. if (cfg->line_out_type != AUTO_PIN_HP_OUT)
  1273. spec->aamix_out_paths[1] =
  1274. check_aamix_out_path(codec, spec->hp_paths[0]);
  1275. if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
  1276. spec->aamix_out_paths[2] =
  1277. check_aamix_out_path(codec, spec->speaker_paths[0]);
  1278. }
  1279. if (cfg->hp_outs && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
  1280. if (count_multiio_pins(codec, cfg->hp_pins[0]) >= 2)
  1281. spec->multi_ios = 1; /* give badness */
  1282. /* re-count num_dacs and squash invalid entries */
  1283. spec->multiout.num_dacs = 0;
  1284. for (i = 0; i < cfg->line_outs; i++) {
  1285. if (spec->private_dac_nids[i])
  1286. spec->multiout.num_dacs++;
  1287. else {
  1288. memmove(spec->private_dac_nids + i,
  1289. spec->private_dac_nids + i + 1,
  1290. sizeof(hda_nid_t) * (cfg->line_outs - i - 1));
  1291. spec->private_dac_nids[cfg->line_outs - 1] = 0;
  1292. }
  1293. }
  1294. spec->ext_channel_count = spec->min_channel_count =
  1295. spec->multiout.num_dacs * 2;
  1296. if (spec->multi_ios == 2) {
  1297. for (i = 0; i < 2; i++)
  1298. spec->private_dac_nids[spec->multiout.num_dacs++] =
  1299. spec->multi_io[i].dac;
  1300. } else if (spec->multi_ios) {
  1301. spec->multi_ios = 0;
  1302. badness += BAD_MULTI_IO;
  1303. }
  1304. /* re-fill the shared DAC for speaker / headphone */
  1305. if (cfg->line_out_type != AUTO_PIN_HP_OUT)
  1306. refill_shared_dacs(codec, cfg->hp_outs,
  1307. spec->multiout.hp_out_nid,
  1308. spec->hp_paths);
  1309. if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
  1310. refill_shared_dacs(codec, cfg->speaker_outs,
  1311. spec->multiout.extra_out_nid,
  1312. spec->speaker_paths);
  1313. /* set initial pinctl targets */
  1314. if (spec->prefer_hp_amp || cfg->line_out_type == AUTO_PIN_HP_OUT)
  1315. val = PIN_HP;
  1316. else
  1317. val = PIN_OUT;
  1318. set_pin_targets(codec, cfg->line_outs, cfg->line_out_pins, val);
  1319. if (cfg->line_out_type != AUTO_PIN_HP_OUT)
  1320. set_pin_targets(codec, cfg->hp_outs, cfg->hp_pins, PIN_HP);
  1321. if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
  1322. val = spec->prefer_hp_amp ? PIN_HP : PIN_OUT;
  1323. set_pin_targets(codec, cfg->speaker_outs,
  1324. cfg->speaker_pins, val);
  1325. }
  1326. return badness;
  1327. }
  1328. #define DEBUG_BADNESS
  1329. #ifdef DEBUG_BADNESS
  1330. #define debug_badness snd_printdd
  1331. #else
  1332. #define debug_badness(...)
  1333. #endif
  1334. static void debug_show_configs(struct hda_gen_spec *spec, struct auto_pin_cfg *cfg)
  1335. {
  1336. debug_badness("multi_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
  1337. cfg->line_out_pins[0], cfg->line_out_pins[1],
  1338. cfg->line_out_pins[2], cfg->line_out_pins[3],
  1339. spec->multiout.dac_nids[0],
  1340. spec->multiout.dac_nids[1],
  1341. spec->multiout.dac_nids[2],
  1342. spec->multiout.dac_nids[3]);
  1343. if (spec->multi_ios > 0)
  1344. debug_badness("multi_ios(%d) = %x/%x : %x/%x\n",
  1345. spec->multi_ios,
  1346. spec->multi_io[0].pin, spec->multi_io[1].pin,
  1347. spec->multi_io[0].dac, spec->multi_io[1].dac);
  1348. debug_badness("hp_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
  1349. cfg->hp_pins[0], cfg->hp_pins[1],
  1350. cfg->hp_pins[2], cfg->hp_pins[3],
  1351. spec->multiout.hp_out_nid[0],
  1352. spec->multiout.hp_out_nid[1],
  1353. spec->multiout.hp_out_nid[2],
  1354. spec->multiout.hp_out_nid[3]);
  1355. debug_badness("spk_outs = %x/%x/%x/%x : %x/%x/%x/%x\n",
  1356. cfg->speaker_pins[0], cfg->speaker_pins[1],
  1357. cfg->speaker_pins[2], cfg->speaker_pins[3],
  1358. spec->multiout.extra_out_nid[0],
  1359. spec->multiout.extra_out_nid[1],
  1360. spec->multiout.extra_out_nid[2],
  1361. spec->multiout.extra_out_nid[3]);
  1362. }
  1363. /* find all available DACs of the codec */
  1364. static void fill_all_dac_nids(struct hda_codec *codec)
  1365. {
  1366. struct hda_gen_spec *spec = codec->spec;
  1367. int i;
  1368. hda_nid_t nid = codec->start_nid;
  1369. spec->num_all_dacs = 0;
  1370. memset(spec->all_dacs, 0, sizeof(spec->all_dacs));
  1371. for (i = 0; i < codec->num_nodes; i++, nid++) {
  1372. if (get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_OUT)
  1373. continue;
  1374. if (spec->num_all_dacs >= ARRAY_SIZE(spec->all_dacs)) {
  1375. snd_printk(KERN_ERR "hda: Too many DACs!\n");
  1376. break;
  1377. }
  1378. spec->all_dacs[spec->num_all_dacs++] = nid;
  1379. }
  1380. }
  1381. static int parse_output_paths(struct hda_codec *codec)
  1382. {
  1383. struct hda_gen_spec *spec = codec->spec;
  1384. struct auto_pin_cfg *cfg = &spec->autocfg;
  1385. struct auto_pin_cfg *best_cfg;
  1386. int best_badness = INT_MAX;
  1387. int badness;
  1388. bool fill_hardwired = true, fill_mio_first = true;
  1389. bool best_wired = true, best_mio = true;
  1390. bool hp_spk_swapped = false;
  1391. best_cfg = kmalloc(sizeof(*best_cfg), GFP_KERNEL);
  1392. if (!best_cfg)
  1393. return -ENOMEM;
  1394. *best_cfg = *cfg;
  1395. for (;;) {
  1396. badness = fill_and_eval_dacs(codec, fill_hardwired,
  1397. fill_mio_first);
  1398. if (badness < 0) {
  1399. kfree(best_cfg);
  1400. return badness;
  1401. }
  1402. debug_badness("==> lo_type=%d, wired=%d, mio=%d, badness=0x%x\n",
  1403. cfg->line_out_type, fill_hardwired, fill_mio_first,
  1404. badness);
  1405. debug_show_configs(spec, cfg);
  1406. if (badness < best_badness) {
  1407. best_badness = badness;
  1408. *best_cfg = *cfg;
  1409. best_wired = fill_hardwired;
  1410. best_mio = fill_mio_first;
  1411. }
  1412. if (!badness)
  1413. break;
  1414. fill_mio_first = !fill_mio_first;
  1415. if (!fill_mio_first)
  1416. continue;
  1417. fill_hardwired = !fill_hardwired;
  1418. if (!fill_hardwired)
  1419. continue;
  1420. if (hp_spk_swapped)
  1421. break;
  1422. hp_spk_swapped = true;
  1423. if (cfg->speaker_outs > 0 &&
  1424. cfg->line_out_type == AUTO_PIN_HP_OUT) {
  1425. cfg->hp_outs = cfg->line_outs;
  1426. memcpy(cfg->hp_pins, cfg->line_out_pins,
  1427. sizeof(cfg->hp_pins));
  1428. cfg->line_outs = cfg->speaker_outs;
  1429. memcpy(cfg->line_out_pins, cfg->speaker_pins,
  1430. sizeof(cfg->speaker_pins));
  1431. cfg->speaker_outs = 0;
  1432. memset(cfg->speaker_pins, 0, sizeof(cfg->speaker_pins));
  1433. cfg->line_out_type = AUTO_PIN_SPEAKER_OUT;
  1434. fill_hardwired = true;
  1435. continue;
  1436. }
  1437. if (cfg->hp_outs > 0 &&
  1438. cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
  1439. cfg->speaker_outs = cfg->line_outs;
  1440. memcpy(cfg->speaker_pins, cfg->line_out_pins,
  1441. sizeof(cfg->speaker_pins));
  1442. cfg->line_outs = cfg->hp_outs;
  1443. memcpy(cfg->line_out_pins, cfg->hp_pins,
  1444. sizeof(cfg->hp_pins));
  1445. cfg->hp_outs = 0;
  1446. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  1447. cfg->line_out_type = AUTO_PIN_HP_OUT;
  1448. fill_hardwired = true;
  1449. continue;
  1450. }
  1451. break;
  1452. }
  1453. if (badness) {
  1454. debug_badness("==> restoring best_cfg\n");
  1455. *cfg = *best_cfg;
  1456. fill_and_eval_dacs(codec, best_wired, best_mio);
  1457. }
  1458. debug_badness("==> Best config: lo_type=%d, wired=%d, mio=%d\n",
  1459. cfg->line_out_type, best_wired, best_mio);
  1460. debug_show_configs(spec, cfg);
  1461. if (cfg->line_out_pins[0]) {
  1462. struct nid_path *path;
  1463. path = snd_hda_get_path_from_idx(codec, spec->out_paths[0]);
  1464. if (path)
  1465. spec->vmaster_nid = look_for_out_vol_nid(codec, path);
  1466. if (spec->vmaster_nid)
  1467. snd_hda_set_vmaster_tlv(codec, spec->vmaster_nid,
  1468. HDA_OUTPUT, spec->vmaster_tlv);
  1469. }
  1470. kfree(best_cfg);
  1471. return 0;
  1472. }
  1473. /* add playback controls from the parsed DAC table */
  1474. static int create_multi_out_ctls(struct hda_codec *codec,
  1475. const struct auto_pin_cfg *cfg)
  1476. {
  1477. struct hda_gen_spec *spec = codec->spec;
  1478. int i, err, noutputs;
  1479. noutputs = cfg->line_outs;
  1480. if (spec->multi_ios > 0 && cfg->line_outs < 3)
  1481. noutputs += spec->multi_ios;
  1482. for (i = 0; i < noutputs; i++) {
  1483. const char *name;
  1484. int index;
  1485. struct nid_path *path;
  1486. path = snd_hda_get_path_from_idx(codec, spec->out_paths[i]);
  1487. if (!path)
  1488. continue;
  1489. name = get_line_out_pfx(codec, i, &index, NID_PATH_VOL_CTL);
  1490. if (!name || !strcmp(name, "CLFE")) {
  1491. /* Center/LFE */
  1492. err = add_vol_ctl(codec, "Center", 0, 1, path);
  1493. if (err < 0)
  1494. return err;
  1495. err = add_vol_ctl(codec, "LFE", 0, 2, path);
  1496. if (err < 0)
  1497. return err;
  1498. } else {
  1499. err = add_stereo_vol(codec, name, index, path);
  1500. if (err < 0)
  1501. return err;
  1502. }
  1503. name = get_line_out_pfx(codec, i, &index, NID_PATH_MUTE_CTL);
  1504. if (!name || !strcmp(name, "CLFE")) {
  1505. err = add_sw_ctl(codec, "Center", 0, 1, path);
  1506. if (err < 0)
  1507. return err;
  1508. err = add_sw_ctl(codec, "LFE", 0, 2, path);
  1509. if (err < 0)
  1510. return err;
  1511. } else {
  1512. err = add_stereo_sw(codec, name, index, path);
  1513. if (err < 0)
  1514. return err;
  1515. }
  1516. }
  1517. return 0;
  1518. }
  1519. static int create_extra_out(struct hda_codec *codec, int path_idx,
  1520. const char *pfx, int cidx)
  1521. {
  1522. struct nid_path *path;
  1523. int err;
  1524. path = snd_hda_get_path_from_idx(codec, path_idx);
  1525. if (!path)
  1526. return 0;
  1527. err = add_stereo_vol(codec, pfx, cidx, path);
  1528. if (err < 0)
  1529. return err;
  1530. err = add_stereo_sw(codec, pfx, cidx, path);
  1531. if (err < 0)
  1532. return err;
  1533. return 0;
  1534. }
  1535. /* add playback controls for speaker and HP outputs */
  1536. static int create_extra_outs(struct hda_codec *codec, int num_pins,
  1537. const int *paths, const char *pfx)
  1538. {
  1539. int i;
  1540. for (i = 0; i < num_pins; i++) {
  1541. const char *name;
  1542. char tmp[44];
  1543. int err, idx = 0;
  1544. if (num_pins == 2 && i == 1 && !strcmp(pfx, "Speaker"))
  1545. name = "Bass Speaker";
  1546. else if (num_pins >= 3) {
  1547. snprintf(tmp, sizeof(tmp), "%s %s",
  1548. pfx, channel_name[i]);
  1549. name = tmp;
  1550. } else {
  1551. name = pfx;
  1552. idx = i;
  1553. }
  1554. err = create_extra_out(codec, paths[i], name, idx);
  1555. if (err < 0)
  1556. return err;
  1557. }
  1558. return 0;
  1559. }
  1560. static int create_hp_out_ctls(struct hda_codec *codec)
  1561. {
  1562. struct hda_gen_spec *spec = codec->spec;
  1563. return create_extra_outs(codec, spec->autocfg.hp_outs,
  1564. spec->hp_paths,
  1565. "Headphone");
  1566. }
  1567. static int create_speaker_out_ctls(struct hda_codec *codec)
  1568. {
  1569. struct hda_gen_spec *spec = codec->spec;
  1570. return create_extra_outs(codec, spec->autocfg.speaker_outs,
  1571. spec->speaker_paths,
  1572. "Speaker");
  1573. }
  1574. /*
  1575. * independent HP controls
  1576. */
  1577. static int indep_hp_info(struct snd_kcontrol *kcontrol,
  1578. struct snd_ctl_elem_info *uinfo)
  1579. {
  1580. return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
  1581. }
  1582. static int indep_hp_get(struct snd_kcontrol *kcontrol,
  1583. struct snd_ctl_elem_value *ucontrol)
  1584. {
  1585. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1586. struct hda_gen_spec *spec = codec->spec;
  1587. ucontrol->value.enumerated.item[0] = spec->indep_hp_enabled;
  1588. return 0;
  1589. }
  1590. static int indep_hp_put(struct snd_kcontrol *kcontrol,
  1591. struct snd_ctl_elem_value *ucontrol)
  1592. {
  1593. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1594. struct hda_gen_spec *spec = codec->spec;
  1595. unsigned int select = ucontrol->value.enumerated.item[0];
  1596. int ret = 0;
  1597. mutex_lock(&spec->pcm_mutex);
  1598. if (spec->active_streams) {
  1599. ret = -EBUSY;
  1600. goto unlock;
  1601. }
  1602. if (spec->indep_hp_enabled != select) {
  1603. spec->indep_hp_enabled = select;
  1604. if (spec->indep_hp_enabled)
  1605. spec->multiout.hp_out_nid[0] = 0;
  1606. else
  1607. spec->multiout.hp_out_nid[0] = spec->alt_dac_nid;
  1608. ret = 1;
  1609. }
  1610. unlock:
  1611. mutex_unlock(&spec->pcm_mutex);
  1612. return ret;
  1613. }
  1614. static const struct snd_kcontrol_new indep_hp_ctl = {
  1615. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1616. .name = "Independent HP",
  1617. .info = indep_hp_info,
  1618. .get = indep_hp_get,
  1619. .put = indep_hp_put,
  1620. };
  1621. static int create_indep_hp_ctls(struct hda_codec *codec)
  1622. {
  1623. struct hda_gen_spec *spec = codec->spec;
  1624. if (!spec->indep_hp)
  1625. return 0;
  1626. if (!spec->multiout.hp_out_nid[0]) {
  1627. spec->indep_hp = 0;
  1628. return 0;
  1629. }
  1630. spec->indep_hp_enabled = false;
  1631. spec->alt_dac_nid = spec->multiout.hp_out_nid[0];
  1632. if (!snd_hda_gen_add_kctl(spec, NULL, &indep_hp_ctl))
  1633. return -ENOMEM;
  1634. return 0;
  1635. }
  1636. /*
  1637. * channel mode enum control
  1638. */
  1639. static int ch_mode_info(struct snd_kcontrol *kcontrol,
  1640. struct snd_ctl_elem_info *uinfo)
  1641. {
  1642. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1643. struct hda_gen_spec *spec = codec->spec;
  1644. int chs;
  1645. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1646. uinfo->count = 1;
  1647. uinfo->value.enumerated.items = spec->multi_ios + 1;
  1648. if (uinfo->value.enumerated.item > spec->multi_ios)
  1649. uinfo->value.enumerated.item = spec->multi_ios;
  1650. chs = uinfo->value.enumerated.item * 2 + spec->min_channel_count;
  1651. sprintf(uinfo->value.enumerated.name, "%dch", chs);
  1652. return 0;
  1653. }
  1654. static int ch_mode_get(struct snd_kcontrol *kcontrol,
  1655. struct snd_ctl_elem_value *ucontrol)
  1656. {
  1657. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1658. struct hda_gen_spec *spec = codec->spec;
  1659. ucontrol->value.enumerated.item[0] =
  1660. (spec->ext_channel_count - spec->min_channel_count) / 2;
  1661. return 0;
  1662. }
  1663. static inline struct nid_path *
  1664. get_multiio_path(struct hda_codec *codec, int idx)
  1665. {
  1666. struct hda_gen_spec *spec = codec->spec;
  1667. return snd_hda_get_path_from_idx(codec,
  1668. spec->out_paths[spec->autocfg.line_outs + idx]);
  1669. }
  1670. static void update_automute_all(struct hda_codec *codec);
  1671. static int set_multi_io(struct hda_codec *codec, int idx, bool output)
  1672. {
  1673. struct hda_gen_spec *spec = codec->spec;
  1674. hda_nid_t nid = spec->multi_io[idx].pin;
  1675. struct nid_path *path;
  1676. path = get_multiio_path(codec, idx);
  1677. if (!path)
  1678. return -EINVAL;
  1679. if (path->active == output)
  1680. return 0;
  1681. if (output) {
  1682. set_pin_target(codec, nid, PIN_OUT, true);
  1683. snd_hda_activate_path(codec, path, true, true);
  1684. set_pin_eapd(codec, nid, true);
  1685. } else {
  1686. set_pin_eapd(codec, nid, false);
  1687. snd_hda_activate_path(codec, path, false, true);
  1688. set_pin_target(codec, nid, spec->multi_io[idx].ctl_in, true);
  1689. }
  1690. /* update jack retasking in case it modifies any of them */
  1691. update_automute_all(codec);
  1692. return 0;
  1693. }
  1694. static int ch_mode_put(struct snd_kcontrol *kcontrol,
  1695. struct snd_ctl_elem_value *ucontrol)
  1696. {
  1697. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1698. struct hda_gen_spec *spec = codec->spec;
  1699. int i, ch;
  1700. ch = ucontrol->value.enumerated.item[0];
  1701. if (ch < 0 || ch > spec->multi_ios)
  1702. return -EINVAL;
  1703. if (ch == (spec->ext_channel_count - spec->min_channel_count) / 2)
  1704. return 0;
  1705. spec->ext_channel_count = ch * 2 + spec->min_channel_count;
  1706. for (i = 0; i < spec->multi_ios; i++)
  1707. set_multi_io(codec, i, i < ch);
  1708. spec->multiout.max_channels = max(spec->ext_channel_count,
  1709. spec->const_channel_count);
  1710. if (spec->need_dac_fix)
  1711. spec->multiout.num_dacs = spec->multiout.max_channels / 2;
  1712. return 1;
  1713. }
  1714. static const struct snd_kcontrol_new channel_mode_enum = {
  1715. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1716. .name = "Channel Mode",
  1717. .info = ch_mode_info,
  1718. .get = ch_mode_get,
  1719. .put = ch_mode_put,
  1720. };
  1721. static int create_multi_channel_mode(struct hda_codec *codec)
  1722. {
  1723. struct hda_gen_spec *spec = codec->spec;
  1724. if (spec->multi_ios > 0) {
  1725. if (!snd_hda_gen_add_kctl(spec, NULL, &channel_mode_enum))
  1726. return -ENOMEM;
  1727. }
  1728. return 0;
  1729. }
  1730. /*
  1731. * aamix loopback enable/disable switch
  1732. */
  1733. #define loopback_mixing_info indep_hp_info
  1734. static int loopback_mixing_get(struct snd_kcontrol *kcontrol,
  1735. struct snd_ctl_elem_value *ucontrol)
  1736. {
  1737. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1738. struct hda_gen_spec *spec = codec->spec;
  1739. ucontrol->value.enumerated.item[0] = spec->aamix_mode;
  1740. return 0;
  1741. }
  1742. static void update_aamix_paths(struct hda_codec *codec, bool do_mix,
  1743. int nomix_path_idx, int mix_path_idx)
  1744. {
  1745. struct nid_path *nomix_path, *mix_path;
  1746. nomix_path = snd_hda_get_path_from_idx(codec, nomix_path_idx);
  1747. mix_path = snd_hda_get_path_from_idx(codec, mix_path_idx);
  1748. if (!nomix_path || !mix_path)
  1749. return;
  1750. if (do_mix) {
  1751. snd_hda_activate_path(codec, nomix_path, false, true);
  1752. snd_hda_activate_path(codec, mix_path, true, true);
  1753. } else {
  1754. snd_hda_activate_path(codec, mix_path, false, true);
  1755. snd_hda_activate_path(codec, nomix_path, true, true);
  1756. }
  1757. }
  1758. static int loopback_mixing_put(struct snd_kcontrol *kcontrol,
  1759. struct snd_ctl_elem_value *ucontrol)
  1760. {
  1761. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1762. struct hda_gen_spec *spec = codec->spec;
  1763. unsigned int val = ucontrol->value.enumerated.item[0];
  1764. if (val == spec->aamix_mode)
  1765. return 0;
  1766. spec->aamix_mode = val;
  1767. update_aamix_paths(codec, val, spec->out_paths[0],
  1768. spec->aamix_out_paths[0]);
  1769. update_aamix_paths(codec, val, spec->hp_paths[0],
  1770. spec->aamix_out_paths[1]);
  1771. update_aamix_paths(codec, val, spec->speaker_paths[0],
  1772. spec->aamix_out_paths[2]);
  1773. return 1;
  1774. }
  1775. static const struct snd_kcontrol_new loopback_mixing_enum = {
  1776. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1777. .name = "Loopback Mixing",
  1778. .info = loopback_mixing_info,
  1779. .get = loopback_mixing_get,
  1780. .put = loopback_mixing_put,
  1781. };
  1782. static int create_loopback_mixing_ctl(struct hda_codec *codec)
  1783. {
  1784. struct hda_gen_spec *spec = codec->spec;
  1785. if (!spec->mixer_nid)
  1786. return 0;
  1787. if (!(spec->aamix_out_paths[0] || spec->aamix_out_paths[1] ||
  1788. spec->aamix_out_paths[2]))
  1789. return 0;
  1790. if (!snd_hda_gen_add_kctl(spec, NULL, &loopback_mixing_enum))
  1791. return -ENOMEM;
  1792. return 0;
  1793. }
  1794. /*
  1795. * shared headphone/mic handling
  1796. */
  1797. static void call_update_outputs(struct hda_codec *codec);
  1798. /* for shared I/O, change the pin-control accordingly */
  1799. static void update_shared_mic_hp(struct hda_codec *codec, bool set_as_mic)
  1800. {
  1801. struct hda_gen_spec *spec = codec->spec;
  1802. unsigned int val;
  1803. hda_nid_t pin = spec->autocfg.inputs[1].pin;
  1804. /* NOTE: this assumes that there are only two inputs, the
  1805. * first is the real internal mic and the second is HP/mic jack.
  1806. */
  1807. val = snd_hda_get_default_vref(codec, pin);
  1808. /* This pin does not have vref caps - let's enable vref on pin 0x18
  1809. instead, as suggested by Realtek */
  1810. if (val == AC_PINCTL_VREF_HIZ && spec->shared_mic_vref_pin) {
  1811. const hda_nid_t vref_pin = spec->shared_mic_vref_pin;
  1812. unsigned int vref_val = snd_hda_get_default_vref(codec, vref_pin);
  1813. if (vref_val != AC_PINCTL_VREF_HIZ)
  1814. snd_hda_set_pin_ctl_cache(codec, vref_pin,
  1815. PIN_IN | (set_as_mic ? vref_val : 0));
  1816. }
  1817. val = set_as_mic ? val | PIN_IN : PIN_HP;
  1818. set_pin_target(codec, pin, val, true);
  1819. spec->automute_speaker = !set_as_mic;
  1820. call_update_outputs(codec);
  1821. }
  1822. /* create a shared input with the headphone out */
  1823. static int create_shared_input(struct hda_codec *codec)
  1824. {
  1825. struct hda_gen_spec *spec = codec->spec;
  1826. struct auto_pin_cfg *cfg = &spec->autocfg;
  1827. unsigned int defcfg;
  1828. hda_nid_t nid;
  1829. /* only one internal input pin? */
  1830. if (cfg->num_inputs != 1)
  1831. return 0;
  1832. defcfg = snd_hda_codec_get_pincfg(codec, cfg->inputs[0].pin);
  1833. if (snd_hda_get_input_pin_attr(defcfg) != INPUT_PIN_ATTR_INT)
  1834. return 0;
  1835. if (cfg->hp_outs == 1 && cfg->line_out_type == AUTO_PIN_SPEAKER_OUT)
  1836. nid = cfg->hp_pins[0]; /* OK, we have a single HP-out */
  1837. else if (cfg->line_outs == 1 && cfg->line_out_type == AUTO_PIN_HP_OUT)
  1838. nid = cfg->line_out_pins[0]; /* OK, we have a single line-out */
  1839. else
  1840. return 0; /* both not available */
  1841. if (!(snd_hda_query_pin_caps(codec, nid) & AC_PINCAP_IN))
  1842. return 0; /* no input */
  1843. cfg->inputs[1].pin = nid;
  1844. cfg->inputs[1].type = AUTO_PIN_MIC;
  1845. cfg->num_inputs = 2;
  1846. spec->shared_mic_hp = 1;
  1847. snd_printdd("hda-codec: Enable shared I/O jack on NID 0x%x\n", nid);
  1848. return 0;
  1849. }
  1850. /*
  1851. * output jack mode
  1852. */
  1853. static int out_jack_mode_info(struct snd_kcontrol *kcontrol,
  1854. struct snd_ctl_elem_info *uinfo)
  1855. {
  1856. static const char * const texts[] = {
  1857. "Line Out", "Headphone Out",
  1858. };
  1859. return snd_hda_enum_helper_info(kcontrol, uinfo, 2, texts);
  1860. }
  1861. static int out_jack_mode_get(struct snd_kcontrol *kcontrol,
  1862. struct snd_ctl_elem_value *ucontrol)
  1863. {
  1864. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1865. hda_nid_t nid = kcontrol->private_value;
  1866. if (snd_hda_codec_get_pin_target(codec, nid) == PIN_HP)
  1867. ucontrol->value.enumerated.item[0] = 1;
  1868. else
  1869. ucontrol->value.enumerated.item[0] = 0;
  1870. return 0;
  1871. }
  1872. static int out_jack_mode_put(struct snd_kcontrol *kcontrol,
  1873. struct snd_ctl_elem_value *ucontrol)
  1874. {
  1875. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1876. hda_nid_t nid = kcontrol->private_value;
  1877. unsigned int val;
  1878. val = ucontrol->value.enumerated.item[0] ? PIN_HP : PIN_OUT;
  1879. if (snd_hda_codec_get_pin_target(codec, nid) == val)
  1880. return 0;
  1881. snd_hda_set_pin_ctl_cache(codec, nid, val);
  1882. return 1;
  1883. }
  1884. static const struct snd_kcontrol_new out_jack_mode_enum = {
  1885. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1886. .info = out_jack_mode_info,
  1887. .get = out_jack_mode_get,
  1888. .put = out_jack_mode_put,
  1889. };
  1890. static bool find_kctl_name(struct hda_codec *codec, const char *name, int idx)
  1891. {
  1892. struct hda_gen_spec *spec = codec->spec;
  1893. int i;
  1894. for (i = 0; i < spec->kctls.used; i++) {
  1895. struct snd_kcontrol_new *kctl = snd_array_elem(&spec->kctls, i);
  1896. if (!strcmp(kctl->name, name) && kctl->index == idx)
  1897. return true;
  1898. }
  1899. return false;
  1900. }
  1901. static void get_jack_mode_name(struct hda_codec *codec, hda_nid_t pin,
  1902. char *name, size_t name_len)
  1903. {
  1904. struct hda_gen_spec *spec = codec->spec;
  1905. int idx = 0;
  1906. snd_hda_get_pin_label(codec, pin, &spec->autocfg, name, name_len, &idx);
  1907. strlcat(name, " Jack Mode", name_len);
  1908. for (; find_kctl_name(codec, name, idx); idx++)
  1909. ;
  1910. }
  1911. static int create_out_jack_modes(struct hda_codec *codec, int num_pins,
  1912. hda_nid_t *pins)
  1913. {
  1914. struct hda_gen_spec *spec = codec->spec;
  1915. int i;
  1916. for (i = 0; i < num_pins; i++) {
  1917. hda_nid_t pin = pins[i];
  1918. unsigned int pincap = snd_hda_query_pin_caps(codec, pin);
  1919. if ((pincap & AC_PINCAP_OUT) && (pincap & AC_PINCAP_HP_DRV)) {
  1920. struct snd_kcontrol_new *knew;
  1921. char name[44];
  1922. get_jack_mode_name(codec, pin, name, sizeof(name));
  1923. knew = snd_hda_gen_add_kctl(spec, name,
  1924. &out_jack_mode_enum);
  1925. if (!knew)
  1926. return -ENOMEM;
  1927. knew->private_value = pin;
  1928. }
  1929. }
  1930. return 0;
  1931. }
  1932. /*
  1933. * input jack mode
  1934. */
  1935. /* from AC_PINCTL_VREF_HIZ to AC_PINCTL_VREF_100 */
  1936. #define NUM_VREFS 6
  1937. static const char * const vref_texts[NUM_VREFS] = {
  1938. "Line In", "Mic 50pc Bias", "Mic 0V Bias",
  1939. "", "Mic 80pc Bias", "Mic 100pc Bias"
  1940. };
  1941. static unsigned int get_vref_caps(struct hda_codec *codec, hda_nid_t pin)
  1942. {
  1943. unsigned int pincap;
  1944. pincap = snd_hda_query_pin_caps(codec, pin);
  1945. pincap = (pincap & AC_PINCAP_VREF) >> AC_PINCAP_VREF_SHIFT;
  1946. /* filter out unusual vrefs */
  1947. pincap &= ~(AC_PINCAP_VREF_GRD | AC_PINCAP_VREF_100);
  1948. return pincap;
  1949. }
  1950. /* convert from the enum item index to the vref ctl index (0=HIZ, 1=50%...) */
  1951. static int get_vref_idx(unsigned int vref_caps, unsigned int item_idx)
  1952. {
  1953. unsigned int i, n = 0;
  1954. for (i = 0; i < NUM_VREFS; i++) {
  1955. if (vref_caps & (1 << i)) {
  1956. if (n == item_idx)
  1957. return i;
  1958. n++;
  1959. }
  1960. }
  1961. return 0;
  1962. }
  1963. /* convert back from the vref ctl index to the enum item index */
  1964. static int cvt_from_vref_idx(unsigned int vref_caps, unsigned int idx)
  1965. {
  1966. unsigned int i, n = 0;
  1967. for (i = 0; i < NUM_VREFS; i++) {
  1968. if (i == idx)
  1969. return n;
  1970. if (vref_caps & (1 << i))
  1971. n++;
  1972. }
  1973. return 0;
  1974. }
  1975. static int in_jack_mode_info(struct snd_kcontrol *kcontrol,
  1976. struct snd_ctl_elem_info *uinfo)
  1977. {
  1978. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1979. hda_nid_t nid = kcontrol->private_value;
  1980. unsigned int vref_caps = get_vref_caps(codec, nid);
  1981. snd_hda_enum_helper_info(kcontrol, uinfo, hweight32(vref_caps),
  1982. vref_texts);
  1983. /* set the right text */
  1984. strcpy(uinfo->value.enumerated.name,
  1985. vref_texts[get_vref_idx(vref_caps, uinfo->value.enumerated.item)]);
  1986. return 0;
  1987. }
  1988. static int in_jack_mode_get(struct snd_kcontrol *kcontrol,
  1989. struct snd_ctl_elem_value *ucontrol)
  1990. {
  1991. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1992. hda_nid_t nid = kcontrol->private_value;
  1993. unsigned int vref_caps = get_vref_caps(codec, nid);
  1994. unsigned int idx;
  1995. idx = snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_VREFEN;
  1996. ucontrol->value.enumerated.item[0] = cvt_from_vref_idx(vref_caps, idx);
  1997. return 0;
  1998. }
  1999. static int in_jack_mode_put(struct snd_kcontrol *kcontrol,
  2000. struct snd_ctl_elem_value *ucontrol)
  2001. {
  2002. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2003. hda_nid_t nid = kcontrol->private_value;
  2004. unsigned int vref_caps = get_vref_caps(codec, nid);
  2005. unsigned int val, idx;
  2006. val = snd_hda_codec_get_pin_target(codec, nid);
  2007. idx = cvt_from_vref_idx(vref_caps, val & AC_PINCTL_VREFEN);
  2008. if (idx == ucontrol->value.enumerated.item[0])
  2009. return 0;
  2010. val &= ~AC_PINCTL_VREFEN;
  2011. val |= get_vref_idx(vref_caps, ucontrol->value.enumerated.item[0]);
  2012. snd_hda_set_pin_ctl_cache(codec, nid, val);
  2013. return 1;
  2014. }
  2015. static const struct snd_kcontrol_new in_jack_mode_enum = {
  2016. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2017. .info = in_jack_mode_info,
  2018. .get = in_jack_mode_get,
  2019. .put = in_jack_mode_put,
  2020. };
  2021. static int create_in_jack_mode(struct hda_codec *codec, hda_nid_t pin)
  2022. {
  2023. struct hda_gen_spec *spec = codec->spec;
  2024. unsigned int defcfg;
  2025. struct snd_kcontrol_new *knew;
  2026. char name[44];
  2027. /* no jack mode for fixed pins */
  2028. defcfg = snd_hda_codec_get_pincfg(codec, pin);
  2029. if (snd_hda_get_input_pin_attr(defcfg) == INPUT_PIN_ATTR_INT)
  2030. return 0;
  2031. /* no multiple vref caps? */
  2032. if (hweight32(get_vref_caps(codec, pin)) <= 1)
  2033. return 0;
  2034. get_jack_mode_name(codec, pin, name, sizeof(name));
  2035. knew = snd_hda_gen_add_kctl(spec, name, &in_jack_mode_enum);
  2036. if (!knew)
  2037. return -ENOMEM;
  2038. knew->private_value = pin;
  2039. return 0;
  2040. }
  2041. /*
  2042. * Parse input paths
  2043. */
  2044. #ifdef CONFIG_PM
  2045. /* add the powersave loopback-list entry */
  2046. static void add_loopback_list(struct hda_gen_spec *spec, hda_nid_t mix, int idx)
  2047. {
  2048. struct hda_amp_list *list;
  2049. if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
  2050. return;
  2051. list = spec->loopback_list + spec->num_loopbacks;
  2052. list->nid = mix;
  2053. list->dir = HDA_INPUT;
  2054. list->idx = idx;
  2055. spec->num_loopbacks++;
  2056. spec->loopback.amplist = spec->loopback_list;
  2057. }
  2058. #else
  2059. #define add_loopback_list(spec, mix, idx) /* NOP */
  2060. #endif
  2061. /* create input playback/capture controls for the given pin */
  2062. static int new_analog_input(struct hda_codec *codec, int input_idx,
  2063. hda_nid_t pin, const char *ctlname, int ctlidx,
  2064. hda_nid_t mix_nid)
  2065. {
  2066. struct hda_gen_spec *spec = codec->spec;
  2067. struct nid_path *path;
  2068. unsigned int val;
  2069. int err, idx;
  2070. if (!nid_has_volume(codec, mix_nid, HDA_INPUT) &&
  2071. !nid_has_mute(codec, mix_nid, HDA_INPUT))
  2072. return 0; /* no need for analog loopback */
  2073. path = snd_hda_add_new_path(codec, pin, mix_nid, 0);
  2074. if (!path)
  2075. return -EINVAL;
  2076. print_nid_path("loopback", path);
  2077. spec->loopback_paths[input_idx] = snd_hda_get_path_idx(codec, path);
  2078. idx = path->idx[path->depth - 1];
  2079. if (nid_has_volume(codec, mix_nid, HDA_INPUT)) {
  2080. val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
  2081. err = __add_pb_vol_ctrl(spec, HDA_CTL_WIDGET_VOL, ctlname, ctlidx, val);
  2082. if (err < 0)
  2083. return err;
  2084. path->ctls[NID_PATH_VOL_CTL] = val;
  2085. }
  2086. if (nid_has_mute(codec, mix_nid, HDA_INPUT)) {
  2087. val = HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT);
  2088. err = __add_pb_sw_ctrl(spec, HDA_CTL_WIDGET_MUTE, ctlname, ctlidx, val);
  2089. if (err < 0)
  2090. return err;
  2091. path->ctls[NID_PATH_MUTE_CTL] = val;
  2092. }
  2093. path->active = true;
  2094. add_loopback_list(spec, mix_nid, idx);
  2095. return 0;
  2096. }
  2097. static int is_input_pin(struct hda_codec *codec, hda_nid_t nid)
  2098. {
  2099. unsigned int pincap = snd_hda_query_pin_caps(codec, nid);
  2100. return (pincap & AC_PINCAP_IN) != 0;
  2101. }
  2102. /* Parse the codec tree and retrieve ADCs */
  2103. static int fill_adc_nids(struct hda_codec *codec)
  2104. {
  2105. struct hda_gen_spec *spec = codec->spec;
  2106. hda_nid_t nid;
  2107. hda_nid_t *adc_nids = spec->adc_nids;
  2108. int max_nums = ARRAY_SIZE(spec->adc_nids);
  2109. int i, nums = 0;
  2110. nid = codec->start_nid;
  2111. for (i = 0; i < codec->num_nodes; i++, nid++) {
  2112. unsigned int caps = get_wcaps(codec, nid);
  2113. int type = get_wcaps_type(caps);
  2114. if (type != AC_WID_AUD_IN || (caps & AC_WCAP_DIGITAL))
  2115. continue;
  2116. adc_nids[nums] = nid;
  2117. if (++nums >= max_nums)
  2118. break;
  2119. }
  2120. spec->num_adc_nids = nums;
  2121. /* copy the detected ADCs to all_adcs[] */
  2122. spec->num_all_adcs = nums;
  2123. memcpy(spec->all_adcs, spec->adc_nids, nums * sizeof(hda_nid_t));
  2124. return nums;
  2125. }
  2126. /* filter out invalid adc_nids that don't give all active input pins;
  2127. * if needed, check whether dynamic ADC-switching is available
  2128. */
  2129. static int check_dyn_adc_switch(struct hda_codec *codec)
  2130. {
  2131. struct hda_gen_spec *spec = codec->spec;
  2132. struct hda_input_mux *imux = &spec->input_mux;
  2133. unsigned int ok_bits;
  2134. int i, n, nums;
  2135. again:
  2136. nums = 0;
  2137. ok_bits = 0;
  2138. for (n = 0; n < spec->num_adc_nids; n++) {
  2139. for (i = 0; i < imux->num_items; i++) {
  2140. if (!spec->input_paths[i][n])
  2141. break;
  2142. }
  2143. if (i >= imux->num_items) {
  2144. ok_bits |= (1 << n);
  2145. nums++;
  2146. }
  2147. }
  2148. if (!ok_bits) {
  2149. if (spec->shared_mic_hp) {
  2150. spec->shared_mic_hp = 0;
  2151. imux->num_items = 1;
  2152. goto again;
  2153. }
  2154. /* check whether ADC-switch is possible */
  2155. for (i = 0; i < imux->num_items; i++) {
  2156. for (n = 0; n < spec->num_adc_nids; n++) {
  2157. if (spec->input_paths[i][n]) {
  2158. spec->dyn_adc_idx[i] = n;
  2159. break;
  2160. }
  2161. }
  2162. }
  2163. snd_printdd("hda-codec: enabling ADC switching\n");
  2164. spec->dyn_adc_switch = 1;
  2165. } else if (nums != spec->num_adc_nids) {
  2166. /* shrink the invalid adcs and input paths */
  2167. nums = 0;
  2168. for (n = 0; n < spec->num_adc_nids; n++) {
  2169. if (!(ok_bits & (1 << n)))
  2170. continue;
  2171. if (n != nums) {
  2172. spec->adc_nids[nums] = spec->adc_nids[n];
  2173. for (i = 0; i < imux->num_items; i++) {
  2174. invalidate_nid_path(codec,
  2175. spec->input_paths[i][nums]);
  2176. spec->input_paths[i][nums] =
  2177. spec->input_paths[i][n];
  2178. }
  2179. }
  2180. nums++;
  2181. }
  2182. spec->num_adc_nids = nums;
  2183. }
  2184. if (imux->num_items == 1 || spec->shared_mic_hp) {
  2185. snd_printdd("hda-codec: reducing to a single ADC\n");
  2186. spec->num_adc_nids = 1; /* reduce to a single ADC */
  2187. }
  2188. /* single index for individual volumes ctls */
  2189. if (!spec->dyn_adc_switch && spec->multi_cap_vol)
  2190. spec->num_adc_nids = 1;
  2191. return 0;
  2192. }
  2193. /* parse capture source paths from the given pin and create imux items */
  2194. static int parse_capture_source(struct hda_codec *codec, hda_nid_t pin,
  2195. int cfg_idx, int num_adcs,
  2196. const char *label, int anchor)
  2197. {
  2198. struct hda_gen_spec *spec = codec->spec;
  2199. struct hda_input_mux *imux = &spec->input_mux;
  2200. int imux_idx = imux->num_items;
  2201. bool imux_added = false;
  2202. int c;
  2203. for (c = 0; c < num_adcs; c++) {
  2204. struct nid_path *path;
  2205. hda_nid_t adc = spec->adc_nids[c];
  2206. if (!is_reachable_path(codec, pin, adc))
  2207. continue;
  2208. path = snd_hda_add_new_path(codec, pin, adc, anchor);
  2209. if (!path)
  2210. continue;
  2211. print_nid_path("input", path);
  2212. spec->input_paths[imux_idx][c] =
  2213. snd_hda_get_path_idx(codec, path);
  2214. if (!imux_added) {
  2215. spec->imux_pins[imux->num_items] = pin;
  2216. snd_hda_add_imux_item(imux, label, cfg_idx, NULL);
  2217. imux_added = true;
  2218. }
  2219. }
  2220. return 0;
  2221. }
  2222. /*
  2223. * create playback/capture controls for input pins
  2224. */
  2225. #define CFG_IDX_MIX 99 /* a dummy cfg->input idx for stereo mix */
  2226. static int create_input_ctls(struct hda_codec *codec)
  2227. {
  2228. struct hda_gen_spec *spec = codec->spec;
  2229. const struct auto_pin_cfg *cfg = &spec->autocfg;
  2230. hda_nid_t mixer = spec->mixer_nid;
  2231. int num_adcs;
  2232. int i, err, type_idx = 0;
  2233. const char *prev_label = NULL;
  2234. unsigned int val;
  2235. num_adcs = fill_adc_nids(codec);
  2236. if (num_adcs < 0)
  2237. return 0;
  2238. for (i = 0; i < cfg->num_inputs; i++) {
  2239. hda_nid_t pin;
  2240. const char *label;
  2241. pin = cfg->inputs[i].pin;
  2242. if (!is_input_pin(codec, pin))
  2243. continue;
  2244. label = hda_get_autocfg_input_label(codec, cfg, i);
  2245. if (prev_label && !strcmp(label, prev_label))
  2246. type_idx++;
  2247. else
  2248. type_idx = 0;
  2249. prev_label = label;
  2250. val = PIN_IN;
  2251. if (cfg->inputs[i].type == AUTO_PIN_MIC)
  2252. val |= snd_hda_get_default_vref(codec, pin);
  2253. set_pin_target(codec, pin, val, false);
  2254. if (mixer) {
  2255. if (is_reachable_path(codec, pin, mixer)) {
  2256. err = new_analog_input(codec, i, pin,
  2257. label, type_idx, mixer);
  2258. if (err < 0)
  2259. return err;
  2260. }
  2261. }
  2262. err = parse_capture_source(codec, pin, i,
  2263. num_adcs, label, -mixer);
  2264. if (err < 0)
  2265. return err;
  2266. if (spec->add_in_jack_modes) {
  2267. err = create_in_jack_mode(codec, pin);
  2268. if (err < 0)
  2269. return err;
  2270. }
  2271. }
  2272. if (mixer && spec->add_stereo_mix_input) {
  2273. err = parse_capture_source(codec, mixer, CFG_IDX_MIX, num_adcs,
  2274. "Stereo Mix", 0);
  2275. if (err < 0)
  2276. return err;
  2277. }
  2278. return 0;
  2279. }
  2280. /*
  2281. * input source mux
  2282. */
  2283. /* get the input path specified by the given adc and imux indices */
  2284. static struct nid_path *get_input_path(struct hda_codec *codec, int adc_idx, int imux_idx)
  2285. {
  2286. struct hda_gen_spec *spec = codec->spec;
  2287. if (imux_idx < 0 || imux_idx >= HDA_MAX_NUM_INPUTS) {
  2288. snd_BUG();
  2289. return NULL;
  2290. }
  2291. if (spec->dyn_adc_switch)
  2292. adc_idx = spec->dyn_adc_idx[imux_idx];
  2293. if (adc_idx < 0 || adc_idx >= AUTO_CFG_MAX_OUTS) {
  2294. snd_BUG();
  2295. return NULL;
  2296. }
  2297. return snd_hda_get_path_from_idx(codec, spec->input_paths[imux_idx][adc_idx]);
  2298. }
  2299. static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
  2300. unsigned int idx);
  2301. static int mux_enum_info(struct snd_kcontrol *kcontrol,
  2302. struct snd_ctl_elem_info *uinfo)
  2303. {
  2304. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2305. struct hda_gen_spec *spec = codec->spec;
  2306. return snd_hda_input_mux_info(&spec->input_mux, uinfo);
  2307. }
  2308. static int mux_enum_get(struct snd_kcontrol *kcontrol,
  2309. struct snd_ctl_elem_value *ucontrol)
  2310. {
  2311. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2312. struct hda_gen_spec *spec = codec->spec;
  2313. unsigned int adc_idx = kcontrol->id.index;
  2314. ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
  2315. return 0;
  2316. }
  2317. static int mux_enum_put(struct snd_kcontrol *kcontrol,
  2318. struct snd_ctl_elem_value *ucontrol)
  2319. {
  2320. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2321. unsigned int adc_idx = kcontrol->id.index;
  2322. return mux_select(codec, adc_idx,
  2323. ucontrol->value.enumerated.item[0]);
  2324. }
  2325. static const struct snd_kcontrol_new cap_src_temp = {
  2326. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2327. .name = "Input Source",
  2328. .info = mux_enum_info,
  2329. .get = mux_enum_get,
  2330. .put = mux_enum_put,
  2331. };
  2332. /*
  2333. * capture volume and capture switch ctls
  2334. */
  2335. typedef int (*put_call_t)(struct snd_kcontrol *kcontrol,
  2336. struct snd_ctl_elem_value *ucontrol);
  2337. /* call the given amp update function for all amps in the imux list at once */
  2338. static int cap_put_caller(struct snd_kcontrol *kcontrol,
  2339. struct snd_ctl_elem_value *ucontrol,
  2340. put_call_t func, int type)
  2341. {
  2342. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2343. struct hda_gen_spec *spec = codec->spec;
  2344. const struct hda_input_mux *imux;
  2345. struct nid_path *path;
  2346. int i, adc_idx, err = 0;
  2347. imux = &spec->input_mux;
  2348. adc_idx = kcontrol->id.index;
  2349. mutex_lock(&codec->control_mutex);
  2350. /* we use the cache-only update at first since multiple input paths
  2351. * may shared the same amp; by updating only caches, the redundant
  2352. * writes to hardware can be reduced.
  2353. */
  2354. codec->cached_write = 1;
  2355. for (i = 0; i < imux->num_items; i++) {
  2356. path = get_input_path(codec, adc_idx, i);
  2357. if (!path || !path->ctls[type])
  2358. continue;
  2359. kcontrol->private_value = path->ctls[type];
  2360. err = func(kcontrol, ucontrol);
  2361. if (err < 0)
  2362. goto error;
  2363. }
  2364. error:
  2365. codec->cached_write = 0;
  2366. mutex_unlock(&codec->control_mutex);
  2367. snd_hda_codec_flush_amp_cache(codec); /* flush the updates */
  2368. if (err >= 0 && spec->cap_sync_hook)
  2369. spec->cap_sync_hook(codec);
  2370. return err;
  2371. }
  2372. /* capture volume ctl callbacks */
  2373. #define cap_vol_info snd_hda_mixer_amp_volume_info
  2374. #define cap_vol_get snd_hda_mixer_amp_volume_get
  2375. #define cap_vol_tlv snd_hda_mixer_amp_tlv
  2376. static int cap_vol_put(struct snd_kcontrol *kcontrol,
  2377. struct snd_ctl_elem_value *ucontrol)
  2378. {
  2379. return cap_put_caller(kcontrol, ucontrol,
  2380. snd_hda_mixer_amp_volume_put,
  2381. NID_PATH_VOL_CTL);
  2382. }
  2383. static const struct snd_kcontrol_new cap_vol_temp = {
  2384. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2385. .name = "Capture Volume",
  2386. .access = (SNDRV_CTL_ELEM_ACCESS_READWRITE |
  2387. SNDRV_CTL_ELEM_ACCESS_TLV_READ |
  2388. SNDRV_CTL_ELEM_ACCESS_TLV_CALLBACK),
  2389. .info = cap_vol_info,
  2390. .get = cap_vol_get,
  2391. .put = cap_vol_put,
  2392. .tlv = { .c = cap_vol_tlv },
  2393. };
  2394. /* capture switch ctl callbacks */
  2395. #define cap_sw_info snd_ctl_boolean_stereo_info
  2396. #define cap_sw_get snd_hda_mixer_amp_switch_get
  2397. static int cap_sw_put(struct snd_kcontrol *kcontrol,
  2398. struct snd_ctl_elem_value *ucontrol)
  2399. {
  2400. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2401. struct hda_gen_spec *spec = codec->spec;
  2402. int ret;
  2403. ret = cap_put_caller(kcontrol, ucontrol,
  2404. snd_hda_mixer_amp_switch_put,
  2405. NID_PATH_MUTE_CTL);
  2406. if (ret < 0)
  2407. return ret;
  2408. if (spec->capture_switch_hook) {
  2409. bool enable = (ucontrol->value.integer.value[0] ||
  2410. ucontrol->value.integer.value[1]);
  2411. spec->capture_switch_hook(codec, enable);
  2412. }
  2413. return ret;
  2414. }
  2415. static const struct snd_kcontrol_new cap_sw_temp = {
  2416. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2417. .name = "Capture Switch",
  2418. .info = cap_sw_info,
  2419. .get = cap_sw_get,
  2420. .put = cap_sw_put,
  2421. };
  2422. static int parse_capvol_in_path(struct hda_codec *codec, struct nid_path *path)
  2423. {
  2424. hda_nid_t nid;
  2425. int i, depth;
  2426. path->ctls[NID_PATH_VOL_CTL] = path->ctls[NID_PATH_MUTE_CTL] = 0;
  2427. for (depth = 0; depth < 3; depth++) {
  2428. if (depth >= path->depth)
  2429. return -EINVAL;
  2430. i = path->depth - depth - 1;
  2431. nid = path->path[i];
  2432. if (!path->ctls[NID_PATH_VOL_CTL]) {
  2433. if (nid_has_volume(codec, nid, HDA_OUTPUT))
  2434. path->ctls[NID_PATH_VOL_CTL] =
  2435. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
  2436. else if (nid_has_volume(codec, nid, HDA_INPUT)) {
  2437. int idx = path->idx[i];
  2438. if (!depth && codec->single_adc_amp)
  2439. idx = 0;
  2440. path->ctls[NID_PATH_VOL_CTL] =
  2441. HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
  2442. }
  2443. }
  2444. if (!path->ctls[NID_PATH_MUTE_CTL]) {
  2445. if (nid_has_mute(codec, nid, HDA_OUTPUT))
  2446. path->ctls[NID_PATH_MUTE_CTL] =
  2447. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT);
  2448. else if (nid_has_mute(codec, nid, HDA_INPUT)) {
  2449. int idx = path->idx[i];
  2450. if (!depth && codec->single_adc_amp)
  2451. idx = 0;
  2452. path->ctls[NID_PATH_MUTE_CTL] =
  2453. HDA_COMPOSE_AMP_VAL(nid, 3, idx, HDA_INPUT);
  2454. }
  2455. }
  2456. }
  2457. return 0;
  2458. }
  2459. static bool is_inv_dmic_pin(struct hda_codec *codec, hda_nid_t nid)
  2460. {
  2461. struct hda_gen_spec *spec = codec->spec;
  2462. struct auto_pin_cfg *cfg = &spec->autocfg;
  2463. unsigned int val;
  2464. int i;
  2465. if (!spec->inv_dmic_split)
  2466. return false;
  2467. for (i = 0; i < cfg->num_inputs; i++) {
  2468. if (cfg->inputs[i].pin != nid)
  2469. continue;
  2470. if (cfg->inputs[i].type != AUTO_PIN_MIC)
  2471. return false;
  2472. val = snd_hda_codec_get_pincfg(codec, nid);
  2473. return snd_hda_get_input_pin_attr(val) == INPUT_PIN_ATTR_INT;
  2474. }
  2475. return false;
  2476. }
  2477. static int add_single_cap_ctl(struct hda_codec *codec, const char *label,
  2478. int idx, bool is_switch, unsigned int ctl,
  2479. bool inv_dmic)
  2480. {
  2481. struct hda_gen_spec *spec = codec->spec;
  2482. char tmpname[44];
  2483. int type = is_switch ? HDA_CTL_WIDGET_MUTE : HDA_CTL_WIDGET_VOL;
  2484. const char *sfx = is_switch ? "Switch" : "Volume";
  2485. unsigned int chs = inv_dmic ? 1 : 3;
  2486. int err;
  2487. if (!ctl)
  2488. return 0;
  2489. if (label)
  2490. snprintf(tmpname, sizeof(tmpname),
  2491. "%s Capture %s", label, sfx);
  2492. else
  2493. snprintf(tmpname, sizeof(tmpname),
  2494. "Capture %s", sfx);
  2495. err = add_control(spec, type, tmpname, idx,
  2496. amp_val_replace_channels(ctl, chs));
  2497. if (err < 0 || !inv_dmic)
  2498. return err;
  2499. /* Make independent right kcontrol */
  2500. if (label)
  2501. snprintf(tmpname, sizeof(tmpname),
  2502. "Inverted %s Capture %s", label, sfx);
  2503. else
  2504. snprintf(tmpname, sizeof(tmpname),
  2505. "Inverted Capture %s", sfx);
  2506. return add_control(spec, type, tmpname, idx,
  2507. amp_val_replace_channels(ctl, 2));
  2508. }
  2509. /* create single (and simple) capture volume and switch controls */
  2510. static int create_single_cap_vol_ctl(struct hda_codec *codec, int idx,
  2511. unsigned int vol_ctl, unsigned int sw_ctl,
  2512. bool inv_dmic)
  2513. {
  2514. int err;
  2515. err = add_single_cap_ctl(codec, NULL, idx, false, vol_ctl, inv_dmic);
  2516. if (err < 0)
  2517. return err;
  2518. err = add_single_cap_ctl(codec, NULL, idx, true, sw_ctl, inv_dmic);
  2519. if (err < 0)
  2520. return err;
  2521. return 0;
  2522. }
  2523. /* create bound capture volume and switch controls */
  2524. static int create_bind_cap_vol_ctl(struct hda_codec *codec, int idx,
  2525. unsigned int vol_ctl, unsigned int sw_ctl)
  2526. {
  2527. struct hda_gen_spec *spec = codec->spec;
  2528. struct snd_kcontrol_new *knew;
  2529. if (vol_ctl) {
  2530. knew = snd_hda_gen_add_kctl(spec, NULL, &cap_vol_temp);
  2531. if (!knew)
  2532. return -ENOMEM;
  2533. knew->index = idx;
  2534. knew->private_value = vol_ctl;
  2535. knew->subdevice = HDA_SUBDEV_AMP_FLAG;
  2536. }
  2537. if (sw_ctl) {
  2538. knew = snd_hda_gen_add_kctl(spec, NULL, &cap_sw_temp);
  2539. if (!knew)
  2540. return -ENOMEM;
  2541. knew->index = idx;
  2542. knew->private_value = sw_ctl;
  2543. knew->subdevice = HDA_SUBDEV_AMP_FLAG;
  2544. }
  2545. return 0;
  2546. }
  2547. /* return the vol ctl when used first in the imux list */
  2548. static unsigned int get_first_cap_ctl(struct hda_codec *codec, int idx, int type)
  2549. {
  2550. struct nid_path *path;
  2551. unsigned int ctl;
  2552. int i;
  2553. path = get_input_path(codec, 0, idx);
  2554. if (!path)
  2555. return 0;
  2556. ctl = path->ctls[type];
  2557. if (!ctl)
  2558. return 0;
  2559. for (i = 0; i < idx - 1; i++) {
  2560. path = get_input_path(codec, 0, i);
  2561. if (path && path->ctls[type] == ctl)
  2562. return 0;
  2563. }
  2564. return ctl;
  2565. }
  2566. /* create individual capture volume and switch controls per input */
  2567. static int create_multi_cap_vol_ctl(struct hda_codec *codec)
  2568. {
  2569. struct hda_gen_spec *spec = codec->spec;
  2570. struct hda_input_mux *imux = &spec->input_mux;
  2571. int i, err, type, type_idx = 0;
  2572. const char *prev_label = NULL;
  2573. for (i = 0; i < imux->num_items; i++) {
  2574. const char *label;
  2575. bool inv_dmic;
  2576. if (imux->items[i].index >= spec->autocfg.num_inputs)
  2577. continue;
  2578. label = hda_get_autocfg_input_label(codec, &spec->autocfg,
  2579. imux->items[i].index);
  2580. if (prev_label && !strcmp(label, prev_label))
  2581. type_idx++;
  2582. else
  2583. type_idx = 0;
  2584. prev_label = label;
  2585. inv_dmic = is_inv_dmic_pin(codec, spec->imux_pins[i]);
  2586. for (type = 0; type < 2; type++) {
  2587. err = add_single_cap_ctl(codec, label, type_idx, type,
  2588. get_first_cap_ctl(codec, i, type),
  2589. inv_dmic);
  2590. if (err < 0)
  2591. return err;
  2592. }
  2593. }
  2594. return 0;
  2595. }
  2596. static int create_capture_mixers(struct hda_codec *codec)
  2597. {
  2598. struct hda_gen_spec *spec = codec->spec;
  2599. struct hda_input_mux *imux = &spec->input_mux;
  2600. int i, n, nums, err;
  2601. if (spec->dyn_adc_switch)
  2602. nums = 1;
  2603. else
  2604. nums = spec->num_adc_nids;
  2605. if (!spec->auto_mic && imux->num_items > 1) {
  2606. struct snd_kcontrol_new *knew;
  2607. const char *name;
  2608. name = nums > 1 ? "Input Source" : "Capture Source";
  2609. knew = snd_hda_gen_add_kctl(spec, name, &cap_src_temp);
  2610. if (!knew)
  2611. return -ENOMEM;
  2612. knew->count = nums;
  2613. }
  2614. for (n = 0; n < nums; n++) {
  2615. bool multi = false;
  2616. bool multi_cap_vol = spec->multi_cap_vol;
  2617. bool inv_dmic = false;
  2618. int vol, sw;
  2619. vol = sw = 0;
  2620. for (i = 0; i < imux->num_items; i++) {
  2621. struct nid_path *path;
  2622. path = get_input_path(codec, n, i);
  2623. if (!path)
  2624. continue;
  2625. parse_capvol_in_path(codec, path);
  2626. if (!vol)
  2627. vol = path->ctls[NID_PATH_VOL_CTL];
  2628. else if (vol != path->ctls[NID_PATH_VOL_CTL]) {
  2629. multi = true;
  2630. if (!same_amp_caps(codec, vol,
  2631. path->ctls[NID_PATH_VOL_CTL], HDA_INPUT))
  2632. multi_cap_vol = true;
  2633. }
  2634. if (!sw)
  2635. sw = path->ctls[NID_PATH_MUTE_CTL];
  2636. else if (sw != path->ctls[NID_PATH_MUTE_CTL]) {
  2637. multi = true;
  2638. if (!same_amp_caps(codec, sw,
  2639. path->ctls[NID_PATH_MUTE_CTL], HDA_INPUT))
  2640. multi_cap_vol = true;
  2641. }
  2642. if (is_inv_dmic_pin(codec, spec->imux_pins[i]))
  2643. inv_dmic = true;
  2644. }
  2645. if (!multi)
  2646. err = create_single_cap_vol_ctl(codec, n, vol, sw,
  2647. inv_dmic);
  2648. else if (!multi_cap_vol)
  2649. err = create_bind_cap_vol_ctl(codec, n, vol, sw);
  2650. else
  2651. err = create_multi_cap_vol_ctl(codec);
  2652. if (err < 0)
  2653. return err;
  2654. }
  2655. return 0;
  2656. }
  2657. /*
  2658. * add mic boosts if needed
  2659. */
  2660. static int parse_mic_boost(struct hda_codec *codec)
  2661. {
  2662. struct hda_gen_spec *spec = codec->spec;
  2663. struct auto_pin_cfg *cfg = &spec->autocfg;
  2664. int i, err;
  2665. int type_idx = 0;
  2666. hda_nid_t nid;
  2667. const char *prev_label = NULL;
  2668. for (i = 0; i < cfg->num_inputs; i++) {
  2669. if (cfg->inputs[i].type > AUTO_PIN_MIC)
  2670. break;
  2671. nid = cfg->inputs[i].pin;
  2672. if (get_wcaps(codec, nid) & AC_WCAP_IN_AMP) {
  2673. const char *label;
  2674. char boost_label[44];
  2675. struct nid_path *path;
  2676. unsigned int val;
  2677. if (!nid_has_volume(codec, nid, HDA_INPUT))
  2678. continue;
  2679. label = hda_get_autocfg_input_label(codec, cfg, i);
  2680. if (prev_label && !strcmp(label, prev_label))
  2681. type_idx++;
  2682. else
  2683. type_idx = 0;
  2684. prev_label = label;
  2685. snprintf(boost_label, sizeof(boost_label),
  2686. "%s Boost Volume", label);
  2687. val = HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_INPUT);
  2688. err = add_control(spec, HDA_CTL_WIDGET_VOL,
  2689. boost_label, type_idx, val);
  2690. if (err < 0)
  2691. return err;
  2692. path = snd_hda_get_nid_path(codec, nid, 0);
  2693. if (path)
  2694. path->ctls[NID_PATH_BOOST_CTL] = val;
  2695. }
  2696. }
  2697. return 0;
  2698. }
  2699. /*
  2700. * parse digital I/Os and set up NIDs in BIOS auto-parse mode
  2701. */
  2702. static void parse_digital(struct hda_codec *codec)
  2703. {
  2704. struct hda_gen_spec *spec = codec->spec;
  2705. struct nid_path *path;
  2706. int i, nums;
  2707. hda_nid_t dig_nid, pin;
  2708. /* support multiple SPDIFs; the secondary is set up as a slave */
  2709. nums = 0;
  2710. for (i = 0; i < spec->autocfg.dig_outs; i++) {
  2711. pin = spec->autocfg.dig_out_pins[i];
  2712. dig_nid = look_for_dac(codec, pin, true);
  2713. if (!dig_nid)
  2714. continue;
  2715. path = snd_hda_add_new_path(codec, dig_nid, pin, 0);
  2716. if (!path)
  2717. continue;
  2718. print_nid_path("digout", path);
  2719. path->active = true;
  2720. spec->digout_paths[i] = snd_hda_get_path_idx(codec, path);
  2721. set_pin_target(codec, pin, PIN_OUT, false);
  2722. if (!nums) {
  2723. spec->multiout.dig_out_nid = dig_nid;
  2724. spec->dig_out_type = spec->autocfg.dig_out_type[0];
  2725. } else {
  2726. spec->multiout.slave_dig_outs = spec->slave_dig_outs;
  2727. if (nums >= ARRAY_SIZE(spec->slave_dig_outs) - 1)
  2728. break;
  2729. spec->slave_dig_outs[nums - 1] = dig_nid;
  2730. }
  2731. nums++;
  2732. }
  2733. if (spec->autocfg.dig_in_pin) {
  2734. pin = spec->autocfg.dig_in_pin;
  2735. dig_nid = codec->start_nid;
  2736. for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
  2737. unsigned int wcaps = get_wcaps(codec, dig_nid);
  2738. if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
  2739. continue;
  2740. if (!(wcaps & AC_WCAP_DIGITAL))
  2741. continue;
  2742. path = snd_hda_add_new_path(codec, pin, dig_nid, 0);
  2743. if (path) {
  2744. print_nid_path("digin", path);
  2745. path->active = true;
  2746. spec->dig_in_nid = dig_nid;
  2747. spec->digin_path = snd_hda_get_path_idx(codec, path);
  2748. set_pin_target(codec, pin, PIN_IN, false);
  2749. break;
  2750. }
  2751. }
  2752. }
  2753. }
  2754. /*
  2755. * input MUX handling
  2756. */
  2757. static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur);
  2758. /* select the given imux item; either unmute exclusively or select the route */
  2759. static int mux_select(struct hda_codec *codec, unsigned int adc_idx,
  2760. unsigned int idx)
  2761. {
  2762. struct hda_gen_spec *spec = codec->spec;
  2763. const struct hda_input_mux *imux;
  2764. struct nid_path *path;
  2765. imux = &spec->input_mux;
  2766. if (!imux->num_items)
  2767. return 0;
  2768. if (idx >= imux->num_items)
  2769. idx = imux->num_items - 1;
  2770. if (spec->cur_mux[adc_idx] == idx)
  2771. return 0;
  2772. path = get_input_path(codec, adc_idx, spec->cur_mux[adc_idx]);
  2773. if (!path)
  2774. return 0;
  2775. if (path->active)
  2776. snd_hda_activate_path(codec, path, false, false);
  2777. spec->cur_mux[adc_idx] = idx;
  2778. if (spec->shared_mic_hp)
  2779. update_shared_mic_hp(codec, spec->cur_mux[adc_idx]);
  2780. if (spec->dyn_adc_switch)
  2781. dyn_adc_pcm_resetup(codec, idx);
  2782. path = get_input_path(codec, adc_idx, idx);
  2783. if (!path)
  2784. return 0;
  2785. if (path->active)
  2786. return 0;
  2787. snd_hda_activate_path(codec, path, true, false);
  2788. if (spec->cap_sync_hook)
  2789. spec->cap_sync_hook(codec);
  2790. return 1;
  2791. }
  2792. /*
  2793. * Jack detections for HP auto-mute and mic-switch
  2794. */
  2795. /* check each pin in the given array; returns true if any of them is plugged */
  2796. static bool detect_jacks(struct hda_codec *codec, int num_pins, hda_nid_t *pins)
  2797. {
  2798. int i, present = 0;
  2799. for (i = 0; i < num_pins; i++) {
  2800. hda_nid_t nid = pins[i];
  2801. if (!nid)
  2802. break;
  2803. /* don't detect pins retasked as inputs */
  2804. if (snd_hda_codec_get_pin_target(codec, nid) & AC_PINCTL_IN_EN)
  2805. continue;
  2806. present |= snd_hda_jack_detect(codec, nid);
  2807. }
  2808. return present;
  2809. }
  2810. /* standard HP/line-out auto-mute helper */
  2811. static void do_automute(struct hda_codec *codec, int num_pins, hda_nid_t *pins,
  2812. bool mute)
  2813. {
  2814. struct hda_gen_spec *spec = codec->spec;
  2815. int i;
  2816. for (i = 0; i < num_pins; i++) {
  2817. hda_nid_t nid = pins[i];
  2818. unsigned int val;
  2819. if (!nid)
  2820. break;
  2821. /* don't reset VREF value in case it's controlling
  2822. * the amp (see alc861_fixup_asus_amp_vref_0f())
  2823. */
  2824. if (spec->keep_vref_in_automute)
  2825. val = snd_hda_codec_get_pin_target(codec, nid) & ~PIN_HP;
  2826. else
  2827. val = 0;
  2828. if (!mute)
  2829. val |= snd_hda_codec_get_pin_target(codec, nid);
  2830. /* here we call update_pin_ctl() so that the pinctl is changed
  2831. * without changing the pinctl target value;
  2832. * the original target value will be still referred at the
  2833. * init / resume again
  2834. */
  2835. update_pin_ctl(codec, nid, val);
  2836. set_pin_eapd(codec, nid, !mute);
  2837. }
  2838. }
  2839. /* Toggle outputs muting */
  2840. void snd_hda_gen_update_outputs(struct hda_codec *codec)
  2841. {
  2842. struct hda_gen_spec *spec = codec->spec;
  2843. int on;
  2844. /* Control HP pins/amps depending on master_mute state;
  2845. * in general, HP pins/amps control should be enabled in all cases,
  2846. * but currently set only for master_mute, just to be safe
  2847. */
  2848. if (!spec->shared_mic_hp) /* don't change HP-pin when shared with mic */
  2849. do_automute(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
  2850. spec->autocfg.hp_pins, spec->master_mute);
  2851. if (!spec->automute_speaker)
  2852. on = 0;
  2853. else
  2854. on = spec->hp_jack_present | spec->line_jack_present;
  2855. on |= spec->master_mute;
  2856. spec->speaker_muted = on;
  2857. do_automute(codec, ARRAY_SIZE(spec->autocfg.speaker_pins),
  2858. spec->autocfg.speaker_pins, on);
  2859. /* toggle line-out mutes if needed, too */
  2860. /* if LO is a copy of either HP or Speaker, don't need to handle it */
  2861. if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0] ||
  2862. spec->autocfg.line_out_pins[0] == spec->autocfg.speaker_pins[0])
  2863. return;
  2864. if (!spec->automute_lo)
  2865. on = 0;
  2866. else
  2867. on = spec->hp_jack_present;
  2868. on |= spec->master_mute;
  2869. spec->line_out_muted = on;
  2870. do_automute(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
  2871. spec->autocfg.line_out_pins, on);
  2872. }
  2873. EXPORT_SYMBOL_HDA(snd_hda_gen_update_outputs);
  2874. static void call_update_outputs(struct hda_codec *codec)
  2875. {
  2876. struct hda_gen_spec *spec = codec->spec;
  2877. if (spec->automute_hook)
  2878. spec->automute_hook(codec);
  2879. else
  2880. snd_hda_gen_update_outputs(codec);
  2881. }
  2882. /* standard HP-automute helper */
  2883. void snd_hda_gen_hp_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
  2884. {
  2885. struct hda_gen_spec *spec = codec->spec;
  2886. spec->hp_jack_present =
  2887. detect_jacks(codec, ARRAY_SIZE(spec->autocfg.hp_pins),
  2888. spec->autocfg.hp_pins);
  2889. if (!spec->detect_hp || (!spec->automute_speaker && !spec->automute_lo))
  2890. return;
  2891. call_update_outputs(codec);
  2892. }
  2893. EXPORT_SYMBOL_HDA(snd_hda_gen_hp_automute);
  2894. /* standard line-out-automute helper */
  2895. void snd_hda_gen_line_automute(struct hda_codec *codec, struct hda_jack_tbl *jack)
  2896. {
  2897. struct hda_gen_spec *spec = codec->spec;
  2898. if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT)
  2899. return;
  2900. /* check LO jack only when it's different from HP */
  2901. if (spec->autocfg.line_out_pins[0] == spec->autocfg.hp_pins[0])
  2902. return;
  2903. spec->line_jack_present =
  2904. detect_jacks(codec, ARRAY_SIZE(spec->autocfg.line_out_pins),
  2905. spec->autocfg.line_out_pins);
  2906. if (!spec->automute_speaker || !spec->detect_lo)
  2907. return;
  2908. call_update_outputs(codec);
  2909. }
  2910. EXPORT_SYMBOL_HDA(snd_hda_gen_line_automute);
  2911. /* standard mic auto-switch helper */
  2912. void snd_hda_gen_mic_autoswitch(struct hda_codec *codec, struct hda_jack_tbl *jack)
  2913. {
  2914. struct hda_gen_spec *spec = codec->spec;
  2915. int i;
  2916. if (!spec->auto_mic)
  2917. return;
  2918. for (i = spec->am_num_entries - 1; i > 0; i--) {
  2919. hda_nid_t pin = spec->am_entry[i].pin;
  2920. /* don't detect pins retasked as outputs */
  2921. if (snd_hda_codec_get_pin_target(codec, pin) & AC_PINCTL_OUT_EN)
  2922. continue;
  2923. if (snd_hda_jack_detect(codec, pin)) {
  2924. mux_select(codec, 0, spec->am_entry[i].idx);
  2925. return;
  2926. }
  2927. }
  2928. mux_select(codec, 0, spec->am_entry[0].idx);
  2929. }
  2930. EXPORT_SYMBOL_HDA(snd_hda_gen_mic_autoswitch);
  2931. /* update jack retasking */
  2932. static void update_automute_all(struct hda_codec *codec)
  2933. {
  2934. struct hda_gen_spec *spec = codec->spec;
  2935. if (spec->hp_automute_hook)
  2936. spec->hp_automute_hook(codec, NULL);
  2937. else
  2938. snd_hda_gen_hp_automute(codec, NULL);
  2939. if (spec->line_automute_hook)
  2940. spec->line_automute_hook(codec, NULL);
  2941. else
  2942. snd_hda_gen_line_automute(codec, NULL);
  2943. if (spec->mic_autoswitch_hook)
  2944. spec->mic_autoswitch_hook(codec, NULL);
  2945. else
  2946. snd_hda_gen_mic_autoswitch(codec, NULL);
  2947. }
  2948. /*
  2949. * Auto-Mute mode mixer enum support
  2950. */
  2951. static int automute_mode_info(struct snd_kcontrol *kcontrol,
  2952. struct snd_ctl_elem_info *uinfo)
  2953. {
  2954. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2955. struct hda_gen_spec *spec = codec->spec;
  2956. static const char * const texts3[] = {
  2957. "Disabled", "Speaker Only", "Line Out+Speaker"
  2958. };
  2959. if (spec->automute_speaker_possible && spec->automute_lo_possible)
  2960. return snd_hda_enum_helper_info(kcontrol, uinfo, 3, texts3);
  2961. return snd_hda_enum_bool_helper_info(kcontrol, uinfo);
  2962. }
  2963. static int automute_mode_get(struct snd_kcontrol *kcontrol,
  2964. struct snd_ctl_elem_value *ucontrol)
  2965. {
  2966. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2967. struct hda_gen_spec *spec = codec->spec;
  2968. unsigned int val = 0;
  2969. if (spec->automute_speaker)
  2970. val++;
  2971. if (spec->automute_lo)
  2972. val++;
  2973. ucontrol->value.enumerated.item[0] = val;
  2974. return 0;
  2975. }
  2976. static int automute_mode_put(struct snd_kcontrol *kcontrol,
  2977. struct snd_ctl_elem_value *ucontrol)
  2978. {
  2979. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2980. struct hda_gen_spec *spec = codec->spec;
  2981. switch (ucontrol->value.enumerated.item[0]) {
  2982. case 0:
  2983. if (!spec->automute_speaker && !spec->automute_lo)
  2984. return 0;
  2985. spec->automute_speaker = 0;
  2986. spec->automute_lo = 0;
  2987. break;
  2988. case 1:
  2989. if (spec->automute_speaker_possible) {
  2990. if (!spec->automute_lo && spec->automute_speaker)
  2991. return 0;
  2992. spec->automute_speaker = 1;
  2993. spec->automute_lo = 0;
  2994. } else if (spec->automute_lo_possible) {
  2995. if (spec->automute_lo)
  2996. return 0;
  2997. spec->automute_lo = 1;
  2998. } else
  2999. return -EINVAL;
  3000. break;
  3001. case 2:
  3002. if (!spec->automute_lo_possible || !spec->automute_speaker_possible)
  3003. return -EINVAL;
  3004. if (spec->automute_speaker && spec->automute_lo)
  3005. return 0;
  3006. spec->automute_speaker = 1;
  3007. spec->automute_lo = 1;
  3008. break;
  3009. default:
  3010. return -EINVAL;
  3011. }
  3012. call_update_outputs(codec);
  3013. return 1;
  3014. }
  3015. static const struct snd_kcontrol_new automute_mode_enum = {
  3016. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  3017. .name = "Auto-Mute Mode",
  3018. .info = automute_mode_info,
  3019. .get = automute_mode_get,
  3020. .put = automute_mode_put,
  3021. };
  3022. static int add_automute_mode_enum(struct hda_codec *codec)
  3023. {
  3024. struct hda_gen_spec *spec = codec->spec;
  3025. if (!snd_hda_gen_add_kctl(spec, NULL, &automute_mode_enum))
  3026. return -ENOMEM;
  3027. return 0;
  3028. }
  3029. /*
  3030. * Check the availability of HP/line-out auto-mute;
  3031. * Set up appropriately if really supported
  3032. */
  3033. static int check_auto_mute_availability(struct hda_codec *codec)
  3034. {
  3035. struct hda_gen_spec *spec = codec->spec;
  3036. struct auto_pin_cfg *cfg = &spec->autocfg;
  3037. int present = 0;
  3038. int i, err;
  3039. if (spec->suppress_auto_mute)
  3040. return 0;
  3041. if (cfg->hp_pins[0])
  3042. present++;
  3043. if (cfg->line_out_pins[0])
  3044. present++;
  3045. if (cfg->speaker_pins[0])
  3046. present++;
  3047. if (present < 2) /* need two different output types */
  3048. return 0;
  3049. if (!cfg->speaker_pins[0] &&
  3050. cfg->line_out_type == AUTO_PIN_SPEAKER_OUT) {
  3051. memcpy(cfg->speaker_pins, cfg->line_out_pins,
  3052. sizeof(cfg->speaker_pins));
  3053. cfg->speaker_outs = cfg->line_outs;
  3054. }
  3055. if (!cfg->hp_pins[0] &&
  3056. cfg->line_out_type == AUTO_PIN_HP_OUT) {
  3057. memcpy(cfg->hp_pins, cfg->line_out_pins,
  3058. sizeof(cfg->hp_pins));
  3059. cfg->hp_outs = cfg->line_outs;
  3060. }
  3061. for (i = 0; i < cfg->hp_outs; i++) {
  3062. hda_nid_t nid = cfg->hp_pins[i];
  3063. if (!is_jack_detectable(codec, nid))
  3064. continue;
  3065. snd_printdd("hda-codec: Enable HP auto-muting on NID 0x%x\n",
  3066. nid);
  3067. snd_hda_jack_detect_enable_callback(codec, nid, HDA_GEN_HP_EVENT,
  3068. spec->hp_automute_hook ?
  3069. spec->hp_automute_hook :
  3070. snd_hda_gen_hp_automute);
  3071. spec->detect_hp = 1;
  3072. }
  3073. if (cfg->line_out_type == AUTO_PIN_LINE_OUT && cfg->line_outs) {
  3074. if (cfg->speaker_outs)
  3075. for (i = 0; i < cfg->line_outs; i++) {
  3076. hda_nid_t nid = cfg->line_out_pins[i];
  3077. if (!is_jack_detectable(codec, nid))
  3078. continue;
  3079. snd_printdd("hda-codec: Enable Line-Out auto-muting on NID 0x%x\n", nid);
  3080. snd_hda_jack_detect_enable_callback(codec, nid,
  3081. HDA_GEN_FRONT_EVENT,
  3082. spec->line_automute_hook ?
  3083. spec->line_automute_hook :
  3084. snd_hda_gen_line_automute);
  3085. spec->detect_lo = 1;
  3086. }
  3087. spec->automute_lo_possible = spec->detect_hp;
  3088. }
  3089. spec->automute_speaker_possible = cfg->speaker_outs &&
  3090. (spec->detect_hp || spec->detect_lo);
  3091. spec->automute_lo = spec->automute_lo_possible;
  3092. spec->automute_speaker = spec->automute_speaker_possible;
  3093. if (spec->automute_speaker_possible || spec->automute_lo_possible) {
  3094. /* create a control for automute mode */
  3095. err = add_automute_mode_enum(codec);
  3096. if (err < 0)
  3097. return err;
  3098. }
  3099. return 0;
  3100. }
  3101. /* check whether all auto-mic pins are valid; setup indices if OK */
  3102. static bool auto_mic_check_imux(struct hda_codec *codec)
  3103. {
  3104. struct hda_gen_spec *spec = codec->spec;
  3105. const struct hda_input_mux *imux;
  3106. int i;
  3107. imux = &spec->input_mux;
  3108. for (i = 0; i < spec->am_num_entries; i++) {
  3109. spec->am_entry[i].idx =
  3110. find_idx_in_nid_list(spec->am_entry[i].pin,
  3111. spec->imux_pins, imux->num_items);
  3112. if (spec->am_entry[i].idx < 0)
  3113. return false; /* no corresponding imux */
  3114. }
  3115. /* we don't need the jack detection for the first pin */
  3116. for (i = 1; i < spec->am_num_entries; i++)
  3117. snd_hda_jack_detect_enable_callback(codec,
  3118. spec->am_entry[i].pin,
  3119. HDA_GEN_MIC_EVENT,
  3120. spec->mic_autoswitch_hook ?
  3121. spec->mic_autoswitch_hook :
  3122. snd_hda_gen_mic_autoswitch);
  3123. return true;
  3124. }
  3125. static int compare_attr(const void *ap, const void *bp)
  3126. {
  3127. const struct automic_entry *a = ap;
  3128. const struct automic_entry *b = bp;
  3129. return (int)(a->attr - b->attr);
  3130. }
  3131. /*
  3132. * Check the availability of auto-mic switch;
  3133. * Set up if really supported
  3134. */
  3135. static int check_auto_mic_availability(struct hda_codec *codec)
  3136. {
  3137. struct hda_gen_spec *spec = codec->spec;
  3138. struct auto_pin_cfg *cfg = &spec->autocfg;
  3139. unsigned int types;
  3140. int i, num_pins;
  3141. if (spec->suppress_auto_mic)
  3142. return 0;
  3143. types = 0;
  3144. num_pins = 0;
  3145. for (i = 0; i < cfg->num_inputs; i++) {
  3146. hda_nid_t nid = cfg->inputs[i].pin;
  3147. unsigned int attr;
  3148. attr = snd_hda_codec_get_pincfg(codec, nid);
  3149. attr = snd_hda_get_input_pin_attr(attr);
  3150. if (types & (1 << attr))
  3151. return 0; /* already occupied */
  3152. switch (attr) {
  3153. case INPUT_PIN_ATTR_INT:
  3154. if (cfg->inputs[i].type != AUTO_PIN_MIC)
  3155. return 0; /* invalid type */
  3156. break;
  3157. case INPUT_PIN_ATTR_UNUSED:
  3158. return 0; /* invalid entry */
  3159. default:
  3160. if (cfg->inputs[i].type > AUTO_PIN_LINE_IN)
  3161. return 0; /* invalid type */
  3162. if (!spec->line_in_auto_switch &&
  3163. cfg->inputs[i].type != AUTO_PIN_MIC)
  3164. return 0; /* only mic is allowed */
  3165. if (!is_jack_detectable(codec, nid))
  3166. return 0; /* no unsol support */
  3167. break;
  3168. }
  3169. if (num_pins >= MAX_AUTO_MIC_PINS)
  3170. return 0;
  3171. types |= (1 << attr);
  3172. spec->am_entry[num_pins].pin = nid;
  3173. spec->am_entry[num_pins].attr = attr;
  3174. num_pins++;
  3175. }
  3176. if (num_pins < 2)
  3177. return 0;
  3178. spec->am_num_entries = num_pins;
  3179. /* sort the am_entry in the order of attr so that the pin with a
  3180. * higher attr will be selected when the jack is plugged.
  3181. */
  3182. sort(spec->am_entry, num_pins, sizeof(spec->am_entry[0]),
  3183. compare_attr, NULL);
  3184. if (!auto_mic_check_imux(codec))
  3185. return 0;
  3186. spec->auto_mic = 1;
  3187. spec->num_adc_nids = 1;
  3188. spec->cur_mux[0] = spec->am_entry[0].idx;
  3189. snd_printdd("hda-codec: Enable auto-mic switch on NID 0x%x/0x%x/0x%x\n",
  3190. spec->am_entry[0].pin,
  3191. spec->am_entry[1].pin,
  3192. spec->am_entry[2].pin);
  3193. return 0;
  3194. }
  3195. /*
  3196. * Parse the given BIOS configuration and set up the hda_gen_spec
  3197. *
  3198. * return 1 if successful, 0 if the proper config is not found,
  3199. * or a negative error code
  3200. */
  3201. int snd_hda_gen_parse_auto_config(struct hda_codec *codec,
  3202. struct auto_pin_cfg *cfg)
  3203. {
  3204. struct hda_gen_spec *spec = codec->spec;
  3205. int err;
  3206. parse_user_hints(codec);
  3207. if (cfg != &spec->autocfg) {
  3208. spec->autocfg = *cfg;
  3209. cfg = &spec->autocfg;
  3210. }
  3211. fill_all_dac_nids(codec);
  3212. if (!cfg->line_outs) {
  3213. if (cfg->dig_outs || cfg->dig_in_pin) {
  3214. spec->multiout.max_channels = 2;
  3215. spec->no_analog = 1;
  3216. goto dig_only;
  3217. }
  3218. return 0; /* can't find valid BIOS pin config */
  3219. }
  3220. if (!spec->no_primary_hp &&
  3221. cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
  3222. cfg->line_outs <= cfg->hp_outs) {
  3223. /* use HP as primary out */
  3224. cfg->speaker_outs = cfg->line_outs;
  3225. memcpy(cfg->speaker_pins, cfg->line_out_pins,
  3226. sizeof(cfg->speaker_pins));
  3227. cfg->line_outs = cfg->hp_outs;
  3228. memcpy(cfg->line_out_pins, cfg->hp_pins, sizeof(cfg->hp_pins));
  3229. cfg->hp_outs = 0;
  3230. memset(cfg->hp_pins, 0, sizeof(cfg->hp_pins));
  3231. cfg->line_out_type = AUTO_PIN_HP_OUT;
  3232. }
  3233. err = parse_output_paths(codec);
  3234. if (err < 0)
  3235. return err;
  3236. err = create_multi_channel_mode(codec);
  3237. if (err < 0)
  3238. return err;
  3239. err = create_multi_out_ctls(codec, cfg);
  3240. if (err < 0)
  3241. return err;
  3242. err = create_hp_out_ctls(codec);
  3243. if (err < 0)
  3244. return err;
  3245. err = create_speaker_out_ctls(codec);
  3246. if (err < 0)
  3247. return err;
  3248. err = create_indep_hp_ctls(codec);
  3249. if (err < 0)
  3250. return err;
  3251. err = create_loopback_mixing_ctl(codec);
  3252. if (err < 0)
  3253. return err;
  3254. err = create_shared_input(codec);
  3255. if (err < 0)
  3256. return err;
  3257. err = create_input_ctls(codec);
  3258. if (err < 0)
  3259. return err;
  3260. spec->const_channel_count = spec->ext_channel_count;
  3261. /* check the multiple speaker and headphone pins */
  3262. if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT)
  3263. spec->const_channel_count = max(spec->const_channel_count,
  3264. cfg->speaker_outs * 2);
  3265. if (cfg->line_out_type != AUTO_PIN_HP_OUT)
  3266. spec->const_channel_count = max(spec->const_channel_count,
  3267. cfg->hp_outs * 2);
  3268. spec->multiout.max_channels = max(spec->ext_channel_count,
  3269. spec->const_channel_count);
  3270. err = check_auto_mute_availability(codec);
  3271. if (err < 0)
  3272. return err;
  3273. err = check_dyn_adc_switch(codec);
  3274. if (err < 0)
  3275. return err;
  3276. if (!spec->shared_mic_hp) {
  3277. err = check_auto_mic_availability(codec);
  3278. if (err < 0)
  3279. return err;
  3280. }
  3281. err = create_capture_mixers(codec);
  3282. if (err < 0)
  3283. return err;
  3284. err = parse_mic_boost(codec);
  3285. if (err < 0)
  3286. return err;
  3287. if (spec->add_out_jack_modes) {
  3288. if (cfg->line_out_type != AUTO_PIN_SPEAKER_OUT) {
  3289. err = create_out_jack_modes(codec, cfg->line_outs,
  3290. cfg->line_out_pins);
  3291. if (err < 0)
  3292. return err;
  3293. }
  3294. if (cfg->line_out_type != AUTO_PIN_HP_OUT) {
  3295. err = create_out_jack_modes(codec, cfg->hp_outs,
  3296. cfg->hp_pins);
  3297. if (err < 0)
  3298. return err;
  3299. }
  3300. }
  3301. dig_only:
  3302. parse_digital(codec);
  3303. return 1;
  3304. }
  3305. EXPORT_SYMBOL_HDA(snd_hda_gen_parse_auto_config);
  3306. /*
  3307. * Build control elements
  3308. */
  3309. /* slave controls for virtual master */
  3310. static const char * const slave_pfxs[] = {
  3311. "Front", "Surround", "Center", "LFE", "Side",
  3312. "Headphone", "Speaker", "Mono", "Line Out",
  3313. "CLFE", "Bass Speaker", "PCM",
  3314. "Speaker Front", "Speaker Surround", "Speaker CLFE", "Speaker Side",
  3315. "Headphone Front", "Headphone Surround", "Headphone CLFE",
  3316. "Headphone Side",
  3317. NULL,
  3318. };
  3319. int snd_hda_gen_build_controls(struct hda_codec *codec)
  3320. {
  3321. struct hda_gen_spec *spec = codec->spec;
  3322. int err;
  3323. if (spec->kctls.used) {
  3324. err = snd_hda_add_new_ctls(codec, spec->kctls.list);
  3325. if (err < 0)
  3326. return err;
  3327. }
  3328. if (spec->multiout.dig_out_nid) {
  3329. err = snd_hda_create_dig_out_ctls(codec,
  3330. spec->multiout.dig_out_nid,
  3331. spec->multiout.dig_out_nid,
  3332. spec->pcm_rec[1].pcm_type);
  3333. if (err < 0)
  3334. return err;
  3335. if (!spec->no_analog) {
  3336. err = snd_hda_create_spdif_share_sw(codec,
  3337. &spec->multiout);
  3338. if (err < 0)
  3339. return err;
  3340. spec->multiout.share_spdif = 1;
  3341. }
  3342. }
  3343. if (spec->dig_in_nid) {
  3344. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
  3345. if (err < 0)
  3346. return err;
  3347. }
  3348. /* if we have no master control, let's create it */
  3349. if (!spec->no_analog &&
  3350. !snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
  3351. err = snd_hda_add_vmaster(codec, "Master Playback Volume",
  3352. spec->vmaster_tlv, slave_pfxs,
  3353. "Playback Volume");
  3354. if (err < 0)
  3355. return err;
  3356. }
  3357. if (!spec->no_analog &&
  3358. !snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
  3359. err = __snd_hda_add_vmaster(codec, "Master Playback Switch",
  3360. NULL, slave_pfxs,
  3361. "Playback Switch",
  3362. true, &spec->vmaster_mute.sw_kctl);
  3363. if (err < 0)
  3364. return err;
  3365. if (spec->vmaster_mute.hook)
  3366. snd_hda_add_vmaster_hook(codec, &spec->vmaster_mute,
  3367. spec->vmaster_mute_enum);
  3368. }
  3369. free_kctls(spec); /* no longer needed */
  3370. if (spec->shared_mic_hp) {
  3371. int err;
  3372. int nid = spec->autocfg.inputs[1].pin;
  3373. err = snd_hda_jack_add_kctl(codec, nid, "Headphone Mic", 0);
  3374. if (err < 0)
  3375. return err;
  3376. err = snd_hda_jack_detect_enable(codec, nid, 0);
  3377. if (err < 0)
  3378. return err;
  3379. }
  3380. err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
  3381. if (err < 0)
  3382. return err;
  3383. return 0;
  3384. }
  3385. EXPORT_SYMBOL_HDA(snd_hda_gen_build_controls);
  3386. /*
  3387. * PCM definitions
  3388. */
  3389. static void call_pcm_playback_hook(struct hda_pcm_stream *hinfo,
  3390. struct hda_codec *codec,
  3391. struct snd_pcm_substream *substream,
  3392. int action)
  3393. {
  3394. struct hda_gen_spec *spec = codec->spec;
  3395. if (spec->pcm_playback_hook)
  3396. spec->pcm_playback_hook(hinfo, codec, substream, action);
  3397. }
  3398. static void call_pcm_capture_hook(struct hda_pcm_stream *hinfo,
  3399. struct hda_codec *codec,
  3400. struct snd_pcm_substream *substream,
  3401. int action)
  3402. {
  3403. struct hda_gen_spec *spec = codec->spec;
  3404. if (spec->pcm_capture_hook)
  3405. spec->pcm_capture_hook(hinfo, codec, substream, action);
  3406. }
  3407. /*
  3408. * Analog playback callbacks
  3409. */
  3410. static int playback_pcm_open(struct hda_pcm_stream *hinfo,
  3411. struct hda_codec *codec,
  3412. struct snd_pcm_substream *substream)
  3413. {
  3414. struct hda_gen_spec *spec = codec->spec;
  3415. int err;
  3416. mutex_lock(&spec->pcm_mutex);
  3417. err = snd_hda_multi_out_analog_open(codec,
  3418. &spec->multiout, substream,
  3419. hinfo);
  3420. if (!err) {
  3421. spec->active_streams |= 1 << STREAM_MULTI_OUT;
  3422. call_pcm_playback_hook(hinfo, codec, substream,
  3423. HDA_GEN_PCM_ACT_OPEN);
  3424. }
  3425. mutex_unlock(&spec->pcm_mutex);
  3426. return err;
  3427. }
  3428. static int playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  3429. struct hda_codec *codec,
  3430. unsigned int stream_tag,
  3431. unsigned int format,
  3432. struct snd_pcm_substream *substream)
  3433. {
  3434. struct hda_gen_spec *spec = codec->spec;
  3435. int err;
  3436. err = snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
  3437. stream_tag, format, substream);
  3438. if (!err)
  3439. call_pcm_playback_hook(hinfo, codec, substream,
  3440. HDA_GEN_PCM_ACT_PREPARE);
  3441. return err;
  3442. }
  3443. static int playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  3444. struct hda_codec *codec,
  3445. struct snd_pcm_substream *substream)
  3446. {
  3447. struct hda_gen_spec *spec = codec->spec;
  3448. int err;
  3449. err = snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  3450. if (!err)
  3451. call_pcm_playback_hook(hinfo, codec, substream,
  3452. HDA_GEN_PCM_ACT_CLEANUP);
  3453. return err;
  3454. }
  3455. static int playback_pcm_close(struct hda_pcm_stream *hinfo,
  3456. struct hda_codec *codec,
  3457. struct snd_pcm_substream *substream)
  3458. {
  3459. struct hda_gen_spec *spec = codec->spec;
  3460. mutex_lock(&spec->pcm_mutex);
  3461. spec->active_streams &= ~(1 << STREAM_MULTI_OUT);
  3462. call_pcm_playback_hook(hinfo, codec, substream,
  3463. HDA_GEN_PCM_ACT_CLOSE);
  3464. mutex_unlock(&spec->pcm_mutex);
  3465. return 0;
  3466. }
  3467. static int capture_pcm_open(struct hda_pcm_stream *hinfo,
  3468. struct hda_codec *codec,
  3469. struct snd_pcm_substream *substream)
  3470. {
  3471. call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_OPEN);
  3472. return 0;
  3473. }
  3474. static int capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  3475. struct hda_codec *codec,
  3476. unsigned int stream_tag,
  3477. unsigned int format,
  3478. struct snd_pcm_substream *substream)
  3479. {
  3480. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  3481. call_pcm_capture_hook(hinfo, codec, substream,
  3482. HDA_GEN_PCM_ACT_PREPARE);
  3483. return 0;
  3484. }
  3485. static int capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  3486. struct hda_codec *codec,
  3487. struct snd_pcm_substream *substream)
  3488. {
  3489. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  3490. call_pcm_capture_hook(hinfo, codec, substream,
  3491. HDA_GEN_PCM_ACT_CLEANUP);
  3492. return 0;
  3493. }
  3494. static int capture_pcm_close(struct hda_pcm_stream *hinfo,
  3495. struct hda_codec *codec,
  3496. struct snd_pcm_substream *substream)
  3497. {
  3498. call_pcm_capture_hook(hinfo, codec, substream, HDA_GEN_PCM_ACT_CLOSE);
  3499. return 0;
  3500. }
  3501. static int alt_playback_pcm_open(struct hda_pcm_stream *hinfo,
  3502. struct hda_codec *codec,
  3503. struct snd_pcm_substream *substream)
  3504. {
  3505. struct hda_gen_spec *spec = codec->spec;
  3506. int err = 0;
  3507. mutex_lock(&spec->pcm_mutex);
  3508. if (!spec->indep_hp_enabled)
  3509. err = -EBUSY;
  3510. else
  3511. spec->active_streams |= 1 << STREAM_INDEP_HP;
  3512. call_pcm_playback_hook(hinfo, codec, substream,
  3513. HDA_GEN_PCM_ACT_OPEN);
  3514. mutex_unlock(&spec->pcm_mutex);
  3515. return err;
  3516. }
  3517. static int alt_playback_pcm_close(struct hda_pcm_stream *hinfo,
  3518. struct hda_codec *codec,
  3519. struct snd_pcm_substream *substream)
  3520. {
  3521. struct hda_gen_spec *spec = codec->spec;
  3522. mutex_lock(&spec->pcm_mutex);
  3523. spec->active_streams &= ~(1 << STREAM_INDEP_HP);
  3524. call_pcm_playback_hook(hinfo, codec, substream,
  3525. HDA_GEN_PCM_ACT_CLOSE);
  3526. mutex_unlock(&spec->pcm_mutex);
  3527. return 0;
  3528. }
  3529. static int alt_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  3530. struct hda_codec *codec,
  3531. unsigned int stream_tag,
  3532. unsigned int format,
  3533. struct snd_pcm_substream *substream)
  3534. {
  3535. snd_hda_codec_setup_stream(codec, hinfo->nid, stream_tag, 0, format);
  3536. call_pcm_playback_hook(hinfo, codec, substream,
  3537. HDA_GEN_PCM_ACT_PREPARE);
  3538. return 0;
  3539. }
  3540. static int alt_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  3541. struct hda_codec *codec,
  3542. struct snd_pcm_substream *substream)
  3543. {
  3544. snd_hda_codec_cleanup_stream(codec, hinfo->nid);
  3545. call_pcm_playback_hook(hinfo, codec, substream,
  3546. HDA_GEN_PCM_ACT_CLEANUP);
  3547. return 0;
  3548. }
  3549. /*
  3550. * Digital out
  3551. */
  3552. static int dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  3553. struct hda_codec *codec,
  3554. struct snd_pcm_substream *substream)
  3555. {
  3556. struct hda_gen_spec *spec = codec->spec;
  3557. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  3558. }
  3559. static int dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  3560. struct hda_codec *codec,
  3561. unsigned int stream_tag,
  3562. unsigned int format,
  3563. struct snd_pcm_substream *substream)
  3564. {
  3565. struct hda_gen_spec *spec = codec->spec;
  3566. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  3567. stream_tag, format, substream);
  3568. }
  3569. static int dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  3570. struct hda_codec *codec,
  3571. struct snd_pcm_substream *substream)
  3572. {
  3573. struct hda_gen_spec *spec = codec->spec;
  3574. return snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
  3575. }
  3576. static int dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  3577. struct hda_codec *codec,
  3578. struct snd_pcm_substream *substream)
  3579. {
  3580. struct hda_gen_spec *spec = codec->spec;
  3581. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  3582. }
  3583. /*
  3584. * Analog capture
  3585. */
  3586. #define alt_capture_pcm_open capture_pcm_open
  3587. #define alt_capture_pcm_close capture_pcm_close
  3588. static int alt_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  3589. struct hda_codec *codec,
  3590. unsigned int stream_tag,
  3591. unsigned int format,
  3592. struct snd_pcm_substream *substream)
  3593. {
  3594. struct hda_gen_spec *spec = codec->spec;
  3595. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number + 1],
  3596. stream_tag, 0, format);
  3597. call_pcm_capture_hook(hinfo, codec, substream,
  3598. HDA_GEN_PCM_ACT_PREPARE);
  3599. return 0;
  3600. }
  3601. static int alt_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  3602. struct hda_codec *codec,
  3603. struct snd_pcm_substream *substream)
  3604. {
  3605. struct hda_gen_spec *spec = codec->spec;
  3606. snd_hda_codec_cleanup_stream(codec,
  3607. spec->adc_nids[substream->number + 1]);
  3608. call_pcm_capture_hook(hinfo, codec, substream,
  3609. HDA_GEN_PCM_ACT_CLEANUP);
  3610. return 0;
  3611. }
  3612. /*
  3613. */
  3614. static const struct hda_pcm_stream pcm_analog_playback = {
  3615. .substreams = 1,
  3616. .channels_min = 2,
  3617. .channels_max = 8,
  3618. /* NID is set in build_pcms */
  3619. .ops = {
  3620. .open = playback_pcm_open,
  3621. .close = playback_pcm_close,
  3622. .prepare = playback_pcm_prepare,
  3623. .cleanup = playback_pcm_cleanup
  3624. },
  3625. };
  3626. static const struct hda_pcm_stream pcm_analog_capture = {
  3627. .substreams = 1,
  3628. .channels_min = 2,
  3629. .channels_max = 2,
  3630. /* NID is set in build_pcms */
  3631. .ops = {
  3632. .open = capture_pcm_open,
  3633. .close = capture_pcm_close,
  3634. .prepare = capture_pcm_prepare,
  3635. .cleanup = capture_pcm_cleanup
  3636. },
  3637. };
  3638. static const struct hda_pcm_stream pcm_analog_alt_playback = {
  3639. .substreams = 1,
  3640. .channels_min = 2,
  3641. .channels_max = 2,
  3642. /* NID is set in build_pcms */
  3643. .ops = {
  3644. .open = alt_playback_pcm_open,
  3645. .close = alt_playback_pcm_close,
  3646. .prepare = alt_playback_pcm_prepare,
  3647. .cleanup = alt_playback_pcm_cleanup
  3648. },
  3649. };
  3650. static const struct hda_pcm_stream pcm_analog_alt_capture = {
  3651. .substreams = 2, /* can be overridden */
  3652. .channels_min = 2,
  3653. .channels_max = 2,
  3654. /* NID is set in build_pcms */
  3655. .ops = {
  3656. .open = alt_capture_pcm_open,
  3657. .close = alt_capture_pcm_close,
  3658. .prepare = alt_capture_pcm_prepare,
  3659. .cleanup = alt_capture_pcm_cleanup
  3660. },
  3661. };
  3662. static const struct hda_pcm_stream pcm_digital_playback = {
  3663. .substreams = 1,
  3664. .channels_min = 2,
  3665. .channels_max = 2,
  3666. /* NID is set in build_pcms */
  3667. .ops = {
  3668. .open = dig_playback_pcm_open,
  3669. .close = dig_playback_pcm_close,
  3670. .prepare = dig_playback_pcm_prepare,
  3671. .cleanup = dig_playback_pcm_cleanup
  3672. },
  3673. };
  3674. static const struct hda_pcm_stream pcm_digital_capture = {
  3675. .substreams = 1,
  3676. .channels_min = 2,
  3677. .channels_max = 2,
  3678. /* NID is set in build_pcms */
  3679. };
  3680. /* Used by build_pcms to flag that a PCM has no playback stream */
  3681. static const struct hda_pcm_stream pcm_null_stream = {
  3682. .substreams = 0,
  3683. .channels_min = 0,
  3684. .channels_max = 0,
  3685. };
  3686. /*
  3687. * dynamic changing ADC PCM streams
  3688. */
  3689. static bool dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
  3690. {
  3691. struct hda_gen_spec *spec = codec->spec;
  3692. hda_nid_t new_adc = spec->adc_nids[spec->dyn_adc_idx[cur]];
  3693. if (spec->cur_adc && spec->cur_adc != new_adc) {
  3694. /* stream is running, let's swap the current ADC */
  3695. __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
  3696. spec->cur_adc = new_adc;
  3697. snd_hda_codec_setup_stream(codec, new_adc,
  3698. spec->cur_adc_stream_tag, 0,
  3699. spec->cur_adc_format);
  3700. return true;
  3701. }
  3702. return false;
  3703. }
  3704. /* analog capture with dynamic dual-adc changes */
  3705. static int dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  3706. struct hda_codec *codec,
  3707. unsigned int stream_tag,
  3708. unsigned int format,
  3709. struct snd_pcm_substream *substream)
  3710. {
  3711. struct hda_gen_spec *spec = codec->spec;
  3712. spec->cur_adc = spec->adc_nids[spec->dyn_adc_idx[spec->cur_mux[0]]];
  3713. spec->cur_adc_stream_tag = stream_tag;
  3714. spec->cur_adc_format = format;
  3715. snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
  3716. return 0;
  3717. }
  3718. static int dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  3719. struct hda_codec *codec,
  3720. struct snd_pcm_substream *substream)
  3721. {
  3722. struct hda_gen_spec *spec = codec->spec;
  3723. snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
  3724. spec->cur_adc = 0;
  3725. return 0;
  3726. }
  3727. static const struct hda_pcm_stream dyn_adc_pcm_analog_capture = {
  3728. .substreams = 1,
  3729. .channels_min = 2,
  3730. .channels_max = 2,
  3731. .nid = 0, /* fill later */
  3732. .ops = {
  3733. .prepare = dyn_adc_capture_pcm_prepare,
  3734. .cleanup = dyn_adc_capture_pcm_cleanup
  3735. },
  3736. };
  3737. static void fill_pcm_stream_name(char *str, size_t len, const char *sfx,
  3738. const char *chip_name)
  3739. {
  3740. char *p;
  3741. if (*str)
  3742. return;
  3743. strlcpy(str, chip_name, len);
  3744. /* drop non-alnum chars after a space */
  3745. for (p = strchr(str, ' '); p; p = strchr(p + 1, ' ')) {
  3746. if (!isalnum(p[1])) {
  3747. *p = 0;
  3748. break;
  3749. }
  3750. }
  3751. strlcat(str, sfx, len);
  3752. }
  3753. /* build PCM streams based on the parsed results */
  3754. int snd_hda_gen_build_pcms(struct hda_codec *codec)
  3755. {
  3756. struct hda_gen_spec *spec = codec->spec;
  3757. struct hda_pcm *info = spec->pcm_rec;
  3758. const struct hda_pcm_stream *p;
  3759. bool have_multi_adcs;
  3760. codec->num_pcms = 1;
  3761. codec->pcm_info = info;
  3762. if (spec->no_analog)
  3763. goto skip_analog;
  3764. fill_pcm_stream_name(spec->stream_name_analog,
  3765. sizeof(spec->stream_name_analog),
  3766. " Analog", codec->chip_name);
  3767. info->name = spec->stream_name_analog;
  3768. if (spec->multiout.num_dacs > 0) {
  3769. p = spec->stream_analog_playback;
  3770. if (!p)
  3771. p = &pcm_analog_playback;
  3772. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
  3773. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
  3774. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  3775. spec->multiout.max_channels;
  3776. if (spec->autocfg.line_out_type == AUTO_PIN_SPEAKER_OUT &&
  3777. spec->autocfg.line_outs == 2)
  3778. info->stream[SNDRV_PCM_STREAM_PLAYBACK].chmap =
  3779. snd_pcm_2_1_chmaps;
  3780. }
  3781. if (spec->num_adc_nids) {
  3782. p = spec->stream_analog_capture;
  3783. if (!p) {
  3784. if (spec->dyn_adc_switch)
  3785. p = &dyn_adc_pcm_analog_capture;
  3786. else
  3787. p = &pcm_analog_capture;
  3788. }
  3789. info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
  3790. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
  3791. }
  3792. skip_analog:
  3793. /* SPDIF for stream index #1 */
  3794. if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
  3795. fill_pcm_stream_name(spec->stream_name_digital,
  3796. sizeof(spec->stream_name_digital),
  3797. " Digital", codec->chip_name);
  3798. codec->num_pcms = 2;
  3799. codec->slave_dig_outs = spec->multiout.slave_dig_outs;
  3800. info = spec->pcm_rec + 1;
  3801. info->name = spec->stream_name_digital;
  3802. if (spec->dig_out_type)
  3803. info->pcm_type = spec->dig_out_type;
  3804. else
  3805. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  3806. if (spec->multiout.dig_out_nid) {
  3807. p = spec->stream_digital_playback;
  3808. if (!p)
  3809. p = &pcm_digital_playback;
  3810. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
  3811. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
  3812. }
  3813. if (spec->dig_in_nid) {
  3814. p = spec->stream_digital_capture;
  3815. if (!p)
  3816. p = &pcm_digital_capture;
  3817. info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
  3818. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
  3819. }
  3820. }
  3821. if (spec->no_analog)
  3822. return 0;
  3823. /* If the use of more than one ADC is requested for the current
  3824. * model, configure a second analog capture-only PCM.
  3825. */
  3826. have_multi_adcs = (spec->num_adc_nids > 1) &&
  3827. !spec->dyn_adc_switch && !spec->auto_mic;
  3828. /* Additional Analaog capture for index #2 */
  3829. if (spec->alt_dac_nid || have_multi_adcs) {
  3830. codec->num_pcms = 3;
  3831. info = spec->pcm_rec + 2;
  3832. info->name = spec->stream_name_analog;
  3833. if (spec->alt_dac_nid) {
  3834. p = spec->stream_analog_alt_playback;
  3835. if (!p)
  3836. p = &pcm_analog_alt_playback;
  3837. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *p;
  3838. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
  3839. spec->alt_dac_nid;
  3840. } else {
  3841. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  3842. pcm_null_stream;
  3843. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = 0;
  3844. }
  3845. if (have_multi_adcs) {
  3846. p = spec->stream_analog_alt_capture;
  3847. if (!p)
  3848. p = &pcm_analog_alt_capture;
  3849. info->stream[SNDRV_PCM_STREAM_CAPTURE] = *p;
  3850. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
  3851. spec->adc_nids[1];
  3852. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
  3853. spec->num_adc_nids - 1;
  3854. } else {
  3855. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  3856. pcm_null_stream;
  3857. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = 0;
  3858. }
  3859. }
  3860. return 0;
  3861. }
  3862. EXPORT_SYMBOL_HDA(snd_hda_gen_build_pcms);
  3863. /*
  3864. * Standard auto-parser initializations
  3865. */
  3866. /* configure the given path as a proper output */
  3867. static void set_output_and_unmute(struct hda_codec *codec, int path_idx)
  3868. {
  3869. struct nid_path *path;
  3870. hda_nid_t pin;
  3871. path = snd_hda_get_path_from_idx(codec, path_idx);
  3872. if (!path || !path->depth)
  3873. return;
  3874. pin = path->path[path->depth - 1];
  3875. restore_pin_ctl(codec, pin);
  3876. snd_hda_activate_path(codec, path, path->active, true);
  3877. set_pin_eapd(codec, pin, path->active);
  3878. }
  3879. /* initialize primary output paths */
  3880. static void init_multi_out(struct hda_codec *codec)
  3881. {
  3882. struct hda_gen_spec *spec = codec->spec;
  3883. int i;
  3884. for (i = 0; i < spec->autocfg.line_outs; i++)
  3885. set_output_and_unmute(codec, spec->out_paths[i]);
  3886. }
  3887. static void __init_extra_out(struct hda_codec *codec, int num_outs, int *paths)
  3888. {
  3889. int i;
  3890. for (i = 0; i < num_outs; i++)
  3891. set_output_and_unmute(codec, paths[i]);
  3892. }
  3893. /* initialize hp and speaker paths */
  3894. static void init_extra_out(struct hda_codec *codec)
  3895. {
  3896. struct hda_gen_spec *spec = codec->spec;
  3897. if (spec->autocfg.line_out_type != AUTO_PIN_HP_OUT)
  3898. __init_extra_out(codec, spec->autocfg.hp_outs, spec->hp_paths);
  3899. if (spec->autocfg.line_out_type != AUTO_PIN_SPEAKER_OUT)
  3900. __init_extra_out(codec, spec->autocfg.speaker_outs,
  3901. spec->speaker_paths);
  3902. }
  3903. /* initialize multi-io paths */
  3904. static void init_multi_io(struct hda_codec *codec)
  3905. {
  3906. struct hda_gen_spec *spec = codec->spec;
  3907. int i;
  3908. for (i = 0; i < spec->multi_ios; i++) {
  3909. hda_nid_t pin = spec->multi_io[i].pin;
  3910. struct nid_path *path;
  3911. path = get_multiio_path(codec, i);
  3912. if (!path)
  3913. continue;
  3914. if (!spec->multi_io[i].ctl_in)
  3915. spec->multi_io[i].ctl_in =
  3916. snd_hda_codec_get_pin_target(codec, pin);
  3917. snd_hda_activate_path(codec, path, path->active, true);
  3918. }
  3919. }
  3920. /* set up input pins and loopback paths */
  3921. static void init_analog_input(struct hda_codec *codec)
  3922. {
  3923. struct hda_gen_spec *spec = codec->spec;
  3924. struct auto_pin_cfg *cfg = &spec->autocfg;
  3925. int i;
  3926. for (i = 0; i < cfg->num_inputs; i++) {
  3927. hda_nid_t nid = cfg->inputs[i].pin;
  3928. if (is_input_pin(codec, nid))
  3929. restore_pin_ctl(codec, nid);
  3930. /* init loopback inputs */
  3931. if (spec->mixer_nid) {
  3932. struct nid_path *path;
  3933. path = snd_hda_get_path_from_idx(codec, spec->loopback_paths[i]);
  3934. if (path)
  3935. snd_hda_activate_path(codec, path,
  3936. path->active, false);
  3937. }
  3938. }
  3939. }
  3940. /* initialize ADC paths */
  3941. static void init_input_src(struct hda_codec *codec)
  3942. {
  3943. struct hda_gen_spec *spec = codec->spec;
  3944. struct hda_input_mux *imux = &spec->input_mux;
  3945. struct nid_path *path;
  3946. int i, c, nums;
  3947. if (spec->dyn_adc_switch)
  3948. nums = 1;
  3949. else
  3950. nums = spec->num_adc_nids;
  3951. for (c = 0; c < nums; c++) {
  3952. for (i = 0; i < imux->num_items; i++) {
  3953. path = get_input_path(codec, c, i);
  3954. if (path) {
  3955. bool active = path->active;
  3956. if (i == spec->cur_mux[c])
  3957. active = true;
  3958. snd_hda_activate_path(codec, path, active, false);
  3959. }
  3960. }
  3961. }
  3962. if (spec->shared_mic_hp)
  3963. update_shared_mic_hp(codec, spec->cur_mux[0]);
  3964. if (spec->cap_sync_hook)
  3965. spec->cap_sync_hook(codec);
  3966. }
  3967. /* set right pin controls for digital I/O */
  3968. static void init_digital(struct hda_codec *codec)
  3969. {
  3970. struct hda_gen_spec *spec = codec->spec;
  3971. int i;
  3972. hda_nid_t pin;
  3973. for (i = 0; i < spec->autocfg.dig_outs; i++)
  3974. set_output_and_unmute(codec, spec->digout_paths[i]);
  3975. pin = spec->autocfg.dig_in_pin;
  3976. if (pin) {
  3977. struct nid_path *path;
  3978. restore_pin_ctl(codec, pin);
  3979. path = snd_hda_get_path_from_idx(codec, spec->digin_path);
  3980. if (path)
  3981. snd_hda_activate_path(codec, path, path->active, false);
  3982. }
  3983. }
  3984. /* clear unsol-event tags on unused pins; Conexant codecs seem to leave
  3985. * invalid unsol tags by some reason
  3986. */
  3987. static void clear_unsol_on_unused_pins(struct hda_codec *codec)
  3988. {
  3989. int i;
  3990. for (i = 0; i < codec->init_pins.used; i++) {
  3991. struct hda_pincfg *pin = snd_array_elem(&codec->init_pins, i);
  3992. hda_nid_t nid = pin->nid;
  3993. if (is_jack_detectable(codec, nid) &&
  3994. !snd_hda_jack_tbl_get(codec, nid))
  3995. snd_hda_codec_update_cache(codec, nid, 0,
  3996. AC_VERB_SET_UNSOLICITED_ENABLE, 0);
  3997. }
  3998. }
  3999. /*
  4000. * initialize the generic spec;
  4001. * this can be put as patch_ops.init function
  4002. */
  4003. int snd_hda_gen_init(struct hda_codec *codec)
  4004. {
  4005. struct hda_gen_spec *spec = codec->spec;
  4006. if (spec->init_hook)
  4007. spec->init_hook(codec);
  4008. snd_hda_apply_verbs(codec);
  4009. codec->cached_write = 1;
  4010. init_multi_out(codec);
  4011. init_extra_out(codec);
  4012. init_multi_io(codec);
  4013. init_analog_input(codec);
  4014. init_input_src(codec);
  4015. init_digital(codec);
  4016. clear_unsol_on_unused_pins(codec);
  4017. /* call init functions of standard auto-mute helpers */
  4018. update_automute_all(codec);
  4019. snd_hda_codec_flush_amp_cache(codec);
  4020. snd_hda_codec_flush_cmd_cache(codec);
  4021. if (spec->vmaster_mute.sw_kctl && spec->vmaster_mute.hook)
  4022. snd_hda_sync_vmaster_hook(&spec->vmaster_mute);
  4023. hda_call_check_power_status(codec, 0x01);
  4024. return 0;
  4025. }
  4026. EXPORT_SYMBOL_HDA(snd_hda_gen_init);
  4027. /*
  4028. * free the generic spec;
  4029. * this can be put as patch_ops.free function
  4030. */
  4031. void snd_hda_gen_free(struct hda_codec *codec)
  4032. {
  4033. snd_hda_gen_spec_free(codec->spec);
  4034. kfree(codec->spec);
  4035. codec->spec = NULL;
  4036. }
  4037. EXPORT_SYMBOL_HDA(snd_hda_gen_free);
  4038. #ifdef CONFIG_PM
  4039. /*
  4040. * check the loopback power save state;
  4041. * this can be put as patch_ops.check_power_status function
  4042. */
  4043. int snd_hda_gen_check_power_status(struct hda_codec *codec, hda_nid_t nid)
  4044. {
  4045. struct hda_gen_spec *spec = codec->spec;
  4046. return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
  4047. }
  4048. EXPORT_SYMBOL_HDA(snd_hda_gen_check_power_status);
  4049. #endif
  4050. /*
  4051. * the generic codec support
  4052. */
  4053. static const struct hda_codec_ops generic_patch_ops = {
  4054. .build_controls = snd_hda_gen_build_controls,
  4055. .build_pcms = snd_hda_gen_build_pcms,
  4056. .init = snd_hda_gen_init,
  4057. .free = snd_hda_gen_free,
  4058. .unsol_event = snd_hda_jack_unsol_event,
  4059. #ifdef CONFIG_PM
  4060. .check_power_status = snd_hda_gen_check_power_status,
  4061. #endif
  4062. };
  4063. int snd_hda_parse_generic_codec(struct hda_codec *codec)
  4064. {
  4065. struct hda_gen_spec *spec;
  4066. int err;
  4067. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  4068. if (!spec)
  4069. return -ENOMEM;
  4070. snd_hda_gen_spec_init(spec);
  4071. codec->spec = spec;
  4072. err = snd_hda_parse_pin_defcfg(codec, &spec->autocfg, NULL, 0);
  4073. if (err < 0)
  4074. return err;
  4075. err = snd_hda_gen_parse_auto_config(codec, &spec->autocfg);
  4076. if (err < 0)
  4077. goto error;
  4078. codec->patch_ops = generic_patch_ops;
  4079. return 0;
  4080. error:
  4081. snd_hda_gen_free(codec);
  4082. return err;
  4083. }
  4084. EXPORT_SYMBOL_HDA(snd_hda_parse_generic_codec);