hda_generic.c 100 KB

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