tea5767.c 10 KB

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