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.
18 #include <asm/div64.h>
19 #include <media/dvb_frontend.h>
22 #include "atbm8830_priv.h"
24 #define dprintk(args...) \
27 printk(KERN_DEBUG "atbm8830: " args); \
32 module_param(debug
, int, 0644);
33 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
35 static int atbm8830_write_reg(struct atbm_state
*priv
, u16 reg
, u8 data
)
39 u8 buf1
[] = { reg
>> 8, reg
& 0xFF };
41 struct i2c_msg msg1
= { .flags
= 0, .buf
= buf1
, .len
= 2 };
42 struct i2c_msg msg2
= { .flags
= 0, .buf
= buf2
, .len
= 1 };
44 dev_addr
= priv
->config
->demod_address
;
49 dprintk("%s: reg=0x%04X, data=0x%02X\n", __func__
, reg
, data
);
51 ret
= i2c_transfer(priv
->i2c
, &msg1
, 1);
55 ret
= i2c_transfer(priv
->i2c
, &msg2
, 1);
56 return (ret
!= 1) ? -EIO
: 0;
59 static int atbm8830_read_reg(struct atbm_state
*priv
, u16 reg
, u8
*p_data
)
64 u8 buf1
[] = { reg
>> 8, reg
& 0xFF };
66 struct i2c_msg msg1
= { .flags
= 0, .buf
= buf1
, .len
= 2 };
67 struct i2c_msg msg2
= { .flags
= I2C_M_RD
, .buf
= buf2
, .len
= 1 };
69 dev_addr
= priv
->config
->demod_address
;
73 ret
= i2c_transfer(priv
->i2c
, &msg1
, 1);
75 dprintk("%s: error reg=0x%04x, ret=%i\n", __func__
, reg
, ret
);
79 ret
= i2c_transfer(priv
->i2c
, &msg2
, 1);
85 dprintk("%s: reg=0x%04X, data=0x%02X\n",
86 __func__
, reg
, buf2
[0]);
91 /* Lock register latch so that multi-register read is atomic */
92 static inline int atbm8830_reglatch_lock(struct atbm_state
*priv
, int lock
)
94 return atbm8830_write_reg(priv
, REG_READ_LATCH
, lock
? 1 : 0);
97 static int set_osc_freq(struct atbm_state
*priv
, u32 freq
/*in kHz*/)
102 /* 0x100000 * freq / 30.4MHz */
103 t
= (u64
)0x100000 * freq
;
107 atbm8830_write_reg(priv
, REG_OSC_CLK
, val
);
108 atbm8830_write_reg(priv
, REG_OSC_CLK
+ 1, val
>> 8);
109 atbm8830_write_reg(priv
, REG_OSC_CLK
+ 2, val
>> 16);
114 static int set_if_freq(struct atbm_state
*priv
, u32 freq
/*in kHz*/)
117 u32 fs
= priv
->config
->osc_clk_freq
;
123 /* 2 * PI * (freq - fs) / fs * (2 ^ 22) */
124 t
= (u64
) 2 * 31416 * (freq
- fs
);
130 atbm8830_write_reg(priv
, REG_TUNER_BASEBAND
, 1);
131 atbm8830_write_reg(priv
, REG_IF_FREQ
, val
);
132 atbm8830_write_reg(priv
, REG_IF_FREQ
+1, val
>> 8);
133 atbm8830_write_reg(priv
, REG_IF_FREQ
+2, val
>> 16);
135 atbm8830_read_reg(priv
, REG_ADC_CONFIG
, &dat
);
137 atbm8830_write_reg(priv
, REG_ADC_CONFIG
, dat
);
140 atbm8830_write_reg(priv
, REG_TUNER_BASEBAND
, 0);
142 atbm8830_read_reg(priv
, REG_ADC_CONFIG
, &dat
);
145 atbm8830_write_reg(priv
, REG_ADC_CONFIG
, dat
);
147 if (priv
->config
->zif_swap_iq
)
148 atbm8830_write_reg(priv
, REG_SWAP_I_Q
, 0x03);
150 atbm8830_write_reg(priv
, REG_SWAP_I_Q
, 0x01);
156 static int is_locked(struct atbm_state
*priv
, u8
*locked
)
160 atbm8830_read_reg(priv
, REG_LOCK_STATUS
, &status
);
163 *locked
= (status
== 1);
167 static int set_agc_config(struct atbm_state
*priv
,
168 u8 min
, u8 max
, u8 hold_loop
)
170 /* no effect if both min and max are zero */
174 atbm8830_write_reg(priv
, REG_AGC_MIN
, min
);
175 atbm8830_write_reg(priv
, REG_AGC_MAX
, max
);
176 atbm8830_write_reg(priv
, REG_AGC_HOLD_LOOP
, hold_loop
);
181 static int set_static_channel_mode(struct atbm_state
*priv
)
185 for (i
= 0; i
< 5; i
++)
186 atbm8830_write_reg(priv
, 0x099B + i
, 0x08);
188 atbm8830_write_reg(priv
, 0x095B, 0x7F);
189 atbm8830_write_reg(priv
, 0x09CB, 0x01);
190 atbm8830_write_reg(priv
, 0x09CC, 0x7F);
191 atbm8830_write_reg(priv
, 0x09CD, 0x7F);
192 atbm8830_write_reg(priv
, 0x0E01, 0x20);
194 /* For single carrier */
195 atbm8830_write_reg(priv
, 0x0B03, 0x0A);
196 atbm8830_write_reg(priv
, 0x0935, 0x10);
197 atbm8830_write_reg(priv
, 0x0936, 0x08);
198 atbm8830_write_reg(priv
, 0x093E, 0x08);
199 atbm8830_write_reg(priv
, 0x096E, 0x06);
201 /* frame_count_max0 */
202 atbm8830_write_reg(priv
, 0x0B09, 0x00);
203 /* frame_count_max1 */
204 atbm8830_write_reg(priv
, 0x0B0A, 0x08);
209 static int set_ts_config(struct atbm_state
*priv
)
211 const struct atbm8830_config
*cfg
= priv
->config
;
213 /*Set parallel/serial ts mode*/
214 atbm8830_write_reg(priv
, REG_TS_SERIAL
, cfg
->serial_ts
? 1 : 0);
215 atbm8830_write_reg(priv
, REG_TS_CLK_MODE
, cfg
->serial_ts
? 1 : 0);
216 /*Set ts sampling edge*/
217 atbm8830_write_reg(priv
, REG_TS_SAMPLE_EDGE
,
218 cfg
->ts_sampling_edge
? 1 : 0);
219 /*Set ts clock freerun*/
220 atbm8830_write_reg(priv
, REG_TS_CLK_FREERUN
,
221 cfg
->ts_clk_gated
? 0 : 1);
226 static int atbm8830_init(struct dvb_frontend
*fe
)
228 struct atbm_state
*priv
= fe
->demodulator_priv
;
229 const struct atbm8830_config
*cfg
= priv
->config
;
231 /*Set oscillator frequency*/
232 set_osc_freq(priv
, cfg
->osc_clk_freq
);
235 set_if_freq(priv
, cfg
->if_freq
);
238 set_agc_config(priv
, cfg
->agc_min
, cfg
->agc_max
,
241 /*Set static channel mode*/
242 set_static_channel_mode(priv
);
245 /*Turn off DSP reset*/
246 atbm8830_write_reg(priv
, 0x000A, 0);
249 atbm8830_write_reg(priv
, 0x020C, 11);
252 atbm8830_write_reg(priv
, REG_DEMOD_RUN
, 1);
258 static void atbm8830_release(struct dvb_frontend
*fe
)
260 struct atbm_state
*state
= fe
->demodulator_priv
;
261 dprintk("%s\n", __func__
);
266 static int atbm8830_set_fe(struct dvb_frontend
*fe
)
268 struct atbm_state
*priv
= fe
->demodulator_priv
;
271 dprintk("%s\n", __func__
);
274 if (fe
->ops
.tuner_ops
.set_params
) {
275 if (fe
->ops
.i2c_gate_ctrl
)
276 fe
->ops
.i2c_gate_ctrl(fe
, 1);
277 fe
->ops
.tuner_ops
.set_params(fe
);
278 if (fe
->ops
.i2c_gate_ctrl
)
279 fe
->ops
.i2c_gate_ctrl(fe
, 0);
282 /* start auto lock */
283 for (i
= 0; i
< 10; i
++) {
285 dprintk("Try %d\n", i
);
286 is_locked(priv
, &locked
);
288 dprintk("ATBM8830 locked!\n");
296 static int atbm8830_get_fe(struct dvb_frontend
*fe
,
297 struct dtv_frontend_properties
*c
)
299 dprintk("%s\n", __func__
);
301 /* TODO: get real readings from device */
302 /* inversion status */
303 c
->inversion
= INVERSION_OFF
;
306 c
->bandwidth_hz
= 8000000;
308 c
->code_rate_HP
= FEC_AUTO
;
309 c
->code_rate_LP
= FEC_AUTO
;
311 c
->modulation
= QAM_AUTO
;
313 /* transmission mode */
314 c
->transmission_mode
= TRANSMISSION_MODE_AUTO
;
317 c
->guard_interval
= GUARD_INTERVAL_AUTO
;
320 c
->hierarchy
= HIERARCHY_NONE
;
325 static int atbm8830_get_tune_settings(struct dvb_frontend
*fe
,
326 struct dvb_frontend_tune_settings
*fesettings
)
328 fesettings
->min_delay_ms
= 0;
329 fesettings
->step_size
= 0;
330 fesettings
->max_drift
= 0;
334 static int atbm8830_read_status(struct dvb_frontend
*fe
,
335 enum fe_status
*fe_status
)
337 struct atbm_state
*priv
= fe
->demodulator_priv
;
341 dprintk("%s\n", __func__
);
344 is_locked(priv
, &locked
);
346 *fe_status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
347 FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK
;
349 dprintk("%s: fe_status=0x%x\n", __func__
, *fe_status
);
351 atbm8830_read_reg(priv
, REG_AGC_LOCK
, &agc_locked
);
352 dprintk("AGC Lock: %d\n", agc_locked
);
357 static int atbm8830_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
359 struct atbm_state
*priv
= fe
->demodulator_priv
;
363 dprintk("%s\n", __func__
);
365 atbm8830_reglatch_lock(priv
, 1);
367 atbm8830_read_reg(priv
, REG_FRAME_ERR_CNT
+ 1, &t
);
368 frame_err
= t
& 0x7F;
370 atbm8830_read_reg(priv
, REG_FRAME_ERR_CNT
, &t
);
373 atbm8830_reglatch_lock(priv
, 0);
375 *ber
= frame_err
* 100 / 32767;
377 dprintk("%s: ber=0x%x\n", __func__
, *ber
);
381 static int atbm8830_read_signal_strength(struct dvb_frontend
*fe
, u16
*signal
)
383 struct atbm_state
*priv
= fe
->demodulator_priv
;
387 dprintk("%s\n", __func__
);
388 atbm8830_reglatch_lock(priv
, 1);
390 atbm8830_read_reg(priv
, REG_AGC_PWM_VAL
+ 1, &t
);
393 atbm8830_read_reg(priv
, REG_AGC_PWM_VAL
, &t
);
396 atbm8830_reglatch_lock(priv
, 0);
398 dprintk("AGC PWM = 0x%02X\n", pwm
);
401 *signal
= pwm
* 0x10000 / 0x400;
406 static int atbm8830_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
408 dprintk("%s\n", __func__
);
413 static int atbm8830_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
415 dprintk("%s\n", __func__
);
420 static int atbm8830_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
422 struct atbm_state
*priv
= fe
->demodulator_priv
;
424 return atbm8830_write_reg(priv
, REG_I2C_GATE
, enable
? 1 : 0);
427 static const struct dvb_frontend_ops atbm8830_ops
= {
428 .delsys
= { SYS_DTMB
},
430 .name
= "AltoBeam ATBM8830/8831 DMB-TH",
431 .frequency_min
= 474000000,
432 .frequency_max
= 858000000,
433 .frequency_stepsize
= 10000,
437 FE_CAN_TRANSMISSION_MODE_AUTO
|
438 FE_CAN_GUARD_INTERVAL_AUTO
441 .release
= atbm8830_release
,
443 .init
= atbm8830_init
,
446 .i2c_gate_ctrl
= atbm8830_i2c_gate_ctrl
,
448 .set_frontend
= atbm8830_set_fe
,
449 .get_frontend
= atbm8830_get_fe
,
450 .get_tune_settings
= atbm8830_get_tune_settings
,
452 .read_status
= atbm8830_read_status
,
453 .read_ber
= atbm8830_read_ber
,
454 .read_signal_strength
= atbm8830_read_signal_strength
,
455 .read_snr
= atbm8830_read_snr
,
456 .read_ucblocks
= atbm8830_read_ucblocks
,
459 struct dvb_frontend
*atbm8830_attach(const struct atbm8830_config
*config
,
460 struct i2c_adapter
*i2c
)
462 struct atbm_state
*priv
= NULL
;
465 dprintk("%s()\n", __func__
);
467 if (config
== NULL
|| i2c
== NULL
)
470 priv
= kzalloc(sizeof(struct atbm_state
), GFP_KERNEL
);
474 priv
->config
= config
;
477 /* check if the demod is there */
478 if (atbm8830_read_reg(priv
, REG_CHIP_ID
, &data
) != 0) {
479 dprintk("%s atbm8830/8831 not found at i2c addr 0x%02X\n",
480 __func__
, priv
->config
->demod_address
);
483 dprintk("atbm8830 chip id: 0x%02X\n", data
);
485 memcpy(&priv
->frontend
.ops
, &atbm8830_ops
,
486 sizeof(struct dvb_frontend_ops
));
487 priv
->frontend
.demodulator_priv
= priv
;
489 atbm8830_init(&priv
->frontend
);
491 atbm8830_i2c_gate_ctrl(&priv
->frontend
, 1);
493 return &priv
->frontend
;
496 dprintk("%s() error_out\n", __func__
);
501 EXPORT_SYMBOL(atbm8830_attach
);
503 MODULE_DESCRIPTION("AltoBeam ATBM8830/8831 GB20600 demodulator driver");
504 MODULE_AUTHOR("David T. L. Wong <davidtlwong@gmail.com>");
505 MODULE_LICENSE("GPL");