Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 3 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Universal routines for AK4531 codec |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/delay.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/slab.h> |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 10 | #include <linux/mutex.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 11 | #include <linux/module.h> |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <sound/core.h> |
| 14 | #include <sound/ak4531_codec.h> |
Takashi Iwai | 9107226 | 2006-08-23 12:04:34 +0200 | [diff] [blame] | 15 | #include <sound/tlv.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
Jaroslav Kysela | 6719292 | 2008-06-16 10:39:34 +0200 | [diff] [blame] | 17 | /* |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 18 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | MODULE_DESCRIPTION("Universal routines for AK4531 codec"); |
| 20 | MODULE_LICENSE("GPL"); |
Jaroslav Kysela | 6719292 | 2008-06-16 10:39:34 +0200 | [diff] [blame] | 21 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 23 | static void snd_ak4531_proc_init(struct snd_card *card, struct snd_ak4531 *ak4531); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | #if 0 |
| 30 | |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 31 | static void snd_ak4531_dump(struct snd_ak4531 *ak4531) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | { |
| 33 | int idx; |
| 34 | |
| 35 | for (idx = 0; idx < 0x19; idx++) |
Takashi Iwai | ee41965 | 2009-02-05 16:11:31 +0100 | [diff] [blame] | 36 | printk(KERN_DEBUG "ak4531 0x%x: 0x%x\n", |
| 37 | idx, ak4531->regs[idx]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | #endif |
| 41 | |
| 42 | /* |
| 43 | * |
| 44 | */ |
| 45 | |
| 46 | #define AK4531_SINGLE(xname, xindex, reg, shift, mask, invert) \ |
| 47 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ |
| 48 | .info = snd_ak4531_info_single, \ |
| 49 | .get = snd_ak4531_get_single, .put = snd_ak4531_put_single, \ |
| 50 | .private_value = reg | (shift << 16) | (mask << 24) | (invert << 22) } |
Takashi Iwai | 9107226 | 2006-08-23 12:04:34 +0200 | [diff] [blame] | 51 | #define AK4531_SINGLE_TLV(xname, xindex, reg, shift, mask, invert, xtlv) \ |
| 52 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ |
| 53 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ |
| 54 | .name = xname, .index = xindex, \ |
| 55 | .info = snd_ak4531_info_single, \ |
| 56 | .get = snd_ak4531_get_single, .put = snd_ak4531_put_single, \ |
| 57 | .private_value = reg | (shift << 16) | (mask << 24) | (invert << 22), \ |
| 58 | .tlv = { .p = (xtlv) } } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 60 | static int snd_ak4531_info_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
| 62 | int mask = (kcontrol->private_value >> 24) & 0xff; |
| 63 | |
| 64 | uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 65 | uinfo->count = 1; |
| 66 | uinfo->value.integer.min = 0; |
| 67 | uinfo->value.integer.max = mask; |
| 68 | return 0; |
| 69 | } |
| 70 | |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 71 | static int snd_ak4531_get_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | { |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 73 | struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | int reg = kcontrol->private_value & 0xff; |
| 75 | int shift = (kcontrol->private_value >> 16) & 0x07; |
| 76 | int mask = (kcontrol->private_value >> 24) & 0xff; |
| 77 | int invert = (kcontrol->private_value >> 22) & 1; |
| 78 | int val; |
| 79 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 80 | mutex_lock(&ak4531->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | val = (ak4531->regs[reg] >> shift) & mask; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 82 | mutex_unlock(&ak4531->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | if (invert) { |
| 84 | val = mask - val; |
| 85 | } |
| 86 | ucontrol->value.integer.value[0] = val; |
| 87 | return 0; |
| 88 | } |
| 89 | |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 90 | static int snd_ak4531_put_single(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 92 | struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | 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 change; |
| 98 | int val; |
| 99 | |
| 100 | val = ucontrol->value.integer.value[0] & mask; |
| 101 | if (invert) { |
| 102 | val = mask - val; |
| 103 | } |
| 104 | val <<= shift; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 105 | mutex_lock(&ak4531->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | val = (ak4531->regs[reg] & ~(mask << shift)) | val; |
| 107 | change = val != ak4531->regs[reg]; |
| 108 | ak4531->write(ak4531, reg, ak4531->regs[reg] = val); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 109 | mutex_unlock(&ak4531->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | return change; |
| 111 | } |
| 112 | |
| 113 | #define AK4531_DOUBLE(xname, xindex, left_reg, right_reg, left_shift, right_shift, mask, invert) \ |
| 114 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ |
| 115 | .info = snd_ak4531_info_double, \ |
| 116 | .get = snd_ak4531_get_double, .put = snd_ak4531_put_double, \ |
| 117 | .private_value = left_reg | (right_reg << 8) | (left_shift << 16) | (right_shift << 19) | (mask << 24) | (invert << 22) } |
Takashi Iwai | 9107226 | 2006-08-23 12:04:34 +0200 | [diff] [blame] | 118 | #define AK4531_DOUBLE_TLV(xname, xindex, left_reg, right_reg, left_shift, right_shift, mask, invert, xtlv) \ |
| 119 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ |
| 120 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ, \ |
| 121 | .name = xname, .index = xindex, \ |
| 122 | .info = snd_ak4531_info_double, \ |
| 123 | .get = snd_ak4531_get_double, .put = snd_ak4531_put_double, \ |
| 124 | .private_value = left_reg | (right_reg << 8) | (left_shift << 16) | (right_shift << 19) | (mask << 24) | (invert << 22), \ |
| 125 | .tlv = { .p = (xtlv) } } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 127 | static int snd_ak4531_info_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | int mask = (kcontrol->private_value >> 24) & 0xff; |
| 130 | |
| 131 | uinfo->type = mask == 1 ? SNDRV_CTL_ELEM_TYPE_BOOLEAN : SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 132 | uinfo->count = 2; |
| 133 | uinfo->value.integer.min = 0; |
| 134 | uinfo->value.integer.max = mask; |
| 135 | return 0; |
| 136 | } |
| 137 | |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 138 | static int snd_ak4531_get_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 140 | struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | int left_reg = kcontrol->private_value & 0xff; |
| 142 | int right_reg = (kcontrol->private_value >> 8) & 0xff; |
| 143 | int left_shift = (kcontrol->private_value >> 16) & 0x07; |
| 144 | int right_shift = (kcontrol->private_value >> 19) & 0x07; |
| 145 | int mask = (kcontrol->private_value >> 24) & 0xff; |
| 146 | int invert = (kcontrol->private_value >> 22) & 1; |
| 147 | int left, right; |
| 148 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 149 | mutex_lock(&ak4531->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | left = (ak4531->regs[left_reg] >> left_shift) & mask; |
| 151 | right = (ak4531->regs[right_reg] >> right_shift) & mask; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 152 | mutex_unlock(&ak4531->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | if (invert) { |
| 154 | left = mask - left; |
| 155 | right = mask - right; |
| 156 | } |
| 157 | ucontrol->value.integer.value[0] = left; |
| 158 | ucontrol->value.integer.value[1] = right; |
| 159 | return 0; |
| 160 | } |
| 161 | |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 162 | static int snd_ak4531_put_double(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | { |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 164 | struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | int left_reg = kcontrol->private_value & 0xff; |
| 166 | int right_reg = (kcontrol->private_value >> 8) & 0xff; |
| 167 | int left_shift = (kcontrol->private_value >> 16) & 0x07; |
| 168 | int right_shift = (kcontrol->private_value >> 19) & 0x07; |
| 169 | int mask = (kcontrol->private_value >> 24) & 0xff; |
| 170 | int invert = (kcontrol->private_value >> 22) & 1; |
| 171 | int change; |
| 172 | int left, right; |
| 173 | |
| 174 | left = ucontrol->value.integer.value[0] & mask; |
| 175 | right = ucontrol->value.integer.value[1] & mask; |
| 176 | if (invert) { |
| 177 | left = mask - left; |
| 178 | right = mask - right; |
| 179 | } |
| 180 | left <<= left_shift; |
| 181 | right <<= right_shift; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 182 | mutex_lock(&ak4531->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | if (left_reg == right_reg) { |
| 184 | left = (ak4531->regs[left_reg] & ~((mask << left_shift) | (mask << right_shift))) | left | right; |
| 185 | change = left != ak4531->regs[left_reg]; |
| 186 | ak4531->write(ak4531, left_reg, ak4531->regs[left_reg] = left); |
| 187 | } else { |
| 188 | left = (ak4531->regs[left_reg] & ~(mask << left_shift)) | left; |
| 189 | right = (ak4531->regs[right_reg] & ~(mask << right_shift)) | right; |
| 190 | change = left != ak4531->regs[left_reg] || right != ak4531->regs[right_reg]; |
| 191 | ak4531->write(ak4531, left_reg, ak4531->regs[left_reg] = left); |
| 192 | ak4531->write(ak4531, right_reg, ak4531->regs[right_reg] = right); |
| 193 | } |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 194 | mutex_unlock(&ak4531->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | return change; |
| 196 | } |
| 197 | |
| 198 | #define AK4531_INPUT_SW(xname, xindex, reg1, reg2, left_shift, right_shift) \ |
| 199 | { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \ |
| 200 | .info = snd_ak4531_info_input_sw, \ |
| 201 | .get = snd_ak4531_get_input_sw, .put = snd_ak4531_put_input_sw, \ |
| 202 | .private_value = reg1 | (reg2 << 8) | (left_shift << 16) | (right_shift << 24) } |
| 203 | |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 204 | static int snd_ak4531_info_input_sw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
| 206 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 207 | uinfo->count = 4; |
| 208 | uinfo->value.integer.min = 0; |
| 209 | uinfo->value.integer.max = 1; |
| 210 | return 0; |
| 211 | } |
| 212 | |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 213 | static int snd_ak4531_get_input_sw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 215 | struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | int reg1 = kcontrol->private_value & 0xff; |
| 217 | int reg2 = (kcontrol->private_value >> 8) & 0xff; |
| 218 | int left_shift = (kcontrol->private_value >> 16) & 0x0f; |
| 219 | int right_shift = (kcontrol->private_value >> 24) & 0x0f; |
| 220 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 221 | mutex_lock(&ak4531->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | ucontrol->value.integer.value[0] = (ak4531->regs[reg1] >> left_shift) & 1; |
| 223 | ucontrol->value.integer.value[1] = (ak4531->regs[reg2] >> left_shift) & 1; |
| 224 | ucontrol->value.integer.value[2] = (ak4531->regs[reg1] >> right_shift) & 1; |
| 225 | ucontrol->value.integer.value[3] = (ak4531->regs[reg2] >> right_shift) & 1; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 226 | mutex_unlock(&ak4531->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 230 | static int snd_ak4531_put_input_sw(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 232 | struct snd_ak4531 *ak4531 = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | int reg1 = kcontrol->private_value & 0xff; |
| 234 | int reg2 = (kcontrol->private_value >> 8) & 0xff; |
| 235 | int left_shift = (kcontrol->private_value >> 16) & 0x0f; |
| 236 | int right_shift = (kcontrol->private_value >> 24) & 0x0f; |
| 237 | int change; |
| 238 | int val1, val2; |
| 239 | |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 240 | mutex_lock(&ak4531->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | val1 = ak4531->regs[reg1] & ~((1 << left_shift) | (1 << right_shift)); |
| 242 | val2 = ak4531->regs[reg2] & ~((1 << left_shift) | (1 << right_shift)); |
| 243 | val1 |= (ucontrol->value.integer.value[0] & 1) << left_shift; |
| 244 | val2 |= (ucontrol->value.integer.value[1] & 1) << left_shift; |
| 245 | val1 |= (ucontrol->value.integer.value[2] & 1) << right_shift; |
| 246 | val2 |= (ucontrol->value.integer.value[3] & 1) << right_shift; |
| 247 | change = val1 != ak4531->regs[reg1] || val2 != ak4531->regs[reg2]; |
| 248 | ak4531->write(ak4531, reg1, ak4531->regs[reg1] = val1); |
| 249 | ak4531->write(ak4531, reg2, ak4531->regs[reg2] = val2); |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 250 | mutex_unlock(&ak4531->reg_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | return change; |
| 252 | } |
| 253 | |
Takashi Iwai | 0cb29ea | 2007-01-29 15:33:49 +0100 | [diff] [blame] | 254 | static const DECLARE_TLV_DB_SCALE(db_scale_master, -6200, 200, 0); |
| 255 | static const DECLARE_TLV_DB_SCALE(db_scale_mono, -2800, 400, 0); |
| 256 | static const DECLARE_TLV_DB_SCALE(db_scale_input, -5000, 200, 0); |
Takashi Iwai | 9107226 | 2006-08-23 12:04:34 +0200 | [diff] [blame] | 257 | |
Takashi Iwai | b4e5e70 | 2020-01-03 09:16:53 +0100 | [diff] [blame] | 258 | static const struct snd_kcontrol_new snd_ak4531_controls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
Takashi Iwai | 9107226 | 2006-08-23 12:04:34 +0200 | [diff] [blame] | 260 | AK4531_DOUBLE_TLV("Master Playback Switch", 0, |
| 261 | AK4531_LMASTER, AK4531_RMASTER, 7, 7, 1, 1, |
| 262 | db_scale_master), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | AK4531_DOUBLE("Master Playback Volume", 0, AK4531_LMASTER, AK4531_RMASTER, 0, 0, 0x1f, 1), |
| 264 | |
Takashi Iwai | 9107226 | 2006-08-23 12:04:34 +0200 | [diff] [blame] | 265 | AK4531_SINGLE_TLV("Master Mono Playback Switch", 0, AK4531_MONO_OUT, 7, 1, 1, |
| 266 | db_scale_mono), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | AK4531_SINGLE("Master Mono Playback Volume", 0, AK4531_MONO_OUT, 0, 0x07, 1), |
| 268 | |
| 269 | AK4531_DOUBLE("PCM Switch", 0, AK4531_LVOICE, AK4531_RVOICE, 7, 7, 1, 1), |
Takashi Iwai | 9107226 | 2006-08-23 12:04:34 +0200 | [diff] [blame] | 270 | AK4531_DOUBLE_TLV("PCM Volume", 0, AK4531_LVOICE, AK4531_RVOICE, 0, 0, 0x1f, 1, |
| 271 | db_scale_input), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | AK4531_DOUBLE("PCM Playback Switch", 0, AK4531_OUT_SW2, AK4531_OUT_SW2, 3, 2, 1, 0), |
| 273 | AK4531_DOUBLE("PCM Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 2, 2, 1, 0), |
| 274 | |
| 275 | AK4531_DOUBLE("PCM Switch", 1, AK4531_LFM, AK4531_RFM, 7, 7, 1, 1), |
Takashi Iwai | 9107226 | 2006-08-23 12:04:34 +0200 | [diff] [blame] | 276 | AK4531_DOUBLE_TLV("PCM Volume", 1, AK4531_LFM, AK4531_RFM, 0, 0, 0x1f, 1, |
| 277 | db_scale_input), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | AK4531_DOUBLE("PCM Playback Switch", 1, AK4531_OUT_SW1, AK4531_OUT_SW1, 6, 5, 1, 0), |
| 279 | AK4531_INPUT_SW("PCM Capture Route", 1, AK4531_LIN_SW1, AK4531_RIN_SW1, 6, 5), |
| 280 | |
| 281 | AK4531_DOUBLE("CD Switch", 0, AK4531_LCD, AK4531_RCD, 7, 7, 1, 1), |
Takashi Iwai | 9107226 | 2006-08-23 12:04:34 +0200 | [diff] [blame] | 282 | AK4531_DOUBLE_TLV("CD Volume", 0, AK4531_LCD, AK4531_RCD, 0, 0, 0x1f, 1, |
| 283 | db_scale_input), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | AK4531_DOUBLE("CD Playback Switch", 0, AK4531_OUT_SW1, AK4531_OUT_SW1, 2, 1, 1, 0), |
| 285 | AK4531_INPUT_SW("CD Capture Route", 0, AK4531_LIN_SW1, AK4531_RIN_SW1, 2, 1), |
| 286 | |
| 287 | AK4531_DOUBLE("Line Switch", 0, AK4531_LLINE, AK4531_RLINE, 7, 7, 1, 1), |
Takashi Iwai | 9107226 | 2006-08-23 12:04:34 +0200 | [diff] [blame] | 288 | AK4531_DOUBLE_TLV("Line Volume", 0, AK4531_LLINE, AK4531_RLINE, 0, 0, 0x1f, 1, |
| 289 | db_scale_input), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | AK4531_DOUBLE("Line Playback Switch", 0, AK4531_OUT_SW1, AK4531_OUT_SW1, 4, 3, 1, 0), |
| 291 | AK4531_INPUT_SW("Line Capture Route", 0, AK4531_LIN_SW1, AK4531_RIN_SW1, 4, 3), |
| 292 | |
| 293 | AK4531_DOUBLE("Aux Switch", 0, AK4531_LAUXA, AK4531_RAUXA, 7, 7, 1, 1), |
Takashi Iwai | 9107226 | 2006-08-23 12:04:34 +0200 | [diff] [blame] | 294 | AK4531_DOUBLE_TLV("Aux Volume", 0, AK4531_LAUXA, AK4531_RAUXA, 0, 0, 0x1f, 1, |
| 295 | db_scale_input), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | AK4531_DOUBLE("Aux Playback Switch", 0, AK4531_OUT_SW2, AK4531_OUT_SW2, 5, 4, 1, 0), |
| 297 | AK4531_INPUT_SW("Aux Capture Route", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 4, 3), |
| 298 | |
| 299 | AK4531_SINGLE("Mono Switch", 0, AK4531_MONO1, 7, 1, 1), |
Takashi Iwai | 9107226 | 2006-08-23 12:04:34 +0200 | [diff] [blame] | 300 | AK4531_SINGLE_TLV("Mono Volume", 0, AK4531_MONO1, 0, 0x1f, 1, db_scale_input), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | AK4531_SINGLE("Mono Playback Switch", 0, AK4531_OUT_SW2, 0, 1, 0), |
| 302 | AK4531_DOUBLE("Mono Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 0, 0, 1, 0), |
| 303 | |
| 304 | AK4531_SINGLE("Mono Switch", 1, AK4531_MONO2, 7, 1, 1), |
Takashi Iwai | 9107226 | 2006-08-23 12:04:34 +0200 | [diff] [blame] | 305 | AK4531_SINGLE_TLV("Mono Volume", 1, AK4531_MONO2, 0, 0x1f, 1, db_scale_input), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | AK4531_SINGLE("Mono Playback Switch", 1, AK4531_OUT_SW2, 1, 1, 0), |
| 307 | AK4531_DOUBLE("Mono Capture Switch", 1, AK4531_LIN_SW2, AK4531_RIN_SW2, 1, 1, 1, 0), |
| 308 | |
Takashi Iwai | 9107226 | 2006-08-23 12:04:34 +0200 | [diff] [blame] | 309 | AK4531_SINGLE_TLV("Mic Volume", 0, AK4531_MIC, 0, 0x1f, 1, db_scale_input), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | AK4531_SINGLE("Mic Switch", 0, AK4531_MIC, 7, 1, 1), |
| 311 | AK4531_SINGLE("Mic Playback Switch", 0, AK4531_OUT_SW1, 0, 1, 0), |
| 312 | AK4531_DOUBLE("Mic Capture Switch", 0, AK4531_LIN_SW1, AK4531_RIN_SW1, 0, 0, 1, 0), |
| 313 | |
| 314 | AK4531_DOUBLE("Mic Bypass Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 7, 7, 1, 0), |
| 315 | AK4531_DOUBLE("Mono1 Bypass Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 6, 6, 1, 0), |
| 316 | AK4531_DOUBLE("Mono2 Bypass Capture Switch", 0, AK4531_LIN_SW2, AK4531_RIN_SW2, 5, 5, 1, 0), |
| 317 | |
| 318 | AK4531_SINGLE("AD Input Select", 0, AK4531_AD_IN, 0, 1, 0), |
| 319 | AK4531_SINGLE("Mic Boost (+30dB)", 0, AK4531_MIC_GAIN, 0, 1, 0) |
| 320 | }; |
| 321 | |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 322 | static int snd_ak4531_free(struct snd_ak4531 *ak4531) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { |
| 324 | if (ak4531) { |
| 325 | if (ak4531->private_free) |
| 326 | ak4531->private_free(ak4531); |
| 327 | kfree(ak4531); |
| 328 | } |
| 329 | return 0; |
| 330 | } |
| 331 | |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 332 | static int snd_ak4531_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 334 | struct snd_ak4531 *ak4531 = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | return snd_ak4531_free(ak4531); |
| 336 | } |
| 337 | |
Takashi Iwai | 24ce805 | 2020-01-05 15:48:14 +0100 | [diff] [blame] | 338 | static const u8 snd_ak4531_initial_map[0x19 + 1] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | 0x9f, /* 00: Master Volume Lch */ |
| 340 | 0x9f, /* 01: Master Volume Rch */ |
| 341 | 0x9f, /* 02: Voice Volume Lch */ |
| 342 | 0x9f, /* 03: Voice Volume Rch */ |
| 343 | 0x9f, /* 04: FM Volume Lch */ |
| 344 | 0x9f, /* 05: FM Volume Rch */ |
| 345 | 0x9f, /* 06: CD Audio Volume Lch */ |
| 346 | 0x9f, /* 07: CD Audio Volume Rch */ |
| 347 | 0x9f, /* 08: Line Volume Lch */ |
| 348 | 0x9f, /* 09: Line Volume Rch */ |
| 349 | 0x9f, /* 0a: Aux Volume Lch */ |
| 350 | 0x9f, /* 0b: Aux Volume Rch */ |
| 351 | 0x9f, /* 0c: Mono1 Volume */ |
| 352 | 0x9f, /* 0d: Mono2 Volume */ |
| 353 | 0x9f, /* 0e: Mic Volume */ |
| 354 | 0x87, /* 0f: Mono-out Volume */ |
| 355 | 0x00, /* 10: Output Mixer SW1 */ |
| 356 | 0x00, /* 11: Output Mixer SW2 */ |
| 357 | 0x00, /* 12: Lch Input Mixer SW1 */ |
| 358 | 0x00, /* 13: Rch Input Mixer SW1 */ |
| 359 | 0x00, /* 14: Lch Input Mixer SW2 */ |
| 360 | 0x00, /* 15: Rch Input Mixer SW2 */ |
| 361 | 0x00, /* 16: Reset & Power Down */ |
| 362 | 0x00, /* 17: Clock Select */ |
| 363 | 0x00, /* 18: AD Input Select */ |
| 364 | 0x01 /* 19: Mic Amp Setup */ |
| 365 | }; |
| 366 | |
Bill Pemberton | e23e7a1 | 2012-12-06 12:35:10 -0500 | [diff] [blame] | 367 | int snd_ak4531_mixer(struct snd_card *card, |
| 368 | struct snd_ak4531 *_ak4531, |
| 369 | struct snd_ak4531 **rak4531) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | { |
| 371 | unsigned int idx; |
| 372 | int err; |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 373 | struct snd_ak4531 *ak4531; |
Takashi Iwai | efb0ad2 | 2020-01-03 09:16:25 +0100 | [diff] [blame] | 374 | static const struct snd_device_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | .dev_free = snd_ak4531_dev_free, |
| 376 | }; |
| 377 | |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 378 | if (snd_BUG_ON(!card || !_ak4531)) |
| 379 | return -EINVAL; |
| 380 | if (rak4531) |
| 381 | *rak4531 = NULL; |
Takashi Iwai | e560d8d | 2005-09-09 14:21:46 +0200 | [diff] [blame] | 382 | ak4531 = kzalloc(sizeof(*ak4531), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | if (ak4531 == NULL) |
| 384 | return -ENOMEM; |
| 385 | *ak4531 = *_ak4531; |
Ingo Molnar | 62932df | 2006-01-16 16:34:20 +0100 | [diff] [blame] | 386 | mutex_init(&ak4531->reg_mutex); |
Takashi Iwai | dadfb98 | 2021-06-08 16:04:50 +0200 | [diff] [blame] | 387 | err = snd_component_add(card, "AK4531"); |
| 388 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | snd_ak4531_free(ak4531); |
| 390 | return err; |
| 391 | } |
| 392 | strcpy(card->mixername, "Asahi Kasei AK4531"); |
| 393 | ak4531->write(ak4531, AK4531_RESET, 0x03); /* no RST, PD */ |
| 394 | udelay(100); |
| 395 | ak4531->write(ak4531, AK4531_CLOCK, 0x00); /* CODEC ADC and CODEC DAC use {LR,B}CLK2 and run off LRCLK2 PLL */ |
Takashi Iwai | 11d3824 | 2005-11-17 16:13:05 +0100 | [diff] [blame] | 396 | for (idx = 0; idx <= 0x19; idx++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | if (idx == AK4531_RESET || idx == AK4531_CLOCK) |
| 398 | continue; |
| 399 | ak4531->write(ak4531, idx, ak4531->regs[idx] = snd_ak4531_initial_map[idx]); /* recording source is mixer */ |
| 400 | } |
| 401 | for (idx = 0; idx < ARRAY_SIZE(snd_ak4531_controls); idx++) { |
Takashi Iwai | dadfb98 | 2021-06-08 16:04:50 +0200 | [diff] [blame] | 402 | err = snd_ctl_add(card, snd_ctl_new1(&snd_ak4531_controls[idx], ak4531)); |
| 403 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | snd_ak4531_free(ak4531); |
| 405 | return err; |
| 406 | } |
| 407 | } |
| 408 | snd_ak4531_proc_init(card, ak4531); |
Takashi Iwai | dadfb98 | 2021-06-08 16:04:50 +0200 | [diff] [blame] | 409 | err = snd_device_new(card, SNDRV_DEV_CODEC, ak4531, &ops); |
| 410 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | snd_ak4531_free(ak4531); |
| 412 | return err; |
| 413 | } |
| 414 | |
| 415 | #if 0 |
| 416 | snd_ak4531_dump(ak4531); |
| 417 | #endif |
Takashi Iwai | da3cec3 | 2008-08-08 17:12:14 +0200 | [diff] [blame] | 418 | if (rak4531) |
| 419 | *rak4531 = ak4531; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | /* |
Takashi Iwai | 11d3824 | 2005-11-17 16:13:05 +0100 | [diff] [blame] | 424 | * power management |
| 425 | */ |
| 426 | #ifdef CONFIG_PM |
| 427 | void snd_ak4531_suspend(struct snd_ak4531 *ak4531) |
| 428 | { |
| 429 | /* mute */ |
| 430 | ak4531->write(ak4531, AK4531_LMASTER, 0x9f); |
| 431 | ak4531->write(ak4531, AK4531_RMASTER, 0x9f); |
| 432 | /* powerdown */ |
| 433 | ak4531->write(ak4531, AK4531_RESET, 0x01); |
| 434 | } |
| 435 | |
| 436 | void snd_ak4531_resume(struct snd_ak4531 *ak4531) |
| 437 | { |
| 438 | int idx; |
| 439 | |
| 440 | /* initialize */ |
| 441 | ak4531->write(ak4531, AK4531_RESET, 0x03); |
| 442 | udelay(100); |
| 443 | ak4531->write(ak4531, AK4531_CLOCK, 0x00); |
| 444 | /* restore mixer registers */ |
| 445 | for (idx = 0; idx <= 0x19; idx++) { |
| 446 | if (idx == AK4531_RESET || idx == AK4531_CLOCK) |
| 447 | continue; |
| 448 | ak4531->write(ak4531, idx, ak4531->regs[idx]); |
| 449 | } |
| 450 | } |
| 451 | #endif |
| 452 | |
| 453 | /* |
Takashi Iwai | adf1b3d | 2005-12-01 10:49:58 +0100 | [diff] [blame] | 454 | * /proc interface |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | */ |
| 456 | |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 457 | static void snd_ak4531_proc_read(struct snd_info_entry *entry, |
| 458 | struct snd_info_buffer *buffer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | { |
Takashi Iwai | 9f38945 | 2005-11-17 14:44:47 +0100 | [diff] [blame] | 460 | struct snd_ak4531 *ak4531 = entry->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | |
| 462 | snd_iprintf(buffer, "Asahi Kasei AK4531\n\n"); |
| 463 | snd_iprintf(buffer, "Recording source : %s\n" |
| 464 | "MIC gain : %s\n", |
| 465 | ak4531->regs[AK4531_AD_IN] & 1 ? "external" : "mixer", |
| 466 | ak4531->regs[AK4531_MIC_GAIN] & 1 ? "+30dB" : "+0dB"); |
| 467 | } |
| 468 | |
Bill Pemberton | e23e7a1 | 2012-12-06 12:35:10 -0500 | [diff] [blame] | 469 | static void |
Takashi Iwai | 23ce154 | 2008-05-30 09:22:22 +0200 | [diff] [blame] | 470 | snd_ak4531_proc_init(struct snd_card *card, struct snd_ak4531 *ak4531) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
Takashi Iwai | 47f2769 | 2019-02-04 16:01:39 +0100 | [diff] [blame] | 472 | snd_card_ro_proc_new(card, "ak4531", ak4531, snd_ak4531_proc_read); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |