oxygen.c 23.4 KB
Newer Older
Clemens Ladisch's avatar
Clemens Ladisch committed
1
/*
2
 * C-Media CMI8788 driver for C-Media's reference design and similar models
Clemens Ladisch's avatar
Clemens Ladisch committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
 *
 *
 *  This driver is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License, version 2.
 *
 *  This driver is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this driver; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

/*
21
22
 * CMI8788:
 *
23
24
25
26
27
 *   SPI 0 -> 1st AK4396 (front)
 *   SPI 1 -> 2nd AK4396 (surround)
 *   SPI 2 -> 3rd AK4396 (center/LFE)
 *   SPI 3 -> WM8785
 *   SPI 4 -> 4th AK4396 (back)
Clemens Ladisch's avatar
Clemens Ladisch committed
28
 *
29
30
 *   GPIO 0 -> DFS0 of AK5385
 *   GPIO 1 -> DFS1 of AK5385
31
32
33
34
35
36
 *
 * X-Meridian models:
 *   GPIO 4 -> enable extension S/PDIF input
 *   GPIO 6 -> enable on-board S/PDIF input
 *
 * Claro models:
37
 *   GPIO 6 -> S/PDIF from optical (0) or coaxial (1) input
38
 *   GPIO 8 -> enable headphone amplifier
39
40
41
 *
 * CM9780:
 *
42
43
44
45
46
47
48
 *   LINE_OUT -> input of ADC
 *
 *   AUX_IN <- aux
 *   CD_IN  <- CD
 *   MIC_IN <- mic
 *
 *   GPO 0 -> route line-in (0) or AC97 output (1) to ADC input
Clemens Ladisch's avatar
Clemens Ladisch committed
49
50
 */

51
#include <linux/delay.h>
52
#include <linux/mutex.h>
Clemens Ladisch's avatar
Clemens Ladisch committed
53
#include <linux/pci.h>
54
#include <linux/module.h>
55
#include <sound/ac97_codec.h>
56
#include <sound/control.h>
Clemens Ladisch's avatar
Clemens Ladisch committed
57
#include <sound/core.h>
58
#include <sound/info.h>
Clemens Ladisch's avatar
Clemens Ladisch committed
59
60
61
62
63
#include <sound/initval.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/tlv.h>
#include "oxygen.h"
64
#include "xonar_dg.h"
65
#include "ak4396.h"
66
#include "wm8785.h"
Clemens Ladisch's avatar
Clemens Ladisch committed
67
68
69

MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
MODULE_DESCRIPTION("C-Media CMI8788 driver");
70
MODULE_LICENSE("GPL v2");
71
72
73
MODULE_SUPPORTED_DEVICE("{{C-Media,CMI8786}"
			",{C-Media,CMI8787}"
			",{C-Media,CMI8788}}");
Clemens Ladisch's avatar
Clemens Ladisch committed
74
75
76

static int index[SNDRV_CARDS] = SNDRV_DEFAULT_IDX;
static char *id[SNDRV_CARDS] = SNDRV_DEFAULT_STR;
77
static bool enable[SNDRV_CARDS] = SNDRV_DEFAULT_ENABLE_PNP;
Clemens Ladisch's avatar
Clemens Ladisch committed
78
79
80
81
82
83
84
85

module_param_array(index, int, NULL, 0444);
MODULE_PARM_DESC(index, "card index");
module_param_array(id, charp, NULL, 0444);
MODULE_PARM_DESC(id, "ID string");
module_param_array(enable, bool, NULL, 0444);
MODULE_PARM_DESC(enable, "enable card");

86
enum {
87
88
	MODEL_CMEDIA_REF,
	MODEL_MERIDIAN,
89
	MODEL_MERIDIAN_2G,
90
91
	MODEL_CLARO,
	MODEL_CLARO_HALO,
92
	MODEL_FANTASIA,
93
	MODEL_SERENADE,
94
	MODEL_2CH_OUTPUT,
95
	MODEL_HG2PCI,
96
	MODEL_XONAR_DG,
97
	MODEL_XONAR_DGX,
98
99
};

100
static const struct pci_device_id oxygen_ids[] = {
101
	/* C-Media's reference design */
102
	{ OXYGEN_PCI_SUBID(0x10b0, 0x0216), .driver_data = MODEL_CMEDIA_REF },
103
	{ OXYGEN_PCI_SUBID(0x10b0, 0x0217), .driver_data = MODEL_CMEDIA_REF },
104
105
106
107
108
109
	{ OXYGEN_PCI_SUBID(0x10b0, 0x0218), .driver_data = MODEL_CMEDIA_REF },
	{ OXYGEN_PCI_SUBID(0x10b0, 0x0219), .driver_data = MODEL_CMEDIA_REF },
	{ OXYGEN_PCI_SUBID(0x13f6, 0x0001), .driver_data = MODEL_CMEDIA_REF },
	{ OXYGEN_PCI_SUBID(0x13f6, 0x0010), .driver_data = MODEL_CMEDIA_REF },
	{ OXYGEN_PCI_SUBID(0x13f6, 0x8788), .driver_data = MODEL_CMEDIA_REF },
	{ OXYGEN_PCI_SUBID(0x147a, 0xa017), .driver_data = MODEL_CMEDIA_REF },
110
	{ OXYGEN_PCI_SUBID(0x1a58, 0x0910), .driver_data = MODEL_CMEDIA_REF },
111
112
	/* Asus Xonar DG */
	{ OXYGEN_PCI_SUBID(0x1043, 0x8467), .driver_data = MODEL_XONAR_DG },
113
114
	/* Asus Xonar DGX */
	{ OXYGEN_PCI_SUBID(0x1043, 0x8521), .driver_data = MODEL_XONAR_DGX },
115
116
	/* PCI 2.0 HD Audio */
	{ OXYGEN_PCI_SUBID(0x13f6, 0x8782), .driver_data = MODEL_2CH_OUTPUT },
117
	/* Kuroutoshikou CMI8787-HG2PCI */
118
	{ OXYGEN_PCI_SUBID(0x13f6, 0xffff), .driver_data = MODEL_HG2PCI },
119
	/* TempoTec HiFier Fantasia */
120
121
	{ OXYGEN_PCI_SUBID(0x14c3, 0x1710), .driver_data = MODEL_FANTASIA },
	/* TempoTec HiFier Serenade */
122
	{ OXYGEN_PCI_SUBID(0x14c3, 0x1711), .driver_data = MODEL_SERENADE },
123
	/* AuzenTech X-Meridian */
124
	{ OXYGEN_PCI_SUBID(0x415a, 0x5431), .driver_data = MODEL_MERIDIAN },
125
	/* AuzenTech X-Meridian 2G */
126
	{ OXYGEN_PCI_SUBID(0x5431, 0x017a), .driver_data = MODEL_MERIDIAN_2G },
127
	/* HT-Omega Claro */
128
	{ OXYGEN_PCI_SUBID(0x7284, 0x9761), .driver_data = MODEL_CLARO },
129
	/* HT-Omega Claro halo */
130
	{ OXYGEN_PCI_SUBID(0x7284, 0x9781), .driver_data = MODEL_CLARO_HALO },
Clemens Ladisch's avatar
Clemens Ladisch committed
131
132
133
134
	{ }
};
MODULE_DEVICE_TABLE(pci, oxygen_ids);

135
136
137
138
139
140

#define GPIO_AK5385_DFS_MASK	0x0003
#define GPIO_AK5385_DFS_NORMAL	0x0000
#define GPIO_AK5385_DFS_DOUBLE	0x0001
#define GPIO_AK5385_DFS_QUAD	0x0002

141
142
143
144
#define GPIO_MERIDIAN_DIG_MASK	0x0050
#define GPIO_MERIDIAN_DIG_EXT	0x0010
#define GPIO_MERIDIAN_DIG_BOARD	0x0040

145
#define GPIO_CLARO_DIG_COAX	0x0040
146
147
#define GPIO_CLARO_HP		0x0100

148
struct generic_data {
149
	unsigned int dacs;
150
	u8 ak4396_regs[4][5];
151
	u16 wm8785_regs[3];
152
153
};

Clemens Ladisch's avatar
Clemens Ladisch committed
154
155
156
157
158
static void ak4396_write(struct oxygen *chip, unsigned int codec,
			 u8 reg, u8 value)
{
	/* maps ALSA channel pair number to SPI output */
	static const u8 codec_spi_map[4] = {
159
		0, 1, 2, 4
Clemens Ladisch's avatar
Clemens Ladisch committed
160
	};
161
162
	struct generic_data *data = chip->model_data;

163
	oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
Clemens Ladisch's avatar
Clemens Ladisch committed
164
			 OXYGEN_SPI_DATA_LENGTH_2 |
165
			 OXYGEN_SPI_CLOCK_160 |
Clemens Ladisch's avatar
Clemens Ladisch committed
166
			 (codec_spi_map[codec] << OXYGEN_SPI_CODEC_SHIFT) |
167
			 OXYGEN_SPI_CEN_LATCH_CLOCK_HI,
Clemens Ladisch's avatar
Clemens Ladisch committed
168
			 AK4396_WRITE | (reg << 8) | value);
169
170
171
172
173
174
175
176
177
178
	data->ak4396_regs[codec][reg] = value;
}

static void ak4396_write_cached(struct oxygen *chip, unsigned int codec,
				u8 reg, u8 value)
{
	struct generic_data *data = chip->model_data;

	if (value != data->ak4396_regs[codec][reg])
		ak4396_write(chip, codec, reg, value);
Clemens Ladisch's avatar
Clemens Ladisch committed
179
180
181
182
}

static void wm8785_write(struct oxygen *chip, u8 reg, unsigned int value)
{
183
184
	struct generic_data *data = chip->model_data;

185
	oxygen_write_spi(chip, OXYGEN_SPI_TRIGGER |
Clemens Ladisch's avatar
Clemens Ladisch committed
186
			 OXYGEN_SPI_DATA_LENGTH_2 |
187
			 OXYGEN_SPI_CLOCK_160 |
188
189
			 (3 << OXYGEN_SPI_CODEC_SHIFT) |
			 OXYGEN_SPI_CEN_LATCH_CLOCK_LO,
Clemens Ladisch's avatar
Clemens Ladisch committed
190
			 (reg << 9) | value);
191
192
	if (reg < ARRAY_SIZE(data->wm8785_regs))
		data->wm8785_regs[reg] = value;
193
194
}

195
static void ak4396_registers_init(struct oxygen *chip)
Clemens Ladisch's avatar
Clemens Ladisch committed
196
{
197
	struct generic_data *data = chip->model_data;
Clemens Ladisch's avatar
Clemens Ladisch committed
198
199
	unsigned int i;

200
	for (i = 0; i < data->dacs; ++i) {
201
202
203
204
205
206
207
208
209
210
		ak4396_write(chip, i, AK4396_CONTROL_1,
			     AK4396_DIF_24_MSB | AK4396_RSTN);
		ak4396_write(chip, i, AK4396_CONTROL_2,
			     data->ak4396_regs[0][AK4396_CONTROL_2]);
		ak4396_write(chip, i, AK4396_CONTROL_3,
			     AK4396_PCM);
		ak4396_write(chip, i, AK4396_LCH_ATT,
			     chip->dac_volume[i * 2]);
		ak4396_write(chip, i, AK4396_RCH_ATT,
			     chip->dac_volume[i * 2 + 1]);
Clemens Ladisch's avatar
Clemens Ladisch committed
211
	}
212
213
214
215
216
217
}

static void ak4396_init(struct oxygen *chip)
{
	struct generic_data *data = chip->model_data;

218
	data->dacs = chip->model.dac_channels_pcm / 2;
219
220
	data->ak4396_regs[0][AK4396_CONTROL_2] =
		AK4396_SMUTE | AK4396_DEM_OFF | AK4396_DFS_NORMAL;
221
	ak4396_registers_init(chip);
Clemens Ladisch's avatar
Clemens Ladisch committed
222
223
224
225
226
	snd_component_add(chip->card, "AK4396");
}

static void ak5385_init(struct oxygen *chip)
{
227
228
	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_AK5385_DFS_MASK);
	oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_AK5385_DFS_MASK);
Clemens Ladisch's avatar
Clemens Ladisch committed
229
230
231
	snd_component_add(chip->card, "AK5385");
}

232
static void wm8785_registers_init(struct oxygen *chip)
Clemens Ladisch's avatar
Clemens Ladisch committed
233
{
234
235
	struct generic_data *data = chip->model_data;

236
	wm8785_write(chip, WM8785_R7, 0);
237
	wm8785_write(chip, WM8785_R0, data->wm8785_regs[0]);
238
	wm8785_write(chip, WM8785_R2, data->wm8785_regs[2]);
239
}
240

241
242
243
244
static void wm8785_init(struct oxygen *chip)
{
	struct generic_data *data = chip->model_data;

245
246
	data->wm8785_regs[0] =
		WM8785_MCR_SLAVE | WM8785_OSR_SINGLE | WM8785_FORMAT_LJUST;
247
	data->wm8785_regs[2] = WM8785_HPFR | WM8785_HPFL;
248
	wm8785_registers_init(chip);
Clemens Ladisch's avatar
Clemens Ladisch committed
249
250
251
252
253
254
255
256
257
258
259
	snd_component_add(chip->card, "WM8785");
}

static void generic_init(struct oxygen *chip)
{
	ak4396_init(chip);
	wm8785_init(chip);
}

static void meridian_init(struct oxygen *chip)
{
260
261
262
263
	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL,
			  GPIO_MERIDIAN_DIG_MASK);
	oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
			      GPIO_MERIDIAN_DIG_BOARD, GPIO_MERIDIAN_DIG_MASK);
Clemens Ladisch's avatar
Clemens Ladisch committed
264
265
266
267
	ak4396_init(chip);
	ak5385_init(chip);
}

268
269
270
271
272
273
274
275
276
static void claro_enable_hp(struct oxygen *chip)
{
	msleep(300);
	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_CLARO_HP);
	oxygen_set_bits16(chip, OXYGEN_GPIO_DATA, GPIO_CLARO_HP);
}

static void claro_init(struct oxygen *chip)
{
277
278
	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_CLARO_DIG_COAX);
	oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_CLARO_DIG_COAX);
279
280
281
282
283
284
	ak4396_init(chip);
	wm8785_init(chip);
	claro_enable_hp(chip);
}

static void claro_halo_init(struct oxygen *chip)
285
{
286
287
	oxygen_set_bits16(chip, OXYGEN_GPIO_CONTROL, GPIO_CLARO_DIG_COAX);
	oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_CLARO_DIG_COAX);
288
289
	ak4396_init(chip);
	ak5385_init(chip);
290
	claro_enable_hp(chip);
291
292
}

293
static void fantasia_init(struct oxygen *chip)
294
295
296
297
298
{
	ak4396_init(chip);
	snd_component_add(chip->card, "CS5340");
}

299
static void stereo_output_init(struct oxygen *chip)
300
301
302
303
{
	ak4396_init(chip);
}

Clemens Ladisch's avatar
Clemens Ladisch committed
304
305
306
307
static void generic_cleanup(struct oxygen *chip)
{
}

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
static void claro_disable_hp(struct oxygen *chip)
{
	oxygen_clear_bits16(chip, OXYGEN_GPIO_DATA, GPIO_CLARO_HP);
}

static void claro_cleanup(struct oxygen *chip)
{
	claro_disable_hp(chip);
}

static void claro_suspend(struct oxygen *chip)
{
	claro_disable_hp(chip);
}

323
324
325
326
327
328
static void generic_resume(struct oxygen *chip)
{
	ak4396_registers_init(chip);
	wm8785_registers_init(chip);
}

329
330
331
332
333
static void meridian_resume(struct oxygen *chip)
{
	ak4396_registers_init(chip);
}

334
static void claro_resume(struct oxygen *chip)
335
336
{
	ak4396_registers_init(chip);
337
	claro_enable_hp(chip);
338
339
}

340
341
342
343
344
static void stereo_resume(struct oxygen *chip)
{
	ak4396_registers_init(chip);
}

Clemens Ladisch's avatar
Clemens Ladisch committed
345
346
347
static void set_ak4396_params(struct oxygen *chip,
			      struct snd_pcm_hw_params *params)
{
348
	struct generic_data *data = chip->model_data;
Clemens Ladisch's avatar
Clemens Ladisch committed
349
350
351
	unsigned int i;
	u8 value;

352
	value = data->ak4396_regs[0][AK4396_CONTROL_2] & ~AK4396_DFS_MASK;
Clemens Ladisch's avatar
Clemens Ladisch committed
353
354
	if (params_rate(params) <= 54000)
		value |= AK4396_DFS_NORMAL;
355
	else if (params_rate(params) <= 108000)
Clemens Ladisch's avatar
Clemens Ladisch committed
356
357
358
		value |= AK4396_DFS_DOUBLE;
	else
		value |= AK4396_DFS_QUAD;
359
360
361

	msleep(1); /* wait for the new MCLK to become stable */

362
	if (value != data->ak4396_regs[0][AK4396_CONTROL_2]) {
363
		for (i = 0; i < data->dacs; ++i) {
364
365
366
367
368
369
370
371
372
373
374
			ak4396_write(chip, i, AK4396_CONTROL_1,
				     AK4396_DIF_24_MSB);
			ak4396_write(chip, i, AK4396_CONTROL_2, value);
			ak4396_write(chip, i, AK4396_CONTROL_1,
				     AK4396_DIF_24_MSB | AK4396_RSTN);
		}
	}
}

static void update_ak4396_volume(struct oxygen *chip)
{
375
	struct generic_data *data = chip->model_data;
376
377
	unsigned int i;

378
	for (i = 0; i < data->dacs; ++i) {
379
380
381
382
		ak4396_write_cached(chip, i, AK4396_LCH_ATT,
				    chip->dac_volume[i * 2]);
		ak4396_write_cached(chip, i, AK4396_RCH_ATT,
				    chip->dac_volume[i * 2 + 1]);
Clemens Ladisch's avatar
Clemens Ladisch committed
383
384
385
386
387
	}
}

static void update_ak4396_mute(struct oxygen *chip)
{
388
	struct generic_data *data = chip->model_data;
Clemens Ladisch's avatar
Clemens Ladisch committed
389
390
391
	unsigned int i;
	u8 value;

392
	value = data->ak4396_regs[0][AK4396_CONTROL_2] & ~AK4396_SMUTE;
Clemens Ladisch's avatar
Clemens Ladisch committed
393
394
	if (chip->dac_mute)
		value |= AK4396_SMUTE;
395
	for (i = 0; i < data->dacs; ++i)
396
		ak4396_write_cached(chip, i, AK4396_CONTROL_2, value);
Clemens Ladisch's avatar
Clemens Ladisch committed
397
398
399
400
401
}

static void set_wm8785_params(struct oxygen *chip,
			      struct snd_pcm_hw_params *params)
{
402
	struct generic_data *data = chip->model_data;
Clemens Ladisch's avatar
Clemens Ladisch committed
403
404
	unsigned int value;

405
	value = WM8785_MCR_SLAVE | WM8785_FORMAT_LJUST;
406
407
408
	if (params_rate(params) <= 48000)
		value |= WM8785_OSR_SINGLE;
	else if (params_rate(params) <= 96000)
Clemens Ladisch's avatar
Clemens Ladisch committed
409
410
		value |= WM8785_OSR_DOUBLE;
	else
411
		value |= WM8785_OSR_QUAD;
412
413
414
	if (value != data->wm8785_regs[0]) {
		wm8785_write(chip, WM8785_R7, 0);
		wm8785_write(chip, WM8785_R0, value);
415
		wm8785_write(chip, WM8785_R2, data->wm8785_regs[2]);
416
	}
Clemens Ladisch's avatar
Clemens Ladisch committed
417
418
419
420
421
422
423
424
}

static void set_ak5385_params(struct oxygen *chip,
			      struct snd_pcm_hw_params *params)
{
	unsigned int value;

	if (params_rate(params) <= 54000)
425
		value = GPIO_AK5385_DFS_NORMAL;
Clemens Ladisch's avatar
Clemens Ladisch committed
426
	else if (params_rate(params) <= 108000)
427
		value = GPIO_AK5385_DFS_DOUBLE;
Clemens Ladisch's avatar
Clemens Ladisch committed
428
	else
429
430
431
		value = GPIO_AK5385_DFS_QUAD;
	oxygen_write16_masked(chip, OXYGEN_GPIO_DATA,
			      value, GPIO_AK5385_DFS_MASK);
Clemens Ladisch's avatar
Clemens Ladisch committed
432
433
}

434
435
436
437
static void set_no_params(struct oxygen *chip, struct snd_pcm_hw_params *params)
{
}

438
439
440
441
442
443
444
static int rolloff_info(struct snd_kcontrol *ctl,
			struct snd_ctl_elem_info *info)
{
	static const char *const names[2] = {
		"Sharp Roll-off", "Slow Roll-off"
	};

445
	return snd_ctl_enum_info(info, 1, 2, names);
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
}

static int rolloff_get(struct snd_kcontrol *ctl,
		       struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	struct generic_data *data = chip->model_data;

	value->value.enumerated.item[0] =
		(data->ak4396_regs[0][AK4396_CONTROL_2] & AK4396_SLOW) != 0;
	return 0;
}

static int rolloff_put(struct snd_kcontrol *ctl,
		       struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	struct generic_data *data = chip->model_data;
	unsigned int i;
	int changed;
	u8 reg;

	mutex_lock(&chip->mutex);
	reg = data->ak4396_regs[0][AK4396_CONTROL_2];
	if (value->value.enumerated.item[0])
		reg |= AK4396_SLOW;
	else
		reg &= ~AK4396_SLOW;
	changed = reg != data->ak4396_regs[0][AK4396_CONTROL_2];
	if (changed) {
476
		for (i = 0; i < data->dacs; ++i)
477
478
479
480
481
482
483
484
485
486
487
488
489
490
			ak4396_write(chip, i, AK4396_CONTROL_2, reg);
	}
	mutex_unlock(&chip->mutex);
	return changed;
}

static const struct snd_kcontrol_new rolloff_control = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "DAC Filter Playback Enum",
	.info = rolloff_info,
	.get = rolloff_get,
	.put = rolloff_put,
};

491
492
493
494
495
496
static int hpf_info(struct snd_kcontrol *ctl, struct snd_ctl_elem_info *info)
{
	static const char *const names[2] = {
		"None", "High-pass Filter"
	};

497
	return snd_ctl_enum_info(info, 1, 2, names);
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
}

static int hpf_get(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	struct generic_data *data = chip->model_data;

	value->value.enumerated.item[0] =
		(data->wm8785_regs[WM8785_R2] & WM8785_HPFR) != 0;
	return 0;
}

static int hpf_put(struct snd_kcontrol *ctl, struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	struct generic_data *data = chip->model_data;
	unsigned int reg;
	int changed;

	mutex_lock(&chip->mutex);
	reg = data->wm8785_regs[WM8785_R2] & ~(WM8785_HPFR | WM8785_HPFL);
	if (value->value.enumerated.item[0])
		reg |= WM8785_HPFR | WM8785_HPFL;
	changed = reg != data->wm8785_regs[WM8785_R2];
	if (changed)
		wm8785_write(chip, WM8785_R2, reg);
	mutex_unlock(&chip->mutex);
	return changed;
}

static const struct snd_kcontrol_new hpf_control = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "ADC Filter Capture Enum",
	.info = hpf_info,
	.get = hpf_get,
	.put = hpf_put,
};

536
537
static int meridian_dig_source_info(struct snd_kcontrol *ctl,
				    struct snd_ctl_elem_info *info)
538
539
540
541
542
543
{
	static const char *const names[2] = { "On-board", "Extension" };

	return snd_ctl_enum_info(info, 1, 2, names);
}

544
545
546
547
548
549
550
551
552
553
static int claro_dig_source_info(struct snd_kcontrol *ctl,
				 struct snd_ctl_elem_info *info)
{
	static const char *const names[2] = { "Optical", "Coaxial" };

	return snd_ctl_enum_info(info, 1, 2, names);
}

static int meridian_dig_source_get(struct snd_kcontrol *ctl,
				   struct snd_ctl_elem_value *value)
554
555
556
557
558
559
560
561
562
{
	struct oxygen *chip = ctl->private_data;

	value->value.enumerated.item[0] =
		!!(oxygen_read16(chip, OXYGEN_GPIO_DATA) &
		   GPIO_MERIDIAN_DIG_EXT);
	return 0;
}

563
564
565
566
567
568
569
570
571
572
573
574
575
static int claro_dig_source_get(struct snd_kcontrol *ctl,
				struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;

	value->value.enumerated.item[0] =
		!!(oxygen_read16(chip, OXYGEN_GPIO_DATA) &
		   GPIO_CLARO_DIG_COAX);
	return 0;
}

static int meridian_dig_source_put(struct snd_kcontrol *ctl,
				   struct snd_ctl_elem_value *value)
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
{
	struct oxygen *chip = ctl->private_data;
	u16 old_reg, new_reg;
	int changed;

	mutex_lock(&chip->mutex);
	old_reg = oxygen_read16(chip, OXYGEN_GPIO_DATA);
	new_reg = old_reg & ~GPIO_MERIDIAN_DIG_MASK;
	if (value->value.enumerated.item[0] == 0)
		new_reg |= GPIO_MERIDIAN_DIG_BOARD;
	else
		new_reg |= GPIO_MERIDIAN_DIG_EXT;
	changed = new_reg != old_reg;
	if (changed)
		oxygen_write16(chip, OXYGEN_GPIO_DATA, new_reg);
	mutex_unlock(&chip->mutex);
	return changed;
}

595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
static int claro_dig_source_put(struct snd_kcontrol *ctl,
				struct snd_ctl_elem_value *value)
{
	struct oxygen *chip = ctl->private_data;
	u16 old_reg, new_reg;
	int changed;

	mutex_lock(&chip->mutex);
	old_reg = oxygen_read16(chip, OXYGEN_GPIO_DATA);
	new_reg = old_reg & ~GPIO_CLARO_DIG_COAX;
	if (value->value.enumerated.item[0])
		new_reg |= GPIO_CLARO_DIG_COAX;
	changed = new_reg != old_reg;
	if (changed)
		oxygen_write16(chip, OXYGEN_GPIO_DATA, new_reg);
	mutex_unlock(&chip->mutex);
	return changed;
}

614
615
616
617
618
619
620
621
static const struct snd_kcontrol_new meridian_dig_source_control = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "IEC958 Source Capture Enum",
	.info = meridian_dig_source_info,
	.get = meridian_dig_source_get,
	.put = meridian_dig_source_put,
};

622
623
624
625
626
627
628
629
static const struct snd_kcontrol_new claro_dig_source_control = {
	.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
	.name = "IEC958 Source Capture Enum",
	.info = claro_dig_source_info,
	.get = claro_dig_source_get,
	.put = claro_dig_source_put,
};

630
631
632
633
634
static int generic_mixer_init(struct oxygen *chip)
{
	return snd_ctl_add(chip->card, snd_ctl_new1(&rolloff_control, chip));
}

635
636
637
638
639
640
641
642
643
644
645
646
647
static int generic_wm8785_mixer_init(struct oxygen *chip)
{
	int err;

	err = generic_mixer_init(chip);
	if (err < 0)
		return err;
	err = snd_ctl_add(chip->card, snd_ctl_new1(&hpf_control, chip));
	if (err < 0)
		return err;
	return 0;
}

648
649
650
651
652
653
654
655
656
657
658
659
660
661
static int meridian_mixer_init(struct oxygen *chip)
{
	int err;

	err = generic_mixer_init(chip);
	if (err < 0)
		return err;
	err = snd_ctl_add(chip->card,
			  snd_ctl_new1(&meridian_dig_source_control, chip));
	if (err < 0)
		return err;
	return 0;
}

662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
static int claro_mixer_init(struct oxygen *chip)
{
	int err;

	err = generic_wm8785_mixer_init(chip);
	if (err < 0)
		return err;
	err = snd_ctl_add(chip->card,
			  snd_ctl_new1(&claro_dig_source_control, chip));
	if (err < 0)
		return err;
	return 0;
}

static int claro_halo_mixer_init(struct oxygen *chip)
{
	int err;

	err = generic_mixer_init(chip);
	if (err < 0)
		return err;
	err = snd_ctl_add(chip->card,
			  snd_ctl_new1(&claro_dig_source_control, chip));
	if (err < 0)
		return err;
	return 0;
}

690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
static void dump_ak4396_registers(struct oxygen *chip,
				  struct snd_info_buffer *buffer)
{
	struct generic_data *data = chip->model_data;
	unsigned int dac, i;

	for (dac = 0; dac < data->dacs; ++dac) {
		snd_iprintf(buffer, "\nAK4396 %u:", dac + 1);
		for (i = 0; i < 5; ++i)
			snd_iprintf(buffer, " %02x", data->ak4396_regs[dac][i]);
	}
	snd_iprintf(buffer, "\n");
}

static void dump_wm8785_registers(struct oxygen *chip,
				  struct snd_info_buffer *buffer)
{
	struct generic_data *data = chip->model_data;
	unsigned int i;

	snd_iprintf(buffer, "\nWM8785:");
	for (i = 0; i < 3; ++i)
		snd_iprintf(buffer, " %03x", data->wm8785_regs[i]);
	snd_iprintf(buffer, "\n");
}

static void dump_oxygen_registers(struct oxygen *chip,
				  struct snd_info_buffer *buffer)
{
	dump_ak4396_registers(chip, buffer);
	dump_wm8785_registers(chip, buffer);
}

Clemens Ladisch's avatar
Clemens Ladisch committed
723
724
725
726
727
728
729
static const DECLARE_TLV_DB_LINEAR(ak4396_db_scale, TLV_DB_GAIN_MUTE, 0);

static const struct oxygen_model model_generic = {
	.shortname = "C-Media CMI8788",
	.longname = "C-Media Oxygen HD Audio",
	.chip = "CMI8788",
	.init = generic_init,
730
	.mixer_init = generic_wm8785_mixer_init,
Clemens Ladisch's avatar
Clemens Ladisch committed
731
	.cleanup = generic_cleanup,
732
	.resume = generic_resume,
Clemens Ladisch's avatar
Clemens Ladisch committed
733
734
735
736
	.set_dac_params = set_ak4396_params,
	.set_adc_params = set_wm8785_params,
	.update_dac_volume = update_ak4396_volume,
	.update_dac_mute = update_ak4396_mute,
737
	.dump_registers = dump_oxygen_registers,
738
	.dac_tlv = ak4396_db_scale,
739
	.model_data_size = sizeof(struct generic_data),
740
741
742
743
744
	.device_config = PLAYBACK_0_TO_I2S |
			 PLAYBACK_1_TO_SPDIF |
			 PLAYBACK_2_TO_AC97_1 |
			 CAPTURE_0_FROM_I2S_1 |
			 CAPTURE_1_FROM_SPDIF |
745
746
			 CAPTURE_2_FROM_AC97_1 |
			 AC97_CD_INPUT,
747
748
	.dac_channels_pcm = 8,
	.dac_channels_mixer = 8,
749
750
	.dac_volume_min = 0,
	.dac_volume_max = 255,
751
752
	.function_flags = OXYGEN_FUNCTION_SPI |
			  OXYGEN_FUNCTION_ENABLE_SPI_4_5,
753
	.dac_mclks = OXYGEN_MCLKS(256, 128, 128),
754
	.adc_mclks = OXYGEN_MCLKS(256, 256, 128),
755
756
	.dac_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
	.adc_i2s_format = OXYGEN_I2S_FORMAT_LJUST,
Clemens Ladisch's avatar
Clemens Ladisch committed
757
758
};

759
760
static int get_oxygen_model(struct oxygen *chip,
			    const struct pci_device_id *id)
761
{
762
763
764
765
766
767
768
769
	static const char *const names[] = {
		[MODEL_MERIDIAN]	= "AuzenTech X-Meridian",
		[MODEL_MERIDIAN_2G]	= "AuzenTech X-Meridian 2G",
		[MODEL_CLARO]		= "HT-Omega Claro",
		[MODEL_CLARO_HALO]	= "HT-Omega Claro halo",
		[MODEL_FANTASIA]	= "TempoTec HiFier Fantasia",
		[MODEL_SERENADE]	= "TempoTec HiFier Serenade",
		[MODEL_HG2PCI]		= "CMI8787-HG2PCI",
770
771
		[MODEL_XONAR_DG]        = "Xonar DG",
		[MODEL_XONAR_DGX]       = "Xonar DGX",
772
773
	};

774
775
776
	chip->model = model_generic;
	switch (id->driver_data) {
	case MODEL_MERIDIAN:
777
	case MODEL_MERIDIAN_2G:
778
		chip->model.init = meridian_init;
779
		chip->model.mixer_init = meridian_mixer_init;
780
781
		chip->model.resume = meridian_resume;
		chip->model.set_adc_params = set_ak5385_params;
782
		chip->model.dump_registers = dump_ak4396_registers;
783
784
785
786
		chip->model.device_config = PLAYBACK_0_TO_I2S |
					    PLAYBACK_1_TO_SPDIF |
					    CAPTURE_0_FROM_I2S_2 |
					    CAPTURE_1_FROM_SPDIF;
787
788
		if (id->driver_data == MODEL_MERIDIAN)
			chip->model.device_config |= AC97_CD_INPUT;
789
		break;
790
791
	case MODEL_CLARO:
		chip->model.init = claro_init;
792
		chip->model.mixer_init = claro_mixer_init;
793
794
795
796
797
798
		chip->model.cleanup = claro_cleanup;
		chip->model.suspend = claro_suspend;
		chip->model.resume = claro_resume;
		break;
	case MODEL_CLARO_HALO:
		chip->model.init = claro_halo_init;
799
		chip->model.mixer_init = claro_halo_mixer_init;
800
801
802
		chip->model.cleanup = claro_cleanup;
		chip->model.suspend = claro_suspend;
		chip->model.resume = claro_resume;
803
		chip->model.set_adc_params = set_ak5385_params;
804
		chip->model.dump_registers = dump_ak4396_registers;
805
806
807
808
		chip->model.device_config = PLAYBACK_0_TO_I2S |
					    PLAYBACK_1_TO_SPDIF |
					    CAPTURE_0_FROM_I2S_2 |
					    CAPTURE_1_FROM_SPDIF;
809
		break;
810
	case MODEL_FANTASIA:
811
	case MODEL_SERENADE:
812
	case MODEL_2CH_OUTPUT:
813
	case MODEL_HG2PCI:
814
815
		chip->model.shortname = "C-Media CMI8787";
		chip->model.chip = "CMI8787";
816
817
		if (id->driver_data == MODEL_FANTASIA)
			chip->model.init = fantasia_init;
818
		else
819
			chip->model.init = stereo_output_init;
820
821
822
		chip->model.resume = stereo_resume;
		chip->model.mixer_init = generic_mixer_init;
		chip->model.set_adc_params = set_no_params;
823
		chip->model.dump_registers = dump_ak4396_registers;
824
		chip->model.device_config = PLAYBACK_0_TO_I2S |
825
					    PLAYBACK_1_TO_SPDIF;
826
		if (id->driver_data == MODEL_FANTASIA) {
827
			chip->model.device_config |= CAPTURE_0_FROM_I2S_1;
828
829
			chip->model.adc_mclks = OXYGEN_MCLKS(256, 128, 128);
		}
830
831
		chip->model.dac_channels_pcm = 2;
		chip->model.dac_channels_mixer = 2;
832
		break;
833
	case MODEL_XONAR_DG:
834
835
	case MODEL_XONAR_DGX:
		chip->model = model_xonar_dg;
836
		break;
837
838
	}
	if (id->driver_data == MODEL_MERIDIAN ||
839
	    id->driver_data == MODEL_MERIDIAN_2G ||
840
	    id->driver_data == MODEL_CLARO_HALO) {
841
842
843
		chip->model.misc_flags = OXYGEN_MISC_MIDI;
		chip->model.device_config |= MIDI_OUTPUT | MIDI_INPUT;
	}
844
845
	if (id->driver_data < ARRAY_SIZE(names) && names[id->driver_data])
		chip->model.shortname = names[id->driver_data];
846
847
848
	return 0;
}

849
850
static int generic_oxygen_probe(struct pci_dev *pci,
				const struct pci_device_id *pci_id)
Clemens Ladisch's avatar
Clemens Ladisch committed
851
852
853
854
855
856
857
858
859
860
{
	static int dev;
	int err;

	if (dev >= SNDRV_CARDS)
		return -ENODEV;
	if (!enable[dev]) {
		++dev;
		return -ENOENT;
	}
861
	err = oxygen_pci_probe(pci, index[dev], id[dev], THIS_MODULE,
862
			       oxygen_ids, get_oxygen_model);
Clemens Ladisch's avatar
Clemens Ladisch committed
863
864
865
866
867
868
	if (err >= 0)
		++dev;
	return err;
}

static struct pci_driver oxygen_driver = {
869
	.name = KBUILD_MODNAME,
Clemens Ladisch's avatar
Clemens Ladisch committed
870
871
	.id_table = oxygen_ids,
	.probe = generic_oxygen_probe,
872
	.remove = oxygen_pci_remove,
873
#ifdef CONFIG_PM_SLEEP
874
875
876
	.driver = {
		.pm = &oxygen_pci_pm,
	},
877
#endif
Clemens Ladisch's avatar
Clemens Ladisch committed
878
879
};

880
module_pci_driver(oxygen_driver);