Merge branch 'topic/hda' into for-linus
[linux-2.6-block.git] / drivers / media / video / 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>
7a00d45c 11 * Copyright (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.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)
23 *
24 *
25 * Revision: 0.7 - maxvol module parm to set maximium volume 0db or +20db
26 * store if muted so we can return it
27 * change balance only if flaged to
28 * Revision: 0.6 - added tone controls
29 * Revision: 0.5 - Fixed odd balance problem
30 * Revision: 0.4 - added muting
31 * Revision: 0.3 - Fixed silly reversed volume controls. :)
32 * Revision: 0.2 - Cleaned up #defines
33 * fixed volume control
34 * Added I2C_DRIVERID_TDA7432
35 * added loudness insmod control
36 * Revision: 0.1 - initial version
37 */
38
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/kernel.h>
1da177e4
LT
42#include <linux/string.h>
43#include <linux/timer.h>
44#include <linux/delay.h>
45#include <linux/errno.h>
46#include <linux/slab.h>
33b687cf 47#include <linux/videodev2.h>
1da177e4 48#include <linux/i2c.h>
1da177e4 49
d531305a 50#include <media/v4l2-device.h>
35ea11ff 51#include <media/v4l2-ioctl.h>
fa3fcceb 52#include <media/i2c-addr.h>
267ea2a9 53#include <media/v4l2-i2c-drv.h>
1da177e4
LT
54
55#ifndef VIDEO_AUDIO_BALANCE
56# define VIDEO_AUDIO_BALANCE 32
57#endif
58
59MODULE_AUTHOR("Eric Sandeen <eric_sandeen@bigfoot.com>");
60MODULE_DESCRIPTION("bttv driver for the tda7432 audio processor chip");
61MODULE_LICENSE("GPL");
62
63static int maxvol;
64static int loudness; /* disable loudness by default */
65static int debug; /* insmod parameter */
66module_param(debug, int, S_IRUGO | S_IWUSR);
67module_param(loudness, int, S_IRUGO);
68MODULE_PARM_DESC(maxvol,"Set maximium volume to +20db (0), default is 0db(1)");
69module_param(maxvol, int, S_IRUGO | S_IWUSR);
70
71
1da177e4
LT
72
73/* Structure of address and subaddresses for the tda7432 */
74
75struct tda7432 {
d531305a 76 struct v4l2_subdev sd;
1da177e4
LT
77 int addr;
78 int input;
79 int volume;
80 int muted;
81 int bass, treble;
82 int lf, lr, rf, rr;
83 int loud;
1da177e4 84};
d531305a
HV
85
86static inline struct tda7432 *to_state(struct v4l2_subdev *sd)
87{
88 return container_of(sd, struct tda7432, sd);
89}
1da177e4 90
1da177e4
LT
91/* The TDA7432 is made by STS-Thompson
92 * http://www.st.com
93 * http://us.st.com/stonline/books/pdf/docs/4056.pdf
94 *
95 * TDA7432: I2C-bus controlled basic audio processor
96 *
97 * The TDA7432 controls basic audio functions like volume, balance,
98 * and tone control (including loudness). It also has four channel
99 * output (for front and rear). Since most vidcap cards probably
100 * don't have 4 channel output, this driver will set front & rear
101 * together (no independent control).
102 */
103
104 /* Subaddresses for TDA7432 */
105
106#define TDA7432_IN 0x00 /* Input select */
107#define TDA7432_VL 0x01 /* Volume */
108#define TDA7432_TN 0x02 /* Bass, Treble (Tone) */
109#define TDA7432_LF 0x03 /* Attenuation LF (Left Front) */
110#define TDA7432_LR 0x04 /* Attenuation LR (Left Rear) */
111#define TDA7432_RF 0x05 /* Attenuation RF (Right Front) */
112#define TDA7432_RR 0x06 /* Attenuation RR (Right Rear) */
113#define TDA7432_LD 0x07 /* Loudness */
114
115
116 /* Masks for bits in TDA7432 subaddresses */
117
118/* Many of these not used - just for documentation */
119
120/* Subaddress 0x00 - Input selection and bass control */
121
122/* Bits 0,1,2 control input:
123 * 0x00 - Stereo input
124 * 0x02 - Mono input
125 * 0x03 - Mute (Using Attenuators Plays better with modules)
126 * Mono probably isn't used - I'm guessing only the stereo
127 * input is connected on most cards, so we'll set it to stereo.
128 *
129 * Bit 3 controls bass cut: 0/1 is non-symmetric/symmetric bass cut
130 * Bit 4 controls bass range: 0/1 is extended/standard bass range
131 *
132 * Highest 3 bits not used
133 */
134
135#define TDA7432_STEREO_IN 0
136#define TDA7432_MONO_IN 2 /* Probably won't be used */
137#define TDA7432_BASS_SYM 1 << 3
138#define TDA7432_BASS_NORM 1 << 4
139
140/* Subaddress 0x01 - Volume */
141
142/* Lower 7 bits control volume from -79dB to +32dB in 1dB steps
143 * Recommended maximum is +20 dB
144 *
145 * +32dB: 0x00
146 * +20dB: 0x0c
147 * 0dB: 0x20
148 * -79dB: 0x6f
149 *
150 * MSB (bit 7) controls loudness: 1/0 is loudness on/off
151 */
152
153#define TDA7432_VOL_0DB 0x20
154#define TDA7432_LD_ON 1 << 7
155
156
157/* Subaddress 0x02 - Tone control */
158
159/* Bits 0,1,2 control absolute treble gain from 0dB to 14dB
160 * 0x0 is 14dB, 0x7 is 0dB
161 *
162 * Bit 3 controls treble attenuation/gain (sign)
163 * 1 = gain (+)
164 * 0 = attenuation (-)
165 *
166 * Bits 4,5,6 control absolute bass gain from 0dB to 14dB
167 * (This is only true for normal base range, set in 0x00)
168 * 0x0 << 4 is 14dB, 0x7 is 0dB
169 *
170 * Bit 7 controls bass attenuation/gain (sign)
171 * 1 << 7 = gain (+)
172 * 0 << 7 = attenuation (-)
173 *
174 * Example:
175 * 1 1 0 1 0 1 0 1 is +4dB bass, -4dB treble
176 */
177
178#define TDA7432_TREBLE_0DB 0xf
179#define TDA7432_TREBLE 7
180#define TDA7432_TREBLE_GAIN 1 << 3
181#define TDA7432_BASS_0DB 0xf
182#define TDA7432_BASS 7 << 4
183#define TDA7432_BASS_GAIN 1 << 7
184
185
186/* Subaddress 0x03 - Left Front attenuation */
187/* Subaddress 0x04 - Left Rear attenuation */
188/* Subaddress 0x05 - Right Front attenuation */
189/* Subaddress 0x06 - Right Rear attenuation */
190
191/* Bits 0,1,2,3,4 control attenuation from 0dB to -37.5dB
192 * in 1.5dB steps.
193 *
194 * 0x00 is 0dB
195 * 0x1f is -37.5dB
196 *
197 * Bit 5 mutes that channel when set (1 = mute, 0 = unmute)
198 * We'll use the mute on the input, though (above)
199 * Bits 6,7 unused
200 */
201
202#define TDA7432_ATTEN_0DB 0x00
203#define TDA7432_MUTE 0x1 << 5
204
205
206/* Subaddress 0x07 - Loudness Control */
207
208/* Bits 0,1,2,3 control loudness from 0dB to -15dB in 1dB steps
209 * when bit 4 is NOT set
210 *
211 * 0x0 is 0dB
212 * 0xf is -15dB
213 *
214 * If bit 4 is set, then there is a flat attenuation according to
215 * the lower 4 bits, as above.
216 *
217 * Bits 5,6,7 unused
218 */
219
220
221
222/* Begin code */
223
d531305a 224static int tda7432_write(struct v4l2_subdev *sd, int subaddr, int val)
1da177e4 225{
d531305a 226 struct i2c_client *client = v4l2_get_subdevdata(sd);
1da177e4 227 unsigned char buffer[2];
d531305a
HV
228
229 v4l2_dbg(2, debug, sd, "In tda7432_write\n");
230 v4l2_dbg(1, debug, sd, "Writing %d 0x%x\n", subaddr, val);
1da177e4
LT
231 buffer[0] = subaddr;
232 buffer[1] = val;
d531305a
HV
233 if (2 != i2c_master_send(client, buffer, 2)) {
234 v4l2_err(sd, "I/O error, trying (write %d 0x%x)\n",
1da177e4
LT
235 subaddr, val);
236 return -1;
237 }
238 return 0;
239}
240
d531305a 241static int tda7432_set(struct v4l2_subdev *sd)
1da177e4 242{
d531305a
HV
243 struct i2c_client *client = v4l2_get_subdevdata(sd);
244 struct tda7432 *t = to_state(sd);
1da177e4 245 unsigned char buf[16];
1da177e4 246
d531305a 247 v4l2_dbg(1, debug, sd,
1da177e4 248 "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
d531305a
HV
249 t->input, t->volume, t->bass, t->treble, t->lf, t->lr,
250 t->rf, t->rr, t->loud);
1da177e4
LT
251 buf[0] = TDA7432_IN;
252 buf[1] = t->input;
253 buf[2] = t->volume;
254 buf[3] = t->bass;
255 buf[4] = t->treble;
256 buf[5] = t->lf;
257 buf[6] = t->lr;
258 buf[7] = t->rf;
259 buf[8] = t->rr;
260 buf[9] = t->loud;
d531305a
HV
261 if (10 != i2c_master_send(client, buf, 10)) {
262 v4l2_err(sd, "I/O error, trying tda7432_set\n");
1da177e4
LT
263 return -1;
264 }
265
266 return 0;
267}
268
d531305a 269static void do_tda7432_init(struct v4l2_subdev *sd)
1da177e4 270{
d531305a
HV
271 struct tda7432 *t = to_state(sd);
272
273 v4l2_dbg(2, debug, sd, "In tda7432_init\n");
1da177e4
LT
274
275 t->input = TDA7432_STEREO_IN | /* Main (stereo) input */
276 TDA7432_BASS_SYM | /* Symmetric bass cut */
277 TDA7432_BASS_NORM; /* Normal bass range */
278 t->volume = 0x3b ; /* -27dB Volume */
279 if (loudness) /* Turn loudness on? */
280 t->volume |= TDA7432_LD_ON;
7a00d45c 281 t->muted = 1;
1da177e4
LT
282 t->treble = TDA7432_TREBLE_0DB; /* 0dB Treble */
283 t->bass = TDA7432_BASS_0DB; /* 0dB Bass */
284 t->lf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
285 t->lr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
286 t->rf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
287 t->rr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
288 t->loud = loudness; /* insmod parameter */
289
d531305a 290 tda7432_set(sd);
1da177e4
LT
291}
292
d531305a 293static int tda7432_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
1da177e4 294{
d531305a 295 struct tda7432 *t = to_state(sd);
1da177e4 296
7a00d45c
MCC
297 switch (ctrl->id) {
298 case V4L2_CID_AUDIO_MUTE:
299 ctrl->value=t->muted;
300 return 0;
301 case V4L2_CID_AUDIO_VOLUME:
1da177e4 302 if (!maxvol){ /* max +20db */
7a00d45c 303 ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 630;
1da177e4 304 } else { /* max 0db */
7a00d45c 305 ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 829;
1da177e4 306 }
7a00d45c
MCC
307 return 0;
308 case V4L2_CID_AUDIO_BALANCE:
309 {
1da177e4
LT
310 if ( (t->lf) < (t->rf) )
311 /* right is attenuated, balance shifted left */
7a00d45c 312 ctrl->value = (32768 - 1057*(t->rf));
1da177e4
LT
313 else
314 /* left is attenuated, balance shifted right */
7a00d45c
MCC
315 ctrl->value = (32768 + 1057*(t->lf));
316 return 0;
317 }
318 case V4L2_CID_AUDIO_BASS:
319 {
1da177e4 320 /* Bass/treble 4 bits each */
7a00d45c
MCC
321 int bass=t->bass;
322 if(bass >= 0x8)
323 bass = ~(bass - 0x8) & 0xf;
324 ctrl->value = (bass << 12)+(bass << 8)+(bass << 4)+(bass);
325 return 0;
1da177e4 326 }
7a00d45c 327 case V4L2_CID_AUDIO_TREBLE:
1da177e4 328 {
7a00d45c
MCC
329 int treble=t->treble;
330 if(treble >= 0x8)
331 treble = ~(treble - 0x8) & 0xf;
332 ctrl->value = (treble << 12)+(treble << 8)+(treble << 4)+(treble);
333 return 0;
334 }
335 }
336 return -EINVAL;
337}
1da177e4 338
d531305a 339static int tda7432_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
7a00d45c 340{
d531305a 341 struct tda7432 *t = to_state(sd);
1da177e4 342
7a00d45c
MCC
343 switch (ctrl->id) {
344 case V4L2_CID_AUDIO_MUTE:
345 t->muted=ctrl->value;
346 break;
347 case V4L2_CID_AUDIO_VOLUME:
348 if(!maxvol){ /* max +20db */
349 t->volume = 0x6f - ((ctrl->value)/630);
350 } else { /* max 0db */
351 t->volume = 0x6f - ((ctrl->value)/829);
352 }
1da177e4
LT
353 if (loudness) /* Turn on the loudness bit */
354 t->volume |= TDA7432_LD_ON;
355
d531305a 356 tda7432_write(sd, TDA7432_VL, t->volume);
7a00d45c
MCC
357 return 0;
358 case V4L2_CID_AUDIO_BALANCE:
359 if (ctrl->value < 32768) {
1da177e4 360 /* shifted to left, attenuate right */
7a00d45c 361 t->rr = (32768 - ctrl->value)/1057;
1da177e4
LT
362 t->rf = t->rr;
363 t->lr = TDA7432_ATTEN_0DB;
364 t->lf = TDA7432_ATTEN_0DB;
7a00d45c 365 } else if(ctrl->value > 32769) {
1da177e4 366 /* shifted to right, attenuate left */
7a00d45c 367 t->lf = (ctrl->value - 32768)/1057;
1da177e4
LT
368 t->lr = t->lf;
369 t->rr = TDA7432_ATTEN_0DB;
370 t->rf = TDA7432_ATTEN_0DB;
7a00d45c 371 } else {
1da177e4
LT
372 /* centered */
373 t->rr = TDA7432_ATTEN_0DB;
374 t->rf = TDA7432_ATTEN_0DB;
375 t->lf = TDA7432_ATTEN_0DB;
376 t->lr = TDA7432_ATTEN_0DB;
377 }
7a00d45c
MCC
378 break;
379 case V4L2_CID_AUDIO_BASS:
380 t->bass = ctrl->value >> 12;
381 if(t->bass>= 0x8)
382 t->bass = (~t->bass & 0xf) + 0x8 ;
383
d531305a 384 tda7432_write(sd, TDA7432_TN, 0x10 | (t->bass << 4) | t->treble);
7a00d45c
MCC
385 return 0;
386 case V4L2_CID_AUDIO_TREBLE:
387 t->treble= ctrl->value >> 12;
388 if(t->treble>= 0x8)
389 t->treble = (~t->treble & 0xf) + 0x8 ;
390
d531305a 391 tda7432_write(sd, TDA7432_TN, 0x10 | (t->bass << 4) | t->treble);
7a00d45c
MCC
392 return 0;
393 default:
394 return -EINVAL;
395 }
1da177e4 396
7a00d45c
MCC
397 /* Used for both mute and balance changes */
398 if (t->muted)
399 {
400 /* Mute & update balance*/
d531305a
HV
401 tda7432_write(sd, TDA7432_LF, t->lf | TDA7432_MUTE);
402 tda7432_write(sd, TDA7432_LR, t->lr | TDA7432_MUTE);
403 tda7432_write(sd, TDA7432_RF, t->rf | TDA7432_MUTE);
404 tda7432_write(sd, TDA7432_RR, t->rr | TDA7432_MUTE);
7a00d45c 405 } else {
d531305a
HV
406 tda7432_write(sd, TDA7432_LF, t->lf);
407 tda7432_write(sd, TDA7432_LR, t->lr);
408 tda7432_write(sd, TDA7432_RF, t->rf);
409 tda7432_write(sd, TDA7432_RR, t->rr);
7a00d45c
MCC
410 }
411 return 0;
412}
1da177e4 413
d531305a 414static int tda7432_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
7a00d45c 415{
d531305a 416 switch (qc->id) {
d531305a 417 case V4L2_CID_AUDIO_VOLUME:
10afbef1
HV
418 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 58880);
419 case V4L2_CID_AUDIO_MUTE:
420 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
d531305a
HV
421 case V4L2_CID_AUDIO_BALANCE:
422 case V4L2_CID_AUDIO_BASS:
423 case V4L2_CID_AUDIO_TREBLE:
10afbef1 424 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
7a00d45c 425 }
d531305a
HV
426 return -EINVAL;
427}
1da177e4 428
d531305a
HV
429/* ----------------------------------------------------------------------- */
430
431static const struct v4l2_subdev_core_ops tda7432_core_ops = {
432 .queryctrl = tda7432_queryctrl,
433 .g_ctrl = tda7432_g_ctrl,
434 .s_ctrl = tda7432_s_ctrl,
1da177e4
LT
435};
436
d531305a
HV
437static const struct v4l2_subdev_ops tda7432_ops = {
438 .core = &tda7432_core_ops,
1da177e4
LT
439};
440
d531305a
HV
441/* ----------------------------------------------------------------------- */
442
443/* *********************** *
444 * i2c interface functions *
445 * *********************** */
446
447static int tda7432_probe(struct i2c_client *client,
448 const struct i2c_device_id *id)
1da177e4 449{
d531305a
HV
450 struct tda7432 *t;
451 struct v4l2_subdev *sd;
452
453 v4l_info(client, "chip found @ 0x%02x (%s)\n",
454 client->addr << 1, client->adapter->name);
455
456 t = kzalloc(sizeof(*t), GFP_KERNEL);
457 if (!t)
458 return -ENOMEM;
459 sd = &t->sd;
460 v4l2_i2c_subdev_init(sd, client, &tda7432_ops);
461 if (loudness < 0 || loudness > 15) {
462 v4l2_warn(sd, "loudness parameter must be between 0 and 15\n");
463 if (loudness < 0)
464 loudness = 0;
465 if (loudness > 15)
466 loudness = 15;
1da177e4
LT
467 }
468
d531305a
HV
469 do_tda7432_init(sd);
470 return 0;
1da177e4
LT
471}
472
d531305a 473static int tda7432_remove(struct i2c_client *client)
1da177e4 474{
d531305a 475 struct v4l2_subdev *sd = i2c_get_clientdata(client);
1da177e4 476
d531305a
HV
477 do_tda7432_init(sd);
478 v4l2_device_unregister_subdev(sd);
479 kfree(to_state(sd));
480 return 0;
481}
1da177e4 482
d531305a
HV
483static const struct i2c_device_id tda7432_id[] = {
484 { "tda7432", 0 },
485 { }
486};
487MODULE_DEVICE_TABLE(i2c, tda7432_id);
488
489static struct v4l2_i2c_driver_data v4l2_i2c_data = {
490 .name = "tda7432",
d531305a
HV
491 .probe = tda7432_probe,
492 .remove = tda7432_remove,
493 .id_table = tda7432_id,
494};