hifier.c 5.6 KB

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