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