patch_via.c 98 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668
  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 || res == VIA_LINE_EVENT)
  1417. via_hp_automute(codec);
  1418. else if (res == VIA_GPIO_EVENT)
  1419. via_gpio_control(codec);
  1420. }
  1421. #ifdef SND_HDA_NEEDS_RESUME
  1422. static int via_suspend(struct hda_codec *codec, pm_message_t state)
  1423. {
  1424. struct via_spec *spec = codec->spec;
  1425. vt1708_stop_hp_work(spec);
  1426. return 0;
  1427. }
  1428. #endif
  1429. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1430. static int via_check_power_status(struct hda_codec *codec, hda_nid_t nid)
  1431. {
  1432. struct via_spec *spec = codec->spec;
  1433. return snd_hda_check_amp_list_power(codec, &spec->loopback, nid);
  1434. }
  1435. #endif
  1436. /*
  1437. */
  1438. static int via_init(struct hda_codec *codec);
  1439. static const struct hda_codec_ops via_patch_ops = {
  1440. .build_controls = via_build_controls,
  1441. .build_pcms = via_build_pcms,
  1442. .init = via_init,
  1443. .free = via_free,
  1444. .unsol_event = via_unsol_event,
  1445. #ifdef SND_HDA_NEEDS_RESUME
  1446. .suspend = via_suspend,
  1447. #endif
  1448. #ifdef CONFIG_SND_HDA_POWER_SAVE
  1449. .check_power_status = via_check_power_status,
  1450. #endif
  1451. };
  1452. static bool is_empty_dac(struct hda_codec *codec, hda_nid_t dac)
  1453. {
  1454. struct via_spec *spec = codec->spec;
  1455. int i;
  1456. for (i = 0; i < spec->multiout.num_dacs; i++) {
  1457. if (spec->multiout.dac_nids[i] == dac)
  1458. return false;
  1459. }
  1460. if (spec->hp_dac_nid == dac)
  1461. return false;
  1462. return true;
  1463. }
  1464. static bool __parse_output_path(struct hda_codec *codec, hda_nid_t nid,
  1465. hda_nid_t target_dac, struct nid_path *path,
  1466. int depth, int wid_type)
  1467. {
  1468. hda_nid_t conn[8];
  1469. int i, nums;
  1470. nums = snd_hda_get_connections(codec, nid, conn, ARRAY_SIZE(conn));
  1471. for (i = 0; i < nums; i++) {
  1472. if (get_wcaps_type(get_wcaps(codec, conn[i])) != AC_WID_AUD_OUT)
  1473. continue;
  1474. if (conn[i] == target_dac || is_empty_dac(codec, conn[i]))
  1475. goto found;
  1476. }
  1477. if (depth >= MAX_NID_PATH_DEPTH)
  1478. return false;
  1479. for (i = 0; i < nums; i++) {
  1480. unsigned int type;
  1481. type = get_wcaps_type(get_wcaps(codec, conn[i]));
  1482. if (type == AC_WID_AUD_OUT ||
  1483. (wid_type != -1 && type != wid_type))
  1484. continue;
  1485. if (__parse_output_path(codec, conn[i], target_dac,
  1486. path, depth + 1, AC_WID_AUD_SEL))
  1487. goto found;
  1488. }
  1489. return false;
  1490. found:
  1491. path->path[path->depth] = conn[i];
  1492. path->idx[path->depth] = i;
  1493. if (nums > 1 && get_wcaps_type(get_wcaps(codec, nid)) != AC_WID_AUD_MIX)
  1494. path->multi[path->depth] = 1;
  1495. path->depth++;
  1496. return true;
  1497. }
  1498. static bool parse_output_path(struct hda_codec *codec, hda_nid_t nid,
  1499. hda_nid_t target_dac, struct nid_path *path)
  1500. {
  1501. if (__parse_output_path(codec, nid, target_dac, path, 1, -1)) {
  1502. path->path[path->depth] = nid;
  1503. path->depth++;
  1504. return true;
  1505. }
  1506. return false;
  1507. }
  1508. static int via_auto_fill_dac_nids(struct hda_codec *codec)
  1509. {
  1510. struct via_spec *spec = codec->spec;
  1511. const struct auto_pin_cfg *cfg = &spec->autocfg;
  1512. int i, dac_num;
  1513. hda_nid_t nid;
  1514. spec->multiout.dac_nids = spec->private_dac_nids;
  1515. dac_num = 0;
  1516. for (i = 0; i < cfg->line_outs; i++) {
  1517. nid = cfg->line_out_pins[i];
  1518. if (!nid)
  1519. continue;
  1520. if (parse_output_path(codec, nid, 0, &spec->out_path[i])) {
  1521. spec->private_dac_nids[i] = spec->out_path[i].path[0];
  1522. dac_num++;
  1523. }
  1524. }
  1525. spec->multiout.num_dacs = dac_num;
  1526. return 0;
  1527. }
  1528. static int create_ch_ctls(struct hda_codec *codec, const char *pfx,
  1529. int chs, bool check_dac, struct nid_path *path)
  1530. {
  1531. struct via_spec *spec = codec->spec;
  1532. char name[32];
  1533. hda_nid_t dac, pin, sel, nid;
  1534. int err;
  1535. dac = check_dac ? path->path[0] : 0;
  1536. pin = path->path[path->depth - 1];
  1537. sel = path->depth > 1 ? path->path[1] : 0;
  1538. if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
  1539. nid = dac;
  1540. else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
  1541. nid = pin;
  1542. else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_NUM_STEPS))
  1543. nid = sel;
  1544. else
  1545. nid = 0;
  1546. if (nid) {
  1547. sprintf(name, "%s Playback Volume", pfx);
  1548. err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
  1549. HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
  1550. if (err < 0)
  1551. return err;
  1552. path->vol_ctl = nid;
  1553. }
  1554. if (dac && check_amp_caps(codec, dac, HDA_OUTPUT, AC_AMPCAP_MUTE))
  1555. nid = dac;
  1556. else if (check_amp_caps(codec, pin, HDA_OUTPUT, AC_AMPCAP_MUTE))
  1557. nid = pin;
  1558. else if (check_amp_caps(codec, sel, HDA_OUTPUT, AC_AMPCAP_MUTE))
  1559. nid = sel;
  1560. else
  1561. nid = 0;
  1562. if (nid) {
  1563. sprintf(name, "%s Playback Switch", pfx);
  1564. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
  1565. HDA_COMPOSE_AMP_VAL(nid, chs, 0, HDA_OUTPUT));
  1566. if (err < 0)
  1567. return err;
  1568. path->mute_ctl = nid;
  1569. }
  1570. return 0;
  1571. }
  1572. static void mangle_smart51(struct hda_codec *codec)
  1573. {
  1574. struct via_spec *spec = codec->spec;
  1575. struct auto_pin_cfg *cfg = &spec->autocfg;
  1576. struct auto_pin_cfg_item *ins = cfg->inputs;
  1577. int i, j, nums, attr;
  1578. int pins[AUTO_CFG_MAX_INS];
  1579. for (attr = INPUT_PIN_ATTR_REAR; attr >= INPUT_PIN_ATTR_NORMAL; attr--) {
  1580. nums = 0;
  1581. for (i = 0; i < cfg->num_inputs; i++) {
  1582. unsigned int def;
  1583. if (ins[i].type > AUTO_PIN_LINE_IN)
  1584. continue;
  1585. def = snd_hda_codec_get_pincfg(codec, ins[i].pin);
  1586. if (snd_hda_get_input_pin_attr(def) != attr)
  1587. continue;
  1588. for (j = 0; j < nums; j++)
  1589. if (ins[pins[j]].type < ins[i].type) {
  1590. memmove(pins + j + 1, pins + j,
  1591. (nums - j) * sizeof(int));
  1592. break;
  1593. }
  1594. pins[j] = i;
  1595. nums++;
  1596. }
  1597. if (cfg->line_outs + nums < 3)
  1598. continue;
  1599. for (i = 0; i < nums; i++) {
  1600. hda_nid_t pin = ins[pins[i]].pin;
  1601. spec->smart51_pins[spec->smart51_nums++] = pin;
  1602. cfg->line_out_pins[cfg->line_outs++] = pin;
  1603. if (cfg->line_outs == 3)
  1604. break;
  1605. }
  1606. return;
  1607. }
  1608. }
  1609. /* add playback controls from the parsed DAC table */
  1610. static int via_auto_create_multi_out_ctls(struct hda_codec *codec)
  1611. {
  1612. struct via_spec *spec = codec->spec;
  1613. struct auto_pin_cfg *cfg = &spec->autocfg;
  1614. static const char * const chname[4] = {
  1615. "Front", "Surround", "C/LFE", "Side"
  1616. };
  1617. int i, idx, err;
  1618. int old_line_outs;
  1619. /* check smart51 */
  1620. old_line_outs = cfg->line_outs;
  1621. if (cfg->line_outs == 1)
  1622. mangle_smart51(codec);
  1623. err = via_auto_fill_dac_nids(codec);
  1624. if (err < 0)
  1625. return err;
  1626. if (spec->multiout.num_dacs < 3) {
  1627. spec->smart51_nums = 0;
  1628. cfg->line_outs = old_line_outs;
  1629. }
  1630. for (i = 0; i < cfg->line_outs; i++) {
  1631. hda_nid_t pin, dac;
  1632. pin = cfg->line_out_pins[i];
  1633. dac = spec->multiout.dac_nids[i];
  1634. if (!pin || !dac)
  1635. continue;
  1636. if (i == HDA_CLFE) {
  1637. err = create_ch_ctls(codec, "Center", 1, true,
  1638. &spec->out_path[i]);
  1639. if (err < 0)
  1640. return err;
  1641. err = create_ch_ctls(codec, "LFE", 2, true,
  1642. &spec->out_path[i]);
  1643. if (err < 0)
  1644. return err;
  1645. } else {
  1646. const char *pfx = chname[i];
  1647. if (cfg->line_out_type == AUTO_PIN_SPEAKER_OUT &&
  1648. cfg->line_outs == 1)
  1649. pfx = "Speaker";
  1650. err = create_ch_ctls(codec, pfx, 3, true,
  1651. &spec->out_path[i]);
  1652. if (err < 0)
  1653. return err;
  1654. }
  1655. }
  1656. idx = get_connection_index(codec, spec->aa_mix_nid,
  1657. spec->multiout.dac_nids[0]);
  1658. if (idx < 0 && spec->dac_mixer_idx)
  1659. idx = spec->dac_mixer_idx;
  1660. if (idx >= 0) {
  1661. /* add control to mixer */
  1662. err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
  1663. "PCM Playback Volume",
  1664. HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
  1665. idx, HDA_INPUT));
  1666. if (err < 0)
  1667. return err;
  1668. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
  1669. "PCM Playback Switch",
  1670. HDA_COMPOSE_AMP_VAL(spec->aa_mix_nid, 3,
  1671. idx, HDA_INPUT));
  1672. if (err < 0)
  1673. return err;
  1674. }
  1675. cfg->line_outs = old_line_outs;
  1676. return 0;
  1677. }
  1678. static int via_auto_create_hp_ctls(struct hda_codec *codec, hda_nid_t pin)
  1679. {
  1680. struct via_spec *spec = codec->spec;
  1681. struct nid_path *path;
  1682. bool check_dac;
  1683. int err;
  1684. if (!pin)
  1685. return 0;
  1686. if (parse_output_path(codec, pin, 0, &spec->hp_path))
  1687. spec->hp_dac_nid = spec->hp_path.path[0];
  1688. else if (spec->multiout.dac_nids[HDA_SIDE] &&
  1689. parse_output_path(codec, pin,
  1690. spec->multiout.dac_nids[HDA_SIDE],
  1691. &spec->hp_path)) {
  1692. spec->hp_dac_nid = spec->hp_path.path[0];
  1693. spec->hp_indep_shared = true;
  1694. } else if (spec->multiout.dac_nids[HDA_CLFE] &&
  1695. parse_output_path(codec, pin,
  1696. spec->multiout.dac_nids[HDA_CLFE],
  1697. &spec->hp_path)) {
  1698. spec->hp_dac_nid = spec->hp_path.path[0];
  1699. spec->hp_indep_shared = true;
  1700. }
  1701. if (!parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
  1702. &spec->hp_dep_path) &&
  1703. !spec->hp_dac_nid)
  1704. return 0;
  1705. if (spec->hp_dac_nid && !spec->hp_indep_shared) {
  1706. path = &spec->hp_path;
  1707. check_dac = true;
  1708. } else {
  1709. path = &spec->hp_dep_path;
  1710. check_dac = false;
  1711. }
  1712. err = create_ch_ctls(codec, "Headphone", 3, check_dac, path);
  1713. if (err < 0)
  1714. return err;
  1715. if (spec->hp_dac_nid) {
  1716. spec->hp_dep_path.vol_ctl = spec->hp_path.vol_ctl;
  1717. spec->hp_dep_path.mute_ctl = spec->hp_path.mute_ctl;
  1718. }
  1719. return 0;
  1720. }
  1721. static int via_auto_create_speaker_ctls(struct hda_codec *codec)
  1722. {
  1723. struct via_spec *spec = codec->spec;
  1724. hda_nid_t pin, dac;
  1725. pin = spec->autocfg.speaker_pins[0];
  1726. if (!spec->autocfg.speaker_outs || !pin)
  1727. return 0;
  1728. if (parse_output_path(codec, pin, 0, &spec->speaker_path)) {
  1729. dac = spec->speaker_path.path[0];
  1730. spec->multiout.extra_out_nid[0] = dac;
  1731. return create_ch_ctls(codec, "Speaker", 3, true,
  1732. &spec->speaker_path);
  1733. }
  1734. if (parse_output_path(codec, pin, spec->multiout.dac_nids[HDA_FRONT],
  1735. &spec->speaker_path))
  1736. return create_ch_ctls(codec, "Speaker", 3, false,
  1737. &spec->speaker_path);
  1738. return 0;
  1739. }
  1740. /* look for ADCs */
  1741. static int via_fill_adcs(struct hda_codec *codec)
  1742. {
  1743. struct via_spec *spec = codec->spec;
  1744. hda_nid_t nid = codec->start_nid;
  1745. int i;
  1746. for (i = 0; i < codec->num_nodes; i++, nid++) {
  1747. unsigned int wcaps = get_wcaps(codec, nid);
  1748. if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
  1749. continue;
  1750. if (wcaps & AC_WCAP_DIGITAL)
  1751. continue;
  1752. if (!(wcaps & AC_WCAP_CONN_LIST))
  1753. continue;
  1754. if (spec->num_adc_nids >= ARRAY_SIZE(spec->adc_nids))
  1755. return -ENOMEM;
  1756. spec->adc_nids[spec->num_adc_nids++] = nid;
  1757. }
  1758. return 0;
  1759. }
  1760. /* input-src control */
  1761. static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
  1762. struct snd_ctl_elem_info *uinfo)
  1763. {
  1764. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1765. struct via_spec *spec = codec->spec;
  1766. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  1767. uinfo->count = 1;
  1768. uinfo->value.enumerated.items = spec->num_inputs;
  1769. if (uinfo->value.enumerated.item >= spec->num_inputs)
  1770. uinfo->value.enumerated.item = spec->num_inputs - 1;
  1771. strcpy(uinfo->value.enumerated.name,
  1772. spec->inputs[uinfo->value.enumerated.item].label);
  1773. return 0;
  1774. }
  1775. static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
  1776. struct snd_ctl_elem_value *ucontrol)
  1777. {
  1778. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1779. struct via_spec *spec = codec->spec;
  1780. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1781. ucontrol->value.enumerated.item[0] = spec->cur_mux[idx];
  1782. return 0;
  1783. }
  1784. static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
  1785. struct snd_ctl_elem_value *ucontrol)
  1786. {
  1787. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  1788. struct via_spec *spec = codec->spec;
  1789. unsigned int idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  1790. hda_nid_t mux;
  1791. int cur;
  1792. cur = ucontrol->value.enumerated.item[0];
  1793. if (cur < 0 || cur >= spec->num_inputs)
  1794. return -EINVAL;
  1795. if (spec->cur_mux[idx] == cur)
  1796. return 0;
  1797. spec->cur_mux[idx] = cur;
  1798. if (spec->dyn_adc_switch) {
  1799. int adc_idx = spec->inputs[cur].adc_idx;
  1800. mux = spec->mux_nids[adc_idx];
  1801. via_dyn_adc_pcm_resetup(codec, cur);
  1802. } else {
  1803. mux = spec->mux_nids[idx];
  1804. if (snd_BUG_ON(!mux))
  1805. return -EINVAL;
  1806. }
  1807. if (mux) {
  1808. /* switch to D0 beofre change index */
  1809. if (snd_hda_codec_read(codec, mux, 0,
  1810. AC_VERB_GET_POWER_STATE, 0x00) != AC_PWRST_D0)
  1811. snd_hda_codec_write(codec, mux, 0,
  1812. AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
  1813. snd_hda_codec_write(codec, mux, 0,
  1814. AC_VERB_SET_CONNECT_SEL,
  1815. spec->inputs[cur].mux_idx);
  1816. }
  1817. /* update jack power state */
  1818. set_widgets_power_state(codec);
  1819. return 0;
  1820. }
  1821. static const struct snd_kcontrol_new via_input_src_ctl = {
  1822. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  1823. /* The multiple "Capture Source" controls confuse alsamixer
  1824. * So call somewhat different..
  1825. */
  1826. /* .name = "Capture Source", */
  1827. .name = "Input Source",
  1828. .info = via_mux_enum_info,
  1829. .get = via_mux_enum_get,
  1830. .put = via_mux_enum_put,
  1831. };
  1832. static int create_input_src_ctls(struct hda_codec *codec, int count)
  1833. {
  1834. struct via_spec *spec = codec->spec;
  1835. struct snd_kcontrol_new *knew;
  1836. if (spec->num_inputs <= 1 || !count)
  1837. return 0; /* no need for single src */
  1838. knew = via_clone_control(spec, &via_input_src_ctl);
  1839. if (!knew)
  1840. return -ENOMEM;
  1841. knew->count = count;
  1842. return 0;
  1843. }
  1844. /* add the powersave loopback-list entry */
  1845. static void add_loopback_list(struct via_spec *spec, hda_nid_t mix, int idx)
  1846. {
  1847. struct hda_amp_list *list;
  1848. if (spec->num_loopbacks >= ARRAY_SIZE(spec->loopback_list) - 1)
  1849. return;
  1850. list = spec->loopback_list + spec->num_loopbacks;
  1851. list->nid = mix;
  1852. list->dir = HDA_INPUT;
  1853. list->idx = idx;
  1854. spec->num_loopbacks++;
  1855. spec->loopback.amplist = spec->loopback_list;
  1856. }
  1857. static bool is_reachable_nid(struct hda_codec *codec, hda_nid_t src,
  1858. hda_nid_t dst)
  1859. {
  1860. return snd_hda_get_conn_index(codec, src, dst, 1) >= 0;
  1861. }
  1862. /* add the input-route to the given pin */
  1863. static bool add_input_route(struct hda_codec *codec, hda_nid_t pin)
  1864. {
  1865. struct via_spec *spec = codec->spec;
  1866. int c, idx;
  1867. spec->inputs[spec->num_inputs].adc_idx = -1;
  1868. spec->inputs[spec->num_inputs].pin = pin;
  1869. for (c = 0; c < spec->num_adc_nids; c++) {
  1870. if (spec->mux_nids[c]) {
  1871. idx = get_connection_index(codec, spec->mux_nids[c],
  1872. pin);
  1873. if (idx < 0)
  1874. continue;
  1875. spec->inputs[spec->num_inputs].mux_idx = idx;
  1876. } else {
  1877. if (!is_reachable_nid(codec, spec->adc_nids[c], pin))
  1878. continue;
  1879. }
  1880. spec->inputs[spec->num_inputs].adc_idx = c;
  1881. /* Can primary ADC satisfy all inputs? */
  1882. if (!spec->dyn_adc_switch &&
  1883. spec->num_inputs > 0 && spec->inputs[0].adc_idx != c) {
  1884. snd_printd(KERN_INFO
  1885. "via: dynamic ADC switching enabled\n");
  1886. spec->dyn_adc_switch = 1;
  1887. }
  1888. return true;
  1889. }
  1890. return false;
  1891. }
  1892. static int get_mux_nids(struct hda_codec *codec);
  1893. /* parse input-routes; fill ADCs, MUXs and input-src entries */
  1894. static int parse_analog_inputs(struct hda_codec *codec)
  1895. {
  1896. struct via_spec *spec = codec->spec;
  1897. const struct auto_pin_cfg *cfg = &spec->autocfg;
  1898. int i, err;
  1899. err = via_fill_adcs(codec);
  1900. if (err < 0)
  1901. return err;
  1902. err = get_mux_nids(codec);
  1903. if (err < 0)
  1904. return err;
  1905. /* fill all input-routes */
  1906. for (i = 0; i < cfg->num_inputs; i++) {
  1907. if (add_input_route(codec, cfg->inputs[i].pin))
  1908. spec->inputs[spec->num_inputs++].label =
  1909. hda_get_autocfg_input_label(codec, cfg, i);
  1910. }
  1911. /* check for internal loopback recording */
  1912. if (spec->aa_mix_nid &&
  1913. add_input_route(codec, spec->aa_mix_nid))
  1914. spec->inputs[spec->num_inputs++].label = "Stereo Mixer";
  1915. return 0;
  1916. }
  1917. /* create analog-loopback volume/switch controls */
  1918. static int create_loopback_ctls(struct hda_codec *codec)
  1919. {
  1920. struct via_spec *spec = codec->spec;
  1921. const struct auto_pin_cfg *cfg = &spec->autocfg;
  1922. const char *prev_label = NULL;
  1923. int type_idx = 0;
  1924. int i, j, err, idx;
  1925. if (!spec->aa_mix_nid)
  1926. return 0;
  1927. for (i = 0; i < cfg->num_inputs; i++) {
  1928. hda_nid_t pin = cfg->inputs[i].pin;
  1929. const char *label = hda_get_autocfg_input_label(codec, cfg, i);
  1930. if (prev_label && !strcmp(label, prev_label))
  1931. type_idx++;
  1932. else
  1933. type_idx = 0;
  1934. prev_label = label;
  1935. idx = get_connection_index(codec, spec->aa_mix_nid, pin);
  1936. if (idx >= 0) {
  1937. err = via_new_analog_input(spec, label, type_idx,
  1938. idx, spec->aa_mix_nid);
  1939. if (err < 0)
  1940. return err;
  1941. add_loopback_list(spec, spec->aa_mix_nid, idx);
  1942. }
  1943. /* remember the label for smart51 control */
  1944. for (j = 0; j < spec->smart51_nums; j++) {
  1945. if (spec->smart51_pins[j] == pin) {
  1946. spec->smart51_idxs[j] = idx;
  1947. spec->smart51_labels[j] = label;
  1948. break;
  1949. }
  1950. }
  1951. }
  1952. return 0;
  1953. }
  1954. /* create mic-boost controls (if present) */
  1955. static int create_mic_boost_ctls(struct hda_codec *codec)
  1956. {
  1957. struct via_spec *spec = codec->spec;
  1958. const struct auto_pin_cfg *cfg = &spec->autocfg;
  1959. int i, err;
  1960. for (i = 0; i < cfg->num_inputs; i++) {
  1961. hda_nid_t pin = cfg->inputs[i].pin;
  1962. unsigned int caps;
  1963. const char *label;
  1964. char name[32];
  1965. if (cfg->inputs[i].type != AUTO_PIN_MIC)
  1966. continue;
  1967. caps = query_amp_caps(codec, pin, HDA_INPUT);
  1968. if (caps == -1 || !(caps & AC_AMPCAP_NUM_STEPS))
  1969. continue;
  1970. label = hda_get_autocfg_input_label(codec, cfg, i);
  1971. snprintf(name, sizeof(name), "%s Boost Volume", label);
  1972. err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
  1973. HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_INPUT));
  1974. if (err < 0)
  1975. return err;
  1976. }
  1977. return 0;
  1978. }
  1979. /* create capture and input-src controls for multiple streams */
  1980. static int create_multi_adc_ctls(struct hda_codec *codec)
  1981. {
  1982. struct via_spec *spec = codec->spec;
  1983. int i, err;
  1984. /* create capture mixer elements */
  1985. for (i = 0; i < spec->num_adc_nids; i++) {
  1986. hda_nid_t adc = spec->adc_nids[i];
  1987. err = __via_add_control(spec, VIA_CTL_WIDGET_VOL,
  1988. "Capture Volume", i,
  1989. HDA_COMPOSE_AMP_VAL(adc, 3, 0,
  1990. HDA_INPUT));
  1991. if (err < 0)
  1992. return err;
  1993. err = __via_add_control(spec, VIA_CTL_WIDGET_MUTE,
  1994. "Capture Switch", i,
  1995. HDA_COMPOSE_AMP_VAL(adc, 3, 0,
  1996. HDA_INPUT));
  1997. if (err < 0)
  1998. return err;
  1999. }
  2000. /* input-source control */
  2001. for (i = 0; i < spec->num_adc_nids; i++)
  2002. if (!spec->mux_nids[i])
  2003. break;
  2004. err = create_input_src_ctls(codec, i);
  2005. if (err < 0)
  2006. return err;
  2007. return 0;
  2008. }
  2009. /* bind capture volume/switch */
  2010. static struct snd_kcontrol_new via_bind_cap_vol_ctl =
  2011. HDA_BIND_VOL("Capture Volume", 0);
  2012. static struct snd_kcontrol_new via_bind_cap_sw_ctl =
  2013. HDA_BIND_SW("Capture Switch", 0);
  2014. static int init_bind_ctl(struct via_spec *spec, struct hda_bind_ctls **ctl_ret,
  2015. struct hda_ctl_ops *ops)
  2016. {
  2017. struct hda_bind_ctls *ctl;
  2018. int i;
  2019. ctl = kzalloc(sizeof(*ctl) + sizeof(long) * 4, GFP_KERNEL);
  2020. if (!ctl)
  2021. return -ENOMEM;
  2022. ctl->ops = ops;
  2023. for (i = 0; i < spec->num_adc_nids; i++)
  2024. ctl->values[i] =
  2025. HDA_COMPOSE_AMP_VAL(spec->adc_nids[i], 3, 0, HDA_INPUT);
  2026. *ctl_ret = ctl;
  2027. return 0;
  2028. }
  2029. /* create capture and input-src controls for dynamic ADC-switch case */
  2030. static int create_dyn_adc_ctls(struct hda_codec *codec)
  2031. {
  2032. struct via_spec *spec = codec->spec;
  2033. struct snd_kcontrol_new *knew;
  2034. int err;
  2035. /* set up the bind capture ctls */
  2036. err = init_bind_ctl(spec, &spec->bind_cap_vol, &snd_hda_bind_vol);
  2037. if (err < 0)
  2038. return err;
  2039. err = init_bind_ctl(spec, &spec->bind_cap_sw, &snd_hda_bind_sw);
  2040. if (err < 0)
  2041. return err;
  2042. /* create capture mixer elements */
  2043. knew = via_clone_control(spec, &via_bind_cap_vol_ctl);
  2044. if (!knew)
  2045. return -ENOMEM;
  2046. knew->private_value = (long)spec->bind_cap_vol;
  2047. knew = via_clone_control(spec, &via_bind_cap_sw_ctl);
  2048. if (!knew)
  2049. return -ENOMEM;
  2050. knew->private_value = (long)spec->bind_cap_sw;
  2051. /* input-source control */
  2052. err = create_input_src_ctls(codec, 1);
  2053. if (err < 0)
  2054. return err;
  2055. return 0;
  2056. }
  2057. /* parse and create capture-related stuff */
  2058. static int via_auto_create_analog_input_ctls(struct hda_codec *codec)
  2059. {
  2060. struct via_spec *spec = codec->spec;
  2061. int err;
  2062. err = parse_analog_inputs(codec);
  2063. if (err < 0)
  2064. return err;
  2065. if (spec->dyn_adc_switch)
  2066. err = create_dyn_adc_ctls(codec);
  2067. else
  2068. err = create_multi_adc_ctls(codec);
  2069. if (err < 0)
  2070. return err;
  2071. err = create_loopback_ctls(codec);
  2072. if (err < 0)
  2073. return err;
  2074. err = create_mic_boost_ctls(codec);
  2075. if (err < 0)
  2076. return err;
  2077. return 0;
  2078. }
  2079. static void vt1708_set_pinconfig_connect(struct hda_codec *codec, hda_nid_t nid)
  2080. {
  2081. unsigned int def_conf;
  2082. unsigned char seqassoc;
  2083. def_conf = snd_hda_codec_get_pincfg(codec, nid);
  2084. seqassoc = (unsigned char) get_defcfg_association(def_conf);
  2085. seqassoc = (seqassoc << 4) | get_defcfg_sequence(def_conf);
  2086. if (get_defcfg_connect(def_conf) == AC_JACK_PORT_NONE
  2087. && (seqassoc == 0xf0 || seqassoc == 0xff)) {
  2088. def_conf = def_conf & (~(AC_JACK_PORT_BOTH << 30));
  2089. snd_hda_codec_set_pincfg(codec, nid, def_conf);
  2090. }
  2091. return;
  2092. }
  2093. static int vt1708_jack_detect_get(struct snd_kcontrol *kcontrol,
  2094. struct snd_ctl_elem_value *ucontrol)
  2095. {
  2096. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2097. struct via_spec *spec = codec->spec;
  2098. if (spec->codec_type != VT1708)
  2099. return 0;
  2100. spec->vt1708_jack_detect =
  2101. !((snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8) & 0x1);
  2102. ucontrol->value.integer.value[0] = spec->vt1708_jack_detect;
  2103. return 0;
  2104. }
  2105. static int vt1708_jack_detect_put(struct snd_kcontrol *kcontrol,
  2106. struct snd_ctl_elem_value *ucontrol)
  2107. {
  2108. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2109. struct via_spec *spec = codec->spec;
  2110. int change;
  2111. if (spec->codec_type != VT1708)
  2112. return 0;
  2113. spec->vt1708_jack_detect = ucontrol->value.integer.value[0];
  2114. change = (0x1 & (snd_hda_codec_read(codec, 0x1, 0, 0xf84, 0) >> 8))
  2115. == !spec->vt1708_jack_detect;
  2116. if (spec->vt1708_jack_detect) {
  2117. mute_aa_path(codec, 1);
  2118. notify_aa_path_ctls(codec);
  2119. }
  2120. return change;
  2121. }
  2122. static const struct snd_kcontrol_new vt1708_jack_detect_ctl = {
  2123. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2124. .name = "Jack Detect",
  2125. .count = 1,
  2126. .info = snd_ctl_boolean_mono_info,
  2127. .get = vt1708_jack_detect_get,
  2128. .put = vt1708_jack_detect_put,
  2129. };
  2130. static void fill_dig_outs(struct hda_codec *codec);
  2131. static void fill_dig_in(struct hda_codec *codec);
  2132. static int via_parse_auto_config(struct hda_codec *codec)
  2133. {
  2134. struct via_spec *spec = codec->spec;
  2135. int err;
  2136. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  2137. if (err < 0)
  2138. return err;
  2139. if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
  2140. return -EINVAL;
  2141. err = via_auto_create_multi_out_ctls(codec);
  2142. if (err < 0)
  2143. return err;
  2144. err = via_auto_create_hp_ctls(codec, spec->autocfg.hp_pins[0]);
  2145. if (err < 0)
  2146. return err;
  2147. err = via_auto_create_speaker_ctls(codec);
  2148. if (err < 0)
  2149. return err;
  2150. err = via_auto_create_analog_input_ctls(codec);
  2151. if (err < 0)
  2152. return err;
  2153. spec->multiout.max_channels = spec->multiout.num_dacs * 2;
  2154. fill_dig_outs(codec);
  2155. fill_dig_in(codec);
  2156. if (spec->kctls.list)
  2157. spec->mixers[spec->num_mixers++] = spec->kctls.list;
  2158. if (spec->hp_dac_nid && spec->hp_dep_path.depth) {
  2159. err = via_hp_build(codec);
  2160. if (err < 0)
  2161. return err;
  2162. }
  2163. err = via_smart51_build(codec);
  2164. if (err < 0)
  2165. return err;
  2166. /* assign slave outs */
  2167. if (spec->slave_dig_outs[0])
  2168. codec->slave_dig_outs = spec->slave_dig_outs;
  2169. return 1;
  2170. }
  2171. static void via_auto_init_dig_outs(struct hda_codec *codec)
  2172. {
  2173. struct via_spec *spec = codec->spec;
  2174. if (spec->multiout.dig_out_nid)
  2175. init_output_pin(codec, spec->autocfg.dig_out_pins[0], PIN_OUT);
  2176. if (spec->slave_dig_outs[0])
  2177. init_output_pin(codec, spec->autocfg.dig_out_pins[1], PIN_OUT);
  2178. }
  2179. static void via_auto_init_dig_in(struct hda_codec *codec)
  2180. {
  2181. struct via_spec *spec = codec->spec;
  2182. if (!spec->dig_in_nid)
  2183. return;
  2184. snd_hda_codec_write(codec, spec->autocfg.dig_in_pin, 0,
  2185. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN);
  2186. }
  2187. /* initialize the unsolicited events */
  2188. static void via_auto_init_unsol_event(struct hda_codec *codec)
  2189. {
  2190. struct via_spec *spec = codec->spec;
  2191. struct auto_pin_cfg *cfg = &spec->autocfg;
  2192. unsigned int ev;
  2193. int i;
  2194. if (cfg->hp_pins[0] && is_jack_detectable(codec, cfg->hp_pins[0]))
  2195. snd_hda_codec_write(codec, cfg->hp_pins[0], 0,
  2196. AC_VERB_SET_UNSOLICITED_ENABLE,
  2197. AC_USRSP_EN | VIA_HP_EVENT | VIA_JACK_EVENT);
  2198. if (cfg->speaker_pins[0])
  2199. ev = VIA_LINE_EVENT;
  2200. else
  2201. ev = 0;
  2202. for (i = 0; i < cfg->line_outs; i++) {
  2203. if (cfg->line_out_pins[i] &&
  2204. is_jack_detectable(codec, cfg->line_out_pins[i]))
  2205. snd_hda_codec_write(codec, cfg->line_out_pins[i], 0,
  2206. AC_VERB_SET_UNSOLICITED_ENABLE,
  2207. AC_USRSP_EN | ev | VIA_JACK_EVENT);
  2208. }
  2209. for (i = 0; i < cfg->num_inputs; i++) {
  2210. if (is_jack_detectable(codec, cfg->inputs[i].pin))
  2211. snd_hda_codec_write(codec, cfg->inputs[i].pin, 0,
  2212. AC_VERB_SET_UNSOLICITED_ENABLE,
  2213. AC_USRSP_EN | VIA_JACK_EVENT);
  2214. }
  2215. }
  2216. static int via_init(struct hda_codec *codec)
  2217. {
  2218. struct via_spec *spec = codec->spec;
  2219. int i;
  2220. for (i = 0; i < spec->num_iverbs; i++)
  2221. snd_hda_sequence_write(codec, spec->init_verbs[i]);
  2222. via_auto_init_multi_out(codec);
  2223. via_auto_init_hp_out(codec);
  2224. via_auto_init_speaker_out(codec);
  2225. via_auto_init_analog_input(codec);
  2226. via_auto_init_dig_outs(codec);
  2227. via_auto_init_dig_in(codec);
  2228. via_auto_init_unsol_event(codec);
  2229. via_hp_automute(codec);
  2230. return 0;
  2231. }
  2232. static void vt1708_update_hp_jack_state(struct work_struct *work)
  2233. {
  2234. struct via_spec *spec = container_of(work, struct via_spec,
  2235. vt1708_hp_work.work);
  2236. if (spec->codec_type != VT1708)
  2237. return;
  2238. /* if jack state toggled */
  2239. if (spec->vt1708_hp_present
  2240. != snd_hda_jack_detect(spec->codec, spec->autocfg.hp_pins[0])) {
  2241. spec->vt1708_hp_present ^= 1;
  2242. via_hp_automute(spec->codec);
  2243. }
  2244. vt1708_start_hp_work(spec);
  2245. }
  2246. static int get_mux_nids(struct hda_codec *codec)
  2247. {
  2248. struct via_spec *spec = codec->spec;
  2249. hda_nid_t nid, conn[8];
  2250. unsigned int type;
  2251. int i, n;
  2252. for (i = 0; i < spec->num_adc_nids; i++) {
  2253. nid = spec->adc_nids[i];
  2254. while (nid) {
  2255. type = get_wcaps_type(get_wcaps(codec, nid));
  2256. if (type == AC_WID_PIN)
  2257. break;
  2258. n = snd_hda_get_connections(codec, nid, conn,
  2259. ARRAY_SIZE(conn));
  2260. if (n <= 0)
  2261. break;
  2262. if (n > 1) {
  2263. spec->mux_nids[i] = nid;
  2264. break;
  2265. }
  2266. nid = conn[0];
  2267. }
  2268. }
  2269. return 0;
  2270. }
  2271. static int patch_vt1708(struct hda_codec *codec)
  2272. {
  2273. struct via_spec *spec;
  2274. int err;
  2275. /* create a codec specific record */
  2276. spec = via_new_spec(codec);
  2277. if (spec == NULL)
  2278. return -ENOMEM;
  2279. spec->aa_mix_nid = 0x17;
  2280. /* Add HP and CD pin config connect bit re-config action */
  2281. vt1708_set_pinconfig_connect(codec, VT1708_HP_PIN_NID);
  2282. vt1708_set_pinconfig_connect(codec, VT1708_CD_PIN_NID);
  2283. /* automatic parse from the BIOS config */
  2284. err = via_parse_auto_config(codec);
  2285. if (err < 0) {
  2286. via_free(codec);
  2287. return err;
  2288. }
  2289. /* add jack detect on/off control */
  2290. if (!via_clone_control(spec, &vt1708_jack_detect_ctl))
  2291. return -ENOMEM;
  2292. /* disable 32bit format on VT1708 */
  2293. if (codec->vendor_id == 0x11061708)
  2294. spec->stream_analog_playback = &vt1708_pcm_analog_s16_playback;
  2295. spec->init_verbs[spec->num_iverbs++] = vt1708_init_verbs;
  2296. codec->patch_ops = via_patch_ops;
  2297. INIT_DELAYED_WORK(&spec->vt1708_hp_work, vt1708_update_hp_jack_state);
  2298. return 0;
  2299. }
  2300. static int patch_vt1709(struct hda_codec *codec)
  2301. {
  2302. struct via_spec *spec;
  2303. int err;
  2304. /* create a codec specific record */
  2305. spec = via_new_spec(codec);
  2306. if (spec == NULL)
  2307. return -ENOMEM;
  2308. spec->aa_mix_nid = 0x18;
  2309. err = via_parse_auto_config(codec);
  2310. if (err < 0) {
  2311. via_free(codec);
  2312. return err;
  2313. }
  2314. codec->patch_ops = via_patch_ops;
  2315. return 0;
  2316. }
  2317. static void set_widgets_power_state_vt1708B(struct hda_codec *codec)
  2318. {
  2319. struct via_spec *spec = codec->spec;
  2320. int imux_is_smixer;
  2321. unsigned int parm;
  2322. int is_8ch = 0;
  2323. if ((spec->codec_type != VT1708B_4CH) &&
  2324. (codec->vendor_id != 0x11064397))
  2325. is_8ch = 1;
  2326. /* SW0 (17h) = stereo mixer */
  2327. imux_is_smixer =
  2328. (snd_hda_codec_read(codec, 0x17, 0, AC_VERB_GET_CONNECT_SEL, 0x00)
  2329. == ((spec->codec_type == VT1708S) ? 5 : 0));
  2330. /* inputs */
  2331. /* PW 1/2/5 (1ah/1bh/1eh) */
  2332. parm = AC_PWRST_D3;
  2333. set_pin_power_state(codec, 0x1a, &parm);
  2334. set_pin_power_state(codec, 0x1b, &parm);
  2335. set_pin_power_state(codec, 0x1e, &parm);
  2336. if (imux_is_smixer)
  2337. parm = AC_PWRST_D0;
  2338. /* SW0 (17h), AIW 0/1 (13h/14h) */
  2339. snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
  2340. snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
  2341. snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
  2342. /* outputs */
  2343. /* PW0 (19h), SW1 (18h), AOW1 (11h) */
  2344. parm = AC_PWRST_D3;
  2345. set_pin_power_state(codec, 0x19, &parm);
  2346. if (spec->smart51_enabled)
  2347. set_pin_power_state(codec, 0x1b, &parm);
  2348. snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
  2349. snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
  2350. /* PW6 (22h), SW2 (26h), AOW2 (24h) */
  2351. if (is_8ch) {
  2352. parm = AC_PWRST_D3;
  2353. set_pin_power_state(codec, 0x22, &parm);
  2354. if (spec->smart51_enabled)
  2355. set_pin_power_state(codec, 0x1a, &parm);
  2356. snd_hda_codec_write(codec, 0x26, 0,
  2357. AC_VERB_SET_POWER_STATE, parm);
  2358. snd_hda_codec_write(codec, 0x24, 0,
  2359. AC_VERB_SET_POWER_STATE, parm);
  2360. } else if (codec->vendor_id == 0x11064397) {
  2361. /* PW7(23h), SW2(27h), AOW2(25h) */
  2362. parm = AC_PWRST_D3;
  2363. set_pin_power_state(codec, 0x23, &parm);
  2364. if (spec->smart51_enabled)
  2365. set_pin_power_state(codec, 0x1a, &parm);
  2366. snd_hda_codec_write(codec, 0x27, 0,
  2367. AC_VERB_SET_POWER_STATE, parm);
  2368. snd_hda_codec_write(codec, 0x25, 0,
  2369. AC_VERB_SET_POWER_STATE, parm);
  2370. }
  2371. /* PW 3/4/7 (1ch/1dh/23h) */
  2372. parm = AC_PWRST_D3;
  2373. /* force to D0 for internal Speaker */
  2374. set_pin_power_state(codec, 0x1c, &parm);
  2375. set_pin_power_state(codec, 0x1d, &parm);
  2376. if (is_8ch)
  2377. set_pin_power_state(codec, 0x23, &parm);
  2378. /* MW0 (16h), Sw3 (27h), AOW 0/3 (10h/25h) */
  2379. snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
  2380. imux_is_smixer ? AC_PWRST_D0 : parm);
  2381. snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
  2382. if (is_8ch) {
  2383. snd_hda_codec_write(codec, 0x25, 0,
  2384. AC_VERB_SET_POWER_STATE, parm);
  2385. snd_hda_codec_write(codec, 0x27, 0,
  2386. AC_VERB_SET_POWER_STATE, parm);
  2387. } else if (codec->vendor_id == 0x11064397 && spec->hp_independent_mode)
  2388. snd_hda_codec_write(codec, 0x25, 0,
  2389. AC_VERB_SET_POWER_STATE, parm);
  2390. }
  2391. static int patch_vt1708S(struct hda_codec *codec);
  2392. static int patch_vt1708B(struct hda_codec *codec)
  2393. {
  2394. struct via_spec *spec;
  2395. int err;
  2396. if (get_codec_type(codec) == VT1708BCE)
  2397. return patch_vt1708S(codec);
  2398. /* create a codec specific record */
  2399. spec = via_new_spec(codec);
  2400. if (spec == NULL)
  2401. return -ENOMEM;
  2402. spec->aa_mix_nid = 0x16;
  2403. /* automatic parse from the BIOS config */
  2404. err = via_parse_auto_config(codec);
  2405. if (err < 0) {
  2406. via_free(codec);
  2407. return err;
  2408. }
  2409. codec->patch_ops = via_patch_ops;
  2410. spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
  2411. return 0;
  2412. }
  2413. /* Patch for VT1708S */
  2414. static const struct hda_verb vt1708S_init_verbs[] = {
  2415. /* Enable Mic Boost Volume backdoor */
  2416. {0x1, 0xf98, 0x1},
  2417. /* don't bybass mixer */
  2418. {0x1, 0xf88, 0xc0},
  2419. { }
  2420. };
  2421. /* fill out digital output widgets; one for master and one for slave outputs */
  2422. static void fill_dig_outs(struct hda_codec *codec)
  2423. {
  2424. struct via_spec *spec = codec->spec;
  2425. int i;
  2426. for (i = 0; i < spec->autocfg.dig_outs; i++) {
  2427. hda_nid_t nid;
  2428. int conn;
  2429. nid = spec->autocfg.dig_out_pins[i];
  2430. if (!nid)
  2431. continue;
  2432. conn = snd_hda_get_connections(codec, nid, &nid, 1);
  2433. if (conn < 1)
  2434. continue;
  2435. if (!spec->multiout.dig_out_nid)
  2436. spec->multiout.dig_out_nid = nid;
  2437. else {
  2438. spec->slave_dig_outs[0] = nid;
  2439. break; /* at most two dig outs */
  2440. }
  2441. }
  2442. }
  2443. static void fill_dig_in(struct hda_codec *codec)
  2444. {
  2445. struct via_spec *spec = codec->spec;
  2446. hda_nid_t dig_nid;
  2447. int i, err;
  2448. if (!spec->autocfg.dig_in_pin)
  2449. return;
  2450. dig_nid = codec->start_nid;
  2451. for (i = 0; i < codec->num_nodes; i++, dig_nid++) {
  2452. unsigned int wcaps = get_wcaps(codec, dig_nid);
  2453. if (get_wcaps_type(wcaps) != AC_WID_AUD_IN)
  2454. continue;
  2455. if (!(wcaps & AC_WCAP_DIGITAL))
  2456. continue;
  2457. if (!(wcaps & AC_WCAP_CONN_LIST))
  2458. continue;
  2459. err = get_connection_index(codec, dig_nid,
  2460. spec->autocfg.dig_in_pin);
  2461. if (err >= 0) {
  2462. spec->dig_in_nid = dig_nid;
  2463. break;
  2464. }
  2465. }
  2466. }
  2467. static void override_mic_boost(struct hda_codec *codec, hda_nid_t pin,
  2468. int offset, int num_steps, int step_size)
  2469. {
  2470. snd_hda_override_amp_caps(codec, pin, HDA_INPUT,
  2471. (offset << AC_AMPCAP_OFFSET_SHIFT) |
  2472. (num_steps << AC_AMPCAP_NUM_STEPS_SHIFT) |
  2473. (step_size << AC_AMPCAP_STEP_SIZE_SHIFT) |
  2474. (0 << AC_AMPCAP_MUTE_SHIFT));
  2475. }
  2476. static int patch_vt1708S(struct hda_codec *codec)
  2477. {
  2478. struct via_spec *spec;
  2479. int err;
  2480. /* create a codec specific record */
  2481. spec = via_new_spec(codec);
  2482. if (spec == NULL)
  2483. return -ENOMEM;
  2484. spec->aa_mix_nid = 0x16;
  2485. override_mic_boost(codec, 0x1a, 0, 3, 40);
  2486. override_mic_boost(codec, 0x1e, 0, 3, 40);
  2487. /* automatic parse from the BIOS config */
  2488. err = via_parse_auto_config(codec);
  2489. if (err < 0) {
  2490. via_free(codec);
  2491. return err;
  2492. }
  2493. spec->init_verbs[spec->num_iverbs++] = vt1708S_init_verbs;
  2494. codec->patch_ops = via_patch_ops;
  2495. /* correct names for VT1708BCE */
  2496. if (get_codec_type(codec) == VT1708BCE) {
  2497. kfree(codec->chip_name);
  2498. codec->chip_name = kstrdup("VT1708BCE", GFP_KERNEL);
  2499. snprintf(codec->bus->card->mixername,
  2500. sizeof(codec->bus->card->mixername),
  2501. "%s %s", codec->vendor_name, codec->chip_name);
  2502. }
  2503. /* correct names for VT1705 */
  2504. if (codec->vendor_id == 0x11064397) {
  2505. kfree(codec->chip_name);
  2506. codec->chip_name = kstrdup("VT1705", GFP_KERNEL);
  2507. snprintf(codec->bus->card->mixername,
  2508. sizeof(codec->bus->card->mixername),
  2509. "%s %s", codec->vendor_name, codec->chip_name);
  2510. }
  2511. spec->set_widgets_power_state = set_widgets_power_state_vt1708B;
  2512. return 0;
  2513. }
  2514. /* Patch for VT1702 */
  2515. static const struct hda_verb vt1702_init_verbs[] = {
  2516. /* mixer enable */
  2517. {0x1, 0xF88, 0x3},
  2518. /* GPIO 0~2 */
  2519. {0x1, 0xF82, 0x3F},
  2520. { }
  2521. };
  2522. static void set_widgets_power_state_vt1702(struct hda_codec *codec)
  2523. {
  2524. int imux_is_smixer =
  2525. snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
  2526. unsigned int parm;
  2527. /* inputs */
  2528. /* PW 1/2/5 (14h/15h/18h) */
  2529. parm = AC_PWRST_D3;
  2530. set_pin_power_state(codec, 0x14, &parm);
  2531. set_pin_power_state(codec, 0x15, &parm);
  2532. set_pin_power_state(codec, 0x18, &parm);
  2533. if (imux_is_smixer)
  2534. parm = AC_PWRST_D0; /* SW0 (13h) = stereo mixer (idx 3) */
  2535. /* SW0 (13h), AIW 0/1/2 (12h/1fh/20h) */
  2536. snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
  2537. snd_hda_codec_write(codec, 0x12, 0, AC_VERB_SET_POWER_STATE, parm);
  2538. snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
  2539. snd_hda_codec_write(codec, 0x20, 0, AC_VERB_SET_POWER_STATE, parm);
  2540. /* outputs */
  2541. /* PW 3/4 (16h/17h) */
  2542. parm = AC_PWRST_D3;
  2543. set_pin_power_state(codec, 0x17, &parm);
  2544. set_pin_power_state(codec, 0x16, &parm);
  2545. /* MW0 (1ah), AOW 0/1 (10h/1dh) */
  2546. snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE,
  2547. imux_is_smixer ? AC_PWRST_D0 : parm);
  2548. snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
  2549. snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
  2550. }
  2551. static int patch_vt1702(struct hda_codec *codec)
  2552. {
  2553. struct via_spec *spec;
  2554. int err;
  2555. /* create a codec specific record */
  2556. spec = via_new_spec(codec);
  2557. if (spec == NULL)
  2558. return -ENOMEM;
  2559. spec->aa_mix_nid = 0x1a;
  2560. /* limit AA path volume to 0 dB */
  2561. snd_hda_override_amp_caps(codec, 0x1A, HDA_INPUT,
  2562. (0x17 << AC_AMPCAP_OFFSET_SHIFT) |
  2563. (0x17 << AC_AMPCAP_NUM_STEPS_SHIFT) |
  2564. (0x5 << AC_AMPCAP_STEP_SIZE_SHIFT) |
  2565. (1 << AC_AMPCAP_MUTE_SHIFT));
  2566. /* automatic parse from the BIOS config */
  2567. err = via_parse_auto_config(codec);
  2568. if (err < 0) {
  2569. via_free(codec);
  2570. return err;
  2571. }
  2572. spec->init_verbs[spec->num_iverbs++] = vt1702_init_verbs;
  2573. codec->patch_ops = via_patch_ops;
  2574. spec->set_widgets_power_state = set_widgets_power_state_vt1702;
  2575. return 0;
  2576. }
  2577. /* Patch for VT1718S */
  2578. static const struct hda_verb vt1718S_init_verbs[] = {
  2579. /* Enable MW0 adjust Gain 5 */
  2580. {0x1, 0xfb2, 0x10},
  2581. /* Enable Boost Volume backdoor */
  2582. {0x1, 0xf88, 0x8},
  2583. { }
  2584. };
  2585. static void set_widgets_power_state_vt1718S(struct hda_codec *codec)
  2586. {
  2587. struct via_spec *spec = codec->spec;
  2588. int imux_is_smixer;
  2589. unsigned int parm;
  2590. /* MUX6 (1eh) = stereo mixer */
  2591. imux_is_smixer =
  2592. snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
  2593. /* inputs */
  2594. /* PW 5/6/7 (29h/2ah/2bh) */
  2595. parm = AC_PWRST_D3;
  2596. set_pin_power_state(codec, 0x29, &parm);
  2597. set_pin_power_state(codec, 0x2a, &parm);
  2598. set_pin_power_state(codec, 0x2b, &parm);
  2599. if (imux_is_smixer)
  2600. parm = AC_PWRST_D0;
  2601. /* MUX6/7 (1eh/1fh), AIW 0/1 (10h/11h) */
  2602. snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
  2603. snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
  2604. snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
  2605. snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
  2606. /* outputs */
  2607. /* PW3 (27h), MW2 (1ah), AOW3 (bh) */
  2608. parm = AC_PWRST_D3;
  2609. set_pin_power_state(codec, 0x27, &parm);
  2610. snd_hda_codec_write(codec, 0x1a, 0, AC_VERB_SET_POWER_STATE, parm);
  2611. snd_hda_codec_write(codec, 0xb, 0, AC_VERB_SET_POWER_STATE, parm);
  2612. /* PW2 (26h), AOW2 (ah) */
  2613. parm = AC_PWRST_D3;
  2614. set_pin_power_state(codec, 0x26, &parm);
  2615. if (spec->smart51_enabled)
  2616. set_pin_power_state(codec, 0x2b, &parm);
  2617. snd_hda_codec_write(codec, 0xa, 0, AC_VERB_SET_POWER_STATE, parm);
  2618. /* PW0 (24h), AOW0 (8h) */
  2619. parm = AC_PWRST_D3;
  2620. set_pin_power_state(codec, 0x24, &parm);
  2621. if (!spec->hp_independent_mode) /* check for redirected HP */
  2622. set_pin_power_state(codec, 0x28, &parm);
  2623. snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
  2624. /* MW9 (21h), Mw2 (1ah), AOW0 (8h) */
  2625. snd_hda_codec_write(codec, 0x21, 0, AC_VERB_SET_POWER_STATE,
  2626. imux_is_smixer ? AC_PWRST_D0 : parm);
  2627. /* PW1 (25h), AOW1 (9h) */
  2628. parm = AC_PWRST_D3;
  2629. set_pin_power_state(codec, 0x25, &parm);
  2630. if (spec->smart51_enabled)
  2631. set_pin_power_state(codec, 0x2a, &parm);
  2632. snd_hda_codec_write(codec, 0x9, 0, AC_VERB_SET_POWER_STATE, parm);
  2633. if (spec->hp_independent_mode) {
  2634. /* PW4 (28h), MW3 (1bh), MUX1(34h), AOW4 (ch) */
  2635. parm = AC_PWRST_D3;
  2636. set_pin_power_state(codec, 0x28, &parm);
  2637. snd_hda_codec_write(codec, 0x1b, 0,
  2638. AC_VERB_SET_POWER_STATE, parm);
  2639. snd_hda_codec_write(codec, 0x34, 0,
  2640. AC_VERB_SET_POWER_STATE, parm);
  2641. snd_hda_codec_write(codec, 0xc, 0,
  2642. AC_VERB_SET_POWER_STATE, parm);
  2643. }
  2644. }
  2645. static int patch_vt1718S(struct hda_codec *codec)
  2646. {
  2647. struct via_spec *spec;
  2648. int err;
  2649. /* create a codec specific record */
  2650. spec = via_new_spec(codec);
  2651. if (spec == NULL)
  2652. return -ENOMEM;
  2653. spec->aa_mix_nid = 0x21;
  2654. override_mic_boost(codec, 0x2b, 0, 3, 40);
  2655. override_mic_boost(codec, 0x29, 0, 3, 40);
  2656. spec->dac_mixer_idx = 5;
  2657. /* automatic parse from the BIOS config */
  2658. err = via_parse_auto_config(codec);
  2659. if (err < 0) {
  2660. via_free(codec);
  2661. return err;
  2662. }
  2663. spec->init_verbs[spec->num_iverbs++] = vt1718S_init_verbs;
  2664. codec->patch_ops = via_patch_ops;
  2665. spec->set_widgets_power_state = set_widgets_power_state_vt1718S;
  2666. return 0;
  2667. }
  2668. /* Patch for VT1716S */
  2669. static int vt1716s_dmic_info(struct snd_kcontrol *kcontrol,
  2670. struct snd_ctl_elem_info *uinfo)
  2671. {
  2672. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  2673. uinfo->count = 1;
  2674. uinfo->value.integer.min = 0;
  2675. uinfo->value.integer.max = 1;
  2676. return 0;
  2677. }
  2678. static int vt1716s_dmic_get(struct snd_kcontrol *kcontrol,
  2679. struct snd_ctl_elem_value *ucontrol)
  2680. {
  2681. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2682. int index = 0;
  2683. index = snd_hda_codec_read(codec, 0x26, 0,
  2684. AC_VERB_GET_CONNECT_SEL, 0);
  2685. if (index != -1)
  2686. *ucontrol->value.integer.value = index;
  2687. return 0;
  2688. }
  2689. static int vt1716s_dmic_put(struct snd_kcontrol *kcontrol,
  2690. struct snd_ctl_elem_value *ucontrol)
  2691. {
  2692. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  2693. struct via_spec *spec = codec->spec;
  2694. int index = *ucontrol->value.integer.value;
  2695. snd_hda_codec_write(codec, 0x26, 0,
  2696. AC_VERB_SET_CONNECT_SEL, index);
  2697. spec->dmic_enabled = index;
  2698. set_widgets_power_state(codec);
  2699. return 1;
  2700. }
  2701. static const struct snd_kcontrol_new vt1716s_dmic_mixer[] = {
  2702. HDA_CODEC_VOLUME("Digital Mic Capture Volume", 0x22, 0x0, HDA_INPUT),
  2703. {
  2704. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  2705. .name = "Digital Mic Capture Switch",
  2706. .subdevice = HDA_SUBDEV_NID_FLAG | 0x26,
  2707. .count = 1,
  2708. .info = vt1716s_dmic_info,
  2709. .get = vt1716s_dmic_get,
  2710. .put = vt1716s_dmic_put,
  2711. },
  2712. {} /* end */
  2713. };
  2714. /* mono-out mixer elements */
  2715. static const struct snd_kcontrol_new vt1716S_mono_out_mixer[] = {
  2716. HDA_CODEC_MUTE("Mono Playback Switch", 0x2a, 0x0, HDA_OUTPUT),
  2717. { } /* end */
  2718. };
  2719. static const struct hda_verb vt1716S_init_verbs[] = {
  2720. /* Enable Boost Volume backdoor */
  2721. {0x1, 0xf8a, 0x80},
  2722. /* don't bybass mixer */
  2723. {0x1, 0xf88, 0xc0},
  2724. /* Enable mono output */
  2725. {0x1, 0xf90, 0x08},
  2726. { }
  2727. };
  2728. static void set_widgets_power_state_vt1716S(struct hda_codec *codec)
  2729. {
  2730. struct via_spec *spec = codec->spec;
  2731. int imux_is_smixer;
  2732. unsigned int parm;
  2733. unsigned int mono_out, present;
  2734. /* SW0 (17h) = stereo mixer */
  2735. imux_is_smixer =
  2736. (snd_hda_codec_read(codec, 0x17, 0,
  2737. AC_VERB_GET_CONNECT_SEL, 0x00) == 5);
  2738. /* inputs */
  2739. /* PW 1/2/5 (1ah/1bh/1eh) */
  2740. parm = AC_PWRST_D3;
  2741. set_pin_power_state(codec, 0x1a, &parm);
  2742. set_pin_power_state(codec, 0x1b, &parm);
  2743. set_pin_power_state(codec, 0x1e, &parm);
  2744. if (imux_is_smixer)
  2745. parm = AC_PWRST_D0;
  2746. /* SW0 (17h), AIW0(13h) */
  2747. snd_hda_codec_write(codec, 0x17, 0, AC_VERB_SET_POWER_STATE, parm);
  2748. snd_hda_codec_write(codec, 0x13, 0, AC_VERB_SET_POWER_STATE, parm);
  2749. parm = AC_PWRST_D3;
  2750. set_pin_power_state(codec, 0x1e, &parm);
  2751. /* PW11 (22h) */
  2752. if (spec->dmic_enabled)
  2753. set_pin_power_state(codec, 0x22, &parm);
  2754. else
  2755. snd_hda_codec_write(codec, 0x22, 0,
  2756. AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
  2757. /* SW2(26h), AIW1(14h) */
  2758. snd_hda_codec_write(codec, 0x26, 0, AC_VERB_SET_POWER_STATE, parm);
  2759. snd_hda_codec_write(codec, 0x14, 0, AC_VERB_SET_POWER_STATE, parm);
  2760. /* outputs */
  2761. /* PW0 (19h), SW1 (18h), AOW1 (11h) */
  2762. parm = AC_PWRST_D3;
  2763. set_pin_power_state(codec, 0x19, &parm);
  2764. /* Smart 5.1 PW2(1bh) */
  2765. if (spec->smart51_enabled)
  2766. set_pin_power_state(codec, 0x1b, &parm);
  2767. snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
  2768. snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
  2769. /* PW7 (23h), SW3 (27h), AOW3 (25h) */
  2770. parm = AC_PWRST_D3;
  2771. set_pin_power_state(codec, 0x23, &parm);
  2772. /* Smart 5.1 PW1(1ah) */
  2773. if (spec->smart51_enabled)
  2774. set_pin_power_state(codec, 0x1a, &parm);
  2775. snd_hda_codec_write(codec, 0x27, 0, AC_VERB_SET_POWER_STATE, parm);
  2776. /* Smart 5.1 PW5(1eh) */
  2777. if (spec->smart51_enabled)
  2778. set_pin_power_state(codec, 0x1e, &parm);
  2779. snd_hda_codec_write(codec, 0x25, 0, AC_VERB_SET_POWER_STATE, parm);
  2780. /* Mono out */
  2781. /* SW4(28h)->MW1(29h)-> PW12 (2ah)*/
  2782. present = snd_hda_jack_detect(codec, 0x1c);
  2783. if (present)
  2784. mono_out = 0;
  2785. else {
  2786. present = snd_hda_jack_detect(codec, 0x1d);
  2787. if (!spec->hp_independent_mode && present)
  2788. mono_out = 0;
  2789. else
  2790. mono_out = 1;
  2791. }
  2792. parm = mono_out ? AC_PWRST_D0 : AC_PWRST_D3;
  2793. snd_hda_codec_write(codec, 0x28, 0, AC_VERB_SET_POWER_STATE, parm);
  2794. snd_hda_codec_write(codec, 0x29, 0, AC_VERB_SET_POWER_STATE, parm);
  2795. snd_hda_codec_write(codec, 0x2a, 0, AC_VERB_SET_POWER_STATE, parm);
  2796. /* PW 3/4 (1ch/1dh) */
  2797. parm = AC_PWRST_D3;
  2798. set_pin_power_state(codec, 0x1c, &parm);
  2799. set_pin_power_state(codec, 0x1d, &parm);
  2800. /* HP Independent Mode, power on AOW3 */
  2801. if (spec->hp_independent_mode)
  2802. snd_hda_codec_write(codec, 0x25, 0,
  2803. AC_VERB_SET_POWER_STATE, parm);
  2804. /* force to D0 for internal Speaker */
  2805. /* MW0 (16h), AOW0 (10h) */
  2806. snd_hda_codec_write(codec, 0x16, 0, AC_VERB_SET_POWER_STATE,
  2807. imux_is_smixer ? AC_PWRST_D0 : parm);
  2808. snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE,
  2809. mono_out ? AC_PWRST_D0 : parm);
  2810. }
  2811. static int patch_vt1716S(struct hda_codec *codec)
  2812. {
  2813. struct via_spec *spec;
  2814. int err;
  2815. /* create a codec specific record */
  2816. spec = via_new_spec(codec);
  2817. if (spec == NULL)
  2818. return -ENOMEM;
  2819. spec->aa_mix_nid = 0x16;
  2820. override_mic_boost(codec, 0x1a, 0, 3, 40);
  2821. override_mic_boost(codec, 0x1e, 0, 3, 40);
  2822. /* automatic parse from the BIOS config */
  2823. err = via_parse_auto_config(codec);
  2824. if (err < 0) {
  2825. via_free(codec);
  2826. return err;
  2827. }
  2828. spec->init_verbs[spec->num_iverbs++] = vt1716S_init_verbs;
  2829. spec->mixers[spec->num_mixers] = vt1716s_dmic_mixer;
  2830. spec->num_mixers++;
  2831. spec->mixers[spec->num_mixers++] = vt1716S_mono_out_mixer;
  2832. codec->patch_ops = via_patch_ops;
  2833. spec->set_widgets_power_state = set_widgets_power_state_vt1716S;
  2834. return 0;
  2835. }
  2836. /* for vt2002P */
  2837. static const struct hda_verb vt2002P_init_verbs[] = {
  2838. /* Class-D speaker related verbs */
  2839. {0x1, 0xfe0, 0x4},
  2840. {0x1, 0xfe9, 0x80},
  2841. {0x1, 0xfe2, 0x22},
  2842. /* Enable Boost Volume backdoor */
  2843. {0x1, 0xfb9, 0x24},
  2844. /* Enable AOW0 to MW9 */
  2845. {0x1, 0xfb8, 0x88},
  2846. { }
  2847. };
  2848. static const struct hda_verb vt1802_init_verbs[] = {
  2849. /* Enable Boost Volume backdoor */
  2850. {0x1, 0xfb9, 0x24},
  2851. /* Enable AOW0 to MW9 */
  2852. {0x1, 0xfb8, 0x88},
  2853. { }
  2854. };
  2855. static void set_widgets_power_state_vt2002P(struct hda_codec *codec)
  2856. {
  2857. struct via_spec *spec = codec->spec;
  2858. int imux_is_smixer;
  2859. unsigned int parm;
  2860. unsigned int present;
  2861. /* MUX9 (1eh) = stereo mixer */
  2862. imux_is_smixer =
  2863. snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
  2864. /* inputs */
  2865. /* PW 5/6/7 (29h/2ah/2bh) */
  2866. parm = AC_PWRST_D3;
  2867. set_pin_power_state(codec, 0x29, &parm);
  2868. set_pin_power_state(codec, 0x2a, &parm);
  2869. set_pin_power_state(codec, 0x2b, &parm);
  2870. parm = AC_PWRST_D0;
  2871. /* MUX9/10 (1eh/1fh), AIW 0/1 (10h/11h) */
  2872. snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
  2873. snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
  2874. snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
  2875. snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
  2876. /* outputs */
  2877. /* AOW0 (8h)*/
  2878. snd_hda_codec_write(codec, 0x8, 0, AC_VERB_SET_POWER_STATE, parm);
  2879. if (spec->codec_type == VT1802) {
  2880. /* PW4 (28h), MW4 (18h), MUX4(38h) */
  2881. parm = AC_PWRST_D3;
  2882. set_pin_power_state(codec, 0x28, &parm);
  2883. snd_hda_codec_write(codec, 0x18, 0,
  2884. AC_VERB_SET_POWER_STATE, parm);
  2885. snd_hda_codec_write(codec, 0x38, 0,
  2886. AC_VERB_SET_POWER_STATE, parm);
  2887. } else {
  2888. /* PW4 (26h), MW4 (1ch), MUX4(37h) */
  2889. parm = AC_PWRST_D3;
  2890. set_pin_power_state(codec, 0x26, &parm);
  2891. snd_hda_codec_write(codec, 0x1c, 0,
  2892. AC_VERB_SET_POWER_STATE, parm);
  2893. snd_hda_codec_write(codec, 0x37, 0,
  2894. AC_VERB_SET_POWER_STATE, parm);
  2895. }
  2896. if (spec->codec_type == VT1802) {
  2897. /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
  2898. parm = AC_PWRST_D3;
  2899. set_pin_power_state(codec, 0x25, &parm);
  2900. snd_hda_codec_write(codec, 0x15, 0,
  2901. AC_VERB_SET_POWER_STATE, parm);
  2902. snd_hda_codec_write(codec, 0x35, 0,
  2903. AC_VERB_SET_POWER_STATE, parm);
  2904. } else {
  2905. /* PW1 (25h), MW1 (19h), MUX1(35h), AOW1 (9h) */
  2906. parm = AC_PWRST_D3;
  2907. set_pin_power_state(codec, 0x25, &parm);
  2908. snd_hda_codec_write(codec, 0x19, 0,
  2909. AC_VERB_SET_POWER_STATE, parm);
  2910. snd_hda_codec_write(codec, 0x35, 0,
  2911. AC_VERB_SET_POWER_STATE, parm);
  2912. }
  2913. if (spec->hp_independent_mode)
  2914. snd_hda_codec_write(codec, 0x9, 0,
  2915. AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
  2916. /* Class-D */
  2917. /* PW0 (24h), MW0(18h/14h), MUX0(34h) */
  2918. present = snd_hda_jack_detect(codec, 0x25);
  2919. parm = AC_PWRST_D3;
  2920. set_pin_power_state(codec, 0x24, &parm);
  2921. parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
  2922. if (spec->codec_type == VT1802)
  2923. snd_hda_codec_write(codec, 0x14, 0,
  2924. AC_VERB_SET_POWER_STATE, parm);
  2925. else
  2926. snd_hda_codec_write(codec, 0x18, 0,
  2927. AC_VERB_SET_POWER_STATE, parm);
  2928. snd_hda_codec_write(codec, 0x34, 0, AC_VERB_SET_POWER_STATE, parm);
  2929. /* Mono Out */
  2930. present = snd_hda_jack_detect(codec, 0x26);
  2931. parm = present ? AC_PWRST_D3 : AC_PWRST_D0;
  2932. if (spec->codec_type == VT1802) {
  2933. /* PW15 (33h), MW8(1ch), MUX8(3ch) */
  2934. snd_hda_codec_write(codec, 0x33, 0,
  2935. AC_VERB_SET_POWER_STATE, parm);
  2936. snd_hda_codec_write(codec, 0x1c, 0,
  2937. AC_VERB_SET_POWER_STATE, parm);
  2938. snd_hda_codec_write(codec, 0x3c, 0,
  2939. AC_VERB_SET_POWER_STATE, parm);
  2940. } else {
  2941. /* PW15 (31h), MW8(17h), MUX8(3bh) */
  2942. snd_hda_codec_write(codec, 0x31, 0,
  2943. AC_VERB_SET_POWER_STATE, parm);
  2944. snd_hda_codec_write(codec, 0x17, 0,
  2945. AC_VERB_SET_POWER_STATE, parm);
  2946. snd_hda_codec_write(codec, 0x3b, 0,
  2947. AC_VERB_SET_POWER_STATE, parm);
  2948. }
  2949. /* MW9 (21h) */
  2950. if (imux_is_smixer || !is_aa_path_mute(codec))
  2951. snd_hda_codec_write(codec, 0x21, 0,
  2952. AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
  2953. else
  2954. snd_hda_codec_write(codec, 0x21, 0,
  2955. AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
  2956. }
  2957. /* patch for vt2002P */
  2958. static int patch_vt2002P(struct hda_codec *codec)
  2959. {
  2960. struct via_spec *spec;
  2961. int err;
  2962. /* create a codec specific record */
  2963. spec = via_new_spec(codec);
  2964. if (spec == NULL)
  2965. return -ENOMEM;
  2966. spec->aa_mix_nid = 0x21;
  2967. spec->dac_mixer_idx = 3;
  2968. override_mic_boost(codec, 0x2b, 0, 3, 40);
  2969. override_mic_boost(codec, 0x29, 0, 3, 40);
  2970. /* automatic parse from the BIOS config */
  2971. err = via_parse_auto_config(codec);
  2972. if (err < 0) {
  2973. via_free(codec);
  2974. return err;
  2975. }
  2976. if (spec->codec_type == VT1802)
  2977. spec->init_verbs[spec->num_iverbs++] = vt1802_init_verbs;
  2978. else
  2979. spec->init_verbs[spec->num_iverbs++] = vt2002P_init_verbs;
  2980. codec->patch_ops = via_patch_ops;
  2981. spec->set_widgets_power_state = set_widgets_power_state_vt2002P;
  2982. return 0;
  2983. }
  2984. /* for vt1812 */
  2985. static const struct hda_verb vt1812_init_verbs[] = {
  2986. /* Enable Boost Volume backdoor */
  2987. {0x1, 0xfb9, 0x24},
  2988. /* Enable AOW0 to MW9 */
  2989. {0x1, 0xfb8, 0xa8},
  2990. { }
  2991. };
  2992. static void set_widgets_power_state_vt1812(struct hda_codec *codec)
  2993. {
  2994. struct via_spec *spec = codec->spec;
  2995. int imux_is_smixer =
  2996. snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 3;
  2997. unsigned int parm;
  2998. unsigned int present;
  2999. /* MUX10 (1eh) = stereo mixer */
  3000. imux_is_smixer =
  3001. snd_hda_codec_read(codec, 0x1e, 0, AC_VERB_GET_CONNECT_SEL, 0x00) == 5;
  3002. /* inputs */
  3003. /* PW 5/6/7 (29h/2ah/2bh) */
  3004. parm = AC_PWRST_D3;
  3005. set_pin_power_state(codec, 0x29, &parm);
  3006. set_pin_power_state(codec, 0x2a, &parm);
  3007. set_pin_power_state(codec, 0x2b, &parm);
  3008. parm = AC_PWRST_D0;
  3009. /* MUX10/11 (1eh/1fh), AIW 0/1 (10h/11h) */
  3010. snd_hda_codec_write(codec, 0x1e, 0, AC_VERB_SET_POWER_STATE, parm);
  3011. snd_hda_codec_write(codec, 0x1f, 0, AC_VERB_SET_POWER_STATE, parm);
  3012. snd_hda_codec_write(codec, 0x10, 0, AC_VERB_SET_POWER_STATE, parm);
  3013. snd_hda_codec_write(codec, 0x11, 0, AC_VERB_SET_POWER_STATE, parm);
  3014. /* outputs */
  3015. /* AOW0 (8h)*/
  3016. snd_hda_codec_write(codec, 0x8, 0,
  3017. AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
  3018. /* PW4 (28h), MW4 (18h), MUX4(38h) */
  3019. parm = AC_PWRST_D3;
  3020. set_pin_power_state(codec, 0x28, &parm);
  3021. snd_hda_codec_write(codec, 0x18, 0, AC_VERB_SET_POWER_STATE, parm);
  3022. snd_hda_codec_write(codec, 0x38, 0, AC_VERB_SET_POWER_STATE, parm);
  3023. /* PW1 (25h), MW1 (15h), MUX1(35h), AOW1 (9h) */
  3024. parm = AC_PWRST_D3;
  3025. set_pin_power_state(codec, 0x25, &parm);
  3026. snd_hda_codec_write(codec, 0x15, 0, AC_VERB_SET_POWER_STATE, parm);
  3027. snd_hda_codec_write(codec, 0x35, 0, AC_VERB_SET_POWER_STATE, parm);
  3028. if (spec->hp_independent_mode)
  3029. snd_hda_codec_write(codec, 0x9, 0,
  3030. AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
  3031. /* Internal Speaker */
  3032. /* PW0 (24h), MW0(14h), MUX0(34h) */
  3033. present = snd_hda_jack_detect(codec, 0x25);
  3034. parm = AC_PWRST_D3;
  3035. set_pin_power_state(codec, 0x24, &parm);
  3036. if (present) {
  3037. snd_hda_codec_write(codec, 0x14, 0,
  3038. AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
  3039. snd_hda_codec_write(codec, 0x34, 0,
  3040. AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
  3041. } else {
  3042. snd_hda_codec_write(codec, 0x14, 0,
  3043. AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
  3044. snd_hda_codec_write(codec, 0x34, 0,
  3045. AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
  3046. }
  3047. /* Mono Out */
  3048. /* PW13 (31h), MW13(1ch), MUX13(3ch), MW14(3eh) */
  3049. present = snd_hda_jack_detect(codec, 0x28);
  3050. parm = AC_PWRST_D3;
  3051. set_pin_power_state(codec, 0x31, &parm);
  3052. if (present) {
  3053. snd_hda_codec_write(codec, 0x1c, 0,
  3054. AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
  3055. snd_hda_codec_write(codec, 0x3c, 0,
  3056. AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
  3057. snd_hda_codec_write(codec, 0x3e, 0,
  3058. AC_VERB_SET_POWER_STATE, AC_PWRST_D3);
  3059. } else {
  3060. snd_hda_codec_write(codec, 0x1c, 0,
  3061. AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
  3062. snd_hda_codec_write(codec, 0x3c, 0,
  3063. AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
  3064. snd_hda_codec_write(codec, 0x3e, 0,
  3065. AC_VERB_SET_POWER_STATE, AC_PWRST_D0);
  3066. }
  3067. /* PW15 (33h), MW15 (1dh), MUX15(3dh) */
  3068. parm = AC_PWRST_D3;
  3069. set_pin_power_state(codec, 0x33, &parm);
  3070. snd_hda_codec_write(codec, 0x1d, 0, AC_VERB_SET_POWER_STATE, parm);
  3071. snd_hda_codec_write(codec, 0x3d, 0, AC_VERB_SET_POWER_STATE, parm);
  3072. }
  3073. /* patch for vt1812 */
  3074. static int patch_vt1812(struct hda_codec *codec)
  3075. {
  3076. struct via_spec *spec;
  3077. int err;
  3078. /* create a codec specific record */
  3079. spec = via_new_spec(codec);
  3080. if (spec == NULL)
  3081. return -ENOMEM;
  3082. spec->aa_mix_nid = 0x21;
  3083. override_mic_boost(codec, 0x2b, 0, 3, 40);
  3084. override_mic_boost(codec, 0x29, 0, 3, 40);
  3085. spec->dac_mixer_idx = 5;
  3086. /* automatic parse from the BIOS config */
  3087. err = via_parse_auto_config(codec);
  3088. if (err < 0) {
  3089. via_free(codec);
  3090. return err;
  3091. }
  3092. spec->init_verbs[spec->num_iverbs++] = vt1812_init_verbs;
  3093. codec->patch_ops = via_patch_ops;
  3094. spec->set_widgets_power_state = set_widgets_power_state_vt1812;
  3095. return 0;
  3096. }
  3097. /*
  3098. * patch entries
  3099. */
  3100. static const struct hda_codec_preset snd_hda_preset_via[] = {
  3101. { .id = 0x11061708, .name = "VT1708", .patch = patch_vt1708},
  3102. { .id = 0x11061709, .name = "VT1708", .patch = patch_vt1708},
  3103. { .id = 0x1106170a, .name = "VT1708", .patch = patch_vt1708},
  3104. { .id = 0x1106170b, .name = "VT1708", .patch = patch_vt1708},
  3105. { .id = 0x1106e710, .name = "VT1709 10-Ch",
  3106. .patch = patch_vt1709},
  3107. { .id = 0x1106e711, .name = "VT1709 10-Ch",
  3108. .patch = patch_vt1709},
  3109. { .id = 0x1106e712, .name = "VT1709 10-Ch",
  3110. .patch = patch_vt1709},
  3111. { .id = 0x1106e713, .name = "VT1709 10-Ch",
  3112. .patch = patch_vt1709},
  3113. { .id = 0x1106e714, .name = "VT1709 6-Ch",
  3114. .patch = patch_vt1709},
  3115. { .id = 0x1106e715, .name = "VT1709 6-Ch",
  3116. .patch = patch_vt1709},
  3117. { .id = 0x1106e716, .name = "VT1709 6-Ch",
  3118. .patch = patch_vt1709},
  3119. { .id = 0x1106e717, .name = "VT1709 6-Ch",
  3120. .patch = patch_vt1709},
  3121. { .id = 0x1106e720, .name = "VT1708B 8-Ch",
  3122. .patch = patch_vt1708B},
  3123. { .id = 0x1106e721, .name = "VT1708B 8-Ch",
  3124. .patch = patch_vt1708B},
  3125. { .id = 0x1106e722, .name = "VT1708B 8-Ch",
  3126. .patch = patch_vt1708B},
  3127. { .id = 0x1106e723, .name = "VT1708B 8-Ch",
  3128. .patch = patch_vt1708B},
  3129. { .id = 0x1106e724, .name = "VT1708B 4-Ch",
  3130. .patch = patch_vt1708B},
  3131. { .id = 0x1106e725, .name = "VT1708B 4-Ch",
  3132. .patch = patch_vt1708B},
  3133. { .id = 0x1106e726, .name = "VT1708B 4-Ch",
  3134. .patch = patch_vt1708B},
  3135. { .id = 0x1106e727, .name = "VT1708B 4-Ch",
  3136. .patch = patch_vt1708B},
  3137. { .id = 0x11060397, .name = "VT1708S",
  3138. .patch = patch_vt1708S},
  3139. { .id = 0x11061397, .name = "VT1708S",
  3140. .patch = patch_vt1708S},
  3141. { .id = 0x11062397, .name = "VT1708S",
  3142. .patch = patch_vt1708S},
  3143. { .id = 0x11063397, .name = "VT1708S",
  3144. .patch = patch_vt1708S},
  3145. { .id = 0x11064397, .name = "VT1705",
  3146. .patch = patch_vt1708S},
  3147. { .id = 0x11065397, .name = "VT1708S",
  3148. .patch = patch_vt1708S},
  3149. { .id = 0x11066397, .name = "VT1708S",
  3150. .patch = patch_vt1708S},
  3151. { .id = 0x11067397, .name = "VT1708S",
  3152. .patch = patch_vt1708S},
  3153. { .id = 0x11060398, .name = "VT1702",
  3154. .patch = patch_vt1702},
  3155. { .id = 0x11061398, .name = "VT1702",
  3156. .patch = patch_vt1702},
  3157. { .id = 0x11062398, .name = "VT1702",
  3158. .patch = patch_vt1702},
  3159. { .id = 0x11063398, .name = "VT1702",
  3160. .patch = patch_vt1702},
  3161. { .id = 0x11064398, .name = "VT1702",
  3162. .patch = patch_vt1702},
  3163. { .id = 0x11065398, .name = "VT1702",
  3164. .patch = patch_vt1702},
  3165. { .id = 0x11066398, .name = "VT1702",
  3166. .patch = patch_vt1702},
  3167. { .id = 0x11067398, .name = "VT1702",
  3168. .patch = patch_vt1702},
  3169. { .id = 0x11060428, .name = "VT1718S",
  3170. .patch = patch_vt1718S},
  3171. { .id = 0x11064428, .name = "VT1718S",
  3172. .patch = patch_vt1718S},
  3173. { .id = 0x11060441, .name = "VT2020",
  3174. .patch = patch_vt1718S},
  3175. { .id = 0x11064441, .name = "VT1828S",
  3176. .patch = patch_vt1718S},
  3177. { .id = 0x11060433, .name = "VT1716S",
  3178. .patch = patch_vt1716S},
  3179. { .id = 0x1106a721, .name = "VT1716S",
  3180. .patch = patch_vt1716S},
  3181. { .id = 0x11060438, .name = "VT2002P", .patch = patch_vt2002P},
  3182. { .id = 0x11064438, .name = "VT2002P", .patch = patch_vt2002P},
  3183. { .id = 0x11060448, .name = "VT1812", .patch = patch_vt1812},
  3184. { .id = 0x11060440, .name = "VT1818S",
  3185. .patch = patch_vt1708S},
  3186. { .id = 0x11060446, .name = "VT1802",
  3187. .patch = patch_vt2002P},
  3188. { .id = 0x11068446, .name = "VT1802",
  3189. .patch = patch_vt2002P},
  3190. {} /* terminator */
  3191. };
  3192. MODULE_ALIAS("snd-hda-codec-id:1106*");
  3193. static struct hda_codec_preset_list via_list = {
  3194. .preset = snd_hda_preset_via,
  3195. .owner = THIS_MODULE,
  3196. };
  3197. MODULE_LICENSE("GPL");
  3198. MODULE_DESCRIPTION("VIA HD-audio codec");
  3199. static int __init patch_via_init(void)
  3200. {
  3201. return snd_hda_add_codec_preset(&via_list);
  3202. }
  3203. static void __exit patch_via_exit(void)
  3204. {
  3205. snd_hda_delete_codec_preset(&via_list);
  3206. }
  3207. module_init(patch_via_init)
  3208. module_exit(patch_via_exit)