patch_via.c 104 KB

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