Merge ../torvalds-2.6/
[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 static struct i2c_msg i2c_msg_init[] = {
94         { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_init_tda8275), i2c_init_tda8275 },
95         { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_disable_bridge), i2c_disable_bridge },
96         { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_set_VS), i2c_set_VS },
97         { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_set_GP01_CF), i2c_set_GP01_CF },
98 };
99
100 static struct i2c_msg i2c_msg_prolog[] = {
101 //      { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_easy_mode), i2c_easy_mode },
102         { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_gainset_off), i2c_gainset_off },
103         { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_tda8290_reset), i2c_tda8290_reset },
104         { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_enable_bridge), i2c_enable_bridge },
105 };
106
107 static struct i2c_msg i2c_msg_config[] = {
108 //      { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_set_freq), i2c_set_freq },
109         { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_agc3_00), i2c_agc3_00 },
110         { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_agc2_BF), i2c_agc2_BF },
111         { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_cb1_D2), i2c_cb1_D2 },
112         { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_cb1_56), i2c_cb1_56 },
113         { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_cb1_52), i2c_cb1_52 },
114 };
115
116 static struct i2c_msg i2c_msg_epilog[] = {
117         { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_cb1_50), i2c_cb1_50 },
118         { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_agc2_7F), i2c_agc2_7F },
119         { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_agc3_08), i2c_agc3_08 },
120         { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_disable_bridge), i2c_disable_bridge },
121         { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_gainset_on), i2c_gainset_on },
122 };
123
124 static struct i2c_msg i2c_msg_standby[] = {
125         { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_enable_bridge), i2c_enable_bridge },
126         { I2C_ADDR_TDA8275, 0, ARRAY_SIZE(i2c_cb1_D0), i2c_cb1_D0 },
127         { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_disable_bridge), i2c_disable_bridge },
128         { I2C_ADDR_TDA8290, 0, ARRAY_SIZE(i2c_tda8290_standby), i2c_tda8290_standby },
129 };
130
131 static int tda8290_tune(struct i2c_client *c)
132 {
133         struct tuner *t = i2c_get_clientdata(c);
134         struct i2c_msg easy_mode =
135                 { I2C_ADDR_TDA8290, 0, 2, t->i2c_easy_mode };
136         struct i2c_msg set_freq =
137                 { I2C_ADDR_TDA8275, 0, 8, t->i2c_set_freq  };
138
139         i2c_transfer(c->adapter, &easy_mode,      1);
140         i2c_transfer(c->adapter, i2c_msg_prolog, ARRAY_SIZE(i2c_msg_prolog));
141
142         i2c_transfer(c->adapter, &set_freq,       1);
143         i2c_transfer(c->adapter, i2c_msg_config, ARRAY_SIZE(i2c_msg_config));
144
145         msleep(550);
146         i2c_transfer(c->adapter, i2c_msg_epilog, ARRAY_SIZE(i2c_msg_epilog));
147         return 0;
148 }
149
150 static void set_frequency(struct tuner *t, u16 ifc, unsigned int freq)
151 {
152         u32 N;
153
154         if (t->mode == V4L2_TUNER_RADIO)
155                 freq = freq / 1000;
156
157         N = (((freq<<3)+ifc)&0x3fffc);
158
159         N = N >> get_freq_entry(div_table, freq);
160         t->i2c_set_freq[0] = 0;
161         t->i2c_set_freq[1] = (unsigned char)(N>>8);
162         t->i2c_set_freq[2] = (unsigned char) N;
163         t->i2c_set_freq[3] = 0x40;
164         t->i2c_set_freq[4] = 0x52;
165         t->i2c_set_freq[5] = get_freq_entry(band_table, freq);
166         t->i2c_set_freq[6] = get_freq_entry(agc_table,  freq);
167         t->i2c_set_freq[7] = 0x8f;
168 }
169
170 #define V4L2_STD_MN     (V4L2_STD_PAL_M|V4L2_STD_PAL_N|V4L2_STD_PAL_Nc|V4L2_STD_NTSC)
171 #define V4L2_STD_B      (V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_SECAM_B)
172 #define V4L2_STD_GH     (V4L2_STD_PAL_G|V4L2_STD_PAL_H|V4L2_STD_SECAM_G|V4L2_STD_SECAM_H)
173 #define V4L2_STD_DK     (V4L2_STD_PAL_DK|V4L2_STD_SECAM_DK)
174
175 static void set_audio(struct tuner *t)
176 {
177         t->i2c_easy_mode[0] = 0x01;
178
179         if (t->std & V4L2_STD_MN)
180                 t->i2c_easy_mode[1] = 0x01;
181         else if (t->std & V4L2_STD_B)
182                 t->i2c_easy_mode[1] = 0x02;
183         else if (t->std & V4L2_STD_GH)
184                 t->i2c_easy_mode[1] = 0x04;
185         else if (t->std & V4L2_STD_PAL_I)
186                 t->i2c_easy_mode[1] = 0x08;
187         else if (t->std & V4L2_STD_DK)
188                 t->i2c_easy_mode[1] = 0x10;
189         else if (t->std & V4L2_STD_SECAM_L)
190                 t->i2c_easy_mode[1] = 0x20;
191 }
192
193 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
194 {
195         struct tuner *t = i2c_get_clientdata(c);
196
197         set_audio(t);
198         set_frequency(t, 864, freq);
199         tda8290_tune(c);
200 }
201
202 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
203 {
204         struct tuner *t = i2c_get_clientdata(c);
205         set_frequency(t, 704, freq);
206         tda8290_tune(c);
207 }
208
209 static int has_signal(struct i2c_client *c)
210 {
211         unsigned char i2c_get_afc[1] = { 0x1B };
212         unsigned char afc = 0;
213
214         i2c_master_send(c, i2c_get_afc, ARRAY_SIZE(i2c_get_afc));
215         i2c_master_recv(c, &afc, 1);
216         return (afc & 0x80)? 65535:0;
217 }
218
219 static void standby(struct i2c_client *c)
220 {
221         i2c_transfer(c->adapter, i2c_msg_standby, ARRAY_SIZE(i2c_msg_standby));
222 }
223
224 int tda8290_init(struct i2c_client *c)
225 {
226         struct tuner *t = i2c_get_clientdata(c);
227
228         strlcpy(c->name, "tda8290+75", sizeof(c->name));
229         tuner_info("tuner: type set to %s\n", c->name);
230         t->tv_freq    = set_tv_freq;
231         t->radio_freq = set_radio_freq;
232         t->has_signal = has_signal;
233         t->standby = standby;
234
235         i2c_master_send(c, i2c_enable_bridge, ARRAY_SIZE(i2c_enable_bridge));
236         i2c_transfer(c->adapter, i2c_msg_init, ARRAY_SIZE(i2c_msg_init));
237         return 0;
238 }
239
240 /*
241  * Overrides for Emacs so that we follow Linus's tabbing style.
242  * ---------------------------------------------------------------------------
243  * Local variables:
244  * c-basic-offset: 8
245  * End:
246  */