2 * Apple Onboard Audio driver for tas codec
4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
6 * GPL v2, can be found in COPYING.
9 * - How to distinguish between 3004 and versions?
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
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...
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
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.
63 #include <linux/i2c.h>
64 #include <linux/i2c-dev.h>
65 #include <asm/pmac_low_i2c.h>
67 #include <linux/delay.h>
68 #include <linux/module.h>
69 #include <linux/mutex.h>
71 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
72 MODULE_LICENSE("GPL");
73 MODULE_DESCRIPTION("tas codec driver for snd-aoa");
75 #include "snd-aoa-codec-tas.h"
76 #include "snd-aoa-codec-tas-gain-table.h"
77 #include "snd-aoa-codec-tas-basstreble.h"
79 #include "../soundbus/soundbus.h"
81 #define PFX "snd-aoa-codec-tas: "
85 struct aoa_codec codec
;
86 struct i2c_client i2c
;
87 u32 mute_l
:1, mute_r
:1 ,
91 u8 cached_volume_l
, cached_volume_r
;
92 u8 mixer_l
[3], mixer_r
[3];
96 /* protects hardware access against concurrency from
97 * userspace when hitting controls and during
98 * codec init/suspend/resume */
102 static int tas_reset_init(struct tas
*tas
);
104 static struct tas
*codec_to_tas(struct aoa_codec
*codec
)
106 return container_of(codec
, struct tas
, codec
);
109 static inline int tas_write_reg(struct tas
*tas
, u8 reg
, u8 len
, u8
*data
)
112 return i2c_smbus_write_byte_data(&tas
->i2c
, reg
, *data
);
114 return i2c_smbus_write_i2c_block_data(&tas
->i2c
, reg
, len
, data
);
117 static void tas3004_set_drc(struct tas
*tas
)
119 unsigned char val
[6];
121 if (tas
->drc_enabled
)
122 val
[0] = 0x50; /* 3:1 above threshold */
124 val
[0] = 0x51; /* disabled */
125 val
[1] = 0x02; /* 1:1 below threshold */
126 if (tas
->drc_range
> 0xef)
128 else if (tas
->drc_range
< 0)
131 val
[2] = tas
->drc_range
;
136 tas_write_reg(tas
, TAS_REG_DRC
, 6, val
);
139 static void tas_set_treble(struct tas
*tas
)
143 tmp
= tas3004_treble(tas
->treble
);
144 tas_write_reg(tas
, TAS_REG_TREBLE
, 1, &tmp
);
147 static void tas_set_bass(struct tas
*tas
)
151 tmp
= tas3004_bass(tas
->bass
);
152 tas_write_reg(tas
, TAS_REG_BASS
, 1, &tmp
);
155 static void tas_set_volume(struct tas
*tas
)
161 left
= tas
->cached_volume_l
;
162 right
= tas
->cached_volume_r
;
164 if (left
> 177) left
= 177;
165 if (right
> 177) right
= 177;
167 if (tas
->mute_l
) left
= 0;
168 if (tas
->mute_r
) right
= 0;
170 /* analysing the volume and mixer tables shows
171 * that they are similar enough when we shift
172 * the mixer table down by 4 bits. The error
173 * is miniscule, in just one item the error
174 * is 1, at a value of 0x07f17b (mixer table
175 * value is 0x07f17a) */
176 tmp
= tas_gaintable
[left
];
180 tmp
= tas_gaintable
[right
];
184 tas_write_reg(tas
, TAS_REG_VOL
, 6, block
);
187 static void tas_set_mixer(struct tas
*tas
)
194 val
= tas
->mixer_l
[i
];
195 if (val
> 177) val
= 177;
196 tmp
= tas_gaintable
[val
];
197 block
[3*i
+0] = tmp
>>16;
198 block
[3*i
+1] = tmp
>>8;
201 tas_write_reg(tas
, TAS_REG_LMIX
, 9, block
);
204 val
= tas
->mixer_r
[i
];
205 if (val
> 177) val
= 177;
206 tmp
= tas_gaintable
[val
];
207 block
[3*i
+0] = tmp
>>16;
208 block
[3*i
+1] = tmp
>>8;
211 tas_write_reg(tas
, TAS_REG_RMIX
, 9, block
);
216 static int tas_dev_register(struct snd_device
*dev
)
221 static struct snd_device_ops ops
= {
222 .dev_register
= tas_dev_register
,
225 static int tas_snd_vol_info(struct snd_kcontrol
*kcontrol
,
226 struct snd_ctl_elem_info
*uinfo
)
228 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
230 uinfo
->value
.integer
.min
= 0;
231 uinfo
->value
.integer
.max
= 177;
235 static int tas_snd_vol_get(struct snd_kcontrol
*kcontrol
,
236 struct snd_ctl_elem_value
*ucontrol
)
238 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
240 mutex_lock(&tas
->mtx
);
241 ucontrol
->value
.integer
.value
[0] = tas
->cached_volume_l
;
242 ucontrol
->value
.integer
.value
[1] = tas
->cached_volume_r
;
243 mutex_unlock(&tas
->mtx
);
247 static int tas_snd_vol_put(struct snd_kcontrol
*kcontrol
,
248 struct snd_ctl_elem_value
*ucontrol
)
250 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
252 mutex_lock(&tas
->mtx
);
253 if (tas
->cached_volume_l
== ucontrol
->value
.integer
.value
[0]
254 && tas
->cached_volume_r
== ucontrol
->value
.integer
.value
[1]) {
255 mutex_unlock(&tas
->mtx
);
259 tas
->cached_volume_l
= ucontrol
->value
.integer
.value
[0];
260 tas
->cached_volume_r
= ucontrol
->value
.integer
.value
[1];
263 mutex_unlock(&tas
->mtx
);
267 static struct snd_kcontrol_new volume_control
= {
268 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
269 .name
= "Master Playback Volume",
270 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
271 .info
= tas_snd_vol_info
,
272 .get
= tas_snd_vol_get
,
273 .put
= tas_snd_vol_put
,
276 static int tas_snd_mute_info(struct snd_kcontrol
*kcontrol
,
277 struct snd_ctl_elem_info
*uinfo
)
279 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
281 uinfo
->value
.integer
.min
= 0;
282 uinfo
->value
.integer
.max
= 1;
286 static int tas_snd_mute_get(struct snd_kcontrol
*kcontrol
,
287 struct snd_ctl_elem_value
*ucontrol
)
289 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
291 mutex_lock(&tas
->mtx
);
292 ucontrol
->value
.integer
.value
[0] = !tas
->mute_l
;
293 ucontrol
->value
.integer
.value
[1] = !tas
->mute_r
;
294 mutex_unlock(&tas
->mtx
);
298 static int tas_snd_mute_put(struct snd_kcontrol
*kcontrol
,
299 struct snd_ctl_elem_value
*ucontrol
)
301 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
303 mutex_lock(&tas
->mtx
);
304 if (tas
->mute_l
== !ucontrol
->value
.integer
.value
[0]
305 && tas
->mute_r
== !ucontrol
->value
.integer
.value
[1]) {
306 mutex_unlock(&tas
->mtx
);
310 tas
->mute_l
= !ucontrol
->value
.integer
.value
[0];
311 tas
->mute_r
= !ucontrol
->value
.integer
.value
[1];
314 mutex_unlock(&tas
->mtx
);
318 static struct snd_kcontrol_new mute_control
= {
319 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
320 .name
= "Master Playback Switch",
321 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
322 .info
= tas_snd_mute_info
,
323 .get
= tas_snd_mute_get
,
324 .put
= tas_snd_mute_put
,
327 static int tas_snd_mixer_info(struct snd_kcontrol
*kcontrol
,
328 struct snd_ctl_elem_info
*uinfo
)
330 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
332 uinfo
->value
.integer
.min
= 0;
333 uinfo
->value
.integer
.max
= 177;
337 static int tas_snd_mixer_get(struct snd_kcontrol
*kcontrol
,
338 struct snd_ctl_elem_value
*ucontrol
)
340 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
341 int idx
= kcontrol
->private_value
;
343 mutex_lock(&tas
->mtx
);
344 ucontrol
->value
.integer
.value
[0] = tas
->mixer_l
[idx
];
345 ucontrol
->value
.integer
.value
[1] = tas
->mixer_r
[idx
];
346 mutex_unlock(&tas
->mtx
);
351 static int tas_snd_mixer_put(struct snd_kcontrol
*kcontrol
,
352 struct snd_ctl_elem_value
*ucontrol
)
354 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
355 int idx
= kcontrol
->private_value
;
357 mutex_lock(&tas
->mtx
);
358 if (tas
->mixer_l
[idx
] == ucontrol
->value
.integer
.value
[0]
359 && tas
->mixer_r
[idx
] == ucontrol
->value
.integer
.value
[1]) {
360 mutex_unlock(&tas
->mtx
);
364 tas
->mixer_l
[idx
] = ucontrol
->value
.integer
.value
[0];
365 tas
->mixer_r
[idx
] = ucontrol
->value
.integer
.value
[1];
369 mutex_unlock(&tas
->mtx
);
373 #define MIXER_CONTROL(n,descr,idx) \
374 static struct snd_kcontrol_new n##_control = { \
375 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
376 .name = descr " Playback Volume", \
377 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
378 .info = tas_snd_mixer_info, \
379 .get = tas_snd_mixer_get, \
380 .put = tas_snd_mixer_put, \
381 .private_value = idx, \
384 MIXER_CONTROL(pcm1
, "PCM", 0);
385 MIXER_CONTROL(monitor
, "Monitor", 2);
387 static int tas_snd_drc_range_info(struct snd_kcontrol
*kcontrol
,
388 struct snd_ctl_elem_info
*uinfo
)
390 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
392 uinfo
->value
.integer
.min
= 0;
393 uinfo
->value
.integer
.max
= TAS3004_DRC_MAX
;
397 static int tas_snd_drc_range_get(struct snd_kcontrol
*kcontrol
,
398 struct snd_ctl_elem_value
*ucontrol
)
400 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
402 mutex_lock(&tas
->mtx
);
403 ucontrol
->value
.integer
.value
[0] = tas
->drc_range
;
404 mutex_unlock(&tas
->mtx
);
408 static int tas_snd_drc_range_put(struct snd_kcontrol
*kcontrol
,
409 struct snd_ctl_elem_value
*ucontrol
)
411 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
413 mutex_lock(&tas
->mtx
);
414 if (tas
->drc_range
== ucontrol
->value
.integer
.value
[0]) {
415 mutex_unlock(&tas
->mtx
);
419 tas
->drc_range
= ucontrol
->value
.integer
.value
[0];
421 tas3004_set_drc(tas
);
422 mutex_unlock(&tas
->mtx
);
426 static struct snd_kcontrol_new drc_range_control
= {
427 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
429 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
430 .info
= tas_snd_drc_range_info
,
431 .get
= tas_snd_drc_range_get
,
432 .put
= tas_snd_drc_range_put
,
435 static int tas_snd_drc_switch_info(struct snd_kcontrol
*kcontrol
,
436 struct snd_ctl_elem_info
*uinfo
)
438 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_BOOLEAN
;
440 uinfo
->value
.integer
.min
= 0;
441 uinfo
->value
.integer
.max
= 1;
445 static int tas_snd_drc_switch_get(struct snd_kcontrol
*kcontrol
,
446 struct snd_ctl_elem_value
*ucontrol
)
448 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
450 mutex_lock(&tas
->mtx
);
451 ucontrol
->value
.integer
.value
[0] = tas
->drc_enabled
;
452 mutex_unlock(&tas
->mtx
);
456 static int tas_snd_drc_switch_put(struct snd_kcontrol
*kcontrol
,
457 struct snd_ctl_elem_value
*ucontrol
)
459 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
461 mutex_lock(&tas
->mtx
);
462 if (tas
->drc_enabled
== ucontrol
->value
.integer
.value
[0]) {
463 mutex_unlock(&tas
->mtx
);
467 tas
->drc_enabled
= ucontrol
->value
.integer
.value
[0];
469 tas3004_set_drc(tas
);
470 mutex_unlock(&tas
->mtx
);
474 static struct snd_kcontrol_new drc_switch_control
= {
475 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
476 .name
= "DRC Range Switch",
477 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
478 .info
= tas_snd_drc_switch_info
,
479 .get
= tas_snd_drc_switch_get
,
480 .put
= tas_snd_drc_switch_put
,
483 static int tas_snd_capture_source_info(struct snd_kcontrol
*kcontrol
,
484 struct snd_ctl_elem_info
*uinfo
)
486 static char *texts
[] = { "Line-In", "Microphone" };
488 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
490 uinfo
->value
.enumerated
.items
= 2;
491 if (uinfo
->value
.enumerated
.item
> 1)
492 uinfo
->value
.enumerated
.item
= 1;
493 strcpy(uinfo
->value
.enumerated
.name
, texts
[uinfo
->value
.enumerated
.item
]);
497 static int tas_snd_capture_source_get(struct snd_kcontrol
*kcontrol
,
498 struct snd_ctl_elem_value
*ucontrol
)
500 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
502 mutex_lock(&tas
->mtx
);
503 ucontrol
->value
.enumerated
.item
[0] = !!(tas
->acr
& TAS_ACR_INPUT_B
);
504 mutex_unlock(&tas
->mtx
);
508 static int tas_snd_capture_source_put(struct snd_kcontrol
*kcontrol
,
509 struct snd_ctl_elem_value
*ucontrol
)
511 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
514 mutex_lock(&tas
->mtx
);
518 * Despite what the data sheet says in one place, the
519 * TAS_ACR_B_MONAUREAL bit forces mono output even when
520 * input A (line in) is selected.
522 tas
->acr
&= ~(TAS_ACR_INPUT_B
| TAS_ACR_B_MONAUREAL
);
523 if (ucontrol
->value
.enumerated
.item
[0])
524 tas
->acr
|= TAS_ACR_INPUT_B
| TAS_ACR_B_MONAUREAL
|
525 TAS_ACR_B_MON_SEL_RIGHT
;
526 if (oldacr
== tas
->acr
) {
527 mutex_unlock(&tas
->mtx
);
531 tas_write_reg(tas
, TAS_REG_ACR
, 1, &tas
->acr
);
532 mutex_unlock(&tas
->mtx
);
536 static struct snd_kcontrol_new capture_source_control
= {
537 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
538 /* If we name this 'Input Source', it properly shows up in
539 * alsamixer as a selection, * but it's shown under the
540 * 'Playback' category.
541 * If I name it 'Capture Source', it shows up in strange
542 * ways (two bools of which one can be selected at a
543 * time) but at least it's shown in the 'Capture'
545 * I was told that this was due to backward compatibility,
546 * but I don't understand then why the mangling is *not*
547 * done when I name it "Input Source".....
549 .name
= "Capture Source",
550 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
551 .info
= tas_snd_capture_source_info
,
552 .get
= tas_snd_capture_source_get
,
553 .put
= tas_snd_capture_source_put
,
556 static int tas_snd_treble_info(struct snd_kcontrol
*kcontrol
,
557 struct snd_ctl_elem_info
*uinfo
)
559 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
561 uinfo
->value
.integer
.min
= TAS3004_TREBLE_MIN
;
562 uinfo
->value
.integer
.max
= TAS3004_TREBLE_MAX
;
566 static int tas_snd_treble_get(struct snd_kcontrol
*kcontrol
,
567 struct snd_ctl_elem_value
*ucontrol
)
569 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
571 mutex_lock(&tas
->mtx
);
572 ucontrol
->value
.integer
.value
[0] = tas
->treble
;
573 mutex_unlock(&tas
->mtx
);
577 static int tas_snd_treble_put(struct snd_kcontrol
*kcontrol
,
578 struct snd_ctl_elem_value
*ucontrol
)
580 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
582 mutex_lock(&tas
->mtx
);
583 if (tas
->treble
== ucontrol
->value
.integer
.value
[0]) {
584 mutex_unlock(&tas
->mtx
);
588 tas
->treble
= ucontrol
->value
.integer
.value
[0];
591 mutex_unlock(&tas
->mtx
);
595 static struct snd_kcontrol_new treble_control
= {
596 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
598 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
599 .info
= tas_snd_treble_info
,
600 .get
= tas_snd_treble_get
,
601 .put
= tas_snd_treble_put
,
604 static int tas_snd_bass_info(struct snd_kcontrol
*kcontrol
,
605 struct snd_ctl_elem_info
*uinfo
)
607 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
609 uinfo
->value
.integer
.min
= TAS3004_BASS_MIN
;
610 uinfo
->value
.integer
.max
= TAS3004_BASS_MAX
;
614 static int tas_snd_bass_get(struct snd_kcontrol
*kcontrol
,
615 struct snd_ctl_elem_value
*ucontrol
)
617 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
619 mutex_lock(&tas
->mtx
);
620 ucontrol
->value
.integer
.value
[0] = tas
->bass
;
621 mutex_unlock(&tas
->mtx
);
625 static int tas_snd_bass_put(struct snd_kcontrol
*kcontrol
,
626 struct snd_ctl_elem_value
*ucontrol
)
628 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
630 mutex_lock(&tas
->mtx
);
631 if (tas
->bass
== ucontrol
->value
.integer
.value
[0]) {
632 mutex_unlock(&tas
->mtx
);
636 tas
->bass
= ucontrol
->value
.integer
.value
[0];
639 mutex_unlock(&tas
->mtx
);
643 static struct snd_kcontrol_new bass_control
= {
644 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
646 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
647 .info
= tas_snd_bass_info
,
648 .get
= tas_snd_bass_get
,
649 .put
= tas_snd_bass_put
,
652 static struct transfer_info tas_transfers
[] = {
655 .formats
= SNDRV_PCM_FMTBIT_S16_BE
| SNDRV_PCM_FMTBIT_S16_BE
|
656 SNDRV_PCM_FMTBIT_S24_BE
| SNDRV_PCM_FMTBIT_S24_BE
,
657 .rates
= SNDRV_PCM_RATE_32000
| SNDRV_PCM_RATE_44100
| SNDRV_PCM_RATE_48000
,
662 .formats
= SNDRV_PCM_FMTBIT_S16_BE
| SNDRV_PCM_FMTBIT_S16_BE
|
663 SNDRV_PCM_FMTBIT_S24_BE
| SNDRV_PCM_FMTBIT_S24_BE
,
664 .rates
= SNDRV_PCM_RATE_32000
| SNDRV_PCM_RATE_44100
| SNDRV_PCM_RATE_48000
,
670 static int tas_usable(struct codec_info_item
*cii
,
671 struct transfer_info
*ti
,
672 struct transfer_info
*out
)
677 static int tas_reset_init(struct tas
*tas
)
681 tas
->codec
.gpio
->methods
->all_amps_off(tas
->codec
.gpio
);
683 tas
->codec
.gpio
->methods
->set_hw_reset(tas
->codec
.gpio
, 0);
685 tas
->codec
.gpio
->methods
->set_hw_reset(tas
->codec
.gpio
, 1);
687 tas
->codec
.gpio
->methods
->set_hw_reset(tas
->codec
.gpio
, 0);
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
))
695 tas
->acr
|= TAS_ACR_ANALOG_PDOWN
;
696 if (tas_write_reg(tas
, TAS_REG_ACR
, 1, &tas
->acr
))
700 if (tas_write_reg(tas
, TAS_REG_MCS2
, 1, &tmp
))
703 tas3004_set_drc(tas
);
705 /* Set treble & bass to 0dB */
706 tas
->treble
= TAS3004_TREBLE_ZERO
;
707 tas
->bass
= TAS3004_BASS_ZERO
;
711 tas
->acr
&= ~TAS_ACR_ANALOG_PDOWN
;
712 if (tas_write_reg(tas
, TAS_REG_ACR
, 1, &tas
->acr
))
720 static int tas_switch_clock(struct codec_info_item
*cii
, enum clock_switch clock
)
722 struct tas
*tas
= cii
->codec_data
;
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
);
730 case CLOCK_SWITCH_SLAVE
:
731 /* Clocks are back, re-init the codec */
732 mutex_lock(&tas
->mtx
);
737 tas
->codec
.gpio
->methods
->all_amps_restore(tas
->codec
.gpio
);
738 mutex_unlock(&tas
->mtx
);
741 /* doesn't happen as of now */
747 /* we are controlled via i2c and assume that is always up
748 * If that wasn't the case, we'd have to suspend once
749 * our i2c device is suspended, and then take note of that! */
750 static int tas_suspend(struct tas
*tas
)
752 mutex_lock(&tas
->mtx
);
754 tas
->acr
|= TAS_ACR_ANALOG_PDOWN
;
755 tas_write_reg(tas
, TAS_REG_ACR
, 1, &tas
->acr
);
756 mutex_unlock(&tas
->mtx
);
760 static int tas_resume(struct tas
*tas
)
763 mutex_lock(&tas
->mtx
);
768 mutex_unlock(&tas
->mtx
);
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
);
784 static struct codec_info tas_codec_info
= {
785 .transfers
= tas_transfers
,
786 /* in theory, we can drive it at 512 too...
787 * but so far the framework doesn't allow
788 * for that and I don't see much point in it. */
789 .sysclock_factor
= 256,
790 /* same here, could be 32 for just one 16 bit format */
792 .owner
= THIS_MODULE
,
793 .usable
= tas_usable
,
794 .switch_clock
= tas_switch_clock
,
796 .suspend
= _tas_suspend
,
797 .resume
= _tas_resume
,
801 static int tas_init_codec(struct aoa_codec
*codec
)
803 struct tas
*tas
= codec_to_tas(codec
);
806 if (!tas
->codec
.gpio
|| !tas
->codec
.gpio
->methods
) {
807 printk(KERN_ERR PFX
"gpios not assigned!!\n");
811 mutex_lock(&tas
->mtx
);
812 if (tas_reset_init(tas
)) {
813 printk(KERN_ERR PFX
"tas failed to initialise\n");
814 mutex_unlock(&tas
->mtx
);
818 mutex_unlock(&tas
->mtx
);
820 if (tas
->codec
.soundbus_dev
->attach_codec(tas
->codec
.soundbus_dev
,
822 &tas_codec_info
, tas
)) {
823 printk(KERN_ERR PFX
"error attaching tas to soundbus\n");
827 if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL
, tas
, &ops
)) {
828 printk(KERN_ERR PFX
"failed to create tas snd device!\n");
831 err
= aoa_snd_ctl_add(snd_ctl_new1(&volume_control
, tas
));
835 err
= aoa_snd_ctl_add(snd_ctl_new1(&mute_control
, tas
));
839 err
= aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control
, tas
));
843 err
= aoa_snd_ctl_add(snd_ctl_new1(&monitor_control
, tas
));
847 err
= aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control
, tas
));
851 err
= aoa_snd_ctl_add(snd_ctl_new1(&drc_range_control
, tas
));
855 err
= aoa_snd_ctl_add(snd_ctl_new1(&drc_switch_control
, tas
));
859 err
= aoa_snd_ctl_add(snd_ctl_new1(&treble_control
, tas
));
863 err
= aoa_snd_ctl_add(snd_ctl_new1(&bass_control
, tas
));
869 tas
->codec
.soundbus_dev
->detach_codec(tas
->codec
.soundbus_dev
, tas
);
870 snd_device_free(aoa_get_card(), tas
);
874 static void tas_exit_codec(struct aoa_codec
*codec
)
876 struct tas
*tas
= codec_to_tas(codec
);
878 if (!tas
->codec
.soundbus_dev
)
880 tas
->codec
.soundbus_dev
->detach_codec(tas
->codec
.soundbus_dev
, tas
);
884 static struct i2c_driver tas_driver
;
886 static int tas_create(struct i2c_adapter
*adapter
,
887 struct device_node
*node
,
892 tas
= kzalloc(sizeof(struct tas
), GFP_KERNEL
);
897 mutex_init(&tas
->mtx
);
898 tas
->i2c
.driver
= &tas_driver
;
899 tas
->i2c
.adapter
= adapter
;
900 tas
->i2c
.addr
= addr
;
901 /* seems that half is a saner default */
902 tas
->drc_range
= TAS3004_DRC_MAX
/ 2;
903 strlcpy(tas
->i2c
.name
, "tas audio codec", I2C_NAME_SIZE
-1);
905 if (i2c_attach_client(&tas
->i2c
)) {
906 printk(KERN_ERR PFX
"failed to attach to i2c\n");
910 strlcpy(tas
->codec
.name
, "tas", MAX_CODEC_NAME_LEN
-1);
911 tas
->codec
.owner
= THIS_MODULE
;
912 tas
->codec
.init
= tas_init_codec
;
913 tas
->codec
.exit
= tas_exit_codec
;
914 tas
->codec
.node
= of_node_get(node
);
916 if (aoa_codec_register(&tas
->codec
)) {
920 "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n",
921 addr
, node
->full_name
);
924 i2c_detach_client(&tas
->i2c
);
926 mutex_destroy(&tas
->mtx
);
931 static int tas_i2c_attach(struct i2c_adapter
*adapter
)
933 struct device_node
*busnode
, *dev
= NULL
;
934 struct pmac_i2c_bus
*bus
;
936 bus
= pmac_i2c_adapter_to_bus(adapter
);
939 busnode
= pmac_i2c_get_bus_node(bus
);
941 while ((dev
= of_get_next_child(busnode
, dev
)) != NULL
) {
942 if (device_is_compatible(dev
, "tas3004")) {
944 printk(KERN_DEBUG PFX
"found tas3004\n");
945 addr
= (u32
*) get_property(dev
, "reg", NULL
);
948 return tas_create(adapter
, dev
, ((*addr
) >> 1) & 0x7f);
950 /* older machines have no 'codec' node with a 'compatible'
951 * property that says 'tas3004', they just have a 'deq'
952 * node without any such property... */
953 if (strcmp(dev
->name
, "deq") == 0) {
955 printk(KERN_DEBUG PFX
"found 'deq' node\n");
956 _addr
= (u32
*) get_property(dev
, "i2c-address", NULL
);
959 addr
= ((*_addr
) >> 1) & 0x7f;
960 /* now, if the address doesn't match any of the two
961 * that a tas3004 can have, we cannot handle this.
962 * I doubt it ever happens but hey. */
963 if (addr
!= 0x34 && addr
!= 0x35)
965 return tas_create(adapter
, dev
, addr
);
971 static int tas_i2c_detach(struct i2c_client
*client
)
973 struct tas
*tas
= container_of(client
, struct tas
, i2c
);
975 u8 tmp
= TAS_ACR_ANALOG_PDOWN
;
977 if ((err
= i2c_detach_client(client
)))
979 aoa_codec_unregister(&tas
->codec
);
980 of_node_put(tas
->codec
.node
);
982 /* power down codec chip */
983 tas_write_reg(tas
, TAS_REG_ACR
, 1, &tmp
);
985 mutex_destroy(&tas
->mtx
);
990 static struct i2c_driver tas_driver
= {
992 .name
= "aoa_codec_tas",
993 .owner
= THIS_MODULE
,
995 .attach_adapter
= tas_i2c_attach
,
996 .detach_client
= tas_i2c_detach
,
999 static int __init
tas_init(void)
1001 return i2c_add_driver(&tas_driver
);
1004 static void __exit
tas_exit(void)
1006 i2c_del_driver(&tas_driver
);
1009 module_init(tas_init
);
1010 module_exit(tas_exit
);