blob: 6732a9843f139bf7b25d096c3b763a65cc2dc894 [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
22static int cxd2820r_set_frontend_t(struct dvb_frontend *fe,
23 struct dvb_frontend_parameters *p)
24{
25 struct cxd2820r_priv *priv = fe->demodulator_priv;
26 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
27 int ret, i;
28 u32 if_khz, if_ctl;
29 u64 num;
30 u8 buf[3], bw_param;
31 u8 bw_params1[][5] = {
32 { 0x17, 0xea, 0xaa, 0xaa, 0xaa }, /* 6 MHz */
33 { 0x14, 0x80, 0x00, 0x00, 0x00 }, /* 7 MHz */
34 { 0x11, 0xf0, 0x00, 0x00, 0x00 }, /* 8 MHz */
35 };
36 u8 bw_params2[][2] = {
37 { 0x1f, 0xdc }, /* 6 MHz */
38 { 0x12, 0xf8 }, /* 7 MHz */
39 { 0x01, 0xe0 }, /* 8 MHz */
40 };
41 struct reg_val_mask tab[] = {
42 { 0x00080, 0x00, 0xff },
43 { 0x00081, 0x03, 0xff },
44 { 0x00085, 0x07, 0xff },
45 { 0x00088, 0x01, 0xff },
46
47 { 0x00070, priv->cfg.ts_mode, 0xff },
48 { 0x000cb, priv->cfg.if_agc_polarity << 6, 0x40 },
49 { 0x000a5, 0x00, 0x01 },
50 { 0x00082, 0x20, 0x60 },
51 { 0x000c2, 0xc3, 0xff },
52 { 0x0016a, 0x50, 0xff },
53 { 0x00427, 0x41, 0xff },
54 };
55
56 dbg("%s: RF=%d BW=%d", __func__, c->frequency, c->bandwidth_hz);
57
58 /* update GPIOs */
59 ret = cxd2820r_gpio(fe);
60 if (ret)
61 goto error;
62
63 /* program tuner */
64 if (fe->ops.tuner_ops.set_params)
65 fe->ops.tuner_ops.set_params(fe, p);
66
67 if (priv->delivery_system != SYS_DVBT) {
68 for (i = 0; i < ARRAY_SIZE(tab); i++) {
69 ret = cxd2820r_wr_reg_mask(priv, tab[i].reg,
70 tab[i].val, tab[i].mask);
71 if (ret)
72 goto error;
73 }
74 }
75
76 priv->delivery_system = SYS_DVBT;
77 priv->ber_running = 0; /* tune stops BER counter */
78
79 switch (c->bandwidth_hz) {
80 case 6000000:
81 if_khz = priv->cfg.if_dvbt_6;
82 i = 0;
83 bw_param = 2;
84 break;
85 case 7000000:
86 if_khz = priv->cfg.if_dvbt_7;
87 i = 1;
88 bw_param = 1;
89 break;
90 case 8000000:
91 if_khz = priv->cfg.if_dvbt_8;
92 i = 2;
93 bw_param = 0;
94 break;
95 default:
96 return -EINVAL;
97 }
98
99 num = if_khz;
100 num *= 0x1000000;
101 if_ctl = cxd2820r_div_u64_round_closest(num, 41000);
102 buf[0] = ((if_ctl >> 16) & 0xff);
103 buf[1] = ((if_ctl >> 8) & 0xff);
104 buf[2] = ((if_ctl >> 0) & 0xff);
105
106 ret = cxd2820r_wr_regs(priv, 0x000b6, buf, 3);
107 if (ret)
108 goto error;
109
110 ret = cxd2820r_wr_regs(priv, 0x0009f, bw_params1[i], 5);
111 if (ret)
112 goto error;
113
114 ret = cxd2820r_wr_reg_mask(priv, 0x000d7, bw_param << 6, 0xc0);
115 if (ret)
116 goto error;
117
118 ret = cxd2820r_wr_regs(priv, 0x000d9, bw_params2[i], 2);
119 if (ret)
120 goto error;
121
122 ret = cxd2820r_wr_reg(priv, 0x000ff, 0x08);
123 if (ret)
124 goto error;
125
126 ret = cxd2820r_wr_reg(priv, 0x000fe, 0x01);
127 if (ret)
128 goto error;
129
130 return ret;
131error:
132 dbg("%s: failed:%d", __func__, ret);
133 return ret;
134}
135
136static int cxd2820r_get_frontend_t(struct dvb_frontend *fe,
137 struct dvb_frontend_parameters *p)
138{
139 struct cxd2820r_priv *priv = fe->demodulator_priv;
140 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
141 int ret;
142 u8 buf[2];
143
144 ret = cxd2820r_rd_regs(priv, 0x0002f, buf, sizeof(buf));
145 if (ret)
146 goto error;
147
148 switch ((buf[0] >> 6) & 0x03) {
149 case 0:
150 c->modulation = QPSK;
151 break;
152 case 1:
153 c->modulation = QAM_16;
154 break;
155 case 2:
156 c->modulation = QAM_64;
157 break;
158 }
159
160 switch ((buf[1] >> 1) & 0x03) {
161 case 0:
162 c->transmission_mode = TRANSMISSION_MODE_2K;
163 break;
164 case 1:
165 c->transmission_mode = TRANSMISSION_MODE_8K;
166 break;
167 }
168
169 switch ((buf[1] >> 3) & 0x03) {
170 case 0:
171 c->guard_interval = GUARD_INTERVAL_1_32;
172 break;
173 case 1:
174 c->guard_interval = GUARD_INTERVAL_1_16;
175 break;
176 case 2:
177 c->guard_interval = GUARD_INTERVAL_1_8;
178 break;
179 case 3:
180 c->guard_interval = GUARD_INTERVAL_1_4;
181 break;
182 }
183
184 switch ((buf[0] >> 3) & 0x07) {
185 case 0:
186 c->hierarchy = HIERARCHY_NONE;
187 break;
188 case 1:
189 c->hierarchy = HIERARCHY_1;
190 break;
191 case 2:
192 c->hierarchy = HIERARCHY_2;
193 break;
194 case 3:
195 c->hierarchy = HIERARCHY_4;
196 break;
197 }
198
199 switch ((buf[0] >> 0) & 0x07) {
200 case 0:
201 c->code_rate_HP = FEC_1_2;
202 break;
203 case 1:
204 c->code_rate_HP = FEC_2_3;
205 break;
206 case 2:
207 c->code_rate_HP = FEC_3_4;
208 break;
209 case 3:
210 c->code_rate_HP = FEC_5_6;
211 break;
212 case 4:
213 c->code_rate_HP = FEC_7_8;
214 break;
215 }
216
217 switch ((buf[1] >> 5) & 0x07) {
218 case 0:
219 c->code_rate_LP = FEC_1_2;
220 break;
221 case 1:
222 c->code_rate_LP = FEC_2_3;
223 break;
224 case 2:
225 c->code_rate_LP = FEC_3_4;
226 break;
227 case 3:
228 c->code_rate_LP = FEC_5_6;
229 break;
230 case 4:
231 c->code_rate_LP = FEC_7_8;
232 break;
233 }
234
235 ret = cxd2820r_rd_reg(priv, 0x007c6, &buf[0]);
236 if (ret)
237 goto error;
238
239 switch ((buf[0] >> 0) & 0x01) {
240 case 0:
241 c->inversion = INVERSION_OFF;
242 break;
243 case 1:
244 c->inversion = INVERSION_ON;
245 break;
246 }
247
248 return ret;
249error:
250 dbg("%s: failed:%d", __func__, ret);
251 return ret;
252}
253
254static int cxd2820r_read_ber_t(struct dvb_frontend *fe, u32 *ber)
255{
256 struct cxd2820r_priv *priv = fe->demodulator_priv;
257 int ret;
258 u8 buf[3], start_ber = 0;
259 *ber = 0;
260
261 if (priv->ber_running) {
262 ret = cxd2820r_rd_regs(priv, 0x00076, buf, sizeof(buf));
263 if (ret)
264 goto error;
265
266 if ((buf[2] >> 7) & 0x01 || (buf[2] >> 4) & 0x01) {
267 *ber = (buf[2] & 0x0f) << 16 | buf[1] << 8 | buf[0];
268 start_ber = 1;
269 }
270 } else {
271 priv->ber_running = 1;
272 start_ber = 1;
273 }
274
275 if (start_ber) {
276 /* (re)start BER */
277 ret = cxd2820r_wr_reg(priv, 0x00079, 0x01);
278 if (ret)
279 goto error;
280 }
281
282 return ret;
283error:
284 dbg("%s: failed:%d", __func__, ret);
285 return ret;
286}
287
288static int cxd2820r_read_signal_strength_t(struct dvb_frontend *fe,
289 u16 *strength)
290{
291 struct cxd2820r_priv *priv = fe->demodulator_priv;
292 int ret;
293 u8 buf[2];
294 u16 tmp;
295
296 ret = cxd2820r_rd_regs(priv, 0x00026, buf, sizeof(buf));
297 if (ret)
298 goto error;
299
300 tmp = (buf[0] & 0x0f) << 8 | buf[1];
301 tmp = ~tmp & 0x0fff;
302
303 /* scale value to 0x0000-0xffff from 0x0000-0x0fff */
304 *strength = tmp * 0xffff / 0x0fff;
305
306 return ret;
307error:
308 dbg("%s: failed:%d", __func__, ret);
309 return ret;
310}
311
312static int cxd2820r_read_snr_t(struct dvb_frontend *fe, u16 *snr)
313{
314 struct cxd2820r_priv *priv = fe->demodulator_priv;
315 int ret;
316 u8 buf[2];
317 u16 tmp;
318 /* report SNR in dB * 10 */
319
320 ret = cxd2820r_rd_regs(priv, 0x00028, buf, sizeof(buf));
321 if (ret)
322 goto error;
323
324 tmp = (buf[0] & 0x1f) << 8 | buf[1];
325 #define CXD2820R_LOG10_8_24 15151336 /* log10(8) << 24 */
326 if (tmp)
327 *snr = (intlog10(tmp) - CXD2820R_LOG10_8_24) / ((1 << 24)
328 / 100);
329 else
330 *snr = 0;
331
332 dbg("%s: dBx10=%d val=%04x", __func__, *snr, tmp);
333
334 return ret;
335error:
336 dbg("%s: failed:%d", __func__, ret);
337 return ret;
338}
339
340static int cxd2820r_read_ucblocks_t(struct dvb_frontend *fe, u32 *ucblocks)
341{
342 *ucblocks = 0;
343 /* no way to read ? */
344 return 0;
345}
346
347static int cxd2820r_read_status_t(struct dvb_frontend *fe, fe_status_t *status)
348{
349 struct cxd2820r_priv *priv = fe->demodulator_priv;
350 int ret;
351 u8 buf[4];
352 *status = 0;
353
354 ret = cxd2820r_rd_reg(priv, 0x00010, &buf[0]);
355 if (ret)
356 goto error;
357
358 if ((buf[0] & 0x07) == 6) {
359 ret = cxd2820r_rd_reg(priv, 0x00073, &buf[1]);
360 if (ret)
361 goto error;
362
363 if (((buf[1] >> 3) & 0x01) == 1) {
364 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
365 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
366 } else {
367 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
368 FE_HAS_VITERBI | FE_HAS_SYNC;
369 }
370 } else {
371 ret = cxd2820r_rd_reg(priv, 0x00014, &buf[2]);
372 if (ret)
373 goto error;
374
375 if ((buf[2] & 0x0f) >= 4) {
376 ret = cxd2820r_rd_reg(priv, 0x00a14, &buf[3]);
377 if (ret)
378 goto error;
379
380 if (((buf[3] >> 4) & 0x01) == 1)
381 *status |= FE_HAS_SIGNAL;
382 }
383 }
384
385 dbg("%s: lock=%02x %02x %02x %02x", __func__,
386 buf[0], buf[1], buf[2], buf[3]);
387
388 return ret;
389error:
390 dbg("%s: failed:%d", __func__, ret);
391 return ret;
392}
393
394static int cxd2820r_init_t(struct dvb_frontend *fe)
395{
396 struct cxd2820r_priv *priv = fe->demodulator_priv;
397 int ret;
398
399 ret = cxd2820r_wr_reg(priv, 0x00085, 0x07);
400 if (ret)
401 goto error;
402
403 return ret;
404error:
405 dbg("%s: failed:%d", __func__, ret);
406 return ret;
407}
408
409static int cxd2820r_sleep_t(struct dvb_frontend *fe)
410{
411 struct cxd2820r_priv *priv = fe->demodulator_priv;
412 int ret, i;
413 struct reg_val_mask tab[] = {
414 { 0x000ff, 0x1f, 0xff },
415 { 0x00085, 0x00, 0xff },
416 { 0x00088, 0x01, 0xff },
417 { 0x00081, 0x00, 0xff },
418 { 0x00080, 0x00, 0xff },
419 };
420
421 dbg("%s", __func__);
422
423 priv->delivery_system = SYS_UNDEFINED;
424
425 for (i = 0; i < ARRAY_SIZE(tab); i++) {
426 ret = cxd2820r_wr_reg_mask(priv, tab[i].reg, tab[i].val,
427 tab[i].mask);
428 if (ret)
429 goto error;
430 }
431
432 return ret;
433error:
434 dbg("%s: failed:%d", __func__, ret);
435 return ret;
436}
437
438static int cxd2820r_get_tune_settings_t(struct dvb_frontend *fe,
439 struct dvb_frontend_tune_settings *s)
440{
441 s->min_delay_ms = 500;
442 s->step_size = fe->ops.info.frequency_stepsize * 2;
443 s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
444
445 return 0;
446}
447