blob: f59d4601cc6311118f10774445173f7ec69d6c29 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -07002 * $Id: tda8290.c,v 1.11 2005/06/18 06:09:06 nsh Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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
14struct freq_entry {
15 u16 freq;
16 u8 value;
17};
18
19static 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
45static struct freq_entry div_table[] = {
46 { 0x1C34, 3 },
47 { 0x0D34, 2 },
48 { 0x067B, 1 },
49 { 0x0000, 0 },
50};
51
52static struct freq_entry agc_table[] = {
53 { 0x22B4, 0x8F },
54 { 0x0B54, 0x9F },
55 { 0x09A4, 0x8F },
56 { 0x0554, 0x9F },
57 { 0x0000, 0xBF },
58};
59
60static __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
69static unsigned char i2c_enable_bridge[2] = { 0x21, 0xC0 };
70static unsigned char i2c_disable_bridge[2] = { 0x21, 0x80 };
71static unsigned char i2c_init_tda8275[14] = { 0x00, 0x00, 0x00, 0x00,
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -070072 0xfC, 0x04, 0xA3, 0x3F,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 0x2A, 0x04, 0xFF, 0x00,
74 0x00, 0x40 };
75static unsigned char i2c_set_VS[2] = { 0x30, 0x6F };
76static unsigned char i2c_set_GP01_CF[2] = { 0x20, 0x0B };
77static unsigned char i2c_tda8290_reset[2] = { 0x00, 0x00 };
78static unsigned char i2c_gainset_off[2] = { 0x28, 0x14 };
79static unsigned char i2c_gainset_on[2] = { 0x28, 0x54 };
80static unsigned char i2c_agc3_00[2] = { 0x80, 0x00 };
81static unsigned char i2c_agc2_BF[2] = { 0x60, 0xBF };
82static unsigned char i2c_cb1_D2[2] = { 0x30, 0xD2 };
83static unsigned char i2c_cb1_56[2] = { 0x30, 0x56 };
84static unsigned char i2c_cb1_52[2] = { 0x30, 0x52 };
85static unsigned char i2c_cb1_50[2] = { 0x30, 0x50 };
86static unsigned char i2c_agc2_7F[2] = { 0x60, 0x7F };
87static unsigned char i2c_agc3_08[2] = { 0x80, 0x08 };
88
89static 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
96static 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
103static 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
112static 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
120static 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
139static void set_frequency(struct tuner *t, u16 ifc)
140{
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700141 u32 freq;
142 u32 N;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700144 if (t->mode == V4L2_TUNER_RADIO)
145 freq = t->freq / 1000;
146 else
147 freq = t->freq;
148
149 N = (((freq<<3)+ifc)&0x3fffc);
150
151 N = N >> get_freq_entry(div_table, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 t->i2c_set_freq[0] = 0;
153 t->i2c_set_freq[1] = (unsigned char)(N>>8);
154 t->i2c_set_freq[2] = (unsigned char) N;
155 t->i2c_set_freq[3] = 0x40;
156 t->i2c_set_freq[4] = 0x52;
Mauro Carvalho Chehab586b0ca2005-06-28 20:45:21 -0700157 t->i2c_set_freq[5] = get_freq_entry(band_table, freq);
158 t->i2c_set_freq[6] = get_freq_entry(agc_table, freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 t->i2c_set_freq[7] = 0x8f;
160}
161
162#define V4L2_STD_MN (V4L2_STD_PAL_M|V4L2_STD_PAL_N|V4L2_STD_PAL_Nc|V4L2_STD_NTSC)
163#define V4L2_STD_B (V4L2_STD_PAL_B|V4L2_STD_PAL_B1|V4L2_STD_SECAM_B)
164#define V4L2_STD_GH (V4L2_STD_PAL_G|V4L2_STD_PAL_H|V4L2_STD_SECAM_G|V4L2_STD_SECAM_H)
165#define V4L2_STD_DK (V4L2_STD_PAL_DK|V4L2_STD_SECAM_DK)
166
167static void set_audio(struct tuner *t)
168{
169 t->i2c_easy_mode[0] = 0x01;
170
171 if (t->std & V4L2_STD_MN)
172 t->i2c_easy_mode[1] = 0x01;
173 else if (t->std & V4L2_STD_B)
174 t->i2c_easy_mode[1] = 0x02;
175 else if (t->std & V4L2_STD_GH)
176 t->i2c_easy_mode[1] = 0x04;
177 else if (t->std & V4L2_STD_PAL_I)
178 t->i2c_easy_mode[1] = 0x08;
179 else if (t->std & V4L2_STD_DK)
180 t->i2c_easy_mode[1] = 0x10;
181 else if (t->std & V4L2_STD_SECAM_L)
182 t->i2c_easy_mode[1] = 0x20;
183}
184
185static void set_tv_freq(struct i2c_client *c, unsigned int freq)
186{
187 struct tuner *t = i2c_get_clientdata(c);
188
189 set_audio(t);
190 set_frequency(t, 864);
191 tda8290_tune(c);
192}
193
194static void set_radio_freq(struct i2c_client *c, unsigned int freq)
195{
196 struct tuner *t = i2c_get_clientdata(c);
197 set_frequency(t, 704);
198 tda8290_tune(c);
199}
200
201static int has_signal(struct i2c_client *c)
202{
203 unsigned char i2c_get_afc[1] = { 0x1B };
204 unsigned char afc = 0;
205
206 i2c_master_send(c, i2c_get_afc, ARRAY_SIZE(i2c_get_afc));
207 i2c_master_recv(c, &afc, 1);
208 return (afc & 0x80)? 65535:0;
209}
210
211int tda8290_init(struct i2c_client *c)
212{
213 struct tuner *t = i2c_get_clientdata(c);
214
215 strlcpy(c->name, "tda8290+75", sizeof(c->name));
216 tuner_info("tuner: type set to %s\n", c->name);
217 t->tv_freq = set_tv_freq;
218 t->radio_freq = set_radio_freq;
219 t->has_signal = has_signal;
220
221 i2c_master_send(c, i2c_enable_bridge, ARRAY_SIZE(i2c_enable_bridge));
222 i2c_transfer(c->adapter, i2c_msg_init, ARRAY_SIZE(i2c_msg_init));
223 return 0;
224}
225
226/*
227 * Overrides for Emacs so that we follow Linus's tabbing style.
228 * ---------------------------------------------------------------------------
229 * Local variables:
230 * c-basic-offset: 8
231 * End:
232 */