blob: 80f1cee23fa5a70bba5f3afd44faa1be21c3d3b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Mauro Carvalho Chehab7a00d452006-08-25 16:53:09 -030011 * Copyright (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/string.h>
29#include <linux/timer.h>
30#include <linux/delay.h>
31#include <linux/errno.h>
32#include <linux/slab.h>
Hans Verkuil33b687c2008-07-25 05:32:50 -030033#include <linux/videodev2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Hans Verkuild5313052008-12-18 13:04:05 -030036#include <media/v4l2-device.h>
Hans Verkuil35ea11f2008-07-20 08:12:02 -030037#include <media/v4l2-ioctl.h>
Mauro Carvalho Chehabfa3fcce2006-03-23 21:45:24 -030038#include <media/i2c-addr.h>
Hans Verkuil267ea2a2009-03-29 09:06:00 -030039#include <media/v4l2-i2c-drv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#ifndef VIDEO_AUDIO_BALANCE
42# define VIDEO_AUDIO_BALANCE 32
43#endif
44
45MODULE_AUTHOR("Eric Sandeen <eric_sandeen@bigfoot.com>");
46MODULE_DESCRIPTION("bttv driver for the tda7432 audio processor chip");
47MODULE_LICENSE("GPL");
48
49static int maxvol;
50static int loudness; /* disable loudness by default */
51static int debug; /* insmod parameter */
52module_param(debug, int, S_IRUGO | S_IWUSR);
53module_param(loudness, int, S_IRUGO);
54MODULE_PARM_DESC(maxvol,"Set maximium volume to +20db (0), default is 0db(1)");
55module_param(maxvol, int, S_IRUGO | S_IWUSR);
56
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/* Structure of address and subaddresses for the tda7432 */
60
61struct tda7432 {
Hans Verkuild5313052008-12-18 13:04:05 -030062 struct v4l2_subdev sd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 int addr;
64 int input;
65 int volume;
66 int muted;
67 int bass, treble;
68 int lf, lr, rf, rr;
69 int loud;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
Hans Verkuild5313052008-12-18 13:04:05 -030071
72static inline struct tda7432 *to_state(struct v4l2_subdev *sd)
73{
74 return container_of(sd, struct tda7432, sd);
75}
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* The TDA7432 is made by STS-Thompson
78 * http://www.st.com
79 * http://us.st.com/stonline/books/pdf/docs/4056.pdf
80 *
81 * TDA7432: I2C-bus controlled basic audio processor
82 *
83 * The TDA7432 controls basic audio functions like volume, balance,
84 * and tone control (including loudness). It also has four channel
85 * output (for front and rear). Since most vidcap cards probably
86 * don't have 4 channel output, this driver will set front & rear
87 * together (no independent control).
88 */
89
90 /* Subaddresses for TDA7432 */
91
92#define TDA7432_IN 0x00 /* Input select */
93#define TDA7432_VL 0x01 /* Volume */
94#define TDA7432_TN 0x02 /* Bass, Treble (Tone) */
95#define TDA7432_LF 0x03 /* Attenuation LF (Left Front) */
96#define TDA7432_LR 0x04 /* Attenuation LR (Left Rear) */
97#define TDA7432_RF 0x05 /* Attenuation RF (Right Front) */
98#define TDA7432_RR 0x06 /* Attenuation RR (Right Rear) */
99#define TDA7432_LD 0x07 /* Loudness */
100
101
102 /* Masks for bits in TDA7432 subaddresses */
103
104/* Many of these not used - just for documentation */
105
106/* Subaddress 0x00 - Input selection and bass control */
107
108/* Bits 0,1,2 control input:
109 * 0x00 - Stereo input
110 * 0x02 - Mono input
111 * 0x03 - Mute (Using Attenuators Plays better with modules)
112 * Mono probably isn't used - I'm guessing only the stereo
113 * input is connected on most cards, so we'll set it to stereo.
114 *
115 * Bit 3 controls bass cut: 0/1 is non-symmetric/symmetric bass cut
116 * Bit 4 controls bass range: 0/1 is extended/standard bass range
117 *
118 * Highest 3 bits not used
119 */
120
121#define TDA7432_STEREO_IN 0
122#define TDA7432_MONO_IN 2 /* Probably won't be used */
123#define TDA7432_BASS_SYM 1 << 3
124#define TDA7432_BASS_NORM 1 << 4
125
126/* Subaddress 0x01 - Volume */
127
128/* Lower 7 bits control volume from -79dB to +32dB in 1dB steps
129 * Recommended maximum is +20 dB
130 *
131 * +32dB: 0x00
132 * +20dB: 0x0c
133 * 0dB: 0x20
134 * -79dB: 0x6f
135 *
136 * MSB (bit 7) controls loudness: 1/0 is loudness on/off
137 */
138
139#define TDA7432_VOL_0DB 0x20
140#define TDA7432_LD_ON 1 << 7
141
142
143/* Subaddress 0x02 - Tone control */
144
145/* Bits 0,1,2 control absolute treble gain from 0dB to 14dB
146 * 0x0 is 14dB, 0x7 is 0dB
147 *
148 * Bit 3 controls treble attenuation/gain (sign)
149 * 1 = gain (+)
150 * 0 = attenuation (-)
151 *
152 * Bits 4,5,6 control absolute bass gain from 0dB to 14dB
153 * (This is only true for normal base range, set in 0x00)
154 * 0x0 << 4 is 14dB, 0x7 is 0dB
155 *
156 * Bit 7 controls bass attenuation/gain (sign)
157 * 1 << 7 = gain (+)
158 * 0 << 7 = attenuation (-)
159 *
160 * Example:
161 * 1 1 0 1 0 1 0 1 is +4dB bass, -4dB treble
162 */
163
164#define TDA7432_TREBLE_0DB 0xf
165#define TDA7432_TREBLE 7
166#define TDA7432_TREBLE_GAIN 1 << 3
167#define TDA7432_BASS_0DB 0xf
168#define TDA7432_BASS 7 << 4
169#define TDA7432_BASS_GAIN 1 << 7
170
171
172/* Subaddress 0x03 - Left Front attenuation */
173/* Subaddress 0x04 - Left Rear attenuation */
174/* Subaddress 0x05 - Right Front attenuation */
175/* Subaddress 0x06 - Right Rear attenuation */
176
177/* Bits 0,1,2,3,4 control attenuation from 0dB to -37.5dB
178 * in 1.5dB steps.
179 *
180 * 0x00 is 0dB
181 * 0x1f is -37.5dB
182 *
183 * Bit 5 mutes that channel when set (1 = mute, 0 = unmute)
184 * We'll use the mute on the input, though (above)
185 * Bits 6,7 unused
186 */
187
188#define TDA7432_ATTEN_0DB 0x00
189#define TDA7432_MUTE 0x1 << 5
190
191
192/* Subaddress 0x07 - Loudness Control */
193
194/* Bits 0,1,2,3 control loudness from 0dB to -15dB in 1dB steps
195 * when bit 4 is NOT set
196 *
197 * 0x0 is 0dB
198 * 0xf is -15dB
199 *
200 * If bit 4 is set, then there is a flat attenuation according to
201 * the lower 4 bits, as above.
202 *
203 * Bits 5,6,7 unused
204 */
205
206
207
208/* Begin code */
209
Hans Verkuild5313052008-12-18 13:04:05 -0300210static int tda7432_write(struct v4l2_subdev *sd, int subaddr, int val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Hans Verkuild5313052008-12-18 13:04:05 -0300212 struct i2c_client *client = v4l2_get_subdevdata(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 unsigned char buffer[2];
Hans Verkuild5313052008-12-18 13:04:05 -0300214
215 v4l2_dbg(2, debug, sd, "In tda7432_write\n");
216 v4l2_dbg(1, debug, sd, "Writing %d 0x%x\n", subaddr, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 buffer[0] = subaddr;
218 buffer[1] = val;
Hans Verkuild5313052008-12-18 13:04:05 -0300219 if (2 != i2c_master_send(client, buffer, 2)) {
220 v4l2_err(sd, "I/O error, trying (write %d 0x%x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 subaddr, val);
222 return -1;
223 }
224 return 0;
225}
226
Hans Verkuild5313052008-12-18 13:04:05 -0300227static int tda7432_set(struct v4l2_subdev *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Hans Verkuild5313052008-12-18 13:04:05 -0300229 struct i2c_client *client = v4l2_get_subdevdata(sd);
230 struct tda7432 *t = to_state(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 unsigned char buf[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Hans Verkuild5313052008-12-18 13:04:05 -0300233 v4l2_dbg(1, debug, sd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
Hans Verkuild5313052008-12-18 13:04:05 -0300235 t->input, t->volume, t->bass, t->treble, t->lf, t->lr,
236 t->rf, t->rr, t->loud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 buf[0] = TDA7432_IN;
238 buf[1] = t->input;
239 buf[2] = t->volume;
240 buf[3] = t->bass;
241 buf[4] = t->treble;
242 buf[5] = t->lf;
243 buf[6] = t->lr;
244 buf[7] = t->rf;
245 buf[8] = t->rr;
246 buf[9] = t->loud;
Hans Verkuild5313052008-12-18 13:04:05 -0300247 if (10 != i2c_master_send(client, buf, 10)) {
248 v4l2_err(sd, "I/O error, trying tda7432_set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return -1;
250 }
251
252 return 0;
253}
254
Hans Verkuild5313052008-12-18 13:04:05 -0300255static void do_tda7432_init(struct v4l2_subdev *sd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Hans Verkuild5313052008-12-18 13:04:05 -0300257 struct tda7432 *t = to_state(sd);
258
259 v4l2_dbg(2, debug, sd, "In tda7432_init\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 t->input = TDA7432_STEREO_IN | /* Main (stereo) input */
262 TDA7432_BASS_SYM | /* Symmetric bass cut */
263 TDA7432_BASS_NORM; /* Normal bass range */
264 t->volume = 0x3b ; /* -27dB Volume */
265 if (loudness) /* Turn loudness on? */
266 t->volume |= TDA7432_LD_ON;
Mauro Carvalho Chehab7a00d452006-08-25 16:53:09 -0300267 t->muted = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 t->treble = TDA7432_TREBLE_0DB; /* 0dB Treble */
269 t->bass = TDA7432_BASS_0DB; /* 0dB Bass */
270 t->lf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
271 t->lr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
272 t->rf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
273 t->rr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
274 t->loud = loudness; /* insmod parameter */
275
Hans Verkuild5313052008-12-18 13:04:05 -0300276 tda7432_set(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Hans Verkuild5313052008-12-18 13:04:05 -0300279static int tda7432_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Hans Verkuild5313052008-12-18 13:04:05 -0300281 struct tda7432 *t = to_state(sd);
Mauro Carvalho Chehab7a00d452006-08-25 16:53:09 -0300282
283 switch (ctrl->id) {
284 case V4L2_CID_AUDIO_MUTE:
285 ctrl->value=t->muted;
286 return 0;
287 case V4L2_CID_AUDIO_VOLUME:
288 if (!maxvol){ /* max +20db */
289 ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 630;
290 } else { /* max 0db */
291 ctrl->value = ( 0x6f - (t->volume & 0x7F) ) * 829;
292 }
293 return 0;
294 case V4L2_CID_AUDIO_BALANCE:
295 {
296 if ( (t->lf) < (t->rf) )
297 /* right is attenuated, balance shifted left */
298 ctrl->value = (32768 - 1057*(t->rf));
299 else
300 /* left is attenuated, balance shifted right */
301 ctrl->value = (32768 + 1057*(t->lf));
302 return 0;
303 }
304 case V4L2_CID_AUDIO_BASS:
305 {
306 /* Bass/treble 4 bits each */
307 int bass=t->bass;
308 if(bass >= 0x8)
309 bass = ~(bass - 0x8) & 0xf;
310 ctrl->value = (bass << 12)+(bass << 8)+(bass << 4)+(bass);
311 return 0;
312 }
313 case V4L2_CID_AUDIO_TREBLE:
314 {
315 int treble=t->treble;
316 if(treble >= 0x8)
317 treble = ~(treble - 0x8) & 0xf;
318 ctrl->value = (treble << 12)+(treble << 8)+(treble << 4)+(treble);
319 return 0;
320 }
321 }
322 return -EINVAL;
323}
324
Hans Verkuild5313052008-12-18 13:04:05 -0300325static int tda7432_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
Mauro Carvalho Chehab7a00d452006-08-25 16:53:09 -0300326{
Hans Verkuild5313052008-12-18 13:04:05 -0300327 struct tda7432 *t = to_state(sd);
Mauro Carvalho Chehab7a00d452006-08-25 16:53:09 -0300328
329 switch (ctrl->id) {
330 case V4L2_CID_AUDIO_MUTE:
331 t->muted=ctrl->value;
332 break;
333 case V4L2_CID_AUDIO_VOLUME:
334 if(!maxvol){ /* max +20db */
335 t->volume = 0x6f - ((ctrl->value)/630);
336 } else { /* max 0db */
337 t->volume = 0x6f - ((ctrl->value)/829);
338 }
339 if (loudness) /* Turn on the loudness bit */
340 t->volume |= TDA7432_LD_ON;
341
Hans Verkuild5313052008-12-18 13:04:05 -0300342 tda7432_write(sd, TDA7432_VL, t->volume);
Mauro Carvalho Chehab7a00d452006-08-25 16:53:09 -0300343 return 0;
344 case V4L2_CID_AUDIO_BALANCE:
345 if (ctrl->value < 32768) {
346 /* shifted to left, attenuate right */
347 t->rr = (32768 - ctrl->value)/1057;
348 t->rf = t->rr;
349 t->lr = TDA7432_ATTEN_0DB;
350 t->lf = TDA7432_ATTEN_0DB;
351 } else if(ctrl->value > 32769) {
352 /* shifted to right, attenuate left */
353 t->lf = (ctrl->value - 32768)/1057;
354 t->lr = t->lf;
355 t->rr = TDA7432_ATTEN_0DB;
356 t->rf = TDA7432_ATTEN_0DB;
357 } else {
358 /* centered */
359 t->rr = TDA7432_ATTEN_0DB;
360 t->rf = TDA7432_ATTEN_0DB;
361 t->lf = TDA7432_ATTEN_0DB;
362 t->lr = TDA7432_ATTEN_0DB;
363 }
364 break;
365 case V4L2_CID_AUDIO_BASS:
366 t->bass = ctrl->value >> 12;
367 if(t->bass>= 0x8)
368 t->bass = (~t->bass & 0xf) + 0x8 ;
369
Hans Verkuild5313052008-12-18 13:04:05 -0300370 tda7432_write(sd, TDA7432_TN, 0x10 | (t->bass << 4) | t->treble);
Mauro Carvalho Chehab7a00d452006-08-25 16:53:09 -0300371 return 0;
372 case V4L2_CID_AUDIO_TREBLE:
373 t->treble= ctrl->value >> 12;
374 if(t->treble>= 0x8)
375 t->treble = (~t->treble & 0xf) + 0x8 ;
376
Hans Verkuild5313052008-12-18 13:04:05 -0300377 tda7432_write(sd, TDA7432_TN, 0x10 | (t->bass << 4) | t->treble);
Mauro Carvalho Chehab7a00d452006-08-25 16:53:09 -0300378 return 0;
379 default:
380 return -EINVAL;
381 }
382
383 /* Used for both mute and balance changes */
384 if (t->muted)
385 {
386 /* Mute & update balance*/
Hans Verkuild5313052008-12-18 13:04:05 -0300387 tda7432_write(sd, TDA7432_LF, t->lf | TDA7432_MUTE);
388 tda7432_write(sd, TDA7432_LR, t->lr | TDA7432_MUTE);
389 tda7432_write(sd, TDA7432_RF, t->rf | TDA7432_MUTE);
390 tda7432_write(sd, TDA7432_RR, t->rr | TDA7432_MUTE);
Mauro Carvalho Chehab7a00d452006-08-25 16:53:09 -0300391 } else {
Hans Verkuild5313052008-12-18 13:04:05 -0300392 tda7432_write(sd, TDA7432_LF, t->lf);
393 tda7432_write(sd, TDA7432_LR, t->lr);
394 tda7432_write(sd, TDA7432_RF, t->rf);
395 tda7432_write(sd, TDA7432_RR, t->rr);
Mauro Carvalho Chehab7a00d452006-08-25 16:53:09 -0300396 }
397 return 0;
398}
399
Hans Verkuild5313052008-12-18 13:04:05 -0300400static int tda7432_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Hans Verkuild5313052008-12-18 13:04:05 -0300402 switch (qc->id) {
Hans Verkuild5313052008-12-18 13:04:05 -0300403 case V4L2_CID_AUDIO_VOLUME:
Hans Verkuil10afbef2009-02-21 18:47:24 -0300404 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 58880);
405 case V4L2_CID_AUDIO_MUTE:
406 return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
Hans Verkuild5313052008-12-18 13:04:05 -0300407 case V4L2_CID_AUDIO_BALANCE:
408 case V4L2_CID_AUDIO_BASS:
409 case V4L2_CID_AUDIO_TREBLE:
Hans Verkuil10afbef2009-02-21 18:47:24 -0300410 return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Hans Verkuild5313052008-12-18 13:04:05 -0300412 return -EINVAL;
413}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Hans Verkuild5313052008-12-18 13:04:05 -0300415/* ----------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Hans Verkuild5313052008-12-18 13:04:05 -0300417static const struct v4l2_subdev_core_ops tda7432_core_ops = {
418 .queryctrl = tda7432_queryctrl,
419 .g_ctrl = tda7432_g_ctrl,
420 .s_ctrl = tda7432_s_ctrl,
421};
422
423static const struct v4l2_subdev_ops tda7432_ops = {
424 .core = &tda7432_core_ops,
425};
426
427/* ----------------------------------------------------------------------- */
428
429/* *********************** *
430 * i2c interface functions *
431 * *********************** */
432
433static int tda7432_probe(struct i2c_client *client,
434 const struct i2c_device_id *id)
435{
436 struct tda7432 *t;
437 struct v4l2_subdev *sd;
438
439 v4l_info(client, "chip found @ 0x%02x (%s)\n",
440 client->addr << 1, client->adapter->name);
441
442 t = kzalloc(sizeof(*t), GFP_KERNEL);
443 if (!t)
444 return -ENOMEM;
445 sd = &t->sd;
446 v4l2_i2c_subdev_init(sd, client, &tda7432_ops);
447 if (loudness < 0 || loudness > 15) {
448 v4l2_warn(sd, "loudness parameter must be between 0 and 15\n");
449 if (loudness < 0)
450 loudness = 0;
451 if (loudness > 15)
452 loudness = 15;
453 }
454
455 do_tda7432_init(sd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return 0;
457}
458
Hans Verkuild5313052008-12-18 13:04:05 -0300459static int tda7432_remove(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Hans Verkuild5313052008-12-18 13:04:05 -0300461 struct v4l2_subdev *sd = i2c_get_clientdata(client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Hans Verkuild5313052008-12-18 13:04:05 -0300463 do_tda7432_init(sd);
464 v4l2_device_unregister_subdev(sd);
465 kfree(to_state(sd));
466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Hans Verkuild5313052008-12-18 13:04:05 -0300469static const struct i2c_device_id tda7432_id[] = {
470 { "tda7432", 0 },
471 { }
472};
473MODULE_DEVICE_TABLE(i2c, tda7432_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Hans Verkuild5313052008-12-18 13:04:05 -0300475static struct v4l2_i2c_driver_data v4l2_i2c_data = {
476 .name = "tda7432",
Hans Verkuild5313052008-12-18 13:04:05 -0300477 .probe = tda7432_probe,
478 .remove = tda7432_remove,
479 .id_table = tda7432_id,
480};