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