hda_generic.c 161 KB

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