patch_via.c 96 KB

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