hda_generic.c 106 KB

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