tea5767.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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/videodev2.h>
  15. #include "tuner-i2c.h"
  16. #include "tea5767.h"
  17. static int debug;
  18. module_param(debug, int, 0644);
  19. MODULE_PARM_DESC(debug, "enable verbose debug messages");
  20. /*****************************************************************************/
  21. struct tea5767_priv {
  22. struct tuner_i2c_props i2c_props;
  23. u32 frequency;
  24. struct tea5767_ctrl ctrl;
  25. };
  26. /*****************************************************************************/
  27. /******************************
  28. * Write mode register values *
  29. ******************************/
  30. /* First register */
  31. #define TEA5767_MUTE 0x80 /* Mutes output */
  32. #define TEA5767_SEARCH 0x40 /* Activates station search */
  33. /* Bits 0-5 for divider MSB */
  34. /* Second register */
  35. /* Bits 0-7 for divider LSB */
  36. /* Third register */
  37. /* Station search from botton to up */
  38. #define TEA5767_SEARCH_UP 0x80
  39. /* Searches with ADC output = 10 */
  40. #define TEA5767_SRCH_HIGH_LVL 0x60
  41. /* Searches with ADC output = 10 */
  42. #define TEA5767_SRCH_MID_LVL 0x40
  43. /* Searches with ADC output = 5 */
  44. #define TEA5767_SRCH_LOW_LVL 0x20
  45. /* if on, div=4*(Frf+Fif)/Fref otherwise, div=4*(Frf-Fif)/Freq) */
  46. #define TEA5767_HIGH_LO_INJECT 0x10
  47. /* Disable stereo */
  48. #define TEA5767_MONO 0x08
  49. /* Disable right channel and turns to mono */
  50. #define TEA5767_MUTE_RIGHT 0x04
  51. /* Disable left channel and turns to mono */
  52. #define TEA5767_MUTE_LEFT 0x02
  53. #define TEA5767_PORT1_HIGH 0x01
  54. /* Fourth register */
  55. #define TEA5767_PORT2_HIGH 0x80
  56. /* Chips stops working. Only I2C bus remains on */
  57. #define TEA5767_STDBY 0x40
  58. /* Japan freq (76-108 MHz. If disabled, 87.5-108 MHz */
  59. #define TEA5767_JAPAN_BAND 0x20
  60. /* Unselected means 32.768 KHz freq as reference. Otherwise Xtal at 13 MHz */
  61. #define TEA5767_XTAL_32768 0x10
  62. /* Cuts weak signals */
  63. #define TEA5767_SOFT_MUTE 0x08
  64. /* Activates high cut control */
  65. #define TEA5767_HIGH_CUT_CTRL 0x04
  66. /* Activates stereo noise control */
  67. #define TEA5767_ST_NOISE_CTL 0x02
  68. /* If activate PORT 1 indicates SEARCH or else it is used as PORT1 */
  69. #define TEA5767_SRCH_IND 0x01
  70. /* Fifth register */
  71. /* By activating, it will use Xtal at 13 MHz as reference for divider */
  72. #define TEA5767_PLLREF_ENABLE 0x80
  73. /* By activating, deemphasis=50, or else, deemphasis of 50us */
  74. #define TEA5767_DEEMPH_75 0X40
  75. /*****************************
  76. * Read mode register values *
  77. *****************************/
  78. /* First register */
  79. #define TEA5767_READY_FLAG_MASK 0x80
  80. #define TEA5767_BAND_LIMIT_MASK 0X40
  81. /* Bits 0-5 for divider MSB after search or preset */
  82. /* Second register */
  83. /* Bits 0-7 for divider LSB after search or preset */
  84. /* Third register */
  85. #define TEA5767_STEREO_MASK 0x80
  86. #define TEA5767_IF_CNTR_MASK 0x7f
  87. /* Fourth register */
  88. #define TEA5767_ADC_LEVEL_MASK 0xf0
  89. /* should be 0 */
  90. #define TEA5767_CHIP_ID_MASK 0x0f
  91. /* Fifth register */
  92. /* Reserved for future extensions */
  93. #define TEA5767_RESERVED_MASK 0xff
  94. /*****************************************************************************/
  95. static void tea5767_status_dump(struct tea5767_priv *priv,
  96. unsigned char *buffer)
  97. {
  98. unsigned int div, frq;
  99. if (TEA5767_READY_FLAG_MASK & buffer[0])
  100. tuner_info("Ready Flag ON\n");
  101. else
  102. tuner_info("Ready Flag OFF\n");
  103. if (TEA5767_BAND_LIMIT_MASK & buffer[0])
  104. tuner_info("Tuner at band limit\n");
  105. else
  106. tuner_info("Tuner not at band limit\n");
  107. div = ((buffer[0] & 0x3f) << 8) | buffer[1];
  108. switch (priv->ctrl.xtal_freq) {
  109. case TEA5767_HIGH_LO_13MHz:
  110. frq = (div * 50000 - 700000 - 225000) / 4; /* Freq in KHz */
  111. break;
  112. case TEA5767_LOW_LO_13MHz:
  113. frq = (div * 50000 + 700000 + 225000) / 4; /* Freq in KHz */
  114. break;
  115. case TEA5767_LOW_LO_32768:
  116. frq = (div * 32768 + 700000 + 225000) / 4; /* Freq in KHz */
  117. break;
  118. case TEA5767_HIGH_LO_32768:
  119. default:
  120. frq = (div * 32768 - 700000 - 225000) / 4; /* Freq in KHz */
  121. break;
  122. }
  123. buffer[0] = (div >> 8) & 0x3f;
  124. buffer[1] = div & 0xff;
  125. tuner_info("Frequency %d.%03d KHz (divider = 0x%04x)\n",
  126. frq / 1000, frq % 1000, div);
  127. if (TEA5767_STEREO_MASK & buffer[2])
  128. tuner_info("Stereo\n");
  129. else
  130. tuner_info("Mono\n");
  131. tuner_info("IF Counter = %d\n", buffer[2] & TEA5767_IF_CNTR_MASK);
  132. tuner_info("ADC Level = %d\n",
  133. (buffer[3] & TEA5767_ADC_LEVEL_MASK) >> 4);
  134. tuner_info("Chip ID = %d\n", (buffer[3] & TEA5767_CHIP_ID_MASK));
  135. tuner_info("Reserved = 0x%02x\n",
  136. (buffer[4] & TEA5767_RESERVED_MASK));
  137. }
  138. /* Freq should be specifyed at 62.5 Hz */
  139. static int set_radio_freq(struct dvb_frontend *fe,
  140. struct analog_parameters *params)
  141. {
  142. struct tea5767_priv *priv = fe->tuner_priv;
  143. unsigned int frq = params->frequency;
  144. unsigned char buffer[5];
  145. unsigned div;
  146. int rc;
  147. tuner_dbg("radio freq = %d.%03d MHz\n", frq/16000,(frq/16)%1000);
  148. buffer[2] = 0;
  149. if (priv->ctrl.port1)
  150. buffer[2] |= TEA5767_PORT1_HIGH;
  151. if (params->audmode == V4L2_TUNER_MODE_MONO) {
  152. tuner_dbg("TEA5767 set to mono\n");
  153. buffer[2] |= TEA5767_MONO;
  154. } else {
  155. tuner_dbg("TEA5767 set to stereo\n");
  156. }
  157. buffer[3] = 0;
  158. if (priv->ctrl.port2)
  159. buffer[3] |= TEA5767_PORT2_HIGH;
  160. if (priv->ctrl.high_cut)
  161. buffer[3] |= TEA5767_HIGH_CUT_CTRL;
  162. if (priv->ctrl.st_noise)
  163. buffer[3] |= TEA5767_ST_NOISE_CTL;
  164. if (priv->ctrl.soft_mute)
  165. buffer[3] |= TEA5767_SOFT_MUTE;
  166. if (priv->ctrl.japan_band)
  167. buffer[3] |= TEA5767_JAPAN_BAND;
  168. buffer[4] = 0;
  169. if (priv->ctrl.deemph_75)
  170. buffer[4] |= TEA5767_DEEMPH_75;
  171. if (priv->ctrl.pllref)
  172. buffer[4] |= TEA5767_PLLREF_ENABLE;
  173. /* Rounds freq to next decimal value - for 62.5 KHz step */
  174. /* frq = 20*(frq/16)+radio_frq[frq%16]; */
  175. switch (priv->ctrl.xtal_freq) {
  176. case TEA5767_HIGH_LO_13MHz:
  177. tuner_dbg("radio HIGH LO inject xtal @ 13 MHz\n");
  178. buffer[2] |= TEA5767_HIGH_LO_INJECT;
  179. div = (frq * (4000 / 16) + 700000 + 225000 + 25000) / 50000;
  180. break;
  181. case TEA5767_LOW_LO_13MHz:
  182. tuner_dbg("radio LOW LO inject xtal @ 13 MHz\n");
  183. div = (frq * (4000 / 16) - 700000 - 225000 + 25000) / 50000;
  184. break;
  185. case TEA5767_LOW_LO_32768:
  186. tuner_dbg("radio LOW LO inject xtal @ 32,768 MHz\n");
  187. buffer[3] |= TEA5767_XTAL_32768;
  188. /* const 700=4000*175 Khz - to adjust freq to right value */
  189. div = ((frq * (4000 / 16) - 700000 - 225000) + 16384) >> 15;
  190. break;
  191. case TEA5767_HIGH_LO_32768:
  192. default:
  193. tuner_dbg("radio HIGH LO inject xtal @ 32,768 MHz\n");
  194. buffer[2] |= TEA5767_HIGH_LO_INJECT;
  195. buffer[3] |= TEA5767_XTAL_32768;
  196. div = ((frq * (4000 / 16) + 700000 + 225000) + 16384) >> 15;
  197. break;
  198. }
  199. buffer[0] = (div >> 8) & 0x3f;
  200. buffer[1] = div & 0xff;
  201. if (5 != (rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 5)))
  202. tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
  203. if (debug) {
  204. if (5 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buffer, 5)))
  205. tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
  206. else
  207. tea5767_status_dump(priv, buffer);
  208. }
  209. priv->frequency = frq * 125 / 2;
  210. return 0;
  211. }
  212. static int tea5767_read_status(struct dvb_frontend *fe, char *buffer)
  213. {
  214. struct tea5767_priv *priv = fe->tuner_priv;
  215. int rc;
  216. memset(buffer, 0, 5);
  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. return -EREMOTEIO;
  220. }
  221. return 0;
  222. }
  223. static inline int tea5767_signal(struct dvb_frontend *fe, const char *buffer)
  224. {
  225. struct tea5767_priv *priv = fe->tuner_priv;
  226. int signal = ((buffer[3] & TEA5767_ADC_LEVEL_MASK) << 8);
  227. tuner_dbg("Signal strength: %d\n", signal);
  228. return signal;
  229. }
  230. static inline int tea5767_stereo(struct dvb_frontend *fe, const char *buffer)
  231. {
  232. struct tea5767_priv *priv = fe->tuner_priv;
  233. int stereo = buffer[2] & TEA5767_STEREO_MASK;
  234. tuner_dbg("Radio ST GET = %02x\n", stereo);
  235. return (stereo ? V4L2_TUNER_SUB_STEREO : 0);
  236. }
  237. static int tea5767_get_status(struct dvb_frontend *fe, u32 *status)
  238. {
  239. unsigned char buffer[5];
  240. *status = 0;
  241. if (0 == tea5767_read_status(fe, buffer)) {
  242. if (tea5767_signal(fe, buffer))
  243. *status = TUNER_STATUS_LOCKED;
  244. if (tea5767_stereo(fe, buffer))
  245. *status |= TUNER_STATUS_STEREO;
  246. }
  247. return 0;
  248. }
  249. static int tea5767_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
  250. {
  251. unsigned char buffer[5];
  252. *strength = 0;
  253. if (0 == tea5767_read_status(fe, buffer))
  254. *strength = tea5767_signal(fe, buffer);
  255. return 0;
  256. }
  257. static int tea5767_standby(struct dvb_frontend *fe)
  258. {
  259. unsigned char buffer[5];
  260. struct tea5767_priv *priv = fe->tuner_priv;
  261. unsigned div, rc;
  262. div = (87500 * 4 + 700 + 225 + 25) / 50; /* Set frequency to 87.5 MHz */
  263. buffer[0] = (div >> 8) & 0x3f;
  264. buffer[1] = div & 0xff;
  265. buffer[2] = TEA5767_PORT1_HIGH;
  266. buffer[3] = TEA5767_PORT2_HIGH | TEA5767_HIGH_CUT_CTRL |
  267. TEA5767_ST_NOISE_CTL | TEA5767_JAPAN_BAND | TEA5767_STDBY;
  268. buffer[4] = 0;
  269. if (5 != (rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 5)))
  270. tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
  271. return 0;
  272. }
  273. int tea5767_autodetection(struct i2c_adapter* i2c_adap, u8 i2c_addr)
  274. {
  275. struct tuner_i2c_props i2c = { .adap = i2c_adap, .addr = i2c_addr };
  276. unsigned char buffer[7] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  277. int rc;
  278. if ((rc = tuner_i2c_xfer_recv(&i2c, buffer, 7))< 5) {
  279. printk(KERN_WARNING "It is not a TEA5767. Received %i bytes.\n", rc);
  280. return -EINVAL;
  281. }
  282. /* If all bytes are the same then it's a TV tuner and not a tea5767 */
  283. if (buffer[0] == buffer[1] && buffer[0] == buffer[2] &&
  284. buffer[0] == buffer[3] && buffer[0] == buffer[4]) {
  285. printk(KERN_WARNING "All bytes are equal. It is not a TEA5767\n");
  286. return -EINVAL;
  287. }
  288. /* Status bytes:
  289. * Byte 4: bit 3:1 : CI (Chip Identification) == 0
  290. * bit 0 : internally set to 0
  291. * Byte 5: bit 7:0 : == 0
  292. */
  293. if (((buffer[3] & 0x0f) != 0x00) || (buffer[4] != 0x00)) {
  294. printk(KERN_WARNING "Chip ID is not zero. It is not a TEA5767\n");
  295. return -EINVAL;
  296. }
  297. return 0;
  298. }
  299. static int tea5767_release(struct dvb_frontend *fe)
  300. {
  301. kfree(fe->tuner_priv);
  302. fe->tuner_priv = NULL;
  303. return 0;
  304. }
  305. static int tea5767_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  306. {
  307. struct tea5767_priv *priv = fe->tuner_priv;
  308. *frequency = priv->frequency;
  309. return 0;
  310. }
  311. static int tea5767_set_config (struct dvb_frontend *fe, void *priv_cfg)
  312. {
  313. struct tea5767_priv *priv = fe->tuner_priv;
  314. memcpy(&priv->ctrl, priv_cfg, sizeof(priv->ctrl));
  315. return 0;
  316. }
  317. static struct dvb_tuner_ops tea5767_tuner_ops = {
  318. .info = {
  319. .name = "tea5767", // Philips TEA5767HN FM Radio
  320. },
  321. .set_analog_params = set_radio_freq,
  322. .set_config = tea5767_set_config,
  323. .sleep = tea5767_standby,
  324. .release = tea5767_release,
  325. .get_frequency = tea5767_get_frequency,
  326. .get_status = tea5767_get_status,
  327. .get_rf_strength = tea5767_get_rf_strength,
  328. };
  329. struct dvb_frontend *tea5767_attach(struct dvb_frontend *fe,
  330. struct i2c_adapter* i2c_adap,
  331. u8 i2c_addr)
  332. {
  333. struct tea5767_priv *priv = NULL;
  334. priv = kzalloc(sizeof(struct tea5767_priv), GFP_KERNEL);
  335. if (priv == NULL)
  336. return NULL;
  337. fe->tuner_priv = priv;
  338. priv->i2c_props.addr = i2c_addr;
  339. priv->i2c_props.adap = i2c_adap;
  340. priv->i2c_props.name = "tea5767";
  341. priv->ctrl.xtal_freq = TEA5767_HIGH_LO_32768;
  342. priv->ctrl.port1 = 1;
  343. priv->ctrl.port2 = 1;
  344. priv->ctrl.high_cut = 1;
  345. priv->ctrl.st_noise = 1;
  346. priv->ctrl.japan_band = 1;
  347. memcpy(&fe->ops.tuner_ops, &tea5767_tuner_ops,
  348. sizeof(struct dvb_tuner_ops));
  349. tuner_info("type set to %s\n", "Philips TEA5767HN FM Radio");
  350. return fe;
  351. }
  352. EXPORT_SYMBOL_GPL(tea5767_attach);
  353. EXPORT_SYMBOL_GPL(tea5767_autodetection);
  354. MODULE_DESCRIPTION("Philips TEA5767 FM tuner driver");
  355. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
  356. MODULE_LICENSE("GPL");