patch_cmedia.c 22 KB

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