tea5767.c 12 KB

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