blob: c3681d19b43e6b018b410d8213f44ba7f16192c8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 Conexant 22702 DVB OFDM demodulator driver
3
4 based on:
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -08005 Alps TDMB7 DVB OFDM demodulator driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
7 Copyright (C) 2001-2002 Convergence Integrated Media GmbH
8 Holger Waechtler <holger@convergence.de>
9
Steven Toth6d897612008-09-03 17:12:12 -030010 Copyright (C) 2004 Steven Toth <stoth@linuxtv.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
26*/
27
28#include <linux/kernel.h>
29#include <linux/init.h>
30#include <linux/module.h>
31#include <linux/string.h>
32#include <linux/slab.h>
33#include <linux/delay.h>
34#include "dvb_frontend.h"
35#include "cx22702.h"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037struct cx22702_state {
38
Steven Toth4e3599a2008-10-16 20:23:45 -030039 struct i2c_adapter *i2c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 /* configuration settings */
Steven Toth4e3599a2008-10-16 20:23:45 -030042 const struct cx22702_config *config;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 struct dvb_frontend frontend;
45
46 /* previous uncorrected block counter */
47 u8 prevUCBlocks;
48};
49
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030050static int debug;
Steven Toth4e3599a2008-10-16 20:23:45 -030051module_param(debug, int, 0644);
52MODULE_PARM_DESC(debug, "Enable verbose debug messages");
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define dprintk if (debug) printk
55
56/* Register values to initialise the demod */
Steven Toth4e3599a2008-10-16 20:23:45 -030057static u8 init_tab[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 0x00, 0x00, /* Stop aquisition */
59 0x0B, 0x06,
60 0x09, 0x01,
61 0x0D, 0x41,
62 0x16, 0x32,
63 0x20, 0x0A,
64 0x21, 0x17,
65 0x24, 0x3e,
66 0x26, 0xff,
67 0x27, 0x10,
68 0x28, 0x00,
69 0x29, 0x00,
70 0x2a, 0x10,
71 0x2b, 0x00,
72 0x2c, 0x10,
73 0x2d, 0x00,
74 0x48, 0xd4,
75 0x49, 0x56,
76 0x6b, 0x1e,
77 0xc8, 0x02,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 0xf9, 0x00,
79 0xfa, 0x00,
80 0xfb, 0x00,
81 0xfc, 0x00,
82 0xfd, 0x00,
83};
84
Steven Toth4e3599a2008-10-16 20:23:45 -030085static int cx22702_writereg(struct cx22702_state *state, u8 reg, u8 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 int ret;
Steven Toth4e3599a2008-10-16 20:23:45 -030088 u8 buf[] = { reg, data };
89 struct i2c_msg msg = {
90 .addr = state->config->demod_address, .flags = 0,
91 .buf = buf, .len = 2 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 ret = i2c_transfer(state->i2c, &msg, 1);
94
Jean Delvare24764102010-09-10 14:03:07 -030095 if (unlikely(ret != 1)) {
Steven Toth4e3599a2008-10-16 20:23:45 -030096 printk(KERN_ERR
97 "%s: error (reg == 0x%02x, val == 0x%02x, ret == %i)\n",
Harvey Harrison271ddbf2008-04-08 23:20:00 -030098 __func__, reg, data, ret);
Jean Delvare24764102010-09-10 14:03:07 -030099 return -1;
100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Jean Delvare24764102010-09-10 14:03:07 -0300102 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Steven Toth4e3599a2008-10-16 20:23:45 -0300105static u8 cx22702_readreg(struct cx22702_state *state, u8 reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 int ret;
Jean Delvare24764102010-09-10 14:03:07 -0300108 u8 data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Steven Toth4e3599a2008-10-16 20:23:45 -0300110 struct i2c_msg msg[] = {
111 { .addr = state->config->demod_address, .flags = 0,
Jean Delvare24764102010-09-10 14:03:07 -0300112 .buf = &reg, .len = 1 },
Steven Toth4e3599a2008-10-16 20:23:45 -0300113 { .addr = state->config->demod_address, .flags = I2C_M_RD,
Jean Delvare24764102010-09-10 14:03:07 -0300114 .buf = &data, .len = 1 } };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 ret = i2c_transfer(state->i2c, msg, 2);
117
Jean Delvare24764102010-09-10 14:03:07 -0300118 if (unlikely(ret != 2)) {
119 printk(KERN_ERR "%s: error (reg == 0x%02x, ret == %i)\n",
120 __func__, reg, ret);
121 return 0;
122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Jean Delvare24764102010-09-10 14:03:07 -0300124 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
Steven Toth4e3599a2008-10-16 20:23:45 -0300127static int cx22702_set_inversion(struct cx22702_state *state, int inversion)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 u8 val;
130
131 switch (inversion) {
Steven Toth4e3599a2008-10-16 20:23:45 -0300132 case INVERSION_AUTO:
133 return -EOPNOTSUPP;
134 case INVERSION_ON:
135 val = cx22702_readreg(state, 0x0C);
136 return cx22702_writereg(state, 0x0C, val | 0x01);
137 case INVERSION_OFF:
138 val = cx22702_readreg(state, 0x0C);
139 return cx22702_writereg(state, 0x0C, val & 0xfe);
140 default:
141 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 }
143
144}
145
146/* Retrieve the demod settings */
Steven Toth4e3599a2008-10-16 20:23:45 -0300147static int cx22702_get_tps(struct cx22702_state *state,
148 struct dvb_ofdm_parameters *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 u8 val;
151
152 /* Make sure the TPS regs are valid */
153 if (!(cx22702_readreg(state, 0x0A) & 0x20))
154 return -EAGAIN;
155
Steven Toth4e3599a2008-10-16 20:23:45 -0300156 val = cx22702_readreg(state, 0x01);
157 switch ((val & 0x18) >> 3) {
158 case 0:
159 p->constellation = QPSK;
160 break;
161 case 1:
162 p->constellation = QAM_16;
163 break;
164 case 2:
165 p->constellation = QAM_64;
166 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
Steven Toth4e3599a2008-10-16 20:23:45 -0300168 switch (val & 0x07) {
169 case 0:
170 p->hierarchy_information = HIERARCHY_NONE;
171 break;
172 case 1:
173 p->hierarchy_information = HIERARCHY_1;
174 break;
175 case 2:
176 p->hierarchy_information = HIERARCHY_2;
177 break;
178 case 3:
179 p->hierarchy_information = HIERARCHY_4;
180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182
183
Steven Toth4e3599a2008-10-16 20:23:45 -0300184 val = cx22702_readreg(state, 0x02);
185 switch ((val & 0x38) >> 3) {
186 case 0:
187 p->code_rate_HP = FEC_1_2;
188 break;
189 case 1:
190 p->code_rate_HP = FEC_2_3;
191 break;
192 case 2:
193 p->code_rate_HP = FEC_3_4;
194 break;
195 case 3:
196 p->code_rate_HP = FEC_5_6;
197 break;
198 case 4:
199 p->code_rate_HP = FEC_7_8;
200 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Steven Toth4e3599a2008-10-16 20:23:45 -0300202 switch (val & 0x07) {
203 case 0:
204 p->code_rate_LP = FEC_1_2;
205 break;
206 case 1:
207 p->code_rate_LP = FEC_2_3;
208 break;
209 case 2:
210 p->code_rate_LP = FEC_3_4;
211 break;
212 case 3:
213 p->code_rate_LP = FEC_5_6;
214 break;
215 case 4:
216 p->code_rate_LP = FEC_7_8;
217 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219
Steven Toth4e3599a2008-10-16 20:23:45 -0300220 val = cx22702_readreg(state, 0x03);
221 switch ((val & 0x0c) >> 2) {
222 case 0:
223 p->guard_interval = GUARD_INTERVAL_1_32;
224 break;
225 case 1:
226 p->guard_interval = GUARD_INTERVAL_1_16;
227 break;
228 case 2:
229 p->guard_interval = GUARD_INTERVAL_1_8;
230 break;
231 case 3:
232 p->guard_interval = GUARD_INTERVAL_1_4;
233 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
Steven Toth4e3599a2008-10-16 20:23:45 -0300235 switch (val & 0x03) {
236 case 0:
237 p->transmission_mode = TRANSMISSION_MODE_2K;
238 break;
239 case 1:
240 p->transmission_mode = TRANSMISSION_MODE_8K;
241 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
244 return 0;
245}
246
Steven Toth4e3599a2008-10-16 20:23:45 -0300247static int cx22702_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
Steven Toth611900c2006-01-09 15:25:12 -0200248{
Steven Toth4e3599a2008-10-16 20:23:45 -0300249 struct cx22702_state *state = fe->demodulator_priv;
250 dprintk("%s(%d)\n", __func__, enable);
Steven Toth611900c2006-01-09 15:25:12 -0200251 if (enable)
Steven Toth4e3599a2008-10-16 20:23:45 -0300252 return cx22702_writereg(state, 0x0D,
253 cx22702_readreg(state, 0x0D) & 0xfe);
Steven Toth611900c2006-01-09 15:25:12 -0200254 else
Steven Toth4e3599a2008-10-16 20:23:45 -0300255 return cx22702_writereg(state, 0x0D,
256 cx22702_readreg(state, 0x0D) | 1);
Steven Toth611900c2006-01-09 15:25:12 -0200257}
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/* Talk to the demod, set the FEC, GUARD, QAM settings etc */
Steven Toth4e3599a2008-10-16 20:23:45 -0300260static int cx22702_set_tps(struct dvb_frontend *fe,
261 struct dvb_frontend_parameters *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 u8 val;
Steven Toth4e3599a2008-10-16 20:23:45 -0300264 struct cx22702_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Patrick Boettcherdea74862006-05-14 05:01:31 -0300266 if (fe->ops.tuner_ops.set_params) {
267 fe->ops.tuner_ops.set_params(fe, p);
Steven Toth4e3599a2008-10-16 20:23:45 -0300268 if (fe->ops.i2c_gate_ctrl)
269 fe->ops.i2c_gate_ctrl(fe, 0);
Gerd Knorr9990d742005-05-01 08:59:20 -0700270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 /* set inversion */
Steven Toth4e3599a2008-10-16 20:23:45 -0300273 cx22702_set_inversion(state, p->inversion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /* set bandwidth */
Steven Toth4e3599a2008-10-16 20:23:45 -0300276 switch (p->u.ofdm.bandwidth) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 case BANDWIDTH_6_MHZ:
Steven Toth4e3599a2008-10-16 20:23:45 -0300278 cx22702_writereg(state, 0x0C,
279 (cx22702_readreg(state, 0x0C) & 0xcf) | 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 break;
281 case BANDWIDTH_7_MHZ:
Steven Toth4e3599a2008-10-16 20:23:45 -0300282 cx22702_writereg(state, 0x0C,
283 (cx22702_readreg(state, 0x0C) & 0xcf) | 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 break;
285 case BANDWIDTH_8_MHZ:
Steven Toth4e3599a2008-10-16 20:23:45 -0300286 cx22702_writereg(state, 0x0C,
287 cx22702_readreg(state, 0x0C) & 0xcf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 break;
289 default:
Steven Toth4e3599a2008-10-16 20:23:45 -0300290 dprintk("%s: invalid bandwidth\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return -EINVAL;
292 }
293
Steven Toth4e3599a2008-10-16 20:23:45 -0300294 p->u.ofdm.code_rate_LP = FEC_AUTO; /* temp hack as manual not working */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 /* use auto configuration? */
Steven Toth4e3599a2008-10-16 20:23:45 -0300297 if ((p->u.ofdm.hierarchy_information == HIERARCHY_AUTO) ||
298 (p->u.ofdm.constellation == QAM_AUTO) ||
299 (p->u.ofdm.code_rate_HP == FEC_AUTO) ||
300 (p->u.ofdm.code_rate_LP == FEC_AUTO) ||
301 (p->u.ofdm.guard_interval == GUARD_INTERVAL_AUTO) ||
302 (p->u.ofdm.transmission_mode == TRANSMISSION_MODE_AUTO)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /* TPS Source - use hardware driven values */
305 cx22702_writereg(state, 0x06, 0x10);
306 cx22702_writereg(state, 0x07, 0x9);
307 cx22702_writereg(state, 0x08, 0xC1);
Steven Toth4e3599a2008-10-16 20:23:45 -0300308 cx22702_writereg(state, 0x0B, cx22702_readreg(state, 0x0B)
309 & 0xfc);
310 cx22702_writereg(state, 0x0C,
311 (cx22702_readreg(state, 0x0C) & 0xBF) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 cx22702_writereg(state, 0x00, 0x01); /* Begin aquisition */
Steven Toth4e3599a2008-10-16 20:23:45 -0300313 dprintk("%s: Autodetecting\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return 0;
315 }
316
317 /* manually programmed values */
Steven Toth4e3599a2008-10-16 20:23:45 -0300318 val = 0;
319 switch (p->u.ofdm.constellation) {
320 case QPSK:
321 val = (val & 0xe7);
322 break;
323 case QAM_16:
324 val = (val & 0xe7) | 0x08;
325 break;
326 case QAM_64:
327 val = (val & 0xe7) | 0x10;
328 break;
329 default:
330 dprintk("%s: invalid constellation\n", __func__);
331 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
Steven Toth4e3599a2008-10-16 20:23:45 -0300333 switch (p->u.ofdm.hierarchy_information) {
334 case HIERARCHY_NONE:
335 val = (val & 0xf8);
336 break;
337 case HIERARCHY_1:
338 val = (val & 0xf8) | 1;
339 break;
340 case HIERARCHY_2:
341 val = (val & 0xf8) | 2;
342 break;
343 case HIERARCHY_4:
344 val = (val & 0xf8) | 3;
345 break;
346 default:
347 dprintk("%s: invalid hierarchy\n", __func__);
348 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Steven Toth4e3599a2008-10-16 20:23:45 -0300350 cx22702_writereg(state, 0x06, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Steven Toth4e3599a2008-10-16 20:23:45 -0300352 val = 0;
353 switch (p->u.ofdm.code_rate_HP) {
354 case FEC_NONE:
355 case FEC_1_2:
356 val = (val & 0xc7);
357 break;
358 case FEC_2_3:
359 val = (val & 0xc7) | 0x08;
360 break;
361 case FEC_3_4:
362 val = (val & 0xc7) | 0x10;
363 break;
364 case FEC_5_6:
365 val = (val & 0xc7) | 0x18;
366 break;
367 case FEC_7_8:
368 val = (val & 0xc7) | 0x20;
369 break;
370 default:
371 dprintk("%s: invalid code_rate_HP\n", __func__);
372 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Steven Toth4e3599a2008-10-16 20:23:45 -0300374 switch (p->u.ofdm.code_rate_LP) {
375 case FEC_NONE:
376 case FEC_1_2:
377 val = (val & 0xf8);
378 break;
379 case FEC_2_3:
380 val = (val & 0xf8) | 1;
381 break;
382 case FEC_3_4:
383 val = (val & 0xf8) | 2;
384 break;
385 case FEC_5_6:
386 val = (val & 0xf8) | 3;
387 break;
388 case FEC_7_8:
389 val = (val & 0xf8) | 4;
390 break;
391 default:
392 dprintk("%s: invalid code_rate_LP\n", __func__);
393 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Steven Toth4e3599a2008-10-16 20:23:45 -0300395 cx22702_writereg(state, 0x07, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Steven Toth4e3599a2008-10-16 20:23:45 -0300397 val = 0;
398 switch (p->u.ofdm.guard_interval) {
399 case GUARD_INTERVAL_1_32:
400 val = (val & 0xf3);
401 break;
402 case GUARD_INTERVAL_1_16:
403 val = (val & 0xf3) | 0x04;
404 break;
405 case GUARD_INTERVAL_1_8:
406 val = (val & 0xf3) | 0x08;
407 break;
408 case GUARD_INTERVAL_1_4:
409 val = (val & 0xf3) | 0x0c;
410 break;
411 default:
412 dprintk("%s: invalid guard_interval\n", __func__);
413 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Steven Toth4e3599a2008-10-16 20:23:45 -0300415 switch (p->u.ofdm.transmission_mode) {
416 case TRANSMISSION_MODE_2K:
417 val = (val & 0xfc);
418 break;
419 case TRANSMISSION_MODE_8K:
420 val = (val & 0xfc) | 1;
421 break;
422 default:
423 dprintk("%s: invalid transmission_mode\n", __func__);
424 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 cx22702_writereg(state, 0x08, val);
Steven Toth4e3599a2008-10-16 20:23:45 -0300427 cx22702_writereg(state, 0x0B,
428 (cx22702_readreg(state, 0x0B) & 0xfc) | 0x02);
429 cx22702_writereg(state, 0x0C,
430 (cx22702_readreg(state, 0x0C) & 0xBF) | 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /* Begin channel aquisition */
433 cx22702_writereg(state, 0x00, 0x01);
434
435 return 0;
436}
437
438/* Reset the demod hardware and reset all of the configuration registers
439 to a default state. */
Steven Toth4e3599a2008-10-16 20:23:45 -0300440static int cx22702_init(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 int i;
Steven Toth4e3599a2008-10-16 20:23:45 -0300443 struct cx22702_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Steven Toth4e3599a2008-10-16 20:23:45 -0300445 cx22702_writereg(state, 0x00, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 msleep(10);
448
Steven Toth4e3599a2008-10-16 20:23:45 -0300449 for (i = 0; i < ARRAY_SIZE(init_tab); i += 2)
450 cx22702_writereg(state, init_tab[i], init_tab[i + 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Steven Toth4e3599a2008-10-16 20:23:45 -0300452 cx22702_writereg(state, 0xf8, (state->config->output_mode << 1)
453 & 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Steven Toth611900c2006-01-09 15:25:12 -0200455 cx22702_i2c_gate_ctrl(fe, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 return 0;
458}
459
Steven Toth4e3599a2008-10-16 20:23:45 -0300460static int cx22702_read_status(struct dvb_frontend *fe, fe_status_t *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Steven Toth4e3599a2008-10-16 20:23:45 -0300462 struct cx22702_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 u8 reg0A;
464 u8 reg23;
465
466 *status = 0;
467
Steven Toth4e3599a2008-10-16 20:23:45 -0300468 reg0A = cx22702_readreg(state, 0x0A);
469 reg23 = cx22702_readreg(state, 0x23);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Steven Toth4e3599a2008-10-16 20:23:45 -0300471 dprintk("%s: status demod=0x%02x agc=0x%02x\n"
472 , __func__, reg0A, reg23);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Steven Toth4e3599a2008-10-16 20:23:45 -0300474 if (reg0A & 0x10) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 *status |= FE_HAS_LOCK;
476 *status |= FE_HAS_VITERBI;
477 *status |= FE_HAS_SYNC;
478 }
479
Steven Toth4e3599a2008-10-16 20:23:45 -0300480 if (reg0A & 0x20)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 *status |= FE_HAS_CARRIER;
482
Steven Toth4e3599a2008-10-16 20:23:45 -0300483 if (reg23 < 0xf0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 *status |= FE_HAS_SIGNAL;
485
486 return 0;
487}
488
Steven Toth4e3599a2008-10-16 20:23:45 -0300489static int cx22702_read_ber(struct dvb_frontend *fe, u32 *ber)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Steven Toth4e3599a2008-10-16 20:23:45 -0300491 struct cx22702_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Steven Toth4e3599a2008-10-16 20:23:45 -0300493 if (cx22702_readreg(state, 0xE4) & 0x02) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /* Realtime statistics */
Steven Toth4e3599a2008-10-16 20:23:45 -0300495 *ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7
496 | (cx22702_readreg(state, 0xDF) & 0x7F);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 } else {
498 /* Averagtine statistics */
Steven Toth4e3599a2008-10-16 20:23:45 -0300499 *ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7
500 | cx22702_readreg(state, 0xDF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502
503 return 0;
504}
505
Steven Toth4e3599a2008-10-16 20:23:45 -0300506static int cx22702_read_signal_strength(struct dvb_frontend *fe,
507 u16 *signal_strength)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Steven Toth4e3599a2008-10-16 20:23:45 -0300509 struct cx22702_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Jean Delvare5b9a6f32010-09-10 10:32:21 -0300511 u16 rs_ber;
Steven Toth4e3599a2008-10-16 20:23:45 -0300512 rs_ber = cx22702_readreg(state, 0x23);
Bradley Kite1e9dadb2006-09-02 21:14:27 -0300513 *signal_strength = (rs_ber << 8) | rs_ber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 return 0;
516}
517
Steven Toth4e3599a2008-10-16 20:23:45 -0300518static int cx22702_read_snr(struct dvb_frontend *fe, u16 *snr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Steven Toth4e3599a2008-10-16 20:23:45 -0300520 struct cx22702_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Jean Delvare5b9a6f32010-09-10 10:32:21 -0300522 u16 rs_ber;
Steven Toth4e3599a2008-10-16 20:23:45 -0300523 if (cx22702_readreg(state, 0xE4) & 0x02) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* Realtime statistics */
Steven Toth4e3599a2008-10-16 20:23:45 -0300525 rs_ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7
526 | (cx22702_readreg(state, 0xDF) & 0x7F);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 } else {
528 /* Averagine statistics */
Steven Toth4e3599a2008-10-16 20:23:45 -0300529 rs_ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 8
530 | cx22702_readreg(state, 0xDF);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 *snr = ~rs_ber;
533
534 return 0;
535}
536
Steven Toth4e3599a2008-10-16 20:23:45 -0300537static int cx22702_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Steven Toth4e3599a2008-10-16 20:23:45 -0300539 struct cx22702_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 u8 _ucblocks;
542
543 /* RS Uncorrectable Packet Count then reset */
Steven Toth4e3599a2008-10-16 20:23:45 -0300544 _ucblocks = cx22702_readreg(state, 0xE3);
Patrick Boettcherf46dbb02005-07-07 17:57:44 -0700545 if (state->prevUCBlocks < _ucblocks)
546 *ucblocks = (_ucblocks - state->prevUCBlocks);
547 else
548 *ucblocks = state->prevUCBlocks - _ucblocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 state->prevUCBlocks = _ucblocks;
550
551 return 0;
552}
553
Steven Toth4e3599a2008-10-16 20:23:45 -0300554static int cx22702_get_frontend(struct dvb_frontend *fe,
555 struct dvb_frontend_parameters *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Steven Toth4e3599a2008-10-16 20:23:45 -0300557 struct cx22702_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Steven Toth4e3599a2008-10-16 20:23:45 -0300559 u8 reg0C = cx22702_readreg(state, 0x0C);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 p->inversion = reg0C & 0x1 ? INVERSION_ON : INVERSION_OFF;
Steven Toth4e3599a2008-10-16 20:23:45 -0300562 return cx22702_get_tps(state, &p->u.ofdm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Steven Toth4e3599a2008-10-16 20:23:45 -0300565static int cx22702_get_tune_settings(struct dvb_frontend *fe,
566 struct dvb_frontend_tune_settings *tune)
Patrick Boettcherf46dbb02005-07-07 17:57:44 -0700567{
568 tune->min_delay_ms = 1000;
569 return 0;
570}
571
Steven Toth4e3599a2008-10-16 20:23:45 -0300572static void cx22702_release(struct dvb_frontend *fe)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Steven Toth4e3599a2008-10-16 20:23:45 -0300574 struct cx22702_state *state = fe->demodulator_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 kfree(state);
576}
577
578static struct dvb_frontend_ops cx22702_ops;
579
Steven Toth4e3599a2008-10-16 20:23:45 -0300580struct dvb_frontend *cx22702_attach(const struct cx22702_config *config,
581 struct i2c_adapter *i2c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
Steven Toth4e3599a2008-10-16 20:23:45 -0300583 struct cx22702_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* allocate memory for the internal state */
Matthias Schwarzott084e24a2009-08-10 22:51:01 -0300586 state = kzalloc(sizeof(struct cx22702_state), GFP_KERNEL);
Patrick Boettcherf46dbb02005-07-07 17:57:44 -0700587 if (state == NULL)
588 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
590 /* setup the state */
591 state->config = config;
592 state->i2c = i2c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* check if the demod is there */
Patrick Boettcherf46dbb02005-07-07 17:57:44 -0700595 if (cx22702_readreg(state, 0x1f) != 0x3)
596 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 /* create dvb_frontend */
Steven Toth4e3599a2008-10-16 20:23:45 -0300599 memcpy(&state->frontend.ops, &cx22702_ops,
600 sizeof(struct dvb_frontend_ops));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 state->frontend.demodulator_priv = state;
602 return &state->frontend;
603
604error:
605 kfree(state);
606 return NULL;
607}
Steven Toth4e3599a2008-10-16 20:23:45 -0300608EXPORT_SYMBOL(cx22702_attach);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610static struct dvb_frontend_ops cx22702_ops = {
611
612 .info = {
613 .name = "Conexant CX22702 DVB-T",
614 .type = FE_OFDM,
615 .frequency_min = 177000000,
616 .frequency_max = 858000000,
617 .frequency_stepsize = 166666,
618 .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
619 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
620 FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
621 FE_CAN_HIERARCHY_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
622 FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER
623 },
624
625 .release = cx22702_release,
626
627 .init = cx22702_init,
Andrew de Quincey02444222006-04-18 17:47:09 -0300628 .i2c_gate_ctrl = cx22702_i2c_gate_ctrl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630 .set_frontend = cx22702_set_tps,
631 .get_frontend = cx22702_get_frontend,
Patrick Boettcherf46dbb02005-07-07 17:57:44 -0700632 .get_tune_settings = cx22702_get_tune_settings,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 .read_status = cx22702_read_status,
635 .read_ber = cx22702_read_ber,
636 .read_signal_strength = cx22702_read_signal_strength,
637 .read_snr = cx22702_read_snr,
638 .read_ucblocks = cx22702_read_ucblocks,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639};
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641MODULE_DESCRIPTION("Conexant CX22702 DVB-T Demodulator driver");
642MODULE_AUTHOR("Steven Toth");
643MODULE_LICENSE("GPL");