tda7432.c 15 KB

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