patch_via.c 98 KB

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