patch_via.c 96 KB

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