patch_via.c 96 KB

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