patch_sigmatel.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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_VOLUME("Mux Capture Volume", 0x12, 0x0, HDA_OUTPUT),
  158. { } /* end */
  159. };
  160. static int stac92xx_build_controls(struct hda_codec *codec)
  161. {
  162. struct sigmatel_spec *spec = codec->spec;
  163. int err;
  164. int i;
  165. err = snd_hda_add_new_ctls(codec, spec->mixer);
  166. if (err < 0)
  167. return err;
  168. for (i = 0; i < spec->num_mixers; i++) {
  169. err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
  170. if (err < 0)
  171. return err;
  172. }
  173. if (spec->multiout.dig_out_nid) {
  174. err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
  175. if (err < 0)
  176. return err;
  177. }
  178. if (spec->dig_in_nid) {
  179. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
  180. if (err < 0)
  181. return err;
  182. }
  183. return 0;
  184. }
  185. static unsigned int ref9200_pin_configs[8] = {
  186. 0x01c47010, 0x01447010, 0x0221401f, 0x01114010,
  187. 0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
  188. };
  189. static unsigned int *stac9200_brd_tbl[] = {
  190. ref9200_pin_configs,
  191. };
  192. static struct hda_board_config stac9200_cfg_tbl[] = {
  193. { .modelname = "ref",
  194. .pci_subvendor = PCI_VENDOR_ID_INTEL,
  195. .pci_subdevice = 0x2668, /* DFI LanParty */
  196. .config = STAC_REF },
  197. {} /* terminator */
  198. };
  199. static unsigned int ref922x_pin_configs[10] = {
  200. 0x01014010, 0x01016011, 0x01012012, 0x0221401f,
  201. 0x01813122, 0x01011014, 0x01441030, 0x01c41030,
  202. 0x40000100, 0x40000100,
  203. };
  204. static unsigned int d945gtp3_pin_configs[10] = {
  205. 0x0221401f, 0x01a19022, 0x01813021, 0x01114010,
  206. 0x40000100, 0x40000100, 0x40000100, 0x40000100,
  207. 0x02a19120, 0x40000100,
  208. };
  209. static unsigned int d945gtp5_pin_configs[10] = {
  210. 0x0221401f, 0x01111012, 0x01813024, 0x01114010,
  211. 0x01a19021, 0x01116011, 0x01452130, 0x40000100,
  212. 0x02a19320, 0x40000100,
  213. };
  214. static unsigned int *stac922x_brd_tbl[] = {
  215. ref922x_pin_configs,
  216. d945gtp3_pin_configs,
  217. d945gtp5_pin_configs,
  218. };
  219. static struct hda_board_config stac922x_cfg_tbl[] = {
  220. { .modelname = "ref",
  221. .pci_subvendor = PCI_VENDOR_ID_INTEL,
  222. .pci_subdevice = 0x2668, /* DFI LanParty */
  223. .config = STAC_REF }, /* SigmaTel reference board */
  224. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  225. .pci_subdevice = 0x0101,
  226. .config = STAC_D945GTP3 }, /* Intel D945GTP - 3 Stack */
  227. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  228. .pci_subdevice = 0x0404,
  229. .config = STAC_D945GTP5 }, /* Intel D945GTP - 5 Stack */
  230. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  231. .pci_subdevice = 0x0303,
  232. .config = STAC_D945GTP5 }, /* Intel D945GNT - 5 Stack */
  233. { .pci_subvendor = PCI_VENDOR_ID_INTEL,
  234. .pci_subdevice = 0x0013,
  235. .config = STAC_D945GTP5 }, /* Intel D955XBK - 5 Stack */
  236. {} /* terminator */
  237. };
  238. static void stac92xx_set_config_regs(struct hda_codec *codec)
  239. {
  240. int i;
  241. struct sigmatel_spec *spec = codec->spec;
  242. unsigned int pin_cfg;
  243. for (i=0; i < spec->num_pins; i++) {
  244. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  245. AC_VERB_SET_CONFIG_DEFAULT_BYTES_0,
  246. spec->pin_configs[i] & 0x000000ff);
  247. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  248. AC_VERB_SET_CONFIG_DEFAULT_BYTES_1,
  249. (spec->pin_configs[i] & 0x0000ff00) >> 8);
  250. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  251. AC_VERB_SET_CONFIG_DEFAULT_BYTES_2,
  252. (spec->pin_configs[i] & 0x00ff0000) >> 16);
  253. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  254. AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
  255. spec->pin_configs[i] >> 24);
  256. pin_cfg = snd_hda_codec_read(codec, spec->pin_nids[i], 0,
  257. AC_VERB_GET_CONFIG_DEFAULT,
  258. 0x00);
  259. snd_printdd(KERN_INFO "hda_codec: pin nid %2.2x pin config %8.8x\n", spec->pin_nids[i], pin_cfg);
  260. }
  261. }
  262. /*
  263. * Analog playback callbacks
  264. */
  265. static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
  266. struct hda_codec *codec,
  267. struct snd_pcm_substream *substream)
  268. {
  269. struct sigmatel_spec *spec = codec->spec;
  270. return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
  271. }
  272. static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  273. struct hda_codec *codec,
  274. unsigned int stream_tag,
  275. unsigned int format,
  276. struct snd_pcm_substream *substream)
  277. {
  278. struct sigmatel_spec *spec = codec->spec;
  279. return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag, format, substream);
  280. }
  281. static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  282. struct hda_codec *codec,
  283. struct snd_pcm_substream *substream)
  284. {
  285. struct sigmatel_spec *spec = codec->spec;
  286. return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  287. }
  288. /*
  289. * Digital playback callbacks
  290. */
  291. static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  292. struct hda_codec *codec,
  293. struct snd_pcm_substream *substream)
  294. {
  295. struct sigmatel_spec *spec = codec->spec;
  296. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  297. }
  298. static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  299. struct hda_codec *codec,
  300. struct snd_pcm_substream *substream)
  301. {
  302. struct sigmatel_spec *spec = codec->spec;
  303. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  304. }
  305. /*
  306. * Analog capture callbacks
  307. */
  308. static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  309. struct hda_codec *codec,
  310. unsigned int stream_tag,
  311. unsigned int format,
  312. struct snd_pcm_substream *substream)
  313. {
  314. struct sigmatel_spec *spec = codec->spec;
  315. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
  316. stream_tag, 0, format);
  317. return 0;
  318. }
  319. static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  320. struct hda_codec *codec,
  321. struct snd_pcm_substream *substream)
  322. {
  323. struct sigmatel_spec *spec = codec->spec;
  324. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
  325. return 0;
  326. }
  327. static struct hda_pcm_stream stac92xx_pcm_digital_playback = {
  328. .substreams = 1,
  329. .channels_min = 2,
  330. .channels_max = 2,
  331. /* NID is set in stac92xx_build_pcms */
  332. .ops = {
  333. .open = stac92xx_dig_playback_pcm_open,
  334. .close = stac92xx_dig_playback_pcm_close
  335. },
  336. };
  337. static struct hda_pcm_stream stac92xx_pcm_digital_capture = {
  338. .substreams = 1,
  339. .channels_min = 2,
  340. .channels_max = 2,
  341. /* NID is set in stac92xx_build_pcms */
  342. };
  343. static struct hda_pcm_stream stac92xx_pcm_analog_playback = {
  344. .substreams = 1,
  345. .channels_min = 2,
  346. .channels_max = 8,
  347. .nid = 0x02, /* NID to query formats and rates */
  348. .ops = {
  349. .open = stac92xx_playback_pcm_open,
  350. .prepare = stac92xx_playback_pcm_prepare,
  351. .cleanup = stac92xx_playback_pcm_cleanup
  352. },
  353. };
  354. static struct hda_pcm_stream stac92xx_pcm_analog_capture = {
  355. .substreams = 2,
  356. .channels_min = 2,
  357. .channels_max = 2,
  358. .nid = 0x06, /* NID to query formats and rates */
  359. .ops = {
  360. .prepare = stac92xx_capture_pcm_prepare,
  361. .cleanup = stac92xx_capture_pcm_cleanup
  362. },
  363. };
  364. static int stac92xx_build_pcms(struct hda_codec *codec)
  365. {
  366. struct sigmatel_spec *spec = codec->spec;
  367. struct hda_pcm *info = spec->pcm_rec;
  368. codec->num_pcms = 1;
  369. codec->pcm_info = info;
  370. info->name = "STAC92xx Analog";
  371. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
  372. info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
  373. if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
  374. codec->num_pcms++;
  375. info++;
  376. info->name = "STAC92xx Digital";
  377. if (spec->multiout.dig_out_nid) {
  378. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
  379. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
  380. }
  381. if (spec->dig_in_nid) {
  382. info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
  383. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
  384. }
  385. }
  386. return 0;
  387. }
  388. static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
  389. {
  390. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
  391. }
  392. static int stac92xx_io_switch_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  393. {
  394. uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
  395. uinfo->count = 1;
  396. uinfo->value.integer.min = 0;
  397. uinfo->value.integer.max = 1;
  398. return 0;
  399. }
  400. static int stac92xx_io_switch_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  401. {
  402. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  403. struct sigmatel_spec *spec = codec->spec;
  404. int io_idx = kcontrol-> private_value & 0xff;
  405. ucontrol->value.integer.value[0] = spec->io_switch[io_idx];
  406. return 0;
  407. }
  408. static int stac92xx_io_switch_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  409. {
  410. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  411. struct sigmatel_spec *spec = codec->spec;
  412. hda_nid_t nid = kcontrol->private_value >> 8;
  413. int io_idx = kcontrol-> private_value & 0xff;
  414. unsigned short val = ucontrol->value.integer.value[0];
  415. spec->io_switch[io_idx] = val;
  416. if (val)
  417. stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
  418. else
  419. stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_IN_EN);
  420. return 1;
  421. }
  422. #define STAC_CODEC_IO_SWITCH(xname, xpval) \
  423. { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
  424. .name = xname, \
  425. .index = 0, \
  426. .info = stac92xx_io_switch_info, \
  427. .get = stac92xx_io_switch_get, \
  428. .put = stac92xx_io_switch_put, \
  429. .private_value = xpval, \
  430. }
  431. enum {
  432. STAC_CTL_WIDGET_VOL,
  433. STAC_CTL_WIDGET_MUTE,
  434. STAC_CTL_WIDGET_IO_SWITCH,
  435. };
  436. static struct snd_kcontrol_new stac92xx_control_templates[] = {
  437. HDA_CODEC_VOLUME(NULL, 0, 0, 0),
  438. HDA_CODEC_MUTE(NULL, 0, 0, 0),
  439. STAC_CODEC_IO_SWITCH(NULL, 0),
  440. };
  441. /* add dynamic controls */
  442. static int stac92xx_add_control(struct sigmatel_spec *spec, int type, const char *name, unsigned long val)
  443. {
  444. struct snd_kcontrol_new *knew;
  445. if (spec->num_kctl_used >= spec->num_kctl_alloc) {
  446. int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
  447. knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
  448. if (! knew)
  449. return -ENOMEM;
  450. if (spec->kctl_alloc) {
  451. memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
  452. kfree(spec->kctl_alloc);
  453. }
  454. spec->kctl_alloc = knew;
  455. spec->num_kctl_alloc = num;
  456. }
  457. knew = &spec->kctl_alloc[spec->num_kctl_used];
  458. *knew = stac92xx_control_templates[type];
  459. knew->name = kstrdup(name, GFP_KERNEL);
  460. if (! knew->name)
  461. return -ENOMEM;
  462. knew->private_value = val;
  463. spec->num_kctl_used++;
  464. return 0;
  465. }
  466. /* flag inputs as additional dynamic lineouts */
  467. static int stac92xx_add_dyn_out_pins(struct hda_codec *codec, struct auto_pin_cfg *cfg)
  468. {
  469. struct sigmatel_spec *spec = codec->spec;
  470. switch (cfg->line_outs) {
  471. case 3:
  472. /* add line-in as side */
  473. if (cfg->input_pins[AUTO_PIN_LINE]) {
  474. cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_LINE];
  475. spec->line_switch = 1;
  476. cfg->line_outs++;
  477. }
  478. break;
  479. case 2:
  480. /* add line-in as clfe and mic as side */
  481. if (cfg->input_pins[AUTO_PIN_LINE]) {
  482. cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_LINE];
  483. spec->line_switch = 1;
  484. cfg->line_outs++;
  485. }
  486. if (cfg->input_pins[AUTO_PIN_MIC]) {
  487. cfg->line_out_pins[3] = cfg->input_pins[AUTO_PIN_MIC];
  488. spec->mic_switch = 1;
  489. cfg->line_outs++;
  490. }
  491. break;
  492. case 1:
  493. /* add line-in as surr and mic as clfe */
  494. if (cfg->input_pins[AUTO_PIN_LINE]) {
  495. cfg->line_out_pins[1] = cfg->input_pins[AUTO_PIN_LINE];
  496. spec->line_switch = 1;
  497. cfg->line_outs++;
  498. }
  499. if (cfg->input_pins[AUTO_PIN_MIC]) {
  500. cfg->line_out_pins[2] = cfg->input_pins[AUTO_PIN_MIC];
  501. spec->mic_switch = 1;
  502. cfg->line_outs++;
  503. }
  504. break;
  505. }
  506. return 0;
  507. }
  508. /* fill in the dac_nids table from the parsed pin configuration */
  509. static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
  510. {
  511. struct sigmatel_spec *spec = codec->spec;
  512. hda_nid_t nid;
  513. int i;
  514. /* check the pins hardwired to audio widget */
  515. for (i = 0; i < cfg->line_outs; i++) {
  516. nid = cfg->line_out_pins[i];
  517. spec->multiout.dac_nids[i] = snd_hda_codec_read(codec, nid, 0,
  518. AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
  519. }
  520. spec->multiout.num_dacs = cfg->line_outs;
  521. return 0;
  522. }
  523. /* add playback controls from the parsed DAC table */
  524. static int stac92xx_auto_create_multi_out_ctls(struct sigmatel_spec *spec, const struct auto_pin_cfg *cfg)
  525. {
  526. char name[32];
  527. static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
  528. hda_nid_t nid;
  529. int i, err;
  530. for (i = 0; i < cfg->line_outs; i++) {
  531. if (!spec->multiout.dac_nids[i])
  532. continue;
  533. nid = spec->multiout.dac_nids[i];
  534. if (i == 2) {
  535. /* Center/LFE */
  536. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Center Playback Volume",
  537. HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
  538. return err;
  539. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "LFE Playback Volume",
  540. HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
  541. return err;
  542. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Center Playback Switch",
  543. HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
  544. return err;
  545. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "LFE Playback Switch",
  546. HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
  547. return err;
  548. } else {
  549. sprintf(name, "%s Playback Volume", chname[i]);
  550. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, name,
  551. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  552. return err;
  553. sprintf(name, "%s Playback Switch", chname[i]);
  554. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, name,
  555. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  556. return err;
  557. }
  558. }
  559. if (spec->line_switch)
  560. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Line In as Output Switch", cfg->input_pins[AUTO_PIN_LINE] << 8)) < 0)
  561. return err;
  562. if (spec->mic_switch)
  563. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_IO_SWITCH, "Mic as Output Switch", (cfg->input_pins[AUTO_PIN_MIC] << 8) | 1)) < 0)
  564. return err;
  565. return 0;
  566. }
  567. /* add playback controls for HP output */
  568. static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec, struct auto_pin_cfg *cfg)
  569. {
  570. struct sigmatel_spec *spec = codec->spec;
  571. hda_nid_t pin = cfg->hp_pin;
  572. hda_nid_t nid;
  573. int i, err;
  574. unsigned int wid_caps;
  575. if (! pin)
  576. return 0;
  577. wid_caps = get_wcaps(codec, pin);
  578. if (wid_caps & AC_WCAP_UNSOL_CAP)
  579. /* Enable unsolicited responses on the HP widget */
  580. snd_hda_codec_write(codec, pin, 0,
  581. AC_VERB_SET_UNSOLICITED_ENABLE,
  582. STAC_UNSOL_ENABLE);
  583. nid = snd_hda_codec_read(codec, pin, 0, AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
  584. for (i = 0; i < cfg->line_outs; i++) {
  585. if (! spec->multiout.dac_nids[i])
  586. continue;
  587. if (spec->multiout.dac_nids[i] == nid)
  588. return 0;
  589. }
  590. spec->multiout.hp_nid = nid;
  591. /* control HP volume/switch on the output mixer amp */
  592. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Headphone Playback Volume",
  593. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  594. return err;
  595. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Headphone Playback Switch",
  596. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  597. return err;
  598. return 0;
  599. }
  600. /* create playback/capture controls for input pins */
  601. static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
  602. {
  603. struct sigmatel_spec *spec = codec->spec;
  604. struct hda_input_mux *imux = &spec->private_imux;
  605. hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
  606. int i, j, k;
  607. for (i = 0; i < AUTO_PIN_LAST; i++) {
  608. int index = -1;
  609. if (cfg->input_pins[i]) {
  610. /* Enable active pin widget as an input */
  611. stac92xx_auto_set_pinctl(codec, cfg->input_pins[i], AC_PINCTL_IN_EN);
  612. imux->items[imux->num_items].label = auto_pin_cfg_labels[i];
  613. for (j=0; j<spec->num_muxes; j++) {
  614. int num_cons = snd_hda_get_connections(codec, spec->mux_nids[j], con_lst, HDA_MAX_NUM_INPUTS);
  615. for (k=0; k<num_cons; k++)
  616. if (con_lst[k] == cfg->input_pins[i]) {
  617. index = k;
  618. break;
  619. }
  620. if (index >= 0)
  621. break;
  622. }
  623. imux->items[imux->num_items].index = index;
  624. imux->num_items++;
  625. }
  626. }
  627. return 0;
  628. }
  629. static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
  630. {
  631. struct sigmatel_spec *spec = codec->spec;
  632. int i;
  633. for (i = 0; i < spec->autocfg.line_outs; i++) {
  634. hda_nid_t nid = spec->autocfg.line_out_pins[i];
  635. stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
  636. }
  637. }
  638. static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
  639. {
  640. struct sigmatel_spec *spec = codec->spec;
  641. hda_nid_t pin;
  642. pin = spec->autocfg.hp_pin;
  643. if (pin) /* connect to front */
  644. stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
  645. }
  646. static int stac922x_parse_auto_config(struct hda_codec *codec)
  647. {
  648. struct sigmatel_spec *spec = codec->spec;
  649. int err;
  650. if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
  651. return err;
  652. if ((err = stac92xx_add_dyn_out_pins(codec, &spec->autocfg)) < 0)
  653. return err;
  654. if ((err = stac92xx_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
  655. return err;
  656. if (! spec->autocfg.line_outs && ! spec->autocfg.hp_pin)
  657. return 0; /* can't find valid pin config */
  658. if ((err = stac92xx_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
  659. (err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg)) < 0 ||
  660. (err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
  661. return err;
  662. spec->multiout.max_channels = spec->multiout.num_dacs * 2;
  663. if (spec->multiout.max_channels > 2)
  664. spec->surr_switch = 1;
  665. if (spec->autocfg.dig_out_pin) {
  666. spec->multiout.dig_out_nid = 0x08;
  667. stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_out_pin, AC_PINCTL_OUT_EN);
  668. }
  669. if (spec->autocfg.dig_in_pin) {
  670. spec->dig_in_nid = 0x09;
  671. stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_in_pin, AC_PINCTL_IN_EN);
  672. }
  673. if (spec->kctl_alloc)
  674. spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
  675. spec->input_mux = &spec->private_imux;
  676. return 1;
  677. }
  678. static int stac9200_parse_auto_config(struct hda_codec *codec)
  679. {
  680. struct sigmatel_spec *spec = codec->spec;
  681. int err;
  682. if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL)) < 0)
  683. return err;
  684. if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
  685. return err;
  686. if (spec->autocfg.dig_out_pin) {
  687. spec->multiout.dig_out_nid = 0x05;
  688. stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_out_pin, AC_PINCTL_OUT_EN);
  689. }
  690. if (spec->autocfg.dig_in_pin) {
  691. spec->dig_in_nid = 0x04;
  692. stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_in_pin, AC_PINCTL_IN_EN);
  693. }
  694. if (spec->kctl_alloc)
  695. spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
  696. spec->input_mux = &spec->private_imux;
  697. return 1;
  698. }
  699. static int stac92xx_init(struct hda_codec *codec)
  700. {
  701. struct sigmatel_spec *spec = codec->spec;
  702. snd_hda_sequence_write(codec, spec->init);
  703. stac92xx_auto_init_multi_out(codec);
  704. stac92xx_auto_init_hp_out(codec);
  705. return 0;
  706. }
  707. static void stac92xx_free(struct hda_codec *codec)
  708. {
  709. struct sigmatel_spec *spec = codec->spec;
  710. int i;
  711. if (! spec)
  712. return;
  713. if (spec->kctl_alloc) {
  714. for (i = 0; i < spec->num_kctl_used; i++)
  715. kfree(spec->kctl_alloc[i].name);
  716. kfree(spec->kctl_alloc);
  717. }
  718. kfree(spec);
  719. }
  720. static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
  721. unsigned int flag)
  722. {
  723. unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
  724. 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
  725. snd_hda_codec_write(codec, nid, 0,
  726. AC_VERB_SET_PIN_WIDGET_CONTROL,
  727. pin_ctl | flag);
  728. }
  729. static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
  730. unsigned int flag)
  731. {
  732. unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
  733. 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
  734. snd_hda_codec_write(codec, nid, 0,
  735. AC_VERB_SET_PIN_WIDGET_CONTROL,
  736. pin_ctl & ~flag);
  737. }
  738. static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
  739. {
  740. struct sigmatel_spec *spec = codec->spec;
  741. struct auto_pin_cfg *cfg = &spec->autocfg;
  742. int i, presence;
  743. if ((res >> 26) != STAC_HP_EVENT)
  744. return;
  745. presence = snd_hda_codec_read(codec, cfg->hp_pin, 0,
  746. AC_VERB_GET_PIN_SENSE, 0x00) >> 31;
  747. if (presence) {
  748. /* disable lineouts, enable hp */
  749. for (i = 0; i < cfg->line_outs; i++)
  750. stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
  751. AC_PINCTL_OUT_EN);
  752. stac92xx_set_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
  753. } else {
  754. /* enable lineouts, disable hp */
  755. for (i = 0; i < cfg->line_outs; i++)
  756. stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
  757. AC_PINCTL_OUT_EN);
  758. stac92xx_reset_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
  759. }
  760. }
  761. #ifdef CONFIG_PM
  762. static int stac92xx_resume(struct hda_codec *codec)
  763. {
  764. struct sigmatel_spec *spec = codec->spec;
  765. int i;
  766. stac92xx_init(codec);
  767. for (i = 0; i < spec->num_mixers; i++)
  768. snd_hda_resume_ctls(codec, spec->mixers[i]);
  769. if (spec->multiout.dig_out_nid)
  770. snd_hda_resume_spdif_out(codec);
  771. if (spec->dig_in_nid)
  772. snd_hda_resume_spdif_in(codec);
  773. return 0;
  774. }
  775. #endif
  776. static struct hda_codec_ops stac92xx_patch_ops = {
  777. .build_controls = stac92xx_build_controls,
  778. .build_pcms = stac92xx_build_pcms,
  779. .init = stac92xx_init,
  780. .free = stac92xx_free,
  781. .unsol_event = stac92xx_unsol_event,
  782. #ifdef CONFIG_PM
  783. .resume = stac92xx_resume,
  784. #endif
  785. };
  786. static int patch_stac9200(struct hda_codec *codec)
  787. {
  788. struct sigmatel_spec *spec;
  789. int err;
  790. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  791. if (spec == NULL)
  792. return -ENOMEM;
  793. codec->spec = spec;
  794. spec->board_config = snd_hda_check_board_config(codec, stac9200_cfg_tbl);
  795. if (spec->board_config < 0)
  796. snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC9200, using BIOS defaults\n");
  797. else {
  798. spec->num_pins = 8;
  799. spec->pin_nids = stac9200_pin_nids;
  800. spec->pin_configs = stac9200_brd_tbl[spec->board_config];
  801. stac92xx_set_config_regs(codec);
  802. }
  803. spec->multiout.max_channels = 2;
  804. spec->multiout.num_dacs = 1;
  805. spec->multiout.dac_nids = stac9200_dac_nids;
  806. spec->adc_nids = stac9200_adc_nids;
  807. spec->mux_nids = stac9200_mux_nids;
  808. spec->num_muxes = 1;
  809. spec->init = stac9200_core_init;
  810. spec->mixer = stac9200_mixer;
  811. err = stac9200_parse_auto_config(codec);
  812. if (err < 0) {
  813. stac92xx_free(codec);
  814. return err;
  815. }
  816. codec->patch_ops = stac92xx_patch_ops;
  817. return 0;
  818. }
  819. static int patch_stac922x(struct hda_codec *codec)
  820. {
  821. struct sigmatel_spec *spec;
  822. int err;
  823. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  824. if (spec == NULL)
  825. return -ENOMEM;
  826. codec->spec = spec;
  827. spec->board_config = snd_hda_check_board_config(codec, stac922x_cfg_tbl);
  828. if (spec->board_config < 0)
  829. snd_printdd(KERN_INFO "hda_codec: Unknown model for STAC922x, using BIOS defaults\n");
  830. else {
  831. spec->num_pins = 10;
  832. spec->pin_nids = stac922x_pin_nids;
  833. spec->pin_configs = stac922x_brd_tbl[spec->board_config];
  834. stac92xx_set_config_regs(codec);
  835. }
  836. spec->adc_nids = stac922x_adc_nids;
  837. spec->mux_nids = stac922x_mux_nids;
  838. spec->num_muxes = 2;
  839. spec->init = stac922x_core_init;
  840. spec->mixer = stac922x_mixer;
  841. spec->multiout.dac_nids = spec->dac_nids;
  842. err = stac922x_parse_auto_config(codec);
  843. if (err < 0) {
  844. stac92xx_free(codec);
  845. return err;
  846. }
  847. codec->patch_ops = stac92xx_patch_ops;
  848. return 0;
  849. }
  850. /*
  851. * patch entries
  852. */
  853. struct hda_codec_preset snd_hda_preset_sigmatel[] = {
  854. { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
  855. { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
  856. { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
  857. { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
  858. { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
  859. { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
  860. { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
  861. {} /* terminator */
  862. };