virtuoso.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. static void pcm1796_write(struct oxygen *chip, unsigned int codec,
  129. u8 reg, u8 value)
  130. {
  131. /* maps ALSA channel pair number to SPI output */
  132. static const u8 codec_map[4] = {
  133. 0, 1, 2, 4
  134. };
  135. oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
  136. OXYGEN_SPI_DATA_LENGTH_2 |
  137. OXYGEN_SPI_CLOCK_160 |
  138. (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
  139. OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
  140. (reg << 8) | value);
  141. }
  142. static void xonar_init(struct oxygen *chip)
  143. {
  144. unsigned int i;
  145. for (i = 0; i < 4; ++i) {
  146. pcm1796_write(chip, i, 18, PCM1796_FMT_24_LJUST | PCM1796_ATLD);
  147. pcm1796_write(chip, i, 19, PCM1796_FLT_SHARP | PCM1796_ATS_1);
  148. pcm1796_write(chip, i, 20, PCM1796_OS_64);
  149. pcm1796_write(chip, i, 21, 0);
  150. pcm1796_write(chip, i, 16, 0xff); /* set ATL/ATR after ATLD */
  151. pcm1796_write(chip, i, 17, 0xff);
  152. }
  153. oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
  154. GPIO_CS5381_M_MASK | GPIO_ALT);
  155. oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
  156. GPIO_CS5381_M_SINGLE,
  157. GPIO_CS5381_M_MASK | GPIO_ALT);
  158. oxygen_ac97_set_bits(chip, 0, CM9780_JACK, CM9780_FMIC2MIC);
  159. oxygen_ac97_clear_bits(chip, 0, CM9780_GPIO_STATUS, GPIO_LINE_MUTE);
  160. msleep(300);
  161. oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_OUTPUT_ENABLE);
  162. oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE);
  163. snd_component_add(chip->card, "PCM1796");
  164. snd_component_add(chip->card, "CS5381");
  165. }
  166. static void xonar_cleanup(struct oxygen *chip)
  167. {
  168. oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_OUTPUT_ENABLE);
  169. }
  170. static void set_pcm1796_params(struct oxygen *chip,
  171. struct snd_pcm_hw_params *params)
  172. {
  173. #if 0
  174. unsigned int i;
  175. u8 value;
  176. value = params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64;
  177. for (i = 0; i < 4; ++i)
  178. pcm1796_write(chip, i, 20, value);
  179. #endif
  180. }
  181. static void update_pcm1796_volume(struct oxygen *chip)
  182. {
  183. unsigned int i;
  184. for (i = 0; i < 4; ++i) {
  185. pcm1796_write(chip, i, 16, chip->dac_volume[i * 2]);
  186. pcm1796_write(chip, i, 17, chip->dac_volume[i * 2 + 1]);
  187. }
  188. }
  189. static void update_pcm1796_mute(struct oxygen *chip)
  190. {
  191. unsigned int i;
  192. u8 value;
  193. value = PCM1796_FMT_24_LJUST | PCM1796_ATLD;
  194. if (chip->dac_mute)
  195. value |= PCM1796_MUTE;
  196. for (i = 0; i < 4; ++i)
  197. pcm1796_write(chip, i, 18, value);
  198. }
  199. static void set_cs5381_params(struct oxygen *chip,
  200. struct snd_pcm_hw_params *params)
  201. {
  202. unsigned int value;
  203. if (params_rate(params) <= 54000)
  204. value = GPIO_CS5381_M_SINGLE;
  205. else if (params_rate(params) <= 108000)
  206. value = GPIO_CS5381_M_DOUBLE;
  207. else
  208. value = GPIO_CS5381_M_QUAD;
  209. oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
  210. value, GPIO_CS5381_M_MASK);
  211. }
  212. static void mute_ac97_ctl(struct oxygen *chip, unsigned int control)
  213. {
  214. unsigned int index = chip->controls[control]->private_value & 0xff;
  215. u16 value;
  216. value = oxygen_read_ac97(chip, 0, index);
  217. if (!(value & 0x8000)) {
  218. oxygen_write_ac97(chip, 0, index, value | 0x8000);
  219. snd_ctl_notify(chip->card, SNDRV_CTL_EVENT_MASK_VALUE,
  220. &chip->controls[control]->id);
  221. }
  222. }
  223. static void xonar_ac97_switch_hook(struct oxygen *chip,
  224. unsigned int reg, int mute)
  225. {
  226. /* line-in is exclusive */
  227. switch (reg) {
  228. case AC97_LINE:
  229. oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
  230. mute ? GPIO_LINE_MUTE : 0,
  231. GPIO_LINE_MUTE);
  232. if (!mute) {
  233. mute_ac97_ctl(chip, CONTROL_MIC_CAPTURE_SWITCH);
  234. mute_ac97_ctl(chip, CONTROL_CD_CAPTURE_SWITCH);
  235. mute_ac97_ctl(chip, CONTROL_AUX_CAPTURE_SWITCH);
  236. }
  237. break;
  238. case AC97_MIC:
  239. case AC97_CD:
  240. case AC97_VIDEO:
  241. case AC97_AUX:
  242. if (!mute) {
  243. oxygen_ac97_set_bits(chip, 0, CM9780_GPIO_STATUS,
  244. GPIO_LINE_MUTE);
  245. mute_ac97_ctl(chip, CONTROL_LINE_CAPTURE_SWITCH);
  246. }
  247. break;
  248. }
  249. }
  250. static int pcm1796_volume_info(struct snd_kcontrol *ctl,
  251. struct snd_ctl_elem_info *info)
  252. {
  253. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  254. info->count = 8;
  255. info->value.integer.min = 0x0f;
  256. info->value.integer.max = 0xff;
  257. return 0;
  258. }
  259. static int alt_switch_get(struct snd_kcontrol *ctl,
  260. struct snd_ctl_elem_value *value)
  261. {
  262. struct oxygen *chip = ctl->private_data;
  263. value->value.integer.value[0] =
  264. !!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & GPIO_ALT);
  265. return 0;
  266. }
  267. static int alt_switch_put(struct snd_kcontrol *ctl,
  268. struct snd_ctl_elem_value *value)
  269. {
  270. struct oxygen *chip = ctl->private_data;
  271. u16 old_bits, new_bits;
  272. int changed;
  273. spin_lock_irq(&chip->reg_lock);
  274. old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA);
  275. if (value->value.integer.value[0])
  276. new_bits = old_bits | GPIO_ALT;
  277. else
  278. new_bits = old_bits & ~GPIO_ALT;
  279. changed = new_bits != old_bits;
  280. if (changed)
  281. oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits);
  282. spin_unlock_irq(&chip->reg_lock);
  283. return changed;
  284. }
  285. static const struct snd_kcontrol_new alt_switch = {
  286. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  287. .name = "Analog Loopback Switch",
  288. .info = snd_ctl_boolean_mono_info,
  289. .get = alt_switch_get,
  290. .put = alt_switch_put,
  291. };
  292. static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -12000, 50, 0);
  293. static int xonar_control_filter(struct snd_kcontrol_new *template)
  294. {
  295. if (!strcmp(template->name, "Master Playback Volume")) {
  296. template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  297. template->info = pcm1796_volume_info,
  298. template->tlv.p = pcm1796_db_scale;
  299. } else if (!strncmp(template->name, "CD Capture ", 11)) {
  300. /* CD in is actually connected to the video in pin */
  301. template->private_value ^= AC97_CD ^ AC97_VIDEO;
  302. } else if (!strcmp(template->name, "Line Capture Volume")) {
  303. return 1; /* line-in bypasses the AC'97 mixer */
  304. }
  305. return 0;
  306. }
  307. static int xonar_mixer_init(struct oxygen *chip)
  308. {
  309. return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
  310. }
  311. static const struct oxygen_model model_xonar = {
  312. .shortname = "Asus AV200",
  313. .longname = "Asus Virtuoso 200",
  314. .chip = "AV200",
  315. .init = xonar_init,
  316. .control_filter = xonar_control_filter,
  317. .mixer_init = xonar_mixer_init,
  318. .cleanup = xonar_cleanup,
  319. .set_dac_params = set_pcm1796_params,
  320. .set_adc_params = set_cs5381_params,
  321. .update_dac_volume = update_pcm1796_volume,
  322. .update_dac_mute = update_pcm1796_mute,
  323. .ac97_switch_hook = xonar_ac97_switch_hook,
  324. .dac_channels = 8,
  325. .used_channels = OXYGEN_CHANNEL_B |
  326. OXYGEN_CHANNEL_C |
  327. OXYGEN_CHANNEL_SPDIF |
  328. OXYGEN_CHANNEL_MULTICH,
  329. .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
  330. .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  331. .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  332. };
  333. static int __devinit xonar_probe(struct pci_dev *pci,
  334. const struct pci_device_id *pci_id)
  335. {
  336. static int dev;
  337. int err;
  338. if (dev >= SNDRV_CARDS)
  339. return -ENODEV;
  340. if (!enable[dev]) {
  341. ++dev;
  342. return -ENOENT;
  343. }
  344. err = oxygen_pci_probe(pci, index[dev], id[dev], 1, &model_xonar);
  345. if (err >= 0)
  346. ++dev;
  347. return err;
  348. }
  349. static struct pci_driver xonar_driver = {
  350. .name = "AV200",
  351. .id_table = xonar_ids,
  352. .probe = xonar_probe,
  353. .remove = __devexit_p(oxygen_pci_remove),
  354. };
  355. static int __init alsa_card_xonar_init(void)
  356. {
  357. return pci_register_driver(&xonar_driver);
  358. }
  359. static void __exit alsa_card_xonar_exit(void)
  360. {
  361. pci_unregister_driver(&xonar_driver);
  362. }
  363. module_init(alsa_card_xonar_init)
  364. module_exit(alsa_card_xonar_exit)