hda_generic.c 130 KB

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