hda_generic.c 97 KB

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