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