2 * ALSA driver for AK4524 / AK4528 / AK4529 / AK4355 / AK4381
5 * Copyright (c) 2000-2003 Jaroslav Kysela <perex@suse.cz>,
6 * Takashi Iwai <tiwai@suse.de>
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.
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.
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
24 #include <sound/driver.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/ak4xxx-adda.h>
33 MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Takashi Iwai <tiwai@suse.de>");
34 MODULE_DESCRIPTION("Routines for control of AK452x / AK43xx AD/DA converters");
35 MODULE_LICENSE("GPL");
37 void snd_akm4xxx_write(akm4xxx_t
*ak
, int chip
, unsigned char reg
, unsigned char val
)
39 ak
->ops
.lock(ak
, chip
);
40 ak
->ops
.write(ak
, chip
, reg
, val
);
43 if (ak
->type
== SND_AK4524
|| ak
->type
== SND_AK4528
) {
44 if ((reg
!= 0x04 && reg
!= 0x05) || (val
& 0x80) == 0)
45 snd_akm4xxx_set(ak
, chip
, reg
, val
);
47 snd_akm4xxx_set_ipga(ak
, chip
, reg
, val
);
50 snd_akm4xxx_set(ak
, chip
, reg
, val
);
52 ak
->ops
.unlock(ak
, chip
);
56 * reset the AKM codecs
57 * @state: 1 = reset codec, 0 = restore the registers
59 * assert the reset operation and restores the register values to the chips.
61 void snd_akm4xxx_reset(akm4xxx_t
*ak
, int state
)
69 for (chip
= 0; chip
< ak
->num_dacs
/2; chip
++) {
70 snd_akm4xxx_write(ak
, chip
, 0x01, state
? 0x00 : 0x03);
74 for (reg
= 0x04; reg
< (ak
->type
== SND_AK4528
? 0x06 : 0x08); reg
++)
75 snd_akm4xxx_write(ak
, chip
, reg
, snd_akm4xxx_get(ak
, chip
, reg
));
76 if (ak
->type
== SND_AK4528
)
79 for (reg
= 0x04; reg
< 0x06; reg
++)
80 snd_akm4xxx_write(ak
, chip
, reg
, snd_akm4xxx_get_ipga(ak
, chip
, reg
));
84 /* FIXME: needed for ak4529? */
88 snd_akm4xxx_write(ak
, 0, 0x01, 0x02); /* reset and soft-mute */
91 for (reg
= 0x00; reg
< 0x0b; reg
++)
93 snd_akm4xxx_write(ak
, 0, reg
, snd_akm4xxx_get(ak
, 0, reg
));
94 snd_akm4xxx_write(ak
, 0, 0x01, 0x01); /* un-reset, unmute */
97 for (chip
= 0; chip
< ak
->num_dacs
/2; chip
++) {
98 snd_akm4xxx_write(ak
, chip
, 0x00, state
? 0x0c : 0x0f);
101 for (reg
= 0x01; reg
< 0x05; reg
++)
102 snd_akm4xxx_write(ak
, chip
, reg
, snd_akm4xxx_get(ak
, chip
, reg
));
109 * initialize all the ak4xxx chips
111 void snd_akm4xxx_init(akm4xxx_t
*ak
)
113 static unsigned char inits_ak4524
[] = {
114 0x00, 0x07, /* 0: all power up */
115 0x01, 0x00, /* 1: ADC/DAC reset */
116 0x02, 0x60, /* 2: 24bit I2S */
117 0x03, 0x19, /* 3: deemphasis off */
118 0x01, 0x03, /* 1: ADC/DAC enable */
119 0x04, 0x00, /* 4: ADC left muted */
120 0x05, 0x00, /* 5: ADC right muted */
121 0x04, 0x80, /* 4: ADC IPGA gain 0dB */
122 0x05, 0x80, /* 5: ADC IPGA gain 0dB */
123 0x06, 0x00, /* 6: DAC left muted */
124 0x07, 0x00, /* 7: DAC right muted */
127 static unsigned char inits_ak4528
[] = {
128 0x00, 0x07, /* 0: all power up */
129 0x01, 0x00, /* 1: ADC/DAC reset */
130 0x02, 0x60, /* 2: 24bit I2S */
131 0x03, 0x0d, /* 3: deemphasis off, turn LR highpass filters on */
132 0x01, 0x03, /* 1: ADC/DAC enable */
133 0x04, 0x00, /* 4: ADC left muted */
134 0x05, 0x00, /* 5: ADC right muted */
137 static unsigned char inits_ak4529
[] = {
138 0x09, 0x01, /* 9: ATS=0, RSTN=1 */
139 0x0a, 0x3f, /* A: all power up, no zero/overflow detection */
140 0x00, 0x0c, /* 0: TDM=0, 24bit I2S, SMUTE=0 */
141 0x01, 0x00, /* 1: ACKS=0, ADC, loop off */
142 0x02, 0xff, /* 2: LOUT1 muted */
143 0x03, 0xff, /* 3: ROUT1 muted */
144 0x04, 0xff, /* 4: LOUT2 muted */
145 0x05, 0xff, /* 5: ROUT2 muted */
146 0x06, 0xff, /* 6: LOUT3 muted */
147 0x07, 0xff, /* 7: ROUT3 muted */
148 0x0b, 0xff, /* B: LOUT4 muted */
149 0x0c, 0xff, /* C: ROUT4 muted */
150 0x08, 0x55, /* 8: deemphasis all off */
153 static unsigned char inits_ak4355
[] = {
154 0x01, 0x02, /* 1: reset and soft-mute */
155 0x00, 0x06, /* 0: mode3(i2s), disable auto-clock detect, disable DZF, sharp roll-off, RSTN#=0 */
156 0x02, 0x0e, /* 2: DA's power up, normal speed, RSTN#=0 */
157 // 0x02, 0x2e, /* quad speed */
158 0x03, 0x01, /* 3: de-emphasis off */
159 0x04, 0x00, /* 4: LOUT1 volume muted */
160 0x05, 0x00, /* 5: ROUT1 volume muted */
161 0x06, 0x00, /* 6: LOUT2 volume muted */
162 0x07, 0x00, /* 7: ROUT2 volume muted */
163 0x08, 0x00, /* 8: LOUT3 volume muted */
164 0x09, 0x00, /* 9: ROUT3 volume muted */
165 0x0a, 0x00, /* a: DATT speed=0, ignore DZF */
166 0x01, 0x01, /* 1: un-reset, unmute */
169 static unsigned char inits_ak4381
[] = {
170 0x00, 0x0c, /* 0: mode3(i2s), disable auto-clock detect */
171 0x01, 0x02, /* 1: de-emphasis off, normal speed, sharp roll-off, DZF off */
172 // 0x01, 0x12, /* quad speed */
173 0x02, 0x00, /* 2: DZF disabled */
174 0x03, 0x00, /* 3: LATT 0 */
175 0x04, 0x00, /* 4: RATT 0 */
176 0x00, 0x0f, /* 0: power-up, un-reset */
181 unsigned char *ptr
, reg
, data
, *inits
;
185 inits
= inits_ak4524
;
186 num_chips
= ak
->num_dacs
/ 2;
189 inits
= inits_ak4528
;
190 num_chips
= ak
->num_dacs
/ 2;
193 inits
= inits_ak4529
;
197 inits
= inits_ak4355
;
201 inits
= inits_ak4381
;
202 num_chips
= ak
->num_dacs
/ 2;
209 for (chip
= 0; chip
< num_chips
; chip
++) {
211 while (*ptr
!= 0xff) {
214 snd_akm4xxx_write(ak
, chip
, reg
, data
);
219 #define AK_GET_CHIP(val) (((val) >> 8) & 0xff)
220 #define AK_GET_ADDR(val) ((val) & 0xff)
221 #define AK_GET_SHIFT(val) (((val) >> 16) & 0x7f)
222 #define AK_GET_INVERT(val) (((val) >> 23) & 1)
223 #define AK_GET_MASK(val) (((val) >> 24) & 0xff)
224 #define AK_COMPOSE(chip,addr,shift,mask) (((chip) << 8) | (addr) | ((shift) << 16) | ((mask) << 24))
225 #define AK_INVERT (1<<23)
227 static int snd_akm4xxx_volume_info(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
* uinfo
)
229 unsigned int mask
= AK_GET_MASK(kcontrol
->private_value
);
231 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
233 uinfo
->value
.integer
.min
= 0;
234 uinfo
->value
.integer
.max
= mask
;
238 static int snd_akm4xxx_volume_get(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
240 akm4xxx_t
*ak
= snd_kcontrol_chip(kcontrol
);
241 int chip
= AK_GET_CHIP(kcontrol
->private_value
);
242 int addr
= AK_GET_ADDR(kcontrol
->private_value
);
243 int invert
= AK_GET_INVERT(kcontrol
->private_value
);
244 unsigned int mask
= AK_GET_MASK(kcontrol
->private_value
);
245 unsigned char val
= snd_akm4xxx_get(ak
, chip
, addr
);
247 ucontrol
->value
.integer
.value
[0] = invert
? mask
- val
: val
;
251 static int snd_akm4xxx_volume_put(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
253 akm4xxx_t
*ak
= snd_kcontrol_chip(kcontrol
);
254 int chip
= AK_GET_CHIP(kcontrol
->private_value
);
255 int addr
= AK_GET_ADDR(kcontrol
->private_value
);
256 int invert
= AK_GET_INVERT(kcontrol
->private_value
);
257 unsigned int mask
= AK_GET_MASK(kcontrol
->private_value
);
258 unsigned char nval
= ucontrol
->value
.integer
.value
[0] % (mask
+1);
263 change
= snd_akm4xxx_get(ak
, chip
, addr
) != nval
;
265 snd_akm4xxx_write(ak
, chip
, addr
, nval
);
269 static int snd_akm4xxx_ipga_gain_info(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
* uinfo
)
271 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
273 uinfo
->value
.integer
.min
= 0;
274 uinfo
->value
.integer
.max
= 36;
278 static int snd_akm4xxx_ipga_gain_get(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
280 akm4xxx_t
*ak
= snd_kcontrol_chip(kcontrol
);
281 int chip
= AK_GET_CHIP(kcontrol
->private_value
);
282 int addr
= AK_GET_ADDR(kcontrol
->private_value
);
283 ucontrol
->value
.integer
.value
[0] = snd_akm4xxx_get_ipga(ak
, chip
, addr
) & 0x7f;
287 static int snd_akm4xxx_ipga_gain_put(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
289 akm4xxx_t
*ak
= snd_kcontrol_chip(kcontrol
);
290 int chip
= AK_GET_CHIP(kcontrol
->private_value
);
291 int addr
= AK_GET_ADDR(kcontrol
->private_value
);
292 unsigned char nval
= (ucontrol
->value
.integer
.value
[0] % 37) | 0x80;
293 int change
= snd_akm4xxx_get_ipga(ak
, chip
, addr
) != nval
;
295 snd_akm4xxx_write(ak
, chip
, addr
, nval
);
299 static int snd_akm4xxx_deemphasis_info(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_info_t
*uinfo
)
301 static char *texts
[4] = {
302 "44.1kHz", "Off", "48kHz", "32kHz",
304 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
306 uinfo
->value
.enumerated
.items
= 4;
307 if (uinfo
->value
.enumerated
.item
>= 4)
308 uinfo
->value
.enumerated
.item
= 3;
309 strcpy(uinfo
->value
.enumerated
.name
, texts
[uinfo
->value
.enumerated
.item
]);
313 static int snd_akm4xxx_deemphasis_get(snd_kcontrol_t
* kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
315 akm4xxx_t
*ak
= snd_kcontrol_chip(kcontrol
);
316 int chip
= AK_GET_CHIP(kcontrol
->private_value
);
317 int addr
= AK_GET_ADDR(kcontrol
->private_value
);
318 int shift
= AK_GET_SHIFT(kcontrol
->private_value
);
319 ucontrol
->value
.enumerated
.item
[0] = (snd_akm4xxx_get(ak
, chip
, addr
) >> shift
) & 3;
323 static int snd_akm4xxx_deemphasis_put(snd_kcontrol_t
*kcontrol
, snd_ctl_elem_value_t
*ucontrol
)
325 akm4xxx_t
*ak
= snd_kcontrol_chip(kcontrol
);
326 int chip
= AK_GET_CHIP(kcontrol
->private_value
);
327 int addr
= AK_GET_ADDR(kcontrol
->private_value
);
328 int shift
= AK_GET_SHIFT(kcontrol
->private_value
);
329 unsigned char nval
= ucontrol
->value
.enumerated
.item
[0] & 3;
332 nval
= (nval
<< shift
) | (snd_akm4xxx_get(ak
, chip
, addr
) & ~(3 << shift
));
333 change
= snd_akm4xxx_get(ak
, chip
, addr
) != nval
;
335 snd_akm4xxx_write(ak
, chip
, addr
, nval
);
340 * build AK4xxx controls
343 int snd_akm4xxx_build_controls(akm4xxx_t
*ak
)
345 unsigned int idx
, num_emphs
;
348 for (idx
= 0; idx
< ak
->num_dacs
; ++idx
) {
350 memset(&ctl
, 0, sizeof(ctl
));
351 strcpy(ctl
.id
.name
, "DAC Volume");
352 ctl
.id
.index
= idx
+ ak
->idx_offset
* 2;
353 ctl
.id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
355 ctl
.info
= snd_akm4xxx_volume_info
;
356 ctl
.get
= snd_akm4xxx_volume_get
;
357 ctl
.put
= snd_akm4xxx_volume_put
;
360 ctl
.private_value
= AK_COMPOSE(idx
/2, (idx
%2) + 6, 0, 127); /* register 6 & 7 */
363 ctl
.private_value
= AK_COMPOSE(idx
/2, (idx
%2) + 4, 0, 127); /* register 4 & 5 */
366 int val
= idx
< 6 ? idx
+ 2 : (idx
- 6) + 0xb; /* registers 2-7 and b,c */
367 ctl
.private_value
= AK_COMPOSE(0, val
, 0, 255) | AK_INVERT
;
371 ctl
.private_value
= AK_COMPOSE(0, idx
+ 4, 0, 255); /* register 4-9, chip #0 only */
374 ctl
.private_value
= AK_COMPOSE(idx
/2, (idx
%2) + 3, 0, 255); /* register 3 & 4 */
379 ctl
.private_data
= ak
;
380 if ((err
= snd_ctl_add(ak
->card
, snd_ctl_new(&ctl
, SNDRV_CTL_ELEM_ACCESS_READ
|SNDRV_CTL_ELEM_ACCESS_WRITE
))) < 0)
383 for (idx
= 0; idx
< ak
->num_adcs
&& ak
->type
== SND_AK4524
; ++idx
) {
385 memset(&ctl
, 0, sizeof(ctl
));
386 strcpy(ctl
.id
.name
, "ADC Volume");
387 ctl
.id
.index
= idx
+ ak
->idx_offset
* 2;
388 ctl
.id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
390 ctl
.info
= snd_akm4xxx_volume_info
;
391 ctl
.get
= snd_akm4xxx_volume_get
;
392 ctl
.put
= snd_akm4xxx_volume_put
;
393 ctl
.private_value
= AK_COMPOSE(idx
/2, (idx
%2) + 4, 0, 127); /* register 4 & 5 */
394 ctl
.private_data
= ak
;
395 if ((err
= snd_ctl_add(ak
->card
, snd_ctl_new(&ctl
, SNDRV_CTL_ELEM_ACCESS_READ
|SNDRV_CTL_ELEM_ACCESS_WRITE
))) < 0)
397 memset(&ctl
, 0, sizeof(ctl
));
398 strcpy(ctl
.id
.name
, "IPGA Analog Capture Volume");
399 ctl
.id
.index
= idx
+ ak
->idx_offset
* 2;
400 ctl
.id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
402 ctl
.info
= snd_akm4xxx_ipga_gain_info
;
403 ctl
.get
= snd_akm4xxx_ipga_gain_get
;
404 ctl
.put
= snd_akm4xxx_ipga_gain_put
;
405 ctl
.private_value
= AK_COMPOSE(idx
/2, (idx
%2) + 4, 0, 0); /* register 4 & 5 */
406 ctl
.private_data
= ak
;
407 if ((err
= snd_ctl_add(ak
->card
, snd_ctl_new(&ctl
, SNDRV_CTL_ELEM_ACCESS_READ
|SNDRV_CTL_ELEM_ACCESS_WRITE
))) < 0)
410 if (ak
->type
== SND_AK4355
)
413 num_emphs
= ak
->num_dacs
/ 2;
414 for (idx
= 0; idx
< num_emphs
; idx
++) {
416 memset(&ctl
, 0, sizeof(ctl
));
417 strcpy(ctl
.id
.name
, "Deemphasis");
418 ctl
.id
.index
= idx
+ ak
->idx_offset
;
419 ctl
.id
.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
421 ctl
.info
= snd_akm4xxx_deemphasis_info
;
422 ctl
.get
= snd_akm4xxx_deemphasis_get
;
423 ctl
.put
= snd_akm4xxx_deemphasis_put
;
427 ctl
.private_value
= AK_COMPOSE(idx
, 3, 0, 0); /* register 3 */
430 int shift
= idx
== 3 ? 6 : (2 - idx
) * 2;
431 ctl
.private_value
= AK_COMPOSE(0, 8, shift
, 0); /* register 8 with shift */
435 ctl
.private_value
= AK_COMPOSE(idx
, 3, 0, 0);
438 ctl
.private_value
= AK_COMPOSE(idx
, 1, 1, 0);
441 ctl
.private_data
= ak
;
442 if ((err
= snd_ctl_add(ak
->card
, snd_ctl_new(&ctl
, SNDRV_CTL_ELEM_ACCESS_READ
|SNDRV_CTL_ELEM_ACCESS_WRITE
))) < 0)
448 static int __init
alsa_akm4xxx_module_init(void)
453 static void __exit
alsa_akm4xxx_module_exit(void)
457 module_init(alsa_akm4xxx_module_init
)
458 module_exit(alsa_akm4xxx_module_exit
)
460 EXPORT_SYMBOL(snd_akm4xxx_write
);
461 EXPORT_SYMBOL(snd_akm4xxx_reset
);
462 EXPORT_SYMBOL(snd_akm4xxx_init
);
463 EXPORT_SYMBOL(snd_akm4xxx_build_controls
);