hifier.c 5.7 KB

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