oxygen.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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/mutex.h>
  30. #include <linux/pci.h>
  31. #include <sound/ac97_codec.h>
  32. #include <sound/control.h>
  33. #include <sound/core.h>
  34. #include <sound/initval.h>
  35. #include <sound/pcm.h>
  36. #include <sound/pcm_params.h>
  37. #include <sound/tlv.h>
  38. #include "oxygen.h"
  39. #include "ak4396.h"
  40. #include "cm9780.h"
  41. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  42. MODULE_DESCRIPTION("C-Media CMI8788 driver");
  43. MODULE_LICENSE("GPL");
  44. MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8788}}");
  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 oxygen_ids[] __devinitdata = {
  55. { OXYGEN_PCI_SUBID(0x10b0, 0x0216) },
  56. { OXYGEN_PCI_SUBID(0x10b0, 0x0218) },
  57. { OXYGEN_PCI_SUBID(0x10b0, 0x0219) },
  58. { OXYGEN_PCI_SUBID(0x13f6, 0x0001) },
  59. { OXYGEN_PCI_SUBID(0x13f6, 0x0010) },
  60. { OXYGEN_PCI_SUBID(0x13f6, 0x8788) },
  61. { OXYGEN_PCI_SUBID(0x147a, 0xa017) },
  62. { OXYGEN_PCI_SUBID(0x1a58, 0x0910) },
  63. { OXYGEN_PCI_SUBID(0x415a, 0x5431), .driver_data = 1 },
  64. { OXYGEN_PCI_SUBID(0x7284, 0x9761) },
  65. { }
  66. };
  67. MODULE_DEVICE_TABLE(pci, oxygen_ids);
  68. #define GPIO_AK5385_DFS_MASK 0x0003
  69. #define GPIO_AK5385_DFS_NORMAL 0x0000
  70. #define GPIO_AK5385_DFS_DOUBLE 0x0001
  71. #define GPIO_AK5385_DFS_QUAD 0x0002
  72. #define GPIO_LINE_MUTE CM9780_GPO0
  73. #define WM8785_R0 0
  74. #define WM8785_R1 1
  75. #define WM8785_R2 2
  76. #define WM8785_R7 7
  77. /* R0 */
  78. #define WM8785_MCR_MASK 0x007
  79. #define WM8785_MCR_SLAVE 0x000
  80. #define WM8785_MCR_MASTER_128 0x001
  81. #define WM8785_MCR_MASTER_192 0x002
  82. #define WM8785_MCR_MASTER_256 0x003
  83. #define WM8785_MCR_MASTER_384 0x004
  84. #define WM8785_MCR_MASTER_512 0x005
  85. #define WM8785_MCR_MASTER_768 0x006
  86. #define WM8785_OSR_MASK 0x018
  87. #define WM8785_OSR_SINGLE 0x000
  88. #define WM8785_OSR_DOUBLE 0x008
  89. #define WM8785_OSR_QUAD 0x010
  90. #define WM8785_FORMAT_MASK 0x060
  91. #define WM8785_FORMAT_RJUST 0x000
  92. #define WM8785_FORMAT_LJUST 0x020
  93. #define WM8785_FORMAT_I2S 0x040
  94. #define WM8785_FORMAT_DSP 0x060
  95. /* R1 */
  96. #define WM8785_WL_MASK 0x003
  97. #define WM8785_WL_16 0x000
  98. #define WM8785_WL_20 0x001
  99. #define WM8785_WL_24 0x002
  100. #define WM8785_WL_32 0x003
  101. #define WM8785_LRP 0x004
  102. #define WM8785_BCLKINV 0x008
  103. #define WM8785_LRSWAP 0x010
  104. #define WM8785_DEVNO_MASK 0x0e0
  105. /* R2 */
  106. #define WM8785_HPFR 0x001
  107. #define WM8785_HPFL 0x002
  108. #define WM8785_SDODIS 0x004
  109. #define WM8785_PWRDNR 0x008
  110. #define WM8785_PWRDNL 0x010
  111. #define WM8785_TDM_MASK 0x1c0
  112. struct generic_data {
  113. u8 ak4396_ctl2;
  114. };
  115. static void ak4396_write(struct oxygen *chip, unsigned int codec,
  116. u8 reg, u8 value)
  117. {
  118. /* maps ALSA channel pair number to SPI output */
  119. static const u8 codec_spi_map[4] = {
  120. 0, 1, 2, 4
  121. };
  122. oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
  123. OXYGEN_SPI_DATA_LENGTH_2 |
  124. OXYGEN_SPI_CLOCK_160 |
  125. (codec_spi_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
  126. OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
  127. AK4396_WRITE | (reg << 8) | value);
  128. }
  129. static void wm8785_write(struct oxygen *chip, u8 reg, unsigned int value)
  130. {
  131. oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
  132. OXYGEN_SPI_DATA_LENGTH_2 |
  133. OXYGEN_SPI_CLOCK_160 |
  134. (3 << OXYGEN_SPI_CODEC_SHIFT) |
  135. OXYGEN_SPI_CEN_LATCH_CLOCK_LO,
  136. (reg << 9) | value);
  137. }
  138. static void ak4396_init(struct oxygen *chip)
  139. {
  140. struct generic_data *data = chip->model_data;
  141. unsigned int i;
  142. data->ak4396_ctl2 = AK4396_DEM_OFF | AK4396_DFS_NORMAL;
  143. for (i = 0; i < 4; ++i) {
  144. ak4396_write(chip, i,
  145. AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN);
  146. ak4396_write(chip, i,
  147. AK4396_CONTROL_2, data->ak4396_ctl2);
  148. ak4396_write(chip, i,
  149. AK4396_CONTROL_3, AK4396_PCM);
  150. ak4396_write(chip, i, AK4396_LCH_ATT, 0xff);
  151. ak4396_write(chip, i, AK4396_RCH_ATT, 0xff);
  152. }
  153. snd_component_add(chip->card, "AK4396");
  154. }
  155. static void ak5385_init(struct oxygen *chip)
  156. {
  157. oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_AK5385_DFS_MASK);
  158. oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_AK5385_DFS_MASK);
  159. snd_component_add(chip->card, "AK5385");
  160. }
  161. static void wm8785_init(struct oxygen *chip)
  162. {
  163. wm8785_write(chip, WM8785_R7, 0);
  164. wm8785_write(chip, WM8785_R0, WM8785_MCR_SLAVE |
  165. WM8785_OSR_SINGLE | WM8785_FORMAT_LJUST);
  166. wm8785_write(chip, WM8785_R1, WM8785_WL_24);
  167. snd_component_add(chip->card, "WM8785");
  168. }
  169. static void cmi9780_init(struct oxygen *chip)
  170. {
  171. oxygen_ac97_clear_bits(chip, 0, CM9780_GPIO_STATUS, GPIO_LINE_MUTE);
  172. }
  173. static void generic_init(struct oxygen *chip)
  174. {
  175. ak4396_init(chip);
  176. wm8785_init(chip);
  177. cmi9780_init(chip);
  178. }
  179. static void meridian_init(struct oxygen *chip)
  180. {
  181. ak4396_init(chip);
  182. ak5385_init(chip);
  183. cmi9780_init(chip);
  184. }
  185. static void generic_cleanup(struct oxygen *chip)
  186. {
  187. }
  188. static void set_ak4396_params(struct oxygen *chip,
  189. struct snd_pcm_hw_params *params)
  190. {
  191. struct generic_data *data = chip->model_data;
  192. unsigned int i;
  193. u8 value;
  194. value = data->ak4396_ctl2 & ~AK4396_DFS_MASK;
  195. if (params_rate(params) <= 54000)
  196. value |= AK4396_DFS_NORMAL;
  197. else if (params_rate(params) <= 108000)
  198. value |= AK4396_DFS_DOUBLE;
  199. else
  200. value |= AK4396_DFS_QUAD;
  201. data->ak4396_ctl2 = value;
  202. for (i = 0; i < 4; ++i) {
  203. ak4396_write(chip, i,
  204. AK4396_CONTROL_1, AK4396_DIF_24_MSB);
  205. ak4396_write(chip, i,
  206. AK4396_CONTROL_2, value);
  207. ak4396_write(chip, i,
  208. AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN);
  209. }
  210. }
  211. static void update_ak4396_volume(struct oxygen *chip)
  212. {
  213. unsigned int i;
  214. for (i = 0; i < 4; ++i) {
  215. ak4396_write(chip, i,
  216. AK4396_LCH_ATT, chip->dac_volume[i * 2]);
  217. ak4396_write(chip, i,
  218. AK4396_RCH_ATT, chip->dac_volume[i * 2 + 1]);
  219. }
  220. }
  221. static void update_ak4396_mute(struct oxygen *chip)
  222. {
  223. struct generic_data *data = chip->model_data;
  224. unsigned int i;
  225. u8 value;
  226. value = data->ak4396_ctl2 & ~AK4396_SMUTE;
  227. if (chip->dac_mute)
  228. value |= AK4396_SMUTE;
  229. data->ak4396_ctl2 = value;
  230. for (i = 0; i < 4; ++i)
  231. ak4396_write(chip, i, AK4396_CONTROL_2, value);
  232. }
  233. static void set_wm8785_params(struct oxygen *chip,
  234. struct snd_pcm_hw_params *params)
  235. {
  236. unsigned int value;
  237. wm8785_write(chip, WM8785_R7, 0);
  238. value = WM8785_MCR_SLAVE | WM8785_FORMAT_LJUST;
  239. if (params_rate(params) <= 48000)
  240. value |= WM8785_OSR_SINGLE;
  241. else if (params_rate(params) <= 96000)
  242. value |= WM8785_OSR_DOUBLE;
  243. else
  244. value |= WM8785_OSR_QUAD;
  245. wm8785_write(chip, WM8785_R0, value);
  246. if (snd_pcm_format_width(params_format(params)) <= 16)
  247. value = WM8785_WL_16;
  248. else
  249. value = WM8785_WL_24;
  250. wm8785_write(chip, WM8785_R1, value);
  251. }
  252. static void set_ak5385_params(struct oxygen *chip,
  253. struct snd_pcm_hw_params *params)
  254. {
  255. unsigned int value;
  256. if (params_rate(params) <= 54000)
  257. value = GPIO_AK5385_DFS_NORMAL;
  258. else if (params_rate(params) <= 108000)
  259. value = GPIO_AK5385_DFS_DOUBLE;
  260. else
  261. value = GPIO_AK5385_DFS_QUAD;
  262. oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
  263. value, GPIO_AK5385_DFS_MASK);
  264. }
  265. static void cmi9780_switch_hook(struct oxygen *chip, unsigned int codec,
  266. unsigned int reg, int mute)
  267. {
  268. if (codec != 0)
  269. return;
  270. switch (reg) {
  271. case AC97_LINE:
  272. oxygen_write_ac97_masked(chip, 0, CM9780_GPIO_STATUS,
  273. mute ? GPIO_LINE_MUTE : 0,
  274. GPIO_LINE_MUTE);
  275. break;
  276. case AC97_MIC:
  277. case AC97_CD:
  278. case AC97_AUX:
  279. if (!mute)
  280. oxygen_ac97_set_bits(chip, 0, CM9780_GPIO_STATUS,
  281. GPIO_LINE_MUTE);
  282. break;
  283. }
  284. }
  285. static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0);
  286. static int ak4396_control_filter(struct snd_kcontrol_new *template)
  287. {
  288. if (!strcmp(template->name, "Master Playback Volume")) {
  289. template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
  290. template->tlv.p = ak4396_db_scale;
  291. }
  292. return 0;
  293. }
  294. static const struct oxygen_model model_generic = {
  295. .shortname = "C-Media CMI8788",
  296. .longname = "C-Media Oxygen HD Audio",
  297. .chip = "CMI8788",
  298. .owner = THIS_MODULE,
  299. .init = generic_init,
  300. .control_filter = ak4396_control_filter,
  301. .cleanup = generic_cleanup,
  302. .set_dac_params = set_ak4396_params,
  303. .set_adc_params = set_wm8785_params,
  304. .update_dac_volume = update_ak4396_volume,
  305. .update_dac_mute = update_ak4396_mute,
  306. .ac97_switch_hook = cmi9780_switch_hook,
  307. .model_data_size = sizeof(struct generic_data),
  308. .dac_channels = 8,
  309. .used_channels = OXYGEN_CHANNEL_A |
  310. OXYGEN_CHANNEL_C |
  311. OXYGEN_CHANNEL_SPDIF |
  312. OXYGEN_CHANNEL_MULTICH |
  313. OXYGEN_CHANNEL_AC97,
  314. .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
  315. .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  316. .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  317. };
  318. static const struct oxygen_model model_meridian = {
  319. .shortname = "C-Media CMI8788",
  320. .longname = "C-Media Oxygen HD Audio",
  321. .chip = "CMI8788",
  322. .owner = THIS_MODULE,
  323. .init = meridian_init,
  324. .control_filter = ak4396_control_filter,
  325. .cleanup = generic_cleanup,
  326. .set_dac_params = set_ak4396_params,
  327. .set_adc_params = set_ak5385_params,
  328. .update_dac_volume = update_ak4396_volume,
  329. .update_dac_mute = update_ak4396_mute,
  330. .ac97_switch_hook = cmi9780_switch_hook,
  331. .model_data_size = sizeof(struct generic_data),
  332. .dac_channels = 8,
  333. .used_channels = OXYGEN_CHANNEL_B |
  334. OXYGEN_CHANNEL_C |
  335. OXYGEN_CHANNEL_SPDIF |
  336. OXYGEN_CHANNEL_MULTICH |
  337. OXYGEN_CHANNEL_AC97,
  338. .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
  339. .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  340. .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  341. };
  342. static int __devinit generic_oxygen_probe(struct pci_dev *pci,
  343. const struct pci_device_id *pci_id)
  344. {
  345. static int dev;
  346. int is_meridian;
  347. int err;
  348. if (dev >= SNDRV_CARDS)
  349. return -ENODEV;
  350. if (!enable[dev]) {
  351. ++dev;
  352. return -ENOENT;
  353. }
  354. is_meridian = pci_id->driver_data;
  355. err = oxygen_pci_probe(pci, index[dev], id[dev], is_meridian,
  356. is_meridian ? &model_meridian : &model_generic);
  357. if (err >= 0)
  358. ++dev;
  359. return err;
  360. }
  361. static struct pci_driver oxygen_driver = {
  362. .name = "CMI8788",
  363. .id_table = oxygen_ids,
  364. .probe = generic_oxygen_probe,
  365. .remove = __devexit_p(oxygen_pci_remove),
  366. };
  367. static int __init alsa_card_oxygen_init(void)
  368. {
  369. return pci_register_driver(&oxygen_driver);
  370. }
  371. static void __exit alsa_card_oxygen_exit(void)
  372. {
  373. pci_unregister_driver(&oxygen_driver);
  374. }
  375. module_init(alsa_card_oxygen_init)
  376. module_exit(alsa_card_oxygen_exit)