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_drc_range_info(struct snd_kcontrol *kcontrol,
346 struct snd_ctl_elem_info *uinfo)
348 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
350 uinfo->value.integer.min = 0;
351 uinfo->value.integer.max = TAS3004_DRC_MAX;
355 static int tas_snd_drc_range_get(struct snd_kcontrol *kcontrol,
356 struct snd_ctl_elem_value *ucontrol)
358 struct tas *tas = snd_kcontrol_chip(kcontrol);
360 ucontrol->value.integer.value[0] = tas->drc_range;
364 static int tas_snd_drc_range_put(struct snd_kcontrol *kcontrol,
365 struct snd_ctl_elem_value *ucontrol)
367 struct tas *tas = snd_kcontrol_chip(kcontrol);
369 if (tas->drc_range == ucontrol->value.integer.value[0])
372 tas->drc_range = ucontrol->value.integer.value[0];
374 tas3004_set_drc(tas);
378 static struct snd_kcontrol_new drc_range_control = {
379 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
381 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
382 .info = tas_snd_drc_range_info,
383 .get = tas_snd_drc_range_get,
384 .put = tas_snd_drc_range_put,
387 static int tas_snd_drc_switch_info(struct snd_kcontrol *kcontrol,
388 struct snd_ctl_elem_info *uinfo)
390 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
392 uinfo->value.integer.min = 0;
393 uinfo->value.integer.max = 1;
397 static int tas_snd_drc_switch_get(struct snd_kcontrol *kcontrol,
398 struct snd_ctl_elem_value *ucontrol)
400 struct tas *tas = snd_kcontrol_chip(kcontrol);
402 ucontrol->value.integer.value[0] = tas->drc_enabled;
406 static int tas_snd_drc_switch_put(struct snd_kcontrol *kcontrol,
407 struct snd_ctl_elem_value *ucontrol)
409 struct tas *tas = snd_kcontrol_chip(kcontrol);
411 if (tas->drc_enabled == ucontrol->value.integer.value[0])
414 tas->drc_enabled = ucontrol->value.integer.value[0];
416 tas3004_set_drc(tas);
420 static struct snd_kcontrol_new drc_switch_control = {
421 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
422 .name = "DRC Range Switch",
423 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
424 .info = tas_snd_drc_switch_info,
425 .get = tas_snd_drc_switch_get,
426 .put = tas_snd_drc_switch_put,
429 static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol,
430 struct snd_ctl_elem_info *uinfo)
432 static char *texts[] = { "Line-In", "Microphone" };
434 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
436 uinfo->value.enumerated.items = 2;
437 if (uinfo->value.enumerated.item > 1)
438 uinfo->value.enumerated.item = 1;
439 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
443 static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
444 struct snd_ctl_elem_value *ucontrol)
446 struct tas *tas = snd_kcontrol_chip(kcontrol);
448 ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
452 static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
453 struct snd_ctl_elem_value *ucontrol)
455 struct tas *tas = snd_kcontrol_chip(kcontrol);
456 int oldacr = tas->acr;
458 tas->acr &= ~TAS_ACR_INPUT_B;
459 if (ucontrol->value.enumerated.item[0])
460 tas->acr |= TAS_ACR_INPUT_B;
461 if (oldacr == tas->acr)
464 tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
468 static struct snd_kcontrol_new capture_source_control = {
469 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
470 /* If we name this 'Input Source', it properly shows up in
471 * alsamixer as a selection, * but it's shown under the
472 * 'Playback' category.
473 * If I name it 'Capture Source', it shows up in strange
474 * ways (two bools of which one can be selected at a
475 * time) but at least it's shown in the 'Capture'
477 * I was told that this was due to backward compatibility,
478 * but I don't understand then why the mangling is *not*
479 * done when I name it "Input Source".....
481 .name = "Capture Source",
482 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
483 .info = tas_snd_capture_source_info,
484 .get = tas_snd_capture_source_get,
485 .put = tas_snd_capture_source_put,
489 static struct transfer_info tas_transfers[] = {
492 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
493 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE,
494 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
499 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
500 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE,
501 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
507 static int tas_usable(struct codec_info_item *cii,
508 struct transfer_info *ti,
509 struct transfer_info *out)
514 static int tas_reset_init(struct tas *tas)
518 tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
520 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
522 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1);
524 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
526 tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
528 tmp = TAS_MCS_SCLK64 | TAS_MCS_SPORT_MODE_I2S | TAS_MCS_SPORT_WL_24BIT;
529 if (tas_write_reg(tas, TAS_REG_MCS, 1, &tmp))
532 tas->acr |= TAS_ACR_ANALOG_PDOWN | TAS_ACR_B_MONAUREAL |
533 TAS_ACR_B_MON_SEL_RIGHT;
534 if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
538 if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp))
541 tas3004_set_drc(tas);
543 /* Set treble & bass to 0dB */
545 tas_write_reg(tas, TAS_REG_TREBLE, 1, &tmp);
546 tas_write_reg(tas, TAS_REG_BASS, 1, &tmp);
548 tas->acr &= ~TAS_ACR_ANALOG_PDOWN;
549 if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
555 static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock)
557 struct tas *tas = cii->codec_data;
560 case CLOCK_SWITCH_PREPARE_SLAVE:
561 /* Clocks are going away, mute mute mute */
562 tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
565 case CLOCK_SWITCH_SLAVE:
566 /* Clocks are back, re-init the codec */
571 tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
574 /* doesn't happen as of now */
580 /* we are controlled via i2c and assume that is always up
581 * If that wasn't the case, we'd have to suspend once
582 * our i2c device is suspended, and then take note of that! */
583 static int tas_suspend(struct tas *tas)
586 tas->acr |= TAS_ACR_ANALOG_PDOWN;
587 tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
591 static int tas_resume(struct tas *tas)
602 static int _tas_suspend(struct codec_info_item *cii, pm_message_t state)
604 return tas_suspend(cii->codec_data);
607 static int _tas_resume(struct codec_info_item *cii)
609 return tas_resume(cii->codec_data);
613 static struct codec_info tas_codec_info = {
614 .transfers = tas_transfers,
615 /* in theory, we can drive it at 512 too...
616 * but so far the framework doesn't allow
617 * for that and I don't see much point in it. */
618 .sysclock_factor = 256,
619 /* same here, could be 32 for just one 16 bit format */
621 .owner = THIS_MODULE,
622 .usable = tas_usable,
623 .switch_clock = tas_switch_clock,
625 .suspend = _tas_suspend,
626 .resume = _tas_resume,
630 static int tas_init_codec(struct aoa_codec *codec)
632 struct tas *tas = codec_to_tas(codec);
635 if (!tas->codec.gpio || !tas->codec.gpio->methods) {
636 printk(KERN_ERR PFX "gpios not assigned!!\n");
640 if (tas_reset_init(tas)) {
641 printk(KERN_ERR PFX "tas failed to initialise\n");
646 if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev,
648 &tas_codec_info, tas)) {
649 printk(KERN_ERR PFX "error attaching tas to soundbus\n");
653 if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, tas, &ops)) {
654 printk(KERN_ERR PFX "failed to create tas snd device!\n");
657 err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas));
661 err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas));
665 err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas));
669 err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas));
673 err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas));
677 err = aoa_snd_ctl_add(snd_ctl_new1(&drc_range_control, tas));
681 err = aoa_snd_ctl_add(snd_ctl_new1(&drc_switch_control, tas));
687 tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
688 snd_device_free(aoa_get_card(), tas);
692 static void tas_exit_codec(struct aoa_codec *codec)
694 struct tas *tas = codec_to_tas(codec);
696 if (!tas->codec.soundbus_dev)
698 tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
702 static struct i2c_driver tas_driver;
704 static int tas_create(struct i2c_adapter *adapter,
705 struct device_node *node,
710 tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
715 tas->i2c.driver = &tas_driver;
716 tas->i2c.adapter = adapter;
717 tas->i2c.addr = addr;
718 /* seems that half is a saner default */
719 tas->drc_range = TAS3004_DRC_MAX / 2;
720 strlcpy(tas->i2c.name, "tas audio codec", I2C_NAME_SIZE-1);
722 if (i2c_attach_client(&tas->i2c)) {
723 printk(KERN_ERR PFX "failed to attach to i2c\n");
727 strlcpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN-1);
728 tas->codec.owner = THIS_MODULE;
729 tas->codec.init = tas_init_codec;
730 tas->codec.exit = tas_exit_codec;
731 tas->codec.node = of_node_get(node);
733 if (aoa_codec_register(&tas->codec)) {
737 "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n",
738 addr, node->full_name);
741 i2c_detach_client(&tas->i2c);
747 static int tas_i2c_attach(struct i2c_adapter *adapter)
749 struct device_node *busnode, *dev = NULL;
750 struct pmac_i2c_bus *bus;
752 bus = pmac_i2c_adapter_to_bus(adapter);
755 busnode = pmac_i2c_get_bus_node(bus);
757 while ((dev = of_get_next_child(busnode, dev)) != NULL) {
758 if (device_is_compatible(dev, "tas3004")) {
760 printk(KERN_DEBUG PFX "found tas3004\n");
761 addr = (u32 *) get_property(dev, "reg", NULL);
764 return tas_create(adapter, dev, ((*addr) >> 1) & 0x7f);
766 /* older machines have no 'codec' node with a 'compatible'
767 * property that says 'tas3004', they just have a 'deq'
768 * node without any such property... */
769 if (strcmp(dev->name, "deq") == 0) {
771 printk(KERN_DEBUG PFX "found 'deq' node\n");
772 _addr = (u32 *) get_property(dev, "i2c-address", NULL);
775 addr = ((*_addr) >> 1) & 0x7f;
776 /* now, if the address doesn't match any of the two
777 * that a tas3004 can have, we cannot handle this.
778 * I doubt it ever happens but hey. */
779 if (addr != 0x34 && addr != 0x35)
781 return tas_create(adapter, dev, addr);
787 static int tas_i2c_detach(struct i2c_client *client)
789 struct tas *tas = container_of(client, struct tas, i2c);
791 u8 tmp = TAS_ACR_ANALOG_PDOWN;
793 if ((err = i2c_detach_client(client)))
795 aoa_codec_unregister(&tas->codec);
796 of_node_put(tas->codec.node);
798 /* power down codec chip */
799 tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
805 static struct i2c_driver tas_driver = {
807 .name = "aoa_codec_tas",
808 .owner = THIS_MODULE,
810 .attach_adapter = tas_i2c_attach,
811 .detach_client = tas_i2c_detach,
814 static int __init tas_init(void)
816 return i2c_add_driver(&tas_driver);
819 static void __exit tas_exit(void)
821 i2c_del_driver(&tas_driver);
824 module_init(tas_init);
825 module_exit(tas_exit);