patch_sigmatel.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. /*
  2. * Universal Interface for Intel High Definition Audio Codec
  3. *
  4. * HD audio interface patch for SigmaTel STAC92xx
  5. *
  6. * Copyright (c) 2005 Embedded Alley Solutions, Inc.
  7. * Matt Porter <mporter@embeddedalley.com>
  8. *
  9. * Based on patch_cmedia.c and patch_realtek.c
  10. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  11. *
  12. * This driver is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This driver is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #include <sound/driver.h>
  27. #include <linux/init.h>
  28. #include <linux/delay.h>
  29. #include <linux/slab.h>
  30. #include <linux/pci.h>
  31. #include <sound/core.h>
  32. #include <sound/asoundef.h>
  33. #include "hda_codec.h"
  34. #include "hda_local.h"
  35. #define NUM_CONTROL_ALLOC 32
  36. #define STAC_HP_EVENT 0x37
  37. #define STAC_UNSOL_ENABLE (AC_USRSP_EN | STAC_HP_EVENT)
  38. #define STAC_REF 0
  39. #define STAC_D945GTP3 1
  40. #define STAC_D945GTP5 2
  41. struct sigmatel_spec {
  42. struct snd_kcontrol_new *mixers[4];
  43. unsigned int num_mixers;
  44. int board_config;
  45. unsigned int surr_switch: 1;
  46. unsigned int line_switch: 1;
  47. unsigned int mic_switch: 1;
  48. /* playback */
  49. struct hda_multi_out multiout;
  50. hda_nid_t dac_nids[4];
  51. /* capture */
  52. hda_nid_t *adc_nids;
  53. unsigned int num_adcs;
  54. hda_nid_t *mux_nids;
  55. unsigned int num_muxes;
  56. hda_nid_t dig_in_nid;
  57. /* pin widgets */
  58. hda_nid_t *pin_nids;
  59. unsigned int num_pins;
  60. unsigned int *pin_configs;
  61. /* codec specific stuff */
  62. struct hda_verb *init;
  63. struct snd_kcontrol_new *mixer;
  64. /* capture source */
  65. struct hda_input_mux *input_mux;
  66. unsigned int cur_mux[2];
  67. /* i/o switches */
  68. unsigned int io_switch[2];
  69. struct hda_pcm pcm_rec[2]; /* PCM information */
  70. /* dynamic controls and input_mux */
  71. struct auto_pin_cfg autocfg;
  72. unsigned int num_kctl_alloc, num_kctl_used;
  73. struct snd_kcontrol_new *kctl_alloc;
  74. struct hda_input_mux private_imux;
  75. };
  76. static hda_nid_t stac9200_adc_nids[1] = {
  77. 0x03,
  78. };
  79. static hda_nid_t stac9200_mux_nids[1] = {
  80. 0x0c,
  81. };
  82. static hda_nid_t stac9200_dac_nids[1] = {
  83. 0x02,
  84. };
  85. static hda_nid_t stac922x_adc_nids[2] = {
  86. 0x06, 0x07,
  87. };
  88. static hda_nid_t stac922x_mux_nids[2] = {
  89. 0x12, 0x13,
  90. };
  91. static hda_nid_t stac9200_pin_nids[8] = {
  92. 0x08, 0x09, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
  93. };
  94. static hda_nid_t stac922x_pin_nids[10] = {
  95. 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
  96. 0x0f, 0x10, 0x11, 0x15, 0x1b,
  97. };
  98. static int stac92xx_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  99. {
  100. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  101. struct sigmatel_spec *spec = codec->spec;
  102. return snd_hda_input_mux_info(spec->input_mux, uinfo);
  103. }
  104. static int stac92xx_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  105. {
  106. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  107. struct sigmatel_spec *spec = codec->spec;
  108. unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  109. ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
  110. return 0;
  111. }
  112. static int stac92xx_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  113. {
  114. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  115. struct sigmatel_spec *spec = codec->spec;
  116. unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  117. return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
  118. spec->mux_nids[adc_idx], &spec->cur_mux[adc_idx]);
  119. }
  120. static struct hda_verb stac9200_core_init[] = {
  121. /* set dac0mux for dac converter */
  122. { 0x07, AC_VERB_SET_CONNECT_SEL, 0x00},
  123. {}
  124. };
  125. static struct hda_verb stac922x_core_init[] = {
  126. /* set master volume and direct control */
  127. { 0x16, AC_VERB_SET_VOLUME_KNOB_CONTROL, 0xff},
  128. {}
  129. };
  130. static struct snd_kcontrol_new stac9200_mixer[] = {
  131. HDA_CODEC_VOLUME("Master Playback Volume", 0xb, 0, HDA_OUTPUT),
  132. HDA_CODEC_MUTE("Master Playback Switch", 0xb, 0, HDA_OUTPUT),
  133. {
  134. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  135. .name = "Input Source",
  136. .count = 1,
  137. .info = stac92xx_mux_enum_info,
  138. .get = stac92xx_mux_enum_get,
  139. .put = stac92xx_mux_enum_put,
  140. },
  141. HDA_CODEC_VOLUME("Capture Volume", 0x0a, 0, HDA_OUTPUT),
  142. HDA_CODEC_MUTE("Capture Switch", 0x0a, 0, HDA_OUTPUT),
  143. HDA_CODEC_VOLUME("Capture Mux Volume", 0x0c, 0, HDA_OUTPUT),
  144. { } /* end */
  145. };
  146. /* This needs to be generated dynamically based on sequence */
  147. static struct snd_kcontrol_new stac922x_mixer[] = {
  148. {
  149. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  150. .name = "Input Source",
  151. .count = 1,
  152. .info = stac92xx_mux_enum_info,
  153. .get = stac92xx_mux_enum_get,
  154. .put = stac92xx_mux_enum_put,
  155. },
  156. HDA_CODEC_VOLUME("Capture Volume", 0x17, 0x0, HDA_INPUT),
  157. HDA_CODEC_MUTE("Capture Switch", 0x17, 0x0, HDA_INPUT),
  158. HDA_CODEC_VOLUME("Mux Capture Volume", 0x12, 0x0, HDA_OUTPUT),
  159. { } /* end */
  160. };
  161. static int stac92xx_build_controls(struct hda_codec *codec)
  162. {
  163. struct sigmatel_spec *spec = codec->spec;
  164. int err;
  165. int i;
  166. err = snd_hda_add_new_ctls(codec, spec->mixer);
  167. if (err < 0)
  168. return err;
  169. for (i = 0; i < spec->num_mixers; i++) {
  170. err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
  171. if (err < 0)
  172. return err;
  173. }
  174. if (spec->multiout.dig_out_nid) {
  175. err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
  176. if (err < 0)
  177. return err;
  178. }
  179. if (spec->dig_in_nid) {
  180. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
  181. if (err < 0)
  182. return err;
  183. }
  184. return 0;
  185. }
  186. static unsigned int ref9200_pin_configs[8] = {
  187. 0x01c47010, 0x01447010, 0x0221401f, 0x01114010,
  188. 0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
  189. };
  190. static unsigned int *stac9200_brd_tbl[] = {
  191. ref9200_pin_configs,
  192. };
  193. static struct hda_board_config stac9200_cfg_tbl[] = {
  194. { .modelname = "ref",
  195. .pci_subvendor = PCI_VENDOR_ID_INTEL,
  196. .pci_subdevice = 0x2668, /* DFI LanParty */
  197. .config = STAC_REF },
  198. {} /* terminator */
  199. };
  200. static unsigned int ref922x_pin_configs[10] = {
  201. 0x01014010, 0x01016011, 0x01012012, 0x0221401f,
  202. 0x01813122, 0x01011014, 0x01441030, 0x01c41030,
  203. 0x40000100, 0x40000100,
  204. };
  205. static unsigned int d945gtp3_pin_configs[10] = {
  206. 0x0221401f, 0x01a19022, 0x01813021, 0x01114010,
  207. 0x40000100, 0x40000100, 0x40000100, 0x40000100,
  208. 0x02a19120, 0x40000100,
  209. };
  210. static unsigned int d945gtp5_pin_configs[10] = {
  211. 0x0221401f, 0x01111012, 0x01813024, 0x01114010,
  212. 0x01a19021, 0x01116011, 0x01452130, 0x40000100,
  213. 0x02a19320, 0x40000100,
  214. };
  215. static unsigned int *stac922x_brd_tbl[] = {
  216. ref922x_pin_configs,
  217. d945gtp3_pin_configs,
  218. d945gtp5_pin_configs,
  219. };
  220. static struct hda_board_config stac922x_cfg_tbl[] = {
  221. { .modelname = "ref",
  222. .pci_subvendor = PCI_VENDOR_ID_INTEL,
  223. .pci_subdevice = 0x2668, /* DFI LanParty */
  224. .config = STAC_REF }, /* SigmaTel reference board */
  225. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  226. .pci_subdevice = 0x0101,
  227. .config = STAC_D945GTP3 }, /* Intel D945GTP - 3 Stack */
  228. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  229. .pci_subdevice = 0x0404,
  230. .config = STAC_D945GTP5 }, /* Intel D945GTP - 5 Stack */
  231. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  232. .pci_subdevice = 0x0303,
  233. .config = STAC_D945GTP5 }, /* Intel D945GNT - 5 Stack */
  234. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  235. .pci_subdevice = 0x0013,
  236. .config = STAC_D945GTP5 }, /* Intel D955XBK - 5 Stack */
  237. {} /* terminator */
  238. };
  239. static void stac92xx_set_config_regs(struct hda_codec *codec)
  240. {
  241. int i;
  242. struct sigmatel_spec *spec = codec->spec;
  243. unsigned int pin_cfg;
  244. for (i=0; i < spec->num_pins; i++) {
  245. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  246. AC_VERB_SET_CONFIG_DEFAULT_BYTES_0,
  247. spec->pin_configs[i] & 0x000000ff);
  248. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  249. AC_VERB_SET_CONFIG_DEFAULT_BYTES_1,
  250. (spec->pin_configs[i] & 0x0000ff00) >> 8);
  251. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  252. AC_VERB_SET_CONFIG_DEFAULT_BYTES_2,
  253. (spec->pin_configs[i] & 0x00ff0000) >> 16);
  254. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  255. AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
  256. spec->pin_configs[i] >> 24);
  257. pin_cfg = snd_hda_codec_read(codec, spec->pin_nids[i], 0,
  258. AC_VERB_GET_CONFIG_DEFAULT,
  259. 0x00);
  260. snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x pin config %8.8x\n", spec->pin_nids[i], pin_cfg);
  261. }
  262. }
  263. /*
  264. * Analog playback callbacks
  265. */
  266. static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
  267. struct hda_codec *codec,
  268. struct snd_pcm_substream *substream)
  269. {
  270. struct sigmatel_spec *spec = codec->spec;
  271. return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
  272. }
  273. static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  274. struct hda_codec *codec,
  275. unsigned int stream_tag,
  276. unsigned int format,
  277. struct snd_pcm_substream *substream)
  278. {
  279. struct sigmatel_spec *spec = codec->spec;
  280. return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag, format, substream);
  281. }
  282. static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  283. struct hda_codec *codec,
  284. struct snd_pcm_substream *substream)
  285. {
  286. struct sigmatel_spec *spec = codec->spec;
  287. return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  288. }
  289. /*
  290. * Digital playback callbacks
  291. */
  292. static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  293. struct hda_codec *codec,
  294. struct snd_pcm_substream *substream)
  295. {
  296. struct sigmatel_spec *spec = codec->spec;
  297. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  298. }
  299. static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  300. struct hda_codec *codec,
  301. struct snd_pcm_substream *substream)
  302. {
  303. struct sigmatel_spec *spec = codec->spec;
  304. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  305. }
  306. /*
  307. * Analog capture callbacks
  308. */
  309. static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  310. struct hda_codec *codec,
  311. unsigned int stream_tag,
  312. unsigned int format,
  313. struct snd_pcm_substream *substream)
  314. {
  315. struct sigmatel_spec *spec = codec->spec;
  316. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
  317. stream_tag, 0, format);
  318. return 0;
  319. }
  320. static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  321. struct hda_codec *codec,
  322. struct snd_pcm_substream *substream)
  323. {
  324. struct sigmatel_spec *spec = codec->spec;
  325. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
  326. return 0;
  327. }
  328. static struct hda_pcm_stream stac92xx_pcm_digital_playback = {
  329. .substreams = 1,
  330. .channels_min = 2,
  331. .channels_max = 2,
  332. /* NID is set in stac92xx_build_pcms */
  333. .ops = {
  334. .open = stac92xx_dig_playback_pcm_open,
  335. .close = stac92xx_dig_playback_pcm_close
  336. },
  337. };
  338. static struct hda_pcm_stream stac92xx_pcm_digital_capture = {
  339. .substreams = 1,
  340. .channels_min = 2,
  341. .channels_max = 2,
  342. /* NID is set in stac92xx_build_pcms */
  343. };
  344. static struct hda_pcm_stream stac92xx_pcm_analog_playback = {
  345. .substreams = 1,
  346. .channels_min = 2,
  347. .channels_max = 8,
  348. .nid = 0x02, /* NID to query formats and rates */
  349. .ops = {
  350. .open = stac92xx_playback_pcm_open,
  351. .prepare = stac92xx_playback_pcm_prepare,
  352. .cleanup = stac92xx_playback_pcm_cleanup
  353. },
  354. };
  355. static struct hda_pcm_stream stac92xx_pcm_analog_capture = {
  356. .substreams = 2,
  357. .channels_min = 2,
  358. .channels_max = 2,
  359. .nid = 0x06, /* NID to query formats and rates */
  360. .ops = {
  361. .prepare = stac92xx_capture_pcm_prepare,
  362. .cleanup = stac92xx_capture_pcm_cleanup
  363. },
  364. };
  365. static int stac92xx_build_pcms(struct hda_codec *codec)
  366. {
  367. struct sigmatel_spec *spec = codec->spec;
  368. struct hda_pcm *info = spec->pcm_rec;
  369. codec->num_pcms = 1;
  370. codec->pcm_info = info;
  371. info->name = "STAC92xx Analog";
  372. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
  373. info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
  374. if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
  375. codec->num_pcms++;
  376. info++;
  377. info->name = "STAC92xx Digital";
  378. if (spec->multiout.dig_out_nid) {
  379. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
  380. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
  381. }
  382. if (spec->dig_in_nid) {
  383. info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
  384. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
  385. }
  386. }
  387. return 0;
  388. }
  389. static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
  390. {
  391. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
  392. }
  393. static int stac92xx_io_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  394. {
  395. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  396. uinfo->count = 1;
  397. uinfo->value.integer.min = 0;
  398. uinfo->value.integer.max = 1;
  399. return 0;
  400. }
  401. static int stac92xx_io_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  402. {
  403. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  404. struct sigmatel_spec *spec = codec->spec;
  405. int io_idx = kcontrol-> private_value & 0xff;
  406. ucontrol->value.integer.value[0] = spec->io_switch[io_idx];
  407. return 0;
  408. }
  409. static int stac92xx_io_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  410. {
  411. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  412. struct sigmatel_spec *spec = codec->spec;
  413. hda_nid_t nid = kcontrol->private_value >> 8;
  414. int io_idx = kcontrol-> private_value & 0xff;
  415. unsigned short val = ucontrol->value.integer.value[0];
  416. spec->io_switch[io_idx] = val;
  417. if (val)
  418. stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
  419. else
  420. stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_IN_EN);
  421. return 1;
  422. }
  423. #define STAC_CODEC_IO_SWITCH(xname, xpval) \
  424. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  425. .name = xname, \
  426. .index = 0, \
  427. .info = stac92xx_io_switch_info, \
  428. .get = stac92xx_io_switch_get, \
  429. .put = stac92xx_io_switch_put, \
  430. .private_value = xpval, \
  431. }
  432. enum {
  433. STAC_CTL_WIDGET_VOL,
  434. STAC_CTL_WIDGET_MUTE,
  435. STAC_CTL_WIDGET_IO_SWITCH,
  436. };
  437. static struct snd_kcontrol_new stac92xx_control_templates[] = {
  438. HDA_CODEC_VOLUME(NULL, 0, 0, 0),
  439. HDA_CODEC_MUTE(NULL, 0, 0, 0),
  440. STAC_CODEC_IO_SWITCH(NULL, 0),
  441. };
  442. /* add dynamic controls */
  443. static int stac92xx_add_control(struct sigmatel_spec *spec, int type, const char *name, unsigned long val)
  444. {
  445. struct snd_kcontrol_new *knew;
  446. if (spec->num_kctl_used >= spec->num_kctl_alloc) {
  447. int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
  448. knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
  449. if (! knew)
  450. return -ENOMEM;
  451. if (spec->kctl_alloc) {
  452. memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
  453. kfree(spec->kctl_alloc);
  454. }
  455. spec->kctl_alloc = knew;
  456. spec->num_kctl_alloc = num;
  457. }
  458. knew = &spec->kctl_alloc[spec->num_kctl_used];
  459. *knew = stac92xx_control_templates[type];
  460. knew->name = kstrdup(name, GFP_KERNEL);
  461. if (! knew->name)
  462. return -ENOMEM;
  463. knew->private_value = val;
  464. spec->num_kctl_used++;
  465. return 0;
  466. }
  467. /* flag inputs as additional dynamic lineouts */
  468. static int stac92xx_add_dyn_out_pins(struct hda_codec *codec, struct auto_pin_cfg *cfg)
  469. {
  470. struct sigmatel_spec *spec = codec->spec;
  471. switch (cfg->line_outs) {
  472. case 3:
  473. /* add line-in as side */
  474. if (cfg->input_pins[AUTO_PIN_LINE]) {
  475. cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_LINE];
  476. spec->line_switch = 1;
  477. cfg->line_outs++;
  478. }
  479. break;
  480. case 2:
  481. /* add line-in as clfe and mic as side */
  482. if (cfg->input_pins[AUTO_PIN_LINE]) {
  483. cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_LINE];
  484. spec->line_switch = 1;
  485. cfg->line_outs++;
  486. }
  487. if (cfg->input_pins[AUTO_PIN_MIC]) {
  488. cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_MIC];
  489. spec->mic_switch = 1;
  490. cfg->line_outs++;
  491. }
  492. break;
  493. case 1:
  494. /* add line-in as surr and mic as clfe */
  495. if (cfg->input_pins[AUTO_PIN_LINE]) {
  496. cfg->line_out_pins[1] = cfg->input_pins[AUTO_PIN_LINE];
  497. spec->line_switch = 1;
  498. cfg->line_outs++;
  499. }
  500. if (cfg->input_pins[AUTO_PIN_MIC]) {
  501. cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_MIC];
  502. spec->mic_switch = 1;
  503. cfg->line_outs++;
  504. }
  505. break;
  506. }
  507. return 0;
  508. }
  509. /* fill in the dac_nids table from the parsed pin configuration */
  510. static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
  511. {
  512. struct sigmatel_spec *spec = codec->spec;
  513. hda_nid_t nid;
  514. int i;
  515. /* check the pins hardwired to audio widget */
  516. for (i = 0; i < cfg->line_outs; i++) {
  517. nid = cfg->line_out_pins[i];
  518. spec->multiout.dac_nids[i] = snd_hda_codec_read(codec, nid, 0,
  519. AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
  520. }
  521. spec->multiout.num_dacs = cfg->line_outs;
  522. return 0;
  523. }
  524. /* add playback controls from the parsed DAC table */
  525. static int stac92xx_auto_create_multi_out_ctls(struct sigmatel_spec *spec, const struct auto_pin_cfg *cfg)
  526. {
  527. char name[32];
  528. static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
  529. hda_nid_t nid;
  530. int i, err;
  531. for (i = 0; i < cfg->line_outs; i++) {
  532. if (!spec->multiout.dac_nids[i])
  533. continue;
  534. nid = spec->multiout.dac_nids[i];
  535. if (i == 2) {
  536. /* Center/LFE */
  537. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Center Playback Volume",
  538. HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
  539. return err;
  540. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "LFE Playback Volume",
  541. HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
  542. return err;
  543. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Center Playback Switch",
  544. HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
  545. return err;
  546. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "LFE Playback Switch",
  547. HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
  548. return err;
  549. } else {
  550. sprintf(name, "%s Playback Volume", chname[i]);
  551. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, name,
  552. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  553. return err;
  554. sprintf(name, "%s Playback Switch", chname[i]);
  555. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, name,
  556. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  557. return err;
  558. }
  559. }
  560. if (spec->line_switch)
  561. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Line In as Output Switch", cfg->input_pins[AUTO_PIN_LINE] << 8)) < 0)
  562. return err;
  563. if (spec->mic_switch)
  564. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Mic as Output Switch", (cfg->input_pins[AUTO_PIN_MIC] << 8) | 1)) < 0)
  565. return err;
  566. return 0;
  567. }
  568. /* add playback controls for HP output */
  569. static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec, struct auto_pin_cfg *cfg)
  570. {
  571. struct sigmatel_spec *spec = codec->spec;
  572. hda_nid_t pin = cfg->hp_pin;
  573. hda_nid_t nid;
  574. int i, err;
  575. unsigned int wid_caps;
  576. if (! pin)
  577. return 0;
  578. wid_caps = get_wcaps(codec, pin);
  579. if (wid_caps & AC_WCAP_UNSOL_CAP)
  580. /* Enable unsolicited responses on the HP widget */
  581. snd_hda_codec_write(codec, pin, 0,
  582. AC_VERB_SET_UNSOLICITED_ENABLE,
  583. STAC_UNSOL_ENABLE);
  584. nid = snd_hda_codec_read(codec, pin, 0, AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
  585. for (i = 0; i < cfg->line_outs; i++) {
  586. if (! spec->multiout.dac_nids[i])
  587. continue;
  588. if (spec->multiout.dac_nids[i] == nid)
  589. return 0;
  590. }
  591. spec->multiout.hp_nid = nid;
  592. /* control HP volume/switch on the output mixer amp */
  593. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Headphone Playback Volume",
  594. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  595. return err;
  596. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Headphone Playback Switch",
  597. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  598. return err;
  599. return 0;
  600. }
  601. /* create playback/capture controls for input pins */
  602. static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
  603. {
  604. struct sigmatel_spec *spec = codec->spec;
  605. struct hda_input_mux *imux = &spec->private_imux;
  606. hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
  607. int i, j, k;
  608. for (i = 0; i < AUTO_PIN_LAST; i++) {
  609. int index = -1;
  610. if (cfg->input_pins[i]) {
  611. /* Enable active pin widget as an input */
  612. stac92xx_auto_set_pinctl(codec, cfg->input_pins[i], AC_PINCTL_IN_EN);
  613. imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
  614. for (j=0; j<spec->num_muxes; j++) {
  615. int num_cons = snd_hda_get_connections(codec, spec->mux_nids[j], con_lst, HDA_MAX_NUM_INPUTS);
  616. for (k=0; k<num_cons; k++)
  617. if (con_lst[k] == cfg->input_pins[i]) {
  618. index = k;
  619. break;
  620. }
  621. if (index >= 0)
  622. break;
  623. }
  624. imux->items[imux->num_items].index = index;
  625. imux->num_items++;
  626. }
  627. }
  628. return 0;
  629. }
  630. static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
  631. {
  632. struct sigmatel_spec *spec = codec->spec;
  633. int i;
  634. for (i = 0; i < spec->autocfg.line_outs; i++) {
  635. hda_nid_t nid = spec->autocfg.line_out_pins[i];
  636. stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
  637. }
  638. }
  639. static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
  640. {
  641. struct sigmatel_spec *spec = codec->spec;
  642. hda_nid_t pin;
  643. pin = spec->autocfg.hp_pin;
  644. if (pin) /* connect to front */
  645. stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
  646. }
  647. static int stac922x_parse_auto_config(struct hda_codec *codec)
  648. {
  649. struct sigmatel_spec *spec = codec->spec;
  650. int err;
  651. if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
  652. return err;
  653. if ((err = stac92xx_add_dyn_out_pins(codec, &spec->autocfg)) < 0)
  654. return err;
  655. if ((err = stac92xx_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
  656. return err;
  657. if (! spec->autocfg.line_outs && ! spec->autocfg.hp_pin)
  658. return 0; /* can't find valid pin config */
  659. if ((err = stac92xx_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
  660. (err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg)) < 0 ||
  661. (err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
  662. return err;
  663. spec->multiout.max_channels = spec->multiout.num_dacs * 2;
  664. if (spec->multiout.max_channels > 2)
  665. spec->surr_switch = 1;
  666. if (spec->autocfg.dig_out_pin) {
  667. spec->multiout.dig_out_nid = 0x08;
  668. stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_out_pin, AC_PINCTL_OUT_EN);
  669. }
  670. if (spec->autocfg.dig_in_pin) {
  671. spec->dig_in_nid = 0x09;
  672. stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_in_pin, AC_PINCTL_IN_EN);
  673. }
  674. if (spec->kctl_alloc)
  675. spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
  676. spec->input_mux = &spec->private_imux;
  677. return 1;
  678. }
  679. static int stac9200_parse_auto_config(struct hda_codec *codec)
  680. {
  681. struct sigmatel_spec *spec = codec->spec;
  682. int err;
  683. if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
  684. return err;
  685. if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
  686. return err;
  687. if (spec->autocfg.dig_out_pin) {
  688. spec->multiout.dig_out_nid = 0x05;
  689. stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_out_pin, AC_PINCTL_OUT_EN);
  690. }
  691. if (spec->autocfg.dig_in_pin) {
  692. spec->dig_in_nid = 0x04;
  693. stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_in_pin, AC_PINCTL_IN_EN);
  694. }
  695. if (spec->kctl_alloc)
  696. spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
  697. spec->input_mux = &spec->private_imux;
  698. return 1;
  699. }
  700. static int stac92xx_init(struct hda_codec *codec)
  701. {
  702. struct sigmatel_spec *spec = codec->spec;
  703. snd_hda_sequence_write(codec, spec->init);
  704. stac92xx_auto_init_multi_out(codec);
  705. stac92xx_auto_init_hp_out(codec);
  706. return 0;
  707. }
  708. static void stac92xx_free(struct hda_codec *codec)
  709. {
  710. struct sigmatel_spec *spec = codec->spec;
  711. int i;
  712. if (! spec)
  713. return;
  714. if (spec->kctl_alloc) {
  715. for (i = 0; i < spec->num_kctl_used; i++)
  716. kfree(spec->kctl_alloc[i].name);
  717. kfree(spec->kctl_alloc);
  718. }
  719. kfree(spec);
  720. }
  721. static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
  722. unsigned int flag)
  723. {
  724. unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
  725. 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
  726. snd_hda_codec_write(codec, nid, 0,
  727. AC_VERB_SET_PIN_WIDGET_CONTROL,
  728. pin_ctl | flag);
  729. }
  730. static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
  731. unsigned int flag)
  732. {
  733. unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
  734. 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
  735. snd_hda_codec_write(codec, nid, 0,
  736. AC_VERB_SET_PIN_WIDGET_CONTROL,
  737. pin_ctl & ~flag);
  738. }
  739. static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
  740. {
  741. struct sigmatel_spec *spec = codec->spec;
  742. struct auto_pin_cfg *cfg = &spec->autocfg;
  743. int i, presence;
  744. if ((res >> 26) != STAC_HP_EVENT)
  745. return;
  746. presence = snd_hda_codec_read(codec, cfg->hp_pin, 0,
  747. AC_VERB_GET_PIN_SENSE, 0x00) >> 31;
  748. if (presence) {
  749. /* disable lineouts, enable hp */
  750. for (i = 0; i < cfg->line_outs; i++)
  751. stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
  752. AC_PINCTL_OUT_EN);
  753. stac92xx_set_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
  754. } else {
  755. /* enable lineouts, disable hp */
  756. for (i = 0; i < cfg->line_outs; i++)
  757. stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
  758. AC_PINCTL_OUT_EN);
  759. stac92xx_reset_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
  760. }
  761. }
  762. #ifdef CONFIG_PM
  763. static int stac92xx_resume(struct hda_codec *codec)
  764. {
  765. struct sigmatel_spec *spec = codec->spec;
  766. int i;
  767. stac92xx_init(codec);
  768. for (i = 0; i < spec->num_mixers; i++)
  769. snd_hda_resume_ctls(codec, spec->mixers[i]);
  770. if (spec->multiout.dig_out_nid)
  771. snd_hda_resume_spdif_out(codec);
  772. if (spec->dig_in_nid)
  773. snd_hda_resume_spdif_in(codec);
  774. return 0;
  775. }
  776. #endif
  777. static struct hda_codec_ops stac92xx_patch_ops = {
  778. .build_controls = stac92xx_build_controls,
  779. .build_pcms = stac92xx_build_pcms,
  780. .init = stac92xx_init,
  781. .free = stac92xx_free,
  782. .unsol_event = stac92xx_unsol_event,
  783. #ifdef CONFIG_PM
  784. .resume = stac92xx_resume,
  785. #endif
  786. };
  787. static int patch_stac9200(struct hda_codec *codec)
  788. {
  789. struct sigmatel_spec *spec;
  790. int err;
  791. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  792. if (spec == NULL)
  793. return -ENOMEM;
  794. codec->spec = spec;
  795. spec->board_config = snd_hda_check_board_config(codec, stac9200_cfg_tbl);
  796. if (spec->board_config < 0)
  797. snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9200, using BIOS defaults\n");
  798. else {
  799. spec->num_pins = 8;
  800. spec->pin_nids = stac9200_pin_nids;
  801. spec->pin_configs = stac9200_brd_tbl[spec->board_config];
  802. stac92xx_set_config_regs(codec);
  803. }
  804. spec->multiout.max_channels = 2;
  805. spec->multiout.num_dacs = 1;
  806. spec->multiout.dac_nids = stac9200_dac_nids;
  807. spec->adc_nids = stac9200_adc_nids;
  808. spec->mux_nids = stac9200_mux_nids;
  809. spec->num_muxes = 1;
  810. spec->init = stac9200_core_init;
  811. spec->mixer = stac9200_mixer;
  812. err = stac9200_parse_auto_config(codec);
  813. if (err < 0) {
  814. stac92xx_free(codec);
  815. return err;
  816. }
  817. codec->patch_ops = stac92xx_patch_ops;
  818. return 0;
  819. }
  820. static int patch_stac922x(struct hda_codec *codec)
  821. {
  822. struct sigmatel_spec *spec;
  823. int err;
  824. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  825. if (spec == NULL)
  826. return -ENOMEM;
  827. codec->spec = spec;
  828. spec->board_config = snd_hda_check_board_config(codec, stac922x_cfg_tbl);
  829. if (spec->board_config < 0)
  830. snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC922x, using BIOS defaults\n");
  831. else {
  832. spec->num_pins = 10;
  833. spec->pin_nids = stac922x_pin_nids;
  834. spec->pin_configs = stac922x_brd_tbl[spec->board_config];
  835. stac92xx_set_config_regs(codec);
  836. }
  837. spec->adc_nids = stac922x_adc_nids;
  838. spec->mux_nids = stac922x_mux_nids;
  839. spec->num_muxes = 2;
  840. spec->init = stac922x_core_init;
  841. spec->mixer = stac922x_mixer;
  842. spec->multiout.dac_nids = spec->dac_nids;
  843. err = stac922x_parse_auto_config(codec);
  844. if (err < 0) {
  845. stac92xx_free(codec);
  846. return err;
  847. }
  848. codec->patch_ops = stac92xx_patch_ops;
  849. return 0;
  850. }
  851. /*
  852. * patch entries
  853. */
  854. struct hda_codec_preset snd_hda_preset_sigmatel[] = {
  855. { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
  856. { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
  857. { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
  858. { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
  859. { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
  860. { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
  861. { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
  862. {} /* terminator */
  863. };