2 * Driver for Zarlink zl10036 DVB-S silicon tuner
4 * Copyright (C) 2006 Tino Reichardt
5 * Copyright (C) 2007-2009 Matthias Schwarzott <zzam@gentoo.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License Version 2, as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 * The data sheet for this tuner can be found at:
18 * http://www.mcmilk.de/projects/dvb-card/datasheets/ZL10036.pdf
20 * This one is working: (at my Avermedia DVB-S Pro)
21 * - zl10036 (40pin, FTA)
23 * A driver for zl10038 should be very similar.
26 #include <linux/module.h>
27 #include <linux/dvb/frontend.h>
28 #include <linux/slab.h>
29 #include <linux/types.h>
33 static int zl10036_debug
;
34 #define dprintk(level, args...) \
35 do { if (zl10036_debug & level) printk(KERN_DEBUG "zl10036: " args); \
38 #define deb_info(args...) dprintk(0x01, args)
39 #define deb_i2c(args...) dprintk(0x02, args)
41 struct zl10036_state
{
42 struct i2c_adapter
*i2c
;
43 const struct zl10036_config
*config
;
49 /* This driver assumes the tuner is driven by a 10.111MHz Cristal */
52 /* Some of the possible dividers:
53 * 64, (write 0x05 to reg), freq step size 158kHz
54 * 10, (write 0x0a to reg), freq step size 1.011kHz (used here)
55 * 5, (write 0x09 to reg), freq step size 2.022kHz
59 #define _RDIV_REG 0x0a
60 #define _FR (_XTAL/_RDIV)
62 #define STATUS_POR 0x80 /* Power on Reset */
63 #define STATUS_FL 0x40 /* Frequency & Phase Lock */
65 /* read/write for zl10036 and zl10038 */
67 static int zl10036_read_status_reg(struct zl10036_state
*state
)
70 struct i2c_msg msg
[1] = {
71 { .addr
= state
->config
->tuner_address
, .flags
= I2C_M_RD
,
72 .buf
= &status
, .len
= sizeof(status
) },
75 if (i2c_transfer(state
->i2c
, msg
, 1) != 1) {
76 printk(KERN_ERR
"%s: i2c read failed at addr=%02x\n",
77 __func__
, state
->config
->tuner_address
);
81 deb_i2c("R(status): %02x [FL=%d]\n", status
,
82 (status
& STATUS_FL
) ? 1 : 0);
83 if (status
& STATUS_POR
)
84 deb_info("%s: Power-On-Reset bit enabled - need to initialize the tuner\n",
90 static int zl10036_write(struct zl10036_state
*state
, u8 buf
[], u8 count
)
92 struct i2c_msg msg
[1] = {
93 { .addr
= state
->config
->tuner_address
, .flags
= 0,
94 .buf
= buf
, .len
= count
},
99 if (zl10036_debug
& 0x02) {
100 /* every 8bit-value satisifes this!
101 * so only check for debug log */
102 if ((buf
[0] & 0x80) == 0x00)
104 else if ((buf
[0] & 0xc0) == 0x80)
106 else if ((buf
[0] & 0xf0) == 0xc0)
108 else if ((buf
[0] & 0xf0) == 0xd0)
110 else if ((buf
[0] & 0xf0) == 0xe0)
112 else if ((buf
[0] & 0xf0) == 0xf0)
115 deb_i2c("W(%d):", reg
);
118 for (i
= 0; i
< count
; i
++)
119 printk(KERN_CONT
" %02x", buf
[i
]);
120 printk(KERN_CONT
"\n");
124 ret
= i2c_transfer(state
->i2c
, msg
, 1);
126 printk(KERN_ERR
"%s: i2c error, ret=%d\n", __func__
, ret
);
133 static void zl10036_release(struct dvb_frontend
*fe
)
135 struct zl10036_state
*state
= fe
->tuner_priv
;
137 fe
->tuner_priv
= NULL
;
141 static int zl10036_sleep(struct dvb_frontend
*fe
)
143 struct zl10036_state
*state
= fe
->tuner_priv
;
144 u8 buf
[] = { 0xf0, 0x80 }; /* regs 12/13 */
147 deb_info("%s\n", __func__
);
149 if (fe
->ops
.i2c_gate_ctrl
)
150 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open i2c_gate */
152 ret
= zl10036_write(state
, buf
, sizeof(buf
));
154 if (fe
->ops
.i2c_gate_ctrl
)
155 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close i2c_gate */
161 * register map of the ZL10036/ZL10038
163 * reg[default] content
164 * 2[0x00]: 0 | N14 | N13 | N12 | N11 | N10 | N9 | N8
165 * 3[0x00]: N7 | N6 | N5 | N4 | N3 | N2 | N1 | N0
166 * 4[0x80]: 1 | 0 | RFG | BA1 | BA0 | BG1 | BG0 | LEN
167 * 5[0x00]: P0 | C1 | C0 | R4 | R3 | R2 | R1 | R0
168 * 6[0xc0]: 1 | 1 | 0 | 0 | RSD | 0 | 0 | 0
169 * 7[0x20]: P1 | BF6 | BF5 | BF4 | BF3 | BF2 | BF1 | 0
170 * 8[0xdb]: 1 | 1 | 0 | 1 | 0 | CC | 1 | 1
171 * 9[0x30]: VSD | V2 | V1 | V0 | S3 | S2 | S1 | S0
172 * 10[0xe1]: 1 | 1 | 1 | 0 | 0 | LS2 | LS1 | LS0
173 * 11[0xf5]: WS | WH2 | WH1 | WH0 | WL2 | WL1 | WL0 | WRE
174 * 12[0xf0]: 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0
175 * 13[0x28]: PD | BR4 | BR3 | BR2 | BR1 | BR0 | CLR | TL
178 static int zl10036_set_frequency(struct zl10036_state
*state
, u32 frequency
)
183 div
= (frequency
+ _FR
/2) / _FR
;
184 state
->frequency
= div
* _FR
;
186 foffset
= frequency
- state
->frequency
;
188 buf
[0] = (div
>> 8) & 0x7f;
189 buf
[1] = (div
>> 0) & 0xff;
191 deb_info("%s: ftodo=%u fpriv=%u ferr=%d div=%u\n", __func__
,
192 frequency
, state
->frequency
, foffset
, div
);
194 return zl10036_write(state
, buf
, sizeof(buf
));
197 static int zl10036_set_bandwidth(struct zl10036_state
*state
, u32 fbw
)
199 /* fbw is measured in kHz */
203 0xc0, 0x00, /* 6/7: rsd=0 bf=0 */
206 0xf0, 0x00, /* 12/13: br=0xa clr=0 tl=0*/
208 u8 zl10036_rsd_off
[] = { 0xc8 }; /* set RSD=1 */
210 /* ensure correct values */
216 #define _BR_MAXIMUM (_XTAL/575) /* _XTAL / 575kHz = 17 */
223 * f(bw)=34,6MHz f(xtal)=10.111MHz
224 * br = (10111/34600) * 63 * 1/K = 14;
226 br
= ((_XTAL
* 21 * 1000) / (fbw
* 419));
229 /* ensure correct values */
232 if (br
> _BR_MAXIMUM
)
237 * bf = fbw/_XTAL * br * k - 1 */
239 bf
= (fbw
* br
* 1257) / (_XTAL
* 1000) - 1;
241 /* ensure correct values */
245 buf_bf
[1] = (bf
<< 1) & 0x7e;
246 buf_br
[1] = (br
<< 2) & 0x7c;
247 deb_info("%s: BW=%d br=%u bf=%u\n", __func__
, fbw
, br
, bf
);
249 if (br
!= state
->br
) {
250 ret
= zl10036_write(state
, buf_br
, sizeof(buf_br
));
255 if (bf
!= state
->bf
) {
256 ret
= zl10036_write(state
, buf_bf
, sizeof(buf_bf
));
260 /* time = br/(32* fxtal) */
261 /* minimal sleep time to be calculated
262 * maximum br is 63 -> max time = 2 /10 MHz = 2e-7 */
265 ret
= zl10036_write(state
, zl10036_rsd_off
,
266 sizeof(zl10036_rsd_off
));
277 static int zl10036_set_gain_params(struct zl10036_state
*state
,
284 rfg
= 0; /* enable when using an lna */
289 buf
[0] = 0x80 | ((rfg
<< 5) & 0x20)
290 | ((ba
<< 3) & 0x18) | ((bg
<< 1) & 0x06);
292 if (!state
->config
->rf_loop_enable
)
296 buf
[1] = _RDIV_REG
| ((c
<< 5) & 0x60);
298 deb_info("%s: c=%u rfg=%u ba=%u bg=%u\n", __func__
, c
, rfg
, ba
, bg
);
299 return zl10036_write(state
, buf
, sizeof(buf
));
302 static int zl10036_set_params(struct dvb_frontend
*fe
)
304 struct dtv_frontend_properties
*p
= &fe
->dtv_property_cache
;
305 struct zl10036_state
*state
= fe
->tuner_priv
;
307 u32 frequency
= p
->frequency
;
312 /* ensure correct values
313 * maybe redundant as core already checks this */
314 if ((frequency
< fe
->ops
.info
.frequency_min
)
315 || (frequency
> fe
->ops
.info
.frequency_max
))
319 * alpha = 1.35 for dvb-s
320 * fBW = (alpha*symbolrate)/(2*0.8)
321 * 1.35 / (2*0.8) = 27 / 32
323 fbw
= (27 * p
->symbol_rate
) / 32;
328 /* Add safe margin of 3MHz */
331 /* setting the charge pump - guessed values */
332 if (frequency
< 950000)
334 else if (frequency
< 1250000)
336 else if (frequency
< 1750000)
338 else if (frequency
< 2175000)
343 if (fe
->ops
.i2c_gate_ctrl
)
344 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open i2c_gate */
346 ret
= zl10036_set_gain_params(state
, c
);
350 ret
= zl10036_set_frequency(state
, p
->frequency
);
354 ret
= zl10036_set_bandwidth(state
, fbw
);
358 /* wait for tuner lock - no idea if this is really needed */
359 for (i
= 0; i
< 20; i
++) {
360 ret
= zl10036_read_status_reg(state
);
364 /* check Frequency & Phase Lock Bit */
372 if (fe
->ops
.i2c_gate_ctrl
)
373 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close i2c_gate */
378 static int zl10036_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
380 struct zl10036_state
*state
= fe
->tuner_priv
;
382 *frequency
= state
->frequency
;
387 static int zl10036_init_regs(struct zl10036_state
*state
)
392 /* could also be one block from reg 2 to 13 and additional 10/11 */
393 u8 zl10036_init_tab
[][2] = {
394 { 0x04, 0x00 }, /* 2/3: div=0x400 - arbitrary value */
395 { 0x8b, _RDIV_REG
}, /* 4/5: rfg=0 ba=1 bg=1 len=? */
396 /* p0=0 c=0 r=_RDIV_REG */
397 { 0xc0, 0x20 }, /* 6/7: rsd=0 bf=0x10 */
398 { 0xd3, 0x40 }, /* 8/9: from datasheet */
399 { 0xe3, 0x5b }, /* 10/11: lock window level */
400 { 0xf0, 0x28 }, /* 12/13: br=0xa clr=0 tl=0*/
401 { 0xe3, 0xf9 }, /* 10/11: unlock window level */
404 /* invalid values to trigger writing */
408 if (!state
->config
->rf_loop_enable
)
409 zl10036_init_tab
[1][0] |= 0x01;
411 deb_info("%s\n", __func__
);
413 for (i
= 0; i
< ARRAY_SIZE(zl10036_init_tab
); i
++) {
414 ret
= zl10036_write(state
, zl10036_init_tab
[i
], 2);
422 static int zl10036_init(struct dvb_frontend
*fe
)
424 struct zl10036_state
*state
= fe
->tuner_priv
;
427 if (fe
->ops
.i2c_gate_ctrl
)
428 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open i2c_gate */
430 ret
= zl10036_read_status_reg(state
);
434 /* Only init if Power-on-Reset bit is set? */
435 ret
= zl10036_init_regs(state
);
437 if (fe
->ops
.i2c_gate_ctrl
)
438 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close i2c_gate */
443 static const struct dvb_tuner_ops zl10036_tuner_ops
= {
445 .name
= "Zarlink ZL10036",
446 .frequency_min
= 950000,
447 .frequency_max
= 2175000
449 .init
= zl10036_init
,
450 .release
= zl10036_release
,
451 .sleep
= zl10036_sleep
,
452 .set_params
= zl10036_set_params
,
453 .get_frequency
= zl10036_get_frequency
,
456 struct dvb_frontend
*zl10036_attach(struct dvb_frontend
*fe
,
457 const struct zl10036_config
*config
,
458 struct i2c_adapter
*i2c
)
460 struct zl10036_state
*state
;
464 printk(KERN_ERR
"%s: no config specified", __func__
);
468 state
= kzalloc(sizeof(struct zl10036_state
), GFP_KERNEL
);
472 state
->config
= config
;
475 if (fe
->ops
.i2c_gate_ctrl
)
476 fe
->ops
.i2c_gate_ctrl(fe
, 1); /* open i2c_gate */
478 ret
= zl10036_read_status_reg(state
);
480 printk(KERN_ERR
"%s: No zl10036 found\n", __func__
);
484 ret
= zl10036_init_regs(state
);
486 printk(KERN_ERR
"%s: tuner initialization failed\n",
491 if (fe
->ops
.i2c_gate_ctrl
)
492 fe
->ops
.i2c_gate_ctrl(fe
, 0); /* close i2c_gate */
494 fe
->tuner_priv
= state
;
496 memcpy(&fe
->ops
.tuner_ops
, &zl10036_tuner_ops
,
497 sizeof(struct dvb_tuner_ops
));
498 printk(KERN_INFO
"%s: tuner initialization (%s addr=0x%02x) ok\n",
499 __func__
, fe
->ops
.tuner_ops
.info
.name
, config
->tuner_address
);
507 EXPORT_SYMBOL(zl10036_attach
);
509 module_param_named(debug
, zl10036_debug
, int, 0644);
510 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
511 MODULE_DESCRIPTION("DVB ZL10036 driver");
512 MODULE_AUTHOR("Tino Reichardt");
513 MODULE_AUTHOR("Matthias Schwarzott");
514 MODULE_LICENSE("GPL");