hda_generic.c 140 KB

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