Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Routines for control of the AK4117 via 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/ak4117.h> |
| 30 | #include <sound/asoundef.h> |
| 31 | |
Jaroslav Kysela | c1017a4 | 2007-10-15 09:50:19 +0200 | [diff] [blame] | 32 | MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | MODULE_DESCRIPTION("AK4117 IEC958 (S/PDIF) receiver by Asahi Kasei"); |
| 34 | MODULE_LICENSE("GPL"); |
| 35 | |
| 36 | #define AK4117_ADDR 0x00 /* fixed address */ |
| 37 | |
| 38 | static void snd_ak4117_timer(unsigned long data); |
| 39 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 40 | static void reg_write(struct ak4117 *ak4117, unsigned char reg, unsigned char val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | ak4117->write(ak4117->private_data, reg, val); |
| 43 | if (reg < sizeof(ak4117->regmap)) |
| 44 | ak4117->regmap[reg] = val; |
| 45 | } |
| 46 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 47 | static inline unsigned char reg_read(struct ak4117 *ak4117, unsigned char reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
| 49 | return ak4117->read(ak4117->private_data, reg); |
| 50 | } |
| 51 | |
| 52 | #if 0 |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 53 | static void reg_dump(struct ak4117 *ak4117) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
| 55 | int i; |
| 56 | |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 57 | printk(KERN_DEBUG "AK4117 REG DUMP:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | for (i = 0; i < 0x1b; i++) |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 59 | printk(KERN_DEBUG "reg[%02x] = %02x (%02x)\n", i, reg_read(ak4117, i), i < sizeof(ak4117->regmap) ? ak4117->regmap[i] : 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | #endif |
| 62 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 63 | static void snd_ak4117_free(struct ak4117 *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
Kirill Tkhai | 115b94d | 2014-02-14 15:47:57 +0400 | [diff] [blame] | 65 | del_timer_sync(&chip->timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | kfree(chip); |
| 67 | } |
| 68 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 69 | static int snd_ak4117_dev_free(struct snd_device *device) |
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 ak4117 *chip = device->device_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | snd_ak4117_free(chip); |
| 73 | return 0; |
| 74 | } |
| 75 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 76 | int snd_ak4117_create(struct snd_card *card, ak4117_read_t *read, ak4117_write_t *write, |
Takashi Iwai | 517400c | 2007-01-29 15:27:56 +0100 | [diff] [blame] | 77 | const unsigned char pgm[5], void *private_data, struct ak4117 **r_ak4117) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 79 | struct ak4117 *chip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | int err = 0; |
| 81 | unsigned char reg; |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 82 | static struct snd_device_ops ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | .dev_free = snd_ak4117_dev_free, |
| 84 | }; |
| 85 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 86 | chip = kzalloc(sizeof(*chip), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | if (chip == NULL) |
| 88 | return -ENOMEM; |
| 89 | spin_lock_init(&chip->lock); |
| 90 | chip->card = card; |
| 91 | chip->read = read; |
| 92 | chip->write = write; |
| 93 | chip->private_data = private_data; |
Takashi Iwai | 867a0e0 | 2015-01-19 11:29:25 +0100 | [diff] [blame] | 94 | setup_timer(&chip->timer, snd_ak4117_timer, (unsigned long)chip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
| 96 | for (reg = 0; reg < 5; reg++) |
| 97 | chip->regmap[reg] = pgm[reg]; |
| 98 | snd_ak4117_reinit(chip); |
| 99 | |
| 100 | chip->rcs0 = reg_read(chip, AK4117_REG_RCS0) & ~(AK4117_QINT | AK4117_CINT | AK4117_STC); |
| 101 | chip->rcs1 = reg_read(chip, AK4117_REG_RCS1); |
| 102 | chip->rcs2 = reg_read(chip, AK4117_REG_RCS2); |
| 103 | |
| 104 | if ((err = snd_device_new(card, SNDRV_DEV_CODEC, chip, &ops)) < 0) |
| 105 | goto __fail; |
| 106 | |
| 107 | if (r_ak4117) |
| 108 | *r_ak4117 = chip; |
| 109 | return 0; |
| 110 | |
| 111 | __fail: |
| 112 | snd_ak4117_free(chip); |
Colin Ian King | 3805e6a | 2016-07-12 11:22:40 +0100 | [diff] [blame] | 113 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 116 | void snd_ak4117_reg_write(struct ak4117 *chip, unsigned char reg, unsigned char mask, unsigned char val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | { |
| 118 | if (reg >= 5) |
| 119 | return; |
| 120 | reg_write(chip, reg, (chip->regmap[reg] & ~mask) | val); |
| 121 | } |
| 122 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 123 | void snd_ak4117_reinit(struct ak4117 *chip) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | unsigned char old = chip->regmap[AK4117_REG_PWRDN], reg; |
| 126 | |
| 127 | del_timer(&chip->timer); |
| 128 | chip->init = 1; |
| 129 | /* bring the chip to reset state and powerdown state */ |
| 130 | reg_write(chip, AK4117_REG_PWRDN, 0); |
| 131 | udelay(200); |
| 132 | /* release reset, but leave powerdown */ |
| 133 | reg_write(chip, AK4117_REG_PWRDN, (old | AK4117_RST) & ~AK4117_PWN); |
| 134 | udelay(200); |
| 135 | for (reg = 1; reg < 5; reg++) |
| 136 | reg_write(chip, reg, chip->regmap[reg]); |
| 137 | /* release powerdown, everything is initialized now */ |
| 138 | reg_write(chip, AK4117_REG_PWRDN, old | AK4117_RST | AK4117_PWN); |
| 139 | chip->init = 0; |
Takashi Iwai | 867a0e0 | 2015-01-19 11:29:25 +0100 | [diff] [blame] | 140 | mod_timer(&chip->timer, 1 + jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static unsigned int external_rate(unsigned char rcs1) |
| 144 | { |
| 145 | switch (rcs1 & (AK4117_FS0|AK4117_FS1|AK4117_FS2|AK4117_FS3)) { |
| 146 | case AK4117_FS_32000HZ: return 32000; |
| 147 | case AK4117_FS_44100HZ: return 44100; |
| 148 | case AK4117_FS_48000HZ: return 48000; |
| 149 | case AK4117_FS_88200HZ: return 88200; |
| 150 | case AK4117_FS_96000HZ: return 96000; |
| 151 | case AK4117_FS_176400HZ: return 176400; |
| 152 | case AK4117_FS_192000HZ: return 192000; |
| 153 | default: return 0; |
| 154 | } |
| 155 | } |
| 156 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 157 | static int snd_ak4117_in_error_info(struct snd_kcontrol *kcontrol, |
| 158 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
| 160 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 161 | uinfo->count = 1; |
| 162 | uinfo->value.integer.min = 0; |
| 163 | uinfo->value.integer.max = LONG_MAX; |
| 164 | return 0; |
| 165 | } |
| 166 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 167 | static int snd_ak4117_in_error_get(struct snd_kcontrol *kcontrol, |
| 168 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 170 | struct ak4117 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | long *ptr; |
| 172 | |
| 173 | spin_lock_irq(&chip->lock); |
| 174 | ptr = (long *)(((char *)chip) + kcontrol->private_value); |
| 175 | ucontrol->value.integer.value[0] = *ptr; |
| 176 | *ptr = 0; |
| 177 | spin_unlock_irq(&chip->lock); |
| 178 | return 0; |
| 179 | } |
| 180 | |
Takashi Iwai | a5ce889 | 2007-07-23 15:42:26 +0200 | [diff] [blame] | 181 | #define snd_ak4117_in_bit_info snd_ctl_boolean_mono_info |
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 | static int snd_ak4117_in_bit_get(struct snd_kcontrol *kcontrol, |
| 184 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 186 | struct ak4117 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | unsigned char reg = kcontrol->private_value & 0xff; |
| 188 | unsigned char bit = (kcontrol->private_value >> 8) & 0xff; |
| 189 | unsigned char inv = (kcontrol->private_value >> 31) & 1; |
| 190 | |
| 191 | ucontrol->value.integer.value[0] = ((reg_read(chip, reg) & (1 << bit)) ? 1 : 0) ^ inv; |
| 192 | return 0; |
| 193 | } |
| 194 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 195 | static int snd_ak4117_rx_info(struct snd_kcontrol *kcontrol, |
| 196 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | { |
| 198 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 199 | uinfo->count = 1; |
| 200 | uinfo->value.integer.min = 0; |
| 201 | uinfo->value.integer.max = 1; |
| 202 | return 0; |
| 203 | } |
| 204 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 205 | static int snd_ak4117_rx_get(struct snd_kcontrol *kcontrol, |
| 206 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 208 | struct ak4117 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | ucontrol->value.integer.value[0] = (chip->regmap[AK4117_REG_IO] & AK4117_IPS) ? 1 : 0; |
| 211 | return 0; |
| 212 | } |
| 213 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 214 | static int snd_ak4117_rx_put(struct snd_kcontrol *kcontrol, |
| 215 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 217 | struct ak4117 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | int change; |
| 219 | u8 old_val; |
| 220 | |
| 221 | spin_lock_irq(&chip->lock); |
| 222 | old_val = chip->regmap[AK4117_REG_IO]; |
| 223 | change = !!ucontrol->value.integer.value[0] != ((old_val & AK4117_IPS) ? 1 : 0); |
| 224 | if (change) |
| 225 | reg_write(chip, AK4117_REG_IO, (old_val & ~AK4117_IPS) | (ucontrol->value.integer.value[0] ? AK4117_IPS : 0)); |
| 226 | spin_unlock_irq(&chip->lock); |
| 227 | return change; |
| 228 | } |
| 229 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 230 | static int snd_ak4117_rate_info(struct snd_kcontrol *kcontrol, |
| 231 | struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | { |
| 233 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 234 | uinfo->count = 1; |
| 235 | uinfo->value.integer.min = 0; |
| 236 | uinfo->value.integer.max = 192000; |
| 237 | return 0; |
| 238 | } |
| 239 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 240 | static int snd_ak4117_rate_get(struct snd_kcontrol *kcontrol, |
| 241 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 243 | struct ak4117 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | ucontrol->value.integer.value[0] = external_rate(reg_read(chip, AK4117_REG_RCS1)); |
| 246 | return 0; |
| 247 | } |
| 248 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 249 | static int snd_ak4117_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
| 251 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 252 | uinfo->count = 1; |
| 253 | return 0; |
| 254 | } |
| 255 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 256 | static int snd_ak4117_spdif_get(struct snd_kcontrol *kcontrol, |
| 257 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 259 | struct ak4117 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | unsigned i; |
| 261 | |
| 262 | for (i = 0; i < AK4117_REG_RXCSB_SIZE; i++) |
| 263 | ucontrol->value.iec958.status[i] = reg_read(chip, AK4117_REG_RXCSB0 + i); |
| 264 | return 0; |
| 265 | } |
| 266 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 267 | static int snd_ak4117_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] | 268 | { |
| 269 | uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958; |
| 270 | uinfo->count = 1; |
| 271 | return 0; |
| 272 | } |
| 273 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 274 | static int snd_ak4117_spdif_mask_get(struct snd_kcontrol *kcontrol, |
| 275 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
| 277 | memset(ucontrol->value.iec958.status, 0xff, AK4117_REG_RXCSB_SIZE); |
| 278 | return 0; |
| 279 | } |
| 280 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 281 | static int snd_ak4117_spdif_pinfo(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | { |
| 283 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; |
| 284 | uinfo->value.integer.min = 0; |
| 285 | uinfo->value.integer.max = 0xffff; |
| 286 | uinfo->count = 4; |
| 287 | return 0; |
| 288 | } |
| 289 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 290 | static int snd_ak4117_spdif_pget(struct snd_kcontrol *kcontrol, |
| 291 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 293 | struct ak4117 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | unsigned short tmp; |
| 295 | |
| 296 | ucontrol->value.integer.value[0] = 0xf8f2; |
| 297 | ucontrol->value.integer.value[1] = 0x4e1f; |
| 298 | tmp = reg_read(chip, AK4117_REG_Pc0) | (reg_read(chip, AK4117_REG_Pc1) << 8); |
| 299 | ucontrol->value.integer.value[2] = tmp; |
| 300 | tmp = reg_read(chip, AK4117_REG_Pd0) | (reg_read(chip, AK4117_REG_Pd1) << 8); |
| 301 | ucontrol->value.integer.value[3] = tmp; |
| 302 | return 0; |
| 303 | } |
| 304 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 305 | static int snd_ak4117_spdif_qinfo(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | { |
| 307 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES; |
| 308 | uinfo->count = AK4117_REG_QSUB_SIZE; |
| 309 | return 0; |
| 310 | } |
| 311 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 312 | static int snd_ak4117_spdif_qget(struct snd_kcontrol *kcontrol, |
| 313 | struct snd_ctl_elem_value *ucontrol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 315 | struct ak4117 *chip = snd_kcontrol_chip(kcontrol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | unsigned i; |
| 317 | |
| 318 | for (i = 0; i < AK4117_REG_QSUB_SIZE; i++) |
| 319 | ucontrol->value.bytes.data[i] = reg_read(chip, AK4117_REG_QSUB_ADDR + i); |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | /* Don't forget to change AK4117_CONTROLS define!!! */ |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 324 | static struct snd_kcontrol_new snd_ak4117_iec958_controls[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | { |
| 326 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 327 | .name = "IEC958 Parity Errors", |
| 328 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 329 | .info = snd_ak4117_in_error_info, |
| 330 | .get = snd_ak4117_in_error_get, |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 331 | .private_value = offsetof(struct ak4117, parity_errors), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | }, |
| 333 | { |
| 334 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 335 | .name = "IEC958 V-Bit Errors", |
| 336 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 337 | .info = snd_ak4117_in_error_info, |
| 338 | .get = snd_ak4117_in_error_get, |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 339 | .private_value = offsetof(struct ak4117, v_bit_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 C-CRC Errors", |
| 344 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 345 | .info = snd_ak4117_in_error_info, |
| 346 | .get = snd_ak4117_in_error_get, |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 347 | .private_value = offsetof(struct ak4117, ccrc_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 Q-CRC Errors", |
| 352 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 353 | .info = snd_ak4117_in_error_info, |
| 354 | .get = snd_ak4117_in_error_get, |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 355 | .private_value = offsetof(struct ak4117, qcrc_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 External Rate", |
| 360 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 361 | .info = snd_ak4117_rate_info, |
| 362 | .get = snd_ak4117_rate_get, |
| 363 | }, |
| 364 | { |
| 365 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 366 | .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,MASK), |
| 367 | .access = SNDRV_CTL_ELEM_ACCESS_READ, |
| 368 | .info = snd_ak4117_spdif_mask_info, |
| 369 | .get = snd_ak4117_spdif_mask_get, |
| 370 | }, |
| 371 | { |
| 372 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 373 | .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT), |
| 374 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 375 | .info = snd_ak4117_spdif_info, |
| 376 | .get = snd_ak4117_spdif_get, |
| 377 | }, |
| 378 | { |
| 379 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
Masanari Iida | ec8f53f | 2012-11-02 00:28:50 +0900 | [diff] [blame] | 380 | .name = "IEC958 Preamble Capture Default", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 382 | .info = snd_ak4117_spdif_pinfo, |
| 383 | .get = snd_ak4117_spdif_pget, |
| 384 | }, |
| 385 | { |
| 386 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 387 | .name = "IEC958 Q-subcode Capture Default", |
| 388 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 389 | .info = snd_ak4117_spdif_qinfo, |
| 390 | .get = snd_ak4117_spdif_qget, |
| 391 | }, |
| 392 | { |
| 393 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 394 | .name = "IEC958 Audio", |
| 395 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 396 | .info = snd_ak4117_in_bit_info, |
| 397 | .get = snd_ak4117_in_bit_get, |
| 398 | .private_value = (1<<31) | (3<<8) | AK4117_REG_RCS0, |
| 399 | }, |
| 400 | { |
| 401 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 402 | .name = "IEC958 Non-PCM Bitstream", |
| 403 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 404 | .info = snd_ak4117_in_bit_info, |
| 405 | .get = snd_ak4117_in_bit_get, |
| 406 | .private_value = (5<<8) | AK4117_REG_RCS1, |
| 407 | }, |
| 408 | { |
| 409 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 410 | .name = "IEC958 DTS Bitstream", |
| 411 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, |
| 412 | .info = snd_ak4117_in_bit_info, |
| 413 | .get = snd_ak4117_in_bit_get, |
| 414 | .private_value = (6<<8) | AK4117_REG_RCS1, |
| 415 | }, |
| 416 | { |
| 417 | .iface = SNDRV_CTL_ELEM_IFACE_PCM, |
| 418 | .name = "AK4117 Input Select", |
| 419 | .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE, |
| 420 | .info = snd_ak4117_rx_info, |
| 421 | .get = snd_ak4117_rx_get, |
| 422 | .put = snd_ak4117_rx_put, |
| 423 | } |
| 424 | }; |
| 425 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 426 | int snd_ak4117_build(struct ak4117 *ak4117, struct snd_pcm_substream *cap_substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 428 | struct snd_kcontrol *kctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | unsigned int idx; |
| 430 | int err; |
| 431 | |
Takashi Iwai | 5e246b8 | 2008-08-08 17:12:47 +0200 | [diff] [blame] | 432 | if (snd_BUG_ON(!cap_substream)) |
| 433 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | ak4117->substream = cap_substream; |
| 435 | for (idx = 0; idx < AK4117_CONTROLS; idx++) { |
| 436 | kctl = snd_ctl_new1(&snd_ak4117_iec958_controls[idx], ak4117); |
| 437 | if (kctl == NULL) |
| 438 | return -ENOMEM; |
| 439 | kctl->id.device = cap_substream->pcm->device; |
| 440 | kctl->id.subdevice = cap_substream->number; |
| 441 | err = snd_ctl_add(ak4117->card, kctl); |
| 442 | if (err < 0) |
| 443 | return err; |
| 444 | ak4117->kctls[idx] = kctl; |
| 445 | } |
| 446 | return 0; |
| 447 | } |
| 448 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 449 | int snd_ak4117_external_rate(struct ak4117 *ak4117) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | { |
| 451 | unsigned char rcs1; |
| 452 | |
| 453 | rcs1 = reg_read(ak4117, AK4117_REG_RCS1); |
| 454 | return external_rate(rcs1); |
| 455 | } |
| 456 | |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 457 | int snd_ak4117_check_rate_and_errors(struct ak4117 *ak4117, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 459 | struct snd_pcm_runtime *runtime = ak4117->substream ? ak4117->substream->runtime : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | unsigned long _flags; |
| 461 | int res = 0; |
| 462 | unsigned char rcs0, rcs1, rcs2; |
| 463 | unsigned char c0, c1; |
| 464 | |
| 465 | rcs1 = reg_read(ak4117, AK4117_REG_RCS1); |
| 466 | if (flags & AK4117_CHECK_NO_STAT) |
| 467 | goto __rate; |
| 468 | rcs0 = reg_read(ak4117, AK4117_REG_RCS0); |
| 469 | rcs2 = reg_read(ak4117, AK4117_REG_RCS2); |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 470 | // printk(KERN_DEBUG "AK IRQ: rcs0 = 0x%x, rcs1 = 0x%x, rcs2 = 0x%x\n", rcs0, rcs1, rcs2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | spin_lock_irqsave(&ak4117->lock, _flags); |
| 472 | if (rcs0 & AK4117_PAR) |
| 473 | ak4117->parity_errors++; |
| 474 | if (rcs0 & AK4117_V) |
| 475 | ak4117->v_bit_errors++; |
| 476 | if (rcs2 & AK4117_CCRC) |
| 477 | ak4117->ccrc_errors++; |
| 478 | if (rcs2 & AK4117_QCRC) |
| 479 | ak4117->qcrc_errors++; |
| 480 | c0 = (ak4117->rcs0 & (AK4117_QINT | AK4117_CINT | AK4117_STC | AK4117_AUDION | AK4117_AUTO | AK4117_UNLCK)) ^ |
| 481 | (rcs0 & (AK4117_QINT | AK4117_CINT | AK4117_STC | AK4117_AUDION | AK4117_AUTO | AK4117_UNLCK)); |
| 482 | c1 = (ak4117->rcs1 & (AK4117_DTSCD | AK4117_NPCM | AK4117_PEM | 0x0f)) ^ |
| 483 | (rcs1 & (AK4117_DTSCD | AK4117_NPCM | AK4117_PEM | 0x0f)); |
| 484 | ak4117->rcs0 = rcs0 & ~(AK4117_QINT | AK4117_CINT | AK4117_STC); |
| 485 | ak4117->rcs1 = rcs1; |
| 486 | ak4117->rcs2 = rcs2; |
| 487 | spin_unlock_irqrestore(&ak4117->lock, _flags); |
| 488 | |
| 489 | if (rcs0 & AK4117_PAR) |
| 490 | snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[0]->id); |
| 491 | if (rcs0 & AK4117_V) |
| 492 | snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[1]->id); |
| 493 | if (rcs2 & AK4117_CCRC) |
| 494 | snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[2]->id); |
| 495 | if (rcs2 & AK4117_QCRC) |
| 496 | snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[3]->id); |
| 497 | |
| 498 | /* rate change */ |
| 499 | if (c1 & 0x0f) |
| 500 | snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[4]->id); |
| 501 | |
| 502 | if ((c1 & AK4117_PEM) | (c0 & AK4117_CINT)) |
| 503 | snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[6]->id); |
| 504 | if (c0 & AK4117_QINT) |
| 505 | snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[8]->id); |
| 506 | |
| 507 | if (c0 & AK4117_AUDION) |
| 508 | snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[9]->id); |
| 509 | if (c1 & AK4117_NPCM) |
| 510 | snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[10]->id); |
| 511 | if (c1 & AK4117_DTSCD) |
| 512 | snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[11]->id); |
| 513 | |
| 514 | if (ak4117->change_callback && (c0 | c1) != 0) |
| 515 | ak4117->change_callback(ak4117, c0, c1); |
| 516 | |
| 517 | __rate: |
| 518 | /* compare rate */ |
| 519 | res = external_rate(rcs1); |
| 520 | if (!(flags & AK4117_CHECK_NO_RATE) && runtime && runtime->rate != res) { |
| 521 | snd_pcm_stream_lock_irqsave(ak4117->substream, _flags); |
| 522 | if (snd_pcm_running(ak4117->substream)) { |
Takashi Iwai | 99b359b | 2005-10-20 18:26:44 +0200 | [diff] [blame] | 523 | // printk(KERN_DEBUG "rate changed (%i <- %i)\n", runtime->rate, res); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | snd_pcm_stop(ak4117->substream, SNDRV_PCM_STATE_DRAINING); |
| 525 | wake_up(&runtime->sleep); |
| 526 | res = 1; |
| 527 | } |
| 528 | snd_pcm_stream_unlock_irqrestore(ak4117->substream, _flags); |
| 529 | } |
| 530 | return res; |
| 531 | } |
| 532 | |
| 533 | static void snd_ak4117_timer(unsigned long data) |
| 534 | { |
Takashi Iwai | 97f02e0 | 2005-11-17 14:17:19 +0100 | [diff] [blame] | 535 | struct ak4117 *chip = (struct ak4117 *)data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | |
| 537 | if (chip->init) |
| 538 | return; |
| 539 | snd_ak4117_check_rate_and_errors(chip, 0); |
Takashi Iwai | 867a0e0 | 2015-01-19 11:29:25 +0100 | [diff] [blame] | 540 | mod_timer(&chip->timer, 1 + jiffies); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | EXPORT_SYMBOL(snd_ak4117_create); |
| 544 | EXPORT_SYMBOL(snd_ak4117_reg_write); |
| 545 | EXPORT_SYMBOL(snd_ak4117_reinit); |
| 546 | EXPORT_SYMBOL(snd_ak4117_build); |
| 547 | EXPORT_SYMBOL(snd_ak4117_external_rate); |
| 548 | EXPORT_SYMBOL(snd_ak4117_check_rate_and_errors); |