patch_ca0110.c 15 KB

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