Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Routines for control of the AK4114 via I2C and 4-wire serial interface |
| 3 | * IEC958 (S/PDIF) receiver by Asahi Kasei |
| 4 | * Copyright (c) by Jaroslav Kysela <perex@suse.cz> |
| 5 | * |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <sound/driver.h> |
| 24 | #include <linux/slab.h> |
| 25 | #include <linux/delay.h> |
| 26 | #include <sound/core.h> |
| 27 | #include <sound/control.h> |
| 28 | #include <sound/pcm.h> |
| 29 | #include <sound/ak4114.h> |
| 30 | #include <sound/asoundef.h> |
| 31 | |
| 32 | MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>"); |
| 33 | MODULE_DESCRIPTION("AK4114 IEC958 (S/PDIF) receiver by Asahi Kasei"); |
| 34 | MODULE_LICENSE("GPL"); |
| 35 | |
| 36 | #define AK4114_ADDR 0x00 /* fixed address */ |
| 37 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 38 | static void ak4114_stats(struct work_struct *work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 40 | static void reg_write(struct ak4114 *ak4114, unsigned char reg, unsigned char val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | ak4114->write(ak4114->private_data, reg, val); |
| 43 | if (reg <= AK4114_REG_INT1_MASK) |
| 44 | ak4114->regmap[reg] = val; |
| 45 | else if (reg >= AK4114_REG_RXCSB0 && reg <= AK4114_REG_TXCSB4) |
| 46 | ak4114->txcsb[reg-AK4114_REG_RXCSB0] = val; |
| 47 | } |
| 48 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 49 | static inline unsigned char reg_read(struct ak4114 *ak4114, unsigned char reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
| 51 | return ak4114->read(ak4114->private_data, reg); |
| 52 | } |
| 53 | |
| 54 | #if 0 |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 55 | static void reg_dump(struct ak4114 *ak4114) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { |
| 57 | int i; |
| 58 | |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 59 | printk(KERN_DEBUG "AK4114 REG DUMP:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | for (i = 0; i < 0x20; i++) |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 61 | printk(KERN_DEBUG "reg[%02x] = %02x (%02x)\n", i, reg_read(ak4114, i), i < sizeof(ak4114->regmap) ? ak4114->regmap[i] : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | } |
| 63 | #endif |
| 64 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 65 | static void snd_ak4114_free(struct ak4114 *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
| 67 | chip->init = 1; /* don't schedule new work */ |
| 68 | mb(); |
Takashi Iwai | 4014c38 | 2006-12-19 17:13:16 +0100 | [diff] [blame^] | 69 | flush_scheduled_work(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | kfree(chip); |
| 71 | } |
| 72 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 73 | static int snd_ak4114_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 75 | struct ak4114 *chip = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | snd_ak4114_free(chip); |
| 77 | return 0; |
| 78 | } |
| 79 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 80 | int snd_ak4114_create(struct snd_card *card, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | ak4114_read_t *read, ak4114_write_t *write, |
| 82 | unsigned char pgm[7], unsigned char txcsb[5], |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 83 | void *private_data, struct ak4114 **r_ak4114) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 85 | struct ak4114 *chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | int err = 0; |
| 87 | unsigned char reg; |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 88 | static struct snd_device_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | .dev_free = snd_ak4114_dev_free, |
| 90 | }; |
| 91 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 92 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | if (chip == NULL) |
| 94 | return -ENOMEM; |
| 95 | spin_lock_init(&chip->lock); |
| 96 | chip->card = card; |
| 97 | chip->read = read; |
| 98 | chip->write = write; |
| 99 | chip->private_data = private_data; |
| 100 | |
| 101 | for (reg = 0; reg < 7; reg++) |
| 102 | chip->regmap[reg] = pgm[reg]; |
| 103 | for (reg = 0; reg < 5; reg++) |
| 104 | chip->txcsb[reg] = txcsb[reg]; |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | snd_ak4114_reinit(chip); |
| 107 | |
| 108 | chip->rcs0 = reg_read(chip, AK4114_REG_RCS0) & ~(AK4114_QINT | AK4114_CINT); |
| 109 | chip->rcs1 = reg_read(chip, AK4114_REG_RCS1); |
| 110 | |
| 111 | if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0) |
| 112 | goto __fail; |
| 113 | |
| 114 | if (r_ak4114) |
| 115 | *r_ak4114 = chip; |
| 116 | return 0; |
| 117 | |
| 118 | __fail: |
| 119 | snd_ak4114_free(chip); |
| 120 | return err < 0 ? err : -EIO; |
| 121 | } |
| 122 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 123 | void snd_ak4114_reg_write(struct ak4114 *chip, unsigned char reg, unsigned char mask, unsigned char val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | if (reg <= AK4114_REG_INT1_MASK) |
| 126 | reg_write(chip, reg, (chip->regmap[reg] & ~mask) | val); |
| 127 | else if (reg >= AK4114_REG_TXCSB0 && reg <= AK4114_REG_TXCSB4) |
| 128 | reg_write(chip, reg, (chip->txcsb[reg] & ~mask) | val); |
| 129 | } |
| 130 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 131 | void snd_ak4114_reinit(struct ak4114 *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
| 133 | unsigned char old = chip->regmap[AK4114_REG_PWRDN], reg; |
| 134 | |
| 135 | chip->init = 1; |
| 136 | mb(); |
Takashi Iwai | 4014c38 | 2006-12-19 17:13:16 +0100 | [diff] [blame^] | 137 | flush_scheduled_work(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | /* bring the chip to reset state and powerdown state */ |
| 139 | reg_write(chip, AK4114_REG_PWRDN, old & ~(AK4114_RST|AK4114_PWN)); |
| 140 | udelay(200); |
| 141 | /* release reset, but leave powerdown */ |
| 142 | reg_write(chip, AK4114_REG_PWRDN, (old | AK4114_RST) & ~AK4114_PWN); |
| 143 | udelay(200); |
| 144 | for (reg = 1; reg < 7; reg++) |
| 145 | reg_write(chip, reg, chip->regmap[reg]); |
| 146 | for (reg = 0; reg < 5; reg++) |
| 147 | reg_write(chip, reg + AK4114_REG_TXCSB0, chip->txcsb[reg]); |
| 148 | /* release powerdown, everything is initialized now */ |
| 149 | reg_write(chip, AK4114_REG_PWRDN, old | AK4114_RST | AK4114_PWN); |
| 150 | /* bring up statistics / event queing */ |
| 151 | chip->init = 0; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 152 | INIT_DELAYED_WORK(&chip->work, ak4114_stats); |
Takashi Iwai | 4014c38 | 2006-12-19 17:13:16 +0100 | [diff] [blame^] | 153 | schedule_delayed_work(&chip->work, HZ / 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static unsigned int external_rate(unsigned char rcs1) |
| 157 | { |
| 158 | switch (rcs1 & (AK4114_FS0|AK4114_FS1|AK4114_FS2|AK4114_FS3)) { |
| 159 | case AK4114_FS_32000HZ: return 32000; |
| 160 | case AK4114_FS_44100HZ: return 44100; |
| 161 | case AK4114_FS_48000HZ: return 48000; |
| 162 | case AK4114_FS_88200HZ: return 88200; |
| 163 | case AK4114_FS_96000HZ: return 96000; |
| 164 | case AK4114_FS_176400HZ: return 176400; |
| 165 | case AK4114_FS_192000HZ: return 192000; |
| 166 | default: return 0; |
| 167 | } |
| 168 | } |
| 169 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 170 | static int snd_ak4114_in_error_info(struct snd_kcontrol *kcontrol, |
| 171 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | { |
| 173 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 174 | uinfo->count = 1; |
| 175 | uinfo->value.integer.min = 0; |
| 176 | uinfo->value.integer.max = LONG_MAX; |
| 177 | return 0; |
| 178 | } |
| 179 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 180 | static int snd_ak4114_in_error_get(struct snd_kcontrol *kcontrol, |
| 181 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 183 | struct ak4114 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | long *ptr; |
| 185 | |
| 186 | spin_lock_irq(&chip->lock); |
| 187 | ptr = (long *)(((char *)chip) + kcontrol->private_value); |
| 188 | ucontrol->value.integer.value[0] = *ptr; |
| 189 | *ptr = 0; |
| 190 | spin_unlock_irq(&chip->lock); |
| 191 | return 0; |
| 192 | } |
| 193 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 194 | static int snd_ak4114_in_bit_info(struct snd_kcontrol *kcontrol, |
| 195 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
| 197 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; |
| 198 | uinfo->count = 1; |
| 199 | uinfo->value.integer.min = 0; |
| 200 | uinfo->value.integer.max = 1; |
| 201 | return 0; |
| 202 | } |
| 203 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 204 | static int snd_ak4114_in_bit_get(struct snd_kcontrol *kcontrol, |
| 205 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 207 | struct ak4114 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | unsigned char reg = kcontrol->private_value & 0xff; |
| 209 | unsigned char bit = (kcontrol->private_value >> 8) & 0xff; |
| 210 | unsigned char inv = (kcontrol->private_value >> 31) & 1; |
| 211 | |
| 212 | ucontrol->value.integer.value[0] = ((reg_read(chip, reg) & (1 << bit)) ? 1 : 0) ^ inv; |
| 213 | return 0; |
| 214 | } |
| 215 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 216 | static int snd_ak4114_rate_info(struct snd_kcontrol *kcontrol, |
| 217 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
| 219 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 220 | uinfo->count = 1; |
| 221 | uinfo->value.integer.min = 0; |
| 222 | uinfo->value.integer.max = 192000; |
| 223 | return 0; |
| 224 | } |
| 225 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 226 | static int snd_ak4114_rate_get(struct snd_kcontrol *kcontrol, |
| 227 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 229 | struct ak4114 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
| 231 | ucontrol->value.integer.value[0] = external_rate(reg_read(chip, AK4114_REG_RCS1)); |
| 232 | return 0; |
| 233 | } |
| 234 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 235 | static int snd_ak4114_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
| 237 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 238 | uinfo->count = 1; |
| 239 | return 0; |
| 240 | } |
| 241 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 242 | static int snd_ak4114_spdif_get(struct snd_kcontrol *kcontrol, |
| 243 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 245 | struct ak4114 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | unsigned i; |
| 247 | |
| 248 | for (i = 0; i < AK4114_REG_RXCSB_SIZE; i++) |
| 249 | ucontrol->value.iec958.status[i] = reg_read(chip, AK4114_REG_RXCSB0 + i); |
| 250 | return 0; |
| 251 | } |
| 252 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 253 | static int snd_ak4114_spdif_playback_get(struct snd_kcontrol *kcontrol, |
| 254 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 256 | struct ak4114 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | unsigned i; |
| 258 | |
| 259 | for (i = 0; i < AK4114_REG_TXCSB_SIZE; i++) |
| 260 | ucontrol->value.iec958.status[i] = chip->txcsb[i]; |
| 261 | return 0; |
| 262 | } |
| 263 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 264 | static int snd_ak4114_spdif_playback_put(struct snd_kcontrol *kcontrol, |
| 265 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 267 | struct ak4114 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | unsigned i; |
| 269 | |
| 270 | for (i = 0; i < AK4114_REG_TXCSB_SIZE; i++) |
| 271 | reg_write(chip, AK4114_REG_TXCSB0 + i, ucontrol->value.iec958.status[i]); |
| 272 | return 0; |
| 273 | } |
| 274 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 275 | static int snd_ak4114_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
| 277 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 278 | uinfo->count = 1; |
| 279 | return 0; |
| 280 | } |
| 281 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 282 | static int snd_ak4114_spdif_mask_get(struct snd_kcontrol *kcontrol, |
| 283 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | { |
| 285 | memset(ucontrol->value.iec958.status, 0xff, AK4114_REG_RXCSB_SIZE); |
| 286 | return 0; |
| 287 | } |
| 288 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 289 | static int snd_ak4114_spdif_pinfo(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
| 291 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 292 | uinfo->value.integer.min = 0; |
| 293 | uinfo->value.integer.max = 0xffff; |
| 294 | uinfo->count = 4; |
| 295 | return 0; |
| 296 | } |
| 297 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 298 | static int snd_ak4114_spdif_pget(struct snd_kcontrol *kcontrol, |
| 299 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 301 | struct ak4114 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | unsigned short tmp; |
| 303 | |
| 304 | ucontrol->value.integer.value[0] = 0xf8f2; |
| 305 | ucontrol->value.integer.value[1] = 0x4e1f; |
| 306 | tmp = reg_read(chip, AK4114_REG_Pc0) | (reg_read(chip, AK4114_REG_Pc1) << 8); |
| 307 | ucontrol->value.integer.value[2] = tmp; |
| 308 | tmp = reg_read(chip, AK4114_REG_Pd0) | (reg_read(chip, AK4114_REG_Pd1) << 8); |
| 309 | ucontrol->value.integer.value[3] = tmp; |
| 310 | return 0; |
| 311 | } |
| 312 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 313 | static int snd_ak4114_spdif_qinfo(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
| 315 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; |
| 316 | uinfo->count = AK4114_REG_QSUB_SIZE; |
| 317 | return 0; |
| 318 | } |
| 319 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 320 | static int snd_ak4114_spdif_qget(struct snd_kcontrol *kcontrol, |
| 321 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 323 | struct ak4114 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | unsigned i; |
| 325 | |
| 326 | for (i = 0; i < AK4114_REG_QSUB_SIZE; i++) |
| 327 | ucontrol->value.bytes.data[i] = reg_read(chip, AK4114_REG_QSUB_ADDR + i); |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | /* Don't forget to change AK4114_CONTROLS define!!! */ |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 332 | static struct snd_kcontrol_new snd_ak4114_iec958_controls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | { |
| 334 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 335 | .name = "IEC958 Parity Errors", |
| 336 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 337 | .info = snd_ak4114_in_error_info, |
| 338 | .get = snd_ak4114_in_error_get, |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 339 | .private_value = offsetof(struct ak4114, parity_errors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | }, |
| 341 | { |
| 342 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 343 | .name = "IEC958 V-Bit Errors", |
| 344 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 345 | .info = snd_ak4114_in_error_info, |
| 346 | .get = snd_ak4114_in_error_get, |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 347 | .private_value = offsetof(struct ak4114, v_bit_errors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | }, |
| 349 | { |
| 350 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 351 | .name = "IEC958 C-CRC Errors", |
| 352 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 353 | .info = snd_ak4114_in_error_info, |
| 354 | .get = snd_ak4114_in_error_get, |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 355 | .private_value = offsetof(struct ak4114, ccrc_errors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | }, |
| 357 | { |
| 358 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 359 | .name = "IEC958 Q-CRC Errors", |
| 360 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 361 | .info = snd_ak4114_in_error_info, |
| 362 | .get = snd_ak4114_in_error_get, |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 363 | .private_value = offsetof(struct ak4114, qcrc_errors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | }, |
| 365 | { |
| 366 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 367 | .name = "IEC958 External Rate", |
| 368 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 369 | .info = snd_ak4114_rate_info, |
| 370 | .get = snd_ak4114_rate_get, |
| 371 | }, |
| 372 | { |
| 373 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 374 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK), |
| 375 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 376 | .info = snd_ak4114_spdif_mask_info, |
| 377 | .get = snd_ak4114_spdif_mask_get, |
| 378 | }, |
| 379 | { |
| 380 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 381 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT), |
| 382 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 383 | .info = snd_ak4114_spdif_info, |
| 384 | .get = snd_ak4114_spdif_playback_get, |
| 385 | .put = snd_ak4114_spdif_playback_put, |
| 386 | }, |
| 387 | { |
| 388 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 389 | .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,MASK), |
| 390 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 391 | .info = snd_ak4114_spdif_mask_info, |
| 392 | .get = snd_ak4114_spdif_mask_get, |
| 393 | }, |
| 394 | { |
| 395 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 396 | .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT), |
| 397 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 398 | .info = snd_ak4114_spdif_info, |
| 399 | .get = snd_ak4114_spdif_get, |
| 400 | }, |
| 401 | { |
| 402 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 403 | .name = "IEC958 Preample Capture Default", |
| 404 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 405 | .info = snd_ak4114_spdif_pinfo, |
| 406 | .get = snd_ak4114_spdif_pget, |
| 407 | }, |
| 408 | { |
| 409 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 410 | .name = "IEC958 Q-subcode Capture Default", |
| 411 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 412 | .info = snd_ak4114_spdif_qinfo, |
| 413 | .get = snd_ak4114_spdif_qget, |
| 414 | }, |
| 415 | { |
| 416 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 417 | .name = "IEC958 Audio", |
| 418 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 419 | .info = snd_ak4114_in_bit_info, |
| 420 | .get = snd_ak4114_in_bit_get, |
| 421 | .private_value = (1<<31) | (1<<8) | AK4114_REG_RCS0, |
| 422 | }, |
| 423 | { |
| 424 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 425 | .name = "IEC958 Non-PCM Bitstream", |
| 426 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 427 | .info = snd_ak4114_in_bit_info, |
| 428 | .get = snd_ak4114_in_bit_get, |
| 429 | .private_value = (6<<8) | AK4114_REG_RCS1, |
| 430 | }, |
| 431 | { |
| 432 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 433 | .name = "IEC958 DTS Bitstream", |
| 434 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 435 | .info = snd_ak4114_in_bit_info, |
| 436 | .get = snd_ak4114_in_bit_get, |
| 437 | .private_value = (3<<8) | AK4114_REG_RCS1, |
| 438 | } |
| 439 | }; |
| 440 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 441 | int snd_ak4114_build(struct ak4114 *ak4114, |
| 442 | struct snd_pcm_substream *ply_substream, |
| 443 | struct snd_pcm_substream *cap_substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 445 | struct snd_kcontrol *kctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | unsigned int idx; |
| 447 | int err; |
| 448 | |
| 449 | snd_assert(cap_substream, return -EINVAL); |
| 450 | ak4114->playback_substream = ply_substream; |
| 451 | ak4114->capture_substream = cap_substream; |
| 452 | for (idx = 0; idx < AK4114_CONTROLS; idx++) { |
| 453 | kctl = snd_ctl_new1(&snd_ak4114_iec958_controls[idx], ak4114); |
| 454 | if (kctl == NULL) |
| 455 | return -ENOMEM; |
| 456 | if (!strstr(kctl->id.name, "Playback")) { |
| 457 | if (ply_substream == NULL) { |
| 458 | snd_ctl_free_one(kctl); |
| 459 | ak4114->kctls[idx] = NULL; |
| 460 | continue; |
| 461 | } |
| 462 | kctl->id.device = ply_substream->pcm->device; |
| 463 | kctl->id.subdevice = ply_substream->number; |
| 464 | } else { |
| 465 | kctl->id.device = cap_substream->pcm->device; |
| 466 | kctl->id.subdevice = cap_substream->number; |
| 467 | } |
| 468 | err = snd_ctl_add(ak4114->card, kctl); |
| 469 | if (err < 0) |
| 470 | return err; |
| 471 | ak4114->kctls[idx] = kctl; |
| 472 | } |
| 473 | return 0; |
| 474 | } |
| 475 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 476 | int snd_ak4114_external_rate(struct ak4114 *ak4114) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | { |
| 478 | unsigned char rcs1; |
| 479 | |
| 480 | rcs1 = reg_read(ak4114, AK4114_REG_RCS1); |
| 481 | return external_rate(rcs1); |
| 482 | } |
| 483 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 484 | int snd_ak4114_check_rate_and_errors(struct ak4114 *ak4114, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 486 | struct snd_pcm_runtime *runtime = ak4114->capture_substream ? ak4114->capture_substream->runtime : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | unsigned long _flags; |
| 488 | int res = 0; |
| 489 | unsigned char rcs0, rcs1; |
| 490 | unsigned char c0, c1; |
| 491 | |
| 492 | rcs1 = reg_read(ak4114, AK4114_REG_RCS1); |
| 493 | if (flags & AK4114_CHECK_NO_STAT) |
| 494 | goto __rate; |
| 495 | rcs0 = reg_read(ak4114, AK4114_REG_RCS0); |
| 496 | spin_lock_irqsave(&ak4114->lock, _flags); |
| 497 | if (rcs0 & AK4114_PAR) |
| 498 | ak4114->parity_errors++; |
| 499 | if (rcs1 & AK4114_V) |
| 500 | ak4114->v_bit_errors++; |
| 501 | if (rcs1 & AK4114_CCRC) |
| 502 | ak4114->ccrc_errors++; |
| 503 | if (rcs1 & AK4114_QCRC) |
| 504 | ak4114->qcrc_errors++; |
| 505 | c0 = (ak4114->rcs0 & (AK4114_QINT | AK4114_CINT | AK4114_PEM | AK4114_AUDION | AK4114_AUTO | AK4114_UNLCK)) ^ |
| 506 | (rcs0 & (AK4114_QINT | AK4114_CINT | AK4114_PEM | AK4114_AUDION | AK4114_AUTO | AK4114_UNLCK)); |
| 507 | c1 = (ak4114->rcs1 & 0xf0) ^ (rcs1 & 0xf0); |
| 508 | ak4114->rcs0 = rcs0 & ~(AK4114_QINT | AK4114_CINT); |
| 509 | ak4114->rcs1 = rcs1; |
| 510 | spin_unlock_irqrestore(&ak4114->lock, _flags); |
| 511 | |
| 512 | if (rcs0 & AK4114_PAR) |
| 513 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[0]->id); |
| 514 | if (rcs0 & AK4114_V) |
| 515 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[1]->id); |
| 516 | if (rcs1 & AK4114_CCRC) |
| 517 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[2]->id); |
| 518 | if (rcs1 & AK4114_QCRC) |
| 519 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[3]->id); |
| 520 | |
| 521 | /* rate change */ |
| 522 | if (c1 & 0xf0) |
| 523 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[4]->id); |
| 524 | |
| 525 | if ((c0 & AK4114_PEM) | (c0 & AK4114_CINT)) |
| 526 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[9]->id); |
| 527 | if (c0 & AK4114_QINT) |
| 528 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[10]->id); |
| 529 | |
| 530 | if (c0 & AK4114_AUDION) |
| 531 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[11]->id); |
| 532 | if (c0 & AK4114_AUTO) |
| 533 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[12]->id); |
| 534 | if (c0 & AK4114_DTSCD) |
| 535 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[13]->id); |
| 536 | |
| 537 | if (ak4114->change_callback && (c0 | c1) != 0) |
| 538 | ak4114->change_callback(ak4114, c0, c1); |
| 539 | |
| 540 | __rate: |
| 541 | /* compare rate */ |
| 542 | res = external_rate(rcs1); |
| 543 | if (!(flags & AK4114_CHECK_NO_RATE) && runtime && runtime->rate != res) { |
| 544 | snd_pcm_stream_lock_irqsave(ak4114->capture_substream, _flags); |
| 545 | if (snd_pcm_running(ak4114->capture_substream)) { |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 546 | // printk(KERN_DEBUG "rate changed (%i <- %i)\n", runtime->rate, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | snd_pcm_stop(ak4114->capture_substream, SNDRV_PCM_STATE_DRAINING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | res = 1; |
| 549 | } |
| 550 | snd_pcm_stream_unlock_irqrestore(ak4114->capture_substream, _flags); |
| 551 | } |
| 552 | return res; |
| 553 | } |
| 554 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 555 | static void ak4114_stats(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 557 | struct ak4114 *chip = container_of(work, struct ak4114, work.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
| 559 | if (chip->init) |
| 560 | return; |
| 561 | snd_ak4114_check_rate_and_errors(chip, 0); |
Takashi Iwai | 4014c38 | 2006-12-19 17:13:16 +0100 | [diff] [blame^] | 562 | schedule_delayed_work(&chip->work, HZ / 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | EXPORT_SYMBOL(snd_ak4114_create); |
| 566 | EXPORT_SYMBOL(snd_ak4114_reg_write); |
| 567 | EXPORT_SYMBOL(snd_ak4114_reinit); |
| 568 | EXPORT_SYMBOL(snd_ak4114_build); |
| 569 | EXPORT_SYMBOL(snd_ak4114_external_rate); |
| 570 | EXPORT_SYMBOL(snd_ak4114_check_rate_and_errors); |