hda_generic.c 144 KB

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