]>
Commit | Line | Data |
---|---|---|
d0ce9946 CL |
1 | /* |
2 | * C-Media CMI8788 driver for C-Media's reference design and for the X-Meridian | |
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 | /* | |
21 | * SPI 0 -> 1st AK4396 (front) | |
22 | * SPI 1 -> 2nd AK4396 (side) | |
23 | * SPI 2 -> 3rd AK4396 (center/LFE) | |
24 | * SPI 3 -> WM8785 | |
25 | * SPI 4 -> 4th AK4396 (rear) | |
26 | * | |
27 | * GPIO 0 -> DFS0 of AK5385 | |
28 | * GPIO 1 -> DFS1 of AK5385 | |
29 | */ | |
30 | ||
d0ce9946 CL |
31 | #include <linux/pci.h> |
32 | #include <sound/core.h> | |
33 | #include <sound/initval.h> | |
34 | #include <sound/pcm.h> | |
35 | #include <sound/pcm_params.h> | |
36 | #include <sound/tlv.h> | |
37 | #include "oxygen.h" | |
38 | ||
39 | MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); | |
40 | MODULE_DESCRIPTION("C-Media CMI8788 driver"); | |
41 | MODULE_LICENSE("GPL"); | |
42 | MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8788}}"); | |
43 | ||
44 | static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX; | |
45 | static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR; | |
46 | static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP; | |
47 | ||
48 | module_param_array(index, int, NULL, 0444); | |
49 | MODULE_PARM_DESC(index, "card index"); | |
50 | module_param_array(id, charp, NULL, 0444); | |
51 | MODULE_PARM_DESC(id, "ID string"); | |
52 | module_param_array(enable, bool, NULL, 0444); | |
53 | MODULE_PARM_DESC(enable, "enable card"); | |
54 | ||
55 | static struct pci_device_id oxygen_ids[] __devinitdata = { | |
56 | { OXYGEN_PCI_SUBID(0x10b0, 0x0216) }, | |
57 | { OXYGEN_PCI_SUBID(0x10b0, 0x0218) }, | |
58 | { OXYGEN_PCI_SUBID(0x10b0, 0x0219) }, | |
59 | { OXYGEN_PCI_SUBID(0x13f6, 0x0001) }, | |
60 | { OXYGEN_PCI_SUBID(0x13f6, 0x0010) }, | |
61 | { OXYGEN_PCI_SUBID(0x13f6, 0x8788) }, | |
62 | { OXYGEN_PCI_SUBID(0x147a, 0xa017) }, | |
63 | { OXYGEN_PCI_SUBID(0x14c3, 0x1710) }, | |
64 | { OXYGEN_PCI_SUBID(0x14c3, 0x1711) }, | |
65 | { OXYGEN_PCI_SUBID(0x1a58, 0x0910) }, | |
66 | { OXYGEN_PCI_SUBID(0x415a, 0x5431), .driver_data = 1 }, | |
67 | { OXYGEN_PCI_SUBID(0x7284, 0x9761) }, | |
68 | { } | |
69 | }; | |
70 | MODULE_DEVICE_TABLE(pci, oxygen_ids); | |
71 | ||
72 | #define AK4396_WRITE 0x2000 | |
73 | ||
74 | /* register 0 */ | |
75 | #define AK4396_RSTN 0x01 | |
76 | #define AK4396_DIF_24_MSB 0x04 | |
77 | /* register 1 */ | |
78 | #define AK4396_SMUTE 0x01 | |
79 | #define AK4396_DEM_OFF 0x02 | |
80 | #define AK4396_DFS_MASK 0x18 | |
81 | #define AK4396_DFS_NORMAL 0x00 | |
82 | #define AK4396_DFS_DOUBLE 0x08 | |
83 | #define AK4396_DFS_QUAD 0x10 | |
84 | ||
85 | /* register 0 */ | |
86 | #define WM8785_OSR_SINGLE 0x000 | |
87 | #define WM8785_OSR_DOUBLE 0x008 | |
88 | #define WM8785_OSR_QUAD 0x010 | |
89 | #define WM8785_FORMAT_LJUST 0x020 | |
90 | #define WM8785_FORMAT_I2S 0x040 | |
91 | /* register 1 */ | |
92 | #define WM8785_WL_16 0x000 | |
93 | #define WM8785_WL_20 0x001 | |
94 | #define WM8785_WL_24 0x002 | |
95 | #define WM8785_WL_32 0x003 | |
96 | ||
97 | static void ak4396_write(struct oxygen *chip, unsigned int codec, | |
98 | u8 reg, u8 value) | |
99 | { | |
100 | /* maps ALSA channel pair number to SPI output */ | |
101 | static const u8 codec_spi_map[4] = { | |
102 | 0, 4, 2, 1 | |
103 | }; | |
104 | oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER_WRITE | | |
105 | OXYGEN_SPI_DATA_LENGTH_2 | | |
106 | (codec_spi_map[codec] << OXYGEN_SPI_CODEC_SHIFT) | | |
107 | OXYGEN_SPI_MAGIC, | |
108 | AK4396_WRITE | (reg << 8) | value); | |
109 | } | |
110 | ||
111 | static void wm8785_write(struct oxygen *chip, u8 reg, unsigned int value) | |
112 | { | |
113 | oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER_WRITE | | |
114 | OXYGEN_SPI_DATA_LENGTH_2 | | |
115 | (3 << OXYGEN_SPI_CODEC_SHIFT), | |
116 | (reg << 9) | value); | |
117 | } | |
118 | ||
119 | static void ak4396_init(struct oxygen *chip) | |
120 | { | |
121 | unsigned int i; | |
122 | ||
123 | chip->ak4396_reg1 = AK4396_DEM_OFF | AK4396_DFS_NORMAL; | |
124 | for (i = 0; i < 4; ++i) { | |
125 | ak4396_write(chip, i, 0, AK4396_DIF_24_MSB | AK4396_RSTN); | |
126 | ak4396_write(chip, i, 1, chip->ak4396_reg1); | |
127 | ak4396_write(chip, i, 2, 0); | |
128 | ak4396_write(chip, i, 3, 0xff); | |
129 | ak4396_write(chip, i, 4, 0xff); | |
130 | } | |
131 | snd_component_add(chip->card, "AK4396"); | |
132 | } | |
133 | ||
134 | static void ak5385_init(struct oxygen *chip) | |
135 | { | |
136 | oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, 0x0003); | |
137 | oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, 0x0003); | |
138 | snd_component_add(chip->card, "AK5385"); | |
139 | } | |
140 | ||
141 | static void wm8785_init(struct oxygen *chip) | |
142 | { | |
143 | wm8785_write(chip, 7, 0); | |
144 | wm8785_write(chip, 0, WM8785_FORMAT_LJUST | WM8785_OSR_SINGLE); | |
145 | wm8785_write(chip, 1, WM8785_WL_24); | |
146 | snd_component_add(chip->card, "WM8785"); | |
147 | } | |
148 | ||
149 | static void generic_init(struct oxygen *chip) | |
150 | { | |
151 | ak4396_init(chip); | |
152 | wm8785_init(chip); | |
153 | } | |
154 | ||
155 | static void meridian_init(struct oxygen *chip) | |
156 | { | |
157 | ak4396_init(chip); | |
158 | ak5385_init(chip); | |
159 | } | |
160 | ||
161 | static void generic_cleanup(struct oxygen *chip) | |
162 | { | |
163 | } | |
164 | ||
165 | static void set_ak4396_params(struct oxygen *chip, | |
166 | struct snd_pcm_hw_params *params) | |
167 | { | |
168 | unsigned int i; | |
169 | u8 value; | |
170 | ||
171 | value = chip->ak4396_reg1 & ~AK4396_DFS_MASK; | |
172 | if (params_rate(params) <= 54000) | |
173 | value |= AK4396_DFS_NORMAL; | |
174 | else if (params_rate(params) < 120000) | |
175 | value |= AK4396_DFS_DOUBLE; | |
176 | else | |
177 | value |= AK4396_DFS_QUAD; | |
178 | chip->ak4396_reg1 = value; | |
179 | for (i = 0; i < 4; ++i) { | |
180 | ak4396_write(chip, i, 0, AK4396_DIF_24_MSB); | |
181 | ak4396_write(chip, i, 1, value); | |
182 | ak4396_write(chip, i, 0, AK4396_DIF_24_MSB | AK4396_RSTN); | |
183 | } | |
184 | } | |
185 | ||
186 | static void update_ak4396_volume(struct oxygen *chip) | |
187 | { | |
188 | unsigned int i; | |
189 | ||
190 | for (i = 0; i < 4; ++i) { | |
191 | ak4396_write(chip, i, 3, chip->dac_volume[i * 2]); | |
192 | ak4396_write(chip, i, 4, chip->dac_volume[i * 2 + 1]); | |
193 | } | |
194 | } | |
195 | ||
196 | static void update_ak4396_mute(struct oxygen *chip) | |
197 | { | |
198 | unsigned int i; | |
199 | u8 value; | |
200 | ||
201 | value = chip->ak4396_reg1 & ~AK4396_SMUTE; | |
202 | if (chip->dac_mute) | |
203 | value |= AK4396_SMUTE; | |
204 | for (i = 0; i < 4; ++i) | |
205 | ak4396_write(chip, i, 1, value); | |
206 | } | |
207 | ||
208 | static void set_wm8785_params(struct oxygen *chip, | |
209 | struct snd_pcm_hw_params *params) | |
210 | { | |
211 | unsigned int value; | |
212 | ||
213 | wm8785_write(chip, 7, 0); | |
214 | ||
215 | value = WM8785_FORMAT_LJUST; | |
216 | if (params_rate(params) == 96000) | |
217 | value |= WM8785_OSR_DOUBLE; | |
218 | else if (params_rate(params) == 192000) | |
219 | value |= WM8785_OSR_QUAD; | |
220 | else | |
221 | value |= WM8785_OSR_SINGLE; | |
222 | wm8785_write(chip, 0, value); | |
223 | ||
224 | if (snd_pcm_format_width(params_format(params)) <= 16) | |
225 | value = WM8785_WL_16; | |
226 | else | |
227 | value = WM8785_WL_24; | |
228 | wm8785_write(chip, 1, value); | |
229 | } | |
230 | ||
231 | static void set_ak5385_params(struct oxygen *chip, | |
232 | struct snd_pcm_hw_params *params) | |
233 | { | |
234 | unsigned int value; | |
235 | ||
236 | if (params_rate(params) <= 54000) | |
237 | value = 0; | |
238 | else if (params_rate(params) <= 108000) | |
239 | value = 1; | |
240 | else | |
241 | value = 2; | |
242 | oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, value, 0x0003); | |
243 | } | |
244 | ||
245 | static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0); | |
246 | ||
247 | static const struct oxygen_model model_generic = { | |
248 | .shortname = "C-Media CMI8788", | |
249 | .longname = "C-Media Oxygen HD Audio", | |
250 | .chip = "CMI8788", | |
251 | .owner = THIS_MODULE, | |
252 | .init = generic_init, | |
253 | .cleanup = generic_cleanup, | |
254 | .set_dac_params = set_ak4396_params, | |
255 | .set_adc_params = set_wm8785_params, | |
256 | .update_dac_volume = update_ak4396_volume, | |
257 | .update_dac_mute = update_ak4396_mute, | |
258 | .dac_tlv = ak4396_db_scale, | |
259 | }; | |
260 | static const struct oxygen_model model_meridian = { | |
261 | .shortname = "C-Media CMI8788", | |
262 | .longname = "C-Media Oxygen HD Audio", | |
263 | .chip = "CMI8788", | |
264 | .owner = THIS_MODULE, | |
265 | .init = meridian_init, | |
266 | .cleanup = generic_cleanup, | |
267 | .set_dac_params = set_ak4396_params, | |
268 | .set_adc_params = set_ak5385_params, | |
269 | .update_dac_volume = update_ak4396_volume, | |
270 | .update_dac_mute = update_ak4396_mute, | |
271 | .dac_tlv = ak4396_db_scale, | |
272 | .record_from_dma_b = 1, | |
273 | }; | |
274 | ||
275 | static int __devinit generic_oxygen_probe(struct pci_dev *pci, | |
276 | const struct pci_device_id *pci_id) | |
277 | { | |
278 | static int dev; | |
279 | const struct oxygen_model *model; | |
280 | int err; | |
281 | ||
282 | if (dev >= SNDRV_CARDS) | |
283 | return -ENODEV; | |
284 | if (!enable[dev]) { | |
285 | ++dev; | |
286 | return -ENOENT; | |
287 | } | |
288 | model = pci_id->driver_data ? &model_meridian : &model_generic; | |
289 | err = oxygen_pci_probe(pci, index[dev], id[dev], model); | |
290 | if (err >= 0) | |
291 | ++dev; | |
292 | return err; | |
293 | } | |
294 | ||
295 | static struct pci_driver oxygen_driver = { | |
296 | .name = "CMI8788", | |
297 | .id_table = oxygen_ids, | |
298 | .probe = generic_oxygen_probe, | |
299 | .remove = __devexit_p(oxygen_pci_remove), | |
300 | }; | |
301 | ||
302 | static int __init alsa_card_oxygen_init(void) | |
303 | { | |
304 | return pci_register_driver(&oxygen_driver); | |
305 | } | |
306 | ||
307 | static void __exit alsa_card_oxygen_exit(void) | |
308 | { | |
309 | pci_unregister_driver(&oxygen_driver); | |
310 | } | |
311 | ||
312 | module_init(alsa_card_oxygen_init) | |
313 | module_exit(alsa_card_oxygen_exit) |