blob: 722de451d15fcedc331e290c5e6eda589fea5e2e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02002 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Universal routines for AK4531 codec
4 *
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include <sound/driver.h>
23#include <linux/delay.h>
24#include <linux/init.h>
25#include <linux/slab.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010026#include <linux/mutex.h>
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <sound/core.h>
29#include <sound/ak4531_codec.h>
Takashi Iwai91072262006-08-23 12:04:34 +020030#include <sound/tlv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020032MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070033MODULE_DESCRIPTION("Universal routines for AK4531 codec");
34MODULE_LICENSE("GPL");
35
Takashi Iwaiadf1b3d2005-12-01 10:49:58 +010036#ifdef CONFIG_PROC_FS
Takashi Iwai9f389452005-11-17 14:44:47 +010037static void snd_ak4531_proc_init(struct snd_card *card, struct snd_ak4531 *ak4531);
Takashi Iwaiadf1b3d2005-12-01 10:49:58 +010038#else
39#define snd_ak4531_proc_init(card,ak)
40#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/*
43 *
44 */
45
46#if 0
47
Takashi Iwai9f389452005-11-17 14:44:47 +010048static void snd_ak4531_dump(struct snd_ak4531 *ak4531)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 int idx;
51
52 for (idx = 0; idx < 0x19; idx++)
53 printk("ak4531 0x%x: 0x%x\n", idx, ak4531->regs[idx]);
54}
55
56#endif
57
58/*
59 *
60 */
61
62#define AK4531_SINGLE(xname, xindex, reg, shift, mask, invert) \
63{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
64 .info = snd_ak4531_info_single, \
65 .get = snd_ak4531_get_single, .put = snd_ak4531_put_single, \
66 .private_value = reg | (shift << 16) | (mask << 24) | (invert << 22) }
Takashi Iwai91072262006-08-23 12:04:34 +020067#define AK4531_SINGLE_TLV(xname, xindex, reg, shift, mask, invert, xtlv) \
68{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
69 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
70 .name = xname, .index = xindex, \
71 .info = snd_ak4531_info_single, \
72 .get = snd_ak4531_get_single, .put = snd_ak4531_put_single, \
73 .private_value = reg | (shift << 16) | (mask << 24) | (invert << 22), \
74 .tlv = { .p = (xtlv) } }
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Takashi Iwai9f389452005-11-17 14:44:47 +010076static int snd_ak4531_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 int mask = (kcontrol->private_value >> 24) & 0xff;
79
80 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
81 uinfo->count = 1;
82 uinfo->value.integer.min = 0;
83 uinfo->value.integer.max = mask;
84 return 0;
85}
86
Takashi Iwai9f389452005-11-17 14:44:47 +010087static int snd_ak4531_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Takashi Iwai9f389452005-11-17 14:44:47 +010089 struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 int reg = kcontrol->private_value & 0xff;
91 int shift = (kcontrol->private_value >> 16) & 0x07;
92 int mask = (kcontrol->private_value >> 24) & 0xff;
93 int invert = (kcontrol->private_value >> 22) & 1;
94 int val;
95
Ingo Molnar62932df2006-01-16 16:34:20 +010096 mutex_lock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 val = (ak4531->regs[reg] >> shift) & mask;
Ingo Molnar62932df2006-01-16 16:34:20 +010098 mutex_unlock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (invert) {
100 val = mask - val;
101 }
102 ucontrol->value.integer.value[0] = val;
103 return 0;
104}
105
Takashi Iwai9f389452005-11-17 14:44:47 +0100106static int snd_ak4531_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Takashi Iwai9f389452005-11-17 14:44:47 +0100108 struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 int reg = kcontrol->private_value & 0xff;
110 int shift = (kcontrol->private_value >> 16) & 0x07;
111 int mask = (kcontrol->private_value >> 24) & 0xff;
112 int invert = (kcontrol->private_value >> 22) & 1;
113 int change;
114 int val;
115
116 val = ucontrol->value.integer.value[0] & mask;
117 if (invert) {
118 val = mask - val;
119 }
120 val <<= shift;
Ingo Molnar62932df2006-01-16 16:34:20 +0100121 mutex_lock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 val = (ak4531->regs[reg] & ~(mask << shift)) | val;
123 change = val != ak4531->regs[reg];
124 ak4531->write(ak4531, reg, ak4531->regs[reg] = val);
Ingo Molnar62932df2006-01-16 16:34:20 +0100125 mutex_unlock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return change;
127}
128
129#define AK4531_DOUBLE(xname, xindex, left_reg, right_reg, left_shift, right_shift, mask, invert) \
130{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
131 .info = snd_ak4531_info_double, \
132 .get = snd_ak4531_get_double, .put = snd_ak4531_put_double, \
133 .private_value = left_reg | (right_reg << 8) | (left_shift << 16) | (right_shift << 19) | (mask << 24) | (invert << 22) }
Takashi Iwai91072262006-08-23 12:04:34 +0200134#define AK4531_DOUBLE_TLV(xname, xindex, left_reg, right_reg, left_shift, right_shift, mask, invert, xtlv) \
135{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
136 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
137 .name = xname, .index = xindex, \
138 .info = snd_ak4531_info_double, \
139 .get = snd_ak4531_get_double, .put = snd_ak4531_put_double, \
140 .private_value = left_reg | (right_reg << 8) | (left_shift << 16) | (right_shift << 19) | (mask << 24) | (invert << 22), \
141 .tlv = { .p = (xtlv) } }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Takashi Iwai9f389452005-11-17 14:44:47 +0100143static int snd_ak4531_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 int mask = (kcontrol->private_value >> 24) & 0xff;
146
147 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
148 uinfo->count = 2;
149 uinfo->value.integer.min = 0;
150 uinfo->value.integer.max = mask;
151 return 0;
152}
153
Takashi Iwai9f389452005-11-17 14:44:47 +0100154static int snd_ak4531_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Takashi Iwai9f389452005-11-17 14:44:47 +0100156 struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 int left_reg = kcontrol->private_value & 0xff;
158 int right_reg = (kcontrol->private_value >> 8) & 0xff;
159 int left_shift = (kcontrol->private_value >> 16) & 0x07;
160 int right_shift = (kcontrol->private_value >> 19) & 0x07;
161 int mask = (kcontrol->private_value >> 24) & 0xff;
162 int invert = (kcontrol->private_value >> 22) & 1;
163 int left, right;
164
Ingo Molnar62932df2006-01-16 16:34:20 +0100165 mutex_lock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 left = (ak4531->regs[left_reg] >> left_shift) & mask;
167 right = (ak4531->regs[right_reg] >> right_shift) & mask;
Ingo Molnar62932df2006-01-16 16:34:20 +0100168 mutex_unlock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (invert) {
170 left = mask - left;
171 right = mask - right;
172 }
173 ucontrol->value.integer.value[0] = left;
174 ucontrol->value.integer.value[1] = right;
175 return 0;
176}
177
Takashi Iwai9f389452005-11-17 14:44:47 +0100178static int snd_ak4531_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Takashi Iwai9f389452005-11-17 14:44:47 +0100180 struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 int left_reg = kcontrol->private_value & 0xff;
182 int right_reg = (kcontrol->private_value >> 8) & 0xff;
183 int left_shift = (kcontrol->private_value >> 16) & 0x07;
184 int right_shift = (kcontrol->private_value >> 19) & 0x07;
185 int mask = (kcontrol->private_value >> 24) & 0xff;
186 int invert = (kcontrol->private_value >> 22) & 1;
187 int change;
188 int left, right;
189
190 left = ucontrol->value.integer.value[0] & mask;
191 right = ucontrol->value.integer.value[1] & mask;
192 if (invert) {
193 left = mask - left;
194 right = mask - right;
195 }
196 left <<= left_shift;
197 right <<= right_shift;
Ingo Molnar62932df2006-01-16 16:34:20 +0100198 mutex_lock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (left_reg == right_reg) {
200 left = (ak4531->regs[left_reg] & ~((mask << left_shift) | (mask << right_shift))) | left | right;
201 change = left != ak4531->regs[left_reg];
202 ak4531->write(ak4531, left_reg, ak4531->regs[left_reg] = left);
203 } else {
204 left = (ak4531->regs[left_reg] & ~(mask << left_shift)) | left;
205 right = (ak4531->regs[right_reg] & ~(mask << right_shift)) | right;
206 change = left != ak4531->regs[left_reg] || right != ak4531->regs[right_reg];
207 ak4531->write(ak4531, left_reg, ak4531->regs[left_reg] = left);
208 ak4531->write(ak4531, right_reg, ak4531->regs[right_reg] = right);
209 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100210 mutex_unlock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return change;
212}
213
214#define AK4531_INPUT_SW(xname, xindex, reg1, reg2, left_shift, right_shift) \
215{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
216 .info = snd_ak4531_info_input_sw, \
217 .get = snd_ak4531_get_input_sw, .put = snd_ak4531_put_input_sw, \
218 .private_value = reg1 | (reg2 << 8) | (left_shift << 16) | (right_shift << 24) }
219
Takashi Iwai9f389452005-11-17 14:44:47 +0100220static int snd_ak4531_info_input_sw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
223 uinfo->count = 4;
224 uinfo->value.integer.min = 0;
225 uinfo->value.integer.max = 1;
226 return 0;
227}
228
Takashi Iwai9f389452005-11-17 14:44:47 +0100229static int snd_ak4531_get_input_sw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Takashi Iwai9f389452005-11-17 14:44:47 +0100231 struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 int reg1 = kcontrol->private_value & 0xff;
233 int reg2 = (kcontrol->private_value >> 8) & 0xff;
234 int left_shift = (kcontrol->private_value >> 16) & 0x0f;
235 int right_shift = (kcontrol->private_value >> 24) & 0x0f;
236
Ingo Molnar62932df2006-01-16 16:34:20 +0100237 mutex_lock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 ucontrol->value.integer.value[0] = (ak4531->regs[reg1] >> left_shift) & 1;
239 ucontrol->value.integer.value[1] = (ak4531->regs[reg2] >> left_shift) & 1;
240 ucontrol->value.integer.value[2] = (ak4531->regs[reg1] >> right_shift) & 1;
241 ucontrol->value.integer.value[3] = (ak4531->regs[reg2] >> right_shift) & 1;
Ingo Molnar62932df2006-01-16 16:34:20 +0100242 mutex_unlock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244}
245
Takashi Iwai9f389452005-11-17 14:44:47 +0100246static int snd_ak4531_put_input_sw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Takashi Iwai9f389452005-11-17 14:44:47 +0100248 struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 int reg1 = kcontrol->private_value & 0xff;
250 int reg2 = (kcontrol->private_value >> 8) & 0xff;
251 int left_shift = (kcontrol->private_value >> 16) & 0x0f;
252 int right_shift = (kcontrol->private_value >> 24) & 0x0f;
253 int change;
254 int val1, val2;
255
Ingo Molnar62932df2006-01-16 16:34:20 +0100256 mutex_lock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 val1 = ak4531->regs[reg1] & ~((1 << left_shift) | (1 << right_shift));
258 val2 = ak4531->regs[reg2] & ~((1 << left_shift) | (1 << right_shift));
259 val1 |= (ucontrol->value.integer.value[0] & 1) << left_shift;
260 val2 |= (ucontrol->value.integer.value[1] & 1) << left_shift;
261 val1 |= (ucontrol->value.integer.value[2] & 1) << right_shift;
262 val2 |= (ucontrol->value.integer.value[3] & 1) << right_shift;
263 change = val1 != ak4531->regs[reg1] || val2 != ak4531->regs[reg2];
264 ak4531->write(ak4531, reg1, ak4531->regs[reg1] = val1);
265 ak4531->write(ak4531, reg2, ak4531->regs[reg2] = val2);
Ingo Molnar62932df2006-01-16 16:34:20 +0100266 mutex_unlock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return change;
268}
269
Takashi Iwai0cb29ea2007-01-29 15:33:49 +0100270static const DECLARE_TLV_DB_SCALE(db_scale_master, -6200, 200, 0);
271static const DECLARE_TLV_DB_SCALE(db_scale_mono, -2800, 400, 0);
272static const DECLARE_TLV_DB_SCALE(db_scale_input, -5000, 200, 0);
Takashi Iwai91072262006-08-23 12:04:34 +0200273
Takashi Iwai9f389452005-11-17 14:44:47 +0100274static struct snd_kcontrol_new snd_ak4531_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Takashi Iwai91072262006-08-23 12:04:34 +0200276AK4531_DOUBLE_TLV("Master Playback Switch", 0,
277 AK4531_LMASTER, AK4531_RMASTER, 7, 7, 1, 1,
278 db_scale_master),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279AK4531_DOUBLE("Master Playback Volume", 0, AK4531_LMASTER, AK4531_RMASTER, 0, 0, 0x1f, 1),
280
Takashi Iwai91072262006-08-23 12:04:34 +0200281AK4531_SINGLE_TLV("Master Mono Playback Switch", 0, AK4531_MONO_OUT, 7, 1, 1,
282 db_scale_mono),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283AK4531_SINGLE("Master Mono Playback Volume", 0, AK4531_MONO_OUT, 0, 0x07, 1),
284
285AK4531_DOUBLE("PCM Switch", 0, AK4531_LVOICE, AK4531_RVOICE, 7, 7, 1, 1),
Takashi Iwai91072262006-08-23 12:04:34 +0200286AK4531_DOUBLE_TLV("PCM Volume", 0, AK4531_LVOICE, AK4531_RVOICE, 0, 0, 0x1f, 1,
287 db_scale_input),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288AK4531_DOUBLE("PCM Playback Switch", 0, AK4531_OUT_SW2, AK4531_OUT_SW2, 3, 2, 1, 0),
289AK4531_DOUBLE("PCM Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 2, 2, 1, 0),
290
291AK4531_DOUBLE("PCM Switch", 1, AK4531_LFM, AK4531_RFM, 7, 7, 1, 1),
Takashi Iwai91072262006-08-23 12:04:34 +0200292AK4531_DOUBLE_TLV("PCM Volume", 1, AK4531_LFM, AK4531_RFM, 0, 0, 0x1f, 1,
293 db_scale_input),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294AK4531_DOUBLE("PCM Playback Switch", 1, AK4531_OUT_SW1, AK4531_OUT_SW1, 6, 5, 1, 0),
295AK4531_INPUT_SW("PCM Capture Route", 1, AK4531_LIN_SW1, AK4531_RIN_SW1, 6, 5),
296
297AK4531_DOUBLE("CD Switch", 0, AK4531_LCD, AK4531_RCD, 7, 7, 1, 1),
Takashi Iwai91072262006-08-23 12:04:34 +0200298AK4531_DOUBLE_TLV("CD Volume", 0, AK4531_LCD, AK4531_RCD, 0, 0, 0x1f, 1,
299 db_scale_input),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300AK4531_DOUBLE("CD Playback Switch", 0, AK4531_OUT_SW1, AK4531_OUT_SW1, 2, 1, 1, 0),
301AK4531_INPUT_SW("CD Capture Route", 0, AK4531_LIN_SW1, AK4531_RIN_SW1, 2, 1),
302
303AK4531_DOUBLE("Line Switch", 0, AK4531_LLINE, AK4531_RLINE, 7, 7, 1, 1),
Takashi Iwai91072262006-08-23 12:04:34 +0200304AK4531_DOUBLE_TLV("Line Volume", 0, AK4531_LLINE, AK4531_RLINE, 0, 0, 0x1f, 1,
305 db_scale_input),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306AK4531_DOUBLE("Line Playback Switch", 0, AK4531_OUT_SW1, AK4531_OUT_SW1, 4, 3, 1, 0),
307AK4531_INPUT_SW("Line Capture Route", 0, AK4531_LIN_SW1, AK4531_RIN_SW1, 4, 3),
308
309AK4531_DOUBLE("Aux Switch", 0, AK4531_LAUXA, AK4531_RAUXA, 7, 7, 1, 1),
Takashi Iwai91072262006-08-23 12:04:34 +0200310AK4531_DOUBLE_TLV("Aux Volume", 0, AK4531_LAUXA, AK4531_RAUXA, 0, 0, 0x1f, 1,
311 db_scale_input),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312AK4531_DOUBLE("Aux Playback Switch", 0, AK4531_OUT_SW2, AK4531_OUT_SW2, 5, 4, 1, 0),
313AK4531_INPUT_SW("Aux Capture Route", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 4, 3),
314
315AK4531_SINGLE("Mono Switch", 0, AK4531_MONO1, 7, 1, 1),
Takashi Iwai91072262006-08-23 12:04:34 +0200316AK4531_SINGLE_TLV("Mono Volume", 0, AK4531_MONO1, 0, 0x1f, 1, db_scale_input),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317AK4531_SINGLE("Mono Playback Switch", 0, AK4531_OUT_SW2, 0, 1, 0),
318AK4531_DOUBLE("Mono Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 0, 0, 1, 0),
319
320AK4531_SINGLE("Mono Switch", 1, AK4531_MONO2, 7, 1, 1),
Takashi Iwai91072262006-08-23 12:04:34 +0200321AK4531_SINGLE_TLV("Mono Volume", 1, AK4531_MONO2, 0, 0x1f, 1, db_scale_input),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322AK4531_SINGLE("Mono Playback Switch", 1, AK4531_OUT_SW2, 1, 1, 0),
323AK4531_DOUBLE("Mono Capture Switch", 1, AK4531_LIN_SW2, AK4531_RIN_SW2, 1, 1, 1, 0),
324
Takashi Iwai91072262006-08-23 12:04:34 +0200325AK4531_SINGLE_TLV("Mic Volume", 0, AK4531_MIC, 0, 0x1f, 1, db_scale_input),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326AK4531_SINGLE("Mic Switch", 0, AK4531_MIC, 7, 1, 1),
327AK4531_SINGLE("Mic Playback Switch", 0, AK4531_OUT_SW1, 0, 1, 0),
328AK4531_DOUBLE("Mic Capture Switch", 0, AK4531_LIN_SW1, AK4531_RIN_SW1, 0, 0, 1, 0),
329
330AK4531_DOUBLE("Mic Bypass Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 7, 7, 1, 0),
331AK4531_DOUBLE("Mono1 Bypass Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 6, 6, 1, 0),
332AK4531_DOUBLE("Mono2 Bypass Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 5, 5, 1, 0),
333
334AK4531_SINGLE("AD Input Select", 0, AK4531_AD_IN, 0, 1, 0),
335AK4531_SINGLE("Mic Boost (+30dB)", 0, AK4531_MIC_GAIN, 0, 1, 0)
336};
337
Takashi Iwai9f389452005-11-17 14:44:47 +0100338static int snd_ak4531_free(struct snd_ak4531 *ak4531)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
340 if (ak4531) {
341 if (ak4531->private_free)
342 ak4531->private_free(ak4531);
343 kfree(ak4531);
344 }
345 return 0;
346}
347
Takashi Iwai9f389452005-11-17 14:44:47 +0100348static int snd_ak4531_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Takashi Iwai9f389452005-11-17 14:44:47 +0100350 struct snd_ak4531 *ak4531 = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return snd_ak4531_free(ak4531);
352}
353
354static u8 snd_ak4531_initial_map[0x19 + 1] = {
355 0x9f, /* 00: Master Volume Lch */
356 0x9f, /* 01: Master Volume Rch */
357 0x9f, /* 02: Voice Volume Lch */
358 0x9f, /* 03: Voice Volume Rch */
359 0x9f, /* 04: FM Volume Lch */
360 0x9f, /* 05: FM Volume Rch */
361 0x9f, /* 06: CD Audio Volume Lch */
362 0x9f, /* 07: CD Audio Volume Rch */
363 0x9f, /* 08: Line Volume Lch */
364 0x9f, /* 09: Line Volume Rch */
365 0x9f, /* 0a: Aux Volume Lch */
366 0x9f, /* 0b: Aux Volume Rch */
367 0x9f, /* 0c: Mono1 Volume */
368 0x9f, /* 0d: Mono2 Volume */
369 0x9f, /* 0e: Mic Volume */
370 0x87, /* 0f: Mono-out Volume */
371 0x00, /* 10: Output Mixer SW1 */
372 0x00, /* 11: Output Mixer SW2 */
373 0x00, /* 12: Lch Input Mixer SW1 */
374 0x00, /* 13: Rch Input Mixer SW1 */
375 0x00, /* 14: Lch Input Mixer SW2 */
376 0x00, /* 15: Rch Input Mixer SW2 */
377 0x00, /* 16: Reset & Power Down */
378 0x00, /* 17: Clock Select */
379 0x00, /* 18: AD Input Select */
380 0x01 /* 19: Mic Amp Setup */
381};
382
Takashi Iwai9f389452005-11-17 14:44:47 +0100383int snd_ak4531_mixer(struct snd_card *card, struct snd_ak4531 *_ak4531,
384 struct snd_ak4531 **rak4531)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 unsigned int idx;
387 int err;
Takashi Iwai9f389452005-11-17 14:44:47 +0100388 struct snd_ak4531 *ak4531;
389 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 .dev_free = snd_ak4531_dev_free,
391 };
392
393 snd_assert(rak4531 != NULL, return -EINVAL);
394 *rak4531 = NULL;
395 snd_assert(card != NULL && _ak4531 != NULL, return -EINVAL);
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200396 ak4531 = kzalloc(sizeof(*ak4531), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (ak4531 == NULL)
398 return -ENOMEM;
399 *ak4531 = *_ak4531;
Ingo Molnar62932df2006-01-16 16:34:20 +0100400 mutex_init(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if ((err = snd_component_add(card, "AK4531")) < 0) {
402 snd_ak4531_free(ak4531);
403 return err;
404 }
405 strcpy(card->mixername, "Asahi Kasei AK4531");
406 ak4531->write(ak4531, AK4531_RESET, 0x03); /* no RST, PD */
407 udelay(100);
408 ak4531->write(ak4531, AK4531_CLOCK, 0x00); /* CODEC ADC and CODEC DAC use {LR,B}CLK2 and run off LRCLK2 PLL */
Takashi Iwai11d38242005-11-17 16:13:05 +0100409 for (idx = 0; idx <= 0x19; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 if (idx == AK4531_RESET || idx == AK4531_CLOCK)
411 continue;
412 ak4531->write(ak4531, idx, ak4531->regs[idx] = snd_ak4531_initial_map[idx]); /* recording source is mixer */
413 }
414 for (idx = 0; idx < ARRAY_SIZE(snd_ak4531_controls); idx++) {
415 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ak4531_controls[idx], ak4531))) < 0) {
416 snd_ak4531_free(ak4531);
417 return err;
418 }
419 }
420 snd_ak4531_proc_init(card, ak4531);
421 if ((err = snd_device_new(card, SNDRV_DEV_CODEC, ak4531, &ops)) < 0) {
422 snd_ak4531_free(ak4531);
423 return err;
424 }
425
426#if 0
427 snd_ak4531_dump(ak4531);
428#endif
429 *rak4531 = ak4531;
430 return 0;
431}
432
433/*
Takashi Iwai11d38242005-11-17 16:13:05 +0100434 * power management
435 */
436#ifdef CONFIG_PM
437void snd_ak4531_suspend(struct snd_ak4531 *ak4531)
438{
439 /* mute */
440 ak4531->write(ak4531, AK4531_LMASTER, 0x9f);
441 ak4531->write(ak4531, AK4531_RMASTER, 0x9f);
442 /* powerdown */
443 ak4531->write(ak4531, AK4531_RESET, 0x01);
444}
445
446void snd_ak4531_resume(struct snd_ak4531 *ak4531)
447{
448 int idx;
449
450 /* initialize */
451 ak4531->write(ak4531, AK4531_RESET, 0x03);
452 udelay(100);
453 ak4531->write(ak4531, AK4531_CLOCK, 0x00);
454 /* restore mixer registers */
455 for (idx = 0; idx <= 0x19; idx++) {
456 if (idx == AK4531_RESET || idx == AK4531_CLOCK)
457 continue;
458 ak4531->write(ak4531, idx, ak4531->regs[idx]);
459 }
460}
461#endif
462
Takashi Iwaiadf1b3d2005-12-01 10:49:58 +0100463#ifdef CONFIG_PROC_FS
Takashi Iwai11d38242005-11-17 16:13:05 +0100464/*
Takashi Iwaiadf1b3d2005-12-01 10:49:58 +0100465 * /proc interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 */
467
Takashi Iwai9f389452005-11-17 14:44:47 +0100468static void snd_ak4531_proc_read(struct snd_info_entry *entry,
469 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Takashi Iwai9f389452005-11-17 14:44:47 +0100471 struct snd_ak4531 *ak4531 = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 snd_iprintf(buffer, "Asahi Kasei AK4531\n\n");
474 snd_iprintf(buffer, "Recording source : %s\n"
475 "MIC gain : %s\n",
476 ak4531->regs[AK4531_AD_IN] & 1 ? "external" : "mixer",
477 ak4531->regs[AK4531_MIC_GAIN] & 1 ? "+30dB" : "+0dB");
478}
479
Takashi Iwai9f389452005-11-17 14:44:47 +0100480static void snd_ak4531_proc_init(struct snd_card *card, struct snd_ak4531 *ak4531)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Takashi Iwai9f389452005-11-17 14:44:47 +0100482 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 if (! snd_card_proc_new(card, "ak4531", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +0200485 snd_info_set_text_ops(entry, ak4531, snd_ak4531_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
Takashi Iwaiadf1b3d2005-12-01 10:49:58 +0100487#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489EXPORT_SYMBOL(snd_ak4531_mixer);
Takashi Iwai11d38242005-11-17 16:13:05 +0100490#ifdef CONFIG_PM
491EXPORT_SYMBOL(snd_ak4531_suspend);
492EXPORT_SYMBOL(snd_ak4531_resume);
493#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495/*
496 * INIT part
497 */
498
499static int __init alsa_ak4531_init(void)
500{
501 return 0;
502}
503
504static void __exit alsa_ak4531_exit(void)
505{
506}
507
508module_init(alsa_ak4531_init)
509module_exit(alsa_ak4531_exit)