hifier.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * C-Media CMI8788 driver for the MediaTek/TempoTec HiFier Fantasia
  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. * CMI8788:
  21. *
  22. * SPI 0 -> AK4396
  23. */
  24. #include <linux/delay.h>
  25. #include <linux/pci.h>
  26. #include <sound/control.h>
  27. #include <sound/core.h>
  28. #include <sound/initval.h>
  29. #include <sound/pcm.h>
  30. #include <sound/tlv.h>
  31. #include "oxygen.h"
  32. #include "ak4396.h"
  33. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  34. MODULE_DESCRIPTION("TempoTec HiFier driver");
  35. MODULE_LICENSE("GPL v2");
  36. static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
  37. static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
  38. static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
  39. module_param_array(index, int, NULL, 0444);
  40. MODULE_PARM_DESC(index, "card index");
  41. module_param_array(id, charp, NULL, 0444);
  42. MODULE_PARM_DESC(id, "ID string");
  43. module_param_array(enable, bool, NULL, 0444);
  44. MODULE_PARM_DESC(enable, "enable card");
  45. static struct pci_device_id hifier_ids[] __devinitdata = {
  46. { OXYGEN_PCI_SUBID(0x14c3, 0x1710) },
  47. { OXYGEN_PCI_SUBID(0x14c3, 0x1711) },
  48. { OXYGEN_PCI_SUBID_BROKEN_EEPROM },
  49. { }
  50. };
  51. MODULE_DEVICE_TABLE(pci, hifier_ids);
  52. struct hifier_data {
  53. u8 ak4396_regs[5];
  54. };
  55. static void ak4396_write(struct oxygen *chip, u8 reg, u8 value)
  56. {
  57. struct hifier_data *data = chip->model_data;
  58. oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
  59. OXYGEN_SPI_DATA_LENGTH_2 |
  60. OXYGEN_SPI_CLOCK_160 |
  61. (0 << OXYGEN_SPI_CODEC_SHIFT) |
  62. OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
  63. AK4396_WRITE | (reg << 8) | value);
  64. data->ak4396_regs[reg] = value;
  65. }
  66. static void ak4396_write_cached(struct oxygen *chip, u8 reg, u8 value)
  67. {
  68. struct hifier_data *data = chip->model_data;
  69. if (value != data->ak4396_regs[reg])
  70. ak4396_write(chip, reg, value);
  71. }
  72. static void hifier_registers_init(struct oxygen *chip)
  73. {
  74. struct hifier_data *data = chip->model_data;
  75. ak4396_write(chip, AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN);
  76. ak4396_write(chip, AK4396_CONTROL_2,
  77. data->ak4396_regs[AK4396_CONTROL_2]);
  78. ak4396_write(chip, AK4396_CONTROL_3, AK4396_PCM);
  79. ak4396_write(chip, AK4396_LCH_ATT, chip->dac_volume[0]);
  80. ak4396_write(chip, AK4396_RCH_ATT, chip->dac_volume[1]);
  81. }
  82. static void hifier_init(struct oxygen *chip)
  83. {
  84. struct hifier_data *data = chip->model_data;
  85. data->ak4396_regs[AK4396_CONTROL_2] =
  86. AK4396_SMUTE | AK4396_DEM_OFF | AK4396_DFS_NORMAL;
  87. hifier_registers_init(chip);
  88. snd_component_add(chip->card, "AK4396");
  89. snd_component_add(chip->card, "CS5340");
  90. }
  91. static void hifier_cleanup(struct oxygen *chip)
  92. {
  93. }
  94. static void hifier_resume(struct oxygen *chip)
  95. {
  96. hifier_registers_init(chip);
  97. }
  98. static void set_ak4396_params(struct oxygen *chip,
  99. struct snd_pcm_hw_params *params)
  100. {
  101. struct hifier_data *data = chip->model_data;
  102. u8 value;
  103. value = data->ak4396_regs[AK4396_CONTROL_2] & ~AK4396_DFS_MASK;
  104. if (params_rate(params) <= 54000)
  105. value |= AK4396_DFS_NORMAL;
  106. else if (params_rate(params) <= 108000)
  107. value |= AK4396_DFS_DOUBLE;
  108. else
  109. value |= AK4396_DFS_QUAD;
  110. msleep(1); /* wait for the new MCLK to become stable */
  111. if (value != data->ak4396_regs[AK4396_CONTROL_2]) {
  112. ak4396_write(chip, AK4396_CONTROL_1,
  113. AK4396_DIF_24_MSB);
  114. ak4396_write(chip, AK4396_CONTROL_2, value);
  115. ak4396_write(chip, AK4396_CONTROL_1,
  116. AK4396_DIF_24_MSB | AK4396_RSTN);
  117. }
  118. }
  119. static void update_ak4396_volume(struct oxygen *chip)
  120. {
  121. ak4396_write_cached(chip, AK4396_LCH_ATT, chip->dac_volume[0]);
  122. ak4396_write_cached(chip, AK4396_RCH_ATT, chip->dac_volume[1]);
  123. }
  124. static void update_ak4396_mute(struct oxygen *chip)
  125. {
  126. struct hifier_data *data = chip->model_data;
  127. u8 value;
  128. value = data->ak4396_regs[AK4396_CONTROL_2] & ~AK4396_SMUTE;
  129. if (chip->dac_mute)
  130. value |= AK4396_SMUTE;
  131. ak4396_write_cached(chip, AK4396_CONTROL_2, value);
  132. }
  133. static void set_cs5340_params(struct oxygen *chip,
  134. struct snd_pcm_hw_params *params)
  135. {
  136. }
  137. static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0);
  138. static const struct oxygen_model model_hifier = {
  139. .shortname = "C-Media CMI8787",
  140. .longname = "C-Media Oxygen HD Audio",
  141. .chip = "CMI8788",
  142. .init = hifier_init,
  143. .cleanup = hifier_cleanup,
  144. .resume = hifier_resume,
  145. .get_i2s_mclk = oxygen_default_i2s_mclk,
  146. .set_dac_params = set_ak4396_params,
  147. .set_adc_params = set_cs5340_params,
  148. .update_dac_volume = update_ak4396_volume,
  149. .update_dac_mute = update_ak4396_mute,
  150. .dac_tlv = ak4396_db_scale,
  151. .model_data_size = sizeof(struct hifier_data),
  152. .device_config = PLAYBACK_0_TO_I2S |
  153. PLAYBACK_1_TO_SPDIF |
  154. CAPTURE_0_FROM_I2S_1,
  155. .dac_channels = 2,
  156. .dac_volume_min = 0,
  157. .dac_volume_max = 255,
  158. .function_flags = OXYGEN_FUNCTION_SPI,
  159. .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  160. .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
  161. };
  162. static int __devinit get_hifier_model(struct oxygen *chip,
  163. const struct pci_device_id *id)
  164. {
  165. chip->model = model_hifier;
  166. return 0;
  167. }
  168. static int __devinit hifier_probe(struct pci_dev *pci,
  169. const struct pci_device_id *pci_id)
  170. {
  171. static int dev;
  172. int err;
  173. if (dev >= SNDRV_CARDS)
  174. return -ENODEV;
  175. if (!enable[dev]) {
  176. ++dev;
  177. return -ENOENT;
  178. }
  179. err = oxygen_pci_probe(pci, index[dev], id[dev], THIS_MODULE,
  180. hifier_ids, get_hifier_model);
  181. if (err >= 0)
  182. ++dev;
  183. return err;
  184. }
  185. static struct pci_driver hifier_driver = {
  186. .name = "CMI8787HiFier",
  187. .id_table = hifier_ids,
  188. .probe = hifier_probe,
  189. .remove = __devexit_p(oxygen_pci_remove),
  190. #ifdef CONFIG_PM
  191. .suspend = oxygen_pci_suspend,
  192. .resume = oxygen_pci_resume,
  193. #endif
  194. };
  195. static int __init alsa_card_hifier_init(void)
  196. {
  197. return pci_register_driver(&hifier_driver);
  198. }
  199. static void __exit alsa_card_hifier_exit(void)
  200. {
  201. pci_unregister_driver(&hifier_driver);
  202. }
  203. module_init(alsa_card_hifier_init)
  204. module_exit(alsa_card_hifier_exit)