patch_cmedia.c 22 KB

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