hda_generic.c 144 KB

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