patch_via.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918
  1. /*
  2. * Universal Interface for Intel High Definition Audio Codec
  3. *
  4. * HD audio interface patch for VIA VT17xx/VT18xx/VT20xx codec
  5. *
  6. * (C) 2006-2009 VIA Technology, Inc.
  7. * (C) 2006-2008 Takashi Iwai <tiwai@suse.de>
  8. *
  9. * This driver is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This driver is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. /* * * * * * * * * * * * * * Release History * * * * * * * * * * * * * * * * */
  24. /* */
  25. /* 2006-03-03 Lydia Wang Create the basic patch to support VT1708 codec */
  26. /* 2006-03-14 Lydia Wang Modify hard code for some pin widget nid */
  27. /* 2006-08-02 Lydia Wang Add support to VT1709 codec */
  28. /* 2006-09-08 Lydia Wang Fix internal loopback recording source select bug */
  29. /* 2007-09-12 Lydia Wang Add EAPD enable during driver initialization */
  30. /* 2007-09-17 Lydia Wang Add VT1708B codec support */
  31. /* 2007-11-14 Lydia Wang Add VT1708A codec HP and CD pin connect config */
  32. /* 2008-02-03 Lydia Wang Fix Rear channels and Back channels inverse issue */
  33. /* 2008-03-06 Lydia Wang Add VT1702 codec and VT1708S codec support */
  34. /* 2008-04-09 Lydia Wang Add mute front speaker when HP plugin */
  35. /* 2008-04-09 Lydia Wang Add Independent HP feature */
  36. /* 2008-05-28 Lydia Wang Add second S/PDIF Out support for VT1702 */
  37. /* 2008-09-15 Logan Li Add VT1708S Mic Boost workaround/backdoor */
  38. /* 2009-02-16 Logan Li Add support for VT1718S */
  39. /* 2009-03-13 Logan Li Add support for VT1716S */
  40. /* 2009-04-14 Lydai Wang Add support for VT1828S and VT2020 */
  41. /* 2009-07-08 Lydia Wang Add support for VT2002P */
  42. /* 2009-07-21 Lydia Wang Add support for VT1812 */
  43. /* 2009-09-19 Lydia Wang Add support for VT1818S */
  44. /* */
  45. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  46. #include <linux/init.h>
  47. #include <linux/delay.h>
  48. #include <linux/slab.h>
  49. #include <linux/module.h>
  50. #include <sound/core.h>
  51. #include <sound/asoundef.h>
  52. #include "hda_codec.h"
  53. #include "hda_local.h"
  54. #include "hda_auto_parser.h"
  55. #include "hda_jack.h"
  56. /* Pin Widget NID */
  57. #define VT1708_HP_PIN_NID 0x20
  58. #define VT1708_CD_PIN_NID 0x24
  59. enum VIA_HDA_CODEC {
  60. UNKNOWN = -1,
  61. VT1708,
  62. VT1709_10CH,
  63. VT1709_6CH,
  64. VT1708B_8CH,
  65. VT1708B_4CH,
  66. VT1708S,
  67. VT1708BCE,
  68. VT1702,
  69. VT1718S,
  70. VT1716S,
  71. VT2002P,
  72. VT1812,
  73. VT1802,
  74. CODEC_TYPES,
  75. };
  76. #define VT2002P_COMPATIBLE(spec) \
  77. ((spec)->codec_type == VT2002P ||\
  78. (spec)->codec_type == VT1812 ||\
  79. (spec)->codec_type == VT1802)
  80. #define MAX_NID_PATH_DEPTH 5
  81. /* output-path: DAC -> ... -> pin
  82. * idx[] contains the source index number of the next widget;
  83. * e.g. idx[0] is the index of the DAC selected by path[1] widget
  84. * multi[] indicates whether it's a selector widget with multi-connectors
  85. * (i.e. the connection selection is mandatory)
  86. * vol_ctl and mute_ctl contains the NIDs for the assigned mixers
  87. */
  88. struct nid_path {
  89. int depth;
  90. hda_nid_t path[MAX_NID_PATH_DEPTH];
  91. unsigned char idx[MAX_NID_PATH_DEPTH];
  92. unsigned char multi[MAX_NID_PATH_DEPTH];
  93. unsigned int vol_ctl;
  94. unsigned int mute_ctl;
  95. };
  96. /* input-path */
  97. struct via_input {
  98. hda_nid_t pin; /* input-pin or aa-mix */
  99. int adc_idx; /* ADC index to be used */
  100. int mux_idx; /* MUX index (if any) */
  101. const char *label; /* input-source label */
  102. };
  103. #define VIA_MAX_ADCS 3
  104. enum {
  105. STREAM_MULTI_OUT = (1 << 0),
  106. STREAM_INDEP_HP = (1 << 1),
  107. };
  108. struct via_spec {
  109. /* codec parameterization */
  110. const struct snd_kcontrol_new *mixers[6];
  111. unsigned int num_mixers;
  112. const struct hda_verb *init_verbs[5];
  113. unsigned int num_iverbs;
  114. char stream_name_analog[32];
  115. char stream_name_hp[32];
  116. const struct hda_pcm_stream *stream_analog_playback;
  117. const struct hda_pcm_stream *stream_analog_capture;
  118. char stream_name_digital[32];
  119. const struct hda_pcm_stream *stream_digital_playback;
  120. const struct hda_pcm_stream *stream_digital_capture;
  121. /* playback */
  122. struct hda_multi_out multiout;
  123. hda_nid_t slave_dig_outs[2];
  124. hda_nid_t hp_dac_nid;
  125. hda_nid_t speaker_dac_nid;
  126. int hp_indep_shared; /* indep HP-DAC is shared with side ch */
  127. int opened_streams; /* STREAM_* bits */
  128. int active_streams; /* STREAM_* bits */
  129. int aamix_mode; /* loopback is enabled for output-path? */
  130. /* Output-paths:
  131. * There are different output-paths depending on the setup.
  132. * out_path, hp_path and speaker_path are primary paths. If both
  133. * direct DAC and aa-loopback routes are available, these contain
  134. * the former paths. Meanwhile *_mix_path contain the paths with
  135. * loopback mixer. (Since the loopback is only for front channel,
  136. * no out_mix_path for surround channels.)
  137. * The HP output has another path, hp_indep_path, which is used in
  138. * the independent-HP mode.
  139. */
  140. struct nid_path out_path[HDA_SIDE + 1];
  141. struct nid_path out_mix_path;
  142. struct nid_path hp_path;
  143. struct nid_path hp_mix_path;
  144. struct nid_path hp_indep_path;
  145. struct nid_path speaker_path;
  146. struct nid_path speaker_mix_path;
  147. /* capture */
  148. unsigned int num_adc_nids;
  149. hda_nid_t adc_nids[VIA_MAX_ADCS];
  150. hda_nid_t mux_nids[VIA_MAX_ADCS];
  151. hda_nid_t aa_mix_nid;
  152. hda_nid_t dig_in_nid;
  153. /* capture source */
  154. bool dyn_adc_switch;
  155. int num_inputs;
  156. struct via_input inputs[AUTO_CFG_MAX_INS + 1];
  157. unsigned int cur_mux[VIA_MAX_ADCS];
  158. /* dynamic DAC switching */
  159. unsigned int cur_dac_stream_tag;
  160. unsigned int cur_dac_format;
  161. unsigned int cur_hp_stream_tag;
  162. unsigned int cur_hp_format;
  163. /* dynamic ADC switching */
  164. hda_nid_t cur_adc;
  165. unsigned int cur_adc_stream_tag;
  166. unsigned int cur_adc_format;
  167. /* PCM information */
  168. struct hda_pcm pcm_rec[3];
  169. /* dynamic controls, init_verbs and input_mux */
  170. struct auto_pin_cfg autocfg;
  171. struct snd_array kctls;
  172. hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
  173. /* HP mode source */
  174. unsigned int hp_independent_mode;
  175. unsigned int dmic_enabled;
  176. unsigned int no_pin_power_ctl;
  177. enum VIA_HDA_CODEC codec_type;
  178. /* analog low-power control */
  179. bool alc_mode;
  180. /* smart51 setup */
  181. unsigned int smart51_nums;
  182. hda_nid_t smart51_pins[2];
  183. int smart51_idxs[2];
  184. const char *smart51_labels[2];
  185. unsigned int smart51_enabled;
  186. /* work to check hp jack state */
  187. struct hda_codec *codec;
  188. struct delayed_work vt1708_hp_work;
  189. int hp_work_active;
  190. int vt1708_jack_detect;
  191. int vt1708_hp_present;
  192. void (*set_widgets_power_state)(struct hda_codec *codec);
  193. struct hda_loopback_check loopback;
  194. int num_loopbacks;
  195. struct hda_amp_list loopback_list[8];
  196. /* bind capture-volume */
  197. struct hda_bind_ctls *bind_cap_vol;
  198. struct hda_bind_ctls *bind_cap_sw;
  199. struct mutex config_mutex;
  200. };
  201. static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
  202. static struct via_spec * via_new_spec(struct hda_codec *codec)
  203. {
  204. struct via_spec *spec;
  205. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  206. if (spec == NULL)
  207. return NULL;
  208. mutex_init(&spec->config_mutex);
  209. codec->spec = spec;
  210. spec->codec = codec;
  211. spec->codec_type = get_codec_type(codec);
  212. /* VT1708BCE & VT1708S are almost same */
  213. if (spec->codec_type == VT1708BCE)
  214. spec->codec_type = VT1708S;
  215. return spec;
  216. }
  217. static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
  218. {
  219. u32 vendor_id = codec->vendor_id;
  220. u16 ven_id = vendor_id >> 16;
  221. u16 dev_id = vendor_id & 0xffff;
  222. enum VIA_HDA_CODEC codec_type;
  223. /* get codec type */
  224. if (ven_id != 0x1106)
  225. codec_type = UNKNOWN;
  226. else if (dev_id >= 0x1708 && dev_id <= 0x170b)
  227. codec_type = VT1708;
  228. else if (dev_id >= 0xe710 && dev_id <= 0xe713)
  229. codec_type = VT1709_10CH;
  230. else if (dev_id >= 0xe714 && dev_id <= 0xe717)
  231. codec_type = VT1709_6CH;
  232. else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
  233. codec_type = VT1708B_8CH;
  234. if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
  235. codec_type = VT1708BCE;
  236. } else if (dev_id >= 0xe724 && dev_id <= 0xe727)
  237. codec_type = VT1708B_4CH;
  238. else if ((dev_id & 0xfff) == 0x397
  239. && (dev_id >> 12) < 8)
  240. codec_type = VT1708S;
  241. else if ((dev_id & 0xfff) == 0x398
  242. && (dev_id >> 12) < 8)
  243. codec_type = VT1702;
  244. else if ((dev_id & 0xfff) == 0x428
  245. && (dev_id >> 12) < 8)
  246. codec_type = VT1718S;
  247. else if (dev_id == 0x0433 || dev_id == 0xa721)
  248. codec_type = VT1716S;
  249. else if (dev_id == 0x0441 || dev_id == 0x4441)
  250. codec_type = VT1718S;
  251. else if (dev_id == 0x0438 || dev_id == 0x4438)
  252. codec_type = VT2002P;
  253. else if (dev_id == 0x0448)
  254. codec_type = VT1812;
  255. else if (dev_id == 0x0440)
  256. codec_type = VT1708S;
  257. else if ((dev_id & 0xfff) == 0x446)
  258. codec_type = VT1802;
  259. else
  260. codec_type = UNKNOWN;
  261. return codec_type;
  262. };
  263. #define VIA_JACK_EVENT 0x20
  264. #define VIA_HP_EVENT 0x01
  265. #define VIA_LINE_EVENT 0x03
  266. enum {
  267. VIA_CTL_WIDGET_VOL,
  268. VIA_CTL_WIDGET_MUTE,
  269. VIA_CTL_WIDGET_ANALOG_MUTE,
  270. };
  271. static void analog_low_current_mode(struct hda_codec *codec);
  272. static bool is_aa_path_mute(struct hda_codec *codec);
  273. #define hp_detect_with_aa(codec) \
  274. (snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") == 1 && \
  275. !is_aa_path_mute(codec))
  276. static void vt1708_stop_hp_work(struct via_spec *spec)
  277. {
  278. if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
  279. return;
  280. if (spec->hp_work_active) {
  281. snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 1);
  282. cancel_delayed_work_sync(&spec->vt1708_hp_work);
  283. spec->hp_work_active = 0;
  284. }
  285. }
  286. static void vt1708_update_hp_work(struct via_spec *spec)
  287. {
  288. if (spec->codec_type != VT1708 || spec->autocfg.hp_pins[0] == 0)
  289. return;
  290. if (spec->vt1708_jack_detect &&
  291. (spec->active_streams || hp_detect_with_aa(spec->codec))) {
  292. if (!spec->hp_work_active) {
  293. snd_hda_codec_write(spec->codec, 0x1, 0, 0xf81, 0);
  294. schedule_delayed_work(&spec->vt1708_hp_work,
  295. msecs_to_jiffies(100));
  296. spec->hp_work_active = 1;
  297. }
  298. } else if (!hp_detect_with_aa(spec->codec))
  299. vt1708_stop_hp_work(spec);
  300. }
  301. static void set_widgets_power_state(struct hda_codec *codec)
  302. {
  303. struct via_spec *spec = codec->spec;
  304. if (spec->set_widgets_power_state)
  305. spec->set_widgets_power_state(codec);
  306. }
  307. static int analog_input_switch_put(struct snd_kcontrol *kcontrol,
  308. struct snd_ctl_elem_value *ucontrol)
  309. {
  310. int change = snd_hda_mixer_amp_switch_put(kcontrol, ucontrol);
  311. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  312. set_widgets_power_state(codec);
  313. analog_low_current_mode(snd_kcontrol_chip(kcontrol));
  314. vt1708_update_hp_work(codec->spec);
  315. return change;
  316. }
  317. /* modify .put = snd_hda_mixer_amp_switch_put */
  318. #define ANALOG_INPUT_MUTE \
  319. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  320. .name = NULL, \
  321. .index = 0, \
  322. .info = snd_hda_mixer_amp_switch_info, \
  323. .get = snd_hda_mixer_amp_switch_get, \
  324. .put = analog_input_switch_put, \
  325. .private_value = HDA_COMPOSE_AMP_VAL(0, 3, 0, 0) }
  326. static const struct snd_kcontrol_new via_control_templates[] = {
  327. HDA_CODEC_VOLUME(NULL, 0, 0, 0),
  328. HDA_CODEC_MUTE(NULL, 0, 0, 0),
  329. ANALOG_INPUT_MUTE,
  330. };
  331. /* add dynamic controls */
  332. static struct snd_kcontrol_new *__via_clone_ctl(struct via_spec *spec,
  333. const struct snd_kcontrol_new *tmpl,
  334. const char *name)
  335. {
  336. struct snd_kcontrol_new *knew;
  337. snd_array_init(&spec->kctls, sizeof(*knew), 32);
  338. knew = snd_array_new(&spec->kctls);
  339. if (!knew)
  340. return NULL;
  341. *knew = *tmpl;
  342. if (!name)
  343. name = tmpl->name;
  344. if (name) {
  345. knew->name = kstrdup(name, GFP_KERNEL);
  346. if (!knew->name)
  347. return NULL;
  348. }
  349. return knew;
  350. }
  351. static int __via_add_control(struct via_spec *spec, int type, const char *name,
  352. int idx, unsigned long val)
  353. {
  354. struct snd_kcontrol_new *knew;
  355. knew = __via_clone_ctl(spec, &via_control_templates[type], name);
  356. if (!knew)
  357. return -ENOMEM;
  358. knew->index = idx;
  359. if (get_amp_nid_(val))
  360. knew->subdevice = HDA_SUBDEV_AMP_FLAG;
  361. knew->private_value = val;
  362. return 0;
  363. }
  364. #define via_add_control(spec, type, name, val) \
  365. __via_add_control(spec, type, name, 0, val)
  366. #define via_clone_control(spec, tmpl) __via_clone_ctl(spec, tmpl, NULL)
  367. static void via_free_kctls(struct hda_codec *codec)
  368. {
  369. struct via_spec *spec = codec->spec;
  370. if (spec->kctls.list) {
  371. struct snd_kcontrol_new *kctl = spec->kctls.list;
  372. int i;
  373. for (i = 0; i < spec->kctls.used; i++)
  374. kfree(kctl[i].name);
  375. }
  376. snd_array_free(&spec->kctls);
  377. }
  378. /* create input playback/capture controls for the given pin */
  379. static int via_new_analog_input(struct via_spec *spec, const char *ctlname,
  380. int type_idx, int idx, int mix_nid)
  381. {
  382. char name[32];
  383. int err;
  384. sprintf(name, "%s Playback Volume", ctlname);
  385. err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
  386. HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
  387. if (err < 0)
  388. return err;
  389. sprintf(name, "%s Playback Switch", ctlname);
  390. err = __via_add_control(spec, VIA_CTL_WIDGET_ANALOG_MUTE, name, type_idx,
  391. HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
  392. if (err < 0)
  393. return err;
  394. return 0;
  395. }
  396. #define get_connection_index(codec, mux, nid) \
  397. snd_hda_get_conn_index(codec, mux, nid, 0)
  398. static bool check_amp_caps(struct hda_codec *codec, hda_nid_t nid, int dir,
  399. unsigned int mask)
  400. {
  401. unsigned int caps;
  402. if (!nid)
  403. return false;
  404. caps = get_wcaps(codec, nid);
  405. if (dir == HDA_INPUT)
  406. caps &= AC_WCAP_IN_AMP;
  407. else
  408. caps &= AC_WCAP_OUT_AMP;
  409. if (!caps)
  410. return false;
  411. if (query_amp_caps(codec, nid, dir) & mask)
  412. return true;
  413. return false;
  414. }
  415. #define have_mute(codec, nid, dir) \
  416. check_amp_caps(codec, nid, dir, AC_AMPCAP_MUTE)
  417. /* enable/disable the output-route mixers */
  418. static void activate_output_mix(struct hda_codec *codec, struct nid_path *path,
  419. hda_nid_t mix_nid, int idx, bool enable)
  420. {
  421. int i, num, val;
  422. if (!path)
  423. return;
  424. num = snd_hda_get_num_conns(codec, mix_nid);
  425. for (i = 0; i < num; i++) {
  426. if (i == idx)
  427. val = AMP_IN_UNMUTE(i);
  428. else
  429. val = AMP_IN_MUTE(i);
  430. snd_hda_codec_write(codec, mix_nid, 0,
  431. AC_VERB_SET_AMP_GAIN_MUTE, val);
  432. }
  433. }
  434. /* enable/disable the output-route */
  435. static void activate_output_path(struct hda_codec *codec, struct nid_path *path,
  436. bool enable, bool force)
  437. {
  438. struct via_spec *spec = codec->spec;
  439. int i;
  440. for (i = 0; i < path->depth; i++) {
  441. hda_nid_t src, dst;
  442. int idx = path->idx[i];
  443. src = path->path[i];
  444. if (i < path->depth - 1)
  445. dst = path->path[i + 1];
  446. else
  447. dst = 0;
  448. if (enable && path->multi[i])
  449. snd_hda_codec_write(codec, dst, 0,
  450. AC_VERB_SET_CONNECT_SEL, idx);
  451. if (!force && (dst == spec->aa_mix_nid))
  452. continue;
  453. if (have_mute(codec, dst, HDA_INPUT))
  454. activate_output_mix(codec, path, dst, idx, enable);
  455. if (!force && (src == path->vol_ctl || src == path->mute_ctl))
  456. continue;
  457. if (have_mute(codec, src, HDA_OUTPUT)) {
  458. int val = enable ? AMP_OUT_UNMUTE : AMP_OUT_MUTE;
  459. snd_hda_codec_write(codec, src, 0,
  460. AC_VERB_SET_AMP_GAIN_MUTE, val);
  461. }
  462. }
  463. }
  464. /* set the given pin as output */
  465. static void init_output_pin(struct hda_codec *codec, hda_nid_t pin,
  466. int pin_type)
  467. {
  468. if (!pin)
  469. return;
  470. snd_hda_set_pin_ctl(codec, pin, pin_type);
  471. if (snd_hda_query_pin_caps(codec, pin) & AC_PINCAP_EAPD)
  472. snd_hda_codec_write(codec, pin, 0,
  473. AC_VERB_SET_EAPD_BTLENABLE, 0x02);
  474. }
  475. static void via_auto_init_output(struct hda_codec *codec,
  476. struct nid_path *path, int pin_type)
  477. {
  478. unsigned int caps;
  479. hda_nid_t pin;
  480. if (!path->depth)
  481. return;
  482. pin = path->path[path->depth - 1];
  483. init_output_pin(codec, pin, pin_type);
  484. if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
  485. caps = query_amp_caps(codec, pin, HDA_OUTPUT);
  486. else
  487. caps = 0;
  488. if (caps & AC_AMPCAP_MUTE) {
  489. unsigned int val;
  490. val = (caps & AC_AMPCAP_OFFSET) >> AC_AMPCAP_OFFSET_SHIFT;
  491. snd_hda_codec_write(codec, pin, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  492. AMP_OUT_MUTE | val);
  493. }
  494. activate_output_path(codec, path, true, true); /* force on */
  495. }
  496. static void via_auto_init_multi_out(struct hda_codec *codec)
  497. {
  498. struct via_spec *spec = codec->spec;
  499. struct nid_path *path;
  500. int i;
  501. for (i = 0; i < spec->autocfg.line_outs + spec->smart51_nums; i++) {
  502. path = &spec->out_path[i];
  503. if (!i && spec->aamix_mode && spec->out_mix_path.depth)
  504. path = &spec->out_mix_path;
  505. via_auto_init_output(codec, path, PIN_OUT);
  506. }
  507. }
  508. /* deactivate the inactive headphone-paths */
  509. static void deactivate_hp_paths(struct hda_codec *codec)
  510. {
  511. struct via_spec *spec = codec->spec;
  512. int shared = spec->hp_indep_shared;
  513. if (spec->hp_independent_mode) {
  514. activate_output_path(codec, &spec->hp_path, false, false);
  515. activate_output_path(codec, &spec->hp_mix_path, false, false);
  516. if (shared)
  517. activate_output_path(codec, &spec->out_path[shared],
  518. false, false);
  519. } else if (spec->aamix_mode || !spec->hp_path.depth) {
  520. activate_output_path(codec, &spec->hp_indep_path, false, false);
  521. activate_output_path(codec, &spec->hp_path, false, false);
  522. } else {
  523. activate_output_path(codec, &spec->hp_indep_path, false, false);
  524. activate_output_path(codec, &spec->hp_mix_path, false, false);
  525. }
  526. }
  527. static void via_auto_init_hp_out(struct hda_codec *codec)
  528. {
  529. struct via_spec *spec = codec->spec;
  530. if (!spec->hp_path.depth) {
  531. via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
  532. return;
  533. }
  534. deactivate_hp_paths(codec);
  535. if (spec->hp_independent_mode)
  536. via_auto_init_output(codec, &spec->hp_indep_path, PIN_HP);
  537. else if (spec->aamix_mode)
  538. via_auto_init_output(codec, &spec->hp_mix_path, PIN_HP);
  539. else
  540. via_auto_init_output(codec, &spec->hp_path, PIN_HP);
  541. }
  542. static void via_auto_init_speaker_out(struct hda_codec *codec)
  543. {
  544. struct via_spec *spec = codec->spec;
  545. if (!spec->autocfg.speaker_outs)
  546. return;
  547. if (!spec->speaker_path.depth) {
  548. via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
  549. return;
  550. }
  551. if (!spec->aamix_mode) {
  552. activate_output_path(codec, &spec->speaker_mix_path,
  553. false, false);
  554. via_auto_init_output(codec, &spec->speaker_path, PIN_OUT);
  555. } else {
  556. activate_output_path(codec, &spec->speaker_path, false, false);
  557. via_auto_init_output(codec, &spec->speaker_mix_path, PIN_OUT);
  558. }
  559. }
  560. static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin);
  561. static void via_hp_automute(struct hda_codec *codec);
  562. static void via_auto_init_analog_input(struct hda_codec *codec)
  563. {
  564. struct via_spec *spec = codec->spec;
  565. const struct auto_pin_cfg *cfg = &spec->autocfg;
  566. hda_nid_t conn[HDA_MAX_CONNECTIONS];
  567. unsigned int ctl;
  568. int i, num_conns;
  569. /* init ADCs */
  570. for (i = 0; i < spec->num_adc_nids; i++) {
  571. hda_nid_t nid = spec->adc_nids[i];
  572. if (!(get_wcaps(codec, nid) & AC_WCAP_IN_AMP) ||
  573. !(query_amp_caps(codec, nid, HDA_INPUT) & AC_AMPCAP_MUTE))
  574. continue;
  575. snd_hda_codec_write(codec, spec->adc_nids[i], 0,
  576. AC_VERB_SET_AMP_GAIN_MUTE,
  577. AMP_IN_UNMUTE(0));
  578. }
  579. /* init pins */
  580. for (i = 0; i < cfg->num_inputs; i++) {
  581. hda_nid_t nid = cfg->inputs[i].pin;
  582. if (spec->smart51_enabled && is_smart51_pins(codec, nid))
  583. ctl = PIN_OUT;
  584. else {
  585. ctl = PIN_IN;
  586. if (cfg->inputs[i].type == AUTO_PIN_MIC)
  587. ctl |= snd_hda_get_default_vref(codec, nid);
  588. }
  589. snd_hda_set_pin_ctl(codec, nid, ctl);
  590. }
  591. /* init input-src */
  592. for (i = 0; i < spec->num_adc_nids; i++) {
  593. int adc_idx = spec->inputs[spec->cur_mux[i]].adc_idx;
  594. /* secondary ADCs must have the unique MUX */
  595. if (i > 0 && !spec->mux_nids[i])
  596. break;
  597. if (spec->mux_nids[adc_idx]) {
  598. int mux_idx = spec->inputs[spec->cur_mux[i]].mux_idx;
  599. snd_hda_codec_write(codec, spec->mux_nids[adc_idx], 0,
  600. AC_VERB_SET_CONNECT_SEL,
  601. mux_idx);
  602. }
  603. if (spec->dyn_adc_switch)
  604. break; /* only one input-src */
  605. }
  606. /* init aa-mixer */
  607. if (!spec->aa_mix_nid)
  608. return;
  609. num_conns = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
  610. ARRAY_SIZE(conn));
  611. for (i = 0; i < num_conns; i++) {
  612. unsigned int caps = get_wcaps(codec, conn[i]);
  613. if (get_wcaps_type(caps) == AC_WID_PIN)
  614. snd_hda_codec_write(codec, spec->aa_mix_nid, 0,
  615. AC_VERB_SET_AMP_GAIN_MUTE,
  616. AMP_IN_MUTE(i));
  617. }
  618. }
  619. static void update_power_state(struct hda_codec *codec, hda_nid_t nid,
  620. unsigned int parm)
  621. {
  622. if (snd_hda_codec_read(codec, nid, 0,
  623. AC_VERB_GET_POWER_STATE, 0) == parm)
  624. return;
  625. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_POWER_STATE, parm);
  626. }
  627. static void set_pin_power_state(struct hda_codec *codec, hda_nid_t nid,
  628. unsigned int *affected_parm)
  629. {
  630. unsigned parm;
  631. unsigned def_conf = snd_hda_codec_get_pincfg(codec, nid);
  632. unsigned no_presence = (def_conf & AC_DEFCFG_MISC)
  633. >> AC_DEFCFG_MISC_SHIFT
  634. & AC_DEFCFG_MISC_NO_PRESENCE; /* do not support pin sense */
  635. struct via_spec *spec = codec->spec;
  636. unsigned present = 0;
  637. no_presence |= spec->no_pin_power_ctl;
  638. if (!no_presence)
  639. present = snd_hda_jack_detect(codec, nid);
  640. if ((spec->smart51_enabled && is_smart51_pins(codec, nid))
  641. || ((no_presence || present)
  642. && get_defcfg_connect(def_conf) != AC_JACK_PORT_NONE)) {
  643. *affected_parm = AC_PWRST_D0; /* if it's connected */
  644. parm = AC_PWRST_D0;
  645. } else
  646. parm = AC_PWRST_D3;
  647. update_power_state(codec, nid, parm);
  648. }
  649. static int via_pin_power_ctl_info(struct snd_kcontrol *kcontrol,
  650. struct snd_ctl_elem_info *uinfo)
  651. {
  652. static const char * const texts[] = {
  653. "Disabled", "Enabled"
  654. };
  655. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  656. uinfo->count = 1;
  657. uinfo->value.enumerated.items = 2;
  658. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  659. uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
  660. strcpy(uinfo->value.enumerated.name,
  661. texts[uinfo->value.enumerated.item]);
  662. return 0;
  663. }
  664. static int via_pin_power_ctl_get(struct snd_kcontrol *kcontrol,
  665. struct snd_ctl_elem_value *ucontrol)
  666. {
  667. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  668. struct via_spec *spec = codec->spec;
  669. ucontrol->value.enumerated.item[0] = !spec->no_pin_power_ctl;
  670. return 0;
  671. }
  672. static int via_pin_power_ctl_put(struct snd_kcontrol *kcontrol,
  673. struct snd_ctl_elem_value *ucontrol)
  674. {
  675. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  676. struct via_spec *spec = codec->spec;
  677. unsigned int val = !ucontrol->value.enumerated.item[0];
  678. if (val == spec->no_pin_power_ctl)
  679. return 0;
  680. spec->no_pin_power_ctl = val;
  681. set_widgets_power_state(codec);
  682. analog_low_current_mode(codec);
  683. return 1;
  684. }
  685. static const struct snd_kcontrol_new via_pin_power_ctl_enum = {
  686. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  687. .name = "Dynamic Power-Control",
  688. .info = via_pin_power_ctl_info,
  689. .get = via_pin_power_ctl_get,
  690. .put = via_pin_power_ctl_put,
  691. };
  692. static int via_independent_hp_info(struct snd_kcontrol *kcontrol,
  693. struct snd_ctl_elem_info *uinfo)
  694. {
  695. static const char * const texts[] = { "OFF", "ON" };
  696. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  697. uinfo->count = 1;
  698. uinfo->value.enumerated.items = 2;
  699. if (uinfo->value.enumerated.item >= 2)
  700. uinfo->value.enumerated.item = 1;
  701. strcpy(uinfo->value.enumerated.name,
  702. texts[uinfo->value.enumerated.item]);
  703. return 0;
  704. }
  705. static int via_independent_hp_get(struct snd_kcontrol *kcontrol,
  706. struct snd_ctl_elem_value *ucontrol)
  707. {
  708. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  709. struct via_spec *spec = codec->spec;
  710. ucontrol->value.enumerated.item[0] = spec->hp_independent_mode;
  711. return 0;
  712. }
  713. /* adjust spec->multiout setup according to the current flags */
  714. static void setup_playback_multi_pcm(struct via_spec *spec)
  715. {
  716. const struct auto_pin_cfg *cfg = &spec->autocfg;
  717. spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
  718. spec->multiout.hp_nid = 0;
  719. if (!spec->hp_independent_mode) {
  720. if (!spec->hp_indep_shared)
  721. spec->multiout.hp_nid = spec->hp_dac_nid;
  722. } else {
  723. if (spec->hp_indep_shared)
  724. spec->multiout.num_dacs = cfg->line_outs - 1;
  725. }
  726. }
  727. /* update DAC setups according to indep-HP switch;
  728. * this function is called only when indep-HP is modified
  729. */
  730. static void switch_indep_hp_dacs(struct hda_codec *codec)
  731. {
  732. struct via_spec *spec = codec->spec;
  733. int shared = spec->hp_indep_shared;
  734. hda_nid_t shared_dac, hp_dac;
  735. if (!spec->opened_streams)
  736. return;
  737. shared_dac = shared ? spec->multiout.dac_nids[shared] : 0;
  738. hp_dac = spec->hp_dac_nid;
  739. if (spec->hp_independent_mode) {
  740. /* switch to indep-HP mode */
  741. if (spec->active_streams & STREAM_MULTI_OUT) {
  742. __snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
  743. __snd_hda_codec_cleanup_stream(codec, shared_dac, 1);
  744. }
  745. if (spec->active_streams & STREAM_INDEP_HP)
  746. snd_hda_codec_setup_stream(codec, hp_dac,
  747. spec->cur_hp_stream_tag, 0,
  748. spec->cur_hp_format);
  749. } else {
  750. /* back to HP or shared-DAC */
  751. if (spec->active_streams & STREAM_INDEP_HP)
  752. __snd_hda_codec_cleanup_stream(codec, hp_dac, 1);
  753. if (spec->active_streams & STREAM_MULTI_OUT) {
  754. hda_nid_t dac;
  755. int ch;
  756. if (shared_dac) { /* reset mutli-ch DAC */
  757. dac = shared_dac;
  758. ch = shared * 2;
  759. } else { /* reset HP DAC */
  760. dac = hp_dac;
  761. ch = 0;
  762. }
  763. snd_hda_codec_setup_stream(codec, dac,
  764. spec->cur_dac_stream_tag, ch,
  765. spec->cur_dac_format);
  766. }
  767. }
  768. setup_playback_multi_pcm(spec);
  769. }
  770. static int via_independent_hp_put(struct snd_kcontrol *kcontrol,
  771. struct snd_ctl_elem_value *ucontrol)
  772. {
  773. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  774. struct via_spec *spec = codec->spec;
  775. int cur, shared;
  776. mutex_lock(&spec->config_mutex);
  777. cur = !!ucontrol->value.enumerated.item[0];
  778. if (spec->hp_independent_mode == cur) {
  779. mutex_unlock(&spec->config_mutex);
  780. return 0;
  781. }
  782. spec->hp_independent_mode = cur;
  783. shared = spec->hp_indep_shared;
  784. deactivate_hp_paths(codec);
  785. if (cur)
  786. activate_output_path(codec, &spec->hp_indep_path, true, false);
  787. else {
  788. if (shared)
  789. activate_output_path(codec, &spec->out_path[shared],
  790. true, false);
  791. if (spec->aamix_mode || !spec->hp_path.depth)
  792. activate_output_path(codec, &spec->hp_mix_path,
  793. true, false);
  794. else
  795. activate_output_path(codec, &spec->hp_path,
  796. true, false);
  797. }
  798. switch_indep_hp_dacs(codec);
  799. mutex_unlock(&spec->config_mutex);
  800. /* update jack power state */
  801. set_widgets_power_state(codec);
  802. via_hp_automute(codec);
  803. return 1;
  804. }
  805. static const struct snd_kcontrol_new via_hp_mixer = {
  806. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  807. .name = "Independent HP",
  808. .info = via_independent_hp_info,
  809. .get = via_independent_hp_get,
  810. .put = via_independent_hp_put,
  811. };
  812. static int via_hp_build(struct hda_codec *codec)
  813. {
  814. struct via_spec *spec = codec->spec;
  815. struct snd_kcontrol_new *knew;
  816. hda_nid_t nid;
  817. nid = spec->autocfg.hp_pins[0];
  818. knew = via_clone_control(spec, &via_hp_mixer);
  819. if (knew == NULL)
  820. return -ENOMEM;
  821. knew->subdevice = HDA_SUBDEV_NID_FLAG | nid;
  822. return 0;
  823. }
  824. static void notify_aa_path_ctls(struct hda_codec *codec)
  825. {
  826. struct via_spec *spec = codec->spec;
  827. int i;
  828. for (i = 0; i < spec->smart51_nums; i++) {
  829. struct snd_kcontrol *ctl;
  830. struct snd_ctl_elem_id id;
  831. memset(&id, 0, sizeof(id));
  832. id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
  833. sprintf(id.name, "%s Playback Volume", spec->smart51_labels[i]);
  834. ctl = snd_hda_find_mixer_ctl(codec, id.name);
  835. if (ctl)
  836. snd_ctl_notify(codec->bus->card,
  837. SNDRV_CTL_EVENT_MASK_VALUE,
  838. &ctl->id);
  839. }
  840. }
  841. static void mute_aa_path(struct hda_codec *codec, int mute)
  842. {
  843. struct via_spec *spec = codec->spec;
  844. int val = mute ? HDA_AMP_MUTE : HDA_AMP_UNMUTE;
  845. int i;
  846. /* check AA path's mute status */
  847. for (i = 0; i < spec->smart51_nums; i++) {
  848. if (spec->smart51_idxs[i] < 0)
  849. continue;
  850. snd_hda_codec_amp_stereo(codec, spec->aa_mix_nid,
  851. HDA_INPUT, spec->smart51_idxs[i],
  852. HDA_AMP_MUTE, val);
  853. }
  854. }
  855. static bool is_smart51_pins(struct hda_codec *codec, hda_nid_t pin)
  856. {
  857. struct via_spec *spec = codec->spec;
  858. int i;
  859. for (i = 0; i < spec->smart51_nums; i++)
  860. if (spec->smart51_pins[i] == pin)
  861. return true;
  862. return false;
  863. }
  864. static int via_smart51_get(struct snd_kcontrol *kcontrol,
  865. struct snd_ctl_elem_value *ucontrol)
  866. {
  867. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  868. struct via_spec *spec = codec->spec;
  869. *ucontrol->value.integer.value = spec->smart51_enabled;
  870. return 0;
  871. }
  872. static int via_smart51_put(struct snd_kcontrol *kcontrol,
  873. struct snd_ctl_elem_value *ucontrol)
  874. {
  875. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  876. struct via_spec *spec = codec->spec;
  877. int out_in = *ucontrol->value.integer.value
  878. ? AC_PINCTL_OUT_EN : AC_PINCTL_IN_EN;
  879. int i;
  880. for (i = 0; i < spec->smart51_nums; i++) {
  881. hda_nid_t nid = spec->smart51_pins[i];
  882. unsigned int parm;
  883. parm = snd_hda_codec_read(codec, nid, 0,
  884. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  885. parm &= ~(AC_PINCTL_IN_EN | AC_PINCTL_OUT_EN);
  886. parm |= out_in;
  887. snd_hda_set_pin_ctl(codec, nid, parm);
  888. if (out_in == AC_PINCTL_OUT_EN) {
  889. mute_aa_path(codec, 1);
  890. notify_aa_path_ctls(codec);
  891. }
  892. }
  893. spec->smart51_enabled = *ucontrol->value.integer.value;
  894. set_widgets_power_state(codec);
  895. return 1;
  896. }
  897. static const struct snd_kcontrol_new via_smart51_mixer = {
  898. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  899. .name = "Smart 5.1",
  900. .count = 1,
  901. .info = snd_ctl_boolean_mono_info,
  902. .get = via_smart51_get,
  903. .put = via_smart51_put,
  904. };
  905. static int via_smart51_build(struct hda_codec *codec)
  906. {
  907. struct via_spec *spec = codec->spec;
  908. if (!spec->smart51_nums)
  909. return 0;
  910. if (!via_clone_control(spec, &via_smart51_mixer))
  911. return -ENOMEM;
  912. return 0;
  913. }
  914. /* check AA path's mute status */
  915. static bool is_aa_path_mute(struct hda_codec *codec)
  916. {
  917. struct via_spec *spec = codec->spec;
  918. const struct hda_amp_list *p;
  919. int i, ch, v;
  920. for (i = 0; i < spec->num_loopbacks; i++) {
  921. p = &spec->loopback_list[i];
  922. for (ch = 0; ch < 2; ch++) {
  923. v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
  924. p->idx);
  925. if (!(v & HDA_AMP_MUTE) && v > 0)
  926. return false;
  927. }
  928. }
  929. return true;
  930. }
  931. /* enter/exit analog low-current mode */
  932. static void __analog_low_current_mode(struct hda_codec *codec, bool force)
  933. {
  934. struct via_spec *spec = codec->spec;
  935. bool enable;
  936. unsigned int verb, parm;
  937. if (spec->no_pin_power_ctl)
  938. enable = false;
  939. else
  940. enable = is_aa_path_mute(codec) && !spec->opened_streams;
  941. if (enable == spec->alc_mode && !force)
  942. return;
  943. spec->alc_mode = enable;
  944. /* decide low current mode's verb & parameter */
  945. switch (spec->codec_type) {
  946. case VT1708B_8CH:
  947. case VT1708B_4CH:
  948. verb = 0xf70;
  949. parm = enable ? 0x02 : 0x00; /* 0x02: 2/3x, 0x00: 1x */
  950. break;
  951. case VT1708S:
  952. case VT1718S:
  953. case VT1716S:
  954. verb = 0xf73;
  955. parm = enable ? 0x51 : 0xe1; /* 0x51: 4/28x, 0xe1: 1x */
  956. break;
  957. case VT1702:
  958. verb = 0xf73;
  959. parm = enable ? 0x01 : 0x1d; /* 0x01: 4/40x, 0x1d: 1x */
  960. break;
  961. case VT2002P:
  962. case VT1812:
  963. case VT1802:
  964. verb = 0xf93;
  965. parm = enable ? 0x00 : 0xe0; /* 0x00: 4/40x, 0xe0: 1x */
  966. break;
  967. default:
  968. return; /* other codecs are not supported */
  969. }
  970. /* send verb */
  971. snd_hda_codec_write(codec, codec->afg, 0, verb, parm);
  972. }
  973. static void analog_low_current_mode(struct hda_codec *codec)
  974. {
  975. return __analog_low_current_mode(codec, false);
  976. }
  977. /*
  978. * generic initialization of ADC, input mixers and output mixers
  979. */
  980. static const struct hda_verb vt1708_init_verbs[] = {
  981. /* power down jack detect function */
  982. {0x1, 0xf81, 0x1},
  983. { }
  984. };
  985. static void set_stream_open(struct hda_codec *codec, int bit, bool active)
  986. {
  987. struct via_spec *spec = codec->spec;
  988. if (active)
  989. spec->opened_streams |= bit;
  990. else
  991. spec->opened_streams &= ~bit;
  992. analog_low_current_mode(codec);
  993. }
  994. static int via_playback_multi_pcm_open(struct hda_pcm_stream *hinfo,
  995. struct hda_codec *codec,
  996. struct snd_pcm_substream *substream)
  997. {
  998. struct via_spec *spec = codec->spec;
  999. const struct auto_pin_cfg *cfg = &spec->autocfg;
  1000. int err;
  1001. spec->multiout.num_dacs = cfg->line_outs + spec->smart51_nums;
  1002. spec->multiout.max_channels = spec->multiout.num_dacs * 2;
  1003. set_stream_open(codec, STREAM_MULTI_OUT, true);
  1004. err = snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
  1005. hinfo);
  1006. if (err < 0) {
  1007. set_stream_open(codec, STREAM_MULTI_OUT, false);
  1008. return err;
  1009. }
  1010. return 0;
  1011. }
  1012. static int via_playback_multi_pcm_close(struct hda_pcm_stream *hinfo,
  1013. struct hda_codec *codec,
  1014. struct snd_pcm_substream *substream)
  1015. {
  1016. set_stream_open(codec, STREAM_MULTI_OUT, false);
  1017. return 0;
  1018. }
  1019. static int via_playback_hp_pcm_open(struct hda_pcm_stream *hinfo,
  1020. struct hda_codec *codec,
  1021. struct snd_pcm_substream *substream)
  1022. {
  1023. struct via_spec *spec = codec->spec;
  1024. if (snd_BUG_ON(!spec->hp_dac_nid))
  1025. return -EINVAL;
  1026. set_stream_open(codec, STREAM_INDEP_HP, true);
  1027. return 0;
  1028. }
  1029. static int via_playback_hp_pcm_close(struct hda_pcm_stream *hinfo,
  1030. struct hda_codec *codec,
  1031. struct snd_pcm_substream *substream)
  1032. {
  1033. set_stream_open(codec, STREAM_INDEP_HP, false);
  1034. return 0;
  1035. }
  1036. static int via_playback_multi_pcm_prepare(struct hda_pcm_stream *hinfo,
  1037. struct hda_codec *codec,
  1038. unsigned int stream_tag,
  1039. unsigned int format,
  1040. struct snd_pcm_substream *substream)
  1041. {
  1042. struct via_spec *spec = codec->spec;
  1043. mutex_lock(&spec->config_mutex);
  1044. setup_playback_multi_pcm(spec);
  1045. snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
  1046. format, substream);
  1047. /* remember for dynamic DAC switch with indep-HP */
  1048. spec->active_streams |= STREAM_MULTI_OUT;
  1049. spec->cur_dac_stream_tag = stream_tag;
  1050. spec->cur_dac_format = format;
  1051. mutex_unlock(&spec->config_mutex);
  1052. vt1708_update_hp_work(spec);
  1053. return 0;
  1054. }
  1055. static int via_playback_hp_pcm_prepare(struct hda_pcm_stream *hinfo,
  1056. struct hda_codec *codec,
  1057. unsigned int stream_tag,
  1058. unsigned int format,
  1059. struct snd_pcm_substream *substream)
  1060. {
  1061. struct via_spec *spec = codec->spec;
  1062. mutex_lock(&spec->config_mutex);
  1063. if (spec->hp_independent_mode)
  1064. snd_hda_codec_setup_stream(codec, spec->hp_dac_nid,
  1065. stream_tag, 0, format);
  1066. spec->active_streams |= STREAM_INDEP_HP;
  1067. spec->cur_hp_stream_tag = stream_tag;
  1068. spec->cur_hp_format = format;
  1069. mutex_unlock(&spec->config_mutex);
  1070. vt1708_update_hp_work(spec);
  1071. return 0;
  1072. }
  1073. static int via_playback_multi_pcm_cleanup(struct hda_pcm_stream *hinfo,
  1074. struct hda_codec *codec,
  1075. struct snd_pcm_substream *substream)
  1076. {
  1077. struct via_spec *spec = codec->spec;
  1078. mutex_lock(&spec->config_mutex);
  1079. snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  1080. spec->active_streams &= ~STREAM_MULTI_OUT;
  1081. mutex_unlock(&spec->config_mutex);
  1082. vt1708_update_hp_work(spec);
  1083. return 0;
  1084. }
  1085. static int via_playback_hp_pcm_cleanup(struct hda_pcm_stream *hinfo,
  1086. struct hda_codec *codec,
  1087. struct snd_pcm_substream *substream)
  1088. {
  1089. struct via_spec *spec = codec->spec;
  1090. mutex_lock(&spec->config_mutex);
  1091. if (spec->hp_independent_mode)
  1092. snd_hda_codec_setup_stream(codec, spec->hp_dac_nid, 0, 0, 0);
  1093. spec->active_streams &= ~STREAM_INDEP_HP;
  1094. mutex_unlock(&spec->config_mutex);
  1095. vt1708_update_hp_work(spec);
  1096. return 0;
  1097. }
  1098. /*
  1099. * Digital out
  1100. */
  1101. static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  1102. struct hda_codec *codec,
  1103. struct snd_pcm_substream *substream)
  1104. {
  1105. struct via_spec *spec = codec->spec;
  1106. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  1107. }
  1108. static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  1109. struct hda_codec *codec,
  1110. struct snd_pcm_substream *substream)
  1111. {
  1112. struct via_spec *spec = codec->spec;
  1113. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  1114. }
  1115. static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  1116. struct hda_codec *codec,
  1117. unsigned int stream_tag,
  1118. unsigned int format,
  1119. struct snd_pcm_substream *substream)
  1120. {
  1121. struct via_spec *spec = codec->spec;
  1122. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  1123. stream_tag, format, substream);
  1124. }
  1125. static int via_dig_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  1126. struct hda_codec *codec,
  1127. struct snd_pcm_substream *substream)
  1128. {
  1129. struct via_spec *spec = codec->spec;
  1130. snd_hda_multi_out_dig_cleanup(codec, &spec->multiout);
  1131. return 0;
  1132. }
  1133. /*
  1134. * Analog capture
  1135. */
  1136. static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  1137. struct hda_codec *codec,
  1138. unsigned int stream_tag,
  1139. unsigned int format,
  1140. struct snd_pcm_substream *substream)
  1141. {
  1142. struct via_spec *spec = codec->spec;
  1143. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
  1144. stream_tag, 0, format);
  1145. return 0;
  1146. }
  1147. static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  1148. struct hda_codec *codec,
  1149. struct snd_pcm_substream *substream)
  1150. {
  1151. struct via_spec *spec = codec->spec;
  1152. snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
  1153. return 0;
  1154. }
  1155. /* analog capture with dynamic ADC switching */
  1156. static int via_dyn_adc_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  1157. struct hda_codec *codec,
  1158. unsigned int stream_tag,
  1159. unsigned int format,
  1160. struct snd_pcm_substream *substream)
  1161. {
  1162. struct via_spec *spec = codec->spec;
  1163. int adc_idx = spec->inputs[spec->cur_mux[0]].adc_idx;
  1164. mutex_lock(&spec->config_mutex);
  1165. spec->cur_adc = spec->adc_nids[adc_idx];
  1166. spec->cur_adc_stream_tag = stream_tag;
  1167. spec->cur_adc_format = format;
  1168. snd_hda_codec_setup_stream(codec, spec->cur_adc, stream_tag, 0, format);
  1169. mutex_unlock(&spec->config_mutex);
  1170. return 0;
  1171. }
  1172. static int via_dyn_adc_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  1173. struct hda_codec *codec,
  1174. struct snd_pcm_substream *substream)
  1175. {
  1176. struct via_spec *spec = codec->spec;
  1177. mutex_lock(&spec->config_mutex);
  1178. snd_hda_codec_cleanup_stream(codec, spec->cur_adc);
  1179. spec->cur_adc = 0;
  1180. mutex_unlock(&spec->config_mutex);
  1181. return 0;
  1182. }
  1183. /* re-setup the stream if running; called from input-src put */
  1184. static bool via_dyn_adc_pcm_resetup(struct hda_codec *codec, int cur)
  1185. {
  1186. struct via_spec *spec = codec->spec;
  1187. int adc_idx = spec->inputs[cur].adc_idx;
  1188. hda_nid_t adc = spec->adc_nids[adc_idx];
  1189. bool ret = false;
  1190. mutex_lock(&spec->config_mutex);
  1191. if (spec->cur_adc && spec->cur_adc != adc) {
  1192. /* stream is running, let's swap the current ADC */
  1193. __snd_hda_codec_cleanup_stream(codec, spec->cur_adc, 1);
  1194. spec->cur_adc = adc;
  1195. snd_hda_codec_setup_stream(codec, adc,
  1196. spec->cur_adc_stream_tag, 0,
  1197. spec->cur_adc_format);
  1198. ret = true;
  1199. }
  1200. mutex_unlock(&spec->config_mutex);
  1201. return ret;
  1202. }
  1203. static const struct hda_pcm_stream via_pcm_analog_playback = {
  1204. .substreams = 1,
  1205. .channels_min = 2,
  1206. .channels_max = 8,
  1207. /* NID is set in via_build_pcms */
  1208. .ops = {
  1209. .open = via_playback_multi_pcm_open,
  1210. .close = via_playback_multi_pcm_close,
  1211. .prepare = via_playback_multi_pcm_prepare,
  1212. .cleanup = via_playback_multi_pcm_cleanup
  1213. },
  1214. };
  1215. static const struct hda_pcm_stream via_pcm_hp_playback = {
  1216. .substreams = 1,
  1217. .channels_min = 2,
  1218. .channels_max = 2,
  1219. /* NID is set in via_build_pcms */
  1220. .ops = {
  1221. .open = via_playback_hp_pcm_open,
  1222. .close = via_playback_hp_pcm_close,
  1223. .prepare = via_playback_hp_pcm_prepare,
  1224. .cleanup = via_playback_hp_pcm_cleanup
  1225. },
  1226. };
  1227. static const struct hda_pcm_stream vt1708_pcm_analog_s16_playback = {
  1228. .substreams = 1,
  1229. .channels_min = 2,
  1230. .channels_max = 8,
  1231. /* NID is set in via_build_pcms */
  1232. /* We got noisy outputs on the right channel on VT1708 when
  1233. * 24bit samples are used. Until any workaround is found,
  1234. * disable the 24bit format, so far.
  1235. */
  1236. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  1237. .ops = {
  1238. .open = via_playback_multi_pcm_open,
  1239. .close = via_playback_multi_pcm_close,
  1240. .prepare = via_playback_multi_pcm_prepare,
  1241. .cleanup = via_playback_multi_pcm_cleanup
  1242. },
  1243. };
  1244. static const struct hda_pcm_stream via_pcm_analog_capture = {
  1245. .substreams = 1, /* will be changed in via_build_pcms() */
  1246. .channels_min = 2,
  1247. .channels_max = 2,
  1248. /* NID is set in via_build_pcms */
  1249. .ops = {
  1250. .prepare = via_capture_pcm_prepare,
  1251. .cleanup = via_capture_pcm_cleanup
  1252. },
  1253. };
  1254. static const struct hda_pcm_stream via_pcm_dyn_adc_analog_capture = {
  1255. .substreams = 1,
  1256. .channels_min = 2,
  1257. .channels_max = 2,
  1258. /* NID is set in via_build_pcms */
  1259. .ops = {
  1260. .prepare = via_dyn_adc_capture_pcm_prepare,
  1261. .cleanup = via_dyn_adc_capture_pcm_cleanup,
  1262. },
  1263. };
  1264. static const struct hda_pcm_stream via_pcm_digital_playback = {
  1265. .substreams = 1,
  1266. .channels_min = 2,
  1267. .channels_max = 2,
  1268. /* NID is set in via_build_pcms */
  1269. .ops = {
  1270. .open = via_dig_playback_pcm_open,
  1271. .close = via_dig_playback_pcm_close,
  1272. .prepare = via_dig_playback_pcm_prepare,
  1273. .cleanup = via_dig_playback_pcm_cleanup
  1274. },
  1275. };
  1276. static const struct hda_pcm_stream via_pcm_digital_capture = {
  1277. .substreams = 1,
  1278. .channels_min = 2,
  1279. .channels_max = 2,
  1280. };
  1281. /*
  1282. * slave controls for virtual master
  1283. */
  1284. static const char * const via_slave_pfxs[] = {
  1285. "Front", "Surround", "Center", "LFE", "Side",
  1286. "Headphone", "Speaker",
  1287. NULL,
  1288. };
  1289. static int via_build_controls(struct hda_codec *codec)
  1290. {
  1291. struct via_spec *spec = codec->spec;
  1292. struct snd_kcontrol *kctl;
  1293. int err, i;
  1294. spec->no_pin_power_ctl = 1;
  1295. if (spec->set_widgets_power_state)
  1296. if (!via_clone_control(spec, &via_pin_power_ctl_enum))
  1297. return -ENOMEM;
  1298. for (i = 0; i < spec->num_mixers; i++) {
  1299. err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
  1300. if (err < 0)
  1301. return err;
  1302. }
  1303. if (spec->multiout.dig_out_nid) {
  1304. err = snd_hda_create_spdif_out_ctls(codec,
  1305. spec->multiout.dig_out_nid,
  1306. spec->multiout.dig_out_nid);
  1307. if (err < 0)
  1308. return err;
  1309. err = snd_hda_create_spdif_share_sw(codec,
  1310. &spec->multiout);
  1311. if (err < 0)
  1312. return err;
  1313. spec->multiout.share_spdif = 1;
  1314. }
  1315. if (spec->dig_in_nid) {
  1316. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
  1317. if (err < 0)
  1318. return err;
  1319. }
  1320. /* if we have no master control, let's create it */
  1321. if (!snd_hda_find_mixer_ctl(codec, "Master Playback Volume")) {
  1322. unsigned int vmaster_tlv[4];
  1323. snd_hda_set_vmaster_tlv(codec, spec->multiout.dac_nids[0],
  1324. HDA_OUTPUT, vmaster_tlv);
  1325. err = snd_hda_add_vmaster(codec, "Master Playback Volume",
  1326. vmaster_tlv, via_slave_pfxs,
  1327. "Playback Volume");
  1328. if (err < 0)
  1329. return err;
  1330. }
  1331. if (!snd_hda_find_mixer_ctl(codec, "Master Playback Switch")) {
  1332. err = snd_hda_add_vmaster(codec, "Master Playback Switch",
  1333. NULL, via_slave_pfxs,
  1334. "Playback Switch");
  1335. if (err < 0)
  1336. return err;
  1337. }
  1338. /* assign Capture Source enums to NID */
  1339. kctl = snd_hda_find_mixer_ctl(codec, "Input Source");
  1340. for (i = 0; kctl && i < kctl->count; i++) {
  1341. if (!spec->mux_nids[i])
  1342. continue;
  1343. err = snd_hda_add_nid(codec, kctl, i, spec->mux_nids[i]);
  1344. if (err < 0)
  1345. return err;
  1346. }
  1347. via_free_kctls(codec); /* no longer needed */
  1348. err = snd_hda_jack_add_kctls(codec, &spec->autocfg);
  1349. if (err < 0)
  1350. return err;
  1351. return 0;
  1352. }
  1353. static int via_build_pcms(struct hda_codec *codec)
  1354. {
  1355. struct via_spec *spec = codec->spec;
  1356. struct hda_pcm *info = spec->pcm_rec;
  1357. codec->num_pcms = 0;
  1358. codec->pcm_info = info;
  1359. if (spec->multiout.num_dacs || spec->num_adc_nids) {
  1360. snprintf(spec->stream_name_analog,
  1361. sizeof(spec->stream_name_analog),
  1362. "%s Analog", codec->chip_name);
  1363. info->name = spec->stream_name_analog;
  1364. if (spec->multiout.num_dacs) {
  1365. if (!spec->stream_analog_playback)
  1366. spec->stream_analog_playback =
  1367. &via_pcm_analog_playback;
  1368. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  1369. *spec->stream_analog_playback;
  1370. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
  1371. spec->multiout.dac_nids[0];
  1372. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  1373. spec->multiout.max_channels;
  1374. }
  1375. if (!spec->stream_analog_capture) {
  1376. if (spec->dyn_adc_switch)
  1377. spec->stream_analog_capture =
  1378. &via_pcm_dyn_adc_analog_capture;
  1379. else
  1380. spec->stream_analog_capture =
  1381. &via_pcm_analog_capture;
  1382. }
  1383. if (spec->num_adc_nids) {
  1384. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  1385. *spec->stream_analog_capture;
  1386. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
  1387. spec->adc_nids[0];
  1388. if (!spec->dyn_adc_switch)
  1389. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams =
  1390. spec->num_adc_nids;
  1391. }
  1392. codec->num_pcms++;
  1393. info++;
  1394. }
  1395. if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
  1396. snprintf(spec->stream_name_digital,
  1397. sizeof(spec->stream_name_digital),
  1398. "%s Digital", codec->chip_name);
  1399. info->name = spec->stream_name_digital;
  1400. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  1401. if (spec->multiout.dig_out_nid) {
  1402. if (!spec->stream_digital_playback)
  1403. spec->stream_digital_playback =
  1404. &via_pcm_digital_playback;
  1405. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  1406. *spec->stream_digital_playback;
  1407. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
  1408. spec->multiout.dig_out_nid;
  1409. }
  1410. if (spec->dig_in_nid) {
  1411. if (!spec->stream_digital_capture)
  1412. spec->stream_digital_capture =
  1413. &via_pcm_digital_capture;
  1414. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  1415. *spec->stream_digital_capture;
  1416. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
  1417. spec->dig_in_nid;
  1418. }
  1419. codec->num_pcms++;
  1420. info++;
  1421. }
  1422. if (spec->hp_dac_nid) {
  1423. snprintf(spec->stream_name_hp, sizeof(spec->stream_name_hp),
  1424. "%s HP", codec->chip_name);
  1425. info->name = spec->stream_name_hp;
  1426. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = via_pcm_hp_playback;
  1427. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
  1428. spec->hp_dac_nid;
  1429. codec->num_pcms++;
  1430. info++;
  1431. }
  1432. return 0;
  1433. }
  1434. static void via_free(struct hda_codec *codec)
  1435. {
  1436. struct via_spec *spec = codec->spec;
  1437. if (!spec)
  1438. return;
  1439. via_free_kctls(codec);
  1440. vt1708_stop_hp_work(spec);
  1441. kfree(spec->bind_cap_vol);
  1442. kfree(spec->bind_cap_sw);
  1443. kfree(spec);
  1444. }
  1445. /* mute/unmute outputs */
  1446. static void toggle_output_mutes(struct hda_codec *codec, int num_pins,
  1447. hda_nid_t *pins, bool mute)
  1448. {
  1449. int i;
  1450. for (i = 0; i < num_pins; i++) {
  1451. unsigned int parm = snd_hda_codec_read(codec, pins[i], 0,
  1452. AC_VERB_GET_PIN_WIDGET_CONTROL, 0);
  1453. if (parm & AC_PINCTL_IN_EN)
  1454. continue;
  1455. if (mute)
  1456. parm &= ~AC_PINCTL_OUT_EN;
  1457. else
  1458. parm |= AC_PINCTL_OUT_EN;
  1459. snd_hda_set_pin_ctl(codec, pins[i], parm);
  1460. }
  1461. }
  1462. /* mute internal speaker if line-out is plugged */
  1463. static void via_line_automute(struct hda_codec *codec, int present)
  1464. {
  1465. struct via_spec *spec = codec->spec;
  1466. if (!spec->autocfg.speaker_outs)
  1467. return;
  1468. if (!present)
  1469. present = snd_hda_jack_detect(codec,
  1470. spec->autocfg.line_out_pins[0]);
  1471. toggle_output_mutes(codec, spec->autocfg.speaker_outs,
  1472. spec->autocfg.speaker_pins,
  1473. present);
  1474. }
  1475. /* mute internal speaker if HP is plugged */
  1476. static void via_hp_automute(struct hda_codec *codec)
  1477. {
  1478. int present = 0;
  1479. int nums;
  1480. struct via_spec *spec = codec->spec;
  1481. if (!spec->hp_independent_mode && spec->autocfg.hp_pins[0] &&
  1482. (spec->codec_type != VT1708 || spec->vt1708_jack_detect) &&
  1483. is_jack_detectable(codec, spec->autocfg.hp_pins[0]))
  1484. present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
  1485. if (spec->smart51_enabled)
  1486. nums = spec->autocfg.line_outs + spec->smart51_nums;
  1487. else
  1488. nums = spec->autocfg.line_outs;
  1489. toggle_output_mutes(codec, nums, spec->autocfg.line_out_pins, present);
  1490. via_line_automute(codec, present);
  1491. }
  1492. #ifdef CONFIG_PM
  1493. static int via_suspend(struct hda_codec *codec)
  1494. {
  1495. struct via_spec *spec = codec->spec;
  1496. vt1708_stop_hp_work(spec);
  1497. if (spec->codec_type == VT1802) {
  1498. /* Fix pop noise on headphones */
  1499. int i;
  1500. for (i = 0; i < spec->autocfg.hp_outs; i++)
  1501. snd_hda_set_pin_ctl(codec, spec->autocfg.hp_pins[i], 0);
  1502. }
  1503. return 0;
  1504. }
  1505. #endif
  1506. #ifdef CONFIG_PM
  1507. static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
  1508. {
  1509. struct via_spec *spec = codec->spec;
  1510. return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
  1511. }
  1512. #endif
  1513. /*
  1514. */
  1515. static int via_init(struct hda_codec *codec);
  1516. static const struct hda_codec_ops via_patch_ops = {
  1517. .build_controls = via_build_controls,
  1518. .build_pcms = via_build_pcms,
  1519. .init = via_init,
  1520. .free = via_free,
  1521. .unsol_event = snd_hda_jack_unsol_event,
  1522. #ifdef CONFIG_PM
  1523. .suspend = via_suspend,
  1524. .check_power_status = via_check_power_status,
  1525. #endif
  1526. };
  1527. static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
  1528. {
  1529. struct via_spec *spec = codec->spec;
  1530. int i;
  1531. for (i = 0; i < spec->multiout.num_dacs; i++) {
  1532. if (spec->multiout.dac_nids[i] == dac)
  1533. return false;
  1534. }
  1535. if (spec->hp_dac_nid == dac)
  1536. return false;
  1537. return true;
  1538. }
  1539. static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
  1540. hda_nid_t target_dac, int with_aa_mix,
  1541. struct nid_path *path, int depth)
  1542. {
  1543. struct via_spec *spec = codec->spec;
  1544. hda_nid_t conn[8];
  1545. int i, nums;
  1546. if (nid == spec->aa_mix_nid) {
  1547. if (!with_aa_mix)
  1548. return false;
  1549. with_aa_mix = 2; /* mark aa-mix is included */
  1550. }
  1551. nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
  1552. for (i = 0; i < nums; i++) {
  1553. if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
  1554. continue;
  1555. if (conn[i] == target_dac || is_empty_dac(codec, conn[i])) {
  1556. /* aa-mix is requested but not included? */
  1557. if (!(spec->aa_mix_nid && with_aa_mix == 1))
  1558. goto found;
  1559. }
  1560. }
  1561. if (depth >= MAX_NID_PATH_DEPTH)
  1562. return false;
  1563. for (i = 0; i < nums; i++) {
  1564. unsigned int type;
  1565. type = get_wcaps_type(get_wcaps(codec, conn[i]));
  1566. if (type == AC_WID_AUD_OUT)
  1567. continue;
  1568. if (__parse_output_path(codec, conn[i], target_dac,
  1569. with_aa_mix, path, depth + 1))
  1570. goto found;
  1571. }
  1572. return false;
  1573. found:
  1574. path->path[path->depth] = conn[i];
  1575. path->idx[path->depth] = i;
  1576. if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
  1577. path->multi[path->depth] = 1;
  1578. path->depth++;
  1579. return true;
  1580. }
  1581. static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
  1582. hda_nid_t target_dac, int with_aa_mix,
  1583. struct nid_path *path)
  1584. {
  1585. if (__parse_output_path(codec, nid, target_dac, with_aa_mix, path, 1)) {
  1586. path->path[path->depth] = nid;
  1587. path->depth++;
  1588. snd_printdd("output-path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
  1589. path->depth, path->path[0], path->path[1],
  1590. path->path[2], path->path[3], path->path[4]);
  1591. return true;
  1592. }
  1593. return false;
  1594. }
  1595. static int via_auto_fill_dac_nids(struct hda_codec *codec)
  1596. {
  1597. struct via_spec *spec = codec->spec;
  1598. const struct auto_pin_cfg *cfg = &spec->autocfg;
  1599. int i, dac_num;
  1600. hda_nid_t nid;
  1601. spec->multiout.dac_nids = spec->private_dac_nids;
  1602. dac_num = 0;
  1603. for (i = 0; i < cfg->line_outs; i++) {
  1604. hda_nid_t dac = 0;
  1605. nid = cfg->line_out_pins[i];
  1606. if (!nid)
  1607. continue;
  1608. if (parse_output_path(codec, nid, 0, 0, &spec->out_path[i]))
  1609. dac = spec->out_path[i].path[0];
  1610. if (!i && parse_output_path(codec, nid, dac, 1,
  1611. &spec->out_mix_path))
  1612. dac = spec->out_mix_path.path[0];
  1613. if (dac) {
  1614. spec->private_dac_nids[i] = dac;
  1615. dac_num++;
  1616. }
  1617. }
  1618. if (!spec->out_path[0].depth && spec->out_mix_path.depth) {
  1619. spec->out_path[0] = spec->out_mix_path;
  1620. spec->out_mix_path.depth = 0;
  1621. }
  1622. spec->multiout.num_dacs = dac_num;
  1623. return 0;
  1624. }
  1625. static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
  1626. int chs, bool check_dac, struct nid_path *path)
  1627. {
  1628. struct via_spec *spec = codec->spec;
  1629. char name[32];
  1630. hda_nid_t dac, pin, sel, nid;
  1631. int err;
  1632. dac = check_dac ? path->path[0] : 0;
  1633. pin = path->path[path->depth - 1];
  1634. sel = path->depth > 1 ? path->path[1] : 0;
  1635. if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
  1636. nid = dac;
  1637. else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
  1638. nid = pin;
  1639. else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
  1640. nid = sel;
  1641. else
  1642. nid = 0;
  1643. if (nid) {
  1644. sprintf(name, "%s Playback Volume", pfx);
  1645. err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
  1646. HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
  1647. if (err < 0)
  1648. return err;
  1649. path->vol_ctl = nid;
  1650. }
  1651. if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
  1652. nid = dac;
  1653. else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
  1654. nid = pin;
  1655. else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
  1656. nid = sel;
  1657. else
  1658. nid = 0;
  1659. if (nid) {
  1660. sprintf(name, "%s Playback Switch", pfx);
  1661. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
  1662. HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
  1663. if (err < 0)
  1664. return err;
  1665. path->mute_ctl = nid;
  1666. }
  1667. return 0;
  1668. }
  1669. static void mangle_smart51(struct hda_codec *codec)
  1670. {
  1671. struct via_spec *spec = codec->spec;
  1672. struct auto_pin_cfg *cfg = &spec->autocfg;
  1673. struct auto_pin_cfg_item *ins = cfg->inputs;
  1674. int i, j, nums, attr;
  1675. int pins[AUTO_CFG_MAX_INS];
  1676. for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
  1677. nums = 0;
  1678. for (i = 0; i < cfg->num_inputs; i++) {
  1679. unsigned int def;
  1680. if (ins[i].type > AUTO_PIN_LINE_IN)
  1681. continue;
  1682. def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
  1683. if (snd_hda_get_input_pin_attr(def) != attr)
  1684. continue;
  1685. for (j = 0; j < nums; j++)
  1686. if (ins[pins[j]].type < ins[i].type) {
  1687. memmove(pins + j + 1, pins + j,
  1688. (nums - j) * sizeof(int));
  1689. break;
  1690. }
  1691. pins[j] = i;
  1692. nums++;
  1693. }
  1694. if (cfg->line_outs + nums < 3)
  1695. continue;
  1696. for (i = 0; i < nums; i++) {
  1697. hda_nid_t pin = ins[pins[i]].pin;
  1698. spec->smart51_pins[spec->smart51_nums++] = pin;
  1699. cfg->line_out_pins[cfg->line_outs++] = pin;
  1700. if (cfg->line_outs == 3)
  1701. break;
  1702. }
  1703. return;
  1704. }
  1705. }
  1706. static void copy_path_mixer_ctls(struct nid_path *dst, struct nid_path *src)
  1707. {
  1708. dst->vol_ctl = src->vol_ctl;
  1709. dst->mute_ctl = src->mute_ctl;
  1710. }
  1711. /* add playback controls from the parsed DAC table */
  1712. static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
  1713. {
  1714. struct via_spec *spec = codec->spec;
  1715. struct auto_pin_cfg *cfg = &spec->autocfg;
  1716. struct nid_path *path;
  1717. static const char * const chname[4] = {
  1718. "Front", "Surround", "C/LFE", "Side"
  1719. };
  1720. int i, idx, err;
  1721. int old_line_outs;
  1722. /* check smart51 */
  1723. old_line_outs = cfg->line_outs;
  1724. if (cfg->line_outs == 1)
  1725. mangle_smart51(codec);
  1726. err = via_auto_fill_dac_nids(codec);
  1727. if (err < 0)
  1728. return err;
  1729. if (spec->multiout.num_dacs < 3) {
  1730. spec->smart51_nums = 0;
  1731. cfg->line_outs = old_line_outs;
  1732. }
  1733. for (i = 0; i < cfg->line_outs; i++) {
  1734. hda_nid_t pin, dac;
  1735. pin = cfg->line_out_pins[i];
  1736. dac = spec->multiout.dac_nids[i];
  1737. if (!pin || !dac)
  1738. continue;
  1739. path = spec->out_path + i;
  1740. if (i == HDA_CLFE) {
  1741. err = create_ch_ctls(codec, "Center", 1, true, path);
  1742. if (err < 0)
  1743. return err;
  1744. err = create_ch_ctls(codec, "LFE", 2, true, path);
  1745. if (err < 0)
  1746. return err;
  1747. } else {
  1748. const char *pfx = chname[i];
  1749. if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
  1750. cfg->line_outs == 1)
  1751. pfx = "Speaker";
  1752. err = create_ch_ctls(codec, pfx, 3, true, path);
  1753. if (err < 0)
  1754. return err;
  1755. }
  1756. if (path != spec->out_path + i)
  1757. copy_path_mixer_ctls(&spec->out_path[i], path);
  1758. if (path == spec->out_path && spec->out_mix_path.depth)
  1759. copy_path_mixer_ctls(&spec->out_mix_path, path);
  1760. }
  1761. idx = get_connection_index(codec, spec->aa_mix_nid,
  1762. spec->multiout.dac_nids[0]);
  1763. if (idx >= 0) {
  1764. /* add control to mixer */
  1765. const char *name;
  1766. name = spec->out_mix_path.depth ?
  1767. "PCM Loopback Playback Volume" : "PCM Playback Volume";
  1768. err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
  1769. HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
  1770. idx, HDA_INPUT));
  1771. if (err < 0)
  1772. return err;
  1773. name = spec->out_mix_path.depth ?
  1774. "PCM Loopback Playback Switch" : "PCM Playback Switch";
  1775. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
  1776. HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
  1777. idx, HDA_INPUT));
  1778. if (err < 0)
  1779. return err;
  1780. }
  1781. cfg->line_outs = old_line_outs;
  1782. return 0;
  1783. }
  1784. static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
  1785. {
  1786. struct via_spec *spec = codec->spec;
  1787. struct nid_path *path;
  1788. bool check_dac;
  1789. int i, err;
  1790. if (!pin)
  1791. return 0;
  1792. if (!parse_output_path(codec, pin, 0, 0, &spec->hp_indep_path)) {
  1793. for (i = HDA_SIDE; i >= HDA_CLFE; i--) {
  1794. if (i < spec->multiout.num_dacs &&
  1795. parse_output_path(codec, pin,
  1796. spec->multiout.dac_nids[i], 0,
  1797. &spec->hp_indep_path)) {
  1798. spec->hp_indep_shared = i;
  1799. break;
  1800. }
  1801. }
  1802. }
  1803. if (spec->hp_indep_path.depth) {
  1804. spec->hp_dac_nid = spec->hp_indep_path.path[0];
  1805. if (!spec->hp_indep_shared)
  1806. spec->hp_path = spec->hp_indep_path;
  1807. }
  1808. /* optionally check front-path w/o AA-mix */
  1809. if (!spec->hp_path.depth)
  1810. parse_output_path(codec, pin,
  1811. spec->multiout.dac_nids[HDA_FRONT], 0,
  1812. &spec->hp_path);
  1813. if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
  1814. 1, &spec->hp_mix_path) && !spec->hp_path.depth)
  1815. return 0;
  1816. if (spec->hp_path.depth) {
  1817. path = &spec->hp_path;
  1818. check_dac = true;
  1819. } else {
  1820. path = &spec->hp_mix_path;
  1821. check_dac = false;
  1822. }
  1823. err = create_ch_ctls(codec, "Headphone", 3, check_dac, path);
  1824. if (err < 0)
  1825. return err;
  1826. if (check_dac)
  1827. copy_path_mixer_ctls(&spec->hp_mix_path, path);
  1828. else
  1829. copy_path_mixer_ctls(&spec->hp_path, path);
  1830. if (spec->hp_indep_path.depth)
  1831. copy_path_mixer_ctls(&spec->hp_indep_path, path);
  1832. return 0;
  1833. }
  1834. static int via_auto_create_speaker_ctls(struct hda_codec *codec)
  1835. {
  1836. struct via_spec *spec = codec->spec;
  1837. struct nid_path *path;
  1838. bool check_dac;
  1839. hda_nid_t pin, dac = 0;
  1840. int err;
  1841. pin = spec->autocfg.speaker_pins[0];
  1842. if (!spec->autocfg.speaker_outs || !pin)
  1843. return 0;
  1844. if (parse_output_path(codec, pin, 0, 0, &spec->speaker_path))
  1845. dac = spec->speaker_path.path[0];
  1846. if (!dac)
  1847. parse_output_path(codec, pin,
  1848. spec->multiout.dac_nids[HDA_FRONT], 0,
  1849. &spec->speaker_path);
  1850. if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
  1851. 1, &spec->speaker_mix_path) && !dac)
  1852. return 0;
  1853. /* no AA-path for front? */
  1854. if (!spec->out_mix_path.depth && spec->speaker_mix_path.depth)
  1855. dac = 0;
  1856. spec->speaker_dac_nid = dac;
  1857. spec->multiout.extra_out_nid[0] = dac;
  1858. if (dac) {
  1859. path = &spec->speaker_path;
  1860. check_dac = true;
  1861. } else {
  1862. path = &spec->speaker_mix_path;
  1863. check_dac = false;
  1864. }
  1865. err = create_ch_ctls(codec, "Speaker", 3, check_dac, path);
  1866. if (err < 0)
  1867. return err;
  1868. if (check_dac)
  1869. copy_path_mixer_ctls(&spec->speaker_mix_path, path);
  1870. else
  1871. copy_path_mixer_ctls(&spec->speaker_path, path);
  1872. return 0;
  1873. }
  1874. #define via_aamix_ctl_info via_pin_power_ctl_info
  1875. static int via_aamix_ctl_get(struct snd_kcontrol *kcontrol,
  1876. struct snd_ctl_elem_value *ucontrol)
  1877. {
  1878. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1879. struct via_spec *spec = codec->spec;
  1880. ucontrol->value.enumerated.item[0] = spec->aamix_mode;
  1881. return 0;
  1882. }
  1883. static void update_aamix_paths(struct hda_codec *codec, int do_mix,
  1884. struct nid_path *nomix, struct nid_path *mix)
  1885. {
  1886. if (do_mix) {
  1887. activate_output_path(codec, nomix, false, false);
  1888. activate_output_path(codec, mix, true, false);
  1889. } else {
  1890. activate_output_path(codec, mix, false, false);
  1891. activate_output_path(codec, nomix, true, false);
  1892. }
  1893. }
  1894. static int via_aamix_ctl_put(struct snd_kcontrol *kcontrol,
  1895. struct snd_ctl_elem_value *ucontrol)
  1896. {
  1897. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1898. struct via_spec *spec = codec->spec;
  1899. unsigned int val = ucontrol->value.enumerated.item[0];
  1900. if (val == spec->aamix_mode)
  1901. return 0;
  1902. spec->aamix_mode = val;
  1903. /* update front path */
  1904. update_aamix_paths(codec, val, &spec->out_path[0], &spec->out_mix_path);
  1905. /* update HP path */
  1906. if (!spec->hp_independent_mode) {
  1907. update_aamix_paths(codec, val, &spec->hp_path,
  1908. &spec->hp_mix_path);
  1909. }
  1910. /* update speaker path */
  1911. update_aamix_paths(codec, val, &spec->speaker_path,
  1912. &spec->speaker_mix_path);
  1913. return 1;
  1914. }
  1915. static const struct snd_kcontrol_new via_aamix_ctl_enum = {
  1916. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1917. .name = "Loopback Mixing",
  1918. .info = via_aamix_ctl_info,
  1919. .get = via_aamix_ctl_get,
  1920. .put = via_aamix_ctl_put,
  1921. };
  1922. static int via_auto_create_loopback_switch(struct hda_codec *codec)
  1923. {
  1924. struct via_spec *spec = codec->spec;
  1925. if (!spec->aa_mix_nid)
  1926. return 0; /* no loopback switching available */
  1927. if (!(spec->out_mix_path.depth || spec->hp_mix_path.depth ||
  1928. spec->speaker_path.depth))
  1929. return 0; /* no loopback switching available */
  1930. if (!via_clone_control(spec, &via_aamix_ctl_enum))
  1931. return -ENOMEM;
  1932. return 0;
  1933. }
  1934. /* look for ADCs */
  1935. static int via_fill_adcs(struct hda_codec *codec)
  1936. {
  1937. struct via_spec *spec = codec->spec;
  1938. hda_nid_t nid = codec->start_nid;
  1939. int i;
  1940. for (i = 0; i < codec->num_nodes; i++, nid++) {
  1941. unsigned int wcaps = get_wcaps(codec, nid);
  1942. if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
  1943. continue;
  1944. if (wcaps & AC_WCAP_DIGITAL)
  1945. continue;
  1946. if (!(wcaps & AC_WCAP_CONN_LIST))
  1947. continue;
  1948. if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
  1949. return -ENOMEM;
  1950. spec->adc_nids[spec->num_adc_nids++] = nid;
  1951. }
  1952. return 0;
  1953. }
  1954. /* input-src control */
  1955. static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
  1956. struct snd_ctl_elem_info *uinfo)
  1957. {
  1958. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1959. struct via_spec *spec = codec->spec;
  1960. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1961. uinfo->count = 1;
  1962. uinfo->value.enumerated.items = spec->num_inputs;
  1963. if (uinfo->value.enumerated.item >= spec->num_inputs)
  1964. uinfo->value.enumerated.item = spec->num_inputs - 1;
  1965. strcpy(uinfo->value.enumerated.name,
  1966. spec->inputs[uinfo->value.enumerated.item].label);
  1967. return 0;
  1968. }
  1969. static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
  1970. struct snd_ctl_elem_value *ucontrol)
  1971. {
  1972. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1973. struct via_spec *spec = codec->spec;
  1974. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1975. ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
  1976. return 0;
  1977. }
  1978. static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
  1979. struct snd_ctl_elem_value *ucontrol)
  1980. {
  1981. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1982. struct via_spec *spec = codec->spec;
  1983. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1984. hda_nid_t mux;
  1985. int cur;
  1986. cur = ucontrol->value.enumerated.item[0];
  1987. if (cur < 0 || cur >= spec->num_inputs)
  1988. return -EINVAL;
  1989. if (spec->cur_mux[idx] == cur)
  1990. return 0;
  1991. spec->cur_mux[idx] = cur;
  1992. if (spec->dyn_adc_switch) {
  1993. int adc_idx = spec->inputs[cur].adc_idx;
  1994. mux = spec->mux_nids[adc_idx];
  1995. via_dyn_adc_pcm_resetup(codec, cur);
  1996. } else {
  1997. mux = spec->mux_nids[idx];
  1998. if (snd_BUG_ON(!mux))
  1999. return -EINVAL;
  2000. }
  2001. if (mux) {
  2002. /* switch to D0 beofre change index */
  2003. update_power_state(codec, mux, AC_PWRST_D0);
  2004. snd_hda_codec_write(codec, mux, 0,
  2005. AC_VERB_SET_CONNECT_SEL,
  2006. spec->inputs[cur].mux_idx);
  2007. }
  2008. /* update jack power state */
  2009. set_widgets_power_state(codec);
  2010. return 0;
  2011. }
  2012. static const struct snd_kcontrol_new via_input_src_ctl = {
  2013. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2014. /* The multiple "Capture Source" controls confuse alsamixer
  2015. * So call somewhat different..
  2016. */
  2017. /* .name = "Capture Source", */
  2018. .name = "Input Source",
  2019. .info = via_mux_enum_info,
  2020. .get = via_mux_enum_get,
  2021. .put = via_mux_enum_put,
  2022. };
  2023. static int create_input_src_ctls(struct hda_codec *codec, int count)
  2024. {
  2025. struct via_spec *spec = codec->spec;
  2026. struct snd_kcontrol_new *knew;
  2027. if (spec->num_inputs <= 1 || !count)
  2028. return 0; /* no need for single src */
  2029. knew = via_clone_control(spec, &via_input_src_ctl);
  2030. if (!knew)
  2031. return -ENOMEM;
  2032. knew->count = count;
  2033. return 0;
  2034. }
  2035. /* add the powersave loopback-list entry */
  2036. static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
  2037. {
  2038. struct hda_amp_list *list;
  2039. if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
  2040. return;
  2041. list = spec->loopback_list + spec->num_loopbacks;
  2042. list->nid = mix;
  2043. list->dir = HDA_INPUT;
  2044. list->idx = idx;
  2045. spec->num_loopbacks++;
  2046. spec->loopback.amplist = spec->loopback_list;
  2047. }
  2048. static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
  2049. hda_nid_t dst)
  2050. {
  2051. return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
  2052. }
  2053. /* add the input-route to the given pin */
  2054. static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
  2055. {
  2056. struct via_spec *spec = codec->spec;
  2057. int c, idx;
  2058. spec->inputs[spec->num_inputs].adc_idx = -1;
  2059. spec->inputs[spec->num_inputs].pin = pin;
  2060. for (c = 0; c < spec->num_adc_nids; c++) {
  2061. if (spec->mux_nids[c]) {
  2062. idx = get_connection_index(codec, spec->mux_nids[c],
  2063. pin);
  2064. if (idx < 0)
  2065. continue;
  2066. spec->inputs[spec->num_inputs].mux_idx = idx;
  2067. } else {
  2068. if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
  2069. continue;
  2070. }
  2071. spec->inputs[spec->num_inputs].adc_idx = c;
  2072. /* Can primary ADC satisfy all inputs? */
  2073. if (!spec->dyn_adc_switch &&
  2074. spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
  2075. snd_printd(KERN_INFO
  2076. "via: dynamic ADC switching enabled\n");
  2077. spec->dyn_adc_switch = 1;
  2078. }
  2079. return true;
  2080. }
  2081. return false;
  2082. }
  2083. static int get_mux_nids(struct hda_codec *codec);
  2084. /* parse input-routes; fill ADCs, MUXs and input-src entries */
  2085. static int parse_analog_inputs(struct hda_codec *codec)
  2086. {
  2087. struct via_spec *spec = codec->spec;
  2088. const struct auto_pin_cfg *cfg = &spec->autocfg;
  2089. int i, err;
  2090. err = via_fill_adcs(codec);
  2091. if (err < 0)
  2092. return err;
  2093. err = get_mux_nids(codec);
  2094. if (err < 0)
  2095. return err;
  2096. /* fill all input-routes */
  2097. for (i = 0; i < cfg->num_inputs; i++) {
  2098. if (add_input_route(codec, cfg->inputs[i].pin))
  2099. spec->inputs[spec->num_inputs++].label =
  2100. hda_get_autocfg_input_label(codec, cfg, i);
  2101. }
  2102. /* check for internal loopback recording */
  2103. if (spec->aa_mix_nid &&
  2104. add_input_route(codec, spec->aa_mix_nid))
  2105. spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
  2106. return 0;
  2107. }
  2108. /* create analog-loopback volume/switch controls */
  2109. static int create_loopback_ctls(struct hda_codec *codec)
  2110. {
  2111. struct via_spec *spec = codec->spec;
  2112. const struct auto_pin_cfg *cfg = &spec->autocfg;
  2113. const char *prev_label = NULL;
  2114. int type_idx = 0;
  2115. int i, j, err, idx;
  2116. if (!spec->aa_mix_nid)
  2117. return 0;
  2118. for (i = 0; i < cfg->num_inputs; i++) {
  2119. hda_nid_t pin = cfg->inputs[i].pin;
  2120. const char *label = hda_get_autocfg_input_label(codec, cfg, i);
  2121. if (prev_label && !strcmp(label, prev_label))
  2122. type_idx++;
  2123. else
  2124. type_idx = 0;
  2125. prev_label = label;
  2126. idx = get_connection_index(codec, spec->aa_mix_nid, pin);
  2127. if (idx >= 0) {
  2128. err = via_new_analog_input(spec, label, type_idx,
  2129. idx, spec->aa_mix_nid);
  2130. if (err < 0)
  2131. return err;
  2132. add_loopback_list(spec, spec->aa_mix_nid, idx);
  2133. }
  2134. /* remember the label for smart51 control */
  2135. for (j = 0; j < spec->smart51_nums; j++) {
  2136. if (spec->smart51_pins[j] == pin) {
  2137. spec->smart51_idxs[j] = idx;
  2138. spec->smart51_labels[j] = label;
  2139. break;
  2140. }
  2141. }
  2142. }
  2143. return 0;
  2144. }
  2145. /* create mic-boost controls (if present) */
  2146. static int create_mic_boost_ctls(struct hda_codec *codec)
  2147. {
  2148. struct via_spec *spec = codec->spec;
  2149. const struct auto_pin_cfg *cfg = &spec->autocfg;
  2150. const char *prev_label = NULL;
  2151. int type_idx = 0;
  2152. int i, err;
  2153. for (i = 0; i < cfg->num_inputs; i++) {
  2154. hda_nid_t pin = cfg->inputs[i].pin;
  2155. unsigned int caps;
  2156. const char *label;
  2157. char name[32];
  2158. if (cfg->inputs[i].type != AUTO_PIN_MIC)
  2159. continue;
  2160. caps = query_amp_caps(codec, pin, HDA_INPUT);
  2161. if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
  2162. continue;
  2163. label = hda_get_autocfg_input_label(codec, cfg, i);
  2164. if (prev_label && !strcmp(label, prev_label))
  2165. type_idx++;
  2166. else
  2167. type_idx = 0;
  2168. prev_label = label;
  2169. snprintf(name, sizeof(name), "%s Boost Volume", label);
  2170. err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
  2171. HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
  2172. if (err < 0)
  2173. return err;
  2174. }
  2175. return 0;
  2176. }
  2177. /* create capture and input-src controls for multiple streams */
  2178. static int create_multi_adc_ctls(struct hda_codec *codec)
  2179. {
  2180. struct via_spec *spec = codec->spec;
  2181. int i, err;
  2182. /* create capture mixer elements */
  2183. for (i = 0; i < spec->num_adc_nids; i++) {
  2184. hda_nid_t adc = spec->adc_nids[i];
  2185. err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
  2186. "Capture Volume", i,
  2187. HDA_COMPOSE_AMP_VAL(adc, 3, 0,
  2188. HDA_INPUT));
  2189. if (err < 0)
  2190. return err;
  2191. err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
  2192. "Capture Switch", i,
  2193. HDA_COMPOSE_AMP_VAL(adc, 3, 0,
  2194. HDA_INPUT));
  2195. if (err < 0)
  2196. return err;
  2197. }
  2198. /* input-source control */
  2199. for (i = 0; i < spec->num_adc_nids; i++)
  2200. if (!spec->mux_nids[i])
  2201. break;
  2202. err = create_input_src_ctls(codec, i);
  2203. if (err < 0)
  2204. return err;
  2205. return 0;
  2206. }
  2207. /* bind capture volume/switch */
  2208. static struct snd_kcontrol_new via_bind_cap_vol_ctl =
  2209. HDA_BIND_VOL("Capture Volume", 0);
  2210. static struct snd_kcontrol_new via_bind_cap_sw_ctl =
  2211. HDA_BIND_SW("Capture Switch", 0);
  2212. static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
  2213. struct hda_ctl_ops *ops)
  2214. {
  2215. struct hda_bind_ctls *ctl;
  2216. int i;
  2217. ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
  2218. if (!ctl)
  2219. return -ENOMEM;
  2220. ctl->ops = ops;
  2221. for (i = 0; i < spec->num_adc_nids; i++)
  2222. ctl->values[i] =
  2223. HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
  2224. *ctl_ret = ctl;
  2225. return 0;
  2226. }
  2227. /* create capture and input-src controls for dynamic ADC-switch case */
  2228. static int create_dyn_adc_ctls(struct hda_codec *codec)
  2229. {
  2230. struct via_spec *spec = codec->spec;
  2231. struct snd_kcontrol_new *knew;
  2232. int err;
  2233. /* set up the bind capture ctls */
  2234. err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
  2235. if (err < 0)
  2236. return err;
  2237. err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
  2238. if (err < 0)
  2239. return err;
  2240. /* create capture mixer elements */
  2241. knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
  2242. if (!knew)
  2243. return -ENOMEM;
  2244. knew->private_value = (long)spec->bind_cap_vol;
  2245. knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
  2246. if (!knew)
  2247. return -ENOMEM;
  2248. knew->private_value = (long)spec->bind_cap_sw;
  2249. /* input-source control */
  2250. err = create_input_src_ctls(codec, 1);
  2251. if (err < 0)
  2252. return err;
  2253. return 0;
  2254. }
  2255. /* parse and create capture-related stuff */
  2256. static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
  2257. {
  2258. struct via_spec *spec = codec->spec;
  2259. int err;
  2260. err = parse_analog_inputs(codec);
  2261. if (err < 0)
  2262. return err;
  2263. if (spec->dyn_adc_switch)
  2264. err = create_dyn_adc_ctls(codec);
  2265. else
  2266. err = create_multi_adc_ctls(codec);
  2267. if (err < 0)
  2268. return err;
  2269. err = create_loopback_ctls(codec);
  2270. if (err < 0)
  2271. return err;
  2272. err = create_mic_boost_ctls(codec);
  2273. if (err < 0)
  2274. return err;
  2275. return 0;
  2276. }
  2277. static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
  2278. {
  2279. unsigned int def_conf;
  2280. unsigned char seqassoc;
  2281. def_conf = snd_hda_codec_get_pincfg(codec, nid);
  2282. seqassoc = (unsigned char) get_defcfg_association(def_conf);
  2283. seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
  2284. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
  2285. && (seqassoc == 0xf0 || seqassoc == 0xff)) {
  2286. def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
  2287. snd_hda_codec_set_pincfg(codec, nid, def_conf);
  2288. }
  2289. return;
  2290. }
  2291. static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
  2292. struct snd_ctl_elem_value *ucontrol)
  2293. {
  2294. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2295. struct via_spec *spec = codec->spec;
  2296. if (spec->codec_type != VT1708)
  2297. return 0;
  2298. ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
  2299. return 0;
  2300. }
  2301. static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
  2302. struct snd_ctl_elem_value *ucontrol)
  2303. {
  2304. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2305. struct via_spec *spec = codec->spec;
  2306. int val;
  2307. if (spec->codec_type != VT1708)
  2308. return 0;
  2309. val = !!ucontrol->value.integer.value[0];
  2310. if (spec->vt1708_jack_detect == val)
  2311. return 0;
  2312. spec->vt1708_jack_detect = val;
  2313. if (spec->vt1708_jack_detect &&
  2314. snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") != 1) {
  2315. mute_aa_path(codec, 1);
  2316. notify_aa_path_ctls(codec);
  2317. }
  2318. via_hp_automute(codec);
  2319. vt1708_update_hp_work(spec);
  2320. return 1;
  2321. }
  2322. static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
  2323. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2324. .name = "Jack Detect",
  2325. .count = 1,
  2326. .info = snd_ctl_boolean_mono_info,
  2327. .get = vt1708_jack_detect_get,
  2328. .put = vt1708_jack_detect_put,
  2329. };
  2330. static void fill_dig_outs(struct hda_codec *codec);
  2331. static void fill_dig_in(struct hda_codec *codec);
  2332. static int via_parse_auto_config(struct hda_codec *codec)
  2333. {
  2334. struct via_spec *spec = codec->spec;
  2335. int err;
  2336. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  2337. if (err < 0)
  2338. return err;
  2339. if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
  2340. return -EINVAL;
  2341. err = via_auto_create_multi_out_ctls(codec);
  2342. if (err < 0)
  2343. return err;
  2344. err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
  2345. if (err < 0)
  2346. return err;
  2347. err = via_auto_create_speaker_ctls(codec);
  2348. if (err < 0)
  2349. return err;
  2350. err = via_auto_create_loopback_switch(codec);
  2351. if (err < 0)
  2352. return err;
  2353. err = via_auto_create_analog_input_ctls(codec);
  2354. if (err < 0)
  2355. return err;
  2356. spec->multiout.max_channels = spec->multiout.num_dacs * 2;
  2357. fill_dig_outs(codec);
  2358. fill_dig_in(codec);
  2359. if (spec->kctls.list)
  2360. spec->mixers[spec->num_mixers++] = spec->kctls.list;
  2361. if (spec->hp_dac_nid && spec->hp_mix_path.depth) {
  2362. err = via_hp_build(codec);
  2363. if (err < 0)
  2364. return err;
  2365. }
  2366. err = via_smart51_build(codec);
  2367. if (err < 0)
  2368. return err;
  2369. /* assign slave outs */
  2370. if (spec->slave_dig_outs[0])
  2371. codec->slave_dig_outs = spec->slave_dig_outs;
  2372. return 1;
  2373. }
  2374. static void via_auto_init_dig_outs(struct hda_codec *codec)
  2375. {
  2376. struct via_spec *spec = codec->spec;
  2377. if (spec->multiout.dig_out_nid)
  2378. init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
  2379. if (spec->slave_dig_outs[0])
  2380. init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
  2381. }
  2382. static void via_auto_init_dig_in(struct hda_codec *codec)
  2383. {
  2384. struct via_spec *spec = codec->spec;
  2385. if (!spec->dig_in_nid)
  2386. return;
  2387. snd_hda_set_pin_ctl(codec, spec->autocfg.dig_in_pin, PIN_IN);
  2388. }
  2389. static void via_jack_output_event(struct hda_codec *codec, struct hda_jack_tbl *tbl)
  2390. {
  2391. set_widgets_power_state(codec);
  2392. via_hp_automute(codec);
  2393. }
  2394. static void via_jack_powerstate_event(struct hda_codec *codec, struct hda_jack_tbl *tbl)
  2395. {
  2396. set_widgets_power_state(codec);
  2397. }
  2398. /* initialize the unsolicited events */
  2399. static void via_auto_init_unsol_event(struct hda_codec *codec)
  2400. {
  2401. struct via_spec *spec = codec->spec;
  2402. struct auto_pin_cfg *cfg = &spec->autocfg;
  2403. unsigned int ev;
  2404. int i;
  2405. hda_jack_callback cb;
  2406. if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
  2407. snd_hda_jack_detect_enable_callback(codec, cfg->hp_pins[0],
  2408. VIA_HP_EVENT | VIA_JACK_EVENT,
  2409. via_jack_output_event);
  2410. if (cfg->speaker_pins[0])
  2411. ev = VIA_LINE_EVENT;
  2412. else
  2413. ev = 0;
  2414. cb = ev ? via_jack_output_event : via_jack_powerstate_event;
  2415. for (i = 0; i < cfg->line_outs; i++) {
  2416. if (cfg->line_out_pins[i] &&
  2417. is_jack_detectable(codec, cfg->line_out_pins[i]))
  2418. snd_hda_jack_detect_enable_callback(codec, cfg->line_out_pins[i],
  2419. ev | VIA_JACK_EVENT, cb);
  2420. }
  2421. for (i = 0; i < cfg->num_inputs; i++) {
  2422. if (is_jack_detectable(codec, cfg->inputs[i].pin))
  2423. snd_hda_jack_detect_enable_callback(codec, cfg->inputs[i].pin,
  2424. VIA_JACK_EVENT,
  2425. via_jack_powerstate_event);
  2426. }
  2427. }
  2428. static int via_init(struct hda_codec *codec)
  2429. {
  2430. struct via_spec *spec = codec->spec;
  2431. int i;
  2432. for (i = 0; i < spec->num_iverbs; i++)
  2433. snd_hda_sequence_write(codec, spec->init_verbs[i]);
  2434. /* init power states */
  2435. set_widgets_power_state(codec);
  2436. __analog_low_current_mode(codec, true);
  2437. via_auto_init_multi_out(codec);
  2438. via_auto_init_hp_out(codec);
  2439. via_auto_init_speaker_out(codec);
  2440. via_auto_init_analog_input(codec);
  2441. via_auto_init_dig_outs(codec);
  2442. via_auto_init_dig_in(codec);
  2443. via_auto_init_unsol_event(codec);
  2444. via_hp_automute(codec);
  2445. vt1708_update_hp_work(spec);
  2446. return 0;
  2447. }
  2448. static void vt1708_update_hp_jack_state(struct work_struct *work)
  2449. {
  2450. struct via_spec *spec = container_of(work, struct via_spec,
  2451. vt1708_hp_work.work);
  2452. if (spec->codec_type != VT1708)
  2453. return;
  2454. snd_hda_jack_set_dirty_all(spec->codec);
  2455. /* if jack state toggled */
  2456. if (spec->vt1708_hp_present
  2457. != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
  2458. spec->vt1708_hp_present ^= 1;
  2459. via_hp_automute(spec->codec);
  2460. }
  2461. if (spec->vt1708_jack_detect)
  2462. schedule_delayed_work(&spec->vt1708_hp_work,
  2463. msecs_to_jiffies(100));
  2464. }
  2465. static int get_mux_nids(struct hda_codec *codec)
  2466. {
  2467. struct via_spec *spec = codec->spec;
  2468. hda_nid_t nid, conn[8];
  2469. unsigned int type;
  2470. int i, n;
  2471. for (i = 0; i < spec->num_adc_nids; i++) {
  2472. nid = spec->adc_nids[i];
  2473. while (nid) {
  2474. type = get_wcaps_type(get_wcaps(codec, nid));
  2475. if (type == AC_WID_PIN)
  2476. break;
  2477. n = snd_hda_get_connections(codec, nid, conn,
  2478. ARRAY_SIZE(conn));
  2479. if (n <= 0)
  2480. break;
  2481. if (n > 1) {
  2482. spec->mux_nids[i] = nid;
  2483. break;
  2484. }
  2485. nid = conn[0];
  2486. }
  2487. }
  2488. return 0;
  2489. }
  2490. static int patch_vt1708(struct hda_codec *codec)
  2491. {
  2492. struct via_spec *spec;
  2493. int err;
  2494. /* create a codec specific record */
  2495. spec = via_new_spec(codec);
  2496. if (spec == NULL)
  2497. return -ENOMEM;
  2498. spec->aa_mix_nid = 0x17;
  2499. /* Add HP and CD pin config connect bit re-config action */
  2500. vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
  2501. vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
  2502. /* automatic parse from the BIOS config */
  2503. err = via_parse_auto_config(codec);
  2504. if (err < 0) {
  2505. via_free(codec);
  2506. return err;
  2507. }
  2508. /* add jack detect on/off control */
  2509. if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
  2510. return -ENOMEM;
  2511. /* disable 32bit format on VT1708 */
  2512. if (codec->vendor_id == 0x11061708)
  2513. spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
  2514. spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
  2515. codec->patch_ops = via_patch_ops;
  2516. INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
  2517. return 0;
  2518. }
  2519. static int patch_vt1709(struct hda_codec *codec)
  2520. {
  2521. struct via_spec *spec;
  2522. int err;
  2523. /* create a codec specific record */
  2524. spec = via_new_spec(codec);
  2525. if (spec == NULL)
  2526. return -ENOMEM;
  2527. spec->aa_mix_nid = 0x18;
  2528. err = via_parse_auto_config(codec);
  2529. if (err < 0) {
  2530. via_free(codec);
  2531. return err;
  2532. }
  2533. codec->patch_ops = via_patch_ops;
  2534. return 0;
  2535. }
  2536. static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
  2537. {
  2538. struct via_spec *spec = codec->spec;
  2539. int imux_is_smixer;
  2540. unsigned int parm;
  2541. int is_8ch = 0;
  2542. if ((spec->codec_type != VT1708B_4CH) &&
  2543. (codec->vendor_id != 0x11064397))
  2544. is_8ch = 1;
  2545. /* SW0 (17h) = stereo mixer */
  2546. imux_is_smixer =
  2547. (snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
  2548. == ((spec->codec_type == VT1708S) ? 5 : 0));
  2549. /* inputs */
  2550. /* PW 1/2/5 (1ah/1bh/1eh) */
  2551. parm = AC_PWRST_D3;
  2552. set_pin_power_state(codec, 0x1a, &parm);
  2553. set_pin_power_state(codec, 0x1b, &parm);
  2554. set_pin_power_state(codec, 0x1e, &parm);
  2555. if (imux_is_smixer)
  2556. parm = AC_PWRST_D0;
  2557. /* SW0 (17h), AIW 0/1 (13h/14h) */
  2558. update_power_state(codec, 0x17, parm);
  2559. update_power_state(codec, 0x13, parm);
  2560. update_power_state(codec, 0x14, parm);
  2561. /* outputs */
  2562. /* PW0 (19h), SW1 (18h), AOW1 (11h) */
  2563. parm = AC_PWRST_D3;
  2564. set_pin_power_state(codec, 0x19, &parm);
  2565. if (spec->smart51_enabled)
  2566. set_pin_power_state(codec, 0x1b, &parm);
  2567. update_power_state(codec, 0x18, parm);
  2568. update_power_state(codec, 0x11, parm);
  2569. /* PW6 (22h), SW2 (26h), AOW2 (24h) */
  2570. if (is_8ch) {
  2571. parm = AC_PWRST_D3;
  2572. set_pin_power_state(codec, 0x22, &parm);
  2573. if (spec->smart51_enabled)
  2574. set_pin_power_state(codec, 0x1a, &parm);
  2575. update_power_state(codec, 0x26, parm);
  2576. update_power_state(codec, 0x24, parm);
  2577. } else if (codec->vendor_id == 0x11064397) {
  2578. /* PW7(23h), SW2(27h), AOW2(25h) */
  2579. parm = AC_PWRST_D3;
  2580. set_pin_power_state(codec, 0x23, &parm);
  2581. if (spec->smart51_enabled)
  2582. set_pin_power_state(codec, 0x1a, &parm);
  2583. update_power_state(codec, 0x27, parm);
  2584. update_power_state(codec, 0x25, parm);
  2585. }
  2586. /* PW 3/4/7 (1ch/1dh/23h) */
  2587. parm = AC_PWRST_D3;
  2588. /* force to D0 for internal Speaker */
  2589. set_pin_power_state(codec, 0x1c, &parm);
  2590. set_pin_power_state(codec, 0x1d, &parm);
  2591. if (is_8ch)
  2592. set_pin_power_state(codec, 0x23, &parm);
  2593. /* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
  2594. update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
  2595. update_power_state(codec, 0x10, parm);
  2596. if (is_8ch) {
  2597. update_power_state(codec, 0x25, parm);
  2598. update_power_state(codec, 0x27, parm);
  2599. } else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
  2600. update_power_state(codec, 0x25, parm);
  2601. }
  2602. static int patch_vt1708S(struct hda_codec *codec);
  2603. static int patch_vt1708B(struct hda_codec *codec)
  2604. {
  2605. struct via_spec *spec;
  2606. int err;
  2607. if (get_codec_type(codec) == VT1708BCE)
  2608. return patch_vt1708S(codec);
  2609. /* create a codec specific record */
  2610. spec = via_new_spec(codec);
  2611. if (spec == NULL)
  2612. return -ENOMEM;
  2613. spec->aa_mix_nid = 0x16;
  2614. /* automatic parse from the BIOS config */
  2615. err = via_parse_auto_config(codec);
  2616. if (err < 0) {
  2617. via_free(codec);
  2618. return err;
  2619. }
  2620. codec->patch_ops = via_patch_ops;
  2621. spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
  2622. return 0;
  2623. }
  2624. /* Patch for VT1708S */
  2625. static const struct hda_verb vt1708S_init_verbs[] = {
  2626. /* Enable Mic Boost Volume backdoor */
  2627. {0x1, 0xf98, 0x1},
  2628. /* don't bybass mixer */
  2629. {0x1, 0xf88, 0xc0},
  2630. { }
  2631. };
  2632. /* fill out digital output widgets; one for master and one for slave outputs */
  2633. static void fill_dig_outs(struct hda_codec *codec)
  2634. {
  2635. struct via_spec *spec = codec->spec;
  2636. int i;
  2637. for (i = 0; i < spec->autocfg.dig_outs; i++) {
  2638. hda_nid_t nid;
  2639. int conn;
  2640. nid = spec->autocfg.dig_out_pins[i];
  2641. if (!nid)
  2642. continue;
  2643. conn = snd_hda_get_connections(codec, nid, &nid, 1);
  2644. if (conn < 1)
  2645. continue;
  2646. if (!spec->multiout.dig_out_nid)
  2647. spec->multiout.dig_out_nid = nid;
  2648. else {
  2649. spec->slave_dig_outs[0] = nid;
  2650. break; /* at most two dig outs */
  2651. }
  2652. }
  2653. }
  2654. static void fill_dig_in(struct hda_codec *codec)
  2655. {
  2656. struct via_spec *spec = codec->spec;
  2657. hda_nid_t dig_nid;
  2658. int i, err;
  2659. if (!spec->autocfg.dig_in_pin)
  2660. return;
  2661. dig_nid = codec->start_nid;
  2662. for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
  2663. unsigned int wcaps = get_wcaps(codec, dig_nid);
  2664. if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
  2665. continue;
  2666. if (!(wcaps & AC_WCAP_DIGITAL))
  2667. continue;
  2668. if (!(wcaps & AC_WCAP_CONN_LIST))
  2669. continue;
  2670. err = get_connection_index(codec, dig_nid,
  2671. spec->autocfg.dig_in_pin);
  2672. if (err >= 0) {
  2673. spec->dig_in_nid = dig_nid;
  2674. break;
  2675. }
  2676. }
  2677. }
  2678. static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
  2679. int offset, int num_steps, int step_size)
  2680. {
  2681. snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
  2682. (offset << AC_AMPCAP_OFFSET_SHIFT) |
  2683. (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
  2684. (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
  2685. (0 << AC_AMPCAP_MUTE_SHIFT));
  2686. }
  2687. static int patch_vt1708S(struct hda_codec *codec)
  2688. {
  2689. struct via_spec *spec;
  2690. int err;
  2691. /* create a codec specific record */
  2692. spec = via_new_spec(codec);
  2693. if (spec == NULL)
  2694. return -ENOMEM;
  2695. spec->aa_mix_nid = 0x16;
  2696. override_mic_boost(codec, 0x1a, 0, 3, 40);
  2697. override_mic_boost(codec, 0x1e, 0, 3, 40);
  2698. /* automatic parse from the BIOS config */
  2699. err = via_parse_auto_config(codec);
  2700. if (err < 0) {
  2701. via_free(codec);
  2702. return err;
  2703. }
  2704. spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
  2705. codec->patch_ops = via_patch_ops;
  2706. /* correct names for VT1708BCE */
  2707. if (get_codec_type(codec) == VT1708BCE) {
  2708. kfree(codec->chip_name);
  2709. codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
  2710. snprintf(codec->bus->card->mixername,
  2711. sizeof(codec->bus->card->mixername),
  2712. "%s %s", codec->vendor_name, codec->chip_name);
  2713. }
  2714. /* correct names for VT1705 */
  2715. if (codec->vendor_id == 0x11064397) {
  2716. kfree(codec->chip_name);
  2717. codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
  2718. snprintf(codec->bus->card->mixername,
  2719. sizeof(codec->bus->card->mixername),
  2720. "%s %s", codec->vendor_name, codec->chip_name);
  2721. }
  2722. spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
  2723. return 0;
  2724. }
  2725. /* Patch for VT1702 */
  2726. static const struct hda_verb vt1702_init_verbs[] = {
  2727. /* mixer enable */
  2728. {0x1, 0xF88, 0x3},
  2729. /* GPIO 0~2 */
  2730. {0x1, 0xF82, 0x3F},
  2731. { }
  2732. };
  2733. static void set_widgets_power_state_vt1702(struct hda_codec *codec)
  2734. {
  2735. int imux_is_smixer =
  2736. snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
  2737. unsigned int parm;
  2738. /* inputs */
  2739. /* PW 1/2/5 (14h/15h/18h) */
  2740. parm = AC_PWRST_D3;
  2741. set_pin_power_state(codec, 0x14, &parm);
  2742. set_pin_power_state(codec, 0x15, &parm);
  2743. set_pin_power_state(codec, 0x18, &parm);
  2744. if (imux_is_smixer)
  2745. parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
  2746. /* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
  2747. update_power_state(codec, 0x13, parm);
  2748. update_power_state(codec, 0x12, parm);
  2749. update_power_state(codec, 0x1f, parm);
  2750. update_power_state(codec, 0x20, parm);
  2751. /* outputs */
  2752. /* PW 3/4 (16h/17h) */
  2753. parm = AC_PWRST_D3;
  2754. set_pin_power_state(codec, 0x17, &parm);
  2755. set_pin_power_state(codec, 0x16, &parm);
  2756. /* MW0 (1ah), AOW 0/1 (10h/1dh) */
  2757. update_power_state(codec, 0x1a, imux_is_smixer ? AC_PWRST_D0 : parm);
  2758. update_power_state(codec, 0x10, parm);
  2759. update_power_state(codec, 0x1d, parm);
  2760. }
  2761. static int patch_vt1702(struct hda_codec *codec)
  2762. {
  2763. struct via_spec *spec;
  2764. int err;
  2765. /* create a codec specific record */
  2766. spec = via_new_spec(codec);
  2767. if (spec == NULL)
  2768. return -ENOMEM;
  2769. spec->aa_mix_nid = 0x1a;
  2770. /* limit AA path volume to 0 dB */
  2771. snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
  2772. (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
  2773. (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
  2774. (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
  2775. (1 << AC_AMPCAP_MUTE_SHIFT));
  2776. /* automatic parse from the BIOS config */
  2777. err = via_parse_auto_config(codec);
  2778. if (err < 0) {
  2779. via_free(codec);
  2780. return err;
  2781. }
  2782. spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
  2783. codec->patch_ops = via_patch_ops;
  2784. spec->set_widgets_power_state = set_widgets_power_state_vt1702;
  2785. return 0;
  2786. }
  2787. /* Patch for VT1718S */
  2788. static const struct hda_verb vt1718S_init_verbs[] = {
  2789. /* Enable MW0 adjust Gain 5 */
  2790. {0x1, 0xfb2, 0x10},
  2791. /* Enable Boost Volume backdoor */
  2792. {0x1, 0xf88, 0x8},
  2793. { }
  2794. };
  2795. static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
  2796. {
  2797. struct via_spec *spec = codec->spec;
  2798. int imux_is_smixer;
  2799. unsigned int parm, parm2;
  2800. /* MUX6 (1eh) = stereo mixer */
  2801. imux_is_smixer =
  2802. snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
  2803. /* inputs */
  2804. /* PW 5/6/7 (29h/2ah/2bh) */
  2805. parm = AC_PWRST_D3;
  2806. set_pin_power_state(codec, 0x29, &parm);
  2807. set_pin_power_state(codec, 0x2a, &parm);
  2808. set_pin_power_state(codec, 0x2b, &parm);
  2809. if (imux_is_smixer)
  2810. parm = AC_PWRST_D0;
  2811. /* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
  2812. update_power_state(codec, 0x1e, parm);
  2813. update_power_state(codec, 0x1f, parm);
  2814. update_power_state(codec, 0x10, parm);
  2815. update_power_state(codec, 0x11, parm);
  2816. /* outputs */
  2817. /* PW3 (27h), MW2 (1ah), AOW3 (bh) */
  2818. parm = AC_PWRST_D3;
  2819. set_pin_power_state(codec, 0x27, &parm);
  2820. update_power_state(codec, 0x1a, parm);
  2821. parm2 = parm; /* for pin 0x0b */
  2822. /* PW2 (26h), AOW2 (ah) */
  2823. parm = AC_PWRST_D3;
  2824. set_pin_power_state(codec, 0x26, &parm);
  2825. if (spec->smart51_enabled)
  2826. set_pin_power_state(codec, 0x2b, &parm);
  2827. update_power_state(codec, 0xa, parm);
  2828. /* PW0 (24h), AOW0 (8h) */
  2829. parm = AC_PWRST_D3;
  2830. set_pin_power_state(codec, 0x24, &parm);
  2831. if (!spec->hp_independent_mode) /* check for redirected HP */
  2832. set_pin_power_state(codec, 0x28, &parm);
  2833. update_power_state(codec, 0x8, parm);
  2834. if (!spec->hp_independent_mode && parm2 != AC_PWRST_D3)
  2835. parm = parm2;
  2836. update_power_state(codec, 0xb, parm);
  2837. /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
  2838. update_power_state(codec, 0x21, imux_is_smixer ? AC_PWRST_D0 : parm);
  2839. /* PW1 (25h), AOW1 (9h) */
  2840. parm = AC_PWRST_D3;
  2841. set_pin_power_state(codec, 0x25, &parm);
  2842. if (spec->smart51_enabled)
  2843. set_pin_power_state(codec, 0x2a, &parm);
  2844. update_power_state(codec, 0x9, parm);
  2845. if (spec->hp_independent_mode) {
  2846. /* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
  2847. parm = AC_PWRST_D3;
  2848. set_pin_power_state(codec, 0x28, &parm);
  2849. update_power_state(codec, 0x1b, parm);
  2850. update_power_state(codec, 0x34, parm);
  2851. update_power_state(codec, 0xc, parm);
  2852. }
  2853. }
  2854. /* Add a connection to the primary DAC from AA-mixer for some codecs
  2855. * This isn't listed from the raw info, but the chip has a secret connection.
  2856. */
  2857. static int add_secret_dac_path(struct hda_codec *codec)
  2858. {
  2859. struct via_spec *spec = codec->spec;
  2860. int i, nums;
  2861. hda_nid_t conn[8];
  2862. hda_nid_t nid;
  2863. if (!spec->aa_mix_nid)
  2864. return 0;
  2865. nums = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
  2866. ARRAY_SIZE(conn) - 1);
  2867. for (i = 0; i < nums; i++) {
  2868. if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
  2869. return 0;
  2870. }
  2871. /* find the primary DAC and add to the connection list */
  2872. nid = codec->start_nid;
  2873. for (i = 0; i < codec->num_nodes; i++, nid++) {
  2874. unsigned int caps = get_wcaps(codec, nid);
  2875. if (get_wcaps_type(caps) == AC_WID_AUD_OUT &&
  2876. !(caps & AC_WCAP_DIGITAL)) {
  2877. conn[nums++] = nid;
  2878. return snd_hda_override_conn_list(codec,
  2879. spec->aa_mix_nid,
  2880. nums, conn);
  2881. }
  2882. }
  2883. return 0;
  2884. }
  2885. static int patch_vt1718S(struct hda_codec *codec)
  2886. {
  2887. struct via_spec *spec;
  2888. int err;
  2889. /* create a codec specific record */
  2890. spec = via_new_spec(codec);
  2891. if (spec == NULL)
  2892. return -ENOMEM;
  2893. spec->aa_mix_nid = 0x21;
  2894. override_mic_boost(codec, 0x2b, 0, 3, 40);
  2895. override_mic_boost(codec, 0x29, 0, 3, 40);
  2896. add_secret_dac_path(codec);
  2897. /* automatic parse from the BIOS config */
  2898. err = via_parse_auto_config(codec);
  2899. if (err < 0) {
  2900. via_free(codec);
  2901. return err;
  2902. }
  2903. spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
  2904. codec->patch_ops = via_patch_ops;
  2905. spec->set_widgets_power_state = set_widgets_power_state_vt1718S;
  2906. return 0;
  2907. }
  2908. /* Patch for VT1716S */
  2909. static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
  2910. struct snd_ctl_elem_info *uinfo)
  2911. {
  2912. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2913. uinfo->count = 1;
  2914. uinfo->value.integer.min = 0;
  2915. uinfo->value.integer.max = 1;
  2916. return 0;
  2917. }
  2918. static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
  2919. struct snd_ctl_elem_value *ucontrol)
  2920. {
  2921. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2922. int index = 0;
  2923. index = snd_hda_codec_read(codec, 0x26, 0,
  2924. AC_VERB_GET_CONNECT_SEL, 0);
  2925. if (index != -1)
  2926. *ucontrol->value.integer.value = index;
  2927. return 0;
  2928. }
  2929. static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
  2930. struct snd_ctl_elem_value *ucontrol)
  2931. {
  2932. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2933. struct via_spec *spec = codec->spec;
  2934. int index = *ucontrol->value.integer.value;
  2935. snd_hda_codec_write(codec, 0x26, 0,
  2936. AC_VERB_SET_CONNECT_SEL, index);
  2937. spec->dmic_enabled = index;
  2938. set_widgets_power_state(codec);
  2939. return 1;
  2940. }
  2941. static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
  2942. HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
  2943. {
  2944. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2945. .name = "Digital Mic Capture Switch",
  2946. .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
  2947. .count = 1,
  2948. .info = vt1716s_dmic_info,
  2949. .get = vt1716s_dmic_get,
  2950. .put = vt1716s_dmic_put,
  2951. },
  2952. {} /* end */
  2953. };
  2954. /* mono-out mixer elements */
  2955. static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
  2956. HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
  2957. { } /* end */
  2958. };
  2959. static const struct hda_verb vt1716S_init_verbs[] = {
  2960. /* Enable Boost Volume backdoor */
  2961. {0x1, 0xf8a, 0x80},
  2962. /* don't bybass mixer */
  2963. {0x1, 0xf88, 0xc0},
  2964. /* Enable mono output */
  2965. {0x1, 0xf90, 0x08},
  2966. { }
  2967. };
  2968. static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
  2969. {
  2970. struct via_spec *spec = codec->spec;
  2971. int imux_is_smixer;
  2972. unsigned int parm;
  2973. unsigned int mono_out, present;
  2974. /* SW0 (17h) = stereo mixer */
  2975. imux_is_smixer =
  2976. (snd_hda_codec_read(codec, 0x17, 0,
  2977. AC_VERB_GET_CONNECT_SEL, 0x00) == 5);
  2978. /* inputs */
  2979. /* PW 1/2/5 (1ah/1bh/1eh) */
  2980. parm = AC_PWRST_D3;
  2981. set_pin_power_state(codec, 0x1a, &parm);
  2982. set_pin_power_state(codec, 0x1b, &parm);
  2983. set_pin_power_state(codec, 0x1e, &parm);
  2984. if (imux_is_smixer)
  2985. parm = AC_PWRST_D0;
  2986. /* SW0 (17h), AIW0(13h) */
  2987. update_power_state(codec, 0x17, parm);
  2988. update_power_state(codec, 0x13, parm);
  2989. parm = AC_PWRST_D3;
  2990. set_pin_power_state(codec, 0x1e, &parm);
  2991. /* PW11 (22h) */
  2992. if (spec->dmic_enabled)
  2993. set_pin_power_state(codec, 0x22, &parm);
  2994. else
  2995. update_power_state(codec, 0x22, AC_PWRST_D3);
  2996. /* SW2(26h), AIW1(14h) */
  2997. update_power_state(codec, 0x26, parm);
  2998. update_power_state(codec, 0x14, parm);
  2999. /* outputs */
  3000. /* PW0 (19h), SW1 (18h), AOW1 (11h) */
  3001. parm = AC_PWRST_D3;
  3002. set_pin_power_state(codec, 0x19, &parm);
  3003. /* Smart 5.1 PW2(1bh) */
  3004. if (spec->smart51_enabled)
  3005. set_pin_power_state(codec, 0x1b, &parm);
  3006. update_power_state(codec, 0x18, parm);
  3007. update_power_state(codec, 0x11, parm);
  3008. /* PW7 (23h), SW3 (27h), AOW3 (25h) */
  3009. parm = AC_PWRST_D3;
  3010. set_pin_power_state(codec, 0x23, &parm);
  3011. /* Smart 5.1 PW1(1ah) */
  3012. if (spec->smart51_enabled)
  3013. set_pin_power_state(codec, 0x1a, &parm);
  3014. update_power_state(codec, 0x27, parm);
  3015. /* Smart 5.1 PW5(1eh) */
  3016. if (spec->smart51_enabled)
  3017. set_pin_power_state(codec, 0x1e, &parm);
  3018. update_power_state(codec, 0x25, parm);
  3019. /* Mono out */
  3020. /* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
  3021. present = snd_hda_jack_detect(codec, 0x1c);
  3022. if (present)
  3023. mono_out = 0;
  3024. else {
  3025. present = snd_hda_jack_detect(codec, 0x1d);
  3026. if (!spec->hp_independent_mode && present)
  3027. mono_out = 0;
  3028. else
  3029. mono_out = 1;
  3030. }
  3031. parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
  3032. update_power_state(codec, 0x28, parm);
  3033. update_power_state(codec, 0x29, parm);
  3034. update_power_state(codec, 0x2a, parm);
  3035. /* PW 3/4 (1ch/1dh) */
  3036. parm = AC_PWRST_D3;
  3037. set_pin_power_state(codec, 0x1c, &parm);
  3038. set_pin_power_state(codec, 0x1d, &parm);
  3039. /* HP Independent Mode, power on AOW3 */
  3040. if (spec->hp_independent_mode)
  3041. update_power_state(codec, 0x25, parm);
  3042. /* force to D0 for internal Speaker */
  3043. /* MW0 (16h), AOW0 (10h) */
  3044. update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
  3045. update_power_state(codec, 0x10, mono_out ? AC_PWRST_D0 : parm);
  3046. }
  3047. static int patch_vt1716S(struct hda_codec *codec)
  3048. {
  3049. struct via_spec *spec;
  3050. int err;
  3051. /* create a codec specific record */
  3052. spec = via_new_spec(codec);
  3053. if (spec == NULL)
  3054. return -ENOMEM;
  3055. spec->aa_mix_nid = 0x16;
  3056. override_mic_boost(codec, 0x1a, 0, 3, 40);
  3057. override_mic_boost(codec, 0x1e, 0, 3, 40);
  3058. /* automatic parse from the BIOS config */
  3059. err = via_parse_auto_config(codec);
  3060. if (err < 0) {
  3061. via_free(codec);
  3062. return err;
  3063. }
  3064. spec->init_verbs[spec->num_iverbs++] = vt1716S_init_verbs;
  3065. spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
  3066. spec->num_mixers++;
  3067. spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
  3068. codec->patch_ops = via_patch_ops;
  3069. spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
  3070. return 0;
  3071. }
  3072. /* for vt2002P */
  3073. static const struct hda_verb vt2002P_init_verbs[] = {
  3074. /* Class-D speaker related verbs */
  3075. {0x1, 0xfe0, 0x4},
  3076. {0x1, 0xfe9, 0x80},
  3077. {0x1, 0xfe2, 0x22},
  3078. /* Enable Boost Volume backdoor */
  3079. {0x1, 0xfb9, 0x24},
  3080. /* Enable AOW0 to MW9 */
  3081. {0x1, 0xfb8, 0x88},
  3082. { }
  3083. };
  3084. static const struct hda_verb vt1802_init_verbs[] = {
  3085. /* Enable Boost Volume backdoor */
  3086. {0x1, 0xfb9, 0x24},
  3087. /* Enable AOW0 to MW9 */
  3088. {0x1, 0xfb8, 0x88},
  3089. { }
  3090. };
  3091. static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
  3092. {
  3093. struct via_spec *spec = codec->spec;
  3094. int imux_is_smixer;
  3095. unsigned int parm;
  3096. unsigned int present;
  3097. /* MUX9 (1eh) = stereo mixer */
  3098. imux_is_smixer =
  3099. snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
  3100. /* inputs */
  3101. /* PW 5/6/7 (29h/2ah/2bh) */
  3102. parm = AC_PWRST_D3;
  3103. set_pin_power_state(codec, 0x29, &parm);
  3104. set_pin_power_state(codec, 0x2a, &parm);
  3105. set_pin_power_state(codec, 0x2b, &parm);
  3106. parm = AC_PWRST_D0;
  3107. /* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
  3108. update_power_state(codec, 0x1e, parm);
  3109. update_power_state(codec, 0x1f, parm);
  3110. update_power_state(codec, 0x10, parm);
  3111. update_power_state(codec, 0x11, parm);
  3112. /* outputs */
  3113. /* AOW0 (8h)*/
  3114. update_power_state(codec, 0x8, parm);
  3115. if (spec->codec_type == VT1802) {
  3116. /* PW4 (28h), MW4 (18h), MUX4(38h) */
  3117. parm = AC_PWRST_D3;
  3118. set_pin_power_state(codec, 0x28, &parm);
  3119. update_power_state(codec, 0x18, parm);
  3120. update_power_state(codec, 0x38, parm);
  3121. } else {
  3122. /* PW4 (26h), MW4 (1ch), MUX4(37h) */
  3123. parm = AC_PWRST_D3;
  3124. set_pin_power_state(codec, 0x26, &parm);
  3125. update_power_state(codec, 0x1c, parm);
  3126. update_power_state(codec, 0x37, parm);
  3127. }
  3128. if (spec->codec_type == VT1802) {
  3129. /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
  3130. parm = AC_PWRST_D3;
  3131. set_pin_power_state(codec, 0x25, &parm);
  3132. update_power_state(codec, 0x15, parm);
  3133. update_power_state(codec, 0x35, parm);
  3134. } else {
  3135. /* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
  3136. parm = AC_PWRST_D3;
  3137. set_pin_power_state(codec, 0x25, &parm);
  3138. update_power_state(codec, 0x19, parm);
  3139. update_power_state(codec, 0x35, parm);
  3140. }
  3141. if (spec->hp_independent_mode)
  3142. update_power_state(codec, 0x9, AC_PWRST_D0);
  3143. /* Class-D */
  3144. /* PW0 (24h), MW0(18h/14h), MUX0(34h) */
  3145. present = snd_hda_jack_detect(codec, 0x25);
  3146. parm = AC_PWRST_D3;
  3147. set_pin_power_state(codec, 0x24, &parm);
  3148. parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
  3149. if (spec->codec_type == VT1802)
  3150. update_power_state(codec, 0x14, parm);
  3151. else
  3152. update_power_state(codec, 0x18, parm);
  3153. update_power_state(codec, 0x34, parm);
  3154. /* Mono Out */
  3155. present = snd_hda_jack_detect(codec, 0x26);
  3156. parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
  3157. if (spec->codec_type == VT1802) {
  3158. /* PW15 (33h), MW8(1ch), MUX8(3ch) */
  3159. update_power_state(codec, 0x33, parm);
  3160. update_power_state(codec, 0x1c, parm);
  3161. update_power_state(codec, 0x3c, parm);
  3162. } else {
  3163. /* PW15 (31h), MW8(17h), MUX8(3bh) */
  3164. update_power_state(codec, 0x31, parm);
  3165. update_power_state(codec, 0x17, parm);
  3166. update_power_state(codec, 0x3b, parm);
  3167. }
  3168. /* MW9 (21h) */
  3169. if (imux_is_smixer || !is_aa_path_mute(codec))
  3170. update_power_state(codec, 0x21, AC_PWRST_D0);
  3171. else
  3172. update_power_state(codec, 0x21, AC_PWRST_D3);
  3173. }
  3174. /*
  3175. * pin fix-up
  3176. */
  3177. enum {
  3178. VIA_FIXUP_INTMIC_BOOST,
  3179. };
  3180. static void via_fixup_intmic_boost(struct hda_codec *codec,
  3181. const struct hda_fixup *fix, int action)
  3182. {
  3183. if (action == HDA_FIXUP_ACT_PRE_PROBE)
  3184. override_mic_boost(codec, 0x30, 0, 2, 40);
  3185. }
  3186. static const struct hda_fixup via_fixups[] = {
  3187. [VIA_FIXUP_INTMIC_BOOST] = {
  3188. .type = HDA_FIXUP_FUNC,
  3189. .v.func = via_fixup_intmic_boost,
  3190. },
  3191. };
  3192. static const struct snd_pci_quirk vt2002p_fixups[] = {
  3193. SND_PCI_QUIRK(0x1043, 0x8532, "Asus X202E", VIA_FIXUP_INTMIC_BOOST),
  3194. {}
  3195. };
  3196. /* patch for vt2002P */
  3197. static int patch_vt2002P(struct hda_codec *codec)
  3198. {
  3199. struct via_spec *spec;
  3200. int err;
  3201. /* create a codec specific record */
  3202. spec = via_new_spec(codec);
  3203. if (spec == NULL)
  3204. return -ENOMEM;
  3205. spec->aa_mix_nid = 0x21;
  3206. override_mic_boost(codec, 0x2b, 0, 3, 40);
  3207. override_mic_boost(codec, 0x29, 0, 3, 40);
  3208. add_secret_dac_path(codec);
  3209. snd_hda_pick_fixup(codec, NULL, vt2002p_fixups, via_fixups);
  3210. snd_hda_apply_fixup(codec, HDA_FIXUP_ACT_PRE_PROBE);
  3211. /* automatic parse from the BIOS config */
  3212. err = via_parse_auto_config(codec);
  3213. if (err < 0) {
  3214. via_free(codec);
  3215. return err;
  3216. }
  3217. if (spec->codec_type == VT1802)
  3218. spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
  3219. else
  3220. spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
  3221. codec->patch_ops = via_patch_ops;
  3222. spec->set_widgets_power_state = set_widgets_power_state_vt2002P;
  3223. return 0;
  3224. }
  3225. /* for vt1812 */
  3226. static const struct hda_verb vt1812_init_verbs[] = {
  3227. /* Enable Boost Volume backdoor */
  3228. {0x1, 0xfb9, 0x24},
  3229. /* Enable AOW0 to MW9 */
  3230. {0x1, 0xfb8, 0xa8},
  3231. { }
  3232. };
  3233. static void set_widgets_power_state_vt1812(struct hda_codec *codec)
  3234. {
  3235. struct via_spec *spec = codec->spec;
  3236. unsigned int parm;
  3237. unsigned int present;
  3238. /* inputs */
  3239. /* PW 5/6/7 (29h/2ah/2bh) */
  3240. parm = AC_PWRST_D3;
  3241. set_pin_power_state(codec, 0x29, &parm);
  3242. set_pin_power_state(codec, 0x2a, &parm);
  3243. set_pin_power_state(codec, 0x2b, &parm);
  3244. parm = AC_PWRST_D0;
  3245. /* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
  3246. update_power_state(codec, 0x1e, parm);
  3247. update_power_state(codec, 0x1f, parm);
  3248. update_power_state(codec, 0x10, parm);
  3249. update_power_state(codec, 0x11, parm);
  3250. /* outputs */
  3251. /* AOW0 (8h)*/
  3252. update_power_state(codec, 0x8, AC_PWRST_D0);
  3253. /* PW4 (28h), MW4 (18h), MUX4(38h) */
  3254. parm = AC_PWRST_D3;
  3255. set_pin_power_state(codec, 0x28, &parm);
  3256. update_power_state(codec, 0x18, parm);
  3257. update_power_state(codec, 0x38, parm);
  3258. /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
  3259. parm = AC_PWRST_D3;
  3260. set_pin_power_state(codec, 0x25, &parm);
  3261. update_power_state(codec, 0x15, parm);
  3262. update_power_state(codec, 0x35, parm);
  3263. if (spec->hp_independent_mode)
  3264. update_power_state(codec, 0x9, AC_PWRST_D0);
  3265. /* Internal Speaker */
  3266. /* PW0 (24h), MW0(14h), MUX0(34h) */
  3267. present = snd_hda_jack_detect(codec, 0x25);
  3268. parm = AC_PWRST_D3;
  3269. set_pin_power_state(codec, 0x24, &parm);
  3270. if (present) {
  3271. update_power_state(codec, 0x14, AC_PWRST_D3);
  3272. update_power_state(codec, 0x34, AC_PWRST_D3);
  3273. } else {
  3274. update_power_state(codec, 0x14, AC_PWRST_D0);
  3275. update_power_state(codec, 0x34, AC_PWRST_D0);
  3276. }
  3277. /* Mono Out */
  3278. /* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
  3279. present = snd_hda_jack_detect(codec, 0x28);
  3280. parm = AC_PWRST_D3;
  3281. set_pin_power_state(codec, 0x31, &parm);
  3282. if (present) {
  3283. update_power_state(codec, 0x1c, AC_PWRST_D3);
  3284. update_power_state(codec, 0x3c, AC_PWRST_D3);
  3285. update_power_state(codec, 0x3e, AC_PWRST_D3);
  3286. } else {
  3287. update_power_state(codec, 0x1c, AC_PWRST_D0);
  3288. update_power_state(codec, 0x3c, AC_PWRST_D0);
  3289. update_power_state(codec, 0x3e, AC_PWRST_D0);
  3290. }
  3291. /* PW15 (33h), MW15 (1dh), MUX15(3dh) */
  3292. parm = AC_PWRST_D3;
  3293. set_pin_power_state(codec, 0x33, &parm);
  3294. update_power_state(codec, 0x1d, parm);
  3295. update_power_state(codec, 0x3d, parm);
  3296. }
  3297. /* patch for vt1812 */
  3298. static int patch_vt1812(struct hda_codec *codec)
  3299. {
  3300. struct via_spec *spec;
  3301. int err;
  3302. /* create a codec specific record */
  3303. spec = via_new_spec(codec);
  3304. if (spec == NULL)
  3305. return -ENOMEM;
  3306. spec->aa_mix_nid = 0x21;
  3307. override_mic_boost(codec, 0x2b, 0, 3, 40);
  3308. override_mic_boost(codec, 0x29, 0, 3, 40);
  3309. add_secret_dac_path(codec);
  3310. /* automatic parse from the BIOS config */
  3311. err = via_parse_auto_config(codec);
  3312. if (err < 0) {
  3313. via_free(codec);
  3314. return err;
  3315. }
  3316. spec->init_verbs[spec->num_iverbs++] = vt1812_init_verbs;
  3317. codec->patch_ops = via_patch_ops;
  3318. spec->set_widgets_power_state = set_widgets_power_state_vt1812;
  3319. return 0;
  3320. }
  3321. /*
  3322. * patch entries
  3323. */
  3324. static const struct hda_codec_preset snd_hda_preset_via[] = {
  3325. { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
  3326. { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
  3327. { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
  3328. { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
  3329. { .id = 0x1106e710, .name = "VT1709 10-Ch",
  3330. .patch = patch_vt1709},
  3331. { .id = 0x1106e711, .name = "VT1709 10-Ch",
  3332. .patch = patch_vt1709},
  3333. { .id = 0x1106e712, .name = "VT1709 10-Ch",
  3334. .patch = patch_vt1709},
  3335. { .id = 0x1106e713, .name = "VT1709 10-Ch",
  3336. .patch = patch_vt1709},
  3337. { .id = 0x1106e714, .name = "VT1709 6-Ch",
  3338. .patch = patch_vt1709},
  3339. { .id = 0x1106e715, .name = "VT1709 6-Ch",
  3340. .patch = patch_vt1709},
  3341. { .id = 0x1106e716, .name = "VT1709 6-Ch",
  3342. .patch = patch_vt1709},
  3343. { .id = 0x1106e717, .name = "VT1709 6-Ch",
  3344. .patch = patch_vt1709},
  3345. { .id = 0x1106e720, .name = "VT1708B 8-Ch",
  3346. .patch = patch_vt1708B},
  3347. { .id = 0x1106e721, .name = "VT1708B 8-Ch",
  3348. .patch = patch_vt1708B},
  3349. { .id = 0x1106e722, .name = "VT1708B 8-Ch",
  3350. .patch = patch_vt1708B},
  3351. { .id = 0x1106e723, .name = "VT1708B 8-Ch",
  3352. .patch = patch_vt1708B},
  3353. { .id = 0x1106e724, .name = "VT1708B 4-Ch",
  3354. .patch = patch_vt1708B},
  3355. { .id = 0x1106e725, .name = "VT1708B 4-Ch",
  3356. .patch = patch_vt1708B},
  3357. { .id = 0x1106e726, .name = "VT1708B 4-Ch",
  3358. .patch = patch_vt1708B},
  3359. { .id = 0x1106e727, .name = "VT1708B 4-Ch",
  3360. .patch = patch_vt1708B},
  3361. { .id = 0x11060397, .name = "VT1708S",
  3362. .patch = patch_vt1708S},
  3363. { .id = 0x11061397, .name = "VT1708S",
  3364. .patch = patch_vt1708S},
  3365. { .id = 0x11062397, .name = "VT1708S",
  3366. .patch = patch_vt1708S},
  3367. { .id = 0x11063397, .name = "VT1708S",
  3368. .patch = patch_vt1708S},
  3369. { .id = 0x11064397, .name = "VT1705",
  3370. .patch = patch_vt1708S},
  3371. { .id = 0x11065397, .name = "VT1708S",
  3372. .patch = patch_vt1708S},
  3373. { .id = 0x11066397, .name = "VT1708S",
  3374. .patch = patch_vt1708S},
  3375. { .id = 0x11067397, .name = "VT1708S",
  3376. .patch = patch_vt1708S},
  3377. { .id = 0x11060398, .name = "VT1702",
  3378. .patch = patch_vt1702},
  3379. { .id = 0x11061398, .name = "VT1702",
  3380. .patch = patch_vt1702},
  3381. { .id = 0x11062398, .name = "VT1702",
  3382. .patch = patch_vt1702},
  3383. { .id = 0x11063398, .name = "VT1702",
  3384. .patch = patch_vt1702},
  3385. { .id = 0x11064398, .name = "VT1702",
  3386. .patch = patch_vt1702},
  3387. { .id = 0x11065398, .name = "VT1702",
  3388. .patch = patch_vt1702},
  3389. { .id = 0x11066398, .name = "VT1702",
  3390. .patch = patch_vt1702},
  3391. { .id = 0x11067398, .name = "VT1702",
  3392. .patch = patch_vt1702},
  3393. { .id = 0x11060428, .name = "VT1718S",
  3394. .patch = patch_vt1718S},
  3395. { .id = 0x11064428, .name = "VT1718S",
  3396. .patch = patch_vt1718S},
  3397. { .id = 0x11060441, .name = "VT2020",
  3398. .patch = patch_vt1718S},
  3399. { .id = 0x11064441, .name = "VT1828S",
  3400. .patch = patch_vt1718S},
  3401. { .id = 0x11060433, .name = "VT1716S",
  3402. .patch = patch_vt1716S},
  3403. { .id = 0x1106a721, .name = "VT1716S",
  3404. .patch = patch_vt1716S},
  3405. { .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
  3406. { .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
  3407. { .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
  3408. { .id = 0x11060440, .name = "VT1818S",
  3409. .patch = patch_vt1708S},
  3410. { .id = 0x11060446, .name = "VT1802",
  3411. .patch = patch_vt2002P},
  3412. { .id = 0x11068446, .name = "VT1802",
  3413. .patch = patch_vt2002P},
  3414. {} /* terminator */
  3415. };
  3416. MODULE_ALIAS("snd-hda-codec-id:1106*");
  3417. static struct hda_codec_preset_list via_list = {
  3418. .preset = snd_hda_preset_via,
  3419. .owner = THIS_MODULE,
  3420. };
  3421. MODULE_LICENSE("GPL");
  3422. MODULE_DESCRIPTION("VIA HD-audio codec");
  3423. static int __init patch_via_init(void)
  3424. {
  3425. return snd_hda_add_codec_preset(&via_list);
  3426. }
  3427. static void __exit patch_via_exit(void)
  3428. {
  3429. snd_hda_delete_codec_preset(&via_list);
  3430. }
  3431. module_init(patch_via_init)
  3432. module_exit(patch_via_exit)