patch_sigmatel.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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@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. #undef STAC_TEST
  36. #define NUM_CONTROL_ALLOC 32
  37. #define STAC_HP_EVENT 0x37
  38. #define STAC_UNSOL_ENABLE (AC_USRSP_EN | STAC_HP_EVENT)
  39. struct sigmatel_spec {
  40. snd_kcontrol_new_t *mixers[4];
  41. unsigned int num_mixers;
  42. unsigned int surr_switch: 1;
  43. /* playback */
  44. struct hda_multi_out multiout;
  45. hda_nid_t dac_nids[4];
  46. /* capture */
  47. hda_nid_t *adc_nids;
  48. unsigned int num_adcs;
  49. hda_nid_t *mux_nids;
  50. unsigned int num_muxes;
  51. hda_nid_t dig_in_nid;
  52. #ifdef STAC_TEST
  53. /* pin widgets */
  54. hda_nid_t *pin_nids;
  55. unsigned int num_pins;
  56. unsigned int *pin_configs;
  57. #endif
  58. /* codec specific stuff */
  59. struct hda_verb *init;
  60. snd_kcontrol_new_t *mixer;
  61. /* capture source */
  62. struct hda_input_mux *input_mux;
  63. unsigned int cur_mux[2];
  64. /* channel mode */
  65. unsigned int num_ch_modes;
  66. unsigned int cur_ch_mode;
  67. struct hda_pcm pcm_rec[2]; /* PCM information */
  68. /* dynamic controls and input_mux */
  69. struct auto_pin_cfg autocfg;
  70. unsigned int num_kctl_alloc, num_kctl_used;
  71. snd_kcontrol_new_t *kctl_alloc;
  72. struct hda_input_mux private_imux;
  73. };
  74. static hda_nid_t stac9200_adc_nids[1] = {
  75. 0x03,
  76. };
  77. static hda_nid_t stac9200_mux_nids[1] = {
  78. 0x0c,
  79. };
  80. static hda_nid_t stac9200_dac_nids[1] = {
  81. 0x02,
  82. };
  83. static hda_nid_t stac922x_adc_nids[2] = {
  84. 0x06, 0x07,
  85. };
  86. static hda_nid_t stac922x_mux_nids[2] = {
  87. 0x12, 0x13,
  88. };
  89. #ifdef STAC_TEST
  90. static hda_nid_t stac9200_pin_nids[8] = {
  91. 0x08, 0x09, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
  92. };
  93. static hda_nid_t stac922x_pin_nids[10] = {
  94. 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
  95. 0x0f, 0x10, 0x11, 0x15, 0x1b,
  96. };
  97. #endif
  98. static int stac92xx_mux_enum_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *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(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *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(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *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 int stac922x_channel_modes[3] = {2, 6, 8};
  131. static int stac922x_ch_mode_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
  132. {
  133. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  134. struct sigmatel_spec *spec = codec->spec;
  135. uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
  136. uinfo->count = 1;
  137. uinfo->value.enumerated.items = spec->num_ch_modes;
  138. if (uinfo->value.enumerated.item >= uinfo->value.enumerated.items)
  139. uinfo->value.enumerated.item = uinfo->value.enumerated.items - 1;
  140. sprintf(uinfo->value.enumerated.name, "%dch",
  141. stac922x_channel_modes[uinfo->value.enumerated.item]);
  142. return 0;
  143. }
  144. static int stac922x_ch_mode_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  145. {
  146. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  147. struct sigmatel_spec *spec = codec->spec;
  148. ucontrol->value.enumerated.item[0] = spec->cur_ch_mode;
  149. return 0;
  150. }
  151. static int stac922x_ch_mode_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
  152. {
  153. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  154. struct sigmatel_spec *spec = codec->spec;
  155. if (ucontrol->value.enumerated.item[0] >= spec->num_ch_modes)
  156. ucontrol->value.enumerated.item[0] = spec->num_ch_modes;
  157. if (ucontrol->value.enumerated.item[0] == spec->cur_ch_mode &&
  158. ! codec->in_resume)
  159. return 0;
  160. spec->cur_ch_mode = ucontrol->value.enumerated.item[0];
  161. spec->multiout.max_channels = stac922x_channel_modes[spec->cur_ch_mode];
  162. return 1;
  163. }
  164. static snd_kcontrol_new_t stac9200_mixer[] = {
  165. HDA_CODEC_VOLUME("Master Playback Volume", 0xb, 0, HDA_OUTPUT),
  166. HDA_CODEC_MUTE("Master Playback Switch", 0xb, 0, HDA_OUTPUT),
  167. {
  168. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  169. .name = "Input Source",
  170. .count = 1,
  171. .info = stac92xx_mux_enum_info,
  172. .get = stac92xx_mux_enum_get,
  173. .put = stac92xx_mux_enum_put,
  174. },
  175. HDA_CODEC_VOLUME("Capture Volume", 0x0a, 0, HDA_OUTPUT),
  176. HDA_CODEC_MUTE("Capture Switch", 0x0a, 0, HDA_OUTPUT),
  177. HDA_CODEC_VOLUME("Capture Mux Volume", 0x0c, 0, HDA_OUTPUT),
  178. { } /* end */
  179. };
  180. /* This needs to be generated dynamically based on sequence */
  181. static snd_kcontrol_new_t stac922x_mixer[] = {
  182. {
  183. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  184. .name = "Input Source",
  185. .count = 1,
  186. .info = stac92xx_mux_enum_info,
  187. .get = stac92xx_mux_enum_get,
  188. .put = stac92xx_mux_enum_put,
  189. },
  190. HDA_CODEC_VOLUME("Capture Volume", 0x17, 0x0, HDA_INPUT),
  191. HDA_CODEC_MUTE("Capture Switch", 0x17, 0x0, HDA_INPUT),
  192. HDA_CODEC_VOLUME("Mux Capture Volume", 0x12, 0x0, HDA_OUTPUT),
  193. { } /* end */
  194. };
  195. static snd_kcontrol_new_t stac922x_ch_mode_mixer[] = {
  196. {
  197. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  198. .name = "Channel Mode",
  199. .info = stac922x_ch_mode_info,
  200. .get = stac922x_ch_mode_get,
  201. .put = stac922x_ch_mode_put,
  202. },
  203. { } /* end */
  204. };
  205. static int stac92xx_build_controls(struct hda_codec *codec)
  206. {
  207. struct sigmatel_spec *spec = codec->spec;
  208. int err;
  209. int i;
  210. err = snd_hda_add_new_ctls(codec, spec->mixer);
  211. if (err < 0)
  212. return err;
  213. for (i = 0; i < spec->num_mixers; i++) {
  214. err = snd_hda_add_new_ctls(codec, spec->mixers[i]);
  215. if (err < 0)
  216. return err;
  217. }
  218. if (spec->surr_switch) {
  219. err = snd_hda_add_new_ctls(codec, stac922x_ch_mode_mixer);
  220. if (err < 0)
  221. return err;
  222. }
  223. if (spec->multiout.dig_out_nid) {
  224. err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
  225. if (err < 0)
  226. return err;
  227. }
  228. if (spec->dig_in_nid) {
  229. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
  230. if (err < 0)
  231. return err;
  232. }
  233. return 0;
  234. }
  235. #ifdef STAC_TEST
  236. static unsigned int stac9200_pin_configs[8] = {
  237. 0x01c47010, 0x01447010, 0x0221401f, 0x01114010,
  238. 0x02a19020, 0x01a19021, 0x90100140, 0x01813122,
  239. };
  240. static unsigned int stac922x_pin_configs[10] = {
  241. 0x01014010, 0x01014011, 0x01014012, 0x0221401f,
  242. 0x01813122, 0x01014014, 0x01441030, 0x01c41030,
  243. 0x40000100, 0x40000100,
  244. };
  245. static void stac92xx_set_config_regs(struct hda_codec *codec)
  246. {
  247. int i;
  248. struct sigmatel_spec *spec = codec->spec;
  249. unsigned int pin_cfg;
  250. for (i=0; i < spec->num_pins; i++) {
  251. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  252. AC_VERB_SET_CONFIG_DEFAULT_BYTES_0,
  253. spec->pin_configs[i] & 0x000000ff);
  254. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  255. AC_VERB_SET_CONFIG_DEFAULT_BYTES_1,
  256. (spec->pin_configs[i] & 0x0000ff00) >> 8);
  257. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  258. AC_VERB_SET_CONFIG_DEFAULT_BYTES_2,
  259. (spec->pin_configs[i] & 0x00ff0000) >> 16);
  260. snd_hda_codec_write(codec, spec->pin_nids[i], 0,
  261. AC_VERB_SET_CONFIG_DEFAULT_BYTES_3,
  262. spec->pin_configs[i] >> 24);
  263. pin_cfg = snd_hda_codec_read(codec, spec->pin_nids[i], 0,
  264. AC_VERB_GET_CONFIG_DEFAULT,
  265. 0x00);
  266. printk("pin nid %2.2x pin config %8.8x\n", spec->pin_nids[i], pin_cfg);
  267. }
  268. }
  269. #endif
  270. /*
  271. * Analog playback callbacks
  272. */
  273. static int stac92xx_playback_pcm_open(struct hda_pcm_stream *hinfo,
  274. struct hda_codec *codec,
  275. snd_pcm_substream_t *substream)
  276. {
  277. struct sigmatel_spec *spec = codec->spec;
  278. return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
  279. }
  280. /*
  281. * set up the i/o for analog out
  282. * when the digital out is available, copy the front out to digital out, too.
  283. */
  284. static int stac92xx_multi_out_analog_prepare(struct hda_codec *codec, struct hda_multi_out *mout,
  285. unsigned int stream_tag,
  286. unsigned int format,
  287. snd_pcm_substream_t *substream)
  288. {
  289. hda_nid_t *nids = mout->dac_nids;
  290. int chs = substream->runtime->channels;
  291. int i;
  292. down(&codec->spdif_mutex);
  293. if (mout->dig_out_nid && mout->dig_out_used != HDA_DIG_EXCLUSIVE) {
  294. if (chs == 2 &&
  295. snd_hda_is_supported_format(codec, mout->dig_out_nid, format) &&
  296. ! (codec->spdif_status & IEC958_AES0_NONAUDIO)) {
  297. mout->dig_out_used = HDA_DIG_ANALOG_DUP;
  298. /* setup digital receiver */
  299. snd_hda_codec_setup_stream(codec, mout->dig_out_nid,
  300. stream_tag, 0, format);
  301. } else {
  302. mout->dig_out_used = 0;
  303. snd_hda_codec_setup_stream(codec, mout->dig_out_nid, 0, 0, 0);
  304. }
  305. }
  306. up(&codec->spdif_mutex);
  307. /* front */
  308. snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag, 0, format);
  309. if (mout->hp_nid)
  310. /* headphone out will just decode front left/right (stereo) */
  311. snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag, 0, format);
  312. /* surrounds */
  313. if (mout->max_channels > 2)
  314. for (i = 1; i < mout->num_dacs; i++) {
  315. if ((mout->max_channels == 6) && (i == 3))
  316. break;
  317. if (chs >= (i + 1) * 2) /* independent out */
  318. snd_hda_codec_setup_stream(codec, nids[i], stream_tag, i * 2,
  319. format);
  320. else /* copy front */
  321. snd_hda_codec_setup_stream(codec, nids[i], stream_tag, 0,
  322. format);
  323. }
  324. return 0;
  325. }
  326. static int stac92xx_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  327. struct hda_codec *codec,
  328. unsigned int stream_tag,
  329. unsigned int format,
  330. snd_pcm_substream_t *substream)
  331. {
  332. struct sigmatel_spec *spec = codec->spec;
  333. return stac92xx_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
  334. format, substream);
  335. }
  336. static int stac92xx_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  337. struct hda_codec *codec,
  338. snd_pcm_substream_t *substream)
  339. {
  340. struct sigmatel_spec *spec = codec->spec;
  341. return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  342. }
  343. /*
  344. * Digital playback callbacks
  345. */
  346. static int stac92xx_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  347. struct hda_codec *codec,
  348. snd_pcm_substream_t *substream)
  349. {
  350. struct sigmatel_spec *spec = codec->spec;
  351. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  352. }
  353. static int stac92xx_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  354. struct hda_codec *codec,
  355. snd_pcm_substream_t *substream)
  356. {
  357. struct sigmatel_spec *spec = codec->spec;
  358. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  359. }
  360. /*
  361. * Analog capture callbacks
  362. */
  363. static int stac92xx_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  364. struct hda_codec *codec,
  365. unsigned int stream_tag,
  366. unsigned int format,
  367. snd_pcm_substream_t *substream)
  368. {
  369. struct sigmatel_spec *spec = codec->spec;
  370. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
  371. stream_tag, 0, format);
  372. return 0;
  373. }
  374. static int stac92xx_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  375. struct hda_codec *codec,
  376. snd_pcm_substream_t *substream)
  377. {
  378. struct sigmatel_spec *spec = codec->spec;
  379. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
  380. return 0;
  381. }
  382. static struct hda_pcm_stream stac92xx_pcm_digital_playback = {
  383. .substreams = 1,
  384. .channels_min = 2,
  385. .channels_max = 2,
  386. /* NID is set in stac92xx_build_pcms */
  387. .ops = {
  388. .open = stac92xx_dig_playback_pcm_open,
  389. .close = stac92xx_dig_playback_pcm_close
  390. },
  391. };
  392. static struct hda_pcm_stream stac92xx_pcm_digital_capture = {
  393. .substreams = 1,
  394. .channels_min = 2,
  395. .channels_max = 2,
  396. /* NID is set in stac92xx_build_pcms */
  397. };
  398. static struct hda_pcm_stream stac92xx_pcm_analog_playback = {
  399. .substreams = 1,
  400. .channels_min = 2,
  401. .channels_max = 8,
  402. .nid = 0x02, /* NID to query formats and rates */
  403. .ops = {
  404. .open = stac92xx_playback_pcm_open,
  405. .prepare = stac92xx_playback_pcm_prepare,
  406. .cleanup = stac92xx_playback_pcm_cleanup
  407. },
  408. };
  409. static struct hda_pcm_stream stac92xx_pcm_analog_capture = {
  410. .substreams = 2,
  411. .channels_min = 2,
  412. .channels_max = 2,
  413. .nid = 0x06, /* NID to query formats and rates */
  414. .ops = {
  415. .prepare = stac92xx_capture_pcm_prepare,
  416. .cleanup = stac92xx_capture_pcm_cleanup
  417. },
  418. };
  419. static int stac92xx_build_pcms(struct hda_codec *codec)
  420. {
  421. struct sigmatel_spec *spec = codec->spec;
  422. struct hda_pcm *info = spec->pcm_rec;
  423. codec->num_pcms = 1;
  424. codec->pcm_info = info;
  425. info->name = "STAC92xx Analog";
  426. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_analog_playback;
  427. info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_analog_capture;
  428. if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
  429. codec->num_pcms++;
  430. info++;
  431. info->name = "STAC92xx Digital";
  432. if (spec->multiout.dig_out_nid) {
  433. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = stac92xx_pcm_digital_playback;
  434. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
  435. }
  436. if (spec->dig_in_nid) {
  437. info->stream[SNDRV_PCM_STREAM_CAPTURE] = stac92xx_pcm_digital_capture;
  438. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
  439. }
  440. }
  441. return 0;
  442. }
  443. enum {
  444. STAC_CTL_WIDGET_VOL,
  445. STAC_CTL_WIDGET_MUTE,
  446. };
  447. static snd_kcontrol_new_t stac92xx_control_templates[] = {
  448. HDA_CODEC_VOLUME(NULL, 0, 0, 0),
  449. HDA_CODEC_MUTE(NULL, 0, 0, 0),
  450. };
  451. /* add dynamic controls */
  452. static int stac92xx_add_control(struct sigmatel_spec *spec, int type, const char *name, unsigned long val)
  453. {
  454. snd_kcontrol_new_t *knew;
  455. if (spec->num_kctl_used >= spec->num_kctl_alloc) {
  456. int num = spec->num_kctl_alloc + NUM_CONTROL_ALLOC;
  457. knew = kcalloc(num + 1, sizeof(*knew), GFP_KERNEL); /* array + terminator */
  458. if (! knew)
  459. return -ENOMEM;
  460. if (spec->kctl_alloc) {
  461. memcpy(knew, spec->kctl_alloc, sizeof(*knew) * spec->num_kctl_alloc);
  462. kfree(spec->kctl_alloc);
  463. }
  464. spec->kctl_alloc = knew;
  465. spec->num_kctl_alloc = num;
  466. }
  467. knew = &spec->kctl_alloc[spec->num_kctl_used];
  468. *knew = stac92xx_control_templates[type];
  469. knew->name = kstrdup(name, GFP_KERNEL);
  470. if (! knew->name)
  471. return -ENOMEM;
  472. knew->private_value = val;
  473. spec->num_kctl_used++;
  474. return 0;
  475. }
  476. /* fill in the dac_nids table from the parsed pin configuration */
  477. static int stac92xx_auto_fill_dac_nids(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
  478. {
  479. struct sigmatel_spec *spec = codec->spec;
  480. hda_nid_t nid;
  481. int i;
  482. /* check the pins hardwired to audio widget */
  483. for (i = 0; i < cfg->line_outs; i++) {
  484. nid = cfg->line_out_pins[i];
  485. spec->multiout.dac_nids[i] = snd_hda_codec_read(codec, nid, 0,
  486. AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
  487. }
  488. spec->multiout.num_dacs = cfg->line_outs;
  489. return 0;
  490. }
  491. /* add playback controls from the parsed DAC table */
  492. static int stac92xx_auto_create_multi_out_ctls(struct sigmatel_spec *spec, const struct auto_pin_cfg *cfg)
  493. {
  494. char name[32];
  495. static const char *chname[4] = { "Front", "Surround", NULL /*CLFE*/, "Side" };
  496. hda_nid_t nid;
  497. int i, err;
  498. for (i = 0; i < cfg->line_outs; i++) {
  499. if (! spec->multiout.dac_nids[i])
  500. continue;
  501. nid = spec->multiout.dac_nids[i];
  502. if (i == 2) {
  503. /* Center/LFE */
  504. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Center Playback Volume",
  505. HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
  506. return err;
  507. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "LFE Playback Volume",
  508. HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
  509. return err;
  510. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Center Playback Switch",
  511. HDA_COMPOSE_AMP_VAL(nid, 1, 0, HDA_OUTPUT))) < 0)
  512. return err;
  513. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "LFE Playback Switch",
  514. HDA_COMPOSE_AMP_VAL(nid, 2, 0, HDA_OUTPUT))) < 0)
  515. return err;
  516. } else {
  517. sprintf(name, "%s Playback Volume", chname[i]);
  518. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, name,
  519. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  520. return err;
  521. sprintf(name, "%s Playback Switch", chname[i]);
  522. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, name,
  523. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  524. return err;
  525. }
  526. }
  527. return 0;
  528. }
  529. /* add playback controls for HP output */
  530. static int stac92xx_auto_create_hp_ctls(struct hda_codec *codec, struct auto_pin_cfg *cfg)
  531. {
  532. struct sigmatel_spec *spec = codec->spec;
  533. hda_nid_t pin = cfg->hp_pin;
  534. hda_nid_t nid;
  535. int i, err;
  536. unsigned int wid_caps;
  537. if (! pin)
  538. return 0;
  539. wid_caps = snd_hda_param_read(codec, pin, AC_PAR_AUDIO_WIDGET_CAP);
  540. if (wid_caps & AC_WCAP_UNSOL_CAP)
  541. /* Enable unsolicited responses on the HP widget */
  542. snd_hda_codec_write(codec, pin, 0,
  543. AC_VERB_SET_UNSOLICITED_ENABLE,
  544. STAC_UNSOL_ENABLE);
  545. nid = snd_hda_codec_read(codec, pin, 0, AC_VERB_GET_CONNECT_LIST, 0) & 0xff;
  546. for (i = 0; i < cfg->line_outs; i++) {
  547. if (! spec->multiout.dac_nids[i])
  548. continue;
  549. if (spec->multiout.dac_nids[i] == nid)
  550. return 0;
  551. }
  552. spec->multiout.hp_nid = nid;
  553. /* control HP volume/switch on the output mixer amp */
  554. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_VOL, "Headphone Playback Volume",
  555. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  556. return err;
  557. if ((err = stac92xx_add_control(spec, STAC_CTL_WIDGET_MUTE, "Headphone Playback Switch",
  558. HDA_COMPOSE_AMP_VAL(nid, 3, 0, HDA_OUTPUT))) < 0)
  559. return err;
  560. return 0;
  561. }
  562. /* create playback/capture controls for input pins */
  563. static int stac92xx_auto_create_analog_input_ctls(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
  564. {
  565. struct sigmatel_spec *spec = codec->spec;
  566. static char *labels[AUTO_PIN_LAST] = {
  567. "Mic", "Front Mic", "Line", "Front Line", "CD", "Aux"
  568. };
  569. struct hda_input_mux *imux = &spec->private_imux;
  570. hda_nid_t con_lst[HDA_MAX_NUM_INPUTS];
  571. int i, j, k;
  572. for (i = 0; i < AUTO_PIN_LAST; i++) {
  573. int index = -1;
  574. if (cfg->input_pins[i]) {
  575. imux->items[imux->num_items].label = labels[i];
  576. for (j=0; j<spec->num_muxes; j++) {
  577. int num_cons = snd_hda_get_connections(codec, spec->mux_nids[j], con_lst, HDA_MAX_NUM_INPUTS);
  578. for (k=0; k<num_cons; k++)
  579. if (con_lst[k] == cfg->input_pins[i]) {
  580. index = k;
  581. break;
  582. }
  583. if (index >= 0)
  584. break;
  585. }
  586. imux->items[imux->num_items].index = index;
  587. imux->num_items++;
  588. }
  589. }
  590. return 0;
  591. }
  592. static void stac92xx_auto_set_pinctl(struct hda_codec *codec, hda_nid_t nid, int pin_type)
  593. {
  594. snd_hda_codec_write(codec, nid, 0, AC_VERB_SET_PIN_WIDGET_CONTROL, pin_type);
  595. }
  596. static void stac92xx_auto_init_multi_out(struct hda_codec *codec)
  597. {
  598. struct sigmatel_spec *spec = codec->spec;
  599. int i;
  600. for (i = 0; i < spec->autocfg.line_outs; i++) {
  601. hda_nid_t nid = spec->autocfg.line_out_pins[i];
  602. stac92xx_auto_set_pinctl(codec, nid, AC_PINCTL_OUT_EN);
  603. }
  604. }
  605. static void stac92xx_auto_init_hp_out(struct hda_codec *codec)
  606. {
  607. struct sigmatel_spec *spec = codec->spec;
  608. hda_nid_t pin;
  609. pin = spec->autocfg.hp_pin;
  610. if (pin) /* connect to front */
  611. stac92xx_auto_set_pinctl(codec, pin, AC_PINCTL_OUT_EN | AC_PINCTL_HP_EN);
  612. }
  613. static int stac922x_parse_auto_config(struct hda_codec *codec)
  614. {
  615. struct sigmatel_spec *spec = codec->spec;
  616. int err;
  617. if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg)) < 0)
  618. return err;
  619. if ((err = stac92xx_auto_fill_dac_nids(codec, &spec->autocfg)) < 0)
  620. return err;
  621. if (! spec->autocfg.line_outs && ! spec->autocfg.hp_pin)
  622. return 0; /* can't find valid pin config */
  623. if ((err = stac92xx_auto_create_multi_out_ctls(spec, &spec->autocfg)) < 0 ||
  624. (err = stac92xx_auto_create_hp_ctls(codec, &spec->autocfg)) < 0 ||
  625. (err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
  626. return err;
  627. spec->multiout.max_channels = spec->multiout.num_dacs * 2;
  628. if (spec->multiout.max_channels > 2) {
  629. spec->surr_switch = 1;
  630. spec->cur_ch_mode = 1;
  631. spec->num_ch_modes = 2;
  632. if (spec->multiout.max_channels == 8) {
  633. spec->cur_ch_mode++;
  634. spec->num_ch_modes++;
  635. }
  636. }
  637. if (spec->autocfg.dig_out_pin) {
  638. spec->multiout.dig_out_nid = 0x08;
  639. stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_out_pin, AC_PINCTL_OUT_EN);
  640. }
  641. if (spec->autocfg.dig_in_pin) {
  642. spec->dig_in_nid = 0x09;
  643. stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_in_pin, AC_PINCTL_IN_EN);
  644. }
  645. if (spec->kctl_alloc)
  646. spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
  647. spec->input_mux = &spec->private_imux;
  648. return 1;
  649. }
  650. static int stac9200_parse_auto_config(struct hda_codec *codec)
  651. {
  652. struct sigmatel_spec *spec = codec->spec;
  653. int err;
  654. if ((err = snd_hda_parse_pin_def_config(codec, &spec->autocfg)) < 0)
  655. return err;
  656. if ((err = stac92xx_auto_create_analog_input_ctls(codec, &spec->autocfg)) < 0)
  657. return err;
  658. if (spec->autocfg.dig_out_pin) {
  659. spec->multiout.dig_out_nid = 0x05;
  660. stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_out_pin, AC_PINCTL_OUT_EN);
  661. }
  662. if (spec->autocfg.dig_in_pin) {
  663. spec->dig_in_nid = 0x04;
  664. stac92xx_auto_set_pinctl(codec, spec->autocfg.dig_in_pin, AC_PINCTL_IN_EN);
  665. }
  666. if (spec->kctl_alloc)
  667. spec->mixers[spec->num_mixers++] = spec->kctl_alloc;
  668. spec->input_mux = &spec->private_imux;
  669. return 1;
  670. }
  671. static int stac92xx_init_pstate(struct hda_codec *codec)
  672. {
  673. hda_nid_t nid, nid_start;
  674. int nodes;
  675. snd_hda_codec_write(codec, 0x01, 0, AC_VERB_SET_POWER_STATE, 0x00);
  676. nodes = snd_hda_get_sub_nodes(codec, codec->afg, &nid_start);
  677. for (nid = nid_start; nid < nodes + nid_start; nid++) {
  678. unsigned int wid_caps = snd_hda_param_read(codec, nid,
  679. AC_PAR_AUDIO_WIDGET_CAP);
  680. if (wid_caps & AC_WCAP_POWER)
  681. snd_hda_codec_write(codec, nid, 0,
  682. AC_VERB_SET_POWER_STATE, 0x00);
  683. }
  684. mdelay(100);
  685. return 0;
  686. }
  687. static int stac92xx_init(struct hda_codec *codec)
  688. {
  689. struct sigmatel_spec *spec = codec->spec;
  690. stac92xx_init_pstate(codec);
  691. snd_hda_sequence_write(codec, spec->init);
  692. stac92xx_auto_init_multi_out(codec);
  693. stac92xx_auto_init_hp_out(codec);
  694. return 0;
  695. }
  696. static void stac92xx_free(struct hda_codec *codec)
  697. {
  698. struct sigmatel_spec *spec = codec->spec;
  699. int i;
  700. if (! spec)
  701. return;
  702. if (spec->kctl_alloc) {
  703. for (i = 0; i < spec->num_kctl_used; i++)
  704. kfree(spec->kctl_alloc[i].name);
  705. kfree(spec->kctl_alloc);
  706. }
  707. kfree(spec);
  708. }
  709. static void stac92xx_set_pinctl(struct hda_codec *codec, hda_nid_t nid,
  710. unsigned int flag)
  711. {
  712. unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
  713. 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
  714. snd_hda_codec_write(codec, nid, 0,
  715. AC_VERB_SET_PIN_WIDGET_CONTROL,
  716. pin_ctl | flag);
  717. }
  718. static void stac92xx_reset_pinctl(struct hda_codec *codec, hda_nid_t nid,
  719. unsigned int flag)
  720. {
  721. unsigned int pin_ctl = snd_hda_codec_read(codec, nid,
  722. 0, AC_VERB_GET_PIN_WIDGET_CONTROL, 0x00);
  723. snd_hda_codec_write(codec, nid, 0,
  724. AC_VERB_SET_PIN_WIDGET_CONTROL,
  725. pin_ctl & ~flag);
  726. }
  727. static void stac92xx_unsol_event(struct hda_codec *codec, unsigned int res)
  728. {
  729. struct sigmatel_spec *spec = codec->spec;
  730. struct auto_pin_cfg *cfg = &spec->autocfg;
  731. int i, presence;
  732. if ((res >> 26) != STAC_HP_EVENT)
  733. return;
  734. presence = snd_hda_codec_read(codec, cfg->hp_pin, 0,
  735. AC_VERB_GET_PIN_SENSE, 0x00) >> 31;
  736. if (presence) {
  737. /* disable lineouts, enable hp */
  738. for (i = 0; i < cfg->line_outs; i++)
  739. stac92xx_reset_pinctl(codec, cfg->line_out_pins[i],
  740. AC_PINCTL_OUT_EN);
  741. stac92xx_set_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
  742. } else {
  743. /* enable lineouts, disable hp */
  744. for (i = 0; i < cfg->line_outs; i++)
  745. stac92xx_set_pinctl(codec, cfg->line_out_pins[i],
  746. AC_PINCTL_OUT_EN);
  747. stac92xx_reset_pinctl(codec, cfg->hp_pin, AC_PINCTL_OUT_EN);
  748. }
  749. }
  750. #ifdef CONFIG_PM
  751. static int stac92xx_resume(struct hda_codec *codec)
  752. {
  753. struct sigmatel_spec *spec = codec->spec;
  754. int i;
  755. stac92xx_init(codec);
  756. for (i = 0; i < spec->num_mixers; i++)
  757. snd_hda_resume_ctls(codec, spec->mixers[i]);
  758. if (spec->multiout.dig_out_nid)
  759. snd_hda_resume_spdif_out(codec);
  760. if (spec->dig_in_nid)
  761. snd_hda_resume_spdif_in(codec);
  762. return 0;
  763. }
  764. #endif
  765. static struct hda_codec_ops stac92xx_patch_ops = {
  766. .build_controls = stac92xx_build_controls,
  767. .build_pcms = stac92xx_build_pcms,
  768. .init = stac92xx_init,
  769. .free = stac92xx_free,
  770. .unsol_event = stac92xx_unsol_event,
  771. #ifdef CONFIG_PM
  772. .resume = stac92xx_resume,
  773. #endif
  774. };
  775. static int patch_stac9200(struct hda_codec *codec)
  776. {
  777. struct sigmatel_spec *spec;
  778. int err;
  779. spec = kcalloc(1, sizeof(*spec), GFP_KERNEL);
  780. if (spec == NULL)
  781. return -ENOMEM;
  782. codec->spec = spec;
  783. #ifdef STAC_TEST
  784. spec->pin_nids = stac9200_pin_nids;
  785. spec->num_pins = 8;
  786. spec->pin_configs = stac9200_pin_configs;
  787. stac92xx_set_config_regs(codec);
  788. #endif
  789. spec->multiout.max_channels = 2;
  790. spec->multiout.num_dacs = 1;
  791. spec->multiout.dac_nids = stac9200_dac_nids;
  792. spec->adc_nids = stac9200_adc_nids;
  793. spec->mux_nids = stac9200_mux_nids;
  794. spec->num_muxes = 1;
  795. spec->init = stac9200_core_init;
  796. spec->mixer = stac9200_mixer;
  797. err = stac9200_parse_auto_config(codec);
  798. if (err < 0) {
  799. stac92xx_free(codec);
  800. return err;
  801. }
  802. codec->patch_ops = stac92xx_patch_ops;
  803. return 0;
  804. }
  805. static int patch_stac922x(struct hda_codec *codec)
  806. {
  807. struct sigmatel_spec *spec;
  808. int err;
  809. spec = kcalloc(1, sizeof(*spec), GFP_KERNEL);
  810. if (spec == NULL)
  811. return -ENOMEM;
  812. codec->spec = spec;
  813. #ifdef STAC_TEST
  814. spec->num_pins = 10;
  815. spec->pin_nids = stac922x_pin_nids;
  816. spec->pin_configs = stac922x_pin_configs;
  817. stac92xx_set_config_regs(codec);
  818. #endif
  819. spec->adc_nids = stac922x_adc_nids;
  820. spec->mux_nids = stac922x_mux_nids;
  821. spec->num_muxes = 2;
  822. spec->init = stac922x_core_init;
  823. spec->mixer = stac922x_mixer;
  824. spec->multiout.dac_nids = spec->dac_nids;
  825. err = stac922x_parse_auto_config(codec);
  826. if (err < 0) {
  827. stac92xx_free(codec);
  828. return err;
  829. }
  830. codec->patch_ops = stac92xx_patch_ops;
  831. return 0;
  832. }
  833. /*
  834. * patch entries
  835. */
  836. struct hda_codec_preset snd_hda_preset_sigmatel[] = {
  837. { .id = 0x83847690, .name = "STAC9200", .patch = patch_stac9200 },
  838. { .id = 0x83847882, .name = "STAC9220 A1", .patch = patch_stac922x },
  839. { .id = 0x83847680, .name = "STAC9221 A1", .patch = patch_stac922x },
  840. { .id = 0x83847880, .name = "STAC9220 A2", .patch = patch_stac922x },
  841. { .id = 0x83847681, .name = "STAC9220D/9223D A2", .patch = patch_stac922x },
  842. { .id = 0x83847682, .name = "STAC9221 A2", .patch = patch_stac922x },
  843. { .id = 0x83847683, .name = "STAC9221D A2", .patch = patch_stac922x },
  844. {} /* terminator */
  845. };