tea5761.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0
  2. // For Philips TEA5761 FM Chip
  3. // I2C address is always 0x20 (0x10 at 7-bit mode).
  4. //
  5. // Copyright (c) 2005-2007 Mauro Carvalho Chehab <mchehab@kernel.org>
  6. #include <linux/i2c.h>
  7. #include <linux/slab.h>
  8. #include <linux/delay.h>
  9. #include <linux/videodev2.h>
  10. #include <media/tuner.h>
  11. #include "tuner-i2c.h"
  12. #include "tea5761.h"
  13. static int debug;
  14. module_param(debug, int, 0644);
  15. MODULE_PARM_DESC(debug, "enable verbose debug messages");
  16. struct tea5761_priv {
  17. struct tuner_i2c_props i2c_props;
  18. u32 frequency;
  19. bool standby;
  20. };
  21. /*****************************************************************************/
  22. /***************************
  23. * TEA5761HN I2C registers *
  24. ***************************/
  25. /* INTREG - Read: bytes 0 and 1 / Write: byte 0 */
  26. /* first byte for reading */
  27. #define TEA5761_INTREG_IFFLAG 0x10
  28. #define TEA5761_INTREG_LEVFLAG 0x8
  29. #define TEA5761_INTREG_FRRFLAG 0x2
  30. #define TEA5761_INTREG_BLFLAG 0x1
  31. /* second byte for reading / byte for writing */
  32. #define TEA5761_INTREG_IFMSK 0x10
  33. #define TEA5761_INTREG_LEVMSK 0x8
  34. #define TEA5761_INTREG_FRMSK 0x2
  35. #define TEA5761_INTREG_BLMSK 0x1
  36. /* FRQSET - Read: bytes 2 and 3 / Write: byte 1 and 2 */
  37. /* First byte */
  38. #define TEA5761_FRQSET_SEARCH_UP 0x80 /* 1=Station search from botton to up */
  39. #define TEA5761_FRQSET_SEARCH_MODE 0x40 /* 1=Search mode */
  40. /* Bits 0-5 for divider MSB */
  41. /* Second byte */
  42. /* Bits 0-7 for divider LSB */
  43. /* TNCTRL - Read: bytes 4 and 5 / Write: Bytes 3 and 4 */
  44. /* first byte */
  45. #define TEA5761_TNCTRL_PUPD_0 0x40 /* Power UP/Power Down MSB */
  46. #define TEA5761_TNCTRL_BLIM 0X20 /* 1= Japan Frequencies, 0= European frequencies */
  47. #define TEA5761_TNCTRL_SWPM 0x10 /* 1= software port is FRRFLAG */
  48. #define TEA5761_TNCTRL_IFCTC 0x08 /* 1= IF count time 15.02 ms, 0= IF count time 2.02 ms */
  49. #define TEA5761_TNCTRL_AFM 0x04
  50. #define TEA5761_TNCTRL_SMUTE 0x02 /* 1= Soft mute */
  51. #define TEA5761_TNCTRL_SNC 0x01
  52. /* second byte */
  53. #define TEA5761_TNCTRL_MU 0x80 /* 1=Hard mute */
  54. #define TEA5761_TNCTRL_SSL_1 0x40
  55. #define TEA5761_TNCTRL_SSL_0 0x20
  56. #define TEA5761_TNCTRL_HLSI 0x10
  57. #define TEA5761_TNCTRL_MST 0x08 /* 1 = mono */
  58. #define TEA5761_TNCTRL_SWP 0x04
  59. #define TEA5761_TNCTRL_DTC 0x02 /* 1 = deemphasis 50 us, 0 = deemphasis 75 us */
  60. #define TEA5761_TNCTRL_AHLSI 0x01
  61. /* FRQCHECK - Read: bytes 6 and 7 */
  62. /* First byte */
  63. /* Bits 0-5 for divider MSB */
  64. /* Second byte */
  65. /* Bits 0-7 for divider LSB */
  66. /* TUNCHECK - Read: bytes 8 and 9 */
  67. /* First byte */
  68. #define TEA5761_TUNCHECK_IF_MASK 0x7e /* IF count */
  69. #define TEA5761_TUNCHECK_TUNTO 0x01
  70. /* Second byte */
  71. #define TEA5761_TUNCHECK_LEV_MASK 0xf0 /* Level Count */
  72. #define TEA5761_TUNCHECK_LD 0x08
  73. #define TEA5761_TUNCHECK_STEREO 0x04
  74. /* TESTREG - Read: bytes 10 and 11 / Write: bytes 5 and 6 */
  75. /* All zero = no test mode */
  76. /* MANID - Read: bytes 12 and 13 */
  77. /* First byte - should be 0x10 */
  78. #define TEA5767_MANID_VERSION_MASK 0xf0 /* Version = 1 */
  79. #define TEA5767_MANID_ID_MSB_MASK 0x0f /* Manufacurer ID - should be 0 */
  80. /* Second byte - Should be 0x2b */
  81. #define TEA5767_MANID_ID_LSB_MASK 0xfe /* Manufacturer ID - should be 0x15 */
  82. #define TEA5767_MANID_IDAV 0x01 /* 1 = Chip has ID, 0 = Chip has no ID */
  83. /* Chip ID - Read: bytes 14 and 15 */
  84. /* First byte - should be 0x57 */
  85. /* Second byte - should be 0x61 */
  86. /*****************************************************************************/
  87. #define FREQ_OFFSET 0 /* for TEA5767, it is 700 to give the right freq */
  88. static void tea5761_status_dump(unsigned char *buffer)
  89. {
  90. unsigned int div, frq;
  91. div = ((buffer[2] & 0x3f) << 8) | buffer[3];
  92. frq = 1000 * (div * 32768 / 1000 + FREQ_OFFSET + 225) / 4; /* Freq in KHz */
  93. printk(KERN_INFO "tea5761: Frequency %d.%03d KHz (divider = 0x%04x)\n",
  94. frq / 1000, frq % 1000, div);
  95. }
  96. /* Freq should be specifyed at 62.5 Hz */
  97. static int __set_radio_freq(struct dvb_frontend *fe,
  98. unsigned int freq,
  99. bool mono)
  100. {
  101. struct tea5761_priv *priv = fe->tuner_priv;
  102. unsigned int frq = freq;
  103. unsigned char buffer[7] = {0, 0, 0, 0, 0, 0, 0 };
  104. unsigned div;
  105. int rc;
  106. tuner_dbg("radio freq counter %d\n", frq);
  107. if (priv->standby) {
  108. tuner_dbg("TEA5761 set to standby mode\n");
  109. buffer[5] |= TEA5761_TNCTRL_MU;
  110. } else {
  111. buffer[4] |= TEA5761_TNCTRL_PUPD_0;
  112. }
  113. if (mono) {
  114. tuner_dbg("TEA5761 set to mono\n");
  115. buffer[5] |= TEA5761_TNCTRL_MST;
  116. } else {
  117. tuner_dbg("TEA5761 set to stereo\n");
  118. }
  119. div = (1000 * (frq * 4 / 16 + 700 + 225) ) >> 15;
  120. buffer[1] = (div >> 8) & 0x3f;
  121. buffer[2] = div & 0xff;
  122. if (debug)
  123. tea5761_status_dump(buffer);
  124. if (7 != (rc = tuner_i2c_xfer_send(&priv->i2c_props, buffer, 7)))
  125. tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
  126. priv->frequency = frq * 125 / 2;
  127. return 0;
  128. }
  129. static int set_radio_freq(struct dvb_frontend *fe,
  130. struct analog_parameters *params)
  131. {
  132. struct tea5761_priv *priv = fe->analog_demod_priv;
  133. priv->standby = false;
  134. return __set_radio_freq(fe, params->frequency,
  135. params->audmode == V4L2_TUNER_MODE_MONO);
  136. }
  137. static int set_radio_sleep(struct dvb_frontend *fe)
  138. {
  139. struct tea5761_priv *priv = fe->analog_demod_priv;
  140. priv->standby = true;
  141. return __set_radio_freq(fe, priv->frequency, false);
  142. }
  143. static int tea5761_read_status(struct dvb_frontend *fe, char *buffer)
  144. {
  145. struct tea5761_priv *priv = fe->tuner_priv;
  146. int rc;
  147. memset(buffer, 0, 16);
  148. if (16 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buffer, 16))) {
  149. tuner_warn("i2c i/o error: rc == %d (should be 16)\n", rc);
  150. return -EREMOTEIO;
  151. }
  152. return 0;
  153. }
  154. static inline int tea5761_signal(struct dvb_frontend *fe, const char *buffer)
  155. {
  156. struct tea5761_priv *priv = fe->tuner_priv;
  157. int signal = ((buffer[9] & TEA5761_TUNCHECK_LEV_MASK) << (13 - 4));
  158. tuner_dbg("Signal strength: %d\n", signal);
  159. return signal;
  160. }
  161. static inline int tea5761_stereo(struct dvb_frontend *fe, const char *buffer)
  162. {
  163. struct tea5761_priv *priv = fe->tuner_priv;
  164. int stereo = buffer[9] & TEA5761_TUNCHECK_STEREO;
  165. tuner_dbg("Radio ST GET = %02x\n", stereo);
  166. return (stereo ? V4L2_TUNER_SUB_STEREO : 0);
  167. }
  168. static int tea5761_get_status(struct dvb_frontend *fe, u32 *status)
  169. {
  170. unsigned char buffer[16];
  171. *status = 0;
  172. if (0 == tea5761_read_status(fe, buffer)) {
  173. if (tea5761_signal(fe, buffer))
  174. *status = TUNER_STATUS_LOCKED;
  175. if (tea5761_stereo(fe, buffer))
  176. *status |= TUNER_STATUS_STEREO;
  177. }
  178. return 0;
  179. }
  180. static int tea5761_get_rf_strength(struct dvb_frontend *fe, u16 *strength)
  181. {
  182. unsigned char buffer[16];
  183. *strength = 0;
  184. if (0 == tea5761_read_status(fe, buffer))
  185. *strength = tea5761_signal(fe, buffer);
  186. return 0;
  187. }
  188. int tea5761_autodetection(struct i2c_adapter* i2c_adap, u8 i2c_addr)
  189. {
  190. unsigned char buffer[16];
  191. int rc;
  192. struct tuner_i2c_props i2c = { .adap = i2c_adap, .addr = i2c_addr };
  193. if (16 != (rc = tuner_i2c_xfer_recv(&i2c, buffer, 16))) {
  194. printk(KERN_WARNING "it is not a TEA5761. Received %i chars.\n", rc);
  195. return -EINVAL;
  196. }
  197. if ((buffer[13] != 0x2b) || (buffer[14] != 0x57) || (buffer[15] != 0x061)) {
  198. printk(KERN_WARNING "Manufacturer ID= 0x%02x, Chip ID = %02x%02x. It is not a TEA5761\n",
  199. buffer[13], buffer[14], buffer[15]);
  200. return -EINVAL;
  201. }
  202. printk(KERN_WARNING "tea5761: TEA%02x%02x detected. Manufacturer ID= 0x%02x\n",
  203. buffer[14], buffer[15], buffer[13]);
  204. return 0;
  205. }
  206. static void tea5761_release(struct dvb_frontend *fe)
  207. {
  208. kfree(fe->tuner_priv);
  209. fe->tuner_priv = NULL;
  210. }
  211. static int tea5761_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  212. {
  213. struct tea5761_priv *priv = fe->tuner_priv;
  214. *frequency = priv->frequency;
  215. return 0;
  216. }
  217. static const struct dvb_tuner_ops tea5761_tuner_ops = {
  218. .info = {
  219. .name = "tea5761", // Philips TEA5761HN FM Radio
  220. },
  221. .set_analog_params = set_radio_freq,
  222. .sleep = set_radio_sleep,
  223. .release = tea5761_release,
  224. .get_frequency = tea5761_get_frequency,
  225. .get_status = tea5761_get_status,
  226. .get_rf_strength = tea5761_get_rf_strength,
  227. };
  228. struct dvb_frontend *tea5761_attach(struct dvb_frontend *fe,
  229. struct i2c_adapter* i2c_adap,
  230. u8 i2c_addr)
  231. {
  232. struct tea5761_priv *priv = NULL;
  233. if (tea5761_autodetection(i2c_adap, i2c_addr) != 0)
  234. return NULL;
  235. priv = kzalloc(sizeof(struct tea5761_priv), GFP_KERNEL);
  236. if (priv == NULL)
  237. return NULL;
  238. fe->tuner_priv = priv;
  239. priv->i2c_props.addr = i2c_addr;
  240. priv->i2c_props.adap = i2c_adap;
  241. priv->i2c_props.name = "tea5761";
  242. memcpy(&fe->ops.tuner_ops, &tea5761_tuner_ops,
  243. sizeof(struct dvb_tuner_ops));
  244. tuner_info("type set to %s\n", "Philips TEA5761HN FM Radio");
  245. return fe;
  246. }
  247. EXPORT_SYMBOL_GPL(tea5761_attach);
  248. EXPORT_SYMBOL_GPL(tea5761_autodetection);
  249. MODULE_DESCRIPTION("Philips TEA5761 FM tuner driver");
  250. MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@kernel.org>");
  251. MODULE_LICENSE("GPL v2");