hifier.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. { OXYGEN_PCI_SUBID_BROKEN_EEPROM },
  44. { }
  45. };
  46. MODULE_DEVICE_TABLE(pci, hifier_ids);
  47. struct hifier_data {
  48. u8 ak4396_ctl2;
  49. };
  50. static void ak4396_write(struct oxygen *chip, u8 reg, u8 value)
  51. {
  52. oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
  53. OXYGEN_SPI_DATA_LENGTH_2 |
  54. OXYGEN_SPI_CLOCK_160 |
  55. (0 << OXYGEN_SPI_CODEC_SHIFT) |
  56. OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
  57. AK4396_WRITE | (reg << 8) | value);
  58. }
  59. static void update_ak4396_volume(struct oxygen *chip)
  60. {
  61. ak4396_write(chip, AK4396_LCH_ATT, chip->dac_volume[0]);
  62. ak4396_write(chip, AK4396_RCH_ATT, chip->dac_volume[1]);
  63. }
  64. static void hifier_registers_init(struct oxygen *chip)
  65. {
  66. struct hifier_data *data = chip->model_data;
  67. ak4396_write(chip, AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN);
  68. ak4396_write(chip, AK4396_CONTROL_2, data->ak4396_ctl2);
  69. ak4396_write(chip, AK4396_CONTROL_3, AK4396_PCM);
  70. update_ak4396_volume(chip);
  71. }
  72. static void hifier_init(struct oxygen *chip)
  73. {
  74. struct hifier_data *data = chip->model_data;
  75. data->ak4396_ctl2 = AK4396_SMUTE | AK4396_DEM_OFF | AK4396_DFS_NORMAL;
  76. hifier_registers_init(chip);
  77. snd_component_add(chip->card, "AK4396");
  78. snd_component_add(chip->card, "CS5340");
  79. }
  80. static void hifier_cleanup(struct oxygen *chip)
  81. {
  82. }
  83. static void hifier_resume(struct oxygen *chip)
  84. {
  85. hifier_registers_init(chip);
  86. }
  87. static void set_ak4396_params(struct oxygen *chip,
  88. struct snd_pcm_hw_params *params)
  89. {
  90. struct hifier_data *data = chip->model_data;
  91. u8 value;
  92. value = data->ak4396_ctl2 & ~AK4396_DFS_MASK;
  93. if (params_rate(params) <= 54000)
  94. value |= AK4396_DFS_NORMAL;
  95. else if (params_rate(params) <= 108000)
  96. value |= AK4396_DFS_DOUBLE;
  97. else
  98. value |= AK4396_DFS_QUAD;
  99. data->ak4396_ctl2 = value;
  100. msleep(1); /* wait for the new MCLK to become stable */
  101. ak4396_write(chip, AK4396_CONTROL_1, AK4396_DIF_24_MSB);
  102. ak4396_write(chip, AK4396_CONTROL_2, value);
  103. ak4396_write(chip, AK4396_CONTROL_1, AK4396_DIF_24_MSB | AK4396_RSTN);
  104. }
  105. static void update_ak4396_mute(struct oxygen *chip)
  106. {
  107. struct hifier_data *data = chip->model_data;
  108. u8 value;
  109. value = data->ak4396_ctl2 & ~AK4396_SMUTE;
  110. if (chip->dac_mute)
  111. value |= AK4396_SMUTE;
  112. data->ak4396_ctl2 = value;
  113. ak4396_write(chip, AK4396_CONTROL_2, value);
  114. }
  115. static void set_cs5340_params(struct oxygen *chip,
  116. struct snd_pcm_hw_params *params)
  117. {
  118. }
  119. static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0);
  120. static int hifier_control_filter(struct snd_kcontrol_new *template)
  121. {
  122. if (!strcmp(template->name, "Stereo Upmixing"))
  123. return 1; /* stereo only - we don't need upmixing */
  124. return 0;
  125. }
  126. static const struct oxygen_model model_hifier = {
  127. .shortname = "C-Media CMI8787",
  128. .longname = "C-Media Oxygen HD Audio",
  129. .chip = "CMI8788",
  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 get_hifier_model(struct oxygen *chip,
  151. const struct pci_device_id *id)
  152. {
  153. chip->model = model_hifier;
  154. return 0;
  155. }
  156. static int __devinit hifier_probe(struct pci_dev *pci,
  157. const struct pci_device_id *pci_id)
  158. {
  159. static int dev;
  160. int err;
  161. if (dev >= SNDRV_CARDS)
  162. return -ENODEV;
  163. if (!enable[dev]) {
  164. ++dev;
  165. return -ENOENT;
  166. }
  167. err = oxygen_pci_probe(pci, index[dev], id[dev], THIS_MODULE,
  168. hifier_ids, get_hifier_model);
  169. if (err >= 0)
  170. ++dev;
  171. return err;
  172. }
  173. static struct pci_driver hifier_driver = {
  174. .name = "CMI8787HiFier",
  175. .id_table = hifier_ids,
  176. .probe = hifier_probe,
  177. .remove = __devexit_p(oxygen_pci_remove),
  178. #ifdef CONFIG_PM
  179. .suspend = oxygen_pci_suspend,
  180. .resume = oxygen_pci_resume,
  181. #endif
  182. };
  183. static int __init alsa_card_hifier_init(void)
  184. {
  185. return pci_register_driver(&hifier_driver);
  186. }
  187. static void __exit alsa_card_hifier_exit(void)
  188. {
  189. pci_unregister_driver(&hifier_driver);
  190. }
  191. module_init(alsa_card_hifier_init)
  192. module_exit(alsa_card_hifier_exit)