patch_via.c 104 KB

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