blob: f514c5ce5c8f4a365310cd46de0cef1a3cad4e89 [file] [log] [blame]
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03001/* tuner-xc2028
2 *
3 * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
Michel Ludwig701672e2007-07-18 10:29:10 -03004 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
5 * - frontend interface
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -03006 * 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>
Michel Ludwig701672e2007-07-18 10:29:10 -030014#include <media/tuner.h>
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030015#include "tuner-driver.h"
16#include "tuner-xc2028.h"
17
Michel Ludwig701672e2007-07-18 10:29:10 -030018#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
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030026/* Firmwares used on tm5600/tm6000 + xc2028/xc3028 */
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -030027
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";
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030040
41struct xc2028_data {
Michel Ludwig701672e2007-07-18 10:29:10 -030042 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;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030050};
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
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -030086 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
Michel Ludwig701672e2007-07-18 10:29:10 -0300101 tuner_info("xc2028: Loading firmware %s\n", name);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300102 rc = request_firmware(&fw, name, &c->dev);
103 if (rc < 0) {
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300104 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
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300109 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
Michel Ludwig701672e2007-07-18 10:29:10 -0300179static int check_firmware(struct i2c_client *c, enum tuner_mode new_mode,
180 fe_bandwidth_t bandwidth)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300181{
182 int rc, version;
183 struct tuner *t = i2c_get_clientdata(c);
184 struct xc2028_data *xc2028 = t->priv;
185 const char *name;
Michel Ludwig701672e2007-07-18 10:29:10 -0300186 int change_digital_bandwidth;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300187
188 if (!t->tuner_callback) {
189 printk(KERN_ERR "xc2028: need tuner_callback to load firmware\n");
190 return -EINVAL;
191 }
192
Michel Ludwig701672e2007-07-18 10:29:10 -0300193 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
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300207 if (xc2028->need_load_generic) {
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300208 if (xc2028->bandwidth==8)
Michel Ludwig701672e2007-07-18 10:29:10 -0300209 name = firmware_8MHZ_INIT0;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300210 else
211 name = firmware_INIT0;
212
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300213 /* 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;
Michel Ludwig701672e2007-07-18 10:29:10 -0300225 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;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300251 }
252
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300253 if (xc2028->firm_type & t->std) {
254 tuner_info("xc3028: no need to load a std-specific firmware.\n");
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300255 return 0;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300256 }
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300257
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300258 rc = load_firmware(c,firmware_INIT1);
Michel Ludwigc2622e52007-07-18 10:26:38 -0300259
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300260 if (t->std & V4L2_STD_MN)
261 name=firmware_MN;
Michel Ludwig701672e2007-07-18 10:29:10 -0300262 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;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300270 else
271 name=firmware_DK;
272
Michel Ludwig701672e2007-07-18 10:29:10 -0300273 tuner_info("xc2028: loading firmware named %s.\n", name);
Michel Ludwig701672e2007-07-18 10:29:10 -0300274 rc = load_firmware(c, name);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300275 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
Michel Ludwig701672e2007-07-18 10:29:10 -0300291 printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300292
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
Michel Ludwig701672e2007-07-18 10:29:10 -0300309static void generic_set_tv_freq(struct i2c_client *c, u32 freq /* in Hz */,
310 enum tuner_mode new_mode, fe_bandwidth_t bandwidth)
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300311{
312 int rc;
313 unsigned char buf[5];
314 struct tuner *t = i2c_get_clientdata(c);
Michel Ludwig701672e2007-07-18 10:29:10 -0300315 u32 div, offset = 0;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300316
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300317 /* HACK: It seems that specific firmware need to be reloaded
318 when freq is changed */
319 struct xc2028_data *xc2028 = t->priv;
Michel Ludwig701672e2007-07-18 10:29:10 -0300320
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300321 xc2028->firm_type=0;
Michel Ludwig701672e2007-07-18 10:29:10 -0300322
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300323 /* Reset GPIO 1 */
324 if (t->tuner_callback) {
325 rc = t->tuner_callback( c->adapter->algo_data,
326 XC2028_TUNER_RESET, 0);
327 if (rc<0)
328 return;
329 }
330 msleep(10);
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300331 printk("xc3028: should set frequency %d kHz)\n", freq / 1000);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300332
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300333 if (check_firmware(c, new_mode, bandwidth)<0)
334 return;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300335
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300336 if(new_mode == T_DIGITAL_TV)
337 offset = 2750000;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300338
Mauro Carvalho Chehabd4e76682007-07-18 23:14:25 -0300339 div = (freq - offset + DIV/2)/DIV;
Mauro Carvalho Chehab2e4160c2007-07-18 13:33:23 -0300340
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300341 /* CMD= Set frequency */
342 send_seq(c, {0x00, 0x02, 0x00, 0x00});
343 if (t->tuner_callback) {
344 rc = t->tuner_callback( c->adapter->algo_data,
345 XC2028_RESET_CLK, 1);
346 if (rc<0)
347 return;
348 }
349
350 msleep(10);
Michel Ludwig701672e2007-07-18 10:29:10 -0300351
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300352 buf[0]= 0xff & (div>>24);
353 buf[1]= 0xff & (div>>16);
354 buf[2]= 0xff & (div>>8);
355 buf[3]= 0xff & (div);
356 buf[4]= 0;
357
358 i2c_send(rc, c, buf, sizeof(buf));
359 if (rc<0)
360 return;
361 msleep(100);
362
363 printk("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
364 buf[1],buf[2],buf[3],buf[4],
365 freq / 16, freq % 16 * 100 / 16);
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300366}
367
368
Michel Ludwig701672e2007-07-18 10:29:10 -0300369static void set_tv_freq(struct i2c_client *c, unsigned int freq)
370{
371 printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
372
373 generic_set_tv_freq(c, freq * 62500l, T_ANALOG_TV,
374 BANDWIDTH_8_MHZ /* unimportant */);
375}
376
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300377static void xc2028_release(struct i2c_client *c)
378{
379 struct tuner *t = i2c_get_clientdata(c);
380
381 kfree(t->priv);
382 t->priv = NULL;
383}
384
385static struct tuner_operations tea5767_tuner_ops = {
386 .set_tv_freq = set_tv_freq,
387 .has_signal = xc2028_signal,
388 .release = xc2028_release,
389// .is_stereo = xc2028_stereo,
390};
391
392
393static int init=0;
394
395int xc2028_tuner_init(struct i2c_client *c)
396{
397 struct tuner *t = i2c_get_clientdata(c);
398 int version = xc2028_get_reg(c, 0x4);
399 int prd_id = xc2028_get_reg(c, 0x8);
400 struct xc2028_data *xc2028;
401
Michel Ludwig701672e2007-07-18 10:29:10 -0300402 tuner_info("Xcv2028/3028 init called!\n");
403
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300404 if (init) {
405 printk (KERN_ERR "Module already initialized!\n");
406 return 0;
407 }
408 init++;
409
410 xc2028 = kzalloc(sizeof(*xc2028), GFP_KERNEL);
411 if (!xc2028)
412 return -ENOMEM;
413 t->priv = xc2028;
414
Michel Ludwig701672e2007-07-18 10:29:10 -0300415 xc2028->bandwidth=BANDWIDTH_6_MHZ;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300416 xc2028->need_load_generic=1;
Michel Ludwig701672e2007-07-18 10:29:10 -0300417 xc2028->mode = T_UNINITIALIZED;
Mauro Carvalho Chehab6cb45872007-10-02 11:57:03 -0300418
419 /* FIXME: Check where t->priv will be freed */
420
421 if (version<0)
422 version=0;
423
424 if (prd_id<0)
425 prd_id=0;
426
427 strlcpy(c->name, "xc2028", sizeof(c->name));
428 tuner_info("type set to %d (%s, hw ver=%d.%d, fw ver=%d.%d, id=0x%04x)\n",
429 t->type, c->name,
430 (version>>12)&0x0f,(version>>8)&0x0f,
431 (version>>4)&0x0f,(version)&0x0f, prd_id);
432
433 memcpy(&t->ops, &tea5767_tuner_ops, sizeof(struct tuner_operations));
434
435 return 0;
436}
Michel Ludwig701672e2007-07-18 10:29:10 -0300437
438static int xc3028_set_params(struct dvb_frontend *fe,
439 struct dvb_frontend_parameters *p)
440{
441 struct i2c_client *c = fe->tuner_priv;
442
443 printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
444
445 generic_set_tv_freq(c, p->frequency, T_DIGITAL_TV,
446 p->u.ofdm.bandwidth);
447
448 return 0;
449}
450
451static int xc3028_dvb_release(struct dvb_frontend *fe)
452{
453 printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
454
455 fe->tuner_priv = NULL;
456
457 return 0;
458}
459
460static int xc3028_dvb_init(struct dvb_frontend *fe)
461{
462 printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
463
464 return 0;
465}
466
467static const struct dvb_tuner_ops xc3028_dvb_tuner_ops = {
468 .info = {
469 .name = "Xceive XC3028",
470 .frequency_min = 42000000,
471 .frequency_max = 864000000,
472 .frequency_step = 50000,
473 },
474
475 .release = xc3028_dvb_release,
476 .init = xc3028_dvb_init,
477
478// int (*sleep)(struct dvb_frontend *fe);
479
480 /** This is for simple PLLs - set all parameters in one go. */
481 .set_params = xc3028_set_params,
482
483 /** This is support for demods like the mt352 - fills out the supplied buffer with what to write. */
484// int (*calc_regs)(struct dvb_frontend *fe, struct dvb_frontend_parameters *p, u8 *buf, int buf_len);
485
486// int (*get_frequency)(struct dvb_frontend *fe, u32 *frequency);
487// int (*get_bandwidth)(struct dvb_frontend *fe, u32 *bandwidth);
488
489// int (*get_status)(struct dvb_frontend *fe, u32 *status);
490};
491
492int xc2028_attach(struct i2c_client *c, struct dvb_frontend *fe)
493{
494 fe->tuner_priv = c;
495
496 memcpy(&fe->ops.tuner_ops, &xc3028_dvb_tuner_ops, sizeof(fe->ops.tuner_ops));
497
498 return 0;
499}
500
501EXPORT_SYMBOL(xc2028_attach);
502