tea5761.c 7.8 KB

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