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