aboutsummaryrefslogtreecommitdiffstats
path: root/sound/aoa/codecs
diff options
context:
space:
mode:
authorJohannes Berg <johannes@sipsolutions.net>2006-06-21 09:42:43 -0400
committerJaroslav Kysela <perex@suse.cz>2006-06-22 15:34:38 -0400
commitf3d9478b2ce468c3115b02ecae7e975990697f15 (patch)
tree8397671cbf251c54bf539016468164e08d0c45ee /sound/aoa/codecs
parent41f0cd3a0c4c6547860cf3b1c2d7968008e6c071 (diff)
[ALSA] snd-aoa: add snd-aoa
This large patch adds all of snd-aoa. Consisting of many modules, it currently replaces snd-powermac for all layout-id based machines and handles many more (for example new powerbooks and powermacs with digital output that previously couldn't be used at all). It also has support for all layout-IDs that Apple has (judging from their Info.plist file) but not all are tested. The driver currently has 2 known regressions over snd-powermac: * it doesn't handle powermac 7,2 and 7,3 * it doesn't have a DRC control on snapper-based machines I will fix those during the 2.6.18 development cycle. Signed-off-by: Johannes Berg <johannes@sipsolutions.net> Signed-off-by: Takashi Iwai <tiwai@suse.de>
Diffstat (limited to 'sound/aoa/codecs')
-rw-r--r--sound/aoa/codecs/Kconfig32
-rw-r--r--sound/aoa/codecs/Makefile3
-rw-r--r--sound/aoa/codecs/snd-aoa-codec-onyx.c1113
-rw-r--r--sound/aoa/codecs/snd-aoa-codec-onyx.h76
-rw-r--r--sound/aoa/codecs/snd-aoa-codec-tas-gain-table.h209
-rw-r--r--sound/aoa/codecs/snd-aoa-codec-tas.c654
-rw-r--r--sound/aoa/codecs/snd-aoa-codec-tas.h47
-rw-r--r--sound/aoa/codecs/snd-aoa-codec-toonie.c141
8 files changed, 2275 insertions, 0 deletions
diff --git a/sound/aoa/codecs/Kconfig b/sound/aoa/codecs/Kconfig
new file mode 100644
index 000000000000..90cf58f68630
--- /dev/null
+++ b/sound/aoa/codecs/Kconfig
@@ -0,0 +1,32 @@
1config SND_AOA_ONYX
2 tristate "support Onyx chip"
3 depends on SND_AOA
4 ---help---
5 This option enables support for the Onyx (pcm3052)
6 codec chip found in the latest Apple machines
7 (most of those with digital audio output).
8
9#config SND_AOA_TOPAZ
10# tristate "support Topaz chips"
11# depends on SND_AOA
12# ---help---
13# This option enables support for the Topaz (CS84xx)
14# codec chips found in the latest Apple machines,
15# these chips do the digital input and output on
16# some PowerMacs.
17
18config SND_AOA_TAS
19 tristate "support TAS chips"
20 depends on SND_AOA
21 ---help---
22 This option enables support for the tas chips
23 found in a lot of Apple Machines, especially
24 iBooks and PowerBooks without digital.
25
26config SND_AOA_TOONIE
27 tristate "support Toonie chip"
28 depends on SND_AOA
29 ---help---
30 This option enables support for the toonie codec
31 found in the Mac Mini. If you have a Mac Mini and
32 want to hear sound, select this option.
diff --git a/sound/aoa/codecs/Makefile b/sound/aoa/codecs/Makefile
new file mode 100644
index 000000000000..31cbe68fd42f
--- /dev/null
+++ b/sound/aoa/codecs/Makefile
@@ -0,0 +1,3 @@
1obj-$(CONFIG_SND_AOA_ONYX) += snd-aoa-codec-onyx.o
2obj-$(CONFIG_SND_AOA_TAS) += snd-aoa-codec-tas.o
3obj-$(CONFIG_SND_AOA_TOONIE) += snd-aoa-codec-toonie.o
diff --git a/sound/aoa/codecs/snd-aoa-codec-onyx.c b/sound/aoa/codecs/snd-aoa-codec-onyx.c
new file mode 100644
index 000000000000..0b7650788f1f
--- /dev/null
+++ b/sound/aoa/codecs/snd-aoa-codec-onyx.c
@@ -0,0 +1,1113 @@
1/*
2 * Apple Onboard Audio driver for Onyx codec
3 *
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
5 *
6 * GPL v2, can be found in COPYING.
7 *
8 *
9 * This is a driver for the pcm3052 codec chip (codenamed Onyx)
10 * that is present in newer Apple hardware (with digital output).
11 *
12 * The Onyx codec has the following connections (listed by the bit
13 * to be used in aoa_codec.connected):
14 * 0: analog output
15 * 1: digital output
16 * 2: line input
17 * 3: microphone input
18 * Note that even though I know of no machine that has for example
19 * the digital output connected but not the analog, I have handled
20 * all the different cases in the code so that this driver may serve
21 * as a good example of what to do.
22 *
23 * NOTE: This driver assumes that there's at most one chip to be
24 * used with one alsa card, in form of creating all kinds
25 * of mixer elements without regard for their existence.
26 * But snd-aoa assumes that there's at most one card, so
27 * this means you can only have one onyx on a system. This
28 * should probably be fixed by changing the assumption of
29 * having just a single card on a system, and making the
30 * 'card' pointer accessible to anyone who needs it instead
31 * of hiding it in the aoa_snd_* functions...
32 *
33 */
34#include <linux/delay.h>
35#include <linux/module.h>
36MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
37MODULE_LICENSE("GPL");
38MODULE_DESCRIPTION("pcm3052 (onyx) codec driver for snd-aoa");
39
40#include "snd-aoa-codec-onyx.h"
41#include "../aoa.h"
42#include "../soundbus/soundbus.h"
43
44
45#define PFX "snd-aoa-codec-onyx: "
46
47struct onyx {
48 /* cache registers 65 to 80, they are write-only! */
49 u8 cache[16];
50 struct i2c_client i2c;
51 struct aoa_codec codec;
52 u32 initialised:1,
53 spdif_locked:1,
54 analog_locked:1,
55 original_mute:2;
56 int open_count;
57 struct codec_info *codec_info;
58
59 /* mutex serializes concurrent access to the device
60 * and this structure.
61 */
62 struct mutex mutex;
63};
64#define codec_to_onyx(c) container_of(c, struct onyx, codec)
65
66/* both return 0 if all ok, else on error */
67static int onyx_read_register(struct onyx *onyx, u8 reg, u8 *value)
68{
69 s32 v;
70
71 if (reg != ONYX_REG_CONTROL) {
72 *value = onyx->cache[reg-FIRSTREGISTER];
73 return 0;
74 }
75 v = i2c_smbus_read_byte_data(&onyx->i2c, reg);
76 if (v < 0)
77 return -1;
78 *value = (u8)v;
79 onyx->cache[ONYX_REG_CONTROL-FIRSTREGISTER] = *value;
80 return 0;
81}
82
83static int onyx_write_register(struct onyx *onyx, u8 reg, u8 value)
84{
85 int result;
86
87 result = i2c_smbus_write_byte_data(&onyx->i2c, reg, value);
88 if (!result)
89 onyx->cache[reg-FIRSTREGISTER] = value;
90 return result;
91}
92
93/* alsa stuff */
94
95static int onyx_dev_register(struct snd_device *dev)
96{
97 return 0;
98}
99
100static struct snd_device_ops ops = {
101 .dev_register = onyx_dev_register,
102};
103
104/* this is necessary because most alsa mixer programs
105 * can't properly handle the negative range */
106#define VOLUME_RANGE_SHIFT 128
107
108static int onyx_snd_vol_info(struct snd_kcontrol *kcontrol,
109 struct snd_ctl_elem_info *uinfo)
110{
111 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
112 uinfo->count = 2;
113 uinfo->value.integer.min = -128 + VOLUME_RANGE_SHIFT;
114 uinfo->value.integer.max = -1 + VOLUME_RANGE_SHIFT;
115 return 0;
116}
117
118static int onyx_snd_vol_get(struct snd_kcontrol *kcontrol,
119 struct snd_ctl_elem_value *ucontrol)
120{
121 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
122 s8 l, r;
123
124 mutex_lock(&onyx->mutex);
125 onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_LEFT, &l);
126 onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_RIGHT, &r);
127 mutex_unlock(&onyx->mutex);
128
129 ucontrol->value.integer.value[0] = l + VOLUME_RANGE_SHIFT;
130 ucontrol->value.integer.value[1] = r + VOLUME_RANGE_SHIFT;
131
132 return 0;
133}
134
135static int onyx_snd_vol_put(struct snd_kcontrol *kcontrol,
136 struct snd_ctl_elem_value *ucontrol)
137{
138 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
139 s8 l, r;
140
141 mutex_lock(&onyx->mutex);
142 onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_LEFT, &l);
143 onyx_read_register(onyx, ONYX_REG_DAC_ATTEN_RIGHT, &r);
144
145 if (l + VOLUME_RANGE_SHIFT == ucontrol->value.integer.value[0] &&
146 r + VOLUME_RANGE_SHIFT == ucontrol->value.integer.value[1]) {
147 mutex_unlock(&onyx->mutex);
148 return 0;
149 }
150
151 onyx_write_register(onyx, ONYX_REG_DAC_ATTEN_LEFT,
152 ucontrol->value.integer.value[0]
153 - VOLUME_RANGE_SHIFT);
154 onyx_write_register(onyx, ONYX_REG_DAC_ATTEN_RIGHT,
155 ucontrol->value.integer.value[1]
156 - VOLUME_RANGE_SHIFT);
157 mutex_unlock(&onyx->mutex);
158
159 return 1;
160}
161
162static struct snd_kcontrol_new volume_control = {
163 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
164 .name = "Master Playback Volume",
165 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
166 .info = onyx_snd_vol_info,
167 .get = onyx_snd_vol_get,
168 .put = onyx_snd_vol_put,
169};
170
171/* like above, this is necessary because a lot
172 * of alsa mixer programs don't handle ranges
173 * that don't start at 0 properly.
174 * even alsamixer is one of them... */
175#define INPUTGAIN_RANGE_SHIFT (-3)
176
177static int onyx_snd_inputgain_info(struct snd_kcontrol *kcontrol,
178 struct snd_ctl_elem_info *uinfo)
179{
180 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
181 uinfo->count = 1;
182 uinfo->value.integer.min = 3 + INPUTGAIN_RANGE_SHIFT;
183 uinfo->value.integer.max = 28 + INPUTGAIN_RANGE_SHIFT;
184 return 0;
185}
186
187static int onyx_snd_inputgain_get(struct snd_kcontrol *kcontrol,
188 struct snd_ctl_elem_value *ucontrol)
189{
190 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
191 u8 ig;
192
193 mutex_lock(&onyx->mutex);
194 onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &ig);
195 mutex_unlock(&onyx->mutex);
196
197 ucontrol->value.integer.value[0] =
198 (ig & ONYX_ADC_PGA_GAIN_MASK) + INPUTGAIN_RANGE_SHIFT;
199
200 return 0;
201}
202
203static int onyx_snd_inputgain_put(struct snd_kcontrol *kcontrol,
204 struct snd_ctl_elem_value *ucontrol)
205{
206 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
207 u8 v, n;
208
209 mutex_lock(&onyx->mutex);
210 onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
211 n = v;
212 n &= ~ONYX_ADC_PGA_GAIN_MASK;
213 n |= (ucontrol->value.integer.value[0] - INPUTGAIN_RANGE_SHIFT)
214 & ONYX_ADC_PGA_GAIN_MASK;
215 onyx_write_register(onyx, ONYX_REG_ADC_CONTROL, n);
216 mutex_unlock(&onyx->mutex);
217
218 return n != v;
219}
220
221static struct snd_kcontrol_new inputgain_control = {
222 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
223 .name = "Master Capture Volume",
224 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
225 .info = onyx_snd_inputgain_info,
226 .get = onyx_snd_inputgain_get,
227 .put = onyx_snd_inputgain_put,
228};
229
230static int onyx_snd_capture_source_info(struct snd_kcontrol *kcontrol,
231 struct snd_ctl_elem_info *uinfo)
232{
233 static char *texts[] = { "Line-In", "Microphone" };
234
235 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
236 uinfo->count = 1;
237 uinfo->value.enumerated.items = 2;
238 if (uinfo->value.enumerated.item > 1)
239 uinfo->value.enumerated.item = 1;
240 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
241 return 0;
242}
243
244static int onyx_snd_capture_source_get(struct snd_kcontrol *kcontrol,
245 struct snd_ctl_elem_value *ucontrol)
246{
247 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
248 s8 v;
249
250 mutex_lock(&onyx->mutex);
251 onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
252 mutex_unlock(&onyx->mutex);
253
254 ucontrol->value.enumerated.item[0] = !!(v&ONYX_ADC_INPUT_MIC);
255
256 return 0;
257}
258
259static void onyx_set_capture_source(struct onyx *onyx, int mic)
260{
261 s8 v;
262
263 mutex_lock(&onyx->mutex);
264 onyx_read_register(onyx, ONYX_REG_ADC_CONTROL, &v);
265 v &= ~ONYX_ADC_INPUT_MIC;
266 if (mic)
267 v |= ONYX_ADC_INPUT_MIC;
268 onyx_write_register(onyx, ONYX_REG_ADC_CONTROL, v);
269 mutex_unlock(&onyx->mutex);
270}
271
272static int onyx_snd_capture_source_put(struct snd_kcontrol *kcontrol,
273 struct snd_ctl_elem_value *ucontrol)
274{
275 onyx_set_capture_source(snd_kcontrol_chip(kcontrol),
276 ucontrol->value.enumerated.item[0]);
277 return 1;
278}
279
280static struct snd_kcontrol_new capture_source_control = {
281 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
282 /* If we name this 'Input Source', it properly shows up in
283 * alsamixer as a selection, * but it's shown under the
284 * 'Playback' category.
285 * If I name it 'Capture Source', it shows up in strange
286 * ways (two bools of which one can be selected at a
287 * time) but at least it's shown in the 'Capture'
288 * category.
289 * I was told that this was due to backward compatibility,
290 * but I don't understand then why the mangling is *not*
291 * done when I name it "Input Source".....
292 */
293 .name = "Capture Source",
294 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
295 .info = onyx_snd_capture_source_info,
296 .get = onyx_snd_capture_source_get,
297 .put = onyx_snd_capture_source_put,
298};
299
300static int onyx_snd_mute_info(struct snd_kcontrol *kcontrol,
301 struct snd_ctl_elem_info *uinfo)
302{
303 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
304 uinfo->count = 2;
305 uinfo->value.integer.min = 0;
306 uinfo->value.integer.max = 1;
307 return 0;
308}
309
310static int onyx_snd_mute_get(struct snd_kcontrol *kcontrol,
311 struct snd_ctl_elem_value *ucontrol)
312{
313 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
314 u8 c;
315
316 mutex_lock(&onyx->mutex);
317 onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &c);
318 mutex_unlock(&onyx->mutex);
319
320 ucontrol->value.integer.value[0] = !(c & ONYX_MUTE_LEFT);
321 ucontrol->value.integer.value[1] = !(c & ONYX_MUTE_RIGHT);
322
323 return 0;
324}
325
326static int onyx_snd_mute_put(struct snd_kcontrol *kcontrol,
327 struct snd_ctl_elem_value *ucontrol)
328{
329 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
330 u8 v = 0, c = 0;
331 int err = -EBUSY;
332
333 mutex_lock(&onyx->mutex);
334 if (onyx->analog_locked)
335 goto out_unlock;
336
337 onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
338 c = v;
339 c &= ~(ONYX_MUTE_RIGHT | ONYX_MUTE_LEFT);
340 if (!ucontrol->value.integer.value[0])
341 c |= ONYX_MUTE_LEFT;
342 if (!ucontrol->value.integer.value[1])
343 c |= ONYX_MUTE_RIGHT;
344 err = onyx_write_register(onyx, ONYX_REG_DAC_CONTROL, c);
345
346 out_unlock:
347 mutex_unlock(&onyx->mutex);
348
349 return !err ? (v != c) : err;
350}
351
352static struct snd_kcontrol_new mute_control = {
353 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
354 .name = "Master Playback Switch",
355 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
356 .info = onyx_snd_mute_info,
357 .get = onyx_snd_mute_get,
358 .put = onyx_snd_mute_put,
359};
360
361
362static int onyx_snd_single_bit_info(struct snd_kcontrol *kcontrol,
363 struct snd_ctl_elem_info *uinfo)
364{
365 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
366 uinfo->count = 1;
367 uinfo->value.integer.min = 0;
368 uinfo->value.integer.max = 1;
369 return 0;
370}
371
372#define FLAG_POLARITY_INVERT 1
373#define FLAG_SPDIFLOCK 2
374
375static int onyx_snd_single_bit_get(struct snd_kcontrol *kcontrol,
376 struct snd_ctl_elem_value *ucontrol)
377{
378 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
379 u8 c;
380 long int pv = kcontrol->private_value;
381 u8 polarity = (pv >> 16) & FLAG_POLARITY_INVERT;
382 u8 address = (pv >> 8) & 0xff;
383 u8 mask = pv & 0xff;
384
385 mutex_lock(&onyx->mutex);
386 onyx_read_register(onyx, address, &c);
387 mutex_unlock(&onyx->mutex);
388
389 ucontrol->value.integer.value[0] = !!(c & mask) ^ polarity;
390
391 return 0;
392}
393
394static int onyx_snd_single_bit_put(struct snd_kcontrol *kcontrol,
395 struct snd_ctl_elem_value *ucontrol)
396{
397 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
398 u8 v = 0, c = 0;
399 int err;
400 long int pv = kcontrol->private_value;
401 u8 polarity = (pv >> 16) & FLAG_POLARITY_INVERT;
402 u8 spdiflock = (pv >> 16) & FLAG_SPDIFLOCK;
403 u8 address = (pv >> 8) & 0xff;
404 u8 mask = pv & 0xff;
405
406 mutex_lock(&onyx->mutex);
407 if (spdiflock && onyx->spdif_locked) {
408 /* even if alsamixer doesn't care.. */
409 err = -EBUSY;
410 goto out_unlock;
411 }
412 onyx_read_register(onyx, address, &v);
413 c = v;
414 c &= ~(mask);
415 if (!!ucontrol->value.integer.value[0] ^ polarity)
416 c |= mask;
417 err = onyx_write_register(onyx, address, c);
418
419 out_unlock:
420 mutex_unlock(&onyx->mutex);
421
422 return !err ? (v != c) : err;
423}
424
425#define SINGLE_BIT(n, type, description, address, mask, flags) \
426static struct snd_kcontrol_new n##_control = { \
427 .iface = SNDRV_CTL_ELEM_IFACE_##type, \
428 .name = description, \
429 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
430 .info = onyx_snd_single_bit_info, \
431 .get = onyx_snd_single_bit_get, \
432 .put = onyx_snd_single_bit_put, \
433 .private_value = (flags << 16) | (address << 8) | mask \
434}
435
436SINGLE_BIT(spdif,
437 MIXER,
438 SNDRV_CTL_NAME_IEC958("", PLAYBACK, SWITCH),
439 ONYX_REG_DIG_INFO4,
440 ONYX_SPDIF_ENABLE,
441 FLAG_SPDIFLOCK);
442SINGLE_BIT(ovr1,
443 MIXER,
444 "Oversampling Rate",
445 ONYX_REG_DAC_CONTROL,
446 ONYX_OVR1,
447 0);
448SINGLE_BIT(flt0,
449 MIXER,
450 "Fast Digital Filter Rolloff",
451 ONYX_REG_DAC_FILTER,
452 ONYX_ROLLOFF_FAST,
453 FLAG_POLARITY_INVERT);
454SINGLE_BIT(hpf,
455 MIXER,
456 "Highpass Filter",
457 ONYX_REG_ADC_HPF_BYPASS,
458 ONYX_HPF_DISABLE,
459 FLAG_POLARITY_INVERT);
460SINGLE_BIT(dm12,
461 MIXER,
462 "Digital De-Emphasis",
463 ONYX_REG_DAC_DEEMPH,
464 ONYX_DIGDEEMPH_CTRL,
465 0);
466
467static int onyx_spdif_info(struct snd_kcontrol *kcontrol,
468 struct snd_ctl_elem_info *uinfo)
469{
470 uinfo->type = SNDRV_CTL_ELEM_TYPE_IEC958;
471 uinfo->count = 1;
472 return 0;
473}
474
475static int onyx_spdif_mask_get(struct snd_kcontrol *kcontrol,
476 struct snd_ctl_elem_value *ucontrol)
477{
478 /* datasheet page 30, all others are 0 */
479 ucontrol->value.iec958.status[0] = 0x3e;
480 ucontrol->value.iec958.status[1] = 0xff;
481
482 ucontrol->value.iec958.status[3] = 0x3f;
483 ucontrol->value.iec958.status[4] = 0x0f;
484
485 return 0;
486}
487
488static struct snd_kcontrol_new onyx_spdif_mask = {
489 .access = SNDRV_CTL_ELEM_ACCESS_READ,
490 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
491 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,CON_MASK),
492 .info = onyx_spdif_info,
493 .get = onyx_spdif_mask_get,
494};
495
496static int onyx_spdif_get(struct snd_kcontrol *kcontrol,
497 struct snd_ctl_elem_value *ucontrol)
498{
499 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
500 u8 v;
501
502 mutex_lock(&onyx->mutex);
503 onyx_read_register(onyx, ONYX_REG_DIG_INFO1, &v);
504 ucontrol->value.iec958.status[0] = v & 0x3e;
505
506 onyx_read_register(onyx, ONYX_REG_DIG_INFO2, &v);
507 ucontrol->value.iec958.status[1] = v;
508
509 onyx_read_register(onyx, ONYX_REG_DIG_INFO3, &v);
510 ucontrol->value.iec958.status[3] = v & 0x3f;
511
512 onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
513 ucontrol->value.iec958.status[4] = v & 0x0f;
514 mutex_unlock(&onyx->mutex);
515
516 return 0;
517}
518
519static int onyx_spdif_put(struct snd_kcontrol *kcontrol,
520 struct snd_ctl_elem_value *ucontrol)
521{
522 struct onyx *onyx = snd_kcontrol_chip(kcontrol);
523 u8 v;
524
525 mutex_lock(&onyx->mutex);
526 onyx_read_register(onyx, ONYX_REG_DIG_INFO1, &v);
527 v = (v & ~0x3e) | (ucontrol->value.iec958.status[0] & 0x3e);
528 onyx_write_register(onyx, ONYX_REG_DIG_INFO1, v);
529
530 v = ucontrol->value.iec958.status[1];
531 onyx_write_register(onyx, ONYX_REG_DIG_INFO2, v);
532
533 onyx_read_register(onyx, ONYX_REG_DIG_INFO3, &v);
534 v = (v & ~0x3f) | (ucontrol->value.iec958.status[3] & 0x3f);
535 onyx_write_register(onyx, ONYX_REG_DIG_INFO3, v);
536
537 onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
538 v = (v & ~0x0f) | (ucontrol->value.iec958.status[4] & 0x0f);
539 onyx_write_register(onyx, ONYX_REG_DIG_INFO4, v);
540 mutex_unlock(&onyx->mutex);
541
542 return 1;
543}
544
545static struct snd_kcontrol_new onyx_spdif_ctrl = {
546 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
547 .iface = SNDRV_CTL_ELEM_IFACE_PCM,
548 .name = SNDRV_CTL_NAME_IEC958("",PLAYBACK,DEFAULT),
549 .info = onyx_spdif_info,
550 .get = onyx_spdif_get,
551 .put = onyx_spdif_put,
552};
553
554/* our registers */
555
556static u8 register_map[] = {
557 ONYX_REG_DAC_ATTEN_LEFT,
558 ONYX_REG_DAC_ATTEN_RIGHT,
559 ONYX_REG_CONTROL,
560 ONYX_REG_DAC_CONTROL,
561 ONYX_REG_DAC_DEEMPH,
562 ONYX_REG_DAC_FILTER,
563 ONYX_REG_DAC_OUTPHASE,
564 ONYX_REG_ADC_CONTROL,
565 ONYX_REG_ADC_HPF_BYPASS,
566 ONYX_REG_DIG_INFO1,
567 ONYX_REG_DIG_INFO2,
568 ONYX_REG_DIG_INFO3,
569 ONYX_REG_DIG_INFO4
570};
571
572static u8 initial_values[ARRAY_SIZE(register_map)] = {
573 0x80, 0x80, /* muted */
574 ONYX_MRST | ONYX_SRST, /* but handled specially! */
575 ONYX_MUTE_LEFT | ONYX_MUTE_RIGHT,
576 0, /* no deemphasis */
577 ONYX_DAC_FILTER_ALWAYS,
578 ONYX_OUTPHASE_INVERTED,
579 (-1 /*dB*/ + 8) & 0xF, /* line in selected, -1 dB gain*/
580 ONYX_ADC_HPF_ALWAYS,
581 (1<<2), /* pcm audio */
582 2, /* category: pcm coder */
583 0, /* sampling frequency 44.1 kHz, clock accuracy level II */
584 1 /* 24 bit depth */
585};
586
587/* reset registers of chip, either to initial or to previous values */
588static int onyx_register_init(struct onyx *onyx)
589{
590 int i;
591 u8 val;
592 u8 regs[sizeof(initial_values)];
593
594 if (!onyx->initialised) {
595 memcpy(regs, initial_values, sizeof(initial_values));
596 if (onyx_read_register(onyx, ONYX_REG_CONTROL, &val))
597 return -1;
598 val &= ~ONYX_SILICONVERSION;
599 val |= initial_values[3];
600 regs[3] = val;
601 } else {
602 for (i=0; i<sizeof(register_map); i++)
603 regs[i] = onyx->cache[register_map[i]-FIRSTREGISTER];
604 }
605
606 for (i=0; i<sizeof(register_map); i++) {
607 if (onyx_write_register(onyx, register_map[i], regs[i]))
608 return -1;
609 }
610 onyx->initialised = 1;
611 return 0;
612}
613
614static struct transfer_info onyx_transfers[] = {
615 /* this is first so we can skip it if no input is present...
616 * No hardware exists with that, but it's here as an example
617 * of what to do :) */
618 {
619 /* analog input */
620 .formats = SNDRV_PCM_FMTBIT_S8 |
621 SNDRV_PCM_FMTBIT_S16_BE |
622 SNDRV_PCM_FMTBIT_S24_BE,
623 .rates = SNDRV_PCM_RATE_8000_96000,
624 .transfer_in = 1,
625 .must_be_clock_source = 0,
626 .tag = 0,
627 },
628 {
629 /* if analog and digital are currently off, anything should go,
630 * so this entry describes everything we can do... */
631 .formats = SNDRV_PCM_FMTBIT_S8 |
632 SNDRV_PCM_FMTBIT_S16_BE |
633 SNDRV_PCM_FMTBIT_S24_BE
634#ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
635 | SNDRV_PCM_FMTBIT_COMPRESSED_16BE
636#endif
637 ,
638 .rates = SNDRV_PCM_RATE_8000_96000,
639 .tag = 0,
640 },
641 {
642 /* analog output */
643 .formats = SNDRV_PCM_FMTBIT_S8 |
644 SNDRV_PCM_FMTBIT_S16_BE |
645 SNDRV_PCM_FMTBIT_S24_BE,
646 .rates = SNDRV_PCM_RATE_8000_96000,
647 .transfer_in = 0,
648 .must_be_clock_source = 0,
649 .tag = 1,
650 },
651 {
652 /* digital pcm output, also possible for analog out */
653 .formats = SNDRV_PCM_FMTBIT_S8 |
654 SNDRV_PCM_FMTBIT_S16_BE |
655 SNDRV_PCM_FMTBIT_S24_BE,
656 .rates = SNDRV_PCM_RATE_32000 |
657 SNDRV_PCM_RATE_44100 |
658 SNDRV_PCM_RATE_48000,
659 .transfer_in = 0,
660 .must_be_clock_source = 0,
661 .tag = 2,
662 },
663#ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
664Once alsa gets supports for this kind of thing we can add it...
665 {
666 /* digital compressed output */
667 .formats = SNDRV_PCM_FMTBIT_COMPRESSED_16BE,
668 .rates = SNDRV_PCM_RATE_32000 |
669 SNDRV_PCM_RATE_44100 |
670 SNDRV_PCM_RATE_48000,
671 .tag = 2,
672 },
673#endif
674 {}
675};
676
677static int onyx_usable(struct codec_info_item *cii,
678 struct transfer_info *ti,
679 struct transfer_info *out)
680{
681 u8 v;
682 struct onyx *onyx = cii->codec_data;
683 int spdif_enabled, analog_enabled;
684
685 mutex_lock(&onyx->mutex);
686 onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
687 spdif_enabled = !!(v & ONYX_SPDIF_ENABLE);
688 onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
689 analog_enabled =
690 (v & (ONYX_MUTE_RIGHT|ONYX_MUTE_LEFT))
691 != (ONYX_MUTE_RIGHT|ONYX_MUTE_LEFT);
692 mutex_unlock(&onyx->mutex);
693
694 switch (ti->tag) {
695 case 0: return 1;
696 case 1: return analog_enabled;
697 case 2: return spdif_enabled;
698 }
699 return 1;
700}
701
702static int onyx_prepare(struct codec_info_item *cii,
703 struct bus_info *bi,
704 struct snd_pcm_substream *substream)
705{
706 u8 v;
707 struct onyx *onyx = cii->codec_data;
708 int err = -EBUSY;
709
710 mutex_lock(&onyx->mutex);
711
712#ifdef SNDRV_PCM_FMTBIT_COMPRESSED_16BE
713 if (substream->runtime->format == SNDRV_PCM_FMTBIT_COMPRESSED_16BE) {
714 /* mute and lock analog output */
715 onyx_read_register(onyx, ONYX_REG_DAC_CONTROL, &v);
716 if (onyx_write_register(onyx
717 ONYX_REG_DAC_CONTROL,
718 v | ONYX_MUTE_RIGHT | ONYX_MUTE_LEFT))
719 goto out_unlock;
720 onyx->analog_locked = 1;
721 err = 0;
722 goto out_unlock;
723 }
724#endif
725 switch (substream->runtime->rate) {
726 case 32000:
727 case 44100:
728 case 48000:
729 /* these rates are ok for all outputs */
730 /* FIXME: program spdif channel control bits here so that
731 * userspace doesn't have to if it only plays pcm! */
732 err = 0;
733 goto out_unlock;
734 default:
735 /* got some rate that the digital output can't do,
736 * so disable and lock it */
737 onyx_read_register(cii->codec_data, ONYX_REG_DIG_INFO4, &v);
738 if (onyx_write_register(onyx,
739 ONYX_REG_DIG_INFO4,
740 v & ~ONYX_SPDIF_ENABLE))
741 goto out_unlock;
742 onyx->spdif_locked = 1;
743 err = 0;
744 goto out_unlock;
745 }
746
747 out_unlock:
748 mutex_unlock(&onyx->mutex);
749
750 return err;
751}
752
753static int onyx_open(struct codec_info_item *cii,
754 struct snd_pcm_substream *substream)
755{
756 struct onyx *onyx = cii->codec_data;
757
758 mutex_lock(&onyx->mutex);
759 onyx->open_count++;
760 mutex_unlock(&onyx->mutex);
761
762 return 0;
763}
764
765static int onyx_close(struct codec_info_item *cii,
766 struct snd_pcm_substream *substream)
767{
768 struct onyx *onyx = cii->codec_data;
769
770 mutex_lock(&onyx->mutex);
771 onyx->open_count--;
772 if (!onyx->open_count)
773 onyx->spdif_locked = onyx->analog_locked = 0;
774 mutex_unlock(&onyx->mutex);
775
776 return 0;
777}
778
779static int onyx_switch_clock(struct codec_info_item *cii,
780 enum clock_switch what)
781{
782 struct onyx *onyx = cii->codec_data;
783
784 mutex_lock(&onyx->mutex);
785 /* this *MUST* be more elaborate later... */
786 switch (what) {
787 case CLOCK_SWITCH_PREPARE_SLAVE:
788 onyx->codec.gpio->methods->all_amps_off(onyx->codec.gpio);
789 break;
790 case CLOCK_SWITCH_SLAVE:
791 onyx->codec.gpio->methods->all_amps_restore(onyx->codec.gpio);
792 break;
793 default: /* silence warning */
794 break;
795 }
796 mutex_unlock(&onyx->mutex);
797
798 return 0;
799}
800
801#ifdef CONFIG_PM
802
803static int onyx_suspend(struct codec_info_item *cii, pm_message_t state)
804{
805 struct onyx *onyx = cii->codec_data;
806 u8 v;
807 int err = -ENXIO;
808
809 mutex_lock(&onyx->mutex);
810 if (onyx_read_register(onyx, ONYX_REG_CONTROL, &v))
811 goto out_unlock;
812 onyx_write_register(onyx, ONYX_REG_CONTROL, v | ONYX_ADPSV | ONYX_DAPSV);
813 /* Apple does a sleep here but the datasheet says to do it on resume */
814 err = 0;
815 out_unlock:
816 mutex_unlock(&onyx->mutex);
817
818 return err;
819}
820
821static int onyx_resume(struct codec_info_item *cii)
822{
823 struct onyx *onyx = cii->codec_data;
824 u8 v;
825 int err = -ENXIO;
826
827 mutex_lock(&onyx->mutex);
828 /* take codec out of suspend */
829 if (onyx_read_register(onyx, ONYX_REG_CONTROL, &v))
830 goto out_unlock;
831 onyx_write_register(onyx, ONYX_REG_CONTROL, v & ~(ONYX_ADPSV | ONYX_DAPSV));
832 /* FIXME: should divide by sample rate, but 8k is the lowest we go */
833 msleep(2205000/8000);
834 /* reset all values */
835 onyx_register_init(onyx);
836 err = 0;
837 out_unlock:
838 mutex_unlock(&onyx->mutex);
839
840 return err;
841}
842
843#endif /* CONFIG_PM */
844
845static struct codec_info onyx_codec_info = {
846 .transfers = onyx_transfers,
847 .sysclock_factor = 256,
848 .bus_factor = 64,
849 .owner = THIS_MODULE,
850 .usable = onyx_usable,
851 .prepare = onyx_prepare,
852 .open = onyx_open,
853 .close = onyx_close,
854 .switch_clock = onyx_switch_clock,
855#ifdef CONFIG_PM
856 .suspend = onyx_suspend,
857 .resume = onyx_resume,
858#endif
859};
860
861static int onyx_init_codec(struct aoa_codec *codec)
862{
863 struct onyx *onyx = codec_to_onyx(codec);
864 struct snd_kcontrol *ctl;
865 struct codec_info *ci = &onyx_codec_info;
866 u8 v;
867 int err;
868
869 if (!onyx->codec.gpio || !onyx->codec.gpio->methods) {
870 printk(KERN_ERR PFX "gpios not assigned!!\n");
871 return -EINVAL;
872 }
873
874 onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
875 msleep(1);
876 onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 1);
877 msleep(1);
878 onyx->codec.gpio->methods->set_hw_reset(onyx->codec.gpio, 0);
879 msleep(1);
880
881 if (onyx_register_init(onyx)) {
882 printk(KERN_ERR PFX "failed to initialise onyx registers\n");
883 return -ENODEV;
884 }
885
886 if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, onyx, &ops)) {
887 printk(KERN_ERR PFX "failed to create onyx snd device!\n");
888 return -ENODEV;
889 }
890
891 /* nothing connected? what a joke! */
892 if ((onyx->codec.connected & 0xF) == 0)
893 return -ENOTCONN;
894
895 /* if no inputs are present... */
896 if ((onyx->codec.connected & 0xC) == 0) {
897 if (!onyx->codec_info)
898 onyx->codec_info = kmalloc(sizeof(struct codec_info), GFP_KERNEL);
899 if (!onyx->codec_info)
900 return -ENOMEM;
901 ci = onyx->codec_info;
902 *ci = onyx_codec_info;
903 ci->transfers++;
904 }
905
906 /* if no outputs are present... */
907 if ((onyx->codec.connected & 3) == 0) {
908 if (!onyx->codec_info)
909 onyx->codec_info = kmalloc(sizeof(struct codec_info), GFP_KERNEL);
910 if (!onyx->codec_info)
911 return -ENOMEM;
912 ci = onyx->codec_info;
913 /* this is fine as there have to be inputs
914 * if we end up in this part of the code */
915 *ci = onyx_codec_info;
916 ci->transfers[1].formats = 0;
917 }
918
919 if (onyx->codec.soundbus_dev->attach_codec(onyx->codec.soundbus_dev,
920 aoa_get_card(),
921 ci, onyx)) {
922 printk(KERN_ERR PFX "error creating onyx pcm\n");
923 return -ENODEV;
924 }
925#define ADDCTL(n) \
926 do { \
927 ctl = snd_ctl_new1(&n, onyx); \
928 if (ctl) { \
929 ctl->id.device = \
930 onyx->codec.soundbus_dev->pcm->device; \
931 err = aoa_snd_ctl_add(ctl); \
932 if (err) \
933 goto error; \
934 } \
935 } while (0)
936
937 if (onyx->codec.soundbus_dev->pcm) {
938 /* give the user appropriate controls
939 * depending on what inputs are connected */
940 if ((onyx->codec.connected & 0xC) == 0xC)
941 ADDCTL(capture_source_control);
942 else if (onyx->codec.connected & 4)
943 onyx_set_capture_source(onyx, 0);
944 else
945 onyx_set_capture_source(onyx, 1);
946 if (onyx->codec.connected & 0xC)
947 ADDCTL(inputgain_control);
948
949 /* depending on what output is connected,
950 * give the user appropriate controls */
951 if (onyx->codec.connected & 1) {
952 ADDCTL(volume_control);
953 ADDCTL(mute_control);
954 ADDCTL(ovr1_control);
955 ADDCTL(flt0_control);
956 ADDCTL(hpf_control);
957 ADDCTL(dm12_control);
958 /* spdif control defaults to off */
959 }
960 if (onyx->codec.connected & 2) {
961 ADDCTL(onyx_spdif_mask);
962 ADDCTL(onyx_spdif_ctrl);
963 }
964 if ((onyx->codec.connected & 3) == 3)
965 ADDCTL(spdif_control);
966 /* if only S/PDIF is connected, enable it unconditionally */
967 if ((onyx->codec.connected & 3) == 2) {
968 onyx_read_register(onyx, ONYX_REG_DIG_INFO4, &v);
969 v |= ONYX_SPDIF_ENABLE;
970 onyx_write_register(onyx, ONYX_REG_DIG_INFO4, v);
971 }
972 }
973#undef ADDCTL
974 printk(KERN_INFO PFX "attached to onyx codec via i2c\n");
975
976 return 0;
977 error:
978 onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
979 snd_device_free(aoa_get_card(), onyx);
980 return err;
981}
982
983static void onyx_exit_codec(struct aoa_codec *codec)
984{
985 struct onyx *onyx = codec_to_onyx(codec);
986
987 if (!onyx->codec.soundbus_dev) {
988 printk(KERN_ERR PFX "onyx_exit_codec called without soundbus_dev!\n");
989 return;
990 }
991 onyx->codec.soundbus_dev->detach_codec(onyx->codec.soundbus_dev, onyx);
992}
993
994static struct i2c_driver onyx_driver;
995
996static int onyx_create(struct i2c_adapter *adapter,
997 struct device_node *node,
998 int addr)
999{
1000 struct onyx *onyx;
1001 u8 dummy;
1002
1003 onyx = kzalloc(sizeof(struct onyx), GFP_KERNEL);
1004
1005 if (!onyx)
1006 return -ENOMEM;
1007
1008 mutex_init(&onyx->mutex);
1009 onyx->i2c.driver = &onyx_driver;
1010 onyx->i2c.adapter = adapter;
1011 onyx->i2c.addr = addr & 0x7f;
1012 strlcpy(onyx->i2c.name, "onyx audio codec", I2C_NAME_SIZE-1);
1013
1014 if (i2c_attach_client(&onyx->i2c)) {
1015 printk(KERN_ERR PFX "failed to attach to i2c\n");
1016 goto fail;
1017 }
1018
1019 /* we try to read from register ONYX_REG_CONTROL
1020 * to check if the codec is present */
1021 if (onyx_read_register(onyx, ONYX_REG_CONTROL, &dummy) != 0) {
1022 i2c_detach_client(&onyx->i2c);
1023 printk(KERN_ERR PFX "failed to read control register\n");
1024 goto fail;
1025 }
1026
1027 strlcpy(onyx->codec.name, "onyx", MAX_CODEC_NAME_LEN-1);
1028 onyx->codec.owner = THIS_MODULE;
1029 onyx->codec.init = onyx_init_codec;
1030 onyx->codec.exit = onyx_exit_codec;
1031 onyx->codec.node = of_node_get(node);
1032
1033 if (aoa_codec_register(&onyx->codec)) {
1034 i2c_detach_client(&onyx->i2c);
1035 goto fail;
1036 }
1037 printk(KERN_DEBUG PFX "created and attached onyx instance\n");
1038 return 0;
1039 fail:
1040 kfree(onyx);
1041 return -EINVAL;
1042}
1043
1044static int onyx_i2c_attach(struct i2c_adapter *adapter)
1045{
1046 struct device_node *busnode, *dev = NULL;
1047 struct pmac_i2c_bus *bus;
1048
1049 bus = pmac_i2c_adapter_to_bus(adapter);
1050 if (bus == NULL)
1051 return -ENODEV;
1052 busnode = pmac_i2c_get_bus_node(bus);
1053
1054 while ((dev = of_get_next_child(busnode, dev)) != NULL) {
1055 if (device_is_compatible(dev, "pcm3052")) {
1056 u32 *addr;
1057 printk(KERN_DEBUG PFX "found pcm3052\n");
1058 addr = (u32 *) get_property(dev, "reg", NULL);
1059 if (!addr)
1060 return -ENODEV;
1061 return onyx_create(adapter, dev, (*addr)>>1);
1062 }
1063 }
1064
1065 /* if that didn't work, try desperate mode for older
1066 * machines that have stuff missing from the device tree */
1067
1068 if (!device_is_compatible(busnode, "k2-i2c"))
1069 return -ENODEV;
1070
1071 printk(KERN_DEBUG PFX "found k2-i2c, checking if onyx chip is on it\n");
1072 /* probe both possible addresses for the onyx chip */
1073 if (onyx_create(adapter, NULL, 0x46) == 0)
1074 return 0;
1075 return onyx_create(adapter, NULL, 0x47);
1076}
1077
1078static int onyx_i2c_detach(struct i2c_client *client)
1079{
1080 struct onyx *onyx = container_of(client, struct onyx, i2c);
1081 int err;
1082
1083 if ((err = i2c_detach_client(client)))
1084 return err;
1085 aoa_codec_unregister(&onyx->codec);
1086 of_node_put(onyx->codec.node);
1087 if (onyx->codec_info)
1088 kfree(onyx->codec_info);
1089 kfree(onyx);
1090 return 0;
1091}
1092
1093static struct i2c_driver onyx_driver = {
1094 .driver = {
1095 .name = "aoa_codec_onyx",
1096 .owner = THIS_MODULE,
1097 },
1098 .attach_adapter = onyx_i2c_attach,
1099 .detach_client = onyx_i2c_detach,
1100};
1101
1102static int __init onyx_init(void)
1103{
1104 return i2c_add_driver(&onyx_driver);
1105}
1106
1107static void __exit onyx_exit(void)
1108{
1109 i2c_del_driver(&onyx_driver);
1110}
1111
1112module_init(onyx_init);
1113module_exit(onyx_exit);
diff --git a/sound/aoa/codecs/snd-aoa-codec-onyx.h b/sound/aoa/codecs/snd-aoa-codec-onyx.h
new file mode 100644
index 000000000000..aeedda773699
--- /dev/null
+++ b/sound/aoa/codecs/snd-aoa-codec-onyx.h
@@ -0,0 +1,76 @@
1/*
2 * Apple Onboard Audio driver for Onyx codec (header)
3 *
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
5 *
6 * GPL v2, can be found in COPYING.
7 */
8#ifndef __SND_AOA_CODEC_ONYX_H
9#define __SND_AOA_CODEC_ONYX_H
10#include <stddef.h>
11#include <linux/i2c.h>
12#include <linux/i2c-dev.h>
13#include <asm/pmac_low_i2c.h>
14#include <asm/prom.h>
15
16/* PCM3052 register definitions */
17
18/* the attenuation registers take values from
19 * -1 (0dB) to -127 (-63.0 dB) or others (muted) */
20#define ONYX_REG_DAC_ATTEN_LEFT 65
21#define FIRSTREGISTER ONYX_REG_DAC_ATTEN_LEFT
22#define ONYX_REG_DAC_ATTEN_RIGHT 66
23
24#define ONYX_REG_CONTROL 67
25# define ONYX_MRST (1<<7)
26# define ONYX_SRST (1<<6)
27# define ONYX_ADPSV (1<<5)
28# define ONYX_DAPSV (1<<4)
29# define ONYX_SILICONVERSION (1<<0)
30/* all others reserved */
31
32#define ONYX_REG_DAC_CONTROL 68
33# define ONYX_OVR1 (1<<6)
34# define ONYX_MUTE_RIGHT (1<<1)
35# define ONYX_MUTE_LEFT (1<<0)
36
37#define ONYX_REG_DAC_DEEMPH 69
38# define ONYX_DIGDEEMPH_SHIFT 5
39# define ONYX_DIGDEEMPH_MASK (3<<ONYX_DIGDEEMPH_SHIFT)
40# define ONYX_DIGDEEMPH_CTRL (1<<4)
41
42#define ONYX_REG_DAC_FILTER 70
43# define ONYX_ROLLOFF_FAST (1<<5)
44# define ONYX_DAC_FILTER_ALWAYS (1<<2)
45
46#define ONYX_REG_DAC_OUTPHASE 71
47# define ONYX_OUTPHASE_INVERTED (1<<0)
48
49#define ONYX_REG_ADC_CONTROL 72
50# define ONYX_ADC_INPUT_MIC (1<<5)
51/* 8 + input gain in dB, valid range for input gain is -4 .. 20 dB */
52# define ONYX_ADC_PGA_GAIN_MASK 0x1f
53
54#define ONYX_REG_ADC_HPF_BYPASS 75
55# define ONYX_HPF_DISABLE (1<<3)
56# define ONYX_ADC_HPF_ALWAYS (1<<2)
57
58#define ONYX_REG_DIG_INFO1 77
59# define ONYX_MASK_DIN_TO_BPZ (1<<7)
60/* bits 1-5 control channel bits 1-5 */
61# define ONYX_DIGOUT_DISABLE (1<<0)
62
63#define ONYX_REG_DIG_INFO2 78
64/* controls channel bits 8-15 */
65
66#define ONYX_REG_DIG_INFO3 79
67/* control channel bits 24-29, high 2 bits reserved */
68
69#define ONYX_REG_DIG_INFO4 80
70# define ONYX_VALIDL (1<<7)
71# define ONYX_VALIDR (1<<6)
72# define ONYX_SPDIF_ENABLE (1<<5)
73/* lower 4 bits control bits 32-35 of channel control and word length */
74# define ONYX_WORDLEN_MASK (0xF)
75
76#endif /* __SND_AOA_CODEC_ONYX_H */
diff --git a/sound/aoa/codecs/snd-aoa-codec-tas-gain-table.h b/sound/aoa/codecs/snd-aoa-codec-tas-gain-table.h
new file mode 100644
index 000000000000..4cfa6757715e
--- /dev/null
+++ b/sound/aoa/codecs/snd-aoa-codec-tas-gain-table.h
@@ -0,0 +1,209 @@
1/*
2 This is the program used to generate below table.
3
4#include <stdio.h>
5#include <math.h>
6int main() {
7 int dB2;
8 printf("/" "* This file is only included exactly once!\n");
9 printf(" *\n");
10 printf(" * If they'd only tell us that generating this table was\n");
11 printf(" * as easy as calculating\n");
12 printf(" * hwvalue = 1048576.0*exp(0.057564628*dB*2)\n");
13 printf(" * :) *" "/\n");
14 printf("static int tas_gaintable[] = {\n");
15 printf(" 0x000000, /" "* -infinity dB *" "/\n");
16 for (dB2=-140;dB2<=36;dB2++)
17 printf(" 0x%.6x, /" "* %-02.1f dB *" "/\n", (int)(1048576.0*exp(0.057564628*dB2)), dB2/2.0);
18 printf("};\n\n");
19}
20
21*/
22
23/* This file is only included exactly once!
24 *
25 * If they'd only tell us that generating this table was
26 * as easy as calculating
27 * hwvalue = 1048576.0*exp(0.057564628*dB*2)
28 * :) */
29static int tas_gaintable[] = {
30 0x000000, /* -infinity dB */
31 0x00014b, /* -70.0 dB */
32 0x00015f, /* -69.5 dB */
33 0x000174, /* -69.0 dB */
34 0x00018a, /* -68.5 dB */
35 0x0001a1, /* -68.0 dB */
36 0x0001ba, /* -67.5 dB */
37 0x0001d4, /* -67.0 dB */
38 0x0001f0, /* -66.5 dB */
39 0x00020d, /* -66.0 dB */
40 0x00022c, /* -65.5 dB */
41 0x00024d, /* -65.0 dB */
42 0x000270, /* -64.5 dB */
43 0x000295, /* -64.0 dB */
44 0x0002bc, /* -63.5 dB */
45 0x0002e6, /* -63.0 dB */
46 0x000312, /* -62.5 dB */
47 0x000340, /* -62.0 dB */
48 0x000372, /* -61.5 dB */
49 0x0003a6, /* -61.0 dB */
50 0x0003dd, /* -60.5 dB */
51 0x000418, /* -60.0 dB */
52 0x000456, /* -59.5 dB */
53 0x000498, /* -59.0 dB */
54 0x0004de, /* -58.5 dB */
55 0x000528, /* -58.0 dB */
56 0x000576, /* -57.5 dB */
57 0x0005c9, /* -57.0 dB */
58 0x000620, /* -56.5 dB */
59 0x00067d, /* -56.0 dB */
60 0x0006e0, /* -55.5 dB */
61 0x000748, /* -55.0 dB */
62 0x0007b7, /* -54.5 dB */
63 0x00082c, /* -54.0 dB */
64 0x0008a8, /* -53.5 dB */
65 0x00092b, /* -53.0 dB */
66 0x0009b6, /* -52.5 dB */
67 0x000a49, /* -52.0 dB */
68 0x000ae5, /* -51.5 dB */
69 0x000b8b, /* -51.0 dB */
70 0x000c3a, /* -50.5 dB */
71 0x000cf3, /* -50.0 dB */
72 0x000db8, /* -49.5 dB */
73 0x000e88, /* -49.0 dB */
74 0x000f64, /* -48.5 dB */
75 0x00104e, /* -48.0 dB */
76 0x001145, /* -47.5 dB */
77 0x00124b, /* -47.0 dB */
78 0x001361, /* -46.5 dB */
79 0x001487, /* -46.0 dB */
80 0x0015be, /* -45.5 dB */
81 0x001708, /* -45.0 dB */
82 0x001865, /* -44.5 dB */
83 0x0019d8, /* -44.0 dB */
84 0x001b60, /* -43.5 dB */
85 0x001cff, /* -43.0 dB */
86 0x001eb7, /* -42.5 dB */
87 0x002089, /* -42.0 dB */
88 0x002276, /* -41.5 dB */
89 0x002481, /* -41.0 dB */
90 0x0026ab, /* -40.5 dB */
91 0x0028f5, /* -40.0 dB */
92 0x002b63, /* -39.5 dB */
93 0x002df5, /* -39.0 dB */
94 0x0030ae, /* -38.5 dB */
95 0x003390, /* -38.0 dB */
96 0x00369e, /* -37.5 dB */
97 0x0039db, /* -37.0 dB */
98 0x003d49, /* -36.5 dB */
99 0x0040ea, /* -36.0 dB */
100 0x0044c3, /* -35.5 dB */
101 0x0048d6, /* -35.0 dB */
102 0x004d27, /* -34.5 dB */
103 0x0051b9, /* -34.0 dB */
104 0x005691, /* -33.5 dB */
105 0x005bb2, /* -33.0 dB */
106 0x006121, /* -32.5 dB */
107 0x0066e3, /* -32.0 dB */
108 0x006cfb, /* -31.5 dB */
109 0x007370, /* -31.0 dB */
110 0x007a48, /* -30.5 dB */
111 0x008186, /* -30.0 dB */
112 0x008933, /* -29.5 dB */
113 0x009154, /* -29.0 dB */
114 0x0099f1, /* -28.5 dB */
115 0x00a310, /* -28.0 dB */
116 0x00acba, /* -27.5 dB */
117 0x00b6f6, /* -27.0 dB */
118 0x00c1cd, /* -26.5 dB */
119 0x00cd49, /* -26.0 dB */
120 0x00d973, /* -25.5 dB */
121 0x00e655, /* -25.0 dB */
122 0x00f3fb, /* -24.5 dB */
123 0x010270, /* -24.0 dB */
124 0x0111c0, /* -23.5 dB */
125 0x0121f9, /* -23.0 dB */
126 0x013328, /* -22.5 dB */
127 0x01455b, /* -22.0 dB */
128 0x0158a2, /* -21.5 dB */
129 0x016d0e, /* -21.0 dB */
130 0x0182af, /* -20.5 dB */
131 0x019999, /* -20.0 dB */
132 0x01b1de, /* -19.5 dB */
133 0x01cb94, /* -19.0 dB */
134 0x01e6cf, /* -18.5 dB */
135 0x0203a7, /* -18.0 dB */
136 0x022235, /* -17.5 dB */
137 0x024293, /* -17.0 dB */
138 0x0264db, /* -16.5 dB */
139 0x02892c, /* -16.0 dB */
140 0x02afa3, /* -15.5 dB */
141 0x02d862, /* -15.0 dB */
142 0x03038a, /* -14.5 dB */
143 0x033142, /* -14.0 dB */
144 0x0361af, /* -13.5 dB */
145 0x0394fa, /* -13.0 dB */
146 0x03cb50, /* -12.5 dB */
147 0x0404de, /* -12.0 dB */
148 0x0441d5, /* -11.5 dB */
149 0x048268, /* -11.0 dB */
150 0x04c6d0, /* -10.5 dB */
151 0x050f44, /* -10.0 dB */
152 0x055c04, /* -9.5 dB */
153 0x05ad50, /* -9.0 dB */
154 0x06036e, /* -8.5 dB */
155 0x065ea5, /* -8.0 dB */
156 0x06bf44, /* -7.5 dB */
157 0x07259d, /* -7.0 dB */
158 0x079207, /* -6.5 dB */
159 0x0804dc, /* -6.0 dB */
160 0x087e80, /* -5.5 dB */
161 0x08ff59, /* -5.0 dB */
162 0x0987d5, /* -4.5 dB */
163 0x0a1866, /* -4.0 dB */
164 0x0ab189, /* -3.5 dB */
165 0x0b53be, /* -3.0 dB */
166 0x0bff91, /* -2.5 dB */
167 0x0cb591, /* -2.0 dB */
168 0x0d765a, /* -1.5 dB */
169 0x0e4290, /* -1.0 dB */
170 0x0f1adf, /* -0.5 dB */
171 0x100000, /* 0.0 dB */
172 0x10f2b4, /* 0.5 dB */
173 0x11f3c9, /* 1.0 dB */
174 0x13041a, /* 1.5 dB */
175 0x14248e, /* 2.0 dB */
176 0x15561a, /* 2.5 dB */
177 0x1699c0, /* 3.0 dB */
178 0x17f094, /* 3.5 dB */
179 0x195bb8, /* 4.0 dB */
180 0x1adc61, /* 4.5 dB */
181 0x1c73d5, /* 5.0 dB */
182 0x1e236d, /* 5.5 dB */
183 0x1fec98, /* 6.0 dB */
184 0x21d0d9, /* 6.5 dB */
185 0x23d1cd, /* 7.0 dB */
186 0x25f125, /* 7.5 dB */
187 0x2830af, /* 8.0 dB */
188 0x2a9254, /* 8.5 dB */
189 0x2d1818, /* 9.0 dB */
190 0x2fc420, /* 9.5 dB */
191 0x3298b0, /* 10.0 dB */
192 0x35982f, /* 10.5 dB */
193 0x38c528, /* 11.0 dB */
194 0x3c224c, /* 11.5 dB */
195 0x3fb278, /* 12.0 dB */
196 0x4378b0, /* 12.5 dB */
197 0x477829, /* 13.0 dB */
198 0x4bb446, /* 13.5 dB */
199 0x5030a1, /* 14.0 dB */
200 0x54f106, /* 14.5 dB */
201 0x59f980, /* 15.0 dB */
202 0x5f4e52, /* 15.5 dB */
203 0x64f403, /* 16.0 dB */
204 0x6aef5e, /* 16.5 dB */
205 0x714575, /* 17.0 dB */
206 0x77fbaa, /* 17.5 dB */
207 0x7f17af, /* 18.0 dB */
208};
209
diff --git a/sound/aoa/codecs/snd-aoa-codec-tas.c b/sound/aoa/codecs/snd-aoa-codec-tas.c
new file mode 100644
index 000000000000..2e39ff6ee349
--- /dev/null
+++ b/sound/aoa/codecs/snd-aoa-codec-tas.c
@@ -0,0 +1,654 @@
1/*
2 * Apple Onboard Audio driver for tas codec
3 *
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
5 *
6 * GPL v2, can be found in COPYING.
7 *
8 * Open questions:
9 * - How to distinguish between 3004 and versions?
10 *
11 * FIXMEs:
12 * - This codec driver doesn't honour the 'connected'
13 * property of the aoa_codec struct, hence if
14 * it is used in machines where not everything is
15 * connected it will display wrong mixer elements.
16 * - Driver assumes that the microphone is always
17 * monaureal and connected to the right channel of
18 * the input. This should also be a codec-dependent
19 * flag, maybe the codec should have 3 different
20 * bits for the three different possibilities how
21 * it can be hooked up...
22 * But as long as I don't see any hardware hooked
23 * up that way...
24 * - As Apple notes in their code, the tas3004 seems
25 * to delay the right channel by one sample. You can
26 * see this when for example recording stereo in
27 * audacity, or recording the tas output via cable
28 * on another machine (use a sinus generator or so).
29 * I tried programming the BiQuads but couldn't
30 * make the delay work, maybe someone can read the
31 * datasheet and fix it. The relevant Apple comment
32 * is in AppleTAS3004Audio.cpp lines 1637 ff. Note
33 * that their comment describing how they program
34 * the filters sucks...
35 *
36 * Other things:
37 * - this should actually register *two* aoa_codec
38 * structs since it has two inputs. Then it must
39 * use the prepare callback to forbid running the
40 * secondary output on a different clock.
41 * Also, whatever bus knows how to do this must
42 * provide two soundbus_dev devices and the fabric
43 * must be able to link them correctly.
44 *
45 * I don't even know if Apple ever uses the second
46 * port on the tas3004 though, I don't think their
47 * i2s controllers can even do it. OTOH, they all
48 * derive the clocks from common clocks, so it
49 * might just be possible. The framework allows the
50 * codec to refine the transfer_info items in the
51 * usable callback, so we can simply remove the
52 * rates the second instance is not using when it
53 * actually is in use.
54 * Maybe we'll need to make the sound busses have
55 * a 'clock group id' value so the codec can
56 * determine if the two outputs can be driven at
57 * the same time. But that is likely overkill, up
58 * to the fabric to not link them up incorrectly,
59 * and up to the hardware designer to not wire
60 * them up in some weird unusable way.
61 */
62#include <stddef.h>
63#include <linux/i2c.h>
64#include <linux/i2c-dev.h>
65#include <asm/pmac_low_i2c.h>
66#include <asm/prom.h>
67#include <linux/delay.h>
68#include <linux/module.h>
69MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
70MODULE_LICENSE("GPL");
71MODULE_DESCRIPTION("tas codec driver for snd-aoa");
72
73#include "snd-aoa-codec-tas.h"
74#include "snd-aoa-codec-tas-gain-table.h"
75#include "../aoa.h"
76#include "../soundbus/soundbus.h"
77
78
79#define PFX "snd-aoa-codec-tas: "
80
81struct tas {
82 struct aoa_codec codec;
83 struct i2c_client i2c;
84 u32 muted_l:1, muted_r:1,
85 controls_created:1;
86 u8 cached_volume_l, cached_volume_r;
87 u8 mixer_l[3], mixer_r[3];
88 u8 acr;
89};
90
91static struct tas *codec_to_tas(struct aoa_codec *codec)
92{
93 return container_of(codec, struct tas, codec);
94}
95
96static inline int tas_write_reg(struct tas *tas, u8 reg, u8 len, u8 *data)
97{
98 if (len == 1)
99 return i2c_smbus_write_byte_data(&tas->i2c, reg, *data);
100 else
101 return i2c_smbus_write_i2c_block_data(&tas->i2c, reg, len, data);
102}
103
104static void tas_set_volume(struct tas *tas)
105{
106 u8 block[6];
107 int tmp;
108 u8 left, right;
109
110 left = tas->cached_volume_l;
111 right = tas->cached_volume_r;
112
113 if (left > 177) left = 177;
114 if (right > 177) right = 177;
115
116 if (tas->muted_l) left = 0;
117 if (tas->muted_r) right = 0;
118
119 /* analysing the volume and mixer tables shows
120 * that they are similar enough when we shift
121 * the mixer table down by 4 bits. The error
122 * is miniscule, in just one item the error
123 * is 1, at a value of 0x07f17b (mixer table
124 * value is 0x07f17a) */
125 tmp = tas_gaintable[left];
126 block[0] = tmp>>20;
127 block[1] = tmp>>12;
128 block[2] = tmp>>4;
129 tmp = tas_gaintable[right];
130 block[3] = tmp>>20;
131 block[4] = tmp>>12;
132 block[5] = tmp>>4;
133 tas_write_reg(tas, TAS_REG_VOL, 6, block);
134}
135
136static void tas_set_mixer(struct tas *tas)
137{
138 u8 block[9];
139 int tmp, i;
140 u8 val;
141
142 for (i=0;i<3;i++) {
143 val = tas->mixer_l[i];
144 if (val > 177) val = 177;
145 tmp = tas_gaintable[val];
146 block[3*i+0] = tmp>>16;
147 block[3*i+1] = tmp>>8;
148 block[3*i+2] = tmp;
149 }
150 tas_write_reg(tas, TAS_REG_LMIX, 9, block);
151
152 for (i=0;i<3;i++) {
153 val = tas->mixer_r[i];
154 if (val > 177) val = 177;
155 tmp = tas_gaintable[val];
156 block[3*i+0] = tmp>>16;
157 block[3*i+1] = tmp>>8;
158 block[3*i+2] = tmp;
159 }
160 tas_write_reg(tas, TAS_REG_RMIX, 9, block);
161}
162
163/* alsa stuff */
164
165static int tas_dev_register(struct snd_device *dev)
166{
167 return 0;
168}
169
170static struct snd_device_ops ops = {
171 .dev_register = tas_dev_register,
172};
173
174static int tas_snd_vol_info(struct snd_kcontrol *kcontrol,
175 struct snd_ctl_elem_info *uinfo)
176{
177 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
178 uinfo->count = 2;
179 uinfo->value.integer.min = 0;
180 uinfo->value.integer.max = 177;
181 return 0;
182}
183
184static int tas_snd_vol_get(struct snd_kcontrol *kcontrol,
185 struct snd_ctl_elem_value *ucontrol)
186{
187 struct tas *tas = snd_kcontrol_chip(kcontrol);
188
189 ucontrol->value.integer.value[0] = tas->cached_volume_l;
190 ucontrol->value.integer.value[1] = tas->cached_volume_r;
191 return 0;
192}
193
194static int tas_snd_vol_put(struct snd_kcontrol *kcontrol,
195 struct snd_ctl_elem_value *ucontrol)
196{
197 struct tas *tas = snd_kcontrol_chip(kcontrol);
198
199 if (tas->cached_volume_l == ucontrol->value.integer.value[0]
200 && tas->cached_volume_r == ucontrol->value.integer.value[1])
201 return 0;
202
203 tas->cached_volume_l = ucontrol->value.integer.value[0];
204 tas->cached_volume_r = ucontrol->value.integer.value[1];
205 tas_set_volume(tas);
206 return 1;
207}
208
209static struct snd_kcontrol_new volume_control = {
210 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
211 .name = "Master Playback Volume",
212 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
213 .info = tas_snd_vol_info,
214 .get = tas_snd_vol_get,
215 .put = tas_snd_vol_put,
216};
217
218static int tas_snd_mute_info(struct snd_kcontrol *kcontrol,
219 struct snd_ctl_elem_info *uinfo)
220{
221 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
222 uinfo->count = 2;
223 uinfo->value.integer.min = 0;
224 uinfo->value.integer.max = 1;
225 return 0;
226}
227
228static int tas_snd_mute_get(struct snd_kcontrol *kcontrol,
229 struct snd_ctl_elem_value *ucontrol)
230{
231 struct tas *tas = snd_kcontrol_chip(kcontrol);
232
233 ucontrol->value.integer.value[0] = !tas->muted_l;
234 ucontrol->value.integer.value[1] = !tas->muted_r;
235 return 0;
236}
237
238static int tas_snd_mute_put(struct snd_kcontrol *kcontrol,
239 struct snd_ctl_elem_value *ucontrol)
240{
241 struct tas *tas = snd_kcontrol_chip(kcontrol);
242
243 if (tas->muted_l == !ucontrol->value.integer.value[0]
244 && tas->muted_r == !ucontrol->value.integer.value[1])
245 return 0;
246
247 tas->muted_l = !ucontrol->value.integer.value[0];
248 tas->muted_r = !ucontrol->value.integer.value[1];
249 tas_set_volume(tas);
250 return 1;
251}
252
253static struct snd_kcontrol_new mute_control = {
254 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
255 .name = "Master Playback Switch",
256 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
257 .info = tas_snd_mute_info,
258 .get = tas_snd_mute_get,
259 .put = tas_snd_mute_put,
260};
261
262static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol,
263 struct snd_ctl_elem_info *uinfo)
264{
265 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
266 uinfo->count = 2;
267 uinfo->value.integer.min = 0;
268 uinfo->value.integer.max = 177;
269 return 0;
270}
271
272static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol,
273 struct snd_ctl_elem_value *ucontrol)
274{
275 struct tas *tas = snd_kcontrol_chip(kcontrol);
276 int idx = kcontrol->private_value;
277
278 ucontrol->value.integer.value[0] = tas->mixer_l[idx];
279 ucontrol->value.integer.value[1] = tas->mixer_r[idx];
280
281 return 0;
282}
283
284static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol,
285 struct snd_ctl_elem_value *ucontrol)
286{
287 struct tas *tas = snd_kcontrol_chip(kcontrol);
288 int idx = kcontrol->private_value;
289
290 if (tas->mixer_l[idx] == ucontrol->value.integer.value[0]
291 && tas->mixer_r[idx] == ucontrol->value.integer.value[1])
292 return 0;
293
294 tas->mixer_l[idx] = ucontrol->value.integer.value[0];
295 tas->mixer_r[idx] = ucontrol->value.integer.value[1];
296
297 tas_set_mixer(tas);
298 return 1;
299}
300
301#define MIXER_CONTROL(n,descr,idx) \
302static struct snd_kcontrol_new n##_control = { \
303 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
304 .name = descr " Playback Volume", \
305 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
306 .info = tas_snd_mixer_info, \
307 .get = tas_snd_mixer_get, \
308 .put = tas_snd_mixer_put, \
309 .private_value = idx, \
310}
311
312MIXER_CONTROL(pcm1, "PCM1", 0);
313MIXER_CONTROL(monitor, "Monitor", 2);
314
315static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol,
316 struct snd_ctl_elem_info *uinfo)
317{
318 static char *texts[] = { "Line-In", "Microphone" };
319
320 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
321 uinfo->count = 1;
322 uinfo->value.enumerated.items = 2;
323 if (uinfo->value.enumerated.item > 1)
324 uinfo->value.enumerated.item = 1;
325 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
326 return 0;
327}
328
329static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
330 struct snd_ctl_elem_value *ucontrol)
331{
332 struct tas *tas = snd_kcontrol_chip(kcontrol);
333
334 ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
335 return 0;
336}
337
338static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
339 struct snd_ctl_elem_value *ucontrol)
340{
341 struct tas *tas = snd_kcontrol_chip(kcontrol);
342 int oldacr = tas->acr;
343
344 tas->acr &= ~TAS_ACR_INPUT_B;
345 if (ucontrol->value.enumerated.item[0])
346 tas->acr |= TAS_ACR_INPUT_B;
347 if (oldacr == tas->acr)
348 return 0;
349 tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
350 return 1;
351}
352
353static struct snd_kcontrol_new capture_source_control = {
354 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
355 /* If we name this 'Input Source', it properly shows up in
356 * alsamixer as a selection, * but it's shown under the
357 * 'Playback' category.
358 * If I name it 'Capture Source', it shows up in strange
359 * ways (two bools of which one can be selected at a
360 * time) but at least it's shown in the 'Capture'
361 * category.
362 * I was told that this was due to backward compatibility,
363 * but I don't understand then why the mangling is *not*
364 * done when I name it "Input Source".....
365 */
366 .name = "Capture Source",
367 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
368 .info = tas_snd_capture_source_info,
369 .get = tas_snd_capture_source_get,
370 .put = tas_snd_capture_source_put,
371};
372
373
374static struct transfer_info tas_transfers[] = {
375 {
376 /* input */
377 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
378 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE,
379 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
380 .transfer_in = 1,
381 },
382 {
383 /* output */
384 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
385 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE,
386 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
387 .transfer_in = 0,
388 },
389 {}
390};
391
392static int tas_usable(struct codec_info_item *cii,
393 struct transfer_info *ti,
394 struct transfer_info *out)
395{
396 return 1;
397}
398
399static int tas_reset_init(struct tas *tas)
400{
401 u8 tmp;
402 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
403 msleep(1);
404 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1);
405 msleep(1);
406 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
407 msleep(1);
408
409 tas->acr &= ~TAS_ACR_ANALOG_PDOWN;
410 tas->acr |= TAS_ACR_B_MONAUREAL | TAS_ACR_B_MON_SEL_RIGHT;
411 if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
412 return -ENODEV;
413
414 tmp = TAS_MCS_SCLK64 | TAS_MCS_SPORT_MODE_I2S | TAS_MCS_SPORT_WL_24BIT;
415 if (tas_write_reg(tas, TAS_REG_MCS, 1, &tmp))
416 return -ENODEV;
417
418 tmp = 0;
419 if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp))
420 return -ENODEV;
421
422 return 0;
423}
424
425/* we are controlled via i2c and assume that is always up
426 * If that wasn't the case, we'd have to suspend once
427 * our i2c device is suspended, and then take note of that! */
428static int tas_suspend(struct tas *tas)
429{
430 tas->acr |= TAS_ACR_ANALOG_PDOWN;
431 tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
432 return 0;
433}
434
435static int tas_resume(struct tas *tas)
436{
437 /* reset codec */
438 tas_reset_init(tas);
439 tas_set_volume(tas);
440 tas_set_mixer(tas);
441 return 0;
442}
443
444#ifdef CONFIG_PM
445static int _tas_suspend(struct codec_info_item *cii, pm_message_t state)
446{
447 return tas_suspend(cii->codec_data);
448}
449
450static int _tas_resume(struct codec_info_item *cii)
451{
452 return tas_resume(cii->codec_data);
453}
454#endif
455
456static struct codec_info tas_codec_info = {
457 .transfers = tas_transfers,
458 /* in theory, we can drive it at 512 too...
459 * but so far the framework doesn't allow
460 * for that and I don't see much point in it. */
461 .sysclock_factor = 256,
462 /* same here, could be 32 for just one 16 bit format */
463 .bus_factor = 64,
464 .owner = THIS_MODULE,
465 .usable = tas_usable,
466#ifdef CONFIG_PM
467 .suspend = _tas_suspend,
468 .resume = _tas_resume,
469#endif
470};
471
472static int tas_init_codec(struct aoa_codec *codec)
473{
474 struct tas *tas = codec_to_tas(codec);
475 int err;
476
477 if (!tas->codec.gpio || !tas->codec.gpio->methods) {
478 printk(KERN_ERR PFX "gpios not assigned!!\n");
479 return -EINVAL;
480 }
481
482 if (tas_reset_init(tas)) {
483 printk(KERN_ERR PFX "tas failed to initialise\n");
484 return -ENXIO;
485 }
486
487 if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev,
488 aoa_get_card(),
489 &tas_codec_info, tas)) {
490 printk(KERN_ERR PFX "error attaching tas to soundbus\n");
491 return -ENODEV;
492 }
493
494 if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, tas, &ops)) {
495 printk(KERN_ERR PFX "failed to create tas snd device!\n");
496 return -ENODEV;
497 }
498 err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas));
499 if (err)
500 goto error;
501
502 err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas));
503 if (err)
504 goto error;
505
506 err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas));
507 if (err)
508 goto error;
509
510 err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas));
511 if (err)
512 goto error;
513
514 err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas));
515 if (err)
516 goto error;
517
518 return 0;
519 error:
520 tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
521 snd_device_free(aoa_get_card(), tas);
522 return err;
523}
524
525static void tas_exit_codec(struct aoa_codec *codec)
526{
527 struct tas *tas = codec_to_tas(codec);
528
529 if (!tas->codec.soundbus_dev)
530 return;
531 tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
532}
533
534
535static struct i2c_driver tas_driver;
536
537static int tas_create(struct i2c_adapter *adapter,
538 struct device_node *node,
539 int addr)
540{
541 struct tas *tas;
542
543 tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
544
545 if (!tas)
546 return -ENOMEM;
547
548 tas->i2c.driver = &tas_driver;
549 tas->i2c.adapter = adapter;
550 tas->i2c.addr = addr;
551 strlcpy(tas->i2c.name, "tas audio codec", I2C_NAME_SIZE-1);
552
553 if (i2c_attach_client(&tas->i2c)) {
554 printk(KERN_ERR PFX "failed to attach to i2c\n");
555 goto fail;
556 }
557
558 strlcpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN-1);
559 tas->codec.owner = THIS_MODULE;
560 tas->codec.init = tas_init_codec;
561 tas->codec.exit = tas_exit_codec;
562 tas->codec.node = of_node_get(node);
563
564 if (aoa_codec_register(&tas->codec)) {
565 goto detach;
566 }
567 printk(KERN_DEBUG "snd-aoa-codec-tas: created and attached tas instance\n");
568 return 0;
569 detach:
570 i2c_detach_client(&tas->i2c);
571 fail:
572 kfree(tas);
573 return -EINVAL;
574}
575
576static int tas_i2c_attach(struct i2c_adapter *adapter)
577{
578 struct device_node *busnode, *dev = NULL;
579 struct pmac_i2c_bus *bus;
580
581 bus = pmac_i2c_adapter_to_bus(adapter);
582 if (bus == NULL)
583 return -ENODEV;
584 busnode = pmac_i2c_get_bus_node(bus);
585
586 while ((dev = of_get_next_child(busnode, dev)) != NULL) {
587 if (device_is_compatible(dev, "tas3004")) {
588 u32 *addr;
589 printk(KERN_DEBUG PFX "found tas3004\n");
590 addr = (u32 *) get_property(dev, "reg", NULL);
591 if (!addr)
592 continue;
593 return tas_create(adapter, dev, ((*addr) >> 1) & 0x7f);
594 }
595 /* older machines have no 'codec' node with a 'compatible'
596 * property that says 'tas3004', they just have a 'deq'
597 * node without any such property... */
598 if (strcmp(dev->name, "deq") == 0) {
599 u32 *_addr, addr;
600 printk(KERN_DEBUG PFX "found 'deq' node\n");
601 _addr = (u32 *) get_property(dev, "i2c-address", NULL);
602 if (!_addr)
603 continue;
604 addr = ((*_addr) >> 1) & 0x7f;
605 /* now, if the address doesn't match any of the two
606 * that a tas3004 can have, we cannot handle this.
607 * I doubt it ever happens but hey. */
608 if (addr != 0x34 && addr != 0x35)
609 continue;
610 return tas_create(adapter, dev, addr);
611 }
612 }
613 return -ENODEV;
614}
615
616static int tas_i2c_detach(struct i2c_client *client)
617{
618 struct tas *tas = container_of(client, struct tas, i2c);
619 int err;
620 u8 tmp = TAS_ACR_ANALOG_PDOWN;
621
622 if ((err = i2c_detach_client(client)))
623 return err;
624 aoa_codec_unregister(&tas->codec);
625 of_node_put(tas->codec.node);
626
627 /* power down codec chip */
628 tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
629
630 kfree(tas);
631 return 0;
632}
633
634static struct i2c_driver tas_driver = {
635 .driver = {
636 .name = "aoa_codec_tas",
637 .owner = THIS_MODULE,
638 },
639 .attach_adapter = tas_i2c_attach,
640 .detach_client = tas_i2c_detach,
641};
642
643static int __init tas_init(void)
644{
645 return i2c_add_driver(&tas_driver);
646}
647
648static void __exit tas_exit(void)
649{
650 i2c_del_driver(&tas_driver);
651}
652
653module_init(tas_init);
654module_exit(tas_exit);
diff --git a/sound/aoa/codecs/snd-aoa-codec-tas.h b/sound/aoa/codecs/snd-aoa-codec-tas.h
new file mode 100644
index 000000000000..daf81f45d83a
--- /dev/null
+++ b/sound/aoa/codecs/snd-aoa-codec-tas.h
@@ -0,0 +1,47 @@
1/*
2 * Apple Onboard Audio driver for tas codec (header)
3 *
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
5 *
6 * GPL v2, can be found in COPYING.
7 */
8#ifndef __SND_AOA_CODECTASH
9#define __SND_AOA_CODECTASH
10
11#define TAS_REG_MCS 0x01 /* main control */
12# define TAS_MCS_FASTLOAD (1<<7)
13# define TAS_MCS_SCLK64 (1<<6)
14# define TAS_MCS_SPORT_MODE_MASK (3<<4)
15# define TAS_MCS_SPORT_MODE_I2S (2<<4)
16# define TAS_MCS_SPORT_MODE_RJ (1<<4)
17# define TAS_MCS_SPORT_MODE_LJ (0<<4)
18# define TAS_MCS_SPORT_WL_MASK (3<<0)
19# define TAS_MCS_SPORT_WL_16BIT (0<<0)
20# define TAS_MCS_SPORT_WL_18BIT (1<<0)
21# define TAS_MCS_SPORT_WL_20BIT (2<<0)
22# define TAS_MCS_SPORT_WL_24BIT (3<<0)
23
24#define TAS_REG_DRC 0x02
25#define TAS_REG_VOL 0x04
26#define TAS_REG_TREBLE 0x05
27#define TAS_REG_BASS 0x06
28#define TAS_REG_LMIX 0x07
29#define TAS_REG_RMIX 0x08
30
31#define TAS_REG_ACR 0x40 /* analog control */
32# define TAS_ACR_B_MONAUREAL (1<<7)
33# define TAS_ACR_B_MON_SEL_RIGHT (1<<6)
34# define TAS_ACR_DEEMPH_MASK (3<<2)
35# define TAS_ACR_DEEMPH_OFF (0<<2)
36# define TAS_ACR_DEEMPH_48KHz (1<<2)
37# define TAS_ACR_DEEMPH_44KHz (2<<2)
38# define TAS_ACR_INPUT_B (1<<1)
39# define TAS_ACR_ANALOG_PDOWN (1<<0)
40
41#define TAS_REG_MCS2 0x43 /* main control 2 */
42# define TAS_MCS2_ALLPASS (1<<1)
43
44#define TAS_REG_LEFT_BIQUAD6 0x10
45#define TAS_REG_RIGHT_BIQUAD6 0x19
46
47#endif /* __SND_AOA_CODECTASH */
diff --git a/sound/aoa/codecs/snd-aoa-codec-toonie.c b/sound/aoa/codecs/snd-aoa-codec-toonie.c
new file mode 100644
index 000000000000..bcc555647e79
--- /dev/null
+++ b/sound/aoa/codecs/snd-aoa-codec-toonie.c
@@ -0,0 +1,141 @@
1/*
2 * Apple Onboard Audio driver for Toonie codec
3 *
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
5 *
6 * GPL v2, can be found in COPYING.
7 *
8 *
9 * This is a driver for the toonie codec chip. This chip is present
10 * on the Mac Mini and is nothing but a DAC.
11 */
12#include <linux/delay.h>
13#include <linux/module.h>
14MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
15MODULE_LICENSE("GPL");
16MODULE_DESCRIPTION("toonie codec driver for snd-aoa");
17
18#include "../aoa.h"
19#include "../soundbus/soundbus.h"
20
21
22#define PFX "snd-aoa-codec-toonie: "
23
24struct toonie {
25 struct aoa_codec codec;
26};
27#define codec_to_toonie(c) container_of(c, struct toonie, codec)
28
29static int toonie_dev_register(struct snd_device *dev)
30{
31 return 0;
32}
33
34static struct snd_device_ops ops = {
35 .dev_register = toonie_dev_register,
36};
37
38static struct transfer_info toonie_transfers[] = {
39 /* This thing *only* has analog output,
40 * the rates are taken from Info.plist
41 * from Darwin. */
42 {
43 .formats = SNDRV_PCM_FMTBIT_S16_BE |
44 SNDRV_PCM_FMTBIT_S24_BE,
45 .rates = SNDRV_PCM_RATE_32000 |
46 SNDRV_PCM_RATE_44100 |
47 SNDRV_PCM_RATE_48000 |
48 SNDRV_PCM_RATE_88200 |
49 SNDRV_PCM_RATE_96000,
50 },
51 {}
52};
53
54#ifdef CONFIG_PM
55static int toonie_suspend(struct codec_info_item *cii, pm_message_t state)
56{
57 /* can we turn it off somehow? */
58 return 0;
59}
60
61static int toonie_resume(struct codec_info_item *cii)
62{
63 return 0;
64}
65#endif /* CONFIG_PM */
66
67static struct codec_info toonie_codec_info = {
68 .transfers = toonie_transfers,
69 .sysclock_factor = 256,
70 .bus_factor = 64,
71 .owner = THIS_MODULE,
72#ifdef CONFIG_PM
73 .suspend = toonie_suspend,
74 .resume = toonie_resume,
75#endif
76};
77
78static int toonie_init_codec(struct aoa_codec *codec)
79{
80 struct toonie *toonie = codec_to_toonie(codec);
81
82 if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, toonie, &ops)) {
83 printk(KERN_ERR PFX "failed to create toonie snd device!\n");
84 return -ENODEV;
85 }
86
87 /* nothing connected? what a joke! */
88 if (toonie->codec.connected != 1)
89 return -ENOTCONN;
90
91 if (toonie->codec.soundbus_dev->attach_codec(toonie->codec.soundbus_dev,
92 aoa_get_card(),
93 &toonie_codec_info, toonie)) {
94 printk(KERN_ERR PFX "error creating toonie pcm\n");
95 return -ENODEV;
96 }
97
98 return 0;
99}
100
101static void toonie_exit_codec(struct aoa_codec *codec)
102{
103 struct toonie *toonie = codec_to_toonie(codec);
104
105 if (!toonie->codec.soundbus_dev) {
106 printk(KERN_ERR PFX "toonie_exit_codec called without soundbus_dev!\n");
107 return;
108 }
109 toonie->codec.soundbus_dev->detach_codec(toonie->codec.soundbus_dev, toonie);
110}
111
112static struct toonie *toonie;
113
114static int __init toonie_init(void)
115{
116 toonie = kzalloc(sizeof(struct toonie), GFP_KERNEL);
117
118 if (!toonie)
119 return -ENOMEM;
120
121 strlcpy(toonie->codec.name, "toonie", sizeof(toonie->codec.name));
122 toonie->codec.owner = THIS_MODULE;
123 toonie->codec.init = toonie_init_codec;
124 toonie->codec.exit = toonie_exit_codec;
125
126 if (aoa_codec_register(&toonie->codec)) {
127 kfree(toonie);
128 return -EINVAL;
129 }
130
131 return 0;
132}
133
134static void __exit toonie_exit(void)
135{
136 aoa_codec_unregister(&toonie->codec);
137 kfree(toonie);
138}
139
140module_init(toonie_init);
141module_exit(toonie_exit);