patch_via.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927
  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_jack.h"
  55. /* Pin Widget NID */
  56. #define VT1708_HP_PIN_NID 0x20
  57. #define VT1708_CD_PIN_NID 0x24
  58. enum VIA_HDA_CODEC {
  59. UNKNOWN = -1,
  60. VT1708,
  61. VT1709_10CH,
  62. VT1709_6CH,
  63. VT1708B_8CH,
  64. VT1708B_4CH,
  65. VT1708S,
  66. VT1708BCE,
  67. VT1702,
  68. VT1718S,
  69. VT1716S,
  70. VT2002P,
  71. VT1812,
  72. VT1802,
  73. CODEC_TYPES,
  74. };
  75. #define VT2002P_COMPATIBLE(spec) \
  76. ((spec)->codec_type == VT2002P ||\
  77. (spec)->codec_type == VT1812 ||\
  78. (spec)->codec_type == VT1802)
  79. #define MAX_NID_PATH_DEPTH 5
  80. /* output-path: DAC -> ... -> pin
  81. * idx[] contains the source index number of the next widget;
  82. * e.g. idx[0] is the index of the DAC selected by path[1] widget
  83. * multi[] indicates whether it's a selector widget with multi-connectors
  84. * (i.e. the connection selection is mandatory)
  85. * vol_ctl and mute_ctl contains the NIDs for the assigned mixers
  86. */
  87. struct nid_path {
  88. int depth;
  89. hda_nid_t path[MAX_NID_PATH_DEPTH];
  90. unsigned char idx[MAX_NID_PATH_DEPTH];
  91. unsigned char multi[MAX_NID_PATH_DEPTH];
  92. unsigned int vol_ctl;
  93. unsigned int mute_ctl;
  94. };
  95. /* input-path */
  96. struct via_input {
  97. hda_nid_t pin; /* input-pin or aa-mix */
  98. int adc_idx; /* ADC index to be used */
  99. int mux_idx; /* MUX index (if any) */
  100. const char *label; /* input-source label */
  101. };
  102. #define VIA_MAX_ADCS 3
  103. enum {
  104. STREAM_MULTI_OUT = (1 << 0),
  105. STREAM_INDEP_HP = (1 << 1),
  106. };
  107. struct via_spec {
  108. /* codec parameterization */
  109. const struct snd_kcontrol_new *mixers[6];
  110. unsigned int num_mixers;
  111. const struct hda_verb *init_verbs[5];
  112. unsigned int num_iverbs;
  113. char stream_name_analog[32];
  114. char stream_name_hp[32];
  115. const struct hda_pcm_stream *stream_analog_playback;
  116. const struct hda_pcm_stream *stream_analog_capture;
  117. char stream_name_digital[32];
  118. const struct hda_pcm_stream *stream_digital_playback;
  119. const struct hda_pcm_stream *stream_digital_capture;
  120. /* playback */
  121. struct hda_multi_out multiout;
  122. hda_nid_t slave_dig_outs[2];
  123. hda_nid_t hp_dac_nid;
  124. hda_nid_t speaker_dac_nid;
  125. int hp_indep_shared; /* indep HP-DAC is shared with side ch */
  126. int opened_streams; /* STREAM_* bits */
  127. int active_streams; /* STREAM_* bits */
  128. int aamix_mode; /* loopback is enabled for output-path? */
  129. /* Output-paths:
  130. * There are different output-paths depending on the setup.
  131. * out_path, hp_path and speaker_path are primary paths. If both
  132. * direct DAC and aa-loopback routes are available, these contain
  133. * the former paths. Meanwhile *_mix_path contain the paths with
  134. * loopback mixer. (Since the loopback is only for front channel,
  135. * no out_mix_path for surround channels.)
  136. * The HP output has another path, hp_indep_path, which is used in
  137. * the independent-HP mode.
  138. */
  139. struct nid_path out_path[HDA_SIDE + 1];
  140. struct nid_path out_mix_path;
  141. struct nid_path hp_path;
  142. struct nid_path hp_mix_path;
  143. struct nid_path hp_indep_path;
  144. struct nid_path speaker_path;
  145. struct nid_path speaker_mix_path;
  146. /* capture */
  147. unsigned int num_adc_nids;
  148. hda_nid_t adc_nids[VIA_MAX_ADCS];
  149. hda_nid_t mux_nids[VIA_MAX_ADCS];
  150. hda_nid_t aa_mix_nid;
  151. hda_nid_t dig_in_nid;
  152. /* capture source */
  153. bool dyn_adc_switch;
  154. int num_inputs;
  155. struct via_input inputs[AUTO_CFG_MAX_INS + 1];
  156. unsigned int cur_mux[VIA_MAX_ADCS];
  157. /* dynamic DAC switching */
  158. unsigned int cur_dac_stream_tag;
  159. unsigned int cur_dac_format;
  160. unsigned int cur_hp_stream_tag;
  161. unsigned int cur_hp_format;
  162. /* dynamic ADC switching */
  163. hda_nid_t cur_adc;
  164. unsigned int cur_adc_stream_tag;
  165. unsigned int cur_adc_format;
  166. /* PCM information */
  167. struct hda_pcm pcm_rec[3];
  168. /* dynamic controls, init_verbs and input_mux */
  169. struct auto_pin_cfg autocfg;
  170. struct snd_array kctls;
  171. hda_nid_t private_dac_nids[AUTO_CFG_MAX_OUTS];
  172. /* HP mode source */
  173. unsigned int hp_independent_mode;
  174. unsigned int dmic_enabled;
  175. unsigned int no_pin_power_ctl;
  176. enum VIA_HDA_CODEC codec_type;
  177. /* analog low-power control */
  178. bool alc_mode;
  179. /* smart51 setup */
  180. unsigned int smart51_nums;
  181. hda_nid_t smart51_pins[2];
  182. int smart51_idxs[2];
  183. const char *smart51_labels[2];
  184. unsigned int smart51_enabled;
  185. /* work to check hp jack state */
  186. struct hda_codec *codec;
  187. struct delayed_work vt1708_hp_work;
  188. int hp_work_active;
  189. int vt1708_jack_detect;
  190. int vt1708_hp_present;
  191. void (*set_widgets_power_state)(struct hda_codec *codec);
  192. struct hda_loopback_check loopback;
  193. int num_loopbacks;
  194. struct hda_amp_list loopback_list[8];
  195. /* bind capture-volume */
  196. struct hda_bind_ctls *bind_cap_vol;
  197. struct hda_bind_ctls *bind_cap_sw;
  198. struct mutex config_mutex;
  199. };
  200. static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec);
  201. static struct via_spec * via_new_spec(struct hda_codec *codec)
  202. {
  203. struct via_spec *spec;
  204. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  205. if (spec == NULL)
  206. return NULL;
  207. mutex_init(&spec->config_mutex);
  208. codec->spec = spec;
  209. spec->codec = codec;
  210. spec->codec_type = get_codec_type(codec);
  211. /* VT1708BCE & VT1708S are almost same */
  212. if (spec->codec_type == VT1708BCE)
  213. spec->codec_type = VT1708S;
  214. return spec;
  215. }
  216. static enum VIA_HDA_CODEC get_codec_type(struct hda_codec *codec)
  217. {
  218. u32 vendor_id = codec->vendor_id;
  219. u16 ven_id = vendor_id >> 16;
  220. u16 dev_id = vendor_id & 0xffff;
  221. enum VIA_HDA_CODEC codec_type;
  222. /* get codec type */
  223. if (ven_id != 0x1106)
  224. codec_type = UNKNOWN;
  225. else if (dev_id >= 0x1708 && dev_id <= 0x170b)
  226. codec_type = VT1708;
  227. else if (dev_id >= 0xe710 && dev_id <= 0xe713)
  228. codec_type = VT1709_10CH;
  229. else if (dev_id >= 0xe714 && dev_id <= 0xe717)
  230. codec_type = VT1709_6CH;
  231. else if (dev_id >= 0xe720 && dev_id <= 0xe723) {
  232. codec_type = VT1708B_8CH;
  233. if (snd_hda_param_read(codec, 0x16, AC_PAR_CONNLIST_LEN) == 0x7)
  234. codec_type = VT1708BCE;
  235. } else if (dev_id >= 0xe724 && dev_id <= 0xe727)
  236. codec_type = VT1708B_4CH;
  237. else if ((dev_id & 0xfff) == 0x397
  238. && (dev_id >> 12) < 8)
  239. codec_type = VT1708S;
  240. else if ((dev_id & 0xfff) == 0x398
  241. && (dev_id >> 12) < 8)
  242. codec_type = VT1702;
  243. else if ((dev_id & 0xfff) == 0x428
  244. && (dev_id >> 12) < 8)
  245. codec_type = VT1718S;
  246. else if (dev_id == 0x0433 || dev_id == 0xa721)
  247. codec_type = VT1716S;
  248. else if (dev_id == 0x0441 || dev_id == 0x4441)
  249. codec_type = VT1718S;
  250. else if (dev_id == 0x0438 || dev_id == 0x4438)
  251. codec_type = VT2002P;
  252. else if (dev_id == 0x0448)
  253. codec_type = VT1812;
  254. else if (dev_id == 0x0440)
  255. codec_type = VT1708S;
  256. else if ((dev_id & 0xfff) == 0x446)
  257. codec_type = VT1802;
  258. else
  259. codec_type = UNKNOWN;
  260. return codec_type;
  261. };
  262. #define VIA_JACK_EVENT 0x20
  263. #define VIA_HP_EVENT 0x01
  264. #define VIA_GPIO_EVENT 0x02
  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_conn_list(codec, mix_nid, NULL);
  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. present = snd_hda_jack_detect(codec, spec->autocfg.hp_pins[0]);
  1484. if (spec->smart51_enabled)
  1485. nums = spec->autocfg.line_outs + spec->smart51_nums;
  1486. else
  1487. nums = spec->autocfg.line_outs;
  1488. toggle_output_mutes(codec, nums, spec->autocfg.line_out_pins, present);
  1489. via_line_automute(codec, present);
  1490. }
  1491. static void via_gpio_control(struct hda_codec *codec)
  1492. {
  1493. unsigned int gpio_data;
  1494. unsigned int vol_counter;
  1495. unsigned int vol;
  1496. unsigned int master_vol;
  1497. struct via_spec *spec = codec->spec;
  1498. gpio_data = snd_hda_codec_read(codec, codec->afg, 0,
  1499. AC_VERB_GET_GPIO_DATA, 0) & 0x03;
  1500. vol_counter = (snd_hda_codec_read(codec, codec->afg, 0,
  1501. 0xF84, 0) & 0x3F0000) >> 16;
  1502. vol = vol_counter & 0x1F;
  1503. master_vol = snd_hda_codec_read(codec, 0x1A, 0,
  1504. AC_VERB_GET_AMP_GAIN_MUTE,
  1505. AC_AMP_GET_INPUT);
  1506. if (gpio_data == 0x02) {
  1507. /* unmute line out */
  1508. snd_hda_set_pin_ctl(codec, spec->autocfg.line_out_pins[0],
  1509. PIN_OUT);
  1510. if (vol_counter & 0x20) {
  1511. /* decrease volume */
  1512. if (vol > master_vol)
  1513. vol = master_vol;
  1514. snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT,
  1515. 0, HDA_AMP_VOLMASK,
  1516. master_vol-vol);
  1517. } else {
  1518. /* increase volume */
  1519. snd_hda_codec_amp_stereo(codec, 0x1A, HDA_INPUT, 0,
  1520. HDA_AMP_VOLMASK,
  1521. ((master_vol+vol) > 0x2A) ? 0x2A :
  1522. (master_vol+vol));
  1523. }
  1524. } else if (!(gpio_data & 0x02)) {
  1525. /* mute line out */
  1526. snd_hda_set_pin_ctl(codec, spec->autocfg.line_out_pins[0], 0);
  1527. }
  1528. }
  1529. /* unsolicited event for jack sensing */
  1530. static void via_unsol_event(struct hda_codec *codec,
  1531. unsigned int res)
  1532. {
  1533. res >>= 26;
  1534. res = snd_hda_jack_get_action(codec, res);
  1535. if (res & VIA_JACK_EVENT)
  1536. set_widgets_power_state(codec);
  1537. res &= ~VIA_JACK_EVENT;
  1538. if (res == VIA_HP_EVENT || res == VIA_LINE_EVENT)
  1539. via_hp_automute(codec);
  1540. else if (res == VIA_GPIO_EVENT)
  1541. via_gpio_control(codec);
  1542. snd_hda_jack_report_sync(codec);
  1543. }
  1544. #ifdef CONFIG_PM
  1545. static int via_suspend(struct hda_codec *codec, pm_message_t state)
  1546. {
  1547. struct via_spec *spec = codec->spec;
  1548. vt1708_stop_hp_work(spec);
  1549. return 0;
  1550. }
  1551. #endif
  1552. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1553. static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
  1554. {
  1555. struct via_spec *spec = codec->spec;
  1556. return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
  1557. }
  1558. #endif
  1559. /*
  1560. */
  1561. static int via_init(struct hda_codec *codec);
  1562. static const struct hda_codec_ops via_patch_ops = {
  1563. .build_controls = via_build_controls,
  1564. .build_pcms = via_build_pcms,
  1565. .init = via_init,
  1566. .free = via_free,
  1567. .unsol_event = via_unsol_event,
  1568. #ifdef CONFIG_PM
  1569. .suspend = via_suspend,
  1570. #endif
  1571. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1572. .check_power_status = via_check_power_status,
  1573. #endif
  1574. };
  1575. static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
  1576. {
  1577. struct via_spec *spec = codec->spec;
  1578. int i;
  1579. for (i = 0; i < spec->multiout.num_dacs; i++) {
  1580. if (spec->multiout.dac_nids[i] == dac)
  1581. return false;
  1582. }
  1583. if (spec->hp_dac_nid == dac)
  1584. return false;
  1585. return true;
  1586. }
  1587. static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
  1588. hda_nid_t target_dac, int with_aa_mix,
  1589. struct nid_path *path, int depth)
  1590. {
  1591. struct via_spec *spec = codec->spec;
  1592. hda_nid_t conn[8];
  1593. int i, nums;
  1594. if (nid == spec->aa_mix_nid) {
  1595. if (!with_aa_mix)
  1596. return false;
  1597. with_aa_mix = 2; /* mark aa-mix is included */
  1598. }
  1599. nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
  1600. for (i = 0; i < nums; i++) {
  1601. if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
  1602. continue;
  1603. if (conn[i] == target_dac || is_empty_dac(codec, conn[i])) {
  1604. /* aa-mix is requested but not included? */
  1605. if (!(spec->aa_mix_nid && with_aa_mix == 1))
  1606. goto found;
  1607. }
  1608. }
  1609. if (depth >= MAX_NID_PATH_DEPTH)
  1610. return false;
  1611. for (i = 0; i < nums; i++) {
  1612. unsigned int type;
  1613. type = get_wcaps_type(get_wcaps(codec, conn[i]));
  1614. if (type == AC_WID_AUD_OUT)
  1615. continue;
  1616. if (__parse_output_path(codec, conn[i], target_dac,
  1617. with_aa_mix, path, depth + 1))
  1618. goto found;
  1619. }
  1620. return false;
  1621. found:
  1622. path->path[path->depth] = conn[i];
  1623. path->idx[path->depth] = i;
  1624. if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
  1625. path->multi[path->depth] = 1;
  1626. path->depth++;
  1627. return true;
  1628. }
  1629. static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
  1630. hda_nid_t target_dac, int with_aa_mix,
  1631. struct nid_path *path)
  1632. {
  1633. if (__parse_output_path(codec, nid, target_dac, with_aa_mix, path, 1)) {
  1634. path->path[path->depth] = nid;
  1635. path->depth++;
  1636. snd_printdd("output-path: depth=%d, %02x/%02x/%02x/%02x/%02x\n",
  1637. path->depth, path->path[0], path->path[1],
  1638. path->path[2], path->path[3], path->path[4]);
  1639. return true;
  1640. }
  1641. return false;
  1642. }
  1643. static int via_auto_fill_dac_nids(struct hda_codec *codec)
  1644. {
  1645. struct via_spec *spec = codec->spec;
  1646. const struct auto_pin_cfg *cfg = &spec->autocfg;
  1647. int i, dac_num;
  1648. hda_nid_t nid;
  1649. spec->multiout.dac_nids = spec->private_dac_nids;
  1650. dac_num = 0;
  1651. for (i = 0; i < cfg->line_outs; i++) {
  1652. hda_nid_t dac = 0;
  1653. nid = cfg->line_out_pins[i];
  1654. if (!nid)
  1655. continue;
  1656. if (parse_output_path(codec, nid, 0, 0, &spec->out_path[i]))
  1657. dac = spec->out_path[i].path[0];
  1658. if (!i && parse_output_path(codec, nid, dac, 1,
  1659. &spec->out_mix_path))
  1660. dac = spec->out_mix_path.path[0];
  1661. if (dac) {
  1662. spec->private_dac_nids[i] = dac;
  1663. dac_num++;
  1664. }
  1665. }
  1666. if (!spec->out_path[0].depth && spec->out_mix_path.depth) {
  1667. spec->out_path[0] = spec->out_mix_path;
  1668. spec->out_mix_path.depth = 0;
  1669. }
  1670. spec->multiout.num_dacs = dac_num;
  1671. return 0;
  1672. }
  1673. static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
  1674. int chs, bool check_dac, struct nid_path *path)
  1675. {
  1676. struct via_spec *spec = codec->spec;
  1677. char name[32];
  1678. hda_nid_t dac, pin, sel, nid;
  1679. int err;
  1680. dac = check_dac ? path->path[0] : 0;
  1681. pin = path->path[path->depth - 1];
  1682. sel = path->depth > 1 ? path->path[1] : 0;
  1683. if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
  1684. nid = dac;
  1685. else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
  1686. nid = pin;
  1687. else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
  1688. nid = sel;
  1689. else
  1690. nid = 0;
  1691. if (nid) {
  1692. sprintf(name, "%s Playback Volume", pfx);
  1693. err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
  1694. HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
  1695. if (err < 0)
  1696. return err;
  1697. path->vol_ctl = nid;
  1698. }
  1699. if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
  1700. nid = dac;
  1701. else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
  1702. nid = pin;
  1703. else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
  1704. nid = sel;
  1705. else
  1706. nid = 0;
  1707. if (nid) {
  1708. sprintf(name, "%s Playback Switch", pfx);
  1709. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
  1710. HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
  1711. if (err < 0)
  1712. return err;
  1713. path->mute_ctl = nid;
  1714. }
  1715. return 0;
  1716. }
  1717. static void mangle_smart51(struct hda_codec *codec)
  1718. {
  1719. struct via_spec *spec = codec->spec;
  1720. struct auto_pin_cfg *cfg = &spec->autocfg;
  1721. struct auto_pin_cfg_item *ins = cfg->inputs;
  1722. int i, j, nums, attr;
  1723. int pins[AUTO_CFG_MAX_INS];
  1724. for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
  1725. nums = 0;
  1726. for (i = 0; i < cfg->num_inputs; i++) {
  1727. unsigned int def;
  1728. if (ins[i].type > AUTO_PIN_LINE_IN)
  1729. continue;
  1730. def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
  1731. if (snd_hda_get_input_pin_attr(def) != attr)
  1732. continue;
  1733. for (j = 0; j < nums; j++)
  1734. if (ins[pins[j]].type < ins[i].type) {
  1735. memmove(pins + j + 1, pins + j,
  1736. (nums - j) * sizeof(int));
  1737. break;
  1738. }
  1739. pins[j] = i;
  1740. nums++;
  1741. }
  1742. if (cfg->line_outs + nums < 3)
  1743. continue;
  1744. for (i = 0; i < nums; i++) {
  1745. hda_nid_t pin = ins[pins[i]].pin;
  1746. spec->smart51_pins[spec->smart51_nums++] = pin;
  1747. cfg->line_out_pins[cfg->line_outs++] = pin;
  1748. if (cfg->line_outs == 3)
  1749. break;
  1750. }
  1751. return;
  1752. }
  1753. }
  1754. static void copy_path_mixer_ctls(struct nid_path *dst, struct nid_path *src)
  1755. {
  1756. dst->vol_ctl = src->vol_ctl;
  1757. dst->mute_ctl = src->mute_ctl;
  1758. }
  1759. /* add playback controls from the parsed DAC table */
  1760. static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
  1761. {
  1762. struct via_spec *spec = codec->spec;
  1763. struct auto_pin_cfg *cfg = &spec->autocfg;
  1764. struct nid_path *path;
  1765. static const char * const chname[4] = {
  1766. "Front", "Surround", "C/LFE", "Side"
  1767. };
  1768. int i, idx, err;
  1769. int old_line_outs;
  1770. /* check smart51 */
  1771. old_line_outs = cfg->line_outs;
  1772. if (cfg->line_outs == 1)
  1773. mangle_smart51(codec);
  1774. err = via_auto_fill_dac_nids(codec);
  1775. if (err < 0)
  1776. return err;
  1777. if (spec->multiout.num_dacs < 3) {
  1778. spec->smart51_nums = 0;
  1779. cfg->line_outs = old_line_outs;
  1780. }
  1781. for (i = 0; i < cfg->line_outs; i++) {
  1782. hda_nid_t pin, dac;
  1783. pin = cfg->line_out_pins[i];
  1784. dac = spec->multiout.dac_nids[i];
  1785. if (!pin || !dac)
  1786. continue;
  1787. path = spec->out_path + i;
  1788. if (i == HDA_CLFE) {
  1789. err = create_ch_ctls(codec, "Center", 1, true, path);
  1790. if (err < 0)
  1791. return err;
  1792. err = create_ch_ctls(codec, "LFE", 2, true, path);
  1793. if (err < 0)
  1794. return err;
  1795. } else {
  1796. const char *pfx = chname[i];
  1797. if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
  1798. cfg->line_outs == 1)
  1799. pfx = "Speaker";
  1800. err = create_ch_ctls(codec, pfx, 3, true, path);
  1801. if (err < 0)
  1802. return err;
  1803. }
  1804. if (path != spec->out_path + i)
  1805. copy_path_mixer_ctls(&spec->out_path[i], path);
  1806. if (path == spec->out_path && spec->out_mix_path.depth)
  1807. copy_path_mixer_ctls(&spec->out_mix_path, path);
  1808. }
  1809. idx = get_connection_index(codec, spec->aa_mix_nid,
  1810. spec->multiout.dac_nids[0]);
  1811. if (idx >= 0) {
  1812. /* add control to mixer */
  1813. const char *name;
  1814. name = spec->out_mix_path.depth ?
  1815. "PCM Loopback Playback Volume" : "PCM Playback Volume";
  1816. err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
  1817. HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
  1818. idx, HDA_INPUT));
  1819. if (err < 0)
  1820. return err;
  1821. name = spec->out_mix_path.depth ?
  1822. "PCM Loopback Playback Switch" : "PCM Playback Switch";
  1823. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
  1824. HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
  1825. idx, HDA_INPUT));
  1826. if (err < 0)
  1827. return err;
  1828. }
  1829. cfg->line_outs = old_line_outs;
  1830. return 0;
  1831. }
  1832. static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
  1833. {
  1834. struct via_spec *spec = codec->spec;
  1835. struct nid_path *path;
  1836. bool check_dac;
  1837. int i, err;
  1838. if (!pin)
  1839. return 0;
  1840. if (!parse_output_path(codec, pin, 0, 0, &spec->hp_indep_path)) {
  1841. for (i = HDA_SIDE; i >= HDA_CLFE; i--) {
  1842. if (i < spec->multiout.num_dacs &&
  1843. parse_output_path(codec, pin,
  1844. spec->multiout.dac_nids[i], 0,
  1845. &spec->hp_indep_path)) {
  1846. spec->hp_indep_shared = i;
  1847. break;
  1848. }
  1849. }
  1850. }
  1851. if (spec->hp_indep_path.depth) {
  1852. spec->hp_dac_nid = spec->hp_indep_path.path[0];
  1853. if (!spec->hp_indep_shared)
  1854. spec->hp_path = spec->hp_indep_path;
  1855. }
  1856. /* optionally check front-path w/o AA-mix */
  1857. if (!spec->hp_path.depth)
  1858. parse_output_path(codec, pin,
  1859. spec->multiout.dac_nids[HDA_FRONT], 0,
  1860. &spec->hp_path);
  1861. if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
  1862. 1, &spec->hp_mix_path) && !spec->hp_path.depth)
  1863. return 0;
  1864. if (spec->hp_path.depth) {
  1865. path = &spec->hp_path;
  1866. check_dac = true;
  1867. } else {
  1868. path = &spec->hp_mix_path;
  1869. check_dac = false;
  1870. }
  1871. err = create_ch_ctls(codec, "Headphone", 3, check_dac, path);
  1872. if (err < 0)
  1873. return err;
  1874. if (check_dac)
  1875. copy_path_mixer_ctls(&spec->hp_mix_path, path);
  1876. else
  1877. copy_path_mixer_ctls(&spec->hp_path, path);
  1878. if (spec->hp_indep_path.depth)
  1879. copy_path_mixer_ctls(&spec->hp_indep_path, path);
  1880. return 0;
  1881. }
  1882. static int via_auto_create_speaker_ctls(struct hda_codec *codec)
  1883. {
  1884. struct via_spec *spec = codec->spec;
  1885. struct nid_path *path;
  1886. bool check_dac;
  1887. hda_nid_t pin, dac = 0;
  1888. int err;
  1889. pin = spec->autocfg.speaker_pins[0];
  1890. if (!spec->autocfg.speaker_outs || !pin)
  1891. return 0;
  1892. if (parse_output_path(codec, pin, 0, 0, &spec->speaker_path))
  1893. dac = spec->speaker_path.path[0];
  1894. if (!dac)
  1895. parse_output_path(codec, pin,
  1896. spec->multiout.dac_nids[HDA_FRONT], 0,
  1897. &spec->speaker_path);
  1898. if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
  1899. 1, &spec->speaker_mix_path) && !dac)
  1900. return 0;
  1901. /* no AA-path for front? */
  1902. if (!spec->out_mix_path.depth && spec->speaker_mix_path.depth)
  1903. dac = 0;
  1904. spec->speaker_dac_nid = dac;
  1905. spec->multiout.extra_out_nid[0] = dac;
  1906. if (dac) {
  1907. path = &spec->speaker_path;
  1908. check_dac = true;
  1909. } else {
  1910. path = &spec->speaker_mix_path;
  1911. check_dac = false;
  1912. }
  1913. err = create_ch_ctls(codec, "Speaker", 3, check_dac, path);
  1914. if (err < 0)
  1915. return err;
  1916. if (check_dac)
  1917. copy_path_mixer_ctls(&spec->speaker_mix_path, path);
  1918. else
  1919. copy_path_mixer_ctls(&spec->speaker_path, path);
  1920. return 0;
  1921. }
  1922. #define via_aamix_ctl_info via_pin_power_ctl_info
  1923. static int via_aamix_ctl_get(struct snd_kcontrol *kcontrol,
  1924. struct snd_ctl_elem_value *ucontrol)
  1925. {
  1926. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1927. struct via_spec *spec = codec->spec;
  1928. ucontrol->value.enumerated.item[0] = spec->aamix_mode;
  1929. return 0;
  1930. }
  1931. static void update_aamix_paths(struct hda_codec *codec, int do_mix,
  1932. struct nid_path *nomix, struct nid_path *mix)
  1933. {
  1934. if (do_mix) {
  1935. activate_output_path(codec, nomix, false, false);
  1936. activate_output_path(codec, mix, true, false);
  1937. } else {
  1938. activate_output_path(codec, mix, false, false);
  1939. activate_output_path(codec, nomix, true, false);
  1940. }
  1941. }
  1942. static int via_aamix_ctl_put(struct snd_kcontrol *kcontrol,
  1943. struct snd_ctl_elem_value *ucontrol)
  1944. {
  1945. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1946. struct via_spec *spec = codec->spec;
  1947. unsigned int val = ucontrol->value.enumerated.item[0];
  1948. if (val == spec->aamix_mode)
  1949. return 0;
  1950. spec->aamix_mode = val;
  1951. /* update front path */
  1952. update_aamix_paths(codec, val, &spec->out_path[0], &spec->out_mix_path);
  1953. /* update HP path */
  1954. if (!spec->hp_independent_mode) {
  1955. update_aamix_paths(codec, val, &spec->hp_path,
  1956. &spec->hp_mix_path);
  1957. }
  1958. /* update speaker path */
  1959. update_aamix_paths(codec, val, &spec->speaker_path,
  1960. &spec->speaker_mix_path);
  1961. return 1;
  1962. }
  1963. static const struct snd_kcontrol_new via_aamix_ctl_enum = {
  1964. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1965. .name = "Loopback Mixing",
  1966. .info = via_aamix_ctl_info,
  1967. .get = via_aamix_ctl_get,
  1968. .put = via_aamix_ctl_put,
  1969. };
  1970. static int via_auto_create_loopback_switch(struct hda_codec *codec)
  1971. {
  1972. struct via_spec *spec = codec->spec;
  1973. if (!spec->aa_mix_nid)
  1974. return 0; /* no loopback switching available */
  1975. if (!(spec->out_mix_path.depth || spec->hp_mix_path.depth ||
  1976. spec->speaker_path.depth))
  1977. return 0; /* no loopback switching available */
  1978. if (!via_clone_control(spec, &via_aamix_ctl_enum))
  1979. return -ENOMEM;
  1980. return 0;
  1981. }
  1982. /* look for ADCs */
  1983. static int via_fill_adcs(struct hda_codec *codec)
  1984. {
  1985. struct via_spec *spec = codec->spec;
  1986. hda_nid_t nid = codec->start_nid;
  1987. int i;
  1988. for (i = 0; i < codec->num_nodes; i++, nid++) {
  1989. unsigned int wcaps = get_wcaps(codec, nid);
  1990. if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
  1991. continue;
  1992. if (wcaps & AC_WCAP_DIGITAL)
  1993. continue;
  1994. if (!(wcaps & AC_WCAP_CONN_LIST))
  1995. continue;
  1996. if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
  1997. return -ENOMEM;
  1998. spec->adc_nids[spec->num_adc_nids++] = nid;
  1999. }
  2000. return 0;
  2001. }
  2002. /* input-src control */
  2003. static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
  2004. struct snd_ctl_elem_info *uinfo)
  2005. {
  2006. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2007. struct via_spec *spec = codec->spec;
  2008. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  2009. uinfo->count = 1;
  2010. uinfo->value.enumerated.items = spec->num_inputs;
  2011. if (uinfo->value.enumerated.item >= spec->num_inputs)
  2012. uinfo->value.enumerated.item = spec->num_inputs - 1;
  2013. strcpy(uinfo->value.enumerated.name,
  2014. spec->inputs[uinfo->value.enumerated.item].label);
  2015. return 0;
  2016. }
  2017. static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
  2018. struct snd_ctl_elem_value *ucontrol)
  2019. {
  2020. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2021. struct via_spec *spec = codec->spec;
  2022. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  2023. ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
  2024. return 0;
  2025. }
  2026. static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
  2027. struct snd_ctl_elem_value *ucontrol)
  2028. {
  2029. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2030. struct via_spec *spec = codec->spec;
  2031. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  2032. hda_nid_t mux;
  2033. int cur;
  2034. cur = ucontrol->value.enumerated.item[0];
  2035. if (cur < 0 || cur >= spec->num_inputs)
  2036. return -EINVAL;
  2037. if (spec->cur_mux[idx] == cur)
  2038. return 0;
  2039. spec->cur_mux[idx] = cur;
  2040. if (spec->dyn_adc_switch) {
  2041. int adc_idx = spec->inputs[cur].adc_idx;
  2042. mux = spec->mux_nids[adc_idx];
  2043. via_dyn_adc_pcm_resetup(codec, cur);
  2044. } else {
  2045. mux = spec->mux_nids[idx];
  2046. if (snd_BUG_ON(!mux))
  2047. return -EINVAL;
  2048. }
  2049. if (mux) {
  2050. /* switch to D0 beofre change index */
  2051. update_power_state(codec, mux, AC_PWRST_D0);
  2052. snd_hda_codec_write(codec, mux, 0,
  2053. AC_VERB_SET_CONNECT_SEL,
  2054. spec->inputs[cur].mux_idx);
  2055. }
  2056. /* update jack power state */
  2057. set_widgets_power_state(codec);
  2058. return 0;
  2059. }
  2060. static const struct snd_kcontrol_new via_input_src_ctl = {
  2061. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2062. /* The multiple "Capture Source" controls confuse alsamixer
  2063. * So call somewhat different..
  2064. */
  2065. /* .name = "Capture Source", */
  2066. .name = "Input Source",
  2067. .info = via_mux_enum_info,
  2068. .get = via_mux_enum_get,
  2069. .put = via_mux_enum_put,
  2070. };
  2071. static int create_input_src_ctls(struct hda_codec *codec, int count)
  2072. {
  2073. struct via_spec *spec = codec->spec;
  2074. struct snd_kcontrol_new *knew;
  2075. if (spec->num_inputs <= 1 || !count)
  2076. return 0; /* no need for single src */
  2077. knew = via_clone_control(spec, &via_input_src_ctl);
  2078. if (!knew)
  2079. return -ENOMEM;
  2080. knew->count = count;
  2081. return 0;
  2082. }
  2083. /* add the powersave loopback-list entry */
  2084. static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
  2085. {
  2086. struct hda_amp_list *list;
  2087. if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
  2088. return;
  2089. list = spec->loopback_list + spec->num_loopbacks;
  2090. list->nid = mix;
  2091. list->dir = HDA_INPUT;
  2092. list->idx = idx;
  2093. spec->num_loopbacks++;
  2094. spec->loopback.amplist = spec->loopback_list;
  2095. }
  2096. static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
  2097. hda_nid_t dst)
  2098. {
  2099. return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
  2100. }
  2101. /* add the input-route to the given pin */
  2102. static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
  2103. {
  2104. struct via_spec *spec = codec->spec;
  2105. int c, idx;
  2106. spec->inputs[spec->num_inputs].adc_idx = -1;
  2107. spec->inputs[spec->num_inputs].pin = pin;
  2108. for (c = 0; c < spec->num_adc_nids; c++) {
  2109. if (spec->mux_nids[c]) {
  2110. idx = get_connection_index(codec, spec->mux_nids[c],
  2111. pin);
  2112. if (idx < 0)
  2113. continue;
  2114. spec->inputs[spec->num_inputs].mux_idx = idx;
  2115. } else {
  2116. if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
  2117. continue;
  2118. }
  2119. spec->inputs[spec->num_inputs].adc_idx = c;
  2120. /* Can primary ADC satisfy all inputs? */
  2121. if (!spec->dyn_adc_switch &&
  2122. spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
  2123. snd_printd(KERN_INFO
  2124. "via: dynamic ADC switching enabled\n");
  2125. spec->dyn_adc_switch = 1;
  2126. }
  2127. return true;
  2128. }
  2129. return false;
  2130. }
  2131. static int get_mux_nids(struct hda_codec *codec);
  2132. /* parse input-routes; fill ADCs, MUXs and input-src entries */
  2133. static int parse_analog_inputs(struct hda_codec *codec)
  2134. {
  2135. struct via_spec *spec = codec->spec;
  2136. const struct auto_pin_cfg *cfg = &spec->autocfg;
  2137. int i, err;
  2138. err = via_fill_adcs(codec);
  2139. if (err < 0)
  2140. return err;
  2141. err = get_mux_nids(codec);
  2142. if (err < 0)
  2143. return err;
  2144. /* fill all input-routes */
  2145. for (i = 0; i < cfg->num_inputs; i++) {
  2146. if (add_input_route(codec, cfg->inputs[i].pin))
  2147. spec->inputs[spec->num_inputs++].label =
  2148. hda_get_autocfg_input_label(codec, cfg, i);
  2149. }
  2150. /* check for internal loopback recording */
  2151. if (spec->aa_mix_nid &&
  2152. add_input_route(codec, spec->aa_mix_nid))
  2153. spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
  2154. return 0;
  2155. }
  2156. /* create analog-loopback volume/switch controls */
  2157. static int create_loopback_ctls(struct hda_codec *codec)
  2158. {
  2159. struct via_spec *spec = codec->spec;
  2160. const struct auto_pin_cfg *cfg = &spec->autocfg;
  2161. const char *prev_label = NULL;
  2162. int type_idx = 0;
  2163. int i, j, err, idx;
  2164. if (!spec->aa_mix_nid)
  2165. return 0;
  2166. for (i = 0; i < cfg->num_inputs; i++) {
  2167. hda_nid_t pin = cfg->inputs[i].pin;
  2168. const char *label = hda_get_autocfg_input_label(codec, cfg, i);
  2169. if (prev_label && !strcmp(label, prev_label))
  2170. type_idx++;
  2171. else
  2172. type_idx = 0;
  2173. prev_label = label;
  2174. idx = get_connection_index(codec, spec->aa_mix_nid, pin);
  2175. if (idx >= 0) {
  2176. err = via_new_analog_input(spec, label, type_idx,
  2177. idx, spec->aa_mix_nid);
  2178. if (err < 0)
  2179. return err;
  2180. add_loopback_list(spec, spec->aa_mix_nid, idx);
  2181. }
  2182. /* remember the label for smart51 control */
  2183. for (j = 0; j < spec->smart51_nums; j++) {
  2184. if (spec->smart51_pins[j] == pin) {
  2185. spec->smart51_idxs[j] = idx;
  2186. spec->smart51_labels[j] = label;
  2187. break;
  2188. }
  2189. }
  2190. }
  2191. return 0;
  2192. }
  2193. /* create mic-boost controls (if present) */
  2194. static int create_mic_boost_ctls(struct hda_codec *codec)
  2195. {
  2196. struct via_spec *spec = codec->spec;
  2197. const struct auto_pin_cfg *cfg = &spec->autocfg;
  2198. const char *prev_label = NULL;
  2199. int type_idx = 0;
  2200. int i, err;
  2201. for (i = 0; i < cfg->num_inputs; i++) {
  2202. hda_nid_t pin = cfg->inputs[i].pin;
  2203. unsigned int caps;
  2204. const char *label;
  2205. char name[32];
  2206. if (cfg->inputs[i].type != AUTO_PIN_MIC)
  2207. continue;
  2208. caps = query_amp_caps(codec, pin, HDA_INPUT);
  2209. if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
  2210. continue;
  2211. label = hda_get_autocfg_input_label(codec, cfg, i);
  2212. if (prev_label && !strcmp(label, prev_label))
  2213. type_idx++;
  2214. else
  2215. type_idx = 0;
  2216. prev_label = label;
  2217. snprintf(name, sizeof(name), "%s Boost Volume", label);
  2218. err = __via_add_control(spec, VIA_CTL_WIDGET_VOL, name, type_idx,
  2219. HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
  2220. if (err < 0)
  2221. return err;
  2222. }
  2223. return 0;
  2224. }
  2225. /* create capture and input-src controls for multiple streams */
  2226. static int create_multi_adc_ctls(struct hda_codec *codec)
  2227. {
  2228. struct via_spec *spec = codec->spec;
  2229. int i, err;
  2230. /* create capture mixer elements */
  2231. for (i = 0; i < spec->num_adc_nids; i++) {
  2232. hda_nid_t adc = spec->adc_nids[i];
  2233. err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
  2234. "Capture Volume", i,
  2235. HDA_COMPOSE_AMP_VAL(adc, 3, 0,
  2236. HDA_INPUT));
  2237. if (err < 0)
  2238. return err;
  2239. err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
  2240. "Capture Switch", i,
  2241. HDA_COMPOSE_AMP_VAL(adc, 3, 0,
  2242. HDA_INPUT));
  2243. if (err < 0)
  2244. return err;
  2245. }
  2246. /* input-source control */
  2247. for (i = 0; i < spec->num_adc_nids; i++)
  2248. if (!spec->mux_nids[i])
  2249. break;
  2250. err = create_input_src_ctls(codec, i);
  2251. if (err < 0)
  2252. return err;
  2253. return 0;
  2254. }
  2255. /* bind capture volume/switch */
  2256. static struct snd_kcontrol_new via_bind_cap_vol_ctl =
  2257. HDA_BIND_VOL("Capture Volume", 0);
  2258. static struct snd_kcontrol_new via_bind_cap_sw_ctl =
  2259. HDA_BIND_SW("Capture Switch", 0);
  2260. static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
  2261. struct hda_ctl_ops *ops)
  2262. {
  2263. struct hda_bind_ctls *ctl;
  2264. int i;
  2265. ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
  2266. if (!ctl)
  2267. return -ENOMEM;
  2268. ctl->ops = ops;
  2269. for (i = 0; i < spec->num_adc_nids; i++)
  2270. ctl->values[i] =
  2271. HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
  2272. *ctl_ret = ctl;
  2273. return 0;
  2274. }
  2275. /* create capture and input-src controls for dynamic ADC-switch case */
  2276. static int create_dyn_adc_ctls(struct hda_codec *codec)
  2277. {
  2278. struct via_spec *spec = codec->spec;
  2279. struct snd_kcontrol_new *knew;
  2280. int err;
  2281. /* set up the bind capture ctls */
  2282. err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
  2283. if (err < 0)
  2284. return err;
  2285. err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
  2286. if (err < 0)
  2287. return err;
  2288. /* create capture mixer elements */
  2289. knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
  2290. if (!knew)
  2291. return -ENOMEM;
  2292. knew->private_value = (long)spec->bind_cap_vol;
  2293. knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
  2294. if (!knew)
  2295. return -ENOMEM;
  2296. knew->private_value = (long)spec->bind_cap_sw;
  2297. /* input-source control */
  2298. err = create_input_src_ctls(codec, 1);
  2299. if (err < 0)
  2300. return err;
  2301. return 0;
  2302. }
  2303. /* parse and create capture-related stuff */
  2304. static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
  2305. {
  2306. struct via_spec *spec = codec->spec;
  2307. int err;
  2308. err = parse_analog_inputs(codec);
  2309. if (err < 0)
  2310. return err;
  2311. if (spec->dyn_adc_switch)
  2312. err = create_dyn_adc_ctls(codec);
  2313. else
  2314. err = create_multi_adc_ctls(codec);
  2315. if (err < 0)
  2316. return err;
  2317. err = create_loopback_ctls(codec);
  2318. if (err < 0)
  2319. return err;
  2320. err = create_mic_boost_ctls(codec);
  2321. if (err < 0)
  2322. return err;
  2323. return 0;
  2324. }
  2325. static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
  2326. {
  2327. unsigned int def_conf;
  2328. unsigned char seqassoc;
  2329. def_conf = snd_hda_codec_get_pincfg(codec, nid);
  2330. seqassoc = (unsigned char) get_defcfg_association(def_conf);
  2331. seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
  2332. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
  2333. && (seqassoc == 0xf0 || seqassoc == 0xff)) {
  2334. def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
  2335. snd_hda_codec_set_pincfg(codec, nid, def_conf);
  2336. }
  2337. return;
  2338. }
  2339. static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
  2340. struct snd_ctl_elem_value *ucontrol)
  2341. {
  2342. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2343. struct via_spec *spec = codec->spec;
  2344. if (spec->codec_type != VT1708)
  2345. return 0;
  2346. ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
  2347. return 0;
  2348. }
  2349. static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
  2350. struct snd_ctl_elem_value *ucontrol)
  2351. {
  2352. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2353. struct via_spec *spec = codec->spec;
  2354. int val;
  2355. if (spec->codec_type != VT1708)
  2356. return 0;
  2357. val = !!ucontrol->value.integer.value[0];
  2358. if (spec->vt1708_jack_detect == val)
  2359. return 0;
  2360. spec->vt1708_jack_detect = val;
  2361. if (spec->vt1708_jack_detect &&
  2362. snd_hda_get_bool_hint(codec, "analog_loopback_hp_detect") != 1) {
  2363. mute_aa_path(codec, 1);
  2364. notify_aa_path_ctls(codec);
  2365. }
  2366. via_hp_automute(codec);
  2367. vt1708_update_hp_work(spec);
  2368. return 1;
  2369. }
  2370. static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
  2371. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2372. .name = "Jack Detect",
  2373. .count = 1,
  2374. .info = snd_ctl_boolean_mono_info,
  2375. .get = vt1708_jack_detect_get,
  2376. .put = vt1708_jack_detect_put,
  2377. };
  2378. static void fill_dig_outs(struct hda_codec *codec);
  2379. static void fill_dig_in(struct hda_codec *codec);
  2380. static int via_parse_auto_config(struct hda_codec *codec)
  2381. {
  2382. struct via_spec *spec = codec->spec;
  2383. int err;
  2384. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  2385. if (err < 0)
  2386. return err;
  2387. if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
  2388. return -EINVAL;
  2389. err = via_auto_create_multi_out_ctls(codec);
  2390. if (err < 0)
  2391. return err;
  2392. err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
  2393. if (err < 0)
  2394. return err;
  2395. err = via_auto_create_speaker_ctls(codec);
  2396. if (err < 0)
  2397. return err;
  2398. err = via_auto_create_loopback_switch(codec);
  2399. if (err < 0)
  2400. return err;
  2401. err = via_auto_create_analog_input_ctls(codec);
  2402. if (err < 0)
  2403. return err;
  2404. spec->multiout.max_channels = spec->multiout.num_dacs * 2;
  2405. fill_dig_outs(codec);
  2406. fill_dig_in(codec);
  2407. if (spec->kctls.list)
  2408. spec->mixers[spec->num_mixers++] = spec->kctls.list;
  2409. if (spec->hp_dac_nid && spec->hp_mix_path.depth) {
  2410. err = via_hp_build(codec);
  2411. if (err < 0)
  2412. return err;
  2413. }
  2414. err = via_smart51_build(codec);
  2415. if (err < 0)
  2416. return err;
  2417. /* assign slave outs */
  2418. if (spec->slave_dig_outs[0])
  2419. codec->slave_dig_outs = spec->slave_dig_outs;
  2420. return 1;
  2421. }
  2422. static void via_auto_init_dig_outs(struct hda_codec *codec)
  2423. {
  2424. struct via_spec *spec = codec->spec;
  2425. if (spec->multiout.dig_out_nid)
  2426. init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
  2427. if (spec->slave_dig_outs[0])
  2428. init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
  2429. }
  2430. static void via_auto_init_dig_in(struct hda_codec *codec)
  2431. {
  2432. struct via_spec *spec = codec->spec;
  2433. if (!spec->dig_in_nid)
  2434. return;
  2435. snd_hda_set_pin_ctl(codec, spec->autocfg.dig_in_pin, PIN_IN);
  2436. }
  2437. /* initialize the unsolicited events */
  2438. static void via_auto_init_unsol_event(struct hda_codec *codec)
  2439. {
  2440. struct via_spec *spec = codec->spec;
  2441. struct auto_pin_cfg *cfg = &spec->autocfg;
  2442. unsigned int ev;
  2443. int i;
  2444. if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
  2445. snd_hda_jack_detect_enable(codec, cfg->hp_pins[0],
  2446. VIA_HP_EVENT | VIA_JACK_EVENT);
  2447. if (cfg->speaker_pins[0])
  2448. ev = VIA_LINE_EVENT;
  2449. else
  2450. ev = 0;
  2451. for (i = 0; i < cfg->line_outs; i++) {
  2452. if (cfg->line_out_pins[i] &&
  2453. is_jack_detectable(codec, cfg->line_out_pins[i]))
  2454. snd_hda_jack_detect_enable(codec, cfg->line_out_pins[i],
  2455. ev | VIA_JACK_EVENT);
  2456. }
  2457. for (i = 0; i < cfg->num_inputs; i++) {
  2458. if (is_jack_detectable(codec, cfg->inputs[i].pin))
  2459. snd_hda_jack_detect_enable(codec, cfg->inputs[i].pin,
  2460. VIA_JACK_EVENT);
  2461. }
  2462. }
  2463. static int via_init(struct hda_codec *codec)
  2464. {
  2465. struct via_spec *spec = codec->spec;
  2466. int i;
  2467. for (i = 0; i < spec->num_iverbs; i++)
  2468. snd_hda_sequence_write(codec, spec->init_verbs[i]);
  2469. /* init power states */
  2470. set_widgets_power_state(codec);
  2471. __analog_low_current_mode(codec, true);
  2472. via_auto_init_multi_out(codec);
  2473. via_auto_init_hp_out(codec);
  2474. via_auto_init_speaker_out(codec);
  2475. via_auto_init_analog_input(codec);
  2476. via_auto_init_dig_outs(codec);
  2477. via_auto_init_dig_in(codec);
  2478. via_auto_init_unsol_event(codec);
  2479. via_hp_automute(codec);
  2480. vt1708_update_hp_work(spec);
  2481. snd_hda_jack_report_sync(codec);
  2482. return 0;
  2483. }
  2484. static void vt1708_update_hp_jack_state(struct work_struct *work)
  2485. {
  2486. struct via_spec *spec = container_of(work, struct via_spec,
  2487. vt1708_hp_work.work);
  2488. if (spec->codec_type != VT1708)
  2489. return;
  2490. snd_hda_jack_set_dirty_all(spec->codec);
  2491. /* if jack state toggled */
  2492. if (spec->vt1708_hp_present
  2493. != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
  2494. spec->vt1708_hp_present ^= 1;
  2495. via_hp_automute(spec->codec);
  2496. }
  2497. if (spec->vt1708_jack_detect)
  2498. schedule_delayed_work(&spec->vt1708_hp_work,
  2499. msecs_to_jiffies(100));
  2500. }
  2501. static int get_mux_nids(struct hda_codec *codec)
  2502. {
  2503. struct via_spec *spec = codec->spec;
  2504. hda_nid_t nid, conn[8];
  2505. unsigned int type;
  2506. int i, n;
  2507. for (i = 0; i < spec->num_adc_nids; i++) {
  2508. nid = spec->adc_nids[i];
  2509. while (nid) {
  2510. type = get_wcaps_type(get_wcaps(codec, nid));
  2511. if (type == AC_WID_PIN)
  2512. break;
  2513. n = snd_hda_get_connections(codec, nid, conn,
  2514. ARRAY_SIZE(conn));
  2515. if (n <= 0)
  2516. break;
  2517. if (n > 1) {
  2518. spec->mux_nids[i] = nid;
  2519. break;
  2520. }
  2521. nid = conn[0];
  2522. }
  2523. }
  2524. return 0;
  2525. }
  2526. static int patch_vt1708(struct hda_codec *codec)
  2527. {
  2528. struct via_spec *spec;
  2529. int err;
  2530. /* create a codec specific record */
  2531. spec = via_new_spec(codec);
  2532. if (spec == NULL)
  2533. return -ENOMEM;
  2534. spec->aa_mix_nid = 0x17;
  2535. /* Add HP and CD pin config connect bit re-config action */
  2536. vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
  2537. vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
  2538. /* automatic parse from the BIOS config */
  2539. err = via_parse_auto_config(codec);
  2540. if (err < 0) {
  2541. via_free(codec);
  2542. return err;
  2543. }
  2544. /* add jack detect on/off control */
  2545. if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
  2546. return -ENOMEM;
  2547. /* disable 32bit format on VT1708 */
  2548. if (codec->vendor_id == 0x11061708)
  2549. spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
  2550. spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
  2551. codec->patch_ops = via_patch_ops;
  2552. INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
  2553. return 0;
  2554. }
  2555. static int patch_vt1709(struct hda_codec *codec)
  2556. {
  2557. struct via_spec *spec;
  2558. int err;
  2559. /* create a codec specific record */
  2560. spec = via_new_spec(codec);
  2561. if (spec == NULL)
  2562. return -ENOMEM;
  2563. spec->aa_mix_nid = 0x18;
  2564. err = via_parse_auto_config(codec);
  2565. if (err < 0) {
  2566. via_free(codec);
  2567. return err;
  2568. }
  2569. codec->patch_ops = via_patch_ops;
  2570. return 0;
  2571. }
  2572. static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
  2573. {
  2574. struct via_spec *spec = codec->spec;
  2575. int imux_is_smixer;
  2576. unsigned int parm;
  2577. int is_8ch = 0;
  2578. if ((spec->codec_type != VT1708B_4CH) &&
  2579. (codec->vendor_id != 0x11064397))
  2580. is_8ch = 1;
  2581. /* SW0 (17h) = stereo mixer */
  2582. imux_is_smixer =
  2583. (snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
  2584. == ((spec->codec_type == VT1708S) ? 5 : 0));
  2585. /* inputs */
  2586. /* PW 1/2/5 (1ah/1bh/1eh) */
  2587. parm = AC_PWRST_D3;
  2588. set_pin_power_state(codec, 0x1a, &parm);
  2589. set_pin_power_state(codec, 0x1b, &parm);
  2590. set_pin_power_state(codec, 0x1e, &parm);
  2591. if (imux_is_smixer)
  2592. parm = AC_PWRST_D0;
  2593. /* SW0 (17h), AIW 0/1 (13h/14h) */
  2594. update_power_state(codec, 0x17, parm);
  2595. update_power_state(codec, 0x13, parm);
  2596. update_power_state(codec, 0x14, parm);
  2597. /* outputs */
  2598. /* PW0 (19h), SW1 (18h), AOW1 (11h) */
  2599. parm = AC_PWRST_D3;
  2600. set_pin_power_state(codec, 0x19, &parm);
  2601. if (spec->smart51_enabled)
  2602. set_pin_power_state(codec, 0x1b, &parm);
  2603. update_power_state(codec, 0x18, parm);
  2604. update_power_state(codec, 0x11, parm);
  2605. /* PW6 (22h), SW2 (26h), AOW2 (24h) */
  2606. if (is_8ch) {
  2607. parm = AC_PWRST_D3;
  2608. set_pin_power_state(codec, 0x22, &parm);
  2609. if (spec->smart51_enabled)
  2610. set_pin_power_state(codec, 0x1a, &parm);
  2611. update_power_state(codec, 0x26, parm);
  2612. update_power_state(codec, 0x24, parm);
  2613. } else if (codec->vendor_id == 0x11064397) {
  2614. /* PW7(23h), SW2(27h), AOW2(25h) */
  2615. parm = AC_PWRST_D3;
  2616. set_pin_power_state(codec, 0x23, &parm);
  2617. if (spec->smart51_enabled)
  2618. set_pin_power_state(codec, 0x1a, &parm);
  2619. update_power_state(codec, 0x27, parm);
  2620. update_power_state(codec, 0x25, parm);
  2621. }
  2622. /* PW 3/4/7 (1ch/1dh/23h) */
  2623. parm = AC_PWRST_D3;
  2624. /* force to D0 for internal Speaker */
  2625. set_pin_power_state(codec, 0x1c, &parm);
  2626. set_pin_power_state(codec, 0x1d, &parm);
  2627. if (is_8ch)
  2628. set_pin_power_state(codec, 0x23, &parm);
  2629. /* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
  2630. update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
  2631. update_power_state(codec, 0x10, parm);
  2632. if (is_8ch) {
  2633. update_power_state(codec, 0x25, parm);
  2634. update_power_state(codec, 0x27, parm);
  2635. } else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
  2636. update_power_state(codec, 0x25, parm);
  2637. }
  2638. static int patch_vt1708S(struct hda_codec *codec);
  2639. static int patch_vt1708B(struct hda_codec *codec)
  2640. {
  2641. struct via_spec *spec;
  2642. int err;
  2643. if (get_codec_type(codec) == VT1708BCE)
  2644. return patch_vt1708S(codec);
  2645. /* create a codec specific record */
  2646. spec = via_new_spec(codec);
  2647. if (spec == NULL)
  2648. return -ENOMEM;
  2649. spec->aa_mix_nid = 0x16;
  2650. /* automatic parse from the BIOS config */
  2651. err = via_parse_auto_config(codec);
  2652. if (err < 0) {
  2653. via_free(codec);
  2654. return err;
  2655. }
  2656. codec->patch_ops = via_patch_ops;
  2657. spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
  2658. return 0;
  2659. }
  2660. /* Patch for VT1708S */
  2661. static const struct hda_verb vt1708S_init_verbs[] = {
  2662. /* Enable Mic Boost Volume backdoor */
  2663. {0x1, 0xf98, 0x1},
  2664. /* don't bybass mixer */
  2665. {0x1, 0xf88, 0xc0},
  2666. { }
  2667. };
  2668. /* fill out digital output widgets; one for master and one for slave outputs */
  2669. static void fill_dig_outs(struct hda_codec *codec)
  2670. {
  2671. struct via_spec *spec = codec->spec;
  2672. int i;
  2673. for (i = 0; i < spec->autocfg.dig_outs; i++) {
  2674. hda_nid_t nid;
  2675. int conn;
  2676. nid = spec->autocfg.dig_out_pins[i];
  2677. if (!nid)
  2678. continue;
  2679. conn = snd_hda_get_connections(codec, nid, &nid, 1);
  2680. if (conn < 1)
  2681. continue;
  2682. if (!spec->multiout.dig_out_nid)
  2683. spec->multiout.dig_out_nid = nid;
  2684. else {
  2685. spec->slave_dig_outs[0] = nid;
  2686. break; /* at most two dig outs */
  2687. }
  2688. }
  2689. }
  2690. static void fill_dig_in(struct hda_codec *codec)
  2691. {
  2692. struct via_spec *spec = codec->spec;
  2693. hda_nid_t dig_nid;
  2694. int i, err;
  2695. if (!spec->autocfg.dig_in_pin)
  2696. return;
  2697. dig_nid = codec->start_nid;
  2698. for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
  2699. unsigned int wcaps = get_wcaps(codec, dig_nid);
  2700. if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
  2701. continue;
  2702. if (!(wcaps & AC_WCAP_DIGITAL))
  2703. continue;
  2704. if (!(wcaps & AC_WCAP_CONN_LIST))
  2705. continue;
  2706. err = get_connection_index(codec, dig_nid,
  2707. spec->autocfg.dig_in_pin);
  2708. if (err >= 0) {
  2709. spec->dig_in_nid = dig_nid;
  2710. break;
  2711. }
  2712. }
  2713. }
  2714. static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
  2715. int offset, int num_steps, int step_size)
  2716. {
  2717. snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
  2718. (offset << AC_AMPCAP_OFFSET_SHIFT) |
  2719. (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
  2720. (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
  2721. (0 << AC_AMPCAP_MUTE_SHIFT));
  2722. }
  2723. static int patch_vt1708S(struct hda_codec *codec)
  2724. {
  2725. struct via_spec *spec;
  2726. int err;
  2727. /* create a codec specific record */
  2728. spec = via_new_spec(codec);
  2729. if (spec == NULL)
  2730. return -ENOMEM;
  2731. spec->aa_mix_nid = 0x16;
  2732. override_mic_boost(codec, 0x1a, 0, 3, 40);
  2733. override_mic_boost(codec, 0x1e, 0, 3, 40);
  2734. /* automatic parse from the BIOS config */
  2735. err = via_parse_auto_config(codec);
  2736. if (err < 0) {
  2737. via_free(codec);
  2738. return err;
  2739. }
  2740. spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
  2741. codec->patch_ops = via_patch_ops;
  2742. /* correct names for VT1708BCE */
  2743. if (get_codec_type(codec) == VT1708BCE) {
  2744. kfree(codec->chip_name);
  2745. codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
  2746. snprintf(codec->bus->card->mixername,
  2747. sizeof(codec->bus->card->mixername),
  2748. "%s %s", codec->vendor_name, codec->chip_name);
  2749. }
  2750. /* correct names for VT1705 */
  2751. if (codec->vendor_id == 0x11064397) {
  2752. kfree(codec->chip_name);
  2753. codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
  2754. snprintf(codec->bus->card->mixername,
  2755. sizeof(codec->bus->card->mixername),
  2756. "%s %s", codec->vendor_name, codec->chip_name);
  2757. }
  2758. spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
  2759. return 0;
  2760. }
  2761. /* Patch for VT1702 */
  2762. static const struct hda_verb vt1702_init_verbs[] = {
  2763. /* mixer enable */
  2764. {0x1, 0xF88, 0x3},
  2765. /* GPIO 0~2 */
  2766. {0x1, 0xF82, 0x3F},
  2767. { }
  2768. };
  2769. static void set_widgets_power_state_vt1702(struct hda_codec *codec)
  2770. {
  2771. int imux_is_smixer =
  2772. snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
  2773. unsigned int parm;
  2774. /* inputs */
  2775. /* PW 1/2/5 (14h/15h/18h) */
  2776. parm = AC_PWRST_D3;
  2777. set_pin_power_state(codec, 0x14, &parm);
  2778. set_pin_power_state(codec, 0x15, &parm);
  2779. set_pin_power_state(codec, 0x18, &parm);
  2780. if (imux_is_smixer)
  2781. parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
  2782. /* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
  2783. update_power_state(codec, 0x13, parm);
  2784. update_power_state(codec, 0x12, parm);
  2785. update_power_state(codec, 0x1f, parm);
  2786. update_power_state(codec, 0x20, parm);
  2787. /* outputs */
  2788. /* PW 3/4 (16h/17h) */
  2789. parm = AC_PWRST_D3;
  2790. set_pin_power_state(codec, 0x17, &parm);
  2791. set_pin_power_state(codec, 0x16, &parm);
  2792. /* MW0 (1ah), AOW 0/1 (10h/1dh) */
  2793. update_power_state(codec, 0x1a, imux_is_smixer ? AC_PWRST_D0 : parm);
  2794. update_power_state(codec, 0x10, parm);
  2795. update_power_state(codec, 0x1d, parm);
  2796. }
  2797. static int patch_vt1702(struct hda_codec *codec)
  2798. {
  2799. struct via_spec *spec;
  2800. int err;
  2801. /* create a codec specific record */
  2802. spec = via_new_spec(codec);
  2803. if (spec == NULL)
  2804. return -ENOMEM;
  2805. spec->aa_mix_nid = 0x1a;
  2806. /* limit AA path volume to 0 dB */
  2807. snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
  2808. (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
  2809. (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
  2810. (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
  2811. (1 << AC_AMPCAP_MUTE_SHIFT));
  2812. /* automatic parse from the BIOS config */
  2813. err = via_parse_auto_config(codec);
  2814. if (err < 0) {
  2815. via_free(codec);
  2816. return err;
  2817. }
  2818. spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
  2819. codec->patch_ops = via_patch_ops;
  2820. spec->set_widgets_power_state = set_widgets_power_state_vt1702;
  2821. return 0;
  2822. }
  2823. /* Patch for VT1718S */
  2824. static const struct hda_verb vt1718S_init_verbs[] = {
  2825. /* Enable MW0 adjust Gain 5 */
  2826. {0x1, 0xfb2, 0x10},
  2827. /* Enable Boost Volume backdoor */
  2828. {0x1, 0xf88, 0x8},
  2829. { }
  2830. };
  2831. static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
  2832. {
  2833. struct via_spec *spec = codec->spec;
  2834. int imux_is_smixer;
  2835. unsigned int parm;
  2836. /* MUX6 (1eh) = stereo mixer */
  2837. imux_is_smixer =
  2838. snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
  2839. /* inputs */
  2840. /* PW 5/6/7 (29h/2ah/2bh) */
  2841. parm = AC_PWRST_D3;
  2842. set_pin_power_state(codec, 0x29, &parm);
  2843. set_pin_power_state(codec, 0x2a, &parm);
  2844. set_pin_power_state(codec, 0x2b, &parm);
  2845. if (imux_is_smixer)
  2846. parm = AC_PWRST_D0;
  2847. /* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
  2848. update_power_state(codec, 0x1e, parm);
  2849. update_power_state(codec, 0x1f, parm);
  2850. update_power_state(codec, 0x10, parm);
  2851. update_power_state(codec, 0x11, parm);
  2852. /* outputs */
  2853. /* PW3 (27h), MW2 (1ah), AOW3 (bh) */
  2854. parm = AC_PWRST_D3;
  2855. set_pin_power_state(codec, 0x27, &parm);
  2856. update_power_state(codec, 0x1a, parm);
  2857. update_power_state(codec, 0xb, parm);
  2858. /* PW2 (26h), AOW2 (ah) */
  2859. parm = AC_PWRST_D3;
  2860. set_pin_power_state(codec, 0x26, &parm);
  2861. if (spec->smart51_enabled)
  2862. set_pin_power_state(codec, 0x2b, &parm);
  2863. update_power_state(codec, 0xa, parm);
  2864. /* PW0 (24h), AOW0 (8h) */
  2865. parm = AC_PWRST_D3;
  2866. set_pin_power_state(codec, 0x24, &parm);
  2867. if (!spec->hp_independent_mode) /* check for redirected HP */
  2868. set_pin_power_state(codec, 0x28, &parm);
  2869. update_power_state(codec, 0x8, parm);
  2870. /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
  2871. update_power_state(codec, 0x21, imux_is_smixer ? AC_PWRST_D0 : parm);
  2872. /* PW1 (25h), AOW1 (9h) */
  2873. parm = AC_PWRST_D3;
  2874. set_pin_power_state(codec, 0x25, &parm);
  2875. if (spec->smart51_enabled)
  2876. set_pin_power_state(codec, 0x2a, &parm);
  2877. update_power_state(codec, 0x9, parm);
  2878. if (spec->hp_independent_mode) {
  2879. /* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
  2880. parm = AC_PWRST_D3;
  2881. set_pin_power_state(codec, 0x28, &parm);
  2882. update_power_state(codec, 0x1b, parm);
  2883. update_power_state(codec, 0x34, parm);
  2884. update_power_state(codec, 0xc, parm);
  2885. }
  2886. }
  2887. /* Add a connection to the primary DAC from AA-mixer for some codecs
  2888. * This isn't listed from the raw info, but the chip has a secret connection.
  2889. */
  2890. static int add_secret_dac_path(struct hda_codec *codec)
  2891. {
  2892. struct via_spec *spec = codec->spec;
  2893. int i, nums;
  2894. hda_nid_t conn[8];
  2895. hda_nid_t nid;
  2896. if (!spec->aa_mix_nid)
  2897. return 0;
  2898. nums = snd_hda_get_connections(codec, spec->aa_mix_nid, conn,
  2899. ARRAY_SIZE(conn) - 1);
  2900. for (i = 0; i < nums; i++) {
  2901. if (get_wcaps_type(get_wcaps(codec, conn[i])) == AC_WID_AUD_OUT)
  2902. return 0;
  2903. }
  2904. /* find the primary DAC and add to the connection list */
  2905. nid = codec->start_nid;
  2906. for (i = 0; i < codec->num_nodes; i++, nid++) {
  2907. unsigned int caps = get_wcaps(codec, nid);
  2908. if (get_wcaps_type(caps) == AC_WID_AUD_OUT &&
  2909. !(caps & AC_WCAP_DIGITAL)) {
  2910. conn[nums++] = nid;
  2911. return snd_hda_override_conn_list(codec,
  2912. spec->aa_mix_nid,
  2913. nums, conn);
  2914. }
  2915. }
  2916. return 0;
  2917. }
  2918. static int patch_vt1718S(struct hda_codec *codec)
  2919. {
  2920. struct via_spec *spec;
  2921. int err;
  2922. /* create a codec specific record */
  2923. spec = via_new_spec(codec);
  2924. if (spec == NULL)
  2925. return -ENOMEM;
  2926. spec->aa_mix_nid = 0x21;
  2927. override_mic_boost(codec, 0x2b, 0, 3, 40);
  2928. override_mic_boost(codec, 0x29, 0, 3, 40);
  2929. add_secret_dac_path(codec);
  2930. /* automatic parse from the BIOS config */
  2931. err = via_parse_auto_config(codec);
  2932. if (err < 0) {
  2933. via_free(codec);
  2934. return err;
  2935. }
  2936. spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
  2937. codec->patch_ops = via_patch_ops;
  2938. spec->set_widgets_power_state = set_widgets_power_state_vt1718S;
  2939. return 0;
  2940. }
  2941. /* Patch for VT1716S */
  2942. static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
  2943. struct snd_ctl_elem_info *uinfo)
  2944. {
  2945. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2946. uinfo->count = 1;
  2947. uinfo->value.integer.min = 0;
  2948. uinfo->value.integer.max = 1;
  2949. return 0;
  2950. }
  2951. static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
  2952. struct snd_ctl_elem_value *ucontrol)
  2953. {
  2954. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2955. int index = 0;
  2956. index = snd_hda_codec_read(codec, 0x26, 0,
  2957. AC_VERB_GET_CONNECT_SEL, 0);
  2958. if (index != -1)
  2959. *ucontrol->value.integer.value = index;
  2960. return 0;
  2961. }
  2962. static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
  2963. struct snd_ctl_elem_value *ucontrol)
  2964. {
  2965. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2966. struct via_spec *spec = codec->spec;
  2967. int index = *ucontrol->value.integer.value;
  2968. snd_hda_codec_write(codec, 0x26, 0,
  2969. AC_VERB_SET_CONNECT_SEL, index);
  2970. spec->dmic_enabled = index;
  2971. set_widgets_power_state(codec);
  2972. return 1;
  2973. }
  2974. static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
  2975. HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
  2976. {
  2977. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2978. .name = "Digital Mic Capture Switch",
  2979. .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
  2980. .count = 1,
  2981. .info = vt1716s_dmic_info,
  2982. .get = vt1716s_dmic_get,
  2983. .put = vt1716s_dmic_put,
  2984. },
  2985. {} /* end */
  2986. };
  2987. /* mono-out mixer elements */
  2988. static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
  2989. HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
  2990. { } /* end */
  2991. };
  2992. static const struct hda_verb vt1716S_init_verbs[] = {
  2993. /* Enable Boost Volume backdoor */
  2994. {0x1, 0xf8a, 0x80},
  2995. /* don't bybass mixer */
  2996. {0x1, 0xf88, 0xc0},
  2997. /* Enable mono output */
  2998. {0x1, 0xf90, 0x08},
  2999. { }
  3000. };
  3001. static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
  3002. {
  3003. struct via_spec *spec = codec->spec;
  3004. int imux_is_smixer;
  3005. unsigned int parm;
  3006. unsigned int mono_out, present;
  3007. /* SW0 (17h) = stereo mixer */
  3008. imux_is_smixer =
  3009. (snd_hda_codec_read(codec, 0x17, 0,
  3010. AC_VERB_GET_CONNECT_SEL, 0x00) == 5);
  3011. /* inputs */
  3012. /* PW 1/2/5 (1ah/1bh/1eh) */
  3013. parm = AC_PWRST_D3;
  3014. set_pin_power_state(codec, 0x1a, &parm);
  3015. set_pin_power_state(codec, 0x1b, &parm);
  3016. set_pin_power_state(codec, 0x1e, &parm);
  3017. if (imux_is_smixer)
  3018. parm = AC_PWRST_D0;
  3019. /* SW0 (17h), AIW0(13h) */
  3020. update_power_state(codec, 0x17, parm);
  3021. update_power_state(codec, 0x13, parm);
  3022. parm = AC_PWRST_D3;
  3023. set_pin_power_state(codec, 0x1e, &parm);
  3024. /* PW11 (22h) */
  3025. if (spec->dmic_enabled)
  3026. set_pin_power_state(codec, 0x22, &parm);
  3027. else
  3028. update_power_state(codec, 0x22, AC_PWRST_D3);
  3029. /* SW2(26h), AIW1(14h) */
  3030. update_power_state(codec, 0x26, parm);
  3031. update_power_state(codec, 0x14, parm);
  3032. /* outputs */
  3033. /* PW0 (19h), SW1 (18h), AOW1 (11h) */
  3034. parm = AC_PWRST_D3;
  3035. set_pin_power_state(codec, 0x19, &parm);
  3036. /* Smart 5.1 PW2(1bh) */
  3037. if (spec->smart51_enabled)
  3038. set_pin_power_state(codec, 0x1b, &parm);
  3039. update_power_state(codec, 0x18, parm);
  3040. update_power_state(codec, 0x11, parm);
  3041. /* PW7 (23h), SW3 (27h), AOW3 (25h) */
  3042. parm = AC_PWRST_D3;
  3043. set_pin_power_state(codec, 0x23, &parm);
  3044. /* Smart 5.1 PW1(1ah) */
  3045. if (spec->smart51_enabled)
  3046. set_pin_power_state(codec, 0x1a, &parm);
  3047. update_power_state(codec, 0x27, parm);
  3048. /* Smart 5.1 PW5(1eh) */
  3049. if (spec->smart51_enabled)
  3050. set_pin_power_state(codec, 0x1e, &parm);
  3051. update_power_state(codec, 0x25, parm);
  3052. /* Mono out */
  3053. /* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
  3054. present = snd_hda_jack_detect(codec, 0x1c);
  3055. if (present)
  3056. mono_out = 0;
  3057. else {
  3058. present = snd_hda_jack_detect(codec, 0x1d);
  3059. if (!spec->hp_independent_mode && present)
  3060. mono_out = 0;
  3061. else
  3062. mono_out = 1;
  3063. }
  3064. parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
  3065. update_power_state(codec, 0x28, parm);
  3066. update_power_state(codec, 0x29, parm);
  3067. update_power_state(codec, 0x2a, parm);
  3068. /* PW 3/4 (1ch/1dh) */
  3069. parm = AC_PWRST_D3;
  3070. set_pin_power_state(codec, 0x1c, &parm);
  3071. set_pin_power_state(codec, 0x1d, &parm);
  3072. /* HP Independent Mode, power on AOW3 */
  3073. if (spec->hp_independent_mode)
  3074. update_power_state(codec, 0x25, parm);
  3075. /* force to D0 for internal Speaker */
  3076. /* MW0 (16h), AOW0 (10h) */
  3077. update_power_state(codec, 0x16, imux_is_smixer ? AC_PWRST_D0 : parm);
  3078. update_power_state(codec, 0x10, mono_out ? AC_PWRST_D0 : parm);
  3079. }
  3080. static int patch_vt1716S(struct hda_codec *codec)
  3081. {
  3082. struct via_spec *spec;
  3083. int err;
  3084. /* create a codec specific record */
  3085. spec = via_new_spec(codec);
  3086. if (spec == NULL)
  3087. return -ENOMEM;
  3088. spec->aa_mix_nid = 0x16;
  3089. override_mic_boost(codec, 0x1a, 0, 3, 40);
  3090. override_mic_boost(codec, 0x1e, 0, 3, 40);
  3091. /* automatic parse from the BIOS config */
  3092. err = via_parse_auto_config(codec);
  3093. if (err < 0) {
  3094. via_free(codec);
  3095. return err;
  3096. }
  3097. spec->init_verbs[spec->num_iverbs++] = vt1716S_init_verbs;
  3098. spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
  3099. spec->num_mixers++;
  3100. spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
  3101. codec->patch_ops = via_patch_ops;
  3102. spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
  3103. return 0;
  3104. }
  3105. /* for vt2002P */
  3106. static const struct hda_verb vt2002P_init_verbs[] = {
  3107. /* Class-D speaker related verbs */
  3108. {0x1, 0xfe0, 0x4},
  3109. {0x1, 0xfe9, 0x80},
  3110. {0x1, 0xfe2, 0x22},
  3111. /* Enable Boost Volume backdoor */
  3112. {0x1, 0xfb9, 0x24},
  3113. /* Enable AOW0 to MW9 */
  3114. {0x1, 0xfb8, 0x88},
  3115. { }
  3116. };
  3117. static const struct hda_verb vt1802_init_verbs[] = {
  3118. /* Enable Boost Volume backdoor */
  3119. {0x1, 0xfb9, 0x24},
  3120. /* Enable AOW0 to MW9 */
  3121. {0x1, 0xfb8, 0x88},
  3122. { }
  3123. };
  3124. static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
  3125. {
  3126. struct via_spec *spec = codec->spec;
  3127. int imux_is_smixer;
  3128. unsigned int parm;
  3129. unsigned int present;
  3130. /* MUX9 (1eh) = stereo mixer */
  3131. imux_is_smixer =
  3132. snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
  3133. /* inputs */
  3134. /* PW 5/6/7 (29h/2ah/2bh) */
  3135. parm = AC_PWRST_D3;
  3136. set_pin_power_state(codec, 0x29, &parm);
  3137. set_pin_power_state(codec, 0x2a, &parm);
  3138. set_pin_power_state(codec, 0x2b, &parm);
  3139. parm = AC_PWRST_D0;
  3140. /* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
  3141. update_power_state(codec, 0x1e, parm);
  3142. update_power_state(codec, 0x1f, parm);
  3143. update_power_state(codec, 0x10, parm);
  3144. update_power_state(codec, 0x11, parm);
  3145. /* outputs */
  3146. /* AOW0 (8h)*/
  3147. update_power_state(codec, 0x8, parm);
  3148. if (spec->codec_type == VT1802) {
  3149. /* PW4 (28h), MW4 (18h), MUX4(38h) */
  3150. parm = AC_PWRST_D3;
  3151. set_pin_power_state(codec, 0x28, &parm);
  3152. update_power_state(codec, 0x18, parm);
  3153. update_power_state(codec, 0x38, parm);
  3154. } else {
  3155. /* PW4 (26h), MW4 (1ch), MUX4(37h) */
  3156. parm = AC_PWRST_D3;
  3157. set_pin_power_state(codec, 0x26, &parm);
  3158. update_power_state(codec, 0x1c, parm);
  3159. update_power_state(codec, 0x37, parm);
  3160. }
  3161. if (spec->codec_type == VT1802) {
  3162. /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
  3163. parm = AC_PWRST_D3;
  3164. set_pin_power_state(codec, 0x25, &parm);
  3165. update_power_state(codec, 0x15, parm);
  3166. update_power_state(codec, 0x35, parm);
  3167. } else {
  3168. /* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
  3169. parm = AC_PWRST_D3;
  3170. set_pin_power_state(codec, 0x25, &parm);
  3171. update_power_state(codec, 0x19, parm);
  3172. update_power_state(codec, 0x35, parm);
  3173. }
  3174. if (spec->hp_independent_mode)
  3175. update_power_state(codec, 0x9, AC_PWRST_D0);
  3176. /* Class-D */
  3177. /* PW0 (24h), MW0(18h/14h), MUX0(34h) */
  3178. present = snd_hda_jack_detect(codec, 0x25);
  3179. parm = AC_PWRST_D3;
  3180. set_pin_power_state(codec, 0x24, &parm);
  3181. parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
  3182. if (spec->codec_type == VT1802)
  3183. update_power_state(codec, 0x14, parm);
  3184. else
  3185. update_power_state(codec, 0x18, parm);
  3186. update_power_state(codec, 0x34, parm);
  3187. /* Mono Out */
  3188. present = snd_hda_jack_detect(codec, 0x26);
  3189. parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
  3190. if (spec->codec_type == VT1802) {
  3191. /* PW15 (33h), MW8(1ch), MUX8(3ch) */
  3192. update_power_state(codec, 0x33, parm);
  3193. update_power_state(codec, 0x1c, parm);
  3194. update_power_state(codec, 0x3c, parm);
  3195. } else {
  3196. /* PW15 (31h), MW8(17h), MUX8(3bh) */
  3197. update_power_state(codec, 0x31, parm);
  3198. update_power_state(codec, 0x17, parm);
  3199. update_power_state(codec, 0x3b, parm);
  3200. }
  3201. /* MW9 (21h) */
  3202. if (imux_is_smixer || !is_aa_path_mute(codec))
  3203. update_power_state(codec, 0x21, AC_PWRST_D0);
  3204. else
  3205. update_power_state(codec, 0x21, AC_PWRST_D3);
  3206. }
  3207. /* patch for vt2002P */
  3208. static int patch_vt2002P(struct hda_codec *codec)
  3209. {
  3210. struct via_spec *spec;
  3211. int err;
  3212. /* create a codec specific record */
  3213. spec = via_new_spec(codec);
  3214. if (spec == NULL)
  3215. return -ENOMEM;
  3216. spec->aa_mix_nid = 0x21;
  3217. override_mic_boost(codec, 0x2b, 0, 3, 40);
  3218. override_mic_boost(codec, 0x29, 0, 3, 40);
  3219. add_secret_dac_path(codec);
  3220. /* automatic parse from the BIOS config */
  3221. err = via_parse_auto_config(codec);
  3222. if (err < 0) {
  3223. via_free(codec);
  3224. return err;
  3225. }
  3226. if (spec->codec_type == VT1802)
  3227. spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
  3228. else
  3229. spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
  3230. codec->patch_ops = via_patch_ops;
  3231. spec->set_widgets_power_state = set_widgets_power_state_vt2002P;
  3232. return 0;
  3233. }
  3234. /* for vt1812 */
  3235. static const struct hda_verb vt1812_init_verbs[] = {
  3236. /* Enable Boost Volume backdoor */
  3237. {0x1, 0xfb9, 0x24},
  3238. /* Enable AOW0 to MW9 */
  3239. {0x1, 0xfb8, 0xa8},
  3240. { }
  3241. };
  3242. static void set_widgets_power_state_vt1812(struct hda_codec *codec)
  3243. {
  3244. struct via_spec *spec = codec->spec;
  3245. unsigned int parm;
  3246. unsigned int present;
  3247. /* inputs */
  3248. /* PW 5/6/7 (29h/2ah/2bh) */
  3249. parm = AC_PWRST_D3;
  3250. set_pin_power_state(codec, 0x29, &parm);
  3251. set_pin_power_state(codec, 0x2a, &parm);
  3252. set_pin_power_state(codec, 0x2b, &parm);
  3253. parm = AC_PWRST_D0;
  3254. /* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
  3255. update_power_state(codec, 0x1e, parm);
  3256. update_power_state(codec, 0x1f, parm);
  3257. update_power_state(codec, 0x10, parm);
  3258. update_power_state(codec, 0x11, parm);
  3259. /* outputs */
  3260. /* AOW0 (8h)*/
  3261. update_power_state(codec, 0x8, AC_PWRST_D0);
  3262. /* PW4 (28h), MW4 (18h), MUX4(38h) */
  3263. parm = AC_PWRST_D3;
  3264. set_pin_power_state(codec, 0x28, &parm);
  3265. update_power_state(codec, 0x18, parm);
  3266. update_power_state(codec, 0x38, parm);
  3267. /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
  3268. parm = AC_PWRST_D3;
  3269. set_pin_power_state(codec, 0x25, &parm);
  3270. update_power_state(codec, 0x15, parm);
  3271. update_power_state(codec, 0x35, parm);
  3272. if (spec->hp_independent_mode)
  3273. update_power_state(codec, 0x9, AC_PWRST_D0);
  3274. /* Internal Speaker */
  3275. /* PW0 (24h), MW0(14h), MUX0(34h) */
  3276. present = snd_hda_jack_detect(codec, 0x25);
  3277. parm = AC_PWRST_D3;
  3278. set_pin_power_state(codec, 0x24, &parm);
  3279. if (present) {
  3280. update_power_state(codec, 0x14, AC_PWRST_D3);
  3281. update_power_state(codec, 0x34, AC_PWRST_D3);
  3282. } else {
  3283. update_power_state(codec, 0x14, AC_PWRST_D0);
  3284. update_power_state(codec, 0x34, AC_PWRST_D0);
  3285. }
  3286. /* Mono Out */
  3287. /* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
  3288. present = snd_hda_jack_detect(codec, 0x28);
  3289. parm = AC_PWRST_D3;
  3290. set_pin_power_state(codec, 0x31, &parm);
  3291. if (present) {
  3292. update_power_state(codec, 0x1c, AC_PWRST_D3);
  3293. update_power_state(codec, 0x3c, AC_PWRST_D3);
  3294. update_power_state(codec, 0x3e, AC_PWRST_D3);
  3295. } else {
  3296. update_power_state(codec, 0x1c, AC_PWRST_D0);
  3297. update_power_state(codec, 0x3c, AC_PWRST_D0);
  3298. update_power_state(codec, 0x3e, AC_PWRST_D0);
  3299. }
  3300. /* PW15 (33h), MW15 (1dh), MUX15(3dh) */
  3301. parm = AC_PWRST_D3;
  3302. set_pin_power_state(codec, 0x33, &parm);
  3303. update_power_state(codec, 0x1d, parm);
  3304. update_power_state(codec, 0x3d, parm);
  3305. }
  3306. /* patch for vt1812 */
  3307. static int patch_vt1812(struct hda_codec *codec)
  3308. {
  3309. struct via_spec *spec;
  3310. int err;
  3311. /* create a codec specific record */
  3312. spec = via_new_spec(codec);
  3313. if (spec == NULL)
  3314. return -ENOMEM;
  3315. spec->aa_mix_nid = 0x21;
  3316. override_mic_boost(codec, 0x2b, 0, 3, 40);
  3317. override_mic_boost(codec, 0x29, 0, 3, 40);
  3318. add_secret_dac_path(codec);
  3319. /* automatic parse from the BIOS config */
  3320. err = via_parse_auto_config(codec);
  3321. if (err < 0) {
  3322. via_free(codec);
  3323. return err;
  3324. }
  3325. spec->init_verbs[spec->num_iverbs++] = vt1812_init_verbs;
  3326. codec->patch_ops = via_patch_ops;
  3327. spec->set_widgets_power_state = set_widgets_power_state_vt1812;
  3328. return 0;
  3329. }
  3330. /*
  3331. * patch entries
  3332. */
  3333. static const struct hda_codec_preset snd_hda_preset_via[] = {
  3334. { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
  3335. { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
  3336. { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
  3337. { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
  3338. { .id = 0x1106e710, .name = "VT1709 10-Ch",
  3339. .patch = patch_vt1709},
  3340. { .id = 0x1106e711, .name = "VT1709 10-Ch",
  3341. .patch = patch_vt1709},
  3342. { .id = 0x1106e712, .name = "VT1709 10-Ch",
  3343. .patch = patch_vt1709},
  3344. { .id = 0x1106e713, .name = "VT1709 10-Ch",
  3345. .patch = patch_vt1709},
  3346. { .id = 0x1106e714, .name = "VT1709 6-Ch",
  3347. .patch = patch_vt1709},
  3348. { .id = 0x1106e715, .name = "VT1709 6-Ch",
  3349. .patch = patch_vt1709},
  3350. { .id = 0x1106e716, .name = "VT1709 6-Ch",
  3351. .patch = patch_vt1709},
  3352. { .id = 0x1106e717, .name = "VT1709 6-Ch",
  3353. .patch = patch_vt1709},
  3354. { .id = 0x1106e720, .name = "VT1708B 8-Ch",
  3355. .patch = patch_vt1708B},
  3356. { .id = 0x1106e721, .name = "VT1708B 8-Ch",
  3357. .patch = patch_vt1708B},
  3358. { .id = 0x1106e722, .name = "VT1708B 8-Ch",
  3359. .patch = patch_vt1708B},
  3360. { .id = 0x1106e723, .name = "VT1708B 8-Ch",
  3361. .patch = patch_vt1708B},
  3362. { .id = 0x1106e724, .name = "VT1708B 4-Ch",
  3363. .patch = patch_vt1708B},
  3364. { .id = 0x1106e725, .name = "VT1708B 4-Ch",
  3365. .patch = patch_vt1708B},
  3366. { .id = 0x1106e726, .name = "VT1708B 4-Ch",
  3367. .patch = patch_vt1708B},
  3368. { .id = 0x1106e727, .name = "VT1708B 4-Ch",
  3369. .patch = patch_vt1708B},
  3370. { .id = 0x11060397, .name = "VT1708S",
  3371. .patch = patch_vt1708S},
  3372. { .id = 0x11061397, .name = "VT1708S",
  3373. .patch = patch_vt1708S},
  3374. { .id = 0x11062397, .name = "VT1708S",
  3375. .patch = patch_vt1708S},
  3376. { .id = 0x11063397, .name = "VT1708S",
  3377. .patch = patch_vt1708S},
  3378. { .id = 0x11064397, .name = "VT1705",
  3379. .patch = patch_vt1708S},
  3380. { .id = 0x11065397, .name = "VT1708S",
  3381. .patch = patch_vt1708S},
  3382. { .id = 0x11066397, .name = "VT1708S",
  3383. .patch = patch_vt1708S},
  3384. { .id = 0x11067397, .name = "VT1708S",
  3385. .patch = patch_vt1708S},
  3386. { .id = 0x11060398, .name = "VT1702",
  3387. .patch = patch_vt1702},
  3388. { .id = 0x11061398, .name = "VT1702",
  3389. .patch = patch_vt1702},
  3390. { .id = 0x11062398, .name = "VT1702",
  3391. .patch = patch_vt1702},
  3392. { .id = 0x11063398, .name = "VT1702",
  3393. .patch = patch_vt1702},
  3394. { .id = 0x11064398, .name = "VT1702",
  3395. .patch = patch_vt1702},
  3396. { .id = 0x11065398, .name = "VT1702",
  3397. .patch = patch_vt1702},
  3398. { .id = 0x11066398, .name = "VT1702",
  3399. .patch = patch_vt1702},
  3400. { .id = 0x11067398, .name = "VT1702",
  3401. .patch = patch_vt1702},
  3402. { .id = 0x11060428, .name = "VT1718S",
  3403. .patch = patch_vt1718S},
  3404. { .id = 0x11064428, .name = "VT1718S",
  3405. .patch = patch_vt1718S},
  3406. { .id = 0x11060441, .name = "VT2020",
  3407. .patch = patch_vt1718S},
  3408. { .id = 0x11064441, .name = "VT1828S",
  3409. .patch = patch_vt1718S},
  3410. { .id = 0x11060433, .name = "VT1716S",
  3411. .patch = patch_vt1716S},
  3412. { .id = 0x1106a721, .name = "VT1716S",
  3413. .patch = patch_vt1716S},
  3414. { .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
  3415. { .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
  3416. { .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
  3417. { .id = 0x11060440, .name = "VT1818S",
  3418. .patch = patch_vt1708S},
  3419. { .id = 0x11060446, .name = "VT1802",
  3420. .patch = patch_vt2002P},
  3421. { .id = 0x11068446, .name = "VT1802",
  3422. .patch = patch_vt2002P},
  3423. {} /* terminator */
  3424. };
  3425. MODULE_ALIAS("snd-hda-codec-id:1106*");
  3426. static struct hda_codec_preset_list via_list = {
  3427. .preset = snd_hda_preset_via,
  3428. .owner = THIS_MODULE,
  3429. };
  3430. MODULE_LICENSE("GPL");
  3431. MODULE_DESCRIPTION("VIA HD-audio codec");
  3432. static int __init patch_via_init(void)
  3433. {
  3434. return snd_hda_add_codec_preset(&via_list);
  3435. }
  3436. static void __exit patch_via_exit(void)
  3437. {
  3438. snd_hda_delete_codec_preset(&via_list);
  3439. }
  3440. module_init(patch_via_init)
  3441. module_exit(patch_via_exit)