Linux-2.6.12-rc2
[pandora-kernel.git] / drivers / media / video / tda8290.c
1 /*
2  * $Id: tda8290.c,v 1.7 2005/03/07 12:01:51 kraxel 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                                                   0x7C, 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)
140 {
141         u32 N = (((t->freq<<3)+ifc)&0x3fffc);
142
143         N = N >> get_freq_entry(div_table, t->freq);
144         t->i2c_set_freq[0] = 0;
145         t->i2c_set_freq[1] = (unsigned char)(N>>8);
146         t->i2c_set_freq[2] = (unsigned char) N;
147         t->i2c_set_freq[3] = 0x40;
148         t->i2c_set_freq[4] = 0x52;
149         t->i2c_set_freq[5] = get_freq_entry(band_table, t->freq);
150         t->i2c_set_freq[6] = get_freq_entry(agc_table,  t->freq);
151         t->i2c_set_freq[7] = 0x8f;
152 }
153
154 #define V4L2_STD_MN     (V4L2_STD_PAL_M|V4L2_STD_PAL_N|V4L2_STD_PAL_Nc|V4L2_STD_NTSC)
155 #define V4L2_STD_B      (V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_SECAM_B)
156 #define V4L2_STD_GH     (V4L2_STD_PAL_G|V4L2_STD_PAL_H|V4L2_STD_SECAM_G|V4L2_STD_SECAM_H)
157 #define V4L2_STD_DK     (V4L2_STD_PAL_DK|V4L2_STD_SECAM_DK)
158
159 static void set_audio(struct tuner *t)
160 {
161         t->i2c_easy_mode[0] = 0x01;
162
163         if (t->std & V4L2_STD_MN)
164                 t->i2c_easy_mode[1] = 0x01;
165         else if (t->std & V4L2_STD_B)
166                 t->i2c_easy_mode[1] = 0x02;
167         else if (t->std & V4L2_STD_GH)
168                 t->i2c_easy_mode[1] = 0x04;
169         else if (t->std & V4L2_STD_PAL_I)
170                 t->i2c_easy_mode[1] = 0x08;
171         else if (t->std & V4L2_STD_DK)
172                 t->i2c_easy_mode[1] = 0x10;
173         else if (t->std & V4L2_STD_SECAM_L)
174                 t->i2c_easy_mode[1] = 0x20;
175 }
176
177 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
178 {
179         struct tuner *t = i2c_get_clientdata(c);
180
181         set_audio(t);
182         set_frequency(t, 864);
183         tda8290_tune(c);
184 }
185
186 static void set_radio_freq(struct i2c_client *c, unsigned int freq)
187 {
188         struct tuner *t = i2c_get_clientdata(c);
189         set_frequency(t, 704);
190         tda8290_tune(c);
191 }
192
193 static int has_signal(struct i2c_client *c)
194 {
195         unsigned char i2c_get_afc[1] = { 0x1B };
196         unsigned char afc = 0;
197
198         i2c_master_send(c, i2c_get_afc, ARRAY_SIZE(i2c_get_afc));
199         i2c_master_recv(c, &afc, 1);
200         return (afc & 0x80)? 65535:0;
201 }
202
203 int tda8290_init(struct i2c_client *c)
204 {
205         struct tuner *t = i2c_get_clientdata(c);
206
207         strlcpy(c->name, "tda8290+75", sizeof(c->name));
208         tuner_info("tuner: type set to %s\n", c->name);
209         t->tv_freq    = set_tv_freq;
210         t->radio_freq = set_radio_freq;
211         t->has_signal = has_signal;
212
213         i2c_master_send(c, i2c_enable_bridge, ARRAY_SIZE(i2c_enable_bridge));
214         i2c_transfer(c->adapter, i2c_msg_init, ARRAY_SIZE(i2c_msg_init));
215         return 0;
216 }
217
218 /*
219  * Overrides for Emacs so that we follow Linus's tabbing style.
220  * ---------------------------------------------------------------------------
221  * Local variables:
222  * c-basic-offset: 8
223  * End:
224  */