2 * Support for AltoBeam GB20600 (a.k.a DMB-TH) demodulator
5 * Copyright (C) 2009 David T.L. Wong <davidtlwong@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * 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 <asm/div64.h>
23 #include "dvb_frontend.h"
26 #include "atbm8830_priv.h"
28 #define dprintk(args...) \
31 printk(KERN_DEBUG "atbm8830: " args); \
36 module_param(debug
, int, 0644);
37 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
39 static int atbm8830_write_reg(struct atbm_state
*priv
, u16 reg
, u8 data
)
43 u8 buf1
[] = { reg
>> 8, reg
& 0xFF };
45 struct i2c_msg msg1
= { .flags
= 0, .buf
= buf1
, .len
= 2 };
46 struct i2c_msg msg2
= { .flags
= 0, .buf
= buf2
, .len
= 1 };
48 dev_addr
= priv
->config
->demod_address
;
53 dprintk("%s: reg=0x%04X, data=0x%02X\n", __func__
, reg
, data
);
55 ret
= i2c_transfer(priv
->i2c
, &msg1
, 1);
59 ret
= i2c_transfer(priv
->i2c
, &msg2
, 1);
60 return (ret
!= 1) ? -EIO
: 0;
63 static int atbm8830_read_reg(struct atbm_state
*priv
, u16 reg
, u8
*p_data
)
68 u8 buf1
[] = { reg
>> 8, reg
& 0xFF };
70 struct i2c_msg msg1
= { .flags
= 0, .buf
= buf1
, .len
= 2 };
71 struct i2c_msg msg2
= { .flags
= I2C_M_RD
, .buf
= buf2
, .len
= 1 };
73 dev_addr
= priv
->config
->demod_address
;
77 ret
= i2c_transfer(priv
->i2c
, &msg1
, 1);
79 dprintk("%s: error reg=0x%04x, ret=%i\n", __func__
, reg
, ret
);
83 ret
= i2c_transfer(priv
->i2c
, &msg2
, 1);
89 dprintk("%s: reg=0x%04X, data=0x%02X\n",
90 __func__
, reg
, buf2
[0]);
95 /* Lock register latch so that multi-register read is atomic */
96 static inline int atbm8830_reglatch_lock(struct atbm_state
*priv
, int lock
)
98 return atbm8830_write_reg(priv
, REG_READ_LATCH
, lock
? 1 : 0);
101 static int set_osc_freq(struct atbm_state
*priv
, u32 freq
/*in kHz*/)
106 /* 0x100000 * freq / 30.4MHz */
107 t
= (u64
)0x100000 * freq
;
111 atbm8830_write_reg(priv
, REG_OSC_CLK
, val
);
112 atbm8830_write_reg(priv
, REG_OSC_CLK
+ 1, val
>> 8);
113 atbm8830_write_reg(priv
, REG_OSC_CLK
+ 2, val
>> 16);
118 static int set_if_freq(struct atbm_state
*priv
, u32 freq
/*in kHz*/)
121 u32 fs
= priv
->config
->osc_clk_freq
;
127 /* 2 * PI * (freq - fs) / fs * (2 ^ 22) */
128 t
= (u64
) 2 * 31416 * (freq
- fs
);
134 atbm8830_write_reg(priv
, REG_TUNER_BASEBAND
, 1);
135 atbm8830_write_reg(priv
, REG_IF_FREQ
, val
);
136 atbm8830_write_reg(priv
, REG_IF_FREQ
+1, val
>> 8);
137 atbm8830_write_reg(priv
, REG_IF_FREQ
+2, val
>> 16);
139 atbm8830_read_reg(priv
, REG_ADC_CONFIG
, &dat
);
141 atbm8830_write_reg(priv
, REG_ADC_CONFIG
, dat
);
144 atbm8830_write_reg(priv
, REG_TUNER_BASEBAND
, 0);
146 atbm8830_read_reg(priv
, REG_ADC_CONFIG
, &dat
);
149 atbm8830_write_reg(priv
, REG_ADC_CONFIG
, dat
);
151 if (priv
->config
->zif_swap_iq
)
152 atbm8830_write_reg(priv
, REG_SWAP_I_Q
, 0x03);
154 atbm8830_write_reg(priv
, REG_SWAP_I_Q
, 0x01);
160 static int is_locked(struct atbm_state
*priv
, u8
*locked
)
164 atbm8830_read_reg(priv
, REG_LOCK_STATUS
, &status
);
167 *locked
= (status
== 1);
171 static int set_agc_config(struct atbm_state
*priv
,
172 u8 min
, u8 max
, u8 hold_loop
)
174 /* no effect if both min and max are zero */
178 atbm8830_write_reg(priv
, REG_AGC_MIN
, min
);
179 atbm8830_write_reg(priv
, REG_AGC_MAX
, max
);
180 atbm8830_write_reg(priv
, REG_AGC_HOLD_LOOP
, hold_loop
);
185 static int set_static_channel_mode(struct atbm_state
*priv
)
189 for (i
= 0; i
< 5; i
++)
190 atbm8830_write_reg(priv
, 0x099B + i
, 0x08);
192 atbm8830_write_reg(priv
, 0x095B, 0x7F);
193 atbm8830_write_reg(priv
, 0x09CB, 0x01);
194 atbm8830_write_reg(priv
, 0x09CC, 0x7F);
195 atbm8830_write_reg(priv
, 0x09CD, 0x7F);
196 atbm8830_write_reg(priv
, 0x0E01, 0x20);
198 /* For single carrier */
199 atbm8830_write_reg(priv
, 0x0B03, 0x0A);
200 atbm8830_write_reg(priv
, 0x0935, 0x10);
201 atbm8830_write_reg(priv
, 0x0936, 0x08);
202 atbm8830_write_reg(priv
, 0x093E, 0x08);
203 atbm8830_write_reg(priv
, 0x096E, 0x06);
205 /* frame_count_max0 */
206 atbm8830_write_reg(priv
, 0x0B09, 0x00);
207 /* frame_count_max1 */
208 atbm8830_write_reg(priv
, 0x0B0A, 0x08);
213 static int set_ts_config(struct atbm_state
*priv
)
215 const struct atbm8830_config
*cfg
= priv
->config
;
217 /*Set parallel/serial ts mode*/
218 atbm8830_write_reg(priv
, REG_TS_SERIAL
, cfg
->serial_ts
? 1 : 0);
219 atbm8830_write_reg(priv
, REG_TS_CLK_MODE
, cfg
->serial_ts
? 1 : 0);
220 /*Set ts sampling edge*/
221 atbm8830_write_reg(priv
, REG_TS_SAMPLE_EDGE
,
222 cfg
->ts_sampling_edge
? 1 : 0);
223 /*Set ts clock freerun*/
224 atbm8830_write_reg(priv
, REG_TS_CLK_FREERUN
,
225 cfg
->ts_clk_gated
? 0 : 1);
230 static int atbm8830_init(struct dvb_frontend
*fe
)
232 struct atbm_state
*priv
= fe
->demodulator_priv
;
233 const struct atbm8830_config
*cfg
= priv
->config
;
235 /*Set oscillator frequency*/
236 set_osc_freq(priv
, cfg
->osc_clk_freq
);
239 set_if_freq(priv
, cfg
->if_freq
);
242 set_agc_config(priv
, cfg
->agc_min
, cfg
->agc_max
,
245 /*Set static channel mode*/
246 set_static_channel_mode(priv
);
249 /*Turn off DSP reset*/
250 atbm8830_write_reg(priv
, 0x000A, 0);
253 atbm8830_write_reg(priv
, 0x020C, 11);
256 atbm8830_write_reg(priv
, REG_DEMOD_RUN
, 1);
262 static void atbm8830_release(struct dvb_frontend
*fe
)
264 struct atbm_state
*state
= fe
->demodulator_priv
;
265 dprintk("%s\n", __func__
);
270 static int atbm8830_set_fe(struct dvb_frontend
*fe
)
272 struct atbm_state
*priv
= fe
->demodulator_priv
;
275 dprintk("%s\n", __func__
);
278 if (fe
->ops
.tuner_ops
.set_params
) {
279 if (fe
->ops
.i2c_gate_ctrl
)
280 fe
->ops
.i2c_gate_ctrl(fe
, 1);
281 fe
->ops
.tuner_ops
.set_params(fe
);
282 if (fe
->ops
.i2c_gate_ctrl
)
283 fe
->ops
.i2c_gate_ctrl(fe
, 0);
286 /* start auto lock */
287 for (i
= 0; i
< 10; i
++) {
289 dprintk("Try %d\n", i
);
290 is_locked(priv
, &locked
);
292 dprintk("ATBM8830 locked!\n");
300 static int atbm8830_get_fe(struct dvb_frontend
*fe
)
302 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
303 dprintk("%s\n", __func__
);
305 /* TODO: get real readings from device */
306 /* inversion status */
307 c
->inversion
= INVERSION_OFF
;
310 c
->bandwidth_hz
= 8000000;
312 c
->code_rate_HP
= FEC_AUTO
;
313 c
->code_rate_LP
= FEC_AUTO
;
315 c
->modulation
= QAM_AUTO
;
317 /* transmission mode */
318 c
->transmission_mode
= TRANSMISSION_MODE_AUTO
;
321 c
->guard_interval
= GUARD_INTERVAL_AUTO
;
324 c
->hierarchy
= HIERARCHY_NONE
;
329 static int atbm8830_get_tune_settings(struct dvb_frontend
*fe
,
330 struct dvb_frontend_tune_settings
*fesettings
)
332 fesettings
->min_delay_ms
= 0;
333 fesettings
->step_size
= 0;
334 fesettings
->max_drift
= 0;
338 static int atbm8830_read_status(struct dvb_frontend
*fe
, fe_status_t
*fe_status
)
340 struct atbm_state
*priv
= fe
->demodulator_priv
;
344 dprintk("%s\n", __func__
);
347 is_locked(priv
, &locked
);
349 *fe_status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
350 FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK
;
352 dprintk("%s: fe_status=0x%x\n", __func__
, *fe_status
);
354 atbm8830_read_reg(priv
, REG_AGC_LOCK
, &agc_locked
);
355 dprintk("AGC Lock: %d\n", agc_locked
);
360 static int atbm8830_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
362 struct atbm_state
*priv
= fe
->demodulator_priv
;
366 dprintk("%s\n", __func__
);
368 atbm8830_reglatch_lock(priv
, 1);
370 atbm8830_read_reg(priv
, REG_FRAME_ERR_CNT
+ 1, &t
);
371 frame_err
= t
& 0x7F;
373 atbm8830_read_reg(priv
, REG_FRAME_ERR_CNT
, &t
);
376 atbm8830_reglatch_lock(priv
, 0);
378 *ber
= frame_err
* 100 / 32767;
380 dprintk("%s: ber=0x%x\n", __func__
, *ber
);
384 static int atbm8830_read_signal_strength(struct dvb_frontend
*fe
, u16
*signal
)
386 struct atbm_state
*priv
= fe
->demodulator_priv
;
390 dprintk("%s\n", __func__
);
391 atbm8830_reglatch_lock(priv
, 1);
393 atbm8830_read_reg(priv
, REG_AGC_PWM_VAL
+ 1, &t
);
396 atbm8830_read_reg(priv
, REG_AGC_PWM_VAL
, &t
);
399 atbm8830_reglatch_lock(priv
, 0);
401 dprintk("AGC PWM = 0x%02X\n", pwm
);
404 *signal
= pwm
* 0x10000 / 0x400;
409 static int atbm8830_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
411 dprintk("%s\n", __func__
);
416 static int atbm8830_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
418 dprintk("%s\n", __func__
);
423 static int atbm8830_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
425 struct atbm_state
*priv
= fe
->demodulator_priv
;
427 return atbm8830_write_reg(priv
, REG_I2C_GATE
, enable
? 1 : 0);
430 static struct dvb_frontend_ops atbm8830_ops
= {
431 .delsys
= { SYS_DTMB
},
433 .name
= "AltoBeam ATBM8830/8831 DMB-TH",
434 .frequency_min
= 474000000,
435 .frequency_max
= 858000000,
436 .frequency_stepsize
= 10000,
440 FE_CAN_TRANSMISSION_MODE_AUTO
|
441 FE_CAN_GUARD_INTERVAL_AUTO
444 .release
= atbm8830_release
,
446 .init
= atbm8830_init
,
449 .i2c_gate_ctrl
= atbm8830_i2c_gate_ctrl
,
451 .set_frontend
= atbm8830_set_fe
,
452 .get_frontend
= atbm8830_get_fe
,
453 .get_tune_settings
= atbm8830_get_tune_settings
,
455 .read_status
= atbm8830_read_status
,
456 .read_ber
= atbm8830_read_ber
,
457 .read_signal_strength
= atbm8830_read_signal_strength
,
458 .read_snr
= atbm8830_read_snr
,
459 .read_ucblocks
= atbm8830_read_ucblocks
,
462 struct dvb_frontend
*atbm8830_attach(const struct atbm8830_config
*config
,
463 struct i2c_adapter
*i2c
)
465 struct atbm_state
*priv
= NULL
;
468 dprintk("%s()\n", __func__
);
470 if (config
== NULL
|| i2c
== NULL
)
473 priv
= kzalloc(sizeof(struct atbm_state
), GFP_KERNEL
);
477 priv
->config
= config
;
480 /* check if the demod is there */
481 if (atbm8830_read_reg(priv
, REG_CHIP_ID
, &data
) != 0) {
482 dprintk("%s atbm8830/8831 not found at i2c addr 0x%02X\n",
483 __func__
, priv
->config
->demod_address
);
486 dprintk("atbm8830 chip id: 0x%02X\n", data
);
488 memcpy(&priv
->frontend
.ops
, &atbm8830_ops
,
489 sizeof(struct dvb_frontend_ops
));
490 priv
->frontend
.demodulator_priv
= priv
;
492 atbm8830_init(&priv
->frontend
);
494 atbm8830_i2c_gate_ctrl(&priv
->frontend
, 1);
496 return &priv
->frontend
;
499 dprintk("%s() error_out\n", __func__
);
504 EXPORT_SYMBOL(atbm8830_attach
);
506 MODULE_DESCRIPTION("AltoBeam ATBM8830/8831 GB20600 demodulator driver");
507 MODULE_AUTHOR("David T. L. Wong <davidtlwong@gmail.com>");
508 MODULE_LICENSE("GPL");