Merge branch 'merge'
[pandora-kernel.git] / sound / aoa / codecs / snd-aoa-codec-tas.c
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>
69 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
70 MODULE_LICENSE("GPL");
71 MODULE_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 "snd-aoa-codec-tas-basstreble.h"
76 #include "../aoa.h"
77 #include "../soundbus/soundbus.h"
78
79 #define PFX "snd-aoa-codec-tas: "
80
81
82 struct tas {
83         struct aoa_codec        codec;
84         struct i2c_client       i2c;
85         u32                     mute_l:1, mute_r:1 ,
86                                 controls_created:1 ,
87                                 drc_enabled:1,
88                                 hw_enabled:1;
89         u8                      cached_volume_l, cached_volume_r;
90         u8                      mixer_l[3], mixer_r[3];
91         u8                      bass, treble;
92         u8                      acr;
93         int                     drc_range;
94 };
95
96 static int tas_reset_init(struct tas *tas);
97
98 static struct tas *codec_to_tas(struct aoa_codec *codec)
99 {
100         return container_of(codec, struct tas, codec);
101 }
102
103 static inline int tas_write_reg(struct tas *tas, u8 reg, u8 len, u8 *data)
104 {
105         if (len == 1)
106                 return i2c_smbus_write_byte_data(&tas->i2c, reg, *data);
107         else
108                 return i2c_smbus_write_i2c_block_data(&tas->i2c, reg, len, data);
109 }
110
111 static void tas3004_set_drc(struct tas *tas)
112 {
113         unsigned char val[6];
114
115         if (tas->drc_enabled)
116                 val[0] = 0x50; /* 3:1 above threshold */
117         else
118                 val[0] = 0x51; /* disabled */
119         val[1] = 0x02; /* 1:1 below threshold */
120         if (tas->drc_range > 0xef)
121                 val[2] = 0xef;
122         else if (tas->drc_range < 0)
123                 val[2] = 0x00;
124         else
125                 val[2] = tas->drc_range;
126         val[3] = 0xb0;
127         val[4] = 0x60;
128         val[5] = 0xa0;
129
130         tas_write_reg(tas, TAS_REG_DRC, 6, val);
131 }
132
133 static void tas_set_treble(struct tas *tas)
134 {
135         u8 tmp;
136
137         tmp = tas3004_treble(tas->treble);
138         tas_write_reg(tas, TAS_REG_TREBLE, 1, &tmp);
139 }
140
141 static void tas_set_bass(struct tas *tas)
142 {
143         u8 tmp;
144
145         tmp = tas3004_bass(tas->bass);
146         tas_write_reg(tas, TAS_REG_BASS, 1, &tmp);
147 }
148
149 static void tas_set_volume(struct tas *tas)
150 {
151         u8 block[6];
152         int tmp;
153         u8 left, right;
154
155         left = tas->cached_volume_l;
156         right = tas->cached_volume_r;
157
158         if (left > 177) left = 177;
159         if (right > 177) right = 177;
160
161         if (tas->mute_l) left = 0;
162         if (tas->mute_r) right = 0;
163
164         /* analysing the volume and mixer tables shows
165          * that they are similar enough when we shift
166          * the mixer table down by 4 bits. The error
167          * is miniscule, in just one item the error
168          * is 1, at a value of 0x07f17b (mixer table
169          * value is 0x07f17a) */
170         tmp = tas_gaintable[left];
171         block[0] = tmp>>20;
172         block[1] = tmp>>12;
173         block[2] = tmp>>4;
174         tmp = tas_gaintable[right];
175         block[3] = tmp>>20;
176         block[4] = tmp>>12;
177         block[5] = tmp>>4;
178         tas_write_reg(tas, TAS_REG_VOL, 6, block);
179 }
180
181 static void tas_set_mixer(struct tas *tas)
182 {
183         u8 block[9];
184         int tmp, i;
185         u8 val;
186
187         for (i=0;i<3;i++) {
188                 val = tas->mixer_l[i];
189                 if (val > 177) val = 177;
190                 tmp = tas_gaintable[val];
191                 block[3*i+0] = tmp>>16;
192                 block[3*i+1] = tmp>>8;
193                 block[3*i+2] = tmp;
194         }
195         tas_write_reg(tas, TAS_REG_LMIX, 9, block);
196
197         for (i=0;i<3;i++) {
198                 val = tas->mixer_r[i];
199                 if (val > 177) val = 177;
200                 tmp = tas_gaintable[val];
201                 block[3*i+0] = tmp>>16;
202                 block[3*i+1] = tmp>>8;
203                 block[3*i+2] = tmp;
204         }
205         tas_write_reg(tas, TAS_REG_RMIX, 9, block);
206 }
207
208 /* alsa stuff */
209
210 static int tas_dev_register(struct snd_device *dev)
211 {
212         return 0;
213 }
214
215 static struct snd_device_ops ops = {
216         .dev_register = tas_dev_register,
217 };
218
219 static int tas_snd_vol_info(struct snd_kcontrol *kcontrol,
220         struct snd_ctl_elem_info *uinfo)
221 {
222         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
223         uinfo->count = 2;
224         uinfo->value.integer.min = 0;
225         uinfo->value.integer.max = 177;
226         return 0;
227 }
228
229 static int tas_snd_vol_get(struct snd_kcontrol *kcontrol,
230         struct snd_ctl_elem_value *ucontrol)
231 {
232         struct tas *tas = snd_kcontrol_chip(kcontrol);
233
234         ucontrol->value.integer.value[0] = tas->cached_volume_l;
235         ucontrol->value.integer.value[1] = tas->cached_volume_r;
236         return 0;
237 }
238
239 static int tas_snd_vol_put(struct snd_kcontrol *kcontrol,
240         struct snd_ctl_elem_value *ucontrol)
241 {
242         struct tas *tas = snd_kcontrol_chip(kcontrol);
243
244         if (tas->cached_volume_l == ucontrol->value.integer.value[0]
245          && tas->cached_volume_r == ucontrol->value.integer.value[1])
246                 return 0;
247
248         tas->cached_volume_l = ucontrol->value.integer.value[0];
249         tas->cached_volume_r = ucontrol->value.integer.value[1];
250         if (tas->hw_enabled)
251                 tas_set_volume(tas);
252         return 1;
253 }
254
255 static struct snd_kcontrol_new volume_control = {
256         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
257         .name = "Master Playback Volume",
258         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
259         .info = tas_snd_vol_info,
260         .get = tas_snd_vol_get,
261         .put = tas_snd_vol_put,
262 };
263
264 static int tas_snd_mute_info(struct snd_kcontrol *kcontrol,
265         struct snd_ctl_elem_info *uinfo)
266 {
267         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
268         uinfo->count = 2;
269         uinfo->value.integer.min = 0;
270         uinfo->value.integer.max = 1;
271         return 0;
272 }
273
274 static int tas_snd_mute_get(struct snd_kcontrol *kcontrol,
275         struct snd_ctl_elem_value *ucontrol)
276 {
277         struct tas *tas = snd_kcontrol_chip(kcontrol);
278
279         ucontrol->value.integer.value[0] = !tas->mute_l;
280         ucontrol->value.integer.value[1] = !tas->mute_r;
281         return 0;
282 }
283
284 static int tas_snd_mute_put(struct snd_kcontrol *kcontrol,
285         struct snd_ctl_elem_value *ucontrol)
286 {
287         struct tas *tas = snd_kcontrol_chip(kcontrol);
288
289         if (tas->mute_l == !ucontrol->value.integer.value[0]
290          && tas->mute_r == !ucontrol->value.integer.value[1])
291                 return 0;
292
293         tas->mute_l = !ucontrol->value.integer.value[0];
294         tas->mute_r = !ucontrol->value.integer.value[1];
295         if (tas->hw_enabled)
296                 tas_set_volume(tas);
297         return 1;
298 }
299
300 static struct snd_kcontrol_new mute_control = {
301         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
302         .name = "Master Playback Switch",
303         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
304         .info = tas_snd_mute_info,
305         .get = tas_snd_mute_get,
306         .put = tas_snd_mute_put,
307 };
308
309 static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol,
310         struct snd_ctl_elem_info *uinfo)
311 {
312         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
313         uinfo->count = 2;
314         uinfo->value.integer.min = 0;
315         uinfo->value.integer.max = 177;
316         return 0;
317 }
318
319 static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol,
320         struct snd_ctl_elem_value *ucontrol)
321 {
322         struct tas *tas = snd_kcontrol_chip(kcontrol);
323         int idx = kcontrol->private_value;
324
325         ucontrol->value.integer.value[0] = tas->mixer_l[idx];
326         ucontrol->value.integer.value[1] = tas->mixer_r[idx];
327
328         return 0;
329 }
330
331 static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol,
332         struct snd_ctl_elem_value *ucontrol)
333 {
334         struct tas *tas = snd_kcontrol_chip(kcontrol);
335         int idx = kcontrol->private_value;
336
337         if (tas->mixer_l[idx] == ucontrol->value.integer.value[0]
338          && tas->mixer_r[idx] == ucontrol->value.integer.value[1])
339                 return 0;
340
341         tas->mixer_l[idx] = ucontrol->value.integer.value[0];
342         tas->mixer_r[idx] = ucontrol->value.integer.value[1];
343
344         if (tas->hw_enabled)
345                 tas_set_mixer(tas);
346         return 1;
347 }
348
349 #define MIXER_CONTROL(n,descr,idx)                      \
350 static struct snd_kcontrol_new n##_control = {          \
351         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,            \
352         .name = descr " Playback Volume",               \
353         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,      \
354         .info = tas_snd_mixer_info,                     \
355         .get = tas_snd_mixer_get,                       \
356         .put = tas_snd_mixer_put,                       \
357         .private_value = idx,                           \
358 }
359
360 MIXER_CONTROL(pcm1, "PCM", 0);
361 MIXER_CONTROL(monitor, "Monitor", 2);
362
363 static int tas_snd_drc_range_info(struct snd_kcontrol *kcontrol,
364         struct snd_ctl_elem_info *uinfo)
365 {
366         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
367         uinfo->count = 1;
368         uinfo->value.integer.min = 0;
369         uinfo->value.integer.max = TAS3004_DRC_MAX;
370         return 0;
371 }
372
373 static int tas_snd_drc_range_get(struct snd_kcontrol *kcontrol,
374         struct snd_ctl_elem_value *ucontrol)
375 {
376         struct tas *tas = snd_kcontrol_chip(kcontrol);
377
378         ucontrol->value.integer.value[0] = tas->drc_range;
379         return 0;
380 }
381
382 static int tas_snd_drc_range_put(struct snd_kcontrol *kcontrol,
383         struct snd_ctl_elem_value *ucontrol)
384 {
385         struct tas *tas = snd_kcontrol_chip(kcontrol);
386
387         if (tas->drc_range == ucontrol->value.integer.value[0])
388                 return 0;
389
390         tas->drc_range = ucontrol->value.integer.value[0];
391         if (tas->hw_enabled)
392                 tas3004_set_drc(tas);
393         return 1;
394 }
395
396 static struct snd_kcontrol_new drc_range_control = {
397         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
398         .name = "DRC Range",
399         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
400         .info = tas_snd_drc_range_info,
401         .get = tas_snd_drc_range_get,
402         .put = tas_snd_drc_range_put,
403 };
404
405 static int tas_snd_drc_switch_info(struct snd_kcontrol *kcontrol,
406         struct snd_ctl_elem_info *uinfo)
407 {
408         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
409         uinfo->count = 1;
410         uinfo->value.integer.min = 0;
411         uinfo->value.integer.max = 1;
412         return 0;
413 }
414
415 static int tas_snd_drc_switch_get(struct snd_kcontrol *kcontrol,
416         struct snd_ctl_elem_value *ucontrol)
417 {
418         struct tas *tas = snd_kcontrol_chip(kcontrol);
419
420         ucontrol->value.integer.value[0] = tas->drc_enabled;
421         return 0;
422 }
423
424 static int tas_snd_drc_switch_put(struct snd_kcontrol *kcontrol,
425         struct snd_ctl_elem_value *ucontrol)
426 {
427         struct tas *tas = snd_kcontrol_chip(kcontrol);
428
429         if (tas->drc_enabled == ucontrol->value.integer.value[0])
430                 return 0;
431
432         tas->drc_enabled = ucontrol->value.integer.value[0];
433         if (tas->hw_enabled)
434                 tas3004_set_drc(tas);
435         return 1;
436 }
437
438 static struct snd_kcontrol_new drc_switch_control = {
439         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
440         .name = "DRC Range Switch",
441         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
442         .info = tas_snd_drc_switch_info,
443         .get = tas_snd_drc_switch_get,
444         .put = tas_snd_drc_switch_put,
445 };
446
447 static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol,
448         struct snd_ctl_elem_info *uinfo)
449 {
450         static char *texts[] = { "Line-In", "Microphone" };
451
452         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
453         uinfo->count = 1;
454         uinfo->value.enumerated.items = 2;
455         if (uinfo->value.enumerated.item > 1)
456                 uinfo->value.enumerated.item = 1;
457         strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
458         return 0;
459 }
460
461 static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
462         struct snd_ctl_elem_value *ucontrol)
463 {
464         struct tas *tas = snd_kcontrol_chip(kcontrol);
465
466         ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
467         return 0;
468 }
469
470 static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
471         struct snd_ctl_elem_value *ucontrol)
472 {
473         struct tas *tas = snd_kcontrol_chip(kcontrol);
474         int oldacr = tas->acr;
475
476         tas->acr &= ~TAS_ACR_INPUT_B;
477         if (ucontrol->value.enumerated.item[0])
478                 tas->acr |= TAS_ACR_INPUT_B;
479         if (oldacr == tas->acr)
480                 return 0;
481         if (tas->hw_enabled)
482                 tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
483         return 1;
484 }
485
486 static struct snd_kcontrol_new capture_source_control = {
487         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
488         /* If we name this 'Input Source', it properly shows up in
489          * alsamixer as a selection, * but it's shown under the
490          * 'Playback' category.
491          * If I name it 'Capture Source', it shows up in strange
492          * ways (two bools of which one can be selected at a
493          * time) but at least it's shown in the 'Capture'
494          * category.
495          * I was told that this was due to backward compatibility,
496          * but I don't understand then why the mangling is *not*
497          * done when I name it "Input Source".....
498          */
499         .name = "Capture Source",
500         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
501         .info = tas_snd_capture_source_info,
502         .get = tas_snd_capture_source_get,
503         .put = tas_snd_capture_source_put,
504 };
505
506 static int tas_snd_treble_info(struct snd_kcontrol *kcontrol,
507         struct snd_ctl_elem_info *uinfo)
508 {
509         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
510         uinfo->count = 1;
511         uinfo->value.integer.min = TAS3004_TREBLE_MIN;
512         uinfo->value.integer.max = TAS3004_TREBLE_MAX;
513         return 0;
514 }
515
516 static int tas_snd_treble_get(struct snd_kcontrol *kcontrol,
517         struct snd_ctl_elem_value *ucontrol)
518 {
519         struct tas *tas = snd_kcontrol_chip(kcontrol);
520
521         ucontrol->value.integer.value[0] = tas->treble;
522         return 0;
523 }
524
525 static int tas_snd_treble_put(struct snd_kcontrol *kcontrol,
526         struct snd_ctl_elem_value *ucontrol)
527 {
528         struct tas *tas = snd_kcontrol_chip(kcontrol);
529
530         if (tas->treble == ucontrol->value.integer.value[0])
531                 return 0;
532
533         tas->treble = ucontrol->value.integer.value[0];
534         if (tas->hw_enabled)
535                 tas_set_treble(tas);
536         return 1;
537 }
538
539 static struct snd_kcontrol_new treble_control = {
540         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
541         .name = "Treble",
542         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
543         .info = tas_snd_treble_info,
544         .get = tas_snd_treble_get,
545         .put = tas_snd_treble_put,
546 };
547
548 static int tas_snd_bass_info(struct snd_kcontrol *kcontrol,
549         struct snd_ctl_elem_info *uinfo)
550 {
551         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
552         uinfo->count = 1;
553         uinfo->value.integer.min = TAS3004_BASS_MIN;
554         uinfo->value.integer.max = TAS3004_BASS_MAX;
555         return 0;
556 }
557
558 static int tas_snd_bass_get(struct snd_kcontrol *kcontrol,
559         struct snd_ctl_elem_value *ucontrol)
560 {
561         struct tas *tas = snd_kcontrol_chip(kcontrol);
562
563         ucontrol->value.integer.value[0] = tas->bass;
564         return 0;
565 }
566
567 static int tas_snd_bass_put(struct snd_kcontrol *kcontrol,
568         struct snd_ctl_elem_value *ucontrol)
569 {
570         struct tas *tas = snd_kcontrol_chip(kcontrol);
571
572         if (tas->bass == ucontrol->value.integer.value[0])
573                 return 0;
574
575         tas->bass = ucontrol->value.integer.value[0];
576         if (tas->hw_enabled)
577                 tas_set_bass(tas);
578         return 1;
579 }
580
581 static struct snd_kcontrol_new bass_control = {
582         .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
583         .name = "Bass",
584         .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
585         .info = tas_snd_bass_info,
586         .get = tas_snd_bass_get,
587         .put = tas_snd_bass_put,
588 };
589
590 static struct transfer_info tas_transfers[] = {
591         {
592                 /* input */
593                 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
594                            SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE,
595                 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
596                 .transfer_in = 1,
597         },
598         {
599                 /* output */
600                 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
601                            SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE,
602                 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
603                 .transfer_in = 0,
604         },
605         {}
606 };
607
608 static int tas_usable(struct codec_info_item *cii,
609                       struct transfer_info *ti,
610                       struct transfer_info *out)
611 {
612         return 1;
613 }
614
615 static int tas_reset_init(struct tas *tas)
616 {
617         u8 tmp;
618
619         tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
620         msleep(5);
621         tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
622         msleep(5);
623         tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1);
624         msleep(20);
625         tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
626         msleep(10);
627         tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
628
629         tmp = TAS_MCS_SCLK64 | TAS_MCS_SPORT_MODE_I2S | TAS_MCS_SPORT_WL_24BIT;
630         if (tas_write_reg(tas, TAS_REG_MCS, 1, &tmp))
631                 return -ENODEV;
632
633         tas->acr |= TAS_ACR_ANALOG_PDOWN | TAS_ACR_B_MONAUREAL |
634                 TAS_ACR_B_MON_SEL_RIGHT;
635         if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
636                 return -ENODEV;
637
638         tmp = 0;
639         if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp))
640                 return -ENODEV;
641
642         tas3004_set_drc(tas);
643
644         /* Set treble & bass to 0dB */
645         tas->treble = TAS3004_TREBLE_ZERO;
646         tas->bass = TAS3004_BASS_ZERO;
647         tas_set_treble(tas);
648         tas_set_bass(tas);
649
650         tas->acr &= ~TAS_ACR_ANALOG_PDOWN;
651         if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
652                 return -ENODEV;
653
654         return 0;
655 }
656
657 static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock)
658 {
659         struct tas *tas = cii->codec_data;
660
661         switch(clock) {
662         case CLOCK_SWITCH_PREPARE_SLAVE:
663                 /* Clocks are going away, mute mute mute */
664                 tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
665                 tas->hw_enabled = 0;
666                 break;
667         case CLOCK_SWITCH_SLAVE:
668                 /* Clocks are back, re-init the codec */
669                 tas_reset_init(tas);
670                 tas_set_volume(tas);
671                 tas_set_mixer(tas);
672                 tas->hw_enabled = 1;
673                 tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
674                 break;
675         default:
676                 /* doesn't happen as of now */
677                 return -EINVAL;
678         }
679         return 0;
680 }
681
682 /* we are controlled via i2c and assume that is always up
683  * If that wasn't the case, we'd have to suspend once
684  * our i2c device is suspended, and then take note of that! */
685 static int tas_suspend(struct tas *tas)
686 {
687         tas->hw_enabled = 0;
688         tas->acr |= TAS_ACR_ANALOG_PDOWN;
689         tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
690         return 0;
691 }
692
693 static int tas_resume(struct tas *tas)
694 {
695         /* reset codec */
696         tas_reset_init(tas);
697         tas_set_volume(tas);
698         tas_set_mixer(tas);
699         tas->hw_enabled = 1;
700         return 0;
701 }
702
703 #ifdef CONFIG_PM
704 static int _tas_suspend(struct codec_info_item *cii, pm_message_t state)
705 {
706         return tas_suspend(cii->codec_data);
707 }
708
709 static int _tas_resume(struct codec_info_item *cii)
710 {
711         return tas_resume(cii->codec_data);
712 }
713 #endif
714
715 static struct codec_info tas_codec_info = {
716         .transfers = tas_transfers,
717         /* in theory, we can drive it at 512 too...
718          * but so far the framework doesn't allow
719          * for that and I don't see much point in it. */
720         .sysclock_factor = 256,
721         /* same here, could be 32 for just one 16 bit format */
722         .bus_factor = 64,
723         .owner = THIS_MODULE,
724         .usable = tas_usable,
725         .switch_clock = tas_switch_clock,
726 #ifdef CONFIG_PM
727         .suspend = _tas_suspend,
728         .resume = _tas_resume,
729 #endif
730 };
731
732 static int tas_init_codec(struct aoa_codec *codec)
733 {
734         struct tas *tas = codec_to_tas(codec);
735         int err;
736
737         if (!tas->codec.gpio || !tas->codec.gpio->methods) {
738                 printk(KERN_ERR PFX "gpios not assigned!!\n");
739                 return -EINVAL;
740         }
741
742         if (tas_reset_init(tas)) {
743                 printk(KERN_ERR PFX "tas failed to initialise\n");
744                 return -ENXIO;
745         }
746         tas->hw_enabled = 1;
747
748         if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev,
749                                                    aoa_get_card(),
750                                                    &tas_codec_info, tas)) {
751                 printk(KERN_ERR PFX "error attaching tas to soundbus\n");
752                 return -ENODEV;
753         }
754
755         if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, tas, &ops)) {
756                 printk(KERN_ERR PFX "failed to create tas snd device!\n");
757                 return -ENODEV;
758         }
759         err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas));
760         if (err)
761                 goto error;
762
763         err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas));
764         if (err)
765                 goto error;
766
767         err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas));
768         if (err)
769                 goto error;
770
771         err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas));
772         if (err)
773                 goto error;
774
775         err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas));
776         if (err)
777                 goto error;
778
779         err = aoa_snd_ctl_add(snd_ctl_new1(&drc_range_control, tas));
780         if (err)
781                 goto error;
782
783         err = aoa_snd_ctl_add(snd_ctl_new1(&drc_switch_control, tas));
784         if (err)
785                 goto error;
786
787         err = aoa_snd_ctl_add(snd_ctl_new1(&treble_control, tas));
788         if (err)
789                 goto error;
790
791         err = aoa_snd_ctl_add(snd_ctl_new1(&bass_control, tas));
792         if (err)
793                 goto error;
794
795         return 0;
796  error:
797         tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
798         snd_device_free(aoa_get_card(), tas);
799         return err;
800 }
801
802 static void tas_exit_codec(struct aoa_codec *codec)
803 {
804         struct tas *tas = codec_to_tas(codec);
805
806         if (!tas->codec.soundbus_dev)
807                 return;
808         tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
809 }
810         
811
812 static struct i2c_driver tas_driver;
813
814 static int tas_create(struct i2c_adapter *adapter,
815                        struct device_node *node,
816                        int addr)
817 {
818         struct tas *tas;
819
820         tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
821
822         if (!tas)
823                 return -ENOMEM;
824
825         tas->i2c.driver = &tas_driver;
826         tas->i2c.adapter = adapter;
827         tas->i2c.addr = addr;
828         /* seems that half is a saner default */
829         tas->drc_range = TAS3004_DRC_MAX / 2;
830         strlcpy(tas->i2c.name, "tas audio codec", I2C_NAME_SIZE-1);
831
832         if (i2c_attach_client(&tas->i2c)) {
833                 printk(KERN_ERR PFX "failed to attach to i2c\n");
834                 goto fail;
835         }
836
837         strlcpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN-1);
838         tas->codec.owner = THIS_MODULE;
839         tas->codec.init = tas_init_codec;
840         tas->codec.exit = tas_exit_codec;
841         tas->codec.node = of_node_get(node);
842
843         if (aoa_codec_register(&tas->codec)) {
844                 goto detach;
845         }
846         printk(KERN_DEBUG
847                "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n",
848                addr, node->full_name);
849         return 0;
850  detach:
851         i2c_detach_client(&tas->i2c);
852  fail:
853         kfree(tas);
854         return -EINVAL;
855 }
856
857 static int tas_i2c_attach(struct i2c_adapter *adapter)
858 {
859         struct device_node *busnode, *dev = NULL;
860         struct pmac_i2c_bus *bus;
861
862         bus = pmac_i2c_adapter_to_bus(adapter);
863         if (bus == NULL)
864                 return -ENODEV;
865         busnode = pmac_i2c_get_bus_node(bus);
866
867         while ((dev = of_get_next_child(busnode, dev)) != NULL) {
868                 if (device_is_compatible(dev, "tas3004")) {
869                         u32 *addr;
870                         printk(KERN_DEBUG PFX "found tas3004\n");
871                         addr = (u32 *) get_property(dev, "reg", NULL);
872                         if (!addr)
873                                 continue;
874                         return tas_create(adapter, dev, ((*addr) >> 1) & 0x7f);
875                 }
876                 /* older machines have no 'codec' node with a 'compatible'
877                  * property that says 'tas3004', they just have a 'deq'
878                  * node without any such property... */
879                 if (strcmp(dev->name, "deq") == 0) {
880                         u32 *_addr, addr;
881                         printk(KERN_DEBUG PFX "found 'deq' node\n");
882                         _addr = (u32 *) get_property(dev, "i2c-address", NULL);
883                         if (!_addr)
884                                 continue;
885                         addr = ((*_addr) >> 1) & 0x7f;
886                         /* now, if the address doesn't match any of the two
887                          * that a tas3004 can have, we cannot handle this.
888                          * I doubt it ever happens but hey. */
889                         if (addr != 0x34 && addr != 0x35)
890                                 continue;
891                         return tas_create(adapter, dev, addr);
892                 }
893         }
894         return -ENODEV;
895 }
896
897 static int tas_i2c_detach(struct i2c_client *client)
898 {
899         struct tas *tas = container_of(client, struct tas, i2c);
900         int err;
901         u8 tmp = TAS_ACR_ANALOG_PDOWN;
902
903         if ((err = i2c_detach_client(client)))
904                 return err;
905         aoa_codec_unregister(&tas->codec);
906         of_node_put(tas->codec.node);
907
908         /* power down codec chip */
909         tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
910
911         kfree(tas);
912         return 0;
913 }
914
915 static struct i2c_driver tas_driver = {
916         .driver = {
917                 .name = "aoa_codec_tas",
918                 .owner = THIS_MODULE,
919         },
920         .attach_adapter = tas_i2c_attach,
921         .detach_client = tas_i2c_detach,
922 };
923
924 static int __init tas_init(void)
925 {
926         return i2c_add_driver(&tas_driver);
927 }
928
929 static void __exit tas_exit(void)
930 {
931         i2c_del_driver(&tas_driver);
932 }
933
934 module_init(tas_init);
935 module_exit(tas_exit);