2 * Apple Onboard Audio driver for tas codec
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6 * GPL v2, can be found in COPYING.
9 * - How to distinguish between 3004 and versions?
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
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...
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.
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
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.
63 #include <linux/i2c.h>
64 #include <linux/i2c-dev.h>
65 #include <asm/pmac_low_i2c.h>
67 #include <linux/delay.h>
68 #include <linux/module.h>
69 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
70 MODULE_LICENSE("GPL");
71 MODULE_DESCRIPTION("tas codec driver for snd-aoa");
73 #include "snd-aoa-codec-tas.h"
74 #include "snd-aoa-codec-tas-gain-table.h"
76 #include "../soundbus/soundbus.h"
78 #define PFX "snd-aoa-codec-tas: "
82 struct aoa_codec codec;
83 struct i2c_client i2c;
84 u32 mute_l:1, mute_r:1 ,
88 u8 cached_volume_l, cached_volume_r;
89 u8 mixer_l[3], mixer_r[3];
94 static int tas_reset_init(struct tas *tas);
96 static struct tas *codec_to_tas(struct aoa_codec *codec)
98 return container_of(codec, struct tas, codec);
101 static inline int tas_write_reg(struct tas *tas, u8 reg, u8 len, u8 *data)
104 return i2c_smbus_write_byte_data(&tas->i2c, reg, *data);
106 return i2c_smbus_write_i2c_block_data(&tas->i2c, reg, len, data);
109 static void tas3004_set_drc(struct tas *tas)
111 unsigned char val[6];
113 if (tas->drc_enabled)
114 val[0] = 0x50; /* 3:1 above threshold */
116 val[0] = 0x51; /* disabled */
117 val[1] = 0x02; /* 1:1 below threshold */
118 if (tas->drc_range > 0xef)
120 else if (tas->drc_range < 0)
123 val[2] = tas->drc_range;
128 tas_write_reg(tas, TAS_REG_DRC, 6, val);
131 static void tas_set_volume(struct tas *tas)
137 left = tas->cached_volume_l;
138 right = tas->cached_volume_r;
140 if (left > 177) left = 177;
141 if (right > 177) right = 177;
143 if (tas->mute_l) left = 0;
144 if (tas->mute_r) right = 0;
146 /* analysing the volume and mixer tables shows
147 * that they are similar enough when we shift
148 * the mixer table down by 4 bits. The error
149 * is miniscule, in just one item the error
150 * is 1, at a value of 0x07f17b (mixer table
151 * value is 0x07f17a) */
152 tmp = tas_gaintable[left];
156 tmp = tas_gaintable[right];
160 tas_write_reg(tas, TAS_REG_VOL, 6, block);
163 static void tas_set_mixer(struct tas *tas)
170 val = tas->mixer_l[i];
171 if (val > 177) val = 177;
172 tmp = tas_gaintable[val];
173 block[3*i+0] = tmp>>16;
174 block[3*i+1] = tmp>>8;
177 tas_write_reg(tas, TAS_REG_LMIX, 9, block);
180 val = tas->mixer_r[i];
181 if (val > 177) val = 177;
182 tmp = tas_gaintable[val];
183 block[3*i+0] = tmp>>16;
184 block[3*i+1] = tmp>>8;
187 tas_write_reg(tas, TAS_REG_RMIX, 9, block);
192 static int tas_dev_register(struct snd_device *dev)
197 static struct snd_device_ops ops = {
198 .dev_register = tas_dev_register,
201 static int tas_snd_vol_info(struct snd_kcontrol *kcontrol,
202 struct snd_ctl_elem_info *uinfo)
204 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
206 uinfo->value.integer.min = 0;
207 uinfo->value.integer.max = 177;
211 static int tas_snd_vol_get(struct snd_kcontrol *kcontrol,
212 struct snd_ctl_elem_value *ucontrol)
214 struct tas *tas = snd_kcontrol_chip(kcontrol);
216 ucontrol->value.integer.value[0] = tas->cached_volume_l;
217 ucontrol->value.integer.value[1] = tas->cached_volume_r;
221 static int tas_snd_vol_put(struct snd_kcontrol *kcontrol,
222 struct snd_ctl_elem_value *ucontrol)
224 struct tas *tas = snd_kcontrol_chip(kcontrol);
226 if (tas->cached_volume_l == ucontrol->value.integer.value[0]
227 && tas->cached_volume_r == ucontrol->value.integer.value[1])
230 tas->cached_volume_l = ucontrol->value.integer.value[0];
231 tas->cached_volume_r = ucontrol->value.integer.value[1];
237 static struct snd_kcontrol_new volume_control = {
238 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
239 .name = "Master Playback Volume",
240 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
241 .info = tas_snd_vol_info,
242 .get = tas_snd_vol_get,
243 .put = tas_snd_vol_put,
246 static int tas_snd_mute_info(struct snd_kcontrol *kcontrol,
247 struct snd_ctl_elem_info *uinfo)
249 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
251 uinfo->value.integer.min = 0;
252 uinfo->value.integer.max = 1;
256 static int tas_snd_mute_get(struct snd_kcontrol *kcontrol,
257 struct snd_ctl_elem_value *ucontrol)
259 struct tas *tas = snd_kcontrol_chip(kcontrol);
261 ucontrol->value.integer.value[0] = !tas->mute_l;
262 ucontrol->value.integer.value[1] = !tas->mute_r;
266 static int tas_snd_mute_put(struct snd_kcontrol *kcontrol,
267 struct snd_ctl_elem_value *ucontrol)
269 struct tas *tas = snd_kcontrol_chip(kcontrol);
271 if (tas->mute_l == !ucontrol->value.integer.value[0]
272 && tas->mute_r == !ucontrol->value.integer.value[1])
275 tas->mute_l = !ucontrol->value.integer.value[0];
276 tas->mute_r = !ucontrol->value.integer.value[1];
282 static struct snd_kcontrol_new mute_control = {
283 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
284 .name = "Master Playback Switch",
285 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
286 .info = tas_snd_mute_info,
287 .get = tas_snd_mute_get,
288 .put = tas_snd_mute_put,
291 static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol,
292 struct snd_ctl_elem_info *uinfo)
294 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
296 uinfo->value.integer.min = 0;
297 uinfo->value.integer.max = 177;
301 static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol,
302 struct snd_ctl_elem_value *ucontrol)
304 struct tas *tas = snd_kcontrol_chip(kcontrol);
305 int idx = kcontrol->private_value;
307 ucontrol->value.integer.value[0] = tas->mixer_l[idx];
308 ucontrol->value.integer.value[1] = tas->mixer_r[idx];
313 static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol,
314 struct snd_ctl_elem_value *ucontrol)
316 struct tas *tas = snd_kcontrol_chip(kcontrol);
317 int idx = kcontrol->private_value;
319 if (tas->mixer_l[idx] == ucontrol->value.integer.value[0]
320 && tas->mixer_r[idx] == ucontrol->value.integer.value[1])
323 tas->mixer_l[idx] = ucontrol->value.integer.value[0];
324 tas->mixer_r[idx] = ucontrol->value.integer.value[1];
331 #define MIXER_CONTROL(n,descr,idx) \
332 static struct snd_kcontrol_new n##_control = { \
333 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
334 .name = descr " Playback Volume", \
335 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
336 .info = tas_snd_mixer_info, \
337 .get = tas_snd_mixer_get, \
338 .put = tas_snd_mixer_put, \
339 .private_value = idx, \
342 MIXER_CONTROL(pcm1, "PCM", 0);
343 MIXER_CONTROL(monitor, "Monitor", 2);
345 static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol,
346 struct snd_ctl_elem_info *uinfo)
348 static char *texts[] = { "Line-In", "Microphone" };
350 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
352 uinfo->value.enumerated.items = 2;
353 if (uinfo->value.enumerated.item > 1)
354 uinfo->value.enumerated.item = 1;
355 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
359 static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
360 struct snd_ctl_elem_value *ucontrol)
362 struct tas *tas = snd_kcontrol_chip(kcontrol);
364 ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
368 static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
369 struct snd_ctl_elem_value *ucontrol)
371 struct tas *tas = snd_kcontrol_chip(kcontrol);
372 int oldacr = tas->acr;
374 tas->acr &= ~TAS_ACR_INPUT_B;
375 if (ucontrol->value.enumerated.item[0])
376 tas->acr |= TAS_ACR_INPUT_B;
377 if (oldacr == tas->acr)
380 tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
384 static struct snd_kcontrol_new capture_source_control = {
385 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
386 /* If we name this 'Input Source', it properly shows up in
387 * alsamixer as a selection, * but it's shown under the
388 * 'Playback' category.
389 * If I name it 'Capture Source', it shows up in strange
390 * ways (two bools of which one can be selected at a
391 * time) but at least it's shown in the 'Capture'
393 * I was told that this was due to backward compatibility,
394 * but I don't understand then why the mangling is *not*
395 * done when I name it "Input Source".....
397 .name = "Capture Source",
398 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
399 .info = tas_snd_capture_source_info,
400 .get = tas_snd_capture_source_get,
401 .put = tas_snd_capture_source_put,
405 static struct transfer_info tas_transfers[] = {
408 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
409 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE,
410 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
415 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
416 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE,
417 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
423 static int tas_usable(struct codec_info_item *cii,
424 struct transfer_info *ti,
425 struct transfer_info *out)
430 static int tas_reset_init(struct tas *tas)
434 tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
436 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
438 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1);
440 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
442 tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
444 tmp = TAS_MCS_SCLK64 | TAS_MCS_SPORT_MODE_I2S | TAS_MCS_SPORT_WL_24BIT;
445 if (tas_write_reg(tas, TAS_REG_MCS, 1, &tmp))
448 tas->acr |= TAS_ACR_ANALOG_PDOWN | TAS_ACR_B_MONAUREAL |
449 TAS_ACR_B_MON_SEL_RIGHT;
450 if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
454 if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp))
457 tas3004_set_drc(tas);
459 /* Set treble & bass to 0dB */
461 tas_write_reg(tas, TAS_REG_TREBLE, 1, &tmp);
462 tas_write_reg(tas, TAS_REG_BASS, 1, &tmp);
464 tas->acr &= ~TAS_ACR_ANALOG_PDOWN;
465 if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
471 static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock)
473 struct tas *tas = cii->codec_data;
476 case CLOCK_SWITCH_PREPARE_SLAVE:
477 /* Clocks are going away, mute mute mute */
478 tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
481 case CLOCK_SWITCH_SLAVE:
482 /* Clocks are back, re-init the codec */
487 tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
490 /* doesn't happen as of now */
496 /* we are controlled via i2c and assume that is always up
497 * If that wasn't the case, we'd have to suspend once
498 * our i2c device is suspended, and then take note of that! */
499 static int tas_suspend(struct tas *tas)
502 tas->acr |= TAS_ACR_ANALOG_PDOWN;
503 tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
507 static int tas_resume(struct tas *tas)
518 static int _tas_suspend(struct codec_info_item *cii, pm_message_t state)
520 return tas_suspend(cii->codec_data);
523 static int _tas_resume(struct codec_info_item *cii)
525 return tas_resume(cii->codec_data);
529 static struct codec_info tas_codec_info = {
530 .transfers = tas_transfers,
531 /* in theory, we can drive it at 512 too...
532 * but so far the framework doesn't allow
533 * for that and I don't see much point in it. */
534 .sysclock_factor = 256,
535 /* same here, could be 32 for just one 16 bit format */
537 .owner = THIS_MODULE,
538 .usable = tas_usable,
539 .switch_clock = tas_switch_clock,
541 .suspend = _tas_suspend,
542 .resume = _tas_resume,
546 static int tas_init_codec(struct aoa_codec *codec)
548 struct tas *tas = codec_to_tas(codec);
551 if (!tas->codec.gpio || !tas->codec.gpio->methods) {
552 printk(KERN_ERR PFX "gpios not assigned!!\n");
556 if (tas_reset_init(tas)) {
557 printk(KERN_ERR PFX "tas failed to initialise\n");
562 if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev,
564 &tas_codec_info, tas)) {
565 printk(KERN_ERR PFX "error attaching tas to soundbus\n");
569 if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, tas, &ops)) {
570 printk(KERN_ERR PFX "failed to create tas snd device!\n");
573 err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas));
577 err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas));
581 err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas));
585 err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas));
589 err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas));
595 tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
596 snd_device_free(aoa_get_card(), tas);
600 static void tas_exit_codec(struct aoa_codec *codec)
602 struct tas *tas = codec_to_tas(codec);
604 if (!tas->codec.soundbus_dev)
606 tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
610 static struct i2c_driver tas_driver;
612 static int tas_create(struct i2c_adapter *adapter,
613 struct device_node *node,
618 tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
623 tas->i2c.driver = &tas_driver;
624 tas->i2c.adapter = adapter;
625 tas->i2c.addr = addr;
626 tas->drc_range = TAS3004_DRC_MAX;
627 strlcpy(tas->i2c.name, "tas audio codec", I2C_NAME_SIZE-1);
629 if (i2c_attach_client(&tas->i2c)) {
630 printk(KERN_ERR PFX "failed to attach to i2c\n");
634 strlcpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN-1);
635 tas->codec.owner = THIS_MODULE;
636 tas->codec.init = tas_init_codec;
637 tas->codec.exit = tas_exit_codec;
638 tas->codec.node = of_node_get(node);
640 if (aoa_codec_register(&tas->codec)) {
644 "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n",
645 addr, node->full_name);
648 i2c_detach_client(&tas->i2c);
654 static int tas_i2c_attach(struct i2c_adapter *adapter)
656 struct device_node *busnode, *dev = NULL;
657 struct pmac_i2c_bus *bus;
659 bus = pmac_i2c_adapter_to_bus(adapter);
662 busnode = pmac_i2c_get_bus_node(bus);
664 while ((dev = of_get_next_child(busnode, dev)) != NULL) {
665 if (device_is_compatible(dev, "tas3004")) {
667 printk(KERN_DEBUG PFX "found tas3004\n");
668 addr = (u32 *) get_property(dev, "reg", NULL);
671 return tas_create(adapter, dev, ((*addr) >> 1) & 0x7f);
673 /* older machines have no 'codec' node with a 'compatible'
674 * property that says 'tas3004', they just have a 'deq'
675 * node without any such property... */
676 if (strcmp(dev->name, "deq") == 0) {
678 printk(KERN_DEBUG PFX "found 'deq' node\n");
679 _addr = (u32 *) get_property(dev, "i2c-address", NULL);
682 addr = ((*_addr) >> 1) & 0x7f;
683 /* now, if the address doesn't match any of the two
684 * that a tas3004 can have, we cannot handle this.
685 * I doubt it ever happens but hey. */
686 if (addr != 0x34 && addr != 0x35)
688 return tas_create(adapter, dev, addr);
694 static int tas_i2c_detach(struct i2c_client *client)
696 struct tas *tas = container_of(client, struct tas, i2c);
698 u8 tmp = TAS_ACR_ANALOG_PDOWN;
700 if ((err = i2c_detach_client(client)))
702 aoa_codec_unregister(&tas->codec);
703 of_node_put(tas->codec.node);
705 /* power down codec chip */
706 tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
712 static struct i2c_driver tas_driver = {
714 .name = "aoa_codec_tas",
715 .owner = THIS_MODULE,
717 .attach_adapter = tas_i2c_attach,
718 .detach_client = tas_i2c_detach,
721 static int __init tas_init(void)
723 return i2c_add_driver(&tas_driver);
726 static void __exit tas_exit(void)
728 i2c_del_driver(&tas_driver);
731 module_init(tas_init);
732 module_exit(tas_exit);