hda_generic.c 144 KB

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