patch_ca0110.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * HD audio interface patch for Creative X-Fi CA0110-IBG chip
  3. *
  4. * Copyright (c) 2008 Takashi Iwai <tiwai@suse.de>
  5. *
  6. * This driver is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This driver is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/init.h>
  21. #include <linux/delay.h>
  22. #include <linux/slab.h>
  23. #include <linux/pci.h>
  24. #include <linux/module.h>
  25. #include <sound/core.h>
  26. #include "hda_codec.h"
  27. #include "hda_local.h"
  28. #include "hda_auto_parser.h"
  29. /*
  30. */
  31. struct ca0110_spec {
  32. struct auto_pin_cfg autocfg;
  33. struct hda_multi_out multiout;
  34. hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
  35. hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
  36. hda_nid_t hp_dac;
  37. hda_nid_t input_pins[AUTO_PIN_LAST];
  38. hda_nid_t adcs[AUTO_PIN_LAST];
  39. hda_nid_t dig_out;
  40. hda_nid_t dig_in;
  41. unsigned int num_inputs;
  42. char input_labels[AUTO_PIN_LAST][32];
  43. struct hda_pcm pcm_rec[2]; /* PCM information */
  44. };
  45. /*
  46. * PCM callbacks
  47. */
  48. static int ca0110_playback_pcm_open(struct hda_pcm_stream *hinfo,
  49. struct hda_codec *codec,
  50. struct snd_pcm_substream *substream)
  51. {
  52. struct ca0110_spec *spec = codec->spec;
  53. return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
  54. hinfo);
  55. }
  56. static int ca0110_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  57. struct hda_codec *codec,
  58. unsigned int stream_tag,
  59. unsigned int format,
  60. struct snd_pcm_substream *substream)
  61. {
  62. struct ca0110_spec *spec = codec->spec;
  63. return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
  64. stream_tag, format, substream);
  65. }
  66. static int ca0110_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  67. struct hda_codec *codec,
  68. struct snd_pcm_substream *substream)
  69. {
  70. struct ca0110_spec *spec = codec->spec;
  71. return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  72. }
  73. /*
  74. * Digital out
  75. */
  76. static int ca0110_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  77. struct hda_codec *codec,
  78. struct snd_pcm_substream *substream)
  79. {
  80. struct ca0110_spec *spec = codec->spec;
  81. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  82. }
  83. static int ca0110_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  84. struct hda_codec *codec,
  85. struct snd_pcm_substream *substream)
  86. {
  87. struct ca0110_spec *spec = codec->spec;
  88. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  89. }
  90. static int ca0110_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  91. struct hda_codec *codec,
  92. unsigned int stream_tag,
  93. unsigned int format,
  94. struct snd_pcm_substream *substream)
  95. {
  96. struct ca0110_spec *spec = codec->spec;
  97. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
  98. format, substream);
  99. }
  100. /*
  101. * Analog capture
  102. */
  103. static int ca0110_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  104. struct hda_codec *codec,
  105. unsigned int stream_tag,
  106. unsigned int format,
  107. struct snd_pcm_substream *substream)
  108. {
  109. struct ca0110_spec *spec = codec->spec;
  110. snd_hda_codec_setup_stream(codec, spec->adcs[substream->number],
  111. stream_tag, 0, format);
  112. return 0;
  113. }
  114. static int ca0110_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  115. struct hda_codec *codec,
  116. struct snd_pcm_substream *substream)
  117. {
  118. struct ca0110_spec *spec = codec->spec;
  119. snd_hda_codec_cleanup_stream(codec, spec->adcs[substream->number]);
  120. return 0;
  121. }
  122. /*
  123. */
  124. static const char * const dirstr[2] = { "Playback", "Capture" };
  125. static int _add_switch(struct hda_codec *codec, hda_nid_t nid, const char *pfx,
  126. int chan, int dir)
  127. {
  128. char namestr[44];
  129. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  130. struct snd_kcontrol_new knew =
  131. HDA_CODEC_MUTE_MONO(namestr, nid, chan, 0, type);
  132. sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
  133. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  134. }
  135. static int _add_volume(struct hda_codec *codec, hda_nid_t nid, const char *pfx,
  136. int chan, int dir)
  137. {
  138. char namestr[44];
  139. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  140. struct snd_kcontrol_new knew =
  141. HDA_CODEC_VOLUME_MONO(namestr, nid, chan, 0, type);
  142. sprintf(namestr, "%s %s Volume", pfx, dirstr[dir]);
  143. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  144. }
  145. #define add_out_switch(codec, nid, pfx) _add_switch(codec, nid, pfx, 3, 0)
  146. #define add_out_volume(codec, nid, pfx) _add_volume(codec, nid, pfx, 3, 0)
  147. #define add_in_switch(codec, nid, pfx) _add_switch(codec, nid, pfx, 3, 1)
  148. #define add_in_volume(codec, nid, pfx) _add_volume(codec, nid, pfx, 3, 1)
  149. #define add_mono_switch(codec, nid, pfx, chan) \
  150. _add_switch(codec, nid, pfx, chan, 0)
  151. #define add_mono_volume(codec, nid, pfx, chan) \
  152. _add_volume(codec, nid, pfx, chan, 0)
  153. static int ca0110_build_controls(struct hda_codec *codec)
  154. {
  155. struct ca0110_spec *spec = codec->spec;
  156. struct auto_pin_cfg *cfg = &spec->autocfg;
  157. static const char * const prefix[AUTO_CFG_MAX_OUTS] = {
  158. "Front", "Surround", NULL, "Side", "Multi"
  159. };
  160. hda_nid_t mutenid;
  161. int i, err;
  162. for (i = 0; i < spec->multiout.num_dacs; i++) {
  163. if (get_wcaps(codec, spec->out_pins[i]) & AC_WCAP_OUT_AMP)
  164. mutenid = spec->out_pins[i];
  165. else
  166. mutenid = spec->multiout.dac_nids[i];
  167. if (!prefix[i]) {
  168. err = add_mono_switch(codec, mutenid,
  169. "Center", 1);
  170. if (err < 0)
  171. return err;
  172. err = add_mono_switch(codec, mutenid,
  173. "LFE", 1);
  174. if (err < 0)
  175. return err;
  176. err = add_mono_volume(codec, spec->multiout.dac_nids[i],
  177. "Center", 1);
  178. if (err < 0)
  179. return err;
  180. err = add_mono_volume(codec, spec->multiout.dac_nids[i],
  181. "LFE", 1);
  182. if (err < 0)
  183. return err;
  184. } else {
  185. err = add_out_switch(codec, mutenid,
  186. prefix[i]);
  187. if (err < 0)
  188. return err;
  189. err = add_out_volume(codec, spec->multiout.dac_nids[i],
  190. prefix[i]);
  191. if (err < 0)
  192. return err;
  193. }
  194. }
  195. if (cfg->hp_outs) {
  196. if (get_wcaps(codec, cfg->hp_pins[0]) & AC_WCAP_OUT_AMP)
  197. mutenid = cfg->hp_pins[0];
  198. else
  199. mutenid = spec->multiout.dac_nids[i];
  200. err = add_out_switch(codec, mutenid, "Headphone");
  201. if (err < 0)
  202. return err;
  203. if (spec->hp_dac) {
  204. err = add_out_volume(codec, spec->hp_dac, "Headphone");
  205. if (err < 0)
  206. return err;
  207. }
  208. }
  209. for (i = 0; i < spec->num_inputs; i++) {
  210. const char *label = spec->input_labels[i];
  211. if (get_wcaps(codec, spec->input_pins[i]) & AC_WCAP_IN_AMP)
  212. mutenid = spec->input_pins[i];
  213. else
  214. mutenid = spec->adcs[i];
  215. err = add_in_switch(codec, mutenid, label);
  216. if (err < 0)
  217. return err;
  218. err = add_in_volume(codec, spec->adcs[i], label);
  219. if (err < 0)
  220. return err;
  221. }
  222. if (spec->dig_out) {
  223. err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
  224. spec->dig_out);
  225. if (err < 0)
  226. return err;
  227. err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
  228. if (err < 0)
  229. return err;
  230. spec->multiout.share_spdif = 1;
  231. }
  232. if (spec->dig_in) {
  233. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
  234. if (err < 0)
  235. return err;
  236. err = add_in_volume(codec, spec->dig_in, "IEC958");
  237. }
  238. return 0;
  239. }
  240. /*
  241. */
  242. static const struct hda_pcm_stream ca0110_pcm_analog_playback = {
  243. .substreams = 1,
  244. .channels_min = 2,
  245. .channels_max = 8,
  246. .ops = {
  247. .open = ca0110_playback_pcm_open,
  248. .prepare = ca0110_playback_pcm_prepare,
  249. .cleanup = ca0110_playback_pcm_cleanup
  250. },
  251. };
  252. static const struct hda_pcm_stream ca0110_pcm_analog_capture = {
  253. .substreams = 1,
  254. .channels_min = 2,
  255. .channels_max = 2,
  256. .ops = {
  257. .prepare = ca0110_capture_pcm_prepare,
  258. .cleanup = ca0110_capture_pcm_cleanup
  259. },
  260. };
  261. static const struct hda_pcm_stream ca0110_pcm_digital_playback = {
  262. .substreams = 1,
  263. .channels_min = 2,
  264. .channels_max = 2,
  265. .ops = {
  266. .open = ca0110_dig_playback_pcm_open,
  267. .close = ca0110_dig_playback_pcm_close,
  268. .prepare = ca0110_dig_playback_pcm_prepare
  269. },
  270. };
  271. static const struct hda_pcm_stream ca0110_pcm_digital_capture = {
  272. .substreams = 1,
  273. .channels_min = 2,
  274. .channels_max = 2,
  275. };
  276. static int ca0110_build_pcms(struct hda_codec *codec)
  277. {
  278. struct ca0110_spec *spec = codec->spec;
  279. struct hda_pcm *info = spec->pcm_rec;
  280. codec->pcm_info = info;
  281. codec->num_pcms = 0;
  282. info->name = "CA0110 Analog";
  283. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0110_pcm_analog_playback;
  284. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
  285. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  286. spec->multiout.max_channels;
  287. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0110_pcm_analog_capture;
  288. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_inputs;
  289. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
  290. codec->num_pcms++;
  291. if (!spec->dig_out && !spec->dig_in)
  292. return 0;
  293. info++;
  294. info->name = "CA0110 Digital";
  295. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  296. if (spec->dig_out) {
  297. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  298. ca0110_pcm_digital_playback;
  299. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
  300. }
  301. if (spec->dig_in) {
  302. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  303. ca0110_pcm_digital_capture;
  304. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
  305. }
  306. codec->num_pcms++;
  307. return 0;
  308. }
  309. static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
  310. {
  311. if (pin) {
  312. snd_hda_set_pin_ctl(codec, pin, PIN_HP);
  313. if (get_wcaps(codec, pin) & AC_WCAP_OUT_AMP)
  314. snd_hda_codec_write(codec, pin, 0,
  315. AC_VERB_SET_AMP_GAIN_MUTE,
  316. AMP_OUT_UNMUTE);
  317. }
  318. if (dac)
  319. snd_hda_codec_write(codec, dac, 0,
  320. AC_VERB_SET_AMP_GAIN_MUTE, AMP_OUT_ZERO);
  321. }
  322. static void init_input(struct hda_codec *codec, hda_nid_t pin, hda_nid_t adc)
  323. {
  324. if (pin) {
  325. snd_hda_set_pin_ctl(codec, pin, PIN_IN |
  326. snd_hda_get_default_vref(codec, pin));
  327. if (get_wcaps(codec, pin) & AC_WCAP_IN_AMP)
  328. snd_hda_codec_write(codec, pin, 0,
  329. AC_VERB_SET_AMP_GAIN_MUTE,
  330. AMP_IN_UNMUTE(0));
  331. }
  332. if (adc)
  333. snd_hda_codec_write(codec, adc, 0, AC_VERB_SET_AMP_GAIN_MUTE,
  334. AMP_IN_UNMUTE(0));
  335. }
  336. static int ca0110_init(struct hda_codec *codec)
  337. {
  338. struct ca0110_spec *spec = codec->spec;
  339. struct auto_pin_cfg *cfg = &spec->autocfg;
  340. int i;
  341. for (i = 0; i < spec->multiout.num_dacs; i++)
  342. init_output(codec, spec->out_pins[i],
  343. spec->multiout.dac_nids[i]);
  344. init_output(codec, cfg->hp_pins[0], spec->hp_dac);
  345. init_output(codec, cfg->dig_out_pins[0], spec->dig_out);
  346. for (i = 0; i < spec->num_inputs; i++)
  347. init_input(codec, spec->input_pins[i], spec->adcs[i]);
  348. init_input(codec, cfg->dig_in_pin, spec->dig_in);
  349. return 0;
  350. }
  351. static void ca0110_free(struct hda_codec *codec)
  352. {
  353. kfree(codec->spec);
  354. }
  355. static const struct hda_codec_ops ca0110_patch_ops = {
  356. .build_controls = ca0110_build_controls,
  357. .build_pcms = ca0110_build_pcms,
  358. .init = ca0110_init,
  359. .free = ca0110_free,
  360. };
  361. static void parse_line_outs(struct hda_codec *codec)
  362. {
  363. struct ca0110_spec *spec = codec->spec;
  364. struct auto_pin_cfg *cfg = &spec->autocfg;
  365. int i, n;
  366. unsigned int def_conf;
  367. hda_nid_t nid;
  368. n = 0;
  369. for (i = 0; i < cfg->line_outs; i++) {
  370. nid = cfg->line_out_pins[i];
  371. def_conf = snd_hda_codec_get_pincfg(codec, nid);
  372. if (!def_conf)
  373. continue; /* invalid pin */
  374. if (snd_hda_get_connections(codec, nid, &spec->dacs[i], 1) != 1)
  375. continue;
  376. spec->out_pins[n++] = nid;
  377. }
  378. spec->multiout.dac_nids = spec->dacs;
  379. spec->multiout.num_dacs = n;
  380. spec->multiout.max_channels = n * 2;
  381. }
  382. static void parse_hp_out(struct hda_codec *codec)
  383. {
  384. struct ca0110_spec *spec = codec->spec;
  385. struct auto_pin_cfg *cfg = &spec->autocfg;
  386. int i;
  387. unsigned int def_conf;
  388. hda_nid_t nid, dac;
  389. if (!cfg->hp_outs)
  390. return;
  391. nid = cfg->hp_pins[0];
  392. def_conf = snd_hda_codec_get_pincfg(codec, nid);
  393. if (!def_conf) {
  394. cfg->hp_outs = 0;
  395. return;
  396. }
  397. if (snd_hda_get_connections(codec, nid, &dac, 1) != 1)
  398. return;
  399. for (i = 0; i < cfg->line_outs; i++)
  400. if (dac == spec->dacs[i])
  401. break;
  402. if (i >= cfg->line_outs) {
  403. spec->hp_dac = dac;
  404. spec->multiout.hp_nid = dac;
  405. }
  406. }
  407. static void parse_input(struct hda_codec *codec)
  408. {
  409. struct ca0110_spec *spec = codec->spec;
  410. struct auto_pin_cfg *cfg = &spec->autocfg;
  411. hda_nid_t nid, pin;
  412. int n, i, j;
  413. n = 0;
  414. nid = codec->start_nid;
  415. for (i = 0; i < codec->num_nodes; i++, nid++) {
  416. unsigned int wcaps = get_wcaps(codec, nid);
  417. unsigned int type = get_wcaps_type(wcaps);
  418. if (type != AC_WID_AUD_IN)
  419. continue;
  420. if (snd_hda_get_connections(codec, nid, &pin, 1) != 1)
  421. continue;
  422. if (pin == cfg->dig_in_pin) {
  423. spec->dig_in = nid;
  424. continue;
  425. }
  426. for (j = 0; j < cfg->num_inputs; j++)
  427. if (cfg->inputs[j].pin == pin)
  428. break;
  429. if (j >= cfg->num_inputs)
  430. continue;
  431. spec->input_pins[n] = pin;
  432. snd_hda_get_pin_label(codec, pin, cfg,
  433. spec->input_labels[n],
  434. sizeof(spec->input_labels[n]), NULL);
  435. spec->adcs[n] = nid;
  436. n++;
  437. }
  438. spec->num_inputs = n;
  439. }
  440. static void parse_digital(struct hda_codec *codec)
  441. {
  442. struct ca0110_spec *spec = codec->spec;
  443. struct auto_pin_cfg *cfg = &spec->autocfg;
  444. if (cfg->dig_outs &&
  445. snd_hda_get_connections(codec, cfg->dig_out_pins[0],
  446. &spec->dig_out, 1) == 1)
  447. spec->multiout.dig_out_nid = spec->dig_out;
  448. }
  449. static int ca0110_parse_auto_config(struct hda_codec *codec)
  450. {
  451. struct ca0110_spec *spec = codec->spec;
  452. int err;
  453. err = snd_hda_parse_pin_def_config(codec, &spec->autocfg, NULL);
  454. if (err < 0)
  455. return err;
  456. parse_line_outs(codec);
  457. parse_hp_out(codec);
  458. parse_digital(codec);
  459. parse_input(codec);
  460. return 0;
  461. }
  462. static int patch_ca0110(struct hda_codec *codec)
  463. {
  464. struct ca0110_spec *spec;
  465. int err;
  466. spec = kzalloc(sizeof(*spec), GFP_KERNEL);
  467. if (!spec)
  468. return -ENOMEM;
  469. codec->spec = spec;
  470. codec->bus->needs_damn_long_delay = 1;
  471. err = ca0110_parse_auto_config(codec);
  472. if (err < 0)
  473. goto error;
  474. codec->patch_ops = ca0110_patch_ops;
  475. return 0;
  476. error:
  477. kfree(codec->spec);
  478. codec->spec = NULL;
  479. return err;
  480. }
  481. /*
  482. * patch entries
  483. */
  484. static const struct hda_codec_preset snd_hda_preset_ca0110[] = {
  485. { .id = 0x1102000a, .name = "CA0110-IBG", .patch = patch_ca0110 },
  486. { .id = 0x1102000b, .name = "CA0110-IBG", .patch = patch_ca0110 },
  487. { .id = 0x1102000d, .name = "SB0880 X-Fi", .patch = patch_ca0110 },
  488. {} /* terminator */
  489. };
  490. MODULE_ALIAS("snd-hda-codec-id:1102000a");
  491. MODULE_ALIAS("snd-hda-codec-id:1102000b");
  492. MODULE_ALIAS("snd-hda-codec-id:1102000d");
  493. MODULE_LICENSE("GPL");
  494. MODULE_DESCRIPTION("Creative CA0110-IBG HD-audio codec");
  495. static struct hda_codec_preset_list ca0110_list = {
  496. .preset = snd_hda_preset_ca0110,
  497. .owner = THIS_MODULE,
  498. };
  499. static int __init patch_ca0110_init(void)
  500. {
  501. return snd_hda_add_codec_preset(&ca0110_list);
  502. }
  503. static void __exit patch_ca0110_exit(void)
  504. {
  505. snd_hda_delete_codec_preset(&ca0110_list);
  506. }
  507. module_init(patch_ca0110_init)
  508. module_exit(patch_ca0110_exit)