Commit | Line | Data |
---|---|---|
f3d9478b JB |
1 | /* |
2 | * Apple Onboard Audio driver for tas codec | |
3 | * | |
4 | * Copyright 2006 Johannes Berg <johannes@sipsolutions.net> | |
5 | * | |
6 | * GPL v2, can be found in COPYING. | |
7 | * | |
8 | * Open questions: | |
9 | * - How to distinguish between 3004 and versions? | |
10 | * | |
11 | * FIXMEs: | |
12 | * - This codec driver doesn't honour the 'connected' | |
13 | * property of the aoa_codec struct, hence if | |
14 | * it is used in machines where not everything is | |
15 | * connected it will display wrong mixer elements. | |
16 | * - Driver assumes that the microphone is always | |
17 | * monaureal and connected to the right channel of | |
18 | * the input. This should also be a codec-dependent | |
19 | * flag, maybe the codec should have 3 different | |
20 | * bits for the three different possibilities how | |
21 | * it can be hooked up... | |
22 | * But as long as I don't see any hardware hooked | |
23 | * up that way... | |
24 | * - As Apple notes in their code, the tas3004 seems | |
25 | * to delay the right channel by one sample. You can | |
26 | * see this when for example recording stereo in | |
27 | * audacity, or recording the tas output via cable | |
28 | * on another machine (use a sinus generator or so). | |
29 | * I tried programming the BiQuads but couldn't | |
30 | * make the delay work, maybe someone can read the | |
31 | * datasheet and fix it. The relevant Apple comment | |
32 | * is in AppleTAS3004Audio.cpp lines 1637 ff. Note | |
33 | * that their comment describing how they program | |
34 | * the filters sucks... | |
35 | * | |
36 | * Other things: | |
37 | * - this should actually register *two* aoa_codec | |
38 | * structs since it has two inputs. Then it must | |
39 | * use the prepare callback to forbid running the | |
40 | * secondary output on a different clock. | |
41 | * Also, whatever bus knows how to do this must | |
42 | * provide two soundbus_dev devices and the fabric | |
43 | * must be able to link them correctly. | |
44 | * | |
45 | * I don't even know if Apple ever uses the second | |
46 | * port on the tas3004 though, I don't think their | |
47 | * i2s controllers can even do it. OTOH, they all | |
48 | * derive the clocks from common clocks, so it | |
49 | * might just be possible. The framework allows the | |
50 | * codec to refine the transfer_info items in the | |
51 | * usable callback, so we can simply remove the | |
52 | * rates the second instance is not using when it | |
53 | * actually is in use. | |
54 | * Maybe we'll need to make the sound busses have | |
55 | * a 'clock group id' value so the codec can | |
56 | * determine if the two outputs can be driven at | |
57 | * the same time. But that is likely overkill, up | |
58 | * to the fabric to not link them up incorrectly, | |
59 | * and up to the hardware designer to not wire | |
60 | * them up in some weird unusable way. | |
61 | */ | |
62 | #include <stddef.h> | |
63 | #include <linux/i2c.h> | |
64 | #include <linux/i2c-dev.h> | |
65 | #include <asm/pmac_low_i2c.h> | |
66 | #include <asm/prom.h> | |
67 | #include <linux/delay.h> | |
68 | #include <linux/module.h> | |
69 | MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>"); | |
70 | MODULE_LICENSE("GPL"); | |
71 | MODULE_DESCRIPTION("tas codec driver for snd-aoa"); | |
72 | ||
73 | #include "snd-aoa-codec-tas.h" | |
74 | #include "snd-aoa-codec-tas-gain-table.h" | |
75 | #include "../aoa.h" | |
76 | #include "../soundbus/soundbus.h" | |
77 | ||
f3d9478b JB |
78 | #define PFX "snd-aoa-codec-tas: " |
79 | ||
6a4f5787 | 80 | |
f3d9478b JB |
81 | struct tas { |
82 | struct aoa_codec codec; | |
83 | struct i2c_client i2c; | |
6a4f5787 BH |
84 | u32 mute_l:1, mute_r:1 , |
85 | controls_created:1 , | |
86 | drc_enabled:1, | |
87 | hw_enabled:1; | |
f3d9478b JB |
88 | u8 cached_volume_l, cached_volume_r; |
89 | u8 mixer_l[3], mixer_r[3]; | |
90 | u8 acr; | |
6a4f5787 | 91 | int drc_range; |
f3d9478b JB |
92 | }; |
93 | ||
6a4f5787 BH |
94 | static int tas_reset_init(struct tas *tas); |
95 | ||
f3d9478b JB |
96 | static struct tas *codec_to_tas(struct aoa_codec *codec) |
97 | { | |
98 | return container_of(codec, struct tas, codec); | |
99 | } | |
100 | ||
101 | static inline int tas_write_reg(struct tas *tas, u8 reg, u8 len, u8 *data) | |
102 | { | |
103 | if (len == 1) | |
104 | return i2c_smbus_write_byte_data(&tas->i2c, reg, *data); | |
105 | else | |
106 | return i2c_smbus_write_i2c_block_data(&tas->i2c, reg, len, data); | |
107 | } | |
108 | ||
6a4f5787 BH |
109 | static void tas3004_set_drc(struct tas *tas) |
110 | { | |
111 | unsigned char val[6]; | |
112 | ||
113 | if (tas->drc_enabled) | |
114 | val[0] = 0x50; /* 3:1 above threshold */ | |
115 | else | |
116 | val[0] = 0x51; /* disabled */ | |
117 | val[1] = 0x02; /* 1:1 below threshold */ | |
118 | if (tas->drc_range > 0xef) | |
119 | val[2] = 0xef; | |
120 | else if (tas->drc_range < 0) | |
121 | val[2] = 0x00; | |
122 | else | |
123 | val[2] = tas->drc_range; | |
124 | val[3] = 0xb0; | |
125 | val[4] = 0x60; | |
126 | val[5] = 0xa0; | |
127 | ||
128 | tas_write_reg(tas, TAS_REG_DRC, 6, val); | |
129 | } | |
130 | ||
f3d9478b JB |
131 | static void tas_set_volume(struct tas *tas) |
132 | { | |
133 | u8 block[6]; | |
134 | int tmp; | |
135 | u8 left, right; | |
136 | ||
137 | left = tas->cached_volume_l; | |
138 | right = tas->cached_volume_r; | |
139 | ||
140 | if (left > 177) left = 177; | |
141 | if (right > 177) right = 177; | |
142 | ||
6a4f5787 BH |
143 | if (tas->mute_l) left = 0; |
144 | if (tas->mute_r) right = 0; | |
f3d9478b JB |
145 | |
146 | /* analysing the volume and mixer tables shows | |
147 | * that they are similar enough when we shift | |
148 | * the mixer table down by 4 bits. The error | |
149 | * is miniscule, in just one item the error | |
150 | * is 1, at a value of 0x07f17b (mixer table | |
151 | * value is 0x07f17a) */ | |
152 | tmp = tas_gaintable[left]; | |
153 | block[0] = tmp>>20; | |
154 | block[1] = tmp>>12; | |
155 | block[2] = tmp>>4; | |
156 | tmp = tas_gaintable[right]; | |
157 | block[3] = tmp>>20; | |
158 | block[4] = tmp>>12; | |
159 | block[5] = tmp>>4; | |
160 | tas_write_reg(tas, TAS_REG_VOL, 6, block); | |
161 | } | |
162 | ||
163 | static void tas_set_mixer(struct tas *tas) | |
164 | { | |
165 | u8 block[9]; | |
166 | int tmp, i; | |
167 | u8 val; | |
168 | ||
169 | for (i=0;i<3;i++) { | |
170 | val = tas->mixer_l[i]; | |
171 | if (val > 177) val = 177; | |
172 | tmp = tas_gaintable[val]; | |
173 | block[3*i+0] = tmp>>16; | |
174 | block[3*i+1] = tmp>>8; | |
175 | block[3*i+2] = tmp; | |
176 | } | |
177 | tas_write_reg(tas, TAS_REG_LMIX, 9, block); | |
178 | ||
179 | for (i=0;i<3;i++) { | |
180 | val = tas->mixer_r[i]; | |
181 | if (val > 177) val = 177; | |
182 | tmp = tas_gaintable[val]; | |
183 | block[3*i+0] = tmp>>16; | |
184 | block[3*i+1] = tmp>>8; | |
185 | block[3*i+2] = tmp; | |
186 | } | |
187 | tas_write_reg(tas, TAS_REG_RMIX, 9, block); | |
188 | } | |
189 | ||
190 | /* alsa stuff */ | |
191 | ||
192 | static int tas_dev_register(struct snd_device *dev) | |
193 | { | |
194 | return 0; | |
195 | } | |
196 | ||
197 | static struct snd_device_ops ops = { | |
198 | .dev_register = tas_dev_register, | |
199 | }; | |
200 | ||
201 | static int tas_snd_vol_info(struct snd_kcontrol *kcontrol, | |
202 | struct snd_ctl_elem_info *uinfo) | |
203 | { | |
204 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
205 | uinfo->count = 2; | |
206 | uinfo->value.integer.min = 0; | |
207 | uinfo->value.integer.max = 177; | |
208 | return 0; | |
209 | } | |
210 | ||
211 | static int tas_snd_vol_get(struct snd_kcontrol *kcontrol, | |
212 | struct snd_ctl_elem_value *ucontrol) | |
213 | { | |
214 | struct tas *tas = snd_kcontrol_chip(kcontrol); | |
215 | ||
216 | ucontrol->value.integer.value[0] = tas->cached_volume_l; | |
217 | ucontrol->value.integer.value[1] = tas->cached_volume_r; | |
218 | return 0; | |
219 | } | |
220 | ||
221 | static int tas_snd_vol_put(struct snd_kcontrol *kcontrol, | |
222 | struct snd_ctl_elem_value *ucontrol) | |
223 | { | |
224 | struct tas *tas = snd_kcontrol_chip(kcontrol); | |
225 | ||
226 | if (tas->cached_volume_l == ucontrol->value.integer.value[0] | |
227 | && tas->cached_volume_r == ucontrol->value.integer.value[1]) | |
228 | return 0; | |
229 | ||
230 | tas->cached_volume_l = ucontrol->value.integer.value[0]; | |
231 | tas->cached_volume_r = ucontrol->value.integer.value[1]; | |
6a4f5787 BH |
232 | if (tas->hw_enabled) |
233 | tas_set_volume(tas); | |
f3d9478b JB |
234 | return 1; |
235 | } | |
236 | ||
237 | static struct snd_kcontrol_new volume_control = { | |
238 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
239 | .name = "Master Playback Volume", | |
240 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, | |
241 | .info = tas_snd_vol_info, | |
242 | .get = tas_snd_vol_get, | |
243 | .put = tas_snd_vol_put, | |
244 | }; | |
245 | ||
246 | static int tas_snd_mute_info(struct snd_kcontrol *kcontrol, | |
247 | struct snd_ctl_elem_info *uinfo) | |
248 | { | |
249 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | |
250 | uinfo->count = 2; | |
251 | uinfo->value.integer.min = 0; | |
252 | uinfo->value.integer.max = 1; | |
253 | return 0; | |
254 | } | |
255 | ||
256 | static int tas_snd_mute_get(struct snd_kcontrol *kcontrol, | |
257 | struct snd_ctl_elem_value *ucontrol) | |
258 | { | |
259 | struct tas *tas = snd_kcontrol_chip(kcontrol); | |
260 | ||
6a4f5787 BH |
261 | ucontrol->value.integer.value[0] = !tas->mute_l; |
262 | ucontrol->value.integer.value[1] = !tas->mute_r; | |
f3d9478b JB |
263 | return 0; |
264 | } | |
265 | ||
266 | static int tas_snd_mute_put(struct snd_kcontrol *kcontrol, | |
267 | struct snd_ctl_elem_value *ucontrol) | |
268 | { | |
269 | struct tas *tas = snd_kcontrol_chip(kcontrol); | |
270 | ||
6a4f5787 BH |
271 | if (tas->mute_l == !ucontrol->value.integer.value[0] |
272 | && tas->mute_r == !ucontrol->value.integer.value[1]) | |
f3d9478b JB |
273 | return 0; |
274 | ||
6a4f5787 BH |
275 | tas->mute_l = !ucontrol->value.integer.value[0]; |
276 | tas->mute_r = !ucontrol->value.integer.value[1]; | |
277 | if (tas->hw_enabled) | |
278 | tas_set_volume(tas); | |
f3d9478b JB |
279 | return 1; |
280 | } | |
281 | ||
282 | static struct snd_kcontrol_new mute_control = { | |
283 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
284 | .name = "Master Playback Switch", | |
285 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, | |
286 | .info = tas_snd_mute_info, | |
287 | .get = tas_snd_mute_get, | |
288 | .put = tas_snd_mute_put, | |
289 | }; | |
290 | ||
291 | static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol, | |
292 | struct snd_ctl_elem_info *uinfo) | |
293 | { | |
294 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
295 | uinfo->count = 2; | |
296 | uinfo->value.integer.min = 0; | |
297 | uinfo->value.integer.max = 177; | |
298 | return 0; | |
299 | } | |
300 | ||
301 | static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol, | |
302 | struct snd_ctl_elem_value *ucontrol) | |
303 | { | |
304 | struct tas *tas = snd_kcontrol_chip(kcontrol); | |
305 | int idx = kcontrol->private_value; | |
306 | ||
307 | ucontrol->value.integer.value[0] = tas->mixer_l[idx]; | |
308 | ucontrol->value.integer.value[1] = tas->mixer_r[idx]; | |
309 | ||
310 | return 0; | |
311 | } | |
312 | ||
313 | static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol, | |
314 | struct snd_ctl_elem_value *ucontrol) | |
315 | { | |
316 | struct tas *tas = snd_kcontrol_chip(kcontrol); | |
317 | int idx = kcontrol->private_value; | |
318 | ||
319 | if (tas->mixer_l[idx] == ucontrol->value.integer.value[0] | |
320 | && tas->mixer_r[idx] == ucontrol->value.integer.value[1]) | |
321 | return 0; | |
322 | ||
323 | tas->mixer_l[idx] = ucontrol->value.integer.value[0]; | |
324 | tas->mixer_r[idx] = ucontrol->value.integer.value[1]; | |
325 | ||
6a4f5787 BH |
326 | if (tas->hw_enabled) |
327 | tas_set_mixer(tas); | |
f3d9478b JB |
328 | return 1; |
329 | } | |
330 | ||
331 | #define MIXER_CONTROL(n,descr,idx) \ | |
332 | static struct snd_kcontrol_new n##_control = { \ | |
333 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, \ | |
334 | .name = descr " Playback Volume", \ | |
335 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, \ | |
336 | .info = tas_snd_mixer_info, \ | |
337 | .get = tas_snd_mixer_get, \ | |
338 | .put = tas_snd_mixer_put, \ | |
339 | .private_value = idx, \ | |
340 | } | |
341 | ||
14b42963 | 342 | MIXER_CONTROL(pcm1, "PCM", 0); |
f3d9478b JB |
343 | MIXER_CONTROL(monitor, "Monitor", 2); |
344 | ||
9b8f52f5 JB |
345 | static int tas_snd_drc_range_info(struct snd_kcontrol *kcontrol, |
346 | struct snd_ctl_elem_info *uinfo) | |
347 | { | |
348 | uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; | |
349 | uinfo->count = 1; | |
350 | uinfo->value.integer.min = 0; | |
351 | uinfo->value.integer.max = TAS3004_DRC_MAX; | |
352 | return 0; | |
353 | } | |
354 | ||
355 | static int tas_snd_drc_range_get(struct snd_kcontrol *kcontrol, | |
356 | struct snd_ctl_elem_value *ucontrol) | |
357 | { | |
358 | struct tas *tas = snd_kcontrol_chip(kcontrol); | |
359 | ||
360 | ucontrol->value.integer.value[0] = tas->drc_range; | |
361 | return 0; | |
362 | } | |
363 | ||
364 | static int tas_snd_drc_range_put(struct snd_kcontrol *kcontrol, | |
365 | struct snd_ctl_elem_value *ucontrol) | |
366 | { | |
367 | struct tas *tas = snd_kcontrol_chip(kcontrol); | |
368 | ||
369 | if (tas->drc_range == ucontrol->value.integer.value[0]) | |
370 | return 0; | |
371 | ||
372 | tas->drc_range = ucontrol->value.integer.value[0]; | |
373 | if (tas->hw_enabled) | |
374 | tas3004_set_drc(tas); | |
375 | return 1; | |
376 | } | |
377 | ||
378 | static struct snd_kcontrol_new drc_range_control = { | |
379 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
380 | .name = "DRC Range", | |
381 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, | |
382 | .info = tas_snd_drc_range_info, | |
383 | .get = tas_snd_drc_range_get, | |
384 | .put = tas_snd_drc_range_put, | |
385 | }; | |
386 | ||
387 | static int tas_snd_drc_switch_info(struct snd_kcontrol *kcontrol, | |
388 | struct snd_ctl_elem_info *uinfo) | |
389 | { | |
390 | uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN; | |
391 | uinfo->count = 1; | |
392 | uinfo->value.integer.min = 0; | |
393 | uinfo->value.integer.max = 1; | |
394 | return 0; | |
395 | } | |
396 | ||
397 | static int tas_snd_drc_switch_get(struct snd_kcontrol *kcontrol, | |
398 | struct snd_ctl_elem_value *ucontrol) | |
399 | { | |
400 | struct tas *tas = snd_kcontrol_chip(kcontrol); | |
401 | ||
402 | ucontrol->value.integer.value[0] = tas->drc_enabled; | |
403 | return 0; | |
404 | } | |
405 | ||
406 | static int tas_snd_drc_switch_put(struct snd_kcontrol *kcontrol, | |
407 | struct snd_ctl_elem_value *ucontrol) | |
408 | { | |
409 | struct tas *tas = snd_kcontrol_chip(kcontrol); | |
410 | ||
411 | if (tas->drc_enabled == ucontrol->value.integer.value[0]) | |
412 | return 0; | |
413 | ||
414 | tas->drc_enabled = ucontrol->value.integer.value[0]; | |
415 | if (tas->hw_enabled) | |
416 | tas3004_set_drc(tas); | |
417 | return 1; | |
418 | } | |
419 | ||
420 | static struct snd_kcontrol_new drc_switch_control = { | |
421 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
422 | .name = "DRC Range Switch", | |
423 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, | |
424 | .info = tas_snd_drc_switch_info, | |
425 | .get = tas_snd_drc_switch_get, | |
426 | .put = tas_snd_drc_switch_put, | |
427 | }; | |
428 | ||
f3d9478b JB |
429 | static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol, |
430 | struct snd_ctl_elem_info *uinfo) | |
431 | { | |
432 | static char *texts[] = { "Line-In", "Microphone" }; | |
433 | ||
434 | uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; | |
435 | uinfo->count = 1; | |
436 | uinfo->value.enumerated.items = 2; | |
437 | if (uinfo->value.enumerated.item > 1) | |
438 | uinfo->value.enumerated.item = 1; | |
439 | strcpy(uinfo->value.enumerated.name, texts[uinfo->value.enumerated.item]); | |
440 | return 0; | |
441 | } | |
442 | ||
443 | static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol, | |
444 | struct snd_ctl_elem_value *ucontrol) | |
445 | { | |
446 | struct tas *tas = snd_kcontrol_chip(kcontrol); | |
447 | ||
448 | ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B); | |
449 | return 0; | |
450 | } | |
451 | ||
452 | static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol, | |
453 | struct snd_ctl_elem_value *ucontrol) | |
454 | { | |
455 | struct tas *tas = snd_kcontrol_chip(kcontrol); | |
456 | int oldacr = tas->acr; | |
457 | ||
458 | tas->acr &= ~TAS_ACR_INPUT_B; | |
459 | if (ucontrol->value.enumerated.item[0]) | |
460 | tas->acr |= TAS_ACR_INPUT_B; | |
461 | if (oldacr == tas->acr) | |
462 | return 0; | |
6a4f5787 BH |
463 | if (tas->hw_enabled) |
464 | tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr); | |
f3d9478b JB |
465 | return 1; |
466 | } | |
467 | ||
468 | static struct snd_kcontrol_new capture_source_control = { | |
469 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, | |
470 | /* If we name this 'Input Source', it properly shows up in | |
471 | * alsamixer as a selection, * but it's shown under the | |
472 | * 'Playback' category. | |
473 | * If I name it 'Capture Source', it shows up in strange | |
474 | * ways (two bools of which one can be selected at a | |
475 | * time) but at least it's shown in the 'Capture' | |
476 | * category. | |
477 | * I was told that this was due to backward compatibility, | |
478 | * but I don't understand then why the mangling is *not* | |
479 | * done when I name it "Input Source"..... | |
480 | */ | |
481 | .name = "Capture Source", | |
482 | .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, | |
483 | .info = tas_snd_capture_source_info, | |
484 | .get = tas_snd_capture_source_get, | |
485 | .put = tas_snd_capture_source_put, | |
486 | }; | |
487 | ||
488 | ||
489 | static struct transfer_info tas_transfers[] = { | |
490 | { | |
491 | /* input */ | |
492 | .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE | | |
493 | SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE, | |
494 | .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, | |
495 | .transfer_in = 1, | |
496 | }, | |
497 | { | |
498 | /* output */ | |
499 | .formats = SNDRV_PCM_FMTBIT_S16_BE | SNDRV_PCM_FMTBIT_S16_BE | | |
500 | SNDRV_PCM_FMTBIT_S24_BE | SNDRV_PCM_FMTBIT_S24_BE, | |
501 | .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000, | |
502 | .transfer_in = 0, | |
503 | }, | |
504 | {} | |
505 | }; | |
506 | ||
507 | static int tas_usable(struct codec_info_item *cii, | |
508 | struct transfer_info *ti, | |
509 | struct transfer_info *out) | |
510 | { | |
511 | return 1; | |
512 | } | |
513 | ||
514 | static int tas_reset_init(struct tas *tas) | |
515 | { | |
516 | u8 tmp; | |
6a4f5787 BH |
517 | |
518 | tas->codec.gpio->methods->all_amps_off(tas->codec.gpio); | |
519 | msleep(5); | |
f3d9478b | 520 | tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0); |
6a4f5787 | 521 | msleep(5); |
f3d9478b | 522 | tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1); |
6a4f5787 | 523 | msleep(20); |
f3d9478b | 524 | tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0); |
6a4f5787 BH |
525 | msleep(10); |
526 | tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio); | |
f3d9478b JB |
527 | |
528 | tmp = TAS_MCS_SCLK64 | TAS_MCS_SPORT_MODE_I2S | TAS_MCS_SPORT_WL_24BIT; | |
529 | if (tas_write_reg(tas, TAS_REG_MCS, 1, &tmp)) | |
530 | return -ENODEV; | |
531 | ||
6a4f5787 BH |
532 | tas->acr |= TAS_ACR_ANALOG_PDOWN | TAS_ACR_B_MONAUREAL | |
533 | TAS_ACR_B_MON_SEL_RIGHT; | |
534 | if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr)) | |
535 | return -ENODEV; | |
536 | ||
f3d9478b JB |
537 | tmp = 0; |
538 | if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp)) | |
539 | return -ENODEV; | |
540 | ||
6a4f5787 BH |
541 | tas3004_set_drc(tas); |
542 | ||
543 | /* Set treble & bass to 0dB */ | |
544 | tmp = 114; | |
545 | tas_write_reg(tas, TAS_REG_TREBLE, 1, &tmp); | |
546 | tas_write_reg(tas, TAS_REG_BASS, 1, &tmp); | |
547 | ||
548 | tas->acr &= ~TAS_ACR_ANALOG_PDOWN; | |
549 | if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr)) | |
550 | return -ENODEV; | |
551 | ||
552 | return 0; | |
553 | } | |
554 | ||
555 | static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock) | |
556 | { | |
557 | struct tas *tas = cii->codec_data; | |
558 | ||
559 | switch(clock) { | |
560 | case CLOCK_SWITCH_PREPARE_SLAVE: | |
561 | /* Clocks are going away, mute mute mute */ | |
562 | tas->codec.gpio->methods->all_amps_off(tas->codec.gpio); | |
563 | tas->hw_enabled = 0; | |
564 | break; | |
565 | case CLOCK_SWITCH_SLAVE: | |
566 | /* Clocks are back, re-init the codec */ | |
567 | tas_reset_init(tas); | |
568 | tas_set_volume(tas); | |
569 | tas_set_mixer(tas); | |
570 | tas->hw_enabled = 1; | |
571 | tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio); | |
572 | break; | |
573 | default: | |
574 | /* doesn't happen as of now */ | |
575 | return -EINVAL; | |
576 | } | |
f3d9478b JB |
577 | return 0; |
578 | } | |
579 | ||
580 | /* we are controlled via i2c and assume that is always up | |
581 | * If that wasn't the case, we'd have to suspend once | |
582 | * our i2c device is suspended, and then take note of that! */ | |
583 | static int tas_suspend(struct tas *tas) | |
584 | { | |
6a4f5787 | 585 | tas->hw_enabled = 0; |
f3d9478b JB |
586 | tas->acr |= TAS_ACR_ANALOG_PDOWN; |
587 | tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr); | |
588 | return 0; | |
589 | } | |
590 | ||
591 | static int tas_resume(struct tas *tas) | |
592 | { | |
593 | /* reset codec */ | |
594 | tas_reset_init(tas); | |
595 | tas_set_volume(tas); | |
596 | tas_set_mixer(tas); | |
6a4f5787 | 597 | tas->hw_enabled = 1; |
f3d9478b JB |
598 | return 0; |
599 | } | |
600 | ||
601 | #ifdef CONFIG_PM | |
602 | static int _tas_suspend(struct codec_info_item *cii, pm_message_t state) | |
603 | { | |
604 | return tas_suspend(cii->codec_data); | |
605 | } | |
606 | ||
607 | static int _tas_resume(struct codec_info_item *cii) | |
608 | { | |
609 | return tas_resume(cii->codec_data); | |
610 | } | |
611 | #endif | |
612 | ||
613 | static struct codec_info tas_codec_info = { | |
614 | .transfers = tas_transfers, | |
615 | /* in theory, we can drive it at 512 too... | |
616 | * but so far the framework doesn't allow | |
617 | * for that and I don't see much point in it. */ | |
618 | .sysclock_factor = 256, | |
619 | /* same here, could be 32 for just one 16 bit format */ | |
620 | .bus_factor = 64, | |
621 | .owner = THIS_MODULE, | |
622 | .usable = tas_usable, | |
6a4f5787 | 623 | .switch_clock = tas_switch_clock, |
f3d9478b JB |
624 | #ifdef CONFIG_PM |
625 | .suspend = _tas_suspend, | |
626 | .resume = _tas_resume, | |
627 | #endif | |
628 | }; | |
629 | ||
630 | static int tas_init_codec(struct aoa_codec *codec) | |
631 | { | |
632 | struct tas *tas = codec_to_tas(codec); | |
633 | int err; | |
634 | ||
635 | if (!tas->codec.gpio || !tas->codec.gpio->methods) { | |
636 | printk(KERN_ERR PFX "gpios not assigned!!\n"); | |
637 | return -EINVAL; | |
638 | } | |
639 | ||
640 | if (tas_reset_init(tas)) { | |
641 | printk(KERN_ERR PFX "tas failed to initialise\n"); | |
642 | return -ENXIO; | |
643 | } | |
6a4f5787 | 644 | tas->hw_enabled = 1; |
f3d9478b JB |
645 | |
646 | if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev, | |
647 | aoa_get_card(), | |
648 | &tas_codec_info, tas)) { | |
649 | printk(KERN_ERR PFX "error attaching tas to soundbus\n"); | |
650 | return -ENODEV; | |
651 | } | |
652 | ||
653 | if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, tas, &ops)) { | |
654 | printk(KERN_ERR PFX "failed to create tas snd device!\n"); | |
655 | return -ENODEV; | |
656 | } | |
657 | err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas)); | |
658 | if (err) | |
659 | goto error; | |
660 | ||
661 | err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas)); | |
662 | if (err) | |
663 | goto error; | |
664 | ||
665 | err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas)); | |
666 | if (err) | |
667 | goto error; | |
668 | ||
669 | err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas)); | |
670 | if (err) | |
671 | goto error; | |
672 | ||
673 | err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas)); | |
674 | if (err) | |
675 | goto error; | |
676 | ||
9b8f52f5 JB |
677 | err = aoa_snd_ctl_add(snd_ctl_new1(&drc_range_control, tas)); |
678 | if (err) | |
679 | goto error; | |
680 | ||
681 | err = aoa_snd_ctl_add(snd_ctl_new1(&drc_switch_control, tas)); | |
682 | if (err) | |
683 | goto error; | |
684 | ||
f3d9478b JB |
685 | return 0; |
686 | error: | |
687 | tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas); | |
688 | snd_device_free(aoa_get_card(), tas); | |
689 | return err; | |
690 | } | |
691 | ||
692 | static void tas_exit_codec(struct aoa_codec *codec) | |
693 | { | |
694 | struct tas *tas = codec_to_tas(codec); | |
695 | ||
696 | if (!tas->codec.soundbus_dev) | |
697 | return; | |
698 | tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas); | |
699 | } | |
700 | ||
701 | ||
702 | static struct i2c_driver tas_driver; | |
703 | ||
704 | static int tas_create(struct i2c_adapter *adapter, | |
705 | struct device_node *node, | |
706 | int addr) | |
707 | { | |
708 | struct tas *tas; | |
709 | ||
710 | tas = kzalloc(sizeof(struct tas), GFP_KERNEL); | |
711 | ||
712 | if (!tas) | |
713 | return -ENOMEM; | |
714 | ||
715 | tas->i2c.driver = &tas_driver; | |
716 | tas->i2c.adapter = adapter; | |
717 | tas->i2c.addr = addr; | |
9b8f52f5 JB |
718 | /* seems that half is a saner default */ |
719 | tas->drc_range = TAS3004_DRC_MAX / 2; | |
f3d9478b JB |
720 | strlcpy(tas->i2c.name, "tas audio codec", I2C_NAME_SIZE-1); |
721 | ||
722 | if (i2c_attach_client(&tas->i2c)) { | |
723 | printk(KERN_ERR PFX "failed to attach to i2c\n"); | |
724 | goto fail; | |
725 | } | |
726 | ||
727 | strlcpy(tas->codec.name, "tas", MAX_CODEC_NAME_LEN-1); | |
728 | tas->codec.owner = THIS_MODULE; | |
729 | tas->codec.init = tas_init_codec; | |
730 | tas->codec.exit = tas_exit_codec; | |
731 | tas->codec.node = of_node_get(node); | |
732 | ||
733 | if (aoa_codec_register(&tas->codec)) { | |
734 | goto detach; | |
735 | } | |
6a4f5787 BH |
736 | printk(KERN_DEBUG |
737 | "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n", | |
738 | addr, node->full_name); | |
f3d9478b JB |
739 | return 0; |
740 | detach: | |
741 | i2c_detach_client(&tas->i2c); | |
742 | fail: | |
743 | kfree(tas); | |
744 | return -EINVAL; | |
745 | } | |
746 | ||
747 | static int tas_i2c_attach(struct i2c_adapter *adapter) | |
748 | { | |
749 | struct device_node *busnode, *dev = NULL; | |
750 | struct pmac_i2c_bus *bus; | |
751 | ||
752 | bus = pmac_i2c_adapter_to_bus(adapter); | |
753 | if (bus == NULL) | |
754 | return -ENODEV; | |
755 | busnode = pmac_i2c_get_bus_node(bus); | |
756 | ||
757 | while ((dev = of_get_next_child(busnode, dev)) != NULL) { | |
758 | if (device_is_compatible(dev, "tas3004")) { | |
759 | u32 *addr; | |
760 | printk(KERN_DEBUG PFX "found tas3004\n"); | |
761 | addr = (u32 *) get_property(dev, "reg", NULL); | |
762 | if (!addr) | |
763 | continue; | |
764 | return tas_create(adapter, dev, ((*addr) >> 1) & 0x7f); | |
765 | } | |
766 | /* older machines have no 'codec' node with a 'compatible' | |
767 | * property that says 'tas3004', they just have a 'deq' | |
768 | * node without any such property... */ | |
769 | if (strcmp(dev->name, "deq") == 0) { | |
770 | u32 *_addr, addr; | |
771 | printk(KERN_DEBUG PFX "found 'deq' node\n"); | |
772 | _addr = (u32 *) get_property(dev, "i2c-address", NULL); | |
773 | if (!_addr) | |
774 | continue; | |
775 | addr = ((*_addr) >> 1) & 0x7f; | |
776 | /* now, if the address doesn't match any of the two | |
777 | * that a tas3004 can have, we cannot handle this. | |
778 | * I doubt it ever happens but hey. */ | |
779 | if (addr != 0x34 && addr != 0x35) | |
780 | continue; | |
781 | return tas_create(adapter, dev, addr); | |
782 | } | |
783 | } | |
784 | return -ENODEV; | |
785 | } | |
786 | ||
787 | static int tas_i2c_detach(struct i2c_client *client) | |
788 | { | |
789 | struct tas *tas = container_of(client, struct tas, i2c); | |
790 | int err; | |
791 | u8 tmp = TAS_ACR_ANALOG_PDOWN; | |
792 | ||
793 | if ((err = i2c_detach_client(client))) | |
794 | return err; | |
795 | aoa_codec_unregister(&tas->codec); | |
796 | of_node_put(tas->codec.node); | |
797 | ||
798 | /* power down codec chip */ | |
799 | tas_write_reg(tas, TAS_REG_ACR, 1, &tmp); | |
800 | ||
801 | kfree(tas); | |
802 | return 0; | |
803 | } | |
804 | ||
805 | static struct i2c_driver tas_driver = { | |
806 | .driver = { | |
807 | .name = "aoa_codec_tas", | |
808 | .owner = THIS_MODULE, | |
809 | }, | |
810 | .attach_adapter = tas_i2c_attach, | |
811 | .detach_client = tas_i2c_detach, | |
812 | }; | |
813 | ||
814 | static int __init tas_init(void) | |
815 | { | |
816 | return i2c_add_driver(&tas_driver); | |
817 | } | |
818 | ||
819 | static void __exit tas_exit(void) | |
820 | { | |
821 | i2c_del_driver(&tas_driver); | |
822 | } | |
823 | ||
824 | module_init(tas_init); | |
825 | module_exit(tas_exit); |