tda7432.c 13 KB

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