3 * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
4 * This code is placed under the terms of the GNU General Public License v2
9 #include <linux/firmware.h>
10 #include <linux/videodev.h>
11 #include <linux/delay.h>
12 #include "tuner-driver.h"
13 #include "tuner-xc2028.h"
15 /* Firmwares used on tm5600/tm6000 + xc2028/xc3028 */
16 static const char *firmware_6M = "tm6000_xc3028_DTV_6M.fw";
17 static const char *firmware_8M = "tm6000_xc3028_78M.fw";
18 static const char *firmware_DK = "tm6000_xc3028_DK_PAL_MTS.fw";
19 static const char *firmware_MN = "tm6000_xc3028_MN_BTSC.fw";
22 v4l2_std_id firm_type; /* video stds supported by current firmware */
23 int bandwidth; /* Firmware bandwidth: 6M, 7M or 8M */
24 int need_load_generic; /* The generic firmware were loaded? */
27 #define i2c_send(rc,c,buf,size) \
28 if (size != (rc = i2c_master_send(c, buf, size))) \
29 tuner_warn("i2c output error: rc = %d (should be %d)\n", \
32 #define i2c_rcv(rc,c,buf,size) \
33 if (size != (rc = i2c_master_recv(c, buf, size))) \
34 tuner_warn("i2c input error: rc = %d (should be %d)\n", \
37 #define send_seq(c, data...) \
39 const static u8 _val[] = data; \
41 (rc = i2c_master_send \
42 (c, _val, sizeof(_val)))) { \
43 printk(KERN_ERR "Error on line %d: %d\n",__LINE__,rc); \
49 static int xc2028_get_reg(struct i2c_client *c, u16 reg)
53 struct tuner *t = i2c_get_clientdata(c);
57 i2c_send(rc, c, buf, sizeof(buf));
61 if (t->tuner_callback) {
62 rc = t->tuner_callback( c->adapter->algo_data,
68 i2c_rcv(rc, c, buf, 2);
72 return (buf[1])|(buf[0]<<8);
75 static int load_firmware (struct i2c_client *c, const char *name)
77 const struct firmware *fw=NULL;
78 struct tuner *t = i2c_get_clientdata(c);
79 unsigned char *p, *endp;
81 static const char firmware_ver[] = "tm6000/xcv v1";
83 tuner_info("Loading firmware %s\n", name);
84 rc = request_firmware(&fw, name, &c->dev);
86 tuner_info("Error %d while requesting firmware\n", rc);
93 tuner_info("Error: firmware size is zero!\n");
97 if (fw->size<sizeof(firmware_ver)-1) {
98 /* Firmware is incorrect */
99 tuner_info("Error: firmware size is less than header (%d<%d)!\n",
100 (int)fw->size,(int)sizeof(firmware_ver)-1);
105 if (memcmp(p,firmware_ver,sizeof(firmware_ver)-1)) {
106 /* Firmware is incorrect */
107 tuner_info("Error: firmware is not for tm5600/6000 + Xcv2028/3028!\n");
111 p+=sizeof(firmware_ver)-1;
115 /* Special callback command received */
116 rc = t->tuner_callback(c->adapter->algo_data,
117 XC2028_TUNER_RESET, (*p)&0x7f);
119 tuner_info("Error at RESET code %d\n",
129 /* Firmware is incorrect */
130 tuner_info("Error: firmware is truncated!\n");
135 tuner_info("Error: firmware file is corrupted!\n");
140 i2c_send(rc, c, p, len);
152 release_firmware(fw);
157 static int check_firmware(struct i2c_client *c)
160 struct tuner *t = i2c_get_clientdata(c);
161 struct xc2028_data *xc2028 = t->priv;
164 if (!t->tuner_callback) {
165 printk(KERN_ERR "xc2028: need tuner_callback to load firmware\n");
169 if (xc2028->need_load_generic) {
170 if (xc2028->bandwidth==6)
175 /* Reset is needed before loading firmware */
176 rc = t->tuner_callback(c->adapter->algo_data,
177 XC2028_TUNER_RESET, 0);
181 rc = load_firmware(c,name);
185 xc2028->need_load_generic=0;
189 if (xc2028->firm_type & t->std)
192 send_seq (c, {0x12, 0x39});
193 send_seq (c, {0x0c, 0x80, 0xf0, 0xf7, 0x3e, 0x75, 0xc1, 0x8a, 0xe4});
194 send_seq (c, {0x0c, 0x02, 0x00});
195 send_seq (c, {0x05, 0x0f, 0xee, 0xaa, 0x5f, 0xea, 0x90});
196 send_seq (c, {0x06, 0x00, 0x0a, 0x4d, 0x8c, 0xf2, 0xd8, 0xcf, 0x30});
197 send_seq (c, {0x06, 0x79, 0x9f});
198 send_seq (c, {0x0b, 0x0d, 0xa4, 0x6c});
199 send_seq (c, {0x0a, 0x01, 0x67, 0x24, 0x40, 0x08, 0xc3, 0x20, 0x10});
200 send_seq (c, {0x0a, 0x64, 0x3c, 0xfa, 0xf7, 0xe1, 0x0c, 0x2c});
201 send_seq (c, {0x09, 0x0b});
202 send_seq (c, {0x10, 0x13});
203 send_seq (c, {0x16, 0x12});
204 send_seq (c, {0x1f, 0x02});
205 send_seq (c, {0x21, 0x02});
206 send_seq (c, {0x01, 0x02});
207 send_seq (c, {0x2b, 0x10});
208 send_seq (c, {0x02, 0x02});
209 send_seq (c, {0x02, 0x03});
210 send_seq (c, {0x00, 0x8c});
212 if (t->std & V4L2_STD_MN)
217 rc = load_firmware(c,name);
221 version = xc2028_get_reg(c, 0x4);
222 tuner_info("Firmware version is %d.%d\n",
223 (version>>4)&0x0f,(version)&0x0f);
225 xc2028->firm_type=t->std;
230 static int xc2028_signal(struct i2c_client *c)
234 if (check_firmware(c)<0)
237 lock = xc2028_get_reg(c, 0x2);
241 /* Frequency is locked. Return signal quality */
243 signal = xc2028_get_reg(c, 0x40);
253 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
256 unsigned char buf[5];
257 struct tuner *t = i2c_get_clientdata(c);
258 unsigned long div = (freq*62500l+DIV/2)/DIV;
260 if (check_firmware(c)<0)
264 if (t->tuner_callback) {
265 rc = t->tuner_callback( c->adapter->algo_data,
266 XC2028_TUNER_RESET, 0);
272 /* CMD= Set frequency */
273 send_seq(c, {0x00, 0x02, 0x00, 0x00});
274 if (t->tuner_callback) {
275 rc = t->tuner_callback( c->adapter->algo_data,
276 XC2028_RESET_CLK, 1);
282 // send_seq(c, {0x00, 0x00, 0x10, 0xd0, 0x00});
284 buf[0]= 0xff & (div>>24);
285 buf[1]= 0xff & (div>>16);
286 buf[2]= 0xff & (div>>8);
287 buf[3]= 0xff & (div);
290 i2c_send(rc, c, buf, sizeof(buf));
295 printk("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
296 buf[1],buf[2],buf[3],buf[4],
297 freq / 16, freq % 16 * 100 / 16);
298 // printk("signal=%d\n",xc2028_signal(c));
302 static void xc2028_release(struct i2c_client *c)
304 struct tuner *t = i2c_get_clientdata(c);
310 static struct tuner_operations tea5767_tuner_ops = {
311 .set_tv_freq = set_tv_freq,
312 .has_signal = xc2028_signal,
313 .release = xc2028_release,
314 // .is_stereo = xc2028_stereo,
320 int xc2028_tuner_init(struct i2c_client *c)
322 struct tuner *t = i2c_get_clientdata(c);
323 int version = xc2028_get_reg(c, 0x4);
324 int prd_id = xc2028_get_reg(c, 0x8);
325 struct xc2028_data *xc2028;
328 printk (KERN_ERR "Module already initialized!\n");
333 xc2028 = kzalloc(sizeof(*xc2028), GFP_KERNEL);
343 xc2028->need_load_generic=1;
345 /* FIXME: Check where t->priv will be freed */
353 strlcpy(c->name, "xc2028", sizeof(c->name));
354 tuner_info("type set to %d (%s, hw ver=%d.%d, fw ver=%d.%d, id=0x%04x)\n",
356 (version>>12)&0x0f,(version>>8)&0x0f,
357 (version>>4)&0x0f,(version)&0x0f, prd_id);
359 memcpy(&t->ops, &tea5767_tuner_ops, sizeof(struct tuner_operations));