Full support for Ginger Console
[linux-ginger.git] / sound / aoa / codecs / tas.c
blob1dd66ddffcaf6b97cf46ef2fe2f055a7ca317d37
1 /*
2 * Apple Onboard Audio driver for tas codec
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6 * GPL v2, can be found in COPYING.
8 * Open questions:
9 * - How to distinguish between 3004 and versions?
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...
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.
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.
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>
70 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
71 MODULE_LICENSE("GPL");
72 MODULE_DESCRIPTION("tas codec driver for snd-aoa");
74 #include "tas.h"
75 #include "tas-gain-table.h"
76 #include "tas-basstreble.h"
77 #include "../aoa.h"
78 #include "../soundbus/soundbus.h"
80 #define PFX "snd-aoa-codec-tas: "
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;
101 static int tas_reset_init(struct tas *tas);
103 static struct tas *codec_to_tas(struct aoa_codec *codec)
105 return container_of(codec, struct tas, codec);
108 static inline int tas_write_reg(struct tas *tas, u8 reg, u8 len, u8 *data)
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);
116 static void tas3004_set_drc(struct tas *tas)
118 unsigned char val[6];
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;
135 tas_write_reg(tas, TAS_REG_DRC, 6, val);
138 static void tas_set_treble(struct tas *tas)
140 u8 tmp;
142 tmp = tas3004_treble(tas->treble);
143 tas_write_reg(tas, TAS_REG_TREBLE, 1, &tmp);
146 static void tas_set_bass(struct tas *tas)
148 u8 tmp;
150 tmp = tas3004_bass(tas->bass);
151 tas_write_reg(tas, TAS_REG_BASS, 1, &tmp);
154 static void tas_set_volume(struct tas *tas)
156 u8 block[6];
157 int tmp;
158 u8 left, right;
160 left = tas->cached_volume_l;
161 right = tas->cached_volume_r;
163 if (left > 177) left = 177;
164 if (right > 177) right = 177;
166 if (tas->mute_l) left = 0;
167 if (tas->mute_r) right = 0;
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);
186 static void tas_set_mixer(struct tas *tas)
188 u8 block[9];
189 int tmp, i;
190 u8 val;
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;
200 tas_write_reg(tas, TAS_REG_LMIX, 9, block);
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;
210 tas_write_reg(tas, TAS_REG_RMIX, 9, block);
213 /* alsa stuff */
215 static int tas_dev_register(struct snd_device *dev)
217 return 0;
220 static struct snd_device_ops ops = {
221 .dev_register = tas_dev_register,
224 static int tas_snd_vol_info(struct snd_kcontrol *kcontrol,
225 struct snd_ctl_elem_info *uinfo)
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;
234 static int tas_snd_vol_get(struct snd_kcontrol *kcontrol,
235 struct snd_ctl_elem_value *ucontrol)
237 struct tas *tas = snd_kcontrol_chip(kcontrol);
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;
246 static int tas_snd_vol_put(struct snd_kcontrol *kcontrol,
247 struct snd_ctl_elem_value *ucontrol)
249 struct tas *tas = snd_kcontrol_chip(kcontrol);
251 if (ucontrol->value.integer.value[0] < 0 ||
252 ucontrol->value.integer.value[0] > 177)
253 return -EINVAL;
254 if (ucontrol->value.integer.value[1] < 0 ||
255 ucontrol->value.integer.value[1] > 177)
256 return -EINVAL;
258 mutex_lock(&tas->mtx);
259 if (tas->cached_volume_l == ucontrol->value.integer.value[0]
260 && tas->cached_volume_r == ucontrol->value.integer.value[1]) {
261 mutex_unlock(&tas->mtx);
262 return 0;
265 tas->cached_volume_l = ucontrol->value.integer.value[0];
266 tas->cached_volume_r = ucontrol->value.integer.value[1];
267 if (tas->hw_enabled)
268 tas_set_volume(tas);
269 mutex_unlock(&tas->mtx);
270 return 1;
273 static struct snd_kcontrol_new volume_control = {
274 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
275 .name = "Master Playback Volume",
276 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
277 .info = tas_snd_vol_info,
278 .get = tas_snd_vol_get,
279 .put = tas_snd_vol_put,
282 #define tas_snd_mute_info snd_ctl_boolean_stereo_info
284 static int tas_snd_mute_get(struct snd_kcontrol *kcontrol,
285 struct snd_ctl_elem_value *ucontrol)
287 struct tas *tas = snd_kcontrol_chip(kcontrol);
289 mutex_lock(&tas->mtx);
290 ucontrol->value.integer.value[0] = !tas->mute_l;
291 ucontrol->value.integer.value[1] = !tas->mute_r;
292 mutex_unlock(&tas->mtx);
293 return 0;
296 static int tas_snd_mute_put(struct snd_kcontrol *kcontrol,
297 struct snd_ctl_elem_value *ucontrol)
299 struct tas *tas = snd_kcontrol_chip(kcontrol);
301 mutex_lock(&tas->mtx);
302 if (tas->mute_l == !ucontrol->value.integer.value[0]
303 && tas->mute_r == !ucontrol->value.integer.value[1]) {
304 mutex_unlock(&tas->mtx);
305 return 0;
308 tas->mute_l = !ucontrol->value.integer.value[0];
309 tas->mute_r = !ucontrol->value.integer.value[1];
310 if (tas->hw_enabled)
311 tas_set_volume(tas);
312 mutex_unlock(&tas->mtx);
313 return 1;
316 static struct snd_kcontrol_new mute_control = {
317 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
318 .name = "Master Playback Switch",
319 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
320 .info = tas_snd_mute_info,
321 .get = tas_snd_mute_get,
322 .put = tas_snd_mute_put,
325 static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol,
326 struct snd_ctl_elem_info *uinfo)
328 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
329 uinfo->count = 2;
330 uinfo->value.integer.min = 0;
331 uinfo->value.integer.max = 177;
332 return 0;
335 static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol,
336 struct snd_ctl_elem_value *ucontrol)
338 struct tas *tas = snd_kcontrol_chip(kcontrol);
339 int idx = kcontrol->private_value;
341 mutex_lock(&tas->mtx);
342 ucontrol->value.integer.value[0] = tas->mixer_l[idx];
343 ucontrol->value.integer.value[1] = tas->mixer_r[idx];
344 mutex_unlock(&tas->mtx);
346 return 0;
349 static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol,
350 struct snd_ctl_elem_value *ucontrol)
352 struct tas *tas = snd_kcontrol_chip(kcontrol);
353 int idx = kcontrol->private_value;
355 mutex_lock(&tas->mtx);
356 if (tas->mixer_l[idx] == ucontrol->value.integer.value[0]
357 && tas->mixer_r[idx] == ucontrol->value.integer.value[1]) {
358 mutex_unlock(&tas->mtx);
359 return 0;
362 tas->mixer_l[idx] = ucontrol->value.integer.value[0];
363 tas->mixer_r[idx] = ucontrol->value.integer.value[1];
365 if (tas->hw_enabled)
366 tas_set_mixer(tas);
367 mutex_unlock(&tas->mtx);
368 return 1;
371 #define MIXER_CONTROL(n,descr,idx) \
372 static struct snd_kcontrol_new n##_control = { \
373 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
374 .name = descr " Playback Volume", \
375 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
376 .info = tas_snd_mixer_info, \
377 .get = tas_snd_mixer_get, \
378 .put = tas_snd_mixer_put, \
379 .private_value = idx, \
382 MIXER_CONTROL(pcm1, "PCM", 0);
383 MIXER_CONTROL(monitor, "Monitor", 2);
385 static int tas_snd_drc_range_info(struct snd_kcontrol *kcontrol,
386 struct snd_ctl_elem_info *uinfo)
388 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
389 uinfo->count = 1;
390 uinfo->value.integer.min = 0;
391 uinfo->value.integer.max = TAS3004_DRC_MAX;
392 return 0;
395 static int tas_snd_drc_range_get(struct snd_kcontrol *kcontrol,
396 struct snd_ctl_elem_value *ucontrol)
398 struct tas *tas = snd_kcontrol_chip(kcontrol);
400 mutex_lock(&tas->mtx);
401 ucontrol->value.integer.value[0] = tas->drc_range;
402 mutex_unlock(&tas->mtx);
403 return 0;
406 static int tas_snd_drc_range_put(struct snd_kcontrol *kcontrol,
407 struct snd_ctl_elem_value *ucontrol)
409 struct tas *tas = snd_kcontrol_chip(kcontrol);
411 if (ucontrol->value.integer.value[0] < 0 ||
412 ucontrol->value.integer.value[0] > TAS3004_DRC_MAX)
413 return -EINVAL;
415 mutex_lock(&tas->mtx);
416 if (tas->drc_range == ucontrol->value.integer.value[0]) {
417 mutex_unlock(&tas->mtx);
418 return 0;
421 tas->drc_range = ucontrol->value.integer.value[0];
422 if (tas->hw_enabled)
423 tas3004_set_drc(tas);
424 mutex_unlock(&tas->mtx);
425 return 1;
428 static struct snd_kcontrol_new drc_range_control = {
429 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
430 .name = "DRC Range",
431 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
432 .info = tas_snd_drc_range_info,
433 .get = tas_snd_drc_range_get,
434 .put = tas_snd_drc_range_put,
437 #define tas_snd_drc_switch_info snd_ctl_boolean_mono_info
439 static int tas_snd_drc_switch_get(struct snd_kcontrol *kcontrol,
440 struct snd_ctl_elem_value *ucontrol)
442 struct tas *tas = snd_kcontrol_chip(kcontrol);
444 mutex_lock(&tas->mtx);
445 ucontrol->value.integer.value[0] = tas->drc_enabled;
446 mutex_unlock(&tas->mtx);
447 return 0;
450 static int tas_snd_drc_switch_put(struct snd_kcontrol *kcontrol,
451 struct snd_ctl_elem_value *ucontrol)
453 struct tas *tas = snd_kcontrol_chip(kcontrol);
455 mutex_lock(&tas->mtx);
456 if (tas->drc_enabled == ucontrol->value.integer.value[0]) {
457 mutex_unlock(&tas->mtx);
458 return 0;
461 tas->drc_enabled = !!ucontrol->value.integer.value[0];
462 if (tas->hw_enabled)
463 tas3004_set_drc(tas);
464 mutex_unlock(&tas->mtx);
465 return 1;
468 static struct snd_kcontrol_new drc_switch_control = {
469 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
470 .name = "DRC Range Switch",
471 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
472 .info = tas_snd_drc_switch_info,
473 .get = tas_snd_drc_switch_get,
474 .put = tas_snd_drc_switch_put,
477 static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol,
478 struct snd_ctl_elem_info *uinfo)
480 static char *texts[] = { "Line-In", "Microphone" };
482 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
483 uinfo->count = 1;
484 uinfo->value.enumerated.items = 2;
485 if (uinfo->value.enumerated.item > 1)
486 uinfo->value.enumerated.item = 1;
487 strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]);
488 return 0;
491 static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
492 struct snd_ctl_elem_value *ucontrol)
494 struct tas *tas = snd_kcontrol_chip(kcontrol);
496 mutex_lock(&tas->mtx);
497 ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
498 mutex_unlock(&tas->mtx);
499 return 0;
502 static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
503 struct snd_ctl_elem_value *ucontrol)
505 struct tas *tas = snd_kcontrol_chip(kcontrol);
506 int oldacr;
508 if (ucontrol->value.enumerated.item[0] > 1)
509 return -EINVAL;
510 mutex_lock(&tas->mtx);
511 oldacr = tas->acr;
514 * Despite what the data sheet says in one place, the
515 * TAS_ACR_B_MONAUREAL bit forces mono output even when
516 * input A (line in) is selected.
518 tas->acr &= ~(TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL);
519 if (ucontrol->value.enumerated.item[0])
520 tas->acr |= TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL |
521 TAS_ACR_B_MON_SEL_RIGHT;
522 if (oldacr == tas->acr) {
523 mutex_unlock(&tas->mtx);
524 return 0;
526 if (tas->hw_enabled)
527 tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
528 mutex_unlock(&tas->mtx);
529 return 1;
532 static struct snd_kcontrol_new capture_source_control = {
533 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
534 /* If we name this 'Input Source', it properly shows up in
535 * alsamixer as a selection, * but it's shown under the
536 * 'Playback' category.
537 * If I name it 'Capture Source', it shows up in strange
538 * ways (two bools of which one can be selected at a
539 * time) but at least it's shown in the 'Capture'
540 * category.
541 * I was told that this was due to backward compatibility,
542 * but I don't understand then why the mangling is *not*
543 * done when I name it "Input Source".....
545 .name = "Capture Source",
546 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
547 .info = tas_snd_capture_source_info,
548 .get = tas_snd_capture_source_get,
549 .put = tas_snd_capture_source_put,
552 static int tas_snd_treble_info(struct snd_kcontrol *kcontrol,
553 struct snd_ctl_elem_info *uinfo)
555 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
556 uinfo->count = 1;
557 uinfo->value.integer.min = TAS3004_TREBLE_MIN;
558 uinfo->value.integer.max = TAS3004_TREBLE_MAX;
559 return 0;
562 static int tas_snd_treble_get(struct snd_kcontrol *kcontrol,
563 struct snd_ctl_elem_value *ucontrol)
565 struct tas *tas = snd_kcontrol_chip(kcontrol);
567 mutex_lock(&tas->mtx);
568 ucontrol->value.integer.value[0] = tas->treble;
569 mutex_unlock(&tas->mtx);
570 return 0;
573 static int tas_snd_treble_put(struct snd_kcontrol *kcontrol,
574 struct snd_ctl_elem_value *ucontrol)
576 struct tas *tas = snd_kcontrol_chip(kcontrol);
578 if (ucontrol->value.integer.value[0] < TAS3004_TREBLE_MIN ||
579 ucontrol->value.integer.value[0] > TAS3004_TREBLE_MAX)
580 return -EINVAL;
581 mutex_lock(&tas->mtx);
582 if (tas->treble == ucontrol->value.integer.value[0]) {
583 mutex_unlock(&tas->mtx);
584 return 0;
587 tas->treble = ucontrol->value.integer.value[0];
588 if (tas->hw_enabled)
589 tas_set_treble(tas);
590 mutex_unlock(&tas->mtx);
591 return 1;
594 static struct snd_kcontrol_new treble_control = {
595 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
596 .name = "Treble",
597 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
598 .info = tas_snd_treble_info,
599 .get = tas_snd_treble_get,
600 .put = tas_snd_treble_put,
603 static int tas_snd_bass_info(struct snd_kcontrol *kcontrol,
604 struct snd_ctl_elem_info *uinfo)
606 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
607 uinfo->count = 1;
608 uinfo->value.integer.min = TAS3004_BASS_MIN;
609 uinfo->value.integer.max = TAS3004_BASS_MAX;
610 return 0;
613 static int tas_snd_bass_get(struct snd_kcontrol *kcontrol,
614 struct snd_ctl_elem_value *ucontrol)
616 struct tas *tas = snd_kcontrol_chip(kcontrol);
618 mutex_lock(&tas->mtx);
619 ucontrol->value.integer.value[0] = tas->bass;
620 mutex_unlock(&tas->mtx);
621 return 0;
624 static int tas_snd_bass_put(struct snd_kcontrol *kcontrol,
625 struct snd_ctl_elem_value *ucontrol)
627 struct tas *tas = snd_kcontrol_chip(kcontrol);
629 if (ucontrol->value.integer.value[0] < TAS3004_BASS_MIN ||
630 ucontrol->value.integer.value[0] > TAS3004_BASS_MAX)
631 return -EINVAL;
632 mutex_lock(&tas->mtx);
633 if (tas->bass == ucontrol->value.integer.value[0]) {
634 mutex_unlock(&tas->mtx);
635 return 0;
638 tas->bass = ucontrol->value.integer.value[0];
639 if (tas->hw_enabled)
640 tas_set_bass(tas);
641 mutex_unlock(&tas->mtx);
642 return 1;
645 static struct snd_kcontrol_new bass_control = {
646 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
647 .name = "Bass",
648 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
649 .info = tas_snd_bass_info,
650 .get = tas_snd_bass_get,
651 .put = tas_snd_bass_put,
654 static struct transfer_info tas_transfers[] = {
656 /* input */
657 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S24_BE,
658 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
659 .transfer_in = 1,
662 /* output */
663 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S24_BE,
664 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
665 .transfer_in = 0,
670 static int tas_usable(struct codec_info_item *cii,
671 struct transfer_info *ti,
672 struct transfer_info *out)
674 return 1;
677 static int tas_reset_init(struct tas *tas)
679 u8 tmp;
681 tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
682 msleep(5);
683 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
684 msleep(5);
685 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1);
686 msleep(20);
687 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
688 msleep(10);
689 tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
691 tmp = TAS_MCS_SCLK64 | TAS_MCS_SPORT_MODE_I2S | TAS_MCS_SPORT_WL_24BIT;
692 if (tas_write_reg(tas, TAS_REG_MCS, 1, &tmp))
693 goto outerr;
695 tas->acr |= TAS_ACR_ANALOG_PDOWN;
696 if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
697 goto outerr;
699 tmp = 0;
700 if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp))
701 goto outerr;
703 tas3004_set_drc(tas);
705 /* Set treble & bass to 0dB */
706 tas->treble = TAS3004_TREBLE_ZERO;
707 tas->bass = TAS3004_BASS_ZERO;
708 tas_set_treble(tas);
709 tas_set_bass(tas);
711 tas->acr &= ~TAS_ACR_ANALOG_PDOWN;
712 if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
713 goto outerr;
715 return 0;
716 outerr:
717 return -ENODEV;
720 static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock)
722 struct tas *tas = cii->codec_data;
724 switch(clock) {
725 case CLOCK_SWITCH_PREPARE_SLAVE:
726 /* Clocks are going away, mute mute mute */
727 tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
728 tas->hw_enabled = 0;
729 break;
730 case CLOCK_SWITCH_SLAVE:
731 /* Clocks are back, re-init the codec */
732 mutex_lock(&tas->mtx);
733 tas_reset_init(tas);
734 tas_set_volume(tas);
735 tas_set_mixer(tas);
736 tas->hw_enabled = 1;
737 tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
738 mutex_unlock(&tas->mtx);
739 break;
740 default:
741 /* doesn't happen as of now */
742 return -EINVAL;
744 return 0;
747 #ifdef CONFIG_PM
748 /* we are controlled via i2c and assume that is always up
749 * If that wasn't the case, we'd have to suspend once
750 * our i2c device is suspended, and then take note of that! */
751 static int tas_suspend(struct tas *tas)
753 mutex_lock(&tas->mtx);
754 tas->hw_enabled = 0;
755 tas->acr |= TAS_ACR_ANALOG_PDOWN;
756 tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
757 mutex_unlock(&tas->mtx);
758 return 0;
761 static int tas_resume(struct tas *tas)
763 /* reset codec */
764 mutex_lock(&tas->mtx);
765 tas_reset_init(tas);
766 tas_set_volume(tas);
767 tas_set_mixer(tas);
768 tas->hw_enabled = 1;
769 mutex_unlock(&tas->mtx);
770 return 0;
773 static int _tas_suspend(struct codec_info_item *cii, pm_message_t state)
775 return tas_suspend(cii->codec_data);
778 static int _tas_resume(struct codec_info_item *cii)
780 return tas_resume(cii->codec_data);
782 #else /* CONFIG_PM */
783 #define _tas_suspend NULL
784 #define _tas_resume NULL
785 #endif /* CONFIG_PM */
787 static struct codec_info tas_codec_info = {
788 .transfers = tas_transfers,
789 /* in theory, we can drive it at 512 too...
790 * but so far the framework doesn't allow
791 * for that and I don't see much point in it. */
792 .sysclock_factor = 256,
793 /* same here, could be 32 for just one 16 bit format */
794 .bus_factor = 64,
795 .owner = THIS_MODULE,
796 .usable = tas_usable,
797 .switch_clock = tas_switch_clock,
798 .suspend = _tas_suspend,
799 .resume = _tas_resume,
802 static int tas_init_codec(struct aoa_codec *codec)
804 struct tas *tas = codec_to_tas(codec);
805 int err;
807 if (!tas->codec.gpio || !tas->codec.gpio->methods) {
808 printk(KERN_ERR PFX "gpios not assigned!!\n");
809 return -EINVAL;
812 mutex_lock(&tas->mtx);
813 if (tas_reset_init(tas)) {
814 printk(KERN_ERR PFX "tas failed to initialise\n");
815 mutex_unlock(&tas->mtx);
816 return -ENXIO;
818 tas->hw_enabled = 1;
819 mutex_unlock(&tas->mtx);
821 if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev,
822 aoa_get_card(),
823 &tas_codec_info, tas)) {
824 printk(KERN_ERR PFX "error attaching tas to soundbus\n");
825 return -ENODEV;
828 if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, tas, &ops)) {
829 printk(KERN_ERR PFX "failed to create tas snd device!\n");
830 return -ENODEV;
832 err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas));
833 if (err)
834 goto error;
836 err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas));
837 if (err)
838 goto error;
840 err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas));
841 if (err)
842 goto error;
844 err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas));
845 if (err)
846 goto error;
848 err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas));
849 if (err)
850 goto error;
852 err = aoa_snd_ctl_add(snd_ctl_new1(&drc_range_control, tas));
853 if (err)
854 goto error;
856 err = aoa_snd_ctl_add(snd_ctl_new1(&drc_switch_control, tas));
857 if (err)
858 goto error;
860 err = aoa_snd_ctl_add(snd_ctl_new1(&treble_control, tas));
861 if (err)
862 goto error;
864 err = aoa_snd_ctl_add(snd_ctl_new1(&bass_control, tas));
865 if (err)
866 goto error;
868 return 0;
869 error:
870 tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
871 snd_device_free(aoa_get_card(), tas);
872 return err;
875 static void tas_exit_codec(struct aoa_codec *codec)
877 struct tas *tas = codec_to_tas(codec);
879 if (!tas->codec.soundbus_dev)
880 return;
881 tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
885 static int tas_create(struct i2c_adapter *adapter,
886 struct device_node *node,
887 int addr)
889 struct i2c_board_info info;
890 struct i2c_client *client;
892 memset(&info, 0, sizeof(struct i2c_board_info));
893 strlcpy(info.type, "aoa_codec_tas", I2C_NAME_SIZE);
894 info.addr = addr;
895 info.platform_data = node;
897 client = i2c_new_device(adapter, &info);
898 if (!client)
899 return -ENODEV;
901 * We know the driver is already loaded, so the device should be
902 * already bound. If not it means binding failed, and then there
903 * is no point in keeping the device instantiated.
905 if (!client->driver) {
906 i2c_unregister_device(client);
907 return -ENODEV;
911 * Let i2c-core delete that device on driver removal.
912 * This is safe because i2c-core holds the core_lock mutex for us.
914 list_add_tail(&client->detected, &client->driver->clients);
915 return 0;
918 static int tas_i2c_probe(struct i2c_client *client,
919 const struct i2c_device_id *id)
921 struct device_node *node = client->dev.platform_data;
922 struct tas *tas;
924 tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
926 if (!tas)
927 return -ENOMEM;
929 mutex_init(&tas->mtx);
930 tas->i2c = client;
931 i2c_set_clientdata(client, tas);
933 /* seems that half is a saner default */
934 tas->drc_range = TAS3004_DRC_MAX / 2;
936 strlcpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN);
937 tas->codec.owner = THIS_MODULE;
938 tas->codec.init = tas_init_codec;
939 tas->codec.exit = tas_exit_codec;
940 tas->codec.node = of_node_get(node);
942 if (aoa_codec_register(&tas->codec)) {
943 goto fail;
945 printk(KERN_DEBUG
946 "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n",
947 (unsigned int)client->addr, node->full_name);
948 return 0;
949 fail:
950 mutex_destroy(&tas->mtx);
951 kfree(tas);
952 return -EINVAL;
955 static int tas_i2c_attach(struct i2c_adapter *adapter)
957 struct device_node *busnode, *dev = NULL;
958 struct pmac_i2c_bus *bus;
960 bus = pmac_i2c_adapter_to_bus(adapter);
961 if (bus == NULL)
962 return -ENODEV;
963 busnode = pmac_i2c_get_bus_node(bus);
965 while ((dev = of_get_next_child(busnode, dev)) != NULL) {
966 if (of_device_is_compatible(dev, "tas3004")) {
967 const u32 *addr;
968 printk(KERN_DEBUG PFX "found tas3004\n");
969 addr = of_get_property(dev, "reg", NULL);
970 if (!addr)
971 continue;
972 return tas_create(adapter, dev, ((*addr) >> 1) & 0x7f);
974 /* older machines have no 'codec' node with a 'compatible'
975 * property that says 'tas3004', they just have a 'deq'
976 * node without any such property... */
977 if (strcmp(dev->name, "deq") == 0) {
978 const u32 *_addr;
979 u32 addr;
980 printk(KERN_DEBUG PFX "found 'deq' node\n");
981 _addr = of_get_property(dev, "i2c-address", NULL);
982 if (!_addr)
983 continue;
984 addr = ((*_addr) >> 1) & 0x7f;
985 /* now, if the address doesn't match any of the two
986 * that a tas3004 can have, we cannot handle this.
987 * I doubt it ever happens but hey. */
988 if (addr != 0x34 && addr != 0x35)
989 continue;
990 return tas_create(adapter, dev, addr);
993 return -ENODEV;
996 static int tas_i2c_remove(struct i2c_client *client)
998 struct tas *tas = i2c_get_clientdata(client);
999 u8 tmp = TAS_ACR_ANALOG_PDOWN;
1001 aoa_codec_unregister(&tas->codec);
1002 of_node_put(tas->codec.node);
1004 /* power down codec chip */
1005 tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
1007 mutex_destroy(&tas->mtx);
1008 kfree(tas);
1009 return 0;
1012 static const struct i2c_device_id tas_i2c_id[] = {
1013 { "aoa_codec_tas", 0 },
1017 static struct i2c_driver tas_driver = {
1018 .driver = {
1019 .name = "aoa_codec_tas",
1020 .owner = THIS_MODULE,
1022 .attach_adapter = tas_i2c_attach,
1023 .probe = tas_i2c_probe,
1024 .remove = tas_i2c_remove,
1025 .id_table = tas_i2c_id,
1028 static int __init tas_init(void)
1030 return i2c_add_driver(&tas_driver);
1033 static void __exit tas_exit(void)
1035 i2c_del_driver(&tas_driver);
1038 module_init(tas_init);
1039 module_exit(tas_exit);