tda7432.c 13 KB

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