1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for Conexant CX24113/CX24128 Tuner (Satellite)
5 * Copyright (C) 2007-8 Patrick Boettcher <pb@linuxtv.org>
7 * Developed for BBTI / Technisat
10 #include <linux/slab.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
15 #include <media/dvb_frontend.h>
20 #define cx_info(args...) do { printk(KERN_INFO "CX24113: " args); } while (0)
21 #define cx_err(args...) do { printk(KERN_ERR "CX24113: " args); } while (0)
23 #define dprintk(args...) \
26 printk(KERN_DEBUG "CX24113: %s: ", __func__); \
31 struct cx24113_state
{
32 struct i2c_adapter
*i2c
;
33 const struct cx24113_config
*config
;
35 #define REV_CX24113 0x23
52 #define LNA_MIN_GAIN 0
53 #define LNA_MID_GAIN 1
54 #define LNA_MAX_GAIN 2
61 #define VCOBANDSEL_6 0x80
62 #define VCOBANDSEL_5 0x01
63 #define VCOBANDSEL_4 0x02
64 #define VCOBANDSEL_3 0x04
65 #define VCOBANDSEL_2 0x08
66 #define VCOBANDSEL_1 0x10
90 static int cx24113_writereg(struct cx24113_state
*state
, int reg
, int data
)
92 u8 buf
[] = { reg
, data
};
93 struct i2c_msg msg
= { .addr
= state
->config
->i2c_addr
,
94 .flags
= 0, .buf
= buf
, .len
= 2 };
95 int err
= i2c_transfer(state
->i2c
, &msg
, 1);
97 printk(KERN_DEBUG
"%s: writereg error(err == %i, reg == 0x%02x, data == 0x%02x)\n",
98 __func__
, err
, reg
, data
);
105 static int cx24113_readreg(struct cx24113_state
*state
, u8 reg
)
109 struct i2c_msg msg
[] = {
110 { .addr
= state
->config
->i2c_addr
,
111 .flags
= 0, .buf
= ®
, .len
= 1 },
112 { .addr
= state
->config
->i2c_addr
,
113 .flags
= I2C_M_RD
, .buf
= &b
, .len
= 1 }
116 ret
= i2c_transfer(state
->i2c
, msg
, 2);
119 printk(KERN_DEBUG
"%s: reg=0x%x (error=%d)\n",
127 static void cx24113_set_parameters(struct cx24113_state
*state
)
131 r
= cx24113_readreg(state
, 0x10) & 0x82;
132 r
|= state
->icp_mode
;
133 r
|= state
->icp_man
<< 4;
134 r
|= state
->icp_dig
<< 2;
135 r
|= state
->prescaler_mode
<< 5;
136 cx24113_writereg(state
, 0x10, r
);
138 r
= (state
->icp_auto_low
<< 0) | (state
->icp_auto_mlow
<< 2)
139 | (state
->icp_auto_mhi
<< 4) | (state
->icp_auto_hi
<< 6);
140 cx24113_writereg(state
, 0x11, r
);
142 if (state
->rev
== REV_CX24113
) {
143 r
= cx24113_readreg(state
, 0x20) & 0xec;
144 r
|= state
->lna_gain
;
145 r
|= state
->rfvga_bias_ctrl
<< 4;
146 cx24113_writereg(state
, 0x20, r
);
149 r
= cx24113_readreg(state
, 0x12) & 0x03;
150 r
|= state
->acp_on
<< 2;
151 r
|= state
->bs_delay
<< 4;
152 cx24113_writereg(state
, 0x12, r
);
154 r
= cx24113_readreg(state
, 0x18) & 0x40;
155 r
|= state
->vco_shift
;
156 if (state
->vco_band
== VCOBANDSEL_6
)
159 r
|= (state
->vco_band
<< 1);
160 cx24113_writereg(state
, 0x18, r
);
162 r
= cx24113_readreg(state
, 0x14) & 0x20;
163 r
|= (state
->vco_mode
<< 6) | ((state
->bs_freqcnt
>> 8) & 0x1f);
164 cx24113_writereg(state
, 0x14, r
);
165 cx24113_writereg(state
, 0x15, (state
->bs_freqcnt
& 0xff));
167 cx24113_writereg(state
, 0x16, (state
->bs_rdiv
>> 4) & 0xff);
168 r
= (cx24113_readreg(state
, 0x17) & 0x0f) |
169 ((state
->bs_rdiv
& 0x0f) << 4);
170 cx24113_writereg(state
, 0x17, r
);
187 static int cx24113_set_gain_settings(struct cx24113_state
*state
,
188 s16 power_estimation
)
190 u8 ampout
= cx24113_readreg(state
, 0x1d) & 0xf0,
191 vga
= cx24113_readreg(state
, 0x1f) & 0x3f,
192 rfvga
= cx24113_readreg(state
, 0x20) & 0xf3;
193 u8 gain_level
= power_estimation
>= state
->tuner_gain_thres
;
195 dprintk("power estimation: %d, thres: %d, gain_level: %d/%d\n",
196 power_estimation
, state
->tuner_gain_thres
,
197 state
->gain_level
, gain_level
);
199 if (gain_level
== state
->gain_level
)
200 return 0; /* nothing to be done */
205 rfvga
|= RFVGA_0
<< 2;
206 vga
|= (VGA_7
<< 3) | VGA_7
;
208 rfvga
|= RFVGA_2
<< 2;
209 vga
|= (VGA_6
<< 3) | VGA_2
;
211 state
->gain_level
= gain_level
;
213 cx24113_writereg(state
, 0x1d, ampout
);
214 cx24113_writereg(state
, 0x1f, vga
);
215 cx24113_writereg(state
, 0x20, rfvga
);
217 return 1; /* did something */
220 static int cx24113_set_Fref(struct cx24113_state
*state
, u8 high
)
222 u8 xtal
= cx24113_readreg(state
, 0x02);
223 if (state
->rev
== 0x43 && state
->vcodiv
== VCODIV4
)
229 return cx24113_writereg(state
, 0x02, xtal
);
232 static int cx24113_enable(struct cx24113_state
*state
, u8 enable
)
234 u8 r21
= (cx24113_readreg(state
, 0x21) & 0xc0) | enable
;
235 if (state
->rev
== REV_CX24113
)
237 return cx24113_writereg(state
, 0x21, r21
);
240 static int cx24113_set_bandwidth(struct cx24113_state
*state
, u32 bandwidth_khz
)
244 if (bandwidth_khz
<= 19000)
246 else if (bandwidth_khz
<= 25000)
251 dprintk("bandwidth to be set: %d\n", bandwidth_khz
);
253 bandwidth_khz
-= 10000;
254 bandwidth_khz
/= 1000;
258 dprintk("bandwidth: %d %d\n", r
>> 6, bandwidth_khz
);
260 r
|= bandwidth_khz
& 0x3f;
262 return cx24113_writereg(state
, 0x1e, r
);
265 static int cx24113_set_clk_inversion(struct cx24113_state
*state
, u8 on
)
267 u8 r
= (cx24113_readreg(state
, 0x10) & 0x7f) | ((on
& 0x1) << 7);
268 return cx24113_writereg(state
, 0x10, r
);
271 static int cx24113_get_status(struct dvb_frontend
*fe
, u32
*status
)
273 struct cx24113_state
*state
= fe
->tuner_priv
;
274 u8 r
= (cx24113_readreg(state
, 0x10) & 0x02) >> 1;
276 *status
|= TUNER_STATUS_LOCKED
;
277 dprintk("PLL locked: %d\n", r
);
281 static u8
cx24113_set_ref_div(struct cx24113_state
*state
, u8 refdiv
)
283 if (state
->rev
== 0x43 && state
->vcodiv
== VCODIV4
)
285 return state
->refdiv
= refdiv
;
288 static void cx24113_calc_pll_nf(struct cx24113_state
*state
, u16
*n
, s32
*f
)
296 s32 freq_hz
= state
->frequency
* 1000;
298 if (state
->config
->xtal_khz
< 20000)
303 if (state
->rev
== REV_CX24113
) {
304 if (state
->frequency
>= 1100000)
309 if (state
->frequency
>= 1165000)
314 state
->vcodiv
= vcodiv
;
316 dprintk("calculating N/F for %dHz with vcodiv %d\n", freq_hz
, vcodiv
);
319 R
= cx24113_set_ref_div(state
, R
+ 1);
321 /* calculate tuner PLL settings: */
322 N
= (freq_hz
/ 100 * vcodiv
) * R
;
323 N
/= (state
->config
->xtal_khz
) * factor
* 2;
324 N
+= 5; /* For round up. */
327 } while (N
< 6 && R
< 3);
330 cx_err("strange frequency: N < 6\n");
334 F
*= (u64
) (R
* vcodiv
* 262144);
335 dprintk("1 N: %d, F: %lld, R: %d\n", N
, (long long)F
, R
);
336 /* do_div needs an u64 as first argument */
338 do_div(dividend
, state
->config
->xtal_khz
* 1000 * factor
* 2);
340 dprintk("2 N: %d, F: %lld, R: %d\n", N
, (long long)F
, R
);
341 F
-= (N
+ 32) * 262144;
343 dprintk("3 N: %d, F: %lld, R: %d\n", N
, (long long)F
, R
);
345 if (state
->Fwindow_enabled
) {
346 if (F
> (262144 / 2 - 1638))
347 F
= 262144 / 2 - 1638;
348 if (F
< (-262144 / 2 + 1638))
349 F
= -262144 / 2 + 1638;
350 if ((F
< 3277 && F
> 0) || (F
> -3277 && F
< 0)) {
352 r
= cx24113_readreg(state
, 0x10);
353 cx24113_writereg(state
, 0x10, r
| (1 << 6));
356 dprintk("4 N: %d, F: %lld, R: %d\n", N
, (long long)F
, R
);
363 static void cx24113_set_nfr(struct cx24113_state
*state
, u16 n
, s32 f
, u8 r
)
366 cx24113_writereg(state
, 0x19, (n
>> 1) & 0xff);
368 reg
= ((n
& 0x1) << 7) | ((f
>> 11) & 0x7f);
369 cx24113_writereg(state
, 0x1a, reg
);
371 cx24113_writereg(state
, 0x1b, (f
>> 3) & 0xff);
373 reg
= cx24113_readreg(state
, 0x1c) & 0x1f;
374 cx24113_writereg(state
, 0x1c, reg
| ((f
& 0x7) << 5));
376 cx24113_set_Fref(state
, r
- 1);
379 static int cx24113_set_frequency(struct cx24113_state
*state
, u32 frequency
)
385 r
= cx24113_readreg(state
, 0x14);
386 cx24113_writereg(state
, 0x14, r
& 0x3f);
388 r
= cx24113_readreg(state
, 0x10);
389 cx24113_writereg(state
, 0x10, r
& 0xbf);
391 state
->frequency
= frequency
;
393 dprintk("tuning to frequency: %d\n", frequency
);
395 cx24113_calc_pll_nf(state
, &n
, &f
);
396 cx24113_set_nfr(state
, n
, f
, state
->refdiv
);
398 r
= cx24113_readreg(state
, 0x18) & 0xbf;
399 if (state
->vcodiv
!= VCODIV2
)
401 cx24113_writereg(state
, 0x18, r
);
403 /* The need for this sleep is not clear. But helps in some cases */
406 r
= cx24113_readreg(state
, 0x1c) & 0xef;
407 cx24113_writereg(state
, 0x1c, r
| (1 << 4));
411 static int cx24113_init(struct dvb_frontend
*fe
)
413 struct cx24113_state
*state
= fe
->tuner_priv
;
416 state
->tuner_gain_thres
= -50;
417 state
->gain_level
= 255; /* to force a gain-setting initialization */
420 if (state
->config
->xtal_khz
< 11000) {
421 state
->icp_auto_hi
= ICP_LEVEL4
;
422 state
->icp_auto_mhi
= ICP_LEVEL4
;
423 state
->icp_auto_mlow
= ICP_LEVEL3
;
424 state
->icp_auto_low
= ICP_LEVEL3
;
426 state
->icp_auto_hi
= ICP_LEVEL4
;
427 state
->icp_auto_mhi
= ICP_LEVEL4
;
428 state
->icp_auto_mlow
= ICP_LEVEL3
;
429 state
->icp_auto_low
= ICP_LEVEL2
;
432 state
->icp_dig
= ICP_LEVEL3
;
433 state
->icp_man
= ICP_LEVEL1
;
436 state
->vco_shift
= 0;
437 state
->vco_band
= VCOBANDSEL_1
;
439 state
->bs_freqcnt
= 0x0fff;
440 state
->bs_rdiv
= 0x0fff;
441 state
->prescaler_mode
= 0;
442 state
->lna_gain
= LNA_MAX_GAIN
;
443 state
->rfvga_bias_ctrl
= 1;
444 state
->Fwindow_enabled
= 1;
446 cx24113_set_Fref(state
, 0);
447 cx24113_enable(state
, 0x3d);
448 cx24113_set_parameters(state
);
450 cx24113_set_gain_settings(state
, -30);
452 cx24113_set_bandwidth(state
, 18025);
453 cx24113_set_clk_inversion(state
, 1);
455 if (state
->config
->xtal_khz
>= 40000)
456 ret
= cx24113_writereg(state
, 0x02,
457 (cx24113_readreg(state
, 0x02) & 0xfb) | (1 << 2));
459 ret
= cx24113_writereg(state
, 0x02,
460 (cx24113_readreg(state
, 0x02) & 0xfb) | (0 << 2));
465 static int cx24113_set_params(struct dvb_frontend
*fe
)
467 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
468 struct cx24113_state
*state
= fe
->tuner_priv
;
469 /* for a ROLL-OFF factor of 0.35, 0.2: 600, 0.25: 625 */
473 bw
= ((c
->symbol_rate
/100) * roll_off
) / 1000;
474 bw
+= (10000000/100) + 5;
477 cx24113_set_bandwidth(state
, bw
);
479 cx24113_set_frequency(state
, c
->frequency
);
481 return cx24113_get_status(fe
, &bw
);
484 static s8 cx24113_agc_table
[2][10] = {
485 {-54, -41, -35, -30, -25, -21, -16, -10, -6, -2},
486 {-39, -35, -30, -25, -19, -15, -11, -5, 1, 9},
489 void cx24113_agc_callback(struct dvb_frontend
*fe
)
491 struct cx24113_state
*state
= fe
->tuner_priv
;
493 if (!fe
->ops
.read_signal_strength
)
497 /* this only works with the current CX24123 implementation */
498 fe
->ops
.read_signal_strength(fe
, (u16
*) &s
);
500 dprintk("signal strength: %d\n", s
);
501 for (i
= 0; i
< sizeof(cx24113_agc_table
[0]); i
++)
502 if (cx24113_agc_table
[state
->gain_level
][i
] > s
)
505 } while (cx24113_set_gain_settings(state
, s
));
507 EXPORT_SYMBOL(cx24113_agc_callback
);
509 static int cx24113_get_frequency(struct dvb_frontend
*fe
, u32
*frequency
)
511 struct cx24113_state
*state
= fe
->tuner_priv
;
512 *frequency
= state
->frequency
;
516 static void cx24113_release(struct dvb_frontend
*fe
)
518 struct cx24113_state
*state
= fe
->tuner_priv
;
520 fe
->tuner_priv
= NULL
;
524 static const struct dvb_tuner_ops cx24113_tuner_ops
= {
526 .name
= "Conexant CX24113",
527 .frequency_min_hz
= 950 * MHz
,
528 .frequency_max_hz
= 2150 * MHz
,
529 .frequency_step_hz
= 125 * kHz
,
532 .release
= cx24113_release
,
534 .init
= cx24113_init
,
536 .set_params
= cx24113_set_params
,
537 .get_frequency
= cx24113_get_frequency
,
538 .get_status
= cx24113_get_status
,
541 struct dvb_frontend
*cx24113_attach(struct dvb_frontend
*fe
,
542 const struct cx24113_config
*config
, struct i2c_adapter
*i2c
)
544 /* allocate memory for the internal state */
545 struct cx24113_state
*state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
551 /* setup the state */
552 state
->config
= config
;
555 cx_info("trying to detect myself\n");
557 /* making a dummy read, because of some expected troubles
559 cx24113_readreg(state
, 0x00);
561 rc
= cx24113_readreg(state
, 0x00);
563 cx_info("CX24113 not found.\n");
570 cx_info("detected CX24113 variant\n");
573 cx_info("successfully detected\n");
576 cx_err("unsupported device id: %x\n", state
->rev
);
579 state
->ver
= cx24113_readreg(state
, 0x01);
580 cx_info("version: %x\n", state
->ver
);
582 /* create dvb_frontend */
583 memcpy(&fe
->ops
.tuner_ops
, &cx24113_tuner_ops
,
584 sizeof(struct dvb_tuner_ops
));
585 fe
->tuner_priv
= state
;
593 EXPORT_SYMBOL(cx24113_attach
);
595 module_param(debug
, int, 0644);
596 MODULE_PARM_DESC(debug
, "Activates frontend debugging (default:0)");
598 MODULE_AUTHOR("Patrick Boettcher <pb@linuxtv.org>");
599 MODULE_DESCRIPTION("DVB Frontend module for Conexant CX24113/CX24128hardware");
600 MODULE_LICENSE("GPL");