blob: 174d916d1aaaadb46f25ffb4a4f1f148a207a883 [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;
Antti Palosaaric98975f2016-08-09 22:00:37 -030027 struct i2c_client *client = priv->client[0];
Antti Palosaari27cfc852011-04-07 16:27:43 -030028 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari13d723b2011-11-13 15:21:58 -030029 int ret, i, bw_i;
Antti Palosaarifcd09f62016-08-08 15:54:10 -030030 unsigned int utmp;
31 u32 if_frequency;
Antti Palosaari27cfc852011-04-07 16:27:43 -030032 u8 buf[3], bw_param;
33 u8 bw_params1[][5] = {
34 { 0x17, 0xea, 0xaa, 0xaa, 0xaa }, /* 6 MHz */
35 { 0x14, 0x80, 0x00, 0x00, 0x00 }, /* 7 MHz */
36 { 0x11, 0xf0, 0x00, 0x00, 0x00 }, /* 8 MHz */
37 };
38 u8 bw_params2[][2] = {
39 { 0x1f, 0xdc }, /* 6 MHz */
40 { 0x12, 0xf8 }, /* 7 MHz */
41 { 0x01, 0xe0 }, /* 8 MHz */
42 };
43 struct reg_val_mask tab[] = {
44 { 0x00080, 0x00, 0xff },
45 { 0x00081, 0x03, 0xff },
46 { 0x00085, 0x07, 0xff },
47 { 0x00088, 0x01, 0xff },
48
Antti Palosaari07fdf7d2016-08-09 20:49:09 -030049 { 0x00070, priv->ts_mode, 0xff },
50 { 0x00071, !priv->ts_clk_inv << 4, 0x10 },
51 { 0x000cb, priv->if_agc_polarity << 6, 0x40 },
Antti Palosaari27cfc852011-04-07 16:27:43 -030052 { 0x000a5, 0x00, 0x01 },
53 { 0x00082, 0x20, 0x60 },
54 { 0x000c2, 0xc3, 0xff },
55 { 0x0016a, 0x50, 0xff },
56 { 0x00427, 0x41, 0xff },
57 };
58
Antti Palosaaric98975f2016-08-09 22:00:37 -030059 dev_dbg(&client->dev,
60 "delivery_system=%d modulation=%d frequency=%u bandwidth_hz=%u inversion=%d\n",
61 c->delivery_system, c->modulation, c->frequency,
62 c->bandwidth_hz, c->inversion);
Antti Palosaari27cfc852011-04-07 16:27:43 -030063
Antti Palosaari13d723b2011-11-13 15:21:58 -030064 switch (c->bandwidth_hz) {
65 case 6000000:
66 bw_i = 0;
67 bw_param = 2;
68 break;
69 case 7000000:
70 bw_i = 1;
71 bw_param = 1;
72 break;
73 case 8000000:
74 bw_i = 2;
75 bw_param = 0;
76 break;
77 default:
78 return -EINVAL;
79 }
80
Antti Palosaari27cfc852011-04-07 16:27:43 -030081 /* program tuner */
82 if (fe->ops.tuner_ops.set_params)
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -030083 fe->ops.tuner_ops.set_params(fe);
Antti Palosaari27cfc852011-04-07 16:27:43 -030084
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);
89 if (ret)
90 goto error;
91 }
92 }
93
94 priv->delivery_system = SYS_DVBT;
Mauro Carvalho Chehab285c0b02014-09-03 15:22:02 -030095 priv->ber_running = false; /* tune stops BER counter */
Antti Palosaari27cfc852011-04-07 16:27:43 -030096
Antti Palosaarifda23fa2011-11-13 14:41:25 -030097 /* program IF frequency */
98 if (fe->ops.tuner_ops.get_if_frequency) {
Antti Palosaarifcd09f62016-08-08 15:54:10 -030099 ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
Antti Palosaarifda23fa2011-11-13 14:41:25 -0300100 if (ret)
101 goto error;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300102 dev_dbg(&client->dev, "if_frequency=%u\n", if_frequency);
Antti Palosaarifcd09f62016-08-08 15:54:10 -0300103 } else {
104 ret = -EINVAL;
105 goto error;
106 }
Antti Palosaarifda23fa2011-11-13 14:41:25 -0300107
Antti Palosaarifcd09f62016-08-08 15:54:10 -0300108 utmp = DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x1000000, CXD2820R_CLK);
109 buf[0] = (utmp >> 16) & 0xff;
110 buf[1] = (utmp >> 8) & 0xff;
111 buf[2] = (utmp >> 0) & 0xff;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300112 ret = cxd2820r_wr_regs(priv, 0x000b6, buf, 3);
113 if (ret)
114 goto error;
115
Antti Palosaari13d723b2011-11-13 15:21:58 -0300116 ret = cxd2820r_wr_regs(priv, 0x0009f, bw_params1[bw_i], 5);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300117 if (ret)
118 goto error;
119
120 ret = cxd2820r_wr_reg_mask(priv, 0x000d7, bw_param << 6, 0xc0);
121 if (ret)
122 goto error;
123
Antti Palosaari13d723b2011-11-13 15:21:58 -0300124 ret = cxd2820r_wr_regs(priv, 0x000d9, bw_params2[bw_i], 2);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300125 if (ret)
126 goto error;
127
128 ret = cxd2820r_wr_reg(priv, 0x000ff, 0x08);
129 if (ret)
130 goto error;
131
132 ret = cxd2820r_wr_reg(priv, 0x000fe, 0x01);
133 if (ret)
134 goto error;
135
136 return ret;
137error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300138 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300139 return ret;
140}
141
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -0200142int cxd2820r_get_frontend_t(struct dvb_frontend *fe,
143 struct dtv_frontend_properties *c)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300144{
145 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300146 struct i2c_client *client = priv->client[0];
Antti Palosaari27cfc852011-04-07 16:27:43 -0300147 int ret;
148 u8 buf[2];
149
Antti Palosaaric98975f2016-08-09 22:00:37 -0300150 dev_dbg(&client->dev, "\n");
151
Antti Palosaari27cfc852011-04-07 16:27:43 -0300152 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 Palosaaric98975f2016-08-09 22:00:37 -0300258 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300259 return ret;
260}
261
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300262int cxd2820r_read_status_t(struct dvb_frontend *fe, enum fe_status *status)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300263{
264 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300265 struct i2c_client *client = priv->client[0];
Antti Palosaari2832fd32016-08-08 20:13:45 -0300266 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300267 int ret;
Antti Palosaari2832fd32016-08-08 20:13:45 -0300268 unsigned int utmp;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300269 u8 buf[4];
270 *status = 0;
271
272 ret = cxd2820r_rd_reg(priv, 0x00010, &buf[0]);
273 if (ret)
274 goto error;
275
276 if ((buf[0] & 0x07) == 6) {
277 ret = cxd2820r_rd_reg(priv, 0x00073, &buf[1]);
278 if (ret)
279 goto error;
280
281 if (((buf[1] >> 3) & 0x01) == 1) {
282 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
283 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
284 } else {
285 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
286 FE_HAS_VITERBI | FE_HAS_SYNC;
287 }
288 } else {
289 ret = cxd2820r_rd_reg(priv, 0x00014, &buf[2]);
290 if (ret)
291 goto error;
292
293 if ((buf[2] & 0x0f) >= 4) {
294 ret = cxd2820r_rd_reg(priv, 0x00a14, &buf[3]);
295 if (ret)
296 goto error;
297
298 if (((buf[3] >> 4) & 0x01) == 1)
299 *status |= FE_HAS_SIGNAL;
300 }
301 }
302
Antti Palosaaric98975f2016-08-09 22:00:37 -0300303 dev_dbg(&client->dev, "lock=%*ph\n", 4, buf);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300304
Antti Palosaari2832fd32016-08-08 20:13:45 -0300305 /* Signal strength */
306 if (*status & FE_HAS_SIGNAL) {
307 unsigned int strength;
308
309 ret = cxd2820r_rd_regs(priv, 0x00026, buf, 2);
310 if (ret)
311 goto error;
312
313 utmp = buf[0] << 8 | buf[1] << 0;
314 utmp = ~utmp & 0x0fff;
315 /* Scale value to 0x0000-0xffff */
316 strength = utmp << 4 | utmp >> 8;
317
318 c->strength.len = 1;
319 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
320 c->strength.stat[0].uvalue = strength;
321 } else {
322 c->strength.len = 1;
323 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
324 }
325
326 /* CNR */
327 if (*status & FE_HAS_VITERBI) {
328 unsigned int cnr;
329
330 ret = cxd2820r_rd_regs(priv, 0x0002c, buf, 2);
331 if (ret)
332 goto error;
333
334 utmp = buf[0] << 8 | buf[1] << 0;
335 if (utmp)
336 cnr = div_u64((u64)(intlog10(utmp)
337 - intlog10(32000 - utmp) + 55532585)
338 * 10000, (1 << 24));
339 else
340 cnr = 0;
341
342 c->cnr.len = 1;
343 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
344 c->cnr.stat[0].svalue = cnr;
345 } else {
346 c->cnr.len = 1;
347 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
348 }
349
350 /* BER */
351 if (*status & FE_HAS_SYNC) {
352 unsigned int post_bit_error;
353 bool start_ber;
354
355 if (priv->ber_running) {
356 ret = cxd2820r_rd_regs(priv, 0x00076, buf, 3);
357 if (ret)
358 goto error;
359
360 if ((buf[2] >> 7) & 0x01) {
361 post_bit_error = buf[2] << 16 | buf[1] << 8 |
362 buf[0] << 0;
363 post_bit_error &= 0x0fffff;
364 start_ber = true;
365 } else {
366 post_bit_error = 0;
367 start_ber = false;
368 }
369 } else {
370 post_bit_error = 0;
371 start_ber = true;
372 }
373
374 if (start_ber) {
375 ret = cxd2820r_wr_reg(priv, 0x00079, 0x01);
376 if (ret)
377 goto error;
378 priv->ber_running = true;
379 }
380
381 priv->post_bit_error += post_bit_error;
382
383 c->post_bit_error.len = 1;
384 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
385 c->post_bit_error.stat[0].uvalue = priv->post_bit_error;
386 } else {
387 c->post_bit_error.len = 1;
388 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
389 }
390
Antti Palosaari27cfc852011-04-07 16:27:43 -0300391 return ret;
392error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300393 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300394 return ret;
395}
396
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300397int cxd2820r_init_t(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300398{
399 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300400 struct i2c_client *client = priv->client[0];
Antti Palosaari27cfc852011-04-07 16:27:43 -0300401 int ret;
402
Antti Palosaaric98975f2016-08-09 22:00:37 -0300403 dev_dbg(&client->dev, "\n");
404
Antti Palosaari27cfc852011-04-07 16:27:43 -0300405 ret = cxd2820r_wr_reg(priv, 0x00085, 0x07);
406 if (ret)
407 goto error;
408
409 return ret;
410error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300411 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300412 return ret;
413}
414
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300415int cxd2820r_sleep_t(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300416{
417 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300418 struct i2c_client *client = priv->client[0];
Antti Palosaari27cfc852011-04-07 16:27:43 -0300419 int ret, i;
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 },
426 };
427
Antti Palosaaric98975f2016-08-09 22:00:37 -0300428 dev_dbg(&client->dev, "\n");
Antti Palosaari27cfc852011-04-07 16:27:43 -0300429
430 priv->delivery_system = SYS_UNDEFINED;
431
432 for (i = 0; i < ARRAY_SIZE(tab); i++) {
433 ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val,
434 tab[i].mask);
435 if (ret)
436 goto error;
437 }
438
439 return ret;
440error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300441 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300442 return ret;
443}
444
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300445int cxd2820r_get_tune_settings_t(struct dvb_frontend *fe,
Antti Palosaari27cfc852011-04-07 16:27:43 -0300446 struct dvb_frontend_tune_settings *s)
447{
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;
451
452 return 0;
453}