hda_generic.c 109 KB

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