3 * Copyright (c) 2007 Mauro Carvalho Chehab (mchehab@infradead.org)
4 * Copyright (c) 2007 Michel Ludwig (michel.ludwig@gmail.com)
6 * This code is placed under the terms of the GNU General Public License v2
10 #include <asm/div64.h>
11 #include <linux/firmware.h>
12 #include <linux/videodev.h>
13 #include <linux/delay.h>
14 #include <media/tuner.h>
15 #include <linux/mutex.h>
16 #include "tuner-driver.h"
17 #include "tuner-xc2028.h"
19 #include <linux/dvb/frontend.h>
20 #include "dvb_frontend.h"
22 /* digital TV standards */
23 #define V4L2_STD_DTV_6MHZ ((v4l2_std_id)0x04000000)
24 #define V4L2_STD_DTV_7MHZ ((v4l2_std_id)0x08000000)
25 #define V4L2_STD_DTV_8MHZ ((v4l2_std_id)0x10000000)
27 /* Firmwares used on tm5600/tm6000 + xc2028/xc3028 */
29 /* Generic firmwares */
30 static const char *firmware_INIT0 = "tm_xc3028_MTS_init0.fw";
31 static const char *firmware_8MHZ_INIT0 = "tm_xc3028_8M_MTS_init0.fw";
32 static const char *firmware_INIT1 = "tm_xc3028_68M_MTS_init1.fw";
34 /* Standard-specific firmwares */
35 static const char *firmware_6M = "tm_xc3028_DTV_6M.fw";
36 static const char *firmware_7M = "tm_xc3028_DTV_7M.fw";
37 static const char *firmware_8M = "tm_xc3028_DTV_8M.fw";
38 static const char *firmware_B = "tm_xc3028_B_PAL.fw";
39 static const char *firmware_DK = "tm_xc3028_DK_PAL_MTS.fw";
40 static const char *firmware_MN = "tm_xc3028_MN_BTSC.fw";
43 v4l2_std_id firm_type; /* video stds supported
44 by current firmware */
45 fe_bandwidth_t bandwidth; /* Firmware bandwidth:
47 int need_load_generic; /* The generic firmware
50 struct i2c_client *i2c_client;
55 #define i2c_send(rc,c,buf,size) \
56 if (size != (rc = i2c_master_send(c, buf, size))) \
57 tuner_warn("i2c output error: rc = %d (should be %d)\n", \
60 #define i2c_rcv(rc,c,buf,size) \
61 if (size != (rc = i2c_master_recv(c, buf, size))) \
62 tuner_warn("i2c input error: rc = %d (should be %d)\n", \
65 #define send_seq(c, data...) \
67 const static u8 _val[] = data; \
69 (rc = i2c_master_send \
70 (c, _val, sizeof(_val)))) { \
71 printk(KERN_ERR "Error on line %d: %d\n",__LINE__,rc); \
77 static int xc2028_get_reg(struct i2c_client *c, u16 reg)
81 struct tuner *t = i2c_get_clientdata(c);
85 i2c_send(rc, c, buf, sizeof(buf));
89 i2c_rcv(rc, c, buf, 2);
93 return (buf[1])|(buf[0]<<8);
96 static int load_firmware (struct i2c_client *c, const char *name)
98 const struct firmware *fw=NULL;
99 struct tuner *t = i2c_get_clientdata(c);
100 unsigned char *p, *endp;
102 static const char firmware_ver[] = "tm6000/xcv v1";
104 tuner_info("xc2028: Loading firmware %s\n", name);
105 rc = request_firmware(&fw, name, &c->dev);
108 tuner_info("Error: firmware %s not found.\n", name);
110 tuner_info("Error %d while requesting firmware %s \n", rc, name);
118 tuner_info("Error: firmware size is zero!\n");
122 if (fw->size<sizeof(firmware_ver)-1) {
123 /* Firmware is incorrect */
124 tuner_info("Error: firmware size is less than header (%d<%d)!\n",
125 (int)fw->size,(int)sizeof(firmware_ver)-1);
130 if (memcmp(p,firmware_ver,sizeof(firmware_ver)-1)) {
131 /* Firmware is incorrect */
132 tuner_info("Error: firmware is not for tm5600/6000 + Xcv2028/3028!\n");
136 p+=sizeof(firmware_ver)-1;
140 /* Special callback command received */
141 rc = t->tuner_callback(c->adapter->algo_data,
142 XC2028_TUNER_RESET, (*p)&0x7f);
144 tuner_info("Error at RESET code %d\n",
154 /* Firmware is incorrect */
155 tuner_info("Error: firmware is truncated!\n");
160 tuner_info("Error: firmware file is corrupted!\n");
165 i2c_send(rc, c, p, len);
177 release_firmware(fw);
182 static int check_firmware(struct i2c_client *c, enum tuner_mode new_mode,
183 fe_bandwidth_t bandwidth)
186 struct tuner *t = i2c_get_clientdata(c);
187 struct xc2028_data *xc2028 = t->priv;
189 int change_digital_bandwidth;
191 if (!t->tuner_callback) {
192 printk(KERN_ERR "xc2028: need tuner_callback to load firmware\n");
196 printk(KERN_INFO "xc2028: I am in mode %u and I should switch to mode %i\n",
197 xc2028->mode, new_mode);
199 /* first of all, determine whether we have switched the mode */
200 if(new_mode != xc2028->mode) {
201 xc2028->mode = new_mode;
202 xc2028->need_load_generic = 1;
205 change_digital_bandwidth = (xc2028->mode == T_DIGITAL_TV
206 && bandwidth != xc2028->bandwidth) ? 1 : 0;
207 tuner_info("xc2028: old bandwidth %u, new bandwidth %u\n", xc2028->bandwidth,
210 if (xc2028->need_load_generic) {
211 if (xc2028->bandwidth==8)
212 name = firmware_8MHZ_INIT0;
214 name = firmware_INIT0;
216 /* Reset is needed before loading firmware */
217 rc = t->tuner_callback(c->adapter->algo_data,
218 XC2028_TUNER_RESET, 0);
222 rc = load_firmware(c,name);
226 xc2028->need_load_generic=0;
228 if(xc2028->mode == T_DIGITAL_TV) {
229 change_digital_bandwidth=1;
233 tuner_info("xc2028: I should change bandwidth %u\n",
234 change_digital_bandwidth);
236 if (change_digital_bandwidth) {
238 case BANDWIDTH_8_MHZ:
239 t->std = V4L2_STD_DTV_8MHZ;
242 case BANDWIDTH_7_MHZ:
243 t->std = V4L2_STD_DTV_7MHZ;
246 case BANDWIDTH_6_MHZ:
247 t->std = V4L2_STD_DTV_6MHZ;
251 tuner_info("error: bandwidth not supported.\n");
253 xc2028->bandwidth = bandwidth;
256 if (xc2028->firm_type & t->std) {
257 tuner_info("xc3028: no need to load a std-specific firmware.\n");
261 rc = load_firmware(c,firmware_INIT1);
263 if (t->std & V4L2_STD_MN)
265 else if (t->std & V4L2_STD_DTV_6MHZ)
267 else if (t->std & V4L2_STD_DTV_7MHZ)
269 else if (t->std & V4L2_STD_DTV_8MHZ)
271 else if (t->std & V4L2_STD_PAL_B)
276 tuner_info("xc2028: loading firmware named %s.\n", name);
277 rc = load_firmware(c, name);
281 version = xc2028_get_reg(c, 0x4);
282 tuner_info("Firmware version is %d.%d\n",
283 (version>>4)&0x0f,(version)&0x0f);
285 xc2028->firm_type=t->std;
290 static int xc2028_signal(struct i2c_client *c)
292 struct tuner *t = i2c_get_clientdata(c);
293 struct xc2028_data *xc2028 = t->priv;
294 int frq_lock, signal=0;
296 mutex_lock(&xc2028->lock);
298 printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
300 frq_lock = xc2028_get_reg(c, 0x2);
304 /* Frequency is locked. Return signal quality */
306 signal = xc2028_get_reg(c, 0x40);
313 mutex_unlock(&xc2028->lock);
320 static void generic_set_tv_freq(struct i2c_client *c, u32 freq /* in Hz */,
321 enum tuner_mode new_mode, fe_bandwidth_t bandwidth)
324 unsigned char buf[5];
325 struct tuner *t = i2c_get_clientdata(c);
328 /* HACK: It seems that specific firmware need to be reloaded
329 when freq is changed */
330 struct xc2028_data *xc2028 = t->priv;
332 mutex_lock(&xc2028->lock);
337 if (t->tuner_callback) {
338 rc = t->tuner_callback( c->adapter->algo_data,
339 XC2028_TUNER_RESET, 0);
344 printk("xc3028: should set frequency %d kHz)\n", freq / 1000);
346 if (check_firmware(c, new_mode, bandwidth)<0)
349 if(new_mode == T_DIGITAL_TV)
352 div = (freq - offset + DIV/2)/DIV;
354 /* CMD= Set frequency */
355 send_seq(c, {0x00, 0x02, 0x00, 0x00});
356 if (t->tuner_callback) {
357 rc = t->tuner_callback( c->adapter->algo_data,
358 XC2028_RESET_CLK, 1);
365 buf[0]= 0xff & (div>>24);
366 buf[1]= 0xff & (div>>16);
367 buf[2]= 0xff & (div>>8);
368 buf[3]= 0xff & (div);
371 i2c_send(rc, c, buf, sizeof(buf));
376 printk("divider= %02x %02x %02x %02x (freq=%d.%02d)\n",
377 buf[1],buf[2],buf[3],buf[4],
378 freq / 16, freq % 16 * 100 / 16);
381 mutex_unlock(&xc2028->lock);
385 static void set_tv_freq(struct i2c_client *c, unsigned int freq)
387 printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
389 generic_set_tv_freq(c, freq * 62500l, T_ANALOG_TV,
390 BANDWIDTH_8_MHZ /* unimportant */);
393 static void xc2028_release(struct i2c_client *c)
395 struct tuner *t = i2c_get_clientdata(c);
401 static struct tuner_operations tea5767_tuner_ops = {
402 .set_tv_freq = set_tv_freq,
403 .has_signal = xc2028_signal,
404 .release = xc2028_release,
405 // .is_stereo = xc2028_stereo,
411 int xc2028_tuner_init(struct i2c_client *c)
413 struct tuner *t = i2c_get_clientdata(c);
414 int version = xc2028_get_reg(c, 0x4);
415 int prd_id = xc2028_get_reg(c, 0x8);
416 struct xc2028_data *xc2028;
418 tuner_info("Xcv2028/3028 init called!\n");
421 printk (KERN_ERR "Module already initialized!\n");
426 xc2028 = kzalloc(sizeof(*xc2028), GFP_KERNEL);
431 xc2028->bandwidth=BANDWIDTH_6_MHZ;
432 xc2028->need_load_generic=1;
433 xc2028->mode = T_UNINITIALIZED;
435 mutex_init(&xc2028->lock);
437 /* FIXME: Check where t->priv will be freed */
445 strlcpy(c->name, "xc2028", sizeof(c->name));
446 tuner_info("type set to %d (%s, hw ver=%d.%d, fw ver=%d.%d, id=0x%04x)\n",
448 (version>>12)&0x0f,(version>>8)&0x0f,
449 (version>>4)&0x0f,(version)&0x0f, prd_id);
451 memcpy(&t->ops, &tea5767_tuner_ops, sizeof(struct tuner_operations));
456 static int xc3028_set_params(struct dvb_frontend *fe,
457 struct dvb_frontend_parameters *p)
459 struct i2c_client *c = fe->tuner_priv;
461 printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
463 generic_set_tv_freq(c, p->frequency, T_DIGITAL_TV,
464 p->u.ofdm.bandwidth);
469 static int xc3028_dvb_release(struct dvb_frontend *fe)
471 printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
473 fe->tuner_priv = NULL;
478 static int xc3028_dvb_init(struct dvb_frontend *fe)
480 printk(KERN_INFO "xc2028: %s called\n", __FUNCTION__);
485 static const struct dvb_tuner_ops xc3028_dvb_tuner_ops = {
487 .name = "Xceive XC3028",
488 .frequency_min = 42000000,
489 .frequency_max = 864000000,
490 .frequency_step = 50000,
493 .release = xc3028_dvb_release,
494 .init = xc3028_dvb_init,
496 // int (*sleep)(struct dvb_frontend *fe);
498 /** This is for simple PLLs - set all parameters in one go. */
499 .set_params = xc3028_set_params,
501 /** This is support for demods like the mt352 - fills out the supplied buffer with what to write. */
502 // int (*calc_regs)(struct dvb_frontend *fe, struct dvb_frontend_parameters *p, u8 *buf, int buf_len);
504 // int (*get_frequency)(struct dvb_frontend *fe, u32 *frequency);
505 // int (*get_bandwidth)(struct dvb_frontend *fe, u32 *bandwidth);
507 // int (*get_status)(struct dvb_frontend *fe, u32 *status);
510 int xc2028_attach(struct i2c_client *c, struct dvb_frontend *fe)
514 memcpy(&fe->ops.tuner_ops, &xc3028_dvb_tuner_ops, sizeof(fe->ops.tuner_ops));
521 EXPORT_SYMBOL(xc2028_attach);