hda_generic.c 97 KB

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