1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Support for AltoBeam GB20600 (a.k.a DMB-TH) demodulator
6 * Copyright (C) 2009 David T.L. Wong <davidtlwong@gmail.com>
10 #include <media/dvb_frontend.h>
13 #include "atbm8830_priv.h"
15 #define dprintk(args...) \
18 printk(KERN_DEBUG "atbm8830: " args); \
23 module_param(debug
, int, 0644);
24 MODULE_PARM_DESC(debug
, "Turn on/off frontend debugging (default:off).");
26 static int atbm8830_write_reg(struct atbm_state
*priv
, u16 reg
, u8 data
)
30 u8 buf1
[] = { reg
>> 8, reg
& 0xFF };
32 struct i2c_msg msg1
= { .flags
= 0, .buf
= buf1
, .len
= 2 };
33 struct i2c_msg msg2
= { .flags
= 0, .buf
= buf2
, .len
= 1 };
35 dev_addr
= priv
->config
->demod_address
;
40 dprintk("%s: reg=0x%04X, data=0x%02X\n", __func__
, reg
, data
);
42 ret
= i2c_transfer(priv
->i2c
, &msg1
, 1);
46 ret
= i2c_transfer(priv
->i2c
, &msg2
, 1);
47 return (ret
!= 1) ? -EIO
: 0;
50 static int atbm8830_read_reg(struct atbm_state
*priv
, u16 reg
, u8
*p_data
)
55 u8 buf1
[] = { reg
>> 8, reg
& 0xFF };
57 struct i2c_msg msg1
= { .flags
= 0, .buf
= buf1
, .len
= 2 };
58 struct i2c_msg msg2
= { .flags
= I2C_M_RD
, .buf
= buf2
, .len
= 1 };
60 dev_addr
= priv
->config
->demod_address
;
64 ret
= i2c_transfer(priv
->i2c
, &msg1
, 1);
66 dprintk("%s: error reg=0x%04x, ret=%i\n", __func__
, reg
, ret
);
70 ret
= i2c_transfer(priv
->i2c
, &msg2
, 1);
76 dprintk("%s: reg=0x%04X, data=0x%02X\n",
77 __func__
, reg
, buf2
[0]);
82 /* Lock register latch so that multi-register read is atomic */
83 static inline int atbm8830_reglatch_lock(struct atbm_state
*priv
, int lock
)
85 return atbm8830_write_reg(priv
, REG_READ_LATCH
, lock
? 1 : 0);
88 static int set_osc_freq(struct atbm_state
*priv
, u32 freq
/*in kHz*/)
93 /* 0x100000 * freq / 30.4MHz */
94 t
= (u64
)0x100000 * freq
;
98 atbm8830_write_reg(priv
, REG_OSC_CLK
, val
);
99 atbm8830_write_reg(priv
, REG_OSC_CLK
+ 1, val
>> 8);
100 atbm8830_write_reg(priv
, REG_OSC_CLK
+ 2, val
>> 16);
105 static int set_if_freq(struct atbm_state
*priv
, u32 freq
/*in kHz*/)
108 u32 fs
= priv
->config
->osc_clk_freq
;
114 /* 2 * PI * (freq - fs) / fs * (2 ^ 22) */
115 t
= (u64
) 2 * 31416 * (freq
- fs
);
121 atbm8830_write_reg(priv
, REG_TUNER_BASEBAND
, 1);
122 atbm8830_write_reg(priv
, REG_IF_FREQ
, val
);
123 atbm8830_write_reg(priv
, REG_IF_FREQ
+1, val
>> 8);
124 atbm8830_write_reg(priv
, REG_IF_FREQ
+2, val
>> 16);
126 atbm8830_read_reg(priv
, REG_ADC_CONFIG
, &dat
);
128 atbm8830_write_reg(priv
, REG_ADC_CONFIG
, dat
);
131 atbm8830_write_reg(priv
, REG_TUNER_BASEBAND
, 0);
133 atbm8830_read_reg(priv
, REG_ADC_CONFIG
, &dat
);
136 atbm8830_write_reg(priv
, REG_ADC_CONFIG
, dat
);
138 if (priv
->config
->zif_swap_iq
)
139 atbm8830_write_reg(priv
, REG_SWAP_I_Q
, 0x03);
141 atbm8830_write_reg(priv
, REG_SWAP_I_Q
, 0x01);
147 static int is_locked(struct atbm_state
*priv
, u8
*locked
)
151 atbm8830_read_reg(priv
, REG_LOCK_STATUS
, &status
);
154 *locked
= (status
== 1);
158 static int set_agc_config(struct atbm_state
*priv
,
159 u8 min
, u8 max
, u8 hold_loop
)
161 /* no effect if both min and max are zero */
165 atbm8830_write_reg(priv
, REG_AGC_MIN
, min
);
166 atbm8830_write_reg(priv
, REG_AGC_MAX
, max
);
167 atbm8830_write_reg(priv
, REG_AGC_HOLD_LOOP
, hold_loop
);
172 static int set_static_channel_mode(struct atbm_state
*priv
)
176 for (i
= 0; i
< 5; i
++)
177 atbm8830_write_reg(priv
, 0x099B + i
, 0x08);
179 atbm8830_write_reg(priv
, 0x095B, 0x7F);
180 atbm8830_write_reg(priv
, 0x09CB, 0x01);
181 atbm8830_write_reg(priv
, 0x09CC, 0x7F);
182 atbm8830_write_reg(priv
, 0x09CD, 0x7F);
183 atbm8830_write_reg(priv
, 0x0E01, 0x20);
185 /* For single carrier */
186 atbm8830_write_reg(priv
, 0x0B03, 0x0A);
187 atbm8830_write_reg(priv
, 0x0935, 0x10);
188 atbm8830_write_reg(priv
, 0x0936, 0x08);
189 atbm8830_write_reg(priv
, 0x093E, 0x08);
190 atbm8830_write_reg(priv
, 0x096E, 0x06);
192 /* frame_count_max0 */
193 atbm8830_write_reg(priv
, 0x0B09, 0x00);
194 /* frame_count_max1 */
195 atbm8830_write_reg(priv
, 0x0B0A, 0x08);
200 static int set_ts_config(struct atbm_state
*priv
)
202 const struct atbm8830_config
*cfg
= priv
->config
;
204 /*Set parallel/serial ts mode*/
205 atbm8830_write_reg(priv
, REG_TS_SERIAL
, cfg
->serial_ts
? 1 : 0);
206 atbm8830_write_reg(priv
, REG_TS_CLK_MODE
, cfg
->serial_ts
? 1 : 0);
207 /*Set ts sampling edge*/
208 atbm8830_write_reg(priv
, REG_TS_SAMPLE_EDGE
,
209 cfg
->ts_sampling_edge
? 1 : 0);
210 /*Set ts clock freerun*/
211 atbm8830_write_reg(priv
, REG_TS_CLK_FREERUN
,
212 cfg
->ts_clk_gated
? 0 : 1);
217 static int atbm8830_init(struct dvb_frontend
*fe
)
219 struct atbm_state
*priv
= fe
->demodulator_priv
;
220 const struct atbm8830_config
*cfg
= priv
->config
;
222 /*Set oscillator frequency*/
223 set_osc_freq(priv
, cfg
->osc_clk_freq
);
226 set_if_freq(priv
, cfg
->if_freq
);
229 set_agc_config(priv
, cfg
->agc_min
, cfg
->agc_max
,
232 /*Set static channel mode*/
233 set_static_channel_mode(priv
);
236 /*Turn off DSP reset*/
237 atbm8830_write_reg(priv
, 0x000A, 0);
240 atbm8830_write_reg(priv
, 0x020C, 11);
243 atbm8830_write_reg(priv
, REG_DEMOD_RUN
, 1);
249 static void atbm8830_release(struct dvb_frontend
*fe
)
251 struct atbm_state
*state
= fe
->demodulator_priv
;
252 dprintk("%s\n", __func__
);
257 static int atbm8830_set_fe(struct dvb_frontend
*fe
)
259 struct atbm_state
*priv
= fe
->demodulator_priv
;
262 dprintk("%s\n", __func__
);
265 if (fe
->ops
.tuner_ops
.set_params
) {
266 if (fe
->ops
.i2c_gate_ctrl
)
267 fe
->ops
.i2c_gate_ctrl(fe
, 1);
268 fe
->ops
.tuner_ops
.set_params(fe
);
269 if (fe
->ops
.i2c_gate_ctrl
)
270 fe
->ops
.i2c_gate_ctrl(fe
, 0);
273 /* start auto lock */
274 for (i
= 0; i
< 10; i
++) {
276 dprintk("Try %d\n", i
);
277 is_locked(priv
, &locked
);
279 dprintk("ATBM8830 locked!\n");
287 static int atbm8830_get_fe(struct dvb_frontend
*fe
,
288 struct dtv_frontend_properties
*c
)
290 dprintk("%s\n", __func__
);
292 /* TODO: get real readings from device */
293 /* inversion status */
294 c
->inversion
= INVERSION_OFF
;
297 c
->bandwidth_hz
= 8000000;
299 c
->code_rate_HP
= FEC_AUTO
;
300 c
->code_rate_LP
= FEC_AUTO
;
302 c
->modulation
= QAM_AUTO
;
304 /* transmission mode */
305 c
->transmission_mode
= TRANSMISSION_MODE_AUTO
;
308 c
->guard_interval
= GUARD_INTERVAL_AUTO
;
311 c
->hierarchy
= HIERARCHY_NONE
;
316 static int atbm8830_get_tune_settings(struct dvb_frontend
*fe
,
317 struct dvb_frontend_tune_settings
*fesettings
)
319 fesettings
->min_delay_ms
= 0;
320 fesettings
->step_size
= 0;
321 fesettings
->max_drift
= 0;
325 static int atbm8830_read_status(struct dvb_frontend
*fe
,
326 enum fe_status
*fe_status
)
328 struct atbm_state
*priv
= fe
->demodulator_priv
;
332 dprintk("%s\n", __func__
);
335 is_locked(priv
, &locked
);
337 *fe_status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
338 FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK
;
340 dprintk("%s: fe_status=0x%x\n", __func__
, *fe_status
);
342 atbm8830_read_reg(priv
, REG_AGC_LOCK
, &agc_locked
);
343 dprintk("AGC Lock: %d\n", agc_locked
);
348 static int atbm8830_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
350 struct atbm_state
*priv
= fe
->demodulator_priv
;
354 dprintk("%s\n", __func__
);
356 atbm8830_reglatch_lock(priv
, 1);
358 atbm8830_read_reg(priv
, REG_FRAME_ERR_CNT
+ 1, &t
);
359 frame_err
= t
& 0x7F;
361 atbm8830_read_reg(priv
, REG_FRAME_ERR_CNT
, &t
);
364 atbm8830_reglatch_lock(priv
, 0);
366 *ber
= frame_err
* 100 / 32767;
368 dprintk("%s: ber=0x%x\n", __func__
, *ber
);
372 static int atbm8830_read_signal_strength(struct dvb_frontend
*fe
, u16
*signal
)
374 struct atbm_state
*priv
= fe
->demodulator_priv
;
378 dprintk("%s\n", __func__
);
379 atbm8830_reglatch_lock(priv
, 1);
381 atbm8830_read_reg(priv
, REG_AGC_PWM_VAL
+ 1, &t
);
384 atbm8830_read_reg(priv
, REG_AGC_PWM_VAL
, &t
);
387 atbm8830_reglatch_lock(priv
, 0);
389 dprintk("AGC PWM = 0x%02X\n", pwm
);
392 *signal
= pwm
* 0x10000 / 0x400;
397 static int atbm8830_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
399 dprintk("%s\n", __func__
);
404 static int atbm8830_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
406 dprintk("%s\n", __func__
);
411 static int atbm8830_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
413 struct atbm_state
*priv
= fe
->demodulator_priv
;
415 return atbm8830_write_reg(priv
, REG_I2C_GATE
, enable
? 1 : 0);
418 static const struct dvb_frontend_ops atbm8830_ops
= {
419 .delsys
= { SYS_DTMB
},
421 .name
= "AltoBeam ATBM8830/8831 DMB-TH",
422 .frequency_min_hz
= 474 * MHz
,
423 .frequency_max_hz
= 858 * MHz
,
424 .frequency_stepsize_hz
= 10 * kHz
,
428 FE_CAN_TRANSMISSION_MODE_AUTO
|
429 FE_CAN_GUARD_INTERVAL_AUTO
432 .release
= atbm8830_release
,
434 .init
= atbm8830_init
,
437 .i2c_gate_ctrl
= atbm8830_i2c_gate_ctrl
,
439 .set_frontend
= atbm8830_set_fe
,
440 .get_frontend
= atbm8830_get_fe
,
441 .get_tune_settings
= atbm8830_get_tune_settings
,
443 .read_status
= atbm8830_read_status
,
444 .read_ber
= atbm8830_read_ber
,
445 .read_signal_strength
= atbm8830_read_signal_strength
,
446 .read_snr
= atbm8830_read_snr
,
447 .read_ucblocks
= atbm8830_read_ucblocks
,
450 struct dvb_frontend
*atbm8830_attach(const struct atbm8830_config
*config
,
451 struct i2c_adapter
*i2c
)
453 struct atbm_state
*priv
= NULL
;
456 dprintk("%s()\n", __func__
);
458 if (config
== NULL
|| i2c
== NULL
)
461 priv
= kzalloc(sizeof(struct atbm_state
), GFP_KERNEL
);
465 priv
->config
= config
;
468 /* check if the demod is there */
469 if (atbm8830_read_reg(priv
, REG_CHIP_ID
, &data
) != 0) {
470 dprintk("%s atbm8830/8831 not found at i2c addr 0x%02X\n",
471 __func__
, priv
->config
->demod_address
);
474 dprintk("atbm8830 chip id: 0x%02X\n", data
);
476 memcpy(&priv
->frontend
.ops
, &atbm8830_ops
,
477 sizeof(struct dvb_frontend_ops
));
478 priv
->frontend
.demodulator_priv
= priv
;
480 atbm8830_init(&priv
->frontend
);
482 atbm8830_i2c_gate_ctrl(&priv
->frontend
, 1);
484 return &priv
->frontend
;
487 dprintk("%s() error_out\n", __func__
);
492 EXPORT_SYMBOL(atbm8830_attach
);
494 MODULE_DESCRIPTION("AltoBeam ATBM8830/8831 GB20600 demodulator driver");
495 MODULE_AUTHOR("David T. L. Wong <davidtlwong@gmail.com>");
496 MODULE_LICENSE("GPL");