blob: 957ec94c72439a7fc6ba71e224901e558a892955 [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 },
45 { 0x1001f, priv->cfg.if_agc_polarity << 7, 0x80 },
46 { 0x10070, priv->cfg.ts_mode, 0xff },
CrazyCat4d1ab182014-06-03 14:19:07 -030047 { 0x10071, !priv->cfg.ts_clock_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
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300152int cxd2820r_read_ber_c(struct dvb_frontend *fe, u32 *ber)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300153{
154 struct cxd2820r_priv *priv = fe->demodulator_priv;
155 int ret;
156 u8 buf[3], start_ber = 0;
157 *ber = 0;
158
159 if (priv->ber_running) {
160 ret = cxd2820r_rd_regs(priv, 0x10076, buf, sizeof(buf));
161 if (ret)
162 goto error;
163
164 if ((buf[2] >> 7) & 0x01 || (buf[2] >> 4) & 0x01) {
165 *ber = (buf[2] & 0x0f) << 16 | buf[1] << 8 | buf[0];
166 start_ber = 1;
167 }
168 } else {
Mauro Carvalho Chehab285c0b02014-09-03 15:22:02 -0300169 priv->ber_running = true;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300170 start_ber = 1;
171 }
172
173 if (start_ber) {
174 /* (re)start BER */
175 ret = cxd2820r_wr_reg(priv, 0x10079, 0x01);
176 if (ret)
177 goto error;
178 }
179
180 return ret;
181error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300182 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300183 return ret;
184}
185
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300186int cxd2820r_read_signal_strength_c(struct dvb_frontend *fe,
Antti Palosaari27cfc852011-04-07 16:27:43 -0300187 u16 *strength)
188{
189 struct cxd2820r_priv *priv = fe->demodulator_priv;
190 int ret;
191 u8 buf[2];
192 u16 tmp;
193
194 ret = cxd2820r_rd_regs(priv, 0x10049, buf, sizeof(buf));
195 if (ret)
196 goto error;
197
198 tmp = (buf[0] & 0x03) << 8 | buf[1];
199 tmp = (~tmp & 0x03ff);
200
201 if (tmp == 512)
202 /* ~no signal */
203 tmp = 0;
204 else if (tmp > 350)
205 tmp = 350;
206
207 /* scale value to 0x0000-0xffff */
208 *strength = tmp * 0xffff / (350-0);
209
210 return ret;
211error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300212 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300213 return ret;
214}
215
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300216int cxd2820r_read_snr_c(struct dvb_frontend *fe, u16 *snr)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300217{
218 struct cxd2820r_priv *priv = fe->demodulator_priv;
219 int ret;
220 u8 tmp;
221 unsigned int A, B;
222 /* report SNR in dB * 10 */
223
224 ret = cxd2820r_rd_reg(priv, 0x10019, &tmp);
225 if (ret)
226 goto error;
227
228 if (((tmp >> 0) & 0x03) % 2) {
229 A = 875;
230 B = 650;
231 } else {
232 A = 950;
233 B = 760;
234 }
235
236 ret = cxd2820r_rd_reg(priv, 0x1004d, &tmp);
237 if (ret)
238 goto error;
239
240 #define CXD2820R_LOG2_E_24 24204406 /* log2(e) << 24 */
241 if (tmp)
242 *snr = A * (intlog2(B / tmp) >> 5) / (CXD2820R_LOG2_E_24 >> 5)
243 / 10;
244 else
245 *snr = 0;
246
247 return ret;
248error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300249 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300250 return ret;
251}
252
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300253int cxd2820r_read_ucblocks_c(struct dvb_frontend *fe, u32 *ucblocks)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300254{
255 *ucblocks = 0;
256 /* no way to read ? */
257 return 0;
258}
259
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300260int cxd2820r_read_status_c(struct dvb_frontend *fe, enum fe_status *status)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300261{
262 struct cxd2820r_priv *priv = fe->demodulator_priv;
263 int ret;
264 u8 buf[2];
265 *status = 0;
266
267 ret = cxd2820r_rd_regs(priv, 0x10088, buf, sizeof(buf));
268 if (ret)
269 goto error;
270
271 if (((buf[0] >> 0) & 0x01) == 1) {
272 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
273 FE_HAS_VITERBI | FE_HAS_SYNC;
274
275 if (((buf[1] >> 3) & 0x01) == 1) {
276 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
277 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
278 }
279 }
280
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300281 dev_dbg(&priv->i2c->dev, "%s: lock=%02x %02x\n", __func__, buf[0],
282 buf[1]);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300283
284 return ret;
285error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300286 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300287 return ret;
288}
289
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300290int cxd2820r_init_c(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300291{
292 struct cxd2820r_priv *priv = fe->demodulator_priv;
293 int ret;
294
295 ret = cxd2820r_wr_reg(priv, 0x00085, 0x07);
296 if (ret)
297 goto error;
298
299 return ret;
300error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300301 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300302 return ret;
303}
304
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300305int cxd2820r_sleep_c(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300306{
307 struct cxd2820r_priv *priv = fe->demodulator_priv;
308 int ret, i;
309 struct reg_val_mask tab[] = {
310 { 0x000ff, 0x1f, 0xff },
311 { 0x00085, 0x00, 0xff },
312 { 0x00088, 0x01, 0xff },
313 { 0x00081, 0x00, 0xff },
314 { 0x00080, 0x00, 0xff },
315 };
316
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300317 dev_dbg(&priv->i2c->dev, "%s\n", __func__);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300318
319 priv->delivery_system = SYS_UNDEFINED;
320
321 for (i = 0; i < ARRAY_SIZE(tab); i++) {
322 ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val,
323 tab[i].mask);
324 if (ret)
325 goto error;
326 }
327
328 return ret;
329error:
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300330 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300331 return ret;
332}
333
Steve Kerrison9ac51c52011-05-02 18:19:13 -0300334int cxd2820r_get_tune_settings_c(struct dvb_frontend *fe,
Antti Palosaari27cfc852011-04-07 16:27:43 -0300335 struct dvb_frontend_tune_settings *s)
336{
337 s->min_delay_ms = 500;
338 s->step_size = 0; /* no zigzag */
339 s->max_drift = 0;
340
341 return 0;
342}