tea5767.c 9.6 KB

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