patch_cmedia.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /*
  2. * Universal Interface for Intel High Definition Audio Codec
  3. *
  4. * HD audio interface patch for C-Media CMI9880
  5. *
  6. * Copyright (c) 2004 Takashi Iwai <tiwai@suse.de>
  7. *
  8. *
  9. * This driver is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This driver is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <sound/driver.h>
  24. #include <linux/init.h>
  25. #include <linux/delay.h>
  26. #include <linux/slab.h>
  27. #include <linux/pci.h>
  28. #include <sound/core.h>
  29. #include "hda_codec.h"
  30. #include "hda_local.h"
  31. #define NUM_PINS 11
  32. /* board config type */
  33. enum {
  34. CMI_MINIMAL, /* back 3-jack */
  35. CMI_MIN_FP, /* back 3-jack + front-panel 2-jack */
  36. CMI_FULL, /* back 6-jack + front-panel 2-jack */
  37. CMI_FULL_DIG, /* back 6-jack + front-panel 2-jack + digital I/O */
  38. CMI_ALLOUT, /* back 5-jack + front-panel 2-jack + digital out */
  39. CMI_AUTO, /* let driver guess it */
  40. CMI_MODELS
  41. };
  42. struct cmi_spec {
  43. int board_config;
  44. unsigned int no_line_in: 1; /* no line-in (5-jack) */
  45. unsigned int front_panel: 1; /* has front-panel 2-jack */
  46. /* playback */
  47. struct hda_multi_out multiout;
  48. hda_nid_t dac_nids[4]; /* NID for each DAC */
  49. int num_dacs;
  50. /* capture */
  51. hda_nid_t *adc_nids;
  52. hda_nid_t dig_in_nid;
  53. /* capture source */
  54. const struct hda_input_mux *input_mux;
  55. unsigned int cur_mux[2];
  56. /* channel mode */
  57. int num_channel_modes;
  58. const struct hda_channel_mode *channel_modes;
  59. struct hda_pcm pcm_rec[2]; /* PCM information */
  60. /* pin deafault configuration */
  61. hda_nid_t pin_nid[NUM_PINS];
  62. unsigned int def_conf[NUM_PINS];
  63. unsigned int pin_def_confs;
  64. /* multichannel pins */
  65. hda_nid_t multich_pin[4]; /* max 8-channel */
  66. struct hda_verb multi_init[9]; /* 2 verbs for each pin + terminator */
  67. };
  68. /*
  69. * input MUX
  70. */
  71. static int cmi_mux_enum_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  72. {
  73. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  74. struct cmi_spec *spec = codec->spec;
  75. return snd_hda_input_mux_info(spec->input_mux, uinfo);
  76. }
  77. static int cmi_mux_enum_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  78. {
  79. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  80. struct cmi_spec *spec = codec->spec;
  81. unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  82. ucontrol->value.enumerated.item[0] = spec->cur_mux[adc_idx];
  83. return 0;
  84. }
  85. static int cmi_mux_enum_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  86. {
  87. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  88. struct cmi_spec *spec = codec->spec;
  89. unsigned int adc_idx = snd_ctl_get_ioffidx(kcontrol, &ucontrol->id);
  90. return snd_hda_input_mux_put(codec, spec->input_mux, ucontrol,
  91. spec->adc_nids[adc_idx], &spec->cur_mux[adc_idx]);
  92. }
  93. /*
  94. * shared line-in, mic for surrounds
  95. */
  96. /* 3-stack / 2 channel */
  97. static struct hda_verb cmi9880_ch2_init[] = {
  98. /* set line-in PIN for input */
  99. { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
  100. /* set mic PIN for input, also enable vref */
  101. { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
  102. /* route front PCM (DAC1) to HP */
  103. { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
  104. {}
  105. };
  106. /* 3-stack / 6 channel */
  107. static struct hda_verb cmi9880_ch6_init[] = {
  108. /* set line-in PIN for output */
  109. { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
  110. /* set mic PIN for output */
  111. { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
  112. /* route front PCM (DAC1) to HP */
  113. { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
  114. {}
  115. };
  116. /* 3-stack+front / 8 channel */
  117. static struct hda_verb cmi9880_ch8_init[] = {
  118. /* set line-in PIN for output */
  119. { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
  120. /* set mic PIN for output */
  121. { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_OUT },
  122. /* route rear-surround PCM (DAC4) to HP */
  123. { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x03 },
  124. {}
  125. };
  126. static struct hda_channel_mode cmi9880_channel_modes[3] = {
  127. { 2, cmi9880_ch2_init },
  128. { 6, cmi9880_ch6_init },
  129. { 8, cmi9880_ch8_init },
  130. };
  131. static int cmi_ch_mode_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
  132. {
  133. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  134. struct cmi_spec *spec = codec->spec;
  135. return snd_hda_ch_mode_info(codec, uinfo, spec->channel_modes,
  136. spec->num_channel_modes);
  137. }
  138. static int cmi_ch_mode_get(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  139. {
  140. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  141. struct cmi_spec *spec = codec->spec;
  142. return snd_hda_ch_mode_get(codec, ucontrol, spec->channel_modes,
  143. spec->num_channel_modes, spec->multiout.max_channels);
  144. }
  145. static int cmi_ch_mode_put(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
  146. {
  147. struct hda_codec *codec = snd_kcontrol_chip(kcontrol);
  148. struct cmi_spec *spec = codec->spec;
  149. return snd_hda_ch_mode_put(codec, ucontrol, spec->channel_modes,
  150. spec->num_channel_modes, &spec->multiout.max_channels);
  151. }
  152. /*
  153. */
  154. static struct snd_kcontrol_new cmi9880_basic_mixer[] = {
  155. /* CMI9880 has no playback volumes! */
  156. HDA_CODEC_MUTE("PCM Playback Switch", 0x03, 0x0, HDA_OUTPUT), /* front */
  157. HDA_CODEC_MUTE("Surround Playback Switch", 0x04, 0x0, HDA_OUTPUT),
  158. HDA_CODEC_MUTE_MONO("Center Playback Switch", 0x05, 1, 0x0, HDA_OUTPUT),
  159. HDA_CODEC_MUTE_MONO("LFE Playback Switch", 0x05, 2, 0x0, HDA_OUTPUT),
  160. HDA_CODEC_MUTE("Side Playback Switch", 0x06, 0x0, HDA_OUTPUT),
  161. {
  162. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  163. /* The multiple "Capture Source" controls confuse alsamixer
  164. * So call somewhat different..
  165. * FIXME: the controls appear in the "playback" view!
  166. */
  167. /* .name = "Capture Source", */
  168. .name = "Input Source",
  169. .count = 2,
  170. .info = cmi_mux_enum_info,
  171. .get = cmi_mux_enum_get,
  172. .put = cmi_mux_enum_put,
  173. },
  174. HDA_CODEC_VOLUME("Capture Volume", 0x08, 0, HDA_INPUT),
  175. HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x09, 0, HDA_INPUT),
  176. HDA_CODEC_MUTE("Capture Switch", 0x08, 0, HDA_INPUT),
  177. HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x09, 0, HDA_INPUT),
  178. HDA_CODEC_VOLUME("PC Speaker Playback Volume", 0x23, 0, HDA_OUTPUT),
  179. HDA_CODEC_MUTE("PC Speaker Playback Switch", 0x23, 0, HDA_OUTPUT),
  180. { } /* end */
  181. };
  182. /*
  183. * shared I/O pins
  184. */
  185. static struct snd_kcontrol_new cmi9880_ch_mode_mixer[] = {
  186. {
  187. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  188. .name = "Channel Mode",
  189. .info = cmi_ch_mode_info,
  190. .get = cmi_ch_mode_get,
  191. .put = cmi_ch_mode_put,
  192. },
  193. { } /* end */
  194. };
  195. /* AUD-in selections:
  196. * 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x1f 0x20
  197. */
  198. static struct hda_input_mux cmi9880_basic_mux = {
  199. .num_items = 4,
  200. .items = {
  201. { "Front Mic", 0x5 },
  202. { "Rear Mic", 0x2 },
  203. { "Line", 0x1 },
  204. { "CD", 0x7 },
  205. }
  206. };
  207. static struct hda_input_mux cmi9880_no_line_mux = {
  208. .num_items = 3,
  209. .items = {
  210. { "Front Mic", 0x5 },
  211. { "Rear Mic", 0x2 },
  212. { "CD", 0x7 },
  213. }
  214. };
  215. /* front, rear, clfe, rear_surr */
  216. static hda_nid_t cmi9880_dac_nids[4] = {
  217. 0x03, 0x04, 0x05, 0x06
  218. };
  219. /* ADC0, ADC1 */
  220. static hda_nid_t cmi9880_adc_nids[2] = {
  221. 0x08, 0x09
  222. };
  223. #define CMI_DIG_OUT_NID 0x07
  224. #define CMI_DIG_IN_NID 0x0a
  225. /*
  226. */
  227. static struct hda_verb cmi9880_basic_init[] = {
  228. /* port-D for line out (rear panel) */
  229. { 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  230. /* port-E for HP out (front panel) */
  231. { 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  232. /* route front PCM to HP */
  233. { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
  234. /* port-A for surround (rear panel) */
  235. { 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  236. /* port-G for CLFE (rear panel) */
  237. { 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  238. { 0x1f, AC_VERB_SET_CONNECT_SEL, 0x02 },
  239. /* port-H for side (rear panel) */
  240. { 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  241. { 0x20, AC_VERB_SET_CONNECT_SEL, 0x01 },
  242. /* port-C for line-in (rear panel) */
  243. { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
  244. /* port-B for mic-in (rear panel) with vref */
  245. { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
  246. /* port-F for mic-in (front panel) with vref */
  247. { 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
  248. /* CD-in */
  249. { 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
  250. /* route front mic to ADC1/2 */
  251. { 0x08, AC_VERB_SET_CONNECT_SEL, 0x05 },
  252. { 0x09, AC_VERB_SET_CONNECT_SEL, 0x05 },
  253. {} /* terminator */
  254. };
  255. static struct hda_verb cmi9880_allout_init[] = {
  256. /* port-D for line out (rear panel) */
  257. { 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  258. /* port-E for HP out (front panel) */
  259. { 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  260. /* route front PCM to HP */
  261. { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
  262. /* port-A for side (rear panel) */
  263. { 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  264. /* port-G for CLFE (rear panel) */
  265. { 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  266. { 0x1f, AC_VERB_SET_CONNECT_SEL, 0x02 },
  267. /* port-H for side (rear panel) */
  268. { 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  269. { 0x20, AC_VERB_SET_CONNECT_SEL, 0x01 },
  270. /* port-C for surround (rear panel) */
  271. { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  272. /* port-B for mic-in (rear panel) with vref */
  273. { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
  274. /* port-F for mic-in (front panel) with vref */
  275. { 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
  276. /* CD-in */
  277. { 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
  278. /* route front mic to ADC1/2 */
  279. { 0x08, AC_VERB_SET_CONNECT_SEL, 0x05 },
  280. { 0x09, AC_VERB_SET_CONNECT_SEL, 0x05 },
  281. {} /* terminator */
  282. };
  283. /*
  284. */
  285. static int cmi9880_build_controls(struct hda_codec *codec)
  286. {
  287. struct cmi_spec *spec = codec->spec;
  288. int err;
  289. err = snd_hda_add_new_ctls(codec, cmi9880_basic_mixer);
  290. if (err < 0)
  291. return err;
  292. if (spec->channel_modes) {
  293. err = snd_hda_add_new_ctls(codec, cmi9880_ch_mode_mixer);
  294. if (err < 0)
  295. return err;
  296. }
  297. if (spec->multiout.dig_out_nid) {
  298. err = snd_hda_create_spdif_out_ctls(codec, spec->multiout.dig_out_nid);
  299. if (err < 0)
  300. return err;
  301. }
  302. if (spec->dig_in_nid) {
  303. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
  304. if (err < 0)
  305. return err;
  306. }
  307. return 0;
  308. }
  309. /* fill in the multi_dac_nids table, which will decide
  310. which audio widget to use for each channel */
  311. static int cmi9880_fill_multi_dac_nids(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
  312. {
  313. struct cmi_spec *spec = codec->spec;
  314. hda_nid_t nid;
  315. int assigned[4];
  316. int i, j;
  317. /* clear the table, only one c-media dac assumed here */
  318. memset(spec->dac_nids, 0, sizeof(spec->dac_nids));
  319. memset(assigned, 0, sizeof(assigned));
  320. /* check the pins we found */
  321. for (i = 0; i < cfg->line_outs; i++) {
  322. nid = cfg->line_out_pins[i];
  323. /* nid 0x0b~0x0e is hardwired to audio widget 0x3~0x6 */
  324. if (nid >= 0x0b && nid <= 0x0e) {
  325. spec->dac_nids[i] = (nid - 0x0b) + 0x03;
  326. assigned[nid - 0x0b] = 1;
  327. }
  328. }
  329. /* left pin can be connect to any audio widget */
  330. for (i = 0; i < cfg->line_outs; i++) {
  331. nid = cfg->line_out_pins[i];
  332. if (nid <= 0x0e)
  333. continue;
  334. /* search for an empty channel */
  335. for (j = 0; j < cfg->line_outs; j++) {
  336. if (! assigned[j]) {
  337. spec->dac_nids[i] = j + 0x03;
  338. assigned[j] = 1;
  339. break;
  340. }
  341. }
  342. }
  343. spec->num_dacs = cfg->line_outs;
  344. return 0;
  345. }
  346. /* create multi_init table, which is used for multichannel initialization */
  347. static int cmi9880_fill_multi_init(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
  348. {
  349. struct cmi_spec *spec = codec->spec;
  350. hda_nid_t nid;
  351. int i, j, k, len;
  352. /* clear the table, only one c-media dac assumed here */
  353. memset(spec->multi_init, 0, sizeof(spec->multi_init));
  354. for (j = 0, i = 0; i < cfg->line_outs; i++) {
  355. hda_nid_t conn[4];
  356. nid = cfg->line_out_pins[i];
  357. /* set as output */
  358. spec->multi_init[j].nid = nid;
  359. spec->multi_init[j].verb = AC_VERB_SET_PIN_WIDGET_CONTROL;
  360. spec->multi_init[j].param = PIN_OUT;
  361. j++;
  362. if (nid > 0x0e) {
  363. /* set connection */
  364. spec->multi_init[j].nid = nid;
  365. spec->multi_init[j].verb = AC_VERB_SET_CONNECT_SEL;
  366. spec->multi_init[j].param = 0;
  367. /* find the index in connect list */
  368. len = snd_hda_get_connections(codec, nid, conn, 4);
  369. for (k = 0; k < len; k++)
  370. if (conn[k] == spec->dac_nids[i]) {
  371. spec->multi_init[j].param = k;
  372. break;
  373. }
  374. j++;
  375. }
  376. }
  377. return 0;
  378. }
  379. static int cmi9880_init(struct hda_codec *codec)
  380. {
  381. struct cmi_spec *spec = codec->spec;
  382. if (spec->board_config == CMI_ALLOUT)
  383. snd_hda_sequence_write(codec, cmi9880_allout_init);
  384. else
  385. snd_hda_sequence_write(codec, cmi9880_basic_init);
  386. if (spec->board_config == CMI_AUTO)
  387. snd_hda_sequence_write(codec, spec->multi_init);
  388. return 0;
  389. }
  390. #ifdef CONFIG_PM
  391. /*
  392. * resume
  393. */
  394. static int cmi9880_resume(struct hda_codec *codec)
  395. {
  396. struct cmi_spec *spec = codec->spec;
  397. cmi9880_init(codec);
  398. snd_hda_resume_ctls(codec, cmi9880_basic_mixer);
  399. if (spec->channel_modes)
  400. snd_hda_resume_ctls(codec, cmi9880_ch_mode_mixer);
  401. if (spec->multiout.dig_out_nid)
  402. snd_hda_resume_spdif_out(codec);
  403. if (spec->dig_in_nid)
  404. snd_hda_resume_spdif_in(codec);
  405. return 0;
  406. }
  407. #endif
  408. /*
  409. * Analog playback callbacks
  410. */
  411. static int cmi9880_playback_pcm_open(struct hda_pcm_stream *hinfo,
  412. struct hda_codec *codec,
  413. struct snd_pcm_substream *substream)
  414. {
  415. struct cmi_spec *spec = codec->spec;
  416. return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream);
  417. }
  418. static int cmi9880_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  419. struct hda_codec *codec,
  420. unsigned int stream_tag,
  421. unsigned int format,
  422. struct snd_pcm_substream *substream)
  423. {
  424. struct cmi_spec *spec = codec->spec;
  425. return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
  426. format, substream);
  427. }
  428. static int cmi9880_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  429. struct hda_codec *codec,
  430. struct snd_pcm_substream *substream)
  431. {
  432. struct cmi_spec *spec = codec->spec;
  433. return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  434. }
  435. /*
  436. * Digital out
  437. */
  438. static int cmi9880_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  439. struct hda_codec *codec,
  440. struct snd_pcm_substream *substream)
  441. {
  442. struct cmi_spec *spec = codec->spec;
  443. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  444. }
  445. static int cmi9880_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  446. struct hda_codec *codec,
  447. struct snd_pcm_substream *substream)
  448. {
  449. struct cmi_spec *spec = codec->spec;
  450. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  451. }
  452. /*
  453. * Analog capture
  454. */
  455. static int cmi9880_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  456. struct hda_codec *codec,
  457. unsigned int stream_tag,
  458. unsigned int format,
  459. struct snd_pcm_substream *substream)
  460. {
  461. struct cmi_spec *spec = codec->spec;
  462. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
  463. stream_tag, 0, format);
  464. return 0;
  465. }
  466. static int cmi9880_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  467. struct hda_codec *codec,
  468. struct snd_pcm_substream *substream)
  469. {
  470. struct cmi_spec *spec = codec->spec;
  471. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number], 0, 0, 0);
  472. return 0;
  473. }
  474. /*
  475. */
  476. static struct hda_pcm_stream cmi9880_pcm_analog_playback = {
  477. .substreams = 1,
  478. .channels_min = 2,
  479. .channels_max = 8,
  480. .nid = 0x03, /* NID to query formats and rates */
  481. .ops = {
  482. .open = cmi9880_playback_pcm_open,
  483. .prepare = cmi9880_playback_pcm_prepare,
  484. .cleanup = cmi9880_playback_pcm_cleanup
  485. },
  486. };
  487. static struct hda_pcm_stream cmi9880_pcm_analog_capture = {
  488. .substreams = 2,
  489. .channels_min = 2,
  490. .channels_max = 2,
  491. .nid = 0x08, /* NID to query formats and rates */
  492. .ops = {
  493. .prepare = cmi9880_capture_pcm_prepare,
  494. .cleanup = cmi9880_capture_pcm_cleanup
  495. },
  496. };
  497. static struct hda_pcm_stream cmi9880_pcm_digital_playback = {
  498. .substreams = 1,
  499. .channels_min = 2,
  500. .channels_max = 2,
  501. /* NID is set in cmi9880_build_pcms */
  502. .ops = {
  503. .open = cmi9880_dig_playback_pcm_open,
  504. .close = cmi9880_dig_playback_pcm_close
  505. },
  506. };
  507. static struct hda_pcm_stream cmi9880_pcm_digital_capture = {
  508. .substreams = 1,
  509. .channels_min = 2,
  510. .channels_max = 2,
  511. /* NID is set in cmi9880_build_pcms */
  512. };
  513. static int cmi9880_build_pcms(struct hda_codec *codec)
  514. {
  515. struct cmi_spec *spec = codec->spec;
  516. struct hda_pcm *info = spec->pcm_rec;
  517. codec->num_pcms = 1;
  518. codec->pcm_info = info;
  519. info->name = "CMI9880";
  520. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_analog_playback;
  521. info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_analog_capture;
  522. if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
  523. codec->num_pcms++;
  524. info++;
  525. info->name = "CMI9880 Digital";
  526. if (spec->multiout.dig_out_nid) {
  527. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_digital_playback;
  528. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
  529. }
  530. if (spec->dig_in_nid) {
  531. info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_digital_capture;
  532. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
  533. }
  534. }
  535. return 0;
  536. }
  537. static void cmi9880_free(struct hda_codec *codec)
  538. {
  539. kfree(codec->spec);
  540. }
  541. /*
  542. */
  543. static const char *cmi9880_models[CMI_MODELS] = {
  544. [CMI_MINIMAL] = "minimal",
  545. [CMI_MIN_FP] = "min_fp",
  546. [CMI_FULL] = "full",
  547. [CMI_FULL_DIG] = "full_dig",
  548. [CMI_ALLOUT] = "allout",
  549. [CMI_AUTO] = "auto",
  550. };
  551. static struct snd_pci_quirk cmi9880_cfg_tbl[] = {
  552. SND_PCI_QUIRK(0x1043, 0x813d, "ASUS P5AD2", CMI_FULL_DIG),
  553. {} /* terminator */
  554. };
  555. static struct hda_codec_ops cmi9880_patch_ops = {
  556. .build_controls = cmi9880_build_controls,
  557. .build_pcms = cmi9880_build_pcms,
  558. .init = cmi9880_init,
  559. .free = cmi9880_free,
  560. #ifdef CONFIG_PM
  561. .resume = cmi9880_resume,
  562. #endif
  563. };
  564. static int patch_cmi9880(struct hda_codec *codec)
  565. {
  566. struct cmi_spec *spec;
  567. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  568. if (spec == NULL)
  569. return -ENOMEM;
  570. codec->spec = spec;
  571. spec->board_config = snd_hda_check_board_config(codec, CMI_MODELS,
  572. cmi9880_models,
  573. cmi9880_cfg_tbl);
  574. if (spec->board_config < 0) {
  575. snd_printdd(KERN_INFO "hda_codec: Unknown model for CMI9880\n");
  576. spec->board_config = CMI_AUTO; /* try everything */
  577. }
  578. /* copy default DAC NIDs */
  579. memcpy(spec->dac_nids, cmi9880_dac_nids, sizeof(spec->dac_nids));
  580. spec->num_dacs = 4;
  581. switch (spec->board_config) {
  582. case CMI_MINIMAL:
  583. case CMI_MIN_FP:
  584. spec->channel_modes = cmi9880_channel_modes;
  585. if (spec->board_config == CMI_MINIMAL)
  586. spec->num_channel_modes = 2;
  587. else {
  588. spec->front_panel = 1;
  589. spec->num_channel_modes = 3;
  590. }
  591. spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
  592. spec->input_mux = &cmi9880_basic_mux;
  593. break;
  594. case CMI_FULL:
  595. case CMI_FULL_DIG:
  596. spec->front_panel = 1;
  597. spec->multiout.max_channels = 8;
  598. spec->input_mux = &cmi9880_basic_mux;
  599. if (spec->board_config == CMI_FULL_DIG) {
  600. spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
  601. spec->dig_in_nid = CMI_DIG_IN_NID;
  602. }
  603. break;
  604. case CMI_ALLOUT:
  605. spec->front_panel = 1;
  606. spec->multiout.max_channels = 8;
  607. spec->no_line_in = 1;
  608. spec->input_mux = &cmi9880_no_line_mux;
  609. spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
  610. break;
  611. case CMI_AUTO:
  612. {
  613. unsigned int port_e, port_f, port_g, port_h;
  614. unsigned int port_spdifi, port_spdifo;
  615. struct auto_pin_cfg cfg;
  616. /* collect pin default configuration */
  617. port_e = snd_hda_codec_read(codec, 0x0f, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
  618. port_f = snd_hda_codec_read(codec, 0x10, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
  619. spec->front_panel = 1;
  620. if (get_defcfg_connect(port_e) == AC_JACK_PORT_NONE ||
  621. get_defcfg_connect(port_f) == AC_JACK_PORT_NONE) {
  622. port_g = snd_hda_codec_read(codec, 0x1f, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
  623. port_h = snd_hda_codec_read(codec, 0x20, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
  624. spec->channel_modes = cmi9880_channel_modes;
  625. /* no front panel */
  626. if (get_defcfg_connect(port_g) == AC_JACK_PORT_NONE ||
  627. get_defcfg_connect(port_h) == AC_JACK_PORT_NONE) {
  628. /* no optional rear panel */
  629. spec->board_config = CMI_MINIMAL;
  630. spec->front_panel = 0;
  631. spec->num_channel_modes = 2;
  632. } else {
  633. spec->board_config = CMI_MIN_FP;
  634. spec->num_channel_modes = 3;
  635. }
  636. spec->input_mux = &cmi9880_basic_mux;
  637. spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
  638. } else {
  639. spec->input_mux = &cmi9880_basic_mux;
  640. port_spdifi = snd_hda_codec_read(codec, 0x13, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
  641. port_spdifo = snd_hda_codec_read(codec, 0x12, 0, AC_VERB_GET_CONFIG_DEFAULT, 0);
  642. if (get_defcfg_connect(port_spdifo) != AC_JACK_PORT_NONE)
  643. spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
  644. if (get_defcfg_connect(port_spdifi) != AC_JACK_PORT_NONE)
  645. spec->dig_in_nid = CMI_DIG_IN_NID;
  646. spec->multiout.max_channels = 8;
  647. }
  648. snd_hda_parse_pin_def_config(codec, &cfg, NULL);
  649. if (cfg.line_outs) {
  650. spec->multiout.max_channels = cfg.line_outs * 2;
  651. cmi9880_fill_multi_dac_nids(codec, &cfg);
  652. cmi9880_fill_multi_init(codec, &cfg);
  653. } else
  654. snd_printd("patch_cmedia: cannot detect association in defcfg\n");
  655. break;
  656. }
  657. }
  658. spec->multiout.num_dacs = spec->num_dacs;
  659. spec->multiout.dac_nids = spec->dac_nids;
  660. spec->adc_nids = cmi9880_adc_nids;
  661. codec->patch_ops = cmi9880_patch_ops;
  662. return 0;
  663. }
  664. /*
  665. * patch entries
  666. */
  667. struct hda_codec_preset snd_hda_preset_cmedia[] = {
  668. { .id = 0x13f69880, .name = "CMI9880", .patch = patch_cmi9880 },
  669. { .id = 0x434d4980, .name = "CMI9880", .patch = patch_cmi9880 },
  670. {} /* terminator */
  671. };