blob: 48848909a5a95f6776e262788755b0501ec59563 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Routines for control of the AK4117 via 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/ak4117.h>
30#include <sound/asoundef.h>
31
Jaroslav Kyselac1017a42007-10-15 09:50:19 +020032MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
Linus Torvalds1da177e2005-04-16 15:20:36 -070033MODULE_DESCRIPTION("AK4117 IEC958 (S/PDIF) receiver by Asahi Kasei");
34MODULE_LICENSE("GPL");
35
36#define AK4117_ADDR 0x00 /* fixed address */
37
38static void snd_ak4117_timer(unsigned long data);
39
Takashi Iwai97f02e02005-11-17 14:17:19 +010040static void reg_write(struct ak4117 *ak4117, unsigned char reg, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
42 ak4117->write(ak4117->private_data, reg, val);
43 if (reg < sizeof(ak4117->regmap))
44 ak4117->regmap[reg] = val;
45}
46
Takashi Iwai97f02e02005-11-17 14:17:19 +010047static inline unsigned char reg_read(struct ak4117 *ak4117, unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 return ak4117->read(ak4117->private_data, reg);
50}
51
52#if 0
Takashi Iwai97f02e02005-11-17 14:17:19 +010053static void reg_dump(struct ak4117 *ak4117)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 int i;
56
Takashi Iwai99b359b2005-10-20 18:26:44 +020057 printk(KERN_DEBUG "AK4117 REG DUMP:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 for (i = 0; i < 0x1b; i++)
Takashi Iwai99b359b2005-10-20 18:26:44 +020059 printk(KERN_DEBUG "reg[%02x] = %02x (%02x)\n", i, reg_read(ak4117, i), i < sizeof(ak4117->regmap) ? ak4117->regmap[i] : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61#endif
62
Takashi Iwai97f02e02005-11-17 14:17:19 +010063static void snd_ak4117_free(struct ak4117 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Kirill Tkhai115b94d2014-02-14 15:47:57 +040065 del_timer_sync(&chip->timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 kfree(chip);
67}
68
Takashi Iwai97f02e02005-11-17 14:17:19 +010069static int snd_ak4117_dev_free(struct snd_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Takashi Iwai97f02e02005-11-17 14:17:19 +010071 struct ak4117 *chip = device->device_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 snd_ak4117_free(chip);
73 return 0;
74}
75
Takashi Iwai97f02e02005-11-17 14:17:19 +010076int snd_ak4117_create(struct snd_card *card, ak4117_read_t *read, ak4117_write_t *write,
Takashi Iwai517400c2007-01-29 15:27:56 +010077 const unsigned char pgm[5], void *private_data, struct ak4117 **r_ak4117)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Takashi Iwai97f02e02005-11-17 14:17:19 +010079 struct ak4117 *chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 int err = 0;
81 unsigned char reg;
Takashi Iwai97f02e02005-11-17 14:17:19 +010082 static struct snd_device_ops ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 .dev_free = snd_ak4117_dev_free,
84 };
85
Takashi Iwai561b2202005-09-09 14:22:34 +020086 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 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 Iwai867a0e02015-01-19 11:29:25 +010094 setup_timer(&chip->timer, snd_ak4117_timer, (unsigned long)chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
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);
113 return err < 0 ? err : -EIO;
114}
115
Takashi Iwai97f02e02005-11-17 14:17:19 +0100116void snd_ak4117_reg_write(struct ak4117 *chip, unsigned char reg, unsigned char mask, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 if (reg >= 5)
119 return;
120 reg_write(chip, reg, (chip->regmap[reg] & ~mask) | val);
121}
122
Takashi Iwai97f02e02005-11-17 14:17:19 +0100123void snd_ak4117_reinit(struct ak4117 *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
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 Iwai867a0e02015-01-19 11:29:25 +0100140 mod_timer(&chip->timer, 1 + jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143static 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 Iwai97f02e02005-11-17 14:17:19 +0100157static int snd_ak4117_in_error_info(struct snd_kcontrol *kcontrol,
158 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
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 Iwai97f02e02005-11-17 14:17:19 +0100167static int snd_ak4117_in_error_get(struct snd_kcontrol *kcontrol,
168 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100170 struct ak4117 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 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 Iwaia5ce8892007-07-23 15:42:26 +0200181#define snd_ak4117_in_bit_info snd_ctl_boolean_mono_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Takashi Iwai97f02e02005-11-17 14:17:19 +0100183static int snd_ak4117_in_bit_get(struct snd_kcontrol *kcontrol,
184 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100186 struct ak4117 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 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 Iwai97f02e02005-11-17 14:17:19 +0100195static int snd_ak4117_rx_info(struct snd_kcontrol *kcontrol,
196 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
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 Iwai97f02e02005-11-17 14:17:19 +0100205static int snd_ak4117_rx_get(struct snd_kcontrol *kcontrol,
206 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100208 struct ak4117 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 ucontrol->value.integer.value[0] = (chip->regmap[AK4117_REG_IO] & AK4117_IPS) ? 1 : 0;
211 return 0;
212}
213
Takashi Iwai97f02e02005-11-17 14:17:19 +0100214static int snd_ak4117_rx_put(struct snd_kcontrol *kcontrol,
215 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100217 struct ak4117 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 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 Iwai97f02e02005-11-17 14:17:19 +0100230static int snd_ak4117_rate_info(struct snd_kcontrol *kcontrol,
231 struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
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 Iwai97f02e02005-11-17 14:17:19 +0100240static int snd_ak4117_rate_get(struct snd_kcontrol *kcontrol,
241 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100243 struct ak4117 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 ucontrol->value.integer.value[0] = external_rate(reg_read(chip, AK4117_REG_RCS1));
246 return 0;
247}
248
Takashi Iwai97f02e02005-11-17 14:17:19 +0100249static int snd_ak4117_spdif_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
252 uinfo->count = 1;
253 return 0;
254}
255
Takashi Iwai97f02e02005-11-17 14:17:19 +0100256static int snd_ak4117_spdif_get(struct snd_kcontrol *kcontrol,
257 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100259 struct ak4117 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 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 Iwai97f02e02005-11-17 14:17:19 +0100267static int snd_ak4117_spdif_mask_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
270 uinfo->count = 1;
271 return 0;
272}
273
Takashi Iwai97f02e02005-11-17 14:17:19 +0100274static int snd_ak4117_spdif_mask_get(struct snd_kcontrol *kcontrol,
275 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 memset(ucontrol->value.iec958.status, 0xff, AK4117_REG_RXCSB_SIZE);
278 return 0;
279}
280
Takashi Iwai97f02e02005-11-17 14:17:19 +0100281static int snd_ak4117_spdif_pinfo(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
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 Iwai97f02e02005-11-17 14:17:19 +0100290static int snd_ak4117_spdif_pget(struct snd_kcontrol *kcontrol,
291 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100293 struct ak4117 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 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 Iwai97f02e02005-11-17 14:17:19 +0100305static int snd_ak4117_spdif_qinfo(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
308 uinfo->count = AK4117_REG_QSUB_SIZE;
309 return 0;
310}
311
Takashi Iwai97f02e02005-11-17 14:17:19 +0100312static int snd_ak4117_spdif_qget(struct snd_kcontrol *kcontrol,
313 struct snd_ctl_elem_value *ucontrol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100315 struct ak4117 *chip = snd_kcontrol_chip(kcontrol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 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 Iwai97f02e02005-11-17 14:17:19 +0100324static struct snd_kcontrol_new snd_ak4117_iec958_controls[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
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 Iwai97f02e02005-11-17 14:17:19 +0100331 .private_value = offsetof(struct ak4117, parity_errors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332},
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 Iwai97f02e02005-11-17 14:17:19 +0100339 .private_value = offsetof(struct ak4117, v_bit_errors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340},
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 Iwai97f02e02005-11-17 14:17:19 +0100347 .private_value = offsetof(struct ak4117, ccrc_errors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348},
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 Iwai97f02e02005-11-17 14:17:19 +0100355 .private_value = offsetof(struct ak4117, qcrc_errors),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356},
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 Iidaec8f53f2012-11-02 00:28:50 +0900380 .name = "IEC958 Preamble Capture Default",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 .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 Iwai97f02e02005-11-17 14:17:19 +0100426int snd_ak4117_build(struct ak4117 *ak4117, struct snd_pcm_substream *cap_substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100428 struct snd_kcontrol *kctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 unsigned int idx;
430 int err;
431
Takashi Iwai5e246b82008-08-08 17:12:47 +0200432 if (snd_BUG_ON(!cap_substream))
433 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 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 Iwai97f02e02005-11-17 14:17:19 +0100449int snd_ak4117_external_rate(struct ak4117 *ak4117)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 unsigned char rcs1;
452
453 rcs1 = reg_read(ak4117, AK4117_REG_RCS1);
454 return external_rate(rcs1);
455}
456
Takashi Iwai97f02e02005-11-17 14:17:19 +0100457int snd_ak4117_check_rate_and_errors(struct ak4117 *ak4117, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100459 struct snd_pcm_runtime *runtime = ak4117->substream ? ak4117->substream->runtime : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 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 Iwai99b359b2005-10-20 18:26:44 +0200470 // printk(KERN_DEBUG "AK IRQ: rcs0 = 0x%x, rcs1 = 0x%x, rcs2 = 0x%x\n", rcs0, rcs1, rcs2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 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 Iwai99b359b2005-10-20 18:26:44 +0200523 // printk(KERN_DEBUG "rate changed (%i <- %i)\n", runtime->rate, res);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 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
533static void snd_ak4117_timer(unsigned long data)
534{
Takashi Iwai97f02e02005-11-17 14:17:19 +0100535 struct ak4117 *chip = (struct ak4117 *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (chip->init)
538 return;
539 snd_ak4117_check_rate_and_errors(chip, 0);
Takashi Iwai867a0e02015-01-19 11:29:25 +0100540 mod_timer(&chip->timer, 1 + jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
543EXPORT_SYMBOL(snd_ak4117_create);
544EXPORT_SYMBOL(snd_ak4117_reg_write);
545EXPORT_SYMBOL(snd_ak4117_reinit);
546EXPORT_SYMBOL(snd_ak4117_build);
547EXPORT_SYMBOL(snd_ak4117_external_rate);
548EXPORT_SYMBOL(snd_ak4117_check_rate_and_errors);