blob: 8f7c42c036eb25d9568f074bf04636121cd924a7 [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
4 * Copyright (c) by Jaroslav Kysela <perex@suse.cz>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <sound/driver.h>
24#include <linux/slab.h>
25#include <linux/delay.h>
26#include <sound/core.h>
27#include <sound/control.h>
28#include <sound/pcm.h>
29#include <sound/ak4114.h>
30#include <sound/asoundef.h>
31
32MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
33MODULE_DESCRIPTION("AK4114 IEC958 (S/PDIF) receiver by Asahi Kasei");
34MODULE_LICENSE("GPL");
35
36#define AK4114_ADDR 0x00 /* fixed address */
37
David Howellsc4028952006-11-22 14:57:56 +000038static void ak4114_stats(struct work_struct *work);
Takashi Iwai51354ae2007-03-30 15:38:39 +020039static void ak4114_init_regs(struct ak4114 *chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Takashi Iwai97f02e02005-11-17 14:17:19 +010041static void reg_write(struct ak4114 *ak4114, unsigned char reg, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
43 ak4114->write(ak4114->private_data, reg, val);
44 if (reg <= AK4114_REG_INT1_MASK)
45 ak4114->regmap[reg] = val;
Jean Delvare1ab774e2007-02-05 13:07:04 +010046 else if (reg >= AK4114_REG_TXCSB0 && reg <= AK4114_REG_TXCSB4)
47 ak4114->txcsb[reg-AK4114_REG_TXCSB0] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Takashi Iwai97f02e02005-11-17 14:17:19 +010050static inline unsigned char reg_read(struct ak4114 *ak4114, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 return ak4114->read(ak4114->private_data, reg);
53}
54
55#if 0
Takashi Iwai97f02e02005-11-17 14:17:19 +010056static void reg_dump(struct ak4114 *ak4114)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 int i;
59
Takashi Iwai99b359b2005-10-20 18:26:44 +020060 printk(KERN_DEBUG "AK4114 REG DUMP:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 for (i = 0; i < 0x20; i++)
Takashi Iwai99b359b2005-10-20 18:26:44 +020062 printk(KERN_DEBUG "reg[%02x] = %02x (%02x)\n", i, reg_read(ak4114, i), i < sizeof(ak4114->regmap) ? ak4114->regmap[i] : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64#endif
65
Takashi Iwai97f02e02005-11-17 14:17:19 +010066static void snd_ak4114_free(struct ak4114 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
68 chip->init = 1; /* don't schedule new work */
69 mb();
Takashi Iwai3b6baa52007-01-31 14:34:38 +010070 cancel_delayed_work(&chip->work);
Takashi Iwai4014c382006-12-19 17:13:16 +010071 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 kfree(chip);
73}
74
Takashi Iwai97f02e02005-11-17 14:17:19 +010075static int snd_ak4114_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Takashi Iwai97f02e02005-11-17 14:17:19 +010077 struct ak4114 *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 snd_ak4114_free(chip);
79 return 0;
80}
81
Takashi Iwai97f02e02005-11-17 14:17:19 +010082int snd_ak4114_create(struct snd_card *card,
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 ak4114_read_t *read, ak4114_write_t *write,
Takashi Iwai517400c2007-01-29 15:27:56 +010084 const unsigned char pgm[7], const unsigned char txcsb[5],
Takashi Iwai97f02e02005-11-17 14:17:19 +010085 void *private_data, struct ak4114 **r_ak4114)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Takashi Iwai97f02e02005-11-17 14:17:19 +010087 struct ak4114 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 int err = 0;
89 unsigned char reg;
Takashi Iwai97f02e02005-11-17 14:17:19 +010090 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 .dev_free = snd_ak4114_dev_free,
92 };
93
Takashi Iwai561b2202005-09-09 14:22:34 +020094 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (chip == NULL)
96 return -ENOMEM;
97 spin_lock_init(&chip->lock);
98 chip->card = card;
99 chip->read = read;
100 chip->write = write;
101 chip->private_data = private_data;
Takashi Iwai3b6baa52007-01-31 14:34:38 +0100102 INIT_DELAYED_WORK(&chip->work, ak4114_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 for (reg = 0; reg < 7; reg++)
105 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
114 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
115 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);
145 for (reg = 1; reg < 7; reg++)
146 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{
155 chip->init = 1;
156 mb();
157 flush_scheduled_work();
158 ak4114_init_regs(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* bring up statistics / event queing */
160 chip->init = 0;
Takashi Iwai51354ae2007-03-30 15:38:39 +0200161 if (chip->kctls[0])
162 schedule_delayed_work(&chip->work, HZ / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165static unsigned int external_rate(unsigned char rcs1)
166{
167 switch (rcs1 & (AK4114_FS0|AK4114_FS1|AK4114_FS2|AK4114_FS3)) {
168 case AK4114_FS_32000HZ: return 32000;
169 case AK4114_FS_44100HZ: return 44100;
170 case AK4114_FS_48000HZ: return 48000;
171 case AK4114_FS_88200HZ: return 88200;
172 case AK4114_FS_96000HZ: return 96000;
173 case AK4114_FS_176400HZ: return 176400;
174 case AK4114_FS_192000HZ: return 192000;
175 default: return 0;
176 }
177}
178
Takashi Iwai97f02e02005-11-17 14:17:19 +0100179static int snd_ak4114_in_error_info(struct snd_kcontrol *kcontrol,
180 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
183 uinfo->count = 1;
184 uinfo->value.integer.min = 0;
185 uinfo->value.integer.max = LONG_MAX;
186 return 0;
187}
188
Takashi Iwai97f02e02005-11-17 14:17:19 +0100189static int snd_ak4114_in_error_get(struct snd_kcontrol *kcontrol,
190 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100192 struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 long *ptr;
194
195 spin_lock_irq(&chip->lock);
196 ptr = (long *)(((char *)chip) + kcontrol->private_value);
197 ucontrol->value.integer.value[0] = *ptr;
198 *ptr = 0;
199 spin_unlock_irq(&chip->lock);
200 return 0;
201}
202
Takashi Iwai97f02e02005-11-17 14:17:19 +0100203static int snd_ak4114_in_bit_info(struct snd_kcontrol *kcontrol,
204 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
207 uinfo->count = 1;
208 uinfo->value.integer.min = 0;
209 uinfo->value.integer.max = 1;
210 return 0;
211}
212
Takashi Iwai97f02e02005-11-17 14:17:19 +0100213static int snd_ak4114_in_bit_get(struct snd_kcontrol *kcontrol,
214 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100216 struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 unsigned char reg = kcontrol->private_value & 0xff;
218 unsigned char bit = (kcontrol->private_value >> 8) & 0xff;
219 unsigned char inv = (kcontrol->private_value >> 31) & 1;
220
221 ucontrol->value.integer.value[0] = ((reg_read(chip, reg) & (1 << bit)) ? 1 : 0) ^ inv;
222 return 0;
223}
224
Takashi Iwai97f02e02005-11-17 14:17:19 +0100225static int snd_ak4114_rate_info(struct snd_kcontrol *kcontrol,
226 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
229 uinfo->count = 1;
230 uinfo->value.integer.min = 0;
231 uinfo->value.integer.max = 192000;
232 return 0;
233}
234
Takashi Iwai97f02e02005-11-17 14:17:19 +0100235static int snd_ak4114_rate_get(struct snd_kcontrol *kcontrol,
236 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100238 struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 ucontrol->value.integer.value[0] = external_rate(reg_read(chip, AK4114_REG_RCS1));
241 return 0;
242}
243
Takashi Iwai97f02e02005-11-17 14:17:19 +0100244static int snd_ak4114_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
247 uinfo->count = 1;
248 return 0;
249}
250
Takashi Iwai97f02e02005-11-17 14:17:19 +0100251static int snd_ak4114_spdif_get(struct snd_kcontrol *kcontrol,
252 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100254 struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 unsigned i;
256
257 for (i = 0; i < AK4114_REG_RXCSB_SIZE; i++)
258 ucontrol->value.iec958.status[i] = reg_read(chip, AK4114_REG_RXCSB0 + i);
259 return 0;
260}
261
Takashi Iwai97f02e02005-11-17 14:17:19 +0100262static int snd_ak4114_spdif_playback_get(struct snd_kcontrol *kcontrol,
263 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100265 struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 unsigned i;
267
268 for (i = 0; i < AK4114_REG_TXCSB_SIZE; i++)
269 ucontrol->value.iec958.status[i] = chip->txcsb[i];
270 return 0;
271}
272
Takashi Iwai97f02e02005-11-17 14:17:19 +0100273static int snd_ak4114_spdif_playback_put(struct snd_kcontrol *kcontrol,
274 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100276 struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 unsigned i;
278
279 for (i = 0; i < AK4114_REG_TXCSB_SIZE; i++)
280 reg_write(chip, AK4114_REG_TXCSB0 + i, ucontrol->value.iec958.status[i]);
281 return 0;
282}
283
Takashi Iwai97f02e02005-11-17 14:17:19 +0100284static int snd_ak4114_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
286 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
287 uinfo->count = 1;
288 return 0;
289}
290
Takashi Iwai97f02e02005-11-17 14:17:19 +0100291static int snd_ak4114_spdif_mask_get(struct snd_kcontrol *kcontrol,
292 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 memset(ucontrol->value.iec958.status, 0xff, AK4114_REG_RXCSB_SIZE);
295 return 0;
296}
297
Takashi Iwai97f02e02005-11-17 14:17:19 +0100298static int snd_ak4114_spdif_pinfo(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
301 uinfo->value.integer.min = 0;
302 uinfo->value.integer.max = 0xffff;
303 uinfo->count = 4;
304 return 0;
305}
306
Takashi Iwai97f02e02005-11-17 14:17:19 +0100307static int snd_ak4114_spdif_pget(struct snd_kcontrol *kcontrol,
308 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100310 struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 unsigned short tmp;
312
313 ucontrol->value.integer.value[0] = 0xf8f2;
314 ucontrol->value.integer.value[1] = 0x4e1f;
315 tmp = reg_read(chip, AK4114_REG_Pc0) | (reg_read(chip, AK4114_REG_Pc1) << 8);
316 ucontrol->value.integer.value[2] = tmp;
317 tmp = reg_read(chip, AK4114_REG_Pd0) | (reg_read(chip, AK4114_REG_Pd1) << 8);
318 ucontrol->value.integer.value[3] = tmp;
319 return 0;
320}
321
Takashi Iwai97f02e02005-11-17 14:17:19 +0100322static int snd_ak4114_spdif_qinfo(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
325 uinfo->count = AK4114_REG_QSUB_SIZE;
326 return 0;
327}
328
Takashi Iwai97f02e02005-11-17 14:17:19 +0100329static int snd_ak4114_spdif_qget(struct snd_kcontrol *kcontrol,
330 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100332 struct ak4114 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 unsigned i;
334
335 for (i = 0; i < AK4114_REG_QSUB_SIZE; i++)
336 ucontrol->value.bytes.data[i] = reg_read(chip, AK4114_REG_QSUB_ADDR + i);
337 return 0;
338}
339
340/* Don't forget to change AK4114_CONTROLS define!!! */
Takashi Iwai97f02e02005-11-17 14:17:19 +0100341static struct snd_kcontrol_new snd_ak4114_iec958_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
344 .name = "IEC958 Parity Errors",
345 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
346 .info = snd_ak4114_in_error_info,
347 .get = snd_ak4114_in_error_get,
Takashi Iwai97f02e02005-11-17 14:17:19 +0100348 .private_value = offsetof(struct ak4114, parity_errors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349},
350{
351 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
352 .name = "IEC958 V-Bit Errors",
353 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
354 .info = snd_ak4114_in_error_info,
355 .get = snd_ak4114_in_error_get,
Takashi Iwai97f02e02005-11-17 14:17:19 +0100356 .private_value = offsetof(struct ak4114, v_bit_errors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357},
358{
359 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
360 .name = "IEC958 C-CRC Errors",
361 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
362 .info = snd_ak4114_in_error_info,
363 .get = snd_ak4114_in_error_get,
Takashi Iwai97f02e02005-11-17 14:17:19 +0100364 .private_value = offsetof(struct ak4114, ccrc_errors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365},
366{
367 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
368 .name = "IEC958 Q-CRC Errors",
369 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
370 .info = snd_ak4114_in_error_info,
371 .get = snd_ak4114_in_error_get,
Takashi Iwai97f02e02005-11-17 14:17:19 +0100372 .private_value = offsetof(struct ak4114, qcrc_errors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373},
374{
375 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
376 .name = "IEC958 External Rate",
377 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
378 .info = snd_ak4114_rate_info,
379 .get = snd_ak4114_rate_get,
380},
381{
382 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
383 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
384 .access = SNDRV_CTL_ELEM_ACCESS_READ,
385 .info = snd_ak4114_spdif_mask_info,
386 .get = snd_ak4114_spdif_mask_get,
387},
388{
389 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
390 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
391 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
392 .info = snd_ak4114_spdif_info,
393 .get = snd_ak4114_spdif_playback_get,
394 .put = snd_ak4114_spdif_playback_put,
395},
396{
397 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
398 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,MASK),
399 .access = SNDRV_CTL_ELEM_ACCESS_READ,
400 .info = snd_ak4114_spdif_mask_info,
401 .get = snd_ak4114_spdif_mask_get,
402},
403{
404 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
405 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
406 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
407 .info = snd_ak4114_spdif_info,
408 .get = snd_ak4114_spdif_get,
409},
410{
411 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
412 .name = "IEC958 Preample Capture Default",
413 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
414 .info = snd_ak4114_spdif_pinfo,
415 .get = snd_ak4114_spdif_pget,
416},
417{
418 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
419 .name = "IEC958 Q-subcode Capture Default",
420 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
421 .info = snd_ak4114_spdif_qinfo,
422 .get = snd_ak4114_spdif_qget,
423},
424{
425 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
426 .name = "IEC958 Audio",
427 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
428 .info = snd_ak4114_in_bit_info,
429 .get = snd_ak4114_in_bit_get,
430 .private_value = (1<<31) | (1<<8) | AK4114_REG_RCS0,
431},
432{
433 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
434 .name = "IEC958 Non-PCM Bitstream",
435 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
436 .info = snd_ak4114_in_bit_info,
437 .get = snd_ak4114_in_bit_get,
438 .private_value = (6<<8) | AK4114_REG_RCS1,
439},
440{
441 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
442 .name = "IEC958 DTS Bitstream",
443 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
444 .info = snd_ak4114_in_bit_info,
445 .get = snd_ak4114_in_bit_get,
446 .private_value = (3<<8) | AK4114_REG_RCS1,
447}
448};
449
Takashi Iwai97f02e02005-11-17 14:17:19 +0100450int snd_ak4114_build(struct ak4114 *ak4114,
451 struct snd_pcm_substream *ply_substream,
452 struct snd_pcm_substream *cap_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100454 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 unsigned int idx;
456 int err;
457
458 snd_assert(cap_substream, return -EINVAL);
459 ak4114->playback_substream = ply_substream;
460 ak4114->capture_substream = cap_substream;
461 for (idx = 0; idx < AK4114_CONTROLS; idx++) {
462 kctl = snd_ctl_new1(&snd_ak4114_iec958_controls[idx], ak4114);
463 if (kctl == NULL)
464 return -ENOMEM;
465 if (!strstr(kctl->id.name, "Playback")) {
466 if (ply_substream == NULL) {
467 snd_ctl_free_one(kctl);
468 ak4114->kctls[idx] = NULL;
469 continue;
470 }
471 kctl->id.device = ply_substream->pcm->device;
472 kctl->id.subdevice = ply_substream->number;
473 } else {
474 kctl->id.device = cap_substream->pcm->device;
475 kctl->id.subdevice = cap_substream->number;
476 }
477 err = snd_ctl_add(ak4114->card, kctl);
478 if (err < 0)
479 return err;
480 ak4114->kctls[idx] = kctl;
481 }
Takashi Iwai51354ae2007-03-30 15:38:39 +0200482 /* trigger workq */
483 schedule_delayed_work(&ak4114->work, HZ / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return 0;
485}
486
Takashi Iwai51354ae2007-03-30 15:38:39 +0200487/* notify kcontrols if any parameters are changed */
488static void ak4114_notify(struct ak4114 *ak4114,
489 unsigned char rcs0, unsigned char rcs1,
490 unsigned char c0, unsigned char c1)
491{
492 if (!ak4114->kctls[0])
493 return;
494
495 if (rcs0 & AK4114_PAR)
496 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
497 &ak4114->kctls[0]->id);
498 if (rcs0 & AK4114_V)
499 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
500 &ak4114->kctls[1]->id);
501 if (rcs1 & AK4114_CCRC)
502 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
503 &ak4114->kctls[2]->id);
504 if (rcs1 & AK4114_QCRC)
505 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
506 &ak4114->kctls[3]->id);
507
508 /* rate change */
509 if (c1 & 0xf0)
510 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
511 &ak4114->kctls[4]->id);
512
513 if ((c0 & AK4114_PEM) | (c0 & AK4114_CINT))
514 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
515 &ak4114->kctls[9]->id);
516 if (c0 & AK4114_QINT)
517 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
518 &ak4114->kctls[10]->id);
519
520 if (c0 & AK4114_AUDION)
521 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
522 &ak4114->kctls[11]->id);
523 if (c0 & AK4114_AUTO)
524 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
525 &ak4114->kctls[12]->id);
526 if (c0 & AK4114_DTSCD)
527 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE,
528 &ak4114->kctls[13]->id);
529}
530
Takashi Iwai97f02e02005-11-17 14:17:19 +0100531int snd_ak4114_external_rate(struct ak4114 *ak4114)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 unsigned char rcs1;
534
535 rcs1 = reg_read(ak4114, AK4114_REG_RCS1);
536 return external_rate(rcs1);
537}
538
Takashi Iwai97f02e02005-11-17 14:17:19 +0100539int snd_ak4114_check_rate_and_errors(struct ak4114 *ak4114, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100541 struct snd_pcm_runtime *runtime = ak4114->capture_substream ? ak4114->capture_substream->runtime : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 unsigned long _flags;
543 int res = 0;
544 unsigned char rcs0, rcs1;
545 unsigned char c0, c1;
546
547 rcs1 = reg_read(ak4114, AK4114_REG_RCS1);
548 if (flags & AK4114_CHECK_NO_STAT)
549 goto __rate;
550 rcs0 = reg_read(ak4114, AK4114_REG_RCS0);
551 spin_lock_irqsave(&ak4114->lock, _flags);
552 if (rcs0 & AK4114_PAR)
553 ak4114->parity_errors++;
554 if (rcs1 & AK4114_V)
555 ak4114->v_bit_errors++;
556 if (rcs1 & AK4114_CCRC)
557 ak4114->ccrc_errors++;
558 if (rcs1 & AK4114_QCRC)
559 ak4114->qcrc_errors++;
560 c0 = (ak4114->rcs0 & (AK4114_QINT | AK4114_CINT | AK4114_PEM | AK4114_AUDION | AK4114_AUTO | AK4114_UNLCK)) ^
561 (rcs0 & (AK4114_QINT | AK4114_CINT | AK4114_PEM | AK4114_AUDION | AK4114_AUTO | AK4114_UNLCK));
562 c1 = (ak4114->rcs1 & 0xf0) ^ (rcs1 & 0xf0);
563 ak4114->rcs0 = rcs0 & ~(AK4114_QINT | AK4114_CINT);
564 ak4114->rcs1 = rcs1;
565 spin_unlock_irqrestore(&ak4114->lock, _flags);
566
Takashi Iwai51354ae2007-03-30 15:38:39 +0200567 ak4114_notify(ak4114, rcs0, rcs1, c0, c1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (ak4114->change_callback && (c0 | c1) != 0)
569 ak4114->change_callback(ak4114, c0, c1);
570
571 __rate:
572 /* compare rate */
573 res = external_rate(rcs1);
574 if (!(flags & AK4114_CHECK_NO_RATE) && runtime && runtime->rate != res) {
575 snd_pcm_stream_lock_irqsave(ak4114->capture_substream, _flags);
576 if (snd_pcm_running(ak4114->capture_substream)) {
Takashi Iwai99b359b2005-10-20 18:26:44 +0200577 // printk(KERN_DEBUG "rate changed (%i <- %i)\n", runtime->rate, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 snd_pcm_stop(ak4114->capture_substream, SNDRV_PCM_STATE_DRAINING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 res = 1;
580 }
581 snd_pcm_stream_unlock_irqrestore(ak4114->capture_substream, _flags);
582 }
583 return res;
584}
585
David Howellsc4028952006-11-22 14:57:56 +0000586static void ak4114_stats(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
David Howellsc4028952006-11-22 14:57:56 +0000588 struct ak4114 *chip = container_of(work, struct ak4114, work.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 if (chip->init)
Takashi Iwai51354ae2007-03-30 15:38:39 +0200591 snd_ak4114_check_rate_and_errors(chip, 0);
592
Takashi Iwai4014c382006-12-19 17:13:16 +0100593 schedule_delayed_work(&chip->work, HZ / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
596EXPORT_SYMBOL(snd_ak4114_create);
597EXPORT_SYMBOL(snd_ak4114_reg_write);
598EXPORT_SYMBOL(snd_ak4114_reinit);
599EXPORT_SYMBOL(snd_ak4114_build);
600EXPORT_SYMBOL(snd_ak4114_external_rate);
601EXPORT_SYMBOL(snd_ak4114_check_rate_and_errors);