hda_generic.c 116 KB

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