V4L/DVB (6426): Some fixes on tuner-xc2028
[deliverable/linux.git] / drivers / media / video / tuner-xc2028.c
CommitLineData
6cb45879
MCC
1/* tuner-xc2028
2 *
3 * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
701672eb
ML
4 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
5 * - frontend interface
6cb45879
MCC
6 * This code is placed under the terms of the GNU General Public License v2
7 */
8
9#include <linux/i2c.h>
10#include <asm/div64.h>
11#include <linux/firmware.h>
12#include <linux/videodev.h>
13#include <linux/delay.h>
701672eb 14#include <media/tuner.h>
6cb45879
MCC
15#include "tuner-driver.h"
16#include "tuner-xc2028.h"
17
701672eb
ML
18#include <linux/dvb/frontend.h>
19#include "dvb_frontend.h"
20
21/* digital TV standards */
22#define V4L2_STD_DTV_6MHZ ((v4l2_std_id)0x04000000)
23#define V4L2_STD_DTV_7MHZ ((v4l2_std_id)0x08000000)
24#define V4L2_STD_DTV_8MHZ ((v4l2_std_id)0x10000000)
25
6cb45879 26/* Firmwares used on tm5600/tm6000 + xc2028/xc3028 */
2e4160ca
MCC
27
28/* Generic firmwares */
29static const char *firmware_INIT0 = "tm_xc3028_MTS_init0.fw";
30static const char *firmware_8MHZ_INIT0 = "tm_xc3028_8M_MTS_init0.fw";
31static const char *firmware_INIT1 = "tm_xc3028_68M_MTS_init1.fw";
32
33/* Standard-specific firmwares */
34static const char *firmware_6M = "tm_xc3028_DTV_6M.fw";
35static const char *firmware_7M = "tm_xc3028_7M.fw";
36static const char *firmware_8M = "tm_xc3028_8M.fw";
37static const char *firmware_B = "tm_xc3028_B_PAL.fw";
38static const char *firmware_DK = "tm_xc3028_DK_PAL_MTS.fw";
39static const char *firmware_MN = "tm_xc3028_MN_BTSC.fw";
6cb45879
MCC
40
41struct xc2028_data {
701672eb
ML
42 v4l2_std_id firm_type; /* video stds supported
43 by current firmware */
44 fe_bandwidth_t bandwidth; /* Firmware bandwidth:
45 6M, 7M or 8M */
46 int need_load_generic; /* The generic firmware
47 were loaded? */
48 enum tuner_mode mode;
49 struct i2c_client *i2c_client;
6cb45879
MCC
50};
51
52#define i2c_send(rc,c,buf,size) \
53if (size != (rc = i2c_master_send(c, buf, size))) \
54 tuner_warn("i2c output error: rc = %d (should be %d)\n", \
55 rc, (int)size);
56
57#define i2c_rcv(rc,c,buf,size) \
58if (size != (rc = i2c_master_recv(c, buf, size))) \
59 tuner_warn("i2c input error: rc = %d (should be %d)\n", \
60 rc, (int)size);
61
62#define send_seq(c, data...) \
63{ int rc; \
64 const static u8 _val[] = data; \
65 if (sizeof(_val) != \
66 (rc = i2c_master_send \
67 (c, _val, sizeof(_val)))) { \
68 printk(KERN_ERR "Error on line %d: %d\n",__LINE__,rc); \
69 return; \
70 } \
71 msleep (10); \
72}
73
74static int xc2028_get_reg(struct i2c_client *c, u16 reg)
75{
76 int rc;
77 unsigned char buf[1];
78 struct tuner *t = i2c_get_clientdata(c);
79
80 buf[0]= reg;
81
82 i2c_send(rc, c, buf, sizeof(buf));
83 if (rc<0)
84 return rc;
85
6cb45879
MCC
86 i2c_rcv(rc, c, buf, 2);
87 if (rc<0)
88 return rc;
89
90 return (buf[1])|(buf[0]<<8);
91}
92
93static int load_firmware (struct i2c_client *c, const char *name)
94{
95 const struct firmware *fw=NULL;
96 struct tuner *t = i2c_get_clientdata(c);
97 unsigned char *p, *endp;
98 int len=0, rc=0;
99 static const char firmware_ver[] = "tm6000/xcv v1";
100
701672eb 101 tuner_info("xc2028: Loading firmware %s\n", name);
6cb45879
MCC
102 rc = request_firmware(&fw, name, &c->dev);
103 if (rc < 0) {
2e4160ca
MCC
104 if (rc==-ENOENT)
105 tuner_info("Error: firmware %s not found.\n", name);
106 else
107 tuner_info("Error %d while requesting firmware %s \n", rc, name);
108
6cb45879
MCC
109 return rc;
110 }
111 p=fw->data;
112 endp=p+fw->size;
113
114 if(fw->size==0) {
115 tuner_info("Error: firmware size is zero!\n");
116 rc=-EINVAL;
117 goto err;
118 }
119 if (fw->size<sizeof(firmware_ver)-1) {
120 /* Firmware is incorrect */
121 tuner_info("Error: firmware size is less than header (%d<%d)!\n",
122 (int)fw->size,(int)sizeof(firmware_ver)-1);
123 rc=-EINVAL;
124 goto err;
125 }
126
127 if (memcmp(p,firmware_ver,sizeof(firmware_ver)-1)) {
128 /* Firmware is incorrect */
129 tuner_info("Error: firmware is not for tm5600/6000 + Xcv2028/3028!\n");
130 rc=-EINVAL;
131 goto err;
132 }
133 p+=sizeof(firmware_ver)-1;
134
135 while(p<endp) {
136 if ((*p) & 0x80) {
137 /* Special callback command received */
138 rc = t->tuner_callback(c->adapter->algo_data,
139 XC2028_TUNER_RESET, (*p)&0x7f);
140 if (rc<0) {
141 tuner_info("Error at RESET code %d\n",
142 (*p)&0x7f);
143 goto err;
144 }
145 p++;
146 continue;
147 }
148 len=*p;
149 p++;
150 if (p+len+1>endp) {
151 /* Firmware is incorrect */
152 tuner_info("Error: firmware is truncated!\n");
153 rc=-EINVAL;
154 goto err;
155 }
156 if (len<=0) {
157 tuner_info("Error: firmware file is corrupted!\n");
158 rc=-EINVAL;
159 goto err;
160 }
161
162 i2c_send(rc, c, p, len);
163 if (rc<0)
164 goto err;
165 p+=len;
166
167 if (*p)
168 msleep(*p);
169 p++;
170 }
171
172
173err:
174 release_firmware(fw);
175
176 return rc;
177}
178
701672eb
ML
179static int check_firmware(struct i2c_client *c, enum tuner_mode new_mode,
180 fe_bandwidth_t bandwidth)
6cb45879
MCC
181{
182 int rc, version;
183 struct tuner *t = i2c_get_clientdata(c);
184 struct xc2028_data *xc2028 = t->priv;
185 const char *name;
701672eb 186 int change_digital_bandwidth;
6cb45879
MCC
187
188 if (!t->tuner_callback) {
189 printk(KERN_ERR "xc2028: need tuner_callback to load firmware\n");
190 return -EINVAL;
191 }
192
701672eb
ML
193 printk(KERN_INFO "xc2028: I am in mode %u and I should switch to mode %i\n",
194 xc2028->mode, new_mode);
195
196 /* first of all, determine whether we have switched the mode */
197 if(new_mode != xc2028->mode) {
198 xc2028->mode = new_mode;
199 xc2028->need_load_generic = 1;
200 }
201
202 change_digital_bandwidth = (xc2028->mode == T_DIGITAL_TV
203 && bandwidth != xc2028->bandwidth) ? 1 : 0;
204 tuner_info("xc2028: old bandwidth %u, new bandwidth %u\n", xc2028->bandwidth,
205 bandwidth);
206
6cb45879 207 if (xc2028->need_load_generic) {
2e4160ca 208 if (xc2028->bandwidth==8)
701672eb 209 name = firmware_8MHZ_INIT0;
2e4160ca
MCC
210 else
211 name = firmware_INIT0;
212
6cb45879
MCC
213 /* Reset is needed before loading firmware */
214 rc = t->tuner_callback(c->adapter->algo_data,
215 XC2028_TUNER_RESET, 0);
216 if (rc<0)
217 return rc;
218
219 rc = load_firmware(c,name);
220 if (rc<0)
221 return rc;
222
223 xc2028->need_load_generic=0;
224 xc2028->firm_type=0;
701672eb
ML
225 if(xc2028->mode == T_DIGITAL_TV) {
226 change_digital_bandwidth=1;
227 }
228 }
229
230 tuner_info("xc2028: I should change bandwidth %u\n",
231 change_digital_bandwidth);
232
233 if (change_digital_bandwidth) {
234 switch(bandwidth) {
235 case BANDWIDTH_8_MHZ:
236 t->std = V4L2_STD_DTV_8MHZ;
237 break;
238
239 case BANDWIDTH_7_MHZ:
240 t->std = V4L2_STD_DTV_7MHZ;
241 break;
242
243 case BANDWIDTH_6_MHZ:
244 t->std = V4L2_STD_DTV_6MHZ;
245 break;
246
247 default:
248 tuner_info("error: bandwidth not supported.\n");
249 };
250 xc2028->bandwidth = bandwidth;
6cb45879
MCC
251 }
252
2e4160ca
MCC
253 if (xc2028->firm_type & t->std) {
254 tuner_info("xc3028: no need to load a std-specific firmware.\n");
6cb45879 255 return 0;
2e4160ca 256 }
6cb45879 257
2e4160ca 258 rc = load_firmware(c,firmware_INIT1);
c2622e5f 259
6cb45879
MCC
260 if (t->std & V4L2_STD_MN)
261 name=firmware_MN;
701672eb
ML
262 else if (t->std & V4L2_STD_DTV_6MHZ)
263 name=firmware_6M;
264 else if (t->std & V4L2_STD_DTV_7MHZ)
265 name=firmware_7M;
266 else if (t->std & V4L2_STD_DTV_8MHZ)
267 name=firmware_8M;
268 else if (t->std & V4L2_STD_PAL_B)
269 name=firmware_B;
6cb45879
MCC
270 else
271 name=firmware_DK;
272
701672eb 273 tuner_info("xc2028: loading firmware named %s.\n", name);
701672eb 274 rc = load_firmware(c, name);
6cb45879
MCC
275 if (rc<0)
276 return rc;
277
278 version = xc2028_get_reg(c, 0x4);
279 tuner_info("Firmware version is %d.%d\n",
280 (version>>4)&0x0f,(version)&0x0f);
281
282 xc2028->firm_type=t->std;
283
284 return 0;
285}
286
287static int xc2028_signal(struct i2c_client *c)
288{
289 int lock, signal;
290
701672eb 291 printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
6cb45879
MCC
292
293 lock = xc2028_get_reg(c, 0x2);
294 if (lock<=0)
295 return lock;
296
297 /* Frequency is locked. Return signal quality */
298
299 signal = xc2028_get_reg(c, 0x40);
300
301 if(signal<=0)
302 return lock;
303
304 return signal;
305}
306
307#define DIV 15625
308
701672eb
ML
309static void generic_set_tv_freq(struct i2c_client *c, u32 freq /* in Hz */,
310 enum tuner_mode new_mode, fe_bandwidth_t bandwidth)
6cb45879
MCC
311{
312 int rc;
313 unsigned char buf[5];
314 struct tuner *t = i2c_get_clientdata(c);
701672eb 315 u32 div, offset = 0;
6cb45879 316
701672eb
ML
317 printk("xc3028: should set frequency %d kHz)\n", freq / 1000);
318
319 if (check_firmware(c, new_mode, bandwidth)<0)
6cb45879
MCC
320 return;
321
701672eb
ML
322 if(new_mode == T_DIGITAL_TV) {
323 switch(bandwidth) {
324 case BANDWIDTH_8_MHZ:
325 offset = 2750000;
326 break;
327
328 case BANDWIDTH_7_MHZ:
329 offset = 2750000;
330 break;
331
332 case BANDWIDTH_6_MHZ:
333 default:
334 printk(KERN_ERR "xc2028: bandwidth not implemented!\n");
335 }
336 }
337
338 div = (freq - offset + DIV/2)/DIV;
339
6cb45879
MCC
340 /* Reset GPIO 1 */
341 if (t->tuner_callback) {
342 rc = t->tuner_callback( c->adapter->algo_data,
343 XC2028_TUNER_RESET, 0);
344 if (rc<0)
345 return;
346 }
347 msleep(10);
348
2e4160ca
MCC
349 char *name;
350
351 rc = load_firmware(c,firmware_INIT1);
352
353 if (t->std & V4L2_STD_MN)
354 name=firmware_MN;
355 else
356 name=firmware_DK;
357
358 rc = load_firmware(c,name);
6cb45879
MCC
359 /* CMD= Set frequency */
360 send_seq(c, {0x00, 0x02, 0x00, 0x00});
361 if (t->tuner_callback) {
362 rc = t->tuner_callback( c->adapter->algo_data,
363 XC2028_RESET_CLK, 1);
364 if (rc<0)
365 return;
366 }
367
368 msleep(10);
369// send_seq(c, {0x00, 0x00, 0x10, 0xd0, 0x00});
370// msleep(100);
701672eb 371
6cb45879
MCC
372 buf[0]= 0xff & (div>>24);
373 buf[1]= 0xff & (div>>16);
374 buf[2]= 0xff & (div>>8);
375 buf[3]= 0xff & (div);
376 buf[4]= 0;
377
378 i2c_send(rc, c, buf, sizeof(buf));
379 if (rc<0)
380 return;
381 msleep(100);
382
383 printk("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
384 buf[1],buf[2],buf[3],buf[4],
385 freq / 16, freq % 16 * 100 / 16);
386// printk("signal=%d\n",xc2028_signal(c));
387}
388
389
701672eb
ML
390static void set_tv_freq(struct i2c_client *c, unsigned int freq)
391{
392 printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
393
394 generic_set_tv_freq(c, freq * 62500l, T_ANALOG_TV,
395 BANDWIDTH_8_MHZ /* unimportant */);
396}
397
6cb45879
MCC
398static void xc2028_release(struct i2c_client *c)
399{
400 struct tuner *t = i2c_get_clientdata(c);
401
402 kfree(t->priv);
403 t->priv = NULL;
404}
405
406static struct tuner_operations tea5767_tuner_ops = {
407 .set_tv_freq = set_tv_freq,
408 .has_signal = xc2028_signal,
409 .release = xc2028_release,
410// .is_stereo = xc2028_stereo,
411};
412
413
414static int init=0;
415
416int xc2028_tuner_init(struct i2c_client *c)
417{
418 struct tuner *t = i2c_get_clientdata(c);
419 int version = xc2028_get_reg(c, 0x4);
420 int prd_id = xc2028_get_reg(c, 0x8);
421 struct xc2028_data *xc2028;
422
701672eb
ML
423 tuner_info("Xcv2028/3028 init called!\n");
424
6cb45879
MCC
425 if (init) {
426 printk (KERN_ERR "Module already initialized!\n");
427 return 0;
428 }
429 init++;
430
431 xc2028 = kzalloc(sizeof(*xc2028), GFP_KERNEL);
432 if (!xc2028)
433 return -ENOMEM;
434 t->priv = xc2028;
435
701672eb 436 xc2028->bandwidth=BANDWIDTH_6_MHZ;
6cb45879 437 xc2028->need_load_generic=1;
701672eb 438 xc2028->mode = T_UNINITIALIZED;
6cb45879
MCC
439
440 /* FIXME: Check where t->priv will be freed */
441
442 if (version<0)
443 version=0;
444
445 if (prd_id<0)
446 prd_id=0;
447
448 strlcpy(c->name, "xc2028", sizeof(c->name));
449 tuner_info("type set to %d (%s, hw ver=%d.%d, fw ver=%d.%d, id=0x%04x)\n",
450 t->type, c->name,
451 (version>>12)&0x0f,(version>>8)&0x0f,
452 (version>>4)&0x0f,(version)&0x0f, prd_id);
453
454 memcpy(&t->ops, &tea5767_tuner_ops, sizeof(struct tuner_operations));
455
456 return 0;
457}
701672eb
ML
458
459static int xc3028_set_params(struct dvb_frontend *fe,
460 struct dvb_frontend_parameters *p)
461{
462 struct i2c_client *c = fe->tuner_priv;
463
464 printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
465
466 generic_set_tv_freq(c, p->frequency, T_DIGITAL_TV,
467 p->u.ofdm.bandwidth);
468
469 return 0;
470}
471
472static int xc3028_dvb_release(struct dvb_frontend *fe)
473{
474 printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
475
476 fe->tuner_priv = NULL;
477
478 return 0;
479}
480
481static int xc3028_dvb_init(struct dvb_frontend *fe)
482{
483 printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
484
485 return 0;
486}
487
488static const struct dvb_tuner_ops xc3028_dvb_tuner_ops = {
489 .info = {
490 .name = "Xceive XC3028",
491 .frequency_min = 42000000,
492 .frequency_max = 864000000,
493 .frequency_step = 50000,
494 },
495
496 .release = xc3028_dvb_release,
497 .init = xc3028_dvb_init,
498
499// int (*sleep)(struct dvb_frontend *fe);
500
501 /** This is for simple PLLs - set all parameters in one go. */
502 .set_params = xc3028_set_params,
503
504 /** This is support for demods like the mt352 - fills out the supplied buffer with what to write. */
505// int (*calc_regs)(struct dvb_frontend *fe, struct dvb_frontend_parameters *p, u8 *buf, int buf_len);
506
507// int (*get_frequency)(struct dvb_frontend *fe, u32 *frequency);
508// int (*get_bandwidth)(struct dvb_frontend *fe, u32 *bandwidth);
509
510// int (*get_status)(struct dvb_frontend *fe, u32 *status);
511};
512
513int xc2028_attach(struct i2c_client *c, struct dvb_frontend *fe)
514{
515 fe->tuner_priv = c;
516
517 memcpy(&fe->ops.tuner_ops, &xc3028_dvb_tuner_ops, sizeof(fe->ops.tuner_ops));
518
519 return 0;
520}
521
522EXPORT_SYMBOL(xc2028_attach);
523
This page took 0.047175 seconds and 5 git commands to generate.