tea5761.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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-driver.h"
  14. #define PREFIX "TEA5761 "
  15. /* from tuner-core.c */
  16. extern int tuner_debug;
  17. struct tea5761_priv {
  18. struct tuner_i2c_props i2c_props;
  19. };
  20. /*****************************************************************************/
  21. /***************************
  22. * TEA5761HN I2C registers *
  23. ***************************/
  24. /* INTREG - Read: bytes 0 and 1 / Write: byte 0 */
  25. /* first byte for reading */
  26. #define TEA5761_INTREG_IFFLAG 0x10
  27. #define TEA5761_INTREG_LEVFLAG 0x8
  28. #define TEA5761_INTREG_FRRFLAG 0x2
  29. #define TEA5761_INTREG_BLFLAG 0x1
  30. /* second byte for reading / byte for writing */
  31. #define TEA5761_INTREG_IFMSK 0x10
  32. #define TEA5761_INTREG_LEVMSK 0x8
  33. #define TEA5761_INTREG_FRMSK 0x2
  34. #define TEA5761_INTREG_BLMSK 0x1
  35. /* FRQSET - Read: bytes 2 and 3 / Write: byte 1 and 2 */
  36. /* First byte */
  37. #define TEA5761_FRQSET_SEARCH_UP 0x80 /* 1=Station search from botton to up */
  38. #define TEA5761_FRQSET_SEARCH_MODE 0x40 /* 1=Search mode */
  39. /* Bits 0-5 for divider MSB */
  40. /* Second byte */
  41. /* Bits 0-7 for divider LSB */
  42. /* TNCTRL - Read: bytes 4 and 5 / Write: Bytes 3 and 4 */
  43. /* first byte */
  44. #define TEA5761_TNCTRL_PUPD_0 0x40 /* Power UP/Power Down MSB */
  45. #define TEA5761_TNCTRL_BLIM 0X20 /* 1= Japan Frequencies, 0= European frequencies */
  46. #define TEA5761_TNCTRL_SWPM 0x10 /* 1= software port is FRRFLAG */
  47. #define TEA5761_TNCTRL_IFCTC 0x08 /* 1= IF count time 15.02 ms, 0= IF count time 2.02 ms */
  48. #define TEA5761_TNCTRL_AFM 0x04
  49. #define TEA5761_TNCTRL_SMUTE 0x02 /* 1= Soft mute */
  50. #define TEA5761_TNCTRL_SNC 0x01
  51. /* second byte */
  52. #define TEA5761_TNCTRL_MU 0x80 /* 1=Hard mute */
  53. #define TEA5761_TNCTRL_SSL_1 0x40
  54. #define TEA5761_TNCTRL_SSL_0 0x20
  55. #define TEA5761_TNCTRL_HLSI 0x10
  56. #define TEA5761_TNCTRL_MST 0x08 /* 1 = mono */
  57. #define TEA5761_TNCTRL_SWP 0x04
  58. #define TEA5761_TNCTRL_DTC 0x02 /* 1 = deemphasis 50 us, 0 = deemphasis 75 us */
  59. #define TEA5761_TNCTRL_AHLSI 0x01
  60. /* FRQCHECK - Read: bytes 6 and 7 */
  61. /* First byte */
  62. /* Bits 0-5 for divider MSB */
  63. /* Second byte */
  64. /* Bits 0-7 for divider LSB */
  65. /* TUNCHECK - Read: bytes 8 and 9 */
  66. /* First byte */
  67. #define TEA5761_TUNCHECK_IF_MASK 0x7e /* IF count */
  68. #define TEA5761_TUNCHECK_TUNTO 0x01
  69. /* Second byte */
  70. #define TEA5761_TUNCHECK_LEV_MASK 0xf0 /* Level Count */
  71. #define TEA5761_TUNCHECK_LD 0x08
  72. #define TEA5761_TUNCHECK_STEREO 0x04
  73. /* TESTREG - Read: bytes 10 and 11 / Write: bytes 5 and 6 */
  74. /* All zero = no test mode */
  75. /* MANID - Read: bytes 12 and 13 */
  76. /* First byte - should be 0x10 */
  77. #define TEA5767_MANID_VERSION_MASK 0xf0 /* Version = 1 */
  78. #define TEA5767_MANID_ID_MSB_MASK 0x0f /* Manufacurer ID - should be 0 */
  79. /* Second byte - Should be 0x2b */
  80. #define TEA5767_MANID_ID_LSB_MASK 0xfe /* Manufacturer ID - should be 0x15 */
  81. #define TEA5767_MANID_IDAV 0x01 /* 1 = Chip has ID, 0 = Chip has no ID */
  82. /* Chip ID - Read: bytes 14 and 15 */
  83. /* First byte - should be 0x57 */
  84. /* Second byte - should be 0x61 */
  85. /*****************************************************************************/
  86. static void set_tv_freq(struct tuner *t, unsigned int freq)
  87. {
  88. tuner_warn("This tuner doesn't support TV freq.\n");
  89. }
  90. #define FREQ_OFFSET 0 /* for TEA5767, it is 700 to give the right freq */
  91. static void tea5761_status_dump(unsigned char *buffer)
  92. {
  93. unsigned int div, frq;
  94. div = ((buffer[2] & 0x3f) << 8) | buffer[3];
  95. frq = 1000 * (div * 32768 / 1000 + FREQ_OFFSET + 225) / 4; /* Freq in KHz */
  96. printk(PREFIX "Frequency %d.%03d KHz (divider = 0x%04x)\n",
  97. frq / 1000, frq % 1000, div);
  98. }
  99. /* Freq should be specifyed at 62.5 Hz */
  100. static void set_radio_freq(struct tuner *t, unsigned int frq)
  101. {
  102. struct tea5761_priv *priv = t->priv;
  103. unsigned char buffer[7] = {0, 0, 0, 0, 0, 0, 0 };
  104. unsigned div;
  105. int rc;
  106. tuner_dbg (PREFIX "radio freq counter %d\n", frq);
  107. if (t->mode == T_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 (t->audmode == V4L2_TUNER_MODE_MONO) {
  114. tuner_dbg("TEA5761 set to mono\n");
  115. buffer[5] |= TEA5761_TNCTRL_MST;
  116. ;
  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 (tuner_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. }
  128. static int tea5761_signal(struct tuner *t)
  129. {
  130. unsigned char buffer[16];
  131. int rc;
  132. struct tea5761_priv *priv = t->priv;
  133. memset(buffer, 0, sizeof(buffer));
  134. if (16 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buffer, 16)))
  135. tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
  136. return ((buffer[9] & TEA5761_TUNCHECK_LEV_MASK) << (13 - 4));
  137. }
  138. static int tea5761_stereo(struct tuner *t)
  139. {
  140. unsigned char buffer[16];
  141. int rc;
  142. struct tea5761_priv *priv = t->priv;
  143. memset(buffer, 0, sizeof(buffer));
  144. if (16 != (rc = tuner_i2c_xfer_recv(&priv->i2c_props, buffer, 16)))
  145. tuner_warn("i2c i/o error: rc == %d (should be 5)\n", rc);
  146. rc = buffer[9] & TEA5761_TUNCHECK_STEREO;
  147. tuner_dbg("TEA5761 radio ST GET = %02x\n", rc);
  148. return (rc ? V4L2_TUNER_SUB_STEREO : 0);
  149. }
  150. int tea5761_autodetection(struct tuner *t)
  151. {
  152. unsigned char buffer[16];
  153. int rc;
  154. struct tuner_i2c_props i2c = { .adap = t->i2c.adapter, .addr = t->i2c.addr };
  155. if (16 != (rc = tuner_i2c_xfer_recv(&i2c, buffer, 16))) {
  156. tuner_warn("it is not a TEA5761. Received %i chars.\n", rc);
  157. return EINVAL;
  158. }
  159. if (!((buffer[13] != 0x2b) || (buffer[14] != 0x57) || (buffer[15] != 0x061))) {
  160. tuner_warn("Manufacturer ID= 0x%02x, Chip ID = %02x%02x. It is not a TEA5761\n",buffer[13],buffer[14],buffer[15]);
  161. return EINVAL;
  162. }
  163. tuner_warn("TEA5761 detected.\n");
  164. return 0;
  165. }
  166. static void tea5761_release(struct tuner *t)
  167. {
  168. kfree(t->priv);
  169. t->priv = NULL;
  170. }
  171. static struct tuner_operations tea5761_tuner_ops = {
  172. .set_tv_freq = set_tv_freq,
  173. .set_radio_freq = set_radio_freq,
  174. .has_signal = tea5761_signal,
  175. .is_stereo = tea5761_stereo,
  176. .release = tea5761_release,
  177. };
  178. int tea5761_tuner_init(struct tuner *t)
  179. {
  180. struct tea5761_priv *priv = NULL;
  181. if (tea5761_autodetection(t) == EINVAL)
  182. return EINVAL;
  183. priv = kzalloc(sizeof(struct tea5761_priv), GFP_KERNEL);
  184. if (priv == NULL)
  185. return -ENOMEM;
  186. t->priv = priv;
  187. priv->i2c_props.addr = t->i2c.addr;
  188. priv->i2c_props.adap = t->i2c.adapter;
  189. tuner_info("type set to %d (%s)\n", t->type, "Philips TEA5761HN FM Radio");
  190. strlcpy(t->i2c.name, "tea5761", sizeof(t->i2c.name));
  191. memcpy(&t->ops, &tea5761_tuner_ops, sizeof(struct tuner_operations));
  192. return (0);
  193. }