patch_via.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. /*
  2. * Universal Interface for Intel High Definition Audio Codec
  3. *
  4. * HD audio interface patch for VIA VT1708 codec
  5. *
  6. * Copyright (c) 2006 Lydia Wang <lydiawang@viatech.com>
  7. * 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. /* */
  30. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  31. #include <sound/driver.h>
  32. #include <linux/init.h>
  33. #include <linux/delay.h>
  34. #include <linux/slab.h>
  35. #include <sound/core.h>
  36. #include "hda_codec.h"
  37. #include "hda_local.h"
  38. /* amp values */
  39. #define AMP_VAL_IDX_SHIFT 19
  40. #define AMP_VAL_IDX_MASK (0x0f<<19)
  41. #define NUM_CONTROL_ALLOC 32
  42. #define NUM_VERB_ALLOC 32
  43. /* Pin Widget NID */
  44. #define VT1708_HP_NID 0x13
  45. #define VT1708_DIGOUT_NID 0x14
  46. #define VT1708_DIGIN_NID 0x16
  47. #define VT1709_HP_DAC_NID 0x28
  48. #define VT1709_DIGOUT_NID 0x13
  49. #define VT1709_DIGIN_NID 0x17
  50. #define IS_VT1708_VENDORID(x) ((x) >= 0x11061708 && (x) <= 0x1106170b)
  51. #define IS_VT1709_10CH_VENDORID(x) ((x) >= 0x1106e710 && (x) <= 0x1106e713)
  52. #define IS_VT1709_6CH_VENDORID(x) ((x) >= 0x1106e714 && (x) <= 0x1106e717)
  53. enum {
  54. VIA_CTL_WIDGET_VOL,
  55. VIA_CTL_WIDGET_MUTE,
  56. };
  57. enum {
  58. AUTO_SEQ_FRONT,
  59. AUTO_SEQ_SURROUND,
  60. AUTO_SEQ_CENLFE,
  61. AUTO_SEQ_SIDE
  62. };
  63. static struct snd_kcontrol_new vt1708_control_templates[] = {
  64. HDA_CODEC_VOLUME(NULL, 0, 0, 0),
  65. HDA_CODEC_MUTE(NULL, 0, 0, 0),
  66. };
  67. struct via_spec {
  68. /* codec parameterization */
  69. struct snd_kcontrol_new *mixers[3];
  70. unsigned int num_mixers;
  71. struct hda_verb *init_verbs;
  72. char *stream_name_analog;
  73. struct hda_pcm_stream *stream_analog_playback;
  74. struct hda_pcm_stream *stream_analog_capture;
  75. char *stream_name_digital;
  76. struct hda_pcm_stream *stream_digital_playback;
  77. struct hda_pcm_stream *stream_digital_capture;
  78. /* playback */
  79. struct hda_multi_out multiout;
  80. /* capture */
  81. unsigned int num_adc_nids;
  82. hda_nid_t *adc_nids;
  83. hda_nid_t dig_in_nid;
  84. /* capture source */
  85. const struct hda_input_mux *input_mux;
  86. unsigned int cur_mux[3];
  87. /* PCM information */
  88. struct hda_pcm pcm_rec[2];
  89. /* dynamic controls, init_verbs and input_mux */
  90. struct auto_pin_cfg autocfg;
  91. unsigned int num_kctl_alloc, num_kctl_used;
  92. struct snd_kcontrol_new *kctl_alloc;
  93. struct hda_input_mux private_imux;
  94. hda_nid_t private_dac_nids[4];
  95. };
  96. static hda_nid_t vt1708_adc_nids[2] = {
  97. /* ADC1-2 */
  98. 0x15, 0x27
  99. };
  100. static hda_nid_t vt1709_adc_nids[3] = {
  101. /* ADC1-2 */
  102. 0x14, 0x15, 0x16
  103. };
  104. /* add dynamic controls */
  105. static int via_add_control(struct via_spec *spec, int type, const char *name,
  106. unsigned long val)
  107. {
  108. struct snd_kcontrol_new *knew;
  109. if (spec->num_kctl_used >= spec->num_kctl_alloc) {
  110. int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
  111. /* array + terminator */
  112. knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL);
  113. if (!knew)
  114. return -ENOMEM;
  115. if (spec->kctl_alloc) {
  116. memcpy(knew, spec->kctl_alloc,
  117. sizeof(*knew) * spec->num_kctl_alloc);
  118. kfree(spec->kctl_alloc);
  119. }
  120. spec->kctl_alloc = knew;
  121. spec->num_kctl_alloc = num;
  122. }
  123. knew = &spec->kctl_alloc[spec->num_kctl_used];
  124. *knew = vt1708_control_templates[type];
  125. knew->name = kstrdup(name, GFP_KERNEL);
  126. if (!knew->name)
  127. return -ENOMEM;
  128. knew->private_value = val;
  129. spec->num_kctl_used++;
  130. return 0;
  131. }
  132. /* create input playback/capture controls for the given pin */
  133. static int via_new_analog_input(struct via_spec *spec, hda_nid_t pin,
  134. const char *ctlname, int idx, int mix_nid)
  135. {
  136. char name[32];
  137. int err;
  138. sprintf(name, "%s Playback Volume", ctlname);
  139. err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
  140. HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
  141. if (err < 0)
  142. return err;
  143. sprintf(name, "%s Playback Switch", ctlname);
  144. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
  145. HDA_COMPOSE_AMP_VAL(mix_nid, 3, idx, HDA_INPUT));
  146. if (err < 0)
  147. return err;
  148. return 0;
  149. }
  150. static void via_auto_set_output_and_unmute(struct hda_codec *codec,
  151. hda_nid_t nid, int pin_type,
  152. int dac_idx)
  153. {
  154. /* set as output */
  155. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL,
  156. pin_type);
  157. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  158. AMP_OUT_UNMUTE);
  159. }
  160. static void via_auto_init_multi_out(struct hda_codec *codec)
  161. {
  162. struct via_spec *spec = codec->spec;
  163. int i;
  164. for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
  165. hda_nid_t nid = spec->autocfg.line_out_pins[i];
  166. if (nid)
  167. via_auto_set_output_and_unmute(codec, nid, PIN_OUT, i);
  168. }
  169. }
  170. static void via_auto_init_hp_out(struct hda_codec *codec)
  171. {
  172. struct via_spec *spec = codec->spec;
  173. hda_nid_t pin;
  174. pin = spec->autocfg.hp_pins[0];
  175. if (pin) /* connect to front */
  176. via_auto_set_output_and_unmute(codec, pin, PIN_HP, 0);
  177. }
  178. static void via_auto_init_analog_input(struct hda_codec *codec)
  179. {
  180. struct via_spec *spec = codec->spec;
  181. int i;
  182. for (i = 0; i < AUTO_PIN_LAST; i++) {
  183. hda_nid_t nid = spec->autocfg.input_pins[i];
  184. snd_hda_codec_write(codec, nid, 0,
  185. AC_VERB_SET_PIN_WIDGET_CONTROL,
  186. (i <= AUTO_PIN_FRONT_MIC ?
  187. PIN_VREF50 : PIN_IN));
  188. }
  189. }
  190. /*
  191. * input MUX handling
  192. */
  193. static int via_mux_enum_info(struct snd_kcontrol *kcontrol,
  194. struct snd_ctl_elem_info *uinfo)
  195. {
  196. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  197. struct via_spec *spec = codec->spec;
  198. return snd_hda_input_mux_info(spec->input_mux, uinfo);
  199. }
  200. static int via_mux_enum_get(struct snd_kcontrol *kcontrol,
  201. struct snd_ctl_elem_value *ucontrol)
  202. {
  203. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  204. struct via_spec *spec = codec->spec;
  205. unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  206. ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
  207. return 0;
  208. }
  209. static int via_mux_enum_put(struct snd_kcontrol *kcontrol,
  210. struct snd_ctl_elem_value *ucontrol)
  211. {
  212. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  213. struct via_spec *spec = codec->spec;
  214. unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  215. unsigned int vendor_id = codec->vendor_id;
  216. /* AIW0 lydia 060801 add for correct sw0 input select */
  217. if (IS_VT1708_VENDORID(vendor_id) && (adc_idx == 0))
  218. return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
  219. 0x18, &spec->cur_mux[adc_idx]);
  220. else if ((IS_VT1709_10CH_VENDORID(vendor_id) ||
  221. IS_VT1709_6CH_VENDORID(vendor_id)) && (adc_idx == 0) )
  222. return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
  223. 0x19, &spec->cur_mux[adc_idx]);
  224. else
  225. return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
  226. spec->adc_nids[adc_idx],
  227. &spec->cur_mux[adc_idx]);
  228. }
  229. /* capture mixer elements */
  230. static struct snd_kcontrol_new vt1708_capture_mixer[] = {
  231. HDA_CODEC_VOLUME("Capture Volume", 0x15, 0x0, HDA_INPUT),
  232. HDA_CODEC_MUTE("Capture Switch", 0x15, 0x0, HDA_INPUT),
  233. HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x27, 0x0, HDA_INPUT),
  234. HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x27, 0x0, HDA_INPUT),
  235. {
  236. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  237. /* The multiple "Capture Source" controls confuse alsamixer
  238. * So call somewhat different..
  239. * FIXME: the controls appear in the "playback" view!
  240. */
  241. /* .name = "Capture Source", */
  242. .name = "Input Source",
  243. .count = 1,
  244. .info = via_mux_enum_info,
  245. .get = via_mux_enum_get,
  246. .put = via_mux_enum_put,
  247. },
  248. { } /* end */
  249. };
  250. /*
  251. * generic initialization of ADC, input mixers and output mixers
  252. */
  253. static struct hda_verb vt1708_volume_init_verbs[] = {
  254. /*
  255. * Unmute ADC0-1 and set the default input to mic-in
  256. */
  257. {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
  258. {0x27, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
  259. /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
  260. * mixer widget
  261. */
  262. /* Amp Indices: CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
  263. {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
  264. {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
  265. {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
  266. {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
  267. {0x17, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
  268. /*
  269. * Set up output mixers (0x19 - 0x1b)
  270. */
  271. /* set vol=0 to output mixers */
  272. {0x19, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
  273. {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
  274. {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
  275. /* Setup default input to PW4 */
  276. {0x20, AC_VERB_SET_CONNECT_SEL, 0x1},
  277. /* Set mic as default input of sw0 */
  278. {0x18, AC_VERB_SET_CONNECT_SEL, 0x2},
  279. /* PW9 Output enable */
  280. {0x25, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
  281. };
  282. static int via_playback_pcm_open(struct hda_pcm_stream *hinfo,
  283. struct hda_codec *codec,
  284. struct snd_pcm_substream *substream)
  285. {
  286. struct via_spec *spec = codec->spec;
  287. return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
  288. }
  289. static int via_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  290. struct hda_codec *codec,
  291. unsigned int stream_tag,
  292. unsigned int format,
  293. struct snd_pcm_substream *substream)
  294. {
  295. struct via_spec *spec = codec->spec;
  296. return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
  297. stream_tag, format, substream);
  298. }
  299. static int via_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  300. struct hda_codec *codec,
  301. struct snd_pcm_substream *substream)
  302. {
  303. struct via_spec *spec = codec->spec;
  304. return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  305. }
  306. /*
  307. * Digital out
  308. */
  309. static int via_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  310. struct hda_codec *codec,
  311. struct snd_pcm_substream *substream)
  312. {
  313. struct via_spec *spec = codec->spec;
  314. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  315. }
  316. static int via_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  317. struct hda_codec *codec,
  318. struct snd_pcm_substream *substream)
  319. {
  320. struct via_spec *spec = codec->spec;
  321. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  322. }
  323. static int via_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  324. struct hda_codec *codec,
  325. unsigned int stream_tag,
  326. unsigned int format,
  327. struct snd_pcm_substream *substream)
  328. {
  329. struct via_spec *spec = codec->spec;
  330. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout,
  331. stream_tag, format, substream);
  332. }
  333. /*
  334. * Analog capture
  335. */
  336. static int via_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  337. struct hda_codec *codec,
  338. unsigned int stream_tag,
  339. unsigned int format,
  340. struct snd_pcm_substream *substream)
  341. {
  342. struct via_spec *spec = codec->spec;
  343. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
  344. stream_tag, 0, format);
  345. return 0;
  346. }
  347. static int via_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  348. struct hda_codec *codec,
  349. struct snd_pcm_substream *substream)
  350. {
  351. struct via_spec *spec = codec->spec;
  352. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
  353. 0, 0, 0);
  354. return 0;
  355. }
  356. static struct hda_pcm_stream vt1708_pcm_analog_playback = {
  357. .substreams = 1,
  358. .channels_min = 2,
  359. .channels_max = 8,
  360. .nid = 0x10, /* NID to query formats and rates */
  361. .ops = {
  362. .open = via_playback_pcm_open,
  363. .prepare = via_playback_pcm_prepare,
  364. .cleanup = via_playback_pcm_cleanup
  365. },
  366. };
  367. static struct hda_pcm_stream vt1708_pcm_analog_capture = {
  368. .substreams = 2,
  369. .channels_min = 2,
  370. .channels_max = 2,
  371. .nid = 0x15, /* NID to query formats and rates */
  372. .ops = {
  373. .prepare = via_capture_pcm_prepare,
  374. .cleanup = via_capture_pcm_cleanup
  375. },
  376. };
  377. static struct hda_pcm_stream vt1708_pcm_digital_playback = {
  378. .substreams = 1,
  379. .channels_min = 2,
  380. .channels_max = 2,
  381. /* NID is set in via_build_pcms */
  382. .ops = {
  383. .open = via_dig_playback_pcm_open,
  384. .close = via_dig_playback_pcm_close,
  385. .prepare = via_dig_playback_pcm_prepare
  386. },
  387. };
  388. static struct hda_pcm_stream vt1708_pcm_digital_capture = {
  389. .substreams = 1,
  390. .channels_min = 2,
  391. .channels_max = 2,
  392. };
  393. static int via_build_controls(struct hda_codec *codec)
  394. {
  395. struct via_spec *spec = codec->spec;
  396. int err;
  397. int i;
  398. for (i = 0; i < spec->num_mixers; i++) {
  399. err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
  400. if (err < 0)
  401. return err;
  402. }
  403. if (spec->multiout.dig_out_nid) {
  404. err = snd_hda_create_spdif_out_ctls(codec,
  405. spec->multiout.dig_out_nid);
  406. if (err < 0)
  407. return err;
  408. }
  409. if (spec->dig_in_nid) {
  410. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
  411. if (err < 0)
  412. return err;
  413. }
  414. return 0;
  415. }
  416. static int via_build_pcms(struct hda_codec *codec)
  417. {
  418. struct via_spec *spec = codec->spec;
  419. struct hda_pcm *info = spec->pcm_rec;
  420. codec->num_pcms = 1;
  421. codec->pcm_info = info;
  422. info->name = spec->stream_name_analog;
  423. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = *(spec->stream_analog_playback);
  424. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dac_nids[0];
  425. info->stream[SNDRV_PCM_STREAM_CAPTURE] = *(spec->stream_analog_capture);
  426. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adc_nids[0];
  427. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  428. spec->multiout.max_channels;
  429. if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
  430. codec->num_pcms++;
  431. info++;
  432. info->name = spec->stream_name_digital;
  433. if (spec->multiout.dig_out_nid) {
  434. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  435. *(spec->stream_digital_playback);
  436. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid =
  437. spec->multiout.dig_out_nid;
  438. }
  439. if (spec->dig_in_nid) {
  440. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  441. *(spec->stream_digital_capture);
  442. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid =
  443. spec->dig_in_nid;
  444. }
  445. }
  446. return 0;
  447. }
  448. static void via_free(struct hda_codec *codec)
  449. {
  450. struct via_spec *spec = codec->spec;
  451. unsigned int i;
  452. if (!spec)
  453. return;
  454. if (spec->kctl_alloc) {
  455. for (i = 0; i < spec->num_kctl_used; i++)
  456. kfree(spec->kctl_alloc[i].name);
  457. kfree(spec->kctl_alloc);
  458. }
  459. kfree(codec->spec);
  460. }
  461. static int via_init(struct hda_codec *codec)
  462. {
  463. struct via_spec *spec = codec->spec;
  464. snd_hda_sequence_write(codec, spec->init_verbs);
  465. return 0;
  466. }
  467. #ifdef CONFIG_PM
  468. /*
  469. * resume
  470. */
  471. static int via_resume(struct hda_codec *codec)
  472. {
  473. struct via_spec *spec = codec->spec;
  474. int i;
  475. via_init(codec);
  476. for (i = 0; i < spec->num_mixers; i++)
  477. snd_hda_resume_ctls(codec, spec->mixers[i]);
  478. if (spec->multiout.dig_out_nid)
  479. snd_hda_resume_spdif_out(codec);
  480. if (spec->dig_in_nid)
  481. snd_hda_resume_spdif_in(codec);
  482. return 0;
  483. }
  484. #endif
  485. /*
  486. */
  487. static struct hda_codec_ops via_patch_ops = {
  488. .build_controls = via_build_controls,
  489. .build_pcms = via_build_pcms,
  490. .init = via_init,
  491. .free = via_free,
  492. #ifdef CONFIG_PM
  493. .resume = via_resume,
  494. #endif
  495. };
  496. /* fill in the dac_nids table from the parsed pin configuration */
  497. static int vt1708_auto_fill_dac_nids(struct via_spec *spec,
  498. const struct auto_pin_cfg *cfg)
  499. {
  500. int i;
  501. hda_nid_t nid;
  502. spec->multiout.num_dacs = cfg->line_outs;
  503. spec->multiout.dac_nids = spec->private_dac_nids;
  504. for(i = 0; i < 4; i++) {
  505. nid = cfg->line_out_pins[i];
  506. if (nid) {
  507. /* config dac list */
  508. switch (i) {
  509. case AUTO_SEQ_FRONT:
  510. spec->multiout.dac_nids[i] = 0x10;
  511. break;
  512. case AUTO_SEQ_CENLFE:
  513. spec->multiout.dac_nids[i] = 0x12;
  514. break;
  515. case AUTO_SEQ_SURROUND:
  516. spec->multiout.dac_nids[i] = 0x13;
  517. break;
  518. case AUTO_SEQ_SIDE:
  519. spec->multiout.dac_nids[i] = 0x11;
  520. break;
  521. }
  522. }
  523. }
  524. return 0;
  525. }
  526. /* add playback controls from the parsed DAC table */
  527. static int vt1708_auto_create_multi_out_ctls(struct via_spec *spec,
  528. const struct auto_pin_cfg *cfg)
  529. {
  530. char name[32];
  531. static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
  532. hda_nid_t nid, nid_vol = 0;
  533. int i, err;
  534. for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
  535. nid = cfg->line_out_pins[i];
  536. if (!nid)
  537. continue;
  538. if (i != AUTO_SEQ_FRONT)
  539. nid_vol = 0x1b - i + 1;
  540. if (i == AUTO_SEQ_CENLFE) {
  541. /* Center/LFE */
  542. err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
  543. "Center Playback Volume",
  544. HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0, HDA_OUTPUT));
  545. if (err < 0)
  546. return err;
  547. err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
  548. "LFE Playback Volume",
  549. HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0, HDA_OUTPUT));
  550. if (err < 0)
  551. return err;
  552. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
  553. "Center Playback Switch",
  554. HDA_COMPOSE_AMP_VAL(nid_vol, 1, 0, HDA_OUTPUT));
  555. if (err < 0)
  556. return err;
  557. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
  558. "LFE Playback Switch",
  559. HDA_COMPOSE_AMP_VAL(nid_vol, 2, 0, HDA_OUTPUT));
  560. if (err < 0)
  561. return err;
  562. } else if (i == AUTO_SEQ_FRONT){
  563. /* add control to mixer index 0 */
  564. err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
  565. "Master Front Playback Volume",
  566. HDA_COMPOSE_AMP_VAL(0x17, 3, 0, HDA_INPUT));
  567. if (err < 0)
  568. return err;
  569. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
  570. "Master Front Playback Switch",
  571. HDA_COMPOSE_AMP_VAL(0x17, 3, 0, HDA_INPUT));
  572. if (err < 0)
  573. return err;
  574. /* add control to PW3 */
  575. sprintf(name, "%s Playback Volume", chname[i]);
  576. err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
  577. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT));
  578. if (err < 0)
  579. return err;
  580. sprintf(name, "%s Playback Switch", chname[i]);
  581. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
  582. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT));
  583. if (err < 0)
  584. return err;
  585. } else {
  586. sprintf(name, "%s Playback Volume", chname[i]);
  587. err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
  588. HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
  589. if (err < 0)
  590. return err;
  591. sprintf(name, "%s Playback Switch", chname[i]);
  592. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
  593. HDA_COMPOSE_AMP_VAL(nid_vol, 3, 0, HDA_OUTPUT));
  594. if (err < 0)
  595. return err;
  596. }
  597. }
  598. return 0;
  599. }
  600. static int vt1708_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
  601. {
  602. int err;
  603. if (!pin)
  604. return 0;
  605. spec->multiout.hp_nid = VT1708_HP_NID; /* AOW3 */
  606. err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
  607. "Headphone Playback Volume",
  608. HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
  609. if (err < 0)
  610. return err;
  611. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
  612. "Headphone Playback Switch",
  613. HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
  614. if (err < 0)
  615. return err;
  616. return 0;
  617. }
  618. /* create playback/capture controls for input pins */
  619. static int vt1708_auto_create_analog_input_ctls(struct via_spec *spec,
  620. const struct auto_pin_cfg *cfg)
  621. {
  622. static char *labels[] = {
  623. "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
  624. };
  625. struct hda_input_mux *imux = &spec->private_imux;
  626. int i, err, idx = 0;
  627. /* for internal loopback recording select */
  628. imux->items[imux->num_items].label = "Stereo Mixer";
  629. imux->items[imux->num_items].index = idx;
  630. imux->num_items++;
  631. for (i = 0; i < AUTO_PIN_LAST; i++) {
  632. if (!cfg->input_pins[i])
  633. continue;
  634. switch (cfg->input_pins[i]) {
  635. case 0x1d: /* Mic */
  636. idx = 2;
  637. break;
  638. case 0x1e: /* Line In */
  639. idx = 3;
  640. break;
  641. case 0x21: /* Front Mic */
  642. idx = 4;
  643. break;
  644. case 0x24: /* CD */
  645. idx = 1;
  646. break;
  647. }
  648. err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
  649. idx, 0x17);
  650. if (err < 0)
  651. return err;
  652. imux->items[imux->num_items].label = labels[i];
  653. imux->items[imux->num_items].index = idx;
  654. imux->num_items++;
  655. }
  656. return 0;
  657. }
  658. static int vt1708_parse_auto_config(struct hda_codec *codec)
  659. {
  660. struct via_spec *spec = codec->spec;
  661. int err;
  662. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  663. if (err < 0)
  664. return err;
  665. err = vt1708_auto_fill_dac_nids(spec, &spec->autocfg);
  666. if (err < 0)
  667. return err;
  668. if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
  669. return 0; /* can't find valid BIOS pin config */
  670. err = vt1708_auto_create_multi_out_ctls(spec, &spec->autocfg);
  671. if (err < 0)
  672. return err;
  673. err = vt1708_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
  674. if (err < 0)
  675. return err;
  676. err = vt1708_auto_create_analog_input_ctls(spec, &spec->autocfg);
  677. if (err < 0)
  678. return err;
  679. spec->multiout.max_channels = spec->multiout.num_dacs * 2;
  680. if (spec->autocfg.dig_out_pin)
  681. spec->multiout.dig_out_nid = VT1708_DIGOUT_NID;
  682. if (spec->autocfg.dig_in_pin)
  683. spec->dig_in_nid = VT1708_DIGIN_NID;
  684. if (spec->kctl_alloc)
  685. spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
  686. spec->init_verbs = vt1708_volume_init_verbs;
  687. spec->input_mux = &spec->private_imux;
  688. return 1;
  689. }
  690. /* init callback for auto-configuration model -- overriding the default init */
  691. static int via_auto_init(struct hda_codec *codec)
  692. {
  693. via_init(codec);
  694. via_auto_init_multi_out(codec);
  695. via_auto_init_hp_out(codec);
  696. via_auto_init_analog_input(codec);
  697. return 0;
  698. }
  699. static int patch_vt1708(struct hda_codec *codec)
  700. {
  701. struct via_spec *spec;
  702. int err;
  703. /* create a codec specific record */
  704. spec = kcalloc(1, sizeof(*spec), GFP_KERNEL);
  705. if (spec == NULL)
  706. return -ENOMEM;
  707. codec->spec = spec;
  708. /* automatic parse from the BIOS config */
  709. err = vt1708_parse_auto_config(codec);
  710. if (err < 0) {
  711. via_free(codec);
  712. return err;
  713. } else if (!err) {
  714. printk(KERN_INFO "hda_codec: Cannot set up configuration "
  715. "from BIOS. Using genenic mode...\n");
  716. }
  717. spec->stream_name_analog = "VT1708 Analog";
  718. spec->stream_analog_playback = &vt1708_pcm_analog_playback;
  719. spec->stream_analog_capture = &vt1708_pcm_analog_capture;
  720. spec->stream_name_digital = "VT1708 Digital";
  721. spec->stream_digital_playback = &vt1708_pcm_digital_playback;
  722. spec->stream_digital_capture = &vt1708_pcm_digital_capture;
  723. if (!spec->adc_nids && spec->input_mux) {
  724. spec->adc_nids = vt1708_adc_nids;
  725. spec->num_adc_nids = ARRAY_SIZE(vt1708_adc_nids);
  726. spec->mixers[spec->num_mixers] = vt1708_capture_mixer;
  727. spec->num_mixers++;
  728. }
  729. codec->patch_ops = via_patch_ops;
  730. codec->patch_ops.init = via_auto_init;
  731. return 0;
  732. }
  733. /* capture mixer elements */
  734. static struct snd_kcontrol_new vt1709_capture_mixer[] = {
  735. HDA_CODEC_VOLUME("Capture Volume", 0x14, 0x0, HDA_INPUT),
  736. HDA_CODEC_MUTE("Capture Switch", 0x14, 0x0, HDA_INPUT),
  737. HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x15, 0x0, HDA_INPUT),
  738. HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x15, 0x0, HDA_INPUT),
  739. HDA_CODEC_VOLUME_IDX("Capture Volume", 2, 0x16, 0x0, HDA_INPUT),
  740. HDA_CODEC_MUTE_IDX("Capture Switch", 2, 0x16, 0x0, HDA_INPUT),
  741. {
  742. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  743. /* The multiple "Capture Source" controls confuse alsamixer
  744. * So call somewhat different..
  745. * FIXME: the controls appear in the "playback" view!
  746. */
  747. /* .name = "Capture Source", */
  748. .name = "Input Source",
  749. .count = 1,
  750. .info = via_mux_enum_info,
  751. .get = via_mux_enum_get,
  752. .put = via_mux_enum_put,
  753. },
  754. { } /* end */
  755. };
  756. /*
  757. * generic initialization of ADC, input mixers and output mixers
  758. */
  759. static struct hda_verb vt1709_10ch_volume_init_verbs[] = {
  760. /*
  761. * Unmute ADC0-2 and set the default input to mic-in
  762. */
  763. {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
  764. {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
  765. {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
  766. /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
  767. * mixer widget
  768. */
  769. /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
  770. {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
  771. {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
  772. {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
  773. {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
  774. {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
  775. /*
  776. * Set up output selector (0x1a, 0x1b, 0x29)
  777. */
  778. /* set vol=0 to output mixers */
  779. {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
  780. {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
  781. {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
  782. /*
  783. * Unmute PW3 and PW4
  784. */
  785. {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
  786. {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
  787. /* Set input of PW4 as AOW4 */
  788. {0x20, AC_VERB_SET_CONNECT_SEL, 0x1},
  789. /* Set mic as default input of sw0 */
  790. {0x19, AC_VERB_SET_CONNECT_SEL, 0x2},
  791. /* PW9 Output enable */
  792. {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
  793. { }
  794. };
  795. static struct hda_pcm_stream vt1709_10ch_pcm_analog_playback = {
  796. .substreams = 1,
  797. .channels_min = 2,
  798. .channels_max = 10,
  799. .nid = 0x10, /* NID to query formats and rates */
  800. .ops = {
  801. .open = via_playback_pcm_open,
  802. .prepare = via_playback_pcm_prepare,
  803. .cleanup = via_playback_pcm_cleanup
  804. },
  805. };
  806. static struct hda_pcm_stream vt1709_6ch_pcm_analog_playback = {
  807. .substreams = 1,
  808. .channels_min = 2,
  809. .channels_max = 6,
  810. .nid = 0x10, /* NID to query formats and rates */
  811. .ops = {
  812. .open = via_playback_pcm_open,
  813. .prepare = via_playback_pcm_prepare,
  814. .cleanup = via_playback_pcm_cleanup
  815. },
  816. };
  817. static struct hda_pcm_stream vt1709_pcm_analog_capture = {
  818. .substreams = 2,
  819. .channels_min = 2,
  820. .channels_max = 2,
  821. .nid = 0x14, /* NID to query formats and rates */
  822. .ops = {
  823. .prepare = via_capture_pcm_prepare,
  824. .cleanup = via_capture_pcm_cleanup
  825. },
  826. };
  827. static struct hda_pcm_stream vt1709_pcm_digital_playback = {
  828. .substreams = 1,
  829. .channels_min = 2,
  830. .channels_max = 2,
  831. /* NID is set in via_build_pcms */
  832. .ops = {
  833. .open = via_dig_playback_pcm_open,
  834. .close = via_dig_playback_pcm_close
  835. },
  836. };
  837. static struct hda_pcm_stream vt1709_pcm_digital_capture = {
  838. .substreams = 1,
  839. .channels_min = 2,
  840. .channels_max = 2,
  841. };
  842. static int vt1709_auto_fill_dac_nids(struct via_spec *spec,
  843. const struct auto_pin_cfg *cfg)
  844. {
  845. int i;
  846. hda_nid_t nid;
  847. if (cfg->line_outs == 4) /* 10 channels */
  848. spec->multiout.num_dacs = cfg->line_outs+1; /* AOW0~AOW4 */
  849. else if (cfg->line_outs == 3) /* 6 channels */
  850. spec->multiout.num_dacs = cfg->line_outs; /* AOW0~AOW2 */
  851. spec->multiout.dac_nids = spec->private_dac_nids;
  852. if (cfg->line_outs == 4) { /* 10 channels */
  853. for (i = 0; i < cfg->line_outs; i++) {
  854. nid = cfg->line_out_pins[i];
  855. if (nid) {
  856. /* config dac list */
  857. switch (i) {
  858. case AUTO_SEQ_FRONT:
  859. /* AOW0 */
  860. spec->multiout.dac_nids[i] = 0x10;
  861. break;
  862. case AUTO_SEQ_CENLFE:
  863. /* AOW2 */
  864. spec->multiout.dac_nids[i] = 0x12;
  865. break;
  866. case AUTO_SEQ_SURROUND:
  867. /* AOW3 */
  868. spec->multiout.dac_nids[i] = 0x27;
  869. break;
  870. case AUTO_SEQ_SIDE:
  871. /* AOW1 */
  872. spec->multiout.dac_nids[i] = 0x11;
  873. break;
  874. default:
  875. break;
  876. }
  877. }
  878. }
  879. spec->multiout.dac_nids[cfg->line_outs] = 0x28; /* AOW4 */
  880. } else if (cfg->line_outs == 3) { /* 6 channels */
  881. for(i = 0; i < cfg->line_outs; i++) {
  882. nid = cfg->line_out_pins[i];
  883. if (nid) {
  884. /* config dac list */
  885. switch(i) {
  886. case AUTO_SEQ_FRONT:
  887. /* AOW0 */
  888. spec->multiout.dac_nids[i] = 0x10;
  889. break;
  890. case AUTO_SEQ_CENLFE:
  891. /* AOW2 */
  892. spec->multiout.dac_nids[i] = 0x12;
  893. break;
  894. case AUTO_SEQ_SURROUND:
  895. /* AOW1 */
  896. spec->multiout.dac_nids[i] = 0x11;
  897. break;
  898. default:
  899. break;
  900. }
  901. }
  902. }
  903. }
  904. return 0;
  905. }
  906. /* add playback controls from the parsed DAC table */
  907. static int vt1709_auto_create_multi_out_ctls(struct via_spec *spec,
  908. const struct auto_pin_cfg *cfg)
  909. {
  910. char name[32];
  911. static const char *chname[4] = { "Front", "Surround", "C/LFE", "Side" };
  912. hda_nid_t nid = 0;
  913. int i, err;
  914. for (i = 0; i <= AUTO_SEQ_SIDE; i++) {
  915. nid = cfg->line_out_pins[i];
  916. if (!nid)
  917. continue;
  918. if (i == AUTO_SEQ_CENLFE) {
  919. /* Center/LFE */
  920. err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
  921. "Center Playback Volume",
  922. HDA_COMPOSE_AMP_VAL(0x1b, 1, 0, HDA_OUTPUT));
  923. if (err < 0)
  924. return err;
  925. err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
  926. "LFE Playback Volume",
  927. HDA_COMPOSE_AMP_VAL(0x1b, 2, 0, HDA_OUTPUT));
  928. if (err < 0)
  929. return err;
  930. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
  931. "Center Playback Switch",
  932. HDA_COMPOSE_AMP_VAL(0x1b, 1, 0, HDA_OUTPUT));
  933. if (err < 0)
  934. return err;
  935. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
  936. "LFE Playback Switch",
  937. HDA_COMPOSE_AMP_VAL(0x1b, 2, 0, HDA_OUTPUT));
  938. if (err < 0)
  939. return err;
  940. } else if (i == AUTO_SEQ_FRONT){
  941. /* add control to mixer index 0 */
  942. err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
  943. "Master Front Playback Volume",
  944. HDA_COMPOSE_AMP_VAL(0x18, 3, 0, HDA_INPUT));
  945. if (err < 0)
  946. return err;
  947. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
  948. "Master Front Playback Switch",
  949. HDA_COMPOSE_AMP_VAL(0x18, 3, 0, HDA_INPUT));
  950. if (err < 0)
  951. return err;
  952. /* add control to PW3 */
  953. sprintf(name, "%s Playback Volume", chname[i]);
  954. err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
  955. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT));
  956. if (err < 0)
  957. return err;
  958. sprintf(name, "%s Playback Switch", chname[i]);
  959. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
  960. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT));
  961. if (err < 0)
  962. return err;
  963. } else if (i == AUTO_SEQ_SURROUND) {
  964. sprintf(name, "%s Playback Volume", chname[i]);
  965. err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
  966. HDA_COMPOSE_AMP_VAL(0x29, 3, 0, HDA_OUTPUT));
  967. if (err < 0)
  968. return err;
  969. sprintf(name, "%s Playback Switch", chname[i]);
  970. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
  971. HDA_COMPOSE_AMP_VAL(0x29, 3, 0, HDA_OUTPUT));
  972. if (err < 0)
  973. return err;
  974. } else if (i == AUTO_SEQ_SIDE) {
  975. sprintf(name, "%s Playback Volume", chname[i]);
  976. err = via_add_control(spec, VIA_CTL_WIDGET_VOL, name,
  977. HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT));
  978. if (err < 0)
  979. return err;
  980. sprintf(name, "%s Playback Switch", chname[i]);
  981. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE, name,
  982. HDA_COMPOSE_AMP_VAL(0x1a, 3, 0, HDA_OUTPUT));
  983. if (err < 0)
  984. return err;
  985. }
  986. }
  987. return 0;
  988. }
  989. static int vt1709_auto_create_hp_ctls(struct via_spec *spec, hda_nid_t pin)
  990. {
  991. int err;
  992. if (!pin)
  993. return 0;
  994. if (spec->multiout.num_dacs == 5) /* 10 channels */
  995. spec->multiout.hp_nid = VT1709_HP_DAC_NID;
  996. else if (spec->multiout.num_dacs == 3) /* 6 channels */
  997. spec->multiout.hp_nid = 0;
  998. err = via_add_control(spec, VIA_CTL_WIDGET_VOL,
  999. "Headphone Playback Volume",
  1000. HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
  1001. if (err < 0)
  1002. return err;
  1003. err = via_add_control(spec, VIA_CTL_WIDGET_MUTE,
  1004. "Headphone Playback Switch",
  1005. HDA_COMPOSE_AMP_VAL(pin, 3, 0, HDA_OUTPUT));
  1006. if (err < 0)
  1007. return err;
  1008. return 0;
  1009. }
  1010. /* create playback/capture controls for input pins */
  1011. static int vt1709_auto_create_analog_input_ctls(struct via_spec *spec,
  1012. const struct auto_pin_cfg *cfg)
  1013. {
  1014. static char *labels[] = {
  1015. "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux", NULL
  1016. };
  1017. struct hda_input_mux *imux = &spec->private_imux;
  1018. int i, err, idx = 0;
  1019. /* for internal loopback recording select */
  1020. imux->items[imux->num_items].label = "Stereo Mixer";
  1021. imux->items[imux->num_items].index = idx;
  1022. imux->num_items++;
  1023. for (i = 0; i < AUTO_PIN_LAST; i++) {
  1024. if (!cfg->input_pins[i])
  1025. continue;
  1026. switch (cfg->input_pins[i]) {
  1027. case 0x1d: /* Mic */
  1028. idx = 2;
  1029. break;
  1030. case 0x1e: /* Line In */
  1031. idx = 3;
  1032. break;
  1033. case 0x21: /* Front Mic */
  1034. idx = 4;
  1035. break;
  1036. case 0x23: /* CD */
  1037. idx = 1;
  1038. break;
  1039. }
  1040. err = via_new_analog_input(spec, cfg->input_pins[i], labels[i],
  1041. idx, 0x18);
  1042. if (err < 0)
  1043. return err;
  1044. imux->items[imux->num_items].label = labels[i];
  1045. imux->items[imux->num_items].index = idx;
  1046. imux->num_items++;
  1047. }
  1048. return 0;
  1049. }
  1050. static int vt1709_parse_auto_config(struct hda_codec *codec)
  1051. {
  1052. struct via_spec *spec = codec->spec;
  1053. int err;
  1054. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  1055. if (err < 0)
  1056. return err;
  1057. err = vt1709_auto_fill_dac_nids(spec, &spec->autocfg);
  1058. if (err < 0)
  1059. return err;
  1060. if (!spec->autocfg.line_outs && !spec->autocfg.hp_pins[0])
  1061. return 0; /* can't find valid BIOS pin config */
  1062. err = vt1709_auto_create_multi_out_ctls(spec, &spec->autocfg);
  1063. if (err < 0)
  1064. return err;
  1065. err = vt1709_auto_create_hp_ctls(spec, spec->autocfg.hp_pins[0]);
  1066. if (err < 0)
  1067. return err;
  1068. err = vt1709_auto_create_analog_input_ctls(spec, &spec->autocfg);
  1069. if (err < 0)
  1070. return err;
  1071. spec->multiout.max_channels = spec->multiout.num_dacs * 2;
  1072. if (spec->autocfg.dig_out_pin)
  1073. spec->multiout.dig_out_nid = VT1709_DIGOUT_NID;
  1074. if (spec->autocfg.dig_in_pin)
  1075. spec->dig_in_nid = VT1709_DIGIN_NID;
  1076. if (spec->kctl_alloc)
  1077. spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
  1078. spec->input_mux = &spec->private_imux;
  1079. return 1;
  1080. }
  1081. static int patch_vt1709_10ch(struct hda_codec *codec)
  1082. {
  1083. struct via_spec *spec;
  1084. int err;
  1085. /* create a codec specific record */
  1086. spec = kcalloc(1, sizeof(*spec), GFP_KERNEL);
  1087. if (spec == NULL)
  1088. return -ENOMEM;
  1089. codec->spec = spec;
  1090. err = vt1709_parse_auto_config(codec);
  1091. if (err < 0) {
  1092. via_free(codec);
  1093. return err;
  1094. } else if (!err) {
  1095. printk(KERN_INFO "hda_codec: Cannot set up configuration. "
  1096. "Using genenic mode...\n");
  1097. }
  1098. spec->init_verbs = vt1709_10ch_volume_init_verbs;
  1099. spec->stream_name_analog = "VT1709 Analog";
  1100. spec->stream_analog_playback = &vt1709_10ch_pcm_analog_playback;
  1101. spec->stream_analog_capture = &vt1709_pcm_analog_capture;
  1102. spec->stream_name_digital = "VT1709 Digital";
  1103. spec->stream_digital_playback = &vt1709_pcm_digital_playback;
  1104. spec->stream_digital_capture = &vt1709_pcm_digital_capture;
  1105. if (!spec->adc_nids && spec->input_mux) {
  1106. spec->adc_nids = vt1709_adc_nids;
  1107. spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
  1108. spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
  1109. spec->num_mixers++;
  1110. }
  1111. codec->patch_ops = via_patch_ops;
  1112. codec->patch_ops.init = via_auto_init;
  1113. return 0;
  1114. }
  1115. /*
  1116. * generic initialization of ADC, input mixers and output mixers
  1117. */
  1118. static struct hda_verb vt1709_6ch_volume_init_verbs[] = {
  1119. /*
  1120. * Unmute ADC0-2 and set the default input to mic-in
  1121. */
  1122. {0x14, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
  1123. {0x15, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
  1124. {0x16, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
  1125. /* Unmute input amps (CD, Line In, Mic 1 & Mic 2) of the analog-loopback
  1126. * mixer widget
  1127. */
  1128. /* Amp Indices: AOW0=0, CD = 1, Mic1 = 2, Line = 3, Mic2 = 4 */
  1129. {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(0)},
  1130. {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(1)},
  1131. {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(2)},
  1132. {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(3)},
  1133. {0x18, AC_VERB_SET_AMP_GAIN_MUTE, AMP_IN_UNMUTE(4)},
  1134. /*
  1135. * Set up output selector (0x1a, 0x1b, 0x29)
  1136. */
  1137. /* set vol=0 to output mixers */
  1138. {0x1a, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
  1139. {0x1b, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
  1140. {0x29, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
  1141. /*
  1142. * Unmute PW3 and PW4
  1143. */
  1144. {0x1f, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
  1145. {0x20, AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO},
  1146. /* Set input of PW4 as MW0 */
  1147. {0x20, AC_VERB_SET_CONNECT_SEL, 0},
  1148. /* Set mic as default input of sw0 */
  1149. {0x19, AC_VERB_SET_CONNECT_SEL, 0x2},
  1150. /* PW9 Output enable */
  1151. {0x24, AC_VERB_SET_PIN_WIDGET_CONTROL, 0x40},
  1152. { }
  1153. };
  1154. static int patch_vt1709_6ch(struct hda_codec *codec)
  1155. {
  1156. struct via_spec *spec;
  1157. int err;
  1158. /* create a codec specific record */
  1159. spec = kcalloc(1, sizeof(*spec), GFP_KERNEL);
  1160. if (spec == NULL)
  1161. return -ENOMEM;
  1162. codec->spec = spec;
  1163. err = vt1709_parse_auto_config(codec);
  1164. if (err < 0) {
  1165. via_free(codec);
  1166. return err;
  1167. } else if (!err) {
  1168. printk(KERN_INFO "hda_codec: Cannot set up configuration. "
  1169. "Using genenic mode...\n");
  1170. }
  1171. spec->init_verbs = vt1709_6ch_volume_init_verbs;
  1172. spec->stream_name_analog = "VT1709 Analog";
  1173. spec->stream_analog_playback = &vt1709_6ch_pcm_analog_playback;
  1174. spec->stream_analog_capture = &vt1709_pcm_analog_capture;
  1175. spec->stream_name_digital = "VT1709 Digital";
  1176. spec->stream_digital_playback = &vt1709_pcm_digital_playback;
  1177. spec->stream_digital_capture = &vt1709_pcm_digital_capture;
  1178. if (!spec->adc_nids && spec->input_mux) {
  1179. spec->adc_nids = vt1709_adc_nids;
  1180. spec->num_adc_nids = ARRAY_SIZE(vt1709_adc_nids);
  1181. spec->mixers[spec->num_mixers] = vt1709_capture_mixer;
  1182. spec->num_mixers++;
  1183. }
  1184. codec->patch_ops = via_patch_ops;
  1185. codec->patch_ops.init = via_auto_init;
  1186. return 0;
  1187. }
  1188. /*
  1189. * patch entries
  1190. */
  1191. struct hda_codec_preset snd_hda_preset_via[] = {
  1192. { .id = 0x11061708, .name = "VIA VT1708", .patch = patch_vt1708},
  1193. { .id = 0x11061709, .name = "VIA VT1708", .patch = patch_vt1708},
  1194. { .id = 0x1106170A, .name = "VIA VT1708", .patch = patch_vt1708},
  1195. { .id = 0x1106170B, .name = "VIA VT1708", .patch = patch_vt1708},
  1196. { .id = 0x1106E710, .name = "VIA VT1709 10-Ch", .patch = patch_vt1709_10ch},
  1197. { .id = 0x1106E711, .name = "VIA VT1709 10-Ch", .patch = patch_vt1709_10ch},
  1198. { .id = 0x1106E712, .name = "VIA VT1709 10-Ch", .patch = patch_vt1709_10ch},
  1199. { .id = 0x1106E713, .name = "VIA VT1709 10-Ch", .patch = patch_vt1709_10ch},
  1200. { .id = 0x1106E714, .name = "VIA VT1709 6-Ch", .patch = patch_vt1709_6ch},
  1201. { .id = 0x1106E715, .name = "VIA VT1709 6-Ch", .patch = patch_vt1709_6ch},
  1202. { .id = 0x1106E716, .name = "VIA VT1709 6-Ch", .patch = patch_vt1709_6ch},
  1203. { .id = 0x1106E717, .name = "VIA VT1709 6-Ch", .patch = patch_vt1709_6ch},
  1204. {} /* terminator */
  1205. };