patch_ca0110.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. /*
  29. */
  30. struct ca0110_spec {
  31. struct auto_pin_cfg autocfg;
  32. struct hda_multi_out multiout;
  33. hda_nid_t out_pins[AUTO_CFG_MAX_OUTS];
  34. hda_nid_t dacs[AUTO_CFG_MAX_OUTS];
  35. hda_nid_t hp_dac;
  36. hda_nid_t input_pins[AUTO_PIN_LAST];
  37. hda_nid_t adcs[AUTO_PIN_LAST];
  38. hda_nid_t dig_out;
  39. hda_nid_t dig_in;
  40. unsigned int num_inputs;
  41. char input_labels[AUTO_PIN_LAST][32];
  42. struct hda_pcm pcm_rec[2]; /* PCM information */
  43. };
  44. /*
  45. * PCM callbacks
  46. */
  47. static int ca0110_playback_pcm_open(struct hda_pcm_stream *hinfo,
  48. struct hda_codec *codec,
  49. struct snd_pcm_substream *substream)
  50. {
  51. struct ca0110_spec *spec = codec->spec;
  52. return snd_hda_multi_out_analog_open(codec, &spec->multiout, substream,
  53. hinfo);
  54. }
  55. static int ca0110_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  56. struct hda_codec *codec,
  57. unsigned int stream_tag,
  58. unsigned int format,
  59. struct snd_pcm_substream *substream)
  60. {
  61. struct ca0110_spec *spec = codec->spec;
  62. return snd_hda_multi_out_analog_prepare(codec, &spec->multiout,
  63. stream_tag, format, substream);
  64. }
  65. static int ca0110_playback_pcm_cleanup(struct hda_pcm_stream *hinfo,
  66. struct hda_codec *codec,
  67. struct snd_pcm_substream *substream)
  68. {
  69. struct ca0110_spec *spec = codec->spec;
  70. return snd_hda_multi_out_analog_cleanup(codec, &spec->multiout);
  71. }
  72. /*
  73. * Digital out
  74. */
  75. static int ca0110_dig_playback_pcm_open(struct hda_pcm_stream *hinfo,
  76. struct hda_codec *codec,
  77. struct snd_pcm_substream *substream)
  78. {
  79. struct ca0110_spec *spec = codec->spec;
  80. return snd_hda_multi_out_dig_open(codec, &spec->multiout);
  81. }
  82. static int ca0110_dig_playback_pcm_close(struct hda_pcm_stream *hinfo,
  83. struct hda_codec *codec,
  84. struct snd_pcm_substream *substream)
  85. {
  86. struct ca0110_spec *spec = codec->spec;
  87. return snd_hda_multi_out_dig_close(codec, &spec->multiout);
  88. }
  89. static int ca0110_dig_playback_pcm_prepare(struct hda_pcm_stream *hinfo,
  90. struct hda_codec *codec,
  91. unsigned int stream_tag,
  92. unsigned int format,
  93. struct snd_pcm_substream *substream)
  94. {
  95. struct ca0110_spec *spec = codec->spec;
  96. return snd_hda_multi_out_dig_prepare(codec, &spec->multiout, stream_tag,
  97. format, substream);
  98. }
  99. /*
  100. * Analog capture
  101. */
  102. static int ca0110_capture_pcm_prepare(struct hda_pcm_stream *hinfo,
  103. struct hda_codec *codec,
  104. unsigned int stream_tag,
  105. unsigned int format,
  106. struct snd_pcm_substream *substream)
  107. {
  108. struct ca0110_spec *spec = codec->spec;
  109. snd_hda_codec_setup_stream(codec, spec->adcs[substream->number],
  110. stream_tag, 0, format);
  111. return 0;
  112. }
  113. static int ca0110_capture_pcm_cleanup(struct hda_pcm_stream *hinfo,
  114. struct hda_codec *codec,
  115. struct snd_pcm_substream *substream)
  116. {
  117. struct ca0110_spec *spec = codec->spec;
  118. snd_hda_codec_cleanup_stream(codec, spec->adcs[substream->number]);
  119. return 0;
  120. }
  121. /*
  122. */
  123. static const char * const dirstr[2] = { "Playback", "Capture" };
  124. static int _add_switch(struct hda_codec *codec, hda_nid_t nid, const char *pfx,
  125. int chan, int dir)
  126. {
  127. char namestr[44];
  128. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  129. struct snd_kcontrol_new knew =
  130. HDA_CODEC_MUTE_MONO(namestr, nid, chan, 0, type);
  131. sprintf(namestr, "%s %s Switch", pfx, dirstr[dir]);
  132. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  133. }
  134. static int _add_volume(struct hda_codec *codec, hda_nid_t nid, const char *pfx,
  135. int chan, int dir)
  136. {
  137. char namestr[44];
  138. int type = dir ? HDA_INPUT : HDA_OUTPUT;
  139. struct snd_kcontrol_new knew =
  140. HDA_CODEC_VOLUME_MONO(namestr, nid, chan, 0, type);
  141. sprintf(namestr, "%s %s Volume", pfx, dirstr[dir]);
  142. return snd_hda_ctl_add(codec, nid, snd_ctl_new1(&knew, codec));
  143. }
  144. #define add_out_switch(codec, nid, pfx) _add_switch(codec, nid, pfx, 3, 0)
  145. #define add_out_volume(codec, nid, pfx) _add_volume(codec, nid, pfx, 3, 0)
  146. #define add_in_switch(codec, nid, pfx) _add_switch(codec, nid, pfx, 3, 1)
  147. #define add_in_volume(codec, nid, pfx) _add_volume(codec, nid, pfx, 3, 1)
  148. #define add_mono_switch(codec, nid, pfx, chan) \
  149. _add_switch(codec, nid, pfx, chan, 0)
  150. #define add_mono_volume(codec, nid, pfx, chan) \
  151. _add_volume(codec, nid, pfx, chan, 0)
  152. static int ca0110_build_controls(struct hda_codec *codec)
  153. {
  154. struct ca0110_spec *spec = codec->spec;
  155. struct auto_pin_cfg *cfg = &spec->autocfg;
  156. static const char * const prefix[AUTO_CFG_MAX_OUTS] = {
  157. "Front", "Surround", NULL, "Side", "Multi"
  158. };
  159. hda_nid_t mutenid;
  160. int i, err;
  161. for (i = 0; i < spec->multiout.num_dacs; i++) {
  162. if (get_wcaps(codec, spec->out_pins[i]) & AC_WCAP_OUT_AMP)
  163. mutenid = spec->out_pins[i];
  164. else
  165. mutenid = spec->multiout.dac_nids[i];
  166. if (!prefix[i]) {
  167. err = add_mono_switch(codec, mutenid,
  168. "Center", 1);
  169. if (err < 0)
  170. return err;
  171. err = add_mono_switch(codec, mutenid,
  172. "LFE", 1);
  173. if (err < 0)
  174. return err;
  175. err = add_mono_volume(codec, spec->multiout.dac_nids[i],
  176. "Center", 1);
  177. if (err < 0)
  178. return err;
  179. err = add_mono_volume(codec, spec->multiout.dac_nids[i],
  180. "LFE", 1);
  181. if (err < 0)
  182. return err;
  183. } else {
  184. err = add_out_switch(codec, mutenid,
  185. prefix[i]);
  186. if (err < 0)
  187. return err;
  188. err = add_out_volume(codec, spec->multiout.dac_nids[i],
  189. prefix[i]);
  190. if (err < 0)
  191. return err;
  192. }
  193. }
  194. if (cfg->hp_outs) {
  195. if (get_wcaps(codec, cfg->hp_pins[0]) & AC_WCAP_OUT_AMP)
  196. mutenid = cfg->hp_pins[0];
  197. else
  198. mutenid = spec->multiout.dac_nids[i];
  199. err = add_out_switch(codec, mutenid, "Headphone");
  200. if (err < 0)
  201. return err;
  202. if (spec->hp_dac) {
  203. err = add_out_volume(codec, spec->hp_dac, "Headphone");
  204. if (err < 0)
  205. return err;
  206. }
  207. }
  208. for (i = 0; i < spec->num_inputs; i++) {
  209. const char *label = spec->input_labels[i];
  210. if (get_wcaps(codec, spec->input_pins[i]) & AC_WCAP_IN_AMP)
  211. mutenid = spec->input_pins[i];
  212. else
  213. mutenid = spec->adcs[i];
  214. err = add_in_switch(codec, mutenid, label);
  215. if (err < 0)
  216. return err;
  217. err = add_in_volume(codec, spec->adcs[i], label);
  218. if (err < 0)
  219. return err;
  220. }
  221. if (spec->dig_out) {
  222. err = snd_hda_create_spdif_out_ctls(codec, spec->dig_out,
  223. spec->dig_out);
  224. if (err < 0)
  225. return err;
  226. err = snd_hda_create_spdif_share_sw(codec, &spec->multiout);
  227. if (err < 0)
  228. return err;
  229. spec->multiout.share_spdif = 1;
  230. }
  231. if (spec->dig_in) {
  232. err = snd_hda_create_spdif_in_ctls(codec, spec->dig_in);
  233. if (err < 0)
  234. return err;
  235. err = add_in_volume(codec, spec->dig_in, "IEC958");
  236. }
  237. return 0;
  238. }
  239. /*
  240. */
  241. static const struct hda_pcm_stream ca0110_pcm_analog_playback = {
  242. .substreams = 1,
  243. .channels_min = 2,
  244. .channels_max = 8,
  245. .ops = {
  246. .open = ca0110_playback_pcm_open,
  247. .prepare = ca0110_playback_pcm_prepare,
  248. .cleanup = ca0110_playback_pcm_cleanup
  249. },
  250. };
  251. static const struct hda_pcm_stream ca0110_pcm_analog_capture = {
  252. .substreams = 1,
  253. .channels_min = 2,
  254. .channels_max = 2,
  255. .ops = {
  256. .prepare = ca0110_capture_pcm_prepare,
  257. .cleanup = ca0110_capture_pcm_cleanup
  258. },
  259. };
  260. static const struct hda_pcm_stream ca0110_pcm_digital_playback = {
  261. .substreams = 1,
  262. .channels_min = 2,
  263. .channels_max = 2,
  264. .ops = {
  265. .open = ca0110_dig_playback_pcm_open,
  266. .close = ca0110_dig_playback_pcm_close,
  267. .prepare = ca0110_dig_playback_pcm_prepare
  268. },
  269. };
  270. static const struct hda_pcm_stream ca0110_pcm_digital_capture = {
  271. .substreams = 1,
  272. .channels_min = 2,
  273. .channels_max = 2,
  274. };
  275. static int ca0110_build_pcms(struct hda_codec *codec)
  276. {
  277. struct ca0110_spec *spec = codec->spec;
  278. struct hda_pcm *info = spec->pcm_rec;
  279. codec->pcm_info = info;
  280. codec->num_pcms = 0;
  281. info->name = "CA0110 Analog";
  282. info->stream[SNDRV_PCM_STREAM_PLAYBACK] = ca0110_pcm_analog_playback;
  283. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dacs[0];
  284. info->stream[SNDRV_PCM_STREAM_PLAYBACK].channels_max =
  285. spec->multiout.max_channels;
  286. info->stream[SNDRV_PCM_STREAM_CAPTURE] = ca0110_pcm_analog_capture;
  287. info->stream[SNDRV_PCM_STREAM_CAPTURE].substreams = spec->num_inputs;
  288. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->adcs[0];
  289. codec->num_pcms++;
  290. if (!spec->dig_out && !spec->dig_in)
  291. return 0;
  292. info++;
  293. info->name = "CA0110 Digital";
  294. info->pcm_type = HDA_PCM_TYPE_SPDIF;
  295. if (spec->dig_out) {
  296. info->stream[SNDRV_PCM_STREAM_PLAYBACK] =
  297. ca0110_pcm_digital_playback;
  298. info->stream[SNDRV_PCM_STREAM_PLAYBACK].nid = spec->dig_out;
  299. }
  300. if (spec->dig_in) {
  301. info->stream[SNDRV_PCM_STREAM_CAPTURE] =
  302. ca0110_pcm_digital_capture;
  303. info->stream[SNDRV_PCM_STREAM_CAPTURE].nid = spec->dig_in;
  304. }
  305. codec->num_pcms++;
  306. return 0;
  307. }
  308. static void init_output(struct hda_codec *codec, hda_nid_t pin, hda_nid_t dac)
  309. {
  310. if (pin) {
  311. snd_hda_codec_write(codec, pin, 0,
  312. AC_VERB_SET_PIN_WIDGET_CONTROL, 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_codec_write(codec, pin, 0,
  326. AC_VERB_SET_PIN_WIDGET_CONTROL, PIN_VREF80);
  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)