1/*
2 *  Routines for control of the TEA6330T circuit via i2c bus
3 *  Sound fader control circuit for car radios by Philips Semiconductors
4 *  Copyright (c) by Jaroslav Kysela <perex@suse.cz>
5 *
6 *
7 *   This program is free software; you can redistribute it and/or modify
8 *   it under the terms of the GNU General Public License as published by
9 *   the Free Software Foundation; either version 2 of the License, or
10 *   (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
20 *
21 */
22
23#include <sound/driver.h>
24#include <linux/init.h>
25#include <linux/slab.h>
26#include <sound/core.h>
27#include <sound/control.h>
28#include <sound/tea6330t.h>
29
30MODULE_AUTHOR("Jaroslav Kysela <perex@suse.cz>");
31MODULE_DESCRIPTION("Routines for control of the TEA6330T circuit via i2c bus");
32MODULE_LICENSE("GPL");
33
34#define TEA6330T_ADDR			(0x80>>1) /* fixed address */
35
36#define TEA6330T_SADDR_VOLUME_LEFT	0x00	/* volume left */
37#define TEA6330T_SADDR_VOLUME_RIGHT	0x01	/* volume right */
38#define TEA6330T_SADDR_BASS		0x02	/* bass control */
39#define TEA6330T_SADDR_TREBLE		0x03	/* treble control */
40#define TEA6330T_SADDR_FADER		0x04	/* fader control */
41#define   TEA6330T_MFN			0x20	/* mute control for selected channels */
42#define   TEA6330T_FCH			0x10	/* select fader channels - front or rear */
43#define TEA6330T_SADDR_AUDIO_SWITCH	0x05	/* audio switch */
44#define   TEA6330T_GMU			0x80	/* mute control, general mute */
45#define   TEA6330T_EQN			0x40	/* equalizer switchover (0=equalizer-on) */
46
47
48struct tea6330t {
49	struct snd_i2c_device *device;
50	struct snd_i2c_bus *bus;
51	int equalizer;
52	int fader;
53	unsigned char regs[8];
54	unsigned char mleft, mright;
55	unsigned char bass, treble;
56	unsigned char max_bass, max_treble;
57};
58
59
60int snd_tea6330t_detect(struct snd_i2c_bus *bus, int equalizer)
61{
62	int res;
63
64	snd_i2c_lock(bus);
65	res = snd_i2c_probeaddr(bus, TEA6330T_ADDR);
66	snd_i2c_unlock(bus);
67	return res;
68}
69
70
71#define TEA6330T_MASTER_VOLUME(xname, xindex) \
72{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
73  .info = snd_tea6330t_info_master_volume, \
74  .get = snd_tea6330t_get_master_volume, .put = snd_tea6330t_put_master_volume }
75
76static int snd_tea6330t_info_master_volume(struct snd_kcontrol *kcontrol,
77					   struct snd_ctl_elem_info *uinfo)
78{
79	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
80	uinfo->count = 2;
81	uinfo->value.integer.min = 0;
82	uinfo->value.integer.max = 43;
83	return 0;
84}
85
86static int snd_tea6330t_get_master_volume(struct snd_kcontrol *kcontrol,
87					  struct snd_ctl_elem_value *ucontrol)
88{
89	struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
90
91	snd_i2c_lock(tea->bus);
92	ucontrol->value.integer.value[0] = tea->mleft - 0x14;
93	ucontrol->value.integer.value[1] = tea->mright - 0x14;
94	snd_i2c_unlock(tea->bus);
95	return 0;
96}
97
98static int snd_tea6330t_put_master_volume(struct snd_kcontrol *kcontrol,
99					  struct snd_ctl_elem_value *ucontrol)
100{
101	struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
102	int change, count, err;
103	unsigned char bytes[3];
104	unsigned char val1, val2;
105
106	val1 = (ucontrol->value.integer.value[0] % 44) + 0x14;
107	val2 = (ucontrol->value.integer.value[1] % 44) + 0x14;
108	snd_i2c_lock(tea->bus);
109	change = val1 != tea->mleft || val2 != tea->mright;
110	tea->mleft = val1;
111	tea->mright = val2;
112	count = 0;
113	if (tea->regs[TEA6330T_SADDR_VOLUME_LEFT] != 0) {
114		bytes[count++] = TEA6330T_SADDR_VOLUME_LEFT;
115		bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = tea->mleft;
116	}
117	if (tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] != 0) {
118		if (count == 0)
119			bytes[count++] = TEA6330T_SADDR_VOLUME_RIGHT;
120		bytes[count++] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = tea->mright;
121	}
122	if (count > 0) {
123		if ((err = snd_i2c_sendbytes(tea->device, bytes, count)) < 0)
124			change = err;
125	}
126	snd_i2c_unlock(tea->bus);
127	return change;
128}
129
130#define TEA6330T_MASTER_SWITCH(xname, xindex) \
131{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
132  .info = snd_tea6330t_info_master_switch, \
133  .get = snd_tea6330t_get_master_switch, .put = snd_tea6330t_put_master_switch }
134
135static int snd_tea6330t_info_master_switch(struct snd_kcontrol *kcontrol,
136					   struct snd_ctl_elem_info *uinfo)
137{
138	uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
139	uinfo->count = 2;
140	uinfo->value.integer.min = 0;
141	uinfo->value.integer.max = 1;
142	return 0;
143}
144
145static int snd_tea6330t_get_master_switch(struct snd_kcontrol *kcontrol,
146					  struct snd_ctl_elem_value *ucontrol)
147{
148	struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
149
150	snd_i2c_lock(tea->bus);
151	ucontrol->value.integer.value[0] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
152	ucontrol->value.integer.value[1] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
153	snd_i2c_unlock(tea->bus);
154	return 0;
155}
156
157static int snd_tea6330t_put_master_switch(struct snd_kcontrol *kcontrol,
158					  struct snd_ctl_elem_value *ucontrol)
159{
160	struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
161	int change, err;
162	unsigned char bytes[3];
163	unsigned char oval1, oval2, val1, val2;
164
165	val1 = ucontrol->value.integer.value[0] & 1;
166	val2 = ucontrol->value.integer.value[1] & 1;
167	snd_i2c_lock(tea->bus);
168	oval1 = tea->regs[TEA6330T_SADDR_VOLUME_LEFT] == 0 ? 0 : 1;
169	oval2 = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] == 0 ? 0 : 1;
170	change = val1 != oval1 || val2 != oval2;
171	tea->regs[TEA6330T_SADDR_VOLUME_LEFT] = val1 ? tea->mleft : 0;
172	tea->regs[TEA6330T_SADDR_VOLUME_RIGHT] = val2 ? tea->mright : 0;
173	bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
174	bytes[1] = tea->regs[TEA6330T_SADDR_VOLUME_LEFT];
175	bytes[2] = tea->regs[TEA6330T_SADDR_VOLUME_RIGHT];
176	if ((err = snd_i2c_sendbytes(tea->device, bytes, 3)) < 0)
177		change = err;
178	snd_i2c_unlock(tea->bus);
179	return change;
180}
181
182#define TEA6330T_BASS(xname, xindex) \
183{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
184  .info = snd_tea6330t_info_bass, \
185  .get = snd_tea6330t_get_bass, .put = snd_tea6330t_put_bass }
186
187static int snd_tea6330t_info_bass(struct snd_kcontrol *kcontrol,
188				  struct snd_ctl_elem_info *uinfo)
189{
190	struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
191
192	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
193	uinfo->count = 1;
194	uinfo->value.integer.min = 0;
195	uinfo->value.integer.max = tea->max_bass;
196	return 0;
197}
198
199static int snd_tea6330t_get_bass(struct snd_kcontrol *kcontrol,
200				 struct snd_ctl_elem_value *ucontrol)
201{
202	struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
203
204	ucontrol->value.integer.value[0] = tea->bass;
205	return 0;
206}
207
208static int snd_tea6330t_put_bass(struct snd_kcontrol *kcontrol,
209				 struct snd_ctl_elem_value *ucontrol)
210{
211	struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
212	int change, err;
213	unsigned char bytes[2];
214	unsigned char val1;
215
216	val1 = ucontrol->value.integer.value[0] % (tea->max_bass + 1);
217	snd_i2c_lock(tea->bus);
218	tea->bass = val1;
219	val1 += tea->equalizer ? 7 : 3;
220	change = tea->regs[TEA6330T_SADDR_BASS] != val1;
221	bytes[0] = TEA6330T_SADDR_BASS;
222	bytes[1] = tea->regs[TEA6330T_SADDR_BASS] = val1;
223	if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
224		change = err;
225	snd_i2c_unlock(tea->bus);
226	return change;
227}
228
229#define TEA6330T_TREBLE(xname, xindex) \
230{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, .index = xindex, \
231  .info = snd_tea6330t_info_treble, \
232  .get = snd_tea6330t_get_treble, .put = snd_tea6330t_put_treble }
233
234static int snd_tea6330t_info_treble(struct snd_kcontrol *kcontrol,
235				    struct snd_ctl_elem_info *uinfo)
236{
237	struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
238
239	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
240	uinfo->count = 1;
241	uinfo->value.integer.min = 0;
242	uinfo->value.integer.max = tea->max_treble;
243	return 0;
244}
245
246static int snd_tea6330t_get_treble(struct snd_kcontrol *kcontrol,
247				   struct snd_ctl_elem_value *ucontrol)
248{
249	struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
250
251	ucontrol->value.integer.value[0] = tea->treble;
252	return 0;
253}
254
255static int snd_tea6330t_put_treble(struct snd_kcontrol *kcontrol,
256				   struct snd_ctl_elem_value *ucontrol)
257{
258	struct tea6330t *tea = snd_kcontrol_chip(kcontrol);
259	int change, err;
260	unsigned char bytes[2];
261	unsigned char val1;
262
263	val1 = ucontrol->value.integer.value[0] % (tea->max_treble + 1);
264	snd_i2c_lock(tea->bus);
265	tea->treble = val1;
266	val1 += 3;
267	change = tea->regs[TEA6330T_SADDR_TREBLE] != val1;
268	bytes[0] = TEA6330T_SADDR_TREBLE;
269	bytes[1] = tea->regs[TEA6330T_SADDR_TREBLE] = val1;
270	if ((err = snd_i2c_sendbytes(tea->device, bytes, 2)) < 0)
271		change = err;
272	snd_i2c_unlock(tea->bus);
273	return change;
274}
275
276static struct snd_kcontrol_new snd_tea6330t_controls[] = {
277TEA6330T_MASTER_SWITCH("Master Playback Switch", 0),
278TEA6330T_MASTER_VOLUME("Master Playback Volume", 0),
279TEA6330T_BASS("Tone Control - Bass", 0),
280TEA6330T_TREBLE("Tone Control - Treble", 0)
281};
282
283static void snd_tea6330_free(struct snd_i2c_device *device)
284{
285	kfree(device->private_data);
286}
287
288int snd_tea6330t_update_mixer(struct snd_card *card,
289			      struct snd_i2c_bus *bus,
290			      int equalizer, int fader)
291{
292	struct snd_i2c_device *device;
293	struct tea6330t *tea;
294	struct snd_kcontrol_new *knew;
295	unsigned int idx;
296	int err = -ENOMEM;
297	u8 default_treble, default_bass;
298	unsigned char bytes[7];
299
300	tea = kzalloc(sizeof(*tea), GFP_KERNEL);
301	if (tea == NULL)
302		return -ENOMEM;
303	if ((err = snd_i2c_device_create(bus, "TEA6330T", TEA6330T_ADDR, &device)) < 0) {
304		kfree(tea);
305		return err;
306	}
307	tea->device = device;
308	tea->bus = bus;
309	tea->equalizer = equalizer;
310	tea->fader = fader;
311	device->private_data = tea;
312	device->private_free = snd_tea6330_free;
313
314	snd_i2c_lock(bus);
315
316	/* turn fader off and handle equalizer */
317	tea->regs[TEA6330T_SADDR_FADER] = 0x3f;
318	tea->regs[TEA6330T_SADDR_AUDIO_SWITCH] = equalizer ? 0 : TEA6330T_EQN;
319	/* initialize mixer */
320	if (!tea->equalizer) {
321		tea->max_bass = 9;
322		tea->max_treble = 8;
323		default_bass = 3 + 4;
324		tea->bass = 4;
325		default_treble = 3 + 4;
326		tea->treble = 4;
327	} else {
328		tea->max_bass = 5;
329		tea->max_treble = 0;
330		default_bass = 7 + 4;
331		tea->bass = 4;
332		default_treble = 3;
333		tea->treble = 0;
334	}
335	tea->mleft = tea->mright = 0x14;
336	tea->regs[TEA6330T_SADDR_BASS] = default_bass;
337	tea->regs[TEA6330T_SADDR_TREBLE] = default_treble;
338
339	/* compose I2C message and put the hardware to initial state */
340	bytes[0] = TEA6330T_SADDR_VOLUME_LEFT;
341	for (idx = 0; idx < 6; idx++)
342		bytes[idx+1] = tea->regs[idx];
343	if ((err = snd_i2c_sendbytes(device, bytes, 7)) < 0)
344		goto __error;
345
346	strcat(card->mixername, ",TEA6330T");
347	if ((err = snd_component_add(card, "TEA6330T")) < 0)
348		goto __error;
349
350	for (idx = 0; idx < ARRAY_SIZE(snd_tea6330t_controls); idx++) {
351		knew = &snd_tea6330t_controls[idx];
352		if (tea->treble == 0 && !strcmp(knew->name, "Tone Control - Treble"))
353			continue;
354		if ((err = snd_ctl_add(card, snd_ctl_new1(knew, tea))) < 0)
355			goto __error;
356	}
357
358	snd_i2c_unlock(bus);
359	return 0;
360
361      __error:
362      	snd_i2c_unlock(bus);
363      	snd_i2c_device_free(device);
364      	return err;
365}
366
367EXPORT_SYMBOL(snd_tea6330t_detect);
368EXPORT_SYMBOL(snd_tea6330t_update_mixer);
369
370/*
371 *  INIT part
372 */
373
374static int __init alsa_tea6330t_init(void)
375{
376	return 0;
377}
378
379static void __exit alsa_tea6330t_exit(void)
380{
381}
382
383module_init(alsa_tea6330t_init)
384module_exit(alsa_tea6330t_exit)
385