tda9875.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. * This code is placed under the terms of the GNU General Public License
  11. * Based on tda9855.c by Steve VanDeBogart (vandebo@uclink.berkeley.edu)
  12. * Which was based on tda8425.c by Greg Alexander (c) 1998
  13. *
  14. * OPTIONS:
  15. * debug - set to 1 if you'd like to see debug messages
  16. *
  17. * Revision: 0.1 - original version
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/sched.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/videodev.h>
  28. #include <linux/i2c.h>
  29. #include <linux/i2c-algo-bit.h>
  30. #include <linux/init.h>
  31. #include "bttv.h"
  32. #include <media/audiochip.h>
  33. #include <media/id.h>
  34. static int debug; /* insmod parameter */
  35. module_param(debug, int, S_IRUGO | S_IWUSR);
  36. MODULE_LICENSE("GPL");
  37. /* Addresses to scan */
  38. static unsigned short normal_i2c[] = {
  39. I2C_TDA9875 >> 1,
  40. I2C_CLIENT_END
  41. };
  42. static unsigned short normal_i2c_range[] = {I2C_CLIENT_END};
  43. I2C_CLIENT_INSMOD;
  44. /* This is a superset of the TDA9875 */
  45. struct tda9875 {
  46. int mode;
  47. int rvol, lvol;
  48. int bass, treble;
  49. struct i2c_client c;
  50. };
  51. static struct i2c_driver driver;
  52. static struct i2c_client client_template;
  53. #define dprintk if (debug) printk
  54. /* The TDA9875 is made by Philips Semiconductor
  55. * http://www.semiconductors.philips.com
  56. * TDA9875: I2C-bus controlled DSP audio processor, FM demodulator
  57. *
  58. */
  59. /* subaddresses for TDA9875 */
  60. #define TDA9875_MUT 0x12 /*General mute (value --> 0b11001100*/
  61. #define TDA9875_CFG 0x01 /* Config register (value --> 0b00000000 */
  62. #define TDA9875_DACOS 0x13 /*DAC i/o select (ADC) 0b0000100*/
  63. #define TDA9875_LOSR 0x16 /*Line output select regirter 0b0100 0001*/
  64. #define TDA9875_CH1V 0x0c /*Channel 1 volume (mute)*/
  65. #define TDA9875_CH2V 0x0d /*Channel 2 volume (mute)*/
  66. #define TDA9875_SC1 0x14 /*SCART 1 in (mono)*/
  67. #define TDA9875_SC2 0x15 /*SCART 2 in (mono)*/
  68. #define TDA9875_ADCIS 0x17 /*ADC input select (mono) 0b0110 000*/
  69. #define TDA9875_AER 0x19 /*Audio effect (AVL+Pseudo) 0b0000 0110*/
  70. #define TDA9875_MCS 0x18 /*Main channel select (DAC) 0b0000100*/
  71. #define TDA9875_MVL 0x1a /* Main volume gauche */
  72. #define TDA9875_MVR 0x1b /* Main volume droite */
  73. #define TDA9875_MBA 0x1d /* Main Basse */
  74. #define TDA9875_MTR 0x1e /* Main treble */
  75. #define TDA9875_ACS 0x1f /* Auxilary channel select (FM) 0b0000000*/
  76. #define TDA9875_AVL 0x20 /* Auxilary volume gauche */
  77. #define TDA9875_AVR 0x21 /* Auxilary volume droite */
  78. #define TDA9875_ABA 0x22 /* Auxilary Basse */
  79. #define TDA9875_ATR 0x23 /* Auxilary treble */
  80. #define TDA9875_MSR 0x02 /* Monitor select register */
  81. #define TDA9875_C1MSB 0x03 /* Carrier 1 (FM) frequency register MSB */
  82. #define TDA9875_C1MIB 0x04 /* Carrier 1 (FM) frequency register (16-8]b */
  83. #define TDA9875_C1LSB 0x05 /* Carrier 1 (FM) frequency register LSB */
  84. #define TDA9875_C2MSB 0x06 /* Carrier 2 (nicam) frequency register MSB */
  85. #define TDA9875_C2MIB 0x07 /* Carrier 2 (nicam) frequency register (16-8]b */
  86. #define TDA9875_C2LSB 0x08 /* Carrier 2 (nicam) frequency register LSB */
  87. #define TDA9875_DCR 0x09 /* Demodulateur configuration regirter*/
  88. #define TDA9875_DEEM 0x0a /* FM de-emphasis regirter*/
  89. #define TDA9875_FMAT 0x0b /* FM Matrix regirter*/
  90. /* values */
  91. #define TDA9875_MUTE_ON 0xff /* general mute */
  92. #define TDA9875_MUTE_OFF 0xcc /* general no mute */
  93. /* Begin code */
  94. static int tda9875_write(struct i2c_client *client, int subaddr, unsigned char val)
  95. {
  96. unsigned char buffer[2];
  97. dprintk("In tda9875_write\n");
  98. dprintk("Writing %d 0x%x\n", subaddr, val);
  99. buffer[0] = subaddr;
  100. buffer[1] = val;
  101. if (2 != i2c_master_send(client,buffer,2)) {
  102. printk(KERN_WARNING "tda9875: I/O error, trying (write %d 0x%x)\n",
  103. subaddr, val);
  104. return -1;
  105. }
  106. return 0;
  107. }
  108. #if 0
  109. static int tda9875_read(struct i2c_client *client)
  110. {
  111. unsigned char buffer;
  112. dprintk("In tda9875_read\n");
  113. if (1 != i2c_master_recv(client,&buffer,1)) {
  114. printk(KERN_WARNING "tda9875: I/O error, trying (read)\n");
  115. return -1;
  116. }
  117. dprintk("Read 0x%02x\n", buffer);
  118. return buffer;
  119. }
  120. #endif
  121. static int i2c_read_register(struct i2c_adapter *adap, int addr, int reg)
  122. {
  123. unsigned char write[1];
  124. unsigned char read[1];
  125. struct i2c_msg msgs[2] = {
  126. { addr, 0, 1, write },
  127. { addr, I2C_M_RD, 1, read }
  128. };
  129. write[0] = reg;
  130. if (2 != i2c_transfer(adap,msgs,2)) {
  131. printk(KERN_WARNING "tda9875: I/O error (read2)\n");
  132. return -1;
  133. }
  134. dprintk("tda9875: chip_read2: reg%d=0x%x\n",reg,read[0]);
  135. return read[0];
  136. }
  137. static void tda9875_set(struct i2c_client *client)
  138. {
  139. struct tda9875 *tda = i2c_get_clientdata(client);
  140. unsigned char a;
  141. dprintk(KERN_DEBUG "tda9875_set(%04x,%04x,%04x,%04x)\n",
  142. tda->lvol,tda->rvol,tda->bass,tda->treble);
  143. a = tda->lvol & 0xff;
  144. tda9875_write(client, TDA9875_MVL, a);
  145. a =tda->rvol & 0xff;
  146. tda9875_write(client, TDA9875_MVR, a);
  147. a =tda->bass & 0xff;
  148. tda9875_write(client, TDA9875_MBA, a);
  149. a =tda->treble & 0xff;
  150. tda9875_write(client, TDA9875_MTR, a);
  151. }
  152. static void do_tda9875_init(struct i2c_client *client)
  153. {
  154. struct tda9875 *t = i2c_get_clientdata(client);
  155. dprintk("In tda9875_init\n");
  156. tda9875_write(client, TDA9875_CFG, 0xd0 ); /*reg de config 0 (reset)*/
  157. tda9875_write(client, TDA9875_MSR, 0x03 ); /* Monitor 0b00000XXX*/
  158. tda9875_write(client, TDA9875_C1MSB, 0x00 ); /*Car1(FM) MSB XMHz*/
  159. tda9875_write(client, TDA9875_C1MIB, 0x00 ); /*Car1(FM) MIB XMHz*/
  160. tda9875_write(client, TDA9875_C1LSB, 0x00 ); /*Car1(FM) LSB XMHz*/
  161. tda9875_write(client, TDA9875_C2MSB, 0x00 ); /*Car2(NICAM) MSB XMHz*/
  162. tda9875_write(client, TDA9875_C2MIB, 0x00 ); /*Car2(NICAM) MIB XMHz*/
  163. tda9875_write(client, TDA9875_C2LSB, 0x00 ); /*Car2(NICAM) LSB XMHz*/
  164. tda9875_write(client, TDA9875_DCR, 0x00 ); /*Demod config 0x00*/
  165. tda9875_write(client, TDA9875_DEEM, 0x44 ); /*DE-Emph 0b0100 0100*/
  166. tda9875_write(client, TDA9875_FMAT, 0x00 ); /*FM Matrix reg 0x00*/
  167. tda9875_write(client, TDA9875_SC1, 0x00 ); /* SCART 1 (SC1)*/
  168. tda9875_write(client, TDA9875_SC2, 0x01 ); /* SCART 2 (sc2)*/
  169. tda9875_write(client, TDA9875_CH1V, 0x10 ); /* Channel volume 1 mute*/
  170. tda9875_write(client, TDA9875_CH2V, 0x10 ); /* Channel volume 2 mute */
  171. tda9875_write(client, TDA9875_DACOS, 0x02 ); /* sig DAC i/o(in:nicam)*/
  172. tda9875_write(client, TDA9875_ADCIS, 0x6f ); /* sig ADC input(in:mono)*/
  173. tda9875_write(client, TDA9875_LOSR, 0x00 ); /* line out (in:mono)*/
  174. tda9875_write(client, TDA9875_AER, 0x00 ); /*06 Effect (AVL+PSEUDO) */
  175. tda9875_write(client, TDA9875_MCS, 0x44 ); /* Main ch select (DAC) */
  176. tda9875_write(client, TDA9875_MVL, 0x03 ); /* Vol Main left 10dB */
  177. tda9875_write(client, TDA9875_MVR, 0x03 ); /* Vol Main right 10dB*/
  178. tda9875_write(client, TDA9875_MBA, 0x00 ); /* Main Bass Main 0dB*/
  179. tda9875_write(client, TDA9875_MTR, 0x00 ); /* Main Treble Main 0dB*/
  180. tda9875_write(client, TDA9875_ACS, 0x44 ); /* Aux chan select (dac)*/
  181. tda9875_write(client, TDA9875_AVL, 0x00 ); /* Vol Aux left 0dB*/
  182. tda9875_write(client, TDA9875_AVR, 0x00 ); /* Vol Aux right 0dB*/
  183. tda9875_write(client, TDA9875_ABA, 0x00 ); /* Aux Bass Main 0dB*/
  184. tda9875_write(client, TDA9875_ATR, 0x00 ); /* Aux Aigus Main 0dB*/
  185. tda9875_write(client, TDA9875_MUT, 0xcc ); /* General mute */
  186. t->mode=AUDIO_UNMUTE;
  187. t->lvol=t->rvol =0; /* 0dB */
  188. t->bass=0; /* 0dB */
  189. t->treble=0; /* 0dB */
  190. tda9875_set(client);
  191. }
  192. /* *********************** *
  193. * i2c interface functions *
  194. * *********************** */
  195. static int tda9875_checkit(struct i2c_adapter *adap, int addr)
  196. {
  197. int dic,rev;
  198. dic=i2c_read_register(adap,addr,254);
  199. rev=i2c_read_register(adap,addr,255);
  200. if(dic==0 || dic==2) { // tda9875 and tda9875A
  201. printk("tda9875: TDA9875%s Rev.%d detected at 0x%x\n",
  202. dic==0?"":"A", rev,addr<<1);
  203. return 1;
  204. }
  205. printk("tda9875: no such chip at 0x%x (dic=0x%x rev=0x%x)\n",addr<<1,dic,rev);
  206. return(0);
  207. }
  208. static int tda9875_attach(struct i2c_adapter *adap, int addr, int kind)
  209. {
  210. struct tda9875 *t;
  211. struct i2c_client *client;
  212. dprintk("In tda9875_attach\n");
  213. t = kmalloc(sizeof *t,GFP_KERNEL);
  214. if (!t)
  215. return -ENOMEM;
  216. memset(t,0,sizeof *t);
  217. client = &t->c;
  218. memcpy(client,&client_template,sizeof(struct i2c_client));
  219. client->adapter = adap;
  220. client->addr = addr;
  221. i2c_set_clientdata(client, t);
  222. if(!tda9875_checkit(adap,addr)) {
  223. kfree(t);
  224. return 1;
  225. }
  226. do_tda9875_init(client);
  227. printk(KERN_INFO "tda9875: init\n");
  228. i2c_attach_client(client);
  229. return 0;
  230. }
  231. static int tda9875_probe(struct i2c_adapter *adap)
  232. {
  233. #ifdef I2C_CLASS_TV_ANALOG
  234. if (adap->class & I2C_CLASS_TV_ANALOG)
  235. return i2c_probe(adap, &addr_data, tda9875_attach);
  236. #else
  237. if (adap->id == (I2C_ALGO_BIT | I2C_HW_B_BT848))
  238. return i2c_probe(adap, &addr_data, tda9875_attach);
  239. #endif
  240. return 0;
  241. }
  242. static int tda9875_detach(struct i2c_client *client)
  243. {
  244. struct tda9875 *t = i2c_get_clientdata(client);
  245. do_tda9875_init(client);
  246. i2c_detach_client(client);
  247. kfree(t);
  248. return 0;
  249. }
  250. static int tda9875_command(struct i2c_client *client,
  251. unsigned int cmd, void *arg)
  252. {
  253. struct tda9875 *t = i2c_get_clientdata(client);
  254. dprintk("In tda9875_command...\n");
  255. switch (cmd) {
  256. /* --- v4l ioctls --- */
  257. /* take care: bttv does userspace copying, we'll get a
  258. kernel pointer here... */
  259. case VIDIOCGAUDIO:
  260. {
  261. struct video_audio *va = arg;
  262. int left,right;
  263. dprintk("VIDIOCGAUDIO\n");
  264. va->flags |= VIDEO_AUDIO_VOLUME |
  265. VIDEO_AUDIO_BASS |
  266. VIDEO_AUDIO_TREBLE;
  267. /* min is -84 max is 24 */
  268. left = (t->lvol+84)*606;
  269. right = (t->rvol+84)*606;
  270. va->volume=max(left,right);
  271. va->balance=(32768*min(left,right))/
  272. (va->volume ? va->volume : 1);
  273. va->balance=(left<right)?
  274. (65535-va->balance) : va->balance;
  275. va->bass = (t->bass+12)*2427; /* min -12 max +15 */
  276. va->treble = (t->treble+12)*2730;/* min -12 max +12 */
  277. va->mode |= VIDEO_SOUND_MONO;
  278. break; /* VIDIOCGAUDIO case */
  279. }
  280. case VIDIOCSAUDIO:
  281. {
  282. struct video_audio *va = arg;
  283. int left,right;
  284. dprintk("VIDEOCSAUDIO...\n");
  285. left = (min(65536 - va->balance,32768) *
  286. va->volume) / 32768;
  287. right = (min(va->balance,(__u16)32768) *
  288. va->volume) / 32768;
  289. t->lvol = ((left/606)-84) & 0xff;
  290. if (t->lvol > 24)
  291. t->lvol = 24;
  292. if (t->lvol < -84)
  293. t->lvol = -84 & 0xff;
  294. t->rvol = ((right/606)-84) & 0xff;
  295. if (t->rvol > 24)
  296. t->rvol = 24;
  297. if (t->rvol < -84)
  298. t->rvol = -84 & 0xff;
  299. t->bass = ((va->bass/2400)-12) & 0xff;
  300. if (t->bass > 15)
  301. t->bass = 15;
  302. if (t->bass < -12)
  303. t->bass = -12 & 0xff;
  304. t->treble = ((va->treble/2700)-12) & 0xff;
  305. if (t->treble > 12)
  306. t->treble = 12;
  307. if (t->treble < -12)
  308. t->treble = -12 & 0xff;
  309. //printk("tda9875 bal:%04x vol:%04x bass:%04x treble:%04x\n",va->balance,va->volume,va->bass,va->treble);
  310. tda9875_set(client);
  311. break;
  312. } /* end of VIDEOCSAUDIO case */
  313. default: /* Not VIDEOCGAUDIO or VIDEOCSAUDIO */
  314. /* nothing */
  315. dprintk("Default\n");
  316. } /* end of (cmd) switch */
  317. return 0;
  318. }
  319. static struct i2c_driver driver = {
  320. .owner = THIS_MODULE,
  321. .name = "i2c tda9875 driver",
  322. .id = I2C_DRIVERID_TDA9875,
  323. .flags = I2C_DF_NOTIFY,
  324. .attach_adapter = tda9875_probe,
  325. .detach_client = tda9875_detach,
  326. .command = tda9875_command,
  327. };
  328. static struct i2c_client client_template =
  329. {
  330. I2C_DEVNAME("tda9875"),
  331. .driver = &driver,
  332. };
  333. static int __init tda9875_init(void)
  334. {
  335. return i2c_add_driver(&driver);
  336. }
  337. static void __exit tda9875_fini(void)
  338. {
  339. i2c_del_driver(&driver);
  340. }
  341. module_init(tda9875_init);
  342. module_exit(tda9875_fini);
  343. /*
  344. * Local variables:
  345. * c-basic-offset: 8
  346. * End:
  347. */