]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/tuner-xc2028.c
V4L/DVB (6424): Improve tuner-xc2028 script
[linux-2.6-omap-h63xx.git] / drivers / media / video / tuner-xc2028.c
1 /* tuner-xc2028
2  *
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
5  */
6
7 #include <linux/i2c.h>
8 #include <asm/div64.h>
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"
14
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";
20
21 struct xc2028_data {
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? */
25 };
26
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",        \
30         rc, (int)size);
31
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",         \
35         rc, (int)size);
36
37 #define send_seq(c, data...)                                            \
38 {       int rc;                                                         \
39         const static u8 _val[] = data;                                  \
40         if (sizeof(_val) !=                                             \
41                                 (rc = i2c_master_send                   \
42                                 (c, _val, sizeof(_val)))) {             \
43                 printk(KERN_ERR "Error on line %d: %d\n",__LINE__,rc);  \
44                 return;                                                 \
45         }                                                               \
46         msleep (10);                                                    \
47 }
48
49 static int xc2028_get_reg(struct i2c_client *c, u16 reg)
50 {
51         int rc;
52         unsigned char buf[1];
53         struct tuner *t = i2c_get_clientdata(c);
54
55         buf[0]= reg;
56
57         i2c_send(rc, c, buf, sizeof(buf));
58         if (rc<0)
59                 return rc;
60
61         if (t->tuner_callback) {
62                 rc = t->tuner_callback( c->adapter->algo_data,
63                                         XC2028_RESET_CLK, 0);
64                 if (rc<0)
65                         return rc;
66         }
67
68         i2c_rcv(rc, c, buf, 2);
69         if (rc<0)
70                 return rc;
71
72         return (buf[1])|(buf[0]<<8);
73 }
74
75 static int load_firmware (struct i2c_client *c, const char *name)
76 {
77         const struct firmware *fw=NULL;
78         struct tuner          *t = i2c_get_clientdata(c);
79         unsigned char         *p, *endp;
80         int                   len=0, rc=0;
81         static const char     firmware_ver[] = "tm6000/xcv v1";
82
83         tuner_info("Loading firmware %s\n", name);
84         rc = request_firmware(&fw, name, &c->dev);
85         if (rc < 0) {
86                 tuner_info("Error %d while requesting firmware\n", rc);
87                 return rc;
88         }
89         p=fw->data;
90         endp=p+fw->size;
91
92         if(fw->size==0) {
93                 tuner_info("Error: firmware size is zero!\n");
94                 rc=-EINVAL;
95                 goto err;
96         }
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);
101                 rc=-EINVAL;
102                 goto err;
103         }
104
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");
108                 rc=-EINVAL;
109                 goto err;
110         }
111         p+=sizeof(firmware_ver)-1;
112
113         while(p<endp) {
114                 if ((*p) & 0x80) {
115                         /* Special callback command received */
116                         rc = t->tuner_callback(c->adapter->algo_data,
117                                              XC2028_TUNER_RESET, (*p)&0x7f);
118                         if (rc<0) {
119                                 tuner_info("Error at RESET code %d\n",
120                                                                 (*p)&0x7f);
121                                 goto err;
122                         }
123                         p++;
124                         continue;
125                 }
126                 len=*p;
127                 p++;
128                 if (p+len+1>endp) {
129                         /* Firmware is incorrect */
130                         tuner_info("Error: firmware is truncated!\n");
131                         rc=-EINVAL;
132                         goto err;
133                 }
134                 if (len<=0) {
135                         tuner_info("Error: firmware file is corrupted!\n");
136                         rc=-EINVAL;
137                         goto err;
138                 }
139
140                 i2c_send(rc, c, p, len);
141                 if (rc<0)
142                         goto err;
143                 p+=len;
144
145                 if (*p)
146                         msleep(*p);
147                 p++;
148         }
149
150
151 err:
152         release_firmware(fw);
153
154         return rc;
155 }
156
157 static int check_firmware(struct i2c_client *c)
158 {
159         int                     rc, version;
160         struct tuner            *t = i2c_get_clientdata(c);
161         struct xc2028_data      *xc2028 = t->priv;
162         const char              *name;
163
164         if (!t->tuner_callback) {
165                 printk(KERN_ERR "xc2028: need tuner_callback to load firmware\n");
166                 return -EINVAL;
167         }
168
169         if (xc2028->need_load_generic) {
170                 if (xc2028->bandwidth==6)
171                         name = firmware_6M;
172                 else
173                         name = firmware_8M;
174
175                 /* Reset is needed before loading firmware */
176                 rc = t->tuner_callback(c->adapter->algo_data,
177                                      XC2028_TUNER_RESET, 0);
178                 if (rc<0)
179                         return rc;
180
181                 rc = load_firmware(c,name);
182                 if (rc<0)
183                         return rc;
184
185                 xc2028->need_load_generic=0;
186                 xc2028->firm_type=0;
187         }
188
189         if (xc2028->firm_type & t->std)
190                 return 0;
191
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});
211
212         if (t->std & V4L2_STD_MN)
213                 name=firmware_MN;
214         else
215                 name=firmware_DK;
216
217         rc = load_firmware(c,name);
218         if (rc<0)
219                 return rc;
220
221         version = xc2028_get_reg(c, 0x4);
222         tuner_info("Firmware version is %d.%d\n",
223                                         (version>>4)&0x0f,(version)&0x0f);
224
225         xc2028->firm_type=t->std;
226
227         return 0;
228 }
229
230 static int xc2028_signal(struct i2c_client *c)
231 {
232         int lock, signal;
233
234         if (check_firmware(c)<0)
235                 return 0;
236
237         lock = xc2028_get_reg(c, 0x2);
238         if (lock<=0)
239                 return lock;
240
241         /* Frequency is locked. Return signal quality */
242
243         signal = xc2028_get_reg(c, 0x40);
244
245         if(signal<=0)
246                 return lock;
247
248         return signal;
249 }
250
251 #define DIV 15625
252
253 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
254 {
255         int           rc;
256         unsigned char buf[5];
257         struct tuner  *t  = i2c_get_clientdata(c);
258         unsigned long div = (freq*62500l+DIV/2)/DIV;
259
260         if (check_firmware(c)<0)
261                 return;
262
263         /* Reset GPIO 1 */
264         if (t->tuner_callback) {
265                 rc = t->tuner_callback( c->adapter->algo_data,
266                                         XC2028_TUNER_RESET, 0);
267                 if (rc<0)
268                         return;
269         }
270         msleep(10);
271
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);
277                 if (rc<0)
278                         return;
279         }
280
281         msleep(10);
282 //      send_seq(c, {0x00, 0x00, 0x10, 0xd0, 0x00});
283 //      msleep(100);
284         buf[0]= 0xff & (div>>24);
285         buf[1]= 0xff & (div>>16);
286         buf[2]= 0xff & (div>>8);
287         buf[3]= 0xff & (div);
288         buf[4]= 0;
289
290         i2c_send(rc, c, buf, sizeof(buf));
291         if (rc<0)
292                 return;
293         msleep(100);
294
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));
299 }
300
301
302 static void xc2028_release(struct i2c_client *c)
303 {
304         struct tuner *t = i2c_get_clientdata(c);
305
306         kfree(t->priv);
307         t->priv = NULL;
308 }
309
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,
315 };
316
317
318 static int init=0;
319
320 int xc2028_tuner_init(struct i2c_client *c)
321 {
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;
326
327         if (init) {
328                 printk (KERN_ERR "Module already initialized!\n");
329                 return 0;
330         }
331         init++;
332
333         xc2028 = kzalloc(sizeof(*xc2028), GFP_KERNEL);
334         if (!xc2028)
335                 return -ENOMEM;
336         t->priv = xc2028;
337
338 #ifdef HACK
339         xc2028->firm_type=1;
340         xc2028->bandwidth=6;
341 #endif
342         xc2028->bandwidth=6;
343         xc2028->need_load_generic=1;
344
345         /* FIXME: Check where t->priv will be freed */
346
347         if (version<0)
348                 version=0;
349
350         if (prd_id<0)
351                 prd_id=0;
352
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",
355                    t->type, c->name,
356                    (version>>12)&0x0f,(version>>8)&0x0f,
357                    (version>>4)&0x0f,(version)&0x0f, prd_id);
358
359         memcpy(&t->ops, &tea5767_tuner_ops, sizeof(struct tuner_operations));
360
361         return 0;
362 }