hda_generic.c 106 KB

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