blob: e641fde753790e61dc41f6036e591c1ad27f60d2 [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_t2(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -030025{
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -020026 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -030027 struct i2c_client *client = priv->client[0];
28 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari43e2ea62016-08-13 13:19:05 -030029 int ret, 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 { 0x1c, 0xb3, 0x33, 0x33, 0x33 }, /* 5 MHz */
35 { 0x17, 0xea, 0xaa, 0xaa, 0xaa }, /* 6 MHz */
36 { 0x14, 0x80, 0x00, 0x00, 0x00 }, /* 7 MHz */
37 { 0x11, 0xf0, 0x00, 0x00, 0x00 }, /* 8 MHz */
38 };
39 struct reg_val_mask tab[] = {
40 { 0x00080, 0x02, 0xff },
41 { 0x00081, 0x20, 0xff },
42 { 0x00085, 0x07, 0xff },
43 { 0x00088, 0x01, 0xff },
44 { 0x02069, 0x01, 0xff },
45
46 { 0x0207f, 0x2a, 0xff },
47 { 0x02082, 0x0a, 0xff },
48 { 0x02083, 0x0a, 0xff },
Antti Palosaari07fdf7d2016-08-09 20:49:09 -030049 { 0x020cb, priv->if_agc_polarity << 6, 0x40 },
50 { 0x02070, priv->ts_mode, 0xff },
51 { 0x02071, !priv->ts_clk_inv << 6, 0x40 },
52 { 0x020b5, priv->spec_inv << 4, 0x10 },
Antti Palosaari27cfc852011-04-07 16:27:43 -030053 { 0x02567, 0x07, 0x0f },
54 { 0x02569, 0x03, 0x03 },
55 { 0x02595, 0x1a, 0xff },
56 { 0x02596, 0x50, 0xff },
57 { 0x02a8c, 0x00, 0xff },
58 { 0x02a8d, 0x34, 0xff },
59 { 0x02a45, 0x06, 0x07 },
60 { 0x03f10, 0x0d, 0xff },
61 { 0x03f11, 0x02, 0xff },
62 { 0x03f12, 0x01, 0xff },
63 { 0x03f23, 0x2c, 0xff },
64 { 0x03f51, 0x13, 0xff },
65 { 0x03f52, 0x01, 0xff },
66 { 0x03f53, 0x00, 0xff },
67 { 0x027e6, 0x14, 0xff },
68 { 0x02786, 0x02, 0x07 },
69 { 0x02787, 0x40, 0xe0 },
70 { 0x027ef, 0x10, 0x18 },
71 };
72
Antti Palosaaric98975f2016-08-09 22:00:37 -030073 dev_dbg(&client->dev,
74 "delivery_system=%d modulation=%d frequency=%u bandwidth_hz=%u inversion=%d stream_id=%u\n",
75 c->delivery_system, c->modulation, c->frequency,
76 c->bandwidth_hz, c->inversion, c->stream_id);
Antti Palosaari27cfc852011-04-07 16:27:43 -030077
Antti Palosaari13d723b2011-11-13 15:21:58 -030078 switch (c->bandwidth_hz) {
79 case 5000000:
80 bw_i = 0;
81 bw_param = 3;
82 break;
83 case 6000000:
84 bw_i = 1;
85 bw_param = 2;
86 break;
87 case 7000000:
88 bw_i = 2;
89 bw_param = 1;
90 break;
91 case 8000000:
92 bw_i = 3;
93 bw_param = 0;
94 break;
95 default:
96 return -EINVAL;
97 }
98
Antti Palosaari27cfc852011-04-07 16:27:43 -030099 /* program tuner */
100 if (fe->ops.tuner_ops.set_params)
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -0300101 fe->ops.tuner_ops.set_params(fe);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300102
103 if (priv->delivery_system != SYS_DVBT2) {
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300104 ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
105 if (ret)
106 goto error;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300107 }
108
109 priv->delivery_system = SYS_DVBT2;
110
Antti Palosaarifda23fa2011-11-13 14:41:25 -0300111 /* program IF frequency */
112 if (fe->ops.tuner_ops.get_if_frequency) {
Antti Palosaarifcd09f62016-08-08 15:54:10 -0300113 ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
Antti Palosaarifda23fa2011-11-13 14:41:25 -0300114 if (ret)
115 goto error;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300116 dev_dbg(&client->dev, "if_frequency=%u\n", if_frequency);
Antti Palosaarifcd09f62016-08-08 15:54:10 -0300117 } else {
118 ret = -EINVAL;
119 goto error;
120 }
Antti Palosaarifda23fa2011-11-13 14:41:25 -0300121
Antti Palosaarifcd09f62016-08-08 15:54:10 -0300122 utmp = DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x1000000, CXD2820R_CLK);
123 buf[0] = (utmp >> 16) & 0xff;
124 buf[1] = (utmp >> 8) & 0xff;
125 buf[2] = (utmp >> 0) & 0xff;
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300126 ret = regmap_bulk_write(priv->regmap[0], 0x20b6, buf, 3);
Antti Palosaarifcd09f62016-08-08 15:54:10 -0300127 if (ret)
128 goto error;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300129
Evgeny Plehov4a747722013-04-07 23:56:46 -0300130 /* PLP filtering */
Mauro Carvalho Chehaba9af5392013-04-15 08:31:56 -0300131 if (c->stream_id > 255) {
Antti Palosaaric98975f2016-08-09 22:00:37 -0300132 dev_dbg(&client->dev, "disable PLP filtering\n");
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300133 ret = regmap_write(priv->regmap[0], 0x23ad, 0x00);
Evgeny Plehov4a747722013-04-07 23:56:46 -0300134 if (ret)
135 goto error;
136 } else {
Antti Palosaaric98975f2016-08-09 22:00:37 -0300137 dev_dbg(&client->dev, "enable PLP filtering\n");
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300138 ret = regmap_write(priv->regmap[0], 0x23af, c->stream_id & 0xff);
Evgeny Plehov4a747722013-04-07 23:56:46 -0300139 if (ret)
140 goto error;
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300141 ret = regmap_write(priv->regmap[0], 0x23ad, 0x01);
Evgeny Plehov4a747722013-04-07 23:56:46 -0300142 if (ret)
143 goto error;
144 }
145
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300146 ret = regmap_bulk_write(priv->regmap[0], 0x209f, bw_params1[bw_i], 5);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300147 if (ret)
148 goto error;
149
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300150 ret = regmap_update_bits(priv->regmap[0], 0x20d7, 0xc0, bw_param << 6);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300151 if (ret)
152 goto error;
153
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300154 ret = regmap_write(priv->regmap[0], 0x00ff, 0x08);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300155 if (ret)
156 goto error;
157
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300158 ret = regmap_write(priv->regmap[0], 0x00fe, 0x01);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300159 if (ret)
160 goto error;
161
162 return ret;
163error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300164 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300165 return ret;
166
167}
168
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -0200169int cxd2820r_get_frontend_t2(struct dvb_frontend *fe,
170 struct dtv_frontend_properties *c)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300171{
172 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300173 struct i2c_client *client = priv->client[0];
Antti Palosaari27cfc852011-04-07 16:27:43 -0300174 int ret;
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300175 unsigned int utmp;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300176 u8 buf[2];
177
Antti Palosaaric98975f2016-08-09 22:00:37 -0300178 dev_dbg(&client->dev, "\n");
179
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300180 ret = regmap_bulk_read(priv->regmap[0], 0x205c, buf, 2);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300181 if (ret)
182 goto error;
183
184 switch ((buf[0] >> 0) & 0x07) {
185 case 0:
186 c->transmission_mode = TRANSMISSION_MODE_2K;
187 break;
188 case 1:
189 c->transmission_mode = TRANSMISSION_MODE_8K;
190 break;
191 case 2:
192 c->transmission_mode = TRANSMISSION_MODE_4K;
193 break;
194 case 3:
195 c->transmission_mode = TRANSMISSION_MODE_1K;
196 break;
197 case 4:
198 c->transmission_mode = TRANSMISSION_MODE_16K;
199 break;
200 case 5:
201 c->transmission_mode = TRANSMISSION_MODE_32K;
202 break;
203 }
204
205 switch ((buf[1] >> 4) & 0x07) {
206 case 0:
207 c->guard_interval = GUARD_INTERVAL_1_32;
208 break;
209 case 1:
210 c->guard_interval = GUARD_INTERVAL_1_16;
211 break;
212 case 2:
213 c->guard_interval = GUARD_INTERVAL_1_8;
214 break;
215 case 3:
216 c->guard_interval = GUARD_INTERVAL_1_4;
217 break;
218 case 4:
219 c->guard_interval = GUARD_INTERVAL_1_128;
220 break;
221 case 5:
222 c->guard_interval = GUARD_INTERVAL_19_128;
223 break;
224 case 6:
225 c->guard_interval = GUARD_INTERVAL_19_256;
226 break;
227 }
228
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300229 ret = regmap_bulk_read(priv->regmap[0], 0x225b, buf, 2);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300230 if (ret)
231 goto error;
232
233 switch ((buf[0] >> 0) & 0x07) {
234 case 0:
235 c->fec_inner = FEC_1_2;
236 break;
237 case 1:
238 c->fec_inner = FEC_3_5;
239 break;
240 case 2:
241 c->fec_inner = FEC_2_3;
242 break;
243 case 3:
244 c->fec_inner = FEC_3_4;
245 break;
246 case 4:
247 c->fec_inner = FEC_4_5;
248 break;
249 case 5:
250 c->fec_inner = FEC_5_6;
251 break;
252 }
253
254 switch ((buf[1] >> 0) & 0x07) {
255 case 0:
256 c->modulation = QPSK;
257 break;
258 case 1:
259 c->modulation = QAM_16;
260 break;
261 case 2:
262 c->modulation = QAM_64;
263 break;
264 case 3:
265 c->modulation = QAM_256;
266 break;
267 }
268
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300269 ret = regmap_read(priv->regmap[0], 0x20b5, &utmp);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300270 if (ret)
271 goto error;
272
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300273 switch ((utmp >> 4) & 0x01) {
Antti Palosaari27cfc852011-04-07 16:27:43 -0300274 case 0:
275 c->inversion = INVERSION_OFF;
276 break;
277 case 1:
278 c->inversion = INVERSION_ON;
279 break;
280 }
281
282 return ret;
283error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300284 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300285 return ret;
286}
287
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300288int cxd2820r_read_status_t2(struct dvb_frontend *fe, enum fe_status *status)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300289{
290 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaari91171fb2016-08-08 21:12:00 -0300291 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300292 struct i2c_client *client = priv->client[0];
Antti Palosaari27cfc852011-04-07 16:27:43 -0300293 int ret;
Antti Palosaarid51dc912016-08-12 16:58:05 -0300294 unsigned int utmp, utmp1, utmp2;
Antti Palosaari91171fb2016-08-08 21:12:00 -0300295 u8 buf[4];
Antti Palosaari27cfc852011-04-07 16:27:43 -0300296
Antti Palosaarid51dc912016-08-12 16:58:05 -0300297 /* Lock detection */
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300298 ret = regmap_bulk_read(priv->regmap[0], 0x2010, &buf[0], 1);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300299 if (ret)
300 goto error;
301
Antti Palosaarid51dc912016-08-12 16:58:05 -0300302 utmp1 = (buf[0] >> 0) & 0x07;
303 utmp2 = (buf[0] >> 5) & 0x01;
304
305 if (utmp1 == 6 && utmp2 == 1) {
306 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
307 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
308 } else if (utmp1 == 6 || utmp2 == 1) {
309 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
310 FE_HAS_VITERBI | FE_HAS_SYNC;
311 } else {
312 *status = 0;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300313 }
314
Antti Palosaarid51dc912016-08-12 16:58:05 -0300315 dev_dbg(&client->dev, "status=%02x raw=%*ph sync=%u ts=%u\n",
316 *status, 1, buf, utmp1, utmp2);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300317
Antti Palosaari91171fb2016-08-08 21:12:00 -0300318 /* Signal strength */
319 if (*status & FE_HAS_SIGNAL) {
320 unsigned int strength;
321
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300322 ret = regmap_bulk_read(priv->regmap[0], 0x2026, buf, 2);
Antti Palosaari91171fb2016-08-08 21:12:00 -0300323 if (ret)
324 goto error;
325
326 utmp = buf[0] << 8 | buf[1] << 0;
327 utmp = ~utmp & 0x0fff;
328 /* Scale value to 0x0000-0xffff */
329 strength = utmp << 4 | utmp >> 8;
330
331 c->strength.len = 1;
332 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
333 c->strength.stat[0].uvalue = strength;
334 } else {
335 c->strength.len = 1;
336 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
337 }
338
339 /* CNR */
340 if (*status & FE_HAS_VITERBI) {
341 unsigned int cnr;
342
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300343 ret = regmap_bulk_read(priv->regmap[0], 0x2028, buf, 2);
Antti Palosaari91171fb2016-08-08 21:12:00 -0300344 if (ret)
345 goto error;
346
347 utmp = buf[0] << 8 | buf[1] << 0;
348 utmp = utmp & 0x0fff;
349 #define CXD2820R_LOG10_8_24 15151336 /* log10(8) << 24 */
350 if (utmp)
351 cnr = div_u64((u64)(intlog10(utmp)
352 - CXD2820R_LOG10_8_24) * 10000,
353 (1 << 24));
354 else
355 cnr = 0;
356
357 c->cnr.len = 1;
358 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
359 c->cnr.stat[0].svalue = cnr;
360 } else {
361 c->cnr.len = 1;
362 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
363 }
364
365 /* BER */
366 if (*status & FE_HAS_SYNC) {
367 unsigned int post_bit_error;
368
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300369 ret = regmap_bulk_read(priv->regmap[0], 0x2039, buf, 4);
Antti Palosaari91171fb2016-08-08 21:12:00 -0300370 if (ret)
371 goto error;
372
373 if ((buf[0] >> 4) & 0x01) {
374 post_bit_error = buf[0] << 24 | buf[1] << 16 |
375 buf[2] << 8 | buf[3] << 0;
376 post_bit_error &= 0x0fffffff;
377 } else {
378 post_bit_error = 0;
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_sleep_t2(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 Palosaari43e2ea62016-08-13 13:19:05 -0300401 int ret;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300402 struct reg_val_mask tab[] = {
403 { 0x000ff, 0x1f, 0xff },
404 { 0x00085, 0x00, 0xff },
405 { 0x00088, 0x01, 0xff },
406 { 0x02069, 0x00, 0xff },
407 { 0x00081, 0x00, 0xff },
408 { 0x00080, 0x00, 0xff },
409 };
410
Antti Palosaaric98975f2016-08-09 22:00:37 -0300411 dev_dbg(&client->dev, "\n");
Antti Palosaari27cfc852011-04-07 16:27:43 -0300412
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300413 ret = cxd2820r_wr_reg_val_mask_tab(priv, tab, ARRAY_SIZE(tab));
414 if (ret)
415 goto error;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300416
417 priv->delivery_system = SYS_UNDEFINED;
418
419 return ret;
420error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300421 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300422 return ret;
423}
424
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300425int cxd2820r_get_tune_settings_t2(struct dvb_frontend *fe,
Antti Palosaari27cfc852011-04-07 16:27:43 -0300426 struct dvb_frontend_tune_settings *s)
427{
428 s->min_delay_ms = 1500;
429 s->step_size = fe->ops.info.frequency_stepsize * 2;
430 s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
431
432 return 0;
433}