File Index
Files and directories
Sign in
FILE

tas.c

File preview with syntax highlighting.

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