cxd2820r_c.c revision fda23faaff3b28a987c22da5f3a17b9f3d4acef8
1/*
2 * Sony CXD2820R demodulator driver
3 *
4 * Copyright (C) 2010 Antti Palosaari <crope@iki.fi>
5 *
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.
10 *
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.
15 *
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.
19 */
20
21
22#include "cxd2820r_priv.h"
23
24int cxd2820r_set_frontend_c(struct dvb_frontend *fe,
25	struct dvb_frontend_parameters *params)
26{
27	struct cxd2820r_priv *priv = fe->demodulator_priv;
28	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
29	int ret, i;
30	u8 buf[2];
31	u32 if_freq;
32	u16 if_ctl;
33	u64 num;
34	struct reg_val_mask tab[] = {
35		{ 0x00080, 0x01, 0xff },
36		{ 0x00081, 0x05, 0xff },
37		{ 0x00085, 0x07, 0xff },
38		{ 0x00088, 0x01, 0xff },
39
40		{ 0x00082, 0x20, 0x60 },
41		{ 0x1016a, 0x48, 0xff },
42		{ 0x100a5, 0x00, 0x01 },
43		{ 0x10020, 0x06, 0x07 },
44		{ 0x10059, 0x50, 0xff },
45		{ 0x10087, 0x0c, 0x3c },
46		{ 0x1008b, 0x07, 0xff },
47		{ 0x1001f, priv->cfg.if_agc_polarity << 7, 0x80 },
48		{ 0x10070, priv->cfg.ts_mode, 0xff },
49	};
50
51	dbg("%s: RF=%d SR=%d", __func__, c->frequency, c->symbol_rate);
52
53	/* update GPIOs */
54	ret = cxd2820r_gpio(fe);
55	if (ret)
56		goto error;
57
58	/* program tuner */
59	if (fe->ops.tuner_ops.set_params)
60		fe->ops.tuner_ops.set_params(fe, params);
61
62	if (priv->delivery_system !=  SYS_DVBC_ANNEX_AC) {
63		for (i = 0; i < ARRAY_SIZE(tab); i++) {
64			ret = cxd2820r_wr_reg_mask(priv, tab[i].reg,
65				tab[i].val, tab[i].mask);
66			if (ret)
67				goto error;
68		}
69	}
70
71	priv->delivery_system = SYS_DVBC_ANNEX_AC;
72	priv->ber_running = 0; /* tune stops BER counter */
73
74	/* program IF frequency */
75	if (fe->ops.tuner_ops.get_if_frequency) {
76		ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
77		if (ret)
78			goto error;
79	} else
80		if_freq = 0;
81
82	dbg("%s: if_freq=%d", __func__, if_freq);
83
84	num = if_freq / 1000; /* Hz => kHz */
85	num *= 0x4000;
86	if_ctl = cxd2820r_div_u64_round_closest(num, 41000);
87	buf[0] = (if_ctl >> 8) & 0x3f;
88	buf[1] = (if_ctl >> 0) & 0xff;
89
90	ret = cxd2820r_wr_regs(priv, 0x10042, buf, 2);
91	if (ret)
92		goto error;
93
94	ret = cxd2820r_wr_reg(priv, 0x000ff, 0x08);
95	if (ret)
96		goto error;
97
98	ret = cxd2820r_wr_reg(priv, 0x000fe, 0x01);
99	if (ret)
100		goto error;
101
102	return ret;
103error:
104	dbg("%s: failed:%d", __func__, ret);
105	return ret;
106}
107
108int cxd2820r_get_frontend_c(struct dvb_frontend *fe,
109	struct dvb_frontend_parameters *p)
110{
111	struct cxd2820r_priv *priv = fe->demodulator_priv;
112	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
113	int ret;
114	u8 buf[2];
115
116	ret = cxd2820r_rd_regs(priv, 0x1001a, buf, 2);
117	if (ret)
118		goto error;
119
120	c->symbol_rate = 2500 * ((buf[0] & 0x0f) << 8 | buf[1]);
121
122	ret = cxd2820r_rd_reg(priv, 0x10019, &buf[0]);
123	if (ret)
124		goto error;
125
126	switch ((buf[0] >> 0) & 0x03) {
127	case 0:
128		c->modulation = QAM_16;
129		break;
130	case 1:
131		c->modulation = QAM_32;
132		break;
133	case 2:
134		c->modulation = QAM_64;
135		break;
136	case 3:
137		c->modulation = QAM_128;
138		break;
139	case 4:
140		c->modulation = QAM_256;
141		break;
142	}
143
144	switch ((buf[0] >> 7) & 0x01) {
145	case 0:
146		c->inversion = INVERSION_OFF;
147		break;
148	case 1:
149		c->inversion = INVERSION_ON;
150		break;
151	}
152
153	return ret;
154error:
155	dbg("%s: failed:%d", __func__, ret);
156	return ret;
157}
158
159int cxd2820r_read_ber_c(struct dvb_frontend *fe, u32 *ber)
160{
161	struct cxd2820r_priv *priv = fe->demodulator_priv;
162	int ret;
163	u8 buf[3], start_ber = 0;
164	*ber = 0;
165
166	if (priv->ber_running) {
167		ret = cxd2820r_rd_regs(priv, 0x10076, buf, sizeof(buf));
168		if (ret)
169			goto error;
170
171		if ((buf[2] >> 7) & 0x01 || (buf[2] >> 4) & 0x01) {
172			*ber = (buf[2] & 0x0f) << 16 | buf[1] << 8 | buf[0];
173			start_ber = 1;
174		}
175	} else {
176		priv->ber_running = 1;
177		start_ber = 1;
178	}
179
180	if (start_ber) {
181		/* (re)start BER */
182		ret = cxd2820r_wr_reg(priv, 0x10079, 0x01);
183		if (ret)
184			goto error;
185	}
186
187	return ret;
188error:
189	dbg("%s: failed:%d", __func__, ret);
190	return ret;
191}
192
193int cxd2820r_read_signal_strength_c(struct dvb_frontend *fe,
194	u16 *strength)
195{
196	struct cxd2820r_priv *priv = fe->demodulator_priv;
197	int ret;
198	u8 buf[2];
199	u16 tmp;
200
201	ret = cxd2820r_rd_regs(priv, 0x10049, buf, sizeof(buf));
202	if (ret)
203		goto error;
204
205	tmp = (buf[0] & 0x03) << 8 | buf[1];
206	tmp = (~tmp & 0x03ff);
207
208	if (tmp == 512)
209		/* ~no signal */
210		tmp = 0;
211	else if (tmp > 350)
212		tmp = 350;
213
214	/* scale value to 0x0000-0xffff */
215	*strength = tmp * 0xffff / (350-0);
216
217	return ret;
218error:
219	dbg("%s: failed:%d", __func__, ret);
220	return ret;
221}
222
223int cxd2820r_read_snr_c(struct dvb_frontend *fe, u16 *snr)
224{
225	struct cxd2820r_priv *priv = fe->demodulator_priv;
226	int ret;
227	u8 tmp;
228	unsigned int A, B;
229	/* report SNR in dB * 10 */
230
231	ret = cxd2820r_rd_reg(priv, 0x10019, &tmp);
232	if (ret)
233		goto error;
234
235	if (((tmp >> 0) & 0x03) % 2) {
236		A = 875;
237		B = 650;
238	} else {
239		A = 950;
240		B = 760;
241	}
242
243	ret = cxd2820r_rd_reg(priv, 0x1004d, &tmp);
244	if (ret)
245		goto error;
246
247	#define CXD2820R_LOG2_E_24 24204406 /* log2(e) << 24 */
248	if (tmp)
249		*snr = A * (intlog2(B / tmp) >> 5) / (CXD2820R_LOG2_E_24 >> 5)
250			/ 10;
251	else
252		*snr = 0;
253
254	return ret;
255error:
256	dbg("%s: failed:%d", __func__, ret);
257	return ret;
258}
259
260int cxd2820r_read_ucblocks_c(struct dvb_frontend *fe, u32 *ucblocks)
261{
262	*ucblocks = 0;
263	/* no way to read ? */
264	return 0;
265}
266
267int cxd2820r_read_status_c(struct dvb_frontend *fe, fe_status_t *status)
268{
269	struct cxd2820r_priv *priv = fe->demodulator_priv;
270	int ret;
271	u8 buf[2];
272	*status = 0;
273
274	ret = cxd2820r_rd_regs(priv, 0x10088, buf, sizeof(buf));
275	if (ret)
276		goto error;
277
278	if (((buf[0] >> 0) & 0x01) == 1) {
279		*status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
280			FE_HAS_VITERBI | FE_HAS_SYNC;
281
282		if (((buf[1] >> 3) & 0x01) == 1) {
283			*status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
284				FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
285		}
286	}
287
288	dbg("%s: lock=%02x %02x", __func__, buf[0], buf[1]);
289
290	return ret;
291error:
292	dbg("%s: failed:%d", __func__, ret);
293	return ret;
294}
295
296int cxd2820r_init_c(struct dvb_frontend *fe)
297{
298	struct cxd2820r_priv *priv = fe->demodulator_priv;
299	int ret;
300
301	ret = cxd2820r_wr_reg(priv, 0x00085, 0x07);
302	if (ret)
303		goto error;
304
305	return ret;
306error:
307	dbg("%s: failed:%d", __func__, ret);
308	return ret;
309}
310
311int cxd2820r_sleep_c(struct dvb_frontend *fe)
312{
313	struct cxd2820r_priv *priv = fe->demodulator_priv;
314	int ret, i;
315	struct reg_val_mask tab[] = {
316		{ 0x000ff, 0x1f, 0xff },
317		{ 0x00085, 0x00, 0xff },
318		{ 0x00088, 0x01, 0xff },
319		{ 0x00081, 0x00, 0xff },
320		{ 0x00080, 0x00, 0xff },
321	};
322
323	dbg("%s", __func__);
324
325	priv->delivery_system = SYS_UNDEFINED;
326
327	for (i = 0; i < ARRAY_SIZE(tab); i++) {
328		ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val,
329			tab[i].mask);
330		if (ret)
331			goto error;
332	}
333
334	return ret;
335error:
336	dbg("%s: failed:%d", __func__, ret);
337	return ret;
338}
339
340int cxd2820r_get_tune_settings_c(struct dvb_frontend *fe,
341	struct dvb_frontend_tune_settings *s)
342{
343	s->min_delay_ms = 500;
344	s->step_size = 0; /* no zigzag */
345	s->max_drift = 0;
346
347	return 0;
348}
349