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