• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/sound/i2c/other/
1/*
2 *   ALSA driver for AK4524 / AK4528 / AK4529 / AK4355 / AK4358 / AK4381
3 *   AD and DA converters
4 *
5 *	Copyright (c) 2000-2004 Jaroslav Kysela <perex@perex.cz>,
6 *				Takashi Iwai <tiwai@suse.de>
7 *
8 *   This program is free software; you can redistribute it and/or modify
9 *   it under the terms of the GNU General Public License as published by
10 *   the Free Software Foundation; either version 2 of the License, or
11 *   (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21 *
22 */
23
24#include <asm/io.h>
25#include <linux/delay.h>
26#include <linux/interrupt.h>
27#include <linux/init.h>
28#include <sound/core.h>
29#include <sound/control.h>
30#include <sound/tlv.h>
31#include <sound/ak4xxx-adda.h>
32#include <sound/info.h>
33
34MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.de>");
35MODULE_DESCRIPTION("Routines for control of AK452x / AK43xx  AD/DA converters");
36MODULE_LICENSE("GPL");
37
38/* write the given register and save the data to the cache */
39void snd_akm4xxx_write(struct snd_akm4xxx *ak, int chip, unsigned char reg,
40		       unsigned char val)
41{
42	ak->ops.lock(ak, chip);
43	ak->ops.write(ak, chip, reg, val);
44
45	/* save the data */
46	snd_akm4xxx_set(ak, chip, reg, val);
47	ak->ops.unlock(ak, chip);
48}
49
50EXPORT_SYMBOL(snd_akm4xxx_write);
51
52/* reset procedure for AK4524 and AK4528 */
53static void ak4524_reset(struct snd_akm4xxx *ak, int state)
54{
55	unsigned int chip;
56	unsigned char reg;
57
58	for (chip = 0; chip < ak->num_dacs/2; chip++) {
59		snd_akm4xxx_write(ak, chip, 0x01, state ? 0x00 : 0x03);
60		if (state)
61			continue;
62		/* DAC volumes */
63		for (reg = 0x04; reg < ak->total_regs; reg++)
64			snd_akm4xxx_write(ak, chip, reg,
65					  snd_akm4xxx_get(ak, chip, reg));
66	}
67}
68
69/* reset procedure for AK4355 and AK4358 */
70static void ak435X_reset(struct snd_akm4xxx *ak, int state)
71{
72	unsigned char reg;
73
74	if (state) {
75		snd_akm4xxx_write(ak, 0, 0x01, 0x02); /* reset and soft-mute */
76		return;
77	}
78	for (reg = 0x00; reg < ak->total_regs; reg++)
79		if (reg != 0x01)
80			snd_akm4xxx_write(ak, 0, reg,
81					  snd_akm4xxx_get(ak, 0, reg));
82	snd_akm4xxx_write(ak, 0, 0x01, 0x01); /* un-reset, unmute */
83}
84
85/* reset procedure for AK4381 */
86static void ak4381_reset(struct snd_akm4xxx *ak, int state)
87{
88	unsigned int chip;
89	unsigned char reg;
90	for (chip = 0; chip < ak->num_dacs/2; chip++) {
91		snd_akm4xxx_write(ak, chip, 0x00, state ? 0x0c : 0x0f);
92		if (state)
93			continue;
94		for (reg = 0x01; reg < ak->total_regs; reg++)
95			snd_akm4xxx_write(ak, chip, reg,
96					  snd_akm4xxx_get(ak, chip, reg));
97	}
98}
99
100/*
101 * reset the AKM codecs
102 * @state: 1 = reset codec, 0 = restore the registers
103 *
104 * assert the reset operation and restores the register values to the chips.
105 */
106void snd_akm4xxx_reset(struct snd_akm4xxx *ak, int state)
107{
108	switch (ak->type) {
109	case SND_AK4524:
110	case SND_AK4528:
111	case SND_AK4620:
112		ak4524_reset(ak, state);
113		break;
114	case SND_AK4529:
115		break;
116	case SND_AK4355:
117		ak435X_reset(ak, state);
118		break;
119	case SND_AK4358:
120		ak435X_reset(ak, state);
121		break;
122	case SND_AK4381:
123		ak4381_reset(ak, state);
124		break;
125	default:
126		break;
127	}
128}
129
130EXPORT_SYMBOL(snd_akm4xxx_reset);
131
132
133/*
134 * Volume conversion table for non-linear volumes
135 * from -63.5dB (mute) to 0dB step 0.5dB
136 *
137 * Used for AK4524/AK4620 input/ouput attenuation, AK4528, and
138 * AK5365 input attenuation
139 */
140static const unsigned char vol_cvt_datt[128] = {
141	0x00, 0x01, 0x01, 0x02, 0x02, 0x03, 0x03, 0x04,
142	0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x06, 0x06,
143	0x06, 0x07, 0x07, 0x08, 0x08, 0x08, 0x09, 0x0a,
144	0x0a, 0x0b, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x0f,
145	0x10, 0x10, 0x11, 0x12, 0x12, 0x13, 0x13, 0x14,
146	0x15, 0x16, 0x17, 0x17, 0x18, 0x19, 0x1a, 0x1c,
147	0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x23,
148	0x24, 0x25, 0x26, 0x28, 0x29, 0x2a, 0x2b, 0x2d,
149	0x2e, 0x30, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
150	0x37, 0x38, 0x39, 0x3b, 0x3c, 0x3e, 0x3f, 0x40,
151	0x41, 0x42, 0x43, 0x44, 0x46, 0x47, 0x48, 0x4a,
152	0x4b, 0x4d, 0x4e, 0x50, 0x51, 0x52, 0x53, 0x54,
153	0x55, 0x56, 0x58, 0x59, 0x5b, 0x5c, 0x5e, 0x5f,
154	0x60, 0x61, 0x62, 0x64, 0x65, 0x66, 0x67, 0x69,
155	0x6a, 0x6c, 0x6d, 0x6f, 0x70, 0x71, 0x72, 0x73,
156	0x75, 0x76, 0x77, 0x79, 0x7a, 0x7c, 0x7d, 0x7f,
157};
158
159/*
160 * dB tables
161 */
162static const DECLARE_TLV_DB_SCALE(db_scale_vol_datt, -6350, 50, 1);
163static const DECLARE_TLV_DB_SCALE(db_scale_8bit, -12750, 50, 1);
164static const DECLARE_TLV_DB_SCALE(db_scale_7bit, -6350, 50, 1);
165static const DECLARE_TLV_DB_LINEAR(db_scale_linear, TLV_DB_GAIN_MUTE, 0);
166
167/*
168 * initialize all the ak4xxx chips
169 */
170void snd_akm4xxx_init(struct snd_akm4xxx *ak)
171{
172	static const unsigned char inits_ak4524[] = {
173		0x00, 0x07, /* 0: all power up */
174		0x01, 0x00, /* 1: ADC/DAC reset */
175		0x02, 0x60, /* 2: 24bit I2S */
176		0x03, 0x19, /* 3: deemphasis off */
177		0x01, 0x03, /* 1: ADC/DAC enable */
178		0x04, 0x00, /* 4: ADC left muted */
179		0x05, 0x00, /* 5: ADC right muted */
180		0x06, 0x00, /* 6: DAC left muted */
181		0x07, 0x00, /* 7: DAC right muted */
182		0xff, 0xff
183	};
184	static const unsigned char inits_ak4528[] = {
185		0x00, 0x07, /* 0: all power up */
186		0x01, 0x00, /* 1: ADC/DAC reset */
187		0x02, 0x60, /* 2: 24bit I2S */
188		0x03, 0x0d, /* 3: deemphasis off, turn LR highpass filters on */
189		0x01, 0x03, /* 1: ADC/DAC enable */
190		0x04, 0x00, /* 4: ADC left muted */
191		0x05, 0x00, /* 5: ADC right muted */
192		0xff, 0xff
193	};
194	static const unsigned char inits_ak4529[] = {
195		0x09, 0x01, /* 9: ATS=0, RSTN=1 */
196		0x0a, 0x3f, /* A: all power up, no zero/overflow detection */
197		0x00, 0x0c, /* 0: TDM=0, 24bit I2S, SMUTE=0 */
198		0x01, 0x00, /* 1: ACKS=0, ADC, loop off */
199		0x02, 0xff, /* 2: LOUT1 muted */
200		0x03, 0xff, /* 3: ROUT1 muted */
201		0x04, 0xff, /* 4: LOUT2 muted */
202		0x05, 0xff, /* 5: ROUT2 muted */
203		0x06, 0xff, /* 6: LOUT3 muted */
204		0x07, 0xff, /* 7: ROUT3 muted */
205		0x0b, 0xff, /* B: LOUT4 muted */
206		0x0c, 0xff, /* C: ROUT4 muted */
207		0x08, 0x55, /* 8: deemphasis all off */
208		0xff, 0xff
209	};
210	static const unsigned char inits_ak4355[] = {
211		0x01, 0x02, /* 1: reset and soft-mute */
212		0x00, 0x06, /* 0: mode3(i2s), disable auto-clock detect,
213			     * disable DZF, sharp roll-off, RSTN#=0 */
214		0x02, 0x0e, /* 2: DA's power up, normal speed, RSTN#=0 */
215		// 0x02, 0x2e, /* quad speed */
216		0x03, 0x01, /* 3: de-emphasis off */
217		0x04, 0x00, /* 4: LOUT1 volume muted */
218		0x05, 0x00, /* 5: ROUT1 volume muted */
219		0x06, 0x00, /* 6: LOUT2 volume muted */
220		0x07, 0x00, /* 7: ROUT2 volume muted */
221		0x08, 0x00, /* 8: LOUT3 volume muted */
222		0x09, 0x00, /* 9: ROUT3 volume muted */
223		0x0a, 0x00, /* a: DATT speed=0, ignore DZF */
224		0x01, 0x01, /* 1: un-reset, unmute */
225		0xff, 0xff
226	};
227	static const unsigned char inits_ak4358[] = {
228		0x01, 0x02, /* 1: reset and soft-mute */
229		0x00, 0x06, /* 0: mode3(i2s), disable auto-clock detect,
230			     * disable DZF, sharp roll-off, RSTN#=0 */
231		0x02, 0x4e, /* 2: DA's power up, normal speed, RSTN#=0 */
232		/* 0x02, 0x6e,*/ /* quad speed */
233		0x03, 0x01, /* 3: de-emphasis off */
234		0x04, 0x00, /* 4: LOUT1 volume muted */
235		0x05, 0x00, /* 5: ROUT1 volume muted */
236		0x06, 0x00, /* 6: LOUT2 volume muted */
237		0x07, 0x00, /* 7: ROUT2 volume muted */
238		0x08, 0x00, /* 8: LOUT3 volume muted */
239		0x09, 0x00, /* 9: ROUT3 volume muted */
240		0x0b, 0x00, /* b: LOUT4 volume muted */
241		0x0c, 0x00, /* c: ROUT4 volume muted */
242		0x0a, 0x00, /* a: DATT speed=0, ignore DZF */
243		0x01, 0x01, /* 1: un-reset, unmute */
244		0xff, 0xff
245	};
246	static const unsigned char inits_ak4381[] = {
247		0x00, 0x0c, /* 0: mode3(i2s), disable auto-clock detect */
248		0x01, 0x02, /* 1: de-emphasis off, normal speed,
249			     * sharp roll-off, DZF off */
250		// 0x01, 0x12, /* quad speed */
251		0x02, 0x00, /* 2: DZF disabled */
252		0x03, 0x00, /* 3: LATT 0 */
253		0x04, 0x00, /* 4: RATT 0 */
254		0x00, 0x0f, /* 0: power-up, un-reset */
255		0xff, 0xff
256	};
257	static const unsigned char inits_ak4620[] = {
258		0x00, 0x07, /* 0: normal */
259		0x01, 0x00, /* 0: reset */
260		0x01, 0x02, /* 1: RSTAD */
261		0x01, 0x03, /* 1: RSTDA */
262		0x01, 0x0f, /* 1: normal */
263		0x02, 0x60, /* 2: 24bit I2S */
264		0x03, 0x01, /* 3: deemphasis off */
265		0x04, 0x00, /* 4: LIN muted */
266		0x05, 0x00, /* 5: RIN muted */
267		0x06, 0x00, /* 6: LOUT muted */
268		0x07, 0x00, /* 7: ROUT muted */
269		0xff, 0xff
270	};
271
272	int chip;
273	const unsigned char *ptr, *inits;
274	unsigned char reg, data;
275
276	memset(ak->images, 0, sizeof(ak->images));
277	memset(ak->volumes, 0, sizeof(ak->volumes));
278
279	switch (ak->type) {
280	case SND_AK4524:
281		inits = inits_ak4524;
282		ak->num_chips = ak->num_dacs / 2;
283		ak->name = "ak4524";
284		ak->total_regs = 0x08;
285		break;
286	case SND_AK4528:
287		inits = inits_ak4528;
288		ak->num_chips = ak->num_dacs / 2;
289		ak->name = "ak4528";
290		ak->total_regs = 0x06;
291		break;
292	case SND_AK4529:
293		inits = inits_ak4529;
294		ak->num_chips = 1;
295		ak->name = "ak4529";
296		ak->total_regs = 0x0d;
297		break;
298	case SND_AK4355:
299		inits = inits_ak4355;
300		ak->num_chips = 1;
301		ak->name = "ak4355";
302		ak->total_regs = 0x0b;
303		break;
304	case SND_AK4358:
305		inits = inits_ak4358;
306		ak->num_chips = 1;
307		ak->name = "ak4358";
308		ak->total_regs = 0x10;
309		break;
310	case SND_AK4381:
311		inits = inits_ak4381;
312		ak->num_chips = ak->num_dacs / 2;
313		ak->name = "ak4381";
314		ak->total_regs = 0x05;
315		break;
316	case SND_AK5365:
317		ak->num_chips = 1;
318		ak->name = "ak5365";
319		ak->total_regs = 0x08;
320		return;
321	case SND_AK4620:
322		inits = inits_ak4620;
323		ak->num_chips = ak->num_dacs / 2;
324		ak->name = "ak4620";
325		ak->total_regs = 0x08;
326		break;
327	default:
328		snd_BUG();
329		return;
330	}
331
332	for (chip = 0; chip < ak->num_chips; chip++) {
333		ptr = inits;
334		while (*ptr != 0xff) {
335			reg = *ptr++;
336			data = *ptr++;
337			snd_akm4xxx_write(ak, chip, reg, data);
338			udelay(10);
339		}
340	}
341}
342
343EXPORT_SYMBOL(snd_akm4xxx_init);
344
345/*
346 * Mixer callbacks
347 */
348#define AK_IPGA 			(1<<20)	/* including IPGA */
349#define AK_VOL_CVT 			(1<<21)	/* need dB conversion */
350#define AK_NEEDSMSB 			(1<<22)	/* need MSB update bit */
351#define AK_INVERT 			(1<<23)	/* data is inverted */
352#define AK_GET_CHIP(val)		(((val) >> 8) & 0xff)
353#define AK_GET_ADDR(val)		((val) & 0xff)
354#define AK_GET_SHIFT(val)		(((val) >> 16) & 0x0f)
355#define AK_GET_VOL_CVT(val)		(((val) >> 21) & 1)
356#define AK_GET_IPGA(val)		(((val) >> 20) & 1)
357#define AK_GET_NEEDSMSB(val)		(((val) >> 22) & 1)
358#define AK_GET_INVERT(val)		(((val) >> 23) & 1)
359#define AK_GET_MASK(val)		(((val) >> 24) & 0xff)
360#define AK_COMPOSE(chip,addr,shift,mask) \
361	(((chip) << 8) | (addr) | ((shift) << 16) | ((mask) << 24))
362
363static int snd_akm4xxx_volume_info(struct snd_kcontrol *kcontrol,
364				   struct snd_ctl_elem_info *uinfo)
365{
366	unsigned int mask = AK_GET_MASK(kcontrol->private_value);
367
368	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
369	uinfo->count = 1;
370	uinfo->value.integer.min = 0;
371	uinfo->value.integer.max = mask;
372	return 0;
373}
374
375static int snd_akm4xxx_volume_get(struct snd_kcontrol *kcontrol,
376				  struct snd_ctl_elem_value *ucontrol)
377{
378	struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
379	int chip = AK_GET_CHIP(kcontrol->private_value);
380	int addr = AK_GET_ADDR(kcontrol->private_value);
381
382	ucontrol->value.integer.value[0] = snd_akm4xxx_get_vol(ak, chip, addr);
383	return 0;
384}
385
386static int put_ak_reg(struct snd_kcontrol *kcontrol, int addr,
387		      unsigned char nval)
388{
389	struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
390	unsigned int mask = AK_GET_MASK(kcontrol->private_value);
391	int chip = AK_GET_CHIP(kcontrol->private_value);
392
393	if (snd_akm4xxx_get_vol(ak, chip, addr) == nval)
394		return 0;
395
396	snd_akm4xxx_set_vol(ak, chip, addr, nval);
397	if (AK_GET_VOL_CVT(kcontrol->private_value) && nval < 128)
398		nval = vol_cvt_datt[nval];
399	if (AK_GET_IPGA(kcontrol->private_value) && nval >= 128)
400		nval++; /* need to correct + 1 since both 127 and 128 are 0dB */
401	if (AK_GET_INVERT(kcontrol->private_value))
402		nval = mask - nval;
403	if (AK_GET_NEEDSMSB(kcontrol->private_value))
404		nval |= 0x80;
405	/* printk(KERN_DEBUG "DEBUG - AK writing reg: chip %x addr %x,
406	   nval %x\n", chip, addr, nval); */
407	snd_akm4xxx_write(ak, chip, addr, nval);
408	return 1;
409}
410
411static int snd_akm4xxx_volume_put(struct snd_kcontrol *kcontrol,
412				  struct snd_ctl_elem_value *ucontrol)
413{
414	unsigned int mask = AK_GET_MASK(kcontrol->private_value);
415	unsigned int val = ucontrol->value.integer.value[0];
416	if (val > mask)
417		return -EINVAL;
418	return put_ak_reg(kcontrol, AK_GET_ADDR(kcontrol->private_value), val);
419}
420
421static int snd_akm4xxx_stereo_volume_info(struct snd_kcontrol *kcontrol,
422					  struct snd_ctl_elem_info *uinfo)
423{
424	unsigned int mask = AK_GET_MASK(kcontrol->private_value);
425
426	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
427	uinfo->count = 2;
428	uinfo->value.integer.min = 0;
429	uinfo->value.integer.max = mask;
430	return 0;
431}
432
433static int snd_akm4xxx_stereo_volume_get(struct snd_kcontrol *kcontrol,
434					 struct snd_ctl_elem_value *ucontrol)
435{
436	struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
437	int chip = AK_GET_CHIP(kcontrol->private_value);
438	int addr = AK_GET_ADDR(kcontrol->private_value);
439
440	ucontrol->value.integer.value[0] = snd_akm4xxx_get_vol(ak, chip, addr);
441	ucontrol->value.integer.value[1] = snd_akm4xxx_get_vol(ak, chip, addr+1);
442	return 0;
443}
444
445static int snd_akm4xxx_stereo_volume_put(struct snd_kcontrol *kcontrol,
446					 struct snd_ctl_elem_value *ucontrol)
447{
448	int addr = AK_GET_ADDR(kcontrol->private_value);
449	unsigned int mask = AK_GET_MASK(kcontrol->private_value);
450	unsigned int val[2];
451	int change;
452
453	val[0] = ucontrol->value.integer.value[0];
454	val[1] = ucontrol->value.integer.value[1];
455	if (val[0] > mask || val[1] > mask)
456		return -EINVAL;
457	change = put_ak_reg(kcontrol, addr, val[0]);
458	change |= put_ak_reg(kcontrol, addr + 1, val[1]);
459	return change;
460}
461
462static int snd_akm4xxx_deemphasis_info(struct snd_kcontrol *kcontrol,
463				       struct snd_ctl_elem_info *uinfo)
464{
465	static char *texts[4] = {
466		"44.1kHz", "Off", "48kHz", "32kHz",
467	};
468	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
469	uinfo->count = 1;
470	uinfo->value.enumerated.items = 4;
471	if (uinfo->value.enumerated.item >= 4)
472		uinfo->value.enumerated.item = 3;
473	strcpy(uinfo->value.enumerated.name,
474	       texts[uinfo->value.enumerated.item]);
475	return 0;
476}
477
478static int snd_akm4xxx_deemphasis_get(struct snd_kcontrol *kcontrol,
479				      struct snd_ctl_elem_value *ucontrol)
480{
481	struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
482	int chip = AK_GET_CHIP(kcontrol->private_value);
483	int addr = AK_GET_ADDR(kcontrol->private_value);
484	int shift = AK_GET_SHIFT(kcontrol->private_value);
485	ucontrol->value.enumerated.item[0] =
486		(snd_akm4xxx_get(ak, chip, addr) >> shift) & 3;
487	return 0;
488}
489
490static int snd_akm4xxx_deemphasis_put(struct snd_kcontrol *kcontrol,
491				      struct snd_ctl_elem_value *ucontrol)
492{
493	struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
494	int chip = AK_GET_CHIP(kcontrol->private_value);
495	int addr = AK_GET_ADDR(kcontrol->private_value);
496	int shift = AK_GET_SHIFT(kcontrol->private_value);
497	unsigned char nval = ucontrol->value.enumerated.item[0] & 3;
498	int change;
499
500	nval = (nval << shift) |
501		(snd_akm4xxx_get(ak, chip, addr) & ~(3 << shift));
502	change = snd_akm4xxx_get(ak, chip, addr) != nval;
503	if (change)
504		snd_akm4xxx_write(ak, chip, addr, nval);
505	return change;
506}
507
508#define ak4xxx_switch_info	snd_ctl_boolean_mono_info
509
510static int ak4xxx_switch_get(struct snd_kcontrol *kcontrol,
511			     struct snd_ctl_elem_value *ucontrol)
512{
513	struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
514	int chip = AK_GET_CHIP(kcontrol->private_value);
515	int addr = AK_GET_ADDR(kcontrol->private_value);
516	int shift = AK_GET_SHIFT(kcontrol->private_value);
517	int invert = AK_GET_INVERT(kcontrol->private_value);
518	/* we observe the (1<<shift) bit only */
519	unsigned char val = snd_akm4xxx_get(ak, chip, addr) & (1<<shift);
520	if (invert)
521		val = ! val;
522	ucontrol->value.integer.value[0] = (val & (1<<shift)) != 0;
523	return 0;
524}
525
526static int ak4xxx_switch_put(struct snd_kcontrol *kcontrol,
527			     struct snd_ctl_elem_value *ucontrol)
528{
529	struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
530	int chip = AK_GET_CHIP(kcontrol->private_value);
531	int addr = AK_GET_ADDR(kcontrol->private_value);
532	int shift = AK_GET_SHIFT(kcontrol->private_value);
533	int invert = AK_GET_INVERT(kcontrol->private_value);
534	long flag = ucontrol->value.integer.value[0];
535	unsigned char val, oval;
536	int change;
537
538	if (invert)
539		flag = ! flag;
540	oval = snd_akm4xxx_get(ak, chip, addr);
541	if (flag)
542		val = oval | (1<<shift);
543	else
544		val = oval & ~(1<<shift);
545	change = (oval != val);
546	if (change)
547		snd_akm4xxx_write(ak, chip, addr, val);
548	return change;
549}
550
551#define AK5365_NUM_INPUTS 5
552
553static int ak4xxx_capture_num_inputs(struct snd_akm4xxx *ak, int mixer_ch)
554{
555	int num_names;
556	const char **input_names;
557
558	input_names = ak->adc_info[mixer_ch].input_names;
559	num_names = 0;
560	while (num_names < AK5365_NUM_INPUTS && input_names[num_names])
561		++num_names;
562	return num_names;
563}
564
565static int ak4xxx_capture_source_info(struct snd_kcontrol *kcontrol,
566				      struct snd_ctl_elem_info *uinfo)
567{
568	struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
569	int mixer_ch = AK_GET_SHIFT(kcontrol->private_value);
570	const char **input_names;
571	int  num_names, idx;
572
573	num_names = ak4xxx_capture_num_inputs(ak, mixer_ch);
574	if (!num_names)
575		return -EINVAL;
576	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
577	uinfo->count = 1;
578	uinfo->value.enumerated.items = num_names;
579	idx = uinfo->value.enumerated.item;
580	if (idx >= num_names)
581		return -EINVAL;
582	input_names = ak->adc_info[mixer_ch].input_names;
583	strncpy(uinfo->value.enumerated.name, input_names[idx],
584		sizeof(uinfo->value.enumerated.name));
585	return 0;
586}
587
588static int ak4xxx_capture_source_get(struct snd_kcontrol *kcontrol,
589				     struct snd_ctl_elem_value *ucontrol)
590{
591	struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
592	int chip = AK_GET_CHIP(kcontrol->private_value);
593	int addr = AK_GET_ADDR(kcontrol->private_value);
594	int mask = AK_GET_MASK(kcontrol->private_value);
595	unsigned char val;
596
597	val = snd_akm4xxx_get(ak, chip, addr) & mask;
598	ucontrol->value.enumerated.item[0] = val;
599	return 0;
600}
601
602static int ak4xxx_capture_source_put(struct snd_kcontrol *kcontrol,
603				     struct snd_ctl_elem_value *ucontrol)
604{
605	struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol);
606	int mixer_ch = AK_GET_SHIFT(kcontrol->private_value);
607	int chip = AK_GET_CHIP(kcontrol->private_value);
608	int addr = AK_GET_ADDR(kcontrol->private_value);
609	int mask = AK_GET_MASK(kcontrol->private_value);
610	unsigned char oval, val;
611	int num_names = ak4xxx_capture_num_inputs(ak, mixer_ch);
612
613	if (ucontrol->value.enumerated.item[0] >= num_names)
614		return -EINVAL;
615
616	oval = snd_akm4xxx_get(ak, chip, addr);
617	val = oval & ~mask;
618	val |= ucontrol->value.enumerated.item[0] & mask;
619	if (val != oval) {
620		snd_akm4xxx_write(ak, chip, addr, val);
621		return 1;
622	}
623	return 0;
624}
625
626/*
627 * build AK4xxx controls
628 */
629
630static int build_dac_controls(struct snd_akm4xxx *ak)
631{
632	int idx, err, mixer_ch, num_stereo;
633	struct snd_kcontrol_new knew;
634
635	mixer_ch = 0;
636	for (idx = 0; idx < ak->num_dacs; ) {
637		/* mute control for Revolution 7.1 - AK4381 */
638		if (ak->type == SND_AK4381
639				&&  ak->dac_info[mixer_ch].switch_name) {
640			memset(&knew, 0, sizeof(knew));
641			knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
642			knew.count = 1;
643			knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
644			knew.name = ak->dac_info[mixer_ch].switch_name;
645			knew.info = ak4xxx_switch_info;
646			knew.get = ak4xxx_switch_get;
647			knew.put = ak4xxx_switch_put;
648			knew.access = 0;
649			/* register 1, bit 0 (SMUTE): 0 = normal operation,
650			   1 = mute */
651			knew.private_value =
652				AK_COMPOSE(idx/2, 1, 0, 0) | AK_INVERT;
653			err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
654			if (err < 0)
655				return err;
656		}
657		memset(&knew, 0, sizeof(knew));
658		if (! ak->dac_info || ! ak->dac_info[mixer_ch].name) {
659			knew.name = "DAC Volume";
660			knew.index = mixer_ch + ak->idx_offset * 2;
661			num_stereo = 1;
662		} else {
663			knew.name = ak->dac_info[mixer_ch].name;
664			num_stereo = ak->dac_info[mixer_ch].num_channels;
665		}
666		knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
667		knew.count = 1;
668		knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
669			SNDRV_CTL_ELEM_ACCESS_TLV_READ;
670		if (num_stereo == 2) {
671			knew.info = snd_akm4xxx_stereo_volume_info;
672			knew.get = snd_akm4xxx_stereo_volume_get;
673			knew.put = snd_akm4xxx_stereo_volume_put;
674		} else {
675			knew.info = snd_akm4xxx_volume_info;
676			knew.get = snd_akm4xxx_volume_get;
677			knew.put = snd_akm4xxx_volume_put;
678		}
679		switch (ak->type) {
680		case SND_AK4524:
681			/* register 6 & 7 */
682			knew.private_value =
683				AK_COMPOSE(idx/2, (idx%2) + 6, 0, 127) |
684				AK_VOL_CVT;
685			knew.tlv.p = db_scale_vol_datt;
686			break;
687		case SND_AK4528:
688			/* register 4 & 5 */
689			knew.private_value =
690				AK_COMPOSE(idx/2, (idx%2) + 4, 0, 127) |
691				AK_VOL_CVT;
692			knew.tlv.p = db_scale_vol_datt;
693			break;
694		case SND_AK4529: {
695			/* registers 2-7 and b,c */
696			int val = idx < 6 ? idx + 2 : (idx - 6) + 0xb;
697			knew.private_value =
698				AK_COMPOSE(0, val, 0, 255) | AK_INVERT;
699			knew.tlv.p = db_scale_8bit;
700			break;
701		}
702		case SND_AK4355:
703			/* register 4-9, chip #0 only */
704			knew.private_value = AK_COMPOSE(0, idx + 4, 0, 255);
705			knew.tlv.p = db_scale_8bit;
706			break;
707		case SND_AK4358: {
708			/* register 4-9 and 11-12, chip #0 only */
709			int  addr = idx < 6 ? idx + 4 : idx + 5;
710			knew.private_value =
711				AK_COMPOSE(0, addr, 0, 127) | AK_NEEDSMSB;
712			knew.tlv.p = db_scale_7bit;
713			break;
714		}
715		case SND_AK4381:
716			/* register 3 & 4 */
717			knew.private_value =
718				AK_COMPOSE(idx/2, (idx%2) + 3, 0, 255);
719			knew.tlv.p = db_scale_linear;
720			break;
721		case SND_AK4620:
722			/* register 6 & 7 */
723			knew.private_value =
724				AK_COMPOSE(idx/2, (idx%2) + 6, 0, 255);
725			knew.tlv.p = db_scale_linear;
726			break;
727		default:
728			return -EINVAL;
729		}
730
731		err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
732		if (err < 0)
733			return err;
734
735		idx += num_stereo;
736		mixer_ch++;
737	}
738	return 0;
739}
740
741static int build_adc_controls(struct snd_akm4xxx *ak)
742{
743	int idx, err, mixer_ch, num_stereo, max_steps;
744	struct snd_kcontrol_new knew;
745
746	mixer_ch = 0;
747	if (ak->type == SND_AK4528)
748		return 0;	/* no controls */
749	for (idx = 0; idx < ak->num_adcs;) {
750		memset(&knew, 0, sizeof(knew));
751		if (! ak->adc_info || ! ak->adc_info[mixer_ch].name) {
752			knew.name = "ADC Volume";
753			knew.index = mixer_ch + ak->idx_offset * 2;
754			num_stereo = 1;
755		} else {
756			knew.name = ak->adc_info[mixer_ch].name;
757			num_stereo = ak->adc_info[mixer_ch].num_channels;
758		}
759		knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
760		knew.count = 1;
761		knew.access = SNDRV_CTL_ELEM_ACCESS_READWRITE |
762			SNDRV_CTL_ELEM_ACCESS_TLV_READ;
763		if (num_stereo == 2) {
764			knew.info = snd_akm4xxx_stereo_volume_info;
765			knew.get = snd_akm4xxx_stereo_volume_get;
766			knew.put = snd_akm4xxx_stereo_volume_put;
767		} else {
768			knew.info = snd_akm4xxx_volume_info;
769			knew.get = snd_akm4xxx_volume_get;
770			knew.put = snd_akm4xxx_volume_put;
771		}
772		/* register 4 & 5 */
773		if (ak->type == SND_AK5365)
774			max_steps = 152;
775		else
776			max_steps = 164;
777		knew.private_value =
778			AK_COMPOSE(idx/2, (idx%2) + 4, 0, max_steps) |
779			AK_VOL_CVT | AK_IPGA;
780		knew.tlv.p = db_scale_vol_datt;
781		err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
782		if (err < 0)
783			return err;
784
785		if (ak->type == SND_AK5365 && (idx % 2) == 0) {
786			if (! ak->adc_info ||
787			    ! ak->adc_info[mixer_ch].switch_name) {
788				knew.name = "Capture Switch";
789				knew.index = mixer_ch + ak->idx_offset * 2;
790			} else
791				knew.name = ak->adc_info[mixer_ch].switch_name;
792			knew.info = ak4xxx_switch_info;
793			knew.get = ak4xxx_switch_get;
794			knew.put = ak4xxx_switch_put;
795			knew.access = 0;
796			/* register 2, bit 0 (SMUTE): 0 = normal operation,
797			   1 = mute */
798			knew.private_value =
799				AK_COMPOSE(idx/2, 2, 0, 0) | AK_INVERT;
800			err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
801			if (err < 0)
802				return err;
803
804			memset(&knew, 0, sizeof(knew));
805			knew.name = ak->adc_info[mixer_ch].selector_name;
806			if (!knew.name) {
807				knew.name = "Capture Channel";
808				knew.index = mixer_ch + ak->idx_offset * 2;
809			}
810
811			knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
812			knew.info = ak4xxx_capture_source_info;
813			knew.get = ak4xxx_capture_source_get;
814			knew.put = ak4xxx_capture_source_put;
815			knew.access = 0;
816			/* input selector control: reg. 1, bits 0-2.
817			 * mis-use 'shift' to pass mixer_ch */
818			knew.private_value
819				= AK_COMPOSE(idx/2, 1, mixer_ch, 0x07);
820			err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
821			if (err < 0)
822				return err;
823		}
824
825		idx += num_stereo;
826		mixer_ch++;
827	}
828	return 0;
829}
830
831static int build_deemphasis(struct snd_akm4xxx *ak, int num_emphs)
832{
833	int idx, err;
834	struct snd_kcontrol_new knew;
835
836	for (idx = 0; idx < num_emphs; idx++) {
837		memset(&knew, 0, sizeof(knew));
838		knew.name = "Deemphasis";
839		knew.index = idx + ak->idx_offset;
840		knew.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
841		knew.count = 1;
842		knew.info = snd_akm4xxx_deemphasis_info;
843		knew.get = snd_akm4xxx_deemphasis_get;
844		knew.put = snd_akm4xxx_deemphasis_put;
845		switch (ak->type) {
846		case SND_AK4524:
847		case SND_AK4528:
848		case SND_AK4620:
849			/* register 3 */
850			knew.private_value = AK_COMPOSE(idx, 3, 0, 0);
851			break;
852		case SND_AK4529: {
853			int shift = idx == 3 ? 6 : (2 - idx) * 2;
854			/* register 8 with shift */
855			knew.private_value = AK_COMPOSE(0, 8, shift, 0);
856			break;
857		}
858		case SND_AK4355:
859		case SND_AK4358:
860			knew.private_value = AK_COMPOSE(idx, 3, 0, 0);
861			break;
862		case SND_AK4381:
863			knew.private_value = AK_COMPOSE(idx, 1, 1, 0);
864			break;
865		default:
866			return -EINVAL;
867		}
868		err = snd_ctl_add(ak->card, snd_ctl_new1(&knew, ak));
869		if (err < 0)
870			return err;
871	}
872	return 0;
873}
874
875#ifdef CONFIG_PROC_FS
876static void proc_regs_read(struct snd_info_entry *entry,
877		struct snd_info_buffer *buffer)
878{
879	struct snd_akm4xxx *ak = (struct snd_akm4xxx *)entry->private_data;
880	int reg, val, chip;
881	for (chip = 0; chip < ak->num_chips; chip++) {
882		for (reg = 0; reg < ak->total_regs; reg++) {
883			val =  snd_akm4xxx_get(ak, chip, reg);
884			snd_iprintf(buffer, "chip %d: 0x%02x = 0x%02x\n", chip,
885					reg, val);
886		}
887	}
888}
889
890static int proc_init(struct snd_akm4xxx *ak)
891{
892	struct snd_info_entry *entry;
893	int err;
894	err = snd_card_proc_new(ak->card, ak->name, &entry);
895	if (err < 0)
896		return err;
897	snd_info_set_text_ops(entry, ak, proc_regs_read);
898	return 0;
899}
900#else /* !CONFIG_PROC_FS */
901static int proc_init(struct snd_akm4xxx *ak) { return 0; }
902#endif
903
904int snd_akm4xxx_build_controls(struct snd_akm4xxx *ak)
905{
906	int err, num_emphs;
907
908	err = build_dac_controls(ak);
909	if (err < 0)
910		return err;
911
912	err = build_adc_controls(ak);
913	if (err < 0)
914		return err;
915	if (ak->type == SND_AK4355 || ak->type == SND_AK4358)
916		num_emphs = 1;
917	else if (ak->type == SND_AK4620)
918		num_emphs = 0;
919	else
920		num_emphs = ak->num_dacs / 2;
921	err = build_deemphasis(ak, num_emphs);
922	if (err < 0)
923		return err;
924	err = proc_init(ak);
925	if (err < 0)
926		return err;
927
928	return 0;
929}
930EXPORT_SYMBOL(snd_akm4xxx_build_controls);
931
932static int __init alsa_akm4xxx_module_init(void)
933{
934	return 0;
935}
936
937static void __exit alsa_akm4xxx_module_exit(void)
938{
939}
940
941module_init(alsa_akm4xxx_module_init)
942module_exit(alsa_akm4xxx_module_exit)
943