hda_generic.c 107 KB

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