tda9875.c 11 KB

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