blob: b70e6eccbd035372e48098188f32aa99e83b4ea5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines for control of the AK4114 via I2C and 4-wire serial interface
3 * IEC958 (S/PDIF) receiver by Asahi Kasei
Jaroslav Kyselac1017a42007-10-15 09:50:19 +02004 * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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 Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/slab.h>
24#include <linux/delay.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040025#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#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 Hofmanfdafad62008-02-11 14:48:06 +010031#include <sound/info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020033MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070034MODULE_DESCRIPTION("AK4114 IEC958 (S/PDIF) receiver by Asahi Kasei");
35MODULE_LICENSE("GPL");
36
37#define AK4114_ADDR 0x00 /* fixed address */
38
David Howellsc4028952006-11-22 14:57:56 +000039static void ak4114_stats(struct work_struct *work);
Takashi Iwai51354ae2007-03-30 15:38:39 +020040static void ak4114_init_regs(struct ak4114 *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Takashi Iwai97f02e02005-11-17 14:17:19 +010042static void reg_write(struct ak4114 *ak4114, unsigned char reg, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 ak4114->write(ak4114->private_data, reg, val);
45 if (reg <= AK4114_REG_INT1_MASK)
46 ak4114->regmap[reg] = val;
Jean Delvare1ab774e2007-02-05 13:07:04 +010047 else if (reg >= AK4114_REG_TXCSB0 && reg <= AK4114_REG_TXCSB4)
48 ak4114->txcsb[reg-AK4114_REG_TXCSB0] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
Takashi Iwai97f02e02005-11-17 14:17:19 +010051static inline unsigned char reg_read(struct ak4114 *ak4114, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 return ak4114->read(ak4114->private_data, reg);
54}
55
56#if 0
Takashi Iwai97f02e02005-11-17 14:17:19 +010057static void reg_dump(struct ak4114 *ak4114)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 int i;
60
Takashi Iwai99b359b2005-10-20 18:26:44 +020061 printk(KERN_DEBUG "AK4114 REG DUMP:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 for (i = 0; i < 0x20; i++)
Takashi Iwaie12483e2013-10-29 16:37:11 +010063 printk(KERN_DEBUG "reg[%02x] = %02x (%02x)\n", i, reg_read(ak4114, i), i < ARRAY_SIZE(ak4114->regmap) ? ak4114->regmap[i] : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65#endif
66
Takashi Iwai97f02e02005-11-17 14:17:19 +010067static void snd_ak4114_free(struct ak4114 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Takashi Iwai4161b452015-01-13 10:53:20 +010069 atomic_inc(&chip->wq_processing); /* don't schedule new work */
Tejun Heo5b84ba22010-12-11 17:51:26 +010070 cancel_delayed_work_sync(&chip->work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 kfree(chip);
72}
73
Takashi Iwai97f02e02005-11-17 14:17:19 +010074static int snd_ak4114_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Takashi Iwai97f02e02005-11-17 14:17:19 +010076 struct ak4114 *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 snd_ak4114_free(chip);
78 return 0;
79}
80
Takashi Iwai97f02e02005-11-17 14:17:19 +010081int snd_ak4114_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 ak4114_read_t *read, ak4114_write_t *write,
Takashi Iwaie12483e2013-10-29 16:37:11 +010083 const unsigned char pgm[6], const unsigned char txcsb[5],
Takashi Iwai97f02e02005-11-17 14:17:19 +010084 void *private_data, struct ak4114 **r_ak4114)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Takashi Iwai97f02e02005-11-17 14:17:19 +010086 struct ak4114 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 int err = 0;
88 unsigned char reg;
Takashi Iwai97f02e02005-11-17 14:17:19 +010089 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 .dev_free = snd_ak4114_dev_free,
91 };
92
Takashi Iwai561b2202005-09-09 14:22:34 +020093 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 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 Iwai3b6baa52007-01-31 14:34:38 +0100101 INIT_DELAYED_WORK(&chip->work, ak4114_stats);
Takashi Iwai4161b452015-01-13 10:53:20 +0100102 atomic_set(&chip->wq_processing, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Takashi Iwaie12483e2013-10-29 16:37:11 +0100104 for (reg = 0; reg < 6; reg++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 chip->regmap[reg] = pgm[reg];
106 for (reg = 0; reg < 5; reg++)
107 chip->txcsb[reg] = txcsb[reg];
108
Takashi Iwai51354ae2007-03-30 15:38:39 +0200109 ak4114_init_regs(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 chip->rcs0 = reg_read(chip, AK4114_REG_RCS0) & ~(AK4114_QINT | AK4114_CINT);
112 chip->rcs1 = reg_read(chip, AK4114_REG_RCS1);
113
Takashi Iwai483eb062014-02-04 11:18:23 +0100114 if ((err = snd_device_new(card, SNDRV_DEV_CODEC, chip, &ops)) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 goto __fail;
116
117 if (r_ak4114)
118 *r_ak4114 = chip;
119 return 0;
120
121 __fail:
122 snd_ak4114_free(chip);
123 return err < 0 ? err : -EIO;
124}
125
Takashi Iwai97f02e02005-11-17 14:17:19 +0100126void snd_ak4114_reg_write(struct ak4114 *chip, unsigned char reg, unsigned char mask, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 if (reg <= AK4114_REG_INT1_MASK)
129 reg_write(chip, reg, (chip->regmap[reg] & ~mask) | val);
130 else if (reg >= AK4114_REG_TXCSB0 && reg <= AK4114_REG_TXCSB4)
Jean Delvare1ab774e2007-02-05 13:07:04 +0100131 reg_write(chip, reg,
132 (chip->txcsb[reg-AK4114_REG_TXCSB0] & ~mask) | val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Takashi Iwai51354ae2007-03-30 15:38:39 +0200135static void ak4114_init_regs(struct ak4114 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 unsigned char old = chip->regmap[AK4114_REG_PWRDN], reg;
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /* bring the chip to reset state and powerdown state */
140 reg_write(chip, AK4114_REG_PWRDN, old & ~(AK4114_RST|AK4114_PWN));
141 udelay(200);
142 /* release reset, but leave powerdown */
143 reg_write(chip, AK4114_REG_PWRDN, (old | AK4114_RST) & ~AK4114_PWN);
144 udelay(200);
Takashi Iwaie12483e2013-10-29 16:37:11 +0100145 for (reg = 1; reg < 6; reg++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 reg_write(chip, reg, chip->regmap[reg]);
147 for (reg = 0; reg < 5; reg++)
148 reg_write(chip, reg + AK4114_REG_TXCSB0, chip->txcsb[reg]);
149 /* release powerdown, everything is initialized now */
150 reg_write(chip, AK4114_REG_PWRDN, old | AK4114_RST | AK4114_PWN);
Takashi Iwai51354ae2007-03-30 15:38:39 +0200151}
152
153void snd_ak4114_reinit(struct ak4114 *chip)
154{
Takashi Iwai4161b452015-01-13 10:53:20 +0100155 if (atomic_inc_return(&chip->wq_processing) == 1)
156 cancel_delayed_work_sync(&chip->work);
Takashi Iwai51354ae2007-03-30 15:38:39 +0200157 ak4114_init_regs(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* bring up statistics / event queing */
Takashi Iwai4161b452015-01-13 10:53:20 +0100159 if (atomic_dec_and_test(&chip->wq_processing))
Takashi Iwai51354ae2007-03-30 15:38:39 +0200160 schedule_delayed_work(&chip->work, HZ / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163static unsigned int external_rate(unsigned char rcs1)
164{
165 switch (rcs1 & (AK4114_FS0|AK4114_FS1|AK4114_FS2|AK4114_FS3)) {
166 case AK4114_FS_32000HZ: return 32000;
167 case AK4114_FS_44100HZ: return 44100;
168 case AK4114_FS_48000HZ: return 48000;
169 case AK4114_FS_88200HZ: return 88200;
170 case AK4114_FS_96000HZ: return 96000;
171 case AK4114_FS_176400HZ: return 176400;
172 case AK4114_FS_192000HZ: return 192000;
173 default: return 0;
174 }
175}
176
Takashi Iwai97f02e02005-11-17 14:17:19 +0100177static int snd_ak4114_in_error_info(struct snd_kcontrol *kcontrol,
178 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
181 uinfo->count = 1;
182 uinfo->value.integer.min = 0;
183 uinfo->value.integer.max = LONG_MAX;
184 return 0;
185}
186
Takashi Iwai97f02e02005-11-17 14:17:19 +0100187static int snd_ak4114_in_error_get(struct snd_kcontrol *kcontrol,
188 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100190 struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 long *ptr;
192
193 spin_lock_irq(&chip->lock);
194 ptr = (long *)(((char *)chip) + kcontrol->private_value);
195 ucontrol->value.integer.value[0] = *ptr;
196 *ptr = 0;
197 spin_unlock_irq(&chip->lock);
198 return 0;
199}
200
Takashi Iwaia5ce8892007-07-23 15:42:26 +0200201#define snd_ak4114_in_bit_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Takashi Iwai97f02e02005-11-17 14:17:19 +0100203static int snd_ak4114_in_bit_get(struct snd_kcontrol *kcontrol,
204 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100206 struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 unsigned char reg = kcontrol->private_value & 0xff;
208 unsigned char bit = (kcontrol->private_value >> 8) & 0xff;
209 unsigned char inv = (kcontrol->private_value >> 31) & 1;
210
211 ucontrol->value.integer.value[0] = ((reg_read(chip, reg) & (1 << bit)) ? 1 : 0) ^ inv;
212 return 0;
213}
214
Takashi Iwai97f02e02005-11-17 14:17:19 +0100215static int snd_ak4114_rate_info(struct snd_kcontrol *kcontrol,
216 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
219 uinfo->count = 1;
220 uinfo->value.integer.min = 0;
221 uinfo->value.integer.max = 192000;
222 return 0;
223}
224
Takashi Iwai97f02e02005-11-17 14:17:19 +0100225static int snd_ak4114_rate_get(struct snd_kcontrol *kcontrol,
226 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100228 struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 ucontrol->value.integer.value[0] = external_rate(reg_read(chip, AK4114_REG_RCS1));
231 return 0;
232}
233
Takashi Iwai97f02e02005-11-17 14:17:19 +0100234static int snd_ak4114_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
236 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
237 uinfo->count = 1;
238 return 0;
239}
240
Takashi Iwai97f02e02005-11-17 14:17:19 +0100241static int snd_ak4114_spdif_get(struct snd_kcontrol *kcontrol,
242 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100244 struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 unsigned i;
246
247 for (i = 0; i < AK4114_REG_RXCSB_SIZE; i++)
248 ucontrol->value.iec958.status[i] = reg_read(chip, AK4114_REG_RXCSB0 + i);
249 return 0;
250}
251
Takashi Iwai97f02e02005-11-17 14:17:19 +0100252static int snd_ak4114_spdif_playback_get(struct snd_kcontrol *kcontrol,
253 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100255 struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 unsigned i;
257
258 for (i = 0; i < AK4114_REG_TXCSB_SIZE; i++)
259 ucontrol->value.iec958.status[i] = chip->txcsb[i];
260 return 0;
261}
262
Takashi Iwai97f02e02005-11-17 14:17:19 +0100263static int snd_ak4114_spdif_playback_put(struct snd_kcontrol *kcontrol,
264 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100266 struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 unsigned i;
268
269 for (i = 0; i < AK4114_REG_TXCSB_SIZE; i++)
270 reg_write(chip, AK4114_REG_TXCSB0 + i, ucontrol->value.iec958.status[i]);
271 return 0;
272}
273
Takashi Iwai97f02e02005-11-17 14:17:19 +0100274static int snd_ak4114_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
277 uinfo->count = 1;
278 return 0;
279}
280
Takashi Iwai97f02e02005-11-17 14:17:19 +0100281static int snd_ak4114_spdif_mask_get(struct snd_kcontrol *kcontrol,
282 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
284 memset(ucontrol->value.iec958.status, 0xff, AK4114_REG_RXCSB_SIZE);
285 return 0;
286}
287
Takashi Iwai97f02e02005-11-17 14:17:19 +0100288static int snd_ak4114_spdif_pinfo(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
291 uinfo->value.integer.min = 0;
292 uinfo->value.integer.max = 0xffff;
293 uinfo->count = 4;
294 return 0;
295}
296
Takashi Iwai97f02e02005-11-17 14:17:19 +0100297static int snd_ak4114_spdif_pget(struct snd_kcontrol *kcontrol,
298 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100300 struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 unsigned short tmp;
302
303 ucontrol->value.integer.value[0] = 0xf8f2;
304 ucontrol->value.integer.value[1] = 0x4e1f;
305 tmp = reg_read(chip, AK4114_REG_Pc0) | (reg_read(chip, AK4114_REG_Pc1) << 8);
306 ucontrol->value.integer.value[2] = tmp;
307 tmp = reg_read(chip, AK4114_REG_Pd0) | (reg_read(chip, AK4114_REG_Pd1) << 8);
308 ucontrol->value.integer.value[3] = tmp;
309 return 0;
310}
311
Takashi Iwai97f02e02005-11-17 14:17:19 +0100312static int snd_ak4114_spdif_qinfo(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
315 uinfo->count = AK4114_REG_QSUB_SIZE;
316 return 0;
317}
318
Takashi Iwai97f02e02005-11-17 14:17:19 +0100319static int snd_ak4114_spdif_qget(struct snd_kcontrol *kcontrol,
320 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100322 struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 unsigned i;
324
325 for (i = 0; i < AK4114_REG_QSUB_SIZE; i++)
326 ucontrol->value.bytes.data[i] = reg_read(chip, AK4114_REG_QSUB_ADDR + i);
327 return 0;
328}
329
330/* Don't forget to change AK4114_CONTROLS define!!! */
Takashi Iwai97f02e02005-11-17 14:17:19 +0100331static struct snd_kcontrol_new snd_ak4114_iec958_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
333 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
334 .name = "IEC958 Parity Errors",
335 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
336 .info = snd_ak4114_in_error_info,
337 .get = snd_ak4114_in_error_get,
Takashi Iwai97f02e02005-11-17 14:17:19 +0100338 .private_value = offsetof(struct ak4114, parity_errors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339},
340{
341 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
342 .name = "IEC958 V-Bit Errors",
343 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
344 .info = snd_ak4114_in_error_info,
345 .get = snd_ak4114_in_error_get,
Takashi Iwai97f02e02005-11-17 14:17:19 +0100346 .private_value = offsetof(struct ak4114, v_bit_errors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347},
348{
349 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
350 .name = "IEC958 C-CRC Errors",
351 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
352 .info = snd_ak4114_in_error_info,
353 .get = snd_ak4114_in_error_get,
Takashi Iwai97f02e02005-11-17 14:17:19 +0100354 .private_value = offsetof(struct ak4114, ccrc_errors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355},
356{
357 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
358 .name = "IEC958 Q-CRC Errors",
359 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
360 .info = snd_ak4114_in_error_info,
361 .get = snd_ak4114_in_error_get,
Takashi Iwai97f02e02005-11-17 14:17:19 +0100362 .private_value = offsetof(struct ak4114, qcrc_errors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363},
364{
365 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
366 .name = "IEC958 External Rate",
367 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
368 .info = snd_ak4114_rate_info,
369 .get = snd_ak4114_rate_get,
370},
371{
372 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
373 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
374 .access = SNDRV_CTL_ELEM_ACCESS_READ,
375 .info = snd_ak4114_spdif_mask_info,
376 .get = snd_ak4114_spdif_mask_get,
377},
378{
379 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
380 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
381 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
382 .info = snd_ak4114_spdif_info,
383 .get = snd_ak4114_spdif_playback_get,
384 .put = snd_ak4114_spdif_playback_put,
385},
386{
387 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
388 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,MASK),
389 .access = SNDRV_CTL_ELEM_ACCESS_READ,
390 .info = snd_ak4114_spdif_mask_info,
391 .get = snd_ak4114_spdif_mask_get,
392},
393{
394 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
395 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
396 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
397 .info = snd_ak4114_spdif_info,
398 .get = snd_ak4114_spdif_get,
399},
400{
401 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
Masanari Iidaec8f53f2012-11-02 00:28:50 +0900402 .name = "IEC958 Preamble Capture Default",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
404 .info = snd_ak4114_spdif_pinfo,
405 .get = snd_ak4114_spdif_pget,
406},
407{
408 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
409 .name = "IEC958 Q-subcode Capture Default",
410 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
411 .info = snd_ak4114_spdif_qinfo,
412 .get = snd_ak4114_spdif_qget,
413},
414{
415 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
416 .name = "IEC958 Audio",
417 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
418 .info = snd_ak4114_in_bit_info,
419 .get = snd_ak4114_in_bit_get,
420 .private_value = (1<<31) | (1<<8) | AK4114_REG_RCS0,
421},
422{
423 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
424 .name = "IEC958 Non-PCM Bitstream",
425 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
426 .info = snd_ak4114_in_bit_info,
427 .get = snd_ak4114_in_bit_get,
Pavel Hofmanc4116ae2007-04-05 17:07:30 +0200428 .private_value = (6<<8) | AK4114_REG_RCS0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429},
430{
431 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
432 .name = "IEC958 DTS Bitstream",
433 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
434 .info = snd_ak4114_in_bit_info,
435 .get = snd_ak4114_in_bit_get,
Pavel Hofmanc4116ae2007-04-05 17:07:30 +0200436 .private_value = (3<<8) | AK4114_REG_RCS0,
437},
438{
439 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
440 .name = "IEC958 PPL Lock Status",
441 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
442 .info = snd_ak4114_in_bit_info,
443 .get = snd_ak4114_in_bit_get,
444 .private_value = (1<<31) | (4<<8) | AK4114_REG_RCS0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446};
447
Pavel Hofmanfdafad62008-02-11 14:48:06 +0100448
449static void snd_ak4114_proc_regs_read(struct snd_info_entry *entry,
450 struct snd_info_buffer *buffer)
451{
452 struct ak4114 *ak4114 = entry->private_data;
453 int reg, val;
454 /* all ak4114 registers 0x00 - 0x1f */
455 for (reg = 0; reg < 0x20; reg++) {
456 val = reg_read(ak4114, reg);
457 snd_iprintf(buffer, "0x%02x = 0x%02x\n", reg, val);
458 }
459}
460
461static void snd_ak4114_proc_init(struct ak4114 *ak4114)
462{
463 struct snd_info_entry *entry;
464 if (!snd_card_proc_new(ak4114->card, "ak4114", &entry))
465 snd_info_set_text_ops(entry, ak4114, snd_ak4114_proc_regs_read);
466}
467
Takashi Iwai97f02e02005-11-17 14:17:19 +0100468int snd_ak4114_build(struct ak4114 *ak4114,
469 struct snd_pcm_substream *ply_substream,
470 struct snd_pcm_substream *cap_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100472 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 unsigned int idx;
474 int err;
475
Takashi Iwai5e246b82008-08-08 17:12:47 +0200476 if (snd_BUG_ON(!cap_substream))
477 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 ak4114->playback_substream = ply_substream;
479 ak4114->capture_substream = cap_substream;
480 for (idx = 0; idx < AK4114_CONTROLS; idx++) {
481 kctl = snd_ctl_new1(&snd_ak4114_iec958_controls[idx], ak4114);
482 if (kctl == NULL)
483 return -ENOMEM;
Pavel Hofmanc4116ae2007-04-05 17:07:30 +0200484 if (strstr(kctl->id.name, "Playback")) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (ply_substream == NULL) {
486 snd_ctl_free_one(kctl);
487 ak4114->kctls[idx] = NULL;
488 continue;
489 }
490 kctl->id.device = ply_substream->pcm->device;
491 kctl->id.subdevice = ply_substream->number;
492 } else {
493 kctl->id.device = cap_substream->pcm->device;
494 kctl->id.subdevice = cap_substream->number;
495 }
496 err = snd_ctl_add(ak4114->card, kctl);
497 if (err < 0)
498 return err;
499 ak4114->kctls[idx] = kctl;
500 }
Pavel Hofmanfdafad62008-02-11 14:48:06 +0100501 snd_ak4114_proc_init(ak4114);
Takashi Iwai51354ae2007-03-30 15:38:39 +0200502 /* trigger workq */
503 schedule_delayed_work(&ak4114->work, HZ / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return 0;
505}
506
Takashi Iwai51354ae2007-03-30 15:38:39 +0200507/* notify kcontrols if any parameters are changed */
508static void ak4114_notify(struct ak4114 *ak4114,
509 unsigned char rcs0, unsigned char rcs1,
510 unsigned char c0, unsigned char c1)
511{
512 if (!ak4114->kctls[0])
513 return;
514
515 if (rcs0 & AK4114_PAR)
516 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
517 &ak4114->kctls[0]->id);
518 if (rcs0 & AK4114_V)
519 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
520 &ak4114->kctls[1]->id);
521 if (rcs1 & AK4114_CCRC)
522 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
523 &ak4114->kctls[2]->id);
524 if (rcs1 & AK4114_QCRC)
525 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
526 &ak4114->kctls[3]->id);
527
528 /* rate change */
529 if (c1 & 0xf0)
530 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
531 &ak4114->kctls[4]->id);
532
533 if ((c0 & AK4114_PEM) | (c0 & AK4114_CINT))
534 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
535 &ak4114->kctls[9]->id);
536 if (c0 & AK4114_QINT)
537 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
538 &ak4114->kctls[10]->id);
539
540 if (c0 & AK4114_AUDION)
541 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
542 &ak4114->kctls[11]->id);
543 if (c0 & AK4114_AUTO)
544 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
545 &ak4114->kctls[12]->id);
546 if (c0 & AK4114_DTSCD)
547 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
548 &ak4114->kctls[13]->id);
Pavel Hofmanc4116ae2007-04-05 17:07:30 +0200549 if (c0 & AK4114_UNLCK)
550 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
551 &ak4114->kctls[14]->id);
Takashi Iwai51354ae2007-03-30 15:38:39 +0200552}
553
Takashi Iwai97f02e02005-11-17 14:17:19 +0100554int snd_ak4114_external_rate(struct ak4114 *ak4114)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 unsigned char rcs1;
557
558 rcs1 = reg_read(ak4114, AK4114_REG_RCS1);
559 return external_rate(rcs1);
560}
561
Takashi Iwai97f02e02005-11-17 14:17:19 +0100562int snd_ak4114_check_rate_and_errors(struct ak4114 *ak4114, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100564 struct snd_pcm_runtime *runtime = ak4114->capture_substream ? ak4114->capture_substream->runtime : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 unsigned long _flags;
566 int res = 0;
567 unsigned char rcs0, rcs1;
568 unsigned char c0, c1;
569
570 rcs1 = reg_read(ak4114, AK4114_REG_RCS1);
571 if (flags & AK4114_CHECK_NO_STAT)
572 goto __rate;
573 rcs0 = reg_read(ak4114, AK4114_REG_RCS0);
574 spin_lock_irqsave(&ak4114->lock, _flags);
575 if (rcs0 & AK4114_PAR)
576 ak4114->parity_errors++;
577 if (rcs1 & AK4114_V)
578 ak4114->v_bit_errors++;
579 if (rcs1 & AK4114_CCRC)
580 ak4114->ccrc_errors++;
581 if (rcs1 & AK4114_QCRC)
582 ak4114->qcrc_errors++;
583 c0 = (ak4114->rcs0 & (AK4114_QINT | AK4114_CINT | AK4114_PEM | AK4114_AUDION | AK4114_AUTO | AK4114_UNLCK)) ^
584 (rcs0 & (AK4114_QINT | AK4114_CINT | AK4114_PEM | AK4114_AUDION | AK4114_AUTO | AK4114_UNLCK));
585 c1 = (ak4114->rcs1 & 0xf0) ^ (rcs1 & 0xf0);
586 ak4114->rcs0 = rcs0 & ~(AK4114_QINT | AK4114_CINT);
587 ak4114->rcs1 = rcs1;
588 spin_unlock_irqrestore(&ak4114->lock, _flags);
589
Takashi Iwai51354ae2007-03-30 15:38:39 +0200590 ak4114_notify(ak4114, rcs0, rcs1, c0, c1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (ak4114->change_callback && (c0 | c1) != 0)
592 ak4114->change_callback(ak4114, c0, c1);
593
594 __rate:
595 /* compare rate */
596 res = external_rate(rcs1);
597 if (!(flags & AK4114_CHECK_NO_RATE) && runtime && runtime->rate != res) {
598 snd_pcm_stream_lock_irqsave(ak4114->capture_substream, _flags);
599 if (snd_pcm_running(ak4114->capture_substream)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200600 // printk(KERN_DEBUG "rate changed (%i <- %i)\n", runtime->rate, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 snd_pcm_stop(ak4114->capture_substream, SNDRV_PCM_STATE_DRAINING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 res = 1;
603 }
604 snd_pcm_stream_unlock_irqrestore(ak4114->capture_substream, _flags);
605 }
606 return res;
607}
608
David Howellsc4028952006-11-22 14:57:56 +0000609static void ak4114_stats(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
David Howellsc4028952006-11-22 14:57:56 +0000611 struct ak4114 *chip = container_of(work, struct ak4114, work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Takashi Iwai4161b452015-01-13 10:53:20 +0100613 if (atomic_inc_return(&chip->wq_processing) == 1)
Pavel Hofman841b23d2008-03-17 08:45:33 +0100614 snd_ak4114_check_rate_and_errors(chip, chip->check_flags);
Takashi Iwai4161b452015-01-13 10:53:20 +0100615 if (atomic_dec_and_test(&chip->wq_processing))
616 schedule_delayed_work(&chip->work, HZ / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619EXPORT_SYMBOL(snd_ak4114_create);
620EXPORT_SYMBOL(snd_ak4114_reg_write);
621EXPORT_SYMBOL(snd_ak4114_reinit);
622EXPORT_SYMBOL(snd_ak4114_build);
623EXPORT_SYMBOL(snd_ak4114_external_rate);
624EXPORT_SYMBOL(snd_ak4114_check_rate_and_errors);