hda_generic.c 137 KB

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