1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62#include <stddef.h>
63#include <linux/i2c.h>
64#include <asm/pmac_low_i2c.h>
65#include <asm/prom.h>
66#include <linux/delay.h>
67#include <linux/module.h>
68#include <linux/mutex.h>
69#include <linux/slab.h>
70
71MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
72MODULE_LICENSE("GPL");
73MODULE_DESCRIPTION("tas codec driver for snd-aoa");
74
75#include "tas.h"
76#include "tas-gain-table.h"
77#include "tas-basstreble.h"
78#include "../aoa.h"
79#include "../soundbus/soundbus.h"
80
81#define PFX "snd-aoa-codec-tas: "
82
83
84struct tas {
85 struct aoa_codec codec;
86 struct i2c_client *i2c;
87 u32 mute_l:1, mute_r:1 ,
88 controls_created:1 ,
89 drc_enabled:1,
90 hw_enabled:1;
91 u8 cached_volume_l, cached_volume_r;
92 u8 mixer_l[3], mixer_r[3];
93 u8 bass, treble;
94 u8 acr;
95 int drc_range;
96
97
98
99 struct mutex mtx;
100};
101
102static int tas_reset_init(struct tas *tas);
103
104static struct tas *codec_to_tas(struct aoa_codec *codec)
105{
106 return container_of(codec, struct tas, codec);
107}
108
109static inline int tas_write_reg(struct tas *tas, u8 reg, u8 len, u8 *data)
110{
111 if (len == 1)
112 return i2c_smbus_write_byte_data(tas->i2c, reg, *data);
113 else
114 return i2c_smbus_write_i2c_block_data(tas->i2c, reg, len, data);
115}
116
117static void tas3004_set_drc(struct tas *tas)
118{
119 unsigned char val[6];
120
121 if (tas->drc_enabled)
122 val[0] = 0x50;
123 else
124 val[0] = 0x51;
125 val[1] = 0x02;
126 if (tas->drc_range > 0xef)
127 val[2] = 0xef;
128 else if (tas->drc_range < 0)
129 val[2] = 0x00;
130 else
131 val[2] = tas->drc_range;
132 val[3] = 0xb0;
133 val[4] = 0x60;
134 val[5] = 0xa0;
135
136 tas_write_reg(tas, TAS_REG_DRC, 6, val);
137}
138
139static void tas_set_treble(struct tas *tas)
140{
141 u8 tmp;
142
143 tmp = tas3004_treble(tas->treble);
144 tas_write_reg(tas, TAS_REG_TREBLE, 1, &tmp);
145}
146
147static void tas_set_bass(struct tas *tas)
148{
149 u8 tmp;
150
151 tmp = tas3004_bass(tas->bass);
152 tas_write_reg(tas, TAS_REG_BASS, 1, &tmp);
153}
154
155static void tas_set_volume(struct tas *tas)
156{
157 u8 block[6];
158 int tmp;
159 u8 left, right;
160
161 left = tas->cached_volume_l;
162 right = tas->cached_volume_r;
163
164 if (left > 177) left = 177;
165 if (right > 177) right = 177;
166
167 if (tas->mute_l) left = 0;
168 if (tas->mute_r) right = 0;
169
170
171
172
173
174
175
176 tmp = tas_gaintable[left];
177 block[0] = tmp>>20;
178 block[1] = tmp>>12;
179 block[2] = tmp>>4;
180 tmp = tas_gaintable[right];
181 block[3] = tmp>>20;
182 block[4] = tmp>>12;
183 block[5] = tmp>>4;
184 tas_write_reg(tas, TAS_REG_VOL, 6, block);
185}
186
187static void tas_set_mixer(struct tas *tas)
188{
189 u8 block[9];
190 int tmp, i;
191 u8 val;
192
193 for (i=0;i<3;i++) {
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;
199 block[3*i+2] = tmp;
200 }
201 tas_write_reg(tas, TAS_REG_LMIX, 9, block);
202
203 for (i=0;i<3;i++) {
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;
209 block[3*i+2] = tmp;
210 }
211 tas_write_reg(tas, TAS_REG_RMIX, 9, block);
212}
213
214
215
216static int tas_dev_register(struct snd_device *dev)
217{
218 return 0;
219}
220
221static struct snd_device_ops ops = {
222 .dev_register = tas_dev_register,
223};
224
225static int tas_snd_vol_info(struct snd_kcontrol *kcontrol,
226 struct snd_ctl_elem_info *uinfo)
227{
228 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
229 uinfo->count = 2;
230 uinfo->value.integer.min = 0;
231 uinfo->value.integer.max = 177;
232 return 0;
233}
234
235static int tas_snd_vol_get(struct snd_kcontrol *kcontrol,
236 struct snd_ctl_elem_value *ucontrol)
237{
238 struct tas *tas = snd_kcontrol_chip(kcontrol);
239
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);
244 return 0;
245}
246
247static int tas_snd_vol_put(struct snd_kcontrol *kcontrol,
248 struct snd_ctl_elem_value *ucontrol)
249{
250 struct tas *tas = snd_kcontrol_chip(kcontrol);
251
252 if (ucontrol->value.integer.value[0] < 0 ||
253 ucontrol->value.integer.value[0] > 177)
254 return -EINVAL;
255 if (ucontrol->value.integer.value[1] < 0 ||
256 ucontrol->value.integer.value[1] > 177)
257 return -EINVAL;
258
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);
263 return 0;
264 }
265
266 tas->cached_volume_l = ucontrol->value.integer.value[0];
267 tas->cached_volume_r = ucontrol->value.integer.value[1];
268 if (tas->hw_enabled)
269 tas_set_volume(tas);
270 mutex_unlock(&tas->mtx);
271 return 1;
272}
273
274static 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,
281};
282
283#define tas_snd_mute_info snd_ctl_boolean_stereo_info
284
285static int tas_snd_mute_get(struct snd_kcontrol *kcontrol,
286 struct snd_ctl_elem_value *ucontrol)
287{
288 struct tas *tas = snd_kcontrol_chip(kcontrol);
289
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);
294 return 0;
295}
296
297static int tas_snd_mute_put(struct snd_kcontrol *kcontrol,
298 struct snd_ctl_elem_value *ucontrol)
299{
300 struct tas *tas = snd_kcontrol_chip(kcontrol);
301
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);
306 return 0;
307 }
308
309 tas->mute_l = !ucontrol->value.integer.value[0];
310 tas->mute_r = !ucontrol->value.integer.value[1];
311 if (tas->hw_enabled)
312 tas_set_volume(tas);
313 mutex_unlock(&tas->mtx);
314 return 1;
315}
316
317static 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,
324};
325
326static int tas_snd_mixer_info(struct snd_kcontrol *kcontrol,
327 struct snd_ctl_elem_info *uinfo)
328{
329 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
330 uinfo->count = 2;
331 uinfo->value.integer.min = 0;
332 uinfo->value.integer.max = 177;
333 return 0;
334}
335
336static int tas_snd_mixer_get(struct snd_kcontrol *kcontrol,
337 struct snd_ctl_elem_value *ucontrol)
338{
339 struct tas *tas = snd_kcontrol_chip(kcontrol);
340 int idx = kcontrol->private_value;
341
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);
346
347 return 0;
348}
349
350static int tas_snd_mixer_put(struct snd_kcontrol *kcontrol,
351 struct snd_ctl_elem_value *ucontrol)
352{
353 struct tas *tas = snd_kcontrol_chip(kcontrol);
354 int idx = kcontrol->private_value;
355
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);
360 return 0;
361 }
362
363 tas->mixer_l[idx] = ucontrol->value.integer.value[0];
364 tas->mixer_r[idx] = ucontrol->value.integer.value[1];
365
366 if (tas->hw_enabled)
367 tas_set_mixer(tas);
368 mutex_unlock(&tas->mtx);
369 return 1;
370}
371
372#define MIXER_CONTROL(n,descr,idx) \
373static 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, \
381}
382
383MIXER_CONTROL(pcm1, "PCM", 0);
384MIXER_CONTROL(monitor, "Monitor", 2);
385
386static int tas_snd_drc_range_info(struct snd_kcontrol *kcontrol,
387 struct snd_ctl_elem_info *uinfo)
388{
389 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
390 uinfo->count = 1;
391 uinfo->value.integer.min = 0;
392 uinfo->value.integer.max = TAS3004_DRC_MAX;
393 return 0;
394}
395
396static int tas_snd_drc_range_get(struct snd_kcontrol *kcontrol,
397 struct snd_ctl_elem_value *ucontrol)
398{
399 struct tas *tas = snd_kcontrol_chip(kcontrol);
400
401 mutex_lock(&tas->mtx);
402 ucontrol->value.integer.value[0] = tas->drc_range;
403 mutex_unlock(&tas->mtx);
404 return 0;
405}
406
407static int tas_snd_drc_range_put(struct snd_kcontrol *kcontrol,
408 struct snd_ctl_elem_value *ucontrol)
409{
410 struct tas *tas = snd_kcontrol_chip(kcontrol);
411
412 if (ucontrol->value.integer.value[0] < 0 ||
413 ucontrol->value.integer.value[0] > TAS3004_DRC_MAX)
414 return -EINVAL;
415
416 mutex_lock(&tas->mtx);
417 if (tas->drc_range == ucontrol->value.integer.value[0]) {
418 mutex_unlock(&tas->mtx);
419 return 0;
420 }
421
422 tas->drc_range = ucontrol->value.integer.value[0];
423 if (tas->hw_enabled)
424 tas3004_set_drc(tas);
425 mutex_unlock(&tas->mtx);
426 return 1;
427}
428
429static struct snd_kcontrol_new drc_range_control = {
430 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
431 .name = "DRC Range",
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,
436};
437
438#define tas_snd_drc_switch_info snd_ctl_boolean_mono_info
439
440static int tas_snd_drc_switch_get(struct snd_kcontrol *kcontrol,
441 struct snd_ctl_elem_value *ucontrol)
442{
443 struct tas *tas = snd_kcontrol_chip(kcontrol);
444
445 mutex_lock(&tas->mtx);
446 ucontrol->value.integer.value[0] = tas->drc_enabled;
447 mutex_unlock(&tas->mtx);
448 return 0;
449}
450
451static int tas_snd_drc_switch_put(struct snd_kcontrol *kcontrol,
452 struct snd_ctl_elem_value *ucontrol)
453{
454 struct tas *tas = snd_kcontrol_chip(kcontrol);
455
456 mutex_lock(&tas->mtx);
457 if (tas->drc_enabled == ucontrol->value.integer.value[0]) {
458 mutex_unlock(&tas->mtx);
459 return 0;
460 }
461
462 tas->drc_enabled = !!ucontrol->value.integer.value[0];
463 if (tas->hw_enabled)
464 tas3004_set_drc(tas);
465 mutex_unlock(&tas->mtx);
466 return 1;
467}
468
469static 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,
476};
477
478static int tas_snd_capture_source_info(struct snd_kcontrol *kcontrol,
479 struct snd_ctl_elem_info *uinfo)
480{
481 static char *texts[] = { "Line-In", "Microphone" };
482
483 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
484 uinfo->count = 1;
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]);
489 return 0;
490}
491
492static int tas_snd_capture_source_get(struct snd_kcontrol *kcontrol,
493 struct snd_ctl_elem_value *ucontrol)
494{
495 struct tas *tas = snd_kcontrol_chip(kcontrol);
496
497 mutex_lock(&tas->mtx);
498 ucontrol->value.enumerated.item[0] = !!(tas->acr & TAS_ACR_INPUT_B);
499 mutex_unlock(&tas->mtx);
500 return 0;
501}
502
503static int tas_snd_capture_source_put(struct snd_kcontrol *kcontrol,
504 struct snd_ctl_elem_value *ucontrol)
505{
506 struct tas *tas = snd_kcontrol_chip(kcontrol);
507 int oldacr;
508
509 if (ucontrol->value.enumerated.item[0] > 1)
510 return -EINVAL;
511 mutex_lock(&tas->mtx);
512 oldacr = tas->acr;
513
514
515
516
517
518
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);
525 return 0;
526 }
527 if (tas->hw_enabled)
528 tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
529 mutex_unlock(&tas->mtx);
530 return 1;
531}
532
533static struct snd_kcontrol_new capture_source_control = {
534 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
535
536
537
538
539
540
541
542
543
544
545
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,
551};
552
553static int tas_snd_treble_info(struct snd_kcontrol *kcontrol,
554 struct snd_ctl_elem_info *uinfo)
555{
556 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
557 uinfo->count = 1;
558 uinfo->value.integer.min = TAS3004_TREBLE_MIN;
559 uinfo->value.integer.max = TAS3004_TREBLE_MAX;
560 return 0;
561}
562
563static int tas_snd_treble_get(struct snd_kcontrol *kcontrol,
564 struct snd_ctl_elem_value *ucontrol)
565{
566 struct tas *tas = snd_kcontrol_chip(kcontrol);
567
568 mutex_lock(&tas->mtx);
569 ucontrol->value.integer.value[0] = tas->treble;
570 mutex_unlock(&tas->mtx);
571 return 0;
572}
573
574static int tas_snd_treble_put(struct snd_kcontrol *kcontrol,
575 struct snd_ctl_elem_value *ucontrol)
576{
577 struct tas *tas = snd_kcontrol_chip(kcontrol);
578
579 if (ucontrol->value.integer.value[0] < TAS3004_TREBLE_MIN ||
580 ucontrol->value.integer.value[0] > TAS3004_TREBLE_MAX)
581 return -EINVAL;
582 mutex_lock(&tas->mtx);
583 if (tas->treble == ucontrol->value.integer.value[0]) {
584 mutex_unlock(&tas->mtx);
585 return 0;
586 }
587
588 tas->treble = ucontrol->value.integer.value[0];
589 if (tas->hw_enabled)
590 tas_set_treble(tas);
591 mutex_unlock(&tas->mtx);
592 return 1;
593}
594
595static struct snd_kcontrol_new treble_control = {
596 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
597 .name = "Treble",
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,
602};
603
604static int tas_snd_bass_info(struct snd_kcontrol *kcontrol,
605 struct snd_ctl_elem_info *uinfo)
606{
607 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
608 uinfo->count = 1;
609 uinfo->value.integer.min = TAS3004_BASS_MIN;
610 uinfo->value.integer.max = TAS3004_BASS_MAX;
611 return 0;
612}
613
614static int tas_snd_bass_get(struct snd_kcontrol *kcontrol,
615 struct snd_ctl_elem_value *ucontrol)
616{
617 struct tas *tas = snd_kcontrol_chip(kcontrol);
618
619 mutex_lock(&tas->mtx);
620 ucontrol->value.integer.value[0] = tas->bass;
621 mutex_unlock(&tas->mtx);
622 return 0;
623}
624
625static int tas_snd_bass_put(struct snd_kcontrol *kcontrol,
626 struct snd_ctl_elem_value *ucontrol)
627{
628 struct tas *tas = snd_kcontrol_chip(kcontrol);
629
630 if (ucontrol->value.integer.value[0] < TAS3004_BASS_MIN ||
631 ucontrol->value.integer.value[0] > TAS3004_BASS_MAX)
632 return -EINVAL;
633 mutex_lock(&tas->mtx);
634 if (tas->bass == ucontrol->value.integer.value[0]) {
635 mutex_unlock(&tas->mtx);
636 return 0;
637 }
638
639 tas->bass = ucontrol->value.integer.value[0];
640 if (tas->hw_enabled)
641 tas_set_bass(tas);
642 mutex_unlock(&tas->mtx);
643 return 1;
644}
645
646static struct snd_kcontrol_new bass_control = {
647 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
648 .name = "Bass",
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,
653};
654
655static struct transfer_info tas_transfers[] = {
656 {
657
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,
660 .transfer_in = 1,
661 },
662 {
663
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,
666 .transfer_in = 0,
667 },
668 {}
669};
670
671static int tas_usable(struct codec_info_item *cii,
672 struct transfer_info *ti,
673 struct transfer_info *out)
674{
675 return 1;
676}
677
678static int tas_reset_init(struct tas *tas)
679{
680 u8 tmp;
681
682 tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
683 msleep(5);
684 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
685 msleep(5);
686 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 1);
687 msleep(20);
688 tas->codec.gpio->methods->set_hw_reset(tas->codec.gpio, 0);
689 msleep(10);
690 tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
691
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))
694 goto outerr;
695
696 tas->acr |= TAS_ACR_ANALOG_PDOWN;
697 if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
698 goto outerr;
699
700 tmp = 0;
701 if (tas_write_reg(tas, TAS_REG_MCS2, 1, &tmp))
702 goto outerr;
703
704 tas3004_set_drc(tas);
705
706
707 tas->treble = TAS3004_TREBLE_ZERO;
708 tas->bass = TAS3004_BASS_ZERO;
709 tas_set_treble(tas);
710 tas_set_bass(tas);
711
712 tas->acr &= ~TAS_ACR_ANALOG_PDOWN;
713 if (tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr))
714 goto outerr;
715
716 return 0;
717 outerr:
718 return -ENODEV;
719}
720
721static int tas_switch_clock(struct codec_info_item *cii, enum clock_switch clock)
722{
723 struct tas *tas = cii->codec_data;
724
725 switch(clock) {
726 case CLOCK_SWITCH_PREPARE_SLAVE:
727
728 tas->codec.gpio->methods->all_amps_off(tas->codec.gpio);
729 tas->hw_enabled = 0;
730 break;
731 case CLOCK_SWITCH_SLAVE:
732
733 mutex_lock(&tas->mtx);
734 tas_reset_init(tas);
735 tas_set_volume(tas);
736 tas_set_mixer(tas);
737 tas->hw_enabled = 1;
738 tas->codec.gpio->methods->all_amps_restore(tas->codec.gpio);
739 mutex_unlock(&tas->mtx);
740 break;
741 default:
742
743 return -EINVAL;
744 }
745 return 0;
746}
747
748#ifdef CONFIG_PM
749
750
751
752static int tas_suspend(struct tas *tas)
753{
754 mutex_lock(&tas->mtx);
755 tas->hw_enabled = 0;
756 tas->acr |= TAS_ACR_ANALOG_PDOWN;
757 tas_write_reg(tas, TAS_REG_ACR, 1, &tas->acr);
758 mutex_unlock(&tas->mtx);
759 return 0;
760}
761
762static int tas_resume(struct tas *tas)
763{
764
765 mutex_lock(&tas->mtx);
766 tas_reset_init(tas);
767 tas_set_volume(tas);
768 tas_set_mixer(tas);
769 tas->hw_enabled = 1;
770 mutex_unlock(&tas->mtx);
771 return 0;
772}
773
774static int _tas_suspend(struct codec_info_item *cii, pm_message_t state)
775{
776 return tas_suspend(cii->codec_data);
777}
778
779static int _tas_resume(struct codec_info_item *cii)
780{
781 return tas_resume(cii->codec_data);
782}
783#else
784#define _tas_suspend NULL
785#define _tas_resume NULL
786#endif
787
788static struct codec_info tas_codec_info = {
789 .transfers = tas_transfers,
790
791
792
793 .sysclock_factor = 256,
794
795 .bus_factor = 64,
796 .owner = THIS_MODULE,
797 .usable = tas_usable,
798 .switch_clock = tas_switch_clock,
799 .suspend = _tas_suspend,
800 .resume = _tas_resume,
801};
802
803static int tas_init_codec(struct aoa_codec *codec)
804{
805 struct tas *tas = codec_to_tas(codec);
806 int err;
807
808 if (!tas->codec.gpio || !tas->codec.gpio->methods) {
809 printk(KERN_ERR PFX "gpios not assigned!!\n");
810 return -EINVAL;
811 }
812
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);
817 return -ENXIO;
818 }
819 tas->hw_enabled = 1;
820 mutex_unlock(&tas->mtx);
821
822 if (tas->codec.soundbus_dev->attach_codec(tas->codec.soundbus_dev,
823 aoa_get_card(),
824 &tas_codec_info, tas)) {
825 printk(KERN_ERR PFX "error attaching tas to soundbus\n");
826 return -ENODEV;
827 }
828
829 if (aoa_snd_device_new(SNDRV_DEV_LOWLEVEL, tas, &ops)) {
830 printk(KERN_ERR PFX "failed to create tas snd device!\n");
831 return -ENODEV;
832 }
833 err = aoa_snd_ctl_add(snd_ctl_new1(&volume_control, tas));
834 if (err)
835 goto error;
836
837 err = aoa_snd_ctl_add(snd_ctl_new1(&mute_control, tas));
838 if (err)
839 goto error;
840
841 err = aoa_snd_ctl_add(snd_ctl_new1(&pcm1_control, tas));
842 if (err)
843 goto error;
844
845 err = aoa_snd_ctl_add(snd_ctl_new1(&monitor_control, tas));
846 if (err)
847 goto error;
848
849 err = aoa_snd_ctl_add(snd_ctl_new1(&capture_source_control, tas));
850 if (err)
851 goto error;
852
853 err = aoa_snd_ctl_add(snd_ctl_new1(&drc_range_control, tas));
854 if (err)
855 goto error;
856
857 err = aoa_snd_ctl_add(snd_ctl_new1(&drc_switch_control, tas));
858 if (err)
859 goto error;
860
861 err = aoa_snd_ctl_add(snd_ctl_new1(&treble_control, tas));
862 if (err)
863 goto error;
864
865 err = aoa_snd_ctl_add(snd_ctl_new1(&bass_control, tas));
866 if (err)
867 goto error;
868
869 return 0;
870 error:
871 tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
872 snd_device_free(aoa_get_card(), tas);
873 return err;
874}
875
876static void tas_exit_codec(struct aoa_codec *codec)
877{
878 struct tas *tas = codec_to_tas(codec);
879
880 if (!tas->codec.soundbus_dev)
881 return;
882 tas->codec.soundbus_dev->detach_codec(tas->codec.soundbus_dev, tas);
883}
884
885
886static int tas_i2c_probe(struct i2c_client *client,
887 const struct i2c_device_id *id)
888{
889 struct device_node *node = client->dev.of_node;
890 struct tas *tas;
891
892 tas = kzalloc(sizeof(struct tas), GFP_KERNEL);
893
894 if (!tas)
895 return -ENOMEM;
896
897 mutex_init(&tas->mtx);
898 tas->i2c = client;
899 i2c_set_clientdata(client, tas);
900
901
902 tas->drc_range = TAS3004_DRC_MAX / 2;
903
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);
909
910 if (aoa_codec_register(&tas->codec)) {
911 goto fail;
912 }
913 printk(KERN_DEBUG
914 "snd-aoa-codec-tas: tas found, addr 0x%02x on %s\n",
915 (unsigned int)client->addr, node->full_name);
916 return 0;
917 fail:
918 mutex_destroy(&tas->mtx);
919 kfree(tas);
920 return -EINVAL;
921}
922
923static int tas_i2c_remove(struct i2c_client *client)
924{
925 struct tas *tas = i2c_get_clientdata(client);
926 u8 tmp = TAS_ACR_ANALOG_PDOWN;
927
928 aoa_codec_unregister(&tas->codec);
929 of_node_put(tas->codec.node);
930
931
932 tas_write_reg(tas, TAS_REG_ACR, 1, &tmp);
933
934 mutex_destroy(&tas->mtx);
935 kfree(tas);
936 return 0;
937}
938
939static const struct i2c_device_id tas_i2c_id[] = {
940 { "MAC,tas3004", 0 },
941 { }
942};
943MODULE_DEVICE_TABLE(i2c,tas_i2c_id);
944
945static struct i2c_driver tas_driver = {
946 .driver = {
947 .name = "aoa_codec_tas",
948 .owner = THIS_MODULE,
949 },
950 .probe = tas_i2c_probe,
951 .remove = tas_i2c_remove,
952 .id_table = tas_i2c_id,
953};
954
955module_i2c_driver(tas_driver);
956