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 |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 4 | * Copyright (c) by Jaroslav Kysela <perex@perex.cz> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/slab.h> |
| 24 | #include <linux/delay.h> |
Paul Gortmaker | da155d5 | 2011-07-15 12:38:28 -0400 | [diff] [blame] | 25 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Pavel Hofman | fdafad6 | 2008-02-11 14:48:06 +0100 | [diff] [blame] | 31 | #include <sound/info.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 33 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | MODULE_DESCRIPTION("AK4114 IEC958 (S/PDIF) receiver by Asahi Kasei"); |
| 35 | MODULE_LICENSE("GPL"); |
| 36 | |
| 37 | #define AK4114_ADDR 0x00 /* fixed address */ |
| 38 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 39 | static void ak4114_stats(struct work_struct *work); |
Takashi Iwai | 51354ae | 2007-03-30 15:38:39 +0200 | [diff] [blame] | 40 | static void ak4114_init_regs(struct ak4114 *chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 42 | 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] | 43 | { |
| 44 | ak4114->write(ak4114->private_data, reg, val); |
| 45 | if (reg <= AK4114_REG_INT1_MASK) |
| 46 | ak4114->regmap[reg] = val; |
Jean Delvare | 1ab774e | 2007-02-05 13:07:04 +0100 | [diff] [blame] | 47 | else if (reg >= AK4114_REG_TXCSB0 && reg <= AK4114_REG_TXCSB4) |
| 48 | ak4114->txcsb[reg-AK4114_REG_TXCSB0] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 51 | static inline unsigned char reg_read(struct ak4114 *ak4114, unsigned char reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | { |
| 53 | return ak4114->read(ak4114->private_data, reg); |
| 54 | } |
| 55 | |
| 56 | #if 0 |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 57 | static void reg_dump(struct ak4114 *ak4114) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
| 59 | int i; |
| 60 | |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 61 | printk(KERN_DEBUG "AK4114 REG DUMP:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | for (i = 0; i < 0x20; i++) |
Takashi Iwai | e12483e | 2013-10-29 16:37:11 +0100 | [diff] [blame] | 63 | printk(KERN_DEBUG "reg[%02x] = %02x (%02x)\n", i, reg_read(ak4114, i), i < ARRAY_SIZE(ak4114->regmap) ? ak4114->regmap[i] : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
| 65 | #endif |
| 66 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 67 | static void snd_ak4114_free(struct ak4114 *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
Takashi Iwai | 4161b45 | 2015-01-13 10:53:20 +0100 | [diff] [blame] | 69 | atomic_inc(&chip->wq_processing); /* don't schedule new work */ |
Tejun Heo | 5b84ba2 | 2010-12-11 17:51:26 +0100 | [diff] [blame] | 70 | cancel_delayed_work_sync(&chip->work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | kfree(chip); |
| 72 | } |
| 73 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 74 | static int snd_ak4114_dev_free(struct snd_device *device) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 76 | struct ak4114 *chip = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | snd_ak4114_free(chip); |
| 78 | return 0; |
| 79 | } |
| 80 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 81 | int snd_ak4114_create(struct snd_card *card, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | ak4114_read_t *read, ak4114_write_t *write, |
Takashi Iwai | e12483e | 2013-10-29 16:37:11 +0100 | [diff] [blame] | 83 | const unsigned char pgm[6], const unsigned char txcsb[5], |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 84 | void *private_data, struct ak4114 **r_ak4114) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 86 | struct ak4114 *chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | int err = 0; |
| 88 | unsigned char reg; |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 89 | static struct snd_device_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | .dev_free = snd_ak4114_dev_free, |
| 91 | }; |
| 92 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 93 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | if (chip == NULL) |
| 95 | return -ENOMEM; |
| 96 | spin_lock_init(&chip->lock); |
| 97 | chip->card = card; |
| 98 | chip->read = read; |
| 99 | chip->write = write; |
| 100 | chip->private_data = private_data; |
Takashi Iwai | 3b6baa5 | 2007-01-31 14:34:38 +0100 | [diff] [blame] | 101 | INIT_DELAYED_WORK(&chip->work, ak4114_stats); |
Takashi Iwai | 4161b45 | 2015-01-13 10:53:20 +0100 | [diff] [blame] | 102 | atomic_set(&chip->wq_processing, 0); |
Takashi Iwai | 1781e78 | 2015-01-16 13:03:28 +0100 | [diff] [blame] | 103 | mutex_init(&chip->reinit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
Takashi Iwai | e12483e | 2013-10-29 16:37:11 +0100 | [diff] [blame] | 105 | for (reg = 0; reg < 6; reg++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | chip->regmap[reg] = pgm[reg]; |
| 107 | for (reg = 0; reg < 5; reg++) |
| 108 | chip->txcsb[reg] = txcsb[reg]; |
| 109 | |
Takashi Iwai | 51354ae | 2007-03-30 15:38:39 +0200 | [diff] [blame] | 110 | ak4114_init_regs(chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | chip->rcs0 = reg_read(chip, AK4114_REG_RCS0) & ~(AK4114_QINT | AK4114_CINT); |
| 113 | chip->rcs1 = reg_read(chip, AK4114_REG_RCS1); |
| 114 | |
Takashi Iwai | 483eb06 | 2014-02-04 11:18:23 +0100 | [diff] [blame] | 115 | if ((err = snd_device_new(card, SNDRV_DEV_CODEC, chip, &ops)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | goto __fail; |
| 117 | |
| 118 | if (r_ak4114) |
| 119 | *r_ak4114 = chip; |
| 120 | return 0; |
| 121 | |
| 122 | __fail: |
| 123 | snd_ak4114_free(chip); |
Colin Ian King | 5137d6d | 2016-07-12 11:26:29 +0100 | [diff] [blame] | 124 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
Takashi Iwai | a850ef8 | 2015-01-13 10:58:31 +0100 | [diff] [blame] | 126 | EXPORT_SYMBOL(snd_ak4114_create); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 128 | 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] | 129 | { |
| 130 | if (reg <= AK4114_REG_INT1_MASK) |
| 131 | reg_write(chip, reg, (chip->regmap[reg] & ~mask) | val); |
| 132 | else if (reg >= AK4114_REG_TXCSB0 && reg <= AK4114_REG_TXCSB4) |
Jean Delvare | 1ab774e | 2007-02-05 13:07:04 +0100 | [diff] [blame] | 133 | reg_write(chip, reg, |
| 134 | (chip->txcsb[reg-AK4114_REG_TXCSB0] & ~mask) | val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
Takashi Iwai | a850ef8 | 2015-01-13 10:58:31 +0100 | [diff] [blame] | 136 | EXPORT_SYMBOL(snd_ak4114_reg_write); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Takashi Iwai | 51354ae | 2007-03-30 15:38:39 +0200 | [diff] [blame] | 138 | static void ak4114_init_regs(struct ak4114 *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | { |
| 140 | unsigned char old = chip->regmap[AK4114_REG_PWRDN], reg; |
| 141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | /* bring the chip to reset state and powerdown state */ |
| 143 | reg_write(chip, AK4114_REG_PWRDN, old & ~(AK4114_RST|AK4114_PWN)); |
| 144 | udelay(200); |
| 145 | /* release reset, but leave powerdown */ |
| 146 | reg_write(chip, AK4114_REG_PWRDN, (old | AK4114_RST) & ~AK4114_PWN); |
| 147 | udelay(200); |
Takashi Iwai | e12483e | 2013-10-29 16:37:11 +0100 | [diff] [blame] | 148 | for (reg = 1; reg < 6; reg++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | reg_write(chip, reg, chip->regmap[reg]); |
| 150 | for (reg = 0; reg < 5; reg++) |
| 151 | reg_write(chip, reg + AK4114_REG_TXCSB0, chip->txcsb[reg]); |
| 152 | /* release powerdown, everything is initialized now */ |
| 153 | reg_write(chip, AK4114_REG_PWRDN, old | AK4114_RST | AK4114_PWN); |
Takashi Iwai | 51354ae | 2007-03-30 15:38:39 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void snd_ak4114_reinit(struct ak4114 *chip) |
| 157 | { |
Takashi Iwai | 4161b45 | 2015-01-13 10:53:20 +0100 | [diff] [blame] | 158 | if (atomic_inc_return(&chip->wq_processing) == 1) |
| 159 | cancel_delayed_work_sync(&chip->work); |
Takashi Iwai | 1781e78 | 2015-01-16 13:03:28 +0100 | [diff] [blame] | 160 | mutex_lock(&chip->reinit_mutex); |
Takashi Iwai | 51354ae | 2007-03-30 15:38:39 +0200 | [diff] [blame] | 161 | ak4114_init_regs(chip); |
Takashi Iwai | 1781e78 | 2015-01-16 13:03:28 +0100 | [diff] [blame] | 162 | mutex_unlock(&chip->reinit_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | /* bring up statistics / event queing */ |
Takashi Iwai | 4161b45 | 2015-01-13 10:53:20 +0100 | [diff] [blame] | 164 | if (atomic_dec_and_test(&chip->wq_processing)) |
Takashi Iwai | 51354ae | 2007-03-30 15:38:39 +0200 | [diff] [blame] | 165 | schedule_delayed_work(&chip->work, HZ / 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
Takashi Iwai | a850ef8 | 2015-01-13 10:58:31 +0100 | [diff] [blame] | 167 | EXPORT_SYMBOL(snd_ak4114_reinit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
| 169 | static unsigned int external_rate(unsigned char rcs1) |
| 170 | { |
| 171 | switch (rcs1 & (AK4114_FS0|AK4114_FS1|AK4114_FS2|AK4114_FS3)) { |
| 172 | case AK4114_FS_32000HZ: return 32000; |
| 173 | case AK4114_FS_44100HZ: return 44100; |
| 174 | case AK4114_FS_48000HZ: return 48000; |
| 175 | case AK4114_FS_88200HZ: return 88200; |
| 176 | case AK4114_FS_96000HZ: return 96000; |
| 177 | case AK4114_FS_176400HZ: return 176400; |
| 178 | case AK4114_FS_192000HZ: return 192000; |
| 179 | default: return 0; |
| 180 | } |
| 181 | } |
| 182 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 183 | static int snd_ak4114_in_error_info(struct snd_kcontrol *kcontrol, |
| 184 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
| 186 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 187 | uinfo->count = 1; |
| 188 | uinfo->value.integer.min = 0; |
| 189 | uinfo->value.integer.max = LONG_MAX; |
| 190 | return 0; |
| 191 | } |
| 192 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 193 | static int snd_ak4114_in_error_get(struct snd_kcontrol *kcontrol, |
| 194 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 196 | struct ak4114 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | long *ptr; |
| 198 | |
| 199 | spin_lock_irq(&chip->lock); |
| 200 | ptr = (long *)(((char *)chip) + kcontrol->private_value); |
| 201 | ucontrol->value.integer.value[0] = *ptr; |
| 202 | *ptr = 0; |
| 203 | spin_unlock_irq(&chip->lock); |
| 204 | return 0; |
| 205 | } |
| 206 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 207 | #define snd_ak4114_in_bit_info snd_ctl_boolean_mono_info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 209 | static int snd_ak4114_in_bit_get(struct snd_kcontrol *kcontrol, |
| 210 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 212 | struct ak4114 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | unsigned char reg = kcontrol->private_value & 0xff; |
| 214 | unsigned char bit = (kcontrol->private_value >> 8) & 0xff; |
| 215 | unsigned char inv = (kcontrol->private_value >> 31) & 1; |
| 216 | |
| 217 | ucontrol->value.integer.value[0] = ((reg_read(chip, reg) & (1 << bit)) ? 1 : 0) ^ inv; |
| 218 | return 0; |
| 219 | } |
| 220 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 221 | static int snd_ak4114_rate_info(struct snd_kcontrol *kcontrol, |
| 222 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
| 224 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 225 | uinfo->count = 1; |
| 226 | uinfo->value.integer.min = 0; |
| 227 | uinfo->value.integer.max = 192000; |
| 228 | return 0; |
| 229 | } |
| 230 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 231 | static int snd_ak4114_rate_get(struct snd_kcontrol *kcontrol, |
| 232 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 234 | struct ak4114 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
| 236 | ucontrol->value.integer.value[0] = external_rate(reg_read(chip, AK4114_REG_RCS1)); |
| 237 | return 0; |
| 238 | } |
| 239 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 240 | 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] | 241 | { |
| 242 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 243 | uinfo->count = 1; |
| 244 | return 0; |
| 245 | } |
| 246 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 247 | static int snd_ak4114_spdif_get(struct snd_kcontrol *kcontrol, |
| 248 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 250 | struct ak4114 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | unsigned i; |
| 252 | |
| 253 | for (i = 0; i < AK4114_REG_RXCSB_SIZE; i++) |
| 254 | ucontrol->value.iec958.status[i] = reg_read(chip, AK4114_REG_RXCSB0 + i); |
| 255 | return 0; |
| 256 | } |
| 257 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 258 | static int snd_ak4114_spdif_playback_get(struct snd_kcontrol *kcontrol, |
| 259 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 261 | struct ak4114 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | unsigned i; |
| 263 | |
| 264 | for (i = 0; i < AK4114_REG_TXCSB_SIZE; i++) |
| 265 | ucontrol->value.iec958.status[i] = chip->txcsb[i]; |
| 266 | return 0; |
| 267 | } |
| 268 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 269 | static int snd_ak4114_spdif_playback_put(struct snd_kcontrol *kcontrol, |
| 270 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 272 | struct ak4114 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | unsigned i; |
| 274 | |
| 275 | for (i = 0; i < AK4114_REG_TXCSB_SIZE; i++) |
| 276 | reg_write(chip, AK4114_REG_TXCSB0 + i, ucontrol->value.iec958.status[i]); |
| 277 | return 0; |
| 278 | } |
| 279 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 280 | 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] | 281 | { |
| 282 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 283 | uinfo->count = 1; |
| 284 | return 0; |
| 285 | } |
| 286 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 287 | static int snd_ak4114_spdif_mask_get(struct snd_kcontrol *kcontrol, |
| 288 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
| 290 | memset(ucontrol->value.iec958.status, 0xff, AK4114_REG_RXCSB_SIZE); |
| 291 | return 0; |
| 292 | } |
| 293 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 294 | 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] | 295 | { |
| 296 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 297 | uinfo->value.integer.min = 0; |
| 298 | uinfo->value.integer.max = 0xffff; |
| 299 | uinfo->count = 4; |
| 300 | return 0; |
| 301 | } |
| 302 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 303 | static int snd_ak4114_spdif_pget(struct snd_kcontrol *kcontrol, |
| 304 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 306 | struct ak4114 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | unsigned short tmp; |
| 308 | |
| 309 | ucontrol->value.integer.value[0] = 0xf8f2; |
| 310 | ucontrol->value.integer.value[1] = 0x4e1f; |
| 311 | tmp = reg_read(chip, AK4114_REG_Pc0) | (reg_read(chip, AK4114_REG_Pc1) << 8); |
| 312 | ucontrol->value.integer.value[2] = tmp; |
| 313 | tmp = reg_read(chip, AK4114_REG_Pd0) | (reg_read(chip, AK4114_REG_Pd1) << 8); |
| 314 | ucontrol->value.integer.value[3] = tmp; |
| 315 | return 0; |
| 316 | } |
| 317 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 318 | 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] | 319 | { |
| 320 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; |
| 321 | uinfo->count = AK4114_REG_QSUB_SIZE; |
| 322 | return 0; |
| 323 | } |
| 324 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 325 | static int snd_ak4114_spdif_qget(struct snd_kcontrol *kcontrol, |
| 326 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 328 | struct ak4114 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | unsigned i; |
| 330 | |
| 331 | for (i = 0; i < AK4114_REG_QSUB_SIZE; i++) |
| 332 | ucontrol->value.bytes.data[i] = reg_read(chip, AK4114_REG_QSUB_ADDR + i); |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | /* Don't forget to change AK4114_CONTROLS define!!! */ |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 337 | static struct snd_kcontrol_new snd_ak4114_iec958_controls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | { |
| 339 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 340 | .name = "IEC958 Parity Errors", |
| 341 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 342 | .info = snd_ak4114_in_error_info, |
| 343 | .get = snd_ak4114_in_error_get, |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 344 | .private_value = offsetof(struct ak4114, parity_errors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | }, |
| 346 | { |
| 347 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 348 | .name = "IEC958 V-Bit Errors", |
| 349 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 350 | .info = snd_ak4114_in_error_info, |
| 351 | .get = snd_ak4114_in_error_get, |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 352 | .private_value = offsetof(struct ak4114, v_bit_errors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | }, |
| 354 | { |
| 355 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 356 | .name = "IEC958 C-CRC Errors", |
| 357 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 358 | .info = snd_ak4114_in_error_info, |
| 359 | .get = snd_ak4114_in_error_get, |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 360 | .private_value = offsetof(struct ak4114, ccrc_errors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | }, |
| 362 | { |
| 363 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 364 | .name = "IEC958 Q-CRC Errors", |
| 365 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 366 | .info = snd_ak4114_in_error_info, |
| 367 | .get = snd_ak4114_in_error_get, |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 368 | .private_value = offsetof(struct ak4114, qcrc_errors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | }, |
| 370 | { |
| 371 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 372 | .name = "IEC958 External Rate", |
| 373 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 374 | .info = snd_ak4114_rate_info, |
| 375 | .get = snd_ak4114_rate_get, |
| 376 | }, |
| 377 | { |
| 378 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 379 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK), |
| 380 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 381 | .info = snd_ak4114_spdif_mask_info, |
| 382 | .get = snd_ak4114_spdif_mask_get, |
| 383 | }, |
| 384 | { |
| 385 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 386 | .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT), |
| 387 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 388 | .info = snd_ak4114_spdif_info, |
| 389 | .get = snd_ak4114_spdif_playback_get, |
| 390 | .put = snd_ak4114_spdif_playback_put, |
| 391 | }, |
| 392 | { |
| 393 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 394 | .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,MASK), |
| 395 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 396 | .info = snd_ak4114_spdif_mask_info, |
| 397 | .get = snd_ak4114_spdif_mask_get, |
| 398 | }, |
| 399 | { |
| 400 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 401 | .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT), |
| 402 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 403 | .info = snd_ak4114_spdif_info, |
| 404 | .get = snd_ak4114_spdif_get, |
| 405 | }, |
| 406 | { |
| 407 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
Masanari Iida | ec8f53f | 2012-11-02 00:28:50 +0900 | [diff] [blame] | 408 | .name = "IEC958 Preamble Capture Default", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 410 | .info = snd_ak4114_spdif_pinfo, |
| 411 | .get = snd_ak4114_spdif_pget, |
| 412 | }, |
| 413 | { |
| 414 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 415 | .name = "IEC958 Q-subcode Capture Default", |
| 416 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 417 | .info = snd_ak4114_spdif_qinfo, |
| 418 | .get = snd_ak4114_spdif_qget, |
| 419 | }, |
| 420 | { |
| 421 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 422 | .name = "IEC958 Audio", |
| 423 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 424 | .info = snd_ak4114_in_bit_info, |
| 425 | .get = snd_ak4114_in_bit_get, |
| 426 | .private_value = (1<<31) | (1<<8) | AK4114_REG_RCS0, |
| 427 | }, |
| 428 | { |
| 429 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 430 | .name = "IEC958 Non-PCM Bitstream", |
| 431 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 432 | .info = snd_ak4114_in_bit_info, |
| 433 | .get = snd_ak4114_in_bit_get, |
Pavel Hofman | c4116ae | 2007-04-05 17:07:30 +0200 | [diff] [blame] | 434 | .private_value = (6<<8) | AK4114_REG_RCS0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | }, |
| 436 | { |
| 437 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 438 | .name = "IEC958 DTS Bitstream", |
| 439 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 440 | .info = snd_ak4114_in_bit_info, |
| 441 | .get = snd_ak4114_in_bit_get, |
Pavel Hofman | c4116ae | 2007-04-05 17:07:30 +0200 | [diff] [blame] | 442 | .private_value = (3<<8) | AK4114_REG_RCS0, |
| 443 | }, |
| 444 | { |
| 445 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 446 | .name = "IEC958 PPL Lock Status", |
| 447 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 448 | .info = snd_ak4114_in_bit_info, |
| 449 | .get = snd_ak4114_in_bit_get, |
| 450 | .private_value = (1<<31) | (4<<8) | AK4114_REG_RCS0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | } |
| 452 | }; |
| 453 | |
Pavel Hofman | fdafad6 | 2008-02-11 14:48:06 +0100 | [diff] [blame] | 454 | |
| 455 | static void snd_ak4114_proc_regs_read(struct snd_info_entry *entry, |
| 456 | struct snd_info_buffer *buffer) |
| 457 | { |
| 458 | struct ak4114 *ak4114 = entry->private_data; |
| 459 | int reg, val; |
| 460 | /* all ak4114 registers 0x00 - 0x1f */ |
| 461 | for (reg = 0; reg < 0x20; reg++) { |
| 462 | val = reg_read(ak4114, reg); |
| 463 | snd_iprintf(buffer, "0x%02x = 0x%02x\n", reg, val); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | static void snd_ak4114_proc_init(struct ak4114 *ak4114) |
| 468 | { |
| 469 | struct snd_info_entry *entry; |
| 470 | if (!snd_card_proc_new(ak4114->card, "ak4114", &entry)) |
| 471 | snd_info_set_text_ops(entry, ak4114, snd_ak4114_proc_regs_read); |
| 472 | } |
| 473 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 474 | int snd_ak4114_build(struct ak4114 *ak4114, |
| 475 | struct snd_pcm_substream *ply_substream, |
| 476 | struct snd_pcm_substream *cap_substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 478 | struct snd_kcontrol *kctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | unsigned int idx; |
| 480 | int err; |
| 481 | |
Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 482 | if (snd_BUG_ON(!cap_substream)) |
| 483 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | ak4114->playback_substream = ply_substream; |
| 485 | ak4114->capture_substream = cap_substream; |
| 486 | for (idx = 0; idx < AK4114_CONTROLS; idx++) { |
| 487 | kctl = snd_ctl_new1(&snd_ak4114_iec958_controls[idx], ak4114); |
| 488 | if (kctl == NULL) |
| 489 | return -ENOMEM; |
Pavel Hofman | c4116ae | 2007-04-05 17:07:30 +0200 | [diff] [blame] | 490 | if (strstr(kctl->id.name, "Playback")) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | if (ply_substream == NULL) { |
| 492 | snd_ctl_free_one(kctl); |
| 493 | ak4114->kctls[idx] = NULL; |
| 494 | continue; |
| 495 | } |
| 496 | kctl->id.device = ply_substream->pcm->device; |
| 497 | kctl->id.subdevice = ply_substream->number; |
| 498 | } else { |
| 499 | kctl->id.device = cap_substream->pcm->device; |
| 500 | kctl->id.subdevice = cap_substream->number; |
| 501 | } |
| 502 | err = snd_ctl_add(ak4114->card, kctl); |
| 503 | if (err < 0) |
| 504 | return err; |
| 505 | ak4114->kctls[idx] = kctl; |
| 506 | } |
Pavel Hofman | fdafad6 | 2008-02-11 14:48:06 +0100 | [diff] [blame] | 507 | snd_ak4114_proc_init(ak4114); |
Takashi Iwai | 51354ae | 2007-03-30 15:38:39 +0200 | [diff] [blame] | 508 | /* trigger workq */ |
| 509 | schedule_delayed_work(&ak4114->work, HZ / 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | return 0; |
| 511 | } |
Takashi Iwai | a850ef8 | 2015-01-13 10:58:31 +0100 | [diff] [blame] | 512 | EXPORT_SYMBOL(snd_ak4114_build); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Takashi Iwai | 51354ae | 2007-03-30 15:38:39 +0200 | [diff] [blame] | 514 | /* notify kcontrols if any parameters are changed */ |
| 515 | static void ak4114_notify(struct ak4114 *ak4114, |
| 516 | unsigned char rcs0, unsigned char rcs1, |
| 517 | unsigned char c0, unsigned char c1) |
| 518 | { |
| 519 | if (!ak4114->kctls[0]) |
| 520 | return; |
| 521 | |
| 522 | if (rcs0 & AK4114_PAR) |
| 523 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 524 | &ak4114->kctls[0]->id); |
| 525 | if (rcs0 & AK4114_V) |
| 526 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 527 | &ak4114->kctls[1]->id); |
| 528 | if (rcs1 & AK4114_CCRC) |
| 529 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 530 | &ak4114->kctls[2]->id); |
| 531 | if (rcs1 & AK4114_QCRC) |
| 532 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 533 | &ak4114->kctls[3]->id); |
| 534 | |
| 535 | /* rate change */ |
| 536 | if (c1 & 0xf0) |
| 537 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 538 | &ak4114->kctls[4]->id); |
| 539 | |
| 540 | if ((c0 & AK4114_PEM) | (c0 & AK4114_CINT)) |
| 541 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 542 | &ak4114->kctls[9]->id); |
| 543 | if (c0 & AK4114_QINT) |
| 544 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 545 | &ak4114->kctls[10]->id); |
| 546 | |
| 547 | if (c0 & AK4114_AUDION) |
| 548 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 549 | &ak4114->kctls[11]->id); |
| 550 | if (c0 & AK4114_AUTO) |
| 551 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 552 | &ak4114->kctls[12]->id); |
| 553 | if (c0 & AK4114_DTSCD) |
| 554 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 555 | &ak4114->kctls[13]->id); |
Pavel Hofman | c4116ae | 2007-04-05 17:07:30 +0200 | [diff] [blame] | 556 | if (c0 & AK4114_UNLCK) |
| 557 | snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, |
| 558 | &ak4114->kctls[14]->id); |
Takashi Iwai | 51354ae | 2007-03-30 15:38:39 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 561 | int snd_ak4114_external_rate(struct ak4114 *ak4114) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | { |
| 563 | unsigned char rcs1; |
| 564 | |
| 565 | rcs1 = reg_read(ak4114, AK4114_REG_RCS1); |
| 566 | return external_rate(rcs1); |
| 567 | } |
Takashi Iwai | a850ef8 | 2015-01-13 10:58:31 +0100 | [diff] [blame] | 568 | EXPORT_SYMBOL(snd_ak4114_external_rate); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 570 | 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] | 571 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 572 | 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] | 573 | unsigned long _flags; |
| 574 | int res = 0; |
| 575 | unsigned char rcs0, rcs1; |
| 576 | unsigned char c0, c1; |
| 577 | |
| 578 | rcs1 = reg_read(ak4114, AK4114_REG_RCS1); |
| 579 | if (flags & AK4114_CHECK_NO_STAT) |
| 580 | goto __rate; |
| 581 | rcs0 = reg_read(ak4114, AK4114_REG_RCS0); |
| 582 | spin_lock_irqsave(&ak4114->lock, _flags); |
| 583 | if (rcs0 & AK4114_PAR) |
| 584 | ak4114->parity_errors++; |
| 585 | if (rcs1 & AK4114_V) |
| 586 | ak4114->v_bit_errors++; |
| 587 | if (rcs1 & AK4114_CCRC) |
| 588 | ak4114->ccrc_errors++; |
| 589 | if (rcs1 & AK4114_QCRC) |
| 590 | ak4114->qcrc_errors++; |
| 591 | c0 = (ak4114->rcs0 & (AK4114_QINT | AK4114_CINT | AK4114_PEM | AK4114_AUDION | AK4114_AUTO | AK4114_UNLCK)) ^ |
| 592 | (rcs0 & (AK4114_QINT | AK4114_CINT | AK4114_PEM | AK4114_AUDION | AK4114_AUTO | AK4114_UNLCK)); |
| 593 | c1 = (ak4114->rcs1 & 0xf0) ^ (rcs1 & 0xf0); |
| 594 | ak4114->rcs0 = rcs0 & ~(AK4114_QINT | AK4114_CINT); |
| 595 | ak4114->rcs1 = rcs1; |
| 596 | spin_unlock_irqrestore(&ak4114->lock, _flags); |
| 597 | |
Takashi Iwai | 51354ae | 2007-03-30 15:38:39 +0200 | [diff] [blame] | 598 | ak4114_notify(ak4114, rcs0, rcs1, c0, c1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | if (ak4114->change_callback && (c0 | c1) != 0) |
| 600 | ak4114->change_callback(ak4114, c0, c1); |
| 601 | |
| 602 | __rate: |
| 603 | /* compare rate */ |
| 604 | res = external_rate(rcs1); |
| 605 | if (!(flags & AK4114_CHECK_NO_RATE) && runtime && runtime->rate != res) { |
| 606 | snd_pcm_stream_lock_irqsave(ak4114->capture_substream, _flags); |
| 607 | if (snd_pcm_running(ak4114->capture_substream)) { |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 608 | // printk(KERN_DEBUG "rate changed (%i <- %i)\n", runtime->rate, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | snd_pcm_stop(ak4114->capture_substream, SNDRV_PCM_STATE_DRAINING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | res = 1; |
| 611 | } |
| 612 | snd_pcm_stream_unlock_irqrestore(ak4114->capture_substream, _flags); |
| 613 | } |
| 614 | return res; |
| 615 | } |
Takashi Iwai | a850ef8 | 2015-01-13 10:58:31 +0100 | [diff] [blame] | 616 | EXPORT_SYMBOL(snd_ak4114_check_rate_and_errors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 618 | static void ak4114_stats(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 620 | struct ak4114 *chip = container_of(work, struct ak4114, work.work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
Takashi Iwai | 4161b45 | 2015-01-13 10:53:20 +0100 | [diff] [blame] | 622 | if (atomic_inc_return(&chip->wq_processing) == 1) |
Pavel Hofman | 841b23d | 2008-03-17 08:45:33 +0100 | [diff] [blame] | 623 | snd_ak4114_check_rate_and_errors(chip, chip->check_flags); |
Takashi Iwai | 4161b45 | 2015-01-13 10:53:20 +0100 | [diff] [blame] | 624 | if (atomic_dec_and_test(&chip->wq_processing)) |
| 625 | schedule_delayed_work(&chip->work, HZ / 10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
Takashi Iwai | 1293617 | 2015-01-13 11:24:08 +0100 | [diff] [blame] | 627 | |
| 628 | #ifdef CONFIG_PM |
| 629 | void snd_ak4114_suspend(struct ak4114 *chip) |
| 630 | { |
| 631 | atomic_inc(&chip->wq_processing); /* don't schedule new work */ |
| 632 | cancel_delayed_work_sync(&chip->work); |
| 633 | } |
| 634 | EXPORT_SYMBOL(snd_ak4114_suspend); |
| 635 | |
| 636 | void snd_ak4114_resume(struct ak4114 *chip) |
| 637 | { |
| 638 | atomic_dec(&chip->wq_processing); |
| 639 | snd_ak4114_reinit(chip); |
| 640 | } |
| 641 | EXPORT_SYMBOL(snd_ak4114_resume); |
| 642 | #endif |