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_t(struct dvb_frontend
*fe
)
26 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
27 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
32 u8 bw_params1
[][5] = {
33 { 0x17, 0xea, 0xaa, 0xaa, 0xaa }, /* 6 MHz */
34 { 0x14, 0x80, 0x00, 0x00, 0x00 }, /* 7 MHz */
35 { 0x11, 0xf0, 0x00, 0x00, 0x00 }, /* 8 MHz */
37 u8 bw_params2
[][2] = {
38 { 0x1f, 0xdc }, /* 6 MHz */
39 { 0x12, 0xf8 }, /* 7 MHz */
40 { 0x01, 0xe0 }, /* 8 MHz */
42 struct reg_val_mask tab
[] = {
43 { 0x00080, 0x00, 0xff },
44 { 0x00081, 0x03, 0xff },
45 { 0x00085, 0x07, 0xff },
46 { 0x00088, 0x01, 0xff },
48 { 0x00070, priv
->cfg
.ts_mode
, 0xff },
49 { 0x000cb, priv
->cfg
.if_agc_polarity
<< 6, 0x40 },
50 { 0x000a5, 0x00, 0x01 },
51 { 0x00082, 0x20, 0x60 },
52 { 0x000c2, 0xc3, 0xff },
53 { 0x0016a, 0x50, 0xff },
54 { 0x00427, 0x41, 0xff },
57 dbg("%s: RF=%d BW=%d", __func__
, c
->frequency
, c
->bandwidth_hz
);
59 switch (c
->bandwidth_hz
) {
77 ret
= cxd2820r_gpio(fe
);
82 if (fe
->ops
.tuner_ops
.set_params
)
83 fe
->ops
.tuner_ops
.set_params(fe
);
85 if (priv
->delivery_system
!= SYS_DVBT
) {
86 for (i
= 0; i
< ARRAY_SIZE(tab
); i
++) {
87 ret
= cxd2820r_wr_reg_mask(priv
, tab
[i
].reg
,
88 tab
[i
].val
, tab
[i
].mask
);
94 priv
->delivery_system
= SYS_DVBT
;
95 priv
->ber_running
= 0; /* tune stops BER counter */
97 /* program IF frequency */
98 if (fe
->ops
.tuner_ops
.get_if_frequency
) {
99 ret
= fe
->ops
.tuner_ops
.get_if_frequency(fe
, &if_freq
);
105 dbg("%s: if_freq=%d", __func__
, if_freq
);
107 num
= if_freq
/ 1000; /* Hz => kHz */
109 if_ctl
= cxd2820r_div_u64_round_closest(num
, 41000);
110 buf
[0] = ((if_ctl
>> 16) & 0xff);
111 buf
[1] = ((if_ctl
>> 8) & 0xff);
112 buf
[2] = ((if_ctl
>> 0) & 0xff);
114 ret
= cxd2820r_wr_regs(priv
, 0x000b6, buf
, 3);
118 ret
= cxd2820r_wr_regs(priv
, 0x0009f, bw_params1
[bw_i
], 5);
122 ret
= cxd2820r_wr_reg_mask(priv
, 0x000d7, bw_param
<< 6, 0xc0);
126 ret
= cxd2820r_wr_regs(priv
, 0x000d9, bw_params2
[bw_i
], 2);
130 ret
= cxd2820r_wr_reg(priv
, 0x000ff, 0x08);
134 ret
= cxd2820r_wr_reg(priv
, 0x000fe, 0x01);
140 dbg("%s: failed:%d", __func__
, ret
);
144 int cxd2820r_get_frontend_t(struct dvb_frontend
*fe
)
146 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
147 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
151 ret
= cxd2820r_rd_regs(priv
, 0x0002f, buf
, sizeof(buf
));
155 switch ((buf
[0] >> 6) & 0x03) {
157 c
->modulation
= QPSK
;
160 c
->modulation
= QAM_16
;
163 c
->modulation
= QAM_64
;
167 switch ((buf
[1] >> 1) & 0x03) {
169 c
->transmission_mode
= TRANSMISSION_MODE_2K
;
172 c
->transmission_mode
= TRANSMISSION_MODE_8K
;
176 switch ((buf
[1] >> 3) & 0x03) {
178 c
->guard_interval
= GUARD_INTERVAL_1_32
;
181 c
->guard_interval
= GUARD_INTERVAL_1_16
;
184 c
->guard_interval
= GUARD_INTERVAL_1_8
;
187 c
->guard_interval
= GUARD_INTERVAL_1_4
;
191 switch ((buf
[0] >> 3) & 0x07) {
193 c
->hierarchy
= HIERARCHY_NONE
;
196 c
->hierarchy
= HIERARCHY_1
;
199 c
->hierarchy
= HIERARCHY_2
;
202 c
->hierarchy
= HIERARCHY_4
;
206 switch ((buf
[0] >> 0) & 0x07) {
208 c
->code_rate_HP
= FEC_1_2
;
211 c
->code_rate_HP
= FEC_2_3
;
214 c
->code_rate_HP
= FEC_3_4
;
217 c
->code_rate_HP
= FEC_5_6
;
220 c
->code_rate_HP
= FEC_7_8
;
224 switch ((buf
[1] >> 5) & 0x07) {
226 c
->code_rate_LP
= FEC_1_2
;
229 c
->code_rate_LP
= FEC_2_3
;
232 c
->code_rate_LP
= FEC_3_4
;
235 c
->code_rate_LP
= FEC_5_6
;
238 c
->code_rate_LP
= FEC_7_8
;
242 ret
= cxd2820r_rd_reg(priv
, 0x007c6, &buf
[0]);
246 switch ((buf
[0] >> 0) & 0x01) {
248 c
->inversion
= INVERSION_OFF
;
251 c
->inversion
= INVERSION_ON
;
257 dbg("%s: failed:%d", __func__
, ret
);
261 int cxd2820r_read_ber_t(struct dvb_frontend
*fe
, u32
*ber
)
263 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
265 u8 buf
[3], start_ber
= 0;
268 if (priv
->ber_running
) {
269 ret
= cxd2820r_rd_regs(priv
, 0x00076, buf
, sizeof(buf
));
273 if ((buf
[2] >> 7) & 0x01 || (buf
[2] >> 4) & 0x01) {
274 *ber
= (buf
[2] & 0x0f) << 16 | buf
[1] << 8 | buf
[0];
278 priv
->ber_running
= 1;
284 ret
= cxd2820r_wr_reg(priv
, 0x00079, 0x01);
291 dbg("%s: failed:%d", __func__
, ret
);
295 int cxd2820r_read_signal_strength_t(struct dvb_frontend
*fe
,
298 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
303 ret
= cxd2820r_rd_regs(priv
, 0x00026, buf
, sizeof(buf
));
307 tmp
= (buf
[0] & 0x0f) << 8 | buf
[1];
310 /* scale value to 0x0000-0xffff from 0x0000-0x0fff */
311 *strength
= tmp
* 0xffff / 0x0fff;
315 dbg("%s: failed:%d", __func__
, ret
);
319 int cxd2820r_read_snr_t(struct dvb_frontend
*fe
, u16
*snr
)
321 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
325 /* report SNR in dB * 10 */
327 ret
= cxd2820r_rd_regs(priv
, 0x00028, buf
, sizeof(buf
));
331 tmp
= (buf
[0] & 0x1f) << 8 | buf
[1];
332 #define CXD2820R_LOG10_8_24 15151336 /* log10(8) << 24 */
334 *snr
= (intlog10(tmp
) - CXD2820R_LOG10_8_24
) / ((1 << 24)
339 dbg("%s: dBx10=%d val=%04x", __func__
, *snr
, tmp
);
343 dbg("%s: failed:%d", __func__
, ret
);
347 int cxd2820r_read_ucblocks_t(struct dvb_frontend
*fe
, u32
*ucblocks
)
350 /* no way to read ? */
354 int cxd2820r_read_status_t(struct dvb_frontend
*fe
, fe_status_t
*status
)
356 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
361 ret
= cxd2820r_rd_reg(priv
, 0x00010, &buf
[0]);
365 if ((buf
[0] & 0x07) == 6) {
366 ret
= cxd2820r_rd_reg(priv
, 0x00073, &buf
[1]);
370 if (((buf
[1] >> 3) & 0x01) == 1) {
371 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
372 FE_HAS_VITERBI
| FE_HAS_SYNC
| FE_HAS_LOCK
;
374 *status
|= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
375 FE_HAS_VITERBI
| FE_HAS_SYNC
;
378 ret
= cxd2820r_rd_reg(priv
, 0x00014, &buf
[2]);
382 if ((buf
[2] & 0x0f) >= 4) {
383 ret
= cxd2820r_rd_reg(priv
, 0x00a14, &buf
[3]);
387 if (((buf
[3] >> 4) & 0x01) == 1)
388 *status
|= FE_HAS_SIGNAL
;
392 dbg("%s: lock=%02x %02x %02x %02x", __func__
,
393 buf
[0], buf
[1], buf
[2], buf
[3]);
397 dbg("%s: failed:%d", __func__
, ret
);
401 int cxd2820r_init_t(struct dvb_frontend
*fe
)
403 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
406 ret
= cxd2820r_wr_reg(priv
, 0x00085, 0x07);
412 dbg("%s: failed:%d", __func__
, ret
);
416 int cxd2820r_sleep_t(struct dvb_frontend
*fe
)
418 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
420 struct reg_val_mask tab
[] = {
421 { 0x000ff, 0x1f, 0xff },
422 { 0x00085, 0x00, 0xff },
423 { 0x00088, 0x01, 0xff },
424 { 0x00081, 0x00, 0xff },
425 { 0x00080, 0x00, 0xff },
430 priv
->delivery_system
= SYS_UNDEFINED
;
432 for (i
= 0; i
< ARRAY_SIZE(tab
); i
++) {
433 ret
= cxd2820r_wr_reg_mask(priv
, tab
[i
].reg
, tab
[i
].val
,
441 dbg("%s: failed:%d", __func__
, ret
);
445 int cxd2820r_get_tune_settings_t(struct dvb_frontend
*fe
,
446 struct dvb_frontend_tune_settings
*s
)
448 s
->min_delay_ms
= 500;
449 s
->step_size
= fe
->ops
.info
.frequency_stepsize
* 2;
450 s
->max_drift
= (fe
->ops
.info
.frequency_stepsize
* 2) + 1;