blob: d402ab68368b679e81575969d5f455394ebbf20d [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 Palosaarifcd09f62016-08-08 15:54:10 -030029 unsigned int utmp;
30 u32 if_frequency;
Antti Palosaari27cfc852011-04-07 16:27:43 -030031 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 },
CrazyCat4d1ab182014-06-03 14:19:07 -030049 { 0x00071, !priv->cfg.ts_clock_inv << 4, 0x10 },
Antti Palosaari27cfc852011-04-07 16:27:43 -030050 { 0x000cb, priv->cfg.if_agc_polarity << 6, 0x40 },
51 { 0x000a5, 0x00, 0x01 },
52 { 0x00082, 0x20, 0x60 },
53 { 0x000c2, 0xc3, 0xff },
54 { 0x0016a, 0x50, 0xff },
55 { 0x00427, 0x41, 0xff },
56 };
57
Antti Palosaari75aeafc2012-07-19 13:10:12 -030058 dev_dbg(&priv->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n", __func__,
59 c->frequency, c->bandwidth_hz);
Antti Palosaari27cfc852011-04-07 16:27:43 -030060
Antti Palosaari13d723b2011-11-13 15:21:58 -030061 switch (c->bandwidth_hz) {
62 case 6000000:
63 bw_i = 0;
64 bw_param = 2;
65 break;
66 case 7000000:
67 bw_i = 1;
68 bw_param = 1;
69 break;
70 case 8000000:
71 bw_i = 2;
72 bw_param = 0;
73 break;
74 default:
75 return -EINVAL;
76 }
77
Antti Palosaari27cfc852011-04-07 16:27:43 -030078 /* program tuner */
79 if (fe->ops.tuner_ops.set_params)
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -030080 fe->ops.tuner_ops.set_params(fe);
Antti Palosaari27cfc852011-04-07 16:27:43 -030081
82 if (priv->delivery_system != SYS_DVBT) {
83 for (i = 0; i < ARRAY_SIZE(tab); i++) {
84 ret = cxd2820r_wr_reg_mask(priv, tab[i].reg,
85 tab[i].val, tab[i].mask);
86 if (ret)
87 goto error;
88 }
89 }
90
91 priv->delivery_system = SYS_DVBT;
Mauro Carvalho Chehab285c0b02014-09-03 15:22:02 -030092 priv->ber_running = false; /* tune stops BER counter */
Antti Palosaari27cfc852011-04-07 16:27:43 -030093
Antti Palosaarifda23fa2011-11-13 14:41:25 -030094 /* program IF frequency */
95 if (fe->ops.tuner_ops.get_if_frequency) {
Antti Palosaarifcd09f62016-08-08 15:54:10 -030096 ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
Antti Palosaarifda23fa2011-11-13 14:41:25 -030097 if (ret)
98 goto error;
Antti Palosaarifcd09f62016-08-08 15:54:10 -030099 dev_dbg(&priv->i2c->dev, "%s: if_frequency=%u\n", __func__,
100 if_frequency);
101 } else {
102 ret = -EINVAL;
103 goto error;
104 }
Antti Palosaarifda23fa2011-11-13 14:41:25 -0300105
Antti Palosaarifcd09f62016-08-08 15:54:10 -0300106 utmp = DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x1000000, CXD2820R_CLK);
107 buf[0] = (utmp >> 16) & 0xff;
108 buf[1] = (utmp >> 8) & 0xff;
109 buf[2] = (utmp >> 0) & 0xff;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300110 ret = cxd2820r_wr_regs(priv, 0x000b6, buf, 3);
111 if (ret)
112 goto error;
113
Antti Palosaari13d723b2011-11-13 15:21:58 -0300114 ret = cxd2820r_wr_regs(priv, 0x0009f, bw_params1[bw_i], 5);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300115 if (ret)
116 goto error;
117
118 ret = cxd2820r_wr_reg_mask(priv, 0x000d7, bw_param << 6, 0xc0);
119 if (ret)
120 goto error;
121
Antti Palosaari13d723b2011-11-13 15:21:58 -0300122 ret = cxd2820r_wr_regs(priv, 0x000d9, bw_params2[bw_i], 2);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300123 if (ret)
124 goto error;
125
126 ret = cxd2820r_wr_reg(priv, 0x000ff, 0x08);
127 if (ret)
128 goto error;
129
130 ret = cxd2820r_wr_reg(priv, 0x000fe, 0x01);
131 if (ret)
132 goto error;
133
134 return ret;
135error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300136 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300137 return ret;
138}
139
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -0200140int cxd2820r_get_frontend_t(struct dvb_frontend *fe,
141 struct dtv_frontend_properties *c)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300142{
143 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300144 int ret;
145 u8 buf[2];
146
147 ret = cxd2820r_rd_regs(priv, 0x0002f, buf, sizeof(buf));
148 if (ret)
149 goto error;
150
151 switch ((buf[0] >> 6) & 0x03) {
152 case 0:
153 c->modulation = QPSK;
154 break;
155 case 1:
156 c->modulation = QAM_16;
157 break;
158 case 2:
159 c->modulation = QAM_64;
160 break;
161 }
162
163 switch ((buf[1] >> 1) & 0x03) {
164 case 0:
165 c->transmission_mode = TRANSMISSION_MODE_2K;
166 break;
167 case 1:
168 c->transmission_mode = TRANSMISSION_MODE_8K;
169 break;
170 }
171
172 switch ((buf[1] >> 3) & 0x03) {
173 case 0:
174 c->guard_interval = GUARD_INTERVAL_1_32;
175 break;
176 case 1:
177 c->guard_interval = GUARD_INTERVAL_1_16;
178 break;
179 case 2:
180 c->guard_interval = GUARD_INTERVAL_1_8;
181 break;
182 case 3:
183 c->guard_interval = GUARD_INTERVAL_1_4;
184 break;
185 }
186
187 switch ((buf[0] >> 3) & 0x07) {
188 case 0:
189 c->hierarchy = HIERARCHY_NONE;
190 break;
191 case 1:
192 c->hierarchy = HIERARCHY_1;
193 break;
194 case 2:
195 c->hierarchy = HIERARCHY_2;
196 break;
197 case 3:
198 c->hierarchy = HIERARCHY_4;
199 break;
200 }
201
202 switch ((buf[0] >> 0) & 0x07) {
203 case 0:
204 c->code_rate_HP = FEC_1_2;
205 break;
206 case 1:
207 c->code_rate_HP = FEC_2_3;
208 break;
209 case 2:
210 c->code_rate_HP = FEC_3_4;
211 break;
212 case 3:
213 c->code_rate_HP = FEC_5_6;
214 break;
215 case 4:
216 c->code_rate_HP = FEC_7_8;
217 break;
218 }
219
220 switch ((buf[1] >> 5) & 0x07) {
221 case 0:
222 c->code_rate_LP = FEC_1_2;
223 break;
224 case 1:
225 c->code_rate_LP = FEC_2_3;
226 break;
227 case 2:
228 c->code_rate_LP = FEC_3_4;
229 break;
230 case 3:
231 c->code_rate_LP = FEC_5_6;
232 break;
233 case 4:
234 c->code_rate_LP = FEC_7_8;
235 break;
236 }
237
238 ret = cxd2820r_rd_reg(priv, 0x007c6, &buf[0]);
239 if (ret)
240 goto error;
241
242 switch ((buf[0] >> 0) & 0x01) {
243 case 0:
244 c->inversion = INVERSION_OFF;
245 break;
246 case 1:
247 c->inversion = INVERSION_ON;
248 break;
249 }
250
251 return ret;
252error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300253 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300254 return ret;
255}
256
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300257int cxd2820r_read_ber_t(struct dvb_frontend *fe, u32 *ber)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300258{
259 struct cxd2820r_priv *priv = fe->demodulator_priv;
260 int ret;
261 u8 buf[3], start_ber = 0;
262 *ber = 0;
263
264 if (priv->ber_running) {
265 ret = cxd2820r_rd_regs(priv, 0x00076, buf, sizeof(buf));
266 if (ret)
267 goto error;
268
269 if ((buf[2] >> 7) & 0x01 || (buf[2] >> 4) & 0x01) {
270 *ber = (buf[2] & 0x0f) << 16 | buf[1] << 8 | buf[0];
271 start_ber = 1;
272 }
273 } else {
Mauro Carvalho Chehab285c0b02014-09-03 15:22:02 -0300274 priv->ber_running = true;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300275 start_ber = 1;
276 }
277
278 if (start_ber) {
279 /* (re)start BER */
280 ret = cxd2820r_wr_reg(priv, 0x00079, 0x01);
281 if (ret)
282 goto error;
283 }
284
285 return ret;
286error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300287 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300288 return ret;
289}
290
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300291int cxd2820r_read_signal_strength_t(struct dvb_frontend *fe,
Antti Palosaari27cfc852011-04-07 16:27:43 -0300292 u16 *strength)
293{
294 struct cxd2820r_priv *priv = fe->demodulator_priv;
295 int ret;
296 u8 buf[2];
297 u16 tmp;
298
299 ret = cxd2820r_rd_regs(priv, 0x00026, buf, sizeof(buf));
300 if (ret)
301 goto error;
302
303 tmp = (buf[0] & 0x0f) << 8 | buf[1];
304 tmp = ~tmp & 0x0fff;
305
306 /* scale value to 0x0000-0xffff from 0x0000-0x0fff */
307 *strength = tmp * 0xffff / 0x0fff;
308
309 return ret;
310error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300311 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300312 return ret;
313}
314
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300315int cxd2820r_read_snr_t(struct dvb_frontend *fe, u16 *snr)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300316{
317 struct cxd2820r_priv *priv = fe->demodulator_priv;
318 int ret;
319 u8 buf[2];
320 u16 tmp;
321 /* report SNR in dB * 10 */
322
323 ret = cxd2820r_rd_regs(priv, 0x00028, buf, sizeof(buf));
324 if (ret)
325 goto error;
326
327 tmp = (buf[0] & 0x1f) << 8 | buf[1];
328 #define CXD2820R_LOG10_8_24 15151336 /* log10(8) << 24 */
329 if (tmp)
330 *snr = (intlog10(tmp) - CXD2820R_LOG10_8_24) / ((1 << 24)
331 / 100);
332 else
333 *snr = 0;
334
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300335 dev_dbg(&priv->i2c->dev, "%s: dBx10=%d val=%04x\n", __func__, *snr,
336 tmp);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300337
338 return ret;
339error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300340 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300341 return ret;
342}
343
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300344int cxd2820r_read_ucblocks_t(struct dvb_frontend *fe, u32 *ucblocks)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300345{
346 *ucblocks = 0;
347 /* no way to read ? */
348 return 0;
349}
350
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300351int cxd2820r_read_status_t(struct dvb_frontend *fe, enum fe_status *status)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300352{
353 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaari2832fd32016-08-08 20:13:45 -0300354 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300355 int ret;
Antti Palosaari2832fd32016-08-08 20:13:45 -0300356 unsigned int utmp;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300357 u8 buf[4];
358 *status = 0;
359
360 ret = cxd2820r_rd_reg(priv, 0x00010, &buf[0]);
361 if (ret)
362 goto error;
363
364 if ((buf[0] & 0x07) == 6) {
365 ret = cxd2820r_rd_reg(priv, 0x00073, &buf[1]);
366 if (ret)
367 goto error;
368
369 if (((buf[1] >> 3) & 0x01) == 1) {
370 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
371 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
372 } else {
373 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
374 FE_HAS_VITERBI | FE_HAS_SYNC;
375 }
376 } else {
377 ret = cxd2820r_rd_reg(priv, 0x00014, &buf[2]);
378 if (ret)
379 goto error;
380
381 if ((buf[2] & 0x0f) >= 4) {
382 ret = cxd2820r_rd_reg(priv, 0x00a14, &buf[3]);
383 if (ret)
384 goto error;
385
386 if (((buf[3] >> 4) & 0x01) == 1)
387 *status |= FE_HAS_SIGNAL;
388 }
389 }
390
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300391 dev_dbg(&priv->i2c->dev, "%s: lock=%*ph\n", __func__, 4, buf);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300392
Antti Palosaari2832fd32016-08-08 20:13:45 -0300393 /* Signal strength */
394 if (*status & FE_HAS_SIGNAL) {
395 unsigned int strength;
396
397 ret = cxd2820r_rd_regs(priv, 0x00026, buf, 2);
398 if (ret)
399 goto error;
400
401 utmp = buf[0] << 8 | buf[1] << 0;
402 utmp = ~utmp & 0x0fff;
403 /* Scale value to 0x0000-0xffff */
404 strength = utmp << 4 | utmp >> 8;
405
406 c->strength.len = 1;
407 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
408 c->strength.stat[0].uvalue = strength;
409 } else {
410 c->strength.len = 1;
411 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
412 }
413
414 /* CNR */
415 if (*status & FE_HAS_VITERBI) {
416 unsigned int cnr;
417
418 ret = cxd2820r_rd_regs(priv, 0x0002c, buf, 2);
419 if (ret)
420 goto error;
421
422 utmp = buf[0] << 8 | buf[1] << 0;
423 if (utmp)
424 cnr = div_u64((u64)(intlog10(utmp)
425 - intlog10(32000 - utmp) + 55532585)
426 * 10000, (1 << 24));
427 else
428 cnr = 0;
429
430 c->cnr.len = 1;
431 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
432 c->cnr.stat[0].svalue = cnr;
433 } else {
434 c->cnr.len = 1;
435 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
436 }
437
438 /* BER */
439 if (*status & FE_HAS_SYNC) {
440 unsigned int post_bit_error;
441 bool start_ber;
442
443 if (priv->ber_running) {
444 ret = cxd2820r_rd_regs(priv, 0x00076, buf, 3);
445 if (ret)
446 goto error;
447
448 if ((buf[2] >> 7) & 0x01) {
449 post_bit_error = buf[2] << 16 | buf[1] << 8 |
450 buf[0] << 0;
451 post_bit_error &= 0x0fffff;
452 start_ber = true;
453 } else {
454 post_bit_error = 0;
455 start_ber = false;
456 }
457 } else {
458 post_bit_error = 0;
459 start_ber = true;
460 }
461
462 if (start_ber) {
463 ret = cxd2820r_wr_reg(priv, 0x00079, 0x01);
464 if (ret)
465 goto error;
466 priv->ber_running = true;
467 }
468
469 priv->post_bit_error += post_bit_error;
470
471 c->post_bit_error.len = 1;
472 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
473 c->post_bit_error.stat[0].uvalue = priv->post_bit_error;
474 } else {
475 c->post_bit_error.len = 1;
476 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
477 }
478
Antti Palosaari27cfc852011-04-07 16:27:43 -0300479 return ret;
480error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300481 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300482 return ret;
483}
484
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300485int cxd2820r_init_t(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300486{
487 struct cxd2820r_priv *priv = fe->demodulator_priv;
488 int ret;
489
490 ret = cxd2820r_wr_reg(priv, 0x00085, 0x07);
491 if (ret)
492 goto error;
493
494 return ret;
495error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300496 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300497 return ret;
498}
499
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300500int cxd2820r_sleep_t(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300501{
502 struct cxd2820r_priv *priv = fe->demodulator_priv;
503 int ret, i;
504 struct reg_val_mask tab[] = {
505 { 0x000ff, 0x1f, 0xff },
506 { 0x00085, 0x00, 0xff },
507 { 0x00088, 0x01, 0xff },
508 { 0x00081, 0x00, 0xff },
509 { 0x00080, 0x00, 0xff },
510 };
511
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300512 dev_dbg(&priv->i2c->dev, "%s\n", __func__);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300513
514 priv->delivery_system = SYS_UNDEFINED;
515
516 for (i = 0; i < ARRAY_SIZE(tab); i++) {
517 ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val,
518 tab[i].mask);
519 if (ret)
520 goto error;
521 }
522
523 return ret;
524error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300525 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300526 return ret;
527}
528
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300529int cxd2820r_get_tune_settings_t(struct dvb_frontend *fe,
Antti Palosaari27cfc852011-04-07 16:27:43 -0300530 struct dvb_frontend_tune_settings *s)
531{
532 s->min_delay_ms = 500;
533 s->step_size = fe->ops.info.frequency_stepsize * 2;
534 s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
535
536 return 0;
537}