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