patch_via.c 104 KB

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