blob: e2dda16d5dcc0caa8fc2a327738b53c9811ccbbb [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>
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
39MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
40MODULE_DESCRIPTION("C-Media CMI8788 driver");
41MODULE_LICENSE("GPL");
42MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8788}}");
43
44static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
45static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
46static int enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
47
48module_param_array(index, int, NULL, 0444);
49MODULE_PARM_DESC(index, "card index");
50module_param_array(id, charp, NULL, 0444);
51MODULE_PARM_DESC(id, "ID string");
52module_param_array(enable, bool, NULL, 0444);
53MODULE_PARM_DESC(enable, "enable card");
54
55static 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};
70MODULE_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
97static 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] = {
Clemens Ladisch7113e952008-01-14 08:55:03 +0100102 0, 1, 2, 4
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100103 };
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
111static 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
119static 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
134static 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
141static 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
149static void generic_init(struct oxygen *chip)
150{
151 ak4396_init(chip);
152 wm8785_init(chip);
153}
154
155static void meridian_init(struct oxygen *chip)
156{
157 ak4396_init(chip);
158 ak5385_init(chip);
159}
160
161static void generic_cleanup(struct oxygen *chip)
162{
163}
164
165static 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
186static 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
196static 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
208static 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
231static 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
245static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0);
246
247static 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,
Clemens Ladische85e0922008-01-16 08:30:38 +0100259 .used_channels = OXYGEN_CHANNEL_A |
260 OXYGEN_CHANNEL_C |
261 OXYGEN_CHANNEL_SPDIF |
262 OXYGEN_CHANNEL_MULTICH |
263 OXYGEN_CHANNEL_AC97,
Clemens Ladisch84aa6b72008-01-16 08:28:54 +0100264 .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100265};
266static const struct oxygen_model model_meridian = {
267 .shortname = "C-Media CMI8788",
268 .longname = "C-Media Oxygen HD Audio",
269 .chip = "CMI8788",
270 .owner = THIS_MODULE,
271 .init = meridian_init,
272 .cleanup = generic_cleanup,
273 .set_dac_params = set_ak4396_params,
274 .set_adc_params = set_ak5385_params,
275 .update_dac_volume = update_ak4396_volume,
276 .update_dac_mute = update_ak4396_mute,
277 .dac_tlv = ak4396_db_scale,
Clemens Ladische85e0922008-01-16 08:30:38 +0100278 .used_channels = OXYGEN_CHANNEL_B |
279 OXYGEN_CHANNEL_C |
280 OXYGEN_CHANNEL_SPDIF |
281 OXYGEN_CHANNEL_MULTICH |
282 OXYGEN_CHANNEL_AC97,
Clemens Ladisch84aa6b72008-01-16 08:28:54 +0100283 .function_flags = OXYGEN_FUNCTION_ENABLE_SPI_4_5,
Clemens Ladischd0ce9942007-12-23 19:50:57 +0100284};
285
286static int __devinit generic_oxygen_probe(struct pci_dev *pci,
287 const struct pci_device_id *pci_id)
288{
289 static int dev;
290 const struct oxygen_model *model;
291 int err;
292
293 if (dev >= SNDRV_CARDS)
294 return -ENODEV;
295 if (!enable[dev]) {
296 ++dev;
297 return -ENOENT;
298 }
299 model = pci_id->driver_data ? &model_meridian : &model_generic;
300 err = oxygen_pci_probe(pci, index[dev], id[dev], model);
301 if (err >= 0)
302 ++dev;
303 return err;
304}
305
306static struct pci_driver oxygen_driver = {
307 .name = "CMI8788",
308 .id_table = oxygen_ids,
309 .probe = generic_oxygen_probe,
310 .remove = __devexit_p(oxygen_pci_remove),
311};
312
313static int __init alsa_card_oxygen_init(void)
314{
315 return pci_register_driver(&oxygen_driver);
316}
317
318static void __exit alsa_card_oxygen_exit(void)
319{
320 pci_unregister_driver(&oxygen_driver);
321}
322
323module_init(alsa_card_oxygen_init)
324module_exit(alsa_card_oxygen_exit)