hda_generic.c 94 KB

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