virtuoso.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * C-Media CMI8788 driver for Asus Xonar cards
  3. *
  4. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  5. *
  6. *
  7. * This driver is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License, version 2.
  9. *
  10. * This driver is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this driver; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /*
  20. * CMI8788:
  21. *
  22. * SPI 0 -> 1st PCM1796 (front)
  23. * SPI 1 -> 2nd PCM1796 (surround)
  24. * SPI 2 -> 3rd PCM1796 (center/LFE)
  25. * SPI 4 -> 4th PCM1796 (back)
  26. *
  27. * GPIO 2 -> M0 of CS5381
  28. * GPIO 3 -> M1 of CS5381
  29. * GPIO 5 <- external power present (D2X only)
  30. * GPIO 7 -> ALT
  31. * GPIO 8 -> enable output to speakers
  32. *
  33. * CM9780:
  34. *
  35. * GPIO 0 -> enable AC'97 bypass (line in -> ADC)
  36. */
  37. #include <linux/pci.h>
  38. #include <linux/delay.h>
  39. #include <linux/mutex.h>
  40. #include <sound/ac97_codec.h>
  41. #include <sound/control.h>
  42. #include <sound/core.h>
  43. #include <sound/initval.h>
  44. #include <sound/pcm.h>
  45. #include <sound/tlv.h>
  46. #include "oxygen.h"
  47. #include "cm9780.h"
  48. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  49. MODULE_DESCRIPTION("Asus AV200 driver");
  50. MODULE_LICENSE("GPL");
  51. MODULE_SUPPORTED_DEVICE("{{Asus,AV200}}");
  52. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  53. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  54. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  55. module_param_array(index, int, NULL, 0444);
  56. MODULE_PARM_DESC(index, "card index");
  57. module_param_array(id, charp, NULL, 0444);
  58. MODULE_PARM_DESC(id, "ID string");
  59. module_param_array(enable, bool, NULL, 0444);
  60. MODULE_PARM_DESC(enable, "enable card");
  61. static struct pci_device_id xonar_ids[] __devinitdata = {
  62. { OXYGEN_PCI_SUBID(0x1043, 0x8269) }, /* Asus Xonar D2 */
  63. { OXYGEN_PCI_SUBID(0x1043, 0x82b7) }, /* Asus Xonar D2X */
  64. { }
  65. };
  66. MODULE_DEVICE_TABLE(pci, xonar_ids);
  67. #define GPIO_CS5381_M_MASK 0x000c
  68. #define GPIO_CS5381_M_SINGLE 0x0000
  69. #define GPIO_CS5381_M_DOUBLE 0x0004
  70. #define GPIO_CS5381_M_QUAD 0x0008
  71. #define GPIO_EXT_POWER 0x0020
  72. #define GPIO_ALT 0x0080
  73. #define GPIO_OUTPUT_ENABLE 0x0100
  74. #define GPIO_LINE_MUTE CM9780_GPO0
  75. /* register 16 */
  76. #define PCM1796_ATL_MASK 0xff
  77. /* register 17 */
  78. #define PCM1796_ATR_MASK 0xff
  79. /* register 18 */
  80. #define PCM1796_MUTE 0x01
  81. #define PCM1796_DME 0x02
  82. #define PCM1796_DMF_MASK 0x0c
  83. #define PCM1796_DMF_DISABLED 0x00
  84. #define PCM1796_DMF_48 0x04
  85. #define PCM1796_DMF_441 0x08
  86. #define PCM1796_DMF_32 0x0c
  87. #define PCM1796_FMT_MASK 0x70
  88. #define PCM1796_FMT_16_RJUST 0x00
  89. #define PCM1796_FMT_20_RJUST 0x10
  90. #define PCM1796_FMT_24_RJUST 0x20
  91. #define PCM1796_FMT_24_LJUST 0x30
  92. #define PCM1796_FMT_16_I2S 0x40
  93. #define PCM1796_FMT_24_I2S 0x50
  94. #define PCM1796_ATLD 0x80
  95. /* register 19 */
  96. #define PCM1796_INZD 0x01
  97. #define PCM1796_FLT_MASK 0x02
  98. #define PCM1796_FLT_SHARP 0x00
  99. #define PCM1796_FLT_SLOW 0x02
  100. #define PCM1796_DFMS 0x04
  101. #define PCM1796_OPE 0x10
  102. #define PCM1796_ATS_MASK 0x60
  103. #define PCM1796_ATS_1 0x00
  104. #define PCM1796_ATS_2 0x20
  105. #define PCM1796_ATS_4 0x40
  106. #define PCM1796_ATS_8 0x60
  107. #define PCM1796_REV 0x80
  108. /* register 20 */
  109. #define PCM1796_OS_MASK 0x03
  110. #define PCM1796_OS_64 0x00
  111. #define PCM1796_OS_32 0x01
  112. #define PCM1796_OS_128 0x02
  113. #define PCM1796_CHSL_MASK 0x04
  114. #define PCM1796_CHSL_LEFT 0x00
  115. #define PCM1796_CHSL_RIGHT 0x04
  116. #define PCM1796_MONO 0x08
  117. #define PCM1796_DFTH 0x10
  118. #define PCM1796_DSD 0x20
  119. #define PCM1796_SRST 0x40
  120. /* register 21 */
  121. #define PCM1796_PCMZ 0x01
  122. #define PCM1796_DZ_MASK 0x06
  123. /* register 22 */
  124. #define PCM1796_ZFGL 0x01
  125. #define PCM1796_ZFGR 0x02
  126. /* register 23 */
  127. #define PCM1796_ID_MASK 0x1f
  128. struct xonar_data {
  129. u8 is_d2x;
  130. u8 has_power;
  131. };
  132. static void pcm1796_write(struct oxygen *chip, unsigned int codec,
  133. u8 reg, u8 value)
  134. {
  135. /* maps ALSA channel pair number to SPI output */
  136. static const u8 codec_map[4] = {
  137. 0, 1, 2, 4
  138. };
  139. oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
  140. OXYGEN_SPI_DATA_LENGTH_2 |
  141. OXYGEN_SPI_CLOCK_160 |
  142. (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
  143. OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
  144. (reg << 8) | value);
  145. }
  146. static void xonar_init(struct oxygen *chip)
  147. {
  148. struct xonar_data *data = chip->model_data;
  149. unsigned int i;
  150. data->is_d2x = chip->pci->subsystem_device == 0x82b7;
  151. for (i = 0; i < 4; ++i) {
  152. pcm1796_write(chip, i, 18, PCM1796_FMT_24_LJUST | PCM1796_ATLD);
  153. pcm1796_write(chip, i, 19, PCM1796_FLT_SHARP | PCM1796_ATS_1);
  154. pcm1796_write(chip, i, 20, PCM1796_OS_64);
  155. pcm1796_write(chip, i, 21, 0);
  156. pcm1796_write(chip, i, 16, 0xff); /* set ATL/ATR after ATLD */
  157. pcm1796_write(chip, i, 17, 0xff);
  158. }
  159. oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
  160. GPIO_CS5381_M_MASK | GPIO_ALT);
  161. oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
  162. GPIO_CS5381_M_SINGLE,
  163. GPIO_CS5381_M_MASK | GPIO_ALT);
  164. if (data->is_d2x) {
  165. oxygen_clear_bits16(chip, OXYGEN_GPIO_CONTROL,
  166. GPIO_EXT_POWER);
  167. oxygen_set_bits16(chip, OXYGEN_GPIO_INTERRUPT_MASK,
  168. GPIO_EXT_POWER);
  169. chip->interrupt_mask |= OXYGEN_INT_GPIO;
  170. data->has_power = !!(oxygen_read16(chip, OXYGEN_GPIO_DATA)
  171. & GPIO_EXT_POWER);
  172. }
  173. oxygen_ac97_set_bits(chip, 0, CM9780_JACK, CM9780_FMIC2MIC);
  174. oxygen_ac97_clear_bits(chip, 0, CM9780_GPIO_STATUS, GPIO_LINE_MUTE);
  175. msleep(300);
  176. oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_OUTPUT_ENABLE);
  177. oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE);
  178. snd_component_add(chip->card, "PCM1796");
  179. snd_component_add(chip->card, "CS5381");
  180. }
  181. static void xonar_cleanup(struct oxygen *chip)
  182. {
  183. oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE);
  184. }
  185. static void set_pcm1796_params(struct oxygen *chip,
  186. struct snd_pcm_hw_params *params)
  187. {
  188. #if 0
  189. unsigned int i;
  190. u8 value;
  191. value = params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64;
  192. for (i = 0; i < 4; ++i)
  193. pcm1796_write(chip, i, 20, value);
  194. #endif
  195. }
  196. static void update_pcm1796_volume(struct oxygen *chip)
  197. {
  198. unsigned int i;
  199. for (i = 0; i < 4; ++i) {
  200. pcm1796_write(chip, i, 16, chip->dac_volume[i * 2]);
  201. pcm1796_write(chip, i, 17, chip->dac_volume[i * 2 + 1]);
  202. }
  203. }
  204. static void update_pcm1796_mute(struct oxygen *chip)
  205. {
  206. unsigned int i;
  207. u8 value;
  208. value = PCM1796_FMT_24_LJUST | PCM1796_ATLD;
  209. if (chip->dac_mute)
  210. value |= PCM1796_MUTE;
  211. for (i = 0; i < 4; ++i)
  212. pcm1796_write(chip, i, 18, value);
  213. }
  214. static void set_cs5381_params(struct oxygen *chip,
  215. struct snd_pcm_hw_params *params)
  216. {
  217. unsigned int value;
  218. if (params_rate(params) <= 54000)
  219. value = GPIO_CS5381_M_SINGLE;
  220. else if (params_rate(params) <= 108000)
  221. value = GPIO_CS5381_M_DOUBLE;
  222. else
  223. value = GPIO_CS5381_M_QUAD;
  224. oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
  225. value, GPIO_CS5381_M_MASK);
  226. }
  227. static void xonar_gpio_changed(struct oxygen *chip)
  228. {
  229. struct xonar_data *data = chip->model_data;
  230. u8 has_power;
  231. if (!data->is_d2x)
  232. return;
  233. has_power = !!(oxygen_read16(chip, OXYGEN_GPIO_DATA)
  234. & GPIO_EXT_POWER);
  235. if (has_power != data->has_power) {
  236. data->has_power = has_power;
  237. if (has_power) {
  238. snd_printk(KERN_NOTICE "power restored\n");
  239. } else {
  240. snd_printk(KERN_CRIT
  241. "Hey! Don't unplug the power cable!\n");
  242. /* TODO: stop PCMs */
  243. }
  244. }
  245. }
  246. static void mute_ac97_ctl(struct oxygen *chip, unsigned int control)
  247. {
  248. unsigned int index = chip->controls[control]->private_value & 0xff;
  249. u16 value;
  250. value = oxygen_read_ac97(chip, 0, index);
  251. if (!(value & 0x8000)) {
  252. oxygen_write_ac97(chip, 0, index, value | 0x8000);
  253. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  254. &chip->controls[control]->id);
  255. }
  256. }
  257. static void xonar_ac97_switch_hook(struct oxygen *chip, unsigned int codec,
  258. unsigned int reg, int mute)
  259. {
  260. if (codec != 0)
  261. return;
  262. /* line-in is exclusive */
  263. switch (reg) {
  264. case AC97_LINE:
  265. oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
  266. mute ? GPIO_LINE_MUTE : 0,
  267. GPIO_LINE_MUTE);
  268. if (!mute) {
  269. mute_ac97_ctl(chip, CONTROL_MIC_CAPTURE_SWITCH);
  270. mute_ac97_ctl(chip, CONTROL_CD_CAPTURE_SWITCH);
  271. mute_ac97_ctl(chip, CONTROL_AUX_CAPTURE_SWITCH);
  272. }
  273. break;
  274. case AC97_MIC:
  275. case AC97_CD:
  276. case AC97_VIDEO:
  277. case AC97_AUX:
  278. if (!mute) {
  279. oxygen_ac97_set_bits(chip, 0, CM9780_GPIO_STATUS,
  280. GPIO_LINE_MUTE);
  281. mute_ac97_ctl(chip, CONTROL_LINE_CAPTURE_SWITCH);
  282. }
  283. break;
  284. }
  285. }
  286. static int pcm1796_volume_info(struct snd_kcontrol *ctl,
  287. struct snd_ctl_elem_info *info)
  288. {
  289. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  290. info->count = 8;
  291. info->value.integer.min = 0x0f;
  292. info->value.integer.max = 0xff;
  293. return 0;
  294. }
  295. static int alt_switch_get(struct snd_kcontrol *ctl,
  296. struct snd_ctl_elem_value *value)
  297. {
  298. struct oxygen *chip = ctl->private_data;
  299. value->value.integer.value[0] =
  300. !!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_ALT);
  301. return 0;
  302. }
  303. static int alt_switch_put(struct snd_kcontrol *ctl,
  304. struct snd_ctl_elem_value *value)
  305. {
  306. struct oxygen *chip = ctl->private_data;
  307. u16 old_bits, new_bits;
  308. int changed;
  309. spin_lock_irq(&chip->reg_lock);
  310. old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA);
  311. if (value->value.integer.value[0])
  312. new_bits = old_bits | GPIO_ALT;
  313. else
  314. new_bits = old_bits & ~GPIO_ALT;
  315. changed = new_bits != old_bits;
  316. if (changed)
  317. oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits);
  318. spin_unlock_irq(&chip->reg_lock);
  319. return changed;
  320. }
  321. static const struct snd_kcontrol_new alt_switch = {
  322. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  323. .name = "Analog Loopback Switch",
  324. .info = snd_ctl_boolean_mono_info,
  325. .get = alt_switch_get,
  326. .put = alt_switch_put,
  327. };
  328. static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -12000, 50, 0);
  329. static int xonar_control_filter(struct snd_kcontrol_new *template)
  330. {
  331. if (!strcmp(template->name, "Master Playback Volume")) {
  332. template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  333. template->info = pcm1796_volume_info,
  334. template->tlv.p = pcm1796_db_scale;
  335. } else if (!strncmp(template->name, "CD Capture ", 11)) {
  336. /* CD in is actually connected to the video in pin */
  337. template->private_value ^= AC97_CD ^ AC97_VIDEO;
  338. } else if (!strcmp(template->name, "Line Capture Volume")) {
  339. return 1; /* line-in bypasses the AC'97 mixer */
  340. }
  341. return 0;
  342. }
  343. static int xonar_mixer_init(struct oxygen *chip)
  344. {
  345. return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
  346. }
  347. static const struct oxygen_model model_xonar = {
  348. .shortname = "Asus AV200",
  349. .longname = "Asus Virtuoso 200",
  350. .chip = "AV200",
  351. .owner = THIS_MODULE,
  352. .init = xonar_init,
  353. .control_filter = xonar_control_filter,
  354. .mixer_init = xonar_mixer_init,
  355. .cleanup = xonar_cleanup,
  356. .set_dac_params = set_pcm1796_params,
  357. .set_adc_params = set_cs5381_params,
  358. .update_dac_volume = update_pcm1796_volume,
  359. .update_dac_mute = update_pcm1796_mute,
  360. .ac97_switch_hook = xonar_ac97_switch_hook,
  361. .gpio_changed = xonar_gpio_changed,
  362. .model_data_size = sizeof(struct xonar_data),
  363. .dac_channels = 8,
  364. .used_channels = OXYGEN_CHANNEL_B |
  365. OXYGEN_CHANNEL_C |
  366. OXYGEN_CHANNEL_SPDIF |
  367. OXYGEN_CHANNEL_MULTICH,
  368. .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
  369. .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  370. .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  371. };
  372. static int __devinit xonar_probe(struct pci_dev *pci,
  373. const struct pci_device_id *pci_id)
  374. {
  375. static int dev;
  376. int err;
  377. if (dev >= SNDRV_CARDS)
  378. return -ENODEV;
  379. if (!enable[dev]) {
  380. ++dev;
  381. return -ENOENT;
  382. }
  383. err = oxygen_pci_probe(pci, index[dev], id[dev], 1, &model_xonar);
  384. if (err >= 0)
  385. ++dev;
  386. return err;
  387. }
  388. static struct pci_driver xonar_driver = {
  389. .name = "AV200",
  390. .id_table = xonar_ids,
  391. .probe = xonar_probe,
  392. .remove = __devexit_p(oxygen_pci_remove),
  393. };
  394. static int __init alsa_card_xonar_init(void)
  395. {
  396. return pci_register_driver(&xonar_driver);
  397. }
  398. static void __exit alsa_card_xonar_exit(void)
  399. {
  400. pci_unregister_driver(&xonar_driver);
  401. }
  402. module_init(alsa_card_xonar_init)
  403. module_exit(alsa_card_xonar_exit)