tea5767.c 9.3 KB

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