blob: 2e39ff6ee349b0888196d1e1a7521e2b7c6acfaa [file] [log] [blame]
Johannes Bergf3d94782006-06-21 15:42:43 +02001/*
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);