2 * Sony CXD2820R demodulator driver
4 * Copyright (C) 2010 Antti Palosaari <crope@iki.fi>
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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "cxd2820r_priv.h"
24 int cxd2820r_set_frontend_c(struct dvb_frontend
*fe
)
26 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
27 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
33 struct reg_val_mask tab
[] = {
34 { 0x00080, 0x01, 0xff },
35 { 0x00081, 0x05, 0xff },
36 { 0x00085, 0x07, 0xff },
37 { 0x00088, 0x01, 0xff },
39 { 0x00082, 0x20, 0x60 },
40 { 0x1016a, 0x48, 0xff },
41 { 0x100a5, 0x00, 0x01 },
42 { 0x10020, 0x06, 0x07 },
43 { 0x10059, 0x50, 0xff },
44 { 0x10087, 0x0c, 0x3c },
45 { 0x1008b, 0x07, 0xff },
46 { 0x1001f, priv
->cfg
.if_agc_polarity
<< 7, 0x80 },
47 { 0x10070, priv
->cfg
.ts_mode
, 0xff },
48 { 0x10071, !priv
->cfg
.ts_clock_inv
<< 4, 0x10 },
51 dev_dbg(&priv
->i2c
->dev
, "%s: frequency=%d symbol_rate=%d\n", __func__
,
52 c
->frequency
, c
->symbol_rate
);
55 if (fe
->ops
.tuner_ops
.set_params
)
56 fe
->ops
.tuner_ops
.set_params(fe
);
58 if (priv
->delivery_system
!= SYS_DVBC_ANNEX_A
) {
59 for (i
= 0; i
< ARRAY_SIZE(tab
); i
++) {
60 ret
= cxd2820r_wr_reg_mask(priv
, tab
[i
].reg
,
61 tab
[i
].val
, tab
[i
].mask
);
67 priv
->delivery_system
= SYS_DVBC_ANNEX_A
;
68 priv
->ber_running
= false; /* tune stops BER counter */
70 /* program IF frequency */
71 if (fe
->ops
.tuner_ops
.get_if_frequency
) {
72 ret
= fe
->ops
.tuner_ops
.get_if_frequency(fe
, &if_freq
);
78 dev_dbg(&priv
->i2c
->dev
, "%s: if_freq=%d\n", __func__
, if_freq
);
80 num
= if_freq
/ 1000; /* Hz => kHz */
82 if_ctl
= 0x4000 - DIV_ROUND_CLOSEST_ULL(num
, 41000);
83 buf
[0] = (if_ctl
>> 8) & 0x3f;
84 buf
[1] = (if_ctl
>> 0) & 0xff;
86 ret
= cxd2820r_wr_regs(priv
, 0x10042, buf
, 2);
90 ret
= cxd2820r_wr_reg(priv
, 0x000ff, 0x08);
94 ret
= cxd2820r_wr_reg(priv
, 0x000fe, 0x01);
100 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
104 int cxd2820r_get_frontend_c(struct dvb_frontend
*fe
)
106 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
107 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
111 ret
= cxd2820r_rd_regs(priv
, 0x1001a, buf
, 2);
115 c
->symbol_rate
= 2500 * ((buf
[0] & 0x0f) << 8 | buf
[1]);
117 ret
= cxd2820r_rd_reg(priv
, 0x10019, &buf
[0]);
121 switch ((buf
[0] >> 0) & 0x07) {
123 c
->modulation
= QAM_16
;
126 c
->modulation
= QAM_32
;
129 c
->modulation
= QAM_64
;
132 c
->modulation
= QAM_128
;
135 c
->modulation
= QAM_256
;
139 switch ((buf
[0] >> 7) & 0x01) {
141 c
->inversion
= INVERSION_OFF
;
144 c
->inversion
= INVERSION_ON
;
150 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
154 int cxd2820r_read_ber_c(struct dvb_frontend
*fe
, u32
*ber
)
156 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
158 u8 buf
[3], start_ber
= 0;
161 if (priv
->ber_running
) {
162 ret
= cxd2820r_rd_regs(priv
, 0x10076, buf
, sizeof(buf
));
166 if ((buf
[2] >> 7) & 0x01 || (buf
[2] >> 4) & 0x01) {
167 *ber
= (buf
[2] & 0x0f) << 16 | buf
[1] << 8 | buf
[0];
171 priv
->ber_running
= true;
177 ret
= cxd2820r_wr_reg(priv
, 0x10079, 0x01);
184 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
188 int cxd2820r_read_signal_strength_c(struct dvb_frontend
*fe
,
191 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
196 ret
= cxd2820r_rd_regs(priv
, 0x10049, buf
, sizeof(buf
));
200 tmp
= (buf
[0] & 0x03) << 8 | buf
[1];
201 tmp
= (~tmp
& 0x03ff);
209 /* scale value to 0x0000-0xffff */
210 *strength
= tmp
* 0xffff / (350-0);
214 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
218 int cxd2820r_read_snr_c(struct dvb_frontend
*fe
, u16
*snr
)
220 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
224 /* report SNR in dB * 10 */
226 ret
= cxd2820r_rd_reg(priv
, 0x10019, &tmp
);
230 if (((tmp
>> 0) & 0x03) % 2) {
238 ret
= cxd2820r_rd_reg(priv
, 0x1004d, &tmp
);
242 #define CXD2820R_LOG2_E_24 24204406 /* log2(e) << 24 */
244 *snr
= A
* (intlog2(B
/ tmp
) >> 5) / (CXD2820R_LOG2_E_24
>> 5)
251 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
255 int cxd2820r_read_ucblocks_c(struct dvb_frontend
*fe
, u32
*ucblocks
)
258 /* no way to read ? */
262 int cxd2820r_read_status_c(struct dvb_frontend
*fe
, enum fe_status
*status
)
264 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
269 ret
= cxd2820r_rd_regs(priv
, 0x10088, buf
, sizeof(buf
));
273 if (((buf
[0] >> 0) & 0x01) == 1) {
274 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
275 FE_HAS_VITERBI
| FE_HAS_SYNC
;
277 if (((buf
[1] >> 3) & 0x01) == 1) {
278 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
279 FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK
;
283 dev_dbg(&priv
->i2c
->dev
, "%s: lock=%02x %02x\n", __func__
, buf
[0],
288 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
292 int cxd2820r_init_c(struct dvb_frontend
*fe
)
294 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
297 ret
= cxd2820r_wr_reg(priv
, 0x00085, 0x07);
303 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
307 int cxd2820r_sleep_c(struct dvb_frontend
*fe
)
309 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
311 struct reg_val_mask tab
[] = {
312 { 0x000ff, 0x1f, 0xff },
313 { 0x00085, 0x00, 0xff },
314 { 0x00088, 0x01, 0xff },
315 { 0x00081, 0x00, 0xff },
316 { 0x00080, 0x00, 0xff },
319 dev_dbg(&priv
->i2c
->dev
, "%s\n", __func__
);
321 priv
->delivery_system
= SYS_UNDEFINED
;
323 for (i
= 0; i
< ARRAY_SIZE(tab
); i
++) {
324 ret
= cxd2820r_wr_reg_mask(priv
, tab
[i
].reg
, tab
[i
].val
,
332 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
336 int cxd2820r_get_tune_settings_c(struct dvb_frontend
*fe
,
337 struct dvb_frontend_tune_settings
*s
)
339 s
->min_delay_ms
= 500;
340 s
->step_size
= 0; /* no zigzag */