tda9875.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * For the TDA9875 chip
  3. * (The TDA9875 is used on the Diamond DTV2000 french version
  4. * Other cards probably use these chips as well.)
  5. * This driver will not complain if used with any
  6. * other i2c device with the same address.
  7. *
  8. * Copyright (c) 2000 Guillaume Delvit based on Gerd Knorr source and
  9. * Eric Sandeen
  10. * Copyright (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
  11. * This code is placed under the terms of the GNU General Public License
  12. * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
  13. * Which was based on tda8425.c by Greg Alexander (c) 1998
  14. *
  15. * OPTIONS:
  16. * debug - set to 1 if you'd like to see debug messages
  17. *
  18. * Revision: 0.1 - original version
  19. */
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/string.h>
  23. #include <linux/timer.h>
  24. #include <linux/delay.h>
  25. #include <linux/errno.h>
  26. #include <linux/slab.h>
  27. #include <linux/i2c.h>
  28. #include <linux/videodev2.h>
  29. #include <media/v4l2-device.h>
  30. #include <media/i2c-addr.h>
  31. static int debug; /* insmod parameter */
  32. module_param(debug, int, S_IRUGO | S_IWUSR);
  33. MODULE_LICENSE("GPL");
  34. /* This is a superset of the TDA9875 */
  35. struct tda9875 {
  36. struct v4l2_subdev sd;
  37. int rvol, lvol;
  38. int bass, treble;
  39. };
  40. static inline struct tda9875 *to_state(struct v4l2_subdev *sd)
  41. {
  42. return container_of(sd, struct tda9875, sd);
  43. }
  44. #define dprintk if (debug) printk
  45. /* The TDA9875 is made by Philips Semiconductor
  46. * http://www.semiconductors.philips.com
  47. * TDA9875: I2C-bus controlled DSP audio processor, FM demodulator
  48. *
  49. */
  50. /* subaddresses for TDA9875 */
  51. #define TDA9875_MUT 0x12 /*General mute (value --> 0b11001100*/
  52. #define TDA9875_CFG 0x01 /* Config register (value --> 0b00000000 */
  53. #define TDA9875_DACOS 0x13 /*DAC i/o select (ADC) 0b0000100*/
  54. #define TDA9875_LOSR 0x16 /*Line output select regirter 0b0100 0001*/
  55. #define TDA9875_CH1V 0x0c /*Channel 1 volume (mute)*/
  56. #define TDA9875_CH2V 0x0d /*Channel 2 volume (mute)*/
  57. #define TDA9875_SC1 0x14 /*SCART 1 in (mono)*/
  58. #define TDA9875_SC2 0x15 /*SCART 2 in (mono)*/
  59. #define TDA9875_ADCIS 0x17 /*ADC input select (mono) 0b0110 000*/
  60. #define TDA9875_AER 0x19 /*Audio effect (AVL+Pseudo) 0b0000 0110*/
  61. #define TDA9875_MCS 0x18 /*Main channel select (DAC) 0b0000100*/
  62. #define TDA9875_MVL 0x1a /* Main volume gauche */
  63. #define TDA9875_MVR 0x1b /* Main volume droite */
  64. #define TDA9875_MBA 0x1d /* Main Basse */
  65. #define TDA9875_MTR 0x1e /* Main treble */
  66. #define TDA9875_ACS 0x1f /* Auxilary channel select (FM) 0b0000000*/
  67. #define TDA9875_AVL 0x20 /* Auxilary volume gauche */
  68. #define TDA9875_AVR 0x21 /* Auxilary volume droite */
  69. #define TDA9875_ABA 0x22 /* Auxilary Basse */
  70. #define TDA9875_ATR 0x23 /* Auxilary treble */
  71. #define TDA9875_MSR 0x02 /* Monitor select register */
  72. #define TDA9875_C1MSB 0x03 /* Carrier 1 (FM) frequency register MSB */
  73. #define TDA9875_C1MIB 0x04 /* Carrier 1 (FM) frequency register (16-8]b */
  74. #define TDA9875_C1LSB 0x05 /* Carrier 1 (FM) frequency register LSB */
  75. #define TDA9875_C2MSB 0x06 /* Carrier 2 (nicam) frequency register MSB */
  76. #define TDA9875_C2MIB 0x07 /* Carrier 2 (nicam) frequency register (16-8]b */
  77. #define TDA9875_C2LSB 0x08 /* Carrier 2 (nicam) frequency register LSB */
  78. #define TDA9875_DCR 0x09 /* Demodulateur configuration regirter*/
  79. #define TDA9875_DEEM 0x0a /* FM de-emphasis regirter*/
  80. #define TDA9875_FMAT 0x0b /* FM Matrix regirter*/
  81. /* values */
  82. #define TDA9875_MUTE_ON 0xff /* general mute */
  83. #define TDA9875_MUTE_OFF 0xcc /* general no mute */
  84. /* Begin code */
  85. static int tda9875_write(struct v4l2_subdev *sd, int subaddr, unsigned char val)
  86. {
  87. struct i2c_client *client = v4l2_get_subdevdata(sd);
  88. unsigned char buffer[2];
  89. v4l2_dbg(1, debug, sd, "Writing %d 0x%x\n", subaddr, val);
  90. buffer[0] = subaddr;
  91. buffer[1] = val;
  92. if (2 != i2c_master_send(client, buffer, 2)) {
  93. v4l2_warn(sd, "I/O error, trying (write %d 0x%x)\n",
  94. subaddr, val);
  95. return -1;
  96. }
  97. return 0;
  98. }
  99. static int i2c_read_register(struct i2c_client *client, int addr, int reg)
  100. {
  101. unsigned char write[1];
  102. unsigned char read[1];
  103. struct i2c_msg msgs[2] = {
  104. { addr, 0, 1, write },
  105. { addr, I2C_M_RD, 1, read }
  106. };
  107. write[0] = reg;
  108. if (2 != i2c_transfer(client->adapter, msgs, 2)) {
  109. v4l_warn(client, "I/O error (read2)\n");
  110. return -1;
  111. }
  112. v4l_dbg(1, debug, client, "chip_read2: reg%d=0x%x\n", reg, read[0]);
  113. return read[0];
  114. }
  115. static void tda9875_set(struct v4l2_subdev *sd)
  116. {
  117. struct tda9875 *tda = to_state(sd);
  118. unsigned char a;
  119. v4l2_dbg(1, debug, sd, "tda9875_set(%04x,%04x,%04x,%04x)\n",
  120. tda->lvol, tda->rvol, tda->bass, tda->treble);
  121. a = tda->lvol & 0xff;
  122. tda9875_write(sd, TDA9875_MVL, a);
  123. a =tda->rvol & 0xff;
  124. tda9875_write(sd, TDA9875_MVR, a);
  125. a =tda->bass & 0xff;
  126. tda9875_write(sd, TDA9875_MBA, a);
  127. a =tda->treble & 0xff;
  128. tda9875_write(sd, TDA9875_MTR, a);
  129. }
  130. static void do_tda9875_init(struct v4l2_subdev *sd)
  131. {
  132. struct tda9875 *t = to_state(sd);
  133. v4l2_dbg(1, debug, sd, "In tda9875_init\n");
  134. tda9875_write(sd, TDA9875_CFG, 0xd0); /*reg de config 0 (reset)*/
  135. tda9875_write(sd, TDA9875_MSR, 0x03); /* Monitor 0b00000XXX*/
  136. tda9875_write(sd, TDA9875_C1MSB, 0x00); /*Car1(FM) MSB XMHz*/
  137. tda9875_write(sd, TDA9875_C1MIB, 0x00); /*Car1(FM) MIB XMHz*/
  138. tda9875_write(sd, TDA9875_C1LSB, 0x00); /*Car1(FM) LSB XMHz*/
  139. tda9875_write(sd, TDA9875_C2MSB, 0x00); /*Car2(NICAM) MSB XMHz*/
  140. tda9875_write(sd, TDA9875_C2MIB, 0x00); /*Car2(NICAM) MIB XMHz*/
  141. tda9875_write(sd, TDA9875_C2LSB, 0x00); /*Car2(NICAM) LSB XMHz*/
  142. tda9875_write(sd, TDA9875_DCR, 0x00); /*Demod config 0x00*/
  143. tda9875_write(sd, TDA9875_DEEM, 0x44); /*DE-Emph 0b0100 0100*/
  144. tda9875_write(sd, TDA9875_FMAT, 0x00); /*FM Matrix reg 0x00*/
  145. tda9875_write(sd, TDA9875_SC1, 0x00); /* SCART 1 (SC1)*/
  146. tda9875_write(sd, TDA9875_SC2, 0x01); /* SCART 2 (sc2)*/
  147. tda9875_write(sd, TDA9875_CH1V, 0x10); /* Channel volume 1 mute*/
  148. tda9875_write(sd, TDA9875_CH2V, 0x10); /* Channel volume 2 mute */
  149. tda9875_write(sd, TDA9875_DACOS, 0x02); /* sig DAC i/o(in:nicam)*/
  150. tda9875_write(sd, TDA9875_ADCIS, 0x6f); /* sig ADC input(in:mono)*/
  151. tda9875_write(sd, TDA9875_LOSR, 0x00); /* line out (in:mono)*/
  152. tda9875_write(sd, TDA9875_AER, 0x00); /*06 Effect (AVL+PSEUDO) */
  153. tda9875_write(sd, TDA9875_MCS, 0x44); /* Main ch select (DAC) */
  154. tda9875_write(sd, TDA9875_MVL, 0x03); /* Vol Main left 10dB */
  155. tda9875_write(sd, TDA9875_MVR, 0x03); /* Vol Main right 10dB*/
  156. tda9875_write(sd, TDA9875_MBA, 0x00); /* Main Bass Main 0dB*/
  157. tda9875_write(sd, TDA9875_MTR, 0x00); /* Main Treble Main 0dB*/
  158. tda9875_write(sd, TDA9875_ACS, 0x44); /* Aux chan select (dac)*/
  159. tda9875_write(sd, TDA9875_AVL, 0x00); /* Vol Aux left 0dB*/
  160. tda9875_write(sd, TDA9875_AVR, 0x00); /* Vol Aux right 0dB*/
  161. tda9875_write(sd, TDA9875_ABA, 0x00); /* Aux Bass Main 0dB*/
  162. tda9875_write(sd, TDA9875_ATR, 0x00); /* Aux Aigus Main 0dB*/
  163. tda9875_write(sd, TDA9875_MUT, 0xcc); /* General mute */
  164. t->lvol = t->rvol = 0; /* 0dB */
  165. t->bass = 0; /* 0dB */
  166. t->treble = 0; /* 0dB */
  167. tda9875_set(sd);
  168. }
  169. static int tda9875_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  170. {
  171. struct tda9875 *t = to_state(sd);
  172. switch (ctrl->id) {
  173. case V4L2_CID_AUDIO_VOLUME:
  174. {
  175. int left = (t->lvol+84)*606;
  176. int right = (t->rvol+84)*606;
  177. ctrl->value=max(left,right);
  178. return 0;
  179. }
  180. case V4L2_CID_AUDIO_BALANCE:
  181. {
  182. int left = (t->lvol+84)*606;
  183. int right = (t->rvol+84)*606;
  184. int volume = max(left,right);
  185. int balance = (32768*min(left,right))/
  186. (volume ? volume : 1);
  187. ctrl->value=(left<right)?
  188. (65535-balance) : balance;
  189. return 0;
  190. }
  191. case V4L2_CID_AUDIO_BASS:
  192. ctrl->value = (t->bass+12)*2427; /* min -12 max +15 */
  193. return 0;
  194. case V4L2_CID_AUDIO_TREBLE:
  195. ctrl->value = (t->treble+12)*2730;/* min -12 max +12 */
  196. return 0;
  197. }
  198. return -EINVAL;
  199. }
  200. static int tda9875_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
  201. {
  202. struct tda9875 *t = to_state(sd);
  203. int chvol = 0, volume = 0, balance = 0, left, right;
  204. switch (ctrl->id) {
  205. case V4L2_CID_AUDIO_VOLUME:
  206. left = (t->lvol+84)*606;
  207. right = (t->rvol+84)*606;
  208. volume = max(left,right);
  209. balance = (32768*min(left,right))/
  210. (volume ? volume : 1);
  211. balance =(left<right)?
  212. (65535-balance) : balance;
  213. volume = ctrl->value;
  214. chvol=1;
  215. break;
  216. case V4L2_CID_AUDIO_BALANCE:
  217. left = (t->lvol+84)*606;
  218. right = (t->rvol+84)*606;
  219. volume=max(left,right);
  220. balance = ctrl->value;
  221. chvol=1;
  222. break;
  223. case V4L2_CID_AUDIO_BASS:
  224. t->bass = ((ctrl->value/2400)-12) & 0xff;
  225. if (t->bass > 15)
  226. t->bass = 15;
  227. if (t->bass < -12)
  228. t->bass = -12 & 0xff;
  229. break;
  230. case V4L2_CID_AUDIO_TREBLE:
  231. t->treble = ((ctrl->value/2700)-12) & 0xff;
  232. if (t->treble > 12)
  233. t->treble = 12;
  234. if (t->treble < -12)
  235. t->treble = -12 & 0xff;
  236. break;
  237. default:
  238. return -EINVAL;
  239. }
  240. if (chvol) {
  241. left = (min(65536 - balance,32768) *
  242. volume) / 32768;
  243. right = (min(balance,32768) *
  244. volume) / 32768;
  245. t->lvol = ((left/606)-84) & 0xff;
  246. if (t->lvol > 24)
  247. t->lvol = 24;
  248. if (t->lvol < -84)
  249. t->lvol = -84 & 0xff;
  250. t->rvol = ((right/606)-84) & 0xff;
  251. if (t->rvol > 24)
  252. t->rvol = 24;
  253. if (t->rvol < -84)
  254. t->rvol = -84 & 0xff;
  255. }
  256. tda9875_set(sd);
  257. return 0;
  258. }
  259. static int tda9875_queryctrl(struct v4l2_subdev *sd, struct v4l2_queryctrl *qc)
  260. {
  261. switch (qc->id) {
  262. case V4L2_CID_AUDIO_VOLUME:
  263. return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 58880);
  264. case V4L2_CID_AUDIO_BASS:
  265. case V4L2_CID_AUDIO_TREBLE:
  266. return v4l2_ctrl_query_fill(qc, 0, 65535, 65535 / 100, 32768);
  267. }
  268. return -EINVAL;
  269. }
  270. /* ----------------------------------------------------------------------- */
  271. static const struct v4l2_subdev_core_ops tda9875_core_ops = {
  272. .queryctrl = tda9875_queryctrl,
  273. .g_ctrl = tda9875_g_ctrl,
  274. .s_ctrl = tda9875_s_ctrl,
  275. };
  276. static const struct v4l2_subdev_ops tda9875_ops = {
  277. .core = &tda9875_core_ops,
  278. };
  279. /* ----------------------------------------------------------------------- */
  280. /* *********************** *
  281. * i2c interface functions *
  282. * *********************** */
  283. static int tda9875_checkit(struct i2c_client *client, int addr)
  284. {
  285. int dic, rev;
  286. dic = i2c_read_register(client, addr, 254);
  287. rev = i2c_read_register(client, addr, 255);
  288. if (dic == 0 || dic == 2) { /* tda9875 and tda9875A */
  289. v4l_info(client, "tda9875%s rev. %d detected at 0x%02x\n",
  290. dic == 0 ? "" : "A", rev, addr << 1);
  291. return 1;
  292. }
  293. v4l_info(client, "no such chip at 0x%02x (dic=0x%x rev=0x%x)\n",
  294. addr << 1, dic, rev);
  295. return 0;
  296. }
  297. static int tda9875_probe(struct i2c_client *client,
  298. const struct i2c_device_id *id)
  299. {
  300. struct tda9875 *t;
  301. struct v4l2_subdev *sd;
  302. v4l_info(client, "chip found @ 0x%02x (%s)\n",
  303. client->addr << 1, client->adapter->name);
  304. if (!tda9875_checkit(client, client->addr))
  305. return -ENODEV;
  306. t = kzalloc(sizeof(*t), GFP_KERNEL);
  307. if (!t)
  308. return -ENOMEM;
  309. sd = &t->sd;
  310. v4l2_i2c_subdev_init(sd, client, &tda9875_ops);
  311. do_tda9875_init(sd);
  312. return 0;
  313. }
  314. static int tda9875_remove(struct i2c_client *client)
  315. {
  316. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  317. do_tda9875_init(sd);
  318. v4l2_device_unregister_subdev(sd);
  319. kfree(to_state(sd));
  320. return 0;
  321. }
  322. static const struct i2c_device_id tda9875_id[] = {
  323. { "tda9875", 0 },
  324. { }
  325. };
  326. MODULE_DEVICE_TABLE(i2c, tda9875_id);
  327. static struct i2c_driver tda9875_driver = {
  328. .driver = {
  329. .owner = THIS_MODULE,
  330. .name = "tda9875",
  331. },
  332. .probe = tda9875_probe,
  333. .remove = tda9875_remove,
  334. .id_table = tda9875_id,
  335. };
  336. static __init int init_tda9875(void)
  337. {
  338. return i2c_add_driver(&tda9875_driver);
  339. }
  340. static __exit void exit_tda9875(void)
  341. {
  342. i2c_del_driver(&tda9875_driver);
  343. }
  344. module_init(init_tda9875);
  345. module_exit(exit_tda9875);