Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | Conexant 22702 DVB OFDM demodulator driver |
| 3 | |
| 4 | based on: |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 5 | Alps TDMB7 DVB OFDM demodulator driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | |
| 7 | Copyright (C) 2001-2002 Convergence Integrated Media GmbH |
| 8 | Holger Waechtler <holger@convergence.de> |
| 9 | |
Steven Toth | 6d89761 | 2008-09-03 17:12:12 -0300 | [diff] [blame] | 10 | Copyright (C) 2004 Steven Toth <stoth@linuxtv.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | struct cx22702_state { |
| 38 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 39 | struct i2c_adapter *i2c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | /* configuration settings */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 42 | const struct cx22702_config *config; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | struct dvb_frontend frontend; |
| 45 | |
| 46 | /* previous uncorrected block counter */ |
| 47 | u8 prevUCBlocks; |
| 48 | }; |
| 49 | |
Douglas Schilling Landgraf | ff699e6 | 2008-04-22 14:41:48 -0300 | [diff] [blame] | 50 | static int debug; |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 51 | module_param(debug, int, 0644); |
| 52 | MODULE_PARM_DESC(debug, "Enable verbose debug messages"); |
| 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #define dprintk if (debug) printk |
| 55 | |
| 56 | /* Register values to initialise the demod */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 57 | static u8 init_tab[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | 0xf9, 0x00, |
| 79 | 0xfa, 0x00, |
| 80 | 0xfb, 0x00, |
| 81 | 0xfc, 0x00, |
| 82 | 0xfd, 0x00, |
| 83 | }; |
| 84 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 85 | static int cx22702_writereg(struct cx22702_state *state, u8 reg, u8 data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
| 87 | int ret; |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 88 | u8 buf[] = { reg, data }; |
| 89 | struct i2c_msg msg = { |
| 90 | .addr = state->config->demod_address, .flags = 0, |
| 91 | .buf = buf, .len = 2 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | |
| 93 | ret = i2c_transfer(state->i2c, &msg, 1); |
| 94 | |
| 95 | if (ret != 1) |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 96 | printk(KERN_ERR |
| 97 | "%s: error (reg == 0x%02x, val == 0x%02x, ret == %i)\n", |
Harvey Harrison | 271ddbf | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 98 | __func__, reg, data, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
| 100 | return (ret != 1) ? -1 : 0; |
| 101 | } |
| 102 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 103 | static u8 cx22702_readreg(struct cx22702_state *state, u8 reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
| 105 | int ret; |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 106 | u8 b0[] = { reg }; |
| 107 | u8 b1[] = { 0 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 109 | struct i2c_msg msg[] = { |
| 110 | { .addr = state->config->demod_address, .flags = 0, |
| 111 | .buf = b0, .len = 1 }, |
| 112 | { .addr = state->config->demod_address, .flags = I2C_M_RD, |
| 113 | .buf = b1, .len = 1 } }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
| 115 | ret = i2c_transfer(state->i2c, msg, 2); |
| 116 | |
| 117 | if (ret != 2) |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 118 | printk(KERN_ERR "%s: readreg error (ret == %i)\n", |
| 119 | __func__, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | return b1[0]; |
| 122 | } |
| 123 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 124 | static int cx22702_set_inversion(struct cx22702_state *state, int inversion) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
| 126 | u8 val; |
| 127 | |
| 128 | switch (inversion) { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 129 | case INVERSION_AUTO: |
| 130 | return -EOPNOTSUPP; |
| 131 | case INVERSION_ON: |
| 132 | val = cx22702_readreg(state, 0x0C); |
| 133 | return cx22702_writereg(state, 0x0C, val | 0x01); |
| 134 | case INVERSION_OFF: |
| 135 | val = cx22702_readreg(state, 0x0C); |
| 136 | return cx22702_writereg(state, 0x0C, val & 0xfe); |
| 137 | default: |
| 138 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | } |
| 142 | |
| 143 | /* Retrieve the demod settings */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 144 | static int cx22702_get_tps(struct cx22702_state *state, |
| 145 | struct dvb_ofdm_parameters *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | { |
| 147 | u8 val; |
| 148 | |
| 149 | /* Make sure the TPS regs are valid */ |
| 150 | if (!(cx22702_readreg(state, 0x0A) & 0x20)) |
| 151 | return -EAGAIN; |
| 152 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 153 | val = cx22702_readreg(state, 0x01); |
| 154 | switch ((val & 0x18) >> 3) { |
| 155 | case 0: |
| 156 | p->constellation = QPSK; |
| 157 | break; |
| 158 | case 1: |
| 159 | p->constellation = QAM_16; |
| 160 | break; |
| 161 | case 2: |
| 162 | p->constellation = QAM_64; |
| 163 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 165 | switch (val & 0x07) { |
| 166 | case 0: |
| 167 | p->hierarchy_information = HIERARCHY_NONE; |
| 168 | break; |
| 169 | case 1: |
| 170 | p->hierarchy_information = HIERARCHY_1; |
| 171 | break; |
| 172 | case 2: |
| 173 | p->hierarchy_information = HIERARCHY_2; |
| 174 | break; |
| 175 | case 3: |
| 176 | p->hierarchy_information = HIERARCHY_4; |
| 177 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 181 | val = cx22702_readreg(state, 0x02); |
| 182 | switch ((val & 0x38) >> 3) { |
| 183 | case 0: |
| 184 | p->code_rate_HP = FEC_1_2; |
| 185 | break; |
| 186 | case 1: |
| 187 | p->code_rate_HP = FEC_2_3; |
| 188 | break; |
| 189 | case 2: |
| 190 | p->code_rate_HP = FEC_3_4; |
| 191 | break; |
| 192 | case 3: |
| 193 | p->code_rate_HP = FEC_5_6; |
| 194 | break; |
| 195 | case 4: |
| 196 | p->code_rate_HP = FEC_7_8; |
| 197 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 199 | switch (val & 0x07) { |
| 200 | case 0: |
| 201 | p->code_rate_LP = FEC_1_2; |
| 202 | break; |
| 203 | case 1: |
| 204 | p->code_rate_LP = FEC_2_3; |
| 205 | break; |
| 206 | case 2: |
| 207 | p->code_rate_LP = FEC_3_4; |
| 208 | break; |
| 209 | case 3: |
| 210 | p->code_rate_LP = FEC_5_6; |
| 211 | break; |
| 212 | case 4: |
| 213 | p->code_rate_LP = FEC_7_8; |
| 214 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 217 | val = cx22702_readreg(state, 0x03); |
| 218 | switch ((val & 0x0c) >> 2) { |
| 219 | case 0: |
| 220 | p->guard_interval = GUARD_INTERVAL_1_32; |
| 221 | break; |
| 222 | case 1: |
| 223 | p->guard_interval = GUARD_INTERVAL_1_16; |
| 224 | break; |
| 225 | case 2: |
| 226 | p->guard_interval = GUARD_INTERVAL_1_8; |
| 227 | break; |
| 228 | case 3: |
| 229 | p->guard_interval = GUARD_INTERVAL_1_4; |
| 230 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 232 | switch (val & 0x03) { |
| 233 | case 0: |
| 234 | p->transmission_mode = TRANSMISSION_MODE_2K; |
| 235 | break; |
| 236 | case 1: |
| 237 | p->transmission_mode = TRANSMISSION_MODE_8K; |
| 238 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 244 | static int cx22702_i2c_gate_ctrl(struct dvb_frontend *fe, int enable) |
Steven Toth | 611900c | 2006-01-09 15:25:12 -0200 | [diff] [blame] | 245 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 246 | struct cx22702_state *state = fe->demodulator_priv; |
| 247 | dprintk("%s(%d)\n", __func__, enable); |
Steven Toth | 611900c | 2006-01-09 15:25:12 -0200 | [diff] [blame] | 248 | if (enable) |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 249 | return cx22702_writereg(state, 0x0D, |
| 250 | cx22702_readreg(state, 0x0D) & 0xfe); |
Steven Toth | 611900c | 2006-01-09 15:25:12 -0200 | [diff] [blame] | 251 | else |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 252 | return cx22702_writereg(state, 0x0D, |
| 253 | cx22702_readreg(state, 0x0D) | 1); |
Steven Toth | 611900c | 2006-01-09 15:25:12 -0200 | [diff] [blame] | 254 | } |
| 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | /* Talk to the demod, set the FEC, GUARD, QAM settings etc */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 257 | static int cx22702_set_tps(struct dvb_frontend *fe, |
| 258 | struct dvb_frontend_parameters *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | { |
| 260 | u8 val; |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 261 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Patrick Boettcher | dea7486 | 2006-05-14 05:01:31 -0300 | [diff] [blame] | 263 | if (fe->ops.tuner_ops.set_params) { |
| 264 | fe->ops.tuner_ops.set_params(fe, p); |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 265 | if (fe->ops.i2c_gate_ctrl) |
| 266 | fe->ops.i2c_gate_ctrl(fe, 0); |
Gerd Knorr | 9990d74 | 2005-05-01 08:59:20 -0700 | [diff] [blame] | 267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
| 269 | /* set inversion */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 270 | cx22702_set_inversion(state, p->inversion); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | |
| 272 | /* set bandwidth */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 273 | switch (p->u.ofdm.bandwidth) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | case BANDWIDTH_6_MHZ: |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 275 | cx22702_writereg(state, 0x0C, |
| 276 | (cx22702_readreg(state, 0x0C) & 0xcf) | 0x20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | break; |
| 278 | case BANDWIDTH_7_MHZ: |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 279 | cx22702_writereg(state, 0x0C, |
| 280 | (cx22702_readreg(state, 0x0C) & 0xcf) | 0x10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | break; |
| 282 | case BANDWIDTH_8_MHZ: |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 283 | cx22702_writereg(state, 0x0C, |
| 284 | cx22702_readreg(state, 0x0C) & 0xcf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | break; |
| 286 | default: |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 287 | dprintk("%s: invalid bandwidth\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | return -EINVAL; |
| 289 | } |
| 290 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 291 | p->u.ofdm.code_rate_LP = FEC_AUTO; /* temp hack as manual not working */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | /* use auto configuration? */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 294 | if ((p->u.ofdm.hierarchy_information == HIERARCHY_AUTO) || |
| 295 | (p->u.ofdm.constellation == QAM_AUTO) || |
| 296 | (p->u.ofdm.code_rate_HP == FEC_AUTO) || |
| 297 | (p->u.ofdm.code_rate_LP == FEC_AUTO) || |
| 298 | (p->u.ofdm.guard_interval == GUARD_INTERVAL_AUTO) || |
| 299 | (p->u.ofdm.transmission_mode == TRANSMISSION_MODE_AUTO)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
| 301 | /* TPS Source - use hardware driven values */ |
| 302 | cx22702_writereg(state, 0x06, 0x10); |
| 303 | cx22702_writereg(state, 0x07, 0x9); |
| 304 | cx22702_writereg(state, 0x08, 0xC1); |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 305 | cx22702_writereg(state, 0x0B, cx22702_readreg(state, 0x0B) |
| 306 | & 0xfc); |
| 307 | cx22702_writereg(state, 0x0C, |
| 308 | (cx22702_readreg(state, 0x0C) & 0xBF) | 0x40); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | cx22702_writereg(state, 0x00, 0x01); /* Begin aquisition */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 310 | dprintk("%s: Autodetecting\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | /* manually programmed values */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 315 | val = 0; |
| 316 | switch (p->u.ofdm.constellation) { |
| 317 | case QPSK: |
| 318 | val = (val & 0xe7); |
| 319 | break; |
| 320 | case QAM_16: |
| 321 | val = (val & 0xe7) | 0x08; |
| 322 | break; |
| 323 | case QAM_64: |
| 324 | val = (val & 0xe7) | 0x10; |
| 325 | break; |
| 326 | default: |
| 327 | dprintk("%s: invalid constellation\n", __func__); |
| 328 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 330 | switch (p->u.ofdm.hierarchy_information) { |
| 331 | case HIERARCHY_NONE: |
| 332 | val = (val & 0xf8); |
| 333 | break; |
| 334 | case HIERARCHY_1: |
| 335 | val = (val & 0xf8) | 1; |
| 336 | break; |
| 337 | case HIERARCHY_2: |
| 338 | val = (val & 0xf8) | 2; |
| 339 | break; |
| 340 | case HIERARCHY_4: |
| 341 | val = (val & 0xf8) | 3; |
| 342 | break; |
| 343 | default: |
| 344 | dprintk("%s: invalid hierarchy\n", __func__); |
| 345 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 347 | cx22702_writereg(state, 0x06, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 349 | val = 0; |
| 350 | switch (p->u.ofdm.code_rate_HP) { |
| 351 | case FEC_NONE: |
| 352 | case FEC_1_2: |
| 353 | val = (val & 0xc7); |
| 354 | break; |
| 355 | case FEC_2_3: |
| 356 | val = (val & 0xc7) | 0x08; |
| 357 | break; |
| 358 | case FEC_3_4: |
| 359 | val = (val & 0xc7) | 0x10; |
| 360 | break; |
| 361 | case FEC_5_6: |
| 362 | val = (val & 0xc7) | 0x18; |
| 363 | break; |
| 364 | case FEC_7_8: |
| 365 | val = (val & 0xc7) | 0x20; |
| 366 | break; |
| 367 | default: |
| 368 | dprintk("%s: invalid code_rate_HP\n", __func__); |
| 369 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 371 | switch (p->u.ofdm.code_rate_LP) { |
| 372 | case FEC_NONE: |
| 373 | case FEC_1_2: |
| 374 | val = (val & 0xf8); |
| 375 | break; |
| 376 | case FEC_2_3: |
| 377 | val = (val & 0xf8) | 1; |
| 378 | break; |
| 379 | case FEC_3_4: |
| 380 | val = (val & 0xf8) | 2; |
| 381 | break; |
| 382 | case FEC_5_6: |
| 383 | val = (val & 0xf8) | 3; |
| 384 | break; |
| 385 | case FEC_7_8: |
| 386 | val = (val & 0xf8) | 4; |
| 387 | break; |
| 388 | default: |
| 389 | dprintk("%s: invalid code_rate_LP\n", __func__); |
| 390 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 392 | cx22702_writereg(state, 0x07, val); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 394 | val = 0; |
| 395 | switch (p->u.ofdm.guard_interval) { |
| 396 | case GUARD_INTERVAL_1_32: |
| 397 | val = (val & 0xf3); |
| 398 | break; |
| 399 | case GUARD_INTERVAL_1_16: |
| 400 | val = (val & 0xf3) | 0x04; |
| 401 | break; |
| 402 | case GUARD_INTERVAL_1_8: |
| 403 | val = (val & 0xf3) | 0x08; |
| 404 | break; |
| 405 | case GUARD_INTERVAL_1_4: |
| 406 | val = (val & 0xf3) | 0x0c; |
| 407 | break; |
| 408 | default: |
| 409 | dprintk("%s: invalid guard_interval\n", __func__); |
| 410 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 412 | switch (p->u.ofdm.transmission_mode) { |
| 413 | case TRANSMISSION_MODE_2K: |
| 414 | val = (val & 0xfc); |
| 415 | break; |
| 416 | case TRANSMISSION_MODE_8K: |
| 417 | val = (val & 0xfc) | 1; |
| 418 | break; |
| 419 | default: |
| 420 | dprintk("%s: invalid transmission_mode\n", __func__); |
| 421 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | } |
| 423 | cx22702_writereg(state, 0x08, val); |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 424 | cx22702_writereg(state, 0x0B, |
| 425 | (cx22702_readreg(state, 0x0B) & 0xfc) | 0x02); |
| 426 | cx22702_writereg(state, 0x0C, |
| 427 | (cx22702_readreg(state, 0x0C) & 0xBF) | 0x40); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | /* Begin channel aquisition */ |
| 430 | cx22702_writereg(state, 0x00, 0x01); |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | /* Reset the demod hardware and reset all of the configuration registers |
| 436 | to a default state. */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 437 | static int cx22702_init(struct dvb_frontend *fe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { |
| 439 | int i; |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 440 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 442 | cx22702_writereg(state, 0x00, 0x02); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | |
| 444 | msleep(10); |
| 445 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 446 | for (i = 0; i < ARRAY_SIZE(init_tab); i += 2) |
| 447 | cx22702_writereg(state, init_tab[i], init_tab[i + 1]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 449 | cx22702_writereg(state, 0xf8, (state->config->output_mode << 1) |
| 450 | & 0x02); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Steven Toth | 611900c | 2006-01-09 15:25:12 -0200 | [diff] [blame] | 452 | cx22702_i2c_gate_ctrl(fe, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 457 | static int cx22702_read_status(struct dvb_frontend *fe, fe_status_t *status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 459 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | u8 reg0A; |
| 461 | u8 reg23; |
| 462 | |
| 463 | *status = 0; |
| 464 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 465 | reg0A = cx22702_readreg(state, 0x0A); |
| 466 | reg23 = cx22702_readreg(state, 0x23); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 468 | dprintk("%s: status demod=0x%02x agc=0x%02x\n" |
| 469 | , __func__, reg0A, reg23); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 471 | if (reg0A & 0x10) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | *status |= FE_HAS_LOCK; |
| 473 | *status |= FE_HAS_VITERBI; |
| 474 | *status |= FE_HAS_SYNC; |
| 475 | } |
| 476 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 477 | if (reg0A & 0x20) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | *status |= FE_HAS_CARRIER; |
| 479 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 480 | if (reg23 < 0xf0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | *status |= FE_HAS_SIGNAL; |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 486 | static int cx22702_read_ber(struct dvb_frontend *fe, u32 *ber) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 488 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 490 | if (cx22702_readreg(state, 0xE4) & 0x02) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | /* Realtime statistics */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 492 | *ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7 |
| 493 | | (cx22702_readreg(state, 0xDF) & 0x7F); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } else { |
| 495 | /* Averagtine statistics */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 496 | *ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7 |
| 497 | | cx22702_readreg(state, 0xDF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | return 0; |
| 501 | } |
| 502 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 503 | static int cx22702_read_signal_strength(struct dvb_frontend *fe, |
| 504 | u16 *signal_strength) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 506 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
Bradley Kite | 1e9dadb | 2006-09-02 21:14:27 -0300 | [diff] [blame] | 508 | u16 rs_ber = 0; |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 509 | rs_ber = cx22702_readreg(state, 0x23); |
Bradley Kite | 1e9dadb | 2006-09-02 21:14:27 -0300 | [diff] [blame] | 510 | *signal_strength = (rs_ber << 8) | rs_ber; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 515 | static int cx22702_read_snr(struct dvb_frontend *fe, u16 *snr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 517 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 519 | u16 rs_ber = 0; |
| 520 | if (cx22702_readreg(state, 0xE4) & 0x02) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | /* Realtime statistics */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 522 | rs_ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 7 |
| 523 | | (cx22702_readreg(state, 0xDF) & 0x7F); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | } else { |
| 525 | /* Averagine statistics */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 526 | rs_ber = (cx22702_readreg(state, 0xDE) & 0x7F) << 8 |
| 527 | | cx22702_readreg(state, 0xDF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | *snr = ~rs_ber; |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 534 | static int cx22702_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 536 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
| 538 | u8 _ucblocks; |
| 539 | |
| 540 | /* RS Uncorrectable Packet Count then reset */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 541 | _ucblocks = cx22702_readreg(state, 0xE3); |
Patrick Boettcher | f46dbb0 | 2005-07-07 17:57:44 -0700 | [diff] [blame] | 542 | if (state->prevUCBlocks < _ucblocks) |
| 543 | *ucblocks = (_ucblocks - state->prevUCBlocks); |
| 544 | else |
| 545 | *ucblocks = state->prevUCBlocks - _ucblocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | state->prevUCBlocks = _ucblocks; |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 551 | static int cx22702_get_frontend(struct dvb_frontend *fe, |
| 552 | struct dvb_frontend_parameters *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 554 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 556 | u8 reg0C = cx22702_readreg(state, 0x0C); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
| 558 | p->inversion = reg0C & 0x1 ? INVERSION_ON : INVERSION_OFF; |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 559 | return cx22702_get_tps(state, &p->u.ofdm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 562 | static int cx22702_get_tune_settings(struct dvb_frontend *fe, |
| 563 | struct dvb_frontend_tune_settings *tune) |
Patrick Boettcher | f46dbb0 | 2005-07-07 17:57:44 -0700 | [diff] [blame] | 564 | { |
| 565 | tune->min_delay_ms = 1000; |
| 566 | return 0; |
| 567 | } |
| 568 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 569 | static void cx22702_release(struct dvb_frontend *fe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 571 | struct cx22702_state *state = fe->demodulator_priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | kfree(state); |
| 573 | } |
| 574 | |
| 575 | static struct dvb_frontend_ops cx22702_ops; |
| 576 | |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 577 | struct dvb_frontend *cx22702_attach(const struct cx22702_config *config, |
| 578 | struct i2c_adapter *i2c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | { |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 580 | struct cx22702_state *state = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
| 582 | /* allocate memory for the internal state */ |
Matthias Schwarzott | 084e24a | 2009-08-10 22:51:01 -0300 | [diff] [blame] | 583 | state = kzalloc(sizeof(struct cx22702_state), GFP_KERNEL); |
Patrick Boettcher | f46dbb0 | 2005-07-07 17:57:44 -0700 | [diff] [blame] | 584 | if (state == NULL) |
| 585 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
| 587 | /* setup the state */ |
| 588 | state->config = config; |
| 589 | state->i2c = i2c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | state->prevUCBlocks = 0; |
| 591 | |
| 592 | /* check if the demod is there */ |
Patrick Boettcher | f46dbb0 | 2005-07-07 17:57:44 -0700 | [diff] [blame] | 593 | if (cx22702_readreg(state, 0x1f) != 0x3) |
| 594 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
| 596 | /* create dvb_frontend */ |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 597 | memcpy(&state->frontend.ops, &cx22702_ops, |
| 598 | sizeof(struct dvb_frontend_ops)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | state->frontend.demodulator_priv = state; |
| 600 | return &state->frontend; |
| 601 | |
| 602 | error: |
| 603 | kfree(state); |
| 604 | return NULL; |
| 605 | } |
Steven Toth | 4e3599a | 2008-10-16 20:23:45 -0300 | [diff] [blame] | 606 | EXPORT_SYMBOL(cx22702_attach); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
| 608 | static struct dvb_frontend_ops cx22702_ops = { |
| 609 | |
| 610 | .info = { |
| 611 | .name = "Conexant CX22702 DVB-T", |
| 612 | .type = FE_OFDM, |
| 613 | .frequency_min = 177000000, |
| 614 | .frequency_max = 858000000, |
| 615 | .frequency_stepsize = 166666, |
| 616 | .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 | |
| 617 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO | |
| 618 | FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO | |
| 619 | FE_CAN_HIERARCHY_AUTO | FE_CAN_GUARD_INTERVAL_AUTO | |
| 620 | FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER |
| 621 | }, |
| 622 | |
| 623 | .release = cx22702_release, |
| 624 | |
| 625 | .init = cx22702_init, |
Andrew de Quincey | 0244422 | 2006-04-18 17:47:09 -0300 | [diff] [blame] | 626 | .i2c_gate_ctrl = cx22702_i2c_gate_ctrl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
| 628 | .set_frontend = cx22702_set_tps, |
| 629 | .get_frontend = cx22702_get_frontend, |
Patrick Boettcher | f46dbb0 | 2005-07-07 17:57:44 -0700 | [diff] [blame] | 630 | .get_tune_settings = cx22702_get_tune_settings, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
| 632 | .read_status = cx22702_read_status, |
| 633 | .read_ber = cx22702_read_ber, |
| 634 | .read_signal_strength = cx22702_read_signal_strength, |
| 635 | .read_snr = cx22702_read_snr, |
| 636 | .read_ucblocks = cx22702_read_ucblocks, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | }; |
| 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | MODULE_DESCRIPTION("Conexant CX22702 DVB-T Demodulator driver"); |
| 640 | MODULE_AUTHOR("Steven Toth"); |
| 641 | MODULE_LICENSE("GPL"); |