hda_generic.c 120 KB

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