[PATCH] aoa: tas: fix initialisation/reset
[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 "../aoa.h"
76 #include "../soundbus/soundbus.h"
77
78 #define PFX "snd-aoa-codec-tas: "
79
80
81 struct tas {
82         struct aoa_codec        codec;
83         struct i2c_client       i2c;
84         u32                     mute_l:1, mute_r:1 ,
85                                 controls_created:1 ,
86                                 drc_enabled:1,
87                                 hw_enabled:1;
88         u8                      cached_volume_l, cached_volume_r;
89         u8                      mixer_l[3], mixer_r[3];
90         u8                      acr;
91         int                     drc_range;
92 };
93
94 static int tas_reset_init(struct tas *tas);
95
96 static struct tas *codec_to_tas(struct aoa_codec *codec)
97 {
98         return container_of(codec, struct tas, codec);
99 }
100
101 static inline int tas_write_reg(struct tas *tas, u8 reg, u8 len, u8 *data)
102 {
103         if (len == 1)
104                 return i2c_smbus_write_byte_data(&tas->i2c, reg, *data);
105         else
106                 return i2c_smbus_write_i2c_block_data(&tas->i2c, reg, len, data);
107 }
108
109 static void tas3004_set_drc(struct tas *tas)
110 {
111         unsigned char val[6];
112
113         if (tas->drc_enabled)
114                 val[0] = 0x50; /* 3:1 above threshold */
115         else
116                 val[0] = 0x51; /* disabled */
117         val[1] = 0x02; /* 1:1 below threshold */
118         if (tas->drc_range > 0xef)
119                 val[2] = 0xef;
120         else if (tas->drc_range < 0)
121                 val[2] = 0x00;
122         else
123                 val[2] = tas->drc_range;
124         val[3] = 0xb0;
125         val[4] = 0x60;
126         val[5] = 0xa0;
127
128         tas_write_reg(tas, TAS_REG_DRC, 6, val);
129 }
130
131 static void tas_set_volume(struct tas *tas)
132 {
133         u8 block[6];
134         int tmp;
135         u8 left, right;
136
137         left = tas->cached_volume_l;
138         right = tas->cached_volume_r;
139
140         if (left > 177) left = 177;
141         if (right > 177) right = 177;
142
143         if (tas->mute_l) left = 0;
144         if (tas->mute_r) right = 0;
145
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];
153         block[0] = tmp>>20;
154         block[1] = tmp>>12;
155         block[2] = tmp>>4;
156         tmp = tas_gaintable[right];
157         block[3] = tmp>>20;
158         block[4] = tmp>>12;
159         block[5] = tmp>>4;
160         tas_write_reg(tas, TAS_REG_VOL, 6, block);
161 }
162
163 static void tas_set_mixer(struct tas *tas)
164 {
165         u8 block[9];
166         int tmp, i;
167         u8 val;
168
169         for (i=0;i<3;i++) {
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;
175                 block[3*i+2] = tmp;
176         }
177         tas_write_reg(tas, TAS_REG_LMIX, 9, block);
178
179         for (i=0;i<3;i++) {
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;
185                 block[3*i+2] = tmp;
186         }
187         tas_write_reg(tas, TAS_REG_RMIX, 9, block);
188 }
189
190 /* alsa stuff */
191
192 static int tas_dev_register(struct snd_device *dev)
193 {
194         return 0;
195 }
196
197 static struct snd_device_ops ops = {
198         .dev_register = tas_dev_register,
199 };
200
201 static int tas_snd_vol_info(struct snd_kcontrol *kcontrol,
202         struct snd_ctl_elem_info *uinfo)
203 {
204         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
205         uinfo->count = 2;
206         uinfo->value.integer.min = 0;
207         uinfo->value.integer.max = 177;
208         return 0;
209 }
210
211 static int tas_snd_vol_get(struct snd_kcontrol *kcontrol,
212         struct snd_ctl_elem_value *ucontrol)
213 {
214         struct tas *tas = snd_kcontrol_chip(kcontrol);
215
216         ucontrol->value.integer.value[0] = tas->cached_volume_l;
217         ucontrol->value.integer.value[1] = tas->cached_volume_r;
218         return 0;
219 }
220
221 static int tas_snd_vol_put(struct snd_kcontrol *kcontrol,
222         struct snd_ctl_elem_value *ucontrol)
223 {
224         struct tas *tas = snd_kcontrol_chip(kcontrol);
225
226         if (tas->cached_volume_l == ucontrol->value.integer.value[0]
227          && tas->cached_volume_r == ucontrol->value.integer.value[1])
228                 return 0;
229
230         tas->cached_volume_l = ucontrol->value.integer.value[0];
231         tas->cached_volume_r = ucontrol->value.integer.value[1];
232         if (tas->hw_enabled)
233                 tas_set_volume(tas);
234         return 1;
235 }
236
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,
244 };
245
246 static int tas_snd_mute_info(struct snd_kcontrol *kcontrol,
247         struct snd_ctl_elem_info *uinfo)
248 {
249         uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
250         uinfo->count = 2;
251         uinfo->value.integer.min = 0;
252         uinfo->value.integer.max = 1;
253         return 0;
254 }
255
256 static int tas_snd_mute_get(struct snd_kcontrol *kcontrol,
257         struct snd_ctl_elem_value *ucontrol)
258 {
259         struct tas *tas = snd_kcontrol_chip(kcontrol);
260
261         ucontrol->value.integer.value[0] = !tas->mute_l;
262         ucontrol->value.integer.value[1] = !tas->mute_r;
263         return 0;
264 }
265
266 static int tas_snd_mute_put(struct snd_kcontrol *kcontrol,
267         struct snd_ctl_elem_value *ucontrol)
268 {
269         struct tas *tas = snd_kcontrol_chip(kcontrol);
270
271         if (tas->mute_l == !ucontrol->value.integer.value[0]
272          && tas->mute_r == !ucontrol->value.integer.value[1])
273                 return 0;
274
275         tas->mute_l = !ucontrol->value.integer.value[0];
276         tas->mute_r = !ucontrol->value.integer.value[1];
277         if (tas->hw_enabled)
278                 tas_set_volume(tas);
279         return 1;
280 }
281
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,
289 };
290
291 static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol,
292         struct snd_ctl_elem_info *uinfo)
293 {
294         uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
295         uinfo->count = 2;
296         uinfo->value.integer.min = 0;
297         uinfo->value.integer.max = 177;
298         return 0;
299 }
300
301 static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol,
302         struct snd_ctl_elem_value *ucontrol)
303 {
304         struct tas *tas = snd_kcontrol_chip(kcontrol);
305         int idx = kcontrol->private_value;
306
307         ucontrol->value.integer.value[0] = tas->mixer_l[idx];
308         ucontrol->value.integer.value[1] = tas->mixer_r[idx];
309
310         return 0;
311 }
312
313 static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol,
314         struct snd_ctl_elem_value *ucontrol)
315 {
316         struct tas *tas = snd_kcontrol_chip(kcontrol);
317         int idx = kcontrol->private_value;
318
319         if (tas->mixer_l[idx] == ucontrol->value.integer.value[0]
320          && tas->mixer_r[idx] == ucontrol->value.integer.value[1])
321                 return 0;
322
323         tas->mixer_l[idx] = ucontrol->value.integer.value[0];
324         tas->mixer_r[idx] = ucontrol->value.integer.value[1];
325
326         if (tas->hw_enabled)
327                 tas_set_mixer(tas);
328         return 1;
329 }
330
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,                           \
340 }
341
342 MIXER_CONTROL(pcm1, "PCM", 0);
343 MIXER_CONTROL(monitor, "Monitor", 2);
344
345 static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol,
346         struct snd_ctl_elem_info *uinfo)
347 {
348         static char *texts[] = { "Line-In", "Microphone" };
349
350         uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
351         uinfo->count = 1;
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]);
356         return 0;
357 }
358
359 static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
360         struct snd_ctl_elem_value *ucontrol)
361 {
362         struct tas *tas = snd_kcontrol_chip(kcontrol);
363
364         ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
365         return 0;
366 }
367
368 static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
369         struct snd_ctl_elem_value *ucontrol)
370 {
371         struct tas *tas = snd_kcontrol_chip(kcontrol);
372         int oldacr = tas->acr;
373
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)
378                 return 0;
379         if (tas->hw_enabled)
380                 tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
381         return 1;
382 }
383
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'
392          * category.
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".....
396          */
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,
402 };
403
404
405 static struct transfer_info tas_transfers[] = {
406         {
407                 /* input */
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,
411                 .transfer_in = 1,
412         },
413         {
414                 /* output */
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,
418                 .transfer_in = 0,
419         },
420         {}
421 };
422
423 static int tas_usable(struct codec_info_item *cii,
424                       struct transfer_info *ti,
425                       struct transfer_info *out)
426 {
427         return 1;
428 }
429
430 static int tas_reset_init(struct tas *tas)
431 {
432         u8 tmp;
433
434         tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
435         msleep(5);
436         tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
437         msleep(5);
438         tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1);
439         msleep(20);
440         tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
441         msleep(10);
442         tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
443
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))
446                 return -ENODEV;
447
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))
451                 return -ENODEV;
452
453         tmp = 0;
454         if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp))
455                 return -ENODEV;
456
457         tas3004_set_drc(tas);
458
459         /* Set treble & bass to 0dB */
460         tmp = 114;
461         tas_write_reg(tas, TAS_REG_TREBLE, 1, &tmp);
462         tas_write_reg(tas, TAS_REG_BASS, 1, &tmp);
463
464         tas->acr &= ~TAS_ACR_ANALOG_PDOWN;
465         if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
466                 return -ENODEV;
467
468         return 0;
469 }
470
471 static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock)
472 {
473         struct tas *tas = cii->codec_data;
474
475         switch(clock) {
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);
479                 tas->hw_enabled = 0;
480                 break;
481         case CLOCK_SWITCH_SLAVE:
482                 /* Clocks are back, re-init the codec */
483                 tas_reset_init(tas);
484                 tas_set_volume(tas);
485                 tas_set_mixer(tas);
486                 tas->hw_enabled = 1;
487                 tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
488                 break;
489         default:
490                 /* doesn't happen as of now */
491                 return -EINVAL;
492         }
493         return 0;
494 }
495
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)
500 {
501         tas->hw_enabled = 0;
502         tas->acr |= TAS_ACR_ANALOG_PDOWN;
503         tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
504         return 0;
505 }
506
507 static int tas_resume(struct tas *tas)
508 {
509         /* reset codec */
510         tas_reset_init(tas);
511         tas_set_volume(tas);
512         tas_set_mixer(tas);
513         tas->hw_enabled = 1;
514         return 0;
515 }
516
517 #ifdef CONFIG_PM
518 static int _tas_suspend(struct codec_info_item *cii, pm_message_t state)
519 {
520         return tas_suspend(cii->codec_data);
521 }
522
523 static int _tas_resume(struct codec_info_item *cii)
524 {
525         return tas_resume(cii->codec_data);
526 }
527 #endif
528
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 */
536         .bus_factor = 64,
537         .owner = THIS_MODULE,
538         .usable = tas_usable,
539         .switch_clock = tas_switch_clock,
540 #ifdef CONFIG_PM
541         .suspend = _tas_suspend,
542         .resume = _tas_resume,
543 #endif
544 };
545
546 static int tas_init_codec(struct aoa_codec *codec)
547 {
548         struct tas *tas = codec_to_tas(codec);
549         int err;
550
551         if (!tas->codec.gpio || !tas->codec.gpio->methods) {
552                 printk(KERN_ERR PFX "gpios not assigned!!\n");
553                 return -EINVAL;
554         }
555
556         if (tas_reset_init(tas)) {
557                 printk(KERN_ERR PFX "tas failed to initialise\n");
558                 return -ENXIO;
559         }
560         tas->hw_enabled = 1;
561
562         if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev,
563                                                    aoa_get_card(),
564                                                    &tas_codec_info, tas)) {
565                 printk(KERN_ERR PFX "error attaching tas to soundbus\n");
566                 return -ENODEV;
567         }
568
569         if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, tas, &ops)) {
570                 printk(KERN_ERR PFX "failed to create tas snd device!\n");
571                 return -ENODEV;
572         }
573         err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas));
574         if (err)
575                 goto error;
576
577         err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas));
578         if (err)
579                 goto error;
580
581         err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas));
582         if (err)
583                 goto error;
584
585         err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas));
586         if (err)
587                 goto error;
588
589         err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas));
590         if (err)
591                 goto error;
592
593         return 0;
594  error:
595         tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
596         snd_device_free(aoa_get_card(), tas);
597         return err;
598 }
599
600 static void tas_exit_codec(struct aoa_codec *codec)
601 {
602         struct tas *tas = codec_to_tas(codec);
603
604         if (!tas->codec.soundbus_dev)
605                 return;
606         tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
607 }
608         
609
610 static struct i2c_driver tas_driver;
611
612 static int tas_create(struct i2c_adapter *adapter,
613                        struct device_node *node,
614                        int addr)
615 {
616         struct tas *tas;
617
618         tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
619
620         if (!tas)
621                 return -ENOMEM;
622
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);
628
629         if (i2c_attach_client(&tas->i2c)) {
630                 printk(KERN_ERR PFX "failed to attach to i2c\n");
631                 goto fail;
632         }
633
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);
639
640         if (aoa_codec_register(&tas->codec)) {
641                 goto detach;
642         }
643         printk(KERN_DEBUG
644                "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n",
645                addr, node->full_name);
646         return 0;
647  detach:
648         i2c_detach_client(&tas->i2c);
649  fail:
650         kfree(tas);
651         return -EINVAL;
652 }
653
654 static int tas_i2c_attach(struct i2c_adapter *adapter)
655 {
656         struct device_node *busnode, *dev = NULL;
657         struct pmac_i2c_bus *bus;
658
659         bus = pmac_i2c_adapter_to_bus(adapter);
660         if (bus == NULL)
661                 return -ENODEV;
662         busnode = pmac_i2c_get_bus_node(bus);
663
664         while ((dev = of_get_next_child(busnode, dev)) != NULL) {
665                 if (device_is_compatible(dev, "tas3004")) {
666                         u32 *addr;
667                         printk(KERN_DEBUG PFX "found tas3004\n");
668                         addr = (u32 *) get_property(dev, "reg", NULL);
669                         if (!addr)
670                                 continue;
671                         return tas_create(adapter, dev, ((*addr) >> 1) & 0x7f);
672                 }
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) {
677                         u32 *_addr, addr;
678                         printk(KERN_DEBUG PFX "found 'deq' node\n");
679                         _addr = (u32 *) get_property(dev, "i2c-address", NULL);
680                         if (!_addr)
681                                 continue;
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)
687                                 continue;
688                         return tas_create(adapter, dev, addr);
689                 }
690         }
691         return -ENODEV;
692 }
693
694 static int tas_i2c_detach(struct i2c_client *client)
695 {
696         struct tas *tas = container_of(client, struct tas, i2c);
697         int err;
698         u8 tmp = TAS_ACR_ANALOG_PDOWN;
699
700         if ((err = i2c_detach_client(client)))
701                 return err;
702         aoa_codec_unregister(&tas->codec);
703         of_node_put(tas->codec.node);
704
705         /* power down codec chip */
706         tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
707
708         kfree(tas);
709         return 0;
710 }
711
712 static struct i2c_driver tas_driver = {
713         .driver = {
714                 .name = "aoa_codec_tas",
715                 .owner = THIS_MODULE,
716         },
717         .attach_adapter = tas_i2c_attach,
718         .detach_client = tas_i2c_detach,
719 };
720
721 static int __init tas_init(void)
722 {
723         return i2c_add_driver(&tas_driver);
724 }
725
726 static void __exit tas_exit(void)
727 {
728         i2c_del_driver(&tas_driver);
729 }
730
731 module_init(tas_init);
732 module_exit(tas_exit);