patch_cmedia.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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 <linux/init.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <linux/pci.h>
  27. #include <linux/module.h>
  28. #include <sound/core.h>
  29. #include "hda_codec.h"
  30. #include "hda_local.h"
  31. #include "hda_auto_parser.h"
  32. #define NUM_PINS 11
  33. /* board config type */
  34. enum {
  35. CMI_MINIMAL, /* back 3-jack */
  36. CMI_MIN_FP, /* back 3-jack + front-panel 2-jack */
  37. CMI_FULL, /* back 6-jack + front-panel 2-jack */
  38. CMI_FULL_DIG, /* back 6-jack + front-panel 2-jack + digital I/O */
  39. CMI_ALLOUT, /* back 5-jack + front-panel 2-jack + digital out */
  40. CMI_AUTO, /* let driver guess it */
  41. CMI_MODELS
  42. };
  43. struct cmi_spec {
  44. int board_config;
  45. unsigned int no_line_in: 1; /* no line-in (5-jack) */
  46. unsigned int front_panel: 1; /* has front-panel 2-jack */
  47. /* playback */
  48. struct hda_multi_out multiout;
  49. hda_nid_t dac_nids[AUTO_CFG_MAX_OUTS]; /* NID for each DAC */
  50. int num_dacs;
  51. /* capture */
  52. const hda_nid_t *adc_nids;
  53. hda_nid_t dig_in_nid;
  54. /* capture source */
  55. const struct hda_input_mux *input_mux;
  56. unsigned int cur_mux[2];
  57. /* channel mode */
  58. int num_channel_modes;
  59. const struct hda_channel_mode *channel_modes;
  60. struct hda_pcm pcm_rec[2]; /* PCM information */
  61. /* pin default 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. 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 const 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 const 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 const 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 const 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 const 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. */
  166. /* .name = "Capture Source", */
  167. .name = "Input Source",
  168. .count = 2,
  169. .info = cmi_mux_enum_info,
  170. .get = cmi_mux_enum_get,
  171. .put = cmi_mux_enum_put,
  172. },
  173. HDA_CODEC_VOLUME("Capture Volume", 0x08, 0, HDA_INPUT),
  174. HDA_CODEC_VOLUME_IDX("Capture Volume", 1, 0x09, 0, HDA_INPUT),
  175. HDA_CODEC_MUTE("Capture Switch", 0x08, 0, HDA_INPUT),
  176. HDA_CODEC_MUTE_IDX("Capture Switch", 1, 0x09, 0, HDA_INPUT),
  177. HDA_CODEC_VOLUME("Beep Playback Volume", 0x23, 0, HDA_OUTPUT),
  178. HDA_CODEC_MUTE("Beep Playback Switch", 0x23, 0, HDA_OUTPUT),
  179. { } /* end */
  180. };
  181. /*
  182. * shared I/O pins
  183. */
  184. static const struct snd_kcontrol_new cmi9880_ch_mode_mixer[] = {
  185. {
  186. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  187. .name = "Channel Mode",
  188. .info = cmi_ch_mode_info,
  189. .get = cmi_ch_mode_get,
  190. .put = cmi_ch_mode_put,
  191. },
  192. { } /* end */
  193. };
  194. /* AUD-in selections:
  195. * 0x0b 0x0c 0x0d 0x0e 0x0f 0x10 0x11 0x1f 0x20
  196. */
  197. static const struct hda_input_mux cmi9880_basic_mux = {
  198. .num_items = 4,
  199. .items = {
  200. { "Front Mic", 0x5 },
  201. { "Rear Mic", 0x2 },
  202. { "Line", 0x1 },
  203. { "CD", 0x7 },
  204. }
  205. };
  206. static const struct hda_input_mux cmi9880_no_line_mux = {
  207. .num_items = 3,
  208. .items = {
  209. { "Front Mic", 0x5 },
  210. { "Rear Mic", 0x2 },
  211. { "CD", 0x7 },
  212. }
  213. };
  214. /* front, rear, clfe, rear_surr */
  215. static const hda_nid_t cmi9880_dac_nids[4] = {
  216. 0x03, 0x04, 0x05, 0x06
  217. };
  218. /* ADC0, ADC1 */
  219. static const hda_nid_t cmi9880_adc_nids[2] = {
  220. 0x08, 0x09
  221. };
  222. #define CMI_DIG_OUT_NID 0x07
  223. #define CMI_DIG_IN_NID 0x0a
  224. /*
  225. */
  226. static const struct hda_verb cmi9880_basic_init[] = {
  227. /* port-D for line out (rear panel) */
  228. { 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  229. /* port-E for HP out (front panel) */
  230. { 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  231. /* route front PCM to HP */
  232. { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
  233. /* port-A for surround (rear panel) */
  234. { 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  235. /* port-G for CLFE (rear panel) */
  236. { 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  237. { 0x1f, AC_VERB_SET_CONNECT_SEL, 0x02 },
  238. /* port-H for side (rear panel) */
  239. { 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  240. { 0x20, AC_VERB_SET_CONNECT_SEL, 0x01 },
  241. /* port-C for line-in (rear panel) */
  242. { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
  243. /* port-B for mic-in (rear panel) with vref */
  244. { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
  245. /* port-F for mic-in (front panel) with vref */
  246. { 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
  247. /* CD-in */
  248. { 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
  249. /* route front mic to ADC1/2 */
  250. { 0x08, AC_VERB_SET_CONNECT_SEL, 0x05 },
  251. { 0x09, AC_VERB_SET_CONNECT_SEL, 0x05 },
  252. {} /* terminator */
  253. };
  254. static const struct hda_verb cmi9880_allout_init[] = {
  255. /* port-D for line out (rear panel) */
  256. { 0x0b, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  257. /* port-E for HP out (front panel) */
  258. { 0x0f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  259. /* route front PCM to HP */
  260. { 0x0f, AC_VERB_SET_CONNECT_SEL, 0x00 },
  261. /* port-A for side (rear panel) */
  262. { 0x0e, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  263. /* port-G for CLFE (rear panel) */
  264. { 0x1f, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  265. { 0x1f, AC_VERB_SET_CONNECT_SEL, 0x02 },
  266. /* port-H for side (rear panel) */
  267. { 0x20, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  268. { 0x20, AC_VERB_SET_CONNECT_SEL, 0x01 },
  269. /* port-C for surround (rear panel) */
  270. { 0x0c, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_HP },
  271. /* port-B for mic-in (rear panel) with vref */
  272. { 0x0d, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
  273. /* port-F for mic-in (front panel) with vref */
  274. { 0x10, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80 },
  275. /* CD-in */
  276. { 0x11, AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_IN },
  277. /* route front mic to ADC1/2 */
  278. { 0x08, AC_VERB_SET_CONNECT_SEL, 0x05 },
  279. { 0x09, AC_VERB_SET_CONNECT_SEL, 0x05 },
  280. {} /* terminator */
  281. };
  282. /*
  283. */
  284. static int cmi9880_build_controls(struct hda_codec *codec)
  285. {
  286. struct cmi_spec *spec = codec->spec;
  287. struct snd_kcontrol *kctl;
  288. int i, 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,
  299. spec->multiout.dig_out_nid,
  300. spec->multiout.dig_out_nid);
  301. if (err < 0)
  302. return err;
  303. err = snd_hda_create_spdif_share_sw(codec,
  304. &spec->multiout);
  305. if (err < 0)
  306. return err;
  307. spec->multiout.share_spdif = 1;
  308. }
  309. if (spec->dig_in_nid) {
  310. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in_nid);
  311. if (err < 0)
  312. return err;
  313. }
  314. /* assign Capture Source enums to NID */
  315. kctl = snd_hda_find_mixer_ctl(codec, "Capture Source");
  316. for (i = 0; kctl && i < kctl->count; i++) {
  317. err = snd_hda_add_nid(codec, kctl, i, spec->adc_nids[i]);
  318. if (err < 0)
  319. return err;
  320. }
  321. return 0;
  322. }
  323. /* fill in the multi_dac_nids table, which will decide
  324. which audio widget to use for each channel */
  325. static int cmi9880_fill_multi_dac_nids(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
  326. {
  327. struct cmi_spec *spec = codec->spec;
  328. hda_nid_t nid;
  329. int assigned[4];
  330. int i, j;
  331. /* clear the table, only one c-media dac assumed here */
  332. memset(spec->dac_nids, 0, sizeof(spec->dac_nids));
  333. memset(assigned, 0, sizeof(assigned));
  334. /* check the pins we found */
  335. for (i = 0; i < cfg->line_outs; i++) {
  336. nid = cfg->line_out_pins[i];
  337. /* nid 0x0b~0x0e is hardwired to audio widget 0x3~0x6 */
  338. if (nid >= 0x0b && nid <= 0x0e) {
  339. spec->dac_nids[i] = (nid - 0x0b) + 0x03;
  340. assigned[nid - 0x0b] = 1;
  341. }
  342. }
  343. /* left pin can be connect to any audio widget */
  344. for (i = 0; i < cfg->line_outs; i++) {
  345. nid = cfg->line_out_pins[i];
  346. if (nid <= 0x0e)
  347. continue;
  348. /* search for an empty channel */
  349. for (j = 0; j < cfg->line_outs; j++) {
  350. if (! assigned[j]) {
  351. spec->dac_nids[i] = j + 0x03;
  352. assigned[j] = 1;
  353. break;
  354. }
  355. }
  356. }
  357. spec->num_dacs = cfg->line_outs;
  358. return 0;
  359. }
  360. /* create multi_init table, which is used for multichannel initialization */
  361. static int cmi9880_fill_multi_init(struct hda_codec *codec, const struct auto_pin_cfg *cfg)
  362. {
  363. struct cmi_spec *spec = codec->spec;
  364. hda_nid_t nid;
  365. int i, j, k;
  366. /* clear the table, only one c-media dac assumed here */
  367. memset(spec->multi_init, 0, sizeof(spec->multi_init));
  368. for (j = 0, i = 0; i < cfg->line_outs; i++) {
  369. nid = cfg->line_out_pins[i];
  370. /* set as output */
  371. spec->multi_init[j].nid = nid;
  372. spec->multi_init[j].verb = AC_VERB_SET_PIN_WIDGET_CONTROL;
  373. spec->multi_init[j].param = PIN_OUT;
  374. j++;
  375. if (nid > 0x0e) {
  376. /* set connection */
  377. spec->multi_init[j].nid = nid;
  378. spec->multi_init[j].verb = AC_VERB_SET_CONNECT_SEL;
  379. spec->multi_init[j].param = 0;
  380. /* find the index in connect list */
  381. k = snd_hda_get_conn_index(codec, nid,
  382. spec->dac_nids[i], 0);
  383. if (k >= 0)
  384. spec->multi_init[j].param = k;
  385. j++;
  386. }
  387. }
  388. return 0;
  389. }
  390. static int cmi9880_init(struct hda_codec *codec)
  391. {
  392. struct cmi_spec *spec = codec->spec;
  393. if (spec->board_config == CMI_ALLOUT)
  394. snd_hda_sequence_write(codec, cmi9880_allout_init);
  395. else
  396. snd_hda_sequence_write(codec, cmi9880_basic_init);
  397. if (spec->board_config == CMI_AUTO)
  398. snd_hda_sequence_write(codec, spec->multi_init);
  399. return 0;
  400. }
  401. /*
  402. * Analog playback callbacks
  403. */
  404. static int cmi9880_playback_pcm_open(struct hda_pcm_stream *hinfo,
  405. struct hda_codec *codec,
  406. struct snd_pcm_substream *substream)
  407. {
  408. struct cmi_spec *spec = codec->spec;
  409. return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
  410. hinfo);
  411. }
  412. static int cmi9880_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  413. struct hda_codec *codec,
  414. unsigned int stream_tag,
  415. unsigned int format,
  416. struct snd_pcm_substream *substream)
  417. {
  418. struct cmi_spec *spec = codec->spec;
  419. return snd_hda_multi_out_analog_prepare(codec, &spec->multiout, stream_tag,
  420. format, substream);
  421. }
  422. static int cmi9880_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  423. struct hda_codec *codec,
  424. struct snd_pcm_substream *substream)
  425. {
  426. struct cmi_spec *spec = codec->spec;
  427. return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  428. }
  429. /*
  430. * Digital out
  431. */
  432. static int cmi9880_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  433. struct hda_codec *codec,
  434. struct snd_pcm_substream *substream)
  435. {
  436. struct cmi_spec *spec = codec->spec;
  437. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  438. }
  439. static int cmi9880_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  440. struct hda_codec *codec,
  441. struct snd_pcm_substream *substream)
  442. {
  443. struct cmi_spec *spec = codec->spec;
  444. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  445. }
  446. static int cmi9880_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  447. struct hda_codec *codec,
  448. unsigned int stream_tag,
  449. unsigned int format,
  450. struct snd_pcm_substream *substream)
  451. {
  452. struct cmi_spec *spec = codec->spec;
  453. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
  454. format, substream);
  455. }
  456. /*
  457. * Analog capture
  458. */
  459. static int cmi9880_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  460. struct hda_codec *codec,
  461. unsigned int stream_tag,
  462. unsigned int format,
  463. struct snd_pcm_substream *substream)
  464. {
  465. struct cmi_spec *spec = codec->spec;
  466. snd_hda_codec_setup_stream(codec, spec->adc_nids[substream->number],
  467. stream_tag, 0, format);
  468. return 0;
  469. }
  470. static int cmi9880_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  471. struct hda_codec *codec,
  472. struct snd_pcm_substream *substream)
  473. {
  474. struct cmi_spec *spec = codec->spec;
  475. snd_hda_codec_cleanup_stream(codec, spec->adc_nids[substream->number]);
  476. return 0;
  477. }
  478. /*
  479. */
  480. static const struct hda_pcm_stream cmi9880_pcm_analog_playback = {
  481. .substreams = 1,
  482. .channels_min = 2,
  483. .channels_max = 8,
  484. .nid = 0x03, /* NID to query formats and rates */
  485. .ops = {
  486. .open = cmi9880_playback_pcm_open,
  487. .prepare = cmi9880_playback_pcm_prepare,
  488. .cleanup = cmi9880_playback_pcm_cleanup
  489. },
  490. };
  491. static const struct hda_pcm_stream cmi9880_pcm_analog_capture = {
  492. .substreams = 2,
  493. .channels_min = 2,
  494. .channels_max = 2,
  495. .nid = 0x08, /* NID to query formats and rates */
  496. .ops = {
  497. .prepare = cmi9880_capture_pcm_prepare,
  498. .cleanup = cmi9880_capture_pcm_cleanup
  499. },
  500. };
  501. static const struct hda_pcm_stream cmi9880_pcm_digital_playback = {
  502. .substreams = 1,
  503. .channels_min = 2,
  504. .channels_max = 2,
  505. /* NID is set in cmi9880_build_pcms */
  506. .ops = {
  507. .open = cmi9880_dig_playback_pcm_open,
  508. .close = cmi9880_dig_playback_pcm_close,
  509. .prepare = cmi9880_dig_playback_pcm_prepare
  510. },
  511. };
  512. static const struct hda_pcm_stream cmi9880_pcm_digital_capture = {
  513. .substreams = 1,
  514. .channels_min = 2,
  515. .channels_max = 2,
  516. /* NID is set in cmi9880_build_pcms */
  517. };
  518. static int cmi9880_build_pcms(struct hda_codec *codec)
  519. {
  520. struct cmi_spec *spec = codec->spec;
  521. struct hda_pcm *info = spec->pcm_rec;
  522. codec->num_pcms = 1;
  523. codec->pcm_info = info;
  524. info->name = "CMI9880";
  525. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_analog_playback;
  526. info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_analog_capture;
  527. if (spec->multiout.dig_out_nid || spec->dig_in_nid) {
  528. codec->num_pcms++;
  529. info++;
  530. info->name = "CMI9880 Digital";
  531. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  532. if (spec->multiout.dig_out_nid) {
  533. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = cmi9880_pcm_digital_playback;
  534. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->multiout.dig_out_nid;
  535. }
  536. if (spec->dig_in_nid) {
  537. info->stream[SNDRV_PCM_STREAM_CAPTURE] = cmi9880_pcm_digital_capture;
  538. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in_nid;
  539. }
  540. }
  541. return 0;
  542. }
  543. static void cmi9880_free(struct hda_codec *codec)
  544. {
  545. kfree(codec->spec);
  546. }
  547. /*
  548. */
  549. static const char * const cmi9880_models[CMI_MODELS] = {
  550. [CMI_MINIMAL] = "minimal",
  551. [CMI_MIN_FP] = "min_fp",
  552. [CMI_FULL] = "full",
  553. [CMI_FULL_DIG] = "full_dig",
  554. [CMI_ALLOUT] = "allout",
  555. [CMI_AUTO] = "auto",
  556. };
  557. static const struct snd_pci_quirk cmi9880_cfg_tbl[] = {
  558. SND_PCI_QUIRK(0x1043, 0x813d, "ASUS P5AD2", CMI_FULL_DIG),
  559. SND_PCI_QUIRK(0x1854, 0x002b, "LG LS75", CMI_MINIMAL),
  560. SND_PCI_QUIRK(0x1854, 0x0032, "LG", CMI_FULL_DIG),
  561. {} /* terminator */
  562. };
  563. static const struct hda_codec_ops cmi9880_patch_ops = {
  564. .build_controls = cmi9880_build_controls,
  565. .build_pcms = cmi9880_build_pcms,
  566. .init = cmi9880_init,
  567. .free = cmi9880_free,
  568. };
  569. static int patch_cmi9880(struct hda_codec *codec)
  570. {
  571. struct cmi_spec *spec;
  572. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  573. if (spec == NULL)
  574. return -ENOMEM;
  575. codec->spec = spec;
  576. spec->board_config = snd_hda_check_board_config(codec, CMI_MODELS,
  577. cmi9880_models,
  578. cmi9880_cfg_tbl);
  579. if (spec->board_config < 0) {
  580. snd_printdd(KERN_INFO "hda_codec: %s: BIOS auto-probing.\n",
  581. codec->chip_name);
  582. spec->board_config = CMI_AUTO; /* try everything */
  583. }
  584. /* copy default DAC NIDs */
  585. memcpy(spec->dac_nids, cmi9880_dac_nids, sizeof(spec->dac_nids));
  586. spec->num_dacs = 4;
  587. switch (spec->board_config) {
  588. case CMI_MINIMAL:
  589. case CMI_MIN_FP:
  590. spec->channel_modes = cmi9880_channel_modes;
  591. if (spec->board_config == CMI_MINIMAL)
  592. spec->num_channel_modes = 2;
  593. else {
  594. spec->front_panel = 1;
  595. spec->num_channel_modes = 3;
  596. }
  597. spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
  598. spec->input_mux = &cmi9880_basic_mux;
  599. break;
  600. case CMI_FULL:
  601. case CMI_FULL_DIG:
  602. spec->front_panel = 1;
  603. spec->multiout.max_channels = 8;
  604. spec->input_mux = &cmi9880_basic_mux;
  605. if (spec->board_config == CMI_FULL_DIG) {
  606. spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
  607. spec->dig_in_nid = CMI_DIG_IN_NID;
  608. }
  609. break;
  610. case CMI_ALLOUT:
  611. spec->front_panel = 1;
  612. spec->multiout.max_channels = 8;
  613. spec->no_line_in = 1;
  614. spec->input_mux = &cmi9880_no_line_mux;
  615. spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
  616. break;
  617. case CMI_AUTO:
  618. {
  619. unsigned int port_e, port_f, port_g, port_h;
  620. unsigned int port_spdifi, port_spdifo;
  621. struct auto_pin_cfg cfg;
  622. /* collect pin default configuration */
  623. port_e = snd_hda_codec_get_pincfg(codec, 0x0f);
  624. port_f = snd_hda_codec_get_pincfg(codec, 0x10);
  625. spec->front_panel = 1;
  626. if (get_defcfg_connect(port_e) == AC_JACK_PORT_NONE ||
  627. get_defcfg_connect(port_f) == AC_JACK_PORT_NONE) {
  628. port_g = snd_hda_codec_get_pincfg(codec, 0x1f);
  629. port_h = snd_hda_codec_get_pincfg(codec, 0x20);
  630. spec->channel_modes = cmi9880_channel_modes;
  631. /* no front panel */
  632. if (get_defcfg_connect(port_g) == AC_JACK_PORT_NONE ||
  633. get_defcfg_connect(port_h) == AC_JACK_PORT_NONE) {
  634. /* no optional rear panel */
  635. spec->board_config = CMI_MINIMAL;
  636. spec->front_panel = 0;
  637. spec->num_channel_modes = 2;
  638. } else {
  639. spec->board_config = CMI_MIN_FP;
  640. spec->num_channel_modes = 3;
  641. }
  642. spec->input_mux = &cmi9880_basic_mux;
  643. spec->multiout.max_channels = cmi9880_channel_modes[0].channels;
  644. } else {
  645. spec->input_mux = &cmi9880_basic_mux;
  646. port_spdifi = snd_hda_codec_get_pincfg(codec, 0x13);
  647. port_spdifo = snd_hda_codec_get_pincfg(codec, 0x12);
  648. if (get_defcfg_connect(port_spdifo) != AC_JACK_PORT_NONE)
  649. spec->multiout.dig_out_nid = CMI_DIG_OUT_NID;
  650. if (get_defcfg_connect(port_spdifi) != AC_JACK_PORT_NONE)
  651. spec->dig_in_nid = CMI_DIG_IN_NID;
  652. spec->multiout.max_channels = 8;
  653. }
  654. snd_hda_parse_pin_def_config(codec, &cfg, NULL);
  655. if (cfg.line_outs) {
  656. spec->multiout.max_channels = cfg.line_outs * 2;
  657. cmi9880_fill_multi_dac_nids(codec, &cfg);
  658. cmi9880_fill_multi_init(codec, &cfg);
  659. } else
  660. snd_printd("patch_cmedia: cannot detect association in defcfg\n");
  661. break;
  662. }
  663. }
  664. spec->multiout.num_dacs = spec->num_dacs;
  665. spec->multiout.dac_nids = spec->dac_nids;
  666. spec->adc_nids = cmi9880_adc_nids;
  667. codec->patch_ops = cmi9880_patch_ops;
  668. return 0;
  669. }
  670. /*
  671. * patch entries
  672. */
  673. static const struct hda_codec_preset snd_hda_preset_cmedia[] = {
  674. { .id = 0x13f69880, .name = "CMI9880", .patch = patch_cmi9880 },
  675. { .id = 0x434d4980, .name = "CMI9880", .patch = patch_cmi9880 },
  676. {} /* terminator */
  677. };
  678. MODULE_ALIAS("snd-hda-codec-id:13f69880");
  679. MODULE_ALIAS("snd-hda-codec-id:434d4980");
  680. MODULE_LICENSE("GPL");
  681. MODULE_DESCRIPTION("C-Media HD-audio codec");
  682. static struct hda_codec_preset_list cmedia_list = {
  683. .preset = snd_hda_preset_cmedia,
  684. .owner = THIS_MODULE,
  685. };
  686. static int __init patch_cmedia_init(void)
  687. {
  688. return snd_hda_add_codec_preset(&cmedia_list);
  689. }
  690. static void __exit patch_cmedia_exit(void)
  691. {
  692. snd_hda_delete_codec_preset(&cmedia_list);
  693. }
  694. module_init(patch_cmedia_init)
  695. module_exit(patch_cmedia_exit)