blob: 0d036e1a43e2ca7dfbe1a9dc9da56ebc3c0ffe45 [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;
Antti Palosaarifcd09f62016-08-08 15:54:10 -030029 unsigned int utmp;
Antti Palosaari27cfc852011-04-07 16:27:43 -030030 u8 buf[2];
Antti Palosaarifcd09f62016-08-08 15:54:10 -030031 u32 if_frequency;
Antti Palosaari27cfc852011-04-07 16:27:43 -030032 struct reg_val_mask tab[] = {
33 { 0x00080, 0x01, 0xff },
34 { 0x00081, 0x05, 0xff },
35 { 0x00085, 0x07, 0xff },
36 { 0x00088, 0x01, 0xff },
37
38 { 0x00082, 0x20, 0x60 },
39 { 0x1016a, 0x48, 0xff },
40 { 0x100a5, 0x00, 0x01 },
41 { 0x10020, 0x06, 0x07 },
42 { 0x10059, 0x50, 0xff },
43 { 0x10087, 0x0c, 0x3c },
44 { 0x1008b, 0x07, 0xff },
Antti Palosaari07fdf7d2016-08-09 20:49:09 -030045 { 0x1001f, priv->if_agc_polarity << 7, 0x80 },
46 { 0x10070, priv->ts_mode, 0xff },
47 { 0x10071, !priv->ts_clk_inv << 4, 0x10 },
Antti Palosaari27cfc852011-04-07 16:27:43 -030048 };
49
Antti Palosaari75aeafc2012-07-19 13:10:12 -030050 dev_dbg(&priv->i2c->dev, "%s: frequency=%d symbol_rate=%d\n", __func__,
51 c->frequency, c->symbol_rate);
Antti Palosaari27cfc852011-04-07 16:27:43 -030052
Antti Palosaari27cfc852011-04-07 16:27:43 -030053 /* program tuner */
54 if (fe->ops.tuner_ops.set_params)
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -030055 fe->ops.tuner_ops.set_params(fe);
Antti Palosaari27cfc852011-04-07 16:27:43 -030056
Mauro Carvalho Chehab03494712011-12-22 18:11:39 -030057 if (priv->delivery_system != SYS_DVBC_ANNEX_A) {
Antti Palosaari27cfc852011-04-07 16:27:43 -030058 for (i = 0; i < ARRAY_SIZE(tab); i++) {
59 ret = cxd2820r_wr_reg_mask(priv, tab[i].reg,
60 tab[i].val, tab[i].mask);
61 if (ret)
62 goto error;
63 }
64 }
65
Mauro Carvalho Chehab03494712011-12-22 18:11:39 -030066 priv->delivery_system = SYS_DVBC_ANNEX_A;
Mauro Carvalho Chehab285c0b02014-09-03 15:22:02 -030067 priv->ber_running = false; /* tune stops BER counter */
Antti Palosaari27cfc852011-04-07 16:27:43 -030068
Antti Palosaarifda23fa2011-11-13 14:41:25 -030069 /* program IF frequency */
70 if (fe->ops.tuner_ops.get_if_frequency) {
Antti Palosaarifcd09f62016-08-08 15:54:10 -030071 ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
Antti Palosaarifda23fa2011-11-13 14:41:25 -030072 if (ret)
73 goto error;
Antti Palosaarifcd09f62016-08-08 15:54:10 -030074 dev_dbg(&priv->i2c->dev, "%s: if_frequency=%u\n", __func__,
75 if_frequency);
76 } else {
77 ret = -EINVAL;
78 goto error;
79 }
Antti Palosaarifda23fa2011-11-13 14:41:25 -030080
Antti Palosaarifcd09f62016-08-08 15:54:10 -030081 utmp = 0x4000 - DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x4000, CXD2820R_CLK);
82 buf[0] = (utmp >> 8) & 0xff;
83 buf[1] = (utmp >> 0) & 0xff;
Antti Palosaari27cfc852011-04-07 16:27:43 -030084 ret = cxd2820r_wr_regs(priv, 0x10042, buf, 2);
85 if (ret)
86 goto error;
87
88 ret = cxd2820r_wr_reg(priv, 0x000ff, 0x08);
89 if (ret)
90 goto error;
91
92 ret = cxd2820r_wr_reg(priv, 0x000fe, 0x01);
93 if (ret)
94 goto error;
95
96 return ret;
97error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -030098 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -030099 return ret;
100}
101
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -0200102int cxd2820r_get_frontend_c(struct dvb_frontend *fe,
103 struct dtv_frontend_properties *c)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300104{
105 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300106 int ret;
107 u8 buf[2];
108
109 ret = cxd2820r_rd_regs(priv, 0x1001a, buf, 2);
110 if (ret)
111 goto error;
112
113 c->symbol_rate = 2500 * ((buf[0] & 0x0f) << 8 | buf[1]);
114
115 ret = cxd2820r_rd_reg(priv, 0x10019, &buf[0]);
116 if (ret)
117 goto error;
118
Janne Huttunencba5d0b2012-05-30 05:28:46 -0300119 switch ((buf[0] >> 0) & 0x07) {
Antti Palosaari27cfc852011-04-07 16:27:43 -0300120 case 0:
121 c->modulation = QAM_16;
122 break;
123 case 1:
124 c->modulation = QAM_32;
125 break;
126 case 2:
127 c->modulation = QAM_64;
128 break;
129 case 3:
130 c->modulation = QAM_128;
131 break;
132 case 4:
133 c->modulation = QAM_256;
134 break;
135 }
136
137 switch ((buf[0] >> 7) & 0x01) {
138 case 0:
139 c->inversion = INVERSION_OFF;
140 break;
141 case 1:
142 c->inversion = INVERSION_ON;
143 break;
144 }
145
146 return ret;
147error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300148 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300149 return ret;
150}
151
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300152int cxd2820r_read_status_c(struct dvb_frontend *fe, enum fe_status *status)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300153{
154 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300155 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300156 int ret;
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300157 unsigned int utmp;
158 u8 buf[3];
Antti Palosaari27cfc852011-04-07 16:27:43 -0300159 *status = 0;
160
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300161 ret = cxd2820r_rd_regs(priv, 0x10088, buf, 2);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300162 if (ret)
163 goto error;
164
165 if (((buf[0] >> 0) & 0x01) == 1) {
166 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
167 FE_HAS_VITERBI | FE_HAS_SYNC;
168
169 if (((buf[1] >> 3) & 0x01) == 1) {
170 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
171 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
172 }
173 }
174
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300175 dev_dbg(&priv->i2c->dev, "%s: lock=%02x %02x\n", __func__, buf[0],
176 buf[1]);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300177
Antti Palosaari90d5d2e2016-08-08 23:03:32 -0300178 /* Signal strength */
179 if (*status & FE_HAS_SIGNAL) {
180 unsigned int strength;
181
182 ret = cxd2820r_rd_regs(priv, 0x10049, buf, 2);
183 if (ret)
184 goto error;
185
186 utmp = buf[0] << 8 | buf[1] << 0;
187 utmp = 511 - sign_extend32(utmp, 9);
188 /* Scale value to 0x0000-0xffff */
189 strength = utmp << 6 | utmp >> 4;
190
191 c->strength.len = 1;
192 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
193 c->strength.stat[0].uvalue = strength;
194 } else {
195 c->strength.len = 1;
196 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
197 }
198
199 /* CNR */
200 if (*status & FE_HAS_VITERBI) {
201 unsigned int cnr, const_a, const_b;
202
203 ret = cxd2820r_rd_reg(priv, 0x10019, &buf[0]);
204 if (ret)
205 goto error;
206
207 if (((buf[0] >> 0) & 0x03) % 2) {
208 const_a = 8750;
209 const_b = 650;
210 } else {
211 const_a = 9500;
212 const_b = 760;
213 }
214
215 ret = cxd2820r_rd_reg(priv, 0x1004d, &buf[0]);
216 if (ret)
217 goto error;
218
219 utmp = buf[0] << 0;
220 #define CXD2820R_LOG2_E_24 24204406 /* log2(e) << 24 */
221 if (utmp)
222 cnr = div_u64((u64)(intlog2(const_b) - intlog2(utmp))
223 * const_a, CXD2820R_LOG2_E_24);
224 else
225 cnr = 0;
226
227 c->cnr.len = 1;
228 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
229 c->cnr.stat[0].svalue = cnr;
230 } else {
231 c->cnr.len = 1;
232 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
233 }
234
235 /* BER */
236 if (*status & FE_HAS_SYNC) {
237 unsigned int post_bit_error;
238 bool start_ber;
239
240 if (priv->ber_running) {
241 ret = cxd2820r_rd_regs(priv, 0x10076, buf, 3);
242 if (ret)
243 goto error;
244
245 if ((buf[2] >> 7) & 0x01) {
246 post_bit_error = buf[2] << 16 | buf[1] << 8 |
247 buf[0] << 0;
248 post_bit_error &= 0x0fffff;
249 start_ber = true;
250 } else {
251 post_bit_error = 0;
252 start_ber = false;
253 }
254 } else {
255 post_bit_error = 0;
256 start_ber = true;
257 }
258
259 if (start_ber) {
260 ret = cxd2820r_wr_reg(priv, 0x10079, 0x01);
261 if (ret)
262 goto error;
263 priv->ber_running = true;
264 }
265
266 priv->post_bit_error += post_bit_error;
267
268 c->post_bit_error.len = 1;
269 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
270 c->post_bit_error.stat[0].uvalue = priv->post_bit_error;
271 } else {
272 c->post_bit_error.len = 1;
273 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
274 }
275
Antti Palosaari27cfc852011-04-07 16:27:43 -0300276 return ret;
277error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300278 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300279 return ret;
280}
281
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300282int cxd2820r_init_c(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300283{
284 struct cxd2820r_priv *priv = fe->demodulator_priv;
285 int ret;
286
287 ret = cxd2820r_wr_reg(priv, 0x00085, 0x07);
288 if (ret)
289 goto error;
290
291 return ret;
292error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300293 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300294 return ret;
295}
296
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300297int cxd2820r_sleep_c(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300298{
299 struct cxd2820r_priv *priv = fe->demodulator_priv;
300 int ret, i;
301 struct reg_val_mask tab[] = {
302 { 0x000ff, 0x1f, 0xff },
303 { 0x00085, 0x00, 0xff },
304 { 0x00088, 0x01, 0xff },
305 { 0x00081, 0x00, 0xff },
306 { 0x00080, 0x00, 0xff },
307 };
308
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300309 dev_dbg(&priv->i2c->dev, "%s\n", __func__);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300310
311 priv->delivery_system = SYS_UNDEFINED;
312
313 for (i = 0; i < ARRAY_SIZE(tab); i++) {
314 ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val,
315 tab[i].mask);
316 if (ret)
317 goto error;
318 }
319
320 return ret;
321error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300322 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300323 return ret;
324}
325
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300326int cxd2820r_get_tune_settings_c(struct dvb_frontend *fe,
Antti Palosaari27cfc852011-04-07 16:27:43 -0300327 struct dvb_frontend_tune_settings *s)
328{
329 s->min_delay_ms = 500;
330 s->step_size = 0; /* no zigzag */
331 s->max_drift = 0;
332
333 return 0;
334}