aboutsummaryrefslogtreecommitdiffstats
path: root/sound/i2c/other
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /sound/i2c/other
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'sound/i2c/other')
-rw-r--r--sound/i2c/other/Makefile16
-rw-r--r--sound/i2c/other/ak4114.c580
-rw-r--r--sound/i2c/other/ak4117.c559
-rw-r--r--sound/i2c/other/ak4xxx-adda.c501
-rw-r--r--sound/i2c/other/tea575x-tuner.c233
5 files changed, 1889 insertions, 0 deletions
diff --git a/sound/i2c/other/Makefile b/sound/i2c/other/Makefile
new file mode 100644
index 000000000000..2fe023ef00a7
--- /dev/null
+++ b/sound/i2c/other/Makefile
@@ -0,0 +1,16 @@
1#
2# Makefile for ALSA
3# Copyright (c) 2003 by Jaroslav Kysela <perex@suse.cz>
4#
5
6snd-ak4114-objs := ak4114.o
7snd-ak4117-objs := ak4117.o
8snd-ak4xxx-adda-objs := ak4xxx-adda.o
9snd-tea575x-tuner-objs := tea575x-tuner.o
10
11# Module Dependency
12obj-$(CONFIG_SND_PDAUDIOCF) += snd-ak4117.o
13obj-$(CONFIG_SND_ICE1712) += snd-ak4xxx-adda.o
14obj-$(CONFIG_SND_ICE1724) += snd-ak4xxx-adda.o
15obj-$(CONFIG_SND_ICE1724) += snd-ak4114.o
16obj-$(CONFIG_SND_FM801_TEA575X) += snd-tea575x-tuner.o
diff --git a/sound/i2c/other/ak4114.c b/sound/i2c/other/ak4114.c
new file mode 100644
index 000000000000..f5e6018ea3f4
--- /dev/null
+++ b/sound/i2c/other/ak4114.c
@@ -0,0 +1,580 @@
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
32MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
33MODULE_DESCRIPTION("AK4114 IEC958 (S/PDIF) receiver by Asahi Kasei");
34MODULE_LICENSE("GPL");
35
36#define AK4114_ADDR 0x00 /* fixed address */
37
38static void ak4114_stats(void *);
39
40static void reg_write(ak4114_t *ak4114, unsigned char reg, unsigned char val)
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
49static inline unsigned char reg_read(ak4114_t *ak4114, unsigned char reg)
50{
51 return ak4114->read(ak4114->private_data, reg);
52}
53
54#if 0
55static void reg_dump(ak4114_t *ak4114)
56{
57 int i;
58
59 printk("AK4114 REG DUMP:\n");
60 for (i = 0; i < 0x20; i++)
61 printk("reg[%02x] = %02x (%02x)\n", i, reg_read(ak4114, i), i < sizeof(ak4114->regmap) ? ak4114->regmap[i] : 0);
62}
63#endif
64
65static void snd_ak4114_free(ak4114_t *chip)
66{
67 chip->init = 1; /* don't schedule new work */
68 mb();
69 if (chip->workqueue != NULL) {
70 flush_workqueue(chip->workqueue);
71 destroy_workqueue(chip->workqueue);
72 }
73 kfree(chip);
74}
75
76static int snd_ak4114_dev_free(snd_device_t *device)
77{
78 ak4114_t *chip = device->device_data;
79 snd_ak4114_free(chip);
80 return 0;
81}
82
83int snd_ak4114_create(snd_card_t *card,
84 ak4114_read_t *read, ak4114_write_t *write,
85 unsigned char pgm[7], unsigned char txcsb[5],
86 void *private_data, ak4114_t **r_ak4114)
87{
88 ak4114_t *chip;
89 int err = 0;
90 unsigned char reg;
91 static snd_device_ops_t ops = {
92 .dev_free = snd_ak4114_dev_free,
93 };
94
95 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
96 if (chip == NULL)
97 return -ENOMEM;
98 spin_lock_init(&chip->lock);
99 chip->card = card;
100 chip->read = read;
101 chip->write = write;
102 chip->private_data = private_data;
103
104 for (reg = 0; reg < 7; reg++)
105 chip->regmap[reg] = pgm[reg];
106 for (reg = 0; reg < 5; reg++)
107 chip->txcsb[reg] = txcsb[reg];
108
109 chip->workqueue = create_workqueue("snd-ak4114");
110 if (chip->workqueue == NULL) {
111 kfree(chip);
112 return -ENOMEM;
113 }
114
115 snd_ak4114_reinit(chip);
116
117 chip->rcs0 = reg_read(chip, AK4114_REG_RCS0) & ~(AK4114_QINT | AK4114_CINT);
118 chip->rcs1 = reg_read(chip, AK4114_REG_RCS1);
119
120 if ((err = snd_device_new(card, SNDRV_DEV_LOWLEVEL, chip, &ops)) < 0)
121 goto __fail;
122
123 if (r_ak4114)
124 *r_ak4114 = chip;
125 return 0;
126
127 __fail:
128 snd_ak4114_free(chip);
129 return err < 0 ? err : -EIO;
130}
131
132void snd_ak4114_reg_write(ak4114_t *chip, unsigned char reg, unsigned char mask, unsigned char val)
133{
134 if (reg <= AK4114_REG_INT1_MASK)
135 reg_write(chip, reg, (chip->regmap[reg] & ~mask) | val);
136 else if (reg >= AK4114_REG_TXCSB0 && reg <= AK4114_REG_TXCSB4)
137 reg_write(chip, reg, (chip->txcsb[reg] & ~mask) | val);
138}
139
140void snd_ak4114_reinit(ak4114_t *chip)
141{
142 unsigned char old = chip->regmap[AK4114_REG_PWRDN], reg;
143
144 chip->init = 1;
145 mb();
146 flush_workqueue(chip->workqueue);
147 /* bring the chip to reset state and powerdown state */
148 reg_write(chip, AK4114_REG_PWRDN, old & ~(AK4114_RST|AK4114_PWN));
149 udelay(200);
150 /* release reset, but leave powerdown */
151 reg_write(chip, AK4114_REG_PWRDN, (old | AK4114_RST) & ~AK4114_PWN);
152 udelay(200);
153 for (reg = 1; reg < 7; reg++)
154 reg_write(chip, reg, chip->regmap[reg]);
155 for (reg = 0; reg < 5; reg++)
156 reg_write(chip, reg + AK4114_REG_TXCSB0, chip->txcsb[reg]);
157 /* release powerdown, everything is initialized now */
158 reg_write(chip, AK4114_REG_PWRDN, old | AK4114_RST | AK4114_PWN);
159 /* bring up statistics / event queing */
160 chip->init = 0;
161 INIT_WORK(&chip->work, ak4114_stats, chip);
162 queue_delayed_work(chip->workqueue, &chip->work, HZ / 10);
163}
164
165static unsigned int external_rate(unsigned char rcs1)
166{
167 switch (rcs1 & (AK4114_FS0|AK4114_FS1|AK4114_FS2|AK4114_FS3)) {
168 case AK4114_FS_32000HZ: return 32000;
169 case AK4114_FS_44100HZ: return 44100;
170 case AK4114_FS_48000HZ: return 48000;
171 case AK4114_FS_88200HZ: return 88200;
172 case AK4114_FS_96000HZ: return 96000;
173 case AK4114_FS_176400HZ: return 176400;
174 case AK4114_FS_192000HZ: return 192000;
175 default: return 0;
176 }
177}
178
179static int snd_ak4114_in_error_info(snd_kcontrol_t *kcontrol,
180 snd_ctl_elem_info_t *uinfo)
181{
182 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
183 uinfo->count = 1;
184 uinfo->value.integer.min = 0;
185 uinfo->value.integer.max = LONG_MAX;
186 return 0;
187}
188
189static int snd_ak4114_in_error_get(snd_kcontrol_t *kcontrol,
190 snd_ctl_elem_value_t *ucontrol)
191{
192 ak4114_t *chip = snd_kcontrol_chip(kcontrol);
193 long *ptr;
194
195 spin_lock_irq(&chip->lock);
196 ptr = (long *)(((char *)chip) + kcontrol->private_value);
197 ucontrol->value.integer.value[0] = *ptr;
198 *ptr = 0;
199 spin_unlock_irq(&chip->lock);
200 return 0;
201}
202
203static int snd_ak4114_in_bit_info(snd_kcontrol_t *kcontrol,
204 snd_ctl_elem_info_t *uinfo)
205{
206 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
207 uinfo->count = 1;
208 uinfo->value.integer.min = 0;
209 uinfo->value.integer.max = 1;
210 return 0;
211}
212
213static int snd_ak4114_in_bit_get(snd_kcontrol_t *kcontrol,
214 snd_ctl_elem_value_t *ucontrol)
215{
216 ak4114_t *chip = snd_kcontrol_chip(kcontrol);
217 unsigned char reg = kcontrol->private_value & 0xff;
218 unsigned char bit = (kcontrol->private_value >> 8) & 0xff;
219 unsigned char inv = (kcontrol->private_value >> 31) & 1;
220
221 ucontrol->value.integer.value[0] = ((reg_read(chip, reg) & (1 << bit)) ? 1 : 0) ^ inv;
222 return 0;
223}
224
225static int snd_ak4114_rate_info(snd_kcontrol_t *kcontrol,
226 snd_ctl_elem_info_t *uinfo)
227{
228 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
229 uinfo->count = 1;
230 uinfo->value.integer.min = 0;
231 uinfo->value.integer.max = 192000;
232 return 0;
233}
234
235static int snd_ak4114_rate_get(snd_kcontrol_t *kcontrol,
236 snd_ctl_elem_value_t *ucontrol)
237{
238 ak4114_t *chip = snd_kcontrol_chip(kcontrol);
239
240 ucontrol->value.integer.value[0] = external_rate(reg_read(chip, AK4114_REG_RCS1));
241 return 0;
242}
243
244static int snd_ak4114_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
245{
246 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
247 uinfo->count = 1;
248 return 0;
249}
250
251static int snd_ak4114_spdif_get(snd_kcontrol_t * kcontrol,
252 snd_ctl_elem_value_t * ucontrol)
253{
254 ak4114_t *chip = snd_kcontrol_chip(kcontrol);
255 unsigned i;
256
257 for (i = 0; i < AK4114_REG_RXCSB_SIZE; i++)
258 ucontrol->value.iec958.status[i] = reg_read(chip, AK4114_REG_RXCSB0 + i);
259 return 0;
260}
261
262static int snd_ak4114_spdif_playback_get(snd_kcontrol_t * kcontrol,
263 snd_ctl_elem_value_t * ucontrol)
264{
265 ak4114_t *chip = snd_kcontrol_chip(kcontrol);
266 unsigned i;
267
268 for (i = 0; i < AK4114_REG_TXCSB_SIZE; i++)
269 ucontrol->value.iec958.status[i] = chip->txcsb[i];
270 return 0;
271}
272
273static int snd_ak4114_spdif_playback_put(snd_kcontrol_t * kcontrol,
274 snd_ctl_elem_value_t * ucontrol)
275{
276 ak4114_t *chip = snd_kcontrol_chip(kcontrol);
277 unsigned i;
278
279 for (i = 0; i < AK4114_REG_TXCSB_SIZE; i++)
280 reg_write(chip, AK4114_REG_TXCSB0 + i, ucontrol->value.iec958.status[i]);
281 return 0;
282}
283
284static int snd_ak4114_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
285{
286 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
287 uinfo->count = 1;
288 return 0;
289}
290
291static int snd_ak4114_spdif_mask_get(snd_kcontrol_t * kcontrol,
292 snd_ctl_elem_value_t * ucontrol)
293{
294 memset(ucontrol->value.iec958.status, 0xff, AK4114_REG_RXCSB_SIZE);
295 return 0;
296}
297
298static int snd_ak4114_spdif_pinfo(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
299{
300 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
301 uinfo->value.integer.min = 0;
302 uinfo->value.integer.max = 0xffff;
303 uinfo->count = 4;
304 return 0;
305}
306
307static int snd_ak4114_spdif_pget(snd_kcontrol_t * kcontrol,
308 snd_ctl_elem_value_t * ucontrol)
309{
310 ak4114_t *chip = snd_kcontrol_chip(kcontrol);
311 unsigned short tmp;
312
313 ucontrol->value.integer.value[0] = 0xf8f2;
314 ucontrol->value.integer.value[1] = 0x4e1f;
315 tmp = reg_read(chip, AK4114_REG_Pc0) | (reg_read(chip, AK4114_REG_Pc1) << 8);
316 ucontrol->value.integer.value[2] = tmp;
317 tmp = reg_read(chip, AK4114_REG_Pd0) | (reg_read(chip, AK4114_REG_Pd1) << 8);
318 ucontrol->value.integer.value[3] = tmp;
319 return 0;
320}
321
322static int snd_ak4114_spdif_qinfo(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
323{
324 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
325 uinfo->count = AK4114_REG_QSUB_SIZE;
326 return 0;
327}
328
329static int snd_ak4114_spdif_qget(snd_kcontrol_t * kcontrol,
330 snd_ctl_elem_value_t * ucontrol)
331{
332 ak4114_t *chip = snd_kcontrol_chip(kcontrol);
333 unsigned i;
334
335 for (i = 0; i < AK4114_REG_QSUB_SIZE; i++)
336 ucontrol->value.bytes.data[i] = reg_read(chip, AK4114_REG_QSUB_ADDR + i);
337 return 0;
338}
339
340/* Don't forget to change AK4114_CONTROLS define!!! */
341static snd_kcontrol_new_t snd_ak4114_iec958_controls[] = {
342{
343 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
344 .name = "IEC958 Parity 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,
348 .private_value = offsetof(ak4114_t, parity_errors),
349},
350{
351 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
352 .name = "IEC958 V-Bit 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,
356 .private_value = offsetof(ak4114_t, v_bit_errors),
357},
358{
359 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
360 .name = "IEC958 C-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,
364 .private_value = offsetof(ak4114_t, ccrc_errors),
365},
366{
367 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
368 .name = "IEC958 Q-CRC Errors",
369 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
370 .info = snd_ak4114_in_error_info,
371 .get = snd_ak4114_in_error_get,
372 .private_value = offsetof(ak4114_t, qcrc_errors),
373},
374{
375 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
376 .name = "IEC958 External Rate",
377 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
378 .info = snd_ak4114_rate_info,
379 .get = snd_ak4114_rate_get,
380},
381{
382 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
383 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,MASK),
384 .access = SNDRV_CTL_ELEM_ACCESS_READ,
385 .info = snd_ak4114_spdif_mask_info,
386 .get = snd_ak4114_spdif_mask_get,
387},
388{
389 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
390 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
391 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
392 .info = snd_ak4114_spdif_info,
393 .get = snd_ak4114_spdif_playback_get,
394 .put = snd_ak4114_spdif_playback_put,
395},
396{
397 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
398 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,MASK),
399 .access = SNDRV_CTL_ELEM_ACCESS_READ,
400 .info = snd_ak4114_spdif_mask_info,
401 .get = snd_ak4114_spdif_mask_get,
402},
403{
404 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
405 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
406 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
407 .info = snd_ak4114_spdif_info,
408 .get = snd_ak4114_spdif_get,
409},
410{
411 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
412 .name = "IEC958 Preample Capture Default",
413 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
414 .info = snd_ak4114_spdif_pinfo,
415 .get = snd_ak4114_spdif_pget,
416},
417{
418 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
419 .name = "IEC958 Q-subcode Capture Default",
420 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
421 .info = snd_ak4114_spdif_qinfo,
422 .get = snd_ak4114_spdif_qget,
423},
424{
425 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
426 .name = "IEC958 Audio",
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 = (1<<31) | (1<<8) | AK4114_REG_RCS0,
431},
432{
433 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
434 .name = "IEC958 Non-PCM 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 = (6<<8) | AK4114_REG_RCS1,
439},
440{
441 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
442 .name = "IEC958 DTS Bitstream",
443 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
444 .info = snd_ak4114_in_bit_info,
445 .get = snd_ak4114_in_bit_get,
446 .private_value = (3<<8) | AK4114_REG_RCS1,
447}
448};
449
450int snd_ak4114_build(ak4114_t *ak4114,
451 snd_pcm_substream_t *ply_substream,
452 snd_pcm_substream_t *cap_substream)
453{
454 snd_kcontrol_t *kctl;
455 unsigned int idx;
456 int err;
457
458 snd_assert(cap_substream, return -EINVAL);
459 ak4114->playback_substream = ply_substream;
460 ak4114->capture_substream = cap_substream;
461 for (idx = 0; idx < AK4114_CONTROLS; idx++) {
462 kctl = snd_ctl_new1(&snd_ak4114_iec958_controls[idx], ak4114);
463 if (kctl == NULL)
464 return -ENOMEM;
465 if (!strstr(kctl->id.name, "Playback")) {
466 if (ply_substream == NULL) {
467 snd_ctl_free_one(kctl);
468 ak4114->kctls[idx] = NULL;
469 continue;
470 }
471 kctl->id.device = ply_substream->pcm->device;
472 kctl->id.subdevice = ply_substream->number;
473 } else {
474 kctl->id.device = cap_substream->pcm->device;
475 kctl->id.subdevice = cap_substream->number;
476 }
477 err = snd_ctl_add(ak4114->card, kctl);
478 if (err < 0)
479 return err;
480 ak4114->kctls[idx] = kctl;
481 }
482 return 0;
483}
484
485int snd_ak4114_external_rate(ak4114_t *ak4114)
486{
487 unsigned char rcs1;
488
489 rcs1 = reg_read(ak4114, AK4114_REG_RCS1);
490 return external_rate(rcs1);
491}
492
493int snd_ak4114_check_rate_and_errors(ak4114_t *ak4114, unsigned int flags)
494{
495 snd_pcm_runtime_t *runtime = ak4114->capture_substream ? ak4114->capture_substream->runtime : NULL;
496 unsigned long _flags;
497 int res = 0;
498 unsigned char rcs0, rcs1;
499 unsigned char c0, c1;
500
501 rcs1 = reg_read(ak4114, AK4114_REG_RCS1);
502 if (flags & AK4114_CHECK_NO_STAT)
503 goto __rate;
504 rcs0 = reg_read(ak4114, AK4114_REG_RCS0);
505 spin_lock_irqsave(&ak4114->lock, _flags);
506 if (rcs0 & AK4114_PAR)
507 ak4114->parity_errors++;
508 if (rcs1 & AK4114_V)
509 ak4114->v_bit_errors++;
510 if (rcs1 & AK4114_CCRC)
511 ak4114->ccrc_errors++;
512 if (rcs1 & AK4114_QCRC)
513 ak4114->qcrc_errors++;
514 c0 = (ak4114->rcs0 & (AK4114_QINT | AK4114_CINT | AK4114_PEM | AK4114_AUDION | AK4114_AUTO | AK4114_UNLCK)) ^
515 (rcs0 & (AK4114_QINT | AK4114_CINT | AK4114_PEM | AK4114_AUDION | AK4114_AUTO | AK4114_UNLCK));
516 c1 = (ak4114->rcs1 & 0xf0) ^ (rcs1 & 0xf0);
517 ak4114->rcs0 = rcs0 & ~(AK4114_QINT | AK4114_CINT);
518 ak4114->rcs1 = rcs1;
519 spin_unlock_irqrestore(&ak4114->lock, _flags);
520
521 if (rcs0 & AK4114_PAR)
522 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[0]->id);
523 if (rcs0 & AK4114_V)
524 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[1]->id);
525 if (rcs1 & AK4114_CCRC)
526 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[2]->id);
527 if (rcs1 & AK4114_QCRC)
528 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[3]->id);
529
530 /* rate change */
531 if (c1 & 0xf0)
532 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[4]->id);
533
534 if ((c0 & AK4114_PEM) | (c0 & AK4114_CINT))
535 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[9]->id);
536 if (c0 & AK4114_QINT)
537 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[10]->id);
538
539 if (c0 & AK4114_AUDION)
540 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[11]->id);
541 if (c0 & AK4114_AUTO)
542 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[12]->id);
543 if (c0 & AK4114_DTSCD)
544 snd_ctl_notify(ak4114->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4114->kctls[13]->id);
545
546 if (ak4114->change_callback && (c0 | c1) != 0)
547 ak4114->change_callback(ak4114, c0, c1);
548
549 __rate:
550 /* compare rate */
551 res = external_rate(rcs1);
552 if (!(flags & AK4114_CHECK_NO_RATE) && runtime && runtime->rate != res) {
553 snd_pcm_stream_lock_irqsave(ak4114->capture_substream, _flags);
554 if (snd_pcm_running(ak4114->capture_substream)) {
555 // printk("rate changed (%i <- %i)\n", runtime->rate, res);
556 snd_pcm_stop(ak4114->capture_substream, SNDRV_PCM_STATE_DRAINING);
557 wake_up(&runtime->sleep);
558 res = 1;
559 }
560 snd_pcm_stream_unlock_irqrestore(ak4114->capture_substream, _flags);
561 }
562 return res;
563}
564
565static void ak4114_stats(void *data)
566{
567 ak4114_t *chip = (ak4114_t *)data;
568
569 if (chip->init)
570 return;
571 snd_ak4114_check_rate_and_errors(chip, 0);
572 queue_delayed_work(chip->workqueue, &chip->work, HZ / 10);
573}
574
575EXPORT_SYMBOL(snd_ak4114_create);
576EXPORT_SYMBOL(snd_ak4114_reg_write);
577EXPORT_SYMBOL(snd_ak4114_reinit);
578EXPORT_SYMBOL(snd_ak4114_build);
579EXPORT_SYMBOL(snd_ak4114_external_rate);
580EXPORT_SYMBOL(snd_ak4114_check_rate_and_errors);
diff --git a/sound/i2c/other/ak4117.c b/sound/i2c/other/ak4117.c
new file mode 100644
index 000000000000..0419c4336a55
--- /dev/null
+++ b/sound/i2c/other/ak4117.c
@@ -0,0 +1,559 @@
1/*
2 * Routines for control of the AK4117 via 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/ak4117.h>
30#include <sound/asoundef.h>
31
32MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
33MODULE_DESCRIPTION("AK4117 IEC958 (S/PDIF) receiver by Asahi Kasei");
34MODULE_LICENSE("GPL");
35
36#define AK4117_ADDR 0x00 /* fixed address */
37
38static void snd_ak4117_timer(unsigned long data);
39
40static void reg_write(ak4117_t *ak4117, unsigned char reg, unsigned char val)
41{
42 ak4117->write(ak4117->private_data, reg, val);
43 if (reg < sizeof(ak4117->regmap))
44 ak4117->regmap[reg] = val;
45}
46
47static inline unsigned char reg_read(ak4117_t *ak4117, unsigned char reg)
48{
49 return ak4117->read(ak4117->private_data, reg);
50}
51
52#if 0
53static void reg_dump(ak4117_t *ak4117)
54{
55 int i;
56
57 printk("AK4117 REG DUMP:\n");
58 for (i = 0; i < 0x1b; i++)
59 printk("reg[%02x] = %02x (%02x)\n", i, reg_read(ak4117, i), i < sizeof(ak4117->regmap) ? ak4117->regmap[i] : 0);
60}
61#endif
62
63static void snd_ak4117_free(ak4117_t *chip)
64{
65 del_timer(&chip->timer);
66 kfree(chip);
67}
68
69static int snd_ak4117_dev_free(snd_device_t *device)
70{
71 ak4117_t *chip = device->device_data;
72 snd_ak4117_free(chip);
73 return 0;
74}
75
76int snd_ak4117_create(snd_card_t *card, ak4117_read_t *read, ak4117_write_t *write,
77 unsigned char pgm[5], void *private_data, ak4117_t **r_ak4117)
78{
79 ak4117_t *chip;
80 int err = 0;
81 unsigned char reg;
82 static snd_device_ops_t ops = {
83 .dev_free = snd_ak4117_dev_free,
84 };
85
86 chip = kcalloc(1, sizeof(*chip), GFP_KERNEL);
87 if (chip == NULL)
88 return -ENOMEM;
89 spin_lock_init(&chip->lock);
90 chip->card = card;
91 chip->read = read;
92 chip->write = write;
93 chip->private_data = private_data;
94 init_timer(&chip->timer);
95 chip->timer.data = (unsigned long)chip;
96 chip->timer.function = snd_ak4117_timer;
97
98 for (reg = 0; reg < 5; reg++)
99 chip->regmap[reg] = pgm[reg];
100 snd_ak4117_reinit(chip);
101
102 chip->rcs0 = reg_read(chip, AK4117_REG_RCS0) & ~(AK4117_QINT | AK4117_CINT | AK4117_STC);
103 chip->rcs1 = reg_read(chip, AK4117_REG_RCS1);
104 chip->rcs2 = reg_read(chip, AK4117_REG_RCS2);
105
106 if ((err = snd_device_new(card, SNDRV_DEV_CODEC, chip, &ops)) < 0)
107 goto __fail;
108
109 if (r_ak4117)
110 *r_ak4117 = chip;
111 return 0;
112
113 __fail:
114 snd_ak4117_free(chip);
115 return err < 0 ? err : -EIO;
116}
117
118void snd_ak4117_reg_write(ak4117_t *chip, unsigned char reg, unsigned char mask, unsigned char val)
119{
120 if (reg >= 5)
121 return;
122 reg_write(chip, reg, (chip->regmap[reg] & ~mask) | val);
123}
124
125void snd_ak4117_reinit(ak4117_t *chip)
126{
127 unsigned char old = chip->regmap[AK4117_REG_PWRDN], reg;
128
129 del_timer(&chip->timer);
130 chip->init = 1;
131 /* bring the chip to reset state and powerdown state */
132 reg_write(chip, AK4117_REG_PWRDN, 0);
133 udelay(200);
134 /* release reset, but leave powerdown */
135 reg_write(chip, AK4117_REG_PWRDN, (old | AK4117_RST) & ~AK4117_PWN);
136 udelay(200);
137 for (reg = 1; reg < 5; reg++)
138 reg_write(chip, reg, chip->regmap[reg]);
139 /* release powerdown, everything is initialized now */
140 reg_write(chip, AK4117_REG_PWRDN, old | AK4117_RST | AK4117_PWN);
141 chip->init = 0;
142 chip->timer.expires = 1 + jiffies;
143 add_timer(&chip->timer);
144}
145
146static unsigned int external_rate(unsigned char rcs1)
147{
148 switch (rcs1 & (AK4117_FS0|AK4117_FS1|AK4117_FS2|AK4117_FS3)) {
149 case AK4117_FS_32000HZ: return 32000;
150 case AK4117_FS_44100HZ: return 44100;
151 case AK4117_FS_48000HZ: return 48000;
152 case AK4117_FS_88200HZ: return 88200;
153 case AK4117_FS_96000HZ: return 96000;
154 case AK4117_FS_176400HZ: return 176400;
155 case AK4117_FS_192000HZ: return 192000;
156 default: return 0;
157 }
158}
159
160static int snd_ak4117_in_error_info(snd_kcontrol_t *kcontrol,
161 snd_ctl_elem_info_t *uinfo)
162{
163 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
164 uinfo->count = 1;
165 uinfo->value.integer.min = 0;
166 uinfo->value.integer.max = LONG_MAX;
167 return 0;
168}
169
170static int snd_ak4117_in_error_get(snd_kcontrol_t *kcontrol,
171 snd_ctl_elem_value_t *ucontrol)
172{
173 ak4117_t *chip = snd_kcontrol_chip(kcontrol);
174 long *ptr;
175
176 spin_lock_irq(&chip->lock);
177 ptr = (long *)(((char *)chip) + kcontrol->private_value);
178 ucontrol->value.integer.value[0] = *ptr;
179 *ptr = 0;
180 spin_unlock_irq(&chip->lock);
181 return 0;
182}
183
184static int snd_ak4117_in_bit_info(snd_kcontrol_t *kcontrol,
185 snd_ctl_elem_info_t *uinfo)
186{
187 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
188 uinfo->count = 1;
189 uinfo->value.integer.min = 0;
190 uinfo->value.integer.max = 1;
191 return 0;
192}
193
194static int snd_ak4117_in_bit_get(snd_kcontrol_t *kcontrol,
195 snd_ctl_elem_value_t *ucontrol)
196{
197 ak4117_t *chip = snd_kcontrol_chip(kcontrol);
198 unsigned char reg = kcontrol->private_value & 0xff;
199 unsigned char bit = (kcontrol->private_value >> 8) & 0xff;
200 unsigned char inv = (kcontrol->private_value >> 31) & 1;
201
202 ucontrol->value.integer.value[0] = ((reg_read(chip, reg) & (1 << bit)) ? 1 : 0) ^ inv;
203 return 0;
204}
205
206static int snd_ak4117_rx_info(snd_kcontrol_t *kcontrol,
207 snd_ctl_elem_info_t *uinfo)
208{
209 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
210 uinfo->count = 1;
211 uinfo->value.integer.min = 0;
212 uinfo->value.integer.max = 1;
213 return 0;
214}
215
216static int snd_ak4117_rx_get(snd_kcontrol_t *kcontrol,
217 snd_ctl_elem_value_t *ucontrol)
218{
219 ak4117_t *chip = snd_kcontrol_chip(kcontrol);
220
221 ucontrol->value.integer.value[0] = (chip->regmap[AK4117_REG_IO] & AK4117_IPS) ? 1 : 0;
222 return 0;
223}
224
225static int snd_ak4117_rx_put(snd_kcontrol_t *kcontrol,
226 snd_ctl_elem_value_t *ucontrol)
227{
228 ak4117_t *chip = snd_kcontrol_chip(kcontrol);
229 int change;
230 u8 old_val;
231
232 spin_lock_irq(&chip->lock);
233 old_val = chip->regmap[AK4117_REG_IO];
234 change = !!ucontrol->value.integer.value[0] != ((old_val & AK4117_IPS) ? 1 : 0);
235 if (change)
236 reg_write(chip, AK4117_REG_IO, (old_val & ~AK4117_IPS) | (ucontrol->value.integer.value[0] ? AK4117_IPS : 0));
237 spin_unlock_irq(&chip->lock);
238 return change;
239}
240
241static int snd_ak4117_rate_info(snd_kcontrol_t *kcontrol,
242 snd_ctl_elem_info_t *uinfo)
243{
244 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
245 uinfo->count = 1;
246 uinfo->value.integer.min = 0;
247 uinfo->value.integer.max = 192000;
248 return 0;
249}
250
251static int snd_ak4117_rate_get(snd_kcontrol_t *kcontrol,
252 snd_ctl_elem_value_t *ucontrol)
253{
254 ak4117_t *chip = snd_kcontrol_chip(kcontrol);
255
256 ucontrol->value.integer.value[0] = external_rate(reg_read(chip, AK4117_REG_RCS1));
257 return 0;
258}
259
260static int snd_ak4117_spdif_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
261{
262 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
263 uinfo->count = 1;
264 return 0;
265}
266
267static int snd_ak4117_spdif_get(snd_kcontrol_t * kcontrol,
268 snd_ctl_elem_value_t * ucontrol)
269{
270 ak4117_t *chip = snd_kcontrol_chip(kcontrol);
271 unsigned i;
272
273 for (i = 0; i < AK4117_REG_RXCSB_SIZE; i++)
274 ucontrol->value.iec958.status[i] = reg_read(chip, AK4117_REG_RXCSB0 + i);
275 return 0;
276}
277
278static int snd_ak4117_spdif_mask_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
279{
280 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
281 uinfo->count = 1;
282 return 0;
283}
284
285static int snd_ak4117_spdif_mask_get(snd_kcontrol_t * kcontrol,
286 snd_ctl_elem_value_t * ucontrol)
287{
288 memset(ucontrol->value.iec958.status, 0xff, AK4117_REG_RXCSB_SIZE);
289 return 0;
290}
291
292static int snd_ak4117_spdif_pinfo(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
293{
294 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
295 uinfo->value.integer.min = 0;
296 uinfo->value.integer.max = 0xffff;
297 uinfo->count = 4;
298 return 0;
299}
300
301static int snd_ak4117_spdif_pget(snd_kcontrol_t * kcontrol,
302 snd_ctl_elem_value_t * ucontrol)
303{
304 ak4117_t *chip = snd_kcontrol_chip(kcontrol);
305 unsigned short tmp;
306
307 ucontrol->value.integer.value[0] = 0xf8f2;
308 ucontrol->value.integer.value[1] = 0x4e1f;
309 tmp = reg_read(chip, AK4117_REG_Pc0) | (reg_read(chip, AK4117_REG_Pc1) << 8);
310 ucontrol->value.integer.value[2] = tmp;
311 tmp = reg_read(chip, AK4117_REG_Pd0) | (reg_read(chip, AK4117_REG_Pd1) << 8);
312 ucontrol->value.integer.value[3] = tmp;
313 return 0;
314}
315
316static int snd_ak4117_spdif_qinfo(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
317{
318 uinfo->type = SNDRV_CTL_ELEM_TYPE_BYTES;
319 uinfo->count = AK4117_REG_QSUB_SIZE;
320 return 0;
321}
322
323static int snd_ak4117_spdif_qget(snd_kcontrol_t * kcontrol,
324 snd_ctl_elem_value_t * ucontrol)
325{
326 ak4117_t *chip = snd_kcontrol_chip(kcontrol);
327 unsigned i;
328
329 for (i = 0; i < AK4117_REG_QSUB_SIZE; i++)
330 ucontrol->value.bytes.data[i] = reg_read(chip, AK4117_REG_QSUB_ADDR + i);
331 return 0;
332}
333
334/* Don't forget to change AK4117_CONTROLS define!!! */
335static snd_kcontrol_new_t snd_ak4117_iec958_controls[] = {
336{
337 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
338 .name = "IEC958 Parity Errors",
339 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
340 .info = snd_ak4117_in_error_info,
341 .get = snd_ak4117_in_error_get,
342 .private_value = offsetof(ak4117_t, parity_errors),
343},
344{
345 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
346 .name = "IEC958 V-Bit Errors",
347 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
348 .info = snd_ak4117_in_error_info,
349 .get = snd_ak4117_in_error_get,
350 .private_value = offsetof(ak4117_t, v_bit_errors),
351},
352{
353 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
354 .name = "IEC958 C-CRC Errors",
355 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
356 .info = snd_ak4117_in_error_info,
357 .get = snd_ak4117_in_error_get,
358 .private_value = offsetof(ak4117_t, ccrc_errors),
359},
360{
361 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
362 .name = "IEC958 Q-CRC Errors",
363 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
364 .info = snd_ak4117_in_error_info,
365 .get = snd_ak4117_in_error_get,
366 .private_value = offsetof(ak4117_t, qcrc_errors),
367},
368{
369 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
370 .name = "IEC958 External Rate",
371 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
372 .info = snd_ak4117_rate_info,
373 .get = snd_ak4117_rate_get,
374},
375{
376 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
377 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,MASK),
378 .access = SNDRV_CTL_ELEM_ACCESS_READ,
379 .info = snd_ak4117_spdif_mask_info,
380 .get = snd_ak4117_spdif_mask_get,
381},
382{
383 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
384 .name = SNDRV_CTL_NAME_IEC958("",CAPTURE,DEFAULT),
385 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
386 .info = snd_ak4117_spdif_info,
387 .get = snd_ak4117_spdif_get,
388},
389{
390 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
391 .name = "IEC958 Preample Capture Default",
392 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
393 .info = snd_ak4117_spdif_pinfo,
394 .get = snd_ak4117_spdif_pget,
395},
396{
397 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
398 .name = "IEC958 Q-subcode Capture Default",
399 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
400 .info = snd_ak4117_spdif_qinfo,
401 .get = snd_ak4117_spdif_qget,
402},
403{
404 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
405 .name = "IEC958 Audio",
406 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
407 .info = snd_ak4117_in_bit_info,
408 .get = snd_ak4117_in_bit_get,
409 .private_value = (1<<31) | (3<<8) | AK4117_REG_RCS0,
410},
411{
412 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
413 .name = "IEC958 Non-PCM Bitstream",
414 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
415 .info = snd_ak4117_in_bit_info,
416 .get = snd_ak4117_in_bit_get,
417 .private_value = (5<<8) | AK4117_REG_RCS1,
418},
419{
420 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
421 .name = "IEC958 DTS Bitstream",
422 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE,
423 .info = snd_ak4117_in_bit_info,
424 .get = snd_ak4117_in_bit_get,
425 .private_value = (6<<8) | AK4117_REG_RCS1,
426},
427{
428 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
429 .name = "AK4117 Input Select",
430 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_WRITE,
431 .info = snd_ak4117_rx_info,
432 .get = snd_ak4117_rx_get,
433 .put = snd_ak4117_rx_put,
434}
435};
436
437int snd_ak4117_build(ak4117_t *ak4117, snd_pcm_substream_t *cap_substream)
438{
439 snd_kcontrol_t *kctl;
440 unsigned int idx;
441 int err;
442
443 snd_assert(cap_substream, return -EINVAL);
444 ak4117->substream = cap_substream;
445 for (idx = 0; idx < AK4117_CONTROLS; idx++) {
446 kctl = snd_ctl_new1(&snd_ak4117_iec958_controls[idx], ak4117);
447 if (kctl == NULL)
448 return -ENOMEM;
449 kctl->id.device = cap_substream->pcm->device;
450 kctl->id.subdevice = cap_substream->number;
451 err = snd_ctl_add(ak4117->card, kctl);
452 if (err < 0)
453 return err;
454 ak4117->kctls[idx] = kctl;
455 }
456 return 0;
457}
458
459int snd_ak4117_external_rate(ak4117_t *ak4117)
460{
461 unsigned char rcs1;
462
463 rcs1 = reg_read(ak4117, AK4117_REG_RCS1);
464 return external_rate(rcs1);
465}
466
467int snd_ak4117_check_rate_and_errors(ak4117_t *ak4117, unsigned int flags)
468{
469 snd_pcm_runtime_t *runtime = ak4117->substream ? ak4117->substream->runtime : NULL;
470 unsigned long _flags;
471 int res = 0;
472 unsigned char rcs0, rcs1, rcs2;
473 unsigned char c0, c1;
474
475 rcs1 = reg_read(ak4117, AK4117_REG_RCS1);
476 if (flags & AK4117_CHECK_NO_STAT)
477 goto __rate;
478 rcs0 = reg_read(ak4117, AK4117_REG_RCS0);
479 rcs2 = reg_read(ak4117, AK4117_REG_RCS2);
480 // printk("AK IRQ: rcs0 = 0x%x, rcs1 = 0x%x, rcs2 = 0x%x\n", rcs0, rcs1, rcs2);
481 spin_lock_irqsave(&ak4117->lock, _flags);
482 if (rcs0 & AK4117_PAR)
483 ak4117->parity_errors++;
484 if (rcs0 & AK4117_V)
485 ak4117->v_bit_errors++;
486 if (rcs2 & AK4117_CCRC)
487 ak4117->ccrc_errors++;
488 if (rcs2 & AK4117_QCRC)
489 ak4117->qcrc_errors++;
490 c0 = (ak4117->rcs0 & (AK4117_QINT | AK4117_CINT | AK4117_STC | AK4117_AUDION | AK4117_AUTO | AK4117_UNLCK)) ^
491 (rcs0 & (AK4117_QINT | AK4117_CINT | AK4117_STC | AK4117_AUDION | AK4117_AUTO | AK4117_UNLCK));
492 c1 = (ak4117->rcs1 & (AK4117_DTSCD | AK4117_NPCM | AK4117_PEM | 0x0f)) ^
493 (rcs1 & (AK4117_DTSCD | AK4117_NPCM | AK4117_PEM | 0x0f));
494 ak4117->rcs0 = rcs0 & ~(AK4117_QINT | AK4117_CINT | AK4117_STC);
495 ak4117->rcs1 = rcs1;
496 ak4117->rcs2 = rcs2;
497 spin_unlock_irqrestore(&ak4117->lock, _flags);
498
499 if (rcs0 & AK4117_PAR)
500 snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[0]->id);
501 if (rcs0 & AK4117_V)
502 snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[1]->id);
503 if (rcs2 & AK4117_CCRC)
504 snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[2]->id);
505 if (rcs2 & AK4117_QCRC)
506 snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[3]->id);
507
508 /* rate change */
509 if (c1 & 0x0f)
510 snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[4]->id);
511
512 if ((c1 & AK4117_PEM) | (c0 & AK4117_CINT))
513 snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[6]->id);
514 if (c0 & AK4117_QINT)
515 snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[8]->id);
516
517 if (c0 & AK4117_AUDION)
518 snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[9]->id);
519 if (c1 & AK4117_NPCM)
520 snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[10]->id);
521 if (c1 & AK4117_DTSCD)
522 snd_ctl_notify(ak4117->card, SNDRV_CTL_EVENT_MASK_VALUE, &ak4117->kctls[11]->id);
523
524 if (ak4117->change_callback && (c0 | c1) != 0)
525 ak4117->change_callback(ak4117, c0, c1);
526
527 __rate:
528 /* compare rate */
529 res = external_rate(rcs1);
530 if (!(flags & AK4117_CHECK_NO_RATE) && runtime && runtime->rate != res) {
531 snd_pcm_stream_lock_irqsave(ak4117->substream, _flags);
532 if (snd_pcm_running(ak4117->substream)) {
533 // printk("rate changed (%i <- %i)\n", runtime->rate, res);
534 snd_pcm_stop(ak4117->substream, SNDRV_PCM_STATE_DRAINING);
535 wake_up(&runtime->sleep);
536 res = 1;
537 }
538 snd_pcm_stream_unlock_irqrestore(ak4117->substream, _flags);
539 }
540 return res;
541}
542
543static void snd_ak4117_timer(unsigned long data)
544{
545 ak4117_t *chip = (ak4117_t *)data;
546
547 if (chip->init)
548 return;
549 snd_ak4117_check_rate_and_errors(chip, 0);
550 chip->timer.expires = 1 + jiffies;
551 add_timer(&chip->timer);
552}
553
554EXPORT_SYMBOL(snd_ak4117_create);
555EXPORT_SYMBOL(snd_ak4117_reg_write);
556EXPORT_SYMBOL(snd_ak4117_reinit);
557EXPORT_SYMBOL(snd_ak4117_build);
558EXPORT_SYMBOL(snd_ak4117_external_rate);
559EXPORT_SYMBOL(snd_ak4117_check_rate_and_errors);
diff --git a/sound/i2c/other/ak4xxx-adda.c b/sound/i2c/other/ak4xxx-adda.c
new file mode 100644
index 000000000000..db2b7274a9d6
--- /dev/null
+++ b/sound/i2c/other/ak4xxx-adda.c
@@ -0,0 +1,501 @@
1/*
2 * ALSA driver for AK4524 / AK4528 / AK4529 / AK4355 / AK4358 / AK4381
3 * AD and DA converters
4 *
5 * Copyright (c) 2000-2004 Jaroslav Kysela <perex@suse.cz>,
6 * Takashi Iwai <tiwai@suse.de>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <sound/driver.h>
25#include <asm/io.h>
26#include <linux/delay.h>
27#include <linux/interrupt.h>
28#include <linux/init.h>
29#include <sound/core.h>
30#include <sound/control.h>
31#include <sound/ak4xxx-adda.h>
32
33MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>, Takashi Iwai <tiwai@suse.de>");
34MODULE_DESCRIPTION("Routines for control of AK452x / AK43xx AD/DA converters");
35MODULE_LICENSE("GPL");
36
37void snd_akm4xxx_write(akm4xxx_t *ak, int chip, unsigned char reg, unsigned char val)
38{
39 ak->ops.lock(ak, chip);
40 ak->ops.write(ak, chip, reg, val);
41
42 /* save the data */
43 if (ak->type == SND_AK4524 || ak->type == SND_AK4528) {
44 if ((reg != 0x04 && reg != 0x05) || (val & 0x80) == 0)
45 snd_akm4xxx_set(ak, chip, reg, val);
46 else
47 snd_akm4xxx_set_ipga(ak, chip, reg, val);
48 } else {
49 /* AK4529, or else */
50 snd_akm4xxx_set(ak, chip, reg, val);
51 }
52 ak->ops.unlock(ak, chip);
53}
54
55/*
56 * reset the AKM codecs
57 * @state: 1 = reset codec, 0 = restore the registers
58 *
59 * assert the reset operation and restores the register values to the chips.
60 */
61void snd_akm4xxx_reset(akm4xxx_t *ak, int state)
62{
63 unsigned int chip;
64 unsigned char reg;
65
66 switch (ak->type) {
67 case SND_AK4524:
68 case SND_AK4528:
69 for (chip = 0; chip < ak->num_dacs/2; chip++) {
70 snd_akm4xxx_write(ak, chip, 0x01, state ? 0x00 : 0x03);
71 if (state)
72 continue;
73 /* DAC volumes */
74 for (reg = 0x04; reg < (ak->type == SND_AK4528 ? 0x06 : 0x08); reg++)
75 snd_akm4xxx_write(ak, chip, reg, snd_akm4xxx_get(ak, chip, reg));
76 if (ak->type == SND_AK4528)
77 continue;
78 /* IPGA */
79 for (reg = 0x04; reg < 0x06; reg++)
80 snd_akm4xxx_write(ak, chip, reg, snd_akm4xxx_get_ipga(ak, chip, reg));
81 }
82 break;
83 case SND_AK4529:
84 /* FIXME: needed for ak4529? */
85 break;
86 case SND_AK4355:
87 case SND_AK4358:
88 if (state) {
89 snd_akm4xxx_write(ak, 0, 0x01, 0x02); /* reset and soft-mute */
90 return;
91 }
92 for (reg = 0x00; reg < 0x0b; reg++)
93 if (reg != 0x01)
94 snd_akm4xxx_write(ak, 0, reg, snd_akm4xxx_get(ak, 0, reg));
95 snd_akm4xxx_write(ak, 0, 0x01, 0x01); /* un-reset, unmute */
96 break;
97 case SND_AK4381:
98 for (chip = 0; chip < ak->num_dacs/2; chip++) {
99 snd_akm4xxx_write(ak, chip, 0x00, state ? 0x0c : 0x0f);
100 if (state)
101 continue;
102 for (reg = 0x01; reg < 0x05; reg++)
103 snd_akm4xxx_write(ak, chip, reg, snd_akm4xxx_get(ak, chip, reg));
104 }
105 break;
106 }
107}
108
109/*
110 * initialize all the ak4xxx chips
111 */
112void snd_akm4xxx_init(akm4xxx_t *ak)
113{
114 static unsigned char inits_ak4524[] = {
115 0x00, 0x07, /* 0: all power up */
116 0x01, 0x00, /* 1: ADC/DAC reset */
117 0x02, 0x60, /* 2: 24bit I2S */
118 0x03, 0x19, /* 3: deemphasis off */
119 0x01, 0x03, /* 1: ADC/DAC enable */
120 0x04, 0x00, /* 4: ADC left muted */
121 0x05, 0x00, /* 5: ADC right muted */
122 0x04, 0x80, /* 4: ADC IPGA gain 0dB */
123 0x05, 0x80, /* 5: ADC IPGA gain 0dB */
124 0x06, 0x00, /* 6: DAC left muted */
125 0x07, 0x00, /* 7: DAC right muted */
126 0xff, 0xff
127 };
128 static unsigned char inits_ak4528[] = {
129 0x00, 0x07, /* 0: all power up */
130 0x01, 0x00, /* 1: ADC/DAC reset */
131 0x02, 0x60, /* 2: 24bit I2S */
132 0x03, 0x0d, /* 3: deemphasis off, turn LR highpass filters on */
133 0x01, 0x03, /* 1: ADC/DAC enable */
134 0x04, 0x00, /* 4: ADC left muted */
135 0x05, 0x00, /* 5: ADC right muted */
136 0xff, 0xff
137 };
138 static unsigned char inits_ak4529[] = {
139 0x09, 0x01, /* 9: ATS=0, RSTN=1 */
140 0x0a, 0x3f, /* A: all power up, no zero/overflow detection */
141 0x00, 0x0c, /* 0: TDM=0, 24bit I2S, SMUTE=0 */
142 0x01, 0x00, /* 1: ACKS=0, ADC, loop off */
143 0x02, 0xff, /* 2: LOUT1 muted */
144 0x03, 0xff, /* 3: ROUT1 muted */
145 0x04, 0xff, /* 4: LOUT2 muted */
146 0x05, 0xff, /* 5: ROUT2 muted */
147 0x06, 0xff, /* 6: LOUT3 muted */
148 0x07, 0xff, /* 7: ROUT3 muted */
149 0x0b, 0xff, /* B: LOUT4 muted */
150 0x0c, 0xff, /* C: ROUT4 muted */
151 0x08, 0x55, /* 8: deemphasis all off */
152 0xff, 0xff
153 };
154 static unsigned char inits_ak4355[] = {
155 0x01, 0x02, /* 1: reset and soft-mute */
156 0x00, 0x06, /* 0: mode3(i2s), disable auto-clock detect, disable DZF, sharp roll-off, RSTN#=0 */
157 0x02, 0x0e, /* 2: DA's power up, normal speed, RSTN#=0 */
158 // 0x02, 0x2e, /* quad speed */
159 0x03, 0x01, /* 3: de-emphasis off */
160 0x04, 0x00, /* 4: LOUT1 volume muted */
161 0x05, 0x00, /* 5: ROUT1 volume muted */
162 0x06, 0x00, /* 6: LOUT2 volume muted */
163 0x07, 0x00, /* 7: ROUT2 volume muted */
164 0x08, 0x00, /* 8: LOUT3 volume muted */
165 0x09, 0x00, /* 9: ROUT3 volume muted */
166 0x0a, 0x00, /* a: DATT speed=0, ignore DZF */
167 0x01, 0x01, /* 1: un-reset, unmute */
168 0xff, 0xff
169 };
170 static unsigned char inits_ak4358[] = {
171 0x01, 0x02, /* 1: reset and soft-mute */
172 0x00, 0x06, /* 0: mode3(i2s), disable auto-clock detect, disable DZF, sharp roll-off, RSTN#=0 */
173 0x02, 0x0e, /* 2: DA's power up, normal speed, RSTN#=0 */
174 // 0x02, 0x2e, /* quad speed */
175 0x03, 0x01, /* 3: de-emphasis off */
176 0x04, 0x00, /* 4: LOUT1 volume muted */
177 0x05, 0x00, /* 5: ROUT1 volume muted */
178 0x06, 0x00, /* 6: LOUT2 volume muted */
179 0x07, 0x00, /* 7: ROUT2 volume muted */
180 0x08, 0x00, /* 8: LOUT3 volume muted */
181 0x09, 0x00, /* 9: ROUT3 volume muted */
182 0x0b, 0x00, /* b: LOUT4 volume muted */
183 0x0c, 0x00, /* c: ROUT4 volume muted */
184 0x0a, 0x00, /* a: DATT speed=0, ignore DZF */
185 0x01, 0x01, /* 1: un-reset, unmute */
186 0xff, 0xff
187 };
188 static unsigned char inits_ak4381[] = {
189 0x00, 0x0c, /* 0: mode3(i2s), disable auto-clock detect */
190 0x01, 0x02, /* 1: de-emphasis off, normal speed, sharp roll-off, DZF off */
191 // 0x01, 0x12, /* quad speed */
192 0x02, 0x00, /* 2: DZF disabled */
193 0x03, 0x00, /* 3: LATT 0 */
194 0x04, 0x00, /* 4: RATT 0 */
195 0x00, 0x0f, /* 0: power-up, un-reset */
196 0xff, 0xff
197 };
198
199 int chip, num_chips;
200 unsigned char *ptr, reg, data, *inits;
201
202 switch (ak->type) {
203 case SND_AK4524:
204 inits = inits_ak4524;
205 num_chips = ak->num_dacs / 2;
206 break;
207 case SND_AK4528:
208 inits = inits_ak4528;
209 num_chips = ak->num_dacs / 2;
210 break;
211 case SND_AK4529:
212 inits = inits_ak4529;
213 num_chips = 1;
214 break;
215 case SND_AK4355:
216 inits = inits_ak4355;
217 num_chips = 1;
218 break;
219 case SND_AK4358:
220 inits = inits_ak4358;
221 num_chips = 1;
222 break;
223 case SND_AK4381:
224 inits = inits_ak4381;
225 num_chips = ak->num_dacs / 2;
226 break;
227 default:
228 snd_BUG();
229 return;
230 }
231
232 for (chip = 0; chip < num_chips; chip++) {
233 ptr = inits;
234 while (*ptr != 0xff) {
235 reg = *ptr++;
236 data = *ptr++;
237 snd_akm4xxx_write(ak, chip, reg, data);
238 }
239 }
240}
241
242#define AK_GET_CHIP(val) (((val) >> 8) & 0xff)
243#define AK_GET_ADDR(val) ((val) & 0xff)
244#define AK_GET_SHIFT(val) (((val) >> 16) & 0x7f)
245#define AK_GET_INVERT(val) (((val) >> 23) & 1)
246#define AK_GET_MASK(val) (((val) >> 24) & 0xff)
247#define AK_COMPOSE(chip,addr,shift,mask) (((chip) << 8) | (addr) | ((shift) << 16) | ((mask) << 24))
248#define AK_INVERT (1<<23)
249
250static int snd_akm4xxx_volume_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
251{
252 unsigned int mask = AK_GET_MASK(kcontrol->private_value);
253
254 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
255 uinfo->count = 1;
256 uinfo->value.integer.min = 0;
257 uinfo->value.integer.max = mask;
258 return 0;
259}
260
261static int snd_akm4xxx_volume_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
262{
263 akm4xxx_t *ak = snd_kcontrol_chip(kcontrol);
264 int chip = AK_GET_CHIP(kcontrol->private_value);
265 int addr = AK_GET_ADDR(kcontrol->private_value);
266 int invert = AK_GET_INVERT(kcontrol->private_value);
267 unsigned int mask = AK_GET_MASK(kcontrol->private_value);
268 unsigned char val = snd_akm4xxx_get(ak, chip, addr);
269
270 ucontrol->value.integer.value[0] = invert ? mask - val : val;
271 return 0;
272}
273
274static int snd_akm4xxx_volume_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
275{
276 akm4xxx_t *ak = snd_kcontrol_chip(kcontrol);
277 int chip = AK_GET_CHIP(kcontrol->private_value);
278 int addr = AK_GET_ADDR(kcontrol->private_value);
279 int invert = AK_GET_INVERT(kcontrol->private_value);
280 unsigned int mask = AK_GET_MASK(kcontrol->private_value);
281 unsigned char nval = ucontrol->value.integer.value[0] % (mask+1);
282 int change;
283
284 if (invert)
285 nval = mask - nval;
286 change = snd_akm4xxx_get(ak, chip, addr) != nval;
287 if (change)
288 snd_akm4xxx_write(ak, chip, addr, nval);
289 return change;
290}
291
292static int snd_akm4xxx_ipga_gain_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t * uinfo)
293{
294 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
295 uinfo->count = 1;
296 uinfo->value.integer.min = 0;
297 uinfo->value.integer.max = 36;
298 return 0;
299}
300
301static int snd_akm4xxx_ipga_gain_get(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
302{
303 akm4xxx_t *ak = snd_kcontrol_chip(kcontrol);
304 int chip = AK_GET_CHIP(kcontrol->private_value);
305 int addr = AK_GET_ADDR(kcontrol->private_value);
306 ucontrol->value.integer.value[0] = snd_akm4xxx_get_ipga(ak, chip, addr) & 0x7f;
307 return 0;
308}
309
310static int snd_akm4xxx_ipga_gain_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
311{
312 akm4xxx_t *ak = snd_kcontrol_chip(kcontrol);
313 int chip = AK_GET_CHIP(kcontrol->private_value);
314 int addr = AK_GET_ADDR(kcontrol->private_value);
315 unsigned char nval = (ucontrol->value.integer.value[0] % 37) | 0x80;
316 int change = snd_akm4xxx_get_ipga(ak, chip, addr) != nval;
317 if (change)
318 snd_akm4xxx_write(ak, chip, addr, nval);
319 return change;
320}
321
322static int snd_akm4xxx_deemphasis_info(snd_kcontrol_t *kcontrol, snd_ctl_elem_info_t *uinfo)
323{
324 static char *texts[4] = {
325 "44.1kHz", "Off", "48kHz", "32kHz",
326 };
327 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
328 uinfo->count = 1;
329 uinfo->value.enumerated.items = 4;
330 if (uinfo->value.enumerated.item >= 4)
331 uinfo->value.enumerated.item = 3;
332 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
333 return 0;
334}
335
336static int snd_akm4xxx_deemphasis_get(snd_kcontrol_t * kcontrol, snd_ctl_elem_value_t *ucontrol)
337{
338 akm4xxx_t *ak = snd_kcontrol_chip(kcontrol);
339 int chip = AK_GET_CHIP(kcontrol->private_value);
340 int addr = AK_GET_ADDR(kcontrol->private_value);
341 int shift = AK_GET_SHIFT(kcontrol->private_value);
342 ucontrol->value.enumerated.item[0] = (snd_akm4xxx_get(ak, chip, addr) >> shift) & 3;
343 return 0;
344}
345
346static int snd_akm4xxx_deemphasis_put(snd_kcontrol_t *kcontrol, snd_ctl_elem_value_t *ucontrol)
347{
348 akm4xxx_t *ak = snd_kcontrol_chip(kcontrol);
349 int chip = AK_GET_CHIP(kcontrol->private_value);
350 int addr = AK_GET_ADDR(kcontrol->private_value);
351 int shift = AK_GET_SHIFT(kcontrol->private_value);
352 unsigned char nval = ucontrol->value.enumerated.item[0] & 3;
353 int change;
354
355 nval = (nval << shift) | (snd_akm4xxx_get(ak, chip, addr) & ~(3 << shift));
356 change = snd_akm4xxx_get(ak, chip, addr) != nval;
357 if (change)
358 snd_akm4xxx_write(ak, chip, addr, nval);
359 return change;
360}
361
362/*
363 * build AK4xxx controls
364 */
365
366int snd_akm4xxx_build_controls(akm4xxx_t *ak)
367{
368 unsigned int idx, num_emphs;
369 snd_kcontrol_t *ctl;
370 int err;
371
372 ctl = kmalloc(sizeof(*ctl), GFP_KERNEL);
373 if (! ctl)
374 return -ENOMEM;
375
376 for (idx = 0; idx < ak->num_dacs; ++idx) {
377 memset(ctl, 0, sizeof(*ctl));
378 strcpy(ctl->id.name, "DAC Volume");
379 ctl->id.index = idx + ak->idx_offset * 2;
380 ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
381 ctl->count = 1;
382 ctl->info = snd_akm4xxx_volume_info;
383 ctl->get = snd_akm4xxx_volume_get;
384 ctl->put = snd_akm4xxx_volume_put;
385 switch (ak->type) {
386 case SND_AK4524:
387 ctl->private_value = AK_COMPOSE(idx/2, (idx%2) + 6, 0, 127); /* register 6 & 7 */
388 break;
389 case SND_AK4528:
390 ctl->private_value = AK_COMPOSE(idx/2, (idx%2) + 4, 0, 127); /* register 4 & 5 */
391 break;
392 case SND_AK4529: {
393 int val = idx < 6 ? idx + 2 : (idx - 6) + 0xb; /* registers 2-7 and b,c */
394 ctl->private_value = AK_COMPOSE(0, val, 0, 255) | AK_INVERT;
395 break;
396 }
397 case SND_AK4355:
398 ctl->private_value = AK_COMPOSE(0, idx + 4, 0, 255); /* register 4-9, chip #0 only */
399 break;
400 case SND_AK4358:
401 if (idx >= 6)
402 ctl->private_value = AK_COMPOSE(0, idx + 5, 0, 255); /* register 4-9, chip #0 only */
403 else
404 ctl->private_value = AK_COMPOSE(0, idx + 4, 0, 255); /* register 4-9, chip #0 only */
405 break;
406 case SND_AK4381:
407 ctl->private_value = AK_COMPOSE(idx/2, (idx%2) + 3, 0, 255); /* register 3 & 4 */
408 break;
409 default:
410 err = -EINVAL;
411 goto __error;
412 }
413 ctl->private_data = ak;
414 if ((err = snd_ctl_add(ak->card, snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|SNDRV_CTL_ELEM_ACCESS_WRITE))) < 0)
415 goto __error;
416 }
417 for (idx = 0; idx < ak->num_adcs && ak->type == SND_AK4524; ++idx) {
418 memset(ctl, 0, sizeof(*ctl));
419 strcpy(ctl->id.name, "ADC Volume");
420 ctl->id.index = idx + ak->idx_offset * 2;
421 ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
422 ctl->count = 1;
423 ctl->info = snd_akm4xxx_volume_info;
424 ctl->get = snd_akm4xxx_volume_get;
425 ctl->put = snd_akm4xxx_volume_put;
426 ctl->private_value = AK_COMPOSE(idx/2, (idx%2) + 4, 0, 127); /* register 4 & 5 */
427 ctl->private_data = ak;
428 if ((err = snd_ctl_add(ak->card, snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|SNDRV_CTL_ELEM_ACCESS_WRITE))) < 0)
429 goto __error;
430
431 memset(ctl, 0, sizeof(*ctl));
432 strcpy(ctl->id.name, "IPGA Analog Capture Volume");
433 ctl->id.index = idx + ak->idx_offset * 2;
434 ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
435 ctl->count = 1;
436 ctl->info = snd_akm4xxx_ipga_gain_info;
437 ctl->get = snd_akm4xxx_ipga_gain_get;
438 ctl->put = snd_akm4xxx_ipga_gain_put;
439 ctl->private_value = AK_COMPOSE(idx/2, (idx%2) + 4, 0, 0); /* register 4 & 5 */
440 ctl->private_data = ak;
441 if ((err = snd_ctl_add(ak->card, snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|SNDRV_CTL_ELEM_ACCESS_WRITE))) < 0)
442 goto __error;
443 }
444 if (ak->type == SND_AK4355 || ak->type == SND_AK4358)
445 num_emphs = 1;
446 else
447 num_emphs = ak->num_dacs / 2;
448 for (idx = 0; idx < num_emphs; idx++) {
449 memset(ctl, 0, sizeof(*ctl));
450 strcpy(ctl->id.name, "Deemphasis");
451 ctl->id.index = idx + ak->idx_offset;
452 ctl->id.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
453 ctl->count = 1;
454 ctl->info = snd_akm4xxx_deemphasis_info;
455 ctl->get = snd_akm4xxx_deemphasis_get;
456 ctl->put = snd_akm4xxx_deemphasis_put;
457 switch (ak->type) {
458 case SND_AK4524:
459 case SND_AK4528:
460 ctl->private_value = AK_COMPOSE(idx, 3, 0, 0); /* register 3 */
461 break;
462 case SND_AK4529: {
463 int shift = idx == 3 ? 6 : (2 - idx) * 2;
464 ctl->private_value = AK_COMPOSE(0, 8, shift, 0); /* register 8 with shift */
465 break;
466 }
467 case SND_AK4355:
468 case SND_AK4358:
469 ctl->private_value = AK_COMPOSE(idx, 3, 0, 0);
470 break;
471 case SND_AK4381:
472 ctl->private_value = AK_COMPOSE(idx, 1, 1, 0);
473 break;
474 }
475 ctl->private_data = ak;
476 if ((err = snd_ctl_add(ak->card, snd_ctl_new(ctl, SNDRV_CTL_ELEM_ACCESS_READ|SNDRV_CTL_ELEM_ACCESS_WRITE))) < 0)
477 goto __error;
478 }
479 err = 0;
480
481 __error:
482 kfree(ctl);
483 return err;
484}
485
486static int __init alsa_akm4xxx_module_init(void)
487{
488 return 0;
489}
490
491static void __exit alsa_akm4xxx_module_exit(void)
492{
493}
494
495module_init(alsa_akm4xxx_module_init)
496module_exit(alsa_akm4xxx_module_exit)
497
498EXPORT_SYMBOL(snd_akm4xxx_write);
499EXPORT_SYMBOL(snd_akm4xxx_reset);
500EXPORT_SYMBOL(snd_akm4xxx_init);
501EXPORT_SYMBOL(snd_akm4xxx_build_controls);
diff --git a/sound/i2c/other/tea575x-tuner.c b/sound/i2c/other/tea575x-tuner.c
new file mode 100644
index 000000000000..0f05a2b9a370
--- /dev/null
+++ b/sound/i2c/other/tea575x-tuner.c
@@ -0,0 +1,233 @@
1/*
2 * ALSA driver for TEA5757/5759 Philips AM/FM radio tuner chips
3 *
4 * Copyright (c) 2004 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 <asm/io.h>
25#include <linux/delay.h>
26#include <linux/interrupt.h>
27#include <linux/init.h>
28#include <sound/core.h>
29#include <sound/tea575x-tuner.h>
30
31MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
32MODULE_DESCRIPTION("Routines for control of TEA5757/5759 Philips AM/FM radio tuner chips");
33MODULE_LICENSE("GPL");
34
35/*
36 * definitions
37 */
38
39#define TEA575X_BIT_SEARCH (1<<24) /* 1 = search action, 0 = tuned */
40#define TEA575X_BIT_UPDOWN (1<<23) /* 0 = search down, 1 = search up */
41#define TEA575X_BIT_MONO (1<<22) /* 0 = stereo, 1 = mono */
42#define TEA575X_BIT_BAND_MASK (3<<20)
43#define TEA575X_BIT_BAND_FM (0<<20)
44#define TEA575X_BIT_BAND_MW (1<<20)
45#define TEA575X_BIT_BAND_LW (1<<21)
46#define TEA575X_BIT_BAND_SW (1<<22)
47#define TEA575X_BIT_PORT_0 (1<<19) /* user bit */
48#define TEA575X_BIT_PORT_1 (1<<18) /* user bit */
49#define TEA575X_BIT_SEARCH_MASK (3<<16) /* search level */
50#define TEA575X_BIT_SEARCH_5_28 (0<<16) /* FM >5uV, AM >28uV */
51#define TEA575X_BIT_SEARCH_10_40 (1<<16) /* FM >10uV, AM > 40uV */
52#define TEA575X_BIT_SEARCH_30_63 (2<<16) /* FM >30uV, AM > 63uV */
53#define TEA575X_BIT_SEARCH_150_1000 (3<<16) /* FM > 150uV, AM > 1000uV */
54#define TEA575X_BIT_DUMMY (1<<15) /* buffer */
55#define TEA575X_BIT_FREQ_MASK 0x7fff
56
57/*
58 * lowlevel part
59 */
60
61static void snd_tea575x_set_freq(tea575x_t *tea)
62{
63 unsigned long freq;
64
65 freq = tea->freq / 16; /* to kHz */
66 if (freq > 108000)
67 freq = 108000;
68 if (freq < 87000)
69 freq = 87000;
70 /* crystal fixup */
71 if (tea->tea5759)
72 freq -= tea->freq_fixup;
73 else
74 freq += tea->freq_fixup;
75 /* freq /= 12.5 */
76 freq *= 10;
77 freq /= 125;
78
79 tea->val &= ~TEA575X_BIT_FREQ_MASK;
80 tea->val |= freq & TEA575X_BIT_FREQ_MASK;
81 tea->ops->write(tea, tea->val);
82}
83
84/*
85 * Linux Video interface
86 */
87
88static int snd_tea575x_ioctl(struct inode *inode, struct file *file,
89 unsigned int cmd, unsigned long data)
90{
91 struct video_device *dev = video_devdata(file);
92 tea575x_t *tea = video_get_drvdata(dev);
93 void __user *arg = (void __user *)data;
94
95 switch(cmd) {
96 case VIDIOCGCAP:
97 {
98 struct video_capability v;
99 v.type = VID_TYPE_TUNER;
100 v.channels = 1;
101 v.audios = 1;
102 /* No we don't do pictures */
103 v.maxwidth = 0;
104 v.maxheight = 0;
105 v.minwidth = 0;
106 v.minheight = 0;
107 strcpy(v.name, tea->tea5759 ? "TEA5759" : "TEA5757");
108 if (copy_to_user(arg,&v,sizeof(v)))
109 return -EFAULT;
110 return 0;
111 }
112 case VIDIOCGTUNER:
113 {
114 struct video_tuner v;
115 if (copy_from_user(&v, arg,sizeof(v))!=0)
116 return -EFAULT;
117 if (v.tuner) /* Only 1 tuner */
118 return -EINVAL;
119 v.rangelow = (87*16000);
120 v.rangehigh = (108*16000);
121 v.flags = VIDEO_TUNER_LOW;
122 v.mode = VIDEO_MODE_AUTO;
123 strcpy(v.name, "FM");
124 v.signal = 0xFFFF;
125 if (copy_to_user(arg, &v, sizeof(v)))
126 return -EFAULT;
127 return 0;
128 }
129 case VIDIOCSTUNER:
130 {
131 struct video_tuner v;
132 if(copy_from_user(&v, arg, sizeof(v)))
133 return -EFAULT;
134 if(v.tuner!=0)
135 return -EINVAL;
136 /* Only 1 tuner so no setting needed ! */
137 return 0;
138 }
139 case VIDIOCGFREQ:
140 if(copy_to_user(arg, &tea->freq, sizeof(tea->freq)))
141 return -EFAULT;
142 return 0;
143 case VIDIOCSFREQ:
144 if(copy_from_user(&tea->freq, arg, sizeof(tea->freq)))
145 return -EFAULT;
146 snd_tea575x_set_freq(tea);
147 return 0;
148 case VIDIOCGAUDIO:
149 {
150 struct video_audio v;
151 memset(&v, 0, sizeof(v));
152 strcpy(v.name, "Radio");
153 if(copy_to_user(arg,&v, sizeof(v)))
154 return -EFAULT;
155 return 0;
156 }
157 case VIDIOCSAUDIO:
158 {
159 struct video_audio v;
160 if(copy_from_user(&v, arg, sizeof(v)))
161 return -EFAULT;
162 if(v.audio)
163 return -EINVAL;
164 return 0;
165 }
166 default:
167 return -ENOIOCTLCMD;
168 }
169}
170
171static void snd_tea575x_release(struct video_device *vfd)
172{
173}
174
175/*
176 * initialize all the tea575x chips
177 */
178void snd_tea575x_init(tea575x_t *tea)
179{
180 unsigned int val;
181
182 val = tea->ops->read(tea);
183 if (val == 0x1ffffff || val == 0) {
184 snd_printk(KERN_ERR "Cannot find TEA575x chip\n");
185 return;
186 }
187
188 memset(&tea->vd, 0, sizeof(tea->vd));
189 tea->vd.owner = tea->card->module;
190 strcpy(tea->vd.name, tea->tea5759 ? "TEA5759 radio" : "TEA5757 radio");
191 tea->vd.type = VID_TYPE_TUNER;
192 tea->vd.hardware = VID_HARDWARE_RTRACK; /* FIXME: assign new number */
193 tea->vd.release = snd_tea575x_release;
194 video_set_drvdata(&tea->vd, tea);
195 tea->vd.fops = &tea->fops;
196 tea->fops.owner = tea->card->module;
197 tea->fops.open = video_exclusive_open;
198 tea->fops.release = video_exclusive_release;
199 tea->fops.ioctl = snd_tea575x_ioctl;
200 if (video_register_device(&tea->vd, VFL_TYPE_RADIO, tea->dev_nr - 1) < 0) {
201 snd_printk(KERN_ERR "unable to register tea575x tuner\n");
202 return;
203 }
204 tea->vd_registered = 1;
205
206 tea->val = TEA575X_BIT_BAND_FM | TEA575X_BIT_SEARCH_10_40;
207 tea->freq = 90500 * 16; /* 90.5Mhz default */
208
209 snd_tea575x_set_freq(tea);
210}
211
212void snd_tea575x_exit(tea575x_t *tea)
213{
214 if (tea->vd_registered) {
215 video_unregister_device(&tea->vd);
216 tea->vd_registered = 0;
217 }
218}
219
220static int __init alsa_tea575x_module_init(void)
221{
222 return 0;
223}
224
225static void __exit alsa_tea575x_module_exit(void)
226{
227}
228
229module_init(alsa_tea575x_module_init)
230module_exit(alsa_tea575x_module_exit)
231
232EXPORT_SYMBOL(snd_tea575x_init);
233EXPORT_SYMBOL(snd_tea575x_exit);