hda_generic.c 165 KB

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