tea5767.c 9.5 KB

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