tda7432.c 13 KB

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