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