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