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