tda7432.c 14 KB

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