oxygen.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * C-Media CMI8788 driver for C-Media's reference design and for the X-Meridian
  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 AK4396 (front)
  21. * SPI 1 -> 2nd AK4396 (surround)
  22. * SPI 2 -> 3rd AK4396 (center/LFE)
  23. * SPI 3 -> WM8785
  24. * SPI 4 -> 4th AK4396 (back)
  25. *
  26. * GPIO 0 -> DFS0 of AK5385
  27. * GPIO 1 -> DFS1 of AK5385
  28. */
  29. #include <linux/pci.h>
  30. #include <sound/control.h>
  31. #include <sound/core.h>
  32. #include <sound/initval.h>
  33. #include <sound/pcm.h>
  34. #include <sound/pcm_params.h>
  35. #include <sound/tlv.h>
  36. #include "oxygen.h"
  37. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  38. MODULE_DESCRIPTION("C-Media CMI8788 driver");
  39. MODULE_LICENSE("GPL");
  40. MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8788}}");
  41. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  42. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  43. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  44. module_param_array(index, int, NULL, 0444);
  45. MODULE_PARM_DESC(index, "card index");
  46. module_param_array(id, charp, NULL, 0444);
  47. MODULE_PARM_DESC(id, "ID string");
  48. module_param_array(enable, bool, NULL, 0444);
  49. MODULE_PARM_DESC(enable, "enable card");
  50. static struct pci_device_id oxygen_ids[] __devinitdata = {
  51. { OXYGEN_PCI_SUBID(0x10b0, 0x0216) },
  52. { OXYGEN_PCI_SUBID(0x10b0, 0x0218) },
  53. { OXYGEN_PCI_SUBID(0x10b0, 0x0219) },
  54. { OXYGEN_PCI_SUBID(0x13f6, 0x0001) },
  55. { OXYGEN_PCI_SUBID(0x13f6, 0x0010) },
  56. { OXYGEN_PCI_SUBID(0x13f6, 0x8788) },
  57. { OXYGEN_PCI_SUBID(0x147a, 0xa017) },
  58. { OXYGEN_PCI_SUBID(0x14c3, 0x1710) },
  59. { OXYGEN_PCI_SUBID(0x14c3, 0x1711) },
  60. { OXYGEN_PCI_SUBID(0x1a58, 0x0910) },
  61. { OXYGEN_PCI_SUBID(0x415a, 0x5431), .driver_data = 1 },
  62. { OXYGEN_PCI_SUBID(0x7284, 0x9761) },
  63. { }
  64. };
  65. MODULE_DEVICE_TABLE(pci, oxygen_ids);
  66. #define AK4396_WRITE 0x2000
  67. /* register 0 */
  68. #define AK4396_RSTN 0x01
  69. #define AK4396_DIF_24_MSB 0x04
  70. /* register 1 */
  71. #define AK4396_SMUTE 0x01
  72. #define AK4396_DEM_OFF 0x02
  73. #define AK4396_DFS_MASK 0x18
  74. #define AK4396_DFS_NORMAL 0x00
  75. #define AK4396_DFS_DOUBLE 0x08
  76. #define AK4396_DFS_QUAD 0x10
  77. /* register 0 */
  78. #define WM8785_OSR_SINGLE 0x000
  79. #define WM8785_OSR_DOUBLE 0x008
  80. #define WM8785_OSR_QUAD 0x010
  81. #define WM8785_FORMAT_LJUST 0x020
  82. #define WM8785_FORMAT_I2S 0x040
  83. /* register 1 */
  84. #define WM8785_WL_16 0x000
  85. #define WM8785_WL_20 0x001
  86. #define WM8785_WL_24 0x002
  87. #define WM8785_WL_32 0x003
  88. static void ak4396_write(struct oxygen *chip, unsigned int codec,
  89. u8 reg, u8 value)
  90. {
  91. /* maps ALSA channel pair number to SPI output */
  92. static const u8 codec_spi_map[4] = {
  93. 0, 1, 2, 4
  94. };
  95. oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER_WRITE |
  96. OXYGEN_SPI_DATA_LENGTH_2 |
  97. (codec_spi_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
  98. OXYGEN_SPI_MAGIC,
  99. AK4396_WRITE | (reg << 8) | value);
  100. }
  101. static void wm8785_write(struct oxygen *chip, u8 reg, unsigned int value)
  102. {
  103. oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER_WRITE |
  104. OXYGEN_SPI_DATA_LENGTH_2 |
  105. (3 << OXYGEN_SPI_CODEC_SHIFT),
  106. (reg << 9) | value);
  107. }
  108. static void ak4396_init(struct oxygen *chip)
  109. {
  110. unsigned int i;
  111. chip->ak4396_reg1 = AK4396_DEM_OFF | AK4396_DFS_NORMAL;
  112. for (i = 0; i < 4; ++i) {
  113. ak4396_write(chip, i, 0, AK4396_DIF_24_MSB | AK4396_RSTN);
  114. ak4396_write(chip, i, 1, chip->ak4396_reg1);
  115. ak4396_write(chip, i, 2, 0);
  116. ak4396_write(chip, i, 3, 0xff);
  117. ak4396_write(chip, i, 4, 0xff);
  118. }
  119. snd_component_add(chip->card, "AK4396");
  120. }
  121. static void ak5385_init(struct oxygen *chip)
  122. {
  123. oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, 0x0003);
  124. oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, 0x0003);
  125. snd_component_add(chip->card, "AK5385");
  126. }
  127. static void wm8785_init(struct oxygen *chip)
  128. {
  129. wm8785_write(chip, 7, 0);
  130. wm8785_write(chip, 0, WM8785_FORMAT_LJUST | WM8785_OSR_SINGLE);
  131. wm8785_write(chip, 1, WM8785_WL_24);
  132. snd_component_add(chip->card, "WM8785");
  133. }
  134. static void generic_init(struct oxygen *chip)
  135. {
  136. ak4396_init(chip);
  137. wm8785_init(chip);
  138. }
  139. static void meridian_init(struct oxygen *chip)
  140. {
  141. ak4396_init(chip);
  142. ak5385_init(chip);
  143. }
  144. static void generic_cleanup(struct oxygen *chip)
  145. {
  146. }
  147. static void set_ak4396_params(struct oxygen *chip,
  148. struct snd_pcm_hw_params *params)
  149. {
  150. unsigned int i;
  151. u8 value;
  152. value = chip->ak4396_reg1 & ~AK4396_DFS_MASK;
  153. if (params_rate(params) <= 54000)
  154. value |= AK4396_DFS_NORMAL;
  155. else if (params_rate(params) < 120000)
  156. value |= AK4396_DFS_DOUBLE;
  157. else
  158. value |= AK4396_DFS_QUAD;
  159. chip->ak4396_reg1 = value;
  160. for (i = 0; i < 4; ++i) {
  161. ak4396_write(chip, i, 0, AK4396_DIF_24_MSB);
  162. ak4396_write(chip, i, 1, value);
  163. ak4396_write(chip, i, 0, AK4396_DIF_24_MSB | AK4396_RSTN);
  164. }
  165. }
  166. static void update_ak4396_volume(struct oxygen *chip)
  167. {
  168. unsigned int i;
  169. for (i = 0; i < 4; ++i) {
  170. ak4396_write(chip, i, 3, chip->dac_volume[i * 2]);
  171. ak4396_write(chip, i, 4, chip->dac_volume[i * 2 + 1]);
  172. }
  173. }
  174. static void update_ak4396_mute(struct oxygen *chip)
  175. {
  176. unsigned int i;
  177. u8 value;
  178. value = chip->ak4396_reg1 & ~AK4396_SMUTE;
  179. if (chip->dac_mute)
  180. value |= AK4396_SMUTE;
  181. for (i = 0; i < 4; ++i)
  182. ak4396_write(chip, i, 1, value);
  183. }
  184. static void set_wm8785_params(struct oxygen *chip,
  185. struct snd_pcm_hw_params *params)
  186. {
  187. unsigned int value;
  188. wm8785_write(chip, 7, 0);
  189. value = WM8785_FORMAT_LJUST;
  190. if (params_rate(params) == 96000)
  191. value |= WM8785_OSR_DOUBLE;
  192. else if (params_rate(params) == 192000)
  193. value |= WM8785_OSR_QUAD;
  194. else
  195. value |= WM8785_OSR_SINGLE;
  196. wm8785_write(chip, 0, value);
  197. if (snd_pcm_format_width(params_format(params)) <= 16)
  198. value = WM8785_WL_16;
  199. else
  200. value = WM8785_WL_24;
  201. wm8785_write(chip, 1, value);
  202. }
  203. static void set_ak5385_params(struct oxygen *chip,
  204. struct snd_pcm_hw_params *params)
  205. {
  206. unsigned int value;
  207. if (params_rate(params) <= 54000)
  208. value = 0;
  209. else if (params_rate(params) <= 108000)
  210. value = 1;
  211. else
  212. value = 2;
  213. oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, value, 0x0003);
  214. }
  215. static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0);
  216. static int ak4396_control_filter(struct snd_kcontrol_new *template)
  217. {
  218. if (!strcmp(template->name, "Master Playback Volume")) {
  219. template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  220. template->tlv.p = ak4396_db_scale;
  221. }
  222. return 0;
  223. }
  224. static const struct oxygen_model model_generic = {
  225. .shortname = "C-Media CMI8788",
  226. .longname = "C-Media Oxygen HD Audio",
  227. .chip = "CMI8788",
  228. .owner = THIS_MODULE,
  229. .init = generic_init,
  230. .control_filter = ak4396_control_filter,
  231. .cleanup = generic_cleanup,
  232. .set_dac_params = set_ak4396_params,
  233. .set_adc_params = set_wm8785_params,
  234. .update_dac_volume = update_ak4396_volume,
  235. .update_dac_mute = update_ak4396_mute,
  236. .used_channels = OXYGEN_CHANNEL_A |
  237. OXYGEN_CHANNEL_C |
  238. OXYGEN_CHANNEL_SPDIF |
  239. OXYGEN_CHANNEL_MULTICH |
  240. OXYGEN_CHANNEL_AC97,
  241. .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
  242. };
  243. static const struct oxygen_model model_meridian = {
  244. .shortname = "C-Media CMI8788",
  245. .longname = "C-Media Oxygen HD Audio",
  246. .chip = "CMI8788",
  247. .owner = THIS_MODULE,
  248. .init = meridian_init,
  249. .control_filter = ak4396_control_filter,
  250. .cleanup = generic_cleanup,
  251. .set_dac_params = set_ak4396_params,
  252. .set_adc_params = set_ak5385_params,
  253. .update_dac_volume = update_ak4396_volume,
  254. .update_dac_mute = update_ak4396_mute,
  255. .used_channels = OXYGEN_CHANNEL_B |
  256. OXYGEN_CHANNEL_C |
  257. OXYGEN_CHANNEL_SPDIF |
  258. OXYGEN_CHANNEL_MULTICH |
  259. OXYGEN_CHANNEL_AC97,
  260. .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
  261. };
  262. static int __devinit generic_oxygen_probe(struct pci_dev *pci,
  263. const struct pci_device_id *pci_id)
  264. {
  265. static int dev;
  266. const struct oxygen_model *model;
  267. int err;
  268. if (dev >= SNDRV_CARDS)
  269. return -ENODEV;
  270. if (!enable[dev]) {
  271. ++dev;
  272. return -ENOENT;
  273. }
  274. model = pci_id->driver_data ? &model_meridian : &model_generic;
  275. err = oxygen_pci_probe(pci, index[dev], id[dev], model);
  276. if (err >= 0)
  277. ++dev;
  278. return err;
  279. }
  280. static struct pci_driver oxygen_driver = {
  281. .name = "CMI8788",
  282. .id_table = oxygen_ids,
  283. .probe = generic_oxygen_probe,
  284. .remove = __devexit_p(oxygen_pci_remove),
  285. };
  286. static int __init alsa_card_oxygen_init(void)
  287. {
  288. return pci_register_driver(&oxygen_driver);
  289. }
  290. static void __exit alsa_card_oxygen_exit(void)
  291. {
  292. pci_unregister_driver(&oxygen_driver);
  293. }
  294. module_init(alsa_card_oxygen_init)
  295. module_exit(alsa_card_oxygen_exit)