MAINTAINERS & files: Canonize the e-mails I use at files
[linux-2.6-block.git] / drivers / media / i2c / tda7432.c
CommitLineData
1da177e4
LT
1/*
2 * For the STS-Thompson TDA7432 audio processor chip
3 *
4 * Handles audio functions: volume, balance, tone, loudness
5 * This driver will not complain if used with any
6 * other i2c device with the same address.
7 *
8 * Muting and tone control by Jonathan Isom <jisom@ematic.com>
9 *
10 * Copyright (c) 2000 Eric Sandeen <eric_sandeen@bigfoot.com>
32590819 11 * Copyright (c) 2006 Mauro Carvalho Chehab <mchehab@kernel.org>
1da177e4
LT
12 * This code is placed under the terms of the GNU General Public License
13 * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
14 * Which was based on tda8425.c by Greg Alexander (c) 1998
15 *
16 * OPTIONS:
17 * debug - set to 1 if you'd like to see debug messages
18 * set to 2 if you'd like to be inundated with debug messages
19 *
20 * loudness - set between 0 and 15 for varying degrees of loudness effect
21 *
22 * maxvol - set maximium volume to +20db (1), default is 0db(0)
1da177e4
LT
23 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/kernel.h>
1da177e4
LT
28#include <linux/string.h>
29#include <linux/timer.h>
30#include <linux/delay.h>
31#include <linux/errno.h>
32#include <linux/slab.h>
33b687cf 33#include <linux/videodev2.h>
1da177e4 34#include <linux/i2c.h>
1da177e4 35
d531305a 36#include <media/v4l2-device.h>
35ea11ff 37#include <media/v4l2-ioctl.h>
5d478e0d 38#include <media/v4l2-ctrls.h>
1da177e4
LT
39
40#ifndef VIDEO_AUDIO_BALANCE
41# define VIDEO_AUDIO_BALANCE 32
42#endif
43
44MODULE_AUTHOR("Eric Sandeen <eric_sandeen@bigfoot.com>");
45MODULE_DESCRIPTION("bttv driver for the tda7432 audio processor chip");
46MODULE_LICENSE("GPL");
47
48static int maxvol;
49static int loudness; /* disable loudness by default */
50static int debug; /* insmod parameter */
51module_param(debug, int, S_IRUGO | S_IWUSR);
7ce338d9 52MODULE_PARM_DESC(debug, "Set debugging level from 0 to 3. Default is off(0).");
1da177e4 53module_param(loudness, int, S_IRUGO);
7ce338d9 54MODULE_PARM_DESC(loudness, "Turn loudness on(1) else off(0). Default is off(0).");
1da177e4 55module_param(maxvol, int, S_IRUGO | S_IWUSR);
7ce338d9 56MODULE_PARM_DESC(maxvol, "Set maximium volume to +20dB(0) else +0dB(1). Default is +20dB(0).");
1da177e4 57
1da177e4
LT
58
59/* Structure of address and subaddresses for the tda7432 */
60
61struct tda7432 {
d531305a 62 struct v4l2_subdev sd;
5d478e0d
HV
63 struct v4l2_ctrl_handler hdl;
64 struct {
65 /* bass/treble cluster */
66 struct v4l2_ctrl *bass;
67 struct v4l2_ctrl *treble;
68 };
69 struct {
70 /* mute/balance cluster */
71 struct v4l2_ctrl *mute;
72 struct v4l2_ctrl *balance;
73 };
1da177e4 74};
d531305a
HV
75
76static inline struct tda7432 *to_state(struct v4l2_subdev *sd)
77{
78 return container_of(sd, struct tda7432, sd);
79}
1da177e4 80
5d478e0d
HV
81static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
82{
83 return &container_of(ctrl->handler, struct tda7432, hdl)->sd;
84}
85
1da177e4
LT
86/* The TDA7432 is made by STS-Thompson
87 * http://www.st.com
88 * http://us.st.com/stonline/books/pdf/docs/4056.pdf
89 *
90 * TDA7432: I2C-bus controlled basic audio processor
91 *
92 * The TDA7432 controls basic audio functions like volume, balance,
93 * and tone control (including loudness). It also has four channel
94 * output (for front and rear). Since most vidcap cards probably
95 * don't have 4 channel output, this driver will set front & rear
96 * together (no independent control).
97 */
98
99 /* Subaddresses for TDA7432 */
100
101#define TDA7432_IN 0x00 /* Input select */
102#define TDA7432_VL 0x01 /* Volume */
103#define TDA7432_TN 0x02 /* Bass, Treble (Tone) */
104#define TDA7432_LF 0x03 /* Attenuation LF (Left Front) */
105#define TDA7432_LR 0x04 /* Attenuation LR (Left Rear) */
106#define TDA7432_RF 0x05 /* Attenuation RF (Right Front) */
107#define TDA7432_RR 0x06 /* Attenuation RR (Right Rear) */
108#define TDA7432_LD 0x07 /* Loudness */
109
110
111 /* Masks for bits in TDA7432 subaddresses */
112
113/* Many of these not used - just for documentation */
114
115/* Subaddress 0x00 - Input selection and bass control */
116
117/* Bits 0,1,2 control input:
118 * 0x00 - Stereo input
119 * 0x02 - Mono input
120 * 0x03 - Mute (Using Attenuators Plays better with modules)
121 * Mono probably isn't used - I'm guessing only the stereo
122 * input is connected on most cards, so we'll set it to stereo.
123 *
124 * Bit 3 controls bass cut: 0/1 is non-symmetric/symmetric bass cut
125 * Bit 4 controls bass range: 0/1 is extended/standard bass range
126 *
127 * Highest 3 bits not used
128 */
129
130#define TDA7432_STEREO_IN 0
131#define TDA7432_MONO_IN 2 /* Probably won't be used */
132#define TDA7432_BASS_SYM 1 << 3
133#define TDA7432_BASS_NORM 1 << 4
134
135/* Subaddress 0x01 - Volume */
136
137/* Lower 7 bits control volume from -79dB to +32dB in 1dB steps
138 * Recommended maximum is +20 dB
139 *
140 * +32dB: 0x00
141 * +20dB: 0x0c
142 * 0dB: 0x20
143 * -79dB: 0x6f
144 *
145 * MSB (bit 7) controls loudness: 1/0 is loudness on/off
146 */
147
148#define TDA7432_VOL_0DB 0x20
149#define TDA7432_LD_ON 1 << 7
150
151
152/* Subaddress 0x02 - Tone control */
153
154/* Bits 0,1,2 control absolute treble gain from 0dB to 14dB
155 * 0x0 is 14dB, 0x7 is 0dB
156 *
157 * Bit 3 controls treble attenuation/gain (sign)
158 * 1 = gain (+)
159 * 0 = attenuation (-)
160 *
161 * Bits 4,5,6 control absolute bass gain from 0dB to 14dB
162 * (This is only true for normal base range, set in 0x00)
163 * 0x0 << 4 is 14dB, 0x7 is 0dB
164 *
165 * Bit 7 controls bass attenuation/gain (sign)
166 * 1 << 7 = gain (+)
167 * 0 << 7 = attenuation (-)
168 *
169 * Example:
170 * 1 1 0 1 0 1 0 1 is +4dB bass, -4dB treble
171 */
172
173#define TDA7432_TREBLE_0DB 0xf
174#define TDA7432_TREBLE 7
175#define TDA7432_TREBLE_GAIN 1 << 3
176#define TDA7432_BASS_0DB 0xf
177#define TDA7432_BASS 7 << 4
178#define TDA7432_BASS_GAIN 1 << 7
179
180
181/* Subaddress 0x03 - Left Front attenuation */
182/* Subaddress 0x04 - Left Rear attenuation */
183/* Subaddress 0x05 - Right Front attenuation */
184/* Subaddress 0x06 - Right Rear attenuation */
185
186/* Bits 0,1,2,3,4 control attenuation from 0dB to -37.5dB
187 * in 1.5dB steps.
188 *
189 * 0x00 is 0dB
190 * 0x1f is -37.5dB
191 *
192 * Bit 5 mutes that channel when set (1 = mute, 0 = unmute)
193 * We'll use the mute on the input, though (above)
194 * Bits 6,7 unused
195 */
196
197#define TDA7432_ATTEN_0DB 0x00
198#define TDA7432_MUTE 0x1 << 5
199
200
201/* Subaddress 0x07 - Loudness Control */
202
203/* Bits 0,1,2,3 control loudness from 0dB to -15dB in 1dB steps
204 * when bit 4 is NOT set
205 *
206 * 0x0 is 0dB
207 * 0xf is -15dB
208 *
209 * If bit 4 is set, then there is a flat attenuation according to
210 * the lower 4 bits, as above.
211 *
212 * Bits 5,6,7 unused
213 */
214
215
216
217/* Begin code */
218
d531305a 219static int tda7432_write(struct v4l2_subdev *sd, int subaddr, int val)
1da177e4 220{
d531305a 221 struct i2c_client *client = v4l2_get_subdevdata(sd);
1da177e4 222 unsigned char buffer[2];
d531305a
HV
223
224 v4l2_dbg(2, debug, sd, "In tda7432_write\n");
225 v4l2_dbg(1, debug, sd, "Writing %d 0x%x\n", subaddr, val);
1da177e4
LT
226 buffer[0] = subaddr;
227 buffer[1] = val;
d531305a
HV
228 if (2 != i2c_master_send(client, buffer, 2)) {
229 v4l2_err(sd, "I/O error, trying (write %d 0x%x)\n",
1da177e4
LT
230 subaddr, val);
231 return -1;
232 }
233 return 0;
234}
235
d531305a 236static int tda7432_set(struct v4l2_subdev *sd)
1da177e4 237{
d531305a 238 struct i2c_client *client = v4l2_get_subdevdata(sd);
1da177e4 239 unsigned char buf[16];
1da177e4 240
1da177e4 241 buf[0] = TDA7432_IN;
5d478e0d
HV
242 buf[1] = TDA7432_STEREO_IN | /* Main (stereo) input */
243 TDA7432_BASS_SYM | /* Symmetric bass cut */
244 TDA7432_BASS_NORM; /* Normal bass range */
245 buf[2] = 0x3b;
246 if (loudness) /* Turn loudness on? */
247 buf[2] |= TDA7432_LD_ON;
248 buf[3] = TDA7432_TREBLE_0DB | (TDA7432_BASS_0DB << 4);
249 buf[4] = TDA7432_ATTEN_0DB;
250 buf[5] = TDA7432_ATTEN_0DB;
251 buf[6] = TDA7432_ATTEN_0DB;
252 buf[7] = TDA7432_ATTEN_0DB;
253 buf[8] = loudness;
254 if (9 != i2c_master_send(client, buf, 9)) {
d531305a 255 v4l2_err(sd, "I/O error, trying tda7432_set\n");
1da177e4
LT
256 return -1;
257 }
258
259 return 0;
260}
261
5d478e0d 262static int tda7432_log_status(struct v4l2_subdev *sd)
1da177e4 263{
5d478e0d 264 struct tda7432 *state = to_state(sd);
1da177e4 265
5d478e0d
HV
266 v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
267 return 0;
7a00d45c 268}
1da177e4 269
5d478e0d 270static int tda7432_s_ctrl(struct v4l2_ctrl *ctrl)
7a00d45c 271{
5d478e0d 272 struct v4l2_subdev *sd = to_sd(ctrl);
d531305a 273 struct tda7432 *t = to_state(sd);
5d478e0d
HV
274 u8 bass, treble, volume;
275 u8 lf, lr, rf, rr;
1da177e4 276
7a00d45c
MCC
277 switch (ctrl->id) {
278 case V4L2_CID_AUDIO_MUTE:
5d478e0d 279 if (t->balance->val < 0) {
1da177e4 280 /* shifted to left, attenuate right */
5d478e0d
HV
281 rr = rf = -t->balance->val;
282 lr = lf = TDA7432_ATTEN_0DB;
283 } else if (t->balance->val > 0) {
1da177e4 284 /* shifted to right, attenuate left */
5d478e0d
HV
285 rr = rf = TDA7432_ATTEN_0DB;
286 lr = lf = t->balance->val;
7a00d45c 287 } else {
1da177e4 288 /* centered */
5d478e0d
HV
289 rr = rf = TDA7432_ATTEN_0DB;
290 lr = lf = TDA7432_ATTEN_0DB;
1da177e4 291 }
5d478e0d
HV
292 if (t->mute->val) {
293 lf |= TDA7432_MUTE;
294 lr |= TDA7432_MUTE;
91ba0e59 295 rf |= TDA7432_MUTE;
5d478e0d
HV
296 rr |= TDA7432_MUTE;
297 }
298 /* Mute & update balance*/
299 tda7432_write(sd, TDA7432_LF, lf);
300 tda7432_write(sd, TDA7432_LR, lr);
301 tda7432_write(sd, TDA7432_RF, rf);
302 tda7432_write(sd, TDA7432_RR, rr);
7a00d45c 303 return 0;
5d478e0d
HV
304 case V4L2_CID_AUDIO_VOLUME:
305 volume = 0x6f - ctrl->val;
306 if (loudness) /* Turn on the loudness bit */
307 volume |= TDA7432_LD_ON;
7a00d45c 308
5d478e0d 309 tda7432_write(sd, TDA7432_VL, volume);
7a00d45c 310 return 0;
d531305a 311 case V4L2_CID_AUDIO_BASS:
5d478e0d
HV
312 bass = t->bass->val;
313 treble = t->treble->val;
314 if (bass >= 0x8)
315 bass = 14 - (bass - 8);
316 if (treble >= 0x8)
317 treble = 14 - (treble - 8);
318
319 tda7432_write(sd, TDA7432_TN, 0x10 | (bass << 4) | treble);
320 return 0;
7a00d45c 321 }
d531305a
HV
322 return -EINVAL;
323}
1da177e4 324
d531305a
HV
325/* ----------------------------------------------------------------------- */
326
5d478e0d 327static const struct v4l2_ctrl_ops tda7432_ctrl_ops = {
d531305a 328 .s_ctrl = tda7432_s_ctrl,
1da177e4
LT
329};
330
5d478e0d
HV
331static const struct v4l2_subdev_core_ops tda7432_core_ops = {
332 .log_status = tda7432_log_status,
5d478e0d
HV
333};
334
d531305a
HV
335static const struct v4l2_subdev_ops tda7432_ops = {
336 .core = &tda7432_core_ops,
1da177e4
LT
337};
338
d531305a
HV
339/* ----------------------------------------------------------------------- */
340
341/* *********************** *
342 * i2c interface functions *
343 * *********************** */
344
345static int tda7432_probe(struct i2c_client *client,
346 const struct i2c_device_id *id)
1da177e4 347{
d531305a
HV
348 struct tda7432 *t;
349 struct v4l2_subdev *sd;
350
351 v4l_info(client, "chip found @ 0x%02x (%s)\n",
352 client->addr << 1, client->adapter->name);
353
c02b211d 354 t = devm_kzalloc(&client->dev, sizeof(*t), GFP_KERNEL);
d531305a
HV
355 if (!t)
356 return -ENOMEM;
357 sd = &t->sd;
358 v4l2_i2c_subdev_init(sd, client, &tda7432_ops);
5d478e0d
HV
359 v4l2_ctrl_handler_init(&t->hdl, 5);
360 v4l2_ctrl_new_std(&t->hdl, &tda7432_ctrl_ops,
361 V4L2_CID_AUDIO_VOLUME, 0, maxvol ? 0x68 : 0x4f, 1, maxvol ? 0x5d : 0x47);
362 t->mute = v4l2_ctrl_new_std(&t->hdl, &tda7432_ctrl_ops,
363 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
364 t->balance = v4l2_ctrl_new_std(&t->hdl, &tda7432_ctrl_ops,
365 V4L2_CID_AUDIO_BALANCE, -31, 31, 1, 0);
366 t->bass = v4l2_ctrl_new_std(&t->hdl, &tda7432_ctrl_ops,
367 V4L2_CID_AUDIO_BASS, 0, 14, 1, 7);
368 t->treble = v4l2_ctrl_new_std(&t->hdl, &tda7432_ctrl_ops,
369 V4L2_CID_AUDIO_TREBLE, 0, 14, 1, 7);
370 sd->ctrl_handler = &t->hdl;
371 if (t->hdl.error) {
372 int err = t->hdl.error;
373
374 v4l2_ctrl_handler_free(&t->hdl);
5d478e0d
HV
375 return err;
376 }
377 v4l2_ctrl_cluster(2, &t->bass);
378 v4l2_ctrl_cluster(2, &t->mute);
379 v4l2_ctrl_handler_setup(&t->hdl);
d531305a
HV
380 if (loudness < 0 || loudness > 15) {
381 v4l2_warn(sd, "loudness parameter must be between 0 and 15\n");
382 if (loudness < 0)
383 loudness = 0;
384 if (loudness > 15)
385 loudness = 15;
1da177e4
LT
386 }
387
5d478e0d 388 tda7432_set(sd);
d531305a 389 return 0;
1da177e4
LT
390}
391
d531305a 392static int tda7432_remove(struct i2c_client *client)
1da177e4 393{
d531305a 394 struct v4l2_subdev *sd = i2c_get_clientdata(client);
5d478e0d 395 struct tda7432 *t = to_state(sd);
1da177e4 396
5d478e0d 397 tda7432_set(sd);
d531305a 398 v4l2_device_unregister_subdev(sd);
5d478e0d 399 v4l2_ctrl_handler_free(&t->hdl);
d531305a
HV
400 return 0;
401}
1da177e4 402
d531305a
HV
403static const struct i2c_device_id tda7432_id[] = {
404 { "tda7432", 0 },
405 { }
406};
407MODULE_DEVICE_TABLE(i2c, tda7432_id);
408
5b9f80af
HV
409static struct i2c_driver tda7432_driver = {
410 .driver = {
5b9f80af
HV
411 .name = "tda7432",
412 },
413 .probe = tda7432_probe,
414 .remove = tda7432_remove,
415 .id_table = tda7432_id,
d531305a 416};
5b9f80af 417
c6e8d86f 418module_i2c_driver(tda7432_driver);