hda_generic.c 158 KB

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