Commit | Line | Data |
---|---|---|
1da177e4 LT |
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/ak4xxx-adda.h> | |
32 | ||
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"); | |
36 | ||
cb9d24e4 TI |
37 | void snd_akm4xxx_write(struct snd_akm4xxx *ak, int chip, unsigned char reg, |
38 | unsigned char val) | |
1da177e4 LT |
39 | { |
40 | ak->ops.lock(ak, chip); | |
41 | ak->ops.write(ak, chip, reg, val); | |
42 | ||
43 | /* save the data */ | |
44 | if (ak->type == SND_AK4524 || ak->type == SND_AK4528) { | |
45 | if ((reg != 0x04 && reg != 0x05) || (val & 0x80) == 0) | |
46 | snd_akm4xxx_set(ak, chip, reg, val); | |
47 | else | |
48 | snd_akm4xxx_set_ipga(ak, chip, reg, val); | |
49 | } else { | |
50 | /* AK4529, or else */ | |
51 | snd_akm4xxx_set(ak, chip, reg, val); | |
52 | } | |
53 | ak->ops.unlock(ak, chip); | |
54 | } | |
55 | ||
cb9d24e4 TI |
56 | EXPORT_SYMBOL(snd_akm4xxx_write); |
57 | ||
58 | /* reset procedure for AK4524 and AK4528 */ | |
59 | static void ak4524_reset(struct snd_akm4xxx *ak, int state) | |
60 | { | |
61 | unsigned int chip; | |
62 | unsigned char reg, maxreg; | |
63 | ||
64 | if (ak->type == SND_AK4528) | |
65 | maxreg = 0x06; | |
66 | else | |
67 | maxreg = 0x08; | |
68 | for (chip = 0; chip < ak->num_dacs/2; chip++) { | |
69 | snd_akm4xxx_write(ak, chip, 0x01, state ? 0x00 : 0x03); | |
70 | if (state) | |
71 | continue; | |
72 | /* DAC volumes */ | |
73 | for (reg = 0x04; reg < maxreg; reg++) | |
74 | snd_akm4xxx_write(ak, chip, reg, | |
75 | snd_akm4xxx_get(ak, chip, reg)); | |
76 | if (ak->type == SND_AK4528) | |
77 | continue; | |
78 | /* IPGA */ | |
79 | for (reg = 0x04; reg < 0x06; reg++) | |
80 | snd_akm4xxx_write(ak, chip, reg, | |
81 | snd_akm4xxx_get_ipga(ak, chip, reg)); | |
82 | } | |
83 | } | |
84 | ||
85 | /* reset procedure for AK4355 and AK4358 */ | |
86 | static void ak4355_reset(struct snd_akm4xxx *ak, int state) | |
87 | { | |
88 | unsigned char reg; | |
89 | ||
90 | if (state) { | |
91 | snd_akm4xxx_write(ak, 0, 0x01, 0x02); /* reset and soft-mute */ | |
92 | return; | |
93 | } | |
94 | for (reg = 0x00; reg < 0x0b; reg++) | |
95 | if (reg != 0x01) | |
96 | snd_akm4xxx_write(ak, 0, reg, | |
97 | snd_akm4xxx_get(ak, 0, reg)); | |
98 | snd_akm4xxx_write(ak, 0, 0x01, 0x01); /* un-reset, unmute */ | |
99 | } | |
100 | ||
101 | /* reset procedure for AK4381 */ | |
102 | static void ak4381_reset(struct snd_akm4xxx *ak, int state) | |
103 | { | |
104 | unsigned int chip; | |
105 | unsigned char reg; | |
106 | ||
107 | for (chip = 0; chip < ak->num_dacs/2; chip++) { | |
108 | snd_akm4xxx_write(ak, chip, 0x00, state ? 0x0c : 0x0f); | |
109 | if (state) | |
110 | continue; | |
111 | for (reg = 0x01; reg < 0x05; reg++) | |
112 | snd_akm4xxx_write(ak, chip, reg, | |
113 | snd_akm4xxx_get(ak, chip, reg)); | |
114 | } | |
115 | } | |
116 | ||
1da177e4 LT |
117 | /* |
118 | * reset the AKM codecs | |
119 | * @state: 1 = reset codec, 0 = restore the registers | |
120 | * | |
121 | * assert the reset operation and restores the register values to the chips. | |
122 | */ | |
97f02e05 | 123 | void snd_akm4xxx_reset(struct snd_akm4xxx *ak, int state) |
1da177e4 | 124 | { |
1da177e4 LT |
125 | switch (ak->type) { |
126 | case SND_AK4524: | |
127 | case SND_AK4528: | |
cb9d24e4 | 128 | ak4524_reset(ak, state); |
1da177e4 LT |
129 | break; |
130 | case SND_AK4529: | |
131 | /* FIXME: needed for ak4529? */ | |
132 | break; | |
133 | case SND_AK4355: | |
134 | case SND_AK4358: | |
cb9d24e4 | 135 | ak4355_reset(ak, state); |
1da177e4 LT |
136 | break; |
137 | case SND_AK4381: | |
cb9d24e4 | 138 | ak4381_reset(ak, state); |
1da177e4 LT |
139 | break; |
140 | } | |
141 | } | |
142 | ||
cb9d24e4 TI |
143 | EXPORT_SYMBOL(snd_akm4xxx_reset); |
144 | ||
1da177e4 LT |
145 | /* |
146 | * initialize all the ak4xxx chips | |
147 | */ | |
97f02e05 | 148 | void snd_akm4xxx_init(struct snd_akm4xxx *ak) |
1da177e4 LT |
149 | { |
150 | static unsigned char inits_ak4524[] = { | |
151 | 0x00, 0x07, /* 0: all power up */ | |
152 | 0x01, 0x00, /* 1: ADC/DAC reset */ | |
153 | 0x02, 0x60, /* 2: 24bit I2S */ | |
154 | 0x03, 0x19, /* 3: deemphasis off */ | |
155 | 0x01, 0x03, /* 1: ADC/DAC enable */ | |
156 | 0x04, 0x00, /* 4: ADC left muted */ | |
157 | 0x05, 0x00, /* 5: ADC right muted */ | |
158 | 0x04, 0x80, /* 4: ADC IPGA gain 0dB */ | |
159 | 0x05, 0x80, /* 5: ADC IPGA gain 0dB */ | |
160 | 0x06, 0x00, /* 6: DAC left muted */ | |
161 | 0x07, 0x00, /* 7: DAC right muted */ | |
162 | 0xff, 0xff | |
163 | }; | |
164 | static unsigned char inits_ak4528[] = { | |
165 | 0x00, 0x07, /* 0: all power up */ | |
166 | 0x01, 0x00, /* 1: ADC/DAC reset */ | |
167 | 0x02, 0x60, /* 2: 24bit I2S */ | |
168 | 0x03, 0x0d, /* 3: deemphasis off, turn LR highpass filters on */ | |
169 | 0x01, 0x03, /* 1: ADC/DAC enable */ | |
170 | 0x04, 0x00, /* 4: ADC left muted */ | |
171 | 0x05, 0x00, /* 5: ADC right muted */ | |
172 | 0xff, 0xff | |
173 | }; | |
174 | static unsigned char inits_ak4529[] = { | |
175 | 0x09, 0x01, /* 9: ATS=0, RSTN=1 */ | |
176 | 0x0a, 0x3f, /* A: all power up, no zero/overflow detection */ | |
177 | 0x00, 0x0c, /* 0: TDM=0, 24bit I2S, SMUTE=0 */ | |
178 | 0x01, 0x00, /* 1: ACKS=0, ADC, loop off */ | |
179 | 0x02, 0xff, /* 2: LOUT1 muted */ | |
180 | 0x03, 0xff, /* 3: ROUT1 muted */ | |
181 | 0x04, 0xff, /* 4: LOUT2 muted */ | |
182 | 0x05, 0xff, /* 5: ROUT2 muted */ | |
183 | 0x06, 0xff, /* 6: LOUT3 muted */ | |
184 | 0x07, 0xff, /* 7: ROUT3 muted */ | |
185 | 0x0b, 0xff, /* B: LOUT4 muted */ | |
186 | 0x0c, 0xff, /* C: ROUT4 muted */ | |
187 | 0x08, 0x55, /* 8: deemphasis all off */ | |
188 | 0xff, 0xff | |
189 | }; | |
190 | static unsigned char inits_ak4355[] = { | |
191 | 0x01, 0x02, /* 1: reset and soft-mute */ | |
cb9d24e4 TI |
192 | 0x00, 0x06, /* 0: mode3(i2s), disable auto-clock detect, |
193 | * disable DZF, sharp roll-off, RSTN#=0 */ | |
1da177e4 LT |
194 | 0x02, 0x0e, /* 2: DA's power up, normal speed, RSTN#=0 */ |
195 | // 0x02, 0x2e, /* quad speed */ | |
196 | 0x03, 0x01, /* 3: de-emphasis off */ | |
197 | 0x04, 0x00, /* 4: LOUT1 volume muted */ | |
198 | 0x05, 0x00, /* 5: ROUT1 volume muted */ | |
199 | 0x06, 0x00, /* 6: LOUT2 volume muted */ | |
200 | 0x07, 0x00, /* 7: ROUT2 volume muted */ | |
201 | 0x08, 0x00, /* 8: LOUT3 volume muted */ | |
202 | 0x09, 0x00, /* 9: ROUT3 volume muted */ | |
203 | 0x0a, 0x00, /* a: DATT speed=0, ignore DZF */ | |
204 | 0x01, 0x01, /* 1: un-reset, unmute */ | |
205 | 0xff, 0xff | |
206 | }; | |
207 | static unsigned char inits_ak4358[] = { | |
208 | 0x01, 0x02, /* 1: reset and soft-mute */ | |
cb9d24e4 TI |
209 | 0x00, 0x06, /* 0: mode3(i2s), disable auto-clock detect, |
210 | * disable DZF, sharp roll-off, RSTN#=0 */ | |
1da177e4 LT |
211 | 0x02, 0x0e, /* 2: DA's power up, normal speed, RSTN#=0 */ |
212 | // 0x02, 0x2e, /* quad speed */ | |
213 | 0x03, 0x01, /* 3: de-emphasis off */ | |
214 | 0x04, 0x00, /* 4: LOUT1 volume muted */ | |
215 | 0x05, 0x00, /* 5: ROUT1 volume muted */ | |
216 | 0x06, 0x00, /* 6: LOUT2 volume muted */ | |
217 | 0x07, 0x00, /* 7: ROUT2 volume muted */ | |
218 | 0x08, 0x00, /* 8: LOUT3 volume muted */ | |
219 | 0x09, 0x00, /* 9: ROUT3 volume muted */ | |
220 | 0x0b, 0x00, /* b: LOUT4 volume muted */ | |
221 | 0x0c, 0x00, /* c: ROUT4 volume muted */ | |
222 | 0x0a, 0x00, /* a: DATT speed=0, ignore DZF */ | |
223 | 0x01, 0x01, /* 1: un-reset, unmute */ | |
224 | 0xff, 0xff | |
225 | }; | |
226 | static unsigned char inits_ak4381[] = { | |
227 | 0x00, 0x0c, /* 0: mode3(i2s), disable auto-clock detect */ | |
cb9d24e4 TI |
228 | 0x01, 0x02, /* 1: de-emphasis off, normal speed, |
229 | * sharp roll-off, DZF off */ | |
1da177e4 LT |
230 | // 0x01, 0x12, /* quad speed */ |
231 | 0x02, 0x00, /* 2: DZF disabled */ | |
232 | 0x03, 0x00, /* 3: LATT 0 */ | |
233 | 0x04, 0x00, /* 4: RATT 0 */ | |
234 | 0x00, 0x0f, /* 0: power-up, un-reset */ | |
235 | 0xff, 0xff | |
236 | }; | |
237 | ||
238 | int chip, num_chips; | |
239 | unsigned char *ptr, reg, data, *inits; | |
240 | ||
241 | switch (ak->type) { | |
242 | case SND_AK4524: | |
243 | inits = inits_ak4524; | |
244 | num_chips = ak->num_dacs / 2; | |
245 | break; | |
246 | case SND_AK4528: | |
247 | inits = inits_ak4528; | |
248 | num_chips = ak->num_dacs / 2; | |
249 | break; | |
250 | case SND_AK4529: | |
251 | inits = inits_ak4529; | |
252 | num_chips = 1; | |
253 | break; | |
254 | case SND_AK4355: | |
255 | inits = inits_ak4355; | |
256 | num_chips = 1; | |
257 | break; | |
258 | case SND_AK4358: | |
259 | inits = inits_ak4358; | |
260 | num_chips = 1; | |
261 | break; | |
262 | case SND_AK4381: | |
263 | inits = inits_ak4381; | |
264 | num_chips = ak->num_dacs / 2; | |
265 | break; | |
266 | default: | |
267 | snd_BUG(); | |
268 | return; | |
269 | } | |
270 | ||
271 | for (chip = 0; chip < num_chips; chip++) { | |
272 | ptr = inits; | |
273 | while (*ptr != 0xff) { | |
274 | reg = *ptr++; | |
275 | data = *ptr++; | |
276 | snd_akm4xxx_write(ak, chip, reg, data); | |
277 | } | |
278 | } | |
279 | } | |
280 | ||
cb9d24e4 TI |
281 | EXPORT_SYMBOL(snd_akm4xxx_init); |
282 | ||
1da177e4 LT |
283 | #define AK_GET_CHIP(val) (((val) >> 8) & 0xff) |
284 | #define AK_GET_ADDR(val) ((val) & 0xff) | |
285 | #define AK_GET_SHIFT(val) (((val) >> 16) & 0x7f) | |
286 | #define AK_GET_INVERT(val) (((val) >> 23) & 1) | |
287 | #define AK_GET_MASK(val) (((val) >> 24) & 0xff) | |
cb9d24e4 TI |
288 | #define AK_COMPOSE(chip,addr,shift,mask) \ |
289 | (((chip) << 8) | (addr) | ((shift) << 16) | ((mask) << 24)) | |
1da177e4 LT |
290 | #define AK_INVERT (1<<23) |
291 | ||
97f02e05 TI |
292 | static int snd_akm4xxx_volume_info(struct snd_kcontrol *kcontrol, |
293 | struct snd_ctl_elem_info *uinfo) | |
1da177e4 LT |
294 | { |
295 | unsigned int mask = AK_GET_MASK(kcontrol->private_value); | |
296 | ||
297 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
298 | uinfo->count = 1; | |
299 | uinfo->value.integer.min = 0; | |
300 | uinfo->value.integer.max = mask; | |
301 | return 0; | |
302 | } | |
303 | ||
97f02e05 TI |
304 | static int snd_akm4xxx_volume_get(struct snd_kcontrol *kcontrol, |
305 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 306 | { |
97f02e05 | 307 | struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
308 | int chip = AK_GET_CHIP(kcontrol->private_value); |
309 | int addr = AK_GET_ADDR(kcontrol->private_value); | |
310 | int invert = AK_GET_INVERT(kcontrol->private_value); | |
311 | unsigned int mask = AK_GET_MASK(kcontrol->private_value); | |
312 | unsigned char val = snd_akm4xxx_get(ak, chip, addr); | |
313 | ||
314 | ucontrol->value.integer.value[0] = invert ? mask - val : val; | |
315 | return 0; | |
316 | } | |
317 | ||
97f02e05 TI |
318 | static int snd_akm4xxx_volume_put(struct snd_kcontrol *kcontrol, |
319 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 320 | { |
97f02e05 | 321 | struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
322 | int chip = AK_GET_CHIP(kcontrol->private_value); |
323 | int addr = AK_GET_ADDR(kcontrol->private_value); | |
324 | int invert = AK_GET_INVERT(kcontrol->private_value); | |
325 | unsigned int mask = AK_GET_MASK(kcontrol->private_value); | |
326 | unsigned char nval = ucontrol->value.integer.value[0] % (mask+1); | |
327 | int change; | |
328 | ||
329 | if (invert) | |
330 | nval = mask - nval; | |
331 | change = snd_akm4xxx_get(ak, chip, addr) != nval; | |
332 | if (change) | |
333 | snd_akm4xxx_write(ak, chip, addr, nval); | |
334 | return change; | |
335 | } | |
336 | ||
c83c0c47 | 337 | static int snd_akm4xxx_stereo_volume_info(struct snd_kcontrol *kcontrol, |
cb9d24e4 | 338 | struct snd_ctl_elem_info *uinfo) |
c83c0c47 JA |
339 | { |
340 | unsigned int mask = AK_GET_MASK(kcontrol->private_value); | |
341 | ||
342 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
343 | uinfo->count = 2; | |
344 | uinfo->value.integer.min = 0; | |
345 | uinfo->value.integer.max = mask; | |
346 | return 0; | |
347 | } | |
348 | ||
349 | static int snd_akm4xxx_stereo_volume_get(struct snd_kcontrol *kcontrol, | |
cb9d24e4 | 350 | struct snd_ctl_elem_value *ucontrol) |
c83c0c47 JA |
351 | { |
352 | struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol); | |
353 | int chip = AK_GET_CHIP(kcontrol->private_value); | |
354 | int addr = AK_GET_ADDR(kcontrol->private_value); | |
355 | int invert = AK_GET_INVERT(kcontrol->private_value); | |
356 | unsigned int mask = AK_GET_MASK(kcontrol->private_value); | |
357 | unsigned char val = snd_akm4xxx_get(ak, chip, addr); | |
358 | ||
359 | ucontrol->value.integer.value[0] = invert ? mask - val : val; | |
360 | ||
361 | val = snd_akm4xxx_get(ak, chip, addr+1); | |
362 | ucontrol->value.integer.value[1] = invert ? mask - val : val; | |
363 | ||
364 | return 0; | |
365 | } | |
366 | ||
367 | static int snd_akm4xxx_stereo_volume_put(struct snd_kcontrol *kcontrol, | |
cb9d24e4 | 368 | struct snd_ctl_elem_value *ucontrol) |
c83c0c47 JA |
369 | { |
370 | struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol); | |
371 | int chip = AK_GET_CHIP(kcontrol->private_value); | |
372 | int addr = AK_GET_ADDR(kcontrol->private_value); | |
373 | int invert = AK_GET_INVERT(kcontrol->private_value); | |
374 | unsigned int mask = AK_GET_MASK(kcontrol->private_value); | |
375 | unsigned char nval = ucontrol->value.integer.value[0] % (mask+1); | |
376 | int change0, change1; | |
377 | ||
378 | if (invert) | |
379 | nval = mask - nval; | |
380 | change0 = snd_akm4xxx_get(ak, chip, addr) != nval; | |
381 | if (change0) | |
382 | snd_akm4xxx_write(ak, chip, addr, nval); | |
383 | ||
384 | nval = ucontrol->value.integer.value[1] % (mask+1); | |
385 | if (invert) | |
386 | nval = mask - nval; | |
387 | change1 = snd_akm4xxx_get(ak, chip, addr+1) != nval; | |
388 | if (change1) | |
389 | snd_akm4xxx_write(ak, chip, addr+1, nval); | |
390 | ||
391 | ||
392 | return change0 || change1; | |
393 | } | |
394 | ||
97f02e05 TI |
395 | static int snd_akm4xxx_ipga_gain_info(struct snd_kcontrol *kcontrol, |
396 | struct snd_ctl_elem_info *uinfo) | |
1da177e4 LT |
397 | { |
398 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
399 | uinfo->count = 1; | |
400 | uinfo->value.integer.min = 0; | |
401 | uinfo->value.integer.max = 36; | |
402 | return 0; | |
403 | } | |
404 | ||
97f02e05 TI |
405 | static int snd_akm4xxx_ipga_gain_get(struct snd_kcontrol *kcontrol, |
406 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 407 | { |
97f02e05 | 408 | struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
409 | int chip = AK_GET_CHIP(kcontrol->private_value); |
410 | int addr = AK_GET_ADDR(kcontrol->private_value); | |
cb9d24e4 TI |
411 | ucontrol->value.integer.value[0] = |
412 | snd_akm4xxx_get_ipga(ak, chip, addr) & 0x7f; | |
1da177e4 LT |
413 | return 0; |
414 | } | |
415 | ||
97f02e05 TI |
416 | static int snd_akm4xxx_ipga_gain_put(struct snd_kcontrol *kcontrol, |
417 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 418 | { |
97f02e05 | 419 | struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
420 | int chip = AK_GET_CHIP(kcontrol->private_value); |
421 | int addr = AK_GET_ADDR(kcontrol->private_value); | |
422 | unsigned char nval = (ucontrol->value.integer.value[0] % 37) | 0x80; | |
423 | int change = snd_akm4xxx_get_ipga(ak, chip, addr) != nval; | |
424 | if (change) | |
425 | snd_akm4xxx_write(ak, chip, addr, nval); | |
426 | return change; | |
427 | } | |
428 | ||
97f02e05 TI |
429 | static int snd_akm4xxx_deemphasis_info(struct snd_kcontrol *kcontrol, |
430 | struct snd_ctl_elem_info *uinfo) | |
1da177e4 LT |
431 | { |
432 | static char *texts[4] = { | |
433 | "44.1kHz", "Off", "48kHz", "32kHz", | |
434 | }; | |
435 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
436 | uinfo->count = 1; | |
437 | uinfo->value.enumerated.items = 4; | |
438 | if (uinfo->value.enumerated.item >= 4) | |
439 | uinfo->value.enumerated.item = 3; | |
cb9d24e4 TI |
440 | strcpy(uinfo->value.enumerated.name, |
441 | texts[uinfo->value.enumerated.item]); | |
1da177e4 LT |
442 | return 0; |
443 | } | |
444 | ||
97f02e05 TI |
445 | static int snd_akm4xxx_deemphasis_get(struct snd_kcontrol *kcontrol, |
446 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 447 | { |
97f02e05 | 448 | struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
449 | int chip = AK_GET_CHIP(kcontrol->private_value); |
450 | int addr = AK_GET_ADDR(kcontrol->private_value); | |
451 | int shift = AK_GET_SHIFT(kcontrol->private_value); | |
cb9d24e4 TI |
452 | ucontrol->value.enumerated.item[0] = |
453 | (snd_akm4xxx_get(ak, chip, addr) >> shift) & 3; | |
1da177e4 LT |
454 | return 0; |
455 | } | |
456 | ||
97f02e05 TI |
457 | static int snd_akm4xxx_deemphasis_put(struct snd_kcontrol *kcontrol, |
458 | struct snd_ctl_elem_value *ucontrol) | |
1da177e4 | 459 | { |
97f02e05 | 460 | struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol); |
1da177e4 LT |
461 | int chip = AK_GET_CHIP(kcontrol->private_value); |
462 | int addr = AK_GET_ADDR(kcontrol->private_value); | |
463 | int shift = AK_GET_SHIFT(kcontrol->private_value); | |
464 | unsigned char nval = ucontrol->value.enumerated.item[0] & 3; | |
465 | int change; | |
466 | ||
cb9d24e4 TI |
467 | nval = (nval << shift) | |
468 | (snd_akm4xxx_get(ak, chip, addr) & ~(3 << shift)); | |
1da177e4 LT |
469 | change = snd_akm4xxx_get(ak, chip, addr) != nval; |
470 | if (change) | |
471 | snd_akm4xxx_write(ak, chip, addr, nval); | |
472 | return change; | |
473 | } | |
474 | ||
30ba6e20 JV |
475 | static int ak4xxx_switch_info(struct snd_kcontrol *kcontrol, |
476 | struct snd_ctl_elem_info *uinfo) | |
477 | { | |
478 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | |
479 | uinfo->count = 1; | |
480 | uinfo->value.integer.min = 0; | |
481 | uinfo->value.integer.max = 1; | |
482 | return 0; | |
483 | } | |
484 | ||
485 | static int ak4xxx_switch_get(struct snd_kcontrol *kcontrol, | |
486 | struct snd_ctl_elem_value *ucontrol) | |
487 | { | |
488 | struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol); | |
489 | int chip = AK_GET_CHIP(kcontrol->private_value); | |
490 | int addr = AK_GET_ADDR(kcontrol->private_value); | |
491 | int shift = AK_GET_SHIFT(kcontrol->private_value); | |
492 | int invert = AK_GET_INVERT(kcontrol->private_value); | |
493 | unsigned char val = snd_akm4xxx_get(ak, chip, addr); | |
494 | ||
495 | if (invert) | |
496 | val = ! val; | |
497 | ucontrol->value.integer.value[0] = (val & (1<<shift)) != 0; | |
498 | return 0; | |
499 | } | |
500 | ||
501 | static int ak4xxx_switch_put(struct snd_kcontrol *kcontrol, | |
502 | struct snd_ctl_elem_value *ucontrol) | |
503 | { | |
504 | struct snd_akm4xxx *ak = snd_kcontrol_chip(kcontrol); | |
505 | int chip = AK_GET_CHIP(kcontrol->private_value); | |
506 | int addr = AK_GET_ADDR(kcontrol->private_value); | |
507 | int shift = AK_GET_SHIFT(kcontrol->private_value); | |
508 | int invert = AK_GET_INVERT(kcontrol->private_value); | |
509 | long flag = ucontrol->value.integer.value[0]; | |
510 | unsigned char val, oval; | |
511 | int change; | |
512 | ||
513 | if (invert) | |
514 | flag = ! flag; | |
515 | oval = snd_akm4xxx_get(ak, chip, addr); | |
516 | if (flag) | |
517 | val = oval | (1<<shift); | |
518 | else | |
519 | val = oval & ~(1<<shift); | |
520 | change = (oval != val); | |
521 | if (change) | |
522 | snd_akm4xxx_write(ak, chip, addr, val); | |
523 | return change; | |
524 | } | |
525 | ||
1da177e4 LT |
526 | /* |
527 | * build AK4xxx controls | |
528 | */ | |
529 | ||
97f02e05 | 530 | int snd_akm4xxx_build_controls(struct snd_akm4xxx *ak) |
1da177e4 LT |
531 | { |
532 | unsigned int idx, num_emphs; | |
97f02e05 | 533 | struct snd_kcontrol *ctl; |
1da177e4 | 534 | int err; |
c83c0c47 JA |
535 | int mixer_ch = 0; |
536 | int num_stereo; | |
1da177e4 LT |
537 | |
538 | ctl = kmalloc(sizeof(*ctl), GFP_KERNEL); | |
539 | if (! ctl) | |
540 | return -ENOMEM; | |
541 | ||
c83c0c47 | 542 | for (idx = 0; idx < ak->num_dacs; ) { |
1da177e4 | 543 | memset(ctl, 0, sizeof(*ctl)); |
c83c0c47 JA |
544 | if (ak->channel_names == NULL) { |
545 | strcpy(ctl->id.name, "DAC Volume"); | |
546 | num_stereo = 1; | |
547 | ctl->id.index = mixer_ch + ak->idx_offset * 2; | |
548 | } else { | |
549 | strcpy(ctl->id.name, ak->channel_names[mixer_ch]); | |
550 | num_stereo = ak->num_stereo[mixer_ch]; | |
cb9d24e4 | 551 | ctl->id.index = 0; |
c83c0c47 | 552 | } |
1da177e4 LT |
553 | ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; |
554 | ctl->count = 1; | |
c83c0c47 JA |
555 | if (num_stereo == 2) { |
556 | ctl->info = snd_akm4xxx_stereo_volume_info; | |
557 | ctl->get = snd_akm4xxx_stereo_volume_get; | |
558 | ctl->put = snd_akm4xxx_stereo_volume_put; | |
559 | } else { | |
560 | ctl->info = snd_akm4xxx_volume_info; | |
561 | ctl->get = snd_akm4xxx_volume_get; | |
562 | ctl->put = snd_akm4xxx_volume_put; | |
563 | } | |
1da177e4 LT |
564 | switch (ak->type) { |
565 | case SND_AK4524: | |
cb9d24e4 TI |
566 | /* register 6 & 7 */ |
567 | ctl->private_value = | |
568 | AK_COMPOSE(idx/2, (idx%2) + 6, 0, 127); | |
1da177e4 LT |
569 | break; |
570 | case SND_AK4528: | |
cb9d24e4 TI |
571 | /* register 4 & 5 */ |
572 | ctl->private_value = | |
573 | AK_COMPOSE(idx/2, (idx%2) + 4, 0, 127); | |
1da177e4 LT |
574 | break; |
575 | case SND_AK4529: { | |
cb9d24e4 TI |
576 | /* registers 2-7 and b,c */ |
577 | int val = idx < 6 ? idx + 2 : (idx - 6) + 0xb; | |
578 | ctl->private_value = | |
579 | AK_COMPOSE(0, val, 0, 255) | AK_INVERT; | |
1da177e4 LT |
580 | break; |
581 | } | |
582 | case SND_AK4355: | |
cb9d24e4 TI |
583 | /* register 4-9, chip #0 only */ |
584 | ctl->private_value = AK_COMPOSE(0, idx + 4, 0, 255); | |
1da177e4 LT |
585 | break; |
586 | case SND_AK4358: | |
587 | if (idx >= 6) | |
cb9d24e4 TI |
588 | /* register 4-9, chip #0 only */ |
589 | ctl->private_value = | |
590 | AK_COMPOSE(0, idx + 5, 0, 255); | |
1da177e4 | 591 | else |
cb9d24e4 TI |
592 | /* register 4-9, chip #0 only */ |
593 | ctl->private_value = | |
594 | AK_COMPOSE(0, idx + 4, 0, 255); | |
1da177e4 LT |
595 | break; |
596 | case SND_AK4381: | |
cb9d24e4 TI |
597 | /* register 3 & 4 */ |
598 | ctl->private_value = | |
599 | AK_COMPOSE(idx/2, (idx%2) + 3, 0, 255); | |
1da177e4 LT |
600 | break; |
601 | default: | |
602 | err = -EINVAL; | |
603 | goto __error; | |
604 | } | |
c83c0c47 | 605 | |
1da177e4 | 606 | ctl->private_data = ak; |
cb9d24e4 TI |
607 | err = snd_ctl_add(ak->card, |
608 | snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ| | |
609 | SNDRV_CTL_ELEM_ACCESS_WRITE)); | |
610 | if (err < 0) | |
1da177e4 | 611 | goto __error; |
c83c0c47 JA |
612 | |
613 | idx += num_stereo; | |
614 | mixer_ch++; | |
1da177e4 LT |
615 | } |
616 | for (idx = 0; idx < ak->num_adcs && ak->type == SND_AK4524; ++idx) { | |
617 | memset(ctl, 0, sizeof(*ctl)); | |
618 | strcpy(ctl->id.name, "ADC Volume"); | |
619 | ctl->id.index = idx + ak->idx_offset * 2; | |
620 | ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; | |
621 | ctl->count = 1; | |
622 | ctl->info = snd_akm4xxx_volume_info; | |
623 | ctl->get = snd_akm4xxx_volume_get; | |
624 | ctl->put = snd_akm4xxx_volume_put; | |
cb9d24e4 TI |
625 | /* register 4 & 5 */ |
626 | ctl->private_value = | |
627 | AK_COMPOSE(idx/2, (idx%2) + 4, 0, 127); | |
1da177e4 | 628 | ctl->private_data = ak; |
cb9d24e4 TI |
629 | err = snd_ctl_add(ak->card, |
630 | snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ| | |
631 | SNDRV_CTL_ELEM_ACCESS_WRITE)); | |
632 | if (err < 0) | |
1da177e4 LT |
633 | goto __error; |
634 | ||
635 | memset(ctl, 0, sizeof(*ctl)); | |
636 | strcpy(ctl->id.name, "IPGA Analog Capture Volume"); | |
637 | ctl->id.index = idx + ak->idx_offset * 2; | |
638 | ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; | |
639 | ctl->count = 1; | |
640 | ctl->info = snd_akm4xxx_ipga_gain_info; | |
641 | ctl->get = snd_akm4xxx_ipga_gain_get; | |
642 | ctl->put = snd_akm4xxx_ipga_gain_put; | |
cb9d24e4 TI |
643 | /* register 4 & 5 */ |
644 | ctl->private_value = AK_COMPOSE(idx/2, (idx%2) + 4, 0, 0); | |
1da177e4 | 645 | ctl->private_data = ak; |
cb9d24e4 TI |
646 | err = snd_ctl_add(ak->card, |
647 | snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ| | |
648 | SNDRV_CTL_ELEM_ACCESS_WRITE)); | |
649 | if (err < 0) | |
1da177e4 LT |
650 | goto __error; |
651 | } | |
683fe153 JV |
652 | |
653 | if (ak->type == SND_AK5365) { | |
654 | memset(ctl, 0, sizeof(*ctl)); | |
655 | if (ak->channel_names == NULL) | |
656 | strcpy(ctl->id.name, "Capture Volume"); | |
657 | else | |
658 | strcpy(ctl->id.name, ak->channel_names[0]); | |
659 | ctl->id.index = ak->idx_offset * 2; | |
660 | ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; | |
661 | ctl->count = 1; | |
662 | ctl->info = snd_akm4xxx_stereo_volume_info; | |
663 | ctl->get = snd_akm4xxx_stereo_volume_get; | |
664 | ctl->put = snd_akm4xxx_stereo_volume_put; | |
665 | /* Registers 4 & 5 (see AK5365 data sheet, pages 34 and 35): | |
666 | * valid values are from 0x00 (mute) to 0x98 (+12dB). */ | |
667 | ctl->private_value = | |
668 | AK_COMPOSE(0, 4, 0, 0x98); | |
669 | ctl->private_data = ak; | |
670 | err = snd_ctl_add(ak->card, | |
671 | snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ| | |
672 | SNDRV_CTL_ELEM_ACCESS_WRITE)); | |
673 | if (err < 0) | |
674 | goto __error; | |
30ba6e20 JV |
675 | |
676 | memset(ctl, 0, sizeof(*ctl)); | |
677 | if (ak->channel_names == NULL) | |
678 | strcpy(ctl->id.name, "Capture Switch"); | |
679 | else | |
680 | strcpy(ctl->id.name, ak->channel_names[1]); | |
681 | ctl->id.index = ak->idx_offset * 2; | |
682 | ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; | |
683 | ctl->count = 1; | |
684 | ctl->info = ak4xxx_switch_info; | |
685 | ctl->get = ak4xxx_switch_get; | |
686 | ctl->put = ak4xxx_switch_put; | |
687 | /* register 2, bit 0 (SMUTE): 0 = normal operation, 1 = mute */ | |
688 | ctl->private_value = | |
689 | AK_COMPOSE(0, 2, 0, 0) | AK_INVERT; | |
690 | ctl->private_data = ak; | |
691 | err = snd_ctl_add(ak->card, | |
692 | snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ| | |
693 | SNDRV_CTL_ELEM_ACCESS_WRITE)); | |
694 | if (err < 0) | |
695 | goto __error; | |
683fe153 JV |
696 | } |
697 | ||
1da177e4 LT |
698 | if (ak->type == SND_AK4355 || ak->type == SND_AK4358) |
699 | num_emphs = 1; | |
700 | else | |
701 | num_emphs = ak->num_dacs / 2; | |
702 | for (idx = 0; idx < num_emphs; idx++) { | |
703 | memset(ctl, 0, sizeof(*ctl)); | |
704 | strcpy(ctl->id.name, "Deemphasis"); | |
705 | ctl->id.index = idx + ak->idx_offset; | |
706 | ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER; | |
707 | ctl->count = 1; | |
708 | ctl->info = snd_akm4xxx_deemphasis_info; | |
709 | ctl->get = snd_akm4xxx_deemphasis_get; | |
710 | ctl->put = snd_akm4xxx_deemphasis_put; | |
711 | switch (ak->type) { | |
712 | case SND_AK4524: | |
713 | case SND_AK4528: | |
cb9d24e4 TI |
714 | /* register 3 */ |
715 | ctl->private_value = AK_COMPOSE(idx, 3, 0, 0); | |
1da177e4 LT |
716 | break; |
717 | case SND_AK4529: { | |
718 | int shift = idx == 3 ? 6 : (2 - idx) * 2; | |
cb9d24e4 TI |
719 | /* register 8 with shift */ |
720 | ctl->private_value = AK_COMPOSE(0, 8, shift, 0); | |
1da177e4 LT |
721 | break; |
722 | } | |
723 | case SND_AK4355: | |
724 | case SND_AK4358: | |
725 | ctl->private_value = AK_COMPOSE(idx, 3, 0, 0); | |
726 | break; | |
727 | case SND_AK4381: | |
728 | ctl->private_value = AK_COMPOSE(idx, 1, 1, 0); | |
729 | break; | |
730 | } | |
731 | ctl->private_data = ak; | |
cb9d24e4 TI |
732 | err = snd_ctl_add(ak->card, |
733 | snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ| | |
734 | SNDRV_CTL_ELEM_ACCESS_WRITE)); | |
735 | if (err < 0) | |
1da177e4 LT |
736 | goto __error; |
737 | } | |
738 | err = 0; | |
739 | ||
740 | __error: | |
741 | kfree(ctl); | |
742 | return err; | |
743 | } | |
744 | ||
cb9d24e4 TI |
745 | EXPORT_SYMBOL(snd_akm4xxx_build_controls); |
746 | ||
1da177e4 LT |
747 | static int __init alsa_akm4xxx_module_init(void) |
748 | { | |
749 | return 0; | |
750 | } | |
751 | ||
752 | static void __exit alsa_akm4xxx_module_exit(void) | |
753 | { | |
754 | } | |
755 | ||
756 | module_init(alsa_akm4xxx_module_init) | |
757 | module_exit(alsa_akm4xxx_module_exit) |