virtuoso.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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_WRITE |
  76. OXYGEN_SPI_DATA_LENGTH_2 |
  77. (codec_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
  78. OXYGEN_SPI_MAGIC,
  79. (reg << 8) | value);
  80. }
  81. static void xonar_init(struct oxygen *chip)
  82. {
  83. unsigned int i;
  84. for (i = 0; i < 4; ++i) {
  85. pcm1796_write(chip, i, 0x12, PCM1796_FMT_24_MSB | PCM1796_ATLD);
  86. pcm1796_write(chip, i, 0x13, 0);
  87. pcm1796_write(chip, i, 0x14, PCM1796_OS_64);
  88. pcm1796_write(chip, i, 0x15, 0);
  89. pcm1796_write(chip, i, 0x10, 0xff);
  90. pcm1796_write(chip, i, 0x11, 0xff);
  91. }
  92. oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, 0x8c);
  93. oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, 0x00, 0x8c);
  94. oxygen_ac97_set_bits(chip, 0, 0x62, 0x0080);
  95. msleep(300);
  96. oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, 0x100);
  97. oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, 0x100);
  98. snd_component_add(chip->card, "PCM1796");
  99. snd_component_add(chip->card, "CS5381");
  100. }
  101. static void xonar_cleanup(struct oxygen *chip)
  102. {
  103. oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, 0x100);
  104. }
  105. static void set_pcm1796_params(struct oxygen *chip,
  106. struct snd_pcm_hw_params *params)
  107. {
  108. #if 0
  109. unsigned int i;
  110. u8 value;
  111. value = params_rate(params) >= 96000 ? PCM1796_OS_32 : PCM1796_OS_64;
  112. for (i = 0; i < 4; ++i)
  113. pcm1796_write(chip, i, 0x14, value);
  114. #endif
  115. }
  116. static void update_pcm1796_volume(struct oxygen *chip)
  117. {
  118. unsigned int i;
  119. for (i = 0; i < 4; ++i) {
  120. pcm1796_write(chip, i, 0x10, chip->dac_volume[i * 2]);
  121. pcm1796_write(chip, i, 0x11, chip->dac_volume[i * 2 + 1]);
  122. }
  123. }
  124. static void update_pcm1796_mute(struct oxygen *chip)
  125. {
  126. unsigned int i;
  127. u8 value;
  128. value = PCM1796_FMT_24_MSB | PCM1796_ATLD;
  129. if (chip->dac_mute)
  130. value |= PCM1796_MUTE;
  131. for (i = 0; i < 4; ++i)
  132. pcm1796_write(chip, i, 0x12, value);
  133. }
  134. static void set_cs5381_params(struct oxygen *chip,
  135. struct snd_pcm_hw_params *params)
  136. {
  137. unsigned int value;
  138. if (params_rate(params) <= 54000)
  139. value = 0;
  140. else if (params_rate(params) <= 108000)
  141. value = 4;
  142. else
  143. value = 8;
  144. oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, value, 0x000c);
  145. }
  146. static int pcm1796_volume_info(struct snd_kcontrol *ctl,
  147. struct snd_ctl_elem_info *info)
  148. {
  149. info->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
  150. info->count = 8;
  151. info->value.integer.min = 0x0f;
  152. info->value.integer.max = 0xff;
  153. return 0;
  154. }
  155. static int alt_switch_get(struct snd_kcontrol *ctl,
  156. struct snd_ctl_elem_value *value)
  157. {
  158. struct oxygen *chip = ctl->private_data;
  159. value->value.integer.value[0] =
  160. !!(oxygen_read16(chip, OXYGEN_GPIO_DATA) & 0x80);
  161. return 0;
  162. }
  163. static int alt_switch_put(struct snd_kcontrol *ctl,
  164. struct snd_ctl_elem_value *value)
  165. {
  166. struct oxygen *chip = ctl->private_data;
  167. u16 old_bits, new_bits;
  168. int changed;
  169. spin_lock_irq(&chip->reg_lock);
  170. old_bits = oxygen_read16(chip, OXYGEN_GPIO_DATA);
  171. if (value->value.integer.value[0])
  172. new_bits = old_bits | 0x80;
  173. else
  174. new_bits = old_bits & ~0x80;
  175. changed = new_bits != old_bits;
  176. if (changed)
  177. oxygen_write16(chip, OXYGEN_GPIO_DATA, new_bits);
  178. spin_unlock_irq(&chip->reg_lock);
  179. return changed;
  180. }
  181. static const struct snd_kcontrol_new alt_switch = {
  182. .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
  183. .name = "Analog Loopback Switch",
  184. .info = snd_ctl_boolean_mono_info,
  185. .get = alt_switch_get,
  186. .put = alt_switch_put,
  187. };
  188. static const DECLARE_TLV_DB_SCALE(pcm1796_db_scale, -12000, 50, 0);
  189. static int xonar_control_filter(struct snd_kcontrol_new *template)
  190. {
  191. if (!strcmp(template->name, "Master Playback Volume")) {
  192. template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  193. template->info = pcm1796_volume_info,
  194. template->tlv.p = pcm1796_db_scale;
  195. } else if (!strncmp(template->name, "CD Capture ", 11)) {
  196. template->private_value ^= AC97_CD ^ AC97_VIDEO;
  197. }
  198. return 0;
  199. }
  200. static int xonar_mixer_init(struct oxygen *chip)
  201. {
  202. return snd_ctl_add(chip->card, snd_ctl_new1(&alt_switch, chip));
  203. }
  204. static const struct oxygen_model model_xonar = {
  205. .shortname = "Asus AV200",
  206. .longname = "Asus Virtuoso 200",
  207. .chip = "AV200",
  208. .init = xonar_init,
  209. .control_filter = xonar_control_filter,
  210. .mixer_init = xonar_mixer_init,
  211. .cleanup = xonar_cleanup,
  212. .set_dac_params = set_pcm1796_params,
  213. .set_adc_params = set_cs5381_params,
  214. .update_dac_volume = update_pcm1796_volume,
  215. .update_dac_mute = update_pcm1796_mute,
  216. .used_channels = OXYGEN_CHANNEL_B |
  217. OXYGEN_CHANNEL_C |
  218. OXYGEN_CHANNEL_SPDIF |
  219. OXYGEN_CHANNEL_MULTICH,
  220. .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
  221. .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  222. .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  223. };
  224. static int __devinit xonar_probe(struct pci_dev *pci,
  225. const struct pci_device_id *pci_id)
  226. {
  227. static int dev;
  228. int err;
  229. if (dev >= SNDRV_CARDS)
  230. return -ENODEV;
  231. if (!enable[dev]) {
  232. ++dev;
  233. return -ENOENT;
  234. }
  235. err = oxygen_pci_probe(pci, index[dev], id[dev], &model_xonar);
  236. if (err >= 0)
  237. ++dev;
  238. return err;
  239. }
  240. static struct pci_driver xonar_driver = {
  241. .name = "AV200",
  242. .id_table = xonar_ids,
  243. .probe = xonar_probe,
  244. .remove = __devexit_p(oxygen_pci_remove),
  245. };
  246. static int __init alsa_card_xonar_init(void)
  247. {
  248. return pci_register_driver(&xonar_driver);
  249. }
  250. static void __exit alsa_card_xonar_exit(void)
  251. {
  252. pci_unregister_driver(&xonar_driver);
  253. }
  254. module_init(alsa_card_xonar_init)
  255. module_exit(alsa_card_xonar_exit)