patch_via.c 98 KB

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