patch_via.c 104 KB

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