patch_via.c 104 KB

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