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