tda7432.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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>
  11. * This code is placed under the terms of the GNU General Public License
  12. * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
  13. * Which was based on tda8425.c by Greg Alexander (c) 1998
  14. *
  15. * OPTIONS:
  16. * debug - set to 1 if you'd like to see debug messages
  17. * set to 2 if you'd like to be inundated with debug messages
  18. *
  19. * loudness - set between 0 and 15 for varying degrees of loudness effect
  20. *
  21. * maxvol - set maximium volume to +20db (1), default is 0db(0)
  22. *
  23. *
  24. * Revision: 0.7 - maxvol module parm to set maximium volume 0db or +20db
  25. * store if muted so we can return it
  26. * change balance only if flaged to
  27. * Revision: 0.6 - added tone controls
  28. * Revision: 0.5 - Fixed odd balance problem
  29. * Revision: 0.4 - added muting
  30. * Revision: 0.3 - Fixed silly reversed volume controls. :)
  31. * Revision: 0.2 - Cleaned up #defines
  32. * fixed volume control
  33. * Added I2C_DRIVERID_TDA7432
  34. * added loudness insmod control
  35. * Revision: 0.1 - initial version
  36. */
  37. #include <linux/module.h>
  38. #include <linux/init.h>
  39. #include <linux/kernel.h>
  40. #include <linux/string.h>
  41. #include <linux/timer.h>
  42. #include <linux/delay.h>
  43. #include <linux/errno.h>
  44. #include <linux/slab.h>
  45. #include <linux/videodev.h>
  46. #include <linux/i2c.h>
  47. #include <media/v4l2-common.h>
  48. #include <media/i2c-addr.h>
  49. #ifndef VIDEO_AUDIO_BALANCE
  50. # define VIDEO_AUDIO_BALANCE 32
  51. #endif
  52. MODULE_AUTHOR("Eric Sandeen <eric_sandeen@bigfoot.com>");
  53. MODULE_DESCRIPTION("bttv driver for the tda7432 audio processor chip");
  54. MODULE_LICENSE("GPL");
  55. static int maxvol;
  56. static int loudness; /* disable loudness by default */
  57. static int debug; /* insmod parameter */
  58. module_param(debug, int, S_IRUGO | S_IWUSR);
  59. module_param(loudness, int, S_IRUGO);
  60. MODULE_PARM_DESC(maxvol,"Set maximium volume to +20db (0), default is 0db(1)");
  61. module_param(maxvol, int, S_IRUGO | S_IWUSR);
  62. /* Address to scan (I2C address of this chip) */
  63. static unsigned short normal_i2c[] = {
  64. I2C_ADDR_TDA7432 >> 1,
  65. I2C_CLIENT_END,
  66. };
  67. I2C_CLIENT_INSMOD;
  68. /* Structure of address and subaddresses for the tda7432 */
  69. struct tda7432 {
  70. int addr;
  71. int input;
  72. int volume;
  73. int muted;
  74. int bass, treble;
  75. int lf, lr, rf, rr;
  76. int loud;
  77. struct i2c_client c;
  78. };
  79. static struct i2c_driver driver;
  80. static struct i2c_client client_template;
  81. /* The TDA7432 is made by STS-Thompson
  82. * http://www.st.com
  83. * http://us.st.com/stonline/books/pdf/docs/4056.pdf
  84. *
  85. * TDA7432: I2C-bus controlled basic audio processor
  86. *
  87. * The TDA7432 controls basic audio functions like volume, balance,
  88. * and tone control (including loudness). It also has four channel
  89. * output (for front and rear). Since most vidcap cards probably
  90. * don't have 4 channel output, this driver will set front & rear
  91. * together (no independent control).
  92. */
  93. /* Subaddresses for TDA7432 */
  94. #define TDA7432_IN 0x00 /* Input select */
  95. #define TDA7432_VL 0x01 /* Volume */
  96. #define TDA7432_TN 0x02 /* Bass, Treble (Tone) */
  97. #define TDA7432_LF 0x03 /* Attenuation LF (Left Front) */
  98. #define TDA7432_LR 0x04 /* Attenuation LR (Left Rear) */
  99. #define TDA7432_RF 0x05 /* Attenuation RF (Right Front) */
  100. #define TDA7432_RR 0x06 /* Attenuation RR (Right Rear) */
  101. #define TDA7432_LD 0x07 /* Loudness */
  102. /* Masks for bits in TDA7432 subaddresses */
  103. /* Many of these not used - just for documentation */
  104. /* Subaddress 0x00 - Input selection and bass control */
  105. /* Bits 0,1,2 control input:
  106. * 0x00 - Stereo input
  107. * 0x02 - Mono input
  108. * 0x03 - Mute (Using Attenuators Plays better with modules)
  109. * Mono probably isn't used - I'm guessing only the stereo
  110. * input is connected on most cards, so we'll set it to stereo.
  111. *
  112. * Bit 3 controls bass cut: 0/1 is non-symmetric/symmetric bass cut
  113. * Bit 4 controls bass range: 0/1 is extended/standard bass range
  114. *
  115. * Highest 3 bits not used
  116. */
  117. #define TDA7432_STEREO_IN 0
  118. #define TDA7432_MONO_IN 2 /* Probably won't be used */
  119. #define TDA7432_BASS_SYM 1 << 3
  120. #define TDA7432_BASS_NORM 1 << 4
  121. /* Subaddress 0x01 - Volume */
  122. /* Lower 7 bits control volume from -79dB to +32dB in 1dB steps
  123. * Recommended maximum is +20 dB
  124. *
  125. * +32dB: 0x00
  126. * +20dB: 0x0c
  127. * 0dB: 0x20
  128. * -79dB: 0x6f
  129. *
  130. * MSB (bit 7) controls loudness: 1/0 is loudness on/off
  131. */
  132. #define TDA7432_VOL_0DB 0x20
  133. #define TDA7432_LD_ON 1 << 7
  134. /* Subaddress 0x02 - Tone control */
  135. /* Bits 0,1,2 control absolute treble gain from 0dB to 14dB
  136. * 0x0 is 14dB, 0x7 is 0dB
  137. *
  138. * Bit 3 controls treble attenuation/gain (sign)
  139. * 1 = gain (+)
  140. * 0 = attenuation (-)
  141. *
  142. * Bits 4,5,6 control absolute bass gain from 0dB to 14dB
  143. * (This is only true for normal base range, set in 0x00)
  144. * 0x0 << 4 is 14dB, 0x7 is 0dB
  145. *
  146. * Bit 7 controls bass attenuation/gain (sign)
  147. * 1 << 7 = gain (+)
  148. * 0 << 7 = attenuation (-)
  149. *
  150. * Example:
  151. * 1 1 0 1 0 1 0 1 is +4dB bass, -4dB treble
  152. */
  153. #define TDA7432_TREBLE_0DB 0xf
  154. #define TDA7432_TREBLE 7
  155. #define TDA7432_TREBLE_GAIN 1 << 3
  156. #define TDA7432_BASS_0DB 0xf
  157. #define TDA7432_BASS 7 << 4
  158. #define TDA7432_BASS_GAIN 1 << 7
  159. /* Subaddress 0x03 - Left Front attenuation */
  160. /* Subaddress 0x04 - Left Rear attenuation */
  161. /* Subaddress 0x05 - Right Front attenuation */
  162. /* Subaddress 0x06 - Right Rear attenuation */
  163. /* Bits 0,1,2,3,4 control attenuation from 0dB to -37.5dB
  164. * in 1.5dB steps.
  165. *
  166. * 0x00 is 0dB
  167. * 0x1f is -37.5dB
  168. *
  169. * Bit 5 mutes that channel when set (1 = mute, 0 = unmute)
  170. * We'll use the mute on the input, though (above)
  171. * Bits 6,7 unused
  172. */
  173. #define TDA7432_ATTEN_0DB 0x00
  174. #define TDA7432_MUTE 0x1 << 5
  175. /* Subaddress 0x07 - Loudness Control */
  176. /* Bits 0,1,2,3 control loudness from 0dB to -15dB in 1dB steps
  177. * when bit 4 is NOT set
  178. *
  179. * 0x0 is 0dB
  180. * 0xf is -15dB
  181. *
  182. * If bit 4 is set, then there is a flat attenuation according to
  183. * the lower 4 bits, as above.
  184. *
  185. * Bits 5,6,7 unused
  186. */
  187. /* Begin code */
  188. static int tda7432_write(struct i2c_client *client, int subaddr, int val)
  189. {
  190. unsigned char buffer[2];
  191. v4l_dbg(2, debug,client,"In tda7432_write\n");
  192. v4l_dbg(1, debug,client,"Writing %d 0x%x\n", subaddr, val);
  193. buffer[0] = subaddr;
  194. buffer[1] = val;
  195. if (2 != i2c_master_send(client,buffer,2)) {
  196. v4l_err(client,"I/O error, trying (write %d 0x%x)\n",
  197. subaddr, val);
  198. return -1;
  199. }
  200. return 0;
  201. }
  202. /* I don't think we ever actually _read_ the chip... */
  203. static int tda7432_set(struct i2c_client *client)
  204. {
  205. struct tda7432 *t = i2c_get_clientdata(client);
  206. unsigned char buf[16];
  207. v4l_dbg(2, debug,client,"In tda7432_set\n");
  208. v4l_dbg(1, debug,client,
  209. "tda7432: 7432_set(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)\n",
  210. t->input,t->volume,t->bass,t->treble,t->lf,t->lr,t->rf,t->rr,t->loud);
  211. buf[0] = TDA7432_IN;
  212. buf[1] = t->input;
  213. buf[2] = t->volume;
  214. buf[3] = t->bass;
  215. buf[4] = t->treble;
  216. buf[5] = t->lf;
  217. buf[6] = t->lr;
  218. buf[7] = t->rf;
  219. buf[8] = t->rr;
  220. buf[9] = t->loud;
  221. if (10 != i2c_master_send(client,buf,10)) {
  222. v4l_err(client,"I/O error, trying tda7432_set\n");
  223. return -1;
  224. }
  225. return 0;
  226. }
  227. static void do_tda7432_init(struct i2c_client *client)
  228. {
  229. struct tda7432 *t = i2c_get_clientdata(client);
  230. v4l_dbg(2, debug,client,"In tda7432_init\n");
  231. t->input = TDA7432_STEREO_IN | /* Main (stereo) input */
  232. TDA7432_BASS_SYM | /* Symmetric bass cut */
  233. TDA7432_BASS_NORM; /* Normal bass range */
  234. t->volume = 0x3b ; /* -27dB Volume */
  235. if (loudness) /* Turn loudness on? */
  236. t->volume |= TDA7432_LD_ON;
  237. t->muted = VIDEO_AUDIO_MUTE;
  238. t->treble = TDA7432_TREBLE_0DB; /* 0dB Treble */
  239. t->bass = TDA7432_BASS_0DB; /* 0dB Bass */
  240. t->lf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
  241. t->lr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
  242. t->rf = TDA7432_ATTEN_0DB; /* 0dB attenuation */
  243. t->rr = TDA7432_ATTEN_0DB; /* 0dB attenuation */
  244. t->loud = loudness; /* insmod parameter */
  245. tda7432_set(client);
  246. }
  247. /* *********************** *
  248. * i2c interface functions *
  249. * *********************** */
  250. static int tda7432_attach(struct i2c_adapter *adap, int addr, int kind)
  251. {
  252. struct tda7432 *t;
  253. struct i2c_client *client;
  254. t = kzalloc(sizeof *t,GFP_KERNEL);
  255. if (!t)
  256. return -ENOMEM;
  257. client = &t->c;
  258. memcpy(client,&client_template,sizeof(struct i2c_client));
  259. client->adapter = adap;
  260. client->addr = addr;
  261. i2c_set_clientdata(client, t);
  262. do_tda7432_init(client);
  263. i2c_attach_client(client);
  264. v4l_info(client, "chip found @ 0x%x (%s)\n", addr << 1, adap->name);
  265. return 0;
  266. }
  267. static int tda7432_probe(struct i2c_adapter *adap)
  268. {
  269. if (adap->class & I2C_CLASS_TV_ANALOG)
  270. return i2c_probe(adap, &addr_data, tda7432_attach);
  271. return 0;
  272. }
  273. static int tda7432_detach(struct i2c_client *client)
  274. {
  275. struct tda7432 *t = i2c_get_clientdata(client);
  276. do_tda7432_init(client);
  277. i2c_detach_client(client);
  278. kfree(t);
  279. return 0;
  280. }
  281. static int tda7432_command(struct i2c_client *client,
  282. unsigned int cmd, void *arg)
  283. {
  284. struct tda7432 *t = i2c_get_clientdata(client);
  285. v4l_dbg(2, debug,client,"In tda7432_command\n");
  286. if (debug>1)
  287. v4l_i2c_print_ioctl(client,cmd);
  288. switch (cmd) {
  289. /* --- v4l ioctls --- */
  290. /* take care: bttv does userspace copying, we'll get a
  291. kernel pointer here... */
  292. /* Query card - scale from TDA7432 settings to V4L settings */
  293. case VIDIOCGAUDIO:
  294. {
  295. struct video_audio *va = arg;
  296. va->flags |= VIDEO_AUDIO_VOLUME |
  297. VIDEO_AUDIO_BASS |
  298. VIDEO_AUDIO_TREBLE |
  299. VIDEO_AUDIO_MUTABLE;
  300. if (t->muted)
  301. va->flags |= VIDEO_AUDIO_MUTE;
  302. va->mode |= VIDEO_SOUND_STEREO;
  303. /* Master volume control
  304. * V4L volume is min 0, max 65535
  305. * TDA7432 Volume:
  306. * Min (-79dB) is 0x6f
  307. * Max (+20dB) is 0x07 (630)
  308. * Max (0dB) is 0x20 (829)
  309. * (Mask out bit 7 of vol - it's for the loudness setting)
  310. */
  311. if (!maxvol){ /* max +20db */
  312. va->volume = ( 0x6f - (t->volume & 0x7F) ) * 630;
  313. } else { /* max 0db */
  314. va->volume = ( 0x6f - (t->volume & 0x7F) ) * 829;
  315. }
  316. /* Balance depends on L,R attenuation
  317. * V4L balance is 0 to 65535, middle is 32768
  318. * TDA7432 attenuation: min (0dB) is 0, max (-37.5dB) is 0x1f
  319. * to scale up to V4L numbers, mult by 1057
  320. * attenuation exists for lf, lr, rf, rr
  321. * we use only lf and rf (front channels)
  322. */
  323. if ( (t->lf) < (t->rf) )
  324. /* right is attenuated, balance shifted left */
  325. va->balance = (32768 - 1057*(t->rf));
  326. else
  327. /* left is attenuated, balance shifted right */
  328. va->balance = (32768 + 1057*(t->lf));
  329. /* Bass/treble 4 bits each */
  330. va->bass=t->bass;
  331. if(va->bass >= 0x8)
  332. va->bass = ~(va->bass - 0x8) & 0xf;
  333. va->bass = (va->bass << 12)+(va->bass << 8)+(va->bass << 4)+(va->bass);
  334. va->treble=t->treble;
  335. if(va->treble >= 0x8)
  336. va->treble = ~(va->treble - 0x8) & 0xf;
  337. va->treble = (va->treble << 12)+(va->treble << 8)+(va->treble << 4)+(va->treble);
  338. break; /* VIDIOCGAUDIO case */
  339. }
  340. /* Set card - scale from V4L settings to TDA7432 settings */
  341. case VIDIOCSAUDIO:
  342. {
  343. struct video_audio *va = arg;
  344. if(va->flags & VIDEO_AUDIO_VOLUME){
  345. if(!maxvol){ /* max +20db */
  346. t->volume = 0x6f - ((va->volume)/630);
  347. } else { /* max 0db */
  348. t->volume = 0x6f - ((va->volume)/829);
  349. }
  350. if (loudness) /* Turn on the loudness bit */
  351. t->volume |= TDA7432_LD_ON;
  352. tda7432_write(client,TDA7432_VL, t->volume);
  353. }
  354. if(va->flags & VIDEO_AUDIO_BASS)
  355. {
  356. t->bass = va->bass >> 12;
  357. if(t->bass>= 0x8)
  358. t->bass = (~t->bass & 0xf) + 0x8 ;
  359. }
  360. if(va->flags & VIDEO_AUDIO_TREBLE)
  361. {
  362. t->treble= va->treble >> 12;
  363. if(t->treble>= 0x8)
  364. t->treble = (~t->treble & 0xf) + 0x8 ;
  365. }
  366. if(va->flags & (VIDEO_AUDIO_TREBLE| VIDEO_AUDIO_BASS))
  367. tda7432_write(client,TDA7432_TN, 0x10 | (t->bass << 4) | t->treble );
  368. if(va->flags & VIDEO_AUDIO_BALANCE) {
  369. if (va->balance < 32768)
  370. {
  371. /* shifted to left, attenuate right */
  372. t->rr = (32768 - va->balance)/1057;
  373. t->rf = t->rr;
  374. t->lr = TDA7432_ATTEN_0DB;
  375. t->lf = TDA7432_ATTEN_0DB;
  376. }
  377. else if(va->balance > 32769)
  378. {
  379. /* shifted to right, attenuate left */
  380. t->lf = (va->balance - 32768)/1057;
  381. t->lr = t->lf;
  382. t->rr = TDA7432_ATTEN_0DB;
  383. t->rf = TDA7432_ATTEN_0DB;
  384. }
  385. else
  386. {
  387. /* centered */
  388. t->rr = TDA7432_ATTEN_0DB;
  389. t->rf = TDA7432_ATTEN_0DB;
  390. t->lf = TDA7432_ATTEN_0DB;
  391. t->lr = TDA7432_ATTEN_0DB;
  392. }
  393. }
  394. t->muted=(va->flags & VIDEO_AUDIO_MUTE);
  395. if (t->muted)
  396. {
  397. /* Mute & update balance*/
  398. tda7432_write(client,TDA7432_LF, t->lf | TDA7432_MUTE);
  399. tda7432_write(client,TDA7432_LR, t->lr | TDA7432_MUTE);
  400. tda7432_write(client,TDA7432_RF, t->rf | TDA7432_MUTE);
  401. tda7432_write(client,TDA7432_RR, t->rr | TDA7432_MUTE);
  402. } else {
  403. tda7432_write(client,TDA7432_LF, t->lf);
  404. tda7432_write(client,TDA7432_LR, t->lr);
  405. tda7432_write(client,TDA7432_RF, t->rf);
  406. tda7432_write(client,TDA7432_RR, t->rr);
  407. }
  408. break;
  409. } /* end of VIDEOCSAUDIO case */
  410. } /* end of (cmd) switch */
  411. return 0;
  412. }
  413. static struct i2c_driver driver = {
  414. .driver = {
  415. .name = "tda7432",
  416. },
  417. .id = I2C_DRIVERID_TDA7432,
  418. .attach_adapter = tda7432_probe,
  419. .detach_client = tda7432_detach,
  420. .command = tda7432_command,
  421. };
  422. static struct i2c_client client_template =
  423. {
  424. .name = "tda7432",
  425. .driver = &driver,
  426. };
  427. static int __init tda7432_init(void)
  428. {
  429. if ( (loudness < 0) || (loudness > 15) ) {
  430. printk(KERN_ERR "loudness parameter must be between 0 and 15\n");
  431. return -EINVAL;
  432. }
  433. return i2c_add_driver(&driver);
  434. }
  435. static void __exit tda7432_fini(void)
  436. {
  437. i2c_del_driver(&driver);
  438. }
  439. module_init(tda7432_init);
  440. module_exit(tda7432_fini);
  441. /*
  442. * Local variables:
  443. * c-basic-offset: 8
  444. * End:
  445. */