virtuoso.c 6.8 KB

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