1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * E3C EC100 demodulator driver
5 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
8 #include <media/dvb_frontend.h>
12 struct i2c_adapter
*i2c
;
13 struct dvb_frontend frontend
;
14 struct ec100_config config
;
19 /* write single register */
20 static int ec100_write_reg(struct ec100_state
*state
, u8 reg
, u8 val
)
23 u8 buf
[2] = {reg
, val
};
24 struct i2c_msg msg
[1] = {
26 .addr
= state
->config
.demod_address
,
33 ret
= i2c_transfer(state
->i2c
, msg
, 1);
37 dev_warn(&state
->i2c
->dev
, "%s: i2c wr failed=%d reg=%02x\n",
38 KBUILD_MODNAME
, ret
, reg
);
45 /* read single register */
46 static int ec100_read_reg(struct ec100_state
*state
, u8 reg
, u8
*val
)
49 struct i2c_msg msg
[2] = {
51 .addr
= state
->config
.demod_address
,
56 .addr
= state
->config
.demod_address
,
63 ret
= i2c_transfer(state
->i2c
, msg
, 2);
67 dev_warn(&state
->i2c
->dev
, "%s: i2c rd failed=%d reg=%02x\n",
68 KBUILD_MODNAME
, ret
, reg
);
75 static int ec100_set_frontend(struct dvb_frontend
*fe
)
77 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
78 struct ec100_state
*state
= fe
->demodulator_priv
;
82 dev_dbg(&state
->i2c
->dev
, "%s: frequency=%d bandwidth_hz=%d\n",
83 __func__
, c
->frequency
, c
->bandwidth_hz
);
86 if (fe
->ops
.tuner_ops
.set_params
)
87 fe
->ops
.tuner_ops
.set_params(fe
);
89 ret
= ec100_write_reg(state
, 0x04, 0x06);
92 ret
= ec100_write_reg(state
, 0x67, 0x58);
95 ret
= ec100_write_reg(state
, 0x05, 0x18);
100 -------+------+------+------
101 A 0x1b | 0xa1 | 0xe7 | 0x2c
102 A 0x1c | 0x55 | 0x63 | 0x72
103 -------+------+------+------
104 B 0x1b | 0xb7 | 0x00 | 0x49
105 B 0x1c | 0x55 | 0x64 | 0x72 */
107 switch (c
->bandwidth_hz
) {
122 ret
= ec100_write_reg(state
, 0x1b, tmp
);
125 ret
= ec100_write_reg(state
, 0x1c, tmp2
);
129 ret
= ec100_write_reg(state
, 0x0c, 0xbb); /* if freq */
132 ret
= ec100_write_reg(state
, 0x0d, 0x31); /* if freq */
136 ret
= ec100_write_reg(state
, 0x08, 0x24);
140 ret
= ec100_write_reg(state
, 0x00, 0x00); /* go */
143 ret
= ec100_write_reg(state
, 0x00, 0x20); /* go */
149 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
153 static int ec100_get_tune_settings(struct dvb_frontend
*fe
,
154 struct dvb_frontend_tune_settings
*fesettings
)
156 fesettings
->min_delay_ms
= 300;
157 fesettings
->step_size
= 0;
158 fesettings
->max_drift
= 0;
163 static int ec100_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
165 struct ec100_state
*state
= fe
->demodulator_priv
;
170 ret
= ec100_read_reg(state
, 0x42, &tmp
);
175 /* bit7 set - have lock */
176 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
|
177 FE_HAS_SYNC
| FE_HAS_LOCK
;
179 ret
= ec100_read_reg(state
, 0x01, &tmp
);
184 /* bit4 set - have signal */
185 *status
|= FE_HAS_SIGNAL
;
187 /* bit0 clear - have ~valid signal */
188 *status
|= FE_HAS_CARRIER
| FE_HAS_VITERBI
;
195 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
199 static int ec100_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
201 struct ec100_state
*state
= fe
->demodulator_priv
;
208 ret
= ec100_read_reg(state
, 0x65, &tmp
);
211 ret
= ec100_read_reg(state
, 0x66, &tmp2
);
215 ber2
= (tmp2
<< 8) | tmp
;
217 /* if counter overflow or clear */
218 if (ber2
< state
->ber
)
221 *ber
= ber2
- state
->ber
;
227 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
231 static int ec100_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
233 struct ec100_state
*state
= fe
->demodulator_priv
;
237 ret
= ec100_read_reg(state
, 0x24, &tmp
);
243 *strength
= ((tmp
<< 8) | tmp
);
247 dev_dbg(&state
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
251 static int ec100_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
257 static int ec100_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
263 static void ec100_release(struct dvb_frontend
*fe
)
265 struct ec100_state
*state
= fe
->demodulator_priv
;
269 static const struct dvb_frontend_ops ec100_ops
;
271 struct dvb_frontend
*ec100_attach(const struct ec100_config
*config
,
272 struct i2c_adapter
*i2c
)
275 struct ec100_state
*state
= NULL
;
278 /* allocate memory for the internal state */
279 state
= kzalloc(sizeof(struct ec100_state
), GFP_KERNEL
);
283 /* setup the state */
285 memcpy(&state
->config
, config
, sizeof(struct ec100_config
));
287 /* check if the demod is there */
288 ret
= ec100_read_reg(state
, 0x33, &tmp
);
289 if (ret
|| tmp
!= 0x0b)
292 /* create dvb_frontend */
293 memcpy(&state
->frontend
.ops
, &ec100_ops
,
294 sizeof(struct dvb_frontend_ops
));
295 state
->frontend
.demodulator_priv
= state
;
297 return &state
->frontend
;
302 EXPORT_SYMBOL(ec100_attach
);
304 static const struct dvb_frontend_ops ec100_ops
= {
305 .delsys
= { SYS_DVBT
},
307 .name
= "E3C EC100 DVB-T",
309 FE_CAN_FEC_1_2
| FE_CAN_FEC_2_3
| FE_CAN_FEC_3_4
|
310 FE_CAN_FEC_5_6
| FE_CAN_FEC_7_8
| FE_CAN_FEC_AUTO
|
311 FE_CAN_QPSK
| FE_CAN_QAM_16
|
312 FE_CAN_QAM_64
| FE_CAN_QAM_AUTO
|
313 FE_CAN_TRANSMISSION_MODE_AUTO
|
314 FE_CAN_GUARD_INTERVAL_AUTO
|
315 FE_CAN_HIERARCHY_AUTO
|
319 .release
= ec100_release
,
320 .set_frontend
= ec100_set_frontend
,
321 .get_tune_settings
= ec100_get_tune_settings
,
322 .read_status
= ec100_read_status
,
323 .read_ber
= ec100_read_ber
,
324 .read_signal_strength
= ec100_read_signal_strength
,
325 .read_snr
= ec100_read_snr
,
326 .read_ucblocks
= ec100_read_ucblocks
,
329 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
330 MODULE_DESCRIPTION("E3C EC100 DVB-T demodulator driver");
331 MODULE_LICENSE("GPL");