tea5767.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * For Philips TEA5767 FM Chip used on some TV Cards like Prolink Pixelview
  3. * I2C address is allways 0xC0.
  4. *
  5. * $Id: tea5767.c,v 1.11 2005/06/21 15:40:33 mchehab Exp $
  6. *
  7. * Copyright (c) 2005 Mauro Carvalho Chehab (mchehab@brturbo.com.br)
  8. * This code is placed under the terms of the GNU General Public License
  9. *
  10. * tea5767 autodetection thanks to Torsten Seeboth and Atsushi Nakagawa
  11. * from their contributions on DScaler.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/sched.h>
  17. #include <linux/string.h>
  18. #include <linux/timer.h>
  19. #include <linux/delay.h>
  20. #include <linux/errno.h>
  21. #include <linux/slab.h>
  22. #include <linux/videodev.h>
  23. #include <linux/i2c.h>
  24. #include <linux/i2c-algo-bit.h>
  25. #include <media/tuner.h>
  26. /* Declared at tuner-core.c */
  27. extern unsigned int tuner_debug;
  28. #define PREFIX "TEA5767 "
  29. /*****************************************************************************/
  30. /******************************
  31. * Write mode register values *
  32. ******************************/
  33. /* First register */
  34. #define TEA5767_MUTE 0x80 /* Mutes output */
  35. #define TEA5767_SEARCH 0x40 /* Activates station search */
  36. /* Bits 0-5 for divider MSB */
  37. /* Second register */
  38. /* Bits 0-7 for divider LSB */
  39. /* Third register */
  40. /* Station search from botton to up */
  41. #define TEA5767_SEARCH_UP 0x80
  42. /* Searches with ADC output = 10 */
  43. #define TEA5767_SRCH_HIGH_LVL 0x60
  44. /* Searches with ADC output = 10 */
  45. #define TEA5767_SRCH_MID_LVL 0x40
  46. /* Searches with ADC output = 5 */
  47. #define TEA5767_SRCH_LOW_LVL 0x20
  48. /* if on, div=4*(Frf+Fif)/Fref otherwise, div=4*(Frf-Fif)/Freq) */
  49. #define TEA5767_HIGH_LO_INJECT 0x10
  50. /* Disable stereo */
  51. #define TEA5767_MONO 0x08
  52. /* Disable right channel and turns to mono */
  53. #define TEA5767_MUTE_RIGHT 0x04
  54. /* Disable left channel and turns to mono */
  55. #define TEA5767_MUTE_LEFT 0x02
  56. #define TEA5767_PORT1_HIGH 0x01
  57. /* Forth register */
  58. #define TEA5767_PORT2_HIGH 0x80
  59. /* Chips stops working. Only I2C bus remains on */
  60. #define TEA5767_STDBY 0x40
  61. /* Japan freq (76-108 MHz. If disabled, 87.5-108 MHz */
  62. #define TEA5767_JAPAN_BAND 0x20
  63. /* Unselected means 32.768 KHz freq as reference. Otherwise Xtal at 13 MHz */
  64. #define TEA5767_XTAL_32768 0x10
  65. /* Cuts weak signals */
  66. #define TEA5767_SOFT_MUTE 0x08
  67. /* Activates high cut control */
  68. #define TEA5767_HIGH_CUT_CTRL 0x04
  69. /* Activates stereo noise control */
  70. #define TEA5767_ST_NOISE_CTL 0x02
  71. /* If activate PORT 1 indicates SEARCH or else it is used as PORT1 */
  72. #define TEA5767_SRCH_IND 0x01
  73. /* Fiveth register */
  74. /* By activating, it will use Xtal at 13 MHz as reference for divider */
  75. #define TEA5767_PLLREF_ENABLE 0x80
  76. /* By activating, deemphasis=50, or else, deemphasis of 50us */
  77. #define TEA5767_DEEMPH_75 0X40
  78. /*****************************
  79. * Read mode register values *
  80. *****************************/
  81. /* First register */
  82. #define TEA5767_READY_FLAG_MASK 0x80
  83. #define TEA5767_BAND_LIMIT_MASK 0X40
  84. /* Bits 0-5 for divider MSB after search or preset */
  85. /* Second register */
  86. /* Bits 0-7 for divider LSB after search or preset */
  87. /* Third register */
  88. #define TEA5767_STEREO_MASK 0x80
  89. #define TEA5767_IF_CNTR_MASK 0x7f
  90. /* Four register */
  91. #define TEA5767_ADC_LEVEL_MASK 0xf0
  92. /* should be 0 */
  93. #define TEA5767_CHIP_ID_MASK 0x0f
  94. /* Fiveth register */
  95. /* Reserved for future extensions */
  96. #define TEA5767_RESERVED_MASK 0xff
  97. /*****************************************************************************/
  98. static void set_tv_freq(struct i2c_client *c, unsigned int freq)
  99. {
  100. struct tuner *t = i2c_get_clientdata(c);
  101. tuner_warn("This tuner doesn't support TV freq.\n");
  102. }
  103. static void tea5767_status_dump(unsigned char *buffer)
  104. {
  105. unsigned int div, frq;
  106. if (TEA5767_READY_FLAG_MASK & buffer[0])
  107. printk(PREFIX "Ready Flag ON\n");
  108. else
  109. printk(PREFIX "Ready Flag OFF\n");
  110. if (TEA5767_BAND_LIMIT_MASK & buffer[0])
  111. printk(PREFIX "Tuner at band limit\n");
  112. else
  113. printk(PREFIX "Tuner not at band limit\n");
  114. div=((buffer[0]&0x3f)<<8) | buffer[1];
  115. switch (TEA5767_HIGH_LO_32768) {
  116. case TEA5767_HIGH_LO_13MHz:
  117. frq = 1000*(div*50-700-225)/4; /* Freq in KHz */
  118. break;
  119. case TEA5767_LOW_LO_13MHz:
  120. frq = 1000*(div*50+700+225)/4; /* Freq in KHz */
  121. break;
  122. case TEA5767_LOW_LO_32768:
  123. frq = 1000*(div*32768/1000+700+225)/4; /* Freq in KHz */
  124. break;
  125. case TEA5767_HIGH_LO_32768:
  126. default:
  127. frq = 1000*(div*32768/1000-700-225)/4; /* Freq in KHz */
  128. break;
  129. }
  130. buffer[0] = (div>>8) & 0x3f;
  131. buffer[1] = div & 0xff;
  132. printk(PREFIX "Frequency %d.%03d KHz (divider = 0x%04x)\n",
  133. frq/1000,frq%1000,div);
  134. if (TEA5767_STEREO_MASK & buffer[2])
  135. printk(PREFIX "Stereo\n");
  136. else
  137. printk(PREFIX "Mono\n");
  138. printk(PREFIX "IF Counter = %d\n",buffer[2] & TEA5767_IF_CNTR_MASK);
  139. printk(PREFIX "ADC Level = %d\n",(buffer[3] & TEA5767_ADC_LEVEL_MASK)>>4);
  140. printk(PREFIX "Chip ID = %d\n",(buffer[3] & TEA5767_CHIP_ID_MASK));
  141. printk(PREFIX "Reserved = 0x%02x\n",(buffer[4] & TEA5767_RESERVED_MASK));
  142. }
  143. /* Freq should be specifyed at 62.5 Hz */
  144. static void set_radio_freq(struct i2c_client *c, unsigned int frq)
  145. {
  146. struct tuner *t = i2c_get_clientdata(c);
  147. unsigned char buffer[5];
  148. unsigned div;
  149. int rc;
  150. if ( tuner_debug )
  151. printk(PREFIX "radio freq counter %d\n",frq);
  152. /* Rounds freq to next decimal value - for 62.5 KHz step */
  153. /* frq = 20*(frq/16)+radio_frq[frq%16]; */
  154. buffer[2] = TEA5767_PORT1_HIGH;
  155. buffer[3] = TEA5767_PORT2_HIGH | TEA5767_HIGH_CUT_CTRL | TEA5767_ST_NOISE_CTL | TEA5767_JAPAN_BAND;
  156. buffer[4]=0;
  157. if (t->audmode == V4L2_TUNER_MODE_MONO) {
  158. tuner_dbg("TEA5767 set to mono\n");
  159. buffer[2] |= TEA5767_MONO;
  160. } else
  161. tuner_dbg("TEA5767 set to stereo\n");
  162. switch (t->type) {
  163. case TEA5767_HIGH_LO_13MHz:
  164. tuner_dbg("TEA5767 radio HIGH LO inject xtal @ 13 MHz\n");
  165. buffer[2] |= TEA5767_HIGH_LO_INJECT;
  166. buffer[4] |= TEA5767_PLLREF_ENABLE;
  167. div = (frq*4/16+700+225+25)/50;
  168. break;
  169. case TEA5767_LOW_LO_13MHz:
  170. tuner_dbg("TEA5767 radio LOW LO inject xtal @ 13 MHz\n");
  171. buffer[4] |= TEA5767_PLLREF_ENABLE;
  172. div = (frq*4/16-700-225+25)/50;
  173. break;
  174. case TEA5767_LOW_LO_32768:
  175. tuner_dbg("TEA5767 radio LOW LO inject xtal @ 32,768 MHz\n");
  176. buffer[3] |= TEA5767_XTAL_32768;
  177. /* const 700=4000*175 Khz - to adjust freq to right value */
  178. div = (1000*(frq*4/16-700-225)+16384)>>15;
  179. break;
  180. case TEA5767_HIGH_LO_32768:
  181. default:
  182. tuner_dbg("TEA5767 radio HIGH LO inject xtal @ 32,768 MHz\n");
  183. buffer[2] |= TEA5767_HIGH_LO_INJECT;
  184. buffer[3] |= TEA5767_XTAL_32768;
  185. div = (1000*(frq*4/16+700+225)+16384)>>15;
  186. break;
  187. }
  188. buffer[0] = (div>>8) & 0x3f;
  189. buffer[1] = div & 0xff;
  190. if ( tuner_debug )
  191. tea5767_status_dump(buffer);
  192. if (5 != (rc = i2c_master_send(c,buffer,5)))
  193. tuner_warn("i2c i/o error: rc == %d (should be 5)\n",rc);
  194. }
  195. static int tea5767_signal(struct i2c_client *c)
  196. {
  197. unsigned char buffer[5];
  198. int rc;
  199. struct tuner *t = i2c_get_clientdata(c);
  200. memset(buffer,0,sizeof(buffer));
  201. if (5 != (rc = i2c_master_recv(c,buffer,5)))
  202. tuner_warn ( "i2c i/o error: rc == %d (should be 5)\n",rc);
  203. return ((buffer[3] & TEA5767_ADC_LEVEL_MASK) <<(13-4));
  204. }
  205. static int tea5767_stereo(struct i2c_client *c)
  206. {
  207. unsigned char buffer[5];
  208. int rc;
  209. struct tuner *t = i2c_get_clientdata(c);
  210. memset(buffer,0,sizeof(buffer));
  211. if (5 != (rc = i2c_master_recv(c,buffer,5)))
  212. tuner_warn ( "i2c i/o error: rc == %d (should be 5)\n",rc);
  213. rc = buffer[2] & TEA5767_STEREO_MASK;
  214. if ( tuner_debug )
  215. tuner_dbg("TEA5767 radio ST GET = %02x\n", rc);
  216. return ( (buffer[2] & TEA5767_STEREO_MASK) ? V4L2_TUNER_SUB_STEREO: 0);
  217. }
  218. int tea_detection(struct i2c_client *c)
  219. {
  220. unsigned char buffer[5]= { 0xff, 0xff, 0xff, 0xff, 0xff };
  221. int rc;
  222. struct tuner *t = i2c_get_clientdata(c);
  223. if (5 != (rc = i2c_master_recv(c,buffer,5))) {
  224. tuner_warn ( "it is not a TEA5767. Received %i chars.\n",rc );
  225. return EINVAL;
  226. }
  227. /* If all bytes are the same then it's a TV tuner and not a tea5767 chip. */
  228. if (buffer[0] == buffer[1] && buffer[0] == buffer[2] &&
  229. buffer[0] == buffer[3] && buffer[0] == buffer[4]) {
  230. tuner_warn ( "All bytes are equal. It is not a TEA5767\n" );
  231. return EINVAL;
  232. }
  233. /* Status bytes:
  234. * Byte 4: bit 3:1 : CI (Chip Identification) == 0
  235. * bit 0 : internally set to 0
  236. * Byte 5: bit 7:0 : == 0
  237. */
  238. if (!((buffer[3] & 0x0f) == 0x00) && (buffer[4] == 0x00)) {
  239. tuner_warn ( "Chip ID is not zero. It is not a TEA5767\n" );
  240. return EINVAL;
  241. }
  242. tuner_warn ( "TEA5767 detected.\n" );
  243. return 0;
  244. }
  245. int tea5767_tuner_init(struct i2c_client *c)
  246. {
  247. struct tuner *t = i2c_get_clientdata(c);
  248. if (tea_detection(c)==EINVAL) return EINVAL;
  249. tuner_info("type set to %d (%s)\n",
  250. t->type, TEA5767_TUNER_NAME);
  251. strlcpy(c->name, TEA5767_TUNER_NAME, sizeof(c->name));
  252. t->tv_freq = set_tv_freq;
  253. t->radio_freq = set_radio_freq;
  254. t->has_signal = tea5767_signal;
  255. t->is_stereo = tea5767_stereo;
  256. return (0);
  257. }