tda7432.c 13 KB

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