blob: 7a16a3331f7e9efc6f12918cdb97f82e3d833b14 [file] [log] [blame]
Johannes Bergf3d94782006-06-21 15:42:43 +02001/*
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>
Johannes Bergf3d94782006-06-21 15:42:43 +020064#include <asm/pmac_low_i2c.h>
65#include <asm/prom.h>
66#include <linux/delay.h>
67#include <linux/module.h>
Johannes Berg30719202006-09-17 21:59:25 +020068#include <linux/mutex.h>
69
Johannes Bergf3d94782006-06-21 15:42:43 +020070MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
71MODULE_LICENSE("GPL");
72MODULE_DESCRIPTION("tas codec driver for snd-aoa");
73
74#include "snd-aoa-codec-tas.h"
75#include "snd-aoa-codec-tas-gain-table.h"
Johannes Berg50099322006-07-10 04:44:41 -070076#include "snd-aoa-codec-tas-basstreble.h"
Johannes Bergf3d94782006-06-21 15:42:43 +020077#include "../aoa.h"
78#include "../soundbus/soundbus.h"
79
Johannes Bergf3d94782006-06-21 15:42:43 +020080#define PFX "snd-aoa-codec-tas: "
81
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -070082
Johannes Bergf3d94782006-06-21 15:42:43 +020083struct tas {
84 struct aoa_codec codec;
85 struct i2c_client i2c;
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -070086 u32 mute_l:1, mute_r:1 ,
87 controls_created:1 ,
88 drc_enabled:1,
89 hw_enabled:1;
Johannes Bergf3d94782006-06-21 15:42:43 +020090 u8 cached_volume_l, cached_volume_r;
91 u8 mixer_l[3], mixer_r[3];
Johannes Berg50099322006-07-10 04:44:41 -070092 u8 bass, treble;
Johannes Bergf3d94782006-06-21 15:42:43 +020093 u8 acr;
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -070094 int drc_range;
Johannes Berg30719202006-09-17 21:59:25 +020095 /* protects hardware access against concurrency from
96 * userspace when hitting controls and during
97 * codec init/suspend/resume */
98 struct mutex mtx;
Johannes Bergf3d94782006-06-21 15:42:43 +020099};
100
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700101static int tas_reset_init(struct tas *tas);
102
Johannes Bergf3d94782006-06-21 15:42:43 +0200103static struct tas *codec_to_tas(struct aoa_codec *codec)
104{
105 return container_of(codec, struct tas, codec);
106}
107
108static 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
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700116static 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
Johannes Berg50099322006-07-10 04:44:41 -0700138static 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
146static 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
Johannes Bergf3d94782006-06-21 15:42:43 +0200154static 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
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700166 if (tas->mute_l) left = 0;
167 if (tas->mute_r) right = 0;
Johannes Bergf3d94782006-06-21 15:42:43 +0200168
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
186static 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
215static int tas_dev_register(struct snd_device *dev)
216{
217 return 0;
218}
219
220static struct snd_device_ops ops = {
221 .dev_register = tas_dev_register,
222};
223
224static 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
234static 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
Johannes Berg30719202006-09-17 21:59:25 +0200239 mutex_lock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200240 ucontrol->value.integer.value[0] = tas->cached_volume_l;
241 ucontrol->value.integer.value[1] = tas->cached_volume_r;
Johannes Berg30719202006-09-17 21:59:25 +0200242 mutex_unlock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200243 return 0;
244}
245
246static 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
Takashi Iwai498ade12007-11-15 16:16:32 +0100251 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;
257
Johannes Berg30719202006-09-17 21:59:25 +0200258 mutex_lock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200259 if (tas->cached_volume_l == ucontrol->value.integer.value[0]
Johannes Berg30719202006-09-17 21:59:25 +0200260 && tas->cached_volume_r == ucontrol->value.integer.value[1]) {
261 mutex_unlock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200262 return 0;
Johannes Berg30719202006-09-17 21:59:25 +0200263 }
Johannes Bergf3d94782006-06-21 15:42:43 +0200264
265 tas->cached_volume_l = ucontrol->value.integer.value[0];
266 tas->cached_volume_r = ucontrol->value.integer.value[1];
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700267 if (tas->hw_enabled)
268 tas_set_volume(tas);
Johannes Berg30719202006-09-17 21:59:25 +0200269 mutex_unlock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200270 return 1;
271}
272
273static 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,
280};
281
Takashi Iwaia5ce8892007-07-23 15:42:26 +0200282#define tas_snd_mute_info snd_ctl_boolean_stereo_info
Johannes Bergf3d94782006-06-21 15:42:43 +0200283
284static int tas_snd_mute_get(struct snd_kcontrol *kcontrol,
285 struct snd_ctl_elem_value *ucontrol)
286{
287 struct tas *tas = snd_kcontrol_chip(kcontrol);
288
Johannes Berg30719202006-09-17 21:59:25 +0200289 mutex_lock(&tas->mtx);
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700290 ucontrol->value.integer.value[0] = !tas->mute_l;
291 ucontrol->value.integer.value[1] = !tas->mute_r;
Johannes Berg30719202006-09-17 21:59:25 +0200292 mutex_unlock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200293 return 0;
294}
295
296static int tas_snd_mute_put(struct snd_kcontrol *kcontrol,
297 struct snd_ctl_elem_value *ucontrol)
298{
299 struct tas *tas = snd_kcontrol_chip(kcontrol);
300
Johannes Berg30719202006-09-17 21:59:25 +0200301 mutex_lock(&tas->mtx);
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700302 if (tas->mute_l == !ucontrol->value.integer.value[0]
Johannes Berg30719202006-09-17 21:59:25 +0200303 && tas->mute_r == !ucontrol->value.integer.value[1]) {
304 mutex_unlock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200305 return 0;
Johannes Berg30719202006-09-17 21:59:25 +0200306 }
Johannes Bergf3d94782006-06-21 15:42:43 +0200307
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700308 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);
Johannes Berg30719202006-09-17 21:59:25 +0200312 mutex_unlock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200313 return 1;
314}
315
316static 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,
323};
324
325static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol,
326 struct snd_ctl_elem_info *uinfo)
327{
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;
333}
334
335static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol,
336 struct snd_ctl_elem_value *ucontrol)
337{
338 struct tas *tas = snd_kcontrol_chip(kcontrol);
339 int idx = kcontrol->private_value;
340
Johannes Berg30719202006-09-17 21:59:25 +0200341 mutex_lock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200342 ucontrol->value.integer.value[0] = tas->mixer_l[idx];
343 ucontrol->value.integer.value[1] = tas->mixer_r[idx];
Johannes Berg30719202006-09-17 21:59:25 +0200344 mutex_unlock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200345
346 return 0;
347}
348
349static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol,
350 struct snd_ctl_elem_value *ucontrol)
351{
352 struct tas *tas = snd_kcontrol_chip(kcontrol);
353 int idx = kcontrol->private_value;
354
Johannes Berg30719202006-09-17 21:59:25 +0200355 mutex_lock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200356 if (tas->mixer_l[idx] == ucontrol->value.integer.value[0]
Johannes Berg30719202006-09-17 21:59:25 +0200357 && tas->mixer_r[idx] == ucontrol->value.integer.value[1]) {
358 mutex_unlock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200359 return 0;
Johannes Berg30719202006-09-17 21:59:25 +0200360 }
Johannes Bergf3d94782006-06-21 15:42:43 +0200361
362 tas->mixer_l[idx] = ucontrol->value.integer.value[0];
363 tas->mixer_r[idx] = ucontrol->value.integer.value[1];
364
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700365 if (tas->hw_enabled)
366 tas_set_mixer(tas);
Johannes Berg30719202006-09-17 21:59:25 +0200367 mutex_unlock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200368 return 1;
369}
370
371#define MIXER_CONTROL(n,descr,idx) \
372static 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, \
380}
381
Johannes Berg14b42962006-07-10 04:44:38 -0700382MIXER_CONTROL(pcm1, "PCM", 0);
Johannes Bergf3d94782006-06-21 15:42:43 +0200383MIXER_CONTROL(monitor, "Monitor", 2);
384
Johannes Berg9b8f52f2006-07-10 04:44:39 -0700385static int tas_snd_drc_range_info(struct snd_kcontrol *kcontrol,
386 struct snd_ctl_elem_info *uinfo)
387{
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;
393}
394
395static int tas_snd_drc_range_get(struct snd_kcontrol *kcontrol,
396 struct snd_ctl_elem_value *ucontrol)
397{
398 struct tas *tas = snd_kcontrol_chip(kcontrol);
399
Johannes Berg30719202006-09-17 21:59:25 +0200400 mutex_lock(&tas->mtx);
Johannes Berg9b8f52f2006-07-10 04:44:39 -0700401 ucontrol->value.integer.value[0] = tas->drc_range;
Johannes Berg30719202006-09-17 21:59:25 +0200402 mutex_unlock(&tas->mtx);
Johannes Berg9b8f52f2006-07-10 04:44:39 -0700403 return 0;
404}
405
406static int tas_snd_drc_range_put(struct snd_kcontrol *kcontrol,
407 struct snd_ctl_elem_value *ucontrol)
408{
409 struct tas *tas = snd_kcontrol_chip(kcontrol);
410
Takashi Iwai498ade12007-11-15 16:16:32 +0100411 if (ucontrol->value.integer.value[0] < 0 ||
412 ucontrol->value.integer.value[0] > TAS3004_DRC_MAX)
413 return -EINVAL;
414
Johannes Berg30719202006-09-17 21:59:25 +0200415 mutex_lock(&tas->mtx);
416 if (tas->drc_range == ucontrol->value.integer.value[0]) {
417 mutex_unlock(&tas->mtx);
Johannes Berg9b8f52f2006-07-10 04:44:39 -0700418 return 0;
Johannes Berg30719202006-09-17 21:59:25 +0200419 }
Johannes Berg9b8f52f2006-07-10 04:44:39 -0700420
421 tas->drc_range = ucontrol->value.integer.value[0];
422 if (tas->hw_enabled)
423 tas3004_set_drc(tas);
Johannes Berg30719202006-09-17 21:59:25 +0200424 mutex_unlock(&tas->mtx);
Johannes Berg9b8f52f2006-07-10 04:44:39 -0700425 return 1;
426}
427
428static 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,
435};
436
Takashi Iwaia5ce8892007-07-23 15:42:26 +0200437#define tas_snd_drc_switch_info snd_ctl_boolean_mono_info
Johannes Berg9b8f52f2006-07-10 04:44:39 -0700438
439static int tas_snd_drc_switch_get(struct snd_kcontrol *kcontrol,
440 struct snd_ctl_elem_value *ucontrol)
441{
442 struct tas *tas = snd_kcontrol_chip(kcontrol);
443
Johannes Berg30719202006-09-17 21:59:25 +0200444 mutex_lock(&tas->mtx);
Johannes Berg9b8f52f2006-07-10 04:44:39 -0700445 ucontrol->value.integer.value[0] = tas->drc_enabled;
Johannes Berg30719202006-09-17 21:59:25 +0200446 mutex_unlock(&tas->mtx);
Johannes Berg9b8f52f2006-07-10 04:44:39 -0700447 return 0;
448}
449
450static int tas_snd_drc_switch_put(struct snd_kcontrol *kcontrol,
451 struct snd_ctl_elem_value *ucontrol)
452{
453 struct tas *tas = snd_kcontrol_chip(kcontrol);
454
Johannes Berg30719202006-09-17 21:59:25 +0200455 mutex_lock(&tas->mtx);
456 if (tas->drc_enabled == ucontrol->value.integer.value[0]) {
457 mutex_unlock(&tas->mtx);
Johannes Berg9b8f52f2006-07-10 04:44:39 -0700458 return 0;
Johannes Berg30719202006-09-17 21:59:25 +0200459 }
Johannes Berg9b8f52f2006-07-10 04:44:39 -0700460
Takashi Iwai498ade12007-11-15 16:16:32 +0100461 tas->drc_enabled = !!ucontrol->value.integer.value[0];
Johannes Berg9b8f52f2006-07-10 04:44:39 -0700462 if (tas->hw_enabled)
463 tas3004_set_drc(tas);
Johannes Berg30719202006-09-17 21:59:25 +0200464 mutex_unlock(&tas->mtx);
Johannes Berg9b8f52f2006-07-10 04:44:39 -0700465 return 1;
466}
467
468static 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,
475};
476
Johannes Bergf3d94782006-06-21 15:42:43 +0200477static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol,
478 struct snd_ctl_elem_info *uinfo)
479{
480 static char *texts[] = { "Line-In", "Microphone" };
481
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;
489}
490
491static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
492 struct snd_ctl_elem_value *ucontrol)
493{
494 struct tas *tas = snd_kcontrol_chip(kcontrol);
495
Johannes Berg30719202006-09-17 21:59:25 +0200496 mutex_lock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200497 ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
Johannes Berg30719202006-09-17 21:59:25 +0200498 mutex_unlock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200499 return 0;
500}
501
502static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
503 struct snd_ctl_elem_value *ucontrol)
504{
505 struct tas *tas = snd_kcontrol_chip(kcontrol);
Johannes Berg30719202006-09-17 21:59:25 +0200506 int oldacr;
507
Takashi Iwai498ade12007-11-15 16:16:32 +0100508 if (ucontrol->value.enumerated.item[0] > 1)
509 return -EINVAL;
Johannes Berg30719202006-09-17 21:59:25 +0200510 mutex_lock(&tas->mtx);
511 oldacr = tas->acr;
Johannes Bergf3d94782006-06-21 15:42:43 +0200512
Paul Mackerras80b8d5d2006-10-31 15:24:45 +0100513 /*
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.
517 */
518 tas->acr &= ~(TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL);
Johannes Bergf3d94782006-06-21 15:42:43 +0200519 if (ucontrol->value.enumerated.item[0])
Paul Mackerras80b8d5d2006-10-31 15:24:45 +0100520 tas->acr |= TAS_ACR_INPUT_B | TAS_ACR_B_MONAUREAL |
521 TAS_ACR_B_MON_SEL_RIGHT;
Johannes Berg30719202006-09-17 21:59:25 +0200522 if (oldacr == tas->acr) {
523 mutex_unlock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200524 return 0;
Johannes Berg30719202006-09-17 21:59:25 +0200525 }
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700526 if (tas->hw_enabled)
527 tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
Johannes Berg30719202006-09-17 21:59:25 +0200528 mutex_unlock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200529 return 1;
530}
531
532static 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".....
544 */
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,
550};
551
Johannes Berg50099322006-07-10 04:44:41 -0700552static int tas_snd_treble_info(struct snd_kcontrol *kcontrol,
553 struct snd_ctl_elem_info *uinfo)
554{
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;
560}
561
562static int tas_snd_treble_get(struct snd_kcontrol *kcontrol,
563 struct snd_ctl_elem_value *ucontrol)
564{
565 struct tas *tas = snd_kcontrol_chip(kcontrol);
566
Johannes Berg30719202006-09-17 21:59:25 +0200567 mutex_lock(&tas->mtx);
Johannes Berg50099322006-07-10 04:44:41 -0700568 ucontrol->value.integer.value[0] = tas->treble;
Johannes Berg30719202006-09-17 21:59:25 +0200569 mutex_unlock(&tas->mtx);
Johannes Berg50099322006-07-10 04:44:41 -0700570 return 0;
571}
572
573static int tas_snd_treble_put(struct snd_kcontrol *kcontrol,
574 struct snd_ctl_elem_value *ucontrol)
575{
576 struct tas *tas = snd_kcontrol_chip(kcontrol);
577
Takashi Iwai498ade12007-11-15 16:16:32 +0100578 if (ucontrol->value.integer.value[0] < TAS3004_TREBLE_MIN ||
579 ucontrol->value.integer.value[0] > TAS3004_TREBLE_MAX)
580 return -EINVAL;
Johannes Berg30719202006-09-17 21:59:25 +0200581 mutex_lock(&tas->mtx);
582 if (tas->treble == ucontrol->value.integer.value[0]) {
583 mutex_unlock(&tas->mtx);
Johannes Berg50099322006-07-10 04:44:41 -0700584 return 0;
Johannes Berg30719202006-09-17 21:59:25 +0200585 }
Johannes Berg50099322006-07-10 04:44:41 -0700586
587 tas->treble = ucontrol->value.integer.value[0];
588 if (tas->hw_enabled)
589 tas_set_treble(tas);
Johannes Berg30719202006-09-17 21:59:25 +0200590 mutex_unlock(&tas->mtx);
Johannes Berg50099322006-07-10 04:44:41 -0700591 return 1;
592}
593
594static 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,
601};
602
603static int tas_snd_bass_info(struct snd_kcontrol *kcontrol,
604 struct snd_ctl_elem_info *uinfo)
605{
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;
611}
612
613static int tas_snd_bass_get(struct snd_kcontrol *kcontrol,
614 struct snd_ctl_elem_value *ucontrol)
615{
616 struct tas *tas = snd_kcontrol_chip(kcontrol);
617
Johannes Berg30719202006-09-17 21:59:25 +0200618 mutex_lock(&tas->mtx);
Johannes Berg50099322006-07-10 04:44:41 -0700619 ucontrol->value.integer.value[0] = tas->bass;
Johannes Berg30719202006-09-17 21:59:25 +0200620 mutex_unlock(&tas->mtx);
Johannes Berg50099322006-07-10 04:44:41 -0700621 return 0;
622}
623
624static int tas_snd_bass_put(struct snd_kcontrol *kcontrol,
625 struct snd_ctl_elem_value *ucontrol)
626{
627 struct tas *tas = snd_kcontrol_chip(kcontrol);
628
Takashi Iwai498ade12007-11-15 16:16:32 +0100629 if (ucontrol->value.integer.value[0] < TAS3004_BASS_MIN ||
630 ucontrol->value.integer.value[0] > TAS3004_BASS_MAX)
631 return -EINVAL;
Johannes Berg30719202006-09-17 21:59:25 +0200632 mutex_lock(&tas->mtx);
633 if (tas->bass == ucontrol->value.integer.value[0]) {
634 mutex_unlock(&tas->mtx);
Johannes Berg50099322006-07-10 04:44:41 -0700635 return 0;
Johannes Berg30719202006-09-17 21:59:25 +0200636 }
Johannes Berg50099322006-07-10 04:44:41 -0700637
638 tas->bass = ucontrol->value.integer.value[0];
639 if (tas->hw_enabled)
640 tas_set_bass(tas);
Johannes Berg30719202006-09-17 21:59:25 +0200641 mutex_unlock(&tas->mtx);
Johannes Berg50099322006-07-10 04:44:41 -0700642 return 1;
643}
644
645static 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,
652};
Johannes Bergf3d94782006-06-21 15:42:43 +0200653
654static struct transfer_info tas_transfers[] = {
655 {
656 /* input */
657 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
658 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE,
659 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
660 .transfer_in = 1,
661 },
662 {
663 /* output */
664 .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE |
665 SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE,
666 .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
667 .transfer_in = 0,
668 },
669 {}
670};
671
672static int tas_usable(struct codec_info_item *cii,
673 struct transfer_info *ti,
674 struct transfer_info *out)
675{
676 return 1;
677}
678
679static int tas_reset_init(struct tas *tas)
680{
681 u8 tmp;
Johannes Bergf3d94782006-06-21 15:42:43 +0200682
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700683 tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
684 msleep(5);
685 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
686 msleep(5);
687 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1);
688 msleep(20);
689 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
690 msleep(10);
691 tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
Johannes Bergf3d94782006-06-21 15:42:43 +0200692
693 tmp = TAS_MCS_SCLK64 | TAS_MCS_SPORT_MODE_I2S | TAS_MCS_SPORT_WL_24BIT;
694 if (tas_write_reg(tas, TAS_REG_MCS, 1, &tmp))
Johannes Berg30719202006-09-17 21:59:25 +0200695 goto outerr;
Johannes Bergf3d94782006-06-21 15:42:43 +0200696
Paul Mackerras80b8d5d2006-10-31 15:24:45 +0100697 tas->acr |= TAS_ACR_ANALOG_PDOWN;
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700698 if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
Johannes Berg30719202006-09-17 21:59:25 +0200699 goto outerr;
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700700
Johannes Bergf3d94782006-06-21 15:42:43 +0200701 tmp = 0;
702 if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp))
Johannes Berg30719202006-09-17 21:59:25 +0200703 goto outerr;
Johannes Bergf3d94782006-06-21 15:42:43 +0200704
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700705 tas3004_set_drc(tas);
706
707 /* Set treble & bass to 0dB */
Johannes Berg50099322006-07-10 04:44:41 -0700708 tas->treble = TAS3004_TREBLE_ZERO;
709 tas->bass = TAS3004_BASS_ZERO;
710 tas_set_treble(tas);
711 tas_set_bass(tas);
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700712
713 tas->acr &= ~TAS_ACR_ANALOG_PDOWN;
714 if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
Johannes Berg30719202006-09-17 21:59:25 +0200715 goto outerr;
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700716
717 return 0;
Johannes Berg30719202006-09-17 21:59:25 +0200718 outerr:
719 return -ENODEV;
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700720}
721
722static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock)
723{
724 struct tas *tas = cii->codec_data;
725
726 switch(clock) {
727 case CLOCK_SWITCH_PREPARE_SLAVE:
728 /* Clocks are going away, mute mute mute */
729 tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
730 tas->hw_enabled = 0;
731 break;
732 case CLOCK_SWITCH_SLAVE:
733 /* Clocks are back, re-init the codec */
Johannes Berg30719202006-09-17 21:59:25 +0200734 mutex_lock(&tas->mtx);
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700735 tas_reset_init(tas);
736 tas_set_volume(tas);
737 tas_set_mixer(tas);
738 tas->hw_enabled = 1;
739 tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
Johannes Berg30719202006-09-17 21:59:25 +0200740 mutex_unlock(&tas->mtx);
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700741 break;
742 default:
743 /* doesn't happen as of now */
744 return -EINVAL;
745 }
Johannes Bergf3d94782006-06-21 15:42:43 +0200746 return 0;
747}
748
Stephen Rothwell1f289602007-07-23 12:10:07 +0200749#ifdef CONFIG_PM
Johannes Bergf3d94782006-06-21 15:42:43 +0200750/* we are controlled via i2c and assume that is always up
751 * If that wasn't the case, we'd have to suspend once
752 * our i2c device is suspended, and then take note of that! */
753static int tas_suspend(struct tas *tas)
754{
Johannes Berg30719202006-09-17 21:59:25 +0200755 mutex_lock(&tas->mtx);
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700756 tas->hw_enabled = 0;
Johannes Bergf3d94782006-06-21 15:42:43 +0200757 tas->acr |= TAS_ACR_ANALOG_PDOWN;
758 tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
Johannes Berg30719202006-09-17 21:59:25 +0200759 mutex_unlock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200760 return 0;
761}
762
763static int tas_resume(struct tas *tas)
764{
765 /* reset codec */
Johannes Berg30719202006-09-17 21:59:25 +0200766 mutex_lock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200767 tas_reset_init(tas);
768 tas_set_volume(tas);
769 tas_set_mixer(tas);
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700770 tas->hw_enabled = 1;
Johannes Berg30719202006-09-17 21:59:25 +0200771 mutex_unlock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200772 return 0;
773}
774
Johannes Bergf3d94782006-06-21 15:42:43 +0200775static int _tas_suspend(struct codec_info_item *cii, pm_message_t state)
776{
777 return tas_suspend(cii->codec_data);
778}
779
780static int _tas_resume(struct codec_info_item *cii)
781{
782 return tas_resume(cii->codec_data);
783}
Stephen Rothwell1f289602007-07-23 12:10:07 +0200784#else /* CONFIG_PM */
785#define _tas_suspend NULL
786#define _tas_resume NULL
787#endif /* CONFIG_PM */
Johannes Bergf3d94782006-06-21 15:42:43 +0200788
789static struct codec_info tas_codec_info = {
790 .transfers = tas_transfers,
791 /* in theory, we can drive it at 512 too...
792 * but so far the framework doesn't allow
793 * for that and I don't see much point in it. */
794 .sysclock_factor = 256,
795 /* same here, could be 32 for just one 16 bit format */
796 .bus_factor = 64,
797 .owner = THIS_MODULE,
798 .usable = tas_usable,
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700799 .switch_clock = tas_switch_clock,
Johannes Bergf3d94782006-06-21 15:42:43 +0200800 .suspend = _tas_suspend,
801 .resume = _tas_resume,
Johannes Bergf3d94782006-06-21 15:42:43 +0200802};
803
804static int tas_init_codec(struct aoa_codec *codec)
805{
806 struct tas *tas = codec_to_tas(codec);
807 int err;
808
809 if (!tas->codec.gpio || !tas->codec.gpio->methods) {
810 printk(KERN_ERR PFX "gpios not assigned!!\n");
811 return -EINVAL;
812 }
813
Johannes Berg30719202006-09-17 21:59:25 +0200814 mutex_lock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200815 if (tas_reset_init(tas)) {
816 printk(KERN_ERR PFX "tas failed to initialise\n");
Johannes Berg30719202006-09-17 21:59:25 +0200817 mutex_unlock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200818 return -ENXIO;
819 }
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700820 tas->hw_enabled = 1;
Johannes Berg30719202006-09-17 21:59:25 +0200821 mutex_unlock(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200822
823 if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev,
824 aoa_get_card(),
825 &tas_codec_info, tas)) {
826 printk(KERN_ERR PFX "error attaching tas to soundbus\n");
827 return -ENODEV;
828 }
829
830 if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, tas, &ops)) {
831 printk(KERN_ERR PFX "failed to create tas snd device!\n");
832 return -ENODEV;
833 }
834 err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas));
835 if (err)
836 goto error;
837
838 err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas));
839 if (err)
840 goto error;
841
842 err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas));
843 if (err)
844 goto error;
845
846 err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas));
847 if (err)
848 goto error;
849
850 err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas));
851 if (err)
852 goto error;
853
Johannes Berg9b8f52f2006-07-10 04:44:39 -0700854 err = aoa_snd_ctl_add(snd_ctl_new1(&drc_range_control, tas));
855 if (err)
856 goto error;
857
858 err = aoa_snd_ctl_add(snd_ctl_new1(&drc_switch_control, tas));
859 if (err)
860 goto error;
861
Johannes Berg50099322006-07-10 04:44:41 -0700862 err = aoa_snd_ctl_add(snd_ctl_new1(&treble_control, tas));
863 if (err)
864 goto error;
865
866 err = aoa_snd_ctl_add(snd_ctl_new1(&bass_control, tas));
867 if (err)
868 goto error;
869
Johannes Bergf3d94782006-06-21 15:42:43 +0200870 return 0;
871 error:
872 tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
873 snd_device_free(aoa_get_card(), tas);
874 return err;
875}
876
877static void tas_exit_codec(struct aoa_codec *codec)
878{
879 struct tas *tas = codec_to_tas(codec);
880
881 if (!tas->codec.soundbus_dev)
882 return;
883 tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
884}
885
886
887static struct i2c_driver tas_driver;
888
889static int tas_create(struct i2c_adapter *adapter,
890 struct device_node *node,
891 int addr)
892{
893 struct tas *tas;
894
895 tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
896
897 if (!tas)
898 return -ENOMEM;
899
Johannes Berg30719202006-09-17 21:59:25 +0200900 mutex_init(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200901 tas->i2c.driver = &tas_driver;
902 tas->i2c.adapter = adapter;
903 tas->i2c.addr = addr;
Johannes Berg9b8f52f2006-07-10 04:44:39 -0700904 /* seems that half is a saner default */
905 tas->drc_range = TAS3004_DRC_MAX / 2;
Jean Delvare023ff3e2007-03-27 11:50:19 +0200906 strlcpy(tas->i2c.name, "tas audio codec", I2C_NAME_SIZE);
Johannes Bergf3d94782006-06-21 15:42:43 +0200907
908 if (i2c_attach_client(&tas->i2c)) {
909 printk(KERN_ERR PFX "failed to attach to i2c\n");
910 goto fail;
911 }
912
Jean Delvare023ff3e2007-03-27 11:50:19 +0200913 strlcpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN);
Johannes Bergf3d94782006-06-21 15:42:43 +0200914 tas->codec.owner = THIS_MODULE;
915 tas->codec.init = tas_init_codec;
916 tas->codec.exit = tas_exit_codec;
917 tas->codec.node = of_node_get(node);
918
919 if (aoa_codec_register(&tas->codec)) {
920 goto detach;
921 }
Benjamin Herrenschmidt6a4f5782006-07-10 04:44:39 -0700922 printk(KERN_DEBUG
923 "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n",
924 addr, node->full_name);
Johannes Bergf3d94782006-06-21 15:42:43 +0200925 return 0;
926 detach:
927 i2c_detach_client(&tas->i2c);
928 fail:
Johannes Berg30719202006-09-17 21:59:25 +0200929 mutex_destroy(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200930 kfree(tas);
931 return -EINVAL;
932}
933
934static int tas_i2c_attach(struct i2c_adapter *adapter)
935{
936 struct device_node *busnode, *dev = NULL;
937 struct pmac_i2c_bus *bus;
938
939 bus = pmac_i2c_adapter_to_bus(adapter);
940 if (bus == NULL)
941 return -ENODEV;
942 busnode = pmac_i2c_get_bus_node(bus);
943
944 while ((dev = of_get_next_child(busnode, dev)) != NULL) {
Stephen Rothwell55b61fe2007-05-03 17:26:52 +1000945 if (of_device_is_compatible(dev, "tas3004")) {
Stephen Rothwella7edd0e2007-04-03 10:52:17 +1000946 const u32 *addr;
Johannes Bergf3d94782006-06-21 15:42:43 +0200947 printk(KERN_DEBUG PFX "found tas3004\n");
Stephen Rothwellc4f55b32007-04-03 22:39:14 +1000948 addr = of_get_property(dev, "reg", NULL);
Johannes Bergf3d94782006-06-21 15:42:43 +0200949 if (!addr)
950 continue;
951 return tas_create(adapter, dev, ((*addr) >> 1) & 0x7f);
952 }
953 /* older machines have no 'codec' node with a 'compatible'
954 * property that says 'tas3004', they just have a 'deq'
955 * node without any such property... */
956 if (strcmp(dev->name, "deq") == 0) {
Stephen Rothwella7edd0e2007-04-03 10:52:17 +1000957 const u32 *_addr;
958 u32 addr;
Johannes Bergf3d94782006-06-21 15:42:43 +0200959 printk(KERN_DEBUG PFX "found 'deq' node\n");
Stephen Rothwellc4f55b32007-04-03 22:39:14 +1000960 _addr = of_get_property(dev, "i2c-address", NULL);
Johannes Bergf3d94782006-06-21 15:42:43 +0200961 if (!_addr)
962 continue;
963 addr = ((*_addr) >> 1) & 0x7f;
964 /* now, if the address doesn't match any of the two
965 * that a tas3004 can have, we cannot handle this.
966 * I doubt it ever happens but hey. */
967 if (addr != 0x34 && addr != 0x35)
968 continue;
969 return tas_create(adapter, dev, addr);
970 }
971 }
972 return -ENODEV;
973}
974
975static int tas_i2c_detach(struct i2c_client *client)
976{
977 struct tas *tas = container_of(client, struct tas, i2c);
978 int err;
979 u8 tmp = TAS_ACR_ANALOG_PDOWN;
980
981 if ((err = i2c_detach_client(client)))
982 return err;
983 aoa_codec_unregister(&tas->codec);
984 of_node_put(tas->codec.node);
985
986 /* power down codec chip */
987 tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
988
Johannes Berg30719202006-09-17 21:59:25 +0200989 mutex_destroy(&tas->mtx);
Johannes Bergf3d94782006-06-21 15:42:43 +0200990 kfree(tas);
991 return 0;
992}
993
994static struct i2c_driver tas_driver = {
995 .driver = {
996 .name = "aoa_codec_tas",
997 .owner = THIS_MODULE,
998 },
999 .attach_adapter = tas_i2c_attach,
1000 .detach_client = tas_i2c_detach,
1001};
1002
1003static int __init tas_init(void)
1004{
1005 return i2c_add_driver(&tas_driver);
1006}
1007
1008static void __exit tas_exit(void)
1009{
1010 i2c_del_driver(&tas_driver);
1011}
1012
1013module_init(tas_init);
1014module_exit(tas_exit);