tda9875.c 12 KB

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