blob: 3bf0dc53360ab5fd2a550477feb05a4584df1046 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/delay.h>
23#include <linux/init.h>
24#include <linux/slab.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010025#include <linux/mutex.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040026#include <linux/module.h>
Ingo Molnar62932df2006-01-16 16:34:20 +010027
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 Kysela67192922008-06-16 10:39:34 +020032/*
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020033MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070034MODULE_DESCRIPTION("Universal routines for AK4531 codec");
35MODULE_LICENSE("GPL");
Jaroslav Kysela67192922008-06-16 10:39:34 +020036*/
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Takashi Iwaiadf1b3d2005-12-01 10:49:58 +010038#ifdef CONFIG_PROC_FS
Takashi Iwai9f389452005-11-17 14:44:47 +010039static void snd_ak4531_proc_init(struct snd_card *card, struct snd_ak4531 *ak4531);
Takashi Iwaiadf1b3d2005-12-01 10:49:58 +010040#else
41#define snd_ak4531_proc_init(card,ak)
42#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 *
46 */
47
48#if 0
49
Takashi Iwai9f389452005-11-17 14:44:47 +010050static void snd_ak4531_dump(struct snd_ak4531 *ak4531)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 int idx;
53
54 for (idx = 0; idx < 0x19; idx++)
Takashi Iwaiee419652009-02-05 16:11:31 +010055 printk(KERN_DEBUG "ak4531 0x%x: 0x%x\n",
56 idx, ak4531->regs[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59#endif
60
61/*
62 *
63 */
64
65#define AK4531_SINGLE(xname, xindex, reg, shift, mask, invert) \
66{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
67 .info = snd_ak4531_info_single, \
68 .get = snd_ak4531_get_single, .put = snd_ak4531_put_single, \
69 .private_value = reg | (shift << 16) | (mask << 24) | (invert << 22) }
Takashi Iwai91072262006-08-23 12:04:34 +020070#define AK4531_SINGLE_TLV(xname, xindex, reg, shift, mask, invert, xtlv) \
71{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
72 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
73 .name = xname, .index = xindex, \
74 .info = snd_ak4531_info_single, \
75 .get = snd_ak4531_get_single, .put = snd_ak4531_put_single, \
76 .private_value = reg | (shift << 16) | (mask << 24) | (invert << 22), \
77 .tlv = { .p = (xtlv) } }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Takashi Iwai9f389452005-11-17 14:44:47 +010079static int snd_ak4531_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 int mask = (kcontrol->private_value >> 24) & 0xff;
82
83 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
84 uinfo->count = 1;
85 uinfo->value.integer.min = 0;
86 uinfo->value.integer.max = mask;
87 return 0;
88}
89
Takashi Iwai9f389452005-11-17 14:44:47 +010090static int snd_ak4531_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Takashi Iwai9f389452005-11-17 14:44:47 +010092 struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 int reg = kcontrol->private_value & 0xff;
94 int shift = (kcontrol->private_value >> 16) & 0x07;
95 int mask = (kcontrol->private_value >> 24) & 0xff;
96 int invert = (kcontrol->private_value >> 22) & 1;
97 int val;
98
Ingo Molnar62932df2006-01-16 16:34:20 +010099 mutex_lock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 val = (ak4531->regs[reg] >> shift) & mask;
Ingo Molnar62932df2006-01-16 16:34:20 +0100101 mutex_unlock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (invert) {
103 val = mask - val;
104 }
105 ucontrol->value.integer.value[0] = val;
106 return 0;
107}
108
Takashi Iwai9f389452005-11-17 14:44:47 +0100109static int snd_ak4531_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Takashi Iwai9f389452005-11-17 14:44:47 +0100111 struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int reg = kcontrol->private_value & 0xff;
113 int shift = (kcontrol->private_value >> 16) & 0x07;
114 int mask = (kcontrol->private_value >> 24) & 0xff;
115 int invert = (kcontrol->private_value >> 22) & 1;
116 int change;
117 int val;
118
119 val = ucontrol->value.integer.value[0] & mask;
120 if (invert) {
121 val = mask - val;
122 }
123 val <<= shift;
Ingo Molnar62932df2006-01-16 16:34:20 +0100124 mutex_lock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 val = (ak4531->regs[reg] & ~(mask << shift)) | val;
126 change = val != ak4531->regs[reg];
127 ak4531->write(ak4531, reg, ak4531->regs[reg] = val);
Ingo Molnar62932df2006-01-16 16:34:20 +0100128 mutex_unlock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return change;
130}
131
132#define AK4531_DOUBLE(xname, xindex, left_reg, right_reg, left_shift, right_shift, mask, invert) \
133{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
134 .info = snd_ak4531_info_double, \
135 .get = snd_ak4531_get_double, .put = snd_ak4531_put_double, \
136 .private_value = left_reg | (right_reg << 8) | (left_shift << 16) | (right_shift << 19) | (mask << 24) | (invert << 22) }
Takashi Iwai91072262006-08-23 12:04:34 +0200137#define AK4531_DOUBLE_TLV(xname, xindex, left_reg, right_reg, left_shift, right_shift, mask, invert, xtlv) \
138{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
139 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \
140 .name = xname, .index = xindex, \
141 .info = snd_ak4531_info_double, \
142 .get = snd_ak4531_get_double, .put = snd_ak4531_put_double, \
143 .private_value = left_reg | (right_reg << 8) | (left_shift << 16) | (right_shift << 19) | (mask << 24) | (invert << 22), \
144 .tlv = { .p = (xtlv) } }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Takashi Iwai9f389452005-11-17 14:44:47 +0100146static int snd_ak4531_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 int mask = (kcontrol->private_value >> 24) & 0xff;
149
150 uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER;
151 uinfo->count = 2;
152 uinfo->value.integer.min = 0;
153 uinfo->value.integer.max = mask;
154 return 0;
155}
156
Takashi Iwai9f389452005-11-17 14:44:47 +0100157static int snd_ak4531_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Takashi Iwai9f389452005-11-17 14:44:47 +0100159 struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 int left_reg = kcontrol->private_value & 0xff;
161 int right_reg = (kcontrol->private_value >> 8) & 0xff;
162 int left_shift = (kcontrol->private_value >> 16) & 0x07;
163 int right_shift = (kcontrol->private_value >> 19) & 0x07;
164 int mask = (kcontrol->private_value >> 24) & 0xff;
165 int invert = (kcontrol->private_value >> 22) & 1;
166 int left, right;
167
Ingo Molnar62932df2006-01-16 16:34:20 +0100168 mutex_lock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 left = (ak4531->regs[left_reg] >> left_shift) & mask;
170 right = (ak4531->regs[right_reg] >> right_shift) & mask;
Ingo Molnar62932df2006-01-16 16:34:20 +0100171 mutex_unlock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (invert) {
173 left = mask - left;
174 right = mask - right;
175 }
176 ucontrol->value.integer.value[0] = left;
177 ucontrol->value.integer.value[1] = right;
178 return 0;
179}
180
Takashi Iwai9f389452005-11-17 14:44:47 +0100181static int snd_ak4531_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Takashi Iwai9f389452005-11-17 14:44:47 +0100183 struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 int left_reg = kcontrol->private_value & 0xff;
185 int right_reg = (kcontrol->private_value >> 8) & 0xff;
186 int left_shift = (kcontrol->private_value >> 16) & 0x07;
187 int right_shift = (kcontrol->private_value >> 19) & 0x07;
188 int mask = (kcontrol->private_value >> 24) & 0xff;
189 int invert = (kcontrol->private_value >> 22) & 1;
190 int change;
191 int left, right;
192
193 left = ucontrol->value.integer.value[0] & mask;
194 right = ucontrol->value.integer.value[1] & mask;
195 if (invert) {
196 left = mask - left;
197 right = mask - right;
198 }
199 left <<= left_shift;
200 right <<= right_shift;
Ingo Molnar62932df2006-01-16 16:34:20 +0100201 mutex_lock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (left_reg == right_reg) {
203 left = (ak4531->regs[left_reg] & ~((mask << left_shift) | (mask << right_shift))) | left | right;
204 change = left != ak4531->regs[left_reg];
205 ak4531->write(ak4531, left_reg, ak4531->regs[left_reg] = left);
206 } else {
207 left = (ak4531->regs[left_reg] & ~(mask << left_shift)) | left;
208 right = (ak4531->regs[right_reg] & ~(mask << right_shift)) | right;
209 change = left != ak4531->regs[left_reg] || right != ak4531->regs[right_reg];
210 ak4531->write(ak4531, left_reg, ak4531->regs[left_reg] = left);
211 ak4531->write(ak4531, right_reg, ak4531->regs[right_reg] = right);
212 }
Ingo Molnar62932df2006-01-16 16:34:20 +0100213 mutex_unlock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return change;
215}
216
217#define AK4531_INPUT_SW(xname, xindex, reg1, reg2, left_shift, right_shift) \
218{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
219 .info = snd_ak4531_info_input_sw, \
220 .get = snd_ak4531_get_input_sw, .put = snd_ak4531_put_input_sw, \
221 .private_value = reg1 | (reg2 << 8) | (left_shift << 16) | (right_shift << 24) }
222
Takashi Iwai9f389452005-11-17 14:44:47 +0100223static int snd_ak4531_info_input_sw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
226 uinfo->count = 4;
227 uinfo->value.integer.min = 0;
228 uinfo->value.integer.max = 1;
229 return 0;
230}
231
Takashi Iwai9f389452005-11-17 14:44:47 +0100232static int snd_ak4531_get_input_sw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Takashi Iwai9f389452005-11-17 14:44:47 +0100234 struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 int reg1 = kcontrol->private_value & 0xff;
236 int reg2 = (kcontrol->private_value >> 8) & 0xff;
237 int left_shift = (kcontrol->private_value >> 16) & 0x0f;
238 int right_shift = (kcontrol->private_value >> 24) & 0x0f;
239
Ingo Molnar62932df2006-01-16 16:34:20 +0100240 mutex_lock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 ucontrol->value.integer.value[0] = (ak4531->regs[reg1] >> left_shift) & 1;
242 ucontrol->value.integer.value[1] = (ak4531->regs[reg2] >> left_shift) & 1;
243 ucontrol->value.integer.value[2] = (ak4531->regs[reg1] >> right_shift) & 1;
244 ucontrol->value.integer.value[3] = (ak4531->regs[reg2] >> right_shift) & 1;
Ingo Molnar62932df2006-01-16 16:34:20 +0100245 mutex_unlock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return 0;
247}
248
Takashi Iwai9f389452005-11-17 14:44:47 +0100249static int snd_ak4531_put_input_sw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Takashi Iwai9f389452005-11-17 14:44:47 +0100251 struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 int reg1 = kcontrol->private_value & 0xff;
253 int reg2 = (kcontrol->private_value >> 8) & 0xff;
254 int left_shift = (kcontrol->private_value >> 16) & 0x0f;
255 int right_shift = (kcontrol->private_value >> 24) & 0x0f;
256 int change;
257 int val1, val2;
258
Ingo Molnar62932df2006-01-16 16:34:20 +0100259 mutex_lock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 val1 = ak4531->regs[reg1] & ~((1 << left_shift) | (1 << right_shift));
261 val2 = ak4531->regs[reg2] & ~((1 << left_shift) | (1 << right_shift));
262 val1 |= (ucontrol->value.integer.value[0] & 1) << left_shift;
263 val2 |= (ucontrol->value.integer.value[1] & 1) << left_shift;
264 val1 |= (ucontrol->value.integer.value[2] & 1) << right_shift;
265 val2 |= (ucontrol->value.integer.value[3] & 1) << right_shift;
266 change = val1 != ak4531->regs[reg1] || val2 != ak4531->regs[reg2];
267 ak4531->write(ak4531, reg1, ak4531->regs[reg1] = val1);
268 ak4531->write(ak4531, reg2, ak4531->regs[reg2] = val2);
Ingo Molnar62932df2006-01-16 16:34:20 +0100269 mutex_unlock(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return change;
271}
272
Takashi Iwai0cb29ea2007-01-29 15:33:49 +0100273static const DECLARE_TLV_DB_SCALE(db_scale_master, -6200, 200, 0);
274static const DECLARE_TLV_DB_SCALE(db_scale_mono, -2800, 400, 0);
275static const DECLARE_TLV_DB_SCALE(db_scale_input, -5000, 200, 0);
Takashi Iwai91072262006-08-23 12:04:34 +0200276
Bill Pembertone23e7a12012-12-06 12:35:10 -0500277static struct snd_kcontrol_new snd_ak4531_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Takashi Iwai91072262006-08-23 12:04:34 +0200279AK4531_DOUBLE_TLV("Master Playback Switch", 0,
280 AK4531_LMASTER, AK4531_RMASTER, 7, 7, 1, 1,
281 db_scale_master),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282AK4531_DOUBLE("Master Playback Volume", 0, AK4531_LMASTER, AK4531_RMASTER, 0, 0, 0x1f, 1),
283
Takashi Iwai91072262006-08-23 12:04:34 +0200284AK4531_SINGLE_TLV("Master Mono Playback Switch", 0, AK4531_MONO_OUT, 7, 1, 1,
285 db_scale_mono),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286AK4531_SINGLE("Master Mono Playback Volume", 0, AK4531_MONO_OUT, 0, 0x07, 1),
287
288AK4531_DOUBLE("PCM Switch", 0, AK4531_LVOICE, AK4531_RVOICE, 7, 7, 1, 1),
Takashi Iwai91072262006-08-23 12:04:34 +0200289AK4531_DOUBLE_TLV("PCM Volume", 0, AK4531_LVOICE, AK4531_RVOICE, 0, 0, 0x1f, 1,
290 db_scale_input),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291AK4531_DOUBLE("PCM Playback Switch", 0, AK4531_OUT_SW2, AK4531_OUT_SW2, 3, 2, 1, 0),
292AK4531_DOUBLE("PCM Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 2, 2, 1, 0),
293
294AK4531_DOUBLE("PCM Switch", 1, AK4531_LFM, AK4531_RFM, 7, 7, 1, 1),
Takashi Iwai91072262006-08-23 12:04:34 +0200295AK4531_DOUBLE_TLV("PCM Volume", 1, AK4531_LFM, AK4531_RFM, 0, 0, 0x1f, 1,
296 db_scale_input),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297AK4531_DOUBLE("PCM Playback Switch", 1, AK4531_OUT_SW1, AK4531_OUT_SW1, 6, 5, 1, 0),
298AK4531_INPUT_SW("PCM Capture Route", 1, AK4531_LIN_SW1, AK4531_RIN_SW1, 6, 5),
299
300AK4531_DOUBLE("CD Switch", 0, AK4531_LCD, AK4531_RCD, 7, 7, 1, 1),
Takashi Iwai91072262006-08-23 12:04:34 +0200301AK4531_DOUBLE_TLV("CD Volume", 0, AK4531_LCD, AK4531_RCD, 0, 0, 0x1f, 1,
302 db_scale_input),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303AK4531_DOUBLE("CD Playback Switch", 0, AK4531_OUT_SW1, AK4531_OUT_SW1, 2, 1, 1, 0),
304AK4531_INPUT_SW("CD Capture Route", 0, AK4531_LIN_SW1, AK4531_RIN_SW1, 2, 1),
305
306AK4531_DOUBLE("Line Switch", 0, AK4531_LLINE, AK4531_RLINE, 7, 7, 1, 1),
Takashi Iwai91072262006-08-23 12:04:34 +0200307AK4531_DOUBLE_TLV("Line Volume", 0, AK4531_LLINE, AK4531_RLINE, 0, 0, 0x1f, 1,
308 db_scale_input),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309AK4531_DOUBLE("Line Playback Switch", 0, AK4531_OUT_SW1, AK4531_OUT_SW1, 4, 3, 1, 0),
310AK4531_INPUT_SW("Line Capture Route", 0, AK4531_LIN_SW1, AK4531_RIN_SW1, 4, 3),
311
312AK4531_DOUBLE("Aux Switch", 0, AK4531_LAUXA, AK4531_RAUXA, 7, 7, 1, 1),
Takashi Iwai91072262006-08-23 12:04:34 +0200313AK4531_DOUBLE_TLV("Aux Volume", 0, AK4531_LAUXA, AK4531_RAUXA, 0, 0, 0x1f, 1,
314 db_scale_input),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315AK4531_DOUBLE("Aux Playback Switch", 0, AK4531_OUT_SW2, AK4531_OUT_SW2, 5, 4, 1, 0),
316AK4531_INPUT_SW("Aux Capture Route", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 4, 3),
317
318AK4531_SINGLE("Mono Switch", 0, AK4531_MONO1, 7, 1, 1),
Takashi Iwai91072262006-08-23 12:04:34 +0200319AK4531_SINGLE_TLV("Mono Volume", 0, AK4531_MONO1, 0, 0x1f, 1, db_scale_input),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320AK4531_SINGLE("Mono Playback Switch", 0, AK4531_OUT_SW2, 0, 1, 0),
321AK4531_DOUBLE("Mono Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 0, 0, 1, 0),
322
323AK4531_SINGLE("Mono Switch", 1, AK4531_MONO2, 7, 1, 1),
Takashi Iwai91072262006-08-23 12:04:34 +0200324AK4531_SINGLE_TLV("Mono Volume", 1, AK4531_MONO2, 0, 0x1f, 1, db_scale_input),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325AK4531_SINGLE("Mono Playback Switch", 1, AK4531_OUT_SW2, 1, 1, 0),
326AK4531_DOUBLE("Mono Capture Switch", 1, AK4531_LIN_SW2, AK4531_RIN_SW2, 1, 1, 1, 0),
327
Takashi Iwai91072262006-08-23 12:04:34 +0200328AK4531_SINGLE_TLV("Mic Volume", 0, AK4531_MIC, 0, 0x1f, 1, db_scale_input),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329AK4531_SINGLE("Mic Switch", 0, AK4531_MIC, 7, 1, 1),
330AK4531_SINGLE("Mic Playback Switch", 0, AK4531_OUT_SW1, 0, 1, 0),
331AK4531_DOUBLE("Mic Capture Switch", 0, AK4531_LIN_SW1, AK4531_RIN_SW1, 0, 0, 1, 0),
332
333AK4531_DOUBLE("Mic Bypass Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 7, 7, 1, 0),
334AK4531_DOUBLE("Mono1 Bypass Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 6, 6, 1, 0),
335AK4531_DOUBLE("Mono2 Bypass Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 5, 5, 1, 0),
336
337AK4531_SINGLE("AD Input Select", 0, AK4531_AD_IN, 0, 1, 0),
338AK4531_SINGLE("Mic Boost (+30dB)", 0, AK4531_MIC_GAIN, 0, 1, 0)
339};
340
Takashi Iwai9f389452005-11-17 14:44:47 +0100341static int snd_ak4531_free(struct snd_ak4531 *ak4531)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 if (ak4531) {
344 if (ak4531->private_free)
345 ak4531->private_free(ak4531);
346 kfree(ak4531);
347 }
348 return 0;
349}
350
Takashi Iwai9f389452005-11-17 14:44:47 +0100351static int snd_ak4531_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Takashi Iwai9f389452005-11-17 14:44:47 +0100353 struct snd_ak4531 *ak4531 = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return snd_ak4531_free(ak4531);
355}
356
357static u8 snd_ak4531_initial_map[0x19 + 1] = {
358 0x9f, /* 00: Master Volume Lch */
359 0x9f, /* 01: Master Volume Rch */
360 0x9f, /* 02: Voice Volume Lch */
361 0x9f, /* 03: Voice Volume Rch */
362 0x9f, /* 04: FM Volume Lch */
363 0x9f, /* 05: FM Volume Rch */
364 0x9f, /* 06: CD Audio Volume Lch */
365 0x9f, /* 07: CD Audio Volume Rch */
366 0x9f, /* 08: Line Volume Lch */
367 0x9f, /* 09: Line Volume Rch */
368 0x9f, /* 0a: Aux Volume Lch */
369 0x9f, /* 0b: Aux Volume Rch */
370 0x9f, /* 0c: Mono1 Volume */
371 0x9f, /* 0d: Mono2 Volume */
372 0x9f, /* 0e: Mic Volume */
373 0x87, /* 0f: Mono-out Volume */
374 0x00, /* 10: Output Mixer SW1 */
375 0x00, /* 11: Output Mixer SW2 */
376 0x00, /* 12: Lch Input Mixer SW1 */
377 0x00, /* 13: Rch Input Mixer SW1 */
378 0x00, /* 14: Lch Input Mixer SW2 */
379 0x00, /* 15: Rch Input Mixer SW2 */
380 0x00, /* 16: Reset & Power Down */
381 0x00, /* 17: Clock Select */
382 0x00, /* 18: AD Input Select */
383 0x01 /* 19: Mic Amp Setup */
384};
385
Bill Pembertone23e7a12012-12-06 12:35:10 -0500386int snd_ak4531_mixer(struct snd_card *card,
387 struct snd_ak4531 *_ak4531,
388 struct snd_ak4531 **rak4531)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 unsigned int idx;
391 int err;
Takashi Iwai9f389452005-11-17 14:44:47 +0100392 struct snd_ak4531 *ak4531;
393 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 .dev_free = snd_ak4531_dev_free,
395 };
396
Takashi Iwaida3cec32008-08-08 17:12:14 +0200397 if (snd_BUG_ON(!card || !_ak4531))
398 return -EINVAL;
399 if (rak4531)
400 *rak4531 = NULL;
Takashi Iwaie560d8d2005-09-09 14:21:46 +0200401 ak4531 = kzalloc(sizeof(*ak4531), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (ak4531 == NULL)
403 return -ENOMEM;
404 *ak4531 = *_ak4531;
Ingo Molnar62932df2006-01-16 16:34:20 +0100405 mutex_init(&ak4531->reg_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if ((err = snd_component_add(card, "AK4531")) < 0) {
407 snd_ak4531_free(ak4531);
408 return err;
409 }
410 strcpy(card->mixername, "Asahi Kasei AK4531");
411 ak4531->write(ak4531, AK4531_RESET, 0x03); /* no RST, PD */
412 udelay(100);
413 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 +0100414 for (idx = 0; idx <= 0x19; idx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (idx == AK4531_RESET || idx == AK4531_CLOCK)
416 continue;
417 ak4531->write(ak4531, idx, ak4531->regs[idx] = snd_ak4531_initial_map[idx]); /* recording source is mixer */
418 }
419 for (idx = 0; idx < ARRAY_SIZE(snd_ak4531_controls); idx++) {
420 if ((err = snd_ctl_add(card, snd_ctl_new1(&snd_ak4531_controls[idx], ak4531))) < 0) {
421 snd_ak4531_free(ak4531);
422 return err;
423 }
424 }
425 snd_ak4531_proc_init(card, ak4531);
426 if ((err = snd_device_new(card, SNDRV_DEV_CODEC, ak4531, &ops)) < 0) {
427 snd_ak4531_free(ak4531);
428 return err;
429 }
430
431#if 0
432 snd_ak4531_dump(ak4531);
433#endif
Takashi Iwaida3cec32008-08-08 17:12:14 +0200434 if (rak4531)
435 *rak4531 = ak4531;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return 0;
437}
438
439/*
Takashi Iwai11d38242005-11-17 16:13:05 +0100440 * power management
441 */
442#ifdef CONFIG_PM
443void snd_ak4531_suspend(struct snd_ak4531 *ak4531)
444{
445 /* mute */
446 ak4531->write(ak4531, AK4531_LMASTER, 0x9f);
447 ak4531->write(ak4531, AK4531_RMASTER, 0x9f);
448 /* powerdown */
449 ak4531->write(ak4531, AK4531_RESET, 0x01);
450}
451
452void snd_ak4531_resume(struct snd_ak4531 *ak4531)
453{
454 int idx;
455
456 /* initialize */
457 ak4531->write(ak4531, AK4531_RESET, 0x03);
458 udelay(100);
459 ak4531->write(ak4531, AK4531_CLOCK, 0x00);
460 /* restore mixer registers */
461 for (idx = 0; idx <= 0x19; idx++) {
462 if (idx == AK4531_RESET || idx == AK4531_CLOCK)
463 continue;
464 ak4531->write(ak4531, idx, ak4531->regs[idx]);
465 }
466}
467#endif
468
Takashi Iwaiadf1b3d2005-12-01 10:49:58 +0100469#ifdef CONFIG_PROC_FS
Takashi Iwai11d38242005-11-17 16:13:05 +0100470/*
Takashi Iwaiadf1b3d2005-12-01 10:49:58 +0100471 * /proc interface
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 */
473
Takashi Iwai9f389452005-11-17 14:44:47 +0100474static void snd_ak4531_proc_read(struct snd_info_entry *entry,
475 struct snd_info_buffer *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Takashi Iwai9f389452005-11-17 14:44:47 +0100477 struct snd_ak4531 *ak4531 = entry->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 snd_iprintf(buffer, "Asahi Kasei AK4531\n\n");
480 snd_iprintf(buffer, "Recording source : %s\n"
481 "MIC gain : %s\n",
482 ak4531->regs[AK4531_AD_IN] & 1 ? "external" : "mixer",
483 ak4531->regs[AK4531_MIC_GAIN] & 1 ? "+30dB" : "+0dB");
484}
485
Bill Pembertone23e7a12012-12-06 12:35:10 -0500486static void
Takashi Iwai23ce1542008-05-30 09:22:22 +0200487snd_ak4531_proc_init(struct snd_card *card, struct snd_ak4531 *ak4531)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Takashi Iwai9f389452005-11-17 14:44:47 +0100489 struct snd_info_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 if (! snd_card_proc_new(card, "ak4531", &entry))
Takashi Iwaibf850202006-04-28 15:13:41 +0200492 snd_info_set_text_ops(entry, ak4531, snd_ak4531_proc_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
Takashi Iwaiadf1b3d2005-12-01 10:49:58 +0100494#endif