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"
79 #define PFX "snd-aoa-codec-tas: "
82 struct aoa_codec codec;
83 struct i2c_client i2c;
84 u32 muted_l:1, muted_r:1,
86 u8 cached_volume_l, cached_volume_r;
87 u8 mixer_l[3], mixer_r[3];
91 static struct tas *codec_to_tas(struct aoa_codec *codec)
93 return container_of(codec, struct tas, codec);
96 static inline int tas_write_reg(struct tas *tas, u8 reg, u8 len, u8 *data)
99 return i2c_smbus_write_byte_data(&tas->i2c, reg, *data);
101 return i2c_smbus_write_i2c_block_data(&tas->i2c, reg, len, data);
104 static void tas_set_volume(struct tas *tas)
110 left = tas->cached_volume_l;
111 right = tas->cached_volume_r;
113 if (left > 177) left = 177;
114 if (right > 177) right = 177;
116 if (tas->muted_l) left = 0;
117 if (tas->muted_r) right = 0;
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];
129 tmp = tas_gaintable[right];
133 tas_write_reg(tas, TAS_REG_VOL, 6, block);
136 static void tas_set_mixer(struct tas *tas)
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;
150 tas_write_reg(tas, TAS_REG_LMIX, 9, block);
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;
160 tas_write_reg(tas, TAS_REG_RMIX, 9, block);
165 static int tas_dev_register(struct snd_device *dev)
170 static struct snd_device_ops ops = {
171 .dev_register = tas_dev_register,
174 static int tas_snd_vol_info(struct snd_kcontrol *kcontrol,
175 struct snd_ctl_elem_info *uinfo)
177 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
179 uinfo->value.integer.min = 0;
180 uinfo->value.integer.max = 177;
184 static int tas_snd_vol_get(struct snd_kcontrol *kcontrol,
185 struct snd_ctl_elem_value *ucontrol)
187 struct tas *tas = snd_kcontrol_chip(kcontrol);
189 ucontrol->value.integer.value[0] = tas->cached_volume_l;
190 ucontrol->value.integer.value[1] = tas->cached_volume_r;
194 static int tas_snd_vol_put(struct snd_kcontrol *kcontrol,
195 struct snd_ctl_elem_value *ucontrol)
197 struct tas *tas = snd_kcontrol_chip(kcontrol);
199 if (tas->cached_volume_l == ucontrol->value.integer.value[0]
200 && tas->cached_volume_r == ucontrol->value.integer.value[1])
203 tas->cached_volume_l = ucontrol->value.integer.value[0];
204 tas->cached_volume_r = ucontrol->value.integer.value[1];
209 static 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,
218 static int tas_snd_mute_info(struct snd_kcontrol *kcontrol,
219 struct snd_ctl_elem_info *uinfo)
221 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
223 uinfo->value.integer.min = 0;
224 uinfo->value.integer.max = 1;
228 static int tas_snd_mute_get(struct snd_kcontrol *kcontrol,
229 struct snd_ctl_elem_value *ucontrol)
231 struct tas *tas = snd_kcontrol_chip(kcontrol);
233 ucontrol->value.integer.value[0] = !tas->muted_l;
234 ucontrol->value.integer.value[1] = !tas->muted_r;
238 static int tas_snd_mute_put(struct snd_kcontrol *kcontrol,
239 struct snd_ctl_elem_value *ucontrol)
241 struct tas *tas = snd_kcontrol_chip(kcontrol);
243 if (tas->muted_l == !ucontrol->value.integer.value[0]
244 && tas->muted_r == !ucontrol->value.integer.value[1])
247 tas->muted_l = !ucontrol->value.integer.value[0];
248 tas->muted_r = !ucontrol->value.integer.value[1];
253 static 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,
262 static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol,
263 struct snd_ctl_elem_info *uinfo)
265 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
267 uinfo->value.integer.min = 0;
268 uinfo->value.integer.max = 177;
272 static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol,
273 struct snd_ctl_elem_value *ucontrol)
275 struct tas *tas = snd_kcontrol_chip(kcontrol);
276 int idx = kcontrol->private_value;
278 ucontrol->value.integer.value[0] = tas->mixer_l[idx];
279 ucontrol->value.integer.value[1] = tas->mixer_r[idx];
284 static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol,
285 struct snd_ctl_elem_value *ucontrol)
287 struct tas *tas = snd_kcontrol_chip(kcontrol);
288 int idx = kcontrol->private_value;
290 if (tas->mixer_l[idx] == ucontrol->value.integer.value[0]
291 && tas->mixer_r[idx] == ucontrol->value.integer.value[1])
294 tas->mixer_l[idx] = ucontrol->value.integer.value[0];
295 tas->mixer_r[idx] = ucontrol->value.integer.value[1];
301 #define MIXER_CONTROL(n,descr,idx) \
302 static 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, \
312 MIXER_CONTROL(pcm1, "PCM1", 0);
313 MIXER_CONTROL(monitor, "Monitor", 2);
315 static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol,
316 struct snd_ctl_elem_info *uinfo)
318 static char *texts[] = { "Line-In", "Microphone" };
320 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
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]);
329 static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
330 struct snd_ctl_elem_value *ucontrol)
332 struct tas *tas = snd_kcontrol_chip(kcontrol);
334 ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
338 static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
339 struct snd_ctl_elem_value *ucontrol)
341 struct tas *tas = snd_kcontrol_chip(kcontrol);
342 int oldacr = tas->acr;
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)
349 tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
353 static 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'
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".....
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,
374 static struct transfer_info tas_transfers[] = {
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,
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,
392 static int tas_usable(struct codec_info_item *cii,
393 struct transfer_info *ti,
394 struct transfer_info *out)
399 static int tas_reset_init(struct tas *tas)
402 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
404 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1);
406 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
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))
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))
419 if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp))
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! */
428 static int tas_suspend(struct tas *tas)
430 tas->acr |= TAS_ACR_ANALOG_PDOWN;
431 tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
435 static int tas_resume(struct tas *tas)
445 static int _tas_suspend(struct codec_info_item *cii, pm_message_t state)
447 return tas_suspend(cii->codec_data);
450 static int _tas_resume(struct codec_info_item *cii)
452 return tas_resume(cii->codec_data);
456 static 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 */
464 .owner = THIS_MODULE,
465 .usable = tas_usable,
467 .suspend = _tas_suspend,
468 .resume = _tas_resume,
472 static int tas_init_codec(struct aoa_codec *codec)
474 struct tas *tas = codec_to_tas(codec);
477 if (!tas->codec.gpio || !tas->codec.gpio->methods) {
478 printk(KERN_ERR PFX "gpios not assigned!!\n");
482 if (tas_reset_init(tas)) {
483 printk(KERN_ERR PFX "tas failed to initialise\n");
487 if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev,
489 &tas_codec_info, tas)) {
490 printk(KERN_ERR PFX "error attaching tas to soundbus\n");
494 if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, tas, &ops)) {
495 printk(KERN_ERR PFX "failed to create tas snd device!\n");
498 err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas));
502 err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas));
506 err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas));
510 err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas));
514 err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas));
520 tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
521 snd_device_free(aoa_get_card(), tas);
525 static void tas_exit_codec(struct aoa_codec *codec)
527 struct tas *tas = codec_to_tas(codec);
529 if (!tas->codec.soundbus_dev)
531 tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
535 static struct i2c_driver tas_driver;
537 static int tas_create(struct i2c_adapter *adapter,
538 struct device_node *node,
543 tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
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);
553 if (i2c_attach_client(&tas->i2c)) {
554 printk(KERN_ERR PFX "failed to attach to i2c\n");
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);
564 if (aoa_codec_register(&tas->codec)) {
567 printk(KERN_DEBUG "snd-aoa-codec-tas: created and attached tas instance\n");
570 i2c_detach_client(&tas->i2c);
576 static int tas_i2c_attach(struct i2c_adapter *adapter)
578 struct device_node *busnode, *dev = NULL;
579 struct pmac_i2c_bus *bus;
581 bus = pmac_i2c_adapter_to_bus(adapter);
584 busnode = pmac_i2c_get_bus_node(bus);
586 while ((dev = of_get_next_child(busnode, dev)) != NULL) {
587 if (device_is_compatible(dev, "tas3004")) {
589 printk(KERN_DEBUG PFX "found tas3004\n");
590 addr = (u32 *) get_property(dev, "reg", NULL);
593 return tas_create(adapter, dev, ((*addr) >> 1) & 0x7f);
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) {
600 printk(KERN_DEBUG PFX "found 'deq' node\n");
601 _addr = (u32 *) get_property(dev, "i2c-address", NULL);
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)
610 return tas_create(adapter, dev, addr);
616 static int tas_i2c_detach(struct i2c_client *client)
618 struct tas *tas = container_of(client, struct tas, i2c);
620 u8 tmp = TAS_ACR_ANALOG_PDOWN;
622 if ((err = i2c_detach_client(client)))
624 aoa_codec_unregister(&tas->codec);
625 of_node_put(tas->codec.node);
627 /* power down codec chip */
628 tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
634 static struct i2c_driver tas_driver = {
636 .name = "aoa_codec_tas",
637 .owner = THIS_MODULE,
639 .attach_adapter = tas_i2c_attach,
640 .detach_client = tas_i2c_detach,
643 static int __init tas_init(void)
645 return i2c_add_driver(&tas_driver);
648 static void __exit tas_exit(void)
650 i2c_del_driver(&tas_driver);
653 module_init(tas_init);
654 module_exit(tas_exit);