blob: 149fdca3fb44e33fd60cda6c44de66041019c86c [file] [log] [blame]
Antti Palosaari27cfc852011-04-07 16:27:43 -03001/*
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
Steve Kerrison9ac51c52011-05-02 18:19:13 -030022#include "cxd2820r_priv.h"
23
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -030024int cxd2820r_set_frontend_c(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -030025{
26 struct cxd2820r_priv *priv = fe->demodulator_priv;
27 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
28 int ret, i;
29 u8 buf[2];
Antti Palosaarifda23fa2011-11-13 14:41:25 -030030 u32 if_freq;
Antti Palosaari27cfc852011-04-07 16:27:43 -030031 u16 if_ctl;
32 u64 num;
33 struct reg_val_mask tab[] = {
34 { 0x00080, 0x01, 0xff },
35 { 0x00081, 0x05, 0xff },
36 { 0x00085, 0x07, 0xff },
37 { 0x00088, 0x01, 0xff },
38
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 },
CrazyCat4d1ab182014-06-03 14:19:07 -030048 { 0x10071, !priv->cfg.ts_clock_inv << 4, 0x10 },
Antti Palosaari27cfc852011-04-07 16:27:43 -030049 };
50
Antti Palosaari75aeafc2012-07-19 13:10:12 -030051 dev_dbg(&priv->i2c->dev, "%s: frequency=%d symbol_rate=%d\n", __func__,
52 c->frequency, c->symbol_rate);
Antti Palosaari27cfc852011-04-07 16:27:43 -030053
Antti Palosaari27cfc852011-04-07 16:27:43 -030054 /* program tuner */
55 if (fe->ops.tuner_ops.set_params)
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -030056 fe->ops.tuner_ops.set_params(fe);
Antti Palosaari27cfc852011-04-07 16:27:43 -030057
Mauro Carvalho Chehab03494712011-12-22 18:11:39 -030058 if (priv->delivery_system != SYS_DVBC_ANNEX_A) {
Antti Palosaari27cfc852011-04-07 16:27:43 -030059 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);
62 if (ret)
63 goto error;
64 }
65 }
66
Mauro Carvalho Chehab03494712011-12-22 18:11:39 -030067 priv->delivery_system = SYS_DVBC_ANNEX_A;
Mauro Carvalho Chehab285c0b02014-09-03 15:22:02 -030068 priv->ber_running = false; /* tune stops BER counter */
Antti Palosaari27cfc852011-04-07 16:27:43 -030069
Antti Palosaarifda23fa2011-11-13 14:41:25 -030070 /* program IF frequency */
71 if (fe->ops.tuner_ops.get_if_frequency) {
72 ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
73 if (ret)
74 goto error;
75 } else
76 if_freq = 0;
77
Antti Palosaari75aeafc2012-07-19 13:10:12 -030078 dev_dbg(&priv->i2c->dev, "%s: if_freq=%d\n", __func__, if_freq);
Antti Palosaarifda23fa2011-11-13 14:41:25 -030079
80 num = if_freq / 1000; /* Hz => kHz */
Antti Palosaari27cfc852011-04-07 16:27:43 -030081 num *= 0x4000;
Evgeny Plehovf286c2a2013-11-09 18:45:41 -030082 if_ctl = 0x4000 - cxd2820r_div_u64_round_closest(num, 41000);
Antti Palosaari27cfc852011-04-07 16:27:43 -030083 buf[0] = (if_ctl >> 8) & 0x3f;
84 buf[1] = (if_ctl >> 0) & 0xff;
85
86 ret = cxd2820r_wr_regs(priv, 0x10042, buf, 2);
87 if (ret)
88 goto error;
89
90 ret = cxd2820r_wr_reg(priv, 0x000ff, 0x08);
91 if (ret)
92 goto error;
93
94 ret = cxd2820r_wr_reg(priv, 0x000fe, 0x01);
95 if (ret)
96 goto error;
97
98 return ret;
99error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300100 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300101 return ret;
102}
103
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -0300104int cxd2820r_get_frontend_c(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300105{
106 struct cxd2820r_priv *priv = fe->demodulator_priv;
107 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
108 int ret;
109 u8 buf[2];
110
111 ret = cxd2820r_rd_regs(priv, 0x1001a, buf, 2);
112 if (ret)
113 goto error;
114
115 c->symbol_rate = 2500 * ((buf[0] & 0x0f) << 8 | buf[1]);
116
117 ret = cxd2820r_rd_reg(priv, 0x10019, &buf[0]);
118 if (ret)
119 goto error;
120
Janne Huttunencba5d0b2012-05-30 05:28:46 -0300121 switch ((buf[0] >> 0) & 0x07) {
Antti Palosaari27cfc852011-04-07 16:27:43 -0300122 case 0:
123 c->modulation = QAM_16;
124 break;
125 case 1:
126 c->modulation = QAM_32;
127 break;
128 case 2:
129 c->modulation = QAM_64;
130 break;
131 case 3:
132 c->modulation = QAM_128;
133 break;
134 case 4:
135 c->modulation = QAM_256;
136 break;
137 }
138
139 switch ((buf[0] >> 7) & 0x01) {
140 case 0:
141 c->inversion = INVERSION_OFF;
142 break;
143 case 1:
144 c->inversion = INVERSION_ON;
145 break;
146 }
147
148 return ret;
149error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300150 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300151 return ret;
152}
153
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300154int cxd2820r_read_ber_c(struct dvb_frontend *fe, u32 *ber)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300155{
156 struct cxd2820r_priv *priv = fe->demodulator_priv;
157 int ret;
158 u8 buf[3], start_ber = 0;
159 *ber = 0;
160
161 if (priv->ber_running) {
162 ret = cxd2820r_rd_regs(priv, 0x10076, buf, sizeof(buf));
163 if (ret)
164 goto error;
165
166 if ((buf[2] >> 7) & 0x01 || (buf[2] >> 4) & 0x01) {
167 *ber = (buf[2] & 0x0f) << 16 | buf[1] << 8 | buf[0];
168 start_ber = 1;
169 }
170 } else {
Mauro Carvalho Chehab285c0b02014-09-03 15:22:02 -0300171 priv->ber_running = true;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300172 start_ber = 1;
173 }
174
175 if (start_ber) {
176 /* (re)start BER */
177 ret = cxd2820r_wr_reg(priv, 0x10079, 0x01);
178 if (ret)
179 goto error;
180 }
181
182 return ret;
183error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300184 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300185 return ret;
186}
187
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300188int cxd2820r_read_signal_strength_c(struct dvb_frontend *fe,
Antti Palosaari27cfc852011-04-07 16:27:43 -0300189 u16 *strength)
190{
191 struct cxd2820r_priv *priv = fe->demodulator_priv;
192 int ret;
193 u8 buf[2];
194 u16 tmp;
195
196 ret = cxd2820r_rd_regs(priv, 0x10049, buf, sizeof(buf));
197 if (ret)
198 goto error;
199
200 tmp = (buf[0] & 0x03) << 8 | buf[1];
201 tmp = (~tmp & 0x03ff);
202
203 if (tmp == 512)
204 /* ~no signal */
205 tmp = 0;
206 else if (tmp > 350)
207 tmp = 350;
208
209 /* scale value to 0x0000-0xffff */
210 *strength = tmp * 0xffff / (350-0);
211
212 return ret;
213error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300214 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300215 return ret;
216}
217
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300218int cxd2820r_read_snr_c(struct dvb_frontend *fe, u16 *snr)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300219{
220 struct cxd2820r_priv *priv = fe->demodulator_priv;
221 int ret;
222 u8 tmp;
223 unsigned int A, B;
224 /* report SNR in dB * 10 */
225
226 ret = cxd2820r_rd_reg(priv, 0x10019, &tmp);
227 if (ret)
228 goto error;
229
230 if (((tmp >> 0) & 0x03) % 2) {
231 A = 875;
232 B = 650;
233 } else {
234 A = 950;
235 B = 760;
236 }
237
238 ret = cxd2820r_rd_reg(priv, 0x1004d, &tmp);
239 if (ret)
240 goto error;
241
242 #define CXD2820R_LOG2_E_24 24204406 /* log2(e) << 24 */
243 if (tmp)
244 *snr = A * (intlog2(B / tmp) >> 5) / (CXD2820R_LOG2_E_24 >> 5)
245 / 10;
246 else
247 *snr = 0;
248
249 return ret;
250error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300251 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300252 return ret;
253}
254
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300255int cxd2820r_read_ucblocks_c(struct dvb_frontend *fe, u32 *ucblocks)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300256{
257 *ucblocks = 0;
258 /* no way to read ? */
259 return 0;
260}
261
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300262int cxd2820r_read_status_c(struct dvb_frontend *fe, fe_status_t *status)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300263{
264 struct cxd2820r_priv *priv = fe->demodulator_priv;
265 int ret;
266 u8 buf[2];
267 *status = 0;
268
269 ret = cxd2820r_rd_regs(priv, 0x10088, buf, sizeof(buf));
270 if (ret)
271 goto error;
272
273 if (((buf[0] >> 0) & 0x01) == 1) {
274 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
275 FE_HAS_VITERBI | FE_HAS_SYNC;
276
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;
280 }
281 }
282
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300283 dev_dbg(&priv->i2c->dev, "%s: lock=%02x %02x\n", __func__, buf[0],
284 buf[1]);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300285
286 return ret;
287error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300288 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300289 return ret;
290}
291
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300292int cxd2820r_init_c(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300293{
294 struct cxd2820r_priv *priv = fe->demodulator_priv;
295 int ret;
296
297 ret = cxd2820r_wr_reg(priv, 0x00085, 0x07);
298 if (ret)
299 goto error;
300
301 return ret;
302error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300303 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300304 return ret;
305}
306
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300307int cxd2820r_sleep_c(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300308{
309 struct cxd2820r_priv *priv = fe->demodulator_priv;
310 int ret, i;
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 },
317 };
318
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300319 dev_dbg(&priv->i2c->dev, "%s\n", __func__);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300320
321 priv->delivery_system = SYS_UNDEFINED;
322
323 for (i = 0; i < ARRAY_SIZE(tab); i++) {
324 ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val,
325 tab[i].mask);
326 if (ret)
327 goto error;
328 }
329
330 return ret;
331error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300332 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300333 return ret;
334}
335
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300336int cxd2820r_get_tune_settings_c(struct dvb_frontend *fe,
Antti Palosaari27cfc852011-04-07 16:27:43 -0300337 struct dvb_frontend_tune_settings *s)
338{
339 s->min_delay_ms = 500;
340 s->step_size = 0; /* no zigzag */
341 s->max_drift = 0;
342
343 return 0;
344}