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 <asm/pmac_low_i2c.h>
66 #include <linux/delay.h>
67 #include <linux/module.h>
68 #include <linux/mutex.h>
69 #include <linux/slab.h>
71 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
72 MODULE_LICENSE("GPL");
73 MODULE_DESCRIPTION("tas codec driver for snd-aoa");
76 #include "tas-gain-table.h"
77 #include "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 if (ucontrol
->value
.integer
.value
[0] < 0 ||
253 ucontrol
->value
.integer
.value
[0] > 177)
255 if (ucontrol
->value
.integer
.value
[1] < 0 ||
256 ucontrol
->value
.integer
.value
[1] > 177)
259 mutex_lock(&tas
->mtx
);
260 if (tas
->cached_volume_l
== ucontrol
->value
.integer
.value
[0]
261 && tas
->cached_volume_r
== ucontrol
->value
.integer
.value
[1]) {
262 mutex_unlock(&tas
->mtx
);
266 tas
->cached_volume_l
= ucontrol
->value
.integer
.value
[0];
267 tas
->cached_volume_r
= ucontrol
->value
.integer
.value
[1];
270 mutex_unlock(&tas
->mtx
);
274 static struct snd_kcontrol_new volume_control
= {
275 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
276 .name
= "Master Playback Volume",
277 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
278 .info
= tas_snd_vol_info
,
279 .get
= tas_snd_vol_get
,
280 .put
= tas_snd_vol_put
,
283 #define tas_snd_mute_info snd_ctl_boolean_stereo_info
285 static int tas_snd_mute_get(struct snd_kcontrol
*kcontrol
,
286 struct snd_ctl_elem_value
*ucontrol
)
288 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
290 mutex_lock(&tas
->mtx
);
291 ucontrol
->value
.integer
.value
[0] = !tas
->mute_l
;
292 ucontrol
->value
.integer
.value
[1] = !tas
->mute_r
;
293 mutex_unlock(&tas
->mtx
);
297 static int tas_snd_mute_put(struct snd_kcontrol
*kcontrol
,
298 struct snd_ctl_elem_value
*ucontrol
)
300 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
302 mutex_lock(&tas
->mtx
);
303 if (tas
->mute_l
== !ucontrol
->value
.integer
.value
[0]
304 && tas
->mute_r
== !ucontrol
->value
.integer
.value
[1]) {
305 mutex_unlock(&tas
->mtx
);
309 tas
->mute_l
= !ucontrol
->value
.integer
.value
[0];
310 tas
->mute_r
= !ucontrol
->value
.integer
.value
[1];
313 mutex_unlock(&tas
->mtx
);
317 static struct snd_kcontrol_new mute_control
= {
318 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
319 .name
= "Master Playback Switch",
320 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
321 .info
= tas_snd_mute_info
,
322 .get
= tas_snd_mute_get
,
323 .put
= tas_snd_mute_put
,
326 static int tas_snd_mixer_info(struct snd_kcontrol
*kcontrol
,
327 struct snd_ctl_elem_info
*uinfo
)
329 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
331 uinfo
->value
.integer
.min
= 0;
332 uinfo
->value
.integer
.max
= 177;
336 static int tas_snd_mixer_get(struct snd_kcontrol
*kcontrol
,
337 struct snd_ctl_elem_value
*ucontrol
)
339 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
340 int idx
= kcontrol
->private_value
;
342 mutex_lock(&tas
->mtx
);
343 ucontrol
->value
.integer
.value
[0] = tas
->mixer_l
[idx
];
344 ucontrol
->value
.integer
.value
[1] = tas
->mixer_r
[idx
];
345 mutex_unlock(&tas
->mtx
);
350 static int tas_snd_mixer_put(struct snd_kcontrol
*kcontrol
,
351 struct snd_ctl_elem_value
*ucontrol
)
353 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
354 int idx
= kcontrol
->private_value
;
356 mutex_lock(&tas
->mtx
);
357 if (tas
->mixer_l
[idx
] == ucontrol
->value
.integer
.value
[0]
358 && tas
->mixer_r
[idx
] == ucontrol
->value
.integer
.value
[1]) {
359 mutex_unlock(&tas
->mtx
);
363 tas
->mixer_l
[idx
] = ucontrol
->value
.integer
.value
[0];
364 tas
->mixer_r
[idx
] = ucontrol
->value
.integer
.value
[1];
368 mutex_unlock(&tas
->mtx
);
372 #define MIXER_CONTROL(n,descr,idx) \
373 static struct snd_kcontrol_new n##_control = { \
374 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
375 .name = descr " Playback Volume", \
376 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \
377 .info = tas_snd_mixer_info, \
378 .get = tas_snd_mixer_get, \
379 .put = tas_snd_mixer_put, \
380 .private_value = idx, \
383 MIXER_CONTROL(pcm1
, "PCM", 0);
384 MIXER_CONTROL(monitor
, "Monitor", 2);
386 static int tas_snd_drc_range_info(struct snd_kcontrol
*kcontrol
,
387 struct snd_ctl_elem_info
*uinfo
)
389 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
391 uinfo
->value
.integer
.min
= 0;
392 uinfo
->value
.integer
.max
= TAS3004_DRC_MAX
;
396 static int tas_snd_drc_range_get(struct snd_kcontrol
*kcontrol
,
397 struct snd_ctl_elem_value
*ucontrol
)
399 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
401 mutex_lock(&tas
->mtx
);
402 ucontrol
->value
.integer
.value
[0] = tas
->drc_range
;
403 mutex_unlock(&tas
->mtx
);
407 static int tas_snd_drc_range_put(struct snd_kcontrol
*kcontrol
,
408 struct snd_ctl_elem_value
*ucontrol
)
410 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
412 if (ucontrol
->value
.integer
.value
[0] < 0 ||
413 ucontrol
->value
.integer
.value
[0] > TAS3004_DRC_MAX
)
416 mutex_lock(&tas
->mtx
);
417 if (tas
->drc_range
== ucontrol
->value
.integer
.value
[0]) {
418 mutex_unlock(&tas
->mtx
);
422 tas
->drc_range
= ucontrol
->value
.integer
.value
[0];
424 tas3004_set_drc(tas
);
425 mutex_unlock(&tas
->mtx
);
429 static struct snd_kcontrol_new drc_range_control
= {
430 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
432 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
433 .info
= tas_snd_drc_range_info
,
434 .get
= tas_snd_drc_range_get
,
435 .put
= tas_snd_drc_range_put
,
438 #define tas_snd_drc_switch_info snd_ctl_boolean_mono_info
440 static int tas_snd_drc_switch_get(struct snd_kcontrol
*kcontrol
,
441 struct snd_ctl_elem_value
*ucontrol
)
443 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
445 mutex_lock(&tas
->mtx
);
446 ucontrol
->value
.integer
.value
[0] = tas
->drc_enabled
;
447 mutex_unlock(&tas
->mtx
);
451 static int tas_snd_drc_switch_put(struct snd_kcontrol
*kcontrol
,
452 struct snd_ctl_elem_value
*ucontrol
)
454 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
456 mutex_lock(&tas
->mtx
);
457 if (tas
->drc_enabled
== ucontrol
->value
.integer
.value
[0]) {
458 mutex_unlock(&tas
->mtx
);
462 tas
->drc_enabled
= !!ucontrol
->value
.integer
.value
[0];
464 tas3004_set_drc(tas
);
465 mutex_unlock(&tas
->mtx
);
469 static struct snd_kcontrol_new drc_switch_control
= {
470 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
471 .name
= "DRC Range Switch",
472 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
473 .info
= tas_snd_drc_switch_info
,
474 .get
= tas_snd_drc_switch_get
,
475 .put
= tas_snd_drc_switch_put
,
478 static int tas_snd_capture_source_info(struct snd_kcontrol
*kcontrol
,
479 struct snd_ctl_elem_info
*uinfo
)
481 static char *texts
[] = { "Line-In", "Microphone" };
483 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
485 uinfo
->value
.enumerated
.items
= 2;
486 if (uinfo
->value
.enumerated
.item
> 1)
487 uinfo
->value
.enumerated
.item
= 1;
488 strcpy(uinfo
->value
.enumerated
.name
, texts
[uinfo
->value
.enumerated
.item
]);
492 static int tas_snd_capture_source_get(struct snd_kcontrol
*kcontrol
,
493 struct snd_ctl_elem_value
*ucontrol
)
495 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
497 mutex_lock(&tas
->mtx
);
498 ucontrol
->value
.enumerated
.item
[0] = !!(tas
->acr
& TAS_ACR_INPUT_B
);
499 mutex_unlock(&tas
->mtx
);
503 static int tas_snd_capture_source_put(struct snd_kcontrol
*kcontrol
,
504 struct snd_ctl_elem_value
*ucontrol
)
506 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
509 if (ucontrol
->value
.enumerated
.item
[0] > 1)
511 mutex_lock(&tas
->mtx
);
515 * Despite what the data sheet says in one place, the
516 * TAS_ACR_B_MONAUREAL bit forces mono output even when
517 * input A (line in) is selected.
519 tas
->acr
&= ~(TAS_ACR_INPUT_B
| TAS_ACR_B_MONAUREAL
);
520 if (ucontrol
->value
.enumerated
.item
[0])
521 tas
->acr
|= TAS_ACR_INPUT_B
| TAS_ACR_B_MONAUREAL
|
522 TAS_ACR_B_MON_SEL_RIGHT
;
523 if (oldacr
== tas
->acr
) {
524 mutex_unlock(&tas
->mtx
);
528 tas_write_reg(tas
, TAS_REG_ACR
, 1, &tas
->acr
);
529 mutex_unlock(&tas
->mtx
);
533 static struct snd_kcontrol_new capture_source_control
= {
534 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
535 /* If we name this 'Input Source', it properly shows up in
536 * alsamixer as a selection, * but it's shown under the
537 * 'Playback' category.
538 * If I name it 'Capture Source', it shows up in strange
539 * ways (two bools of which one can be selected at a
540 * time) but at least it's shown in the 'Capture'
542 * I was told that this was due to backward compatibility,
543 * but I don't understand then why the mangling is *not*
544 * done when I name it "Input Source".....
546 .name
= "Capture Source",
547 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
548 .info
= tas_snd_capture_source_info
,
549 .get
= tas_snd_capture_source_get
,
550 .put
= tas_snd_capture_source_put
,
553 static int tas_snd_treble_info(struct snd_kcontrol
*kcontrol
,
554 struct snd_ctl_elem_info
*uinfo
)
556 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
558 uinfo
->value
.integer
.min
= TAS3004_TREBLE_MIN
;
559 uinfo
->value
.integer
.max
= TAS3004_TREBLE_MAX
;
563 static int tas_snd_treble_get(struct snd_kcontrol
*kcontrol
,
564 struct snd_ctl_elem_value
*ucontrol
)
566 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
568 mutex_lock(&tas
->mtx
);
569 ucontrol
->value
.integer
.value
[0] = tas
->treble
;
570 mutex_unlock(&tas
->mtx
);
574 static int tas_snd_treble_put(struct snd_kcontrol
*kcontrol
,
575 struct snd_ctl_elem_value
*ucontrol
)
577 struct tas
*tas
= snd_kcontrol_chip(kcontrol
);
579 if (ucontrol
->value
.integer
.value
[0] < TAS3004_TREBLE_MIN
||
580 ucontrol
->value
.integer
.value
[0] > TAS3004_TREBLE_MAX
)
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 if (ucontrol
->value
.integer
.value
[0] < TAS3004_BASS_MIN
||
631 ucontrol
->value
.integer
.value
[0] > TAS3004_BASS_MAX
)
633 mutex_lock(&tas
->mtx
);
634 if (tas
->bass
== ucontrol
->value
.integer
.value
[0]) {
635 mutex_unlock(&tas
->mtx
);
639 tas
->bass
= ucontrol
->value
.integer
.value
[0];
642 mutex_unlock(&tas
->mtx
);
646 static struct snd_kcontrol_new bass_control
= {
647 .iface
= SNDRV_CTL_ELEM_IFACE_MIXER
,
649 .access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
,
650 .info
= tas_snd_bass_info
,
651 .get
= tas_snd_bass_get
,
652 .put
= tas_snd_bass_put
,
655 static struct transfer_info tas_transfers
[] = {
658 .formats
= SNDRV_PCM_FMTBIT_S16_BE
| SNDRV_PCM_FMTBIT_S24_BE
,
659 .rates
= SNDRV_PCM_RATE_32000
| SNDRV_PCM_RATE_44100
| SNDRV_PCM_RATE_48000
,
664 .formats
= SNDRV_PCM_FMTBIT_S16_BE
| SNDRV_PCM_FMTBIT_S24_BE
,
665 .rates
= SNDRV_PCM_RATE_32000
| SNDRV_PCM_RATE_44100
| SNDRV_PCM_RATE_48000
,
671 static int tas_usable(struct codec_info_item
*cii
,
672 struct transfer_info
*ti
,
673 struct transfer_info
*out
)
678 static int tas_reset_init(struct tas
*tas
)
682 tas
->codec
.gpio
->methods
->all_amps_off(tas
->codec
.gpio
);
684 tas
->codec
.gpio
->methods
->set_hw_reset(tas
->codec
.gpio
, 0);
686 tas
->codec
.gpio
->methods
->set_hw_reset(tas
->codec
.gpio
, 1);
688 tas
->codec
.gpio
->methods
->set_hw_reset(tas
->codec
.gpio
, 0);
690 tas
->codec
.gpio
->methods
->all_amps_restore(tas
->codec
.gpio
);
692 tmp
= TAS_MCS_SCLK64
| TAS_MCS_SPORT_MODE_I2S
| TAS_MCS_SPORT_WL_24BIT
;
693 if (tas_write_reg(tas
, TAS_REG_MCS
, 1, &tmp
))
696 tas
->acr
|= TAS_ACR_ANALOG_PDOWN
;
697 if (tas_write_reg(tas
, TAS_REG_ACR
, 1, &tas
->acr
))
701 if (tas_write_reg(tas
, TAS_REG_MCS2
, 1, &tmp
))
704 tas3004_set_drc(tas
);
706 /* Set treble & bass to 0dB */
707 tas
->treble
= TAS3004_TREBLE_ZERO
;
708 tas
->bass
= TAS3004_BASS_ZERO
;
712 tas
->acr
&= ~TAS_ACR_ANALOG_PDOWN
;
713 if (tas_write_reg(tas
, TAS_REG_ACR
, 1, &tas
->acr
))
721 static int tas_switch_clock(struct codec_info_item
*cii
, enum clock_switch clock
)
723 struct tas
*tas
= cii
->codec_data
;
726 case CLOCK_SWITCH_PREPARE_SLAVE
:
727 /* Clocks are going away, mute mute mute */
728 tas
->codec
.gpio
->methods
->all_amps_off(tas
->codec
.gpio
);
731 case CLOCK_SWITCH_SLAVE
:
732 /* Clocks are back, re-init the codec */
733 mutex_lock(&tas
->mtx
);
738 tas
->codec
.gpio
->methods
->all_amps_restore(tas
->codec
.gpio
);
739 mutex_unlock(&tas
->mtx
);
742 /* doesn't happen as of now */
749 /* we are controlled via i2c and assume that is always up
750 * If that wasn't the case, we'd have to suspend once
751 * our i2c device is suspended, and then take note of that! */
752 static int tas_suspend(struct tas
*tas
)
754 mutex_lock(&tas
->mtx
);
756 tas
->acr
|= TAS_ACR_ANALOG_PDOWN
;
757 tas_write_reg(tas
, TAS_REG_ACR
, 1, &tas
->acr
);
758 mutex_unlock(&tas
->mtx
);
762 static int tas_resume(struct tas
*tas
)
765 mutex_lock(&tas
->mtx
);
770 mutex_unlock(&tas
->mtx
);
774 static int _tas_suspend(struct codec_info_item
*cii
, pm_message_t state
)
776 return tas_suspend(cii
->codec_data
);
779 static int _tas_resume(struct codec_info_item
*cii
)
781 return tas_resume(cii
->codec_data
);
783 #else /* CONFIG_PM */
784 #define _tas_suspend NULL
785 #define _tas_resume NULL
786 #endif /* CONFIG_PM */
788 static struct codec_info tas_codec_info
= {
789 .transfers
= tas_transfers
,
790 /* in theory, we can drive it at 512 too...
791 * but so far the framework doesn't allow
792 * for that and I don't see much point in it. */
793 .sysclock_factor
= 256,
794 /* same here, could be 32 for just one 16 bit format */
796 .owner
= THIS_MODULE
,
797 .usable
= tas_usable
,
798 .switch_clock
= tas_switch_clock
,
799 .suspend
= _tas_suspend
,
800 .resume
= _tas_resume
,
803 static int tas_init_codec(struct aoa_codec
*codec
)
805 struct tas
*tas
= codec_to_tas(codec
);
808 if (!tas
->codec
.gpio
|| !tas
->codec
.gpio
->methods
) {
809 printk(KERN_ERR PFX
"gpios not assigned!!\n");
813 mutex_lock(&tas
->mtx
);
814 if (tas_reset_init(tas
)) {
815 printk(KERN_ERR PFX
"tas failed to initialise\n");
816 mutex_unlock(&tas
->mtx
);
820 mutex_unlock(&tas
->mtx
);
822 if (tas
->codec
.soundbus_dev
->attach_codec(tas
->codec
.soundbus_dev
,
824 &tas_codec_info
, tas
)) {
825 printk(KERN_ERR PFX
"error attaching tas to soundbus\n");
829 if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL
, tas
, &ops
)) {
830 printk(KERN_ERR PFX
"failed to create tas snd device!\n");
833 err
= aoa_snd_ctl_add(snd_ctl_new1(&volume_control
, tas
));
837 err
= aoa_snd_ctl_add(snd_ctl_new1(&mute_control
, tas
));
841 err
= aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control
, tas
));
845 err
= aoa_snd_ctl_add(snd_ctl_new1(&monitor_control
, tas
));
849 err
= aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control
, tas
));
853 err
= aoa_snd_ctl_add(snd_ctl_new1(&drc_range_control
, tas
));
857 err
= aoa_snd_ctl_add(snd_ctl_new1(&drc_switch_control
, tas
));
861 err
= aoa_snd_ctl_add(snd_ctl_new1(&treble_control
, tas
));
865 err
= aoa_snd_ctl_add(snd_ctl_new1(&bass_control
, tas
));
871 tas
->codec
.soundbus_dev
->detach_codec(tas
->codec
.soundbus_dev
, tas
);
872 snd_device_free(aoa_get_card(), tas
);
876 static void tas_exit_codec(struct aoa_codec
*codec
)
878 struct tas
*tas
= codec_to_tas(codec
);
880 if (!tas
->codec
.soundbus_dev
)
882 tas
->codec
.soundbus_dev
->detach_codec(tas
->codec
.soundbus_dev
, tas
);
886 static int tas_i2c_probe(struct i2c_client
*client
,
887 const struct i2c_device_id
*id
)
889 struct device_node
*node
= client
->dev
.of_node
;
892 tas
= kzalloc(sizeof(struct tas
), GFP_KERNEL
);
897 mutex_init(&tas
->mtx
);
899 i2c_set_clientdata(client
, tas
);
901 /* seems that half is a saner default */
902 tas
->drc_range
= TAS3004_DRC_MAX
/ 2;
904 strlcpy(tas
->codec
.name
, "tas", MAX_CODEC_NAME_LEN
);
905 tas
->codec
.owner
= THIS_MODULE
;
906 tas
->codec
.init
= tas_init_codec
;
907 tas
->codec
.exit
= tas_exit_codec
;
908 tas
->codec
.node
= of_node_get(node
);
910 if (aoa_codec_register(&tas
->codec
)) {
914 "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n",
915 (unsigned int)client
->addr
, node
->full_name
);
918 mutex_destroy(&tas
->mtx
);
923 static int tas_i2c_remove(struct i2c_client
*client
)
925 struct tas
*tas
= i2c_get_clientdata(client
);
926 u8 tmp
= TAS_ACR_ANALOG_PDOWN
;
928 aoa_codec_unregister(&tas
->codec
);
929 of_node_put(tas
->codec
.node
);
931 /* power down codec chip */
932 tas_write_reg(tas
, TAS_REG_ACR
, 1, &tmp
);
934 mutex_destroy(&tas
->mtx
);
939 static const struct i2c_device_id tas_i2c_id
[] = {
940 { "MAC,tas3004", 0 },
943 MODULE_DEVICE_TABLE(i2c
,tas_i2c_id
);
945 static struct i2c_driver tas_driver
= {
947 .name
= "aoa_codec_tas",
948 .owner
= THIS_MODULE
,
950 .probe
= tas_i2c_probe
,
951 .remove
= tas_i2c_remove
,
952 .id_table
= tas_i2c_id
,
955 module_i2c_driver(tas_driver
);