tda7432.c 11 KB

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