blob: af5890e4f8377f54e06404eb087bd6e4cefab94a [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_t(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;
Antti Palosaari13d723b2011-11-13 15:21:58 -030028 int ret, i, bw_i;
Antti Palosaarifda23fa2011-11-13 14:41:25 -030029 u32 if_freq, if_ctl;
Antti Palosaari27cfc852011-04-07 16:27:43 -030030 u64 num;
31 u8 buf[3], bw_param;
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 */
36 };
37 u8 bw_params2[][2] = {
38 { 0x1f, 0xdc }, /* 6 MHz */
39 { 0x12, 0xf8 }, /* 7 MHz */
40 { 0x01, 0xe0 }, /* 8 MHz */
41 };
42 struct reg_val_mask tab[] = {
43 { 0x00080, 0x00, 0xff },
44 { 0x00081, 0x03, 0xff },
45 { 0x00085, 0x07, 0xff },
46 { 0x00088, 0x01, 0xff },
47
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 },
55 };
56
Antti Palosaari75aeafc2012-07-19 13:10:12 -030057 dev_dbg(&priv->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n", __func__,
58 c->frequency, c->bandwidth_hz);
Antti Palosaari27cfc852011-04-07 16:27:43 -030059
Antti Palosaari13d723b2011-11-13 15:21:58 -030060 switch (c->bandwidth_hz) {
61 case 6000000:
62 bw_i = 0;
63 bw_param = 2;
64 break;
65 case 7000000:
66 bw_i = 1;
67 bw_param = 1;
68 break;
69 case 8000000:
70 bw_i = 2;
71 bw_param = 0;
72 break;
73 default:
74 return -EINVAL;
75 }
76
Antti Palosaari27cfc852011-04-07 16:27:43 -030077 /* update GPIOs */
78 ret = cxd2820r_gpio(fe);
79 if (ret)
80 goto error;
81
82 /* program tuner */
83 if (fe->ops.tuner_ops.set_params)
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -030084 fe->ops.tuner_ops.set_params(fe);
Antti Palosaari27cfc852011-04-07 16:27:43 -030085
86 if (priv->delivery_system != SYS_DVBT) {
87 for (i = 0; i < ARRAY_SIZE(tab); i++) {
88 ret = cxd2820r_wr_reg_mask(priv, tab[i].reg,
89 tab[i].val, tab[i].mask);
90 if (ret)
91 goto error;
92 }
93 }
94
95 priv->delivery_system = SYS_DVBT;
96 priv->ber_running = 0; /* tune stops BER counter */
97
Antti Palosaarifda23fa2011-11-13 14:41:25 -030098 /* program IF frequency */
99 if (fe->ops.tuner_ops.get_if_frequency) {
100 ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
101 if (ret)
102 goto error;
103 } else
104 if_freq = 0;
105
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300106 dev_dbg(&priv->i2c->dev, "%s: if_freq=%d\n", __func__, if_freq);
Antti Palosaarifda23fa2011-11-13 14:41:25 -0300107
108 num = if_freq / 1000; /* Hz => kHz */
Antti Palosaari27cfc852011-04-07 16:27:43 -0300109 num *= 0x1000000;
110 if_ctl = cxd2820r_div_u64_round_closest(num, 41000);
111 buf[0] = ((if_ctl >> 16) & 0xff);
112 buf[1] = ((if_ctl >> 8) & 0xff);
113 buf[2] = ((if_ctl >> 0) & 0xff);
114
115 ret = cxd2820r_wr_regs(priv, 0x000b6, buf, 3);
116 if (ret)
117 goto error;
118
Antti Palosaari13d723b2011-11-13 15:21:58 -0300119 ret = cxd2820r_wr_regs(priv, 0x0009f, bw_params1[bw_i], 5);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300120 if (ret)
121 goto error;
122
123 ret = cxd2820r_wr_reg_mask(priv, 0x000d7, bw_param << 6, 0xc0);
124 if (ret)
125 goto error;
126
Antti Palosaari13d723b2011-11-13 15:21:58 -0300127 ret = cxd2820r_wr_regs(priv, 0x000d9, bw_params2[bw_i], 2);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300128 if (ret)
129 goto error;
130
131 ret = cxd2820r_wr_reg(priv, 0x000ff, 0x08);
132 if (ret)
133 goto error;
134
135 ret = cxd2820r_wr_reg(priv, 0x000fe, 0x01);
136 if (ret)
137 goto error;
138
139 return ret;
140error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300141 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300142 return ret;
143}
144
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -0300145int cxd2820r_get_frontend_t(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300146{
147 struct cxd2820r_priv *priv = fe->demodulator_priv;
148 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
149 int ret;
150 u8 buf[2];
151
152 ret = cxd2820r_rd_regs(priv, 0x0002f, buf, sizeof(buf));
153 if (ret)
154 goto error;
155
156 switch ((buf[0] >> 6) & 0x03) {
157 case 0:
158 c->modulation = QPSK;
159 break;
160 case 1:
161 c->modulation = QAM_16;
162 break;
163 case 2:
164 c->modulation = QAM_64;
165 break;
166 }
167
168 switch ((buf[1] >> 1) & 0x03) {
169 case 0:
170 c->transmission_mode = TRANSMISSION_MODE_2K;
171 break;
172 case 1:
173 c->transmission_mode = TRANSMISSION_MODE_8K;
174 break;
175 }
176
177 switch ((buf[1] >> 3) & 0x03) {
178 case 0:
179 c->guard_interval = GUARD_INTERVAL_1_32;
180 break;
181 case 1:
182 c->guard_interval = GUARD_INTERVAL_1_16;
183 break;
184 case 2:
185 c->guard_interval = GUARD_INTERVAL_1_8;
186 break;
187 case 3:
188 c->guard_interval = GUARD_INTERVAL_1_4;
189 break;
190 }
191
192 switch ((buf[0] >> 3) & 0x07) {
193 case 0:
194 c->hierarchy = HIERARCHY_NONE;
195 break;
196 case 1:
197 c->hierarchy = HIERARCHY_1;
198 break;
199 case 2:
200 c->hierarchy = HIERARCHY_2;
201 break;
202 case 3:
203 c->hierarchy = HIERARCHY_4;
204 break;
205 }
206
207 switch ((buf[0] >> 0) & 0x07) {
208 case 0:
209 c->code_rate_HP = FEC_1_2;
210 break;
211 case 1:
212 c->code_rate_HP = FEC_2_3;
213 break;
214 case 2:
215 c->code_rate_HP = FEC_3_4;
216 break;
217 case 3:
218 c->code_rate_HP = FEC_5_6;
219 break;
220 case 4:
221 c->code_rate_HP = FEC_7_8;
222 break;
223 }
224
225 switch ((buf[1] >> 5) & 0x07) {
226 case 0:
227 c->code_rate_LP = FEC_1_2;
228 break;
229 case 1:
230 c->code_rate_LP = FEC_2_3;
231 break;
232 case 2:
233 c->code_rate_LP = FEC_3_4;
234 break;
235 case 3:
236 c->code_rate_LP = FEC_5_6;
237 break;
238 case 4:
239 c->code_rate_LP = FEC_7_8;
240 break;
241 }
242
243 ret = cxd2820r_rd_reg(priv, 0x007c6, &buf[0]);
244 if (ret)
245 goto error;
246
247 switch ((buf[0] >> 0) & 0x01) {
248 case 0:
249 c->inversion = INVERSION_OFF;
250 break;
251 case 1:
252 c->inversion = INVERSION_ON;
253 break;
254 }
255
256 return ret;
257error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300258 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300259 return ret;
260}
261
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300262int cxd2820r_read_ber_t(struct dvb_frontend *fe, u32 *ber)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300263{
264 struct cxd2820r_priv *priv = fe->demodulator_priv;
265 int ret;
266 u8 buf[3], start_ber = 0;
267 *ber = 0;
268
269 if (priv->ber_running) {
270 ret = cxd2820r_rd_regs(priv, 0x00076, buf, sizeof(buf));
271 if (ret)
272 goto error;
273
274 if ((buf[2] >> 7) & 0x01 || (buf[2] >> 4) & 0x01) {
275 *ber = (buf[2] & 0x0f) << 16 | buf[1] << 8 | buf[0];
276 start_ber = 1;
277 }
278 } else {
279 priv->ber_running = 1;
280 start_ber = 1;
281 }
282
283 if (start_ber) {
284 /* (re)start BER */
285 ret = cxd2820r_wr_reg(priv, 0x00079, 0x01);
286 if (ret)
287 goto error;
288 }
289
290 return ret;
291error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300292 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300293 return ret;
294}
295
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300296int cxd2820r_read_signal_strength_t(struct dvb_frontend *fe,
Antti Palosaari27cfc852011-04-07 16:27:43 -0300297 u16 *strength)
298{
299 struct cxd2820r_priv *priv = fe->demodulator_priv;
300 int ret;
301 u8 buf[2];
302 u16 tmp;
303
304 ret = cxd2820r_rd_regs(priv, 0x00026, buf, sizeof(buf));
305 if (ret)
306 goto error;
307
308 tmp = (buf[0] & 0x0f) << 8 | buf[1];
309 tmp = ~tmp & 0x0fff;
310
311 /* scale value to 0x0000-0xffff from 0x0000-0x0fff */
312 *strength = tmp * 0xffff / 0x0fff;
313
314 return ret;
315error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300316 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300317 return ret;
318}
319
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300320int cxd2820r_read_snr_t(struct dvb_frontend *fe, u16 *snr)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300321{
322 struct cxd2820r_priv *priv = fe->demodulator_priv;
323 int ret;
324 u8 buf[2];
325 u16 tmp;
326 /* report SNR in dB * 10 */
327
328 ret = cxd2820r_rd_regs(priv, 0x00028, buf, sizeof(buf));
329 if (ret)
330 goto error;
331
332 tmp = (buf[0] & 0x1f) << 8 | buf[1];
333 #define CXD2820R_LOG10_8_24 15151336 /* log10(8) << 24 */
334 if (tmp)
335 *snr = (intlog10(tmp) - CXD2820R_LOG10_8_24) / ((1 << 24)
336 / 100);
337 else
338 *snr = 0;
339
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300340 dev_dbg(&priv->i2c->dev, "%s: dBx10=%d val=%04x\n", __func__, *snr,
341 tmp);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300342
343 return ret;
344error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300345 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300346 return ret;
347}
348
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300349int cxd2820r_read_ucblocks_t(struct dvb_frontend *fe, u32 *ucblocks)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300350{
351 *ucblocks = 0;
352 /* no way to read ? */
353 return 0;
354}
355
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300356int cxd2820r_read_status_t(struct dvb_frontend *fe, fe_status_t *status)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300357{
358 struct cxd2820r_priv *priv = fe->demodulator_priv;
359 int ret;
360 u8 buf[4];
361 *status = 0;
362
363 ret = cxd2820r_rd_reg(priv, 0x00010, &buf[0]);
364 if (ret)
365 goto error;
366
367 if ((buf[0] & 0x07) == 6) {
368 ret = cxd2820r_rd_reg(priv, 0x00073, &buf[1]);
369 if (ret)
370 goto error;
371
372 if (((buf[1] >> 3) & 0x01) == 1) {
373 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
374 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
375 } else {
376 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
377 FE_HAS_VITERBI | FE_HAS_SYNC;
378 }
379 } else {
380 ret = cxd2820r_rd_reg(priv, 0x00014, &buf[2]);
381 if (ret)
382 goto error;
383
384 if ((buf[2] & 0x0f) >= 4) {
385 ret = cxd2820r_rd_reg(priv, 0x00a14, &buf[3]);
386 if (ret)
387 goto error;
388
389 if (((buf[3] >> 4) & 0x01) == 1)
390 *status |= FE_HAS_SIGNAL;
391 }
392 }
393
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300394 dev_dbg(&priv->i2c->dev, "%s: lock=%*ph\n", __func__, 4, buf);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300395
396 return ret;
397error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300398 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300399 return ret;
400}
401
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300402int cxd2820r_init_t(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300403{
404 struct cxd2820r_priv *priv = fe->demodulator_priv;
405 int ret;
406
407 ret = cxd2820r_wr_reg(priv, 0x00085, 0x07);
408 if (ret)
409 goto error;
410
411 return ret;
412error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300413 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300414 return ret;
415}
416
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300417int cxd2820r_sleep_t(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300418{
419 struct cxd2820r_priv *priv = fe->demodulator_priv;
420 int ret, i;
421 struct reg_val_mask tab[] = {
422 { 0x000ff, 0x1f, 0xff },
423 { 0x00085, 0x00, 0xff },
424 { 0x00088, 0x01, 0xff },
425 { 0x00081, 0x00, 0xff },
426 { 0x00080, 0x00, 0xff },
427 };
428
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300429 dev_dbg(&priv->i2c->dev, "%s\n", __func__);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300430
431 priv->delivery_system = SYS_UNDEFINED;
432
433 for (i = 0; i < ARRAY_SIZE(tab); i++) {
434 ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val,
435 tab[i].mask);
436 if (ret)
437 goto error;
438 }
439
440 return ret;
441error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300442 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300443 return ret;
444}
445
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300446int cxd2820r_get_tune_settings_t(struct dvb_frontend *fe,
Antti Palosaari27cfc852011-04-07 16:27:43 -0300447 struct dvb_frontend_tune_settings *s)
448{
449 s->min_delay_ms = 500;
450 s->step_size = fe->ops.info.frequency_stepsize * 2;
451 s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
452
453 return 0;
454}