hda_generic.c 135 KB

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