tea5767.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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.21 2005/07/14 03:06:43 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. #include <media/tuner.h>
  18. #define PREFIX "TEA5767 "
  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. /* Forth 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. /* Fiveth 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. /* Four register */
  81. #define TEA5767_ADC_LEVEL_MASK 0xf0
  82. /* should be 0 */
  83. #define TEA5767_CHIP_ID_MASK 0x0f
  84. /* Fiveth 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->mode == T_STANDBY) {
  156. tuner_dbg("TEA5767 set to standby mode\n");
  157. buffer[3] |= TEA5767_STDBY;
  158. }
  159. if (t->audmode == V4L2_TUNER_MODE_MONO) {
  160. tuner_dbg("TEA5767 set to mono\n");
  161. buffer[2] |= TEA5767_MONO;
  162. } else {
  163. tuner_dbg("TEA5767 set to stereo\n");
  164. }
  165. /* Should be replaced */
  166. switch (TEA5767_HIGH_LO_32768) {
  167. case TEA5767_HIGH_LO_13MHz:
  168. tuner_dbg ("TEA5767 radio HIGH LO inject xtal @ 13 MHz\n");
  169. buffer[2] |= TEA5767_HIGH_LO_INJECT;
  170. buffer[4] |= TEA5767_PLLREF_ENABLE;
  171. div = (frq * 4000 / 16 + 700000 + 225000 + 25000) / 50000;
  172. break;
  173. case TEA5767_LOW_LO_13MHz:
  174. tuner_dbg ("TEA5767 radio LOW LO inject xtal @ 13 MHz\n");
  175. buffer[4] |= TEA5767_PLLREF_ENABLE;
  176. div = (frq * 4000 / 16 - 700000 - 225000 + 25000) / 50000;
  177. break;
  178. case TEA5767_LOW_LO_32768:
  179. tuner_dbg ("TEA5767 radio LOW LO inject xtal @ 32,768 MHz\n");
  180. buffer[3] |= TEA5767_XTAL_32768;
  181. /* const 700=4000*175 Khz - to adjust freq to right value */
  182. div = ((frq * 4000 / 16 - 700000 - 225000) + 16384) >> 15;
  183. break;
  184. case TEA5767_HIGH_LO_32768:
  185. default:
  186. tuner_dbg ("TEA5767 radio HIGH LO inject xtal @ 32,768 MHz\n");
  187. buffer[2] |= TEA5767_HIGH_LO_INJECT;
  188. buffer[3] |= TEA5767_XTAL_32768;
  189. div = ((frq * (4000 / 16) + 700000 + 225000) + 16384) >> 15;
  190. break;
  191. }
  192. buffer[0] = (div >> 8) & 0x3f;
  193. buffer[1] = div & 0xff;
  194. if (5 != (rc = i2c_master_send(c, buffer, 5)))
  195. tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
  196. if (tuner_debug) {
  197. if (5 != (rc = i2c_master_recv(c, buffer, 5)))
  198. tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
  199. else
  200. tea5767_status_dump(buffer);
  201. }
  202. }
  203. static int tea5767_signal(struct i2c_client *c)
  204. {
  205. unsigned char buffer[5];
  206. int rc;
  207. struct tuner *t = i2c_get_clientdata(c);
  208. memset(buffer, 0, sizeof(buffer));
  209. if (5 != (rc = i2c_master_recv(c, buffer, 5)))
  210. tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
  211. return ((buffer[3] & TEA5767_ADC_LEVEL_MASK) << (13 - 4));
  212. }
  213. static int tea5767_stereo(struct i2c_client *c)
  214. {
  215. unsigned char buffer[5];
  216. int rc;
  217. struct tuner *t = i2c_get_clientdata(c);
  218. memset(buffer, 0, sizeof(buffer));
  219. if (5 != (rc = i2c_master_recv(c, buffer, 5)))
  220. tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
  221. rc = buffer[2] & TEA5767_STEREO_MASK;
  222. tuner_dbg("TEA5767 radio ST GET = %02x\n", rc);
  223. return ((buffer[2] & TEA5767_STEREO_MASK) ? V4L2_TUNER_SUB_STEREO : 0);
  224. }
  225. int tea5767_autodetection(struct i2c_client *c)
  226. {
  227. unsigned char buffer[5] = { 0xff, 0xff, 0xff, 0xff, 0xff };
  228. int rc;
  229. struct tuner *t = i2c_get_clientdata(c);
  230. if (5 != (rc = i2c_master_recv(c, buffer, 5))) {
  231. tuner_warn("It is not a TEA5767. Received %i bytes.\n", rc);
  232. return EINVAL;
  233. }
  234. /* If all bytes are the same then it's a TV tuner and not a tea5767 chip. */
  235. if (buffer[0] == buffer[1] && buffer[0] == buffer[2] &&
  236. buffer[0] == buffer[3] && buffer[0] == buffer[4]) {
  237. tuner_warn("All bytes are equal. It is not a TEA5767\n");
  238. return EINVAL;
  239. }
  240. /* Status bytes:
  241. * Byte 4: bit 3:1 : CI (Chip Identification) == 0
  242. * bit 0 : internally set to 0
  243. * Byte 5: bit 7:0 : == 0
  244. */
  245. if (!((buffer[3] & 0x0f) == 0x00) && (buffer[4] == 0x00)) {
  246. tuner_warn("Chip ID is not zero. It is not a TEA5767\n");
  247. return EINVAL;
  248. }
  249. tuner_warn("TEA5767 detected.\n");
  250. return 0;
  251. }
  252. int tea5767_tuner_init(struct i2c_client *c)
  253. {
  254. struct tuner *t = i2c_get_clientdata(c);
  255. if (tea5767_autodetection(c) == EINVAL)
  256. return EINVAL;
  257. tuner_info("type set to %d (%s)\n", t->type, "Philips TEA5767HN FM Radio");
  258. strlcpy(c->name, "tea5767", sizeof(c->name));
  259. t->tv_freq = set_tv_freq;
  260. t->radio_freq = set_radio_freq;
  261. t->has_signal = tea5767_signal;
  262. t->is_stereo = tea5767_stereo;
  263. return (0);
  264. }