tda8290.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. *
  3. * i2c tv tuner chip device driver
  4. * controls the philips tda8290+75 tuner chip combo.
  5. */
  6. #include <linux/i2c.h>
  7. #include <linux/videodev.h>
  8. #include <linux/delay.h>
  9. #include <media/tuner.h>
  10. #define I2C_ADDR_TDA8290 0x4b
  11. #define I2C_ADDR_TDA8275 0x61
  12. /* ---------------------------------------------------------------------- */
  13. struct freq_entry {
  14. u16 freq;
  15. u8 value;
  16. };
  17. static struct freq_entry band_table[] = {
  18. { 0x2DF4, 0x1C },
  19. { 0x2574, 0x14 },
  20. { 0x22B4, 0x0C },
  21. { 0x20D4, 0x0B },
  22. { 0x1E74, 0x3B },
  23. { 0x1C34, 0x33 },
  24. { 0x16F4, 0x5B },
  25. { 0x1454, 0x53 },
  26. { 0x12D4, 0x52 },
  27. { 0x1034, 0x4A },
  28. { 0x0EE4, 0x7A },
  29. { 0x0D34, 0x72 },
  30. { 0x0B54, 0x9A },
  31. { 0x0914, 0x91 },
  32. { 0x07F4, 0x89 },
  33. { 0x0774, 0xB9 },
  34. { 0x067B, 0xB1 },
  35. { 0x0634, 0xD9 },
  36. { 0x05A4, 0xD8 }, // FM radio
  37. { 0x0494, 0xD0 },
  38. { 0x03BC, 0xC8 },
  39. { 0x0394, 0xF8 }, // 57250000 Hz
  40. { 0x0000, 0xF0 }, // 0
  41. };
  42. static struct freq_entry div_table[] = {
  43. { 0x1C34, 3 },
  44. { 0x0D34, 2 },
  45. { 0x067B, 1 },
  46. { 0x0000, 0 },
  47. };
  48. static struct freq_entry agc_table[] = {
  49. { 0x22B4, 0x8F },
  50. { 0x0B54, 0x9F },
  51. { 0x09A4, 0x8F },
  52. { 0x0554, 0x9F },
  53. { 0x0000, 0xBF },
  54. };
  55. static __u8 get_freq_entry( struct freq_entry* table, __u16 freq)
  56. {
  57. while(table->freq && table->freq > freq)
  58. table++;
  59. return table->value;
  60. }
  61. /* ---------------------------------------------------------------------- */
  62. static unsigned char i2c_enable_bridge[2] = { 0x21, 0xC0 };
  63. static unsigned char i2c_disable_bridge[2] = { 0x21, 0x80 };
  64. static unsigned char i2c_init_tda8275[14] = { 0x00, 0x00, 0x00, 0x00,
  65. 0xfC, 0x04, 0xA3, 0x3F,
  66. 0x2A, 0x04, 0xFF, 0x00,
  67. 0x00, 0x40 };
  68. static unsigned char i2c_set_VS[2] = { 0x30, 0x6F };
  69. static unsigned char i2c_set_GP01_CF[2] = { 0x20, 0x0B };
  70. static unsigned char i2c_tda8290_reset[2] = { 0x00, 0x00 };
  71. static unsigned char i2c_tda8290_standby[2] = { 0x00, 0x02 };
  72. static unsigned char i2c_gainset_off[2] = { 0x28, 0x14 };
  73. static unsigned char i2c_gainset_on[2] = { 0x28, 0x54 };
  74. static unsigned char i2c_agc3_00[2] = { 0x80, 0x00 };
  75. static unsigned char i2c_agc2_BF[2] = { 0x60, 0xBF };
  76. static unsigned char i2c_cb1_D0[2] = { 0x30, 0xD0 };
  77. static unsigned char i2c_cb1_D2[2] = { 0x30, 0xD2 };
  78. static unsigned char i2c_cb1_56[2] = { 0x30, 0x56 };
  79. static unsigned char i2c_cb1_52[2] = { 0x30, 0x52 };
  80. static unsigned char i2c_cb1_50[2] = { 0x30, 0x50 };
  81. static unsigned char i2c_agc2_7F[2] = { 0x60, 0x7F };
  82. static unsigned char i2c_agc3_08[2] = { 0x80, 0x08 };
  83. static struct i2c_msg i2c_msg_init[] = {
  84. { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_init_tda8275), i2c_init_tda8275 },
  85. { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_disable_bridge), i2c_disable_bridge },
  86. { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_set_VS), i2c_set_VS },
  87. { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_set_GP01_CF), i2c_set_GP01_CF },
  88. };
  89. static struct i2c_msg i2c_msg_prolog[] = {
  90. // { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_easy_mode), i2c_easy_mode },
  91. { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_gainset_off), i2c_gainset_off },
  92. { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_tda8290_reset), i2c_tda8290_reset },
  93. { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_enable_bridge), i2c_enable_bridge },
  94. };
  95. static struct i2c_msg i2c_msg_config[] = {
  96. // { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_set_freq), i2c_set_freq },
  97. { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_agc3_00), i2c_agc3_00 },
  98. { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_agc2_BF), i2c_agc2_BF },
  99. { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_cb1_D2), i2c_cb1_D2 },
  100. { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_cb1_56), i2c_cb1_56 },
  101. { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_cb1_52), i2c_cb1_52 },
  102. };
  103. static struct i2c_msg i2c_msg_epilog[] = {
  104. { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_cb1_50), i2c_cb1_50 },
  105. { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_agc2_7F), i2c_agc2_7F },
  106. { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_agc3_08), i2c_agc3_08 },
  107. { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_disable_bridge), i2c_disable_bridge },
  108. { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_gainset_on), i2c_gainset_on },
  109. };
  110. static struct i2c_msg i2c_msg_standby[] = {
  111. { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_enable_bridge), i2c_enable_bridge },
  112. { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_cb1_D0), i2c_cb1_D0 },
  113. { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_disable_bridge), i2c_disable_bridge },
  114. { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_tda8290_standby), i2c_tda8290_standby },
  115. };
  116. static int tda8290_tune(struct i2c_client *c)
  117. {
  118. struct tuner *t = i2c_get_clientdata(c);
  119. struct i2c_msg easy_mode =
  120. { I2C_ADDR_TDA8290, 0, 2, t->i2c_easy_mode };
  121. struct i2c_msg set_freq =
  122. { I2C_ADDR_TDA8275, 0, 8, t->i2c_set_freq };
  123. i2c_transfer(c->adapter, &easy_mode, 1);
  124. i2c_transfer(c->adapter, i2c_msg_prolog, ARRAY_SIZE(i2c_msg_prolog));
  125. i2c_transfer(c->adapter, &set_freq, 1);
  126. i2c_transfer(c->adapter, i2c_msg_config, ARRAY_SIZE(i2c_msg_config));
  127. msleep(550);
  128. i2c_transfer(c->adapter, i2c_msg_epilog, ARRAY_SIZE(i2c_msg_epilog));
  129. return 0;
  130. }
  131. static void set_frequency(struct tuner *t, u16 ifc, unsigned int freq)
  132. {
  133. u32 N;
  134. if (t->mode == V4L2_TUNER_RADIO)
  135. freq = freq / 1000;
  136. N = (((freq<<3)+ifc)&0x3fffc);
  137. N = N >> get_freq_entry(div_table, freq);
  138. t->i2c_set_freq[0] = 0;
  139. t->i2c_set_freq[1] = (unsigned char)(N>>8);
  140. t->i2c_set_freq[2] = (unsigned char) N;
  141. t->i2c_set_freq[3] = 0x40;
  142. t->i2c_set_freq[4] = 0x52;
  143. t->i2c_set_freq[5] = get_freq_entry(band_table, freq);
  144. t->i2c_set_freq[6] = get_freq_entry(agc_table, freq);
  145. t->i2c_set_freq[7] = 0x8f;
  146. }
  147. #define V4L2_STD_MN (V4L2_STD_PAL_M|V4L2_STD_PAL_N|V4L2_STD_PAL_Nc|V4L2_STD_NTSC)
  148. #define V4L2_STD_B (V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_SECAM_B)
  149. #define V4L2_STD_GH (V4L2_STD_PAL_G|V4L2_STD_PAL_H|V4L2_STD_SECAM_G|V4L2_STD_SECAM_H)
  150. #define V4L2_STD_DK (V4L2_STD_PAL_DK|V4L2_STD_SECAM_DK)
  151. static void set_audio(struct tuner *t)
  152. {
  153. t->i2c_easy_mode[0] = 0x01;
  154. if (t->std & V4L2_STD_MN)
  155. t->i2c_easy_mode[1] = 0x01;
  156. else if (t->std & V4L2_STD_B)
  157. t->i2c_easy_mode[1] = 0x02;
  158. else if (t->std & V4L2_STD_GH)
  159. t->i2c_easy_mode[1] = 0x04;
  160. else if (t->std & V4L2_STD_PAL_I)
  161. t->i2c_easy_mode[1] = 0x08;
  162. else if (t->std & V4L2_STD_DK)
  163. t->i2c_easy_mode[1] = 0x10;
  164. else if (t->std & V4L2_STD_SECAM_L)
  165. t->i2c_easy_mode[1] = 0x20;
  166. }
  167. static void set_tv_freq(struct i2c_client *c, unsigned int freq)
  168. {
  169. struct tuner *t = i2c_get_clientdata(c);
  170. set_audio(t);
  171. set_frequency(t, 864, freq);
  172. tda8290_tune(c);
  173. }
  174. static void set_radio_freq(struct i2c_client *c, unsigned int freq)
  175. {
  176. struct tuner *t = i2c_get_clientdata(c);
  177. set_frequency(t, 704, freq);
  178. tda8290_tune(c);
  179. }
  180. static int has_signal(struct i2c_client *c)
  181. {
  182. unsigned char i2c_get_afc[1] = { 0x1B };
  183. unsigned char afc = 0;
  184. i2c_master_send(c, i2c_get_afc, ARRAY_SIZE(i2c_get_afc));
  185. i2c_master_recv(c, &afc, 1);
  186. return (afc & 0x80)? 65535:0;
  187. }
  188. static void standby(struct i2c_client *c)
  189. {
  190. i2c_transfer(c->adapter, i2c_msg_standby, ARRAY_SIZE(i2c_msg_standby));
  191. }
  192. int tda8290_init(struct i2c_client *c)
  193. {
  194. struct tuner *t = i2c_get_clientdata(c);
  195. strlcpy(c->name, "tda8290+75", sizeof(c->name));
  196. tuner_info("tuner: type set to %s\n", c->name);
  197. t->tv_freq = set_tv_freq;
  198. t->radio_freq = set_radio_freq;
  199. t->has_signal = has_signal;
  200. t->standby = standby;
  201. i2c_master_send(c, i2c_enable_bridge, ARRAY_SIZE(i2c_enable_bridge));
  202. i2c_transfer(c->adapter, i2c_msg_init, ARRAY_SIZE(i2c_msg_init));
  203. return 0;
  204. }
  205. /*
  206. * Overrides for Emacs so that we follow Linus's tabbing style.
  207. * ---------------------------------------------------------------------------
  208. * Local variables:
  209. * c-basic-offset: 8
  210. * End:
  211. */