2 * Driver for the Integrant ITD1000 "Zero-IF Tuner IC for Direct Broadcast Satellite"
4 * Copyright (c) 2007-8 Patrick Boettcher <pb@linuxtv.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.=
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/delay.h>
25 #include <linux/dvb/frontend.h>
26 #include <linux/i2c.h>
27 #include <linux/slab.h>
29 #include "dvb_frontend.h"
32 #include "itd1000_priv.h"
34 /* Max transfer size done by I2C transfer functions */
35 #define MAX_XFER_SIZE 64
38 module_param(debug
, int, 0644);
39 MODULE_PARM_DESC(debug
, "Turn on/off debugging (default:off).");
41 #define itd_dbg(args...) do { \
43 printk(KERN_DEBUG "ITD1000: " args);\
47 #define itd_warn(args...) do { \
48 printk(KERN_WARNING "ITD1000: " args); \
51 #define itd_info(args...) do { \
52 printk(KERN_INFO "ITD1000: " args); \
55 /* don't write more than one byte with flexcop behind */
56 static int itd1000_write_regs(struct itd1000_state
*state
, u8 reg
, u8 v
[], u8 len
)
58 u8 buf
[MAX_XFER_SIZE
];
59 struct i2c_msg msg
= {
60 .addr
= state
->cfg
->i2c_address
, .flags
= 0, .buf
= buf
, .len
= len
+1
63 if (1 + len
> sizeof(buf
)) {
65 "itd1000: i2c wr reg=%04x: len=%d is too big!\n",
71 memcpy(&buf
[1], v
, len
);
73 /* itd_dbg("wr %02x: %02x\n", reg, v[0]); */
75 if (i2c_transfer(state
->i2c
, &msg
, 1) != 1) {
76 printk(KERN_WARNING
"itd1000 I2C write failed\n");
82 static int itd1000_read_reg(struct itd1000_state
*state
, u8 reg
)
85 struct i2c_msg msg
[2] = {
86 { .addr
= state
->cfg
->i2c_address
, .flags
= 0, .buf
= ®
, .len
= 1 },
87 { .addr
= state
->cfg
->i2c_address
, .flags
= I2C_M_RD
, .buf
= &val
, .len
= 1 },
90 /* ugly flexcop workaround */
91 itd1000_write_regs(state
, (reg
- 1) & 0xff, &state
->shadow
[(reg
- 1) & 0xff], 1);
93 if (i2c_transfer(state
->i2c
, msg
, 2) != 2) {
94 itd_warn("itd1000 I2C read failed\n");
100 static inline int itd1000_write_reg(struct itd1000_state
*state
, u8 r
, u8 v
)
102 int ret
= itd1000_write_regs(state
, r
, &v
, 1);
103 state
->shadow
[r
] = v
;
110 u8 pgaext
: 4; /* PLLFH */
111 u8 bbgvmin
: 4; /* BBGVMIN */
112 } itd1000_lpf_pga
[] = {
114 { 5200000, 0x8, 0x3 },
115 { 12200000, 0x4, 0x3 },
116 { 15400000, 0x2, 0x3 },
117 { 19800000, 0x2, 0x3 },
118 { 21500000, 0x2, 0x3 },
119 { 24500000, 0x2, 0x3 },
120 { 28400000, 0x2, 0x3 },
121 { 33400000, 0x2, 0x3 },
122 { 34400000, 0x1, 0x4 },
123 { 34400000, 0x1, 0x4 },
124 { 38400000, 0x1, 0x4 },
125 { 38400000, 0x1, 0x4 },
126 { 40400000, 0x1, 0x4 },
127 { 45400000, 0x1, 0x4 },
130 static void itd1000_set_lpf_bw(struct itd1000_state
*state
, u32 symbol_rate
)
133 u8 con1
= itd1000_read_reg(state
, CON1
) & 0xfd;
134 u8 pllfh
= itd1000_read_reg(state
, PLLFH
) & 0x0f;
135 u8 bbgvmin
= itd1000_read_reg(state
, BBGVMIN
) & 0xf0;
136 u8 bw
= itd1000_read_reg(state
, BW
) & 0xf0;
138 itd_dbg("symbol_rate = %d\n", symbol_rate
);
140 /* not sure what is that ? - starting to download the table */
141 itd1000_write_reg(state
, CON1
, con1
| (1 << 1));
143 for (i
= 0; i
< ARRAY_SIZE(itd1000_lpf_pga
); i
++)
144 if (symbol_rate
< itd1000_lpf_pga
[i
].symbol_rate
) {
145 itd_dbg("symrate: index: %d pgaext: %x, bbgvmin: %x\n", i
, itd1000_lpf_pga
[i
].pgaext
, itd1000_lpf_pga
[i
].bbgvmin
);
146 itd1000_write_reg(state
, PLLFH
, pllfh
| (itd1000_lpf_pga
[i
].pgaext
<< 4));
147 itd1000_write_reg(state
, BBGVMIN
, bbgvmin
| (itd1000_lpf_pga
[i
].bbgvmin
));
148 itd1000_write_reg(state
, BW
, bw
| (i
& 0x0f));
152 itd1000_write_reg(state
, CON1
, con1
| (0 << 1));
158 } itd1000_vcorg
[] = {
166 { 8, 500000 }, /* this is intentional. */
176 static void itd1000_set_vco(struct itd1000_state
*state
, u32 freq_khz
)
179 u8 gvbb_i2c
= itd1000_read_reg(state
, GVBB_I2C
) & 0xbf;
180 u8 vco_chp1_i2c
= itd1000_read_reg(state
, VCO_CHP1_I2C
) & 0x0f;
183 /* reserved bit again (reset ?) */
184 itd1000_write_reg(state
, GVBB_I2C
, gvbb_i2c
| (1 << 6));
186 for (i
= 0; i
< ARRAY_SIZE(itd1000_vcorg
); i
++) {
187 if (freq_khz
< itd1000_vcorg
[i
].fmax_rg
) {
188 itd1000_write_reg(state
, VCO_CHP1_I2C
, vco_chp1_i2c
| (itd1000_vcorg
[i
].vcorg
<< 4));
191 adcout
= itd1000_read_reg(state
, PLLLOCK
) & 0x0f;
193 itd_dbg("VCO: %dkHz: %d -> ADCOUT: %d %02x\n", freq_khz
, itd1000_vcorg
[i
].vcorg
, adcout
, vco_chp1_i2c
);
196 if (!(itd1000_vcorg
[i
].vcorg
== 7 || itd1000_vcorg
[i
].vcorg
== 15))
197 itd1000_write_reg(state
, VCO_CHP1_I2C
, vco_chp1_i2c
| ((itd1000_vcorg
[i
].vcorg
+ 1) << 4));
198 } else if (adcout
< 2) {
199 if (!(itd1000_vcorg
[i
].vcorg
== 1 || itd1000_vcorg
[i
].vcorg
== 9))
200 itd1000_write_reg(state
, VCO_CHP1_I2C
, vco_chp1_i2c
| ((itd1000_vcorg
[i
].vcorg
- 1) << 4));
207 static const struct {
209 u8 values
[10]; /* RFTR, RFST1 - RFST9 */
210 } itd1000_fre_values
[] = {
211 { 1075000, { 0x59, 0x1d, 0x1c, 0x17, 0x16, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
212 { 1250000, { 0x89, 0x1e, 0x1d, 0x17, 0x15, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
213 { 1450000, { 0x89, 0x1e, 0x1d, 0x17, 0x15, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
214 { 1650000, { 0x69, 0x1e, 0x1d, 0x17, 0x15, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
215 { 1750000, { 0x69, 0x1e, 0x17, 0x15, 0x14, 0x0f, 0x0e, 0x0c, 0x0b, 0x0a } },
216 { 1850000, { 0x69, 0x1d, 0x17, 0x16, 0x14, 0x0f, 0x0e, 0x0d, 0x0b, 0x0a } },
217 { 1900000, { 0x69, 0x1d, 0x17, 0x15, 0x14, 0x0f, 0x0e, 0x0d, 0x0b, 0x0a } },
218 { 1950000, { 0x69, 0x1d, 0x17, 0x16, 0x14, 0x13, 0x0e, 0x0d, 0x0b, 0x0a } },
219 { 2050000, { 0x69, 0x1e, 0x1d, 0x17, 0x16, 0x14, 0x13, 0x0e, 0x0b, 0x0a } },
220 { 2150000, { 0x69, 0x1d, 0x1c, 0x17, 0x15, 0x14, 0x13, 0x0f, 0x0e, 0x0b } }
226 static void itd1000_set_lo(struct itd1000_state
*state
, u32 freq_khz
)
232 plln
= (freq_khz
* 1000) / 2 / FREF
;
234 /* Compute the factional part times 1000 */
235 tmp
= plln
% 1000000;
239 do_div(tmp
, 1000000);
242 state
->frequency
= ((plln
* 1000) + (pllf
* 1000)/1048576) * 2*FREF
;
243 itd_dbg("frequency: %dkHz (wanted) %dkHz (set), PLLF = %d, PLLN = %d\n", freq_khz
, state
->frequency
, pllf
, plln
);
245 itd1000_write_reg(state
, PLLNH
, 0x80); /* PLLNH */
246 itd1000_write_reg(state
, PLLNL
, plln
& 0xff);
247 itd1000_write_reg(state
, PLLFH
, (itd1000_read_reg(state
, PLLFH
) & 0xf0) | ((pllf
>> 16) & 0x0f));
248 itd1000_write_reg(state
, PLLFM
, (pllf
>> 8) & 0xff);
249 itd1000_write_reg(state
, PLLFL
, (pllf
>> 0) & 0xff);
251 for (i
= 0; i
< ARRAY_SIZE(itd1000_fre_values
); i
++) {
252 if (freq_khz
<= itd1000_fre_values
[i
].freq
) {
253 itd_dbg("fre_values: %d\n", i
);
254 itd1000_write_reg(state
, RFTR
, itd1000_fre_values
[i
].values
[0]);
255 for (j
= 0; j
< 9; j
++)
256 itd1000_write_reg(state
, RFST1
+j
, itd1000_fre_values
[i
].values
[j
+1]);
261 itd1000_set_vco(state
, freq_khz
);
264 static int itd1000_set_parameters(struct dvb_frontend
*fe
)
266 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
267 struct itd1000_state
*state
= fe
->tuner_priv
;
270 itd1000_set_lo(state
, c
->frequency
);
271 itd1000_set_lpf_bw(state
, c
->symbol_rate
);
273 pllcon1
= itd1000_read_reg(state
, PLLCON1
) & 0x7f;
274 itd1000_write_reg(state
, PLLCON1
, pllcon1
| (1 << 7));
275 itd1000_write_reg(state
, PLLCON1
, pllcon1
);
280 static int itd1000_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
282 struct itd1000_state
*state
= fe
->tuner_priv
;
283 *frequency
= state
->frequency
;
287 static int itd1000_get_bandwidth(struct dvb_frontend
*fe
, u32
*bandwidth
)
292 static u8 itd1000_init_tab
[][2] = {
293 { PLLCON1
, 0x65 }, /* Register does not change */
294 { PLLNH
, 0x80 }, /* Bits [7:6] do not change */
295 { RESERVED_0X6D
, 0x3b },
296 { VCO_CHP2_I2C
, 0x12 },
297 { 0x72, 0xf9 }, /* No such regsister defined */
298 { RESERVED_0X73
, 0xff },
299 { RESERVED_0X74
, 0xb2 },
300 { RESERVED_0X75
, 0xc7 },
304 { RESERVED_0X7E
, 0x4f },
305 { 0x82, 0x88 }, /* No such regsister defined */
306 { 0x83, 0x80 }, /* No such regsister defined */
307 { 0x84, 0x80 }, /* No such regsister defined */
308 { RESERVED_0X85
, 0x74 },
309 { RESERVED_0X86
, 0xff },
310 { RESERVED_0X88
, 0x02 },
311 { RESERVED_0X89
, 0x16 },
313 { RESERVED_0X94
, 0x66 },
314 { RESERVED_0X95
, 0x66 },
315 { RESERVED_0X96
, 0x77 },
316 { RESERVED_0X97
, 0x99 },
317 { RESERVED_0X98
, 0xff },
318 { RESERVED_0X99
, 0xfc },
319 { RESERVED_0X9A
, 0xba },
320 { RESERVED_0X9B
, 0xaa },
323 static u8 itd1000_reinit_tab
[][2] = {
324 { VCO_CHP1_I2C
, 0x8a },
332 static int itd1000_init(struct dvb_frontend
*fe
)
334 struct itd1000_state
*state
= fe
->tuner_priv
;
337 for (i
= 0; i
< ARRAY_SIZE(itd1000_init_tab
); i
++)
338 itd1000_write_reg(state
, itd1000_init_tab
[i
][0], itd1000_init_tab
[i
][1]);
340 for (i
= 0; i
< ARRAY_SIZE(itd1000_reinit_tab
); i
++)
341 itd1000_write_reg(state
, itd1000_reinit_tab
[i
][0], itd1000_reinit_tab
[i
][1]);
346 static int itd1000_sleep(struct dvb_frontend
*fe
)
351 static void itd1000_release(struct dvb_frontend
*fe
)
353 kfree(fe
->tuner_priv
);
354 fe
->tuner_priv
= NULL
;
357 static const struct dvb_tuner_ops itd1000_tuner_ops
= {
359 .name
= "Integrant ITD1000",
360 .frequency_min
= 950000,
361 .frequency_max
= 2150000,
362 .frequency_step
= 125, /* kHz for QPSK frontends */
365 .release
= itd1000_release
,
367 .init
= itd1000_init
,
368 .sleep
= itd1000_sleep
,
370 .set_params
= itd1000_set_parameters
,
371 .get_frequency
= itd1000_get_frequency
,
372 .get_bandwidth
= itd1000_get_bandwidth
376 struct dvb_frontend
*itd1000_attach(struct dvb_frontend
*fe
, struct i2c_adapter
*i2c
, struct itd1000_config
*cfg
)
378 struct itd1000_state
*state
= NULL
;
381 state
= kzalloc(sizeof(struct itd1000_state
), GFP_KERNEL
);
388 i
= itd1000_read_reg(state
, 0);
393 itd_info("successfully identified (ID: %d)\n", i
);
395 memset(state
->shadow
, 0xff, sizeof(state
->shadow
));
396 for (i
= 0x65; i
< 0x9c; i
++)
397 state
->shadow
[i
] = itd1000_read_reg(state
, i
);
399 memcpy(&fe
->ops
.tuner_ops
, &itd1000_tuner_ops
, sizeof(struct dvb_tuner_ops
));
401 fe
->tuner_priv
= state
;
405 EXPORT_SYMBOL(itd1000_attach
);
407 MODULE_AUTHOR("Patrick Boettcher <pb@linuxtv.org>");
408 MODULE_DESCRIPTION("Integrant ITD1000 driver");
409 MODULE_LICENSE("GPL");