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