patch_via.c 95 KB

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