virtuoso.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. * SPI 0 -> 1st PCM1796 (front)
  21. * SPI 1 -> 2nd PCM1796 (surround)
  22. * SPI 2 -> 3rd PCM1796 (center/LFE)
  23. * SPI 4 -> 4th PCM1796 (back)
  24. *
  25. * GPIO 2 -> M0 of CS5381
  26. * GPIO 3 -> M1 of CS5381
  27. * GPIO 5 <- ? (D2X only)
  28. * GPIO 7 -> ALT
  29. * GPIO 8 -> ? (amps enable?)
  30. */
  31. #include <linux/pci.h>
  32. #include <linux/delay.h>
  33. #include <linux/mutex.h>
  34. #include <sound/ac97_codec.h>
  35. #include <sound/control.h>
  36. #include <sound/core.h>
  37. #include <sound/initval.h>
  38. #include <sound/pcm.h>
  39. #include <sound/tlv.h>
  40. #include "oxygen.h"
  41. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  42. MODULE_DESCRIPTION("Asus AV200 driver");
  43. MODULE_LICENSE("GPL");
  44. MODULE_SUPPORTED_DEVICE("{{Asus,AV200}}");
  45. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  46. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  47. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  48. module_param_array(index, int, NULL, 0444);
  49. MODULE_PARM_DESC(index, "card index");
  50. module_param_array(id, charp, NULL, 0444);
  51. MODULE_PARM_DESC(id, "ID string");
  52. module_param_array(enable, bool, NULL, 0444);
  53. MODULE_PARM_DESC(enable, "enable card");
  54. static struct pci_device_id xonar_ids[] __devinitdata = {
  55. { OXYGEN_PCI_SUBID(0x1043, 0x8269) }, /* Asus Xonar D2 */
  56. { OXYGEN_PCI_SUBID(0x1043, 0x82b7) }, /* Asus Xonar D2X */
  57. { }
  58. };
  59. MODULE_DEVICE_TABLE(pci, xonar_ids);
  60. /* register 0x12 */
  61. #define PCM1796_MUTE 0x01
  62. #define PCM1796_FMT_24_MSB 0x30
  63. #define PCM1796_ATLD 0x80
  64. /* register 0x14 */
  65. #define PCM1796_OS_64 0x00
  66. #define PCM1796_OS_32 0x01
  67. #define PCM1796_OS_128 0x02
  68. static void pcm1796_write(struct oxygen *chip, unsigned int codec,
  69. u8 reg, u8 value)
  70. {
  71. /* maps ALSA channel pair number to SPI output */
  72. static const u8 codec_map[4] = {
  73. 0, 1, 2, 4
  74. };
  75. oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
  76. OXYGEN_SPI_DATA_LENGTH_2 |
  77. OXYGEN_SPI_CLOCK_160 |
  78. (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
  79. OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
  80. (reg << 8) | value);
  81. }
  82. static void xonar_init(struct oxygen *chip)
  83. {
  84. unsigned int i;
  85. for (i = 0; i < 4; ++i) {
  86. pcm1796_write(chip, i, 0x12, PCM1796_FMT_24_MSB | PCM1796_ATLD);
  87. pcm1796_write(chip, i, 0x13, 0);
  88. pcm1796_write(chip, i, 0x14, PCM1796_OS_64);
  89. pcm1796_write(chip, i, 0x15, 0);
  90. pcm1796_write(chip, i, 0x10, 0xff);
  91. pcm1796_write(chip, i, 0x11, 0xff);
  92. }
  93. oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, 0x8c);
  94. oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, 0x00, 0x8c);
  95. oxygen_ac97_set_bits(chip, 0, 0x62, 0x0080);
  96. msleep(300);
  97. oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, 0x100);
  98. oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, 0x100);
  99. snd_component_add(chip->card, "PCM1796");
  100. snd_component_add(chip->card, "CS5381");
  101. }
  102. static void xonar_cleanup(struct oxygen *chip)
  103. {
  104. oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, 0x100);
  105. }
  106. static void set_pcm1796_params(struct oxygen *chip,
  107. struct snd_pcm_hw_params *params)
  108. {
  109. #if 0
  110. unsigned int i;
  111. u8 value;
  112. value = params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64;
  113. for (i = 0; i < 4; ++i)
  114. pcm1796_write(chip, i, 0x14, value);
  115. #endif
  116. }
  117. static void update_pcm1796_volume(struct oxygen *chip)
  118. {
  119. unsigned int i;
  120. for (i = 0; i < 4; ++i) {
  121. pcm1796_write(chip, i, 0x10, chip->dac_volume[i * 2]);
  122. pcm1796_write(chip, i, 0x11, chip->dac_volume[i * 2 + 1]);
  123. }
  124. }
  125. static void update_pcm1796_mute(struct oxygen *chip)
  126. {
  127. unsigned int i;
  128. u8 value;
  129. value = PCM1796_FMT_24_MSB | PCM1796_ATLD;
  130. if (chip->dac_mute)
  131. value |= PCM1796_MUTE;
  132. for (i = 0; i < 4; ++i)
  133. pcm1796_write(chip, i, 0x12, value);
  134. }
  135. static void set_cs5381_params(struct oxygen *chip,
  136. struct snd_pcm_hw_params *params)
  137. {
  138. unsigned int value;
  139. if (params_rate(params) <= 54000)
  140. value = 0;
  141. else if (params_rate(params) <= 108000)
  142. value = 4;
  143. else
  144. value = 8;
  145. oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, value, 0x000c);
  146. }
  147. static int pcm1796_volume_info(struct snd_kcontrol *ctl,
  148. struct snd_ctl_elem_info *info)
  149. {
  150. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  151. info->count = 8;
  152. info->value.integer.min = 0x0f;
  153. info->value.integer.max = 0xff;
  154. return 0;
  155. }
  156. static int alt_switch_get(struct snd_kcontrol *ctl,
  157. struct snd_ctl_elem_value *value)
  158. {
  159. struct oxygen *chip = ctl->private_data;
  160. value->value.integer.value[0] =
  161. !!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & 0x80);
  162. return 0;
  163. }
  164. static int alt_switch_put(struct snd_kcontrol *ctl,
  165. struct snd_ctl_elem_value *value)
  166. {
  167. struct oxygen *chip = ctl->private_data;
  168. u16 old_bits, new_bits;
  169. int changed;
  170. spin_lock_irq(&chip->reg_lock);
  171. old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA);
  172. if (value->value.integer.value[0])
  173. new_bits = old_bits | 0x80;
  174. else
  175. new_bits = old_bits & ~0x80;
  176. changed = new_bits != old_bits;
  177. if (changed)
  178. oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits);
  179. spin_unlock_irq(&chip->reg_lock);
  180. return changed;
  181. }
  182. static const struct snd_kcontrol_new alt_switch = {
  183. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  184. .name = "Analog Loopback Switch",
  185. .info = snd_ctl_boolean_mono_info,
  186. .get = alt_switch_get,
  187. .put = alt_switch_put,
  188. };
  189. static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -12000, 50, 0);
  190. static int xonar_control_filter(struct snd_kcontrol_new *template)
  191. {
  192. if (!strcmp(template->name, "Master Playback Volume")) {
  193. template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  194. template->info = pcm1796_volume_info,
  195. template->tlv.p = pcm1796_db_scale;
  196. } else if (!strncmp(template->name, "CD Capture ", 11)) {
  197. template->private_value ^= AC97_CD ^ AC97_VIDEO;
  198. }
  199. return 0;
  200. }
  201. static int xonar_mixer_init(struct oxygen *chip)
  202. {
  203. return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
  204. }
  205. static const struct oxygen_model model_xonar = {
  206. .shortname = "Asus AV200",
  207. .longname = "Asus Virtuoso 200",
  208. .chip = "AV200",
  209. .init = xonar_init,
  210. .control_filter = xonar_control_filter,
  211. .mixer_init = xonar_mixer_init,
  212. .cleanup = xonar_cleanup,
  213. .set_dac_params = set_pcm1796_params,
  214. .set_adc_params = set_cs5381_params,
  215. .update_dac_volume = update_pcm1796_volume,
  216. .update_dac_mute = update_pcm1796_mute,
  217. .used_channels = OXYGEN_CHANNEL_B |
  218. OXYGEN_CHANNEL_C |
  219. OXYGEN_CHANNEL_SPDIF |
  220. OXYGEN_CHANNEL_MULTICH,
  221. .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
  222. .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  223. .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  224. };
  225. static int __devinit xonar_probe(struct pci_dev *pci,
  226. const struct pci_device_id *pci_id)
  227. {
  228. static int dev;
  229. int err;
  230. if (dev >= SNDRV_CARDS)
  231. return -ENODEV;
  232. if (!enable[dev]) {
  233. ++dev;
  234. return -ENOENT;
  235. }
  236. err = oxygen_pci_probe(pci, index[dev], id[dev], 1, &model_xonar);
  237. if (err >= 0)
  238. ++dev;
  239. return err;
  240. }
  241. static struct pci_driver xonar_driver = {
  242. .name = "AV200",
  243. .id_table = xonar_ids,
  244. .probe = xonar_probe,
  245. .remove = __devexit_p(oxygen_pci_remove),
  246. };
  247. static int __init alsa_card_xonar_init(void)
  248. {
  249. return pci_register_driver(&xonar_driver);
  250. }
  251. static void __exit alsa_card_xonar_exit(void)
  252. {
  253. pci_unregister_driver(&xonar_driver);
  254. }
  255. module_init(alsa_card_xonar_init)
  256. module_exit(alsa_card_xonar_exit)