hda_generic.c 158 KB

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