blob: e0e54ab51e5c7efb2f00f733a535d127097e62a8 [file] [log] [blame]
Clemens Ladischd0ce9942007-12-23 19:50:57 +01001/*
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)
Clemens Ladisch7113e952008-01-14 08:55:03 +010022 * SPI 1 -> 2nd AK4396 (surround)
Clemens Ladischd0ce9942007-12-23 19:50:57 +010023 * SPI 2 -> 3rd AK4396 (center/LFE)
24 * SPI 3 -> WM8785
Clemens Ladisch7113e952008-01-14 08:55:03 +010025 * SPI 4 -> 4th AK4396 (back)
Clemens Ladischd0ce9942007-12-23 19:50:57 +010026 *
27 * GPIO 0 -> DFS0 of AK5385
28 * GPIO 1 -> DFS1 of AK5385
29 */
30
Clemens Ladischd0ce9942007-12-23 19:50:57 +010031#include <linux/pci.h>
Clemens Ladischccc80fb2008-01-16 08:32:08 +010032#include <sound/control.h>
Clemens Ladischd0ce9942007-12-23 19:50:57 +010033#include <sound/core.h>
34#include <sound/initval.h>
35#include <sound/pcm.h>
36#include <sound/pcm_params.h>
37#include <sound/tlv.h>
38#include "oxygen.h"
39
40MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
41MODULE_DESCRIPTION("C-Media CMI8788 driver");
42MODULE_LICENSE("GPL");
43MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8788}}");
44
45static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
46static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
47static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
48
49module_param_array(index, int, NULL, 0444);
50MODULE_PARM_DESC(index, "card index");
51module_param_array(id, charp, NULL, 0444);
52MODULE_PARM_DESC(id, "ID string");
53module_param_array(enable, bool, NULL, 0444);
54MODULE_PARM_DESC(enable, "enable card");
55
56static struct pci_device_id oxygen_ids[] __devinitdata = {
57 { OXYGEN_PCI_SUBID(0x10b0, 0x0216) },
58 { OXYGEN_PCI_SUBID(0x10b0, 0x0218) },
59 { OXYGEN_PCI_SUBID(0x10b0, 0x0219) },
60 { OXYGEN_PCI_SUBID(0x13f6, 0x0001) },
61 { OXYGEN_PCI_SUBID(0x13f6, 0x0010) },
62 { OXYGEN_PCI_SUBID(0x13f6, 0x8788) },
63 { OXYGEN_PCI_SUBID(0x147a, 0xa017) },
64 { OXYGEN_PCI_SUBID(0x14c3, 0x1710) },
65 { OXYGEN_PCI_SUBID(0x14c3, 0x1711) },
66 { OXYGEN_PCI_SUBID(0x1a58, 0x0910) },
67 { OXYGEN_PCI_SUBID(0x415a, 0x5431), .driver_data = 1 },
68 { OXYGEN_PCI_SUBID(0x7284, 0x9761) },
69 { }
70};
71MODULE_DEVICE_TABLE(pci, oxygen_ids);
72
73#define AK4396_WRITE 0x2000
74
75/* register 0 */
76#define AK4396_RSTN 0x01
77#define AK4396_DIF_24_MSB 0x04
78/* register 1 */
79#define AK4396_SMUTE 0x01
80#define AK4396_DEM_OFF 0x02
81#define AK4396_DFS_MASK 0x18
82#define AK4396_DFS_NORMAL 0x00
83#define AK4396_DFS_DOUBLE 0x08
84#define AK4396_DFS_QUAD 0x10
85
86/* register 0 */
87#define WM8785_OSR_SINGLE 0x000
88#define WM8785_OSR_DOUBLE 0x008
89#define WM8785_OSR_QUAD 0x010
90#define WM8785_FORMAT_LJUST 0x020
91#define WM8785_FORMAT_I2S 0x040
92/* register 1 */
93#define WM8785_WL_16 0x000
94#define WM8785_WL_20 0x001
95#define WM8785_WL_24 0x002
96#define WM8785_WL_32 0x003
97
98static void ak4396_write(struct oxygen *chip, unsigned int codec,
99 u8 reg, u8 value)
100{
101 /* maps ALSA channel pair number to SPI output */
102 static const u8 codec_spi_map[4] = {
Clemens Ladisch7113e952008-01-14 08:55:03 +0100103 0, 1, 2, 4
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100104 };
Clemens Ladischc2353a02008-01-18 09:17:53 +0100105 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100106 OXYGEN_SPI_DATA_LENGTH_2 |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100107 OXYGEN_SPI_CLOCK_160 |
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100108 (codec_spi_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100109 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100110 AK4396_WRITE | (reg << 8) | value);
111}
112
113static void wm8785_write(struct oxygen *chip, u8 reg, unsigned int value)
114{
Clemens Ladischc2353a02008-01-18 09:17:53 +0100115 oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100116 OXYGEN_SPI_DATA_LENGTH_2 |
Clemens Ladischc2353a02008-01-18 09:17:53 +0100117 OXYGEN_SPI_CLOCK_160 |
118 (3 << OXYGEN_SPI_CODEC_SHIFT) |
119 OXYGEN_SPI_CEN_LATCH_CLOCK_LO,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100120 (reg << 9) | value);
121}
122
123static void ak4396_init(struct oxygen *chip)
124{
125 unsigned int i;
126
127 chip->ak4396_reg1 = AK4396_DEM_OFF | AK4396_DFS_NORMAL;
128 for (i = 0; i < 4; ++i) {
129 ak4396_write(chip, i, 0, AK4396_DIF_24_MSB | AK4396_RSTN);
130 ak4396_write(chip, i, 1, chip->ak4396_reg1);
131 ak4396_write(chip, i, 2, 0);
132 ak4396_write(chip, i, 3, 0xff);
133 ak4396_write(chip, i, 4, 0xff);
134 }
135 snd_component_add(chip->card, "AK4396");
136}
137
138static void ak5385_init(struct oxygen *chip)
139{
140 oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, 0x0003);
141 oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, 0x0003);
142 snd_component_add(chip->card, "AK5385");
143}
144
145static void wm8785_init(struct oxygen *chip)
146{
147 wm8785_write(chip, 7, 0);
148 wm8785_write(chip, 0, WM8785_FORMAT_LJUST | WM8785_OSR_SINGLE);
149 wm8785_write(chip, 1, WM8785_WL_24);
150 snd_component_add(chip->card, "WM8785");
151}
152
153static void generic_init(struct oxygen *chip)
154{
155 ak4396_init(chip);
156 wm8785_init(chip);
157}
158
159static void meridian_init(struct oxygen *chip)
160{
161 ak4396_init(chip);
162 ak5385_init(chip);
163}
164
165static void generic_cleanup(struct oxygen *chip)
166{
167}
168
Clemens Ladisch747c6012008-01-16 08:32:53 +0100169static void generic_pcm_hardware_filter(unsigned int channel,
170 struct snd_pcm_hardware *hardware)
171{
172 if (channel == PCM_A) {
173 hardware->rates = SNDRV_PCM_RATE_44100 |
174 SNDRV_PCM_RATE_48000 |
175 SNDRV_PCM_RATE_96000 |
176 SNDRV_PCM_RATE_192000;
177 hardware->rate_min = 44100;
178 }
179}
180
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100181static void set_ak4396_params(struct oxygen *chip,
182 struct snd_pcm_hw_params *params)
183{
184 unsigned int i;
185 u8 value;
186
187 value = chip->ak4396_reg1 & ~AK4396_DFS_MASK;
188 if (params_rate(params) <= 54000)
189 value |= AK4396_DFS_NORMAL;
190 else if (params_rate(params) < 120000)
191 value |= AK4396_DFS_DOUBLE;
192 else
193 value |= AK4396_DFS_QUAD;
194 chip->ak4396_reg1 = value;
195 for (i = 0; i < 4; ++i) {
196 ak4396_write(chip, i, 0, AK4396_DIF_24_MSB);
197 ak4396_write(chip, i, 1, value);
198 ak4396_write(chip, i, 0, AK4396_DIF_24_MSB | AK4396_RSTN);
199 }
200}
201
202static void update_ak4396_volume(struct oxygen *chip)
203{
204 unsigned int i;
205
206 for (i = 0; i < 4; ++i) {
207 ak4396_write(chip, i, 3, chip->dac_volume[i * 2]);
208 ak4396_write(chip, i, 4, chip->dac_volume[i * 2 + 1]);
209 }
210}
211
212static void update_ak4396_mute(struct oxygen *chip)
213{
214 unsigned int i;
215 u8 value;
216
217 value = chip->ak4396_reg1 & ~AK4396_SMUTE;
218 if (chip->dac_mute)
219 value |= AK4396_SMUTE;
220 for (i = 0; i < 4; ++i)
221 ak4396_write(chip, i, 1, value);
222}
223
224static void set_wm8785_params(struct oxygen *chip,
225 struct snd_pcm_hw_params *params)
226{
227 unsigned int value;
228
229 wm8785_write(chip, 7, 0);
230
231 value = WM8785_FORMAT_LJUST;
232 if (params_rate(params) == 96000)
233 value |= WM8785_OSR_DOUBLE;
234 else if (params_rate(params) == 192000)
235 value |= WM8785_OSR_QUAD;
236 else
237 value |= WM8785_OSR_SINGLE;
238 wm8785_write(chip, 0, value);
239
240 if (snd_pcm_format_width(params_format(params)) <= 16)
241 value = WM8785_WL_16;
242 else
243 value = WM8785_WL_24;
244 wm8785_write(chip, 1, value);
245}
246
247static void set_ak5385_params(struct oxygen *chip,
248 struct snd_pcm_hw_params *params)
249{
250 unsigned int value;
251
252 if (params_rate(params) <= 54000)
253 value = 0;
254 else if (params_rate(params) <= 108000)
255 value = 1;
256 else
257 value = 2;
258 oxygen_write16_masked(chip, OXYGEN_GPIO_DATA, value, 0x0003);
259}
260
261static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0);
262
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100263static int ak4396_control_filter(struct snd_kcontrol_new *template)
264{
265 if (!strcmp(template->name, "Master Playback Volume")) {
266 template->access |= SNDRV_CTL_ELEM_ACCESS_TLV_READ;
267 template->tlv.p = ak4396_db_scale;
268 }
269 return 0;
270}
271
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100272static const struct oxygen_model model_generic = {
273 .shortname = "C-Media CMI8788",
274 .longname = "C-Media Oxygen HD Audio",
275 .chip = "CMI8788",
276 .owner = THIS_MODULE,
277 .init = generic_init,
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100278 .control_filter = ak4396_control_filter,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100279 .cleanup = generic_cleanup,
Clemens Ladisch747c6012008-01-16 08:32:53 +0100280 .pcm_hardware_filter = generic_pcm_hardware_filter,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100281 .set_dac_params = set_ak4396_params,
282 .set_adc_params = set_wm8785_params,
283 .update_dac_volume = update_ak4396_volume,
284 .update_dac_mute = update_ak4396_mute,
Clemens Ladische85e0922008-01-16 08:30:38 +0100285 .used_channels = OXYGEN_CHANNEL_A |
286 OXYGEN_CHANNEL_C |
287 OXYGEN_CHANNEL_SPDIF |
288 OXYGEN_CHANNEL_MULTICH |
289 OXYGEN_CHANNEL_AC97,
Clemens Ladisch84aa6b72008-01-16 08:28:54 +0100290 .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100291 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
292 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100293};
294static const struct oxygen_model model_meridian = {
295 .shortname = "C-Media CMI8788",
296 .longname = "C-Media Oxygen HD Audio",
297 .chip = "CMI8788",
298 .owner = THIS_MODULE,
299 .init = meridian_init,
Clemens Ladischccc80fb2008-01-16 08:32:08 +0100300 .control_filter = ak4396_control_filter,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100301 .cleanup = generic_cleanup,
302 .set_dac_params = set_ak4396_params,
303 .set_adc_params = set_ak5385_params,
304 .update_dac_volume = update_ak4396_volume,
305 .update_dac_mute = update_ak4396_mute,
Clemens Ladische85e0922008-01-16 08:30:38 +0100306 .used_channels = OXYGEN_CHANNEL_B |
307 OXYGEN_CHANNEL_C |
308 OXYGEN_CHANNEL_SPDIF |
309 OXYGEN_CHANNEL_MULTICH |
310 OXYGEN_CHANNEL_AC97,
Clemens Ladisch84aa6b72008-01-16 08:28:54 +0100311 .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
Clemens Ladisch05855ba2008-01-17 09:05:09 +0100312 .dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
313 .adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100314};
315
316static int __devinit generic_oxygen_probe(struct pci_dev *pci,
317 const struct pci_device_id *pci_id)
318{
319 static int dev;
320 const struct oxygen_model *model;
321 int err;
322
323 if (dev >= SNDRV_CARDS)
324 return -ENODEV;
325 if (!enable[dev]) {
326 ++dev;
327 return -ENOENT;
328 }
329 model = pci_id->driver_data ? &model_meridian : &model_generic;
330 err = oxygen_pci_probe(pci, index[dev], id[dev], model);
331 if (err >= 0)
332 ++dev;
333 return err;
334}
335
336static struct pci_driver oxygen_driver = {
337 .name = "CMI8788",
338 .id_table = oxygen_ids,
339 .probe = generic_oxygen_probe,
340 .remove = __devexit_p(oxygen_pci_remove),
341};
342
343static int __init alsa_card_oxygen_init(void)
344{
345 return pci_register_driver(&oxygen_driver);
346}
347
348static void __exit alsa_card_oxygen_exit(void)
349{
350 pci_unregister_driver(&oxygen_driver);
351}
352
353module_init(alsa_card_oxygen_init)
354module_exit(alsa_card_oxygen_exit)