2 * Driver for Maxim MAX2165 silicon tuner
4 * Copyright (c) 2009 David T. L. Wong <davidtlwong@gmail.com>
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.
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/videodev2.h>
21 #include <linux/delay.h>
22 #include <linux/dvb/frontend.h>
23 #include <linux/i2c.h>
24 #include <linux/slab.h>
26 #include <media/dvb_frontend.h>
29 #include "max2165_priv.h"
30 #include "tuner-i2c.h"
32 #define dprintk(args...) \
35 printk(KERN_DEBUG "max2165: " args); \
39 module_param(debug
, int, 0644);
40 MODULE_PARM_DESC(debug
, "Turn on/off debugging (default:off).");
42 static int max2165_write_reg(struct max2165_priv
*priv
, u8 reg
, u8 data
)
45 u8 buf
[] = { reg
, data
};
46 struct i2c_msg msg
= { .flags
= 0, .buf
= buf
, .len
= 2 };
48 msg
.addr
= priv
->config
->i2c_address
;
51 dprintk("%s: reg=0x%02X, data=0x%02X\n", __func__
, reg
, data
);
53 ret
= i2c_transfer(priv
->i2c
, &msg
, 1);
56 dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
57 __func__
, reg
, data
, ret
);
59 return (ret
!= 1) ? -EIO
: 0;
62 static int max2165_read_reg(struct max2165_priv
*priv
, u8 reg
, u8
*p_data
)
65 u8 dev_addr
= priv
->config
->i2c_address
;
69 struct i2c_msg msg
[] = {
70 { .addr
= dev_addr
, .flags
= 0, .buf
= b0
, .len
= 1 },
71 { .addr
= dev_addr
, .flags
= I2C_M_RD
, .buf
= b1
, .len
= 1 },
74 ret
= i2c_transfer(priv
->i2c
, msg
, 2);
76 dprintk("%s: error reg=0x%x, ret=%i\n", __func__
, reg
, ret
);
82 dprintk("%s: reg=0x%02X, data=0x%02X\n",
83 __func__
, reg
, b1
[0]);
87 static int max2165_mask_write_reg(struct max2165_priv
*priv
, u8 reg
,
94 ret
= max2165_read_reg(priv
, reg
, &v
);
99 ret
= max2165_write_reg(priv
, reg
, v
);
104 static int max2165_read_rom_table(struct max2165_priv
*priv
)
109 for (i
= 0; i
< 3; i
++) {
110 max2165_write_reg(priv
, REG_ROM_TABLE_ADDR
, i
+ 1);
111 max2165_read_reg(priv
, REG_ROM_TABLE_DATA
, &dat
[i
]);
114 priv
->tf_ntch_low_cfg
= dat
[0] >> 4;
115 priv
->tf_ntch_hi_cfg
= dat
[0] & 0x0F;
116 priv
->tf_balun_low_ref
= dat
[1] & 0x0F;
117 priv
->tf_balun_hi_ref
= dat
[1] >> 4;
118 priv
->bb_filter_7mhz_cfg
= dat
[2] & 0x0F;
119 priv
->bb_filter_8mhz_cfg
= dat
[2] >> 4;
121 dprintk("tf_ntch_low_cfg = 0x%X\n", priv
->tf_ntch_low_cfg
);
122 dprintk("tf_ntch_hi_cfg = 0x%X\n", priv
->tf_ntch_hi_cfg
);
123 dprintk("tf_balun_low_ref = 0x%X\n", priv
->tf_balun_low_ref
);
124 dprintk("tf_balun_hi_ref = 0x%X\n", priv
->tf_balun_hi_ref
);
125 dprintk("bb_filter_7mhz_cfg = 0x%X\n", priv
->bb_filter_7mhz_cfg
);
126 dprintk("bb_filter_8mhz_cfg = 0x%X\n", priv
->bb_filter_8mhz_cfg
);
131 static int max2165_set_osc(struct max2165_priv
*priv
, u8 osc
/*MHz*/)
141 max2165_mask_write_reg(priv
, REG_PLL_CFG
, 0x07, v
);
146 static int max2165_set_bandwidth(struct max2165_priv
*priv
, u32 bw
)
151 val
= priv
->bb_filter_8mhz_cfg
;
153 val
= priv
->bb_filter_7mhz_cfg
;
155 max2165_mask_write_reg(priv
, REG_BASEBAND_CTRL
, 0xF0, val
<< 4);
160 static int fixpt_div32(u32 dividend
, u32 divisor
, u32
*quotient
, u32
*fraction
)
169 q
= dividend
/ divisor
;
170 remainder
= dividend
- q
* divisor
;
172 for (i
= 0; i
< 31; i
++) {
174 if (remainder
>= divisor
) {
176 remainder
-= divisor
;
187 static int max2165_set_rf(struct max2165_priv
*priv
, u32 freq
)
192 u32 quotient
, fraction
;
195 /* Set PLL divider according to RF frequency */
196 ret
= fixpt_div32(freq
/ 1000, priv
->config
->osc_clk
* 1000,
197 "ient
, &fraction
);
201 /* 20-bit fraction */
204 max2165_write_reg(priv
, REG_NDIV_INT
, quotient
);
205 max2165_mask_write_reg(priv
, REG_NDIV_FRAC2
, 0x0F, fraction
>> 16);
206 max2165_write_reg(priv
, REG_NDIV_FRAC1
, fraction
>> 8);
207 max2165_write_reg(priv
, REG_NDIV_FRAC0
, fraction
);
210 tf_ntch
= (freq
< 725000000) ?
211 priv
->tf_ntch_low_cfg
: priv
->tf_ntch_hi_cfg
;
213 /* Tracking filter balun */
214 t
= priv
->tf_balun_low_ref
;
215 t
+= (priv
->tf_balun_hi_ref
- priv
->tf_balun_low_ref
)
216 * (freq
/ 1000 - 470000) / (780000 - 470000);
219 dprintk("tf = %X\n", tf
);
222 max2165_write_reg(priv
, REG_TRACK_FILTER
, tf
);
227 static void max2165_debug_status(struct max2165_priv
*priv
)
230 u8 auto_vco_success
, auto_vco_active
;
232 u8 dc_offset_low
, dc_offset_hi
;
233 u8 signal_lv_over_threshold
;
234 u8 vco
, vco_sub_band
, adc
;
236 max2165_read_reg(priv
, REG_STATUS
, &status
);
237 max2165_read_reg(priv
, REG_AUTOTUNE
, &autotune
);
239 auto_vco_success
= (status
>> 6) & 0x01;
240 auto_vco_active
= (status
>> 5) & 0x01;
241 pll_locked
= (status
>> 4) & 0x01;
242 dc_offset_low
= (status
>> 3) & 0x01;
243 dc_offset_hi
= (status
>> 2) & 0x01;
244 signal_lv_over_threshold
= status
& 0x01;
247 vco_sub_band
= (autotune
>> 3) & 0x7;
248 adc
= autotune
& 0x7;
250 dprintk("auto VCO active: %d, auto VCO success: %d\n",
251 auto_vco_active
, auto_vco_success
);
252 dprintk("PLL locked: %d\n", pll_locked
);
253 dprintk("DC offset low: %d, DC offset high: %d\n",
254 dc_offset_low
, dc_offset_hi
);
255 dprintk("Signal lvl over threshold: %d\n", signal_lv_over_threshold
);
256 dprintk("VCO: %d, VCO Sub-band: %d, ADC: %d\n", vco
, vco_sub_band
, adc
);
259 static int max2165_set_params(struct dvb_frontend
*fe
)
261 struct max2165_priv
*priv
= fe
->tuner_priv
;
262 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
265 switch (c
->bandwidth_hz
) {
268 priv
->frequency
= c
->frequency
;
271 printk(KERN_INFO
"MAX2165: bandwidth %d Hz not supported.\n",
276 dprintk("%s() frequency=%d\n", __func__
, c
->frequency
);
278 if (fe
->ops
.i2c_gate_ctrl
)
279 fe
->ops
.i2c_gate_ctrl(fe
, 1);
280 max2165_set_bandwidth(priv
, c
->bandwidth_hz
);
281 ret
= max2165_set_rf(priv
, priv
->frequency
);
283 max2165_debug_status(priv
);
284 if (fe
->ops
.i2c_gate_ctrl
)
285 fe
->ops
.i2c_gate_ctrl(fe
, 0);
293 static int max2165_get_frequency(struct dvb_frontend
*fe
, u32
*freq
)
295 struct max2165_priv
*priv
= fe
->tuner_priv
;
296 dprintk("%s()\n", __func__
);
297 *freq
= priv
->frequency
;
301 static int max2165_get_bandwidth(struct dvb_frontend
*fe
, u32
*bw
)
303 struct max2165_priv
*priv
= fe
->tuner_priv
;
304 dprintk("%s()\n", __func__
);
306 *bw
= priv
->bandwidth
;
310 static int max2165_get_status(struct dvb_frontend
*fe
, u32
*status
)
312 struct max2165_priv
*priv
= fe
->tuner_priv
;
315 dprintk("%s()\n", __func__
);
317 if (fe
->ops
.i2c_gate_ctrl
)
318 fe
->ops
.i2c_gate_ctrl(fe
, 1);
320 max2165_debug_status(priv
);
321 *status
= lock_status
;
323 if (fe
->ops
.i2c_gate_ctrl
)
324 fe
->ops
.i2c_gate_ctrl(fe
, 0);
329 static int max2165_sleep(struct dvb_frontend
*fe
)
331 dprintk("%s()\n", __func__
);
335 static int max2165_init(struct dvb_frontend
*fe
)
337 struct max2165_priv
*priv
= fe
->tuner_priv
;
338 dprintk("%s()\n", __func__
);
340 if (fe
->ops
.i2c_gate_ctrl
)
341 fe
->ops
.i2c_gate_ctrl(fe
, 1);
343 /* Setup initial values */
344 /* Fractional Mode on */
345 max2165_write_reg(priv
, REG_NDIV_FRAC2
, 0x18);
347 max2165_write_reg(priv
, REG_LNA
, 0x01);
348 max2165_write_reg(priv
, REG_PLL_CFG
, 0x7A);
349 max2165_write_reg(priv
, REG_TEST
, 0x08);
350 max2165_write_reg(priv
, REG_SHUTDOWN
, 0x40);
351 max2165_write_reg(priv
, REG_VCO_CTRL
, 0x84);
352 max2165_write_reg(priv
, REG_BASEBAND_CTRL
, 0xC3);
353 max2165_write_reg(priv
, REG_DC_OFFSET_CTRL
, 0x75);
354 max2165_write_reg(priv
, REG_DC_OFFSET_DAC
, 0x00);
355 max2165_write_reg(priv
, REG_ROM_TABLE_ADDR
, 0x00);
357 max2165_set_osc(priv
, priv
->config
->osc_clk
);
359 max2165_read_rom_table(priv
);
361 max2165_set_bandwidth(priv
, 8000000);
363 if (fe
->ops
.i2c_gate_ctrl
)
364 fe
->ops
.i2c_gate_ctrl(fe
, 0);
369 static void max2165_release(struct dvb_frontend
*fe
)
371 struct max2165_priv
*priv
= fe
->tuner_priv
;
372 dprintk("%s()\n", __func__
);
375 fe
->tuner_priv
= NULL
;
378 static const struct dvb_tuner_ops max2165_tuner_ops
= {
380 .name
= "Maxim MAX2165",
381 .frequency_min
= 470000000,
382 .frequency_max
= 862000000,
383 .frequency_step
= 50000,
386 .release
= max2165_release
,
387 .init
= max2165_init
,
388 .sleep
= max2165_sleep
,
390 .set_params
= max2165_set_params
,
391 .set_analog_params
= NULL
,
392 .get_frequency
= max2165_get_frequency
,
393 .get_bandwidth
= max2165_get_bandwidth
,
394 .get_status
= max2165_get_status
397 struct dvb_frontend
*max2165_attach(struct dvb_frontend
*fe
,
398 struct i2c_adapter
*i2c
,
399 struct max2165_config
*cfg
)
401 struct max2165_priv
*priv
= NULL
;
403 dprintk("%s(%d-%04x)\n", __func__
,
404 i2c
? i2c_adapter_id(i2c
) : -1,
405 cfg
? cfg
->i2c_address
: -1);
407 priv
= kzalloc(sizeof(struct max2165_priv
), GFP_KERNEL
);
411 memcpy(&fe
->ops
.tuner_ops
, &max2165_tuner_ops
,
412 sizeof(struct dvb_tuner_ops
));
416 fe
->tuner_priv
= priv
;
419 max2165_debug_status(priv
);
423 EXPORT_SYMBOL(max2165_attach
);
425 MODULE_AUTHOR("David T. L. Wong <davidtlwong@gmail.com>");
426 MODULE_DESCRIPTION("Maxim MAX2165 silicon tuner driver");
427 MODULE_LICENSE("GPL");