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