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